|
|
@@ -0,0 +1,234 @@
|
|
|
+/**
|
|
|
+ ******************************************************************************
|
|
|
+ * @file App_pc.c
|
|
|
+ * @brief PC 上位机通信任务 (App 应用层)
|
|
|
+ *
|
|
|
+ * 上位机 (Qt/串口助手) 经 USART1(115200 8N1) 按"精简版"协议交互:
|
|
|
+ * 0x01 设置目标温度: 校验 40~90 → 写 EEPROM 参数 → 回 0x81 (01成功/00失败);
|
|
|
+ * 0x02 请求24路温度: 取 Mid_temp BMC 24 路快照 → 回 0x82 (无效通道补 0)。
|
|
|
+ * 帧格式 |AA 55|CMD|LEN|Payload|CRC8|, 详见 Com_pc.h。
|
|
|
+ * 注: 0x01 仅保存目标温度, 不自动启动预加热/PID (与 UI 的 Apply 行为一致)。
|
|
|
+ ******************************************************************************
|
|
|
+ */
|
|
|
+#include <stdio.h>
|
|
|
+#include <string.h>
|
|
|
+#include "App_pc.h"
|
|
|
+#include "FreeRTOS.h"
|
|
|
+#include "task.h"
|
|
|
+#include "Com_pc.h"
|
|
|
+#include "Com_usart1.h"
|
|
|
+#include "Com_parameter.h"
|
|
|
+#include "Mid_temp.h"
|
|
|
+
|
|
|
+/* 任务参数 */
|
|
|
+#define TASK_PC_PRIORITY 2
|
|
|
+#define TASK_PC_STACK_SIZE 512
|
|
|
+
|
|
|
+/* 轮询周期: 上位机 500ms 请求一次温度, 20ms 轮询足够即时应答 */
|
|
|
+#define PC_POLL_PERIOD_MS 20
|
|
|
+
|
|
|
+/* RX 滚动缓冲 (一帧最多 COM_PC_FRAME_MAX_LEN 字节) */
|
|
|
+#define PC_RX_BUF_LEN (COM_PC_FRAME_MAX_LEN + 8)
|
|
|
+
|
|
|
+TaskHandle_t xPCTaskHandle = NULL;
|
|
|
+
|
|
|
+/**
|
|
|
+ * @brief 处理 0x01 设置目标温度命令
|
|
|
+ * @note 仅保存到 EEPROM 参数 (等效 UI Apply); 越界或写入失败回 0x81 FAIL
|
|
|
+ * @param payload : 载荷指针 (长度 1, temp)
|
|
|
+ * @param plen : 载荷长度
|
|
|
+ * @return 无
|
|
|
+ */
|
|
|
+static void App_Pc_HandleSetTarget(const uint8_t *payload, uint8_t plen)
|
|
|
+{
|
|
|
+ uint8_t status = COM_PC_STATUS_FAIL;
|
|
|
+ uint8_t frame[COM_PC_FRAME_MAX_LEN];
|
|
|
+ uint16_t flen;
|
|
|
+
|
|
|
+ if (plen >= 1)
|
|
|
+ {
|
|
|
+ uint8_t temp = payload[0];
|
|
|
+ if (temp >= COM_PC_TARGET_MIN && temp <= COM_PC_TARGET_MAX)
|
|
|
+ {
|
|
|
+ if (Com_parameter_set_target(temp) == 0)
|
|
|
+ {
|
|
|
+ status = COM_PC_STATUS_OK;
|
|
|
+ printf("[PC] set target=%d OK\r\n", (int)temp);
|
|
|
+ }
|
|
|
+ else
|
|
|
+ {
|
|
|
+ printf("[PC] set target=%d EEPROM fail\r\n", (int)temp);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ else
|
|
|
+ {
|
|
|
+ printf("[PC] set target=%d out of range\r\n", (int)temp);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ flen = Com_pc_set_ack(status, frame);
|
|
|
+ if (flen > 0)
|
|
|
+ {
|
|
|
+ Com_usart1_send(frame, flen);
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+/**
|
|
|
+ * @brief 处理 0x02 请求24路温度命令: 回 0x82 快照
|
|
|
+ * @note 取 Mid_temp BMC 24 路 (CH01..CH24 = BMC[0..23]);
|
|
|
+ * 通道无效/无数据 (值为负) 补 0, 避免负值错报销 0~255 无符号。
|
|
|
+ * @param 无
|
|
|
+ * @return 无
|
|
|
+ */
|
|
|
+static void App_Pc_HandleGetTemp(void)
|
|
|
+{
|
|
|
+ uint8_t temps[COM_PC_PAYLOAD_TEMPS];
|
|
|
+ uint8_t frame[COM_PC_FRAME_MAX_LEN];
|
|
|
+ uint8_t i;
|
|
|
+ uint16_t flen;
|
|
|
+
|
|
|
+ for (i = 0; i < COM_PC_PAYLOAD_TEMPS; i++)
|
|
|
+ {
|
|
|
+ int16_t t = Mid_Temp_GetBmcTemp(i);
|
|
|
+ temps[i] = (t >= 0) ? (uint8_t)t : 0;
|
|
|
+ }
|
|
|
+
|
|
|
+ flen = Com_pc_temp_reply(temps, frame);
|
|
|
+ if (flen > 0)
|
|
|
+ {
|
|
|
+ Com_usart1_send(frame, flen);
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+/**
|
|
|
+ * @brief 轮询串口 RX, 校验并解析上位机帧
|
|
|
+ * @note 滚动扫描 |AA 55|CMD|LEN|Payload|CRC8| 帧; 命中 0x01/0x02 分发处理
|
|
|
+ * @param 无
|
|
|
+ * @return 无
|
|
|
+ */
|
|
|
+static void App_Pc_PollRx(void)
|
|
|
+{
|
|
|
+ static uint8_t s_rx[PC_RX_BUF_LEN];
|
|
|
+ static uint16_t s_len = 0;
|
|
|
+ uint16_t avail;
|
|
|
+ uint16_t pos;
|
|
|
+
|
|
|
+ avail = Com_usart1_available();
|
|
|
+ if (avail > 0)
|
|
|
+ {
|
|
|
+ uint16_t can = avail;
|
|
|
+ if (can > (uint16_t)(PC_RX_BUF_LEN - s_len))
|
|
|
+ {
|
|
|
+ can = (uint16_t)(PC_RX_BUF_LEN - s_len);
|
|
|
+ }
|
|
|
+ s_len += (uint16_t)Com_usart1_read(&s_rx[s_len], can);
|
|
|
+ }
|
|
|
+
|
|
|
+ pos = 0;
|
|
|
+ while ((uint16_t)(pos + 5) <= s_len)
|
|
|
+ {
|
|
|
+ if (s_rx[pos] == COM_PC_HEADER_H && s_rx[pos + 1] == COM_PC_HEADER_L)
|
|
|
+ {
|
|
|
+ uint16_t need = (uint16_t)(s_rx[pos + 3] + 5); /* 4(头+cmd+len+首) + payload + 1(CRC) */
|
|
|
+ uint8_t cmd;
|
|
|
+ const uint8_t *payload;
|
|
|
+ uint8_t plen;
|
|
|
+
|
|
|
+ if ((uint16_t)(s_len - pos) < need)
|
|
|
+ {
|
|
|
+ break; /* 帧未收全, 等待更多数据 */
|
|
|
+ }
|
|
|
+
|
|
|
+ if (Com_pc_check_frame(&s_rx[pos], need, &cmd, &payload, &plen))
|
|
|
+ {
|
|
|
+ if (cmd == COM_PC_CMD_SET_TARGET)
|
|
|
+ {
|
|
|
+ App_Pc_HandleSetTarget(payload, plen);
|
|
|
+ }
|
|
|
+ else if (cmd == COM_PC_CMD_GET_TEMP)
|
|
|
+ {
|
|
|
+ printf("[PC] get temp request\r\n");
|
|
|
+ App_Pc_HandleGetTemp();
|
|
|
+ }
|
|
|
+ else
|
|
|
+ {
|
|
|
+ printf("[PC] unknown cmd=0x%02X\r\n", (int)cmd);
|
|
|
+ }
|
|
|
+ pos += need;
|
|
|
+ }
|
|
|
+ else
|
|
|
+ {
|
|
|
+ pos += 1; /* 帧头合法但校验失败, 前进 1 字节 */
|
|
|
+ }
|
|
|
+ }
|
|
|
+ else
|
|
|
+ {
|
|
|
+ pos += 1;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ if (pos > 0)
|
|
|
+ {
|
|
|
+ uint16_t remain = (uint16_t)(s_len - pos);
|
|
|
+ if (remain > 0)
|
|
|
+ {
|
|
|
+ memmove(s_rx, &s_rx[pos], remain);
|
|
|
+ }
|
|
|
+ s_len = remain;
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+/**
|
|
|
+ * @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 : 任务入参 (未使用)
|
|
|
+ * @return 无 (任务不可返回)
|
|
|
+ */
|
|
|
+static void Task_PC_Process(void *argument)
|
|
|
+{
|
|
|
+ (void)argument;
|
|
|
+
|
|
|
+ printf("PC host task running...\r\n");
|
|
|
+ App_Pc_SelfTest();
|
|
|
+
|
|
|
+ while (1)
|
|
|
+ {
|
|
|
+ App_Pc_PollRx();
|
|
|
+ vTaskDelay(pdMS_TO_TICKS(PC_POLL_PERIOD_MS));
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+/**
|
|
|
+ * @brief 创建上位机通信任务
|
|
|
+ * @note 在 vTaskStartScheduler() 之前调用一次; 句柄存于 xPCTaskHandle
|
|
|
+ * @param 无
|
|
|
+ * @return 无
|
|
|
+ */
|
|
|
+void App_pc_task_create(void)
|
|
|
+{
|
|
|
+ BaseType_t ret;
|
|
|
+
|
|
|
+ ret = xTaskCreate(Task_PC_Process, "pcTask", TASK_PC_STACK_SIZE,
|
|
|
+ NULL, TASK_PC_PRIORITY, &xPCTaskHandle);
|
|
|
+ if (ret != pdPASS)
|
|
|
+ {
|
|
|
+ printf("[ERR] PC host task create failed\r\n");
|
|
|
+ }
|
|
|
+}
|