ソースを参照

增加:守护进程 + IWDG看门狗 + 一键关闭开关
1. 新增App_guardian (优先级6, 500ms, 唯一喂狗者): 5路心跳(超时2.5s)、CPLD通讯存活、快照故障标志、栈水位巡检
2.分级处置: 告警打印 -> fail-safe (PTC清零风扇保持)
-> 心跳超时软复位 -> 停喂等IWDG硬复位 (3s)
3. 新增Int_iwdg封装, 复用CubeMX MX_IWDG1_Init;
CubeMX开启IWDG1
4. GUARD_ENABLE一键总开关
5.main.c启动打印复位原因

zhanghongyi 2 ヶ月 前
親
コミット
445cfd3322

+ 63 - 0
App/Inc/App_guardian.h

@@ -0,0 +1,63 @@
+/**
+  ******************************************************************************
+  * @file    App_guardian.h
+  * @brief   软件守护任务接口 (App 应用层)
+  *
+  * @note    guardian (prio 6, 500ms 周期) 是唯一喂狗者: 查 5 路任务心跳、
+  *          CPLD 通讯存活、快照故障标志、任务栈水位; 分级处置
+  *          (告警打印 → fail-safe → 软复位 → 停喂等 IWDG 硬复位)。
+  *
+  *          一键关闭: GUARD_ENABLE=0 时 guardian 任务不存在、MX_IWDG1_Init
+  *          提前返回 (IWDG 从未启动), feed/create 均为空宏, 零开销。
+  *          注意关 guardian 必须同时不启动 IWDG, 否则 3s 必复位。
+  ******************************************************************************
+  */
+#ifndef __APP_GUARDIAN_H
+#define __APP_GUARDIAN_H
+
+#include <stdint.h>
+
+/* 守护总开关: 1=启用(guardian任务+IWDG), 0=全部关闭(调试/维护用), 改后重编烧录 */
+#define GUARD_ENABLE  0
+
+/* 心跳 ID (被监视任务; usbSendTask 不纳入, 避免 Com 层反向依赖 App 层) */
+#define GUARD_ID_CPLD     0
+#define GUARD_ID_LVGL     1
+#define GUARD_ID_PREHEAT  2
+#define GUARD_ID_PID      3
+#define GUARD_ID_PC       4
+#define GUARD_ID_COUNT    5
+
+/* 可调阈值 */
+#define GUARD_PERIOD_MS         500    /* guardian 检查周期 */
+#define GUARD_HB_TIMEOUT_MS     2500   /* 心跳超时 (确认任务卡死) */
+#define GUARD_BOOT_GRACE_MS     3000   /* 上电宽限 (首轮检查前等待各任务首次报活) */
+#define GUARD_COMM_LOST_ROUNDS  3      /* 通讯连续失联轮数 (触发 fail-safe) */
+#define GUARD_STACK_WARN_WORDS  64     /* 栈剩余字数告警线 */
+
+#if GUARD_ENABLE
+
+/**
+ * @brief  任务报活 (各被监视任务主循环每轮调用一次)
+ * @note   仅任务上下文调用; 开销为一次 tick 读取
+ * @param  id : 心跳 ID (GUARD_ID_xxx)
+ * @return 无
+ */
+void App_guardian_feed(uint8_t id);
+
+/**
+ * @brief  创建守护任务
+ * @note   在 vTaskStartScheduler() 之前调用一次
+ * @param  无
+ * @return 无
+ */
+void App_guardian_task_create(void);
+
+#else
+
+#define App_guardian_feed(id)       do { } while (0)
+#define App_guardian_task_create()  do { } while (0)
+
+#endif /* GUARD_ENABLE */
+
+#endif /* __APP_GUARDIAN_H */

+ 5 - 0
App/Inc/App_pc.h

@@ -10,6 +10,11 @@
 #ifndef __APP_PC_H
 #ifndef __APP_PC_H
 #define __APP_PC_H
 #define __APP_PC_H
 
 
+#include "FreeRTOS.h"
+#include "task.h"
+
+extern TaskHandle_t xPCTaskHandle;
+
 /**
 /**
  * @brief  创建上位机通信任务
  * @brief  创建上位机通信任务
  * @param  无
  * @param  无

+ 3 - 0
App/Src/App_cpld.c

@@ -21,6 +21,7 @@
 #include "Com_usb_cdc.h"
 #include "Com_usb_cdc.h"
 #include "Mid_pid.h"
 #include "Mid_pid.h"
 #include "Mid_temp.h"
 #include "Mid_temp.h"
+#include "App_guardian.h"
 
 
 /* 任务参数 (数字越大优先级越高) */
 /* 任务参数 (数字越大优先级越高) */
 #define TASK_CPLD_PRIORITY      4
 #define TASK_CPLD_PRIORITY      4
