20230201_1811_emb/1811Project_LPc/Src/Usr/Task/Task-GuangDian/WatchDogTask.c
Rjh913828050 78f3903d1d 类型:重构
内容:1811项目下位机软件第一版完整程序
人员:任家豪
2023-10-08 13:56:23 +08:00

95 lines
2.7 KiB
C
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#include "WatchDogTask.h"
#include "FileSysTask.h"
#include "McuBspWatchdog.h"
#include <cpu.h>
#include <os.h>
#include <stdio.h>
#include <string.h>
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;
}