softdevice_handler.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490
  1. /* Copyright (c) 2012 Nordic Semiconductor. All Rights Reserved.
  2. *
  3. * The information contained herein is property of Nordic Semiconductor ASA.
  4. * Terms and conditions of usage are described in detail in NORDIC
  5. * SEMICONDUCTOR STANDARD SOFTWARE LICENSE AGREEMENT.
  6. *
  7. * Licensees are granted free, non-transferable use of the information. NO
  8. * WARRANTY of ANY KIND is provided. This heading must NOT be removed from
  9. * the file.
  10. *
  11. */
  12. #include "softdevice_handler.h"
  13. #include <stdlib.h>
  14. #include <stdint.h>
  15. #include <stdio.h>
  16. #include <string.h>
  17. #include "nordic_common.h"
  18. #include "app_error.h"
  19. #include "nrf_assert.h"
  20. #include "nrf_nvic.h"
  21. #include "nrf.h"
  22. #include "nrf_log.h"
  23. #include "sdk_common.h"
  24. #include "nrf_drv_config.h"
  25. #if CLOCK_ENABLED
  26. #include "nrf_drv_clock.h"
  27. #endif
  28. #if defined(ANT_STACK_SUPPORT_REQD) && defined(BLE_STACK_SUPPORT_REQD)
  29. #include "ant_interface.h"
  30. #elif defined(ANT_STACK_SUPPORT_REQD)
  31. #include "ant_interface.h"
  32. #elif defined(BLE_STACK_SUPPORT_REQD)
  33. #include "ble.h"
  34. #endif
  35. #if defined(NRF_LOG_USES_RTT) && NRF_LOG_USES_RTT == 1
  36. #define SD_HANDLER_LOG(...) NRF_LOG_PRINTF(__VA_ARGS__)
  37. #else
  38. #define SD_HANDLER_LOG(...)
  39. #endif
  40. #if defined(NRF_LOG_USES_RTT) && NRF_LOG_USES_RTT == 1
  41. #define SD_HANDLER_LOG_INIT() NRF_LOG_INIT()
  42. #else
  43. #define SD_HANDLER_LOG_INIT()
  44. #endif
  45. #define RAM_START_ADDRESS 0x20000000
  46. #define SOFTDEVICE_EVT_IRQ SD_EVT_IRQn /**< SoftDevice Event IRQ number. Used for both protocol events and SoC events. */
  47. #define SOFTDEVICE_EVT_IRQHandler SD_EVT_IRQHandler
  48. #define RAM_TOTAL_SIZE ((NRF_FICR->INFO.RAM)*1024)
  49. #define RAM_END_ADDRESS (RAM_START_ADDRESS + RAM_TOTAL_SIZE)
  50. #define SOFTDEVICE_VS_UUID_COUNT 0
  51. #define SOFTDEVICE_GATTS_ATTR_TAB_SIZE BLE_GATTS_ATTR_TAB_SIZE_DEFAULT
  52. #define SOFTDEVICE_GATTS_SRV_CHANGED 0
  53. #define SOFTDEVICE_PERIPH_CONN_COUNT 1
  54. #define SOFTDEVICE_CENTRAL_CONN_COUNT 4
  55. #define SOFTDEVICE_CENTRAL_SEC_COUNT 1
  56. /* Global nvic state instance, required by nrf_nvic.h */
  57. nrf_nvic_state_t nrf_nvic_state;
  58. static softdevice_evt_schedule_func_t m_evt_schedule_func; /**< Pointer to function for propagating SoftDevice events to the scheduler. */
  59. static volatile bool m_softdevice_enabled = false; /**< Variable to indicate whether the SoftDevice is enabled. */
  60. #ifdef BLE_STACK_SUPPORT_REQD
  61. // The following three definitions is needed only if BLE events are needed to be pulled from the stack.
  62. static uint8_t * mp_ble_evt_buffer; /**< Buffer for receiving BLE events from the SoftDevice. */
  63. static uint16_t m_ble_evt_buffer_size; /**< Size of BLE event buffer. */
  64. static ble_evt_handler_t m_ble_evt_handler; /**< Application event handler for handling BLE events. */
  65. #endif
  66. #ifdef ANT_STACK_SUPPORT_REQD
  67. // The following two definition is needed only if ANT events are needed to be pulled from the stack.
  68. static ant_evt_t m_ant_evt_buffer; /**< Buffer for receiving ANT events from the SoftDevice. */
  69. static ant_evt_handler_t m_ant_evt_handler; /**< Application event handler for handling ANT events. */
  70. #endif
  71. static sys_evt_handler_t m_sys_evt_handler; /**< Application event handler for handling System (SOC) events. */
  72. /**@brief Callback function for asserts in the SoftDevice.
  73. *
  74. * @details A pointer to this function will be passed to the SoftDevice. This function will be
  75. * called by the SoftDevice if certain unrecoverable errors occur within the
  76. * application or SoftDevice.
  77. *
  78. * See @ref nrf_fault_handler_t for more details.
  79. *
  80. * @param[in] id Fault identifier. See @ref NRF_FAULT_IDS.
  81. * @param[in] pc The program counter of the instruction that triggered the fault.
  82. * @param[in] info Optional additional information regarding the fault. Refer to each fault
  83. * identifier for details.
  84. */
  85. void softdevice_fault_handler(uint32_t id, uint32_t pc, uint32_t info)
  86. {
  87. app_error_fault_handler(id, pc, info);
  88. }
  89. void intern_softdevice_events_execute(void)
  90. {
  91. if (!m_softdevice_enabled)
  92. {
  93. // SoftDevice not enabled. This can be possible if the SoftDevice was enabled by the
  94. // application without using this module's API (i.e softdevice_handler_init)
  95. return;
  96. }
  97. #if CLOCK_ENABLED
  98. bool no_more_soc_evts = false;
  99. #else
  100. bool no_more_soc_evts = (m_sys_evt_handler == NULL);
  101. #endif
  102. #ifdef BLE_STACK_SUPPORT_REQD
  103. bool no_more_ble_evts = (m_ble_evt_handler == NULL);
  104. #endif
  105. #ifdef ANT_STACK_SUPPORT_REQD
  106. bool no_more_ant_evts = (m_ant_evt_handler == NULL);
  107. #endif
  108. for (;;)
  109. {
  110. uint32_t err_code;
  111. if (!no_more_soc_evts)
  112. {
  113. uint32_t evt_id;
  114. // Pull event from SOC.
  115. err_code = sd_evt_get(&evt_id);
  116. if (err_code == NRF_ERROR_NOT_FOUND)
  117. {
  118. no_more_soc_evts = true;
  119. }
  120. else if (err_code != NRF_SUCCESS)
  121. {
  122. APP_ERROR_HANDLER(err_code);
  123. }
  124. else
  125. {
  126. // Call application's SOC event handler.
  127. #if CLOCK_ENABLED
  128. nrf_drv_clock_on_soc_event(evt_id);
  129. if (m_sys_evt_handler)
  130. {
  131. m_sys_evt_handler(evt_id);
  132. }
  133. #else
  134. m_sys_evt_handler(evt_id);
  135. #endif
  136. }
  137. }
  138. #ifdef BLE_STACK_SUPPORT_REQD
  139. // Fetch BLE Events.
  140. if (!no_more_ble_evts)
  141. {
  142. // Pull event from stack
  143. uint16_t evt_len = m_ble_evt_buffer_size;
  144. err_code = sd_ble_evt_get(mp_ble_evt_buffer, &evt_len);
  145. if (err_code == NRF_ERROR_NOT_FOUND)
  146. {
  147. no_more_ble_evts = true;
  148. }
  149. else if (err_code != NRF_SUCCESS)
  150. {
  151. APP_ERROR_HANDLER(err_code);
  152. }
  153. else
  154. {
  155. // Call application's BLE stack event handler.
  156. m_ble_evt_handler((ble_evt_t *)mp_ble_evt_buffer);
  157. }
  158. }
  159. #endif
  160. #ifdef ANT_STACK_SUPPORT_REQD
  161. // Fetch ANT Events.
  162. if (!no_more_ant_evts)
  163. {
  164. // Pull event from stack
  165. err_code = sd_ant_event_get(&m_ant_evt_buffer.channel,
  166. &m_ant_evt_buffer.event,
  167. m_ant_evt_buffer.msg.evt_buffer);
  168. if (err_code == NRF_ERROR_NOT_FOUND)
  169. {
  170. no_more_ant_evts = true;
  171. }
  172. else if (err_code != NRF_SUCCESS)
  173. {
  174. APP_ERROR_HANDLER(err_code);
  175. }
  176. else
  177. {
  178. // Call application's ANT stack event handler.
  179. m_ant_evt_handler(&m_ant_evt_buffer);
  180. }
  181. }
  182. #endif
  183. if (no_more_soc_evts)
  184. {
  185. // There are no remaining System (SOC) events to be fetched from the SoftDevice.
  186. #if defined(ANT_STACK_SUPPORT_REQD) && defined(BLE_STACK_SUPPORT_REQD)
  187. // Check if there are any remaining BLE and ANT events.
  188. if (no_more_ble_evts && no_more_ant_evts)
  189. {
  190. break;
  191. }
  192. #elif defined(BLE_STACK_SUPPORT_REQD)
  193. // Check if there are any remaining BLE events.
  194. if (no_more_ble_evts)
  195. {
  196. break;
  197. }
  198. #elif defined(ANT_STACK_SUPPORT_REQD)
  199. // Check if there are any remaining ANT events.
  200. if (no_more_ant_evts)
  201. {
  202. break;
  203. }
  204. #else
  205. // No need to check for BLE or ANT events since there is no support for BLE and ANT
  206. // required.
  207. break;
  208. #endif
  209. }
  210. }
  211. }
  212. bool softdevice_handler_isEnabled(void)
  213. {
  214. return m_softdevice_enabled;
  215. }
  216. uint32_t softdevice_handler_init(nrf_clock_lf_cfg_t * p_clock_lf_cfg,
  217. void * p_ble_evt_buffer,
  218. uint16_t ble_evt_buffer_size,
  219. softdevice_evt_schedule_func_t evt_schedule_func)
  220. {
  221. uint32_t err_code;
  222. SD_HANDLER_LOG_INIT();
  223. // Save configuration.
  224. #if defined (BLE_STACK_SUPPORT_REQD)
  225. // Check that buffer is not NULL.
  226. if (p_ble_evt_buffer == NULL)
  227. {
  228. return NRF_ERROR_INVALID_PARAM;
  229. }
  230. // Check that buffer is correctly aligned.
  231. if (!is_word_aligned(p_ble_evt_buffer))
  232. {
  233. return NRF_ERROR_INVALID_PARAM;
  234. }
  235. mp_ble_evt_buffer = (uint8_t *)p_ble_evt_buffer;
  236. m_ble_evt_buffer_size = ble_evt_buffer_size;
  237. #else
  238. // The variables p_ble_evt_buffer and ble_evt_buffer_size is not needed if BLE Stack support
  239. // is not required.
  240. UNUSED_PARAMETER(p_ble_evt_buffer);
  241. UNUSED_PARAMETER(ble_evt_buffer_size);
  242. #endif
  243. m_evt_schedule_func = evt_schedule_func;
  244. // Initialize SoftDevice.
  245. #if defined(S212) || defined(S332)
  246. err_code = sd_softdevice_enable(p_clock_lf_cfg, softdevice_fault_handler, ANT_LICENSE_KEY);
  247. #else
  248. err_code = sd_softdevice_enable(p_clock_lf_cfg, softdevice_fault_handler);
  249. #endif
  250. if (err_code != NRF_SUCCESS)
  251. {
  252. return err_code;
  253. }
  254. m_softdevice_enabled = true;
  255. // Enable BLE event interrupt (interrupt priority has already been set by the stack).
  256. #ifdef SOFTDEVICE_PRESENT
  257. return sd_nvic_EnableIRQ((IRQn_Type)SOFTDEVICE_EVT_IRQ);
  258. #else
  259. //In case of Serialization NVIC must be accessed directly.
  260. NVIC_EnableIRQ(SOFTDEVICE_EVT_IRQ);
  261. return NRF_SUCCESS;
  262. #endif
  263. }
  264. uint32_t softdevice_handler_sd_disable(void)
  265. {
  266. uint32_t err_code = sd_softdevice_disable();
  267. m_softdevice_enabled = !(err_code == NRF_SUCCESS);
  268. return err_code;
  269. }
  270. #ifdef BLE_STACK_SUPPORT_REQD
  271. uint32_t softdevice_ble_evt_handler_set(ble_evt_handler_t ble_evt_handler)
  272. {
  273. VERIFY_PARAM_NOT_NULL(ble_evt_handler);
  274. m_ble_evt_handler = ble_evt_handler;
  275. return NRF_SUCCESS;
  276. }
  277. #endif
  278. #ifdef ANT_STACK_SUPPORT_REQD
  279. uint32_t softdevice_ant_evt_handler_set(ant_evt_handler_t ant_evt_handler)
  280. {
  281. VERIFY_PARAM_NOT_NULL(ant_evt_handler);
  282. m_ant_evt_handler = ant_evt_handler;
  283. return NRF_SUCCESS;
  284. }
  285. #endif
  286. uint32_t softdevice_sys_evt_handler_set(sys_evt_handler_t sys_evt_handler)
  287. {
  288. VERIFY_PARAM_NOT_NULL(sys_evt_handler);
  289. m_sys_evt_handler = sys_evt_handler;
  290. return NRF_SUCCESS;
  291. }
  292. /**@brief Function for handling the Application's BLE Stack events interrupt.
  293. *
  294. * @details This function is called whenever an event is ready to be pulled.
  295. */
  296. void SOFTDEVICE_EVT_IRQHandler(void)
  297. {
  298. if (m_evt_schedule_func != NULL)
  299. {
  300. uint32_t err_code = m_evt_schedule_func();
  301. APP_ERROR_CHECK(err_code);
  302. }
  303. else
  304. {
  305. intern_softdevice_events_execute();
  306. }
  307. }
  308. #if defined(BLE_STACK_SUPPORT_REQD)
  309. uint32_t softdevice_enable_get_default_config(uint8_t central_links_count,
  310. uint8_t periph_links_count,
  311. ble_enable_params_t * p_ble_enable_params)
  312. {
  313. memset(p_ble_enable_params, 0, sizeof(ble_enable_params_t));
  314. p_ble_enable_params->common_enable_params.vs_uuid_count = 1;
  315. p_ble_enable_params->gatts_enable_params.attr_tab_size = SOFTDEVICE_GATTS_ATTR_TAB_SIZE;
  316. p_ble_enable_params->gatts_enable_params.service_changed = SOFTDEVICE_GATTS_SRV_CHANGED;
  317. p_ble_enable_params->gap_enable_params.periph_conn_count = periph_links_count;
  318. p_ble_enable_params->gap_enable_params.central_conn_count = central_links_count;
  319. if (p_ble_enable_params->gap_enable_params.central_conn_count != 0)
  320. {
  321. p_ble_enable_params->gap_enable_params.central_sec_count = SOFTDEVICE_CENTRAL_SEC_COUNT;
  322. }
  323. return NRF_SUCCESS;
  324. }
  325. #if defined(NRF_LOG_USES_RTT) && NRF_LOG_USES_RTT == 1
  326. static inline uint32_t ram_total_size_get(void)
  327. {
  328. #ifdef NRF51
  329. uint32_t size_ram_blocks = (uint32_t)NRF_FICR->SIZERAMBLOCKS;
  330. uint32_t total_ram_size = size_ram_blocks;
  331. total_ram_size = total_ram_size*(NRF_FICR->NUMRAMBLOCK);
  332. return total_ram_size;
  333. #elif defined (NRF52)
  334. return RAM_TOTAL_SIZE;
  335. #endif /* NRF51 */
  336. }
  337. /*lint --e{528} -save suppress 528: symbol not referenced */
  338. /**@brief Function for finding the end address of the RAM.
  339. *
  340. * @retval ram_end_address Address of the end of the RAM.
  341. */
  342. static inline uint32_t ram_end_address_get(void)
  343. {
  344. uint32_t ram_end_address = (uint32_t)RAM_START_ADDRESS;
  345. ram_end_address+= ram_total_size_get();
  346. return ram_end_address;
  347. }
  348. /*lint -restore*/
  349. #endif //ENABLE_DEBUG_LOG_SUPPORT
  350. /*lint --e{10} --e{19} --e{27} --e{40} --e{529} -save suppress Error 27: Illegal character */
  351. uint32_t sd_check_ram_start(uint32_t sd_req_ram_start)
  352. {
  353. #if (defined(S130) || defined(S132) || defined(S332))
  354. #if defined ( __CC_ARM )
  355. extern uint32_t Image$$RW_IRAM1$$Base;
  356. const volatile uint32_t ram_start = (uint32_t) &Image$$RW_IRAM1$$Base;
  357. #elif defined ( __ICCARM__ )
  358. extern uint32_t __ICFEDIT_region_RAM_start__;
  359. volatile uint32_t ram_start = (uint32_t) &__ICFEDIT_region_RAM_start__;
  360. #elif defined ( __GNUC__ )
  361. extern uint32_t __data_start__;
  362. volatile uint32_t ram_start = (uint32_t) &__data_start__;
  363. #endif//__CC_ARM
  364. if (ram_start != sd_req_ram_start)
  365. {
  366. #if defined(NRF_LOG_USES_RTT) && NRF_LOG_USES_RTT == 1
  367. uint32_t app_ram_size= ram_end_address_get();
  368. SD_HANDLER_LOG("RAM START ADDR 0x%x should be adjusted to 0x%x\r\n",
  369. ram_start,
  370. sd_req_ram_start);
  371. app_ram_size -= sd_req_ram_start;
  372. SD_HANDLER_LOG("RAM SIZE should be adjusted to 0x%x \r\n",
  373. app_ram_size);
  374. #endif //NRF_LOG_USES_RTT
  375. return NRF_SUCCESS;
  376. }
  377. #endif//defined(S130) || defined(S132) || defined(S332)
  378. return NRF_SUCCESS;
  379. }
  380. uint32_t softdevice_enable(ble_enable_params_t * p_ble_enable_params)
  381. {
  382. #if (defined(S130) || defined(S132) || defined(S332))
  383. uint32_t err_code;
  384. uint32_t app_ram_base;
  385. #if defined ( __CC_ARM )
  386. extern uint32_t Image$$RW_IRAM1$$Base;
  387. const volatile uint32_t ram_start = (uint32_t) &Image$$RW_IRAM1$$Base;
  388. #elif defined ( __ICCARM__ )
  389. extern uint32_t __ICFEDIT_region_RAM_start__;
  390. volatile uint32_t ram_start = (uint32_t) &__ICFEDIT_region_RAM_start__;
  391. #elif defined ( __GNUC__ )
  392. extern uint32_t __data_start__;
  393. volatile uint32_t ram_start = (uint32_t) &__data_start__;
  394. #endif
  395. app_ram_base = ram_start;
  396. SD_HANDLER_LOG("sd_ble_enable: RAM START at 0x%x\r\n",
  397. app_ram_base);
  398. err_code = sd_ble_enable(p_ble_enable_params, &app_ram_base);
  399. #if defined(NRF_LOG_USES_RTT) && NRF_LOG_USES_RTT == 1
  400. if (app_ram_base != ram_start)
  401. {
  402. uint32_t app_ram_size= ram_end_address_get();
  403. SD_HANDLER_LOG("sd_ble_enable: app_ram_base should be adjusted to 0x%x\r\n",
  404. app_ram_base);
  405. app_ram_size -= app_ram_base;
  406. SD_HANDLER_LOG("ram size should be adjusted to 0x%x \r\n",
  407. app_ram_size);
  408. }
  409. else if (err_code != NRF_SUCCESS)
  410. {
  411. SD_HANDLER_LOG("sd_ble_enable: error 0x%x\r\n", err_code);
  412. while(1);
  413. }
  414. #endif // NRF_LOG_USES_RTT
  415. return err_code;
  416. #else
  417. return NRF_SUCCESS;
  418. #endif //defined(S130) || defined(S132) || defined(S332)
  419. }
  420. /*lint -restore*/
  421. #endif //BLE_STACK_SUPPORT_REQD