Explorar el Código

1.上位机回复命令0x81命令中新增0x02,表示预加热结束并跳转PID。
2.新增获取8路CT7428的命令
3.上位机获取BMC和CT7428温度的时候如果有数据失效都用0xFF表示,不再是0

zhanghongyi hace 1 mes
padre
commit
d1541bfde1

+ 13 - 3
App/App/Inc/App_pc.h

@@ -3,12 +3,13 @@
   * @file    App_pc.h
   * @brief   PC 上位机通信任务接口 (App 应用层)
   *
- * @note   上位机经 USART1(115200 8N1) 下发 0x01~0x0C 命令, 本任务解析后回
- *         0x81~0x8C 应答; 另按 1s 主动上报 0xF0 状态快照 (上位机据此判在线/
+ * @note   上位机经 USART1(115200 8N1) 下发 0x01~0x0D 命令, 本任务解析后回
+ *         0x81~0x8D 应答; 另按 1s 主动上报 0xF0 状态快照 (上位机据此判在线/
  *         离线)。0x0C 是变长 ASCII 文本 (PID 在线标定, 应答同长 ASCII)。
+ *         另有两条设备主动帧: 0xF0 周期上报、0x81(STATUS=02) 预热达标事件。
  *         协议见 Com_pc_protocol.h。
   ******************************************************************************
-  */
+ */
 #ifndef __APP_PC_H
 #define __APP_PC_H
 
@@ -24,4 +25,13 @@ extern TaskHandle_t xPCTaskHandle;
  */
 void App_pc_task_create(void);
 
+/**
+ * @brief  通知上位机"预加热达标并已移交 PID 控温" (由预热任务调用)
+ * @note   只置事件标志, 帧由 pcTask 下一拍补发 (0x81, STATUS=COM_PC_STATUS_PREHEAT_DONE),
+ *         保证 USART1 协议帧只有 pcTask 一个写者。单字节标志跨任务置位, 无需临界区
+ * @param  无
+ * @return 无
+ */
+void App_pc_notify_preheat_done(void);
+
 #endif /* __APP_PC_H */

+ 76 - 3
App/App/Src/App_pc.c

@@ -6,7 +6,8 @@
  * 上位机 (PC/串口助手) 经 USART1(115200 8N1) 按下述命令交互:
  *   0x01 设置目标温度: 校验 40~90 → 写 EEPROM 参数 → 回 0x81 (01成功/00失败),
  *                      成功后自动启动预加热并切换屏幕到预加热页面;
- *   0x02 请求24路温度: 取 Mid_temp BMC 24 路快照 → 回 0x82 (无效通道补 0);
+ *   0x02 请求24路温度: 取 Mid_temp BMC 24 路快照 → 回 0x82 (无效通道填 0xFF);
+ *   0x0D 请求CT温度: 取本板 CT7428 本组 8 路 → 回 0x8D (无效通道填 0xFF);
  *   0x03/0x04 升级相关: 见 App_update (回 0x83/0x84);
  *   0x05~0x0B 控制命令 (回 0x85~0x8B): 停预热 / 跳预热 / 启停 PID /
  *              读 UID / 停加热块 / 停风扇。
@@ -17,6 +18,7 @@
  *              全部 24 路一次生效并重置积分; 空载荷 = 回读。只改 RAM 不落 EEPROM,
  *              复位回默认值。不拉起加热闭环 ⇒ 不做 CPLD/BMC 门禁。
  *   0xF0 主动上报 (无请求配对): 1s 周期状态快照,常态报文。
+ *   0x81 主动补发 (无请求配对): 预加热达标并自动移交 PID 时补发一帧 0x81(STATUS=02);
  * 帧格式 |AA 55|CMD|LEN|Payload|CRC8|, 详见 Com_pc_protocol.h。
  ******************************************************************************
  */
@@ -128,7 +130,8 @@ static void App_Pc_HandleSetTarget(const uint8_t *payload, uint8_t plen)
 /**
  * @brief  处理 0x02 请求24路温度命令: 回 0x82 快照
  * @note   取 Mid_temp BMC 24 路 (CH01..CH24 = BMC[0..23]);
- *         通道无效/无数据 (值为负) 补 0, 避免负值错报销 0~255 无符号。
+ *         通道无效/无数据 (值为负) 填 0xFF (= COM_PC_TEMP_INVALID), 与 CPLD 线上
+ *         0xFF=无效 同编码 ⇒ 上位机能分开"传感器无效"与"真实 0℃"。
  * @param  无
  * @return 无
  */
@@ -142,7 +145,7 @@ static void App_Pc_HandleGetTemp(void)
     for (i = 0; i < COM_PC_PAYLOAD_TEMPS; i++)
     {
         int16_t t = Mid_Temp_GetBmcTemp(i);
-        temps[i] = (t >= 0) ? (uint8_t)t : 0;
+        temps[i] = (t >= 0) ? (uint8_t)t : COM_PC_TEMP_INVALID;
     }
 
     flen = Com_pc_temp_reply(temps, frame);
@@ -152,6 +155,34 @@ static void App_Pc_HandleGetTemp(void)
     }
 }
 
