Subversion Repositories LedShow

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.   * @brief   SMARTCARD HAL module driver.
  6.   *          This file provides firmware functions to manage the following
  7.   *          functionalities of the SMARTCARD peripheral:
  8.   *           + Initialization and de-initialization functions
  9.   *           + IO operation functions
  10.   *           + Peripheral Control functions
  11.   *           + Peripheral State and Error functions
  12.   *
  13.   @verbatim
  14.   ==============================================================================
  15.                      ##### How to use this driver #####
  16.   ==============================================================================
  17.     [..]
  18.       The SMARTCARD HAL driver can be used as follows:
  19.  
  20.     (#) Declare a SMARTCARD_HandleTypeDef handle structure.
  21.     (#) Initialize the SMARTCARD low level resources by implementing the HAL_SMARTCARD_MspInit() API:
  22.         (##) Enable the interface clock of the USARTx associated to the SMARTCARD.
  23.         (##) SMARTCARD pins configuration:
  24.             (+++) Enable the clock for the SMARTCARD GPIOs.
  25.             (+++) Configure SMARTCARD pins as alternate function pull-up.
  26.         (##) NVIC configuration if you need to use interrupt process (HAL_SMARTCARD_Transmit_IT()
  27.              and HAL_SMARTCARD_Receive_IT() APIs):
  28.             (+++) Configure the USARTx interrupt priority.
  29.             (+++) Enable the NVIC USART IRQ handle.
  30.         (##) DMA Configuration if you need to use DMA process (HAL_SMARTCARD_Transmit_DMA()
  31.              and HAL_SMARTCARD_Receive_DMA() APIs):
  32.             (+++) Declare a DMA handle structure for the Tx/Rx channel.
  33.             (+++) Enable the DMAx interface clock.
  34.             (+++) Configure the declared DMA handle structure with the required Tx/Rx parameters.
  35.             (+++) Configure the DMA Tx/Rx channel.
  36.             (+++) Associate the initialized DMA handle to the SMARTCARD DMA Tx/Rx handle.
  37.             (+++) Configure the priority and enable the NVIC for the transfer complete interrupt on the DMA Tx/Rx channel.
  38.             (+++) Configure the USARTx interrupt priority and enable the NVIC USART IRQ handle
  39.                   (used for last byte sending completion detection in DMA non circular mode)
  40.  
  41.     (#) Program the Baud Rate, Word Length , Stop Bit, Parity, Hardware
  42.         flow control and Mode(Receiver/Transmitter) in the SMARTCARD Init structure.
  43.  
  44.     (#) Initialize the SMARTCARD registers by calling the HAL_SMARTCARD_Init() API:
  45.         (++) These APIs configure also the low level Hardware GPIO, CLOCK, CORTEX...etc)
  46.              by calling the customized HAL_SMARTCARD_MspInit() API.
  47.     [..]
  48.     (@) The specific SMARTCARD interrupts (Transmission complete interrupt,
  49.         RXNE interrupt and Error Interrupts) will be managed using the macros
  50.         __HAL_SMARTCARD_ENABLE_IT() and __HAL_SMARTCARD_DISABLE_IT() inside the transmit and receive process.
  51.  
  52.     [..]
  53.     Three operation modes are available within this driver :
  54.  
  55.     *** Polling mode IO operation ***
  56.     =================================
  57.     [..]
  58.       (+) Send an amount of data in blocking mode using HAL_SMARTCARD_Transmit()
  59.       (+) Receive an amount of data in blocking mode using HAL_SMARTCARD_Receive()
  60.  
  61.     *** Interrupt mode IO operation ***
  62.     ===================================
  63.     [..]
  64.       (+) Send an amount of data in non blocking mode using HAL_SMARTCARD_Transmit_IT()
  65.       (+) At transmission end of transfer HAL_SMARTCARD_TxCpltCallback is executed and user can
  66.           add his own code by customization of function pointer HAL_SMARTCARD_TxCpltCallback
  67.       (+) Receive an amount of data in non blocking mode using HAL_SMARTCARD_Receive_IT()
  68.       (+) At reception end of transfer HAL_SMARTCARD_RxCpltCallback is executed and user can
  69.           add his own code by customization of function pointer HAL_SMARTCARD_RxCpltCallback
  70.       (+) In case of transfer Error, HAL_SMARTCARD_ErrorCallback() function is executed and user can
  71.           add his own code by customization of function pointer HAL_SMARTCARD_ErrorCallback
  72.  
  73.     *** DMA mode IO operation ***
  74.     ==============================
  75.     [..]
  76.       (+) Send an amount of data in non blocking mode (DMA) using HAL_SMARTCARD_Transmit_DMA()
  77.       (+) At transmission end of transfer HAL_SMARTCARD_TxCpltCallback is executed and user can
  78.           add his own code by customization of function pointer HAL_SMARTCARD_TxCpltCallback
  79.       (+) Receive an amount of data in non blocking mode (DMA) using HAL_SMARTCARD_Receive_DMA()
  80.       (+) At reception end of transfer HAL_SMARTCARD_RxCpltCallback is executed and user can
  81.           add his own code by customization of function pointer HAL_SMARTCARD_RxCpltCallback
  82.       (+) In case of transfer Error, HAL_SMARTCARD_ErrorCallback() function is executed and user can
  83.           add his own code by customization of function pointer HAL_SMARTCARD_ErrorCallback
  84.  
  85.     *** SMARTCARD HAL driver macros list ***
  86.     ========================================
  87.     [..]
  88.       Below the list of most used macros in SMARTCARD HAL driver.
  89.  
  90.       (+) __HAL_SMARTCARD_ENABLE: Enable the SMARTCARD peripheral
  91.       (+) __HAL_SMARTCARD_DISABLE: Disable the SMARTCARD peripheral
  92.       (+) __HAL_SMARTCARD_GET_FLAG : Check whether the specified SMARTCARD flag is set or not
  93.       (+) __HAL_SMARTCARD_CLEAR_FLAG : Clear the specified SMARTCARD pending flag
  94.       (+) __HAL_SMARTCARD_ENABLE_IT: Enable the specified SMARTCARD interrupt
  95.       (+) __HAL_SMARTCARD_DISABLE_IT: Disable the specified SMARTCARD interrupt
  96.  
  97.     [..]
  98.       (@) You can refer to the SMARTCARD HAL driver header file for more useful macros
  99.  
  100.     ##### Callback registration #####
  101.     ==================================
  102.  
  103.     [..]
  104.     The compilation define USE_HAL_SMARTCARD_REGISTER_CALLBACKS when set to 1
  105.     allows the user to configure dynamically the driver callbacks.
  106.  
  107.     [..]
  108.     Use Function @ref HAL_SMARTCARD_RegisterCallback() to register a user callback.
  109.     Function @ref HAL_SMARTCARD_RegisterCallback() allows to register following callbacks:
  110.     (+) TxCpltCallback            : Tx Complete Callback.
  111.     (+) RxCpltCallback            : Rx Complete Callback.
  112.     (+) ErrorCallback             : Error Callback.
  113.     (+) AbortCpltCallback         : Abort Complete Callback.
  114.     (+) AbortTransmitCpltCallback : Abort Transmit Complete Callback.
  115.     (+) AbortReceiveCpltCallback  : Abort Receive Complete Callback.
  116.     (+) MspInitCallback           : SMARTCARD MspInit.
  117.     (+) MspDeInitCallback         : SMARTCARD MspDeInit.
  118.     This function takes as parameters the HAL peripheral handle, the Callback ID
  119.     and a pointer to the user callback function.
  120.  
  121.     [..]
  122.     Use function @ref HAL_SMARTCARD_UnRegisterCallback() to reset a callback to the default
  123.     weak (surcharged) function.
  124.     @ref HAL_SMARTCARD_UnRegisterCallback() takes as parameters the HAL peripheral handle,
  125.     and the Callback ID.
  126.     This function allows to reset following callbacks:
  127.     (+) TxCpltCallback            : Tx Complete Callback.
  128.     (+) RxCpltCallback            : Rx Complete Callback.
  129.     (+) ErrorCallback             : Error Callback.
  130.     (+) AbortCpltCallback         : Abort Complete Callback.
  131.     (+) AbortTransmitCpltCallback : Abort Transmit Complete Callback.
  132.     (+) AbortReceiveCpltCallback  : Abort Receive Complete Callback.
  133.     (+) MspInitCallback           : SMARTCARD MspInit.
  134.     (+) MspDeInitCallback         : SMARTCARD MspDeInit.
  135.  
  136.     [..]
  137.     By default, after the @ref HAL_SMARTCARD_Init() and when the state is HAL_SMARTCARD_STATE_RESET
  138.     all callbacks are set to the corresponding weak (surcharged) functions:
  139.     examples @ref HAL_SMARTCARD_TxCpltCallback(), @ref HAL_SMARTCARD_RxCpltCallback().
  140.     Exception done for MspInit and MspDeInit functions that are respectively
  141.     reset to the legacy weak (surcharged) functions in the @ref HAL_SMARTCARD_Init()
  142.     and @ref HAL_SMARTCARD_DeInit() only when these callbacks are null (not registered beforehand).
  143.     If not, MspInit or MspDeInit are not null, the @ref HAL_SMARTCARD_Init() and @ref HAL_SMARTCARD_DeInit()
  144.     keep and use the user MspInit/MspDeInit callbacks (registered beforehand).
  145.  
  146.     [..]
  147.     Callbacks can be registered/unregistered in HAL_SMARTCARD_STATE_READY state only.
  148.     Exception done MspInit/MspDeInit that can be registered/unregistered
  149.     in HAL_SMARTCARD_STATE_READY or HAL_SMARTCARD_STATE_RESET state, thus registered (user)
  150.     MspInit/DeInit callbacks can be used during the Init/DeInit.
  151.     In that case first register the MspInit/MspDeInit user callbacks
  152.     using @ref HAL_SMARTCARD_RegisterCallback() before calling @ref HAL_SMARTCARD_DeInit()
  153.     or @ref HAL_SMARTCARD_Init() function.
  154.  
  155.     [..]
  156.     When The compilation define USE_HAL_SMARTCARD_REGISTER_CALLBACKS is set to 0 or
  157.     not defined, the callback registration feature is not available
  158.     and weak (surcharged) callbacks are used.
  159.  
  160.   @endverbatim
  161.   ******************************************************************************
  162.   * @attention
  163.   *
  164.   * <h2><center>&copy; Copyright (c) 2016 STMicroelectronics.
  165.   * All rights reserved.</center></h2>
  166.   *
  167.   * This software component is licensed by ST under BSD 3-Clause license,
  168.   * the "License"; You may not use this file except in compliance with the
  169.   * License. You may obtain a copy of the License at:
  170.   *                        opensource.org/licenses/BSD-3-Clause
  171.   *
  172.   ******************************************************************************
  173.   */
  174.  
  175. /* Includes ------------------------------------------------------------------*/
  176. #include "stm32f1xx_hal.h"
  177.  
  178. /** @addtogroup STM32F1xx_HAL_Driver
  179.   * @{
  180.   */
  181.  
  182. /** @defgroup SMARTCARD SMARTCARD
  183.   * @brief HAL SMARTCARD module driver
  184.   * @{
  185.   */
  186. #ifdef HAL_SMARTCARD_MODULE_ENABLED
  187. /* Private typedef -----------------------------------------------------------*/
  188. /* Private define ------------------------------------------------------------*/
  189. /** @addtogroup SMARTCARD_Private_Constants
  190.   * @{
  191.   */
  192. /**
  193.   * @}
  194.   */
  195.  
  196. /* Private macro -------------------------------------------------------------*/
  197. /* Private variables ---------------------------------------------------------*/
  198. /* Private function prototypes -----------------------------------------------*/
  199. /** @addtogroup SMARTCARD_Private_Functions
  200.   * @{
  201.   */
  202. #if (USE_HAL_SMARTCARD_REGISTER_CALLBACKS == 1)
  203. void SMARTCARD_InitCallbacksToDefault(SMARTCARD_HandleTypeDef *hsc);
  204. #endif /* USE_HAL_SMARTCARD_REGISTER_CALLBACKS */
  205. static void SMARTCARD_EndTxTransfer(SMARTCARD_HandleTypeDef *hsc);
  206. static void SMARTCARD_EndRxTransfer(SMARTCARD_HandleTypeDef *hsc);
  207. static void SMARTCARD_SetConfig (SMARTCARD_HandleTypeDef *hsc);
  208. static HAL_StatusTypeDef SMARTCARD_Transmit_IT(SMARTCARD_HandleTypeDef *hsc);
  209. static HAL_StatusTypeDef SMARTCARD_EndTransmit_IT(SMARTCARD_HandleTypeDef *hsc);
  210. static HAL_StatusTypeDef SMARTCARD_Receive_IT(SMARTCARD_HandleTypeDef *hsc);
  211. static void SMARTCARD_DMATransmitCplt(DMA_HandleTypeDef *hdma);
  212. static void SMARTCARD_DMAReceiveCplt(DMA_HandleTypeDef *hdma);
  213. static void SMARTCARD_DMAError(DMA_HandleTypeDef *hdma);
  214. static void SMARTCARD_DMAAbortOnError(DMA_HandleTypeDef *hdma);
  215. static void SMARTCARD_DMATxAbortCallback(DMA_HandleTypeDef *hdma);
  216. static void SMARTCARD_DMARxAbortCallback(DMA_HandleTypeDef *hdma);
  217. static void SMARTCARD_DMATxOnlyAbortCallback(DMA_HandleTypeDef *hdma);
  218. static void SMARTCARD_DMARxOnlyAbortCallback(DMA_HandleTypeDef *hdma);
  219. static HAL_StatusTypeDef SMARTCARD_WaitOnFlagUntilTimeout(SMARTCARD_HandleTypeDef *hsc, uint32_t Flag, FlagStatus Status, uint32_t Tickstart, uint32_t Timeout);
  220. /**
  221.   * @}
  222.   */
  223.  
  224. /* Exported functions --------------------------------------------------------*/
  225. /** @defgroup SMARTCARD_Exported_Functions SMARTCARD Exported Functions
  226.   * @{
  227.   */
  228.  
  229. /** @defgroup SMARTCARD_Exported_Functions_Group1 SmartCard Initialization and de-initialization functions
  230.   *  @brief    Initialization and Configuration functions
  231.   *
  232. @verbatim
  233.   ==============================================================================
  234.               ##### Initialization and Configuration functions #####
  235.   ==============================================================================
  236.   [..]
  237.   This subsection provides a set of functions allowing to initialize the USART
  238.   in Smartcard mode.
  239.   [..]
  240.   The Smartcard interface is designed to support asynchronous protocol Smartcards as
  241.   defined in the ISO 7816-3 standard.
  242.   [..]
  243.   The USART can provide a clock to the smartcard through the SCLK output.
  244.   In smartcard mode, SCLK is not associated to the communication but is simply derived
  245.   from the internal peripheral input clock through a 5-bit prescaler.
  246.   [..]
  247.   (+) For the Smartcard mode only these parameters can be configured:
  248.       (++) Baud Rate
  249.       (++) Word Length => Should be 9 bits (8 bits + parity)
  250.       (++) Stop Bit
  251.       (++) Parity: => Should be enabled
  252.       (++) USART polarity
  253.       (++) USART phase
  254.       (++) USART LastBit
  255.       (++) Receiver/transmitter modes
  256.       (++) Prescaler
  257.       (++) GuardTime
  258.       (++) NACKState: The Smartcard NACK state
  259.  
  260.      (+) Recommended SmartCard interface configuration to get the Answer to Reset from the Card:
  261.         (++) Word Length = 9 Bits
  262.         (++) 1.5 Stop Bit
  263.         (++) Even parity
  264.         (++) BaudRate = 12096 baud
  265.         (++) Tx and Rx enabled
  266.   [..]
  267.   Please refer to the ISO 7816-3 specification for more details.
  268.  
  269.   [..]
  270.    (@) It is also possible to choose 0.5 stop bit for receiving but it is recommended
  271.        to use 1.5 stop bits for both transmitting and receiving to avoid switching
  272.        between the two configurations.
  273.   [..]
  274.     The HAL_SMARTCARD_Init() function follows the USART  SmartCard configuration
  275.     procedures (details for the procedures are available in reference manuals
  276.     (RM0008 for STM32F10Xxx MCUs and RM0041 for STM32F100xx MCUs)).
  277.  
  278. @endverbatim
  279.  
  280.   The SMARTCARD frame format is given in the following table:
  281.        +-------------------------------------------------------------+
  282.        |   M bit |  PCE bit  |        SMARTCARD frame                |
  283.        |---------------------|---------------------------------------|
  284.        |    1    |    1      |    | SB | 8 bit data | PB | STB |     |
  285.        +-------------------------------------------------------------+
  286.   * @{
  287.   */
  288.  
  289. /**
  290.   * @brief  Initializes the SmartCard mode according to the specified
  291.   *         parameters in the SMARTCARD_InitTypeDef and create the associated handle.
  292.   * @param  hsc    Pointer to a SMARTCARD_HandleTypeDef structure that contains
  293.   *                the configuration information for SMARTCARD module.
  294.   * @retval HAL status
  295.   */
  296. HAL_StatusTypeDef HAL_SMARTCARD_Init(SMARTCARD_HandleTypeDef *hsc)
  297. {
  298.   /* Check the SMARTCARD handle allocation */
  299.   if(hsc == NULL)
  300.   {
  301.     return HAL_ERROR;
  302.   }
  303.  
  304.   /* Check the parameters */
  305.   assert_param(IS_SMARTCARD_INSTANCE(hsc->Instance));
  306.   assert_param(IS_SMARTCARD_NACK_STATE(hsc->Init.NACKState));
  307.  
  308.   if(hsc->gState == HAL_SMARTCARD_STATE_RESET)
  309.   {
  310.     /* Allocate lock resource and initialize it */
  311.     hsc->Lock = HAL_UNLOCKED;
  312.  
  313. #if USE_HAL_SMARTCARD_REGISTER_CALLBACKS == 1
  314.     SMARTCARD_InitCallbacksToDefault(hsc);
  315.  
  316.     if (hsc->MspInitCallback == NULL)
  317.     {
  318.       hsc->MspInitCallback = HAL_SMARTCARD_MspInit;
  319.     }
  320.  
  321.     /* Init the low level hardware */
  322.     hsc->MspInitCallback(hsc);
  323. #else
  324.     /* Init the low level hardware : GPIO, CLOCK */
  325.     HAL_SMARTCARD_MspInit(hsc);
  326. #endif /* USE_HAL_SMARTCARD_REGISTER_CALLBACKS */
  327.   }
  328.  
  329.   hsc->gState = HAL_SMARTCARD_STATE_BUSY;
  330.  
  331.   /* Set the Prescaler */
  332.   MODIFY_REG(hsc->Instance->GTPR, USART_GTPR_PSC, hsc->Init.Prescaler);
  333.  
  334.   /* Set the Guard Time */
  335.   MODIFY_REG(hsc->Instance->GTPR, USART_GTPR_GT, ((hsc->Init.GuardTime)<<8U));
  336.  
  337.   /* Set the Smartcard Communication parameters */
  338.   SMARTCARD_SetConfig(hsc);
  339.  
  340.   /* In SmartCard mode, the following bits must be kept cleared:
  341.   - LINEN bit in the USART_CR2 register
  342.   - HDSEL and IREN bits in the USART_CR3 register.*/
  343.   CLEAR_BIT(hsc->Instance->CR2, USART_CR2_LINEN);
  344.   CLEAR_BIT(hsc->Instance->CR3, (USART_CR3_IREN | USART_CR3_HDSEL));
  345.  
  346.   /* Enable the SMARTCARD Parity Error Interrupt */
  347.   SET_BIT(hsc->Instance->CR1, USART_CR1_PEIE);
  348.  
  349.   /* Enable the SMARTCARD Framing Error Interrupt */
  350.   SET_BIT(hsc->Instance->CR3, USART_CR3_EIE);
  351.  
  352.   /* Enable the Peripheral */
  353.   __HAL_SMARTCARD_ENABLE(hsc);
  354.  
  355.   /* Configure the Smartcard NACK state */
  356.   MODIFY_REG(hsc->Instance->CR3, USART_CR3_NACK, hsc->Init.NACKState);
  357.  
  358.   /* Enable the SC mode by setting the SCEN bit in the CR3 register */
  359.   hsc->Instance->CR3 |= (USART_CR3_SCEN);
  360.  
  361.   /* Initialize the SMARTCARD state*/
  362.   hsc->ErrorCode = HAL_SMARTCARD_ERROR_NONE;
  363.   hsc->gState= HAL_SMARTCARD_STATE_READY;
  364.   hsc->RxState= HAL_SMARTCARD_STATE_READY;
  365.  
  366.   return HAL_OK;
  367. }
  368.  
  369. /**
  370.   * @brief DeInitializes the USART SmartCard peripheral
  371.   * @param  hsc    Pointer to a SMARTCARD_HandleTypeDef structure that contains
  372.   *                the configuration information for SMARTCARD module.
  373.   * @retval HAL status
  374.   */
  375. HAL_StatusTypeDef HAL_SMARTCARD_DeInit(SMARTCARD_HandleTypeDef *hsc)
  376. {
  377.   /* Check the SMARTCARD handle allocation */
  378.   if(hsc == NULL)
  379.   {
  380.     return HAL_ERROR;
  381.   }
  382.  
  383.   /* Check the parameters */
  384.   assert_param(IS_SMARTCARD_INSTANCE(hsc->Instance));
  385.  
  386.   hsc->gState = HAL_SMARTCARD_STATE_BUSY;
  387.  
  388.   /* Disable the Peripheral */
  389.   __HAL_SMARTCARD_DISABLE(hsc);
  390.  
  391.   /* DeInit the low level hardware */
  392. #if USE_HAL_SMARTCARD_REGISTER_CALLBACKS == 1
  393.   if (hsc->MspDeInitCallback == NULL)
  394.   {
  395.     hsc->MspDeInitCallback = HAL_SMARTCARD_MspDeInit;
  396.   }
  397.   /* DeInit the low level hardware */
  398.   hsc->MspDeInitCallback(hsc);
  399. #else
  400.   HAL_SMARTCARD_MspDeInit(hsc);
  401. #endif /* USE_HAL_SMARTCARD_REGISTER_CALLBACKS */
  402.  
  403.   hsc->ErrorCode = HAL_SMARTCARD_ERROR_NONE;
  404.   hsc->gState = HAL_SMARTCARD_STATE_RESET;
  405.   hsc->RxState = HAL_SMARTCARD_STATE_RESET;
  406.  
  407.   /* Release Lock */
  408.   __HAL_UNLOCK(hsc);
  409.  
  410.   return HAL_OK;
  411. }
  412.  
  413. /**
  414.   * @brief  SMARTCARD MSP Init
  415.   * @param  hsc    Pointer to a SMARTCARD_HandleTypeDef structure that contains
  416.   *                the configuration information for SMARTCARD module.
  417.   * @retval None
  418.   */
  419. __weak void HAL_SMARTCARD_MspInit(SMARTCARD_HandleTypeDef *hsc)
  420. {
  421.   /* Prevent unused argument(s) compilation warning */
  422.   UNUSED(hsc);
  423.  
  424.   /* NOTE : This function should not be modified, when the callback is needed,
  425.             the HAL_SMARTCARD_MspInit can be implemented in the user file
  426.    */
  427. }
  428.  
  429. /**
  430.   * @brief SMARTCARD MSP DeInit
  431.   * @param  hsc    Pointer to a SMARTCARD_HandleTypeDef structure that contains
  432.   *                the configuration information for SMARTCARD module.
  433.   * @retval None
  434.   */
  435. __weak void HAL_SMARTCARD_MspDeInit(SMARTCARD_HandleTypeDef *hsc)
  436. {
  437.   /* Prevent unused argument(s) compilation warning */
  438.   UNUSED(hsc);
  439.  
  440.   /* NOTE : This function should not be modified, when the callback is needed,
  441.             the HAL_SMARTCARD_MspDeInit can be implemented in the user file
  442.    */
  443. }
  444.  
  445. #if (USE_HAL_SMARTCARD_REGISTER_CALLBACKS == 1)
  446. /**
  447.   * @brief  Register a User SMARTCARD Callback
  448.   *         To be used instead of the weak predefined callback
  449.   * @param  hsc smartcard handle
  450.   * @param  CallbackID ID of the callback to be registered
  451.   *         This parameter can be one of the following values:
  452.   *           @arg @ref HAL_SMARTCARD_TX_COMPLETE_CB_ID Tx Complete Callback ID
  453.   *           @arg @ref HAL_SMARTCARD_RX_COMPLETE_CB_ID Rx Complete Callback ID
  454.   *           @arg @ref HAL_SMARTCARD_ERROR_CB_ID Error Callback ID
  455.   *           @arg @ref HAL_SMARTCARD_ABORT_COMPLETE_CB_ID Abort Complete Callback ID
  456.   *           @arg @ref HAL_SMARTCARD_ABORT_TRANSMIT_COMPLETE_CB_ID Abort Transmit Complete Callback ID
  457.   *           @arg @ref HAL_SMARTCARD_ABORT_RECEIVE_COMPLETE_CB_ID Abort Receive Complete Callback ID
  458.   *           @arg @ref HAL_SMARTCARD_MSPINIT_CB_ID MspInit Callback ID
  459.   *           @arg @ref HAL_SMARTCARD_MSPDEINIT_CB_ID MspDeInit Callback ID
  460.   * @param  pCallback pointer to the Callback function
  461.   * @retval HAL status
  462.   */
  463. HAL_StatusTypeDef HAL_SMARTCARD_RegisterCallback(SMARTCARD_HandleTypeDef *hsc, HAL_SMARTCARD_CallbackIDTypeDef CallbackID, pSMARTCARD_CallbackTypeDef pCallback)
  464. {
  465.   HAL_StatusTypeDef status = HAL_OK;
  466.  
  467.   if (pCallback == NULL)
  468.   {
  469.     /* Update the error code */
  470.     hsc->ErrorCode |= HAL_SMARTCARD_ERROR_INVALID_CALLBACK;
  471.  
  472.     return HAL_ERROR;
  473.   }
  474.   /* Process locked */
  475.   __HAL_LOCK(hsc);
  476.  
  477.   if (hsc->gState == HAL_SMARTCARD_STATE_READY)
  478.   {
  479.     switch (CallbackID)
  480.     {
  481.  
  482.       case HAL_SMARTCARD_TX_COMPLETE_CB_ID :
  483.         hsc->TxCpltCallback = pCallback;
  484.         break;
  485.  
  486.       case HAL_SMARTCARD_RX_COMPLETE_CB_ID :
  487.         hsc->RxCpltCallback = pCallback;
  488.         break;
  489.  
  490.       case HAL_SMARTCARD_ERROR_CB_ID :
  491.         hsc->ErrorCallback = pCallback;
  492.         break;
  493.  
  494.       case HAL_SMARTCARD_ABORT_COMPLETE_CB_ID :
  495.         hsc->AbortCpltCallback = pCallback;
  496.         break;
  497.  
  498.       case HAL_SMARTCARD_ABORT_TRANSMIT_COMPLETE_CB_ID :
  499.         hsc->AbortTransmitCpltCallback = pCallback;
  500.         break;
  501.  
  502.       case HAL_SMARTCARD_ABORT_RECEIVE_COMPLETE_CB_ID :
  503.         hsc->AbortReceiveCpltCallback = pCallback;
  504.         break;
  505.  
  506.  
  507.       case HAL_SMARTCARD_MSPINIT_CB_ID :
  508.         hsc->MspInitCallback = pCallback;
  509.         break;
  510.  
  511.       case HAL_SMARTCARD_MSPDEINIT_CB_ID :
  512.         hsc->MspDeInitCallback = pCallback;
  513.         break;
  514.  
  515.       default :
  516.         /* Update the error code */
  517.         hsc->ErrorCode |= HAL_SMARTCARD_ERROR_INVALID_CALLBACK;
  518.  
  519.         /* Return error status */
  520.         status =  HAL_ERROR;
  521.         break;
  522.     }
  523.   }
  524.   else if (hsc->gState == HAL_SMARTCARD_STATE_RESET)
  525.   {
  526.     switch (CallbackID)
  527.     {
  528.       case HAL_SMARTCARD_MSPINIT_CB_ID :
  529.         hsc->MspInitCallback = pCallback;
  530.         break;
  531.  
  532.       case HAL_SMARTCARD_MSPDEINIT_CB_ID :
  533.         hsc->MspDeInitCallback = pCallback;
  534.         break;
  535.  
  536.       default :
  537.         /* Update the error code */
  538.         hsc->ErrorCode |= HAL_SMARTCARD_ERROR_INVALID_CALLBACK;
  539.  
  540.         /* Return error status */
  541.         status =  HAL_ERROR;
  542.         break;
  543.     }
  544.   }
  545.   else
  546.   {
  547.     /* Update the error code */
  548.     hsc->ErrorCode |= HAL_SMARTCARD_ERROR_INVALID_CALLBACK;
  549.  
  550.     /* Return error status */
  551.     status =  HAL_ERROR;
  552.   }
  553.  
  554.   /* Release Lock */
  555.   __HAL_UNLOCK(hsc);
  556.  
  557.   return status;
  558. }
  559.  
  560. /**
  561.   * @brief  Unregister an SMARTCARD callback
  562.   *         SMARTCARD callback is redirected to the weak predefined callback
  563.   * @param  hsc smartcard handle
  564.   * @param  CallbackID ID of the callback to be unregistered
  565.   *         This parameter can be one of the following values:
  566.   *           @arg @ref HAL_SMARTCARD_TX_COMPLETE_CB_ID Tx Complete Callback ID
  567.   *           @arg @ref HAL_SMARTCARD_RX_COMPLETE_CB_ID Rx Complete Callback ID
  568.   *           @arg @ref HAL_SMARTCARD_ERROR_CB_ID Error Callback ID
  569.   *           @arg @ref HAL_SMARTCARD_ABORT_COMPLETE_CB_ID Abort Complete Callback ID
  570.   *           @arg @ref HAL_SMARTCARD_ABORT_TRANSMIT_COMPLETE_CB_ID Abort Transmit Complete Callback ID
  571.   *           @arg @ref HAL_SMARTCARD_ABORT_RECEIVE_COMPLETE_CB_ID Abort Receive Complete Callback ID
  572.   *           @arg @ref HAL_SMARTCARD_MSPINIT_CB_ID MspInit Callback ID
  573.   *           @arg @ref HAL_SMARTCARD_MSPDEINIT_CB_ID MspDeInit Callback ID
  574.   * @retval HAL status
  575.   */
  576. HAL_StatusTypeDef HAL_SMARTCARD_UnRegisterCallback(SMARTCARD_HandleTypeDef *hsc, HAL_SMARTCARD_CallbackIDTypeDef CallbackID)
  577. {
  578.   HAL_StatusTypeDef status = HAL_OK;
  579.  
  580.   /* Process locked */
  581.   __HAL_LOCK(hsc);
  582.  
  583.   if (HAL_SMARTCARD_STATE_READY == hsc->gState)
  584.   {
  585.     switch (CallbackID)
  586.     {
  587.       case HAL_SMARTCARD_TX_COMPLETE_CB_ID :
  588.         hsc->TxCpltCallback = HAL_SMARTCARD_TxCpltCallback;                       /* Legacy weak TxCpltCallback            */
  589.         break;
  590.  
  591.       case HAL_SMARTCARD_RX_COMPLETE_CB_ID :
  592.         hsc->RxCpltCallback = HAL_SMARTCARD_RxCpltCallback;                       /* Legacy weak RxCpltCallback            */
  593.         break;
  594.  
  595.       case HAL_SMARTCARD_ERROR_CB_ID :
  596.         hsc->ErrorCallback = HAL_SMARTCARD_ErrorCallback;                         /* Legacy weak ErrorCallback             */
  597.         break;
  598.  
  599.       case HAL_SMARTCARD_ABORT_COMPLETE_CB_ID :
  600.         hsc->AbortCpltCallback = HAL_SMARTCARD_AbortCpltCallback;                 /* Legacy weak AbortCpltCallback         */
  601.         break;
  602.  
  603.       case HAL_SMARTCARD_ABORT_TRANSMIT_COMPLETE_CB_ID :
  604.         hsc->AbortTransmitCpltCallback = HAL_SMARTCARD_AbortTransmitCpltCallback; /* Legacy weak AbortTransmitCpltCallback */
  605.         break;
  606.  
  607.       case HAL_SMARTCARD_ABORT_RECEIVE_COMPLETE_CB_ID :
  608.         hsc->AbortReceiveCpltCallback = HAL_SMARTCARD_AbortReceiveCpltCallback;   /* Legacy weak AbortReceiveCpltCallback  */
  609.         break;
  610.  
  611.  
  612.       case HAL_SMARTCARD_MSPINIT_CB_ID :
  613.         hsc->MspInitCallback = HAL_SMARTCARD_MspInit;                             /* Legacy weak MspInitCallback           */
  614.         break;
  615.  
  616.       case HAL_SMARTCARD_MSPDEINIT_CB_ID :
  617.         hsc->MspDeInitCallback = HAL_SMARTCARD_MspDeInit;                         /* Legacy weak MspDeInitCallback         */
  618.         break;
  619.  
  620.       default :
  621.         /* Update the error code */
  622.         hsc->ErrorCode |= HAL_SMARTCARD_ERROR_INVALID_CALLBACK;
  623.  
  624.         /* Return error status */
  625.         status =  HAL_ERROR;
  626.         break;
  627.     }
  628.   }
  629.   else if (HAL_SMARTCARD_STATE_RESET == hsc->gState)
  630.   {
  631.     switch (CallbackID)
  632.     {
  633.       case HAL_SMARTCARD_MSPINIT_CB_ID :
  634.         hsc->MspInitCallback = HAL_SMARTCARD_MspInit;
  635.         break;
  636.  
  637.       case HAL_SMARTCARD_MSPDEINIT_CB_ID :
  638.         hsc->MspDeInitCallback = HAL_SMARTCARD_MspDeInit;
  639.         break;
  640.  
  641.       default :
  642.         /* Update the error code */
  643.         hsc->ErrorCode |= HAL_SMARTCARD_ERROR_INVALID_CALLBACK;
  644.  
  645.         /* Return error status */
  646.         status =  HAL_ERROR;
  647.         break;
  648.     }
  649.   }
  650.   else
  651.   {
  652.     /* Update the error code */
  653.     hsc->ErrorCode |= HAL_SMARTCARD_ERROR_INVALID_CALLBACK;
  654.  
  655.     /* Return error status */
  656.     status =  HAL_ERROR;
  657.   }
  658.  
  659.   /* Release Lock */
  660.   __HAL_UNLOCK(hsc);
  661.  
  662.   return status;
  663. }
  664. #endif /* USE_HAL_SMARTCARD_REGISTER_CALLBACKS */
  665.  
  666. /**
  667.   * @}
  668.   */
  669.  
  670. /** @defgroup SMARTCARD_Exported_Functions_Group2 IO operation functions
  671.   * @brief    SMARTCARD Transmit and Receive functions
  672.   *
  673. @verbatim
  674.  ===============================================================================
  675.                       ##### IO operation functions #####
  676.  ===============================================================================
  677.  [..]
  678.    This subsection provides a set of functions allowing to manage the SMARTCARD data transfers.
  679.  
  680.  [..]
  681.     (#) Smartcard is a single wire half duplex communication protocol.
  682.     The Smartcard interface is designed to support asynchronous protocol Smartcards as
  683.     defined in the ISO 7816-3 standard.
  684.     (#) The USART should be configured as:
  685.        (++) 8 bits plus parity: where M=1 and PCE=1 in the USART_CR1 register
  686.        (++) 1.5 stop bits when transmitting and receiving: where STOP=11 in the USART_CR2 register.
  687.  
  688.     (#) There are two modes of transfer:
  689.        (++) Blocking mode: The communication is performed in polling mode.
  690.             The HAL status of all data processing is returned by the same function
  691.             after finishing transfer.
  692.        (++) Non Blocking mode: The communication is performed using Interrupts
  693.            or DMA, These APIs return the HAL status.
  694.            The end of the data processing will be indicated through the
  695.            dedicated SMARTCARD IRQ when using Interrupt mode or the DMA IRQ when
  696.            using DMA mode.
  697.            The HAL_SMARTCARD_TxCpltCallback(), HAL_SMARTCARD_RxCpltCallback() user callbacks
  698.            will be executed respectively at the end of the Transmit or Receive process
  699.            The HAL_SMARTCARD_ErrorCallback() user callback will be executed when a communication error is detected
  700.  
  701.     (#) Blocking mode APIs are :
  702.         (++) HAL_SMARTCARD_Transmit()
  703.         (++) HAL_SMARTCARD_Receive()
  704.  
  705.     (#) Non Blocking mode APIs with Interrupt are :
  706.         (++) HAL_SMARTCARD_Transmit_IT()
  707.         (++) HAL_SMARTCARD_Receive_IT()
  708.         (++) HAL_SMARTCARD_IRQHandler()
  709.  
  710.     (#) Non Blocking mode functions with DMA are :
  711.         (++) HAL_SMARTCARD_Transmit_DMA()
  712.         (++) HAL_SMARTCARD_Receive_DMA()
  713.  
  714.     (#) A set of Transfer Complete Callbacks are provided in non Blocking mode:
  715.         (++) HAL_SMARTCARD_TxCpltCallback()
  716.         (++) HAL_SMARTCARD_RxCpltCallback()
  717.         (++) HAL_SMARTCARD_ErrorCallback()
  718.  
  719.     (#) Non-Blocking mode transfers could be aborted using Abort API's :
  720.         (+) HAL_SMARTCARD_Abort()
  721.         (+) HAL_SMARTCARD_AbortTransmit()
  722.         (+) HAL_SMARTCARD_AbortReceive()
  723.         (+) HAL_SMARTCARD_Abort_IT()
  724.         (+) HAL_SMARTCARD_AbortTransmit_IT()
  725.         (+) HAL_SMARTCARD_AbortReceive_IT()
  726.  
  727.     (#) For Abort services based on interrupts (HAL_SMARTCARD_Abortxxx_IT), a set of Abort Complete Callbacks are provided:
  728.         (+) HAL_SMARTCARD_AbortCpltCallback()
  729.         (+) HAL_SMARTCARD_AbortTransmitCpltCallback()
  730.         (+) HAL_SMARTCARD_AbortReceiveCpltCallback()
  731.  
  732.     (#) In Non-Blocking mode transfers, possible errors are split into 2 categories.
  733.         Errors are handled as follows :
  734.        (+) Error is considered as Recoverable and non blocking : Transfer could go till end, but error severity is
  735.            to be evaluated by user : this concerns Frame Error, Parity Error or Noise Error in Interrupt mode reception .
  736.            Received character is then retrieved and stored in Rx buffer, Error code is set to allow user to identify error type,
  737.            and HAL_SMARTCARD_ErrorCallback() user callback is executed. Transfer is kept ongoing on SMARTCARD side.
  738.            If user wants to abort it, Abort services should be called by user.
  739.        (+) Error is considered as Blocking : Transfer could not be completed properly and is aborted.
  740.            This concerns Frame Error in Interrupt mode tranmission, Overrun Error in Interrupt mode reception and all errors in DMA mode.
  741.            Error code is set to allow user to identify error type, and HAL_SMARTCARD_ErrorCallback() user callback is executed.
  742.  
  743. @endverbatim
  744.   * @{
  745.   */
  746.  
  747. /**
  748.   * @brief Send an amount of data in blocking mode
  749.   * @param  hsc    Pointer to a SMARTCARD_HandleTypeDef structure that contains
  750.   *                the configuration information for SMARTCARD module.
  751.   * @param  pData  Pointer to data buffer
  752.   * @param  Size   Amount of data to be sent
  753.   * @param  Timeout Timeout duration
  754.   * @retval HAL status
  755.   */
  756. HAL_StatusTypeDef HAL_SMARTCARD_Transmit(SMARTCARD_HandleTypeDef *hsc, uint8_t *pData, uint16_t Size, uint32_t Timeout)
  757. {
  758.   uint8_t *tmp = pData;
  759.   uint32_t tickstart = 0U;
  760.  
  761.   if(hsc->gState == HAL_SMARTCARD_STATE_READY)
  762.   {
  763.     if((pData == NULL) || (Size == 0U))
  764.     {
  765.       return  HAL_ERROR;
  766.     }
  767.  
  768.     /* Process Locked */
  769.     __HAL_LOCK(hsc);
  770.  
  771.     hsc->ErrorCode = HAL_SMARTCARD_ERROR_NONE;
  772.     hsc->gState = HAL_SMARTCARD_STATE_BUSY_TX;
  773.  
  774.     /* Init tickstart for timeout managment */
  775.     tickstart = HAL_GetTick();
  776.  
  777.     hsc->TxXferSize = Size;
  778.     hsc->TxXferCount = Size;
  779.     while(hsc->TxXferCount > 0U)
  780.     {
  781.       hsc->TxXferCount--;
  782.       if(SMARTCARD_WaitOnFlagUntilTimeout(hsc, SMARTCARD_FLAG_TXE, RESET, tickstart, Timeout) != HAL_OK)
  783.       {
  784.         return HAL_TIMEOUT;
  785.       }
  786.       hsc->Instance->DR = (uint8_t)(*tmp & 0xFFU);
  787.       tmp++;
  788.     }
  789.  
  790.     if(SMARTCARD_WaitOnFlagUntilTimeout(hsc, SMARTCARD_FLAG_TC, RESET, tickstart, Timeout) != HAL_OK)
  791.     {
  792.       return HAL_TIMEOUT;
  793.     }
  794.  
  795.         /* At end of Tx process, restore hsc->gState to Ready */
  796.     hsc->gState = HAL_SMARTCARD_STATE_READY;
  797.  
  798.     /* Process Unlocked */
  799.     __HAL_UNLOCK(hsc);
  800.  
  801.     return HAL_OK;
  802.   }
  803.   else
  804.   {
  805.     return HAL_BUSY;
  806.   }
  807. }
  808.  
  809. /**
  810.   * @brief Receive an amount of data in blocking mode
  811.   * @param  hsc    Pointer to a SMARTCARD_HandleTypeDef structure that contains
  812.   *                the configuration information for SMARTCARD module.
  813.   * @param  pData  Pointer to data buffer
  814.   * @param  Size   Amount of data to be received
  815.   * @param  Timeout Timeout duration
  816.   * @retval HAL status
  817.   */
  818. HAL_StatusTypeDef HAL_SMARTCARD_Receive(SMARTCARD_HandleTypeDef *hsc, uint8_t *pData, uint16_t Size, uint32_t Timeout)
  819. {
  820.   uint8_t  *tmp = pData;
  821.   uint32_t tickstart = 0U;
  822.  
  823.   if(hsc->RxState == HAL_SMARTCARD_STATE_READY)
  824.   {
  825.     if((pData == NULL) || (Size == 0U))
  826.     {
  827.       return  HAL_ERROR;
  828.     }
  829.  
  830.     /* Process Locked */
  831.     __HAL_LOCK(hsc);
  832.  
  833.     hsc->ErrorCode = HAL_SMARTCARD_ERROR_NONE;
  834.     hsc->RxState = HAL_SMARTCARD_STATE_BUSY_RX;
  835.  
  836.     /* Init tickstart for timeout managment */
  837.     tickstart = HAL_GetTick();
  838.  
  839.     hsc->RxXferSize = Size;
  840.     hsc->RxXferCount = Size;
  841.  
  842.     /* Check the remain data to be received */
  843.     while(hsc->RxXferCount > 0U)
  844.     {
  845.       hsc->RxXferCount--;
  846.       if(SMARTCARD_WaitOnFlagUntilTimeout(hsc, SMARTCARD_FLAG_RXNE, RESET, tickstart, Timeout) != HAL_OK)
  847.       {
  848.         return HAL_TIMEOUT;
  849.       }
  850.       *tmp = (uint8_t)(hsc->Instance->DR & (uint8_t)0xFFU);
  851.       tmp++;
  852.     }
  853.  
  854.     /* At end of Rx process, restore hsc->RxState to Ready */
  855.     hsc->RxState = HAL_SMARTCARD_STATE_READY;
  856.  
  857.     /* Process Unlocked */
  858.     __HAL_UNLOCK(hsc);
  859.  
  860.     return HAL_OK;
  861.   }
  862.   else
  863.   {
  864.     return HAL_BUSY;
  865.   }
  866. }
  867.  
  868. /**
  869.   * @brief Send an amount of data in non blocking mode
  870.   * @param  hsc    Pointer to a SMARTCARD_HandleTypeDef structure that contains
  871.   *                the configuration information for SMARTCARD module.
  872.   * @param  pData  Pointer to data buffer
  873.   * @param  Size   Amount of data to be sent
  874.   * @retval HAL status
  875.   */
  876. HAL_StatusTypeDef HAL_SMARTCARD_Transmit_IT(SMARTCARD_HandleTypeDef *hsc, uint8_t *pData, uint16_t Size)
  877. {
  878.   /* Check that a Tx process is not already ongoing */
  879.   if(hsc->gState == HAL_SMARTCARD_STATE_READY)
  880.   {
  881.     if((pData == NULL) || (Size == 0U))
  882.     {
  883.       return HAL_ERROR;
  884.     }
  885.  
  886.     /* Process Locked */
  887.     __HAL_LOCK(hsc);
  888.  
  889.     hsc->pTxBuffPtr = pData;
  890.     hsc->TxXferSize = Size;
  891.     hsc->TxXferCount = Size;
  892.  
  893.     hsc->ErrorCode = HAL_SMARTCARD_ERROR_NONE;
  894.     hsc->gState = HAL_SMARTCARD_STATE_BUSY_TX;
  895.  
  896.     /* Process Unlocked */
  897.     __HAL_UNLOCK(hsc);
  898.  
  899.     /* Enable the SMARTCARD Parity Error Interrupt */
  900.     SET_BIT(hsc->Instance->CR1, USART_CR1_PEIE);
  901.  
  902.     /* Disable the SMARTCARD Error Interrupt: (Frame error, noise error, overrun error) */
  903.     CLEAR_BIT(hsc->Instance->CR3, USART_CR3_EIE);
  904.  
  905.     /* Enable the SMARTCARD Transmit data register empty Interrupt */
  906.     SET_BIT(hsc->Instance->CR1, USART_CR1_TXEIE);
  907.  
  908.     return HAL_OK;
  909.   }
  910.   else
  911.   {
  912.     return HAL_BUSY;
  913.   }
  914. }
  915.  
  916. /**
  917.   * @brief Receive an amount of data in non blocking mode
  918.   * @param  hsc    Pointer to a SMARTCARD_HandleTypeDef structure that contains
  919.   *                the configuration information for SMARTCARD module.
  920.   * @param  pData  Pointer to data buffer
  921.   * @param  Size   Amount of data to be received
  922.   * @retval HAL status
  923.   */
  924. HAL_StatusTypeDef HAL_SMARTCARD_Receive_IT(SMARTCARD_HandleTypeDef *hsc, uint8_t *pData, uint16_t Size)
  925. {
  926.   /* Check that a Rx process is not already ongoing */
  927.   if(hsc->RxState == HAL_SMARTCARD_STATE_READY)
  928.   {
  929.     if((pData == NULL) || (Size == 0U))
  930.     {
  931.       return HAL_ERROR;
  932.     }
  933.  
  934.     /* Process Locked */
  935.     __HAL_LOCK(hsc);
  936.  
  937.     hsc->pRxBuffPtr = pData;
  938.     hsc->RxXferSize = Size;
  939.     hsc->RxXferCount = Size;
  940.  
  941.     hsc->ErrorCode = HAL_SMARTCARD_ERROR_NONE;
  942.     hsc->RxState = HAL_SMARTCARD_STATE_BUSY_RX;
  943.  
  944.     /* Process Unlocked */
  945.     __HAL_UNLOCK(hsc);
  946.  
  947.     /* Enable the SMARTCARD Parity Error and Data Register not empty Interrupts */
  948.     SET_BIT(hsc->Instance->CR1, USART_CR1_PEIE| USART_CR1_RXNEIE);
  949.  
  950.     /* Enable the SMARTCARD Error Interrupt: (Frame error, noise error, overrun error) */
  951.     SET_BIT(hsc->Instance->CR3, USART_CR3_EIE);
  952.  
  953.     return HAL_OK;
  954.   }
  955.   else
  956.   {
  957.     return HAL_BUSY;
  958.   }
  959. }
  960.  
  961. /**
  962.   * @brief Send an amount of data in non blocking mode
  963.   * @param  hsc    Pointer to a SMARTCARD_HandleTypeDef structure that contains
  964.   *                the configuration information for SMARTCARD module.
  965.   * @param  pData  Pointer to data buffer
  966.   * @param  Size   Amount of data to be sent
  967.   * @retval HAL status
  968.   */
  969. HAL_StatusTypeDef HAL_SMARTCARD_Transmit_DMA(SMARTCARD_HandleTypeDef *hsc, uint8_t *pData, uint16_t Size)
  970. {
  971.   uint32_t *tmp;
  972.  
  973.   /* Check that a Tx process is not already ongoing */
  974.   if(hsc->gState == HAL_SMARTCARD_STATE_READY)
  975.   {
  976.     if((pData == NULL) || (Size == 0U))
  977.     {
  978.       return HAL_ERROR;
  979.     }
  980.  
  981.     /* Process Locked */
  982.     __HAL_LOCK(hsc);
  983.  
  984.     hsc->pTxBuffPtr = pData;
  985.     hsc->TxXferSize = Size;
  986.     hsc->TxXferCount = Size;
  987.  
  988.     hsc->ErrorCode = HAL_SMARTCARD_ERROR_NONE;
  989.     hsc->gState = HAL_SMARTCARD_STATE_BUSY_TX;
  990.  
  991.     /* Set the SMARTCARD DMA transfer complete callback */
  992.     hsc->hdmatx->XferCpltCallback = SMARTCARD_DMATransmitCplt;
  993.  
  994.     /* Set the DMA error callback */
  995.     hsc->hdmatx->XferErrorCallback = SMARTCARD_DMAError;
  996.  
  997.     /* Set the DMA abort callback */
  998.     hsc->hdmatx->XferAbortCallback = NULL;
  999.  
  1000.     /* Enable the SMARTCARD transmit DMA channel */
  1001.     tmp = (uint32_t*)&pData;
  1002.     HAL_DMA_Start_IT(hsc->hdmatx, *(uint32_t*)tmp, (uint32_t)&hsc->Instance->DR, Size);
  1003.  
  1004.      /* Clear the TC flag in the SR register by writing 0 to it */
  1005.     __HAL_SMARTCARD_CLEAR_FLAG(hsc, SMARTCARD_FLAG_TC);
  1006.  
  1007.     /* Process Unlocked */
  1008.     __HAL_UNLOCK(hsc);
  1009.  
  1010.     /* Enable the DMA transfer for transmit request by setting the DMAT bit
  1011.     in the SMARTCARD CR3 register */
  1012.     SET_BIT(hsc->Instance->CR3, USART_CR3_DMAT);
  1013.  
  1014.     return HAL_OK;
  1015.   }
  1016.   else
  1017.   {
  1018.     return HAL_BUSY;
  1019.   }
  1020. }
  1021.  
  1022. /**
  1023.   * @brief Receive an amount of data in non blocking mode
  1024.   * @param  hsc    Pointer to a SMARTCARD_HandleTypeDef structure that contains
  1025.   *                the configuration information for SMARTCARD module.
  1026.   * @param  pData  Pointer to data buffer
  1027.   * @param  Size   Amount of data to be received
  1028.   * @note   When the SMARTCARD parity is enabled (PCE = 1) the data received contain the parity bit.s
  1029.   * @retval HAL status
  1030.   */
  1031. HAL_StatusTypeDef HAL_SMARTCARD_Receive_DMA(SMARTCARD_HandleTypeDef *hsc, uint8_t *pData, uint16_t Size)
  1032. {
  1033.   uint32_t *tmp;
  1034.  
  1035.   /* Check that a Rx process is not already ongoing */
  1036.   if(hsc->RxState == HAL_SMARTCARD_STATE_READY)
  1037.   {
  1038.     if((pData == NULL) || (Size == 0U))
  1039.     {
  1040.       return HAL_ERROR;
  1041.     }
  1042.  
  1043.     /* Process Locked */
  1044.     __HAL_LOCK(hsc);
  1045.  
  1046.     hsc->pRxBuffPtr = pData;
  1047.     hsc->RxXferSize = Size;
  1048.  
  1049.     hsc->ErrorCode = HAL_SMARTCARD_ERROR_NONE;
  1050.     hsc->RxState = HAL_SMARTCARD_STATE_BUSY_RX;
  1051.  
  1052.     /* Set the SMARTCARD DMA transfer complete callback */
  1053.     hsc->hdmarx->XferCpltCallback = SMARTCARD_DMAReceiveCplt;
  1054.  
  1055.     /* Set the DMA error callback */
  1056.     hsc->hdmarx->XferErrorCallback = SMARTCARD_DMAError;
  1057.  
  1058.     /* Set the DMA abort callback */
  1059.     hsc->hdmatx->XferAbortCallback = NULL;
  1060.  
  1061.     /* Enable the DMA channel */
  1062.     tmp = (uint32_t*)&pData;
  1063.     HAL_DMA_Start_IT(hsc->hdmarx, (uint32_t)&hsc->Instance->DR, *(uint32_t*)tmp, Size);
  1064.  
  1065.     /* Clear the Overrun flag just before enabling the DMA Rx request: can be mandatory for the second transfer */
  1066.     __HAL_SMARTCARD_CLEAR_OREFLAG(hsc);
  1067.  
  1068.     /* Process Unlocked */
  1069.     __HAL_UNLOCK(hsc);
  1070.  
  1071.     /* Enable the SMARTCARD Parity Error Interrupt */
  1072.     SET_BIT(hsc->Instance->CR1, USART_CR1_PEIE);
  1073.  
  1074.     /* Enable the SMARTCARD Error Interrupt: (Frame error, noise error, overrun error) */
  1075.     SET_BIT(hsc->Instance->CR3, USART_CR3_EIE);
  1076.  
  1077.     /* Enable the DMA transfer for the receiver request by setting the DMAR bit
  1078.     in the SMARTCARD CR3 register */
  1079.     SET_BIT(hsc->Instance->CR3, USART_CR3_DMAR);
  1080.  
  1081.     return HAL_OK;
  1082.   }
  1083.   else
  1084.   {
  1085.     return HAL_BUSY;
  1086.   }
  1087. }
  1088.  
  1089. /**
  1090.   * @brief  Abort ongoing transfers (blocking mode).
  1091.   * @param  hsc SMARTCARD handle.
  1092.   * @note   This procedure could be used for aborting any ongoing transfer started in Interrupt or DMA mode.
  1093.   *         This procedure performs following operations :
  1094.   *           - Disable PPP Interrupts
  1095.   *           - Disable the DMA transfer in the peripheral register (if enabled)
  1096.   *           - Abort DMA transfer by calling HAL_DMA_Abort (in case of transfer in DMA mode)
  1097.   *           - Set handle State to READY
  1098.   * @note   This procedure is executed in blocking mode : when exiting function, Abort is considered as completed.
  1099.   * @retval HAL status
  1100. */
  1101. HAL_StatusTypeDef HAL_SMARTCARD_Abort(SMARTCARD_HandleTypeDef *hsc)
  1102. {
  1103.   /* Disable TXEIE, TCIE, RXNE, PE and ERR (Frame error, noise error, overrun error) interrupts */
  1104.   CLEAR_BIT(hsc->Instance->CR1, (USART_CR1_RXNEIE | USART_CR1_PEIE | USART_CR1_TXEIE | USART_CR1_TCIE));
  1105.   CLEAR_BIT(hsc->Instance->CR3, USART_CR3_EIE);
  1106.  
  1107.   /* Disable the SMARTCARD DMA Tx request if enabled */
  1108.   if(HAL_IS_BIT_SET(hsc->Instance->CR3, USART_CR3_DMAT))
  1109.   {
  1110.     CLEAR_BIT(hsc->Instance->CR3, USART_CR3_DMAT);
  1111.  
  1112.     /* Abort the SMARTCARD DMA Tx channel : use blocking DMA Abort API (no callback) */
  1113.     if(hsc->hdmatx != NULL)
  1114.     {
  1115.       /* Set the SMARTCARD DMA Abort callback to Null.
  1116.          No call back execution at end of DMA abort procedure */
  1117.       hsc->hdmatx->XferAbortCallback = NULL;
  1118.  
  1119.       HAL_DMA_Abort(hsc->hdmatx);
  1120.     }
  1121.   }
  1122.  
  1123.   /* Disable the SMARTCARD DMA Rx request if enabled */
  1124.   if(HAL_IS_BIT_SET(hsc->Instance->CR3, USART_CR3_DMAR))
  1125.   {
  1126.     CLEAR_BIT(hsc->Instance->CR3, USART_CR3_DMAR);
  1127.  
  1128.     /* Abort the SMARTCARD DMA Rx channel : use blocking DMA Abort API (no callback) */
  1129.     if(hsc->hdmarx != NULL)
  1130.     {
  1131.       /* Set the SMARTCARD DMA Abort callback to Null.
  1132.          No call back execution at end of DMA abort procedure */
  1133.       hsc->hdmarx->XferAbortCallback = NULL;
  1134.  
  1135.       HAL_DMA_Abort(hsc->hdmarx);
  1136.     }
  1137.   }
  1138.  
  1139.   /* Reset Tx and Rx transfer counters */
  1140.   hsc->TxXferCount = 0x00U;
  1141.   hsc->RxXferCount = 0x00U;
  1142.  
  1143.   /* Reset ErrorCode */
  1144.   hsc->ErrorCode = HAL_SMARTCARD_ERROR_NONE;
  1145.  
  1146.   /* Restore hsc->RxState and hsc->gState to Ready */
  1147.   hsc->RxState = HAL_SMARTCARD_STATE_READY;
  1148.   hsc->gState = HAL_SMARTCARD_STATE_READY;
  1149.  
  1150.   return HAL_OK;
  1151. }
  1152.  
  1153. /**
  1154.   * @brief  Abort ongoing Transmit transfer (blocking mode).
  1155.   * @param  hsc SMARTCARD handle.
  1156.   * @note   This procedure could be used for aborting any ongoing transfer started in Interrupt or DMA mode.
  1157.   *         This procedure performs following operations :
  1158.   *           - Disable SMARTCARD Interrupts (Tx)
  1159.   *           - Disable the DMA transfer in the peripheral register (if enabled)
  1160.   *           - Abort DMA transfer by calling HAL_DMA_Abort (in case of transfer in DMA mode)
  1161.   *           - Set handle State to READY
  1162.   * @note   This procedure is executed in blocking mode : when exiting function, Abort is considered as completed.
  1163.   * @retval HAL status
  1164. */
  1165. HAL_StatusTypeDef HAL_SMARTCARD_AbortTransmit(SMARTCARD_HandleTypeDef *hsc)
  1166. {
  1167.   /* Disable TXEIE and TCIE interrupts */
  1168.   CLEAR_BIT(hsc->Instance->CR1, (USART_CR1_TXEIE | USART_CR1_TCIE));
  1169.  
  1170.   /* Disable the SMARTCARD DMA Tx request if enabled */
  1171.   if(HAL_IS_BIT_SET(hsc->Instance->CR3, USART_CR3_DMAT))
  1172.   {
  1173.     CLEAR_BIT(hsc->Instance->CR3, USART_CR3_DMAT);
  1174.  
  1175.     /* Abort the SMARTCARD DMA Tx channel : use blocking DMA Abort API (no callback) */
  1176.     if(hsc->hdmatx != NULL)
  1177.     {
  1178.       /* Set the SMARTCARD DMA Abort callback to Null.
  1179.          No call back execution at end of DMA abort procedure */
  1180.       hsc->hdmatx->XferAbortCallback = NULL;
  1181.  
  1182.       HAL_DMA_Abort(hsc->hdmatx);
  1183.     }
  1184.   }
  1185.  
  1186.   /* Reset Tx transfer counter */
  1187.   hsc->TxXferCount = 0x00U;
  1188.  
  1189.   /* Restore hsc->gState to Ready */
  1190.   hsc->gState = HAL_SMARTCARD_STATE_READY;
  1191.  
  1192.   return HAL_OK;
  1193. }
  1194.  
  1195. /**
  1196.   * @brief  Abort ongoing Receive transfer (blocking mode).
  1197.   * @param  hsc SMARTCARD handle.
  1198.   * @note   This procedure could be used for aborting any ongoing transfer started in Interrupt or DMA mode.
  1199.   *         This procedure performs following operations :
  1200.   *           - Disable PPP Interrupts
  1201.   *           - Disable the DMA transfer in the peripheral register (if enabled)
  1202.   *           - Abort DMA transfer by calling HAL_DMA_Abort (in case of transfer in DMA mode)
  1203.   *           - Set handle State to READY
  1204.   * @note   This procedure is executed in blocking mode : when exiting function, Abort is considered as completed.
  1205.   * @retval HAL status
  1206. */
  1207. HAL_StatusTypeDef HAL_SMARTCARD_AbortReceive(SMARTCARD_HandleTypeDef *hsc)
  1208. {
  1209.   /* Disable RXNE, PE and ERR (Frame error, noise error, overrun error) interrupts */
  1210.   CLEAR_BIT(hsc->Instance->CR1, (USART_CR1_RXNEIE | USART_CR1_PEIE));
  1211.   CLEAR_BIT(hsc->Instance->CR3, USART_CR3_EIE);
  1212.  
  1213.   /* Disable the SMARTCARD DMA Rx request if enabled */
  1214.   if(HAL_IS_BIT_SET(hsc->Instance->CR3, USART_CR3_DMAR))
  1215.   {
  1216.     CLEAR_BIT(hsc->Instance->CR3, USART_CR3_DMAR);
  1217.  
  1218.     /* Abort the SMARTCARD DMA Rx channel : use blocking DMA Abort API (no callback) */
  1219.     if(hsc->hdmarx != NULL)
  1220.     {
  1221.       /* Set the SMARTCARD DMA Abort callback to Null.
  1222.          No call back execution at end of DMA abort procedure */
  1223.       hsc->hdmarx->XferAbortCallback = NULL;
  1224.  
  1225.       HAL_DMA_Abort(hsc->hdmarx);
  1226.     }
  1227.   }
  1228.  
  1229.   /* Reset Rx transfer counter */
  1230.   hsc->RxXferCount = 0x00U;
  1231.  
  1232.   /* Restore hsc->RxState to Ready */
  1233.   hsc->RxState = HAL_SMARTCARD_STATE_READY;
  1234.  
  1235.   return HAL_OK;
  1236. }
  1237.  
  1238. /**
  1239.   * @brief  Abort ongoing transfers (Interrupt mode).
  1240.   * @param  hsc SMARTCARD handle.
  1241.   * @note   This procedure could be used for aborting any ongoing transfer started in Interrupt or DMA mode.
  1242.   *         This procedure performs following operations :
  1243.   *           - Disable PPP Interrupts
  1244.   *           - Disable the DMA transfer in the peripheral register (if enabled)
  1245.   *           - Abort DMA transfer by calling HAL_DMA_Abort_IT (in case of transfer in DMA mode)
  1246.   *           - Set handle State to READY
  1247.   *           - At abort completion, call user abort complete callback
  1248.   * @note   This procedure is executed in Interrupt mode, meaning that abort procedure could be
  1249.   *         considered as completed only when user abort complete callback is executed (not when exiting function).
  1250.   * @retval HAL status
  1251. */
  1252. HAL_StatusTypeDef HAL_SMARTCARD_Abort_IT(SMARTCARD_HandleTypeDef *hsc)
  1253. {
  1254.   uint32_t AbortCplt = 0x01U;
  1255.  
  1256.   /* Disable TXEIE, TCIE, RXNE, PE and ERR (Frame error, noise error, overrun error) interrupts */
  1257.   CLEAR_BIT(hsc->Instance->CR1, (USART_CR1_RXNEIE | USART_CR1_PEIE | USART_CR1_TXEIE | USART_CR1_TCIE));
  1258.   CLEAR_BIT(hsc->Instance->CR3, USART_CR3_EIE);
  1259.  
  1260.   /* If DMA Tx and/or DMA Rx Handles are associated to SMARTCARD Handle, DMA Abort complete callbacks should be initialised
  1261.      before any call to DMA Abort functions */
  1262.   /* DMA Tx Handle is valid */
  1263.   if(hsc->hdmatx != NULL)
  1264.   {
  1265.     /* Set DMA Abort Complete callback if SMARTCARD DMA Tx request if enabled.
  1266.        Otherwise, set it to NULL */
  1267.     if(HAL_IS_BIT_SET(hsc->Instance->CR3, USART_CR3_DMAT))
  1268.     {
  1269.       hsc->hdmatx->XferAbortCallback = SMARTCARD_DMATxAbortCallback;
  1270.     }
  1271.     else
  1272.     {
  1273.       hsc->hdmatx->XferAbortCallback = NULL;
  1274.     }
  1275.   }
  1276.   /* DMA Rx Handle is valid */
  1277.   if(hsc->hdmarx != NULL)
  1278.   {
  1279.     /* Set DMA Abort Complete callback if SMARTCARD DMA Rx request if enabled.
  1280.        Otherwise, set it to NULL */
  1281.     if(HAL_IS_BIT_SET(hsc->Instance->CR3, USART_CR3_DMAR))
  1282.     {
  1283.       hsc->hdmarx->XferAbortCallback = SMARTCARD_DMARxAbortCallback;
  1284.     }
  1285.     else
  1286.     {
  1287.       hsc->hdmarx->XferAbortCallback = NULL;
  1288.     }
  1289.   }
  1290.  
  1291.   /* Disable the SMARTCARD DMA Tx request if enabled */
  1292.   if(HAL_IS_BIT_SET(hsc->Instance->CR3, USART_CR3_DMAT))
  1293.   {
  1294.     /* Disable DMA Tx at SMARTCARD level */
  1295.     CLEAR_BIT(hsc->Instance->CR3, USART_CR3_DMAT);
  1296.  
  1297.     /* Abort the SMARTCARD DMA Tx channel : use non blocking DMA Abort API (callback) */
  1298.     if(hsc->hdmatx != NULL)
  1299.     {
  1300.       /* SMARTCARD Tx DMA Abort callback has already been initialised :
  1301.          will lead to call HAL_SMARTCARD_AbortCpltCallback() at end of DMA abort procedure */
  1302.  
  1303.       /* Abort DMA TX */
  1304.       if(HAL_DMA_Abort_IT(hsc->hdmatx) != HAL_OK)
  1305.       {
  1306.         hsc->hdmatx->XferAbortCallback = NULL;
  1307.       }
  1308.       else
  1309.       {
  1310.         AbortCplt = 0x00U;
  1311.       }
  1312.     }
  1313.   }
  1314.  
  1315.   /* Disable the SMARTCARD DMA Rx request if enabled */
  1316.   if(HAL_IS_BIT_SET(hsc->Instance->CR3, USART_CR3_DMAR))
  1317.   {
  1318.     CLEAR_BIT(hsc->Instance->CR3, USART_CR3_DMAR);
  1319.  
  1320.     /* Abort the SMARTCARD DMA Rx channel : use non blocking DMA Abort API (callback) */
  1321.     if(hsc->hdmarx != NULL)
  1322.     {
  1323.       /* SMARTCARD Rx DMA Abort callback has already been initialised :
  1324.          will lead to call HAL_SMARTCARD_AbortCpltCallback() at end of DMA abort procedure */
  1325.  
  1326.       /* Abort DMA RX */
  1327.       if(HAL_DMA_Abort_IT(hsc->hdmarx) != HAL_OK)
  1328.       {
  1329.         hsc->hdmarx->XferAbortCallback = NULL;
  1330.         AbortCplt = 0x01U;
  1331.       }
  1332.       else
  1333.       {
  1334.         AbortCplt = 0x00U;
  1335.       }
  1336.     }
  1337.   }
  1338.  
  1339.   /* if no DMA abort complete callback execution is required => call user Abort Complete callback */
  1340.   if(AbortCplt == 0x01U)
  1341.   {
  1342.     /* Reset Tx and Rx transfer counters */
  1343.     hsc->TxXferCount = 0x00U;
  1344.     hsc->RxXferCount = 0x00U;
  1345.  
  1346.     /* Reset ErrorCode */
  1347.     hsc->ErrorCode = HAL_SMARTCARD_ERROR_NONE;
  1348.  
  1349.     /* Restore hsc->gState and hsc->RxState to Ready */
  1350.     hsc->gState  = HAL_SMARTCARD_STATE_READY;
  1351.     hsc->RxState = HAL_SMARTCARD_STATE_READY;
  1352.  
  1353.     /* As no DMA to be aborted, call directly user Abort complete callback */
  1354. #if (USE_HAL_SMARTCARD_REGISTER_CALLBACKS == 1)
  1355.     /* Call registered Abort complete callback */
  1356.     hsc->AbortCpltCallback(hsc);
  1357. #else
  1358.     /* Call legacy weak Abort complete callback */
  1359.     HAL_SMARTCARD_AbortCpltCallback(hsc);
  1360. #endif /* USE_HAL_SMARTCARD_REGISTER_CALLBACK */
  1361.   }
  1362.   return HAL_OK;
  1363. }
  1364.  
  1365. /**
  1366.   * @brief  Abort ongoing Transmit transfer (Interrupt mode).
  1367.   * @param  hsc SMARTCARD handle.
  1368.   * @note   This procedure could be used for aborting any ongoing transfer started in Interrupt or DMA mode.
  1369.   *         This procedure performs following operations :
  1370.   *           - Disable SMARTCARD Interrupts (Tx)
  1371.   *           - Disable the DMA transfer in the peripheral register (if enabled)
  1372.   *           - Abort DMA transfer by calling HAL_DMA_Abort_IT (in case of transfer in DMA mode)
  1373.   *           - Set handle State to READY
  1374.   *           - At abort completion, call user abort complete callback
  1375.   * @note   This procedure is executed in Interrupt mode, meaning that abort procedure could be
  1376.   *         considered as completed only when user abort complete callback is executed (not when exiting function).
  1377.   * @retval HAL status
  1378. */
  1379. HAL_StatusTypeDef HAL_SMARTCARD_AbortTransmit_IT(SMARTCARD_HandleTypeDef *hsc)
  1380. {
  1381.   /* Disable TXEIE and TCIE interrupts */
  1382.   CLEAR_BIT(hsc->Instance->CR1, (USART_CR1_TXEIE | USART_CR1_TCIE));
  1383.  
  1384.   /* Disable the SMARTCARD DMA Tx request if enabled */
  1385.   if(HAL_IS_BIT_SET(hsc->Instance->CR3, USART_CR3_DMAT))
  1386.   {
  1387.     CLEAR_BIT(hsc->Instance->CR3, USART_CR3_DMAT);
  1388.  
  1389.     /* Abort the SMARTCARD DMA Tx channel : use blocking DMA Abort API (no callback) */
  1390.     if(hsc->hdmatx != NULL)
  1391.     {
  1392.       /* Set the SMARTCARD DMA Abort callback :
  1393.          will lead to call HAL_SMARTCARD_AbortCpltCallback() at end of DMA abort procedure */
  1394.       hsc->hdmatx->XferAbortCallback = SMARTCARD_DMATxOnlyAbortCallback;
  1395.  
  1396.       /* Abort DMA TX */
  1397.       if(HAL_DMA_Abort_IT(hsc->hdmatx) != HAL_OK)
  1398.       {
  1399.         /* Call Directly hsc->hdmatx->XferAbortCallback function in case of error */
  1400.         hsc->hdmatx->XferAbortCallback(hsc->hdmatx);
  1401.       }
  1402.     }
  1403.     else
  1404.     {
  1405.       /* Reset Tx transfer counter */
  1406.       hsc->TxXferCount = 0x00U;
  1407.  
  1408.       /* Restore hsc->gState to Ready */
  1409.       hsc->gState = HAL_SMARTCARD_STATE_READY;
  1410.  
  1411.       /* As no DMA to be aborted, call directly user Abort complete callback */
  1412. #if (USE_HAL_SMARTCARD_REGISTER_CALLBACKS == 1)
  1413.       /* Call registered Abort Transmit Complete Callback */
  1414.       hsc->AbortTransmitCpltCallback(hsc);
  1415. #else
  1416.       /* Call legacy weak Abort Transmit Complete Callback */
  1417.       HAL_SMARTCARD_AbortTransmitCpltCallback(hsc);
  1418. #endif /* USE_HAL_SMARTCARD_REGISTER_CALLBACK */
  1419.     }
  1420.   }
  1421.   else
  1422.   {
  1423.     /* Reset Tx transfer counter */
  1424.     hsc->TxXferCount = 0x00U;
  1425.  
  1426.     /* Restore hsc->gState to Ready */
  1427.     hsc->gState = HAL_SMARTCARD_STATE_READY;
  1428.  
  1429.     /* As no DMA to be aborted, call directly user Abort complete callback */
  1430. #if (USE_HAL_SMARTCARD_REGISTER_CALLBACKS == 1)
  1431.     /* Call registered Abort Transmit Complete Callback */
  1432.     hsc->AbortTransmitCpltCallback(hsc);
  1433. #else
  1434.     /* Call legacy weak Abort Transmit Complete Callback */
  1435.     HAL_SMARTCARD_AbortTransmitCpltCallback(hsc);
  1436. #endif /* USE_HAL_SMARTCARD_REGISTER_CALLBACK */
  1437.   }
  1438.  
  1439.   return HAL_OK;
  1440. }
  1441.  
  1442. /**
  1443.   * @brief  Abort ongoing Receive transfer (Interrupt mode).
  1444.   * @param  hsc SMARTCARD handle.
  1445.   * @note   This procedure could be used for aborting any ongoing transfer started in Interrupt or DMA mode.
  1446.   *         This procedure performs following operations :
  1447.   *           - Disable SMARTCARD Interrupts (Rx)
  1448.   *           - Disable the DMA transfer in the peripheral register (if enabled)
  1449.   *           - Abort DMA transfer by calling HAL_DMA_Abort_IT (in case of transfer in DMA mode)
  1450.   *           - Set handle State to READY
  1451.   *           - At abort completion, call user abort complete callback
  1452.   * @note   This procedure is executed in Interrupt mode, meaning that abort procedure could be
  1453.   *         considered as completed only when user abort complete callback is executed (not when exiting function).
  1454.   * @retval HAL status
  1455. */
  1456. HAL_StatusTypeDef HAL_SMARTCARD_AbortReceive_IT(SMARTCARD_HandleTypeDef *hsc)
  1457. {
  1458.   /* Disable RXNE, PE and ERR (Frame error, noise error, overrun error) interrupts */
  1459.   CLEAR_BIT(hsc->Instance->CR1, (USART_CR1_RXNEIE | USART_CR1_PEIE));
  1460.   CLEAR_BIT(hsc->Instance->CR3, USART_CR3_EIE);
  1461.  
  1462.   /* Disable the SMARTCARD DMA Rx request if enabled */
  1463.   if(HAL_IS_BIT_SET(hsc->Instance->CR3, USART_CR3_DMAR))
  1464.   {
  1465.     CLEAR_BIT(hsc->Instance->CR3, USART_CR3_DMAR);
  1466.  
  1467.     /* Abort the SMARTCARD DMA Rx channel : use blocking DMA Abort API (no callback) */
  1468.     if(hsc->hdmarx != NULL)
  1469.     {
  1470.       /* Set the SMARTCARD DMA Abort callback :
  1471.          will lead to call HAL_SMARTCARD_AbortCpltCallback() at end of DMA abort procedure */
  1472.       hsc->hdmarx->XferAbortCallback = SMARTCARD_DMARxOnlyAbortCallback;
  1473.  
  1474.       /* Abort DMA RX */
  1475.       if(HAL_DMA_Abort_IT(hsc->hdmarx) != HAL_OK)
  1476.       {
  1477.         /* Call Directly hsc->hdmarx->XferAbortCallback function in case of error */
  1478.         hsc->hdmarx->XferAbortCallback(hsc->hdmarx);
  1479.       }
  1480.     }
  1481.     else
  1482.     {
  1483.       /* Reset Rx transfer counter */
  1484.       hsc->RxXferCount = 0x00U;
  1485.  
  1486.       /* Restore hsc->RxState to Ready */
  1487.       hsc->RxState = HAL_SMARTCARD_STATE_READY;
  1488.  
  1489.       /* As no DMA to be aborted, call directly user Abort complete callback */
  1490. #if (USE_HAL_SMARTCARD_REGISTER_CALLBACKS == 1)
  1491.       /* Call registered Abort Receive Complete Callback */
  1492.       hsc->AbortReceiveCpltCallback(hsc);
  1493. #else
  1494.       /* Call legacy weak Abort Receive Complete Callback */
  1495.       HAL_SMARTCARD_AbortReceiveCpltCallback(hsc);
  1496. #endif /* USE_HAL_SMARTCARD_REGISTER_CALLBACK */
  1497.     }
  1498.   }
  1499.   else
  1500.   {
  1501.     /* Reset Rx transfer counter */
  1502.     hsc->RxXferCount = 0x00U;
  1503.  
  1504.     /* Restore hsc->RxState to Ready */
  1505.     hsc->RxState = HAL_SMARTCARD_STATE_READY;
  1506.  
  1507.     /* As no DMA to be aborted, call directly user Abort complete callback */
  1508. #if (USE_HAL_SMARTCARD_REGISTER_CALLBACKS == 1)
  1509.     /* Call registered Abort Receive Complete Callback */
  1510.     hsc->AbortReceiveCpltCallback(hsc);
  1511. #else
  1512.     /* Call legacy weak Abort Receive Complete Callback */
  1513.     HAL_SMARTCARD_AbortReceiveCpltCallback(hsc);
  1514. #endif /* USE_HAL_SMARTCARD_REGISTER_CALLBACK */
  1515.   }
  1516.  
  1517.   return HAL_OK;
  1518. }
  1519.  
  1520. /**
  1521.   * @brief This function handles SMARTCARD interrupt request.
  1522.   * @param  hsc    Pointer to a SMARTCARD_HandleTypeDef structure that contains
  1523.   *                the configuration information for SMARTCARD module.
  1524.   * @retval None
  1525.   */
  1526. void HAL_SMARTCARD_IRQHandler(SMARTCARD_HandleTypeDef *hsc)
  1527. {
  1528.   uint32_t isrflags   = READ_REG(hsc->Instance->SR);
  1529.   uint32_t cr1its     = READ_REG(hsc->Instance->CR1);
  1530.   uint32_t cr3its     = READ_REG(hsc->Instance->CR3);
  1531.   uint32_t dmarequest = 0x00U;
  1532.   uint32_t errorflags = 0x00U;
  1533.  
  1534.   /* If no error occurs */
  1535.   errorflags = (isrflags & (uint32_t)(USART_SR_PE | USART_SR_FE | USART_SR_ORE | USART_SR_NE));
  1536.   if(errorflags == RESET)
  1537.   {
  1538.     /* SMARTCARD in mode Receiver -------------------------------------------------*/
  1539.     if(((isrflags & USART_SR_RXNE) != RESET) && ((cr1its & USART_CR1_RXNEIE) != RESET))
  1540.     {
  1541.       SMARTCARD_Receive_IT(hsc);
  1542.       return;
  1543.     }
  1544.   }
  1545.  
  1546.   /* If some errors occur */
  1547.   if((errorflags != RESET) && (((cr3its & USART_CR3_EIE) != RESET) || ((cr1its & (USART_CR1_RXNEIE | USART_CR1_PEIE)) != RESET)))
  1548.   {
  1549.     /* SMARTCARD parity error interrupt occurred ---------------------------*/
  1550.     if(((isrflags & SMARTCARD_FLAG_PE) != RESET) && ((cr1its & USART_CR1_PEIE) != RESET))
  1551.     {
  1552.       hsc->ErrorCode |= HAL_SMARTCARD_ERROR_PE;
  1553.     }
  1554.  
  1555.     /* SMARTCARD frame error interrupt occurred ----------------------------*/
  1556.     if(((isrflags & SMARTCARD_FLAG_FE) != RESET) && ((cr3its & USART_CR3_EIE) != RESET))
  1557.     {
  1558.       hsc->ErrorCode |= HAL_SMARTCARD_ERROR_FE;
  1559.     }
  1560.  
  1561.     /* SMARTCARD noise error interrupt occurred ----------------------------*/
  1562.     if(((isrflags & SMARTCARD_FLAG_NE) != RESET) && ((cr3its & USART_CR3_EIE) != RESET))
  1563.     {
  1564.       hsc->ErrorCode |= HAL_SMARTCARD_ERROR_NE;
  1565.     }
  1566.  
  1567.     /* SMARTCARD Over-Run interrupt occurred -------------------------------*/
  1568.     if(((isrflags & SMARTCARD_FLAG_ORE) != RESET) && (((cr1its & USART_CR1_RXNEIE) != RESET) || ((cr3its & USART_CR3_EIE) != RESET)))
  1569.     {
  1570.       hsc->ErrorCode |= HAL_SMARTCARD_ERROR_ORE;
  1571.     }
  1572.     /* Call the Error call Back in case of Errors --------------------------*/
  1573.     if(hsc->ErrorCode != HAL_SMARTCARD_ERROR_NONE)
  1574.     {
  1575.       /* SMARTCARD in mode Receiver ----------------------------------------*/
  1576.       if(((isrflags & USART_SR_RXNE) != RESET) && ((cr1its & USART_CR1_RXNEIE) != RESET))
  1577.       {
  1578.         SMARTCARD_Receive_IT(hsc);
  1579.       }
  1580.  
  1581.       /* If Overrun error occurs, or if any error occurs in DMA mode reception,
  1582.          consider error as blocking */
  1583.       dmarequest = HAL_IS_BIT_SET(hsc->Instance->CR3, USART_CR3_DMAR);
  1584.       if(((hsc->ErrorCode & HAL_SMARTCARD_ERROR_ORE) != RESET) || dmarequest)
  1585.       {
  1586.         /* Blocking error : transfer is aborted
  1587.           Set the SMARTCARD state ready to be able to start again the process,
  1588.           Disable Rx Interrupts, and disable Rx DMA request, if ongoing */
  1589.         SMARTCARD_EndRxTransfer(hsc);
  1590.         /* Disable the SMARTCARD DMA Rx request if enabled */
  1591.         if(HAL_IS_BIT_SET(hsc->Instance->CR3, USART_CR3_DMAR))
  1592.         {
  1593.           CLEAR_BIT(hsc->Instance->CR3, USART_CR3_DMAR);
  1594.  
  1595.           /* Abort the SMARTCARD DMA Rx channel */
  1596.           if(hsc->hdmarx != NULL)
  1597.           {
  1598.             /* Set the SMARTCARD DMA Abort callback :
  1599.               will lead to call HAL_SMARTCARD_ErrorCallback() at end of DMA abort procedure */
  1600.             hsc->hdmarx->XferAbortCallback = SMARTCARD_DMAAbortOnError;
  1601.  
  1602.            if(HAL_DMA_Abort_IT(hsc->hdmarx) != HAL_OK)
  1603.             {
  1604.               /* Call Directly XferAbortCallback function in case of error */
  1605.               hsc->hdmarx->XferAbortCallback(hsc->hdmarx);
  1606.             }
  1607.           }
  1608.           else
  1609.           {
  1610. #if (USE_HAL_SMARTCARD_REGISTER_CALLBACKS == 1)
  1611.             /* Call registered user error callback */
  1612.             hsc->ErrorCallback(hsc);
  1613. #else
  1614.             /* Call legacy weak user error callback */
  1615.             HAL_SMARTCARD_ErrorCallback(hsc);
  1616. #endif /* USE_HAL_SMARTCARD_REGISTER_CALLBACK */
  1617.           }
  1618.         }
  1619.         else
  1620.         {
  1621. #if (USE_HAL_SMARTCARD_REGISTER_CALLBACKS == 1)
  1622.           /* Call registered user error callback */
  1623.           hsc->ErrorCallback(hsc);
  1624. #else
  1625.           /* Call legacy weak user error callback */
  1626.           HAL_SMARTCARD_ErrorCallback(hsc);
  1627. #endif /* USE_HAL_SMARTCARD_REGISTER_CALLBACK */
  1628.         }
  1629.       }
  1630.       else
  1631.       {
  1632.         /* Non Blocking error : transfer could go on.
  1633.            Error is notified to user through user error callback */
  1634. #if (USE_HAL_SMARTCARD_REGISTER_CALLBACKS == 1)
  1635.         /* Call registered user error callback */
  1636.         hsc->ErrorCallback(hsc);
  1637. #else
  1638.         /* Call legacy weak user error callback */
  1639.         HAL_SMARTCARD_ErrorCallback(hsc);
  1640. #endif /* USE_HAL_SMARTCARD_REGISTER_CALLBACK */
  1641.         hsc->ErrorCode = HAL_SMARTCARD_ERROR_NONE;
  1642.       }
  1643.     }
  1644.     return;
  1645.   } /* End if some error occurs */
  1646.  
  1647.   /* SMARTCARD in mode Transmitter ------------------------------------------*/
  1648.   if(((isrflags & SMARTCARD_FLAG_TXE) != RESET) && ((cr1its & USART_CR1_TXEIE) != RESET))
  1649.   {
  1650.     SMARTCARD_Transmit_IT(hsc);
  1651.     return;
  1652.   }
  1653.  
  1654.   /* SMARTCARD in mode Transmitter (transmission end) -----------------------*/
  1655.   if(((isrflags & SMARTCARD_FLAG_TC) != RESET) && ((cr1its & USART_CR1_TCIE) != RESET))
  1656.   {
  1657.     SMARTCARD_EndTransmit_IT(hsc);
  1658.     return;
  1659.   }
  1660. }
  1661.  
  1662. /**
  1663.   * @brief Tx Transfer completed callbacks
  1664.   * @param  hsc    Pointer to a SMARTCARD_HandleTypeDef structure that contains
  1665.   *                the configuration information for SMARTCARD module.
  1666.   * @retval None
  1667.   */
  1668. __weak void HAL_SMARTCARD_TxCpltCallback(SMARTCARD_HandleTypeDef *hsc)
  1669. {
  1670.   /* Prevent unused argument(s) compilation warning */
  1671.   UNUSED(hsc);
  1672.  
  1673.   /* NOTE : This function should not be modified, when the callback is needed,
  1674.             the HAL_SMARTCARD_TxCpltCallback can be implemented in the user file.
  1675.    */
  1676. }
  1677.  
  1678. /**
  1679.   * @brief Rx Transfer completed callback
  1680.   * @param  hsc    Pointer to a SMARTCARD_HandleTypeDef structure that contains
  1681.   *                the configuration information for SMARTCARD module.
  1682.   * @retval None
  1683.   */
  1684. __weak void HAL_SMARTCARD_RxCpltCallback(SMARTCARD_HandleTypeDef *hsc)
  1685. {
  1686.   /* Prevent unused argument(s) compilation warning */
  1687.   UNUSED(hsc);
  1688.  
  1689.   /* NOTE : This function should not be modified, when the callback is needed,
  1690.             the HAL_SMARTCARD_RxCpltCallback can be implemented in the user file.
  1691.    */
  1692. }
  1693.  
  1694. /**
  1695.   * @brief SMARTCARD error callback
  1696.   * @param  hsc    Pointer to a SMARTCARD_HandleTypeDef structure that contains
  1697.   *                the configuration information for SMARTCARD module.
  1698.   * @retval None
  1699.   */
  1700. __weak void HAL_SMARTCARD_ErrorCallback(SMARTCARD_HandleTypeDef *hsc)
  1701. {
  1702.   /* Prevent unused argument(s) compilation warning */
  1703.   UNUSED(hsc);
  1704.  
  1705.   /* NOTE : This function should not be modified, when the callback is needed,
  1706.             the HAL_SMARTCARD_ErrorCallback can be implemented in the user file.
  1707.    */
  1708. }
  1709.  
  1710. /**
  1711.   * @brief  SMARTCARD Abort Complete callback.
  1712.   * @param  hsc SMARTCARD handle.
  1713.   * @retval None
  1714.   */
  1715. __weak void HAL_SMARTCARD_AbortCpltCallback (SMARTCARD_HandleTypeDef *hsc)
  1716. {
  1717.   /* Prevent unused argument(s) compilation warning */
  1718.   UNUSED(hsc);
  1719.  
  1720.   /* NOTE : This function should not be modified, when the callback is needed,
  1721.             the HAL_SMARTCARD_AbortCpltCallback can be implemented in the user file.
  1722.    */
  1723. }
  1724.  
  1725. /**
  1726.   * @brief  SMARTCARD Abort Transmit Complete callback.
  1727.   * @param  hsc SMARTCARD handle.
  1728.   * @retval None
  1729.   */
  1730. __weak void HAL_SMARTCARD_AbortTransmitCpltCallback (SMARTCARD_HandleTypeDef *hsc)
  1731. {
  1732.     /* Prevent unused argument(s) compilation warning */
  1733.     UNUSED(hsc);
  1734.  
  1735.   /* NOTE : This function should not be modified, when the callback is needed,
  1736.             the HAL_SMARTCARD_AbortTransmitCpltCallback can be implemented in the user file.
  1737.    */
  1738. }
  1739.  
  1740. /**
  1741.   * @brief  SMARTCARD Abort Receive Complete callback.
  1742.   * @param  hsc SMARTCARD handle.
  1743.   * @retval None
  1744.   */
  1745. __weak void HAL_SMARTCARD_AbortReceiveCpltCallback (SMARTCARD_HandleTypeDef *hsc)
  1746. {
  1747.     /* Prevent unused argument(s) compilation warning */
  1748.     UNUSED(hsc);
  1749.  
  1750.   /* NOTE : This function should not be modified, when the callback is needed,
  1751.             the HAL_SMARTCARD_AbortReceiveCpltCallback can be implemented in the user file.
  1752.    */
  1753. }
  1754.  
  1755. /**
  1756.   * @}
  1757.   */
  1758.  
  1759. /** @defgroup SMARTCARD_Exported_Functions_Group3 Peripheral State and Errors functions
  1760.   *  @brief   SMARTCARD State and Errors functions
  1761.   *
  1762. @verbatim
  1763.  ===============================================================================
  1764.                 ##### Peripheral State and Errors functions #####
  1765.  ===============================================================================
  1766.     [..]
  1767.     This subsection provides a set of functions allowing to control the SmartCard.
  1768.      (+) HAL_SMARTCARD_GetState() API can be helpful to check in run-time the state of the SmartCard peripheral.
  1769.      (+) HAL_SMARTCARD_GetError() check in run-time errors that could be occurred during communication.
  1770. @endverbatim
  1771.   * @{
  1772.   */
  1773.  
  1774. /**
  1775.   * @brief Return the SMARTCARD handle state
  1776.   * @param  hsc    Pointer to a SMARTCARD_HandleTypeDef structure that contains
  1777.   *                the configuration information for SMARTCARD module.
  1778.   * @retval HAL state
  1779.   */
  1780. HAL_SMARTCARD_StateTypeDef HAL_SMARTCARD_GetState(SMARTCARD_HandleTypeDef *hsc)
  1781. {
  1782.   uint32_t temp1= 0x00U, temp2 = 0x00U;
  1783.   temp1 = hsc->gState;
  1784.   temp2 = hsc->RxState;
  1785.  
  1786.   return (HAL_SMARTCARD_StateTypeDef)(temp1 | temp2);
  1787. }
  1788.  
  1789. /**
  1790.   * @brief  Return the SMARTCARD error code
  1791.   * @param  hsc  Pointer to a SMARTCARD_HandleTypeDef structure that contains
  1792.   *              the configuration information for the specified SMARTCARD.
  1793.   * @retval SMARTCARD Error Code
  1794.   */
  1795. uint32_t HAL_SMARTCARD_GetError(SMARTCARD_HandleTypeDef *hsc)
  1796. {
  1797.   return hsc->ErrorCode;
  1798. }
  1799.  
  1800. /**
  1801.   * @}
  1802.   */
  1803.  
  1804. /**
  1805.   * @}
  1806.   */
  1807.  
  1808. /** @defgroup SMARTCARD_Private_Functions SMARTCARD Private Functions
  1809.   * @{
  1810.   */
  1811.  
  1812. #if (USE_HAL_SMARTCARD_REGISTER_CALLBACKS == 1)
  1813. /**
  1814.   * @brief  Initialize the callbacks to their default values.
  1815.   * @param  hsc SMARTCARD handle.
  1816.   * @retval none
  1817.   */
  1818. void SMARTCARD_InitCallbacksToDefault(SMARTCARD_HandleTypeDef *hsc)
  1819. {
  1820.   /* Init the SMARTCARD Callback settings */
  1821.   hsc->TxCpltCallback            = HAL_SMARTCARD_TxCpltCallback;            /* Legacy weak TxCpltCallback            */
  1822.   hsc->RxCpltCallback            = HAL_SMARTCARD_RxCpltCallback;            /* Legacy weak RxCpltCallback            */
  1823.   hsc->ErrorCallback             = HAL_SMARTCARD_ErrorCallback;             /* Legacy weak ErrorCallback             */
  1824.   hsc->AbortCpltCallback         = HAL_SMARTCARD_AbortCpltCallback;         /* Legacy weak AbortCpltCallback         */
  1825.   hsc->AbortTransmitCpltCallback = HAL_SMARTCARD_AbortTransmitCpltCallback; /* Legacy weak AbortTransmitCpltCallback */
  1826.   hsc->AbortReceiveCpltCallback  = HAL_SMARTCARD_AbortReceiveCpltCallback;  /* Legacy weak AbortReceiveCpltCallback  */
  1827.  
  1828. }
  1829. #endif /* USE_HAL_SMARTCARD_REGISTER_CALLBACKS */
  1830.  
  1831. /**
  1832.   * @brief DMA SMARTCARD transmit process complete callback
  1833.   * @param  hdma   Pointer to a DMA_HandleTypeDef structure that contains
  1834.   *                the configuration information for the specified DMA module.
  1835.   * @retval None
  1836.   */
  1837. static void SMARTCARD_DMATransmitCplt(DMA_HandleTypeDef *hdma)
  1838. {
  1839.   SMARTCARD_HandleTypeDef* hsc = ( SMARTCARD_HandleTypeDef* )((DMA_HandleTypeDef* )hdma)->Parent;
  1840.  
  1841.   hsc->TxXferCount = 0U;
  1842.  
  1843.   /* Disable the DMA transfer for transmit request by setting the DMAT bit
  1844.      in the USART CR3 register */
  1845.   CLEAR_BIT(hsc->Instance->CR3, USART_CR3_DMAT);
  1846.  
  1847.   /* Enable the SMARTCARD Transmit Complete Interrupt */
  1848.   SET_BIT(hsc->Instance->CR1, USART_CR1_TCIE);
  1849. }
  1850.  
  1851. /**
  1852.   * @brief DMA SMARTCARD receive process complete callback
  1853.   * @param  hdma   Pointer to a DMA_HandleTypeDef structure that contains
  1854.   *                the configuration information for the specified DMA module.
  1855.   * @retval None
  1856.   */
  1857. static void SMARTCARD_DMAReceiveCplt(DMA_HandleTypeDef *hdma)
  1858. {
  1859.   SMARTCARD_HandleTypeDef* hsc = ( SMARTCARD_HandleTypeDef* )((DMA_HandleTypeDef* )hdma)->Parent;
  1860.  
  1861.   hsc->RxXferCount = 0U;
  1862.  
  1863.   /* Disable RXNE, PE and ERR (Frame error, noise error, overrun error) interrupts */
  1864.   CLEAR_BIT(hsc->Instance->CR1, (USART_CR1_RXNEIE | USART_CR1_PEIE));
  1865.   CLEAR_BIT(hsc->Instance->CR3, USART_CR3_EIE);
  1866.  
  1867.   /* Disable the DMA transfer for the receiver request by setting the DMAR bit
  1868.      in the USART CR3 register */
  1869.   CLEAR_BIT(hsc->Instance->CR3, USART_CR3_DMAR);
  1870.  
  1871.   /* At end of Rx process, restore hsc->RxState to Ready */
  1872.   hsc->RxState = HAL_SMARTCARD_STATE_READY;
  1873.  
  1874. #if (USE_HAL_SMARTCARD_REGISTER_CALLBACKS == 1)
  1875.   /* Call registered Rx complete callback */
  1876.   hsc->RxCpltCallback(hsc);
  1877. #else
  1878.   /* Call legacy weak Rx complete callback */
  1879.   HAL_SMARTCARD_RxCpltCallback(hsc);
  1880. #endif /* USE_HAL_SMARTCARD_REGISTER_CALLBACK */
  1881. }
  1882.  
  1883. /**
  1884.   * @brief DMA SMARTCARD communication error callback
  1885.   * @param  hdma   Pointer to a DMA_HandleTypeDef structure that contains
  1886.   *                the configuration information for the specified DMA module.
  1887.   * @retval None
  1888.   */
  1889. static void SMARTCARD_DMAError(DMA_HandleTypeDef *hdma)
  1890. {
  1891.   uint32_t dmarequest = 0x00U;
  1892.   SMARTCARD_HandleTypeDef* hsc = ( SMARTCARD_HandleTypeDef* )((DMA_HandleTypeDef* )hdma)->Parent;
  1893.   hsc->RxXferCount = 0U;
  1894.   hsc->TxXferCount = 0U;
  1895.   hsc->ErrorCode = HAL_SMARTCARD_ERROR_DMA;
  1896.  
  1897.   /* Stop SMARTCARD DMA Tx request if ongoing */
  1898.   dmarequest = HAL_IS_BIT_SET(hsc->Instance->CR3, USART_CR3_DMAT);
  1899.   if((hsc->gState == HAL_SMARTCARD_STATE_BUSY_TX) && dmarequest)
  1900.   {
  1901.     SMARTCARD_EndTxTransfer(hsc);
  1902.   }
  1903.  
  1904.   /* Stop SMARTCARD DMA Rx request if ongoing */
  1905.   dmarequest = HAL_IS_BIT_SET(hsc->Instance->CR3, USART_CR3_DMAR);
  1906.   if((hsc->RxState == HAL_SMARTCARD_STATE_BUSY_RX) && dmarequest)
  1907.   {
  1908.     SMARTCARD_EndRxTransfer(hsc);
  1909.   }
  1910.  
  1911. #if (USE_HAL_SMARTCARD_REGISTER_CALLBACKS == 1)
  1912.   /* Call registered user error callback */
  1913.   hsc->ErrorCallback(hsc);
  1914. #else
  1915.   /* Call legacy weak user error callback */
  1916.   HAL_SMARTCARD_ErrorCallback(hsc);
  1917. #endif /* USE_HAL_SMARTCARD_REGISTER_CALLBACK */
  1918. }
  1919.  
  1920. /**
  1921.   * @brief  This function handles SMARTCARD Communication Timeout.
  1922.   * @param  hsc    Pointer to a SMARTCARD_HandleTypeDef structure that contains
  1923.   *                the configuration information for SMARTCARD module.
  1924.   * @param  Flag   Specifies the SMARTCARD flag to check.
  1925.   * @param  Status The new Flag status (SET or RESET).
  1926.   * @param  Timeout Timeout duration
  1927.   * @param  Tickstart Tick start value
  1928.   * @retval HAL status
  1929.   */
  1930. static HAL_StatusTypeDef SMARTCARD_WaitOnFlagUntilTimeout(SMARTCARD_HandleTypeDef *hsc, uint32_t Flag, FlagStatus Status, uint32_t Tickstart, uint32_t Timeout)
  1931. {
  1932.   /* Wait until flag is set */
  1933.   while((__HAL_SMARTCARD_GET_FLAG(hsc, Flag) ? SET : RESET) == Status)
  1934.   {
  1935.     /* Check for the Timeout */
  1936.     if(Timeout != HAL_MAX_DELAY)
  1937.     {
  1938.       if((Timeout == 0U)||((HAL_GetTick() - Tickstart ) > Timeout))
  1939.       {
  1940.         /* Disable TXE and RXNE interrupts for the interrupt process */
  1941.         CLEAR_BIT(hsc->Instance->CR1, USART_CR1_TXEIE);
  1942.         CLEAR_BIT(hsc->Instance->CR1, USART_CR1_RXNEIE);
  1943.  
  1944.         hsc->gState= HAL_SMARTCARD_STATE_READY;
  1945.         hsc->RxState= HAL_SMARTCARD_STATE_READY;
  1946.  
  1947.         /* Process Unlocked */
  1948.         __HAL_UNLOCK(hsc);
  1949.  
  1950.         return HAL_TIMEOUT;
  1951.       }
  1952.     }
  1953.   }
  1954.   return HAL_OK;
  1955. }
  1956.  
  1957. /**
  1958.   * @brief  End ongoing Tx transfer on SMARTCARD peripheral (following error detection or Transmit completion).
  1959.   * @param  hsc    Pointer to a SMARTCARD_HandleTypeDef structure that contains
  1960.   *                the configuration information for SMARTCARD module.
  1961.   * @retval None
  1962.   */
  1963. static void SMARTCARD_EndTxTransfer(SMARTCARD_HandleTypeDef *hsc)
  1964. {
  1965.   /* At end of Tx process, restore hsc->gState to Ready */
  1966.   hsc->gState = HAL_SMARTCARD_STATE_READY;
  1967.  
  1968.   /* Disable TXEIE and TCIE interrupts */
  1969.   CLEAR_BIT(hsc->Instance->CR1, (USART_CR1_TXEIE | USART_CR1_TCIE));
  1970. }
  1971.  
  1972.  
  1973. /**
  1974.   * @brief  End ongoing Rx transfer on SMARTCARD peripheral (following error detection or Reception completion).
  1975.   * @param  hsc    Pointer to a SMARTCARD_HandleTypeDef structure that contains
  1976.   *                the configuration information for SMARTCARD module.
  1977.   * @retval None
  1978.   */
  1979. static void SMARTCARD_EndRxTransfer(SMARTCARD_HandleTypeDef *hsc)
  1980. {
  1981.   /* At end of Rx process, restore hsc->RxState to Ready */
  1982.   hsc->RxState = HAL_SMARTCARD_STATE_READY;
  1983.  
  1984.   /* Disable RXNE, PE and ERR (Frame error, noise error, overrun error) interrupts */
  1985.   CLEAR_BIT(hsc->Instance->CR1, (USART_CR1_RXNEIE | USART_CR1_PEIE));
  1986.   CLEAR_BIT(hsc->Instance->CR3, USART_CR3_EIE);
  1987. }
  1988.  
  1989. /**
  1990.   * @brief Send an amount of data in non blocking mode
  1991.   * @param  hsc    Pointer to a SMARTCARD_HandleTypeDef structure that contains
  1992.   *                the configuration information for SMARTCARD module.
  1993.   * @retval HAL status
  1994.   */
  1995. static HAL_StatusTypeDef SMARTCARD_Transmit_IT(SMARTCARD_HandleTypeDef *hsc)
  1996. {
  1997.  
  1998.   /* Check that a Tx process is ongoing */
  1999.   if(hsc->gState == HAL_SMARTCARD_STATE_BUSY_TX)
  2000.   {
  2001.     hsc->Instance->DR = (uint8_t)(*hsc->pTxBuffPtr & 0xFFU);
  2002.     hsc->pTxBuffPtr++;
  2003.  
  2004.     if(--hsc->TxXferCount == 0U)
  2005.     {
  2006.       /* Disable the SMARTCARD Transmit data register empty Interrupt */
  2007.       CLEAR_BIT(hsc->Instance->CR1, USART_CR1_TXEIE);
  2008.  
  2009.       /* Enable the SMARTCARD Transmit Complete Interrupt */
  2010.       SET_BIT(hsc->Instance->CR1, USART_CR1_TCIE);
  2011.     }
  2012.  
  2013.     return HAL_OK;
  2014.   }
  2015.   else
  2016.   {
  2017.     return HAL_BUSY;
  2018.   }
  2019. }
  2020.  
  2021. /**
  2022.   * @brief  Wraps up transmission in non blocking mode.
  2023.   * @param  hsc    Pointer to a SMARTCARD_HandleTypeDef structure that contains
  2024.   *                the configuration information for the specified SMARTCARD module.
  2025.   * @retval HAL status
  2026.   */
  2027. static HAL_StatusTypeDef SMARTCARD_EndTransmit_IT(SMARTCARD_HandleTypeDef *hsc)
  2028. {
  2029.   /* Disable the SMARTCARD Transmit Complete Interrupt */
  2030.   CLEAR_BIT(hsc->Instance->CR1, USART_CR1_TCIE);
  2031.  
  2032.   /* Disable the SMARTCARD Error Interrupt: (Frame error, noise error, overrun error) */
  2033.   CLEAR_BIT(hsc->Instance->CR3, USART_CR3_EIE);
  2034.  
  2035.   /* Tx process is ended, restore hsc->gState to Ready */
  2036.   hsc->gState = HAL_SMARTCARD_STATE_READY;
  2037.  
  2038. #if (USE_HAL_SMARTCARD_REGISTER_CALLBACKS == 1)
  2039.   /* Call registered Tx complete callback */
  2040.   hsc->TxCpltCallback(hsc);
  2041. #else
  2042.   /* Call legacy weak Tx complete callback */
  2043.   HAL_SMARTCARD_TxCpltCallback(hsc);
  2044. #endif /* USE_HAL_SMARTCARD_REGISTER_CALLBACK */
  2045.  
  2046.   return HAL_OK;
  2047. }
  2048.  
  2049. /**
  2050.   * @brief Receive an amount of data in non blocking mode
  2051.   * @param  hsc    Pointer to a SMARTCARD_HandleTypeDef structure that contains
  2052.   *                the configuration information for SMARTCARD module.
  2053.   * @retval HAL status
  2054.   */
  2055. static HAL_StatusTypeDef SMARTCARD_Receive_IT(SMARTCARD_HandleTypeDef *hsc)
  2056. {
  2057.  
  2058.   /* Check that a Rx process is ongoing */
  2059.   if(hsc->RxState == HAL_SMARTCARD_STATE_BUSY_RX)
  2060.   {
  2061.     *hsc->pRxBuffPtr = (uint8_t)(hsc->Instance->DR & (uint8_t)0xFFU);
  2062.     hsc->pRxBuffPtr++;
  2063.  
  2064.     if(--hsc->RxXferCount == 0U)
  2065.     {
  2066.       CLEAR_BIT(hsc->Instance->CR1, USART_CR1_RXNEIE);
  2067.  
  2068.       /* Disable the SMARTCARD Parity Error Interrupt */
  2069.       CLEAR_BIT(hsc->Instance->CR1, USART_CR1_PEIE);
  2070.  
  2071.       /* Disable the SMARTCARD Error Interrupt: (Frame error, noise error, overrun error) */
  2072.       CLEAR_BIT(hsc->Instance->CR3, USART_CR3_EIE);
  2073.  
  2074.       /* Rx process is completed, restore hsc->RxState to Ready */
  2075.       hsc->RxState = HAL_SMARTCARD_STATE_READY;
  2076.  
  2077. #if (USE_HAL_SMARTCARD_REGISTER_CALLBACKS == 1)
  2078.       /* Call registered Rx complete callback */
  2079.       hsc->RxCpltCallback(hsc);
  2080. #else
  2081.       /* Call legacy weak Rx complete callback */
  2082.       HAL_SMARTCARD_RxCpltCallback(hsc);
  2083. #endif /* USE_HAL_SMARTCARD_REGISTER_CALLBACK */
  2084.  
  2085.       return HAL_OK;
  2086.     }
  2087.     return HAL_OK;
  2088.   }
  2089.   else
  2090.   {
  2091.     return HAL_BUSY;
  2092.   }
  2093. }
  2094.  
  2095. /**
  2096.   * @brief  DMA SMARTCARD communication abort callback, when initiated by HAL services on Error
  2097.   *         (To be called at end of DMA Abort procedure following error occurrence).
  2098.   * @param  hdma DMA handle.
  2099.   * @retval None
  2100.   */
  2101. static void SMARTCARD_DMAAbortOnError(DMA_HandleTypeDef *hdma)
  2102. {
  2103.   SMARTCARD_HandleTypeDef* hsc = (SMARTCARD_HandleTypeDef* )((DMA_HandleTypeDef* )hdma)->Parent;
  2104.   hsc->RxXferCount = 0x00U;
  2105.   hsc->TxXferCount = 0x00U;
  2106.  
  2107. #if (USE_HAL_SMARTCARD_REGISTER_CALLBACKS == 1)
  2108.   /* Call registered user error callback */
  2109.   hsc->ErrorCallback(hsc);
  2110. #else
  2111.   /* Call legacy weak user error callback */
  2112.   HAL_SMARTCARD_ErrorCallback(hsc);
  2113. #endif /* USE_HAL_SMARTCARD_REGISTER_CALLBACK */
  2114. }
  2115.  
  2116. /**
  2117.   * @brief  DMA SMARTCARD Tx communication abort callback, when initiated by user
  2118.   *         (To be called at end of DMA Tx Abort procedure following user abort request).
  2119.   * @note   When this callback is executed, User Abort complete call back is called only if no
  2120.   *         Abort still ongoing for Rx DMA Handle.
  2121.   * @param  hdma DMA handle.
  2122.   * @retval None
  2123.   */
  2124. static void SMARTCARD_DMATxAbortCallback(DMA_HandleTypeDef *hdma)
  2125. {
  2126.   SMARTCARD_HandleTypeDef* hsc = ( SMARTCARD_HandleTypeDef* )((DMA_HandleTypeDef* )hdma)->Parent;
  2127.  
  2128.   hsc->hdmatx->XferAbortCallback = NULL;
  2129.  
  2130.   /* Check if an Abort process is still ongoing */
  2131.   if(hsc->hdmarx != NULL)
  2132.   {
  2133.     if(hsc->hdmarx->XferAbortCallback != NULL)
  2134.     {
  2135.       return;
  2136.     }
  2137.   }
  2138.  
  2139.   /* No Abort process still ongoing : All DMA channels are aborted, call user Abort Complete callback */
  2140.   hsc->TxXferCount = 0x00U;
  2141.   hsc->RxXferCount = 0x00U;
  2142.  
  2143.   /* Reset ErrorCode */
  2144.   hsc->ErrorCode = HAL_SMARTCARD_ERROR_NONE;
  2145.  
  2146.   /* Restore hsc->gState and hsc->RxState to Ready */
  2147.   hsc->gState  = HAL_SMARTCARD_STATE_READY;
  2148.   hsc->RxState = HAL_SMARTCARD_STATE_READY;
  2149.  
  2150. #if (USE_HAL_SMARTCARD_REGISTER_CALLBACKS == 1)
  2151.   /* Call registered Abort complete callback */
  2152.   hsc->AbortCpltCallback(hsc);
  2153. #else
  2154.   /* Call legacy weak Abort complete callback */
  2155.   HAL_SMARTCARD_AbortCpltCallback(hsc);
  2156. #endif /* USE_HAL_SMARTCARD_REGISTER_CALLBACK */
  2157. }
  2158.  
  2159. /**
  2160.   * @brief  DMA SMARTCARD Rx communication abort callback, when initiated by user
  2161.   *         (To be called at end of DMA Rx Abort procedure following user abort request).
  2162.   * @note   When this callback is executed, User Abort complete call back is called only if no
  2163.   *         Abort still ongoing for Tx DMA Handle.
  2164.   * @param  hdma DMA handle.
  2165.   * @retval None
  2166.   */
  2167. static void SMARTCARD_DMARxAbortCallback(DMA_HandleTypeDef *hdma)
  2168. {
  2169.   SMARTCARD_HandleTypeDef* hsc = ( SMARTCARD_HandleTypeDef* )((DMA_HandleTypeDef* )hdma)->Parent;
  2170.  
  2171.   hsc->hdmarx->XferAbortCallback = NULL;
  2172.  
  2173.   /* Check if an Abort process is still ongoing */
  2174.   if(hsc->hdmatx != NULL)
  2175.   {
  2176.     if(hsc->hdmatx->XferAbortCallback != NULL)
  2177.     {
  2178.       return;
  2179.     }
  2180.   }
  2181.  
  2182.   /* No Abort process still ongoing : All DMA channels are aborted, call user Abort Complete callback */
  2183.   hsc->TxXferCount = 0x00U;
  2184.   hsc->RxXferCount = 0x00U;
  2185.  
  2186.   /* Reset ErrorCode */
  2187.   hsc->ErrorCode = HAL_SMARTCARD_ERROR_NONE;
  2188.  
  2189.   /* Restore hsc->gState and hsc->RxState to Ready */
  2190.   hsc->gState  = HAL_SMARTCARD_STATE_READY;
  2191.   hsc->RxState = HAL_SMARTCARD_STATE_READY;
  2192.  
  2193. #if (USE_HAL_SMARTCARD_REGISTER_CALLBACKS == 1)
  2194.   /* Call registered Abort complete callback */
  2195.   hsc->AbortCpltCallback(hsc);
  2196. #else
  2197.   /* Call legacy weak Abort complete callback */
  2198.   HAL_SMARTCARD_AbortCpltCallback(hsc);
  2199. #endif /* USE_HAL_SMARTCARD_REGISTER_CALLBACK */
  2200. }
  2201.  
  2202. /**
  2203.   * @brief  DMA SMARTCARD Tx communication abort callback, when initiated by user by a call to
  2204.   *         HAL_SMARTCARD_AbortTransmit_IT API (Abort only Tx transfer)
  2205.   *         (This callback is executed at end of DMA Tx Abort procedure following user abort request,
  2206.   *         and leads to user Tx Abort Complete callback execution).
  2207.   * @param  hdma DMA handle.
  2208.   * @retval None
  2209.   */
  2210. static void SMARTCARD_DMATxOnlyAbortCallback(DMA_HandleTypeDef *hdma)
  2211. {
  2212.   SMARTCARD_HandleTypeDef* hsc = ( SMARTCARD_HandleTypeDef* )((DMA_HandleTypeDef* )hdma)->Parent;
  2213.  
  2214.   hsc->TxXferCount = 0x00U;
  2215.  
  2216.   /* Restore hsc->gState to Ready */
  2217.   hsc->gState = HAL_SMARTCARD_STATE_READY;
  2218.  
  2219. #if (USE_HAL_SMARTCARD_REGISTER_CALLBACKS == 1)
  2220.   /* Call registered Abort Transmit Complete Callback */
  2221.   hsc->AbortTransmitCpltCallback(hsc);
  2222. #else
  2223.   /* Call legacy weak Abort Transmit Complete Callback */
  2224.   HAL_SMARTCARD_AbortTransmitCpltCallback(hsc);
  2225. #endif /* USE_HAL_SMARTCARD_REGISTER_CALLBACK */
  2226. }
  2227.  
  2228. /**
  2229.   * @brief  DMA SMARTCARD Rx communication abort callback, when initiated by user by a call to
  2230.   *         HAL_SMARTCARD_AbortReceive_IT API (Abort only Rx transfer)
  2231.   *         (This callback is executed at end of DMA Rx Abort procedure following user abort request,
  2232.   *         and leads to user Rx Abort Complete callback execution).
  2233.   * @param  hdma DMA handle.
  2234.   * @retval None
  2235.   */
  2236. static void SMARTCARD_DMARxOnlyAbortCallback(DMA_HandleTypeDef *hdma)
  2237. {
  2238.   SMARTCARD_HandleTypeDef* hsc = ( SMARTCARD_HandleTypeDef* )((DMA_HandleTypeDef* )hdma)->Parent;
  2239.  
  2240.   hsc->RxXferCount = 0x00U;
  2241.  
  2242.   /* Restore hsc->RxState to Ready */
  2243.   hsc->RxState = HAL_SMARTCARD_STATE_READY;
  2244.  
  2245. #if (USE_HAL_SMARTCARD_REGISTER_CALLBACKS == 1)
  2246.   /* Call registered Abort Receive Complete Callback */
  2247.   hsc->AbortReceiveCpltCallback(hsc);
  2248. #else
  2249.   /* Call legacy weak Abort Receive Complete Callback */
  2250.   HAL_SMARTCARD_AbortReceiveCpltCallback(hsc);
  2251. #endif /* USE_HAL_SMARTCARD_REGISTER_CALLBACK */
  2252. }
  2253.  
  2254. /**
  2255.   * @brief Configure the SMARTCARD peripheral
  2256.   * @param  hsc    Pointer to a SMARTCARD_HandleTypeDef structure that contains
  2257.   *                the configuration information for SMARTCARD module.
  2258.   * @retval None
  2259.   */
  2260. static void SMARTCARD_SetConfig(SMARTCARD_HandleTypeDef *hsc)
  2261. {
  2262.   uint32_t tmpreg = 0x00U;
  2263.   uint32_t pclk;
  2264.  
  2265.   /* Check the parameters */
  2266.   assert_param(IS_SMARTCARD_INSTANCE(hsc->Instance));
  2267.   assert_param(IS_SMARTCARD_POLARITY(hsc->Init.CLKPolarity));
  2268.   assert_param(IS_SMARTCARD_PHASE(hsc->Init.CLKPhase));
  2269.   assert_param(IS_SMARTCARD_LASTBIT(hsc->Init.CLKLastBit));
  2270.   assert_param(IS_SMARTCARD_BAUDRATE(hsc->Init.BaudRate));
  2271.   assert_param(IS_SMARTCARD_WORD_LENGTH(hsc->Init.WordLength));
  2272.   assert_param(IS_SMARTCARD_STOPBITS(hsc->Init.StopBits));
  2273.   assert_param(IS_SMARTCARD_PARITY(hsc->Init.Parity));
  2274.   assert_param(IS_SMARTCARD_MODE(hsc->Init.Mode));
  2275.   assert_param(IS_SMARTCARD_NACK_STATE(hsc->Init.NACKState));
  2276.  
  2277.   /* The LBCL, CPOL and CPHA bits have to be selected when both the transmitter and the
  2278.      receiver are disabled (TE=RE=0) to ensure that the clock pulses function correctly. */
  2279.   CLEAR_BIT(hsc->Instance->CR1, (USART_CR1_TE | USART_CR1_RE));
  2280.  
  2281.   /*---------------------------- USART CR2 Configuration ---------------------*/
  2282.   tmpreg = hsc->Instance->CR2;
  2283.   /* Clear CLKEN, CPOL, CPHA and LBCL bits */
  2284.   tmpreg &= (uint32_t)~((uint32_t)(USART_CR2_CPHA | USART_CR2_CPOL | USART_CR2_CLKEN | USART_CR2_LBCL));
  2285.   /* Configure the SMARTCARD Clock, CPOL, CPHA and LastBit -----------------------*/
  2286.   /* Set CPOL bit according to hsc->Init.CLKPolarity value */
  2287.   /* Set CPHA bit according to hsc->Init.CLKPhase value */
  2288.   /* Set LBCL bit according to hsc->Init.CLKLastBit value */
  2289.   /* Set Stop Bits: Set STOP[13:12] bits according to hsc->Init.StopBits value */
  2290.   tmpreg |= (uint32_t)(USART_CR2_CLKEN | hsc->Init.CLKPolarity |
  2291.                       hsc->Init.CLKPhase| hsc->Init.CLKLastBit | hsc->Init.StopBits);
  2292.   /* Write to USART CR2 */
  2293.   WRITE_REG(hsc->Instance->CR2, (uint32_t)tmpreg);
  2294.  
  2295.   tmpreg = hsc->Instance->CR2;
  2296.  
  2297.   /* Clear STOP[13:12] bits */
  2298.   tmpreg &= (uint32_t)~((uint32_t)USART_CR2_STOP);
  2299.  
  2300.   /* Set Stop Bits: Set STOP[13:12] bits according to hsc->Init.StopBits value */
  2301.   tmpreg |= (uint32_t)(hsc->Init.StopBits);
  2302.  
  2303.   /* Write to USART CR2 */
  2304.   WRITE_REG(hsc->Instance->CR2, (uint32_t)tmpreg);
  2305.  
  2306.   /*-------------------------- USART CR1 Configuration -----------------------*/
  2307.   tmpreg = hsc->Instance->CR1;
  2308.  
  2309.   /* Clear M, PCE, PS, TE and RE bits */
  2310.   tmpreg &= (uint32_t)~((uint32_t)(USART_CR1_M | USART_CR1_PCE | USART_CR1_PS | USART_CR1_TE | \
  2311.                                    USART_CR1_RE));
  2312.  
  2313.   /* Configure the SMARTCARD Word Length, Parity and mode:
  2314.      Set the M bits according to hsc->Init.WordLength value
  2315.      Set PCE and PS bits according to hsc->Init.Parity value
  2316.      Set TE and RE bits according to hsc->Init.Mode value */
  2317.   tmpreg |= (uint32_t)hsc->Init.WordLength | hsc->Init.Parity | hsc->Init.Mode;
  2318.  
  2319.   /* Write to USART CR1 */
  2320.   WRITE_REG(hsc->Instance->CR1, (uint32_t)tmpreg);
  2321.  
  2322.   /*-------------------------- USART CR3 Configuration -----------------------*/
  2323.   /* Clear CTSE and RTSE bits */
  2324.   CLEAR_BIT(hsc->Instance->CR3, (USART_CR3_RTSE | USART_CR3_CTSE));
  2325.  
  2326.   /*-------------------------- USART BRR Configuration -----------------------*/
  2327.   if(hsc->Instance == USART1)
  2328.   {
  2329.     pclk = HAL_RCC_GetPCLK2Freq();
  2330.     hsc->Instance->BRR = SMARTCARD_BRR(pclk, hsc->Init.BaudRate);
  2331.   }
  2332.   else
  2333.   {
  2334.     pclk = HAL_RCC_GetPCLK1Freq();
  2335.     hsc->Instance->BRR = SMARTCARD_BRR(pclk, hsc->Init.BaudRate);
  2336.   }
  2337. }
  2338.  
  2339. /**
  2340.   * @}
  2341.   */
  2342.  
  2343. #endif /* HAL_SMARTCARD_MODULE_ENABLED */
  2344. /**
  2345.   * @}
  2346.   */
  2347.  
  2348. /**
  2349.   * @}
  2350.   */
  2351.  
  2352. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
  2353.