|
@@ -5,10 +5,14 @@
|
|
|
*
|
|
*
|
|
|
* 主机角色: 周期组装 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 快照帧,
|
|
|
|
|
|
|
+ * 按 v2.0 §20 停等节奏工作: 发 SET_ALL → 等 SET_ACK/NACK →
|
|
|
|
|
+ * 发 GET_ALL → 等 GET_ACK + GET_DATA (0x42, 83B),
|
|
|
* 刷新 Mid_temp 温度缓存 (BMC 24 路 / CT7428 27 路),
|
|
* 刷新 Mid_temp 温度缓存 (BMC 24 路 / CT7428 27 路),
|
|
|
* 并将全部遥测字段经 USB 虚拟串口 (printf → CDC) 打印。
|
|
* 并将全部遥测字段经 USB 虚拟串口 (printf → CDC) 打印。
|
|
|
* 注: SET_ALL 同时作为 30s 看门狗喂狗报文。
|
|
* 注: SET_ALL 同时作为 30s 看门狗喂狗报文。
|
|
|
|
|
+ * 流恢复 (v2.0 §7.4): 帧头+TYPE+LENGTH 一旦接受为候选帧, 即使 CRC 错也
|
|
|
|
|
+ * 按声明 LENGTH 整帧消费后再重新找头, 防止 payload 内的
|
|
|
|
|
+ * A5 5A 被误认为新帧。
|
|
|
******************************************************************************
|
|
******************************************************************************
|
|
|
*/
|
|
*/
|
|
|
#include <stdio.h>
|
|
#include <stdio.h>
|
|
@@ -27,102 +31,123 @@
|
|
|
#define TASK_CPLD_PRIORITY 4
|
|
#define TASK_CPLD_PRIORITY 4
|
|
|
#define TASK_CPLD_STACK_SIZE 512
|
|
#define TASK_CPLD_STACK_SIZE 512
|
|
|
|
|
|
|
|
-/* CPLD 控制周期: 1s 定期下发; 远小于 CPLD 30s 无通讯超时 */
|
|
|
|
|
|
|
+/* CPLD 控制周期: 1s 一轮 SET→GET; 远小于 CPLD 30s 无通讯超时 */
|
|
|
#define CPLD_SEND_PERIOD_MS 1000
|
|
#define CPLD_SEND_PERIOD_MS 1000
|
|
|
|
|
|
|
|
-/* RX 滚动缓冲 (一帧最多 COM_CPLD_FRAME_MAX_LEN 字节) */
|
|
|
|
|
-#define CPLD_RX_BUF_LEN (COM_CPLD_FRAME_MAX_LEN + 8)
|
|
|
|
|
|
|
+/* 单步停等超时: 等待 SET_ACK / GET_DATA 的时间上限 (ms) */
|
|
|
|
|
+#define CPLD_WAIT_ACK_MS 200
|
|
|
|
|
+
|
|
|
|
|
+/* RX 滚动缓冲 (一帧最多 COM_CPLD_FRAME_MAX_LEN 字节; 256 有余量) */
|
|
|
|
|
+#define CPLD_RX_BUF_LEN (COM_CPLD_FRAME_MAX_LEN + 256)
|
|
|
|
|
|
|
|
/* 通讯存活判定窗口: 超过该时长未收到任何有效帧视为失联 */
|
|
/* 通讯存活判定窗口: 超过该时长未收到任何有效帧视为失联 */
|
|
|
#define APP_CPLD_COMM_TIMEOUT_MS 4000
|
|
#define APP_CPLD_COMM_TIMEOUT_MS 4000
|
|
|
|
|
|
|
|
|
|
+/* 停等状态 */
|
|
|
|
|
+typedef enum {
|
|
|
|
|
+ CPLD_SEQ_IDLE = 0, /* 周期起点: 发 SET_ALL */
|
|
|
|
|
+ CPLD_SEQ_WAIT_SET_ACK, /* 等 SET_ACK / NACK */
|
|
|
|
|
+ CPLD_SEQ_SEND_GET, /* 发 GET_ALL */
|
|
|
|
|
+ CPLD_SEQ_WAIT_GET_DATA /* 等 GET_ACK(+GET_DATA) / NACK */
|
|
|
|
|
+} cpld_seq_t;
|
|
|
|
|
+
|
|
|
TaskHandle_t xCPLDProcessTaskHandle = NULL;
|
|
TaskHandle_t xCPLDProcessTaskHandle = NULL;
|
|
|
|
|
|
|
|
/* 最近一次收到 CPLD 有效帧的 tick (0 = 从未收到) */
|
|
/* 最近一次收到 CPLD 有效帧的 tick (0 = 从未收到) */
|
|
|
static volatile uint32_t s_last_rx_ticks = 0;
|
|
static volatile uint32_t s_last_rx_ticks = 0;
|
|
|
|
|
|
|
|
|
|
+/* 收到的 GET_DATA 帧计数 (供 guardian/诊断: 从未增长 = 数据链路断) */
|
|
|
|
|
+static volatile uint32_t s_get_data_cnt = 0;
|
|
|
|
|
+
|
|
|
/**
|
|
/**
|
|
|
- * @brief 解析刚收到的快照并打印全部遥测字段
|
|
|
|
|
- * @note 仅打印, 数据已由 Mid_Temp_UpdateSnapshot 缓存
|
|
|
|
|
|
|
+ * @brief 解析刚收到的 GET_DATA 并打印全部遥测字段
|
|
|
|
|
+ * @note 仅打印, 数据已由 Mid_Temp_UpdateGetData 缓存
|
|
|
* @param 无
|
|
* @param 无
|
|
|
* @return 无
|
|
* @return 无
|
|
|
*/
|
|
*/
|
|
|
-static void App_Cpld_DumpSnapshot(void)
|
|
|
|
|
|
|
+static void App_Cpld_DumpGetData(void)
|
|
|
{
|
|
{
|
|
|
const mid_temp_snapshot_t *s = Mid_Temp_GetSnapshot();
|
|
const mid_temp_snapshot_t *s = Mid_Temp_GetSnapshot();
|
|
|
uint8_t i;
|
|
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("[SNAP] ptc_applied:");
|
|
|
|
|
+ for (i = 0; i < 24; i++)
|
|
|
|
|
+ {
|
|
|
|
|
+ TELEMETRY_PRINTF(" %d", (int)s->ptc_applied[i]);
|
|
|
|
|
+ }
|
|
|
|
|
+ TELEMETRY_PRINTF("\r\n[FAN] front: %d %d %d %d | rear: %d %d %d %d\r\n",
|
|
|
|
|
+ (int)s->front_applied[0], (int)s->front_applied[1],
|
|
|
|
|
+ (int)s->front_applied[2], (int)s->front_applied[3],
|
|
|
|
|
+ (int)s->rear_applied[0], (int)s->rear_applied[1],
|
|
|
|
|
+ (int)s->rear_applied[2], (int)s->rear_applied[3]);
|
|
|
|
|
|
|
|
TELEMETRY_PRINTF("[BMC 0-11] ");
|
|
TELEMETRY_PRINTF("[BMC 0-11] ");
|
|
|
for (i = 0; i < 12; i++)
|
|
for (i = 0; i < 12; i++)
|
|
|
{
|
|
{
|
|
|
- TELEMETRY_PRINTF("%d=%d ", (int)i, (int)s->bmc[i]);
|
|
|
|
|
|
|
+ if (s->bmc[i] == 0xFF) { TELEMETRY_PRINTF("%d=inv ", (int)i); }
|
|
|
|
|
+ else { TELEMETRY_PRINTF("%d=%d ", (int)i, (int)s->bmc[i]); }
|
|
|
}
|
|
}
|
|
|
TELEMETRY_PRINTF("\r\n[BMC 12-23] ");
|
|
TELEMETRY_PRINTF("\r\n[BMC 12-23] ");
|
|
|
for (i = 12; i < 24; i++)
|
|
for (i = 12; i < 24; i++)
|
|
|
{
|
|
{
|
|
|
- TELEMETRY_PRINTF("%d=%d ", (int)i, (int)s->bmc[i]);
|
|
|
|
|
|
|
+ if (s->bmc[i] == 0xFF) { TELEMETRY_PRINTF("%d=inv ", (int)i); }
|
|
|
|
|
+ else { TELEMETRY_PRINTF("%d=%d ", (int)i, (int)s->bmc[i]); }
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
TELEMETRY_PRINTF("\r\n[CT 0-13] ");
|
|
TELEMETRY_PRINTF("\r\n[CT 0-13] ");
|
|
|
for (i = 0; i < 14; i++)
|
|
for (i = 0; i < 14; i++)
|
|
|
{
|
|
{
|
|
|
- TELEMETRY_PRINTF("%d=%d ", (int)i, (int)s->ct[i]);
|
|
|
|
|
|
|
+ if (s->ct[i] == 0xFF) { TELEMETRY_PRINTF("%d=inv ", (int)i); }
|
|
|
|
|
+ else { TELEMETRY_PRINTF("%d=%d ", (int)i, (int)s->ct[i]); }
|
|
|
}
|
|
}
|
|
|
TELEMETRY_PRINTF("\r\n[CT 14-26] ");
|
|
TELEMETRY_PRINTF("\r\n[CT 14-26] ");
|
|
|
for (i = 14; i < 27; i++)
|
|
for (i = 14; i < 27; i++)
|
|
|
{
|
|
{
|
|
|
- TELEMETRY_PRINTF("%d=%d ", (int)i, (int)s->ct[i]);
|
|
|
|
|
|
|
+ if (s->ct[i] == 0xFF) { TELEMETRY_PRINTF("%d=inv ", (int)i); }
|
|
|
|
|
+ else { TELEMETRY_PRINTF("%d=%d ", (int)i, (int)s->ct[i]); }
|
|
|
}
|
|
}
|
|
|
|
|
+ TELEMETRY_PRINTF("\r\n");
|
|
|
|
|
+}
|
|
|
|
|
|
|
|
- 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 打印 NACK 告警
|
|
|
|
|
+ * @param nack : 已解析的 NACK 内容
|
|
|
|
|
+ * @return 无
|
|
|
|
|
+ */
|
|
|
|
|
+static void App_Cpld_ReportNack(const com_cpld_nack_t *nack)
|
|
|
|
|
+{
|
|
|
|
|
+ static const char * const reason_str[] = {
|
|
|
|
|
+ "-", "UNSUPPORTED", "BAD_LENGTH", "BAD_VALUE", "QUEUE_FULL", "BUSY"
|
|
|
|
|
+ };
|
|
|
|
|
+ const char *rs = (nack->reason <= COM_CPLD_NACK_BUSY)
|
|
|
|
|
+ ? reason_str[nack->reason] : "-";
|
|
|
|
|
+
|
|
|
|
|
+ LOG_PRINTF("[ERR] CPLD: NACK req=0x%02X reason=%s(%d)\r\n",
|
|
|
|
|
+ (int)nack->request_type, rs, (int)nack->reason);
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
/**
|
|
|
- * @brief 轮询串口 RX, 校验并解析 CPLD 回传帧
|
|
|
|
|
- * @note 滚动扫描 |A5 5A|Type|Length|Payload|CRC| 帧; 命中 0x82 快照后
|
|
|
|
|
- * 刷新温度缓存并打印; 0x06 ACK 仅打印状态码。
|
|
|
|
|
- * @param 无
|
|
|
|
|
|
|
+ * @brief 轮询串口 RX, 按声明 LENGTH 消费并解析 CPLD 回传帧 (v2.0 §7.4)
|
|
|
|
|
+ * @note 命中帧头后先按 LENGTH 取整帧: CRC 错/长度不符也整帧跳过
|
|
|
|
|
+ * (把 payload 内的 A5 5A 一并消费), 再重新找头。
|
|
|
|
|
+ * 类型分发: 0x81 SET_ACK / 0x82 GET_ACK / 0x42 GET_DATA(83B) /
|
|
|
|
|
+ * 0xC0 NACK(2B) / 0x50 MOTOR_DONE (本板不应出现)。
|
|
|
|
|
+ * @param p_wait_set_ack : [out] 收到 SET_ACK 置 1 (可为 NULL)
|
|
|
|
|
+ * @param p_wait_get : [out] 收到 GET_ACK 或 GET_DATA 置 1 (可为 NULL)
|
|
|
|
|
+ * @param p_wait_get_data : [out] 收到 GET_DATA 置 1 (可为 NULL)
|
|
|
* @return 无
|
|
* @return 无
|
|
|
*/
|
|
*/
|
|
|
-static void App_Cpld_PollRx(void)
|
|
|
|
|
|
|
+static void App_Cpld_PollRx(uint8_t *p_wait_set_ack, uint8_t *p_wait_get, uint8_t *p_wait_get_data)
|
|
|
{
|
|
{
|
|
|
static uint8_t s_rx[CPLD_RX_BUF_LEN];
|
|
static uint8_t s_rx[CPLD_RX_BUF_LEN];
|
|
|
static uint16_t s_len = 0;
|
|
static uint16_t s_len = 0;
|
|
|
uint16_t avail;
|
|
uint16_t avail;
|
|
|
uint16_t pos;
|
|
uint16_t pos;
|
|
|
|
|
|
|
|
|
|
+ if (p_wait_set_ack) { *p_wait_set_ack = 0; }
|
|
|
|
|
+ if (p_wait_get) { *p_wait_get = 0; }
|
|
|
|
|
+ if (p_wait_get_data) { *p_wait_get_data = 0; }
|
|
|
|
|
+
|
|
|
avail = Com_cpld_usart_available();
|
|
avail = Com_cpld_usart_available();
|
|
|
if (avail > 0)
|
|
if (avail > 0)
|
|
|
{
|
|
{
|
|
@@ -138,7 +163,7 @@ static void App_Cpld_PollRx(void)
|
|
|
{
|
|
{
|
|
|
if (s_rx[pos] == COM_CPLD_HEADER_H && s_rx[pos + 1] == COM_CPLD_HEADER_L)
|
|
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) */
|
|
|
|
|
|
|
+ uint16_t need = (uint16_t)(s_rx[pos + 3] + 6); /* 头2+type+length+payload+2(CRC) */
|
|
|
uint8_t type;
|
|
uint8_t type;
|
|
|
const uint8_t *payload;
|
|
const uint8_t *payload;
|
|
|
uint8_t plen;
|
|
uint8_t plen;
|
|
@@ -148,32 +173,71 @@ static void App_Cpld_PollRx(void)
|
|
|
break; /* 帧未收全, 等待更多数据 */
|
|
break; /* 帧未收全, 等待更多数据 */
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
+ /* §7.4: 候选帧一旦成立, CRC 错也按声明 LENGTH 整帧消费 */
|
|
|
if (Com_cpld_check_frame(&s_rx[pos], need, &type, &payload, &plen))
|
|
if (Com_cpld_check_frame(&s_rx[pos], need, &type, &payload, &plen))
|
|
|
{
|
|
{
|
|
|
s_last_rx_ticks = xTaskGetTickCount();
|
|
s_last_rx_ticks = xTaskGetTickCount();
|
|
|
|
|
|
|
|
- if (type == COM_CPLD_TYPE_SNAPSHOT_DATA)
|
|
|
|
|
|
|
+ switch (type)
|
|
|
{
|
|
{
|
|
|
- if (Mid_Temp_UpdateSnapshot(payload, plen))
|
|
|
|
|
|
|
+ case COM_CPLD_TYPE_SET_ACK:
|
|
|
|
|
+ if (p_wait_set_ack) { *p_wait_set_ack = 1; }
|
|
|
|
|
+ break;
|
|
|
|
|
+
|
|
|
|
|
+ case COM_CPLD_TYPE_GET_ACK:
|
|
|
|
|
+ if (p_wait_get) { *p_wait_get = 1; }
|
|
|
|
|
+ break;
|
|
|
|
|
+
|
|
|
|
|
+ case COM_CPLD_TYPE_GET_DATA:
|
|
|
|
|
+ if (plen == COM_CPLD_PAYLOAD_GET_DATA)
|
|
|
{
|
|
{
|
|
|
- App_Cpld_DumpSnapshot();
|
|
|
|
|
|
|
+ if (Mid_Temp_UpdateGetData(payload, plen))
|
|
|
|
|
+ {
|
|
|
|
|
+ s_get_data_cnt++;
|
|
|
|
|
+ if (p_wait_get) { *p_wait_get = 1; }
|
|
|
|
|
+ if (p_wait_get_data) { *p_wait_get_data = 1; }
|
|
|
|
|
+ App_Cpld_DumpGetData();
|
|
|
|
|
+ }
|
|
|
|
|
+ else
|
|
|
|
|
+ {
|
|
|
|
|
+ LOG_PRINTF("[ERR] CPLD: GET_DATA len=%d\r\n", (int)plen);
|
|
|
|
|
+ }
|
|
|
}
|
|
}
|
|
|
else
|
|
else
|
|
|
{
|
|
{
|
|
|
- LOG_PRINTF("[ERR] CPLD: snapshot len=%d\r\n", (int)plen);
|
|
|
|
|
|
|
+ LOG_PRINTF("[ERR] CPLD: GET_DATA bad len=%d\r\n", (int)plen);
|
|
|
}
|
|
}
|
|
|
- }
|
|
|
|
|
- else if (type == COM_CPLD_TYPE_ACK_NACK)
|
|
|
|
|
|
|
+ break;
|
|
|
|
|
+
|
|
|
|
|
+ case COM_CPLD_TYPE_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]);
|
|
|
|
|
|
|
+ com_cpld_nack_t nack;
|
|
|
|
|
+ if (Com_cpld_nack_parse(payload, plen, &nack))
|
|
|
|
|
+ {
|
|
|
|
|
+ App_Cpld_ReportNack(&nack);
|
|
|
|
|
+ /* NACK = 请求未执行, 两种等待都放行 */
|
|
|
|
|
+ if (p_wait_set_ack) { *p_wait_set_ack = 1; }
|
|
|
|
|
+ if (p_wait_get) { *p_wait_get = 1; }
|
|
|
|
|
+ }
|
|
|
|
|
+ break;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ case COM_CPLD_TYPE_MOTOR_DONE:
|
|
|
|
|
+ /* MRDIMM 异步完成帧, 6400 产品不应出现 */
|
|
|
|
|
+ LOG_PRINTF("[ERR] CPLD: unexpected MOTOR_DONE\r\n");
|
|
|
|
|
+ break;
|
|
|
|
|
+
|
|
|
|
|
+ default:
|
|
|
|
|
+ /* 其余 TYPE: CRC 合法但本板不识别, 仅统计 */
|
|
|
|
|
+ LOG_PRINTF("[WARN] CPLD: unknown type=0x%02X len=%d\r\n",
|
|
|
|
|
+ (int)type, (int)plen);
|
|
|
|
|
+ break;
|
|
|
}
|
|
}
|
|
|
pos += need;
|
|
pos += need;
|
|
|
}
|
|
}
|
|
|
else
|
|
else
|
|
|
{
|
|
{
|
|
|
- pos += 1; /* 帧头合法但校验失败, 前进 1 字节 */
|
|
|
|
|
|
|
+ pos += need; /* 帧头合法但 CRC 错: 整帧消费后重新找头 */
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
else
|
|
else
|
|
@@ -194,26 +258,43 @@ static void App_Cpld_PollRx(void)
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
/**
|
|
|
- * @brief 查询 CPLD 通讯是否存活
|
|
|
|
|
- * @note 最近 APP_CPLD_COMM_TIMEOUT_MS 内收到过任何 CPLD 有效帧
|
|
|
|
|
- * (ACK/SNAPSHOT) 视为存活; 从未收到返回 0。
|
|
|
|
|
- * @param 无
|
|
|
|
|
- * @return 1=通讯正常, 0=未接通/失联
|
|
|
|
|
|
|
+ * @brief 等待指定应答出现 (轮询 RX, 限时)
|
|
|
|
|
+ * @param flag : [in/out] 等待标志, 本函数轮询 PollRx 期间被命中置 1
|
|
|
|
|
+ * @param which : 0=等 SET_ACK/NACK, 1=等 GET 组 (GET_ACK/GET_DATA/NACK)
|
|
|
|
|
+ * @return 1=等到应答, 0=超时
|
|
|
*/
|
|
*/
|
|
|
-uint8_t App_cpld_comm_alive(void)
|
|
|
|
|
|
|
+static uint8_t App_Cpld_WaitReply(uint8_t *flag, uint8_t which)
|
|
|
{
|
|
{
|
|
|
- if (s_last_rx_ticks == 0)
|
|
|
|
|
|
|
+ uint32_t waited = 0;
|
|
|
|
|
+
|
|
|
|
|
+ while (waited < CPLD_WAIT_ACK_MS)
|
|
|
{
|
|
{
|
|
|
- return 0;
|
|
|
|
|
|
|
+ if (which == 0)
|
|
|
|
|
+ {
|
|
|
|
|
+ App_Cpld_PollRx(flag, NULL, NULL);
|
|
|
|
|
+ }
|
|
|
|
|
+ else
|
|
|
|
|
+ {
|
|
|
|
|
+ App_Cpld_PollRx(NULL, flag, NULL);
|
|
|
|
|
+ }
|
|
|
|
|
+ if (*flag)
|
|
|
|
|
+ {
|
|
|
|
|
+ return 1;
|
|
|
|
|
+ }
|
|
|
|
|
+ vTaskDelay(pdMS_TO_TICKS(10));
|
|
|
|
|
+ waited += 10;
|
|
|
}
|
|
}
|
|
|
- return ((uint32_t)(xTaskGetTickCount() - s_last_rx_ticks) <
|
|
|
|
|
- (uint32_t)pdMS_TO_TICKS(APP_CPLD_COMM_TIMEOUT_MS)) ? 1 : 0;
|
|
|
|
|
|
|
+ LOG_PRINTF("[WARN] CPLD: %s timeout %dms\r\n",
|
|
|
|
|
+ (which == 0) ? "SET_ACK" : "GET reply", (int)CPLD_WAIT_ACK_MS);
|
|
|
|
|
+ return 0;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
/**
|
|
|
- * @brief CPLD 通信任务体: 周期下发 SET_ALL 并轮询快照
|
|
|
|
|
|
|
+ * @brief CPLD 通信任务体: 停等节奏下发 SET_ALL / GET_ALL 并解析回包
|
|
|
* @note 在任务上下文完成 Mid_PID_Init()/Mid_Temp_Init() (软复位后依赖);
|
|
* @note 在任务上下文完成 Mid_PID_Init()/Mid_Temp_Init() (软复位后依赖);
|
|
|
* SET_ALL 兼作 CPLD 看门狗喂狗报文 (30s 超时)。
|
|
* SET_ALL 兼作 CPLD 看门狗喂狗报文 (30s 超时)。
|
|
|
|
|
+ * 每轮: SET_ALL → 等 SET_ACK → GET_ALL → 等 GET_ACK+GET_DATA。
|
|
|
|
|
+ * 某步超时不中断周期, 下轮继续 (等价自动重发)。
|
|
|
* @param argument : 任务入参 (未使用)
|
|
* @param argument : 任务入参 (未使用)
|
|
|
* @return 无 (任务不可返回)
|
|
* @return 无 (任务不可返回)
|
|
|
*/
|
|
*/
|
|
@@ -222,6 +303,7 @@ static void Task_CPLD_Process(void *argument)
|
|
|
(void)argument;
|
|
(void)argument;
|
|
|
uint8_t frame[COM_CPLD_FRAME_MAX_LEN];
|
|
uint8_t frame[COM_CPLD_FRAME_MAX_LEN];
|
|
|
uint16_t flen;
|
|
uint16_t flen;
|
|
|
|
|
+ cpld_seq_t seq = CPLD_SEQ_IDLE;
|
|
|
|
|
|
|
|
LOG_PRINTF("[INFO] CPLD: task running\r\n");
|
|
LOG_PRINTF("[INFO] CPLD: task running\r\n");
|
|
|
|
|
|
|
@@ -232,23 +314,52 @@ static void Task_CPLD_Process(void *argument)
|
|
|
{
|
|
{
|
|
|
App_guardian_feed(GUARD_ID_CPLD);
|
|
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)
|
|
|
|
|
|
|
+ switch (seq)
|
|
|
|
|
+ {
|
|
|
|
|
+ case CPLD_SEQ_IDLE:
|
|
|
{
|
|
{
|
|
|
- Com_cpld_usart_send(frame, flen);
|
|
|
|
|
|
|
+ uint8_t got = 0;
|
|
|
|
|
+
|
|
|
|
|
+ /* 读输出缓冲+封装 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);
|
|
|
|
|
+ App_Cpld_PollRx(&got, NULL, NULL);
|
|
|
|
|
+ if (!got)
|
|
|
|
|
+ {
|
|
|
|
|
+ App_Cpld_WaitReply(&got, 0);
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ seq = CPLD_SEQ_SEND_GET;
|
|
|
|
|
+ break;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- flen = Com_cpld_get_snapshot(frame);
|
|
|
|
|
- if (flen > 0)
|
|
|
|
|
|
|
+ case CPLD_SEQ_SEND_GET:
|
|
|
{
|
|
{
|
|
|
- Com_cpld_usart_send(frame, flen);
|
|
|
|
|
|
|
+ uint8_t got = 0;
|
|
|
|
|
+
|
|
|
|
|
+ flen = Com_cpld_get_all(frame);
|
|
|
|
|
+ if (flen > 0)
|
|
|
|
|
+ {
|
|
|
|
|
+ Com_cpld_usart_send(frame, flen);
|
|
|
|
|
+ App_Cpld_PollRx(NULL, &got, NULL);
|
|
|
|
|
+ if (!got)
|
|
|
|
|
+ {
|
|
|
|
|
+ App_Cpld_WaitReply(&got, 1);
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ seq = CPLD_SEQ_IDLE;
|
|
|
|
|
+ break;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- App_Cpld_PollRx();
|
|
|
|
|
|
|
+ default:
|
|
|
|
|
+ seq = CPLD_SEQ_IDLE;
|
|
|
|
|
+ break;
|
|
|
|
|
+ }
|
|
|
|
|
|
|
|
vTaskDelay(pdMS_TO_TICKS(CPLD_SEND_PERIOD_MS));
|
|
vTaskDelay(pdMS_TO_TICKS(CPLD_SEND_PERIOD_MS));
|
|
|
}
|
|
}
|
|
@@ -271,3 +382,30 @@ void App_cpld_task_create(void)
|
|
|
LOG_PRINTF("[ERR] CPLD: task create failed\r\n");
|
|
LOG_PRINTF("[ERR] CPLD: task create failed\r\n");
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
|
|
+
|
|
|
|
|
+/**
|
|
|
|
|
+ * @brief 查询 CPLD 通讯是否存活
|
|
|
|
|
+ * @note 最近 APP_CPLD_COMM_TIMEOUT_MS 内收到过任何 CPLD 有效帧
|
|
|
|
|
+ * (ACK/GET_DATA/NACK) 视为存活; 从未收到返回 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 查询是否已收到过 GET_DATA 帧 (温度数据链路打通)
|
|
|
|
|
+ * @param 无
|
|
|
|
|
+ * @return 1=收到过, 0=从未收到
|
|
|
|
|
+ */
|
|
|
|
|
+uint8_t App_cpld_data_seen(void)
|
|
|
|
|
+{
|
|
|
|
|
+ return (s_get_data_cnt != 0) ? 1 : 0;
|
|
|
|
|
+}
|