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