changed bspfdcan

This commit is contained in:
2026-01-09 03:05:55 +08:00
parent 59e545e3f2
commit 93abe2381f
3 changed files with 367 additions and 479 deletions

View File

@@ -1,93 +1,103 @@
#ifndef __BSP_FDCAN_H__
#define __BSP_FDCAN_H__
#ifndef BSP_CAN_H
#define BSP_CAN_H
//在此选择CAN类型两者只能选择一个
#define FDCAN //G系列和H7系列使用FDCAN
//#define BXCAN //F系列使用BxCAN
//CAN类型宏定义检查有错误停止编译
#if !defined(FDCAN) && !defined(BXCAN)
#error "Neither FDCAN nor BXCAN is defined. Please define one of them."
#elif defined(FDCAN) && defined(BXCAN)
#error "Both FDCAN and BXCAN are defined. Please define only one."
#endif
#include "main.h"
#include "fdcan.h"
#include <stdint.h>
#ifdef FDCAN
#include "fdcan.h"
#define hcan1 hfdcan1
#define hcan2 hfdcan2
#define hcan3 hfdcan3
#define CAN_MX_REGISTER_CNT 16 // 最大FDCAN设备注册数量
#define MX_CAN_FILTER_CNT (3 * 14) // 最多可以使用的CAN过滤器数量支持3个CAN
#define DEVICE_CAN_CNT 3 // FDCAN1, FDCAN2, FDCAN3
#define CAN_MX_REGISTER_CNT 16 // 这个数量取决于CAN总线的负载
#define MX_CAN_FILTER_CNT (3 * 14) // 最多可以使用的CAN过滤器数量,目前远不会用到这么多
#define DEVICE_CAN_CNT 3 //H723VG有3个FDCAN
#define CAN_CLASS 0
#define CAN_FD_BRS 1
#endif
#ifdef BXCAN
#include "can.h"
// 最多能够支持的CAN设备数
#define CAN_MX_REGISTER_CNT 16 // 这个数量取决于CAN总线的负载
#define MX_CAN_FILTER_CNT (2 * 14) // 最多可以使用的CAN过滤器数量,目前远不会用到这么多
#define DEVICE_CAN_CNT 2 // 根据板子设定,F407IG有CAN1,CAN2,因此为2;F334只有一个,则设为1
// 如果只有1个CAN,还需要把bsp_can.c中所有的hcan2变量改为hcan1(别担心,主要是总线和FIFO的负载均衡,不影响功能)
#endif
#define CAN_BR_125K 0
#define CAN_BR_200K 1
#define CAN_BR_250K 2
#define CAN_BR_500K 3
#define CAN_BR_1M 4
#define CAN_BR_2M 5
#define CAN_BR_2M5 6
#define CAN_BR_3M2 7
#define CAN_BR_4M 8
#define CAN_BR_5M 9
/* FDCAN实例结构体每个注册到FDCAN的模块都应该有这个变量 */
/* can instance typedef, every module registered to CAN should have this variable */
#pragma pack(1)
typedef struct _FDCANInstance {
FDCAN_HandleTypeDef *can_handle; // FDCAN句柄
FDCAN_TxHeaderTypeDef txconf; // FDCAN报文发送配置
uint32_t tx_id; // 发送id
uint32_t tx_mailbox; // FDCAN消息填入的邮箱号
uint8_t tx_buff[64]; // 发送缓存FDCAN最大支持64字节
uint8_t rx_buff[64]; // 接收缓存FDCAN最大支持64字节
uint32_t rx_id; // 接收id
uint8_t rx_len; // 接收长度
// 接收的回调函数,用于解析接收到的数据
void (*can_module_callback)(struct _FDCANInstance *); // 回调函数需要实例来区分注册的设备
void *id; // 使用FDCAN外设的模块指针
typedef struct _
{
#ifdef FDCAN
FDCAN_HandleTypeDef *can_handle; // can句柄
FDCAN_TxHeaderTypeDef txconf; // CAN报文发送配置
#else
CAN_HandleTypeDef *can_handle; // can句柄
CAN_TxHeaderTypeDef txconf; // CAN报文发送配置
#endif
uint32_t tx_id; // 发送id
uint32_t tx_mailbox; // CAN消息填入的邮箱号
uint8_t tx_buff[8]; // 发送缓存,发送消息长度可以通过CANSetDLC()设定,最大为8
uint8_t rx_buff[8]; // 接收缓存,最大消息长度为8
uint32_t rx_id; // 接收id
uint8_t rx_len; // 接收长度,可能为0-8
// 接收的回调函数,用于解析接收到的数据
void (*can_module_callback)(struct _ *); // callback needs an instance to tell among registered ones
void *id; // 使用can外设的模块指针(即id指向的模块拥有此can实例,是父子关系)
} FDCANInstance;
#pragma pack()
/* FDCAN实例初始化结构体将此结构体指针传入注册函数 */
typedef struct {
FDCAN_HandleTypeDef *can_handle; // FDCAN句柄
uint32_t tx_id; // 发送id
uint32_t rx_id; // 接收id
/* CAN实例初始化结构体,将此结构体指针传入注册函数 */
typedef struct
{
#ifdef FDCAN
FDCAN_HandleTypeDef *can_handle; // can句柄
#else
CAN_HandleTypeDef *can_handle; // can句柄
#endif
uint32_t tx_id; // 发送id
uint32_t rx_id; // 接收id
void (*can_module_callback)(FDCANInstance *); // 处理接收数据的回调函数
void *id; // 拥有FDCAN实例的模块地址
void *id; // 拥有can实例的模块地址,用于区分不同的模块(如果有需要的话),如果不需要可以不传入
} FDCAN_Init_Config_s;
/**
* @brief 注册一个模块到FDCAN服务在使用FDCAN设备前调用
* @param config 初始化配置
* @return FDCANInstance* 模块拥有的FDCAN实例
* @brief Register a module to CAN service,remember to call this before using a CAN device
* 注册(初始化)一个can实例,需要传入初始化配置的指针.
* @param config init config
* @return CANInstance* can instance owned by module
*/
FDCANInstance *FDCANRegister(FDCAN_Init_Config_s *config);
FDCANInstance *CANRegister(FDCAN_Init_Config_s *config);
/**
* @brief 修改FDCAN发送报文的数据帧长度
* @param _instance 要修改长度的FDCAN实例
* @param length 设定长度FDCAN最大支持64字节
* @brief 修改CAN发送报文的数据帧长度;注意最大长度为8,在没有进行修改的时候,默认长度为8
*
* @param _instance 要修改长度的can实例
* @param length 设定长度
*/
void FDCANSetDLC(FDCANInstance *_instance, uint8_t length);
void CANSetDLC(FDCANInstance *_instance, uint8_t length);
/**
* @brief 通过FDCAN实例发送消息
* 发送前需要向FDCAN实例的tx_buff写入发送数据
* @brief transmit mesg through CAN device,通过can实例发送消息
* 发送前需要向CAN实例的tx_buff写入发送数据
*
* @attention 超时时间不应该超过调用此函数的任务的周期,否则会导致任务阻塞
*
* @param _instance 模块拥有的FDCAN实例
* @param timeout 超时时间,单位为ms
* @return uint8_t 发送成功返回1,失败返回0
* @param timeout 超时时间,单位为ms;后续改为us,获得更精确的控制
* @param _instance* can instance owned by module
*/
uint8_t FDCANTransmit(FDCANInstance *_instance, float timeout);
uint8_t CANTransmit(FDCANInstance *_instance,float timeout);
/**
* @brief 设置FDCAN波特率
* @param hfdcan FDCAN句柄
* @param mode CAN模式CAN_CLASS或CAN_FD_BRS
* @param baud 波特率选择
*/
void bsp_fdcan_set_baud(FDCAN_HandleTypeDef *hfdcan, uint8_t mode, uint8_t baud);
// 弱定义的接收回调函数,用户可重写
__weak void fdcan1_rx_callback(void);
__weak void fdcan2_rx_callback(void);
__weak void fdcan3_rx_callback(void);
#endif /* __BSP_FDCAN_H_ */
#endif