Просмотр исходного кода

日志体系整理: 格式统一 + Com层开关宏 + 行原子输出

zhanghongyi 2 месяцев назад
Родитель
Сommit
5bd6d6d0c1

+ 19 - 18
App/Src/App_cpld.c

@@ -18,6 +18,7 @@
 #include "task.h"
 #include "Com_cpld_protocol.h"
 #include "Com_cpld_usart.h"
+#include "Com_usb_cdc.h"
 #include "Mid_pid.h"
 #include "Mid_temp.h"
 
@@ -50,7 +51,7 @@ static void App_Cpld_DumpSnapshot(void)
     const mid_temp_snapshot_t *s = Mid_Temp_GetSnapshot();
     uint8_t i;
 
-    printf("[SNAP] mode=%d fault=%04X wd=%d/%d manual=%d "
+    TELEMETRY_PRINTF("[SNAP] mode=%d fault=%04X wd=%d/%d manual=%d "
            "bmc(v%d st%d) ct(v%d st%d) hardprot=%d "
            "ep(bmc=%u ct=%u ctrl=%u) age(bmc=%u ct=%u)\r\n",
            (int)s->mode, (int)s->fault_flags,
@@ -63,39 +64,39 @@ static void App_Cpld_DumpSnapshot(void)
            (unsigned)s->control_epoch,
            (unsigned)s->bmc_age, (unsigned)s->ct_age);
 
-    printf("[BMC 0-11] ");
+    TELEMETRY_PRINTF("[BMC 0-11] ");
     for (i = 0; i < 12; i++)
     {
-        printf("%d=%d ", (int)i, (int)s->bmc[i]);
+        TELEMETRY_PRINTF("%d=%d ", (int)i, (int)s->bmc[i]);
     }
-    printf("\r\n[BMC 12-23] ");
+    TELEMETRY_PRINTF("\r\n[BMC 12-23] ");
     for (i = 12; i < 24; i++)
     {
-        printf("%d=%d ", (int)i, (int)s->bmc[i]);
+        TELEMETRY_PRINTF("%d=%d ", (int)i, (int)s->bmc[i]);
     }
 
-    printf("\r\n[CT 0-13] ");
+    TELEMETRY_PRINTF("\r\n[CT 0-13] ");
     for (i = 0; i < 14; i++)
     {
-        printf("%d=%d ", (int)i, (int)s->ct[i]);
+        TELEMETRY_PRINTF("%d=%d ", (int)i, (int)s->ct[i]);
     }
-    printf("\r\n[CT 14-26] ");
+    TELEMETRY_PRINTF("\r\n[CT 14-26] ");
     for (i = 14; i < 27; i++)
     {
-        printf("%d=%d ", (int)i, (int)s->ct[i]);
+        TELEMETRY_PRINTF("%d=%d ", (int)i, (int)s->ct[i]);
     }
 
-    printf("\r\n[PTC] req:");
+    TELEMETRY_PRINTF("\r\n[PTC] req:");
     for (i = 0; i < 24; i++)
     {
-        printf(" %d", (int)s->ptc_requested[i]);
+        TELEMETRY_PRINTF(" %d", (int)s->ptc_requested[i]);
     }
-    printf("\r\n      app:");
+    TELEMETRY_PRINTF("\r\n      app:");
     for (i = 0; i < 24; i++)
     {
-        printf(" %d", (int)s->ptc_applied[i]);
+        TELEMETRY_PRINTF(" %d", (int)s->ptc_applied[i]);
     }
-    printf("\r\n[FAN] front req/app: %d/%d %d/%d %d/%d %d/%d | "
+    TELEMETRY_PRINTF("\r\n[FAN] front req/app: %d/%d %d/%d %d/%d %d/%d | "
            "rear req/app: %d/%d %d/%d %d/%d %d/%d\r\n",
            (int)s->front_requested[0], (int)s->front_applied[0],
            (int)s->front_requested[1], (int)s->front_applied[1],
@@ -158,12 +159,12 @@ static void App_Cpld_PollRx(void)
                     }
                     else
                     {
-                        printf("[ERR] snapshot len=%d\r\n", (int)plen);
+                        LOG_PRINTF("[ERR] CPLD: snapshot len=%d\r\n", (int)plen);
                     }
                 }
                 else if (type == COM_CPLD_TYPE_ACK_NACK)
                 {
-                    printf("[ACK] ok=%d req=0x%02X st=%d detail=%d\r\n",
+                    TELEMETRY_PRINTF("[ACK] ok=%d req=0x%02X st=%d detail=%d\r\n",
                            (int)payload[0], (int)payload[1],
                            (int)payload[2], (int)payload[3]);
                 }
@@ -221,7 +222,7 @@ static void Task_CPLD_Process(void *argument)
     uint8_t frame[COM_CPLD_FRAME_MAX_LEN];
     uint16_t flen;
 
-    printf("CPLD process task running...\r\n");
+    LOG_PRINTF("[INFO] CPLD: task running\r\n");
 
     Mid_PID_Init();
     Mid_Temp_Init();
@@ -264,6 +265,6 @@ void App_cpld_task_create(void)
                       NULL, TASK_CPLD_PRIORITY, &xCPLDProcessTaskHandle);
     if (ret != pdPASS)
     {
-        printf("[ERR] CPLD process task create failed\r\n");
+        LOG_PRINTF("[ERR] CPLD: task create failed\r\n");
     }
 }

