|
@@ -5,16 +5,21 @@
|
|
|
*
|
|
*
|
|
|
* 主机角色: 周期组装 SET_ALL(0x01) 帧并经 USART3(921600) 下发,
|
|
* 主机角色: 周期组装 SET_ALL(0x01) 帧并经 USART3(921600) 下发,
|
|
|
* payload 取 Mid 层共享输出缓冲 (24 PTC + 8 FAN)。
|
|
* payload 取 Mid 层共享输出缓冲 (24 PTC + 8 FAN)。
|
|
|
|
|
+ * 同时周期轮询 GET_SNAPSHOT(0x02), 解析回传的 0x82 快照帧,
|
|
|
|
|
+ * 刷新 Mid_temp 温度缓存 (BMC 24 路 / CT7428 27 路),
|
|
|
|
|
+ * 并将全部遥测字段经 USB 虚拟串口 (printf → CDC) 打印。
|
|
|
* 注: SET_ALL 同时作为 30s 看门狗喂狗报文。
|
|
* 注: SET_ALL 同时作为 30s 看门狗喂狗报文。
|
|
|
******************************************************************************
|
|
******************************************************************************
|
|
|
*/
|
|
*/
|
|
|
#include <stdio.h>
|
|
#include <stdio.h>
|
|
|
|
|
+#include <string.h>
|
|
|
#include "App_cpld.h"
|
|
#include "App_cpld.h"
|
|
|
#include "FreeRTOS.h"
|
|
#include "FreeRTOS.h"
|
|
|
#include "task.h"
|
|
#include "task.h"
|
|
|
#include "Com_protocol.h"
|
|
#include "Com_protocol.h"
|
|
|
#include "Com_usart3.h"
|
|
#include "Com_usart3.h"
|
|
|
#include "Mid_pid.h"
|
|
#include "Mid_pid.h"
|
|
|
|
|
+#include "Mid_temp.h"
|
|
|
|
|
|
|
|
/* 任务参数 (数字越大优先级越高) */
|
|
/* 任务参数 (数字越大优先级越高) */
|
|
|
#define TASK_CPLD_PRIORITY 4
|
|
#define TASK_CPLD_PRIORITY 4
|
|
@@ -23,11 +28,164 @@
|
|
|
/* CPLD 控制周期: 1s 定期下发; 远小于 CPLD 30s 无通讯超时 */
|
|
/* CPLD 控制周期: 1s 定期下发; 远小于 CPLD 30s 无通讯超时 */
|
|
|
#define CPLD_SEND_PERIOD_MS 1000
|
|
#define CPLD_SEND_PERIOD_MS 1000
|
|
|
|
|
|
|
|
|
|
+/* RX 滚动缓冲 (一帧最多 COM_PROTOCOL_FRAME_MAX_LEN 字节) */
|
|
|
|
|
+#define CPLD_RX_BUF_LEN (COM_PROTOCOL_FRAME_MAX_LEN + 8)
|
|
|
|
|
+
|
|
|
TaskHandle_t xCPLDProcessTaskHandle = NULL;
|
|
TaskHandle_t xCPLDProcessTaskHandle = NULL;
|
|
|
|
|
|
|
|
/**
|
|
/**
|
|
|
- * @brief CPLD 通信任务体: 周期组装并下发 SET_ALL 帧
|
|
|
|
|
- * @note 在任务上下文完成 Mid_PID_Init() (软复位后依赖 PID 数据表初始化);
|
|
|
|
|
|
|
+ * @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;
|
|
|
|
|
+
|
|
|
|
|
+ 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);
|
|
|
|
|
+
|
|
|
|
|
+ printf("[BMC 0-11] ");
|
|
|
|
|
+ for (i = 0; i < 12; i++)
|
|
|
|
|
+ {
|
|
|
|
|
+ printf("%d=%d ", (int)i, (int)s->bmc[i]);
|
|
|
|
|
+ }
|
|
|
|
|
+ printf("\r\n[BMC 12-23] ");
|
|
|
|
|
+ for (i = 12; i < 24; i++)
|
|
|
|
|
+ {
|
|
|
|
|
+ printf("%d=%d ", (int)i, (int)s->bmc[i]);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ printf("\r\n[CT 0-13] ");
|
|
|
|
|
+ for (i = 0; i < 14; i++)
|
|
|
|
|
+ {
|
|
|
|
|
+ printf("%d=%d ", (int)i, (int)s->ct[i]);
|
|
|
|
|
+ }
|
|
|
|
|
+ printf("\r\n[CT 14-26] ");
|
|
|
|
|
+ for (i = 14; i < 27; i++)
|
|
|
|
|
+ {
|
|
|
|
|
+ printf("%d=%d ", (int)i, (int)s->ct[i]);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ printf("\r\n[PTC] req:");
|
|
|
|
|
+ for (i = 0; i < 24; i++)
|
|
|
|
|
+ {
|
|
|
|
|
+ printf(" %d", (int)s->ptc_requested[i]);
|
|
|
|
|
+ }
|
|
|
|
|
+ printf("\r\n app:");
|
|
|
|
|
+ for (i = 0; i < 24; i++)
|
|
|
|
|
+ {
|
|
|
|
|
+ printf(" %d", (int)s->ptc_applied[i]);
|
|
|
|
|
+ }
|
|
|
|
|
+ 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_usart3_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_usart3_read(&s_rx[s_len], avail);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ pos = 0;
|
|
|
|
|
+ while ((uint16_t)(pos + 6) <= s_len)
|
|
|
|
|
+ {
|
|
|
|
|
+ if (s_rx[pos] == COM_PROTOCOL_HEADER_H && s_rx[pos + 1] == COM_PROTOCOL_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_protocol_check_frame(&s_rx[pos], need, &type, &payload, &plen))
|
|
|
|
|
+ {
|
|
|
|
|
+ if (type == COM_PROTOCOL_TYPE_SNAPSHOT_DATA)
|
|
|
|
|
+ {
|
|
|
|
|
+ if (Mid_Temp_UpdateSnapshot(payload, plen))
|
|
|
|
|
+ {
|
|
|
|
|
+ App_Cpld_DumpSnapshot();
|
|
|
|
|
+ }
|
|
|
|
|
+ else
|
|
|
|
|
+ {
|
|
|
|
|
+ printf("[ERR] snapshot len=%d\r\n", (int)plen);
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ else if (type == COM_PROTOCOL_TYPE_ACK_NACK)
|
|
|
|
|
+ {
|
|
|
|
|
+ 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 通信任务体: 周期下发 SET_ALL 并轮询快照
|
|
|
|
|
+ * @note 在任务上下文完成 Mid_PID_Init()/Mid_Temp_Init() (软复位后依赖);
|
|
|
* SET_ALL 兼作 CPLD 看门狗喂狗报文 (30s 超时)。
|
|
* SET_ALL 兼作 CPLD 看门狗喂狗报文 (30s 超时)。
|
|
|
* @param argument : 任务入参 (未使用)
|
|
* @param argument : 任务入参 (未使用)
|
|
|
* @return 无 (任务不可返回)
|
|
* @return 无 (任务不可返回)
|
|
@@ -41,6 +199,7 @@ static void Task_CPLD_Process(void *argument)
|
|
|
printf("CPLD process task running...\r\n");
|
|
printf("CPLD process task running...\r\n");
|
|
|
|
|
|
|
|
Mid_PID_Init();
|
|
Mid_PID_Init();
|
|
|
|
|
+ Mid_Temp_Init();
|
|
|
|
|
|
|
|
while (1)
|
|
while (1)
|
|
|
{
|
|
{
|
|
@@ -49,6 +208,15 @@ static void Task_CPLD_Process(void *argument)
|
|
|
{
|
|
{
|
|
|
Com_usart3_send(frame, flen);
|
|
Com_usart3_send(frame, flen);
|
|
|
}
|
|
}
|
|
|
|
|
+
|
|
|
|
|
+ flen = Com_protocol_get_snapshot(frame);
|
|
|
|
|
+ if (flen > 0)
|
|
|
|
|
+ {
|
|
|
|
|
+ Com_usart3_send(frame, flen);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ App_Cpld_PollRx();
|
|
|
|
|
+
|
|
|
vTaskDelay(pdMS_TO_TICKS(CPLD_SEND_PERIOD_MS));
|
|
vTaskDelay(pdMS_TO_TICKS(CPLD_SEND_PERIOD_MS));
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|