修复协议解析器, 打通与 MPU 的双向通信

- protocol: 修复 edc_cycle_parse_byte 状态机死代码 (switch 恒 return, 收帧逻辑不可达), 并加 LEN 未就绪保护
- protocol: UART 默认引脚改为 GPIO11/12 (UART0)
- sdkconfig.defaults: 控制台切至 USB Serial/JTAG, 避免与协议 UART0 冲突
- web_ctrl: 新增协议状态 API 与 Cycle 快照; on_cycle 回 DONE 并按 cycle_id 去重
- main: 接入协议初始化与接收任务, 更新接线注释
- 实测: MPU 每秒下发 Cycle, I2C/UART 双通道 12/12 收, DONE 12/12 回
This commit is contained in:
kirl committed 2026-07-29 23:24:31 +08:00
1 parent 40ee976236
commit af4f0edc08
9 files changed
+218 -20

No files matched your search

+2 -1
View File
@@ -177,8 +177,9 @@ uint16_t edc_crc16(const uint8_t *data, size_t len)
- **RX**: 接收 CYCLE_DATA (冗余备份)
- **TX**: 发送 CYCLE_DONE / CYCLE_REQUEST
- 默认引脚: TX=GPIO8, RX=GPIO9
- 默认引脚: TX=GPIO11, RX=GPIO12 (UART0)
- 波特率: 115200, 8N1
- 注: ESP32-C5 日志控制台使用 USB Serial/JTAG, UART0 专用于本协议
### 6.3 冗余策略
+13 -12
View File
@@ -105,13 +105,14 @@ int edc_cycle_pack(const edc_cycle_t *cycle, uint8_t *out)
edc_parse_result_t edc_cycle_parse_byte(edc_frame_ctx_t *ctx, uint8_t byte,
edc_cycle_t *cycle)
{
switch (ctx->state) {
case EDC_PARSE_OK:
/* 上一帧解析完成后, 复位准备接收下一帧 */
if (ctx->state == EDC_PARSE_OK) {
ctx->idx = 0;
ctx->state = EDC_PARSE_WAITING;
/* fall through */
case EDC_PARSE_WAITING:
default:
}
/* ---- 等待 SOF1 ---- */
if (ctx->idx == 0) {
if (byte == EDC_PROTO_SOF1) {
ctx->buf[0] = byte;
ctx->idx = 1;
@@ -119,8 +120,7 @@ edc_parse_result_t edc_cycle_parse_byte(edc_frame_ctx_t *ctx, uint8_t byte,
return EDC_PARSE_WAITING;
}
/* ---- 收帧中 (idx >= 1) ---- */
/* ---- 等待 SOF2 ---- */
if (ctx->idx == 1) {
if (byte != EDC_PROTO_SOF2) {
ctx->idx = 0;
@@ -147,8 +147,8 @@ edc_parse_result_t edc_cycle_parse_byte(edc_frame_ctx_t *ctx, uint8_t byte,
ctx->len = 2 + 1 + 1 + data_len + 2; /* SOF+CMD+LEN+DATA+CRC */
}
/* 收满一帧 */
if (ctx->idx >= ctx->len) {
/* 收满一帧 (idx>=4 保证 ctx->len 已按 LEN 字段设置) */
if (ctx->idx >= 4 && ctx->idx >= ctx->len) {
ctx->idx = ctx->len;
/* CRC 校验 */
@@ -295,8 +295,9 @@ static bool uart_poll_once(void)
{
uint8_t byte;
bool got = false;
int limit = 64; /* 每次最多处理 64 字节, 防止饿死 IDLE 任务 */
while (uart_read_bytes(s_uart_port, &byte, 1, pdMS_TO_TICKS(0)) > 0) {
while (limit-- > 0 && uart_read_bytes(s_uart_port, &byte, 1, pdMS_TO_TICKS(0)) > 0) {
s_stat.uart.bytes_total++;
edc_cycle_t cycle;
@@ -365,7 +366,7 @@ void edc_proto_init(const edc_proto_config_t *cfg)
.sda_io_num = dft.i2c_sda,
.scl_io_num = dft.i2c_scl,
.clk_source = I2C_CLK_SRC_DEFAULT,
.send_buf_depth = 0,
.send_buf_depth = 8, /* 最少需要非零值 */
.receive_buf_depth = EDC_I2C_BUF_SIZE,
.slave_addr = dft.i2c_addr,
.addr_bit_len = I2C_ADDR_BIT_LEN_7,
@@ -485,6 +486,6 @@ void edc_proto_task(void *pvParam)
portEXIT_CRITICAL(&s_lock);
edc_proto_on_cycle(&cycle, ch);
}
vTaskDelay(pdMS_TO_TICKS(2));
vTaskDelay(pdMS_TO_TICKS(10));
}
}
+2 -2
View File
@@ -64,8 +64,8 @@ extern "C" {
/* ---- UART 默认 ---- */
#define EDC_UART_PORT_DEFAULT UART_NUM_0
#define EDC_UART_BAUDRATE 115200
#define EDC_UART_RX_DEFAULT GPIO_NUM_9
#define EDC_UART_TX_DEFAULT GPIO_NUM_8
#define EDC_UART_RX_DEFAULT GPIO_NUM_12
#define EDC_UART_TX_DEFAULT GPIO_NUM_11
#define EDC_UART_BUF_SIZE 512
/* ================================================================