+ 3 - 7
App/Src/App_display.c

@@ -14,6 +14,7 @@
 #include "lv_port_disp.h"
 #include "lv_port_indev.h"
 #include "Int_gt1151.h"
+#include "Com_usb_cdc.h"
 #include "Ui.h"
 
 /* 任务参数 (数字越大优先级越高); LVGL 渲染/动画/触摸处理较重, 栈需较大 */
@@ -32,7 +33,7 @@ TaskHandle_t xLVGLDisplayTaskHandle = NULL;
 static void Task_LVGL_Display(void *argument)
 {
     (void)argument;
-    printf("LVGL display task running...\r\n");
+    LOG_PRINTF("[INFO] LVGL: task running\r\n");
 
     /* LVGL 内核初始化 */
     lv_init();
@@ -43,14 +44,9 @@ static void Task_LVGL_Display(void *argument)
     /* 初始化 GT1151Q 电容触摸芯片 */
     if (Int_gt1151_init() == 0)
     {
-        printf("GT1151 Init OK\r\n");
         /* 注册触摸输入设备 (注意: 需在 lv_init 之后、主循环 lv_timer_handler 之前) */
         lv_port_indev_init();
     }
-    else
-    {
-        printf("[ERR] GT1151 Init failed\r\n");
-    }
 
     /* 创建工业设备主页 (UI 页面层) */
     Ui_init();
@@ -77,6 +73,6 @@ void App_display_task_create(void)
                       NULL, TASK_LVGL_PRIORITY, &xLVGLDisplayTaskHandle);
     if (ret != pdPASS)
     {
-        printf("[ERR] LVGL display task create failed\r\n");
+        LOG_PRINTF("[ERR] LVGL: task create failed\r\n");
     }
 }

+ 8 - 26
App/Src/App_pc.c

@@ -17,6 +17,7 @@
 #include "task.h"
 #include "Com_pc_protocol.h"
 #include "Com_pc_usart.h"
+#include "Com_usb_cdc.h"
 #include "Com_parameter.h"
 #include "Mid_temp.h"
 
@@ -53,16 +54,16 @@ static void App_Pc_HandleSetTarget(const uint8_t *payload, uint8_t plen)
             if (Com_parameter_set_target(temp) == 0)
             {
                 status = COM_PC_STATUS_OK;
-                printf("[PC] set target=%d OK\r\n", (int)temp);
+                LOG_PRINTF("[OK] PC: set target=%d\r\n", (int)temp);
             }
             else
             {
-                printf("[PC] set target=%d EEPROM fail\r\n", (int)temp);
+                LOG_PRINTF("[ERR] PC: set target=%d EEPROM fail\r\n", (int)temp);
             }
         }
         else
         {
-            printf("[PC] set target=%d out of range\r\n", (int)temp);
+            LOG_PRINTF("[ERR] PC: set target=%d out of range\r\n", (int)temp);
         }
     }
 
