2026-06-26 00:57:59 +08:00
|
|
|
/**
|
2026-05-10 17:28:14 +08:00
|
|
|
* @file task_manager.cpp
|
|
|
|
|
* @brief 任务调度管理器实现。
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
#include "main.h"
|
|
|
|
|
#include "sys/task_manager.h"
|
|
|
|
|
#include "sys/config.h"
|
2026-06-26 00:57:59 +08:00
|
|
|
#include "sys/critical.h"
|
2026-05-30 00:24:31 +08:00
|
|
|
|
2026-05-10 17:28:14 +08:00
|
|
|
task_scheduler_t g_scheduler = {0};
|
|
|
|
|
|
2026-05-30 00:24:31 +08:00
|
|
|
volatile uint32_t g_app_uptime_sec = 0U;
|
|
|
|
|
|
2026-05-10 21:27:51 +08:00
|
|
|
/**
|
|
|
|
|
* @brief 初始化任务调度器内部状态。
|
|
|
|
|
* @param[in] 无。
|
|
|
|
|
* @param[out] 无。
|
|
|
|
|
* @retval 无返回值。
|
|
|
|
|
* @note 清零待执行任务位图以及所有分频节拍计数器。
|
|
|
|
|
*/
|
2026-05-10 17:28:14 +08:00
|
|
|
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;
|
2026-05-10 21:27:51 +08:00
|
|
|
g_scheduler.div_vref_sample = 0U;
|
|
|
|
|
g_scheduler.div_temp_sample = 0U;
|
|
|
|
|
g_scheduler.div_can_rx = 0U;
|
|
|
|
|
g_scheduler.div_can_tx = 0U;
|
2026-07-11 17:52:05 +08:00
|
|
|
g_scheduler.div_status_led = 0U;
|
2026-05-10 21:27:51 +08:00
|
|
|
g_scheduler.div_heartbeat = 0U;
|
2026-07-15 00:34:56 +08:00
|
|
|
g_scheduler.div_diag_print = 0U;
|
2026-05-10 17:28:14 +08:00
|
|
|
}
|
|
|
|
|
|
2026-05-10 21:27:51 +08:00
|
|
|
/**
|
|
|
|
|
* @brief 置位一个或多个待执行任务标志。
|
|
|
|
|
* @param[in] taskMask 待置位的任务位图掩码。
|
|
|
|
|
* @param[out] 无。
|
|
|
|
|
* @retval 无返回值。
|
|
|
|
|
* @note 该操作不清除其他已存在的任务位。
|
|
|
|
|
*/
|
2026-05-10 17:28:14 +08:00
|
|
|
void TaskManager::set(uint32_t taskMask)
|
|
|
|
|
{
|
2026-06-26 00:57:59 +08:00
|
|
|
uint32_t save = RT_CRITICAL_SAVE();
|
2026-05-10 17:28:14 +08:00
|
|
|
g_scheduler.task_flags |= taskMask;
|
2026-06-26 00:57:59 +08:00
|
|
|
RT_CRITICAL_RESTORE(save);
|
2026-05-10 17:28:14 +08:00
|
|
|
}
|
|
|
|
|
|
2026-05-10 21:27:51 +08:00
|
|
|
/**
|
|
|
|
|
* @brief 原子地取出当前所有待执行任务位图并清零。
|
|
|
|
|
* @param[in] 无。
|
|
|
|
|
* @param[out] 无。
|
|
|
|
|
* @retval uint32_t 返回当前待执行任务位图。
|
|
|
|
|
* @note 内部通过关中断实现对 `task_flags` 的原子交换。
|
|
|
|
|
*/
|
2026-05-10 17:28:14 +08:00
|
|
|
uint32_t TaskManager::takePending()
|
|
|
|
|
{
|
|
|
|
|
uint32_t pending;
|
2026-06-26 00:57:59 +08:00
|
|
|
uint32_t save = RT_CRITICAL_SAVE();
|
2026-05-10 17:28:14 +08:00
|
|
|
pending = g_scheduler.task_flags;
|
|
|
|
|
g_scheduler.task_flags &= ~pending;
|
2026-06-26 00:57:59 +08:00
|
|
|
RT_CRITICAL_RESTORE(save);
|
2026-05-10 17:28:14 +08:00
|
|
|
return pending;
|
|
|
|
|
}
|
|
|
|
|
|
2026-05-10 21:27:51 +08:00
|
|
|
/**
|
|
|
|
|
* @brief 推进一次 100us 调度节拍。
|
|
|
|
|
* @param[in] 无。
|
|
|
|
|
* @param[out] 无。
|
|
|
|
|
* @retval 无返回值。
|
2026-05-30 00:24:31 +08:00
|
|
|
* @note 按预设周期推进节拍并置位 ADC、状态灯、心跳与诊断打印等任务;CAN 接收改由 RX 中断事件驱动。
|
2026-05-10 21:27:51 +08:00
|
|
|
*/
|
2026-05-10 17:28:14 +08:00
|
|
|
void TaskManager::tick100us()
|
|
|
|
|
{
|
|
|
|
|
g_scheduler.tick_100us++;
|
|
|
|
|
|
2026-05-10 21:27:51 +08:00
|
|
|
if(++g_scheduler.div_vref_sample >= CONFIG_TASK_PERIOD_VREF_SAMPLE_TICKS) {
|
|
|
|
|
g_scheduler.div_vref_sample = 0U;
|
2026-05-10 17:28:14 +08:00
|
|
|
set(1UL << TASK_ADC_VREF_SAMPLE);
|
|
|
|
|
}
|
|
|
|
|
|
2026-05-10 21:27:51 +08:00
|
|
|
if(++g_scheduler.div_temp_sample >= CONFIG_TASK_PERIOD_TEMP_SAMPLE_TICKS) {
|
|
|
|
|
g_scheduler.div_temp_sample = 0U;
|
2026-05-10 17:28:14 +08:00
|
|
|
g_scheduler.tick_1ms++;
|
|
|
|
|
set((1UL << TASK_ADC_TEMP_SAMPLE) | (1UL << TASK_ADC_PROCESS));
|
|
|
|
|
}
|
|
|
|
|
|
2026-05-10 21:27:51 +08:00
|
|
|
if(++g_scheduler.div_can_rx >= CONFIG_TASK_PERIOD_CAN_RX_TICKS) {
|
|
|
|
|
g_scheduler.div_can_rx = 0U;
|
2026-05-10 17:28:14 +08:00
|
|
|
g_scheduler.tick_10ms++;
|
|
|
|
|
}
|
|
|
|
|
|
2026-05-10 21:27:51 +08:00
|
|
|
if(++g_scheduler.div_can_tx >= CONFIG_TASK_PERIOD_CAN_TX_TICKS) {
|
|
|
|
|
g_scheduler.div_can_tx = 0U;
|
2026-07-11 17:52:05 +08:00
|
|
|
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;
|
2026-05-10 17:28:14 +08:00
|
|
|
g_scheduler.tick_100ms++;
|
2026-07-11 17:52:05 +08:00
|
|
|
set(1UL << TASK_STATUS_LED);
|
2026-05-10 17:28:14 +08:00
|
|
|
}
|
|
|
|
|
|
2026-05-10 21:27:51 +08:00
|
|
|
if(++g_scheduler.div_heartbeat >= CONFIG_TASK_PERIOD_HEARTBEAT_TICKS) {
|
|
|
|
|
g_scheduler.div_heartbeat = 0U;
|
2026-05-10 17:28:14 +08:00
|
|
|
g_scheduler.tick_1s++;
|
2026-05-30 00:24:31 +08:00
|
|
|
g_app_uptime_sec++;
|
|
|
|
|
set(1UL << TASK_HEARTBEAT);
|
2026-05-10 17:28:14 +08:00
|
|
|
}
|
2026-07-15 00:34:56 +08:00
|
|
|
|
|
|
|
|
#if (CONFIG_UART_TEMP_PERIODIC_PRINT != 0)
|
|
|
|
|
if(++g_scheduler.div_diag_print >= CONFIG_TASK_PERIOD_DIAG_PRINT_TICKS) {
|
|
|
|
|
g_scheduler.div_diag_print = 0U;
|
|
|
|
|
set(1UL << TASK_DIAGNOSTIC_PRINT);
|
|
|
|
|
}
|
|
|
|
|
#endif
|
2026-05-10 17:28:14 +08:00
|
|
|
}
|
|
|
|
|
|
2026-05-10 21:27:51 +08:00
|
|
|
/**
|
|
|
|
|
* @brief 获取全局调度器状态对象引用。
|
|
|
|
|
* @param[in] 无。
|
|
|
|
|
* @param[out] 无。
|
|
|
|
|
* @retval task_scheduler_t& 返回全局调度器状态对象引用。
|
|
|
|
|
* @note 供调试或其他模块观察当前节拍与任务位图。
|
|
|
|
|
*/
|
2026-05-10 17:28:14 +08:00
|
|
|
task_scheduler_t &TaskManager::state()
|
|
|
|
|
{
|
|
|
|
|
return g_scheduler;
|
2026-05-30 00:24:31 +08:00
|
|
|
}
|