STM32L443xx HAL User Manual
|
00001 /** 00002 ****************************************************************************** 00003 * @file stm32l4xx_hal_smartcard.c 00004 * @author MCD Application Team 00005 * @brief SMARTCARD HAL module driver. 00006 * This file provides firmware functions to manage the following 00007 * functionalities of the SMARTCARD peripheral: 00008 * + Initialization and de-initialization functions 00009 * + IO operation functions 00010 * + Peripheral Control functions 00011 * + Peripheral State and Error functions 00012 * 00013 ****************************************************************************** 00014 * @attention 00015 * 00016 * Copyright (c) 2017 STMicroelectronics. 00017 * All rights reserved. 00018 * 00019 * This software is licensed under terms that can be found in the LICENSE file 00020 * in the root directory of this software component. 00021 * If no LICENSE file comes with this software, it is provided AS-IS. 00022 * 00023 ****************************************************************************** 00024 @verbatim 00025 ============================================================================== 00026 ##### How to use this driver ##### 00027 ============================================================================== 00028 [..] 00029 The SMARTCARD HAL driver can be used as follows: 00030 00031 (#) Declare a SMARTCARD_HandleTypeDef handle structure (eg. SMARTCARD_HandleTypeDef hsmartcard). 00032 (#) Associate a USART to the SMARTCARD handle hsmartcard. 00033 (#) Initialize the SMARTCARD low level resources by implementing the HAL_SMARTCARD_MspInit() API: 00034 (++) Enable the USARTx interface clock. 00035 (++) USART pins configuration: 00036 (+++) Enable the clock for the USART GPIOs. 00037 (+++) Configure the USART pins (TX as alternate function pull-up, RX as alternate function Input). 00038 (++) NVIC configuration if you need to use interrupt process (HAL_SMARTCARD_Transmit_IT() 00039 and HAL_SMARTCARD_Receive_IT() APIs): 00040 (+++) Configure the USARTx interrupt priority. 00041 (+++) Enable the NVIC USART IRQ handle. 00042 (++) DMA Configuration if you need to use DMA process (HAL_SMARTCARD_Transmit_DMA() 00043 and HAL_SMARTCARD_Receive_DMA() APIs): 00044 (+++) Declare a DMA handle structure for the Tx/Rx channel. 00045 (+++) Enable the DMAx interface clock. 00046 (+++) Configure the declared DMA handle structure with the required Tx/Rx parameters. 00047 (+++) Configure the DMA Tx/Rx channel. 00048 (+++) Associate the initialized DMA handle to the SMARTCARD DMA Tx/Rx handle. 00049 (+++) Configure the priority and enable the NVIC for the transfer complete 00050 interrupt on the DMA Tx/Rx channel. 00051 00052 (#) Program the Baud Rate, Parity, Mode(Receiver/Transmitter), clock enabling/disabling and accordingly, 00053 the clock parameters (parity, phase, last bit), prescaler value, guard time and NACK on transmission 00054 error enabling or disabling in the hsmartcard handle Init structure. 00055 00056 (#) If required, program SMARTCARD advanced features (TX/RX pins swap, TimeOut, auto-retry counter,...) 00057 in the hsmartcard handle AdvancedInit structure. 00058 00059 (#) Initialize the SMARTCARD registers by calling the HAL_SMARTCARD_Init() API: 00060 (++) This API configures also the low level Hardware GPIO, CLOCK, CORTEX...etc) 00061 by calling the customized HAL_SMARTCARD_MspInit() API. 00062 [..] 00063 (@) The specific SMARTCARD interrupts (Transmission complete interrupt, 00064 RXNE interrupt and Error Interrupts) will be managed using the macros 00065 __HAL_SMARTCARD_ENABLE_IT() and __HAL_SMARTCARD_DISABLE_IT() inside the transmit and receive process. 00066 00067 [..] 00068 [..] Three operation modes are available within this driver : 00069 00070 *** Polling mode IO operation *** 00071 ================================= 00072 [..] 00073 (+) Send an amount of data in blocking mode using HAL_SMARTCARD_Transmit() 00074 (+) Receive an amount of data in blocking mode using HAL_SMARTCARD_Receive() 00075 00076 *** Interrupt mode IO operation *** 00077 =================================== 00078 [..] 00079 (+) Send an amount of data in non-blocking mode using HAL_SMARTCARD_Transmit_IT() 00080 (+) At transmission end of transfer HAL_SMARTCARD_TxCpltCallback() is executed and user can 00081 add his own code by customization of function pointer HAL_SMARTCARD_TxCpltCallback() 00082 (+) Receive an amount of data in non-blocking mode using HAL_SMARTCARD_Receive_IT() 00083 (+) At reception end of transfer HAL_SMARTCARD_RxCpltCallback() is executed and user can 00084 add his own code by customization of function pointer HAL_SMARTCARD_RxCpltCallback() 00085 (+) In case of transfer Error, HAL_SMARTCARD_ErrorCallback() function is executed and user can 00086 add his own code by customization of function pointer HAL_SMARTCARD_ErrorCallback() 00087 00088 *** DMA mode IO operation *** 00089 ============================== 00090 [..] 00091 (+) Send an amount of data in non-blocking mode (DMA) using HAL_SMARTCARD_Transmit_DMA() 00092 (+) At transmission end of transfer HAL_SMARTCARD_TxCpltCallback() is executed and user can 00093 add his own code by customization of function pointer HAL_SMARTCARD_TxCpltCallback() 00094 (+) Receive an amount of data in non-blocking mode (DMA) using HAL_SMARTCARD_Receive_DMA() 00095 (+) At reception end of transfer HAL_SMARTCARD_RxCpltCallback() is executed and user can 00096 add his own code by customization of function pointer HAL_SMARTCARD_RxCpltCallback() 00097 (+) In case of transfer Error, HAL_SMARTCARD_ErrorCallback() function is executed and user can 00098 add his own code by customization of function pointer HAL_SMARTCARD_ErrorCallback() 00099 00100 *** SMARTCARD HAL driver macros list *** 00101 ======================================== 00102 [..] 00103 Below the list of most used macros in SMARTCARD HAL driver. 00104 00105 (+) __HAL_SMARTCARD_GET_FLAG : Check whether or not the specified SMARTCARD flag is set 00106 (+) __HAL_SMARTCARD_CLEAR_FLAG : Clear the specified SMARTCARD pending flag 00107 (+) __HAL_SMARTCARD_ENABLE_IT: Enable the specified SMARTCARD interrupt 00108 (+) __HAL_SMARTCARD_DISABLE_IT: Disable the specified SMARTCARD interrupt 00109 (+) __HAL_SMARTCARD_GET_IT_SOURCE: Check whether or not the specified SMARTCARD interrupt is enabled 00110 00111 [..] 00112 (@) You can refer to the SMARTCARD HAL driver header file for more useful macros 00113 00114 ##### Callback registration ##### 00115 ================================== 00116 00117 [..] 00118 The compilation define USE_HAL_SMARTCARD_REGISTER_CALLBACKS when set to 1 00119 allows the user to configure dynamically the driver callbacks. 00120 00121 [..] 00122 Use Function HAL_SMARTCARD_RegisterCallback() to register a user callback. 00123 Function HAL_SMARTCARD_RegisterCallback() allows to register following callbacks: 00124 (+) TxCpltCallback : Tx Complete Callback. 00125 (+) RxCpltCallback : Rx Complete Callback. 00126 (+) ErrorCallback : Error Callback. 00127 (+) AbortCpltCallback : Abort Complete Callback. 00128 (+) AbortTransmitCpltCallback : Abort Transmit Complete Callback. 00129 (+) AbortReceiveCpltCallback : Abort Receive Complete Callback. 00130 (+) RxFifoFullCallback : Rx Fifo Full Callback. 00131 (+) TxFifoEmptyCallback : Tx Fifo Empty Callback. 00132 (+) MspInitCallback : SMARTCARD MspInit. 00133 (+) MspDeInitCallback : SMARTCARD MspDeInit. 00134 This function takes as parameters the HAL peripheral handle, the Callback ID 00135 and a pointer to the user callback function. 00136 00137 [..] 00138 Use function HAL_SMARTCARD_UnRegisterCallback() to reset a callback to the default 00139 weak (surcharged) function. 00140 HAL_SMARTCARD_UnRegisterCallback() takes as parameters the HAL peripheral handle, 00141 and the Callback ID. 00142 This function allows to reset following callbacks: 00143 (+) TxCpltCallback : Tx Complete Callback. 00144 (+) RxCpltCallback : Rx Complete Callback. 00145 (+) ErrorCallback : Error Callback. 00146 (+) AbortCpltCallback : Abort Complete Callback. 00147 (+) AbortTransmitCpltCallback : Abort Transmit Complete Callback. 00148 (+) AbortReceiveCpltCallback : Abort Receive Complete Callback. 00149 (+) RxFifoFullCallback : Rx Fifo Full Callback. 00150 (+) TxFifoEmptyCallback : Tx Fifo Empty Callback. 00151 (+) MspInitCallback : SMARTCARD MspInit. 00152 (+) MspDeInitCallback : SMARTCARD MspDeInit. 00153 00154 [..] 00155 By default, after the HAL_SMARTCARD_Init() and when the state is HAL_SMARTCARD_STATE_RESET 00156 all callbacks are set to the corresponding weak (surcharged) functions: 00157 examples HAL_SMARTCARD_TxCpltCallback(), HAL_SMARTCARD_RxCpltCallback(). 00158 Exception done for MspInit and MspDeInit functions that are respectively 00159 reset to the legacy weak (surcharged) functions in the HAL_SMARTCARD_Init() 00160 and HAL_SMARTCARD_DeInit() only when these callbacks are null (not registered beforehand). 00161 If not, MspInit or MspDeInit are not null, the HAL_SMARTCARD_Init() and HAL_SMARTCARD_DeInit() 00162 keep and use the user MspInit/MspDeInit callbacks (registered beforehand). 00163 00164 [..] 00165 Callbacks can be registered/unregistered in HAL_SMARTCARD_STATE_READY state only. 00166 Exception done MspInit/MspDeInit that can be registered/unregistered 00167 in HAL_SMARTCARD_STATE_READY or HAL_SMARTCARD_STATE_RESET state, thus registered (user) 00168 MspInit/DeInit callbacks can be used during the Init/DeInit. 00169 In that case first register the MspInit/MspDeInit user callbacks 00170 using HAL_SMARTCARD_RegisterCallback() before calling HAL_SMARTCARD_DeInit() 00171 or HAL_SMARTCARD_Init() function. 00172 00173 [..] 00174 When The compilation define USE_HAL_SMARTCARD_REGISTER_CALLBACKS is set to 0 or 00175 not defined, the callback registration feature is not available 00176 and weak (surcharged) callbacks are used. 00177 00178 00179 @endverbatim 00180 ****************************************************************************** 00181 */ 00182 00183 /* Includes ------------------------------------------------------------------*/ 00184 #include "stm32l4xx_hal.h" 00185 00186 /** @addtogroup STM32L4xx_HAL_Driver 00187 * @{ 00188 */ 00189 00190 /** @defgroup SMARTCARD SMARTCARD 00191 * @brief HAL SMARTCARD module driver 00192 * @{ 00193 */ 00194 00195 #ifdef HAL_SMARTCARD_MODULE_ENABLED 00196 00197 /* Private typedef -----------------------------------------------------------*/ 00198 /* Private define ------------------------------------------------------------*/ 00199 /** @defgroup SMARTCARD_Private_Constants SMARTCARD Private Constants 00200 * @{ 00201 */ 00202 #define SMARTCARD_TEACK_REACK_TIMEOUT 1000U /*!< SMARTCARD TX or RX enable acknowledge time-out value */ 00203 00204 #if defined(USART_CR1_FIFOEN) 00205 #define USART_CR1_FIELDS ((uint32_t)(USART_CR1_M | USART_CR1_PCE | USART_CR1_PS | USART_CR1_TE | \ 00206 USART_CR1_RE | USART_CR1_OVER8| \ 00207 USART_CR1_FIFOEN)) /*!< USART CR1 fields of parameters set by SMARTCARD_SetConfig API */ 00208 #else 00209 #define USART_CR1_FIELDS ((uint32_t)(USART_CR1_M | USART_CR1_PCE | USART_CR1_PS | USART_CR1_TE | \ 00210 USART_CR1_RE | USART_CR1_OVER8)) /*!< USART CR1 fields of parameters set by SMARTCARD_SetConfig API */ 00211 #endif /* USART_CR1_FIFOEN */ 00212 00213 #define USART_CR2_CLK_FIELDS ((uint32_t)(USART_CR2_CLKEN | USART_CR2_CPOL | \ 00214 USART_CR2_CPHA | USART_CR2_LBCL)) /*!< SMARTCARD clock-related USART CR2 fields of parameters */ 00215 00216 #define USART_CR2_FIELDS ((uint32_t)(USART_CR2_RTOEN | USART_CR2_CLK_FIELDS | \ 00217 USART_CR2_STOP)) /*!< USART CR2 fields of parameters set by SMARTCARD_SetConfig API */ 00218 00219 #if defined(USART_CR1_FIFOEN) 00220 #define USART_CR3_FIELDS ((uint32_t)(USART_CR3_ONEBIT | USART_CR3_NACK | USART_CR3_SCARCNT | \ 00221 USART_CR3_TXFTCFG | USART_CR3_RXFTCFG )) /*!< USART CR3 fields of parameters set by SMARTCARD_SetConfig API */ 00222 #else 00223 #define USART_CR3_FIELDS ((uint32_t)(USART_CR3_ONEBIT | USART_CR3_NACK | \ 00224 USART_CR3_SCARCNT)) /*!< USART CR3 fields of parameters set by SMARTCARD_SetConfig API */ 00225 #endif /* USART_CR1_FIFOEN */ 00226 00227 #define USART_BRR_MIN 0x10U /*!< USART BRR minimum authorized value */ 00228 00229 #define USART_BRR_MAX 0x0000FFFFU /*!< USART BRR maximum authorized value */ 00230 /** 00231 * @} 00232 */ 00233 00234 /* Private macros ------------------------------------------------------------*/ 00235 /* Private variables ---------------------------------------------------------*/ 00236 /* Private function prototypes -----------------------------------------------*/ 00237 /** @addtogroup SMARTCARD_Private_Functions 00238 * @{ 00239 */ 00240 #if (USE_HAL_SMARTCARD_REGISTER_CALLBACKS == 1) 00241 void SMARTCARD_InitCallbacksToDefault(SMARTCARD_HandleTypeDef *hsmartcard); 00242 #endif /* USE_HAL_SMARTCARD_REGISTER_CALLBACKS */ 00243 static HAL_StatusTypeDef SMARTCARD_SetConfig(SMARTCARD_HandleTypeDef *hsmartcard); 00244 static void SMARTCARD_AdvFeatureConfig(SMARTCARD_HandleTypeDef *hsmartcard); 00245 static HAL_StatusTypeDef SMARTCARD_CheckIdleState(SMARTCARD_HandleTypeDef *hsmartcard); 00246 static HAL_StatusTypeDef SMARTCARD_WaitOnFlagUntilTimeout(SMARTCARD_HandleTypeDef *hsmartcard, uint32_t Flag, 00247 FlagStatus Status, uint32_t Tickstart, uint32_t Timeout); 00248 static void SMARTCARD_EndTxTransfer(SMARTCARD_HandleTypeDef *hsmartcard); 00249 static void SMARTCARD_EndRxTransfer(SMARTCARD_HandleTypeDef *hsmartcard); 00250 static void SMARTCARD_DMATransmitCplt(DMA_HandleTypeDef *hdma); 00251 static void SMARTCARD_DMAReceiveCplt(DMA_HandleTypeDef *hdma); 00252 static void SMARTCARD_DMAError(DMA_HandleTypeDef *hdma); 00253 static void SMARTCARD_DMAAbortOnError(DMA_HandleTypeDef *hdma); 00254 static void SMARTCARD_DMATxAbortCallback(DMA_HandleTypeDef *hdma); 00255 static void SMARTCARD_DMARxAbortCallback(DMA_HandleTypeDef *hdma); 00256 static void SMARTCARD_DMATxOnlyAbortCallback(DMA_HandleTypeDef *hdma); 00257 static void SMARTCARD_DMARxOnlyAbortCallback(DMA_HandleTypeDef *hdma); 00258 static void SMARTCARD_TxISR(SMARTCARD_HandleTypeDef *hsmartcard); 00259 #if defined(USART_CR1_FIFOEN) 00260 static void SMARTCARD_TxISR_FIFOEN(SMARTCARD_HandleTypeDef *hsmartcard); 00261 #endif /* USART_CR1_FIFOEN */ 00262 static void SMARTCARD_EndTransmit_IT(SMARTCARD_HandleTypeDef *hsmartcard); 00263 static void SMARTCARD_RxISR(SMARTCARD_HandleTypeDef *hsmartcard); 00264 #if defined(USART_CR1_FIFOEN) 00265 static void SMARTCARD_RxISR_FIFOEN(SMARTCARD_HandleTypeDef *hsmartcard); 00266 #endif /* USART_CR1_FIFOEN */ 00267 /** 00268 * @} 00269 */ 00270 00271 /* Exported functions --------------------------------------------------------*/ 00272 00273 /** @defgroup SMARTCARD_Exported_Functions SMARTCARD Exported Functions 00274 * @{ 00275 */ 00276 00277 /** @defgroup SMARTCARD_Exported_Functions_Group1 Initialization and de-initialization functions 00278 * @brief Initialization and Configuration functions 00279 * 00280 @verbatim 00281 ============================================================================== 00282 ##### Initialization and Configuration functions ##### 00283 ============================================================================== 00284 [..] 00285 This subsection provides a set of functions allowing to initialize the USARTx 00286 associated to the SmartCard. 00287 (+) These parameters can be configured: 00288 (++) Baud Rate 00289 (++) Parity: parity should be enabled, frame Length is fixed to 8 bits plus parity 00290 (++) Receiver/transmitter modes 00291 (++) Synchronous mode (and if enabled, phase, polarity and last bit parameters) 00292 (++) Prescaler value 00293 (++) Guard bit time 00294 (++) NACK enabling or disabling on transmission error 00295 00296 (+) The following advanced features can be configured as well: 00297 (++) TX and/or RX pin level inversion 00298 (++) data logical level inversion 00299 (++) RX and TX pins swap 00300 (++) RX overrun detection disabling 00301 (++) DMA disabling on RX error 00302 (++) MSB first on communication line 00303 (++) Time out enabling (and if activated, timeout value) 00304 (++) Block length 00305 (++) Auto-retry counter 00306 [..] 00307 The HAL_SMARTCARD_Init() API follows the USART synchronous configuration procedures 00308 (details for the procedures are available in reference manual). 00309 00310 @endverbatim 00311 00312 The USART frame format is given in the following table: 00313 00314 Table 1. USART frame format. 00315 +---------------------------------------------------------------+ 00316 | M1M0 bits | PCE bit | USART frame | 00317 |-----------------------|---------------------------------------| 00318 | 01 | 1 | | SB | 8 bit data | PB | STB | | 00319 +---------------------------------------------------------------+ 00320 00321 00322 * @{ 00323 */ 00324 00325 /** 00326 * @brief Initialize the SMARTCARD mode according to the specified 00327 * parameters in the SMARTCARD_HandleTypeDef and initialize the associated handle. 00328 * @param hsmartcard Pointer to a SMARTCARD_HandleTypeDef structure that contains 00329 * the configuration information for the specified SMARTCARD module. 00330 * @retval HAL status 00331 */ 00332 HAL_StatusTypeDef HAL_SMARTCARD_Init(SMARTCARD_HandleTypeDef *hsmartcard) 00333 { 00334 /* Check the SMARTCARD handle allocation */ 00335 if (hsmartcard == NULL) 00336 { 00337 return HAL_ERROR; 00338 } 00339 00340 /* Check the USART associated to the SMARTCARD handle */ 00341 assert_param(IS_SMARTCARD_INSTANCE(hsmartcard->Instance)); 00342 00343 if (hsmartcard->gState == HAL_SMARTCARD_STATE_RESET) 00344 { 00345 /* Allocate lock resource and initialize it */ 00346 hsmartcard->Lock = HAL_UNLOCKED; 00347 00348 #if USE_HAL_SMARTCARD_REGISTER_CALLBACKS == 1 00349 SMARTCARD_InitCallbacksToDefault(hsmartcard); 00350 00351 if (hsmartcard->MspInitCallback == NULL) 00352 { 00353 hsmartcard->MspInitCallback = HAL_SMARTCARD_MspInit; 00354 } 00355 00356 /* Init the low level hardware */ 00357 hsmartcard->MspInitCallback(hsmartcard); 00358 #else 00359 /* Init the low level hardware : GPIO, CLOCK */ 00360 HAL_SMARTCARD_MspInit(hsmartcard); 00361 #endif /* USE_HAL_SMARTCARD_REGISTER_CALLBACKS */ 00362 } 00363 00364 hsmartcard->gState = HAL_SMARTCARD_STATE_BUSY; 00365 00366 /* Disable the Peripheral to set smartcard mode */ 00367 CLEAR_BIT(hsmartcard->Instance->CR1, USART_CR1_UE); 00368 00369 /* In SmartCard mode, the following bits must be kept cleared: 00370 - LINEN in the USART_CR2 register, 00371 - HDSEL and IREN bits in the USART_CR3 register.*/ 00372 CLEAR_BIT(hsmartcard->Instance->CR2, USART_CR2_LINEN); 00373 CLEAR_BIT(hsmartcard->Instance->CR3, (USART_CR3_HDSEL | USART_CR3_IREN)); 00374 00375 /* set the USART in SMARTCARD mode */ 00376 SET_BIT(hsmartcard->Instance->CR3, USART_CR3_SCEN); 00377 00378 /* Set the SMARTCARD Communication parameters */ 00379 if (SMARTCARD_SetConfig(hsmartcard) == HAL_ERROR) 00380 { 00381 return HAL_ERROR; 00382 } 00383 00384 /* Set the SMARTCARD transmission completion indication */ 00385 SMARTCARD_TRANSMISSION_COMPLETION_SETTING(hsmartcard); 00386 00387 if (hsmartcard->AdvancedInit.AdvFeatureInit != SMARTCARD_ADVFEATURE_NO_INIT) 00388 { 00389 SMARTCARD_AdvFeatureConfig(hsmartcard); 00390 } 00391 00392 /* Enable the Peripheral */ 00393 SET_BIT(hsmartcard->Instance->CR1, USART_CR1_UE); 00394 00395 /* TEACK and/or REACK to check before moving hsmartcard->gState and hsmartcard->RxState to Ready */ 00396 return (SMARTCARD_CheckIdleState(hsmartcard)); 00397 } 00398 00399 /** 00400 * @brief DeInitialize the SMARTCARD peripheral. 00401 * @param hsmartcard Pointer to a SMARTCARD_HandleTypeDef structure that contains 00402 * the configuration information for the specified SMARTCARD module. 00403 * @retval HAL status 00404 */ 00405 HAL_StatusTypeDef HAL_SMARTCARD_DeInit(SMARTCARD_HandleTypeDef *hsmartcard) 00406 { 00407 /* Check the SMARTCARD handle allocation */ 00408 if (hsmartcard == NULL) 00409 { 00410 return HAL_ERROR; 00411 } 00412 00413 /* Check the USART/UART associated to the SMARTCARD handle */ 00414 assert_param(IS_SMARTCARD_INSTANCE(hsmartcard->Instance)); 00415 00416 hsmartcard->gState = HAL_SMARTCARD_STATE_BUSY; 00417 00418 /* Disable the Peripheral */ 00419 CLEAR_BIT(hsmartcard->Instance->CR1, USART_CR1_UE); 00420 00421 WRITE_REG(hsmartcard->Instance->CR1, 0x0U); 00422 WRITE_REG(hsmartcard->Instance->CR2, 0x0U); 00423 WRITE_REG(hsmartcard->Instance->CR3, 0x0U); 00424 WRITE_REG(hsmartcard->Instance->RTOR, 0x0U); 00425 WRITE_REG(hsmartcard->Instance->GTPR, 0x0U); 00426 00427 /* DeInit the low level hardware */ 00428 #if USE_HAL_SMARTCARD_REGISTER_CALLBACKS == 1 00429 if (hsmartcard->MspDeInitCallback == NULL) 00430 { 00431 hsmartcard->MspDeInitCallback = HAL_SMARTCARD_MspDeInit; 00432 } 00433 /* DeInit the low level hardware */ 00434 hsmartcard->MspDeInitCallback(hsmartcard); 00435 #else 00436 HAL_SMARTCARD_MspDeInit(hsmartcard); 00437 #endif /* USE_HAL_SMARTCARD_REGISTER_CALLBACKS */ 00438 00439 hsmartcard->ErrorCode = HAL_SMARTCARD_ERROR_NONE; 00440 hsmartcard->gState = HAL_SMARTCARD_STATE_RESET; 00441 hsmartcard->RxState = HAL_SMARTCARD_STATE_RESET; 00442 00443 /* Process Unlock */ 00444 __HAL_UNLOCK(hsmartcard); 00445 00446 return HAL_OK; 00447 } 00448 00449 /** 00450 * @brief Initialize the SMARTCARD MSP. 00451 * @param hsmartcard Pointer to a SMARTCARD_HandleTypeDef structure that contains 00452 * the configuration information for the specified SMARTCARD module. 00453 * @retval None 00454 */ 00455 __weak void HAL_SMARTCARD_MspInit(SMARTCARD_HandleTypeDef *hsmartcard) 00456 { 00457 /* Prevent unused argument(s) compilation warning */ 00458 UNUSED(hsmartcard); 00459 00460 /* NOTE : This function should not be modified, when the callback is needed, 00461 the HAL_SMARTCARD_MspInit can be implemented in the user file 00462 */ 00463 } 00464 00465 /** 00466 * @brief DeInitialize the SMARTCARD MSP. 00467 * @param hsmartcard Pointer to a SMARTCARD_HandleTypeDef structure that contains 00468 * the configuration information for the specified SMARTCARD module. 00469 * @retval None 00470 */ 00471 __weak void HAL_SMARTCARD_MspDeInit(SMARTCARD_HandleTypeDef *hsmartcard) 00472 { 00473 /* Prevent unused argument(s) compilation warning */ 00474 UNUSED(hsmartcard); 00475 00476 /* NOTE : This function should not be modified, when the callback is needed, 00477 the HAL_SMARTCARD_MspDeInit can be implemented in the user file 00478 */ 00479 } 00480 00481 #if (USE_HAL_SMARTCARD_REGISTER_CALLBACKS == 1) 00482 /** 00483 * @brief Register a User SMARTCARD Callback 00484 * To be used instead of the weak predefined callback 00485 * @param hsmartcard smartcard handle 00486 * @param CallbackID ID of the callback to be registered 00487 * This parameter can be one of the following values: 00488 * @arg @ref HAL_SMARTCARD_TX_COMPLETE_CB_ID Tx Complete Callback ID 00489 * @arg @ref HAL_SMARTCARD_RX_COMPLETE_CB_ID Rx Complete Callback ID 00490 * @arg @ref HAL_SMARTCARD_ERROR_CB_ID Error Callback ID 00491 * @arg @ref HAL_SMARTCARD_ABORT_COMPLETE_CB_ID Abort Complete Callback ID 00492 * @arg @ref HAL_SMARTCARD_ABORT_TRANSMIT_COMPLETE_CB_ID Abort Transmit Complete Callback ID 00493 * @arg @ref HAL_SMARTCARD_ABORT_RECEIVE_COMPLETE_CB_ID Abort Receive Complete Callback ID 00494 * @arg @ref HAL_SMARTCARD_RX_FIFO_FULL_CB_ID Rx Fifo Full Callback ID 00495 * @arg @ref HAL_SMARTCARD_TX_FIFO_EMPTY_CB_ID Tx Fifo Empty Callback ID 00496 * @arg @ref HAL_SMARTCARD_MSPINIT_CB_ID MspInit Callback ID 00497 * @arg @ref HAL_SMARTCARD_MSPDEINIT_CB_ID MspDeInit Callback ID 00498 * @param pCallback pointer to the Callback function 00499 * @retval HAL status 00500 */ 00501 HAL_StatusTypeDef HAL_SMARTCARD_RegisterCallback(SMARTCARD_HandleTypeDef *hsmartcard, 00502 HAL_SMARTCARD_CallbackIDTypeDef CallbackID, 00503 pSMARTCARD_CallbackTypeDef pCallback) 00504 { 00505 HAL_StatusTypeDef status = HAL_OK; 00506 00507 if (pCallback == NULL) 00508 { 00509 /* Update the error code */ 00510 hsmartcard->ErrorCode |= HAL_SMARTCARD_ERROR_INVALID_CALLBACK; 00511 00512 return HAL_ERROR; 00513 } 00514 /* Process locked */ 00515 __HAL_LOCK(hsmartcard); 00516 00517 if (hsmartcard->gState == HAL_SMARTCARD_STATE_READY) 00518 { 00519 switch (CallbackID) 00520 { 00521 00522 case HAL_SMARTCARD_TX_COMPLETE_CB_ID : 00523 hsmartcard->TxCpltCallback = pCallback; 00524 break; 00525 00526 case HAL_SMARTCARD_RX_COMPLETE_CB_ID : 00527 hsmartcard->RxCpltCallback = pCallback; 00528 break; 00529 00530 case HAL_SMARTCARD_ERROR_CB_ID : 00531 hsmartcard->ErrorCallback = pCallback; 00532 break; 00533 00534 case HAL_SMARTCARD_ABORT_COMPLETE_CB_ID : 00535 hsmartcard->AbortCpltCallback = pCallback; 00536 break; 00537 00538 case HAL_SMARTCARD_ABORT_TRANSMIT_COMPLETE_CB_ID : 00539 hsmartcard->AbortTransmitCpltCallback = pCallback; 00540 break; 00541 00542 case HAL_SMARTCARD_ABORT_RECEIVE_COMPLETE_CB_ID : 00543 hsmartcard->AbortReceiveCpltCallback = pCallback; 00544 break; 00545 00546 #if defined(USART_CR1_FIFOEN) 00547 case HAL_SMARTCARD_RX_FIFO_FULL_CB_ID : 00548 hsmartcard->RxFifoFullCallback = pCallback; 00549 break; 00550 00551 case HAL_SMARTCARD_TX_FIFO_EMPTY_CB_ID : 00552 hsmartcard->TxFifoEmptyCallback = pCallback; 00553 break; 00554 #endif /* USART_CR1_FIFOEN */ 00555 00556 case HAL_SMARTCARD_MSPINIT_CB_ID : 00557 hsmartcard->MspInitCallback = pCallback; 00558 break; 00559 00560 case HAL_SMARTCARD_MSPDEINIT_CB_ID : 00561 hsmartcard->MspDeInitCallback = pCallback; 00562 break; 00563 00564 default : 00565 /* Update the error code */ 00566 hsmartcard->ErrorCode |= HAL_SMARTCARD_ERROR_INVALID_CALLBACK; 00567 00568 /* Return error status */ 00569 status = HAL_ERROR; 00570 break; 00571 } 00572 } 00573 else if (hsmartcard->gState == HAL_SMARTCARD_STATE_RESET) 00574 { 00575 switch (CallbackID) 00576 { 00577 case HAL_SMARTCARD_MSPINIT_CB_ID : 00578 hsmartcard->MspInitCallback = pCallback; 00579 break; 00580 00581 case HAL_SMARTCARD_MSPDEINIT_CB_ID : 00582 hsmartcard->MspDeInitCallback = pCallback; 00583 break; 00584 00585 default : 00586 /* Update the error code */ 00587 hsmartcard->ErrorCode |= HAL_SMARTCARD_ERROR_INVALID_CALLBACK; 00588 00589 /* Return error status */ 00590 status = HAL_ERROR; 00591 break; 00592 } 00593 } 00594 else 00595 { 00596 /* Update the error code */ 00597 hsmartcard->ErrorCode |= HAL_SMARTCARD_ERROR_INVALID_CALLBACK; 00598 00599 /* Return error status */ 00600 status = HAL_ERROR; 00601 } 00602 00603 /* Release Lock */ 00604 __HAL_UNLOCK(hsmartcard); 00605 00606 return status; 00607 } 00608 00609 /** 00610 * @brief Unregister an SMARTCARD callback 00611 * SMARTCARD callback is redirected to the weak predefined callback 00612 * @param hsmartcard smartcard handle 00613 * @param CallbackID ID of the callback to be unregistered 00614 * This parameter can be one of the following values: 00615 * @arg @ref HAL_SMARTCARD_TX_COMPLETE_CB_ID Tx Complete Callback ID 00616 * @arg @ref HAL_SMARTCARD_RX_COMPLETE_CB_ID Rx Complete Callback ID 00617 * @arg @ref HAL_SMARTCARD_ERROR_CB_ID Error Callback ID 00618 * @arg @ref HAL_SMARTCARD_ABORT_COMPLETE_CB_ID Abort Complete Callback ID 00619 * @arg @ref HAL_SMARTCARD_ABORT_TRANSMIT_COMPLETE_CB_ID Abort Transmit Complete Callback ID 00620 * @arg @ref HAL_SMARTCARD_ABORT_RECEIVE_COMPLETE_CB_ID Abort Receive Complete Callback ID 00621 * @arg @ref HAL_SMARTCARD_RX_FIFO_FULL_CB_ID Rx Fifo Full Callback ID 00622 * @arg @ref HAL_SMARTCARD_TX_FIFO_EMPTY_CB_ID Tx Fifo Empty Callback ID 00623 * @arg @ref HAL_SMARTCARD_MSPINIT_CB_ID MspInit Callback ID 00624 * @arg @ref HAL_SMARTCARD_MSPDEINIT_CB_ID MspDeInit Callback ID 00625 * @retval HAL status 00626 */ 00627 HAL_StatusTypeDef HAL_SMARTCARD_UnRegisterCallback(SMARTCARD_HandleTypeDef *hsmartcard, 00628 HAL_SMARTCARD_CallbackIDTypeDef CallbackID) 00629 { 00630 HAL_StatusTypeDef status = HAL_OK; 00631 00632 /* Process locked */ 00633 __HAL_LOCK(hsmartcard); 00634 00635 if (HAL_SMARTCARD_STATE_READY == hsmartcard->gState) 00636 { 00637 switch (CallbackID) 00638 { 00639 case HAL_SMARTCARD_TX_COMPLETE_CB_ID : 00640 hsmartcard->TxCpltCallback = HAL_SMARTCARD_TxCpltCallback; /* Legacy weak TxCpltCallback */ 00641 break; 00642 00643 case HAL_SMARTCARD_RX_COMPLETE_CB_ID : 00644 hsmartcard->RxCpltCallback = HAL_SMARTCARD_RxCpltCallback; /* Legacy weak RxCpltCallback */ 00645 break; 00646 00647 case HAL_SMARTCARD_ERROR_CB_ID : 00648 hsmartcard->ErrorCallback = HAL_SMARTCARD_ErrorCallback; /* Legacy weak ErrorCallback */ 00649 break; 00650 00651 case HAL_SMARTCARD_ABORT_COMPLETE_CB_ID : 00652 hsmartcard->AbortCpltCallback = HAL_SMARTCARD_AbortCpltCallback; /* Legacy weak AbortCpltCallback */ 00653 break; 00654 00655 case HAL_SMARTCARD_ABORT_TRANSMIT_COMPLETE_CB_ID : 00656 hsmartcard->AbortTransmitCpltCallback = HAL_SMARTCARD_AbortTransmitCpltCallback; /* Legacy weak 00657 AbortTransmitCpltCallback*/ 00658 break; 00659 00660 case HAL_SMARTCARD_ABORT_RECEIVE_COMPLETE_CB_ID : 00661 hsmartcard->AbortReceiveCpltCallback = HAL_SMARTCARD_AbortReceiveCpltCallback; /* Legacy weak 00662 AbortReceiveCpltCallback */ 00663 break; 00664 00665 #if defined(USART_CR1_FIFOEN) 00666 case HAL_SMARTCARD_RX_FIFO_FULL_CB_ID : 00667 hsmartcard->RxFifoFullCallback = HAL_SMARTCARDEx_RxFifoFullCallback; /* Legacy weak RxFifoFullCallback */ 00668 break; 00669 00670 case HAL_SMARTCARD_TX_FIFO_EMPTY_CB_ID : 00671 hsmartcard->TxFifoEmptyCallback = HAL_SMARTCARDEx_TxFifoEmptyCallback; /* Legacy weak TxFifoEmptyCallback */ 00672 break; 00673 #endif /* USART_CR1_FIFOEN */ 00674 00675 case HAL_SMARTCARD_MSPINIT_CB_ID : 00676 hsmartcard->MspInitCallback = HAL_SMARTCARD_MspInit; /* Legacy weak MspInitCallback */ 00677 break; 00678 00679 case HAL_SMARTCARD_MSPDEINIT_CB_ID : 00680 hsmartcard->MspDeInitCallback = HAL_SMARTCARD_MspDeInit; /* Legacy weak MspDeInitCallback */ 00681 break; 00682 00683 default : 00684 /* Update the error code */ 00685 hsmartcard->ErrorCode |= HAL_SMARTCARD_ERROR_INVALID_CALLBACK; 00686 00687 /* Return error status */ 00688 status = HAL_ERROR; 00689 break; 00690 } 00691 } 00692 else if (HAL_SMARTCARD_STATE_RESET == hsmartcard->gState) 00693 { 00694 switch (CallbackID) 00695 { 00696 case HAL_SMARTCARD_MSPINIT_CB_ID : 00697 hsmartcard->MspInitCallback = HAL_SMARTCARD_MspInit; 00698 break; 00699 00700 case HAL_SMARTCARD_MSPDEINIT_CB_ID : 00701 hsmartcard->MspDeInitCallback = HAL_SMARTCARD_MspDeInit; 00702 break; 00703 00704 default : 00705 /* Update the error code */ 00706 hsmartcard->ErrorCode |= HAL_SMARTCARD_ERROR_INVALID_CALLBACK; 00707 00708 /* Return error status */ 00709 status = HAL_ERROR; 00710 break; 00711 } 00712 } 00713 else 00714 { 00715 /* Update the error code */ 00716 hsmartcard->ErrorCode |= HAL_SMARTCARD_ERROR_INVALID_CALLBACK; 00717 00718 /* Return error status */ 00719 status = HAL_ERROR; 00720 } 00721 00722 /* Release Lock */ 00723 __HAL_UNLOCK(hsmartcard); 00724 00725 return status; 00726 } 00727 #endif /* USE_HAL_SMARTCARD_REGISTER_CALLBACKS */ 00728 00729 /** 00730 * @} 00731 */ 00732 00733 /** @defgroup SMARTCARD_Exported_Functions_Group2 IO operation functions 00734 * @brief SMARTCARD Transmit and Receive functions 00735 * 00736 @verbatim 00737 ============================================================================== 00738 ##### IO operation functions ##### 00739 ============================================================================== 00740 [..] 00741 This subsection provides a set of functions allowing to manage the SMARTCARD data transfers. 00742 00743 [..] 00744 Smartcard is a single wire half duplex communication protocol. 00745 The Smartcard interface is designed to support asynchronous protocol Smartcards as 00746 defined in the ISO 7816-3 standard. The USART should be configured as: 00747 (+) 8 bits plus parity: where M=1 and PCE=1 in the USART_CR1 register 00748 (+) 1.5 stop bits when transmitting and receiving: where STOP=11 in the USART_CR2 register. 00749 00750 [..] 00751 (#) There are two modes of transfer: 00752 (##) Blocking mode: The communication is performed in polling mode. 00753 The HAL status of all data processing is returned by the same function 00754 after finishing transfer. 00755 (##) Non-Blocking mode: The communication is performed using Interrupts 00756 or DMA, the relevant API's return the HAL status. 00757 The end of the data processing will be indicated through the 00758 dedicated SMARTCARD IRQ when using Interrupt mode or the DMA IRQ when 00759 using DMA mode. 00760 (##) The HAL_SMARTCARD_TxCpltCallback(), HAL_SMARTCARD_RxCpltCallback() user callbacks 00761 will be executed respectively at the end of the Transmit or Receive process 00762 The HAL_SMARTCARD_ErrorCallback() user callback will be executed when a communication 00763 error is detected. 00764 00765 (#) Blocking mode APIs are : 00766 (##) HAL_SMARTCARD_Transmit() 00767 (##) HAL_SMARTCARD_Receive() 00768 00769 (#) Non Blocking mode APIs with Interrupt are : 00770 (##) HAL_SMARTCARD_Transmit_IT() 00771 (##) HAL_SMARTCARD_Receive_IT() 00772 (##) HAL_SMARTCARD_IRQHandler() 00773 00774 (#) Non Blocking mode functions with DMA are : 00775 (##) HAL_SMARTCARD_Transmit_DMA() 00776 (##) HAL_SMARTCARD_Receive_DMA() 00777 00778 (#) A set of Transfer Complete Callbacks are provided in non Blocking mode: 00779 (##) HAL_SMARTCARD_TxCpltCallback() 00780 (##) HAL_SMARTCARD_RxCpltCallback() 00781 (##) HAL_SMARTCARD_ErrorCallback() 00782 00783 [..] 00784 (#) Non-Blocking mode transfers could be aborted using Abort API's : 00785 (##) HAL_SMARTCARD_Abort() 00786 (##) HAL_SMARTCARD_AbortTransmit() 00787 (##) HAL_SMARTCARD_AbortReceive() 00788 (##) HAL_SMARTCARD_Abort_IT() 00789 (##) HAL_SMARTCARD_AbortTransmit_IT() 00790 (##) HAL_SMARTCARD_AbortReceive_IT() 00791 00792 (#) For Abort services based on interrupts (HAL_SMARTCARD_Abortxxx_IT), 00793 a set of Abort Complete Callbacks are provided: 00794 (##) HAL_SMARTCARD_AbortCpltCallback() 00795 (##) HAL_SMARTCARD_AbortTransmitCpltCallback() 00796 (##) HAL_SMARTCARD_AbortReceiveCpltCallback() 00797 00798 (#) In Non-Blocking mode transfers, possible errors are split into 2 categories. 00799 Errors are handled as follows : 00800 (##) Error is considered as Recoverable and non blocking : Transfer could go till end, but error severity is 00801 to be evaluated by user : this concerns Frame Error, 00802 Parity Error or Noise Error in Interrupt mode reception . 00803 Received character is then retrieved and stored in Rx buffer, 00804 Error code is set to allow user to identify error type, 00805 and HAL_SMARTCARD_ErrorCallback() user callback is executed. Transfer is kept ongoing on SMARTCARD side. 00806 If user wants to abort it, Abort services should be called by user. 00807 (##) Error is considered as Blocking : Transfer could not be completed properly and is aborted. 00808 This concerns Frame Error in Interrupt mode transmission, Overrun Error in Interrupt 00809 mode reception and all errors in DMA mode. 00810 Error code is set to allow user to identify error type, 00811 and HAL_SMARTCARD_ErrorCallback() user callback is executed. 00812 00813 @endverbatim 00814 * @{ 00815 */ 00816 00817 /** 00818 * @brief Send an amount of data in blocking mode. 00819 * @note When FIFO mode is enabled, writing a data in the TDR register adds one 00820 * data to the TXFIFO. Write operations to the TDR register are performed 00821 * when TXFNF flag is set. From hardware perspective, TXFNF flag and 00822 * TXE are mapped on the same bit-field. 00823 * @param hsmartcard Pointer to a SMARTCARD_HandleTypeDef structure that contains 00824 * the configuration information for the specified SMARTCARD module. 00825 * @param pData pointer to data buffer. 00826 * @param Size amount of data to be sent. 00827 * @param Timeout Timeout duration. 00828 * @retval HAL status 00829 */ 00830 HAL_StatusTypeDef HAL_SMARTCARD_Transmit(SMARTCARD_HandleTypeDef *hsmartcard, const uint8_t *pData, uint16_t Size, 00831 uint32_t Timeout) 00832 { 00833 uint32_t tickstart; 00834 const uint8_t *ptmpdata = pData; 00835 00836 /* Check that a Tx process is not already ongoing */ 00837 if (hsmartcard->gState == HAL_SMARTCARD_STATE_READY) 00838 { 00839 if ((ptmpdata == NULL) || (Size == 0U)) 00840 { 00841 return HAL_ERROR; 00842 } 00843 00844 /* Process Locked */ 00845 __HAL_LOCK(hsmartcard); 00846 00847 hsmartcard->gState = HAL_SMARTCARD_STATE_BUSY_TX; 00848 00849 /* Init tickstart for timeout management */ 00850 tickstart = HAL_GetTick(); 00851 00852 /* Disable the Peripheral first to update mode for TX master */ 00853 CLEAR_BIT(hsmartcard->Instance->CR1, USART_CR1_UE); 00854 00855 /* In case of TX only mode, if NACK is enabled, the USART must be able to monitor 00856 the bidirectional line to detect a NACK signal in case of parity error. 00857 Therefore, the receiver block must be enabled as well (RE bit must be set). */ 00858 if ((hsmartcard->Init.Mode == SMARTCARD_MODE_TX) 00859 && (hsmartcard->Init.NACKEnable == SMARTCARD_NACK_ENABLE)) 00860 { 00861 SET_BIT(hsmartcard->Instance->CR1, USART_CR1_RE); 00862 } 00863 /* Enable Tx */ 00864 SET_BIT(hsmartcard->Instance->CR1, USART_CR1_TE); 00865 00866 /* Enable the Peripheral */ 00867 SET_BIT(hsmartcard->Instance->CR1, USART_CR1_UE); 00868 00869 /* Perform a TX/RX FIFO Flush */ 00870 __HAL_SMARTCARD_FLUSH_DRREGISTER(hsmartcard); 00871 00872 hsmartcard->ErrorCode = HAL_SMARTCARD_ERROR_NONE; 00873 hsmartcard->TxXferSize = Size; 00874 hsmartcard->TxXferCount = Size; 00875 00876 while (hsmartcard->TxXferCount > 0U) 00877 { 00878 hsmartcard->TxXferCount--; 00879 if (SMARTCARD_WaitOnFlagUntilTimeout(hsmartcard, SMARTCARD_FLAG_TXE, RESET, tickstart, Timeout) != HAL_OK) 00880 { 00881 return HAL_TIMEOUT; 00882 } 00883 hsmartcard->Instance->TDR = (uint8_t)(*ptmpdata & 0xFFU); 00884 ptmpdata++; 00885 } 00886 if (SMARTCARD_WaitOnFlagUntilTimeout(hsmartcard, SMARTCARD_TRANSMISSION_COMPLETION_FLAG(hsmartcard), RESET, 00887 tickstart, Timeout) != HAL_OK) 00888 { 00889 return HAL_TIMEOUT; 00890 } 00891 00892 /* Disable the Peripheral first to update mode */ 00893 CLEAR_BIT(hsmartcard->Instance->CR1, USART_CR1_UE); 00894 if ((hsmartcard->Init.Mode == SMARTCARD_MODE_TX) 00895 && (hsmartcard->Init.NACKEnable == SMARTCARD_NACK_ENABLE)) 00896 { 00897 /* In case of TX only mode, if NACK is enabled, receiver block has been enabled 00898 for Transmit phase. Disable this receiver block. */ 00899 CLEAR_BIT(hsmartcard->Instance->CR1, USART_CR1_RE); 00900 } 00901 if ((hsmartcard->Init.Mode == SMARTCARD_MODE_TX_RX) 00902 || (hsmartcard->Init.NACKEnable == SMARTCARD_NACK_ENABLE)) 00903 { 00904 /* Perform a TX FIFO Flush at end of Tx phase, as all sent bytes are appearing in Rx Data register */ 00905 __HAL_SMARTCARD_FLUSH_DRREGISTER(hsmartcard); 00906 } 00907 SET_BIT(hsmartcard->Instance->CR1, USART_CR1_UE); 00908 00909 /* At end of Tx process, restore hsmartcard->gState to Ready */ 00910 hsmartcard->gState = HAL_SMARTCARD_STATE_READY; 00911 00912 /* Process Unlocked */ 00913 __HAL_UNLOCK(hsmartcard); 00914 00915 return HAL_OK; 00916 } 00917 else 00918 { 00919 return HAL_BUSY; 00920 } 00921 } 00922 00923 /** 00924 * @brief Receive an amount of data in blocking mode. 00925 * @note When FIFO mode is enabled, the RXFNE flag is set as long as the RXFIFO 00926 * is not empty. Read operations from the RDR register are performed when 00927 * RXFNE flag is set. From hardware perspective, RXFNE flag and 00928 * RXNE are mapped on the same bit-field. 00929 * @param hsmartcard Pointer to a SMARTCARD_HandleTypeDef structure that contains 00930 * the configuration information for the specified SMARTCARD module. 00931 * @param pData pointer to data buffer. 00932 * @param Size amount of data to be received. 00933 * @param Timeout Timeout duration. 00934 * @retval HAL status 00935 */ 00936 HAL_StatusTypeDef HAL_SMARTCARD_Receive(SMARTCARD_HandleTypeDef *hsmartcard, uint8_t *pData, uint16_t Size, 00937 uint32_t Timeout) 00938 { 00939 uint32_t tickstart; 00940 uint8_t *ptmpdata = pData; 00941 00942 /* Check that a Rx process is not already ongoing */ 00943 if (hsmartcard->RxState == HAL_SMARTCARD_STATE_READY) 00944 { 00945 if ((ptmpdata == NULL) || (Size == 0U)) 00946 { 00947 return HAL_ERROR; 00948 } 00949 00950 /* Process Locked */ 00951 __HAL_LOCK(hsmartcard); 00952 00953 hsmartcard->ErrorCode = HAL_SMARTCARD_ERROR_NONE; 00954 hsmartcard->RxState = HAL_SMARTCARD_STATE_BUSY_RX; 00955 00956 /* Init tickstart for timeout management */ 00957 tickstart = HAL_GetTick(); 00958 00959 hsmartcard->RxXferSize = Size; 00960 hsmartcard->RxXferCount = Size; 00961 00962 /* Check the remain data to be received */ 00963 while (hsmartcard->RxXferCount > 0U) 00964 { 00965 hsmartcard->RxXferCount--; 00966 00967 if (SMARTCARD_WaitOnFlagUntilTimeout(hsmartcard, SMARTCARD_FLAG_RXNE, RESET, tickstart, Timeout) != HAL_OK) 00968 { 00969 return HAL_TIMEOUT; 00970 } 00971 *ptmpdata = (uint8_t)(hsmartcard->Instance->RDR & (uint8_t)0x00FF); 00972 ptmpdata++; 00973 } 00974 00975 /* At end of Rx process, restore hsmartcard->RxState to Ready */ 00976 hsmartcard->RxState = HAL_SMARTCARD_STATE_READY; 00977 00978 /* Process Unlocked */ 00979 __HAL_UNLOCK(hsmartcard); 00980 00981 return HAL_OK; 00982 } 00983 else 00984 { 00985 return HAL_BUSY; 00986 } 00987 } 00988 00989 /** 00990 * @brief Send an amount of data in interrupt mode. 00991 * @note When FIFO mode is disabled, USART interrupt is generated whenever 00992 * USART_TDR register is empty, i.e one interrupt per data to transmit. 00993 * @note When FIFO mode is enabled, USART interrupt is generated whenever 00994 * TXFIFO threshold reached. In that case the interrupt rate depends on 00995 * TXFIFO threshold configuration. 00996 * @note This function sets the hsmartcard->TxIsr function pointer according to 00997 * the FIFO mode (data transmission processing depends on FIFO mode). 00998 * @param hsmartcard Pointer to a SMARTCARD_HandleTypeDef structure that contains 00999 * the configuration information for the specified SMARTCARD module. 01000 * @param pData pointer to data buffer. 01001 * @param Size amount of data to be sent. 01002 * @retval HAL status 01003 */ 01004 HAL_StatusTypeDef HAL_SMARTCARD_Transmit_IT(SMARTCARD_HandleTypeDef *hsmartcard, const uint8_t *pData, uint16_t Size) 01005 { 01006 /* Check that a Tx process is not already ongoing */ 01007 if (hsmartcard->gState == HAL_SMARTCARD_STATE_READY) 01008 { 01009 if ((pData == NULL) || (Size == 0U)) 01010 { 01011 return HAL_ERROR; 01012 } 01013 01014 /* Process Locked */ 01015 __HAL_LOCK(hsmartcard); 01016 01017 hsmartcard->ErrorCode = HAL_SMARTCARD_ERROR_NONE; 01018 hsmartcard->gState = HAL_SMARTCARD_STATE_BUSY_TX; 01019 01020 hsmartcard->pTxBuffPtr = pData; 01021 hsmartcard->TxXferSize = Size; 01022 hsmartcard->TxXferCount = Size; 01023 hsmartcard->TxISR = NULL; 01024 01025 /* Disable the Peripheral first to update mode for TX master */ 01026 CLEAR_BIT(hsmartcard->Instance->CR1, USART_CR1_UE); 01027 01028 /* In case of TX only mode, if NACK is enabled, the USART must be able to monitor 01029 the bidirectional line to detect a NACK signal in case of parity error. 01030 Therefore, the receiver block must be enabled as well (RE bit must be set). */ 01031 if ((hsmartcard->Init.Mode == SMARTCARD_MODE_TX) 01032 && (hsmartcard->Init.NACKEnable == SMARTCARD_NACK_ENABLE)) 01033 { 01034 SET_BIT(hsmartcard->Instance->CR1, USART_CR1_RE); 01035 } 01036 /* Enable Tx */ 01037 SET_BIT(hsmartcard->Instance->CR1, USART_CR1_TE); 01038 01039 /* Enable the Peripheral */ 01040 SET_BIT(hsmartcard->Instance->CR1, USART_CR1_UE); 01041 01042 /* Perform a TX/RX FIFO Flush */ 01043 __HAL_SMARTCARD_FLUSH_DRREGISTER(hsmartcard); 01044 01045 /* Configure Tx interrupt processing */ 01046 #if defined(USART_CR1_FIFOEN) 01047 if (hsmartcard->FifoMode == SMARTCARD_FIFOMODE_ENABLE) 01048 { 01049 /* Set the Tx ISR function pointer */ 01050 hsmartcard->TxISR = SMARTCARD_TxISR_FIFOEN; 01051 01052 /* Process Unlocked */ 01053 __HAL_UNLOCK(hsmartcard); 01054 01055 /* Enable the SMARTCARD Error Interrupt: (Frame error) */ 01056 SET_BIT(hsmartcard->Instance->CR3, USART_CR3_EIE); 01057 01058 /* Enable the TX FIFO threshold interrupt */ 01059 SET_BIT(hsmartcard->Instance->CR3, USART_CR3_TXFTIE); 01060 } 01061 else 01062 { 01063 /* Set the Tx ISR function pointer */ 01064 hsmartcard->TxISR = SMARTCARD_TxISR; 01065 01066 /* Process Unlocked */ 01067 __HAL_UNLOCK(hsmartcard); 01068 01069 /* Enable the SMARTCARD Error Interrupt: (Frame error) */ 01070 SET_BIT(hsmartcard->Instance->CR3, USART_CR3_EIE); 01071 01072 /* Enable the SMARTCARD Transmit Data Register Empty Interrupt */ 01073 SET_BIT(hsmartcard->Instance->CR1, USART_CR1_TXEIE_TXFNFIE); 01074 } 01075 #else 01076 /* Set the Tx ISR function pointer */ 01077 hsmartcard->TxISR = SMARTCARD_TxISR; 01078 01079 /* Process Unlocked */ 01080 __HAL_UNLOCK(hsmartcard); 01081 01082 /* Enable the SMARTCARD Error Interrupt: (Frame error) */ 01083 SET_BIT(hsmartcard->Instance->CR3, USART_CR3_EIE); 01084 01085 /* Enable the SMARTCARD Transmit Data Register Empty Interrupt */ 01086 SET_BIT(hsmartcard->Instance->CR1, USART_CR1_TXEIE); 01087 #endif /* USART_CR1_FIFOEN */ 01088 01089 return HAL_OK; 01090 } 01091 else 01092 { 01093 return HAL_BUSY; 01094 } 01095 } 01096 01097 /** 01098 * @brief Receive an amount of data in interrupt mode. 01099 * @note When FIFO mode is disabled, USART interrupt is generated whenever 01100 * USART_RDR register can be read, i.e one interrupt per data to receive. 01101 * @note When FIFO mode is enabled, USART interrupt is generated whenever 01102 * RXFIFO threshold reached. In that case the interrupt rate depends on 01103 * RXFIFO threshold configuration. 01104 * @note This function sets the hsmartcard->RxIsr function pointer according to 01105 * the FIFO mode (data reception processing depends on FIFO mode). 01106 * @param hsmartcard Pointer to a SMARTCARD_HandleTypeDef structure that contains 01107 * the configuration information for the specified SMARTCARD module. 01108 * @param pData pointer to data buffer. 01109 * @param Size amount of data to be received. 01110 * @retval HAL status 01111 */ 01112 HAL_StatusTypeDef HAL_SMARTCARD_Receive_IT(SMARTCARD_HandleTypeDef *hsmartcard, uint8_t *pData, uint16_t Size) 01113 { 01114 /* Check that a Rx process is not already ongoing */ 01115 if (hsmartcard->RxState == HAL_SMARTCARD_STATE_READY) 01116 { 01117 if ((pData == NULL) || (Size == 0U)) 01118 { 01119 return HAL_ERROR; 01120 } 01121 01122 /* Process Locked */ 01123 __HAL_LOCK(hsmartcard); 01124 01125 hsmartcard->ErrorCode = HAL_SMARTCARD_ERROR_NONE; 01126 hsmartcard->RxState = HAL_SMARTCARD_STATE_BUSY_RX; 01127 01128 hsmartcard->pRxBuffPtr = pData; 01129 hsmartcard->RxXferSize = Size; 01130 hsmartcard->RxXferCount = Size; 01131 01132 /* Configure Rx interrupt processing */ 01133 #if defined(USART_CR1_FIFOEN) 01134 if ((hsmartcard->FifoMode == SMARTCARD_FIFOMODE_ENABLE) && (Size >= hsmartcard->NbRxDataToProcess)) 01135 { 01136 /* Set the Rx ISR function pointer */ 01137 hsmartcard->RxISR = SMARTCARD_RxISR_FIFOEN; 01138 01139 /* Process Unlocked */ 01140 __HAL_UNLOCK(hsmartcard); 01141 01142 /* Enable the SMARTCART Parity Error interrupt and RX FIFO Threshold interrupt */ 01143 SET_BIT(hsmartcard->Instance->CR1, USART_CR1_PEIE); 01144 SET_BIT(hsmartcard->Instance->CR3, USART_CR3_RXFTIE); 01145 } 01146 else 01147 { 01148 /* Set the Rx ISR function pointer */ 01149 hsmartcard->RxISR = SMARTCARD_RxISR; 01150 01151 /* Process Unlocked */ 01152 __HAL_UNLOCK(hsmartcard); 01153 01154 /* Enable the SMARTCARD Parity Error and Data Register not empty Interrupts */ 01155 SET_BIT(hsmartcard->Instance->CR1, USART_CR1_PEIE | USART_CR1_RXNEIE_RXFNEIE); 01156 } 01157 #else 01158 /* Set the Rx ISR function pointer */ 01159 hsmartcard->RxISR = SMARTCARD_RxISR; 01160 01161 /* Process Unlocked */ 01162 __HAL_UNLOCK(hsmartcard); 01163 01164 /* Enable the SMARTCARD Parity Error and Data Register not empty Interrupts */ 01165 SET_BIT(hsmartcard->Instance->CR1, USART_CR1_PEIE | USART_CR1_RXNEIE); 01166 #endif /* USART_CR1_FIFOEN */ 01167 01168 /* Enable the SMARTCARD Error Interrupt: (Frame error, noise error, overrun error) */ 01169 SET_BIT(hsmartcard->Instance->CR3, USART_CR3_EIE); 01170 01171 return HAL_OK; 01172 } 01173 else 01174 { 01175 return HAL_BUSY; 01176 } 01177 } 01178 01179 /** 01180 * @brief Send an amount of data in DMA mode. 01181 * @param hsmartcard Pointer to a SMARTCARD_HandleTypeDef structure that contains 01182 * the configuration information for the specified SMARTCARD module. 01183 * @param pData pointer to data buffer. 01184 * @param Size amount of data to be sent. 01185 * @retval HAL status 01186 */ 01187 HAL_StatusTypeDef HAL_SMARTCARD_Transmit_DMA(SMARTCARD_HandleTypeDef *hsmartcard, const uint8_t *pData, uint16_t Size) 01188 { 01189 /* Check that a Tx process is not already ongoing */ 01190 if (hsmartcard->gState == HAL_SMARTCARD_STATE_READY) 01191 { 01192 if ((pData == NULL) || (Size == 0U)) 01193 { 01194 return HAL_ERROR; 01195 } 01196 01197 /* Process Locked */ 01198 __HAL_LOCK(hsmartcard); 01199 01200 hsmartcard->gState = HAL_SMARTCARD_STATE_BUSY_TX; 01201 01202 hsmartcard->ErrorCode = HAL_SMARTCARD_ERROR_NONE; 01203 hsmartcard->pTxBuffPtr = pData; 01204 hsmartcard->TxXferSize = Size; 01205 hsmartcard->TxXferCount = Size; 01206 01207 /* Disable the Peripheral first to update mode for TX master */ 01208 CLEAR_BIT(hsmartcard->Instance->CR1, USART_CR1_UE); 01209 01210 /* In case of TX only mode, if NACK is enabled, the USART must be able to monitor 01211 the bidirectional line to detect a NACK signal in case of parity error. 01212 Therefore, the receiver block must be enabled as well (RE bit must be set). */ 01213 if ((hsmartcard->Init.Mode == SMARTCARD_MODE_TX) 01214 && (hsmartcard->Init.NACKEnable == SMARTCARD_NACK_ENABLE)) 01215 { 01216 SET_BIT(hsmartcard->Instance->CR1, USART_CR1_RE); 01217 } 01218 /* Enable Tx */ 01219 SET_BIT(hsmartcard->Instance->CR1, USART_CR1_TE); 01220 01221 /* Enable the Peripheral */ 01222 SET_BIT(hsmartcard->Instance->CR1, USART_CR1_UE); 01223 01224 /* Perform a TX/RX FIFO Flush */ 01225 __HAL_SMARTCARD_FLUSH_DRREGISTER(hsmartcard); 01226 01227 /* Set the SMARTCARD DMA transfer complete callback */ 01228 hsmartcard->hdmatx->XferCpltCallback = SMARTCARD_DMATransmitCplt; 01229 01230 /* Set the SMARTCARD error callback */ 01231 hsmartcard->hdmatx->XferErrorCallback = SMARTCARD_DMAError; 01232 01233 /* Set the DMA abort callback */ 01234 hsmartcard->hdmatx->XferAbortCallback = NULL; 01235 01236 /* Enable the SMARTCARD transmit DMA channel */ 01237 if (HAL_DMA_Start_IT(hsmartcard->hdmatx, (uint32_t)hsmartcard->pTxBuffPtr, (uint32_t)&hsmartcard->Instance->TDR, 01238 Size) == HAL_OK) 01239 { 01240 /* Clear the TC flag in the ICR register */ 01241 CLEAR_BIT(hsmartcard->Instance->ICR, USART_ICR_TCCF); 01242 01243 /* Process Unlocked */ 01244 __HAL_UNLOCK(hsmartcard); 01245 01246 /* Enable the UART Error Interrupt: (Frame error) */ 01247 SET_BIT(hsmartcard->Instance->CR3, USART_CR3_EIE); 01248 01249 /* Enable the DMA transfer for transmit request by setting the DMAT bit 01250 in the SMARTCARD associated USART CR3 register */ 01251 SET_BIT(hsmartcard->Instance->CR3, USART_CR3_DMAT); 01252 01253 return HAL_OK; 01254 } 01255 else 01256 { 01257 /* Set error code to DMA */ 01258 hsmartcard->ErrorCode = HAL_SMARTCARD_ERROR_DMA; 01259 01260 /* Process Unlocked */ 01261 __HAL_UNLOCK(hsmartcard); 01262 01263 /* Restore hsmartcard->State to ready */ 01264 hsmartcard->gState = HAL_SMARTCARD_STATE_READY; 01265 01266 return HAL_ERROR; 01267 } 01268 } 01269 else 01270 { 01271 return HAL_BUSY; 01272 } 01273 } 01274 01275 /** 01276 * @brief Receive an amount of data in DMA mode. 01277 * @param hsmartcard Pointer to a SMARTCARD_HandleTypeDef structure that contains 01278 * the configuration information for the specified SMARTCARD module. 01279 * @param pData pointer to data buffer. 01280 * @param Size amount of data to be received. 01281 * @note The SMARTCARD-associated USART parity is enabled (PCE = 1), 01282 * the received data contain the parity bit (MSB position). 01283 * @retval HAL status 01284 */ 01285 HAL_StatusTypeDef HAL_SMARTCARD_Receive_DMA(SMARTCARD_HandleTypeDef *hsmartcard, uint8_t *pData, uint16_t Size) 01286 { 01287 /* Check that a Rx process is not already ongoing */ 01288 if (hsmartcard->RxState == HAL_SMARTCARD_STATE_READY) 01289 { 01290 if ((pData == NULL) || (Size == 0U)) 01291 { 01292 return HAL_ERROR; 01293 } 01294 01295 /* Process Locked */ 01296 __HAL_LOCK(hsmartcard); 01297 01298 hsmartcard->ErrorCode = HAL_SMARTCARD_ERROR_NONE; 01299 hsmartcard->RxState = HAL_SMARTCARD_STATE_BUSY_RX; 01300 01301 hsmartcard->pRxBuffPtr = pData; 01302 hsmartcard->RxXferSize = Size; 01303 01304 /* Set the SMARTCARD DMA transfer complete callback */ 01305 hsmartcard->hdmarx->XferCpltCallback = SMARTCARD_DMAReceiveCplt; 01306 01307 /* Set the SMARTCARD DMA error callback */ 01308 hsmartcard->hdmarx->XferErrorCallback = SMARTCARD_DMAError; 01309 01310 /* Set the DMA abort callback */ 01311 hsmartcard->hdmarx->XferAbortCallback = NULL; 01312 01313 /* Enable the DMA channel */ 01314 if (HAL_DMA_Start_IT(hsmartcard->hdmarx, (uint32_t)&hsmartcard->Instance->RDR, (uint32_t)hsmartcard->pRxBuffPtr, 01315 Size) == HAL_OK) 01316 { 01317 /* Process Unlocked */ 01318 __HAL_UNLOCK(hsmartcard); 01319 01320 /* Enable the SMARTCARD Parity Error Interrupt */ 01321 SET_BIT(hsmartcard->Instance->CR1, USART_CR1_PEIE); 01322 01323 /* Enable the SMARTCARD Error Interrupt: (Frame error, noise error, overrun error) */ 01324 SET_BIT(hsmartcard->Instance->CR3, USART_CR3_EIE); 01325 01326 /* Enable the DMA transfer for the receiver request by setting the DMAR bit 01327 in the SMARTCARD associated USART CR3 register */ 01328 SET_BIT(hsmartcard->Instance->CR3, USART_CR3_DMAR); 01329 01330 return HAL_OK; 01331 } 01332 else 01333 { 01334 /* Set error code to DMA */ 01335 hsmartcard->ErrorCode = HAL_SMARTCARD_ERROR_DMA; 01336 01337 /* Process Unlocked */ 01338 __HAL_UNLOCK(hsmartcard); 01339 01340 /* Restore hsmartcard->State to ready */ 01341 hsmartcard->RxState = HAL_SMARTCARD_STATE_READY; 01342 01343 return HAL_ERROR; 01344 } 01345 } 01346 else 01347 { 01348 return HAL_BUSY; 01349 } 01350 } 01351 01352 /** 01353 * @brief Abort ongoing transfers (blocking mode). 01354 * @param hsmartcard Pointer to a SMARTCARD_HandleTypeDef structure that contains 01355 * the configuration information for the specified SMARTCARD module. 01356 * @note This procedure could be used for aborting any ongoing transfer started in Interrupt or DMA mode. 01357 * This procedure performs following operations : 01358 * - Disable SMARTCARD Interrupts (Tx and Rx) 01359 * - Disable the DMA transfer in the peripheral register (if enabled) 01360 * - Abort DMA transfer by calling HAL_DMA_Abort (in case of transfer in DMA mode) 01361 * - Set handle State to READY 01362 * @note This procedure is executed in blocking mode : when exiting function, Abort is considered as completed. 01363 * @retval HAL status 01364 */ 01365 HAL_StatusTypeDef HAL_SMARTCARD_Abort(SMARTCARD_HandleTypeDef *hsmartcard) 01366 { 01367 #if defined(USART_CR1_FIFOEN) 01368 /* Disable RTOIE, EOBIE, TXEIE, TCIE, RXNE, PE, RXFT, TXFT and 01369 ERR (Frame error, noise error, overrun error) interrupts */ 01370 CLEAR_BIT(hsmartcard->Instance->CR1, 01371 (USART_CR1_RXNEIE_RXFNEIE | USART_CR1_PEIE | USART_CR1_TXEIE_TXFNFIE | USART_CR1_TCIE | USART_CR1_RTOIE | 01372 USART_CR1_EOBIE)); 01373 CLEAR_BIT(hsmartcard->Instance->CR3, (USART_CR3_EIE | USART_CR3_RXFTIE | USART_CR3_TXFTIE)); 01374 #else 01375 /* Disable RTOIE, EOBIE, TXEIE, TCIE, RXNE, PE and ERR (Frame error, noise error, overrun error) interrupts */ 01376 CLEAR_BIT(hsmartcard->Instance->CR1, 01377 (USART_CR1_RXNEIE | USART_CR1_PEIE | USART_CR1_TXEIE | USART_CR1_TCIE | USART_CR1_RTOIE | 01378 USART_CR1_EOBIE)); 01379 CLEAR_BIT(hsmartcard->Instance->CR3, USART_CR3_EIE); 01380 #endif /* USART_CR1_FIFOEN */ 01381 01382 /* Disable the SMARTCARD DMA Tx request if enabled */ 01383 if (HAL_IS_BIT_SET(hsmartcard->Instance->CR3, USART_CR3_DMAT)) 01384 { 01385 CLEAR_BIT(hsmartcard->Instance->CR3, USART_CR3_DMAT); 01386 01387 /* Abort the SMARTCARD DMA Tx channel : use blocking DMA Abort API (no callback) */ 01388 if (hsmartcard->hdmatx != NULL) 01389 { 01390 /* Set the SMARTCARD DMA Abort callback to Null. 01391 No call back execution at end of DMA abort procedure */ 01392 hsmartcard->hdmatx->XferAbortCallback = NULL; 01393 01394 if (HAL_DMA_Abort(hsmartcard->hdmatx) != HAL_OK) 01395 { 01396 if (HAL_DMA_GetError(hsmartcard->hdmatx) == HAL_DMA_ERROR_TIMEOUT) 01397 { 01398 /* Set error code to DMA */ 01399 hsmartcard->ErrorCode = HAL_SMARTCARD_ERROR_DMA; 01400 01401 return HAL_TIMEOUT; 01402 } 01403 } 01404 } 01405 } 01406 01407 /* Disable the SMARTCARD DMA Rx request if enabled */ 01408 if (HAL_IS_BIT_SET(hsmartcard->Instance->CR3, USART_CR3_DMAR)) 01409 { 01410 CLEAR_BIT(hsmartcard->Instance->CR3, USART_CR3_DMAR); 01411 01412 /* Abort the SMARTCARD DMA Rx channel : use blocking DMA Abort API (no callback) */ 01413 if (hsmartcard->hdmarx != NULL) 01414 { 01415 /* Set the SMARTCARD DMA Abort callback to Null. 01416 No call back execution at end of DMA abort procedure */ 01417 hsmartcard->hdmarx->XferAbortCallback = NULL; 01418 01419 if (HAL_DMA_Abort(hsmartcard->hdmarx) != HAL_OK) 01420 { 01421 if (HAL_DMA_GetError(hsmartcard->hdmarx) == HAL_DMA_ERROR_TIMEOUT) 01422 { 01423 /* Set error code to DMA */ 01424 hsmartcard->ErrorCode = HAL_SMARTCARD_ERROR_DMA; 01425 01426 return HAL_TIMEOUT; 01427 } 01428 } 01429 } 01430 } 01431 01432 /* Reset Tx and Rx transfer counters */ 01433 hsmartcard->TxXferCount = 0U; 01434 hsmartcard->RxXferCount = 0U; 01435 01436 /* Clear the Error flags in the ICR register */ 01437 __HAL_SMARTCARD_CLEAR_FLAG(hsmartcard, 01438 SMARTCARD_CLEAR_OREF | SMARTCARD_CLEAR_NEF | SMARTCARD_CLEAR_PEF | SMARTCARD_CLEAR_FEF | 01439 SMARTCARD_CLEAR_RTOF | SMARTCARD_CLEAR_EOBF); 01440 01441 /* Restore hsmartcard->gState and hsmartcard->RxState to Ready */ 01442 hsmartcard->gState = HAL_SMARTCARD_STATE_READY; 01443 hsmartcard->RxState = HAL_SMARTCARD_STATE_READY; 01444 01445 /* Reset Handle ErrorCode to No Error */ 01446 hsmartcard->ErrorCode = HAL_SMARTCARD_ERROR_NONE; 01447 01448 return HAL_OK; 01449 } 01450 01451 /** 01452 * @brief Abort ongoing Transmit transfer (blocking mode). 01453 * @param hsmartcard Pointer to a SMARTCARD_HandleTypeDef structure that contains 01454 * the configuration information for the specified SMARTCARD module. 01455 * @note This procedure could be used for aborting any ongoing Tx transfer started in Interrupt or DMA mode. 01456 * This procedure performs following operations : 01457 * - Disable SMARTCARD Interrupts (Tx) 01458 * - Disable the DMA transfer in the peripheral register (if enabled) 01459 * - Abort DMA transfer by calling HAL_DMA_Abort (in case of transfer in DMA mode) 01460 * - Set handle State to READY 01461 * @note This procedure is executed in blocking mode : when exiting function, Abort is considered as completed. 01462 * @retval HAL status 01463 */ 01464 HAL_StatusTypeDef HAL_SMARTCARD_AbortTransmit(SMARTCARD_HandleTypeDef *hsmartcard) 01465 { 01466 #if defined(USART_CR1_FIFOEN) 01467 /* Disable TCIE, TXEIE and TXFTIE interrupts */ 01468 CLEAR_BIT(hsmartcard->Instance->CR1, (USART_CR1_TXEIE_TXFNFIE | USART_CR1_TCIE)); 01469 CLEAR_BIT(hsmartcard->Instance->CR3, USART_CR3_TXFTIE); 01470 #else 01471 /* Disable TXEIE and TCIE interrupts */ 01472 CLEAR_BIT(hsmartcard->Instance->CR1, (USART_CR1_TXEIE | USART_CR1_TCIE)); 01473 #endif /* USART_CR1_FIFOEN */ 01474 01475 /* Check if a receive process is ongoing or not. If not disable ERR IT */ 01476 if (hsmartcard->RxState == HAL_SMARTCARD_STATE_READY) 01477 { 01478 /* Disable the SMARTCARD Error Interrupt: (Frame error) */ 01479 CLEAR_BIT(hsmartcard->Instance->CR3, USART_CR3_EIE); 01480 } 01481 01482 /* Disable the SMARTCARD DMA Tx request if enabled */ 01483 if (HAL_IS_BIT_SET(hsmartcard->Instance->CR3, USART_CR3_DMAT)) 01484 { 01485 CLEAR_BIT(hsmartcard->Instance->CR3, USART_CR3_DMAT); 01486 01487 /* Abort the SMARTCARD DMA Tx channel : use blocking DMA Abort API (no callback) */ 01488 if (hsmartcard->hdmatx != NULL) 01489 { 01490 /* Set the SMARTCARD DMA Abort callback to Null. 01491 No call back execution at end of DMA abort procedure */ 01492 hsmartcard->hdmatx->XferAbortCallback = NULL; 01493 01494 if (HAL_DMA_Abort(hsmartcard->hdmatx) != HAL_OK) 01495 { 01496 if (HAL_DMA_GetError(hsmartcard->hdmatx) == HAL_DMA_ERROR_TIMEOUT) 01497 { 01498 /* Set error code to DMA */ 01499 hsmartcard->ErrorCode = HAL_SMARTCARD_ERROR_DMA; 01500 01501 return HAL_TIMEOUT; 01502 } 01503 } 01504 } 01505 } 01506 01507 /* Reset Tx transfer counter */ 01508 hsmartcard->TxXferCount = 0U; 01509 01510 /* Clear the Error flags in the ICR register */ 01511 __HAL_SMARTCARD_CLEAR_FLAG(hsmartcard, SMARTCARD_CLEAR_FEF); 01512 01513 /* Restore hsmartcard->gState to Ready */ 01514 hsmartcard->gState = HAL_SMARTCARD_STATE_READY; 01515 01516 return HAL_OK; 01517 } 01518 01519 /** 01520 * @brief Abort ongoing Receive transfer (blocking mode). 01521 * @param hsmartcard Pointer to a SMARTCARD_HandleTypeDef structure that contains 01522 * the configuration information for the specified SMARTCARD module. 01523 * @note This procedure could be used for aborting any ongoing Rx transfer started in Interrupt or DMA mode. 01524 * This procedure performs following operations : 01525 * - Disable SMARTCARD Interrupts (Rx) 01526 * - Disable the DMA transfer in the peripheral register (if enabled) 01527 * - Abort DMA transfer by calling HAL_DMA_Abort (in case of transfer in DMA mode) 01528 * - Set handle State to READY 01529 * @note This procedure is executed in blocking mode : when exiting function, Abort is considered as completed. 01530 * @retval HAL status 01531 */ 01532 HAL_StatusTypeDef HAL_SMARTCARD_AbortReceive(SMARTCARD_HandleTypeDef *hsmartcard) 01533 { 01534 #if defined(USART_CR1_FIFOEN) 01535 /* Disable RTOIE, EOBIE, RXNE, PE, RXFT, TXFT and ERR (Frame error, noise error, overrun error) interrupts */ 01536 CLEAR_BIT(hsmartcard->Instance->CR1, (USART_CR1_RXNEIE_RXFNEIE | USART_CR1_PEIE | USART_CR1_RTOIE | 01537 USART_CR1_EOBIE)); 01538 CLEAR_BIT(hsmartcard->Instance->CR3, (USART_CR3_EIE | USART_CR3_RXFTIE)); 01539 #else 01540 /* Disable RTOIE, EOBIE, RXNE, PE and ERR (Frame error, noise error, overrun error) interrupts */ 01541 CLEAR_BIT(hsmartcard->Instance->CR1, (USART_CR1_RXNEIE | USART_CR1_PEIE | USART_CR1_RTOIE | USART_CR1_EOBIE)); 01542 CLEAR_BIT(hsmartcard->Instance->CR3, USART_CR3_EIE); 01543 #endif /* USART_CR1_FIFOEN */ 01544 01545 /* Check if a Transmit process is ongoing or not. If not disable ERR IT */ 01546 if (hsmartcard->gState == HAL_SMARTCARD_STATE_READY) 01547 { 01548 /* Disable the SMARTCARD Error Interrupt: (Frame error) */ 01549 CLEAR_BIT(hsmartcard->Instance->CR3, USART_CR3_EIE); 01550 } 01551 01552 /* Disable the SMARTCARD DMA Rx request if enabled */ 01553 if (HAL_IS_BIT_SET(hsmartcard->Instance->CR3, USART_CR3_DMAR)) 01554 { 01555 CLEAR_BIT(hsmartcard->Instance->CR3, USART_CR3_DMAR); 01556 01557 /* Abort the SMARTCARD DMA Rx channel : use blocking DMA Abort API (no callback) */ 01558 if (hsmartcard->hdmarx != NULL) 01559 { 01560 /* Set the SMARTCARD DMA Abort callback to Null. 01561 No call back execution at end of DMA abort procedure */ 01562 hsmartcard->hdmarx->XferAbortCallback = NULL; 01563 01564 if (HAL_DMA_Abort(hsmartcard->hdmarx) != HAL_OK) 01565 { 01566 if (HAL_DMA_GetError(hsmartcard->hdmarx) == HAL_DMA_ERROR_TIMEOUT) 01567 { 01568 /* Set error code to DMA */ 01569 hsmartcard->ErrorCode = HAL_SMARTCARD_ERROR_DMA; 01570 01571 return HAL_TIMEOUT; 01572 } 01573 } 01574 } 01575 } 01576 01577 /* Reset Rx transfer counter */ 01578 hsmartcard->RxXferCount = 0U; 01579 01580 /* Clear the Error flags in the ICR register */ 01581 __HAL_SMARTCARD_CLEAR_FLAG(hsmartcard, 01582 SMARTCARD_CLEAR_OREF | SMARTCARD_CLEAR_NEF | SMARTCARD_CLEAR_PEF | SMARTCARD_CLEAR_FEF | 01583 SMARTCARD_CLEAR_RTOF | SMARTCARD_CLEAR_EOBF); 01584 01585 /* Restore hsmartcard->RxState to Ready */ 01586 hsmartcard->RxState = HAL_SMARTCARD_STATE_READY; 01587 01588 return HAL_OK; 01589 } 01590 01591 /** 01592 * @brief Abort ongoing transfers (Interrupt mode). 01593 * @param hsmartcard Pointer to a SMARTCARD_HandleTypeDef structure that contains 01594 * the configuration information for the specified SMARTCARD module. 01595 * @note This procedure could be used for aborting any ongoing transfer started in Interrupt or DMA mode. 01596 * This procedure performs following operations : 01597 * - Disable SMARTCARD Interrupts (Tx and Rx) 01598 * - Disable the DMA transfer in the peripheral register (if enabled) 01599 * - Abort DMA transfer by calling HAL_DMA_Abort_IT (in case of transfer in DMA mode) 01600 * - Set handle State to READY 01601 * - At abort completion, call user abort complete callback 01602 * @note This procedure is executed in Interrupt mode, meaning that abort procedure could be 01603 * considered as completed only when user abort complete callback is executed (not when exiting function). 01604 * @retval HAL status 01605 */ 01606 HAL_StatusTypeDef HAL_SMARTCARD_Abort_IT(SMARTCARD_HandleTypeDef *hsmartcard) 01607 { 01608 uint32_t abortcplt = 1U; 01609 01610 #if defined(USART_CR1_FIFOEN) 01611 /* Disable RTOIE, EOBIE, TXEIE, TCIE, RXNE, PE, RXFT, TXFT and 01612 ERR (Frame error, noise error, overrun error) interrupts */ 01613 CLEAR_BIT(hsmartcard->Instance->CR1, 01614 (USART_CR1_RXNEIE_RXFNEIE | USART_CR1_PEIE | USART_CR1_TXEIE_TXFNFIE | USART_CR1_TCIE | USART_CR1_RTOIE | 01615 USART_CR1_EOBIE)); 01616 CLEAR_BIT(hsmartcard->Instance->CR3, (USART_CR3_EIE | USART_CR3_RXFTIE | USART_CR3_TXFTIE)); 01617 #else 01618 /* Disable RTOIE, EOBIE, TXEIE, TCIE, RXNE, PE and ERR (Frame error, noise error, overrun error) interrupts */ 01619 CLEAR_BIT(hsmartcard->Instance->CR1, 01620 (USART_CR1_RXNEIE | USART_CR1_PEIE | USART_CR1_TXEIE | USART_CR1_TCIE | USART_CR1_RTOIE | 01621 USART_CR1_EOBIE)); 01622 CLEAR_BIT(hsmartcard->Instance->CR3, USART_CR3_EIE); 01623 #endif /* USART_CR1_FIFOEN */ 01624 01625 /* If DMA Tx and/or DMA Rx Handles are associated to SMARTCARD Handle, 01626 DMA Abort complete callbacks should be initialised before any call 01627 to DMA Abort functions */ 01628 /* DMA Tx Handle is valid */ 01629 if (hsmartcard->hdmatx != NULL) 01630 { 01631 /* Set DMA Abort Complete callback if SMARTCARD DMA Tx request if enabled. 01632 Otherwise, set it to NULL */ 01633 if (HAL_IS_BIT_SET(hsmartcard->Instance->CR3, USART_CR3_DMAT)) 01634 { 01635 hsmartcard->hdmatx->XferAbortCallback = SMARTCARD_DMATxAbortCallback; 01636 } 01637 else 01638 { 01639 hsmartcard->hdmatx->XferAbortCallback = NULL; 01640 } 01641 } 01642 /* DMA Rx Handle is valid */ 01643 if (hsmartcard->hdmarx != NULL) 01644 { 01645 /* Set DMA Abort Complete callback if SMARTCARD DMA Rx request if enabled. 01646 Otherwise, set it to NULL */ 01647 if (HAL_IS_BIT_SET(hsmartcard->Instance->CR3, USART_CR3_DMAR)) 01648 { 01649 hsmartcard->hdmarx->XferAbortCallback = SMARTCARD_DMARxAbortCallback; 01650 } 01651 else 01652 { 01653 hsmartcard->hdmarx->XferAbortCallback = NULL; 01654 } 01655 } 01656 01657 /* Disable the SMARTCARD DMA Tx request if enabled */ 01658 if (HAL_IS_BIT_SET(hsmartcard->Instance->CR3, USART_CR3_DMAT)) 01659 { 01660 /* Disable DMA Tx at UART level */ 01661 CLEAR_BIT(hsmartcard->Instance->CR3, USART_CR3_DMAT); 01662 01663 /* Abort the SMARTCARD DMA Tx channel : use non blocking DMA Abort API (callback) */ 01664 if (hsmartcard->hdmatx != NULL) 01665 { 01666 /* SMARTCARD Tx DMA Abort callback has already been initialised : 01667 will lead to call HAL_SMARTCARD_AbortCpltCallback() at end of DMA abort procedure */ 01668 01669 /* Abort DMA TX */ 01670 if (HAL_DMA_Abort_IT(hsmartcard->hdmatx) != HAL_OK) 01671 { 01672 hsmartcard->hdmatx->XferAbortCallback = NULL; 01673 } 01674 else 01675 { 01676 abortcplt = 0U; 01677 } 01678 } 01679 } 01680 01681 /* Disable the SMARTCARD DMA Rx request if enabled */ 01682 if (HAL_IS_BIT_SET(hsmartcard->Instance->CR3, USART_CR3_DMAR)) 01683 { 01684 CLEAR_BIT(hsmartcard->Instance->CR3, USART_CR3_DMAR); 01685 01686 /* Abort the SMARTCARD DMA Rx channel : use non blocking DMA Abort API (callback) */ 01687 if (hsmartcard->hdmarx != NULL) 01688 { 01689 /* SMARTCARD Rx DMA Abort callback has already been initialised : 01690 will lead to call HAL_SMARTCARD_AbortCpltCallback() at end of DMA abort procedure */ 01691 01692 /* Abort DMA RX */ 01693 if (HAL_DMA_Abort_IT(hsmartcard->hdmarx) != HAL_OK) 01694 { 01695 hsmartcard->hdmarx->XferAbortCallback = NULL; 01696 abortcplt = 1U; 01697 } 01698 else 01699 { 01700 abortcplt = 0U; 01701 } 01702 } 01703 } 01704 01705 /* if no DMA abort complete callback execution is required => call user Abort Complete callback */ 01706 if (abortcplt == 1U) 01707 { 01708 /* Reset Tx and Rx transfer counters */ 01709 hsmartcard->TxXferCount = 0U; 01710 hsmartcard->RxXferCount = 0U; 01711 01712 /* Clear ISR function pointers */ 01713 hsmartcard->RxISR = NULL; 01714 hsmartcard->TxISR = NULL; 01715 01716 /* Reset errorCode */ 01717 hsmartcard->ErrorCode = HAL_SMARTCARD_ERROR_NONE; 01718 01719 /* Clear the Error flags in the ICR register */ 01720 __HAL_SMARTCARD_CLEAR_FLAG(hsmartcard, 01721 SMARTCARD_CLEAR_OREF | SMARTCARD_CLEAR_NEF | SMARTCARD_CLEAR_PEF | 01722 SMARTCARD_CLEAR_FEF | SMARTCARD_CLEAR_RTOF | SMARTCARD_CLEAR_EOBF); 01723 01724 /* Restore hsmartcard->gState and hsmartcard->RxState to Ready */ 01725 hsmartcard->gState = HAL_SMARTCARD_STATE_READY; 01726 hsmartcard->RxState = HAL_SMARTCARD_STATE_READY; 01727 01728 /* As no DMA to be aborted, call directly user Abort complete callback */ 01729 #if (USE_HAL_SMARTCARD_REGISTER_CALLBACKS == 1) 01730 /* Call registered Abort complete callback */ 01731 hsmartcard->AbortCpltCallback(hsmartcard); 01732 #else 01733 /* Call legacy weak Abort complete callback */ 01734 HAL_SMARTCARD_AbortCpltCallback(hsmartcard); 01735 #endif /* USE_HAL_SMARTCARD_REGISTER_CALLBACK */ 01736 } 01737 01738 return HAL_OK; 01739 } 01740 01741 /** 01742 * @brief Abort ongoing Transmit transfer (Interrupt mode). 01743 * @param hsmartcard Pointer to a SMARTCARD_HandleTypeDef structure that contains 01744 * the configuration information for the specified SMARTCARD module. 01745 * @note This procedure could be used for aborting any ongoing Tx transfer started in Interrupt or DMA mode. 01746 * This procedure performs following operations : 01747 * - Disable SMARTCARD Interrupts (Tx) 01748 * - Disable the DMA transfer in the peripheral register (if enabled) 01749 * - Abort DMA transfer by calling HAL_DMA_Abort_IT (in case of transfer in DMA mode) 01750 * - Set handle State to READY 01751 * - At abort completion, call user abort complete callback 01752 * @note This procedure is executed in Interrupt mode, meaning that abort procedure could be 01753 * considered as completed only when user abort complete callback is executed (not when exiting function). 01754 * @retval HAL status 01755 */ 01756 HAL_StatusTypeDef HAL_SMARTCARD_AbortTransmit_IT(SMARTCARD_HandleTypeDef *hsmartcard) 01757 { 01758 #if defined(USART_CR1_FIFOEN) 01759 /* Disable TCIE, TXEIE and TXFTIE interrupts */ 01760 CLEAR_BIT(hsmartcard->Instance->CR1, (USART_CR1_TXEIE_TXFNFIE | USART_CR1_TCIE)); 01761 CLEAR_BIT(hsmartcard->Instance->CR3, USART_CR3_TXFTIE); 01762 #else 01763 /* Disable TXEIE and TCIE interrupts */ 01764 CLEAR_BIT(hsmartcard->Instance->CR1, (USART_CR1_TXEIE | USART_CR1_TCIE)); 01765 #endif /* USART_CR1_FIFOEN */ 01766 01767 /* Check if a receive process is ongoing or not. If not disable ERR IT */ 01768 if (hsmartcard->RxState == HAL_SMARTCARD_STATE_READY) 01769 { 01770 /* Disable the SMARTCARD Error Interrupt: (Frame error) */ 01771 CLEAR_BIT(hsmartcard->Instance->CR3, USART_CR3_EIE); 01772 } 01773 01774 /* Disable the SMARTCARD DMA Tx request if enabled */ 01775 if (HAL_IS_BIT_SET(hsmartcard->Instance->CR3, USART_CR3_DMAT)) 01776 { 01777 CLEAR_BIT(hsmartcard->Instance->CR3, USART_CR3_DMAT); 01778 01779 /* Abort the SMARTCARD DMA Tx channel : use non blocking DMA Abort API (callback) */ 01780 if (hsmartcard->hdmatx != NULL) 01781 { 01782 /* Set the SMARTCARD DMA Abort callback : 01783 will lead to call HAL_SMARTCARD_AbortCpltCallback() at end of DMA abort procedure */ 01784 hsmartcard->hdmatx->XferAbortCallback = SMARTCARD_DMATxOnlyAbortCallback; 01785 01786 /* Abort DMA TX */ 01787 if (HAL_DMA_Abort_IT(hsmartcard->hdmatx) != HAL_OK) 01788 { 01789 /* Call Directly hsmartcard->hdmatx->XferAbortCallback function in case of error */ 01790 hsmartcard->hdmatx->XferAbortCallback(hsmartcard->hdmatx); 01791 } 01792 } 01793 else 01794 { 01795 /* Reset Tx transfer counter */ 01796 hsmartcard->TxXferCount = 0U; 01797 01798 /* Clear TxISR function pointers */ 01799 hsmartcard->TxISR = NULL; 01800 01801 /* Restore hsmartcard->gState to Ready */ 01802 hsmartcard->gState = HAL_SMARTCARD_STATE_READY; 01803 01804 /* As no DMA to be aborted, call directly user Abort complete callback */ 01805 #if (USE_HAL_SMARTCARD_REGISTER_CALLBACKS == 1) 01806 /* Call registered Abort Transmit Complete Callback */ 01807 hsmartcard->AbortTransmitCpltCallback(hsmartcard); 01808 #else 01809 /* Call legacy weak Abort Transmit Complete Callback */ 01810 HAL_SMARTCARD_AbortTransmitCpltCallback(hsmartcard); 01811 #endif /* USE_HAL_SMARTCARD_REGISTER_CALLBACK */ 01812 } 01813 } 01814 else 01815 { 01816 /* Reset Tx transfer counter */ 01817 hsmartcard->TxXferCount = 0U; 01818 01819 /* Clear TxISR function pointers */ 01820 hsmartcard->TxISR = NULL; 01821 01822 /* Clear the Error flags in the ICR register */ 01823 __HAL_SMARTCARD_CLEAR_FLAG(hsmartcard, SMARTCARD_CLEAR_FEF); 01824 01825 /* Restore hsmartcard->gState to Ready */ 01826 hsmartcard->gState = HAL_SMARTCARD_STATE_READY; 01827 01828 /* As no DMA to be aborted, call directly user Abort complete callback */ 01829 #if (USE_HAL_SMARTCARD_REGISTER_CALLBACKS == 1) 01830 /* Call registered Abort Transmit Complete Callback */ 01831 hsmartcard->AbortTransmitCpltCallback(hsmartcard); 01832 #else 01833 /* Call legacy weak Abort Transmit Complete Callback */ 01834 HAL_SMARTCARD_AbortTransmitCpltCallback(hsmartcard); 01835 #endif /* USE_HAL_SMARTCARD_REGISTER_CALLBACK */ 01836 } 01837 01838 return HAL_OK; 01839 } 01840 01841 /** 01842 * @brief Abort ongoing Receive transfer (Interrupt mode). 01843 * @param hsmartcard Pointer to a SMARTCARD_HandleTypeDef structure that contains 01844 * the configuration information for the specified SMARTCARD module. 01845 * @note This procedure could be used for aborting any ongoing Rx transfer started in Interrupt or DMA mode. 01846 * This procedure performs following operations : 01847 * - Disable SMARTCARD Interrupts (Rx) 01848 * - Disable the DMA transfer in the peripheral register (if enabled) 01849 * - Abort DMA transfer by calling HAL_DMA_Abort_IT (in case of transfer in DMA mode) 01850 * - Set handle State to READY 01851 * - At abort completion, call user abort complete callback 01852 * @note This procedure is executed in Interrupt mode, meaning that abort procedure could be 01853 * considered as completed only when user abort complete callback is executed (not when exiting function). 01854 * @retval HAL status 01855 */ 01856 HAL_StatusTypeDef HAL_SMARTCARD_AbortReceive_IT(SMARTCARD_HandleTypeDef *hsmartcard) 01857 { 01858 #if defined(USART_CR1_FIFOEN) 01859 /* Disable RTOIE, EOBIE, RXNE, PE, RXFT and ERR (Frame error, noise error, overrun error) interrupts */ 01860 CLEAR_BIT(hsmartcard->Instance->CR1, (USART_CR1_RXNEIE_RXFNEIE | USART_CR1_PEIE | USART_CR1_RTOIE | 01861 USART_CR1_EOBIE)); 01862 CLEAR_BIT(hsmartcard->Instance->CR3, (USART_CR3_EIE | USART_CR3_RXFTIE)); 01863 #else 01864 /* Disable RTOIE, EOBIE, RXNE, PE and ERR (Frame error, noise error, overrun error) interrupts */ 01865 CLEAR_BIT(hsmartcard->Instance->CR1, (USART_CR1_RXNEIE | USART_CR1_PEIE | USART_CR1_RTOIE | USART_CR1_EOBIE)); 01866 CLEAR_BIT(hsmartcard->Instance->CR3, USART_CR3_EIE); 01867 #endif /* USART_CR1_FIFOEN */ 01868 01869 /* Check if a Transmit process is ongoing or not. If not disable ERR IT */ 01870 if (hsmartcard->gState == HAL_SMARTCARD_STATE_READY) 01871 { 01872 /* Disable the SMARTCARD Error Interrupt: (Frame error) */ 01873 CLEAR_BIT(hsmartcard->Instance->CR3, USART_CR3_EIE); 01874 } 01875 01876 /* Disable the SMARTCARD DMA Rx request if enabled */ 01877 if (HAL_IS_BIT_SET(hsmartcard->Instance->CR3, USART_CR3_DMAR)) 01878 { 01879 CLEAR_BIT(hsmartcard->Instance->CR3, USART_CR3_DMAR); 01880 01881 /* Abort the SMARTCARD DMA Rx channel : use non blocking DMA Abort API (callback) */ 01882 if (hsmartcard->hdmarx != NULL) 01883 { 01884 /* Set the SMARTCARD DMA Abort callback : 01885 will lead to call HAL_SMARTCARD_AbortCpltCallback() at end of DMA abort procedure */ 01886 hsmartcard->hdmarx->XferAbortCallback = SMARTCARD_DMARxOnlyAbortCallback; 01887 01888 /* Abort DMA RX */ 01889 if (HAL_DMA_Abort_IT(hsmartcard->hdmarx) != HAL_OK) 01890 { 01891 /* Call Directly hsmartcard->hdmarx->XferAbortCallback function in case of error */ 01892 hsmartcard->hdmarx->XferAbortCallback(hsmartcard->hdmarx); 01893 } 01894 } 01895 else 01896 { 01897 /* Reset Rx transfer counter */ 01898 hsmartcard->RxXferCount = 0U; 01899 01900 /* Clear RxISR function pointer */ 01901 hsmartcard->RxISR = NULL; 01902 01903 /* Clear the Error flags in the ICR register */ 01904 __HAL_SMARTCARD_CLEAR_FLAG(hsmartcard, 01905 SMARTCARD_CLEAR_OREF | SMARTCARD_CLEAR_NEF | SMARTCARD_CLEAR_PEF | 01906 SMARTCARD_CLEAR_FEF | SMARTCARD_CLEAR_RTOF | SMARTCARD_CLEAR_EOBF); 01907 01908 /* Restore hsmartcard->RxState to Ready */ 01909 hsmartcard->RxState = HAL_SMARTCARD_STATE_READY; 01910 01911 /* As no DMA to be aborted, call directly user Abort complete callback */ 01912 #if (USE_HAL_SMARTCARD_REGISTER_CALLBACKS == 1) 01913 /* Call registered Abort Receive Complete Callback */ 01914 hsmartcard->AbortReceiveCpltCallback(hsmartcard); 01915 #else 01916 /* Call legacy weak Abort Receive Complete Callback */ 01917 HAL_SMARTCARD_AbortReceiveCpltCallback(hsmartcard); 01918 #endif /* USE_HAL_SMARTCARD_REGISTER_CALLBACK */ 01919 } 01920 } 01921 else 01922 { 01923 /* Reset Rx transfer counter */ 01924 hsmartcard->RxXferCount = 0U; 01925 01926 /* Clear RxISR function pointer */ 01927 hsmartcard->RxISR = NULL; 01928 01929 /* Clear the Error flags in the ICR register */ 01930 __HAL_SMARTCARD_CLEAR_FLAG(hsmartcard, 01931 SMARTCARD_CLEAR_OREF | SMARTCARD_CLEAR_NEF | SMARTCARD_CLEAR_PEF | 01932 SMARTCARD_CLEAR_FEF | SMARTCARD_CLEAR_RTOF | SMARTCARD_CLEAR_EOBF); 01933 01934 /* Restore hsmartcard->RxState to Ready */ 01935 hsmartcard->RxState = HAL_SMARTCARD_STATE_READY; 01936 01937 /* As no DMA to be aborted, call directly user Abort complete callback */ 01938 #if (USE_HAL_SMARTCARD_REGISTER_CALLBACKS == 1) 01939 /* Call registered Abort Receive Complete Callback */ 01940 hsmartcard->AbortReceiveCpltCallback(hsmartcard); 01941 #else 01942 /* Call legacy weak Abort Receive Complete Callback */ 01943 HAL_SMARTCARD_AbortReceiveCpltCallback(hsmartcard); 01944 #endif /* USE_HAL_SMARTCARD_REGISTER_CALLBACK */ 01945 } 01946 01947 return HAL_OK; 01948 } 01949 01950 /** 01951 * @brief Handle SMARTCARD interrupt requests. 01952 * @param hsmartcard Pointer to a SMARTCARD_HandleTypeDef structure that contains 01953 * the configuration information for the specified SMARTCARD module. 01954 * @retval None 01955 */ 01956 void HAL_SMARTCARD_IRQHandler(SMARTCARD_HandleTypeDef *hsmartcard) 01957 { 01958 uint32_t isrflags = READ_REG(hsmartcard->Instance->ISR); 01959 uint32_t cr1its = READ_REG(hsmartcard->Instance->CR1); 01960 uint32_t cr3its = READ_REG(hsmartcard->Instance->CR3); 01961 uint32_t errorflags; 01962 uint32_t errorcode; 01963 01964 /* If no error occurs */ 01965 errorflags = (isrflags & (uint32_t)(USART_ISR_PE | USART_ISR_FE | USART_ISR_ORE | USART_ISR_NE | USART_ISR_RTOF)); 01966 if (errorflags == 0U) 01967 { 01968 /* SMARTCARD in mode Receiver ---------------------------------------------------*/ 01969 #if defined(USART_CR1_FIFOEN) 01970 if (((isrflags & USART_ISR_RXNE_RXFNE) != 0U) 01971 && (((cr1its & USART_CR1_RXNEIE_RXFNEIE) != 0U) 01972 || ((cr3its & USART_CR3_RXFTIE) != 0U))) 01973 #else 01974 if (((isrflags & USART_ISR_RXNE) != 0U) 01975 && ((cr1its & USART_CR1_RXNEIE) != 0U)) 01976 #endif /* USART_CR1_FIFOEN */ 01977 { 01978 if (hsmartcard->RxISR != NULL) 01979 { 01980 hsmartcard->RxISR(hsmartcard); 01981 } 01982 return; 01983 } 01984 } 01985 01986 /* If some errors occur */ 01987 #if defined(USART_CR1_FIFOEN) 01988 if ((errorflags != 0U) 01989 && ((((cr3its & (USART_CR3_RXFTIE | USART_CR3_EIE)) != 0U) 01990 || ((cr1its & (USART_CR1_RXNEIE_RXFNEIE | USART_CR1_PEIE)) != 0U)))) 01991 #else 01992 if ((errorflags != 0U) 01993 && (((cr3its & USART_CR3_EIE) != 0U) 01994 || ((cr1its & (USART_CR1_RXNEIE | USART_CR1_PEIE)) != 0U))) 01995 #endif /* USART_CR1_FIFOEN */ 01996 { 01997 /* SMARTCARD parity error interrupt occurred -------------------------------------*/ 01998 if (((isrflags & USART_ISR_PE) != 0U) && ((cr1its & USART_CR1_PEIE) != 0U)) 01999 { 02000 __HAL_SMARTCARD_CLEAR_IT(hsmartcard, SMARTCARD_CLEAR_PEF); 02001 02002 hsmartcard->ErrorCode |= HAL_SMARTCARD_ERROR_PE; 02003 } 02004 02005 /* SMARTCARD frame error interrupt occurred --------------------------------------*/ 02006 if (((isrflags & USART_ISR_FE) != 0U) && ((cr3its & USART_CR3_EIE) != 0U)) 02007 { 02008 __HAL_SMARTCARD_CLEAR_IT(hsmartcard, SMARTCARD_CLEAR_FEF); 02009 02010 hsmartcard->ErrorCode |= HAL_SMARTCARD_ERROR_FE; 02011 } 02012 02013 /* SMARTCARD noise error interrupt occurred --------------------------------------*/ 02014 if (((isrflags & USART_ISR_NE) != 0U) && ((cr3its & USART_CR3_EIE) != 0U)) 02015 { 02016 __HAL_SMARTCARD_CLEAR_IT(hsmartcard, SMARTCARD_CLEAR_NEF); 02017 02018 hsmartcard->ErrorCode |= HAL_SMARTCARD_ERROR_NE; 02019 } 02020 02021 /* SMARTCARD Over-Run interrupt occurred -----------------------------------------*/ 02022 #if defined(USART_CR1_FIFOEN) 02023 if (((isrflags & USART_ISR_ORE) != 0U) 02024 && (((cr1its & USART_CR1_RXNEIE_RXFNEIE) != 0U) 02025 || ((cr3its & USART_CR3_RXFTIE) != 0U) 02026 || ((cr3its & USART_CR3_EIE) != 0U))) 02027 #else 02028 if (((isrflags & USART_ISR_ORE) != 0U) 02029 && (((cr1its & USART_CR1_RXNEIE) != 0U) 02030 || ((cr3its & USART_CR3_EIE) != 0U))) 02031 #endif /* USART_CR1_FIFOEN */ 02032 { 02033 __HAL_SMARTCARD_CLEAR_IT(hsmartcard, SMARTCARD_CLEAR_OREF); 02034 02035 hsmartcard->ErrorCode |= HAL_SMARTCARD_ERROR_ORE; 02036 } 02037 02038 /* SMARTCARD receiver timeout interrupt occurred -----------------------------------------*/ 02039 if (((isrflags & USART_ISR_RTOF) != 0U) && ((cr1its & USART_CR1_RTOIE) != 0U)) 02040 { 02041 __HAL_SMARTCARD_CLEAR_IT(hsmartcard, SMARTCARD_CLEAR_RTOF); 02042 02043 hsmartcard->ErrorCode |= HAL_SMARTCARD_ERROR_RTO; 02044 } 02045 02046 /* Call SMARTCARD Error Call back function if need be --------------------------*/ 02047 if (hsmartcard->ErrorCode != HAL_SMARTCARD_ERROR_NONE) 02048 { 02049 /* SMARTCARD in mode Receiver ---------------------------------------------------*/ 02050 #if defined(USART_CR1_FIFOEN) 02051 if (((isrflags & USART_ISR_RXNE_RXFNE) != 0U) 02052 && (((cr1its & USART_CR1_RXNEIE_RXFNEIE) != 0U) 02053 || ((cr3its & USART_CR3_RXFTIE) != 0U))) 02054 #else 02055 if (((isrflags & USART_ISR_RXNE) != 0U) 02056 && ((cr1its & USART_CR1_RXNEIE) != 0U)) 02057 #endif /* USART_CR1_FIFOEN */ 02058 { 02059 if (hsmartcard->RxISR != NULL) 02060 { 02061 hsmartcard->RxISR(hsmartcard); 02062 } 02063 } 02064 02065 /* If Error is to be considered as blocking : 02066 - Receiver Timeout error in Reception 02067 - Overrun error in Reception 02068 - any error occurs in DMA mode reception 02069 */ 02070 errorcode = hsmartcard->ErrorCode; 02071 if ((HAL_IS_BIT_SET(hsmartcard->Instance->CR3, USART_CR3_DMAR)) 02072 || ((errorcode & (HAL_SMARTCARD_ERROR_RTO | HAL_SMARTCARD_ERROR_ORE)) != 0U)) 02073 { 02074 /* Blocking error : transfer is aborted 02075 Set the SMARTCARD state ready to be able to start again the process, 02076 Disable Rx Interrupts, and disable Rx DMA request, if ongoing */ 02077 SMARTCARD_EndRxTransfer(hsmartcard); 02078 02079 /* Disable the SMARTCARD DMA Rx request if enabled */ 02080 if (HAL_IS_BIT_SET(hsmartcard->Instance->CR3, USART_CR3_DMAR)) 02081 { 02082 CLEAR_BIT(hsmartcard->Instance->CR3, USART_CR3_DMAR); 02083 02084 /* Abort the SMARTCARD DMA Rx channel */ 02085 if (hsmartcard->hdmarx != NULL) 02086 { 02087 /* Set the SMARTCARD DMA Abort callback : 02088 will lead to call HAL_SMARTCARD_ErrorCallback() at end of DMA abort procedure */ 02089 hsmartcard->hdmarx->XferAbortCallback = SMARTCARD_DMAAbortOnError; 02090 02091 /* Abort DMA RX */ 02092 if (HAL_DMA_Abort_IT(hsmartcard->hdmarx) != HAL_OK) 02093 { 02094 /* Call Directly hsmartcard->hdmarx->XferAbortCallback function in case of error */ 02095 hsmartcard->hdmarx->XferAbortCallback(hsmartcard->hdmarx); 02096 } 02097 } 02098 else 02099 { 02100 #if (USE_HAL_SMARTCARD_REGISTER_CALLBACKS == 1) 02101 /* Call registered user error callback */ 02102 hsmartcard->ErrorCallback(hsmartcard); 02103 #else 02104 /* Call legacy weak user error callback */ 02105 HAL_SMARTCARD_ErrorCallback(hsmartcard); 02106 #endif /* USE_HAL_SMARTCARD_REGISTER_CALLBACK */ 02107 } 02108 } 02109 else 02110 { 02111 #if (USE_HAL_SMARTCARD_REGISTER_CALLBACKS == 1) 02112 /* Call registered user error callback */ 02113 hsmartcard->ErrorCallback(hsmartcard); 02114 #else 02115 /* Call legacy weak user error callback */ 02116 HAL_SMARTCARD_ErrorCallback(hsmartcard); 02117 #endif /* USE_HAL_SMARTCARD_REGISTER_CALLBACK */ 02118 } 02119 } 02120 /* other error type to be considered as blocking : 02121 - Frame error in Transmission 02122 */ 02123 else if ((hsmartcard->gState == HAL_SMARTCARD_STATE_BUSY_TX) 02124 && ((errorcode & HAL_SMARTCARD_ERROR_FE) != 0U)) 02125 { 02126 /* Blocking error : transfer is aborted 02127 Set the SMARTCARD state ready to be able to start again the process, 02128 Disable Tx Interrupts, and disable Tx DMA request, if ongoing */ 02129 SMARTCARD_EndTxTransfer(hsmartcard); 02130 02131 /* Disable the SMARTCARD DMA Tx request if enabled */ 02132 if (HAL_IS_BIT_SET(hsmartcard->Instance->CR3, USART_CR3_DMAT)) 02133 { 02134 CLEAR_BIT(hsmartcard->Instance->CR3, USART_CR3_DMAT); 02135 02136 /* Abort the SMARTCARD DMA Tx channel */ 02137 if (hsmartcard->hdmatx != NULL) 02138 { 02139 /* Set the SMARTCARD DMA Abort callback : 02140 will lead to call HAL_SMARTCARD_ErrorCallback() at end of DMA abort procedure */ 02141 hsmartcard->hdmatx->XferAbortCallback = SMARTCARD_DMAAbortOnError; 02142 02143 /* Abort DMA TX */ 02144 if (HAL_DMA_Abort_IT(hsmartcard->hdmatx) != HAL_OK) 02145 { 02146 /* Call Directly hsmartcard->hdmatx->XferAbortCallback function in case of error */ 02147 hsmartcard->hdmatx->XferAbortCallback(hsmartcard->hdmatx); 02148 } 02149 } 02150 else 02151 { 02152 #if (USE_HAL_SMARTCARD_REGISTER_CALLBACKS == 1) 02153 /* Call registered user error callback */ 02154 hsmartcard->ErrorCallback(hsmartcard); 02155 #else 02156 /* Call legacy weak user error callback */ 02157 HAL_SMARTCARD_ErrorCallback(hsmartcard); 02158 #endif /* USE_HAL_SMARTCARD_REGISTER_CALLBACK */ 02159 } 02160 } 02161 else 02162 { 02163 #if (USE_HAL_SMARTCARD_REGISTER_CALLBACKS == 1) 02164 /* Call registered user error callback */ 02165 hsmartcard->ErrorCallback(hsmartcard); 02166 #else 02167 /* Call legacy weak user error callback */ 02168 HAL_SMARTCARD_ErrorCallback(hsmartcard); 02169 #endif /* USE_HAL_SMARTCARD_REGISTER_CALLBACK */ 02170 } 02171 } 02172 else 02173 { 02174 /* Non Blocking error : transfer could go on. 02175 Error is notified to user through user error callback */ 02176 #if (USE_HAL_SMARTCARD_REGISTER_CALLBACKS == 1) 02177 /* Call registered user error callback */ 02178 hsmartcard->ErrorCallback(hsmartcard); 02179 #else 02180 /* Call legacy weak user error callback */ 02181 HAL_SMARTCARD_ErrorCallback(hsmartcard); 02182 #endif /* USE_HAL_SMARTCARD_REGISTER_CALLBACK */ 02183 hsmartcard->ErrorCode = HAL_SMARTCARD_ERROR_NONE; 02184 } 02185 } 02186 return; 02187 02188 } /* End if some error occurs */ 02189 02190 /* SMARTCARD in mode Receiver, end of block interruption ------------------------*/ 02191 if (((isrflags & USART_ISR_EOBF) != 0U) && ((cr1its & USART_CR1_EOBIE) != 0U)) 02192 { 02193 hsmartcard->RxState = HAL_SMARTCARD_STATE_READY; 02194 __HAL_UNLOCK(hsmartcard); 02195 #if (USE_HAL_SMARTCARD_REGISTER_CALLBACKS == 1) 02196 /* Call registered Rx complete callback */ 02197 hsmartcard->RxCpltCallback(hsmartcard); 02198 #else 02199 /* Call legacy weak Rx complete callback */ 02200 HAL_SMARTCARD_RxCpltCallback(hsmartcard); 02201 #endif /* USE_HAL_SMARTCARD_REGISTER_CALLBACK */ 02202 /* Clear EOBF interrupt after HAL_SMARTCARD_RxCpltCallback() call for the End of Block information 02203 to be available during HAL_SMARTCARD_RxCpltCallback() processing */ 02204 __HAL_SMARTCARD_CLEAR_IT(hsmartcard, SMARTCARD_CLEAR_EOBF); 02205 return; 02206 } 02207 02208 /* SMARTCARD in mode Transmitter ------------------------------------------------*/ 02209 #if defined(USART_CR1_FIFOEN) 02210 if (((isrflags & USART_ISR_TXE_TXFNF) != 0U) 02211 && (((cr1its & USART_CR1_TXEIE_TXFNFIE) != 0U) 02212 || ((cr3its & USART_CR3_TXFTIE) != 0U))) 02213 #else 02214 if (((isrflags & USART_ISR_TXE) != 0U) 02215 && ((cr1its & USART_CR1_TXEIE) != 0U)) 02216 #endif /* USART_CR1_FIFOEN */ 02217 { 02218 if (hsmartcard->TxISR != NULL) 02219 { 02220 hsmartcard->TxISR(hsmartcard); 02221 } 02222 return; 02223 } 02224 02225 /* SMARTCARD in mode Transmitter (transmission end) ------------------------*/ 02226 if (__HAL_SMARTCARD_GET_IT(hsmartcard, hsmartcard->AdvancedInit.TxCompletionIndication) != RESET) 02227 { 02228 if (__HAL_SMARTCARD_GET_IT_SOURCE(hsmartcard, hsmartcard->AdvancedInit.TxCompletionIndication) != RESET) 02229 { 02230 SMARTCARD_EndTransmit_IT(hsmartcard); 02231 return; 02232 } 02233 } 02234 02235 #if defined(USART_CR1_FIFOEN) 02236 /* SMARTCARD TX Fifo Empty occurred ----------------------------------------------*/ 02237 if (((isrflags & USART_ISR_TXFE) != 0U) && ((cr1its & USART_CR1_TXFEIE) != 0U)) 02238 { 02239 #if (USE_HAL_SMARTCARD_REGISTER_CALLBACKS == 1) 02240 /* Call registered Tx Fifo Empty Callback */ 02241 hsmartcard->TxFifoEmptyCallback(hsmartcard); 02242 #else 02243 /* Call legacy weak Tx Fifo Empty Callback */ 02244 HAL_SMARTCARDEx_TxFifoEmptyCallback(hsmartcard); 02245 #endif /* USE_HAL_SMARTCARD_REGISTER_CALLBACK */ 02246 return; 02247 } 02248 02249 /* SMARTCARD RX Fifo Full occurred ----------------------------------------------*/ 02250 if (((isrflags & USART_ISR_RXFF) != 0U) && ((cr1its & USART_CR1_RXFFIE) != 0U)) 02251 { 02252 #if (USE_HAL_SMARTCARD_REGISTER_CALLBACKS == 1) 02253 /* Call registered Rx Fifo Full Callback */ 02254 hsmartcard->RxFifoFullCallback(hsmartcard); 02255 #else 02256 /* Call legacy weak Rx Fifo Full Callback */ 02257 HAL_SMARTCARDEx_RxFifoFullCallback(hsmartcard); 02258 #endif /* USE_HAL_SMARTCARD_REGISTER_CALLBACK */ 02259 return; 02260 } 02261 #endif /* USART_CR1_FIFOEN */ 02262 } 02263 02264 /** 02265 * @brief Tx Transfer completed callback. 02266 * @param hsmartcard Pointer to a SMARTCARD_HandleTypeDef structure that contains 02267 * the configuration information for the specified SMARTCARD module. 02268 * @retval None 02269 */ 02270 __weak void HAL_SMARTCARD_TxCpltCallback(SMARTCARD_HandleTypeDef *hsmartcard) 02271 { 02272 /* Prevent unused argument(s) compilation warning */ 02273 UNUSED(hsmartcard); 02274 02275 /* NOTE : This function should not be modified, when the callback is needed, 02276 the HAL_SMARTCARD_TxCpltCallback can be implemented in the user file. 02277 */ 02278 } 02279 02280 /** 02281 * @brief Rx Transfer completed callback. 02282 * @param hsmartcard Pointer to a SMARTCARD_HandleTypeDef structure that contains 02283 * the configuration information for the specified SMARTCARD module. 02284 * @retval None 02285 */ 02286 __weak void HAL_SMARTCARD_RxCpltCallback(SMARTCARD_HandleTypeDef *hsmartcard) 02287 { 02288 /* Prevent unused argument(s) compilation warning */ 02289 UNUSED(hsmartcard); 02290 02291 /* NOTE : This function should not be modified, when the callback is needed, 02292 the HAL_SMARTCARD_RxCpltCallback can be implemented in the user file. 02293 */ 02294 } 02295 02296 /** 02297 * @brief SMARTCARD error callback. 02298 * @param hsmartcard Pointer to a SMARTCARD_HandleTypeDef structure that contains 02299 * the configuration information for the specified SMARTCARD module. 02300 * @retval None 02301 */ 02302 __weak void HAL_SMARTCARD_ErrorCallback(SMARTCARD_HandleTypeDef *hsmartcard) 02303 { 02304 /* Prevent unused argument(s) compilation warning */ 02305 UNUSED(hsmartcard); 02306 02307 /* NOTE : This function should not be modified, when the callback is needed, 02308 the HAL_SMARTCARD_ErrorCallback can be implemented in the user file. 02309 */ 02310 } 02311 02312 /** 02313 * @brief SMARTCARD Abort Complete callback. 02314 * @param hsmartcard Pointer to a SMARTCARD_HandleTypeDef structure that contains 02315 * the configuration information for the specified SMARTCARD module. 02316 * @retval None 02317 */ 02318 __weak void HAL_SMARTCARD_AbortCpltCallback(SMARTCARD_HandleTypeDef *hsmartcard) 02319 { 02320 /* Prevent unused argument(s) compilation warning */ 02321 UNUSED(hsmartcard); 02322 02323 /* NOTE : This function should not be modified, when the callback is needed, 02324 the HAL_SMARTCARD_AbortCpltCallback can be implemented in the user file. 02325 */ 02326 } 02327 02328 /** 02329 * @brief SMARTCARD Abort Complete callback. 02330 * @param hsmartcard Pointer to a SMARTCARD_HandleTypeDef structure that contains 02331 * the configuration information for the specified SMARTCARD module. 02332 * @retval None 02333 */ 02334 __weak void HAL_SMARTCARD_AbortTransmitCpltCallback(SMARTCARD_HandleTypeDef *hsmartcard) 02335 { 02336 /* Prevent unused argument(s) compilation warning */ 02337 UNUSED(hsmartcard); 02338 02339 /* NOTE : This function should not be modified, when the callback is needed, 02340 the HAL_SMARTCARD_AbortTransmitCpltCallback can be implemented in the user file. 02341 */ 02342 } 02343 02344 /** 02345 * @brief SMARTCARD Abort Receive Complete callback. 02346 * @param hsmartcard Pointer to a SMARTCARD_HandleTypeDef structure that contains 02347 * the configuration information for the specified SMARTCARD module. 02348 * @retval None 02349 */ 02350 __weak void HAL_SMARTCARD_AbortReceiveCpltCallback(SMARTCARD_HandleTypeDef *hsmartcard) 02351 { 02352 /* Prevent unused argument(s) compilation warning */ 02353 UNUSED(hsmartcard); 02354 02355 /* NOTE : This function should not be modified, when the callback is needed, 02356 the HAL_SMARTCARD_AbortReceiveCpltCallback can be implemented in the user file. 02357 */ 02358 } 02359 02360 /** 02361 * @} 02362 */ 02363 02364 /** @defgroup SMARTCARD_Exported_Functions_Group4 Peripheral State and Errors functions 02365 * @brief SMARTCARD State and Errors functions 02366 * 02367 @verbatim 02368 ============================================================================== 02369 ##### Peripheral State and Errors functions ##### 02370 ============================================================================== 02371 [..] 02372 This subsection provides a set of functions allowing to return the State of SmartCard 02373 handle and also return Peripheral Errors occurred during communication process 02374 (+) HAL_SMARTCARD_GetState() API can be helpful to check in run-time the state 02375 of the SMARTCARD peripheral. 02376 (+) HAL_SMARTCARD_GetError() checks in run-time errors that could occur during 02377 communication. 02378 02379 @endverbatim 02380 * @{ 02381 */ 02382 02383 /** 02384 * @brief Return the SMARTCARD handle state. 02385 * @param hsmartcard Pointer to a SMARTCARD_HandleTypeDef structure that contains 02386 * the configuration information for the specified SMARTCARD module. 02387 * @retval SMARTCARD handle state 02388 */ 02389 HAL_SMARTCARD_StateTypeDef HAL_SMARTCARD_GetState(SMARTCARD_HandleTypeDef *hsmartcard) 02390 { 02391 /* Return SMARTCARD handle state */ 02392 uint32_t temp1; 02393 uint32_t temp2; 02394 temp1 = (uint32_t)hsmartcard->gState; 02395 temp2 = (uint32_t)hsmartcard->RxState; 02396 02397 return (HAL_SMARTCARD_StateTypeDef)(temp1 | temp2); 02398 } 02399 02400 /** 02401 * @brief Return the SMARTCARD handle error code. 02402 * @param hsmartcard Pointer to a SMARTCARD_HandleTypeDef structure that contains 02403 * the configuration information for the specified SMARTCARD module. 02404 * @retval SMARTCARD handle Error Code 02405 */ 02406 uint32_t HAL_SMARTCARD_GetError(SMARTCARD_HandleTypeDef *hsmartcard) 02407 { 02408 return hsmartcard->ErrorCode; 02409 } 02410 02411 /** 02412 * @} 02413 */ 02414 02415 /** 02416 * @} 02417 */ 02418 02419 /** @defgroup SMARTCARD_Private_Functions SMARTCARD Private Functions 02420 * @{ 02421 */ 02422 02423 #if (USE_HAL_SMARTCARD_REGISTER_CALLBACKS == 1) 02424 /** 02425 * @brief Initialize the callbacks to their default values. 02426 * @param hsmartcard SMARTCARD handle. 02427 * @retval none 02428 */ 02429 void SMARTCARD_InitCallbacksToDefault(SMARTCARD_HandleTypeDef *hsmartcard) 02430 { 02431 /* Init the SMARTCARD Callback settings */ 02432 hsmartcard->TxCpltCallback = HAL_SMARTCARD_TxCpltCallback; /* Legacy weak TxCpltCallback */ 02433 hsmartcard->RxCpltCallback = HAL_SMARTCARD_RxCpltCallback; /* Legacy weak RxCpltCallback */ 02434 hsmartcard->ErrorCallback = HAL_SMARTCARD_ErrorCallback; /* Legacy weak ErrorCallback */ 02435 hsmartcard->AbortCpltCallback = HAL_SMARTCARD_AbortCpltCallback; /* Legacy weak AbortCpltCallback */ 02436 hsmartcard->AbortTransmitCpltCallback = HAL_SMARTCARD_AbortTransmitCpltCallback; /* Legacy weak 02437 AbortTransmitCpltCallback */ 02438 hsmartcard->AbortReceiveCpltCallback = HAL_SMARTCARD_AbortReceiveCpltCallback; /* Legacy weak 02439 AbortReceiveCpltCallback */ 02440 #if defined(USART_CR1_FIFOEN) 02441 hsmartcard->RxFifoFullCallback = HAL_SMARTCARDEx_RxFifoFullCallback; /* Legacy weak 02442 RxFifoFullCallback */ 02443 hsmartcard->TxFifoEmptyCallback = HAL_SMARTCARDEx_TxFifoEmptyCallback; /* Legacy weak 02444 TxFifoEmptyCallback */ 02445 #endif /* USART_CR1_FIFOEN */ 02446 02447 } 02448 #endif /* USE_HAL_SMARTCARD_REGISTER_CALLBACKS */ 02449 02450 /** 02451 * @brief Configure the SMARTCARD associated USART peripheral. 02452 * @param hsmartcard Pointer to a SMARTCARD_HandleTypeDef structure that contains 02453 * the configuration information for the specified SMARTCARD module. 02454 * @retval HAL status 02455 */ 02456 static HAL_StatusTypeDef SMARTCARD_SetConfig(SMARTCARD_HandleTypeDef *hsmartcard) 02457 { 02458 uint32_t tmpreg; 02459 SMARTCARD_ClockSourceTypeDef clocksource; 02460 HAL_StatusTypeDef ret = HAL_OK; 02461 #if defined(USART_PRESC_PRESCALER) 02462 static const uint16_t SMARTCARDPrescTable[12] = {1U, 2U, 4U, 6U, 8U, 10U, 12U, 16U, 32U, 64U, 128U, 256U}; 02463 #endif /* USART_PRESC_PRESCALER */ 02464 uint32_t pclk; 02465 02466 /* Check the parameters */ 02467 assert_param(IS_SMARTCARD_INSTANCE(hsmartcard->Instance)); 02468 assert_param(IS_SMARTCARD_BAUDRATE(hsmartcard->Init.BaudRate)); 02469 assert_param(IS_SMARTCARD_WORD_LENGTH(hsmartcard->Init.WordLength)); 02470 assert_param(IS_SMARTCARD_STOPBITS(hsmartcard->Init.StopBits)); 02471 assert_param(IS_SMARTCARD_PARITY(hsmartcard->Init.Parity)); 02472 assert_param(IS_SMARTCARD_MODE(hsmartcard->Init.Mode)); 02473 assert_param(IS_SMARTCARD_POLARITY(hsmartcard->Init.CLKPolarity)); 02474 assert_param(IS_SMARTCARD_PHASE(hsmartcard->Init.CLKPhase)); 02475 assert_param(IS_SMARTCARD_LASTBIT(hsmartcard->Init.CLKLastBit)); 02476 assert_param(IS_SMARTCARD_ONE_BIT_SAMPLE(hsmartcard->Init.OneBitSampling)); 02477 assert_param(IS_SMARTCARD_NACK(hsmartcard->Init.NACKEnable)); 02478 assert_param(IS_SMARTCARD_TIMEOUT(hsmartcard->Init.TimeOutEnable)); 02479 assert_param(IS_SMARTCARD_AUTORETRY_COUNT(hsmartcard->Init.AutoRetryCount)); 02480 #if defined(USART_PRESC_PRESCALER) 02481 assert_param(IS_SMARTCARD_CLOCKPRESCALER(hsmartcard->Init.ClockPrescaler)); 02482 #endif /* USART_PRESC_PRESCALER */ 02483 02484 /*-------------------------- USART CR1 Configuration -----------------------*/ 02485 /* In SmartCard mode, M and PCE are forced to 1 (8 bits + parity). 02486 * Oversampling is forced to 16 (OVER8 = 0). 02487 * Configure the Parity and Mode: 02488 * set PS bit according to hsmartcard->Init.Parity value 02489 * set TE and RE bits according to hsmartcard->Init.Mode value */ 02490 tmpreg = (((uint32_t)hsmartcard->Init.Parity) | ((uint32_t)hsmartcard->Init.Mode) | 02491 ((uint32_t)hsmartcard->Init.WordLength)); 02492 MODIFY_REG(hsmartcard->Instance->CR1, USART_CR1_FIELDS, tmpreg); 02493 02494 /*-------------------------- USART CR2 Configuration -----------------------*/ 02495 tmpreg = hsmartcard->Init.StopBits; 02496 /* Synchronous mode is activated by default */ 02497 tmpreg |= (uint32_t) USART_CR2_CLKEN | hsmartcard->Init.CLKPolarity; 02498 tmpreg |= (uint32_t) hsmartcard->Init.CLKPhase | hsmartcard->Init.CLKLastBit; 02499 tmpreg |= (uint32_t) hsmartcard->Init.TimeOutEnable; 02500 MODIFY_REG(hsmartcard->Instance->CR2, USART_CR2_FIELDS, tmpreg); 02501 02502 /*-------------------------- USART CR3 Configuration -----------------------*/ 02503 /* Configure 02504 * - one-bit sampling method versus three samples' majority rule 02505 * according to hsmartcard->Init.OneBitSampling 02506 * - NACK transmission in case of parity error according 02507 * to hsmartcard->Init.NACKEnable 02508 * - autoretry counter according to hsmartcard->Init.AutoRetryCount */ 02509 02510 tmpreg = (uint32_t) hsmartcard->Init.OneBitSampling | hsmartcard->Init.NACKEnable; 02511 tmpreg |= ((uint32_t)hsmartcard->Init.AutoRetryCount << USART_CR3_SCARCNT_Pos); 02512 MODIFY_REG(hsmartcard->Instance->CR3, USART_CR3_FIELDS, tmpreg); 02513 02514 #if defined(USART_PRESC_PRESCALER) 02515 /*--------------------- SMARTCARD clock PRESC Configuration ----------------*/ 02516 /* Configure 02517 * - SMARTCARD Clock Prescaler: set PRESCALER according to hsmartcard->Init.ClockPrescaler value */ 02518 MODIFY_REG(hsmartcard->Instance->PRESC, USART_PRESC_PRESCALER, hsmartcard->Init.ClockPrescaler); 02519 #endif /* USART_PRESC_PRESCALER */ 02520 02521 /*-------------------------- USART GTPR Configuration ----------------------*/ 02522 tmpreg = (hsmartcard->Init.Prescaler | ((uint32_t)hsmartcard->Init.GuardTime << USART_GTPR_GT_Pos)); 02523 MODIFY_REG(hsmartcard->Instance->GTPR, (uint16_t)(USART_GTPR_GT | USART_GTPR_PSC), (uint16_t)tmpreg); 02524 02525 /*-------------------------- USART RTOR Configuration ----------------------*/ 02526 tmpreg = ((uint32_t)hsmartcard->Init.BlockLength << USART_RTOR_BLEN_Pos); 02527 if (hsmartcard->Init.TimeOutEnable == SMARTCARD_TIMEOUT_ENABLE) 02528 { 02529 assert_param(IS_SMARTCARD_TIMEOUT_VALUE(hsmartcard->Init.TimeOutValue)); 02530 tmpreg |= (uint32_t) hsmartcard->Init.TimeOutValue; 02531 } 02532 MODIFY_REG(hsmartcard->Instance->RTOR, (USART_RTOR_RTO | USART_RTOR_BLEN), tmpreg); 02533 02534 /*-------------------------- USART BRR Configuration -----------------------*/ 02535 SMARTCARD_GETCLOCKSOURCE(hsmartcard, clocksource); 02536 tmpreg = 0U; 02537 switch (clocksource) 02538 { 02539 case SMARTCARD_CLOCKSOURCE_PCLK1: 02540 pclk = HAL_RCC_GetPCLK1Freq(); 02541 #if defined(USART_PRESC_PRESCALER) 02542 tmpreg = (uint32_t)(((pclk / SMARTCARDPrescTable[hsmartcard->Init.ClockPrescaler]) + 02543 (hsmartcard->Init.BaudRate / 2U)) / hsmartcard->Init.BaudRate); 02544 #else 02545 tmpreg = (uint32_t)((pclk + (hsmartcard->Init.BaudRate / 2U)) / hsmartcard->Init.BaudRate); 02546 #endif /* USART_PRESC_PRESCALER */ 02547 break; 02548 case SMARTCARD_CLOCKSOURCE_PCLK2: 02549 pclk = HAL_RCC_GetPCLK2Freq(); 02550 #if defined(USART_PRESC_PRESCALER) 02551 tmpreg = (uint32_t)(((pclk / SMARTCARDPrescTable[hsmartcard->Init.ClockPrescaler]) + 02552 (hsmartcard->Init.BaudRate / 2U)) / hsmartcard->Init.BaudRate); 02553 #else 02554 tmpreg = (uint32_t)((pclk + (hsmartcard->Init.BaudRate / 2U)) / hsmartcard->Init.BaudRate); 02555 #endif /* USART_PRESC_PRESCALER */ 02556 break; 02557 case SMARTCARD_CLOCKSOURCE_HSI: 02558 #if defined(USART_PRESC_PRESCALER) 02559 tmpreg = (uint32_t)(((HSI_VALUE / SMARTCARDPrescTable[hsmartcard->Init.ClockPrescaler]) + 02560 (hsmartcard->Init.BaudRate / 2U)) / hsmartcard->Init.BaudRate); 02561 #else 02562 tmpreg = (uint32_t)((HSI_VALUE + (hsmartcard->Init.BaudRate / 2U)) / hsmartcard->Init.BaudRate); 02563 #endif /* USART_PRESC_PRESCALER */ 02564 break; 02565 case SMARTCARD_CLOCKSOURCE_SYSCLK: 02566 pclk = HAL_RCC_GetSysClockFreq(); 02567 #if defined(USART_PRESC_PRESCALER) 02568 tmpreg = (uint32_t)(((pclk / SMARTCARDPrescTable[hsmartcard->Init.ClockPrescaler]) + 02569 (hsmartcard->Init.BaudRate / 2U)) / hsmartcard->Init.BaudRate); 02570 #else 02571 tmpreg = (uint32_t)((pclk + (hsmartcard->Init.BaudRate / 2U)) / hsmartcard->Init.BaudRate); 02572 #endif /* USART_PRESC_PRESCALER */ 02573 break; 02574 case SMARTCARD_CLOCKSOURCE_LSE: 02575 #if defined(USART_PRESC_PRESCALER) 02576 tmpreg = (uint32_t)(((uint16_t)(LSE_VALUE / SMARTCARDPrescTable[hsmartcard->Init.ClockPrescaler]) + 02577 (hsmartcard->Init.BaudRate / 2U)) / hsmartcard->Init.BaudRate); 02578 #else 02579 tmpreg = (uint32_t)((LSE_VALUE + (hsmartcard->Init.BaudRate / 2U)) / hsmartcard->Init.BaudRate); 02580 #endif /* USART_PRESC_PRESCALER */ 02581 break; 02582 default: 02583 ret = HAL_ERROR; 02584 break; 02585 } 02586 02587 /* USARTDIV must be greater than or equal to 0d16 */ 02588 if ((tmpreg >= USART_BRR_MIN) && (tmpreg <= USART_BRR_MAX)) 02589 { 02590 hsmartcard->Instance->BRR = (uint16_t)tmpreg; 02591 } 02592 else 02593 { 02594 ret = HAL_ERROR; 02595 } 02596 02597 #if defined(USART_CR1_FIFOEN) 02598 /* Initialize the number of data to process during RX/TX ISR execution */ 02599 hsmartcard->NbTxDataToProcess = 1U; 02600 hsmartcard->NbRxDataToProcess = 1U; 02601 #endif /* USART_CR1_FIFOEN */ 02602 02603 /* Clear ISR function pointers */ 02604 hsmartcard->RxISR = NULL; 02605 hsmartcard->TxISR = NULL; 02606 02607 return ret; 02608 } 02609 02610 02611 /** 02612 * @brief Configure the SMARTCARD associated USART peripheral advanced features. 02613 * @param hsmartcard Pointer to a SMARTCARD_HandleTypeDef structure that contains 02614 * the configuration information for the specified SMARTCARD module. 02615 * @retval None 02616 */ 02617 static void SMARTCARD_AdvFeatureConfig(SMARTCARD_HandleTypeDef *hsmartcard) 02618 { 02619 /* Check whether the set of advanced features to configure is properly set */ 02620 assert_param(IS_SMARTCARD_ADVFEATURE_INIT(hsmartcard->AdvancedInit.AdvFeatureInit)); 02621 02622 /* if required, configure TX pin active level inversion */ 02623 if (HAL_IS_BIT_SET(hsmartcard->AdvancedInit.AdvFeatureInit, SMARTCARD_ADVFEATURE_TXINVERT_INIT)) 02624 { 02625 assert_param(IS_SMARTCARD_ADVFEATURE_TXINV(hsmartcard->AdvancedInit.TxPinLevelInvert)); 02626 MODIFY_REG(hsmartcard->Instance->CR2, USART_CR2_TXINV, hsmartcard->AdvancedInit.TxPinLevelInvert); 02627 } 02628 02629 /* if required, configure RX pin active level inversion */ 02630 if (HAL_IS_BIT_SET(hsmartcard->AdvancedInit.AdvFeatureInit, SMARTCARD_ADVFEATURE_RXINVERT_INIT)) 02631 { 02632 assert_param(IS_SMARTCARD_ADVFEATURE_RXINV(hsmartcard->AdvancedInit.RxPinLevelInvert)); 02633 MODIFY_REG(hsmartcard->Instance->CR2, USART_CR2_RXINV, hsmartcard->AdvancedInit.RxPinLevelInvert); 02634 } 02635 02636 /* if required, configure data inversion */ 02637 if (HAL_IS_BIT_SET(hsmartcard->AdvancedInit.AdvFeatureInit, SMARTCARD_ADVFEATURE_DATAINVERT_INIT)) 02638 { 02639 assert_param(IS_SMARTCARD_ADVFEATURE_DATAINV(hsmartcard->AdvancedInit.DataInvert)); 02640 MODIFY_REG(hsmartcard->Instance->CR2, USART_CR2_DATAINV, hsmartcard->AdvancedInit.DataInvert); 02641 } 02642 02643 /* if required, configure RX/TX pins swap */ 02644 if (HAL_IS_BIT_SET(hsmartcard->AdvancedInit.AdvFeatureInit, SMARTCARD_ADVFEATURE_SWAP_INIT)) 02645 { 02646 assert_param(IS_SMARTCARD_ADVFEATURE_SWAP(hsmartcard->AdvancedInit.Swap)); 02647 MODIFY_REG(hsmartcard->Instance->CR2, USART_CR2_SWAP, hsmartcard->AdvancedInit.Swap); 02648 } 02649 02650 /* if required, configure RX overrun detection disabling */ 02651 if (HAL_IS_BIT_SET(hsmartcard->AdvancedInit.AdvFeatureInit, SMARTCARD_ADVFEATURE_RXOVERRUNDISABLE_INIT)) 02652 { 02653 assert_param(IS_SMARTCARD_OVERRUN(hsmartcard->AdvancedInit.OverrunDisable)); 02654 MODIFY_REG(hsmartcard->Instance->CR3, USART_CR3_OVRDIS, hsmartcard->AdvancedInit.OverrunDisable); 02655 } 02656 02657 /* if required, configure DMA disabling on reception error */ 02658 if (HAL_IS_BIT_SET(hsmartcard->AdvancedInit.AdvFeatureInit, SMARTCARD_ADVFEATURE_DMADISABLEONERROR_INIT)) 02659 { 02660 assert_param(IS_SMARTCARD_ADVFEATURE_DMAONRXERROR(hsmartcard->AdvancedInit.DMADisableonRxError)); 02661 MODIFY_REG(hsmartcard->Instance->CR3, USART_CR3_DDRE, hsmartcard->AdvancedInit.DMADisableonRxError); 02662 } 02663 02664 /* if required, configure MSB first on communication line */ 02665 if (HAL_IS_BIT_SET(hsmartcard->AdvancedInit.AdvFeatureInit, SMARTCARD_ADVFEATURE_MSBFIRST_INIT)) 02666 { 02667 assert_param(IS_SMARTCARD_ADVFEATURE_MSBFIRST(hsmartcard->AdvancedInit.MSBFirst)); 02668 MODIFY_REG(hsmartcard->Instance->CR2, USART_CR2_MSBFIRST, hsmartcard->AdvancedInit.MSBFirst); 02669 } 02670 02671 } 02672 02673 /** 02674 * @brief Check the SMARTCARD Idle State. 02675 * @param hsmartcard Pointer to a SMARTCARD_HandleTypeDef structure that contains 02676 * the configuration information for the specified SMARTCARD module. 02677 * @retval HAL status 02678 */ 02679 static HAL_StatusTypeDef SMARTCARD_CheckIdleState(SMARTCARD_HandleTypeDef *hsmartcard) 02680 { 02681 uint32_t tickstart; 02682 02683 /* Initialize the SMARTCARD ErrorCode */ 02684 hsmartcard->ErrorCode = HAL_SMARTCARD_ERROR_NONE; 02685 02686 /* Init tickstart for timeout management */ 02687 tickstart = HAL_GetTick(); 02688 02689 /* Check if the Transmitter is enabled */ 02690 if ((hsmartcard->Instance->CR1 & USART_CR1_TE) == USART_CR1_TE) 02691 { 02692 /* Wait until TEACK flag is set */ 02693 if (SMARTCARD_WaitOnFlagUntilTimeout(hsmartcard, USART_ISR_TEACK, RESET, tickstart, 02694 SMARTCARD_TEACK_REACK_TIMEOUT) != HAL_OK) 02695 { 02696 /* Timeout occurred */ 02697 return HAL_TIMEOUT; 02698 } 02699 } 02700 /* Check if the Receiver is enabled */ 02701 if ((hsmartcard->Instance->CR1 & USART_CR1_RE) == USART_CR1_RE) 02702 { 02703 /* Wait until REACK flag is set */ 02704 if (SMARTCARD_WaitOnFlagUntilTimeout(hsmartcard, USART_ISR_REACK, RESET, tickstart, 02705 SMARTCARD_TEACK_REACK_TIMEOUT) != HAL_OK) 02706 { 02707 /* Timeout occurred */ 02708 return HAL_TIMEOUT; 02709 } 02710 } 02711 02712 /* Initialize the SMARTCARD states */ 02713 hsmartcard->gState = HAL_SMARTCARD_STATE_READY; 02714 hsmartcard->RxState = HAL_SMARTCARD_STATE_READY; 02715 02716 /* Process Unlocked */ 02717 __HAL_UNLOCK(hsmartcard); 02718 02719 return HAL_OK; 02720 } 02721 02722 /** 02723 * @brief Handle SMARTCARD Communication Timeout. It waits 02724 * until a flag is no longer in the specified status. 02725 * @param hsmartcard Pointer to a SMARTCARD_HandleTypeDef structure that contains 02726 * the configuration information for the specified SMARTCARD module. 02727 * @param Flag Specifies the SMARTCARD flag to check. 02728 * @param Status The actual Flag status (SET or RESET). 02729 * @param Tickstart Tick start value 02730 * @param Timeout Timeout duration. 02731 * @retval HAL status 02732 */ 02733 static HAL_StatusTypeDef SMARTCARD_WaitOnFlagUntilTimeout(SMARTCARD_HandleTypeDef *hsmartcard, uint32_t Flag, 02734 FlagStatus Status, uint32_t Tickstart, uint32_t Timeout) 02735 { 02736 /* Wait until flag is set */ 02737 while ((__HAL_SMARTCARD_GET_FLAG(hsmartcard, Flag) ? SET : RESET) == Status) 02738 { 02739 /* Check for the Timeout */ 02740 if (Timeout != HAL_MAX_DELAY) 02741 { 02742 if (((HAL_GetTick() - Tickstart) > Timeout) || (Timeout == 0U)) 02743 { 02744 /* Disable TXE, RXNE, PE and ERR (Frame error, noise error, overrun error) 02745 interrupts for the interrupt process */ 02746 #if defined(USART_CR1_FIFOEN) 02747 CLEAR_BIT(hsmartcard->Instance->CR1, (USART_CR1_RXNEIE_RXFNEIE | USART_CR1_PEIE | USART_CR1_TXEIE_TXFNFIE)); 02748 #else 02749 CLEAR_BIT(hsmartcard->Instance->CR1, (USART_CR1_RXNEIE | USART_CR1_PEIE | USART_CR1_TXEIE)); 02750 #endif /* USART_CR1_FIFOEN */ 02751 CLEAR_BIT(hsmartcard->Instance->CR3, USART_CR3_EIE); 02752 02753 hsmartcard->gState = HAL_SMARTCARD_STATE_READY; 02754 hsmartcard->RxState = HAL_SMARTCARD_STATE_READY; 02755 02756 /* Process Unlocked */ 02757 __HAL_UNLOCK(hsmartcard); 02758 return HAL_TIMEOUT; 02759 } 02760 } 02761 } 02762 return HAL_OK; 02763 } 02764 02765 02766 /** 02767 * @brief End ongoing Tx transfer on SMARTCARD peripheral (following error detection or Transmit completion). 02768 * @param hsmartcard Pointer to a SMARTCARD_HandleTypeDef structure that contains 02769 * the configuration information for the specified SMARTCARD module. 02770 * @retval None 02771 */ 02772 static void SMARTCARD_EndTxTransfer(SMARTCARD_HandleTypeDef *hsmartcard) 02773 { 02774 /* Disable TXEIE, TCIE and ERR (Frame error, noise error, overrun error) interrupts */ 02775 #if defined(USART_CR1_FIFOEN) 02776 CLEAR_BIT(hsmartcard->Instance->CR1, (USART_CR1_TXEIE_TXFNFIE | USART_CR1_TCIE)); 02777 #else 02778 CLEAR_BIT(hsmartcard->Instance->CR1, (USART_CR1_TXEIE | USART_CR1_TCIE)); 02779 #endif /* USART_CR1_FIFOEN */ 02780 CLEAR_BIT(hsmartcard->Instance->CR3, USART_CR3_EIE); 02781 02782 /* At end of Tx process, restore hsmartcard->gState to Ready */ 02783 hsmartcard->gState = HAL_SMARTCARD_STATE_READY; 02784 } 02785 02786 02787 /** 02788 * @brief End ongoing Rx transfer on UART peripheral (following error detection or Reception completion). 02789 * @param hsmartcard Pointer to a SMARTCARD_HandleTypeDef structure that contains 02790 * the configuration information for the specified SMARTCARD module. 02791 * @retval None 02792 */ 02793 static void SMARTCARD_EndRxTransfer(SMARTCARD_HandleTypeDef *hsmartcard) 02794 { 02795 /* Disable RXNE, PE and ERR (Frame error, noise error, overrun error) interrupts */ 02796 #if defined(USART_CR1_FIFOEN) 02797 CLEAR_BIT(hsmartcard->Instance->CR1, (USART_CR1_RXNEIE_RXFNEIE | USART_CR1_PEIE)); 02798 #else 02799 CLEAR_BIT(hsmartcard->Instance->CR1, (USART_CR1_RXNEIE | USART_CR1_PEIE)); 02800 #endif /* USART_CR1_FIFOEN */ 02801 CLEAR_BIT(hsmartcard->Instance->CR3, USART_CR3_EIE); 02802 02803 /* At end of Rx process, restore hsmartcard->RxState to Ready */ 02804 hsmartcard->RxState = HAL_SMARTCARD_STATE_READY; 02805 } 02806 02807 02808 /** 02809 * @brief DMA SMARTCARD transmit process complete callback. 02810 * @param hdma Pointer to a DMA_HandleTypeDef structure that contains 02811 * the configuration information for the specified DMA module. 02812 * @retval None 02813 */ 02814 static void SMARTCARD_DMATransmitCplt(DMA_HandleTypeDef *hdma) 02815 { 02816 SMARTCARD_HandleTypeDef *hsmartcard = (SMARTCARD_HandleTypeDef *)(hdma->Parent); 02817 hsmartcard->TxXferCount = 0U; 02818 02819 /* Disable the DMA transfer for transmit request by resetting the DMAT bit 02820 in the SMARTCARD associated USART CR3 register */ 02821 CLEAR_BIT(hsmartcard->Instance->CR3, USART_CR3_DMAT); 02822 02823 /* Enable the SMARTCARD Transmit Complete Interrupt */ 02824 __HAL_SMARTCARD_ENABLE_IT(hsmartcard, hsmartcard->AdvancedInit.TxCompletionIndication); 02825 } 02826 02827 /** 02828 * @brief DMA SMARTCARD receive process complete callback. 02829 * @param hdma Pointer to a DMA_HandleTypeDef structure that contains 02830 * the configuration information for the specified DMA module. 02831 * @retval None 02832 */ 02833 static void SMARTCARD_DMAReceiveCplt(DMA_HandleTypeDef *hdma) 02834 { 02835 SMARTCARD_HandleTypeDef *hsmartcard = (SMARTCARD_HandleTypeDef *)(hdma->Parent); 02836 hsmartcard->RxXferCount = 0U; 02837 02838 /* Disable PE and ERR (Frame error, noise error, overrun error) interrupts */ 02839 CLEAR_BIT(hsmartcard->Instance->CR1, USART_CR1_PEIE); 02840 CLEAR_BIT(hsmartcard->Instance->CR3, USART_CR3_EIE); 02841 02842 /* Disable the DMA transfer for the receiver request by resetting the DMAR bit 02843 in the SMARTCARD associated USART CR3 register */ 02844 CLEAR_BIT(hsmartcard->Instance->CR3, USART_CR3_DMAR); 02845 02846 /* At end of Rx process, restore hsmartcard->RxState to Ready */ 02847 hsmartcard->RxState = HAL_SMARTCARD_STATE_READY; 02848 02849 #if (USE_HAL_SMARTCARD_REGISTER_CALLBACKS == 1) 02850 /* Call registered Rx complete callback */ 02851 hsmartcard->RxCpltCallback(hsmartcard); 02852 #else 02853 /* Call legacy weak Rx complete callback */ 02854 HAL_SMARTCARD_RxCpltCallback(hsmartcard); 02855 #endif /* USE_HAL_SMARTCARD_REGISTER_CALLBACK */ 02856 } 02857 02858 /** 02859 * @brief DMA SMARTCARD communication error callback. 02860 * @param hdma Pointer to a DMA_HandleTypeDef structure that contains 02861 * the configuration information for the specified DMA module. 02862 * @retval None 02863 */ 02864 static void SMARTCARD_DMAError(DMA_HandleTypeDef *hdma) 02865 { 02866 SMARTCARD_HandleTypeDef *hsmartcard = (SMARTCARD_HandleTypeDef *)(hdma->Parent); 02867 02868 /* Stop SMARTCARD DMA Tx request if ongoing */ 02869 if (hsmartcard->gState == HAL_SMARTCARD_STATE_BUSY_TX) 02870 { 02871 if (HAL_IS_BIT_SET(hsmartcard->Instance->CR3, USART_CR3_DMAT)) 02872 { 02873 hsmartcard->TxXferCount = 0U; 02874 SMARTCARD_EndTxTransfer(hsmartcard); 02875 } 02876 } 02877 02878 /* Stop SMARTCARD DMA Rx request if ongoing */ 02879 if (hsmartcard->RxState == HAL_SMARTCARD_STATE_BUSY_RX) 02880 { 02881 if (HAL_IS_BIT_SET(hsmartcard->Instance->CR3, USART_CR3_DMAR)) 02882 { 02883 hsmartcard->RxXferCount = 0U; 02884 SMARTCARD_EndRxTransfer(hsmartcard); 02885 } 02886 } 02887 02888 hsmartcard->ErrorCode |= HAL_SMARTCARD_ERROR_DMA; 02889 #if (USE_HAL_SMARTCARD_REGISTER_CALLBACKS == 1) 02890 /* Call registered user error callback */ 02891 hsmartcard->ErrorCallback(hsmartcard); 02892 #else 02893 /* Call legacy weak user error callback */ 02894 HAL_SMARTCARD_ErrorCallback(hsmartcard); 02895 #endif /* USE_HAL_SMARTCARD_REGISTER_CALLBACK */ 02896 } 02897 02898 /** 02899 * @brief DMA SMARTCARD communication abort callback, when initiated by HAL services on Error 02900 * (To be called at end of DMA Abort procedure following error occurrence). 02901 * @param hdma DMA handle. 02902 * @retval None 02903 */ 02904 static void SMARTCARD_DMAAbortOnError(DMA_HandleTypeDef *hdma) 02905 { 02906 SMARTCARD_HandleTypeDef *hsmartcard = (SMARTCARD_HandleTypeDef *)(hdma->Parent); 02907 hsmartcard->RxXferCount = 0U; 02908 hsmartcard->TxXferCount = 0U; 02909 02910 #if (USE_HAL_SMARTCARD_REGISTER_CALLBACKS == 1) 02911 /* Call registered user error callback */ 02912 hsmartcard->ErrorCallback(hsmartcard); 02913 #else 02914 /* Call legacy weak user error callback */ 02915 HAL_SMARTCARD_ErrorCallback(hsmartcard); 02916 #endif /* USE_HAL_SMARTCARD_REGISTER_CALLBACK */ 02917 } 02918 02919 /** 02920 * @brief DMA SMARTCARD Tx communication abort callback, when initiated by user 02921 * (To be called at end of DMA Tx Abort procedure following user abort request). 02922 * @note When this callback is executed, User Abort complete call back is called only if no 02923 * Abort still ongoing for Rx DMA Handle. 02924 * @param hdma DMA handle. 02925 * @retval None 02926 */ 02927 static void SMARTCARD_DMATxAbortCallback(DMA_HandleTypeDef *hdma) 02928 { 02929 SMARTCARD_HandleTypeDef *hsmartcard = (SMARTCARD_HandleTypeDef *)(hdma->Parent); 02930 02931 hsmartcard->hdmatx->XferAbortCallback = NULL; 02932 02933 /* Check if an Abort process is still ongoing */ 02934 if (hsmartcard->hdmarx != NULL) 02935 { 02936 if (hsmartcard->hdmarx->XferAbortCallback != NULL) 02937 { 02938 return; 02939 } 02940 } 02941 02942 /* No Abort process still ongoing : All DMA channels are aborted, call user Abort Complete callback */ 02943 hsmartcard->TxXferCount = 0U; 02944 hsmartcard->RxXferCount = 0U; 02945 02946 /* Reset errorCode */ 02947 hsmartcard->ErrorCode = HAL_SMARTCARD_ERROR_NONE; 02948 02949 /* Clear the Error flags in the ICR register */ 02950 __HAL_SMARTCARD_CLEAR_FLAG(hsmartcard, 02951 SMARTCARD_CLEAR_OREF | SMARTCARD_CLEAR_NEF | SMARTCARD_CLEAR_PEF | SMARTCARD_CLEAR_FEF | 02952 SMARTCARD_CLEAR_RTOF | SMARTCARD_CLEAR_EOBF); 02953 02954 /* Restore hsmartcard->gState and hsmartcard->RxState to Ready */ 02955 hsmartcard->gState = HAL_SMARTCARD_STATE_READY; 02956 hsmartcard->RxState = HAL_SMARTCARD_STATE_READY; 02957 02958 #if (USE_HAL_SMARTCARD_REGISTER_CALLBACKS == 1) 02959 /* Call registered Abort complete callback */ 02960 hsmartcard->AbortCpltCallback(hsmartcard); 02961 #else 02962 /* Call legacy weak Abort complete callback */ 02963 HAL_SMARTCARD_AbortCpltCallback(hsmartcard); 02964 #endif /* USE_HAL_SMARTCARD_REGISTER_CALLBACK */ 02965 } 02966 02967 02968 /** 02969 * @brief DMA SMARTCARD Rx communication abort callback, when initiated by user 02970 * (To be called at end of DMA Rx Abort procedure following user abort request). 02971 * @note When this callback is executed, User Abort complete call back is called only if no 02972 * Abort still ongoing for Tx DMA Handle. 02973 * @param hdma DMA handle. 02974 * @retval None 02975 */ 02976 static void SMARTCARD_DMARxAbortCallback(DMA_HandleTypeDef *hdma) 02977 { 02978 SMARTCARD_HandleTypeDef *hsmartcard = (SMARTCARD_HandleTypeDef *)(hdma->Parent); 02979 02980 hsmartcard->hdmarx->XferAbortCallback = NULL; 02981 02982 /* Check if an Abort process is still ongoing */ 02983 if (hsmartcard->hdmatx != NULL) 02984 { 02985 if (hsmartcard->hdmatx->XferAbortCallback != NULL) 02986 { 02987 return; 02988 } 02989 } 02990 02991 /* No Abort process still ongoing : All DMA channels are aborted, call user Abort Complete callback */ 02992 hsmartcard->TxXferCount = 0U; 02993 hsmartcard->RxXferCount = 0U; 02994 02995 /* Reset errorCode */ 02996 hsmartcard->ErrorCode = HAL_SMARTCARD_ERROR_NONE; 02997 02998 /* Clear the Error flags in the ICR register */ 02999 __HAL_SMARTCARD_CLEAR_FLAG(hsmartcard, 03000 SMARTCARD_CLEAR_OREF | SMARTCARD_CLEAR_NEF | SMARTCARD_CLEAR_PEF | SMARTCARD_CLEAR_FEF | 03001 SMARTCARD_CLEAR_RTOF | SMARTCARD_CLEAR_EOBF); 03002 03003 /* Restore hsmartcard->gState and hsmartcard->RxState to Ready */ 03004 hsmartcard->gState = HAL_SMARTCARD_STATE_READY; 03005 hsmartcard->RxState = HAL_SMARTCARD_STATE_READY; 03006 03007 #if (USE_HAL_SMARTCARD_REGISTER_CALLBACKS == 1) 03008 /* Call registered Abort complete callback */ 03009 hsmartcard->AbortCpltCallback(hsmartcard); 03010 #else 03011 /* Call legacy weak Abort complete callback */ 03012 HAL_SMARTCARD_AbortCpltCallback(hsmartcard); 03013 #endif /* USE_HAL_SMARTCARD_REGISTER_CALLBACK */ 03014 } 03015 03016 03017 /** 03018 * @brief DMA SMARTCARD Tx communication abort callback, when initiated by user by a call to 03019 * HAL_SMARTCARD_AbortTransmit_IT API (Abort only Tx transfer) 03020 * (This callback is executed at end of DMA Tx Abort procedure following user abort request, 03021 * and leads to user Tx Abort Complete callback execution). 03022 * @param hdma DMA handle. 03023 * @retval None 03024 */ 03025 static void SMARTCARD_DMATxOnlyAbortCallback(DMA_HandleTypeDef *hdma) 03026 { 03027 SMARTCARD_HandleTypeDef *hsmartcard = (SMARTCARD_HandleTypeDef *)(hdma->Parent); 03028 03029 hsmartcard->TxXferCount = 0U; 03030 03031 /* Clear the Error flags in the ICR register */ 03032 __HAL_SMARTCARD_CLEAR_FLAG(hsmartcard, SMARTCARD_CLEAR_FEF); 03033 03034 /* Restore hsmartcard->gState to Ready */ 03035 hsmartcard->gState = HAL_SMARTCARD_STATE_READY; 03036 03037 #if (USE_HAL_SMARTCARD_REGISTER_CALLBACKS == 1) 03038 /* Call registered Abort Transmit Complete Callback */ 03039 hsmartcard->AbortTransmitCpltCallback(hsmartcard); 03040 #else 03041 /* Call legacy weak Abort Transmit Complete Callback */ 03042 HAL_SMARTCARD_AbortTransmitCpltCallback(hsmartcard); 03043 #endif /* USE_HAL_SMARTCARD_REGISTER_CALLBACK */ 03044 } 03045 03046 /** 03047 * @brief DMA SMARTCARD Rx communication abort callback, when initiated by user by a call to 03048 * HAL_SMARTCARD_AbortReceive_IT API (Abort only Rx transfer) 03049 * (This callback is executed at end of DMA Rx Abort procedure following user abort request, 03050 * and leads to user Rx Abort Complete callback execution). 03051 * @param hdma DMA handle. 03052 * @retval None 03053 */ 03054 static void SMARTCARD_DMARxOnlyAbortCallback(DMA_HandleTypeDef *hdma) 03055 { 03056 SMARTCARD_HandleTypeDef *hsmartcard = (SMARTCARD_HandleTypeDef *)(hdma->Parent); 03057 03058 hsmartcard->RxXferCount = 0U; 03059 03060 /* Clear the Error flags in the ICR register */ 03061 __HAL_SMARTCARD_CLEAR_FLAG(hsmartcard, 03062 SMARTCARD_CLEAR_OREF | SMARTCARD_CLEAR_NEF | SMARTCARD_CLEAR_PEF | SMARTCARD_CLEAR_FEF | 03063 SMARTCARD_CLEAR_RTOF | SMARTCARD_CLEAR_EOBF); 03064 03065 /* Restore hsmartcard->RxState to Ready */ 03066 hsmartcard->RxState = HAL_SMARTCARD_STATE_READY; 03067 03068 #if (USE_HAL_SMARTCARD_REGISTER_CALLBACKS == 1) 03069 /* Call registered Abort Receive Complete Callback */ 03070 hsmartcard->AbortReceiveCpltCallback(hsmartcard); 03071 #else 03072 /* Call legacy weak Abort Receive Complete Callback */ 03073 HAL_SMARTCARD_AbortReceiveCpltCallback(hsmartcard); 03074 #endif /* USE_HAL_SMARTCARD_REGISTER_CALLBACK */ 03075 } 03076 03077 /** 03078 * @brief Send an amount of data in non-blocking mode. 03079 * @note Function called under interruption only, once 03080 * interruptions have been enabled by HAL_SMARTCARD_Transmit_IT() 03081 * and when the FIFO mode is disabled. 03082 * @param hsmartcard Pointer to a SMARTCARD_HandleTypeDef structure that contains 03083 * the configuration information for the specified SMARTCARD module. 03084 * @retval None 03085 */ 03086 static void SMARTCARD_TxISR(SMARTCARD_HandleTypeDef *hsmartcard) 03087 { 03088 /* Check that a Tx process is ongoing */ 03089 if (hsmartcard->gState == HAL_SMARTCARD_STATE_BUSY_TX) 03090 { 03091 if (hsmartcard->TxXferCount == 0U) 03092 { 03093 /* Disable the SMARTCARD Transmit Data Register Empty Interrupt */ 03094 #if defined(USART_CR1_FIFOEN) 03095 CLEAR_BIT(hsmartcard->Instance->CR1, USART_CR1_TXEIE_TXFNFIE); 03096 #else 03097 CLEAR_BIT(hsmartcard->Instance->CR1, USART_CR1_TXEIE); 03098 #endif /* USART_CR1_FIFOEN */ 03099 03100 /* Enable the SMARTCARD Transmit Complete Interrupt */ 03101 __HAL_SMARTCARD_ENABLE_IT(hsmartcard, hsmartcard->AdvancedInit.TxCompletionIndication); 03102 } 03103 else 03104 { 03105 hsmartcard->Instance->TDR = (uint8_t)(*hsmartcard->pTxBuffPtr & 0xFFU); 03106 hsmartcard->pTxBuffPtr++; 03107 hsmartcard->TxXferCount--; 03108 } 03109 } 03110 } 03111 03112 #if defined(USART_CR1_FIFOEN) 03113 /** 03114 * @brief Send an amount of data in non-blocking mode. 03115 * @note Function called under interruption only, once 03116 * interruptions have been enabled by HAL_SMARTCARD_Transmit_IT() 03117 * and when the FIFO mode is enabled. 03118 * @param hsmartcard Pointer to a SMARTCARD_HandleTypeDef structure that contains 03119 * the configuration information for the specified SMARTCARD module. 03120 * @retval None 03121 */ 03122 static void SMARTCARD_TxISR_FIFOEN(SMARTCARD_HandleTypeDef *hsmartcard) 03123 { 03124 uint16_t nb_tx_data; 03125 03126 /* Check that a Tx process is ongoing */ 03127 if (hsmartcard->gState == HAL_SMARTCARD_STATE_BUSY_TX) 03128 { 03129 for (nb_tx_data = hsmartcard->NbTxDataToProcess ; nb_tx_data > 0U ; nb_tx_data--) 03130 { 03131 if (hsmartcard->TxXferCount == 0U) 03132 { 03133 /* Disable the SMARTCARD Transmit Data Register Empty Interrupt */ 03134 CLEAR_BIT(hsmartcard->Instance->CR1, USART_CR1_TXEIE_TXFNFIE); 03135 03136 /* Enable the SMARTCARD Transmit Complete Interrupt */ 03137 __HAL_SMARTCARD_ENABLE_IT(hsmartcard, hsmartcard->AdvancedInit.TxCompletionIndication); 03138 } 03139 else if (READ_BIT(hsmartcard->Instance->ISR, USART_ISR_TXE_TXFNF) != 0U) 03140 { 03141 hsmartcard->Instance->TDR = (uint8_t)(*hsmartcard->pTxBuffPtr & 0xFFU); 03142 hsmartcard->pTxBuffPtr++; 03143 hsmartcard->TxXferCount--; 03144 } 03145 else 03146 { 03147 /* Nothing to do */ 03148 } 03149 } 03150 } 03151 } 03152 03153 #endif /* USART_CR1_FIFOEN */ 03154 /** 03155 * @brief Wrap up transmission in non-blocking mode. 03156 * @param hsmartcard Pointer to a SMARTCARD_HandleTypeDef structure that contains 03157 * the configuration information for the specified SMARTCARD module. 03158 * @retval None 03159 */ 03160 static void SMARTCARD_EndTransmit_IT(SMARTCARD_HandleTypeDef *hsmartcard) 03161 { 03162 /* Disable the SMARTCARD Transmit Complete Interrupt */ 03163 __HAL_SMARTCARD_DISABLE_IT(hsmartcard, hsmartcard->AdvancedInit.TxCompletionIndication); 03164 03165 /* Check if a receive process is ongoing or not. If not disable ERR IT */ 03166 if (hsmartcard->RxState == HAL_SMARTCARD_STATE_READY) 03167 { 03168 /* Disable the SMARTCARD Error Interrupt: (Frame error) */ 03169 CLEAR_BIT(hsmartcard->Instance->CR3, USART_CR3_EIE); 03170 } 03171 03172 /* Disable the Peripheral first to update mode */ 03173 CLEAR_BIT(hsmartcard->Instance->CR1, USART_CR1_UE); 03174 if ((hsmartcard->Init.Mode == SMARTCARD_MODE_TX) 03175 && (hsmartcard->Init.NACKEnable == SMARTCARD_NACK_ENABLE)) 03176 { 03177 /* In case of TX only mode, if NACK is enabled, receiver block has been enabled 03178 for Transmit phase. Disable this receiver block. */ 03179 CLEAR_BIT(hsmartcard->Instance->CR1, USART_CR1_RE); 03180 } 03181 if ((hsmartcard->Init.Mode == SMARTCARD_MODE_TX_RX) 03182 || (hsmartcard->Init.NACKEnable == SMARTCARD_NACK_ENABLE)) 03183 { 03184 /* Perform a TX FIFO Flush at end of Tx phase, as all sent bytes are appearing in Rx Data register */ 03185 __HAL_SMARTCARD_FLUSH_DRREGISTER(hsmartcard); 03186 } 03187 SET_BIT(hsmartcard->Instance->CR1, USART_CR1_UE); 03188 03189 /* Tx process is ended, restore hsmartcard->gState to Ready */ 03190 hsmartcard->gState = HAL_SMARTCARD_STATE_READY; 03191 03192 /* Clear TxISR function pointer */ 03193 hsmartcard->TxISR = NULL; 03194 03195 #if (USE_HAL_SMARTCARD_REGISTER_CALLBACKS == 1) 03196 /* Call registered Tx complete callback */ 03197 hsmartcard->TxCpltCallback(hsmartcard); 03198 #else 03199 /* Call legacy weak Tx complete callback */ 03200 HAL_SMARTCARD_TxCpltCallback(hsmartcard); 03201 #endif /* USE_HAL_SMARTCARD_REGISTER_CALLBACK */ 03202 } 03203 03204 /** 03205 * @brief Receive an amount of data in non-blocking mode. 03206 * @note Function called under interruption only, once 03207 * interruptions have been enabled by HAL_SMARTCARD_Receive_IT() 03208 * and when the FIFO mode is disabled. 03209 * @param hsmartcard Pointer to a SMARTCARD_HandleTypeDef structure that contains 03210 * the configuration information for the specified SMARTCARD module. 03211 * @retval None 03212 */ 03213 static void SMARTCARD_RxISR(SMARTCARD_HandleTypeDef *hsmartcard) 03214 { 03215 /* Check that a Rx process is ongoing */ 03216 if (hsmartcard->RxState == HAL_SMARTCARD_STATE_BUSY_RX) 03217 { 03218 *hsmartcard->pRxBuffPtr = (uint8_t)(hsmartcard->Instance->RDR & (uint8_t)0xFF); 03219 hsmartcard->pRxBuffPtr++; 03220 03221 hsmartcard->RxXferCount--; 03222 if (hsmartcard->RxXferCount == 0U) 03223 { 03224 #if defined(USART_CR1_FIFOEN) 03225 CLEAR_BIT(hsmartcard->Instance->CR1, USART_CR1_RXNEIE_RXFNEIE); 03226 #else 03227 CLEAR_BIT(hsmartcard->Instance->CR1, USART_CR1_RXNEIE); 03228 #endif /* USART_CR1_FIFOEN */ 03229 03230 /* Check if a transmit process is ongoing or not. If not disable ERR IT */ 03231 if (hsmartcard->gState == HAL_SMARTCARD_STATE_READY) 03232 { 03233 /* Disable the SMARTCARD Error Interrupt: (Frame error, noise error, overrun error) */ 03234 CLEAR_BIT(hsmartcard->Instance->CR3, USART_CR3_EIE); 03235 } 03236 03237 /* Disable the SMARTCARD Parity Error Interrupt */ 03238 CLEAR_BIT(hsmartcard->Instance->CR1, USART_CR1_PEIE); 03239 03240 hsmartcard->RxState = HAL_SMARTCARD_STATE_READY; 03241 03242 /* Clear RxISR function pointer */ 03243 hsmartcard->RxISR = NULL; 03244 03245 #if (USE_HAL_SMARTCARD_REGISTER_CALLBACKS == 1) 03246 /* Call registered Rx complete callback */ 03247 hsmartcard->RxCpltCallback(hsmartcard); 03248 #else 03249 /* Call legacy weak Rx complete callback */ 03250 HAL_SMARTCARD_RxCpltCallback(hsmartcard); 03251 #endif /* USE_HAL_SMARTCARD_REGISTER_CALLBACK */ 03252 } 03253 } 03254 else 03255 { 03256 /* Clear RXNE interrupt flag */ 03257 __HAL_SMARTCARD_SEND_REQ(hsmartcard, SMARTCARD_RXDATA_FLUSH_REQUEST); 03258 } 03259 } 03260 03261 #if defined(USART_CR1_FIFOEN) 03262 /** 03263 * @brief Receive an amount of data in non-blocking mode. 03264 * @note Function called under interruption only, once 03265 * interruptions have been enabled by HAL_SMARTCARD_Receive_IT() 03266 * and when the FIFO mode is enabled. 03267 * @param hsmartcard Pointer to a SMARTCARD_HandleTypeDef structure that contains 03268 * the configuration information for the specified SMARTCARD module. 03269 * @retval None 03270 */ 03271 static void SMARTCARD_RxISR_FIFOEN(SMARTCARD_HandleTypeDef *hsmartcard) 03272 { 03273 uint16_t nb_rx_data; 03274 uint16_t rxdatacount; 03275 03276 /* Check that a Rx process is ongoing */ 03277 if (hsmartcard->RxState == HAL_SMARTCARD_STATE_BUSY_RX) 03278 { 03279 for (nb_rx_data = hsmartcard->NbRxDataToProcess ; nb_rx_data > 0U ; nb_rx_data--) 03280 { 03281 *hsmartcard->pRxBuffPtr = (uint8_t)(hsmartcard->Instance->RDR & (uint8_t)0xFF); 03282 hsmartcard->pRxBuffPtr++; 03283 03284 hsmartcard->RxXferCount--; 03285 if (hsmartcard->RxXferCount == 0U) 03286 { 03287 CLEAR_BIT(hsmartcard->Instance->CR1, USART_CR1_RXNEIE_RXFNEIE); 03288 03289 /* Check if a transmit process is ongoing or not. If not disable ERR IT */ 03290 if (hsmartcard->gState == HAL_SMARTCARD_STATE_READY) 03291 { 03292 /* Disable the SMARTCARD Error Interrupt: (Frame error, noise error, overrun error) */ 03293 CLEAR_BIT(hsmartcard->Instance->CR3, USART_CR3_EIE); 03294 } 03295 03296 /* Disable the SMARTCARD Parity Error Interrupt */ 03297 CLEAR_BIT(hsmartcard->Instance->CR1, USART_CR1_PEIE); 03298 03299 hsmartcard->RxState = HAL_SMARTCARD_STATE_READY; 03300 03301 /* Clear RxISR function pointer */ 03302 hsmartcard->RxISR = NULL; 03303 03304 #if (USE_HAL_SMARTCARD_REGISTER_CALLBACKS == 1) 03305 /* Call registered Rx complete callback */ 03306 hsmartcard->RxCpltCallback(hsmartcard); 03307 #else 03308 /* Call legacy weak Rx complete callback */ 03309 HAL_SMARTCARD_RxCpltCallback(hsmartcard); 03310 #endif /* USE_HAL_SMARTCARD_REGISTER_CALLBACK */ 03311 } 03312 } 03313 03314 /* When remaining number of bytes to receive is less than the RX FIFO 03315 threshold, next incoming frames are processed as if FIFO mode was 03316 disabled (i.e. one interrupt per received frame). 03317 */ 03318 rxdatacount = hsmartcard->RxXferCount; 03319 if (((rxdatacount != 0U)) && (rxdatacount < hsmartcard->NbRxDataToProcess)) 03320 { 03321 /* Disable the UART RXFT interrupt*/ 03322 CLEAR_BIT(hsmartcard->Instance->CR3, USART_CR3_RXFTIE); 03323 03324 /* Update the RxISR function pointer */ 03325 hsmartcard->RxISR = SMARTCARD_RxISR; 03326 03327 /* Enable the UART Data Register Not Empty interrupt */ 03328 SET_BIT(hsmartcard->Instance->CR1, USART_CR1_RXNEIE_RXFNEIE); 03329 } 03330 } 03331 else 03332 { 03333 /* Clear RXNE interrupt flag */ 03334 __HAL_SMARTCARD_SEND_REQ(hsmartcard, SMARTCARD_RXDATA_FLUSH_REQUEST); 03335 } 03336 } 03337 03338 #endif /* USART_CR1_FIFOEN */ 03339 /** 03340 * @} 03341 */ 03342 03343 #endif /* HAL_SMARTCARD_MODULE_ENABLED */ 03344 /** 03345 * @} 03346 */ 03347 03348 /** 03349 * @} 03350 */ 03351