解决Flash操作与BLE事件互斥问题:时间管理和事件调度

需积分: 0 0 下载量 138 浏览量 更新于2024-08-05 收藏 615KB PDF 举报
本文档主要关注在使用BlueNRG-1/2芯片进行蓝牙低功耗(BLE)通信时,如何处理与Flash操作之间的互斥问题。由于Flash擦除操作过程中会暂时关闭中断,导致在该期间无线电功能可能会受到影响,例如失去蓝牙信号或设备死机。ST的官方软件开发工具包(SDK)虽然提供了访问Flash的示例,但在处理多连接、主从模式以及多种蓝牙事件的复杂应用场景时,确保Flash操作与蓝牙事件之间的同步变得尤为困难。 为了解决这个问题,作者建议采用以下策略: 1. **初始化与中断管理**: - 设置radio中断活动掩码,启用关键的中断事件(如0x0001, 0x0002, 0x0004, 0x0008, 0x0010, 0x0020),以确保在必要的时候能接收蓝牙事件。 - 重写aci_hal_end_of_radio_activity_event函数,每当有中断发生时,记录并创建一个空闲时间节点。 2. **双向链表管理**: - 使用双向链表结构来跟踪待执行的Flash操作,如擦除和写入任务。这样可以确保在有足够空闲时间时,按照先进先出(FIFO)原则执行Flash操作,而不是立即执行。 - 另外,还维护一个空闲时间链表,用于检测何时设备处于无蓝牙事件的空闲状态,以便执行Flash操作。 3. **主循环中的调度**: - 设计一个专门的Flash操作调度函数,定期检查系统是否拥有足够的空闲时间来执行下一个Flash操作。如果有,就从待执行链表中取出第一个任务并执行,然后更新链表。 4. **避免干扰**: - 避免在蓝牙数据传输或其他关键操作期间执行Flash擦除等长时间操作,以免对蓝牙性能造成干扰。 通过以上策略,开发者可以有效地平衡Flash操作与BLE事件之间的交互,减少因Flash操作导致的蓝牙不稳定问题,提升系统的稳定性和效率。这对于需要频繁操作Flash且依赖蓝牙功能的设备尤为重要,如物联网设备或嵌入式系统。

注释以下代码#define TP_PRIO configMAX_PRIORITIES - 5 static void ble_tp_connected(struct bt_conn *conn, u8_t err); static void ble_tp_disconnected(struct bt_conn *conn, u8_t reason); static int bl_tp_send_indicate(struct bt_conn *conn, const struct bt_gatt_attr *attr, const void *data, u16_t len); struct bt_conn *ble_tp_conn; struct bt_gatt_exchange_params exchg_mtu; TaskHandle_t ble_tp_task_h; int tx_mtu_size = 20; u8_t tp_start = 0; static u8_t created_tp_task = 0; static u8_t isRegister = 0; static struct bt_conn_cb ble_tp_conn_callbacks = { .connected = ble_tp_connected, .disconnected = ble_tp_disconnected, }; static void ble_tp_tx_mtu_size(struct bt_conn *conn, u8_t err, struct bt_gatt_exchange_params *params) { if(!err) { tx_mtu_size = bt_gatt_get_mtu(ble_tp_conn); BT_WARN("ble tp echange mtu size success, mtu size: %d", tx_mtu_size); } else { BT_WARN("ble tp echange mtu size failure, err: %d", err); } } static void ble_tp_connected(struct bt_conn *conn, u8_t err) { if(err || conn->type != BT_CONN_TYPE_LE) { return; } int tx_octets = 0x00fb; int tx_time = 0x0848; int ret = -1; BT_INFO("%s",__func__); ble_tp_conn = conn; . ret = bt_le_set_data_len(ble_tp_conn, tx_octets, tx_time); if(!ret) { BT_WARN("ble tp set data length success."); } else { BT_WARN("ble tp set data length failure, err: %d\n", ret); } exchg_mtu.func = ble_tp_tx_mtu_size; ret = bt_gatt_exchange_mtu(ble_tp_conn, &exchg_mtu); if (!ret) { BT_WARN("ble tp exchange mtu size pending."); } else { BT_WARN("ble tp exchange mtu size failure, err: %d", ret); } } static void ble_tp_disconnected(struct bt_conn *conn, u8_t reason) { if(conn->type != BT_CONN_TYPE_LE) { return; } BT_INFO("%s",__func__); ble_tp_conn = NULL; } static int ble_tp_recv_rd(struct bt_conn *conn, const struct bt_gatt_attr *attr, void *buf, u16_t len, u16_t offset) { int size = 9; char data[9] = {0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09}; memcpy(buf, data, size); return size; }

2023-05-19 上传

