Files
tronone-h7-scaffold/User_Code/bsp/fdcan/bsp_fdcan.h

104 lines
4.1 KiB
C
Raw Normal View History

2026-01-09 03:05:55 +08:00
#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
2025-10-26 13:39:49 +08:00
#include <stdint.h>
2026-01-09 03:05:55 +08:00
#ifdef FDCAN
#include "fdcan.h"
#define hcan1 hfdcan1
#define hcan2 hfdcan2
#define hcan3 hfdcan3
#define CAN_MX_REGISTER_CNT 16 // 这个数量取决于CAN总线的负载
#define MX_CAN_FILTER_CNT (3 * 14) // 最多可以使用的CAN过滤器数量,目前远不会用到这么多
#define DEVICE_CAN_CNT 3 //H723VG有3个FDCAN
2025-10-24 15:30:40 +08:00
2026-01-09 03:05:55 +08:00
#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
2025-10-26 13:26:30 +08:00
2025-10-24 15:30:40 +08:00
2026-01-09 03:05:55 +08:00
/* can instance typedef, every module registered to CAN should have this variable */
2025-10-26 13:26:30 +08:00
#pragma pack(1)
2026-01-09 03:05:55 +08:00
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实例,是父子关系)
2025-10-26 13:26:30 +08:00
} FDCANInstance;
#pragma pack()
2026-01-09 03:05:55 +08:00
/* 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
2025-10-26 13:26:30 +08:00
void (*can_module_callback)(FDCANInstance *); // 处理接收数据的回调函数
2026-01-09 03:05:55 +08:00
void *id; // 拥有can实例的模块地址,用于区分不同的模块(如果有需要的话),如果不需要可以不传入
2025-10-26 13:26:30 +08:00
} FDCAN_Init_Config_s;
/**
2026-01-09 03:05:55 +08:00
* @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
2025-10-26 13:26:30 +08:00
*/
2026-01-09 03:05:55 +08:00
FDCANInstance *CANRegister(FDCAN_Init_Config_s *config);
2025-10-26 13:26:30 +08:00
/**
2026-01-09 03:05:55 +08:00
* @brief CAN发送报文的数据帧长度;8,,8
*
* @param _instance can实例
* @param length
2025-10-26 13:26:30 +08:00
*/
2026-01-09 03:05:55 +08:00
void CANSetDLC(FDCANInstance *_instance, uint8_t length);
2025-10-26 13:26:30 +08:00
/**
2026-01-09 03:05:55 +08:00
* @brief transmit mesg through CAN device,can实例发送消息
* CAN实例的tx_buff写入发送数据
2025-10-26 13:39:49 +08:00
*
* @attention ,
*
2026-01-09 03:05:55 +08:00
* @param timeout ,ms;us,
* @param _instance* can instance owned by module
2025-10-26 13:26:30 +08:00
*/
2026-01-09 03:05:55 +08:00
uint8_t CANTransmit(FDCANInstance *_instance,float timeout);
2025-10-24 15:30:40 +08:00
2026-01-09 03:05:55 +08:00
#endif