+/**
+ * @brief  处理 0x0D 请求 CT7428 8 路温度命令: 回 0x8D 快照
+ * @note   取本板 CT7428 当前组 offset1~8 (即预加热用的那 8 路);
+ *         通道无效/无数据 (值为负) 填 0xFF (= COM_PC_TEMP_INVALID), 与 0x02 同口径。
+ *         无门禁: 纯读温度, 失联/BMC 未就绪也照常返回当前快照
+ * @param  无
+ * @return 无
+ */
+static void App_Pc_HandleGetCt(void)
+{
+    uint8_t ct[COM_PC_PAYLOAD_CT];
+    uint8_t frame[COM_PC_FRAME_MAX_LEN];
+    uint8_t i;
+    uint16_t flen;
+
+    for (i = 0; i < COM_PC_PAYLOAD_CT; i++)
+    {
+        int16_t t = Mid_Temp_GetPreheatRaw(i);
+        ct[i] = (t >= 0) ? (uint8_t)t : COM_PC_TEMP_INVALID;
+    }
+
+    flen = Com_pc_ct_reply(ct, frame);
+    if (flen > 0)
+    {
+        Mid_pc_usart_send(frame, flen);
+    }
+}
+
 /**
  * @brief  处理 0x03 进入升级模式命令
  * @note   应答(0x83)入环并等其真正上线后: 停控温输出 → 写复位魔数 → 软复位进 Bootloader。
@@ -614,6 +645,42 @@ static void App_Pc_HandleStopFans(void)
     App_Pc_SendCtrlAck(COM_PC_ACK_STOP_FANS, COM_PC_STATUS_OK);
 }
 
+/* 预加热达标移交 PID 的事件标志: 预热任务置位, pcTask 补发一帧 0x81(STATUS=02)。
+ * 单字节, 跨任务置位/清位均原子; 只标志"待发", 不携带数据 */
+static volatile uint8_t s_preheat_done_evt = 0;
+
+void App_pc_notify_preheat_done(void)
+{
+    s_preheat_done_evt = 1;
+}
+
+/**
+ * @brief  补发"预加热达标并移交 PID"事件帧 (0x81, STATUS=02)
+ * @note   由 pcTask 每拍调用: 有事件才发一帧并清标志, 保证 USART1 只有一个写者。
+ *         与 0x01 的即时应答同为 0x81, 靠 STATUS(02) 区分
+ * @param  无
+ * @return 无
+ */
+static void App_Pc_ReportPreheatDone(void)
+{
+    uint8_t  frame[COM_PC_FRAME_MAX_LEN];
+    uint16_t flen;
+
+    if (!s_preheat_done_evt)
+    {
+        return;
+    }
+    s_preheat_done_evt = 0;
+
+    flen = Com_pc_set_ack(COM_PC_STATUS_PREHEAT_DONE, frame);
+    if (flen > 0)
+    {
+        (void)Mid_pc_usart_send(frame, flen);
+        LOG_PRINTF("[INFO] PC: preheat done, notify 0x81 status=%d\r\n",
+                   (int)COM_PC_STATUS_PREHEAT_DONE);
+    }
+}
+
 /**
  * @brief  周期上报设备状态 (0xF0): 1s 一帧, 状态位有翻转则立刻补发
  * @note   只为让上位机判在线/离线, 不参与任何控制; 载荷 8B 见 Com_pc_protocol.h。
@@ -736,6 +803,11 @@ static void App_Pc_PollRx(void)
                     LOG_PRINTF("[INFO] PC: get temp request\r\n");
                     App_Pc_HandleGetTemp();
                 }
+                else if (cmd == COM_PC_CMD_GET_CT)
+                {
+                    LOG_PRINTF("[INFO] PC: get CT request\r\n");
+                    App_Pc_HandleGetCt();
+                }
                 else if (cmd == COM_PC_CMD_ENTER_BL)
                 {
                     App_Pc_HandleEnterBootloader();
@@ -821,6 +893,7 @@ static void Task_PC_Process(void *argument)
 
         App_Pc_PollRx();
         App_Pc_ReportStatus();
+        App_Pc_ReportPreheatDone();
         vTaskDelay(pdMS_TO_TICKS(PC_POLL_PERIOD_MS));
     }
 }

+ 2 - 0
App/App/Src/App_preheat.c

@@ -27,6 +27,7 @@
 #include "Mid_temp.h"
 #include "Ui_preheat.h"
 #include "App_guardian.h"
+#include "App_pc.h"
 
 /* 队列长度 */
 #define APP_PREHEAT_UI_QUEUE_LEN    4      /* 屏端命令队列 */
