feat(ble): 添加BLE NUS模块替换原有的BLE串口功能

- 将ble_serial_module替换为ble_nus_module以使用标准的BLE NUS服务
- 移除不再使用的cdc_wrapper_module和相关事件处理
- 更新协议传输层抽象,支持USB CDC和BLE NUS两种传输方式
- 创建统一的proto_rx_event和proto_tx_event替代专用的串行通信事件
- 添加proto_common.h定义传输类型枚举
- 修改protocol_module接口以支持多传输方式
- 在prj.conf中启用CONFIG_BT_ZEPHYR_NUS配置选项
This commit is contained in:
2026-04-15 10:23:12 +08:00
parent c4b205b8a1
commit bc42a4dd63
25 changed files with 538 additions and 1499 deletions

View File

@@ -16,8 +16,9 @@
#include <zephyr/sys/ring_buffer.h>
#include <zephyr/sys/util.h>
#include "usb_cdc_rx_event.h"
#include "usb_cdc_tx_event.h"
#include "proto_rx_event.h"
#include "proto_tx_event.h"
#include "protocol_module.h"
#include "usb_state_event.h"
LOG_MODULE_REGISTER(MODULE, LOG_LEVEL_INF);
@@ -25,7 +26,9 @@ LOG_MODULE_REGISTER(MODULE, LOG_LEVEL_INF);
#define USB_CDC_RX_RING_BUF_SIZE 256
#define USB_CDC_TX_RING_BUF_SIZE 256
#define USB_CDC_RX_CHUNK_SIZE 32
#define USB_CDC_PROTO_RX_BUF_SIZE 128
#define USB_CDC_CONTROL_POLL_INTERVAL K_MSEC(100)
#define USB_CDC_PROTO_RX_FLUSH_DELAY K_MSEC(3)
#define USB_CDC_EXPECTED_BAUDRATE 115200U
static const struct device *const cdc_dev = DEVICE_DT_GET_ONE(zephyr_cdc_acm_uart);
@@ -36,12 +39,15 @@ static struct ring_buf rx_ringbuf;
static struct ring_buf tx_ringbuf;
static struct k_work rx_work;
static struct k_work_delayable control_work;
static struct k_work_delayable rx_flush_work;
static bool initialized;
static bool running;
static bool usb_active;
static bool usb_function_prepared;
static bool dtr_ready;
static bool rx_enabled;
static uint8_t proto_rx_buf[USB_CDC_PROTO_RX_BUF_SIZE];
static size_t proto_rx_len;
static bool is_usb_owner_snapshot(const struct usb_state_event *event)
{
@@ -66,6 +72,8 @@ static void disable_uart_io(void)
uart_irq_tx_disable(cdc_dev);
rx_enabled = false;
dtr_ready = false;
proto_rx_len = 0U;
k_work_cancel_delayable(&rx_flush_work);
reset_ring_buffers();
}
@@ -133,10 +141,32 @@ static void rx_work_handler(struct k_work *work)
return;
}
(void)submit_usb_cdc_rx_event(buffer, len);
if ((proto_rx_len + len) > sizeof(proto_rx_buf)) {
LOG_WRN("Drop oversized CDC protobuf message len:%u",
(uint32_t)(proto_rx_len + len));
proto_rx_len = 0U;
}
if (len > 0U) {
memcpy(&proto_rx_buf[proto_rx_len], buffer, len);
proto_rx_len += len;
k_work_reschedule(&rx_flush_work, USB_CDC_PROTO_RX_FLUSH_DELAY);
}
}
}
static void rx_flush_work_handler(struct k_work *work)
{
ARG_UNUSED(work);
if (!running || !usb_active || !dtr_ready || (proto_rx_len == 0U)) {
return;
}
(void)submit_proto_rx_event(PROTO_TRANSPORT_USB_CDC, proto_rx_buf, proto_rx_len);
proto_rx_len = 0U;
}
static void control_work_handler(struct k_work *work)
{
uint32_t dtr = 0U;
@@ -250,8 +280,10 @@ static int module_init(void)
reset_ring_buffers();
k_work_init(&rx_work, rx_work_handler);
k_work_init_delayable(&control_work, control_work_handler);
k_work_init_delayable(&rx_flush_work, rx_flush_work_handler);
uart_irq_callback_set(cdc_dev, cdc_interrupt_handler);
usb_function_prepared = false;
proto_rx_len = 0U;
return 0;
}
@@ -276,6 +308,7 @@ static void module_pause(void)
}
k_work_cancel_delayable(&control_work);
k_work_cancel_delayable(&rx_flush_work);
disable_uart_io();
running = false;
}
@@ -304,6 +337,8 @@ static bool handle_usb_state_event(const struct usb_state_event *event)
if (!usb_active) {
k_work_cancel_delayable(&control_work);
k_work_cancel_delayable(&rx_flush_work);
protocol_module_reset_transport_state(PROTO_TRANSPORT_USB_CDC);
disable_uart_io();
} else if (running) {
k_work_reschedule(&control_work, K_NO_WAIT);
@@ -312,11 +347,15 @@ static bool handle_usb_state_event(const struct usb_state_event *event)
return false;
}
static bool handle_usb_cdc_tx_event(const struct usb_cdc_tx_event *event)
static bool handle_proto_tx_event(const struct proto_tx_event *event)
{
uint32_t written;
unsigned int key;
if (event->transport != PROTO_TRANSPORT_USB_CDC) {
return false;
}
if (!running || !usb_active || !dtr_ready) {
return false;
}
@@ -343,8 +382,8 @@ static bool app_event_handler(const struct app_event_header *aeh)
return handle_usb_state_event(cast_usb_state_event(aeh));
}
if (is_usb_cdc_tx_event(aeh)) {
return handle_usb_cdc_tx_event(cast_usb_cdc_tx_event(aeh));
if (is_proto_tx_event(aeh)) {
return handle_proto_tx_event(cast_proto_tx_event(aeh));
}
if (is_module_state_event(aeh)) {
@@ -402,7 +441,7 @@ static bool app_event_handler(const struct app_event_header *aeh)
APP_EVENT_LISTENER(MODULE, app_event_handler);
APP_EVENT_SUBSCRIBE(MODULE, module_state_event);
APP_EVENT_SUBSCRIBE(MODULE, proto_tx_event);
APP_EVENT_SUBSCRIBE(MODULE, usb_state_event);
APP_EVENT_SUBSCRIBE(MODULE, usb_cdc_tx_event);
APP_EVENT_SUBSCRIBE_EARLY(MODULE, power_down_event);
APP_EVENT_SUBSCRIBE(MODULE, wake_up_event);