Files
DCU_APP/GD32E508_DCU_APP/sys/task_manager.cpp
T

69 lines
1.7 KiB
C++
Raw Normal View History

/**
* @file task_manager.cpp
* @brief 任务调度管理器实现。
*/
#include "main.h"
#include "sys/task_manager.h"
#include "sys/config.h"
task_scheduler_t g_scheduler = {0};
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;
}
void TaskManager::set(uint32_t taskMask)
{
g_scheduler.task_flags |= taskMask;
}
uint32_t TaskManager::takePending()
{
uint32_t pending;
__disable_irq();
pending = g_scheduler.task_flags;
g_scheduler.task_flags &= ~pending;
__enable_irq();
return pending;
}
void TaskManager::tick100us()
{
g_scheduler.tick_100us++;
if((g_scheduler.tick_100us % CONFIG_TASK_PERIOD_VREF_SAMPLE_TICKS) == 0U) {
set(1UL << TASK_ADC_VREF_SAMPLE);
}
if((g_scheduler.tick_100us % CONFIG_TASK_PERIOD_TEMP_SAMPLE_TICKS) == 0U) {
g_scheduler.tick_1ms++;
set((1UL << TASK_ADC_TEMP_SAMPLE) | (1UL << TASK_ADC_PROCESS));
}
if((g_scheduler.tick_100us % CONFIG_TASK_PERIOD_CAN_RX_TICKS) == 0U) {
g_scheduler.tick_10ms++;
set((1UL << TASK_CAN_RX_POLL) | (1UL << TASK_CAN_RX_PARSE));
}
if((g_scheduler.tick_100us % CONFIG_TASK_PERIOD_CAN_TX_TICKS) == 0U) {
g_scheduler.tick_100ms++;
set((1UL << TASK_CAN_TX_PREPARE) | (1UL << TASK_CAN_TX_SEND) | (1UL << TASK_STATUS_LED));
}
if((g_scheduler.tick_100us % CONFIG_TASK_PERIOD_HEARTBEAT_TICKS) == 0U) {
g_scheduler.tick_1s++;
set((1UL << TASK_HEARTBEAT) | (1UL << TASK_DIAGNOSTIC_PRINT));
}
}
task_scheduler_t &TaskManager::state()
{
return g_scheduler;
}