1.5wuling_zhuanjietou/HARDWARE/CAN/CanBusDrv.h

268 lines
6.2 KiB
C
Raw Normal View History

2024-10-17 01:06:51 +00:00
/***********************************************************************
* @file name: canbusdrv.h
* @create date: 2022-11-12
* @author
* @version: V 1.0.0
* @remark:
<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>MCU can<EFBFBD>ӿ<EFBFBD>֮<EFBFBD><EFBFBD><EFBFBD>Ľӿ<EFBFBD>
************************************************************************/
#ifndef __CANBUSDRV_H__
#define __CANBUSDRV_H__
/***************************************************************************
* Can <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ϣ
**************************************************************************/
/* ֧<>ֵ<EFBFBD><D6B5><EFBFBD><EFBFBD><EFBFBD>can MCU */
#define BSP_CAN_CHANNEL_NUM (2)
/* <20><><EFBFBD>ݱ<EFBFBD><DDB1>ĵ<EFBFBD><C4B5><EFBFBD><EFBFBD>󳤶<EFBFBD> */
#define BSP_CAN_DATA_LENGTH_MAX (8)
/***************************************************************************
* <EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʹ<EFBFBD>õ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ͷ<EFBFBD><EFBFBD><EFBFBD>
**************************************************************************/
/* <20><>MCU<43><55><EFBFBD>ص<EFBFBD><D8B5><EFBFBD><EFBFBD>Ͷ<EFBFBD><CDB6><EFBFBD> */
#include "porting.h"
#include "string.h"
/* <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ͣ<EFBFBD><CDA3><EFBFBD><EFBFBD>Ҷ<EFBFBD><D2B6><EFBFBD><EFBFBD><EFBFBD>һ<EFBFBD><D2BB><EFBFBD><EFBFBD>Ϣת<CFA2><D7AA>Ϊ<EFBFBD>˽ṹ<CBBD>ĺ<EFBFBD><C4BA><EFBFBD> */
typedef CanTxMsg can_frame_t;
static __inline void CAN_FRAME_INIT(can_frame_t * pt_frame, unsigned int canid,const unsigned char *data, unsigned char len)
{
if (pt_frame != NULL)
{
pt_frame->IDE = 0;
pt_frame->StdId = 0;
pt_frame->ExtId = 0;
if (canid >= 0x800)
{
pt_frame->IDE = CAN_ID_EXT;
pt_frame->ExtId = canid;
}
else
{
pt_frame->IDE = CAN_ID_STD;
pt_frame->StdId = canid;
}
if (len == 0 || data == NULL)
{
pt_frame->DLC = 0;
pt_frame->RTR = CAN_RTR_REMOTE;
}
else
{
pt_frame->RTR = CAN_RTR_DATA;
pt_frame->DLC = len;
memcpy(pt_frame->Data, data, len);
}
}
}
#if 0
/* <20><><EFBFBD><EFBFBD>can<61><6E><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> */
typedef enum {
E_CAN_FRAME_STD_MASK = 0x01,
E_CAN_FRAME_EXT_MASK = 0x02,
E_CAN_FRAME_RTR_MASK = 0x04,
} can_frame_mask_e;
/* <20><><EFBFBD><EFBFBD>ͳһ<CDB3><D2BB>can<61><6E>Ϣ<EFBFBD><CFA2><EFBFBD><EFBFBD> */
typedef struct {
unsigned int id;
can_frame_mask_e frame_type;
#if BSP_CAN_DATA_LENGTH_MAX > 255
unsigned short data_len;
#else
unsigned char data_len;
#endif
unsigned char data[BSP_CAN_DATA_LENGTH_MAX];
} can_frame_t;
static __inline void CAN_FRAME_INIT(can_frame_t * pt_frame, unsigned int canid,const unsigned char *data, unsigned char len)
{
if (pt_frame != NULL)
{
pt_frame->id = canid;
pt_frame->frame_type = 0;
pt_frame->frame_type |= (canid >= 0x800)?E_CAN_FRAME_EXT_MASK:E_CAN_FRAME_STD_MASK;
if (len ==0 || data == NULL)
{
pt_frame->frame_type |= E_CAN_FRAME_RTR_MASK;
pt_frame->data_len = 0;
}
else
{
pt_frame->data_len = (len>BSP_CAN_DATA_LENGTH_MAX)?BSP_CAN_DATA_LENGTH_MAX:len;
memcpy(pt_frame->data, data, pt_frame->data_len);
}
}
}
#endif
/***************************************************************************
* <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>MCU CAN Ӳ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>صĽӿ<EFBFBD>
**************************************************************************/
/**
* <EFBFBD>ײ<EFBFBD>MCU <EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʵ<EFBFBD>ֵĽӿ<EFBFBD>
*/
typedef struct {
unsigned short krate; /* can<61><6E><EFBFBD><EFBFBD><EFBFBD><EFBFBD> */
bool listen; /* <20>Ƿ<EFBFBD><C7B7><EFBFBD><EFBFBD><EFBFBD>ģʽ<C4A3><CABD><EFBFBD><EFBFBD>, true <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD> false <20>Ǽ<EFBFBD><C7BC><EFBFBD> */
bool domain_h; /* <20><><EFBFBD><EFBFBD>Ϊ<EFBFBD>ߣ<EFBFBD><DFA3><EFBFBD><EFBFBD>ǵ<EFBFBD>, true Ϊ<>ߣ<EFBFBD> false Ϊ<><CEAA> */
bool sleep; /* ˯<><CBAF>ģʽ<C4A3><CABD> true Ϊ<><CEAA><EFBFBD>ߣ<EFBFBD> false Ϊ<><CEAA><EFBFBD><EFBFBD><EFBFBD><EFBFBD> */
bool abom; /* <20><><EFBFBD><EFBFBD><EFBFBD>Ƿ<EFBFBD><C7B7>ָ<EFBFBD><D6B8><EFBFBD> true Ϊ<>Զ<EFBFBD><D4B6>ָ<EFBFBD> false Ϊ<><CEAA><EFBFBD>Զ<EFBFBD><D4B6>ָ<EFBFBD> */
}bsp_can_cfg_t;
/**
* @brief: <EFBFBD>ӿڵ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ϣ
*
* @param channel: can ͨ<EFBFBD><EFBFBD><EFBFBD>ţ<EFBFBD> <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>MCU_CAN_CHANNEL_NUM
* @param pt_bsp_cfg[IN]: <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Զ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
*
* @return:
* <EFBFBD><EFBFBD><EFBFBD>ؽ<EFBFBD><EFBFBD><EFBFBD>: true - open <EFBFBD>ɹ<EFBFBD> false - open ʧ<EFBFBD><EFBFBD>
*/
bool bsp_can_open(unsigned char channel, const bsp_can_cfg_t * pt_bsp_cfg);
/**
* @brief: <EFBFBD>رսӿ<EFBFBD>
*
* @return:
* <EFBFBD><EFBFBD><EFBFBD>ؽ<EFBFBD><EFBFBD><EFBFBD>: true - open <EFBFBD>ɹ<EFBFBD> false - open ʧ<EFBFBD><EFBFBD>
*/
bool bsp_can_close(unsigned char channel);
#define BSP_CAN_SEND_FAIL (0xFF)
/**
* @brief: <EFBFBD><EFBFBD><EFBFBD>ͽӿ<EFBFBD>
*
* @param channel: ͨ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>
* @param pt_fram[IN]: <EFBFBD><EFBFBD><EFBFBD>͵<EFBFBD><EFBFBD><EFBFBD>Ϣ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>
*
* @return:
* <EFBFBD><EFBFBD><EFBFBD>ؽ<EFBFBD><EFBFBD><EFBFBD>: <EFBFBD><EFBFBD><EFBFBD>ͳɹ<EFBFBD>(<EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ĵ<EFBFBD><EFBFBD><EFBFBD>)<EFBFBD><EFBFBD>FIFO id
* ʧ<EFBFBD>ܷ<EFBFBD><EFBFBD><EFBFBD>0xFF
*/
unsigned char bsp_can_msg_send(unsigned char channel, const can_frame_t * pt_fram);
/**
* @brief: <EFBFBD><EFBFBD><EFBFBD>յ<EFBFBD><EFBFBD><EFBFBD>Ϣ<EFBFBD><EFBFBD>HOOK
*
* @param channel: ͨ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>
* @param pt_fram[IN]: <EFBFBD><EFBFBD><EFBFBD>յ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ϣ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>
*
* @return:
*/
void bsp_can_msg_receive_hook(unsigned char channel, const can_frame_t * pt_fram);
/**
* @brief: <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ϣ<EFBFBD><EFBFBD><EFBFBD>ɵ<EFBFBD>hook
*
* @param channel: ͨ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>
* @param fifo_id: <EFBFBD><EFBFBD>Ϣ<EFBFBD>յ<EFBFBD>ID
* @param success: true <EFBFBD><EFBFBD><EFBFBD>ͳɹ<EFBFBD><EFBFBD><EFBFBD> false <EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʧ<EFBFBD><EFBFBD>
*
* @return: void
*
* @remark: <EFBFBD><EFBFBD><EFBFBD><EFBFBD>FIFO<EFBFBD>յ<EFBFBD>hook
*/
void bsp_can_msg_sent_hook(unsigned char channel, unsigned char fifo_id, bool success);
/**
* @brief: <EFBFBD><EFBFBD><EFBFBD><EFBFBD>PASSIVE hook
*
* @param channel: ͨ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>
*
* @return: void
*/
void bsp_can_passive_err_hook(unsigned char channel);
/**
* @brief: <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>л<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ӧ״̬hook<EFBFBD><EFBFBD><EFBFBD><EFBFBD>
*
* @param channel: can ͨ<EFBFBD><EFBFBD>,
*
* @return: void
*
* @remark: <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ӧ<EFBFBD><EFBFBD><EFBFBD>л<EFBFBD>δ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ӧ<EFBFBD><EFBFBD>ʱ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>һ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>
*/
void can_bus_err_passive_hook(unsigned char channel);
/***************************************************************************
* <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ӿ<EFBFBD>
**************************************************************************/
/**
* @brief: <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ݱ<EFBFBD><EFBFBD><EFBFBD>
*
* @param chan: can ͨ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>
* @param canid: <EFBFBD><EFBFBD><EFBFBD><EFBFBD>ID
* @param data[IN]: <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
* @param len: <EFBFBD><EFBFBD><EFBFBD>ݳ<EFBFBD><EFBFBD><EFBFBD>
*
* @return <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ƿ<EFBFBD><EFBFBD>ɹ<EFBFBD><EFBFBD><EFBFBD>
* true: <EFBFBD>ɹ<EFBFBD> false: ʧ<EFBFBD><EFBFBD>
*/
bool can_bus_send(unsigned char chan,unsigned int canid,const unsigned char *data, unsigned char len);
/**
* @brief: <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ݱ<EFBFBD><EFBFBD><EFBFBD>
*
* @param chan: can ͨ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>
* @param canid: <EFBFBD><EFBFBD><EFBFBD><EFBFBD>ID
* @param data[IN]: <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
* @param len: <EFBFBD><EFBFBD><EFBFBD>ݳ<EFBFBD><EFBFBD><EFBFBD>
*
* @return <EFBFBD><EFBFBD><EFBFBD>ر<EFBFBD>ʶ<EFBFBD>ţ<EFBFBD>0xFF <EFBFBD><EFBFBD>ʶʧ<EFBFBD><EFBFBD>
*/
unsigned char can_bus_send_rt(unsigned char chan,unsigned int canid,const unsigned char *data, unsigned char len);
/**
* @brief: <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
*
* @param chan: canͨ<EFBFBD><EFBFBD>
* @param krate: <EFBFBD><EFBFBD><EFBFBD><EFBFBD>
* @param highdomain: <EFBFBD>ߵ<EFBFBD>ƽ<EFBFBD>ź<EFBFBD>
* @param listen: <EFBFBD>Ƿ<EFBFBD>Ϊ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>ģʽ
*
* @return:
* true: <EFBFBD>ɹ<EFBFBD> false ʧ<EFBFBD><EFBFBD>
*/
bool can_bus_open(unsigned char chan,unsigned short krate, bool highdomain, bool listen);
/**
* @brief: <EFBFBD><EFBFBD>can<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>йص<EFBFBD><EFBFBD><EFBFBD><EFBFBD>д<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
*/
void can_bus_handle(void);
#endif