feat: 添加CDC协议通信模块支持

- 集成nanopb库用于protobuf序列化
- 创建cdc_wrapper_module.c实现帧解析功能
- 实现protocol_module.c处理协议编解码
- 定义device_comm.proto通信协议
- 修改CMakeLists.txt添加protobuf源文件
- 更新配置启用NANOPB支持
- 移除usb_cdc_module中基于行的处理逻辑
This commit is contained in:
2026-04-11 18:21:18 +08:00
parent 39c6a1fe84
commit 227158870a
8 changed files with 478 additions and 40 deletions

View File

@@ -28,7 +28,6 @@ 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_MAX_LINE_SIZE 96
#define USB_CDC_CONTROL_POLL_INTERVAL K_MSEC(100)
#define USB_CDC_EXPECTED_BAUDRATE 115200U
@@ -46,9 +45,6 @@ static bool usb_active;
static bool usb_function_prepared;
static bool dtr_ready;
static bool rx_enabled;
static bool rx_line_overflow;
static uint8_t rx_line_buf[USB_CDC_MAX_LINE_SIZE];
static size_t rx_line_len;
static void submit_usb_cdc_rx_event(const uint8_t *data, size_t len)
{
@@ -72,8 +68,6 @@ static void reset_ring_buffers(void)
ring_buf_init(&rx_ringbuf, sizeof(rx_ring_buffer), rx_ring_buffer);
ring_buf_init(&tx_ringbuf, sizeof(tx_ring_buffer), tx_ring_buffer);
rx_line_len = 0U;
rx_line_overflow = false;
irq_unlock(key);
}
@@ -134,36 +128,6 @@ static void validate_line_coding(void)
#endif
}
static void process_rx_byte(uint8_t byte)
{
if ((byte == '\r') || (byte == '\n')) {
if (rx_line_overflow) {
LOG_WRN("Drop oversized CDC line");
rx_line_overflow = false;
rx_line_len = 0U;
return;
}
if (rx_line_len > 0U) {
submit_usb_cdc_rx_event(rx_line_buf, rx_line_len);
rx_line_len = 0U;
}
return;
}
if (rx_line_overflow) {
return;
}
if (rx_line_len >= sizeof(rx_line_buf)) {
rx_line_overflow = true;
return;
}
rx_line_buf[rx_line_len++] = byte;
}
static void rx_work_handler(struct k_work *work)
{
uint8_t buffer[USB_CDC_RX_CHUNK_SIZE];
@@ -181,9 +145,7 @@ static void rx_work_handler(struct k_work *work)
return;
}
for (uint32_t i = 0; i < len; i++) {
process_rx_byte(buffer[i]);
}
submit_usb_cdc_rx_event(buffer, len);
}
}