App_guardian.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. /**
  2. ******************************************************************************
  3. * @file App_guardian.c
  4. * @brief 软件守护任务 (App 应用层)
  5. *
  6. * @note 唯一喂狗者 (prio 6, 500ms 周期): 查 5 路心跳 / CPLD 通讯存活 /
  7. * 快照故障标志 / 栈水位; 分级处置 (告警 → fail-safe → 软复位 →
  8. * 停喂等 IWDG 硬复位)。每轮检查做完最后才喂狗, guardian 自身
  9. * 卡住则狗咬复位。
  10. *
  11. * fail-safe 语义: 仅用于数据故障 (快照异常/BMC 全无效),
  12. * CPLD 任务下个 1s 周期自动下发;
  13. * 通讯中断只上报不清缓冲, 靠 CPLD 侧 30s
  14. * 超时兜底
  15. ******************************************************************************
  16. */
  17. #include "App_guardian.h"
  18. #if GUARD_ENABLE
  19. #include "FreeRTOS.h"
  20. #include "task.h"
  21. #include "main.h"
  22. #include "Com_usb_cdc.h"
  23. #include "Int_iwdg.h"
  24. #include "Mid_pid.h"
  25. #include "Mid_temp.h"
  26. #include "App_cpld.h"
  27. #include "App_display.h"
  28. #include "App_preheat.h"
  29. #include "App_pidControl.h"
  30. #include "App_pc.h"
  31. /* 任务参数 (应用任务最高可用 6, timer 任务占 7) */
  32. #define TASK_GUARDIAN_PRIORITY 6
  33. #define TASK_GUARDIAN_STACK_SIZE 512
  34. /* 复位前遗言 flush 等待 */
  35. #define GUARD_REBOOT_FLUSH_MS 100
  36. static TaskHandle_t s_guard_task = NULL;
  37. /* 各任务末次报活 tick (0=从未报活, 宽限期内不检查) */
  38. static volatile uint32_t s_last_feed[GUARD_ID_COUNT] = {0, 0, 0, 0, 0};
  39. static uint32_t s_grace_until = 0;
  40. static uint8_t s_comm_lost_rounds = 0;
  41. static uint8_t s_comm_lost_reported = 0;
  42. static uint8_t s_snap_fault_reported = 0;
  43. static const char * const s_task_names[GUARD_ID_COUNT] =
  44. {"CPLD", "LVGL", "Preheat", "PID", "PC"};
  45. void App_guardian_feed(uint8_t id)
  46. {
  47. if (id < GUARD_ID_COUNT)
  48. {
  49. s_last_feed[id] = xTaskGetTickCount();
  50. }
  51. }
  52. /* fail-safe: 24 路 PTC 清零, 8 路风扇保持 (用公有写缓冲 API, 须在锁内调用) */
  53. static void App_Guardian_Failsafe(void)
  54. {
  55. uint8_t i;
  56. Mid_PID_LockOutput();
  57. for (i = 0; i < PID_CHANNEL_MAX; i++)
  58. {
  59. Mid_PID_HeaterControl(i, 0.0f);
  60. }
  61. Mid_PID_UnlockOutput();
  62. LOG_PRINTF("[ERR] Guard: failsafe, PTC off\r\n");
  63. }
  64. /* 遗言打印 + flush + 软复位 (不返回) */
  65. static void App_Guardian_Reboot(const char *reason)
  66. {
  67. LOG_PRINTF("[ERR] Guard: rebooting (%s)\r\n", reason);
  68. vTaskDelay(pdMS_TO_TICKS(GUARD_REBOOT_FLUSH_MS));
  69. NVIC_SystemReset();
  70. while (1)
  71. {
  72. }
  73. }
  74. /* 心跳检查: 任一超时即确认卡死, 走 L3 软复位 */
  75. static void App_Guardian_CheckHeartbeat(uint32_t now)
  76. {
  77. uint8_t i;
  78. if ((int32_t)(now - s_grace_until) < 0)
  79. {
  80. return; /* 上电宽限内不检查 */
  81. }
  82. for (i = 0; i < GUARD_ID_COUNT; i++)
  83. {
  84. if (s_last_feed[i] == 0 ||
  85. (now - s_last_feed[i]) > (uint32_t)pdMS_TO_TICKS(GUARD_HB_TIMEOUT_MS))
  86. {
  87. LOG_PRINTF("[ERR] Guard: %s heartbeat timeout\r\n", s_task_names[i]);
  88. App_Guardian_Reboot("heartbeat");
  89. }
  90. }
  91. }
  92. /* 通讯链路检查: 连续失联只上报,不清缓冲, 帧到不了下位机
  93. * 执行权在 CPLD 30s 超时, 恢复打印恢复 */
  94. static void App_Guardian_CheckComm(void)
  95. {
  96. if (App_cpld_comm_alive())
  97. {
  98. if (s_comm_lost_reported)
  99. {
  100. s_comm_lost_reported = 0;
  101. LOG_PRINTF("[OK] Guard: CPLD comm recovered\r\n");
  102. }
  103. s_comm_lost_rounds = 0;
  104. return;
  105. }
  106. if (s_comm_lost_rounds < 0xFF)
  107. {
  108. s_comm_lost_rounds++;
  109. }
  110. if (s_comm_lost_rounds >= GUARD_COMM_LOST_ROUNDS && !s_comm_lost_reported)
  111. {
  112. s_comm_lost_reported = 1;
  113. LOG_PRINTF("[ERR] Guard: CPLD comm lost\r\n");
  114. }
  115. }
  116. /* 快照故障检查: 硬件保护/故障掩码触发 fail-safe;
  117. * valid/stale/age 只诊断打印 */
  118. static void App_Guardian_CheckSnapshot(void)
  119. {
  120. const mid_temp_snapshot_t *s = Mid_Temp_GetSnapshot();
  121. if ((s->hard_protect != 0) || (s->fault_flags != 0))
  122. {
  123. if (!s_snap_fault_reported)
  124. {
  125. s_snap_fault_reported = 1;
  126. LOG_PRINTF("[ERR] Guard: snapshot fault hardprot=%d flags=0x%04X\r\n",
  127. (int)s->hard_protect, (int)s->fault_flags);
  128. }
  129. App_Guardian_Failsafe();
  130. }
  131. else
  132. {
  133. s_snap_fault_reported = 0;
  134. }
  135. }
  136. /* 栈水位巡检 (含自身, handle 传 NULL 查调用者): 低水位一次性告警 */
  137. static void App_Guardian_CheckStack(void)
  138. {
  139. static uint8_t s_warned[GUARD_ID_COUNT + 1] = {0};
  140. TaskHandle_t handles[GUARD_ID_COUNT + 1];
  141. uint8_t i;
  142. UBaseType_t wm;
  143. handles[0] = xCPLDProcessTaskHandle;
  144. handles[1] = xLVGLDisplayTaskHandle;
  145. handles[2] = xPreheatTaskHandle;
  146. handles[3] = xPidControlTaskHandle;
  147. handles[4] = xPCTaskHandle;
  148. handles[5] = NULL; /* 自身 */
  149. for (i = 0; i < (uint8_t)(GUARD_ID_COUNT + 1); i++)
  150. {
  151. if (s_warned[i])
  152. {
  153. continue;
  154. }
  155. if (i < GUARD_ID_COUNT && handles[i] == NULL)
  156. {
  157. continue; /* 任务尚未创建 */
  158. }
  159. wm = uxTaskGetStackHighWaterMark(handles[i]);
  160. if (wm < (UBaseType_t)GUARD_STACK_WARN_WORDS)
  161. {
  162. s_warned[i] = 1;
  163. if (i < GUARD_ID_COUNT)
  164. {
  165. LOG_PRINTF("[ERR] Guard: %s stack low=%u\r\n",
  166. s_task_names[i], (unsigned)wm);
  167. }
  168. else
  169. {
  170. LOG_PRINTF("[ERR] Guard: self stack low=%u\r\n", (unsigned)wm);
  171. }
  172. }
  173. }
  174. }
  175. static void Task_Guardian(void *argument)
  176. {
  177. (void)argument;
  178. LOG_PRINTF("[INFO] Guard: task running\r\n");
  179. s_grace_until = xTaskGetTickCount() + pdMS_TO_TICKS(GUARD_BOOT_GRACE_MS);
  180. while (1)
  181. {
  182. App_Guardian_CheckHeartbeat(xTaskGetTickCount());
  183. App_Guardian_CheckComm();
  184. App_Guardian_CheckSnapshot();
  185. App_Guardian_CheckStack();
  186. Int_iwdg_feed(); /* 最后喂狗 */
  187. vTaskDelay(pdMS_TO_TICKS(GUARD_PERIOD_MS));
  188. }
  189. }
  190. void App_guardian_task_create(void)
  191. {
  192. BaseType_t ret;
  193. ret = xTaskCreate(Task_Guardian, "guardTask", TASK_GUARDIAN_STACK_SIZE,
  194. NULL, TASK_GUARDIAN_PRIORITY, &s_guard_task);
  195. if (ret != pdPASS)
  196. {
  197. LOG_PRINTF("[ERR] Guard: task create failed\r\n");
  198. }
  199. }
  200. #endif /* GUARD_ENABLE */