- 添加CANFD发送完成中断处理函数,确保只有MTFNERR置位时才计数发送成功 - 新增通道健康检查功能,防止病态通道被持续重发的测试帧反复推回BUS_OFF - 重构CANFD配置结构体,添加TX中断配置项并修正GPIO映射设置 - 移除原有的软件发送队列机制,改为直接硬件发送模式 - 更新项目配置文件中的CANFD源文件路径命名 fix(canfd): 修复CANFD发送计数和错误处理逻辑 - 修正发送计数只在硬件MTFNERR确认后增加,而非请求进入邮箱时 - 改进错误日志输出,添加详细的错误类型和通道状态信息 - 优化总线恢复机制,通过收发器静默-唤醒协助硬件自动恢复 - 添加条件编译开关控制启动时自检和回环测试功能
131 lines
3.6 KiB
C++
131 lines
3.6 KiB
C++
/**
|
|
* @file task_manager.cpp
|
|
* @brief 任务调度管理器实现。
|
|
*/
|
|
|
|
#include "main.h"
|
|
#include "sys/task_manager.h"
|
|
#include "sys/config.h"
|
|
#include "sys/critical.h"
|
|
|
|
task_scheduler_t g_scheduler = {0};
|
|
|
|
volatile uint32_t g_app_uptime_sec = 0U;
|
|
|
|
/**
|
|
* @brief 初始化任务调度器内部状态。
|
|
* @param[in] 无。
|
|
* @param[out] 无。
|
|
* @retval 无返回值。
|
|
* @note 清零待执行任务位图以及所有分频节拍计数器。
|
|
*/
|
|
void TaskManager::init()
|
|
{
|
|
g_scheduler.task_flags = 0U;
|
|
g_scheduler.tick_100us = 0U;
|
|
g_scheduler.tick_1ms = 0U;
|
|
g_scheduler.tick_10ms = 0U;
|
|
g_scheduler.tick_100ms = 0U;
|
|
g_scheduler.tick_1s = 0U;
|
|
g_scheduler.div_vref_sample = 0U;
|
|
g_scheduler.div_temp_sample = 0U;
|
|
g_scheduler.div_can_rx = 0U;
|
|
g_scheduler.div_can_tx = 0U;
|
|
g_scheduler.div_status_led = 0U;
|
|
g_scheduler.div_heartbeat = 0U;
|
|
}
|
|
|
|
/**
|
|
* @brief 置位一个或多个待执行任务标志。
|
|
* @param[in] taskMask 待置位的任务位图掩码。
|
|
* @param[out] 无。
|
|
* @retval 无返回值。
|
|
* @note 该操作不清除其他已存在的任务位。
|
|
*/
|
|
void TaskManager::set(uint32_t taskMask)
|
|
{
|
|
uint32_t save = RT_CRITICAL_SAVE();
|
|
g_scheduler.task_flags |= taskMask;
|
|
RT_CRITICAL_RESTORE(save);
|
|
}
|
|
|
|
/**
|
|
* @brief 原子地取出当前所有待执行任务位图并清零。
|
|
* @param[in] 无。
|
|
* @param[out] 无。
|
|
* @retval uint32_t 返回当前待执行任务位图。
|
|
* @note 内部通过关中断实现对 `task_flags` 的原子交换。
|
|
*/
|
|
uint32_t TaskManager::takePending()
|
|
{
|
|
uint32_t pending;
|
|
uint32_t save = RT_CRITICAL_SAVE();
|
|
pending = g_scheduler.task_flags;
|
|
g_scheduler.task_flags &= ~pending;
|
|
RT_CRITICAL_RESTORE(save);
|
|
return pending;
|
|
}
|
|
|
|
/**
|
|
* @brief 推进一次 100us 调度节拍。
|
|
* @param[in] 无。
|
|
* @param[out] 无。
|
|
* @retval 无返回值。
|
|
* @note 按预设周期推进节拍并置位 ADC、状态灯、心跳与诊断打印等任务;CAN 接收改由 RX 中断事件驱动。
|
|
*/
|
|
void TaskManager::tick100us()
|
|
{
|
|
g_scheduler.tick_100us++;
|
|
|
|
if(++g_scheduler.div_vref_sample >= CONFIG_TASK_PERIOD_VREF_SAMPLE_TICKS) {
|
|
g_scheduler.div_vref_sample = 0U;
|
|
set(1UL << TASK_ADC_VREF_SAMPLE);
|
|
}
|
|
|
|
if(++g_scheduler.div_temp_sample >= CONFIG_TASK_PERIOD_TEMP_SAMPLE_TICKS) {
|
|
g_scheduler.div_temp_sample = 0U;
|
|
g_scheduler.tick_1ms++;
|
|
set((1UL << TASK_ADC_TEMP_SAMPLE) | (1UL << TASK_ADC_PROCESS));
|
|
}
|
|
|
|
if(++g_scheduler.div_can_rx >= CONFIG_TASK_PERIOD_CAN_RX_TICKS) {
|
|
g_scheduler.div_can_rx = 0U;
|
|
g_scheduler.tick_10ms++;
|
|
}
|
|
|
|
if(++g_scheduler.div_can_tx >= CONFIG_TASK_PERIOD_CAN_TX_TICKS) {
|
|
g_scheduler.div_can_tx = 0U;
|
|
g_scheduler.tick_1ms++;
|
|
set(1UL << TASK_CAN_TX_SEND);
|
|
}
|
|
|
|
if(++g_scheduler.div_status_led >= CONFIG_TASK_PERIOD_STATUS_LED_TICKS) {
|
|
g_scheduler.div_status_led = 0U;
|
|
g_scheduler.tick_100ms++;
|
|
set(1UL << TASK_STATUS_LED);
|
|
}
|
|
|
|
if(++g_scheduler.div_heartbeat >= CONFIG_TASK_PERIOD_HEARTBEAT_TICKS) {
|
|
g_scheduler.div_heartbeat = 0U;
|
|
g_scheduler.tick_1s++;
|
|
g_app_uptime_sec++;
|
|
#if (CONFIG_UART_TEMP_PERIODIC_PRINT != 0)
|
|
set((1UL << TASK_HEARTBEAT) | (1UL << TASK_DIAGNOSTIC_PRINT));
|
|
#else
|
|
set(1UL << TASK_HEARTBEAT);
|
|
#endif
|
|
}
|
|
}
|
|
|
|
/**
|
|
* @brief 获取全局调度器状态对象引用。
|
|
* @param[in] 无。
|
|
* @param[out] 无。
|
|
* @retval task_scheduler_t& 返回全局调度器状态对象引用。
|
|
* @note 供调试或其他模块观察当前节拍与任务位图。
|
|
*/
|
|
task_scheduler_t &TaskManager::state()
|
|
{
|
|
return g_scheduler;
|
|
}
|