2 ******************************************************************************
3 * @file stm32f4xx_can.c
4 * @author MCD Application Team
7 * @brief This file provides firmware functions to manage the following
8 * functionalities of the Controller area network (CAN) peripheral:
9 * - Initialization and Configuration
10 * - CAN Frames Transmission
11 * - CAN Frames Reception
12 * - Operation modes switch
14 * - Interrupts and flags
18 * ===================================================================
19 * How to use this driver
20 * ===================================================================
22 * 1. Enable the CAN controller interface clock using
23 * RCC_APB1PeriphClockCmd(RCC_APB1Periph_CAN1, ENABLE); for CAN1
24 * and RCC_APB1PeriphClockCmd(RCC_APB1Periph_CAN2, ENABLE); for CAN2
25 * @note In case you are using CAN2 only, you have to enable the CAN1 clock.
27 * 2. CAN pins configuration
28 * - Enable the clock for the CAN GPIOs using the following function:
29 * RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOx, ENABLE);
30 * - Connect the involved CAN pins to AF9 using the following function
31 * GPIO_PinAFConfig(GPIOx, GPIO_PinSourcex, GPIO_AF_CANx);
32 * - Configure these CAN pins in alternate function mode by calling
33 * the function GPIO_Init();
35 * 3. Initialise and configure the CAN using CAN_Init() and
36 * CAN_FilterInit() functions.
38 * 4. Transmit the desired CAN frame using CAN_Transmit() function.
40 * 5. Check the transmission of a CAN frame using CAN_TransmitStatus()
43 * 6. Cancel the transmission of a CAN frame using CAN_CancelTransmit()
46 * 7. Receive a CAN frame using CAN_Recieve() function.
48 * 8. Release the receive FIFOs using CAN_FIFORelease() function.
50 * 9. Return the number of pending received frames using
51 * CAN_MessagePending() function.
53 * 10. To control CAN events you can use one of the following two methods:
54 * - Check on CAN flags using the CAN_GetFlagStatus() function.
55 * - Use CAN interrupts through the function CAN_ITConfig() at
56 * initialization phase and CAN_GetITStatus() function into
57 * interrupt routines to check if the event has occurred or not.
58 * After checking on a flag you should clear it using CAN_ClearFlag()
59 * function. And after checking on an interrupt event you should
60 * clear it using CAN_ClearITPendingBit() function.
65 ******************************************************************************
68 * THE PRESENT FIRMWARE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING CUSTOMERS
69 * WITH CODING INFORMATION REGARDING THEIR PRODUCTS IN ORDER FOR THEM TO SAVE
70 * TIME. AS A RESULT, STMICROELECTRONICS SHALL NOT BE HELD LIABLE FOR ANY
71 * DIRECT, INDIRECT OR CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING
72 * FROM THE CONTENT OF SUCH FIRMWARE AND/OR THE USE MADE BY CUSTOMERS OF THE
73 * CODING INFORMATION CONTAINED HEREIN IN CONNECTION WITH THEIR PRODUCTS.
75 * <h2><center>© COPYRIGHT 2011 STMicroelectronics</center></h2>
76 ******************************************************************************
79 /* Includes ------------------------------------------------------------------*/
80 #include "stm32f4xx_can.h"
81 #include "stm32f4xx_rcc.h"
83 /** @addtogroup STM32F4xx_StdPeriph_Driver
88 * @brief CAN driver modules
91 /* Private typedef -----------------------------------------------------------*/
92 /* Private define ------------------------------------------------------------*/
94 /* CAN Master Control Register bits */
95 #define MCR_DBF ((uint32_t)0x00010000) /* software master reset */
97 /* CAN Mailbox Transmit Request */
98 #define TMIDxR_TXRQ ((uint32_t)0x00000001) /* Transmit mailbox request */
100 /* CAN Filter Master Register bits */
101 #define FMR_FINIT ((uint32_t)0x00000001) /* Filter init mode */
103 /* Time out for INAK bit */
104 #define INAK_TIMEOUT ((uint32_t)0x0000FFFF)
105 /* Time out for SLAK bit */
106 #define SLAK_TIMEOUT ((uint32_t)0x0000FFFF)
108 /* Flags in TSR register */
109 #define CAN_FLAGS_TSR ((uint32_t)0x08000000)
110 /* Flags in RF1R register */
111 #define CAN_FLAGS_RF1R ((uint32_t)0x04000000)
112 /* Flags in RF0R register */
113 #define CAN_FLAGS_RF0R ((uint32_t)0x02000000)
114 /* Flags in MSR register */
115 #define CAN_FLAGS_MSR ((uint32_t)0x01000000)
116 /* Flags in ESR register */
117 #define CAN_FLAGS_ESR ((uint32_t)0x00F00000)
119 /* Mailboxes definition */
120 #define CAN_TXMAILBOX_0 ((uint8_t)0x00)
121 #define CAN_TXMAILBOX_1 ((uint8_t)0x01)
122 #define CAN_TXMAILBOX_2 ((uint8_t)0x02)
124 #define CAN_MODE_MASK ((uint32_t) 0x00000003)
126 /* Private macro -------------------------------------------------------------*/
127 /* Private variables ---------------------------------------------------------*/
128 /* Private function prototypes -----------------------------------------------*/
129 /* Private functions ---------------------------------------------------------*/
130 static ITStatus CheckITStatus(uint32_t CAN_Reg, uint32_t It_Bit);
132 /** @defgroup CAN_Private_Functions
136 /** @defgroup CAN_Group1 Initialization and Configuration functions
137 * @brief Initialization and Configuration functions
140 ===============================================================================
141 Initialization and Configuration functions
142 ===============================================================================
143 This section provides functions allowing to
144 - Initialize the CAN peripherals : Prescaler, operating mode, the maximum number
145 of time quanta to perform resynchronization, the number of time quanta in
146 Bit Segment 1 and 2 and many other modes.
147 Refer to @ref CAN_InitTypeDef for more details.
148 - Configures the CAN reception filter.
149 - Select the start bank filter for slave CAN.
150 - Enables or disables the Debug Freeze mode for CAN
151 - Enables or disables the CAN Time Trigger Operation communication mode
158 * @brief Deinitializes the CAN peripheral registers to their default reset values.
159 * @param CANx: where x can be 1 or 2 to select the CAN peripheral.
162 void CAN_DeInit(CAN_TypeDef* CANx)
164 /* Check the parameters */
165 assert_param(IS_CAN_ALL_PERIPH(CANx));
169 /* Enable CAN1 reset state */
170 RCC_APB1PeriphResetCmd(RCC_APB1Periph_CAN1, ENABLE);
171 /* Release CAN1 from reset state */
172 RCC_APB1PeriphResetCmd(RCC_APB1Periph_CAN1, DISABLE);
176 /* Enable CAN2 reset state */
177 RCC_APB1PeriphResetCmd(RCC_APB1Periph_CAN2, ENABLE);
178 /* Release CAN2 from reset state */
179 RCC_APB1PeriphResetCmd(RCC_APB1Periph_CAN2, DISABLE);
184 * @brief Initializes the CAN peripheral according to the specified
185 * parameters in the CAN_InitStruct.
186 * @param CANx: where x can be 1 or 2 to select the CAN peripheral.
187 * @param CAN_InitStruct: pointer to a CAN_InitTypeDef structure that contains
188 * the configuration information for the CAN peripheral.
189 * @retval Constant indicates initialization succeed which will be
190 * CAN_InitStatus_Failed or CAN_InitStatus_Success.
192 uint8_t CAN_Init(CAN_TypeDef* CANx, CAN_InitTypeDef* CAN_InitStruct)
194 uint8_t InitStatus = CAN_InitStatus_Failed;
195 uint32_t wait_ack = 0x00000000;
196 /* Check the parameters */
197 assert_param(IS_CAN_ALL_PERIPH(CANx));
198 assert_param(IS_FUNCTIONAL_STATE(CAN_InitStruct->CAN_TTCM));
199 assert_param(IS_FUNCTIONAL_STATE(CAN_InitStruct->CAN_ABOM));
200 assert_param(IS_FUNCTIONAL_STATE(CAN_InitStruct->CAN_AWUM));
201 assert_param(IS_FUNCTIONAL_STATE(CAN_InitStruct->CAN_NART));
202 assert_param(IS_FUNCTIONAL_STATE(CAN_InitStruct->CAN_RFLM));
203 assert_param(IS_FUNCTIONAL_STATE(CAN_InitStruct->CAN_TXFP));
204 assert_param(IS_CAN_MODE(CAN_InitStruct->CAN_Mode));
205 assert_param(IS_CAN_SJW(CAN_InitStruct->CAN_SJW));
206 assert_param(IS_CAN_BS1(CAN_InitStruct->CAN_BS1));
207 assert_param(IS_CAN_BS2(CAN_InitStruct->CAN_BS2));
208 assert_param(IS_CAN_PRESCALER(CAN_InitStruct->CAN_Prescaler));
210 /* Exit from sleep mode */
211 CANx->MCR &= (~(uint32_t)CAN_MCR_SLEEP);
213 /* Request initialisation */
214 CANx->MCR |= CAN_MCR_INRQ ;
216 /* Wait the acknowledge */
217 while (((CANx->MSR & CAN_MSR_INAK) != CAN_MSR_INAK) && (wait_ack != INAK_TIMEOUT))
222 /* Check acknowledge */
223 if ((CANx->MSR & CAN_MSR_INAK) != CAN_MSR_INAK)
225 InitStatus = CAN_InitStatus_Failed;
229 /* Set the time triggered communication mode */
230 if (CAN_InitStruct->CAN_TTCM == ENABLE)
232 CANx->MCR |= CAN_MCR_TTCM;
236 CANx->MCR &= ~(uint32_t)CAN_MCR_TTCM;
239 /* Set the automatic bus-off management */
240 if (CAN_InitStruct->CAN_ABOM == ENABLE)
242 CANx->MCR |= CAN_MCR_ABOM;
246 CANx->MCR &= ~(uint32_t)CAN_MCR_ABOM;
249 /* Set the automatic wake-up mode */
250 if (CAN_InitStruct->CAN_AWUM == ENABLE)
252 CANx->MCR |= CAN_MCR_AWUM;
256 CANx->MCR &= ~(uint32_t)CAN_MCR_AWUM;
259 /* Set the no automatic retransmission */
260 if (CAN_InitStruct->CAN_NART == ENABLE)
262 CANx->MCR |= CAN_MCR_NART;
266 CANx->MCR &= ~(uint32_t)CAN_MCR_NART;
269 /* Set the receive FIFO locked mode */
270 if (CAN_InitStruct->CAN_RFLM == ENABLE)
272 CANx->MCR |= CAN_MCR_RFLM;
276 CANx->MCR &= ~(uint32_t)CAN_MCR_RFLM;
279 /* Set the transmit FIFO priority */
280 if (CAN_InitStruct->CAN_TXFP == ENABLE)
282 CANx->MCR |= CAN_MCR_TXFP;
286 CANx->MCR &= ~(uint32_t)CAN_MCR_TXFP;
289 /* Set the bit timing register */
290 CANx->BTR = (uint32_t)((uint32_t)CAN_InitStruct->CAN_Mode << 30) | \
291 ((uint32_t)CAN_InitStruct->CAN_SJW << 24) | \
292 ((uint32_t)CAN_InitStruct->CAN_BS1 << 16) | \
293 ((uint32_t)CAN_InitStruct->CAN_BS2 << 20) | \
294 ((uint32_t)CAN_InitStruct->CAN_Prescaler - 1);
296 /* Request leave initialisation */
297 CANx->MCR &= ~(uint32_t)CAN_MCR_INRQ;
299 /* Wait the acknowledge */
302 while (((CANx->MSR & CAN_MSR_INAK) == CAN_MSR_INAK) && (wait_ack != INAK_TIMEOUT))
307 /* ...and check acknowledged */
308 if ((CANx->MSR & CAN_MSR_INAK) == CAN_MSR_INAK)
310 InitStatus = CAN_InitStatus_Failed;
314 InitStatus = CAN_InitStatus_Success ;
318 /* At this step, return the status of initialization */
323 * @brief Configures the CAN reception filter according to the specified
324 * parameters in the CAN_FilterInitStruct.
325 * @param CAN_FilterInitStruct: pointer to a CAN_FilterInitTypeDef structure that
326 * contains the configuration information.
329 void CAN_FilterInit(CAN_FilterInitTypeDef* CAN_FilterInitStruct)
331 uint32_t filter_number_bit_pos = 0;
332 /* Check the parameters */
333 assert_param(IS_CAN_FILTER_NUMBER(CAN_FilterInitStruct->CAN_FilterNumber));
334 assert_param(IS_CAN_FILTER_MODE(CAN_FilterInitStruct->CAN_FilterMode));
335 assert_param(IS_CAN_FILTER_SCALE(CAN_FilterInitStruct->CAN_FilterScale));
336 assert_param(IS_CAN_FILTER_FIFO(CAN_FilterInitStruct->CAN_FilterFIFOAssignment));
337 assert_param(IS_FUNCTIONAL_STATE(CAN_FilterInitStruct->CAN_FilterActivation));
339 filter_number_bit_pos = ((uint32_t)1) << CAN_FilterInitStruct->CAN_FilterNumber;
341 /* Initialisation mode for the filter */
342 CAN1->FMR |= FMR_FINIT;
344 /* Filter Deactivation */
345 CAN1->FA1R &= ~(uint32_t)filter_number_bit_pos;
348 if (CAN_FilterInitStruct->CAN_FilterScale == CAN_FilterScale_16bit)
350 /* 16-bit scale for the filter */
351 CAN1->FS1R &= ~(uint32_t)filter_number_bit_pos;
353 /* First 16-bit identifier and First 16-bit mask */
354 /* Or First 16-bit identifier and Second 16-bit identifier */
355 CAN1->sFilterRegister[CAN_FilterInitStruct->CAN_FilterNumber].FR1 =
356 ((0x0000FFFF & (uint32_t)CAN_FilterInitStruct->CAN_FilterMaskIdLow) << 16) |
357 (0x0000FFFF & (uint32_t)CAN_FilterInitStruct->CAN_FilterIdLow);
359 /* Second 16-bit identifier and Second 16-bit mask */
360 /* Or Third 16-bit identifier and Fourth 16-bit identifier */
361 CAN1->sFilterRegister[CAN_FilterInitStruct->CAN_FilterNumber].FR2 =
362 ((0x0000FFFF & (uint32_t)CAN_FilterInitStruct->CAN_FilterMaskIdHigh) << 16) |
363 (0x0000FFFF & (uint32_t)CAN_FilterInitStruct->CAN_FilterIdHigh);
366 if (CAN_FilterInitStruct->CAN_FilterScale == CAN_FilterScale_32bit)
368 /* 32-bit scale for the filter */
369 CAN1->FS1R |= filter_number_bit_pos;
370 /* 32-bit identifier or First 32-bit identifier */
371 CAN1->sFilterRegister[CAN_FilterInitStruct->CAN_FilterNumber].FR1 =
372 ((0x0000FFFF & (uint32_t)CAN_FilterInitStruct->CAN_FilterIdHigh) << 16) |
373 (0x0000FFFF & (uint32_t)CAN_FilterInitStruct->CAN_FilterIdLow);
374 /* 32-bit mask or Second 32-bit identifier */
375 CAN1->sFilterRegister[CAN_FilterInitStruct->CAN_FilterNumber].FR2 =
376 ((0x0000FFFF & (uint32_t)CAN_FilterInitStruct->CAN_FilterMaskIdHigh) << 16) |
377 (0x0000FFFF & (uint32_t)CAN_FilterInitStruct->CAN_FilterMaskIdLow);
381 if (CAN_FilterInitStruct->CAN_FilterMode == CAN_FilterMode_IdMask)
383 /*Id/Mask mode for the filter*/
384 CAN1->FM1R &= ~(uint32_t)filter_number_bit_pos;
386 else /* CAN_FilterInitStruct->CAN_FilterMode == CAN_FilterMode_IdList */
388 /*Identifier list mode for the filter*/
389 CAN1->FM1R |= (uint32_t)filter_number_bit_pos;
392 /* Filter FIFO assignment */
393 if (CAN_FilterInitStruct->CAN_FilterFIFOAssignment == CAN_Filter_FIFO0)
395 /* FIFO 0 assignation for the filter */
396 CAN1->FFA1R &= ~(uint32_t)filter_number_bit_pos;
399 if (CAN_FilterInitStruct->CAN_FilterFIFOAssignment == CAN_Filter_FIFO1)
401 /* FIFO 1 assignation for the filter */
402 CAN1->FFA1R |= (uint32_t)filter_number_bit_pos;
405 /* Filter activation */
406 if (CAN_FilterInitStruct->CAN_FilterActivation == ENABLE)
408 CAN1->FA1R |= filter_number_bit_pos;
411 /* Leave the initialisation mode for the filter */
412 CAN1->FMR &= ~FMR_FINIT;
416 * @brief Fills each CAN_InitStruct member with its default value.
417 * @param CAN_InitStruct: pointer to a CAN_InitTypeDef structure which ill be initialized.
420 void CAN_StructInit(CAN_InitTypeDef* CAN_InitStruct)
422 /* Reset CAN init structure parameters values */
424 /* Initialize the time triggered communication mode */
425 CAN_InitStruct->CAN_TTCM = DISABLE;
427 /* Initialize the automatic bus-off management */
428 CAN_InitStruct->CAN_ABOM = DISABLE;
430 /* Initialize the automatic wake-up mode */
431 CAN_InitStruct->CAN_AWUM = DISABLE;
433 /* Initialize the no automatic retransmission */
434 CAN_InitStruct->CAN_NART = DISABLE;
436 /* Initialize the receive FIFO locked mode */
437 CAN_InitStruct->CAN_RFLM = DISABLE;
439 /* Initialize the transmit FIFO priority */
440 CAN_InitStruct->CAN_TXFP = DISABLE;
442 /* Initialize the CAN_Mode member */
443 CAN_InitStruct->CAN_Mode = CAN_Mode_Normal;
445 /* Initialize the CAN_SJW member */
446 CAN_InitStruct->CAN_SJW = CAN_SJW_1tq;
448 /* Initialize the CAN_BS1 member */
449 CAN_InitStruct->CAN_BS1 = CAN_BS1_4tq;
451 /* Initialize the CAN_BS2 member */
452 CAN_InitStruct->CAN_BS2 = CAN_BS2_3tq;
454 /* Initialize the CAN_Prescaler member */
455 CAN_InitStruct->CAN_Prescaler = 1;
459 * @brief Select the start bank filter for slave CAN.
460 * @param CAN_BankNumber: Select the start slave bank filter from 1..27.
463 void CAN_SlaveStartBank(uint8_t CAN_BankNumber)
465 /* Check the parameters */
466 assert_param(IS_CAN_BANKNUMBER(CAN_BankNumber));
468 /* Enter Initialisation mode for the filter */
469 CAN1->FMR |= FMR_FINIT;
471 /* Select the start slave bank */
472 CAN1->FMR &= (uint32_t)0xFFFFC0F1 ;
473 CAN1->FMR |= (uint32_t)(CAN_BankNumber)<<8;
475 /* Leave Initialisation mode for the filter */
476 CAN1->FMR &= ~FMR_FINIT;
480 * @brief Enables or disables the DBG Freeze for CAN.
481 * @param CANx: where x can be 1 or 2 to to select the CAN peripheral.
482 * @param NewState: new state of the CAN peripheral.
483 * This parameter can be: ENABLE (CAN reception/transmission is frozen
484 * during debug. Reception FIFOs can still be accessed/controlled normally)
485 * or DISABLE (CAN is working during debug).
488 void CAN_DBGFreeze(CAN_TypeDef* CANx, FunctionalState NewState)
490 /* Check the parameters */
491 assert_param(IS_CAN_ALL_PERIPH(CANx));
492 assert_param(IS_FUNCTIONAL_STATE(NewState));
494 if (NewState != DISABLE)
496 /* Enable Debug Freeze */
497 CANx->MCR |= MCR_DBF;
501 /* Disable Debug Freeze */
502 CANx->MCR &= ~MCR_DBF;
508 * @brief Enables or disables the CAN Time TriggerOperation communication mode.
509 * @note DLC must be programmed as 8 in order Time Stamp (2 bytes) to be
510 * sent over the CAN bus.
511 * @param CANx: where x can be 1 or 2 to to select the CAN peripheral.
512 * @param NewState: Mode new state. This parameter can be: ENABLE or DISABLE.
513 * When enabled, Time stamp (TIME[15:0]) value is sent in the last two
514 * data bytes of the 8-byte message: TIME[7:0] in data byte 6 and TIME[15:8]
518 void CAN_TTComModeCmd(CAN_TypeDef* CANx, FunctionalState NewState)
520 /* Check the parameters */
521 assert_param(IS_CAN_ALL_PERIPH(CANx));
522 assert_param(IS_FUNCTIONAL_STATE(NewState));
523 if (NewState != DISABLE)
525 /* Enable the TTCM mode */
526 CANx->MCR |= CAN_MCR_TTCM;
529 CANx->sTxMailBox[0].TDTR |= ((uint32_t)CAN_TDT0R_TGT);
530 CANx->sTxMailBox[1].TDTR |= ((uint32_t)CAN_TDT1R_TGT);
531 CANx->sTxMailBox[2].TDTR |= ((uint32_t)CAN_TDT2R_TGT);
535 /* Disable the TTCM mode */
536 CANx->MCR &= (uint32_t)(~(uint32_t)CAN_MCR_TTCM);
539 CANx->sTxMailBox[0].TDTR &= ((uint32_t)~CAN_TDT0R_TGT);
540 CANx->sTxMailBox[1].TDTR &= ((uint32_t)~CAN_TDT1R_TGT);
541 CANx->sTxMailBox[2].TDTR &= ((uint32_t)~CAN_TDT2R_TGT);
549 /** @defgroup CAN_Group2 CAN Frames Transmission functions
550 * @brief CAN Frames Transmission functions
553 ===============================================================================
554 CAN Frames Transmission functions
555 ===============================================================================
556 This section provides functions allowing to
557 - Initiate and transmit a CAN frame message (if there is an empty mailbox).
558 - Check the transmission status of a CAN Frame
559 - Cancel a transmit request
566 * @brief Initiates and transmits a CAN frame message.
567 * @param CANx: where x can be 1 or 2 to to select the CAN peripheral.
568 * @param TxMessage: pointer to a structure which contains CAN Id, CAN DLC and CAN data.
569 * @retval The number of the mailbox that is used for transmission or
570 * CAN_TxStatus_NoMailBox if there is no empty mailbox.
572 uint8_t CAN_Transmit(CAN_TypeDef* CANx, CanTxMsg* TxMessage)
574 uint8_t transmit_mailbox = 0;
575 /* Check the parameters */
576 assert_param(IS_CAN_ALL_PERIPH(CANx));
577 assert_param(IS_CAN_IDTYPE(TxMessage->IDE));
578 assert_param(IS_CAN_RTR(TxMessage->RTR));
579 assert_param(IS_CAN_DLC(TxMessage->DLC));
581 /* Select one empty transmit mailbox */
582 if ((CANx->TSR&CAN_TSR_TME0) == CAN_TSR_TME0)
584 transmit_mailbox = 0;
586 else if ((CANx->TSR&CAN_TSR_TME1) == CAN_TSR_TME1)
588 transmit_mailbox = 1;
590 else if ((CANx->TSR&CAN_TSR_TME2) == CAN_TSR_TME2)
592 transmit_mailbox = 2;
596 transmit_mailbox = CAN_TxStatus_NoMailBox;
599 if (transmit_mailbox != CAN_TxStatus_NoMailBox)
602 CANx->sTxMailBox[transmit_mailbox].TIR &= TMIDxR_TXRQ;
603 if (TxMessage->IDE == CAN_Id_Standard)
605 assert_param(IS_CAN_STDID(TxMessage->StdId));
606 CANx->sTxMailBox[transmit_mailbox].TIR |= ((TxMessage->StdId << 21) | \
611 assert_param(IS_CAN_EXTID(TxMessage->ExtId));
612 CANx->sTxMailBox[transmit_mailbox].TIR |= ((TxMessage->ExtId << 3) | \
618 TxMessage->DLC &= (uint8_t)0x0000000F;
619 CANx->sTxMailBox[transmit_mailbox].TDTR &= (uint32_t)0xFFFFFFF0;
620 CANx->sTxMailBox[transmit_mailbox].TDTR |= TxMessage->DLC;
622 /* Set up the data field */
623 CANx->sTxMailBox[transmit_mailbox].TDLR = (((uint32_t)TxMessage->Data[3] << 24) |
624 ((uint32_t)TxMessage->Data[2] << 16) |
625 ((uint32_t)TxMessage->Data[1] << 8) |
626 ((uint32_t)TxMessage->Data[0]));
627 CANx->sTxMailBox[transmit_mailbox].TDHR = (((uint32_t)TxMessage->Data[7] << 24) |
628 ((uint32_t)TxMessage->Data[6] << 16) |
629 ((uint32_t)TxMessage->Data[5] << 8) |
630 ((uint32_t)TxMessage->Data[4]));
631 /* Request transmission */
632 CANx->sTxMailBox[transmit_mailbox].TIR |= TMIDxR_TXRQ;
634 return transmit_mailbox;
638 * @brief Checks the transmission status of a CAN Frame.
639 * @param CANx: where x can be 1 or 2 to select the CAN peripheral.
640 * @param TransmitMailbox: the number of the mailbox that is used for transmission.
641 * @retval CAN_TxStatus_Ok if the CAN driver transmits the message,
642 * CAN_TxStatus_Failed in an other case.
644 uint8_t CAN_TransmitStatus(CAN_TypeDef* CANx, uint8_t TransmitMailbox)
648 /* Check the parameters */
649 assert_param(IS_CAN_ALL_PERIPH(CANx));
650 assert_param(IS_CAN_TRANSMITMAILBOX(TransmitMailbox));
652 switch (TransmitMailbox)
654 case (CAN_TXMAILBOX_0):
655 state = CANx->TSR & (CAN_TSR_RQCP0 | CAN_TSR_TXOK0 | CAN_TSR_TME0);
657 case (CAN_TXMAILBOX_1):
658 state = CANx->TSR & (CAN_TSR_RQCP1 | CAN_TSR_TXOK1 | CAN_TSR_TME1);
660 case (CAN_TXMAILBOX_2):
661 state = CANx->TSR & (CAN_TSR_RQCP2 | CAN_TSR_TXOK2 | CAN_TSR_TME2);
664 state = CAN_TxStatus_Failed;
669 /* transmit pending */
670 case (0x0): state = CAN_TxStatus_Pending;
672 /* transmit failed */
673 case (CAN_TSR_RQCP0 | CAN_TSR_TME0): state = CAN_TxStatus_Failed;
675 case (CAN_TSR_RQCP1 | CAN_TSR_TME1): state = CAN_TxStatus_Failed;
677 case (CAN_TSR_RQCP2 | CAN_TSR_TME2): state = CAN_TxStatus_Failed;
679 /* transmit succeeded */
680 case (CAN_TSR_RQCP0 | CAN_TSR_TXOK0 | CAN_TSR_TME0):state = CAN_TxStatus_Ok;
682 case (CAN_TSR_RQCP1 | CAN_TSR_TXOK1 | CAN_TSR_TME1):state = CAN_TxStatus_Ok;
684 case (CAN_TSR_RQCP2 | CAN_TSR_TXOK2 | CAN_TSR_TME2):state = CAN_TxStatus_Ok;
686 default: state = CAN_TxStatus_Failed;
689 return (uint8_t) state;
693 * @brief Cancels a transmit request.
694 * @param CANx: where x can be 1 or 2 to select the CAN peripheral.
695 * @param Mailbox: Mailbox number.
698 void CAN_CancelTransmit(CAN_TypeDef* CANx, uint8_t Mailbox)
700 /* Check the parameters */
701 assert_param(IS_CAN_ALL_PERIPH(CANx));
702 assert_param(IS_CAN_TRANSMITMAILBOX(Mailbox));
703 /* abort transmission */
706 case (CAN_TXMAILBOX_0): CANx->TSR |= CAN_TSR_ABRQ0;
708 case (CAN_TXMAILBOX_1): CANx->TSR |= CAN_TSR_ABRQ1;
710 case (CAN_TXMAILBOX_2): CANx->TSR |= CAN_TSR_ABRQ2;
721 /** @defgroup CAN_Group3 CAN Frames Reception functions
722 * @brief CAN Frames Reception functions
725 ===============================================================================
726 CAN Frames Reception functions
727 ===============================================================================
728 This section provides functions allowing to
729 - Receive a correct CAN frame
730 - Release a specified receive FIFO (2 FIFOs are available)
731 - Return the number of the pending received CAN frames
738 * @brief Receives a correct CAN frame.
739 * @param CANx: where x can be 1 or 2 to select the CAN peripheral.
740 * @param FIFONumber: Receive FIFO number, CAN_FIFO0 or CAN_FIFO1.
741 * @param RxMessage: pointer to a structure receive frame which contains CAN Id,
742 * CAN DLC, CAN data and FMI number.
745 void CAN_Receive(CAN_TypeDef* CANx, uint8_t FIFONumber, CanRxMsg* RxMessage)
747 /* Check the parameters */
748 assert_param(IS_CAN_ALL_PERIPH(CANx));
749 assert_param(IS_CAN_FIFO(FIFONumber));
751 RxMessage->IDE = (uint8_t)0x04 & CANx->sFIFOMailBox[FIFONumber].RIR;
752 if (RxMessage->IDE == CAN_Id_Standard)
754 RxMessage->StdId = (uint32_t)0x000007FF & (CANx->sFIFOMailBox[FIFONumber].RIR >> 21);
758 RxMessage->ExtId = (uint32_t)0x1FFFFFFF & (CANx->sFIFOMailBox[FIFONumber].RIR >> 3);
761 RxMessage->RTR = (uint8_t)0x02 & CANx->sFIFOMailBox[FIFONumber].RIR;
763 RxMessage->DLC = (uint8_t)0x0F & CANx->sFIFOMailBox[FIFONumber].RDTR;
765 RxMessage->FMI = (uint8_t)0xFF & (CANx->sFIFOMailBox[FIFONumber].RDTR >> 8);
766 /* Get the data field */
767 RxMessage->Data[0] = (uint8_t)0xFF & CANx->sFIFOMailBox[FIFONumber].RDLR;
768 RxMessage->Data[1] = (uint8_t)0xFF & (CANx->sFIFOMailBox[FIFONumber].RDLR >> 8);
769 RxMessage->Data[2] = (uint8_t)0xFF & (CANx->sFIFOMailBox[FIFONumber].RDLR >> 16);
770 RxMessage->Data[3] = (uint8_t)0xFF & (CANx->sFIFOMailBox[FIFONumber].RDLR >> 24);
771 RxMessage->Data[4] = (uint8_t)0xFF & CANx->sFIFOMailBox[FIFONumber].RDHR;
772 RxMessage->Data[5] = (uint8_t)0xFF & (CANx->sFIFOMailBox[FIFONumber].RDHR >> 8);
773 RxMessage->Data[6] = (uint8_t)0xFF & (CANx->sFIFOMailBox[FIFONumber].RDHR >> 16);
774 RxMessage->Data[7] = (uint8_t)0xFF & (CANx->sFIFOMailBox[FIFONumber].RDHR >> 24);
775 /* Release the FIFO */
777 if (FIFONumber == CAN_FIFO0)
779 CANx->RF0R |= CAN_RF0R_RFOM0;
782 else /* FIFONumber == CAN_FIFO1 */
784 CANx->RF1R |= CAN_RF1R_RFOM1;
789 * @brief Releases the specified receive FIFO.
790 * @param CANx: where x can be 1 or 2 to select the CAN peripheral.
791 * @param FIFONumber: FIFO to release, CAN_FIFO0 or CAN_FIFO1.
794 void CAN_FIFORelease(CAN_TypeDef* CANx, uint8_t FIFONumber)
796 /* Check the parameters */
797 assert_param(IS_CAN_ALL_PERIPH(CANx));
798 assert_param(IS_CAN_FIFO(FIFONumber));
800 if (FIFONumber == CAN_FIFO0)
802 CANx->RF0R |= CAN_RF0R_RFOM0;
805 else /* FIFONumber == CAN_FIFO1 */
807 CANx->RF1R |= CAN_RF1R_RFOM1;
812 * @brief Returns the number of pending received messages.
813 * @param CANx: where x can be 1 or 2 to select the CAN peripheral.
814 * @param FIFONumber: Receive FIFO number, CAN_FIFO0 or CAN_FIFO1.
815 * @retval NbMessage : which is the number of pending message.
817 uint8_t CAN_MessagePending(CAN_TypeDef* CANx, uint8_t FIFONumber)
819 uint8_t message_pending=0;
820 /* Check the parameters */
821 assert_param(IS_CAN_ALL_PERIPH(CANx));
822 assert_param(IS_CAN_FIFO(FIFONumber));
823 if (FIFONumber == CAN_FIFO0)
825 message_pending = (uint8_t)(CANx->RF0R&(uint32_t)0x03);
827 else if (FIFONumber == CAN_FIFO1)
829 message_pending = (uint8_t)(CANx->RF1R&(uint32_t)0x03);
835 return message_pending;
842 /** @defgroup CAN_Group4 CAN Operation modes functions
843 * @brief CAN Operation modes functions
846 ===============================================================================
847 CAN Operation modes functions
848 ===============================================================================
849 This section provides functions allowing to select the CAN Operation modes
852 - initialization mode
860 * @brief Selects the CAN Operation mode.
861 * @param CAN_OperatingMode: CAN Operating Mode.
862 * This parameter can be one of @ref CAN_OperatingMode_TypeDef enumeration.
863 * @retval status of the requested mode which can be
864 * - CAN_ModeStatus_Failed: CAN failed entering the specific mode
865 * - CAN_ModeStatus_Success: CAN Succeed entering the specific mode
867 uint8_t CAN_OperatingModeRequest(CAN_TypeDef* CANx, uint8_t CAN_OperatingMode)
869 uint8_t status = CAN_ModeStatus_Failed;
871 /* Timeout for INAK or also for SLAK bits*/
872 uint32_t timeout = INAK_TIMEOUT;
874 /* Check the parameters */
875 assert_param(IS_CAN_ALL_PERIPH(CANx));
876 assert_param(IS_CAN_OPERATING_MODE(CAN_OperatingMode));
878 if (CAN_OperatingMode == CAN_OperatingMode_Initialization)
880 /* Request initialisation */
881 CANx->MCR = (uint32_t)((CANx->MCR & (uint32_t)(~(uint32_t)CAN_MCR_SLEEP)) | CAN_MCR_INRQ);
883 /* Wait the acknowledge */
884 while (((CANx->MSR & CAN_MODE_MASK) != CAN_MSR_INAK) && (timeout != 0))
888 if ((CANx->MSR & CAN_MODE_MASK) != CAN_MSR_INAK)
890 status = CAN_ModeStatus_Failed;
894 status = CAN_ModeStatus_Success;
897 else if (CAN_OperatingMode == CAN_OperatingMode_Normal)
899 /* Request leave initialisation and sleep mode and enter Normal mode */
900 CANx->MCR &= (uint32_t)(~(CAN_MCR_SLEEP|CAN_MCR_INRQ));
902 /* Wait the acknowledge */
903 while (((CANx->MSR & CAN_MODE_MASK) != 0) && (timeout!=0))
907 if ((CANx->MSR & CAN_MODE_MASK) != 0)
909 status = CAN_ModeStatus_Failed;
913 status = CAN_ModeStatus_Success;
916 else if (CAN_OperatingMode == CAN_OperatingMode_Sleep)
918 /* Request Sleep mode */
919 CANx->MCR = (uint32_t)((CANx->MCR & (uint32_t)(~(uint32_t)CAN_MCR_INRQ)) | CAN_MCR_SLEEP);
921 /* Wait the acknowledge */
922 while (((CANx->MSR & CAN_MODE_MASK) != CAN_MSR_SLAK) && (timeout!=0))
926 if ((CANx->MSR & CAN_MODE_MASK) != CAN_MSR_SLAK)
928 status = CAN_ModeStatus_Failed;
932 status = CAN_ModeStatus_Success;
937 status = CAN_ModeStatus_Failed;
940 return (uint8_t) status;
944 * @brief Enters the Sleep (low power) mode.
945 * @param CANx: where x can be 1 or 2 to select the CAN peripheral.
946 * @retval CAN_Sleep_Ok if sleep entered, CAN_Sleep_Failed otherwise.
948 uint8_t CAN_Sleep(CAN_TypeDef* CANx)
950 uint8_t sleepstatus = CAN_Sleep_Failed;
952 /* Check the parameters */
953 assert_param(IS_CAN_ALL_PERIPH(CANx));
955 /* Request Sleep mode */
956 CANx->MCR = (((CANx->MCR) & (uint32_t)(~(uint32_t)CAN_MCR_INRQ)) | CAN_MCR_SLEEP);
958 /* Sleep mode status */
959 if ((CANx->MSR & (CAN_MSR_SLAK|CAN_MSR_INAK)) == CAN_MSR_SLAK)
961 /* Sleep mode not entered */
962 sleepstatus = CAN_Sleep_Ok;
964 /* return sleep mode status */
965 return (uint8_t)sleepstatus;
969 * @brief Wakes up the CAN peripheral from sleep mode .
970 * @param CANx: where x can be 1 or 2 to select the CAN peripheral.
971 * @retval CAN_WakeUp_Ok if sleep mode left, CAN_WakeUp_Failed otherwise.
973 uint8_t CAN_WakeUp(CAN_TypeDef* CANx)
975 uint32_t wait_slak = SLAK_TIMEOUT;
976 uint8_t wakeupstatus = CAN_WakeUp_Failed;
978 /* Check the parameters */
979 assert_param(IS_CAN_ALL_PERIPH(CANx));
981 /* Wake up request */
982 CANx->MCR &= ~(uint32_t)CAN_MCR_SLEEP;
984 /* Sleep mode status */
985 while(((CANx->MSR & CAN_MSR_SLAK) == CAN_MSR_SLAK)&&(wait_slak!=0x00))
989 if((CANx->MSR & CAN_MSR_SLAK) != CAN_MSR_SLAK)
991 /* wake up done : Sleep mode exited */
992 wakeupstatus = CAN_WakeUp_Ok;
994 /* return wakeup status */
995 return (uint8_t)wakeupstatus;
1002 /** @defgroup CAN_Group5 CAN Bus Error management functions
1003 * @brief CAN Bus Error management functions
1006 ===============================================================================
1007 CAN Bus Error management functions
1008 ===============================================================================
1009 This section provides functions allowing to
1010 - Return the CANx's last error code (LEC)
1011 - Return the CANx Receive Error Counter (REC)
1012 - Return the LSB of the 9-bit CANx Transmit Error Counter(TEC).
1014 @note If TEC is greater than 255, The CAN is in bus-off state.
1015 @note if REC or TEC are greater than 96, an Error warning flag occurs.
1016 @note if REC or TEC are greater than 127, an Error Passive Flag occurs.
1023 * @brief Returns the CANx's last error code (LEC).
1024 * @param CANx: where x can be 1 or 2 to select the CAN peripheral.
1025 * @retval Error code:
1026 * - CAN_ERRORCODE_NoErr: No Error
1027 * - CAN_ERRORCODE_StuffErr: Stuff Error
1028 * - CAN_ERRORCODE_FormErr: Form Error
1029 * - CAN_ERRORCODE_ACKErr : Acknowledgment Error
1030 * - CAN_ERRORCODE_BitRecessiveErr: Bit Recessive Error
1031 * - CAN_ERRORCODE_BitDominantErr: Bit Dominant Error
1032 * - CAN_ERRORCODE_CRCErr: CRC Error
1033 * - CAN_ERRORCODE_SoftwareSetErr: Software Set Error
1035 uint8_t CAN_GetLastErrorCode(CAN_TypeDef* CANx)
1037 uint8_t errorcode=0;
1039 /* Check the parameters */
1040 assert_param(IS_CAN_ALL_PERIPH(CANx));
1042 /* Get the error code*/
1043 errorcode = (((uint8_t)CANx->ESR) & (uint8_t)CAN_ESR_LEC);
1045 /* Return the error code*/
1050 * @brief Returns the CANx Receive Error Counter (REC).
1051 * @note In case of an error during reception, this counter is incremented
1052 * by 1 or by 8 depending on the error condition as defined by the CAN
1053 * standard. After every successful reception, the counter is
1054 * decremented by 1 or reset to 120 if its value was higher than 128.
1055 * When the counter value exceeds 127, the CAN controller enters the
1056 * error passive state.
1057 * @param CANx: where x can be 1 or 2 to to select the CAN peripheral.
1058 * @retval CAN Receive Error Counter.
1060 uint8_t CAN_GetReceiveErrorCounter(CAN_TypeDef* CANx)
1064 /* Check the parameters */
1065 assert_param(IS_CAN_ALL_PERIPH(CANx));
1067 /* Get the Receive Error Counter*/
1068 counter = (uint8_t)((CANx->ESR & CAN_ESR_REC)>> 24);
1070 /* Return the Receive Error Counter*/
1076 * @brief Returns the LSB of the 9-bit CANx Transmit Error Counter(TEC).
1077 * @param CANx: where x can be 1 or 2 to to select the CAN peripheral.
1078 * @retval LSB of the 9-bit CAN Transmit Error Counter.
1080 uint8_t CAN_GetLSBTransmitErrorCounter(CAN_TypeDef* CANx)
1084 /* Check the parameters */
1085 assert_param(IS_CAN_ALL_PERIPH(CANx));
1087 /* Get the LSB of the 9-bit CANx Transmit Error Counter(TEC) */
1088 counter = (uint8_t)((CANx->ESR & CAN_ESR_TEC)>> 16);
1090 /* Return the LSB of the 9-bit CANx Transmit Error Counter(TEC) */
1097 /** @defgroup CAN_Group6 Interrupts and flags management functions
1098 * @brief Interrupts and flags management functions
1101 ===============================================================================
1102 Interrupts and flags management functions
1103 ===============================================================================
1105 This section provides functions allowing to configure the CAN Interrupts and
1106 to get the status and clear flags and Interrupts pending bits.
1108 The CAN provides 14 Interrupts sources and 15 Flags:
1113 The 15 flags can be divided on 4 groups:
1116 -----------------------
1119 CAN_FLAG_RQCP2 : Request completed MailBoxes 0, 1 and 2 Flags
1120 Set when when the last request (transmit or abort) has
1124 -----------------------
1127 CAN_FLAG_FMP1 : FIFO 0 and 1 Message Pending Flags
1128 set to signal that messages are pending in the receive
1130 These Flags are cleared only by hardware.
1133 CAN_FLAG_FF1 : FIFO 0 and 1 Full Flags
1134 set when three messages are stored in the selected
1138 CAN_FLAG_FOV1 : FIFO 0 and 1 Overrun Flags
1139 set when a new message has been received and passed
1140 the filter while the FIFO was full.
1142 C. Operating Mode Flags
1143 -----------------------
1144 CAN_FLAG_WKU : Wake up Flag
1145 set to signal that a SOF bit has been detected while
1146 the CAN hardware was in Sleep mode.
1148 CAN_FLAG_SLAK : Sleep acknowledge Flag
1149 Set to signal that the CAN has entered Sleep Mode.
1152 -----------------------
1153 CAN_FLAG_EWG : Error Warning Flag
1154 Set when the warning limit has been reached (Receive
1155 Error Counter or Transmit Error Counter greater than 96).
1156 This Flag is cleared only by hardware.
1158 CAN_FLAG_EPV : Error Passive Flag
1159 Set when the Error Passive limit has been reached
1160 (Receive Error Counter or Transmit Error Counter
1162 This Flag is cleared only by hardware.
1164 CAN_FLAG_BOF : Bus-Off Flag
1165 set when CAN enters the bus-off state. The bus-off
1166 state is entered on TEC overflow, greater than 255.
1167 This Flag is cleared only by hardware.
1169 CAN_FLAG_LEC : Last error code Flag
1170 set If a message has been transferred (reception or
1171 transmission) with error, and the error code is hold.
1176 The 14 interrupts can be divided on 4 groups:
1178 A. Transmit interrupt
1179 -----------------------
1180 CAN_IT_TME : Transmit mailbox empty Interrupt
1181 if enabled, this interrupt source is pending when
1182 no transmit request are pending for Tx mailboxes.
1184 B. Receive Interrupts
1185 -----------------------
1187 CAN_IT_FMP1 : FIFO 0 and FIFO1 message pending Interrupts
1188 if enabled, these interrupt sources are pending when
1189 messages are pending in the receive FIFO.
1190 The corresponding interrupt pending bits are cleared
1194 CAN_IT_FF1 : FIFO 0 and FIFO1 full Interrupts
1195 if enabled, these interrupt sources are pending when
1196 three messages are stored in the selected FIFO.
1199 CAN_IT_FOV1 : FIFO 0 and FIFO1 overrun Interrupts
1200 if enabled, these interrupt sources are pending when
1201 a new message has been received and passed the filter
1202 while the FIFO was full.
1204 C. Operating Mode Interrupts
1205 -------------------------------
1206 CAN_IT_WKU : Wake-up Interrupt
1207 if enabled, this interrupt source is pending when
1208 a SOF bit has been detected while the CAN hardware was
1211 CAN_IT_SLK : Sleep acknowledge Interrupt
1212 if enabled, this interrupt source is pending when
1213 the CAN has entered Sleep Mode.
1216 -----------------------
1217 CAN_IT_EWG : Error warning Interrupt
1218 if enabled, this interrupt source is pending when
1219 the warning limit has been reached (Receive Error
1220 Counter or Transmit Error Counter=96).
1222 CAN_IT_EPV : Error passive Interrupt
1223 if enabled, this interrupt source is pending when
1224 the Error Passive limit has been reached (Receive
1225 Error Counter or Transmit Error Counter>127).
1227 CAN_IT_BOF : Bus-off Interrupt
1228 if enabled, this interrupt source is pending when
1229 CAN enters the bus-off state. The bus-off state is
1230 entered on TEC overflow, greater than 255.
1231 This Flag is cleared only by hardware.
1233 CAN_IT_LEC : Last error code Interrupt
1234 if enabled, this interrupt source is pending when
1235 a message has been transferred (reception or
1236 transmission) with error, and the error code is hold.
1238 CAN_IT_ERR : Error Interrupt
1239 if enabled, this interrupt source is pending when
1240 an error condition is pending.
1243 Managing the CAN controller events :
1244 ------------------------------------
1245 The user should identify which mode will be used in his application to manage
1246 the CAN controller events: Polling mode or Interrupt mode.
1248 1. In the Polling Mode it is advised to use the following functions:
1249 - CAN_GetFlagStatus() : to check if flags events occur.
1250 - CAN_ClearFlag() : to clear the flags events.
1254 2. In the Interrupt Mode it is advised to use the following functions:
1255 - CAN_ITConfig() : to enable or disable the interrupt source.
1256 - CAN_GetITStatus() : to check if Interrupt occurs.
1257 - CAN_ClearITPendingBit() : to clear the Interrupt pending Bit (corresponding Flag).
1258 @note This function has no impact on CAN_IT_FMP0 and CAN_IT_FMP1 Interrupts
1259 pending bits since there are cleared only by hardware.
1265 * @brief Enables or disables the specified CANx interrupts.
1266 * @param CANx: where x can be 1 or 2 to to select the CAN peripheral.
1267 * @param CAN_IT: specifies the CAN interrupt sources to be enabled or disabled.
1268 * This parameter can be:
1269 * @arg CAN_IT_TME: Transmit mailbox empty Interrupt
1270 * @arg CAN_IT_FMP0: FIFO 0 message pending Interrupt
1271 * @arg CAN_IT_FF0: FIFO 0 full Interrupt
1272 * @arg CAN_IT_FOV0: FIFO 0 overrun Interrupt
1273 * @arg CAN_IT_FMP1: FIFO 1 message pending Interrupt
1274 * @arg CAN_IT_FF1: FIFO 1 full Interrupt
1275 * @arg CAN_IT_FOV1: FIFO 1 overrun Interrupt
1276 * @arg CAN_IT_WKU: Wake-up Interrupt
1277 * @arg CAN_IT_SLK: Sleep acknowledge Interrupt
1278 * @arg CAN_IT_EWG: Error warning Interrupt
1279 * @arg CAN_IT_EPV: Error passive Interrupt
1280 * @arg CAN_IT_BOF: Bus-off Interrupt
1281 * @arg CAN_IT_LEC: Last error code Interrupt
1282 * @arg CAN_IT_ERR: Error Interrupt
1283 * @param NewState: new state of the CAN interrupts.
1284 * This parameter can be: ENABLE or DISABLE.
1287 void CAN_ITConfig(CAN_TypeDef* CANx, uint32_t CAN_IT, FunctionalState NewState)
1289 /* Check the parameters */
1290 assert_param(IS_CAN_ALL_PERIPH(CANx));
1291 assert_param(IS_CAN_IT(CAN_IT));
1292 assert_param(IS_FUNCTIONAL_STATE(NewState));
1294 if (NewState != DISABLE)
1296 /* Enable the selected CANx interrupt */
1297 CANx->IER |= CAN_IT;
1301 /* Disable the selected CANx interrupt */
1302 CANx->IER &= ~CAN_IT;
1306 * @brief Checks whether the specified CAN flag is set or not.
1307 * @param CANx: where x can be 1 or 2 to to select the CAN peripheral.
1308 * @param CAN_FLAG: specifies the flag to check.
1309 * This parameter can be one of the following values:
1310 * @arg CAN_FLAG_RQCP0: Request MailBox0 Flag
1311 * @arg CAN_FLAG_RQCP1: Request MailBox1 Flag
1312 * @arg CAN_FLAG_RQCP2: Request MailBox2 Flag
1313 * @arg CAN_FLAG_FMP0: FIFO 0 Message Pending Flag
1314 * @arg CAN_FLAG_FF0: FIFO 0 Full Flag
1315 * @arg CAN_FLAG_FOV0: FIFO 0 Overrun Flag
1316 * @arg CAN_FLAG_FMP1: FIFO 1 Message Pending Flag
1317 * @arg CAN_FLAG_FF1: FIFO 1 Full Flag
1318 * @arg CAN_FLAG_FOV1: FIFO 1 Overrun Flag
1319 * @arg CAN_FLAG_WKU: Wake up Flag
1320 * @arg CAN_FLAG_SLAK: Sleep acknowledge Flag
1321 * @arg CAN_FLAG_EWG: Error Warning Flag
1322 * @arg CAN_FLAG_EPV: Error Passive Flag
1323 * @arg CAN_FLAG_BOF: Bus-Off Flag
1324 * @arg CAN_FLAG_LEC: Last error code Flag
1325 * @retval The new state of CAN_FLAG (SET or RESET).
1327 FlagStatus CAN_GetFlagStatus(CAN_TypeDef* CANx, uint32_t CAN_FLAG)
1329 FlagStatus bitstatus = RESET;
1331 /* Check the parameters */
1332 assert_param(IS_CAN_ALL_PERIPH(CANx));
1333 assert_param(IS_CAN_GET_FLAG(CAN_FLAG));
1336 if((CAN_FLAG & CAN_FLAGS_ESR) != (uint32_t)RESET)
1338 /* Check the status of the specified CAN flag */
1339 if ((CANx->ESR & (CAN_FLAG & 0x000FFFFF)) != (uint32_t)RESET)
1341 /* CAN_FLAG is set */
1346 /* CAN_FLAG is reset */
1350 else if((CAN_FLAG & CAN_FLAGS_MSR) != (uint32_t)RESET)
1352 /* Check the status of the specified CAN flag */
1353 if ((CANx->MSR & (CAN_FLAG & 0x000FFFFF)) != (uint32_t)RESET)
1355 /* CAN_FLAG is set */
1360 /* CAN_FLAG is reset */
1364 else if((CAN_FLAG & CAN_FLAGS_TSR) != (uint32_t)RESET)
1366 /* Check the status of the specified CAN flag */
1367 if ((CANx->TSR & (CAN_FLAG & 0x000FFFFF)) != (uint32_t)RESET)
1369 /* CAN_FLAG is set */
1374 /* CAN_FLAG is reset */
1378 else if((CAN_FLAG & CAN_FLAGS_RF0R) != (uint32_t)RESET)
1380 /* Check the status of the specified CAN flag */
1381 if ((CANx->RF0R & (CAN_FLAG & 0x000FFFFF)) != (uint32_t)RESET)
1383 /* CAN_FLAG is set */
1388 /* CAN_FLAG is reset */
1392 else /* If(CAN_FLAG & CAN_FLAGS_RF1R != (uint32_t)RESET) */
1394 /* Check the status of the specified CAN flag */
1395 if ((uint32_t)(CANx->RF1R & (CAN_FLAG & 0x000FFFFF)) != (uint32_t)RESET)
1397 /* CAN_FLAG is set */
1402 /* CAN_FLAG is reset */
1406 /* Return the CAN_FLAG status */
1411 * @brief Clears the CAN's pending flags.
1412 * @param CANx: where x can be 1 or 2 to to select the CAN peripheral.
1413 * @param CAN_FLAG: specifies the flag to clear.
1414 * This parameter can be one of the following values:
1415 * @arg CAN_FLAG_RQCP0: Request MailBox0 Flag
1416 * @arg CAN_FLAG_RQCP1: Request MailBox1 Flag
1417 * @arg CAN_FLAG_RQCP2: Request MailBox2 Flag
1418 * @arg CAN_FLAG_FF0: FIFO 0 Full Flag
1419 * @arg CAN_FLAG_FOV0: FIFO 0 Overrun Flag
1420 * @arg CAN_FLAG_FF1: FIFO 1 Full Flag
1421 * @arg CAN_FLAG_FOV1: FIFO 1 Overrun Flag
1422 * @arg CAN_FLAG_WKU: Wake up Flag
1423 * @arg CAN_FLAG_SLAK: Sleep acknowledge Flag
1424 * @arg CAN_FLAG_LEC: Last error code Flag
1427 void CAN_ClearFlag(CAN_TypeDef* CANx, uint32_t CAN_FLAG)
1430 /* Check the parameters */
1431 assert_param(IS_CAN_ALL_PERIPH(CANx));
1432 assert_param(IS_CAN_CLEAR_FLAG(CAN_FLAG));
1434 if (CAN_FLAG == CAN_FLAG_LEC) /* ESR register */
1436 /* Clear the selected CAN flags */
1437 CANx->ESR = (uint32_t)RESET;
1439 else /* MSR or TSR or RF0R or RF1R */
1441 flagtmp = CAN_FLAG & 0x000FFFFF;
1443 if ((CAN_FLAG & CAN_FLAGS_RF0R)!=(uint32_t)RESET)
1446 CANx->RF0R = (uint32_t)(flagtmp);
1448 else if ((CAN_FLAG & CAN_FLAGS_RF1R)!=(uint32_t)RESET)
1451 CANx->RF1R = (uint32_t)(flagtmp);
1453 else if ((CAN_FLAG & CAN_FLAGS_TSR)!=(uint32_t)RESET)
1455 /* Transmit Flags */
1456 CANx->TSR = (uint32_t)(flagtmp);
1458 else /* If((CAN_FLAG & CAN_FLAGS_MSR)!=(uint32_t)RESET) */
1460 /* Operating mode Flags */
1461 CANx->MSR = (uint32_t)(flagtmp);
1467 * @brief Checks whether the specified CANx interrupt has occurred or not.
1468 * @param CANx: where x can be 1 or 2 to to select the CAN peripheral.
1469 * @param CAN_IT: specifies the CAN interrupt source to check.
1470 * This parameter can be one of the following values:
1471 * @arg CAN_IT_TME: Transmit mailbox empty Interrupt
1472 * @arg CAN_IT_FMP0: FIFO 0 message pending Interrupt
1473 * @arg CAN_IT_FF0: FIFO 0 full Interrupt
1474 * @arg CAN_IT_FOV0: FIFO 0 overrun Interrupt
1475 * @arg CAN_IT_FMP1: FIFO 1 message pending Interrupt
1476 * @arg CAN_IT_FF1: FIFO 1 full Interrupt
1477 * @arg CAN_IT_FOV1: FIFO 1 overrun Interrupt
1478 * @arg CAN_IT_WKU: Wake-up Interrupt
1479 * @arg CAN_IT_SLK: Sleep acknowledge Interrupt
1480 * @arg CAN_IT_EWG: Error warning Interrupt
1481 * @arg CAN_IT_EPV: Error passive Interrupt
1482 * @arg CAN_IT_BOF: Bus-off Interrupt
1483 * @arg CAN_IT_LEC: Last error code Interrupt
1484 * @arg CAN_IT_ERR: Error Interrupt
1485 * @retval The current state of CAN_IT (SET or RESET).
1487 ITStatus CAN_GetITStatus(CAN_TypeDef* CANx, uint32_t CAN_IT)
1489 ITStatus itstatus = RESET;
1490 /* Check the parameters */
1491 assert_param(IS_CAN_ALL_PERIPH(CANx));
1492 assert_param(IS_CAN_IT(CAN_IT));
1494 /* check the interrupt enable bit */
1495 if((CANx->IER & CAN_IT) != RESET)
1497 /* in case the Interrupt is enabled, .... */
1501 /* Check CAN_TSR_RQCPx bits */
1502 itstatus = CheckITStatus(CANx->TSR, CAN_TSR_RQCP0|CAN_TSR_RQCP1|CAN_TSR_RQCP2);
1505 /* Check CAN_RF0R_FMP0 bit */
1506 itstatus = CheckITStatus(CANx->RF0R, CAN_RF0R_FMP0);
1509 /* Check CAN_RF0R_FULL0 bit */
1510 itstatus = CheckITStatus(CANx->RF0R, CAN_RF0R_FULL0);
1513 /* Check CAN_RF0R_FOVR0 bit */
1514 itstatus = CheckITStatus(CANx->RF0R, CAN_RF0R_FOVR0);
1517 /* Check CAN_RF1R_FMP1 bit */
1518 itstatus = CheckITStatus(CANx->RF1R, CAN_RF1R_FMP1);
1521 /* Check CAN_RF1R_FULL1 bit */
1522 itstatus = CheckITStatus(CANx->RF1R, CAN_RF1R_FULL1);
1525 /* Check CAN_RF1R_FOVR1 bit */
1526 itstatus = CheckITStatus(CANx->RF1R, CAN_RF1R_FOVR1);
1529 /* Check CAN_MSR_WKUI bit */
1530 itstatus = CheckITStatus(CANx->MSR, CAN_MSR_WKUI);
1533 /* Check CAN_MSR_SLAKI bit */
1534 itstatus = CheckITStatus(CANx->MSR, CAN_MSR_SLAKI);
1537 /* Check CAN_ESR_EWGF bit */
1538 itstatus = CheckITStatus(CANx->ESR, CAN_ESR_EWGF);
1541 /* Check CAN_ESR_EPVF bit */
1542 itstatus = CheckITStatus(CANx->ESR, CAN_ESR_EPVF);
1545 /* Check CAN_ESR_BOFF bit */
1546 itstatus = CheckITStatus(CANx->ESR, CAN_ESR_BOFF);
1549 /* Check CAN_ESR_LEC bit */
1550 itstatus = CheckITStatus(CANx->ESR, CAN_ESR_LEC);
1553 /* Check CAN_MSR_ERRI bit */
1554 itstatus = CheckITStatus(CANx->MSR, CAN_MSR_ERRI);
1557 /* in case of error, return RESET */
1564 /* in case the Interrupt is not enabled, return RESET */
1568 /* Return the CAN_IT status */
1573 * @brief Clears the CANx's interrupt pending bits.
1574 * @param CANx: where x can be 1 or 2 to to select the CAN peripheral.
1575 * @param CAN_IT: specifies the interrupt pending bit to clear.
1576 * This parameter can be one of the following values:
1577 * @arg CAN_IT_TME: Transmit mailbox empty Interrupt
1578 * @arg CAN_IT_FF0: FIFO 0 full Interrupt
1579 * @arg CAN_IT_FOV0: FIFO 0 overrun Interrupt
1580 * @arg CAN_IT_FF1: FIFO 1 full Interrupt
1581 * @arg CAN_IT_FOV1: FIFO 1 overrun Interrupt
1582 * @arg CAN_IT_WKU: Wake-up Interrupt
1583 * @arg CAN_IT_SLK: Sleep acknowledge Interrupt
1584 * @arg CAN_IT_EWG: Error warning Interrupt
1585 * @arg CAN_IT_EPV: Error passive Interrupt
1586 * @arg CAN_IT_BOF: Bus-off Interrupt
1587 * @arg CAN_IT_LEC: Last error code Interrupt
1588 * @arg CAN_IT_ERR: Error Interrupt
1591 void CAN_ClearITPendingBit(CAN_TypeDef* CANx, uint32_t CAN_IT)
1593 /* Check the parameters */
1594 assert_param(IS_CAN_ALL_PERIPH(CANx));
1595 assert_param(IS_CAN_CLEAR_IT(CAN_IT));
1600 /* Clear CAN_TSR_RQCPx (rc_w1)*/
1601 CANx->TSR = CAN_TSR_RQCP0|CAN_TSR_RQCP1|CAN_TSR_RQCP2;
1604 /* Clear CAN_RF0R_FULL0 (rc_w1)*/
1605 CANx->RF0R = CAN_RF0R_FULL0;
1608 /* Clear CAN_RF0R_FOVR0 (rc_w1)*/
1609 CANx->RF0R = CAN_RF0R_FOVR0;
1612 /* Clear CAN_RF1R_FULL1 (rc_w1)*/
1613 CANx->RF1R = CAN_RF1R_FULL1;
1616 /* Clear CAN_RF1R_FOVR1 (rc_w1)*/
1617 CANx->RF1R = CAN_RF1R_FOVR1;
1620 /* Clear CAN_MSR_WKUI (rc_w1)*/
1621 CANx->MSR = CAN_MSR_WKUI;
1624 /* Clear CAN_MSR_SLAKI (rc_w1)*/
1625 CANx->MSR = CAN_MSR_SLAKI;
1628 /* Clear CAN_MSR_ERRI (rc_w1) */
1629 CANx->MSR = CAN_MSR_ERRI;
1630 /* @note the corresponding Flag is cleared by hardware depending on the CAN Bus status*/
1633 /* Clear CAN_MSR_ERRI (rc_w1) */
1634 CANx->MSR = CAN_MSR_ERRI;
1635 /* @note the corresponding Flag is cleared by hardware depending on the CAN Bus status*/
1638 /* Clear CAN_MSR_ERRI (rc_w1) */
1639 CANx->MSR = CAN_MSR_ERRI;
1640 /* @note the corresponding Flag is cleared by hardware depending on the CAN Bus status*/
1643 /* Clear LEC bits */
1645 /* Clear CAN_MSR_ERRI (rc_w1) */
1646 CANx->MSR = CAN_MSR_ERRI;
1651 /* Clear CAN_MSR_ERRI (rc_w1) */
1652 CANx->MSR = CAN_MSR_ERRI;
1653 /* @note BOFF, EPVF and EWGF Flags are cleared by hardware depending on the CAN Bus status*/
1664 * @brief Checks whether the CAN interrupt has occurred or not.
1665 * @param CAN_Reg: specifies the CAN interrupt register to check.
1666 * @param It_Bit: specifies the interrupt source bit to check.
1667 * @retval The new state of the CAN Interrupt (SET or RESET).
1669 static ITStatus CheckITStatus(uint32_t CAN_Reg, uint32_t It_Bit)
1671 ITStatus pendingbitstatus = RESET;
1673 if ((CAN_Reg & It_Bit) != (uint32_t)RESET)
1676 pendingbitstatus = SET;
1680 /* CAN_IT is reset */
1681 pendingbitstatus = RESET;
1683 return pendingbitstatus;
1698 /******************* (C) COPYRIGHT 2011 STMicroelectronics *****END OF FILE****/