@@ -147,12 +148,12 @@ static void App_Pc_PollRx(void)
                 }
                 else if (cmd == COM_PC_CMD_GET_TEMP)
                 {
-                    printf("[PC] get temp request\r\n");
+                    LOG_PRINTF("[INFO] PC: get temp request\r\n");
                     App_Pc_HandleGetTemp();
                 }
                 else
                 {
-                    printf("[PC] unknown cmd=0x%02X\r\n", (int)cmd);
+                    LOG_PRINTF("[ERR] PC: unknown cmd=0x%02X\r\n", (int)cmd);
                 }
                 pos += need;
             }
@@ -178,24 +179,6 @@ static void App_Pc_PollRx(void)
     }
 }
 
-/**
- * @brief 启动自检: 用协议文档示例向量核对 CRC8 实现
- * @note   AA 55 02 00 → AD; AA 55 01 01 32 (50℃) → 7C; AA 55 01 01 46 (70℃) → 37
- * @param  无
- * @return 无
- */
-static void App_Pc_SelfTest(void)
-{
-    static const uint8_t v_req[]  = { 0xAA, 0x55, 0x02, 0x00 };
-    static const uint8_t v_set50[] = { 0xAA, 0x55, 0x01, 0x01, 0x32 };
-    static const uint8_t v_set70[] = { 0xAA, 0x55, 0x01, 0x01, 0x46 };
-
-    printf("[PC] CRC self-test: req=%02X/AD set50=%02X/7C set70=%02X/37\r\n",
-           (int)Com_pc_crc8(v_req,  sizeof(v_req)),
-           (int)Com_pc_crc8(v_set50, sizeof(v_set50)),
-           (int)Com_pc_crc8(v_set70, sizeof(v_set70)));
-}
-
 /**
  * @brief  上位机通信任务体: 周期轮询 USART1 RX 并应答
  * @param  argument : 任务入参 (未使用)
@@ -205,8 +188,7 @@ static void Task_PC_Process(void *argument)
 {
     (void)argument;
 
-    printf("PC host task running...\r\n");
-    App_Pc_SelfTest();
+    LOG_PRINTF("[INFO] PC: task running\r\n");
 
     while (1)
     {
@@ -229,6 +211,6 @@ void App_pc_task_create(void)
                       NULL, TASK_PC_PRIORITY, &xPCTaskHandle);
     if (ret != pdPASS)
     {
-        printf("[ERR] PC host task create failed\r\n");
+        LOG_PRINTF("[ERR] PC: task create failed\r\n");
     }
 }

+ 5 - 4
App/Src/App_pidControl.c

@@ -17,6 +17,7 @@
 #include "task.h"
 #include "Com_cpld_protocol.h"
 #include "Com_cpld_usart.h"
+#include "Com_usb_cdc.h"
 #include "Mid_pid.h"
 #include "Mid_temp.h"
 
@@ -45,7 +46,7 @@ static void Task_PidControl(void *argument)
     uint8_t  frame[COM_CPLD_FRAME_MAX_LEN];
     uint16_t flen;
 
-    printf("PID control task running...\r\n");
+    LOG_PRINTF("[INFO] PID: task running\r\n");
 
     while (1)
     {
@@ -123,7 +124,7 @@ void App_pidControl_Start(uint8_t target)
     }
 
     s_running = 1;
-    printf("[PID] start target=%d\r\n", (int)target);
+    LOG_PRINTF("[INFO] PID: start target=%d\r\n", (int)target);
 }
 
 void App_pidControl_Stop(void)
@@ -151,7 +152,7 @@ void App_pidControl_Stop(void)
         Com_cpld_usart_send(frame, flen);
     }
 
-    printf("[PID] stop\r\n");
+    LOG_PRINTF("[INFO] PID: stop\r\n");
 }
 
 uint8_t App_pidControl_is_running(void)
@@ -173,6 +174,6 @@ void App_pidControl_task_create(void)
                       NULL, APP_PIDCONTROL_TASK_PRIORITY, &xPidControlTaskHandle);
     if (ret != pdPASS)
     {
-        printf("[ERR] PID control task create failed\r\n");
+        LOG_PRINTF("[ERR] PID: task create failed\r\n");
     }
 }

+ 10 - 9
App/Src/App_preheat.c

@@ -21,6 +21,7 @@
 #include "queue.h"
 #include "Com_cpld_protocol.h"
 #include "Com_cpld_usart.h"
+#include "Com_usb_cdc.h"
 #include "Mid_pid.h"
 #include "Mid_temp.h"
 #include "Ui_preheat.h"
@@ -65,7 +66,7 @@ static int16_t  s_target = 0;
 static void preheat_handle_start(const app_preheat_cmd_t *cmd)
 {
     s_target = cmd->target;
-    printf("[Preheat] start target=%d src=%d\r\n", (int)s_target, (int)cmd->src);
+    LOG_PRINTF("[INFO] Preheat: start target=%d src=%d\r\n", (int)s_target, (int)cmd->src);
     App_pidControl_Stop();
     s_state = PREHEAT_STATE_RUNNING;
 }
@@ -78,7 +79,7 @@ static void preheat_handle_start(const app_preheat_cmd_t *cmd)
  */
 static void preheat_handle_stop(const app_preheat_cmd_t *cmd)
 {
-    printf("[Preheat] stop src=%d\r\n", (int)cmd->src);
+    LOG_PRINTF("[INFO] Preheat: stop src=%d\r\n", (int)cmd->src);
     App_pidControl_Stop();
     s_state = PREHEAT_STATE_IDLE;
 }
