- 将设备相关文件重新组织为device/spi和comm分组 - 添加EtherCAT从站通信模块(esc_exmc_bus、esc_lan9252、ethercat_slave等) - 添加EXMC外设组件支持 - 统一文件命名规范为小写 refactor(canfd): 优化CANFD模块中断安全性和配置方式 - 替换自定义临界区函数为RT_CRITICAL_SAVE/RESTORE宏 - 使用配置常量CONFIG_IRQ_PREEMPT_CAN替代硬编码值 - 简化邮箱满时的处理逻辑,移除不必要的SysTick等待 - 移除废弃的中断保存头文件引用 refactor(timer): 更新定时器配置以使用配置常量 - 修复头文件包含路径大小写问题 - 使用CONFIG_NVIC_PRIORITY_GROUP配置项替代硬编码值
124 lines
3.4 KiB
C++
124 lines
3.4 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_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_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;
|
|
}
|