Appiko
evt_sd_handler.c
1 
19 #include "stddef.h"
20 #include "stdbool.h"
21 #include "nrf_sdm.h"
22 #include "app_error.h"
23 #include "common_util.h"
24 #include "stdint.h"
25 
26 #include "evt_sd_handler.h"
27 #include "nrf_assert.h"
28 #include "nrf_nvic.h"
29 
30 #if ISR_MANAGER == 1
31 #include "isr_manager.h"
32 #endif
33 
34 #define SWI_USED SWI_USED_EVT_SD_HANDLER
35 
39 #define SWI_ID CONCAT_2(NRF_SWI, SWI_USED)
40 #define SWI_IRQN SWI_IRQN_a(SWI_USED)
41 #define SWI_IRQ_Handler SWI_IRQ_Handler_a(SWI_USED)
42 
43 #define SWI_IRQN_a(n) SWI_IRQN_b(n)
44 #define SWI_IRQN_b(n) SWI##n##_EGU##n##_IRQn
45 
46 #define SWI_IRQ_Handler_a(n) SWI_IRQ_Handler_b(n)
47 #define SWI_IRQ_Handler_b(n) SWI##n##_EGU##n##_IRQHandler
48 
50 uint32_t ble_evt_buffer[
52  CEIL_DIV(BLE_EVT_LEN_MAX(BLE_GATT_ATT_MTU_DEFAULT)
53  ,sizeof(uint32_t))];
54 
55 void (*ble_handler)(ble_evt_t * evt);
56 void (*soc_handler)(uint32_t evt);
57 
59  (void (* ble_evt_handler)(ble_evt_t * ble_evt),
60  void (* soc_evt_handler)(uint32_t soc_evt_id))
61 {
62  ASSERT(ble_evt_handler != NULL);
63  ASSERT(soc_evt_handler != NULL);
64  ble_handler = ble_evt_handler;
65  soc_handler = soc_evt_handler;
66 
67  uint32_t err_code = sd_nvic_EnableIRQ(SWI_IRQN);
68  APP_ERROR_CHECK(err_code);
69 }
70 
71 #if ISR_MANAGER == 1
72 void evt_sd_handler_swi_Handler ()
73 #else
74 void SWI_IRQ_Handler(void)
75 #endif
76 {
77  bool soc_evts_handled = false;
78  while(soc_evts_handled == false)
79  {
80  uint32_t err_code, evt_id;
81  err_code = sd_evt_get(&evt_id);
82  if (err_code == NRF_ERROR_NOT_FOUND)
83  {
84  //No more events
85  soc_evts_handled = true;
86  }
87  else if (err_code != NRF_SUCCESS)
88  {
89  APP_ERROR_CHECK(err_code);
90  }
91  else
92  {
93  // Call the SoC event handler.
94  soc_handler(evt_id);
95  }
96  }
97 
98  bool ble_evts_handled = false;
99  while(ble_evts_handled == false)
100  {
101  // Pull event from stack
102  uint16_t evt_len = sizeof(ble_evt_buffer);
103 
104  uint32_t err_code = sd_ble_evt_get((uint8_t*)ble_evt_buffer, &evt_len);
105  if (err_code == NRF_ERROR_NOT_FOUND)
106  {
107  ble_evts_handled = true;
108  }
109  else if (err_code != NRF_SUCCESS)
110  {
111  APP_ERROR_CHECK(err_code);
112  }
113  else
114  {
115  // Call the BLE stack event handler.
116  ble_handler((ble_evt_t *) ble_evt_buffer);
117  }
118  }
119 }
void evt_sd_handler_init(void(*ble_evt_handler)(ble_evt_t *ble_evt), void(*soc_evt_handler)(uint32_t soc_evt_id))
Initializes the SWI2 interrupt routine and stores the handlers for the BLE and SoC events.
#define APP_ERROR_CHECK(ERR_CODE)
Macro calls error handler if the provided error code is not NRF_SUCCESS. The behavior of this call de...
Definition: app_error.h:55
#define ASSERT(expression)
Macro for runtime assertion of an expression. If the expression is false the assert_nrf_callback func...
Definition: nrf_assert.h:48
#define CEIL_DIV(A, B)
When the result of a division is not an integer, the result is rounded up to the next integer.
Definition: common_util.h:90