123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326 |
- #include "sdk_common.h"
- #if NRF_MODULE_ENABLED(NRF_SDH_BLE)
- #include "nrf_sdh_ble.h"
- #include "nrf_sdh.h"
- #include "app_error.h"
- #include "nrf_strerror.h"
- #define NRF_LOG_MODULE_NAME nrf_sdh_ble
- #if NRF_SDH_BLE_LOG_ENABLED
- #define NRF_LOG_LEVEL NRF_SDH_BLE_LOG_LEVEL
- #define NRF_LOG_INFO_COLOR NRF_SDH_BLE_INFO_COLOR
- #define NRF_LOG_DEBUG_COLOR NRF_SDH_BLE_DEBUG_COLOR
- #else
- #define NRF_LOG_LEVEL 0
- #endif
- #include "nrf_log.h"
- NRF_LOG_MODULE_REGISTER();
- NRF_SECTION_SET_DEF(sdh_ble_observers, nrf_sdh_ble_evt_observer_t, NRF_SDH_BLE_OBSERVER_PRIO_LEVELS);
- #if defined(__CC_ARM)
- extern uint32_t Image$$RW_IRAM1$$Base;
- uint32_t const * const m_ram_start = &Image$$RW_IRAM1$$Base;
- #elif defined(__ICCARM__)
- extern uint32_t __ICFEDIT_region_RAM_start__;
- uint32_t const * const m_ram_start = &__ICFEDIT_region_RAM_start__;
- #elif defined(__SES_ARM)
- extern uint32_t __app_ram_start__;
- uint32_t const * const m_ram_start = &__app_ram_start__;
- #elif defined(__GNUC__)
- extern uint32_t __data_start__;
- uint32_t const * const m_ram_start = &__data_start__;
- #endif
- #define RAM_START 0x20000000
- #define APP_RAM_START (uint32_t)m_ram_start
- static bool m_stack_is_enabled;
- ret_code_t nrf_sdh_ble_app_ram_start_get(uint32_t * p_app_ram_start)
- {
- if (p_app_ram_start == NULL)
- {
- return NRF_ERROR_NULL;
- }
- *p_app_ram_start = APP_RAM_START;
- return NRF_SUCCESS;
- }
- ret_code_t nrf_sdh_ble_default_cfg_set(uint8_t conn_cfg_tag, uint32_t * p_ram_start)
- {
- uint32_t ret_code;
- ret_code = nrf_sdh_ble_app_ram_start_get(p_ram_start);
- if (ret_code != NRF_SUCCESS)
- {
- return ret_code;
- }
- #if defined (S112) || defined(S312)
- STATIC_ASSERT(NRF_SDH_BLE_CENTRAL_LINK_COUNT == 0, "When using s112, NRF_SDH_BLE_CENTRAL_LINK_COUNT must be 0.");
- #endif
-
-
-
-
- ble_cfg_t ble_cfg;
- #if (NRF_SDH_BLE_TOTAL_LINK_COUNT != 0)
-
- memset(&ble_cfg, 0, sizeof(ble_cfg));
- ble_cfg.conn_cfg.conn_cfg_tag = conn_cfg_tag;
- ble_cfg.conn_cfg.params.gap_conn_cfg.conn_count = NRF_SDH_BLE_TOTAL_LINK_COUNT;
- ble_cfg.conn_cfg.params.gap_conn_cfg.event_length = NRF_SDH_BLE_GAP_EVENT_LENGTH;
- ret_code = sd_ble_cfg_set(BLE_CONN_CFG_GAP, &ble_cfg, *p_ram_start);
- if (ret_code != NRF_SUCCESS)
- {
- NRF_LOG_ERROR("sd_ble_cfg_set() returned %s when attempting to set BLE_CONN_CFG_GAP.",
- nrf_strerror_get(ret_code));
- }
-
- memset(&ble_cfg, 0, sizeof(ble_cfg));
- #if !defined (S122)
- ble_cfg.gap_cfg.role_count_cfg.periph_role_count = NRF_SDH_BLE_PERIPHERAL_LINK_COUNT;
- #endif
- #if !defined (S112) && !defined(S312) && !defined(S113)
- ble_cfg.gap_cfg.role_count_cfg.central_role_count = NRF_SDH_BLE_CENTRAL_LINK_COUNT;
- ble_cfg.gap_cfg.role_count_cfg.central_sec_count = MIN(NRF_SDH_BLE_CENTRAL_LINK_COUNT,
- BLE_GAP_ROLE_COUNT_CENTRAL_SEC_DEFAULT);
- #endif
- ret_code = sd_ble_cfg_set(BLE_GAP_CFG_ROLE_COUNT, &ble_cfg, *p_ram_start);
- if (ret_code != NRF_SUCCESS)
- {
- NRF_LOG_ERROR("sd_ble_cfg_set() returned %s when attempting to set BLE_GAP_CFG_ROLE_COUNT.",
- nrf_strerror_get(ret_code));
- }
-
- #if (NRF_SDH_BLE_GATT_MAX_MTU_SIZE != 23)
- memset(&ble_cfg, 0x00, sizeof(ble_cfg));
- ble_cfg.conn_cfg.conn_cfg_tag = conn_cfg_tag;
- ble_cfg.conn_cfg.params.gatt_conn_cfg.att_mtu = NRF_SDH_BLE_GATT_MAX_MTU_SIZE;
- ret_code = sd_ble_cfg_set(BLE_CONN_CFG_GATT, &ble_cfg, *p_ram_start);
- if (ret_code != NRF_SUCCESS)
- {
- NRF_LOG_ERROR("sd_ble_cfg_set() returned %s when attempting to set BLE_CONN_CFG_GATT.",
- nrf_strerror_get(ret_code));
- }
- #endif
- #endif
-
- memset(&ble_cfg, 0, sizeof(ble_cfg));
- ble_cfg.common_cfg.vs_uuid_cfg.vs_uuid_count = NRF_SDH_BLE_VS_UUID_COUNT;
- ret_code = sd_ble_cfg_set(BLE_COMMON_CFG_VS_UUID, &ble_cfg, *p_ram_start);
- if (ret_code != NRF_SUCCESS)
- {
- NRF_LOG_ERROR("sd_ble_cfg_set() returned %s when attempting to set BLE_COMMON_CFG_VS_UUID.",
- nrf_strerror_get(ret_code));
- }
-
- memset(&ble_cfg, 0x00, sizeof(ble_cfg));
- ble_cfg.gatts_cfg.attr_tab_size.attr_tab_size = NRF_SDH_BLE_GATTS_ATTR_TAB_SIZE;
- ret_code = sd_ble_cfg_set(BLE_GATTS_CFG_ATTR_TAB_SIZE, &ble_cfg, *p_ram_start);
- if (ret_code != NRF_SUCCESS)
- {
- NRF_LOG_ERROR("sd_ble_cfg_set() returned %s when attempting to set BLE_GATTS_CFG_ATTR_TAB_SIZE.",
- nrf_strerror_get(ret_code));
- }
-
- memset(&ble_cfg, 0x00, sizeof(ble_cfg));
- ble_cfg.gatts_cfg.service_changed.service_changed = NRF_SDH_BLE_SERVICE_CHANGED;
- ret_code = sd_ble_cfg_set(BLE_GATTS_CFG_SERVICE_CHANGED, &ble_cfg, *p_ram_start);
- if (ret_code != NRF_SUCCESS)
- {
- NRF_LOG_ERROR("sd_ble_cfg_set() returned %s when attempting to set BLE_GATTS_CFG_SERVICE_CHANGED.",
- nrf_strerror_get(ret_code));
- }
- return NRF_SUCCESS;
- }
- static uint32_t ram_end_address_get(void)
- {
- uint32_t ram_total_size;
- #ifdef NRF51
- uint32_t block_size = NRF_FICR->SIZERAMBLOCKS;
- ram_total_size = block_size * NRF_FICR->NUMRAMBLOCK;
- #else
- ram_total_size = NRF_FICR->INFO.RAM * 1024;
- #endif
- return RAM_START + ram_total_size;
- }
- ret_code_t nrf_sdh_ble_enable(uint32_t * const p_app_ram_start)
- {
-
- uint32_t const app_ram_start_link = *p_app_ram_start;
- ret_code_t ret_code = sd_ble_enable(p_app_ram_start);
- if (*p_app_ram_start > app_ram_start_link)
- {
- NRF_LOG_WARNING("Insufficient RAM allocated for the SoftDevice.");
- NRF_LOG_WARNING("Change the RAM start location from 0x%x to 0x%x.",
- app_ram_start_link, *p_app_ram_start);
- NRF_LOG_WARNING("Maximum RAM size for application is 0x%x.",
- ram_end_address_get() - (*p_app_ram_start));
- }
- else
- {
- NRF_LOG_DEBUG("RAM starts at 0x%x", app_ram_start_link);
- if (*p_app_ram_start != app_ram_start_link)
- {
- NRF_LOG_DEBUG("RAM start location can be adjusted to 0x%x.", *p_app_ram_start);
- NRF_LOG_DEBUG("RAM size for application can be adjusted to 0x%x.",
- ram_end_address_get() - (*p_app_ram_start));
- }
- }
- if (ret_code == NRF_SUCCESS)
- {
- m_stack_is_enabled = true;
- }
- else
- {
- NRF_LOG_ERROR("sd_ble_enable() returned %s.", nrf_strerror_get(ret_code));
- }
- return ret_code;
- }
- static void nrf_sdh_ble_evts_poll(void * p_context)
- {
- UNUSED_VARIABLE(p_context);
- ret_code_t ret_code;
- if (!m_stack_is_enabled)
- {
- return;
- }
- while (true)
- {
-
- __ALIGN(4) uint8_t evt_buffer[NRF_SDH_BLE_EVT_BUF_SIZE];
-
- ble_evt_t * p_ble_evt;
- uint16_t evt_len = (uint16_t)sizeof(evt_buffer);
- ret_code = sd_ble_evt_get(evt_buffer, &evt_len);
- if (ret_code != NRF_SUCCESS)
- {
- break;
- }
- p_ble_evt = (ble_evt_t *)evt_buffer;
- NRF_LOG_DEBUG("BLE event: 0x%x.", p_ble_evt->header.evt_id);
-
- nrf_section_iter_t iter;
- for (nrf_section_iter_init(&iter, &sdh_ble_observers);
- nrf_section_iter_get(&iter) != NULL;
- nrf_section_iter_next(&iter))
- {
- nrf_sdh_ble_evt_observer_t * p_observer;
- nrf_sdh_ble_evt_handler_t handler;
- p_observer = (nrf_sdh_ble_evt_observer_t *)nrf_section_iter_get(&iter);
- handler = p_observer->handler;
- handler(p_ble_evt, p_observer->p_context);
- }
- }
- if (ret_code != NRF_ERROR_NOT_FOUND)
- {
- APP_ERROR_HANDLER(ret_code);
- }
- }
- NRF_SDH_STACK_OBSERVER(m_nrf_sdh_ble_evts_poll, NRF_SDH_BLE_STACK_OBSERVER_PRIO) =
- {
- .handler = nrf_sdh_ble_evts_poll,
- .p_context = NULL,
- };
- #endif
|