本帖最后由 南来之风 于 2023-7-31 16:12 编辑
STM32WBA52CG是支持蓝牙BLE 5.3,官方提供的STM32Cube_FW_WBA_V1.1.0资料包中提供了一个非常好的入门案例:BLE_p2pServer。
准备材料:
Keil, ST BLE Toolbox
图形化配置时钟:
主要的代码包括在主函数中调用aci_gatt_init()来初始化GATT接口,调用aci_gap_init()来初始化GAP:
/* Initialize GATT interface */
ret = aci_gatt_init();
if (ret != BLE_STATUS_SUCCESS)
{
APP_DBG_MSG(" Fail : aci_gatt_init command, result: 0x%02X\n", ret);
}
else
{
APP_DBG_MSG(" Success: aci_gatt_init command\n");
}
/* Initialize GAP interface */
role = 0U;
role |= GAP_PERIPHERAL_ROLE;
/* USER CODE BEGIN Role_Mngt*/
/* USER CODE END Role_Mngt */
if (role > 0)
{
ret = aci_gap_init(role,
#if ((CFG_BD_ADDRESS_TYPE == GAP_RESOLVABLE_PRIVATE_ADDR) || (CFG_BD_ADDRESS_TYPE == GAP_NON_RESOLVABLE_PRIVATE_ADDR))
PRIVACY_ENABLED,
#else
PRIVACY_DISABLED,
#endif /* (CFG_BD_ADDRESS_TYPE == GAP_RESOLVABLE_PRIVATE_ADDR) || (CFG_BD_ADDRESS_TYPE == GAP_NON_RESOLVABLE_PRIVATE_ADDR) */
sizeof(a_GapDeviceName),
&gap_service_handle,
&gap_dev_name_char_handle,
&gap_appearance_char_handle);
相关的函数调用栈:
main() -> MX_APPE_Init() -> APP_BLE_Init() -> Ble_Hci_Gap_Gatt_Init()
安卓手机安装ST BLE TOOLBOX V1.3.5,这个版本支持STM32WB系列的新产品例程。
打开ST蓝牙助手,找到例程对应的ST ble设备,即下图的p2pS_00。这里的设备名称与官方文档中的有些差异:- Open ST BLE Toolbox application: select the P2PS_WBAxx in the device list, where xx is the 2 last digits of the BD ADDRESS.
点击Connect后,串口助手输出如下:==>> Start Ble_Hci_Gap_Gatt_Init function
Success: aci_hal_write_config_data command - CONFIG_DATA_PUBADDR_OFFSET
Public Bluetooth Address: 00:80:e1:2a:18:00
Success: aci_hal_write_config_data command - CONFIG_DATA_IR_OFFSET
Success: aci_hal_write_config_data command - CONFIG_DATA_ER_OFFSET
Success: aci_hal_set_tx_power_level command
Success: aci_gatt_init command
Success: aci_gap_init command
Success: aci_gatt_update_char_value - Device Name
Success: aci_gatt_update_char_value - Appearance
Success: hci_le_set_default_phy command
Success: aci_gap_set_io_capability command
Success: aci_gap_set_authentication_requirement command
Success: aci_gap_configure_whitelist command
==>> End Ble_Hci_Gap_Gatt_Init function
Services and Characteristics creation
Success: aci_gatt_add_service command: P2P_Server
Success: aci_gatt_add_char command : LED_C
Success: aci_gatt_add_char command : SWITCH_C
End of Services and Characteristics creation
Success: aci_hal_set_radio_activity_mask command
==>> aci_gap_set_discoverable - Success
==>> Success: Start Advertising
>>== HCI_LE_CONNECTION_COMPLETE_SUBEVT_CODE - Connection handle: 0x0001
- Connection established with @:7e:3b:af:7b:98:e6
>>== HCI_LE_CONNECTION_UPDATE_COMPLETE_SUBEVT_CODE
>>== HCI_LE_CONNECTION_UPDATE_COMPLETE_SUBEVT_CODE
-- P2P APPLICATION SERVER : NOTIFICATION ENABLED
点击LED灯的图标,即可打开或者关闭板子上的LED.
串口助手的输出:
-- GATT : LED CONFIGURATION RECEIVED
-- P2P APPLICATION SERVER : LED1 ON
-- GATT : LED CONFIGURATION RECEIVED
-- P2P APPLICATION SERVER : LED1 OFF
在开发板上按BTN1,Button 1 pressed
-- P2P APPLICATION SERVER : INFORM CLIENT BUTTON 1 PUSHED
Success: aci_gatt_update_char_value SWITCH_C command
实物图:
附件:ST BLE TOOLBOX
ST BLE Toolbox_1.3.5_Apkpure.zip
(5.76 MB)
|