Back to home page

LXR

 
 

    


File indexing completed on 2025-05-11 08:23:09

0001 /**
0002   ******************************************************************************
0003   * @file    stm32h7xx_hal_smartcard.c
0004   * @author  MCD Application Team
0005   * @brief   SMARTCARD HAL module driver.
0006   *          This file provides firmware functions to manage the following
0007   *          functionalities of the SMARTCARD peripheral:
0008   *           + Initialization and de-initialization functions
0009   *           + IO operation functions
0010   *           + Peripheral Control functions
0011   *           + Peripheral State and Error functions
0012   *
0013   ******************************************************************************
0014   * @attention
0015   *
0016   * Copyright (c) 2017 STMicroelectronics.
0017   * All rights reserved.
0018   *
0019   * This software is licensed under terms that can be found in the LICENSE file
0020   * in the root directory of this software component.
0021   * If no LICENSE file comes with this software, it is provided AS-IS.
0022   *
0023   ******************************************************************************
0024   @verbatim
0025   ==============================================================================
0026                         ##### How to use this driver #####
0027   ==============================================================================
0028   [..]
0029     The SMARTCARD HAL driver can be used as follows:
0030 
0031     (#) Declare a SMARTCARD_HandleTypeDef handle structure (eg. SMARTCARD_HandleTypeDef hsmartcard).
0032     (#) Associate a USART to the SMARTCARD handle hsmartcard.
0033     (#) Initialize the SMARTCARD low level resources by implementing the HAL_SMARTCARD_MspInit() API:
0034         (++) Enable the USARTx interface clock.
0035         (++) USART pins configuration:
0036              (+++) Enable the clock for the USART GPIOs.
0037              (+++) Configure the USART pins (TX as alternate function pull-up, RX as alternate function Input).
0038         (++) NVIC configuration if you need to use interrupt process (HAL_SMARTCARD_Transmit_IT()
0039              and HAL_SMARTCARD_Receive_IT() APIs):
0040              (+++) Configure the USARTx interrupt priority.
0041              (+++) Enable the NVIC USART IRQ handle.
0042         (++) DMA Configuration if you need to use DMA process (HAL_SMARTCARD_Transmit_DMA()
0043              and HAL_SMARTCARD_Receive_DMA() APIs):
0044              (+++) Declare a DMA handle structure for the Tx/Rx channel.
0045              (+++) Enable the DMAx interface clock.
0046              (+++) Configure the declared DMA handle structure with the required Tx/Rx parameters.
0047              (+++) Configure the DMA Tx/Rx channel.
0048              (+++) Associate the initialized DMA handle to the SMARTCARD DMA Tx/Rx handle.
0049              (+++) Configure the priority and enable the NVIC for the transfer complete
0050                    interrupt on the DMA Tx/Rx channel.
0051 
0052     (#) Program the Baud Rate, Parity, Mode(Receiver/Transmitter), clock enabling/disabling and accordingly,
0053         the clock parameters (parity, phase, last bit), prescaler value, guard time and NACK on transmission
0054         error enabling or disabling in the hsmartcard handle Init structure.
0055 
0056     (#) If required, program SMARTCARD advanced features (TX/RX pins swap, TimeOut, auto-retry counter,...)
0057         in the hsmartcard handle AdvancedInit structure.
0058 
0059     (#) Initialize the SMARTCARD registers by calling the HAL_SMARTCARD_Init() API:
0060         (++) This API configures also the low level Hardware GPIO, CLOCK, CORTEX...etc)
0061              by calling the customized HAL_SMARTCARD_MspInit() API.
0062         [..]
0063         (@) The specific SMARTCARD interrupts (Transmission complete interrupt,
0064              RXNE interrupt and Error Interrupts) will be managed using the macros
0065              __HAL_SMARTCARD_ENABLE_IT() and __HAL_SMARTCARD_DISABLE_IT() inside the transmit and receive process.
0066 
0067     [..]
0068     [..] Three operation modes are available within this driver :
0069 
0070      *** Polling mode IO operation ***
0071      =================================
0072      [..]
0073        (+) Send an amount of data in blocking mode using HAL_SMARTCARD_Transmit()
0074        (+) Receive an amount of data in blocking mode using HAL_SMARTCARD_Receive()
0075 
0076      *** Interrupt mode IO operation ***
0077      ===================================
0078      [..]
0079        (+) Send an amount of data in non-blocking mode using HAL_SMARTCARD_Transmit_IT()
0080        (+) At transmission end of transfer HAL_SMARTCARD_TxCpltCallback() is executed and user can
0081             add his own code by customization of function pointer HAL_SMARTCARD_TxCpltCallback()
0082        (+) Receive an amount of data in non-blocking mode using HAL_SMARTCARD_Receive_IT()
0083        (+) At reception end of transfer HAL_SMARTCARD_RxCpltCallback() is executed and user can
0084             add his own code by customization of function pointer HAL_SMARTCARD_RxCpltCallback()
0085        (+) In case of transfer Error, HAL_SMARTCARD_ErrorCallback() function is executed and user can
0086             add his own code by customization of function pointer HAL_SMARTCARD_ErrorCallback()
0087 
0088      *** DMA mode IO operation ***
0089      ==============================
0090      [..]
0091        (+) Send an amount of data in non-blocking mode (DMA) using HAL_SMARTCARD_Transmit_DMA()
0092        (+) At transmission end of transfer HAL_SMARTCARD_TxCpltCallback() is executed and user can
0093             add his own code by customization of function pointer HAL_SMARTCARD_TxCpltCallback()
0094        (+) Receive an amount of data in non-blocking mode (DMA) using HAL_SMARTCARD_Receive_DMA()
0095        (+) At reception end of transfer HAL_SMARTCARD_RxCpltCallback() is executed and user can
0096             add his own code by customization of function pointer HAL_SMARTCARD_RxCpltCallback()
0097        (+) In case of transfer Error, HAL_SMARTCARD_ErrorCallback() function is executed and user can
0098             add his own code by customization of function pointer HAL_SMARTCARD_ErrorCallback()
0099 
0100      *** SMARTCARD HAL driver macros list ***
0101      ========================================
0102      [..]
0103        Below the list of most used macros in SMARTCARD HAL driver.
0104 
0105        (+) __HAL_SMARTCARD_GET_FLAG : Check whether or not the specified SMARTCARD flag is set
0106        (+) __HAL_SMARTCARD_CLEAR_FLAG : Clear the specified SMARTCARD pending flag
0107        (+) __HAL_SMARTCARD_ENABLE_IT: Enable the specified SMARTCARD interrupt
0108        (+) __HAL_SMARTCARD_DISABLE_IT: Disable the specified SMARTCARD interrupt
0109        (+) __HAL_SMARTCARD_GET_IT_SOURCE: Check whether or not the specified SMARTCARD interrupt is enabled
0110 
0111      [..]
0112        (@) You can refer to the SMARTCARD HAL driver header file for more useful macros
0113 
0114     ##### Callback registration #####
0115     ==================================
0116 
0117     [..]
0118     The compilation define USE_HAL_SMARTCARD_REGISTER_CALLBACKS when set to 1
0119     allows the user to configure dynamically the driver callbacks.
0120 
0121     [..]
0122     Use Function HAL_SMARTCARD_RegisterCallback() to register a user callback.
0123     Function HAL_SMARTCARD_RegisterCallback() allows to register following callbacks:
0124     (+) TxCpltCallback            : Tx Complete Callback.
0125     (+) RxCpltCallback            : Rx Complete Callback.
0126     (+) ErrorCallback             : Error Callback.
0127     (+) AbortCpltCallback         : Abort Complete Callback.
0128     (+) AbortTransmitCpltCallback : Abort Transmit Complete Callback.
0129     (+) AbortReceiveCpltCallback  : Abort Receive Complete Callback.
0130     (+) RxFifoFullCallback        : Rx Fifo Full Callback.
0131     (+) TxFifoEmptyCallback       : Tx Fifo Empty Callback.
0132     (+) MspInitCallback           : SMARTCARD MspInit.
0133     (+) MspDeInitCallback         : SMARTCARD MspDeInit.
0134     This function takes as parameters the HAL peripheral handle, the Callback ID
0135     and a pointer to the user callback function.
0136 
0137     [..]
0138     Use function HAL_SMARTCARD_UnRegisterCallback() to reset a callback to the default
0139     weak function.
0140     HAL_SMARTCARD_UnRegisterCallback() takes as parameters the HAL peripheral handle,
0141     and the Callback ID.
0142     This function allows to reset following callbacks:
0143     (+) TxCpltCallback            : Tx Complete Callback.
0144     (+) RxCpltCallback            : Rx Complete Callback.
0145     (+) ErrorCallback             : Error Callback.
0146     (+) AbortCpltCallback         : Abort Complete Callback.
0147     (+) AbortTransmitCpltCallback : Abort Transmit Complete Callback.
0148     (+) AbortReceiveCpltCallback  : Abort Receive Complete Callback.
0149     (+) RxFifoFullCallback        : Rx Fifo Full Callback.
0150     (+) TxFifoEmptyCallback       : Tx Fifo Empty Callback.
0151     (+) MspInitCallback           : SMARTCARD MspInit.
0152     (+) MspDeInitCallback         : SMARTCARD MspDeInit.
0153 
0154     [..]
0155     By default, after the HAL_SMARTCARD_Init() and when the state is HAL_SMARTCARD_STATE_RESET
0156     all callbacks are set to the corresponding weak functions:
0157     examples HAL_SMARTCARD_TxCpltCallback(), HAL_SMARTCARD_RxCpltCallback().
0158     Exception done for MspInit and MspDeInit functions that are respectively
0159     reset to the legacy weak functions in the HAL_SMARTCARD_Init()
0160     and HAL_SMARTCARD_DeInit() only when these callbacks are null (not registered beforehand).
0161     If not, MspInit or MspDeInit are not null, the HAL_SMARTCARD_Init() and HAL_SMARTCARD_DeInit()
0162     keep and use the user MspInit/MspDeInit callbacks (registered beforehand).
0163 
0164     [..]
0165     Callbacks can be registered/unregistered in HAL_SMARTCARD_STATE_READY state only.
0166     Exception done MspInit/MspDeInit that can be registered/unregistered
0167     in HAL_SMARTCARD_STATE_READY or HAL_SMARTCARD_STATE_RESET state, thus registered (user)
0168     MspInit/DeInit callbacks can be used during the Init/DeInit.
0169     In that case first register the MspInit/MspDeInit user callbacks
0170     using HAL_SMARTCARD_RegisterCallback() before calling HAL_SMARTCARD_DeInit()
0171     or HAL_SMARTCARD_Init() function.
0172 
0173     [..]
0174     When The compilation define USE_HAL_SMARTCARD_REGISTER_CALLBACKS is set to 0 or
0175     not defined, the callback registration feature is not available
0176     and weak callbacks are used.
0177 
0178 
0179   @endverbatim
0180   ******************************************************************************
0181   */
0182 
0183 /* Includes ------------------------------------------------------------------*/
0184 #include "stm32h7xx_hal.h"
0185 
0186 /** @addtogroup STM32H7xx_HAL_Driver
0187   * @{
0188   */
0189 
0190 /** @defgroup SMARTCARD SMARTCARD
0191   * @ingroup RTEMSBSPsARMSTM32H7
0192   * @brief HAL SMARTCARD module driver
0193   * @{
0194   */
0195 
0196 #ifdef HAL_SMARTCARD_MODULE_ENABLED
0197 
0198 /* Private typedef -----------------------------------------------------------*/
0199 /* Private define ------------------------------------------------------------*/
0200 /** @defgroup SMARTCARD_Private_Constants SMARTCARD Private Constants
0201   * @ingroup RTEMSBSPsARMSTM32H7
0202   * @{
0203   */
0204 #define SMARTCARD_TEACK_REACK_TIMEOUT  1000U       /*!< SMARTCARD TX or RX enable acknowledge time-out value */
0205 
0206 #define USART_CR1_FIELDS  ((uint32_t)(USART_CR1_M | USART_CR1_PCE | USART_CR1_PS | USART_CR1_TE | \
0207                                       USART_CR1_RE | USART_CR1_OVER8| \
0208                                       USART_CR1_FIFOEN))  /*!< USART CR1 fields of parameters set by SMARTCARD_SetConfig API */
0209 
0210 #define USART_CR2_CLK_FIELDS  ((uint32_t)(USART_CR2_CLKEN | USART_CR2_CPOL | \
0211                                           USART_CR2_CPHA | USART_CR2_LBCL)) /*!< SMARTCARD clock-related USART CR2 fields of parameters */
0212 
0213 #define USART_CR2_FIELDS  ((uint32_t)(USART_CR2_RTOEN | USART_CR2_CLK_FIELDS | \
0214                                       USART_CR2_STOP)) /*!< USART CR2 fields of parameters set by SMARTCARD_SetConfig API */
0215 
0216 #define USART_CR3_FIELDS  ((uint32_t)(USART_CR3_ONEBIT | USART_CR3_NACK | USART_CR3_SCARCNT | \
0217                                       USART_CR3_TXFTCFG | USART_CR3_RXFTCFG )) /*!< USART CR3 fields of parameters set by SMARTCARD_SetConfig API */
0218 
0219 #define USART_BRR_MIN  0x10U        /*!< USART BRR minimum authorized value */
0220 
0221 #define USART_BRR_MAX  0x0000FFFFU  /*!< USART BRR maximum authorized value */
0222 /**
0223   * @}
0224   */
0225 
0226 /* Private macros ------------------------------------------------------------*/
0227 /* Private variables ---------------------------------------------------------*/
0228 /* Private function prototypes -----------------------------------------------*/
0229 /** @addtogroup SMARTCARD_Private_Functions
0230   * @{
0231   */
0232 #if (USE_HAL_SMARTCARD_REGISTER_CALLBACKS == 1)
0233 void SMARTCARD_InitCallbacksToDefault(SMARTCARD_HandleTypeDef *hsmartcard);
0234 #endif /* USE_HAL_SMARTCARD_REGISTER_CALLBACKS */
0235 static HAL_StatusTypeDef SMARTCARD_SetConfig(SMARTCARD_HandleTypeDef *hsmartcard);
0236 static void SMARTCARD_AdvFeatureConfig(SMARTCARD_HandleTypeDef *hsmartcard);
0237 static HAL_StatusTypeDef SMARTCARD_CheckIdleState(SMARTCARD_HandleTypeDef *hsmartcard);
0238 static HAL_StatusTypeDef SMARTCARD_WaitOnFlagUntilTimeout(SMARTCARD_HandleTypeDef *hsmartcard, uint32_t Flag,
0239                                                           FlagStatus Status, uint32_t Tickstart, uint32_t Timeout);
0240 static void SMARTCARD_EndTxTransfer(SMARTCARD_HandleTypeDef *hsmartcard);
0241 static void SMARTCARD_EndRxTransfer(SMARTCARD_HandleTypeDef *hsmartcard);
0242 static void SMARTCARD_DMATransmitCplt(DMA_HandleTypeDef *hdma);
0243 static void SMARTCARD_DMAReceiveCplt(DMA_HandleTypeDef *hdma);
0244 static void SMARTCARD_DMAError(DMA_HandleTypeDef *hdma);
0245 static void SMARTCARD_DMAAbortOnError(DMA_HandleTypeDef *hdma);
0246 static void SMARTCARD_DMATxAbortCallback(DMA_HandleTypeDef *hdma);
0247 static void SMARTCARD_DMARxAbortCallback(DMA_HandleTypeDef *hdma);
0248 static void SMARTCARD_DMATxOnlyAbortCallback(DMA_HandleTypeDef *hdma);
0249 static void SMARTCARD_DMARxOnlyAbortCallback(DMA_HandleTypeDef *hdma);
0250 static void SMARTCARD_TxISR(SMARTCARD_HandleTypeDef *hsmartcard);
0251 static void SMARTCARD_TxISR_FIFOEN(SMARTCARD_HandleTypeDef *hsmartcard);
0252 static void SMARTCARD_EndTransmit_IT(SMARTCARD_HandleTypeDef *hsmartcard);
0253 static void SMARTCARD_RxISR(SMARTCARD_HandleTypeDef *hsmartcard);
0254 static void SMARTCARD_RxISR_FIFOEN(SMARTCARD_HandleTypeDef *hsmartcard);
0255 /**
0256   * @}
0257   */
0258 
0259 /* Exported functions --------------------------------------------------------*/
0260 
0261 /** @defgroup SMARTCARD_Exported_Functions SMARTCARD Exported Functions
0262   * @ingroup RTEMSBSPsARMSTM32H7
0263   * @{
0264   */
0265 
0266 /** @defgroup SMARTCARD_Exported_Functions_Group1 Initialization and de-initialization functions
0267   * @ingroup RTEMSBSPsARMSTM32H7
0268   * @brief    Initialization and Configuration functions
0269   *
0270 @verbatim
0271   ==============================================================================
0272               ##### Initialization and Configuration functions #####
0273   ==============================================================================
0274   [..]
0275   This subsection provides a set of functions allowing to initialize the USARTx
0276   associated to the SmartCard.
0277   (+) These parameters can be configured:
0278       (++) Baud Rate
0279       (++) Parity: parity should be enabled, frame Length is fixed to 8 bits plus parity
0280       (++) Receiver/transmitter modes
0281       (++) Synchronous mode (and if enabled, phase, polarity and last bit parameters)
0282       (++) Prescaler value
0283       (++) Guard bit time
0284       (++) NACK enabling or disabling on transmission error
0285 
0286   (+) The following advanced features can be configured as well:
0287       (++) TX and/or RX pin level inversion
0288       (++) data logical level inversion
0289       (++) RX and TX pins swap
0290       (++) RX overrun detection disabling
0291       (++) DMA disabling on RX error
0292       (++) MSB first on communication line
0293       (++) Time out enabling (and if activated, timeout value)
0294       (++) Block length
0295       (++) Auto-retry counter
0296   [..]
0297   The HAL_SMARTCARD_Init() API follows the USART synchronous configuration procedures
0298   (details for the procedures are available in reference manual).
0299 
0300 @endverbatim
0301 
0302   The USART frame format is given in the following table:
0303 
0304     Table 1. USART frame format.
0305     +---------------------------------------------------------------+
0306     | M1M0 bits |  PCE bit  |            USART frame                |
0307     |-----------------------|---------------------------------------|
0308     |     01    |    1      |    | SB | 8 bit data | PB | STB |     |
0309     +---------------------------------------------------------------+
0310 
0311 
0312   * @{
0313   */
0314 
0315 /**
0316   * @brief  Initialize the SMARTCARD mode according to the specified
0317   *         parameters in the SMARTCARD_HandleTypeDef and initialize the associated handle.
0318   * @param  hsmartcard Pointer to a SMARTCARD_HandleTypeDef structure that contains
0319   *                    the configuration information for the specified SMARTCARD module.
0320   * @retval HAL status
0321   */
0322 HAL_StatusTypeDef HAL_SMARTCARD_Init(SMARTCARD_HandleTypeDef *hsmartcard)
0323 {
0324   /* Check the SMARTCARD handle allocation */
0325   if (hsmartcard == NULL)
0326   {
0327     return HAL_ERROR;
0328   }
0329 
0330   /* Check the USART associated to the SMARTCARD handle */
0331   assert_param(IS_SMARTCARD_INSTANCE(hsmartcard->Instance));
0332 
0333   if (hsmartcard->gState == HAL_SMARTCARD_STATE_RESET)
0334   {
0335     /* Allocate lock resource and initialize it */
0336     hsmartcard->Lock = HAL_UNLOCKED;
0337 
0338 #if USE_HAL_SMARTCARD_REGISTER_CALLBACKS == 1
0339     SMARTCARD_InitCallbacksToDefault(hsmartcard);
0340 
0341     if (hsmartcard->MspInitCallback == NULL)
0342     {
0343       hsmartcard->MspInitCallback = HAL_SMARTCARD_MspInit;
0344     }
0345 
0346     /* Init the low level hardware */
0347     hsmartcard->MspInitCallback(hsmartcard);
0348 #else
0349     /* Init the low level hardware : GPIO, CLOCK */
0350     HAL_SMARTCARD_MspInit(hsmartcard);
0351 #endif /* USE_HAL_SMARTCARD_REGISTER_CALLBACKS */
0352   }
0353 
0354   hsmartcard->gState = HAL_SMARTCARD_STATE_BUSY;
0355 
0356   /* Disable the Peripheral to set smartcard mode */
0357   CLEAR_BIT(hsmartcard->Instance->CR1, USART_CR1_UE);
0358 
0359   /* In SmartCard mode, the following bits must be kept cleared:
0360   - LINEN in the USART_CR2 register,
0361   - HDSEL and IREN  bits in the USART_CR3 register.*/
0362   CLEAR_BIT(hsmartcard->Instance->CR2, USART_CR2_LINEN);
0363   CLEAR_BIT(hsmartcard->Instance->CR3, (USART_CR3_HDSEL | USART_CR3_IREN));
0364 
0365   /* set the USART in SMARTCARD mode */
0366   SET_BIT(hsmartcard->Instance->CR3, USART_CR3_SCEN);
0367 
0368   /* Set the SMARTCARD Communication parameters */
0369   if (SMARTCARD_SetConfig(hsmartcard) == HAL_ERROR)
0370   {
0371     return HAL_ERROR;
0372   }
0373 
0374   /* Set the SMARTCARD transmission completion indication */
0375   SMARTCARD_TRANSMISSION_COMPLETION_SETTING(hsmartcard);
0376 
0377   if (hsmartcard->AdvancedInit.AdvFeatureInit != SMARTCARD_ADVFEATURE_NO_INIT)
0378   {
0379     SMARTCARD_AdvFeatureConfig(hsmartcard);
0380   }
0381 
0382   /* Enable the Peripheral */
0383   SET_BIT(hsmartcard->Instance->CR1, USART_CR1_UE);
0384 
0385   /* TEACK and/or REACK to check before moving hsmartcard->gState and hsmartcard->RxState to Ready */
0386   return (SMARTCARD_CheckIdleState(hsmartcard));
0387 }
0388 
0389 /**
0390   * @brief  DeInitialize the SMARTCARD peripheral.
0391   * @param  hsmartcard Pointer to a SMARTCARD_HandleTypeDef structure that contains
0392   *                    the configuration information for the specified SMARTCARD module.
0393   * @retval HAL status
0394   */
0395 HAL_StatusTypeDef HAL_SMARTCARD_DeInit(SMARTCARD_HandleTypeDef *hsmartcard)
0396 {
0397   /* Check the SMARTCARD handle allocation */
0398   if (hsmartcard == NULL)
0399   {
0400     return HAL_ERROR;
0401   }
0402 
0403   /* Check the USART/UART associated to the SMARTCARD handle */
0404   assert_param(IS_SMARTCARD_INSTANCE(hsmartcard->Instance));
0405 
0406   hsmartcard->gState = HAL_SMARTCARD_STATE_BUSY;
0407 
0408   /* Disable the Peripheral */
0409   CLEAR_BIT(hsmartcard->Instance->CR1, USART_CR1_UE);
0410 
0411   WRITE_REG(hsmartcard->Instance->CR1, 0x0U);
0412   WRITE_REG(hsmartcard->Instance->CR2, 0x0U);
0413   WRITE_REG(hsmartcard->Instance->CR3, 0x0U);
0414   WRITE_REG(hsmartcard->Instance->RTOR, 0x0U);
0415   WRITE_REG(hsmartcard->Instance->GTPR, 0x0U);
0416 
0417   /* DeInit the low level hardware */
0418 #if USE_HAL_SMARTCARD_REGISTER_CALLBACKS == 1
0419   if (hsmartcard->MspDeInitCallback == NULL)
0420   {
0421     hsmartcard->MspDeInitCallback = HAL_SMARTCARD_MspDeInit;
0422   }
0423   /* DeInit the low level hardware */
0424   hsmartcard->MspDeInitCallback(hsmartcard);
0425 #else
0426   HAL_SMARTCARD_MspDeInit(hsmartcard);
0427 #endif /* USE_HAL_SMARTCARD_REGISTER_CALLBACKS */
0428 
0429   hsmartcard->ErrorCode = HAL_SMARTCARD_ERROR_NONE;
0430   hsmartcard->gState    = HAL_SMARTCARD_STATE_RESET;
0431   hsmartcard->RxState   = HAL_SMARTCARD_STATE_RESET;
0432 
0433   /* Process Unlock */
0434   __HAL_UNLOCK(hsmartcard);
0435 
0436   return HAL_OK;
0437 }
0438 
0439 /**
0440   * @brief  Initialize the SMARTCARD MSP.
0441   * @param  hsmartcard Pointer to a SMARTCARD_HandleTypeDef structure that contains
0442   *                    the configuration information for the specified SMARTCARD module.
0443   * @retval None
0444   */
0445 __weak void HAL_SMARTCARD_MspInit(SMARTCARD_HandleTypeDef *hsmartcard)
0446 {
0447   /* Prevent unused argument(s) compilation warning */
0448   UNUSED(hsmartcard);
0449 
0450   /* NOTE : This function should not be modified, when the callback is needed,
0451             the HAL_SMARTCARD_MspInit can be implemented in the user file
0452    */
0453 }
0454 
0455 /**
0456   * @brief  DeInitialize the SMARTCARD MSP.
0457   * @param  hsmartcard Pointer to a SMARTCARD_HandleTypeDef structure that contains
0458   *                    the configuration information for the specified SMARTCARD module.
0459   * @retval None
0460   */
0461 __weak void HAL_SMARTCARD_MspDeInit(SMARTCARD_HandleTypeDef *hsmartcard)
0462 {
0463   /* Prevent unused argument(s) compilation warning */
0464   UNUSED(hsmartcard);
0465 
0466   /* NOTE : This function should not be modified, when the callback is needed,
0467             the HAL_SMARTCARD_MspDeInit can be implemented in the user file
0468    */
0469 }
0470 
0471 #if (USE_HAL_SMARTCARD_REGISTER_CALLBACKS == 1)
0472 /**
0473   * @brief  Register a User SMARTCARD Callback
0474   *         To be used to override the weak predefined callback
0475   * @note   The HAL_SMARTCARD_RegisterCallback() may be called before HAL_SMARTCARD_Init()
0476   *         in HAL_SMARTCARD_STATE_RESET to register callbacks for HAL_SMARTCARD_MSPINIT_CB_ID
0477   *         and HAL_SMARTCARD_MSPDEINIT_CB_ID
0478   * @param  hsmartcard smartcard handle
0479   * @param  CallbackID ID of the callback to be registered
0480   *         This parameter can be one of the following values:
0481   *           @arg @ref HAL_SMARTCARD_TX_COMPLETE_CB_ID Tx Complete Callback ID
0482   *           @arg @ref HAL_SMARTCARD_RX_COMPLETE_CB_ID Rx Complete Callback ID
0483   *           @arg @ref HAL_SMARTCARD_ERROR_CB_ID Error Callback ID
0484   *           @arg @ref HAL_SMARTCARD_ABORT_COMPLETE_CB_ID Abort Complete Callback ID
0485   *           @arg @ref HAL_SMARTCARD_ABORT_TRANSMIT_COMPLETE_CB_ID Abort Transmit Complete Callback ID
0486   *           @arg @ref HAL_SMARTCARD_ABORT_RECEIVE_COMPLETE_CB_ID Abort Receive Complete Callback ID
0487   *           @arg @ref HAL_SMARTCARD_RX_FIFO_FULL_CB_ID Rx Fifo Full Callback ID
0488   *           @arg @ref HAL_SMARTCARD_TX_FIFO_EMPTY_CB_ID Tx Fifo Empty Callback ID
0489   *           @arg @ref HAL_SMARTCARD_MSPINIT_CB_ID MspInit Callback ID
0490   *           @arg @ref HAL_SMARTCARD_MSPDEINIT_CB_ID MspDeInit Callback ID
0491   * @param  pCallback pointer to the Callback function
0492   * @retval HAL status
0493   */
0494 HAL_StatusTypeDef HAL_SMARTCARD_RegisterCallback(SMARTCARD_HandleTypeDef *hsmartcard,
0495                                                  HAL_SMARTCARD_CallbackIDTypeDef CallbackID,
0496                                                  pSMARTCARD_CallbackTypeDef pCallback)
0497 {
0498   HAL_StatusTypeDef status = HAL_OK;
0499 
0500   if (pCallback == NULL)
0501   {
0502     /* Update the error code */
0503     hsmartcard->ErrorCode |= HAL_SMARTCARD_ERROR_INVALID_CALLBACK;
0504 
0505     return HAL_ERROR;
0506   }
0507 
0508   if (hsmartcard->gState == HAL_SMARTCARD_STATE_READY)
0509   {
0510     switch (CallbackID)
0511     {
0512 
0513       case HAL_SMARTCARD_TX_COMPLETE_CB_ID :
0514         hsmartcard->TxCpltCallback = pCallback;
0515         break;
0516 
0517       case HAL_SMARTCARD_RX_COMPLETE_CB_ID :
0518         hsmartcard->RxCpltCallback = pCallback;
0519         break;
0520 
0521       case HAL_SMARTCARD_ERROR_CB_ID :
0522         hsmartcard->ErrorCallback = pCallback;
0523         break;
0524 
0525       case HAL_SMARTCARD_ABORT_COMPLETE_CB_ID :
0526         hsmartcard->AbortCpltCallback = pCallback;
0527         break;
0528 
0529       case HAL_SMARTCARD_ABORT_TRANSMIT_COMPLETE_CB_ID :
0530         hsmartcard->AbortTransmitCpltCallback = pCallback;
0531         break;
0532 
0533       case HAL_SMARTCARD_ABORT_RECEIVE_COMPLETE_CB_ID :
0534         hsmartcard->AbortReceiveCpltCallback = pCallback;
0535         break;
0536 
0537       case HAL_SMARTCARD_RX_FIFO_FULL_CB_ID :
0538         hsmartcard->RxFifoFullCallback = pCallback;
0539         break;
0540 
0541       case HAL_SMARTCARD_TX_FIFO_EMPTY_CB_ID :
0542         hsmartcard->TxFifoEmptyCallback = pCallback;
0543         break;
0544 
0545       case HAL_SMARTCARD_MSPINIT_CB_ID :
0546         hsmartcard->MspInitCallback = pCallback;
0547         break;
0548 
0549       case HAL_SMARTCARD_MSPDEINIT_CB_ID :
0550         hsmartcard->MspDeInitCallback = pCallback;
0551         break;
0552 
0553       default :
0554         /* Update the error code */
0555         hsmartcard->ErrorCode |= HAL_SMARTCARD_ERROR_INVALID_CALLBACK;
0556 
0557         /* Return error status */
0558         status =  HAL_ERROR;
0559         break;
0560     }
0561   }
0562   else if (hsmartcard->gState == HAL_SMARTCARD_STATE_RESET)
0563   {
0564     switch (CallbackID)
0565     {
0566       case HAL_SMARTCARD_MSPINIT_CB_ID :
0567         hsmartcard->MspInitCallback = pCallback;
0568         break;
0569 
0570       case HAL_SMARTCARD_MSPDEINIT_CB_ID :
0571         hsmartcard->MspDeInitCallback = pCallback;
0572         break;
0573 
0574       default :
0575         /* Update the error code */
0576         hsmartcard->ErrorCode |= HAL_SMARTCARD_ERROR_INVALID_CALLBACK;
0577 
0578         /* Return error status */
0579         status =  HAL_ERROR;
0580         break;
0581     }
0582   }
0583   else
0584   {
0585     /* Update the error code */
0586     hsmartcard->ErrorCode |= HAL_SMARTCARD_ERROR_INVALID_CALLBACK;
0587 
0588     /* Return error status */
0589     status =  HAL_ERROR;
0590   }
0591 
0592   return status;
0593 }
0594 
0595 /**
0596   * @brief  Unregister an SMARTCARD callback
0597   *         SMARTCARD callback is redirected to the weak predefined callback
0598   * @note   The HAL_SMARTCARD_UnRegisterCallback() may be called before HAL_SMARTCARD_Init()
0599   *         in HAL_SMARTCARD_STATE_RESET to un-register callbacks for HAL_SMARTCARD_MSPINIT_CB_ID
0600   *         and HAL_SMARTCARD_MSPDEINIT_CB_ID
0601   * @param  hsmartcard smartcard handle
0602   * @param  CallbackID ID of the callback to be unregistered
0603   *         This parameter can be one of the following values:
0604   *           @arg @ref HAL_SMARTCARD_TX_COMPLETE_CB_ID Tx Complete Callback ID
0605   *           @arg @ref HAL_SMARTCARD_RX_COMPLETE_CB_ID Rx Complete Callback ID
0606   *           @arg @ref HAL_SMARTCARD_ERROR_CB_ID Error Callback ID
0607   *           @arg @ref HAL_SMARTCARD_ABORT_COMPLETE_CB_ID Abort Complete Callback ID
0608   *           @arg @ref HAL_SMARTCARD_ABORT_TRANSMIT_COMPLETE_CB_ID Abort Transmit Complete Callback ID
0609   *           @arg @ref HAL_SMARTCARD_ABORT_RECEIVE_COMPLETE_CB_ID Abort Receive Complete Callback ID
0610   *           @arg @ref HAL_SMARTCARD_RX_FIFO_FULL_CB_ID Rx Fifo Full Callback ID
0611   *           @arg @ref HAL_SMARTCARD_TX_FIFO_EMPTY_CB_ID Tx Fifo Empty Callback ID
0612   *           @arg @ref HAL_SMARTCARD_MSPINIT_CB_ID MspInit Callback ID
0613   *           @arg @ref HAL_SMARTCARD_MSPDEINIT_CB_ID MspDeInit Callback ID
0614   * @retval HAL status
0615   */
0616 HAL_StatusTypeDef HAL_SMARTCARD_UnRegisterCallback(SMARTCARD_HandleTypeDef *hsmartcard,
0617                                                    HAL_SMARTCARD_CallbackIDTypeDef CallbackID)
0618 {
0619   HAL_StatusTypeDef status = HAL_OK;
0620 
0621   if (HAL_SMARTCARD_STATE_READY == hsmartcard->gState)
0622   {
0623     switch (CallbackID)
0624     {
0625       case HAL_SMARTCARD_TX_COMPLETE_CB_ID :
0626         hsmartcard->TxCpltCallback = HAL_SMARTCARD_TxCpltCallback;                 /* Legacy weak TxCpltCallback */
0627         break;
0628 
0629       case HAL_SMARTCARD_RX_COMPLETE_CB_ID :
0630         hsmartcard->RxCpltCallback = HAL_SMARTCARD_RxCpltCallback;                 /* Legacy weak RxCpltCallback */
0631         break;
0632 
0633       case HAL_SMARTCARD_ERROR_CB_ID :
0634         hsmartcard->ErrorCallback = HAL_SMARTCARD_ErrorCallback;                   /* Legacy weak ErrorCallback  */
0635         break;
0636 
0637       case HAL_SMARTCARD_ABORT_COMPLETE_CB_ID :
0638         hsmartcard->AbortCpltCallback = HAL_SMARTCARD_AbortCpltCallback;           /* Legacy weak AbortCpltCallback */
0639         break;
0640 
0641       case HAL_SMARTCARD_ABORT_TRANSMIT_COMPLETE_CB_ID :
0642         hsmartcard->AbortTransmitCpltCallback = HAL_SMARTCARD_AbortTransmitCpltCallback; /* Legacy weak
0643                                                                                             AbortTransmitCpltCallback*/
0644         break;
0645 
0646       case HAL_SMARTCARD_ABORT_RECEIVE_COMPLETE_CB_ID :
0647         hsmartcard->AbortReceiveCpltCallback = HAL_SMARTCARD_AbortReceiveCpltCallback;  /* Legacy weak
0648                                                                                            AbortReceiveCpltCallback */
0649         break;
0650 
0651       case HAL_SMARTCARD_RX_FIFO_FULL_CB_ID :
0652         hsmartcard->RxFifoFullCallback = HAL_SMARTCARDEx_RxFifoFullCallback;      /* Legacy weak RxFifoFullCallback */
0653         break;
0654 
0655       case HAL_SMARTCARD_TX_FIFO_EMPTY_CB_ID :
0656         hsmartcard->TxFifoEmptyCallback = HAL_SMARTCARDEx_TxFifoEmptyCallback;    /* Legacy weak TxFifoEmptyCallback */
0657         break;
0658 
0659       case HAL_SMARTCARD_MSPINIT_CB_ID :
0660         hsmartcard->MspInitCallback = HAL_SMARTCARD_MspInit;                       /* Legacy weak MspInitCallback  */
0661         break;
0662 
0663       case HAL_SMARTCARD_MSPDEINIT_CB_ID :
0664         hsmartcard->MspDeInitCallback = HAL_SMARTCARD_MspDeInit;                   /* Legacy weak MspDeInitCallback */
0665         break;
0666 
0667       default :
0668         /* Update the error code */
0669         hsmartcard->ErrorCode |= HAL_SMARTCARD_ERROR_INVALID_CALLBACK;
0670 
0671         /* Return error status */
0672         status =  HAL_ERROR;
0673         break;
0674     }
0675   }
0676   else if (HAL_SMARTCARD_STATE_RESET == hsmartcard->gState)
0677   {
0678     switch (CallbackID)
0679     {
0680       case HAL_SMARTCARD_MSPINIT_CB_ID :
0681         hsmartcard->MspInitCallback = HAL_SMARTCARD_MspInit;
0682         break;
0683 
0684       case HAL_SMARTCARD_MSPDEINIT_CB_ID :
0685         hsmartcard->MspDeInitCallback = HAL_SMARTCARD_MspDeInit;
0686         break;
0687 
0688       default :
0689         /* Update the error code */
0690         hsmartcard->ErrorCode |= HAL_SMARTCARD_ERROR_INVALID_CALLBACK;
0691 
0692         /* Return error status */
0693         status =  HAL_ERROR;
0694         break;
0695     }
0696   }
0697   else
0698   {
0699     /* Update the error code */
0700     hsmartcard->ErrorCode |= HAL_SMARTCARD_ERROR_INVALID_CALLBACK;
0701 
0702     /* Return error status */
0703     status =  HAL_ERROR;
0704   }
0705 
0706   return status;
0707 }
0708 #endif /* USE_HAL_SMARTCARD_REGISTER_CALLBACKS */
0709 
0710 /**
0711   * @}
0712   */
0713 
0714 /** @defgroup SMARTCARD_Exported_Functions_Group2 IO operation functions
0715   * @ingroup RTEMSBSPsARMSTM32H7
0716   * @brief    SMARTCARD Transmit and Receive functions
0717   *
0718 @verbatim
0719   ==============================================================================
0720                          ##### IO operation functions #####
0721   ==============================================================================
0722   [..]
0723     This subsection provides a set of functions allowing to manage the SMARTCARD data transfers.
0724 
0725   [..]
0726     Smartcard is a single wire half duplex communication protocol.
0727     The Smartcard interface is designed to support asynchronous protocol Smartcards as
0728     defined in the ISO 7816-3 standard. The USART should be configured as:
0729     (+) 8 bits plus parity: where M=1 and PCE=1 in the USART_CR1 register
0730     (+) 1.5 stop bits when transmitting and receiving: where STOP=11 in the USART_CR2 register.
0731 
0732   [..]
0733     (#) There are two modes of transfer:
0734         (##) Blocking mode: The communication is performed in polling mode.
0735              The HAL status of all data processing is returned by the same function
0736              after finishing transfer.
0737         (##) Non-Blocking mode: The communication is performed using Interrupts
0738              or DMA, the relevant API's return the HAL status.
0739              The end of the data processing will be indicated through the
0740              dedicated SMARTCARD IRQ when using Interrupt mode or the DMA IRQ when
0741              using DMA mode.
0742         (##) The HAL_SMARTCARD_TxCpltCallback(), HAL_SMARTCARD_RxCpltCallback() user callbacks
0743              will be executed respectively at the end of the Transmit or Receive process
0744              The HAL_SMARTCARD_ErrorCallback() user callback will be executed when a communication
0745              error is detected.
0746 
0747     (#) Blocking mode APIs are :
0748         (##) HAL_SMARTCARD_Transmit()
0749         (##) HAL_SMARTCARD_Receive()
0750 
0751     (#) Non Blocking mode APIs with Interrupt are :
0752         (##) HAL_SMARTCARD_Transmit_IT()
0753         (##) HAL_SMARTCARD_Receive_IT()
0754         (##) HAL_SMARTCARD_IRQHandler()
0755 
0756     (#) Non Blocking mode functions with DMA are :
0757         (##) HAL_SMARTCARD_Transmit_DMA()
0758         (##) HAL_SMARTCARD_Receive_DMA()
0759 
0760     (#) A set of Transfer Complete Callbacks are provided in non Blocking mode:
0761         (##) HAL_SMARTCARD_TxCpltCallback()
0762         (##) HAL_SMARTCARD_RxCpltCallback()
0763         (##) HAL_SMARTCARD_ErrorCallback()
0764 
0765   [..]
0766     (#) Non-Blocking mode transfers could be aborted using Abort API's :
0767         (##) HAL_SMARTCARD_Abort()
0768         (##) HAL_SMARTCARD_AbortTransmit()
0769         (##) HAL_SMARTCARD_AbortReceive()
0770         (##) HAL_SMARTCARD_Abort_IT()
0771         (##) HAL_SMARTCARD_AbortTransmit_IT()
0772         (##) HAL_SMARTCARD_AbortReceive_IT()
0773 
0774     (#) For Abort services based on interrupts (HAL_SMARTCARD_Abortxxx_IT),
0775         a set of Abort Complete Callbacks are provided:
0776         (##) HAL_SMARTCARD_AbortCpltCallback()
0777         (##) HAL_SMARTCARD_AbortTransmitCpltCallback()
0778         (##) HAL_SMARTCARD_AbortReceiveCpltCallback()
0779 
0780     (#) In Non-Blocking mode transfers, possible errors are split into 2 categories.
0781         Errors are handled as follows :
0782        (##) Error is considered as Recoverable and non blocking : Transfer could go till end, but error severity is
0783            to be evaluated by user : this concerns Frame Error,
0784            Parity Error or Noise Error in Interrupt mode reception .
0785            Received character is then retrieved and stored in Rx buffer,
0786            Error code is set to allow user to identify error type,
0787            and HAL_SMARTCARD_ErrorCallback() user callback is executed. Transfer is kept ongoing on SMARTCARD side.
0788            If user wants to abort it, Abort services should be called by user.
0789        (##) Error is considered as Blocking : Transfer could not be completed properly and is aborted.
0790            This concerns Frame Error in Interrupt mode transmission, Overrun Error in Interrupt
0791            mode reception and all errors in DMA mode.
0792            Error code is set to allow user to identify error type,
0793            and HAL_SMARTCARD_ErrorCallback() user callback is executed.
0794 
0795 @endverbatim
0796   * @{
0797   */
0798 
0799 /**
0800   * @brief  Send an amount of data in blocking mode.
0801   * @note   When FIFO mode is enabled, writing a data in the TDR register adds one
0802   *         data to the TXFIFO. Write operations to the TDR register are performed
0803   *         when TXFNF flag is set. From hardware perspective, TXFNF flag and
0804   *         TXE are mapped on the same bit-field.
0805   * @param  hsmartcard Pointer to a SMARTCARD_HandleTypeDef structure that contains
0806   *                    the configuration information for the specified SMARTCARD module.
0807   * @param  pData pointer to data buffer.
0808   * @param  Size amount of data to be sent.
0809   * @param  Timeout  Timeout duration.
0810   * @retval HAL status
0811   */
0812 HAL_StatusTypeDef HAL_SMARTCARD_Transmit(SMARTCARD_HandleTypeDef *hsmartcard, const uint8_t *pData, uint16_t Size,
0813                                          uint32_t Timeout)
0814 {
0815   uint32_t tickstart;
0816   const uint8_t  *ptmpdata = pData;
0817 
0818   /* Check that a Tx process is not already ongoing */
0819   if (hsmartcard->gState == HAL_SMARTCARD_STATE_READY)
0820   {
0821     if ((ptmpdata == NULL) || (Size == 0U))
0822     {
0823       return  HAL_ERROR;
0824     }
0825 
0826     /* Process Locked */
0827     __HAL_LOCK(hsmartcard);
0828 
0829     hsmartcard->gState = HAL_SMARTCARD_STATE_BUSY_TX;
0830 
0831     /* Init tickstart for timeout management */
0832     tickstart = HAL_GetTick();
0833 
0834     /* Disable the Peripheral first to update mode for TX master */
0835     CLEAR_BIT(hsmartcard->Instance->CR1, USART_CR1_UE);
0836 
0837     /* In case of TX only mode, if NACK is enabled, the USART must be able to monitor
0838        the bidirectional line to detect a NACK signal in case of parity error.
0839        Therefore, the receiver block must be enabled as well (RE bit must be set). */
0840     if ((hsmartcard->Init.Mode == SMARTCARD_MODE_TX)
0841         && (hsmartcard->Init.NACKEnable == SMARTCARD_NACK_ENABLE))
0842     {
0843       SET_BIT(hsmartcard->Instance->CR1, USART_CR1_RE);
0844     }
0845     /* Enable Tx */
0846     SET_BIT(hsmartcard->Instance->CR1, USART_CR1_TE);
0847 
0848     /* Enable the Peripheral */
0849     SET_BIT(hsmartcard->Instance->CR1, USART_CR1_UE);
0850 
0851     /* Perform a TX/RX FIFO Flush */
0852     __HAL_SMARTCARD_FLUSH_DRREGISTER(hsmartcard);
0853 
0854     hsmartcard->ErrorCode = HAL_SMARTCARD_ERROR_NONE;
0855     hsmartcard->TxXferSize = Size;
0856     hsmartcard->TxXferCount = Size;
0857 
0858     while (hsmartcard->TxXferCount > 0U)
0859     {
0860       hsmartcard->TxXferCount--;
0861       if (SMARTCARD_WaitOnFlagUntilTimeout(hsmartcard, SMARTCARD_FLAG_TXE, RESET, tickstart, Timeout) != HAL_OK)
0862       {
0863         return HAL_TIMEOUT;
0864       }
0865       hsmartcard->Instance->TDR = (uint8_t)(*ptmpdata & 0xFFU);
0866       ptmpdata++;
0867     }
0868     if (SMARTCARD_WaitOnFlagUntilTimeout(hsmartcard, SMARTCARD_TRANSMISSION_COMPLETION_FLAG(hsmartcard), RESET,
0869                                          tickstart, Timeout) != HAL_OK)
0870     {
0871       return HAL_TIMEOUT;
0872     }
0873 
0874     /* Disable the Peripheral first to update mode */
0875     CLEAR_BIT(hsmartcard->Instance->CR1, USART_CR1_UE);
0876     if ((hsmartcard->Init.Mode == SMARTCARD_MODE_TX)
0877         && (hsmartcard->Init.NACKEnable == SMARTCARD_NACK_ENABLE))
0878     {
0879       /* In case of TX only mode, if NACK is enabled, receiver block has been enabled
0880          for Transmit phase. Disable this receiver block. */
0881       CLEAR_BIT(hsmartcard->Instance->CR1, USART_CR1_RE);
0882     }
0883     if ((hsmartcard->Init.Mode == SMARTCARD_MODE_TX_RX)
0884         || (hsmartcard->Init.NACKEnable == SMARTCARD_NACK_ENABLE))
0885     {
0886       /* Perform a TX FIFO Flush at end of Tx phase, as all sent bytes are appearing in Rx Data register */
0887       __HAL_SMARTCARD_FLUSH_DRREGISTER(hsmartcard);
0888     }
0889     SET_BIT(hsmartcard->Instance->CR1, USART_CR1_UE);
0890 
0891     /* At end of Tx process, restore hsmartcard->gState to Ready */
0892     hsmartcard->gState = HAL_SMARTCARD_STATE_READY;
0893 
0894     /* Process Unlocked */
0895     __HAL_UNLOCK(hsmartcard);
0896 
0897     return HAL_OK;
0898   }
0899   else
0900   {
0901     return HAL_BUSY;
0902   }
0903 }
0904 
0905 /**
0906   * @brief  Receive an amount of data in blocking mode.
0907   * @note   When FIFO mode is enabled, the RXFNE flag is set as long as the RXFIFO
0908   *         is not empty. Read operations from the RDR register are performed when
0909   *         RXFNE flag is set. From hardware perspective, RXFNE flag and
0910   *         RXNE are mapped on the same bit-field.
0911   * @param  hsmartcard Pointer to a SMARTCARD_HandleTypeDef structure that contains
0912   *                    the configuration information for the specified SMARTCARD module.
0913   * @param  pData pointer to data buffer.
0914   * @param  Size amount of data to be received.
0915   * @param  Timeout Timeout duration.
0916   * @retval HAL status
0917   */
0918 HAL_StatusTypeDef HAL_SMARTCARD_Receive(SMARTCARD_HandleTypeDef *hsmartcard, uint8_t *pData, uint16_t Size,
0919                                         uint32_t Timeout)
0920 {
0921   uint32_t tickstart;
0922   uint8_t  *ptmpdata = pData;
0923 
0924   /* Check that a Rx process is not already ongoing */
0925   if (hsmartcard->RxState == HAL_SMARTCARD_STATE_READY)
0926   {
0927     if ((ptmpdata == NULL) || (Size == 0U))
0928     {
0929       return  HAL_ERROR;
0930     }
0931 
0932     /* Process Locked */
0933     __HAL_LOCK(hsmartcard);
0934 
0935     hsmartcard->ErrorCode = HAL_SMARTCARD_ERROR_NONE;
0936     hsmartcard->RxState   = HAL_SMARTCARD_STATE_BUSY_RX;
0937 
0938     /* Init tickstart for timeout management */
0939     tickstart = HAL_GetTick();
0940 
0941     hsmartcard->RxXferSize = Size;
0942     hsmartcard->RxXferCount = Size;
0943 
0944     /* Check the remain data to be received */
0945     while (hsmartcard->RxXferCount > 0U)
0946     {
0947       hsmartcard->RxXferCount--;
0948 
0949       if (SMARTCARD_WaitOnFlagUntilTimeout(hsmartcard, SMARTCARD_FLAG_RXNE, RESET, tickstart, Timeout) != HAL_OK)
0950       {
0951         return HAL_TIMEOUT;
0952       }
0953       *ptmpdata = (uint8_t)(hsmartcard->Instance->RDR & (uint8_t)0x00FF);
0954       ptmpdata++;
0955     }
0956 
0957     /* At end of Rx process, restore hsmartcard->RxState to Ready */
0958     hsmartcard->RxState = HAL_SMARTCARD_STATE_READY;
0959 
0960     /* Process Unlocked */
0961     __HAL_UNLOCK(hsmartcard);
0962 
0963     return HAL_OK;
0964   }
0965   else
0966   {
0967     return HAL_BUSY;
0968   }
0969 }
0970 
0971 /**
0972   * @brief  Send an amount of data in interrupt mode.
0973   * @note   When FIFO mode is disabled, USART interrupt is generated whenever
0974   *         USART_TDR register is empty, i.e one interrupt per data to transmit.
0975   * @note   When FIFO mode is enabled, USART interrupt is generated whenever
0976   *         TXFIFO threshold reached. In that case the interrupt rate depends on
0977   *         TXFIFO threshold configuration.
0978   * @note   This function sets the hsmartcard->TxIsr function pointer according to
0979   *         the FIFO mode (data transmission processing depends on FIFO mode).
0980   * @param  hsmartcard Pointer to a SMARTCARD_HandleTypeDef structure that contains
0981   *                    the configuration information for the specified SMARTCARD module.
0982   * @param  pData pointer to data buffer.
0983   * @param  Size amount of data to be sent.
0984   * @retval HAL status
0985   */
0986 HAL_StatusTypeDef HAL_SMARTCARD_Transmit_IT(SMARTCARD_HandleTypeDef *hsmartcard, const uint8_t *pData, uint16_t Size)
0987 {
0988   /* Check that a Tx process is not already ongoing */
0989   if (hsmartcard->gState == HAL_SMARTCARD_STATE_READY)
0990   {
0991     if ((pData == NULL) || (Size == 0U))
0992     {
0993       return HAL_ERROR;
0994     }
0995 
0996     /* Process Locked */
0997     __HAL_LOCK(hsmartcard);
0998 
0999     hsmartcard->ErrorCode = HAL_SMARTCARD_ERROR_NONE;
1000     hsmartcard->gState = HAL_SMARTCARD_STATE_BUSY_TX;
1001 
1002     hsmartcard->pTxBuffPtr  = pData;
1003     hsmartcard->TxXferSize  = Size;
1004     hsmartcard->TxXferCount = Size;
1005     hsmartcard->TxISR       = NULL;
1006 
1007     /* Disable the Peripheral first to update mode for TX master */
1008     CLEAR_BIT(hsmartcard->Instance->CR1, USART_CR1_UE);
1009 
1010     /* In case of TX only mode, if NACK is enabled, the USART must be able to monitor
1011        the bidirectional line to detect a NACK signal in case of parity error.
1012        Therefore, the receiver block must be enabled as well (RE bit must be set). */
1013     if ((hsmartcard->Init.Mode == SMARTCARD_MODE_TX)
1014         && (hsmartcard->Init.NACKEnable == SMARTCARD_NACK_ENABLE))
1015     {
1016       SET_BIT(hsmartcard->Instance->CR1, USART_CR1_RE);
1017     }
1018     /* Enable Tx */
1019     SET_BIT(hsmartcard->Instance->CR1, USART_CR1_TE);
1020 
1021     /* Enable the Peripheral */
1022     SET_BIT(hsmartcard->Instance->CR1, USART_CR1_UE);
1023 
1024     /* Perform a TX/RX FIFO Flush */
1025     __HAL_SMARTCARD_FLUSH_DRREGISTER(hsmartcard);
1026 
1027     /* Configure Tx interrupt processing */
1028     if (hsmartcard->FifoMode == SMARTCARD_FIFOMODE_ENABLE)
1029     {
1030       /* Set the Tx ISR function pointer */
1031       hsmartcard->TxISR = SMARTCARD_TxISR_FIFOEN;
1032 
1033       /* Process Unlocked */
1034       __HAL_UNLOCK(hsmartcard);
1035 
1036       /* Enable the SMARTCARD Error Interrupt: (Frame error) */
1037       SET_BIT(hsmartcard->Instance->CR3, USART_CR3_EIE);
1038 
1039       /* Enable the TX FIFO threshold interrupt */
1040       SET_BIT(hsmartcard->Instance->CR3, USART_CR3_TXFTIE);
1041     }
1042     else
1043     {
1044       /* Set the Tx ISR function pointer */
1045       hsmartcard->TxISR = SMARTCARD_TxISR;
1046 
1047       /* Process Unlocked */
1048       __HAL_UNLOCK(hsmartcard);
1049 
1050       /* Enable the SMARTCARD Error Interrupt: (Frame error) */
1051       SET_BIT(hsmartcard->Instance->CR3, USART_CR3_EIE);
1052 
1053       /* Enable the SMARTCARD Transmit Data Register Empty Interrupt */
1054       SET_BIT(hsmartcard->Instance->CR1, USART_CR1_TXEIE_TXFNFIE);
1055     }
1056 
1057     return HAL_OK;
1058   }
1059   else
1060   {
1061     return HAL_BUSY;
1062   }
1063 }
1064 
1065 /**
1066   * @brief  Receive an amount of data in interrupt mode.
1067   * @note   When FIFO mode is disabled, USART interrupt is generated whenever
1068   *         USART_RDR register can be read, i.e one interrupt per data to receive.
1069   * @note   When FIFO mode is enabled, USART interrupt is generated whenever
1070   *         RXFIFO threshold reached. In that case the interrupt rate depends on
1071   *         RXFIFO threshold configuration.
1072   * @note   This function sets the hsmartcard->RxIsr function pointer according to
1073   *         the FIFO mode (data reception processing depends on FIFO mode).
1074   * @param  hsmartcard Pointer to a SMARTCARD_HandleTypeDef structure that contains
1075   *                    the configuration information for the specified SMARTCARD module.
1076   * @param  pData pointer to data buffer.
1077   * @param  Size amount of data to be received.
1078   * @retval HAL status
1079   */
1080 HAL_StatusTypeDef HAL_SMARTCARD_Receive_IT(SMARTCARD_HandleTypeDef *hsmartcard, uint8_t *pData, uint16_t Size)
1081 {
1082   /* Check that a Rx process is not already ongoing */
1083   if (hsmartcard->RxState == HAL_SMARTCARD_STATE_READY)
1084   {
1085     if ((pData == NULL) || (Size == 0U))
1086     {
1087       return HAL_ERROR;
1088     }
1089 
1090     /* Process Locked */
1091     __HAL_LOCK(hsmartcard);
1092 
1093     hsmartcard->ErrorCode = HAL_SMARTCARD_ERROR_NONE;
1094     hsmartcard->RxState   = HAL_SMARTCARD_STATE_BUSY_RX;
1095 
1096     hsmartcard->pRxBuffPtr = pData;
1097     hsmartcard->RxXferSize = Size;
1098     hsmartcard->RxXferCount = Size;
1099 
1100     /* Configure Rx interrupt processing */
1101     if ((hsmartcard->FifoMode == SMARTCARD_FIFOMODE_ENABLE) && (Size >= hsmartcard->NbRxDataToProcess))
1102     {
1103       /* Set the Rx ISR function pointer */
1104       hsmartcard->RxISR = SMARTCARD_RxISR_FIFOEN;
1105 
1106       /* Process Unlocked */
1107       __HAL_UNLOCK(hsmartcard);
1108 
1109       /* Enable the SMARTCART Parity Error interrupt and RX FIFO Threshold interrupt */
1110       SET_BIT(hsmartcard->Instance->CR1, USART_CR1_PEIE);
1111       SET_BIT(hsmartcard->Instance->CR3, USART_CR3_RXFTIE);
1112     }
1113     else
1114     {
1115       /* Set the Rx ISR function pointer */
1116       hsmartcard->RxISR = SMARTCARD_RxISR;
1117 
1118       /* Process Unlocked */
1119       __HAL_UNLOCK(hsmartcard);
1120 
1121       /* Enable the SMARTCARD Parity Error and Data Register not empty Interrupts */
1122       SET_BIT(hsmartcard->Instance->CR1, USART_CR1_PEIE | USART_CR1_RXNEIE_RXFNEIE);
1123     }
1124 
1125     /* Enable the SMARTCARD Error Interrupt: (Frame error, noise error, overrun error) */
1126     SET_BIT(hsmartcard->Instance->CR3, USART_CR3_EIE);
1127 
1128     return HAL_OK;
1129   }
1130   else
1131   {
1132     return HAL_BUSY;
1133   }
1134 }
1135 
1136 /**
1137   * @brief  Send an amount of data in DMA mode.
1138   * @param  hsmartcard Pointer to a SMARTCARD_HandleTypeDef structure that contains
1139   *                    the configuration information for the specified SMARTCARD module.
1140   * @param  pData pointer to data buffer.
1141   * @param  Size amount of data to be sent.
1142   * @retval HAL status
1143   */
1144 HAL_StatusTypeDef HAL_SMARTCARD_Transmit_DMA(SMARTCARD_HandleTypeDef *hsmartcard, const uint8_t *pData, uint16_t Size)
1145 {
1146   /* Check that a Tx process is not already ongoing */
1147   if (hsmartcard->gState == HAL_SMARTCARD_STATE_READY)
1148   {
1149     if ((pData == NULL) || (Size == 0U))
1150     {
1151       return HAL_ERROR;
1152     }
1153 
1154     /* Process Locked */
1155     __HAL_LOCK(hsmartcard);
1156 
1157     hsmartcard->gState = HAL_SMARTCARD_STATE_BUSY_TX;
1158 
1159     hsmartcard->ErrorCode = HAL_SMARTCARD_ERROR_NONE;
1160     hsmartcard->pTxBuffPtr = pData;
1161     hsmartcard->TxXferSize = Size;
1162     hsmartcard->TxXferCount = Size;
1163 
1164     /* Disable the Peripheral first to update mode for TX master */
1165     CLEAR_BIT(hsmartcard->Instance->CR1, USART_CR1_UE);
1166 
1167     /* In case of TX only mode, if NACK is enabled, the USART must be able to monitor
1168        the bidirectional line to detect a NACK signal in case of parity error.
1169        Therefore, the receiver block must be enabled as well (RE bit must be set). */
1170     if ((hsmartcard->Init.Mode == SMARTCARD_MODE_TX)
1171         && (hsmartcard->Init.NACKEnable == SMARTCARD_NACK_ENABLE))
1172     {
1173       SET_BIT(hsmartcard->Instance->CR1, USART_CR1_RE);
1174     }
1175     /* Enable Tx */
1176     SET_BIT(hsmartcard->Instance->CR1, USART_CR1_TE);
1177 
1178     /* Enable the Peripheral */
1179     SET_BIT(hsmartcard->Instance->CR1, USART_CR1_UE);
1180 
1181     /* Perform a TX/RX FIFO Flush */
1182     __HAL_SMARTCARD_FLUSH_DRREGISTER(hsmartcard);
1183 
1184     /* Set the SMARTCARD DMA transfer complete callback */
1185     hsmartcard->hdmatx->XferCpltCallback = SMARTCARD_DMATransmitCplt;
1186 
1187     /* Set the SMARTCARD error callback */
1188     hsmartcard->hdmatx->XferErrorCallback = SMARTCARD_DMAError;
1189 
1190     /* Set the DMA abort callback */
1191     hsmartcard->hdmatx->XferAbortCallback = NULL;
1192 
1193     /* Enable the SMARTCARD transmit DMA channel */
1194     if (HAL_DMA_Start_IT(hsmartcard->hdmatx, (uint32_t)hsmartcard->pTxBuffPtr, (uint32_t)&hsmartcard->Instance->TDR,
1195                          Size) == HAL_OK)
1196     {
1197       /* Clear the TC flag in the ICR register */
1198       CLEAR_BIT(hsmartcard->Instance->ICR, USART_ICR_TCCF);
1199 
1200       /* Process Unlocked */
1201       __HAL_UNLOCK(hsmartcard);
1202 
1203       /* Enable the UART Error Interrupt: (Frame error) */
1204       SET_BIT(hsmartcard->Instance->CR3, USART_CR3_EIE);
1205 
1206       /* Enable the DMA transfer for transmit request by setting the DMAT bit
1207          in the SMARTCARD associated USART CR3 register */
1208       SET_BIT(hsmartcard->Instance->CR3, USART_CR3_DMAT);
1209 
1210       return HAL_OK;
1211     }
1212     else
1213     {
1214       /* Set error code to DMA */
1215       hsmartcard->ErrorCode = HAL_SMARTCARD_ERROR_DMA;
1216 
1217       /* Process Unlocked */
1218       __HAL_UNLOCK(hsmartcard);
1219 
1220       /* Restore hsmartcard->State to ready */
1221       hsmartcard->gState = HAL_SMARTCARD_STATE_READY;
1222 
1223       return HAL_ERROR;
1224     }
1225   }
1226   else
1227   {
1228     return HAL_BUSY;
1229   }
1230 }
1231 
1232 /**
1233   * @brief  Receive an amount of data in DMA mode.
1234   * @param  hsmartcard Pointer to a SMARTCARD_HandleTypeDef structure that contains
1235   *                    the configuration information for the specified SMARTCARD module.
1236   * @param  pData pointer to data buffer.
1237   * @param  Size amount of data to be received.
1238   * @note   The SMARTCARD-associated USART parity is enabled (PCE = 1),
1239   *         the received data contain the parity bit (MSB position).
1240   * @retval HAL status
1241   */
1242 HAL_StatusTypeDef HAL_SMARTCARD_Receive_DMA(SMARTCARD_HandleTypeDef *hsmartcard, uint8_t *pData, uint16_t Size)
1243 {
1244   /* Check that a Rx process is not already ongoing */
1245   if (hsmartcard->RxState == HAL_SMARTCARD_STATE_READY)
1246   {
1247     if ((pData == NULL) || (Size == 0U))
1248     {
1249       return HAL_ERROR;
1250     }
1251 
1252     /* Process Locked */
1253     __HAL_LOCK(hsmartcard);
1254 
1255     hsmartcard->ErrorCode = HAL_SMARTCARD_ERROR_NONE;
1256     hsmartcard->RxState   = HAL_SMARTCARD_STATE_BUSY_RX;
1257 
1258     hsmartcard->pRxBuffPtr = pData;
1259     hsmartcard->RxXferSize = Size;
1260 
1261     /* Set the SMARTCARD DMA transfer complete callback */
1262     hsmartcard->hdmarx->XferCpltCallback = SMARTCARD_DMAReceiveCplt;
1263 
1264     /* Set the SMARTCARD DMA error callback */
1265     hsmartcard->hdmarx->XferErrorCallback = SMARTCARD_DMAError;
1266 
1267     /* Set the DMA abort callback */
1268     hsmartcard->hdmarx->XferAbortCallback = NULL;
1269 
1270     /* Enable the DMA channel */
1271     if (HAL_DMA_Start_IT(hsmartcard->hdmarx, (uint32_t)&hsmartcard->Instance->RDR, (uint32_t)hsmartcard->pRxBuffPtr,
1272                          Size) == HAL_OK)
1273     {
1274       /* Process Unlocked */
1275       __HAL_UNLOCK(hsmartcard);
1276 
1277       /* Enable the SMARTCARD Parity Error Interrupt */
1278       SET_BIT(hsmartcard->Instance->CR1, USART_CR1_PEIE);
1279 
1280       /* Enable the SMARTCARD Error Interrupt: (Frame error, noise error, overrun error) */
1281       SET_BIT(hsmartcard->Instance->CR3, USART_CR3_EIE);
1282 
1283       /* Enable the DMA transfer for the receiver request by setting the DMAR bit
1284          in the SMARTCARD associated USART CR3 register */
1285       SET_BIT(hsmartcard->Instance->CR3, USART_CR3_DMAR);
1286 
1287       return HAL_OK;
1288     }
1289     else
1290     {
1291       /* Set error code to DMA */
1292       hsmartcard->ErrorCode = HAL_SMARTCARD_ERROR_DMA;
1293 
1294       /* Process Unlocked */
1295       __HAL_UNLOCK(hsmartcard);
1296 
1297       /* Restore hsmartcard->State to ready */
1298       hsmartcard->RxState = HAL_SMARTCARD_STATE_READY;
1299 
1300       return HAL_ERROR;
1301     }
1302   }
1303   else
1304   {
1305     return HAL_BUSY;
1306   }
1307 }
1308 
1309 /**
1310   * @brief  Abort ongoing transfers (blocking mode).
1311   * @param  hsmartcard Pointer to a SMARTCARD_HandleTypeDef structure that contains
1312   *                    the configuration information for the specified SMARTCARD module.
1313   * @note   This procedure could be used for aborting any ongoing transfer started in Interrupt or DMA mode.
1314   *         This procedure performs following operations :
1315   *           - Disable SMARTCARD Interrupts (Tx and Rx)
1316   *           - Disable the DMA transfer in the peripheral register (if enabled)
1317   *           - Abort DMA transfer by calling HAL_DMA_Abort (in case of transfer in DMA mode)
1318   *           - Set handle State to READY
1319   * @note   This procedure is executed in blocking mode : when exiting function, Abort is considered as completed.
1320   * @retval HAL status
1321   */
1322 HAL_StatusTypeDef HAL_SMARTCARD_Abort(SMARTCARD_HandleTypeDef *hsmartcard)
1323 {
1324   /* Disable RTOIE, EOBIE, TXEIE, TCIE, RXNE, PE, RXFT, TXFT and
1325      ERR (Frame error, noise error, overrun error) interrupts */
1326   CLEAR_BIT(hsmartcard->Instance->CR1,
1327             (USART_CR1_RXNEIE_RXFNEIE | USART_CR1_PEIE | USART_CR1_TXEIE_TXFNFIE | USART_CR1_TCIE | USART_CR1_RTOIE |
1328              USART_CR1_EOBIE));
1329   CLEAR_BIT(hsmartcard->Instance->CR3, (USART_CR3_EIE | USART_CR3_RXFTIE | USART_CR3_TXFTIE));
1330 
1331   /* Disable the SMARTCARD DMA Tx request if enabled */
1332   if (HAL_IS_BIT_SET(hsmartcard->Instance->CR3, USART_CR3_DMAT))
1333   {
1334     CLEAR_BIT(hsmartcard->Instance->CR3, USART_CR3_DMAT);
1335 
1336     /* Abort the SMARTCARD DMA Tx channel : use blocking DMA Abort API (no callback) */
1337     if (hsmartcard->hdmatx != NULL)
1338     {
1339       /* Set the SMARTCARD DMA Abort callback to Null.
1340          No call back execution at end of DMA abort procedure */
1341       hsmartcard->hdmatx->XferAbortCallback = NULL;
1342 
1343       if (HAL_DMA_Abort(hsmartcard->hdmatx) != HAL_OK)
1344       {
1345         if (HAL_DMA_GetError(hsmartcard->hdmatx) == HAL_DMA_ERROR_TIMEOUT)
1346         {
1347           /* Set error code to DMA */
1348           hsmartcard->ErrorCode = HAL_SMARTCARD_ERROR_DMA;
1349 
1350           return HAL_TIMEOUT;
1351         }
1352       }
1353     }
1354   }
1355 
1356   /* Disable the SMARTCARD DMA Rx request if enabled */
1357   if (HAL_IS_BIT_SET(hsmartcard->Instance->CR3, USART_CR3_DMAR))
1358   {
1359     CLEAR_BIT(hsmartcard->Instance->CR3, USART_CR3_DMAR);
1360 
1361     /* Abort the SMARTCARD DMA Rx channel : use blocking DMA Abort API (no callback) */
1362     if (hsmartcard->hdmarx != NULL)
1363     {
1364       /* Set the SMARTCARD DMA Abort callback to Null.
1365          No call back execution at end of DMA abort procedure */
1366       hsmartcard->hdmarx->XferAbortCallback = NULL;
1367 
1368       if (HAL_DMA_Abort(hsmartcard->hdmarx) != HAL_OK)
1369       {
1370         if (HAL_DMA_GetError(hsmartcard->hdmarx) == HAL_DMA_ERROR_TIMEOUT)
1371         {
1372           /* Set error code to DMA */
1373           hsmartcard->ErrorCode = HAL_SMARTCARD_ERROR_DMA;
1374 
1375           return HAL_TIMEOUT;
1376         }
1377       }
1378     }
1379   }
1380 
1381   /* Reset Tx and Rx transfer counters */
1382   hsmartcard->TxXferCount = 0U;
1383   hsmartcard->RxXferCount = 0U;
1384 
1385   /* Clear the Error flags in the ICR register */
1386   __HAL_SMARTCARD_CLEAR_FLAG(hsmartcard,
1387                              SMARTCARD_CLEAR_OREF | SMARTCARD_CLEAR_NEF | SMARTCARD_CLEAR_PEF | SMARTCARD_CLEAR_FEF |
1388                              SMARTCARD_CLEAR_RTOF | SMARTCARD_CLEAR_EOBF);
1389 
1390   /* Restore hsmartcard->gState and hsmartcard->RxState to Ready */
1391   hsmartcard->gState  = HAL_SMARTCARD_STATE_READY;
1392   hsmartcard->RxState = HAL_SMARTCARD_STATE_READY;
1393 
1394   /* Reset Handle ErrorCode to No Error */
1395   hsmartcard->ErrorCode = HAL_SMARTCARD_ERROR_NONE;
1396 
1397   return HAL_OK;
1398 }
1399 
1400 /**
1401   * @brief  Abort ongoing Transmit transfer (blocking mode).
1402   * @param  hsmartcard Pointer to a SMARTCARD_HandleTypeDef structure that contains
1403   *                    the configuration information for the specified SMARTCARD module.
1404   * @note   This procedure could be used for aborting any ongoing Tx transfer started in Interrupt or DMA mode.
1405   *         This procedure performs following operations :
1406   *           - Disable SMARTCARD Interrupts (Tx)
1407   *           - Disable the DMA transfer in the peripheral register (if enabled)
1408   *           - Abort DMA transfer by calling HAL_DMA_Abort (in case of transfer in DMA mode)
1409   *           - Set handle State to READY
1410   * @note   This procedure is executed in blocking mode : when exiting function, Abort is considered as completed.
1411   * @retval HAL status
1412   */
1413 HAL_StatusTypeDef HAL_SMARTCARD_AbortTransmit(SMARTCARD_HandleTypeDef *hsmartcard)
1414 {
1415   /* Disable TCIE, TXEIE and TXFTIE interrupts */
1416   CLEAR_BIT(hsmartcard->Instance->CR1, (USART_CR1_TXEIE_TXFNFIE | USART_CR1_TCIE));
1417   CLEAR_BIT(hsmartcard->Instance->CR3, USART_CR3_TXFTIE);
1418 
1419   /* Check if a receive process is ongoing or not. If not disable ERR IT */
1420   if (hsmartcard->RxState == HAL_SMARTCARD_STATE_READY)
1421   {
1422     /* Disable the SMARTCARD Error Interrupt: (Frame error) */
1423     CLEAR_BIT(hsmartcard->Instance->CR3, USART_CR3_EIE);
1424   }
1425 
1426   /* Disable the SMARTCARD DMA Tx request if enabled */
1427   if (HAL_IS_BIT_SET(hsmartcard->Instance->CR3, USART_CR3_DMAT))
1428   {
1429     CLEAR_BIT(hsmartcard->Instance->CR3, USART_CR3_DMAT);
1430 
1431     /* Abort the SMARTCARD DMA Tx channel : use blocking DMA Abort API (no callback) */
1432     if (hsmartcard->hdmatx != NULL)
1433     {
1434       /* Set the SMARTCARD DMA Abort callback to Null.
1435          No call back execution at end of DMA abort procedure */
1436       hsmartcard->hdmatx->XferAbortCallback = NULL;
1437 
1438       if (HAL_DMA_Abort(hsmartcard->hdmatx) != HAL_OK)
1439       {
1440         if (HAL_DMA_GetError(hsmartcard->hdmatx) == HAL_DMA_ERROR_TIMEOUT)
1441         {
1442           /* Set error code to DMA */
1443           hsmartcard->ErrorCode = HAL_SMARTCARD_ERROR_DMA;
1444 
1445           return HAL_TIMEOUT;
1446         }
1447       }
1448     }
1449   }
1450 
1451   /* Reset Tx transfer counter */
1452   hsmartcard->TxXferCount = 0U;
1453 
1454   /* Clear the Error flags in the ICR register */
1455   __HAL_SMARTCARD_CLEAR_FLAG(hsmartcard, SMARTCARD_CLEAR_FEF);
1456 
1457   /* Restore hsmartcard->gState to Ready */
1458   hsmartcard->gState = HAL_SMARTCARD_STATE_READY;
1459 
1460   return HAL_OK;
1461 }
1462 
1463 /**
1464   * @brief  Abort ongoing Receive transfer (blocking mode).
1465   * @param  hsmartcard Pointer to a SMARTCARD_HandleTypeDef structure that contains
1466   *                    the configuration information for the specified SMARTCARD module.
1467   * @note   This procedure could be used for aborting any ongoing Rx transfer started in Interrupt or DMA mode.
1468   *         This procedure performs following operations :
1469   *           - Disable SMARTCARD Interrupts (Rx)
1470   *           - Disable the DMA transfer in the peripheral register (if enabled)
1471   *           - Abort DMA transfer by calling HAL_DMA_Abort (in case of transfer in DMA mode)
1472   *           - Set handle State to READY
1473   * @note   This procedure is executed in blocking mode : when exiting function, Abort is considered as completed.
1474   * @retval HAL status
1475   */
1476 HAL_StatusTypeDef HAL_SMARTCARD_AbortReceive(SMARTCARD_HandleTypeDef *hsmartcard)
1477 {
1478   /* Disable RTOIE, EOBIE, RXNE, PE, RXFT, TXFT and  ERR (Frame error, noise error, overrun error) interrupts */
1479   CLEAR_BIT(hsmartcard->Instance->CR1, (USART_CR1_RXNEIE_RXFNEIE | USART_CR1_PEIE | USART_CR1_RTOIE |
1480                                         USART_CR1_EOBIE));
1481   CLEAR_BIT(hsmartcard->Instance->CR3, (USART_CR3_EIE | USART_CR3_RXFTIE));
1482 
1483   /* Check if a Transmit process is ongoing or not. If not disable ERR IT */
1484   if (hsmartcard->gState == HAL_SMARTCARD_STATE_READY)
1485   {
1486     /* Disable the SMARTCARD Error Interrupt: (Frame error) */
1487     CLEAR_BIT(hsmartcard->Instance->CR3, USART_CR3_EIE);
1488   }
1489 
1490   /* Disable the SMARTCARD DMA Rx request if enabled */
1491   if (HAL_IS_BIT_SET(hsmartcard->Instance->CR3, USART_CR3_DMAR))
1492   {
1493     CLEAR_BIT(hsmartcard->Instance->CR3, USART_CR3_DMAR);
1494 
1495     /* Abort the SMARTCARD DMA Rx channel : use blocking DMA Abort API (no callback) */
1496     if (hsmartcard->hdmarx != NULL)
1497     {
1498       /* Set the SMARTCARD DMA Abort callback to Null.
1499          No call back execution at end of DMA abort procedure */
1500       hsmartcard->hdmarx->XferAbortCallback = NULL;
1501 
1502       if (HAL_DMA_Abort(hsmartcard->hdmarx) != HAL_OK)
1503       {
1504         if (HAL_DMA_GetError(hsmartcard->hdmarx) == HAL_DMA_ERROR_TIMEOUT)
1505         {
1506           /* Set error code to DMA */
1507           hsmartcard->ErrorCode = HAL_SMARTCARD_ERROR_DMA;
1508 
1509           return HAL_TIMEOUT;
1510         }
1511       }
1512     }
1513   }
1514 
1515   /* Reset Rx transfer counter */
1516   hsmartcard->RxXferCount = 0U;
1517 
1518   /* Clear the Error flags in the ICR register */
1519   __HAL_SMARTCARD_CLEAR_FLAG(hsmartcard,
1520                              SMARTCARD_CLEAR_OREF | SMARTCARD_CLEAR_NEF | SMARTCARD_CLEAR_PEF | SMARTCARD_CLEAR_FEF |
1521                              SMARTCARD_CLEAR_RTOF | SMARTCARD_CLEAR_EOBF);
1522 
1523   /* Restore hsmartcard->RxState to Ready */
1524   hsmartcard->RxState = HAL_SMARTCARD_STATE_READY;
1525 
1526   return HAL_OK;
1527 }
1528 
1529 /**
1530   * @brief  Abort ongoing transfers (Interrupt mode).
1531   * @param  hsmartcard Pointer to a SMARTCARD_HandleTypeDef structure that contains
1532   *                    the configuration information for the specified SMARTCARD module.
1533   * @note   This procedure could be used for aborting any ongoing transfer started in Interrupt or DMA mode.
1534   *         This procedure performs following operations :
1535   *           - Disable SMARTCARD Interrupts (Tx and Rx)
1536   *           - Disable the DMA transfer in the peripheral register (if enabled)
1537   *           - Abort DMA transfer by calling HAL_DMA_Abort_IT (in case of transfer in DMA mode)
1538   *           - Set handle State to READY
1539   *           - At abort completion, call user abort complete callback
1540   * @note   This procedure is executed in Interrupt mode, meaning that abort procedure could be
1541   *         considered as completed only when user abort complete callback is executed (not when exiting function).
1542   * @retval HAL status
1543   */
1544 HAL_StatusTypeDef HAL_SMARTCARD_Abort_IT(SMARTCARD_HandleTypeDef *hsmartcard)
1545 {
1546   uint32_t abortcplt = 1U;
1547 
1548   /* Disable RTOIE, EOBIE, TXEIE, TCIE, RXNE, PE, RXFT, TXFT and
1549      ERR (Frame error, noise error, overrun error) interrupts */
1550   CLEAR_BIT(hsmartcard->Instance->CR1,
1551             (USART_CR1_RXNEIE_RXFNEIE | USART_CR1_PEIE | USART_CR1_TXEIE_TXFNFIE | USART_CR1_TCIE | USART_CR1_RTOIE |
1552              USART_CR1_EOBIE));
1553   CLEAR_BIT(hsmartcard->Instance->CR3, (USART_CR3_EIE | USART_CR3_RXFTIE | USART_CR3_TXFTIE));
1554 
1555   /* If DMA Tx and/or DMA Rx Handles are associated to SMARTCARD Handle,
1556      DMA Abort complete callbacks should be initialised before any call
1557      to DMA Abort functions */
1558   /* DMA Tx Handle is valid */
1559   if (hsmartcard->hdmatx != NULL)
1560   {
1561     /* Set DMA Abort Complete callback if SMARTCARD DMA Tx request if enabled.
1562        Otherwise, set it to NULL */
1563     if (HAL_IS_BIT_SET(hsmartcard->Instance->CR3, USART_CR3_DMAT))
1564     {
1565       hsmartcard->hdmatx->XferAbortCallback = SMARTCARD_DMATxAbortCallback;
1566     }
1567     else
1568     {
1569       hsmartcard->hdmatx->XferAbortCallback = NULL;
1570     }
1571   }
1572   /* DMA Rx Handle is valid */
1573   if (hsmartcard->hdmarx != NULL)
1574   {
1575     /* Set DMA Abort Complete callback if SMARTCARD DMA Rx request if enabled.
1576        Otherwise, set it to NULL */
1577     if (HAL_IS_BIT_SET(hsmartcard->Instance->CR3, USART_CR3_DMAR))
1578     {
1579       hsmartcard->hdmarx->XferAbortCallback = SMARTCARD_DMARxAbortCallback;
1580     }
1581     else
1582     {
1583       hsmartcard->hdmarx->XferAbortCallback = NULL;
1584     }
1585   }
1586 
1587   /* Disable the SMARTCARD DMA Tx request if enabled */
1588   if (HAL_IS_BIT_SET(hsmartcard->Instance->CR3, USART_CR3_DMAT))
1589   {
1590     /* Disable DMA Tx at UART level */
1591     CLEAR_BIT(hsmartcard->Instance->CR3, USART_CR3_DMAT);
1592 
1593     /* Abort the SMARTCARD DMA Tx channel : use non blocking DMA Abort API (callback) */
1594     if (hsmartcard->hdmatx != NULL)
1595     {
1596       /* SMARTCARD Tx DMA Abort callback has already been initialised :
1597          will lead to call HAL_SMARTCARD_AbortCpltCallback() at end of DMA abort procedure */
1598 
1599       /* Abort DMA TX */
1600       if (HAL_DMA_Abort_IT(hsmartcard->hdmatx) != HAL_OK)
1601       {
1602         hsmartcard->hdmatx->XferAbortCallback = NULL;
1603       }
1604       else
1605       {
1606         abortcplt = 0U;
1607       }
1608     }
1609   }
1610 
1611   /* Disable the SMARTCARD DMA Rx request if enabled */
1612   if (HAL_IS_BIT_SET(hsmartcard->Instance->CR3, USART_CR3_DMAR))
1613   {
1614     CLEAR_BIT(hsmartcard->Instance->CR3, USART_CR3_DMAR);
1615 
1616     /* Abort the SMARTCARD DMA Rx channel : use non blocking DMA Abort API (callback) */
1617     if (hsmartcard->hdmarx != NULL)
1618     {
1619       /* SMARTCARD Rx DMA Abort callback has already been initialised :
1620          will lead to call HAL_SMARTCARD_AbortCpltCallback() at end of DMA abort procedure */
1621 
1622       /* Abort DMA RX */
1623       if (HAL_DMA_Abort_IT(hsmartcard->hdmarx) != HAL_OK)
1624       {
1625         hsmartcard->hdmarx->XferAbortCallback = NULL;
1626         abortcplt = 1U;
1627       }
1628       else
1629       {
1630         abortcplt = 0U;
1631       }
1632     }
1633   }
1634 
1635   /* if no DMA abort complete callback execution is required => call user Abort Complete callback */
1636   if (abortcplt == 1U)
1637   {
1638     /* Reset Tx and Rx transfer counters */
1639     hsmartcard->TxXferCount = 0U;
1640     hsmartcard->RxXferCount = 0U;
1641 
1642     /* Clear ISR function pointers */
1643     hsmartcard->RxISR = NULL;
1644     hsmartcard->TxISR = NULL;
1645 
1646     /* Reset errorCode */
1647     hsmartcard->ErrorCode = HAL_SMARTCARD_ERROR_NONE;
1648 
1649     /* Clear the Error flags in the ICR register */
1650     __HAL_SMARTCARD_CLEAR_FLAG(hsmartcard,
1651                                SMARTCARD_CLEAR_OREF | SMARTCARD_CLEAR_NEF | SMARTCARD_CLEAR_PEF |
1652                                SMARTCARD_CLEAR_FEF | SMARTCARD_CLEAR_RTOF | SMARTCARD_CLEAR_EOBF);
1653 
1654     /* Restore hsmartcard->gState and hsmartcard->RxState to Ready */
1655     hsmartcard->gState  = HAL_SMARTCARD_STATE_READY;
1656     hsmartcard->RxState = HAL_SMARTCARD_STATE_READY;
1657 
1658     /* As no DMA to be aborted, call directly user Abort complete callback */
1659 #if (USE_HAL_SMARTCARD_REGISTER_CALLBACKS == 1)
1660     /* Call registered Abort complete callback */
1661     hsmartcard->AbortCpltCallback(hsmartcard);
1662 #else
1663     /* Call legacy weak Abort complete callback */
1664     HAL_SMARTCARD_AbortCpltCallback(hsmartcard);
1665 #endif /* USE_HAL_SMARTCARD_REGISTER_CALLBACK */
1666   }
1667 
1668   return HAL_OK;
1669 }
1670 
1671 /**
1672   * @brief  Abort ongoing Transmit transfer (Interrupt mode).
1673   * @param  hsmartcard Pointer to a SMARTCARD_HandleTypeDef structure that contains
1674   *                    the configuration information for the specified SMARTCARD module.
1675   * @note   This procedure could be used for aborting any ongoing Tx transfer started in Interrupt or DMA mode.
1676   *         This procedure performs following operations :
1677   *           - Disable SMARTCARD Interrupts (Tx)
1678   *           - Disable the DMA transfer in the peripheral register (if enabled)
1679   *           - Abort DMA transfer by calling HAL_DMA_Abort_IT (in case of transfer in DMA mode)
1680   *           - Set handle State to READY
1681   *           - At abort completion, call user abort complete callback
1682   * @note   This procedure is executed in Interrupt mode, meaning that abort procedure could be
1683   *         considered as completed only when user abort complete callback is executed (not when exiting function).
1684   * @retval HAL status
1685   */
1686 HAL_StatusTypeDef HAL_SMARTCARD_AbortTransmit_IT(SMARTCARD_HandleTypeDef *hsmartcard)
1687 {
1688   /* Disable TCIE, TXEIE and TXFTIE interrupts */
1689   CLEAR_BIT(hsmartcard->Instance->CR1, (USART_CR1_TXEIE_TXFNFIE | USART_CR1_TCIE));
1690   CLEAR_BIT(hsmartcard->Instance->CR3, USART_CR3_TXFTIE);
1691 
1692   /* Check if a receive process is ongoing or not. If not disable ERR IT */
1693   if (hsmartcard->RxState == HAL_SMARTCARD_STATE_READY)
1694   {
1695     /* Disable the SMARTCARD Error Interrupt: (Frame error) */
1696     CLEAR_BIT(hsmartcard->Instance->CR3, USART_CR3_EIE);
1697   }
1698 
1699   /* Disable the SMARTCARD DMA Tx request if enabled */
1700   if (HAL_IS_BIT_SET(hsmartcard->Instance->CR3, USART_CR3_DMAT))
1701   {
1702     CLEAR_BIT(hsmartcard->Instance->CR3, USART_CR3_DMAT);
1703 
1704     /* Abort the SMARTCARD DMA Tx channel : use non blocking DMA Abort API (callback) */
1705     if (hsmartcard->hdmatx != NULL)
1706     {
1707       /* Set the SMARTCARD DMA Abort callback :
1708          will lead to call HAL_SMARTCARD_AbortCpltCallback() at end of DMA abort procedure */
1709       hsmartcard->hdmatx->XferAbortCallback = SMARTCARD_DMATxOnlyAbortCallback;
1710 
1711       /* Abort DMA TX */
1712       if (HAL_DMA_Abort_IT(hsmartcard->hdmatx) != HAL_OK)
1713       {
1714         /* Call Directly hsmartcard->hdmatx->XferAbortCallback function in case of error */
1715         hsmartcard->hdmatx->XferAbortCallback(hsmartcard->hdmatx);
1716       }
1717     }
1718     else
1719     {
1720       /* Reset Tx transfer counter */
1721       hsmartcard->TxXferCount = 0U;
1722 
1723       /* Clear TxISR function pointers */
1724       hsmartcard->TxISR = NULL;
1725 
1726       /* Restore hsmartcard->gState to Ready */
1727       hsmartcard->gState = HAL_SMARTCARD_STATE_READY;
1728 
1729       /* As no DMA to be aborted, call directly user Abort complete callback */
1730 #if (USE_HAL_SMARTCARD_REGISTER_CALLBACKS == 1)
1731       /* Call registered Abort Transmit Complete Callback */
1732       hsmartcard->AbortTransmitCpltCallback(hsmartcard);
1733 #else
1734       /* Call legacy weak Abort Transmit Complete Callback */
1735       HAL_SMARTCARD_AbortTransmitCpltCallback(hsmartcard);
1736 #endif /* USE_HAL_SMARTCARD_REGISTER_CALLBACK */
1737     }
1738   }
1739   else
1740   {
1741     /* Reset Tx transfer counter */
1742     hsmartcard->TxXferCount = 0U;
1743 
1744     /* Clear TxISR function pointers */
1745     hsmartcard->TxISR = NULL;
1746 
1747     /* Clear the Error flags in the ICR register */
1748     __HAL_SMARTCARD_CLEAR_FLAG(hsmartcard, SMARTCARD_CLEAR_FEF);
1749 
1750     /* Restore hsmartcard->gState to Ready */
1751     hsmartcard->gState = HAL_SMARTCARD_STATE_READY;
1752 
1753     /* As no DMA to be aborted, call directly user Abort complete callback */
1754 #if (USE_HAL_SMARTCARD_REGISTER_CALLBACKS == 1)
1755     /* Call registered Abort Transmit Complete Callback */
1756     hsmartcard->AbortTransmitCpltCallback(hsmartcard);
1757 #else
1758     /* Call legacy weak Abort Transmit Complete Callback */
1759     HAL_SMARTCARD_AbortTransmitCpltCallback(hsmartcard);
1760 #endif /* USE_HAL_SMARTCARD_REGISTER_CALLBACK */
1761   }
1762 
1763   return HAL_OK;
1764 }
1765 
1766 /**
1767   * @brief  Abort ongoing Receive transfer (Interrupt mode).
1768   * @param  hsmartcard Pointer to a SMARTCARD_HandleTypeDef structure that contains
1769   *                    the configuration information for the specified SMARTCARD module.
1770   * @note   This procedure could be used for aborting any ongoing Rx transfer started in Interrupt or DMA mode.
1771   *         This procedure performs following operations :
1772   *           - Disable SMARTCARD Interrupts (Rx)
1773   *           - Disable the DMA transfer in the peripheral register (if enabled)
1774   *           - Abort DMA transfer by calling HAL_DMA_Abort_IT (in case of transfer in DMA mode)
1775   *           - Set handle State to READY
1776   *           - At abort completion, call user abort complete callback
1777   * @note   This procedure is executed in Interrupt mode, meaning that abort procedure could be
1778   *         considered as completed only when user abort complete callback is executed (not when exiting function).
1779   * @retval HAL status
1780   */
1781 HAL_StatusTypeDef HAL_SMARTCARD_AbortReceive_IT(SMARTCARD_HandleTypeDef *hsmartcard)
1782 {
1783   /* Disable RTOIE, EOBIE, RXNE, PE, RXFT and  ERR (Frame error, noise error, overrun error) interrupts */
1784   CLEAR_BIT(hsmartcard->Instance->CR1, (USART_CR1_RXNEIE_RXFNEIE | USART_CR1_PEIE | USART_CR1_RTOIE |
1785                                         USART_CR1_EOBIE));
1786   CLEAR_BIT(hsmartcard->Instance->CR3, (USART_CR3_EIE | USART_CR3_RXFTIE));
1787 
1788   /* Check if a Transmit process is ongoing or not. If not disable ERR IT */
1789   if (hsmartcard->gState == HAL_SMARTCARD_STATE_READY)
1790   {
1791     /* Disable the SMARTCARD Error Interrupt: (Frame error) */
1792     CLEAR_BIT(hsmartcard->Instance->CR3, USART_CR3_EIE);
1793   }
1794 
1795   /* Disable the SMARTCARD DMA Rx request if enabled */
1796   if (HAL_IS_BIT_SET(hsmartcard->Instance->CR3, USART_CR3_DMAR))
1797   {
1798     CLEAR_BIT(hsmartcard->Instance->CR3, USART_CR3_DMAR);
1799 
1800     /* Abort the SMARTCARD DMA Rx channel : use non blocking DMA Abort API (callback) */
1801     if (hsmartcard->hdmarx != NULL)
1802     {
1803       /* Set the SMARTCARD DMA Abort callback :
1804          will lead to call HAL_SMARTCARD_AbortCpltCallback() at end of DMA abort procedure */
1805       hsmartcard->hdmarx->XferAbortCallback = SMARTCARD_DMARxOnlyAbortCallback;
1806 
1807       /* Abort DMA RX */
1808       if (HAL_DMA_Abort_IT(hsmartcard->hdmarx) != HAL_OK)
1809       {
1810         /* Call Directly hsmartcard->hdmarx->XferAbortCallback function in case of error */
1811         hsmartcard->hdmarx->XferAbortCallback(hsmartcard->hdmarx);
1812       }
1813     }
1814     else
1815     {
1816       /* Reset Rx transfer counter */
1817       hsmartcard->RxXferCount = 0U;
1818 
1819       /* Clear RxISR function pointer */
1820       hsmartcard->RxISR = NULL;
1821 
1822       /* Clear the Error flags in the ICR register */
1823       __HAL_SMARTCARD_CLEAR_FLAG(hsmartcard,
1824                                  SMARTCARD_CLEAR_OREF | SMARTCARD_CLEAR_NEF | SMARTCARD_CLEAR_PEF |
1825                                  SMARTCARD_CLEAR_FEF | SMARTCARD_CLEAR_RTOF | SMARTCARD_CLEAR_EOBF);
1826 
1827       /* Restore hsmartcard->RxState to Ready */
1828       hsmartcard->RxState = HAL_SMARTCARD_STATE_READY;
1829 
1830       /* As no DMA to be aborted, call directly user Abort complete callback */
1831 #if (USE_HAL_SMARTCARD_REGISTER_CALLBACKS == 1)
1832       /* Call registered Abort Receive Complete Callback */
1833       hsmartcard->AbortReceiveCpltCallback(hsmartcard);
1834 #else
1835       /* Call legacy weak Abort Receive Complete Callback */
1836       HAL_SMARTCARD_AbortReceiveCpltCallback(hsmartcard);
1837 #endif /* USE_HAL_SMARTCARD_REGISTER_CALLBACK */
1838     }
1839   }
1840   else
1841   {
1842     /* Reset Rx transfer counter */
1843     hsmartcard->RxXferCount = 0U;
1844 
1845     /* Clear RxISR function pointer */
1846     hsmartcard->RxISR = NULL;
1847 
1848     /* Clear the Error flags in the ICR register */
1849     __HAL_SMARTCARD_CLEAR_FLAG(hsmartcard,
1850                                SMARTCARD_CLEAR_OREF | SMARTCARD_CLEAR_NEF | SMARTCARD_CLEAR_PEF |
1851                                SMARTCARD_CLEAR_FEF | SMARTCARD_CLEAR_RTOF | SMARTCARD_CLEAR_EOBF);
1852 
1853     /* Restore hsmartcard->RxState to Ready */
1854     hsmartcard->RxState = HAL_SMARTCARD_STATE_READY;
1855 
1856     /* As no DMA to be aborted, call directly user Abort complete callback */
1857 #if (USE_HAL_SMARTCARD_REGISTER_CALLBACKS == 1)
1858     /* Call registered Abort Receive Complete Callback */
1859     hsmartcard->AbortReceiveCpltCallback(hsmartcard);
1860 #else
1861     /* Call legacy weak Abort Receive Complete Callback */
1862     HAL_SMARTCARD_AbortReceiveCpltCallback(hsmartcard);
1863 #endif /* USE_HAL_SMARTCARD_REGISTER_CALLBACK */
1864   }
1865 
1866   return HAL_OK;
1867 }
1868 
1869 /**
1870   * @brief  Handle SMARTCARD interrupt requests.
1871   * @param  hsmartcard Pointer to a SMARTCARD_HandleTypeDef structure that contains
1872   *                    the configuration information for the specified SMARTCARD module.
1873   * @retval None
1874   */
1875 void HAL_SMARTCARD_IRQHandler(SMARTCARD_HandleTypeDef *hsmartcard)
1876 {
1877   uint32_t isrflags   = READ_REG(hsmartcard->Instance->ISR);
1878   uint32_t cr1its     = READ_REG(hsmartcard->Instance->CR1);
1879   uint32_t cr3its     = READ_REG(hsmartcard->Instance->CR3);
1880   uint32_t errorflags;
1881   uint32_t errorcode;
1882 
1883   /* If no error occurs */
1884   errorflags = (isrflags & (uint32_t)(USART_ISR_PE | USART_ISR_FE | USART_ISR_ORE | USART_ISR_NE | USART_ISR_RTOF));
1885   if (errorflags == 0U)
1886   {
1887     /* SMARTCARD in mode Receiver ---------------------------------------------------*/
1888     if (((isrflags & USART_ISR_RXNE_RXFNE) != 0U)
1889         && (((cr1its & USART_CR1_RXNEIE_RXFNEIE) != 0U)
1890             || ((cr3its & USART_CR3_RXFTIE) != 0U)))
1891     {
1892       if (hsmartcard->RxISR != NULL)
1893       {
1894         hsmartcard->RxISR(hsmartcard);
1895       }
1896       return;
1897     }
1898   }
1899 
1900   /* If some errors occur */
1901   if ((errorflags != 0U)
1902       && ((((cr3its & (USART_CR3_RXFTIE | USART_CR3_EIE)) != 0U)
1903            || ((cr1its & (USART_CR1_RXNEIE_RXFNEIE | USART_CR1_PEIE)) != 0U))))
1904   {
1905     /* SMARTCARD parity error interrupt occurred -------------------------------------*/
1906     if (((isrflags & USART_ISR_PE) != 0U) && ((cr1its & USART_CR1_PEIE) != 0U))
1907     {
1908       __HAL_SMARTCARD_CLEAR_IT(hsmartcard, SMARTCARD_CLEAR_PEF);
1909 
1910       hsmartcard->ErrorCode |= HAL_SMARTCARD_ERROR_PE;
1911     }
1912 
1913     /* SMARTCARD frame error interrupt occurred --------------------------------------*/
1914     if (((isrflags & USART_ISR_FE) != 0U) && ((cr3its & USART_CR3_EIE) != 0U))
1915     {
1916       __HAL_SMARTCARD_CLEAR_IT(hsmartcard, SMARTCARD_CLEAR_FEF);
1917 
1918       hsmartcard->ErrorCode |= HAL_SMARTCARD_ERROR_FE;
1919     }
1920 
1921     /* SMARTCARD noise error interrupt occurred --------------------------------------*/
1922     if (((isrflags & USART_ISR_NE) != 0U) && ((cr3its & USART_CR3_EIE) != 0U))
1923     {
1924       __HAL_SMARTCARD_CLEAR_IT(hsmartcard, SMARTCARD_CLEAR_NEF);
1925 
1926       hsmartcard->ErrorCode |= HAL_SMARTCARD_ERROR_NE;
1927     }
1928 
1929     /* SMARTCARD Over-Run interrupt occurred -----------------------------------------*/
1930     if (((isrflags & USART_ISR_ORE) != 0U)
1931         && (((cr1its & USART_CR1_RXNEIE_RXFNEIE) != 0U)
1932             || ((cr3its & USART_CR3_RXFTIE) != 0U)
1933             || ((cr3its & USART_CR3_EIE) != 0U)))
1934     {
1935       __HAL_SMARTCARD_CLEAR_IT(hsmartcard, SMARTCARD_CLEAR_OREF);
1936 
1937       hsmartcard->ErrorCode |= HAL_SMARTCARD_ERROR_ORE;
1938     }
1939 
1940     /* SMARTCARD receiver timeout interrupt occurred -----------------------------------------*/
1941     if (((isrflags & USART_ISR_RTOF) != 0U) && ((cr1its & USART_CR1_RTOIE) != 0U))
1942     {
1943       __HAL_SMARTCARD_CLEAR_IT(hsmartcard, SMARTCARD_CLEAR_RTOF);
1944 
1945       hsmartcard->ErrorCode |= HAL_SMARTCARD_ERROR_RTO;
1946     }
1947 
1948     /* Call SMARTCARD Error Call back function if need be --------------------------*/
1949     if (hsmartcard->ErrorCode != HAL_SMARTCARD_ERROR_NONE)
1950     {
1951       /* SMARTCARD in mode Receiver ---------------------------------------------------*/
1952       if (((isrflags & USART_ISR_RXNE_RXFNE) != 0U)
1953           && (((cr1its & USART_CR1_RXNEIE_RXFNEIE) != 0U)
1954               || ((cr3its & USART_CR3_RXFTIE) != 0U)))
1955       {
1956         if (hsmartcard->RxISR != NULL)
1957         {
1958           hsmartcard->RxISR(hsmartcard);
1959         }
1960       }
1961 
1962       /* If Error is to be considered as blocking :
1963           - Receiver Timeout error in Reception
1964           - Overrun error in Reception
1965           - any error occurs in DMA mode reception
1966       */
1967       errorcode = hsmartcard->ErrorCode;
1968       if ((HAL_IS_BIT_SET(hsmartcard->Instance->CR3, USART_CR3_DMAR))
1969           || ((errorcode & (HAL_SMARTCARD_ERROR_RTO | HAL_SMARTCARD_ERROR_ORE)) != 0U))
1970       {
1971         /* Blocking error : transfer is aborted
1972            Set the SMARTCARD state ready to be able to start again the process,
1973            Disable Rx Interrupts, and disable Rx DMA request, if ongoing */
1974         SMARTCARD_EndRxTransfer(hsmartcard);
1975 
1976         /* Disable the SMARTCARD DMA Rx request if enabled */
1977         if (HAL_IS_BIT_SET(hsmartcard->Instance->CR3, USART_CR3_DMAR))
1978         {
1979           CLEAR_BIT(hsmartcard->Instance->CR3, USART_CR3_DMAR);
1980 
1981           /* Abort the SMARTCARD DMA Rx channel */
1982           if (hsmartcard->hdmarx != NULL)
1983           {
1984             /* Set the SMARTCARD DMA Abort callback :
1985                will lead to call HAL_SMARTCARD_ErrorCallback() at end of DMA abort procedure */
1986             hsmartcard->hdmarx->XferAbortCallback = SMARTCARD_DMAAbortOnError;
1987 
1988             /* Abort DMA RX */
1989             if (HAL_DMA_Abort_IT(hsmartcard->hdmarx) != HAL_OK)
1990             {
1991               /* Call Directly hsmartcard->hdmarx->XferAbortCallback function in case of error */
1992               hsmartcard->hdmarx->XferAbortCallback(hsmartcard->hdmarx);
1993             }
1994           }
1995           else
1996           {
1997 #if (USE_HAL_SMARTCARD_REGISTER_CALLBACKS == 1)
1998             /* Call registered user error callback */
1999             hsmartcard->ErrorCallback(hsmartcard);
2000 #else
2001             /* Call legacy weak user error callback */
2002             HAL_SMARTCARD_ErrorCallback(hsmartcard);
2003 #endif /* USE_HAL_SMARTCARD_REGISTER_CALLBACK */
2004           }
2005         }
2006         else
2007         {
2008 #if (USE_HAL_SMARTCARD_REGISTER_CALLBACKS == 1)
2009           /* Call registered user error callback */
2010           hsmartcard->ErrorCallback(hsmartcard);
2011 #else
2012           /* Call legacy weak user error callback */
2013           HAL_SMARTCARD_ErrorCallback(hsmartcard);
2014 #endif /* USE_HAL_SMARTCARD_REGISTER_CALLBACK */
2015         }
2016       }
2017       /* other error type to be considered as blocking :
2018           - Frame error in Transmission
2019       */
2020       else if ((hsmartcard->gState == HAL_SMARTCARD_STATE_BUSY_TX)
2021                && ((errorcode & HAL_SMARTCARD_ERROR_FE) != 0U))
2022       {
2023         /* Blocking error : transfer is aborted
2024            Set the SMARTCARD state ready to be able to start again the process,
2025            Disable Tx Interrupts, and disable Tx DMA request, if ongoing */
2026         SMARTCARD_EndTxTransfer(hsmartcard);
2027 
2028         /* Disable the SMARTCARD DMA Tx request if enabled */
2029         if (HAL_IS_BIT_SET(hsmartcard->Instance->CR3, USART_CR3_DMAT))
2030         {
2031           CLEAR_BIT(hsmartcard->Instance->CR3, USART_CR3_DMAT);
2032 
2033           /* Abort the SMARTCARD DMA Tx channel */
2034           if (hsmartcard->hdmatx != NULL)
2035           {
2036             /* Set the SMARTCARD DMA Abort callback :
2037                will lead to call HAL_SMARTCARD_ErrorCallback() at end of DMA abort procedure */
2038             hsmartcard->hdmatx->XferAbortCallback = SMARTCARD_DMAAbortOnError;
2039 
2040             /* Abort DMA TX */
2041             if (HAL_DMA_Abort_IT(hsmartcard->hdmatx) != HAL_OK)
2042             {
2043               /* Call Directly hsmartcard->hdmatx->XferAbortCallback function in case of error */
2044               hsmartcard->hdmatx->XferAbortCallback(hsmartcard->hdmatx);
2045             }
2046           }
2047           else
2048           {
2049 #if (USE_HAL_SMARTCARD_REGISTER_CALLBACKS == 1)
2050             /* Call registered user error callback */
2051             hsmartcard->ErrorCallback(hsmartcard);
2052 #else
2053             /* Call legacy weak user error callback */
2054             HAL_SMARTCARD_ErrorCallback(hsmartcard);
2055 #endif /* USE_HAL_SMARTCARD_REGISTER_CALLBACK */
2056           }
2057         }
2058         else
2059         {
2060 #if (USE_HAL_SMARTCARD_REGISTER_CALLBACKS == 1)
2061           /* Call registered user error callback */
2062           hsmartcard->ErrorCallback(hsmartcard);
2063 #else
2064           /* Call legacy weak user error callback */
2065           HAL_SMARTCARD_ErrorCallback(hsmartcard);
2066 #endif /* USE_HAL_SMARTCARD_REGISTER_CALLBACK */
2067         }
2068       }
2069       else
2070       {
2071         /* Non Blocking error : transfer could go on.
2072            Error is notified to user through user error callback */
2073 #if (USE_HAL_SMARTCARD_REGISTER_CALLBACKS == 1)
2074         /* Call registered user error callback */
2075         hsmartcard->ErrorCallback(hsmartcard);
2076 #else
2077         /* Call legacy weak user error callback */
2078         HAL_SMARTCARD_ErrorCallback(hsmartcard);
2079 #endif /* USE_HAL_SMARTCARD_REGISTER_CALLBACK */
2080         hsmartcard->ErrorCode = HAL_SMARTCARD_ERROR_NONE;
2081       }
2082     }
2083     return;
2084 
2085   } /* End if some error occurs */
2086 
2087   /* SMARTCARD in mode Receiver, end of block interruption ------------------------*/
2088   if (((isrflags & USART_ISR_EOBF) != 0U) && ((cr1its & USART_CR1_EOBIE) != 0U))
2089   {
2090     hsmartcard->RxState = HAL_SMARTCARD_STATE_READY;
2091     __HAL_UNLOCK(hsmartcard);
2092 #if (USE_HAL_SMARTCARD_REGISTER_CALLBACKS == 1)
2093     /* Call registered Rx complete callback */
2094     hsmartcard->RxCpltCallback(hsmartcard);
2095 #else
2096     /* Call legacy weak Rx complete callback */
2097     HAL_SMARTCARD_RxCpltCallback(hsmartcard);
2098 #endif /* USE_HAL_SMARTCARD_REGISTER_CALLBACK */
2099     /* Clear EOBF interrupt after HAL_SMARTCARD_RxCpltCallback() call for the End of Block information
2100        to be available during HAL_SMARTCARD_RxCpltCallback() processing */
2101     __HAL_SMARTCARD_CLEAR_IT(hsmartcard, SMARTCARD_CLEAR_EOBF);
2102     return;
2103   }
2104 
2105   /* SMARTCARD in mode Transmitter ------------------------------------------------*/
2106   if (((isrflags & USART_ISR_TXE_TXFNF) != 0U)
2107       && (((cr1its & USART_CR1_TXEIE_TXFNFIE) != 0U)
2108           || ((cr3its & USART_CR3_TXFTIE) != 0U)))
2109   {
2110     if (hsmartcard->TxISR != NULL)
2111     {
2112       hsmartcard->TxISR(hsmartcard);
2113     }
2114     return;
2115   }
2116 
2117   /* SMARTCARD in mode Transmitter (transmission end) ------------------------*/
2118   if (__HAL_SMARTCARD_GET_IT(hsmartcard, hsmartcard->AdvancedInit.TxCompletionIndication) != RESET)
2119   {
2120     if (__HAL_SMARTCARD_GET_IT_SOURCE(hsmartcard, hsmartcard->AdvancedInit.TxCompletionIndication) != RESET)
2121     {
2122       SMARTCARD_EndTransmit_IT(hsmartcard);
2123       return;
2124     }
2125   }
2126 
2127   /* SMARTCARD TX Fifo Empty occurred ----------------------------------------------*/
2128   if (((isrflags & USART_ISR_TXFE) != 0U) && ((cr1its & USART_CR1_TXFEIE) != 0U))
2129   {
2130 #if (USE_HAL_SMARTCARD_REGISTER_CALLBACKS == 1)
2131     /* Call registered Tx Fifo Empty Callback */
2132     hsmartcard->TxFifoEmptyCallback(hsmartcard);
2133 #else
2134     /* Call legacy weak Tx Fifo Empty Callback */
2135     HAL_SMARTCARDEx_TxFifoEmptyCallback(hsmartcard);
2136 #endif /* USE_HAL_SMARTCARD_REGISTER_CALLBACK */
2137     return;
2138   }
2139 
2140   /* SMARTCARD RX Fifo Full occurred ----------------------------------------------*/
2141   if (((isrflags & USART_ISR_RXFF) != 0U) && ((cr1its & USART_CR1_RXFFIE) != 0U))
2142   {
2143 #if (USE_HAL_SMARTCARD_REGISTER_CALLBACKS == 1)
2144     /* Call registered Rx Fifo Full Callback */
2145     hsmartcard->RxFifoFullCallback(hsmartcard);
2146 #else
2147     /* Call legacy weak Rx Fifo Full Callback */
2148     HAL_SMARTCARDEx_RxFifoFullCallback(hsmartcard);
2149 #endif /* USE_HAL_SMARTCARD_REGISTER_CALLBACK */
2150     return;
2151   }
2152 }
2153 
2154 /**
2155   * @brief  Tx Transfer completed callback.
2156   * @param  hsmartcard Pointer to a SMARTCARD_HandleTypeDef structure that contains
2157   *                    the configuration information for the specified SMARTCARD module.
2158   * @retval None
2159   */
2160 __weak void HAL_SMARTCARD_TxCpltCallback(SMARTCARD_HandleTypeDef *hsmartcard)
2161 {
2162   /* Prevent unused argument(s) compilation warning */
2163   UNUSED(hsmartcard);
2164 
2165   /* NOTE : This function should not be modified, when the callback is needed,
2166             the HAL_SMARTCARD_TxCpltCallback can be implemented in the user file.
2167    */
2168 }
2169 
2170 /**
2171   * @brief  Rx Transfer completed callback.
2172   * @param  hsmartcard Pointer to a SMARTCARD_HandleTypeDef structure that contains
2173   *                    the configuration information for the specified SMARTCARD module.
2174   * @retval None
2175   */
2176 __weak void HAL_SMARTCARD_RxCpltCallback(SMARTCARD_HandleTypeDef *hsmartcard)
2177 {
2178   /* Prevent unused argument(s) compilation warning */
2179   UNUSED(hsmartcard);
2180 
2181   /* NOTE : This function should not be modified, when the callback is needed,
2182             the HAL_SMARTCARD_RxCpltCallback can be implemented in the user file.
2183    */
2184 }
2185 
2186 /**
2187   * @brief  SMARTCARD error callback.
2188   * @param  hsmartcard Pointer to a SMARTCARD_HandleTypeDef structure that contains
2189   *                    the configuration information for the specified SMARTCARD module.
2190   * @retval None
2191   */
2192 __weak void HAL_SMARTCARD_ErrorCallback(SMARTCARD_HandleTypeDef *hsmartcard)
2193 {
2194   /* Prevent unused argument(s) compilation warning */
2195   UNUSED(hsmartcard);
2196 
2197   /* NOTE : This function should not be modified, when the callback is needed,
2198             the HAL_SMARTCARD_ErrorCallback can be implemented in the user file.
2199    */
2200 }
2201 
2202 /**
2203   * @brief  SMARTCARD Abort Complete callback.
2204   * @param  hsmartcard Pointer to a SMARTCARD_HandleTypeDef structure that contains
2205   *                    the configuration information for the specified SMARTCARD module.
2206   * @retval None
2207   */
2208 __weak void HAL_SMARTCARD_AbortCpltCallback(SMARTCARD_HandleTypeDef *hsmartcard)
2209 {
2210   /* Prevent unused argument(s) compilation warning */
2211   UNUSED(hsmartcard);
2212 
2213   /* NOTE : This function should not be modified, when the callback is needed,
2214             the HAL_SMARTCARD_AbortCpltCallback can be implemented in the user file.
2215    */
2216 }
2217 
2218 /**
2219   * @brief  SMARTCARD Abort Complete callback.
2220   * @param  hsmartcard Pointer to a SMARTCARD_HandleTypeDef structure that contains
2221   *                    the configuration information for the specified SMARTCARD module.
2222   * @retval None
2223   */
2224 __weak void HAL_SMARTCARD_AbortTransmitCpltCallback(SMARTCARD_HandleTypeDef *hsmartcard)
2225 {
2226   /* Prevent unused argument(s) compilation warning */
2227   UNUSED(hsmartcard);
2228 
2229   /* NOTE : This function should not be modified, when the callback is needed,
2230             the HAL_SMARTCARD_AbortTransmitCpltCallback can be implemented in the user file.
2231    */
2232 }
2233 
2234 /**
2235   * @brief  SMARTCARD Abort Receive Complete callback.
2236   * @param  hsmartcard Pointer to a SMARTCARD_HandleTypeDef structure that contains
2237   *                    the configuration information for the specified SMARTCARD module.
2238   * @retval None
2239   */
2240 __weak void HAL_SMARTCARD_AbortReceiveCpltCallback(SMARTCARD_HandleTypeDef *hsmartcard)
2241 {
2242   /* Prevent unused argument(s) compilation warning */
2243   UNUSED(hsmartcard);
2244 
2245   /* NOTE : This function should not be modified, when the callback is needed,
2246             the HAL_SMARTCARD_AbortReceiveCpltCallback can be implemented in the user file.
2247    */
2248 }
2249 
2250 /**
2251   * @}
2252   */
2253 
2254 /** @defgroup SMARTCARD_Exported_Functions_Group4 Peripheral State and Errors functions
2255   * @ingroup RTEMSBSPsARMSTM32H7
2256   * @brief    SMARTCARD State and Errors functions
2257   *
2258 @verbatim
2259   ==============================================================================
2260                   ##### Peripheral State and Errors functions #####
2261   ==============================================================================
2262   [..]
2263     This subsection provides a set of functions allowing to return the State of SmartCard
2264     handle and also return Peripheral Errors occurred during communication process
2265      (+) HAL_SMARTCARD_GetState() API can be helpful to check in run-time the state
2266          of the SMARTCARD peripheral.
2267      (+) HAL_SMARTCARD_GetError() checks in run-time errors that could occur during
2268          communication.
2269 
2270 @endverbatim
2271   * @{
2272   */
2273 
2274 /**
2275   * @brief  Return the SMARTCARD handle state.
2276   * @param  hsmartcard Pointer to a SMARTCARD_HandleTypeDef structure that contains
2277   *                    the configuration information for the specified SMARTCARD module.
2278   * @retval SMARTCARD handle state
2279   */
2280 HAL_SMARTCARD_StateTypeDef HAL_SMARTCARD_GetState(const SMARTCARD_HandleTypeDef *hsmartcard)
2281 {
2282   /* Return SMARTCARD handle state */
2283   uint32_t temp1;
2284   uint32_t temp2;
2285   temp1 = (uint32_t)hsmartcard->gState;
2286   temp2 = (uint32_t)hsmartcard->RxState;
2287 
2288   return (HAL_SMARTCARD_StateTypeDef)(temp1 | temp2);
2289 }
2290 
2291 /**
2292   * @brief  Return the SMARTCARD handle error code.
2293   * @param  hsmartcard Pointer to a SMARTCARD_HandleTypeDef structure that contains
2294   *                    the configuration information for the specified SMARTCARD module.
2295   * @retval SMARTCARD handle Error Code
2296   */
2297 uint32_t HAL_SMARTCARD_GetError(const SMARTCARD_HandleTypeDef *hsmartcard)
2298 {
2299   return hsmartcard->ErrorCode;
2300 }
2301 
2302 /**
2303   * @}
2304   */
2305 
2306 /**
2307   * @}
2308   */
2309 
2310 /** @defgroup SMARTCARD_Private_Functions SMARTCARD Private Functions
2311   * @ingroup RTEMSBSPsARMSTM32H7
2312   * @{
2313   */
2314 
2315 #if (USE_HAL_SMARTCARD_REGISTER_CALLBACKS == 1)
2316 /**
2317   * @brief  Initialize the callbacks to their default values.
2318   * @param  hsmartcard SMARTCARD handle.
2319   * @retval none
2320   */
2321 void SMARTCARD_InitCallbacksToDefault(SMARTCARD_HandleTypeDef *hsmartcard)
2322 {
2323   /* Init the SMARTCARD Callback settings */
2324   hsmartcard->TxCpltCallback            = HAL_SMARTCARD_TxCpltCallback;            /* Legacy weak TxCpltCallback    */
2325   hsmartcard->RxCpltCallback            = HAL_SMARTCARD_RxCpltCallback;            /* Legacy weak RxCpltCallback    */
2326   hsmartcard->ErrorCallback             = HAL_SMARTCARD_ErrorCallback;             /* Legacy weak ErrorCallback     */
2327   hsmartcard->AbortCpltCallback         = HAL_SMARTCARD_AbortCpltCallback;         /* Legacy weak AbortCpltCallback */
2328   hsmartcard->AbortTransmitCpltCallback = HAL_SMARTCARD_AbortTransmitCpltCallback; /* Legacy weak
2329                                                                                       AbortTransmitCpltCallback     */
2330   hsmartcard->AbortReceiveCpltCallback  = HAL_SMARTCARD_AbortReceiveCpltCallback;  /* Legacy weak
2331                                                                                       AbortReceiveCpltCallback      */
2332   hsmartcard->RxFifoFullCallback        = HAL_SMARTCARDEx_RxFifoFullCallback;      /* Legacy weak
2333                                                                                       RxFifoFullCallback            */
2334   hsmartcard->TxFifoEmptyCallback       = HAL_SMARTCARDEx_TxFifoEmptyCallback;     /* Legacy weak
2335                                                                                       TxFifoEmptyCallback           */
2336 
2337 }
2338 #endif /* USE_HAL_SMARTCARD_REGISTER_CALLBACKS */
2339 
2340 /**
2341   * @brief  Configure the SMARTCARD associated USART peripheral.
2342   * @param  hsmartcard Pointer to a SMARTCARD_HandleTypeDef structure that contains
2343   *                    the configuration information for the specified SMARTCARD module.
2344   * @retval HAL status
2345   */
2346 static HAL_StatusTypeDef SMARTCARD_SetConfig(SMARTCARD_HandleTypeDef *hsmartcard)
2347 {
2348   uint32_t tmpreg;
2349   SMARTCARD_ClockSourceTypeDef clocksource;
2350   HAL_StatusTypeDef ret = HAL_OK;
2351   static const uint16_t SMARTCARDPrescTable[12] = {1U, 2U, 4U, 6U, 8U, 10U, 12U, 16U, 32U, 64U, 128U, 256U};
2352   PLL2_ClocksTypeDef pll2_clocks;
2353   PLL3_ClocksTypeDef pll3_clocks;
2354   uint32_t pclk;
2355 
2356   /* Check the parameters */
2357   assert_param(IS_SMARTCARD_INSTANCE(hsmartcard->Instance));
2358   assert_param(IS_SMARTCARD_BAUDRATE(hsmartcard->Init.BaudRate));
2359   assert_param(IS_SMARTCARD_WORD_LENGTH(hsmartcard->Init.WordLength));
2360   assert_param(IS_SMARTCARD_STOPBITS(hsmartcard->Init.StopBits));
2361   assert_param(IS_SMARTCARD_PARITY(hsmartcard->Init.Parity));
2362   assert_param(IS_SMARTCARD_MODE(hsmartcard->Init.Mode));
2363   assert_param(IS_SMARTCARD_POLARITY(hsmartcard->Init.CLKPolarity));
2364   assert_param(IS_SMARTCARD_PHASE(hsmartcard->Init.CLKPhase));
2365   assert_param(IS_SMARTCARD_LASTBIT(hsmartcard->Init.CLKLastBit));
2366   assert_param(IS_SMARTCARD_ONE_BIT_SAMPLE(hsmartcard->Init.OneBitSampling));
2367   assert_param(IS_SMARTCARD_NACK(hsmartcard->Init.NACKEnable));
2368   assert_param(IS_SMARTCARD_TIMEOUT(hsmartcard->Init.TimeOutEnable));
2369   assert_param(IS_SMARTCARD_AUTORETRY_COUNT(hsmartcard->Init.AutoRetryCount));
2370   assert_param(IS_SMARTCARD_CLOCKPRESCALER(hsmartcard->Init.ClockPrescaler));
2371 
2372   /*-------------------------- USART CR1 Configuration -----------------------*/
2373   /* In SmartCard mode, M and PCE are forced to 1 (8 bits + parity).
2374    * Oversampling is forced to 16 (OVER8 = 0).
2375    * Configure the Parity and Mode:
2376    *  set PS bit according to hsmartcard->Init.Parity value
2377    *  set TE and RE bits according to hsmartcard->Init.Mode value */
2378   tmpreg = (((uint32_t)hsmartcard->Init.Parity) | ((uint32_t)hsmartcard->Init.Mode) |
2379             ((uint32_t)hsmartcard->Init.WordLength));
2380   MODIFY_REG(hsmartcard->Instance->CR1, USART_CR1_FIELDS, tmpreg);
2381 
2382   /*-------------------------- USART CR2 Configuration -----------------------*/
2383   tmpreg = hsmartcard->Init.StopBits;
2384   /* Synchronous mode is activated by default */
2385   tmpreg |= (uint32_t) USART_CR2_CLKEN | hsmartcard->Init.CLKPolarity;
2386   tmpreg |= (uint32_t) hsmartcard->Init.CLKPhase | hsmartcard->Init.CLKLastBit;
2387   tmpreg |= (uint32_t) hsmartcard->Init.TimeOutEnable;
2388   MODIFY_REG(hsmartcard->Instance->CR2, USART_CR2_FIELDS, tmpreg);
2389 
2390   /*-------------------------- USART CR3 Configuration -----------------------*/
2391   /* Configure
2392    * - one-bit sampling method versus three samples' majority rule
2393    *   according to hsmartcard->Init.OneBitSampling
2394    * - NACK transmission in case of parity error according
2395    *   to hsmartcard->Init.NACKEnable
2396    * - autoretry counter according to hsmartcard->Init.AutoRetryCount */
2397 
2398   tmpreg = (uint32_t) hsmartcard->Init.OneBitSampling | hsmartcard->Init.NACKEnable;
2399   tmpreg |= ((uint32_t)hsmartcard->Init.AutoRetryCount << USART_CR3_SCARCNT_Pos);
2400   MODIFY_REG(hsmartcard->Instance->CR3, USART_CR3_FIELDS, tmpreg);
2401 
2402   /*--------------------- SMARTCARD clock PRESC Configuration ----------------*/
2403   /* Configure
2404   * - SMARTCARD Clock Prescaler: set PRESCALER according to hsmartcard->Init.ClockPrescaler value */
2405   MODIFY_REG(hsmartcard->Instance->PRESC, USART_PRESC_PRESCALER, hsmartcard->Init.ClockPrescaler);
2406 
2407   /*-------------------------- USART GTPR Configuration ----------------------*/
2408   tmpreg = (hsmartcard->Init.Prescaler | ((uint32_t)hsmartcard->Init.GuardTime << USART_GTPR_GT_Pos));
2409   MODIFY_REG(hsmartcard->Instance->GTPR, (uint16_t)(USART_GTPR_GT | USART_GTPR_PSC), (uint16_t)tmpreg);
2410 
2411   /*-------------------------- USART RTOR Configuration ----------------------*/
2412   tmpreg = ((uint32_t)hsmartcard->Init.BlockLength << USART_RTOR_BLEN_Pos);
2413   if (hsmartcard->Init.TimeOutEnable == SMARTCARD_TIMEOUT_ENABLE)
2414   {
2415     assert_param(IS_SMARTCARD_TIMEOUT_VALUE(hsmartcard->Init.TimeOutValue));
2416     tmpreg |= (uint32_t) hsmartcard->Init.TimeOutValue;
2417   }
2418   MODIFY_REG(hsmartcard->Instance->RTOR, (USART_RTOR_RTO | USART_RTOR_BLEN), tmpreg);
2419 
2420   /*-------------------------- USART BRR Configuration -----------------------*/
2421   SMARTCARD_GETCLOCKSOURCE(hsmartcard, clocksource);
2422   tmpreg =   0U;
2423   switch (clocksource)
2424   {
2425     case SMARTCARD_CLOCKSOURCE_D2PCLK1:
2426       pclk = HAL_RCC_GetPCLK1Freq();
2427       tmpreg = (uint32_t)(((pclk / SMARTCARDPrescTable[hsmartcard->Init.ClockPrescaler]) +
2428                            (hsmartcard->Init.BaudRate / 2U)) / hsmartcard->Init.BaudRate);
2429       break;
2430     case SMARTCARD_CLOCKSOURCE_D2PCLK2:
2431       pclk = HAL_RCC_GetPCLK2Freq();
2432       tmpreg = (uint32_t)(((pclk / SMARTCARDPrescTable[hsmartcard->Init.ClockPrescaler]) +
2433                            (hsmartcard->Init.BaudRate / 2U)) / hsmartcard->Init.BaudRate);
2434       break;
2435     case SMARTCARD_CLOCKSOURCE_PLL2Q:
2436       HAL_RCCEx_GetPLL2ClockFreq(&pll2_clocks);
2437       tmpreg = (uint32_t)(((pll2_clocks.PLL2_Q_Frequency / SMARTCARDPrescTable[hsmartcard->Init.ClockPrescaler]) +
2438                            (hsmartcard->Init.BaudRate / 2U)) / hsmartcard->Init.BaudRate);
2439       break;
2440     case SMARTCARD_CLOCKSOURCE_PLL3Q:
2441       HAL_RCCEx_GetPLL3ClockFreq(&pll3_clocks);
2442       tmpreg = (uint32_t)(((pll3_clocks.PLL3_Q_Frequency / SMARTCARDPrescTable[hsmartcard->Init.ClockPrescaler]) +
2443                            (hsmartcard->Init.BaudRate / 2U)) / hsmartcard->Init.BaudRate);
2444       break;
2445     case SMARTCARD_CLOCKSOURCE_HSI:
2446       if (__HAL_RCC_GET_FLAG(RCC_FLAG_HSIDIV) != 0U)
2447       {
2448         tmpreg = (uint32_t)((((HSI_VALUE >> (__HAL_RCC_GET_HSI_DIVIDER() >> 3U)) /
2449                               SMARTCARDPrescTable[hsmartcard->Init.ClockPrescaler]) +
2450                              (hsmartcard->Init.BaudRate / 2U)) / hsmartcard->Init.BaudRate);
2451       }
2452       else
2453       {
2454         tmpreg = (uint32_t)(((HSI_VALUE / SMARTCARDPrescTable[hsmartcard->Init.ClockPrescaler]) +
2455                              (hsmartcard->Init.BaudRate / 2U)) / hsmartcard->Init.BaudRate);
2456       }
2457       break;
2458     case SMARTCARD_CLOCKSOURCE_CSI:
2459       tmpreg = (uint32_t)(((CSI_VALUE / SMARTCARDPrescTable[hsmartcard->Init.ClockPrescaler]) +
2460                            (hsmartcard->Init.BaudRate / 2U)) / hsmartcard->Init.BaudRate);
2461       break;
2462     case SMARTCARD_CLOCKSOURCE_LSE:
2463       tmpreg = (uint32_t)(((uint16_t)(LSE_VALUE / SMARTCARDPrescTable[hsmartcard->Init.ClockPrescaler]) +
2464                            (hsmartcard->Init.BaudRate / 2U)) / hsmartcard->Init.BaudRate);
2465       break;
2466     default:
2467       ret = HAL_ERROR;
2468       break;
2469   }
2470 
2471   /* USARTDIV must be greater than or equal to 0d16 */
2472   if ((tmpreg >= USART_BRR_MIN) && (tmpreg <= USART_BRR_MAX))
2473   {
2474     hsmartcard->Instance->BRR = (uint16_t)tmpreg;
2475   }
2476   else
2477   {
2478     ret = HAL_ERROR;
2479   }
2480 
2481   /* Initialize the number of data to process during RX/TX ISR execution */
2482   hsmartcard->NbTxDataToProcess = 1U;
2483   hsmartcard->NbRxDataToProcess = 1U;
2484 
2485   /* Clear ISR function pointers */
2486   hsmartcard->RxISR   = NULL;
2487   hsmartcard->TxISR   = NULL;
2488 
2489   return ret;
2490 }
2491 
2492 
2493 /**
2494   * @brief Configure the SMARTCARD associated USART peripheral advanced features.
2495   * @param hsmartcard Pointer to a SMARTCARD_HandleTypeDef structure that contains
2496   *                   the configuration information for the specified SMARTCARD module.
2497   * @retval None
2498   */
2499 static void SMARTCARD_AdvFeatureConfig(SMARTCARD_HandleTypeDef *hsmartcard)
2500 {
2501   /* Check whether the set of advanced features to configure is properly set */
2502   assert_param(IS_SMARTCARD_ADVFEATURE_INIT(hsmartcard->AdvancedInit.AdvFeatureInit));
2503 
2504   /* if required, configure TX pin active level inversion */
2505   if (HAL_IS_BIT_SET(hsmartcard->AdvancedInit.AdvFeatureInit, SMARTCARD_ADVFEATURE_TXINVERT_INIT))
2506   {
2507     assert_param(IS_SMARTCARD_ADVFEATURE_TXINV(hsmartcard->AdvancedInit.TxPinLevelInvert));
2508     MODIFY_REG(hsmartcard->Instance->CR2, USART_CR2_TXINV, hsmartcard->AdvancedInit.TxPinLevelInvert);
2509   }
2510 
2511   /* if required, configure RX pin active level inversion */
2512   if (HAL_IS_BIT_SET(hsmartcard->AdvancedInit.AdvFeatureInit, SMARTCARD_ADVFEATURE_RXINVERT_INIT))
2513   {
2514     assert_param(IS_SMARTCARD_ADVFEATURE_RXINV(hsmartcard->AdvancedInit.RxPinLevelInvert));
2515     MODIFY_REG(hsmartcard->Instance->CR2, USART_CR2_RXINV, hsmartcard->AdvancedInit.RxPinLevelInvert);
2516   }
2517 
2518   /* if required, configure data inversion */
2519   if (HAL_IS_BIT_SET(hsmartcard->AdvancedInit.AdvFeatureInit, SMARTCARD_ADVFEATURE_DATAINVERT_INIT))
2520   {
2521     assert_param(IS_SMARTCARD_ADVFEATURE_DATAINV(hsmartcard->AdvancedInit.DataInvert));
2522     MODIFY_REG(hsmartcard->Instance->CR2, USART_CR2_DATAINV, hsmartcard->AdvancedInit.DataInvert);
2523   }
2524 
2525   /* if required, configure RX/TX pins swap */
2526   if (HAL_IS_BIT_SET(hsmartcard->AdvancedInit.AdvFeatureInit, SMARTCARD_ADVFEATURE_SWAP_INIT))
2527   {
2528     assert_param(IS_SMARTCARD_ADVFEATURE_SWAP(hsmartcard->AdvancedInit.Swap));
2529     MODIFY_REG(hsmartcard->Instance->CR2, USART_CR2_SWAP, hsmartcard->AdvancedInit.Swap);
2530   }
2531 
2532   /* if required, configure RX overrun detection disabling */
2533   if (HAL_IS_BIT_SET(hsmartcard->AdvancedInit.AdvFeatureInit, SMARTCARD_ADVFEATURE_RXOVERRUNDISABLE_INIT))
2534   {
2535     assert_param(IS_SMARTCARD_OVERRUN(hsmartcard->AdvancedInit.OverrunDisable));
2536     MODIFY_REG(hsmartcard->Instance->CR3, USART_CR3_OVRDIS, hsmartcard->AdvancedInit.OverrunDisable);
2537   }
2538 
2539   /* if required, configure DMA disabling on reception error */
2540   if (HAL_IS_BIT_SET(hsmartcard->AdvancedInit.AdvFeatureInit, SMARTCARD_ADVFEATURE_DMADISABLEONERROR_INIT))
2541   {
2542     assert_param(IS_SMARTCARD_ADVFEATURE_DMAONRXERROR(hsmartcard->AdvancedInit.DMADisableonRxError));
2543     MODIFY_REG(hsmartcard->Instance->CR3, USART_CR3_DDRE, hsmartcard->AdvancedInit.DMADisableonRxError);
2544   }
2545 
2546   /* if required, configure MSB first on communication line */
2547   if (HAL_IS_BIT_SET(hsmartcard->AdvancedInit.AdvFeatureInit, SMARTCARD_ADVFEATURE_MSBFIRST_INIT))
2548   {
2549     assert_param(IS_SMARTCARD_ADVFEATURE_MSBFIRST(hsmartcard->AdvancedInit.MSBFirst));
2550     MODIFY_REG(hsmartcard->Instance->CR2, USART_CR2_MSBFIRST, hsmartcard->AdvancedInit.MSBFirst);
2551   }
2552 
2553 }
2554 
2555 /**
2556   * @brief Check the SMARTCARD Idle State.
2557   * @param hsmartcard Pointer to a SMARTCARD_HandleTypeDef structure that contains
2558   *                   the configuration information for the specified SMARTCARD module.
2559   * @retval HAL status
2560   */
2561 static HAL_StatusTypeDef SMARTCARD_CheckIdleState(SMARTCARD_HandleTypeDef *hsmartcard)
2562 {
2563   uint32_t tickstart;
2564 
2565   /* Initialize the SMARTCARD ErrorCode */
2566   hsmartcard->ErrorCode = HAL_SMARTCARD_ERROR_NONE;
2567 
2568   /* Init tickstart for timeout management */
2569   tickstart = HAL_GetTick();
2570 
2571   /* Check if the Transmitter is enabled */
2572   if ((hsmartcard->Instance->CR1 & USART_CR1_TE) == USART_CR1_TE)
2573   {
2574     /* Wait until TEACK flag is set */
2575     if (SMARTCARD_WaitOnFlagUntilTimeout(hsmartcard, USART_ISR_TEACK, RESET, tickstart,
2576                                          SMARTCARD_TEACK_REACK_TIMEOUT) != HAL_OK)
2577     {
2578       /* Timeout occurred */
2579       return HAL_TIMEOUT;
2580     }
2581   }
2582   /* Check if the Receiver is enabled */
2583   if ((hsmartcard->Instance->CR1 & USART_CR1_RE) == USART_CR1_RE)
2584   {
2585     /* Wait until REACK flag is set */
2586     if (SMARTCARD_WaitOnFlagUntilTimeout(hsmartcard, USART_ISR_REACK, RESET, tickstart,
2587                                          SMARTCARD_TEACK_REACK_TIMEOUT) != HAL_OK)
2588     {
2589       /* Timeout occurred */
2590       return HAL_TIMEOUT;
2591     }
2592   }
2593 
2594   /* Initialize the SMARTCARD states */
2595   hsmartcard->gState  = HAL_SMARTCARD_STATE_READY;
2596   hsmartcard->RxState = HAL_SMARTCARD_STATE_READY;
2597 
2598   /* Process Unlocked */
2599   __HAL_UNLOCK(hsmartcard);
2600 
2601   return HAL_OK;
2602 }
2603 
2604 /**
2605   * @brief  Handle SMARTCARD Communication Timeout. It waits
2606   *         until a flag is no longer in the specified status.
2607   * @param  hsmartcard Pointer to a SMARTCARD_HandleTypeDef structure that contains
2608   *                   the configuration information for the specified SMARTCARD module.
2609   * @param  Flag Specifies the SMARTCARD flag to check.
2610   * @param  Status The actual Flag status (SET or RESET).
2611   * @param  Tickstart Tick start value
2612   * @param  Timeout Timeout duration.
2613   * @retval HAL status
2614   */
2615 static HAL_StatusTypeDef SMARTCARD_WaitOnFlagUntilTimeout(SMARTCARD_HandleTypeDef *hsmartcard, uint32_t Flag,
2616                                                           FlagStatus Status, uint32_t Tickstart, uint32_t Timeout)
2617 {
2618   /* Wait until flag is set */
2619   while ((__HAL_SMARTCARD_GET_FLAG(hsmartcard, Flag) ? SET : RESET) == Status)
2620   {
2621     /* Check for the Timeout */
2622     if (Timeout != HAL_MAX_DELAY)
2623     {
2624       if (((HAL_GetTick() - Tickstart) > Timeout) || (Timeout == 0U))
2625       {
2626         /* Disable TXE, RXNE, PE and ERR (Frame error, noise error, overrun error)
2627            interrupts for the interrupt process */
2628         CLEAR_BIT(hsmartcard->Instance->CR1, (USART_CR1_RXNEIE_RXFNEIE | USART_CR1_PEIE | USART_CR1_TXEIE_TXFNFIE));
2629         CLEAR_BIT(hsmartcard->Instance->CR3, USART_CR3_EIE);
2630 
2631         hsmartcard->gState  = HAL_SMARTCARD_STATE_READY;
2632         hsmartcard->RxState = HAL_SMARTCARD_STATE_READY;
2633 
2634         /* Process Unlocked */
2635         __HAL_UNLOCK(hsmartcard);
2636         return HAL_TIMEOUT;
2637       }
2638     }
2639   }
2640   return HAL_OK;
2641 }
2642 
2643 
2644 /**
2645   * @brief  End ongoing Tx transfer on SMARTCARD peripheral (following error detection or Transmit completion).
2646   * @param  hsmartcard Pointer to a SMARTCARD_HandleTypeDef structure that contains
2647   *                    the configuration information for the specified SMARTCARD module.
2648   * @retval None
2649   */
2650 static void SMARTCARD_EndTxTransfer(SMARTCARD_HandleTypeDef *hsmartcard)
2651 {
2652   /* Disable TXEIE, TCIE and ERR (Frame error, noise error, overrun error) interrupts */
2653   CLEAR_BIT(hsmartcard->Instance->CR1, (USART_CR1_TXEIE_TXFNFIE | USART_CR1_TCIE));
2654   CLEAR_BIT(hsmartcard->Instance->CR3, USART_CR3_EIE);
2655 
2656   /* At end of Tx process, restore hsmartcard->gState to Ready */
2657   hsmartcard->gState = HAL_SMARTCARD_STATE_READY;
2658 }
2659 
2660 
2661 /**
2662   * @brief  End ongoing Rx transfer on UART peripheral (following error detection or Reception completion).
2663   * @param  hsmartcard Pointer to a SMARTCARD_HandleTypeDef structure that contains
2664   *                    the configuration information for the specified SMARTCARD module.
2665   * @retval None
2666   */
2667 static void SMARTCARD_EndRxTransfer(SMARTCARD_HandleTypeDef *hsmartcard)
2668 {
2669   /* Disable RXNE, PE and ERR (Frame error, noise error, overrun error) interrupts */
2670   CLEAR_BIT(hsmartcard->Instance->CR1, (USART_CR1_RXNEIE_RXFNEIE | USART_CR1_PEIE));
2671   CLEAR_BIT(hsmartcard->Instance->CR3, USART_CR3_EIE);
2672 
2673   /* At end of Rx process, restore hsmartcard->RxState to Ready */
2674   hsmartcard->RxState = HAL_SMARTCARD_STATE_READY;
2675 }
2676 
2677 
2678 /**
2679   * @brief  DMA SMARTCARD transmit process complete callback.
2680   * @param  hdma Pointer to a DMA_HandleTypeDef structure that contains
2681   *              the configuration information for the specified DMA module.
2682   * @retval None
2683   */
2684 static void SMARTCARD_DMATransmitCplt(DMA_HandleTypeDef *hdma)
2685 {
2686   SMARTCARD_HandleTypeDef *hsmartcard = (SMARTCARD_HandleTypeDef *)(hdma->Parent);
2687   hsmartcard->TxXferCount = 0U;
2688 
2689   /* Disable the DMA transfer for transmit request by resetting the DMAT bit
2690   in the SMARTCARD associated USART CR3 register */
2691   CLEAR_BIT(hsmartcard->Instance->CR3, USART_CR3_DMAT);
2692 
2693   /* Enable the SMARTCARD Transmit Complete Interrupt */
2694   __HAL_SMARTCARD_ENABLE_IT(hsmartcard, hsmartcard->AdvancedInit.TxCompletionIndication);
2695 }
2696 
2697 /**
2698   * @brief  DMA SMARTCARD receive process complete callback.
2699   * @param  hdma Pointer to a DMA_HandleTypeDef structure that contains
2700   *              the configuration information for the specified DMA module.
2701   * @retval None
2702   */
2703 static void SMARTCARD_DMAReceiveCplt(DMA_HandleTypeDef *hdma)
2704 {
2705   SMARTCARD_HandleTypeDef *hsmartcard = (SMARTCARD_HandleTypeDef *)(hdma->Parent);
2706   hsmartcard->RxXferCount = 0U;
2707 
2708   /* Disable PE and ERR (Frame error, noise error, overrun error) interrupts */
2709   CLEAR_BIT(hsmartcard->Instance->CR1, USART_CR1_PEIE);
2710   CLEAR_BIT(hsmartcard->Instance->CR3, USART_CR3_EIE);
2711 
2712   /* Disable the DMA transfer for the receiver request by resetting the DMAR bit
2713      in the SMARTCARD associated USART CR3 register */
2714   CLEAR_BIT(hsmartcard->Instance->CR3, USART_CR3_DMAR);
2715 
2716   /* At end of Rx process, restore hsmartcard->RxState to Ready */
2717   hsmartcard->RxState = HAL_SMARTCARD_STATE_READY;
2718 
2719 #if (USE_HAL_SMARTCARD_REGISTER_CALLBACKS == 1)
2720   /* Call registered Rx complete callback */
2721   hsmartcard->RxCpltCallback(hsmartcard);
2722 #else
2723   /* Call legacy weak Rx complete callback */
2724   HAL_SMARTCARD_RxCpltCallback(hsmartcard);
2725 #endif /* USE_HAL_SMARTCARD_REGISTER_CALLBACK */
2726 }
2727 
2728 /**
2729   * @brief  DMA SMARTCARD communication error callback.
2730   * @param  hdma Pointer to a DMA_HandleTypeDef structure that contains
2731   *              the configuration information for the specified DMA module.
2732   * @retval None
2733   */
2734 static void SMARTCARD_DMAError(DMA_HandleTypeDef *hdma)
2735 {
2736   SMARTCARD_HandleTypeDef *hsmartcard = (SMARTCARD_HandleTypeDef *)(hdma->Parent);
2737 
2738   /* Stop SMARTCARD DMA Tx request if ongoing */
2739   if (hsmartcard->gState == HAL_SMARTCARD_STATE_BUSY_TX)
2740   {
2741     if (HAL_IS_BIT_SET(hsmartcard->Instance->CR3, USART_CR3_DMAT))
2742     {
2743       hsmartcard->TxXferCount = 0U;
2744       SMARTCARD_EndTxTransfer(hsmartcard);
2745     }
2746   }
2747 
2748   /* Stop SMARTCARD DMA Rx request if ongoing */
2749   if (hsmartcard->RxState == HAL_SMARTCARD_STATE_BUSY_RX)
2750   {
2751     if (HAL_IS_BIT_SET(hsmartcard->Instance->CR3, USART_CR3_DMAR))
2752     {
2753       hsmartcard->RxXferCount = 0U;
2754       SMARTCARD_EndRxTransfer(hsmartcard);
2755     }
2756   }
2757 
2758   hsmartcard->ErrorCode |= HAL_SMARTCARD_ERROR_DMA;
2759 #if (USE_HAL_SMARTCARD_REGISTER_CALLBACKS == 1)
2760   /* Call registered user error callback */
2761   hsmartcard->ErrorCallback(hsmartcard);
2762 #else
2763   /* Call legacy weak user error callback */
2764   HAL_SMARTCARD_ErrorCallback(hsmartcard);
2765 #endif /* USE_HAL_SMARTCARD_REGISTER_CALLBACK */
2766 }
2767 
2768 /**
2769   * @brief  DMA SMARTCARD communication abort callback, when initiated by HAL services on Error
2770   *         (To be called at end of DMA Abort procedure following error occurrence).
2771   * @param  hdma DMA handle.
2772   * @retval None
2773   */
2774 static void SMARTCARD_DMAAbortOnError(DMA_HandleTypeDef *hdma)
2775 {
2776   SMARTCARD_HandleTypeDef *hsmartcard = (SMARTCARD_HandleTypeDef *)(hdma->Parent);
2777   hsmartcard->RxXferCount = 0U;
2778   hsmartcard->TxXferCount = 0U;
2779 
2780 #if (USE_HAL_SMARTCARD_REGISTER_CALLBACKS == 1)
2781   /* Call registered user error callback */
2782   hsmartcard->ErrorCallback(hsmartcard);
2783 #else
2784   /* Call legacy weak user error callback */
2785   HAL_SMARTCARD_ErrorCallback(hsmartcard);
2786 #endif /* USE_HAL_SMARTCARD_REGISTER_CALLBACK */
2787 }
2788 
2789 /**
2790   * @brief  DMA SMARTCARD Tx communication abort callback, when initiated by user
2791   *         (To be called at end of DMA Tx Abort procedure following user abort request).
2792   * @note   When this callback is executed, User Abort complete call back is called only if no
2793   *         Abort still ongoing for Rx DMA Handle.
2794   * @param  hdma DMA handle.
2795   * @retval None
2796   */
2797 static void SMARTCARD_DMATxAbortCallback(DMA_HandleTypeDef *hdma)
2798 {
2799   SMARTCARD_HandleTypeDef *hsmartcard = (SMARTCARD_HandleTypeDef *)(hdma->Parent);
2800 
2801   hsmartcard->hdmatx->XferAbortCallback = NULL;
2802 
2803   /* Check if an Abort process is still ongoing */
2804   if (hsmartcard->hdmarx != NULL)
2805   {
2806     if (hsmartcard->hdmarx->XferAbortCallback != NULL)
2807     {
2808       return;
2809     }
2810   }
2811 
2812   /* No Abort process still ongoing : All DMA channels are aborted, call user Abort Complete callback */
2813   hsmartcard->TxXferCount = 0U;
2814   hsmartcard->RxXferCount = 0U;
2815 
2816   /* Reset errorCode */
2817   hsmartcard->ErrorCode = HAL_SMARTCARD_ERROR_NONE;
2818 
2819   /* Clear the Error flags in the ICR register */
2820   __HAL_SMARTCARD_CLEAR_FLAG(hsmartcard,
2821                              SMARTCARD_CLEAR_OREF | SMARTCARD_CLEAR_NEF | SMARTCARD_CLEAR_PEF | SMARTCARD_CLEAR_FEF |
2822                              SMARTCARD_CLEAR_RTOF | SMARTCARD_CLEAR_EOBF);
2823 
2824   /* Restore hsmartcard->gState and hsmartcard->RxState to Ready */
2825   hsmartcard->gState  = HAL_SMARTCARD_STATE_READY;
2826   hsmartcard->RxState = HAL_SMARTCARD_STATE_READY;
2827 
2828 #if (USE_HAL_SMARTCARD_REGISTER_CALLBACKS == 1)
2829   /* Call registered Abort complete callback */
2830   hsmartcard->AbortCpltCallback(hsmartcard);
2831 #else
2832   /* Call legacy weak Abort complete callback */
2833   HAL_SMARTCARD_AbortCpltCallback(hsmartcard);
2834 #endif /* USE_HAL_SMARTCARD_REGISTER_CALLBACK */
2835 }
2836 
2837 
2838 /**
2839   * @brief  DMA SMARTCARD Rx communication abort callback, when initiated by user
2840   *         (To be called at end of DMA Rx Abort procedure following user abort request).
2841   * @note   When this callback is executed, User Abort complete call back is called only if no
2842   *         Abort still ongoing for Tx DMA Handle.
2843   * @param  hdma DMA handle.
2844   * @retval None
2845   */
2846 static void SMARTCARD_DMARxAbortCallback(DMA_HandleTypeDef *hdma)
2847 {
2848   SMARTCARD_HandleTypeDef *hsmartcard = (SMARTCARD_HandleTypeDef *)(hdma->Parent);
2849 
2850   hsmartcard->hdmarx->XferAbortCallback = NULL;
2851 
2852   /* Check if an Abort process is still ongoing */
2853   if (hsmartcard->hdmatx != NULL)
2854   {
2855     if (hsmartcard->hdmatx->XferAbortCallback != NULL)
2856     {
2857       return;
2858     }
2859   }
2860 
2861   /* No Abort process still ongoing : All DMA channels are aborted, call user Abort Complete callback */
2862   hsmartcard->TxXferCount = 0U;
2863   hsmartcard->RxXferCount = 0U;
2864 
2865   /* Reset errorCode */
2866   hsmartcard->ErrorCode = HAL_SMARTCARD_ERROR_NONE;
2867 
2868   /* Clear the Error flags in the ICR register */
2869   __HAL_SMARTCARD_CLEAR_FLAG(hsmartcard,
2870                              SMARTCARD_CLEAR_OREF | SMARTCARD_CLEAR_NEF | SMARTCARD_CLEAR_PEF | SMARTCARD_CLEAR_FEF |
2871                              SMARTCARD_CLEAR_RTOF | SMARTCARD_CLEAR_EOBF);
2872 
2873   /* Restore hsmartcard->gState and hsmartcard->RxState to Ready */
2874   hsmartcard->gState  = HAL_SMARTCARD_STATE_READY;
2875   hsmartcard->RxState = HAL_SMARTCARD_STATE_READY;
2876 
2877 #if (USE_HAL_SMARTCARD_REGISTER_CALLBACKS == 1)
2878   /* Call registered Abort complete callback */
2879   hsmartcard->AbortCpltCallback(hsmartcard);
2880 #else
2881   /* Call legacy weak Abort complete callback */
2882   HAL_SMARTCARD_AbortCpltCallback(hsmartcard);
2883 #endif /* USE_HAL_SMARTCARD_REGISTER_CALLBACK */
2884 }
2885 
2886 
2887 /**
2888   * @brief  DMA SMARTCARD Tx communication abort callback, when initiated by user by a call to
2889   *         HAL_SMARTCARD_AbortTransmit_IT API (Abort only Tx transfer)
2890   *         (This callback is executed at end of DMA Tx Abort procedure following user abort request,
2891   *         and leads to user Tx Abort Complete callback execution).
2892   * @param  hdma DMA handle.
2893   * @retval None
2894   */
2895 static void SMARTCARD_DMATxOnlyAbortCallback(DMA_HandleTypeDef *hdma)
2896 {
2897   SMARTCARD_HandleTypeDef *hsmartcard = (SMARTCARD_HandleTypeDef *)(hdma->Parent);
2898 
2899   hsmartcard->TxXferCount = 0U;
2900 
2901   /* Clear the Error flags in the ICR register */
2902   __HAL_SMARTCARD_CLEAR_FLAG(hsmartcard, SMARTCARD_CLEAR_FEF);
2903 
2904   /* Restore hsmartcard->gState to Ready */
2905   hsmartcard->gState = HAL_SMARTCARD_STATE_READY;
2906 
2907 #if (USE_HAL_SMARTCARD_REGISTER_CALLBACKS == 1)
2908   /* Call registered Abort Transmit Complete Callback */
2909   hsmartcard->AbortTransmitCpltCallback(hsmartcard);
2910 #else
2911   /* Call legacy weak Abort Transmit Complete Callback */
2912   HAL_SMARTCARD_AbortTransmitCpltCallback(hsmartcard);
2913 #endif /* USE_HAL_SMARTCARD_REGISTER_CALLBACK */
2914 }
2915 
2916 /**
2917   * @brief  DMA SMARTCARD Rx communication abort callback, when initiated by user by a call to
2918   *         HAL_SMARTCARD_AbortReceive_IT API (Abort only Rx transfer)
2919   *         (This callback is executed at end of DMA Rx Abort procedure following user abort request,
2920   *         and leads to user Rx Abort Complete callback execution).
2921   * @param  hdma DMA handle.
2922   * @retval None
2923   */
2924 static void SMARTCARD_DMARxOnlyAbortCallback(DMA_HandleTypeDef *hdma)
2925 {
2926   SMARTCARD_HandleTypeDef *hsmartcard = (SMARTCARD_HandleTypeDef *)(hdma->Parent);
2927 
2928   hsmartcard->RxXferCount = 0U;
2929 
2930   /* Clear the Error flags in the ICR register */
2931   __HAL_SMARTCARD_CLEAR_FLAG(hsmartcard,
2932                              SMARTCARD_CLEAR_OREF | SMARTCARD_CLEAR_NEF | SMARTCARD_CLEAR_PEF | SMARTCARD_CLEAR_FEF |
2933                              SMARTCARD_CLEAR_RTOF | SMARTCARD_CLEAR_EOBF);
2934 
2935   /* Restore hsmartcard->RxState to Ready */
2936   hsmartcard->RxState = HAL_SMARTCARD_STATE_READY;
2937 
2938 #if (USE_HAL_SMARTCARD_REGISTER_CALLBACKS == 1)
2939   /* Call registered Abort Receive Complete Callback */
2940   hsmartcard->AbortReceiveCpltCallback(hsmartcard);
2941 #else
2942   /* Call legacy weak Abort Receive Complete Callback */
2943   HAL_SMARTCARD_AbortReceiveCpltCallback(hsmartcard);
2944 #endif /* USE_HAL_SMARTCARD_REGISTER_CALLBACK */
2945 }
2946 
2947 /**
2948   * @brief  Send an amount of data in non-blocking mode.
2949   * @note   Function called under interruption only, once
2950   *         interruptions have been enabled by HAL_SMARTCARD_Transmit_IT()
2951   *         and when the FIFO mode is disabled.
2952   * @param  hsmartcard Pointer to a SMARTCARD_HandleTypeDef structure that contains
2953   *                    the configuration information for the specified SMARTCARD module.
2954   * @retval None
2955   */
2956 static void SMARTCARD_TxISR(SMARTCARD_HandleTypeDef *hsmartcard)
2957 {
2958   /* Check that a Tx process is ongoing */
2959   if (hsmartcard->gState == HAL_SMARTCARD_STATE_BUSY_TX)
2960   {
2961     if (hsmartcard->TxXferCount == 0U)
2962     {
2963       /* Disable the SMARTCARD Transmit Data Register Empty Interrupt */
2964       CLEAR_BIT(hsmartcard->Instance->CR1, USART_CR1_TXEIE_TXFNFIE);
2965 
2966       /* Enable the SMARTCARD Transmit Complete Interrupt */
2967       __HAL_SMARTCARD_ENABLE_IT(hsmartcard, hsmartcard->AdvancedInit.TxCompletionIndication);
2968     }
2969     else
2970     {
2971       hsmartcard->Instance->TDR = (uint8_t)(*hsmartcard->pTxBuffPtr & 0xFFU);
2972       hsmartcard->pTxBuffPtr++;
2973       hsmartcard->TxXferCount--;
2974     }
2975   }
2976 }
2977 
2978 /**
2979   * @brief  Send an amount of data in non-blocking mode.
2980   * @note   Function called under interruption only, once
2981   *         interruptions have been enabled by HAL_SMARTCARD_Transmit_IT()
2982   *         and when the FIFO mode is enabled.
2983   * @param  hsmartcard Pointer to a SMARTCARD_HandleTypeDef structure that contains
2984   *                    the configuration information for the specified SMARTCARD module.
2985   * @retval None
2986   */
2987 static void SMARTCARD_TxISR_FIFOEN(SMARTCARD_HandleTypeDef *hsmartcard)
2988 {
2989   uint16_t   nb_tx_data;
2990 
2991   /* Check that a Tx process is ongoing */
2992   if (hsmartcard->gState == HAL_SMARTCARD_STATE_BUSY_TX)
2993   {
2994     for (nb_tx_data = hsmartcard->NbTxDataToProcess ; nb_tx_data > 0U ; nb_tx_data--)
2995     {
2996       if (hsmartcard->TxXferCount == 0U)
2997       {
2998         /* Disable the SMARTCARD Transmit Data Register Empty Interrupt */
2999         CLEAR_BIT(hsmartcard->Instance->CR1, USART_CR1_TXEIE_TXFNFIE);
3000 
3001         /* Enable the SMARTCARD Transmit Complete Interrupt */
3002         __HAL_SMARTCARD_ENABLE_IT(hsmartcard, hsmartcard->AdvancedInit.TxCompletionIndication);
3003       }
3004       else if (READ_BIT(hsmartcard->Instance->ISR, USART_ISR_TXE_TXFNF) != 0U)
3005       {
3006         hsmartcard->Instance->TDR = (uint8_t)(*hsmartcard->pTxBuffPtr & 0xFFU);
3007         hsmartcard->pTxBuffPtr++;
3008         hsmartcard->TxXferCount--;
3009       }
3010       else
3011       {
3012         /* Nothing to do */
3013       }
3014     }
3015   }
3016 }
3017 
3018 /**
3019   * @brief  Wrap up transmission in non-blocking mode.
3020   * @param  hsmartcard Pointer to a SMARTCARD_HandleTypeDef structure that contains
3021   *                    the configuration information for the specified SMARTCARD module.
3022   * @retval None
3023   */
3024 static void SMARTCARD_EndTransmit_IT(SMARTCARD_HandleTypeDef *hsmartcard)
3025 {
3026   /* Disable the SMARTCARD Transmit Complete Interrupt */
3027   __HAL_SMARTCARD_DISABLE_IT(hsmartcard, hsmartcard->AdvancedInit.TxCompletionIndication);
3028 
3029   /* Check if a receive process is ongoing or not. If not disable ERR IT */
3030   if (hsmartcard->RxState == HAL_SMARTCARD_STATE_READY)
3031   {
3032     /* Disable the SMARTCARD Error Interrupt: (Frame error) */
3033     CLEAR_BIT(hsmartcard->Instance->CR3, USART_CR3_EIE);
3034   }
3035 
3036   /* Disable the Peripheral first to update mode */
3037   CLEAR_BIT(hsmartcard->Instance->CR1, USART_CR1_UE);
3038   if ((hsmartcard->Init.Mode == SMARTCARD_MODE_TX)
3039       && (hsmartcard->Init.NACKEnable == SMARTCARD_NACK_ENABLE))
3040   {
3041     /* In case of TX only mode, if NACK is enabled, receiver block has been enabled
3042        for Transmit phase. Disable this receiver block. */
3043     CLEAR_BIT(hsmartcard->Instance->CR1, USART_CR1_RE);
3044   }
3045   if ((hsmartcard->Init.Mode == SMARTCARD_MODE_TX_RX)
3046       || (hsmartcard->Init.NACKEnable == SMARTCARD_NACK_ENABLE))
3047   {
3048     /* Perform a TX FIFO Flush at end of Tx phase, as all sent bytes are appearing in Rx Data register */
3049     __HAL_SMARTCARD_FLUSH_DRREGISTER(hsmartcard);
3050   }
3051   SET_BIT(hsmartcard->Instance->CR1, USART_CR1_UE);
3052 
3053   /* Tx process is ended, restore hsmartcard->gState to Ready */
3054   hsmartcard->gState = HAL_SMARTCARD_STATE_READY;
3055 
3056   /* Clear TxISR function pointer */
3057   hsmartcard->TxISR = NULL;
3058 
3059 #if (USE_HAL_SMARTCARD_REGISTER_CALLBACKS == 1)
3060   /* Call registered Tx complete callback */
3061   hsmartcard->TxCpltCallback(hsmartcard);
3062 #else
3063   /* Call legacy weak Tx complete callback */
3064   HAL_SMARTCARD_TxCpltCallback(hsmartcard);
3065 #endif /* USE_HAL_SMARTCARD_REGISTER_CALLBACK */
3066 }
3067 
3068 /**
3069   * @brief  Receive an amount of data in non-blocking mode.
3070   * @note   Function called under interruption only, once
3071   *         interruptions have been enabled by HAL_SMARTCARD_Receive_IT()
3072   *         and when the FIFO mode is disabled.
3073   * @param  hsmartcard Pointer to a SMARTCARD_HandleTypeDef structure that contains
3074   *                    the configuration information for the specified SMARTCARD module.
3075   * @retval None
3076   */
3077 static void SMARTCARD_RxISR(SMARTCARD_HandleTypeDef *hsmartcard)
3078 {
3079   /* Check that a Rx process is ongoing */
3080   if (hsmartcard->RxState == HAL_SMARTCARD_STATE_BUSY_RX)
3081   {
3082     *hsmartcard->pRxBuffPtr = (uint8_t)(hsmartcard->Instance->RDR & (uint8_t)0xFF);
3083     hsmartcard->pRxBuffPtr++;
3084 
3085     hsmartcard->RxXferCount--;
3086     if (hsmartcard->RxXferCount == 0U)
3087     {
3088       CLEAR_BIT(hsmartcard->Instance->CR1, USART_CR1_RXNEIE_RXFNEIE);
3089 
3090       /* Check if a transmit process is ongoing or not. If not disable ERR IT */
3091       if (hsmartcard->gState == HAL_SMARTCARD_STATE_READY)
3092       {
3093         /* Disable the SMARTCARD Error Interrupt: (Frame error, noise error, overrun error) */
3094         CLEAR_BIT(hsmartcard->Instance->CR3, USART_CR3_EIE);
3095       }
3096 
3097       /* Disable the SMARTCARD Parity Error Interrupt */
3098       CLEAR_BIT(hsmartcard->Instance->CR1, USART_CR1_PEIE);
3099 
3100       hsmartcard->RxState = HAL_SMARTCARD_STATE_READY;
3101 
3102       /* Clear RxISR function pointer */
3103       hsmartcard->RxISR = NULL;
3104 
3105 #if (USE_HAL_SMARTCARD_REGISTER_CALLBACKS == 1)
3106       /* Call registered Rx complete callback */
3107       hsmartcard->RxCpltCallback(hsmartcard);
3108 #else
3109       /* Call legacy weak Rx complete callback */
3110       HAL_SMARTCARD_RxCpltCallback(hsmartcard);
3111 #endif /* USE_HAL_SMARTCARD_REGISTER_CALLBACK */
3112     }
3113   }
3114   else
3115   {
3116     /* Clear RXNE interrupt flag */
3117     __HAL_SMARTCARD_SEND_REQ(hsmartcard, SMARTCARD_RXDATA_FLUSH_REQUEST);
3118   }
3119 }
3120 
3121 /**
3122   * @brief  Receive an amount of data in non-blocking mode.
3123   * @note   Function called under interruption only, once
3124   *         interruptions have been enabled by HAL_SMARTCARD_Receive_IT()
3125   *         and when the FIFO mode is enabled.
3126   * @param  hsmartcard Pointer to a SMARTCARD_HandleTypeDef structure that contains
3127   *                    the configuration information for the specified SMARTCARD module.
3128   * @retval None
3129   */
3130 static void SMARTCARD_RxISR_FIFOEN(SMARTCARD_HandleTypeDef *hsmartcard)
3131 {
3132   uint16_t   nb_rx_data;
3133   uint16_t rxdatacount;
3134 
3135   /* Check that a Rx process is ongoing */
3136   if (hsmartcard->RxState == HAL_SMARTCARD_STATE_BUSY_RX)
3137   {
3138     for (nb_rx_data = hsmartcard->NbRxDataToProcess ; nb_rx_data > 0U ; nb_rx_data--)
3139     {
3140       *hsmartcard->pRxBuffPtr = (uint8_t)(hsmartcard->Instance->RDR & (uint8_t)0xFF);
3141       hsmartcard->pRxBuffPtr++;
3142 
3143       hsmartcard->RxXferCount--;
3144       if (hsmartcard->RxXferCount == 0U)
3145       {
3146         CLEAR_BIT(hsmartcard->Instance->CR1, USART_CR1_RXNEIE_RXFNEIE);
3147 
3148         /* Check if a transmit process is ongoing or not. If not disable ERR IT */
3149         if (hsmartcard->gState == HAL_SMARTCARD_STATE_READY)
3150         {
3151           /* Disable the SMARTCARD Error Interrupt: (Frame error, noise error, overrun error) */
3152           CLEAR_BIT(hsmartcard->Instance->CR3, USART_CR3_EIE);
3153         }
3154 
3155         /* Disable the SMARTCARD Parity Error Interrupt */
3156         CLEAR_BIT(hsmartcard->Instance->CR1, USART_CR1_PEIE);
3157 
3158         hsmartcard->RxState = HAL_SMARTCARD_STATE_READY;
3159 
3160         /* Clear RxISR function pointer */
3161         hsmartcard->RxISR = NULL;
3162 
3163 #if (USE_HAL_SMARTCARD_REGISTER_CALLBACKS == 1)
3164         /* Call registered Rx complete callback */
3165         hsmartcard->RxCpltCallback(hsmartcard);
3166 #else
3167         /* Call legacy weak Rx complete callback */
3168         HAL_SMARTCARD_RxCpltCallback(hsmartcard);
3169 #endif /* USE_HAL_SMARTCARD_REGISTER_CALLBACK */
3170       }
3171     }
3172 
3173     /* When remaining number of bytes to receive is less than the RX FIFO
3174     threshold, next incoming frames are processed as if FIFO mode was
3175     disabled (i.e. one interrupt per received frame).
3176     */
3177     rxdatacount = hsmartcard->RxXferCount;
3178     if (((rxdatacount != 0U)) && (rxdatacount < hsmartcard->NbRxDataToProcess))
3179     {
3180       /* Disable the UART RXFT interrupt*/
3181       CLEAR_BIT(hsmartcard->Instance->CR3, USART_CR3_RXFTIE);
3182 
3183       /* Update the RxISR function pointer */
3184       hsmartcard->RxISR = SMARTCARD_RxISR;
3185 
3186       /* Enable the UART Data Register Not Empty interrupt */
3187       SET_BIT(hsmartcard->Instance->CR1, USART_CR1_RXNEIE_RXFNEIE);
3188     }
3189   }
3190   else
3191   {
3192     /* Clear RXNE interrupt flag */
3193     __HAL_SMARTCARD_SEND_REQ(hsmartcard, SMARTCARD_RXDATA_FLUSH_REQUEST);
3194   }
3195 }
3196 
3197 /**
3198   * @}
3199   */
3200 
3201 #endif /* HAL_SMARTCARD_MODULE_ENABLED */
3202 /**
3203   * @}
3204   */
3205 
3206 /**
3207   * @}
3208   */
3209