主体为工作区累积的业务改动(CAN/PDO/桥/SSC/task_manager 等,详目见 config.h 版本注释链),新增 CoE 0x100A 真版本上报与 0x2502 APP→BOOT 总线交接。启动链(startup/SystemInit/initial/systick/hardfault)与 HEAD 逐字节一致;1.6.35~38 的跳转诊断实验(3M 化/BKP 面包屑/ haltWithWatchdog)未提交、已整体回退,补丁存 _revert_backup_20260916/。 真机实测(配对 BOOT 1.1.0.18):刷写 warm jump 与 reset cold jump 均可。
128 lines
3.7 KiB
C++
128 lines
3.7 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;
|
|
g_scheduler.div_diag_print = 0U;
|
|
}
|
|
|
|
/**
|
|
* @brief 置位一个或多个待执行任务标志。
|
|
* @param[in] taskMask 待置位的任务位图掩码。
|
|
* @param[out] 无。
|
|
* @retval 无返回值。
|
|
* @note 该操作不清除其他已存在的任务位;通过 __LDREXW/__STREXW 独占访问循环实现无锁原子置位。
|
|
*/
|
|
void TaskManager::set(uint32_t taskMask)
|
|
{
|
|
uint32_t val;
|
|
do {
|
|
val = __LDREXW(&g_scheduler.task_flags);
|
|
} while (__STREXW(val | taskMask, &g_scheduler.task_flags) != 0U);
|
|
__DMB();
|
|
}
|
|
|
|
/**
|
|
* @brief 原子地取出当前所有待执行任务位图并清零。
|
|
* @param[in] 无。
|
|
* @param[out] 无。
|
|
* @retval uint32_t 返回当前待执行任务位图。
|
|
* @note 通过 __LDREXW/__STREXW 独占访问循环实现对 `task_flags` 的原子读取与清零,彻底避免被 Preempt 0 ISR 抢占丢失标志。
|
|
*/
|
|
uint32_t TaskManager::takePending()
|
|
{
|
|
uint32_t pending;
|
|
do {
|
|
pending = __LDREXW(&g_scheduler.task_flags);
|
|
} while (__STREXW(0U, &g_scheduler.task_flags) != 0U);
|
|
__DMB();
|
|
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;
|
|
set(1UL << TASK_CAN_TX_SEND);
|
|
}
|
|
|
|
if(++g_scheduler.div_heartbeat >= CONFIG_TASK_PERIOD_HEARTBEAT_TICKS) {
|
|
g_scheduler.div_heartbeat = 0U;
|
|
g_scheduler.tick_1s++;
|
|
g_app_uptime_sec++;
|
|
set(1UL << TASK_HEARTBEAT);
|
|
}
|
|
|
|
/* 始终 5s 调度诊断任务:默认静默检查错误增量,PERIODIC_PRINT=1 时打全量摘要 */
|
|
if(++g_scheduler.div_diag_print >= CONFIG_TASK_PERIOD_DIAG_PRINT_TICKS) {
|
|
g_scheduler.div_diag_print = 0U;
|
|
set(1UL << TASK_DIAGNOSTIC_PRINT);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* @brief 获取全局调度器状态对象引用。
|
|
* @param[in] 无。
|
|
* @param[out] 无。
|
|
* @retval task_scheduler_t& 返回全局调度器状态对象引用。
|
|
* @note 供调试或其他模块观察当前节拍与任务位图。
|
|
*/
|
|
task_scheduler_t &TaskManager::state()
|
|
{
|
|
return g_scheduler;
|
|
}
|