Appiko
main.c
1 /*
2  * main.c : Application to transmit data over RF
3  * Copyright (C) 2019 Appiko
4  *
5  * This program is free software: you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation, either version 3 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program. If not, see <https://www.gnu.org/licenses/>.
17  */
18 
28 #include <stdbool.h>
29 #include <stdint.h>
30 #include "math.h"
31 
32 #include "nrf.h"
33 
34 #include "boards.h"
35 #include "hal_clocks.h"
36 #include "ms_timer.h"
37 #include "hal_spim.h"
38 #include "hal_gpio.h"
39 #include "hal_nop_delay.h"
40 #include "log.h"
41 #include "nrf_util.h"
42 #include "pin_trace.h"
43 #include "S2LP_Config.h"
45 #include "radio_drv.h"
46 #include "cc1x_utils.h"
47 #include "cc112x_def.h"
48 #include "spi_rf_nrf52.h"
49 
53 //SRadioInit xRadioInit =
54 //{
55 //
56 // .lBandwidth = BANDWIDTH,
57 // .lDatarate = DATARATE,
58 // .xModulationSelect = MODULATION_SELECT,
59 // .lFreqDev = FREQ_DEVIATION,
60 // .lFrequencyBase = BASE_FREQUENCY,
61 //
62 //
63 //};
64 //
65 //
67 //* @brief Packet Basic structure fitting
68 //*/
69 //PktBasicInit xBasicInit={
70 // PREAMBLE_LENGTH,
71 // SYNC_LENGTH,
72 // SYNC_WORD,
73 // VARIABLE_LENGTH,
74 // EXTENDED_LENGTH_FIELD,
75 // CRC_MODE,
76 // EN_ADDRESS,
77 // EN_FEC,
78 // EN_WHITENING
79 //};
80 //
81 //
83 //* @brief GPIO structure fitting
84 //*/
85 //SGpioInit xGpioIRQ={
86 // S2LP_GPIO_0,
87 // S2LP_GPIO_MODE_DIGITAL_OUTPUT_LP,
88 // S2LP_GPIO_DIG_OUT_READY
89 //};
90 
91 //S2LPIrqs myGpioIrq =
92 //{
93 //
94 // .IRQ_TX_DATA_SENT = 1,
95 //};
96 
97 #define GPIOTE_CHANNEL_USED 0
98 
99 #define GPIO_PIN GPIO0
100 
101 
102 
106 //S2LPIrqs xIrqStatus;
107 //volatile uint8_t arr_test[2];
108 volatile uint16_t test_cnt;
109 void ms_timer_handler ()
110 {
111 // log_printf("%s \n",__func__);
112 // S2LPCmdStrobeFlushTxFifo();
113 // S2LPSpiWriteFifo(sizeof(arr_test), arr_test);
114 // S2LPSpiWriteFifo(sizeof(test_cnt), (uint8_t *)&test_cnt);
115 // arr_test[1] = 0xCD;
116 // arr_test[0] = 0xAB;
117 // hal_gpio_pin_set (PA_EN_PIN);
118 // hal_nop_delay_us (500);
119 // radio_send ((uint8_t *)arr_test, sizeof(arr_test));
120 
121  test_cnt++;
122  radio_send ((uint8_t *)&test_cnt, sizeof(test_cnt));
123 // radio_transmit ();
124 // radio_prepare ((unsigned char *)arr_test, (uint16_t)sizeof(arr_test));
125 
126 // hal_nop_delay_ms(50);
127 // radio_idle ();
128  /* fit the TX FIFO */
129 
130  /* send the TX command */
131 // S2LPGpioInit(&xGpioIRQ);
132 // S2LPCmdStrobeTx();
133 }
134 
136 static void rgb_led_init(void)
137 {
138  hal_gpio_cfg_output(LED_RED, !(LEDS_ACTIVE_STATE));
139  hal_gpio_cfg_output(LED_GREEN, !(LEDS_ACTIVE_STATE));
140  hal_gpio_cfg_output(LED_BLUE, !(LEDS_ACTIVE_STATE));
141 }
142 
144 static void rgb_led_cycle(void)
145 {
146  hal_gpio_pin_write(LED_RED, (LEDS_ACTIVE_STATE));
147  hal_gpio_pin_write(LED_GREEN, !(LEDS_ACTIVE_STATE));
148  hal_gpio_pin_write(LED_BLUE, !(LEDS_ACTIVE_STATE));
149  hal_nop_delay_ms(250);
150  hal_gpio_pin_write(LED_RED, !(LEDS_ACTIVE_STATE));
151  hal_gpio_pin_write(LED_GREEN, (LEDS_ACTIVE_STATE));
152  hal_gpio_pin_write(LED_BLUE, !(LEDS_ACTIVE_STATE));
153  hal_nop_delay_ms(250);
154  hal_gpio_pin_write(LED_RED, !(LEDS_ACTIVE_STATE));
155  hal_gpio_pin_write(LED_GREEN, !(LEDS_ACTIVE_STATE));
156  hal_gpio_pin_write(LED_BLUE, (LEDS_ACTIVE_STATE));
157  hal_nop_delay_ms(250);
158  hal_gpio_pin_write(LED_RED, !(LEDS_ACTIVE_STATE));
159  hal_gpio_pin_write(LED_GREEN, !(LEDS_ACTIVE_STATE));
160  hal_gpio_pin_write(LED_BLUE, !(LEDS_ACTIVE_STATE));
161 }
162 
163 void ms_timer_10ms (void)
164 {
165 
166 }
168 // * Different calls to sleep depending on the status of Softdevice
169 // */
170 //void slumber(void)
171 //{
172 // uint8_t is_sd_enabled;
173 // sd_softdevice_is_enabled(&is_sd_enabled);
174 // // Would in the SENSING mode
175 // if(is_sd_enabled == 0)
176 // {
177 // __WFI();
178 // }
179 // else
180 // {
181 // sd_app_evt_wait();
182 // }
183 //}
184 //
185 
186 void GPIOTE_IRQHandler ()
187 {
188  log_printf("%s\n",__func__);
189  NRF_GPIOTE->EVENTS_IN[GPIOTE_CHANNEL_USED] = 0;
190  hal_gpio_pin_toggle (LED_RED);
191 // S2LPGpioInit(&xGpioIRQ);
192  if(radio_check_status_flag (MARC_NO_FAILURE) )
193  {
194  log_printf("Data sent\n");
195 // hal_gpio_pin_clear (PA_EN_PIN);
196  trxSpiCmdStrobe (SFTX);
197 
198 // S2LPGpioIrqClearStatus();
199  }
200 }
204 int main(void)
205 {
206  rgb_led_init();
207  rgb_led_cycle();
208  /* Initial printf */
209  log_init();
210  log_printf("Hello World from Long range RF Comm..!!\n");
211 
213  ms_timer_init(APP_IRQ_PRIORITY_LOWEST);
214 // for(uint8_t cnt = 0; cnt < ARRAY_SIZE(arr_test); cnt++)
215 // {
216 // arr_test[cnt] = cnt+1;
217 // }
218  test_cnt = 0;
219 // S2LPSpiInit ();
220 
221  radio_init(4);
222  radio_set_freq (915000);
223 // set_rf_packet_length ((unsigned char)sizeof(arr_test));
224 // radio_prepare ((unsigned char *)&test_cnt, (uint16_t)sizeof(test_cnt));
225  set_rf_packet_length (sizeof(test_cnt));
226  log_printf("Here..!!\n");
227  hal_gpio_cfg_output (PA_EN_PIN, 1);
228 
229  hal_gpio_cfg_input (GPIO_PIN, HAL_GPIO_PULL_DISABLED);
230  NRF_GPIOTE->CONFIG[GPIOTE_CHANNEL_USED] = ((GPIOTE_CONFIG_MODE_Event << GPIOTE_CONFIG_MODE_Pos) & GPIOTE_CONFIG_MODE_Msk) |
231  ((GPIOTE_CONFIG_POLARITY_HiToLo << GPIOTE_CONFIG_POLARITY_Pos)&GPIOTE_CONFIG_POLARITY_Msk) |
232  ((GPIO_PIN << GPIOTE_CONFIG_PSEL_Pos) & GPIOTE_CONFIG_PSEL_Msk);
233  NRF_GPIOTE->INTENSET = (GPIOTE_INTENSET_IN0_Enabled<<GPIOTE_INTENSET_IN0_Pos)&GPIOTE_INTENSET_IN0_Msk;
234 
235  ms_timer_start (MS_TIMER1, MS_REPEATED_CALL, MS_TIMER_TICKS_MS(1000), ms_timer_handler);
236 
237  NRF_GPIOTE->EVENTS_IN[GPIOTE_CHANNEL_USED] = 0;
238  NVIC_SetPriority (GPIOTE_IRQn, APP_IRQ_PRIORITY_LOW);
239  NVIC_ClearPendingIRQ (GPIOTE_IRQn);
240  NVIC_EnableIRQ (GPIOTE_IRQn);
241 
242 // ms_timer_start (MS_TIMER2, MS_REPEATED_CALL, MS_TIMER_TICKS_MS(10), ms_timer_10ms);
243  while(1)
244  {
245  __WFI ();
246  }
247 }
248 
Common configuration header file.
#define GPIOTE_CHANNEL_USED
Radio structure fitting.
Definition: main.c:97
#define LEDS_ACTIVE_STATE
Definition: bluey_1v1.h:44
void GPIOTE_IRQHandler()
Definition: main.c:192
void lfclk_init(lfclk_src_t lfclk_src)
Function to initialize the LF clock.
Definition: hal_clocks.c:23
Repeated call of the timer.
Definition: ms_timer.h:83
#define MS_TIMER_TICKS_MS(ms)
Definition: ms_timer.h:64
void ms_timer_init(uint32_t irq_priority)
Definition: ms_timer.c:111
volatile uint16_t test_cnt
IRQ status struct declaration.
Definition: main.c:108
Millisecond Timer 1.
Definition: ms_timer.h:70
S2LP Configuration and useful defines .
void ms_timer_start(ms_timer_num id, ms_timer_mode mode, uint64_t ticks, void(*handler)(void))
Definition: ms_timer.c:134
HAL_GPIO_PULL_DISABLED.
Definition: hal_gpio.h:37
int main(void)
Function for application main entry.
Definition: main.c:90
External crystal.
Definition: hal_clocks.h:38