Appiko
main.c
1 /*
2  * main.c : Application to receive 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 #include "nrf_nvic.h"
34 
35 #include "boards.h"
36 #include "hal_clocks.h"
37 #include "ms_timer.h"
38 #include "hal_spim.h"
39 #include "hal_gpio.h"
40 #include "hal_nop_delay.h"
41 #include "log.h"
42 #include "nrf_util.h"
43 #include "pin_trace.h"
44 #include "rf_rx_ble.h"
45 #include "ble.h"
46 #include "nrf_soc.h"
47 #include "nrf_sdm.h"
48 #include "radio_drv.h"
49 #include "cc1x_utils.h"
50 #include "cc112x_def.h"
51 
52 
56 uint8_t vectcRxBuff[128], cRxData;
57 
58 uint16_t current_pkt_no;
59 
60 volatile bool is_timer_on = false;
61 
62 uint16_t pkt_no = 0;
63 
64 uint16_t start_pkt_no = 0;
65 
66 int32_t rssi_sum = 0;
67 
71 #define GPIOTE_CHANNEL_USED 0
72 
73 #define GPIO_PIN GPIO0
74 
75 #define TEST_DURATION_S 100
76 
77 #define TEST_DURATION_MS TEST_DURATION_S*1000
78 
82 //S2LPIrqs xIrqStatus;
83 
84 
85 //static volatile bool rx_started = false;
86 
87 static mod_ble_data_t ble_data;
88 
89 static volatile bool is_connected = false;
90 void ms_timer_handler ()
91 {
92  log_printf ("Packets dropped in %d sec : %d\n",TEST_DURATION_S, TEST_DURATION_S - pkt_no);
93  log_printf ("Avg RSSI : %d\n", (int8_t)(rssi_sum/pkt_no));
94  log_printf ("Test params :\n");
95 // log_printf ("%d, %d, %d, %d, %d, %d\n", MODULATION_SELECT, DATARATE, FREQ_DEVIATION,
96 // BANDWIDTH, PREAMBLE_LENGTH, SYNC_LENGTH);
97 
98  log_printf ("Packet no.s : S %d, C %d\n", start_pkt_no, current_pkt_no);
99  is_timer_on = false;
100 
101 }
102 
104 static void rgb_led_init(void)
105 {
106  hal_gpio_cfg_output(LED_RED, !(LEDS_ACTIVE_STATE));
107  hal_gpio_cfg_output(LED_GREEN, !(LEDS_ACTIVE_STATE));
108  hal_gpio_cfg_output(LED_BLUE, !(LEDS_ACTIVE_STATE));
109 }
110 
112 static void rgb_led_cycle(void)
113 {
114  hal_gpio_pin_write(LED_RED, (LEDS_ACTIVE_STATE));
115  hal_gpio_pin_write(LED_GREEN, !(LEDS_ACTIVE_STATE));
116  hal_gpio_pin_write(LED_BLUE, !(LEDS_ACTIVE_STATE));
117  hal_nop_delay_ms(50);
118  hal_gpio_pin_write(LED_RED, !(LEDS_ACTIVE_STATE));
119  hal_gpio_pin_write(LED_GREEN, (LEDS_ACTIVE_STATE));
120  hal_gpio_pin_write(LED_BLUE, !(LEDS_ACTIVE_STATE));
121  hal_nop_delay_ms(50);
122  hal_gpio_pin_write(LED_RED, !(LEDS_ACTIVE_STATE));
123  hal_gpio_pin_write(LED_GREEN, !(LEDS_ACTIVE_STATE));
124  hal_gpio_pin_write(LED_BLUE, (LEDS_ACTIVE_STATE));
125  hal_nop_delay_ms(50);
126  hal_gpio_pin_write(LED_RED, !(LEDS_ACTIVE_STATE));
127  hal_gpio_pin_write(LED_GREEN, !(LEDS_ACTIVE_STATE));
128  hal_gpio_pin_write(LED_BLUE, !(LEDS_ACTIVE_STATE));
129 }
130 
131 void ms_timer_10ms (void)
132 {
133 }
134 
135 void on_connect ()
136 {
137  is_connected = true;
138 }
140 // * Different calls to sleep depending on the status of Softdevice
141 // */
142 void slumber(void)
143 {
144  uint8_t is_sd_enabled;
145  sd_softdevice_is_enabled(&is_sd_enabled);
146  // Would in the SENSING mode
147  if(is_sd_enabled == 0)
148  {
149  __WFI();
150  }
151  else
152  {
153  sd_app_evt_wait();
154  }
155 }
156 
157 void start_rx(void)
158 {
159  bool break_now = false;
160  do{
161 // S2LPGpioInit(&xGpioIRQ);
162 // S2LPCmdStrobeRx();
163 
164 // S2LPGpioIrqGetStatus((S2LPIrqs*) &irqs);
165 // log_printf ("Rx? I0x%X S0x%X", irqs, g_xStatus);
166 // if(g_xStatus.MC_STATE == MC_STATE_RX)
167 // if(radio_check_status_flag (MARC_NO_FAILURE))
168  {
169  break_now = true;
170 // log_printf(" Yay\n");
171  }
172  radio_receive_on ();
173 // else
174 // {
175 // log_printf(" Nay\n");
176 // log_printf("Reset..!!");
177 // uint8_t is_sd_enabled;
178 // sd_softdevice_is_enabled(&is_sd_enabled);
179 // if(is_sd_enabled == 0)
180 // {
181 // sd_nvic_SystemReset();
182 // }
183 // else
184 // {
185 // NVIC_SystemReset ();
186 // }
187 // }
188 
189  } while(break_now == false);
190 }
191 
193 {
194  NRF_GPIOTE->EVENTS_IN[GPIOTE_CHANNEL_USED] = 0;
195 // log_printf("%s\n",__func__);
196  if(radio_check_status_flag (MARC_NO_FAILURE) )
197  {
198 // hal_nop_delay_ms(100);
199 // rx_started = true;
200 // log_printf("Data Recieved : %d dBm\n", S2LPRadioGetRssidBm ());
201 // if(rx_started == false)
202 // {
203 // ms_timer_start (MS_TIMER1, MS_REPEATED_CALL, MS_TIMER_TICKS_MS(50), ms_timer_handler);
204 // }
205 // rx_started = true;
206 
207  hal_gpio_pin_toggle (LED_BLUE);
208 
209  /* Read the RX FIFO */
210 // S2LPSpiReadFifo(cRxData, (uint8_t *)&current_pkt_no);
211 
212  cRxData = sizeof(vectcRxBuff);
213  radio_read (vectcRxBuff, (uint8_t *)&cRxData);
214  /* Flush the RX FIFO */
215 // for(uint32_t i =0; i < cRxData; i++)
216 // {
217 // log_printf("%x ", vectcRxBuff[i]);
218 // }
219 // log_printf("\n");
220 
221  current_pkt_no = vectcRxBuff[0] | (vectcRxBuff[1] << 8);
222  pkt_no++;
223  if(is_timer_on == false)
224  {
225  ms_timer_start (MS_TIMER0, MS_SINGLE_CALL, MS_TIMER_TICKS_MS(TEST_DURATION_MS), ms_timer_handler);
226  is_timer_on = true;
227  pkt_no = 0;
228  rssi_sum = 0;
229  start_pkt_no = current_pkt_no;
230  }
231  log_printf("Test Val : %d\n", current_pkt_no);
232 // S2LPGpioIrqClearStatus();
233  log_printf("RSSI : %d\n", (int8_t)radio_get_rssi ());
234 // if(is_connected)
235  {
236  ble_data.rf_rx_rssi = (uint8_t)radio_get_rssi ();
237  ble_data.pkt_no = current_pkt_no;
238  ble_data.CRC_ERR = (uint8_t) radio_check_status_flag (MARC_PKT_DISC_CRC);
239  rf_rx_ble_update_status_byte (&ble_data);
240  }
241  rssi_sum += ble_data.rf_rx_rssi;
242  }
243  else
244  {
245 // uint32_t irqs;
246 // S2LPGpioIrqGetStatus((S2LPIrqs*) &irqs);
247 // S2LPRefreshStatus();
248  log_printf ("*** Data not ready.\n");
249  }
250 
251  start_rx();
252 }
256 int main(void)
257 {
259  /* Initial printf */
260  log_init();
261  log_printf("Hello World from RF_RX..!!\n");
262 
263 #if DC_DC_CIRCUITRY == true //Defined in the board header file
264  NRF_POWER->DCDCEN = POWER_DCDCEN_DCDCEN_Disabled << POWER_DCDCEN_DCDCEN_Pos;
265 #endif
266  NRF_POWER->TASKS_LOWPWR = 1;
267 
268  ms_timer_init(APP_IRQ_PRIORITY_LOWEST);
269 // S2LPSpiInit ();
270  rgb_led_init();
271  rgb_led_cycle();
272 // hal_gpio_cfg_output (SDN,0);
273 // hal_gpio_pin_set (SDN);
274 // hal_nop_delay_ms (1);
275 // hal_gpio_pin_clear (SDN);
276  radio_init(4);
277  radio_set_freq (915000);
278  set_rf_packet_length (2);
279 
280  hal_gpio_cfg_output (LNA_EN_PIN, 1);
281  hal_gpio_cfg_input (GPIO_PIN, HAL_GPIO_PULL_DOWN);
282  NRF_GPIOTE->CONFIG[GPIOTE_CHANNEL_USED] = ((GPIOTE_CONFIG_MODE_Event << GPIOTE_CONFIG_MODE_Pos) & GPIOTE_CONFIG_MODE_Msk) |
283  ((GPIOTE_CONFIG_POLARITY_HiToLo << GPIOTE_CONFIG_POLARITY_Pos)&GPIOTE_CONFIG_POLARITY_Msk) |
284  ((GPIO_PIN << GPIOTE_CONFIG_PSEL_Pos) & GPIOTE_CONFIG_PSEL_Msk);
285  NRF_GPIOTE->INTENSET = (GPIOTE_INTENSET_IN0_Enabled<<GPIOTE_INTENSET_IN0_Pos)&GPIOTE_INTENSET_IN0_Msk;
286 
287 // S2LPGpioInit(&xGpioIRQ);
288 // S2LPRadioInit(&xRadioInit);
293 // S2LPPktBasicInit(&xBasicInit);
294 // S2LPGpioIrqDeInit(NULL);
295 // {
298 //
299 // /* payload length config */
300 // S2LPPktBasicSetPayloadLength(2);
301 //
302 // /* RX timeout config */
303 // SET_INFINITE_RX_TIMEOUT();
304 // }
305 //
306 //
307 // S2LPGpioIrqClearStatus();
308 
309  rf_rx_ble_stack_init ();
310  rf_rx_ble_gap_params_init ();
311  rf_rx_ble_adv_init ();
312  rf_rx_ble_adv_start (on_connect);
313  rf_rx_ble_service_init ();
314 
315  ble_data.rf_rx_rssi = 0;
316  rf_rx_ble_update_status_byte (&ble_data);
317 
318  start_rx();
319 
320  log_printf("Here..!!\n");
321  NRF_GPIOTE->EVENTS_IN[GPIOTE_CHANNEL_USED] = 0;
322  NVIC_SetPriority (GPIOTE_IRQn, APP_IRQ_PRIORITY_LOW);
323  NVIC_ClearPendingIRQ (GPIOTE_IRQn);
324  NVIC_EnableIRQ (GPIOTE_IRQn);
325  while(1)
326  {
327  slumber ();
328  }
329 }
330 
#define GPIOTE_CHANNEL_USED
Preemption priority IRQ.
Definition: main.c:71
#define LEDS_ACTIVE_STATE
Definition: bluey_1v1.h:44
uint8_t vectcRxBuff[128]
Rx buffer declaration: how to store the received data.
Definition: main.c:56
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
#define MS_TIMER_TICKS_MS(ms)
Definition: ms_timer.h:64
void ms_timer_init(uint32_t irq_priority)
Definition: ms_timer.c:111
Millisecond Timer 0.
Definition: ms_timer.h:69
void ms_timer_start(ms_timer_num id, ms_timer_mode mode, uint64_t ticks, void(*handler)(void))
Definition: ms_timer.c:134
void slumber(void)
Definition: main.c:180
int main(void)
Function for application main entry.
Definition: main.c:90
HAL_GPIO_PULL_DOWN.
Definition: hal_gpio.h:38
External crystal.
Definition: hal_clocks.h:38
One shot call of the timer.
Definition: ms_timer.h:82