@@ -93,7 +94,7 @@ static void preheat_handle_stop(const app_preheat_cmd_t *cmd)
 static void preheat_handle_skip(const app_preheat_cmd_t *cmd)
 {
     s_target = cmd->target;
-    printf("[Preheat] skip, start PID control target=%d src=%d\r\n",
+    LOG_PRINTF("[INFO] Preheat: skip, start PID control target=%d src=%d\r\n",
            (int)s_target, (int)cmd->src);
     App_pidControl_Start((uint8_t)s_target);
     Ui_preheat_done_exit();
@@ -109,7 +110,7 @@ static void preheat_handle_skip(const app_preheat_cmd_t *cmd)
 static void preheat_handle_start_pid(const app_preheat_cmd_t *cmd)
 {
     s_target = cmd->target;
-    printf("[Preheat] start PID directly target=%d src=%d\r\n",
+    LOG_PRINTF("[INFO] Preheat: start PID directly target=%d src=%d\r\n",
            (int)s_target, (int)cmd->src);
     App_pidControl_Start((uint8_t)s_target);
     s_state = PREHEAT_STATE_IDLE;
@@ -161,7 +162,7 @@ static void preheat_ctrl_step(void)
 
     if (all_reached)
     {
-        printf("[Preheat] reach target=%d, hand over to PID\r\n", (int)s_target);
+        LOG_PRINTF("[INFO] Preheat: reach target=%d, hand over to PID\r\n", (int)s_target);
         App_pidControl_Start((uint8_t)s_target);
         Ui_preheat_done_exit();            /* 线程安全: 内部 lv_async_call 投递到 LVGL 任务 */
         s_state = PREHEAT_STATE_IDLE;      /* 输出权移交 PID 控温任务 */
@@ -182,7 +183,7 @@ static void App_preheat_init(void)
                              * sizeof(app_preheat_cmd_t));
     if (s_qset == NULL)
     {
-        printf("[ERR] Preheat queue-set create failed\r\n");
+        LOG_PRINTF("[ERR] Preheat: queue-set create failed\r\n");
         return;
     }
 
@@ -194,7 +195,7 @@ static void App_preheat_init(void)
     {
         xQueueAddToSet(s_upper_q, s_qset);
     }
-    printf("[OK] Preheat queue-set init\r\n");
+    LOG_PRINTF("[OK] Preheat: queue-set init\r\n");
 }
 
 BaseType_t App_preheat_send_cmd(app_preheat_cmd_t *cmd)
@@ -230,7 +231,7 @@ void Task_Preheat(void *argument)
     QueueSetMemberHandle_t qm;
     app_preheat_cmd_t      cmd;
 
-    printf("Preheat task running...\r\n");
+    LOG_PRINTF("[INFO] Preheat: task running\r\n");
 
     while (1)
     {
@@ -300,6 +301,6 @@ void App_preheat_task_create(void)
                       NULL, APP_PREHEAT_TASK_PRIORITY, &xPreheatTaskHandle);
     if (ret != pdPASS)
     {
-        printf("[ERR] Preheat task create failed\r\n");
+        LOG_PRINTF("[ERR] Preheat: task create failed\r\n");
     }
 }

+ 25 - 0
Com/Inc/Com_usb_cdc.h

@@ -2,6 +2,7 @@
 #define __COM_USB_CDC_H
 
 #include <stdint.h>
+#include <stdio.h>
 
 #define COM_USB_TX_BUFFER_SIZE   2048
 #define COM_USB_RX_BUFFER_SIZE    512
@@ -12,6 +13,30 @@
    避免第一包掉进主机重枚举窗口被丢弃;酌情调大(如 1000) */
 #define COM_USB_BOOT_HOLDOFF_MS   500
 
+/* 日志行缓冲(字节): 须覆盖最长单行(SNAP行约123B); 超长截断 */
+#define COM_LOG_LINE_BUF_SIZE  192
+
+/* 打印总开关: 0=关闭全部打印(含遥测dump/故障打印, 关闭后零开销),1=开启 */
+#define COM_LOG_ENABLE        1
+/* 遥测开关: 0=关闭周期遥测dump(SNAP/BMC/CT/PTC/FAN/ACK),1=开启; 仅 COM_LOG_ENABLE=1 时有效 */
+#define COM_TELEMETRY_ENABLE  1
+
+/* 调试日志宏: 开->Com_log_printf(行原子), 关->空语句(零开销) */
+#if COM_LOG_ENABLE
+#define LOG_PRINTF(...)         Com_log_printf(__VA_ARGS__)
+#else
+#define LOG_PRINTF(...)         do { } while (0)
+#endif
+
+/* 遥测dump宏: 总开关与遥测开关同时为1才打印 */
+#if (COM_LOG_ENABLE && COM_TELEMETRY_ENABLE)
+#define TELEMETRY_PRINTF(...)   Com_log_printf(__VA_ARGS__)
+#else
+#define TELEMETRY_PRINTF(...)   do { } while (0)
+#endif
+
+int Com_log_printf(const char *fmt, ...);
+
 void Com_usb_cdc_init(void);
 void Com_usb_cdc_rx_handler(uint8_t *buf, uint32_t len);
 uint32_t Com_usb_cdc_read(uint8_t *buf, uint32_t len);

+ 52 - 0
Com/Src/Com_usb_cdc.c

@@ -1,6 +1,7 @@
 #include "Com_usb_cdc.h"
 #include <stdio.h>
 #include <string.h>
+#include <stdarg.h>
 
 #include "FreeRTOS.h"
 #include "task.h"
@@ -111,6 +112,7 @@ void Com_usb_cdc_init(void)
 
 /**
  * @brief printf 重定向:把字符写入 TX 流缓冲(多任务安全)
+ * @note  逐字符入队, 仅字符级原子; 整行打印请用 Com_log_printf
  */
 int fputc(int ch, FILE *f)
 {
@@ -122,6 +124,56 @@ int fputc(int ch, FILE *f)
     return ch;
 }
 
+/**
+ * @brief 行原子日志输出: 先格式化到行缓冲, 再单次整行入队
+ * @note  单次 xStreamBufferSend 独占一个临界区, 行内不被其他任务
+ *        打印穿插; 超时为0, 缓冲满时丢弃不阻塞; 单行超长截断。
+ * @param fmt : 格式串 (同 printf)
+ * @return vsnprintf 返回的字符数 (截断时为原长度), 出错返回 0
+ */
+int Com_log_printf(const char *fmt, ...)
+{
+    char buf[COM_LOG_LINE_BUF_SIZE];
+    va_list ap;
+    int n;
+    size_t len;
+
+    if ((fmt == NULL) || (xUsbTxStream == NULL))
+    {
+        return 0;
+    }
+
+    va_start(ap, fmt);
+    n = vsnprintf(buf, sizeof(buf), fmt, ap);
+    va_end(ap);
+
+    if (n < 0)
+    {
+        /* 非C99语义的库截断时返回负值: 缓冲区已有截断内容,
+         * 强制封尾后按实际长度发送 */
+        buf[sizeof(buf) - 1] = '\0';
+        len = strlen(buf);
+    }
+    else
+    {
+        if (n == 0)
+        {
+            return 0;
+        }
+        len = (size_t)n;
+        if (len >= sizeof(buf))
+        {
+            len = sizeof(buf) - 1;
+        }
+    }
+    if (len == 0)
+    {
+        return 0;
+    }
+    xStreamBufferSend(xUsbTxStream, (const uint8_t *)buf, len, 0);
+    return n;
+}
+
 /**
  * @brief CDC 接收回调桥接:把收到的数据存入 RX 流缓冲
  */

+ 3 - 3
Core/Src/main.c

@@ -76,7 +76,7 @@ static void MPU_Config(void);
 /* USER CODE BEGIN 0 */
 void vApplicationStackOverflowHook(TaskHandle_t xTask, char *pcTaskName)
 {
-	printf("Task stack overflow");
+	LOG_PRINTF("[ERR] Task stack overflow");
     while (1) {
     }
 }
@@ -138,7 +138,7 @@ int main(void)
   Com_usb_cdc_init();
   Com_cpld_usart_init();
   Com_pc_usart_init();
-  printf("System Init OK\r\n");
+  LOG_PRINTF("[OK] System Init\r\n");
   /* USER CODE END 2 */
 
   /* Infinite loop */
@@ -150,7 +150,7 @@ int main(void)
 
   while (1)
   {
-    printf("FreeRTOS scheduler exited unexpectedly!\r\n");
+    LOG_PRINTF("[ERR] RTOS scheduler exited\r\n");
     /* USER CODE END WHILE */
 
     /* USER CODE BEGIN 3 */

+ 2 - 1
Core/Src/stm32h7xx_it.c

@@ -25,6 +25,7 @@
 #include "FreeRTOS.h"
 #include "task.h"
 #include "stdio.h"
+#include "Com_usb_cdc.h"
 /* USER CODE END Includes */
 
 /* Private typedef -----------------------------------------------------------*/
@@ -98,7 +99,7 @@ void HardFault_Handler(void)
   while (1)
   {
     /* USER CODE BEGIN W1_HardFault_IRQn 0 */
-    printf("Hard fault occurred!\n");
+    LOG_PRINTF("[ERR] Hard fault\r\n");
     /* USER CODE END W1_HardFault_IRQn 0 */
   }
 }

+ 0 - 6
Int/Src/Int_gt1151.c

@@ -1,4 +1,3 @@
-#include <stdio.h>
 #include "Int_gt1151.h"
 #include "Core_i2c_sw.h"
 
@@ -116,11 +115,6 @@ uint8_t Int_gt1151_init(void)
 
     HAL_Delay(100);
 
-    /* 读取并打印产品 ID (仅作调试参考, 不同屏幕批次 ID 可能不同) */
-    temp[4] = 0;
-    Int_gt1151_rd_reg(INT_GT1151_PID_REG, temp, 4);
-    printf("CTP ID:%s\r\n", temp);
-
     /* 软复位并进入正常工作状态 */
     temp[0] = 0X02;
     Int_gt1151_wr_reg(INT_GT1151_CTRL_REG, temp, 1);

+ 3 - 0
LVGL8v3/ui/Ui_temp_setting.c

@@ -19,6 +19,7 @@
 #include "Ui_preheat.h"
 #include "Ui_theme.h"
 #include "Com_parameter.h"
+#include "Com_usb_cdc.h"
 #include "App_preheat.h"
 
 /* 目标温度范围 */
@@ -535,10 +536,12 @@ static void ui_tset_apply_cb(lv_event_t * e)
 
     if (ret == 0)
     {
+        LOG_PRINTF("[OK] UI: set target=%d\r\n", (int)s_target);
         ui_tset_show_saved();
     }
     else
     {
+        LOG_PRINTF("[ERR] UI: set target=%d EEPROM fail\r\n", (int)s_target);
         ui_tset_show_failed();
     }
 }