- 将 CANFD 接收机制由轮询改为 FIFO0 的中断事件驱动处理 - 实现循环取空硬件 FIFO0,提高接收效率和响应速度 - 引入环形缓冲区缓存接收帧,支持多路通道多帧队列管理 - 置位解析任务标志位,实现接收数据的异步解析 - 增加接收中断统计,包括 FIFO 满和溢出计数 - 重构临界区代码,使用自定义函数保护中断状态 - 修改接收过滤器配置,支持按掩码匹配标准帧进入 FIFO0 - 使能 CAN FIFO0 多种中断标志,包括非空、满和溢出中断
312 lines
11 KiB
C++
312 lines
11 KiB
C++
/**
|
|
* @file task_runner.cpp
|
|
* @brief 任务执行器实现。
|
|
*/
|
|
|
|
#include "main.h"
|
|
#include "device/adc.h"
|
|
#include "device/watchdog.h"
|
|
#include "device/app_spi_command.h"
|
|
#include "device/canfd.h"
|
|
#include "device/temp_sensor.h"
|
|
#include "sys/config.h"
|
|
#include "sys/ota_boot_request.h"
|
|
#include "sys/task_manager.h"
|
|
#include "sys/task_runner.h"
|
|
|
|
namespace {
|
|
constexpr uint32_t kAdcFilterWindow = CONFIG_ADC_FILTER_SIZE + 1U;
|
|
|
|
/**
|
|
* @brief Push one sample into a fixed-size sliding window buffer.
|
|
* @param[in,out] buffer Sample buffer with `kAdcFilterWindow` elements.
|
|
* @param[in] value New sample value.
|
|
*/
|
|
static void pushSample(float *buffer, float value)
|
|
{
|
|
for(uint32_t i = 0U; i < CONFIG_ADC_FILTER_SIZE; ++i) {
|
|
buffer[i] = buffer[i + 1U];
|
|
}
|
|
buffer[CONFIG_ADC_FILTER_SIZE] = value;
|
|
}
|
|
|
|
/**
|
|
* @brief Apply median filtering to a fixed-size sample window.
|
|
* @param[in] buffer Sample buffer with `kAdcFilterWindow` elements.
|
|
* @return Median value in the current window.
|
|
*/
|
|
static float medianFilter(const float *buffer)
|
|
{
|
|
float sorted[kAdcFilterWindow];
|
|
|
|
for(uint32_t i = 0U; i < kAdcFilterWindow; ++i) {
|
|
sorted[i] = buffer[i];
|
|
}
|
|
|
|
for(uint32_t i = 1U; i < kAdcFilterWindow; ++i) {
|
|
const float key = sorted[i];
|
|
uint32_t j = i;
|
|
while((j > 0U) && (sorted[j - 1U] > key)) {
|
|
sorted[j] = sorted[j - 1U];
|
|
--j;
|
|
}
|
|
sorted[j] = key;
|
|
}
|
|
|
|
return sorted[kAdcFilterWindow / 2U];
|
|
}
|
|
} // namespace
|
|
|
|
struct TaskRunner::Runtime {
|
|
temp_sensor_data_t sample{};
|
|
float temperature_samples[kAdcFilterWindow] = {0.0f};
|
|
float vdda_samples[kAdcFilterWindow] = {0.0f};
|
|
float vin_adc_samples[kAdcFilterWindow] = {0.0f};
|
|
uint16_t last_vin_raw = 0U;
|
|
uint16_t last_vref_raw = 0U;
|
|
uint16_t last_temp_raw = 0U;
|
|
uint8_t last_rx_channel = 0U;
|
|
uint32_t last_rx_id = 0U;
|
|
uint8_t last_rx_len = 0U;
|
|
uint8_t last_rx_data[64] = {0};
|
|
};
|
|
|
|
TaskRunner::Runtime TaskRunner::s_runtime;
|
|
|
|
/**
|
|
* @brief 采样 VIN 与 VREF 原始 ADC 数值。
|
|
* @param[in] 无。
|
|
* @param[out] 无。
|
|
* @retval 无返回值。
|
|
* @note 从 ADC DMA 缓冲中读取外部 VIN_ADC 与内部 VREFINT 通道最新值。
|
|
*/
|
|
void TaskRunner::taskAdcVrefSample()
|
|
{
|
|
s_runtime.last_vin_raw = Adc::readVinRaw();
|
|
s_runtime.last_vref_raw = Adc::readVrefRaw();
|
|
}
|
|
|
|
/**
|
|
* @brief 采样内部温度通道原始 ADC 数值。
|
|
* @param[in] 无。
|
|
* @param[out] 无。
|
|
* @retval 无返回值。
|
|
* @note 读取 ADC DMA 缓冲中的内部温度通道最新值。
|
|
*/
|
|
void TaskRunner::taskAdcTempSample()
|
|
{
|
|
s_runtime.last_temp_raw = Adc::readTempRaw();
|
|
}
|
|
|
|
/**
|
|
* @brief 根据原始采样值计算工程量结果。
|
|
* @param[in] 无。
|
|
* @param[out] 无。
|
|
* @retval 无返回值。
|
|
* @note 输出 `vin_adc_v`、`vdda` 和 `temperature` 等可直接使用的工程值。
|
|
*/
|
|
void TaskRunner::taskAdcProcess()
|
|
{
|
|
auto &s = s_runtime.sample;
|
|
const float vdda = Adc::calculateVdda(s_runtime.last_vref_raw);
|
|
const float vin_adc_v = Adc::calculateVinAdcVoltage(s_runtime.last_vin_raw, vdda);
|
|
const float temperature = Adc::calculateTemperature(s_runtime.last_temp_raw, vdda);
|
|
const bool sampleValid = (s_runtime.last_vref_raw > 0U) &&
|
|
(s_runtime.last_vref_raw < 4095U) &&
|
|
(s_runtime.last_temp_raw > 0U) &&
|
|
(s_runtime.last_temp_raw < 4095U) &&
|
|
(vdda >= 2.7f) && (vdda <= 3.6f) &&
|
|
(temperature >= -40.0f) && (temperature <= 125.0f);
|
|
|
|
if(sampleValid) {
|
|
pushSample(s_runtime.vdda_samples, vdda);
|
|
pushSample(s_runtime.vin_adc_samples, vin_adc_v);
|
|
pushSample(s_runtime.temperature_samples, temperature);
|
|
}
|
|
|
|
s.vin_raw = s_runtime.last_vin_raw;
|
|
s.vref_raw = s_runtime.last_vref_raw;
|
|
s.temp_raw = s_runtime.last_temp_raw;
|
|
s.vdda = medianFilter(s_runtime.vdda_samples);
|
|
s.vin_adc_v = medianFilter(s_runtime.vin_adc_samples);
|
|
s.temperature = medianFilter(s_runtime.temperature_samples);
|
|
s.valid = sampleValid ? 1U : 0U;
|
|
}
|
|
|
|
/**
|
|
* @brief 执行兼容保留的 CAN 接收轮询任务。
|
|
* @param[in] 无。
|
|
* @param[out] 无。
|
|
* @retval 无返回值。
|
|
* @note 当前三路 CANFD 接收已由 RX0 中断驱动,该任务仅保留接口兼容性。
|
|
*/
|
|
void TaskRunner::taskCanRxPoll() { CanFd::poll(); }
|
|
|
|
/**
|
|
* @brief 执行预留的 CAN 发送执行任务。
|
|
* @param[in] 无。
|
|
* @param[out] 无。
|
|
* @retval 无返回值。
|
|
* @note 当前阶段不做周期性发送调度;若后续需要显式验证某一路发送能力,可由调用方先 prepare,再直接调用 `CanFd::sendPrepared()`。
|
|
*/
|
|
void TaskRunner::taskCanTxSend()
|
|
{
|
|
/* 当前阶段不做周期性发送调度。 */
|
|
}
|
|
|
|
/**
|
|
* @brief 批量解析并消费待处理的 CAN 接收报文。
|
|
* @param[in] 无。
|
|
* @param[out] 无。
|
|
* @retval 无返回值。
|
|
* @note 一次任务按预算尽量清空软件接收队列;若仍有积压,则重新置位解析任务。
|
|
*/
|
|
void TaskRunner::taskCanRxParse()
|
|
{
|
|
uint32_t consumed = 0U;
|
|
while(consumed < CONFIG_CAN_RX_PARSE_BUDGET) {
|
|
if(!CanFd::consumeRxFrame(&s_runtime.last_rx_id, s_runtime.last_rx_data, &s_runtime.last_rx_len, &s_runtime.last_rx_channel)) {
|
|
break;
|
|
}
|
|
if(OtaBootRequest::handleCanFrame(s_runtime.last_rx_channel,
|
|
s_runtime.last_rx_id,
|
|
s_runtime.last_rx_data,
|
|
s_runtime.last_rx_len)) {
|
|
OtaBootRequest::process();
|
|
return;
|
|
}
|
|
consumed++;
|
|
}
|
|
|
|
if(CanFd::hasRxFrame()) {
|
|
TaskManager::set(1UL << TASK_CAN_RX_PARSE);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* @brief 执行预留的 CAN 发送准备任务。
|
|
* @param[in] 无。
|
|
* @param[out] 无。
|
|
* @retval 无返回值。
|
|
* @note 当前阶段三路 CANFD 仅要求底层收发能力可用,不做周期性业务状态广播。
|
|
*/
|
|
void TaskRunner::taskCanTxPrepare()
|
|
{
|
|
/* 当前阶段不做周期性业务状态广播。 */
|
|
}
|
|
|
|
/**
|
|
* @brief 执行状态灯维护任务。
|
|
* @param[in] 无。
|
|
* @param[out] 无。
|
|
* @retval 无返回值。
|
|
* @note 当前仅保留任务入口。
|
|
*/
|
|
void TaskRunner::taskStatusLed() { /* 预留 */ }
|
|
|
|
/**
|
|
* @brief 执行系统心跳维护任务。
|
|
* @param[in] 无。
|
|
* @param[out] 无。
|
|
* @retval 无返回值。
|
|
* @note 当前仅保留任务入口。
|
|
*/
|
|
void TaskRunner::taskHeartbeat() { OtaBootRequest::process(); }
|
|
|
|
/**
|
|
* @brief 输出当前温度、电压与 CAN 诊断信息。
|
|
* @param[in] 无。
|
|
* @param[out] 无。
|
|
* @retval 无返回值。
|
|
* @note 由 1 秒节拍触发,通过 UART 打印当前温度、电压;采用定点整数格式,避免依赖浮点 `printf` 支持。
|
|
*/
|
|
void TaskRunner::taskDiagnosticPrint()
|
|
{
|
|
const auto *diag = CanFd::diag();
|
|
const auto &s = s_runtime.sample;
|
|
const int32_t temp_x100 = static_cast<int32_t>(s.temperature * 100.0f);
|
|
const uint32_t vdda_mV = static_cast<uint32_t>(s.vdda * 1000.0f);
|
|
const uint32_t vin_adc_mV = static_cast<uint32_t>(s.vin_adc_v * 1000.0f);
|
|
|
|
#if (CONFIG_UART_DIAG_LEVEL == 0)
|
|
printf("[APP] uptime=%lus valid=%u, temp_raw=%u, vref_raw=%u, temp=%ld.%02ld C, vdda=%lu.%03lu V, vin_adc=%lu.%03lu V\r\n",
|
|
(unsigned long)g_app_uptime_sec,
|
|
static_cast<unsigned int>(s.valid),
|
|
static_cast<unsigned int>(s.temp_raw),
|
|
static_cast<unsigned int>(s.vref_raw),
|
|
static_cast<long>(temp_x100 / 100),
|
|
static_cast<long>((temp_x100 >= 0 ? temp_x100 : -temp_x100) % 100),
|
|
static_cast<unsigned long>(vdda_mV / 1000U),
|
|
static_cast<unsigned long>(vdda_mV % 1000U),
|
|
static_cast<unsigned long>(vin_adc_mV / 1000U),
|
|
static_cast<unsigned long>(vin_adc_mV % 1000U));
|
|
#else
|
|
printf("[APP] uptime=%lus valid=%u, temp=%ld.%02ld C, vdda=%lu.%03lu V, vin_adc=%lu.%03lu V, rx_ch=%u, rx_id=0x%08lX, rx_len=%u, tx_ch=%u, can_tx=%lu, can_rx=%lu, sw_drop=%lu, hw_full=%lu, hw_over=%lu, tx_drop=%lu, can_err=%lu\r\n",
|
|
(unsigned long)g_app_uptime_sec,
|
|
static_cast<unsigned int>(s.valid),
|
|
static_cast<long>(temp_x100 / 100),
|
|
static_cast<long>((temp_x100 >= 0 ? temp_x100 : -temp_x100) % 100),
|
|
static_cast<unsigned long>(vdda_mV / 1000U),
|
|
static_cast<unsigned long>(vdda_mV % 1000U),
|
|
static_cast<unsigned long>(vin_adc_mV / 1000U),
|
|
static_cast<unsigned long>(vin_adc_mV % 1000U),
|
|
static_cast<unsigned int>(s_runtime.last_rx_channel),
|
|
static_cast<unsigned long>(s_runtime.last_rx_id),
|
|
static_cast<unsigned int>(s_runtime.last_rx_len),
|
|
static_cast<unsigned int>(diag->last_tx_channel),
|
|
static_cast<unsigned long>(diag->tx_count),
|
|
static_cast<unsigned long>(diag->rx_count),
|
|
static_cast<unsigned long>(diag->rx_drop_count),
|
|
static_cast<unsigned long>(diag->rx_fifo_full_count),
|
|
static_cast<unsigned long>(diag->rx_fifo_overfull_count),
|
|
static_cast<unsigned long>(diag->tx_drop_count),
|
|
static_cast<unsigned long>(diag->error_count));
|
|
#endif
|
|
}
|
|
|
|
/**
|
|
* @brief 初始化任务执行器运行时上下文。
|
|
* @param[in] 无。
|
|
* @param[out] 无。
|
|
* @retval 无返回值。
|
|
* @note 执行一次完整 ADC 采样,并同步填充运行时缓存的原始值起点。
|
|
*/
|
|
void TaskRunner::init()
|
|
{
|
|
Adc::collectSample(&s_runtime.sample);
|
|
s_runtime.last_vin_raw = s_runtime.sample.vin_raw;
|
|
s_runtime.last_vref_raw = s_runtime.sample.vref_raw;
|
|
s_runtime.last_temp_raw = s_runtime.sample.temp_raw;
|
|
|
|
for(uint32_t i = 0U; i < kAdcFilterWindow; ++i) {
|
|
s_runtime.temperature_samples[i] = s_runtime.sample.temperature;
|
|
s_runtime.vdda_samples[i] = s_runtime.sample.vdda;
|
|
s_runtime.vin_adc_samples[i] = s_runtime.sample.vin_adc_v;
|
|
}
|
|
}
|
|
|
|
/**
|
|
* @brief 执行当前所有已置位的待处理任务。
|
|
* @param[in] 无。
|
|
* @param[out] 无。
|
|
* @retval 无返回值。
|
|
* @note 先原子取出任务位图;若当前没有待处理任务则直接返回。
|
|
*/
|
|
void TaskRunner::runPending()
|
|
{
|
|
Watchdog::feed();
|
|
|
|
const uint32_t pending = TaskManager::takePending();
|
|
if(pending == 0U) return;
|
|
|
|
if(pending & (1UL << TASK_ADC_VREF_SAMPLE)) taskAdcVrefSample();
|
|
if(pending & (1UL << TASK_ADC_TEMP_SAMPLE)) taskAdcTempSample();
|
|
if(pending & (1UL << TASK_ADC_PROCESS)) taskAdcProcess();
|
|
if(pending & (1UL << TASK_CAN_RX_POLL)) taskCanRxPoll();
|
|
if(pending & (1UL << TASK_CAN_RX_PARSE)) taskCanRxParse();
|
|
if(pending & (1UL << TASK_STATUS_LED)) taskStatusLed();
|
|
if(pending & (1UL << TASK_HEARTBEAT)) taskHeartbeat();
|
|
if(pending & (1UL << TASK_DIAGNOSTIC_PRINT)) taskDiagnosticPrint();
|
|
if(pending & (1UL << TASK_SPI_LOG)) AppSpiCommand::poll();
|
|
}
|