?? stm32f10x_can.c
字號:
/******************** (C) COPYRIGHT 2006 STMicroelectronics ********************
* File Name : stm32f10x_can.c
* Author : MCD Application Team
* Date First Issued : 09/29/2006
* Description : This file provides all the CAN firmware functions.
********************************************************************************
* History:
* 02/05/2007: V0.1
* 09/29/2006: V0.01
********************************************************************************
* 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 "stm32f10x_can.h"
#include "stm32f10x_rcc.h"
/* Private typedef -----------------------------------------------------------*/
/* Private define ------------------------------------------------------------*/
/* CAN Master Control Register bits */
#define CAN_MCR_INRQ ((u32)0x00000001) /* Initialization request */
#define CAN_MCR_SLEEP ((u32)0x00000002) /* Sleep mode request */
#define CAN_MCR_TXFP ((u32)0x00000004) /* Transmit FIFO priority */
#define CAN_MCR_RFLM ((u32)0x00000008) /* Receive FIFO locked mode */
#define CAN_MCR_NART ((u32)0x00000010) /* No automatic retransmission */
#define CAN_MCR_AWUM ((u32)0x00000020) /* Automatic wake up mode */
#define CAN_MCR_ABOM ((u32)0x00000040) /* Automatic bus-off management */
#define CAN_MCR_TTCM ((u32)0x00000080) /* time triggered communication */
#define CAN_MCR_Reset ((u32)0x00008000) /* bxCAN master reset */
#define CAN_MCR_DbF ((u32)0x00010000) /* Debug freeze */
/* CAN Master Status Register bits */
#define CAN_MSR_INAK ((u32)0x00000001) /* Initialization acknowledge */
#define CAN_MSR_SLAK ((u32)0x00000002) /* Sleep acknowledge */
#define CAN_MSR_ERRI ((u32)0x00000004) /* Error interrupt */
#define CAN_MSR_WKUI ((u32)0x00000008) /* Wake-up interrupt */
#define CAN_MSR_SLAKI ((u32)0x00000010) /* Sleep acknowledge interrupt */
#define CAN_MSR_TXM ((u32)0x00000100) /* Transmit mode */
#define CAN_MSR_RXM ((u32)0x00000200) /* Receive mode */
#define CAN_MSR_SAMP ((u32)0x00000400) /* Last sample point */
#define CAN_MSR_RX ((u32)0x00000800) /* CAN Rx signal */
/* CAN Transmit Status Register bits */
#define CAN_TSR_RQCP0 ((u32)0x00000001) /* Request completed mailbox0 */
#define CAN_TSR_TXOK0 ((u32)0x00000002) /* Transmission OK of mailbox0 */
#define CAN_TSR_ALST0 ((u32)0x00000004) /* Arbitration lost for mailbox0 */
#define CAN_TSR_TERR0 ((u32)0x00000008) /* Transmission error of mailbox0 */
#define CAN_TSR_ABRQ0 ((u32)0x00000080) /* Abort request for mailbox0 */
#define CAN_TSR_RQCP1 ((u32)0x00000100) /* Request completed mailbox1 */
#define CAN_TSR_TXOK1 ((u32)0x00000200) /* Transmission OK of mailbox1 */
#define CAN_TSR_ALST1 ((u32)0x00000400) /* Arbitration lost for mailbox1 */
#define CAN_TSR_TERR1 ((u32)0x00000800) /* Transmission error of mailbox1 */
#define CAN_TSR_ABRQ1 ((u32)0x00008000) /* Abort request for mailbox1 */
#define CAN_TSR_RQCP2 ((u32)0x00010000) /* Request completed mailbox2 */
#define CAN_TSR_TXOK2 ((u32)0x00020000) /* Transmission OK of mailbox2 */
#define CAN_TSR_ALST2 ((u32)0x00040000) /* Arbitration lost for mailbox2 */
#define CAN_TSR_TERR2 ((u32)0x00080000) /* Transmission error of mailbox2 */
#define CAN_TSR_ABRQ2 ((u32)0x00800000) /* Abort request for mailbox2 */
#define CAN_TSR_CODE0 ((u32)0x01000000) /* Mailbox code bit 0 */
#define CAN_TSR_CODE1 ((u32)0x02000000) /* Mailbox code bit 1 */
#define CAN_TSR_TME0 ((u32)0x04000000) /* Transmit mailbox 0 empty */
#define CAN_TSR_TME1 ((u32)0x08000000) /* Transmit mailbox 1 empty */
#define CAN_TSR_TME2 ((u32)0x10000000) /* Transmit mailbox 2 empty */
#define CAN_TSR_LOW0 ((u32)0x20000000) /* Lowest priority flag for mailbox 0 */
#define CAN_TSR_LOW1 ((u32)0x40000000) /* Lowest priority flag for mailbox 1 */
#define CAN_TSR_LOW2 ((u32)0x80000000) /* Lowest priority flag for mailbox 2 */
/* CAN Receive FIFO 0 Register bits */
#define CAN_RF0R_FMP00 ((u32)0x00000001) /* FIFO 0 message pending bit 0 */
#define CAN_RF0R_FMP01 ((u32)0x00000002) /* FIFO 0 message pending bit 1 */
#define CAN_RF0R_FULL0 ((u32)0x00000008) /* FIFO 0 full */
#define CAN_RF0R_FOVR0 ((u32)0x00000010) /* FIFO 0 overrun */
#define CAN_RF0R_RFOM0 ((u32)0x00000020) /* Release FIFO 0 output mailbox */
/* CAN Receive FIFO 1 Register bits */
#define CAN_RF1R_FMP10 ((u32)0x00000001) /* FIFO 1 message pending bit 0 */
#define CAN_RF1R_FMP11 ((u32)0x00000002) /* FIFO 1 message pending bit 1 */
#define CAN_RF1R_FULL1 ((u32)0x00000008) /* FIFO 1 full */
#define CAN_RF1R_FOVR1 ((u32)0x00000010) /* FIFO 1 overrun */
#define CAN_RF1R_RFOM1 ((u32)0x00000020) /* Release FIFO 1 output mailbox */
/* CAN Error Status Register bits */
#define CAN_ESR_EWGF ((u32)0x00000001) /* Error warning flag */
#define CAN_ESR_EPVF ((u32)0x00000002) /* Error passive flag */
#define CAN_ESR_BOFF ((u32)0x00000004) /* Bus-off flag */
/* CAN Bit Timing Register bits */
#define CAN_BTR_CLK8 ((u32)0x00008000) /* CLK8 8Mhz clock input */
#define CAN_BTR_LBKM ((u32)0x40000000) /* Loop back mode */
#define CAN_BTR_SILM ((u32)0x80000000) /* Silent mode */
/* CAN Mailbox Transmit Request */
#define CAN_TMIDxR_TXRQ ((u32)0x00000001) /* Transmit mailbox request */
/* CAN Filter Master Register bits */
#define CAN_FMR_FINIT ((u32)0x00000001) /* Filter init mode */
/* Private macro -------------------------------------------------------------*/
/* Private variables ---------------------------------------------------------*/
/* Private function prototypes -----------------------------------------------*/
ITStatus CheckITStatus(u32 CAN_Reg, u32 It_Bit);
/* Private functions ---------------------------------------------------------*/
/*******************************************************************************
* Function Name : CAN_DeInit
* Description : Deinitializes the CAN peripheral registers to their default
* reset values.
* Input : None.
* Output : None.
* Return : None.
*******************************************************************************/
void CAN_DeInit(void)
{
/* Enable CAN reset state */
RCC_APB1PeriphResetCmd(RCC_APB1Periph_CAN, ENABLE);
/* Release CAN from reset state */
RCC_APB1PeriphResetCmd(RCC_APB1Periph_CAN, DISABLE);
}
/*******************************************************************************
* Function Name : CAN_Init
* Description : Initializes the CAN peripheral according to the specified
* parameters in the CAN_InitStruct.
* Input : CAN_InitStruct: pointer to a CAN_InitTypeDef structure that
contains the configuration information for the CAN peripheral.
* Output : None.
* Return : Constant indicates initialization succeed which will be
* CANINITFAILED or CANINITOK.
*******************************************************************************/
u8 CAN_Init(CAN_InitTypeDef* CAN_InitStruct)
{
u8 InitStatus = 0;
/* Request initialisation */
CAN->MCR = CAN_MCR_INRQ;
/* ...and check acknowledged */
if ((CAN->MSR & CAN_MSR_INAK) == 0)
{
InitStatus = CANINITFAILED;
}
else
{
/* Set the time triggered communication mode */
if (CAN_InitStruct->CAN_TTCM == ENABLE)
{
CAN->MCR |= CAN_MCR_TTCM;
}
else
{
CAN->MCR &= ~CAN_MCR_TTCM;
}
/* Set the automatic bus-off management */
if (CAN_InitStruct->CAN_ABOM == ENABLE)
{
CAN->MCR |= CAN_MCR_ABOM;
}
else
{
CAN->MCR &= ~CAN_MCR_ABOM;
}
/* Set the automatic wake-up mode */
if (CAN_InitStruct->CAN_AWUM == ENABLE)
{
CAN->MCR |= CAN_MCR_AWUM;
}
else
{
CAN->MCR &= ~CAN_MCR_AWUM;
}
/* Set the no automatic retransmission */
if (CAN_InitStruct->CAN_NART == ENABLE)
{
CAN->MCR |= CAN_MCR_NART;
}
else
{
CAN->MCR &= ~CAN_MCR_NART;
}
/* Set the receive FIFO locked mode */
if (CAN_InitStruct->CAN_RFLM == ENABLE)
{
CAN->MCR |= CAN_MCR_RFLM;
}
else
{
CAN->MCR &= ~CAN_MCR_RFLM;
}
/* Set the transmit FIFO priority */
if (CAN_InitStruct->CAN_TXFP == ENABLE)
{
CAN->MCR |= CAN_MCR_TXFP;
}
else
{
CAN->MCR &= ~CAN_MCR_TXFP;
}
/* Set the bit timing register */
CAN->BTR = (u32)(CAN_InitStruct->CAN_Mode << 30) | (CAN_InitStruct->CAN_SJW << 24) |
(CAN_InitStruct->CAN_BS1 << 16) | (CAN_InitStruct->CAN_BS2 << 20) |
(CAN_InitStruct->CAN_Clock << 15) | (CAN_InitStruct->CAN_Prescaler - 1);
InitStatus = CANINITOK;
/* Request leave initialisation */
CAN->MCR &= ~CAN_MCR_INRQ;
/* ...and check acknowledged */
if ((CAN->MSR & CAN_MSR_INAK) != CAN_MSR_INAK)
{
InitStatus = CANINITFAILED;
}
}
/* At this step, return the status of initialization */
return InitStatus;
}
/*******************************************************************************
* Function Name : CAN_FilterInit
* Description : Initializes the CAN peripheral according to the specified
* parameters in the CAN_FilterInitStruct.
* Input : CAN_FilterInitStruct: pointer to a CAN_FilterInitTypeDef
* structure that contains the configuration information.
* Output : None.
* Return : None.
*******************************************************************************/
void CAN_FilterInit(CAN_FilterInitTypeDef* CAN_FilterInitStruct)
{
u16 FilterNumber_BitPos = 0;
FilterNumber_BitPos = (0x0001 << (CAN_FilterInitStruct->CAN_FilterNumber));
/* Initialisation mode for the filter */
CAN->FMR |= CAN_FMR_FINIT;
/* Filter Deactivation */
CAN->FA0R &= ~FilterNumber_BitPos;
/* Filter Scale */
if (CAN_FilterInitStruct->CAN_FilterScale == CAN_FilterScale_16bit)
{
/* 16-bit scale for the filter */
CAN->FS0R &= ~FilterNumber_BitPos;
/* First 16-bit identifier and First 16-bit mask */
/* Or First 16-bit identifier and Second 16-bit identifier */
CAN->sFilterRegister[CAN_FilterInitStruct->CAN_FilterNumber].FR0 = ((u32)(0x0000FFFF & CAN_FilterInitStruct->CAN_FilterMaskIdLow) << 16) |
(0x0000FFFF & CAN_FilterInitStruct->CAN_FilterIdLow);
/* Second 16-bit identifier and Second 16-bit mask */
/* Or Third 16-bit identifier and Fourth 16-bit identifier */
CAN->sFilterRegister[CAN_FilterInitStruct->CAN_FilterNumber].FR1 = ((u32)(0x0000FFFF & CAN_FilterInitStruct->CAN_FilterMaskIdHigh) << 16) |
(0x0000FFFF & CAN_FilterInitStruct->CAN_FilterIdHigh);
}
if (CAN_FilterInitStruct->CAN_FilterScale == CAN_FilterScale_32bit)
{
/* 32-bit scale for the filter */
CAN->FS0R |= FilterNumber_BitPos;
/* 32-bit identifier or First 32-bit identifier */
CAN->sFilterRegister[CAN_FilterInitStruct->CAN_FilterNumber].FR0 = ((u32)(0x0000FFFF & CAN_FilterInitStruct->CAN_FilterIdHigh) << 16) |
(0x0000FFFF & CAN_FilterInitStruct->CAN_FilterIdLow);
/* 32-bit mask or Second 32-bit identifier */
CAN->sFilterRegister[CAN_FilterInitStruct->CAN_FilterNumber].FR1 = ((u32)(0x0000FFFF & CAN_FilterInitStruct->CAN_FilterMaskIdHigh) << 16) |
(0x0000FFFF & CAN_FilterInitStruct->CAN_FilterMaskIdLow);
}
/* Filter Mode */
if (CAN_FilterInitStruct->CAN_FilterMode == CAN_FilterMode_IdMask)
{
/*Id/Mask mode for the filter*/
CAN->FM0R &= ~FilterNumber_BitPos;
}
else /* CAN_FilterInitStruct->CAN_FilterMode == CAN_FilterMode_IdList */
{
/*Identifier list mode for the filter*/
CAN->FM0R |= FilterNumber_BitPos;
}
/* Filter FIFO assignment */
if (CAN_FilterInitStruct->CAN_FilterFIFOAssignment == CAN_FilterFIFO0)
{
/* FIFO 0 assignation for the filter */
CAN->FFA0R &= ~FilterNumber_BitPos;
}
if (CAN_FilterInitStruct->CAN_FilterFIFOAssignment == CAN_FilterFIFO1)
{
/* FIFO 1 assignation for the filter */
CAN->FFA0R |= FilterNumber_BitPos;
}
/* Filter activation */
if (CAN_FilterInitStruct->CAN_FilterActivation == ENABLE)
{
CAN->FA0R |= FilterNumber_BitPos;
}
/* Leave the initialisation mode for the filter */
CAN->FMR &= ~CAN_FMR_FINIT;
}
/*******************************************************************************
* Function Name : CAN_StructInit
* Description : Fills each CAN_InitStruct member with its default value.
* Input : CAN_InitStruct: pointer to a CAN_InitTypeDef structure which
* will be initialized.
* Output : None.
* Return : None.
*******************************************************************************/
void CAN_StructInit(CAN_InitTypeDef* CAN_InitStruct)
{
/* Reset CAN init structure parameters values */
/* Initialize the time triggered communication mode */
CAN_InitStruct->CAN_TTCM = DISABLE;
/* Initialize the automatic bus-off management */
CAN_InitStruct->CAN_ABOM = DISABLE;
/* Initialize the automatic wake-up mode */
CAN_InitStruct->CAN_AWUM = DISABLE;
/* Initialize the no automatic retransmission */
CAN_InitStruct->CAN_NART = DISABLE;
/* Initialize the receive FIFO locked mode */
CAN_InitStruct->CAN_RFLM = DISABLE;
/* Initialize the transmit FIFO priority */
CAN_InitStruct->CAN_TXFP = DISABLE;
/* Initialize the CAN_Mode member */
CAN_InitStruct->CAN_Mode = CAN_Mode_Normal;
/* Initialize the CAN_SJW member */
CAN_InitStruct->CAN_SJW = CAN_SJW_0tq;
/* Initialize the CAN_BS1 member */
CAN_InitStruct->CAN_BS1 = CAN_BS1_4tq;
/* Initialize the CAN_BS2 member */
CAN_InitStruct->CAN_BS2 = CAN_BS2_3tq;
/* Initialize the CAN_Clock member */
CAN_InitStruct->CAN_Clock = CAN_Clock_APB;
/* Initialize the CAN_Prescaler member */
CAN_InitStruct->CAN_Prescaler = 1;
}
/*******************************************************************************
* Function Name : CAN_ITConfig
* Description : Enables or disables the CAN interrupts.
* Input : - CAN_IT: specifies the CAN interrupt sources to be enabled or
* disabled.
* - NewState: new state of the CAN interrupts.
* This parameter can be: ENABLE or DISABLE.
* Output : None.
* Return : None.
*******************************************************************************/
void CAN_ITConfig(u32 CAN_IT, FunctionalState NewState)
{
if (NewState != DISABLE)
{
/* Enable the selected CAN interrupt */
CAN->IER |= CAN_IT;
}
else
{
/* Disable the selected CAN interrupt */
CAN->IER &= ~CAN_IT;
}
}
/*******************************************************************************
* Function Name : CAN_Transmit
* Description : Initiates the transmission of a message.
* Input : TxMessage: pointer to a structure which contains CAN Id, CAN
* DLC and CAN datas.
* Output : None.
* Return : The number of the mailbox that is used for transmission
* or CAN_NO_MB if there is no empty mailbox.
*******************************************************************************/
u8 CAN_Transmit(CanTxMsg* TxMessage)
{
u8 TransmitMailbox = 0;
/* Select one empty transmit mailbox */
if ((CAN->TSR&CAN_TSR_TME0) == CAN_TSR_TME0)
{
TransmitMailbox = 0;
}
else if ((CAN->TSR&CAN_TSR_TME1) == CAN_TSR_TME1)
{
TransmitMailbox = 1;
}
else if ((CAN->TSR&CAN_TSR_TME2) == CAN_TSR_TME2)
{
TransmitMailbox = 2;
}
else
{
TransmitMailbox = CAN_NO_MB;
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -