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