@@ -205,6 +206,7 @@ static void preheat_ctrl_step(void)
         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 任务 */
+        App_pc_notify_preheat_done();      /* 通知上位机: 补发 0x81 STATUS=02 (达标移交) */
         s_state = PREHEAT_STATE_IDLE;      /* 输出权移交 PID 控温任务 */
     }
 }

+ 23 - 2
App/Com/Inc/Com_pc_protocol.h

@@ -28,9 +28,16 @@
                                          * 成功: 存 EEPROM + 自动启预热 + 切预热页;
                                          * 失败: 越界 或 EEPROM 写失败 */
 #define COM_PC_CMD_GET_TEMP     0x02    /* PC->MCU 0B: 取 24 路 BMC 温度 (应答 0x82) */
-#define COM_PC_ACK_SET          0x81    /* MCU->PC 1B: 0x01 的 STATUS */
+#define COM_PC_CMD_GET_CT       0x0D    /* PC->MCU 0B: 取 CT7428 当前组 8 路温度 (应答 0x8D) */
+#define COM_PC_ACK_SET          0x81    /* MCU->PC 1B: 0x01 的 STATUS (另有异步值 02, 见 STATUS 段) */
 #define COM_PC_ACK_TEMP         0x82    /* MCU->PC 24B: CH01..CH24 = BMC[0..23], 1B/路;
-                                         * 通道无效(值<0)补 0 —— 无符号载荷放不下负值 */
+                                         * 通道无效(值<0) 填 COM_PC_TEMP_INVALID(0xFF) */
+#define COM_PC_ACK_CT           0x8D    /* MCU->PC 8B: CT7428 当前组 offset1~8, 1B/路;
+                                         * 通道无效(值<0) 填 COM_PC_TEMP_INVALID(0xFF) */
+
+/* 温度应答里的"无效"哨兵: 与 CPLD 线上编码一致 (0x00~0xFE = 0~254℃, 0xFF = 无效)。
+ * 0xFF 不可能是真实温度 (最高 254) ⇒ 无歧义。注意!别用 0 表示无效, 与"真实 0℃"混淆 */
+#define COM_PC_TEMP_INVALID     0xFF
 
 /* --- OTA 命令 (App 侧处理, 配套 Bootloader) --- */
 #define COM_PC_CMD_ENTER_BL     0x03    /* PC->MCU 0B: 进入升级模式 (应答 0x83)
@@ -112,6 +119,7 @@
 #define COM_PC_PAYLOAD_TARGET   1       /* 0x01 请求; 解析侧按 plen>=1 判, 未引用本宏 */
 #define COM_PC_PAYLOAD_ACK      1       /* 0x81 / 0x85~0x88 / 0x8A / 0x8B */
 #define COM_PC_PAYLOAD_TEMPS    24      /* 0x82 应答: 24 路; 也是全协议最大载荷 */
+#define COM_PC_PAYLOAD_CT       8       /* 0x8D 应答: 本组 8 路 CT */
 #define COM_PC_PAYLOAD_UID      12      /* 0x89 应答: 3 字 x 4B */
 #define COM_PC_PAYLOAD_STAT     8       /* 0xF0 上报: 见上方字段说明
                                          * 注意!别改成 6/21/24/67 —— YMODEM 的单字节 ACK
@@ -123,6 +131,11 @@
 #define COM_PC_STATUS_OK        1       /* 成功 */
 #define COM_PC_STATUS_FAIL      0       /* 失败 (越界 / EEPROM 写失败 / 前提不满足) */
 
+/* 注意!0x81 的"异步"状态: 预加热达标并自动移交 PID 时, 设备主动补发一帧 0x81,
+ * 载荷 STATUS=02。它不是 0x01 请求的即时应答(那条只回 00/01); 上位机按
+ * "收到 0x81 且载荷=02"识别"预热已结束、已进入控温"。仅此一处使用 02。 */
+#define COM_PC_STATUS_PREHEAT_DONE 2    /* 预加热达标, 已移交 PID 控温 */
+
 /* 目标温度合法范围 (与 UI 滑条、EEPROM 参数一致) */
 #define COM_PC_TARGET_MIN       40
 #define COM_PC_TARGET_MAX       90
@@ -189,6 +202,14 @@ uint16_t Com_pc_uid_reply(const uint8_t *uid12, uint8_t *frame);
  */
 uint16_t Com_pc_temp_reply(const uint8_t *temps, uint8_t *frame);
 
