STM32F439xx HAL User Manual
stm32f4xx_hal_can.c
Go to the documentation of this file.
00001 /**
00002   ******************************************************************************
00003   * @file    stm32f4xx_hal_can.c
00004   * @author  MCD Application Team
00005   * @brief   CAN HAL module driver.
00006   *          This file provides firmware functions to manage the following
00007   *          functionalities of the Controller Area Network (CAN) peripheral:
00008   *           + Initialization and de-initialization functions
00009   *           + Configuration functions
00010   *           + Control functions
00011   *           + Interrupts management
00012   *           + Callbacks functions
00013   *           + Peripheral State and Error functions
00014   *
00015   @verbatim
00016   ==============================================================================
00017                         ##### How to use this driver #####
00018   ==============================================================================
00019     [..]
00020       (#) Initialize the CAN low level resources by implementing the
00021           HAL_CAN_MspInit():
00022          (++) Enable the CAN interface clock using __HAL_RCC_CANx_CLK_ENABLE()
00023          (++) Configure CAN pins
00024              (+++) Enable the clock for the CAN GPIOs
00025              (+++) Configure CAN pins as alternate function open-drain
00026          (++) In case of using interrupts (e.g. HAL_CAN_ActivateNotification())
00027              (+++) Configure the CAN interrupt priority using
00028                    HAL_NVIC_SetPriority()
00029              (+++) Enable the CAN IRQ handler using HAL_NVIC_EnableIRQ()
00030              (+++) In CAN IRQ handler, call HAL_CAN_IRQHandler()
00031 
00032       (#) Initialize the CAN peripheral using HAL_CAN_Init() function. This
00033           function resorts to HAL_CAN_MspInit() for low-level initialization.
00034 
00035       (#) Configure the reception filters using the following configuration
00036           functions:
00037             (++) HAL_CAN_ConfigFilter()
00038 
00039       (#) Start the CAN module using HAL_CAN_Start() function. At this level
00040           the node is active on the bus: it receive messages, and can send
00041           messages.
00042 
00043       (#) To manage messages transmission, the following Tx control functions
00044           can be used:
00045             (++) HAL_CAN_AddTxMessage() to request transmission of a new
00046                  message.
00047             (++) HAL_CAN_AbortTxRequest() to abort transmission of a pending
00048                  message.
00049             (++) HAL_CAN_GetTxMailboxesFreeLevel() to get the number of free Tx
00050                  mailboxes.
00051             (++) HAL_CAN_IsTxMessagePending() to check if a message is pending
00052                  in a Tx mailbox.
00053             (++) HAL_CAN_GetTxTimestamp() to get the timestamp of Tx message
00054                  sent, if time triggered communication mode is enabled.
00055 
00056       (#) When a message is received into the CAN Rx FIFOs, it can be retrieved
00057           using the HAL_CAN_GetRxMessage() function. The function
00058           HAL_CAN_GetRxFifoFillLevel() allows to know how many Rx message are
00059           stored in the Rx Fifo.
00060 
00061       (#) Calling the HAL_CAN_Stop() function stops the CAN module.
00062 
00063       (#) The deinitialization is achieved with HAL_CAN_DeInit() function.
00064 
00065 
00066       *** Polling mode operation ***
00067       ==============================
00068     [..]
00069       (#) Reception:
00070             (++) Monitor reception of message using HAL_CAN_GetRxFifoFillLevel()
00071                  until at least one message is received.
00072             (++) Then get the message using HAL_CAN_GetRxMessage().
00073 
00074       (#) Transmission:
00075             (++) Monitor the Tx mailboxes availability until at least one Tx
00076                  mailbox is free, using HAL_CAN_GetTxMailboxesFreeLevel().
00077             (++) Then request transmission of a message using
00078                  HAL_CAN_AddTxMessage().
00079 
00080 
00081       *** Interrupt mode operation ***
00082       ================================
00083     [..]
00084       (#) Notifications are activated using HAL_CAN_ActivateNotification()
00085           function. Then, the process can be controlled through the
00086           available user callbacks: HAL_CAN_xxxCallback(), using same APIs
00087           HAL_CAN_GetRxMessage() and HAL_CAN_AddTxMessage().
00088 
00089       (#) Notifications can be deactivated using
00090           HAL_CAN_DeactivateNotification() function.
00091 
00092       (#) Special care should be taken for CAN_IT_RX_FIFO0_MSG_PENDING and
00093           CAN_IT_RX_FIFO1_MSG_PENDING notifications. These notifications trig
00094           the callbacks HAL_CAN_RxFIFO0MsgPendingCallback() and
00095           HAL_CAN_RxFIFO1MsgPendingCallback(). User has two possible options
00096           here.
00097             (++) Directly get the Rx message in the callback, using
00098                  HAL_CAN_GetRxMessage().
00099             (++) Or deactivate the notification in the callback without
00100                  getting the Rx message. The Rx message can then be got later
00101                  using HAL_CAN_GetRxMessage(). Once the Rx message have been
00102                  read, the notification can be activated again.
00103 
00104 
00105       *** Sleep mode ***
00106       ==================
00107     [..]
00108       (#) The CAN peripheral can be put in sleep mode (low power), using
00109           HAL_CAN_RequestSleep(). The sleep mode will be entered as soon as the
00110           current CAN activity (transmission or reception of a CAN frame) will
00111           be completed.
00112 
00113       (#) A notification can be activated to be informed when the sleep mode
00114           will be entered.
00115 
00116       (#) It can be checked if the sleep mode is entered using
00117           HAL_CAN_IsSleepActive().
00118           Note that the CAN state (accessible from the API HAL_CAN_GetState())
00119           is HAL_CAN_STATE_SLEEP_PENDING as soon as the sleep mode request is
00120           submitted (the sleep mode is not yet entered), and become
00121           HAL_CAN_STATE_SLEEP_ACTIVE when the sleep mode is effective.
00122 
00123       (#) The wake-up from sleep mode can be trigged by two ways:
00124             (++) Using HAL_CAN_WakeUp(). When returning from this function,
00125                  the sleep mode is exited (if return status is HAL_OK).
00126             (++) When a start of Rx CAN frame is detected by the CAN peripheral,
00127                  if automatic wake up mode is enabled.
00128 
00129   @endverbatim
00130   ******************************************************************************
00131   * @attention
00132   *
00133   * <h2><center>&copy; COPYRIGHT(c) 2016 STMicroelectronics</center></h2>
00134   *
00135   * Redistribution and use in source and binary forms, with or without modification,
00136   * are permitted provided that the following conditions are met:
00137   *   1. Redistributions of source code must retain the above copyright notice,
00138   *      this list of conditions and the following disclaimer.
00139   *   2. Redistributions in binary form must reproduce the above copyright notice,
00140   *      this list of conditions and the following disclaimer in the documentation
00141   *      and/or other materials provided with the distribution.
00142   *   3. Neither the name of STMicroelectronics nor the names of its contributors
00143   *      may be used to endorse or promote products derived from this software
00144   *      without specific prior written permission.
00145   *
00146   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
00147   * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00148   * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
00149   * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
00150   * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
00151   * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
00152   * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
00153   * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
00154   * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
00155   * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00156   *
00157   ******************************************************************************
00158   */
00159 
00160 /* Includes ------------------------------------------------------------------*/
00161 #include "stm32f4xx_hal.h"
00162 
00163 /** @addtogroup STM32F4xx_HAL_Driver
00164   * @{
00165   */
00166 
00167 #if defined(CAN1)
00168 
00169 /** @defgroup CAN CAN
00170   * @brief CAN driver modules
00171   * @{
00172   */
00173 
00174 #ifdef HAL_CAN_MODULE_ENABLED
00175 
00176 #ifdef HAL_CAN_LEGACY_MODULE_ENABLED
00177   #error "The CAN driver cannot be used with its legacy, Please enable only one CAN module at once"
00178 #endif
00179 
00180 /* Private typedef -----------------------------------------------------------*/
00181 /* Private define ------------------------------------------------------------*/
00182 /** @defgroup CAN_Private_Constants CAN Private Constants
00183   * @{
00184   */
00185 #define CAN_TIMEOUT_VALUE 10U
00186 /**
00187   * @}
00188   */
00189 /* Private macro -------------------------------------------------------------*/
00190 /* Private variables ---------------------------------------------------------*/
00191 /* Private function prototypes -----------------------------------------------*/
00192 /* Exported functions --------------------------------------------------------*/
00193 
00194 /** @defgroup CAN_Exported_Functions CAN Exported Functions
00195   * @{
00196   */
00197 
00198 /** @defgroup CAN_Exported_Functions_Group1 Initialization and de-initialization functions
00199  *  @brief    Initialization and Configuration functions
00200  *
00201 @verbatim
00202   ==============================================================================
00203               ##### Initialization and de-initialization functions #####
00204   ==============================================================================
00205     [..]  This section provides functions allowing to:
00206       (+) HAL_CAN_Init                       : Initialize and configure the CAN.
00207       (+) HAL_CAN_DeInit                     : De-initialize the CAN.
00208       (+) HAL_CAN_MspInit                    : Initialize the CAN MSP.
00209       (+) HAL_CAN_MspDeInit                  : DeInitialize the CAN MSP.
00210 
00211 @endverbatim
00212   * @{
00213   */
00214 
00215 /**
00216   * @brief  Initializes the CAN peripheral according to the specified
00217   *         parameters in the CAN_InitStruct.
00218   * @param  hcan pointer to a CAN_HandleTypeDef structure that contains
00219   *         the configuration information for the specified CAN.
00220   * @retval HAL status
00221   */
00222 HAL_StatusTypeDef HAL_CAN_Init(CAN_HandleTypeDef *hcan)
00223 {
00224   uint32_t tickstart = 0U;
00225 
00226   /* Check CAN handle */
00227   if (hcan == NULL)
00228   {
00229     return HAL_ERROR;
00230   }
00231 
00232   /* Check the parameters */
00233   assert_param(IS_CAN_ALL_INSTANCE(hcan->Instance));
00234   assert_param(IS_FUNCTIONAL_STATE(hcan->Init.TimeTriggeredMode));
00235   assert_param(IS_FUNCTIONAL_STATE(hcan->Init.AutoBusOff));
00236   assert_param(IS_FUNCTIONAL_STATE(hcan->Init.AutoWakeUp));
00237   assert_param(IS_FUNCTIONAL_STATE(hcan->Init.AutoRetransmission));
00238   assert_param(IS_FUNCTIONAL_STATE(hcan->Init.ReceiveFifoLocked));
00239   assert_param(IS_FUNCTIONAL_STATE(hcan->Init.TransmitFifoPriority));
00240   assert_param(IS_CAN_MODE(hcan->Init.Mode));
00241   assert_param(IS_CAN_SJW(hcan->Init.SyncJumpWidth));
00242   assert_param(IS_CAN_BS1(hcan->Init.TimeSeg1));
00243   assert_param(IS_CAN_BS2(hcan->Init.TimeSeg2));
00244   assert_param(IS_CAN_PRESCALER(hcan->Init.Prescaler));
00245 
00246   if (hcan->State == HAL_CAN_STATE_RESET)
00247   {
00248     /* Init the low level hardware: CLOCK, NVIC */
00249     HAL_CAN_MspInit(hcan);
00250   }
00251 
00252   /* Exit from sleep mode */
00253   CLEAR_BIT(hcan->Instance->MCR, CAN_MCR_SLEEP);
00254 
00255   /* Get tick */
00256   tickstart = HAL_GetTick();
00257 
00258   /* Check Sleep mode leave acknowledge */
00259   while ((hcan->Instance->MSR & CAN_MSR_SLAK) != RESET)
00260   {
00261     if ((HAL_GetTick() - tickstart) > CAN_TIMEOUT_VALUE)
00262     {
00263       /* Update error code */
00264       hcan->ErrorCode |= HAL_CAN_ERROR_TIMEOUT;
00265 
00266       /* Change CAN state */
00267       hcan->State = HAL_CAN_STATE_ERROR;
00268 
00269       return HAL_ERROR;
00270     }
00271   }
00272 
00273   /* Request initialisation */
00274   SET_BIT(hcan->Instance->MCR, CAN_MCR_INRQ);
00275 
00276   /* Get tick */
00277   tickstart = HAL_GetTick();
00278 
00279   /* Wait initialisation acknowledge */
00280   while ((hcan->Instance->MSR & CAN_MSR_INAK) == RESET)
00281   {
00282     if ((HAL_GetTick() - tickstart) > CAN_TIMEOUT_VALUE)
00283     {
00284       /* Update error code */
00285       hcan->ErrorCode |= HAL_CAN_ERROR_TIMEOUT;
00286 
00287       /* Change CAN state */
00288       hcan->State = HAL_CAN_STATE_ERROR;
00289 
00290       return HAL_ERROR;
00291     }
00292   }
00293 
00294   /* Set the time triggered communication mode */
00295   if (hcan->Init.TimeTriggeredMode == ENABLE)
00296   {
00297     SET_BIT(hcan->Instance->MCR, CAN_MCR_TTCM);
00298   }
00299   else
00300   {
00301     CLEAR_BIT(hcan->Instance->MCR, CAN_MCR_TTCM);
00302   }
00303 
00304   /* Set the automatic bus-off management */
00305   if (hcan->Init.AutoBusOff == ENABLE)
00306   {
00307     SET_BIT(hcan->Instance->MCR, CAN_MCR_ABOM);
00308   }
00309   else
00310   {
00311     CLEAR_BIT(hcan->Instance->MCR, CAN_MCR_ABOM);
00312   }
00313 
00314   /* Set the automatic wake-up mode */
00315   if (hcan->Init.AutoWakeUp == ENABLE)
00316   {
00317     SET_BIT(hcan->Instance->MCR, CAN_MCR_AWUM);
00318   }
00319   else
00320   {
00321     CLEAR_BIT(hcan->Instance->MCR, CAN_MCR_AWUM);
00322   }
00323 
00324   /* Set the automatic retransmission */
00325   if (hcan->Init.AutoRetransmission == ENABLE)
00326   {
00327     CLEAR_BIT(hcan->Instance->MCR, CAN_MCR_NART);
00328   }
00329   else
00330   {
00331     SET_BIT(hcan->Instance->MCR, CAN_MCR_NART);
00332   }
00333 
00334   /* Set the receive FIFO locked mode */
00335   if (hcan->Init.ReceiveFifoLocked == ENABLE)
00336   {
00337     SET_BIT(hcan->Instance->MCR, CAN_MCR_RFLM);
00338   }
00339   else
00340   {
00341     CLEAR_BIT(hcan->Instance->MCR, CAN_MCR_RFLM);
00342   }
00343 
00344   /* Set the transmit FIFO priority */
00345   if (hcan->Init.TransmitFifoPriority == ENABLE)
00346   {
00347     SET_BIT(hcan->Instance->MCR, CAN_MCR_TXFP);
00348   }
00349   else
00350   {
00351     CLEAR_BIT(hcan->Instance->MCR, CAN_MCR_TXFP);
00352   }
00353 
00354   /* Set the bit timing register */
00355   WRITE_REG(hcan->Instance->BTR, (uint32_t)(hcan->Init.Mode           |
00356                                             hcan->Init.SyncJumpWidth  |
00357                                             hcan->Init.TimeSeg1       |
00358                                             hcan->Init.TimeSeg2       |
00359                                             (hcan->Init.Prescaler - 1U)));
00360 
00361   /* Initialize the error code */
00362   hcan->ErrorCode = HAL_CAN_ERROR_NONE;
00363 
00364   /* Initialize the CAN state */
00365   hcan->State = HAL_CAN_STATE_READY;
00366 
00367   /* Return function status */
00368   return HAL_OK;
00369 }
00370 
00371 /**
00372   * @brief  Deinitializes the CAN peripheral registers to their default
00373   *         reset values.
00374   * @param  hcan pointer to a CAN_HandleTypeDef structure that contains
00375   *         the configuration information for the specified CAN.
00376   * @retval HAL status
00377   */
00378 HAL_StatusTypeDef HAL_CAN_DeInit(CAN_HandleTypeDef *hcan)
00379 {
00380   /* Check CAN handle */
00381   if (hcan == NULL)
00382   {
00383     return HAL_ERROR;
00384   }
00385 
00386   /* Check the parameters */
00387   assert_param(IS_CAN_ALL_INSTANCE(hcan->Instance));
00388 
00389   /* Stop the CAN module */
00390   HAL_CAN_Stop(hcan);
00391 
00392   /* DeInit the low level hardware: CLOCK, NVIC */
00393   HAL_CAN_MspDeInit(hcan);
00394 
00395   /* Reset the CAN peripheral */
00396   SET_BIT(hcan->Instance->MCR, CAN_MCR_RESET);
00397 
00398   /* Reset the CAN ErrorCode */
00399   hcan->ErrorCode = HAL_CAN_ERROR_NONE;
00400 
00401   /* Change CAN state */
00402   hcan->State = HAL_CAN_STATE_RESET;
00403 
00404   /* Return function status */
00405   return HAL_OK;
00406 }
00407 
00408 /**
00409   * @brief  Initializes the CAN MSP.
00410   * @param  hcan pointer to a CAN_HandleTypeDef structure that contains
00411   *         the configuration information for the specified CAN.
00412   * @retval None
00413   */
00414 __weak void HAL_CAN_MspInit(CAN_HandleTypeDef *hcan)
00415 {
00416   /* Prevent unused argument(s) compilation warning */
00417   UNUSED(hcan);
00418 
00419   /* NOTE : This function Should not be modified, when the callback is needed,
00420             the HAL_CAN_MspInit could be implemented in the user file
00421    */
00422 }
00423 
00424 /**
00425   * @brief  DeInitializes the CAN MSP.
00426   * @param  hcan pointer to a CAN_HandleTypeDef structure that contains
00427   *         the configuration information for the specified CAN.
00428   * @retval None
00429   */
00430 __weak void HAL_CAN_MspDeInit(CAN_HandleTypeDef *hcan)
00431 {
00432   /* Prevent unused argument(s) compilation warning */
00433   UNUSED(hcan);
00434 
00435   /* NOTE : This function Should not be modified, when the callback is needed,
00436             the HAL_CAN_MspDeInit could be implemented in the user file
00437    */
00438 }
00439 
00440 
00441 /**
00442   * @}
00443   */
00444 
00445 /** @defgroup CAN_Exported_Functions_Group2 Configuration functions
00446  *  @brief    Configuration functions.
00447  *
00448 @verbatim
00449   ==============================================================================
00450               ##### Configuration functions #####
00451   ==============================================================================
00452     [..]  This section provides functions allowing to:
00453       (+) HAL_CAN_ConfigFilter            : Configure the CAN reception filters
00454 
00455 @endverbatim
00456   * @{
00457   */
00458 
00459 /**
00460   * @brief  Configures the CAN reception filter according to the specified
00461   *         parameters in the CAN_FilterInitStruct.
00462   * @param  hcan pointer to a CAN_HandleTypeDef structure that contains
00463   *         the configuration information for the specified CAN.
00464   * @param  sFilterConfig pointer to a CAN_FilterTypeDef structure that
00465   *         contains the filter configuration information.
00466   * @retval None
00467   */
00468 HAL_StatusTypeDef HAL_CAN_ConfigFilter(CAN_HandleTypeDef *hcan, CAN_FilterTypeDef *sFilterConfig)
00469 {
00470   uint32_t filternbrbitpos = 0U;
00471   CAN_TypeDef *can_ip = hcan->Instance;
00472 
00473   if ((hcan->State == HAL_CAN_STATE_READY) ||
00474       (hcan->State == HAL_CAN_STATE_LISTENING))
00475   {
00476     /* Check the parameters */
00477     assert_param(IS_CAN_FILTER_ID_HALFWORD(sFilterConfig->FilterIdHigh));
00478     assert_param(IS_CAN_FILTER_ID_HALFWORD(sFilterConfig->FilterIdLow));
00479     assert_param(IS_CAN_FILTER_ID_HALFWORD(sFilterConfig->FilterMaskIdHigh));
00480     assert_param(IS_CAN_FILTER_ID_HALFWORD(sFilterConfig->FilterMaskIdLow));
00481     assert_param(IS_CAN_FILTER_MODE(sFilterConfig->FilterMode));
00482     assert_param(IS_CAN_FILTER_SCALE(sFilterConfig->FilterScale));
00483     assert_param(IS_CAN_FILTER_FIFO(sFilterConfig->FilterFIFOAssignment));
00484     assert_param(IS_FUNCTIONAL_STATE(sFilterConfig->FilterActivation));
00485 
00486 #if defined(CAN3)
00487     /* Check the CAN instance */
00488     if (hcan->Instance == CAN3)
00489     {
00490       /* CAN3 is single instance with 14 dedicated filters banks */
00491 
00492       /* Check the parameters */
00493       assert_param(IS_CAN_FILTER_BANK_SINGLE(sFilterConfig->FilterBank));
00494     }
00495     else
00496     {
00497       /* CAN1 and CAN2 are dual instances with 28 common filters banks */
00498       /* Select master instance to access the filter banks */
00499       can_ip = CAN1;
00500 
00501       /* Check the parameters */
00502       assert_param(IS_CAN_FILTER_BANK_DUAL(sFilterConfig->FilterBank));
00503       assert_param(IS_CAN_FILTER_BANK_DUAL(sFilterConfig->SlaveStartFilterBank));
00504     }
00505 #elif defined(CAN2)
00506     /* CAN1 and CAN2 are dual instances with 28 common filters banks */
00507     /* Select master instance to access the filter banks */
00508     can_ip = CAN1;
00509 
00510     /* Check the parameters */
00511     assert_param(IS_CAN_FILTER_BANK_DUAL(sFilterConfig->FilterBank));
00512     assert_param(IS_CAN_FILTER_BANK_DUAL(sFilterConfig->SlaveStartFilterBank));
00513 #else
00514     /* CAN1 is single instance with 14 dedicated filters banks */
00515 
00516     /* Check the parameters */
00517     assert_param(IS_CAN_FILTER_BANK_SINGLE(sFilterConfig->FilterBank));
00518 #endif
00519 
00520     /* Initialisation mode for the filter */
00521     SET_BIT(can_ip->FMR, CAN_FMR_FINIT);
00522 
00523 #if defined(CAN3)
00524     /* Check the CAN instance */
00525     if (can_ip == CAN1)
00526     {
00527       /* Select the start filter number of CAN2 slave instance */
00528       CLEAR_BIT(can_ip->FMR, CAN_FMR_CAN2SB);
00529       SET_BIT(can_ip->FMR, sFilterConfig->SlaveStartFilterBank << CAN_FMR_CAN2SB_Pos);
00530     }
00531 
00532 #elif defined(CAN2)
00533     /* Select the start filter number of CAN2 slave instance */
00534     CLEAR_BIT(can_ip->FMR, CAN_FMR_CAN2SB);
00535     SET_BIT(can_ip->FMR, sFilterConfig->SlaveStartFilterBank << CAN_FMR_CAN2SB_Pos);
00536 
00537 #endif
00538     /* Convert filter number into bit position */
00539     filternbrbitpos = (1U) << sFilterConfig->FilterBank;
00540 
00541     /* Filter Deactivation */
00542     CLEAR_BIT(can_ip->FA1R, filternbrbitpos);
00543 
00544     /* Filter Scale */
00545     if (sFilterConfig->FilterScale == CAN_FILTERSCALE_16BIT)
00546     {
00547       /* 16-bit scale for the filter */
00548       CLEAR_BIT(can_ip->FS1R, filternbrbitpos);
00549 
00550       /* First 16-bit identifier and First 16-bit mask */
00551       /* Or First 16-bit identifier and Second 16-bit identifier */
00552       can_ip->sFilterRegister[sFilterConfig->FilterBank].FR1 =
00553         ((0x0000FFFFU & (uint32_t)sFilterConfig->FilterMaskIdLow) << 16U) |
00554         (0x0000FFFFU & (uint32_t)sFilterConfig->FilterIdLow);
00555 
00556       /* Second 16-bit identifier and Second 16-bit mask */
00557       /* Or Third 16-bit identifier and Fourth 16-bit identifier */
00558       can_ip->sFilterRegister[sFilterConfig->FilterBank].FR2 =
00559         ((0x0000FFFFU & (uint32_t)sFilterConfig->FilterMaskIdHigh) << 16U) |
00560         (0x0000FFFFU & (uint32_t)sFilterConfig->FilterIdHigh);
00561     }
00562 
00563     if (sFilterConfig->FilterScale == CAN_FILTERSCALE_32BIT)
00564     {
00565       /* 32-bit scale for the filter */
00566       SET_BIT(can_ip->FS1R, filternbrbitpos);
00567 
00568       /* 32-bit identifier or First 32-bit identifier */
00569       can_ip->sFilterRegister[sFilterConfig->FilterBank].FR1 =
00570         ((0x0000FFFFU & (uint32_t)sFilterConfig->FilterIdHigh) << 16U) |
00571         (0x0000FFFFU & (uint32_t)sFilterConfig->FilterIdLow);
00572 
00573       /* 32-bit mask or Second 32-bit identifier */
00574       can_ip->sFilterRegister[sFilterConfig->FilterBank].FR2 =
00575         ((0x0000FFFFU & (uint32_t)sFilterConfig->FilterMaskIdHigh) << 16U) |
00576         (0x0000FFFFU & (uint32_t)sFilterConfig->FilterMaskIdLow);
00577     }
00578 
00579     /* Filter Mode */
00580     if (sFilterConfig->FilterMode == CAN_FILTERMODE_IDMASK)
00581     {
00582       /* Id/Mask mode for the filter*/
00583       CLEAR_BIT(can_ip->FM1R, filternbrbitpos);
00584     }
00585     else /* CAN_FilterInitStruct->CAN_FilterMode == CAN_FilterMode_IdList */
00586     {
00587       /* Identifier list mode for the filter*/
00588       SET_BIT(can_ip->FM1R, filternbrbitpos);
00589     }
00590 
00591     /* Filter FIFO assignment */
00592     if (sFilterConfig->FilterFIFOAssignment == CAN_FILTER_FIFO0)
00593     {
00594       /* FIFO 0 assignation for the filter */
00595       CLEAR_BIT(can_ip->FFA1R, filternbrbitpos);
00596     }
00597     else
00598     {
00599       /* FIFO 1 assignation for the filter */
00600       SET_BIT(can_ip->FFA1R, filternbrbitpos);
00601     }
00602 
00603     /* Filter activation */
00604     if (sFilterConfig->FilterActivation == ENABLE)
00605     {
00606       SET_BIT(can_ip->FA1R, filternbrbitpos);
00607     }
00608 
00609     /* Leave the initialisation mode for the filter */
00610     CLEAR_BIT(can_ip->FMR, CAN_FMR_FINIT);
00611 
00612     /* Return function status */
00613     return HAL_OK;
00614   }
00615   else
00616   {
00617     /* Update error code */
00618     hcan->ErrorCode |= HAL_CAN_ERROR_NOT_INITIALIZED;
00619 
00620     return HAL_ERROR;
00621   }
00622 }
00623 
00624 /**
00625   * @}
00626   */
00627 
00628 /** @defgroup CAN_Exported_Functions_Group3 Control functions
00629  *  @brief    Control functions
00630  *
00631 @verbatim
00632   ==============================================================================
00633                       ##### Control functions #####
00634   ==============================================================================
00635     [..]  This section provides functions allowing to:
00636       (+) HAL_CAN_Start                    : Start the CAN module
00637       (+) HAL_CAN_Stop                     : Stop the CAN module
00638       (+) HAL_CAN_RequestSleep             : Request sleep mode entry.
00639       (+) HAL_CAN_WakeUp                   : Wake up from sleep mode.
00640       (+) HAL_CAN_IsSleepActive            : Check is sleep mode is active.
00641       (+) HAL_CAN_AddTxMessage             : Add a message to the Tx mailboxes
00642                                              and activate the corresponding
00643                                              transmission request
00644       (+) HAL_CAN_AbortTxRequest           : Abort transmission request
00645       (+) HAL_CAN_GetTxMailboxesFreeLevel  : Return Tx mailboxes free level
00646       (+) HAL_CAN_IsTxMessagePending       : Check if a transmission request is
00647                                              pending on the selected Tx mailbox
00648       (+) HAL_CAN_GetRxMessage             : Get a CAN frame from the Rx FIFO
00649       (+) HAL_CAN_GetRxFifoFillLevel       : Return Rx FIFO fill level
00650 
00651 @endverbatim
00652   * @{
00653   */
00654 
00655 /**
00656   * @brief  Start the CAN module.
00657   * @param  hcan pointer to an CAN_HandleTypeDef structure that contains
00658   *         the configuration information for the specified CAN.
00659   * @retval HAL status
00660   */
00661 HAL_StatusTypeDef HAL_CAN_Start(CAN_HandleTypeDef *hcan)
00662 {
00663   uint32_t tickstart = 0U;
00664 
00665   if (hcan->State == HAL_CAN_STATE_READY)
00666   {
00667     /* Change CAN peripheral state */
00668     hcan->State = HAL_CAN_STATE_LISTENING;
00669 
00670     /* Request leave initialisation */
00671     CLEAR_BIT(hcan->Instance->MCR, CAN_MCR_INRQ);
00672 
00673     /* Get tick */
00674     tickstart = HAL_GetTick();
00675 
00676     /* Wait the acknowledge */
00677     while ((hcan->Instance->MSR & CAN_MSR_INAK) != RESET)
00678     {
00679       /* Check for the Timeout */
00680       if ((HAL_GetTick() - tickstart) > CAN_TIMEOUT_VALUE)
00681       {
00682         /* Update error code */
00683         hcan->ErrorCode |= HAL_CAN_ERROR_TIMEOUT;
00684 
00685         /* Change CAN state */
00686         hcan->State = HAL_CAN_STATE_ERROR;
00687 
00688         return HAL_ERROR;
00689       }
00690     }
00691 
00692     /* Reset the CAN ErrorCode */
00693     hcan->ErrorCode = HAL_CAN_ERROR_NONE;
00694 
00695     /* Return function status */
00696     return HAL_OK;
00697   }
00698   else
00699   {
00700     /* Update error code */
00701     hcan->ErrorCode |= HAL_CAN_ERROR_NOT_READY;
00702 
00703     return HAL_ERROR;
00704   }
00705 }
00706 
00707 /**
00708   * @brief  Stop the CAN module and enable access to configuration registers.
00709   * @param  hcan pointer to an CAN_HandleTypeDef structure that contains
00710   *         the configuration information for the specified CAN.
00711   * @retval HAL status
00712   */
00713 HAL_StatusTypeDef HAL_CAN_Stop(CAN_HandleTypeDef *hcan)
00714 {
00715   uint32_t tickstart = 0U;
00716 
00717   if (hcan->State == HAL_CAN_STATE_LISTENING)
00718   {
00719     /* Request initialisation */
00720     SET_BIT(hcan->Instance->MCR, CAN_MCR_INRQ);
00721 
00722     /* Get tick */
00723     tickstart = HAL_GetTick();
00724 
00725     /* Wait the acknowledge */
00726     while ((hcan->Instance->MSR & CAN_MSR_INAK) == RESET)
00727     {
00728       /* Check for the Timeout */
00729       if ((HAL_GetTick() - tickstart) > CAN_TIMEOUT_VALUE)
00730       {
00731         /* Update error code */
00732         hcan->ErrorCode |= HAL_CAN_ERROR_TIMEOUT;
00733 
00734         /* Change CAN state */
00735         hcan->State = HAL_CAN_STATE_ERROR;
00736 
00737         return HAL_ERROR;
00738       }
00739     }
00740 
00741     /* Exit from sleep mode */
00742     CLEAR_BIT(hcan->Instance->MCR, CAN_MCR_SLEEP);
00743 
00744     /* Change CAN peripheral state */
00745     hcan->State = HAL_CAN_STATE_READY;
00746 
00747     /* Return function status */
00748     return HAL_OK;
00749   }
00750   else
00751   {
00752     /* Update error code */
00753     hcan->ErrorCode |= HAL_CAN_ERROR_NOT_STARTED;
00754 
00755     return HAL_ERROR;
00756   }
00757 }
00758 
00759 /**
00760   * @brief  Request the sleep mode (low power) entry.
00761   *         When returning from this function, Sleep mode will be entered
00762   *         as soon as the current CAN activity (transmission or reception
00763   *         of a CAN frame) has been completed.
00764   * @param  hcan pointer to a CAN_HandleTypeDef structure that contains
00765   *         the configuration information for the specified CAN.
00766   * @retval HAL status.
00767   */
00768 HAL_StatusTypeDef HAL_CAN_RequestSleep(CAN_HandleTypeDef *hcan)
00769 {
00770   if ((hcan->State == HAL_CAN_STATE_READY) ||
00771       (hcan->State == HAL_CAN_STATE_LISTENING))
00772   {
00773     /* Request Sleep mode */
00774     SET_BIT(hcan->Instance->MCR, CAN_MCR_SLEEP);
00775 
00776     /* Return function status */
00777     return HAL_OK;
00778   }
00779   else
00780   {
00781     /* Update error code */
00782     hcan->ErrorCode |= HAL_CAN_ERROR_NOT_INITIALIZED;
00783 
00784     /* Return function status */
00785     return HAL_ERROR;
00786   }
00787 }
00788 
00789 /**
00790   * @brief  Wake up from sleep mode.
00791   *         When returning with HAL_OK status from this function, Sleep mode
00792   *         is exited.
00793   * @param  hcan pointer to a CAN_HandleTypeDef structure that contains
00794   *         the configuration information for the specified CAN.
00795   * @retval HAL status.
00796   */
00797 HAL_StatusTypeDef HAL_CAN_WakeUp(CAN_HandleTypeDef *hcan)
00798 {
00799   __IO uint32_t count = 0;
00800   uint32_t timeout = 1000000U;
00801 
00802   if ((hcan->State == HAL_CAN_STATE_READY) ||
00803       (hcan->State == HAL_CAN_STATE_LISTENING))
00804   {
00805     /* Wake up request */
00806     CLEAR_BIT(hcan->Instance->MCR, CAN_MCR_SLEEP);
00807 
00808     /* Wait sleep mode is exited */
00809     do
00810     {
00811       /* Check if timeout is reached */
00812       if (++count > timeout)
00813       {
00814         /* Update error code */
00815         hcan->ErrorCode |= HAL_CAN_ERROR_TIMEOUT;
00816 
00817         return HAL_ERROR;
00818       }
00819     }
00820     while ((hcan->Instance->MSR & CAN_MSR_SLAK) != RESET);
00821 
00822     /* Return function status */
00823     return HAL_OK;
00824   }
00825   else
00826   {
00827     /* Update error code */
00828     hcan->ErrorCode |= HAL_CAN_ERROR_NOT_INITIALIZED;
00829 
00830     return HAL_ERROR;
00831   }
00832 }
00833 
00834 /**
00835   * @brief  Check is sleep mode is active.
00836   * @param  hcan pointer to a CAN_HandleTypeDef structure that contains
00837   *         the configuration information for the specified CAN.
00838   * @retval Status
00839   *          - 0 : Sleep mode is not active.
00840   *          - 1 : Sleep mode is active.
00841   */
00842 uint32_t HAL_CAN_IsSleepActive(CAN_HandleTypeDef *hcan)
00843 {
00844   uint32_t status = 0U;
00845 
00846   if ((hcan->State == HAL_CAN_STATE_READY) ||
00847       (hcan->State == HAL_CAN_STATE_LISTENING))
00848   {
00849     /* Check Sleep mode */
00850     if ((hcan->Instance->MSR & CAN_MSR_SLAK) != RESET)
00851     {
00852       status = 1U;
00853     }
00854   }
00855 
00856   /* Return function status */
00857   return status;
00858 }
00859 
00860 /**
00861   * @brief  Add a message to the first free Tx mailbox and activate the
00862   *         corresponding transmission request.
00863   * @param  hcan pointer to a CAN_HandleTypeDef structure that contains
00864   *         the configuration information for the specified CAN.
00865   * @param  pHeader pointer to a CAN_TxHeaderTypeDef structure.
00866   * @param  aData array containing the payload of the Tx frame.
00867   * @param  pTxMailbox pointer to a variable where the function will return
00868   *         the TxMailbox used to store the Tx message.
00869   *         This parameter can be a value of @arg CAN_Tx_Mailboxes.
00870   * @retval HAL status
00871   */
00872 HAL_StatusTypeDef HAL_CAN_AddTxMessage(CAN_HandleTypeDef *hcan, CAN_TxHeaderTypeDef *pHeader, uint8_t aData[], uint32_t *pTxMailbox)
00873 {
00874   uint32_t transmitmailbox;
00875 
00876   /* Check the parameters */
00877   assert_param(IS_CAN_IDTYPE(pHeader->IDE));
00878   assert_param(IS_CAN_RTR(pHeader->RTR));
00879   assert_param(IS_CAN_DLC(pHeader->DLC));
00880   if (pHeader->IDE == CAN_ID_STD)
00881   {
00882     assert_param(IS_CAN_STDID(pHeader->StdId));
00883   }
00884   else
00885   {
00886     assert_param(IS_CAN_EXTID(pHeader->ExtId));
00887   }
00888   assert_param(IS_FUNCTIONAL_STATE(pHeader->TransmitGlobalTime));
00889 
00890   if ((hcan->State == HAL_CAN_STATE_READY) ||
00891       (hcan->State == HAL_CAN_STATE_LISTENING))
00892   {
00893     /* Check that all the Tx mailboxes are not full */
00894     if (((hcan->Instance->TSR & CAN_TSR_TME0) != RESET) ||
00895         ((hcan->Instance->TSR & CAN_TSR_TME1) != RESET) ||
00896         ((hcan->Instance->TSR & CAN_TSR_TME2) != RESET))
00897     {
00898       /* Select an empty transmit mailbox */
00899       transmitmailbox = (hcan->Instance->TSR & CAN_TSR_CODE) >> CAN_TSR_CODE_Pos;
00900 
00901       /* Store the Tx mailbox */
00902       *pTxMailbox = 1U << transmitmailbox;
00903 
00904       /* Set up the Id */
00905       if (pHeader->IDE == CAN_ID_STD)
00906       {
00907         hcan->Instance->sTxMailBox[transmitmailbox].TIR = ((pHeader->StdId << CAN_TI0R_STID_Pos) |
00908                                                            pHeader->RTR);
00909       }
00910       else
00911       {
00912         hcan->Instance->sTxMailBox[transmitmailbox].TIR = ((pHeader->ExtId << CAN_TI0R_EXID_Pos) |
00913                                                            pHeader->IDE |
00914                                                            pHeader->RTR);
00915       }
00916 
00917       /* Set up the DLC */
00918       hcan->Instance->sTxMailBox[transmitmailbox].TDTR = (pHeader->DLC);
00919 
00920       /* Set up the Transmit Global Time mode */
00921       if (pHeader->TransmitGlobalTime == ENABLE)
00922       {
00923         SET_BIT(hcan->Instance->sTxMailBox[transmitmailbox].TDTR, CAN_TDT0R_TGT);
00924       }
00925 
00926       /* Set up the data field */
00927       WRITE_REG(hcan->Instance->sTxMailBox[transmitmailbox].TDHR,
00928                 ((uint32_t)aData[7] << CAN_TDH0R_DATA7_Pos) |
00929                 ((uint32_t)aData[6] << CAN_TDH0R_DATA6_Pos) |
00930                 ((uint32_t)aData[5] << CAN_TDH0R_DATA5_Pos) |
00931                 ((uint32_t)aData[4] << CAN_TDH0R_DATA4_Pos));
00932       WRITE_REG(hcan->Instance->sTxMailBox[transmitmailbox].TDLR,
00933                 ((uint32_t)aData[3] << CAN_TDL0R_DATA3_Pos) |
00934                 ((uint32_t)aData[2] << CAN_TDL0R_DATA2_Pos) |
00935                 ((uint32_t)aData[1] << CAN_TDL0R_DATA1_Pos) |
00936                 ((uint32_t)aData[0] << CAN_TDL0R_DATA0_Pos));
00937 
00938       /* Request transmission */
00939       SET_BIT(hcan->Instance->sTxMailBox[transmitmailbox].TIR, CAN_TI0R_TXRQ);
00940 
00941       /* Return function status */
00942       return HAL_OK;
00943     }
00944     else
00945     {
00946       /* Update error code */
00947       hcan->ErrorCode |= HAL_CAN_ERROR_PARAM;
00948 
00949       return HAL_ERROR;
00950     }
00951   }
00952   else
00953   {
00954     /* Update error code */
00955     hcan->ErrorCode |= HAL_CAN_ERROR_NOT_INITIALIZED;
00956 
00957     return HAL_ERROR;
00958   }
00959 }
00960 
00961 /**
00962   * @brief  Abort transmission requests
00963   * @param  hcan pointer to an CAN_HandleTypeDef structure that contains
00964   *         the configuration information for the specified CAN.
00965   * @param  TxMailboxes List of the Tx Mailboxes to abort.
00966   *         This parameter can be any combination of @arg CAN_Tx_Mailboxes.
00967   * @retval HAL status
00968   */
00969 HAL_StatusTypeDef HAL_CAN_AbortTxRequest(CAN_HandleTypeDef *hcan, uint32_t TxMailboxes)
00970 {
00971   /* Check function parameters */
00972   assert_param(IS_CAN_TX_MAILBOX_LIST(TxMailboxes));
00973 
00974   if ((hcan->State == HAL_CAN_STATE_READY) ||
00975       (hcan->State == HAL_CAN_STATE_LISTENING))
00976   {
00977     /* Check Tx Mailbox 0 */
00978     if ((TxMailboxes & CAN_TX_MAILBOX0) != RESET)
00979     {
00980       /* Add cancellation request for Tx Mailbox 0 */
00981       SET_BIT(hcan->Instance->TSR, CAN_TSR_ABRQ0);
00982     }
00983 
00984     /* Check Tx Mailbox 1 */
00985     if ((TxMailboxes & CAN_TX_MAILBOX1) != RESET)
00986     {
00987       /* Add cancellation request for Tx Mailbox 1 */
00988       SET_BIT(hcan->Instance->TSR, CAN_TSR_ABRQ1);
00989     }
00990 
00991     /* Check Tx Mailbox 2 */
00992     if ((TxMailboxes & CAN_TX_MAILBOX2) != RESET)
00993     {
00994       /* Add cancellation request for Tx Mailbox 2 */
00995       SET_BIT(hcan->Instance->TSR, CAN_TSR_ABRQ2);
00996     }
00997 
00998     /* Return function status */
00999     return HAL_OK;
01000   }
01001   else
01002   {
01003     /* Update error code */
01004     hcan->ErrorCode |= HAL_CAN_ERROR_NOT_INITIALIZED;
01005 
01006     return HAL_ERROR;
01007   }
01008 }
01009 
01010 /**
01011   * @brief  Return Tx Mailboxes free level: number of free Tx Mailboxes.
01012   * @param  hcan pointer to a CAN_HandleTypeDef structure that contains
01013   *         the configuration information for the specified CAN.
01014   * @retval Number of free Tx Mailboxes.
01015   */
01016 uint32_t HAL_CAN_GetTxMailboxesFreeLevel(CAN_HandleTypeDef *hcan)
01017 {
01018   uint32_t freelevel = 0U;
01019 
01020   if ((hcan->State == HAL_CAN_STATE_READY) ||
01021       (hcan->State == HAL_CAN_STATE_LISTENING))
01022   {
01023     /* Check Tx Mailbox 0 status */
01024     if ((hcan->Instance->TSR & CAN_TSR_TME0) != RESET)
01025     {
01026       freelevel++;
01027     }
01028 
01029     /* Check Tx Mailbox 1 status */
01030     if ((hcan->Instance->TSR & CAN_TSR_TME1) != RESET)
01031     {
01032       freelevel++;
01033     }
01034 
01035     /* Check Tx Mailbox 2 status */
01036     if ((hcan->Instance->TSR & CAN_TSR_TME2) != RESET)
01037     {
01038       freelevel++;
01039     }
01040   }
01041 
01042   /* Return Tx Mailboxes free level */
01043   return freelevel;
01044 }
01045 
01046 /**
01047   * @brief  Check if a transmission request is pending on the selected Tx
01048   *         Mailboxes.
01049   * @param  hcan pointer to an CAN_HandleTypeDef structure that contains
01050   *         the configuration information for the specified CAN.
01051   * @param  TxMailboxes List of Tx Mailboxes to check.
01052   *         This parameter can be any combination of @arg CAN_Tx_Mailboxes.
01053   * @retval Status
01054   *          - 0 : No pending transmission request on any selected Tx Mailboxes.
01055   *          - 1 : Pending transmission request on at least one of the selected
01056   *                Tx Mailbox.
01057   */
01058 uint32_t HAL_CAN_IsTxMessagePending(CAN_HandleTypeDef *hcan, uint32_t TxMailboxes)
01059 {
01060   uint32_t status = 0U;
01061 
01062   /* Check function parameters */
01063   assert_param(IS_CAN_TX_MAILBOX_LIST(TxMailboxes));
01064 
01065   if ((hcan->State == HAL_CAN_STATE_READY) ||
01066       (hcan->State == HAL_CAN_STATE_LISTENING))
01067   {
01068     /* Check pending transmission request on the selected Tx Mailboxes */
01069     if ((hcan->Instance->TSR & (TxMailboxes << CAN_TSR_TME0_Pos)) != (TxMailboxes << CAN_TSR_TME0_Pos))
01070     {
01071       status = 1U;
01072     }
01073   }
01074 
01075   /* Return status */
01076   return status;
01077 }
01078 
01079 /**
01080   * @brief  Return timestamp of Tx message sent, if time triggered communication
01081             mode is enabled.
01082   * @param  hcan pointer to a CAN_HandleTypeDef structure that contains
01083   *         the configuration information for the specified CAN.
01084   * @param  TxMailbox Tx Mailbox where the timestamp of message sent will be
01085   *         read.
01086   *         This parameter can be one value of @arg CAN_Tx_Mailboxes.
01087   * @retval Timestamp of message sent from Tx Mailbox.
01088   */
01089 uint32_t HAL_CAN_GetTxTimestamp(CAN_HandleTypeDef *hcan, uint32_t TxMailbox)
01090 {
01091   uint32_t timestamp = 0U;
01092   uint32_t transmitmailbox;
01093 
01094   /* Check function parameters */
01095   assert_param(IS_CAN_TX_MAILBOX(TxMailbox));
01096 
01097   if ((hcan->State == HAL_CAN_STATE_READY) ||
01098       (hcan->State == HAL_CAN_STATE_LISTENING))
01099   {
01100     /* Select the Tx mailbox */
01101     transmitmailbox = POSITION_VAL(TxMailbox);
01102 
01103     /* Get timestamp */
01104     timestamp = (hcan->Instance->sTxMailBox[transmitmailbox].TDTR & CAN_TDT0R_TIME) >> CAN_TDT0R_TIME_Pos;
01105   }
01106 
01107   /* Return the timestamp */
01108   return timestamp;
01109 }
01110 
01111 /**
01112   * @brief  Get an CAN frame from the Rx FIFO zone into the message RAM.
01113   * @param  hcan pointer to an CAN_HandleTypeDef structure that contains
01114   *         the configuration information for the specified CAN.
01115   * @param  RxFifo Fifo number of the received message to be read.
01116   *         This parameter can be a value of @arg CAN_receive_FIFO_number.
01117   * @param  pHeader pointer to a CAN_RxHeaderTypeDef structure where the header
01118   *         of the Rx frame will be stored.
01119   * @param  aData array where the payload of the Rx frame will be stored.
01120   * @retval HAL status
01121   */
01122 HAL_StatusTypeDef HAL_CAN_GetRxMessage(CAN_HandleTypeDef *hcan, uint32_t RxFifo, CAN_RxHeaderTypeDef *pHeader, uint8_t aData[])
01123 {
01124   assert_param(IS_CAN_RX_FIFO(RxFifo));
01125 
01126   if ((hcan->State == HAL_CAN_STATE_READY) ||
01127       (hcan->State == HAL_CAN_STATE_LISTENING))
01128   {
01129     /* Check the Rx FIFO */
01130     if (RxFifo == CAN_RX_FIFO0) /* Rx element is assigned to Rx FIFO 0 */
01131     {
01132       /* Check that the Rx FIFO 0 is not empty */
01133       if ((hcan->Instance->RF0R & CAN_RF0R_FMP0) == RESET)
01134       {
01135         /* Update error code */
01136         hcan->ErrorCode |= HAL_CAN_ERROR_PARAM;
01137 
01138         return HAL_ERROR;
01139       }
01140     }
01141     else if (RxFifo == CAN_RX_FIFO1) /* Rx element is assigned to Rx FIFO 1 */
01142     {
01143       /* Check that the Rx FIFO 1 is not empty */
01144       if ((hcan->Instance->RF1R & CAN_RF1R_FMP1) == RESET)
01145       {
01146         /* Update error code */
01147         hcan->ErrorCode |= HAL_CAN_ERROR_PARAM;
01148 
01149         return HAL_ERROR;
01150       }
01151     }
01152 
01153     /* Get the header */
01154     pHeader->IDE = CAN_RI0R_IDE & hcan->Instance->sFIFOMailBox[RxFifo].RIR;
01155     if (pHeader->IDE == CAN_ID_STD)
01156     {
01157       pHeader->StdId = (CAN_RI0R_STID & hcan->Instance->sFIFOMailBox[RxFifo].RIR) >> CAN_TI0R_STID_Pos;
01158     }
01159     else
01160     {
01161       pHeader->ExtId = ((CAN_RI0R_EXID | CAN_RI0R_STID) & hcan->Instance->sFIFOMailBox[RxFifo].RIR) >> CAN_RI0R_EXID_Pos;
01162     }
01163     pHeader->RTR = (CAN_RI0R_RTR & hcan->Instance->sFIFOMailBox[RxFifo].RIR) >> CAN_RI0R_RTR_Pos;
01164     pHeader->DLC = (CAN_RDT0R_DLC & hcan->Instance->sFIFOMailBox[RxFifo].RDTR) >> CAN_RDT0R_DLC_Pos;
01165     pHeader->FilterMatchIndex = (CAN_RDT0R_FMI & hcan->Instance->sFIFOMailBox[RxFifo].RDTR) >> CAN_RDT0R_FMI_Pos;
01166     pHeader->Timestamp = (CAN_RDT0R_TIME & hcan->Instance->sFIFOMailBox[RxFifo].RDTR) >> CAN_RDT0R_TIME_Pos;
01167 
01168     /* Get the data */
01169     aData[0] = (CAN_RDL0R_DATA0 & hcan->Instance->sFIFOMailBox[RxFifo].RDLR) >> CAN_RDL0R_DATA0_Pos;
01170     aData[1] = (CAN_RDL0R_DATA1 & hcan->Instance->sFIFOMailBox[RxFifo].RDLR) >> CAN_RDL0R_DATA1_Pos;
01171     aData[2] = (CAN_RDL0R_DATA2 & hcan->Instance->sFIFOMailBox[RxFifo].RDLR) >> CAN_RDL0R_DATA2_Pos;
01172     aData[3] = (CAN_RDL0R_DATA3 & hcan->Instance->sFIFOMailBox[RxFifo].RDLR) >> CAN_RDL0R_DATA3_Pos;
01173     aData[4] = (CAN_RDH0R_DATA4 & hcan->Instance->sFIFOMailBox[RxFifo].RDHR) >> CAN_RDH0R_DATA4_Pos;
01174     aData[5] = (CAN_RDH0R_DATA5 & hcan->Instance->sFIFOMailBox[RxFifo].RDHR) >> CAN_RDH0R_DATA5_Pos;
01175     aData[6] = (CAN_RDH0R_DATA6 & hcan->Instance->sFIFOMailBox[RxFifo].RDHR) >> CAN_RDH0R_DATA6_Pos;
01176     aData[7] = (CAN_RDH0R_DATA7 & hcan->Instance->sFIFOMailBox[RxFifo].RDHR) >> CAN_RDH0R_DATA7_Pos;
01177 
01178     /* Release the FIFO */
01179     if (RxFifo == CAN_RX_FIFO0) /* Rx element is assigned to Rx FIFO 0 */
01180     {
01181       /* Release RX FIFO 0 */
01182       SET_BIT(hcan->Instance->RF0R, CAN_RF0R_RFOM0);
01183     }
01184     else if (RxFifo == CAN_RX_FIFO1) /* Rx element is assigned to Rx FIFO 1 */
01185     {
01186       /* Release RX FIFO 1 */
01187       SET_BIT(hcan->Instance->RF1R, CAN_RF1R_RFOM1);
01188     }
01189 
01190     /* Return function status */
01191     return HAL_OK;
01192   }
01193   else
01194   {
01195     /* Update error code */
01196     hcan->ErrorCode |= HAL_CAN_ERROR_NOT_INITIALIZED;
01197 
01198     return HAL_ERROR;
01199   }
01200 }
01201 
01202 /**
01203   * @brief  Return Rx FIFO fill level.
01204   * @param  hcan pointer to an CAN_HandleTypeDef structure that contains
01205   *         the configuration information for the specified CAN.
01206   * @param  RxFifo Rx FIFO.
01207   *         This parameter can be a value of @arg CAN_receive_FIFO_number.
01208   * @retval Number of messages available in Rx FIFO.
01209   */
01210 uint32_t HAL_CAN_GetRxFifoFillLevel(CAN_HandleTypeDef *hcan, uint32_t RxFifo)
01211 {
01212   uint32_t filllevel = 0U;
01213 
01214   /* Check function parameters */
01215   assert_param(IS_CAN_RX_FIFO(RxFifo));
01216 
01217   if ((hcan->State == HAL_CAN_STATE_READY) ||
01218       (hcan->State == HAL_CAN_STATE_LISTENING))
01219   {
01220     if (RxFifo == CAN_RX_FIFO0)
01221     {
01222       filllevel = hcan->Instance->RF0R & CAN_RF0R_FMP0;
01223     }
01224     else /* RxFifo == CAN_RX_FIFO1 */
01225     {
01226       filllevel = hcan->Instance->RF1R & CAN_RF1R_FMP1;
01227     }
01228   }
01229 
01230   /* Return Rx FIFO fill level */
01231   return filllevel;
01232 }
01233 
01234 /**
01235   * @}
01236   */
01237 
01238 /** @defgroup CAN_Exported_Functions_Group4 Interrupts management
01239  *  @brief    Interrupts management
01240  *
01241 @verbatim
01242   ==============================================================================
01243                        ##### Interrupts management #####
01244   ==============================================================================
01245     [..]  This section provides functions allowing to:
01246       (+) HAL_CAN_ActivateNotification      : Enable interrupts
01247       (+) HAL_CAN_DeactivateNotification    : Disable interrupts
01248       (+) HAL_CAN_IRQHandler                : Handles CAN interrupt request
01249 
01250 @endverbatim
01251   * @{
01252   */
01253 
01254 /**
01255   * @brief  Enable interrupts.
01256   * @param  hcan pointer to an CAN_HandleTypeDef structure that contains
01257   *         the configuration information for the specified CAN.
01258   * @param  ActiveITs indicates which interrupts will be enabled.
01259   *         This parameter can be any combination of @arg CAN_Interrupts.
01260   * @retval HAL status
01261   */
01262 HAL_StatusTypeDef HAL_CAN_ActivateNotification(CAN_HandleTypeDef *hcan, uint32_t ActiveITs)
01263 {
01264   /* Check function parameters */
01265   assert_param(IS_CAN_IT(ActiveITs));
01266 
01267   if ((hcan->State == HAL_CAN_STATE_READY) ||
01268       (hcan->State == HAL_CAN_STATE_LISTENING))
01269   {
01270     /* Enable the selected interrupts */
01271     __HAL_CAN_ENABLE_IT(hcan, ActiveITs);
01272 
01273     /* Return function status */
01274     return HAL_OK;
01275   }
01276   else
01277   {
01278     /* Update error code */
01279     hcan->ErrorCode |= HAL_CAN_ERROR_NOT_INITIALIZED;
01280 
01281     return HAL_ERROR;
01282   }
01283 }
01284 
01285 /**
01286   * @brief  Disable interrupts.
01287   * @param  hcan pointer to an CAN_HandleTypeDef structure that contains
01288   *         the configuration information for the specified CAN.
01289   * @param  InactiveITs indicates which interrupts will be disabled.
01290   *         This parameter can be any combination of @arg CAN_Interrupts.
01291   * @retval HAL status
01292   */
01293 HAL_StatusTypeDef HAL_CAN_DeactivateNotification(CAN_HandleTypeDef *hcan, uint32_t InactiveITs)
01294 {
01295   /* Check function parameters */
01296   assert_param(IS_CAN_IT(InactiveITs));
01297 
01298   if ((hcan->State == HAL_CAN_STATE_READY) ||
01299       (hcan->State == HAL_CAN_STATE_LISTENING))
01300   {
01301     /* Disable the selected interrupts */
01302     __HAL_CAN_DISABLE_IT(hcan, InactiveITs);
01303 
01304     /* Return function status */
01305     return HAL_OK;
01306   }
01307   else
01308   {
01309     /* Update error code */
01310     hcan->ErrorCode |= HAL_CAN_ERROR_NOT_INITIALIZED;
01311 
01312     return HAL_ERROR;
01313   }
01314 }
01315 
01316 /**
01317   * @brief  Handles CAN interrupt request
01318   * @param  hcan pointer to a CAN_HandleTypeDef structure that contains
01319   *         the configuration information for the specified CAN.
01320   * @retval None
01321   */
01322 void HAL_CAN_IRQHandler(CAN_HandleTypeDef *hcan)
01323 {
01324   uint32_t errorcode = HAL_CAN_ERROR_NONE;
01325   uint32_t interrupts = READ_REG(hcan->Instance->IER);
01326   uint32_t msrflags = READ_REG(hcan->Instance->MSR);
01327   uint32_t tsrflags = READ_REG(hcan->Instance->TSR);
01328   uint32_t rf0rflags = READ_REG(hcan->Instance->RF0R);
01329   uint32_t rf1rflags = READ_REG(hcan->Instance->RF1R);
01330   uint32_t esrflags = READ_REG(hcan->Instance->ESR);
01331 
01332   /* Transmit Mailbox empty interrupt management *****************************/
01333   if ((interrupts & CAN_IT_TX_MAILBOX_EMPTY) != RESET)
01334   {
01335     /* Transmit Mailbox 0 management *****************************************/
01336     if ((tsrflags & CAN_TSR_RQCP0) != RESET)
01337     {
01338       /* Clear the Transmission Complete flag (and TXOK0,ALST0,TERR0 bits) */
01339       __HAL_CAN_CLEAR_FLAG(hcan, CAN_FLAG_RQCP0);
01340 
01341       if ((tsrflags & CAN_TSR_TXOK0) != RESET)
01342       {
01343         /* Transmission Mailbox 0 complete callback */
01344         /* Call weak (surcharged) callback */
01345         HAL_CAN_TxMailbox0CompleteCallback(hcan);
01346       }
01347       else
01348       {
01349         if ((tsrflags & CAN_TSR_ALST0) != RESET)
01350         {
01351           /* Update error code */
01352           errorcode |= HAL_CAN_ERROR_TX_ALST0;
01353         }
01354         else if ((tsrflags & CAN_TSR_TERR0) != RESET)
01355         {
01356           /* Update error code */
01357           errorcode |= HAL_CAN_ERROR_TX_TERR0;
01358         }
01359         else
01360         {
01361           /* Transmission Mailbox 0 abort callback */
01362           /* Call weak (surcharged) callback */
01363           HAL_CAN_TxMailbox0AbortCallback(hcan);
01364         }
01365       }
01366     }
01367 
01368     /* Transmit Mailbox 1 management *****************************************/
01369     if ((tsrflags & CAN_TSR_RQCP1) != RESET)
01370     {
01371       /* Clear the Transmission Complete flag (and TXOK1,ALST1,TERR1 bits) */
01372       __HAL_CAN_CLEAR_FLAG(hcan, CAN_FLAG_RQCP1);
01373 
01374       if ((tsrflags & CAN_TSR_TXOK1) != RESET)
01375       {
01376         /* Transmission Mailbox 1 complete callback */
01377         /* Call weak (surcharged) callback */
01378         HAL_CAN_TxMailbox1CompleteCallback(hcan);
01379       }
01380       else
01381       {
01382         if ((tsrflags & CAN_TSR_ALST1) != RESET)
01383         {
01384           /* Update error code */
01385           errorcode |= HAL_CAN_ERROR_TX_ALST1;
01386         }
01387         else if ((tsrflags & CAN_TSR_TERR1) != RESET)
01388         {
01389           /* Update error code */
01390           errorcode |= HAL_CAN_ERROR_TX_TERR1;
01391         }
01392         else
01393         {
01394           /* Transmission Mailbox 1 abort callback */
01395           /* Call weak (surcharged) callback */
01396           HAL_CAN_TxMailbox1AbortCallback(hcan);
01397         }
01398       }
01399     }
01400 
01401     /* Transmit Mailbox 2 management *****************************************/
01402     if ((tsrflags & CAN_TSR_RQCP2) != RESET)
01403     {
01404       /* Clear the Transmission Complete flag (and TXOK2,ALST2,TERR2 bits) */
01405       __HAL_CAN_CLEAR_FLAG(hcan, CAN_FLAG_RQCP2);
01406 
01407       if ((tsrflags & CAN_TSR_TXOK2) != RESET)
01408       {
01409         /* Transmission Mailbox 2 complete callback */
01410         /* Call weak (surcharged) callback */
01411         HAL_CAN_TxMailbox2CompleteCallback(hcan);
01412       }
01413       else
01414       {
01415         if ((tsrflags & CAN_TSR_ALST2) != RESET)
01416         {
01417           /* Update error code */
01418           errorcode |= HAL_CAN_ERROR_TX_ALST2;
01419         }
01420         else if ((tsrflags & CAN_TSR_TERR2) != RESET)
01421         {
01422           /* Update error code */
01423           errorcode |= HAL_CAN_ERROR_TX_TERR2;
01424         }
01425         else
01426         {
01427           /* Transmission Mailbox 2 abort callback */
01428           /* Call weak (surcharged) callback */
01429           HAL_CAN_TxMailbox2AbortCallback(hcan);
01430         }
01431       }
01432     }
01433   }
01434 
01435   /* Receive FIFO 0 overrun interrupt management *****************************/
01436   if ((interrupts & CAN_IT_RX_FIFO0_OVERRUN) != RESET)
01437   {
01438     if ((rf0rflags & CAN_RF0R_FOVR0) != RESET)
01439     {
01440       /* Set CAN error code to Rx Fifo 0 overrun error */
01441       errorcode |= HAL_CAN_ERROR_RX_FOV0;
01442 
01443       /* Clear FIFO0 Overrun Flag */
01444       __HAL_CAN_CLEAR_FLAG(hcan, CAN_FLAG_FOV0);
01445     }
01446   }
01447 
01448   /* Receive FIFO 0 full interrupt management ********************************/
01449   if ((interrupts & CAN_IT_RX_FIFO0_FULL) != RESET)
01450   {
01451     if ((rf0rflags & CAN_RF0R_FULL0) != RESET)
01452     {
01453       /* Clear FIFO 0 full Flag */
01454       __HAL_CAN_CLEAR_FLAG(hcan, CAN_FLAG_FF0);
01455 
01456       /* Receive FIFO 0 full Callback */
01457       /* Call weak (surcharged) callback */
01458       HAL_CAN_RxFifo0FullCallback(hcan);
01459     }
01460   }
01461 
01462   /* Receive FIFO 0 message pending interrupt management *********************/
01463   if ((interrupts & CAN_IT_RX_FIFO0_MSG_PENDING) != RESET)
01464   {
01465     /* Check if message is still pending */
01466     if ((hcan->Instance->RF0R & CAN_RF0R_FMP0) != RESET)
01467     {
01468       /* Receive FIFO 0 mesage pending Callback */
01469       /* Call weak (surcharged) callback */
01470       HAL_CAN_RxFifo0MsgPendingCallback(hcan);
01471     }
01472   }
01473 
01474   /* Receive FIFO 1 overrun interrupt management *****************************/
01475   if ((interrupts & CAN_IT_RX_FIFO1_OVERRUN) != RESET)
01476   {
01477     if ((rf1rflags & CAN_RF1R_FOVR1) != RESET)
01478     {
01479       /* Set CAN error code to Rx Fifo 1 overrun error */
01480       errorcode |= HAL_CAN_ERROR_RX_FOV1;
01481 
01482       /* Clear FIFO1 Overrun Flag */
01483       __HAL_CAN_CLEAR_FLAG(hcan, CAN_FLAG_FOV1);
01484     }
01485   }
01486 
01487   /* Receive FIFO 1 full interrupt management ********************************/
01488   if ((interrupts & CAN_IT_RX_FIFO1_FULL) != RESET)
01489   {
01490     if ((rf1rflags & CAN_RF1R_FULL1) != RESET)
01491     {
01492       /* Clear FIFO 1 full Flag */
01493       __HAL_CAN_CLEAR_FLAG(hcan, CAN_FLAG_FF1);
01494 
01495       /* Receive FIFO 1 full Callback */
01496       /* Call weak (surcharged) callback */
01497       HAL_CAN_RxFifo1FullCallback(hcan);
01498     }
01499   }
01500 
01501   /* Receive FIFO 1 message pending interrupt management *********************/
01502   if ((interrupts & CAN_IT_RX_FIFO1_MSG_PENDING) != RESET)
01503   {
01504     /* Check if message is still pending */
01505     if ((hcan->Instance->RF1R & CAN_RF1R_FMP1) != RESET)
01506     {
01507       /* Receive FIFO 1 mesage pending Callback */
01508       /* Call weak (surcharged) callback */
01509       HAL_CAN_RxFifo1MsgPendingCallback(hcan);
01510     }
01511   }
01512 
01513   /* Sleep interrupt management *********************************************/
01514   if ((interrupts & CAN_IT_SLEEP_ACK) != RESET)
01515   {
01516     if ((msrflags & CAN_MSR_SLAKI) != RESET)
01517     {
01518       /* Clear Sleep interrupt Flag */
01519       __HAL_CAN_CLEAR_FLAG(hcan, CAN_FLAG_SLAKI);
01520 
01521       /* Sleep Callback */
01522       /* Call weak (surcharged) callback */
01523       HAL_CAN_SleepCallback(hcan);
01524     }
01525   }
01526 
01527   /* WakeUp interrupt management *********************************************/
01528   if ((interrupts & CAN_IT_WAKEUP) != RESET)
01529   {
01530     if ((msrflags & CAN_MSR_WKUI) != RESET)
01531     {
01532       /* Clear WakeUp Flag */
01533       __HAL_CAN_CLEAR_FLAG(hcan, CAN_FLAG_WKU);
01534 
01535       /* WakeUp Callback */
01536       /* Call weak (surcharged) callback */
01537       HAL_CAN_WakeUpFromRxMsgCallback(hcan);
01538     }
01539   }
01540 
01541   /* Error interrupts management *********************************************/
01542   if ((interrupts & CAN_IT_ERROR) != RESET)
01543   {
01544     if ((msrflags & CAN_MSR_ERRI) != RESET)
01545     {
01546       /* Check Error Warning Flag */
01547       if (((interrupts & CAN_IT_ERROR_WARNING) != RESET) &&
01548           ((esrflags & CAN_ESR_EWGF) != RESET))
01549       {
01550         /* Set CAN error code to Error Warning */
01551         errorcode |= HAL_CAN_ERROR_EWG;
01552 
01553         /* No need for clear of Error Warning Flag as read-only */
01554       }
01555 
01556       /* Check Error Passive Flag */
01557       if (((interrupts & CAN_IT_ERROR_PASSIVE) != RESET) &&
01558           ((esrflags & CAN_ESR_EPVF) != RESET))
01559       {
01560         /* Set CAN error code to Error Passive */
01561         errorcode |= HAL_CAN_ERROR_EPV;
01562 
01563         /* No need for clear of Error Passive Flag as read-only */
01564       }
01565 
01566       /* Check Bus-off Flag */
01567       if (((interrupts & CAN_IT_BUSOFF) != RESET) &&
01568           ((esrflags & CAN_ESR_BOFF) != RESET))
01569       {
01570         /* Set CAN error code to Bus-Off */
01571         errorcode |= HAL_CAN_ERROR_BOF;
01572 
01573         /* No need for clear of Error Bus-Off as read-only */
01574       }
01575 
01576       /* Check Last Error Code Flag */
01577       if (((interrupts & CAN_IT_LAST_ERROR_CODE) != RESET) &&
01578           ((esrflags & CAN_ESR_LEC) != RESET))
01579       {
01580         switch (esrflags & CAN_ESR_LEC)
01581         {
01582           case (CAN_ESR_LEC_0):
01583             /* Set CAN error code to Stuff error */
01584             errorcode |= HAL_CAN_ERROR_STF;
01585             break;
01586           case (CAN_ESR_LEC_1):
01587             /* Set CAN error code to Form error */
01588             errorcode |= HAL_CAN_ERROR_FOR;
01589             break;
01590           case (CAN_ESR_LEC_1 | CAN_ESR_LEC_0):
01591             /* Set CAN error code to Acknowledgement error */
01592             errorcode |= HAL_CAN_ERROR_ACK;
01593             break;
01594           case (CAN_ESR_LEC_2):
01595             /* Set CAN error code to Bit recessive error */
01596             errorcode |= HAL_CAN_ERROR_BR;
01597             break;
01598           case (CAN_ESR_LEC_2 | CAN_ESR_LEC_0):
01599             /* Set CAN error code to Bit Dominant error */
01600             errorcode |= HAL_CAN_ERROR_BD;
01601             break;
01602           case (CAN_ESR_LEC_2 | CAN_ESR_LEC_1):
01603             /* Set CAN error code to CRC error */
01604             errorcode |= HAL_CAN_ERROR_CRC;
01605             break;
01606           default:
01607             break;
01608         }
01609 
01610         /* Clear Last error code Flag */
01611         CLEAR_BIT(hcan->Instance->ESR, CAN_ESR_LEC);
01612       }
01613     }
01614 
01615     /* Clear ERRI Flag */
01616     __HAL_CAN_CLEAR_FLAG(hcan, CAN_FLAG_ERRI);
01617   }
01618 
01619   /* Call the Error call Back in case of Errors */
01620   if (errorcode != HAL_CAN_ERROR_NONE)
01621   {
01622     /* Update error code in handle */
01623     hcan->ErrorCode |= errorcode;
01624 
01625     /* Call Error callback function */
01626     /* Call weak (surcharged) callback */
01627     HAL_CAN_ErrorCallback(hcan);
01628   }
01629 }
01630 
01631 /**
01632   * @}
01633   */
01634 
01635 /** @defgroup CAN_Exported_Functions_Group5 Callback functions
01636  *  @brief   CAN Callback functions
01637  *
01638 @verbatim
01639   ==============================================================================
01640                           ##### Callback functions #####
01641   ==============================================================================
01642     [..]
01643     This subsection provides the following callback functions:
01644       (+) HAL_CAN_TxMailbox0CompleteCallback
01645       (+) HAL_CAN_TxMailbox1CompleteCallback
01646       (+) HAL_CAN_TxMailbox2CompleteCallback
01647       (+) HAL_CAN_TxMailbox0AbortCallback
01648       (+) HAL_CAN_TxMailbox1AbortCallback
01649       (+) HAL_CAN_TxMailbox2AbortCallback
01650       (+) HAL_CAN_RxFifo0MsgPendingCallback
01651       (+) HAL_CAN_RxFifo0FullCallback
01652       (+) HAL_CAN_RxFifo1MsgPendingCallback
01653       (+) HAL_CAN_RxFifo1FullCallback
01654       (+) HAL_CAN_SleepCallback
01655       (+) HAL_CAN_WakeUpFromRxMsgCallback
01656       (+) HAL_CAN_ErrorCallback
01657 
01658 @endverbatim
01659   * @{
01660   */
01661 
01662 /**
01663   * @brief  Transmission Mailbox 0 complete callback.
01664   * @param  hcan pointer to a CAN_HandleTypeDef structure that contains
01665   *         the configuration information for the specified CAN.
01666   * @retval None
01667   */
01668 __weak void HAL_CAN_TxMailbox0CompleteCallback(CAN_HandleTypeDef *hcan)
01669 {
01670   /* Prevent unused argument(s) compilation warning */
01671   UNUSED(hcan);
01672 
01673   /* NOTE : This function Should not be modified, when the callback is needed,
01674             the HAL_CAN_TxMailbox0CompleteCallback could be implemented in the
01675             user file
01676    */
01677 }
01678 
01679 /**
01680   * @brief  Transmission Mailbox 1 complete callback.
01681   * @param  hcan pointer to a CAN_HandleTypeDef structure that contains
01682   *         the configuration information for the specified CAN.
01683   * @retval None
01684   */
01685 __weak void HAL_CAN_TxMailbox1CompleteCallback(CAN_HandleTypeDef *hcan)
01686 {
01687   /* Prevent unused argument(s) compilation warning */
01688   UNUSED(hcan);
01689 
01690   /* NOTE : This function Should not be modified, when the callback is needed,
01691             the HAL_CAN_TxMailbox1CompleteCallback could be implemented in the
01692             user file
01693    */
01694 }
01695 
01696 /**
01697   * @brief  Transmission Mailbox 2 complete callback.
01698   * @param  hcan pointer to a CAN_HandleTypeDef structure that contains
01699   *         the configuration information for the specified CAN.
01700   * @retval None
01701   */
01702 __weak void HAL_CAN_TxMailbox2CompleteCallback(CAN_HandleTypeDef *hcan)
01703 {
01704   /* Prevent unused argument(s) compilation warning */
01705   UNUSED(hcan);
01706 
01707   /* NOTE : This function Should not be modified, when the callback is needed,
01708             the HAL_CAN_TxMailbox2CompleteCallback could be implemented in the
01709             user file
01710    */
01711 }
01712 
01713 /**
01714   * @brief  Transmission Mailbox 0 Cancellation callback.
01715   * @param  hcan pointer to an CAN_HandleTypeDef structure that contains
01716   *         the configuration information for the specified CAN.
01717   * @retval None
01718   */
01719 __weak void HAL_CAN_TxMailbox0AbortCallback(CAN_HandleTypeDef *hcan)
01720 {
01721   /* Prevent unused argument(s) compilation warning */
01722   UNUSED(hcan);
01723 
01724   /* NOTE : This function Should not be modified, when the callback is needed,
01725             the HAL_CAN_TxMailbox0AbortCallback could be implemented in the
01726             user file
01727    */
01728 }
01729 
01730 /**
01731   * @brief  Transmission Mailbox 1 Cancellation callback.
01732   * @param  hcan pointer to an CAN_HandleTypeDef structure that contains
01733   *         the configuration information for the specified CAN.
01734   * @retval None
01735   */
01736 __weak void HAL_CAN_TxMailbox1AbortCallback(CAN_HandleTypeDef *hcan)
01737 {
01738   /* Prevent unused argument(s) compilation warning */
01739   UNUSED(hcan);
01740 
01741   /* NOTE : This function Should not be modified, when the callback is needed,
01742             the HAL_CAN_TxMailbox1AbortCallback could be implemented in the
01743             user file
01744    */
01745 }
01746 
01747 /**
01748   * @brief  Transmission Mailbox 2 Cancellation callback.
01749   * @param  hcan pointer to an CAN_HandleTypeDef structure that contains
01750   *         the configuration information for the specified CAN.
01751   * @retval None
01752   */
01753 __weak void HAL_CAN_TxMailbox2AbortCallback(CAN_HandleTypeDef *hcan)
01754 {
01755   /* Prevent unused argument(s) compilation warning */
01756   UNUSED(hcan);
01757 
01758   /* NOTE : This function Should not be modified, when the callback is needed,
01759             the HAL_CAN_TxMailbox2AbortCallback could be implemented in the
01760             user file
01761    */
01762 }
01763 
01764 /**
01765   * @brief  Rx FIFO 0 message pending callback.
01766   * @param  hcan pointer to a CAN_HandleTypeDef structure that contains
01767   *         the configuration information for the specified CAN.
01768   * @retval None
01769   */
01770 __weak void HAL_CAN_RxFifo0MsgPendingCallback(CAN_HandleTypeDef *hcan)
01771 {
01772   /* Prevent unused argument(s) compilation warning */
01773   UNUSED(hcan);
01774 
01775   /* NOTE : This function Should not be modified, when the callback is needed,
01776             the HAL_CAN_RxFifo0MsgPendingCallback could be implemented in the
01777             user file
01778    */
01779 }
01780 
01781 /**
01782   * @brief  Rx FIFO 0 full callback.
01783   * @param  hcan pointer to a CAN_HandleTypeDef structure that contains
01784   *         the configuration information for the specified CAN.
01785   * @retval None
01786   */
01787 __weak void HAL_CAN_RxFifo0FullCallback(CAN_HandleTypeDef *hcan)
01788 {
01789   /* Prevent unused argument(s) compilation warning */
01790   UNUSED(hcan);
01791 
01792   /* NOTE : This function Should not be modified, when the callback is needed,
01793             the HAL_CAN_RxFifo0FullCallback could be implemented in the user
01794             file
01795    */
01796 }
01797 
01798 /**
01799   * @brief  Rx FIFO 1 message pending callback.
01800   * @param  hcan pointer to a CAN_HandleTypeDef structure that contains
01801   *         the configuration information for the specified CAN.
01802   * @retval None
01803   */
01804 __weak void HAL_CAN_RxFifo1MsgPendingCallback(CAN_HandleTypeDef *hcan)
01805 {
01806   /* Prevent unused argument(s) compilation warning */
01807   UNUSED(hcan);
01808 
01809   /* NOTE : This function Should not be modified, when the callback is needed,
01810             the HAL_CAN_RxFifo1MsgPendingCallback could be implemented in the
01811             user file
01812    */
01813 }
01814 
01815 /**
01816   * @brief  Rx FIFO 1 full callback.
01817   * @param  hcan pointer to a CAN_HandleTypeDef structure that contains
01818   *         the configuration information for the specified CAN.
01819   * @retval None
01820   */
01821 __weak void HAL_CAN_RxFifo1FullCallback(CAN_HandleTypeDef *hcan)
01822 {
01823   /* Prevent unused argument(s) compilation warning */
01824   UNUSED(hcan);
01825 
01826   /* NOTE : This function Should not be modified, when the callback is needed,
01827             the HAL_CAN_RxFifo1FullCallback could be implemented in the user
01828             file
01829    */
01830 }
01831 
01832 /**
01833   * @brief  Sleep callback.
01834   * @param  hcan pointer to a CAN_HandleTypeDef structure that contains
01835   *         the configuration information for the specified CAN.
01836   * @retval None
01837   */
01838 __weak void HAL_CAN_SleepCallback(CAN_HandleTypeDef *hcan)
01839 {
01840   /* Prevent unused argument(s) compilation warning */
01841   UNUSED(hcan);
01842 
01843   /* NOTE : This function Should not be modified, when the callback is needed,
01844             the HAL_CAN_SleepCallback could be implemented in the user file
01845    */
01846 }
01847 
01848 /**
01849   * @brief  WakeUp from Rx message callback.
01850   * @param  hcan pointer to a CAN_HandleTypeDef structure that contains
01851   *         the configuration information for the specified CAN.
01852   * @retval None
01853   */
01854 __weak void HAL_CAN_WakeUpFromRxMsgCallback(CAN_HandleTypeDef *hcan)
01855 {
01856   /* Prevent unused argument(s) compilation warning */
01857   UNUSED(hcan);
01858 
01859   /* NOTE : This function Should not be modified, when the callback is needed,
01860             the HAL_CAN_WakeUpFromRxMsgCallback could be implemented in the
01861             user file
01862    */
01863 }
01864 
01865 /**
01866   * @brief  Error CAN callback.
01867   * @param  hcan pointer to a CAN_HandleTypeDef structure that contains
01868   *         the configuration information for the specified CAN.
01869   * @retval None
01870   */
01871 __weak void HAL_CAN_ErrorCallback(CAN_HandleTypeDef *hcan)
01872 {
01873   /* Prevent unused argument(s) compilation warning */
01874   UNUSED(hcan);
01875 
01876   /* NOTE : This function Should not be modified, when the callback is needed,
01877             the HAL_CAN_ErrorCallback could be implemented in the user file
01878    */
01879 }
01880 
01881 /**
01882   * @}
01883   */
01884 
01885 /** @defgroup CAN_Exported_Functions_Group6 Peripheral State and Error functions
01886  *  @brief   CAN Peripheral State functions
01887  *
01888 @verbatim
01889   ==============================================================================
01890             ##### Peripheral State and Error functions #####
01891   ==============================================================================
01892     [..]
01893     This subsection provides functions allowing to :
01894       (+) HAL_CAN_GetState()  : Return the CAN state.
01895       (+) HAL_CAN_GetError()  : Return the CAN error codes if any.
01896       (+) HAL_CAN_ResetError(): Reset the CAN error codes if any.
01897 
01898 @endverbatim
01899   * @{
01900   */
01901 
01902 /**
01903   * @brief  Return the CAN state.
01904   * @param  hcan pointer to a CAN_HandleTypeDef structure that contains
01905   *         the configuration information for the specified CAN.
01906   * @retval HAL state
01907   */
01908 HAL_CAN_StateTypeDef HAL_CAN_GetState(CAN_HandleTypeDef *hcan)
01909 {
01910   HAL_CAN_StateTypeDef state = hcan->State;
01911 
01912   if ((hcan->State == HAL_CAN_STATE_READY) ||
01913       (hcan->State == HAL_CAN_STATE_LISTENING))
01914   {
01915     /* Check sleep mode acknowledge flag */
01916     if ((hcan->Instance->MSR & CAN_MSR_SLAK) != RESET)
01917     {
01918       /* Sleep mode is active */
01919       state = HAL_CAN_STATE_SLEEP_ACTIVE;
01920     }
01921     /* Check sleep mode request flag */
01922     else if ((hcan->Instance->MCR & CAN_MCR_SLEEP) != RESET)
01923     {
01924       /* Sleep mode request is pending */
01925       state = HAL_CAN_STATE_SLEEP_PENDING;
01926     }
01927   }
01928 
01929   /* Return CAN state */
01930   return state;
01931 }
01932 
01933 /**
01934   * @brief  Return the CAN error code.
01935   * @param  hcan pointer to a CAN_HandleTypeDef structure that contains
01936   *         the configuration information for the specified CAN.
01937   * @retval CAN Error Code
01938   */
01939 uint32_t HAL_CAN_GetError(CAN_HandleTypeDef *hcan)
01940 {
01941   /* Return CAN error code */
01942   return hcan->ErrorCode;
01943 }
01944 
01945 /**
01946   * @brief  Reset the CAN error code.
01947   * @param  hcan pointer to a CAN_HandleTypeDef structure that contains
01948   *         the configuration information for the specified CAN.
01949   * @retval HAL status
01950   */
01951 HAL_StatusTypeDef HAL_CAN_ResetError(CAN_HandleTypeDef *hcan)
01952 {
01953   HAL_StatusTypeDef status = HAL_OK;
01954 
01955   if ((hcan->State == HAL_CAN_STATE_READY) ||
01956       (hcan->State == HAL_CAN_STATE_LISTENING))
01957   {
01958     /* Reset CAN error code */
01959     hcan->ErrorCode = 0U;
01960   }
01961   else
01962   {
01963     /* Update error code */
01964     hcan->ErrorCode |= HAL_CAN_ERROR_NOT_INITIALIZED;
01965 
01966     status = HAL_ERROR;
01967   }
01968 
01969   /* Return the status */
01970   return status;
01971 }
01972 
01973 /**
01974   * @}
01975   */
01976 
01977 /**
01978   * @}
01979   */
01980 
01981 #endif /* HAL_CAN_MODULE_ENABLED */
01982 
01983 /**
01984   * @}
01985   */
01986 
01987 #endif /* CAN1 */
01988 
01989 /**
01990   * @}
01991   */
01992 
01993 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/