Merge pull request #10558 from iNavFlight/MrD_Correct-comments-on-OSD-symbols
[inav.git] / lib / main / STM32F7 / Drivers / STM32F7xx_HAL_Driver / Src / stm32f7xx_hal_can.c
blob6b122b823ffa9b38490d1f15e42ffbab36e1b903
1 /**
2 ******************************************************************************
3 * @file stm32f7xx_hal_can.c
4 * @author MCD Application Team
5 * @version V1.2.2
6 * @date 14-April-2017
7 * @brief CAN HAL module driver.
8 * This file provides firmware functions to manage the following
9 * functionalities of the Controller Area Network (CAN) peripheral:
10 * + Initialization and de-initialization functions
11 * + IO operation functions
12 * + Peripheral Control functions
13 * + Peripheral State and Error functions
15 @verbatim
16 ==============================================================================
17 ##### How to use this driver #####
18 ==============================================================================
19 [..]
20 (#) Enable the CAN controller interface clock using
21 __HAL_RCC_CAN1_CLK_ENABLE() for CAN1, __HAL_RCC_CAN2_CLK_ENABLE() for CAN2
22 and __HAL_RCC_CAN3_CLK_ENABLE() for CAN3
23 -@- In case you are using CAN2 only, you have to enable the CAN1 clock.
25 (#) CAN pins configuration
26 (++) Enable the clock for the CAN GPIOs using the following function:
27 __HAL_RCC_GPIOx_CLK_ENABLE()
28 (++) Connect and configure the involved CAN pins to AF9 using the
29 following function HAL_GPIO_Init()
31 (#) Initialize and configure the CAN using HAL_CAN_Init() function.
33 (#) Transmit the desired CAN frame using HAL_CAN_Transmit() function.
35 (#) Or transmit the desired CAN frame using HAL_CAN_Transmit_IT() function.
37 (#) Receive a CAN frame using HAL_CAN_Receive() function.
39 (#) Or receive a CAN frame using HAL_CAN_Receive_IT() function.
41 *** Polling mode IO operation ***
42 =================================
43 [..]
44 (+) Start the CAN peripheral transmission and wait the end of this operation
45 using HAL_CAN_Transmit(), at this stage user can specify the value of timeout
46 according to his end application
47 (+) Start the CAN peripheral reception and wait the end of this operation
48 using HAL_CAN_Receive(), at this stage user can specify the value of timeout
49 according to his end application
51 *** Interrupt mode IO operation ***
52 ===================================
53 [..]
54 (+) Start the CAN peripheral transmission using HAL_CAN_Transmit_IT()
55 (+) Start the CAN peripheral reception using HAL_CAN_Receive_IT()
56 (+) Use HAL_CAN_IRQHandler() called under the used CAN Interrupt subroutine
57 (+) At CAN end of transmission HAL_CAN_TxCpltCallback() function is executed and user can
58 add his own code by customization of function pointer HAL_CAN_TxCpltCallback
59 (+) In case of CAN Error, HAL_CAN_ErrorCallback() function is executed and user can
60 add his own code by customization of function pointer HAL_CAN_ErrorCallback
62 *** CAN HAL driver macros list ***
63 =============================================
64 [..]
65 Below the list of most used macros in CAN HAL driver.
67 (+) __HAL_CAN_ENABLE_IT: Enable the specified CAN interrupts
68 (+) __HAL_CAN_DISABLE_IT: Disable the specified CAN interrupts
69 (+) __HAL_CAN_GET_IT_SOURCE: Check if the specified CAN interrupt source is enabled or disabled
70 (+) __HAL_CAN_CLEAR_FLAG: Clear the CAN's pending flags
71 (+) __HAL_CAN_GET_FLAG: Get the selected CAN's flag status
73 [..]
74 (@) You can refer to the CAN HAL driver header file for more useful macros
76 @endverbatim
78 ******************************************************************************
79 * @attention
81 * <h2><center>&copy; COPYRIGHT(c) 2017 STMicroelectronics</center></h2>
83 * Redistribution and use in source and binary forms, with or without modification,
84 * are permitted provided that the following conditions are met:
85 * 1. Redistributions of source code must retain the above copyright notice,
86 * this list of conditions and the following disclaimer.
87 * 2. Redistributions in binary form must reproduce the above copyright notice,
88 * this list of conditions and the following disclaimer in the documentation
89 * and/or other materials provided with the distribution.
90 * 3. Neither the name of STMicroelectronics nor the names of its contributors
91 * may be used to endorse or promote products derived from this software
92 * without specific prior written permission.
94 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
95 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
96 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
97 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
98 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
99 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
100 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
101 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
102 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
103 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
105 ******************************************************************************
108 /* Includes ------------------------------------------------------------------*/
109 #include "stm32f7xx_hal.h"
111 /** @addtogroup STM32F7xx_HAL_Driver
112 * @{
115 /** @defgroup CAN CAN
116 * @brief CAN driver modules
117 * @{
120 #ifdef HAL_CAN_MODULE_ENABLED
123 /* Private typedef -----------------------------------------------------------*/
124 /* Private define ------------------------------------------------------------*/
125 /** @addtogroup CAN_Private_Constants
126 * @{
128 #define CAN_TIMEOUT_VALUE 10
130 * @}
132 /* Private macro -------------------------------------------------------------*/
133 /* Private variables ---------------------------------------------------------*/
134 /* Private function prototypes -----------------------------------------------*/
135 /** @addtogroup CAN_Private_Functions
136 * @{
138 static HAL_StatusTypeDef CAN_Receive_IT(CAN_HandleTypeDef* hcan, uint8_t FIFONumber);
139 static HAL_StatusTypeDef CAN_Transmit_IT(CAN_HandleTypeDef* hcan);
141 * @}
144 /* Exported functions --------------------------------------------------------*/
145 /** @defgroup CAN_Exported_Functions CAN Exported Functions
146 * @{
149 /** @defgroup CAN_Exported_Functions_Group1 Initialization and de-initialization functions
150 * @brief Initialization and Configuration functions
152 @verbatim
153 ==============================================================================
154 ##### Initialization and de-initialization functions #####
155 ==============================================================================
156 [..] This section provides functions allowing to:
157 (+) Initialize and configure the CAN.
158 (+) De-initialize the CAN.
160 @endverbatim
161 * @{
165 * @brief Initializes the CAN peripheral according to the specified
166 * parameters in the CAN_InitStruct.
167 * @param hcan: pointer to a CAN_HandleTypeDef structure that contains
168 * the configuration information for the specified CAN.
169 * @retval HAL status
171 HAL_StatusTypeDef HAL_CAN_Init(CAN_HandleTypeDef* hcan)
173 uint32_t InitStatus = CAN_INITSTATUS_FAILED;
174 uint32_t tickstart = 0;
176 /* Check CAN handle */
177 if(hcan == NULL)
179 return HAL_ERROR;
182 /* Check the parameters */
183 assert_param(IS_CAN_ALL_INSTANCE(hcan->Instance));
184 assert_param(IS_FUNCTIONAL_STATE(hcan->Init.TTCM));
185 assert_param(IS_FUNCTIONAL_STATE(hcan->Init.ABOM));
186 assert_param(IS_FUNCTIONAL_STATE(hcan->Init.AWUM));
187 assert_param(IS_FUNCTIONAL_STATE(hcan->Init.NART));
188 assert_param(IS_FUNCTIONAL_STATE(hcan->Init.RFLM));
189 assert_param(IS_FUNCTIONAL_STATE(hcan->Init.TXFP));
190 assert_param(IS_CAN_MODE(hcan->Init.Mode));
191 assert_param(IS_CAN_SJW(hcan->Init.SJW));
192 assert_param(IS_CAN_BS1(hcan->Init.BS1));
193 assert_param(IS_CAN_BS2(hcan->Init.BS2));
194 assert_param(IS_CAN_PRESCALER(hcan->Init.Prescaler));
197 if(hcan->State == HAL_CAN_STATE_RESET)
199 /* Allocate lock resource and initialize it */
200 hcan->Lock = HAL_UNLOCKED;
201 /* Init the low level hardware */
202 HAL_CAN_MspInit(hcan);
205 /* Initialize the CAN state*/
206 hcan->State = HAL_CAN_STATE_BUSY;
208 /* Exit from sleep mode */
209 hcan->Instance->MCR &= (~(uint32_t)CAN_MCR_SLEEP);
211 /* Request initialisation */
212 hcan->Instance->MCR |= CAN_MCR_INRQ ;
214 /* Get tick */
215 tickstart = HAL_GetTick();
217 /* Wait the acknowledge */
218 while((hcan->Instance->MSR & CAN_MSR_INAK) != CAN_MSR_INAK)
220 if((HAL_GetTick() - tickstart ) > CAN_TIMEOUT_VALUE)
222 hcan->State= HAL_CAN_STATE_TIMEOUT;
223 /* Process unlocked */
224 __HAL_UNLOCK(hcan);
225 return HAL_TIMEOUT;
229 /* Check acknowledge */
230 if ((hcan->Instance->MSR & CAN_MSR_INAK) == CAN_MSR_INAK)
232 /* Set the time triggered communication mode */
233 if (hcan->Init.TTCM == ENABLE)
235 hcan->Instance->MCR |= CAN_MCR_TTCM;
237 else
239 hcan->Instance->MCR &= ~(uint32_t)CAN_MCR_TTCM;
242 /* Set the automatic bus-off management */
243 if (hcan->Init.ABOM == ENABLE)
245 hcan->Instance->MCR |= CAN_MCR_ABOM;
247 else
249 hcan->Instance->MCR &= ~(uint32_t)CAN_MCR_ABOM;
252 /* Set the automatic wake-up mode */
253 if (hcan->Init.AWUM == ENABLE)
255 hcan->Instance->MCR |= CAN_MCR_AWUM;
257 else
259 hcan->Instance->MCR &= ~(uint32_t)CAN_MCR_AWUM;
262 /* Set the no automatic retransmission */
263 if (hcan->Init.NART == ENABLE)
265 hcan->Instance->MCR |= CAN_MCR_NART;
267 else
269 hcan->Instance->MCR &= ~(uint32_t)CAN_MCR_NART;
272 /* Set the receive FIFO locked mode */
273 if (hcan->Init.RFLM == ENABLE)
275 hcan->Instance->MCR |= CAN_MCR_RFLM;
277 else
279 hcan->Instance->MCR &= ~(uint32_t)CAN_MCR_RFLM;
282 /* Set the transmit FIFO priority */
283 if (hcan->Init.TXFP == ENABLE)
285 hcan->Instance->MCR |= CAN_MCR_TXFP;
287 else
289 hcan->Instance->MCR &= ~(uint32_t)CAN_MCR_TXFP;
292 /* Set the bit timing register */
293 hcan->Instance->BTR = (uint32_t)((uint32_t)hcan->Init.Mode) | \
294 ((uint32_t)hcan->Init.SJW) | \
295 ((uint32_t)hcan->Init.BS1) | \
296 ((uint32_t)hcan->Init.BS2) | \
297 ((uint32_t)hcan->Init.Prescaler - 1);
299 /* Request leave initialisation */
300 hcan->Instance->MCR &= ~(uint32_t)CAN_MCR_INRQ;
302 /* Get tick */
303 tickstart = HAL_GetTick();
305 /* Wait the acknowledge */
306 while((hcan->Instance->MSR & CAN_MSR_INAK) == CAN_MSR_INAK)
308 if((HAL_GetTick() - tickstart ) > CAN_TIMEOUT_VALUE)
310 hcan->State= HAL_CAN_STATE_TIMEOUT;
311 /* Process unlocked */
312 __HAL_UNLOCK(hcan);
313 return HAL_TIMEOUT;
317 /* Check acknowledged */
318 if ((hcan->Instance->MSR & CAN_MSR_INAK) != CAN_MSR_INAK)
320 InitStatus = CAN_INITSTATUS_SUCCESS;
324 if(InitStatus == CAN_INITSTATUS_SUCCESS)
326 /* Set CAN error code to none */
327 hcan->ErrorCode = HAL_CAN_ERROR_NONE;
329 /* Initialize the CAN state */
330 hcan->State = HAL_CAN_STATE_READY;
332 /* Return function status */
333 return HAL_OK;
335 else
337 /* Initialize the CAN state */
338 hcan->State = HAL_CAN_STATE_ERROR;
340 /* Return function status */
341 return HAL_ERROR;
346 * @brief Configures the CAN reception filter according to the specified
347 * parameters in the CAN_FilterInitStruct.
348 * @param hcan: pointer to a CAN_HandleTypeDef structure that contains
349 * the configuration information for the specified CAN.
350 * @param sFilterConfig: pointer to a CAN_FilterConfTypeDef structure that
351 * contains the filter configuration information.
352 * @retval None
354 HAL_StatusTypeDef HAL_CAN_ConfigFilter(CAN_HandleTypeDef* hcan, CAN_FilterConfTypeDef* sFilterConfig)
356 uint32_t filternbrbitpos = 0;
357 CAN_TypeDef *can_ip;
359 /* Check the parameters */
360 assert_param(IS_CAN_FILTER_NUMBER(sFilterConfig->FilterNumber));
361 assert_param(IS_CAN_FILTER_MODE(sFilterConfig->FilterMode));
362 assert_param(IS_CAN_FILTER_SCALE(sFilterConfig->FilterScale));
363 assert_param(IS_CAN_FILTER_FIFO(sFilterConfig->FilterFIFOAssignment));
364 assert_param(IS_FUNCTIONAL_STATE(sFilterConfig->FilterActivation));
365 assert_param(IS_CAN_BANKNUMBER(sFilterConfig->BankNumber));
367 filternbrbitpos = ((uint32_t)1) << sFilterConfig->FilterNumber;
368 #if defined (CAN3)
369 /* Check the CAN instance */
370 if(hcan->Instance == CAN3)
372 can_ip = CAN3;
374 else
376 can_ip = CAN1;
378 #else
379 can_ip = CAN1;
380 #endif
382 /* Initialisation mode for the filter */
383 can_ip->FMR |= (uint32_t)CAN_FMR_FINIT;
385 #if defined (CAN2)
386 /* Select the start slave bank */
387 can_ip->FMR &= ~((uint32_t)CAN_FMR_CAN2SB);
388 can_ip->FMR |= (uint32_t)(sFilterConfig->BankNumber << 8);
389 #endif
391 /* Filter Deactivation */
392 can_ip->FA1R &= ~(uint32_t)filternbrbitpos;
394 /* Filter Scale */
395 if (sFilterConfig->FilterScale == CAN_FILTERSCALE_16BIT)
397 /* 16-bit scale for the filter */
398 can_ip->FS1R &= ~(uint32_t)filternbrbitpos;
400 /* First 16-bit identifier and First 16-bit mask */
401 /* Or First 16-bit identifier and Second 16-bit identifier */
402 can_ip->sFilterRegister[sFilterConfig->FilterNumber].FR1 =
403 ((0x0000FFFF & (uint32_t)sFilterConfig->FilterMaskIdLow) << 16) |
404 (0x0000FFFF & (uint32_t)sFilterConfig->FilterIdLow);
406 /* Second 16-bit identifier and Second 16-bit mask */
407 /* Or Third 16-bit identifier and Fourth 16-bit identifier */
408 can_ip->sFilterRegister[sFilterConfig->FilterNumber].FR2 =
409 ((0x0000FFFF & (uint32_t)sFilterConfig->FilterMaskIdHigh) << 16) |
410 (0x0000FFFF & (uint32_t)sFilterConfig->FilterIdHigh);
413 if (sFilterConfig->FilterScale == CAN_FILTERSCALE_32BIT)
415 /* 32-bit scale for the filter */
416 can_ip->FS1R |= filternbrbitpos;
418 /* 32-bit identifier or First 32-bit identifier */
419 can_ip->sFilterRegister[sFilterConfig->FilterNumber].FR1 =
420 ((0x0000FFFF & (uint32_t)sFilterConfig->FilterIdHigh) << 16) |
421 (0x0000FFFF & (uint32_t)sFilterConfig->FilterIdLow);
423 /* 32-bit mask or Second 32-bit identifier */
424 can_ip->sFilterRegister[sFilterConfig->FilterNumber].FR2 =
425 ((0x0000FFFF & (uint32_t)sFilterConfig->FilterMaskIdHigh) << 16) |
426 (0x0000FFFF & (uint32_t)sFilterConfig->FilterMaskIdLow);
429 /* Filter Mode */
430 if (sFilterConfig->FilterMode == CAN_FILTERMODE_IDMASK)
432 /*Id/Mask mode for the filter*/
433 can_ip->FM1R &= ~(uint32_t)filternbrbitpos;
435 else /* CAN_FilterInitStruct->CAN_FilterMode == CAN_FilterMode_IdList */
437 /*Identifier list mode for the filter*/
438 can_ip->FM1R |= (uint32_t)filternbrbitpos;
441 /* Filter FIFO assignment */
442 if (sFilterConfig->FilterFIFOAssignment == CAN_FILTER_FIFO0)
444 /* FIFO 0 assignation for the filter */
445 can_ip->FFA1R &= ~(uint32_t)filternbrbitpos;
448 if (sFilterConfig->FilterFIFOAssignment == CAN_FILTER_FIFO1)
450 /* FIFO 1 assignation for the filter */
451 can_ip->FFA1R |= (uint32_t)filternbrbitpos;
454 /* Filter activation */
455 if (sFilterConfig->FilterActivation == ENABLE)
457 can_ip->FA1R |= filternbrbitpos;
460 /* Leave the initialisation mode for the filter */
461 can_ip->FMR &= ~((uint32_t)CAN_FMR_FINIT);
463 /* Return function status */
464 return HAL_OK;
468 * @brief Deinitializes the CANx peripheral registers to their default reset values.
469 * @param hcan: pointer to a CAN_HandleTypeDef structure that contains
470 * the configuration information for the specified CAN.
471 * @retval HAL status
473 HAL_StatusTypeDef HAL_CAN_DeInit(CAN_HandleTypeDef* hcan)
475 /* Check CAN handle */
476 if(hcan == NULL)
478 return HAL_ERROR;
481 /* Check the parameters */
482 assert_param(IS_CAN_ALL_INSTANCE(hcan->Instance));
484 /* Change CAN state */
485 hcan->State = HAL_CAN_STATE_BUSY;
487 /* DeInit the low level hardware */
488 HAL_CAN_MspDeInit(hcan);
490 /* Change CAN state */
491 hcan->State = HAL_CAN_STATE_RESET;
493 /* Release Lock */
494 __HAL_UNLOCK(hcan);
496 /* Return function status */
497 return HAL_OK;
501 * @brief Initializes the CAN MSP.
502 * @param hcan: pointer to a CAN_HandleTypeDef structure that contains
503 * the configuration information for the specified CAN.
504 * @retval None
506 __weak void HAL_CAN_MspInit(CAN_HandleTypeDef* hcan)
508 /* Prevent unused argument(s) compilation warning */
509 UNUSED(hcan);
510 /* NOTE : This function Should not be modified, when the callback is needed,
511 the HAL_CAN_MspInit could be implemented in the user file
516 * @brief DeInitializes the CAN MSP.
517 * @param hcan: pointer to a CAN_HandleTypeDef structure that contains
518 * the configuration information for the specified CAN.
519 * @retval None
521 __weak void HAL_CAN_MspDeInit(CAN_HandleTypeDef* hcan)
523 /* Prevent unused argument(s) compilation warning */
524 UNUSED(hcan);
525 /* NOTE : This function Should not be modified, when the callback is needed,
526 the HAL_CAN_MspDeInit could be implemented in the user file
531 * @}
534 /** @defgroup CAN_Exported_Functions_Group2 IO operation functions
535 * @brief IO operation functions
537 @verbatim
538 ==============================================================================
539 ##### IO operation functions #####
540 ==============================================================================
541 [..] This section provides functions allowing to:
542 (+) Transmit a CAN frame message.
543 (+) Receive a CAN frame message.
544 (+) Enter CAN peripheral in sleep mode.
545 (+) Wake up the CAN peripheral from sleep mode.
547 @endverbatim
548 * @{
552 * @brief Initiates and transmits a CAN frame message.
553 * @param hcan: pointer to a CAN_HandleTypeDef structure that contains
554 * the configuration information for the specified CAN.
555 * @param Timeout: Specify Timeout value
556 * @retval HAL status
558 HAL_StatusTypeDef HAL_CAN_Transmit(CAN_HandleTypeDef* hcan, uint32_t Timeout)
560 uint32_t transmitmailbox = CAN_TXSTATUS_NOMAILBOX;
561 uint32_t tickstart = 0U;
563 /* Check the parameters */
564 assert_param(IS_CAN_IDTYPE(hcan->pTxMsg->IDE));
565 assert_param(IS_CAN_RTR(hcan->pTxMsg->RTR));
566 assert_param(IS_CAN_DLC(hcan->pTxMsg->DLC));
568 if(((hcan->Instance->TSR&CAN_TSR_TME0) == CAN_TSR_TME0) || \
569 ((hcan->Instance->TSR&CAN_TSR_TME1) == CAN_TSR_TME1) || \
570 ((hcan->Instance->TSR&CAN_TSR_TME2) == CAN_TSR_TME2))
572 /* Process locked */
573 __HAL_LOCK(hcan);
575 /* Change CAN state */
576 switch(hcan->State)
578 case(HAL_CAN_STATE_BUSY_RX0):
579 hcan->State = HAL_CAN_STATE_BUSY_TX_RX0;
580 break;
581 case(HAL_CAN_STATE_BUSY_RX1):
582 hcan->State = HAL_CAN_STATE_BUSY_TX_RX1;
583 break;
584 case(HAL_CAN_STATE_BUSY_RX0_RX1):
585 hcan->State = HAL_CAN_STATE_BUSY_TX_RX0_RX1;
586 break;
587 default: /* HAL_CAN_STATE_READY */
588 hcan->State = HAL_CAN_STATE_BUSY_TX;
589 break;
592 /* Select one empty transmit mailbox */
593 if ((hcan->Instance->TSR&CAN_TSR_TME0) == CAN_TSR_TME0)
595 transmitmailbox = CAN_TXMAILBOX_0;
597 else if ((hcan->Instance->TSR&CAN_TSR_TME1) == CAN_TSR_TME1)
599 transmitmailbox = CAN_TXMAILBOX_1;
601 else
603 transmitmailbox = CAN_TXMAILBOX_2;
606 /* Set up the Id */
607 hcan->Instance->sTxMailBox[transmitmailbox].TIR &= CAN_TI0R_TXRQ;
608 if (hcan->pTxMsg->IDE == CAN_ID_STD)
610 assert_param(IS_CAN_STDID(hcan->pTxMsg->StdId));
611 hcan->Instance->sTxMailBox[transmitmailbox].TIR |= ((hcan->pTxMsg->StdId << 21U) | \
612 hcan->pTxMsg->RTR);
614 else
616 assert_param(IS_CAN_EXTID(hcan->pTxMsg->ExtId));
617 hcan->Instance->sTxMailBox[transmitmailbox].TIR |= ((hcan->pTxMsg->ExtId << 3U) | \
618 hcan->pTxMsg->IDE | \
619 hcan->pTxMsg->RTR);
622 /* Set up the DLC */
623 hcan->pTxMsg->DLC &= (uint8_t)0x0000000F;
624 hcan->Instance->sTxMailBox[transmitmailbox].TDTR &= (uint32_t)0xFFFFFFF0U;
625 hcan->Instance->sTxMailBox[transmitmailbox].TDTR |= hcan->pTxMsg->DLC;
627 /* Set up the data field */
628 hcan->Instance->sTxMailBox[transmitmailbox].TDLR = (((uint32_t)hcan->pTxMsg->Data[3U] << 24U) |
629 ((uint32_t)hcan->pTxMsg->Data[2U] << 16U) |
630 ((uint32_t)hcan->pTxMsg->Data[1U] << 8U) |
631 ((uint32_t)hcan->pTxMsg->Data[0U]));
632 hcan->Instance->sTxMailBox[transmitmailbox].TDHR = (((uint32_t)hcan->pTxMsg->Data[7U] << 24U) |
633 ((uint32_t)hcan->pTxMsg->Data[6U] << 16U) |
634 ((uint32_t)hcan->pTxMsg->Data[5U] << 8U) |
635 ((uint32_t)hcan->pTxMsg->Data[4U]));
636 /* Request transmission */
637 hcan->Instance->sTxMailBox[transmitmailbox].TIR |= CAN_TI0R_TXRQ;
639 /* Get tick */
640 tickstart = HAL_GetTick();
642 /* Check End of transmission flag */
643 while(!(__HAL_CAN_TRANSMIT_STATUS(hcan, transmitmailbox)))
645 /* Check for the Timeout */
646 if(Timeout != HAL_MAX_DELAY)
648 if((Timeout == 0U)||((HAL_GetTick() - tickstart ) > Timeout))
650 hcan->State = HAL_CAN_STATE_TIMEOUT;
652 __HAL_CAN_CANCEL_TRANSMIT(hcan, transmitmailbox);
654 /* Process unlocked */
655 __HAL_UNLOCK(hcan);
656 return HAL_TIMEOUT;
661 /* Change CAN state */
662 switch(hcan->State)
664 case(HAL_CAN_STATE_BUSY_TX_RX0):
665 hcan->State = HAL_CAN_STATE_BUSY_RX0;
666 break;
667 case(HAL_CAN_STATE_BUSY_TX_RX1):
668 hcan->State = HAL_CAN_STATE_BUSY_RX1;
669 break;
670 case(HAL_CAN_STATE_BUSY_TX_RX0_RX1):
671 hcan->State = HAL_CAN_STATE_BUSY_RX0_RX1;
672 break;
673 default: /* HAL_CAN_STATE_BUSY_TX */
674 hcan->State = HAL_CAN_STATE_READY;
675 break;
678 /* Process unlocked */
679 __HAL_UNLOCK(hcan);
681 /* Return function status */
682 return HAL_OK;
684 else
686 /* Change CAN state */
687 hcan->State = HAL_CAN_STATE_ERROR;
689 /* Return function status */
690 return HAL_ERROR;
695 * @brief Initiates and transmits a CAN frame message.
696 * @param hcan: pointer to a CAN_HandleTypeDef structure that contains
697 * the configuration information for the specified CAN.
698 * @retval HAL status
700 HAL_StatusTypeDef HAL_CAN_Transmit_IT(CAN_HandleTypeDef* hcan)
702 uint32_t transmitmailbox = CAN_TXSTATUS_NOMAILBOX;
704 /* Check the parameters */
705 assert_param(IS_CAN_IDTYPE(hcan->pTxMsg->IDE));
706 assert_param(IS_CAN_RTR(hcan->pTxMsg->RTR));
707 assert_param(IS_CAN_DLC(hcan->pTxMsg->DLC));
709 if(((hcan->Instance->TSR&CAN_TSR_TME0) == CAN_TSR_TME0) || \
710 ((hcan->Instance->TSR&CAN_TSR_TME1) == CAN_TSR_TME1) || \
711 ((hcan->Instance->TSR&CAN_TSR_TME2) == CAN_TSR_TME2))
713 /* Process Locked */
714 __HAL_LOCK(hcan);
716 /* Select one empty transmit mailbox */
717 if((hcan->Instance->TSR&CAN_TSR_TME0) == CAN_TSR_TME0)
719 transmitmailbox = CAN_TXMAILBOX_0;
721 else if((hcan->Instance->TSR&CAN_TSR_TME1) == CAN_TSR_TME1)
723 transmitmailbox = CAN_TXMAILBOX_1;
725 else
727 transmitmailbox = CAN_TXMAILBOX_2;
730 /* Set up the Id */
731 hcan->Instance->sTxMailBox[transmitmailbox].TIR &= CAN_TI0R_TXRQ;
732 if(hcan->pTxMsg->IDE == CAN_ID_STD)
734 assert_param(IS_CAN_STDID(hcan->pTxMsg->StdId));
735 hcan->Instance->sTxMailBox[transmitmailbox].TIR |= ((hcan->pTxMsg->StdId << 21U) | \
736 hcan->pTxMsg->RTR);
738 else
740 assert_param(IS_CAN_EXTID(hcan->pTxMsg->ExtId));
741 hcan->Instance->sTxMailBox[transmitmailbox].TIR |= ((hcan->pTxMsg->ExtId << 3U) | \
742 hcan->pTxMsg->IDE | \
743 hcan->pTxMsg->RTR);
746 /* Set up the DLC */
747 hcan->pTxMsg->DLC &= (uint8_t)0x0000000F;
748 hcan->Instance->sTxMailBox[transmitmailbox].TDTR &= (uint32_t)0xFFFFFFF0U;
749 hcan->Instance->sTxMailBox[transmitmailbox].TDTR |= hcan->pTxMsg->DLC;
751 /* Set up the data field */
752 hcan->Instance->sTxMailBox[transmitmailbox].TDLR = (((uint32_t)hcan->pTxMsg->Data[3U] << 24U) |
753 ((uint32_t)hcan->pTxMsg->Data[2U] << 16U) |
754 ((uint32_t)hcan->pTxMsg->Data[1U] << 8U) |
755 ((uint32_t)hcan->pTxMsg->Data[0U]));
756 hcan->Instance->sTxMailBox[transmitmailbox].TDHR = (((uint32_t)hcan->pTxMsg->Data[7U] << 24U) |
757 ((uint32_t)hcan->pTxMsg->Data[6U] << 16U) |
758 ((uint32_t)hcan->pTxMsg->Data[5U] << 8U) |
759 ((uint32_t)hcan->pTxMsg->Data[4U]));
761 /* Change CAN state */
762 switch(hcan->State)
764 case(HAL_CAN_STATE_BUSY_RX0):
765 hcan->State = HAL_CAN_STATE_BUSY_TX_RX0;
766 break;
767 case(HAL_CAN_STATE_BUSY_RX1):
768 hcan->State = HAL_CAN_STATE_BUSY_TX_RX1;
769 break;
770 case(HAL_CAN_STATE_BUSY_RX0_RX1):
771 hcan->State = HAL_CAN_STATE_BUSY_TX_RX0_RX1;
772 break;
773 default: /* HAL_CAN_STATE_READY */
774 hcan->State = HAL_CAN_STATE_BUSY_TX;
775 break;
778 /* Set CAN error code to none */
779 hcan->ErrorCode = HAL_CAN_ERROR_NONE;
781 /* Process Unlocked */
782 __HAL_UNLOCK(hcan);
784 /* Request transmission */
785 hcan->Instance->sTxMailBox[transmitmailbox].TIR |= CAN_TI0R_TXRQ;
787 /* Enable Error warning, Error passive, Bus-off,
788 Last error and Error Interrupts */
789 __HAL_CAN_ENABLE_IT(hcan, CAN_IT_EWG |
790 CAN_IT_EPV |
791 CAN_IT_BOF |
792 CAN_IT_LEC |
793 CAN_IT_ERR |
794 CAN_IT_TME);
796 else
798 /* Change CAN state */
799 hcan->State = HAL_CAN_STATE_ERROR;
801 /* Return function status */
802 return HAL_ERROR;
805 return HAL_OK;
809 * @brief Receives a correct CAN frame.
810 * @param hcan: pointer to a CAN_HandleTypeDef structure that contains
811 * the configuration information for the specified CAN.
812 * @param FIFONumber: FIFO Number value
813 * @param Timeout: Specify Timeout value
814 * @retval HAL status
816 HAL_StatusTypeDef HAL_CAN_Receive(CAN_HandleTypeDef* hcan, uint8_t FIFONumber, uint32_t Timeout)
818 uint32_t tickstart = 0U;
819 CanRxMsgTypeDef* pRxMsg = NULL;
821 /* Check the parameters */
822 assert_param(IS_CAN_FIFO(FIFONumber));
824 /* Check if CAN state is not busy for RX FIFO0 */
825 if ((FIFONumber == CAN_FIFO0) && ((hcan->State == HAL_CAN_STATE_BUSY_RX0) || \
826 (hcan->State == HAL_CAN_STATE_BUSY_TX_RX0) || \
827 (hcan->State == HAL_CAN_STATE_BUSY_RX0_RX1) || \
828 (hcan->State == HAL_CAN_STATE_BUSY_TX_RX0_RX1)))
830 return HAL_BUSY;
833 /* Check if CAN state is not busy for RX FIFO1 */
834 if ((FIFONumber == CAN_FIFO1) && ((hcan->State == HAL_CAN_STATE_BUSY_RX1) || \
835 (hcan->State == HAL_CAN_STATE_BUSY_TX_RX1) || \
836 (hcan->State == HAL_CAN_STATE_BUSY_RX0_RX1) || \
837 (hcan->State == HAL_CAN_STATE_BUSY_TX_RX0_RX1)))
839 return HAL_BUSY;
842 /* Process locked */
843 __HAL_LOCK(hcan);
845 /* Change CAN state */
846 if (FIFONumber == CAN_FIFO0)
848 switch(hcan->State)
850 case(HAL_CAN_STATE_BUSY_TX):
851 hcan->State = HAL_CAN_STATE_BUSY_TX_RX0;
852 break;
853 case(HAL_CAN_STATE_BUSY_RX1):
854 hcan->State = HAL_CAN_STATE_BUSY_RX0_RX1;
855 break;
856 case(HAL_CAN_STATE_BUSY_TX_RX1):
857 hcan->State = HAL_CAN_STATE_BUSY_TX_RX0_RX1;
858 break;
859 default: /* HAL_CAN_STATE_READY */
860 hcan->State = HAL_CAN_STATE_BUSY_RX0;
861 break;
864 else /* FIFONumber == CAN_FIFO1 */
866 switch(hcan->State)
868 case(HAL_CAN_STATE_BUSY_TX):
869 hcan->State = HAL_CAN_STATE_BUSY_TX_RX1;
870 break;
871 case(HAL_CAN_STATE_BUSY_RX0):
872 hcan->State = HAL_CAN_STATE_BUSY_RX0_RX1;
873 break;
874 case(HAL_CAN_STATE_BUSY_TX_RX0):
875 hcan->State = HAL_CAN_STATE_BUSY_TX_RX0_RX1;
876 break;
877 default: /* HAL_CAN_STATE_READY */
878 hcan->State = HAL_CAN_STATE_BUSY_RX1;
879 break;
883 /* Get tick */
884 tickstart = HAL_GetTick();
886 /* Check pending message */
887 while(__HAL_CAN_MSG_PENDING(hcan, FIFONumber) == 0U)
889 /* Check for the Timeout */
890 if(Timeout != HAL_MAX_DELAY)
892 if((Timeout == 0U)||((HAL_GetTick() - tickstart ) > Timeout))
894 hcan->State = HAL_CAN_STATE_TIMEOUT;
895 /* Process unlocked */
896 __HAL_UNLOCK(hcan);
897 return HAL_TIMEOUT;
902 /* Set RxMsg pointer */
903 if(FIFONumber == CAN_FIFO0)
905 pRxMsg = hcan->pRxMsg;
907 else /* FIFONumber == CAN_FIFO1 */
909 pRxMsg = hcan->pRx1Msg;
912 /* Get the Id */
913 pRxMsg->IDE = (uint8_t)0x04 & hcan->Instance->sFIFOMailBox[FIFONumber].RIR;
914 if (pRxMsg->IDE == CAN_ID_STD)
916 pRxMsg->StdId = 0x000007FFU & (hcan->Instance->sFIFOMailBox[FIFONumber].RIR >> 21U);
918 else
920 pRxMsg->ExtId = 0x1FFFFFFFU & (hcan->Instance->sFIFOMailBox[FIFONumber].RIR >> 3U);
923 pRxMsg->RTR = (uint8_t)0x02 & hcan->Instance->sFIFOMailBox[FIFONumber].RIR;
924 /* Get the DLC */
925 pRxMsg->DLC = (uint8_t)0x0F & hcan->Instance->sFIFOMailBox[FIFONumber].RDTR;
926 /* Get the FMI */
927 pRxMsg->FMI = (uint8_t)0xFF & (hcan->Instance->sFIFOMailBox[FIFONumber].RDTR >> 8U);
928 /* Get the FIFONumber */
929 pRxMsg->FIFONumber = FIFONumber;
930 /* Get the data field */
931 pRxMsg->Data[0] = (uint8_t)0xFF & hcan->Instance->sFIFOMailBox[FIFONumber].RDLR;
932 pRxMsg->Data[1] = (uint8_t)0xFF & (hcan->Instance->sFIFOMailBox[FIFONumber].RDLR >> 8U);
933 pRxMsg->Data[2] = (uint8_t)0xFF & (hcan->Instance->sFIFOMailBox[FIFONumber].RDLR >> 16U);
934 pRxMsg->Data[3] = (uint8_t)0xFF & (hcan->Instance->sFIFOMailBox[FIFONumber].RDLR >> 24U);
935 pRxMsg->Data[4] = (uint8_t)0xFF & hcan->Instance->sFIFOMailBox[FIFONumber].RDHR;
936 pRxMsg->Data[5] = (uint8_t)0xFF & (hcan->Instance->sFIFOMailBox[FIFONumber].RDHR >> 8U);
937 pRxMsg->Data[6] = (uint8_t)0xFF & (hcan->Instance->sFIFOMailBox[FIFONumber].RDHR >> 16U);
938 pRxMsg->Data[7] = (uint8_t)0xFF & (hcan->Instance->sFIFOMailBox[FIFONumber].RDHR >> 24U);
940 /* Release the FIFO */
941 if(FIFONumber == CAN_FIFO0)
943 /* Release FIFO0 */
944 __HAL_CAN_FIFO_RELEASE(hcan, CAN_FIFO0);
946 else /* FIFONumber == CAN_FIFO1 */
948 /* Release FIFO1 */
949 __HAL_CAN_FIFO_RELEASE(hcan, CAN_FIFO1);
952 /* Change CAN state */
953 if (FIFONumber == CAN_FIFO0)
955 switch(hcan->State)
957 case(HAL_CAN_STATE_BUSY_TX_RX0):
958 hcan->State = HAL_CAN_STATE_BUSY_TX;
959 break;
960 case(HAL_CAN_STATE_BUSY_RX0_RX1):
961 hcan->State = HAL_CAN_STATE_BUSY_RX1;
962 break;
963 case(HAL_CAN_STATE_BUSY_TX_RX0_RX1):
964 hcan->State = HAL_CAN_STATE_BUSY_TX_RX1;
965 break;
966 default: /* HAL_CAN_STATE_BUSY_RX0 */
967 hcan->State = HAL_CAN_STATE_READY;
968 break;
971 else /* FIFONumber == CAN_FIFO1 */
973 switch(hcan->State)
975 case(HAL_CAN_STATE_BUSY_TX_RX1):
976 hcan->State = HAL_CAN_STATE_BUSY_TX;
977 break;
978 case(HAL_CAN_STATE_BUSY_RX0_RX1):
979 hcan->State = HAL_CAN_STATE_BUSY_RX0;
980 break;
981 case(HAL_CAN_STATE_BUSY_TX_RX0_RX1):
982 hcan->State = HAL_CAN_STATE_BUSY_TX_RX0;
983 break;
984 default: /* HAL_CAN_STATE_BUSY_RX1 */
985 hcan->State = HAL_CAN_STATE_READY;
986 break;
990 /* Process unlocked */
991 __HAL_UNLOCK(hcan);
993 /* Return function status */
994 return HAL_OK;
998 * @brief Receives a correct CAN frame.
999 * @param hcan: Pointer to a CAN_HandleTypeDef structure that contains
1000 * the configuration information for the specified CAN.
1001 * @param FIFONumber: Specify the FIFO number
1002 * @retval HAL status
1004 HAL_StatusTypeDef HAL_CAN_Receive_IT(CAN_HandleTypeDef* hcan, uint8_t FIFONumber)
1006 /* Check the parameters */
1007 assert_param(IS_CAN_FIFO(FIFONumber));
1009 /* Check if CAN state is not busy for RX FIFO0 */
1010 if((FIFONumber == CAN_FIFO0) && ((hcan->State == HAL_CAN_STATE_BUSY_RX0) || \
1011 (hcan->State == HAL_CAN_STATE_BUSY_TX_RX0) || \
1012 (hcan->State == HAL_CAN_STATE_BUSY_RX0_RX1) || \
1013 (hcan->State == HAL_CAN_STATE_BUSY_TX_RX0_RX1)))
1015 return HAL_BUSY;
1018 /* Check if CAN state is not busy for RX FIFO1 */
1019 if((FIFONumber == CAN_FIFO1) && ((hcan->State == HAL_CAN_STATE_BUSY_RX1) || \
1020 (hcan->State == HAL_CAN_STATE_BUSY_TX_RX1) || \
1021 (hcan->State == HAL_CAN_STATE_BUSY_RX0_RX1) || \
1022 (hcan->State == HAL_CAN_STATE_BUSY_TX_RX0_RX1)))
1024 return HAL_BUSY;
1027 /* Process locked */
1028 __HAL_LOCK(hcan);
1030 /* Change CAN state */
1031 if(FIFONumber == CAN_FIFO0)
1033 switch(hcan->State)
1035 case(HAL_CAN_STATE_BUSY_TX):
1036 hcan->State = HAL_CAN_STATE_BUSY_TX_RX0;
1037 break;
1038 case(HAL_CAN_STATE_BUSY_RX1):
1039 hcan->State = HAL_CAN_STATE_BUSY_RX0_RX1;
1040 break;
1041 case(HAL_CAN_STATE_BUSY_TX_RX1):
1042 hcan->State = HAL_CAN_STATE_BUSY_TX_RX0_RX1;
1043 break;
1044 default: /* HAL_CAN_STATE_READY */
1045 hcan->State = HAL_CAN_STATE_BUSY_RX0;
1046 break;
1049 else /* FIFONumber == CAN_FIFO1 */
1051 switch(hcan->State)
1053 case(HAL_CAN_STATE_BUSY_TX):
1054 hcan->State = HAL_CAN_STATE_BUSY_TX_RX1;
1055 break;
1056 case(HAL_CAN_STATE_BUSY_RX0):
1057 hcan->State = HAL_CAN_STATE_BUSY_RX0_RX1;
1058 break;
1059 case(HAL_CAN_STATE_BUSY_TX_RX0):
1060 hcan->State = HAL_CAN_STATE_BUSY_TX_RX0_RX1;
1061 break;
1062 default: /* HAL_CAN_STATE_READY */
1063 hcan->State = HAL_CAN_STATE_BUSY_RX1;
1064 break;
1067 /* Set CAN error code to none */
1068 hcan->ErrorCode = HAL_CAN_ERROR_NONE;
1070 /* Enable interrupts: */
1071 /* - Enable Error warning Interrupt */
1072 /* - Enable Error passive Interrupt */
1073 /* - Enable Bus-off Interrupt */
1074 /* - Enable Last error code Interrupt */
1075 /* - Enable Error Interrupt */
1076 /* - Enable Transmit mailbox empty Interrupt */
1077 __HAL_CAN_ENABLE_IT(hcan, CAN_IT_EWG |
1078 CAN_IT_EPV |
1079 CAN_IT_BOF |
1080 CAN_IT_LEC |
1081 CAN_IT_ERR |
1082 CAN_IT_TME);
1084 if(FIFONumber == CAN_FIFO0)
1086 /* Enable FIFO 0 overrun and message pending Interrupt */
1087 __HAL_CAN_ENABLE_IT(hcan, CAN_IT_FOV0 | CAN_IT_FMP0);
1089 else
1091 /* Enable FIFO 1 overrun and message pending Interrupt */
1092 __HAL_CAN_ENABLE_IT(hcan, CAN_IT_FOV1 | CAN_IT_FMP1);
1095 /* Return function status */
1096 return HAL_OK;
1100 * @brief Enters the Sleep (low power) mode.
1101 * @param hcan: pointer to a CAN_HandleTypeDef structure that contains
1102 * the configuration information for the specified CAN.
1103 * @retval HAL status.
1105 HAL_StatusTypeDef HAL_CAN_Sleep(CAN_HandleTypeDef* hcan)
1107 uint32_t tickstart = 0;
1109 /* Process locked */
1110 __HAL_LOCK(hcan);
1112 /* Change CAN state */
1113 hcan->State = HAL_CAN_STATE_BUSY;
1115 /* Request Sleep mode */
1116 hcan->Instance->MCR = (((hcan->Instance->MCR) & (uint32_t)(~(uint32_t)CAN_MCR_INRQ)) | CAN_MCR_SLEEP);
1118 /* Sleep mode status */
1119 if ((hcan->Instance->MSR & (CAN_MSR_SLAK|CAN_MSR_INAK)) != CAN_MSR_SLAK)
1121 /* Process unlocked */
1122 __HAL_UNLOCK(hcan);
1124 /* Return function status */
1125 return HAL_ERROR;
1128 /* Get tick */
1129 tickstart = HAL_GetTick();
1131 /* Wait the acknowledge */
1132 while((hcan->Instance->MSR & (CAN_MSR_SLAK|CAN_MSR_INAK)) != CAN_MSR_SLAK)
1134 if((HAL_GetTick() - tickstart) > CAN_TIMEOUT_VALUE)
1136 hcan->State = HAL_CAN_STATE_TIMEOUT;
1137 /* Process unlocked */
1138 __HAL_UNLOCK(hcan);
1139 return HAL_TIMEOUT;
1143 /* Change CAN state */
1144 hcan->State = HAL_CAN_STATE_READY;
1146 /* Process unlocked */
1147 __HAL_UNLOCK(hcan);
1149 /* Return function status */
1150 return HAL_OK;
1154 * @brief Wakes up the CAN peripheral from sleep mode, after that the CAN peripheral
1155 * is in the normal mode.
1156 * @param hcan: pointer to a CAN_HandleTypeDef structure that contains
1157 * the configuration information for the specified CAN.
1158 * @retval HAL status.
1160 HAL_StatusTypeDef HAL_CAN_WakeUp(CAN_HandleTypeDef* hcan)
1162 uint32_t tickstart = 0;
1164 /* Process locked */
1165 __HAL_LOCK(hcan);
1167 /* Change CAN state */
1168 hcan->State = HAL_CAN_STATE_BUSY;
1170 /* Wake up request */
1171 hcan->Instance->MCR &= ~(uint32_t)CAN_MCR_SLEEP;
1173 /* Get tick */
1174 tickstart = HAL_GetTick();
1176 /* Sleep mode status */
1177 while((hcan->Instance->MSR & CAN_MSR_SLAK) == CAN_MSR_SLAK)
1179 if((HAL_GetTick() - tickstart) > CAN_TIMEOUT_VALUE)
1181 hcan->State= HAL_CAN_STATE_TIMEOUT;
1182 /* Process unlocked */
1183 __HAL_UNLOCK(hcan);
1184 return HAL_TIMEOUT;
1187 if((hcan->Instance->MSR & CAN_MSR_SLAK) == CAN_MSR_SLAK)
1189 /* Process unlocked */
1190 __HAL_UNLOCK(hcan);
1192 /* Return function status */
1193 return HAL_ERROR;
1196 /* Change CAN state */
1197 hcan->State = HAL_CAN_STATE_READY;
1199 /* Process unlocked */
1200 __HAL_UNLOCK(hcan);
1202 /* Return function status */
1203 return HAL_OK;
1207 * @brief Handles CAN interrupt request
1208 * @param hcan: pointer to a CAN_HandleTypeDef structure that contains
1209 * the configuration information for the specified CAN.
1210 * @retval None
1212 void HAL_CAN_IRQHandler(CAN_HandleTypeDef* hcan)
1214 uint32_t tmp1 = 0U, tmp2 = 0U, tmp3 = 0U;
1215 uint32_t errorcode = HAL_CAN_ERROR_NONE;
1217 /* Check Overrun flag for FIFO0 */
1218 tmp1 = __HAL_CAN_GET_FLAG(hcan, CAN_FLAG_FOV0);
1219 tmp2 = __HAL_CAN_GET_IT_SOURCE(hcan, CAN_IT_FOV0);
1220 if(tmp1 && tmp2)
1222 /* Set CAN error code to FOV0 error */
1223 errorcode |= HAL_CAN_ERROR_FOV0;
1225 /* Clear FIFO0 Overrun Flag */
1226 __HAL_CAN_CLEAR_FLAG(hcan, CAN_FLAG_FOV0);
1228 /* Check Overrun flag for FIFO1 */
1229 tmp1 = __HAL_CAN_GET_FLAG(hcan, CAN_FLAG_FOV1);
1230 tmp2 = __HAL_CAN_GET_IT_SOURCE(hcan, CAN_IT_FOV1);
1232 if(tmp1 && tmp2)
1234 /* Set CAN error code to FOV1 error */
1235 errorcode |= HAL_CAN_ERROR_FOV1;
1237 /* Clear FIFO1 Overrun Flag */
1238 __HAL_CAN_CLEAR_FLAG(hcan, CAN_FLAG_FOV1);
1241 /* Check End of transmission flag */
1242 if(__HAL_CAN_GET_IT_SOURCE(hcan, CAN_IT_TME))
1244 tmp1 = __HAL_CAN_TRANSMIT_STATUS(hcan, CAN_TXMAILBOX_0);
1245 tmp2 = __HAL_CAN_TRANSMIT_STATUS(hcan, CAN_TXMAILBOX_1);
1246 tmp3 = __HAL_CAN_TRANSMIT_STATUS(hcan, CAN_TXMAILBOX_2);
1247 if(tmp1 || tmp2 || tmp3)
1249 tmp1 = __HAL_CAN_GET_FLAG(hcan, CAN_FLAG_TXOK0);
1250 tmp2 = __HAL_CAN_GET_FLAG(hcan, CAN_FLAG_TXOK1);
1251 tmp3 = __HAL_CAN_GET_FLAG(hcan, CAN_FLAG_TXOK2);
1252 /* Check Transmit success */
1253 if(tmp1 || tmp2 || tmp3)
1255 /* Call transmit function */
1256 CAN_Transmit_IT(hcan);
1258 else /* Transmit failure */
1260 /* Set CAN error code to TXFAIL error */
1261 errorcode |= HAL_CAN_ERROR_TXFAIL;
1264 /* Clear transmission status flags (RQCPx and TXOKx) */
1265 SET_BIT(hcan->Instance->TSR, CAN_TSR_RQCP0 | CAN_TSR_RQCP1 | CAN_TSR_RQCP2 | \
1266 CAN_FLAG_TXOK0 | CAN_FLAG_TXOK1 | CAN_FLAG_TXOK2);
1270 tmp1 = __HAL_CAN_MSG_PENDING(hcan, CAN_FIFO0);
1271 tmp2 = __HAL_CAN_GET_IT_SOURCE(hcan, CAN_IT_FMP0);
1272 /* Check End of reception flag for FIFO0 */
1273 if((tmp1 != 0U) && tmp2)
1275 /* Call receive function */
1276 CAN_Receive_IT(hcan, CAN_FIFO0);
1279 tmp1 = __HAL_CAN_MSG_PENDING(hcan, CAN_FIFO1);
1280 tmp2 = __HAL_CAN_GET_IT_SOURCE(hcan, CAN_IT_FMP1);
1281 /* Check End of reception flag for FIFO1 */
1282 if((tmp1 != 0U) && tmp2)
1284 /* Call receive function */
1285 CAN_Receive_IT(hcan, CAN_FIFO1);
1288 /* Set error code in handle */
1289 hcan->ErrorCode |= errorcode;
1291 tmp1 = __HAL_CAN_GET_FLAG(hcan, CAN_FLAG_EWG);
1292 tmp2 = __HAL_CAN_GET_IT_SOURCE(hcan, CAN_IT_EWG);
1293 tmp3 = __HAL_CAN_GET_IT_SOURCE(hcan, CAN_IT_ERR);
1294 /* Check Error Warning Flag */
1295 if(tmp1 && tmp2 && tmp3)
1297 /* Set CAN error code to EWG error */
1298 hcan->ErrorCode |= HAL_CAN_ERROR_EWG;
1301 tmp1 = __HAL_CAN_GET_FLAG(hcan, CAN_FLAG_EPV);
1302 tmp2 = __HAL_CAN_GET_IT_SOURCE(hcan, CAN_IT_EPV);
1303 tmp3 = __HAL_CAN_GET_IT_SOURCE(hcan, CAN_IT_ERR);
1304 /* Check Error Passive Flag */
1305 if(tmp1 && tmp2 && tmp3)
1307 /* Set CAN error code to EPV error */
1308 hcan->ErrorCode |= HAL_CAN_ERROR_EPV;
1311 tmp1 = __HAL_CAN_GET_FLAG(hcan, CAN_FLAG_BOF);
1312 tmp2 = __HAL_CAN_GET_IT_SOURCE(hcan, CAN_IT_BOF);
1313 tmp3 = __HAL_CAN_GET_IT_SOURCE(hcan, CAN_IT_ERR);
1314 /* Check Bus-Off Flag */
1315 if(tmp1 && tmp2 && tmp3)
1317 /* Set CAN error code to BOF error */
1318 hcan->ErrorCode |= HAL_CAN_ERROR_BOF;
1321 tmp1 = HAL_IS_BIT_CLR(hcan->Instance->ESR, CAN_ESR_LEC);
1322 tmp2 = __HAL_CAN_GET_IT_SOURCE(hcan, CAN_IT_LEC);
1323 tmp3 = __HAL_CAN_GET_IT_SOURCE(hcan, CAN_IT_ERR);
1324 /* Check Last error code Flag */
1325 if((!tmp1) && tmp2 && tmp3)
1327 tmp1 = (hcan->Instance->ESR) & CAN_ESR_LEC;
1328 switch(tmp1)
1330 case(CAN_ESR_LEC_0):
1331 /* Set CAN error code to STF error */
1332 hcan->ErrorCode |= HAL_CAN_ERROR_STF;
1333 break;
1334 case(CAN_ESR_LEC_1):
1335 /* Set CAN error code to FOR error */
1336 hcan->ErrorCode |= HAL_CAN_ERROR_FOR;
1337 break;
1338 case(CAN_ESR_LEC_1 | CAN_ESR_LEC_0):
1339 /* Set CAN error code to ACK error */
1340 hcan->ErrorCode |= HAL_CAN_ERROR_ACK;
1341 break;
1342 case(CAN_ESR_LEC_2):
1343 /* Set CAN error code to BR error */
1344 hcan->ErrorCode |= HAL_CAN_ERROR_BR;
1345 break;
1346 case(CAN_ESR_LEC_2 | CAN_ESR_LEC_0):
1347 /* Set CAN error code to BD error */
1348 hcan->ErrorCode |= HAL_CAN_ERROR_BD;
1349 break;
1350 case(CAN_ESR_LEC_2 | CAN_ESR_LEC_1):
1351 /* Set CAN error code to CRC error */
1352 hcan->ErrorCode |= HAL_CAN_ERROR_CRC;
1353 break;
1354 default:
1355 break;
1358 /* Clear Last error code Flag */
1359 hcan->Instance->ESR &= ~(CAN_ESR_LEC);
1362 /* Call the Error call Back in case of Errors */
1363 if(hcan->ErrorCode != HAL_CAN_ERROR_NONE)
1365 /* Clear ERRI Flag */
1366 hcan->Instance->MSR = CAN_MSR_ERRI;
1367 /* Set the CAN state ready to be able to start again the process */
1368 hcan->State = HAL_CAN_STATE_READY;
1370 /* Disable interrupts: */
1371 /* - Disable Error warning Interrupt */
1372 /* - Disable Error passive Interrupt */
1373 /* - Disable Bus-off Interrupt */
1374 /* - Disable Last error code Interrupt */
1375 /* - Disable Error Interrupt */
1376 /* - Disable FIFO 0 message pending Interrupt */
1377 /* - Disable FIFO 0 Overrun Interrupt */
1378 /* - Disable FIFO 1 message pending Interrupt */
1379 /* - Disable FIFO 1 Overrun Interrupt */
1380 /* - Disable Transmit mailbox empty Interrupt */
1381 __HAL_CAN_DISABLE_IT(hcan, CAN_IT_EWG |
1382 CAN_IT_EPV |
1383 CAN_IT_BOF |
1384 CAN_IT_LEC |
1385 CAN_IT_ERR |
1386 CAN_IT_FMP0|
1387 CAN_IT_FOV0|
1388 CAN_IT_FMP1|
1389 CAN_IT_FOV1|
1390 CAN_IT_TME);
1392 /* Call Error callback function */
1393 HAL_CAN_ErrorCallback(hcan);
1398 * @brief Transmission complete callback in non blocking mode
1399 * @param hcan: pointer to a CAN_HandleTypeDef structure that contains
1400 * the configuration information for the specified CAN.
1401 * @retval None
1403 __weak void HAL_CAN_TxCpltCallback(CAN_HandleTypeDef* hcan)
1405 /* Prevent unused argument(s) compilation warning */
1406 UNUSED(hcan);
1407 /* NOTE : This function Should not be modified, when the callback is needed,
1408 the HAL_CAN_TxCpltCallback could be implemented in the user file
1413 * @brief Transmission complete callback in non blocking mode
1414 * @param hcan: pointer to a CAN_HandleTypeDef structure that contains
1415 * the configuration information for the specified CAN.
1416 * @retval None
1418 __weak void HAL_CAN_RxCpltCallback(CAN_HandleTypeDef* hcan)
1420 /* Prevent unused argument(s) compilation warning */
1421 UNUSED(hcan);
1422 /* NOTE : This function Should not be modified, when the callback is needed,
1423 the HAL_CAN_RxCpltCallback could be implemented in the user file
1428 * @brief Error CAN callback.
1429 * @param hcan: pointer to a CAN_HandleTypeDef structure that contains
1430 * the configuration information for the specified CAN.
1431 * @retval None
1433 __weak void HAL_CAN_ErrorCallback(CAN_HandleTypeDef *hcan)
1435 /* Prevent unused argument(s) compilation warning */
1436 UNUSED(hcan);
1437 /* NOTE : This function Should not be modified, when the callback is needed,
1438 the HAL_CAN_ErrorCallback could be implemented in the user file
1443 * @}
1446 /** @defgroup CAN_Exported_Functions_Group3 Peripheral State and Error functions
1447 * @brief CAN Peripheral State functions
1449 @verbatim
1450 ==============================================================================
1451 ##### Peripheral State and Error functions #####
1452 ==============================================================================
1453 [..]
1454 This subsection provides functions allowing to :
1455 (+) Check the CAN state.
1456 (+) Check CAN Errors detected during interrupt process
1458 @endverbatim
1459 * @{
1463 * @brief return the CAN state
1464 * @param hcan: pointer to a CAN_HandleTypeDef structure that contains
1465 * the configuration information for the specified CAN.
1466 * @retval HAL state
1468 HAL_CAN_StateTypeDef HAL_CAN_GetState(CAN_HandleTypeDef* hcan)
1470 /* Return CAN state */
1471 return hcan->State;
1475 * @brief Return the CAN error code
1476 * @param hcan: pointer to a CAN_HandleTypeDef structure that contains
1477 * the configuration information for the specified CAN.
1478 * @retval CAN Error Code
1480 uint32_t HAL_CAN_GetError(CAN_HandleTypeDef *hcan)
1482 return hcan->ErrorCode;
1486 * @}
1490 * @brief Initiates and transmits a CAN frame message.
1491 * @param hcan: pointer to a CAN_HandleTypeDef structure that contains
1492 * the configuration information for the specified CAN.
1493 * @retval HAL status
1495 static HAL_StatusTypeDef CAN_Transmit_IT(CAN_HandleTypeDef* hcan)
1497 /* Disable Transmit mailbox empty Interrupt */
1498 __HAL_CAN_DISABLE_IT(hcan, CAN_IT_TME);
1500 if(hcan->State == HAL_CAN_STATE_BUSY_TX)
1502 /* Disable Error warning, Error passive, Bus-off, Last error code
1503 and Error Interrupts */
1504 __HAL_CAN_DISABLE_IT(hcan, CAN_IT_EWG |
1505 CAN_IT_EPV |
1506 CAN_IT_BOF |
1507 CAN_IT_LEC |
1508 CAN_IT_ERR );
1511 /* Change CAN state */
1512 switch(hcan->State)
1514 case(HAL_CAN_STATE_BUSY_TX_RX0):
1515 hcan->State = HAL_CAN_STATE_BUSY_RX0;
1516 break;
1517 case(HAL_CAN_STATE_BUSY_TX_RX1):
1518 hcan->State = HAL_CAN_STATE_BUSY_RX1;
1519 break;
1520 case(HAL_CAN_STATE_BUSY_TX_RX0_RX1):
1521 hcan->State = HAL_CAN_STATE_BUSY_RX0_RX1;
1522 break;
1523 default: /* HAL_CAN_STATE_BUSY_TX */
1524 hcan->State = HAL_CAN_STATE_READY;
1525 break;
1528 /* Transmission complete callback */
1529 HAL_CAN_TxCpltCallback(hcan);
1531 return HAL_OK;
1535 * @brief Receives a correct CAN frame.
1536 * @param hcan: Pointer to a CAN_HandleTypeDef structure that contains
1537 * the configuration information for the specified CAN.
1538 * @param FIFONumber: Specify the FIFO number
1539 * @retval HAL status
1540 * @retval None
1542 static HAL_StatusTypeDef CAN_Receive_IT(CAN_HandleTypeDef* hcan, uint8_t FIFONumber)
1544 uint32_t tmp1 = 0U;
1545 CanRxMsgTypeDef* pRxMsg = NULL;
1547 /* Set RxMsg pointer */
1548 if(FIFONumber == CAN_FIFO0)
1550 pRxMsg = hcan->pRxMsg;
1552 else /* FIFONumber == CAN_FIFO1 */
1554 pRxMsg = hcan->pRx1Msg;
1557 /* Get the Id */
1558 pRxMsg->IDE = (uint8_t)0x04 & hcan->Instance->sFIFOMailBox[FIFONumber].RIR;
1559 if (pRxMsg->IDE == CAN_ID_STD)
1561 pRxMsg->StdId = 0x000007FFU & (hcan->Instance->sFIFOMailBox[FIFONumber].RIR >> 21U);
1563 else
1565 pRxMsg->ExtId = 0x1FFFFFFFU & (hcan->Instance->sFIFOMailBox[FIFONumber].RIR >> 3U);
1568 pRxMsg->RTR = (uint8_t)0x02 & hcan->Instance->sFIFOMailBox[FIFONumber].RIR;
1569 /* Get the DLC */
1570 pRxMsg->DLC = (uint8_t)0x0F & hcan->Instance->sFIFOMailBox[FIFONumber].RDTR;
1571 /* Get the FIFONumber */
1572 pRxMsg->FIFONumber = FIFONumber;
1573 /* Get the FMI */
1574 pRxMsg->FMI = (uint8_t)0xFF & (hcan->Instance->sFIFOMailBox[FIFONumber].RDTR >> 8U);
1575 /* Get the data field */
1576 pRxMsg->Data[0] = (uint8_t)0xFF & hcan->Instance->sFIFOMailBox[FIFONumber].RDLR;
1577 pRxMsg->Data[1] = (uint8_t)0xFF & (hcan->Instance->sFIFOMailBox[FIFONumber].RDLR >> 8U);
1578 pRxMsg->Data[2] = (uint8_t)0xFF & (hcan->Instance->sFIFOMailBox[FIFONumber].RDLR >> 16U);
1579 pRxMsg->Data[3] = (uint8_t)0xFF & (hcan->Instance->sFIFOMailBox[FIFONumber].RDLR >> 24U);
1580 pRxMsg->Data[4] = (uint8_t)0xFF & hcan->Instance->sFIFOMailBox[FIFONumber].RDHR;
1581 pRxMsg->Data[5] = (uint8_t)0xFF & (hcan->Instance->sFIFOMailBox[FIFONumber].RDHR >> 8U);
1582 pRxMsg->Data[6] = (uint8_t)0xFF & (hcan->Instance->sFIFOMailBox[FIFONumber].RDHR >> 16U);
1583 pRxMsg->Data[7] = (uint8_t)0xFF & (hcan->Instance->sFIFOMailBox[FIFONumber].RDHR >> 24U);
1584 /* Release the FIFO */
1585 /* Release FIFO0 */
1586 if (FIFONumber == CAN_FIFO0)
1588 __HAL_CAN_FIFO_RELEASE(hcan, CAN_FIFO0);
1590 /* Disable FIFO 0 overrun and message pending Interrupt */
1591 __HAL_CAN_DISABLE_IT(hcan, CAN_IT_FOV0 | CAN_IT_FMP0);
1593 /* Release FIFO1 */
1594 else /* FIFONumber == CAN_FIFO1 */
1596 __HAL_CAN_FIFO_RELEASE(hcan, CAN_FIFO1);
1598 /* Disable FIFO 1 overrun and message pending Interrupt */
1599 __HAL_CAN_DISABLE_IT(hcan, CAN_IT_FOV1 | CAN_IT_FMP1);
1602 tmp1 = hcan->State;
1603 if((tmp1 == HAL_CAN_STATE_BUSY_RX0) || (tmp1 == HAL_CAN_STATE_BUSY_RX1))
1605 /* Disable Error warning, Error passive, Bus-off, Last error code
1606 and Error Interrupts */
1607 __HAL_CAN_DISABLE_IT(hcan, CAN_IT_EWG |
1608 CAN_IT_EPV |
1609 CAN_IT_BOF |
1610 CAN_IT_LEC |
1611 CAN_IT_ERR);
1614 /* Change CAN state */
1615 if (FIFONumber == CAN_FIFO0)
1617 switch(hcan->State)
1619 case(HAL_CAN_STATE_BUSY_TX_RX0):
1620 hcan->State = HAL_CAN_STATE_BUSY_TX;
1621 break;
1622 case(HAL_CAN_STATE_BUSY_RX0_RX1):
1623 hcan->State = HAL_CAN_STATE_BUSY_RX1;
1624 break;
1625 case(HAL_CAN_STATE_BUSY_TX_RX0_RX1):
1626 hcan->State = HAL_CAN_STATE_BUSY_TX_RX1;
1627 break;
1628 default: /* HAL_CAN_STATE_BUSY_RX0 */
1629 hcan->State = HAL_CAN_STATE_READY;
1630 break;
1633 else /* FIFONumber == CAN_FIFO1 */
1635 switch(hcan->State)
1637 case(HAL_CAN_STATE_BUSY_TX_RX1):
1638 hcan->State = HAL_CAN_STATE_BUSY_TX;
1639 break;
1640 case(HAL_CAN_STATE_BUSY_RX0_RX1):
1641 hcan->State = HAL_CAN_STATE_BUSY_RX0;
1642 break;
1643 case(HAL_CAN_STATE_BUSY_TX_RX0_RX1):
1644 hcan->State = HAL_CAN_STATE_BUSY_TX_RX0;
1645 break;
1646 default: /* HAL_CAN_STATE_BUSY_RX1 */
1647 hcan->State = HAL_CAN_STATE_READY;
1648 break;
1652 /* Receive complete callback */
1653 HAL_CAN_RxCpltCallback(hcan);
1655 /* Return function status */
1656 return HAL_OK;
1660 * @}
1663 #endif /* HAL_CAN_MODULE_ENABLED */
1665 * @}
1669 * @}
1672 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/