nrf_sdh_ble.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324
  1. /**
  2. * Copyright (c) 2017 - 2019, 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. ble_cfg.gap_cfg.role_count_cfg.periph_role_count = NRF_SDH_BLE_PERIPHERAL_LINK_COUNT;
  116. #if !defined (S112) && !defined(S312)
  117. ble_cfg.gap_cfg.role_count_cfg.central_role_count = NRF_SDH_BLE_CENTRAL_LINK_COUNT;
  118. ble_cfg.gap_cfg.role_count_cfg.central_sec_count = MIN(NRF_SDH_BLE_CENTRAL_LINK_COUNT,
  119. BLE_GAP_ROLE_COUNT_CENTRAL_SEC_DEFAULT);
  120. #endif
  121. ret_code = sd_ble_cfg_set(BLE_GAP_CFG_ROLE_COUNT, &ble_cfg, *p_ram_start);
  122. if (ret_code != NRF_SUCCESS)
  123. {
  124. NRF_LOG_ERROR("sd_ble_cfg_set() returned %s when attempting to set BLE_GAP_CFG_ROLE_COUNT.",
  125. nrf_strerror_get(ret_code));
  126. }
  127. // Configure the maximum ATT MTU.
  128. #if (NRF_SDH_BLE_GATT_MAX_MTU_SIZE != 23)
  129. memset(&ble_cfg, 0x00, sizeof(ble_cfg));
  130. ble_cfg.conn_cfg.conn_cfg_tag = conn_cfg_tag;
  131. ble_cfg.conn_cfg.params.gatt_conn_cfg.att_mtu = NRF_SDH_BLE_GATT_MAX_MTU_SIZE;
  132. ret_code = sd_ble_cfg_set(BLE_CONN_CFG_GATT, &ble_cfg, *p_ram_start);
  133. if (ret_code != NRF_SUCCESS)
  134. {
  135. NRF_LOG_ERROR("sd_ble_cfg_set() returned %s when attempting to set BLE_CONN_CFG_GATT.",
  136. nrf_strerror_get(ret_code));
  137. }
  138. #endif // NRF_SDH_BLE_GATT_MAX_MTU_SIZE != 23
  139. #endif // NRF_SDH_BLE_TOTAL_LINK_COUNT != 0
  140. // Configure number of custom UUIDS.
  141. memset(&ble_cfg, 0, sizeof(ble_cfg));
  142. ble_cfg.common_cfg.vs_uuid_cfg.vs_uuid_count = NRF_SDH_BLE_VS_UUID_COUNT;
  143. ret_code = sd_ble_cfg_set(BLE_COMMON_CFG_VS_UUID, &ble_cfg, *p_ram_start);
  144. if (ret_code != NRF_SUCCESS)
  145. {
  146. NRF_LOG_ERROR("sd_ble_cfg_set() returned %s when attempting to set BLE_COMMON_CFG_VS_UUID.",
  147. nrf_strerror_get(ret_code));
  148. }
  149. // Configure the GATTS attribute table.
  150. memset(&ble_cfg, 0x00, sizeof(ble_cfg));
  151. ble_cfg.gatts_cfg.attr_tab_size.attr_tab_size = NRF_SDH_BLE_GATTS_ATTR_TAB_SIZE;
  152. ret_code = sd_ble_cfg_set(BLE_GATTS_CFG_ATTR_TAB_SIZE, &ble_cfg, *p_ram_start);
  153. if (ret_code != NRF_SUCCESS)
  154. {
  155. NRF_LOG_ERROR("sd_ble_cfg_set() returned %s when attempting to set BLE_GATTS_CFG_ATTR_TAB_SIZE.",
  156. nrf_strerror_get(ret_code));
  157. }
  158. // Configure Service Changed characteristic.
  159. memset(&ble_cfg, 0x00, sizeof(ble_cfg));
  160. ble_cfg.gatts_cfg.service_changed.service_changed = NRF_SDH_BLE_SERVICE_CHANGED;
  161. ret_code = sd_ble_cfg_set(BLE_GATTS_CFG_SERVICE_CHANGED, &ble_cfg, *p_ram_start);
  162. if (ret_code != NRF_SUCCESS)
  163. {
  164. NRF_LOG_ERROR("sd_ble_cfg_set() returned %s when attempting to set BLE_GATTS_CFG_SERVICE_CHANGED.",
  165. nrf_strerror_get(ret_code));
  166. }
  167. return NRF_SUCCESS;
  168. }
  169. /**@brief Function for finding the end address of the RAM. */
  170. static uint32_t ram_end_address_get(void)
  171. {
  172. uint32_t ram_total_size;
  173. #ifdef NRF51
  174. uint32_t block_size = NRF_FICR->SIZERAMBLOCKS;
  175. ram_total_size = block_size * NRF_FICR->NUMRAMBLOCK;
  176. #else
  177. ram_total_size = NRF_FICR->INFO.RAM * 1024;
  178. #endif
  179. return RAM_START + ram_total_size;
  180. }
  181. ret_code_t nrf_sdh_ble_enable(uint32_t * const p_app_ram_start)
  182. {
  183. // Start of RAM, obtained from linker symbol.
  184. uint32_t const app_ram_start_link = *p_app_ram_start;
  185. ret_code_t ret_code = sd_ble_enable(p_app_ram_start);
  186. if (*p_app_ram_start > app_ram_start_link)
  187. {
  188. NRF_LOG_WARNING("Insufficient RAM allocated for the SoftDevice.");
  189. NRF_LOG_WARNING("Change the RAM start location from 0x%x to 0x%x.",
  190. app_ram_start_link, *p_app_ram_start);
  191. NRF_LOG_WARNING("Maximum RAM size for application is 0x%x.",
  192. ram_end_address_get() - (*p_app_ram_start));
  193. }
  194. else
  195. {
  196. NRF_LOG_DEBUG("RAM starts at 0x%x", app_ram_start_link);
  197. if (*p_app_ram_start != app_ram_start_link)
  198. {
  199. NRF_LOG_DEBUG("RAM start location can be adjusted to 0x%x.", *p_app_ram_start);
  200. NRF_LOG_DEBUG("RAM size for application can be adjusted to 0x%x.",
  201. ram_end_address_get() - (*p_app_ram_start));
  202. }
  203. }
  204. if (ret_code == NRF_SUCCESS)
  205. {
  206. m_stack_is_enabled = true;
  207. }
  208. else
  209. {
  210. NRF_LOG_ERROR("sd_ble_enable() returned %s.", nrf_strerror_get(ret_code));
  211. }
  212. return ret_code;
  213. }
  214. /**@brief Function for polling BLE events.
  215. *
  216. * @param[in] p_context Context of the observer.
  217. */
  218. static void nrf_sdh_ble_evts_poll(void * p_context)
  219. {
  220. UNUSED_VARIABLE(p_context);
  221. ret_code_t ret_code;
  222. if (!m_stack_is_enabled)
  223. {
  224. return;
  225. }
  226. while (true)
  227. {
  228. /*lint -save -e(587) */
  229. __ALIGN(4) uint8_t evt_buffer[NRF_SDH_BLE_EVT_BUF_SIZE];
  230. /*lint -restore */
  231. ble_evt_t * p_ble_evt;
  232. uint16_t evt_len = (uint16_t)sizeof(evt_buffer);
  233. ret_code = sd_ble_evt_get(evt_buffer, &evt_len);
  234. if (ret_code != NRF_SUCCESS)
  235. {
  236. break;
  237. }
  238. p_ble_evt = (ble_evt_t *)evt_buffer;
  239. NRF_LOG_DEBUG("BLE event: 0x%x.", p_ble_evt->header.evt_id);
  240. // Forward the event to BLE observers.
  241. nrf_section_iter_t iter;
  242. for (nrf_section_iter_init(&iter, &sdh_ble_observers);
  243. nrf_section_iter_get(&iter) != NULL;
  244. nrf_section_iter_next(&iter))
  245. {
  246. nrf_sdh_ble_evt_observer_t * p_observer;
  247. nrf_sdh_ble_evt_handler_t handler;
  248. p_observer = (nrf_sdh_ble_evt_observer_t *)nrf_section_iter_get(&iter);
  249. handler = p_observer->handler;
  250. handler(p_ble_evt, p_observer->p_context);
  251. }
  252. }
  253. if (ret_code != NRF_ERROR_NOT_FOUND)
  254. {
  255. APP_ERROR_HANDLER(ret_code);
  256. }
  257. }
  258. NRF_SDH_STACK_OBSERVER(m_nrf_sdh_ble_evts_poll, NRF_SDH_BLE_STACK_OBSERVER_PRIO) =
  259. {
  260. .handler = nrf_sdh_ble_evts_poll,
  261. .p_context = NULL,
  262. };
  263. #endif // NRF_MODULE_ENABLED(NRF_SDH_BLE)