ser_softdevice_handler.c 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311
  1. /**
  2. * Copyright (c) 2014 - 2020, Nordic Semiconductor ASA
  3. *
  4. * All rights reserved.
  5. *
  6. * Redistribution and use in source and binary forms, with or without modification,
  7. * are permitted provided that the following conditions are met:
  8. *
  9. * 1. Redistributions of source code must retain the above copyright notice, this
  10. * list of conditions and the following disclaimer.
  11. *
  12. * 2. Redistributions in binary form, except as embedded into a Nordic
  13. * Semiconductor ASA integrated circuit in a product or a software update for
  14. * such product, must reproduce the above copyright notice, this list of
  15. * conditions and the following disclaimer in the documentation and/or other
  16. * materials provided with the distribution.
  17. *
  18. * 3. Neither the name of Nordic Semiconductor ASA nor the names of its
  19. * contributors may be used to endorse or promote products derived from this
  20. * software without specific prior written permission.
  21. *
  22. * 4. This software, with or without modification, must only be used with a
  23. * Nordic Semiconductor ASA integrated circuit.
  24. *
  25. * 5. Any software provided in binary form under this license must not be reverse
  26. * engineered, decompiled, modified and/or disassembled.
  27. *
  28. * THIS SOFTWARE IS PROVIDED BY NORDIC SEMICONDUCTOR ASA "AS IS" AND ANY EXPRESS
  29. * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  30. * OF MERCHANTABILITY, NONINFRINGEMENT, AND FITNESS FOR A PARTICULAR PURPOSE ARE
  31. * DISCLAIMED. IN NO EVENT SHALL NORDIC SEMICONDUCTOR ASA OR CONTRIBUTORS BE
  32. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  33. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
  34. * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  35. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  36. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
  37. * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  38. *
  39. */
  40. #include <stdlib.h>
  41. #include <string.h>
  42. #include "nrf_queue.h"
  43. #include "app_scheduler.h"
  44. #include "nrf_sdh.h"
  45. #include "nrf_sdm.h"
  46. #include "ser_sd_transport.h"
  47. #include "ser_app_hal.h"
  48. #include "ser_config.h"
  49. #include "nrf_soc.h"
  50. #include "ble_serialization.h"
  51. #if defined(BLE_STACK_SUPPORT_REQD)
  52. #include "ble_app.h"
  53. #include "nrf_sdh_ble.h"
  54. #endif
  55. #if defined(ANT_STACK_SUPPORT_REQD)
  56. #include "ant_event.h"
  57. #endif
  58. #define SD_BLE_EVT_MAILBOX_QUEUE_SIZE 5 /**< Size of mailbox queue. */
  59. /** @brief Structure used to pass packet details through mailbox.
  60. */
  61. #if defined(BLE_STACK_SUPPORT_REQD)
  62. typedef struct
  63. {
  64. //lint -save -e666
  65. uint32_t evt_data[CEIL_DIV(NRF_SDH_BLE_EVT_BUF_SIZE, sizeof (uint32_t))]; /**< Buffer for decoded event */
  66. //lint -restore
  67. } ser_sd_handler_evt_data_t;
  68. #endif
  69. #if defined(ANT_STACK_SUPPORT_REQD)
  70. typedef struct
  71. {
  72. uint32_t evt_data[CEIL_DIV(sizeof(ant_evt_t), sizeof (uint32_t))]; /**< Buffer for decoded event */
  73. } ser_ant_sd_handler_evt_data_t;
  74. #endif
  75. /** @brief
  76. * Mailbox used for communication between event handler (called from serial stream
  77. * interrupt context) and event processing function (called from scheduler or interrupt context).
  78. */
  79. #if defined(BLE_STACK_SUPPORT_REQD)
  80. NRF_QUEUE_DEF(ser_sd_handler_evt_data_t,
  81. m_sd_ble_evt_mailbox,
  82. SD_BLE_EVT_MAILBOX_QUEUE_SIZE,
  83. NRF_QUEUE_MODE_NO_OVERFLOW);
  84. #endif
  85. #if defined(ANT_STACK_SUPPORT_REQD)
  86. NRF_QUEUE_DEF(ser_ant_sd_handler_evt_data_t,
  87. m_sd_ant_evt_mailbox,
  88. SD_BLE_EVT_MAILBOX_QUEUE_SIZE,
  89. NRF_QUEUE_MODE_NO_OVERFLOW);
  90. #endif
  91. NRF_QUEUE_DEF(uint32_t,
  92. m_sd_soc_evt_mailbox,
  93. SD_BLE_EVT_MAILBOX_QUEUE_SIZE,
  94. NRF_QUEUE_MODE_NO_OVERFLOW);
  95. /**
  96. * @brief Function to be replaced by user implementation if needed.
  97. *
  98. * Weak function - user can add different implementation of this function if application needs it.
  99. */
  100. __WEAK void os_rsp_set_handler(void)
  101. {
  102. }
  103. static void connectivity_reset_low(void)
  104. {
  105. //Signal a reset to the connectivity chip by setting the reset pin low.
  106. ser_app_hal_nrf_reset_pin_clear();
  107. ser_app_hal_delay(CONN_CHIP_RESET_TIME);
  108. }
  109. static void connectivity_reset_high(void)
  110. {
  111. //Set the reset level to high again.
  112. ser_app_hal_nrf_reset_pin_set();
  113. //Wait for connectivity chip to be ready.
  114. ser_app_hal_delay(CONN_CHIP_WAKEUP_TIME);
  115. }
  116. #if defined(BLE_STACK_SUPPORT_REQD)
  117. static void ser_softdevice_ble_evt_handler(uint8_t * p_data, uint16_t length)
  118. {
  119. ser_sd_handler_evt_data_t item;
  120. uint32_t err_code;
  121. uint32_t len32 = sizeof (item.evt_data);
  122. err_code = ble_event_dec(p_data, length, (ble_evt_t *)item.evt_data, &len32);
  123. APP_ERROR_CHECK(err_code);
  124. err_code = ser_sd_transport_rx_free(p_data);
  125. APP_ERROR_CHECK(err_code);
  126. err_code = nrf_queue_push(&m_sd_ble_evt_mailbox, &item);
  127. APP_ERROR_CHECK(err_code);
  128. ser_app_hal_nrf_evt_pending();
  129. }
  130. #endif
  131. #if defined(ANT_STACK_SUPPORT_REQD)
  132. static void ser_softdevice_ant_evt_handler(uint8_t * p_data, uint16_t length)
  133. {
  134. ser_ant_sd_handler_evt_data_t item;
  135. uint32_t err_code;
  136. uint32_t len32 = sizeof (item.evt_data);
  137. err_code = ant_event_dec(p_data, length, (ant_evt_t *)item.evt_data, &len32);
  138. APP_ERROR_CHECK(err_code);
  139. err_code = ser_sd_transport_rx_free(p_data);
  140. APP_ERROR_CHECK(err_code);
  141. err_code = nrf_queue_push(&m_sd_ant_evt_mailbox, &item);
  142. APP_ERROR_CHECK(err_code);
  143. ser_app_hal_nrf_evt_pending();
  144. }
  145. #endif
  146. void ser_softdevice_flash_operation_success_evt(bool success)
  147. {
  148. uint32_t evt_type = success ? NRF_EVT_FLASH_OPERATION_SUCCESS :
  149. NRF_EVT_FLASH_OPERATION_ERROR;
  150. uint32_t err_code = nrf_queue_push(&m_sd_soc_evt_mailbox, &evt_type);
  151. APP_ERROR_CHECK(err_code);
  152. ser_app_hal_nrf_evt_pending();
  153. }
  154. /**
  155. * @brief Function called while waiting for connectivity chip response. It handles incoming events.
  156. */
  157. static void ser_sd_rsp_wait(void)
  158. {
  159. do
  160. {
  161. (void)sd_app_evt_wait();
  162. //intern_softdevice_events_execute();
  163. }
  164. while (ser_sd_transport_is_busy());
  165. }
  166. uint32_t sd_evt_get(uint32_t * p_evt_id)
  167. {
  168. return nrf_queue_pop(&m_sd_soc_evt_mailbox, p_evt_id);
  169. }
  170. #if defined(BLE_STACK_SUPPORT_REQD)
  171. uint32_t sd_ble_evt_get(uint8_t * p_data, uint16_t * p_len)
  172. {
  173. uint32_t err_code = nrf_queue_pop(&m_sd_ble_evt_mailbox, p_data);
  174. if (err_code == NRF_SUCCESS) //if anything in the mailbox
  175. {
  176. if (((ble_evt_t *)p_data)->header.evt_len > *p_len)
  177. {
  178. err_code = NRF_ERROR_DATA_SIZE;
  179. }
  180. else
  181. {
  182. *p_len = ((ble_evt_t *)p_data)->header.evt_len;
  183. }
  184. }
  185. else
  186. {
  187. err_code = NRF_ERROR_NOT_FOUND;
  188. }
  189. return err_code;
  190. }
  191. #endif
  192. #if defined(ANT_STACK_SUPPORT_REQD)
  193. uint32_t sd_ant_event_get(uint8_t* p_channel, uint8_t* p_event, uint8_t* p_ant_mesg)
  194. {
  195. SER_ASSERT_NOT_NULL(p_channel);
  196. SER_ASSERT_NOT_NULL(p_event);
  197. SER_ASSERT_NOT_NULL(p_ant_mesg);
  198. uint32_t err_code;
  199. ser_ant_sd_handler_evt_data_t item;
  200. err_code = nrf_queue_pop(&m_sd_ant_evt_mailbox, &item);
  201. if (err_code == NRF_SUCCESS)
  202. {
  203. *p_event = ((ant_evt_t *)item.evt_data) -> event;
  204. *p_channel = ((ant_evt_t *)item.evt_data) -> channel;
  205. memcpy(p_ant_mesg, ((ant_evt_t *)item.evt_data)->message.aucMessage, MESG_BUFFER_SIZE);
  206. } else {
  207. err_code = NRF_ERROR_NOT_FOUND;
  208. }
  209. return err_code;
  210. }
  211. #endif
  212. #if defined(BLE_STACK_SUPPORT_REQD)
  213. uint32_t sd_ble_evt_mailbox_length_get(uint32_t * p_mailbox_length)
  214. {
  215. *p_mailbox_length = nrf_queue_utilization_get(&m_sd_ble_evt_mailbox);
  216. return NRF_SUCCESS;
  217. }
  218. #endif
  219. #if (defined(S332) || defined(S212))
  220. uint32_t sd_softdevice_enable(nrf_clock_lf_cfg_t const * p_clock_lf_cfg,
  221. nrf_fault_handler_t fault_handler,
  222. const char* p_license_key)
  223. #else
  224. uint32_t sd_softdevice_enable(nrf_clock_lf_cfg_t const * p_clock_lf_cfg,
  225. nrf_fault_handler_t assertion_handler)
  226. #endif
  227. {
  228. uint32_t err_code;
  229. err_code = ser_app_hal_hw_init(ser_softdevice_flash_operation_success_evt);
  230. if (err_code == NRF_SUCCESS)
  231. {
  232. connectivity_reset_low();
  233. nrf_queue_reset(&m_sd_soc_evt_mailbox);
  234. ser_sd_transport_evt_handler_t ble_evt_handler = NULL;
  235. ser_sd_transport_evt_handler_t ant_evt_handler = NULL;
  236. #ifdef BLE_STACK_SUPPORT_REQD
  237. ble_evt_handler = ser_softdevice_ble_evt_handler;
  238. nrf_queue_reset(&m_sd_ble_evt_mailbox);
  239. #endif // BLE_STACK_SUPPORT_REQD
  240. #ifdef ANT_STACK_SUPPORT_REQD
  241. ant_evt_handler = ser_softdevice_ant_evt_handler;
  242. nrf_queue_reset(&m_sd_ant_evt_mailbox);
  243. #endif // ANT_STACK_SUPPORT_REQD
  244. err_code = ser_sd_transport_open(ble_evt_handler,
  245. ant_evt_handler,
  246. ser_sd_rsp_wait,
  247. os_rsp_set_handler,
  248. NULL);
  249. if (err_code == NRF_SUCCESS)
  250. {
  251. connectivity_reset_high();
  252. }
  253. ser_app_hal_nrf_evt_irq_priority_set();
  254. }
  255. return err_code;
  256. }
  257. uint32_t sd_softdevice_disable(void)
  258. {
  259. return ser_sd_transport_close();
  260. }