From 5e6077884b6e410b7eb6eeea873d6168830ef81e Mon Sep 17 00:00:00 2001 From: yelvlab Date: Wed, 13 Aug 2025 00:39:30 +0800 Subject: [PATCH] fix command code --- Inc/board_config.h | 13 ++- Inc/command.h | 88 ++++++++++++++ Src/command.c | 277 ++++++++++++++++++++++++++++----------------- Src/led.c | 2 +- Src/main.c | 5 - 5 files changed, 274 insertions(+), 111 deletions(-) diff --git a/Inc/board_config.h b/Inc/board_config.h index 649faab..ca1aa67 100644 --- a/Inc/board_config.h +++ b/Inc/board_config.h @@ -3,13 +3,18 @@ /* >>>>>>>>>>>>>>>>>>>>[RS485 PHY DEFINE]<<<<<<<<<<<<<<<<<<<< */ -// #define RS485_MAX13487 //RS485 PHY : MAX13487 (AutoDir) -#undef RS485_MAX13487 //RS485 PHY : SP3487 (no AutoDir) +// #define RS485_MAX13487 // RS485 PHY : MAX13487 (AutoDir) +#undef RS485_MAX13487 // RS485 PHY : SP3487 (no AutoDir) /* >>>>>>>>>>>>>>>>>>>>[IIC TYPE DEFINE]<<<<<<<<<<<<<<<<<<<< */ -// #define SOFTWARE_IIC //IIC Type : Software IIC -#undef SOFTWARE_IIC //IIC Type : Hardware IIC +// #define SOFTWARE_IIC // IIC Type : Software IIC +#undef SOFTWARE_IIC // IIC Type : Hardware IIC + +/* >>>>>>>>>>>>>>>>>>>>[DEBUG ASSERTIONS DEFINE]<<<<<<<<<<<<<<<<<<<< */ + +// #define DEBUG_VERBOSE // Debug Assertions Status : Debug Verbose Information +#undef DEBUG_VERBOSE // Debug Assertions Status : No Debug Verbose Information /******************************************************************************/ diff --git a/Inc/command.h b/Inc/command.h index 10fa122..5fe2650 100644 --- a/Inc/command.h +++ b/Inc/command.h @@ -1,13 +1,101 @@ +/** + * @file command.h + * @brief 串口命令解析与处理模块接口声明。 + * @details 提供基于环形缓冲区的串口协议解析、命令处理及状态管理功能, + * 支持格式为 D5 03 LEN [cmd] CRC 的命令帧解析与响应。 + */ #ifndef COMMAND_H #define COMMAND_H #include #include +/** + * @defgroup Command 命令处理模块 + * @brief 串口命令解析与处理 + * @{ + */ + +/** + * @section Command_Protocol 协议格式 + * 接收命令帧格式: + * @code + * [0] HEADER = 0xD5 // 包头标识 + * [1] BOARD_TYPE = 0x03 // 板卡类型标识 + * [2] LEN = 数据区字节数 // 有效载荷长度 + * [3..(3+LEN-1)] 数据 // 命令数据 + * [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) 初始化环形缓冲区: + * @code{.c} + * uart_ring_buffer_init(); + * @endcode + * + * 2) 在主循环中调用命令处理: + * @code{.c} + * while(1) { + * command_process(); // 处理接收到的命令 + * // 其他业务逻辑 + * } + * @endcode + * + * 3) 查询传感器上报状态: + * @code{.c} + * if(get_sensor_report_enabled()) { + * // 执行传感器数据上报 + * } + * @endcode + */ + +/** + * @brief 获取传感器周期上报使能状态。 + * @return bool 上报状态。 + * @retval true 传感器周期上报已启用。 + * @retval false 传感器周期上报已禁用。 + * @ingroup Command + */ bool get_sensor_report_enabled(void); + +/** + * @brief 设置传感器周期上报使能状态。 + * @param enabled 上报使能标志。 + * @arg true 启用传感器周期上报。 + * @arg false 禁用传感器周期上报。 + * @note 推荐通过此函数修改状态,便于后续功能扩展。 + * @ingroup Command + */ void set_sensor_report_enabled(bool enabled); +/** + * @brief 处理串口环形缓冲区中的命令数据。 + * @details 基于状态机的非阻塞协议解析器,处理完整的命令帧并自动响应。 + * 每次调用处理缓冲区中所有可用数据,遇到错误时自动重置状态机。 + * @note 使用静态变量维护解析状态,函数不可重入。 + * @warning 依赖环形缓冲区正确实现,建议在主循环中周期调用。 + * @ingroup Command + */ void command_process(void); + +/** + * @brief 解析并处理完整的命令帧。 + * @param cmd 指向完整命令帧的缓冲区(从包头0xD5开始)。 + * @param len 命令帧总长度(字节)。 + * @note 内部函数,由 command_process() 调用,一般不直接使用。 + * @ingroup Command + */ void handle_command(const uint8_t *cmd, uint8_t len); +/** @} */ // end of Command group + #endif // COMMAND_H diff --git a/Src/command.c b/Src/command.c index dfa9ea7..0691a3e 100644 --- a/Src/command.c +++ b/Src/command.c @@ -1,56 +1,92 @@ +/** + * @file command.c + * @brief 串口命令解析与处理模块实现 + * @details 实现基于状态机的协议解析器,支持 D5 03 LEN [cmd] CRC 格式的命令处理, + * 包含命令帧解析、响应生成和传感器状态管理功能。 + * @author Hulk + * @date 2025-08-13 + * @version 1.0.0 + * @ingroup Command + */ + #include "command.h" #include "uart_ring_buffer.h" #include "led.h" #include #include #include +#include "board_config.h" -/* - 协议说明: - Host -> Device 帧格式: - [0] HEADER = 0xD5 - [1] BOARD_TYPE = 0x03 - [2] LEN = 数据区字节数 - [3..(3+LEN-1)] 数据 - [last] CRC = 从下标 1 到 (last-1) 的累加和低 8 位 +/* ============================================================================ + * 协议格式说明 + * ============================================================================ */ - 最小协议包长度为 6 字节 +/** + * @name 协议帧格式 + * @{ + * @details + * Host -> Device 命令帧格式: + * [0] HEADER = 0xD5 // 包头标识 + * [1] BOARD_TYPE = 0x03 // 板卡类型标识 + * [2] LEN = 数据区字节数 // 有效载荷长度 + * [3..(3+LEN-1)] 数据 // 命令数据,如 "M1", "M2S123" + * [last] CRC = 校验码 // 从索引1到(last-1)的累加和低8位 + * + * 最小协议包长度为 6 字节 + * 数据示例(两字节命令):"M1" / "M2" / "M3" + * + * Device -> Host 响应帧格式: + * [0] 0xB5 // 响应包头 + * [1] TYPE // 响应类型(0xF0=成功,0xF1..=错误类型) + * [2] LEN // 响应数据长度 + * [3..(3+LEN-1)] 数据 // 响应数据,如 "ok", "err" + * [last] CRC // 校验码(同命令帧规则) + * @} + */ - 数据示例(两字节命令):"M1" / "M2" / "M3" +/* ============================================================================ + * 协议常量定义 + * ============================================================================ */ - Device -> Host 应答:复用 0xB5 开头: - [0] 0xB5, [1] TYPE(例如 0xF0=OK, 0xF1..=错误类), [2] LEN, [3..] payload, [last] CRC(同上规则) -*/ +/** @name 协议帧标识符 + * @{ */ +#define PROTOCOL_PACKAGE_HEADER 0xD5 /**< 命令帧包头标识 */ +#define PROTOCOL_BOARD_TYPE 0x03 /**< 板卡类型标识 */ +/** @} */ -// 旧工程中的外部状态与复位函数(本工程暂不直接使用,按要求保留为注释): -// void fwdgt_reset_mcu(void); // 看门狗复位 - -#define PROTOCOL_PACKAGE_HEADER 0xD5 -#define PROTOCOL_BOARD_TYPE 0x03 -#define PROTOCOL_PACKAGE_LENGTH 0x02 - -#define COMMAND_MIN_LEN 2 // 最小命令长度,如M1 M2命令 - -// 最小/最大整帧长度:header(1)+type(1)+len(1)+payload(len>=2)+crc(1) = 3 + LEN + 1 -#define PROTOCOL_MIN_FRAME_LEN (3 + COMMAND_MIN_LEN + 1) -#define PROTOCOL_MAX_FRAME_LEN 32 - -/* 可选的应答类型定义(与示例保持一致,可按需扩展) */ -#define RESP_HEADER 0xB5 -#define RESP_TYPE_OK 0xF0 -#define RESP_TYPE_CRC_ERR 0xF1 -#define RESP_TYPE_HEADER_ERR 0xF2 -#define RESP_TYPE_TYPE_ERR 0xF3 -#define RESP_TYPE_LEN_ERR 0xF4 +/** @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 16 /**< 最大完整帧长度 */ +/** @} */ +/** @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 /**< 长度错误 */ +/** @} */ +/* ============================================================================ + * 模块内部变量 + * ============================================================================ */ +/** @brief 传感器周期上报使能标志 */ static volatile bool s_sensor_report_enabled = false; -static volatile uint32_t s_cmd_param = 0; // 保存形如 M123S 的附加参数(十进制),完整保留为 uint32_t -// 统一的应答负载常量 -static const uint8_t s_report_status_ok[] = { 'o', 'k' }; -static const uint8_t s_report_status_err[] = { 'e','r','r' }; +/** @name 预设响应数据 + * @{ */ +static const uint8_t s_report_status_ok[] = { 'o', 'k' }; /**< 成功响应数据 */ +static const uint8_t s_report_status_err[] = { 'e','r','r' }; /**< 错误响应数据 */ +/** @} */ + +/* ============================================================================ + * 公共接口函数 + * ============================================================================ */ /** * @brief 查询是否启用周期性传感器上报。 @@ -99,7 +135,17 @@ static uint8_t command_sum_crc_calc(const uint8_t *data, uint8_t len) return (uint8_t)(crc & 0xFF); } -/* 发送应答:header(0xB5), type, len, payload[len], crc */ +/** + * @brief 发送协议响应帧。 + * @details 构造并发送格式为 B5 TYPE LEN [payload] CRC 的响应帧, + * 自动计算CRC校验值并通过串口输出。 + * @param type 响应类型码(如 RESP_TYPE_OK, RESP_TYPE_CRC_ERR 等)。 + * @param payload 指向响应数据的缓冲区,可为 NULL。 + * @param len 响应数据长度(字节),payload为NULL时填充0。 + * @note 内部使用固定大小缓冲区,超长响应将被丢弃。 + * @warning 通过 printf 发送,确保串口已正确初始化。 + * @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); @@ -115,26 +161,33 @@ static void send_response(uint8_t type, const uint8_t *payload, uint8_t len) for (uint8_t i = 0; i < buf_len; i++) { printf("%c", buf[i]); } + + // 刷新缓冲区 + fflush(stdout); } -/* 简单数字工具 */ /** * @brief 判断字符是否为十进制数字字符。 - * @param c 输入字符(ASCII,范围通常为 0..255)。 - * @return true 当且仅当 c 在 '0'..'9' 区间内;否则返回 false。 + * @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 从给定缓冲区前缀解析十进制无符号整数。 - * @details 自 s[0] 起连续读取十进制数字字符 '0'..'9',累加成无符号 32 位整数, - * 最多读取 n 个字节;一旦遇到非数字或用尽 n 则停止。 - * @param s 输入字符缓冲区起始(不保证以 '\0' 结束)。 - * @param n 可供解析的最大字节数(从 s[0] 起)。 - * @param out 若非 NULL,输出解析出的数值(当返回 0 时 out 不被更新)。 - * @return uint8_t 实际消耗的数字字符个数;若首字符不是数字则返回 0。 - * @note 本函数不处理空白、正负号及前缀;不做溢出检测,超过 uint32_t 的情形按无符号溢出语义累加。 + * @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) @@ -151,15 +204,25 @@ static uint8_t parse_uint_dec(const uint8_t *s, uint8_t n, uint32_t *out) return i; } +/* ============================================================================ + * 命令处理函数 + * ============================================================================ */ + /** - * @brief 解析并处理一条完整的命令帧。 - * @details 帧格式:D5 03 LEN [cmd] CRC,LEN 为命令负载字节数,[cmd] 以 'M' 开头: - * - 无参:M(例如 M1、M10、M201、M100) - * - 带参:MS(param 为十进制,uint32_t,例如 M1S123、M22S456) - * 本函数首先校验长度一致性(3+LEN+1==len)与最小帧长,然后按上述规则解析 [cmd], - * 调用具体动作(如 led_on/off、状态开关),并通过 send_response 回应。 - * @param frame 指向完整帧的缓冲区(从 HEADER=0xD5 起始)。 - * @param len 完整帧总长度(字节)。 + * @brief 解析并处理完整的命令帧。 + * @details 处理经过协议校验的完整命令帧,支持以下命令格式: + * - 无参数命令:M<数字>(如 M1、M2、M10、M201) + * - 带参数命令:M<数字>S<参数>(如 M100S123,参数为十进制) + * + * 支持的命令: + * - M1: 开启LED,启用传感器上报 + * - M2: 关闭LED,禁用传感器上报 + * - M100S: 设置PWM值(示例) + * + * @param frame 指向完整命令帧的缓冲区(从包头0xD5开始)。 + * @param len 命令帧总长度(字节)。 + * @note 函数内部进行帧格式校验,格式错误时自动发送错误响应。 + * @warning 假设输入帧已通过基本协议校验(包头、类型、CRC等)。 * @ingroup Command */ void handle_command(const uint8_t *frame, uint8_t len) { @@ -198,12 +261,16 @@ void handle_command(const uint8_t *frame, uint8_t len) { set_sensor_report_enabled(true); led_on(); send_response(RESP_TYPE_OK, s_report_status_ok, sizeof(s_report_status_ok)); + uint8_t test_response1[] = { 0xAA, 0xBB, 0xCC, 0xDD }; + send_response(RESP_TYPE_OK, test_response1, sizeof(test_response1)); return; case 2u: // M2命令 led_off(); set_sensor_report_enabled(false); led_off(); send_response(RESP_TYPE_OK, s_report_status_ok, sizeof(s_report_status_ok)); + uint8_t test_response2[] = { 0xDD, 0xCC, 0xBB, 0xAA }; + send_response(RESP_TYPE_OK, test_response2, sizeof(test_response2)); return; // 示例:M3、M10、M201、M100 等(按需添加) @@ -253,10 +320,34 @@ void handle_command(const uint8_t *frame, uint8_t len) { } } +/** + * @brief 处理串口环形缓冲区中的命令数据,解析完整的协议帧。 + * @details 本函数实现一个基于状态机的协议解析器,用于处理格式为 D5 03 LEN [cmd] CRC 的命令帧: + * - 状态1:等待包头字节 PROTOCOL_PACKAGE_HEADER (0xD5) + * - 状态2:接收板卡类型字节 PROTOCOL_BOARD_TYPE (0x03) + * - 状态3:接收长度字段并计算期望的完整帧长度 + * - 状态4:继续接收剩余数据直到完整帧 + * - 状态5:对完整帧进行校验(包头、板卡类型、CRC)并处理 + * + * 函数采用非阻塞方式处理,每次调用处理缓冲区中所有可用数据。 + * 遇到格式错误、长度异常或校验失败时自动重置状态机。 + * + * @note 本函数使用静态变量维护解析状态,因此不可重入。在中断环境中使用需注意并发安全。 + * 协议帧最大长度受 PROTOCOL_MAX_FRAME_LEN 限制,超出范围的帧将被丢弃。 + * + * @warning 函数依赖 uart_ring_buffer_available() 和 uart_ring_buffer_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_total = 0; // 0 表示尚未确定总长度 + static uint8_t expected_cmd_len = 0; // 0 表示尚未确定总长度 while (uart_ring_buffer_available() > 0) { int byte = uart_ring_buffer_get(); @@ -265,81 +356,65 @@ void command_process(void) { if (cmd_len == 0) { if ((uint8_t)byte == PROTOCOL_PACKAGE_HEADER) { cmd_buf[cmd_len++] = (uint8_t)byte; - expected_total = 0; // 等待进一步字段以确定长度 + expected_cmd_len = 0; // 等待进一步字段以确定长度 } else { // 丢弃非起始字节 } continue; } - // 累积后续字节 + if (cmd_len >= PROTOCOL_MAX_FRAME_LEN) { + // 防御:缓冲溢出,复位状态机 + cmd_len = 0; + expected_cmd_len = 0; + } + + // 缓存后续字节 cmd_buf[cmd_len++] = (uint8_t)byte; // 当到达长度字段(索引 2)后,确定总长度:3 + LEN + 1 if (cmd_len == 3) { uint8_t payload_len = cmd_buf[2]; - expected_total = (uint8_t)(3 + payload_len + 1); - if (expected_total > PROTOCOL_MAX_FRAME_LEN) { + expected_cmd_len = (uint8_t)(3 + payload_len + 1); + if (expected_cmd_len > PROTOCOL_MAX_FRAME_LEN) { // 异常:长度超界,复位状态机 cmd_len = 0; - expected_total = 0; + expected_cmd_len = 0; } continue; } - if (expected_total > 0 && cmd_len == expected_total) { + if (expected_cmd_len > 0 && cmd_len == expected_cmd_len) { // 到帧尾,进行各项校验 - bool ok = true; + bool verification_status = true; + + #ifdef DEBUG_VERBOSE if (cmd_buf[0] != PROTOCOL_PACKAGE_HEADER) { send_response(RESP_TYPE_HEADER_ERR, s_report_status_err, sizeof(s_report_status_err)); - ok = false; + verification_status = false; } - if (ok && cmd_buf[1] != PROTOCOL_BOARD_TYPE) { + #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)); - ok = false; + verification_status = false; } - if (ok) { - uint8_t crc_calc = command_sum_crc_calc(cmd_buf, expected_total); - uint8_t crc_recv = cmd_buf[expected_total - 1]; + 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)); - ok = false; + verification_status = false; } } - if (ok) { - handle_command(cmd_buf, expected_total); + if (verification_status) { + handle_command(cmd_buf, expected_cmd_len); } // 复位,等待下一帧 cmd_len = 0; - expected_total = 0; - } - - if (cmd_len >= PROTOCOL_MAX_FRAME_LEN) { - // 防御:缓冲溢出,复位状态机 - cmd_len = 0; - expected_total = 0; + expected_cmd_len = 0; } } } - -// ----------------------------------------------------------------------------- -// 下列为与传感器相关的报告函数占位,按要求“保留但注释掉”。 -// 原工程中依赖 ldc1612/tmp112a 读数并通过 0xB5 帧打印上报。 -// 若后续集成这些传感器驱动,可解注释并补齐实现。 -// -// static uint8_t package_header[3] = {0xB5, 0xF0, 0x04}; -// static uint8_t package_data[4] = {0}; -// -// void eddy_current_value_report(void) { -// uint32_t eddy_current_value_uint32 = ldc1612_get_raw_channel_result(CHANNEL_0); -// (void)eddy_current_value_uint32; -// // 按原协议组帧并 printf 发送:B5 F0 04 [data3..data0] CRC -// } -// -// void tempture_value_report(void) { -// uint32_t temperature_uint32 = tmp112a_get_raw_channel_result(); -// (void)temperature_uint32; -// // 按原协议组帧并 printf 发送:B5 F0 04 [data3..data0] CRC -// } diff --git a/Src/led.c b/Src/led.c index 7fa8316..b39ec12 100644 --- a/Src/led.c +++ b/Src/led.c @@ -4,7 +4,7 @@ void led_init(void) { rcu_periph_clock_enable(LED_RCU); gpio_mode_set(LED_PORT, GPIO_MODE_OUTPUT, GPIO_PUPD_NONE, LED_PIN); gpio_output_options_set(LED_PORT, GPIO_OTYPE_PP, GPIO_OSPEED_50MHZ, LED_PIN); - gpio_bit_reset(LED_PORT, LED_PIN); + gpio_bit_set(LED_PORT, LED_PIN); } void led_on(void) { diff --git a/Src/main.c b/Src/main.c index bb425a8..cc932ba 100644 --- a/Src/main.c +++ b/Src/main.c @@ -59,11 +59,6 @@ int main(void) printf("Hello USART0!"); led_off(); while(1){ - // led_toggle(); - - // 发送数据到 USART0 - // usart_data_transmit(RS485_PHY, (uint8_t)'H'); - // printf("Hello USART0!"); delay_ms(100); command_process(); }