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