STM32F103xB HAL User Manual
|
00001 /** 00002 ****************************************************************************** 00003 * @file stm32f1xx_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 @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) 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 "stm32f1xx_hal.h" 00214 00215 /** @addtogroup STM32F1xx_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(CAN2) 00858 /* CAN1 and CAN2 are dual instances with 28 common filters banks */ 00859 /* Select master instance to access the filter banks */ 00860 can_ip = CAN1; 00861 00862 /* Check the parameters */ 00863 assert_param(IS_CAN_FILTER_BANK_DUAL(sFilterConfig->FilterBank)); 00864 assert_param(IS_CAN_FILTER_BANK_DUAL(sFilterConfig->SlaveStartFilterBank)); 00865 #else 00866 /* CAN1 is single instance with 14 dedicated filters banks */ 00867 00868 /* Check the parameters */ 00869 assert_param(IS_CAN_FILTER_BANK_SINGLE(sFilterConfig->FilterBank)); 00870 #endif 00871 00872 /* Initialisation mode for the filter */ 00873 SET_BIT(can_ip->FMR, CAN_FMR_FINIT); 00874 00875 #if defined(CAN2) 00876 /* Select the start filter number of CAN2 slave instance */ 00877 CLEAR_BIT(can_ip->FMR, CAN_FMR_CAN2SB); 00878 SET_BIT(can_ip->FMR, sFilterConfig->SlaveStartFilterBank << CAN_FMR_CAN2SB_Pos); 00879 00880 #endif 00881 /* Convert filter number into bit position */ 00882 filternbrbitpos = (uint32_t)1 << (sFilterConfig->FilterBank & 0x1FU); 00883 00884 /* Filter Deactivation */ 00885 CLEAR_BIT(can_ip->FA1R, filternbrbitpos); 00886 00887 /* Filter Scale */ 00888 if (sFilterConfig->FilterScale == CAN_FILTERSCALE_16BIT) 00889 { 00890 /* 16-bit scale for the filter */ 00891 CLEAR_BIT(can_ip->FS1R, filternbrbitpos); 00892 00893 /* First 16-bit identifier and First 16-bit mask */ 00894 /* Or First 16-bit identifier and Second 16-bit identifier */ 00895 can_ip->sFilterRegister[sFilterConfig->FilterBank].FR1 = 00896 ((0x0000FFFFU & (uint32_t)sFilterConfig->FilterMaskIdLow) << 16U) | 00897 (0x0000FFFFU & (uint32_t)sFilterConfig->FilterIdLow); 00898 00899 /* Second 16-bit identifier and Second 16-bit mask */ 00900 /* Or Third 16-bit identifier and Fourth 16-bit identifier */ 00901 can_ip->sFilterRegister[sFilterConfig->FilterBank].FR2 = 00902 ((0x0000FFFFU & (uint32_t)sFilterConfig->FilterMaskIdHigh) << 16U) | 00903 (0x0000FFFFU & (uint32_t)sFilterConfig->FilterIdHigh); 00904 } 00905 00906 if (sFilterConfig->FilterScale == CAN_FILTERSCALE_32BIT) 00907 { 00908 /* 32-bit scale for the filter */ 00909 SET_BIT(can_ip->FS1R, filternbrbitpos); 00910 00911 /* 32-bit identifier or First 32-bit identifier */ 00912 can_ip->sFilterRegister[sFilterConfig->FilterBank].FR1 = 00913 ((0x0000FFFFU & (uint32_t)sFilterConfig->FilterIdHigh) << 16U) | 00914 (0x0000FFFFU & (uint32_t)sFilterConfig->FilterIdLow); 00915 00916 /* 32-bit mask or Second 32-bit identifier */ 00917 can_ip->sFilterRegister[sFilterConfig->FilterBank].FR2 = 00918 ((0x0000FFFFU & (uint32_t)sFilterConfig->FilterMaskIdHigh) << 16U) | 00919 (0x0000FFFFU & (uint32_t)sFilterConfig->FilterMaskIdLow); 00920 } 00921 00922 /* Filter Mode */ 00923 if (sFilterConfig->FilterMode == CAN_FILTERMODE_IDMASK) 00924 { 00925 /* Id/Mask mode for the filter*/ 00926 CLEAR_BIT(can_ip->FM1R, filternbrbitpos); 00927 } 00928 else /* CAN_FilterInitStruct->CAN_FilterMode == CAN_FilterMode_IdList */ 00929 { 00930 /* Identifier list mode for the filter*/ 00931 SET_BIT(can_ip->FM1R, filternbrbitpos); 00932 } 00933 00934 /* Filter FIFO assignment */ 00935 if (sFilterConfig->FilterFIFOAssignment == CAN_FILTER_FIFO0) 00936 { 00937 /* FIFO 0 assignation for the filter */ 00938 CLEAR_BIT(can_ip->FFA1R, filternbrbitpos); 00939 } 00940 else 00941 { 00942 /* FIFO 1 assignation for the filter */ 00943 SET_BIT(can_ip->FFA1R, filternbrbitpos); 00944 } 00945 00946 /* Filter activation */ 00947 if (sFilterConfig->FilterActivation == CAN_FILTER_ENABLE) 00948 { 00949 SET_BIT(can_ip->FA1R, filternbrbitpos); 00950 } 00951 00952 /* Leave the initialisation mode for the filter */ 00953 CLEAR_BIT(can_ip->FMR, CAN_FMR_FINIT); 00954 00955 /* Return function status */ 00956 return HAL_OK; 00957 } 00958 else 00959 { 00960 /* Update error code */ 00961 hcan->ErrorCode |= HAL_CAN_ERROR_NOT_INITIALIZED; 00962 00963 return HAL_ERROR; 00964 } 00965 } 00966 00967 /** 00968 * @} 00969 */ 00970 00971 /** @defgroup CAN_Exported_Functions_Group3 Control functions 00972 * @brief Control functions 00973 * 00974 @verbatim 00975 ============================================================================== 00976 ##### Control functions ##### 00977 ============================================================================== 00978 [..] This section provides functions allowing to: 00979 (+) HAL_CAN_Start : Start the CAN module 00980 (+) HAL_CAN_Stop : Stop the CAN module 00981 (+) HAL_CAN_RequestSleep : Request sleep mode entry. 00982 (+) HAL_CAN_WakeUp : Wake up from sleep mode. 00983 (+) HAL_CAN_IsSleepActive : Check is sleep mode is active. 00984 (+) HAL_CAN_AddTxMessage : Add a message to the Tx mailboxes 00985 and activate the corresponding 00986 transmission request 00987 (+) HAL_CAN_AbortTxRequest : Abort transmission request 00988 (+) HAL_CAN_GetTxMailboxesFreeLevel : Return Tx mailboxes free level 00989 (+) HAL_CAN_IsTxMessagePending : Check if a transmission request is 00990 pending on the selected Tx mailbox 00991 (+) HAL_CAN_GetRxMessage : Get a CAN frame from the Rx FIFO 00992 (+) HAL_CAN_GetRxFifoFillLevel : Return Rx FIFO fill level 00993 00994 @endverbatim 00995 * @{ 00996 */ 00997 00998 /** 00999 * @brief Start the CAN module. 01000 * @param hcan pointer to an CAN_HandleTypeDef structure that contains 01001 * the configuration information for the specified CAN. 01002 * @retval HAL status 01003 */ 01004 HAL_StatusTypeDef HAL_CAN_Start(CAN_HandleTypeDef *hcan) 01005 { 01006 uint32_t tickstart; 01007 01008 if (hcan->State == HAL_CAN_STATE_READY) 01009 { 01010 /* Change CAN peripheral state */ 01011 hcan->State = HAL_CAN_STATE_LISTENING; 01012 01013 /* Request leave initialisation */ 01014 CLEAR_BIT(hcan->Instance->MCR, CAN_MCR_INRQ); 01015 01016 /* Get tick */ 01017 tickstart = HAL_GetTick(); 01018 01019 /* Wait the acknowledge */ 01020 while ((hcan->Instance->MSR & CAN_MSR_INAK) != 0U) 01021 { 01022 /* Check for the Timeout */ 01023 if ((HAL_GetTick() - tickstart) > CAN_TIMEOUT_VALUE) 01024 { 01025 /* Update error code */ 01026 hcan->ErrorCode |= HAL_CAN_ERROR_TIMEOUT; 01027 01028 /* Change CAN state */ 01029 hcan->State = HAL_CAN_STATE_ERROR; 01030 01031 return HAL_ERROR; 01032 } 01033 } 01034 01035 /* Reset the CAN ErrorCode */ 01036 hcan->ErrorCode = HAL_CAN_ERROR_NONE; 01037 01038 /* Return function status */ 01039 return HAL_OK; 01040 } 01041 else 01042 { 01043 /* Update error code */ 01044 hcan->ErrorCode |= HAL_CAN_ERROR_NOT_READY; 01045 01046 return HAL_ERROR; 01047 } 01048 } 01049 01050 /** 01051 * @brief Stop the CAN module and enable access to configuration registers. 01052 * @param hcan pointer to an CAN_HandleTypeDef structure that contains 01053 * the configuration information for the specified CAN. 01054 * @retval HAL status 01055 */ 01056 HAL_StatusTypeDef HAL_CAN_Stop(CAN_HandleTypeDef *hcan) 01057 { 01058 uint32_t tickstart; 01059 01060 if (hcan->State == HAL_CAN_STATE_LISTENING) 01061 { 01062 /* Request initialisation */ 01063 SET_BIT(hcan->Instance->MCR, CAN_MCR_INRQ); 01064 01065 /* Get tick */ 01066 tickstart = HAL_GetTick(); 01067 01068 /* Wait the acknowledge */ 01069 while ((hcan->Instance->MSR & CAN_MSR_INAK) == 0U) 01070 { 01071 /* Check for the Timeout */ 01072 if ((HAL_GetTick() - tickstart) > CAN_TIMEOUT_VALUE) 01073 { 01074 /* Update error code */ 01075 hcan->ErrorCode |= HAL_CAN_ERROR_TIMEOUT; 01076 01077 /* Change CAN state */ 01078 hcan->State = HAL_CAN_STATE_ERROR; 01079 01080 return HAL_ERROR; 01081 } 01082 } 01083 01084 /* Exit from sleep mode */ 01085 CLEAR_BIT(hcan->Instance->MCR, CAN_MCR_SLEEP); 01086 01087 /* Change CAN peripheral state */ 01088 hcan->State = HAL_CAN_STATE_READY; 01089 01090 /* Return function status */ 01091 return HAL_OK; 01092 } 01093 else 01094 { 01095 /* Update error code */ 01096 hcan->ErrorCode |= HAL_CAN_ERROR_NOT_STARTED; 01097 01098 return HAL_ERROR; 01099 } 01100 } 01101 01102 /** 01103 * @brief Request the sleep mode (low power) entry. 01104 * When returning from this function, Sleep mode will be entered 01105 * as soon as the current CAN activity (transmission or reception 01106 * of a CAN frame) has been completed. 01107 * @param hcan pointer to a CAN_HandleTypeDef structure that contains 01108 * the configuration information for the specified CAN. 01109 * @retval HAL status. 01110 */ 01111 HAL_StatusTypeDef HAL_CAN_RequestSleep(CAN_HandleTypeDef *hcan) 01112 { 01113 HAL_CAN_StateTypeDef state = hcan->State; 01114 01115 if ((state == HAL_CAN_STATE_READY) || 01116 (state == HAL_CAN_STATE_LISTENING)) 01117 { 01118 /* Request Sleep mode */ 01119 SET_BIT(hcan->Instance->MCR, CAN_MCR_SLEEP); 01120 01121 /* Return function status */ 01122 return HAL_OK; 01123 } 01124 else 01125 { 01126 /* Update error code */ 01127 hcan->ErrorCode |= HAL_CAN_ERROR_NOT_INITIALIZED; 01128 01129 /* Return function status */ 01130 return HAL_ERROR; 01131 } 01132 } 01133 01134 /** 01135 * @brief Wake up from sleep mode. 01136 * When returning with HAL_OK status from this function, Sleep mode 01137 * is exited. 01138 * @param hcan pointer to a CAN_HandleTypeDef structure that contains 01139 * the configuration information for the specified CAN. 01140 * @retval HAL status. 01141 */ 01142 HAL_StatusTypeDef HAL_CAN_WakeUp(CAN_HandleTypeDef *hcan) 01143 { 01144 __IO uint32_t count = 0; 01145 uint32_t timeout = 1000000U; 01146 HAL_CAN_StateTypeDef state = hcan->State; 01147 01148 if ((state == HAL_CAN_STATE_READY) || 01149 (state == HAL_CAN_STATE_LISTENING)) 01150 { 01151 /* Wake up request */ 01152 CLEAR_BIT(hcan->Instance->MCR, CAN_MCR_SLEEP); 01153 01154 /* Wait sleep mode is exited */ 01155 do 01156 { 01157 /* Increment counter */ 01158 count++; 01159 01160 /* Check if timeout is reached */ 01161 if (count > timeout) 01162 { 01163 /* Update error code */ 01164 hcan->ErrorCode |= HAL_CAN_ERROR_TIMEOUT; 01165 01166 return HAL_ERROR; 01167 } 01168 } 01169 while ((hcan->Instance->MSR & CAN_MSR_SLAK) != 0U); 01170 01171 /* Return function status */ 01172 return HAL_OK; 01173 } 01174 else 01175 { 01176 /* Update error code */ 01177 hcan->ErrorCode |= HAL_CAN_ERROR_NOT_INITIALIZED; 01178 01179 return HAL_ERROR; 01180 } 01181 } 01182 01183 /** 01184 * @brief Check is sleep mode is active. 01185 * @param hcan pointer to a CAN_HandleTypeDef structure that contains 01186 * the configuration information for the specified CAN. 01187 * @retval Status 01188 * - 0 : Sleep mode is not active. 01189 * - 1 : Sleep mode is active. 01190 */ 01191 uint32_t HAL_CAN_IsSleepActive(CAN_HandleTypeDef *hcan) 01192 { 01193 uint32_t status = 0U; 01194 HAL_CAN_StateTypeDef state = hcan->State; 01195 01196 if ((state == HAL_CAN_STATE_READY) || 01197 (state == HAL_CAN_STATE_LISTENING)) 01198 { 01199 /* Check Sleep mode */ 01200 if ((hcan->Instance->MSR & CAN_MSR_SLAK) != 0U) 01201 { 01202 status = 1U; 01203 } 01204 } 01205 01206 /* Return function status */ 01207 return status; 01208 } 01209 01210 /** 01211 * @brief Add a message to the first free Tx mailbox and activate the 01212 * corresponding transmission request. 01213 * @param hcan pointer to a CAN_HandleTypeDef structure that contains 01214 * the configuration information for the specified CAN. 01215 * @param pHeader pointer to a CAN_TxHeaderTypeDef structure. 01216 * @param aData array containing the payload of the Tx frame. 01217 * @param pTxMailbox pointer to a variable where the function will return 01218 * the TxMailbox used to store the Tx message. 01219 * This parameter can be a value of @arg CAN_Tx_Mailboxes. 01220 * @retval HAL status 01221 */ 01222 HAL_StatusTypeDef HAL_CAN_AddTxMessage(CAN_HandleTypeDef *hcan, CAN_TxHeaderTypeDef *pHeader, uint8_t aData[], uint32_t *pTxMailbox) 01223 { 01224 uint32_t transmitmailbox; 01225 HAL_CAN_StateTypeDef state = hcan->State; 01226 uint32_t tsr = READ_REG(hcan->Instance->TSR); 01227 01228 /* Check the parameters */ 01229 assert_param(IS_CAN_IDTYPE(pHeader->IDE)); 01230 assert_param(IS_CAN_RTR(pHeader->RTR)); 01231 assert_param(IS_CAN_DLC(pHeader->DLC)); 01232 if (pHeader->IDE == CAN_ID_STD) 01233 { 01234 assert_param(IS_CAN_STDID(pHeader->StdId)); 01235 } 01236 else 01237 { 01238 assert_param(IS_CAN_EXTID(pHeader->ExtId)); 01239 } 01240 assert_param(IS_FUNCTIONAL_STATE(pHeader->TransmitGlobalTime)); 01241 01242 if ((state == HAL_CAN_STATE_READY) || 01243 (state == HAL_CAN_STATE_LISTENING)) 01244 { 01245 /* Check that all the Tx mailboxes are not full */ 01246 if (((tsr & CAN_TSR_TME0) != 0U) || 01247 ((tsr & CAN_TSR_TME1) != 0U) || 01248 ((tsr & CAN_TSR_TME2) != 0U)) 01249 { 01250 /* Select an empty transmit mailbox */ 01251 transmitmailbox = (tsr & CAN_TSR_CODE) >> CAN_TSR_CODE_Pos; 01252 01253 /* Check transmit mailbox value */ 01254 if (transmitmailbox > 2U) 01255 { 01256 /* Update error code */ 01257 hcan->ErrorCode |= HAL_CAN_ERROR_INTERNAL; 01258 01259 return HAL_ERROR; 01260 } 01261 01262 /* Store the Tx mailbox */ 01263 *pTxMailbox = (uint32_t)1 << transmitmailbox; 01264 01265 /* Set up the Id */ 01266 if (pHeader->IDE == CAN_ID_STD) 01267 { 01268 hcan->Instance->sTxMailBox[transmitmailbox].TIR = ((pHeader->StdId << CAN_TI0R_STID_Pos) | 01269 pHeader->RTR); 01270 } 01271 else 01272 { 01273 hcan->Instance->sTxMailBox[transmitmailbox].TIR = ((pHeader->ExtId << CAN_TI0R_EXID_Pos) | 01274 pHeader->IDE | 01275 pHeader->RTR); 01276 } 01277 01278 /* Set up the DLC */ 01279 hcan->Instance->sTxMailBox[transmitmailbox].TDTR = (pHeader->DLC); 01280 01281 /* Set up the Transmit Global Time mode */ 01282 if (pHeader->TransmitGlobalTime == ENABLE) 01283 { 01284 SET_BIT(hcan->Instance->sTxMailBox[transmitmailbox].TDTR, CAN_TDT0R_TGT); 01285 } 01286 01287 /* Set up the data field */ 01288 WRITE_REG(hcan->Instance->sTxMailBox[transmitmailbox].TDHR, 01289 ((uint32_t)aData[7] << CAN_TDH0R_DATA7_Pos) | 01290 ((uint32_t)aData[6] << CAN_TDH0R_DATA6_Pos) | 01291 ((uint32_t)aData[5] << CAN_TDH0R_DATA5_Pos) | 01292 ((uint32_t)aData[4] << CAN_TDH0R_DATA4_Pos)); 01293 WRITE_REG(hcan->Instance->sTxMailBox[transmitmailbox].TDLR, 01294 ((uint32_t)aData[3] << CAN_TDL0R_DATA3_Pos) | 01295 ((uint32_t)aData[2] << CAN_TDL0R_DATA2_Pos) | 01296 ((uint32_t)aData[1] << CAN_TDL0R_DATA1_Pos) | 01297 ((uint32_t)aData[0] << CAN_TDL0R_DATA0_Pos)); 01298 01299 /* Request transmission */ 01300 SET_BIT(hcan->Instance->sTxMailBox[transmitmailbox].TIR, CAN_TI0R_TXRQ); 01301 01302 /* Return function status */ 01303 return HAL_OK; 01304 } 01305 else 01306 { 01307 /* Update error code */ 01308 hcan->ErrorCode |= HAL_CAN_ERROR_PARAM; 01309 01310 return HAL_ERROR; 01311 } 01312 } 01313 else 01314 { 01315 /* Update error code */ 01316 hcan->ErrorCode |= HAL_CAN_ERROR_NOT_INITIALIZED; 01317 01318 return HAL_ERROR; 01319 } 01320 } 01321 01322 /** 01323 * @brief Abort transmission requests 01324 * @param hcan pointer to an CAN_HandleTypeDef structure that contains 01325 * the configuration information for the specified CAN. 01326 * @param TxMailboxes List of the Tx Mailboxes to abort. 01327 * This parameter can be any combination of @arg CAN_Tx_Mailboxes. 01328 * @retval HAL status 01329 */ 01330 HAL_StatusTypeDef HAL_CAN_AbortTxRequest(CAN_HandleTypeDef *hcan, uint32_t TxMailboxes) 01331 { 01332 HAL_CAN_StateTypeDef state = hcan->State; 01333 01334 /* Check function parameters */ 01335 assert_param(IS_CAN_TX_MAILBOX_LIST(TxMailboxes)); 01336 01337 if ((state == HAL_CAN_STATE_READY) || 01338 (state == HAL_CAN_STATE_LISTENING)) 01339 { 01340 /* Check Tx Mailbox 0 */ 01341 if ((TxMailboxes & CAN_TX_MAILBOX0) != 0U) 01342 { 01343 /* Add cancellation request for Tx Mailbox 0 */ 01344 SET_BIT(hcan->Instance->TSR, CAN_TSR_ABRQ0); 01345 } 01346 01347 /* Check Tx Mailbox 1 */ 01348 if ((TxMailboxes & CAN_TX_MAILBOX1) != 0U) 01349 { 01350 /* Add cancellation request for Tx Mailbox 1 */ 01351 SET_BIT(hcan->Instance->TSR, CAN_TSR_ABRQ1); 01352 } 01353 01354 /* Check Tx Mailbox 2 */ 01355 if ((TxMailboxes & CAN_TX_MAILBOX2) != 0U) 01356 { 01357 /* Add cancellation request for Tx Mailbox 2 */ 01358 SET_BIT(hcan->Instance->TSR, CAN_TSR_ABRQ2); 01359 } 01360 01361 /* Return function status */ 01362 return HAL_OK; 01363 } 01364 else 01365 { 01366 /* Update error code */ 01367 hcan->ErrorCode |= HAL_CAN_ERROR_NOT_INITIALIZED; 01368 01369 return HAL_ERROR; 01370 } 01371 } 01372 01373 /** 01374 * @brief Return Tx Mailboxes free level: number of free Tx Mailboxes. 01375 * @param hcan pointer to a CAN_HandleTypeDef structure that contains 01376 * the configuration information for the specified CAN. 01377 * @retval Number of free Tx Mailboxes. 01378 */ 01379 uint32_t HAL_CAN_GetTxMailboxesFreeLevel(CAN_HandleTypeDef *hcan) 01380 { 01381 uint32_t freelevel = 0U; 01382 HAL_CAN_StateTypeDef state = hcan->State; 01383 01384 if ((state == HAL_CAN_STATE_READY) || 01385 (state == HAL_CAN_STATE_LISTENING)) 01386 { 01387 /* Check Tx Mailbox 0 status */ 01388 if ((hcan->Instance->TSR & CAN_TSR_TME0) != 0U) 01389 { 01390 freelevel++; 01391 } 01392 01393 /* Check Tx Mailbox 1 status */ 01394 if ((hcan->Instance->TSR & CAN_TSR_TME1) != 0U) 01395 { 01396 freelevel++; 01397 } 01398 01399 /* Check Tx Mailbox 2 status */ 01400 if ((hcan->Instance->TSR & CAN_TSR_TME2) != 0U) 01401 { 01402 freelevel++; 01403 } 01404 } 01405 01406 /* Return Tx Mailboxes free level */ 01407 return freelevel; 01408 } 01409 01410 /** 01411 * @brief Check if a transmission request is pending on the selected Tx 01412 * Mailboxes. 01413 * @param hcan pointer to an CAN_HandleTypeDef structure that contains 01414 * the configuration information for the specified CAN. 01415 * @param TxMailboxes List of Tx Mailboxes to check. 01416 * This parameter can be any combination of @arg CAN_Tx_Mailboxes. 01417 * @retval Status 01418 * - 0 : No pending transmission request on any selected Tx Mailboxes. 01419 * - 1 : Pending transmission request on at least one of the selected 01420 * Tx Mailbox. 01421 */ 01422 uint32_t HAL_CAN_IsTxMessagePending(CAN_HandleTypeDef *hcan, uint32_t TxMailboxes) 01423 { 01424 uint32_t status = 0U; 01425 HAL_CAN_StateTypeDef state = hcan->State; 01426 01427 /* Check function parameters */ 01428 assert_param(IS_CAN_TX_MAILBOX_LIST(TxMailboxes)); 01429 01430 if ((state == HAL_CAN_STATE_READY) || 01431 (state == HAL_CAN_STATE_LISTENING)) 01432 { 01433 /* Check pending transmission request on the selected Tx Mailboxes */ 01434 if ((hcan->Instance->TSR & (TxMailboxes << CAN_TSR_TME0_Pos)) != (TxMailboxes << CAN_TSR_TME0_Pos)) 01435 { 01436 status = 1U; 01437 } 01438 } 01439 01440 /* Return status */ 01441 return status; 01442 } 01443 01444 /** 01445 * @brief Return timestamp of Tx message sent, if time triggered communication 01446 mode is enabled. 01447 * @param hcan pointer to a CAN_HandleTypeDef structure that contains 01448 * the configuration information for the specified CAN. 01449 * @param TxMailbox Tx Mailbox where the timestamp of message sent will be 01450 * read. 01451 * This parameter can be one value of @arg CAN_Tx_Mailboxes. 01452 * @retval Timestamp of message sent from Tx Mailbox. 01453 */ 01454 uint32_t HAL_CAN_GetTxTimestamp(CAN_HandleTypeDef *hcan, uint32_t TxMailbox) 01455 { 01456 uint32_t timestamp = 0U; 01457 uint32_t transmitmailbox; 01458 HAL_CAN_StateTypeDef state = hcan->State; 01459 01460 /* Check function parameters */ 01461 assert_param(IS_CAN_TX_MAILBOX(TxMailbox)); 01462 01463 if ((state == HAL_CAN_STATE_READY) || 01464 (state == HAL_CAN_STATE_LISTENING)) 01465 { 01466 /* Select the Tx mailbox */ 01467 transmitmailbox = POSITION_VAL(TxMailbox); 01468 01469 /* Get timestamp */ 01470 timestamp = (hcan->Instance->sTxMailBox[transmitmailbox].TDTR & CAN_TDT0R_TIME) >> CAN_TDT0R_TIME_Pos; 01471 } 01472 01473 /* Return the timestamp */ 01474 return timestamp; 01475 } 01476 01477 /** 01478 * @brief Get an CAN frame from the Rx FIFO zone into the message RAM. 01479 * @param hcan pointer to an CAN_HandleTypeDef structure that contains 01480 * the configuration information for the specified CAN. 01481 * @param RxFifo Fifo number of the received message to be read. 01482 * This parameter can be a value of @arg CAN_receive_FIFO_number. 01483 * @param pHeader pointer to a CAN_RxHeaderTypeDef structure where the header 01484 * of the Rx frame will be stored. 01485 * @param aData array where the payload of the Rx frame will be stored. 01486 * @retval HAL status 01487 */ 01488 HAL_StatusTypeDef HAL_CAN_GetRxMessage(CAN_HandleTypeDef *hcan, uint32_t RxFifo, CAN_RxHeaderTypeDef *pHeader, uint8_t aData[]) 01489 { 01490 HAL_CAN_StateTypeDef state = hcan->State; 01491 01492 assert_param(IS_CAN_RX_FIFO(RxFifo)); 01493 01494 if ((state == HAL_CAN_STATE_READY) || 01495 (state == HAL_CAN_STATE_LISTENING)) 01496 { 01497 /* Check the Rx FIFO */ 01498 if (RxFifo == CAN_RX_FIFO0) /* Rx element is assigned to Rx FIFO 0 */ 01499 { 01500 /* Check that the Rx FIFO 0 is not empty */ 01501 if ((hcan->Instance->RF0R & CAN_RF0R_FMP0) == 0U) 01502 { 01503 /* Update error code */ 01504 hcan->ErrorCode |= HAL_CAN_ERROR_PARAM; 01505 01506 return HAL_ERROR; 01507 } 01508 } 01509 else /* Rx element is assigned to Rx FIFO 1 */ 01510 { 01511 /* Check that the Rx FIFO 1 is not empty */ 01512 if ((hcan->Instance->RF1R & CAN_RF1R_FMP1) == 0U) 01513 { 01514 /* Update error code */ 01515 hcan->ErrorCode |= HAL_CAN_ERROR_PARAM; 01516 01517 return HAL_ERROR; 01518 } 01519 } 01520 01521 /* Get the header */ 01522 pHeader->IDE = CAN_RI0R_IDE & hcan->Instance->sFIFOMailBox[RxFifo].RIR; 01523 if (pHeader->IDE == CAN_ID_STD) 01524 { 01525 pHeader->StdId = (CAN_RI0R_STID & hcan->Instance->sFIFOMailBox[RxFifo].RIR) >> CAN_TI0R_STID_Pos; 01526 } 01527 else 01528 { 01529 pHeader->ExtId = ((CAN_RI0R_EXID | CAN_RI0R_STID) & hcan->Instance->sFIFOMailBox[RxFifo].RIR) >> CAN_RI0R_EXID_Pos; 01530 } 01531 pHeader->RTR = (CAN_RI0R_RTR & hcan->Instance->sFIFOMailBox[RxFifo].RIR); 01532 pHeader->DLC = (CAN_RDT0R_DLC & hcan->Instance->sFIFOMailBox[RxFifo].RDTR) >> CAN_RDT0R_DLC_Pos; 01533 pHeader->FilterMatchIndex = (CAN_RDT0R_FMI & hcan->Instance->sFIFOMailBox[RxFifo].RDTR) >> CAN_RDT0R_FMI_Pos; 01534 pHeader->Timestamp = (CAN_RDT0R_TIME & hcan->Instance->sFIFOMailBox[RxFifo].RDTR) >> CAN_RDT0R_TIME_Pos; 01535 01536 /* Get the data */ 01537 aData[0] = (uint8_t)((CAN_RDL0R_DATA0 & hcan->Instance->sFIFOMailBox[RxFifo].RDLR) >> CAN_RDL0R_DATA0_Pos); 01538 aData[1] = (uint8_t)((CAN_RDL0R_DATA1 & hcan->Instance->sFIFOMailBox[RxFifo].RDLR) >> CAN_RDL0R_DATA1_Pos); 01539 aData[2] = (uint8_t)((CAN_RDL0R_DATA2 & hcan->Instance->sFIFOMailBox[RxFifo].RDLR) >> CAN_RDL0R_DATA2_Pos); 01540 aData[3] = (uint8_t)((CAN_RDL0R_DATA3 & hcan->Instance->sFIFOMailBox[RxFifo].RDLR) >> CAN_RDL0R_DATA3_Pos); 01541 aData[4] = (uint8_t)((CAN_RDH0R_DATA4 & hcan->Instance->sFIFOMailBox[RxFifo].RDHR) >> CAN_RDH0R_DATA4_Pos); 01542 aData[5] = (uint8_t)((CAN_RDH0R_DATA5 & hcan->Instance->sFIFOMailBox[RxFifo].RDHR) >> CAN_RDH0R_DATA5_Pos); 01543 aData[6] = (uint8_t)((CAN_RDH0R_DATA6 & hcan->Instance->sFIFOMailBox[RxFifo].RDHR) >> CAN_RDH0R_DATA6_Pos); 01544 aData[7] = (uint8_t)((CAN_RDH0R_DATA7 & hcan->Instance->sFIFOMailBox[RxFifo].RDHR) >> CAN_RDH0R_DATA7_Pos); 01545 01546 /* Release the FIFO */ 01547 if (RxFifo == CAN_RX_FIFO0) /* Rx element is assigned to Rx FIFO 0 */ 01548 { 01549 /* Release RX FIFO 0 */ 01550 SET_BIT(hcan->Instance->RF0R, CAN_RF0R_RFOM0); 01551 } 01552 else /* Rx element is assigned to Rx FIFO 1 */ 01553 { 01554 /* Release RX FIFO 1 */ 01555 SET_BIT(hcan->Instance->RF1R, CAN_RF1R_RFOM1); 01556 } 01557 01558 /* Return function status */ 01559 return HAL_OK; 01560 } 01561 else 01562 { 01563 /* Update error code */ 01564 hcan->ErrorCode |= HAL_CAN_ERROR_NOT_INITIALIZED; 01565 01566 return HAL_ERROR; 01567 } 01568 } 01569 01570 /** 01571 * @brief Return Rx FIFO fill level. 01572 * @param hcan pointer to an CAN_HandleTypeDef structure that contains 01573 * the configuration information for the specified CAN. 01574 * @param RxFifo Rx FIFO. 01575 * This parameter can be a value of @arg CAN_receive_FIFO_number. 01576 * @retval Number of messages available in Rx FIFO. 01577 */ 01578 uint32_t HAL_CAN_GetRxFifoFillLevel(CAN_HandleTypeDef *hcan, uint32_t RxFifo) 01579 { 01580 uint32_t filllevel = 0U; 01581 HAL_CAN_StateTypeDef state = hcan->State; 01582 01583 /* Check function parameters */ 01584 assert_param(IS_CAN_RX_FIFO(RxFifo)); 01585 01586 if ((state == HAL_CAN_STATE_READY) || 01587 (state == HAL_CAN_STATE_LISTENING)) 01588 { 01589 if (RxFifo == CAN_RX_FIFO0) 01590 { 01591 filllevel = hcan->Instance->RF0R & CAN_RF0R_FMP0; 01592 } 01593 else /* RxFifo == CAN_RX_FIFO1 */ 01594 { 01595 filllevel = hcan->Instance->RF1R & CAN_RF1R_FMP1; 01596 } 01597 } 01598 01599 /* Return Rx FIFO fill level */ 01600 return filllevel; 01601 } 01602 01603 /** 01604 * @} 01605 */ 01606 01607 /** @defgroup CAN_Exported_Functions_Group4 Interrupts management 01608 * @brief Interrupts management 01609 * 01610 @verbatim 01611 ============================================================================== 01612 ##### Interrupts management ##### 01613 ============================================================================== 01614 [..] This section provides functions allowing to: 01615 (+) HAL_CAN_ActivateNotification : Enable interrupts 01616 (+) HAL_CAN_DeactivateNotification : Disable interrupts 01617 (+) HAL_CAN_IRQHandler : Handles CAN interrupt request 01618 01619 @endverbatim 01620 * @{ 01621 */ 01622 01623 /** 01624 * @brief Enable interrupts. 01625 * @param hcan pointer to an CAN_HandleTypeDef structure that contains 01626 * the configuration information for the specified CAN. 01627 * @param ActiveITs indicates which interrupts will be enabled. 01628 * This parameter can be any combination of @arg CAN_Interrupts. 01629 * @retval HAL status 01630 */ 01631 HAL_StatusTypeDef HAL_CAN_ActivateNotification(CAN_HandleTypeDef *hcan, uint32_t ActiveITs) 01632 { 01633 HAL_CAN_StateTypeDef state = hcan->State; 01634 01635 /* Check function parameters */ 01636 assert_param(IS_CAN_IT(ActiveITs)); 01637 01638 if ((state == HAL_CAN_STATE_READY) || 01639 (state == HAL_CAN_STATE_LISTENING)) 01640 { 01641 /* Enable the selected interrupts */ 01642 __HAL_CAN_ENABLE_IT(hcan, ActiveITs); 01643 01644 /* Return function status */ 01645 return HAL_OK; 01646 } 01647 else 01648 { 01649 /* Update error code */ 01650 hcan->ErrorCode |= HAL_CAN_ERROR_NOT_INITIALIZED; 01651 01652 return HAL_ERROR; 01653 } 01654 } 01655 01656 /** 01657 * @brief Disable interrupts. 01658 * @param hcan pointer to an CAN_HandleTypeDef structure that contains 01659 * the configuration information for the specified CAN. 01660 * @param InactiveITs indicates which interrupts will be disabled. 01661 * This parameter can be any combination of @arg CAN_Interrupts. 01662 * @retval HAL status 01663 */ 01664 HAL_StatusTypeDef HAL_CAN_DeactivateNotification(CAN_HandleTypeDef *hcan, uint32_t InactiveITs) 01665 { 01666 HAL_CAN_StateTypeDef state = hcan->State; 01667 01668 /* Check function parameters */ 01669 assert_param(IS_CAN_IT(InactiveITs)); 01670 01671 if ((state == HAL_CAN_STATE_READY) || 01672 (state == HAL_CAN_STATE_LISTENING)) 01673 { 01674 /* Disable the selected interrupts */ 01675 __HAL_CAN_DISABLE_IT(hcan, InactiveITs); 01676 01677 /* Return function status */ 01678 return HAL_OK; 01679 } 01680 else 01681 { 01682 /* Update error code */ 01683 hcan->ErrorCode |= HAL_CAN_ERROR_NOT_INITIALIZED; 01684 01685 return HAL_ERROR; 01686 } 01687 } 01688 01689 /** 01690 * @brief Handles CAN interrupt request 01691 * @param hcan pointer to a CAN_HandleTypeDef structure that contains 01692 * the configuration information for the specified CAN. 01693 * @retval None 01694 */ 01695 void HAL_CAN_IRQHandler(CAN_HandleTypeDef *hcan) 01696 { 01697 uint32_t errorcode = HAL_CAN_ERROR_NONE; 01698 uint32_t interrupts = READ_REG(hcan->Instance->IER); 01699 uint32_t msrflags = READ_REG(hcan->Instance->MSR); 01700 uint32_t tsrflags = READ_REG(hcan->Instance->TSR); 01701 uint32_t rf0rflags = READ_REG(hcan->Instance->RF0R); 01702 uint32_t rf1rflags = READ_REG(hcan->Instance->RF1R); 01703 uint32_t esrflags = READ_REG(hcan->Instance->ESR); 01704 01705 /* Transmit Mailbox empty interrupt management *****************************/ 01706 if ((interrupts & CAN_IT_TX_MAILBOX_EMPTY) != 0U) 01707 { 01708 /* Transmit Mailbox 0 management *****************************************/ 01709 if ((tsrflags & CAN_TSR_RQCP0) != 0U) 01710 { 01711 /* Clear the Transmission Complete flag (and TXOK0,ALST0,TERR0 bits) */ 01712 __HAL_CAN_CLEAR_FLAG(hcan, CAN_FLAG_RQCP0); 01713 01714 if ((tsrflags & CAN_TSR_TXOK0) != 0U) 01715 { 01716 /* Transmission Mailbox 0 complete callback */ 01717 #if USE_HAL_CAN_REGISTER_CALLBACKS == 1 01718 /* Call registered callback*/ 01719 hcan->TxMailbox0CompleteCallback(hcan); 01720 #else 01721 /* Call weak (surcharged) callback */ 01722 HAL_CAN_TxMailbox0CompleteCallback(hcan); 01723 #endif /* USE_HAL_CAN_REGISTER_CALLBACKS */ 01724 } 01725 else 01726 { 01727 if ((tsrflags & CAN_TSR_ALST0) != 0U) 01728 { 01729 /* Update error code */ 01730 errorcode |= HAL_CAN_ERROR_TX_ALST0; 01731 } 01732 else if ((tsrflags & CAN_TSR_TERR0) != 0U) 01733 { 01734 /* Update error code */ 01735 errorcode |= HAL_CAN_ERROR_TX_TERR0; 01736 } 01737 else 01738 { 01739 /* Transmission Mailbox 0 abort callback */ 01740 #if USE_HAL_CAN_REGISTER_CALLBACKS == 1 01741 /* Call registered callback*/ 01742 hcan->TxMailbox0AbortCallback(hcan); 01743 #else 01744 /* Call weak (surcharged) callback */ 01745 HAL_CAN_TxMailbox0AbortCallback(hcan); 01746 #endif /* USE_HAL_CAN_REGISTER_CALLBACKS */ 01747 } 01748 } 01749 } 01750 01751 /* Transmit Mailbox 1 management *****************************************/ 01752 if ((tsrflags & CAN_TSR_RQCP1) != 0U) 01753 { 01754 /* Clear the Transmission Complete flag (and TXOK1,ALST1,TERR1 bits) */ 01755 __HAL_CAN_CLEAR_FLAG(hcan, CAN_FLAG_RQCP1); 01756 01757 if ((tsrflags & CAN_TSR_TXOK1) != 0U) 01758 { 01759 /* Transmission Mailbox 1 complete callback */ 01760 #if USE_HAL_CAN_REGISTER_CALLBACKS == 1 01761 /* Call registered callback*/ 01762 hcan->TxMailbox1CompleteCallback(hcan); 01763 #else 01764 /* Call weak (surcharged) callback */ 01765 HAL_CAN_TxMailbox1CompleteCallback(hcan); 01766 #endif /* USE_HAL_CAN_REGISTER_CALLBACKS */ 01767 } 01768 else 01769 { 01770 if ((tsrflags & CAN_TSR_ALST1) != 0U) 01771 { 01772 /* Update error code */ 01773 errorcode |= HAL_CAN_ERROR_TX_ALST1; 01774 } 01775 else if ((tsrflags & CAN_TSR_TERR1) != 0U) 01776 { 01777 /* Update error code */ 01778 errorcode |= HAL_CAN_ERROR_TX_TERR1; 01779 } 01780 else 01781 { 01782 /* Transmission Mailbox 1 abort callback */ 01783 #if USE_HAL_CAN_REGISTER_CALLBACKS == 1 01784 /* Call registered callback*/ 01785 hcan->TxMailbox1AbortCallback(hcan); 01786 #else 01787 /* Call weak (surcharged) callback */ 01788 HAL_CAN_TxMailbox1AbortCallback(hcan); 01789 #endif /* USE_HAL_CAN_REGISTER_CALLBACKS */ 01790 } 01791 } 01792 } 01793 01794 /* Transmit Mailbox 2 management *****************************************/ 01795 if ((tsrflags & CAN_TSR_RQCP2) != 0U) 01796 { 01797 /* Clear the Transmission Complete flag (and TXOK2,ALST2,TERR2 bits) */ 01798 __HAL_CAN_CLEAR_FLAG(hcan, CAN_FLAG_RQCP2); 01799 01800 if ((tsrflags & CAN_TSR_TXOK2) != 0U) 01801 { 01802 /* Transmission Mailbox 2 complete callback */ 01803 #if USE_HAL_CAN_REGISTER_CALLBACKS == 1 01804 /* Call registered callback*/ 01805 hcan->TxMailbox2CompleteCallback(hcan); 01806 #else 01807 /* Call weak (surcharged) callback */ 01808 HAL_CAN_TxMailbox2CompleteCallback(hcan); 01809 #endif /* USE_HAL_CAN_REGISTER_CALLBACKS */ 01810 } 01811 else 01812 { 01813 if ((tsrflags & CAN_TSR_ALST2) != 0U) 01814 { 01815 /* Update error code */ 01816 errorcode |= HAL_CAN_ERROR_TX_ALST2; 01817 } 01818 else if ((tsrflags & CAN_TSR_TERR2) != 0U) 01819 { 01820 /* Update error code */ 01821 errorcode |= HAL_CAN_ERROR_TX_TERR2; 01822 } 01823 else 01824 { 01825 /* Transmission Mailbox 2 abort callback */ 01826 #if USE_HAL_CAN_REGISTER_CALLBACKS == 1 01827 /* Call registered callback*/ 01828 hcan->TxMailbox2AbortCallback(hcan); 01829 #else 01830 /* Call weak (surcharged) callback */ 01831 HAL_CAN_TxMailbox2AbortCallback(hcan); 01832 #endif /* USE_HAL_CAN_REGISTER_CALLBACKS */ 01833 } 01834 } 01835 } 01836 } 01837 01838 /* Receive FIFO 0 overrun interrupt management *****************************/ 01839 if ((interrupts & CAN_IT_RX_FIFO0_OVERRUN) != 0U) 01840 { 01841 if ((rf0rflags & CAN_RF0R_FOVR0) != 0U) 01842 { 01843 /* Set CAN error code to Rx Fifo 0 overrun error */ 01844 errorcode |= HAL_CAN_ERROR_RX_FOV0; 01845 01846 /* Clear FIFO0 Overrun Flag */ 01847 __HAL_CAN_CLEAR_FLAG(hcan, CAN_FLAG_FOV0); 01848 } 01849 } 01850 01851 /* Receive FIFO 0 full interrupt management ********************************/ 01852 if ((interrupts & CAN_IT_RX_FIFO0_FULL) != 0U) 01853 { 01854 if ((rf0rflags & CAN_RF0R_FULL0) != 0U) 01855 { 01856 /* Clear FIFO 0 full Flag */ 01857 __HAL_CAN_CLEAR_FLAG(hcan, CAN_FLAG_FF0); 01858 01859 /* Receive FIFO 0 full Callback */ 01860 #if USE_HAL_CAN_REGISTER_CALLBACKS == 1 01861 /* Call registered callback*/ 01862 hcan->RxFifo0FullCallback(hcan); 01863 #else 01864 /* Call weak (surcharged) callback */ 01865 HAL_CAN_RxFifo0FullCallback(hcan); 01866 #endif /* USE_HAL_CAN_REGISTER_CALLBACKS */ 01867 } 01868 } 01869 01870 /* Receive FIFO 0 message pending interrupt management *********************/ 01871 if ((interrupts & CAN_IT_RX_FIFO0_MSG_PENDING) != 0U) 01872 { 01873 /* Check if message is still pending */ 01874 if ((hcan->Instance->RF0R & CAN_RF0R_FMP0) != 0U) 01875 { 01876 /* Receive FIFO 0 message pending Callback */ 01877 #if USE_HAL_CAN_REGISTER_CALLBACKS == 1 01878 /* Call registered callback*/ 01879 hcan->RxFifo0MsgPendingCallback(hcan); 01880 #else 01881 /* Call weak (surcharged) callback */ 01882 HAL_CAN_RxFifo0MsgPendingCallback(hcan); 01883 #endif /* USE_HAL_CAN_REGISTER_CALLBACKS */ 01884 } 01885 } 01886 01887 /* Receive FIFO 1 overrun interrupt management *****************************/ 01888 if ((interrupts & CAN_IT_RX_FIFO1_OVERRUN) != 0U) 01889 { 01890 if ((rf1rflags & CAN_RF1R_FOVR1) != 0U) 01891 { 01892 /* Set CAN error code to Rx Fifo 1 overrun error */ 01893 errorcode |= HAL_CAN_ERROR_RX_FOV1; 01894 01895 /* Clear FIFO1 Overrun Flag */ 01896 __HAL_CAN_CLEAR_FLAG(hcan, CAN_FLAG_FOV1); 01897 } 01898 } 01899 01900 /* Receive FIFO 1 full interrupt management ********************************/ 01901 if ((interrupts & CAN_IT_RX_FIFO1_FULL) != 0U) 01902 { 01903 if ((rf1rflags & CAN_RF1R_FULL1) != 0U) 01904 { 01905 /* Clear FIFO 1 full Flag */ 01906 __HAL_CAN_CLEAR_FLAG(hcan, CAN_FLAG_FF1); 01907 01908 /* Receive FIFO 1 full Callback */ 01909 #if USE_HAL_CAN_REGISTER_CALLBACKS == 1 01910 /* Call registered callback*/ 01911 hcan->RxFifo1FullCallback(hcan); 01912 #else 01913 /* Call weak (surcharged) callback */ 01914 HAL_CAN_RxFifo1FullCallback(hcan); 01915 #endif /* USE_HAL_CAN_REGISTER_CALLBACKS */ 01916 } 01917 } 01918 01919 /* Receive FIFO 1 message pending interrupt management *********************/ 01920 if ((interrupts & CAN_IT_RX_FIFO1_MSG_PENDING) != 0U) 01921 { 01922 /* Check if message is still pending */ 01923 if ((hcan->Instance->RF1R & CAN_RF1R_FMP1) != 0U) 01924 { 01925 /* Receive FIFO 1 message pending Callback */ 01926 #if USE_HAL_CAN_REGISTER_CALLBACKS == 1 01927 /* Call registered callback*/ 01928 hcan->RxFifo1MsgPendingCallback(hcan); 01929 #else 01930 /* Call weak (surcharged) callback */ 01931 HAL_CAN_RxFifo1MsgPendingCallback(hcan); 01932 #endif /* USE_HAL_CAN_REGISTER_CALLBACKS */ 01933 } 01934 } 01935 01936 /* Sleep interrupt management *********************************************/ 01937 if ((interrupts & CAN_IT_SLEEP_ACK) != 0U) 01938 { 01939 if ((msrflags & CAN_MSR_SLAKI) != 0U) 01940 { 01941 /* Clear Sleep interrupt Flag */ 01942 __HAL_CAN_CLEAR_FLAG(hcan, CAN_FLAG_SLAKI); 01943 01944 /* Sleep Callback */ 01945 #if USE_HAL_CAN_REGISTER_CALLBACKS == 1 01946 /* Call registered callback*/ 01947 hcan->SleepCallback(hcan); 01948 #else 01949 /* Call weak (surcharged) callback */ 01950 HAL_CAN_SleepCallback(hcan); 01951 #endif /* USE_HAL_CAN_REGISTER_CALLBACKS */ 01952 } 01953 } 01954 01955 /* WakeUp interrupt management *********************************************/ 01956 if ((interrupts & CAN_IT_WAKEUP) != 0U) 01957 { 01958 if ((msrflags & CAN_MSR_WKUI) != 0U) 01959 { 01960 /* Clear WakeUp Flag */ 01961 __HAL_CAN_CLEAR_FLAG(hcan, CAN_FLAG_WKU); 01962 01963 /* WakeUp Callback */ 01964 #if USE_HAL_CAN_REGISTER_CALLBACKS == 1 01965 /* Call registered callback*/ 01966 hcan->WakeUpFromRxMsgCallback(hcan); 01967 #else 01968 /* Call weak (surcharged) callback */ 01969 HAL_CAN_WakeUpFromRxMsgCallback(hcan); 01970 #endif /* USE_HAL_CAN_REGISTER_CALLBACKS */ 01971 } 01972 } 01973 01974 /* Error interrupts management *********************************************/ 01975 if ((interrupts & CAN_IT_ERROR) != 0U) 01976 { 01977 if ((msrflags & CAN_MSR_ERRI) != 0U) 01978 { 01979 /* Check Error Warning Flag */ 01980 if (((interrupts & CAN_IT_ERROR_WARNING) != 0U) && 01981 ((esrflags & CAN_ESR_EWGF) != 0U)) 01982 { 01983 /* Set CAN error code to Error Warning */ 01984 errorcode |= HAL_CAN_ERROR_EWG; 01985 01986 /* No need for clear of Error Warning Flag as read-only */ 01987 } 01988 01989 /* Check Error Passive Flag */ 01990 if (((interrupts & CAN_IT_ERROR_PASSIVE) != 0U) && 01991 ((esrflags & CAN_ESR_EPVF) != 0U)) 01992 { 01993 /* Set CAN error code to Error Passive */ 01994 errorcode |= HAL_CAN_ERROR_EPV; 01995 01996 /* No need for clear of Error Passive Flag as read-only */ 01997 } 01998 01999 /* Check Bus-off Flag */ 02000 if (((interrupts & CAN_IT_BUSOFF) != 0U) && 02001 ((esrflags & CAN_ESR_BOFF) != 0U)) 02002 { 02003 /* Set CAN error code to Bus-Off */ 02004 errorcode |= HAL_CAN_ERROR_BOF; 02005 02006 /* No need for clear of Error Bus-Off as read-only */ 02007 } 02008 02009 /* Check Last Error Code Flag */ 02010 if (((interrupts & CAN_IT_LAST_ERROR_CODE) != 0U) && 02011 ((esrflags & CAN_ESR_LEC) != 0U)) 02012 { 02013 switch (esrflags & CAN_ESR_LEC) 02014 { 02015 case (CAN_ESR_LEC_0): 02016 /* Set CAN error code to Stuff error */ 02017 errorcode |= HAL_CAN_ERROR_STF; 02018 break; 02019 case (CAN_ESR_LEC_1): 02020 /* Set CAN error code to Form error */ 02021 errorcode |= HAL_CAN_ERROR_FOR; 02022 break; 02023 case (CAN_ESR_LEC_1 | CAN_ESR_LEC_0): 02024 /* Set CAN error code to Acknowledgement error */ 02025 errorcode |= HAL_CAN_ERROR_ACK; 02026 break; 02027 case (CAN_ESR_LEC_2): 02028 /* Set CAN error code to Bit recessive error */ 02029 errorcode |= HAL_CAN_ERROR_BR; 02030 break; 02031 case (CAN_ESR_LEC_2 | CAN_ESR_LEC_0): 02032 /* Set CAN error code to Bit Dominant error */ 02033 errorcode |= HAL_CAN_ERROR_BD; 02034 break; 02035 case (CAN_ESR_LEC_2 | CAN_ESR_LEC_1): 02036 /* Set CAN error code to CRC error */ 02037 errorcode |= HAL_CAN_ERROR_CRC; 02038 break; 02039 default: 02040 break; 02041 } 02042 02043 /* Clear Last error code Flag */ 02044 CLEAR_BIT(hcan->Instance->ESR, CAN_ESR_LEC); 02045 } 02046 } 02047 02048 /* Clear ERRI Flag */ 02049 __HAL_CAN_CLEAR_FLAG(hcan, CAN_FLAG_ERRI); 02050 } 02051 02052 /* Call the Error call Back in case of Errors */ 02053 if (errorcode != HAL_CAN_ERROR_NONE) 02054 { 02055 /* Update error code in handle */ 02056 hcan->ErrorCode |= errorcode; 02057 02058 /* Call Error callback function */ 02059 #if USE_HAL_CAN_REGISTER_CALLBACKS == 1 02060 /* Call registered callback*/ 02061 hcan->ErrorCallback(hcan); 02062 #else 02063 /* Call weak (surcharged) callback */ 02064 HAL_CAN_ErrorCallback(hcan); 02065 #endif /* USE_HAL_CAN_REGISTER_CALLBACKS */ 02066 } 02067 } 02068 02069 /** 02070 * @} 02071 */ 02072 02073 /** @defgroup CAN_Exported_Functions_Group5 Callback functions 02074 * @brief CAN Callback functions 02075 * 02076 @verbatim 02077 ============================================================================== 02078 ##### Callback functions ##### 02079 ============================================================================== 02080 [..] 02081 This subsection provides the following callback functions: 02082 (+) HAL_CAN_TxMailbox0CompleteCallback 02083 (+) HAL_CAN_TxMailbox1CompleteCallback 02084 (+) HAL_CAN_TxMailbox2CompleteCallback 02085 (+) HAL_CAN_TxMailbox0AbortCallback 02086 (+) HAL_CAN_TxMailbox1AbortCallback 02087 (+) HAL_CAN_TxMailbox2AbortCallback 02088 (+) HAL_CAN_RxFifo0MsgPendingCallback 02089 (+) HAL_CAN_RxFifo0FullCallback 02090 (+) HAL_CAN_RxFifo1MsgPendingCallback 02091 (+) HAL_CAN_RxFifo1FullCallback 02092 (+) HAL_CAN_SleepCallback 02093 (+) HAL_CAN_WakeUpFromRxMsgCallback 02094 (+) HAL_CAN_ErrorCallback 02095 02096 @endverbatim 02097 * @{ 02098 */ 02099 02100 /** 02101 * @brief Transmission Mailbox 0 complete callback. 02102 * @param hcan pointer to a CAN_HandleTypeDef structure that contains 02103 * the configuration information for the specified CAN. 02104 * @retval None 02105 */ 02106 __weak void HAL_CAN_TxMailbox0CompleteCallback(CAN_HandleTypeDef *hcan) 02107 { 02108 /* Prevent unused argument(s) compilation warning */ 02109 UNUSED(hcan); 02110 02111 /* NOTE : This function Should not be modified, when the callback is needed, 02112 the HAL_CAN_TxMailbox0CompleteCallback could be implemented in the 02113 user file 02114 */ 02115 } 02116 02117 /** 02118 * @brief Transmission Mailbox 1 complete callback. 02119 * @param hcan pointer to a CAN_HandleTypeDef structure that contains 02120 * the configuration information for the specified CAN. 02121 * @retval None 02122 */ 02123 __weak void HAL_CAN_TxMailbox1CompleteCallback(CAN_HandleTypeDef *hcan) 02124 { 02125 /* Prevent unused argument(s) compilation warning */ 02126 UNUSED(hcan); 02127 02128 /* NOTE : This function Should not be modified, when the callback is needed, 02129 the HAL_CAN_TxMailbox1CompleteCallback could be implemented in the 02130 user file 02131 */ 02132 } 02133 02134 /** 02135 * @brief Transmission Mailbox 2 complete callback. 02136 * @param hcan pointer to a CAN_HandleTypeDef structure that contains 02137 * the configuration information for the specified CAN. 02138 * @retval None 02139 */ 02140 __weak void HAL_CAN_TxMailbox2CompleteCallback(CAN_HandleTypeDef *hcan) 02141 { 02142 /* Prevent unused argument(s) compilation warning */ 02143 UNUSED(hcan); 02144 02145 /* NOTE : This function Should not be modified, when the callback is needed, 02146 the HAL_CAN_TxMailbox2CompleteCallback could be implemented in the 02147 user file 02148 */ 02149 } 02150 02151 /** 02152 * @brief Transmission Mailbox 0 Cancellation callback. 02153 * @param hcan pointer to an CAN_HandleTypeDef structure that contains 02154 * the configuration information for the specified CAN. 02155 * @retval None 02156 */ 02157 __weak void HAL_CAN_TxMailbox0AbortCallback(CAN_HandleTypeDef *hcan) 02158 { 02159 /* Prevent unused argument(s) compilation warning */ 02160 UNUSED(hcan); 02161 02162 /* NOTE : This function Should not be modified, when the callback is needed, 02163 the HAL_CAN_TxMailbox0AbortCallback could be implemented in the 02164 user file 02165 */ 02166 } 02167 02168 /** 02169 * @brief Transmission Mailbox 1 Cancellation callback. 02170 * @param hcan pointer to an CAN_HandleTypeDef structure that contains 02171 * the configuration information for the specified CAN. 02172 * @retval None 02173 */ 02174 __weak void HAL_CAN_TxMailbox1AbortCallback(CAN_HandleTypeDef *hcan) 02175 { 02176 /* Prevent unused argument(s) compilation warning */ 02177 UNUSED(hcan); 02178 02179 /* NOTE : This function Should not be modified, when the callback is needed, 02180 the HAL_CAN_TxMailbox1AbortCallback could be implemented in the 02181 user file 02182 */ 02183 } 02184 02185 /** 02186 * @brief Transmission Mailbox 2 Cancellation callback. 02187 * @param hcan pointer to an CAN_HandleTypeDef structure that contains 02188 * the configuration information for the specified CAN. 02189 * @retval None 02190 */ 02191 __weak void HAL_CAN_TxMailbox2AbortCallback(CAN_HandleTypeDef *hcan) 02192 { 02193 /* Prevent unused argument(s) compilation warning */ 02194 UNUSED(hcan); 02195 02196 /* NOTE : This function Should not be modified, when the callback is needed, 02197 the HAL_CAN_TxMailbox2AbortCallback could be implemented in the 02198 user file 02199 */ 02200 } 02201 02202 /** 02203 * @brief Rx FIFO 0 message pending callback. 02204 * @param hcan pointer to a CAN_HandleTypeDef structure that contains 02205 * the configuration information for the specified CAN. 02206 * @retval None 02207 */ 02208 __weak void HAL_CAN_RxFifo0MsgPendingCallback(CAN_HandleTypeDef *hcan) 02209 { 02210 /* Prevent unused argument(s) compilation warning */ 02211 UNUSED(hcan); 02212 02213 /* NOTE : This function Should not be modified, when the callback is needed, 02214 the HAL_CAN_RxFifo0MsgPendingCallback could be implemented in the 02215 user file 02216 */ 02217 } 02218 02219 /** 02220 * @brief Rx FIFO 0 full callback. 02221 * @param hcan pointer to a CAN_HandleTypeDef structure that contains 02222 * the configuration information for the specified CAN. 02223 * @retval None 02224 */ 02225 __weak void HAL_CAN_RxFifo0FullCallback(CAN_HandleTypeDef *hcan) 02226 { 02227 /* Prevent unused argument(s) compilation warning */ 02228 UNUSED(hcan); 02229 02230 /* NOTE : This function Should not be modified, when the callback is needed, 02231 the HAL_CAN_RxFifo0FullCallback could be implemented in the user 02232 file 02233 */ 02234 } 02235 02236 /** 02237 * @brief Rx FIFO 1 message pending callback. 02238 * @param hcan pointer to a CAN_HandleTypeDef structure that contains 02239 * the configuration information for the specified CAN. 02240 * @retval None 02241 */ 02242 __weak void HAL_CAN_RxFifo1MsgPendingCallback(CAN_HandleTypeDef *hcan) 02243 { 02244 /* Prevent unused argument(s) compilation warning */ 02245 UNUSED(hcan); 02246 02247 /* NOTE : This function Should not be modified, when the callback is needed, 02248 the HAL_CAN_RxFifo1MsgPendingCallback could be implemented in the 02249 user file 02250 */ 02251 } 02252 02253 /** 02254 * @brief Rx FIFO 1 full callback. 02255 * @param hcan pointer to a CAN_HandleTypeDef structure that contains 02256 * the configuration information for the specified CAN. 02257 * @retval None 02258 */ 02259 __weak void HAL_CAN_RxFifo1FullCallback(CAN_HandleTypeDef *hcan) 02260 { 02261 /* Prevent unused argument(s) compilation warning */ 02262 UNUSED(hcan); 02263 02264 /* NOTE : This function Should not be modified, when the callback is needed, 02265 the HAL_CAN_RxFifo1FullCallback could be implemented in the user 02266 file 02267 */ 02268 } 02269 02270 /** 02271 * @brief Sleep callback. 02272 * @param hcan pointer to a CAN_HandleTypeDef structure that contains 02273 * the configuration information for the specified CAN. 02274 * @retval None 02275 */ 02276 __weak void HAL_CAN_SleepCallback(CAN_HandleTypeDef *hcan) 02277 { 02278 /* Prevent unused argument(s) compilation warning */ 02279 UNUSED(hcan); 02280 02281 /* NOTE : This function Should not be modified, when the callback is needed, 02282 the HAL_CAN_SleepCallback could be implemented in the user file 02283 */ 02284 } 02285 02286 /** 02287 * @brief WakeUp from Rx message 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_WakeUpFromRxMsgCallback(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_WakeUpFromRxMsgCallback could be implemented in the 02299 user file 02300 */ 02301 } 02302 02303 /** 02304 * @brief Error CAN callback. 02305 * @param hcan pointer to a CAN_HandleTypeDef structure that contains 02306 * the configuration information for the specified CAN. 02307 * @retval None 02308 */ 02309 __weak void HAL_CAN_ErrorCallback(CAN_HandleTypeDef *hcan) 02310 { 02311 /* Prevent unused argument(s) compilation warning */ 02312 UNUSED(hcan); 02313 02314 /* NOTE : This function Should not be modified, when the callback is needed, 02315 the HAL_CAN_ErrorCallback could be implemented in the user file 02316 */ 02317 } 02318 02319 /** 02320 * @} 02321 */ 02322 02323 /** @defgroup CAN_Exported_Functions_Group6 Peripheral State and Error functions 02324 * @brief CAN Peripheral State functions 02325 * 02326 @verbatim 02327 ============================================================================== 02328 ##### Peripheral State and Error functions ##### 02329 ============================================================================== 02330 [..] 02331 This subsection provides functions allowing to : 02332 (+) HAL_CAN_GetState() : Return the CAN state. 02333 (+) HAL_CAN_GetError() : Return the CAN error codes if any. 02334 (+) HAL_CAN_ResetError(): Reset the CAN error codes if any. 02335 02336 @endverbatim 02337 * @{ 02338 */ 02339 02340 /** 02341 * @brief Return the CAN state. 02342 * @param hcan pointer to a CAN_HandleTypeDef structure that contains 02343 * the configuration information for the specified CAN. 02344 * @retval HAL state 02345 */ 02346 HAL_CAN_StateTypeDef HAL_CAN_GetState(CAN_HandleTypeDef *hcan) 02347 { 02348 HAL_CAN_StateTypeDef state = hcan->State; 02349 02350 if ((state == HAL_CAN_STATE_READY) || 02351 (state == HAL_CAN_STATE_LISTENING)) 02352 { 02353 /* Check sleep mode acknowledge flag */ 02354 if ((hcan->Instance->MSR & CAN_MSR_SLAK) != 0U) 02355 { 02356 /* Sleep mode is active */ 02357 state = HAL_CAN_STATE_SLEEP_ACTIVE; 02358 } 02359 /* Check sleep mode request flag */ 02360 else if ((hcan->Instance->MCR & CAN_MCR_SLEEP) != 0U) 02361 { 02362 /* Sleep mode request is pending */ 02363 state = HAL_CAN_STATE_SLEEP_PENDING; 02364 } 02365 else 02366 { 02367 /* Neither sleep mode request nor sleep mode acknowledge */ 02368 } 02369 } 02370 02371 /* Return CAN state */ 02372 return state; 02373 } 02374 02375 /** 02376 * @brief Return the CAN error code. 02377 * @param hcan pointer to a CAN_HandleTypeDef structure that contains 02378 * the configuration information for the specified CAN. 02379 * @retval CAN Error Code 02380 */ 02381 uint32_t HAL_CAN_GetError(CAN_HandleTypeDef *hcan) 02382 { 02383 /* Return CAN error code */ 02384 return hcan->ErrorCode; 02385 } 02386 02387 /** 02388 * @brief Reset the CAN error code. 02389 * @param hcan pointer to a CAN_HandleTypeDef structure that contains 02390 * the configuration information for the specified CAN. 02391 * @retval HAL status 02392 */ 02393 HAL_StatusTypeDef HAL_CAN_ResetError(CAN_HandleTypeDef *hcan) 02394 { 02395 HAL_StatusTypeDef status = HAL_OK; 02396 HAL_CAN_StateTypeDef state = hcan->State; 02397 02398 if ((state == HAL_CAN_STATE_READY) || 02399 (state == HAL_CAN_STATE_LISTENING)) 02400 { 02401 /* Reset CAN error code */ 02402 hcan->ErrorCode = 0U; 02403 } 02404 else 02405 { 02406 /* Update error code */ 02407 hcan->ErrorCode |= HAL_CAN_ERROR_NOT_INITIALIZED; 02408 02409 status = HAL_ERROR; 02410 } 02411 02412 /* Return the status */ 02413 return status; 02414 } 02415 02416 /** 02417 * @} 02418 */ 02419 02420 /** 02421 * @} 02422 */ 02423 02424 #endif /* HAL_CAN_MODULE_ENABLED */ 02425 02426 /** 02427 * @} 02428 */ 02429 02430 #endif /* CAN1 */ 02431 02432 /** 02433 * @} 02434 */ 02435 02436 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/