对以下代码进行注释并给出可复制代码static void ble_tp_notify_task(void *pvParameters) { int err = -1; char data[244] = {0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09}; while(1) { err = bt_gatt_notify(ble_tp_conn, get_attr(BT_CHAR_BLE_TP_NOT_ATTR_VAL_INDEX), data, (tx_mtu_size - 3)); BT_WARN("ble tp send notify : %d", err); } } static void ble_tp_not_ccc_changed(const struct bt_gatt_attr attr, u16_t value) { int err; BT_WARN("ccc:value=[%d]",value); if(tp_start) { if(value == BT_GATT_CCC_NOTIFY) { if(xTaskCreate(ble_tp_notify_task, (char)"bletp", 256, NULL, TP_PRIO, &ble_tp_task_h) == pdPASS) { created_tp_task = 1; BT_WARN("Create throughput tx task success."); } else { created_tp_task = 0; BT_WARN("Create throughput tx task fail."); } } else { if(created_tp_task) { BT_WARN("Delete throughput tx task."); vTaskDelete(ble_tp_task_h); created_tp_task = 0; } } } else { if(created_tp_task) { BT_WARN("Delete throughput tx task."); vTaskDelete(ble_tp_task_h); created_tp_task = 0; } if(value == BT_GATT_CCC_NOTIFY) { err = bt_gatt_notify(ble_tp_conn, get_attr(BT_CHAR_BLE_TP_NOT_ATTR_VAL_INDEX), "notify", strlen("notify")); BT_WARN("ble tp send indatcate: %d", err); } } } static struct bt_gatt_attr attrs[]= { BT_GATT_PRIMARY_SERVICE(BT_UUID_SVC_BLE_TP), BT_GATT_CHARACTERISTIC(BT_UUID_CHAR_BLE_TP_RD, BT_GATT_CHRC_READ, BT_GATT_PERM_READ, ble_tp_recv_rd, NULL, NULL), BT_GATT_CHARACTERISTIC(BT_UUID_CHAR_BLE_TP_WR, BT_GATT_CHRC_WRITE |BT_GATT_CHRC_WRITE_WITHOUT_RESP, BT_GATT_PERM_WRITE|BT_GATT_PERM_PREPARE_WRITE, NULL, ble_tp_recv_wr, NULL), BT_GATT_CHARACTERISTIC(BT_UUID_CHAR_BLE_TP_IND, BT_GATT_CHRC_INDICATE, 0, NULL, NULL, NULL), BT_GATT_CCC(ble_tp_ind_ccc_changed, BT_GATT_PERM_READ | BT_GATT_PERM_WRITE), BT_GATT_CHARACTERISTIC(BT_UUID_CHAR_BLE_TP_NOT, BT_GATT_CHRC_NOTIFY, 0, NULL, NULL, NULL), BT_GATT_CCC(ble_tp_not_ccc_changed, BT_GATT_PERM_READ | BT_GATT_PERM_WRITE) };

2023-05-19 上传

#define DEVICE_NAME "BL618_GATT" // 设备名称 #define PROFILE_NUM 1 // 设备支持的服务数量 #define PROFILE_A_APP_ID 0 // 第一个服务的ID static void gap_event_handler(ble_event_t *event); static void gatt_event_handler(ble_event_t *event); int main(void) { // 初始化蓝牙协议栈 bluetooth_init(gap_event_handler, gatt_event_handler); // 设置设备名称 bluetooth_set_device_name(DEVICE_NAME); // 创建一个服务 bluetooth_gatt_create_service(PROFILE_NUM); // 添加服务的特征值 bluetooth_gatt_add_char(PROFILE_A_APP_ID, "CHAR_A", 0xFF01, 0x20, NULL); // 开始广播 bluetooth_start_advertising(); while (1) { // 等待事件 bluetooth_wait_for_event(); } return 0; } static void gap_event_handler(ble_event_t *event) { switch (event->type) { case BLE_GAP_EVENT_ADV_IND: { // 收到广播包,可以连接该设备 ble_gap_connect(&event->gap_event.adv_ind.address); break; } case BLE_GAP_EVENT_CONNECTED: { // 连接成功,可以开始 GATT 操作 break; } case BLE_GAP_EVENT_DISCONNECTED: { // 断开连接,重新开始广播 bluetooth_start_advertising(); break; } default: break; } } static void gatt_event_handler(ble_event_t *event) { switch (event->type) { case BLE_GATT_EVENT_READ: { // 处理读操作 break; } case BLE_GATT_EVENT_WRITE: { // 处理写操作 ble_err_t err = ble_gatt_server_send_indication(event->conn_handle, 0x1234, raw_data, sizeof(raw_data)); // 发送通知给主机 if (err != BLE_ERR_NONE) { // 发送失败,需要处理错误 break; } break; } default: break; } }给出无注释代码

2023-05-25 上传