STM32F479xx HAL User Manual
stm32f4xx_hal_smartcard.c
Go to the documentation of this file.
00001 /**
00002   ******************************************************************************
00003   * @file    stm32f4xx_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.
00021     (#) Initialize the SMARTCARD low level resources by implementing the HAL_SMARTCARD_MspInit() API:
00022         (##) Enable the interface clock of the USARTx associated to the SMARTCARD.
00023         (##) SMARTCARD pins configuration:
00024             (+++) Enable the clock for the SMARTCARD GPIOs.
00025             (+++) Configure SMARTCARD pins as alternate function pull-up.
00026         (##) NVIC configuration if you need to use interrupt process (HAL_SMARTCARD_Transmit_IT()
00027              and HAL_SMARTCARD_Receive_IT() APIs):
00028             (+++) Configure the USARTx interrupt priority.
00029             (+++) Enable the NVIC USART IRQ handle.
00030         (##) DMA Configuration if you need to use DMA process (HAL_SMARTCARD_Transmit_DMA()
00031              and HAL_SMARTCARD_Receive_DMA() APIs):
00032             (+++) Declare a DMA handle structure for the Tx/Rx stream.
00033             (+++) Enable the DMAx interface clock.
00034             (+++) Configure the declared DMA handle structure with the required Tx/Rx parameters.
00035             (+++) Configure the DMA Tx/Rx stream.
00036             (+++) Associate the initialized DMA handle to the SMARTCARD DMA Tx/Rx handle.
00037             (+++) Configure the priority and enable the NVIC for the transfer complete interrupt on the DMA Tx/Rx stream.
00038             (+++) Configure the USARTx interrupt priority and enable the NVIC USART IRQ handle
00039                   (used for last byte sending completion detection in DMA non circular mode)
00040 
00041     (#) Program the Baud Rate, Word Length , Stop Bit, Parity, Hardware
00042         flow control and Mode(Receiver/Transmitter) in the SMARTCARD Init structure.
00043 
00044     (#) Initialize the SMARTCARD registers by calling the HAL_SMARTCARD_Init() API:
00045         (++) These APIs configure also the low level Hardware GPIO, CLOCK, CORTEX...etc)
00046              by calling the customized HAL_SMARTCARD_MspInit() API.
00047     [..]
00048     (@) The specific SMARTCARD interrupts (Transmission complete interrupt,
00049         RXNE interrupt and Error Interrupts) will be managed using the macros
00050         __HAL_SMARTCARD_ENABLE_IT() and __HAL_SMARTCARD_DISABLE_IT() inside the transmit and receive process.
00051 
00052     [..]
00053     Three operation modes are available within this driver :
00054 
00055     *** Polling mode IO operation ***
00056     =================================
00057     [..]
00058       (+) Send an amount of data in blocking mode using HAL_SMARTCARD_Transmit()
00059       (+) Receive an amount of data in blocking mode using HAL_SMARTCARD_Receive()
00060 
00061     *** Interrupt mode IO operation ***
00062     ===================================
00063     [..]
00064       (+) Send an amount of data in non blocking mode using HAL_SMARTCARD_Transmit_IT()
00065       (+) At transmission end of transfer HAL_SMARTCARD_TxCpltCallback is executed and user can
00066           add his own code by customization of function pointer HAL_SMARTCARD_TxCpltCallback
00067       (+) Receive an amount of data in non blocking mode using HAL_SMARTCARD_Receive_IT()
00068       (+) At reception end of transfer HAL_SMARTCARD_RxCpltCallback is executed and user can
00069           add his own code by customization of function pointer HAL_SMARTCARD_RxCpltCallback
00070       (+) In case of transfer Error, HAL_SMARTCARD_ErrorCallback() function is executed and user can
00071           add his own code by customization of function pointer HAL_SMARTCARD_ErrorCallback
00072 
00073     *** DMA mode IO operation ***
00074     ==============================
00075     [..]
00076       (+) Send an amount of data in non blocking mode (DMA) using HAL_SMARTCARD_Transmit_DMA()
00077       (+) At transmission end of transfer HAL_SMARTCARD_TxCpltCallback is executed and user can
00078           add his own code by customization of function pointer HAL_SMARTCARD_TxCpltCallback
00079       (+) Receive an amount of data in non blocking mode (DMA) using HAL_SMARTCARD_Receive_DMA()
00080       (+) At reception end of transfer HAL_SMARTCARD_RxCpltCallback is executed and user can
00081           add his own code by customization of function pointer HAL_SMARTCARD_RxCpltCallback
00082       (+) In case of transfer Error, HAL_SMARTCARD_ErrorCallback() function is executed and user can
00083           add his own code by customization of function pointer HAL_SMARTCARD_ErrorCallback
00084 
00085     *** SMARTCARD HAL driver macros list ***
00086     ========================================
00087     [..]
00088       Below the list of most used macros in SMARTCARD HAL driver.
00089 
00090       (+) __HAL_SMARTCARD_ENABLE: Enable the SMARTCARD peripheral
00091       (+) __HAL_SMARTCARD_DISABLE: Disable the SMARTCARD peripheral
00092       (+) __HAL_SMARTCARD_GET_FLAG : Check whether the specified SMARTCARD flag is set or not
00093       (+) __HAL_SMARTCARD_CLEAR_FLAG : Clear the specified SMARTCARD pending flag
00094       (+) __HAL_SMARTCARD_ENABLE_IT: Enable the specified SMARTCARD interrupt
00095       (+) __HAL_SMARTCARD_DISABLE_IT: Disable the specified SMARTCARD interrupt
00096 
00097     [..]
00098       (@) You can refer to the SMARTCARD HAL driver header file for more useful macros
00099 
00100     ##### Callback registration #####
00101     ==================================
00102 
00103     [..]
00104     The compilation define USE_HAL_SMARTCARD_REGISTER_CALLBACKS when set to 1
00105     allows the user to configure dynamically the driver callbacks.
00106 
00107     [..]
00108     Use Function HAL_SMARTCARD_RegisterCallback() to register a user callback.
00109     Function HAL_SMARTCARD_RegisterCallback() allows to register following callbacks:
00110     (+) TxCpltCallback            : Tx Complete Callback.
00111     (+) RxCpltCallback            : Rx Complete Callback.
00112     (+) ErrorCallback             : Error Callback.
00113     (+) AbortCpltCallback         : Abort Complete Callback.
00114     (+) AbortTransmitCpltCallback : Abort Transmit Complete Callback.
00115     (+) AbortReceiveCpltCallback  : Abort Receive Complete Callback.
00116     (+) MspInitCallback           : SMARTCARD MspInit.
00117     (+) MspDeInitCallback         : SMARTCARD MspDeInit.
00118     This function takes as parameters the HAL peripheral handle, the Callback ID
00119     and a pointer to the user callback function.
00120 
00121     [..]
00122     Use function HAL_SMARTCARD_UnRegisterCallback() to reset a callback to the default
00123     weak (surcharged) function.
00124     HAL_SMARTCARD_UnRegisterCallback() takes as parameters the HAL peripheral handle,
00125     and the Callback ID.
00126     This function allows to reset following callbacks:
00127     (+) TxCpltCallback            : Tx Complete Callback.
00128     (+) RxCpltCallback            : Rx Complete Callback.
00129     (+) ErrorCallback             : Error Callback.
00130     (+) AbortCpltCallback         : Abort Complete Callback.
00131     (+) AbortTransmitCpltCallback : Abort Transmit Complete Callback.
00132     (+) AbortReceiveCpltCallback  : Abort Receive Complete Callback.
00133     (+) MspInitCallback           : SMARTCARD MspInit.
00134     (+) MspDeInitCallback         : SMARTCARD MspDeInit.
00135 
00136     [..]
00137     By default, after the HAL_SMARTCARD_Init() and when the state is HAL_SMARTCARD_STATE_RESET
00138     all callbacks are set to the corresponding weak (surcharged) functions:
00139     examples HAL_SMARTCARD_TxCpltCallback(), HAL_SMARTCARD_RxCpltCallback().
00140     Exception done for MspInit and MspDeInit functions that are respectively
00141     reset to the legacy weak (surcharged) functions in the HAL_SMARTCARD_Init()
00142     and HAL_SMARTCARD_DeInit() only when these callbacks are null (not registered beforehand).
00143     If not, MspInit or MspDeInit are not null, the HAL_SMARTCARD_Init() and HAL_SMARTCARD_DeInit()
00144     keep and use the user MspInit/MspDeInit callbacks (registered beforehand).
00145 
00146     [..]
00147     Callbacks can be registered/unregistered in HAL_SMARTCARD_STATE_READY state only.
00148     Exception done MspInit/MspDeInit that can be registered/unregistered
00149     in HAL_SMARTCARD_STATE_READY or HAL_SMARTCARD_STATE_RESET state, thus registered (user)
00150     MspInit/DeInit callbacks can be used during the Init/DeInit.
00151     In that case first register the MspInit/MspDeInit user callbacks
00152     using HAL_SMARTCARD_RegisterCallback() before calling HAL_SMARTCARD_DeInit()
00153     or HAL_SMARTCARD_Init() function.
00154 
00155     [..]
00156     When The compilation define USE_HAL_SMARTCARD_REGISTER_CALLBACKS is set to 0 or
00157     not defined, the callback registration feature is not available
00158     and weak (surcharged) callbacks are used.
00159 
00160   @endverbatim
00161   ******************************************************************************
00162   * @attention
00163   *
00164   * <h2><center>&copy; Copyright (c) 2016 STMicroelectronics.
00165   * All rights reserved.</center></h2>
00166   *
00167   * This software component is licensed by ST under BSD 3-Clause license,
00168   * the "License"; You may not use this file except in compliance with the
00169   * License. You may obtain a copy of the License at:
00170   *                        opensource.org/licenses/BSD-3-Clause
00171   *
00172   ******************************************************************************
00173   */
00174 
00175 /* Includes ------------------------------------------------------------------*/
00176 #include "stm32f4xx_hal.h"
00177 
00178 /** @addtogroup STM32F4xx_HAL_Driver
00179   * @{
00180   */
00181 
00182 /** @defgroup SMARTCARD SMARTCARD
00183   * @brief HAL SMARTCARD module driver
00184   * @{
00185   */
00186 #ifdef HAL_SMARTCARD_MODULE_ENABLED
00187 /* Private typedef -----------------------------------------------------------*/
00188 /* Private define ------------------------------------------------------------*/
00189 /** @addtogroup SMARTCARD_Private_Constants
00190   * @{
00191   */
00192 /**
00193   * @}
00194   */
00195 
00196 /* Private macro -------------------------------------------------------------*/
00197 /* Private variables ---------------------------------------------------------*/
00198 /* Private function prototypes -----------------------------------------------*/
00199 /** @addtogroup SMARTCARD_Private_Functions
00200   * @{
00201   */
00202 #if (USE_HAL_SMARTCARD_REGISTER_CALLBACKS == 1)
00203 void SMARTCARD_InitCallbacksToDefault(SMARTCARD_HandleTypeDef *hsc);
00204 #endif /* USE_HAL_SMARTCARD_REGISTER_CALLBACKS */
00205 static void SMARTCARD_EndTxTransfer(SMARTCARD_HandleTypeDef *hsc);
00206 static void SMARTCARD_EndRxTransfer(SMARTCARD_HandleTypeDef *hsc);
00207 static void SMARTCARD_SetConfig (SMARTCARD_HandleTypeDef *hsc);
00208 static HAL_StatusTypeDef SMARTCARD_Transmit_IT(SMARTCARD_HandleTypeDef *hsc);
00209 static HAL_StatusTypeDef SMARTCARD_EndTransmit_IT(SMARTCARD_HandleTypeDef *hsc);
00210 static HAL_StatusTypeDef SMARTCARD_Receive_IT(SMARTCARD_HandleTypeDef *hsc);
00211 static void SMARTCARD_DMATransmitCplt(DMA_HandleTypeDef *hdma);
00212 static void SMARTCARD_DMAReceiveCplt(DMA_HandleTypeDef *hdma);
00213 static void SMARTCARD_DMAError(DMA_HandleTypeDef *hdma);
00214 static void SMARTCARD_DMAAbortOnError(DMA_HandleTypeDef *hdma);
00215 static void SMARTCARD_DMATxAbortCallback(DMA_HandleTypeDef *hdma);
00216 static void SMARTCARD_DMARxAbortCallback(DMA_HandleTypeDef *hdma);
00217 static void SMARTCARD_DMATxOnlyAbortCallback(DMA_HandleTypeDef *hdma);
00218 static void SMARTCARD_DMARxOnlyAbortCallback(DMA_HandleTypeDef *hdma);
00219 static HAL_StatusTypeDef SMARTCARD_WaitOnFlagUntilTimeout(SMARTCARD_HandleTypeDef *hsc, uint32_t Flag, FlagStatus Status, uint32_t Tickstart, uint32_t Timeout);
00220 /**
00221   * @}
00222   */
00223 
00224 /* Exported functions --------------------------------------------------------*/
00225 /** @defgroup SMARTCARD_Exported_Functions SMARTCARD Exported Functions
00226   * @{
00227   */
00228 
00229 /** @defgroup SMARTCARD_Exported_Functions_Group1 SmartCard Initialization and de-initialization functions
00230   *  @brief    Initialization and Configuration functions
00231   *
00232 @verbatim
00233   ==============================================================================
00234               ##### Initialization and Configuration functions #####
00235   ==============================================================================
00236   [..]
00237   This subsection provides a set of functions allowing to initialize the USART
00238   in Smartcard mode.
00239   [..]
00240   The Smartcard interface is designed to support asynchronous protocol Smartcards as
00241   defined in the ISO 7816-3 standard.
00242   [..]
00243   The USART can provide a clock to the smartcard through the SCLK output.
00244   In smartcard mode, SCLK is not associated to the communication but is simply derived
00245   from the internal peripheral input clock through a 5-bit prescaler.
00246   [..]
00247   (+) For the Smartcard mode only these parameters can be configured:
00248       (++) Baud Rate
00249       (++) Word Length => Should be 9 bits (8 bits + parity)
00250       (++) Stop Bit
00251       (++) Parity: => Should be enabled
00252       (++) USART polarity
00253       (++) USART phase
00254       (++) USART LastBit
00255       (++) Receiver/transmitter modes
00256       (++) Prescaler
00257       (++) GuardTime
00258       (++) NACKState: The Smartcard NACK state
00259 
00260      (+) Recommended SmartCard interface configuration to get the Answer to Reset from the Card:
00261         (++) Word Length = 9 Bits
00262         (++) 1.5 Stop Bit
00263         (++) Even parity
00264         (++) BaudRate = 12096 baud
00265         (++) Tx and Rx enabled
00266   [..]
00267   Please refer to the ISO 7816-3 specification for more details.
00268 
00269   [..]
00270    (@) It is also possible to choose 0.5 stop bit for receiving but it is recommended
00271        to use 1.5 stop bits for both transmitting and receiving to avoid switching
00272        between the two configurations.
00273   [..]
00274     The HAL_SMARTCARD_Init() function follows the USART  SmartCard configuration
00275     procedures (details for the procedures are available in reference manual
00276     (RM0430 for STM32F4X3xx MCUs and RM0402 for STM32F412xx MCUs
00277      RM0383 for STM32F411xC/E MCUs and RM0401 for STM32F410xx MCUs
00278      RM0090 for STM32F4X5xx/STM32F4X7xx/STM32F429xx/STM32F439xx MCUs
00279      RM0390 for STM32F446xx MCUs and RM0386 for STM32F469xx/STM32F479xx MCUs)).
00280 
00281 @endverbatim
00282 
00283   The SMARTCARD frame format is given in the following table:
00284        +-------------------------------------------------------------+
00285        |   M bit |  PCE bit  |        SMARTCARD frame                |
00286        |---------------------|---------------------------------------|
00287        |    1    |    1      |    | SB | 8 bit data | PB | STB |     |
00288        +-------------------------------------------------------------+
00289   * @{
00290   */
00291 
00292 /**
00293   * @brief  Initializes the SmartCard mode according to the specified
00294   *         parameters in the SMARTCARD_InitTypeDef and create the associated handle.
00295   * @param  hsc    Pointer to a SMARTCARD_HandleTypeDef structure that contains
00296   *                the configuration information for SMARTCARD module.
00297   * @retval HAL status
00298   */
00299 HAL_StatusTypeDef HAL_SMARTCARD_Init(SMARTCARD_HandleTypeDef *hsc)
00300 {
00301   /* Check the SMARTCARD handle allocation */
00302   if(hsc == NULL)
00303   {
00304     return HAL_ERROR;
00305   }
00306 
00307   /* Check the parameters */
00308   assert_param(IS_SMARTCARD_INSTANCE(hsc->Instance));
00309   assert_param(IS_SMARTCARD_NACK_STATE(hsc->Init.NACKState));
00310 
00311   if(hsc->gState == HAL_SMARTCARD_STATE_RESET)
00312   {
00313     /* Allocate lock resource and initialize it */
00314     hsc->Lock = HAL_UNLOCKED;
00315 
00316 #if USE_HAL_SMARTCARD_REGISTER_CALLBACKS == 1
00317     SMARTCARD_InitCallbacksToDefault(hsc);
00318 
00319     if (hsc->MspInitCallback == NULL)
00320     {
00321       hsc->MspInitCallback = HAL_SMARTCARD_MspInit;
00322     }
00323 
00324     /* Init the low level hardware */
00325     hsc->MspInitCallback(hsc);
00326 #else
00327     /* Init the low level hardware : GPIO, CLOCK */
00328     HAL_SMARTCARD_MspInit(hsc);
00329 #endif /* USE_HAL_SMARTCARD_REGISTER_CALLBACKS */
00330   }
00331 
00332   hsc->gState = HAL_SMARTCARD_STATE_BUSY;
00333 
00334   /* Set the Prescaler */
00335   MODIFY_REG(hsc->Instance->GTPR, USART_GTPR_PSC, hsc->Init.Prescaler);
00336 
00337   /* Set the Guard Time */
00338   MODIFY_REG(hsc->Instance->GTPR, USART_GTPR_GT, ((hsc->Init.GuardTime)<<8U));
00339 
00340   /* Set the Smartcard Communication parameters */
00341   SMARTCARD_SetConfig(hsc);
00342 
00343   /* In SmartCard mode, the following bits must be kept cleared:
00344   - LINEN bit in the USART_CR2 register
00345   - HDSEL and IREN bits in the USART_CR3 register.*/
00346   CLEAR_BIT(hsc->Instance->CR2, USART_CR2_LINEN);
00347   CLEAR_BIT(hsc->Instance->CR3, (USART_CR3_IREN | USART_CR3_HDSEL));
00348 
00349   /* Enable the SMARTCARD Parity Error Interrupt */
00350   SET_BIT(hsc->Instance->CR1, USART_CR1_PEIE);
00351 
00352   /* Enable the SMARTCARD Framing Error Interrupt */
00353   SET_BIT(hsc->Instance->CR3, USART_CR3_EIE);
00354 
00355   /* Enable the Peripheral */
00356   __HAL_SMARTCARD_ENABLE(hsc);
00357 
00358   /* Configure the Smartcard NACK state */
00359   MODIFY_REG(hsc->Instance->CR3, USART_CR3_NACK, hsc->Init.NACKState);
00360 
00361   /* Enable the SC mode by setting the SCEN bit in the CR3 register */
00362   hsc->Instance->CR3 |= (USART_CR3_SCEN);
00363 
00364   /* Initialize the SMARTCARD state*/
00365   hsc->ErrorCode = HAL_SMARTCARD_ERROR_NONE;
00366   hsc->gState= HAL_SMARTCARD_STATE_READY;
00367   hsc->RxState= HAL_SMARTCARD_STATE_READY;
00368 
00369   return HAL_OK;
00370 }
00371 
00372 /**
00373   * @brief DeInitializes the USART SmartCard peripheral
00374   * @param  hsc    Pointer to a SMARTCARD_HandleTypeDef structure that contains
00375   *                the configuration information for SMARTCARD module.
00376   * @retval HAL status
00377   */
00378 HAL_StatusTypeDef HAL_SMARTCARD_DeInit(SMARTCARD_HandleTypeDef *hsc)
00379 {
00380   /* Check the SMARTCARD handle allocation */
00381   if(hsc == NULL)
00382   {
00383     return HAL_ERROR;
00384   }
00385 
00386   /* Check the parameters */
00387   assert_param(IS_SMARTCARD_INSTANCE(hsc->Instance));
00388 
00389   hsc->gState = HAL_SMARTCARD_STATE_BUSY;
00390 
00391   /* Disable the Peripheral */
00392   __HAL_SMARTCARD_DISABLE(hsc);
00393 
00394   /* DeInit the low level hardware */
00395 #if USE_HAL_SMARTCARD_REGISTER_CALLBACKS == 1
00396   if (hsc->MspDeInitCallback == NULL)
00397   {
00398     hsc->MspDeInitCallback = HAL_SMARTCARD_MspDeInit;
00399   }
00400   /* DeInit the low level hardware */
00401   hsc->MspDeInitCallback(hsc);
00402 #else
00403   HAL_SMARTCARD_MspDeInit(hsc);
00404 #endif /* USE_HAL_SMARTCARD_REGISTER_CALLBACKS */
00405 
00406   hsc->ErrorCode = HAL_SMARTCARD_ERROR_NONE;
00407   hsc->gState = HAL_SMARTCARD_STATE_RESET;
00408   hsc->RxState = HAL_SMARTCARD_STATE_RESET;
00409 
00410   /* Release Lock */
00411   __HAL_UNLOCK(hsc);
00412 
00413   return HAL_OK;
00414 }
00415 
00416 /**
00417   * @brief  SMARTCARD MSP Init
00418   * @param  hsc    Pointer to a SMARTCARD_HandleTypeDef structure that contains
00419   *                the configuration information for SMARTCARD module.
00420   * @retval None
00421   */
00422 __weak void HAL_SMARTCARD_MspInit(SMARTCARD_HandleTypeDef *hsc)
00423 {
00424   /* Prevent unused argument(s) compilation warning */
00425   UNUSED(hsc);
00426 
00427   /* NOTE : This function should not be modified, when the callback is needed,
00428             the HAL_SMARTCARD_MspInit can be implemented in the user file
00429    */
00430 }
00431 
00432 /**
00433   * @brief SMARTCARD MSP DeInit
00434   * @param  hsc    Pointer to a SMARTCARD_HandleTypeDef structure that contains
00435   *                the configuration information for SMARTCARD module.
00436   * @retval None
00437   */
00438 __weak void HAL_SMARTCARD_MspDeInit(SMARTCARD_HandleTypeDef *hsc)
00439 {
00440   /* Prevent unused argument(s) compilation warning */
00441   UNUSED(hsc);
00442 
00443   /* NOTE : This function should not be modified, when the callback is needed,
00444             the HAL_SMARTCARD_MspDeInit can be implemented in the user file
00445    */
00446 }
00447 
00448 #if (USE_HAL_SMARTCARD_REGISTER_CALLBACKS == 1)
00449 /**
00450   * @brief  Register a User SMARTCARD Callback
00451   *         To be used instead of the weak predefined callback
00452   * @param  hsc smartcard handle
00453   * @param  CallbackID ID of the callback to be registered
00454   *         This parameter can be one of the following values:
00455   *           @arg @ref HAL_SMARTCARD_TX_COMPLETE_CB_ID Tx Complete Callback ID
00456   *           @arg @ref HAL_SMARTCARD_RX_COMPLETE_CB_ID Rx Complete Callback ID
00457   *           @arg @ref HAL_SMARTCARD_ERROR_CB_ID Error Callback ID
00458   *           @arg @ref HAL_SMARTCARD_ABORT_COMPLETE_CB_ID Abort Complete Callback ID
00459   *           @arg @ref HAL_SMARTCARD_ABORT_TRANSMIT_COMPLETE_CB_ID Abort Transmit Complete Callback ID
00460   *           @arg @ref HAL_SMARTCARD_ABORT_RECEIVE_COMPLETE_CB_ID Abort Receive Complete Callback ID
00461   *           @arg @ref HAL_SMARTCARD_MSPINIT_CB_ID MspInit Callback ID
00462   *           @arg @ref HAL_SMARTCARD_MSPDEINIT_CB_ID MspDeInit Callback ID
00463   * @param  pCallback pointer to the Callback function
00464   * @retval HAL status
00465   */
00466 HAL_StatusTypeDef HAL_SMARTCARD_RegisterCallback(SMARTCARD_HandleTypeDef *hsc, HAL_SMARTCARD_CallbackIDTypeDef CallbackID, pSMARTCARD_CallbackTypeDef pCallback)
00467 {
00468   HAL_StatusTypeDef status = HAL_OK;
00469 
00470   if (pCallback == NULL)
00471   {
00472     /* Update the error code */
00473     hsc->ErrorCode |= HAL_SMARTCARD_ERROR_INVALID_CALLBACK;
00474 
00475     return HAL_ERROR;
00476   }
00477   /* Process locked */
00478   __HAL_LOCK(hsc);
00479 
00480   if (hsc->gState == HAL_SMARTCARD_STATE_READY)
00481   {
00482     switch (CallbackID)
00483     {
00484 
00485       case HAL_SMARTCARD_TX_COMPLETE_CB_ID :
00486         hsc->TxCpltCallback = pCallback;
00487         break;
00488 
00489       case HAL_SMARTCARD_RX_COMPLETE_CB_ID :
00490         hsc->RxCpltCallback = pCallback;
00491         break;
00492 
00493       case HAL_SMARTCARD_ERROR_CB_ID :
00494         hsc->ErrorCallback = pCallback;
00495         break;
00496 
00497       case HAL_SMARTCARD_ABORT_COMPLETE_CB_ID :
00498         hsc->AbortCpltCallback = pCallback;
00499         break;
00500 
00501       case HAL_SMARTCARD_ABORT_TRANSMIT_COMPLETE_CB_ID :
00502         hsc->AbortTransmitCpltCallback = pCallback;
00503         break;
00504 
00505       case HAL_SMARTCARD_ABORT_RECEIVE_COMPLETE_CB_ID :
00506         hsc->AbortReceiveCpltCallback = pCallback;
00507         break;
00508 
00509 
00510       case HAL_SMARTCARD_MSPINIT_CB_ID :
00511         hsc->MspInitCallback = pCallback;
00512         break;
00513 
00514       case HAL_SMARTCARD_MSPDEINIT_CB_ID :
00515         hsc->MspDeInitCallback = pCallback;
00516         break;
00517 
00518       default :
00519         /* Update the error code */
00520         hsc->ErrorCode |= HAL_SMARTCARD_ERROR_INVALID_CALLBACK;
00521 
00522         /* Return error status */
00523         status =  HAL_ERROR;
00524         break;
00525     }
00526   }
00527   else if (hsc->gState == HAL_SMARTCARD_STATE_RESET)
00528   {
00529     switch (CallbackID)
00530     {
00531       case HAL_SMARTCARD_MSPINIT_CB_ID :
00532         hsc->MspInitCallback = pCallback;
00533         break;
00534 
00535       case HAL_SMARTCARD_MSPDEINIT_CB_ID :
00536         hsc->MspDeInitCallback = pCallback;
00537         break;
00538 
00539       default :
00540         /* Update the error code */
00541         hsc->ErrorCode |= HAL_SMARTCARD_ERROR_INVALID_CALLBACK;
00542 
00543         /* Return error status */
00544         status =  HAL_ERROR;
00545         break;
00546     }
00547   }
00548   else
00549   {
00550     /* Update the error code */
00551     hsc->ErrorCode |= HAL_SMARTCARD_ERROR_INVALID_CALLBACK;
00552 
00553     /* Return error status */
00554     status =  HAL_ERROR;
00555   }
00556 
00557   /* Release Lock */
00558   __HAL_UNLOCK(hsc);
00559 
00560   return status;
00561 }
00562 
00563 /**
00564   * @brief  Unregister an SMARTCARD callback
00565   *         SMARTCARD callback is redirected to the weak predefined callback
00566   * @param  hsc smartcard handle
00567   * @param  CallbackID ID of the callback to be unregistered
00568   *         This parameter can be one of the following values:
00569   *           @arg @ref HAL_SMARTCARD_TX_COMPLETE_CB_ID Tx Complete Callback ID
00570   *           @arg @ref HAL_SMARTCARD_RX_COMPLETE_CB_ID Rx Complete Callback ID
00571   *           @arg @ref HAL_SMARTCARD_ERROR_CB_ID Error Callback ID
00572   *           @arg @ref HAL_SMARTCARD_ABORT_COMPLETE_CB_ID Abort Complete Callback ID
00573   *           @arg @ref HAL_SMARTCARD_ABORT_TRANSMIT_COMPLETE_CB_ID Abort Transmit Complete Callback ID
00574   *           @arg @ref HAL_SMARTCARD_ABORT_RECEIVE_COMPLETE_CB_ID Abort Receive Complete Callback ID
00575   *           @arg @ref HAL_SMARTCARD_MSPINIT_CB_ID MspInit Callback ID
00576   *           @arg @ref HAL_SMARTCARD_MSPDEINIT_CB_ID MspDeInit Callback ID
00577   * @retval HAL status
00578   */
00579 HAL_StatusTypeDef HAL_SMARTCARD_UnRegisterCallback(SMARTCARD_HandleTypeDef *hsc, HAL_SMARTCARD_CallbackIDTypeDef CallbackID)
00580 {
00581   HAL_StatusTypeDef status = HAL_OK;
00582 
00583   /* Process locked */
00584   __HAL_LOCK(hsc);
00585 
00586   if (HAL_SMARTCARD_STATE_READY == hsc->gState)
00587   {
00588     switch (CallbackID)
00589     {
00590       case HAL_SMARTCARD_TX_COMPLETE_CB_ID :
00591         hsc->TxCpltCallback = HAL_SMARTCARD_TxCpltCallback;                       /* Legacy weak TxCpltCallback            */
00592         break;
00593 
00594       case HAL_SMARTCARD_RX_COMPLETE_CB_ID :
00595         hsc->RxCpltCallback = HAL_SMARTCARD_RxCpltCallback;                       /* Legacy weak RxCpltCallback            */
00596         break;
00597 
00598       case HAL_SMARTCARD_ERROR_CB_ID :
00599         hsc->ErrorCallback = HAL_SMARTCARD_ErrorCallback;                         /* Legacy weak ErrorCallback             */
00600         break;
00601 
00602       case HAL_SMARTCARD_ABORT_COMPLETE_CB_ID :
00603         hsc->AbortCpltCallback = HAL_SMARTCARD_AbortCpltCallback;                 /* Legacy weak AbortCpltCallback         */
00604         break;
00605 
00606       case HAL_SMARTCARD_ABORT_TRANSMIT_COMPLETE_CB_ID :
00607         hsc->AbortTransmitCpltCallback = HAL_SMARTCARD_AbortTransmitCpltCallback; /* Legacy weak AbortTransmitCpltCallback */
00608         break;
00609 
00610       case HAL_SMARTCARD_ABORT_RECEIVE_COMPLETE_CB_ID :
00611         hsc->AbortReceiveCpltCallback = HAL_SMARTCARD_AbortReceiveCpltCallback;   /* Legacy weak AbortReceiveCpltCallback  */
00612         break;
00613 
00614 
00615       case HAL_SMARTCARD_MSPINIT_CB_ID :
00616         hsc->MspInitCallback = HAL_SMARTCARD_MspInit;                             /* Legacy weak MspInitCallback           */
00617         break;
00618 
00619       case HAL_SMARTCARD_MSPDEINIT_CB_ID :
00620         hsc->MspDeInitCallback = HAL_SMARTCARD_MspDeInit;                         /* Legacy weak MspDeInitCallback         */
00621         break;
00622 
00623       default :
00624         /* Update the error code */
00625         hsc->ErrorCode |= HAL_SMARTCARD_ERROR_INVALID_CALLBACK;
00626 
00627         /* Return error status */
00628         status =  HAL_ERROR;
00629         break;
00630     }
00631   }
00632   else if (HAL_SMARTCARD_STATE_RESET == hsc->gState)
00633   {
00634     switch (CallbackID)
00635     {
00636       case HAL_SMARTCARD_MSPINIT_CB_ID :
00637         hsc->MspInitCallback = HAL_SMARTCARD_MspInit;
00638         break;
00639 
00640       case HAL_SMARTCARD_MSPDEINIT_CB_ID :
00641         hsc->MspDeInitCallback = HAL_SMARTCARD_MspDeInit;
00642         break;
00643 
00644       default :
00645         /* Update the error code */
00646         hsc->ErrorCode |= HAL_SMARTCARD_ERROR_INVALID_CALLBACK;
00647 
00648         /* Return error status */
00649         status =  HAL_ERROR;
00650         break;
00651     }
00652   }
00653   else
00654   {
00655     /* Update the error code */
00656     hsc->ErrorCode |= HAL_SMARTCARD_ERROR_INVALID_CALLBACK;
00657 
00658     /* Return error status */
00659     status =  HAL_ERROR;
00660   }
00661 
00662   /* Release Lock */
00663   __HAL_UNLOCK(hsc);
00664 
00665   return status;
00666 }
00667 #endif /* USE_HAL_SMARTCARD_REGISTER_CALLBACKS */
00668 
00669 /**
00670   * @}
00671   */
00672 
00673 /** @defgroup SMARTCARD_Exported_Functions_Group2 IO operation functions
00674   * @brief    SMARTCARD Transmit and Receive functions
00675   *
00676 @verbatim
00677  ===============================================================================
00678                       ##### IO operation functions #####
00679  ===============================================================================
00680  [..]
00681    This subsection provides a set of functions allowing to manage the SMARTCARD data transfers.
00682 
00683  [..]
00684     (#) Smartcard is a single wire half duplex communication protocol.
00685     The Smartcard interface is designed to support asynchronous protocol Smartcards as
00686     defined in the ISO 7816-3 standard.
00687     (#) The USART should be configured as:
00688        (++) 8 bits plus parity: where M=1 and PCE=1 in the USART_CR1 register
00689        (++) 1.5 stop bits when transmitting and receiving: where STOP=11 in the USART_CR2 register.
00690 
00691     (#) There are two modes of transfer:
00692        (++) Blocking mode: The communication is performed in polling mode.
00693             The HAL status of all data processing is returned by the same function
00694             after finishing transfer.
00695        (++) Non Blocking mode: The communication is performed using Interrupts
00696            or DMA, These APIs return the HAL status.
00697            The end of the data processing will be indicated through the
00698            dedicated SMARTCARD IRQ when using Interrupt mode or the DMA IRQ when
00699            using DMA mode.
00700            The HAL_SMARTCARD_TxCpltCallback(), HAL_SMARTCARD_RxCpltCallback() user callbacks
00701            will be executed respectively at the end of the Transmit or Receive process
00702            The HAL_SMARTCARD_ErrorCallback() user callback will be executed when a communication error is detected
00703 
00704     (#) Blocking mode APIs are :
00705         (++) HAL_SMARTCARD_Transmit()
00706         (++) HAL_SMARTCARD_Receive()
00707 
00708     (#) Non Blocking mode APIs with Interrupt are :
00709         (++) HAL_SMARTCARD_Transmit_IT()
00710         (++) HAL_SMARTCARD_Receive_IT()
00711         (++) HAL_SMARTCARD_IRQHandler()
00712 
00713     (#) Non Blocking mode functions with DMA are :
00714         (++) HAL_SMARTCARD_Transmit_DMA()
00715         (++) HAL_SMARTCARD_Receive_DMA()
00716 
00717     (#) A set of Transfer Complete Callbacks are provided in non Blocking mode:
00718         (++) HAL_SMARTCARD_TxCpltCallback()
00719         (++) HAL_SMARTCARD_RxCpltCallback()
00720         (++) HAL_SMARTCARD_ErrorCallback()
00721 
00722     (#) Non-Blocking mode transfers could be aborted using Abort API's :
00723         (+) HAL_SMARTCARD_Abort()
00724         (+) HAL_SMARTCARD_AbortTransmit()
00725         (+) HAL_SMARTCARD_AbortReceive()
00726         (+) HAL_SMARTCARD_Abort_IT()
00727         (+) HAL_SMARTCARD_AbortTransmit_IT()
00728         (+) HAL_SMARTCARD_AbortReceive_IT()
00729 
00730     (#) For Abort services based on interrupts (HAL_SMARTCARD_Abortxxx_IT), a set of Abort Complete Callbacks are provided:
00731         (+) HAL_SMARTCARD_AbortCpltCallback()
00732         (+) HAL_SMARTCARD_AbortTransmitCpltCallback()
00733         (+) HAL_SMARTCARD_AbortReceiveCpltCallback()
00734 
00735     (#) In Non-Blocking mode transfers, possible errors are split into 2 categories.
00736         Errors are handled as follows :
00737        (+) Error is considered as Recoverable and non blocking : Transfer could go till end, but error severity is
00738            to be evaluated by user : this concerns Frame Error, Parity Error or Noise Error in Interrupt mode reception .
00739            Received character is then retrieved and stored in Rx buffer, Error code is set to allow user to identify error type,
00740            and HAL_SMARTCARD_ErrorCallback() user callback is executed. Transfer is kept ongoing on SMARTCARD side.
00741            If user wants to abort it, Abort services should be called by user.
00742        (+) Error is considered as Blocking : Transfer could not be completed properly and is aborted.
00743            This concerns Frame Error in Interrupt mode transmission, Overrun Error in Interrupt mode reception and all errors in DMA mode.
00744            Error code is set to allow user to identify error type, and HAL_SMARTCARD_ErrorCallback() user callback is executed.
00745 
00746 @endverbatim
00747   * @{
00748   */
00749 
00750 /**
00751   * @brief Send an amount of data in blocking mode
00752   * @param  hsc    Pointer to a SMARTCARD_HandleTypeDef structure that contains
00753   *                the configuration information for SMARTCARD module.
00754   * @param  pData  Pointer to data buffer
00755   * @param  Size   Amount of data to be sent
00756   * @param  Timeout Timeout duration
00757   * @retval HAL status
00758   */
00759 HAL_StatusTypeDef HAL_SMARTCARD_Transmit(SMARTCARD_HandleTypeDef *hsc, uint8_t *pData, uint16_t Size, uint32_t Timeout)
00760 {
00761   uint8_t *tmp = pData;
00762   uint32_t tickstart = 0U;
00763 
00764   if(hsc->gState == HAL_SMARTCARD_STATE_READY)
00765   {
00766     if((pData == NULL) || (Size == 0U))
00767     {
00768       return  HAL_ERROR;
00769     }
00770 
00771     /* Process Locked */
00772     __HAL_LOCK(hsc);
00773 
00774     hsc->ErrorCode = HAL_SMARTCARD_ERROR_NONE;
00775     hsc->gState = HAL_SMARTCARD_STATE_BUSY_TX;
00776 
00777     /* Init tickstart for timeout management */
00778     tickstart = HAL_GetTick();
00779 
00780     hsc->TxXferSize = Size;
00781     hsc->TxXferCount = Size;
00782     while(hsc->TxXferCount > 0U)
00783     {
00784       hsc->TxXferCount--;
00785       if(SMARTCARD_WaitOnFlagUntilTimeout(hsc, SMARTCARD_FLAG_TXE, RESET, tickstart, Timeout) != HAL_OK)
00786       {
00787         return HAL_TIMEOUT;
00788       }
00789       hsc->Instance->DR = (uint8_t)(*tmp & 0xFFU);
00790       tmp++;
00791     }
00792 
00793     if(SMARTCARD_WaitOnFlagUntilTimeout(hsc, SMARTCARD_FLAG_TC, RESET, tickstart, Timeout) != HAL_OK)
00794     {
00795       return HAL_TIMEOUT;
00796     }
00797 
00798         /* At end of Tx process, restore hsc->gState to Ready */
00799     hsc->gState = HAL_SMARTCARD_STATE_READY;
00800 
00801     /* Process Unlocked */
00802     __HAL_UNLOCK(hsc);
00803 
00804     return HAL_OK;
00805   }
00806   else
00807   {
00808     return HAL_BUSY;
00809   }
00810 }
00811 
00812 /**
00813   * @brief Receive an amount of data in blocking mode
00814   * @param  hsc    Pointer to a SMARTCARD_HandleTypeDef structure that contains
00815   *                the configuration information for SMARTCARD module.
00816   * @param  pData  Pointer to data buffer
00817   * @param  Size   Amount of data to be received
00818   * @param  Timeout Timeout duration
00819   * @retval HAL status
00820   */
00821 HAL_StatusTypeDef HAL_SMARTCARD_Receive(SMARTCARD_HandleTypeDef *hsc, uint8_t *pData, uint16_t Size, uint32_t Timeout)
00822 {
00823   uint8_t  *tmp = pData;
00824   uint32_t tickstart = 0U;
00825 
00826   if(hsc->RxState == HAL_SMARTCARD_STATE_READY)
00827   {
00828     if((pData == NULL) || (Size == 0U))
00829     {
00830       return  HAL_ERROR;
00831     }
00832 
00833     /* Process Locked */
00834     __HAL_LOCK(hsc);
00835 
00836     hsc->ErrorCode = HAL_SMARTCARD_ERROR_NONE;
00837     hsc->RxState = HAL_SMARTCARD_STATE_BUSY_RX;
00838 
00839     /* Init tickstart for timeout management */
00840     tickstart = HAL_GetTick();
00841 
00842     hsc->RxXferSize = Size;
00843     hsc->RxXferCount = Size;
00844 
00845     /* Check the remain data to be received */
00846     while(hsc->RxXferCount > 0U)
00847     {
00848       hsc->RxXferCount--;
00849       if(SMARTCARD_WaitOnFlagUntilTimeout(hsc, SMARTCARD_FLAG_RXNE, RESET, tickstart, Timeout) != HAL_OK)
00850       {
00851         return HAL_TIMEOUT;
00852       }
00853       *tmp = (uint8_t)(hsc->Instance->DR & (uint8_t)0xFFU);
00854       tmp++;
00855     }
00856 
00857     /* At end of Rx process, restore hsc->RxState to Ready */
00858     hsc->RxState = HAL_SMARTCARD_STATE_READY;
00859 
00860     /* Process Unlocked */
00861     __HAL_UNLOCK(hsc);
00862 
00863     return HAL_OK;
00864   }
00865   else
00866   {
00867     return HAL_BUSY;
00868   }
00869 }
00870 
00871 /**
00872   * @brief Send an amount of data in non blocking mode
00873   * @param  hsc    Pointer to a SMARTCARD_HandleTypeDef structure that contains
00874   *                the configuration information for SMARTCARD module.
00875   * @param  pData  Pointer to data buffer
00876   * @param  Size   Amount of data to be sent
00877   * @retval HAL status
00878   */
00879 HAL_StatusTypeDef HAL_SMARTCARD_Transmit_IT(SMARTCARD_HandleTypeDef *hsc, uint8_t *pData, uint16_t Size)
00880 {
00881   /* Check that a Tx process is not already ongoing */
00882   if(hsc->gState == HAL_SMARTCARD_STATE_READY)
00883   {
00884     if((pData == NULL) || (Size == 0U))
00885     {
00886       return HAL_ERROR;
00887     }
00888 
00889     /* Process Locked */
00890     __HAL_LOCK(hsc);
00891 
00892     hsc->pTxBuffPtr = pData;
00893     hsc->TxXferSize = Size;
00894     hsc->TxXferCount = Size;
00895 
00896     hsc->ErrorCode = HAL_SMARTCARD_ERROR_NONE;
00897     hsc->gState = HAL_SMARTCARD_STATE_BUSY_TX;
00898 
00899     /* Process Unlocked */
00900     __HAL_UNLOCK(hsc);
00901 
00902     /* Enable the SMARTCARD Parity Error Interrupt */
00903     SET_BIT(hsc->Instance->CR1, USART_CR1_PEIE);
00904 
00905     /* Disable the SMARTCARD Error Interrupt: (Frame error, noise error, overrun error) */
00906     CLEAR_BIT(hsc->Instance->CR3, USART_CR3_EIE);
00907 
00908     /* Enable the SMARTCARD Transmit data register empty Interrupt */
00909     SET_BIT(hsc->Instance->CR1, USART_CR1_TXEIE);
00910 
00911     return HAL_OK;
00912   }
00913   else
00914   {
00915     return HAL_BUSY;
00916   }
00917 }
00918 
00919 /**
00920   * @brief Receive an amount of data in non blocking mode
00921   * @param  hsc    Pointer to a SMARTCARD_HandleTypeDef structure that contains
00922   *                the configuration information for SMARTCARD module.
00923   * @param  pData  Pointer to data buffer
00924   * @param  Size   Amount of data to be received
00925   * @retval HAL status
00926   */
00927 HAL_StatusTypeDef HAL_SMARTCARD_Receive_IT(SMARTCARD_HandleTypeDef *hsc, uint8_t *pData, uint16_t Size)
00928 {
00929   /* Check that a Rx process is not already ongoing */
00930   if(hsc->RxState == HAL_SMARTCARD_STATE_READY)
00931   {
00932     if((pData == NULL) || (Size == 0U))
00933     {
00934       return HAL_ERROR;
00935     }
00936 
00937     /* Process Locked */
00938     __HAL_LOCK(hsc);
00939 
00940     hsc->pRxBuffPtr = pData;
00941     hsc->RxXferSize = Size;
00942     hsc->RxXferCount = Size;
00943 
00944     hsc->ErrorCode = HAL_SMARTCARD_ERROR_NONE;
00945     hsc->RxState = HAL_SMARTCARD_STATE_BUSY_RX;
00946 
00947     /* Process Unlocked */
00948     __HAL_UNLOCK(hsc);
00949 
00950     /* Enable the SMARTCARD Parity Error and Data Register not empty Interrupts */
00951     SET_BIT(hsc->Instance->CR1, USART_CR1_PEIE| USART_CR1_RXNEIE);
00952 
00953     /* Enable the SMARTCARD Error Interrupt: (Frame error, noise error, overrun error) */
00954     SET_BIT(hsc->Instance->CR3, USART_CR3_EIE);
00955 
00956     return HAL_OK;
00957   }
00958   else
00959   {
00960     return HAL_BUSY;
00961   }
00962 }
00963 
00964 /**
00965   * @brief Send an amount of data in non blocking mode
00966   * @param  hsc    Pointer to a SMARTCARD_HandleTypeDef structure that contains
00967   *                the configuration information for SMARTCARD module.
00968   * @param  pData  Pointer to data buffer
00969   * @param  Size   Amount of data to be sent
00970   * @retval HAL status
00971   */
00972 HAL_StatusTypeDef HAL_SMARTCARD_Transmit_DMA(SMARTCARD_HandleTypeDef *hsc, uint8_t *pData, uint16_t Size)
00973 {
00974   uint32_t *tmp;
00975 
00976   /* Check that a Tx process is not already ongoing */
00977   if(hsc->gState == HAL_SMARTCARD_STATE_READY)
00978   {
00979     if((pData == NULL) || (Size == 0U))
00980     {
00981       return HAL_ERROR;
00982     }
00983 
00984     /* Process Locked */
00985     __HAL_LOCK(hsc);
00986 
00987     hsc->pTxBuffPtr = pData;
00988     hsc->TxXferSize = Size;
00989     hsc->TxXferCount = Size;
00990 
00991     hsc->ErrorCode = HAL_SMARTCARD_ERROR_NONE;
00992     hsc->gState = HAL_SMARTCARD_STATE_BUSY_TX;
00993 
00994     /* Set the SMARTCARD DMA transfer complete callback */
00995     hsc->hdmatx->XferCpltCallback = SMARTCARD_DMATransmitCplt;
00996 
00997     /* Set the DMA error callback */
00998     hsc->hdmatx->XferErrorCallback = SMARTCARD_DMAError;
00999 
01000     /* Set the DMA abort callback */
01001     hsc->hdmatx->XferAbortCallback = NULL;
01002 
01003     /* Enable the SMARTCARD transmit DMA stream */
01004     tmp = (uint32_t*)&pData;
01005     HAL_DMA_Start_IT(hsc->hdmatx, *(uint32_t*)tmp, (uint32_t)&hsc->Instance->DR, Size);
01006 
01007      /* Clear the TC flag in the SR register by writing 0 to it */
01008     __HAL_SMARTCARD_CLEAR_FLAG(hsc, SMARTCARD_FLAG_TC);
01009 
01010     /* Process Unlocked */
01011     __HAL_UNLOCK(hsc);
01012 
01013     /* Enable the DMA transfer for transmit request by setting the DMAT bit
01014     in the SMARTCARD CR3 register */
01015     SET_BIT(hsc->Instance->CR3, USART_CR3_DMAT);
01016 
01017     return HAL_OK;
01018   }
01019   else
01020   {
01021     return HAL_BUSY;
01022   }
01023 }
01024 
01025 /**
01026   * @brief Receive an amount of data in non blocking mode
01027   * @param  hsc    Pointer to a SMARTCARD_HandleTypeDef structure that contains
01028   *                the configuration information for SMARTCARD module.
01029   * @param  pData  Pointer to data buffer
01030   * @param  Size   Amount of data to be received
01031   * @note   When the SMARTCARD parity is enabled (PCE = 1) the data received contain the parity bit.s
01032   * @retval HAL status
01033   */
01034 HAL_StatusTypeDef HAL_SMARTCARD_Receive_DMA(SMARTCARD_HandleTypeDef *hsc, uint8_t *pData, uint16_t Size)
01035 {
01036   uint32_t *tmp;
01037 
01038   /* Check that a Rx process is not already ongoing */
01039   if(hsc->RxState == HAL_SMARTCARD_STATE_READY)
01040   {
01041     if((pData == NULL) || (Size == 0U))
01042     {
01043       return HAL_ERROR;
01044     }
01045 
01046     /* Process Locked */
01047     __HAL_LOCK(hsc);
01048 
01049     hsc->pRxBuffPtr = pData;
01050     hsc->RxXferSize = Size;
01051 
01052     hsc->ErrorCode = HAL_SMARTCARD_ERROR_NONE;
01053     hsc->RxState = HAL_SMARTCARD_STATE_BUSY_RX;
01054 
01055     /* Set the SMARTCARD DMA transfer complete callback */
01056     hsc->hdmarx->XferCpltCallback = SMARTCARD_DMAReceiveCplt;
01057 
01058     /* Set the DMA error callback */
01059     hsc->hdmarx->XferErrorCallback = SMARTCARD_DMAError;
01060 
01061     /* Set the DMA abort callback */
01062     hsc->hdmatx->XferAbortCallback = NULL;
01063 
01064     /* Enable the DMA stream */
01065     tmp = (uint32_t*)&pData;
01066     HAL_DMA_Start_IT(hsc->hdmarx, (uint32_t)&hsc->Instance->DR, *(uint32_t*)tmp, Size);
01067 
01068     /* Clear the Overrun flag just before enabling the DMA Rx request: can be mandatory for the second transfer */
01069     __HAL_SMARTCARD_CLEAR_OREFLAG(hsc);
01070 
01071     /* Process Unlocked */
01072     __HAL_UNLOCK(hsc);
01073 
01074     /* Enable the SMARTCARD Parity Error Interrupt */
01075     SET_BIT(hsc->Instance->CR1, USART_CR1_PEIE);
01076 
01077     /* Enable the SMARTCARD Error Interrupt: (Frame error, noise error, overrun error) */
01078     SET_BIT(hsc->Instance->CR3, USART_CR3_EIE);
01079 
01080     /* Enable the DMA transfer for the receiver request by setting the DMAR bit
01081     in the SMARTCARD CR3 register */
01082     SET_BIT(hsc->Instance->CR3, USART_CR3_DMAR);
01083 
01084     return HAL_OK;
01085   }
01086   else
01087   {
01088     return HAL_BUSY;
01089   }
01090 }
01091 
01092 /**
01093   * @brief  Abort ongoing transfers (blocking mode).
01094   * @param  hsc SMARTCARD handle.
01095   * @note   This procedure could be used for aborting any ongoing transfer started in Interrupt or DMA mode.
01096   *         This procedure performs following operations :
01097   *           - Disable PPP Interrupts
01098   *           - Disable the DMA transfer in the peripheral register (if enabled)
01099   *           - Abort DMA transfer by calling HAL_DMA_Abort (in case of transfer in DMA mode)
01100   *           - Set handle State to READY
01101   * @note   This procedure is executed in blocking mode : when exiting function, Abort is considered as completed.
01102   * @retval HAL status
01103 */
01104 HAL_StatusTypeDef HAL_SMARTCARD_Abort(SMARTCARD_HandleTypeDef *hsc)
01105 {
01106   /* Disable TXEIE, TCIE, RXNE, PE and ERR (Frame error, noise error, overrun error) interrupts */
01107   CLEAR_BIT(hsc->Instance->CR1, (USART_CR1_RXNEIE | USART_CR1_PEIE | USART_CR1_TXEIE | USART_CR1_TCIE));
01108   CLEAR_BIT(hsc->Instance->CR3, USART_CR3_EIE);
01109 
01110   /* Disable the SMARTCARD DMA Tx request if enabled */
01111   if(HAL_IS_BIT_SET(hsc->Instance->CR3, USART_CR3_DMAT))
01112   {
01113     CLEAR_BIT(hsc->Instance->CR3, USART_CR3_DMAT);
01114 
01115     /* Abort the SMARTCARD DMA Tx channel : use blocking DMA Abort API (no callback) */
01116     if(hsc->hdmatx != NULL)
01117     {
01118       /* Set the SMARTCARD DMA Abort callback to Null.
01119          No call back execution at end of DMA abort procedure */
01120       hsc->hdmatx->XferAbortCallback = NULL;
01121 
01122       HAL_DMA_Abort(hsc->hdmatx);
01123     }
01124   }
01125 
01126   /* Disable the SMARTCARD DMA Rx request if enabled */
01127   if(HAL_IS_BIT_SET(hsc->Instance->CR3, USART_CR3_DMAR))
01128   {
01129     CLEAR_BIT(hsc->Instance->CR3, USART_CR3_DMAR);
01130 
01131     /* Abort the SMARTCARD DMA Rx channel : use blocking DMA Abort API (no callback) */
01132     if(hsc->hdmarx != NULL)
01133     {
01134       /* Set the SMARTCARD DMA Abort callback to Null.
01135          No call back execution at end of DMA abort procedure */
01136       hsc->hdmarx->XferAbortCallback = NULL;
01137 
01138       HAL_DMA_Abort(hsc->hdmarx);
01139     }
01140   }
01141 
01142   /* Reset Tx and Rx transfer counters */
01143   hsc->TxXferCount = 0x00U;
01144   hsc->RxXferCount = 0x00U;
01145 
01146   /* Reset ErrorCode */
01147   hsc->ErrorCode = HAL_SMARTCARD_ERROR_NONE;
01148 
01149   /* Restore hsc->RxState and hsc->gState to Ready */
01150   hsc->RxState = HAL_SMARTCARD_STATE_READY;
01151   hsc->gState = HAL_SMARTCARD_STATE_READY;
01152 
01153   return HAL_OK;
01154 }
01155 
01156 /**
01157   * @brief  Abort ongoing Transmit transfer (blocking mode).
01158   * @param  hsc SMARTCARD handle.
01159   * @note   This procedure could be used for aborting any ongoing transfer started in Interrupt or DMA mode.
01160   *         This procedure performs following operations :
01161   *           - Disable SMARTCARD Interrupts (Tx)
01162   *           - Disable the DMA transfer in the peripheral register (if enabled)
01163   *           - Abort DMA transfer by calling HAL_DMA_Abort (in case of transfer in DMA mode)
01164   *           - Set handle State to READY
01165   * @note   This procedure is executed in blocking mode : when exiting function, Abort is considered as completed.
01166   * @retval HAL status
01167 */
01168 HAL_StatusTypeDef HAL_SMARTCARD_AbortTransmit(SMARTCARD_HandleTypeDef *hsc)
01169 {
01170   /* Disable TXEIE and TCIE interrupts */
01171   CLEAR_BIT(hsc->Instance->CR1, (USART_CR1_TXEIE | USART_CR1_TCIE));
01172 
01173   /* Disable the SMARTCARD DMA Tx request if enabled */
01174   if(HAL_IS_BIT_SET(hsc->Instance->CR3, USART_CR3_DMAT))
01175   {
01176     CLEAR_BIT(hsc->Instance->CR3, USART_CR3_DMAT);
01177 
01178     /* Abort the SMARTCARD DMA Tx channel : use blocking DMA Abort API (no callback) */
01179     if(hsc->hdmatx != NULL)
01180     {
01181       /* Set the SMARTCARD DMA Abort callback to Null.
01182          No call back execution at end of DMA abort procedure */
01183       hsc->hdmatx->XferAbortCallback = NULL;
01184 
01185       HAL_DMA_Abort(hsc->hdmatx);
01186     }
01187   }
01188 
01189   /* Reset Tx transfer counter */
01190   hsc->TxXferCount = 0x00U;
01191 
01192   /* Restore hsc->gState to Ready */
01193   hsc->gState = HAL_SMARTCARD_STATE_READY;
01194 
01195   return HAL_OK;
01196 }
01197 
01198 /**
01199   * @brief  Abort ongoing Receive transfer (blocking mode).
01200   * @param  hsc SMARTCARD handle.
01201   * @note   This procedure could be used for aborting any ongoing transfer started in Interrupt or DMA mode.
01202   *         This procedure performs following operations :
01203   *           - Disable PPP Interrupts
01204   *           - Disable the DMA transfer in the peripheral register (if enabled)
01205   *           - Abort DMA transfer by calling HAL_DMA_Abort (in case of transfer in DMA mode)
01206   *           - Set handle State to READY
01207   * @note   This procedure is executed in blocking mode : when exiting function, Abort is considered as completed.
01208   * @retval HAL status
01209 */
01210 HAL_StatusTypeDef HAL_SMARTCARD_AbortReceive(SMARTCARD_HandleTypeDef *hsc)
01211 {
01212   /* Disable RXNE, PE and ERR (Frame error, noise error, overrun error) interrupts */
01213   CLEAR_BIT(hsc->Instance->CR1, (USART_CR1_RXNEIE | USART_CR1_PEIE));
01214   CLEAR_BIT(hsc->Instance->CR3, USART_CR3_EIE);
01215 
01216   /* Disable the SMARTCARD DMA Rx request if enabled */
01217   if(HAL_IS_BIT_SET(hsc->Instance->CR3, USART_CR3_DMAR))
01218   {
01219     CLEAR_BIT(hsc->Instance->CR3, USART_CR3_DMAR);
01220 
01221     /* Abort the SMARTCARD DMA Rx channel : use blocking DMA Abort API (no callback) */
01222     if(hsc->hdmarx != NULL)
01223     {
01224       /* Set the SMARTCARD DMA Abort callback to Null.
01225          No call back execution at end of DMA abort procedure */
01226       hsc->hdmarx->XferAbortCallback = NULL;
01227 
01228       HAL_DMA_Abort(hsc->hdmarx);
01229     }
01230   }
01231 
01232   /* Reset Rx transfer counter */
01233   hsc->RxXferCount = 0x00U;
01234 
01235   /* Restore hsc->RxState to Ready */
01236   hsc->RxState = HAL_SMARTCARD_STATE_READY;
01237 
01238   return HAL_OK;
01239 }
01240 
01241 /**
01242   * @brief  Abort ongoing transfers (Interrupt mode).
01243   * @param  hsc SMARTCARD handle.
01244   * @note   This procedure could be used for aborting any ongoing transfer started in Interrupt or DMA mode.
01245   *         This procedure performs following operations :
01246   *           - Disable PPP Interrupts
01247   *           - Disable the DMA transfer in the peripheral register (if enabled)
01248   *           - Abort DMA transfer by calling HAL_DMA_Abort_IT (in case of transfer in DMA mode)
01249   *           - Set handle State to READY
01250   *           - At abort completion, call user abort complete callback
01251   * @note   This procedure is executed in Interrupt mode, meaning that abort procedure could be
01252   *         considered as completed only when user abort complete callback is executed (not when exiting function).
01253   * @retval HAL status
01254 */
01255 HAL_StatusTypeDef HAL_SMARTCARD_Abort_IT(SMARTCARD_HandleTypeDef *hsc)
01256 {
01257   uint32_t AbortCplt = 0x01U;
01258 
01259   /* Disable TXEIE, TCIE, RXNE, PE and ERR (Frame error, noise error, overrun error) interrupts */
01260   CLEAR_BIT(hsc->Instance->CR1, (USART_CR1_RXNEIE | USART_CR1_PEIE | USART_CR1_TXEIE | USART_CR1_TCIE));
01261   CLEAR_BIT(hsc->Instance->CR3, USART_CR3_EIE);
01262 
01263   /* If DMA Tx and/or DMA Rx Handles are associated to SMARTCARD Handle, DMA Abort complete callbacks should be initialised
01264      before any call to DMA Abort functions */
01265   /* DMA Tx Handle is valid */
01266   if(hsc->hdmatx != NULL)
01267   {
01268     /* Set DMA Abort Complete callback if SMARTCARD DMA Tx request if enabled.
01269        Otherwise, set it to NULL */
01270     if(HAL_IS_BIT_SET(hsc->Instance->CR3, USART_CR3_DMAT))
01271     {
01272       hsc->hdmatx->XferAbortCallback = SMARTCARD_DMATxAbortCallback;
01273     }
01274     else
01275     {
01276       hsc->hdmatx->XferAbortCallback = NULL;
01277     }
01278   }
01279   /* DMA Rx Handle is valid */
01280   if(hsc->hdmarx != NULL)
01281   {
01282     /* Set DMA Abort Complete callback if SMARTCARD DMA Rx request if enabled.
01283        Otherwise, set it to NULL */
01284     if(HAL_IS_BIT_SET(hsc->Instance->CR3, USART_CR3_DMAR))
01285     {
01286       hsc->hdmarx->XferAbortCallback = SMARTCARD_DMARxAbortCallback;
01287     }
01288     else
01289     {
01290       hsc->hdmarx->XferAbortCallback = NULL;
01291     }
01292   }
01293 
01294   /* Disable the SMARTCARD DMA Tx request if enabled */
01295   if(HAL_IS_BIT_SET(hsc->Instance->CR3, USART_CR3_DMAT))
01296   {
01297     /* Disable DMA Tx at SMARTCARD level */
01298     CLEAR_BIT(hsc->Instance->CR3, USART_CR3_DMAT);
01299 
01300     /* Abort the SMARTCARD DMA Tx channel : use non blocking DMA Abort API (callback) */
01301     if(hsc->hdmatx != NULL)
01302     {
01303       /* SMARTCARD Tx DMA Abort callback has already been initialised :
01304          will lead to call HAL_SMARTCARD_AbortCpltCallback() at end of DMA abort procedure */
01305 
01306       /* Abort DMA TX */
01307       if(HAL_DMA_Abort_IT(hsc->hdmatx) != HAL_OK)
01308       {
01309         hsc->hdmatx->XferAbortCallback = NULL;
01310       }
01311       else
01312       {
01313         AbortCplt = 0x00U;
01314       }
01315     }
01316   }
01317 
01318   /* Disable the SMARTCARD DMA Rx request if enabled */
01319   if(HAL_IS_BIT_SET(hsc->Instance->CR3, USART_CR3_DMAR))
01320   {
01321     CLEAR_BIT(hsc->Instance->CR3, USART_CR3_DMAR);
01322 
01323     /* Abort the SMARTCARD DMA Rx channel : use non blocking DMA Abort API (callback) */
01324     if(hsc->hdmarx != NULL)
01325     {
01326       /* SMARTCARD Rx DMA Abort callback has already been initialised :
01327          will lead to call HAL_SMARTCARD_AbortCpltCallback() at end of DMA abort procedure */
01328 
01329       /* Abort DMA RX */
01330       if(HAL_DMA_Abort_IT(hsc->hdmarx) != HAL_OK)
01331       {
01332         hsc->hdmarx->XferAbortCallback = NULL;
01333         AbortCplt = 0x01U;
01334       }
01335       else
01336       {
01337         AbortCplt = 0x00U;
01338       }
01339     }
01340   }
01341 
01342   /* if no DMA abort complete callback execution is required => call user Abort Complete callback */
01343   if(AbortCplt == 0x01U)
01344   {
01345     /* Reset Tx and Rx transfer counters */
01346     hsc->TxXferCount = 0x00U;
01347     hsc->RxXferCount = 0x00U;
01348 
01349     /* Reset ErrorCode */
01350     hsc->ErrorCode = HAL_SMARTCARD_ERROR_NONE;
01351 
01352     /* Restore hsc->gState and hsc->RxState to Ready */
01353     hsc->gState  = HAL_SMARTCARD_STATE_READY;
01354     hsc->RxState = HAL_SMARTCARD_STATE_READY;
01355 
01356     /* As no DMA to be aborted, call directly user Abort complete callback */
01357 #if (USE_HAL_SMARTCARD_REGISTER_CALLBACKS == 1)
01358     /* Call registered Abort complete callback */
01359     hsc->AbortCpltCallback(hsc);
01360 #else
01361     /* Call legacy weak Abort complete callback */
01362     HAL_SMARTCARD_AbortCpltCallback(hsc);
01363 #endif /* USE_HAL_SMARTCARD_REGISTER_CALLBACK */
01364   }
01365   return HAL_OK;
01366 }
01367 
01368 /**
01369   * @brief  Abort ongoing Transmit transfer (Interrupt mode).
01370   * @param  hsc SMARTCARD handle.
01371   * @note   This procedure could be used for aborting any ongoing transfer started in Interrupt or DMA mode.
01372   *         This procedure performs following operations :
01373   *           - Disable SMARTCARD Interrupts (Tx)
01374   *           - Disable the DMA transfer in the peripheral register (if enabled)
01375   *           - Abort DMA transfer by calling HAL_DMA_Abort_IT (in case of transfer in DMA mode)
01376   *           - Set handle State to READY
01377   *           - At abort completion, call user abort complete callback
01378   * @note   This procedure is executed in Interrupt mode, meaning that abort procedure could be
01379   *         considered as completed only when user abort complete callback is executed (not when exiting function).
01380   * @retval HAL status
01381 */
01382 HAL_StatusTypeDef HAL_SMARTCARD_AbortTransmit_IT(SMARTCARD_HandleTypeDef *hsc)
01383 {
01384   /* Disable TXEIE and TCIE interrupts */
01385   CLEAR_BIT(hsc->Instance->CR1, (USART_CR1_TXEIE | USART_CR1_TCIE));
01386 
01387   /* Disable the SMARTCARD DMA Tx request if enabled */
01388   if(HAL_IS_BIT_SET(hsc->Instance->CR3, USART_CR3_DMAT))
01389   {
01390     CLEAR_BIT(hsc->Instance->CR3, USART_CR3_DMAT);
01391 
01392     /* Abort the SMARTCARD DMA Tx channel : use blocking DMA Abort API (no callback) */
01393     if(hsc->hdmatx != NULL)
01394     {
01395       /* Set the SMARTCARD DMA Abort callback :
01396          will lead to call HAL_SMARTCARD_AbortCpltCallback() at end of DMA abort procedure */
01397       hsc->hdmatx->XferAbortCallback = SMARTCARD_DMATxOnlyAbortCallback;
01398 
01399       /* Abort DMA TX */
01400       if(HAL_DMA_Abort_IT(hsc->hdmatx) != HAL_OK)
01401       {
01402         /* Call Directly hsc->hdmatx->XferAbortCallback function in case of error */
01403         hsc->hdmatx->XferAbortCallback(hsc->hdmatx);
01404       }
01405     }
01406     else
01407     {
01408       /* Reset Tx transfer counter */
01409       hsc->TxXferCount = 0x00U;
01410 
01411       /* Restore hsc->gState to Ready */
01412       hsc->gState = HAL_SMARTCARD_STATE_READY;
01413 
01414       /* As no DMA to be aborted, call directly user Abort complete callback */
01415 #if (USE_HAL_SMARTCARD_REGISTER_CALLBACKS == 1)
01416       /* Call registered Abort Transmit Complete Callback */
01417       hsc->AbortTransmitCpltCallback(hsc);
01418 #else
01419       /* Call legacy weak Abort Transmit Complete Callback */
01420       HAL_SMARTCARD_AbortTransmitCpltCallback(hsc);
01421 #endif /* USE_HAL_SMARTCARD_REGISTER_CALLBACK */
01422     }
01423   }
01424   else
01425   {
01426     /* Reset Tx transfer counter */
01427     hsc->TxXferCount = 0x00U;
01428 
01429     /* Restore hsc->gState to Ready */
01430     hsc->gState = HAL_SMARTCARD_STATE_READY;
01431 
01432     /* As no DMA to be aborted, call directly user Abort complete callback */
01433 #if (USE_HAL_SMARTCARD_REGISTER_CALLBACKS == 1)
01434     /* Call registered Abort Transmit Complete Callback */
01435     hsc->AbortTransmitCpltCallback(hsc);
01436 #else
01437     /* Call legacy weak Abort Transmit Complete Callback */
01438     HAL_SMARTCARD_AbortTransmitCpltCallback(hsc);
01439 #endif /* USE_HAL_SMARTCARD_REGISTER_CALLBACK */
01440   }
01441 
01442   return HAL_OK;
01443 }
01444 
01445 /**
01446   * @brief  Abort ongoing Receive transfer (Interrupt mode).
01447   * @param  hsc SMARTCARD handle.
01448   * @note   This procedure could be used for aborting any ongoing transfer started in Interrupt or DMA mode.
01449   *         This procedure performs following operations :
01450   *           - Disable SMARTCARD Interrupts (Rx)
01451   *           - Disable the DMA transfer in the peripheral register (if enabled)
01452   *           - Abort DMA transfer by calling HAL_DMA_Abort_IT (in case of transfer in DMA mode)
01453   *           - Set handle State to READY
01454   *           - At abort completion, call user abort complete callback
01455   * @note   This procedure is executed in Interrupt mode, meaning that abort procedure could be
01456   *         considered as completed only when user abort complete callback is executed (not when exiting function).
01457   * @retval HAL status
01458 */
01459 HAL_StatusTypeDef HAL_SMARTCARD_AbortReceive_IT(SMARTCARD_HandleTypeDef *hsc)
01460 {
01461   /* Disable RXNE, PE and ERR (Frame error, noise error, overrun error) interrupts */
01462   CLEAR_BIT(hsc->Instance->CR1, (USART_CR1_RXNEIE | USART_CR1_PEIE));
01463   CLEAR_BIT(hsc->Instance->CR3, USART_CR3_EIE);
01464 
01465   /* Disable the SMARTCARD DMA Rx request if enabled */
01466   if(HAL_IS_BIT_SET(hsc->Instance->CR3, USART_CR3_DMAR))
01467   {
01468     CLEAR_BIT(hsc->Instance->CR3, USART_CR3_DMAR);
01469 
01470     /* Abort the SMARTCARD DMA Rx channel : use blocking DMA Abort API (no callback) */
01471     if(hsc->hdmarx != NULL)
01472     {
01473       /* Set the SMARTCARD DMA Abort callback :
01474          will lead to call HAL_SMARTCARD_AbortCpltCallback() at end of DMA abort procedure */
01475       hsc->hdmarx->XferAbortCallback = SMARTCARD_DMARxOnlyAbortCallback;
01476 
01477       /* Abort DMA RX */
01478       if(HAL_DMA_Abort_IT(hsc->hdmarx) != HAL_OK)
01479       {
01480         /* Call Directly hsc->hdmarx->XferAbortCallback function in case of error */
01481         hsc->hdmarx->XferAbortCallback(hsc->hdmarx);
01482       }
01483     }
01484     else
01485     {
01486       /* Reset Rx transfer counter */
01487       hsc->RxXferCount = 0x00U;
01488 
01489       /* Restore hsc->RxState to Ready */
01490       hsc->RxState = HAL_SMARTCARD_STATE_READY;
01491 
01492       /* As no DMA to be aborted, call directly user Abort complete callback */
01493 #if (USE_HAL_SMARTCARD_REGISTER_CALLBACKS == 1)
01494       /* Call registered Abort Receive Complete Callback */
01495       hsc->AbortReceiveCpltCallback(hsc);
01496 #else
01497       /* Call legacy weak Abort Receive Complete Callback */
01498       HAL_SMARTCARD_AbortReceiveCpltCallback(hsc);
01499 #endif /* USE_HAL_SMARTCARD_REGISTER_CALLBACK */
01500     }
01501   }
01502   else
01503   {
01504     /* Reset Rx transfer counter */
01505     hsc->RxXferCount = 0x00U;
01506 
01507     /* Restore hsc->RxState to Ready */
01508     hsc->RxState = HAL_SMARTCARD_STATE_READY;
01509 
01510     /* As no DMA to be aborted, call directly user Abort complete callback */
01511 #if (USE_HAL_SMARTCARD_REGISTER_CALLBACKS == 1)
01512     /* Call registered Abort Receive Complete Callback */
01513     hsc->AbortReceiveCpltCallback(hsc);
01514 #else
01515     /* Call legacy weak Abort Receive Complete Callback */
01516     HAL_SMARTCARD_AbortReceiveCpltCallback(hsc);
01517 #endif /* USE_HAL_SMARTCARD_REGISTER_CALLBACK */
01518   }
01519 
01520   return HAL_OK;
01521 }
01522 
01523 /**
01524   * @brief This function handles SMARTCARD interrupt request.
01525   * @param  hsc    Pointer to a SMARTCARD_HandleTypeDef structure that contains
01526   *                the configuration information for SMARTCARD module.
01527   * @retval None
01528   */
01529 void HAL_SMARTCARD_IRQHandler(SMARTCARD_HandleTypeDef *hsc)
01530 {
01531   uint32_t isrflags   = READ_REG(hsc->Instance->SR);
01532   uint32_t cr1its     = READ_REG(hsc->Instance->CR1);
01533   uint32_t cr3its     = READ_REG(hsc->Instance->CR3);
01534   uint32_t dmarequest = 0x00U;
01535   uint32_t errorflags = 0x00U;
01536 
01537   /* If no error occurs */
01538   errorflags = (isrflags & (uint32_t)(USART_SR_PE | USART_SR_FE | USART_SR_ORE | USART_SR_NE));
01539   if(errorflags == RESET)
01540   {
01541     /* SMARTCARD in mode Receiver -------------------------------------------------*/
01542     if(((isrflags & USART_SR_RXNE) != RESET) && ((cr1its & USART_CR1_RXNEIE) != RESET))
01543     {
01544       SMARTCARD_Receive_IT(hsc);
01545       return;
01546     }
01547   }
01548 
01549   /* If some errors occur */
01550   if((errorflags != RESET) && (((cr3its & USART_CR3_EIE) != RESET) || ((cr1its & (USART_CR1_RXNEIE | USART_CR1_PEIE)) != RESET)))
01551   {
01552     /* SMARTCARD parity error interrupt occurred ---------------------------*/
01553     if(((isrflags & SMARTCARD_FLAG_PE) != RESET) && ((cr1its & USART_CR1_PEIE) != RESET))
01554     {
01555       hsc->ErrorCode |= HAL_SMARTCARD_ERROR_PE;
01556     }
01557 
01558     /* SMARTCARD frame error interrupt occurred ----------------------------*/
01559     if(((isrflags & SMARTCARD_FLAG_FE) != RESET) && ((cr3its & USART_CR3_EIE) != RESET))
01560     {
01561       hsc->ErrorCode |= HAL_SMARTCARD_ERROR_FE;
01562     }
01563 
01564     /* SMARTCARD noise error interrupt occurred ----------------------------*/
01565     if(((isrflags & SMARTCARD_FLAG_NE) != RESET) && ((cr3its & USART_CR3_EIE) != RESET))
01566     {
01567       hsc->ErrorCode |= HAL_SMARTCARD_ERROR_NE;
01568     }
01569 
01570     /* SMARTCARD Over-Run interrupt occurred -------------------------------*/
01571     if(((isrflags & SMARTCARD_FLAG_ORE) != RESET) && (((cr1its & USART_CR1_RXNEIE) != RESET) || ((cr3its & USART_CR3_EIE) != RESET)))
01572     {
01573       hsc->ErrorCode |= HAL_SMARTCARD_ERROR_ORE;
01574     }
01575     /* Call the Error call Back in case of Errors --------------------------*/
01576     if(hsc->ErrorCode != HAL_SMARTCARD_ERROR_NONE)
01577     {
01578       /* SMARTCARD in mode Receiver ----------------------------------------*/
01579       if(((isrflags & USART_SR_RXNE) != RESET) && ((cr1its & USART_CR1_RXNEIE) != RESET))
01580       {
01581         SMARTCARD_Receive_IT(hsc);
01582       }
01583 
01584       /* If Overrun error occurs, or if any error occurs in DMA mode reception,
01585          consider error as blocking */
01586       dmarequest = HAL_IS_BIT_SET(hsc->Instance->CR3, USART_CR3_DMAR);
01587       if(((hsc->ErrorCode & HAL_SMARTCARD_ERROR_ORE) != RESET) || dmarequest)
01588       {
01589         /* Blocking error : transfer is aborted
01590           Set the SMARTCARD state ready to be able to start again the process,
01591           Disable Rx Interrupts, and disable Rx DMA request, if ongoing */
01592         SMARTCARD_EndRxTransfer(hsc);
01593         /* Disable the SMARTCARD DMA Rx request if enabled */
01594         if(HAL_IS_BIT_SET(hsc->Instance->CR3, USART_CR3_DMAR))
01595         {
01596           CLEAR_BIT(hsc->Instance->CR3, USART_CR3_DMAR);
01597 
01598           /* Abort the SMARTCARD DMA Rx channel */
01599           if(hsc->hdmarx != NULL)
01600           {
01601             /* Set the SMARTCARD DMA Abort callback :
01602               will lead to call HAL_SMARTCARD_ErrorCallback() at end of DMA abort procedure */
01603             hsc->hdmarx->XferAbortCallback = SMARTCARD_DMAAbortOnError;
01604 
01605            if(HAL_DMA_Abort_IT(hsc->hdmarx) != HAL_OK)
01606             {
01607               /* Call Directly XferAbortCallback function in case of error */
01608               hsc->hdmarx->XferAbortCallback(hsc->hdmarx);
01609             }
01610           }
01611           else
01612           {
01613 #if (USE_HAL_SMARTCARD_REGISTER_CALLBACKS == 1)
01614             /* Call registered user error callback */
01615             hsc->ErrorCallback(hsc);
01616 #else
01617             /* Call legacy weak user error callback */
01618             HAL_SMARTCARD_ErrorCallback(hsc);
01619 #endif /* USE_HAL_SMARTCARD_REGISTER_CALLBACK */
01620           }
01621         }
01622         else
01623         {
01624 #if (USE_HAL_SMARTCARD_REGISTER_CALLBACKS == 1)
01625           /* Call registered user error callback */
01626           hsc->ErrorCallback(hsc);
01627 #else
01628           /* Call legacy weak user error callback */
01629           HAL_SMARTCARD_ErrorCallback(hsc);
01630 #endif /* USE_HAL_SMARTCARD_REGISTER_CALLBACK */
01631         }
01632       }
01633       else
01634       {
01635         /* Non Blocking error : transfer could go on.
01636            Error is notified to user through user error callback */
01637 #if (USE_HAL_SMARTCARD_REGISTER_CALLBACKS == 1)
01638         /* Call registered user error callback */
01639         hsc->ErrorCallback(hsc);
01640 #else
01641         /* Call legacy weak user error callback */
01642         HAL_SMARTCARD_ErrorCallback(hsc);
01643 #endif /* USE_HAL_SMARTCARD_REGISTER_CALLBACK */
01644         hsc->ErrorCode = HAL_SMARTCARD_ERROR_NONE;
01645       }
01646     }
01647     return;
01648   } /* End if some error occurs */
01649 
01650   /* SMARTCARD in mode Transmitter ------------------------------------------*/
01651   if(((isrflags & SMARTCARD_FLAG_TXE) != RESET) && ((cr1its & USART_CR1_TXEIE) != RESET))
01652   {
01653     SMARTCARD_Transmit_IT(hsc);
01654     return;
01655   }
01656 
01657   /* SMARTCARD in mode Transmitter (transmission end) -----------------------*/
01658   if(((isrflags & SMARTCARD_FLAG_TC) != RESET) && ((cr1its & USART_CR1_TCIE) != RESET))
01659   {
01660     SMARTCARD_EndTransmit_IT(hsc);
01661     return;
01662   }
01663 }
01664 
01665 /**
01666   * @brief Tx Transfer completed callbacks
01667   * @param  hsc    Pointer to a SMARTCARD_HandleTypeDef structure that contains
01668   *                the configuration information for SMARTCARD module.
01669   * @retval None
01670   */
01671 __weak void HAL_SMARTCARD_TxCpltCallback(SMARTCARD_HandleTypeDef *hsc)
01672 {
01673   /* Prevent unused argument(s) compilation warning */
01674   UNUSED(hsc);
01675 
01676   /* NOTE : This function should not be modified, when the callback is needed,
01677             the HAL_SMARTCARD_TxCpltCallback can be implemented in the user file.
01678    */
01679 }
01680 
01681 /**
01682   * @brief Rx Transfer completed callback
01683   * @param  hsc    Pointer to a SMARTCARD_HandleTypeDef structure that contains
01684   *                the configuration information for SMARTCARD module.
01685   * @retval None
01686   */
01687 __weak void HAL_SMARTCARD_RxCpltCallback(SMARTCARD_HandleTypeDef *hsc)
01688 {
01689   /* Prevent unused argument(s) compilation warning */
01690   UNUSED(hsc);
01691 
01692   /* NOTE : This function should not be modified, when the callback is needed,
01693             the HAL_SMARTCARD_RxCpltCallback can be implemented in the user file.
01694    */
01695 }
01696 
01697 /**
01698   * @brief SMARTCARD error callback
01699   * @param  hsc    Pointer to a SMARTCARD_HandleTypeDef structure that contains
01700   *                the configuration information for SMARTCARD module.
01701   * @retval None
01702   */
01703 __weak void HAL_SMARTCARD_ErrorCallback(SMARTCARD_HandleTypeDef *hsc)
01704 {
01705   /* Prevent unused argument(s) compilation warning */
01706   UNUSED(hsc);
01707 
01708   /* NOTE : This function should not be modified, when the callback is needed,
01709             the HAL_SMARTCARD_ErrorCallback can be implemented in the user file.
01710    */
01711 }
01712 
01713 /**
01714   * @brief  SMARTCARD Abort Complete callback.
01715   * @param  hsc SMARTCARD handle.
01716   * @retval None
01717   */
01718 __weak void HAL_SMARTCARD_AbortCpltCallback (SMARTCARD_HandleTypeDef *hsc)
01719 {
01720   /* Prevent unused argument(s) compilation warning */
01721   UNUSED(hsc);
01722 
01723   /* NOTE : This function should not be modified, when the callback is needed,
01724             the HAL_SMARTCARD_AbortCpltCallback can be implemented in the user file.
01725    */
01726 }
01727 
01728 /**
01729   * @brief  SMARTCARD Abort Transmit Complete callback.
01730   * @param  hsc SMARTCARD handle.
01731   * @retval None
01732   */
01733 __weak void HAL_SMARTCARD_AbortTransmitCpltCallback (SMARTCARD_HandleTypeDef *hsc)
01734 {
01735     /* Prevent unused argument(s) compilation warning */
01736     UNUSED(hsc);
01737 
01738   /* NOTE : This function should not be modified, when the callback is needed,
01739             the HAL_SMARTCARD_AbortTransmitCpltCallback can be implemented in the user file.
01740    */
01741 }
01742 
01743 /**
01744   * @brief  SMARTCARD Abort Receive Complete callback.
01745   * @param  hsc SMARTCARD handle.
01746   * @retval None
01747   */
01748 __weak void HAL_SMARTCARD_AbortReceiveCpltCallback (SMARTCARD_HandleTypeDef *hsc)
01749 {
01750     /* Prevent unused argument(s) compilation warning */
01751     UNUSED(hsc);
01752 
01753   /* NOTE : This function should not be modified, when the callback is needed,
01754             the HAL_SMARTCARD_AbortReceiveCpltCallback can be implemented in the user file.
01755    */
01756 }
01757 
01758 /**
01759   * @}
01760   */
01761 
01762 /** @defgroup SMARTCARD_Exported_Functions_Group3 Peripheral State and Errors functions
01763   *  @brief   SMARTCARD State and Errors functions
01764   *
01765 @verbatim
01766  ===============================================================================
01767                 ##### Peripheral State and Errors functions #####
01768  ===============================================================================
01769     [..]
01770     This subsection provides a set of functions allowing to control the SmartCard.
01771      (+) HAL_SMARTCARD_GetState() API can be helpful to check in run-time the state of the SmartCard peripheral.
01772      (+) HAL_SMARTCARD_GetError() check in run-time errors that could be occurred during communication.
01773 @endverbatim
01774   * @{
01775   */
01776 
01777 /**
01778   * @brief Return the SMARTCARD handle state
01779   * @param  hsc    Pointer to a SMARTCARD_HandleTypeDef structure that contains
01780   *                the configuration information for SMARTCARD module.
01781   * @retval HAL state
01782   */
01783 HAL_SMARTCARD_StateTypeDef HAL_SMARTCARD_GetState(SMARTCARD_HandleTypeDef *hsc)
01784 {
01785   uint32_t temp1= 0x00U, temp2 = 0x00U;
01786   temp1 = hsc->gState;
01787   temp2 = hsc->RxState;
01788 
01789   return (HAL_SMARTCARD_StateTypeDef)(temp1 | temp2);
01790 }
01791 
01792 /**
01793   * @brief  Return the SMARTCARD error code
01794   * @param  hsc  Pointer to a SMARTCARD_HandleTypeDef structure that contains
01795   *              the configuration information for the specified SMARTCARD.
01796   * @retval SMARTCARD Error Code
01797   */
01798 uint32_t HAL_SMARTCARD_GetError(SMARTCARD_HandleTypeDef *hsc)
01799 {
01800   return hsc->ErrorCode;
01801 }
01802 
01803 /**
01804   * @}
01805   */
01806 
01807 /**
01808   * @}
01809   */
01810 
01811 /** @defgroup SMARTCARD_Private_Functions SMARTCARD Private Functions
01812   * @{
01813   */
01814 
01815 #if (USE_HAL_SMARTCARD_REGISTER_CALLBACKS == 1)
01816 /**
01817   * @brief  Initialize the callbacks to their default values.
01818   * @param  hsc SMARTCARD handle.
01819   * @retval none
01820   */
01821 void SMARTCARD_InitCallbacksToDefault(SMARTCARD_HandleTypeDef *hsc)
01822 {
01823   /* Init the SMARTCARD Callback settings */
01824   hsc->TxCpltCallback            = HAL_SMARTCARD_TxCpltCallback;            /* Legacy weak TxCpltCallback            */
01825   hsc->RxCpltCallback            = HAL_SMARTCARD_RxCpltCallback;            /* Legacy weak RxCpltCallback            */
01826   hsc->ErrorCallback             = HAL_SMARTCARD_ErrorCallback;             /* Legacy weak ErrorCallback             */
01827   hsc->AbortCpltCallback         = HAL_SMARTCARD_AbortCpltCallback;         /* Legacy weak AbortCpltCallback         */
01828   hsc->AbortTransmitCpltCallback = HAL_SMARTCARD_AbortTransmitCpltCallback; /* Legacy weak AbortTransmitCpltCallback */
01829   hsc->AbortReceiveCpltCallback  = HAL_SMARTCARD_AbortReceiveCpltCallback;  /* Legacy weak AbortReceiveCpltCallback  */
01830 
01831 }
01832 #endif /* USE_HAL_SMARTCARD_REGISTER_CALLBACKS */
01833 
01834 /**
01835   * @brief DMA SMARTCARD transmit process complete callback
01836   * @param  hdma   Pointer to a DMA_HandleTypeDef structure that contains
01837   *                the configuration information for the specified DMA module.
01838   * @retval None
01839   */
01840 static void SMARTCARD_DMATransmitCplt(DMA_HandleTypeDef *hdma)
01841 {
01842   SMARTCARD_HandleTypeDef* hsc = ( SMARTCARD_HandleTypeDef* )((DMA_HandleTypeDef* )hdma)->Parent;
01843 
01844   hsc->TxXferCount = 0U;
01845 
01846   /* Disable the DMA transfer for transmit request by setting the DMAT bit
01847      in the USART CR3 register */
01848   CLEAR_BIT(hsc->Instance->CR3, USART_CR3_DMAT);
01849 
01850   /* Enable the SMARTCARD Transmit Complete Interrupt */
01851   SET_BIT(hsc->Instance->CR1, USART_CR1_TCIE);
01852 }
01853 
01854 /**
01855   * @brief DMA SMARTCARD receive process complete callback
01856   * @param  hdma   Pointer to a DMA_HandleTypeDef structure that contains
01857   *                the configuration information for the specified DMA module.
01858   * @retval None
01859   */
01860 static void SMARTCARD_DMAReceiveCplt(DMA_HandleTypeDef *hdma)
01861 {
01862   SMARTCARD_HandleTypeDef* hsc = ( SMARTCARD_HandleTypeDef* )((DMA_HandleTypeDef* )hdma)->Parent;
01863 
01864   hsc->RxXferCount = 0U;
01865 
01866   /* Disable RXNE, PE and ERR (Frame error, noise error, overrun error) interrupts */
01867   CLEAR_BIT(hsc->Instance->CR1, (USART_CR1_RXNEIE | USART_CR1_PEIE));
01868   CLEAR_BIT(hsc->Instance->CR3, USART_CR3_EIE);
01869 
01870   /* Disable the DMA transfer for the receiver request by setting the DMAR bit
01871      in the USART CR3 register */
01872   CLEAR_BIT(hsc->Instance->CR3, USART_CR3_DMAR);
01873 
01874   /* At end of Rx process, restore hsc->RxState to Ready */
01875   hsc->RxState = HAL_SMARTCARD_STATE_READY;
01876 
01877 #if (USE_HAL_SMARTCARD_REGISTER_CALLBACKS == 1)
01878   /* Call registered Rx complete callback */
01879   hsc->RxCpltCallback(hsc);
01880 #else
01881   /* Call legacy weak Rx complete callback */
01882   HAL_SMARTCARD_RxCpltCallback(hsc);
01883 #endif /* USE_HAL_SMARTCARD_REGISTER_CALLBACK */
01884 }
01885 
01886 /**
01887   * @brief DMA SMARTCARD communication error callback
01888   * @param  hdma   Pointer to a DMA_HandleTypeDef structure that contains
01889   *                the configuration information for the specified DMA module.
01890   * @retval None
01891   */
01892 static void SMARTCARD_DMAError(DMA_HandleTypeDef *hdma)
01893 {
01894   uint32_t dmarequest = 0x00U;
01895   SMARTCARD_HandleTypeDef* hsc = ( SMARTCARD_HandleTypeDef* )((DMA_HandleTypeDef* )hdma)->Parent;
01896   hsc->RxXferCount = 0U;
01897   hsc->TxXferCount = 0U;
01898   hsc->ErrorCode = HAL_SMARTCARD_ERROR_DMA;
01899 
01900   /* Stop SMARTCARD DMA Tx request if ongoing */
01901   dmarequest = HAL_IS_BIT_SET(hsc->Instance->CR3, USART_CR3_DMAT);
01902   if((hsc->gState == HAL_SMARTCARD_STATE_BUSY_TX) && dmarequest)
01903   {
01904     SMARTCARD_EndTxTransfer(hsc);
01905   }
01906 
01907   /* Stop SMARTCARD DMA Rx request if ongoing */
01908   dmarequest = HAL_IS_BIT_SET(hsc->Instance->CR3, USART_CR3_DMAR);
01909   if((hsc->RxState == HAL_SMARTCARD_STATE_BUSY_RX) && dmarequest)
01910   {
01911     SMARTCARD_EndRxTransfer(hsc);
01912   }
01913 
01914 #if (USE_HAL_SMARTCARD_REGISTER_CALLBACKS == 1)
01915   /* Call registered user error callback */
01916   hsc->ErrorCallback(hsc);
01917 #else
01918   /* Call legacy weak user error callback */
01919   HAL_SMARTCARD_ErrorCallback(hsc);
01920 #endif /* USE_HAL_SMARTCARD_REGISTER_CALLBACK */
01921 }
01922 
01923 /**
01924   * @brief  This function handles SMARTCARD Communication Timeout.
01925   * @param  hsc    Pointer to a SMARTCARD_HandleTypeDef structure that contains
01926   *                the configuration information for SMARTCARD module.
01927   * @param  Flag   Specifies the SMARTCARD flag to check.
01928   * @param  Status The new Flag status (SET or RESET).
01929   * @param  Timeout Timeout duration
01930   * @param  Tickstart Tick start value
01931   * @retval HAL status
01932   */
01933 static HAL_StatusTypeDef SMARTCARD_WaitOnFlagUntilTimeout(SMARTCARD_HandleTypeDef *hsc, uint32_t Flag, FlagStatus Status, uint32_t Tickstart, uint32_t Timeout)
01934 {
01935   /* Wait until flag is set */
01936   while((__HAL_SMARTCARD_GET_FLAG(hsc, Flag) ? SET : RESET) == Status)
01937   {
01938     /* Check for the Timeout */
01939     if(Timeout != HAL_MAX_DELAY)
01940     {
01941       if((Timeout == 0U)||((HAL_GetTick() - Tickstart ) > Timeout))
01942       {
01943         /* Disable TXE and RXNE interrupts for the interrupt process */
01944         CLEAR_BIT(hsc->Instance->CR1, USART_CR1_TXEIE);
01945         CLEAR_BIT(hsc->Instance->CR1, USART_CR1_RXNEIE);
01946 
01947         hsc->gState= HAL_SMARTCARD_STATE_READY;
01948         hsc->RxState= HAL_SMARTCARD_STATE_READY;
01949 
01950         /* Process Unlocked */
01951         __HAL_UNLOCK(hsc);
01952 
01953         return HAL_TIMEOUT;
01954       }
01955     }
01956   }
01957   return HAL_OK;
01958 }
01959 
01960 /**
01961   * @brief  End ongoing Tx transfer on SMARTCARD peripheral (following error detection or Transmit completion).
01962   * @param  hsc    Pointer to a SMARTCARD_HandleTypeDef structure that contains
01963   *                the configuration information for SMARTCARD module.
01964   * @retval None
01965   */
01966 static void SMARTCARD_EndTxTransfer(SMARTCARD_HandleTypeDef *hsc)
01967 {
01968   /* At end of Tx process, restore hsc->gState to Ready */
01969   hsc->gState = HAL_SMARTCARD_STATE_READY;
01970 
01971   /* Disable TXEIE and TCIE interrupts */
01972   CLEAR_BIT(hsc->Instance->CR1, (USART_CR1_TXEIE | USART_CR1_TCIE));
01973 }
01974 
01975 
01976 /**
01977   * @brief  End ongoing Rx transfer on SMARTCARD peripheral (following error detection or Reception completion).
01978   * @param  hsc    Pointer to a SMARTCARD_HandleTypeDef structure that contains
01979   *                the configuration information for SMARTCARD module.
01980   * @retval None
01981   */
01982 static void SMARTCARD_EndRxTransfer(SMARTCARD_HandleTypeDef *hsc)
01983 {
01984   /* At end of Rx process, restore hsc->RxState to Ready */
01985   hsc->RxState = HAL_SMARTCARD_STATE_READY;
01986 
01987   /* Disable RXNE, PE and ERR (Frame error, noise error, overrun error) interrupts */
01988   CLEAR_BIT(hsc->Instance->CR1, (USART_CR1_RXNEIE | USART_CR1_PEIE));
01989   CLEAR_BIT(hsc->Instance->CR3, USART_CR3_EIE);
01990 }
01991 
01992 /**
01993   * @brief Send an amount of data in non blocking mode
01994   * @param  hsc    Pointer to a SMARTCARD_HandleTypeDef structure that contains
01995   *                the configuration information for SMARTCARD module.
01996   * @retval HAL status
01997   */
01998 static HAL_StatusTypeDef SMARTCARD_Transmit_IT(SMARTCARD_HandleTypeDef *hsc)
01999 {
02000 
02001   /* Check that a Tx process is ongoing */
02002   if(hsc->gState == HAL_SMARTCARD_STATE_BUSY_TX)
02003   {
02004     hsc->Instance->DR = (uint8_t)(*hsc->pTxBuffPtr & 0xFFU);
02005     hsc->pTxBuffPtr++;
02006 
02007     if(--hsc->TxXferCount == 0U)
02008     {
02009       /* Disable the SMARTCARD Transmit data register empty Interrupt */
02010       CLEAR_BIT(hsc->Instance->CR1, USART_CR1_TXEIE);
02011 
02012       /* Enable the SMARTCARD Transmit Complete Interrupt */
02013       SET_BIT(hsc->Instance->CR1, USART_CR1_TCIE);
02014     }
02015 
02016     return HAL_OK;
02017   }
02018   else
02019   {
02020     return HAL_BUSY;
02021   }
02022 }
02023 
02024 /**
02025   * @brief  Wraps up transmission in non blocking mode.
02026   * @param  hsc    Pointer to a SMARTCARD_HandleTypeDef structure that contains
02027   *                the configuration information for the specified SMARTCARD module.
02028   * @retval HAL status
02029   */
02030 static HAL_StatusTypeDef SMARTCARD_EndTransmit_IT(SMARTCARD_HandleTypeDef *hsc)
02031 {
02032   /* Disable the SMARTCARD Transmit Complete Interrupt */
02033   CLEAR_BIT(hsc->Instance->CR1, USART_CR1_TCIE);
02034 
02035   /* Disable the SMARTCARD Error Interrupt: (Frame error, noise error, overrun error) */
02036   CLEAR_BIT(hsc->Instance->CR3, USART_CR3_EIE);
02037 
02038   /* Tx process is ended, restore hsc->gState to Ready */
02039   hsc->gState = HAL_SMARTCARD_STATE_READY;
02040 
02041 #if (USE_HAL_SMARTCARD_REGISTER_CALLBACKS == 1)
02042   /* Call registered Tx complete callback */
02043   hsc->TxCpltCallback(hsc);
02044 #else
02045   /* Call legacy weak Tx complete callback */
02046   HAL_SMARTCARD_TxCpltCallback(hsc);
02047 #endif /* USE_HAL_SMARTCARD_REGISTER_CALLBACK */
02048 
02049   return HAL_OK;
02050 }
02051 
02052 /**
02053   * @brief Receive an amount of data in non blocking mode
02054   * @param  hsc    Pointer to a SMARTCARD_HandleTypeDef structure that contains
02055   *                the configuration information for SMARTCARD module.
02056   * @retval HAL status
02057   */
02058 static HAL_StatusTypeDef SMARTCARD_Receive_IT(SMARTCARD_HandleTypeDef *hsc)
02059 {
02060 
02061   /* Check that a Rx process is ongoing */
02062   if(hsc->RxState == HAL_SMARTCARD_STATE_BUSY_RX)
02063   {
02064     *hsc->pRxBuffPtr = (uint8_t)(hsc->Instance->DR & (uint8_t)0xFFU);
02065     hsc->pRxBuffPtr++;
02066 
02067     if(--hsc->RxXferCount == 0U)
02068     {
02069       CLEAR_BIT(hsc->Instance->CR1, USART_CR1_RXNEIE);
02070 
02071       /* Disable the SMARTCARD Parity Error Interrupt */
02072       CLEAR_BIT(hsc->Instance->CR1, USART_CR1_PEIE);
02073 
02074       /* Disable the SMARTCARD Error Interrupt: (Frame error, noise error, overrun error) */
02075       CLEAR_BIT(hsc->Instance->CR3, USART_CR3_EIE);
02076 
02077       /* Rx process is completed, restore hsc->RxState to Ready */
02078       hsc->RxState = HAL_SMARTCARD_STATE_READY;
02079 
02080 #if (USE_HAL_SMARTCARD_REGISTER_CALLBACKS == 1)
02081       /* Call registered Rx complete callback */
02082       hsc->RxCpltCallback(hsc);
02083 #else
02084       /* Call legacy weak Rx complete callback */
02085       HAL_SMARTCARD_RxCpltCallback(hsc);
02086 #endif /* USE_HAL_SMARTCARD_REGISTER_CALLBACK */
02087 
02088       return HAL_OK;
02089     }
02090     return HAL_OK;
02091   }
02092   else
02093   {
02094     return HAL_BUSY;
02095   }
02096 }
02097 
02098 /**
02099   * @brief  DMA SMARTCARD communication abort callback, when initiated by HAL services on Error
02100   *         (To be called at end of DMA Abort procedure following error occurrence).
02101   * @param  hdma DMA handle.
02102   * @retval None
02103   */
02104 static void SMARTCARD_DMAAbortOnError(DMA_HandleTypeDef *hdma)
02105 {
02106   SMARTCARD_HandleTypeDef* hsc = (SMARTCARD_HandleTypeDef* )((DMA_HandleTypeDef* )hdma)->Parent;
02107   hsc->RxXferCount = 0x00U;
02108   hsc->TxXferCount = 0x00U;
02109 
02110 #if (USE_HAL_SMARTCARD_REGISTER_CALLBACKS == 1)
02111   /* Call registered user error callback */
02112   hsc->ErrorCallback(hsc);
02113 #else
02114   /* Call legacy weak user error callback */
02115   HAL_SMARTCARD_ErrorCallback(hsc);
02116 #endif /* USE_HAL_SMARTCARD_REGISTER_CALLBACK */
02117 }
02118 
02119 /**
02120   * @brief  DMA SMARTCARD Tx communication abort callback, when initiated by user
02121   *         (To be called at end of DMA Tx Abort procedure following user abort request).
02122   * @note   When this callback is executed, User Abort complete call back is called only if no
02123   *         Abort still ongoing for Rx DMA Handle.
02124   * @param  hdma DMA handle.
02125   * @retval None
02126   */
02127 static void SMARTCARD_DMATxAbortCallback(DMA_HandleTypeDef *hdma)
02128 {
02129   SMARTCARD_HandleTypeDef* hsc = ( SMARTCARD_HandleTypeDef* )((DMA_HandleTypeDef* )hdma)->Parent;
02130 
02131   hsc->hdmatx->XferAbortCallback = NULL;
02132 
02133   /* Check if an Abort process is still ongoing */
02134   if(hsc->hdmarx != NULL)
02135   {
02136     if(hsc->hdmarx->XferAbortCallback != NULL)
02137     {
02138       return;
02139     }
02140   }
02141 
02142   /* No Abort process still ongoing : All DMA channels are aborted, call user Abort Complete callback */
02143   hsc->TxXferCount = 0x00U;
02144   hsc->RxXferCount = 0x00U;
02145 
02146   /* Reset ErrorCode */
02147   hsc->ErrorCode = HAL_SMARTCARD_ERROR_NONE;
02148 
02149   /* Restore hsc->gState and hsc->RxState to Ready */
02150   hsc->gState  = HAL_SMARTCARD_STATE_READY;
02151   hsc->RxState = HAL_SMARTCARD_STATE_READY;
02152 
02153 #if (USE_HAL_SMARTCARD_REGISTER_CALLBACKS == 1)
02154   /* Call registered Abort complete callback */
02155   hsc->AbortCpltCallback(hsc);
02156 #else
02157   /* Call legacy weak Abort complete callback */
02158   HAL_SMARTCARD_AbortCpltCallback(hsc);
02159 #endif /* USE_HAL_SMARTCARD_REGISTER_CALLBACK */
02160 }
02161 
02162 /**
02163   * @brief  DMA SMARTCARD Rx communication abort callback, when initiated by user
02164   *         (To be called at end of DMA Rx Abort procedure following user abort request).
02165   * @note   When this callback is executed, User Abort complete call back is called only if no
02166   *         Abort still ongoing for Tx DMA Handle.
02167   * @param  hdma DMA handle.
02168   * @retval None
02169   */
02170 static void SMARTCARD_DMARxAbortCallback(DMA_HandleTypeDef *hdma)
02171 {
02172   SMARTCARD_HandleTypeDef* hsc = ( SMARTCARD_HandleTypeDef* )((DMA_HandleTypeDef* )hdma)->Parent;
02173 
02174   hsc->hdmarx->XferAbortCallback = NULL;
02175 
02176   /* Check if an Abort process is still ongoing */
02177   if(hsc->hdmatx != NULL)
02178   {
02179     if(hsc->hdmatx->XferAbortCallback != NULL)
02180     {
02181       return;
02182     }
02183   }
02184 
02185   /* No Abort process still ongoing : All DMA channels are aborted, call user Abort Complete callback */
02186   hsc->TxXferCount = 0x00U;
02187   hsc->RxXferCount = 0x00U;
02188 
02189   /* Reset ErrorCode */
02190   hsc->ErrorCode = HAL_SMARTCARD_ERROR_NONE;
02191 
02192   /* Restore hsc->gState and hsc->RxState to Ready */
02193   hsc->gState  = HAL_SMARTCARD_STATE_READY;
02194   hsc->RxState = HAL_SMARTCARD_STATE_READY;
02195 
02196 #if (USE_HAL_SMARTCARD_REGISTER_CALLBACKS == 1)
02197   /* Call registered Abort complete callback */
02198   hsc->AbortCpltCallback(hsc);
02199 #else
02200   /* Call legacy weak Abort complete callback */
02201   HAL_SMARTCARD_AbortCpltCallback(hsc);
02202 #endif /* USE_HAL_SMARTCARD_REGISTER_CALLBACK */
02203 }
02204 
02205 /**
02206   * @brief  DMA SMARTCARD Tx communication abort callback, when initiated by user by a call to
02207   *         HAL_SMARTCARD_AbortTransmit_IT API (Abort only Tx transfer)
02208   *         (This callback is executed at end of DMA Tx Abort procedure following user abort request,
02209   *         and leads to user Tx Abort Complete callback execution).
02210   * @param  hdma DMA handle.
02211   * @retval None
02212   */
02213 static void SMARTCARD_DMATxOnlyAbortCallback(DMA_HandleTypeDef *hdma)
02214 {
02215   SMARTCARD_HandleTypeDef* hsc = ( SMARTCARD_HandleTypeDef* )((DMA_HandleTypeDef* )hdma)->Parent;
02216 
02217   hsc->TxXferCount = 0x00U;
02218 
02219   /* Restore hsc->gState to Ready */
02220   hsc->gState = HAL_SMARTCARD_STATE_READY;
02221 
02222 #if (USE_HAL_SMARTCARD_REGISTER_CALLBACKS == 1)
02223   /* Call registered Abort Transmit Complete Callback */
02224   hsc->AbortTransmitCpltCallback(hsc);
02225 #else
02226   /* Call legacy weak Abort Transmit Complete Callback */
02227   HAL_SMARTCARD_AbortTransmitCpltCallback(hsc);
02228 #endif /* USE_HAL_SMARTCARD_REGISTER_CALLBACK */
02229 }
02230 
02231 /**
02232   * @brief  DMA SMARTCARD Rx communication abort callback, when initiated by user by a call to
02233   *         HAL_SMARTCARD_AbortReceive_IT API (Abort only Rx transfer)
02234   *         (This callback is executed at end of DMA Rx Abort procedure following user abort request,
02235   *         and leads to user Rx Abort Complete callback execution).
02236   * @param  hdma DMA handle.
02237   * @retval None
02238   */
02239 static void SMARTCARD_DMARxOnlyAbortCallback(DMA_HandleTypeDef *hdma)
02240 {
02241   SMARTCARD_HandleTypeDef* hsc = ( SMARTCARD_HandleTypeDef* )((DMA_HandleTypeDef* )hdma)->Parent;
02242 
02243   hsc->RxXferCount = 0x00U;
02244 
02245   /* Restore hsc->RxState to Ready */
02246   hsc->RxState = HAL_SMARTCARD_STATE_READY;
02247 
02248 #if (USE_HAL_SMARTCARD_REGISTER_CALLBACKS == 1)
02249   /* Call registered Abort Receive Complete Callback */
02250   hsc->AbortReceiveCpltCallback(hsc);
02251 #else
02252   /* Call legacy weak Abort Receive Complete Callback */
02253   HAL_SMARTCARD_AbortReceiveCpltCallback(hsc);
02254 #endif /* USE_HAL_SMARTCARD_REGISTER_CALLBACK */
02255 }
02256 
02257 /**
02258   * @brief Configure the SMARTCARD peripheral
02259   * @param  hsc    Pointer to a SMARTCARD_HandleTypeDef structure that contains
02260   *                the configuration information for SMARTCARD module.
02261   * @retval None
02262   */
02263 static void SMARTCARD_SetConfig(SMARTCARD_HandleTypeDef *hsc)
02264 {
02265   uint32_t tmpreg = 0x00U;
02266   uint32_t pclk;
02267 
02268   /* Check the parameters */
02269   assert_param(IS_SMARTCARD_INSTANCE(hsc->Instance));
02270   assert_param(IS_SMARTCARD_POLARITY(hsc->Init.CLKPolarity));
02271   assert_param(IS_SMARTCARD_PHASE(hsc->Init.CLKPhase));
02272   assert_param(IS_SMARTCARD_LASTBIT(hsc->Init.CLKLastBit));
02273   assert_param(IS_SMARTCARD_BAUDRATE(hsc->Init.BaudRate));
02274   assert_param(IS_SMARTCARD_WORD_LENGTH(hsc->Init.WordLength));
02275   assert_param(IS_SMARTCARD_STOPBITS(hsc->Init.StopBits));
02276   assert_param(IS_SMARTCARD_PARITY(hsc->Init.Parity));
02277   assert_param(IS_SMARTCARD_MODE(hsc->Init.Mode));
02278   assert_param(IS_SMARTCARD_NACK_STATE(hsc->Init.NACKState));
02279 
02280   /* The LBCL, CPOL and CPHA bits have to be selected when both the transmitter and the
02281      receiver are disabled (TE=RE=0) to ensure that the clock pulses function correctly. */
02282   CLEAR_BIT(hsc->Instance->CR1, (USART_CR1_TE | USART_CR1_RE));
02283 
02284   /*---------------------------- USART CR2 Configuration ---------------------*/
02285   tmpreg = hsc->Instance->CR2;
02286   /* Clear CLKEN, CPOL, CPHA and LBCL bits */
02287   tmpreg &= (uint32_t)~((uint32_t)(USART_CR2_CPHA | USART_CR2_CPOL | USART_CR2_CLKEN | USART_CR2_LBCL));
02288   /* Configure the SMARTCARD Clock, CPOL, CPHA and LastBit -----------------------*/
02289   /* Set CPOL bit according to hsc->Init.CLKPolarity value */
02290   /* Set CPHA bit according to hsc->Init.CLKPhase value */
02291   /* Set LBCL bit according to hsc->Init.CLKLastBit value */
02292   /* Set Stop Bits: Set STOP[13:12] bits according to hsc->Init.StopBits value */
02293   tmpreg |= (uint32_t)(USART_CR2_CLKEN | hsc->Init.CLKPolarity |
02294                       hsc->Init.CLKPhase| hsc->Init.CLKLastBit | hsc->Init.StopBits);
02295   /* Write to USART CR2 */
02296   WRITE_REG(hsc->Instance->CR2, (uint32_t)tmpreg);
02297 
02298   tmpreg = hsc->Instance->CR2;
02299 
02300   /* Clear STOP[13:12] bits */
02301   tmpreg &= (uint32_t)~((uint32_t)USART_CR2_STOP);
02302 
02303   /* Set Stop Bits: Set STOP[13:12] bits according to hsc->Init.StopBits value */
02304   tmpreg |= (uint32_t)(hsc->Init.StopBits);
02305 
02306   /* Write to USART CR2 */
02307   WRITE_REG(hsc->Instance->CR2, (uint32_t)tmpreg);
02308 
02309   /*-------------------------- USART CR1 Configuration -----------------------*/
02310   tmpreg = hsc->Instance->CR1;
02311 
02312   /* Clear M, PCE, PS, TE and RE bits */
02313   tmpreg &= (uint32_t)~((uint32_t)(USART_CR1_M | USART_CR1_PCE | USART_CR1_PS | USART_CR1_TE | \
02314                                    USART_CR1_RE));
02315 
02316   /* Configure the SMARTCARD Word Length, Parity and mode:
02317      Set the M bits according to hsc->Init.WordLength value
02318      Set PCE and PS bits according to hsc->Init.Parity value
02319      Set TE and RE bits according to hsc->Init.Mode value */
02320   tmpreg |= (uint32_t)hsc->Init.WordLength | hsc->Init.Parity | hsc->Init.Mode;
02321 
02322   /* Write to USART CR1 */
02323   WRITE_REG(hsc->Instance->CR1, (uint32_t)tmpreg);
02324 
02325   /*-------------------------- USART CR3 Configuration -----------------------*/
02326   /* Clear CTSE and RTSE bits */
02327   CLEAR_BIT(hsc->Instance->CR3, (USART_CR3_RTSE | USART_CR3_CTSE));
02328 
02329   /*-------------------------- USART BRR Configuration -----------------------*/
02330 #if defined(USART6)
02331   if((hsc->Instance == USART1) || (hsc->Instance == USART6))
02332   {
02333     pclk = HAL_RCC_GetPCLK2Freq();
02334     hsc->Instance->BRR = SMARTCARD_BRR(pclk, hsc->Init.BaudRate);
02335   }
02336 #else
02337   if(hsc->Instance == USART1)
02338   {
02339     pclk = HAL_RCC_GetPCLK2Freq();
02340     hsc->Instance->BRR = SMARTCARD_BRR(pclk, hsc->Init.BaudRate);
02341   }
02342 #endif /* USART6 */
02343   else
02344   {
02345     pclk = HAL_RCC_GetPCLK1Freq();
02346     hsc->Instance->BRR = SMARTCARD_BRR(pclk, hsc->Init.BaudRate);
02347   }
02348 }
02349 
02350 /**
02351   * @}
02352   */
02353 
02354 #endif /* HAL_SMARTCARD_MODULE_ENABLED */
02355 /**
02356   * @}
02357   */
02358 
02359 /**
02360   * @}
02361   */
02362 
02363 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/