diff --git a/Src/command.c b/Src/command.c index 51adcf1..44d0b36 100644 --- a/Src/command.c +++ b/Src/command.c @@ -28,6 +28,8 @@ #define PROTOCOL_BOARD_TYPE 0x03 #define PROTOCOL_PACKAGE_LENGTH 0x02 +#define PACKAGE_MIN_LENGTH 6 + /* 可选的应答类型定义(与示例保持一致,可按需扩展) */ #define RESP_HEADER 0xB5 #define RESP_TYPE_OK 0xF0 @@ -42,7 +44,10 @@ static uint8_t proto_sum_crc(const uint8_t *data, uint8_t len) { uint8_t crc = 0; - if (len < 3) return 0; // 最少应为 header + X + crc + // 仅在满足协议最小帧长时计算(header + type + len + payload + crc) + if (len < PACKAGE_MIN_LENGTH) return 0; + + // 累加从索引 1 到 len-2 的字节(不含 header 和 crc 字节) for (uint8_t i = 1; i < (uint8_t)(len - 1); i++) { crc += data[i]; } @@ -69,7 +74,8 @@ static void send_response(uint8_t type, const uint8_t *payload, uint8_t len) void handle_command(const uint8_t *cmd, uint8_t len) { // 期望帧:D5 03 02 'M' '1' CRC(或 'M2'/'M3') - if (len < 6) return; // 最小长度 3+2+1 + if (len < PACKAGE_MIN_LENGTH) return; // 最小长度 3+2+1 + uint8_t payload_len = cmd[2]; if (payload_len < 2) return; // 我们期望 2 字节命令 @@ -102,8 +108,8 @@ void command_process(void) { static uint8_t cmd_len = 0; static uint8_t expected_total = 0; // 0 表示尚未确定总长度 - while (uart_rx_available() > 0) { - int byte = uart_rx_get(); + while (uart_ring_buffer_available() > 0) { + int byte = uart_ring_buffer_get(); if (byte < 0) break; if (cmd_len == 0) { diff --git a/Src/gd32e23x_it.c b/Src/gd32e23x_it.c index 01293ab..d3a29d1 100644 --- a/Src/gd32e23x_it.c +++ b/Src/gd32e23x_it.c @@ -101,14 +101,6 @@ void SysTick_Handler(void) { void USART0_IRQHandler(void) { if (RESET != usart_interrupt_flag_get(USART0, USART_INT_FLAG_RBNE)) { uint8_t data = usart_data_receive(USART0); - uart_rx_put(data); + (void)uart_ring_buffer_put(data); // 缓冲满时丢弃,返回值可用于统计 } - /* 处理 USART0 中断 - ** USART_INT_FLAG_RBNE 读缓冲区非空中断标志 - ** 即:当 USART0 的接收缓冲区有数据时,触发该中断 - ** 该中断为只读中断,不需要手动清除 - ** - ** - ** - */ } \ No newline at end of file diff --git a/Src/main.c b/Src/main.c index ef560be..e7b61de 100644 --- a/Src/main.c +++ b/Src/main.c @@ -36,6 +36,7 @@ OF SUCH DAMAGE. #include "systick.h" #include "uart.h" #include "led.h" +#include "command.h" #include /*!