From c42094626a1dfd38d8ff039da2cfd0dd21398eb2 Mon Sep 17 00:00:00 2001 From: Roy Date: Thu, 20 Jul 2023 16:24:56 +0800 Subject: [PATCH] enable ble stack --- app_config.h | 13 +++++++++++++ main.c | 45 ++++++++++++++++++++++++++++++++++++++++++++- 2 files changed, 57 insertions(+), 1 deletion(-) diff --git a/app_config.h b/app_config.h index 8ea1cee..963257f 100644 --- a/app_config.h +++ b/app_config.h @@ -83,6 +83,19 @@ extern "C" #define POWER_CONFIG_IRQ_PRIORITY 6 #define CLOCK_CONFIG_IRQ_PRIORITY 6 +// SoftDevice BLE event handler +#define NRF_SDH_BLE_ENABLED 1 +// The number of vendor-specific UUIDs. +#define NRF_SDH_BLE_VS_UUID_COUNT 0 +// Attribute Table size in bytes. The size must be a multiple of 4. +#define NRF_SDH_BLE_GATTS_ATTR_TAB_SIZE 1408 +// BLE Observers - Observers and priority levels +#define NRF_SDH_BLE_OBSERVER_PRIO_LEVELS 4 +// Include the Service Changed characteristic in the Attribute Table. +#define NRF_SDH_BLE_SERVICE_CHANGED 0 +// Static maximum MTU size. +#define NRF_SDH_BLE_GATT_MAX_MTU_SIZE 247 + #ifdef __cplusplus } #endif diff --git a/main.c b/main.c index 5dee871..aa2e064 100644 --- a/main.c +++ b/main.c @@ -6,9 +6,9 @@ extern "C" { #endif -#include "sdk_config.h" #include "app_config.h" #include "apply_old_config.h" +#include "sdk_config.h" #include "nrf_log.h" #include "nrf_log_ctrl.h" @@ -17,6 +17,10 @@ extern "C" #include "nrf_delay.h" #include "nrf_gpio.h" +#include "nrf_sdh.h" +#include "nrf_sdh_ble.h" +#include "nrf_sdh_soc.h" + #include "FreeRTOS.h" #include "task.h" @@ -24,6 +28,43 @@ extern "C" } #endif +#define APP_BLE_CONN_CFG_TAG 1 /**< A tag identifying the SoftDevice BLE configuration. */ +#define APP_BLE_OBSERVER_PRIO 3 /**< Application's BLE observer priority. You shouldn't need to modify this value. */ + +static void ble_evt_handler(ble_evt_t const *p_ble_evt, void *p_context) +{ + // TODO... + __BKPT(255); +} + +static void le_stack_Init(void) +{ + ret_code_t err_code; + uint32_t ram_start = 0; + ble_cfg_t ble_cfg; + memset(&ble_cfg, 0x00, sizeof(ble_cfg)); + ble_cfg.conn_cfg.conn_cfg_tag = APP_BLE_CONN_CFG_TAG; + ble_cfg.conn_cfg.params.gatts_conn_cfg.hvn_tx_queue_size = 8; + + err_code = nrf_sdh_enable_request(); + APP_ERROR_CHECK(err_code); + + // Configure the BLE stack using the default settings. + // Fetch the start address of the application RAM. + err_code = nrf_sdh_ble_default_cfg_set(APP_BLE_CONN_CFG_TAG, &ram_start); + APP_ERROR_CHECK(err_code); + + err_code = sd_ble_cfg_set(BLE_CONN_CFG_GATTS, &ble_cfg, ram_start); + APP_ERROR_CHECK(err_code); + + // Enable BLE stack. + err_code = nrf_sdh_ble_enable(&ram_start); + APP_ERROR_CHECK(err_code); + + // Register a handler for BLE events. + NRF_SDH_BLE_OBSERVER(m_ble_observer, APP_BLE_OBSERVER_PRIO, ble_evt_handler, NULL); +} + void led1_task(void *pArg) { nrf_gpio_cfg_output(13); @@ -62,6 +103,8 @@ int main(void) NRF_LOG_DEFAULT_BACKENDS_INIT(); NRF_LOG_INFO("%s Build: %s %s", DEVICE_NAME, __TIME__, __DATE__); + le_stack_Init(); + xTaskCreate(led1_task, "led1_task", 160, NULL, 3, NULL); xTaskCreate(led2_task, "led2_task", 160, NULL, 3, NULL);