五、Design an application using BlueNRG, BlueNRG-MS ACI APIs
对BlueNRG, BlueNRG-MS的基本操作:
1.初始化
- Initialize list heads of ready and free hci data packet queues
--> HCI_Init();
- Init SPI interface for allowing external microcontroller to get access to the BlueNRG features properly
--> SdkEvalSpiInit(SPI_MODE_EXTI);
- Reset the BlueNRG, BlueNRG-MS network coprocessor
--> BlueNRG_RST();
- Configure BlueNRG, BlueNRG-MS public address (if public address is used)
--> uint8_t bdaddr[] = {0xaa, 0x00, 0x00, 0xE1, 0x80, 0x02};
ret = aci_hal_write_config_data(CONFIG_DATA_PUBADDR_OFFSET,
CONFIG_DATA_PUBADDR_LEN,bdaddr);
if(ret) PRINTF("Setting BD_ADDR failed.n");
--> ret = aci_gatt_init();
if(ret) PRINTF("GATT_Init failed.n");
- Init BLE NRG GAP layer depending on the selected device role
--> uint16_t service_handle, dev_name_char_handle, appearance_char_handle;
#if GAP_PERIPHERAL
uint8_t role = GAP_PERIPHERAL_ROLE;
#else
uint8_t role = GAP_CENTRAL_ROLE;
#endif
#if BLUENRG_MS
ret = aci_gap_init(role, 0, 0x07, &service_handle,
&dev_name_char_handle, &appearance_char_handle);
#else
ret = aci_gap_init(role, &service_handle, &dev_name_char_handle,
&appearance_char_handle);
#endif
if(ret) PRINTF("GAP_Init failed.n");
- Set the proper security I/O capability and authentication requirement (if BLE NRG security is used)
--> #if GATT_SERVER
/* User application function where service and characteristics are defined: refer to Section Services & Characteristics Configuration Section
*/
ret = Add_Server_Services_Characteristics();
if(ret == BLE_STATUS_SUCCESS)
PRINTF("Services & Characteristics added successfully.n");
#else
PRINTF("Error while adding Services & Characteristics.n");
#endif
- Define the required Services & Characteristics if the device is a GATT server
- Add a while(1) loopcalling the HCI_Process() API and a specific user application function where user actions/events are processed (advertising, connections, services and characteristics discovery, notification and related events).
--> HCI_Process()
2.Events and events Callback
The ACI framework notifies this event to the user application through the HCI_Event_CB() callback. The HCI_Event_CB() callback is called within the HCI_Process()on file hci.c
在CANNON蓝牙LED例程里对应的代码为:
/* Initialize the BlueNRG SPI driver */
BNRG_SPI_Init();
/* Initialize the BlueNRG HCI */
HCI_Init();
/* Reset BlueNRG hardware */
BlueNRG_RST();
/* loopcalling the HCI_Process() API */
HCI_Process();
在HCI_Process()函数里不断得判断事件队列是否不为空,如果不为空则说明有事件触发,调用HCI_Event_CB()来处理该事件。
蓝牙LED例程中
手机APP向BlueNRG 发送数据会触发EVT_VENDOR,然后对数据进行判断,调用函数ble_device_on_message()去实现LED的亮与灭。