| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273 |
- /**
- ******************************************************************************
- * @file App_cpld.c
- * @brief CPLD 通信任务 (App 应用层)
- *
- * 主机角色: 周期组装 SET_ALL(0x01) 帧并经 USART3(921600) 下发,
- * payload 取 Mid 层共享输出缓冲 (24 PTC + 8 FAN)。
- * 同时周期轮询 GET_SNAPSHOT(0x02), 解析回传的 0x82 快照帧,
- * 刷新 Mid_temp 温度缓存 (BMC 24 路 / CT7428 27 路),
- * 并将全部遥测字段经 USB 虚拟串口 (printf → CDC) 打印。
- * 注: SET_ALL 同时作为 30s 看门狗喂狗报文。
- ******************************************************************************
- */
- #include <stdio.h>
- #include <string.h>
- #include "App_cpld.h"
- #include "FreeRTOS.h"
- #include "task.h"
- #include "Com_cpld_protocol.h"
- #include "Com_cpld_usart.h"
- #include "Com_usb_cdc.h"
- #include "Mid_pid.h"
- #include "Mid_temp.h"
- #include "App_guardian.h"
- /* 任务参数 (数字越大优先级越高) */
- #define TASK_CPLD_PRIORITY 4
- #define TASK_CPLD_STACK_SIZE 512
- /* CPLD 控制周期: 1s 定期下发; 远小于 CPLD 30s 无通讯超时 */
- #define CPLD_SEND_PERIOD_MS 1000
- /* RX 滚动缓冲 (一帧最多 COM_CPLD_FRAME_MAX_LEN 字节) */
- #define CPLD_RX_BUF_LEN (COM_CPLD_FRAME_MAX_LEN + 8)
- /* 通讯存活判定窗口: 超过该时长未收到任何有效帧视为失联 */
- #define APP_CPLD_COMM_TIMEOUT_MS 4000
- TaskHandle_t xCPLDProcessTaskHandle = NULL;
- /* 最近一次收到 CPLD 有效帧的 tick (0 = 从未收到) */
- static volatile uint32_t s_last_rx_ticks = 0;
- /**
- * @brief 解析刚收到的快照并打印全部遥测字段
- * @note 仅打印, 数据已由 Mid_Temp_UpdateSnapshot 缓存
- * @param 无
- * @return 无
- */
- static void App_Cpld_DumpSnapshot(void)
- {
- const mid_temp_snapshot_t *s = Mid_Temp_GetSnapshot();
- uint8_t i;
- TELEMETRY_PRINTF("[SNAP] mode=%d fault=%04X wd=%d/%d manual=%d "
- "bmc(v%d st%d) ct(v%d st%d) hardprot=%d "
- "ep(bmc=%u ct=%u ctrl=%u) age(bmc=%u ct=%u)\r\n",
- (int)s->mode, (int)s->fault_flags,
- (int)s->watchdog_active, (int)s->watchdog_remaining,
- (int)s->manual_mode,
- (int)s->bmc_valid, (int)s->bmc_stale,
- (int)s->ct_valid, (int)s->ct_stale,
- (int)s->hard_protect,
- (unsigned)s->bmc_epoch, (unsigned)s->ct_epoch,
- (unsigned)s->control_epoch,
- (unsigned)s->bmc_age, (unsigned)s->ct_age);
- TELEMETRY_PRINTF("[BMC 0-11] ");
- for (i = 0; i < 12; i++)
- {
- TELEMETRY_PRINTF("%d=%d ", (int)i, (int)s->bmc[i]);
- }
- TELEMETRY_PRINTF("\r\n[BMC 12-23] ");
- for (i = 12; i < 24; i++)
- {
- TELEMETRY_PRINTF("%d=%d ", (int)i, (int)s->bmc[i]);
- }
- TELEMETRY_PRINTF("\r\n[CT 0-13] ");
- for (i = 0; i < 14; i++)
- {
- TELEMETRY_PRINTF("%d=%d ", (int)i, (int)s->ct[i]);
- }
- TELEMETRY_PRINTF("\r\n[CT 14-26] ");
- for (i = 14; i < 27; i++)
- {
- TELEMETRY_PRINTF("%d=%d ", (int)i, (int)s->ct[i]);
- }
- TELEMETRY_PRINTF("\r\n[PTC] req:");
- for (i = 0; i < 24; i++)
- {
- TELEMETRY_PRINTF(" %d", (int)s->ptc_requested[i]);
- }
- TELEMETRY_PRINTF("\r\n app:");
- for (i = 0; i < 24; i++)
- {
- TELEMETRY_PRINTF(" %d", (int)s->ptc_applied[i]);
- }
- TELEMETRY_PRINTF("\r\n[FAN] front req/app: %d/%d %d/%d %d/%d %d/%d | "
- "rear req/app: %d/%d %d/%d %d/%d %d/%d\r\n",
- (int)s->front_requested[0], (int)s->front_applied[0],
- (int)s->front_requested[1], (int)s->front_applied[1],
- (int)s->front_requested[2], (int)s->front_applied[2],
- (int)s->front_requested[3], (int)s->front_applied[3],
- (int)s->rear_requested[0], (int)s->rear_applied[0],
- (int)s->rear_requested[1], (int)s->rear_applied[1],
- (int)s->rear_requested[2], (int)s->rear_applied[2],
- (int)s->rear_requested[3], (int)s->rear_applied[3]);
- }
- /**
- * @brief 轮询串口 RX, 校验并解析 CPLD 回传帧
- * @note 滚动扫描 |A5 5A|Type|Length|Payload|CRC| 帧; 命中 0x82 快照后
- * 刷新温度缓存并打印; 0x06 ACK 仅打印状态码。
- * @param 无
- * @return 无
- */
- static void App_Cpld_PollRx(void)
- {
- static uint8_t s_rx[CPLD_RX_BUF_LEN];
- static uint16_t s_len = 0;
- uint16_t avail;
- uint16_t pos;
- avail = Com_cpld_usart_available();
- if (avail > 0)
- {
- if (avail > (uint16_t)(CPLD_RX_BUF_LEN - s_len))
- {
- avail = (uint16_t)(CPLD_RX_BUF_LEN - s_len);
- }
- s_len += (uint16_t)Com_cpld_usart_read(&s_rx[s_len], avail);
- }
- pos = 0;
- while ((uint16_t)(pos + 6) <= s_len)
- {
- if (s_rx[pos] == COM_CPLD_HEADER_H && s_rx[pos + 1] == COM_CPLD_HEADER_L)
- {
- uint16_t need = (uint16_t)(s_rx[pos + 3] + 6); /* 3(头+type+len) + payload + 2(CRC) */
- uint8_t type;
- const uint8_t *payload;
- uint8_t plen;
- if ((uint16_t)(s_len - pos) < need)
- {
- break; /* 帧未收全, 等待更多数据 */
- }
- if (Com_cpld_check_frame(&s_rx[pos], need, &type, &payload, &plen))
- {
- s_last_rx_ticks = xTaskGetTickCount();
- if (type == COM_CPLD_TYPE_SNAPSHOT_DATA)
- {
- if (Mid_Temp_UpdateSnapshot(payload, plen))
- {
- App_Cpld_DumpSnapshot();
- }
- else
- {
- LOG_PRINTF("[ERR] CPLD: snapshot len=%d\r\n", (int)plen);
- }
- }
- else if (type == COM_CPLD_TYPE_ACK_NACK)
- {
- TELEMETRY_PRINTF("[ACK] ok=%d req=0x%02X st=%d detail=%d\r\n",
- (int)payload[0], (int)payload[1],
- (int)payload[2], (int)payload[3]);
- }
- 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 查询 CPLD 通讯是否存活
- * @note 最近 APP_CPLD_COMM_TIMEOUT_MS 内收到过任何 CPLD 有效帧
- * (ACK/SNAPSHOT) 视为存活; 从未收到返回 0。
- * @param 无
- * @return 1=通讯正常, 0=未接通/失联
- */
- uint8_t App_cpld_comm_alive(void)
- {
- if (s_last_rx_ticks == 0)
- {
- return 0;
- }
- return ((uint32_t)(xTaskGetTickCount() - s_last_rx_ticks) <
- (uint32_t)pdMS_TO_TICKS(APP_CPLD_COMM_TIMEOUT_MS)) ? 1 : 0;
- }
- /**
- * @brief CPLD 通信任务体: 周期下发 SET_ALL 并轮询快照
- * @note 在任务上下文完成 Mid_PID_Init()/Mid_Temp_Init() (软复位后依赖);
- * SET_ALL 兼作 CPLD 看门狗喂狗报文 (30s 超时)。
- * @param argument : 任务入参 (未使用)
- * @return 无 (任务不可返回)
- */
- static void Task_CPLD_Process(void *argument)
- {
- (void)argument;
- uint8_t frame[COM_CPLD_FRAME_MAX_LEN];
- uint16_t flen;
- LOG_PRINTF("[INFO] CPLD: task running\r\n");
- Mid_PID_Init();
- Mid_Temp_Init();
- while (1)
- {
- App_guardian_feed(GUARD_ID_CPLD);
- /* 读输出缓冲+封装 SET_ALL 须在同一临界区, 防止读到 PID/预热
- * 任务批量写入到一半的缓冲; 发送 (可能阻塞) 在锁外进行 */
- Mid_PID_LockOutput();
- flen = Com_cpld_set_all(Mid_PID_GetOutputDuty(), frame);
- Mid_PID_UnlockOutput();
- if (flen > 0)
- {
- Com_cpld_usart_send(frame, flen);
- }
- flen = Com_cpld_get_snapshot(frame);
- if (flen > 0)
- {
- Com_cpld_usart_send(frame, flen);
- }
- App_Cpld_PollRx();
- vTaskDelay(pdMS_TO_TICKS(CPLD_SEND_PERIOD_MS));
- }
- }
- /**
- * @brief 创建 CPLD 通信任务
- * @note 在 vTaskStartScheduler() 之前调用一次; 句柄存于 xCPLDProcessTaskHandle。
- * @param 无
- * @return 无
- */
- void App_cpld_task_create(void)
- {
- BaseType_t ret;
- ret = xTaskCreate(Task_CPLD_Process, "cpldProcessTask", TASK_CPLD_STACK_SIZE,
- NULL, TASK_CPLD_PRIORITY, &xCPLDProcessTaskHandle);
- if (ret != pdPASS)
- {
- LOG_PRINTF("[ERR] CPLD: task create failed\r\n");
- }
- }
|