nrf_sdh_ble.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326
  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_BLE)
  42. #include "nrf_sdh_ble.h"
  43. #include "nrf_sdh.h"
  44. #include "app_error.h"
  45. #include "nrf_strerror.h"
  46. #define NRF_LOG_MODULE_NAME nrf_sdh_ble
  47. #if NRF_SDH_BLE_LOG_ENABLED
  48. #define NRF_LOG_LEVEL NRF_SDH_BLE_LOG_LEVEL
  49. #define NRF_LOG_INFO_COLOR NRF_SDH_BLE_INFO_COLOR
  50. #define NRF_LOG_DEBUG_COLOR NRF_SDH_BLE_DEBUG_COLOR
  51. #else
  52. #define NRF_LOG_LEVEL 0
  53. #endif // NRF_SDH_BLE_LOG_ENABLED
  54. #include "nrf_log.h"
  55. NRF_LOG_MODULE_REGISTER();
  56. // Create section set "sdh_ble_observers".
  57. NRF_SECTION_SET_DEF(sdh_ble_observers, nrf_sdh_ble_evt_observer_t, NRF_SDH_BLE_OBSERVER_PRIO_LEVELS);
  58. //lint -save -e10 -e19 -e40 -e27 Illegal character (0x24)
  59. #if defined(__CC_ARM)
  60. extern uint32_t Image$$RW_IRAM1$$Base;
  61. uint32_t const * const m_ram_start = &Image$$RW_IRAM1$$Base;
  62. #elif defined(__ICCARM__)
  63. extern uint32_t __ICFEDIT_region_RAM_start__;
  64. uint32_t const * const m_ram_start = &__ICFEDIT_region_RAM_start__;
  65. #elif defined(__SES_ARM)
  66. extern uint32_t __app_ram_start__;
  67. uint32_t const * const m_ram_start = &__app_ram_start__;
  68. #elif defined(__GNUC__)
  69. extern uint32_t __data_start__;
  70. uint32_t const * const m_ram_start = &__data_start__;
  71. #endif
  72. //lint -restore
  73. #define RAM_START 0x20000000
  74. #define APP_RAM_START (uint32_t)m_ram_start
  75. static bool m_stack_is_enabled;
  76. ret_code_t nrf_sdh_ble_app_ram_start_get(uint32_t * p_app_ram_start)
  77. {
  78. if (p_app_ram_start == NULL)
  79. {
  80. return NRF_ERROR_NULL;
  81. }
  82. *p_app_ram_start = APP_RAM_START;
  83. return NRF_SUCCESS;
  84. }
  85. ret_code_t nrf_sdh_ble_default_cfg_set(uint8_t conn_cfg_tag, uint32_t * p_ram_start)
  86. {
  87. uint32_t ret_code;
  88. ret_code = nrf_sdh_ble_app_ram_start_get(p_ram_start);
  89. if (ret_code != NRF_SUCCESS)
  90. {
  91. return ret_code;
  92. }
  93. #if defined (S112) || defined(S312)
  94. STATIC_ASSERT(NRF_SDH_BLE_CENTRAL_LINK_COUNT == 0, "When using s112, NRF_SDH_BLE_CENTRAL_LINK_COUNT must be 0.");
  95. #endif
  96. // Overwrite some of the default settings of the BLE stack.
  97. // If any of the calls to sd_ble_cfg_set() fail, log the error but carry on so that
  98. // wrong RAM settings can be caught by nrf_sdh_ble_enable() and a meaningful error
  99. // message will be printed to the user suggesting the correct value.
  100. ble_cfg_t ble_cfg;
  101. #if (NRF_SDH_BLE_TOTAL_LINK_COUNT != 0)
  102. // Configure the connection count.
  103. memset(&ble_cfg, 0, sizeof(ble_cfg));
  104. ble_cfg.conn_cfg.conn_cfg_tag = conn_cfg_tag;
  105. ble_cfg.conn_cfg.params.gap_conn_cfg.conn_count = NRF_SDH_BLE_TOTAL_LINK_COUNT;
  106. ble_cfg.conn_cfg.params.gap_conn_cfg.event_length = NRF_SDH_BLE_GAP_EVENT_LENGTH;
  107. ret_code = sd_ble_cfg_set(BLE_CONN_CFG_GAP, &ble_cfg, *p_ram_start);
  108. if (ret_code != NRF_SUCCESS)
  109. {
  110. NRF_LOG_ERROR("sd_ble_cfg_set() returned %s when attempting to set BLE_CONN_CFG_GAP.",
  111. nrf_strerror_get(ret_code));
  112. }
  113. // Configure the connection roles.
  114. memset(&ble_cfg, 0, sizeof(ble_cfg));
  115. #if !defined (S122)
  116. ble_cfg.gap_cfg.role_count_cfg.periph_role_count = NRF_SDH_BLE_PERIPHERAL_LINK_COUNT;
  117. #endif // !defined (S122)
  118. #if !defined (S112) && !defined(S312) && !defined(S113)
  119. ble_cfg.gap_cfg.role_count_cfg.central_role_count = NRF_SDH_BLE_CENTRAL_LINK_COUNT;
  120. ble_cfg.gap_cfg.role_count_cfg.central_sec_count = MIN(NRF_SDH_BLE_CENTRAL_LINK_COUNT,
  121. BLE_GAP_ROLE_COUNT_CENTRAL_SEC_DEFAULT);
  122. #endif // !defined (S112) && !defined(S312) && !defined(S113)
  123. ret_code = sd_ble_cfg_set(BLE_GAP_CFG_ROLE_COUNT, &ble_cfg, *p_ram_start);
  124. if (ret_code != NRF_SUCCESS)
  125. {
  126. NRF_LOG_ERROR("sd_ble_cfg_set() returned %s when attempting to set BLE_GAP_CFG_ROLE_COUNT.",
  127. nrf_strerror_get(ret_code));
  128. }
  129. // Configure the maximum ATT MTU.
  130. #if (NRF_SDH_BLE_GATT_MAX_MTU_SIZE != 23)
  131. memset(&ble_cfg, 0x00, sizeof(ble_cfg));
  132. ble_cfg.conn_cfg.conn_cfg_tag = conn_cfg_tag;
  133. ble_cfg.conn_cfg.params.gatt_conn_cfg.att_mtu = NRF_SDH_BLE_GATT_MAX_MTU_SIZE;
  134. ret_code = sd_ble_cfg_set(BLE_CONN_CFG_GATT, &ble_cfg, *p_ram_start);
  135. if (ret_code != NRF_SUCCESS)
  136. {
  137. NRF_LOG_ERROR("sd_ble_cfg_set() returned %s when attempting to set BLE_CONN_CFG_GATT.",
  138. nrf_strerror_get(ret_code));
  139. }
  140. #endif // NRF_SDH_BLE_GATT_MAX_MTU_SIZE != 23
  141. #endif // NRF_SDH_BLE_TOTAL_LINK_COUNT != 0
  142. // Configure number of custom UUIDS.
  143. memset(&ble_cfg, 0, sizeof(ble_cfg));
  144. ble_cfg.common_cfg.vs_uuid_cfg.vs_uuid_count = NRF_SDH_BLE_VS_UUID_COUNT;
  145. ret_code = sd_ble_cfg_set(BLE_COMMON_CFG_VS_UUID, &ble_cfg, *p_ram_start);
  146. if (ret_code != NRF_SUCCESS)
  147. {
  148. NRF_LOG_ERROR("sd_ble_cfg_set() returned %s when attempting to set BLE_COMMON_CFG_VS_UUID.",
  149. nrf_strerror_get(ret_code));
  150. }
  151. // Configure the GATTS attribute table.
  152. memset(&ble_cfg, 0x00, sizeof(ble_cfg));
  153. ble_cfg.gatts_cfg.attr_tab_size.attr_tab_size = NRF_SDH_BLE_GATTS_ATTR_TAB_SIZE;
  154. ret_code = sd_ble_cfg_set(BLE_GATTS_CFG_ATTR_TAB_SIZE, &ble_cfg, *p_ram_start);
  155. if (ret_code != NRF_SUCCESS)
  156. {
  157. NRF_LOG_ERROR("sd_ble_cfg_set() returned %s when attempting to set BLE_GATTS_CFG_ATTR_TAB_SIZE.",
  158. nrf_strerror_get(ret_code));
  159. }
  160. // Configure Service Changed characteristic.
  161. memset(&ble_cfg, 0x00, sizeof(ble_cfg));
  162. ble_cfg.gatts_cfg.service_changed.service_changed = NRF_SDH_BLE_SERVICE_CHANGED;
  163. ret_code = sd_ble_cfg_set(BLE_GATTS_CFG_SERVICE_CHANGED, &ble_cfg, *p_ram_start);
  164. if (ret_code != NRF_SUCCESS)
  165. {
  166. NRF_LOG_ERROR("sd_ble_cfg_set() returned %s when attempting to set BLE_GATTS_CFG_SERVICE_CHANGED.",
  167. nrf_strerror_get(ret_code));
  168. }
  169. return NRF_SUCCESS;
  170. }
  171. /**@brief Function for finding the end address of the RAM. */
  172. static uint32_t ram_end_address_get(void)
  173. {
  174. uint32_t ram_total_size;
  175. #ifdef NRF51
  176. uint32_t block_size = NRF_FICR->SIZERAMBLOCKS;
  177. ram_total_size = block_size * NRF_FICR->NUMRAMBLOCK;
  178. #else
  179. ram_total_size = NRF_FICR->INFO.RAM * 1024;
  180. #endif
  181. return RAM_START + ram_total_size;
  182. }
  183. ret_code_t nrf_sdh_ble_enable(uint32_t * const p_app_ram_start)
  184. {
  185. // Start of RAM, obtained from linker symbol.
  186. uint32_t const app_ram_start_link = *p_app_ram_start;
  187. ret_code_t ret_code = sd_ble_enable(p_app_ram_start);
  188. if (*p_app_ram_start > app_ram_start_link)
  189. {
  190. NRF_LOG_WARNING("Insufficient RAM allocated for the SoftDevice.");
  191. NRF_LOG_WARNING("Change the RAM start location from 0x%x to 0x%x.",
  192. app_ram_start_link, *p_app_ram_start);
  193. NRF_LOG_WARNING("Maximum RAM size for application is 0x%x.",
  194. ram_end_address_get() - (*p_app_ram_start));
  195. }
  196. else
  197. {
  198. NRF_LOG_DEBUG("RAM starts at 0x%x", app_ram_start_link);
  199. if (*p_app_ram_start != app_ram_start_link)
  200. {
  201. NRF_LOG_DEBUG("RAM start location can be adjusted to 0x%x.", *p_app_ram_start);
  202. NRF_LOG_DEBUG("RAM size for application can be adjusted to 0x%x.",
  203. ram_end_address_get() - (*p_app_ram_start));
  204. }
  205. }
  206. if (ret_code == NRF_SUCCESS)
  207. {
  208. m_stack_is_enabled = true;
  209. }
  210. else
  211. {
  212. NRF_LOG_ERROR("sd_ble_enable() returned %s.", nrf_strerror_get(ret_code));
  213. }
  214. return ret_code;
  215. }
  216. /**@brief Function for polling BLE events.
  217. *
  218. * @param[in] p_context Context of the observer.
  219. */
  220. static void nrf_sdh_ble_evts_poll(void * p_context)
  221. {
  222. UNUSED_VARIABLE(p_context);
  223. ret_code_t ret_code;
  224. if (!m_stack_is_enabled)
  225. {
  226. return;
  227. }
  228. while (true)
  229. {
  230. /*lint -save -e(587) */
  231. __ALIGN(4) uint8_t evt_buffer[NRF_SDH_BLE_EVT_BUF_SIZE];
  232. /*lint -restore */
  233. ble_evt_t * p_ble_evt;
  234. uint16_t evt_len = (uint16_t)sizeof(evt_buffer);
  235. ret_code = sd_ble_evt_get(evt_buffer, &evt_len);
  236. if (ret_code != NRF_SUCCESS)
  237. {
  238. break;
  239. }
  240. p_ble_evt = (ble_evt_t *)evt_buffer;
  241. NRF_LOG_DEBUG("BLE event: 0x%x.", p_ble_evt->header.evt_id);
  242. // Forward the event to BLE observers.
  243. nrf_section_iter_t iter;
  244. for (nrf_section_iter_init(&iter, &sdh_ble_observers);
  245. nrf_section_iter_get(&iter) != NULL;
  246. nrf_section_iter_next(&iter))
  247. {
  248. nrf_sdh_ble_evt_observer_t * p_observer;
  249. nrf_sdh_ble_evt_handler_t handler;
  250. p_observer = (nrf_sdh_ble_evt_observer_t *)nrf_section_iter_get(&iter);
  251. handler = p_observer->handler;
  252. handler(p_ble_evt, p_observer->p_context);
  253. }
  254. }
  255. if (ret_code != NRF_ERROR_NOT_FOUND)
  256. {
  257. APP_ERROR_HANDLER(ret_code);
  258. }
  259. }
  260. NRF_SDH_STACK_OBSERVER(m_nrf_sdh_ble_evts_poll, NRF_SDH_BLE_STACK_OBSERVER_PRIO) =
  261. {
  262. .handler = nrf_sdh_ble_evts_poll,
  263. .p_context = NULL,
  264. };
  265. #endif // NRF_MODULE_ENABLED(NRF_SDH_BLE)