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