📄 can.c
字号:
/******************** (C) COPYRIGHT 2003 STMicroelectronics ********************
* File Name : can.c
* Author : MCD Application Team
* Date First Issued : 27/10/2003
* Description : This file contains all the functions prototypes for the
* CAN bus software library.
********************************************************************************
* History:
* 27/10/2003 : Created
*******************************************************************************/
#include "can.h"
#include "xti.h"
#include "pcu.h"
// 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)
// array of pre-defined timing parameters for standard bitrates
u16 CanTimings[] = { // value bitrate NTQ TSEG1 TSEG2 SJW BRP
CAN_TIMING(11, 4, 4, 5), // 0x3AC4 100 kbit/s 16 11 4 4 5
CAN_TIMING(11, 4, 4, 4), // 0x3AC3 125 kbit/s 16 11 4 4 4
CAN_TIMING( 4, 3, 3, 4), // 0x2383 250 kbit/s 8 4 3 3 4
CAN_TIMING(13, 2, 1, 1), // 0x1C00 500 kbit/s 16 13 2 1 1
CAN_TIMING( 4, 3, 1, 1), // 0x2300 1 Mbit/s 8 4 3 1 1
};
/*******************************************************************************
* 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(int bitrate)
{
CAN->BTR = CanTimings[bitrate]; // write the predefined timing value
CAN->BRPR = 0; // clear the Extended Baud Rate Prescaler
}
/*******************************************************************************
* 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(int tseg1, int tseg2, int sjw, int brp)
{
CAN->BTR = CAN_TIMING(tseg1, tseg2, sjw, brp);
CAN->BRPR = ((brp-1) >> 6) & 0x0F;
}
/*******************************************************************************
* 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(int WakeupMode)
{
// Wakeup Line 6 is linked to CAN RX pin (port 1.11)
// Wakeup Line 2 is linked to external pin (port 2.8)
int WakeupLine = (WakeupMode == CAN_WAKEUP_ON_CAN ? XTI_Line6 : XTI_Line2);
CAN_WaitEndOfTx();
XTI_Init();
// Configure the Wakeup Line mode, select Falling edge (transition to dominant state)
XTI_LineModeConfig(WakeupLine, XTI_FallingEdge);
// Enable Wake-Up interrupt
XTI_LineConfig(WakeupLine, ENABLE);
// Enable Wake-Up mode with interrupt
XTI_ModeConfig(XTI_WakeUpInterrupt, ENABLE);
XTI_PendingBitClear(XTI_InterruptLineValue());
// Enter STOP mode (resume execution from here)
PCU_EnterLPM(PCU_STOP);
}
/*******************************************************************************
* Function Name : CAN_GetFreeIF
* Description : Search the first free message interface, starting from 0
* Input : None
* Output : None
* Return : A free message interface number (0 or 1) if found, else -1
*******************************************************************************/
static int CAN_GetFreeIF(void)
{
if ((CAN->sMsgObj[0].COMR & CAN_CRQ_BUSY) == 0)
return 0;
else if ((CAN->sMsgObj[1].COMR & CAN_CRQ_BUSY) == 0)
return 1;
else return -1;
}
/*******************************************************************************
* Macro Name : xxx_ID_MSK, xxx_ID_ARB
* Description : Form the Mask and Arbitration registers value to filter a range
* of identifiers or a fixed identifier, for standard and extended IDs
*******************************************************************************/
#define RANGE_ID_MSK(range_start, range_end) (~((range_end) - (range_start)))
#define RANGE_ID_ARB(range_start, range_end) ((range_start) & (range_end))
#define FIXED_ID_MSK(id) RANGE_ID_MSK((id), (id))
#define FIXED_ID_ARB(id) RANGE_ID_ARB((id), (id))
#define STD_RANGE_ID_MSK(range_start, range_end) ((u16)((RANGE_ID_MSK((range_start), (range_end)) & 0x7FF) << 2))
#define STD_RANGE_ID_ARB(range_start, range_end) ((u16)(RANGE_ID_ARB((range_start), (range_end)) << 2))
#define STD_FIXED_ID_MSK(id) ((u16)((FIXED_ID_MSK(id) & 0x7FF) << 2))
#define STD_FIXED_ID_ARB(id) ((u16)(FIXED_ID_ARB(id) << 2))
#define EXT_RANGE_ID_MSK_L(range_start, range_end) ((u16)(RANGE_ID_MSK((range_start), (range_end)) >> 11))
#define EXT_RANGE_ID_MSK_H(range_start, range_end) ((u16)(STD_RANGE_ID_MSK((range_start), (range_end)) | ((RANGE_ID_MSK((range_start), (range_end)) >> 27) & 0x03)))
#define EXT_RANGE_ID_ARB_L(range_start, range_end) ((u16)(RANGE_ID_ARB((range_start), (range_end)) >> 11))
#define EXT_RANGE_ID_ARB_H(range_start, range_end) ((u16)(STD_RANGE_ID_ARB((range_start), (range_end)) | ((RANGE_ID_ARB((range_start), (range_end)) >> 27) & 0x03)))
#define EXT_FIXED_ID_MSK_L(id) ((u16)(FIXED_ID_MSK(id) >> 11))
#define EXT_FIXED_ID_MSK_H(id) ((u16)(STD_FIXED_ID_MSK(id) | ((FIXED_ID_MSK(id) >> 27) & 0x03)))
#define EXT_FIXED_ID_ARB_L(id) ((u16)(FIXED_ID_ARB(id) >> 11))
#define EXT_FIXED_ID_ARB_H(id) ((u16)(STD_FIXED_ID_ARB(id) | ((FIXED_ID_ARB(id) >> 27) & 0x03)))
/*******************************************************************************
* 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(int msgobj)
{
int msg_if;
while ((msg_if = CAN_GetFreeIF()) == -1);
CAN->sMsgObj[msg_if].COMM = CAN_COM_WRITE
| CAN_COM_MASK
| CAN_COM_ARB
| CAN_COM_CTRL
| CAN_COM_DATAA
| CAN_COM_DATAB;
CAN->sMsgObj[msg_if].MASK1 = 0;
CAN->sMsgObj[msg_if].MASK2 = 0;
CAN->sMsgObj[msg_if].ARB1 = 0;
CAN->sMsgObj[msg_if].ARB2 = 0;
CAN->sMsgObj[msg_if].MSGC = 0;
CAN->sMsgObj[msg_if].DA1 = 0;
CAN->sMsgObj[msg_if].DA2 = 0;
CAN->sMsgObj[msg_if].DB1 = 0;
CAN->sMsgObj[msg_if].DB2 = 0;
CAN->sMsgObj[msg_if].COMR = 1 + 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(int msgobj, int idType)
{
int msg_if;
while ((msg_if = CAN_GetFreeIF()) == -1);
CAN->sMsgObj[msg_if].COMM = CAN_COM_WRITE
| CAN_COM_MASK
| CAN_COM_ARB
| CAN_COM_CTRL
| CAN_COM_DATAA
| CAN_COM_DATAB;
CAN->sMsgObj[msg_if].MASK1 = 0;
CAN->sMsgObj[msg_if].ARB1 = 0;
if (idType == CAN_STD_ID)
{
CAN->sMsgObj[msg_if].MASK2 = CAN_MSK_MDIR;
CAN->sMsgObj[msg_if].ARB2 = CAN_ARB_MSGVAL | CAN_ARB_DIR;
}
else
{
CAN->sMsgObj[msg_if].MASK2 = CAN_MSK_MDIR | CAN_MSK_MXTD;
CAN->sMsgObj[msg_if].ARB2 = CAN_ARB_MSGVAL | CAN_ARB_DIR | CAN_ARB_XTD;
}
CAN->sMsgObj[msg_if].MSGC = CAN_CTL_TXIE | CAN_CTL_EOB;
CAN->sMsgObj[msg_if].DA1 = 0;
CAN->sMsgObj[msg_if].DA2 = 0;
CAN->sMsgObj[msg_if].DB1 = 0;
CAN->sMsgObj[msg_if].DB2 = 0;
CAN->sMsgObj[msg_if].COMR = 1 + msgobj;
}
/*******************************************************************************
* 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(int msgobj, int idType, u32 idLow, u32 idHigh, bool singleOrFifoLast)
{
int msg_if;
while ((msg_if = CAN_GetFreeIF()) == -1);
CAN->sMsgObj[msg_if].COMM = CAN_COM_WRITE
| CAN_COM_MASK
| CAN_COM_ARB
| CAN_COM_CTRL
| CAN_COM_DATAA
| CAN_COM_DATAB;
if (idType == CAN_STD_ID)
{
CAN->sMsgObj[msg_if].MASK1 = 0;
CAN->sMsgObj[msg_if].MASK2 = STD_RANGE_ID_MSK(idLow, idHigh);
CAN->sMsgObj[msg_if].ARB1 = 0;
CAN->sMsgObj[msg_if].ARB2 = CAN_ARB_MSGVAL | STD_RANGE_ID_ARB(idLow, idHigh);
}
else
{
CAN->sMsgObj[msg_if].MASK1 = EXT_RANGE_ID_MSK_L(idLow, idHigh);
CAN->sMsgObj[msg_if].MASK2 = CAN_MSK_MXTD | EXT_RANGE_ID_MSK_H(idLow, idHigh);
CAN->sMsgObj[msg_if].ARB1 = EXT_RANGE_ID_ARB_L(idLow, idHigh);
CAN->sMsgObj[msg_if].ARB2 = CAN_ARB_MSGVAL | CAN_ARB_XTD | EXT_RANGE_ID_ARB_H(idLow, idHigh);
}
CAN->sMsgObj[msg_if].MSGC = CAN_CTL_RXIE | CAN_CTL_UMASK | (singleOrFifoLast ? CAN_CTL_EOB : 0);
CAN->sMsgObj[msg_if].DA1 = 0;
CAN->sMsgObj[msg_if].DA2 = 0;
CAN->sMsgObj[msg_if].DB1 = 0;
CAN->sMsgObj[msg_if].DB2 = 0;
CAN->sMsgObj[msg_if].COMR = 1 + msgobj;
}
/*******************************************************************************
* Function Name : CAN_InvalidateAllMsgObj
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -