📄 71x_can.c
字号:
/******************** (C) COPYRIGHT 2007 STMicroelectronics ********************
* File Name : 71x_can.c
* Author : MCD Application Team
* Version : V4.0
* Date : 10/09/2007
* Description : This file contains all the functions prototypes for the
* CAN bus firmware library.
********************************************************************************
* THE PRESENT SOFTWARE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING CUSTOMERS
* WITH CODING INFORMATION REGARDING THEIR PRODUCTS IN ORDER FOR THEM TO SAVE TIME.
* AS A RESULT, STMICROELECTRONICS SHALL NOT BE HELD LIABLE FOR ANY DIRECT,
* INDIRECT OR CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING FROM THE
* CONTENT OF SUCH SOFTWARE AND/OR THE USE MADE BY CUSTOMERS OF THE CODING
* INFORMATION CONTAINED HEREIN IN CONNECTION WITH THEIR PRODUCTS.
*******************************************************************************/
/* Includes ------------------------------------------------------------------*/
#include "71x_can.h"
#include "71x_xti.h"
#include "71x_pcu.h"
/* Private typedef -----------------------------------------------------------*/
/* 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)
/* Private define ------------------------------------------------------------*/
/* Private macro -------------------------------------------------------------*/
/*******************************************************************************
* 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))))
#define EXT_RANGE_ID_MSK_H(range_start, range_end) \
((u16)(RANGE_ID_MSK((range_start), (range_end)) >> 16) & 0x1FFF)
#define EXT_RANGE_ID_ARB_L(range_start, range_end) \
((u16)(RANGE_ID_ARB((range_start), (range_end))))
#define EXT_RANGE_ID_ARB_H(range_start, range_end) \
((u16)(RANGE_ID_ARB((range_start), (range_end)) >> 16) & 0x1FFF)
#define EXT_FIXED_ID_MSK_L(id) ((u16)(FIXED_ID_MSK(id)))
#define EXT_FIXED_ID_MSK_H(id) ((u16)(FIXED_ID_MSK(id) >> 16 ) & 0x1FFF)
#define EXT_FIXED_ID_ARB_L(id) ((u16)(FIXED_ID_ARB(id)))
#define EXT_FIXED_ID_ARB_H(id) ((u16)(FIXED_ID_ARB(id) >> 16) & 0x1FFF)
/* Private variables ---------------------------------------------------------*/
/* array of pre-defined timing parameters for standard bitrates for APB1=8MHz */
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 */
};
/* Private function prototypes -----------------------------------------------*/
/* Private functions ---------------------------------------------------------*/
/*******************************************************************************
* Function Name : CAN_EnterInitMode
* Description : Switch the CAN into initialization mode
* Input 1 : any binary value formed from the CAN_CR_xxx defines
* Output : None
* Return : None
* Note : CAN_LeaveInitMode must be called when all is done
*******************************************************************************/
void CAN_EnterInitMode(u8 mask)
{
CAN->CR = mask | CAN_CR_INIT;
/* reset the status */
CAN->SR = 0;
}
/*******************************************************************************
* Function Name : CAN_LeaveInitMode
* Description : Leave the initialization mode (switch into normal mode)
* Input : None
* Output : None
* Return : None
*******************************************************************************/
void CAN_LeaveInitMode(void)
{
CAN->CR &= ~(CAN_CR_INIT | CAN_CR_CCE);
}
/*******************************************************************************
* Function Name : CAN_EnterTestMode
* Description : Switch the CAN into test mode
* Input 1 : any binary value formed from the CAN_TESTR_xxx defines
* Output : None
* Return : None
* Note : CAN_LeaveTestMode must be called when all is done
*******************************************************************************/
void CAN_EnterTestMode(u8 mask)
{
CAN->CR |= CAN_CR_TEST;
CAN->TESTR |= mask;
}
/*******************************************************************************
* Function Name : CAN_LeaveTestMode
* Description : Leave the current test mode (switch into normal mode)
* Input : None
* Output : None
* Return : None
*******************************************************************************/
void CAN_LeaveTestMode(void)
{
CAN->CR |= CAN_CR_TEST;
CAN->TESTR &= ~(CAN_TESTR_LBACK | CAN_TESTR_SILENT | CAN_TESTR_BASIC);
CAN->CR &= ~CAN_CR_TEST;
}
/*******************************************************************************
* 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)
{
/* write the predefined timing value */
CAN->BTR = CanTimings[bitrate];
/* clear the Extended Baud Rate Prescaler */
CAN->BRPR = 0;
}
/*******************************************************************************
* Function Name : CAN_SetTiming
* Description : Setup the CAN timing with specific parameters
* Input 1 : Time Segment before the sample point position, from 2 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)
{
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(u32 WakeupMode)
{
/* Wakeup Line 6 is linked to CAN RX pin (port 1.11) */
/* Wakeup Line 2 is linked to external pin (port 2.8) */
u32 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_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 2
*******************************************************************************/
static u32 CAN_GetFreeIF(void)
{
if ((CAN->sMsgObj[0].CRR & CAN_CRR_BUSY) == 0)
{
return 0;
}
else if ((CAN->sMsgObj[1].CRR & CAN_CRR_BUSY) == 0)
{
return 1;
}
else
{
return 2;
}
}
/*******************************************************************************
* Function Name : CAN_SetUnusedMsgObj
* Description : Configure the message object as unused
* Input 1 : message object number, from 0 to 31
* Output : None
* Return : 1 if interface found
* 0 if no interface found
*******************************************************************************/
u32 CAN_SetUnusedMsgObj(u32 msgobj)
{
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;
CAN->sMsgObj[msg_if].M1R = 0;
CAN->sMsgObj[msg_if].M2R = 0;
CAN->sMsgObj[msg_if].A1R = 0;
CAN->sMsgObj[msg_if].A2R = 0;
CAN->sMsgObj[msg_if].MCR = 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_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
* Input 3 : DISABLE or ENABLE remote functionality
* Output : None
* Return : 1 if interface found
* 0 if no interface found
*******************************************************************************/
u32 CAN_SetTxMsgObj(u32 msgobj, u32 idType, FunctionalState RemoteEN)
{
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;
CAN->sMsgObj[msg_if].M1R = 0xFFFF;
CAN->sMsgObj[msg_if].A1R = 0;
if (idType == CAN_STD_ID)
{
CAN->sMsgObj[msg_if].M2R = CAN_M2R_MDIR | 0x1FFF;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -