nrf_sdh.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415
  1. /**
  2. * Copyright (c) 2017 - 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 "sdk_common.h"
  41. #if NRF_MODULE_ENABLED(NRF_SDH)
  42. #include "nrf_sdh.h"
  43. #include <stdint.h>
  44. #include "nrf_sdm.h"
  45. #include "nrf_nvic.h"
  46. #include "sdk_config.h"
  47. #include "app_error.h"
  48. #include "app_util_platform.h"
  49. #define NRF_LOG_MODULE_NAME nrf_sdh
  50. #if NRF_SDH_LOG_ENABLED
  51. #define NRF_LOG_LEVEL NRF_SDH_LOG_LEVEL
  52. #define NRF_LOG_INFO_COLOR NRF_SDH_INFO_COLOR
  53. #define NRF_LOG_DEBUG_COLOR NRF_SDH_DEBUG_COLOR
  54. #else
  55. #define NRF_LOG_LEVEL 0
  56. #endif // NRF_SDH_LOG_ENABLED
  57. #include "nrf_log.h"
  58. NRF_LOG_MODULE_REGISTER();
  59. // Validate configuration options.
  60. #if (NRF_SDH_DISPATCH_MODEL == NRF_SDH_DISPATCH_MODEL_APPSH)
  61. #if (!APP_SCHEDULER_ENABLED)
  62. #error app_scheduler is required when NRF_SDH_DISPATCH_MODEL is set to NRF_SDH_DISPATCH_MODEL_APPSH
  63. #endif
  64. #include "app_scheduler.h"
  65. #endif
  66. #if ( (NRF_SDH_CLOCK_LF_SRC == NRF_CLOCK_LF_SRC_RC) \
  67. && (NRF_SDH_CLOCK_LF_ACCURACY != NRF_CLOCK_LF_ACCURACY_500_PPM))
  68. #warning Please select NRF_CLOCK_LF_ACCURACY_500_PPM when using NRF_CLOCK_LF_SRC_RC
  69. #endif
  70. // Create section "sdh_req_observers".
  71. NRF_SECTION_SET_DEF(sdh_req_observers, nrf_sdh_req_observer_t, NRF_SDH_REQ_OBSERVER_PRIO_LEVELS);
  72. // Create section "sdh_state_observers".
  73. NRF_SECTION_SET_DEF(sdh_state_observers, nrf_sdh_state_observer_t, NRF_SDH_STATE_OBSERVER_PRIO_LEVELS);
  74. // Create section "sdh_stack_observers".
  75. NRF_SECTION_SET_DEF(sdh_stack_observers, nrf_sdh_stack_observer_t, NRF_SDH_STACK_OBSERVER_PRIO_LEVELS);
  76. static bool m_nrf_sdh_enabled; /**< Variable to indicate whether the SoftDevice is enabled. */
  77. static bool m_nrf_sdh_suspended; /**< Variable to indicate whether this module is suspended. */
  78. static bool m_nrf_sdh_continue; /**< Variable to indicate whether enable/disable process was started. */
  79. /**@brief Function for notifying request observers.
  80. *
  81. * @param[in] evt Type of request event.
  82. */
  83. static ret_code_t sdh_request_observer_notify(nrf_sdh_req_evt_t req)
  84. {
  85. nrf_section_iter_t iter;
  86. NRF_LOG_DEBUG("State request: 0x%08X", req);
  87. for (nrf_section_iter_init(&iter, &sdh_req_observers);
  88. nrf_section_iter_get(&iter) != NULL;
  89. nrf_section_iter_next(&iter))
  90. {
  91. nrf_sdh_req_observer_t * p_observer;
  92. nrf_sdh_req_evt_handler_t handler;
  93. p_observer = (nrf_sdh_req_observer_t *) nrf_section_iter_get(&iter);
  94. handler = p_observer->handler;
  95. if (handler(req, p_observer->p_context))
  96. {
  97. NRF_LOG_DEBUG("Notify observer 0x%08X => ready", p_observer);
  98. }
  99. else
  100. {
  101. // Process is stopped.
  102. NRF_LOG_DEBUG("Notify observer 0x%08X => blocking", p_observer);
  103. return NRF_ERROR_BUSY;
  104. }
  105. }
  106. return NRF_SUCCESS;
  107. }
  108. /**@brief Function for stage request observers.
  109. *
  110. * @param[in] evt Type of stage event.
  111. */
  112. static void sdh_state_observer_notify(nrf_sdh_state_evt_t evt)
  113. {
  114. nrf_section_iter_t iter;
  115. NRF_LOG_DEBUG("State change: 0x%08X", evt);
  116. for (nrf_section_iter_init(&iter, &sdh_state_observers);
  117. nrf_section_iter_get(&iter) != NULL;
  118. nrf_section_iter_next(&iter))
  119. {
  120. nrf_sdh_state_observer_t * p_observer;
  121. nrf_sdh_state_evt_handler_t handler;
  122. p_observer = (nrf_sdh_state_observer_t *) nrf_section_iter_get(&iter);
  123. handler = p_observer->handler;
  124. handler(evt, p_observer->p_context);
  125. }
  126. }
  127. static void softdevices_evt_irq_enable(void)
  128. {
  129. #ifdef SOFTDEVICE_PRESENT
  130. ret_code_t ret_code = sd_nvic_EnableIRQ((IRQn_Type)SD_EVT_IRQn);
  131. APP_ERROR_CHECK(ret_code);
  132. #else
  133. // In case of serialization, NVIC must be accessed directly.
  134. NVIC_EnableIRQ(SD_EVT_IRQn);
  135. #endif
  136. }
  137. static void softdevice_evt_irq_disable(void)
  138. {
  139. #ifdef SOFTDEVICE_PRESENT
  140. ret_code_t ret_code = sd_nvic_DisableIRQ((IRQn_Type)SD_EVT_IRQn);
  141. APP_ERROR_CHECK(ret_code);
  142. #else
  143. // In case of serialization, NVIC must be accessed directly.
  144. NVIC_DisableIRQ(SD_EVT_IRQn);
  145. #endif
  146. }
  147. ret_code_t nrf_sdh_enable_request(void)
  148. {
  149. ret_code_t ret_code;
  150. if (m_nrf_sdh_enabled)
  151. {
  152. NRF_LOG_RAW_INFO("nrf_sdh_enable_request -- NRF_ERROR_INVALID_STATE\n");
  153. return NRF_ERROR_INVALID_STATE;
  154. }
  155. m_nrf_sdh_continue = true;
  156. // Notify observers about SoftDevice enable request.
  157. if (sdh_request_observer_notify(NRF_SDH_EVT_ENABLE_REQUEST) == NRF_ERROR_BUSY)
  158. {
  159. // Enable process was stopped.
  160. NRF_LOG_RAW_INFO("nrf_sdh_enable_request -- NRF_SUCCESS\n");
  161. return NRF_SUCCESS;
  162. }
  163. // Notify observers about starting SoftDevice enable process.
  164. sdh_state_observer_notify(NRF_SDH_EVT_STATE_ENABLE_PREPARE);
  165. NRF_LOG_RAW_INFO("nrf_sdh_enable_request -- sdh_state_observer_notify\n");
  166. nrf_clock_lf_cfg_t const clock_lf_cfg =
  167. {
  168. .source = NRF_SDH_CLOCK_LF_SRC,
  169. .rc_ctiv = NRF_SDH_CLOCK_LF_RC_CTIV,
  170. .rc_temp_ctiv = NRF_SDH_CLOCK_LF_RC_TEMP_CTIV,
  171. .accuracy = NRF_SDH_CLOCK_LF_ACCURACY
  172. };
  173. CRITICAL_REGION_ENTER();
  174. #ifdef ANT_LICENSE_KEY
  175. ret_code = sd_softdevice_enable(&clock_lf_cfg, app_error_fault_handler, ANT_LICENSE_KEY);
  176. #else
  177. ret_code = sd_softdevice_enable(&clock_lf_cfg, app_error_fault_handler);
  178. #endif
  179. m_nrf_sdh_enabled = (ret_code == NRF_SUCCESS);
  180. CRITICAL_REGION_EXIT();
  181. if (ret_code != NRF_SUCCESS)
  182. {
  183. return ret_code;
  184. }
  185. m_nrf_sdh_continue = false;
  186. m_nrf_sdh_suspended = false;
  187. // Enable event interrupt.
  188. // Interrupt priority has already been set by the stack.
  189. softdevices_evt_irq_enable();
  190. // Notify observers about a finished SoftDevice enable process.
  191. sdh_state_observer_notify(NRF_SDH_EVT_STATE_ENABLED);
  192. return NRF_SUCCESS;
  193. }
  194. ret_code_t nrf_sdh_disable_request(void)
  195. {
  196. ret_code_t ret_code;
  197. if (!m_nrf_sdh_enabled)
  198. {
  199. NRF_LOG_RAW_INFO("nrf_sdh_disable_request -- NRF_ERROR_INVALID_STATE\n");
  200. return NRF_ERROR_INVALID_STATE;
  201. }
  202. m_nrf_sdh_continue = true;
  203. // Notify observers about SoftDevice disable request.
  204. if (sdh_request_observer_notify(NRF_SDH_EVT_DISABLE_REQUEST) == NRF_ERROR_BUSY)
  205. {
  206. // Disable process was stopped.
  207. NRF_LOG_RAW_INFO("nrf_sdh_disable_request -- NRF_SUCCESS\n");
  208. return NRF_SUCCESS;
  209. }
  210. // Notify observers about starting SoftDevice disable process.
  211. sdh_state_observer_notify(NRF_SDH_EVT_STATE_DISABLE_PREPARE);
  212. CRITICAL_REGION_ENTER();
  213. ret_code = sd_softdevice_disable();
  214. m_nrf_sdh_enabled = false;
  215. CRITICAL_REGION_EXIT();
  216. if (ret_code != NRF_SUCCESS)
  217. {
  218. return ret_code;
  219. }
  220. m_nrf_sdh_continue = false;
  221. softdevice_evt_irq_disable();
  222. // Notify observers about a finished SoftDevice enable process.
  223. sdh_state_observer_notify(NRF_SDH_EVT_STATE_DISABLED);
  224. return NRF_SUCCESS;
  225. }
  226. ret_code_t nrf_sdh_request_continue(void)
  227. {
  228. if (!m_nrf_sdh_continue)
  229. {
  230. return NRF_ERROR_INVALID_STATE;
  231. }
  232. if (m_nrf_sdh_enabled)
  233. {
  234. return nrf_sdh_disable_request();
  235. }
  236. else
  237. {
  238. return nrf_sdh_enable_request();
  239. }
  240. }
  241. bool nrf_sdh_is_enabled(void)
  242. {
  243. return m_nrf_sdh_enabled;
  244. }
  245. void nrf_sdh_suspend(void)
  246. {
  247. if (!m_nrf_sdh_enabled)
  248. {
  249. return;
  250. }
  251. softdevice_evt_irq_disable();
  252. m_nrf_sdh_suspended = true;
  253. }
  254. void nrf_sdh_resume(void)
  255. {
  256. if ((!m_nrf_sdh_suspended) || (!m_nrf_sdh_enabled))
  257. {
  258. return;
  259. }
  260. // Force calling ISR again to make sure that events not previously pulled have been processed.
  261. #ifdef SOFTDEVICE_PRESENT
  262. ret_code_t ret_code = sd_nvic_SetPendingIRQ((IRQn_Type)SD_EVT_IRQn);
  263. APP_ERROR_CHECK(ret_code);
  264. #else
  265. NVIC_SetPendingIRQ((IRQn_Type)SD_EVT_IRQn);
  266. #endif
  267. softdevices_evt_irq_enable();
  268. m_nrf_sdh_suspended = false;
  269. }
  270. bool nrf_sdh_is_suspended(void)
  271. {
  272. return (!m_nrf_sdh_enabled) || (m_nrf_sdh_suspended);
  273. }
  274. void nrf_sdh_evts_poll(void)
  275. {
  276. nrf_section_iter_t iter;
  277. // Notify observers about pending SoftDevice event.
  278. for (nrf_section_iter_init(&iter, &sdh_stack_observers);
  279. nrf_section_iter_get(&iter) != NULL;
  280. nrf_section_iter_next(&iter))
  281. {
  282. nrf_sdh_stack_observer_t * p_observer;
  283. nrf_sdh_stack_evt_handler_t handler;
  284. p_observer = (nrf_sdh_stack_observer_t *) nrf_section_iter_get(&iter);
  285. handler = p_observer->handler;
  286. handler(p_observer->p_context);
  287. }
  288. }
  289. #if (NRF_SDH_DISPATCH_MODEL == NRF_SDH_DISPATCH_MODEL_INTERRUPT)
  290. void SD_EVT_IRQHandler(void)
  291. {
  292. nrf_sdh_evts_poll();
  293. }
  294. #elif (NRF_SDH_DISPATCH_MODEL == NRF_SDH_DISPATCH_MODEL_APPSH)
  295. /**@brief Function for polling SoftDevice events.
  296. *
  297. * @note This function is compatible with @ref app_sched_event_handler_t.
  298. *
  299. * @param[in] p_event_data Pointer to the event data.
  300. * @param[in] event_size Size of the event data.
  301. */
  302. static void appsh_events_poll(void * p_event_data, uint16_t event_size)
  303. {
  304. UNUSED_PARAMETER(p_event_data);
  305. UNUSED_PARAMETER(event_size);
  306. nrf_sdh_evts_poll();
  307. }
  308. void SD_EVT_IRQHandler(void)
  309. {
  310. ret_code_t ret_code = app_sched_event_put(NULL, 0, appsh_events_poll);
  311. APP_ERROR_CHECK(ret_code);
  312. }
  313. #elif (NRF_SDH_DISPATCH_MODEL == NRF_SDH_DISPATCH_MODEL_POLLING)
  314. #else
  315. #error "Unknown SoftDevice handler dispatch model."
  316. #endif // NRF_SDH_DISPATCH_MODEL
  317. #endif // NRF_MODULE_ENABLED(NRF_SDH)