+/**
+ * @brief  封装 CT7428 8 路温度返回 (0x8D) 帧: 8 字节温度, 每路 1 字节整数 ℃
+ * @param  ct8   : 8 路温度数组指针 (本组 offset1~8)
+ * @param  frame : 输出帧缓冲区指针
+ * @return 帧总长度; 输入非法返回 0
+ */
+uint16_t Com_pc_ct_reply(const uint8_t *ct8, uint8_t *frame);
+
 /**
  * @brief  封装 PID 标定文本应答 (0x8C): 直接把 ASCII 文本装进载荷
  * @note   文本由调用方按本文件 0x0C/0x8C 说明构造 (如 "OK P=2.5 I=0.125 D=0.04")

+ 16 - 0
App/Com/Src/Com_pc_protocol.c

@@ -176,6 +176,22 @@ uint16_t Com_pc_temp_reply(const uint8_t *temps, uint8_t *frame)
     return Com_pc_encode(COM_PC_ACK_TEMP, temps, COM_PC_PAYLOAD_TEMPS, frame);
 }
 
+/**
+ * @brief  封装 CT7428 8 路温度返回 (0x8D) 帧: 8 字节温度, 每路 1 字节整数 ℃
+ * @param  ct8   : 8 路温度数组指针 (本组 offset1~8, 每路 0~255)
+ * @param  frame : 输出帧缓冲区指针
+ * @return 帧总长度; 输入非法返回 0
+ */
+uint16_t Com_pc_ct_reply(const uint8_t *ct8, uint8_t *frame)
+{
+    if (ct8 == NULL || frame == NULL)
+    {
+        return 0;
+    }
+
+    return Com_pc_encode(COM_PC_ACK_CT, ct8, COM_PC_PAYLOAD_CT, frame);
+}
+
 /**
  * @brief  封装设备状态上报 (0xF0): 8 字节状态快照
  * @note   非请求式主动帧, 由 pcTask 按 1s 周期(或状态位翻转时) 发出

+ 11 - 0
App/Mid/Inc/Mid_temp.h

@@ -25,6 +25,7 @@
 #define MID_TEMP_CT_CH           27    /* CT7428 温度通道数 (协议定长, 3 芯片 x 9 寄存器) */
 #define MID_TEMP_CAVITY_NUM      4     /* 腔体(风扇分区)数: 1 腔体 = 1 组加热区 */
 #define MID_TEMP_PREHEAT_NUM     4     /* 预加热达标判定路数 = 腔体数 (每腔体 2 路 CT 取均温) */
+#define MID_TEMP_PREHEAT_RAW_NUM (MID_TEMP_PREHEAT_NUM * 2)  /* 8: 预热涉及的本组 CT 原始路数 */
 
 /* 温度无效哨兵: getter 对 wire 值 0xFF 返回该值 (温度非负, 负值必为无效) */
 #define MID_TEMP_INVALID         (-127)
@@ -114,6 +115,16 @@ int16_t Mid_Temp_GetCtTemp(uint8_t ch);
  */
 int16_t Mid_Temp_GetPreheat(uint8_t idx);
 
+/**
+ * @brief  获取本组预热用 CT7428 单路原始读数 (0~7)
+ * @note   展开 Mid_temp.c 的预加热通道表: idx/2 = 腔体号, idx%2 = 进风/出风,
+ *         第几组由 MID_TEMP_CT_GROUP 选。与 Mid_Temp_GetPreheat(取均温) 不同,
+ *         本函数给单路原始值 —— 供上位机 0x0D 查看本组 8 路 CT
+ * @param  idx : 本组序号 (0~MID_TEMP_PREHEAT_RAW_NUM-1)
+ * @return 温度 (℃); 越界或无效返回 MID_TEMP_INVALID (-127)
+ */
+int16_t Mid_Temp_GetPreheatRaw(uint8_t idx);
+
 /**
  * @brief  获取腔体(风扇分区)温度
  * @note   专供风扇分档: 每路 = 该腔体 BMC 最中间 2 路的均温 (通道表见

+ 12 - 0
App/Mid/Src/Mid_temp.c

@@ -152,6 +152,18 @@ int16_t Mid_Temp_GetPreheat(uint8_t idx)
     return (int16_t)((a + b + 1) / 2);
 }
 
+int16_t Mid_Temp_GetPreheatRaw(uint8_t idx)
+{
+    if (idx >= MID_TEMP_PREHEAT_RAW_NUM)
+    {
+        return MID_TEMP_INVALID;
+    }
+
+    /* 展开预加热通道表: idx/2 = 腔体号, idx%2 = 进风/出风; 组号由 MID_TEMP_CT_GROUP 选。
+     * 返回单路原始读数, 不取均温 —— 供上位机 0x0D 查看本组 8 路 CT 各自的值 */
+    return Mid_Temp_GetCtTemp(s_preheat_ct_ch[idx / 2][idx % 2]);
+}
+
 int16_t Mid_Temp_GetCavityTemp(uint8_t idx)
 {
     int16_t a;