2 ******************************************************************************
\r
3 * @file stm32f10x_can.c
\r
4 * @author MCD Application Team
\r
7 * @brief This file provides all the CAN firmware functions.
\r
8 ******************************************************************************
\r
11 * THE PRESENT FIRMWARE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING CUSTOMERS
\r
12 * WITH CODING INFORMATION REGARDING THEIR PRODUCTS IN ORDER FOR THEM TO SAVE
\r
13 * TIME. AS A RESULT, STMICROELECTRONICS SHALL NOT BE HELD LIABLE FOR ANY
\r
14 * DIRECT, INDIRECT OR CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING
\r
15 * FROM THE CONTENT OF SUCH FIRMWARE AND/OR THE USE MADE BY CUSTOMERS OF THE
\r
16 * CODING INFORMATION CONTAINED HEREIN IN CONNECTION WITH THEIR PRODUCTS.
\r
18 * <h2><center>© COPYRIGHT 2010 STMicroelectronics</center></h2>
\r
21 /* Includes ------------------------------------------------------------------*/
\r
22 #include "stm32f10x_can.h"
\r
23 #include "stm32f10x_rcc.h"
\r
25 /** @addtogroup STM32F10x_StdPeriph_Driver
\r
30 * @brief CAN driver modules
\r
34 /** @defgroup CAN_Private_TypesDefinitions
\r
42 /** @defgroup CAN_Private_Defines
\r
46 /* CAN Master Control Register bits */
\r
47 #define MCR_INRQ ((uint32_t)0x00000001) /* Initialization request */
\r
48 #define MCR_SLEEP ((uint32_t)0x00000002) /* Sleep mode request */
\r
49 #define MCR_TXFP ((uint32_t)0x00000004) /* Transmit FIFO priority */
\r
50 #define MCR_RFLM ((uint32_t)0x00000008) /* Receive FIFO locked mode */
\r
51 #define MCR_NART ((uint32_t)0x00000010) /* No automatic retransmission */
\r
52 #define MCR_AWUM ((uint32_t)0x00000020) /* Automatic wake up mode */
\r
53 #define MCR_ABOM ((uint32_t)0x00000040) /* Automatic bus-off management */
\r
54 #define MCR_TTCM ((uint32_t)0x00000080) /* time triggered communication */
\r
55 #define MCR_RESET ((uint32_t)0x00008000) /* time triggered communication */
\r
56 #define MCR_DBF ((uint32_t)0x00010000) /* software master reset */
\r
58 /* CAN Master Status Register bits */
\r
59 #define MSR_INAK ((uint32_t)0x00000001) /* Initialization acknowledge */
\r
60 #define MSR_WKUI ((uint32_t)0x00000008) /* Wake-up interrupt */
\r
61 #define MSR_SLAKI ((uint32_t)0x00000010) /* Sleep acknowledge interrupt */
\r
63 /* CAN Transmit Status Register bits */
\r
64 #define TSR_RQCP0 ((uint32_t)0x00000001) /* Request completed mailbox0 */
\r
65 #define TSR_TXOK0 ((uint32_t)0x00000002) /* Transmission OK of mailbox0 */
\r
66 #define TSR_ABRQ0 ((uint32_t)0x00000080) /* Abort request for mailbox0 */
\r
67 #define TSR_RQCP1 ((uint32_t)0x00000100) /* Request completed mailbox1 */
\r
68 #define TSR_TXOK1 ((uint32_t)0x00000200) /* Transmission OK of mailbox1 */
\r
69 #define TSR_ABRQ1 ((uint32_t)0x00008000) /* Abort request for mailbox1 */
\r
70 #define TSR_RQCP2 ((uint32_t)0x00010000) /* Request completed mailbox2 */
\r
71 #define TSR_TXOK2 ((uint32_t)0x00020000) /* Transmission OK of mailbox2 */
\r
72 #define TSR_ABRQ2 ((uint32_t)0x00800000) /* Abort request for mailbox2 */
\r
73 #define TSR_TME0 ((uint32_t)0x04000000) /* Transmit mailbox 0 empty */
\r
74 #define TSR_TME1 ((uint32_t)0x08000000) /* Transmit mailbox 1 empty */
\r
75 #define TSR_TME2 ((uint32_t)0x10000000) /* Transmit mailbox 2 empty */
\r
77 /* CAN Receive FIFO 0 Register bits */
\r
78 #define RF0R_FULL0 ((uint32_t)0x00000008) /* FIFO 0 full */
\r
79 #define RF0R_FOVR0 ((uint32_t)0x00000010) /* FIFO 0 overrun */
\r
80 #define RF0R_RFOM0 ((uint32_t)0x00000020) /* Release FIFO 0 output mailbox */
\r
82 /* CAN Receive FIFO 1 Register bits */
\r
83 #define RF1R_FULL1 ((uint32_t)0x00000008) /* FIFO 1 full */
\r
84 #define RF1R_FOVR1 ((uint32_t)0x00000010) /* FIFO 1 overrun */
\r
85 #define RF1R_RFOM1 ((uint32_t)0x00000020) /* Release FIFO 1 output mailbox */
\r
87 /* CAN Error Status Register bits */
\r
88 #define ESR_EWGF ((uint32_t)0x00000001) /* Error warning flag */
\r
89 #define ESR_EPVF ((uint32_t)0x00000002) /* Error passive flag */
\r
90 #define ESR_BOFF ((uint32_t)0x00000004) /* Bus-off flag */
\r
92 /* CAN Mailbox Transmit Request */
\r
93 #define TMIDxR_TXRQ ((uint32_t)0x00000001) /* Transmit mailbox request */
\r
95 /* CAN Filter Master Register bits */
\r
96 #define FMR_FINIT ((uint32_t)0x00000001) /* Filter init mode */
\r
98 /* Time out for INAK bit */
\r
99 #define INAK_TimeOut ((uint32_t)0x0000FFFF)
\r
101 /* Time out for SLAK bit */
\r
102 #define SLAK_TimeOut ((uint32_t)0x0000FFFF)
\r
108 /** @defgroup CAN_Private_Macros
\r
116 /** @defgroup CAN_Private_Variables
\r
124 /** @defgroup CAN_Private_FunctionPrototypes
\r
128 static ITStatus CheckITStatus(uint32_t CAN_Reg, uint32_t It_Bit);
\r
134 /** @defgroup CAN_Private_Functions
\r
139 * @brief Deinitializes the CAN peripheral registers to their default reset values.
\r
140 * @param CANx: where x can be 1 or 2 to select the CAN peripheral.
\r
143 void CAN_DeInit(CAN_TypeDef* CANx)
\r
145 /* Check the parameters */
\r
146 assert_param(IS_CAN_ALL_PERIPH(CANx));
\r
150 /* Enable CAN1 reset state */
\r
151 RCC_APB1PeriphResetCmd(RCC_APB1Periph_CAN1, ENABLE);
\r
152 /* Release CAN1 from reset state */
\r
153 RCC_APB1PeriphResetCmd(RCC_APB1Periph_CAN1, DISABLE);
\r
157 /* Enable CAN2 reset state */
\r
158 RCC_APB1PeriphResetCmd(RCC_APB1Periph_CAN2, ENABLE);
\r
159 /* Release CAN2 from reset state */
\r
160 RCC_APB1PeriphResetCmd(RCC_APB1Periph_CAN2, DISABLE);
\r
165 * @brief Initializes the CAN peripheral according to the specified
\r
166 * parameters in the CAN_InitStruct.
\r
167 * @param CANx: where x can be 1 or 2 to to select the CAN peripheral.
\r
168 * @param CAN_InitStruct: pointer to a CAN_InitTypeDef structure that
\r
169 * contains the configuration information for the CAN peripheral.
\r
170 * @retval Constant indicates initialization succeed which will be
\r
171 * CANINITFAILED or CANINITOK.
\r
173 uint8_t CAN_Init(CAN_TypeDef* CANx, CAN_InitTypeDef* CAN_InitStruct)
\r
175 uint8_t InitStatus = CANINITFAILED;
\r
176 uint32_t wait_ack = 0x00000000;
\r
177 /* Check the parameters */
\r
178 assert_param(IS_CAN_ALL_PERIPH(CANx));
\r
179 assert_param(IS_FUNCTIONAL_STATE(CAN_InitStruct->CAN_TTCM));
\r
180 assert_param(IS_FUNCTIONAL_STATE(CAN_InitStruct->CAN_ABOM));
\r
181 assert_param(IS_FUNCTIONAL_STATE(CAN_InitStruct->CAN_AWUM));
\r
182 assert_param(IS_FUNCTIONAL_STATE(CAN_InitStruct->CAN_NART));
\r
183 assert_param(IS_FUNCTIONAL_STATE(CAN_InitStruct->CAN_RFLM));
\r
184 assert_param(IS_FUNCTIONAL_STATE(CAN_InitStruct->CAN_TXFP));
\r
185 assert_param(IS_CAN_MODE(CAN_InitStruct->CAN_Mode));
\r
186 assert_param(IS_CAN_SJW(CAN_InitStruct->CAN_SJW));
\r
187 assert_param(IS_CAN_BS1(CAN_InitStruct->CAN_BS1));
\r
188 assert_param(IS_CAN_BS2(CAN_InitStruct->CAN_BS2));
\r
189 assert_param(IS_CAN_PRESCALER(CAN_InitStruct->CAN_Prescaler));
\r
191 /* exit from sleep mode */
\r
192 CANx->MCR &= ~MCR_SLEEP;
\r
194 /* Request initialisation */
\r
195 CANx->MCR |= MCR_INRQ ;
\r
197 /* Wait the acknowledge */
\r
198 while (((CANx->MSR & MSR_INAK) != MSR_INAK) && (wait_ack != INAK_TimeOut))
\r
203 /* ...and check acknowledged */
\r
204 if ((CANx->MSR & MSR_INAK) != MSR_INAK)
\r
206 InitStatus = CANINITFAILED;
\r
210 /* Set the time triggered communication mode */
\r
211 if (CAN_InitStruct->CAN_TTCM == ENABLE)
\r
213 CANx->MCR |= MCR_TTCM;
\r
217 CANx->MCR &= ~MCR_TTCM;
\r
220 /* Set the automatic bus-off management */
\r
221 if (CAN_InitStruct->CAN_ABOM == ENABLE)
\r
223 CANx->MCR |= MCR_ABOM;
\r
227 CANx->MCR &= ~MCR_ABOM;
\r
230 /* Set the automatic wake-up mode */
\r
231 if (CAN_InitStruct->CAN_AWUM == ENABLE)
\r
233 CANx->MCR |= MCR_AWUM;
\r
237 CANx->MCR &= ~MCR_AWUM;
\r
240 /* Set the no automatic retransmission */
\r
241 if (CAN_InitStruct->CAN_NART == ENABLE)
\r
243 CANx->MCR |= MCR_NART;
\r
247 CANx->MCR &= ~MCR_NART;
\r
250 /* Set the receive FIFO locked mode */
\r
251 if (CAN_InitStruct->CAN_RFLM == ENABLE)
\r
253 CANx->MCR |= MCR_RFLM;
\r
257 CANx->MCR &= ~MCR_RFLM;
\r
260 /* Set the transmit FIFO priority */
\r
261 if (CAN_InitStruct->CAN_TXFP == ENABLE)
\r
263 CANx->MCR |= MCR_TXFP;
\r
267 CANx->MCR &= ~MCR_TXFP;
\r
270 /* Set the bit timing register */
\r
271 CANx->BTR = (uint32_t)((uint32_t)CAN_InitStruct->CAN_Mode << 30) | ((uint32_t)CAN_InitStruct->CAN_SJW << 24) |
\r
272 ((uint32_t)CAN_InitStruct->CAN_BS1 << 16) | ((uint32_t)CAN_InitStruct->CAN_BS2 << 20) |
\r
273 ((uint32_t)CAN_InitStruct->CAN_Prescaler - 1);
\r
275 /* Request leave initialisation */
\r
276 CANx->MCR &= ~MCR_INRQ;
\r
278 /* Wait the acknowledge */
\r
281 while (((CANx->MSR & MSR_INAK) == MSR_INAK) && (wait_ack != INAK_TimeOut))
\r
286 /* ...and check acknowledged */
\r
287 if ((CANx->MSR & MSR_INAK) == MSR_INAK)
\r
289 InitStatus = CANINITFAILED;
\r
293 InitStatus = CANINITOK ;
\r
297 /* At this step, return the status of initialization */
\r
302 * @brief Initializes the CAN peripheral according to the specified
\r
303 * parameters in the CAN_FilterInitStruct.
\r
304 * @param CAN_FilterInitStruct: pointer to a CAN_FilterInitTypeDef
\r
305 * structure that contains the configuration information.
\r
308 void CAN_FilterInit(CAN_FilterInitTypeDef* CAN_FilterInitStruct)
\r
310 uint32_t filter_number_bit_pos = 0;
\r
311 /* Check the parameters */
\r
312 assert_param(IS_CAN_FILTER_NUMBER(CAN_FilterInitStruct->CAN_FilterNumber));
\r
313 assert_param(IS_CAN_FILTER_MODE(CAN_FilterInitStruct->CAN_FilterMode));
\r
314 assert_param(IS_CAN_FILTER_SCALE(CAN_FilterInitStruct->CAN_FilterScale));
\r
315 assert_param(IS_CAN_FILTER_FIFO(CAN_FilterInitStruct->CAN_FilterFIFOAssignment));
\r
316 assert_param(IS_FUNCTIONAL_STATE(CAN_FilterInitStruct->CAN_FilterActivation));
\r
318 filter_number_bit_pos = ((uint32_t)0x00000001) << CAN_FilterInitStruct->CAN_FilterNumber;
\r
320 /* Initialisation mode for the filter */
\r
321 CAN1->FMR |= FMR_FINIT;
\r
323 /* Filter Deactivation */
\r
324 CAN1->FA1R &= ~(uint32_t)filter_number_bit_pos;
\r
327 if (CAN_FilterInitStruct->CAN_FilterScale == CAN_FilterScale_16bit)
\r
329 /* 16-bit scale for the filter */
\r
330 CAN1->FS1R &= ~(uint32_t)filter_number_bit_pos;
\r
332 /* First 16-bit identifier and First 16-bit mask */
\r
333 /* Or First 16-bit identifier and Second 16-bit identifier */
\r
334 CAN1->sFilterRegister[CAN_FilterInitStruct->CAN_FilterNumber].FR1 =
\r
335 ((0x0000FFFF & (uint32_t)CAN_FilterInitStruct->CAN_FilterMaskIdLow) << 16) |
\r
336 (0x0000FFFF & (uint32_t)CAN_FilterInitStruct->CAN_FilterIdLow);
\r
338 /* Second 16-bit identifier and Second 16-bit mask */
\r
339 /* Or Third 16-bit identifier and Fourth 16-bit identifier */
\r
340 CAN1->sFilterRegister[CAN_FilterInitStruct->CAN_FilterNumber].FR2 =
\r
341 ((0x0000FFFF & (uint32_t)CAN_FilterInitStruct->CAN_FilterMaskIdHigh) << 16) |
\r
342 (0x0000FFFF & (uint32_t)CAN_FilterInitStruct->CAN_FilterIdHigh);
\r
345 if (CAN_FilterInitStruct->CAN_FilterScale == CAN_FilterScale_32bit)
\r
347 /* 32-bit scale for the filter */
\r
348 CAN1->FS1R |= filter_number_bit_pos;
\r
349 /* 32-bit identifier or First 32-bit identifier */
\r
350 CAN1->sFilterRegister[CAN_FilterInitStruct->CAN_FilterNumber].FR1 =
\r
351 ((0x0000FFFF & (uint32_t)CAN_FilterInitStruct->CAN_FilterIdHigh) << 16) |
\r
352 (0x0000FFFF & (uint32_t)CAN_FilterInitStruct->CAN_FilterIdLow);
\r
353 /* 32-bit mask or Second 32-bit identifier */
\r
354 CAN1->sFilterRegister[CAN_FilterInitStruct->CAN_FilterNumber].FR2 =
\r
355 ((0x0000FFFF & (uint32_t)CAN_FilterInitStruct->CAN_FilterMaskIdHigh) << 16) |
\r
356 (0x0000FFFF & (uint32_t)CAN_FilterInitStruct->CAN_FilterMaskIdLow);
\r
360 if (CAN_FilterInitStruct->CAN_FilterMode == CAN_FilterMode_IdMask)
\r
362 /*Id/Mask mode for the filter*/
\r
363 CAN1->FM1R &= ~(uint32_t)filter_number_bit_pos;
\r
365 else /* CAN_FilterInitStruct->CAN_FilterMode == CAN_FilterMode_IdList */
\r
367 /*Identifier list mode for the filter*/
\r
368 CAN1->FM1R |= (uint32_t)filter_number_bit_pos;
\r
371 /* Filter FIFO assignment */
\r
372 if (CAN_FilterInitStruct->CAN_FilterFIFOAssignment == CAN_FilterFIFO0)
\r
374 /* FIFO 0 assignation for the filter */
\r
375 CAN1->FFA1R &= ~(uint32_t)filter_number_bit_pos;
\r
378 if (CAN_FilterInitStruct->CAN_FilterFIFOAssignment == CAN_FilterFIFO1)
\r
380 /* FIFO 1 assignation for the filter */
\r
381 CAN1->FFA1R |= (uint32_t)filter_number_bit_pos;
\r
384 /* Filter activation */
\r
385 if (CAN_FilterInitStruct->CAN_FilterActivation == ENABLE)
\r
387 CAN1->FA1R |= filter_number_bit_pos;
\r
390 /* Leave the initialisation mode for the filter */
\r
391 CAN1->FMR &= ~FMR_FINIT;
\r
395 * @brief Fills each CAN_InitStruct member with its default value.
\r
396 * @param CAN_InitStruct: pointer to a CAN_InitTypeDef structure which
\r
397 * will be initialized.
\r
400 void CAN_StructInit(CAN_InitTypeDef* CAN_InitStruct)
\r
402 /* Reset CAN init structure parameters values */
\r
403 /* Initialize the time triggered communication mode */
\r
404 CAN_InitStruct->CAN_TTCM = DISABLE;
\r
405 /* Initialize the automatic bus-off management */
\r
406 CAN_InitStruct->CAN_ABOM = DISABLE;
\r
407 /* Initialize the automatic wake-up mode */
\r
408 CAN_InitStruct->CAN_AWUM = DISABLE;
\r
409 /* Initialize the no automatic retransmission */
\r
410 CAN_InitStruct->CAN_NART = DISABLE;
\r
411 /* Initialize the receive FIFO locked mode */
\r
412 CAN_InitStruct->CAN_RFLM = DISABLE;
\r
413 /* Initialize the transmit FIFO priority */
\r
414 CAN_InitStruct->CAN_TXFP = DISABLE;
\r
415 /* Initialize the CAN_Mode member */
\r
416 CAN_InitStruct->CAN_Mode = CAN_Mode_Normal;
\r
417 /* Initialize the CAN_SJW member */
\r
418 CAN_InitStruct->CAN_SJW = CAN_SJW_1tq;
\r
419 /* Initialize the CAN_BS1 member */
\r
420 CAN_InitStruct->CAN_BS1 = CAN_BS1_4tq;
\r
421 /* Initialize the CAN_BS2 member */
\r
422 CAN_InitStruct->CAN_BS2 = CAN_BS2_3tq;
\r
423 /* Initialize the CAN_Prescaler member */
\r
424 CAN_InitStruct->CAN_Prescaler = 1;
\r
428 * @brief Select the start bank filter for slave CAN.
\r
429 * @note This function applies only to STM32 Connectivity line devices.
\r
430 * @param CAN_BankNumber: Select the start slave bank filter from 1..27.
\r
433 void CAN_SlaveStartBank(uint8_t CAN_BankNumber)
\r
435 /* Check the parameters */
\r
436 assert_param(IS_CAN_BANKNUMBER(CAN_BankNumber));
\r
437 /* enter Initialisation mode for the filter */
\r
438 CAN1->FMR |= FMR_FINIT;
\r
439 /* Select the start slave bank */
\r
440 CAN1->FMR &= (uint32_t)0xFFFFC0F1 ;
\r
441 CAN1->FMR |= (uint32_t)(CAN_BankNumber)<<8;
\r
442 /* Leave Initialisation mode for the filter */
\r
443 CAN1->FMR &= ~FMR_FINIT;
\r
447 * @brief Enables or disables the specified CAN interrupts.
\r
448 * @param CANx: where x can be 1 or 2 to to select the CAN peripheral.
\r
449 * @param CAN_IT: specifies the CAN interrupt sources to be enabled or disabled.
\r
450 * This parameter can be: CAN_IT_TME, CAN_IT_FMP0, CAN_IT_FF0,
\r
451 * CAN_IT_FOV0, CAN_IT_FMP1, CAN_IT_FF1,
\r
452 * CAN_IT_FOV1, CAN_IT_EWG, CAN_IT_EPV,
\r
453 * CAN_IT_LEC, CAN_IT_ERR, CAN_IT_WKU or
\r
455 * @param NewState: new state of the CAN interrupts.
\r
456 * This parameter can be: ENABLE or DISABLE.
\r
459 void CAN_ITConfig(CAN_TypeDef* CANx, uint32_t CAN_IT, FunctionalState NewState)
\r
461 /* Check the parameters */
\r
462 assert_param(IS_CAN_ALL_PERIPH(CANx));
\r
463 assert_param(IS_CAN_ITConfig(CAN_IT));
\r
464 assert_param(IS_FUNCTIONAL_STATE(NewState));
\r
466 if (NewState != DISABLE)
\r
468 /* Enable the selected CAN interrupt */
\r
469 CANx->IER |= CAN_IT;
\r
473 /* Disable the selected CAN interrupt */
\r
474 CANx->IER &= ~CAN_IT;
\r
479 * @brief Initiates the transmission of a message.
\r
480 * @param CANx: where x can be 1 or 2 to to select the CAN peripheral.
\r
481 * @param TxMessage: pointer to a structure which contains CAN Id, CAN
\r
482 * DLC and CAN datas.
\r
483 * @retval The number of the mailbox that is used for transmission
\r
484 * or CAN_NO_MB if there is no empty mailbox.
\r
486 uint8_t CAN_Transmit(CAN_TypeDef* CANx, CanTxMsg* TxMessage)
\r
488 uint8_t transmit_mailbox = 0;
\r
489 /* Check the parameters */
\r
490 assert_param(IS_CAN_ALL_PERIPH(CANx));
\r
491 assert_param(IS_CAN_IDTYPE(TxMessage->IDE));
\r
492 assert_param(IS_CAN_RTR(TxMessage->RTR));
\r
493 assert_param(IS_CAN_DLC(TxMessage->DLC));
\r
495 /* Select one empty transmit mailbox */
\r
496 if ((CANx->TSR&TSR_TME0) == TSR_TME0)
\r
498 transmit_mailbox = 0;
\r
500 else if ((CANx->TSR&TSR_TME1) == TSR_TME1)
\r
502 transmit_mailbox = 1;
\r
504 else if ((CANx->TSR&TSR_TME2) == TSR_TME2)
\r
506 transmit_mailbox = 2;
\r
510 transmit_mailbox = CAN_NO_MB;
\r
513 if (transmit_mailbox != CAN_NO_MB)
\r
515 /* Set up the Id */
\r
516 CANx->sTxMailBox[transmit_mailbox].TIR &= TMIDxR_TXRQ;
\r
517 if (TxMessage->IDE == CAN_ID_STD)
\r
519 assert_param(IS_CAN_STDID(TxMessage->StdId));
\r
520 CANx->sTxMailBox[transmit_mailbox].TIR |= ((TxMessage->StdId << 21) | TxMessage->RTR);
\r
524 assert_param(IS_CAN_EXTID(TxMessage->ExtId));
\r
525 CANx->sTxMailBox[transmit_mailbox].TIR |= ((TxMessage->ExtId<<3) | TxMessage->IDE |
\r
530 /* Set up the DLC */
\r
531 TxMessage->DLC &= (uint8_t)0x0000000F;
\r
532 CANx->sTxMailBox[transmit_mailbox].TDTR &= (uint32_t)0xFFFFFFF0;
\r
533 CANx->sTxMailBox[transmit_mailbox].TDTR |= TxMessage->DLC;
\r
535 /* Set up the data field */
\r
536 CANx->sTxMailBox[transmit_mailbox].TDLR = (((uint32_t)TxMessage->Data[3] << 24) |
\r
537 ((uint32_t)TxMessage->Data[2] << 16) |
\r
538 ((uint32_t)TxMessage->Data[1] << 8) |
\r
539 ((uint32_t)TxMessage->Data[0]));
\r
540 CANx->sTxMailBox[transmit_mailbox].TDHR = (((uint32_t)TxMessage->Data[7] << 24) |
\r
541 ((uint32_t)TxMessage->Data[6] << 16) |
\r
542 ((uint32_t)TxMessage->Data[5] << 8) |
\r
543 ((uint32_t)TxMessage->Data[4]));
\r
544 /* Request transmission */
\r
545 CANx->sTxMailBox[transmit_mailbox].TIR |= TMIDxR_TXRQ;
\r
547 return transmit_mailbox;
\r
551 * @brief Checks the transmission of a message.
\r
552 * @param CANx: where x can be 1 or 2 to to select the CAN peripheral.
\r
553 * @param TransmitMailbox: the number of the mailbox that is used for transmission.
\r
554 * @retval CANTXOK if the CAN driver transmits the message, CANTXFAILED in an other case.
\r
556 uint8_t CAN_TransmitStatus(CAN_TypeDef* CANx, uint8_t TransmitMailbox)
\r
558 /* RQCP, TXOK and TME bits */
\r
560 /* Check the parameters */
\r
561 assert_param(IS_CAN_ALL_PERIPH(CANx));
\r
562 assert_param(IS_CAN_TRANSMITMAILBOX(TransmitMailbox));
\r
563 switch (TransmitMailbox)
\r
565 case (0): state |= (uint8_t)((CANx->TSR & TSR_RQCP0) << 2);
\r
566 state |= (uint8_t)((CANx->TSR & TSR_TXOK0) >> 0);
\r
567 state |= (uint8_t)((CANx->TSR & TSR_TME0) >> 26);
\r
569 case (1): state |= (uint8_t)((CANx->TSR & TSR_RQCP1) >> 6);
\r
570 state |= (uint8_t)((CANx->TSR & TSR_TXOK1) >> 8);
\r
571 state |= (uint8_t)((CANx->TSR & TSR_TME1) >> 27);
\r
573 case (2): state |= (uint8_t)((CANx->TSR & TSR_RQCP2) >> 14);
\r
574 state |= (uint8_t)((CANx->TSR & TSR_TXOK2) >> 16);
\r
575 state |= (uint8_t)((CANx->TSR & TSR_TME2) >> 28);
\r
578 state = CANTXFAILED;
\r
583 /* transmit pending */
\r
584 case (0x0): state = CANTXPENDING;
\r
586 /* transmit failed */
\r
587 case (0x5): state = CANTXFAILED;
\r
589 /* transmit succedeed */
\r
590 case (0x7): state = CANTXOK;
\r
593 state = CANTXFAILED;
\r
600 * @brief Cancels a transmit request.
\r
601 * @param CANx: where x can be 1 or 2 to to select the CAN peripheral.
\r
602 * @param Mailbox: Mailbox number.
\r
605 void CAN_CancelTransmit(CAN_TypeDef* CANx, uint8_t Mailbox)
\r
607 /* Check the parameters */
\r
608 assert_param(IS_CAN_ALL_PERIPH(CANx));
\r
609 assert_param(IS_CAN_TRANSMITMAILBOX(Mailbox));
\r
610 /* abort transmission */
\r
613 case (0): CANx->TSR |= TSR_ABRQ0;
\r
615 case (1): CANx->TSR |= TSR_ABRQ1;
\r
617 case (2): CANx->TSR |= TSR_ABRQ2;
\r
625 * @brief Releases a FIFO.
\r
626 * @param CANx: where x can be 1 or 2 to to select the CAN peripheral.
\r
627 * @param FIFONumber: FIFO to release, CAN_FIFO0 or CAN_FIFO1.
\r
630 void CAN_FIFORelease(CAN_TypeDef* CANx, uint8_t FIFONumber)
\r
632 /* Check the parameters */
\r
633 assert_param(IS_CAN_ALL_PERIPH(CANx));
\r
634 assert_param(IS_CAN_FIFO(FIFONumber));
\r
635 /* Release FIFO0 */
\r
636 if (FIFONumber == CAN_FIFO0)
\r
638 CANx->RF0R = RF0R_RFOM0;
\r
640 /* Release FIFO1 */
\r
641 else /* FIFONumber == CAN_FIFO1 */
\r
643 CANx->RF1R = RF1R_RFOM1;
\r
648 * @brief Returns the number of pending messages.
\r
649 * @param CANx: where x can be 1 or 2 to to select the CAN peripheral.
\r
650 * @param FIFONumber: Receive FIFO number, CAN_FIFO0 or CAN_FIFO1.
\r
651 * @retval NbMessage which is the number of pending message.
\r
653 uint8_t CAN_MessagePending(CAN_TypeDef* CANx, uint8_t FIFONumber)
\r
655 uint8_t message_pending=0;
\r
656 /* Check the parameters */
\r
657 assert_param(IS_CAN_ALL_PERIPH(CANx));
\r
658 assert_param(IS_CAN_FIFO(FIFONumber));
\r
659 if (FIFONumber == CAN_FIFO0)
\r
661 message_pending = (uint8_t)(CANx->RF0R&(uint32_t)0x03);
\r
663 else if (FIFONumber == CAN_FIFO1)
\r
665 message_pending = (uint8_t)(CANx->RF1R&(uint32_t)0x03);
\r
669 message_pending = 0;
\r
671 return message_pending;
\r
675 * @brief Receives a message.
\r
676 * @param CANx: where x can be 1 or 2 to to select the CAN peripheral.
\r
677 * @param FIFONumber: Receive FIFO number, CAN_FIFO0 or CAN_FIFO1.
\r
678 * @param RxMessage: pointer to a structure receive message which
\r
679 * contains CAN Id, CAN DLC, CAN datas and FMI number.
\r
682 void CAN_Receive(CAN_TypeDef* CANx, uint8_t FIFONumber, CanRxMsg* RxMessage)
\r
684 /* Check the parameters */
\r
685 assert_param(IS_CAN_ALL_PERIPH(CANx));
\r
686 assert_param(IS_CAN_FIFO(FIFONumber));
\r
688 RxMessage->IDE = (uint8_t)0x04 & CANx->sFIFOMailBox[FIFONumber].RIR;
\r
689 if (RxMessage->IDE == CAN_ID_STD)
\r
691 RxMessage->StdId = (uint32_t)0x000007FF & (CANx->sFIFOMailBox[FIFONumber].RIR >> 21);
\r
695 RxMessage->ExtId = (uint32_t)0x1FFFFFFF & (CANx->sFIFOMailBox[FIFONumber].RIR >> 3);
\r
698 RxMessage->RTR = (uint8_t)0x02 & CANx->sFIFOMailBox[FIFONumber].RIR;
\r
700 RxMessage->DLC = (uint8_t)0x0F & CANx->sFIFOMailBox[FIFONumber].RDTR;
\r
702 RxMessage->FMI = (uint8_t)0xFF & (CANx->sFIFOMailBox[FIFONumber].RDTR >> 8);
\r
703 /* Get the data field */
\r
704 RxMessage->Data[0] = (uint8_t)0xFF & CANx->sFIFOMailBox[FIFONumber].RDLR;
\r
705 RxMessage->Data[1] = (uint8_t)0xFF & (CANx->sFIFOMailBox[FIFONumber].RDLR >> 8);
\r
706 RxMessage->Data[2] = (uint8_t)0xFF & (CANx->sFIFOMailBox[FIFONumber].RDLR >> 16);
\r
707 RxMessage->Data[3] = (uint8_t)0xFF & (CANx->sFIFOMailBox[FIFONumber].RDLR >> 24);
\r
708 RxMessage->Data[4] = (uint8_t)0xFF & CANx->sFIFOMailBox[FIFONumber].RDHR;
\r
709 RxMessage->Data[5] = (uint8_t)0xFF & (CANx->sFIFOMailBox[FIFONumber].RDHR >> 8);
\r
710 RxMessage->Data[6] = (uint8_t)0xFF & (CANx->sFIFOMailBox[FIFONumber].RDHR >> 16);
\r
711 RxMessage->Data[7] = (uint8_t)0xFF & (CANx->sFIFOMailBox[FIFONumber].RDHR >> 24);
\r
712 /* Release the FIFO */
\r
713 CAN_FIFORelease(CANx, FIFONumber);
\r
717 * @brief Enables or disables the DBG Freeze for CAN.
\r
718 * @param CANx: where x can be 1 or 2 to to select the CAN peripheral.
\r
719 * @param NewState: new state of the CAN peripheral.
\r
720 * This parameter can be: ENABLE or DISABLE.
\r
723 void CAN_DBGFreeze(CAN_TypeDef* CANx, FunctionalState NewState)
\r
725 /* Check the parameters */
\r
726 assert_param(IS_CAN_ALL_PERIPH(CANx));
\r
727 assert_param(IS_FUNCTIONAL_STATE(NewState));
\r
729 if (NewState != DISABLE)
\r
731 /* Enable Debug Freeze */
\r
732 CANx->MCR |= MCR_DBF;
\r
736 /* Disable Debug Freeze */
\r
737 CANx->MCR &= ~MCR_DBF;
\r
742 * @brief Enters the low power mode.
\r
743 * @param CANx: where x can be 1 or 2 to to select the CAN peripheral.
\r
744 * @retval CANSLEEPOK if sleep entered, CANSLEEPFAILED in an other case.
\r
746 uint8_t CAN_Sleep(CAN_TypeDef* CANx)
\r
748 uint8_t sleepstatus = CANSLEEPFAILED;
\r
750 /* Check the parameters */
\r
751 assert_param(IS_CAN_ALL_PERIPH(CANx));
\r
753 /* Request Sleep mode */
\r
754 CANx->MCR = (((CANx->MCR) & (uint32_t)(~MCR_INRQ)) | MCR_SLEEP);
\r
756 /* Sleep mode status */
\r
757 if ((CANx->MSR & (CAN_MSR_SLAK|CAN_MSR_INAK)) == CAN_MSR_SLAK)
\r
759 /* Sleep mode not entered */
\r
760 sleepstatus = CANSLEEPOK;
\r
762 /* At this step, sleep mode status */
\r
763 return (uint8_t)sleepstatus;
\r
767 * @brief Wakes the CAN up.
\r
768 * @param CANx: where x can be 1 or 2 to to select the CAN peripheral.
\r
769 * @retval CANWAKEUPOK if sleep mode left, CANWAKEUPFAILED in an other case.
\r
771 uint8_t CAN_WakeUp(CAN_TypeDef* CANx)
\r
773 uint32_t wait_slak = SLAK_TimeOut ;
\r
774 uint8_t wakeupstatus = CANWAKEUPFAILED;
\r
776 /* Check the parameters */
\r
777 assert_param(IS_CAN_ALL_PERIPH(CANx));
\r
779 /* Wake up request */
\r
780 CANx->MCR &= ~MCR_SLEEP;
\r
782 /* Sleep mode status */
\r
783 while(((CANx->MSR & CAN_MSR_SLAK) == CAN_MSR_SLAK)&&(wait_slak!=0x00))
\r
787 if((CANx->MSR & CAN_MSR_SLAK) != CAN_MSR_SLAK)
\r
789 /* Sleep mode exited */
\r
790 wakeupstatus = CANWAKEUPOK;
\r
792 /* At this step, sleep mode status */
\r
793 return (uint8_t)wakeupstatus;
\r
797 * @brief Checks whether the specified CAN flag is set or not.
\r
798 * @param CANx: where x can be 1 or 2 to to select the CAN peripheral.
\r
799 * @param CAN_FLAG: specifies the flag to check.
\r
800 * This parameter can be: CAN_FLAG_EWG, CAN_FLAG_EPV or CAN_FLAG_BOF.
\r
801 * @retval The new state of CAN_FLAG (SET or RESET).
\r
803 FlagStatus CAN_GetFlagStatus(CAN_TypeDef* CANx, uint32_t CAN_FLAG)
\r
805 FlagStatus bitstatus = RESET;
\r
806 /* Check the parameters */
\r
807 assert_param(IS_CAN_ALL_PERIPH(CANx));
\r
808 assert_param(IS_CAN_FLAG(CAN_FLAG));
\r
809 /* Check the status of the specified CAN flag */
\r
810 if ((CANx->ESR & CAN_FLAG) != (uint32_t)RESET)
\r
812 /* CAN_FLAG is set */
\r
817 /* CAN_FLAG is reset */
\r
820 /* Return the CAN_FLAG status */
\r
825 * @brief Clears the CAN's pending flags.
\r
826 * @param CANx: where x can be 1 or 2 to to select the CAN peripheral.
\r
827 * @param CAN_FLAG: specifies the flag to clear.
\r
830 void CAN_ClearFlag(CAN_TypeDef* CANx, uint32_t CAN_FLAG)
\r
832 /* Check the parameters */
\r
833 assert_param(IS_CAN_ALL_PERIPH(CANx));
\r
834 assert_param(IS_CAN_FLAG(CAN_FLAG));
\r
835 /* Clear the selected CAN flags */
\r
836 CANx->ESR &= ~CAN_FLAG;
\r
840 * @brief Checks whether the specified CAN interrupt has occurred or not.
\r
841 * @param CANx: where x can be 1 or 2 to to select the CAN peripheral.
\r
842 * @param CAN_IT: specifies the CAN interrupt source to check.
\r
843 * This parameter can be: CAN_IT_RQCP0, CAN_IT_RQCP1, CAN_IT_RQCP2,
\r
844 * CAN_IT_FF0, CAN_IT_FOV0, CAN_IT_FF1,
\r
845 * CAN_IT_FOV1, CAN_IT_EWG, CAN_IT_EPV,
\r
846 * CAN_IT_BOF, CAN_IT_WKU or CAN_IT_SLK.
\r
847 * @retval The new state of CAN_IT (SET or RESET).
\r
849 ITStatus CAN_GetITStatus(CAN_TypeDef* CANx, uint32_t CAN_IT)
\r
851 ITStatus pendingbitstatus = RESET;
\r
852 /* Check the parameters */
\r
853 assert_param(IS_CAN_ALL_PERIPH(CANx));
\r
854 assert_param(IS_CAN_ITStatus(CAN_IT));
\r
858 pendingbitstatus = CheckITStatus(CANx->TSR, TSR_RQCP0);
\r
861 pendingbitstatus = CheckITStatus(CANx->TSR, TSR_RQCP1);
\r
864 pendingbitstatus = CheckITStatus(CANx->TSR, TSR_RQCP2);
\r
867 pendingbitstatus = CheckITStatus(CANx->RF0R, RF0R_FULL0);
\r
870 pendingbitstatus = CheckITStatus(CANx->RF0R, RF0R_FOVR0);
\r
873 pendingbitstatus = CheckITStatus(CANx->RF1R, RF1R_FULL1);
\r
876 pendingbitstatus = CheckITStatus(CANx->RF1R, RF1R_FOVR1);
\r
879 pendingbitstatus = CheckITStatus(CANx->ESR, ESR_EWGF);
\r
882 pendingbitstatus = CheckITStatus(CANx->ESR, ESR_EPVF);
\r
885 pendingbitstatus = CheckITStatus(CANx->ESR, ESR_BOFF);
\r
888 pendingbitstatus = CheckITStatus(CANx->MSR, MSR_SLAKI);
\r
891 pendingbitstatus = CheckITStatus(CANx->MSR, MSR_WKUI);
\r
894 pendingbitstatus = RESET;
\r
897 /* Return the CAN_IT status */
\r
898 return pendingbitstatus;
\r
902 * @brief Clears the CAN
\92s interrupt pending bits.
\r
903 * @param CANx: where x can be 1 or 2 to to select the CAN peripheral.
\r
904 * @param CAN_IT: specifies the interrupt pending bit to clear.
\r
907 void CAN_ClearITPendingBit(CAN_TypeDef* CANx, uint32_t CAN_IT)
\r
909 /* Check the parameters */
\r
910 assert_param(IS_CAN_ALL_PERIPH(CANx));
\r
911 assert_param(IS_CAN_ITStatus(CAN_IT));
\r
915 CANx->TSR = TSR_RQCP0; /* rc_w1*/
\r
918 CANx->TSR = TSR_RQCP1; /* rc_w1*/
\r
921 CANx->TSR = TSR_RQCP2; /* rc_w1*/
\r
924 CANx->RF0R = RF0R_FULL0; /* rc_w1*/
\r
927 CANx->RF0R = RF0R_FOVR0; /* rc_w1*/
\r
930 CANx->RF1R = RF1R_FULL1; /* rc_w1*/
\r
933 CANx->RF1R = RF1R_FOVR1; /* rc_w1*/
\r
936 CANx->ESR &= ~ ESR_EWGF; /* rw */
\r
939 CANx->ESR &= ~ ESR_EPVF; /* rw */
\r
942 CANx->ESR &= ~ ESR_BOFF; /* rw */
\r
945 CANx->MSR = MSR_WKUI; /* rc_w1*/
\r
948 CANx->MSR = MSR_SLAKI; /* rc_w1*/
\r
956 * @brief Checks whether the CAN interrupt has occurred or not.
\r
957 * @param CAN_Reg: specifies the CAN interrupt register to check.
\r
958 * @param It_Bit: specifies the interrupt source bit to check.
\r
959 * @retval The new state of the CAN Interrupt (SET or RESET).
\r
961 static ITStatus CheckITStatus(uint32_t CAN_Reg, uint32_t It_Bit)
\r
963 ITStatus pendingbitstatus = RESET;
\r
965 if ((CAN_Reg & It_Bit) != (uint32_t)RESET)
\r
967 /* CAN_IT is set */
\r
968 pendingbitstatus = SET;
\r
972 /* CAN_IT is reset */
\r
973 pendingbitstatus = RESET;
\r
975 return pendingbitstatus;
\r
990 /******************* (C) COPYRIGHT 2010 STMicroelectronics *****END OF FILE****/
\r