| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231 |
- /**
- ******************************************************************************
- * @file App_guardian.c
- * @brief 软件守护任务 (App 应用层)
- *
- * @note 唯一喂狗者 (prio 6, 500ms 周期): 查 5 路心跳 / CPLD 通讯存活 /
- * 快照故障标志 / 栈水位; 分级处置 (告警 → fail-safe → 软复位 →
- * 停喂等 IWDG 硬复位)。每轮检查做完最后才喂狗, guardian 自身
- * 卡住则狗咬复位。
- *
- * fail-safe 语义: 仅用于数据故障 (快照异常/BMC 全无效),
- * CPLD 任务下个 1s 周期自动下发;
- * 通讯中断只上报不清缓冲, 靠 CPLD 侧 30s
- * 超时兜底
- ******************************************************************************
- */
- #include "App_guardian.h"
- #if GUARD_ENABLE
- #include "FreeRTOS.h"
- #include "task.h"
- #include "main.h"
- #include "Com_usb_cdc.h"
- #include "Int_iwdg.h"
- #include "Mid_pid.h"
- #include "Mid_temp.h"
- #include "App_cpld.h"
- #include "App_display.h"
- #include "App_preheat.h"
- #include "App_pidControl.h"
- #include "App_pc.h"
- /* 任务参数 (应用任务最高可用 6, timer 任务占 7) */
- #define TASK_GUARDIAN_PRIORITY 6
- #define TASK_GUARDIAN_STACK_SIZE 512
- /* 复位前遗言 flush 等待 */
- #define GUARD_REBOOT_FLUSH_MS 100
- static TaskHandle_t s_guard_task = NULL;
- /* 各任务末次报活 tick (0=从未报活, 宽限期内不检查) */
- static volatile uint32_t s_last_feed[GUARD_ID_COUNT] = {0, 0, 0, 0, 0};
- static uint32_t s_grace_until = 0;
- static uint8_t s_comm_lost_rounds = 0;
- static uint8_t s_comm_lost_reported = 0;
- static uint8_t s_snap_fault_reported = 0;
- static const char * const s_task_names[GUARD_ID_COUNT] =
- {"CPLD", "LVGL", "Preheat", "PID", "PC"};
- void App_guardian_feed(uint8_t id)
- {
- if (id < GUARD_ID_COUNT)
- {
- s_last_feed[id] = xTaskGetTickCount();
- }
- }
- /* fail-safe: 24 路 PTC 清零, 8 路风扇保持 (用公有写缓冲 API, 须在锁内调用) */
- static void App_Guardian_Failsafe(void)
- {
- uint8_t i;
- Mid_PID_LockOutput();
- for (i = 0; i < PID_CHANNEL_MAX; i++)
- {
- Mid_PID_HeaterControl(i, 0.0f);
- }
- Mid_PID_UnlockOutput();
- LOG_PRINTF("[ERR] Guard: failsafe, PTC off\r\n");
- }
- /* 遗言打印 + flush + 软复位 (不返回) */
- static void App_Guardian_Reboot(const char *reason)
- {
- LOG_PRINTF("[ERR] Guard: rebooting (%s)\r\n", reason);
- vTaskDelay(pdMS_TO_TICKS(GUARD_REBOOT_FLUSH_MS));
- NVIC_SystemReset();
- while (1)
- {
- }
- }
- /* 心跳检查: 任一超时即确认卡死, 走 L3 软复位 */
- static void App_Guardian_CheckHeartbeat(uint32_t now)
- {
- uint8_t i;
- if ((int32_t)(now - s_grace_until) < 0)
- {
- return; /* 上电宽限内不检查 */
- }
- for (i = 0; i < GUARD_ID_COUNT; i++)
- {
- if (s_last_feed[i] == 0 ||
- (now - s_last_feed[i]) > (uint32_t)pdMS_TO_TICKS(GUARD_HB_TIMEOUT_MS))
- {
- LOG_PRINTF("[ERR] Guard: %s heartbeat timeout\r\n", s_task_names[i]);
- App_Guardian_Reboot("heartbeat");
- }
- }
- }
- /* 通讯链路检查: 连续失联只上报,不清缓冲, 帧到不了下位机
- * 执行权在 CPLD 30s 超时, 恢复打印恢复 */
- static void App_Guardian_CheckComm(void)
- {
- if (App_cpld_comm_alive())
- {
- if (s_comm_lost_reported)
- {
- s_comm_lost_reported = 0;
- LOG_PRINTF("[OK] Guard: CPLD comm recovered\r\n");
- }
- s_comm_lost_rounds = 0;
- return;
- }
- if (s_comm_lost_rounds < 0xFF)
- {
- s_comm_lost_rounds++;
- }
- if (s_comm_lost_rounds >= GUARD_COMM_LOST_ROUNDS && !s_comm_lost_reported)
- {
- s_comm_lost_reported = 1;
- LOG_PRINTF("[ERR] Guard: CPLD comm lost\r\n");
- }
- }
- /* 快照故障检查: 硬件保护/故障掩码触发 fail-safe;
- * valid/stale/age 只诊断打印 */
- static void App_Guardian_CheckSnapshot(void)
- {
- const mid_temp_snapshot_t *s = Mid_Temp_GetSnapshot();
- if ((s->hard_protect != 0) || (s->fault_flags != 0))
- {
- if (!s_snap_fault_reported)
- {
- s_snap_fault_reported = 1;
- LOG_PRINTF("[ERR] Guard: snapshot fault hardprot=%d flags=0x%04X\r\n",
- (int)s->hard_protect, (int)s->fault_flags);
- }
- App_Guardian_Failsafe();
- }
- else
- {
- s_snap_fault_reported = 0;
- }
- }
- /* 栈水位巡检 (含自身, handle 传 NULL 查调用者): 低水位一次性告警 */
- static void App_Guardian_CheckStack(void)
- {
- static uint8_t s_warned[GUARD_ID_COUNT + 1] = {0};
- TaskHandle_t handles[GUARD_ID_COUNT + 1];
- uint8_t i;
- UBaseType_t wm;
- handles[0] = xCPLDProcessTaskHandle;
- handles[1] = xLVGLDisplayTaskHandle;
- handles[2] = xPreheatTaskHandle;
- handles[3] = xPidControlTaskHandle;
- handles[4] = xPCTaskHandle;
- handles[5] = NULL; /* 自身 */
- for (i = 0; i < (uint8_t)(GUARD_ID_COUNT + 1); i++)
- {
- if (s_warned[i])
- {
- continue;
- }
- if (i < GUARD_ID_COUNT && handles[i] == NULL)
- {
- continue; /* 任务尚未创建 */
- }
- wm = uxTaskGetStackHighWaterMark(handles[i]);
- if (wm < (UBaseType_t)GUARD_STACK_WARN_WORDS)
- {
- s_warned[i] = 1;
- if (i < GUARD_ID_COUNT)
- {
- LOG_PRINTF("[ERR] Guard: %s stack low=%u\r\n",
- s_task_names[i], (unsigned)wm);
- }
- else
- {
- LOG_PRINTF("[ERR] Guard: self stack low=%u\r\n", (unsigned)wm);
- }
- }
- }
- }
- static void Task_Guardian(void *argument)
- {
- (void)argument;
- LOG_PRINTF("[INFO] Guard: task running\r\n");
- s_grace_until = xTaskGetTickCount() + pdMS_TO_TICKS(GUARD_BOOT_GRACE_MS);
- while (1)
- {
- App_Guardian_CheckHeartbeat(xTaskGetTickCount());
- App_Guardian_CheckComm();
- App_Guardian_CheckSnapshot();
- App_Guardian_CheckStack();
- Int_iwdg_feed(); /* 最后喂狗 */
- vTaskDelay(pdMS_TO_TICKS(GUARD_PERIOD_MS));
- }
- }
- void App_guardian_task_create(void)
- {
- BaseType_t ret;
- ret = xTaskCreate(Task_Guardian, "guardTask", TASK_GUARDIAN_STACK_SIZE,
- NULL, TASK_GUARDIAN_PRIORITY, &s_guard_task);
- if (ret != pdPASS)
- {
- LOG_PRINTF("[ERR] Guard: task create failed\r\n");
- }
- }
- #endif /* GUARD_ENABLE */
|