diff --git a/AGENTS.md b/AGENTS.md index 1d67af4..5b77b63 100644 --- a/AGENTS.md +++ b/AGENTS.md @@ -38,6 +38,11 @@ 该实例 HTTPS/SSH 直传大包均不稳,已在本仓库配置 `http.proxy http://127.0.0.1:6789` + `http.sslVerify=false`(仓库级), 代理端口变化时用 `git config http.proxy http://127.0.0.1:<新端口>` 更新。 +- **printf 浮点**:`--specs=nano.specs` 默认裁剪 `%f`(输出空白),已在 + `cmake/project.cmake` 启用 `-u _printf_float`(F405 flash 充裕,ROM +8~10KB 无感)。 +- **在线排查寄存器地址速查**(GD32F4xx):ADC0=0x40012000(RDATA@0x4C)、 + USART0=0x40011000、USART1=0x40004400、GPIOA=0x40020000(OCTL@0x14)、 + RCU:CTL0=0x40023800 / **PLL=0x40023804** / **CFG0=0x40023808**。 ## 开发进度 @@ -48,18 +53,30 @@ (模板已有,COM4 观测)。 - **RS485 数据口驱动**(`rs485.h/rs485.c`):USART0 PA9/PA10 AF7 + DE PA8 GPIO 手动切换;独立接收环形缓冲(预留一格策略 + PRIMASK 临界区);阻塞发送 - (DE→TBE→TC→DE,防尾字节截断);主循环 `rs485_process()` 回显拉通。 + (DE→TBE→TC→DE,防尾字节截断);`rs485_rx_clear()` 清接收缓冲。 - **NTC 测温驱动**(`ntc.h/ntc.c`):PC5 模拟输入 + ADC0 常规通道(软件触发单次 - 转换 + 校准),16 次采样均值 + 分压反解阻值 + B 值公式换算摄氏度。 + 转换 + 校准),16 次采样均值 + 分压反解阻值 + B 值公式换算摄氏度 + (含开路/短路电源轨检测)。 - `debug_uart_write()` 助手(uart.c):debug 口阻塞单字节发送。 +- **命令协议框架移植**(`command.h/command.c`,自 E23x 移植):D5 帧协议 + (D5 BOARD_TYPE LEN [cmd] CRC 累加和校验)+ B5 响应;数据源改为 RS485 收缓冲 + (`rs485_rx_*`),响应经 `rs485_send` 发出(DE 自动切换);帧状态机 + CRC + + 参数解析工具(uint_to_str/parse_uint_dec/parse_timer_parameter/find_parameter_value) + 全套保留;通用命令骨架:**M888 软复位 / M999 版本号 / M9999 进 OTA**; + 业务命令(加热/控温)待定义,switch 中扩展(TODO 注释有格式示例)。 + `command_execute(cmd_str)` 调试入口保留(本地直接 handle,不经接收缓冲)。 + RS485 拉通回显已由命令协议替代(`rs485_process` 移除)。 - 工程改名:`FilmForge_FW`、`BOARD_TYPE_CODE 41`。 ### 计划 / TODO(已提出尚未实现) - [ ] **RS485 拉通上板验证**:COM5(RS485)发数据回显 + COM4(debug)观测, DE 时序实测(逻辑分析仪看 PA8 与 TX 时序)。 -- [ ] **NTC 测温上板验证**:COM4 观测打印温度,与室温计对比;`NTC_B_VALUE` - 确认(B3950 vs B3435——待两点法实测判定)。 +- [ ] **NTC 测温上板验证**:固件/换算已验证正确(ADC 忠实读数),**当前板上 + PC5 实测 3.296V(ADC≈4090 满量程)→ NTC 分压支路未连通**(上电后固件打印 + "NTC OPEN")。排查:万用表量 PC5 对 GND 电压(应约 1.5V);查 NTC 焊点/ + 走线/接地/网络号;断电量焊在板上 NTC 两端阻值(热风焊可能损坏 NTC 致开路)。 + 修好后应读约 1.55V(ADC≈1929)→ 27~29℃。`NTC_B_VALUE` 待两点法确认。 - [ ] **加热驱动**:加热功率器件的驱动与控制(方案待定:MOS/PWM/继电器), 与测温闭环(控温策略)。 - [ ] **正式数据协议**:RS485 从回显切换到正式协议(沿用 D5/B5 帧体系或新定义), diff --git a/CMakeLists.txt b/CMakeLists.txt index 154f7ab..e3bcfc1 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -38,9 +38,9 @@ set(TARGET_SRC Src/uart_ring_buffer.c Src/rs485.c Src/ntc.c + Src/command.c # TODO: 待从 GD32E23x 工程移植后启用 - # Src/command.c # Src/i2c.c ) diff --git a/Inc/board_config.h b/Inc/board_config.h index 896f700..d4f2a95 100644 --- a/Inc/board_config.h +++ b/Inc/board_config.h @@ -28,7 +28,7 @@ /* >>>>>>>>>>>>>>>>>>>>[COMMAND DEBUG]<<<<<<<<<<<<<<<<<<<< */ #define COM_DEBUG CFG_DISABLE // CFG_DISABLE: Command debug off; CFG_ENABLE: Command debug on -/* TODO: 串口命令协议模块(command.c)尚未移植(见 AGENTS.md 进度清单) */ +/* 命令协议框架已移植(Src/command.c,数据口 RS485),业务命令待定义 */ /* >>>>>>>>>>>>>>>>>>>>[DEBUG ASSERTIONS DEFINE]<<<<<<<<<<<<<<<<<<<< */ diff --git a/Inc/command.h b/Inc/command.h new file mode 100644 index 0000000..b42d8c9 --- /dev/null +++ b/Inc/command.h @@ -0,0 +1,84 @@ +/** + * @file command.h + * @brief 串口命令解析与处理模块接口声明(FilmForge:数据口走 RS485)。 + * @details 提供基于环形缓冲区的串口协议解析、命令处理及状态管理功能, + * 支持格式为 D5 BOARD_TYPE LEN [cmd] CRC 的命令帧解析与响应。 + */ +#ifndef COMMAND_H +#define COMMAND_H + +#include +#include + +/** + * @defgroup Command 命令处理模块 + * @brief 串口命令解析与处理 + * @{ + */ + +/** + * @section Command_Protocol 协议格式 + * 接收命令帧格式(经 RS485 数据口): + * @code + * [0] HEADER = 0xD5 // 包头标识 + * [1] BOARD_TYPE // 板卡类型标识 + * [2] LEN = 数据区字节数 // 有效载荷长度 + * [3..(3+LEN-1)] 数据 // 命令数据,如 "M999" + * [last] CRC // 校验码(从索引1累加到len-2的低8位) + * @endcode + * + * 响应帧格式: + * @code + * [0] HEADER = 0xB5 // 响应包头 + * [1] TYPE // 响应类型(0xF0=成功,0xF1..=错误类型) + * [2] LEN // 响应数据长度 + * [3..(3+LEN-1)] 数据 // 响应数据 + * [last] CRC // 校验码 + * @endcode + * + * @section Command_Usage 使用说明 + * 1) 初始化 RS485 数据口: + * @code{.c} + * rs485_init(); + * @endcode + * + * 2) 在主循环中调用命令处理: + * @code{.c} + * while(1) { + * command_process(); // 处理 RS485 接收到的命令 + * // 其他业务逻辑 + * } + * @endcode + */ + +/** + * @brief 处理 RS485 接收环形缓冲区中的命令数据。 + * @details 基于状态机的非阻塞协议解析器,处理完整的命令帧并自动响应。 + * 每次调用处理缓冲区中所有可用数据,遇到错误时自动重置状态机。 + * @note 使用静态变量维护解析状态,函数不可重入。 + * @warning 依赖 RS485 接收环形缓冲区正确实现,建议在主循环中周期调用。 + * @ingroup Command + */ +void command_process(void); + +/** + * @brief 解析并处理完整的命令帧。 + * @param frame 指向完整命令帧的缓冲区(从包头0xD5开始)。 + * @param len 命令帧总长度(字节)。 + * @note 内部函数,由 command_process() 调用,一般不直接使用。 + * @ingroup Command + */ +void handle_command(const uint8_t *frame, uint8_t len); + +/** + * @brief 执行命令(简化版,调试用)。 + * @details 根据命令字符串直接构造协议帧并执行,无需手动构造协议帧 + * @param cmd_str 命令字符串(如"M999"、"M888") + * @note 简化的测试函数,自动处理协议帧构造、CRC计算和命令执行 + * @ingroup Command + */ +void command_execute(const char *cmd_str); + +/** @} */ // end of Command group + +#endif // COMMAND_H diff --git a/Inc/rs485.h b/Inc/rs485.h index f1bc398..346d680 100644 --- a/Inc/rs485.h +++ b/Inc/rs485.h @@ -23,13 +23,6 @@ void rs485_init(void); */ void rs485_send(const uint8_t *data, uint16_t len); -/** - * @brief RS485 主循环处理(拉通阶段:原样回显)。 - * @details 从接收环形缓冲取字节并回发到 RS485,同时转发到 debug 串口。 - * 在主循环中周期调用。 - */ -void rs485_process(void); - /** * @brief 获取当前 RS485 接收环形缓冲区可读字节数。 */ @@ -41,6 +34,12 @@ uint8_t rs485_rx_available(void); */ int rs485_rx_get(void); +/** + * @brief 清空 RS485 接收环形缓冲区(丢弃已接收数据)。 + * @note 内部关中断保护,与 ISR 无竞争。 + */ +void rs485_rx_clear(void); + /** * @brief USART0 接收中断处理(由 gd32f4xx_it.c 的 USART0_IRQHandler 调用)。 */ diff --git a/Src/command.c b/Src/command.c new file mode 100644 index 0000000..c8c0737 --- /dev/null +++ b/Src/command.c @@ -0,0 +1,573 @@ +/** + * @file command.c + * @brief 串口命令解析与处理模块实现(FilmForge:数据口走 RS485)。 + * @details 实现基于状态机的协议解析器,支持 D5 BOARD_TYPE LEN [cmd] CRC 格式的 + * 命令处理,包含命令帧解析、响应生成功能。 + * 自 GD32E23x 工程移植:数据源改为 RS485 接收环形缓冲区, + * 响应经 rs485_send() 发出(DE 收发自动切换)。 + * @author Hulk + * @date 2026-10-09 + * @version 1.1.0(FilmForge 移植版) + * @ingroup Command + */ + +#include "command.h" +#include "rs485.h" +#include "uart.h" +#include "systick.h" +#include "board_config.h" +#include +#include +#include +#include + +/* ============================================================================ + * 协议常量定义 + * ============================================================================ */ + +/** @name 协议帧标识符 + * @{ */ +#define PROTOCOL_PACKAGE_HEADER 0xD5 /**< 命令帧包头标识 */ +/** @} */ + +/** @name 命令长度限制 + * @{ */ +#define COMMAND_MIN_LEN 2 /**< 最小命令长度,如"M1" */ +#define PROTOCOL_MIN_FRAME_LEN (3 + COMMAND_MIN_LEN + 1) /**< 最小完整帧长度:header+type+len+payload+crc = 6 */ +#define PROTOCOL_MAX_FRAME_LEN 32 /**< 最大完整帧长度 */ +/** @} */ + +/** @name 响应帧标识符 + * @{ */ +#define RESP_HEADER 0xB5 /**< 响应帧包头标识 >**/ +#define RESP_TYPE_OK 0xF0 /**< 成功响应类型 >**/ +#define RESP_TYPE_CRC_ERR 0xF1 /**< CRC校验错误 >**/ +#define RESP_TYPE_HEADER_ERR 0xF2 /**< 包头错误 >**/ +#define RESP_TYPE_TYPE_ERR 0xF3 /**< 板类型错误 >**/ +#define RESP_TYPE_LEN_ERR 0xF4 /**< 长度错误 >**/ +#define RESP_TYPE_PARAM_ERR 0xFD /**< 参数错误 >**/ +#define RESP_TYPE_CMD_ERR 0xFE /**< 命令错误 >**/ +#define RESP_TYPE_ERR 0xFF /**< 通用错误 >**/ +/** @} */ + +/* ============================================================================ + * 模块内部变量 + * ============================================================================ */ + +/** @name 预设响应数据 + * @{ */ +static const uint8_t s_report_status_ok[] = { 'o', 'k' }; /**< 成功响应数据 */ +static const uint8_t s_report_status_err[] = { 'e','r','r' }; /**< 错误响应数据 */ +/** @} */ + +void system_software_reset(void) +{ + // 确保所有待处理的内存访问完成 + __DSB(); + + // 执行系统复位 + NVIC_SystemReset(); + + // 以下代码不会执行 + while(1); +} + +/* Debug output control */ +#if COM_DEBUG == CFG_ENABLE + #define COMMAND_DEBUG(fmt, ...) printf("[COMMAND] " fmt "\r\n", ##__VA_ARGS__) +#else + #define COMMAND_DEBUG(fmt, ...) +#endif + +/* ============================================================================ + * 公共接口函数 + * ============================================================================ */ + +/** + * @brief 计算协议包的 8 位累加校验值(Checksum)。 + * @details 对输入缓冲区逐字节累加并取低 8 位,累加范围为 data[1] 至 data[len-2], + * 即不包含包头 HEADER(索引 0)与尾部 CRC 字节(索引 len-1)。 + * 当 len 小于最小协议帧长度(PROTOCOL_MIN_FRAME_LEN)时返回 0。 + * @param data 指向待校验的完整协议包缓冲区。 + * @param len 缓冲区总长度(字节),应满足 header + type + len + payload + crc 的最小格式。 + * @return uint8_t 计算得到的 8 位校验值。 + * @note 本函数实现为简单求和校验(Checksum),非多项式 CRC;与本协议“从索引 1 累加到 len-2”的规则一致。 + * @ingroup Command + */ +static uint8_t command_sum_crc_calc(const uint8_t *data, uint8_t len) +{ + uint16_t crc = 0; + // 仅在满足协议最小帧长时计算(header + type + len + payload + crc) + if (len < PROTOCOL_MIN_FRAME_LEN) return 0; + + // 累加从索引 1 到 len-2 的字节(不含 header 和 crc 字节) + for (uint8_t i = 1; i < (len - 1); i++) + { + crc += data[i]; + } + return (uint8_t)(crc & 0xFF); +} + +/** + * @brief 发送协议响应帧(经 RS485 数据口,DE 收发自动切换)。 + * @details 构造并发送格式为 B5 TYPE LEN [payload] CRC 的响应帧, + * 自动计算CRC校验值并通过 RS485 发出。 + * @param type 响应类型码(如 RESP_TYPE_OK, RESP_TYPE_CRC_ERR 等)。 + * @param payload 指向响应数据的缓冲区,当len为0时可为NULL。 + * @param len 响应数据长度(字节),为0时不复制payload数据。 + * @note 内部使用固定大小缓冲区,超长响应将被丢弃。 + * @ingroup Command + */ +static void send_response(uint8_t type, const uint8_t *payload, uint8_t len) +{ + uint8_t buf_len = (uint8_t)(3 + len + 1); + uint8_t buf[16]; // 简单场景足够,必要时可增大 + if (buf_len > sizeof(buf)) return; // 防御 + + buf[0] = RESP_HEADER; + buf[1] = type; + buf[2] = len; + + // 简化逻辑:只有当len > 0且payload非空时才复制数据 + if (len > 0 && payload != NULL) { + for (uint8_t i = 0; i < len; i++) { + buf[3 + i] = payload[i]; + } + } + + buf[buf_len - 1] = command_sum_crc_calc(buf, buf_len); + + // 经 RS485 发出(rs485_send 内部处理 DE 切换与发送完成等待) + rs485_send(buf, buf_len); +} + +/** + * @brief 发送协议响应帧(调试用,发送到 DEBUG_UART)。 + * @details 构造并发送格式为 B5 TYPE LEN [payload] CRC 的响应帧, + * 自动计算CRC校验值并通过 DEBUG_UART 输出(Hex 视图观察协议交互用)。 + * @param type 响应类型码(如 RESP_TYPE_OK, RESP_TYPE_CRC_ERR 等)。 + * @param payload 指向响应数据的缓冲区,当len为0时可为NULL。 + * @param len 响应数据长度(字节)。 + * @note 内部使用固定大小缓冲区,超长响应将被丢弃。 + * @ingroup Command + */ +static void send_response_debug(uint8_t type, const uint8_t *payload, uint8_t len) __attribute__((unused)); +static void send_response_debug(uint8_t type, const uint8_t *payload, uint8_t len) +{ + uint8_t buf_len = (uint8_t)(3 + len + 1); + uint8_t buf[16]; // 简单场景足够,必要时可增大 + if (buf_len > sizeof(buf)) return; // 防御 + + buf[0] = RESP_HEADER; + buf[1] = type; + buf[2] = len; + + if (len > 0 && payload != NULL) { + for (uint8_t i = 0; i < len; i++) { + buf[3 + i] = payload[i]; + } + } + + buf[buf_len - 1] = command_sum_crc_calc(buf, buf_len); + + // 经 debug 串口逐字节输出 + for (uint8_t i = 0; i < buf_len; i++) { + debug_uart_write(buf[i]); + } + // 等待发送完成 + while (RESET == usart_flag_get(DEBUG_UART, USART_FLAG_TC)) {} +} + +/** + * @brief 判断字符是否为十进制数字字符。 + * @param c 待检查的字符(ASCII码值)。 + * @return bool 判断结果。 + * @retval true 字符为 '0' 到 '9' 之间的数字字符。 + * @retval false 字符不是十进制数字字符。 + * @ingroup Command + */ +static inline bool is_dec_digit(uint8_t c) { return (c >= '0' && c <= '9'); } + +/** + * @brief 将一个无符号整数转换为字符串并追加到缓冲区。 + * @param value 要转换的数字。 + * @param buffer 指向目标缓冲区的指针,转换后的字符串将写入此处。 + * @return uint8_t 写入的字符数。 + * @ingroup Command + */ +static uint8_t uint_to_str(uint32_t value, char *buffer) { + char temp[10]; // 32位无符号整数最多10位 + int i = 0; + + if (value == 0) { + buffer[0] = '0'; + return 1; + } + + // 将数字逆序转换为字符存入临时数组 + while (value > 0) { + temp[i++] = (char)((value % 10) + '0'); + value /= 10; + } + + // 将逆序的字符串反转并存入目标缓冲区 + uint8_t len = (uint8_t)i; + for (int j = 0; j < len; j++) { + buffer[j] = temp[--i]; + } + + return len; +} + +/** + * @brief 从缓冲区解析十进制无符号整数。 + * @details 从指定位置开始连续读取十进制数字字符,累加构成32位无符号整数。 + * 遇到非数字字符或到达长度限制时停止解析。 + * @param s 指向待解析字符缓冲区的起始位置。 + * @param n 允许解析的最大字符数。 + * @param out 输出参数,存储解析结果,可为NULL。 + * @return uint8_t 实际消耗的字符数。 + * @retval 0 首字符不是数字,解析失败。 + * @retval >0 成功解析的数字字符个数。 + * @note 不处理符号、空白字符或进制前缀。 + * @warning 不进行溢出检查,超出uint32_t范围时按无符号算术溢出处理。 + * @ingroup Command + */ +static uint8_t parse_uint_dec(const uint8_t *s, uint8_t n, uint32_t *out) +{ + uint8_t i = 0; + uint32_t v = 0; + while (i < n && is_dec_digit(s[i])) + { + v = v * 10u + (uint32_t)(s[i] - '0'); + i++; + } + if (i == 0) return 0; // 未读到数字 + if (out) *out = v; // + return i; +} + +/** + * @brief 通用T参数解析函数 + * @details 解析命令中的T参数(定时参数),格式为T<数字> + * @param cmd 指向命令缓冲区 + * @param cmd_index 当前解析位置的指针(会被更新) + * @param cmd_len 命令总长度 + * @param timer_value 输出参数,存储解析到的定时值 + * @return bool 解析结果 + * @retval true 成功解析到T参数 + * @retval false 没有T参数或解析失败 + * @note 如果解析成功,cmd_index会被更新到T参数后的位置 + * @ingroup Command + */ +static bool __attribute__((unused)) parse_timer_parameter(const uint8_t *cmd, uint8_t *cmd_index, uint8_t cmd_len, uint32_t *timer_value) +{ + if (*cmd_index >= cmd_len || cmd[*cmd_index] != 'T') { + return false; // 没有T参数 + } + + uint8_t temp_index = *cmd_index + 1; // T后的位置 + const uint8_t used_timer_cmd = parse_uint_dec(&cmd[temp_index], (uint8_t)(cmd_len - temp_index), timer_value); + + if (used_timer_cmd == 0) { + return false; // T后面没有有效数字 + } + + *cmd_index = (uint8_t)(temp_index + used_timer_cmd); // 更新索引 + return true; +} + +/** + * @brief 检查命令是否完全解析完毕 + * @details 验证命令中是否还有未解析的字符,用于格式验证 + * @param cmd_index 当前解析位置 + * @param cmd_len 命令总长度 + * @return bool 检查结果 + * @retval true 命令完全解析完毕 + * @retval false 还有未解析的字符 + * @ingroup Command + */ +static bool __attribute__((unused)) is_command_fully_parsed(uint8_t cmd_index, uint8_t cmd_len) +{ + return (cmd_index == cmd_len); +} + +/** + * @brief 在命令字符串中查找指定参数的值 + * @param cmd 指向命令起始位置(Mxxx 之后) + * @param cmd_len 命令剩余长度 + * @param key 要查找的参数字符(如 'P', 'S') + * @param value 输出参数,存储解析到的值 + * @return bool 是否找到该参数 + * @ingroup Command + */ +static bool __attribute__((unused)) find_parameter_value(const uint8_t *cmd, uint8_t cmd_len, char key, uint32_t *value) +{ + for (uint8_t i = 0; i < cmd_len; i++) { + if (cmd[i] == (uint8_t)key) { + uint8_t param_idx = i + 1; + if (param_idx >= cmd_len) return false; + if (parse_uint_dec(&cmd[param_idx], cmd_len - param_idx, value) > 0) { + return true; + } + return false; + } + } + return false; +} + +/* ============================================================================ + * 命令处理函数 + * ============================================================================ */ + +/** + * @brief 解析并处理完整的命令帧。 + * @details 处理经过协议校验的完整命令帧,支持以下命令格式: + * - 无参数命令:M<数字>(如 M999、M888) + * - 带参数命令:M<数字>S<参数>(如 M100S123,参数为十进制) + * + * 支持的命令(FilmForge 通用骨架): + * - M888: 软件复位 + * - M999: 输出固件版本号(v<板卡码>.<主>.<次>.<修订>) + * - M9999: 进入 OTA 模式(复位进 Bootloader) + * + * 具体业务命令(加热/测温/控温等)待定,在 switch 中按需扩展。 + * + * @param frame 指向完整命令帧的缓冲区(从包头0xD5开始)。 + * @param len 命令帧总长度(字节)。 + * @note 函数内部进行帧格式校验,格式错误时自动发送错误响应。 + * @warning 假设输入帧已通过基本协议校验(包头、类型、CRC等)。 + * @ingroup Command + */ + +void handle_command(const uint8_t *frame, uint8_t len) { + // 帧格式:D5 BOARD_TYPE LEN [cmd] CRC; cmd 支持变长,如 "M999"、"M100S123" + uint8_t cmd_len = frame[2]; + if (len < PROTOCOL_MIN_FRAME_LEN || (uint8_t)(3 + cmd_len + 1) != len) return; // 长度不匹配或者小于最小限制 + + const uint8_t *cmd = &frame[3]; // 提取命令部分 + + // 命令必须以 'M' 开头 + if (cmd[0] != 'M'){ + send_response(RESP_TYPE_TYPE_ERR, s_report_status_err, sizeof(s_report_status_err)); + return; + } + + // 从 'M' 后开始解析 + uint8_t cmd_index = 1; + // 解析M后的十进制数,即命令本体 + uint32_t base_cmd = 0; + uint8_t used_base_cmd = parse_uint_dec(&cmd[cmd_index], (cmd_len - cmd_index), &base_cmd); + if (used_base_cmd == 0) + { + // 'M' 后没有数字,格式错误 + send_response(RESP_TYPE_LEN_ERR, s_report_status_err, sizeof(s_report_status_err)); + return; + } + + cmd_index = (uint8_t)(cmd_index + used_base_cmd); // 更新索引到命令后 + + switch (base_cmd) { + /* ========================================== + * M888 软件重启命令 + * ========================================== */ + case 888u: + // 先发送确认响应 + send_response(RESP_TYPE_OK, s_report_status_ok, sizeof(s_report_status_ok)); + + // 短暂延时确保响应发送完成 + delay_ms(100); + + // 执行软件重启 + system_software_reset(); + return; + + /* ========================================== + * M999 输出固件版本号命令 + * ========================================== */ + case 999u: //M999: 输出固件版本号 + { + char version_str[16]; + char *p = version_str; + + *p++ = 'v'; + p += uint_to_str(BOARD_TYPE_CODE, p); + *p++ = '.'; + p += uint_to_str(FW_VERSION_MAJOR, p); + *p++ = '.'; + p += uint_to_str(FW_VERSION_MINOR, p); + *p++ = '.'; + p += uint_to_str(FW_VERSION_PATCH, p); + *p = '\0'; // null-terminate for printf safety + + uint8_t n = (uint8_t)(p - version_str); + send_response(RESP_TYPE_OK, (const uint8_t *)version_str, n); + COMMAND_DEBUG("Firmware Version: %s", version_str); + } + return; + + /* ========================================== + * M9999 进入OTA模式 + * ========================================== */ + case 9999u: //M9999: 进入OTA模式 + __disable_irq(); // 关中断,防止竞态条件 + NVIC_SystemReset(); // 触发系统复位,进入Bootloader + return; + + /* ========================================== + * 具体业务命令(加热驱动/控温等) + * TODO: 待定义后在下方按 base_cmd 扩展,格式参考: + * case 100u: // M100S + * if (!is_command_fully_parsed(cmd_index, cmd_len)) { ... 参数解析 } + * find_parameter_value(...); // 按键取参 + * send_response(RESP_TYPE_OK, s_report_status_ok, ...); + * return; + * ========================================== */ + default: + send_response(RESP_TYPE_CMD_ERR, s_report_status_err, sizeof(s_report_status_err)); + break; + } +} + +/** + * @brief 处理 RS485 接收环形缓冲区中的命令数据,解析完整的协议帧。 + * @details 本函数实现一个基于状态机的协议解析器,用于处理格式为 + * D5 BOARD_TYPE LEN [cmd] CRC 的命令帧(数据源:RS485 数据口): + * - 状态1:等待包头字节 PROTOCOL_PACKAGE_HEADER (0xD5) + * - 状态2:接收板卡类型字节 PROTOCOL_BOARD_TYPE + * - 状态3:接收长度字段并计算期望的完整帧长度 + * - 状态4:继续接收剩余数据直到完整帧 + * - 状态5:对完整帧进行校验(包头、板卡类型、CRC)并处理 + * + * 函数采用非阻塞方式处理,每次调用处理缓冲区中所有可用数据。 + * 遇到格式错误、长度异常或校验失败时自动重置状态机。 + * + * @note 本函数使用静态变量维护解析状态,因此不可重入。在中断环境中使用需注意并发安全。 + * 协议帧最大长度受 PROTOCOL_MAX_FRAME_LEN 限制,超出范围的帧将被丢弃。 + * + * @warning 函数依赖 rs485_rx_available() 和 rs485_rx_get() 正确实现, + * 若这些函数有缺陷可能导致死循环或数据丢失。 + * + * @see handle_command() 用于处理校验通过的完整命令帧 + * @see command_sum_crc_calc() 用于计算和校验 CRC 值 + * @see send_response() 用于发送错误响应 + * + * @ingroup Command + */ +void command_process(void) { + static uint8_t cmd_buf[PROTOCOL_MAX_FRAME_LEN]; + static uint8_t cmd_len = 0; + static uint8_t expected_cmd_len = 0; // 0 表示尚未确定总长度 + + while (rs485_rx_available() > 0) { + int byte = rs485_rx_get(); + if (byte < 0) break; + + if (cmd_len == 0) { + if ((uint8_t)byte == PROTOCOL_PACKAGE_HEADER) { + cmd_buf[cmd_len++] = (uint8_t)byte; + expected_cmd_len = 0; // 等待进一步字段以确定长度 + } else { + // 丢弃非起始字节 + } + continue; + } + + if (cmd_len >= PROTOCOL_MAX_FRAME_LEN) { + // 防御:缓冲溢出,复位状态机 + cmd_len = 0; + expected_cmd_len = 0; + continue; + } + + // 缓存后续字节 + cmd_buf[cmd_len++] = (uint8_t)byte; + + // 当到达长度字段(索引 2)后,确定总长度:3 + LEN + 1 + if (cmd_len == 3) { + uint8_t payload_len = cmd_buf[2]; + expected_cmd_len = (uint8_t)(3 + payload_len + 1); + if (expected_cmd_len > PROTOCOL_MAX_FRAME_LEN) { + // 异常:长度超界,复位状态机 + cmd_len = 0; + expected_cmd_len = 0; + } + continue; + } + + if (expected_cmd_len > 0 && cmd_len == expected_cmd_len) { + // 到帧尾,进行各项校验 + bool verification_status = true; + + #if DEBUG_VERBOSE == CFG_ENABLE + if (cmd_buf[0] != PROTOCOL_PACKAGE_HEADER) { + send_response(RESP_TYPE_HEADER_ERR, s_report_status_err, sizeof(s_report_status_err)); + verification_status = false; + } + #endif + + if (verification_status && cmd_buf[1] != PROTOCOL_BOARD_TYPE) { + send_response(RESP_TYPE_TYPE_ERR, s_report_status_err, sizeof(s_report_status_err)); + verification_status = false; + } + if (verification_status) { + uint8_t crc_calc = command_sum_crc_calc(cmd_buf, expected_cmd_len); + uint8_t crc_recv = cmd_buf[expected_cmd_len - 1]; + if (crc_calc != crc_recv) { + send_response(RESP_TYPE_CRC_ERR, s_report_status_err, sizeof(s_report_status_err)); + verification_status = false; + } + } + + if (verification_status) { + handle_command(cmd_buf, expected_cmd_len); + } else { + // 验证失败时清空缓冲区,避免后续帧受影响 + rs485_rx_clear(); + } + + // 复位,等待下一帧 + cmd_len = 0; + expected_cmd_len = 0; + } + } +} + +/** + * @brief 执行命令(简化版) + * @details 根据命令字符串直接构造协议帧并执行,无需手动构造协议帧 + * @param cmd_str 命令字符串(如"M999"、"M888") + * @note 简化的测试函数,自动处理协议帧构造、CRC计算和命令执行。 + * 与 E23x 版不同:构造后直接调用 handle_command()(不经接收缓冲)。 + * @ingroup Command + */ +void command_execute(const char *cmd_str) +{ + if (cmd_str == NULL) return; + + uint8_t cmd_len = (uint8_t)strlen(cmd_str); + uint8_t frame_len = 3 + cmd_len + 1; // header + type + len + cmd + crc + uint8_t frame_buf[32]; // 简单固定缓冲区 + + if (frame_len > sizeof(frame_buf)) return; + + // 构造命令帧 + frame_buf[0] = PROTOCOL_PACKAGE_HEADER; // 0xD5 + frame_buf[1] = PROTOCOL_BOARD_TYPE; // Board Type + frame_buf[2] = cmd_len; // 命令长度 + + // 复制命令数据 + for (uint8_t i = 0; i < cmd_len; i++) { + frame_buf[3 + i] = (uint8_t)cmd_str[i]; + } + + // 计算CRC + uint16_t crc = 0; + for (uint8_t i = 1; i < (frame_len - 1); i++) { + crc += frame_buf[i]; + } + frame_buf[frame_len - 1] = (uint8_t)(crc & 0xFF); + + // 直接交命令处理(本地执行,不经 RS485 接收缓冲) + handle_command(frame_buf, frame_len); +} diff --git a/Src/main.c b/Src/main.c index 38f1ea6..d161247 100644 --- a/Src/main.c +++ b/Src/main.c @@ -11,6 +11,7 @@ #include "uart.h" #include "uart_ring_buffer.h" #include "rs485.h" +#include "command.h" #include "ntc.h" #include "led.h" #include @@ -44,8 +45,8 @@ int main(void) #endif while(1){ - /* RS485 数据口处理(拉通阶段:回显) */ - rs485_process(); + /* RS485 数据口命令处理(D5 帧协议,具体业务命令在 command.c 扩展) */ + command_process(); /* debug 串口周期打印 NTC 温度(约 1s 一次) */ #if DEBUG_MODE == CFG_ENABLE diff --git a/Src/rs485.c b/Src/rs485.c index 98f4767..b9764ac 100644 --- a/Src/rs485.c +++ b/Src/rs485.c @@ -3,7 +3,6 @@ #include "gd32f4xx_rcu.h" #include "gd32f4xx_gpio.h" #include "board_config.h" -#include "uart.h" /* 独立接收环形缓冲区(与 debug 串口的 uart_ring_buffer 隔离), 采用"预留一格"区分空/满,最大可用容量为 RS485_RX_BUFFER_SIZE-1 */ @@ -92,16 +91,11 @@ int rs485_rx_get(void) { return data; } -void rs485_process(void) { - while (rs485_rx_available() > 0) { - int b = rs485_rx_get(); - if (b < 0) { - break; - } - /* 拉通阶段:原样回发 RS485,同时转发 debug 串口便于观察 */ - rs485_send((const uint8_t *)&b, 1); - debug_uart_write((uint8_t)b); - } +void rs485_rx_clear(void) { + uint32_t primask = rs485_irq_save(); + rs485_write_index = 0; + rs485_read_index = 0; + rs485_irq_restore(primask); } void rs485_irq_handler(void) {