nrf_sdh.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402
  1. /**
  2. * Copyright (c) 2017 - 2018, 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. return NRF_ERROR_INVALID_STATE;
  153. }
  154. m_nrf_sdh_continue = true;
  155. // Notify observers about SoftDevice enable request.
  156. if (sdh_request_observer_notify(NRF_SDH_EVT_ENABLE_REQUEST) == NRF_ERROR_BUSY)
  157. {
  158. // Enable process was stopped.
  159. return NRF_SUCCESS;
  160. }
  161. // Notify observers about starting SoftDevice enable process.
  162. sdh_state_observer_notify(NRF_SDH_EVT_STATE_ENABLE_PREPARE);
  163. nrf_clock_lf_cfg_t const clock_lf_cfg =
  164. {
  165. .source = NRF_SDH_CLOCK_LF_SRC,
  166. .rc_ctiv = NRF_SDH_CLOCK_LF_RC_CTIV,
  167. .rc_temp_ctiv = NRF_SDH_CLOCK_LF_RC_TEMP_CTIV,
  168. .accuracy = NRF_SDH_CLOCK_LF_ACCURACY
  169. };
  170. CRITICAL_REGION_ENTER();
  171. #ifdef ANT_LICENSE_KEY
  172. ret_code = sd_softdevice_enable(&clock_lf_cfg, app_error_fault_handler, ANT_LICENSE_KEY);
  173. #else
  174. ret_code = sd_softdevice_enable(&clock_lf_cfg, app_error_fault_handler);
  175. #endif
  176. m_nrf_sdh_enabled = (ret_code == NRF_SUCCESS);
  177. CRITICAL_REGION_EXIT();
  178. if (ret_code != NRF_SUCCESS)
  179. {
  180. return ret_code;
  181. }
  182. m_nrf_sdh_continue = false;
  183. m_nrf_sdh_suspended = false;
  184. // Enable event interrupt.
  185. // Interrupt priority has already been set by the stack.
  186. softdevices_evt_irq_enable();
  187. // Notify observers about a finished SoftDevice enable process.
  188. sdh_state_observer_notify(NRF_SDH_EVT_STATE_ENABLED);
  189. return NRF_SUCCESS;
  190. }
  191. ret_code_t nrf_sdh_disable_request(void)
  192. {
  193. ret_code_t ret_code;
  194. if (!m_nrf_sdh_enabled)
  195. {
  196. return NRF_ERROR_INVALID_STATE;
  197. }
  198. m_nrf_sdh_continue = true;
  199. // Notify observers about SoftDevice disable request.
  200. if (sdh_request_observer_notify(NRF_SDH_EVT_DISABLE_REQUEST) == NRF_ERROR_BUSY)
  201. {
  202. // Disable process was stopped.
  203. return NRF_SUCCESS;
  204. }
  205. // Notify observers about starting SoftDevice disable process.
  206. sdh_state_observer_notify(NRF_SDH_EVT_STATE_DISABLE_PREPARE);
  207. CRITICAL_REGION_ENTER();
  208. ret_code = sd_softdevice_disable();
  209. m_nrf_sdh_enabled = false;
  210. CRITICAL_REGION_EXIT();
  211. if (ret_code != NRF_SUCCESS)
  212. {
  213. return ret_code;
  214. }
  215. m_nrf_sdh_continue = false;
  216. softdevice_evt_irq_disable();
  217. // Notify observers about a finished SoftDevice enable process.
  218. sdh_state_observer_notify(NRF_SDH_EVT_STATE_DISABLED);
  219. return NRF_SUCCESS;
  220. }
  221. ret_code_t nrf_sdh_request_continue(void)
  222. {
  223. if (!m_nrf_sdh_continue)
  224. {
  225. return NRF_ERROR_INVALID_STATE;
  226. }
  227. if (m_nrf_sdh_enabled)
  228. {
  229. return nrf_sdh_disable_request();
  230. }
  231. else
  232. {
  233. return nrf_sdh_enable_request();
  234. }
  235. }
  236. bool nrf_sdh_is_enabled(void)
  237. {
  238. return m_nrf_sdh_enabled;
  239. }
  240. void nrf_sdh_suspend(void)
  241. {
  242. if (!m_nrf_sdh_enabled)
  243. {
  244. return;
  245. }
  246. softdevice_evt_irq_disable();
  247. m_nrf_sdh_suspended = true;
  248. }
  249. void nrf_sdh_resume(void)
  250. {
  251. if ((!m_nrf_sdh_suspended) || (!m_nrf_sdh_enabled))
  252. {
  253. return;
  254. }
  255. // Force calling ISR again to make sure that events not previously pulled have been processed.
  256. #ifdef SOFTDEVICE_PRESENT
  257. ret_code_t ret_code = sd_nvic_SetPendingIRQ((IRQn_Type)SD_EVT_IRQn);
  258. APP_ERROR_CHECK(ret_code);
  259. #else
  260. NVIC_SetPendingIRQ((IRQn_Type)SD_EVT_IRQn);
  261. #endif
  262. softdevices_evt_irq_enable();
  263. m_nrf_sdh_suspended = false;
  264. }
  265. bool nrf_sdh_is_suspended(void)
  266. {
  267. return (!m_nrf_sdh_enabled) || (m_nrf_sdh_suspended);
  268. }
  269. void nrf_sdh_evts_poll(void)
  270. {
  271. nrf_section_iter_t iter;
  272. // Notify observers about pending SoftDevice event.
  273. for (nrf_section_iter_init(&iter, &sdh_stack_observers);
  274. nrf_section_iter_get(&iter) != NULL;
  275. nrf_section_iter_next(&iter))
  276. {
  277. nrf_sdh_stack_observer_t * p_observer;
  278. nrf_sdh_stack_evt_handler_t handler;
  279. p_observer = (nrf_sdh_stack_observer_t *) nrf_section_iter_get(&iter);
  280. handler = p_observer->handler;
  281. handler(p_observer->p_context);
  282. }
  283. }
  284. #if (NRF_SDH_DISPATCH_MODEL == NRF_SDH_DISPATCH_MODEL_INTERRUPT)
  285. void SD_EVT_IRQHandler(void)
  286. {
  287. nrf_sdh_evts_poll();
  288. }
  289. #elif (NRF_SDH_DISPATCH_MODEL == NRF_SDH_DISPATCH_MODEL_APPSH)
  290. /**@brief Function for polling SoftDevice events.
  291. *
  292. * @note This function is compatible with @ref app_sched_event_handler_t.
  293. *
  294. * @param[in] p_event_data Pointer to the event data.
  295. * @param[in] event_size Size of the event data.
  296. */
  297. static void appsh_events_poll(void * p_event_data, uint16_t event_size)
  298. {
  299. UNUSED_PARAMETER(p_event_data);
  300. UNUSED_PARAMETER(event_size);
  301. nrf_sdh_evts_poll();
  302. }
  303. void SD_EVT_IRQHandler(void)
  304. {
  305. ret_code_t ret_code = app_sched_event_put(NULL, 0, appsh_events_poll);
  306. APP_ERROR_CHECK(ret_code);
  307. }
  308. #elif (NRF_SDH_DISPATCH_MODEL == NRF_SDH_DISPATCH_MODEL_POLLING)
  309. #else
  310. #error "Unknown SoftDevice handler dispatch model."
  311. #endif // NRF_SDH_DISPATCH_MODEL
  312. #endif // NRF_MODULE_ENABLED(NRF_SDH)