sick_dt35/User/task/mointor.c
2025-04-02 21:42:58 +08:00

36 lines
1.8 KiB
C

/* Includes ----------------------------------------------------------------- */
#include "device\can.h" // 确保包含 CAN_Debug_t 的定义
#include "task\user_task.h"
#include "bsp\led.h"
#include "cmsis_os.h" // 用于线程操作
/* Private typedef ---------------------------------------------------------- */
/* Private define ----------------------------------------------------------- */
/* Private macro ------------------------------------------------------------ */
/* Private variables -------------------------------------------------------- */
extern can_t can_debug; // 引用 CAN 调试结构体
/* Private function --------------------------------------------------------- */
/* Exported functions ------------------------------------------------------- */
void Task_Monitor(void *argument) {
(void)argument; // 消除未使用参数的警告
const uint32_t delay_tick = osKernelGetTickFreq() / TASK_FREQ_MONITOR; // 100Hz
uint32_t tick = osKernelGetTickCount(); /* 控制任务运行频率的计时 */
uint32_t last_debug_counter = 0; // 上一次的 DebugCounter 值
while (1) {
tick += delay_tick; /* 计算下一个唤醒时刻 */
BSP_LED_SET(BSP_LED1, BSP_LED_TAGGLE); // 切换 LED 状态
// 检查 DebugCounter 是否增加
if (can_debug.DebugCounter == last_debug_counter) {
// 如果 DebugCounter 没有增加,说明 Task_Can 可能卡住
osThreadTerminate(task_runtime.thread.can); // 终止 Task_Can 线程
task_runtime.thread.can = osThreadNew(Task_Can, NULL, NULL); // 重启 Task_Can 线程
} else {
// 如果 DebugCounter 增加,更新 last_debug_counter
last_debug_counter = can_debug.DebugCounter;
}
osDelayUntil(tick); /* 运行结束,等待下一次唤醒 */
}
}