Files

55 lines
2.2 KiB
C
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
#ifndef BSP_CAN_H
#define BSP_CAN_H
#include <stdint.h>
#include "fdcan.h"
#ifdef __cplusplus
extern "C" {
#endif
/* 三路总线统一按经典 CAN 标准帧工作:11 位 ID、DLC 0~8、1 Mbps。
* 与 C620 电调、DM-J 系列出厂默认模式一致,不涉及 FD 帧。
* 数据缓冲按 FD 上限 64 预留,将来单路切 FD 时接口不必改。 */
#define CAN_MAX_DATA_LEN 64
#define CAN_RX_CALLBACK_MAX 32
typedef FDCAN_HandleTypeDef fdcan_t;
/* 接收回调在 FDCAN RX0 中断上下文执行,必须短:
* 只做数据拷贝与状态更新,不要在里面做浮点重运算、打印或任何 RTOS 阻塞调用。 */
typedef void (*CanRxCallback)(uint32_t std_id, const uint8_t* data, uint8_t dlc);
/* 每路总线的收发统计,用于判断"丢帧是因为总线带宽不够,还是因为 ISR 太慢"。
* 现场排查优先看 rx_lost 与 tx_fail 是否在涨。 */
typedef struct {
uint32_t rx_frames; /* 已成功递交回调的帧数 */
uint32_t rx_unclaimed; /* 注册表未命中而丢弃的帧数(IDA 配错时最先体现) */
uint32_t rx_lost; /* RX FIFO 溢出,硬件丢帧 */
uint32_t tx_ok; /* 发送成功 */
uint32_t tx_fail; /* 发送失败(TX FIFO 满或总线错误) */
} CanStats;
/* 注册 (总线, 标准 ID) → 回调。
* 必须在 CanStart() 之前完成,避免中断已开而无人认领的那段窗口。
* 返回 0 成功;-1 表示表满或参数非法。 */
int CanRxRegister(fdcan_t* hfdcan, uint32_t std_id, CanRxCallback callback);
/* 配置三路过滤器(全通 + 软件按 ID 分发)并启动,使能 RX FIFO0 中断与丢帧中断。
* 在 FreeRTOS 任务上下文中调用一次,需晚于所有 CanRxRegister()。 */
void CanStart(void);
/* 发送一帧标准数据帧。dlc 为字节数 0~8(经典帧下等同于 DLC 编码)。
* 返回 HAL_OK;HAL_ERROR 表示发送 FIFO 满或总线错误,调用方应决定重试或丢弃。 */
HAL_StatusTypeDef CanSend(fdcan_t* hfdcan, uint32_t std_id, const uint8_t* data, uint8_t dlc);
/* 取某一路的统计快照;hfdcan 不是 hfdcan1/2/3 时返回 NULL。 */
const CanStats* CanGetStats(const fdcan_t* hfdcan);
#ifdef __cplusplus
}
#endif
#endif /* BSP_CAN_H */