App_cpld.c 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  1. /**
  2. ******************************************************************************
  3. * @file App_cpld.c
  4. * @brief CPLD 通信任务 (App 应用层)
  5. *
  6. * 主机角色: 周期组装 SET_ALL(0x01) 帧并经 USART3(921600) 下发,
  7. * payload 取 Mid 层共享输出缓冲 (24 PTC + 8 FAN)。
  8. * 同时周期轮询 GET_SNAPSHOT(0x02), 解析回传的 0x82 快照帧,
  9. * 刷新 Mid_temp 温度缓存 (BMC 24 路 / CT7428 27 路),
  10. * 并将全部遥测字段经 USB 虚拟串口 (printf → CDC) 打印。
  11. * 注: SET_ALL 同时作为 30s 看门狗喂狗报文。
  12. ******************************************************************************
  13. */
  14. #include <stdio.h>
  15. #include <string.h>
  16. #include "App_cpld.h"
  17. #include "FreeRTOS.h"
  18. #include "task.h"
  19. #include "Com_cpld_protocol.h"
  20. #include "Com_cpld_usart.h"
  21. #include "Com_usb_cdc.h"
  22. #include "Mid_pid.h"
  23. #include "Mid_temp.h"
  24. #include "App_guardian.h"
  25. /* 任务参数 (数字越大优先级越高) */
  26. #define TASK_CPLD_PRIORITY 4
  27. #define TASK_CPLD_STACK_SIZE 512
  28. /* CPLD 控制周期: 1s 定期下发; 远小于 CPLD 30s 无通讯超时 */
  29. #define CPLD_SEND_PERIOD_MS 1000
  30. /* RX 滚动缓冲 (一帧最多 COM_CPLD_FRAME_MAX_LEN 字节) */
  31. #define CPLD_RX_BUF_LEN (COM_CPLD_FRAME_MAX_LEN + 8)
  32. /* 通讯存活判定窗口: 超过该时长未收到任何有效帧视为失联 */
  33. #define APP_CPLD_COMM_TIMEOUT_MS 4000
  34. TaskHandle_t xCPLDProcessTaskHandle = NULL;
  35. /* 最近一次收到 CPLD 有效帧的 tick (0 = 从未收到) */
  36. static volatile uint32_t s_last_rx_ticks = 0;
  37. /**
  38. * @brief 解析刚收到的快照并打印全部遥测字段
  39. * @note 仅打印, 数据已由 Mid_Temp_UpdateSnapshot 缓存
  40. * @param 无
  41. * @return 无
  42. */
  43. static void App_Cpld_DumpSnapshot(void)
  44. {
  45. const mid_temp_snapshot_t *s = Mid_Temp_GetSnapshot();
  46. uint8_t i;
  47. TELEMETRY_PRINTF("[SNAP] mode=%d fault=%04X wd=%d/%d manual=%d "
  48. "bmc(v%d st%d) ct(v%d st%d) hardprot=%d "
  49. "ep(bmc=%u ct=%u ctrl=%u) age(bmc=%u ct=%u)\r\n",
  50. (int)s->mode, (int)s->fault_flags,
  51. (int)s->watchdog_active, (int)s->watchdog_remaining,
  52. (int)s->manual_mode,
  53. (int)s->bmc_valid, (int)s->bmc_stale,
  54. (int)s->ct_valid, (int)s->ct_stale,
  55. (int)s->hard_protect,
  56. (unsigned)s->bmc_epoch, (unsigned)s->ct_epoch,
  57. (unsigned)s->control_epoch,
  58. (unsigned)s->bmc_age, (unsigned)s->ct_age);
  59. TELEMETRY_PRINTF("[BMC 0-11] ");
  60. for (i = 0; i < 12; i++)
  61. {
  62. TELEMETRY_PRINTF("%d=%d ", (int)i, (int)s->bmc[i]);
  63. }
  64. TELEMETRY_PRINTF("\r\n[BMC 12-23] ");
  65. for (i = 12; i < 24; i++)
  66. {
  67. TELEMETRY_PRINTF("%d=%d ", (int)i, (int)s->bmc[i]);
  68. }
  69. TELEMETRY_PRINTF("\r\n[CT 0-13] ");
  70. for (i = 0; i < 14; i++)
  71. {
  72. TELEMETRY_PRINTF("%d=%d ", (int)i, (int)s->ct[i]);
  73. }
  74. TELEMETRY_PRINTF("\r\n[CT 14-26] ");
  75. for (i = 14; i < 27; i++)
  76. {
  77. TELEMETRY_PRINTF("%d=%d ", (int)i, (int)s->ct[i]);
  78. }
  79. TELEMETRY_PRINTF("\r\n[PTC] req:");
  80. for (i = 0; i < 24; i++)
  81. {
  82. TELEMETRY_PRINTF(" %d", (int)s->ptc_requested[i]);
  83. }
  84. TELEMETRY_PRINTF("\r\n app:");
  85. for (i = 0; i < 24; i++)
  86. {
  87. TELEMETRY_PRINTF(" %d", (int)s->ptc_applied[i]);
  88. }
  89. TELEMETRY_PRINTF("\r\n[FAN] front req/app: %d/%d %d/%d %d/%d %d/%d | "
  90. "rear req/app: %d/%d %d/%d %d/%d %d/%d\r\n",
  91. (int)s->front_requested[0], (int)s->front_applied[0],
  92. (int)s->front_requested[1], (int)s->front_applied[1],
  93. (int)s->front_requested[2], (int)s->front_applied[2],
  94. (int)s->front_requested[3], (int)s->front_applied[3],
  95. (int)s->rear_requested[0], (int)s->rear_applied[0],
  96. (int)s->rear_requested[1], (int)s->rear_applied[1],
  97. (int)s->rear_requested[2], (int)s->rear_applied[2],
  98. (int)s->rear_requested[3], (int)s->rear_applied[3]);
  99. }
  100. /**
  101. * @brief 轮询串口 RX, 校验并解析 CPLD 回传帧
  102. * @note 滚动扫描 |A5 5A|Type|Length|Payload|CRC| 帧; 命中 0x82 快照后
  103. * 刷新温度缓存并打印; 0x06 ACK 仅打印状态码。
  104. * @param 无
  105. * @return 无
  106. */
  107. static void App_Cpld_PollRx(void)
  108. {
  109. static uint8_t s_rx[CPLD_RX_BUF_LEN];
  110. static uint16_t s_len = 0;
  111. uint16_t avail;
  112. uint16_t pos;
  113. avail = Com_cpld_usart_available();
  114. if (avail > 0)
  115. {
  116. if (avail > (uint16_t)(CPLD_RX_BUF_LEN - s_len))
  117. {
  118. avail = (uint16_t)(CPLD_RX_BUF_LEN - s_len);
  119. }
  120. s_len += (uint16_t)Com_cpld_usart_read(&s_rx[s_len], avail);
  121. }
  122. pos = 0;
  123. while ((uint16_t)(pos + 6) <= s_len)
  124. {
  125. if (s_rx[pos] == COM_CPLD_HEADER_H && s_rx[pos + 1] == COM_CPLD_HEADER_L)
  126. {
  127. uint16_t need = (uint16_t)(s_rx[pos + 3] + 6); /* 3(头+type+len) + payload + 2(CRC) */
  128. uint8_t type;
  129. const uint8_t *payload;
  130. uint8_t plen;
  131. if ((uint16_t)(s_len - pos) < need)
  132. {
  133. break; /* 帧未收全, 等待更多数据 */
  134. }
  135. if (Com_cpld_check_frame(&s_rx[pos], need, &type, &payload, &plen))
  136. {
  137. s_last_rx_ticks = xTaskGetTickCount();
  138. if (type == COM_CPLD_TYPE_SNAPSHOT_DATA)
  139. {
  140. if (Mid_Temp_UpdateSnapshot(payload, plen))
  141. {
  142. App_Cpld_DumpSnapshot();
  143. }
  144. else
  145. {
  146. LOG_PRINTF("[ERR] CPLD: snapshot len=%d\r\n", (int)plen);
  147. }
  148. }
  149. else if (type == COM_CPLD_TYPE_ACK_NACK)
  150. {
  151. TELEMETRY_PRINTF("[ACK] ok=%d req=0x%02X st=%d detail=%d\r\n",
  152. (int)payload[0], (int)payload[1],
  153. (int)payload[2], (int)payload[3]);
  154. }
  155. pos += need;
  156. }
  157. else
  158. {
  159. pos += 1; /* 帧头合法但校验失败, 前进 1 字节 */
  160. }
  161. }
  162. else
  163. {
  164. pos += 1;
  165. }
  166. }
  167. if (pos > 0)
  168. {
  169. uint16_t remain = (uint16_t)(s_len - pos);
  170. if (remain > 0)
  171. {
  172. memmove(s_rx, &s_rx[pos], remain);
  173. }
  174. s_len = remain;
  175. }
  176. }
  177. /**
  178. * @brief 查询 CPLD 通讯是否存活
  179. * @note 最近 APP_CPLD_COMM_TIMEOUT_MS 内收到过任何 CPLD 有效帧
  180. * (ACK/SNAPSHOT) 视为存活; 从未收到返回 0。
  181. * @param 无
  182. * @return 1=通讯正常, 0=未接通/失联
  183. */
  184. uint8_t App_cpld_comm_alive(void)
  185. {
  186. if (s_last_rx_ticks == 0)
  187. {
  188. return 0;
  189. }
  190. return ((uint32_t)(xTaskGetTickCount() - s_last_rx_ticks) <
  191. (uint32_t)pdMS_TO_TICKS(APP_CPLD_COMM_TIMEOUT_MS)) ? 1 : 0;
  192. }
  193. /**
  194. * @brief CPLD 通信任务体: 周期下发 SET_ALL 并轮询快照
  195. * @note 在任务上下文完成 Mid_PID_Init()/Mid_Temp_Init() (软复位后依赖);
  196. * SET_ALL 兼作 CPLD 看门狗喂狗报文 (30s 超时)。
  197. * @param argument : 任务入参 (未使用)
  198. * @return 无 (任务不可返回)
  199. */
  200. static void Task_CPLD_Process(void *argument)
  201. {
  202. (void)argument;
  203. uint8_t frame[COM_CPLD_FRAME_MAX_LEN];
  204. uint16_t flen;
  205. LOG_PRINTF("[INFO] CPLD: task running\r\n");
  206. Mid_PID_Init();
  207. Mid_Temp_Init();
  208. while (1)
  209. {
  210. App_guardian_feed(GUARD_ID_CPLD);
  211. /* 读输出缓冲+封装 SET_ALL 须在同一临界区, 防止读到 PID/预热
  212. * 任务批量写入到一半的缓冲; 发送 (可能阻塞) 在锁外进行 */
  213. Mid_PID_LockOutput();
  214. flen = Com_cpld_set_all(Mid_PID_GetOutputDuty(), frame);
  215. Mid_PID_UnlockOutput();
  216. if (flen > 0)
  217. {
  218. Com_cpld_usart_send(frame, flen);
  219. }
  220. flen = Com_cpld_get_snapshot(frame);
  221. if (flen > 0)
  222. {
  223. Com_cpld_usart_send(frame, flen);
  224. }
  225. App_Cpld_PollRx();
  226. vTaskDelay(pdMS_TO_TICKS(CPLD_SEND_PERIOD_MS));
  227. }
  228. }
  229. /**
  230. * @brief 创建 CPLD 通信任务
  231. * @note 在 vTaskStartScheduler() 之前调用一次; 句柄存于 xCPLDProcessTaskHandle。
  232. * @param 无
  233. * @return 无
  234. */
  235. void App_cpld_task_create(void)
  236. {
  237. BaseType_t ret;
  238. ret = xTaskCreate(Task_CPLD_Process, "cpldProcessTask", TASK_CPLD_STACK_SIZE,
  239. NULL, TASK_CPLD_PRIORITY, &xCPLDProcessTaskHandle);
  240. if (ret != pdPASS)
  241. {
  242. LOG_PRINTF("[ERR] CPLD: task create failed\r\n");
  243. }
  244. }