- 将 CANFD 接收机制由轮询改为 FIFO0 的中断事件驱动处理 - 实现循环取空硬件 FIFO0,提高接收效率和响应速度 - 引入环形缓冲区缓存接收帧,支持多路通道多帧队列管理 - 置位解析任务标志位,实现接收数据的异步解析 - 增加接收中断统计,包括 FIFO 满和溢出计数 - 重构临界区代码,使用自定义函数保护中断状态 - 修改接收过滤器配置,支持按掩码匹配标准帧进入 FIFO0 - 使能 CAN FIFO0 多种中断标志,包括非空、满和溢出中断
149 lines
4.0 KiB
C++
149 lines
4.0 KiB
C++
/**
|
|
* @file task_manager.cpp
|
|
* @brief 任务调度管理器实现。
|
|
*/
|
|
|
|
#include "main.h"
|
|
#include "sys/task_manager.h"
|
|
#include "sys/config.h"
|
|
|
|
namespace {
|
|
/**
|
|
* @brief Enter a critical section and preserve the previous interrupt mask.
|
|
* @param[in] 无。
|
|
* @param[out] 无。
|
|
* @retval uint32_t Previous PRIMASK value.
|
|
*/
|
|
static uint32_t enterCriticalSection()
|
|
{
|
|
const uint32_t primask = __get_PRIMASK();
|
|
__disable_irq();
|
|
return primask;
|
|
}
|
|
|
|
/**
|
|
* @brief Exit a critical section by restoring the saved interrupt mask.
|
|
* @param[in] primask Previous PRIMASK value returned by `enterCriticalSection()`.
|
|
* @param[out] 无。
|
|
* @retval 无返回值。
|
|
*/
|
|
static void exitCriticalSection(uint32_t primask)
|
|
{
|
|
__set_PRIMASK(primask);
|
|
}
|
|
} // namespace
|
|
|
|
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_heartbeat = 0U;
|
|
}
|
|
|
|
/**
|
|
* @brief 置位一个或多个待执行任务标志。
|
|
* @param[in] taskMask 待置位的任务位图掩码。
|
|
* @param[out] 无。
|
|
* @retval 无返回值。
|
|
* @note 该操作不清除其他已存在的任务位。
|
|
*/
|
|
void TaskManager::set(uint32_t taskMask)
|
|
{
|
|
const uint32_t primask = enterCriticalSection();
|
|
g_scheduler.task_flags |= taskMask;
|
|
exitCriticalSection(primask);
|
|
}
|
|
|
|
/**
|
|
* @brief 原子地取出当前所有待执行任务位图并清零。
|
|
* @param[in] 无。
|
|
* @param[out] 无。
|
|
* @retval uint32_t 返回当前待执行任务位图。
|
|
* @note 内部通过关中断实现对 `task_flags` 的原子交换。
|
|
*/
|
|
uint32_t TaskManager::takePending()
|
|
{
|
|
uint32_t pending;
|
|
const uint32_t primask = enterCriticalSection();
|
|
pending = g_scheduler.task_flags;
|
|
g_scheduler.task_flags &= ~pending;
|
|
exitCriticalSection(primask);
|
|
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_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;
|
|
}
|