feat(canfd): 修复CAN发送问题。实现CANFD发送完成中断处理和通道健康检查

- 添加CANFD发送完成中断处理函数,确保只有MTFNERR置位时才计数发送成功
- 新增通道健康检查功能,防止病态通道被持续重发的测试帧反复推回BUS_OFF
- 重构CANFD配置结构体,添加TX中断配置项并修正GPIO映射设置
- 移除原有的软件发送队列机制,改为直接硬件发送模式
- 更新项目配置文件中的CANFD源文件路径命名

fix(canfd): 修复CANFD发送计数和错误处理逻辑

- 修正发送计数只在硬件MTFNERR确认后增加,而非请求进入邮箱时
- 改进错误日志输出,添加详细的错误类型和通道状态信息
- 优化总线恢复机制,通过收发器静默-唤醒协助硬件自动恢复
- 添加条件编译开关控制启动时自检和回环测试功能
This commit is contained in:
cbb
2026-07-11 17:52:05 +08:00
parent 3691cf2aba
commit 10989939e9
9 changed files with 361 additions and 265 deletions
+18 -21
View File
@@ -3,9 +3,6 @@
#include <stdint.h>
/** @brief CANFD 状态报文的数据长度,单位字节。 */
#define CANFD_STATUS_DLEN 12U
/** @brief 每通道反馈快照中保存的最近一帧最大数据长度,受 SPI 应答帧总长度约束。 */
#define CANFD_FEEDBACK_LAST_DATA_MAX 32U
@@ -58,7 +55,7 @@ public:
*/
struct ChannelStats {
uint32_t rx_count; /**< 该通道累计成功接收帧数。 */
uint32_t tx_count; /**< 该通道累计成功发送帧数。 */
uint32_t tx_count; /**< 该通道累计由硬件 MTFNERR 确认的成功发送帧数。 */
uint32_t rx_drop_count; /**< 该通道累计软件丢帧数。 */
uint32_t error_count; /**< 该通道累计错误计数(与发送邮箱失败合并)。 */
uint32_t last_rx_id; /**< 该通道最近一帧报文标识符。 */
@@ -104,15 +101,6 @@ public:
uint8_t count; /**< 当前占用条目数。 */
uint32_t seq; /**< 全局更新序号(每次 refresh 时递增)。 */
};
/**
* @brief CANFD 状态发送载荷。
*/
struct StatusPayload {
uint16_t temp_raw;
uint16_t vref_raw;
float vdda;
float temperature;
};
static void init();
@@ -132,21 +120,32 @@ public:
static Mode mode();
static void poll();
static void prepareStatus(const StatusPayload *status);
static bool prepareStatusForChannel(uint8_t channelIndex, const StatusPayload *status);
static void sendPrepared();
static bool hasRxFrame();
static bool consumeRxFrame(uint32_t *id, uint8_t *data, uint8_t *length, uint8_t *channel = nullptr);
static bool transmitFrame(uint8_t channelIndex, uint32_t ext_id,
const uint8_t *data, uint8_t dlc);
const uint8_t *data, uint8_t dlc, bool periodic = false);
static const Diag *diag();
/**
* @brief 检查指定 CAN 通道当前是否处于健康可发送状态。
* @retval true 未处于 BUS_OFF 且 TEC 未超过预警阈值。
* @retval false 通道处于 BUS_OFF 或 TEC 过高,应避免再向其发送测试帧。
*/
static bool isChannelHealthy(uint8_t channelIndex);
/**
* @brief 刷新三路 1Hz 反馈快照。
* @note 通常由任务调度器在 1 秒节拍调用,将各通道实时计数与最近一帧信息抓拍到 s_feedback。
*/
static void refreshFeedbackSnapshot();
/**
* @brief 保留的手动 BUS_OFF 恢复诊断入口。
* @note 正常周期任务不调用;控制器已启用硬件自动 BUS_OFF 恢复,避免在
* 发送重试期间切换收发器 S 脚而人为制造额外位错误。
*/
static void recoverBusOffIfNeeded();
/**
* @brief 拷贝指定通道的最新反馈快照到调用方缓冲。
* @param[in] channelIndex 目标 CAN 通道号(0/1/2)。
@@ -167,6 +166,7 @@ public:
static void setSilentMode();
static void onRx0Interrupt();
static void handleRxInterrupt(uint8_t channelIndex);
static void handleTxInterrupt(uint8_t channelIndex);
private:
static constexpr uint8_t kRxQueueDepth = 16U;
@@ -195,8 +195,6 @@ private:
static void configureRxFilter(uint32_t canPeriph, uint8_t filterNumber, Mode mode);
static bool initNominalTiming(uint32_t canPeriph);
static bool initFdMode(uint32_t canPeriph);
static void buildStatusFrame(const StatusPayload *status);
static bool enqueueTxFrame(uint8_t channelIndex, const StatusPayload *status);
static Diag s_diag;
static RxQueue s_rxQueues[3];
@@ -209,6 +207,5 @@ private:
};
using canfd_diag_t = CanFd::Diag;
using canfd_status_payload_t = CanFd::StatusPayload;
#endif /* DEVICE_CANFD_H */
#endif /* DEVICE_CANFD_H */