📄 73x_can.c
字号:
/******************** (C) COPYRIGHT 2005 STMicroelectronics ********************
* File Name : 73x_can.c
* Author : MCD Application Team
* Date First Issued : 09/27/2005 : V1.0
* Description : This file contains all the functions prototypes for the
* CAN bus software library.
**********************************************************************************
* History:
* 09/27/2005 : V1.0
**********************************************************************************
* 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.
*********************************************************************************/
#include "73x_can.h"
#include "73x_cfg.h"
/* Include of other module interface headers ---------------------------------*/
/* Local includes ------------------------------------------------------------*/
/* Private typedef -----------------------------------------------------------*/
/* Private define ------------------------------------------------------------*/
/* Private macro -------------------------------------------------------------*/
/* 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 variables ---------------------------------------------------------*/
/* 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 */
};
/* Private function prototypes -----------------------------------------------*/
/* Interface functions -------------------------------------------------------*/
/* Private functions ---------------------------------------------------------*/
/**********************************************************************************/
/* Function Name : CAN_DeInit */
/* Description : Deinitializes the CANx peripheral registers to their default */
/* reset values. */
/* Input : CANx: where x can be 0, 1 or 2 to select the CAN peripheral */
/* Output : None */
/* Return : None */
/**********************************************************************************/
void CAN_DeInit (CAN_TypeDef *CANx)
{
if(CANx == CAN0)
{
CFG_PeripheralClockConfig(CFG_CLK_CAN0,DISABLE);
CFG_PeripheralClockConfig(CFG_CLK_CAN0,ENABLE);
}
else if (CANx == CAN1)
{
CFG_PeripheralClockConfig(CFG_CLK_CAN1,DISABLE);
CFG_PeripheralClockConfig(CFG_CLK_CAN1,ENABLE);
}
else if (CANx == CAN2)
{
CFG_PeripheralClockConfig(CFG_CLK_CAN2,DISABLE);
CFG_PeripheralClockConfig(CFG_CLK_CAN2,ENABLE);
}
}
/*********************************************************************************/
/* Function Name : CAN_Init */
/* Description : Configure CAN */
/* Input : - CANx: where x can be 0, 1 or 2 to select the CAN peripheral*/
/* : - [CAN_InitStruct] Initialization structure {Mask, Bitrate} */
/* Output : None */
/* Return : None */
/*********************************************************************************/
void CAN_Init(CAN_TypeDef *CANx, CAN_InitTypeDef* CAN_InitStruct)
{
CAN_EnterInitMode(CANx, CAN_CR_CCE | CAN_InitStruct->CAN_Mask);
CAN_SetBitrate(CANx, CAN_InitStruct->CAN_Bitrate);
CAN_LeaveInitMode(CANx);
CAN_LeaveTestMode(CANx);
}
/*********************************************************************************/
/* Function Name : CAN_StructInit */
/* Description : Initialize the CAN Init Structure */
/* Input : CAN_InitStruct : pointer to a CAN_InitTypeDef structure */
/* which will be initialized. */
/* Return : None. */
/*********************************************************************************/
void CAN_StructInit(CAN_InitTypeDef* CAN_InitStruct)
{
/* Reset CAN init structure parameters values */
CAN_InitStruct->CAN_Mask = 0x0;
CAN_InitStruct->CAN_Bitrate = 0x2301;
}
/*********************************************************************************/
/* Function Name : CAN_SetBitrate */
/* Description : Setup a standard CAN bitrate */
/* Input : -CANx: where x can be 0, 1 or 2 to select the CAN peripheral */
/* -One of the CAN_BITRATE_xxx defines */
/* Output : None */
/* Return : None */
/* Note : CAN must be in initialization mode */
/*********************************************************************************/
void CAN_SetBitrate(CAN_TypeDef *CANx, u32 bitrate)
{
CANx->BTR = CanTimings[bitrate]; /*write the predefined timing value*/
CANx->BRPR = 0; /*clear the Extended Baud Rate Prescaler*/
}
/********************************************************************************/
/* Function Name : CAN_SetTiming */
/* Description : Setup the CAN timing with specific parameters */
/* Input : -CANx: where x can be 0, 1 or 2 to select the CAN peripheral*/
/* -Time Segment before the sample point position, from 1 to 16*/
/* -Time Segment after the sample point position, from 1 to 8 */
/* -Synchronisation Jump Width, from 1 to 4 */
/* -Baud Rate Prescaler, from 1 to 1024 */
/* Output : None */
/* Return : None */
/* Note : CAN must be in initialization mode */
/***************************************************************************** **/
void CAN_SetTiming(CAN_TypeDef *CANx, u32 tseg1, u32 tseg2, u32 sjw, u32 brp)
{
CANx->BTR = CAN_TIMING(tseg1, tseg2, sjw, brp);
CANx->BRPR = ((brp-1) >> 6) & 0x0F;
}
/*******************************************************************************/
/* Function Name : CAN_GetFreeIF */
/* Description : Search the first free message interface, starting from 0 */
/* Input : CANx: where x can be 0, 1 or 2 to select the CAN peripheral*/
/* Output : None */
/* Return : A free message interface number (0 or 1) if found, else 2 */
/*******************************************************************************/
static u32 CAN_GetFreeIF(CAN_TypeDef *CANx)
{
if ((CANx->sMsgObj[0].CRR & CAN_CRR_BUSY) == 0)
return 0;
else if ((CANx->sMsgObj[1].CRR & CAN_CRR_BUSY) == 0)
return 1;
else return 2;
}
/*-------------------------------------------------------------------------------------*/
/* 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 : Configures the message object as unused */
/* Input : -CANx: where x can be 0, 1 or 2 to select the CAN peripheral */
/* -Message object number, from 0 to 31 */
/* Output : None */
/* Return : None */
/*********************************************************************************/
void CAN_SetUnusedMsgObj(CAN_TypeDef *CANx, u32 msgobj)
{
u32 msg_if;
while ((msg_if = CAN_GetFreeIF(CANx)) == 2);
CANx->sMsgObj[msg_if].CMR = CAN_CMR_WRRD
| CAN_CMR_MASK
| CAN_CMR_ARB
| CAN_CMR_CONTROL
| CAN_CMR_DATAA
| CAN_CMR_DATAB;
CANx->sMsgObj[msg_if].M1R = 0;
CANx->sMsgObj[msg_if].M2R = 0;
CANx->sMsgObj[msg_if].A1R = 0;
CANx->sMsgObj[msg_if].A2R = 0;
CANx->sMsgObj[msg_if].MCR = 0;
CANx->sMsgObj[msg_if].DA1R = 0;
CANx->sMsgObj[msg_if].DA2R = 0;
CANx->sMsgObj[msg_if].DB1R = 0;
CANx->sMsgObj[msg_if].DB2R = 0;
CANx->sMsgObj[msg_if].CRR = 1 + msgobj;
}
/*********************************************************************************/
/* Function Name : CAN_SetTxMsgObj */
/* Description : Configure the message object as TX */
/* Input : -CANx: where x can be 0, 1 or 2 to select the CAN peripheral */
/* -Message object number, from 0 to 31 */
/* -CAN_STD_ID or CAN_EXT_ID */
/* Output : None */
/* Return : None */
/*********************************************************************************/
void CAN_SetTxMsgObj(CAN_TypeDef *CANx, u32 msgobj, u32 idType)
{
u32 msg_if;
while ((msg_if = CAN_GetFreeIF(CANx)) == 2);
CANx->sMsgObj[msg_if].CMR = CAN_CMR_WRRD
| CAN_CMR_MASK
| CAN_CMR_ARB
| CAN_CMR_CONTROL
| CAN_CMR_DATAA
| CAN_CMR_DATAB;
CANx->sMsgObj[msg_if].M1R = 0;
CANx->sMsgObj[msg_if].A1R = 0;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -