📄 71x_can.c
字号:
CAN->sMsgObj[msg_if].A2R = CAN_A2R_MSGVAL | CAN_A2R_DIR;
}
else
{
CAN->sMsgObj[msg_if].M2R = CAN_M2R_MDIR | CAN_M2R_MXTD | 0x1FFF;
CAN->sMsgObj[msg_if].A2R = CAN_A2R_MSGVAL | CAN_A2R_DIR | CAN_A2R_XTD;
}
CAN->sMsgObj[msg_if].MCR = CAN_MCR_TXIE | CAN_MCR_EOB | ( RemoteEN ? CAN_MCR_RMTEN : 0)
| ( RemoteEN ? CAN_MCR_UMASK : 0);
CAN->sMsgObj[msg_if].DA1R = 0;
CAN->sMsgObj[msg_if].DA2R = 0;
CAN->sMsgObj[msg_if].DB1R = 0;
CAN->sMsgObj[msg_if].DB2R = 0;
CAN->sMsgObj[msg_if].CRR = 1 + msgobj;
return 1;
}
/*******************************************************************************
* 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 : 0 if no interface IF free found else 1
*******************************************************************************/
u32 CAN_SetRxMsgObj(u32 msgobj, u32 idType, u32 idLow, u32 idHigh,
bool singleOrFifoLast)
{
u32 msg_if;
if ((msg_if = CAN_GetFreeIF()) == 2)
return 0;
CAN->sMsgObj[msg_if].CMR = CAN_CMR_WRRD
| CAN_CMR_MASK
| CAN_CMR_ARB
| CAN_CMR_CONTROL
| CAN_CMR_DATAA
| CAN_CMR_DATAB;
if (idType == CAN_STD_ID)
{
CAN->sMsgObj[msg_if].M1R = 0xFFFF;
CAN->sMsgObj[msg_if].M2R = CAN_M2R_MXTD | STD_RANGE_ID_MSK(idLow, idHigh);
CAN->sMsgObj[msg_if].A1R = 0;
CAN->sMsgObj[msg_if].A2R = CAN_A2R_MSGVAL | STD_RANGE_ID_ARB(idLow, idHigh);
}
else
{
CAN->sMsgObj[msg_if].M1R = EXT_RANGE_ID_MSK_L(idLow, idHigh);
CAN->sMsgObj[msg_if].M2R = CAN_M2R_MXTD | EXT_RANGE_ID_MSK_H(idLow, idHigh);
CAN->sMsgObj[msg_if].A1R = EXT_RANGE_ID_ARB_L(idLow, idHigh);
CAN->sMsgObj[msg_if].A2R = CAN_A2R_MSGVAL | CAN_A2R_XTD
| EXT_RANGE_ID_ARB_H(idLow, idHigh);
}
CAN->sMsgObj[msg_if].MCR = CAN_MCR_RXIE | CAN_MCR_UMASK
| (singleOrFifoLast ? CAN_MCR_EOB : 0);
CAN->sMsgObj[msg_if].DA1R = 0;
CAN->sMsgObj[msg_if].DA2R = 0;
CAN->sMsgObj[msg_if].DB1R = 0;
CAN->sMsgObj[msg_if].DB2R = 0;
CAN->sMsgObj[msg_if].CRR = 1 + msgobj;
return 1;
}
/*******************************************************************************
* Function Name : CAN_SetUnusedAllMsgObj
* Description : Configure all the message objects as unused
* Input : None
* Output : None
* Return : 1 if all message objects are set invalid else 0
*******************************************************************************/
u32 CAN_SetUnusedAllMsgObj(void)
{
u32 i;
for (i = 0; i < 32; i++)
{
if ( CAN_SetUnusedMsgObj(i) == 0)
return 0;
}
return 1;
}
/*******************************************************************************
* 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)
{
CAN_EnterInitMode(CAN_CR_CCE | mask);
CAN_SetBitrate(bitrate);
CAN_LeaveInitMode();
CAN_LeaveTestMode();
}
/*******************************************************************************
* Function Name : CAN_ReleaseMessage
* Description : Release the message object
* Input 1 : message object number, from 0 to 31
* Output : None
* Return : 1 if interface IF found else 0
*******************************************************************************/
u32 CAN_ReleaseMessage(u32 msgobj)
{
u32 i;
if ((i = CAN_GetFreeIF()) == 2)
return 0;
CAN->sMsgObj[i].CMR = CAN_CMR_CLRINTPND | CAN_CMR_TXRQST;
CAN->sMsgObj[i].CRR = 1 + msgobj;
return 1;
}
/*******************************************************************************
* 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
*******************************************************************************/
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
*******************************************************************************/
void CAN_ReleaseRxMessage(u32 msgobj)
{
CAN->sMsgObj[1].CMR = CAN_CMR_CLRINTPND | CAN_CMR_TXRQST;
CAN->sMsgObj[1].CRR = 1 + msgobj;
}
/*******************************************************************************
* Function Name : CAN_UpdateMsgObj
* Description : Updates the CAN message object with the pCanMsg fields, it
* does not start the transmission of the CAN message object
* 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 IF0 found free else 0
*******************************************************************************/
u32 CAN_UpdateMsgObj(u32 msgobj, canmsg* pCanMsg)
{
if (CAN->sMsgObj[0].CRR & CAN_CRR_BUSY)
return 0;
/* read the Arbitration and Message Control */
CAN->sMsgObj[0].CMR = CAN_CMR_ARB | CAN_CMR_CONTROL;
CAN->sMsgObj[0].CRR = 1 + msgobj;
while (CAN->sMsgObj[0].CRR & CAN_CRR_BUSY)
{
/*Wait*/
}
/* update the contents needed for transmission */
CAN->sMsgObj[0].CMR = CAN_CMR_WRRD
| CAN_CMR_ARB
| CAN_CMR_CONTROL
| CAN_CMR_DATAA
| CAN_CMR_DATAB;
if ((CAN->sMsgObj[0].A2R & CAN_A2R_XTD) == 0)
{
/* standard ID */
CAN->sMsgObj[0].A1R = 0;
CAN->sMsgObj[0].A2R = (CAN->sMsgObj[0].A2R & 0xE000)
| STD_FIXED_ID_ARB(pCanMsg->Id);
}
else
{
/* extended ID*/
CAN->sMsgObj[0].A1R = EXT_FIXED_ID_ARB_L(pCanMsg->Id);
CAN->sMsgObj[0].A2R = (CAN->sMsgObj[0].A2R & 0xE000)
| EXT_FIXED_ID_ARB_H(pCanMsg->Id);
}
CAN->sMsgObj[0].MCR = (CAN->sMsgObj[0].MCR & 0xFEF0) | CAN_MCR_NEWDAT
| pCanMsg->Dlc;
CAN->sMsgObj[0].DA1R = ((u16)pCanMsg->Data[1] << 8) | pCanMsg->Data[0];
CAN->sMsgObj[0].DA2R = ((u16)pCanMsg->Data[3] << 8) | pCanMsg->Data[2];
CAN->sMsgObj[0].DB1R = ((u16)pCanMsg->Data[5] << 8) | pCanMsg->Data[4];
CAN->sMsgObj[0].DB2R = ((u16)pCanMsg->Data[7] << 8) | pCanMsg->Data[6];
CAN->sMsgObj[0].CRR = 1 + msgobj;
while ( CAN->sMsgObj[0].CRR & CAN_CRR_BUSY)
{
/* wait */
}
return 1;
}
/*******************************************************************************
* Function Name : CAN_SendMessage
* Description : Updates and Starts 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 request OK, else 0
*******************************************************************************/
u32 CAN_SendMessage(u32 msgobj, canmsg* pCanMsg)
{
if (CAN_UpdateMsgObj(msgobj, pCanMsg) == 0)
return 0;
CAN->SR &= ~CAN_SR_TXOK;
return ( CAN_TransmitRequest( msgobj));
}
/*******************************************************************************
* Function Name : CAN_TransmitRequest
* Description : This function requests the transmission of a message object
* Input : msgobj: number of the message object that should be
* transmitted
* Output : None
* Return : 1 if interface ready to request the transmission else 0
*******************************************************************************/
u32 CAN_TransmitRequest( u32 msgobj )
{
u16 msg_if;
if ((msg_if = CAN_GetFreeIF()) == 2)
return 0;
/* Set the transmit request in the command mask register */
CAN->sMsgObj[msg_if].CMR = CAN_CMR_WRRD | CAN_CMR_TXRQST;
/* Write the message object number in the command request register */
CAN->sMsgObj[msg_if].CRR = 1 + msgobj;
return 1;
}
/*******************************************************************************
* 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)
{
u32 tempId = 0;
if (!CAN_GetMsgReceiveStatus(msgobj))
{
return 0;
}
CAN->SR &= ~CAN_SR_RXOK;
/* read the message contents */
CAN->sMsgObj[1].CMR = CAN_CMR_MASK
| CAN_CMR_ARB
| CAN_CMR_CONTROL
| CAN_CMR_CLRINTPND
| (release ? CAN_CMR_TXRQST : 0)
| CAN_CMR_DATAA
| CAN_CMR_DATAB;
CAN->sMsgObj[1].CRR = 1 + msgobj;
while (CAN->sMsgObj[1].CRR & CAN_CRR_BUSY)
{
/*Wait*/
}
if ((CAN->sMsgObj[1].A2R & CAN_A2R_XTD) == 0)
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -