App_cpld.c 8.0 KB

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