L4 HAL Drivers

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers stm32l4xx_hal_smartcard.c Source File

stm32l4xx_hal_smartcard.c

Go to the documentation of this file.
00001 /**
00002   ******************************************************************************
00003   * @file    stm32l4xx_hal_smartcard.c
00004   * @author  MCD Application Team
00005   * @version V1.1.0
00006   * @date    16-September-2015
00007   * @brief   SMARTCARD HAL module driver.
00008   *          This file provides firmware functions to manage the following
00009   *          functionalities of the SMARTCARD peripheral:
00010   *           + Initialization and de-initialization functions
00011   *           + IO operation functions
00012   *           + Peripheral Control functions
00013   *           + Peripheral State and Error functions
00014   *
00015   @verbatim
00016   ==============================================================================
00017                         ##### How to use this driver #####
00018   ==============================================================================
00019   [..]
00020     The SMARTCARD HAL driver can be used as follows:
00021 
00022     (#) Declare a SMARTCARD_HandleTypeDef handle structure (eg. SMARTCARD_HandleTypeDef hsmartcard).
00023     (#) Associate a USART to the SMARTCARD handle hsmartcard.
00024     (#) Initialize the SMARTCARD low level resources by implementing the HAL_SMARTCARD_MspInit() API:
00025         (++) Enable the USARTx interface clock.
00026         (++) USART pins configuration:
00027              (+++) Enable the clock for the USART GPIOs.
00028              (+++) Configure the USART pins (TX as alternate function pull-up, RX as alternate function Input).
00029         (++) NVIC configuration if you need to use interrupt process (HAL_SMARTCARD_Transmit_IT()
00030              and HAL_SMARTCARD_Receive_IT() APIs):
00031              (++) Configure the USARTx interrupt priority.
00032              (++) Enable the NVIC USART IRQ handle.
00033         (++) DMA Configuration if you need to use DMA process (HAL_SMARTCARD_Transmit_DMA()
00034              and HAL_SMARTCARD_Receive_DMA() APIs):
00035              (+++) Declare a DMA handle structure for the Tx/Rx channel.
00036              (+++) Enable the DMAx interface clock.
00037              (+++) Configure the declared DMA handle structure with the required Tx/Rx parameters.
00038              (+++) Configure the DMA Tx/Rx channel.
00039              (+++) Associate the initialized DMA handle to the SMARTCARD DMA Tx/Rx handle.
00040              (+++) Configure the priority and enable the NVIC for the transfer complete interrupt on the DMA Tx/Rx channel.
00041 
00042     (#) Program the Baud Rate, Parity, Mode(Receiver/Transmitter), clock enabling/disabling and accordingly,
00043         the clock parameters (parity, phase, last bit), prescaler value, guard time and NACK on transmission
00044         error enabling or disabling in the hsmartcard handle Init structure.
00045 
00046     (#) If required, program SMARTCARD advanced features (TX/RX pins swap, TimeOut, auto-retry counter,...)
00047         in the hsmartcard handle AdvancedInit structure.
00048 
00049     (#) Initialize the SMARTCARD registers by calling the HAL_SMARTCARD_Init() API:
00050         (++) This API configures also the low level Hardware GPIO, CLOCK, CORTEX...etc)
00051              by calling the customized HAL_SMARTCARD_MspInit() API.
00052         [..]
00053         (@) The specific SMARTCARD interrupts (Transmission complete interrupt,
00054              RXNE interrupt and Error Interrupts) will be managed using the macros
00055              __HAL_SMARTCARD_ENABLE_IT() and __HAL_SMARTCARD_DISABLE_IT() inside the transmit and receive process.
00056 
00057     [..]
00058     [..] Three operation modes are available within this driver :
00059 
00060      *** Polling mode IO operation ***
00061      =================================
00062      [..]
00063        (+) Send an amount of data in blocking mode using HAL_SMARTCARD_Transmit()
00064        (+) Receive an amount of data in blocking mode using HAL_SMARTCARD_Receive()
00065 
00066      *** Interrupt mode IO operation ***
00067      ===================================
00068      [..]
00069        (+) Send an amount of data in non-blocking mode using HAL_SMARTCARD_Transmit_IT()
00070        (+) At transmission end of transfer HAL_SMARTCARD_TxCpltCallback() is executed and user can
00071             add his own code by customization of function pointer HAL_SMARTCARD_TxCpltCallback()
00072        (+) Receive an amount of data in non-blocking mode using HAL_SMARTCARD_Receive_IT()
00073        (+) At reception end of transfer HAL_SMARTCARD_RxCpltCallback() is executed and user can
00074             add his own code by customization of function pointer HAL_SMARTCARD_RxCpltCallback()
00075        (+) In case of transfer Error, HAL_SMARTCARD_ErrorCallback() function is executed and user can
00076             add his own code by customization of function pointer HAL_SMARTCARD_ErrorCallback()
00077 
00078      *** DMA mode IO operation ***
00079      ==============================
00080      [..]
00081        (+) Send an amount of data in non-blocking mode (DMA) using HAL_SMARTCARD_Transmit_DMA()
00082        (+) At transmission end of transfer HAL_SMARTCARD_TxCpltCallback() is executed and user can
00083             add his own code by customization of function pointer HAL_SMARTCARD_TxCpltCallback()
00084        (+) Receive an amount of data in non-blocking mode (DMA) using HAL_SMARTCARD_Receive_DMA()
00085        (+) At reception end of transfer HAL_SMARTCARD_RxCpltCallback() is executed and user can
00086             add his own code by customization of function pointer HAL_SMARTCARD_RxCpltCallback()
00087        (+) In case of transfer Error, HAL_SMARTCARD_ErrorCallback() function is executed and user can
00088             add his own code by customization of function pointer HAL_SMARTCARD_ErrorCallback()
00089 
00090      *** SMARTCARD HAL driver macros list ***
00091      ========================================
00092      [..]
00093        Below the list of most used macros in SMARTCARD HAL driver.
00094 
00095        (+) __HAL_SMARTCARD_GET_FLAG : Check whether or not the specified SMARTCARD flag is set
00096        (+) __HAL_SMARTCARD_CLEAR_FLAG : Clear the specified SMARTCARD pending flag
00097        (+) __HAL_SMARTCARD_ENABLE_IT: Enable the specified SMARTCARD interrupt
00098        (+) __HAL_SMARTCARD_DISABLE_IT: Disable the specified SMARTCARD interrupt
00099        (+) __HAL_SMARTCARD_GET_IT_SOURCE: Check whether or not the specified SMARTCARD interrupt is enabled
00100 
00101      [..]
00102        (@) You can refer to the SMARTCARD HAL driver header file for more useful macros
00103 
00104   @endverbatim
00105   ******************************************************************************
00106   * @attention
00107   *
00108   * <h2><center>&copy; COPYRIGHT(c) 2015 STMicroelectronics</center></h2>
00109   *
00110   * Redistribution and use in source and binary forms, with or without modification,
00111   * are permitted provided that the following conditions are met:
00112   *   1. Redistributions of source code must retain the above copyright notice,
00113   *      this list of conditions and the following disclaimer.
00114   *   2. Redistributions in binary form must reproduce the above copyright notice,
00115   *      this list of conditions and the following disclaimer in the documentation
00116   *      and/or other materials provided with the distribution.
00117   *   3. Neither the name of STMicroelectronics nor the names of its contributors
00118   *      may be used to endorse or promote products derived from this software
00119   *      without specific prior written permission.
00120   *
00121   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
00122   * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00123   * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
00124   * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
00125   * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
00126   * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
00127   * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
00128   * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
00129   * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
00130   * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00131   *
00132   ******************************************************************************
00133   */
00134 
00135 /* Includes ------------------------------------------------------------------*/
00136 #include "stm32l4xx_hal.h"
00137 
00138 /** @addtogroup STM32L4xx_HAL_Driver
00139   * @{
00140   */
00141 
00142 /** @defgroup SMARTCARD SMARTCARD
00143   * @brief HAL SMARTCARD module driver
00144   * @{
00145   */
00146 
00147 #ifdef HAL_SMARTCARD_MODULE_ENABLED
00148 
00149 /* Private typedef -----------------------------------------------------------*/
00150 /* Private define ------------------------------------------------------------*/
00151 /** @defgroup SMARTCARD_Private_Constants SMARTCARD Private Constants
00152  * @{
00153  */
00154 #define SMARTCARD_TEACK_REACK_TIMEOUT               1000      /*!< SMARTCARD TX or RX enable acknowledge time-out value  */
00155 
00156 #define USART_CR1_FIELDS      ((uint32_t)(USART_CR1_M | USART_CR1_PCE | USART_CR1_PS | \
00157                                      USART_CR1_TE | USART_CR1_RE | USART_CR1_OVER8))       /*!< USART CR1 fields of parameters set by SMARTCARD_SetConfig API */
00158 #define USART_CR2_CLK_FIELDS  ((uint32_t)(USART_CR2_CLKEN|USART_CR2_CPOL|USART_CR2_CPHA|USART_CR2_LBCL)) /*!< SMARTCARD clock-related USART CR2 fields of parameters */
00159 #define USART_CR2_FIELDS      ((uint32_t)(USART_CR2_RTOEN|USART_CR2_CLK_FIELDS|USART_CR2_STOP)) /*!< USART CR2 fields of parameters set by SMARTCARD_SetConfig API */
00160 #define USART_CR3_FIELDS      ((uint32_t)(USART_CR3_ONEBIT|USART_CR3_NACK|USART_CR3_SCARCNT))   /*!< USART CR3 fields of parameters set by SMARTCARD_SetConfig API */
00161 /**
00162   * @}
00163   */
00164 
00165 /* Private macros ------------------------------------------------------------*/
00166 /* Private variables ---------------------------------------------------------*/
00167 /* Private function prototypes -----------------------------------------------*/
00168 /** @addtogroup SMARTCARD_Private_Functions
00169   * @{
00170   */
00171 static void SMARTCARD_DMATransmitCplt(DMA_HandleTypeDef *hdma);
00172 static void SMARTCARD_DMAReceiveCplt(DMA_HandleTypeDef *hdma);
00173 static void SMARTCARD_DMAError(DMA_HandleTypeDef *hdma);
00174 static HAL_StatusTypeDef SMARTCARD_SetConfig(SMARTCARD_HandleTypeDef *hsmartcard);
00175 static void SMARTCARD_AdvFeatureConfig(SMARTCARD_HandleTypeDef *hsmartcard);
00176 static HAL_StatusTypeDef SMARTCARD_WaitOnFlagUntilTimeout(SMARTCARD_HandleTypeDef *hsmartcard, uint32_t Flag, FlagStatus Status, uint32_t Timeout);
00177 static HAL_StatusTypeDef SMARTCARD_CheckIdleState(SMARTCARD_HandleTypeDef *hsmartcard);
00178 static HAL_StatusTypeDef SMARTCARD_Transmit_IT(SMARTCARD_HandleTypeDef *hsmartcard);
00179 static HAL_StatusTypeDef SMARTCARD_EndTransmit_IT(SMARTCARD_HandleTypeDef *hsmartcard);
00180 static HAL_StatusTypeDef SMARTCARD_Receive_IT(SMARTCARD_HandleTypeDef *hsmartcard);
00181 /**
00182   * @}
00183   */
00184 
00185 /* Exported functions --------------------------------------------------------*/
00186 
00187 /** @defgroup SMARTCARD_Exported_Functions SMARTCARD Exported Functions
00188   * @{
00189   */
00190 
00191 /** @defgroup SMARTCARD_Exported_Functions_Group1 Initialization and de-initialization functions
00192   *  @brief    Initialization and Configuration functions
00193   *
00194 @verbatim
00195   ===============================================================================
00196               ##### Initialization and Configuration functions #####
00197   ===============================================================================
00198   [..]
00199   This subsection provides a set of functions allowing to initialize the USARTx
00200   associated to the SmartCard.
00201   (+) These parameters can be configured:
00202       (++) Baud Rate
00203       (++) Parity: parity should be enabled,
00204            Frame Length is fixed to 8 bits plus parity:
00205            the USART frame format is given in the following table:
00206 
00207       (+++) Table 1. USART frame format.
00208       (+++) +---------------------------------------------------------------+        
00209       (+++) | M1M0 bits |  PCE bit  |            USART frame                |        
00210       (+++) |-----------------------|---------------------------------------|        
00211       (+++) |     01    |    1      |    | SB | 8 bit data | PB | STB |     |        
00212       (+++) +---------------------------------------------------------------+        
00213 
00214       (++) Receiver/transmitter modes
00215       (++) Synchronous mode (and if enabled, phase, polarity and last bit parameters)
00216       (++) Prescaler value
00217       (++) Guard bit time
00218       (++) NACK enabling or disabling on transmission error
00219 
00220   (+) The following advanced features can be configured as well:
00221       (++) TX and/or RX pin level inversion
00222       (++) data logical level inversion
00223       (++) RX and TX pins swap
00224       (++) RX overrun detection disabling
00225       (++) DMA disabling on RX error
00226       (++) MSB first on communication line
00227       (++) Time out enabling (and if activated, timeout value)
00228       (++) Block length
00229       (++) Auto-retry counter
00230   [..]
00231   The HAL_SMARTCARD_Init() API follows the USART synchronous configuration procedures
00232   (details for the procedures are available in reference manual).
00233 
00234 @endverbatim
00235   * @{
00236   */
00237 
00238 /**
00239   * @brief Initialize the SMARTCARD mode according to the specified
00240   *         parameters in the SMARTCARD_HandleTypeDef and initialize the associated handle.
00241   * @param hsmartcard: Pointer to a SMARTCARD_HandleTypeDef structure that contains
00242   *                    the configuration information for the specified SMARTCARD module.
00243   * @retval HAL status
00244   */
00245 HAL_StatusTypeDef HAL_SMARTCARD_Init(SMARTCARD_HandleTypeDef *hsmartcard)
00246 {
00247   /* Check the SMARTCARD handle allocation */
00248   if(hsmartcard == NULL)
00249   {
00250     return HAL_ERROR;
00251   }
00252 
00253   /* Check the USART associated to the SmartCard */
00254   assert_param(IS_SMARTCARD_INSTANCE(hsmartcard->Instance));
00255 
00256   if(hsmartcard->State == HAL_SMARTCARD_STATE_RESET)
00257   {
00258     /* Allocate lock resource and initialize it */
00259     hsmartcard->Lock = HAL_UNLOCKED;
00260 
00261     /* Init the low level hardware : GPIO, CLOCK */
00262     HAL_SMARTCARD_MspInit(hsmartcard);
00263   }
00264 
00265   hsmartcard->State = HAL_SMARTCARD_STATE_BUSY;
00266 
00267   /* Disable the Peripheral to set smartcard mode */
00268   CLEAR_BIT(hsmartcard->Instance->CR1, USART_CR1_UE);
00269 
00270   /* In SmartCard mode, the following bits must be kept cleared:
00271   - LINEN in the USART_CR2 register,
00272   - HDSEL and IREN  bits in the USART_CR3 register.*/
00273   CLEAR_BIT(hsmartcard->Instance->CR2, USART_CR2_LINEN);
00274   CLEAR_BIT(hsmartcard->Instance->CR3, (USART_CR3_HDSEL | USART_CR3_IREN));
00275 
00276   /* set the USART in SMARTCARD mode */
00277   SET_BIT(hsmartcard->Instance->CR3, USART_CR3_SCEN);
00278 
00279   /* Set the SMARTCARD Communication parameters */
00280   if (SMARTCARD_SetConfig(hsmartcard) == HAL_ERROR)
00281   {
00282     return HAL_ERROR;
00283   }
00284 
00285   if (hsmartcard->AdvancedInit.AdvFeatureInit != SMARTCARD_ADVFEATURE_NO_INIT)
00286   {
00287     SMARTCARD_AdvFeatureConfig(hsmartcard);
00288   }
00289 
00290   /* Enable the Peripheral */
00291   SET_BIT(hsmartcard->Instance->CR1, USART_CR1_UE);
00292 
00293   /* TEACK and/or REACK to check before moving hsmartcard->State to Ready */
00294   return (SMARTCARD_CheckIdleState(hsmartcard));
00295 }
00296 
00297 
00298 /**
00299   * @brief DeInitialize the SMARTCARD peripheral.
00300   * @param hsmartcard: Pointer to a SMARTCARD_HandleTypeDef structure that contains
00301   *                    the configuration information for the specified SMARTCARD module.
00302   * @retval HAL status
00303   */
00304 HAL_StatusTypeDef HAL_SMARTCARD_DeInit(SMARTCARD_HandleTypeDef *hsmartcard)
00305 {
00306   /* Check the SMARTCARD handle allocation */
00307   if(hsmartcard == NULL)
00308   {
00309     return HAL_ERROR;
00310   }
00311 
00312   /* Check the parameters */
00313   assert_param(IS_SMARTCARD_INSTANCE(hsmartcard->Instance));
00314 
00315   hsmartcard->State = HAL_SMARTCARD_STATE_BUSY;
00316 
00317   /* Disable the Peripheral */
00318   CLEAR_BIT(hsmartcard->Instance->CR1, USART_CR1_UE);
00319 
00320   WRITE_REG(hsmartcard->Instance->CR1, 0x0);
00321   WRITE_REG(hsmartcard->Instance->CR2, 0x0);
00322   WRITE_REG(hsmartcard->Instance->CR3, 0x0);
00323   WRITE_REG(hsmartcard->Instance->RTOR, 0x0);
00324   WRITE_REG(hsmartcard->Instance->GTPR, 0x0);
00325 
00326   /* DeInit the low level hardware */
00327   HAL_SMARTCARD_MspDeInit(hsmartcard);
00328 
00329   hsmartcard->ErrorCode = HAL_SMARTCARD_ERROR_NONE;
00330   hsmartcard->State = HAL_SMARTCARD_STATE_RESET;
00331 
00332   /* Process Unlock */
00333   __HAL_UNLOCK(hsmartcard);
00334 
00335   return HAL_OK;
00336 }
00337 
00338 /**
00339   * @brief Initialize the SMARTCARD MSP.
00340   * @param hsmartcard: Pointer to a SMARTCARD_HandleTypeDef structure that contains
00341   *                    the configuration information for the specified SMARTCARD module.
00342   * @retval None
00343   */
00344  __weak void HAL_SMARTCARD_MspInit(SMARTCARD_HandleTypeDef *hsmartcard)
00345 {
00346   /* NOTE : This function should not be modified, when the callback is needed,
00347             the HAL_SMARTCARD_MspInit can be implemented in the user file
00348    */
00349 }
00350 
00351 /**
00352   * @brief DeInitialize the SMARTCARD MSP.
00353   * @param hsmartcard: Pointer to a SMARTCARD_HandleTypeDef structure that contains
00354   *                    the configuration information for the specified SMARTCARD module.
00355   * @retval None
00356   */
00357  __weak void HAL_SMARTCARD_MspDeInit(SMARTCARD_HandleTypeDef *hsmartcard)
00358 {
00359   /* NOTE : This function should not be modified, when the callback is needed,
00360             the HAL_SMARTCARD_MspDeInit can be implemented in the user file
00361    */
00362 }
00363 
00364 /**
00365   * @}
00366   */
00367 
00368 /** @defgroup SMARTCARD_Exported_Functions_Group2 IO operation functions
00369   *  @brief   SMARTCARD Transmit and Receive functions
00370   *
00371 @verbatim
00372   ==============================================================================
00373                          ##### IO operation functions #####
00374   ==============================================================================
00375   [..]
00376     This subsection provides a set of functions allowing to manage the SMARTCARD data transfers.
00377 
00378   [..]
00379     Smartcard is a single wire half duplex communication protocol.
00380     The Smartcard interface is designed to support asynchronous protocol Smartcards as
00381     defined in the ISO 7816-3 standard. The USART should be configured as:
00382     (+) 8 bits plus parity: where M=1 and PCE=1 in the USART_CR1 register
00383     (+) 1.5 stop bits when transmitting and receiving: where STOP=11 in the USART_CR2 register.
00384 
00385   [..]
00386     (+) There are two modes of transfer:
00387         (++) Blocking mode: The communication is performed in polling mode.
00388              The HAL status of all data processing is returned by the same function
00389              after finishing transfer.
00390         (++) No-Blocking mode: The communication is performed using Interrupts
00391              or DMA, the relevant API's return the HAL status.
00392              The end of the data processing will be indicated through the
00393              dedicated SMARTCARD IRQ when using Interrupt mode or the DMA IRQ when
00394              using DMA mode.
00395         (++) The HAL_SMARTCARD_TxCpltCallback(), HAL_SMARTCARD_RxCpltCallback() user callbacks
00396              will be executed respectively at the end of the Transmit or Receive process
00397              The HAL_SMARTCARD_ErrorCallback() user callback will be executed when a communication
00398              error is detected.
00399 
00400     (+) Blocking mode APIs are :
00401         (++) HAL_SMARTCARD_Transmit()
00402         (++) HAL_SMARTCARD_Receive()
00403 
00404     (+) Non Blocking mode APIs with Interrupt are :
00405         (++) HAL_SMARTCARD_Transmit_IT()
00406         (++) HAL_SMARTCARD_Receive_IT()
00407         (++) HAL_SMARTCARD_IRQHandler()
00408 
00409     (+) Non Blocking mode functions with DMA are :
00410         (++) HAL_SMARTCARD_Transmit_DMA()
00411         (++) HAL_SMARTCARD_Receive_DMA()
00412 
00413     (+) A set of Transfer Complete Callbacks are provided in non Blocking mode:
00414         (++) HAL_SMARTCARD_TxCpltCallback()
00415         (++) HAL_SMARTCARD_RxCpltCallback()
00416         (++) HAL_SMARTCARD_ErrorCallback()
00417 
00418 @endverbatim
00419   * @{
00420   */
00421 
00422 /**
00423   * @brief Send an amount of data in blocking mode.
00424   * @param hsmartcard: Pointer to a SMARTCARD_HandleTypeDef structure that contains
00425   *                    the configuration information for the specified SMARTCARD module.
00426   * @param pData: pointer to data buffer.
00427   * @param Size: amount of data to be sent.
00428   * @param Timeout : Timeout duration.
00429   * @retval HAL status
00430   */
00431 HAL_StatusTypeDef HAL_SMARTCARD_Transmit(SMARTCARD_HandleTypeDef *hsmartcard, uint8_t *pData, uint16_t Size, uint32_t Timeout)
00432 {
00433   if ((hsmartcard->State == HAL_SMARTCARD_STATE_READY) || (hsmartcard->State == HAL_SMARTCARD_STATE_BUSY_RX))
00434   {
00435     if((pData == NULL) || (Size == 0))
00436     {
00437       return  HAL_ERROR;
00438     }
00439 
00440     /* Process Locked */
00441     __HAL_LOCK(hsmartcard);
00442 
00443     /* Check if a receive process is ongoing or not */
00444     if(hsmartcard->State == HAL_SMARTCARD_STATE_BUSY_RX)
00445     {
00446       hsmartcard->State = HAL_SMARTCARD_STATE_BUSY_TX_RX;
00447     }
00448     else
00449     {
00450       hsmartcard->State = HAL_SMARTCARD_STATE_BUSY_TX;
00451     }
00452 
00453     /* Disable the Peripheral first to update mode for TX master */
00454     CLEAR_BIT(hsmartcard->Instance->CR1, USART_CR1_UE);
00455 
00456     /* Disable Rx, enable Tx */
00457     CLEAR_BIT(hsmartcard->Instance->CR1, USART_CR1_RE);
00458     SET_BIT(hsmartcard->Instance->RQR, SMARTCARD_RXDATA_FLUSH_REQUEST);
00459     SET_BIT(hsmartcard->Instance->CR1, USART_CR1_TE);
00460     
00461     /* Enable the Peripheral */
00462     SET_BIT(hsmartcard->Instance->CR1, USART_CR1_UE);
00463 
00464     hsmartcard->ErrorCode = HAL_SMARTCARD_ERROR_NONE;
00465     hsmartcard->TxXferSize = Size;
00466     hsmartcard->TxXferCount = Size;
00467 
00468     while(hsmartcard->TxXferCount > 0)
00469     {
00470       hsmartcard->TxXferCount--;
00471       if(SMARTCARD_WaitOnFlagUntilTimeout(hsmartcard, SMARTCARD_FLAG_TXE, RESET, Timeout) != HAL_OK)
00472       {
00473         return HAL_TIMEOUT;
00474       }
00475       hsmartcard->Instance->TDR = (*pData++ & (uint8_t)0xFF);
00476     }
00477     if(SMARTCARD_WaitOnFlagUntilTimeout(hsmartcard, SMARTCARD_FLAG_TC, RESET, Timeout) != HAL_OK)
00478     {
00479       return HAL_TIMEOUT;
00480     }
00481     /* Re-enable Rx at end of transmission if initial mode is Rx/Tx */
00482     if(hsmartcard->Init.Mode == SMARTCARD_MODE_TX_RX)
00483     {
00484       /* Disable the Peripheral first to update modes */
00485       CLEAR_BIT(hsmartcard->Instance->CR1, USART_CR1_UE);
00486       SET_BIT(hsmartcard->Instance->CR1, USART_CR1_RE);
00487       /* Enable the Peripheral */
00488       SET_BIT(hsmartcard->Instance->CR1, USART_CR1_UE);
00489     }
00490     
00491     /* Check if a receive process is ongoing or not */
00492     if(hsmartcard->State == HAL_SMARTCARD_STATE_BUSY_TX_RX)
00493     {
00494       hsmartcard->State = HAL_SMARTCARD_STATE_BUSY_RX;
00495     }
00496     else
00497     {
00498       hsmartcard->State = HAL_SMARTCARD_STATE_READY;
00499     }
00500 
00501     /* Process Unlocked */
00502     __HAL_UNLOCK(hsmartcard);
00503 
00504     return HAL_OK;
00505   }
00506   else
00507   {
00508     return HAL_BUSY;
00509   }
00510 }
00511 
00512 /**
00513   * @brief Receive an amount of data in blocking mode.
00514   * @param hsmartcard: Pointer to a SMARTCARD_HandleTypeDef structure that contains
00515   *                    the configuration information for the specified SMARTCARD module.
00516   * @param pData: pointer to data buffer.
00517   * @param Size: amount of data to be received.
00518   * @param Timeout : Timeout duration.
00519   * @retval HAL status
00520   */
00521 HAL_StatusTypeDef HAL_SMARTCARD_Receive(SMARTCARD_HandleTypeDef *hsmartcard, uint8_t *pData, uint16_t Size, uint32_t Timeout)
00522 {
00523   if ((hsmartcard->State == HAL_SMARTCARD_STATE_READY) || (hsmartcard->State == HAL_SMARTCARD_STATE_BUSY_TX))
00524   {
00525     if((pData == NULL) || (Size == 0))
00526     {
00527       return  HAL_ERROR;
00528     }
00529 
00530     /* Process Locked */
00531     __HAL_LOCK(hsmartcard);
00532 
00533     /* Check if a non-blocking transmit process is ongoing or not */
00534     if(hsmartcard->State == HAL_SMARTCARD_STATE_BUSY_TX)
00535     {
00536       hsmartcard->State = HAL_SMARTCARD_STATE_BUSY_TX_RX;
00537     }
00538     else
00539     {
00540       hsmartcard->State = HAL_SMARTCARD_STATE_BUSY_RX;
00541     }
00542 
00543     hsmartcard->ErrorCode = HAL_SMARTCARD_ERROR_NONE;
00544     hsmartcard->RxXferSize = Size;
00545     hsmartcard->RxXferCount = Size;
00546 
00547     /* Check the remain data to be received */
00548     while(hsmartcard->RxXferCount > 0)
00549     {
00550       hsmartcard->RxXferCount--;
00551       if(SMARTCARD_WaitOnFlagUntilTimeout(hsmartcard, SMARTCARD_FLAG_RXNE, RESET, Timeout) != HAL_OK)
00552       {
00553         return HAL_TIMEOUT;
00554       }
00555       *pData++ = (uint8_t)(hsmartcard->Instance->RDR & (uint8_t)0x00FF);
00556     }
00557 
00558     /* Check if a non-blocking transmit process is ongoing or not */
00559     if(hsmartcard->State == HAL_SMARTCARD_STATE_BUSY_TX_RX)
00560     {
00561       hsmartcard->State = HAL_SMARTCARD_STATE_BUSY_TX;
00562     }
00563     else
00564     {
00565     hsmartcard->State = HAL_SMARTCARD_STATE_READY;
00566     }
00567 
00568     /* Process Unlocked */
00569     __HAL_UNLOCK(hsmartcard);
00570 
00571     return HAL_OK;
00572   }
00573   else
00574   {
00575     return HAL_BUSY;
00576   }
00577 }
00578 
00579 /**
00580   * @brief Send an amount of data in interrupt mode.
00581   * @param hsmartcard: Pointer to a SMARTCARD_HandleTypeDef structure that contains
00582   *                    the configuration information for the specified SMARTCARD module.
00583   * @param pData: pointer to data buffer.
00584   * @param Size: amount of data to be sent.
00585   * @retval HAL status
00586   */
00587 HAL_StatusTypeDef HAL_SMARTCARD_Transmit_IT(SMARTCARD_HandleTypeDef *hsmartcard, uint8_t *pData, uint16_t Size)
00588 {
00589   if ((hsmartcard->State == HAL_SMARTCARD_STATE_READY) || (hsmartcard->State == HAL_SMARTCARD_STATE_BUSY_RX))
00590   {
00591     if((pData == NULL) || (Size == 0))
00592     {
00593       return HAL_ERROR;
00594     }
00595 
00596     /* Process Locked */
00597     __HAL_LOCK(hsmartcard);
00598 
00599     /* Check if a receive process is ongoing or not */
00600     if(hsmartcard->State == HAL_SMARTCARD_STATE_BUSY_RX)
00601     {
00602       hsmartcard->State = HAL_SMARTCARD_STATE_BUSY_TX_RX;
00603     }
00604     else
00605     {
00606       hsmartcard->State = HAL_SMARTCARD_STATE_BUSY_TX;
00607     }
00608 
00609     hsmartcard->ErrorCode = HAL_SMARTCARD_ERROR_NONE;
00610     hsmartcard->pTxBuffPtr = pData;
00611     hsmartcard->TxXferSize = Size;
00612     hsmartcard->TxXferCount = Size;
00613 
00614     /* Disable the Peripheral first to update mode for TX master */
00615     CLEAR_BIT(hsmartcard->Instance->CR1, USART_CR1_UE);
00616 
00617     /* Disable Rx, enable Tx */
00618     CLEAR_BIT(hsmartcard->Instance->CR1, USART_CR1_RE);
00619     SET_BIT(hsmartcard->Instance->RQR, SMARTCARD_RXDATA_FLUSH_REQUEST);
00620     SET_BIT(hsmartcard->Instance->CR1, USART_CR1_TE);
00621     
00622     /* Enable the Peripheral */
00623     SET_BIT(hsmartcard->Instance->CR1, USART_CR1_UE);
00624 
00625     /* Enable the SMARTCARD Error Interrupt: (Frame error, noise error, overrun error) */
00626     __HAL_SMARTCARD_ENABLE_IT(hsmartcard, SMARTCARD_IT_ERR);
00627 
00628     /* Process Unlocked */
00629     __HAL_UNLOCK(hsmartcard);
00630 
00631     /* Enable the SMARTCARD Transmit Data Register Empty Interrupt */
00632     __HAL_SMARTCARD_ENABLE_IT(hsmartcard, SMARTCARD_IT_TXE);
00633 
00634     return HAL_OK;
00635   }
00636   else
00637   {
00638     return HAL_BUSY;
00639   }
00640 }
00641 
00642 /**
00643   * @brief Receive an amount of data in interrupt mode.
00644   * @param hsmartcard: Pointer to a SMARTCARD_HandleTypeDef structure that contains
00645   *                    the configuration information for the specified SMARTCARD module.
00646   * @param pData: pointer to data buffer.
00647   * @param Size: amount of data to be received.
00648   * @retval HAL status
00649   */
00650 HAL_StatusTypeDef HAL_SMARTCARD_Receive_IT(SMARTCARD_HandleTypeDef *hsmartcard, uint8_t *pData, uint16_t Size)
00651 {
00652   if ((hsmartcard->State == HAL_SMARTCARD_STATE_READY) || (hsmartcard->State == HAL_SMARTCARD_STATE_BUSY_TX))
00653   {
00654     if((pData == NULL) || (Size == 0))
00655     {
00656       return HAL_ERROR;
00657     }
00658 
00659     /* Process Locked */
00660     __HAL_LOCK(hsmartcard);
00661 
00662     /* Check if a transmit process is ongoing or not */
00663     if(hsmartcard->State == HAL_SMARTCARD_STATE_BUSY_TX)
00664     {
00665       hsmartcard->State = HAL_SMARTCARD_STATE_BUSY_TX_RX;
00666     }
00667     else
00668     {
00669       hsmartcard->State = HAL_SMARTCARD_STATE_BUSY_RX;
00670     }
00671 
00672     hsmartcard->ErrorCode = HAL_SMARTCARD_ERROR_NONE;
00673     hsmartcard->pRxBuffPtr = pData;
00674     hsmartcard->RxXferSize = Size;
00675     hsmartcard->RxXferCount = Size;
00676 
00677     /* Enable the SMARTCARD Parity Error Interrupt */
00678     __HAL_SMARTCARD_ENABLE_IT(hsmartcard, SMARTCARD_IT_PE);
00679 
00680     /* Enable the SMARTCARD Error Interrupt: (Frame error, noise error, overrun error) */
00681     __HAL_SMARTCARD_ENABLE_IT(hsmartcard, SMARTCARD_IT_ERR);
00682 
00683     /* Process Unlocked */
00684     __HAL_UNLOCK(hsmartcard);
00685 
00686     /* Enable the SMARTCARD Data Register not empty Interrupt */
00687     __HAL_SMARTCARD_ENABLE_IT(hsmartcard, SMARTCARD_IT_RXNE);
00688 
00689     return HAL_OK;
00690   }
00691   else
00692   {
00693     return HAL_BUSY;
00694   }
00695 }
00696 
00697 /**
00698   * @brief Send an amount of data in DMA mode.
00699   * @param hsmartcard: Pointer to a SMARTCARD_HandleTypeDef structure that contains
00700   *                    the configuration information for the specified SMARTCARD module.
00701   * @param pData: pointer to data buffer.
00702   * @param Size: amount of data to be sent.
00703   * @retval HAL status
00704   */
00705 HAL_StatusTypeDef HAL_SMARTCARD_Transmit_DMA(SMARTCARD_HandleTypeDef *hsmartcard, uint8_t *pData, uint16_t Size)
00706 {
00707   uint32_t *tmp;
00708 
00709   if ((hsmartcard->State == HAL_SMARTCARD_STATE_READY) || (hsmartcard->State == HAL_SMARTCARD_STATE_BUSY_RX))
00710   {
00711     if((pData == NULL) || (Size == 0))
00712     {
00713       return HAL_ERROR;
00714     }
00715 
00716     /* Process Locked */
00717     __HAL_LOCK(hsmartcard);
00718 
00719     /* Check if a receive process is ongoing or not */
00720     if(hsmartcard->State == HAL_SMARTCARD_STATE_BUSY_RX)
00721     {
00722       hsmartcard->State = HAL_SMARTCARD_STATE_BUSY_TX_RX;
00723     }
00724     else
00725     {
00726       hsmartcard->State = HAL_SMARTCARD_STATE_BUSY_TX;
00727     }
00728 
00729     hsmartcard->ErrorCode = HAL_SMARTCARD_ERROR_NONE;
00730     hsmartcard->pTxBuffPtr = pData;
00731     hsmartcard->TxXferSize = Size;
00732     hsmartcard->TxXferCount = Size;
00733 
00734     /* Disable the Peripheral first to update mode for TX master */
00735     CLEAR_BIT(hsmartcard->Instance->CR1, USART_CR1_UE);
00736 
00737     /* Disable Rx, enable Tx */
00738     CLEAR_BIT(hsmartcard->Instance->CR1, USART_CR1_RE);
00739     SET_BIT(hsmartcard->Instance->RQR, SMARTCARD_RXDATA_FLUSH_REQUEST);
00740     SET_BIT(hsmartcard->Instance->CR1, USART_CR1_TE);
00741     
00742     /* Enable the Peripheral */
00743     SET_BIT(hsmartcard->Instance->CR1, USART_CR1_UE);
00744 
00745     /* Set the SMARTCARD DMA transfer complete callback */
00746     hsmartcard->hdmatx->XferCpltCallback = SMARTCARD_DMATransmitCplt;
00747 
00748     /* Set the SMARTCARD error callback */
00749     hsmartcard->hdmatx->XferErrorCallback = SMARTCARD_DMAError;
00750 
00751     /* Enable the SMARTCARD transmit DMA channel */
00752     tmp = (uint32_t*)&pData;
00753     HAL_DMA_Start_IT(hsmartcard->hdmatx, *(uint32_t*)tmp, (uint32_t)&hsmartcard->Instance->TDR, Size);
00754 
00755     /* Enable the DMA transfer for transmit request by setting the DMAT bit
00756        in the SMARTCARD associated USART CR3 register */
00757     SET_BIT(hsmartcard->Instance->CR3, USART_CR3_DMAT);
00758 
00759     /* Process Unlocked */
00760     __HAL_UNLOCK(hsmartcard);
00761 
00762     return HAL_OK;
00763   }
00764   else
00765   {
00766     return HAL_BUSY;
00767   }
00768 }
00769 
00770 /**
00771   * @brief Receive an amount of data in DMA mode.
00772   * @param hsmartcard: Pointer to a SMARTCARD_HandleTypeDef structure that contains
00773   *                    the configuration information for the specified SMARTCARD module.
00774   * @param pData: pointer to data buffer.
00775   * @param Size: amount of data to be received.
00776   * @note   The SMARTCARD-associated USART parity is enabled (PCE = 1),
00777   *         the received data contain the parity bit (MSB position).
00778   * @retval HAL status
00779   */
00780 HAL_StatusTypeDef HAL_SMARTCARD_Receive_DMA(SMARTCARD_HandleTypeDef *hsmartcard, uint8_t *pData, uint16_t Size)
00781 {
00782   uint32_t *tmp;
00783 
00784   if ((hsmartcard->State == HAL_SMARTCARD_STATE_READY) || (hsmartcard->State == HAL_SMARTCARD_STATE_BUSY_TX))
00785   {
00786     if((pData == NULL) || (Size == 0))
00787     {
00788       return HAL_ERROR;
00789     }
00790 
00791     /* Process Locked */
00792     __HAL_LOCK(hsmartcard);
00793 
00794     /* Check if a transmit process is ongoing or not */
00795     if(hsmartcard->State == HAL_SMARTCARD_STATE_BUSY_TX)
00796     {
00797       hsmartcard->State = HAL_SMARTCARD_STATE_BUSY_TX_RX;
00798     }
00799     else
00800     {
00801       hsmartcard->State = HAL_SMARTCARD_STATE_BUSY_RX;
00802     }
00803 
00804     hsmartcard->ErrorCode = HAL_SMARTCARD_ERROR_NONE;
00805     hsmartcard->pRxBuffPtr = pData;
00806     hsmartcard->RxXferSize = Size;
00807 
00808     /* Set the SMARTCARD DMA transfer complete callback */
00809     hsmartcard->hdmarx->XferCpltCallback = SMARTCARD_DMAReceiveCplt;
00810 
00811     /* Set the SMARTCARD DMA error callback */
00812     hsmartcard->hdmarx->XferErrorCallback = SMARTCARD_DMAError;
00813 
00814     /* Enable the DMA channel */
00815     tmp = (uint32_t*)&pData;
00816     HAL_DMA_Start_IT(hsmartcard->hdmarx, (uint32_t)&hsmartcard->Instance->RDR, *(uint32_t*)tmp, Size);
00817 
00818     /* Enable the DMA transfer for the receiver request by setting the DMAR bit
00819        in the SMARTCARD associated USART CR3 register */
00820     SET_BIT(hsmartcard->Instance->CR3, USART_CR3_DMAR);
00821 
00822     /* Process Unlocked */
00823     __HAL_UNLOCK(hsmartcard);
00824 
00825     return HAL_OK;
00826   }
00827   else
00828   {
00829     return HAL_BUSY;
00830   }
00831 }
00832 
00833 /**
00834   * @brief Handle SMARTCARD interrupt requests.
00835   * @param hsmartcard: Pointer to a SMARTCARD_HandleTypeDef structure that contains
00836   *                    the configuration information for the specified SMARTCARD module.
00837   * @retval None
00838   */
00839 void HAL_SMARTCARD_IRQHandler(SMARTCARD_HandleTypeDef *hsmartcard)
00840 {
00841   /* SMARTCARD parity error interrupt occurred -------------------------------------*/
00842   if((__HAL_SMARTCARD_GET_IT(hsmartcard, SMARTCARD_IT_PE) != RESET) && (__HAL_SMARTCARD_GET_IT_SOURCE(hsmartcard, SMARTCARD_IT_PE) != RESET))
00843   {
00844     __HAL_SMARTCARD_CLEAR_IT(hsmartcard, SMARTCARD_CLEAR_PEF);
00845     hsmartcard->ErrorCode |= HAL_SMARTCARD_ERROR_PE;
00846     /* Set the SMARTCARD state ready to be able to start again the process */
00847     hsmartcard->State = HAL_SMARTCARD_STATE_READY;
00848   }
00849 
00850   /* SMARTCARD frame error interrupt occurred --------------------------------------*/
00851   if((__HAL_SMARTCARD_GET_IT(hsmartcard, SMARTCARD_IT_FE) != RESET) && (__HAL_SMARTCARD_GET_IT_SOURCE(hsmartcard, SMARTCARD_IT_ERR) != RESET))
00852   {
00853     __HAL_SMARTCARD_CLEAR_IT(hsmartcard, SMARTCARD_CLEAR_FEF);
00854     hsmartcard->ErrorCode |= HAL_SMARTCARD_ERROR_FE;
00855     /* Set the SMARTCARD state ready to be able to start again the process */
00856     hsmartcard->State = HAL_SMARTCARD_STATE_READY;
00857   }
00858 
00859   /* SMARTCARD noise error interrupt occurred --------------------------------------*/
00860   if((__HAL_SMARTCARD_GET_IT(hsmartcard, SMARTCARD_IT_NE) != RESET) && (__HAL_SMARTCARD_GET_IT_SOURCE(hsmartcard, SMARTCARD_IT_ERR) != RESET))
00861   {
00862     __HAL_SMARTCARD_CLEAR_IT(hsmartcard, SMARTCARD_CLEAR_NEF);
00863     hsmartcard->ErrorCode |= HAL_SMARTCARD_ERROR_NE;
00864     /* Set the SMARTCARD state ready to be able to start again the process */
00865     hsmartcard->State = HAL_SMARTCARD_STATE_READY;
00866   }
00867 
00868   /* SMARTCARD Over-Run interrupt occurred -----------------------------------------*/
00869   if((__HAL_SMARTCARD_GET_IT(hsmartcard, SMARTCARD_IT_ORE) != RESET) && (__HAL_SMARTCARD_GET_IT_SOURCE(hsmartcard, SMARTCARD_IT_ERR) != RESET))
00870   {
00871     __HAL_SMARTCARD_CLEAR_IT(hsmartcard, SMARTCARD_CLEAR_OREF);
00872     hsmartcard->ErrorCode |= HAL_SMARTCARD_ERROR_ORE;
00873     /* Set the SMARTCARD state ready to be able to start again the process */
00874     hsmartcard->State = HAL_SMARTCARD_STATE_READY;
00875   }
00876 
00877   /* SMARTCARD receiver timeout interrupt occurred -----------------------------------------*/
00878   if((__HAL_SMARTCARD_GET_IT(hsmartcard, SMARTCARD_IT_RTO) != RESET) && (__HAL_SMARTCARD_GET_IT_SOURCE(hsmartcard, SMARTCARD_IT_RTO) != RESET))
00879   {
00880     __HAL_SMARTCARD_CLEAR_IT(hsmartcard, SMARTCARD_CLEAR_RTOF);
00881     hsmartcard->ErrorCode |= HAL_SMARTCARD_ERROR_RTO;
00882     /* Set the SMARTCARD state ready to be able to start again the process */
00883     hsmartcard->State = HAL_SMARTCARD_STATE_READY;
00884   }
00885 
00886   /* Call SMARTCARD Error Call back function if need be --------------------------*/
00887   if(hsmartcard->ErrorCode != HAL_SMARTCARD_ERROR_NONE)
00888   {
00889     HAL_SMARTCARD_ErrorCallback(hsmartcard);
00890   }
00891 
00892   /* SMARTCARD in mode Receiver ---------------------------------------------------*/
00893   if((__HAL_SMARTCARD_GET_IT(hsmartcard, SMARTCARD_IT_RXNE) != RESET) && (__HAL_SMARTCARD_GET_IT_SOURCE(hsmartcard, SMARTCARD_IT_RXNE) != RESET))
00894   {
00895     SMARTCARD_Receive_IT(hsmartcard);
00896     /* Clear RXNE interrupt flag done by reading RDR in SMARTCARD_Receive_IT() */
00897   }
00898 
00899   /* SMARTCARD in mode Receiver, end of block interruption ------------------------*/
00900   if((__HAL_SMARTCARD_GET_IT(hsmartcard, SMARTCARD_IT_EOB) != RESET) && (__HAL_SMARTCARD_GET_IT_SOURCE(hsmartcard, SMARTCARD_IT_EOB) != RESET))
00901   {
00902     hsmartcard->State = HAL_SMARTCARD_STATE_READY;
00903     __HAL_UNLOCK(hsmartcard);
00904     HAL_SMARTCARD_RxCpltCallback(hsmartcard);
00905     /* Clear EOBF interrupt after HAL_SMARTCARD_RxCpltCallback() call for the End of Block information
00906      * to be available during HAL_SMARTCARD_RxCpltCallback() processing */
00907     __HAL_SMARTCARD_CLEAR_IT(hsmartcard, SMARTCARD_CLEAR_EOBF);
00908   }
00909 
00910   /* SMARTCARD in mode Transmitter ------------------------------------------------*/
00911  if((__HAL_SMARTCARD_GET_IT(hsmartcard, SMARTCARD_IT_TXE) != RESET) &&(__HAL_SMARTCARD_GET_IT_SOURCE(hsmartcard, SMARTCARD_IT_TXE) != RESET))
00912   {
00913     SMARTCARD_Transmit_IT(hsmartcard);
00914   }
00915 
00916   /* SMARTCARD in mode Transmitter (transmission end) ------------------------*/
00917  if((__HAL_SMARTCARD_GET_IT(hsmartcard, SMARTCARD_IT_TC) != RESET) &&(__HAL_SMARTCARD_GET_IT_SOURCE(hsmartcard, SMARTCARD_IT_TC) != RESET))
00918   {
00919     SMARTCARD_EndTransmit_IT(hsmartcard);
00920   }
00921 }
00922 
00923 /**
00924   * @brief Tx Transfer completed callback.
00925   * @param hsmartcard: Pointer to a SMARTCARD_HandleTypeDef structure that contains
00926   *                    the configuration information for the specified SMARTCARD module.
00927   * @retval None
00928   */
00929  __weak void HAL_SMARTCARD_TxCpltCallback(SMARTCARD_HandleTypeDef *hsmartcard)
00930 {
00931   /* NOTE : This function should not be modified, when the callback is needed,
00932             the HAL_SMARTCARD_TxCpltCallback can be implemented in the user file.
00933    */
00934 }
00935 
00936 /**
00937   * @brief Rx Transfer completed callback.
00938   * @param hsmartcard: Pointer to a SMARTCARD_HandleTypeDef structure that contains
00939   *                    the configuration information for the specified SMARTCARD module.
00940   * @retval None
00941   */
00942 __weak void HAL_SMARTCARD_RxCpltCallback(SMARTCARD_HandleTypeDef *hsmartcard)
00943 {
00944   /* NOTE : This function should not be modified, when the callback is needed,
00945             the HAL_SMARTCARD_RxCpltCallback can be implemented in the user file.
00946    */
00947 }
00948 
00949 /**
00950   * @brief SMARTCARD error callback.
00951   * @param hsmartcard: Pointer to a SMARTCARD_HandleTypeDef structure that contains
00952   *                    the configuration information for the specified SMARTCARD module.
00953   * @retval None
00954   */
00955 __weak void HAL_SMARTCARD_ErrorCallback(SMARTCARD_HandleTypeDef *hsmartcard)
00956 {
00957   /* NOTE : This function should not be modified, when the callback is needed,
00958             the HAL_SMARTCARD_ErrorCallback can be implemented in the user file.
00959    */
00960 }
00961 
00962 /**
00963   * @}
00964   */
00965 
00966 /** @defgroup SMARTCARD_Exported_Functions_Group4 Peripheral State and Errors functions
00967   *  @brief   SMARTCARD State and Errors functions
00968   *
00969 @verbatim
00970   ==============================================================================
00971                   ##### Peripheral State and Errors functions #####
00972   ==============================================================================
00973   [..]
00974     This subsection provides a set of functions allowing to return the State of SmartCard
00975     handle and also return Peripheral Errors occurred during communication process
00976      (+) HAL_SMARTCARD_GetState() API can be helpful to check in run-time the state
00977          of the SMARTCARD peripheral.
00978      (+) HAL_SMARTCARD_GetError() checks in run-time errors that could occur during
00979          communication.
00980 
00981 @endverbatim
00982   * @{
00983   */
00984 
00985 
00986 /**
00987   * @brief  Return the SMARTCARD handle state.
00988   * @param hsmartcard: Pointer to a SMARTCARD_HandleTypeDef structure that contains
00989   *                    the configuration information for the specified SMARTCARD module.
00990   * @retval SMARTCARD handle state
00991   */
00992 HAL_SMARTCARD_StateTypeDef HAL_SMARTCARD_GetState(SMARTCARD_HandleTypeDef *hsmartcard)
00993 {
00994   return hsmartcard->State;
00995 }
00996 
00997 /**
00998   * @brief  Return the SMARTCARD handle error code.
00999   * @param hsmartcard: Pointer to a SMARTCARD_HandleTypeDef structure that contains
01000   *                    the configuration information for the specified SMARTCARD module.
01001 * @retval SMARTCARD handle Error Code
01002 */
01003 uint32_t HAL_SMARTCARD_GetError(SMARTCARD_HandleTypeDef *hsmartcard)
01004 {
01005   return hsmartcard->ErrorCode;
01006 }
01007 
01008 /**
01009   * @}
01010   */
01011 
01012 /**
01013   * @}
01014   */
01015 
01016 /** @defgroup SMARTCARD_Private_Functions   SMARTCARD Private Functions
01017   * @{
01018   */
01019 
01020 /**
01021   * @brief Send an amount of data in non-blocking mode.
01022   * @param hsmartcard: Pointer to a SMARTCARD_HandleTypeDef structure that contains
01023   *                the configuration information for the specified SMARTCARD module.
01024   *         Function called under interruption only, once
01025   *         interruptions have been enabled by HAL_SMARTCARD_Transmit_IT()
01026   * @retval HAL status
01027   */
01028 static HAL_StatusTypeDef SMARTCARD_Transmit_IT(SMARTCARD_HandleTypeDef *hsmartcard)
01029 {
01030   if ((hsmartcard->State == HAL_SMARTCARD_STATE_BUSY_TX) || (hsmartcard->State == HAL_SMARTCARD_STATE_BUSY_TX_RX))
01031   {
01032 
01033     if(hsmartcard->TxXferCount == 0)
01034     {
01035       /* Disable the SMARTCARD Transmit Data Register Empty Interrupt */
01036       __HAL_SMARTCARD_DISABLE_IT(hsmartcard, SMARTCARD_IT_TXE);
01037 
01038       /* Enable the SMARTCARD Transmit Complete Interrupt */
01039       __HAL_SMARTCARD_ENABLE_IT(hsmartcard, SMARTCARD_IT_TC);
01040 
01041       return HAL_OK;
01042     }
01043     else
01044     {
01045       hsmartcard->Instance->TDR = (*hsmartcard->pTxBuffPtr++ & (uint8_t)0xFF);
01046       hsmartcard->TxXferCount--;
01047 
01048       return HAL_OK;
01049     }
01050   }
01051   else
01052   {
01053     return HAL_BUSY;
01054   }
01055 }
01056 
01057 /**
01058   * @brief  Wrap up transmission in non-blocking mode.
01059   * @param hsmartcard: Pointer to a SMARTCARD_HandleTypeDef structure that contains
01060   *                the configuration information for the specified SMARTCARD module.
01061   * @retval HAL status
01062   */
01063 static HAL_StatusTypeDef SMARTCARD_EndTransmit_IT(SMARTCARD_HandleTypeDef *hsmartcard)
01064 {
01065   /* Disable the SMARTCARD Transmit Complete Interrupt */
01066   __HAL_SMARTCARD_DISABLE_IT(hsmartcard, SMARTCARD_IT_TC);
01067 
01068   /* Check if a receive process is ongoing or not */
01069   if(hsmartcard->State == HAL_SMARTCARD_STATE_BUSY_TX_RX)
01070   {
01071     hsmartcard->State = HAL_SMARTCARD_STATE_BUSY_RX;
01072 
01073     /* Re-enable Rx at end of transmission if initial mode is Rx/Tx */
01074     if(hsmartcard->Init.Mode == SMARTCARD_MODE_TX_RX)
01075     {
01076       /* Disable the Peripheral first to update modes */
01077       CLEAR_BIT(hsmartcard->Instance->CR1, USART_CR1_UE);
01078       SET_BIT(hsmartcard->Instance->CR1, USART_CR1_RE);
01079       /* Enable the Peripheral */
01080       SET_BIT(hsmartcard->Instance->CR1, USART_CR1_UE);
01081     }
01082   }
01083   else
01084   {
01085     /* Disable the SMARTCARD Error Interrupt: (Frame error, noise error, overrun error) */
01086     __HAL_SMARTCARD_DISABLE_IT(hsmartcard, SMARTCARD_IT_ERR);
01087 
01088     hsmartcard->State = HAL_SMARTCARD_STATE_READY;
01089   }
01090 
01091   HAL_SMARTCARD_TxCpltCallback(hsmartcard);
01092 
01093   return HAL_OK;
01094 }
01095 
01096 
01097 /**
01098   * @brief Receive an amount of data in non-blocking mode.
01099   * @param hsmartcard: Pointer to a SMARTCARD_HandleTypeDef structure that contains
01100   *                the configuration information for the specified SMARTCARD module.
01101   *         Function called under interruption only, once
01102   *         interruptions have been enabled by HAL_SMARTCARD_Receive_IT().
01103   * @retval HAL status
01104   */
01105 static HAL_StatusTypeDef SMARTCARD_Receive_IT(SMARTCARD_HandleTypeDef *hsmartcard)
01106 {
01107   if ((hsmartcard->State == HAL_SMARTCARD_STATE_BUSY_RX) || (hsmartcard->State == HAL_SMARTCARD_STATE_BUSY_TX_RX))
01108   {
01109 
01110     *hsmartcard->pRxBuffPtr++ = (uint8_t)(hsmartcard->Instance->RDR & (uint8_t)0xFF);
01111 
01112     if(--hsmartcard->RxXferCount == 0)
01113     {
01114       __HAL_SMARTCARD_DISABLE_IT(hsmartcard, SMARTCARD_IT_RXNE);
01115 
01116       /* Check if a transmit process is ongoing or not */
01117       if(hsmartcard->State == HAL_SMARTCARD_STATE_BUSY_TX_RX)
01118       {
01119         hsmartcard->State = HAL_SMARTCARD_STATE_BUSY_TX;
01120       }
01121       else
01122       {
01123         /* Disable the SMARTCARD Parity Error Interrupt */
01124         __HAL_SMARTCARD_DISABLE_IT(hsmartcard, SMARTCARD_IT_PE);
01125 
01126         /* Disable the SMARTCARD Error Interrupt: (Frame error, noise error, overrun error) */
01127         __HAL_SMARTCARD_DISABLE_IT(hsmartcard, SMARTCARD_IT_ERR);
01128 
01129         hsmartcard->State = HAL_SMARTCARD_STATE_READY;
01130       }
01131 
01132       HAL_SMARTCARD_RxCpltCallback(hsmartcard);
01133 
01134       return HAL_OK;
01135     }
01136 
01137     return HAL_OK;
01138   }
01139   else
01140   {
01141     return HAL_BUSY;
01142   }
01143 }
01144 
01145 /**
01146   * @brief  Handle SMARTCARD Communication Timeout.
01147   * @param hsmartcard: Pointer to a SMARTCARD_HandleTypeDef structure that contains
01148   *                    the configuration information for the specified SMARTCARD module.
01149   * @param  Flag: specifies the SMARTCARD flag to check.
01150   * @param  Status: The new Flag status (SET or RESET).
01151   * @param  Timeout: Timeout duration.
01152   * @retval HAL status
01153   */
01154 static HAL_StatusTypeDef SMARTCARD_WaitOnFlagUntilTimeout(SMARTCARD_HandleTypeDef *hsmartcard, uint32_t Flag, FlagStatus Status, uint32_t Timeout)
01155 {
01156   uint32_t tickstart = HAL_GetTick();
01157 
01158   /* Wait until flag is set */
01159   if(Status == RESET)
01160   {
01161     while(__HAL_SMARTCARD_GET_FLAG(hsmartcard, Flag) == RESET)
01162     {
01163       /* Check for the Timeout */
01164       if(Timeout != HAL_MAX_DELAY)
01165       {
01166         if((Timeout == 0) || ((HAL_GetTick()-tickstart) > Timeout))
01167         {
01168           /* Disable TXE, RXNE, PE and ERR (Frame error, noise error, overrun error) interrupts for the interrupt process */
01169           __HAL_SMARTCARD_DISABLE_IT(hsmartcard, SMARTCARD_IT_TXE);
01170           __HAL_SMARTCARD_DISABLE_IT(hsmartcard, SMARTCARD_IT_RXNE);
01171           __HAL_SMARTCARD_DISABLE_IT(hsmartcard, SMARTCARD_IT_PE);
01172           __HAL_SMARTCARD_DISABLE_IT(hsmartcard, SMARTCARD_IT_ERR);
01173 
01174           hsmartcard->State= HAL_SMARTCARD_STATE_READY;
01175 
01176           /* Process Unlocked */
01177           __HAL_UNLOCK(hsmartcard);
01178 
01179           return HAL_TIMEOUT;
01180         }
01181       }
01182     }
01183   }
01184   else
01185   {
01186     while(__HAL_SMARTCARD_GET_FLAG(hsmartcard, Flag) != RESET)
01187     {
01188       /* Check for the Timeout */
01189       if(Timeout != HAL_MAX_DELAY)
01190       {
01191         if((Timeout == 0) || ((HAL_GetTick()-tickstart) > Timeout))
01192         {
01193           /* Disable TXE, RXNE, PE and ERR (Frame error, noise error, overrun error) interrupts for the interrupt process */
01194           __HAL_SMARTCARD_DISABLE_IT(hsmartcard, SMARTCARD_IT_TXE);
01195           __HAL_SMARTCARD_DISABLE_IT(hsmartcard, SMARTCARD_IT_RXNE);
01196           __HAL_SMARTCARD_DISABLE_IT(hsmartcard, SMARTCARD_IT_PE);
01197           __HAL_SMARTCARD_DISABLE_IT(hsmartcard, SMARTCARD_IT_ERR);
01198 
01199           hsmartcard->State= HAL_SMARTCARD_STATE_READY;
01200 
01201           /* Process Unlocked */
01202           __HAL_UNLOCK(hsmartcard);
01203 
01204           return HAL_TIMEOUT;
01205         }
01206       }
01207     }
01208   }
01209   return HAL_OK;
01210 }
01211 
01212 /**
01213   * @brief DMA SMARTCARD transmit process complete callback.
01214   * @param  hdma: Pointer to a DMA_HandleTypeDef structure that contains
01215   *               the configuration information for the specified DMA module.
01216   * @retval None
01217   */
01218 static void SMARTCARD_DMATransmitCplt(DMA_HandleTypeDef *hdma)
01219 {
01220   SMARTCARD_HandleTypeDef* hsmartcard = ( SMARTCARD_HandleTypeDef* )((DMA_HandleTypeDef* )hdma)->Parent;
01221   hsmartcard->TxXferCount = 0;
01222 
01223   /* Disable the DMA transfer for transmit request by resetting the DMAT bit
01224   in the SMARTCARD associated USART CR3 register */
01225   CLEAR_BIT(hsmartcard->Instance->CR3, USART_CR3_DMAT);
01226 
01227   /* Enable the SMARTCARD Transmit Complete Interrupt */
01228   __HAL_SMARTCARD_ENABLE_IT(hsmartcard, SMARTCARD_IT_TC);
01229 }
01230 
01231 /**
01232   * @brief DMA SMARTCARD receive process complete callback.
01233   * @param  hdma: Pointer to a DMA_HandleTypeDef structure that contains
01234   *               the configuration information for the specified DMA module.
01235   * @retval None
01236   */
01237 static void SMARTCARD_DMAReceiveCplt(DMA_HandleTypeDef *hdma)
01238 {
01239   SMARTCARD_HandleTypeDef* hsmartcard = ( SMARTCARD_HandleTypeDef* )((DMA_HandleTypeDef* )hdma)->Parent;
01240   hsmartcard->RxXferCount = 0;
01241 
01242   /* Disable the DMA transfer for the receiver request by resetting the DMAR bit
01243      in the SMARTCARD associated USART CR3 register */
01244   CLEAR_BIT(hsmartcard->Instance->CR3, USART_CR3_DMAR);
01245 
01246   /* Check if a transmit process is ongoing or not */
01247   if(hsmartcard->State == HAL_SMARTCARD_STATE_BUSY_TX_RX)
01248   {
01249     hsmartcard->State = HAL_SMARTCARD_STATE_BUSY_TX;
01250   }
01251   else
01252   {
01253     hsmartcard->State = HAL_SMARTCARD_STATE_READY;
01254   }
01255 
01256   HAL_SMARTCARD_RxCpltCallback(hsmartcard);
01257 }
01258 
01259 /**
01260   * @brief DMA SMARTCARD communication error callback.
01261   * @param  hdma: Pointer to a DMA_HandleTypeDef structure that contains
01262   *               the configuration information for the specified DMA module.
01263   * @retval None
01264   */
01265 static void SMARTCARD_DMAError(DMA_HandleTypeDef *hdma)
01266 {
01267   SMARTCARD_HandleTypeDef* hsmartcard = ( SMARTCARD_HandleTypeDef* )((DMA_HandleTypeDef* )hdma)->Parent;
01268   hsmartcard->RxXferCount = 0;
01269   hsmartcard->TxXferCount = 0;
01270   hsmartcard->State= HAL_SMARTCARD_STATE_READY;
01271   hsmartcard->ErrorCode |= HAL_SMARTCARD_ERROR_DMA;
01272   HAL_SMARTCARD_ErrorCallback(hsmartcard);
01273 }
01274 
01275 /**
01276   * @brief Configure the SMARTCARD associated USART peripheral.
01277   * @param hsmartcard: Pointer to a SMARTCARD_HandleTypeDef structure that contains
01278   *                    the configuration information for the specified SMARTCARD module.
01279   * @retval None
01280   */
01281 static HAL_StatusTypeDef SMARTCARD_SetConfig(SMARTCARD_HandleTypeDef *hsmartcard)
01282 {
01283   uint32_t tmpreg                          = 0x00000000;
01284   SMARTCARD_ClockSourceTypeDef clocksource = SMARTCARD_CLOCKSOURCE_UNDEFINED;
01285   HAL_StatusTypeDef ret                    = HAL_OK;
01286 
01287   /* Check the parameters */
01288   assert_param(IS_SMARTCARD_INSTANCE(hsmartcard->Instance));
01289   assert_param(IS_SMARTCARD_BAUDRATE(hsmartcard->Init.BaudRate));
01290   assert_param(IS_SMARTCARD_WORD_LENGTH(hsmartcard->Init.WordLength));
01291   assert_param(IS_SMARTCARD_STOPBITS(hsmartcard->Init.StopBits));
01292   assert_param(IS_SMARTCARD_PARITY(hsmartcard->Init.Parity));
01293   assert_param(IS_SMARTCARD_MODE(hsmartcard->Init.Mode));
01294   assert_param(IS_SMARTCARD_POLARITY(hsmartcard->Init.CLKPolarity));
01295   assert_param(IS_SMARTCARD_PHASE(hsmartcard->Init.CLKPhase));
01296   assert_param(IS_SMARTCARD_LASTBIT(hsmartcard->Init.CLKLastBit));
01297   assert_param(IS_SMARTCARD_ONE_BIT_SAMPLE(hsmartcard->Init.OneBitSampling));
01298   assert_param(IS_SMARTCARD_NACK(hsmartcard->Init.NACKEnable));
01299   assert_param(IS_SMARTCARD_TIMEOUT(hsmartcard->Init.TimeOutEnable));
01300   assert_param(IS_SMARTCARD_AUTORETRY_COUNT(hsmartcard->Init.AutoRetryCount));
01301 
01302   /*-------------------------- USART CR1 Configuration -----------------------*/
01303   /* In SmartCard mode, M and PCE are forced to 1 (8 bits + parity).
01304    * Oversampling is forced to 16 (OVER8 = 0).
01305    * Configure the Parity and Mode:
01306    *  set PS bit according to hsmartcard->Init.Parity value
01307    *  set TE and RE bits according to hsmartcard->Init.Mode value */
01308   tmpreg = (uint32_t) hsmartcard->Init.Parity | hsmartcard->Init.Mode;
01309   tmpreg |= (uint32_t) hsmartcard->Init.WordLength;
01310   MODIFY_REG(hsmartcard->Instance->CR1, USART_CR1_FIELDS, tmpreg);
01311 
01312   /*-------------------------- USART CR2 Configuration -----------------------*/
01313   /* Stop bits are forced to 1.5 (STOP = 11) */
01314   tmpreg = hsmartcard->Init.StopBits;
01315   /* Synchronous mode is activated by default */
01316   tmpreg |= (uint32_t) USART_CR2_CLKEN | hsmartcard->Init.CLKPolarity;
01317   tmpreg |= (uint32_t) hsmartcard->Init.CLKPhase | hsmartcard->Init.CLKLastBit;
01318   tmpreg |= (uint32_t) hsmartcard->Init.TimeOutEnable;
01319   MODIFY_REG(hsmartcard->Instance->CR2, USART_CR2_FIELDS, tmpreg);
01320 
01321   /*-------------------------- USART CR3 Configuration -----------------------*/
01322   /* Configure
01323    * - one-bit sampling method versus three samples' majority rule
01324    *   according to hsmartcard->Init.OneBitSampling
01325    * - NACK transmission in case of parity error according
01326    *   to hsmartcard->Init.NACKEnable
01327    * - autoretry counter according to hsmartcard->Init.AutoRetryCount     */
01328   tmpreg =  (uint32_t) hsmartcard->Init.OneBitSampling | hsmartcard->Init.NACKEnable;
01329   tmpreg |= ((uint32_t)hsmartcard->Init.AutoRetryCount << SMARTCARD_CR3_SCARCNT_LSB_POS);
01330   MODIFY_REG(hsmartcard->Instance-> CR3,USART_CR3_FIELDS, tmpreg);
01331 
01332   /*-------------------------- USART GTPR Configuration ----------------------*/
01333   tmpreg = (hsmartcard->Init.Prescaler | (((uint32_t)hsmartcard->Init.GuardTime-12) << SMARTCARD_GTPR_GT_LSB_POS));
01334   MODIFY_REG(hsmartcard->Instance->GTPR, (USART_GTPR_GT|USART_GTPR_PSC), tmpreg);
01335 
01336   /*-------------------------- USART RTOR Configuration ----------------------*/
01337   tmpreg =   ((uint32_t)hsmartcard->Init.BlockLength << SMARTCARD_RTOR_BLEN_LSB_POS);
01338   if (hsmartcard->Init.TimeOutEnable == SMARTCARD_TIMEOUT_ENABLE)
01339   {
01340     assert_param(IS_SMARTCARD_TIMEOUT_VALUE(hsmartcard->Init.TimeOutValue));
01341     tmpreg |=  (uint32_t) hsmartcard->Init.TimeOutValue;
01342   }
01343   MODIFY_REG(hsmartcard->Instance->RTOR, (USART_RTOR_RTO|USART_RTOR_BLEN), tmpreg);
01344 
01345   /*-------------------------- USART BRR Configuration -----------------------*/
01346   SMARTCARD_GETCLOCKSOURCE(hsmartcard, clocksource);
01347   switch (clocksource)
01348   {
01349     case SMARTCARD_CLOCKSOURCE_PCLK1:
01350       hsmartcard->Instance->BRR = (uint16_t)(HAL_RCC_GetPCLK1Freq() / hsmartcard->Init.BaudRate);
01351       break;
01352     case SMARTCARD_CLOCKSOURCE_PCLK2:
01353       hsmartcard->Instance->BRR = (uint16_t)(HAL_RCC_GetPCLK2Freq() / hsmartcard->Init.BaudRate);
01354       break;
01355     case SMARTCARD_CLOCKSOURCE_HSI:
01356       hsmartcard->Instance->BRR = (uint16_t)(HSI_VALUE / hsmartcard->Init.BaudRate);
01357       break;
01358     case SMARTCARD_CLOCKSOURCE_SYSCLK:
01359       hsmartcard->Instance->BRR = (uint16_t)(HAL_RCC_GetSysClockFreq() / hsmartcard->Init.BaudRate);
01360       break;
01361     case SMARTCARD_CLOCKSOURCE_LSE:
01362       hsmartcard->Instance->BRR = (uint16_t)(LSE_VALUE / hsmartcard->Init.BaudRate);
01363       break;
01364     case SMARTCARD_CLOCKSOURCE_UNDEFINED:
01365     default:
01366       ret = HAL_ERROR;
01367       break;
01368   }
01369 
01370   return ret;
01371 }
01372 
01373 
01374 /**
01375   * @brief Configure the SMARTCARD associated USART peripheral advanced features.
01376   * @param hsmartcard: Pointer to a SMARTCARD_HandleTypeDef structure that contains
01377   *                    the configuration information for the specified SMARTCARD module.
01378   * @retval None
01379   */
01380 static void SMARTCARD_AdvFeatureConfig(SMARTCARD_HandleTypeDef *hsmartcard)
01381 {
01382   /* Check whether the set of advanced features to configure is properly set */
01383   assert_param(IS_SMARTCARD_ADVFEATURE_INIT(hsmartcard->AdvancedInit.AdvFeatureInit));
01384 
01385   /* if required, configure TX pin active level inversion */
01386   if (HAL_IS_BIT_SET(hsmartcard->AdvancedInit.AdvFeatureInit, SMARTCARD_ADVFEATURE_TXINVERT_INIT))
01387   {
01388     assert_param(IS_SMARTCARD_ADVFEATURE_TXINV(hsmartcard->AdvancedInit.TxPinLevelInvert));
01389     MODIFY_REG(hsmartcard->Instance->CR2, USART_CR2_TXINV, hsmartcard->AdvancedInit.TxPinLevelInvert);
01390   }
01391 
01392   /* if required, configure RX pin active level inversion */
01393   if (HAL_IS_BIT_SET(hsmartcard->AdvancedInit.AdvFeatureInit, SMARTCARD_ADVFEATURE_RXINVERT_INIT))
01394   {
01395     assert_param(IS_SMARTCARD_ADVFEATURE_RXINV(hsmartcard->AdvancedInit.RxPinLevelInvert));
01396     MODIFY_REG(hsmartcard->Instance->CR2, USART_CR2_RXINV, hsmartcard->AdvancedInit.RxPinLevelInvert);
01397   }
01398 
01399   /* if required, configure data inversion */
01400   if (HAL_IS_BIT_SET(hsmartcard->AdvancedInit.AdvFeatureInit, SMARTCARD_ADVFEATURE_DATAINVERT_INIT))
01401   {
01402     assert_param(IS_SMARTCARD_ADVFEATURE_DATAINV(hsmartcard->AdvancedInit.DataInvert));
01403     MODIFY_REG(hsmartcard->Instance->CR2, USART_CR2_DATAINV, hsmartcard->AdvancedInit.DataInvert);
01404   }
01405 
01406   /* if required, configure RX/TX pins swap */
01407   if (HAL_IS_BIT_SET(hsmartcard->AdvancedInit.AdvFeatureInit, SMARTCARD_ADVFEATURE_SWAP_INIT))
01408   {
01409     assert_param(IS_SMARTCARD_ADVFEATURE_SWAP(hsmartcard->AdvancedInit.Swap));
01410     MODIFY_REG(hsmartcard->Instance->CR2, USART_CR2_SWAP, hsmartcard->AdvancedInit.Swap);
01411   }
01412 
01413   /* if required, configure RX overrun detection disabling */
01414   if (HAL_IS_BIT_SET(hsmartcard->AdvancedInit.AdvFeatureInit, SMARTCARD_ADVFEATURE_RXOVERRUNDISABLE_INIT))
01415   {
01416     assert_param(IS_SMARTCARD_OVERRUN(hsmartcard->AdvancedInit.OverrunDisable));
01417     MODIFY_REG(hsmartcard->Instance->CR3, USART_CR3_OVRDIS, hsmartcard->AdvancedInit.OverrunDisable);
01418   }
01419 
01420   /* if required, configure DMA disabling on reception error */
01421   if (HAL_IS_BIT_SET(hsmartcard->AdvancedInit.AdvFeatureInit, SMARTCARD_ADVFEATURE_DMADISABLEONERROR_INIT))
01422   {
01423     assert_param(IS_SMARTCARD_ADVFEATURE_DMAONRXERROR(hsmartcard->AdvancedInit.DMADisableonRxError));
01424     MODIFY_REG(hsmartcard->Instance->CR3, USART_CR3_DDRE, hsmartcard->AdvancedInit.DMADisableonRxError);
01425   }
01426 
01427   /* if required, configure MSB first on communication line */
01428   if (HAL_IS_BIT_SET(hsmartcard->AdvancedInit.AdvFeatureInit, SMARTCARD_ADVFEATURE_MSBFIRST_INIT))
01429   {
01430     assert_param(IS_SMARTCARD_ADVFEATURE_MSBFIRST(hsmartcard->AdvancedInit.MSBFirst));
01431     MODIFY_REG(hsmartcard->Instance->CR2, USART_CR2_MSBFIRST, hsmartcard->AdvancedInit.MSBFirst);
01432   }
01433 
01434 }
01435 
01436 /**
01437   * @brief Check the SMARTCARD Idle State.
01438   * @param hsmartcard: Pointer to a SMARTCARD_HandleTypeDef structure that contains
01439   *                    the configuration information for the specified SMARTCARD module.
01440   * @retval HAL status
01441   */
01442 static HAL_StatusTypeDef SMARTCARD_CheckIdleState(SMARTCARD_HandleTypeDef *hsmartcard)
01443 {
01444 
01445   /* Initialize the SMARTCARD ErrorCode */
01446   hsmartcard->ErrorCode = HAL_SMARTCARD_ERROR_NONE;
01447 
01448   /* Check if the Transmitter is enabled */
01449   if((hsmartcard->Instance->CR1 & USART_CR1_TE) == USART_CR1_TE)
01450   {
01451     /* Wait until TEACK flag is set */
01452     if(SMARTCARD_WaitOnFlagUntilTimeout(hsmartcard, USART_ISR_TEACK, RESET, SMARTCARD_TEACK_REACK_TIMEOUT) != HAL_OK)
01453     {
01454       return HAL_TIMEOUT;
01455     }
01456   }
01457   /* Check if the Receiver is enabled */
01458   if((hsmartcard->Instance->CR1 & USART_CR1_RE) == USART_CR1_RE)
01459   {
01460     /* Wait until REACK flag is set */
01461     if(SMARTCARD_WaitOnFlagUntilTimeout(hsmartcard, USART_ISR_REACK, RESET, SMARTCARD_TEACK_REACK_TIMEOUT) != HAL_OK)
01462     {
01463       return HAL_TIMEOUT;
01464     }
01465   }
01466 
01467   /* Initialize the SMARTCARD state*/
01468   hsmartcard->State= HAL_SMARTCARD_STATE_READY;
01469 
01470   /* Process Unlocked */
01471   __HAL_UNLOCK(hsmartcard);
01472 
01473   return HAL_OK;
01474 }
01475 
01476 /**
01477   * @}
01478   */
01479 
01480 #endif /* HAL_SMARTCARD_MODULE_ENABLED */
01481 /**
01482   * @}
01483   */
01484 
01485 /**
01486   * @}
01487   */
01488 
01489 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
01490