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