App_pc.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. /**
  2. ******************************************************************************
  3. * @file App_pc.c
  4. * @brief PC 上位机通信任务 (App 应用层)
  5. *
  6. * 上位机 (Qt/串口助手) 经 USART1(115200 8N1) 按"精简版"协议交互:
  7. * 0x01 设置目标温度: 校验 40~90 → 写 EEPROM 参数 → 回 0x81 (01成功/00失败);
  8. * 0x02 请求24路温度: 取 Mid_temp BMC 24 路快照 → 回 0x82 (无效通道补 0)。
  9. * 帧格式 |AA 55|CMD|LEN|Payload|CRC8|, 详见 Com_pc_protocol.h。
  10. * 注: 0x01 仅保存目标温度, 不自动启动预加热/PID (与 UI 的 Apply 行为一致)。
  11. ******************************************************************************
  12. */
  13. #include <stdio.h>
  14. #include <string.h>
  15. #include "App_pc.h"
  16. #include "FreeRTOS.h"
  17. #include "task.h"
  18. #include "Com_pc_protocol.h"
  19. #include "Com_pc_usart.h"
  20. #include "Com_usb_cdc.h"
  21. #include "Com_parameter.h"
  22. #include "Mid_temp.h"
  23. /* 任务参数 */
  24. #define TASK_PC_PRIORITY 2
  25. #define TASK_PC_STACK_SIZE 512
  26. /* 轮询周期: 上位机 500ms 请求一次温度, 20ms 轮询足够即时应答 */
  27. #define PC_POLL_PERIOD_MS 20
  28. /* RX 滚动缓冲 (一帧最多 COM_PC_FRAME_MAX_LEN 字节) */
  29. #define PC_RX_BUF_LEN (COM_PC_FRAME_MAX_LEN + 8)
  30. TaskHandle_t xPCTaskHandle = NULL;
  31. /**
  32. * @brief 处理 0x01 设置目标温度命令
  33. * @note 仅保存到 EEPROM 参数 (等效 UI Apply); 越界或写入失败回 0x81 FAIL
  34. * @param payload : 载荷指针 (长度 1, temp)
  35. * @param plen : 载荷长度
  36. * @return 无
  37. */
  38. static void App_Pc_HandleSetTarget(const uint8_t *payload, uint8_t plen)
  39. {
  40. uint8_t status = COM_PC_STATUS_FAIL;
  41. uint8_t frame[COM_PC_FRAME_MAX_LEN];
  42. uint16_t flen;
  43. if (plen >= 1)
  44. {
  45. uint8_t temp = payload[0];
  46. if (temp >= COM_PC_TARGET_MIN && temp <= COM_PC_TARGET_MAX)
  47. {
  48. if (Com_parameter_set_target(temp) == 0)
  49. {
  50. status = COM_PC_STATUS_OK;
  51. LOG_PRINTF("[OK] PC: set target=%d\r\n", (int)temp);
  52. }
  53. else
  54. {
  55. LOG_PRINTF("[ERR] PC: set target=%d EEPROM fail\r\n", (int)temp);
  56. }
  57. }
  58. else
  59. {
  60. LOG_PRINTF("[ERR] PC: set target=%d out of range\r\n", (int)temp);
  61. }
  62. }
  63. flen = Com_pc_set_ack(status, frame);
  64. if (flen > 0)
  65. {
  66. Com_pc_usart_send(frame, flen);
  67. }
  68. }
  69. /**
  70. * @brief 处理 0x02 请求24路温度命令: 回 0x82 快照
  71. * @note 取 Mid_temp BMC 24 路 (CH01..CH24 = BMC[0..23]);
  72. * 通道无效/无数据 (值为负) 补 0, 避免负值错报销 0~255 无符号。
  73. * @param 无
  74. * @return 无
  75. */
  76. static void App_Pc_HandleGetTemp(void)
  77. {
  78. uint8_t temps[COM_PC_PAYLOAD_TEMPS];
  79. uint8_t frame[COM_PC_FRAME_MAX_LEN];
  80. uint8_t i;
  81. uint16_t flen;
  82. for (i = 0; i < COM_PC_PAYLOAD_TEMPS; i++)
  83. {
  84. int16_t t = Mid_Temp_GetBmcTemp(i);
  85. temps[i] = (t >= 0) ? (uint8_t)t : 0;
  86. }
  87. flen = Com_pc_temp_reply(temps, frame);
  88. if (flen > 0)
  89. {
  90. Com_pc_usart_send(frame, flen);
  91. }
  92. }
  93. /**
  94. * @brief 轮询串口 RX, 校验并解析上位机帧
  95. * @note 滚动扫描 |AA 55|CMD|LEN|Payload|CRC8| 帧; 命中 0x01/0x02 分发处理
  96. * @param 无
  97. * @return 无
  98. */
  99. static void App_Pc_PollRx(void)
  100. {
  101. static uint8_t s_rx[PC_RX_BUF_LEN];
  102. static uint16_t s_len = 0;
  103. uint16_t avail;
  104. uint16_t pos;
  105. avail = Com_pc_usart_available();
  106. if (avail > 0)
  107. {
  108. uint16_t can = avail;
  109. if (can > (uint16_t)(PC_RX_BUF_LEN - s_len))
  110. {
  111. can = (uint16_t)(PC_RX_BUF_LEN - s_len);
  112. }
  113. s_len += (uint16_t)Com_pc_usart_read(&s_rx[s_len], can);
  114. }
  115. pos = 0;
  116. while ((uint16_t)(pos + 5) <= s_len)
  117. {
  118. if (s_rx[pos] == COM_PC_HEADER_H && s_rx[pos + 1] == COM_PC_HEADER_L)
  119. {
  120. uint16_t need = (uint16_t)(s_rx[pos + 3] + 5); /* 4(头+cmd+len+首) + payload + 1(CRC) */
  121. uint8_t cmd;
  122. const uint8_t *payload;
  123. uint8_t plen;
  124. if ((uint16_t)(s_len - pos) < need)
  125. {
  126. break; /* 帧未收全, 等待更多数据 */
  127. }
  128. if (Com_pc_check_frame(&s_rx[pos], need, &cmd, &payload, &plen))
  129. {
  130. if (cmd == COM_PC_CMD_SET_TARGET)
  131. {
  132. App_Pc_HandleSetTarget(payload, plen);
  133. }
  134. else if (cmd == COM_PC_CMD_GET_TEMP)
  135. {
  136. LOG_PRINTF("[INFO] PC: get temp request\r\n");
  137. App_Pc_HandleGetTemp();
  138. }
  139. else
  140. {
  141. LOG_PRINTF("[ERR] PC: unknown cmd=0x%02X\r\n", (int)cmd);
  142. }
  143. pos += need;
  144. }
  145. else
  146. {
  147. pos += 1; /* 帧头合法但校验失败, 前进 1 字节 */
  148. }
  149. }
  150. else
  151. {
  152. pos += 1;
  153. }
  154. }
  155. if (pos > 0)
  156. {
  157. uint16_t remain = (uint16_t)(s_len - pos);
  158. if (remain > 0)
  159. {
  160. memmove(s_rx, &s_rx[pos], remain);
  161. }
  162. s_len = remain;
  163. }
  164. }
  165. /**
  166. * @brief 上位机通信任务体: 周期轮询 USART1 RX 并应答
  167. * @param argument : 任务入参 (未使用)
  168. * @return 无 (任务不可返回)
  169. */
  170. static void Task_PC_Process(void *argument)
  171. {
  172. (void)argument;
  173. LOG_PRINTF("[INFO] PC: task running\r\n");
  174. while (1)
  175. {
  176. App_Pc_PollRx();
  177. vTaskDelay(pdMS_TO_TICKS(PC_POLL_PERIOD_MS));
  178. }
  179. }
  180. /**
  181. * @brief 创建上位机通信任务
  182. * @note 在 vTaskStartScheduler() 之前调用一次; 句柄存于 xPCTaskHandle
  183. * @param 无
  184. * @return 无
  185. */
  186. void App_pc_task_create(void)
  187. {
  188. BaseType_t ret;
  189. ret = xTaskCreate(Task_PC_Process, "pcTask", TASK_PC_STACK_SIZE,
  190. NULL, TASK_PC_PRIORITY, &xPCTaskHandle);
  191. if (ret != pdPASS)
  192. {
  193. LOG_PRINTF("[ERR] PC: task create failed\r\n");
  194. }
  195. }