From 6c56fe8a602eb13f52cc50ff27b9674489d00de5 Mon Sep 17 00:00:00 2001 From: zhongxuanzhen Date: Fri, 8 May 2026 16:14:00 +0800 Subject: [PATCH] =?UTF-8?q?=E8=A7=A3=E5=86=B3=E7=BC=96=E8=AF=91=E9=94=99?= =?UTF-8?q?=E8=AF=AF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Core/Src/debug_log.c | 140 -------------------- Core/Src/main.c | 1 + Core/Src/multi_uart_router.c | 2 +- Core/Src/uart2_print.c | 6 +- Core/Src/uart3_passthrough.c | 4 +- docs/UART3智能数据路由与透传功能开发计划.md | 22 +-- docs/多通信接口统一指令处理系统开发计划.md | 28 ++-- 7 files changed, 32 insertions(+), 171 deletions(-) diff --git a/Core/Src/debug_log.c b/Core/Src/debug_log.c index 31e0ccb..15a4fd0 100644 --- a/Core/Src/debug_log.c +++ b/Core/Src/debug_log.c @@ -112,146 +112,6 @@ void DebugLog_EnableModule(const char *module, bool enable) } } -void DebugLog_Output(log_level_t level, const char *module, const char *fmt, ...) -{ - if (level < g_current_level) { - return; - } - - if (!is_module_enabled(module)) { - return; - } - - char buffer[256]; - int len = 0; - - len += snprintf(buffer + len, sizeof(buffer) - len, "[%s][%s] ", - g_level_str[level], module ? module : "MAIN"); - - if (len < (int)sizeof(buffer) - 1) { - va_list args; - va_start(args, fmt); - len += vsnprintf(buffer + len, sizeof(buffer) - len, fmt, args); - va_end(args); - } - - if (len > 0 && len < (int)sizeof(buffer)) { - buffer[len++] = '\r'; - if (len < (int)sizeof(buffer)) { -/** - ****************************************************************************** - * @file debug_log.c - * @brief 增强型调试日志系统实现 - * @author Application Layer - * @version 1.0 - ****************************************************************************** - * @attention - * 本模块实现增强型调试日志系统 - * 设计依据:多通信接口统一指令处理系统开发计划 第3.4节 - * - * 日志格式:[LEVEL][MODULE] message - * 示例:[INFO][CMD] Command received: RL - ****************************************************************************** - */ - -#include "debug_log.h" -#include "uart2_print.h" -#include -#include -#include -#include "multi_uart_router.h" -#define MAX_MODULES 16 -#define MODULE_NAME_LEN 16 - -typedef struct { - char name[MODULE_NAME_LEN]; - bool enabled; -} module_config_t; - -static log_level_t g_current_level = LOG_LEVEL_DEBUG; -static module_config_t g_modules[MAX_MODULES]; -static bool g_modules_initialized = false; - -static const char *const g_level_str[] = { - [LOG_LEVEL_DEBUG] = "DEBUG", - [LOG_LEVEL_INFO] = "INFO ", - [LOG_LEVEL_WARN] = "WARN ", - [LOG_LEVEL_ERROR] = "ERROR", -}; - -static void init_modules(void) -{ - if (g_modules_initialized) { - return; - } - - for (int i = 0; i < MAX_MODULES; i++) { - g_modules[i].name[0] = '\0'; - g_modules[i].enabled = false; - } - - g_modules_initialized = true; -} - -static bool is_module_enabled(const char *module) -{ - if (module == NULL || module[0] == '\0') { - return true; - } - - init_modules(); - - for (int i = 0; i < MAX_MODULES; i++) { - if (g_modules[i].name[0] != '\0' && - strncmp(g_modules[i].name, module, MODULE_NAME_LEN - 1) == 0) { - return g_modules[i].enabled; - } - } - - return true; -} - -void DebugLog_Init(void) -{ - g_current_level = LOG_LEVEL_DEBUG; - init_modules(); - - UART2_Print_String("[LOG] Debug log system initialized\r\n"); -} - -void DebugLog_SetLevel(log_level_t level) -{ - g_current_level = level; -} - -log_level_t DebugLog_GetLevel(void) -{ - return g_current_level; -} - -void DebugLog_EnableModule(const char *module, bool enable) -{ - if (module == NULL || module[0] == '\0') { - return; - } - - init_modules(); - - for (int i = 0; i < MAX_MODULES; i++) { - if (g_modules[i].name[0] == '\0') { - strncpy(g_modules[i].name, module, MODULE_NAME_LEN - 1); - g_modules[i].name[MODULE_NAME_LEN - 1] = '\0'; - g_modules[i].enabled = enable; - return; - } - - if (strncmp(g_modules[i].name, module, MODULE_NAME_LEN - 1) == 0) { - g_modules[i].enabled = enable; - return; - } - } -} - void DebugLog_Output(log_level_t level, const char *module, const char *fmt, ...) { if (level < g_current_level) { diff --git a/Core/Src/main.c b/Core/Src/main.c index fa18581..9d5ac97 100644 --- a/Core/Src/main.c +++ b/Core/Src/main.c @@ -396,6 +396,7 @@ int main(void) /* USER CODE END WHILE */ } +} /* USER CODE END 3 */ /** * @brief System Clock Configuration diff --git a/Core/Src/multi_uart_router.c b/Core/Src/multi_uart_router.c index 5dff38d..1114952 100644 --- a/Core/Src/multi_uart_router.c +++ b/Core/Src/multi_uart_router.c @@ -681,7 +681,7 @@ bool MultiUART_IsBusy(uint8_t port_id) if (port_id >= PORT_COUNT) return false; // 调试串口特殊处理 - if (port_id == PORT_UART2) { + if (port_id == PORT_DEBUG) { // UART2_Print 使用单独的标志位,这里简单兼容 return false; } diff --git a/Core/Src/uart2_print.c b/Core/Src/uart2_print.c index 22ef2f9..c7763ce 100644 --- a/Core/Src/uart2_print.c +++ b/Core/Src/uart2_print.c @@ -462,7 +462,7 @@ int fputc(int ch, FILE *f) { (void)f; UART2_Print_Send((uint8_t *)&ch, 1); - MultiUART_Send(PORT_UART3, (uint8_t *)&ch, 1); /* 增加:同时发给 UART3 */ + MultiUART_Send(PORT_RS485, (uint8_t *)&ch, 1); /* 增加:同时发给 UART3 */ return ch; } #endif @@ -471,7 +471,7 @@ int fputc(int ch, FILE *f) int __io_putchar(int ch) { UART2_Print_Send((uint8_t *)&ch, 1); - MultiUART_Send(PORT_UART3, (uint8_t *)&ch, 1); /* 增加:同时发给 UART3 */ + MultiUART_Send(PORT_RS485, (uint8_t *)&ch, 1); /* 增加:同时发给 UART3 */ return ch; } @@ -479,7 +479,7 @@ int _write(int file, char *ptr, int len) { (void)file; UART2_Print_Send((uint8_t *)ptr, len); - MultiUART_Send(PORT_UART3, (uint8_t *)ptr, len); /* 增加:同时发给 UART3 */ + MultiUART_Send(PORT_RS485, (uint8_t *)ptr, len); /* 增加:同时发给 UART3 */ return len; } #endif \ No newline at end of file diff --git a/Core/Src/uart3_passthrough.c b/Core/Src/uart3_passthrough.c index 3e45d3f..12729ae 100644 --- a/Core/Src/uart3_passthrough.c +++ b/Core/Src/uart3_passthrough.c @@ -208,7 +208,7 @@ void Passthrough_Task(void) } uint8_t byte = node->data[node->offset++]; - MultiUART_Send(PORT_UART1, &byte, 1); + MultiUART_Send(PORT_433, &byte, 1); ctx->queue.pending_count--; ctx->stats.total_bytes_sent++; @@ -232,7 +232,7 @@ void Passthrough_OnTxComplete(void) */ bool Passthrough_CanSend(void) { - return (MultiUART_GetTxAvailable(PORT_UART1) > 0) && + return (MultiUART_GetTxAvailable(PORT_433) > 0) && (g_passthrough_ctx.queue.pending_count > 0); } diff --git a/docs/UART3智能数据路由与透传功能开发计划.md b/docs/UART3智能数据路由与透传功能开发计划.md index e937ba7..8a9789d 100644 --- a/docs/UART3智能数据路由与透传功能开发计划.md +++ b/docs/UART3智能数据路由与透传功能开发计划.md @@ -138,7 +138,7 @@ void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart) { if (huart->Instance == USART3) { - MultiUART_FeedByte(PORT_UART3, uart3_rx_byte); + MultiUART_FeedByte(PORT_RS485, uart3_rx_byte); HAL_UART_Receive_IT(&huart3, &uart3_rx_byte, 1); } } @@ -149,7 +149,7 @@ void CmdRouter_Task(void) // ... UART1/UART3处理 ... for (port_id_t port_id = 0; port_id < PORT_COUNT; port_id++) { - if (port_id == PORT_UART2) continue; // UART2单独处理 + if (port_id == PORT_DEBUG) continue; // UART2单独处理 uint8_t byte; while (MultiUART_ReadByte(port_id, &byte) > 0) { @@ -380,7 +380,7 @@ void Passthrough_Task(void) if (node->offset < node->length) { // 发送一个字节 uint8_t byte = node->data[node->offset++]; - MultiUART_Send(PORT_UART1, &byte, 1); + MultiUART_Send(PORT_433, &byte, 1); } // 检查节点是否发送完成 @@ -659,7 +659,7 @@ void CmdRouter_Task_UART3_Enhanced(void) // 1. 读取UART3接收缓冲区 uint8_t byte; - while (MultiUART_ReadByte(PORT_UART3, &byte) > 0) { + while (MultiUART_ReadByte(PORT_RS485, &byte) > 0) { // 2. 协议识别 route_result_t route = UART3_Protocol_FeedByte(byte, current_tick); @@ -844,7 +844,7 @@ void Passthrough_Task(void) // 发送一个字节 uint8_t byte = node->data[node->offset++]; - MultiUART_Send(PORT_UART1, &byte, 1); + MultiUART_Send(PORT_433, &byte, 1); ctx->queue.pending_count--; ctx->stats.total_bytes_sent++; @@ -902,7 +902,7 @@ uint16_t Passthrough_PushBuffer(const uint8_t *data, uint16_t length) bool Passthrough_CanSend(void) { // 检查UART1 TX是否忙 - return (MultiUART_GetTxAvailable(PORT_UART1) > 0) && + return (MultiUART_GetTxAvailable(PORT_433) > 0) && (g_passthrough_ctx.queue.pending_count > 0); } ``` @@ -934,8 +934,8 @@ void CmdRouter_Task(void) #else // 原有逻辑:所有数据喂给CmdParser uint8_t byte; - while (MultiUART_ReadByte(PORT_UART3, &byte) > 0) { - CmdParser_SetSourcePort(PORT_UART3); + while (MultiUART_ReadByte(PORT_RS485, &byte) > 0) { + CmdParser_SetSourcePort(PORT_RS485); CmdParser_FeedByte(byte, current_tick); } #endif @@ -955,14 +955,14 @@ static void UART3_SmartRouter_Task(uint32_t current_tick) uint8_t byte; // 读取所有待处理的字节 - while (MultiUART_ReadByte(PORT_UART3, &byte) > 0) { + while (MultiUART_ReadByte(PORT_RS485, &byte) > 0) { // 协议识别 route_result_t route = UART3_Protocol_FeedByte(byte, current_tick); switch (route) { case ROUTE_CMD: // 指令路径 - CmdParser_SetSourcePort(PORT_UART3); + CmdParser_SetSourcePort(PORT_RS485); CmdParser_FeedByte(byte, current_tick); LOG_DEBUG("[UART3] CMD byte: 0x%02X", byte); break; @@ -997,7 +997,7 @@ static void UART3_SmartRouter_Task(uint32_t current_tick) #### 3.3.2 透传引擎发送接口 ```c -// 复用现有的 MultiUART_Send(PORT_UART1, data, len) +// 复用现有的 MultiUART_Send(PORT_433, data, len) // 透传引擎将数据写入UART1的发送缓冲区 // 由MultiUART_TxCpltCallback驱动后续发送 ``` diff --git a/docs/多通信接口统一指令处理系统开发计划.md b/docs/多通信接口统一指令处理系统开发计划.md index df1cd23..8f025ea 100644 --- a/docs/多通信接口统一指令处理系统开发计划.md +++ b/docs/多通信接口统一指令处理系统开发计划.md @@ -285,7 +285,7 @@ void CmdRouter_Init(void); /** * @brief 向指定端口的解析器喂入数据 * @note 由UART中断回调调用,线程安全 - * @param port_id: 端口ID (PORT_UART1/PORT_UART2/PORT_UART3) + * @param port_id: 端口ID (PORT_433/PORT_DEBUG/PORT_RS485) * @param byte: 接收到的字节 * @param current_tick: 系统时间戳 * @retval 无 @@ -325,9 +325,9 @@ void CmdRouter_SendResponseFmt(port_id_t port_id, const char *fmt, ...); ```c /** 端口ID枚举 */ typedef enum { - PORT_UART1 = 0, /**< RF433模块 */ - PORT_UART2 = 1, /**< 调试串口 */ - PORT_UART3 = 2, /**< RS485模块 */ + PORT_433 = 0, /**< RF433模块 */ + PORT_DEBUG = 1, /**< 调试串口 */ + PORT_RS485 = 2, /**< RS485模块 */ PORT_COUNT } port_id_t; @@ -396,18 +396,18 @@ void CmdParser_SetResponseCallback(response_callback_t callback); * @note 静态表,根据port_id索引查找对应UART句柄 */ static UART_HandleTypeDef* const g_port_uart_map[PORT_COUNT] = { - [PORT_UART1] = &huart1, // RF433 - [PORT_UART2] = &huart2, // DEBUG - [PORT_UART3] = &huart3, // RS485 + [PORT_433] = &huart1, // RF433 + [PORT_DEBUG] = &huart2, // DEBUG + [PORT_RS485] = &huart3, // RS485 }; /** * @brief 端口名称表(用于日志输出) */ static const char* const g_port_name_map[PORT_COUNT] = { - [PORT_UART1] = "UART1", - [PORT_UART2] = "UART2", - [PORT_UART3] = "UART3", + [PORT_433] = "UART1", + [PORT_DEBUG] = "UART2", + [PORT_RS485] = "UART3", }; ``` @@ -650,9 +650,9 @@ void Configure_UART_Priorities(void) #include "cmd_parser.h" typedef enum { - PORT_UART1 = 0, - PORT_UART2 = 1, - PORT_UART3 = 2, + PORT_433 = 0, + PORT_DEBUG = 1, + PORT_RS485 = 2, PORT_COUNT } port_id_t; @@ -683,7 +683,7 @@ void MultiUART_SendString(port_id_t port_id, const char *str); void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart) { if (huart->Instance == USART1) { - MultiUART_FeedByte(PORT_UART1, rf433_uart_rx_tmp, HAL_GetTick()); + MultiUART_FeedByte(PORT_433, rf433_uart_rx_tmp, HAL_GetTick()); HAL_UART_Receive_IT(&huart1, &rf433_uart_rx_tmp, 1); } // ... 其他端口保持原样 ...