Subversion Repositories DashDisplay

Rev

Rev 2 | Blame | Compare with Previous | Last modification | View Log | Download | RSS feed

  1. /**
  2.   ******************************************************************************
  3.   * @file    stm32f1xx_hal_smartcard.c
  4.   * @author  MCD Application Team
  5.   * @version V1.0.4
  6.   * @date    29-April-2016
  7.   * @brief   SMARTCARD HAL module driver.
  8.   *          This file provides firmware functions to manage the following
  9.   *          functionalities of the SMARTCARD peripheral:
  10.   *           + Initialization and de-initialization functions
  11.   *           + IO operation functions
  12.   *           + Peripheral State and Errors functions
  13.   *           + Peripheral Control functions
  14.   *
  15.   @verbatim
  16.   ==============================================================================
  17.                         ##### How to use this driver #####
  18.   ==============================================================================
  19.   [..]
  20.     The SMARTCARD HAL driver can be used as follows:
  21.  
  22.     (#) Declare a SMARTCARD_HandleTypeDef handle structure.
  23.     (#) Initialize the SMARTCARD low level resources by implementing the HAL_SMARTCARD_MspInit() API:
  24.         (##) Enable the interface clock of the USARTx associated to the SMARTCARD.
  25.         (##) SMARTCARD pins configuration:
  26.             (+++) Enable the clock for the SMARTCARD GPIOs.
  27.              (+++) Configure the USART pins (TX as alternate function pull-up, RX as alternate function Input).
  28.         (##) NVIC configuration if you need to use interrupt process (HAL_SMARTCARD_Transmit_IT()
  29.              and HAL_SMARTCARD_Receive_IT() APIs):
  30.             (+++) Configure the USARTx interrupt priority.
  31.             (+++) Enable the NVIC USART IRQ handle.
  32.         (##) DMA Configuration if you need to use DMA process (HAL_SMARTCARD_Transmit_DMA()
  33.              and HAL_SMARTCARD_Receive_DMA() APIs):
  34.             (+++) Declare a DMA handle structure for the Tx/Rx channel.
  35.             (+++) Enable the DMAx interface clock.
  36.             (+++) Configure the declared DMA handle structure with the required Tx/Rx parameters.
  37.             (+++) Configure the DMA Tx/Rx channel.
  38.             (+++) Associate the initilalized DMA handle to the SMARTCARD DMA Tx/Rx handle.
  39.             (+++) Configure the priority and enable the NVIC for the transfer complete interrupt on the DMA Tx/Rx channel.
  40.             (+++) Configure the USARTx interrupt priority and enable the NVIC USART IRQ handle
  41.                   (used for last byte sending completion detection in DMA non circular mode)
  42.  
  43.     (#) Program the Baud Rate, Word Length , Stop Bit, Parity, Hardware
  44.         flow control and Mode(Receiver/Transmitter) in the SMARTCARD Init structure.
  45.  
  46.     (#) Initialize the SMARTCARD registers by calling the HAL_SMARTCARD_Init() API:
  47.         (++) This API configures also the low level Hardware GPIO, CLOCK, CORTEX...etc)
  48.              by calling the customed HAL_SMARTCARD_MspInit(&hsc) API.
  49.  
  50.         -@@- The specific SMARTCARD interrupts (Transmission complete interrupt,
  51.              RXNE interrupt and Error Interrupts) will be managed using the macros
  52.              __HAL_SMARTCARD_ENABLE_IT() and __HAL_SMARTCARD_DISABLE_IT() inside the transmit and receive process.
  53.          
  54.     (#) Three operation modes are available within this driver :
  55.  
  56.      *** Polling mode IO operation ***
  57.      =================================
  58.      [..]    
  59.        (+) Send an amount of data in blocking mode using HAL_SMARTCARD_Transmit()
  60.        (+) Receive an amount of data in blocking mode using HAL_SMARTCARD_Receive()
  61.        
  62.      *** Interrupt mode IO operation ***    
  63.      ===================================
  64.      [..]    
  65.        (+) Send an amount of data in non blocking mode using HAL_SMARTCARD_Transmit_IT()
  66.        (+) At transmission end of transfer HAL_SMARTCARD_TxCpltCallback is executed and user can
  67.             add his own code by customization of function pointer HAL_SMARTCARD_TxCpltCallback
  68.        (+) Receive an amount of data in non blocking mode using HAL_SMARTCARD_Receive_IT()
  69.        (+) At reception end of transfer HAL_SMARTCARD_RxCpltCallback is executed and user can
  70.             add his own code by customization of function pointer HAL_SMARTCARD_RxCpltCallback                                      
  71.        (+) In case of transfer Error, HAL_SMARTCARD_ErrorCallback() function is executed and user can
  72.             add his own code by customization of function pointer HAL_SMARTCARD_ErrorCallback
  73.  
  74.      *** DMA mode IO operation ***    
  75.      ==============================
  76.      [..]
  77.        (+) Send an amount of data in non blocking mode (DMA) using HAL_SMARTCARD_Transmit_DMA()
  78.        (+) At transmission end of transfer HAL_SMARTCARD_TxCpltCallback is executed and user can
  79.             add his own code by customization of function pointer HAL_SMARTCARD_TxCpltCallback
  80.        (+) Receive an amount of data in non blocking mode (DMA) using HAL_SMARTCARD_Receive_DMA()
  81.        (+) At reception end of transfer HAL_SMARTCARD_RxCpltCallback is executed and user can
  82.             add his own code by customization of function pointer HAL_SMARTCARD_RxCpltCallback                                      
  83.        (+) In case of transfer Error, HAL_SMARTCARD_ErrorCallback() function is executed and user can
  84.             add his own code by customization of function pointer HAL_SMARTCARD_ErrorCallback
  85.  
  86.      *** SMARTCARD HAL driver macros list ***
  87.      ========================================
  88.      [..]
  89.        Below the list of most used macros in SMARTCARD HAL driver.
  90.        
  91.        (+) __HAL_SMARTCARD_ENABLE: Enable the SMARTCARD peripheral
  92.        (+) __HAL_SMARTCARD_DISABLE: Disable the SMARTCARD peripheral    
  93.        (+) __HAL_SMARTCARD_GET_FLAG : Check whether the specified SMARTCARD flag is set or not
  94.        (+) __HAL_SMARTCARD_CLEAR_FLAG : Clear the specified SMARTCARD pending flag
  95.        (+) __HAL_SMARTCARD_ENABLE_IT: Enable the specified SMARTCARD interrupt
  96.        (+) __HAL_SMARTCARD_DISABLE_IT: Disable the specified SMARTCARD interrupt
  97.        (+) __HAL_SMARTCARD_GET_IT_SOURCE: Check whether the specified SMARTCARD interrupt has occurred or not
  98.      
  99.      [..]
  100.        (@) You can refer to the SMARTCARD HAL driver header file for more useful macros
  101.  
  102.   @endverbatim
  103.   ******************************************************************************
  104.   * @attention
  105.   *
  106.   * <h2><center>&copy; COPYRIGHT(c) 2016 STMicroelectronics</center></h2>
  107.   *
  108.   * Redistribution and use in source and binary forms, with or without modification,
  109.   * are permitted provided that the following conditions are met:
  110.   *   1. Redistributions of source code must retain the above copyright notice,
  111.   *      this list of conditions and the following disclaimer.
  112.   *   2. Redistributions in binary form must reproduce the above copyright notice,
  113.   *      this list of conditions and the following disclaimer in the documentation
  114.   *      and/or other materials provided with the distribution.
  115.   *   3. Neither the name of STMicroelectronics nor the names of its contributors
  116.   *      may be used to endorse or promote products derived from this software
  117.   *      without specific prior written permission.
  118.   *
  119.   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  120.   * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  121.   * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  122.   * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  123.   * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  124.   * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  125.   * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  126.   * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  127.   * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  128.   * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  129.   *
  130.   ******************************************************************************
  131.   */
  132.  
  133. /* Includes ------------------------------------------------------------------*/
  134. #include "stm32f1xx_hal.h"
  135.  
  136. /** @addtogroup STM32F1xx_HAL_Driver
  137.   * @{
  138.   */
  139.  
  140. /** @defgroup SMARTCARD SMARTCARD
  141.   * @brief HAL SMARTCARD module driver
  142.   * @{
  143.   */
  144.  
  145. #ifdef HAL_SMARTCARD_MODULE_ENABLED
  146.  
  147. /* Private typedef -----------------------------------------------------------*/
  148. /* Private define ------------------------------------------------------------*/
  149. /* Private macros --------------------------------------------------------*/
  150. /* Private variables ---------------------------------------------------------*/
  151. /* Private function prototypes -----------------------------------------------*/
  152. /** @addtogroup SMARTCARD_Private_Functions   SMARTCARD Private Functions
  153.   * @{
  154.   */
  155. static HAL_StatusTypeDef SMARTCARD_Transmit_IT(SMARTCARD_HandleTypeDef *hsc);
  156. static HAL_StatusTypeDef SMARTCARD_EndTransmit_IT(SMARTCARD_HandleTypeDef *hsmartcard);
  157. static HAL_StatusTypeDef SMARTCARD_Receive_IT(SMARTCARD_HandleTypeDef *hsc);
  158. static void SMARTCARD_SetConfig (SMARTCARD_HandleTypeDef *hsc);
  159. static void SMARTCARD_DMATransmitCplt(DMA_HandleTypeDef *hdma);
  160. static void SMARTCARD_DMAReceiveCplt(DMA_HandleTypeDef *hdma);
  161. static void SMARTCARD_DMAError(DMA_HandleTypeDef *hdma);
  162. static HAL_StatusTypeDef SMARTCARD_WaitOnFlagUntilTimeout(SMARTCARD_HandleTypeDef *hsc, uint32_t Flag, FlagStatus Status, uint32_t Timeout);
  163. /**
  164.   * @}
  165.   */
  166.  
  167. /* Exported functions ---------------------------------------------------------*/
  168.  
  169. /** @defgroup SMARTCARD_Exported_Functions SMARTCARD Exported Functions
  170.   * @{
  171.   */
  172.  
  173. /** @defgroup SMARTCARD_Exported_Functions_Group1 Initialization and de-initialization functions
  174.   *  @brief    Initialization and Configuration functions
  175.   *
  176. @verbatim
  177.  
  178.   ==============================================================================
  179.               ##### Initialization and Configuration functions #####
  180.   ==============================================================================
  181.   [..]
  182.   This subsection provides a set of functions allowing to initialize the USART
  183.   in Smartcard mode.
  184.   [..]
  185.   The Smartcard interface is designed to support asynchronous protocol Smartcards as
  186.   defined in the ISO 7816-3 standard.
  187.   [..]
  188.   The USART can provide a clock to the smartcard through the SCLK output.
  189.   In smartcard mode, SCLK is not associated to the communication but is simply derived
  190.   from the internal peripheral input clock through a 5-bit prescaler.
  191.   [..]
  192.   (+) For the Smartcard mode only these parameters can be configured:
  193.       (++) Baud Rate
  194.       (++) Word Length => Should be 9 bits (8 bits + parity)
  195.       (++) Stop Bit
  196.       (++) Parity: => Should be enabled
  197.       (++) USART polarity
  198.       (++) USART phase
  199.       (++) USART LastBit
  200.       (++) Receiver/transmitter modes
  201.       (++) Prescaler
  202.       (++) GuardTime
  203.       (++) NACKState: The Smartcard NACK state
  204.  
  205.      (+) Recommended SmartCard interface configuration to get the Answer to Reset from the Card:
  206.         (++) Word Length = 9 Bits
  207.         (++) 1.5 Stop Bit
  208.         (++) Even parity
  209.         (++) BaudRate = 12096 baud
  210.         (++) Tx and Rx enabled
  211.   [..]
  212.   Please refer to the ISO 7816-3 specification for more details.
  213.  
  214.     (@) It is also possible to choose 0.5 stop bit for receiving but it is recommended
  215.         to use 1.5 stop bits for both transmitting and receiving to avoid switching
  216.         between the two configurations.
  217.   [..]
  218.   The HAL_SMARTCARD_Init() function follows the USART  SmartCard configuration
  219.   procedure (details for the procedure are available in reference manuals
  220.   (RM0008 for STM32F10Xxx MCUs and RM0041 for STM32F100xx MCUs)).
  221.  
  222. @endverbatim
  223.   * @{
  224.   */
  225.  
  226. /*
  227.   Additionnal remark on the smartcard frame:
  228.    +-------------------------------------------------------------+
  229.    |   M bit |  PCE bit  |        SMARTCARD frame                |
  230.    |---------------------|---------------------------------------|
  231.    |    1    |    1      |    | SB | 8 bit data | PB | STB |     |
  232.    +-------------------------------------------------------------+
  233. */
  234.  
  235. /**
  236.   * @brief  Initializes the SmartCard mode according to the specified
  237.   *         parameters in the SMARTCARD_HandleTypeDef and create the associated handle.
  238.   * @param  hsc: Pointer to a SMARTCARD_HandleTypeDef structure that contains
  239.   *              the configuration information for the specified SMARTCARD module.
  240.   * @retval HAL status
  241.   */
  242. HAL_StatusTypeDef HAL_SMARTCARD_Init(SMARTCARD_HandleTypeDef *hsc)
  243. {
  244.   /* Check the SMARTCARD handle allocation */
  245.   if(hsc == NULL)
  246.   {
  247.     return HAL_ERROR;
  248.   }
  249.  
  250.   /* Check Wordlength, Parity and Stop bits parameters */
  251.   if (  (!(IS_SMARTCARD_WORD_LENGTH(hsc->Init.WordLength)))
  252.       ||(!(IS_SMARTCARD_STOPBITS(hsc->Init.StopBits)))
  253.       ||(!(IS_SMARTCARD_PARITY(hsc->Init.Parity)))  )
  254.   {
  255.     return HAL_ERROR;
  256.   }
  257.  
  258.   /* Check the parameters */
  259.   assert_param(IS_SMARTCARD_INSTANCE(hsc->Instance));
  260.   assert_param(IS_SMARTCARD_NACK_STATE(hsc->Init.NACKState));
  261.   assert_param(IS_SMARTCARD_PRESCALER(hsc->Init.Prescaler));
  262.  
  263.   if(hsc->State == HAL_SMARTCARD_STATE_RESET)
  264.   {  
  265.     /* Allocate lock resource and initialize it */
  266.     hsc->Lock = HAL_UNLOCKED;
  267.    
  268.     /* Init the low level hardware */
  269.     HAL_SMARTCARD_MspInit(hsc);
  270.   }
  271.  
  272.   hsc->State = HAL_SMARTCARD_STATE_BUSY;
  273.  
  274.   /* Disable the Peripheral */
  275.   __HAL_SMARTCARD_DISABLE(hsc);
  276.  
  277.   /* Set the Prescaler */
  278.   MODIFY_REG(hsc->Instance->GTPR, USART_GTPR_PSC, hsc->Init.Prescaler);
  279.  
  280.   /* Set the Guard Time */
  281.   MODIFY_REG(hsc->Instance->GTPR, USART_GTPR_GT, ((hsc->Init.GuardTime)<<8));
  282.  
  283.   /* Set the Smartcard Communication parameters */
  284.   SMARTCARD_SetConfig(hsc);
  285.  
  286.   /* In SmartCard mode, the following bits must be kept cleared:
  287.   - LINEN bit in the USART_CR2 register
  288.   - HDSEL and IREN bits in the USART_CR3 register.*/
  289.   CLEAR_BIT(hsc->Instance->CR2, USART_CR2_LINEN);
  290.   CLEAR_BIT(hsc->Instance->CR3, (USART_CR3_IREN | USART_CR3_HDSEL));
  291.  
  292.   /* Enable the Peripharal */
  293.   __HAL_SMARTCARD_ENABLE(hsc);
  294.  
  295.   /* Configure the Smartcard NACK state */
  296.   MODIFY_REG(hsc->Instance->CR3, USART_CR3_NACK, hsc->Init.NACKState);
  297.  
  298.   /* Enable the SC mode by setting the SCEN bit in the CR3 register */
  299.   SET_BIT(hsc->Instance->CR3, USART_CR3_SCEN);
  300.  
  301.   /* Initialize the SMARTCARD state*/
  302.   hsc->ErrorCode = HAL_SMARTCARD_ERROR_NONE;
  303.   hsc->State= HAL_SMARTCARD_STATE_READY;
  304.  
  305.   return HAL_OK;
  306. }
  307.  
  308. /**
  309.   * @brief  DeInitializes the SMARTCARD peripheral
  310.   * @param  hsc: Pointer to a SMARTCARD_HandleTypeDef structure that contains
  311.   *              the configuration information for the specified SMARTCARD module.
  312.   * @retval HAL status
  313.   */
  314. HAL_StatusTypeDef HAL_SMARTCARD_DeInit(SMARTCARD_HandleTypeDef *hsc)
  315. {
  316.   /* Check the SMARTCARD handle allocation */
  317.   if(hsc == NULL)
  318.   {
  319.     return HAL_ERROR;
  320.   }
  321.  
  322.   /* Check the parameters */
  323.   assert_param(IS_SMARTCARD_INSTANCE(hsc->Instance));
  324.  
  325.   hsc->State = HAL_SMARTCARD_STATE_BUSY;
  326.  
  327.   /* Disable the Peripheral */
  328.   __HAL_SMARTCARD_DISABLE(hsc);
  329.  
  330.   hsc->Instance->CR1 = 0x0;
  331.   hsc->Instance->CR2 = 0x0;
  332.   hsc->Instance->CR3 = 0x0;
  333.   hsc->Instance->BRR = 0x0;
  334.   hsc->Instance->GTPR = 0x0;
  335.  
  336.   /* DeInit the low level hardware */
  337.   HAL_SMARTCARD_MspDeInit(hsc);
  338.  
  339.   hsc->ErrorCode = HAL_SMARTCARD_ERROR_NONE;
  340.   hsc->State = HAL_SMARTCARD_STATE_RESET;
  341.  
  342.   /* Release Lock */
  343.   __HAL_UNLOCK(hsc);
  344.  
  345.   return HAL_OK;
  346. }
  347.  
  348. /**
  349.   * @brief  SMARTCARD MSP Init.
  350.   * @param  hsc: Pointer to a SMARTCARD_HandleTypeDef structure that contains
  351.   *                the configuration information for the specified SMARTCARD module.
  352.   * @retval None
  353.   */
  354.  __weak void HAL_SMARTCARD_MspInit(SMARTCARD_HandleTypeDef *hsc)
  355. {
  356.   /* Prevent unused argument(s) compilation warning */
  357.   UNUSED(hsc);
  358.   /* NOTE: This function should not be modified, when the callback is needed,
  359.            the HAL_SMARTCARD_MspInit can be implemented in the user file
  360.    */
  361. }
  362.  
  363. /**
  364.   * @brief  SMARTCARD MSP DeInit.
  365.   * @param  hsc: Pointer to a SMARTCARD_HandleTypeDef structure that contains
  366.   *                the configuration information for the specified SMARTCARD module.
  367.   * @retval None
  368.   */
  369.  __weak void HAL_SMARTCARD_MspDeInit(SMARTCARD_HandleTypeDef *hsc)
  370. {
  371.   /* Prevent unused argument(s) compilation warning */
  372.   UNUSED(hsc);
  373.   /* NOTE: This function should not be modified, when the callback is needed,
  374.            the HAL_SMARTCARD_MspDeInit can be implemented in the user file
  375.    */
  376. }
  377.  
  378. /**
  379.   * @}
  380.   */
  381.  
  382. /** @defgroup SMARTCARD_Exported_Functions_Group2 IO operation functions
  383.   *  @brief   SMARTCARD Transmit and Receive functions
  384.   *
  385. @verbatim
  386.   ==============================================================================
  387.                          ##### IO operation functions #####
  388.   ==============================================================================
  389.   [..]
  390.     This subsection provides a set of functions allowing to manage the SMARTCARD data transfers.
  391.  
  392.   [..]
  393.     (#) Smartcard is a single wire half duplex communication protocol.
  394.     The Smartcard interface is designed to support asynchronous protocol Smartcards as
  395.     defined in the ISO 7816-3 standard.
  396.     (#) The USART should be configured as:
  397.         (++) 8 bits plus parity: where M=1 and PCE=1 in the USART_CR1 register
  398.         (++) 1.5 stop bits when transmitting and receiving: where STOP=11 in the USART_CR2 register.
  399.  
  400.     (#) There are two modes of transfer:
  401.         (++) Blocking mode: The communication is performed in polling mode.
  402.              The HAL status of all data processing is returned by the same function
  403.              after finishing transfer.  
  404.         (++) No-Blocking mode: The communication is performed using Interrupts
  405.              or DMA, the relevant API's return the HAL status.
  406.              The end of the data processing will be indicated through the
  407.              dedicated SMARTCARD IRQ when using Interrupt mode or the DMA IRQ when
  408.              using DMA mode.
  409.              The HAL_SMARTCARD_TxCpltCallback(), HAL_SMARTCARD_RxCpltCallback() user callbacks
  410.              will be executed respectively at the end of the Transmit or Receive process
  411.              The HAL_SMARTCARD_ErrorCallback() user callback will be executed when a communication
  412.              error is detected.
  413.  
  414.     (#) Blocking mode APIs are :
  415.         (++) HAL_SMARTCARD_Transmit()
  416.         (++) HAL_SMARTCARD_Receive()
  417.  
  418.     (#) Non Blocking mode APIs with Interrupt are :
  419.         (++) HAL_SMARTCARD_Transmit_IT()
  420.         (++) HAL_SMARTCARD_Receive_IT()
  421.         (++) HAL_SMARTCARD_IRQHandler()
  422.  
  423.     (#) Non Blocking mode functions with DMA are :
  424.         (++) HAL_SMARTCARD_Transmit_DMA()
  425.         (++) HAL_SMARTCARD_Receive_DMA()
  426.  
  427.     (#) A set of Transfer Complete Callbacks are provided in non Blocking mode:
  428.         (++) HAL_SMARTCARD_TxCpltCallback()
  429.         (++) HAL_SMARTCARD_RxCpltCallback()
  430.         (++) HAL_SMARTCARD_ErrorCallback()
  431.  
  432. @endverbatim
  433.   * @{
  434.   */
  435.  
  436. /**
  437.   * @brief  Sends an amount of data in blocking mode.
  438.   * @param  hsc: Pointer to a SMARTCARD_HandleTypeDef structure that contains
  439.   *                the configuration information for the specified SMARTCARD module.
  440.   * @param  pData: Pointer to data buffer
  441.   * @param  Size: Amount of data to be sent
  442.   * @param  Timeout: Specify timeout value  
  443.   * @retval HAL status
  444.   */
  445. HAL_StatusTypeDef HAL_SMARTCARD_Transmit(SMARTCARD_HandleTypeDef *hsc, uint8_t *pData, uint16_t Size, uint32_t Timeout)
  446. {
  447.   uint32_t tmp_state = 0;
  448.  
  449.   tmp_state = hsc->State;
  450.   if((tmp_state == HAL_SMARTCARD_STATE_READY) || (tmp_state == HAL_SMARTCARD_STATE_BUSY_RX))
  451.   {
  452.     if((pData == NULL) || (Size == 0))
  453.     {
  454.       return  HAL_ERROR;
  455.     }
  456.  
  457.     /* Process Locked */
  458.     __HAL_LOCK(hsc);
  459.  
  460.     hsc->ErrorCode = HAL_SMARTCARD_ERROR_NONE;
  461.     /* Check if a non-blocking receive process is ongoing or not */
  462.     if(hsc->State == HAL_SMARTCARD_STATE_BUSY_RX)
  463.     {
  464.       hsc->State = HAL_SMARTCARD_STATE_BUSY_TX_RX;
  465.     }
  466.     else
  467.     {
  468.       hsc->State = HAL_SMARTCARD_STATE_BUSY_TX;
  469.     }
  470.    
  471.     hsc->TxXferSize = Size;
  472.     hsc->TxXferCount = Size;
  473.     while(hsc->TxXferCount > 0)
  474.     {
  475.       if(SMARTCARD_WaitOnFlagUntilTimeout(hsc, SMARTCARD_FLAG_TXE, RESET, Timeout) != HAL_OK)
  476.       {
  477.         return HAL_TIMEOUT;
  478.       }
  479.       WRITE_REG(hsc->Instance->DR, (*pData++ & (uint8_t)0xFF));
  480.       hsc->TxXferCount--;
  481.     }
  482.  
  483.     if(SMARTCARD_WaitOnFlagUntilTimeout(hsc, SMARTCARD_FLAG_TC, RESET, Timeout) != HAL_OK)
  484.     {
  485.       return HAL_TIMEOUT;
  486.     }
  487.  
  488.     /* Check if a non-blocking receive process is ongoing or not */
  489.     if(hsc->State == HAL_SMARTCARD_STATE_BUSY_TX_RX)
  490.     {
  491.       hsc->State = HAL_SMARTCARD_STATE_BUSY_RX;
  492.     }
  493.     else
  494.     {
  495.       hsc->State = HAL_SMARTCARD_STATE_READY;
  496.     }
  497.     /* Process Unlocked */
  498.     __HAL_UNLOCK(hsc);
  499.  
  500.     return HAL_OK;
  501.   }
  502.   else
  503.   {
  504.     return HAL_BUSY;
  505.   }
  506. }
  507.  
  508. /**
  509.   * @brief  Receive an amount of data in blocking mode.
  510.   * @param  hsc: Pointer to a SMARTCARD_HandleTypeDef structure that contains
  511.   *                the configuration information for the specified SMARTCARD module.
  512.   * @param  pData: Pointer to data buffer
  513.   * @param  Size: Amount of data to be received
  514.   * @param  Timeout: Specify timeout value    
  515.   * @retval HAL status
  516.   */
  517. HAL_StatusTypeDef HAL_SMARTCARD_Receive(SMARTCARD_HandleTypeDef *hsc, uint8_t *pData, uint16_t Size, uint32_t Timeout)
  518. {
  519.   uint32_t tmp_state = 0;
  520.  
  521.   tmp_state = hsc->State;
  522.   if((tmp_state == HAL_SMARTCARD_STATE_READY) || (tmp_state == HAL_SMARTCARD_STATE_BUSY_TX))
  523.   {
  524.     if((pData == NULL) || (Size == 0))
  525.     {
  526.       return  HAL_ERROR;
  527.     }
  528.  
  529.     /* Process Locked */
  530.     __HAL_LOCK(hsc);
  531.    
  532.     hsc->ErrorCode = HAL_SMARTCARD_ERROR_NONE;
  533.  
  534.     /* Check if a non-blocking transmit process is ongoing or not */
  535.     if(hsc->State == HAL_SMARTCARD_STATE_BUSY_TX)
  536.     {
  537.       hsc->State = HAL_SMARTCARD_STATE_BUSY_TX_RX;
  538.     }
  539.     else
  540.     {
  541.       hsc->State = HAL_SMARTCARD_STATE_BUSY_RX;
  542.     }
  543.  
  544.     hsc->RxXferSize = Size;
  545.     hsc->RxXferCount = Size;
  546.     /* Check the remain data to be received */
  547.     while(hsc->RxXferCount > 0)
  548.     {
  549.       if(SMARTCARD_WaitOnFlagUntilTimeout(hsc, SMARTCARD_FLAG_RXNE, RESET, Timeout) != HAL_OK)
  550.       {
  551.         return HAL_TIMEOUT;
  552.       }
  553.       *pData++ = (uint8_t)(hsc->Instance->DR & (uint8_t)0x00FF);              
  554.       hsc->RxXferCount--;
  555.     }
  556.  
  557.     /* Check if a non-blocking transmit process is ongoing or not */
  558.     if(hsc->State == HAL_SMARTCARD_STATE_BUSY_TX_RX)
  559.     {
  560.       hsc->State = HAL_SMARTCARD_STATE_BUSY_TX;
  561.     }
  562.     else
  563.     {
  564.       hsc->State = HAL_SMARTCARD_STATE_READY;
  565.     }
  566.  
  567.     /* Process Unlocked */
  568.     __HAL_UNLOCK(hsc);
  569.  
  570.     return HAL_OK;
  571.   }
  572.   else
  573.   {
  574.     return HAL_BUSY;
  575.   }
  576. }
  577.  
  578. /**
  579.   * @brief  Sends an amount of data in non-blocking mode.
  580.   * @param  hsc: Pointer to a SMARTCARD_HandleTypeDef structure that contains
  581.   *                the configuration information for the specified SMARTCARD module.
  582.   * @param  pData: Pointer to data buffer
  583.   * @param  Size: Amount of data to be sent
  584.   * @retval HAL status
  585.   */
  586. HAL_StatusTypeDef HAL_SMARTCARD_Transmit_IT(SMARTCARD_HandleTypeDef *hsc, uint8_t *pData, uint16_t Size)
  587. {
  588.   uint32_t tmp_state = 0;
  589.  
  590.   tmp_state = hsc->State;
  591.   if((tmp_state == HAL_SMARTCARD_STATE_READY) || (tmp_state == HAL_SMARTCARD_STATE_BUSY_RX))
  592.   {
  593.     if((pData == NULL) || (Size == 0))
  594.     {
  595.       return HAL_ERROR;
  596.     }
  597.  
  598.     /* Process Locked */
  599.     __HAL_LOCK(hsc);
  600.  
  601.     hsc->pTxBuffPtr = pData;
  602.     hsc->TxXferSize = Size;
  603.     hsc->TxXferCount = Size;
  604.  
  605.     hsc->ErrorCode = HAL_SMARTCARD_ERROR_NONE;
  606.     /* Check if a non-blocking receive process is ongoing or not */
  607.     if(hsc->State == HAL_SMARTCARD_STATE_BUSY_RX)
  608.     {
  609.       hsc->State = HAL_SMARTCARD_STATE_BUSY_TX_RX;
  610.     }
  611.     else
  612.     {
  613.       hsc->State = HAL_SMARTCARD_STATE_BUSY_TX;
  614.     }
  615.  
  616.     /* Process Unlocked */
  617.     __HAL_UNLOCK(hsc);
  618.  
  619.     /* Enable the SMARTCARD Error Interrupt: (Frame error, noise error, overrun error) */
  620.     __HAL_SMARTCARD_ENABLE_IT(hsc, SMARTCARD_IT_ERR);
  621.  
  622.     /* Enable the SMARTCARD Transmit data register empty Interrupt */
  623.     __HAL_SMARTCARD_ENABLE_IT(hsc, SMARTCARD_IT_TXE);
  624.  
  625.     return HAL_OK;
  626.   }
  627.   else
  628.   {
  629.     return HAL_BUSY;
  630.   }
  631. }
  632.  
  633. /**
  634.   * @brief  Receives an amount of data in non-blocking mode.
  635.   * @param  hsc: Pointer to a SMARTCARD_HandleTypeDef structure that contains
  636.   *                the configuration information for the specified SMARTCARD module.
  637.   * @param  pData: Pointer to data buffer
  638.   * @param  Size: Amount of data to be received
  639.   * @retval HAL status
  640.   */
  641. HAL_StatusTypeDef HAL_SMARTCARD_Receive_IT(SMARTCARD_HandleTypeDef *hsc, uint8_t *pData, uint16_t Size)
  642. {
  643.   uint32_t tmp_state = 0;
  644.  
  645.   tmp_state = hsc->State;
  646.   if((tmp_state == HAL_SMARTCARD_STATE_READY) || (tmp_state == HAL_SMARTCARD_STATE_BUSY_TX))
  647.   {
  648.     if((pData == NULL) || (Size == 0))
  649.     {
  650.       return HAL_ERROR;
  651.     }
  652.  
  653.     /* Process Locked */
  654.     __HAL_LOCK(hsc);
  655.  
  656.     hsc->pRxBuffPtr = pData;
  657.     hsc->RxXferSize = Size;
  658.     hsc->RxXferCount = Size;
  659.  
  660.     hsc->ErrorCode = HAL_SMARTCARD_ERROR_NONE;
  661.     /* Check if a non-blocking transmit process is ongoing or not */
  662.     if(hsc->State == HAL_SMARTCARD_STATE_BUSY_TX)
  663.     {
  664.       hsc->State = HAL_SMARTCARD_STATE_BUSY_TX_RX;
  665.     }
  666.     else
  667.     {
  668.       hsc->State = HAL_SMARTCARD_STATE_BUSY_RX;
  669.     }
  670.  
  671.     /* Process Unlocked */
  672.     __HAL_UNLOCK(hsc);
  673.  
  674.     /* Enable the SMARTCARD Data Register not empty Interrupt */
  675.     __HAL_SMARTCARD_ENABLE_IT(hsc, SMARTCARD_IT_RXNE);
  676.  
  677.     /* Enable the SMARTCARD Parity Error Interrupt */
  678.     __HAL_SMARTCARD_ENABLE_IT(hsc, SMARTCARD_IT_PE);
  679.  
  680.     /* Enable the SMARTCARD Error Interrupt: (Frame error, noise error, overrun error) */
  681.     __HAL_SMARTCARD_ENABLE_IT(hsc, SMARTCARD_IT_ERR);
  682.  
  683.     return HAL_OK;
  684.   }
  685.   else
  686.   {
  687.     return HAL_BUSY;
  688.   }
  689. }
  690.  
  691. /**
  692.   * @brief  Sends an amount of data in non-blocking mode.
  693.   * @param  hsc: Pointer to a SMARTCARD_HandleTypeDef structure that contains
  694.   *                the configuration information for the specified SMARTCARD module.
  695.   * @param  pData: Pointer to data buffer
  696.   * @param  Size: Amount of data to be sent
  697.   * @retval HAL status
  698.   */
  699. HAL_StatusTypeDef HAL_SMARTCARD_Transmit_DMA(SMARTCARD_HandleTypeDef *hsc, uint8_t *pData, uint16_t Size)
  700. {
  701.   uint32_t *tmp = 0;
  702.   uint32_t tmp_state = 0;
  703.  
  704.   tmp_state = hsc->State;
  705.   if((tmp_state == HAL_SMARTCARD_STATE_READY) || (tmp_state == HAL_SMARTCARD_STATE_BUSY_RX))
  706.   {
  707.     if((pData == NULL) || (Size == 0))
  708.     {
  709.       return HAL_ERROR;
  710.     }
  711.  
  712.     /* Process Locked */
  713.     __HAL_LOCK(hsc);
  714.  
  715.     hsc->pTxBuffPtr = pData;
  716.     hsc->TxXferSize = Size;
  717.     hsc->TxXferCount = Size;
  718.  
  719.     hsc->ErrorCode = HAL_SMARTCARD_ERROR_NONE;
  720.     /* Check if a non-blocking receive process is ongoing or not */
  721.     if(hsc->State == HAL_SMARTCARD_STATE_BUSY_RX)
  722.     {
  723.       hsc->State = HAL_SMARTCARD_STATE_BUSY_TX_RX;
  724.     }
  725.     else
  726.     {
  727.       hsc->State = HAL_SMARTCARD_STATE_BUSY_TX;
  728.     }
  729.  
  730.     /* Set the SMARTCARD DMA transfer complete callback */
  731.     hsc->hdmatx->XferCpltCallback = SMARTCARD_DMATransmitCplt;
  732.  
  733.     /* Set the DMA error callback */
  734.     hsc->hdmatx->XferErrorCallback = SMARTCARD_DMAError;
  735.  
  736.     /* Enable the SMARTCARD transmit DMA channel */
  737.     tmp = (uint32_t*)&pData;
  738.     HAL_DMA_Start_IT(hsc->hdmatx, *(uint32_t*)tmp, (uint32_t)&hsc->Instance->DR, Size);
  739.  
  740.      /* Clear the TC flag in the SR register by writing 0 to it */
  741.     __HAL_SMARTCARD_CLEAR_FLAG(hsc, SMARTCARD_FLAG_TC);
  742.  
  743.     /* Enable the DMA transfer for transmit request by setting the DMAT bit
  744.     in the SMARTCARD CR3 register */
  745.     SET_BIT(hsc->Instance->CR3,USART_CR3_DMAT);
  746.  
  747.     /* Process Unlocked */
  748.     __HAL_UNLOCK(hsc);
  749.  
  750.     return HAL_OK;
  751.   }
  752.   else
  753.   {
  754.     return HAL_BUSY;
  755.   }
  756. }
  757.  
  758. /**
  759.   * @brief  Receive an amount of data in non-blocking mode.
  760.   * @param  hsc: Pointer to a SMARTCARD_HandleTypeDef structure that contains
  761.   *                the configuration information for the specified SMARTCARD module.
  762.   * @param  pData: Pointer to data buffer
  763.   * @param  Size: Amount of data to be received
  764.   * @note   When the SMARTCARD parity is enabled (PCE = 1) the data received contain the parity bit.
  765.   * @retval HAL status
  766.   */
  767. HAL_StatusTypeDef HAL_SMARTCARD_Receive_DMA(SMARTCARD_HandleTypeDef *hsc, uint8_t *pData, uint16_t Size)
  768. {
  769.   uint32_t *tmp = 0;
  770.   uint32_t tmp_state = 0;
  771.  
  772.   tmp_state = hsc->State;
  773.   if((tmp_state == HAL_SMARTCARD_STATE_READY) || (tmp_state == HAL_SMARTCARD_STATE_BUSY_TX))
  774.   {
  775.     if((pData == NULL) || (Size == 0))
  776.     {
  777.       return HAL_ERROR;
  778.     }
  779.  
  780.     /* Process Locked */
  781.     __HAL_LOCK(hsc);
  782.  
  783.     hsc->pRxBuffPtr = pData;
  784.     hsc->RxXferSize = Size;
  785.  
  786.     hsc->ErrorCode = HAL_SMARTCARD_ERROR_NONE;
  787.     /* Check if a non-blocking transmit process is ongoing or not */
  788.     if(hsc->State == HAL_SMARTCARD_STATE_BUSY_TX)
  789.     {
  790.       hsc->State = HAL_SMARTCARD_STATE_BUSY_TX_RX;
  791.     }
  792.     else
  793.     {
  794.       hsc->State = HAL_SMARTCARD_STATE_BUSY_RX;
  795.     }
  796.  
  797.     /* Set the SMARTCARD DMA transfer complete callback */
  798.     hsc->hdmarx->XferCpltCallback = SMARTCARD_DMAReceiveCplt;
  799.  
  800.     /* Set the DMA error callback */
  801.     hsc->hdmarx->XferErrorCallback = SMARTCARD_DMAError;
  802.  
  803.     /* Enable the DMA channel */
  804.     tmp = (uint32_t*)&pData;
  805.     HAL_DMA_Start_IT(hsc->hdmarx, (uint32_t)&hsc->Instance->DR, *(uint32_t*)tmp, Size);
  806.  
  807.     /* Enable the DMA transfer for the receiver request by setting the DMAR bit
  808.     in the SMARTCARD CR3 register */
  809.     SET_BIT(hsc->Instance->CR3,USART_CR3_DMAR);
  810.  
  811.     /* Process Unlocked */
  812.     __HAL_UNLOCK(hsc);
  813.  
  814.     return HAL_OK;
  815.   }
  816.   else
  817.   {
  818.     return HAL_BUSY;
  819.   }
  820. }
  821.  
  822. /**
  823.   * @brief  This function handles SMARTCARD interrupt request.
  824.   * @param  hsc: Pointer to a SMARTCARD_HandleTypeDef structure that contains
  825.   *                the configuration information for the specified SMARTCARD module.
  826.   * @retval None
  827.   */
  828. void HAL_SMARTCARD_IRQHandler(SMARTCARD_HandleTypeDef *hsc)
  829. {
  830.   uint32_t  tmp_flag = 0, tmp_it_source = 0;
  831.  
  832.   tmp_flag = __HAL_SMARTCARD_GET_FLAG(hsc, SMARTCARD_FLAG_PE);
  833.   tmp_it_source = __HAL_SMARTCARD_GET_IT_SOURCE(hsc, SMARTCARD_IT_PE);
  834.   /* SMARTCARD parity error interrupt occurred -----------------------------------*/
  835.   if((tmp_flag != RESET) && (tmp_it_source != RESET))
  836.   {
  837.     hsc->ErrorCode |= HAL_SMARTCARD_ERROR_PE;
  838.   }
  839.  
  840.   tmp_flag = __HAL_SMARTCARD_GET_FLAG(hsc, SMARTCARD_FLAG_FE);
  841.   tmp_it_source = __HAL_SMARTCARD_GET_IT_SOURCE(hsc, SMARTCARD_IT_ERR);
  842.   /* SMARTCARD frame error interrupt occurred ------------------------------------*/
  843.   if((tmp_flag != RESET) && (tmp_it_source != RESET))
  844.   {
  845.     hsc->ErrorCode |= HAL_SMARTCARD_ERROR_FE;
  846.   }
  847.  
  848.   tmp_flag = __HAL_SMARTCARD_GET_FLAG(hsc, SMARTCARD_FLAG_NE);
  849.   /* SMARTCARD noise error interrupt occurred ------------------------------------*/
  850.   if((tmp_flag != RESET) && (tmp_it_source != RESET))
  851.   {
  852.     hsc->ErrorCode |= HAL_SMARTCARD_ERROR_NE;
  853.   }
  854.  
  855.   tmp_flag = __HAL_SMARTCARD_GET_FLAG(hsc, SMARTCARD_FLAG_ORE);
  856.   /* SMARTCARD Over-Run interrupt occurred ---------------------------------------*/
  857.   if((tmp_flag != RESET) && (tmp_it_source != RESET))
  858.   {
  859.     hsc->ErrorCode |= HAL_SMARTCARD_ERROR_ORE;
  860.   }
  861.  
  862.   tmp_flag = __HAL_SMARTCARD_GET_FLAG(hsc, SMARTCARD_FLAG_RXNE);
  863.   tmp_it_source = __HAL_SMARTCARD_GET_IT_SOURCE(hsc, SMARTCARD_IT_RXNE);
  864.   /* SMARTCARD in mode Receiver --------------------------------------------------*/
  865.   if((tmp_flag != RESET) && (tmp_it_source != RESET))
  866.   {
  867.     SMARTCARD_Receive_IT(hsc);
  868.   }
  869.  
  870.   tmp_flag = __HAL_SMARTCARD_GET_FLAG(hsc, SMARTCARD_FLAG_TXE);
  871.   tmp_it_source = __HAL_SMARTCARD_GET_IT_SOURCE(hsc, SMARTCARD_IT_TXE);
  872.   /* SMARTCARD in mode Transmitter -----------------------------------------------*/
  873.   if((tmp_flag != RESET) && (tmp_it_source != RESET))
  874.   {
  875.     SMARTCARD_Transmit_IT(hsc);
  876.   }
  877.  
  878.   tmp_flag = __HAL_SMARTCARD_GET_FLAG(hsc, SMARTCARD_FLAG_TC);
  879.   tmp_it_source = __HAL_SMARTCARD_GET_IT_SOURCE(hsc, SMARTCARD_IT_TC);
  880.   /* SMARTCARD in mode Transmitter (transmission end) ------------------------*/
  881.   if((tmp_flag != RESET) && (tmp_it_source != RESET))
  882.   {
  883.     SMARTCARD_EndTransmit_IT(hsc);
  884.   }
  885.  
  886.   /* Call the Error call Back in case of Errors */
  887.   if(hsc->ErrorCode != HAL_SMARTCARD_ERROR_NONE)
  888.   {
  889.     /* Clear all the error flag at once */
  890.     __HAL_SMARTCARD_CLEAR_PEFLAG(hsc);
  891.  
  892.     /* Set the SMARTCARD state ready to be able to start again the process */
  893.     hsc->State= HAL_SMARTCARD_STATE_READY;
  894.     HAL_SMARTCARD_ErrorCallback(hsc);
  895.   }
  896. }
  897.  
  898. /**
  899.   * @brief  Tx Transfer completed callback.
  900.   * @param  hsc: Pointer to a SMARTCARD_HandleTypeDef structure that contains
  901.   *                the configuration information for the specified SMARTCARD module.
  902.   * @retval None
  903.   */
  904.  __weak void HAL_SMARTCARD_TxCpltCallback(SMARTCARD_HandleTypeDef *hsc)
  905. {
  906.   /* Prevent unused argument(s) compilation warning */
  907.   UNUSED(hsc);
  908.   /* NOTE: This function should not be modified, when the callback is needed,
  909.            the HAL_SMARTCARD_TxCpltCallback can be implemented in the user file
  910.    */
  911. }
  912.  
  913. /**
  914.   * @brief  Rx Transfer completed callback.
  915.   * @param  hsc: Pointer to a SMARTCARD_HandleTypeDef structure that contains
  916.   *                the configuration information for the specified SMARTCARD module.
  917.   * @retval None
  918.   */
  919. __weak void HAL_SMARTCARD_RxCpltCallback(SMARTCARD_HandleTypeDef *hsc)
  920. {
  921.   /* Prevent unused argument(s) compilation warning */
  922.   UNUSED(hsc);
  923.   /* NOTE: This function should not be modified, when the callback is needed,
  924.            the HAL_SMARTCARD_RxCpltCallback can be implemented in the user file
  925.    */
  926. }
  927.  
  928. /**
  929.   * @brief  SMARTCARD error callback.
  930.   * @param  hsc: Pointer to a SMARTCARD_HandleTypeDef structure that contains
  931.   *                the configuration information for the specified SMARTCARD module.
  932.   * @retval None
  933.   */
  934.  __weak void HAL_SMARTCARD_ErrorCallback(SMARTCARD_HandleTypeDef *hsc)
  935. {
  936.   /* Prevent unused argument(s) compilation warning */
  937.   UNUSED(hsc);
  938.   /* NOTE: This function should not be modified, when the callback is needed,
  939.            the HAL_SMARTCARD_ErrorCallback can be implemented in the user file
  940.    */
  941. }
  942.  
  943. /**
  944.   * @}
  945.   */
  946.  
  947. /** @defgroup SMARTCARD_Exported_Functions_Group3 Peripheral State and Errors functions
  948.   *  @brief   SMARTCARD State and Errors functions
  949.   *
  950. @verbatim  
  951.   ==============================================================================
  952.                   ##### Peripheral State and Errors functions #####
  953.   ==============================================================================  
  954.   [..]
  955.     This subsection provides a set of functions allowing to return the State of SmartCard
  956.     communication process and also return Peripheral Errors occurred during communication process
  957.      (+) HAL_SMARTCARD_GetState() API can be helpful to check in run-time the state
  958.          of the SMARTCARD peripheral.
  959.      (+) HAL_SMARTCARD_GetError() check in run-time errors that could be occurred during
  960.          communication.
  961.  
  962. @endverbatim
  963.   * @{
  964.   */
  965.  
  966. /**
  967.   * @brief  Returns the SMARTCARD state.
  968.   * @param  hsc: Pointer to a SMARTCARD_HandleTypeDef structure that contains
  969.   *                the configuration information for the specified SMARTCARD module.
  970.   * @retval HAL state
  971.   */
  972. HAL_SMARTCARD_StateTypeDef HAL_SMARTCARD_GetState(SMARTCARD_HandleTypeDef *hsc)
  973. {
  974.   return hsc->State;
  975. }
  976.  
  977. /**
  978.   * @brief  Return the SMARTCARD error code
  979.   * @param  hsc: Pointer to a SMARTCARD_HandleTypeDef structure that contains
  980.   *                the configuration information for the specified SMARTCARD module.
  981.   * @retval SMARTCARD Error Code
  982.   */
  983. uint32_t HAL_SMARTCARD_GetError(SMARTCARD_HandleTypeDef *hsc)
  984. {
  985.   return hsc->ErrorCode;
  986. }
  987.  
  988. /**
  989.   * @}
  990.   */
  991.  
  992. /**
  993.   * @}
  994.   */
  995.  
  996. /** @defgroup SMARTCARD_Private_Functions   SMARTCARD Private Functions
  997.   *  @brief   SMARTCARD Private functions
  998.   * @{
  999.   */
  1000. /**
  1001.   * @brief  DMA SMARTCARD transmit process complete callback.
  1002.   * @param  hdma: Pointer to a DMA_HandleTypeDef structure that contains
  1003.   *               the configuration information for the specified DMA module.
  1004.   * @retval None
  1005.   */
  1006. static void SMARTCARD_DMATransmitCplt(DMA_HandleTypeDef *hdma)
  1007. {
  1008.   SMARTCARD_HandleTypeDef* hsc = ( SMARTCARD_HandleTypeDef* )((DMA_HandleTypeDef* )hdma)->Parent;
  1009.  
  1010.   hsc->TxXferCount = 0;
  1011.  
  1012.   /* Disable the DMA transfer for transmit request by setting the DMAT bit
  1013.      in the SMARTCARD CR3 register */
  1014.   CLEAR_BIT(hsc->Instance->CR3, USART_CR3_DMAT);
  1015.  
  1016.   /* Enable the SMARTCARD Transmit Complete Interrupt */    
  1017.   __HAL_SMARTCARD_ENABLE_IT(hsc, SMARTCARD_IT_TC);
  1018. }
  1019.  
  1020. /**
  1021.   * @brief  DMA SMARTCARD receive process complete callback.
  1022.   * @param  hdma: Pointer to a DMA_HandleTypeDef structure that contains
  1023.   *               the configuration information for the specified DMA module.
  1024.   * @retval None
  1025.   */
  1026. static void SMARTCARD_DMAReceiveCplt(DMA_HandleTypeDef *hdma)  
  1027. {
  1028.   SMARTCARD_HandleTypeDef* hsc = ( SMARTCARD_HandleTypeDef* )((DMA_HandleTypeDef* )hdma)->Parent;
  1029.  
  1030.   hsc->RxXferCount = 0;
  1031.  
  1032.   /* Disable the DMA transfer for the receiver request by setting the DMAR bit
  1033.      in the USART CR3 register */
  1034.   CLEAR_BIT(hsc->Instance->CR3, USART_CR3_DMAR);
  1035.  
  1036.   /* Check if a non-blocking transmit process is ongoing or not */
  1037.   if(hsc->State == HAL_SMARTCARD_STATE_BUSY_TX_RX)
  1038.   {
  1039.     hsc->State = HAL_SMARTCARD_STATE_BUSY_TX;
  1040.   }
  1041.   else
  1042.   {
  1043.     hsc->State = HAL_SMARTCARD_STATE_READY;
  1044.   }
  1045.  
  1046.   HAL_SMARTCARD_RxCpltCallback(hsc);
  1047. }
  1048.  
  1049. /**
  1050.   * @brief  DMA SMARTCARD communication error callback.
  1051.   * @param  hdma: Pointer to a DMA_HandleTypeDef structure that contains
  1052.   *               the configuration information for the specified DMA module.
  1053.   * @retval None
  1054.   */
  1055. static void SMARTCARD_DMAError(DMA_HandleTypeDef *hdma)  
  1056. {
  1057.   SMARTCARD_HandleTypeDef* hsc = ( SMARTCARD_HandleTypeDef* )((DMA_HandleTypeDef* )hdma)->Parent;
  1058.  
  1059.   hsc->RxXferCount = 0;
  1060.   hsc->TxXferCount = 0;
  1061.   hsc->ErrorCode = HAL_SMARTCARD_ERROR_DMA;
  1062.   hsc->State= HAL_SMARTCARD_STATE_READY;
  1063.  
  1064.   HAL_SMARTCARD_ErrorCallback(hsc);
  1065. }
  1066.  
  1067. /**
  1068.   * @brief  This function handles SMARTCARD Communication Timeout.
  1069.   * @param  hsc: Pointer to a SMARTCARD_HandleTypeDef structure that contains
  1070.   *                the configuration information for the specified SMARTCARD module.
  1071.   * @param  Flag: specifies the SMARTCARD flag to check.
  1072.   * @param  Status: The new Flag status (SET or RESET).
  1073.   * @param  Timeout: Timeout duration
  1074.   * @retval HAL status
  1075.   */
  1076. static HAL_StatusTypeDef SMARTCARD_WaitOnFlagUntilTimeout(SMARTCARD_HandleTypeDef *hsc, uint32_t Flag, FlagStatus Status, uint32_t Timeout)
  1077. {
  1078.   uint32_t tickstart = 0;
  1079.  
  1080.   /* Get tick */
  1081.   tickstart = HAL_GetTick();
  1082.  
  1083.   /* Wait until flag is set */
  1084.   if(Status == RESET)
  1085.   {
  1086.     while(__HAL_SMARTCARD_GET_FLAG(hsc, Flag) == RESET)
  1087.     {
  1088.       /* Check for the Timeout */
  1089.       if(Timeout != HAL_MAX_DELAY)
  1090.       {
  1091.         if((Timeout == 0)||((HAL_GetTick() - tickstart ) > Timeout))
  1092.         {
  1093.           /* Disable TXE and RXNE interrupts for the interrupt process */
  1094.           __HAL_SMARTCARD_DISABLE_IT(hsc, SMARTCARD_IT_TXE);
  1095.           __HAL_SMARTCARD_DISABLE_IT(hsc, SMARTCARD_IT_RXNE);
  1096.  
  1097.           hsc->State= HAL_SMARTCARD_STATE_READY;
  1098.  
  1099.           /* Process Unlocked */
  1100.           __HAL_UNLOCK(hsc);
  1101.  
  1102.           return HAL_TIMEOUT;
  1103.         }
  1104.       }
  1105.     }
  1106.   }
  1107.   else
  1108.   {
  1109.     while(__HAL_SMARTCARD_GET_FLAG(hsc, Flag) != RESET)
  1110.     {
  1111.       /* Check for the Timeout */
  1112.       if(Timeout != HAL_MAX_DELAY)
  1113.       {
  1114.         if((Timeout == 0)||((HAL_GetTick() - tickstart ) > Timeout))
  1115.         {
  1116.           /* Disable TXE and RXNE interrupts for the interrupt process */
  1117.           __HAL_SMARTCARD_DISABLE_IT(hsc, SMARTCARD_IT_TXE);
  1118.           __HAL_SMARTCARD_DISABLE_IT(hsc, SMARTCARD_IT_RXNE);
  1119.  
  1120.           hsc->State= HAL_SMARTCARD_STATE_READY;
  1121.  
  1122.           /* Process Unlocked */
  1123.           __HAL_UNLOCK(hsc);
  1124.        
  1125.           return HAL_TIMEOUT;
  1126.         }
  1127.       }
  1128.     }
  1129.   }
  1130.   return HAL_OK;
  1131. }
  1132.  
  1133. /**
  1134.   * @brief  Send an amount of data in non-blocking mode.
  1135.   * @param  hsc: Pointer to a SMARTCARD_HandleTypeDef structure that contains
  1136.   *                the configuration information for the specified SMARTCARD module.
  1137.   *         Function called under interruption only, once
  1138.   *         interruptions have been enabled by HAL_SMARTCARD_Transmit_IT()      
  1139.   * @retval HAL status
  1140.   */
  1141. static HAL_StatusTypeDef SMARTCARD_Transmit_IT(SMARTCARD_HandleTypeDef *hsc)
  1142. {
  1143.   uint32_t tmp_state = 0;
  1144.  
  1145.   tmp_state = hsc->State;
  1146.   if((tmp_state == HAL_SMARTCARD_STATE_BUSY_TX) || (tmp_state == HAL_SMARTCARD_STATE_BUSY_TX_RX))
  1147.   {
  1148.     WRITE_REG(hsc->Instance->DR, (*hsc->pTxBuffPtr++ & (uint8_t)0xFF));
  1149.    
  1150.     if(--hsc->TxXferCount == 0)
  1151.     {
  1152.       /* Disable the SMARTCARD Transmit Data Register Empty Interrupt */
  1153.       __HAL_SMARTCARD_DISABLE_IT(hsc, SMARTCARD_IT_TXE);
  1154.  
  1155.       /* Enable the SMARTCARD Transmit Complete Interrupt */    
  1156.       __HAL_SMARTCARD_ENABLE_IT(hsc, SMARTCARD_IT_TC);
  1157.     }
  1158.    
  1159.     return HAL_OK;
  1160.   }
  1161.   else
  1162.   {
  1163.     return HAL_BUSY;
  1164.   }
  1165. }
  1166.  
  1167.  
  1168. /**
  1169.   * @brief  Wraps up transmission in non blocking mode.
  1170.   * @param  hsmartcard: pointer to a SMARTCARD_HandleTypeDef structure that contains
  1171.   *                the configuration information for the specified SMARTCARD module.
  1172.   * @retval HAL status
  1173.   */
  1174. static HAL_StatusTypeDef SMARTCARD_EndTransmit_IT(SMARTCARD_HandleTypeDef *hsmartcard)
  1175. {
  1176.   /* Disable the SMARTCARD Transmit Complete Interrupt */    
  1177.   __HAL_SMARTCARD_DISABLE_IT(hsmartcard, SMARTCARD_IT_TC);
  1178.  
  1179.   /* Check if a receive process is ongoing or not */
  1180.   if(hsmartcard->State == HAL_SMARTCARD_STATE_BUSY_TX_RX)
  1181.   {
  1182.     hsmartcard->State = HAL_SMARTCARD_STATE_BUSY_RX;
  1183.   }
  1184.   else
  1185.   {
  1186.     /* Disable the SMARTCARD Error Interrupt: (Frame error, noise error, overrun error) */
  1187.     __HAL_SMARTCARD_DISABLE_IT(hsmartcard, SMARTCARD_IT_ERR);
  1188.    
  1189.     hsmartcard->State = HAL_SMARTCARD_STATE_READY;
  1190.   }
  1191.  
  1192.   HAL_SMARTCARD_TxCpltCallback(hsmartcard);
  1193.  
  1194.   return HAL_OK;
  1195. }
  1196.  
  1197.  
  1198. /**
  1199.   * @brief  Receive an amount of data in non-blocking mode.
  1200.   * @param  hsc: Pointer to a SMARTCARD_HandleTypeDef structure that contains
  1201.   *                the configuration information for the specified SMARTCARD module.
  1202.   * @retval HAL status
  1203.   */
  1204. static HAL_StatusTypeDef SMARTCARD_Receive_IT(SMARTCARD_HandleTypeDef *hsc)
  1205. {
  1206.   uint32_t tmp_state = 0;
  1207.  
  1208.   tmp_state = hsc->State;
  1209.   if((tmp_state == HAL_SMARTCARD_STATE_BUSY_RX) || (tmp_state == HAL_SMARTCARD_STATE_BUSY_TX_RX))
  1210.   {
  1211.     *hsc->pRxBuffPtr++ = (uint8_t)(hsc->Instance->DR & (uint8_t)0xFF);
  1212.    
  1213.     if(--hsc->RxXferCount == 0)
  1214.     {
  1215.       __HAL_SMARTCARD_DISABLE_IT(hsc, SMARTCARD_IT_RXNE);
  1216.      
  1217.       /* Disable the SMARTCARD Parity Error Interrupt */
  1218.       __HAL_SMARTCARD_DISABLE_IT(hsc, SMARTCARD_IT_PE);
  1219.  
  1220.       /* Disable the SMARTCARD Error Interrupt: (Frame error, noise error, overrun error) */
  1221.       __HAL_SMARTCARD_DISABLE_IT(hsc, SMARTCARD_IT_ERR);
  1222.  
  1223.       /* Check if a non-blocking transmit process is ongoing or not */
  1224.       if(hsc->State == HAL_SMARTCARD_STATE_BUSY_TX_RX)
  1225.       {
  1226.         hsc->State = HAL_SMARTCARD_STATE_BUSY_TX;
  1227.       }
  1228.       else
  1229.       {
  1230.         hsc->State = HAL_SMARTCARD_STATE_READY;
  1231.       }
  1232.  
  1233.       HAL_SMARTCARD_RxCpltCallback(hsc);
  1234.  
  1235.       return HAL_OK;
  1236.     }
  1237.     return HAL_OK;
  1238.   }
  1239.   else
  1240.   {
  1241.     return HAL_BUSY;
  1242.   }
  1243. }
  1244.  
  1245. /**
  1246.   * @brief  Configures the SMARTCARD peripheral.
  1247.   * @param  hsc: Pointer to a SMARTCARD_HandleTypeDef structure that contains
  1248.   *                the configuration information for the specified SMARTCARD module.
  1249.   * @retval None
  1250.   */
  1251. static void SMARTCARD_SetConfig(SMARTCARD_HandleTypeDef *hsc)
  1252. {
  1253.   /* Check the parameters */
  1254.   assert_param(IS_SMARTCARD_POLARITY(hsc->Init.CLKPolarity));
  1255.   assert_param(IS_SMARTCARD_PHASE(hsc->Init.CLKPhase));
  1256.   assert_param(IS_SMARTCARD_LASTBIT(hsc->Init.CLKLastBit));
  1257.   assert_param(IS_SMARTCARD_BAUDRATE(hsc->Init.BaudRate));  
  1258.   assert_param(IS_SMARTCARD_WORD_LENGTH(hsc->Init.WordLength));
  1259.   assert_param(IS_SMARTCARD_STOPBITS(hsc->Init.StopBits));
  1260.   assert_param(IS_SMARTCARD_PARITY(hsc->Init.Parity));
  1261.   assert_param(IS_SMARTCARD_MODE(hsc->Init.Mode));
  1262.   assert_param(IS_SMARTCARD_NACK_STATE(hsc->Init.NACKState));
  1263.  
  1264.   /* The LBCL, CPOL and CPHA bits have to be selected when both the transmitter and the
  1265.      receiver are disabled (TE=RE=0) to ensure that the clock pulses function correctly. */
  1266.   CLEAR_BIT(hsc->Instance->CR1, (uint32_t)(USART_CR1_TE | USART_CR1_RE));
  1267.  
  1268.   /*------ SMARTCARD-associated USART registers setting : CR2 Configuration ------*/
  1269.   /* Clear CLKEN, CPOL, CPHA and LBCL bits */
  1270.   /* Configure the SMARTCARD Clock, CPOL, CPHA and LastBit -----------------------*/
  1271.   /* Set CPOL bit according to hsc->Init.CLKPolarity value */
  1272.   /* Set CPHA bit according to hsc->Init.CLKPhase value */
  1273.   /* Set LBCL bit according to hsc->Init.CLKLastBit value */
  1274.   MODIFY_REG(hsc->Instance->CR2,
  1275.              ((uint32_t)(USART_CR2_CPHA | USART_CR2_CPOL | USART_CR2_CLKEN | USART_CR2_LBCL)),
  1276.              ((uint32_t)(USART_CR2_CLKEN | hsc->Init.CLKPolarity | hsc->Init.CLKPhase| hsc->Init.CLKLastBit)) );
  1277.  
  1278.   /* Set Stop Bits: Set STOP[13:12] bits according to hsc->Init.StopBits value */
  1279.   MODIFY_REG(hsc->Instance->CR2, USART_CR2_STOP,(uint32_t)(hsc->Init.StopBits));
  1280.  
  1281.   /*------ SMARTCARD-associated USART registers setting : CR1 Configuration ------*/
  1282.   /* Clear M, PCE, PS, TE and RE bits */
  1283.   /* Configure the SMARTCARD Word Length, Parity and mode:
  1284.      Set the M according to hsc->Init.WordLength value (forced to 1 as 9B data frame should be selected)
  1285.      Set PCE and PS bits according to hsc->Init.Parity value (PCE bit forced to 1 as parity control should always be enabled)
  1286.      Set TE and RE bits according to hsc->Init.Mode value */
  1287.   MODIFY_REG(hsc->Instance->CR1,
  1288.              ((uint32_t)(USART_CR1_M | USART_CR1_PCE | USART_CR1_PS | USART_CR1_TE | USART_CR1_RE)),
  1289.              ((uint32_t)(USART_CR1_M | USART_CR1_PCE | hsc->Init.Parity | hsc->Init.Mode)) );
  1290.  
  1291.   /*------ SMARTCARD-associated USART registers setting : CR3 Configuration ------*/
  1292.   /* Clear CTSE and RTSE bits */
  1293.   CLEAR_BIT(hsc->Instance->CR3, (USART_CR3_RTSE | USART_CR3_CTSE));
  1294.  
  1295.   /*------ SMARTCARD-associated USART registers setting : BRR Configuration ------*/
  1296.   if(hsc->Instance == USART1)
  1297.   {
  1298.     hsc->Instance->BRR = SMARTCARD_BRR(HAL_RCC_GetPCLK2Freq(), hsc->Init.BaudRate);
  1299.   }
  1300.   else
  1301.   {
  1302.     hsc->Instance->BRR = SMARTCARD_BRR(HAL_RCC_GetPCLK1Freq(), hsc->Init.BaudRate);
  1303.   }
  1304. }
  1305.  
  1306. /**
  1307.   * @}
  1308.   */
  1309.  
  1310. #endif /* HAL_SMARTCARD_MODULE_ENABLED */
  1311. /**
  1312.   * @}
  1313.   */
  1314.  
  1315. /**
  1316.   * @}
  1317.   */
  1318.  
  1319. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
  1320.