sentry_chassis_hzz/bsp/bsp_can.h

63 lines
2.1 KiB
C
Raw Normal View History

2022-10-20 17:13:02 +08:00
#ifndef BSP_CAN_H
#define BSP_CAN_H
#include <stdint-gcc.h>
2022-10-20 17:13:02 +08:00
#include "can.h"
#define MX_REGISTER_DEVICE_CNT 12 // maximum number of device can be registered to CAN service
// this number depends on the load of CAN bus.
#define MX_CAN_FILTER_CNT (2 * 14) // temporarily useless
2022-10-20 17:13:02 +08:00
#define DEVICE_CAN_CNT 2 // CAN1,CAN2
/* can instance typedef, every module registered to CAN should have this variable */
#pragma pack(1)
2022-10-30 22:19:13 +08:00
typedef struct _
2022-10-20 17:13:02 +08:00
{
2022-12-02 23:10:36 +08:00
CAN_HandleTypeDef *can_handle; // can句柄
CAN_TxHeaderTypeDef txconf; // CAN报文发送配置
uint32_t tx_id; // 发送id
uint32_t tx_mailbox; // CAN消息填入的邮箱号
uint8_t tx_buff[8]; // 发送缓存,最大为8
uint8_t rx_buff[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
2022-10-20 17:13:02 +08:00
} can_instance;
#pragma pack()
2022-10-20 17:13:02 +08:00
2022-12-02 23:10:36 +08:00
/* this structure is used for initialization */
typedef struct
2022-10-30 22:19:13 +08:00
{
CAN_HandleTypeDef *can_handle;
2022-10-30 22:19:13 +08:00
uint32_t tx_id;
uint32_t rx_id;
void (*can_module_callback)(can_instance *);
} can_instance_config_s;
2022-10-30 22:19:13 +08:00
2022-12-02 23:10:36 +08:00
/**
* @brief CAN发送报文的数据帧长度;8,,8
*
* @param _instance can实例
* @param length
*/
void CANSetDLC(can_instance *_instance, uint8_t length);
2022-10-30 22:19:13 +08:00
2022-10-20 17:13:02 +08:00
/**
2022-12-02 23:10:36 +08:00
* @brief transmit mesg through CAN device,can实例发送消息
* CAN实例的tx_buff写入发送数据
*
2022-12-02 23:10:36 +08:00
* @param _instance* can instance owned by module
2022-10-20 17:13:02 +08:00
*/
void CANTransmit(can_instance *_instance);
2022-10-30 22:19:13 +08:00
2022-10-20 17:13:02 +08:00
/**
* @brief Register a module to CAN service,remember to call this before using a CAN device
2022-12-02 23:10:36 +08:00
* ()can实例,.
2022-10-30 22:19:13 +08:00
* @param config init config
* @return can_instance* can instance owned by module
2022-10-20 17:13:02 +08:00
*/
2022-12-02 23:10:36 +08:00
can_instance *CANRegister(can_instance_config_s *config);
2022-10-20 17:13:02 +08:00
#endif