2018年7月19日(2018年8月2日更新)
学习使用nrf52832,感觉坑真大,看起来实在是要好好阅读一下提供的例程源码了.
SDK版本:nRF5_SDK_15.0.0_a53641a
SoftDevice版本:s132_nrf52_6.0.0
例程:nRF5_SDK_15.0.0_a53641a\examples\ble_peripheral\ble_app_blinky
主函数开始:
主函数:
/**@brief Function for application main entry.
*/
int main(void)
{
// Initialize.
log_init();
leds_init();
timers_init();
buttons_init();
power_management_init();
ble_stack_init();
gap_params_init();
gatt_init();
services_init();
advertising_init();
conn_params_init();
// Start execution.
NRF_LOG_INFO("Blinky example started.");
advertising_start();
// Enter main loop.
for (;;)
{
idle_state_handle();
}
}
没什么好说的,主要看蓝牙初始化部分吧
蓝牙初始化部分:
蓝牙:协议栈内存初始化
ble_stack_init
初始化协议栈的内存分配,设置回调函数为ble_evt_handler
static void ble_stack_init(void)
{
ret_code_t err_code;
//使能SoftDevice,能理解,所有蓝牙有关的都在SoftDevice中,所以首先必须使能SoftDevice
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.
uint32_t ram_start = 0;
err_code = nrf_sdh_ble_default_cfg_set(APP_BLE_CONN_CFG_TAG, &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);
}
蓝牙:GAP初始化
蓝牙BLE建立GATT连接前,必须经过GAP协议,GAP用来控制设备的连接广播等,GAP使设备广播或可别发现、可被连接等.
gap_params_init
就是进行GAP初始化,设定连接参数
static void gap_params_init(void)
{
ret_code_t err_code;
ble_gap_conn_params_t gap_conn_params;
ble_gap_conn_sec_mode_t sec_mode;
BLE_GAP_CONN_SEC_MODE_SET_OPEN(&sec_mode);//加密级别为Security Mode 1 Level 1
//设置蓝牙名称
err_code = sd_ble_gap_device_name_set(&sec_mode,(const uint8_t *)DEVICE_NAME,strlen(DEVICE_NAME));
APP_ERROR_CHECK(err_code);
//Set GAP Peripheral Preferred Connection Parameters.
memset(&gap_conn_params, 0, sizeof(gap_conn_params));
gap_conn_params.min_conn_interval = MIN_CONN_INTERVAL;
gap_conn_params.max_conn_interval = MAX_CONN_INTERVAL;
gap_conn_params.slave_latency = SLAVE_LATENCY;
gap_conn_params.conn_sup_timeout = CONN_SUP_TIMEOUT;
err_code = sd_ble_gap_ppcp_set(&gap_conn_params);
APP_ERROR_CHECK(err_code);
}
GAP四种广播:
通用广播:通用广播是用途最广的广播方式。进行通用广播的设备能够被扫描设备扫描到,或者在接收到连接请求时作为从设备进入一个连接。通用广播可以在没有连接的情况下发出,换句话说,没有主从设备之分。
定向广播:为了尽可能快的建立连接。这种报文包含两个地址:广播者的地址和发起者的地址。发起设备收到发绐自己的定向广播报文后,可以立即发送连接请求作为回应。
不可连接广播:只广播数据,不能被扫描或者连接。只能根据主机的要求在广播态和就绪态之间切换。
可发现广播:不能用于发起连接,但允许其他设备扫描该广播设备。设备可以被发现,既可以广播数据,又可以响应扫描,但不能建立连接。这是一种适用于广播数据的广播形式,动态数据可以包含于广播数据之中,而静态数据可以包含于扫描响应数据之中。可发现广播不会进入连接态,而只能在停止后回到就绪态。
蓝牙:GATT初始化
GATT才是真正BLE的部分了.
/**@brief Function for initializing the GATT module.
*/
static void gatt_init(void)
{
ret_code_t err_code = nrf_ble_gatt_init(&m_gatt, NULL);
APP_ERROR_CHECK(err_code);
}
这就没什么说的了,根本就是直接调用了SDK中的ret_code_t nrf_ble_gatt_init(nrf_ble_gatt_t * p_gatt,nrf_ble_gatt_evt_handler_t evt_handler )
,很好理解,问题是,m_gatt的定义并不是简单的nrf_ble_gatt_t m_gatt;
,而是使用宏:NRF_BLE_GATT_DEF(m_gatt);
进行定义
/**@brief Macro for defining a nrf_ble_gatt instance.
*
* @param _name Name of the instance.
* @hideinitializer
*/
#define NRF_BLE_GATT_DEF(_name) \
static nrf_ble_gatt_t _name; \
NRF_SDH_BLE_OBSERVER(_name ## _obs, \
NRF_BLE_GATT_BLE_OBSERVER_PRIO, \
nrf_ble_gatt_on_ble_evt, &_name)
SDK中也说明了此宏目的是Macro for defining a nrf_ble_gatt instance.
关键在此宏中调用的NRF_SDH_BLE_OBSERVER
,
Macro for registering nrf_sdh_soc_evt_observer_t. Modules that want to be notified about SoC events must register the handler using this macro.
主机(手机)通过需要蓝牙写入,必须使用此宏注册处理处理函数!如果不使用,导致写入数据后SOC无法处理写入的数据.
吐槽下,sdk中根本没有说明处理流程,必须看到某个函数内容才说明此函数必须调用,却不说明此函数相关的函数必须也要使用,所以很容易漏掉一些重要内容,必须要将所有的api都看一遍才行.难道是Datasheet中有写?还是我漏掉了什么必看的文档?
Services初始化
/**@brief Function for initializing services that will be used by the application.
*/
static void services_init(void)
{
ret_code_t err_code;
ble_lbs_init_t init = {0};
nrf_ble_qwr_init_t qwr_init = {0};
// Initialize Queued Write Module.
qwr_init.error_handler = nrf_qwr_error_handler;
err_code = nrf_ble_qwr_init(&m_qwr, &qwr_init);
APP_ERROR_CHECK(err_code);
// Initialize LBS.
init.led_write_handler = led_write_handler;
err_code = ble_lbs_init(&m_lbs, &init);
APP_ERROR_CHECK(err_code);
}
暂时不看nrf_ble_qwr_init函数~
主要来看ble_lbs_init,此函数目的是增加led及button的相关Service Characteristic等
uint32_t ble_lbs_init(ble_lbs_t * p_lbs, const ble_lbs_init_t * p_lbs_init)
{
uint32_t err_code;
ble_uuid_t ble_uuid;
// Initialize service structure.
p_lbs->led_write_handler = p_lbs_init->led_write_handler;
// Add service.
ble_uuid128_t base_uuid = {LBS_UUID_BASE};
err_code = sd_ble_uuid_vs_add(&base_uuid, &p_lbs->uuid_type);
VERIFY_SUCCESS(err_code);
ble_uuid.type = p_lbs->uuid_type;
ble_uuid.uuid = LBS_UUID_SERVICE;
err_code = sd_ble_gatts_service_add(BLE_GATTS_SRVC_TYPE_PRIMARY, &ble_uuid, &p_lbs->service_handle);
VERIFY_SUCCESS(err_code);
// Add characteristics.
err_code = button_char_add(p_lbs, p_lbs_init);
VERIFY_SUCCESS(err_code);
err_code = led_char_add(p_lbs, p_lbs_init);
VERIFY_SUCCESS(err_code);
return NRF_SUCCESS;
}
不做详细解析,后续一篇专门来看GATT的service部分:简书 nRF52832 GATT 自定义Service/Characteristic
广播配置
广播初始化,设定广播周期,广播超时时间等参数,其中还会配置广播的uuid信息
/**@brief Function for initializing the Advertising functionality.
*
* @details Encodes the required advertising data and passes it to the stack.
* Also builds a structure to be passed to the stack when starting advertising.
*/
static void advertising_init(void)
{
ret_code_t err_code;
ble_advdata_t advdata;
ble_advdata_t srdata;
ble_uuid_t adv_uuids[] = {{LBS_UUID_SERVICE, m_lbs.uuid_type}};
// Build and set advertising data.
memset(&advdata, 0, sizeof(advdata));
advdata.name_type = BLE_ADVDATA_FULL_NAME;
advdata.include_appearance = true;
advdata.flags = BLE_GAP_ADV_FLAGS_LE_ONLY_GENERAL_DISC_MODE;
memset(&srdata, 0, sizeof(srdata));
srdata.uuids_complete.uuid_cnt = sizeof(adv_uuids) / sizeof(adv_uuids[0]);
srdata.uuids_complete.p_uuids = adv_uuids;
err_code = ble_advdata_encode(&advdata, m_adv_data.adv_data.p_data, &m_adv_data.adv_data.len);
APP_ERROR_CHECK(err_code);
err_code = ble_advdata_encode(&srdata, m_adv_data.scan_rsp_data.p_data, &m_adv_data.scan_rsp_data.len);
APP_ERROR_CHECK(err_code);
ble_gap_adv_params_t adv_params;
// Set advertising parameters.
memset(&adv_params, 0, sizeof(adv_params));
adv_params.primary_phy = BLE_GAP_PHY_1MBPS;
adv_params.duration = APP_ADV_DURATION;
adv_params.properties.type = BLE_GAP_ADV_TYPE_CONNECTABLE_SCANNABLE_UNDIRECTED;
adv_params.p_peer_addr = NULL;
adv_params.filter_policy = BLE_GAP_ADV_FP_ANY;
adv_params.interval = APP_ADV_INTERVAL;
err_code = sd_ble_gap_adv_set_configure(&m_adv_handle, &m_adv_data, &adv_params);
APP_ERROR_CHECK(err_code);
}
涉及很多蓝牙的内容,最好配合蓝牙的资料来看
简书 nRF52832 广播相关配置
连接参数配置
蓝牙连接的一些参数配置信息,详细需要参考蓝牙文档了
/**@brief Function for initializing the Connection Parameters module.
*/
static void conn_params_init(void)
{
ret_code_t err_code;
ble_conn_params_init_t cp_init;
memset(&cp_init, 0, sizeof(cp_init));
cp_init.p_conn_params = NULL;
cp_init.first_conn_params_update_delay = FIRST_CONN_PARAMS_UPDATE_DELAY;
cp_init.next_conn_params_update_delay = NEXT_CONN_PARAMS_UPDATE_DELAY;
cp_init.max_conn_params_update_count = MAX_CONN_PARAMS_UPDATE_COUNT;
cp_init.start_on_notify_cccd_handle = BLE_GATT_HANDLE_INVALID;
cp_init.disconnect_on_fail = false;
cp_init.evt_handler = on_conn_params_evt;
cp_init.error_handler = conn_params_error_handler;
err_code = ble_conn_params_init(&cp_init);
APP_ERROR_CHECK(err_code);
}
启动广播
调用sd_ble_gap_adv_start
开始广播,同时点亮LED表示广播状态
/**@brief Function for starting advertising.
*/
static void advertising_start(void)
{
ret_code_t err_code;
err_code = sd_ble_gap_adv_start(m_adv_handle, APP_BLE_CONN_CFG_TAG);
APP_ERROR_CHECK(err_code);
bsp_board_led_on(ADVERTISING_LED);
}
网友评论