feat(canfd): 切换 CANFD 接收为中断驱动和环形缓冲机制
- 将 CANFD 接收机制由轮询改为 FIFO0 的中断事件驱动处理 - 实现循环取空硬件 FIFO0,提高接收效率和响应速度 - 引入环形缓冲区缓存接收帧,支持多路通道多帧队列管理 - 置位解析任务标志位,实现接收数据的异步解析 - 增加接收中断统计,包括 FIFO 满和溢出计数 - 重构临界区代码,使用自定义函数保护中断状态 - 修改接收过滤器配置,支持按掩码匹配标准帧进入 FIFO0 - 使能 CAN FIFO0 多种中断标志,包括非空、满和溢出中断
This commit is contained in:
@@ -5,14 +5,66 @@
|
||||
|
||||
#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;
|
||||
@@ -30,7 +82,8 @@ TaskRunner::Runtime TaskRunner::s_runtime;
|
||||
*/
|
||||
void TaskRunner::taskAdcVrefSample()
|
||||
{
|
||||
Adc::collectSample(&s_runtime.sample);
|
||||
s_runtime.last_vin_raw = Adc::readVinRaw();
|
||||
s_runtime.last_vref_raw = Adc::readVrefRaw();
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -42,7 +95,7 @@ void TaskRunner::taskAdcVrefSample()
|
||||
*/
|
||||
void TaskRunner::taskAdcTempSample()
|
||||
{
|
||||
Adc::collectSample(&s_runtime.sample);
|
||||
s_runtime.last_temp_raw = Adc::readTempRaw();
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -54,9 +107,30 @@ void TaskRunner::taskAdcTempSample()
|
||||
*/
|
||||
void TaskRunner::taskAdcProcess()
|
||||
{
|
||||
/* 当前采样任务已直接调用 Adc::collectSample() 填充工程值。
|
||||
* 该处理任务保留为调度兼容入口,后续若重新拆分采样/换算阶段,可在此恢复。 */
|
||||
(void)s_runtime.sample;
|
||||
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;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -77,21 +151,36 @@ void TaskRunner::taskCanRxPoll() { CanFd::poll(); }
|
||||
*/
|
||||
void TaskRunner::taskCanTxSend()
|
||||
{
|
||||
/* 当前阶段不做周期性发送调度。
|
||||
* 若后续需要显式验证某一路发送能力,可由调用方先 prepare,再直接调用 CanFd::sendPrepared()。 */
|
||||
/* 当前阶段不做周期性发送调度。 */
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief 解析并消费一帧待处理的 CAN 接收报文。
|
||||
* @brief 批量解析并消费待处理的 CAN 接收报文。
|
||||
* @param[in] 无。
|
||||
* @param[out] 无。
|
||||
* @retval 无返回值。
|
||||
* @note 若当前存在待消费报文,则更新最近一次接收的 ID、数据长度、数据内容与来源通道号。
|
||||
* @note 一次任务按预算尽量清空软件接收队列;若仍有积压,则重新置位解析任务。
|
||||
*/
|
||||
void TaskRunner::taskCanRxParse()
|
||||
{
|
||||
if(CanFd::hasRxFrame())
|
||||
CanFd::consumeRxFrame(&s_runtime.last_rx_id, s_runtime.last_rx_data, &s_runtime.last_rx_len, &s_runtime.last_rx_channel);
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -99,16 +188,11 @@ void TaskRunner::taskCanRxParse()
|
||||
* @param[in] 无。
|
||||
* @param[out] 无。
|
||||
* @retval 无返回值。
|
||||
* @note 当前阶段三路 CANFD 仅要求底层收发能力可用,不做周期性业务状态广播;后续若需要恢复按通道发送,可在此处显式调用 `CanFd::prepareStatusForChannel()` 或 `CanFd::prepareStatus()`。
|
||||
* @note 当前阶段三路 CANFD 仅要求底层收发能力可用,不做周期性业务状态广播。
|
||||
*/
|
||||
void TaskRunner::taskCanTxPrepare()
|
||||
{
|
||||
/* 当前阶段三路 CANFD 只要求底层收发能力可用,不做周期性业务状态广播。
|
||||
* 保留该任务入口,后续若需要恢复按通道发送,可在此处显式调用:
|
||||
* CanFd::prepareStatusForChannel(channel, &status);
|
||||
* 或:
|
||||
* CanFd::prepareStatus(&status);
|
||||
*/
|
||||
/* 当前阶段不做周期性业务状态广播。 */
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -116,7 +200,7 @@ void TaskRunner::taskCanTxPrepare()
|
||||
* @param[in] 无。
|
||||
* @param[out] 无。
|
||||
* @retval 无返回值。
|
||||
* @note 当前仅保留任务入口,具体状态灯策略尚未实现。
|
||||
* @note 当前仅保留任务入口。
|
||||
*/
|
||||
void TaskRunner::taskStatusLed() { /* 预留 */ }
|
||||
|
||||
@@ -125,32 +209,47 @@ void TaskRunner::taskStatusLed() { /* 预留 */ }
|
||||
* @param[in] 无。
|
||||
* @param[out] 无。
|
||||
* @retval 无返回值。
|
||||
* @note 当前仅保留任务入口,后续可在此加入周期健康检查或轻量保活逻辑。
|
||||
* @note 当前仅保留任务入口。
|
||||
*/
|
||||
void TaskRunner::taskHeartbeat() { /* 预留 */ }
|
||||
void TaskRunner::taskHeartbeat() { OtaBootRequest::process(); }
|
||||
|
||||
/**
|
||||
* @brief 输出当前温度、电压与 CAN 诊断信息。
|
||||
* @param[in] 无。
|
||||
* @param[out] 无。
|
||||
* @retval 无返回值。
|
||||
* @note 由 1 秒节拍触发,通过 UART 打印当前温度、电压,以及最近一次 CAN 接收与统计信息。
|
||||
* @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("temp=%.2f C, vdda=%.3f V, vin_adc=%.3f V\r\n",
|
||||
s.temperature,
|
||||
s.vdda,
|
||||
s.vin_adc_v);
|
||||
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("temp=%.2f C, vdda=%.3f V, vin_adc=%.3f V, rx_ch=%u, rx_id=0x%08lX, rx_len=%u, tx_ch=%u, can_tx=%lu, can_rx=%lu, can_drop=%lu, tx_drop=%lu, can_err=%lu\r\n",
|
||||
s.temperature,
|
||||
s.vdda,
|
||||
s.vin_adc_v,
|
||||
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),
|
||||
@@ -158,6 +257,8 @@ void TaskRunner::taskDiagnosticPrint()
|
||||
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
|
||||
@@ -173,6 +274,15 @@ void TaskRunner::taskDiagnosticPrint()
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -184,6 +294,8 @@ void TaskRunner::init()
|
||||
*/
|
||||
void TaskRunner::runPending()
|
||||
{
|
||||
Watchdog::feed();
|
||||
|
||||
const uint32_t pending = TaskManager::takePending();
|
||||
if(pending == 0U) return;
|
||||
|
||||
@@ -195,4 +307,5 @@ void TaskRunner::runPending()
|
||||
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();
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user