1 /**
2   ******************************************************************************
3   * @file    stm32l4xx_hal_can.c
4   * @author  MCD Application Team
5   * @brief   CAN HAL module driver.
6   *          This file provides firmware functions to manage the following
7   *          functionalities of the Controller Area Network (CAN) peripheral:
8   *           + Initialization and de-initialization functions
9   *           + Configuration functions
10   *           + Control functions
11   *           + Interrupts management
12   *           + Callbacks functions
13   *           + Peripheral State and Error functions
14   *
15   @verbatim
16   ==============================================================================
17                         ##### How to use this driver #####
18   ==============================================================================
19     [..]
20       (#) Initialize the CAN low level resources by implementing the
21           HAL_CAN_MspInit():
22          (++) Enable the CAN interface clock using __HAL_RCC_CANx_CLK_ENABLE()
23          (++) Configure CAN pins
24              (+++) Enable the clock for the CAN GPIOs
25              (+++) Configure CAN pins as alternate function open-drain
26          (++) In case of using interrupts (e.g. HAL_CAN_ActivateNotification())
27              (+++) Configure the CAN interrupt priority using
28                    HAL_NVIC_SetPriority()
29              (+++) Enable the CAN IRQ handler using HAL_NVIC_EnableIRQ()
30              (+++) In CAN IRQ handler, call HAL_CAN_IRQHandler()
31 
32       (#) Initialize the CAN peripheral using HAL_CAN_Init() function. This
33           function resorts to HAL_CAN_MspInit() for low-level initialization.
34 
35       (#) Configure the reception filters using the following configuration
36           functions:
37             (++) HAL_CAN_ConfigFilter()
38 
39       (#) Start the CAN module using HAL_CAN_Start() function. At this level
40           the node is active on the bus: it receive messages, and can send
41           messages.
42 
43       (#) To manage messages transmission, the following Tx control functions
44           can be used:
45             (++) HAL_CAN_AddTxMessage() to request transmission of a new
46                  message.
47             (++) HAL_CAN_AbortTxRequest() to abort transmission of a pending
48                  message.
49             (++) HAL_CAN_GetTxMailboxesFreeLevel() to get the number of free Tx
50                  mailboxes.
51             (++) HAL_CAN_IsTxMessagePending() to check if a message is pending
52                  in a Tx mailbox.
53             (++) HAL_CAN_GetTxTimestamp() to get the timestamp of Tx message
54                  sent, if time triggered communication mode is enabled.
55 
56       (#) When a message is received into the CAN Rx FIFOs, it can be retrieved
57           using the HAL_CAN_GetRxMessage() function. The function
58           HAL_CAN_GetRxFifoFillLevel() allows to know how many Rx message are
59           stored in the Rx Fifo.
60 
61       (#) Calling the HAL_CAN_Stop() function stops the CAN module.
62 
63       (#) The deinitialization is achieved with HAL_CAN_DeInit() function.
64 
65 
66       *** Polling mode operation ***
67       ==============================
68     [..]
69       (#) Reception:
70             (++) Monitor reception of message using HAL_CAN_GetRxFifoFillLevel()
71                  until at least one message is received.
72             (++) Then get the message using HAL_CAN_GetRxMessage().
73 
74       (#) Transmission:
75             (++) Monitor the Tx mailboxes availability until at least one Tx
76                  mailbox is free, using HAL_CAN_GetTxMailboxesFreeLevel().
77             (++) Then request transmission of a message using
78                  HAL_CAN_AddTxMessage().
79 
80 
81       *** Interrupt mode operation ***
82       ================================
83     [..]
84       (#) Notifications are activated using HAL_CAN_ActivateNotification()
85           function. Then, the process can be controlled through the
86           available user callbacks: HAL_CAN_xxxCallback(), using same APIs
87           HAL_CAN_GetRxMessage() and HAL_CAN_AddTxMessage().
88 
89       (#) Notifications can be deactivated using
90           HAL_CAN_DeactivateNotification() function.
91 
92       (#) Special care should be taken for CAN_IT_RX_FIFO0_MSG_PENDING and
93           CAN_IT_RX_FIFO1_MSG_PENDING notifications. These notifications trig
94           the callbacks HAL_CAN_RxFIFO0MsgPendingCallback() and
95           HAL_CAN_RxFIFO1MsgPendingCallback(). User has two possible options
96           here.
97             (++) Directly get the Rx message in the callback, using
98                  HAL_CAN_GetRxMessage().
99             (++) Or deactivate the notification in the callback without
100                  getting the Rx message. The Rx message can then be got later
101                  using HAL_CAN_GetRxMessage(). Once the Rx message have been
102                  read, the notification can be activated again.
103 
104 
105       *** Sleep mode ***
106       ==================
107     [..]
108       (#) The CAN peripheral can be put in sleep mode (low power), using
109           HAL_CAN_RequestSleep(). The sleep mode will be entered as soon as the
110           current CAN activity (transmission or reception of a CAN frame) will
111           be completed.
112 
113       (#) A notification can be activated to be informed when the sleep mode
114           will be entered.
115 
116       (#) It can be checked if the sleep mode is entered using
117           HAL_CAN_IsSleepActive().
118           Note that the CAN state (accessible from the API HAL_CAN_GetState())
119           is HAL_CAN_STATE_SLEEP_PENDING as soon as the sleep mode request is
120           submitted (the sleep mode is not yet entered), and become
121           HAL_CAN_STATE_SLEEP_ACTIVE when the sleep mode is effective.
122 
123       (#) The wake-up from sleep mode can be trigged by two ways:
124             (++) Using HAL_CAN_WakeUp(). When returning from this function,
125                  the sleep mode is exited (if return status is HAL_OK).
126             (++) When a start of Rx CAN frame is detected by the CAN peripheral,
127                  if automatic wake up mode is enabled.
128 
129   *** Callback registration ***
130   =============================================
131 
132   The compilation define  USE_HAL_CAN_REGISTER_CALLBACKS when set to 1
133   allows the user to configure dynamically the driver callbacks.
134   Use Function @ref HAL_CAN_RegisterCallback() to register an interrupt callback.
135 
136   Function @ref HAL_CAN_RegisterCallback() allows to register following callbacks:
137     (+) TxMailbox0CompleteCallback   : Tx Mailbox 0 Complete Callback.
138     (+) TxMailbox1CompleteCallback   : Tx Mailbox 1 Complete Callback.
139     (+) TxMailbox2CompleteCallback   : Tx Mailbox 2 Complete Callback.
140     (+) TxMailbox0AbortCallback      : Tx Mailbox 0 Abort Callback.
141     (+) TxMailbox1AbortCallback      : Tx Mailbox 1 Abort Callback.
142     (+) TxMailbox2AbortCallback      : Tx Mailbox 2 Abort Callback.
143     (+) RxFifo0MsgPendingCallback    : Rx Fifo 0 Message Pending Callback.
144     (+) RxFifo0FullCallback          : Rx Fifo 0 Full Callback.
145     (+) RxFifo1MsgPendingCallback    : Rx Fifo 1 Message Pending Callback.
146     (+) RxFifo1FullCallback          : Rx Fifo 1 Full Callback.
147     (+) SleepCallback                : Sleep Callback.
148     (+) WakeUpFromRxMsgCallback      : Wake Up From Rx Message Callback.
149     (+) ErrorCallback                : Error Callback.
150     (+) MspInitCallback              : CAN MspInit.
151     (+) MspDeInitCallback            : CAN MspDeInit.
152   This function takes as parameters the HAL peripheral handle, the Callback ID
153   and a pointer to the user callback function.
154 
155   Use function @ref HAL_CAN_UnRegisterCallback() to reset a callback to the default
156   weak function.
157   @ref HAL_CAN_UnRegisterCallback takes as parameters the HAL peripheral handle,
158   and the Callback ID.
159   This function allows to reset following callbacks:
160     (+) TxMailbox0CompleteCallback   : Tx Mailbox 0 Complete Callback.
161     (+) TxMailbox1CompleteCallback   : Tx Mailbox 1 Complete Callback.
162     (+) TxMailbox2CompleteCallback   : Tx Mailbox 2 Complete Callback.
163     (+) TxMailbox0AbortCallback      : Tx Mailbox 0 Abort Callback.
164     (+) TxMailbox1AbortCallback      : Tx Mailbox 1 Abort Callback.
165     (+) TxMailbox2AbortCallback      : Tx Mailbox 2 Abort Callback.
166     (+) RxFifo0MsgPendingCallback    : Rx Fifo 0 Message Pending Callback.
167     (+) RxFifo0FullCallback          : Rx Fifo 0 Full Callback.
168     (+) RxFifo1MsgPendingCallback    : Rx Fifo 1 Message Pending Callback.
169     (+) RxFifo1FullCallback          : Rx Fifo 1 Full Callback.
170     (+) SleepCallback                : Sleep Callback.
171     (+) WakeUpFromRxMsgCallback      : Wake Up From Rx Message Callback.
172     (+) ErrorCallback                : Error Callback.
173     (+) MspInitCallback              : CAN MspInit.
174     (+) MspDeInitCallback            : CAN MspDeInit.
175 
176   By default, after the @ref HAL_CAN_Init() and when the state is HAL_CAN_STATE_RESET,
177   all callbacks are set to the corresponding weak functions:
178   example @ref HAL_CAN_ErrorCallback().
179   Exception done for MspInit and MspDeInit functions that are
180   reset to the legacy weak function in the @ref HAL_CAN_Init()/ @ref HAL_CAN_DeInit() only when
181   these callbacks are null (not registered beforehand).
182   if not, MspInit or MspDeInit are not null, the @ref HAL_CAN_Init()/ @ref HAL_CAN_DeInit()
183   keep and use the user MspInit/MspDeInit callbacks (registered beforehand)
184 
185   Callbacks can be registered/unregistered in HAL_CAN_STATE_READY state only.
186   Exception done MspInit/MspDeInit that can be registered/unregistered
187   in HAL_CAN_STATE_READY or HAL_CAN_STATE_RESET state,
188   thus registered (user) MspInit/DeInit callbacks can be used during the Init/DeInit.
189   In that case first register the MspInit/MspDeInit user callbacks
190   using @ref HAL_CAN_RegisterCallback() before calling @ref HAL_CAN_DeInit()
191   or @ref HAL_CAN_Init() function.
192 
193   When The compilation define USE_HAL_CAN_REGISTER_CALLBACKS is set to 0 or
194   not defined, the callback registration feature is not available and all callbacks
195   are set to the corresponding weak functions.
196 
197   @endverbatim
198   ******************************************************************************
199   * @attention
200   *
201   * <h2><center>&copy; COPYRIGHT(c) 2017 STMicroelectronics</center></h2>
202   *
203   * Redistribution and use in source and binary forms, with or without modification,
204   * are permitted provided that the following conditions are met:
205   *   1. Redistributions of source code must retain the above copyright notice,
206   *      this list of conditions and the following disclaimer.
207   *   2. Redistributions in binary form must reproduce the above copyright notice,
208   *      this list of conditions and the following disclaimer in the documentation
209   *      and/or other materials provided with the distribution.
210   *   3. Neither the name of STMicroelectronics nor the names of its contributors
211   *      may be used to endorse or promote products derived from this software
212   *      without specific prior written permission.
213   *
214   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
215   * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
216   * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
217   * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
218   * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
219   * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
220   * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
221   * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
222   * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
223   * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
224   *
225   ******************************************************************************
226   */
227 
228 /* Includes ------------------------------------------------------------------*/
229 #include "stm32l4xx_hal.h"
230 
231 /** @addtogroup STM32L4xx_HAL_Driver
232   * @{
233   */
234 
235 #if defined(CAN1)
236 
237 /** @defgroup CAN CAN
238   * @brief CAN driver modules
239   * @{
240   */
241 
242 #ifdef HAL_CAN_MODULE_ENABLED
243 
244 #ifdef HAL_CAN_LEGACY_MODULE_ENABLED
245   #error "The CAN driver cannot be used with its legacy, Please enable only one CAN module at once"
246 #endif
247 
248 /* Private typedef -----------------------------------------------------------*/
249 /* Private define ------------------------------------------------------------*/
250 /** @defgroup CAN_Private_Constants CAN Private Constants
251   * @{
252   */
253 #define CAN_TIMEOUT_VALUE 10U
254 /**
255   * @}
256   */
257 /* Private macro -------------------------------------------------------------*/
258 /* Private variables ---------------------------------------------------------*/
259 /* Private function prototypes -----------------------------------------------*/
260 /* Exported functions --------------------------------------------------------*/
261 
262 /** @defgroup CAN_Exported_Functions CAN Exported Functions
263   * @{
264   */
265 
266 /** @defgroup CAN_Exported_Functions_Group1 Initialization and de-initialization functions
267  *  @brief    Initialization and Configuration functions
268  *
269 @verbatim
270   ==============================================================================
271               ##### Initialization and de-initialization functions #####
272   ==============================================================================
273     [..]  This section provides functions allowing to:
274       (+) HAL_CAN_Init                       : Initialize and configure the CAN.
275       (+) HAL_CAN_DeInit                     : De-initialize the CAN.
276       (+) HAL_CAN_MspInit                    : Initialize the CAN MSP.
277       (+) HAL_CAN_MspDeInit                  : DeInitialize the CAN MSP.
278 
279 @endverbatim
280   * @{
281   */
282 
283 /**
284   * @brief  Initializes the CAN peripheral according to the specified
285   *         parameters in the CAN_InitStruct.
286   * @param  hcan pointer to a CAN_HandleTypeDef structure that contains
287   *         the configuration information for the specified CAN.
288   * @retval HAL status
289   */
HAL_CAN_Init(CAN_HandleTypeDef * hcan)290 HAL_StatusTypeDef HAL_CAN_Init(CAN_HandleTypeDef *hcan)
291 {
292   uint32_t tickstart;
293 
294   /* Check CAN handle */
295   if (hcan == NULL)
296   {
297     return HAL_ERROR;
298   }
299 
300   /* Check the parameters */
301   assert_param(IS_CAN_ALL_INSTANCE(hcan->Instance));
302   assert_param(IS_FUNCTIONAL_STATE(hcan->Init.TimeTriggeredMode));
303   assert_param(IS_FUNCTIONAL_STATE(hcan->Init.AutoBusOff));
304   assert_param(IS_FUNCTIONAL_STATE(hcan->Init.AutoWakeUp));
305   assert_param(IS_FUNCTIONAL_STATE(hcan->Init.AutoRetransmission));
306   assert_param(IS_FUNCTIONAL_STATE(hcan->Init.ReceiveFifoLocked));
307   assert_param(IS_FUNCTIONAL_STATE(hcan->Init.TransmitFifoPriority));
308   assert_param(IS_CAN_MODE(hcan->Init.Mode));
309   assert_param(IS_CAN_SJW(hcan->Init.SyncJumpWidth));
310   assert_param(IS_CAN_BS1(hcan->Init.TimeSeg1));
311   assert_param(IS_CAN_BS2(hcan->Init.TimeSeg2));
312   assert_param(IS_CAN_PRESCALER(hcan->Init.Prescaler));
313 
314 #if USE_HAL_CAN_REGISTER_CALLBACKS == 1
315   if (hcan->State == HAL_CAN_STATE_RESET)
316   {
317     /* Reset callbacks to legacy functions */
318     hcan->RxFifo0MsgPendingCallback  =  HAL_CAN_RxFifo0MsgPendingCallback;  /* Legacy weak RxFifo0MsgPendingCallback  */
319     hcan->RxFifo0FullCallback        =  HAL_CAN_RxFifo0FullCallback;        /* Legacy weak RxFifo0FullCallback        */
320     hcan->RxFifo1MsgPendingCallback  =  HAL_CAN_RxFifo1MsgPendingCallback;  /* Legacy weak RxFifo1MsgPendingCallback  */
321     hcan->RxFifo1FullCallback        =  HAL_CAN_RxFifo1FullCallback;        /* Legacy weak RxFifo1FullCallback        */
322     hcan->TxMailbox0CompleteCallback =  HAL_CAN_TxMailbox0CompleteCallback; /* Legacy weak TxMailbox0CompleteCallback */
323     hcan->TxMailbox1CompleteCallback =  HAL_CAN_TxMailbox1CompleteCallback; /* Legacy weak TxMailbox1CompleteCallback */
324     hcan->TxMailbox2CompleteCallback =  HAL_CAN_TxMailbox2CompleteCallback; /* Legacy weak TxMailbox2CompleteCallback */
325     hcan->TxMailbox0AbortCallback    =  HAL_CAN_TxMailbox0AbortCallback;    /* Legacy weak TxMailbox0AbortCallback    */
326     hcan->TxMailbox1AbortCallback    =  HAL_CAN_TxMailbox1AbortCallback;    /* Legacy weak TxMailbox1AbortCallback    */
327     hcan->TxMailbox2AbortCallback    =  HAL_CAN_TxMailbox2AbortCallback;    /* Legacy weak TxMailbox2AbortCallback    */
328     hcan->SleepCallback              =  HAL_CAN_SleepCallback;              /* Legacy weak SleepCallback              */
329     hcan->WakeUpFromRxMsgCallback    =  HAL_CAN_WakeUpFromRxMsgCallback;    /* Legacy weak WakeUpFromRxMsgCallback    */
330     hcan->ErrorCallback              =  HAL_CAN_ErrorCallback;              /* Legacy weak ErrorCallback              */
331 
332     if (hcan->MspInitCallback == NULL)
333     {
334       hcan->MspInitCallback = HAL_CAN_MspInit; /* Legacy weak MspInit */
335     }
336 
337     /* Init the low level hardware: CLOCK, NVIC */
338     hcan->MspInitCallback(hcan);
339   }
340 
341 #else
342   if (hcan->State == HAL_CAN_STATE_RESET)
343   {
344     /* Init the low level hardware: CLOCK, NVIC */
345     HAL_CAN_MspInit(hcan);
346   }
347 #endif /* (USE_HAL_CAN_REGISTER_CALLBACKS) */
348 
349   /* Exit from sleep mode */
350   CLEAR_BIT(hcan->Instance->MCR, CAN_MCR_SLEEP);
351 
352   /* Get tick */
353   tickstart = HAL_GetTick();
354 
355   /* Check Sleep mode leave acknowledge */
356   while ((hcan->Instance->MSR & CAN_MSR_SLAK) != 0U)
357   {
358     if ((HAL_GetTick() - tickstart) > CAN_TIMEOUT_VALUE)
359     {
360       /* Update error code */
361       hcan->ErrorCode |= HAL_CAN_ERROR_TIMEOUT;
362 
363       /* Change CAN state */
364       hcan->State = HAL_CAN_STATE_ERROR;
365 
366       return HAL_ERROR;
367     }
368   }
369 
370   /* Request initialisation */
371   SET_BIT(hcan->Instance->MCR, CAN_MCR_INRQ);
372 
373   /* Get tick */
374   tickstart = HAL_GetTick();
375 
376   /* Wait initialisation acknowledge */
377   while ((hcan->Instance->MSR & CAN_MSR_INAK) == 0U)
378   {
379     if ((HAL_GetTick() - tickstart) > CAN_TIMEOUT_VALUE)
380     {
381       /* Update error code */
382       hcan->ErrorCode |= HAL_CAN_ERROR_TIMEOUT;
383 
384       /* Change CAN state */
385       hcan->State = HAL_CAN_STATE_ERROR;
386 
387       return HAL_ERROR;
388     }
389   }
390 
391   /* Set the time triggered communication mode */
392   if (hcan->Init.TimeTriggeredMode == ENABLE)
393   {
394     SET_BIT(hcan->Instance->MCR, CAN_MCR_TTCM);
395   }
396   else
397   {
398     CLEAR_BIT(hcan->Instance->MCR, CAN_MCR_TTCM);
399   }
400 
401   /* Set the automatic bus-off management */
402   if (hcan->Init.AutoBusOff == ENABLE)
403   {
404     SET_BIT(hcan->Instance->MCR, CAN_MCR_ABOM);
405   }
406   else
407   {
408     CLEAR_BIT(hcan->Instance->MCR, CAN_MCR_ABOM);
409   }
410 
411   /* Set the automatic wake-up mode */
412   if (hcan->Init.AutoWakeUp == ENABLE)
413   {
414     SET_BIT(hcan->Instance->MCR, CAN_MCR_AWUM);
415   }
416   else
417   {
418     CLEAR_BIT(hcan->Instance->MCR, CAN_MCR_AWUM);
419   }
420 
421   /* Set the automatic retransmission */
422   if (hcan->Init.AutoRetransmission == ENABLE)
423   {
424     CLEAR_BIT(hcan->Instance->MCR, CAN_MCR_NART);
425   }
426   else
427   {
428     SET_BIT(hcan->Instance->MCR, CAN_MCR_NART);
429   }
430 
431   /* Set the receive FIFO locked mode */
432   if (hcan->Init.ReceiveFifoLocked == ENABLE)
433   {
434     SET_BIT(hcan->Instance->MCR, CAN_MCR_RFLM);
435   }
436   else
437   {
438     CLEAR_BIT(hcan->Instance->MCR, CAN_MCR_RFLM);
439   }
440 
441   /* Set the transmit FIFO priority */
442   if (hcan->Init.TransmitFifoPriority == ENABLE)
443   {
444     SET_BIT(hcan->Instance->MCR, CAN_MCR_TXFP);
445   }
446   else
447   {
448     CLEAR_BIT(hcan->Instance->MCR, CAN_MCR_TXFP);
449   }
450 
451   /* Set the bit timing register */
452   WRITE_REG(hcan->Instance->BTR, (uint32_t)(hcan->Init.Mode           |
453                                             hcan->Init.SyncJumpWidth  |
454                                             hcan->Init.TimeSeg1       |
455                                             hcan->Init.TimeSeg2       |
456                                             (hcan->Init.Prescaler - 1U)));
457 
458   /* Initialize the error code */
459   hcan->ErrorCode = HAL_CAN_ERROR_NONE;
460 
461   /* Initialize the CAN state */
462   hcan->State = HAL_CAN_STATE_READY;
463 
464   /* Return function status */
465   return HAL_OK;
466 }
467 
468 /**
469   * @brief  Deinitializes the CAN peripheral registers to their default
470   *         reset values.
471   * @param  hcan pointer to a CAN_HandleTypeDef structure that contains
472   *         the configuration information for the specified CAN.
473   * @retval HAL status
474   */
HAL_CAN_DeInit(CAN_HandleTypeDef * hcan)475 HAL_StatusTypeDef HAL_CAN_DeInit(CAN_HandleTypeDef *hcan)
476 {
477   /* Check CAN handle */
478   if (hcan == NULL)
479   {
480     return HAL_ERROR;
481   }
482 
483   /* Check the parameters */
484   assert_param(IS_CAN_ALL_INSTANCE(hcan->Instance));
485 
486   /* Stop the CAN module */
487   (void)HAL_CAN_Stop(hcan);
488 
489 #if USE_HAL_CAN_REGISTER_CALLBACKS == 1
490   if (hcan->MspDeInitCallback == NULL)
491   {
492     hcan->MspDeInitCallback = HAL_CAN_MspDeInit; /* Legacy weak MspDeInit */
493   }
494 
495   /* DeInit the low level hardware: CLOCK, NVIC */
496   hcan->MspDeInitCallback(hcan);
497 
498 #else
499   /* DeInit the low level hardware: CLOCK, NVIC */
500   HAL_CAN_MspDeInit(hcan);
501 #endif /* (USE_HAL_CAN_REGISTER_CALLBACKS) */
502 
503   /* Reset the CAN peripheral */
504   SET_BIT(hcan->Instance->MCR, CAN_MCR_RESET);
505 
506   /* Reset the CAN ErrorCode */
507   hcan->ErrorCode = HAL_CAN_ERROR_NONE;
508 
509   /* Change CAN state */
510   hcan->State = HAL_CAN_STATE_RESET;
511 
512   /* Return function status */
513   return HAL_OK;
514 }
515 
516 /**
517   * @brief  Initializes the CAN MSP.
518   * @param  hcan pointer to a CAN_HandleTypeDef structure that contains
519   *         the configuration information for the specified CAN.
520   * @retval None
521   */
HAL_CAN_MspInit(CAN_HandleTypeDef * hcan)522 __weak void HAL_CAN_MspInit(CAN_HandleTypeDef *hcan)
523 {
524   /* Prevent unused argument(s) compilation warning */
525   UNUSED(hcan);
526 
527   /* NOTE : This function Should not be modified, when the callback is needed,
528             the HAL_CAN_MspInit could be implemented in the user file
529    */
530 }
531 
532 /**
533   * @brief  DeInitializes the CAN MSP.
534   * @param  hcan pointer to a CAN_HandleTypeDef structure that contains
535   *         the configuration information for the specified CAN.
536   * @retval None
537   */
HAL_CAN_MspDeInit(CAN_HandleTypeDef * hcan)538 __weak void HAL_CAN_MspDeInit(CAN_HandleTypeDef *hcan)
539 {
540   /* Prevent unused argument(s) compilation warning */
541   UNUSED(hcan);
542 
543   /* NOTE : This function Should not be modified, when the callback is needed,
544             the HAL_CAN_MspDeInit could be implemented in the user file
545    */
546 }
547 
548 #if USE_HAL_CAN_REGISTER_CALLBACKS == 1
549 /**
550   * @brief  Register a CAN CallBack.
551   *         To be used instead of the weak predefined callback
552   * @param  hcan pointer to a CAN_HandleTypeDef structure that contains
553   *         the configuration information for CAN module
554   * @param  CallbackID ID of the callback to be registered
555   *         This parameter can be one of the following values:
556   *           @arg @ref HAL_CAN_TX_MAILBOX0_COMPLETE_CALLBACK_CB_ID Tx Mailbox 0 Complete callback ID
557   *           @arg @ref HAL_CAN_TX_MAILBOX1_COMPLETE_CALLBACK_CB_ID Tx Mailbox 1 Complete callback ID
558   *           @arg @ref HAL_CAN_TX_MAILBOX2_COMPLETE_CALLBACK_CB_ID Tx Mailbox 2 Complete callback ID
559   *           @arg @ref HAL_CAN_TX_MAILBOX0_ABORT_CALLBACK_CB_ID Tx Mailbox 0 Abort callback ID
560   *           @arg @ref HAL_CAN_TX_MAILBOX1_ABORT_CALLBACK_CB_ID Tx Mailbox 1 Abort callback ID
561   *           @arg @ref HAL_CAN_TX_MAILBOX2_ABORT_CALLBACK_CB_ID Tx Mailbox 2 Abort callback ID
562   *           @arg @ref HAL_CAN_RX_FIFO0_MSG_PENDING_CALLBACK_CB_ID Rx Fifo 0 message pending callback ID
563   *           @arg @ref HAL_CAN_RX_FIFO0_FULL_CALLBACK_CB_ID Rx Fifo 0 full callback ID
564   *           @arg @ref HAL_CAN_RX_FIFO1_MSGPENDING_CALLBACK_CB_ID Rx Fifo 1 message pending callback ID
565   *           @arg @ref HAL_CAN_RX_FIFO1_FULL_CALLBACK_CB_ID Rx Fifo 1 full callback ID
566   *           @arg @ref HAL_CAN_SLEEP_CALLBACK_CB_ID Sleep callback ID
567   *           @arg @ref HAL_CAN_WAKEUP_FROM_RX_MSG_CALLBACK_CB_ID Wake Up from Rx message callback ID
568   *           @arg @ref HAL_CAN_ERROR_CALLBACK_CB_ID Error callback ID
569   *           @arg @ref HAL_CAN_MSPINIT_CB_ID MspInit callback ID
570   *           @arg @ref HAL_CAN_MSPDEINIT_CB_ID MspDeInit callback ID
571   * @param  pCallback pointer to the Callback function
572   * @retval HAL status
573   */
HAL_CAN_RegisterCallback(CAN_HandleTypeDef * hcan,HAL_CAN_CallbackIDTypeDef CallbackID,void (* pCallback)(CAN_HandleTypeDef * _hcan))574 HAL_StatusTypeDef HAL_CAN_RegisterCallback(CAN_HandleTypeDef *hcan, HAL_CAN_CallbackIDTypeDef CallbackID, void (* pCallback)(CAN_HandleTypeDef *_hcan))
575 {
576   HAL_StatusTypeDef status = HAL_OK;
577 
578   if (pCallback == NULL)
579   {
580     /* Update the error code */
581     hcan->ErrorCode |= HAL_CAN_ERROR_INVALID_CALLBACK;
582 
583     return HAL_ERROR;
584   }
585 
586   if (hcan->State == HAL_CAN_STATE_READY)
587   {
588     switch (CallbackID)
589     {
590       case HAL_CAN_TX_MAILBOX0_COMPLETE_CB_ID :
591         hcan->TxMailbox0CompleteCallback = pCallback;
592         break;
593 
594       case HAL_CAN_TX_MAILBOX1_COMPLETE_CB_ID :
595         hcan->TxMailbox1CompleteCallback = pCallback;
596         break;
597 
598       case HAL_CAN_TX_MAILBOX2_COMPLETE_CB_ID :
599         hcan->TxMailbox2CompleteCallback = pCallback;
600         break;
601 
602       case HAL_CAN_TX_MAILBOX0_ABORT_CB_ID :
603         hcan->TxMailbox0AbortCallback = pCallback;
604         break;
605 
606       case HAL_CAN_TX_MAILBOX1_ABORT_CB_ID :
607         hcan->TxMailbox1AbortCallback = pCallback;
608         break;
609 
610       case HAL_CAN_TX_MAILBOX2_ABORT_CB_ID :
611         hcan->TxMailbox2AbortCallback = pCallback;
612         break;
613 
614       case HAL_CAN_RX_FIFO0_MSG_PENDING_CB_ID :
615         hcan->RxFifo0MsgPendingCallback = pCallback;
616         break;
617 
618       case HAL_CAN_RX_FIFO0_FULL_CB_ID :
619         hcan->RxFifo0FullCallback = pCallback;
620         break;
621 
622       case HAL_CAN_RX_FIFO1_MSG_PENDING_CB_ID :
623         hcan->RxFifo1MsgPendingCallback = pCallback;
624         break;
625 
626       case HAL_CAN_RX_FIFO1_FULL_CB_ID :
627         hcan->RxFifo1FullCallback = pCallback;
628         break;
629 
630       case HAL_CAN_SLEEP_CB_ID :
631         hcan->SleepCallback = pCallback;
632         break;
633 
634       case HAL_CAN_WAKEUP_FROM_RX_MSG_CB_ID :
635         hcan->WakeUpFromRxMsgCallback = pCallback;
636         break;
637 
638       case HAL_CAN_ERROR_CB_ID :
639         hcan->ErrorCallback = pCallback;
640         break;
641 
642       case HAL_CAN_MSPINIT_CB_ID :
643         hcan->MspInitCallback = pCallback;
644         break;
645 
646       case HAL_CAN_MSPDEINIT_CB_ID :
647         hcan->MspDeInitCallback = pCallback;
648         break;
649 
650       default :
651         /* Update the error code */
652         hcan->ErrorCode |= HAL_CAN_ERROR_INVALID_CALLBACK;
653 
654         /* Return error status */
655         status =  HAL_ERROR;
656         break;
657     }
658   }
659   else if (hcan->State == HAL_CAN_STATE_RESET)
660   {
661     switch (CallbackID)
662     {
663       case HAL_CAN_MSPINIT_CB_ID :
664         hcan->MspInitCallback = pCallback;
665         break;
666 
667       case HAL_CAN_MSPDEINIT_CB_ID :
668         hcan->MspDeInitCallback = pCallback;
669         break;
670 
671       default :
672         /* Update the error code */
673         hcan->ErrorCode |= HAL_CAN_ERROR_INVALID_CALLBACK;
674 
675         /* Return error status */
676         status =  HAL_ERROR;
677         break;
678     }
679   }
680   else
681   {
682     /* Update the error code */
683     hcan->ErrorCode |= HAL_CAN_ERROR_INVALID_CALLBACK;
684 
685     /* Return error status */
686     status =  HAL_ERROR;
687   }
688 
689   return status;
690 }
691 
692 /**
693   * @brief  Unregister a CAN CallBack.
694   *         CAN callabck is redirected to the weak predefined callback
695   * @param  hcan pointer to a CAN_HandleTypeDef structure that contains
696   *         the configuration information for CAN module
697   * @param  CallbackID ID of the callback to be unregistered
698   *         This parameter can be one of the following values:
699   *           @arg @ref HAL_CAN_TX_MAILBOX0_COMPLETE_CALLBACK_CB_ID Tx Mailbox 0 Complete callback ID
700   *           @arg @ref HAL_CAN_TX_MAILBOX1_COMPLETE_CALLBACK_CB_ID Tx Mailbox 1 Complete callback ID
701   *           @arg @ref HAL_CAN_TX_MAILBOX2_COMPLETE_CALLBACK_CB_ID Tx Mailbox 2 Complete callback ID
702   *           @arg @ref HAL_CAN_TX_MAILBOX0_ABORT_CALLBACK_CB_ID Tx Mailbox 0 Abort callback ID
703   *           @arg @ref HAL_CAN_TX_MAILBOX1_ABORT_CALLBACK_CB_ID Tx Mailbox 1 Abort callback ID
704   *           @arg @ref HAL_CAN_TX_MAILBOX2_ABORT_CALLBACK_CB_ID Tx Mailbox 2 Abort callback ID
705   *           @arg @ref HAL_CAN_RX_FIFO0_MSG_PENDING_CALLBACK_CB_ID Rx Fifo 0 message pending callback ID
706   *           @arg @ref HAL_CAN_RX_FIFO0_FULL_CALLBACK_CB_ID Rx Fifo 0 full callback ID
707   *           @arg @ref HAL_CAN_RX_FIFO1_MSGPENDING_CALLBACK_CB_ID Rx Fifo 1 message pending callback ID
708   *           @arg @ref HAL_CAN_RX_FIFO1_FULL_CALLBACK_CB_ID Rx Fifo 1 full callback ID
709   *           @arg @ref HAL_CAN_SLEEP_CALLBACK_CB_ID Sleep callback ID
710   *           @arg @ref HAL_CAN_WAKEUP_FROM_RX_MSG_CALLBACK_CB_ID Wake Up from Rx message callback ID
711   *           @arg @ref HAL_CAN_ERROR_CALLBACK_CB_ID Error callback ID
712   *           @arg @ref HAL_CAN_MSPINIT_CB_ID MspInit callback ID
713   *           @arg @ref HAL_CAN_MSPDEINIT_CB_ID MspDeInit callback ID
714   * @retval HAL status
715   */
HAL_CAN_UnRegisterCallback(CAN_HandleTypeDef * hcan,HAL_CAN_CallbackIDTypeDef CallbackID)716 HAL_StatusTypeDef HAL_CAN_UnRegisterCallback(CAN_HandleTypeDef *hcan, HAL_CAN_CallbackIDTypeDef CallbackID)
717 {
718   HAL_StatusTypeDef status = HAL_OK;
719 
720   if (hcan->State == HAL_CAN_STATE_READY)
721   {
722     switch (CallbackID)
723     {
724       case HAL_CAN_TX_MAILBOX0_COMPLETE_CB_ID :
725         hcan->TxMailbox0CompleteCallback = HAL_CAN_TxMailbox0CompleteCallback;
726         break;
727 
728       case HAL_CAN_TX_MAILBOX1_COMPLETE_CB_ID :
729         hcan->TxMailbox1CompleteCallback = HAL_CAN_TxMailbox1CompleteCallback;
730         break;
731 
732       case HAL_CAN_TX_MAILBOX2_COMPLETE_CB_ID :
733         hcan->TxMailbox2CompleteCallback = HAL_CAN_TxMailbox2CompleteCallback;
734         break;
735 
736       case HAL_CAN_TX_MAILBOX0_ABORT_CB_ID :
737         hcan->TxMailbox0AbortCallback = HAL_CAN_TxMailbox0AbortCallback;
738         break;
739 
740       case HAL_CAN_TX_MAILBOX1_ABORT_CB_ID :
741         hcan->TxMailbox1AbortCallback = HAL_CAN_TxMailbox1AbortCallback;
742         break;
743 
744       case HAL_CAN_TX_MAILBOX2_ABORT_CB_ID :
745         hcan->TxMailbox2AbortCallback = HAL_CAN_TxMailbox2AbortCallback;
746         break;
747 
748       case HAL_CAN_RX_FIFO0_MSG_PENDING_CB_ID :
749         hcan->RxFifo0MsgPendingCallback = HAL_CAN_RxFifo0MsgPendingCallback;
750         break;
751 
752       case HAL_CAN_RX_FIFO0_FULL_CB_ID :
753         hcan->RxFifo0FullCallback = HAL_CAN_RxFifo0FullCallback;
754         break;
755 
756       case HAL_CAN_RX_FIFO1_MSG_PENDING_CB_ID :
757         hcan->RxFifo1MsgPendingCallback = HAL_CAN_RxFifo1MsgPendingCallback;
758         break;
759 
760       case HAL_CAN_RX_FIFO1_FULL_CB_ID :
761         hcan->RxFifo1FullCallback = HAL_CAN_RxFifo1FullCallback;
762         break;
763 
764       case HAL_CAN_SLEEP_CB_ID :
765         hcan->SleepCallback = HAL_CAN_SleepCallback;
766         break;
767 
768       case HAL_CAN_WAKEUP_FROM_RX_MSG_CB_ID :
769         hcan->WakeUpFromRxMsgCallback = HAL_CAN_WakeUpFromRxMsgCallback;
770         break;
771 
772       case HAL_CAN_ERROR_CB_ID :
773         hcan->ErrorCallback = HAL_CAN_ErrorCallback;
774         break;
775 
776       case HAL_CAN_MSPINIT_CB_ID :
777         hcan->MspInitCallback = HAL_CAN_MspInit;
778         break;
779 
780       case HAL_CAN_MSPDEINIT_CB_ID :
781         hcan->MspDeInitCallback = HAL_CAN_MspDeInit;
782         break;
783 
784       default :
785         /* Update the error code */
786         hcan->ErrorCode |= HAL_CAN_ERROR_INVALID_CALLBACK;
787 
788         /* Return error status */
789         status =  HAL_ERROR;
790         break;
791     }
792   }
793   else if (hcan->State == HAL_CAN_STATE_RESET)
794   {
795     switch (CallbackID)
796     {
797       case HAL_CAN_MSPINIT_CB_ID :
798         hcan->MspInitCallback = HAL_CAN_MspInit;
799         break;
800 
801       case HAL_CAN_MSPDEINIT_CB_ID :
802         hcan->MspDeInitCallback = HAL_CAN_MspDeInit;
803         break;
804 
805       default :
806         /* Update the error code */
807         hcan->ErrorCode |= HAL_CAN_ERROR_INVALID_CALLBACK;
808 
809         /* Return error status */
810         status =  HAL_ERROR;
811         break;
812     }
813   }
814   else
815   {
816     /* Update the error code */
817     hcan->ErrorCode |= HAL_CAN_ERROR_INVALID_CALLBACK;
818 
819     /* Return error status */
820     status =  HAL_ERROR;
821   }
822 
823   return status;
824 }
825 #endif /* USE_HAL_CAN_REGISTER_CALLBACKS */
826 
827 /**
828   * @}
829   */
830 
831 /** @defgroup CAN_Exported_Functions_Group2 Configuration functions
832  *  @brief    Configuration functions.
833  *
834 @verbatim
835   ==============================================================================
836               ##### Configuration functions #####
837   ==============================================================================
838     [..]  This section provides functions allowing to:
839       (+) HAL_CAN_ConfigFilter            : Configure the CAN reception filters
840 
841 @endverbatim
842   * @{
843   */
844 
845 /**
846   * @brief  Configures the CAN reception filter according to the specified
847   *         parameters in the CAN_FilterInitStruct.
848   * @param  hcan pointer to a CAN_HandleTypeDef structure that contains
849   *         the configuration information for the specified CAN.
850   * @param  sFilterConfig pointer to a CAN_FilterTypeDef structure that
851   *         contains the filter configuration information.
852   * @retval None
853   */
HAL_CAN_ConfigFilter(CAN_HandleTypeDef * hcan,CAN_FilterTypeDef * sFilterConfig)854 HAL_StatusTypeDef HAL_CAN_ConfigFilter(CAN_HandleTypeDef *hcan, CAN_FilterTypeDef *sFilterConfig)
855 {
856   uint32_t filternbrbitpos;
857   CAN_TypeDef *can_ip = hcan->Instance;
858   HAL_CAN_StateTypeDef state = hcan->State;
859 
860   if ((state == HAL_CAN_STATE_READY) ||
861       (state == HAL_CAN_STATE_LISTENING))
862   {
863     /* Check the parameters */
864     assert_param(IS_CAN_FILTER_ID_HALFWORD(sFilterConfig->FilterIdHigh));
865     assert_param(IS_CAN_FILTER_ID_HALFWORD(sFilterConfig->FilterIdLow));
866     assert_param(IS_CAN_FILTER_ID_HALFWORD(sFilterConfig->FilterMaskIdHigh));
867     assert_param(IS_CAN_FILTER_ID_HALFWORD(sFilterConfig->FilterMaskIdLow));
868     assert_param(IS_CAN_FILTER_MODE(sFilterConfig->FilterMode));
869     assert_param(IS_CAN_FILTER_SCALE(sFilterConfig->FilterScale));
870     assert_param(IS_CAN_FILTER_FIFO(sFilterConfig->FilterFIFOAssignment));
871     assert_param(IS_CAN_FILTER_ACTIVATION(sFilterConfig->FilterActivation));
872 
873 #if   defined(CAN2)
874     /* CAN1 and CAN2 are dual instances with 28 common filters banks */
875     /* Select master instance to access the filter banks */
876     can_ip = CAN1;
877 
878     /* Check the parameters */
879     assert_param(IS_CAN_FILTER_BANK_DUAL(sFilterConfig->FilterBank));
880     assert_param(IS_CAN_FILTER_BANK_DUAL(sFilterConfig->SlaveStartFilterBank));
881 #else
882     /* CAN1 is single instance with 14 dedicated filters banks */
883 
884     /* Check the parameters */
885     assert_param(IS_CAN_FILTER_BANK_SINGLE(sFilterConfig->FilterBank));
886 #endif
887 
888     /* Initialisation mode for the filter */
889     SET_BIT(can_ip->FMR, CAN_FMR_FINIT);
890 
891 #if   defined(CAN2)
892     /* Select the start filter number of CAN2 slave instance */
893     CLEAR_BIT(can_ip->FMR, CAN_FMR_CAN2SB);
894     SET_BIT(can_ip->FMR, sFilterConfig->SlaveStartFilterBank << CAN_FMR_CAN2SB_Pos);
895 
896 #endif
897     /* Convert filter number into bit position */
898     filternbrbitpos = (uint32_t)1 << (sFilterConfig->FilterBank & 0x1FU);
899 
900     /* Filter Deactivation */
901     CLEAR_BIT(can_ip->FA1R, filternbrbitpos);
902 
903     /* Filter Scale */
904     if (sFilterConfig->FilterScale == CAN_FILTERSCALE_16BIT)
905     {
906       /* 16-bit scale for the filter */
907       CLEAR_BIT(can_ip->FS1R, filternbrbitpos);
908 
909       /* First 16-bit identifier and First 16-bit mask */
910       /* Or First 16-bit identifier and Second 16-bit identifier */
911       can_ip->sFilterRegister[sFilterConfig->FilterBank].FR1 =
912         ((0x0000FFFFU & (uint32_t)sFilterConfig->FilterMaskIdLow) << 16U) |
913         (0x0000FFFFU & (uint32_t)sFilterConfig->FilterIdLow);
914 
915       /* Second 16-bit identifier and Second 16-bit mask */
916       /* Or Third 16-bit identifier and Fourth 16-bit identifier */
917       can_ip->sFilterRegister[sFilterConfig->FilterBank].FR2 =
918         ((0x0000FFFFU & (uint32_t)sFilterConfig->FilterMaskIdHigh) << 16U) |
919         (0x0000FFFFU & (uint32_t)sFilterConfig->FilterIdHigh);
920     }
921 
922     if (sFilterConfig->FilterScale == CAN_FILTERSCALE_32BIT)
923     {
924       /* 32-bit scale for the filter */
925       SET_BIT(can_ip->FS1R, filternbrbitpos);
926 
927       /* 32-bit identifier or First 32-bit identifier */
928       can_ip->sFilterRegister[sFilterConfig->FilterBank].FR1 =
929         ((0x0000FFFFU & (uint32_t)sFilterConfig->FilterIdHigh) << 16U) |
930         (0x0000FFFFU & (uint32_t)sFilterConfig->FilterIdLow);
931 
932       /* 32-bit mask or Second 32-bit identifier */
933       can_ip->sFilterRegister[sFilterConfig->FilterBank].FR2 =
934         ((0x0000FFFFU & (uint32_t)sFilterConfig->FilterMaskIdHigh) << 16U) |
935         (0x0000FFFFU & (uint32_t)sFilterConfig->FilterMaskIdLow);
936     }
937 
938     /* Filter Mode */
939     if (sFilterConfig->FilterMode == CAN_FILTERMODE_IDMASK)
940     {
941       /* Id/Mask mode for the filter*/
942       CLEAR_BIT(can_ip->FM1R, filternbrbitpos);
943     }
944     else /* CAN_FilterInitStruct->CAN_FilterMode == CAN_FilterMode_IdList */
945     {
946       /* Identifier list mode for the filter*/
947       SET_BIT(can_ip->FM1R, filternbrbitpos);
948     }
949 
950     /* Filter FIFO assignment */
951     if (sFilterConfig->FilterFIFOAssignment == CAN_FILTER_FIFO0)
952     {
953       /* FIFO 0 assignation for the filter */
954       CLEAR_BIT(can_ip->FFA1R, filternbrbitpos);
955     }
956     else
957     {
958       /* FIFO 1 assignation for the filter */
959       SET_BIT(can_ip->FFA1R, filternbrbitpos);
960     }
961 
962     /* Filter activation */
963     if (sFilterConfig->FilterActivation == CAN_FILTER_ENABLE)
964     {
965       SET_BIT(can_ip->FA1R, filternbrbitpos);
966     }
967 
968     /* Leave the initialisation mode for the filter */
969     CLEAR_BIT(can_ip->FMR, CAN_FMR_FINIT);
970 
971     /* Return function status */
972     return HAL_OK;
973   }
974   else
975   {
976     /* Update error code */
977     hcan->ErrorCode |= HAL_CAN_ERROR_NOT_INITIALIZED;
978 
979     return HAL_ERROR;
980   }
981 }
982 
983 /**
984   * @}
985   */
986 
987 /** @defgroup CAN_Exported_Functions_Group3 Control functions
988  *  @brief    Control functions
989  *
990 @verbatim
991   ==============================================================================
992                       ##### Control functions #####
993   ==============================================================================
994     [..]  This section provides functions allowing to:
995       (+) HAL_CAN_Start                    : Start the CAN module
996       (+) HAL_CAN_Stop                     : Stop the CAN module
997       (+) HAL_CAN_RequestSleep             : Request sleep mode entry.
998       (+) HAL_CAN_WakeUp                   : Wake up from sleep mode.
999       (+) HAL_CAN_IsSleepActive            : Check is sleep mode is active.
1000       (+) HAL_CAN_AddTxMessage             : Add a message to the Tx mailboxes
1001                                              and activate the corresponding
1002                                              transmission request
1003       (+) HAL_CAN_AbortTxRequest           : Abort transmission request
1004       (+) HAL_CAN_GetTxMailboxesFreeLevel  : Return Tx mailboxes free level
1005       (+) HAL_CAN_IsTxMessagePending       : Check if a transmission request is
1006                                              pending on the selected Tx mailbox
1007       (+) HAL_CAN_GetRxMessage             : Get a CAN frame from the Rx FIFO
1008       (+) HAL_CAN_GetRxFifoFillLevel       : Return Rx FIFO fill level
1009 
1010 @endverbatim
1011   * @{
1012   */
1013 
1014 /**
1015   * @brief  Start the CAN module.
1016   * @param  hcan pointer to an CAN_HandleTypeDef structure that contains
1017   *         the configuration information for the specified CAN.
1018   * @retval HAL status
1019   */
HAL_CAN_Start(CAN_HandleTypeDef * hcan)1020 HAL_StatusTypeDef HAL_CAN_Start(CAN_HandleTypeDef *hcan)
1021 {
1022   uint32_t tickstart;
1023 
1024   if (hcan->State == HAL_CAN_STATE_READY)
1025   {
1026     /* Change CAN peripheral state */
1027     hcan->State = HAL_CAN_STATE_LISTENING;
1028 
1029     /* Request leave initialisation */
1030     CLEAR_BIT(hcan->Instance->MCR, CAN_MCR_INRQ);
1031 
1032     /* Get tick */
1033     tickstart = HAL_GetTick();
1034 
1035     /* Wait the acknowledge */
1036     while ((hcan->Instance->MSR & CAN_MSR_INAK) != 0U)
1037     {
1038       /* Check for the Timeout */
1039       if ((HAL_GetTick() - tickstart) > CAN_TIMEOUT_VALUE)
1040       {
1041         /* Update error code */
1042         hcan->ErrorCode |= HAL_CAN_ERROR_TIMEOUT;
1043 
1044         /* Change CAN state */
1045         hcan->State = HAL_CAN_STATE_ERROR;
1046 
1047         return HAL_ERROR;
1048       }
1049     }
1050 
1051     /* Reset the CAN ErrorCode */
1052     hcan->ErrorCode = HAL_CAN_ERROR_NONE;
1053 
1054     /* Return function status */
1055     return HAL_OK;
1056   }
1057   else
1058   {
1059     /* Update error code */
1060     hcan->ErrorCode |= HAL_CAN_ERROR_NOT_READY;
1061 
1062     return HAL_ERROR;
1063   }
1064 }
1065 
1066 /**
1067   * @brief  Stop the CAN module and enable access to configuration registers.
1068   * @param  hcan pointer to an CAN_HandleTypeDef structure that contains
1069   *         the configuration information for the specified CAN.
1070   * @retval HAL status
1071   */
HAL_CAN_Stop(CAN_HandleTypeDef * hcan)1072 HAL_StatusTypeDef HAL_CAN_Stop(CAN_HandleTypeDef *hcan)
1073 {
1074   uint32_t tickstart;
1075 
1076   if (hcan->State == HAL_CAN_STATE_LISTENING)
1077   {
1078     /* Request initialisation */
1079     SET_BIT(hcan->Instance->MCR, CAN_MCR_INRQ);
1080 
1081     /* Get tick */
1082     tickstart = HAL_GetTick();
1083 
1084     /* Wait the acknowledge */
1085     while ((hcan->Instance->MSR & CAN_MSR_INAK) == 0U)
1086     {
1087       /* Check for the Timeout */
1088       if ((HAL_GetTick() - tickstart) > CAN_TIMEOUT_VALUE)
1089       {
1090         /* Update error code */
1091         hcan->ErrorCode |= HAL_CAN_ERROR_TIMEOUT;
1092 
1093         /* Change CAN state */
1094         hcan->State = HAL_CAN_STATE_ERROR;
1095 
1096         return HAL_ERROR;
1097       }
1098     }
1099 
1100     /* Exit from sleep mode */
1101     CLEAR_BIT(hcan->Instance->MCR, CAN_MCR_SLEEP);
1102 
1103     /* Change CAN peripheral state */
1104     hcan->State = HAL_CAN_STATE_READY;
1105 
1106     /* Return function status */
1107     return HAL_OK;
1108   }
1109   else
1110   {
1111     /* Update error code */
1112     hcan->ErrorCode |= HAL_CAN_ERROR_NOT_STARTED;
1113 
1114     return HAL_ERROR;
1115   }
1116 }
1117 
1118 /**
1119   * @brief  Request the sleep mode (low power) entry.
1120   *         When returning from this function, Sleep mode will be entered
1121   *         as soon as the current CAN activity (transmission or reception
1122   *         of a CAN frame) has been completed.
1123   * @param  hcan pointer to a CAN_HandleTypeDef structure that contains
1124   *         the configuration information for the specified CAN.
1125   * @retval HAL status.
1126   */
HAL_CAN_RequestSleep(CAN_HandleTypeDef * hcan)1127 HAL_StatusTypeDef HAL_CAN_RequestSleep(CAN_HandleTypeDef *hcan)
1128 {
1129   HAL_CAN_StateTypeDef state = hcan->State;
1130 
1131   if ((state == HAL_CAN_STATE_READY) ||
1132       (state == HAL_CAN_STATE_LISTENING))
1133   {
1134     /* Request Sleep mode */
1135     SET_BIT(hcan->Instance->MCR, CAN_MCR_SLEEP);
1136 
1137     /* Return function status */
1138     return HAL_OK;
1139   }
1140   else
1141   {
1142     /* Update error code */
1143     hcan->ErrorCode |= HAL_CAN_ERROR_NOT_INITIALIZED;
1144 
1145     /* Return function status */
1146     return HAL_ERROR;
1147   }
1148 }
1149 
1150 /**
1151   * @brief  Wake up from sleep mode.
1152   *         When returning with HAL_OK status from this function, Sleep mode
1153   *         is exited.
1154   * @param  hcan pointer to a CAN_HandleTypeDef structure that contains
1155   *         the configuration information for the specified CAN.
1156   * @retval HAL status.
1157   */
HAL_CAN_WakeUp(CAN_HandleTypeDef * hcan)1158 HAL_StatusTypeDef HAL_CAN_WakeUp(CAN_HandleTypeDef *hcan)
1159 {
1160   __IO uint32_t count = 0;
1161   uint32_t timeout = 1000000U;
1162   HAL_CAN_StateTypeDef state = hcan->State;
1163 
1164   if ((state == HAL_CAN_STATE_READY) ||
1165       (state == HAL_CAN_STATE_LISTENING))
1166   {
1167     /* Wake up request */
1168     CLEAR_BIT(hcan->Instance->MCR, CAN_MCR_SLEEP);
1169 
1170     /* Wait sleep mode is exited */
1171     do
1172     {
1173       /* Increment counter */
1174       count++;
1175 
1176       /* Check if timeout is reached */
1177       if (count > timeout)
1178       {
1179         /* Update error code */
1180         hcan->ErrorCode |= HAL_CAN_ERROR_TIMEOUT;
1181 
1182         return HAL_ERROR;
1183       }
1184     }
1185     while ((hcan->Instance->MSR & CAN_MSR_SLAK) != 0U);
1186 
1187     /* Return function status */
1188     return HAL_OK;
1189   }
1190   else
1191   {
1192     /* Update error code */
1193     hcan->ErrorCode |= HAL_CAN_ERROR_NOT_INITIALIZED;
1194 
1195     return HAL_ERROR;
1196   }
1197 }
1198 
1199 /**
1200   * @brief  Check is sleep mode is active.
1201   * @param  hcan pointer to a CAN_HandleTypeDef structure that contains
1202   *         the configuration information for the specified CAN.
1203   * @retval Status
1204   *          - 0 : Sleep mode is not active.
1205   *          - 1 : Sleep mode is active.
1206   */
HAL_CAN_IsSleepActive(CAN_HandleTypeDef * hcan)1207 uint32_t HAL_CAN_IsSleepActive(CAN_HandleTypeDef *hcan)
1208 {
1209   uint32_t status = 0U;
1210   HAL_CAN_StateTypeDef state = hcan->State;
1211 
1212   if ((state == HAL_CAN_STATE_READY) ||
1213       (state == HAL_CAN_STATE_LISTENING))
1214   {
1215     /* Check Sleep mode */
1216     if ((hcan->Instance->MSR & CAN_MSR_SLAK) != 0U)
1217     {
1218       status = 1U;
1219     }
1220   }
1221 
1222   /* Return function status */
1223   return status;
1224 }
1225 
1226 /**
1227   * @brief  Add a message to the first free Tx mailbox and activate the
1228   *         corresponding transmission request.
1229   * @param  hcan pointer to a CAN_HandleTypeDef structure that contains
1230   *         the configuration information for the specified CAN.
1231   * @param  pHeader pointer to a CAN_TxHeaderTypeDef structure.
1232   * @param  aData array containing the payload of the Tx frame.
1233   * @param  pTxMailbox pointer to a variable where the function will return
1234   *         the TxMailbox used to store the Tx message.
1235   *         This parameter can be a value of @arg CAN_Tx_Mailboxes.
1236   * @retval HAL status
1237   */
HAL_CAN_AddTxMessage(CAN_HandleTypeDef * hcan,CAN_TxHeaderTypeDef * pHeader,uint8_t aData[],uint32_t * pTxMailbox)1238 HAL_StatusTypeDef HAL_CAN_AddTxMessage(CAN_HandleTypeDef *hcan, CAN_TxHeaderTypeDef *pHeader, uint8_t aData[], uint32_t *pTxMailbox)
1239 {
1240   uint32_t transmitmailbox;
1241   HAL_CAN_StateTypeDef state = hcan->State;
1242   uint32_t tsr = READ_REG(hcan->Instance->TSR);
1243 
1244   /* Check the parameters */
1245   assert_param(IS_CAN_IDTYPE(pHeader->IDE));
1246   assert_param(IS_CAN_RTR(pHeader->RTR));
1247   assert_param(IS_CAN_DLC(pHeader->DLC));
1248   if (pHeader->IDE == CAN_ID_STD)
1249   {
1250     assert_param(IS_CAN_STDID(pHeader->StdId));
1251   }
1252   else
1253   {
1254     assert_param(IS_CAN_EXTID(pHeader->ExtId));
1255   }
1256   assert_param(IS_FUNCTIONAL_STATE(pHeader->TransmitGlobalTime));
1257 
1258   if ((state == HAL_CAN_STATE_READY) ||
1259       (state == HAL_CAN_STATE_LISTENING))
1260   {
1261     /* Check that all the Tx mailboxes are not full */
1262     if (((tsr & CAN_TSR_TME0) != 0U) ||
1263         ((tsr & CAN_TSR_TME1) != 0U) ||
1264         ((tsr & CAN_TSR_TME2) != 0U))
1265     {
1266       /* Select an empty transmit mailbox */
1267       transmitmailbox = (tsr & CAN_TSR_CODE) >> CAN_TSR_CODE_Pos;
1268 
1269       /* Check transmit mailbox value */
1270       if (transmitmailbox > 2U)
1271       {
1272         /* Update error code */
1273         hcan->ErrorCode |= HAL_CAN_ERROR_INTERNAL;
1274 
1275         return HAL_ERROR;
1276       }
1277 
1278       /* Store the Tx mailbox */
1279       *pTxMailbox = (uint32_t)1 << transmitmailbox;
1280 
1281       /* Set up the Id */
1282       if (pHeader->IDE == CAN_ID_STD)
1283       {
1284         hcan->Instance->sTxMailBox[transmitmailbox].TIR = ((pHeader->StdId << CAN_TI0R_STID_Pos) |
1285                                                            pHeader->RTR);
1286       }
1287       else
1288       {
1289         hcan->Instance->sTxMailBox[transmitmailbox].TIR = ((pHeader->ExtId << CAN_TI0R_EXID_Pos) |
1290                                                            pHeader->IDE |
1291                                                            pHeader->RTR);
1292       }
1293 
1294       /* Set up the DLC */
1295       hcan->Instance->sTxMailBox[transmitmailbox].TDTR = (pHeader->DLC);
1296 
1297       /* Set up the Transmit Global Time mode */
1298       if (pHeader->TransmitGlobalTime == ENABLE)
1299       {
1300         SET_BIT(hcan->Instance->sTxMailBox[transmitmailbox].TDTR, CAN_TDT0R_TGT);
1301       }
1302 
1303       /* Set up the data field */
1304       WRITE_REG(hcan->Instance->sTxMailBox[transmitmailbox].TDHR,
1305                 ((uint32_t)aData[7] << CAN_TDH0R_DATA7_Pos) |
1306                 ((uint32_t)aData[6] << CAN_TDH0R_DATA6_Pos) |
1307                 ((uint32_t)aData[5] << CAN_TDH0R_DATA5_Pos) |
1308                 ((uint32_t)aData[4] << CAN_TDH0R_DATA4_Pos));
1309       WRITE_REG(hcan->Instance->sTxMailBox[transmitmailbox].TDLR,
1310                 ((uint32_t)aData[3] << CAN_TDL0R_DATA3_Pos) |
1311                 ((uint32_t)aData[2] << CAN_TDL0R_DATA2_Pos) |
1312                 ((uint32_t)aData[1] << CAN_TDL0R_DATA1_Pos) |
1313                 ((uint32_t)aData[0] << CAN_TDL0R_DATA0_Pos));
1314 
1315       /* Request transmission */
1316       SET_BIT(hcan->Instance->sTxMailBox[transmitmailbox].TIR, CAN_TI0R_TXRQ);
1317 
1318       /* Return function status */
1319       return HAL_OK;
1320     }
1321     else
1322     {
1323       /* Update error code */
1324       hcan->ErrorCode |= HAL_CAN_ERROR_PARAM;
1325 
1326       return HAL_ERROR;
1327     }
1328   }
1329   else
1330   {
1331     /* Update error code */
1332     hcan->ErrorCode |= HAL_CAN_ERROR_NOT_INITIALIZED;
1333 
1334     return HAL_ERROR;
1335   }
1336 }
1337 
1338 /**
1339   * @brief  Abort transmission requests
1340   * @param  hcan pointer to an CAN_HandleTypeDef structure that contains
1341   *         the configuration information for the specified CAN.
1342   * @param  TxMailboxes List of the Tx Mailboxes to abort.
1343   *         This parameter can be any combination of @arg CAN_Tx_Mailboxes.
1344   * @retval HAL status
1345   */
HAL_CAN_AbortTxRequest(CAN_HandleTypeDef * hcan,uint32_t TxMailboxes)1346 HAL_StatusTypeDef HAL_CAN_AbortTxRequest(CAN_HandleTypeDef *hcan, uint32_t TxMailboxes)
1347 {
1348   HAL_CAN_StateTypeDef state = hcan->State;
1349 
1350   /* Check function parameters */
1351   assert_param(IS_CAN_TX_MAILBOX_LIST(TxMailboxes));
1352 
1353   if ((state == HAL_CAN_STATE_READY) ||
1354       (state == HAL_CAN_STATE_LISTENING))
1355   {
1356     /* Check Tx Mailbox 0 */
1357     if ((TxMailboxes & CAN_TX_MAILBOX0) != 0U)
1358     {
1359       /* Add cancellation request for Tx Mailbox 0 */
1360       SET_BIT(hcan->Instance->TSR, CAN_TSR_ABRQ0);
1361     }
1362 
1363     /* Check Tx Mailbox 1 */
1364     if ((TxMailboxes & CAN_TX_MAILBOX1) != 0U)
1365     {
1366       /* Add cancellation request for Tx Mailbox 1 */
1367       SET_BIT(hcan->Instance->TSR, CAN_TSR_ABRQ1);
1368     }
1369 
1370     /* Check Tx Mailbox 2 */
1371     if ((TxMailboxes & CAN_TX_MAILBOX2) != 0U)
1372     {
1373       /* Add cancellation request for Tx Mailbox 2 */
1374       SET_BIT(hcan->Instance->TSR, CAN_TSR_ABRQ2);
1375     }
1376 
1377     /* Return function status */
1378     return HAL_OK;
1379   }
1380   else
1381   {
1382     /* Update error code */
1383     hcan->ErrorCode |= HAL_CAN_ERROR_NOT_INITIALIZED;
1384 
1385     return HAL_ERROR;
1386   }
1387 }
1388 
1389 /**
1390   * @brief  Return Tx Mailboxes free level: number of free Tx Mailboxes.
1391   * @param  hcan pointer to a CAN_HandleTypeDef structure that contains
1392   *         the configuration information for the specified CAN.
1393   * @retval Number of free Tx Mailboxes.
1394   */
HAL_CAN_GetTxMailboxesFreeLevel(CAN_HandleTypeDef * hcan)1395 uint32_t HAL_CAN_GetTxMailboxesFreeLevel(CAN_HandleTypeDef *hcan)
1396 {
1397   uint32_t freelevel = 0U;
1398   HAL_CAN_StateTypeDef state = hcan->State;
1399 
1400   if ((state == HAL_CAN_STATE_READY) ||
1401       (state == HAL_CAN_STATE_LISTENING))
1402   {
1403     /* Check Tx Mailbox 0 status */
1404     if ((hcan->Instance->TSR & CAN_TSR_TME0) != 0U)
1405     {
1406       freelevel++;
1407     }
1408 
1409     /* Check Tx Mailbox 1 status */
1410     if ((hcan->Instance->TSR & CAN_TSR_TME1) != 0U)
1411     {
1412       freelevel++;
1413     }
1414 
1415     /* Check Tx Mailbox 2 status */
1416     if ((hcan->Instance->TSR & CAN_TSR_TME2) != 0U)
1417     {
1418       freelevel++;
1419     }
1420   }
1421 
1422   /* Return Tx Mailboxes free level */
1423   return freelevel;
1424 }
1425 
1426 /**
1427   * @brief  Check if a transmission request is pending on the selected Tx
1428   *         Mailboxes.
1429   * @param  hcan pointer to an CAN_HandleTypeDef structure that contains
1430   *         the configuration information for the specified CAN.
1431   * @param  TxMailboxes List of Tx Mailboxes to check.
1432   *         This parameter can be any combination of @arg CAN_Tx_Mailboxes.
1433   * @retval Status
1434   *          - 0 : No pending transmission request on any selected Tx Mailboxes.
1435   *          - 1 : Pending transmission request on at least one of the selected
1436   *                Tx Mailbox.
1437   */
HAL_CAN_IsTxMessagePending(CAN_HandleTypeDef * hcan,uint32_t TxMailboxes)1438 uint32_t HAL_CAN_IsTxMessagePending(CAN_HandleTypeDef *hcan, uint32_t TxMailboxes)
1439 {
1440   uint32_t status = 0U;
1441   HAL_CAN_StateTypeDef state = hcan->State;
1442 
1443   /* Check function parameters */
1444   assert_param(IS_CAN_TX_MAILBOX_LIST(TxMailboxes));
1445 
1446   if ((state == HAL_CAN_STATE_READY) ||
1447       (state == HAL_CAN_STATE_LISTENING))
1448   {
1449     /* Check pending transmission request on the selected Tx Mailboxes */
1450     if ((hcan->Instance->TSR & (TxMailboxes << CAN_TSR_TME0_Pos)) != (TxMailboxes << CAN_TSR_TME0_Pos))
1451     {
1452       status = 1U;
1453     }
1454   }
1455 
1456   /* Return status */
1457   return status;
1458 }
1459 
1460 /**
1461   * @brief  Return timestamp of Tx message sent, if time triggered communication
1462             mode is enabled.
1463   * @param  hcan pointer to a CAN_HandleTypeDef structure that contains
1464   *         the configuration information for the specified CAN.
1465   * @param  TxMailbox Tx Mailbox where the timestamp of message sent will be
1466   *         read.
1467   *         This parameter can be one value of @arg CAN_Tx_Mailboxes.
1468   * @retval Timestamp of message sent from Tx Mailbox.
1469   */
HAL_CAN_GetTxTimestamp(CAN_HandleTypeDef * hcan,uint32_t TxMailbox)1470 uint32_t HAL_CAN_GetTxTimestamp(CAN_HandleTypeDef *hcan, uint32_t TxMailbox)
1471 {
1472   uint32_t timestamp = 0U;
1473   uint32_t transmitmailbox;
1474   HAL_CAN_StateTypeDef state = hcan->State;
1475 
1476   /* Check function parameters */
1477   assert_param(IS_CAN_TX_MAILBOX(TxMailbox));
1478 
1479   if ((state == HAL_CAN_STATE_READY) ||
1480       (state == HAL_CAN_STATE_LISTENING))
1481   {
1482     /* Select the Tx mailbox */
1483     transmitmailbox = POSITION_VAL(TxMailbox);
1484 
1485     /* Get timestamp */
1486     timestamp = (hcan->Instance->sTxMailBox[transmitmailbox].TDTR & CAN_TDT0R_TIME) >> CAN_TDT0R_TIME_Pos;
1487   }
1488 
1489   /* Return the timestamp */
1490   return timestamp;
1491 }
1492 
1493 /**
1494   * @brief  Get an CAN frame from the Rx FIFO zone into the message RAM.
1495   * @param  hcan pointer to an CAN_HandleTypeDef structure that contains
1496   *         the configuration information for the specified CAN.
1497   * @param  RxFifo Fifo number of the received message to be read.
1498   *         This parameter can be a value of @arg CAN_receive_FIFO_number.
1499   * @param  pHeader pointer to a CAN_RxHeaderTypeDef structure where the header
1500   *         of the Rx frame will be stored.
1501   * @param  aData array where the payload of the Rx frame will be stored.
1502   * @retval HAL status
1503   */
HAL_CAN_GetRxMessage(CAN_HandleTypeDef * hcan,uint32_t RxFifo,CAN_RxHeaderTypeDef * pHeader,uint8_t aData[])1504 HAL_StatusTypeDef HAL_CAN_GetRxMessage(CAN_HandleTypeDef *hcan, uint32_t RxFifo, CAN_RxHeaderTypeDef *pHeader, uint8_t aData[])
1505 {
1506   HAL_CAN_StateTypeDef state = hcan->State;
1507 
1508   assert_param(IS_CAN_RX_FIFO(RxFifo));
1509 
1510   if ((state == HAL_CAN_STATE_READY) ||
1511       (state == HAL_CAN_STATE_LISTENING))
1512   {
1513     /* Check the Rx FIFO */
1514     if (RxFifo == CAN_RX_FIFO0) /* Rx element is assigned to Rx FIFO 0 */
1515     {
1516       /* Check that the Rx FIFO 0 is not empty */
1517       if ((hcan->Instance->RF0R & CAN_RF0R_FMP0) == 0U)
1518       {
1519         /* Update error code */
1520         hcan->ErrorCode |= HAL_CAN_ERROR_PARAM;
1521 
1522         return HAL_ERROR;
1523       }
1524     }
1525     else /* Rx element is assigned to Rx FIFO 1 */
1526     {
1527       /* Check that the Rx FIFO 1 is not empty */
1528       if ((hcan->Instance->RF1R & CAN_RF1R_FMP1) == 0U)
1529       {
1530         /* Update error code */
1531         hcan->ErrorCode |= HAL_CAN_ERROR_PARAM;
1532 
1533         return HAL_ERROR;
1534       }
1535     }
1536 
1537     /* Get the header */
1538     pHeader->IDE = CAN_RI0R_IDE & hcan->Instance->sFIFOMailBox[RxFifo].RIR;
1539     if (pHeader->IDE == CAN_ID_STD)
1540     {
1541       pHeader->StdId = (CAN_RI0R_STID & hcan->Instance->sFIFOMailBox[RxFifo].RIR) >> CAN_TI0R_STID_Pos;
1542     }
1543     else
1544     {
1545       pHeader->ExtId = ((CAN_RI0R_EXID | CAN_RI0R_STID) & hcan->Instance->sFIFOMailBox[RxFifo].RIR) >> CAN_RI0R_EXID_Pos;
1546     }
1547     pHeader->RTR = (CAN_RI0R_RTR & hcan->Instance->sFIFOMailBox[RxFifo].RIR) >> CAN_RI0R_RTR_Pos;
1548     pHeader->DLC = (CAN_RDT0R_DLC & hcan->Instance->sFIFOMailBox[RxFifo].RDTR) >> CAN_RDT0R_DLC_Pos;
1549     pHeader->FilterMatchIndex = (CAN_RDT0R_FMI & hcan->Instance->sFIFOMailBox[RxFifo].RDTR) >> CAN_RDT0R_FMI_Pos;
1550     pHeader->Timestamp = (CAN_RDT0R_TIME & hcan->Instance->sFIFOMailBox[RxFifo].RDTR) >> CAN_RDT0R_TIME_Pos;
1551 
1552     /* Get the data */
1553     aData[0] = (uint8_t)((CAN_RDL0R_DATA0 & hcan->Instance->sFIFOMailBox[RxFifo].RDLR) >> CAN_RDL0R_DATA0_Pos);
1554     aData[1] = (uint8_t)((CAN_RDL0R_DATA1 & hcan->Instance->sFIFOMailBox[RxFifo].RDLR) >> CAN_RDL0R_DATA1_Pos);
1555     aData[2] = (uint8_t)((CAN_RDL0R_DATA2 & hcan->Instance->sFIFOMailBox[RxFifo].RDLR) >> CAN_RDL0R_DATA2_Pos);
1556     aData[3] = (uint8_t)((CAN_RDL0R_DATA3 & hcan->Instance->sFIFOMailBox[RxFifo].RDLR) >> CAN_RDL0R_DATA3_Pos);
1557     aData[4] = (uint8_t)((CAN_RDH0R_DATA4 & hcan->Instance->sFIFOMailBox[RxFifo].RDHR) >> CAN_RDH0R_DATA4_Pos);
1558     aData[5] = (uint8_t)((CAN_RDH0R_DATA5 & hcan->Instance->sFIFOMailBox[RxFifo].RDHR) >> CAN_RDH0R_DATA5_Pos);
1559     aData[6] = (uint8_t)((CAN_RDH0R_DATA6 & hcan->Instance->sFIFOMailBox[RxFifo].RDHR) >> CAN_RDH0R_DATA6_Pos);
1560     aData[7] = (uint8_t)((CAN_RDH0R_DATA7 & hcan->Instance->sFIFOMailBox[RxFifo].RDHR) >> CAN_RDH0R_DATA7_Pos);
1561 
1562     /* Release the FIFO */
1563     if (RxFifo == CAN_RX_FIFO0) /* Rx element is assigned to Rx FIFO 0 */
1564     {
1565       /* Release RX FIFO 0 */
1566       SET_BIT(hcan->Instance->RF0R, CAN_RF0R_RFOM0);
1567     }
1568     else /* Rx element is assigned to Rx FIFO 1 */
1569     {
1570       /* Release RX FIFO 1 */
1571       SET_BIT(hcan->Instance->RF1R, CAN_RF1R_RFOM1);
1572     }
1573 
1574     /* Return function status */
1575     return HAL_OK;
1576   }
1577   else
1578   {
1579     /* Update error code */
1580     hcan->ErrorCode |= HAL_CAN_ERROR_NOT_INITIALIZED;
1581 
1582     return HAL_ERROR;
1583   }
1584 }
1585 
1586 /**
1587   * @brief  Return Rx FIFO fill level.
1588   * @param  hcan pointer to an CAN_HandleTypeDef structure that contains
1589   *         the configuration information for the specified CAN.
1590   * @param  RxFifo Rx FIFO.
1591   *         This parameter can be a value of @arg CAN_receive_FIFO_number.
1592   * @retval Number of messages available in Rx FIFO.
1593   */
HAL_CAN_GetRxFifoFillLevel(CAN_HandleTypeDef * hcan,uint32_t RxFifo)1594 uint32_t HAL_CAN_GetRxFifoFillLevel(CAN_HandleTypeDef *hcan, uint32_t RxFifo)
1595 {
1596   uint32_t filllevel = 0U;
1597   HAL_CAN_StateTypeDef state = hcan->State;
1598 
1599   /* Check function parameters */
1600   assert_param(IS_CAN_RX_FIFO(RxFifo));
1601 
1602   if ((state == HAL_CAN_STATE_READY) ||
1603       (state == HAL_CAN_STATE_LISTENING))
1604   {
1605     if (RxFifo == CAN_RX_FIFO0)
1606     {
1607       filllevel = hcan->Instance->RF0R & CAN_RF0R_FMP0;
1608     }
1609     else /* RxFifo == CAN_RX_FIFO1 */
1610     {
1611       filllevel = hcan->Instance->RF1R & CAN_RF1R_FMP1;
1612     }
1613   }
1614 
1615   /* Return Rx FIFO fill level */
1616   return filllevel;
1617 }
1618 
1619 /**
1620   * @}
1621   */
1622 
1623 /** @defgroup CAN_Exported_Functions_Group4 Interrupts management
1624  *  @brief    Interrupts management
1625  *
1626 @verbatim
1627   ==============================================================================
1628                        ##### Interrupts management #####
1629   ==============================================================================
1630     [..]  This section provides functions allowing to:
1631       (+) HAL_CAN_ActivateNotification      : Enable interrupts
1632       (+) HAL_CAN_DeactivateNotification    : Disable interrupts
1633       (+) HAL_CAN_IRQHandler                : Handles CAN interrupt request
1634 
1635 @endverbatim
1636   * @{
1637   */
1638 
1639 /**
1640   * @brief  Enable interrupts.
1641   * @param  hcan pointer to an CAN_HandleTypeDef structure that contains
1642   *         the configuration information for the specified CAN.
1643   * @param  ActiveITs indicates which interrupts will be enabled.
1644   *         This parameter can be any combination of @arg CAN_Interrupts.
1645   * @retval HAL status
1646   */
HAL_CAN_ActivateNotification(CAN_HandleTypeDef * hcan,uint32_t ActiveITs)1647 HAL_StatusTypeDef HAL_CAN_ActivateNotification(CAN_HandleTypeDef *hcan, uint32_t ActiveITs)
1648 {
1649   HAL_CAN_StateTypeDef state = hcan->State;
1650 
1651   /* Check function parameters */
1652   assert_param(IS_CAN_IT(ActiveITs));
1653 
1654   if ((state == HAL_CAN_STATE_READY) ||
1655       (state == HAL_CAN_STATE_LISTENING))
1656   {
1657     /* Enable the selected interrupts */
1658     __HAL_CAN_ENABLE_IT(hcan, ActiveITs);
1659 
1660     /* Return function status */
1661     return HAL_OK;
1662   }
1663   else
1664   {
1665     /* Update error code */
1666     hcan->ErrorCode |= HAL_CAN_ERROR_NOT_INITIALIZED;
1667 
1668     return HAL_ERROR;
1669   }
1670 }
1671 
1672 /**
1673   * @brief  Disable interrupts.
1674   * @param  hcan pointer to an CAN_HandleTypeDef structure that contains
1675   *         the configuration information for the specified CAN.
1676   * @param  InactiveITs indicates which interrupts will be disabled.
1677   *         This parameter can be any combination of @arg CAN_Interrupts.
1678   * @retval HAL status
1679   */
HAL_CAN_DeactivateNotification(CAN_HandleTypeDef * hcan,uint32_t InactiveITs)1680 HAL_StatusTypeDef HAL_CAN_DeactivateNotification(CAN_HandleTypeDef *hcan, uint32_t InactiveITs)
1681 {
1682   HAL_CAN_StateTypeDef state = hcan->State;
1683 
1684   /* Check function parameters */
1685   assert_param(IS_CAN_IT(InactiveITs));
1686 
1687   if ((state == HAL_CAN_STATE_READY) ||
1688       (state == HAL_CAN_STATE_LISTENING))
1689   {
1690     /* Disable the selected interrupts */
1691     __HAL_CAN_DISABLE_IT(hcan, InactiveITs);
1692 
1693     /* Return function status */
1694     return HAL_OK;
1695   }
1696   else
1697   {
1698     /* Update error code */
1699     hcan->ErrorCode |= HAL_CAN_ERROR_NOT_INITIALIZED;
1700 
1701     return HAL_ERROR;
1702   }
1703 }
1704 
1705 /**
1706   * @brief  Handles CAN interrupt request
1707   * @param  hcan pointer to a CAN_HandleTypeDef structure that contains
1708   *         the configuration information for the specified CAN.
1709   * @retval None
1710   */
HAL_CAN_IRQHandler(CAN_HandleTypeDef * hcan)1711 void HAL_CAN_IRQHandler(CAN_HandleTypeDef *hcan)
1712 {
1713   uint32_t errorcode = HAL_CAN_ERROR_NONE;
1714   uint32_t interrupts = READ_REG(hcan->Instance->IER);
1715   uint32_t msrflags = READ_REG(hcan->Instance->MSR);
1716   uint32_t tsrflags = READ_REG(hcan->Instance->TSR);
1717   uint32_t rf0rflags = READ_REG(hcan->Instance->RF0R);
1718   uint32_t rf1rflags = READ_REG(hcan->Instance->RF1R);
1719   uint32_t esrflags = READ_REG(hcan->Instance->ESR);
1720 
1721   /* Transmit Mailbox empty interrupt management *****************************/
1722   if ((interrupts & CAN_IT_TX_MAILBOX_EMPTY) != 0U)
1723   {
1724     /* Transmit Mailbox 0 management *****************************************/
1725     if ((tsrflags & CAN_TSR_RQCP0) != 0U)
1726     {
1727       /* Clear the Transmission Complete flag (and TXOK0,ALST0,TERR0 bits) */
1728       __HAL_CAN_CLEAR_FLAG(hcan, CAN_FLAG_RQCP0);
1729 
1730       if ((tsrflags & CAN_TSR_TXOK0) != 0U)
1731       {
1732         /* Transmission Mailbox 0 complete callback */
1733 #if USE_HAL_CAN_REGISTER_CALLBACKS == 1
1734         /* Call registered callback*/
1735         hcan->TxMailbox0CompleteCallback(hcan);
1736 #else
1737         /* Call weak (surcharged) callback */
1738         HAL_CAN_TxMailbox0CompleteCallback(hcan);
1739 #endif /* USE_HAL_CAN_REGISTER_CALLBACKS */
1740       }
1741       else
1742       {
1743         if ((tsrflags & CAN_TSR_ALST0) != 0U)
1744         {
1745           /* Update error code */
1746           errorcode |= HAL_CAN_ERROR_TX_ALST0;
1747         }
1748         else if ((tsrflags & CAN_TSR_TERR0) != 0U)
1749         {
1750           /* Update error code */
1751           errorcode |= HAL_CAN_ERROR_TX_TERR0;
1752         }
1753         else
1754         {
1755           /* Transmission Mailbox 0 abort callback */
1756 #if USE_HAL_CAN_REGISTER_CALLBACKS == 1
1757           /* Call registered callback*/
1758           hcan->TxMailbox0AbortCallback(hcan);
1759 #else
1760           /* Call weak (surcharged) callback */
1761           HAL_CAN_TxMailbox0AbortCallback(hcan);
1762 #endif /* USE_HAL_CAN_REGISTER_CALLBACKS */
1763         }
1764       }
1765     }
1766 
1767     /* Transmit Mailbox 1 management *****************************************/
1768     if ((tsrflags & CAN_TSR_RQCP1) != 0U)
1769     {
1770       /* Clear the Transmission Complete flag (and TXOK1,ALST1,TERR1 bits) */
1771       __HAL_CAN_CLEAR_FLAG(hcan, CAN_FLAG_RQCP1);
1772 
1773       if ((tsrflags & CAN_TSR_TXOK1) != 0U)
1774       {
1775         /* Transmission Mailbox 1 complete callback */
1776 #if USE_HAL_CAN_REGISTER_CALLBACKS == 1
1777         /* Call registered callback*/
1778         hcan->TxMailbox1CompleteCallback(hcan);
1779 #else
1780         /* Call weak (surcharged) callback */
1781         HAL_CAN_TxMailbox1CompleteCallback(hcan);
1782 #endif /* USE_HAL_CAN_REGISTER_CALLBACKS */
1783       }
1784       else
1785       {
1786         if ((tsrflags & CAN_TSR_ALST1) != 0U)
1787         {
1788           /* Update error code */
1789           errorcode |= HAL_CAN_ERROR_TX_ALST1;
1790         }
1791         else if ((tsrflags & CAN_TSR_TERR1) != 0U)
1792         {
1793           /* Update error code */
1794           errorcode |= HAL_CAN_ERROR_TX_TERR1;
1795         }
1796         else
1797         {
1798           /* Transmission Mailbox 1 abort callback */
1799 #if USE_HAL_CAN_REGISTER_CALLBACKS == 1
1800           /* Call registered callback*/
1801           hcan->TxMailbox1AbortCallback(hcan);
1802 #else
1803           /* Call weak (surcharged) callback */
1804           HAL_CAN_TxMailbox1AbortCallback(hcan);
1805 #endif /* USE_HAL_CAN_REGISTER_CALLBACKS */
1806         }
1807       }
1808     }
1809 
1810     /* Transmit Mailbox 2 management *****************************************/
1811     if ((tsrflags & CAN_TSR_RQCP2) != 0U)
1812     {
1813       /* Clear the Transmission Complete flag (and TXOK2,ALST2,TERR2 bits) */
1814       __HAL_CAN_CLEAR_FLAG(hcan, CAN_FLAG_RQCP2);
1815 
1816       if ((tsrflags & CAN_TSR_TXOK2) != 0U)
1817       {
1818         /* Transmission Mailbox 2 complete callback */
1819 #if USE_HAL_CAN_REGISTER_CALLBACKS == 1
1820         /* Call registered callback*/
1821         hcan->TxMailbox2CompleteCallback(hcan);
1822 #else
1823         /* Call weak (surcharged) callback */
1824         HAL_CAN_TxMailbox2CompleteCallback(hcan);
1825 #endif /* USE_HAL_CAN_REGISTER_CALLBACKS */
1826       }
1827       else
1828       {
1829         if ((tsrflags & CAN_TSR_ALST2) != 0U)
1830         {
1831           /* Update error code */
1832           errorcode |= HAL_CAN_ERROR_TX_ALST2;
1833         }
1834         else if ((tsrflags & CAN_TSR_TERR2) != 0U)
1835         {
1836           /* Update error code */
1837           errorcode |= HAL_CAN_ERROR_TX_TERR2;
1838         }
1839         else
1840         {
1841           /* Transmission Mailbox 2 abort callback */
1842 #if USE_HAL_CAN_REGISTER_CALLBACKS == 1
1843           /* Call registered callback*/
1844           hcan->TxMailbox2AbortCallback(hcan);
1845 #else
1846           /* Call weak (surcharged) callback */
1847           HAL_CAN_TxMailbox2AbortCallback(hcan);
1848 #endif /* USE_HAL_CAN_REGISTER_CALLBACKS */
1849         }
1850       }
1851     }
1852   }
1853 
1854   /* Receive FIFO 0 overrun interrupt management *****************************/
1855   if ((interrupts & CAN_IT_RX_FIFO0_OVERRUN) != 0U)
1856   {
1857     if ((rf0rflags & CAN_RF0R_FOVR0) != 0U)
1858     {
1859       /* Set CAN error code to Rx Fifo 0 overrun error */
1860       errorcode |= HAL_CAN_ERROR_RX_FOV0;
1861 
1862       /* Clear FIFO0 Overrun Flag */
1863       __HAL_CAN_CLEAR_FLAG(hcan, CAN_FLAG_FOV0);
1864     }
1865   }
1866 
1867   /* Receive FIFO 0 full interrupt management ********************************/
1868   if ((interrupts & CAN_IT_RX_FIFO0_FULL) != 0U)
1869   {
1870     if ((rf0rflags & CAN_RF0R_FULL0) != 0U)
1871     {
1872       /* Clear FIFO 0 full Flag */
1873       __HAL_CAN_CLEAR_FLAG(hcan, CAN_FLAG_FF0);
1874 
1875       /* Receive FIFO 0 full Callback */
1876 #if USE_HAL_CAN_REGISTER_CALLBACKS == 1
1877       /* Call registered callback*/
1878       hcan->RxFifo0FullCallback(hcan);
1879 #else
1880       /* Call weak (surcharged) callback */
1881       HAL_CAN_RxFifo0FullCallback(hcan);
1882 #endif /* USE_HAL_CAN_REGISTER_CALLBACKS */
1883     }
1884   }
1885 
1886   /* Receive FIFO 0 message pending interrupt management *********************/
1887   if ((interrupts & CAN_IT_RX_FIFO0_MSG_PENDING) != 0U)
1888   {
1889     /* Check if message is still pending */
1890     if ((hcan->Instance->RF0R & CAN_RF0R_FMP0) != 0U)
1891     {
1892       /* Receive FIFO 0 mesage pending Callback */
1893 #if USE_HAL_CAN_REGISTER_CALLBACKS == 1
1894       /* Call registered callback*/
1895       hcan->RxFifo0MsgPendingCallback(hcan);
1896 #else
1897       /* Call weak (surcharged) callback */
1898       HAL_CAN_RxFifo0MsgPendingCallback(hcan);
1899 #endif /* USE_HAL_CAN_REGISTER_CALLBACKS */
1900     }
1901   }
1902 
1903   /* Receive FIFO 1 overrun interrupt management *****************************/
1904   if ((interrupts & CAN_IT_RX_FIFO1_OVERRUN) != 0U)
1905   {
1906     if ((rf1rflags & CAN_RF1R_FOVR1) != 0U)
1907     {
1908       /* Set CAN error code to Rx Fifo 1 overrun error */
1909       errorcode |= HAL_CAN_ERROR_RX_FOV1;
1910 
1911       /* Clear FIFO1 Overrun Flag */
1912       __HAL_CAN_CLEAR_FLAG(hcan, CAN_FLAG_FOV1);
1913     }
1914   }
1915 
1916   /* Receive FIFO 1 full interrupt management ********************************/
1917   if ((interrupts & CAN_IT_RX_FIFO1_FULL) != 0U)
1918   {
1919     if ((rf1rflags & CAN_RF1R_FULL1) != 0U)
1920     {
1921       /* Clear FIFO 1 full Flag */
1922       __HAL_CAN_CLEAR_FLAG(hcan, CAN_FLAG_FF1);
1923 
1924       /* Receive FIFO 1 full Callback */
1925 #if USE_HAL_CAN_REGISTER_CALLBACKS == 1
1926       /* Call registered callback*/
1927       hcan->RxFifo1FullCallback(hcan);
1928 #else
1929       /* Call weak (surcharged) callback */
1930       HAL_CAN_RxFifo1FullCallback(hcan);
1931 #endif /* USE_HAL_CAN_REGISTER_CALLBACKS */
1932     }
1933   }
1934 
1935   /* Receive FIFO 1 message pending interrupt management *********************/
1936   if ((interrupts & CAN_IT_RX_FIFO1_MSG_PENDING) != 0U)
1937   {
1938     /* Check if message is still pending */
1939     if ((hcan->Instance->RF1R & CAN_RF1R_FMP1) != 0U)
1940     {
1941       /* Receive FIFO 1 mesage pending Callback */
1942 #if USE_HAL_CAN_REGISTER_CALLBACKS == 1
1943       /* Call registered callback*/
1944       hcan->RxFifo1MsgPendingCallback(hcan);
1945 #else
1946       /* Call weak (surcharged) callback */
1947       HAL_CAN_RxFifo1MsgPendingCallback(hcan);
1948 #endif /* USE_HAL_CAN_REGISTER_CALLBACKS */
1949     }
1950   }
1951 
1952   /* Sleep interrupt management *********************************************/
1953   if ((interrupts & CAN_IT_SLEEP_ACK) != 0U)
1954   {
1955     if ((msrflags & CAN_MSR_SLAKI) != 0U)
1956     {
1957       /* Clear Sleep interrupt Flag */
1958       __HAL_CAN_CLEAR_FLAG(hcan, CAN_FLAG_SLAKI);
1959 
1960       /* Sleep Callback */
1961 #if USE_HAL_CAN_REGISTER_CALLBACKS == 1
1962       /* Call registered callback*/
1963       hcan->SleepCallback(hcan);
1964 #else
1965       /* Call weak (surcharged) callback */
1966       HAL_CAN_SleepCallback(hcan);
1967 #endif /* USE_HAL_CAN_REGISTER_CALLBACKS */
1968     }
1969   }
1970 
1971   /* WakeUp interrupt management *********************************************/
1972   if ((interrupts & CAN_IT_WAKEUP) != 0U)
1973   {
1974     if ((msrflags & CAN_MSR_WKUI) != 0U)
1975     {
1976       /* Clear WakeUp Flag */
1977       __HAL_CAN_CLEAR_FLAG(hcan, CAN_FLAG_WKU);
1978 
1979       /* WakeUp Callback */
1980 #if USE_HAL_CAN_REGISTER_CALLBACKS == 1
1981       /* Call registered callback*/
1982       hcan->WakeUpFromRxMsgCallback(hcan);
1983 #else
1984       /* Call weak (surcharged) callback */
1985       HAL_CAN_WakeUpFromRxMsgCallback(hcan);
1986 #endif /* USE_HAL_CAN_REGISTER_CALLBACKS */
1987     }
1988   }
1989 
1990   /* Error interrupts management *********************************************/
1991   if ((interrupts & CAN_IT_ERROR) != 0U)
1992   {
1993     if ((msrflags & CAN_MSR_ERRI) != 0U)
1994     {
1995       /* Check Error Warning Flag */
1996       if (((interrupts & CAN_IT_ERROR_WARNING) != 0U) &&
1997           ((esrflags & CAN_ESR_EWGF) != 0U))
1998       {
1999         /* Set CAN error code to Error Warning */
2000         errorcode |= HAL_CAN_ERROR_EWG;
2001 
2002         /* No need for clear of Error Warning Flag as read-only */
2003       }
2004 
2005       /* Check Error Passive Flag */
2006       if (((interrupts & CAN_IT_ERROR_PASSIVE) != 0U) &&
2007           ((esrflags & CAN_ESR_EPVF) != 0U))
2008       {
2009         /* Set CAN error code to Error Passive */
2010         errorcode |= HAL_CAN_ERROR_EPV;
2011 
2012         /* No need for clear of Error Passive Flag as read-only */
2013       }
2014 
2015       /* Check Bus-off Flag */
2016       if (((interrupts & CAN_IT_BUSOFF) != 0U) &&
2017           ((esrflags & CAN_ESR_BOFF) != 0U))
2018       {
2019         /* Set CAN error code to Bus-Off */
2020         errorcode |= HAL_CAN_ERROR_BOF;
2021 
2022         /* No need for clear of Error Bus-Off as read-only */
2023       }
2024 
2025       /* Check Last Error Code Flag */
2026       if (((interrupts & CAN_IT_LAST_ERROR_CODE) != 0U) &&
2027           ((esrflags & CAN_ESR_LEC) != 0U))
2028       {
2029         switch (esrflags & CAN_ESR_LEC)
2030         {
2031           case (CAN_ESR_LEC_0):
2032             /* Set CAN error code to Stuff error */
2033             errorcode |= HAL_CAN_ERROR_STF;
2034             break;
2035           case (CAN_ESR_LEC_1):
2036             /* Set CAN error code to Form error */
2037             errorcode |= HAL_CAN_ERROR_FOR;
2038             break;
2039           case (CAN_ESR_LEC_1 | CAN_ESR_LEC_0):
2040             /* Set CAN error code to Acknowledgement error */
2041             errorcode |= HAL_CAN_ERROR_ACK;
2042             break;
2043           case (CAN_ESR_LEC_2):
2044             /* Set CAN error code to Bit recessive error */
2045             errorcode |= HAL_CAN_ERROR_BR;
2046             break;
2047           case (CAN_ESR_LEC_2 | CAN_ESR_LEC_0):
2048             /* Set CAN error code to Bit Dominant error */
2049             errorcode |= HAL_CAN_ERROR_BD;
2050             break;
2051           case (CAN_ESR_LEC_2 | CAN_ESR_LEC_1):
2052             /* Set CAN error code to CRC error */
2053             errorcode |= HAL_CAN_ERROR_CRC;
2054             break;
2055           default:
2056             break;
2057         }
2058 
2059         /* Clear Last error code Flag */
2060         CLEAR_BIT(hcan->Instance->ESR, CAN_ESR_LEC);
2061       }
2062     }
2063 
2064     /* Clear ERRI Flag */
2065     __HAL_CAN_CLEAR_FLAG(hcan, CAN_FLAG_ERRI);
2066   }
2067 
2068   /* Call the Error call Back in case of Errors */
2069   if (errorcode != HAL_CAN_ERROR_NONE)
2070   {
2071     /* Update error code in handle */
2072     hcan->ErrorCode |= errorcode;
2073 
2074     /* Call Error callback function */
2075 #if USE_HAL_CAN_REGISTER_CALLBACKS == 1
2076     /* Call registered callback*/
2077     hcan->ErrorCallback(hcan);
2078 #else
2079     /* Call weak (surcharged) callback */
2080     HAL_CAN_ErrorCallback(hcan);
2081 #endif /* USE_HAL_CAN_REGISTER_CALLBACKS */
2082   }
2083 }
2084 
2085 /**
2086   * @}
2087   */
2088 
2089 /** @defgroup CAN_Exported_Functions_Group5 Callback functions
2090  *  @brief   CAN Callback functions
2091  *
2092 @verbatim
2093   ==============================================================================
2094                           ##### Callback functions #####
2095   ==============================================================================
2096     [..]
2097     This subsection provides the following callback functions:
2098       (+) HAL_CAN_TxMailbox0CompleteCallback
2099       (+) HAL_CAN_TxMailbox1CompleteCallback
2100       (+) HAL_CAN_TxMailbox2CompleteCallback
2101       (+) HAL_CAN_TxMailbox0AbortCallback
2102       (+) HAL_CAN_TxMailbox1AbortCallback
2103       (+) HAL_CAN_TxMailbox2AbortCallback
2104       (+) HAL_CAN_RxFifo0MsgPendingCallback
2105       (+) HAL_CAN_RxFifo0FullCallback
2106       (+) HAL_CAN_RxFifo1MsgPendingCallback
2107       (+) HAL_CAN_RxFifo1FullCallback
2108       (+) HAL_CAN_SleepCallback
2109       (+) HAL_CAN_WakeUpFromRxMsgCallback
2110       (+) HAL_CAN_ErrorCallback
2111 
2112 @endverbatim
2113   * @{
2114   */
2115 
2116 /**
2117   * @brief  Transmission Mailbox 0 complete callback.
2118   * @param  hcan pointer to a CAN_HandleTypeDef structure that contains
2119   *         the configuration information for the specified CAN.
2120   * @retval None
2121   */
HAL_CAN_TxMailbox0CompleteCallback(CAN_HandleTypeDef * hcan)2122 __weak void HAL_CAN_TxMailbox0CompleteCallback(CAN_HandleTypeDef *hcan)
2123 {
2124   /* Prevent unused argument(s) compilation warning */
2125   UNUSED(hcan);
2126 
2127   /* NOTE : This function Should not be modified, when the callback is needed,
2128             the HAL_CAN_TxMailbox0CompleteCallback could be implemented in the
2129             user file
2130    */
2131 }
2132 
2133 /**
2134   * @brief  Transmission Mailbox 1 complete callback.
2135   * @param  hcan pointer to a CAN_HandleTypeDef structure that contains
2136   *         the configuration information for the specified CAN.
2137   * @retval None
2138   */
HAL_CAN_TxMailbox1CompleteCallback(CAN_HandleTypeDef * hcan)2139 __weak void HAL_CAN_TxMailbox1CompleteCallback(CAN_HandleTypeDef *hcan)
2140 {
2141   /* Prevent unused argument(s) compilation warning */
2142   UNUSED(hcan);
2143 
2144   /* NOTE : This function Should not be modified, when the callback is needed,
2145             the HAL_CAN_TxMailbox1CompleteCallback could be implemented in the
2146             user file
2147    */
2148 }
2149 
2150 /**
2151   * @brief  Transmission Mailbox 2 complete callback.
2152   * @param  hcan pointer to a CAN_HandleTypeDef structure that contains
2153   *         the configuration information for the specified CAN.
2154   * @retval None
2155   */
HAL_CAN_TxMailbox2CompleteCallback(CAN_HandleTypeDef * hcan)2156 __weak void HAL_CAN_TxMailbox2CompleteCallback(CAN_HandleTypeDef *hcan)
2157 {
2158   /* Prevent unused argument(s) compilation warning */
2159   UNUSED(hcan);
2160 
2161   /* NOTE : This function Should not be modified, when the callback is needed,
2162             the HAL_CAN_TxMailbox2CompleteCallback could be implemented in the
2163             user file
2164    */
2165 }
2166 
2167 /**
2168   * @brief  Transmission Mailbox 0 Cancellation callback.
2169   * @param  hcan pointer to an CAN_HandleTypeDef structure that contains
2170   *         the configuration information for the specified CAN.
2171   * @retval None
2172   */
HAL_CAN_TxMailbox0AbortCallback(CAN_HandleTypeDef * hcan)2173 __weak void HAL_CAN_TxMailbox0AbortCallback(CAN_HandleTypeDef *hcan)
2174 {
2175   /* Prevent unused argument(s) compilation warning */
2176   UNUSED(hcan);
2177 
2178   /* NOTE : This function Should not be modified, when the callback is needed,
2179             the HAL_CAN_TxMailbox0AbortCallback could be implemented in the
2180             user file
2181    */
2182 }
2183 
2184 /**
2185   * @brief  Transmission Mailbox 1 Cancellation callback.
2186   * @param  hcan pointer to an CAN_HandleTypeDef structure that contains
2187   *         the configuration information for the specified CAN.
2188   * @retval None
2189   */
HAL_CAN_TxMailbox1AbortCallback(CAN_HandleTypeDef * hcan)2190 __weak void HAL_CAN_TxMailbox1AbortCallback(CAN_HandleTypeDef *hcan)
2191 {
2192   /* Prevent unused argument(s) compilation warning */
2193   UNUSED(hcan);
2194 
2195   /* NOTE : This function Should not be modified, when the callback is needed,
2196             the HAL_CAN_TxMailbox1AbortCallback could be implemented in the
2197             user file
2198    */
2199 }
2200 
2201 /**
2202   * @brief  Transmission Mailbox 2 Cancellation callback.
2203   * @param  hcan pointer to an CAN_HandleTypeDef structure that contains
2204   *         the configuration information for the specified CAN.
2205   * @retval None
2206   */
HAL_CAN_TxMailbox2AbortCallback(CAN_HandleTypeDef * hcan)2207 __weak void HAL_CAN_TxMailbox2AbortCallback(CAN_HandleTypeDef *hcan)
2208 {
2209   /* Prevent unused argument(s) compilation warning */
2210   UNUSED(hcan);
2211 
2212   /* NOTE : This function Should not be modified, when the callback is needed,
2213             the HAL_CAN_TxMailbox2AbortCallback could be implemented in the
2214             user file
2215    */
2216 }
2217 
2218 /**
2219   * @brief  Rx FIFO 0 message pending callback.
2220   * @param  hcan pointer to a CAN_HandleTypeDef structure that contains
2221   *         the configuration information for the specified CAN.
2222   * @retval None
2223   */
HAL_CAN_RxFifo0MsgPendingCallback(CAN_HandleTypeDef * hcan)2224 __weak void HAL_CAN_RxFifo0MsgPendingCallback(CAN_HandleTypeDef *hcan)
2225 {
2226   /* Prevent unused argument(s) compilation warning */
2227   UNUSED(hcan);
2228 
2229   /* NOTE : This function Should not be modified, when the callback is needed,
2230             the HAL_CAN_RxFifo0MsgPendingCallback could be implemented in the
2231             user file
2232    */
2233 }
2234 
2235 /**
2236   * @brief  Rx FIFO 0 full callback.
2237   * @param  hcan pointer to a CAN_HandleTypeDef structure that contains
2238   *         the configuration information for the specified CAN.
2239   * @retval None
2240   */
HAL_CAN_RxFifo0FullCallback(CAN_HandleTypeDef * hcan)2241 __weak void HAL_CAN_RxFifo0FullCallback(CAN_HandleTypeDef *hcan)
2242 {
2243   /* Prevent unused argument(s) compilation warning */
2244   UNUSED(hcan);
2245 
2246   /* NOTE : This function Should not be modified, when the callback is needed,
2247             the HAL_CAN_RxFifo0FullCallback could be implemented in the user
2248             file
2249    */
2250 }
2251 
2252 /**
2253   * @brief  Rx FIFO 1 message pending callback.
2254   * @param  hcan pointer to a CAN_HandleTypeDef structure that contains
2255   *         the configuration information for the specified CAN.
2256   * @retval None
2257   */
HAL_CAN_RxFifo1MsgPendingCallback(CAN_HandleTypeDef * hcan)2258 __weak void HAL_CAN_RxFifo1MsgPendingCallback(CAN_HandleTypeDef *hcan)
2259 {
2260   /* Prevent unused argument(s) compilation warning */
2261   UNUSED(hcan);
2262 
2263   /* NOTE : This function Should not be modified, when the callback is needed,
2264             the HAL_CAN_RxFifo1MsgPendingCallback could be implemented in the
2265             user file
2266    */
2267 }
2268 
2269 /**
2270   * @brief  Rx FIFO 1 full callback.
2271   * @param  hcan pointer to a CAN_HandleTypeDef structure that contains
2272   *         the configuration information for the specified CAN.
2273   * @retval None
2274   */
HAL_CAN_RxFifo1FullCallback(CAN_HandleTypeDef * hcan)2275 __weak void HAL_CAN_RxFifo1FullCallback(CAN_HandleTypeDef *hcan)
2276 {
2277   /* Prevent unused argument(s) compilation warning */
2278   UNUSED(hcan);
2279 
2280   /* NOTE : This function Should not be modified, when the callback is needed,
2281             the HAL_CAN_RxFifo1FullCallback could be implemented in the user
2282             file
2283    */
2284 }
2285 
2286 /**
2287   * @brief  Sleep callback.
2288   * @param  hcan pointer to a CAN_HandleTypeDef structure that contains
2289   *         the configuration information for the specified CAN.
2290   * @retval None
2291   */
HAL_CAN_SleepCallback(CAN_HandleTypeDef * hcan)2292 __weak void HAL_CAN_SleepCallback(CAN_HandleTypeDef *hcan)
2293 {
2294   /* Prevent unused argument(s) compilation warning */
2295   UNUSED(hcan);
2296 
2297   /* NOTE : This function Should not be modified, when the callback is needed,
2298             the HAL_CAN_SleepCallback could be implemented in the user file
2299    */
2300 }
2301 
2302 /**
2303   * @brief  WakeUp from Rx message callback.
2304   * @param  hcan pointer to a CAN_HandleTypeDef structure that contains
2305   *         the configuration information for the specified CAN.
2306   * @retval None
2307   */
HAL_CAN_WakeUpFromRxMsgCallback(CAN_HandleTypeDef * hcan)2308 __weak void HAL_CAN_WakeUpFromRxMsgCallback(CAN_HandleTypeDef *hcan)
2309 {
2310   /* Prevent unused argument(s) compilation warning */
2311   UNUSED(hcan);
2312 
2313   /* NOTE : This function Should not be modified, when the callback is needed,
2314             the HAL_CAN_WakeUpFromRxMsgCallback could be implemented in the
2315             user file
2316    */
2317 }
2318 
2319 /**
2320   * @brief  Error CAN callback.
2321   * @param  hcan pointer to a CAN_HandleTypeDef structure that contains
2322   *         the configuration information for the specified CAN.
2323   * @retval None
2324   */
HAL_CAN_ErrorCallback(CAN_HandleTypeDef * hcan)2325 __weak void HAL_CAN_ErrorCallback(CAN_HandleTypeDef *hcan)
2326 {
2327   /* Prevent unused argument(s) compilation warning */
2328   UNUSED(hcan);
2329 
2330   /* NOTE : This function Should not be modified, when the callback is needed,
2331             the HAL_CAN_ErrorCallback could be implemented in the user file
2332    */
2333 }
2334 
2335 /**
2336   * @}
2337   */
2338 
2339 /** @defgroup CAN_Exported_Functions_Group6 Peripheral State and Error functions
2340  *  @brief   CAN Peripheral State functions
2341  *
2342 @verbatim
2343   ==============================================================================
2344             ##### Peripheral State and Error functions #####
2345   ==============================================================================
2346     [..]
2347     This subsection provides functions allowing to :
2348       (+) HAL_CAN_GetState()  : Return the CAN state.
2349       (+) HAL_CAN_GetError()  : Return the CAN error codes if any.
2350       (+) HAL_CAN_ResetError(): Reset the CAN error codes if any.
2351 
2352 @endverbatim
2353   * @{
2354   */
2355 
2356 /**
2357   * @brief  Return the CAN state.
2358   * @param  hcan pointer to a CAN_HandleTypeDef structure that contains
2359   *         the configuration information for the specified CAN.
2360   * @retval HAL state
2361   */
HAL_CAN_GetState(CAN_HandleTypeDef * hcan)2362 HAL_CAN_StateTypeDef HAL_CAN_GetState(CAN_HandleTypeDef *hcan)
2363 {
2364   HAL_CAN_StateTypeDef state = hcan->State;
2365 
2366   if ((state == HAL_CAN_STATE_READY) ||
2367       (state == HAL_CAN_STATE_LISTENING))
2368   {
2369     /* Check sleep mode acknowledge flag */
2370     if ((hcan->Instance->MSR & CAN_MSR_SLAK) != 0U)
2371     {
2372       /* Sleep mode is active */
2373       state = HAL_CAN_STATE_SLEEP_ACTIVE;
2374     }
2375     /* Check sleep mode request flag */
2376     else if ((hcan->Instance->MCR & CAN_MCR_SLEEP) != 0U)
2377     {
2378       /* Sleep mode request is pending */
2379       state = HAL_CAN_STATE_SLEEP_PENDING;
2380     }
2381     else
2382     {
2383       /* Neither sleep mode request nor sleep mode acknowledge */
2384     }
2385   }
2386 
2387   /* Return CAN state */
2388   return state;
2389 }
2390 
2391 /**
2392   * @brief  Return the CAN error code.
2393   * @param  hcan pointer to a CAN_HandleTypeDef structure that contains
2394   *         the configuration information for the specified CAN.
2395   * @retval CAN Error Code
2396   */
HAL_CAN_GetError(CAN_HandleTypeDef * hcan)2397 uint32_t HAL_CAN_GetError(CAN_HandleTypeDef *hcan)
2398 {
2399   /* Return CAN error code */
2400   return hcan->ErrorCode;
2401 }
2402 
2403 /**
2404   * @brief  Reset the CAN error code.
2405   * @param  hcan pointer to a CAN_HandleTypeDef structure that contains
2406   *         the configuration information for the specified CAN.
2407   * @retval HAL status
2408   */
HAL_CAN_ResetError(CAN_HandleTypeDef * hcan)2409 HAL_StatusTypeDef HAL_CAN_ResetError(CAN_HandleTypeDef *hcan)
2410 {
2411   HAL_StatusTypeDef status = HAL_OK;
2412   HAL_CAN_StateTypeDef state = hcan->State;
2413 
2414   if ((state == HAL_CAN_STATE_READY) ||
2415       (state == HAL_CAN_STATE_LISTENING))
2416   {
2417     /* Reset CAN error code */
2418     hcan->ErrorCode = 0U;
2419   }
2420   else
2421   {
2422     /* Update error code */
2423     hcan->ErrorCode |= HAL_CAN_ERROR_NOT_INITIALIZED;
2424 
2425     status = HAL_ERROR;
2426   }
2427 
2428   /* Return the status */
2429   return status;
2430 }
2431 
2432 /**
2433   * @}
2434   */
2435 
2436 /**
2437   * @}
2438   */
2439 
2440 #endif /* HAL_CAN_MODULE_ENABLED */
2441 
2442 /**
2443   * @}
2444   */
2445 
2446 #endif /* CAN1 */
2447 
2448 /**
2449   * @}
2450   */
2451 
2452 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
2453