@@ -229,6 +230,8 @@ static void Task_CPLD_Process(void *argument)
 
 
     while (1)
     while (1)
     {
     {
+        App_guardian_feed(GUARD_ID_CPLD);
+
         /* 读输出缓冲+封装 SET_ALL 须在同一临界区, 防止读到 PID/预热
         /* 读输出缓冲+封装 SET_ALL 须在同一临界区, 防止读到 PID/预热
          * 任务批量写入到一半的缓冲; 发送 (可能阻塞) 在锁外进行 */
          * 任务批量写入到一半的缓冲; 发送 (可能阻塞) 在锁外进行 */
         Mid_PID_LockOutput();
         Mid_PID_LockOutput();

+ 2 - 0
App/Src/App_display.c

@@ -15,6 +15,7 @@
 #include "lv_port_indev.h"
 #include "lv_port_indev.h"
 #include "Int_gt1151.h"
 #include "Int_gt1151.h"
 #include "Com_usb_cdc.h"
 #include "Com_usb_cdc.h"
+#include "App_guardian.h"
 #include "Ui.h"
 #include "Ui.h"
 
 
 /* 任务参数 (数字越大优先级越高); LVGL 渲染/动画/触摸处理较重, 栈需较大 */
 /* 任务参数 (数字越大优先级越高); LVGL 渲染/动画/触摸处理较重, 栈需较大 */
@@ -53,6 +54,7 @@ static void Task_LVGL_Display(void *argument)
 
 
     while (1)
     while (1)
     {
     {
+        App_guardian_feed(GUARD_ID_LVGL);
         /* 周期驱动 LVGL(渲染/动画/输入),配合 LV_DISP_DEF_REFR_PERIOD 30ms */
         /* 周期驱动 LVGL(渲染/动画/输入),配合 LV_DISP_DEF_REFR_PERIOD 30ms */
         lv_timer_handler();
         lv_timer_handler();
         vTaskDelay(pdMS_TO_TICKS(5));
         vTaskDelay(pdMS_TO_TICKS(5));

+ 230 - 0
App/Src/App_guardian.c

@@ -0,0 +1,230 @@
+/**
+  ******************************************************************************
+  * @file    App_guardian.c
+  * @brief   软件守护任务 (App 应用层)
+  *
+  * @note    唯一喂狗者 (prio 6, 500ms 周期): 查 5 路心跳 / CPLD 通讯存活 /
+  *          快照故障标志 / 栈水位; 分级处置 (告警 → fail-safe → 软复位 →
+  *          停喂等 IWDG 硬复位)。每轮检查做完最后才喂狗, guardian 自身
+  *          卡住则狗咬复位。
+  *
+  *          fail-safe 语义: PTC[0..23] 清零、风扇保持, CPLD 任务下个 1s
+  *          周期自动下发; 通讯中断期间帧发不到 CPLD, 靠 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");
+        }
+    }
+}
+
+/* 通讯链路检查: 连续失联则 fail-safe, 恢复打恢复 */
+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");
+        App_Guardian_Failsafe();
+    }
+}
+
+/* 快照故障检查: 硬件保护/故障掩码触发 fail-safe;
+ * valid/stale/age 本期只诊断打印 (判据等 CPLD 侧逻辑, H3 联动点) */
+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 */

+ 2 - 0
App/Src/App_main.c

@@ -12,6 +12,7 @@
 #include "App_preheat.h"
 #include "App_preheat.h"
 #include "App_pidControl.h"
 #include "App_pidControl.h"
 #include "App_pc.h"
 #include "App_pc.h"
+#include "App_guardian.h"
 
 
 void App_Task_Create(void)
 void App_Task_Create(void)
 {
 {
@@ -20,4 +21,5 @@ void App_Task_Create(void)
     App_preheat_task_create();       /* prio2 预加热状态机 */
     App_preheat_task_create();       /* prio2 预加热状态机 */
     App_pidControl_task_create();    /* prio2 PID 闭环控温 (默认待命) */
     App_pidControl_task_create();    /* prio2 PID 闭环控温 (默认待命) */
     App_pc_task_create();            /* prio2 PC 上位机 (USART1) 协议应答 */
     App_pc_task_create();            /* prio2 PC 上位机 (USART1) 协议应答 */
+    App_guardian_task_create();      /* prio6 守护 (唯一喂狗者, GUARD_ENABLE=0 时为空) */
 }
 }

+ 3 - 0
App/Src/App_pc.c

@@ -20,6 +20,7 @@
 #include "Com_usb_cdc.h"
 #include "Com_usb_cdc.h"
 #include "Com_parameter.h"
 #include "Com_parameter.h"
 #include "Mid_temp.h"
 #include "Mid_temp.h"
+#include "App_guardian.h"
 
 
 /* 任务参数 */
 /* 任务参数 */
 #define TASK_PC_PRIORITY       2
 #define TASK_PC_PRIORITY       2
@@ -192,6 +193,8 @@ static void Task_PC_Process(void *argument)
 
 
     while (1)
     while (1)
     {
     {
+        App_guardian_feed(GUARD_ID_PC);
+
         App_Pc_PollRx();
         App_Pc_PollRx();
         vTaskDelay(pdMS_TO_TICKS(PC_POLL_PERIOD_MS));
         vTaskDelay(pdMS_TO_TICKS(PC_POLL_PERIOD_MS));
     }
     }

+ 3 - 0
App/Src/App_pidControl.c

@@ -20,6 +20,7 @@
 #include "Com_usb_cdc.h"
 #include "Com_usb_cdc.h"
 #include "Mid_pid.h"
 #include "Mid_pid.h"
 #include "Mid_temp.h"
 #include "Mid_temp.h"
+#include "App_guardian.h"
 
 
 /* 任务参数 */
 /* 任务参数 */
 #define APP_PIDCONTROL_TASK_PRIORITY   2
 #define APP_PIDCONTROL_TASK_PRIORITY   2
@@ -50,6 +51,8 @@ static void Task_PidControl(void *argument)
 
 
     while (1)
     while (1)
     {
     {
+        App_guardian_feed(GUARD_ID_PID);
+
         if (s_running)
         if (s_running)
         {
         {
             int16_t sensor4[MID_TEMP_PREHEAT_NUM];
             int16_t sensor4[MID_TEMP_PREHEAT_NUM];

+ 3 - 0
App/Src/App_preheat.c

@@ -25,6 +25,7 @@
 #include "Mid_pid.h"
 #include "Mid_pid.h"
 #include "Mid_temp.h"
 #include "Mid_temp.h"
 #include "Ui_preheat.h"
 #include "Ui_preheat.h"
+#include "App_guardian.h"
 
 
 /* 队列长度 */
 /* 队列长度 */
 #define APP_PREHEAT_UI_QUEUE_LEN    4      /* 屏端命令队列 */
 #define APP_PREHEAT_UI_QUEUE_LEN    4      /* 屏端命令队列 */
@@ -235,6 +236,8 @@ void Task_Preheat(void *argument)
 
 
     while (1)
     while (1)
     {
     {
+        App_guardian_feed(GUARD_ID_PREHEAT);
+
         /* 阻塞等待任一来源命令; 超时则执行控制周期步进 */
         /* 阻塞等待任一来源命令; 超时则执行控制周期步进 */
         qm = xQueueSelectFromSet(s_qset, pdMS_TO_TICKS(APP_PREHEAT_PERIOD_MS));
         qm = xQueueSelectFromSet(s_qset, pdMS_TO_TICKS(APP_PREHEAT_PERIOD_MS));
 
 

+ 52 - 0
Core/Inc/iwdg.h

@@ -0,0 +1,52 @@
+/* USER CODE BEGIN Header */
+/**
+  ******************************************************************************
+  * @file    iwdg.h
+  * @brief   This file contains all the function prototypes for
+  *          the iwdg.c file
+  ******************************************************************************
+  * @attention
+  *
+  * Copyright (c) 2026 STMicroelectronics.
+  * All rights reserved.
+  *
+  * This software is licensed under terms that can be found in the LICENSE file
+  * in the root directory of this software component.
+  * If no LICENSE file comes with this software, it is provided AS-IS.
+  *
+  ******************************************************************************
+  */
+/* USER CODE END Header */
+/* Define to prevent recursive inclusion -------------------------------------*/
+#ifndef __IWDG_H__
+#define __IWDG_H__
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+/* Includes ------------------------------------------------------------------*/
+#include "main.h"
+
+/* USER CODE BEGIN Includes */
+
+/* USER CODE END Includes */
+
+extern IWDG_HandleTypeDef hiwdg1;
+
+/* USER CODE BEGIN Private defines */
+
+/* USER CODE END Private defines */
+
+void MX_IWDG1_Init(void);
+
+/* USER CODE BEGIN Prototypes */
+
+/* USER CODE END Prototypes */
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif /* __IWDG_H__ */
+

+ 1 - 1
Core/Inc/stm32h7xx_hal_conf.h

@@ -61,7 +61,7 @@
 /* #define HAL_OSPI_MODULE_ENABLED   */
 /* #define HAL_OSPI_MODULE_ENABLED   */
 /* #define HAL_I2S_MODULE_ENABLED   */
 /* #define HAL_I2S_MODULE_ENABLED   */
 /* #define HAL_SMBUS_MODULE_ENABLED   */
 /* #define HAL_SMBUS_MODULE_ENABLED   */
-/* #define HAL_IWDG_MODULE_ENABLED   */
+#define HAL_IWDG_MODULE_ENABLED
 /* #define HAL_LPTIM_MODULE_ENABLED   */
 /* #define HAL_LPTIM_MODULE_ENABLED   */
 #define HAL_LTDC_MODULE_ENABLED
 #define HAL_LTDC_MODULE_ENABLED
 #define HAL_QSPI_MODULE_ENABLED
 #define HAL_QSPI_MODULE_ENABLED

+ 59 - 0
Core/Src/iwdg.c

@@ -0,0 +1,59 @@
+/* USER CODE BEGIN Header */
+/**
+  ******************************************************************************
+  * @file    iwdg.c
+  * @brief   This file provides code for the configuration
+  *          of the IWDG instances.
+  ******************************************************************************
+  * @attention
+  *
+  * Copyright (c) 2026 STMicroelectronics.
+  * All rights reserved.
+  *
+  * This software is licensed under terms that can be found in the LICENSE file
+  * in the root directory of this software component.
+  * If no LICENSE file comes with this software, it is provided AS-IS.
+  *
+  ******************************************************************************
+  */
+/* USER CODE END Header */
+/* Includes ------------------------------------------------------------------*/
+#include "iwdg.h"
+
+/* USER CODE BEGIN 0 */
+#include "App_guardian.h"
+/* USER CODE END 0 */
+
+IWDG_HandleTypeDef hiwdg1;
+
+/* IWDG1 init function */
+void MX_IWDG1_Init(void)
+{
+
+  /* USER CODE BEGIN IWDG1_Init 0 */
+#if !GUARD_ENABLE
+  return;   /* 守护关闭: IWDG 从未启动 (启动后无法关闭, 只能复位) */
+#endif
+  /* USER CODE END IWDG1_Init 0 */
+
+  /* USER CODE BEGIN IWDG1_Init 1 */
+
+  /* USER CODE END IWDG1_Init 1 */
+  hiwdg1.Instance = IWDG1;
+  hiwdg1.Init.Prescaler = IWDG_PRESCALER_32;
+  hiwdg1.Init.Window = 4095;
+  hiwdg1.Init.Reload = 3000;
+  if (HAL_IWDG_Init(&hiwdg1) != HAL_OK)
+  {
+    Error_Handler();
+  }
+  /* USER CODE BEGIN IWDG1_Init 2 */
+
+  /* USER CODE END IWDG1_Init 2 */
+
+}
+
+/* USER CODE BEGIN 1 */
+
+/* USER CODE END 1 */
+

+ 33 - 1
Core/Src/main.c

@@ -21,6 +21,7 @@
 #include "dma.h"
 #include "dma.h"
 #include "dma2d.h"
 #include "dma2d.h"
 #include "i2c.h"
 #include "i2c.h"
+#include "iwdg.h"
 #include "ltdc.h"
 #include "ltdc.h"
 #include "quadspi.h"
 #include "quadspi.h"
 #include "usart.h"
 #include "usart.h"
@@ -42,6 +43,7 @@
 #include "Com_pc_usart.h"
 #include "Com_pc_usart.h"
 #include "Com_usb_cdc.h"
 #include "Com_usb_cdc.h"
 #include "Com_parameter.h"
 #include "Com_parameter.h"
+#include "Int_iwdg.h"
 /* USER CODE END Includes */
 /* USER CODE END Includes */
 
 
 /* Private typedef -----------------------------------------------------------*/
 /* Private typedef -----------------------------------------------------------*/
@@ -131,13 +133,41 @@ int main(void)
   MX_DMA2D_Init();
   MX_DMA2D_Init();
   MX_USB_DEVICE_Init();
   MX_USB_DEVICE_Init();
   MX_USART3_UART_Init();
   MX_USART3_UART_Init();
+  MX_IWDG1_Init();
   /* USER CODE BEGIN 2 */
   /* USER CODE BEGIN 2 */
+  {
+    /* 上电打印复位原因 (IWDG/Soft/PIN/POR/BOR), 定位"谁复位的板子"; 读后清标志 */
+    uint32_t rsr = RCC->RSR;
+    if (rsr & RCC_RSR_IWDG1RSTF)
+    {
+      LOG_PRINTF("[INFO] Reset cause: IWDG\r\n");
+    }
+    if (rsr & RCC_RSR_WWDG1RSTF)
+    {
+      LOG_PRINTF("[INFO] Reset cause: WWDG\r\n");
+    }
+    if (rsr & RCC_RSR_SFTRSTF)
+    {
+      LOG_PRINTF("[INFO] Reset cause: SOFT\r\n");
+    }
+    if (rsr & RCC_RSR_PINRSTF)
+    {
+      LOG_PRINTF("[INFO] Reset cause: PIN\r\n");
+    }
+    if (rsr & (RCC_RSR_PORRSTF | RCC_RSR_BORRSTF))
+    {
+      LOG_PRINTF("[INFO] Reset cause: POR/BOR\r\n");
+    }
+    RCC->RSR |= RCC_RSR_RMVF;
+  }
   Int_sdram_init();
   Int_sdram_init();
   Int_lcd_init();
   Int_lcd_init();
   Com_parameter_init();
   Com_parameter_init();
   Com_usb_cdc_init();
   Com_usb_cdc_init();
   Com_cpld_usart_init();
   Com_cpld_usart_init();
   Com_pc_usart_init();
   Com_pc_usart_init();
+  Int_iwdg_init();          /* 重装载计数器, 缩短上电到 guardian 首喂的窗口 */
+  Int_iwdg_feed();
   LOG_PRINTF("[OK] System Init\r\n");
   LOG_PRINTF("[OK] System Init\r\n");
   /* USER CODE END 2 */
   /* USER CODE END 2 */
 
 
@@ -181,9 +211,11 @@ void SystemClock_Config(void)
   /** Initializes the RCC Oscillators according to the specified parameters
   /** Initializes the RCC Oscillators according to the specified parameters
   * in the RCC_OscInitTypeDef structure.
   * in the RCC_OscInitTypeDef structure.
   */
   */
-  RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI48|RCC_OSCILLATORTYPE_HSI;
+  RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI48|RCC_OSCILLATORTYPE_HSI
+                              |RCC_OSCILLATORTYPE_LSI;
   RCC_OscInitStruct.HSIState = RCC_HSI_DIV1;
   RCC_OscInitStruct.HSIState = RCC_HSI_DIV1;
   RCC_OscInitStruct.HSICalibrationValue = RCC_HSICALIBRATION_DEFAULT;
   RCC_OscInitStruct.HSICalibrationValue = RCC_HSICALIBRATION_DEFAULT;
+  RCC_OscInitStruct.LSIState = RCC_LSI_ON;
   RCC_OscInitStruct.HSI48State = RCC_HSI48_ON;
   RCC_OscInitStruct.HSI48State = RCC_HSI48_ON;
   RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
   RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
   RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSI;
   RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSI;

+ 237 - 0
Drivers/STM32H7xx_HAL_Driver/Inc/stm32h7xx_hal_iwdg.h

@@ -0,0 +1,237 @@
+/**
+  ******************************************************************************
+  * @file    stm32h7xx_hal_iwdg.h
+  * @author  MCD Application Team
+  * @brief   Header file of IWDG HAL module.
+  ******************************************************************************
+  * @attention
+  *
+  * Copyright (c) 2017 STMicroelectronics.
+  * All rights reserved.
+  *
+  * This software is licensed under terms that can be found in the LICENSE file
+  * in the root directory of this software component.
+  * If no LICENSE file comes with this software, it is provided AS-IS.
+  *
+  ******************************************************************************
+  */
+
+/* Define to prevent recursive inclusion -------------------------------------*/
+#ifndef STM32H7xx_HAL_IWDG_H
+#define STM32H7xx_HAL_IWDG_H
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+/* Includes ------------------------------------------------------------------*/
+#include "stm32h7xx_hal_def.h"
+
+/** @addtogroup STM32H7xx_HAL_Driver
+  * @{
+  */
+
+/** @defgroup IWDG IWDG
+  * @{
+  */
+
+/* Exported types ------------------------------------------------------------*/
+/** @defgroup IWDG_Exported_Types IWDG Exported Types
+  * @{
+  */
+
+/**
+  * @brief  IWDG Init structure definition
+  */
+typedef struct
+{
+  uint32_t Prescaler;  /*!< Select the prescaler of the IWDG.
+                            This parameter can be a value of @ref IWDG_Prescaler */
+
+  uint32_t Reload;     /*!< Specifies the IWDG down-counter reload value.
+                            This parameter must be a number between Min_Data = 0 and Max_Data = 0x0FFF */
+
+  uint32_t Window;     /*!< Specifies the window value to be compared to the down-counter.
+                            This parameter must be a number between Min_Data = 0 and Max_Data = 0x0FFF */
+
+} IWDG_InitTypeDef;
+
+/**
+  * @brief  IWDG Handle Structure definition
+  */
+typedef struct
+{
+  IWDG_TypeDef                 *Instance;  /*!< Register base address    */
+
+  IWDG_InitTypeDef             Init;       /*!< IWDG required parameters */
+} IWDG_HandleTypeDef;
+
+
+/**
+  * @}
+  */
+
+/* Exported constants --------------------------------------------------------*/
+/** @defgroup IWDG_Exported_Constants IWDG Exported Constants
+  * @{
+  */
+
+/** @defgroup IWDG_Prescaler IWDG Prescaler
+  * @{
+  */
+#define IWDG_PRESCALER_4                0x00000000u                                     /*!< IWDG prescaler set to 4   */
+#define IWDG_PRESCALER_8                IWDG_PR_PR_0                                    /*!< IWDG prescaler set to 8   */
+#define IWDG_PRESCALER_16               IWDG_PR_PR_1                                    /*!< IWDG prescaler set to 16  */
+#define IWDG_PRESCALER_32               (IWDG_PR_PR_1 | IWDG_PR_PR_0)                   /*!< IWDG prescaler set to 32  */
+#define IWDG_PRESCALER_64               IWDG_PR_PR_2                                    /*!< IWDG prescaler set to 64  */
+#define IWDG_PRESCALER_128              (IWDG_PR_PR_2 | IWDG_PR_PR_0)                   /*!< IWDG prescaler set to 128 */
+#define IWDG_PRESCALER_256              (IWDG_PR_PR_2 | IWDG_PR_PR_1)                   /*!< IWDG prescaler set to 256 */
+/**
+  * @}
+  */
+
+/** @defgroup IWDG_Window_option IWDG Window option
+  * @{
+  */
+#define IWDG_WINDOW_DISABLE             IWDG_WINR_WIN
+/**
+  * @}
+  */
+
+/**
+  * @}
+  */
+
+/* Exported macros -----------------------------------------------------------*/
+/** @defgroup IWDG_Exported_Macros IWDG Exported Macros
+  * @{
+  */
+
+/**
+  * @brief  Enable the IWDG peripheral.
+  * @param  __HANDLE__  IWDG handle
+  * @retval None
+  */
+#define __HAL_IWDG_START(__HANDLE__)                WRITE_REG((__HANDLE__)->Instance->KR, IWDG_KEY_ENABLE)
+
+/**
+  * @brief  Reload IWDG counter with value defined in the reload register
+  *         (write access to IWDG_PR, IWDG_RLR and IWDG_WINR registers disabled).
+  * @param  __HANDLE__  IWDG handle
+  * @retval None
+  */
+#define __HAL_IWDG_RELOAD_COUNTER(__HANDLE__)       WRITE_REG((__HANDLE__)->Instance->KR, IWDG_KEY_RELOAD)
+
+/**
+  * @}
+  */
+
+/* Exported functions --------------------------------------------------------*/
+/** @defgroup IWDG_Exported_Functions  IWDG Exported Functions
+  * @{
+  */
+
+/** @defgroup IWDG_Exported_Functions_Group1 Initialization and Start functions
+  * @{
+  */
+/* Initialization/Start functions  ********************************************/
+HAL_StatusTypeDef     HAL_IWDG_Init(IWDG_HandleTypeDef *hiwdg);
+/**
+  * @}
+  */
+
+/** @defgroup IWDG_Exported_Functions_Group2 IO operation functions
+  * @{
+  */
+/* I/O operation functions ****************************************************/
+HAL_StatusTypeDef     HAL_IWDG_Refresh(IWDG_HandleTypeDef *hiwdg);
+/**
+  * @}
+  */
+
+/**
+  * @}
+  */
+
+/* Private constants ---------------------------------------------------------*/
+/** @defgroup IWDG_Private_Constants IWDG Private Constants
+  * @{
+  */
+
+/**
+  * @brief  IWDG Key Register BitMask
+  */
+#define IWDG_KEY_RELOAD                 0x0000AAAAu  /*!< IWDG Reload Counter Enable   */
+#define IWDG_KEY_ENABLE                 0x0000CCCCu  /*!< IWDG Peripheral Enable       */
+#define IWDG_KEY_WRITE_ACCESS_ENABLE    0x00005555u  /*!< IWDG KR Write Access Enable  */
+#define IWDG_KEY_WRITE_ACCESS_DISABLE   0x00000000u  /*!< IWDG KR Write Access Disable */
+
+/**
+  * @}
+  */
+
+/* Private macros ------------------------------------------------------------*/
+/** @defgroup IWDG_Private_Macros IWDG Private Macros
+  * @{
+  */
+
+/**
+  * @brief  Enable write access to IWDG_PR, IWDG_RLR and IWDG_WINR registers.
+  * @param  __HANDLE__  IWDG handle
+  * @retval None
+  */
+#define IWDG_ENABLE_WRITE_ACCESS(__HANDLE__)  WRITE_REG((__HANDLE__)->Instance->KR, IWDG_KEY_WRITE_ACCESS_ENABLE)
+
+/**
+  * @brief  Disable write access to IWDG_PR, IWDG_RLR and IWDG_WINR registers.
+  * @param  __HANDLE__  IWDG handle
+  * @retval None
+  */
+#define IWDG_DISABLE_WRITE_ACCESS(__HANDLE__) WRITE_REG((__HANDLE__)->Instance->KR, IWDG_KEY_WRITE_ACCESS_DISABLE)
+
+/**
+  * @brief  Check IWDG prescaler value.
+  * @param  __PRESCALER__  IWDG prescaler value
+  * @retval None
+  */
+#define IS_IWDG_PRESCALER(__PRESCALER__)      (((__PRESCALER__) == IWDG_PRESCALER_4)  || \
+                                               ((__PRESCALER__) == IWDG_PRESCALER_8)  || \
+                                               ((__PRESCALER__) == IWDG_PRESCALER_16) || \
+                                               ((__PRESCALER__) == IWDG_PRESCALER_32) || \
+                                               ((__PRESCALER__) == IWDG_PRESCALER_64) || \
+                                               ((__PRESCALER__) == IWDG_PRESCALER_128)|| \
+                                               ((__PRESCALER__) == IWDG_PRESCALER_256))
+
+/**
+  * @brief  Check IWDG reload value.
+  * @param  __RELOAD__  IWDG reload value
+  * @retval None
+  */
+#define IS_IWDG_RELOAD(__RELOAD__)            ((__RELOAD__) <= IWDG_RLR_RL)
+
+/**
+  * @brief  Check IWDG window value.
+  * @param  __WINDOW__  IWDG window value
+  * @retval None
+  */
+#define IS_IWDG_WINDOW(__WINDOW__)            ((__WINDOW__) <= IWDG_WINR_WIN)
+
+
+/**
+  * @}
+  */
+
+/**
+  * @}
+  */
+
+/**
+  * @}
+  */
+
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif /* STM32H7xx_HAL_IWDG_H */

+ 338 - 0
Drivers/STM32H7xx_HAL_Driver/Inc/stm32h7xx_ll_iwdg.h

@@ -0,0 +1,338 @@
+/**
+  ******************************************************************************
+  * @file    stm32h7xx_ll_iwdg.h
+  * @author  MCD Application Team
+  * @brief   Header file of IWDG LL module.
+  ******************************************************************************
+  * @attention
+  *
+  * Copyright (c) 2017 STMicroelectronics.
+  * All rights reserved.
+  *
+  * This software is licensed under terms that can be found in the LICENSE file
+  * in the root directory of this software component.
+  * If no LICENSE file comes with this software, it is provided AS-IS.
+  *
+  ******************************************************************************
+  */
+
+/* Define to prevent recursive inclusion -------------------------------------*/
+#ifndef STM32H7xx_LL_IWDG_H
+#define STM32H7xx_LL_IWDG_H
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+/* Includes ------------------------------------------------------------------*/
+#include "stm32h7xx.h"
+
+/** @addtogroup STM32H7xx_LL_Driver
+  * @{
+  */
+
+#if defined(IWDG1) || defined(IWDG2)
+
+/** @defgroup IWDG_LL IWDG
+  * @{
+  */
+
+/* Private types -------------------------------------------------------------*/
+/* Private variables ---------------------------------------------------------*/
+
+/* Private constants ---------------------------------------------------------*/
+/** @defgroup IWDG_LL_Private_Constants IWDG Private Constants
+  * @{
+  */
+#define LL_IWDG_KEY_RELOAD                 0x0000AAAAU               /*!< IWDG Reload Counter Enable   */
+#define LL_IWDG_KEY_ENABLE                 0x0000CCCCU               /*!< IWDG Peripheral Enable       */
+#define LL_IWDG_KEY_WR_ACCESS_ENABLE       0x00005555U               /*!< IWDG KR Write Access Enable  */
+#define LL_IWDG_KEY_WR_ACCESS_DISABLE      0x00000000U               /*!< IWDG KR Write Access Disable */
+/**
+  * @}
+  */
+
+/* Private macros ------------------------------------------------------------*/
+
+/* Exported types ------------------------------------------------------------*/
+/* Exported constants --------------------------------------------------------*/
+/** @defgroup IWDG_LL_Exported_Constants IWDG Exported Constants
+  * @{
+  */
+
+/** @defgroup IWDG_LL_EC_GET_FLAG Get Flags Defines
+  * @brief    Flags defines which can be used with LL_IWDG_ReadReg function
+  * @{
+  */
+#define LL_IWDG_SR_PVU                     IWDG_SR_PVU                           /*!< Watchdog prescaler value update */
+#define LL_IWDG_SR_RVU                     IWDG_SR_RVU                           /*!< Watchdog counter reload value update */
+#define LL_IWDG_SR_WVU                     IWDG_SR_WVU                           /*!< Watchdog counter window value update */
+/**
+  * @}
+  */
+
+/** @defgroup IWDG_LL_EC_PRESCALER  Prescaler Divider
+  * @{
+  */
+#define LL_IWDG_PRESCALER_4                0x00000000U                           /*!< Divider by 4   */
+#define LL_IWDG_PRESCALER_8                (IWDG_PR_PR_0)                        /*!< Divider by 8   */
+#define LL_IWDG_PRESCALER_16               (IWDG_PR_PR_1)                        /*!< Divider by 16  */
+#define LL_IWDG_PRESCALER_32               (IWDG_PR_PR_1 | IWDG_PR_PR_0)         /*!< Divider by 32  */
+#define LL_IWDG_PRESCALER_64               (IWDG_PR_PR_2)                        /*!< Divider by 64  */
+#define LL_IWDG_PRESCALER_128              (IWDG_PR_PR_2 | IWDG_PR_PR_0)         /*!< Divider by 128 */
+#define LL_IWDG_PRESCALER_256              (IWDG_PR_PR_2 | IWDG_PR_PR_1)         /*!< Divider by 256 */
+/**
+  * @}
+  */
+
+/**
+  * @}
+  */
+
+/* Exported macro ------------------------------------------------------------*/
+/** @defgroup IWDG_LL_Exported_Macros IWDG Exported Macros
+  * @{
+  */
+
+/** @defgroup IWDG_LL_EM_WRITE_READ Common Write and read registers Macros
+  * @{
+  */
+
+/**
+  * @brief  Write a value in IWDG register
+  * @param  __INSTANCE__ IWDG Instance
+  * @param  __REG__ Register to be written
+  * @param  __VALUE__ Value to be written in the register
+  * @retval None
+  */
+#define LL_IWDG_WriteReg(__INSTANCE__, __REG__, __VALUE__) WRITE_REG(__INSTANCE__->__REG__, (__VALUE__))
+
+/**
+  * @brief  Read a value in IWDG register
+  * @param  __INSTANCE__ IWDG Instance
+  * @param  __REG__ Register to be read
+  * @retval Register value
+  */
+#define LL_IWDG_ReadReg(__INSTANCE__, __REG__) READ_REG(__INSTANCE__->__REG__)
+/**
+  * @}
+  */
+
+/**
+  * @}
+  */
+
+
+/* Exported functions --------------------------------------------------------*/
+/** @defgroup IWDG_LL_Exported_Functions IWDG Exported Functions
+  * @{
+  */
+/** @defgroup IWDG_LL_EF_Configuration Configuration
+  * @{
+  */
+
+/**
+  * @brief  Start the Independent Watchdog
+  * @note   Except if the hardware watchdog option is selected
+  * @rmtoll KR           KEY           LL_IWDG_Enable
+  * @param  IWDGx IWDG Instance
+  * @retval None
+  */
+__STATIC_INLINE void LL_IWDG_Enable(IWDG_TypeDef *IWDGx)
+{
+  WRITE_REG(IWDGx->KR, LL_IWDG_KEY_ENABLE);
+}
+
+/**
+  * @brief  Reloads IWDG counter with value defined in the reload register
+  * @rmtoll KR           KEY           LL_IWDG_ReloadCounter
+  * @param  IWDGx IWDG Instance
+  * @retval None
+  */
+__STATIC_INLINE void LL_IWDG_ReloadCounter(IWDG_TypeDef *IWDGx)
+{
+  WRITE_REG(IWDGx->KR, LL_IWDG_KEY_RELOAD);
+}
+
+/**
+  * @brief  Enable write access to IWDG_PR, IWDG_RLR and IWDG_WINR registers
+  * @rmtoll KR           KEY           LL_IWDG_EnableWriteAccess
+  * @param  IWDGx IWDG Instance
+  * @retval None
+  */
+__STATIC_INLINE void LL_IWDG_EnableWriteAccess(IWDG_TypeDef *IWDGx)
+{
+  WRITE_REG(IWDGx->KR, LL_IWDG_KEY_WR_ACCESS_ENABLE);
+}
+
+/**
+  * @brief  Disable write access to IWDG_PR, IWDG_RLR and IWDG_WINR registers
+  * @rmtoll KR           KEY           LL_IWDG_DisableWriteAccess
+  * @param  IWDGx IWDG Instance
+  * @retval None
+  */
+__STATIC_INLINE void LL_IWDG_DisableWriteAccess(IWDG_TypeDef *IWDGx)
+{
+  WRITE_REG(IWDGx->KR, LL_IWDG_KEY_WR_ACCESS_DISABLE);
+}
+
+/**
+  * @brief  Select the prescaler of the IWDG
+  * @rmtoll PR           PR            LL_IWDG_SetPrescaler
+  * @param  IWDGx IWDG Instance
+  * @param  Prescaler This parameter can be one of the following values:
+  *         @arg @ref LL_IWDG_PRESCALER_4
+  *         @arg @ref LL_IWDG_PRESCALER_8
+  *         @arg @ref LL_IWDG_PRESCALER_16
+  *         @arg @ref LL_IWDG_PRESCALER_32
+  *         @arg @ref LL_IWDG_PRESCALER_64
+  *         @arg @ref LL_IWDG_PRESCALER_128
+  *         @arg @ref LL_IWDG_PRESCALER_256
+  * @retval None
+  */
+__STATIC_INLINE void LL_IWDG_SetPrescaler(IWDG_TypeDef *IWDGx, uint32_t Prescaler)
+{
+  WRITE_REG(IWDGx->PR, IWDG_PR_PR & Prescaler);
+}
+
+/**
+  * @brief  Get the selected prescaler of the IWDG
+  * @rmtoll PR           PR            LL_IWDG_GetPrescaler
+  * @param  IWDGx IWDG Instance
+  * @retval Returned value can be one of the following values:
+  *         @arg @ref LL_IWDG_PRESCALER_4
+  *         @arg @ref LL_IWDG_PRESCALER_8
+  *         @arg @ref LL_IWDG_PRESCALER_16
+  *         @arg @ref LL_IWDG_PRESCALER_32
+  *         @arg @ref LL_IWDG_PRESCALER_64
+  *         @arg @ref LL_IWDG_PRESCALER_128
+  *         @arg @ref LL_IWDG_PRESCALER_256
+  */
+__STATIC_INLINE uint32_t LL_IWDG_GetPrescaler(const IWDG_TypeDef *IWDGx)
+{
+  return (READ_REG(IWDGx->PR));
+}
+
+/**
+  * @brief  Specify the IWDG down-counter reload value
+  * @rmtoll RLR          RL            LL_IWDG_SetReloadCounter
+  * @param  IWDGx IWDG Instance
+  * @param  Counter Value between Min_Data=0 and Max_Data=0x0FFF
+  * @retval None
+  */
+__STATIC_INLINE void LL_IWDG_SetReloadCounter(IWDG_TypeDef *IWDGx, uint32_t Counter)
+{
+  WRITE_REG(IWDGx->RLR, IWDG_RLR_RL & Counter);
+}
+
+/**
+  * @brief  Get the specified IWDG down-counter reload value
+  * @rmtoll RLR          RL            LL_IWDG_GetReloadCounter
+  * @param  IWDGx IWDG Instance
+  * @retval Value between Min_Data=0 and Max_Data=0x0FFF
+  */
+__STATIC_INLINE uint32_t LL_IWDG_GetReloadCounter(const IWDG_TypeDef *IWDGx)
+{
+  return (READ_REG(IWDGx->RLR));
+}
+
+/**
+  * @brief  Specify high limit of the window value to be compared to the down-counter.
+  * @rmtoll WINR         WIN           LL_IWDG_SetWindow
+  * @param  IWDGx IWDG Instance
+  * @param  Window Value between Min_Data=0 and Max_Data=0x0FFF
+  * @retval None
+  */
+__STATIC_INLINE void LL_IWDG_SetWindow(IWDG_TypeDef *IWDGx, uint32_t Window)
+{
+  WRITE_REG(IWDGx->WINR, IWDG_WINR_WIN & Window);
+}
+
+/**
+  * @brief  Get the high limit of the window value specified.
+  * @rmtoll WINR         WIN           LL_IWDG_GetWindow
+  * @param  IWDGx IWDG Instance
+  * @retval Value between Min_Data=0 and Max_Data=0x0FFF
+  */
+__STATIC_INLINE uint32_t LL_IWDG_GetWindow(const IWDG_TypeDef *IWDGx)
+{
+  return (READ_REG(IWDGx->WINR));
+}
+
+/**
+  * @}
+  */
+
+/** @defgroup IWDG_LL_EF_FLAG_Management FLAG_Management
+  * @{
+  */
+
+/**
+  * @brief  Check if flag Prescaler Value Update is set or not
+  * @rmtoll SR           PVU           LL_IWDG_IsActiveFlag_PVU
+  * @param  IWDGx IWDG Instance
+  * @retval State of bit (1 or 0).
+  */
+__STATIC_INLINE uint32_t LL_IWDG_IsActiveFlag_PVU(const IWDG_TypeDef *IWDGx)
+{
+  return ((READ_BIT(IWDGx->SR, IWDG_SR_PVU) == (IWDG_SR_PVU)) ? 1UL : 0UL);
+}
+
+/**
+  * @brief  Check if flag Reload Value Update is set or not
+  * @rmtoll SR           RVU           LL_IWDG_IsActiveFlag_RVU
+  * @param  IWDGx IWDG Instance
+  * @retval State of bit (1 or 0).
+  */
+__STATIC_INLINE uint32_t LL_IWDG_IsActiveFlag_RVU(const IWDG_TypeDef *IWDGx)
+{
+  return ((READ_BIT(IWDGx->SR, IWDG_SR_RVU) == (IWDG_SR_RVU)) ? 1UL : 0UL);
+}
+
+/**
+  * @brief  Check if flag Window Value Update is set or not
+  * @rmtoll SR           WVU           LL_IWDG_IsActiveFlag_WVU
+  * @param  IWDGx IWDG Instance
+  * @retval State of bit (1 or 0).
+  */
+__STATIC_INLINE uint32_t LL_IWDG_IsActiveFlag_WVU(const IWDG_TypeDef *IWDGx)
+{
+  return ((READ_BIT(IWDGx->SR, IWDG_SR_WVU) == (IWDG_SR_WVU)) ? 1UL : 0UL);
+}
+
+/**
+  * @brief  Check if all flags Prescaler, Reload & Window Value Update are reset or not
+  * @rmtoll SR           PVU           LL_IWDG_IsReady\n
+  *         SR           RVU           LL_IWDG_IsReady\n
+  *         SR           WVU           LL_IWDG_IsReady
+  * @param  IWDGx IWDG Instance
+  * @retval State of bits (1 or 0).
+  */
+__STATIC_INLINE uint32_t LL_IWDG_IsReady(const IWDG_TypeDef *IWDGx)
+{
+  return ((READ_BIT(IWDGx->SR, IWDG_SR_PVU | IWDG_SR_RVU | IWDG_SR_WVU) == 0U) ? 1UL : 0UL);
+}
+
+/**
+  * @}
+  */
+
+/**
+  * @}
+  */
+
+/**
+  * @}
+  */
+
+#endif /* IWDG1 || IWDG2 */
+
+/**
+  * @}
+  */
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif /* STM32H7xx_LL_IWDG_H */

+ 284 - 0
Drivers/STM32H7xx_HAL_Driver/Src/stm32h7xx_hal_iwdg.c

@@ -0,0 +1,284 @@
+/**
+  ******************************************************************************
+  * @file    stm32h7xx_hal_iwdg.c
+  * @author  MCD Application Team
+  * @brief   IWDG HAL module driver.
+  *          This file provides firmware functions to manage the following
+  *          functionalities of the Independent Watchdog (IWDG) peripheral:
+  *           + Initialization and Start functions
+  *           + IO operation functions
+  *
+  ******************************************************************************
+  * @attention
+  *
+  * Copyright (c) 2017 STMicroelectronics.
+  * All rights reserved.
+  *
+  * This software is licensed under terms that can be found in the LICENSE file
+  * in the root directory of this software component.
+  * If no LICENSE file comes with this software, it is provided AS-IS.
+  *
+  ******************************************************************************
+  @verbatim
+  ==============================================================================
+                    ##### IWDG Generic features #####
+  ==============================================================================
+  [..]
+    (+) The IWDG can be started by either software or hardware (configurable
+        through option byte).
+
+    (+) The IWDG is clocked by the Low-Speed Internal clock (LSI) and thus stays
+        active even if the main clock fails.
+
+    (+) Once the IWDG is started, the LSI is forced ON and both cannot be
+        disabled. The counter starts counting down from the reset value (0xFFF).
+        When it reaches the end of count value (0x000) a reset signal is
+        generated (IWDG reset).
+
+    (+) Whenever the key value 0x0000 AAAA is written in the IWDG_KR register,
+        the IWDG_RLR value is reloaded into the counter and the watchdog reset
+        is prevented.
+
+    (+) The IWDG is implemented in the VDD voltage domain that is still functional
+        in STOP and STANDBY mode (IWDG reset can wake up the CPU from STANDBY).
+        IWDGRST flag in RCC_CSR register can be used to inform when an IWDG
+        reset occurs.
+
+    (+) Debug mode: When the microcontroller enters debug mode (core halted),
+        the IWDG counter either continues to work normally or stops, depending
+        on DBG_IWDG_STOP configuration bit in DBG module, accessible through
+        __HAL_DBGMCU_FREEZE_IWDG1() or __HAL_DBGMCU_FREEZE2_IWDG2() and
+        __HAL_DBGMCU_UnFreeze_IWDG1 or __HAL_DBGMCU_UnFreeze2_IWDG2() macros.
+
+    [..] Min-max timeout value @32KHz (LSI): ~125us / ~32.7s
+         The IWDG timeout may vary due to LSI clock frequency dispersion.
+         STM32H7xx devices provide the capability to measure the LSI clock
+         frequency (LSI clock is internally connected to TIM16 CH1 input capture).
+         The measured value can be used to have an IWDG timeout with an
+         acceptable accuracy.
+
+    [..] Default timeout value (necessary for IWDG_SR status register update):
+         Constant LSI_VALUE is defined based on the nominal LSI clock frequency.
+         This frequency being subject to variations as mentioned above, the
+         default timeout value (defined through constant HAL_IWDG_DEFAULT_TIMEOUT
+         below) may become too short or too long.
+         In such cases, this default timeout value can be tuned by redefining
+         the constant LSI_VALUE at user-application level (based, for instance,
+         on the measured LSI clock frequency as explained above).
+
+                     ##### How to use this driver #####
+  ==============================================================================
+  [..]
+    (#) Use IWDG using HAL_IWDG_Init() function to :
+      (++) Enable instance by writing Start keyword in IWDG_KEY register. LSI
+           clock is forced ON and IWDG counter starts counting down.
+      (++) Enable write access to configuration registers:
+          IWDG_PR, IWDG_RLR and IWDG_WINR.
+      (++) Configure the IWDG prescaler and counter reload value. This reload
+           value will be loaded in the IWDG counter each time the watchdog is
+           reloaded, then the IWDG will start counting down from this value.
+      (++) Depending on window parameter:
+        (+++) If Window Init parameter is same as Window register value,
+             nothing more is done but reload counter value in order to exit
+             function with exact time base.
+        (+++) Else modify Window register. This will automatically reload
+             watchdog counter.
+      (++) Wait for status flags to be reset.
+
+    (#) Then the application program must refresh the IWDG counter at regular
+        intervals during normal operation to prevent an MCU reset, using
+        HAL_IWDG_Refresh() function.
+
+     *** IWDG HAL driver macros list ***
+     ====================================
+     [..]
+       Below the list of most used macros in IWDG HAL driver:
+      (+) __HAL_IWDG_START: Enable the IWDG peripheral
+      (+) __HAL_IWDG_RELOAD_COUNTER: Reloads IWDG counter with value defined in
+          the reload register
+
+  @endverbatim
+  */
+
+/* Includes ------------------------------------------------------------------*/
+#include "stm32h7xx_hal.h"
+
+/** @addtogroup STM32H7xx_HAL_Driver
+  * @{
+  */
+
+#ifdef HAL_IWDG_MODULE_ENABLED
+/** @addtogroup IWDG
+  * @brief IWDG HAL module driver.
+  * @{
+  */
+
+/* Private typedef -----------------------------------------------------------*/
+/* Private define ------------------------------------------------------------*/
+/** @defgroup IWDG_Private_Defines IWDG Private Defines
+  * @{
+  */
+/* Status register needs up to 5 LSI clock periods divided by the clock
+   prescaler to be updated. The number of LSI clock periods is upper-rounded to
+   6 for the timeout value calculation.
+   The timeout value is calculated using the highest prescaler (256) and
+   the LSI_VALUE constant. The value of this constant can be changed by the user
+   to take into account possible LSI clock period variations.
+   The timeout value is multiplied by 1000 to be converted in milliseconds.
+   LSI startup time is also considered here by adding LSI_STARTUP_TIME
+   converted in milliseconds. */
+#define HAL_IWDG_DEFAULT_TIMEOUT        (((6UL * 256UL * 1000UL) / (LSI_VALUE / 128U)) + \
+                                         ((LSI_STARTUP_TIME / 1000UL) + 1UL))
+#define IWDG_KERNEL_UPDATE_FLAGS        (IWDG_SR_WVU | IWDG_SR_RVU | IWDG_SR_PVU)
+/**
+  * @}
+  */
+
+/* Private macro -------------------------------------------------------------*/
+/* Private variables ---------------------------------------------------------*/
+/* Private function prototypes -----------------------------------------------*/
+/* Exported functions --------------------------------------------------------*/
+
+/** @addtogroup IWDG_Exported_Functions
+  * @{
+  */
+
+/** @addtogroup IWDG_Exported_Functions_Group1
+  *  @brief    Initialization and Start functions.
+  *
+@verbatim
+ ===============================================================================
+          ##### Initialization and Start functions #####
+ ===============================================================================
+ [..]  This section provides functions allowing to:
+      (+) Initialize the IWDG according to the specified parameters in the
+          IWDG_InitTypeDef of associated handle.
+      (+) Manage Window option.
+      (+) Once initialization is performed in HAL_IWDG_Init function, Watchdog
+          is reloaded in order to exit function with correct time base.
+
+@endverbatim
+  * @{
+  */
+
+/**
+  * @brief  Initialize the IWDG according to the specified parameters in the
+  *         IWDG_InitTypeDef and start watchdog. Before exiting function,
+  *         watchdog is refreshed in order to have correct time base.
+  * @param  hiwdg  pointer to a IWDG_HandleTypeDef structure that contains
+  *                the configuration information for the specified IWDG module.
+  * @retval HAL status
+  */
+HAL_StatusTypeDef HAL_IWDG_Init(IWDG_HandleTypeDef *hiwdg)
+{
+  uint32_t tickstart;
+
+  /* Check the IWDG handle allocation */
+  if (hiwdg == NULL)
+  {
+    return HAL_ERROR;
+  }
+
+  /* Check the parameters */
+  assert_param(IS_IWDG_ALL_INSTANCE(hiwdg->Instance));
+  assert_param(IS_IWDG_PRESCALER(hiwdg->Init.Prescaler));
+  assert_param(IS_IWDG_RELOAD(hiwdg->Init.Reload));
+  assert_param(IS_IWDG_WINDOW(hiwdg->Init.Window));
+
+  /* Enable IWDG. LSI is turned on automatically */
+  __HAL_IWDG_START(hiwdg);
+
+  /* Enable write access to IWDG_PR, IWDG_RLR and IWDG_WINR registers by writing
+  0x5555 in KR */
+  IWDG_ENABLE_WRITE_ACCESS(hiwdg);
+
+  /* Write to IWDG registers the Prescaler & Reload values to work with */
+  hiwdg->Instance->PR = hiwdg->Init.Prescaler;
+  hiwdg->Instance->RLR = hiwdg->Init.Reload;
+
+  /* Check pending flag, if previous update not done, return timeout */
+  tickstart = HAL_GetTick();
+
+  /* Wait for register to be updated */
+  while ((hiwdg->Instance->SR & IWDG_KERNEL_UPDATE_FLAGS) != 0x00u)
+  {
+    if ((HAL_GetTick() - tickstart) > HAL_IWDG_DEFAULT_TIMEOUT)
+    {
+      if ((hiwdg->Instance->SR & IWDG_KERNEL_UPDATE_FLAGS) != 0x00u)
+      {
+        return HAL_TIMEOUT;
+      }
+    }
+  }
+
+  /* If window parameter is different than current value, modify window
+  register */
+  if (hiwdg->Instance->WINR != hiwdg->Init.Window)
+  {
+    /* Write to IWDG WINR the IWDG_Window value to compare with. In any case,
+    even if window feature is disabled, Watchdog will be reloaded by writing
+    windows register */
+    hiwdg->Instance->WINR = hiwdg->Init.Window;
+  }
+  else
+  {
+    /* Reload IWDG counter with value defined in the reload register */
+    __HAL_IWDG_RELOAD_COUNTER(hiwdg);
+  }
+
+  /* Return function status */
+  return HAL_OK;
+}
+
+
+/**
+  * @}
+  */
+
+
+/** @addtogroup IWDG_Exported_Functions_Group2
+  *  @brief   IO operation functions
+  *
+@verbatim
+ ===============================================================================
+                      ##### IO operation functions #####
+ ===============================================================================
+ [..]  This section provides functions allowing to:
+      (+) Refresh the IWDG.
+
+@endverbatim
+  * @{
+  */
+
+/**
+  * @brief  Refresh the IWDG.
+  * @param  hiwdg  pointer to a IWDG_HandleTypeDef structure that contains
+  *                the configuration information for the specified IWDG module.
+  * @retval HAL status
+  */
+HAL_StatusTypeDef HAL_IWDG_Refresh(IWDG_HandleTypeDef *hiwdg)
+{
+  /* Reload IWDG counter with value defined in the reload register */
+  __HAL_IWDG_RELOAD_COUNTER(hiwdg);
+
+  /* Return function status */
+  return HAL_OK;
+}
+
+
+/**
+  * @}
+  */
+
+/**
+  * @}
+  */
+
+#endif /* HAL_IWDG_MODULE_ENABLED */
+/**
+  * @}
+  */
+
+/**
+  * @}
+  */

+ 1 - 1
FreeRTOS/FreeRTOSConfig.h

@@ -650,7 +650,7 @@
 #define INCLUDE_vTaskDelay                     1
 #define INCLUDE_vTaskDelay                     1
 #define INCLUDE_xTaskGetSchedulerState         1
 #define INCLUDE_xTaskGetSchedulerState         1
 #define INCLUDE_xTaskGetCurrentTaskHandle      1
 #define INCLUDE_xTaskGetCurrentTaskHandle      1
-#define INCLUDE_uxTaskGetStackHighWaterMark    0
+#define INCLUDE_uxTaskGetStackHighWaterMark    1   /* guardian 栈水位巡检用, CubeMX重生成后复查 */
 #define INCLUDE_xTaskGetIdleTaskHandle         0
 #define INCLUDE_xTaskGetIdleTaskHandle         0
 #define INCLUDE_eTaskGetState                  0
 #define INCLUDE_eTaskGetState                  0
 #define INCLUDE_xEventGroupSetBitFromISR       1
 #define INCLUDE_xEventGroupSetBitFromISR       1

+ 20 - 0
Int/Inc/Int_iwdg.h

@@ -0,0 +1,20 @@
+/**
+  ******************************************************************************
+  * @file    Int_iwdg.h
+  * @brief   独立看门狗 Int 层封装
+  *
+  * @note    底层初始化由 CubeMX 生成的 MX_IWDG1_Init 完成
+  *          (Prescaler=32, Reload=3000, Window=4095, 名义 3s 超时,
+  *           LSI 偏差下实际约 3~5.6s)。
+  ******************************************************************************
+  */
+#ifndef __INT_IWDG_H
+#define __INT_IWDG_H
+
+/* 初始化看门狗 (复用 MX_IWDG1_Init, 可重复调用, 顺带重装载计数器) */
+void Int_iwdg_init(void);
+
+/* 喂狗 (单调调用者: guardian 任务) */
+void Int_iwdg_feed(void);
+
+#endif /* __INT_IWDG_H */

+ 12 - 0
Int/Src/Int_iwdg.c

@@ -0,0 +1,12 @@
+#include "Int_iwdg.h"
+#include "iwdg.h"
+
+void Int_iwdg_init(void)
+{
+    MX_IWDG1_Init();
+}
+
+void Int_iwdg_feed(void)
+{
+    HAL_IWDG_Refresh(&hiwdg1);
+}

+ 122 - 0
MDK-ARM/STMH7_temperature_control_board.uvprojx

@@ -1404,6 +1404,62 @@
               <FileType>1</FileType>
               <FileType>1</FileType>
               <FilePath>../Drivers/STM32H7xx_HAL_Driver/Src/stm32h7xx_hal_sdram.c</FilePath>
               <FilePath>../Drivers/STM32H7xx_HAL_Driver/Src/stm32h7xx_hal_sdram.c</FilePath>
             </File>
             </File>
+            <File>
+              <FileName>stm32h7xx_hal_iwdg.c</FileName>
+              <FileType>1</FileType>
+              <FilePath>../Drivers/STM32H7xx_HAL_Driver/Src/stm32h7xx_hal_iwdg.c</FilePath>
+              <FileOption>
+                <CommonProperty>
+                  <UseCPPCompiler>2</UseCPPCompiler>
+                  <RVCTCodeConst>0</RVCTCodeConst>
+                  <RVCTZI>0</RVCTZI>
+                  <RVCTOtherData>0</RVCTOtherData>
+                  <ModuleSelection>0</ModuleSelection>
+                  <IncludeInBuild>1</IncludeInBuild>
+                  <AlwaysBuild>2</AlwaysBuild>
+                  <GenerateAssemblyFile>2</GenerateAssemblyFile>
+                  <AssembleAssemblyFile>2</AssembleAssemblyFile>
+                  <PublicsOnly>2</PublicsOnly>
+                  <StopOnExitCode>11</StopOnExitCode>
+                  <CustomArgument></CustomArgument>
+                  <IncludeLibraryModules></IncludeLibraryModules>
+                  <ComprImg>1</ComprImg>
+                </CommonProperty>
+                <FileArmAds>
+                  <Cads>
+                    <interw>2</interw>
+                    <Optim>0</Optim>
+                    <oTime>2</oTime>
+                    <SplitLS>2</SplitLS>
+                    <OneElfS>2</OneElfS>
+                    <Strict>2</Strict>
+                    <EnumInt>2</EnumInt>
+                    <PlainCh>2</PlainCh>
+                    <Ropi>2</Ropi>
+                    <Rwpi>2</Rwpi>
+                    <wLevel>0</wLevel>
+                    <uThumb>2</uThumb>
+                    <uSurpInc>2</uSurpInc>
+                    <uC99>2</uC99>
+                    <uGnu>2</uGnu>
+                    <useXO>2</useXO>
+                    <v6Lang>0</v6Lang>
+                    <v6LangP>0</v6LangP>
+                    <vShortEn>2</vShortEn>
+                    <vShortWch>2</vShortWch>
+                    <v6Lto>2</v6Lto>
+                    <v6WtE>2</v6WtE>
+                    <v6Rtti>2</v6Rtti>
+                    <VariousControls>
+                      <MiscControls></MiscControls>
+                      <Define></Define>
+                      <Undefine></Undefine>
+                      <IncludePath></IncludePath>
+                    </VariousControls>
+                  </Cads>
+                </FileArmAds>
+              </FileOption>
+            </File>
             <File>
             <File>
               <FileName>stm32h7xx_hal_ltdc.c</FileName>
               <FileName>stm32h7xx_hal_ltdc.c</FileName>
               <FileType>1</FileType>
               <FileType>1</FileType>
@@ -2599,6 +2655,11 @@
               <FileType>1</FileType>
               <FileType>1</FileType>
               <FilePath>..\App\Src\App_pc.c</FilePath>
               <FilePath>..\App\Src\App_pc.c</FilePath>
             </File>
             </File>
+            <File>
+              <FileName>App_guardian.c</FileName>
+              <FileType>1</FileType>
+              <FilePath>..\App\Src\App_guardian.c</FilePath>
+            </File>
           </Files>
           </Files>
         </Group>
         </Group>
         <Group>
         <Group>
@@ -2684,6 +2745,11 @@
               <FileType>1</FileType>
               <FileType>1</FileType>
               <FilePath>../Int/Src/Int_gt1151.c</FilePath>
               <FilePath>../Int/Src/Int_gt1151.c</FilePath>
             </File>
             </File>
+            <File>
+              <FileName>Int_iwdg.c</FileName>
+              <FileType>1</FileType>
+              <FilePath>..\Int\Src\Int_iwdg.c</FilePath>
+            </File>
           </Files>
           </Files>
         </Group>
         </Group>
         <Group>
         <Group>
@@ -2826,6 +2892,62 @@
               <FileType>1</FileType>
               <FileType>1</FileType>
               <FilePath>../Core/Src/i2c.c</FilePath>
               <FilePath>../Core/Src/i2c.c</FilePath>
             </File>
             </File>
+            <File>
+              <FileName>iwdg.c</FileName>
+              <FileType>1</FileType>
+              <FilePath>../Core/Src/iwdg.c</FilePath>
+              <FileOption>
+                <CommonProperty>
+                  <UseCPPCompiler>2</UseCPPCompiler>
+                  <RVCTCodeConst>0</RVCTCodeConst>
+                  <RVCTZI>0</RVCTZI>
+                  <RVCTOtherData>0</RVCTOtherData>
+                  <ModuleSelection>0</ModuleSelection>
+                  <IncludeInBuild>1</IncludeInBuild>
+                  <AlwaysBuild>2</AlwaysBuild>
+                  <GenerateAssemblyFile>2</GenerateAssemblyFile>
+                  <AssembleAssemblyFile>2</AssembleAssemblyFile>
+                  <PublicsOnly>2</PublicsOnly>
+                  <StopOnExitCode>11</StopOnExitCode>
+                  <CustomArgument></CustomArgument>
+                  <IncludeLibraryModules></IncludeLibraryModules>
+                  <ComprImg>1</ComprImg>
+                </CommonProperty>
+                <FileArmAds>
+                  <Cads>
+                    <interw>2</interw>
+                    <Optim>0</Optim>
+                    <oTime>2</oTime>
+                    <SplitLS>2</SplitLS>
+                    <OneElfS>2</OneElfS>
+                    <Strict>2</Strict>
+                    <EnumInt>2</EnumInt>
+                    <PlainCh>2</PlainCh>
+                    <Ropi>2</Ropi>
+                    <Rwpi>2</Rwpi>
+                    <wLevel>0</wLevel>
+                    <uThumb>2</uThumb>
+                    <uSurpInc>2</uSurpInc>
+                    <uC99>2</uC99>
+                    <uGnu>2</uGnu>
+                    <useXO>2</useXO>
+                    <v6Lang>0</v6Lang>
+                    <v6LangP>0</v6LangP>
+                    <vShortEn>2</vShortEn>
+                    <vShortWch>2</vShortWch>
+                    <v6Lto>2</v6Lto>
+                    <v6WtE>2</v6WtE>
+                    <v6Rtti>2</v6Rtti>
+                    <VariousControls>
+                      <MiscControls></MiscControls>
+                      <Define></Define>
+                      <Undefine></Undefine>
+                      <IncludePath></IncludePath>
+                    </VariousControls>
+                  </Cads>
+                </FileArmAds>
+              </FileOption>
+            </File>
             <File>
             <File>
               <FileName>ltdc.c</FileName>
               <FileName>ltdc.c</FileName>
               <FileType>1</FileType>
               <FileType>1</FileType>

ファイルの差分が大きいため隠しています
+ 24 - 17
STMH7_temperature_control_board.ioc