#include "WatchDogTask.h" #include "FileSysTask.h" #include "McuBspWatchdog.h" #include #include #include #include typedef struct { unsigned char TaskWatchdogTime; // 单位,S unsigned char TaskWatcbdogEnable; // 看门狗事件使能 char TaskName[64]; // 注册该看门狗事件的任务名称 } TASK_WATCHDOG_EVENT; static TASK_WATCHDOG_EVENT gTaskWatchdogSta[TASK_WATCHDOG_NUM_MAX] = {{0}}; // 事件池 static unsigned char gTaskWatchdogTimeOut = 0; // 看门狗超时 void TaskWatchDog(void *arg) { char i; unsigned int RealWbyte; int ret; /* 将看门狗任务开启写入日志 */ do { ret = TaskFileLogWrite("TaskWatchDog: Join in!!\n", strlen("TaskWatchDog: Join in!!\n"), &RealWbyte); } while (ret > 0); while (1) { for (i = 0; i < TASK_WATCHDOG_NUM_MAX; i++) { if (gTaskWatchdogSta[i].TaskWatcbdogEnable == 1) { gTaskWatchdogSta[i].TaskWatchdogTime++; if (gTaskWatchdogSta[i].TaskWatchdogTime > 60) { /* 将程序跑飞的任务ID写入日志文件 */ char LogData[64] = {0}; sprintf(LogData, "TaskWatchDog: %s fleet!!\n", gTaskWatchdogSta[i].TaskName); do { McuBspWatchdogFreedWatchdog(); // 进行实质的看门狗喂狗 ret = TaskFileLogWrite(LogData, strlen(LogData), &RealWbyte); if (ret > 0) { OSTimeDly(10); } } while (ret > 0); gTaskWatchdogTimeOut = 1; } } } if (gTaskWatchdogTimeOut == 1) { McuBspWatchdogStopFreedWatchdog(); } else { McuBspWatchdogFreedWatchdog(); } OSTimeDly(1000); } } void TaskWatchdogInit(void) { McuBspWatchdogInit(); } void TaskWatchDogRestart(void) { gTaskWatchdogTimeOut = 1; } int TaskWatchdogRegEvent(const char *TaskName) { int8_t i; if (TaskName == NULL) { return -1; } for (i = 0; i < TASK_WATCHDOG_NUM_MAX; i++) { if (gTaskWatchdogSta[i].TaskWatcbdogEnable != 1) { gTaskWatchdogSta[i].TaskWatchdogTime = 0; gTaskWatchdogSta[i].TaskWatcbdogEnable = 1; memcpy(gTaskWatchdogSta[i].TaskName, TaskName, strlen(TaskName)); return i; } } return -1; } int TaskWatchdogFreed(int8_t id) { if ((id >= TASK_WATCHDOG_NUM_MAX) || (id < 0)) { return -1; } if (gTaskWatchdogSta[id].TaskWatcbdogEnable == 1) { gTaskWatchdogSta[id].TaskWatchdogTime = 0; } return 0; }