📄 can.h
字号:
/****************************************Copyright (c)**************************************************
**
** STR710 development team
**
**
** http://www.appchip.com
**
**--------------文件信息--------------------------------------------------------------------------------
** 文 件 名: emi.h
** 创 建 人: lhl
** 创建日期: 2006年4月10日
** 描 述: 该文件包含了所有EMI功能函数原型与所需要的常量
**
**--------------历史版本--------------------------------------------------------------------------------
** 创 建 人: lhl
** 版 本: V1.0
** 日 期: 2006年4月10日
** 描 述: 原始版本
**
**------------------------------------------------------------------------------------------------------
********************************************************************************************************/
#ifndef __CAN_H
#define __CAN_H
#include "types.h"
#include "str710.h"
#include "cpu_cfg.h"
#if EN_ARM_CAN > 0 //决定是否编译该文件
/************************************** 宏定义,常量定义 ******************************************/
#define CAN_CR_TEST 0x0080 /* 控制寄存器 */
#define CAN_CR_CCE 0x0040
#define CAN_CR_DAR 0x0020
#define CAN_CR_EIE 0x0008
#define CAN_CR_SIE 0x0004
#define CAN_CR_IE 0x0002
#define CAN_CR_INIT 0x0001
#define CAN_SR_LEC 0x0007 /* 状态寄存器 */
#define CAN_SR_TXOK 0x0008
#define CAN_SR_RXOK 0x0010
#define CAN_SR_EPASS 0x0020
#define CAN_SR_EWARN 0x0040
#define CAN_SR_BOFF 0x0080
#define CAN_TESTR_RX 0x0080 /* 测试寄存器 */
#define CAN_TESTR_TX1 0x0040
#define CAN_TESTR_TX0 0x0020
#define CAN_TESTR_LBACK 0x0010
#define CAN_TESTR_SILENT 0x0008
#define CAN_TESTR_BASIC 0x0004
#define CAN_CRR_BUSY 0x8000 /* IFn / Command请求寄存器 */
#define CAN_CMR_WRRD 0x0080 /* IFn / Command屏蔽寄存器 */
#define CAN_CMR_MASK 0x0040
#define CAN_CMR_ARB 0x0020
#define CAN_CMR_CONTROL 0x0010
#define CAN_CMR_CLRINTPND 0x0008
#define CAN_CMR_TXRQST 0x0004
#define CAN_CMR_DATAA 0x0002
#define CAN_CMR_DATAB 0x0001
#define CAN_M2R_MXTD 0x8000 /* IFn / Command屏蔽寄存器2 */
#define CAN_M2R_MDIR 0x4000
#define CAN_A2R_MSGVAL 0x8000 /* IFn / Arbitration屏蔽寄存器2 */
#define CAN_A2R_XTD 0x4000
#define CAN_A2R_DIR 0x2000
#define CAN_MCR_NEWDAT 0x8000 /* IFn / Message控制寄存器2 */
#define CAN_MCR_MSGLST 0x4000
#define CAN_MCR_INTPND 0x2000
#define CAN_MCR_UMASK 0x1000
#define CAN_MCR_TXIE 0x0800
#define CAN_MCR_RXIE 0x0400
#define CAN_MCR_RMTEN 0x0200
#define CAN_MCR_TXRQST 0x0100
#define CAN_MCR_EOB 0x0080
#define CAN_LAST_STD_ID ((1<<11) - 1) // message ID limits
#define CAN_LAST_EXT_ID ((1L<<29) - 1)
/************************************** 枚举类型定义 *******************************************/
typedef enum /* 标准BIT率参考值 */
{
CAN_BITRATE_100K,
CAN_BITRATE_125K,
CAN_BITRATE_250K,
CAN_BITRATE_500K,
CAN_BITRATE_1M
}CAN_BITRATE_T;
typedef enum /* CAN WAKE UP 模式 */
{
CAN_WAKEUP_ON_EXT,
CAN_WAKEUP_ON_CAN
}CAN_WAKEUP_MODE_T;
typedef enum /* CAN 消息ID类型 */
{
CAN_STD_ID,
CAN_EXT_ID
}MSG_ID_TYPE_T;
/************************************** 结构体类型定义 ******************************************/
typedef struct /* CAN 消息结构体 */
{
UWORD32 id_type;
volatile UWORD32 id;
volatile UWORD8 dlc;
volatile UWORD8 data[8];
}CAN_MSG_T;
/*********************** 全局外部函数原型声明,CAN 功能接口宏定义 *********************************/
/* 该宏切换CAN 到初始化模式, mask可以是任何数值 */
#define CAN_ENTER_INIT_MODE(mask) CAN_CR = (mask) | CAN_CR_INIT; \
CAN_SR = 0 /* 复位状态 */
/* 该宏切换CAN 到正常的模式, 离开初始化模式 */
#define CAN_LEAVE_INIT_MODE() CAN_CR &= ~(CAN_CR_INIT | CAN_CR_CCE)
/* 该宏切换CAN 到测试模式 */
#define CAN_ENTER_TEST_MODE(mask) CAN_CR |= CAN_CR_TEST; \
CAN_TESTR |= mask
/* 该宏切换CAN 到测试模式到正常模式 */
#define CAN_LEAVE_TEST_MODE() CAN_CR |= CAN_CR_TEST; \
CAN_TESTR &= ~(CAN_TESTR_LBACK | CAN_TESTR_SILENT | CAN_TESTR_BASIC); \
CAN_CR &= ~CAN_CR_TEST
/* 该宏测试收到消息的等待状态 */
#define CAN_WAITING_MSG_FLAG(msg_obj) (msg_obj) < 16 ? CAN_ND1R & (1 << (msg_obj)) : CAN_ND2R & (1 << ((msg_obj)-16))
/* 该宏测试传送消息请求状态标志 */
#define CAN_TRANSMIT_REQUESTED_FLAG(msg_obj) (msg_obj) < 16 ? CAN_TR1R & (1 << (msg_obj)) : CAN_TR2R & (1 << ((msg_obj)-16))
/* 该宏测试消息对象的中断状态标志 */
#define CAN_INTERRUPT_PENDING(msg_obj) (msg_obj) < 16 ? CAN_IP1R & (1 << (msg_obj)) : CAN_IP2R & (1 << ((msg_obj)-16))
/* 该宏判断是否是有效的消息对象 */
#define CAN_VALID_MSG_OBJ_FLAG(msg_obj) (msg_obj) < 16 ? CAN_MV1R & (1 << (msg_obj)) : CAN_MV2R & (1 << ((msg_obj)-16))
/* 该宏等待当前的传送结束 */
#define CAN_WAIT_TX_END() while ((CAN_SR & CAN_SR_TXOK) == 0); \
CAN_SR &= ~CAN_SR_TXOK
/* macro to format the timing register value from the timing parameters*/
#define CAN_TIMING(tseg1, tseg2, sjw, brp) (((tseg2-1) & 0x07) << 12) | (((tseg1-1) & 0x0F) << 8) | (((sjw-1) & 0x03) << 6) | ((brp-1) & 0x3F)
/*******************************************************************************
* Function Name : CAN_SetBitrate
* Description : Setup a standard CAN bitrate
* Input 1 : one of the CAN_BITRATE_xxx defines
* Output : None
* Return : None
* Note : CAN must be in initialization mode
*******************************************************************************/
void CAN_SetBitrate(u32 bitrate);
/*******************************************************************************
* Function Name : CAN_SetTiming
* Description : Setup the CAN timing with specific parameters
* Input 1 : Time Segment before the sample point position, from 1 to 16
* Input 2 : Time Segment after the sample point position, from 1 to 8
* Input 3 : Synchronisation Jump Width, from 1 to 4
* Input 4 : Baud Rate Prescaler, from 1 to 1024
* Output : None
* Return : None
* Note : CAN must be in initialization mode
*******************************************************************************/
void CAN_SetTiming(u32 tseg1, u32 tseg2, u32 sjw, u32 brp);
/*******************************************************************************
* Function Name : CAN_SleepRequest
* Description : Request the CAN cell to enter sleep mode
* Input 1 : CAN_WAKEUP_ON_EXT or CAN_WAKEUP_ON_CAN
* Output : None
* Return : None
*******************************************************************************/
void CAN_SleepRequest(u32 WakeupMode);
/*******************************************************************************
* Function Name : CAN_SetUnusedMsgObj
* Description : Configure the message object as unused
* Input 1 : message object number, from 0 to 31
* Output : None
* Return : None
*******************************************************************************/
void CAN_SetUnusedMsgObj(u32 msgobj);
/*******************************************************************************
* Function Name : CAN_SetTxMsgObj
* Description : Configure the message object as TX
* Input 1 : message object number, from 0 to 31
* Input 2 : CAN_STD_ID or CAN_EXT_ID
* Output : None
* Return : None
*******************************************************************************/
void CAN_SetTxMsgObj(u32 msgobj, u32 idType);
/*******************************************************************************
* Function Name : CAN_SetRxMsgObj
* Description : Configure the message object as RX
* Input 1 : message object number, from 0 to 31
* Input 2 : CAN_STD_ID or CAN_EXT_ID
* Input 3 : low part of the identifier range used for acceptance filtering
* Input 4 : high part of the identifier range used for acceptance filtering
* Input 5 : TRUE for a single receive object or a FIFO receive object that
* is the last one of the FIFO
* FALSE for a FIFO receive object that is not the last one
* Output : None
* Return : None
*******************************************************************************/
void CAN_SetRxMsgObj(u32 msgobj, u32 idType, u32 idLow, u32 idHigh, bool singleOrFifoLast);
/*******************************************************************************
* Function Name : CAN_InvalidateAllMsgObj
* Description : Configure all the message objects as unused
* Input : None
* Output : None
* Return : None
*******************************************************************************/
void CAN_InvalidateAllMsgObj(void);
/*******************************************************************************
* Function Name : CAN_Init
* Description : Initialize the CAN cell and set the bitrate
* Input 1 : any binary value formed from the CAN_CTL_xxx defines
* Input 2 : one of the CAN_BITRATE_xxx defines
* Output : None
* Return : None
*******************************************************************************/
void CAN_Init(u8 mask, u32 bitrate);
/*******************************************************************************
* Function Name : CAN_ReleaseMessage
* Description : Release the message object
* Input 1 : message object number, from 0 to 31
* Output : None
* Return : None
*******************************************************************************/
void CAN_ReleaseMessage(u32 msgobj);
/*******************************************************************************
* Function Name : CAN_ReleaseTxMessage
* Description : Release the transmit message object
* Input 1 : message object number, from 0 to 31
* Output : None
* Return : None
* Note : assume that message interface 0 is free
*******************************************************************************/
inline void CAN_ReleaseTxMessage(u32 msgobj)
{
CAN->sMsgObj[0].CMR = CAN_CMR_CLRINTPND | CAN_CMR_TXRQST;
CAN->sMsgObj[0].CRR = 1 + msgobj;
}
/*******************************************************************************
* Function Name : CAN_ReleaseRxMessage
* Description : Release the receive message object
* Input 1 : message object number, from 0 to 31
* Output : None
* Return : None
* Note : assume that message interface 1 is free
*******************************************************************************/
inline void CAN_ReleaseRxMessage(u32 msgobj)
{
CAN->sMsgObj[1].CMR = CAN_CMR_CLRINTPND | CAN_CMR_TXRQST;
CAN->sMsgObj[1].CRR = 1 + msgobj;
}
/*******************************************************************************
* Function Name : CAN_SendMessage
* Description : Start transmission of a message
* Input 1 : message object number, from 0 to 31
* Input 2 : pointer to the message structure containing data to transmit
* Output : None
* Return : 1 if transmission was OK, else 0
*******************************************************************************/
u32 CAN_SendMessage(u32 msgobj, canmsg* pCanMsg);
/*******************************************************************************
* Function Name : CAN_ReceiveMessage
* Description : Get the message, if received
* Input 1 : message object number, from 0 to 31
* Input 2 : if TRUE, the message object is released when getting the data
* if FALSE, the message object is not released
* Input 3 : pointer to the message structure where received data is stored
* Output : None
* Return : 1 if reception was OK, else 0 (no message pending)
*******************************************************************************/
u32 CAN_ReceiveMessage(u32 msgobj, bool release, canmsg* pCanMsg);
/*******************************************************************************
* Function Name : CAN_WaitEndOfTx
* Description : Wait until current transmission is finished
* Input : None
* Output : None
* Return : None
*******************************************************************************/
void CAN_WaitEndOfTx(void);
/*******************************************************************************
* Function Name : CAN_BasicSendMessage
* Description : Start transmission of a message in BASIC mode
* Input 1 : pointer to the message structure containing data to transmit
* Output : None
* Return : 1 if transmission was OK, else 0
* Note : CAN must be in BASIC mode
*******************************************************************************/
u32 CAN_BasicSendMessage(canmsg* pCanMsg);
/*******************************************************************************
* Function Name : CAN_BasicReceiveMessage
* Description : Get the message in BASIC mode, if received
* Input 1 : pointer to the message structure where received data is stored
* Output : None
* Return : 1 if reception was OK, else 0 (no message pending)
* Note : CAN must be in BASIC mode
*******************************************************************************/
u32 CAN_BasicReceiveMessage(canmsg* pCanMsg);
#endif /* __can_H */
/******************* (C) COPYRIGHT 2003 STMicroelectronics *****END OF FILE****/
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -