Subversion Repositories ScreenTimer

Rev

Blame | Last modification | View Log | Download | RSS feed

  1. /**
  2.   ******************************************************************************
  3.   * @file    stm32f0xx_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 (eg. SMARTCARD_HandleTypeDef hsmartcard).
  21.     (#) Associate a USART to the SMARTCARD handle hsmartcard.
  22.     (#) Initialize the SMARTCARD low level resources by implementing the HAL_SMARTCARD_MspInit() API:
  23.         (++) Enable the USARTx interface clock.
  24.         (++) USART pins configuration:
  25.              (+++) Enable the clock for the USART GPIOs.
  26.              (+++) Configure the USART pins (TX as alternate function pull-up, RX as alternate function Input).
  27.         (++) NVIC configuration if you need to use interrupt process (HAL_SMARTCARD_Transmit_IT()
  28.              and HAL_SMARTCARD_Receive_IT() APIs):
  29.              (+++) Configure the USARTx interrupt priority.
  30.              (+++) Enable the NVIC USART IRQ handle.
  31.         (++) DMA Configuration if you need to use DMA process (HAL_SMARTCARD_Transmit_DMA()
  32.              and HAL_SMARTCARD_Receive_DMA() APIs):
  33.              (+++) Declare a DMA handle structure for the Tx/Rx channel.
  34.              (+++) Enable the DMAx interface clock.
  35.              (+++) Configure the declared DMA handle structure with the required Tx/Rx parameters.
  36.              (+++) Configure the DMA Tx/Rx channel.
  37.              (+++) Associate the initialized DMA handle to the SMARTCARD DMA Tx/Rx handle.
  38.              (+++) Configure the priority and enable the NVIC for the transfer complete
  39.                    interrupt on the DMA Tx/Rx channel.
  40.  
  41.     (#) Program the Baud Rate, Parity, Mode(Receiver/Transmitter), clock enabling/disabling and accordingly,
  42.         the clock parameters (parity, phase, last bit), prescaler value, guard time and NACK on transmission
  43.         error enabling or disabling in the hsmartcard handle Init structure.
  44.  
  45.     (#) If required, program SMARTCARD advanced features (TX/RX pins swap, TimeOut, auto-retry counter,...)
  46.         in the hsmartcard handle AdvancedInit structure.
  47.  
  48.     (#) Initialize the SMARTCARD registers by calling the HAL_SMARTCARD_Init() API:
  49.         (++) This API configures also the low level Hardware GPIO, CLOCK, CORTEX...etc)
  50.              by calling the customized HAL_SMARTCARD_MspInit() API.
  51.         [..]
  52.         (@) The specific SMARTCARD interrupts (Transmission complete interrupt,
  53.              RXNE interrupt and Error Interrupts) will be managed using the macros
  54.              __HAL_SMARTCARD_ENABLE_IT() and __HAL_SMARTCARD_DISABLE_IT() inside the transmit and receive process.
  55.  
  56.     [..]
  57.     [..] Three operation modes are available within this driver :
  58.  
  59.      *** Polling mode IO operation ***
  60.      =================================
  61.      [..]
  62.        (+) Send an amount of data in blocking mode using HAL_SMARTCARD_Transmit()
  63.        (+) Receive an amount of data in blocking mode using HAL_SMARTCARD_Receive()
  64.  
  65.      *** Interrupt mode IO operation ***
  66.      ===================================
  67.      [..]
  68.        (+) Send an amount of data in non-blocking mode using HAL_SMARTCARD_Transmit_IT()
  69.        (+) At transmission end of transfer HAL_SMARTCARD_TxCpltCallback() is executed and user can
  70.             add his own code by customization of function pointer HAL_SMARTCARD_TxCpltCallback()
  71.        (+) Receive an amount of data in non-blocking mode using HAL_SMARTCARD_Receive_IT()
  72.        (+) At reception end of transfer HAL_SMARTCARD_RxCpltCallback() is executed and user can
  73.             add his own code by customization of function pointer HAL_SMARTCARD_RxCpltCallback()
  74.        (+) In case of transfer Error, HAL_SMARTCARD_ErrorCallback() function is executed and user can
  75.             add his own code by customization of function pointer HAL_SMARTCARD_ErrorCallback()
  76.  
  77.      *** DMA mode IO operation ***
  78.      ==============================
  79.      [..]
  80.        (+) Send an amount of data in non-blocking mode (DMA) using HAL_SMARTCARD_Transmit_DMA()
  81.        (+) At transmission end of transfer HAL_SMARTCARD_TxCpltCallback() is executed and user can
  82.             add his own code by customization of function pointer HAL_SMARTCARD_TxCpltCallback()
  83.        (+) Receive an amount of data in non-blocking mode (DMA) using HAL_SMARTCARD_Receive_DMA()
  84.        (+) At reception end of transfer HAL_SMARTCARD_RxCpltCallback() is executed and user can
  85.             add his own code by customization of function pointer HAL_SMARTCARD_RxCpltCallback()
  86.        (+) In case of transfer Error, HAL_SMARTCARD_ErrorCallback() function is executed and user can
  87.             add his own code by customization of function pointer HAL_SMARTCARD_ErrorCallback()
  88.  
  89.      *** SMARTCARD HAL driver macros list ***
  90.      ========================================
  91.      [..]
  92.        Below the list of most used macros in SMARTCARD HAL driver.
  93.  
  94.        (+) __HAL_SMARTCARD_GET_FLAG : Check whether or not the specified SMARTCARD flag is set
  95.        (+) __HAL_SMARTCARD_CLEAR_FLAG : Clear the specified SMARTCARD pending flag
  96.        (+) __HAL_SMARTCARD_ENABLE_IT: Enable the specified SMARTCARD interrupt
  97.        (+) __HAL_SMARTCARD_DISABLE_IT: Disable the specified SMARTCARD interrupt
  98.        (+) __HAL_SMARTCARD_GET_IT_SOURCE: Check whether or not the specified SMARTCARD interrupt is enabled
  99.  
  100.      [..]
  101.        (@) You can refer to the SMARTCARD HAL driver header file for more useful macros
  102.  
  103.     ##### Callback registration #####
  104.     ==================================
  105.  
  106.     [..]
  107.     The compilation define USE_HAL_SMARTCARD_REGISTER_CALLBACKS when set to 1
  108.     allows the user to configure dynamically the driver callbacks.
  109.  
  110.     [..]
  111.     Use Function HAL_SMARTCARD_RegisterCallback() to register a user callback.
  112.     Function HAL_SMARTCARD_RegisterCallback() allows to register following callbacks:
  113.     (+) TxCpltCallback            : Tx Complete Callback.
  114.     (+) RxCpltCallback            : Rx Complete Callback.
  115.     (+) ErrorCallback             : Error Callback.
  116.     (+) AbortCpltCallback         : Abort Complete Callback.
  117.     (+) AbortTransmitCpltCallback : Abort Transmit Complete Callback.
  118.     (+) AbortReceiveCpltCallback  : Abort Receive Complete Callback.
  119.     (+) MspInitCallback           : SMARTCARD MspInit.
  120.     (+) MspDeInitCallback         : SMARTCARD MspDeInit.
  121.     This function takes as parameters the HAL peripheral handle, the Callback ID
  122.     and a pointer to the user callback function.
  123.  
  124.     [..]
  125.     Use function HAL_SMARTCARD_UnRegisterCallback() to reset a callback to the default
  126.     weak (surcharged) function.
  127.     HAL_SMARTCARD_UnRegisterCallback() takes as parameters the HAL peripheral handle,
  128.     and the Callback ID.
  129.     This function allows to reset following callbacks:
  130.     (+) TxCpltCallback            : Tx Complete Callback.
  131.     (+) RxCpltCallback            : Rx Complete Callback.
  132.     (+) ErrorCallback             : Error Callback.
  133.     (+) AbortCpltCallback         : Abort Complete Callback.
  134.     (+) AbortTransmitCpltCallback : Abort Transmit Complete Callback.
  135.     (+) AbortReceiveCpltCallback  : Abort Receive Complete Callback.
  136.     (+) MspInitCallback           : SMARTCARD MspInit.
  137.     (+) MspDeInitCallback         : SMARTCARD MspDeInit.
  138.  
  139.     [..]
  140.     By default, after the HAL_SMARTCARD_Init() and when the state is HAL_SMARTCARD_STATE_RESET
  141.     all callbacks are set to the corresponding weak (surcharged) functions:
  142.     examples HAL_SMARTCARD_TxCpltCallback(), HAL_SMARTCARD_RxCpltCallback().
  143.     Exception done for MspInit and MspDeInit functions that are respectively
  144.     reset to the legacy weak (surcharged) functions in the HAL_SMARTCARD_Init()
  145.     and HAL_SMARTCARD_DeInit() only when these callbacks are null (not registered beforehand).
  146.     If not, MspInit or MspDeInit are not null, the HAL_SMARTCARD_Init() and HAL_SMARTCARD_DeInit()
  147.     keep and use the user MspInit/MspDeInit callbacks (registered beforehand).
  148.  
  149.     [..]
  150.     Callbacks can be registered/unregistered in HAL_SMARTCARD_STATE_READY state only.
  151.     Exception done MspInit/MspDeInit that can be registered/unregistered
  152.     in HAL_SMARTCARD_STATE_READY or HAL_SMARTCARD_STATE_RESET state, thus registered (user)
  153.     MspInit/DeInit callbacks can be used during the Init/DeInit.
  154.     In that case first register the MspInit/MspDeInit user callbacks
  155.     using HAL_SMARTCARD_RegisterCallback() before calling HAL_SMARTCARD_DeInit()
  156.     or HAL_SMARTCARD_Init() function.
  157.  
  158.     [..]
  159.     When The compilation define USE_HAL_SMARTCARD_REGISTER_CALLBACKS is set to 0 or
  160.     not defined, the callback registration feature is not available
  161.     and weak (surcharged) callbacks are used.
  162.  
  163.  
  164.   @endverbatim
  165.   ******************************************************************************
  166.   * @attention
  167.   *
  168.   * <h2><center>&copy; Copyright (c) 2016 STMicroelectronics.
  169.   * All rights reserved.</center></h2>
  170.   *
  171.   * This software component is licensed by ST under BSD 3-Clause license,
  172.   * the "License"; You may not use this file except in compliance with the
  173.   * License. You may obtain a copy of the License at:
  174.   *                        opensource.org/licenses/BSD-3-Clause
  175.   *
  176.   ******************************************************************************
  177.   */
  178. #if !defined(STM32F030x6) && !defined(STM32F030x8) && !defined(STM32F070x6) && !defined(STM32F070xB) && !defined(STM32F030xC)
  179. /* Includes ------------------------------------------------------------------*/
  180. #include "stm32f0xx_hal.h"
  181.  
  182. /** @addtogroup STM32F0xx_HAL_Driver
  183.   * @{
  184.   */
  185.  
  186. /** @defgroup SMARTCARD SMARTCARD
  187.   * @brief HAL SMARTCARD module driver
  188.   * @{
  189.   */
  190.  
  191. #ifdef HAL_SMARTCARD_MODULE_ENABLED
  192.  
  193. /* Private typedef -----------------------------------------------------------*/
  194. /* Private define ------------------------------------------------------------*/
  195. /** @defgroup SMARTCARD_Private_Constants SMARTCARD Private Constants
  196.   * @{
  197.   */
  198. #define SMARTCARD_TEACK_REACK_TIMEOUT  1000U       /*!< SMARTCARD TX or RX enable acknowledge time-out value */
  199.  
  200. #define USART_CR1_FIELDS  ((uint32_t)(USART_CR1_M | USART_CR1_PCE | USART_CR1_PS | USART_CR1_TE | \
  201.                                         USART_CR1_RE | USART_CR1_OVER8)) /*!< USART CR1 fields of parameters set by SMARTCARD_SetConfig API */
  202.  
  203. #define USART_CR2_CLK_FIELDS  ((uint32_t)(USART_CR2_CLKEN | USART_CR2_CPOL | \
  204.                                           USART_CR2_CPHA | USART_CR2_LBCL)) /*!< SMARTCARD clock-related USART CR2 fields of parameters */
  205.  
  206. #define USART_CR2_FIELDS  ((uint32_t)(USART_CR2_RTOEN | USART_CR2_CLK_FIELDS | \
  207.                                       USART_CR2_STOP)) /*!< USART CR2 fields of parameters set by SMARTCARD_SetConfig API */
  208.  
  209. #define USART_CR3_FIELDS  ((uint32_t)(USART_CR3_ONEBIT | USART_CR3_NACK | \
  210.                                       USART_CR3_SCARCNT)) /*!< USART CR3 fields of parameters set by SMARTCARD_SetConfig API */
  211.  
  212. #define USART_BRR_MIN  0x10U        /*!< USART BRR minimum authorized value */
  213.  
  214. #define USART_BRR_MAX  0x0000FFFFU  /*!< USART BRR maximum authorized value */
  215. /**
  216.   * @}
  217.   */
  218.  
  219. /* Private macros ------------------------------------------------------------*/
  220. /* Private variables ---------------------------------------------------------*/
  221. /* Private function prototypes -----------------------------------------------*/
  222. /** @addtogroup SMARTCARD_Private_Functions
  223.   * @{
  224.   */
  225. #if (USE_HAL_SMARTCARD_REGISTER_CALLBACKS == 1)
  226. void SMARTCARD_InitCallbacksToDefault(SMARTCARD_HandleTypeDef *hsmartcard);
  227. #endif /* USE_HAL_SMARTCARD_REGISTER_CALLBACKS */
  228. static HAL_StatusTypeDef SMARTCARD_SetConfig(SMARTCARD_HandleTypeDef *hsmartcard);
  229. static void SMARTCARD_AdvFeatureConfig(SMARTCARD_HandleTypeDef *hsmartcard);
  230. static HAL_StatusTypeDef SMARTCARD_CheckIdleState(SMARTCARD_HandleTypeDef *hsmartcard);
  231. static HAL_StatusTypeDef SMARTCARD_WaitOnFlagUntilTimeout(SMARTCARD_HandleTypeDef *hsmartcard, uint32_t Flag,
  232.                                                           FlagStatus Status, uint32_t Tickstart, uint32_t Timeout);
  233. static void SMARTCARD_EndTxTransfer(SMARTCARD_HandleTypeDef *hsmartcard);
  234. static void SMARTCARD_EndRxTransfer(SMARTCARD_HandleTypeDef *hsmartcard);
  235. static void SMARTCARD_DMATransmitCplt(DMA_HandleTypeDef *hdma);
  236. static void SMARTCARD_DMAReceiveCplt(DMA_HandleTypeDef *hdma);
  237. static void SMARTCARD_DMAError(DMA_HandleTypeDef *hdma);
  238. static void SMARTCARD_DMAAbortOnError(DMA_HandleTypeDef *hdma);
  239. static void SMARTCARD_DMATxAbortCallback(DMA_HandleTypeDef *hdma);
  240. static void SMARTCARD_DMARxAbortCallback(DMA_HandleTypeDef *hdma);
  241. static void SMARTCARD_DMATxOnlyAbortCallback(DMA_HandleTypeDef *hdma);
  242. static void SMARTCARD_DMARxOnlyAbortCallback(DMA_HandleTypeDef *hdma);
  243. static void SMARTCARD_TxISR(SMARTCARD_HandleTypeDef *hsmartcard);
  244. static void SMARTCARD_EndTransmit_IT(SMARTCARD_HandleTypeDef *hsmartcard);
  245. static void SMARTCARD_RxISR(SMARTCARD_HandleTypeDef *hsmartcard);
  246. /**
  247.   * @}
  248.   */
  249.  
  250. /* Exported functions --------------------------------------------------------*/
  251.  
  252. /** @defgroup SMARTCARD_Exported_Functions SMARTCARD Exported Functions
  253.   * @{
  254.   */
  255.  
  256. /** @defgroup SMARTCARD_Exported_Functions_Group1 Initialization and de-initialization functions
  257.   * @brief    Initialization and Configuration functions
  258.   *
  259. @verbatim
  260.   ==============================================================================
  261.               ##### Initialization and Configuration functions #####
  262.   ==============================================================================
  263.   [..]
  264.   This subsection provides a set of functions allowing to initialize the USARTx
  265.   associated to the SmartCard.
  266.   (+) These parameters can be configured:
  267.       (++) Baud Rate
  268.       (++) Parity: parity should be enabled, frame Length is fixed to 8 bits plus parity
  269.       (++) Receiver/transmitter modes
  270.       (++) Synchronous mode (and if enabled, phase, polarity and last bit parameters)
  271.       (++) Prescaler value
  272.       (++) Guard bit time
  273.       (++) NACK enabling or disabling on transmission error
  274.  
  275.   (+) The following advanced features can be configured as well:
  276.       (++) TX and/or RX pin level inversion
  277.       (++) data logical level inversion
  278.       (++) RX and TX pins swap
  279.       (++) RX overrun detection disabling
  280.       (++) DMA disabling on RX error
  281.       (++) MSB first on communication line
  282.       (++) Time out enabling (and if activated, timeout value)
  283.       (++) Block length
  284.       (++) Auto-retry counter
  285.   [..]
  286.   The HAL_SMARTCARD_Init() API follows the USART synchronous configuration procedures
  287.   (details for the procedures are available in reference manual).
  288.  
  289. @endverbatim
  290.  
  291.   The USART frame format is given in the following table:
  292.  
  293.     Table 1. USART frame format.
  294.     +---------------------------------------------------------------+
  295.     | M1M0 bits |  PCE bit  |            USART frame                |
  296.     |-----------------------|---------------------------------------|
  297.     |     01    |    1      |    | SB | 8 bit data | PB | STB |     |
  298.     +---------------------------------------------------------------+
  299.  
  300.  
  301.   * @{
  302.   */
  303.  
  304. /**
  305.   * @brief  Initialize the SMARTCARD mode according to the specified
  306.   *         parameters in the SMARTCARD_HandleTypeDef and initialize the associated handle.
  307.   * @param  hsmartcard Pointer to a SMARTCARD_HandleTypeDef structure that contains
  308.   *                    the configuration information for the specified SMARTCARD module.
  309.   * @retval HAL status
  310.   */
  311. HAL_StatusTypeDef HAL_SMARTCARD_Init(SMARTCARD_HandleTypeDef *hsmartcard)
  312. {
  313.   /* Check the SMARTCARD handle allocation */
  314.   if (hsmartcard == NULL)
  315.   {
  316.     return HAL_ERROR;
  317.   }
  318.  
  319.   /* Check the USART associated to the SMARTCARD handle */
  320.   assert_param(IS_SMARTCARD_INSTANCE(hsmartcard->Instance));
  321.  
  322.   if (hsmartcard->gState == HAL_SMARTCARD_STATE_RESET)
  323.   {
  324.     /* Allocate lock resource and initialize it */
  325.     hsmartcard->Lock = HAL_UNLOCKED;
  326.  
  327. #if USE_HAL_SMARTCARD_REGISTER_CALLBACKS == 1
  328.     SMARTCARD_InitCallbacksToDefault(hsmartcard);
  329.  
  330.     if (hsmartcard->MspInitCallback == NULL)
  331.     {
  332.       hsmartcard->MspInitCallback = HAL_SMARTCARD_MspInit;
  333.     }
  334.  
  335.     /* Init the low level hardware */
  336.     hsmartcard->MspInitCallback(hsmartcard);
  337. #else
  338.     /* Init the low level hardware : GPIO, CLOCK */
  339.     HAL_SMARTCARD_MspInit(hsmartcard);
  340. #endif /* USE_HAL_SMARTCARD_REGISTER_CALLBACKS */
  341.   }
  342.  
  343.   hsmartcard->gState = HAL_SMARTCARD_STATE_BUSY;
  344.  
  345.   /* Disable the Peripheral to set smartcard mode */
  346.   CLEAR_BIT(hsmartcard->Instance->CR1, USART_CR1_UE);
  347.  
  348.   /* In SmartCard mode, the following bits must be kept cleared:
  349.   - LINEN in the USART_CR2 register,
  350.   - HDSEL and IREN  bits in the USART_CR3 register.*/
  351.   CLEAR_BIT(hsmartcard->Instance->CR2, USART_CR2_LINEN);
  352.   CLEAR_BIT(hsmartcard->Instance->CR3, (USART_CR3_HDSEL | USART_CR3_IREN));
  353.  
  354.   /* set the USART in SMARTCARD mode */
  355.   SET_BIT(hsmartcard->Instance->CR3, USART_CR3_SCEN);
  356.  
  357.   /* Set the SMARTCARD Communication parameters */
  358.   if (SMARTCARD_SetConfig(hsmartcard) == HAL_ERROR)
  359.   {
  360.     return HAL_ERROR;
  361.   }
  362.  
  363.   /* Set the SMARTCARD transmission completion indication */
  364.   SMARTCARD_TRANSMISSION_COMPLETION_SETTING(hsmartcard);
  365.  
  366.   if (hsmartcard->AdvancedInit.AdvFeatureInit != SMARTCARD_ADVFEATURE_NO_INIT)
  367.   {
  368.     SMARTCARD_AdvFeatureConfig(hsmartcard);
  369.   }
  370.  
  371.   /* Enable the Peripheral */
  372.   SET_BIT(hsmartcard->Instance->CR1, USART_CR1_UE);
  373.  
  374.   /* TEACK and/or REACK to check before moving hsmartcard->gState and hsmartcard->RxState to Ready */
  375.   return (SMARTCARD_CheckIdleState(hsmartcard));
  376. }
  377.  
  378. /**
  379.   * @brief  DeInitialize the SMARTCARD peripheral.
  380.   * @param  hsmartcard Pointer to a SMARTCARD_HandleTypeDef structure that contains
  381.   *                    the configuration information for the specified SMARTCARD module.
  382.   * @retval HAL status
  383.   */
  384. HAL_StatusTypeDef HAL_SMARTCARD_DeInit(SMARTCARD_HandleTypeDef *hsmartcard)
  385. {
  386.   /* Check the SMARTCARD handle allocation */
  387.   if (hsmartcard == NULL)
  388.   {
  389.     return HAL_ERROR;
  390.   }
  391.  
  392.   /* Check the USART/UART associated to the SMARTCARD handle */
  393.   assert_param(IS_SMARTCARD_INSTANCE(hsmartcard->Instance));
  394.  
  395.   hsmartcard->gState = HAL_SMARTCARD_STATE_BUSY;
  396.  
  397.   /* Disable the Peripheral */
  398.   CLEAR_BIT(hsmartcard->Instance->CR1, USART_CR1_UE);
  399.  
  400.   WRITE_REG(hsmartcard->Instance->CR1, 0x0U);
  401.   WRITE_REG(hsmartcard->Instance->CR2, 0x0U);
  402.   WRITE_REG(hsmartcard->Instance->CR3, 0x0U);
  403.   WRITE_REG(hsmartcard->Instance->RTOR, 0x0U);
  404.   WRITE_REG(hsmartcard->Instance->GTPR, 0x0U);
  405.  
  406.   /* DeInit the low level hardware */
  407. #if USE_HAL_SMARTCARD_REGISTER_CALLBACKS == 1
  408.   if (hsmartcard->MspDeInitCallback == NULL)
  409.   {
  410.     hsmartcard->MspDeInitCallback = HAL_SMARTCARD_MspDeInit;
  411.   }
  412.   /* DeInit the low level hardware */
  413.   hsmartcard->MspDeInitCallback(hsmartcard);
  414. #else
  415.   HAL_SMARTCARD_MspDeInit(hsmartcard);
  416. #endif /* USE_HAL_SMARTCARD_REGISTER_CALLBACKS */
  417.  
  418.   hsmartcard->ErrorCode = HAL_SMARTCARD_ERROR_NONE;
  419.   hsmartcard->gState    = HAL_SMARTCARD_STATE_RESET;
  420.   hsmartcard->RxState   = HAL_SMARTCARD_STATE_RESET;
  421.  
  422.   /* Process Unlock */
  423.   __HAL_UNLOCK(hsmartcard);
  424.  
  425.   return HAL_OK;
  426. }
  427.  
  428. /**
  429.   * @brief  Initialize the SMARTCARD MSP.
  430.   * @param  hsmartcard Pointer to a SMARTCARD_HandleTypeDef structure that contains
  431.   *                    the configuration information for the specified SMARTCARD module.
  432.   * @retval None
  433.   */
  434. __weak void HAL_SMARTCARD_MspInit(SMARTCARD_HandleTypeDef *hsmartcard)
  435. {
  436.   /* Prevent unused argument(s) compilation warning */
  437.   UNUSED(hsmartcard);
  438.  
  439.   /* NOTE : This function should not be modified, when the callback is needed,
  440.             the HAL_SMARTCARD_MspInit can be implemented in the user file
  441.    */
  442. }
  443.  
  444. /**
  445.   * @brief  DeInitialize the SMARTCARD MSP.
  446.   * @param  hsmartcard Pointer to a SMARTCARD_HandleTypeDef structure that contains
  447.   *                    the configuration information for the specified SMARTCARD module.
  448.   * @retval None
  449.   */
  450. __weak void HAL_SMARTCARD_MspDeInit(SMARTCARD_HandleTypeDef *hsmartcard)
  451. {
  452.   /* Prevent unused argument(s) compilation warning */
  453.   UNUSED(hsmartcard);
  454.  
  455.   /* NOTE : This function should not be modified, when the callback is needed,
  456.             the HAL_SMARTCARD_MspDeInit can be implemented in the user file
  457.    */
  458. }
  459.  
  460. #if (USE_HAL_SMARTCARD_REGISTER_CALLBACKS == 1)
  461. /**
  462.   * @brief  Register a User SMARTCARD Callback
  463.   *         To be used instead of the weak predefined callback
  464.   * @param  hsmartcard smartcard handle
  465.   * @param  CallbackID ID of the callback to be registered
  466.   *         This parameter can be one of the following values:
  467.   *           @arg @ref HAL_SMARTCARD_TX_COMPLETE_CB_ID Tx Complete Callback ID
  468.   *           @arg @ref HAL_SMARTCARD_RX_COMPLETE_CB_ID Rx Complete Callback ID
  469.   *           @arg @ref HAL_SMARTCARD_ERROR_CB_ID Error Callback ID
  470.   *           @arg @ref HAL_SMARTCARD_ABORT_COMPLETE_CB_ID Abort Complete Callback ID
  471.   *           @arg @ref HAL_SMARTCARD_ABORT_TRANSMIT_COMPLETE_CB_ID Abort Transmit Complete Callback ID
  472.   *           @arg @ref HAL_SMARTCARD_ABORT_RECEIVE_COMPLETE_CB_ID Abort Receive Complete Callback ID
  473.   *           @arg @ref HAL_SMARTCARD_MSPINIT_CB_ID MspInit Callback ID
  474.   *           @arg @ref HAL_SMARTCARD_MSPDEINIT_CB_ID MspDeInit Callback ID
  475.   * @param  pCallback pointer to the Callback function
  476.   * @retval HAL status
  477.   */
  478. HAL_StatusTypeDef HAL_SMARTCARD_RegisterCallback(SMARTCARD_HandleTypeDef *hsmartcard,
  479.                                                  HAL_SMARTCARD_CallbackIDTypeDef CallbackID,
  480.                                                  pSMARTCARD_CallbackTypeDef pCallback)
  481. {
  482.   HAL_StatusTypeDef status = HAL_OK;
  483.  
  484.   if (pCallback == NULL)
  485.   {
  486.     /* Update the error code */
  487.     hsmartcard->ErrorCode |= HAL_SMARTCARD_ERROR_INVALID_CALLBACK;
  488.  
  489.     return HAL_ERROR;
  490.   }
  491.   /* Process locked */
  492.   __HAL_LOCK(hsmartcard);
  493.  
  494.   if (hsmartcard->gState == HAL_SMARTCARD_STATE_READY)
  495.   {
  496.     switch (CallbackID)
  497.     {
  498.  
  499.       case HAL_SMARTCARD_TX_COMPLETE_CB_ID :
  500.         hsmartcard->TxCpltCallback = pCallback;
  501.         break;
  502.  
  503.       case HAL_SMARTCARD_RX_COMPLETE_CB_ID :
  504.         hsmartcard->RxCpltCallback = pCallback;
  505.         break;
  506.  
  507.       case HAL_SMARTCARD_ERROR_CB_ID :
  508.         hsmartcard->ErrorCallback = pCallback;
  509.         break;
  510.  
  511.       case HAL_SMARTCARD_ABORT_COMPLETE_CB_ID :
  512.         hsmartcard->AbortCpltCallback = pCallback;
  513.         break;
  514.  
  515.       case HAL_SMARTCARD_ABORT_TRANSMIT_COMPLETE_CB_ID :
  516.         hsmartcard->AbortTransmitCpltCallback = pCallback;
  517.         break;
  518.  
  519.       case HAL_SMARTCARD_ABORT_RECEIVE_COMPLETE_CB_ID :
  520.         hsmartcard->AbortReceiveCpltCallback = pCallback;
  521.         break;
  522.  
  523.  
  524.       case HAL_SMARTCARD_MSPINIT_CB_ID :
  525.         hsmartcard->MspInitCallback = pCallback;
  526.         break;
  527.  
  528.       case HAL_SMARTCARD_MSPDEINIT_CB_ID :
  529.         hsmartcard->MspDeInitCallback = pCallback;
  530.         break;
  531.  
  532.       default :
  533.         /* Update the error code */
  534.         hsmartcard->ErrorCode |= HAL_SMARTCARD_ERROR_INVALID_CALLBACK;
  535.  
  536.         /* Return error status */
  537.         status =  HAL_ERROR;
  538.         break;
  539.     }
  540.   }
  541.   else if (hsmartcard->gState == HAL_SMARTCARD_STATE_RESET)
  542.   {
  543.     switch (CallbackID)
  544.     {
  545.       case HAL_SMARTCARD_MSPINIT_CB_ID :
  546.         hsmartcard->MspInitCallback = pCallback;
  547.         break;
  548.  
  549.       case HAL_SMARTCARD_MSPDEINIT_CB_ID :
  550.         hsmartcard->MspDeInitCallback = pCallback;
  551.         break;
  552.  
  553.       default :
  554.         /* Update the error code */
  555.         hsmartcard->ErrorCode |= HAL_SMARTCARD_ERROR_INVALID_CALLBACK;
  556.  
  557.         /* Return error status */
  558.         status =  HAL_ERROR;
  559.         break;
  560.     }
  561.   }
  562.   else
  563.   {
  564.     /* Update the error code */
  565.     hsmartcard->ErrorCode |= HAL_SMARTCARD_ERROR_INVALID_CALLBACK;
  566.  
  567.     /* Return error status */
  568.     status =  HAL_ERROR;
  569.   }
  570.  
  571.   /* Release Lock */
  572.   __HAL_UNLOCK(hsmartcard);
  573.  
  574.   return status;
  575. }
  576.  
  577. /**
  578.   * @brief  Unregister an SMARTCARD callback
  579.   *         SMARTCARD callback is redirected to the weak predefined callback
  580.   * @param  hsmartcard smartcard handle
  581.   * @param  CallbackID ID of the callback to be unregistered
  582.   *         This parameter can be one of the following values:
  583.   *           @arg @ref HAL_SMARTCARD_TX_COMPLETE_CB_ID Tx Complete Callback ID
  584.   *           @arg @ref HAL_SMARTCARD_RX_COMPLETE_CB_ID Rx Complete Callback ID
  585.   *           @arg @ref HAL_SMARTCARD_ERROR_CB_ID Error Callback ID
  586.   *           @arg @ref HAL_SMARTCARD_ABORT_COMPLETE_CB_ID Abort Complete Callback ID
  587.   *           @arg @ref HAL_SMARTCARD_ABORT_TRANSMIT_COMPLETE_CB_ID Abort Transmit Complete Callback ID
  588.   *           @arg @ref HAL_SMARTCARD_ABORT_RECEIVE_COMPLETE_CB_ID Abort Receive Complete Callback ID
  589.   *           @arg @ref HAL_SMARTCARD_MSPINIT_CB_ID MspInit Callback ID
  590.   *           @arg @ref HAL_SMARTCARD_MSPDEINIT_CB_ID MspDeInit Callback ID
  591.   * @retval HAL status
  592.   */
  593. HAL_StatusTypeDef HAL_SMARTCARD_UnRegisterCallback(SMARTCARD_HandleTypeDef *hsmartcard,
  594.                                                    HAL_SMARTCARD_CallbackIDTypeDef CallbackID)
  595. {
  596.   HAL_StatusTypeDef status = HAL_OK;
  597.  
  598.   /* Process locked */
  599.   __HAL_LOCK(hsmartcard);
  600.  
  601.   if (HAL_SMARTCARD_STATE_READY == hsmartcard->gState)
  602.   {
  603.     switch (CallbackID)
  604.     {
  605.       case HAL_SMARTCARD_TX_COMPLETE_CB_ID :
  606.         hsmartcard->TxCpltCallback = HAL_SMARTCARD_TxCpltCallback;                 /* Legacy weak TxCpltCallback */
  607.         break;
  608.  
  609.       case HAL_SMARTCARD_RX_COMPLETE_CB_ID :
  610.         hsmartcard->RxCpltCallback = HAL_SMARTCARD_RxCpltCallback;                 /* Legacy weak RxCpltCallback */
  611.         break;
  612.  
  613.       case HAL_SMARTCARD_ERROR_CB_ID :
  614.         hsmartcard->ErrorCallback = HAL_SMARTCARD_ErrorCallback;                   /* Legacy weak ErrorCallback  */
  615.         break;
  616.  
  617.       case HAL_SMARTCARD_ABORT_COMPLETE_CB_ID :
  618.         hsmartcard->AbortCpltCallback = HAL_SMARTCARD_AbortCpltCallback;           /* Legacy weak AbortCpltCallback */
  619.         break;
  620.  
  621.       case HAL_SMARTCARD_ABORT_TRANSMIT_COMPLETE_CB_ID :
  622.         hsmartcard->AbortTransmitCpltCallback = HAL_SMARTCARD_AbortTransmitCpltCallback; /* Legacy weak
  623.                                                                                             AbortTransmitCpltCallback*/
  624.         break;
  625.  
  626.       case HAL_SMARTCARD_ABORT_RECEIVE_COMPLETE_CB_ID :
  627.         hsmartcard->AbortReceiveCpltCallback = HAL_SMARTCARD_AbortReceiveCpltCallback;  /* Legacy weak
  628.                                                                                            AbortReceiveCpltCallback */
  629.         break;
  630.  
  631.  
  632.       case HAL_SMARTCARD_MSPINIT_CB_ID :
  633.         hsmartcard->MspInitCallback = HAL_SMARTCARD_MspInit;                       /* Legacy weak MspInitCallback  */
  634.         break;
  635.  
  636.       case HAL_SMARTCARD_MSPDEINIT_CB_ID :
  637.         hsmartcard->MspDeInitCallback = HAL_SMARTCARD_MspDeInit;                   /* Legacy weak MspDeInitCallback */
  638.         break;
  639.  
  640.       default :
  641.         /* Update the error code */
  642.         hsmartcard->ErrorCode |= HAL_SMARTCARD_ERROR_INVALID_CALLBACK;
  643.  
  644.         /* Return error status */
  645.         status =  HAL_ERROR;
  646.         break;
  647.     }
  648.   }
  649.   else if (HAL_SMARTCARD_STATE_RESET == hsmartcard->gState)
  650.   {
  651.     switch (CallbackID)
  652.     {
  653.       case HAL_SMARTCARD_MSPINIT_CB_ID :
  654.         hsmartcard->MspInitCallback = HAL_SMARTCARD_MspInit;
  655.         break;
  656.  
  657.       case HAL_SMARTCARD_MSPDEINIT_CB_ID :
  658.         hsmartcard->MspDeInitCallback = HAL_SMARTCARD_MspDeInit;
  659.         break;
  660.  
  661.       default :
  662.         /* Update the error code */
  663.         hsmartcard->ErrorCode |= HAL_SMARTCARD_ERROR_INVALID_CALLBACK;
  664.  
  665.         /* Return error status */
  666.         status =  HAL_ERROR;
  667.         break;
  668.     }
  669.   }
  670.   else
  671.   {
  672.     /* Update the error code */
  673.     hsmartcard->ErrorCode |= HAL_SMARTCARD_ERROR_INVALID_CALLBACK;
  674.  
  675.     /* Return error status */
  676.     status =  HAL_ERROR;
  677.   }
  678.  
  679.   /* Release Lock */
  680.   __HAL_UNLOCK(hsmartcard);
  681.  
  682.   return status;
  683. }
  684. #endif /* USE_HAL_SMARTCARD_REGISTER_CALLBACKS */
  685.  
  686. /**
  687.   * @}
  688.   */
  689.  
  690. /** @defgroup SMARTCARD_Exported_Functions_Group2 IO operation functions
  691.   * @brief    SMARTCARD Transmit and Receive functions
  692.   *
  693. @verbatim
  694.   ==============================================================================
  695.                          ##### IO operation functions #####
  696.   ==============================================================================
  697.   [..]
  698.     This subsection provides a set of functions allowing to manage the SMARTCARD data transfers.
  699.  
  700.   [..]
  701.     Smartcard is a single wire half duplex communication protocol.
  702.     The Smartcard interface is designed to support asynchronous protocol Smartcards as
  703.     defined in the ISO 7816-3 standard. The USART should be configured as:
  704.     (+) 8 bits plus parity: where M=1 and PCE=1 in the USART_CR1 register
  705.     (+) 1.5 stop bits when transmitting and receiving: where STOP=11 in the USART_CR2 register.
  706.  
  707.   [..]
  708.     (#) There are two modes of transfer:
  709.         (##) Blocking mode: The communication is performed in polling mode.
  710.              The HAL status of all data processing is returned by the same function
  711.              after finishing transfer.
  712.         (##) Non-Blocking mode: The communication is performed using Interrupts
  713.              or DMA, the relevant API's return the HAL status.
  714.              The end of the data processing will be indicated through the
  715.              dedicated SMARTCARD IRQ when using Interrupt mode or the DMA IRQ when
  716.              using DMA mode.
  717.         (##) The HAL_SMARTCARD_TxCpltCallback(), HAL_SMARTCARD_RxCpltCallback() user callbacks
  718.              will be executed respectively at the end of the Transmit or Receive process
  719.              The HAL_SMARTCARD_ErrorCallback() user callback will be executed when a communication
  720.              error is detected.
  721.  
  722.     (#) Blocking mode APIs are :
  723.         (##) HAL_SMARTCARD_Transmit()
  724.         (##) HAL_SMARTCARD_Receive()
  725.  
  726.     (#) Non Blocking mode APIs with Interrupt are :
  727.         (##) HAL_SMARTCARD_Transmit_IT()
  728.         (##) HAL_SMARTCARD_Receive_IT()
  729.         (##) HAL_SMARTCARD_IRQHandler()
  730.  
  731.     (#) Non Blocking mode functions with DMA are :
  732.         (##) HAL_SMARTCARD_Transmit_DMA()
  733.         (##) HAL_SMARTCARD_Receive_DMA()
  734.  
  735.     (#) A set of Transfer Complete Callbacks are provided in non Blocking mode:
  736.         (##) HAL_SMARTCARD_TxCpltCallback()
  737.         (##) HAL_SMARTCARD_RxCpltCallback()
  738.         (##) HAL_SMARTCARD_ErrorCallback()
  739.  
  740.   [..]
  741.     (#) Non-Blocking mode transfers could be aborted using Abort API's :
  742.         (##) HAL_SMARTCARD_Abort()
  743.         (##) HAL_SMARTCARD_AbortTransmit()
  744.         (##) HAL_SMARTCARD_AbortReceive()
  745.         (##) HAL_SMARTCARD_Abort_IT()
  746.         (##) HAL_SMARTCARD_AbortTransmit_IT()
  747.         (##) HAL_SMARTCARD_AbortReceive_IT()
  748.  
  749.     (#) For Abort services based on interrupts (HAL_SMARTCARD_Abortxxx_IT),
  750.         a set of Abort Complete Callbacks are provided:
  751.         (##) HAL_SMARTCARD_AbortCpltCallback()
  752.         (##) HAL_SMARTCARD_AbortTransmitCpltCallback()
  753.         (##) HAL_SMARTCARD_AbortReceiveCpltCallback()
  754.  
  755.     (#) In Non-Blocking mode transfers, possible errors are split into 2 categories.
  756.         Errors are handled as follows :
  757.        (##) Error is considered as Recoverable and non blocking : Transfer could go till end, but error severity is
  758.            to be evaluated by user : this concerns Frame Error,
  759.            Parity Error or Noise Error in Interrupt mode reception .
  760.            Received character is then retrieved and stored in Rx buffer,
  761.            Error code is set to allow user to identify error type,
  762.            and HAL_SMARTCARD_ErrorCallback() user callback is executed. Transfer is kept ongoing on SMARTCARD side.
  763.            If user wants to abort it, Abort services should be called by user.
  764.        (##) Error is considered as Blocking : Transfer could not be completed properly and is aborted.
  765.            This concerns Frame Error in Interrupt mode transmission, Overrun Error in Interrupt
  766.            mode reception and all errors in DMA mode.
  767.            Error code is set to allow user to identify error type,
  768.            and HAL_SMARTCARD_ErrorCallback() user callback is executed.
  769.  
  770. @endverbatim
  771.   * @{
  772.   */
  773.  
  774. /**
  775.   * @brief  Send an amount of data in blocking mode.
  776.   * @param  hsmartcard Pointer to a SMARTCARD_HandleTypeDef structure that contains
  777.   *                    the configuration information for the specified SMARTCARD module.
  778.   * @param  pData pointer to data buffer.
  779.   * @param  Size amount of data to be sent.
  780.   * @param  Timeout  Timeout duration.
  781.   * @retval HAL status
  782.   */
  783. HAL_StatusTypeDef HAL_SMARTCARD_Transmit(SMARTCARD_HandleTypeDef *hsmartcard, uint8_t *pData, uint16_t Size,
  784.                                          uint32_t Timeout)
  785. {
  786.   uint32_t tickstart;
  787.   uint8_t  *ptmpdata = pData;
  788.  
  789.   /* Check that a Tx process is not already ongoing */
  790.   if (hsmartcard->gState == HAL_SMARTCARD_STATE_READY)
  791.   {
  792.     if ((ptmpdata == NULL) || (Size == 0U))
  793.     {
  794.       return  HAL_ERROR;
  795.     }
  796.  
  797.     /* Process Locked */
  798.     __HAL_LOCK(hsmartcard);
  799.  
  800.     hsmartcard->gState = HAL_SMARTCARD_STATE_BUSY_TX;
  801.  
  802.     /* Init tickstart for timeout management */
  803.     tickstart = HAL_GetTick();
  804.  
  805.     /* Disable the Peripheral first to update mode for TX master */
  806.     CLEAR_BIT(hsmartcard->Instance->CR1, USART_CR1_UE);
  807.  
  808.     /* In case of TX only mode, if NACK is enabled, the USART must be able to monitor
  809.        the bidirectional line to detect a NACK signal in case of parity error.
  810.        Therefore, the receiver block must be enabled as well (RE bit must be set). */
  811.     if ((hsmartcard->Init.Mode == SMARTCARD_MODE_TX)
  812.         && (hsmartcard->Init.NACKEnable == SMARTCARD_NACK_ENABLE))
  813.     {
  814.       SET_BIT(hsmartcard->Instance->CR1, USART_CR1_RE);
  815.     }
  816.     /* Enable Tx */
  817.     SET_BIT(hsmartcard->Instance->CR1, USART_CR1_TE);
  818.  
  819.     /* Enable the Peripheral */
  820.     SET_BIT(hsmartcard->Instance->CR1, USART_CR1_UE);
  821.  
  822.     /* Perform a TX/RX FIFO Flush */
  823.     __HAL_SMARTCARD_FLUSH_DRREGISTER(hsmartcard);
  824.  
  825.     hsmartcard->ErrorCode = HAL_SMARTCARD_ERROR_NONE;
  826.     hsmartcard->TxXferSize = Size;
  827.     hsmartcard->TxXferCount = Size;
  828.  
  829.     while (hsmartcard->TxXferCount > 0U)
  830.     {
  831.       hsmartcard->TxXferCount--;
  832.       if (SMARTCARD_WaitOnFlagUntilTimeout(hsmartcard, SMARTCARD_FLAG_TXE, RESET, tickstart, Timeout) != HAL_OK)
  833.       {
  834.         return HAL_TIMEOUT;
  835.       }
  836.       hsmartcard->Instance->TDR = (uint8_t)(*ptmpdata & 0xFFU);
  837.       ptmpdata++;
  838.     }
  839.     if (SMARTCARD_WaitOnFlagUntilTimeout(hsmartcard, SMARTCARD_TRANSMISSION_COMPLETION_FLAG(hsmartcard), RESET,
  840.                                          tickstart, Timeout) != HAL_OK)
  841.     {
  842.       return HAL_TIMEOUT;
  843.     }
  844.  
  845.     /* Disable the Peripheral first to update mode */
  846.     CLEAR_BIT(hsmartcard->Instance->CR1, USART_CR1_UE);
  847.     if ((hsmartcard->Init.Mode == SMARTCARD_MODE_TX)
  848.         && (hsmartcard->Init.NACKEnable == SMARTCARD_NACK_ENABLE))
  849.     {
  850.       /* In case of TX only mode, if NACK is enabled, receiver block has been enabled
  851.          for Transmit phase. Disable this receiver block. */
  852.       CLEAR_BIT(hsmartcard->Instance->CR1, USART_CR1_RE);
  853.     }
  854.     if ((hsmartcard->Init.Mode == SMARTCARD_MODE_TX_RX)
  855.         || (hsmartcard->Init.NACKEnable == SMARTCARD_NACK_ENABLE))
  856.     {
  857.       /* Perform a TX FIFO Flush at end of Tx phase, as all sent bytes are appearing in Rx Data register */
  858.       __HAL_SMARTCARD_FLUSH_DRREGISTER(hsmartcard);
  859.     }
  860.     SET_BIT(hsmartcard->Instance->CR1, USART_CR1_UE);
  861.  
  862.     /* At end of Tx process, restore hsmartcard->gState to Ready */
  863.     hsmartcard->gState = HAL_SMARTCARD_STATE_READY;
  864.  
  865.     /* Process Unlocked */
  866.     __HAL_UNLOCK(hsmartcard);
  867.  
  868.     return HAL_OK;
  869.   }
  870.   else
  871.   {
  872.     return HAL_BUSY;
  873.   }
  874. }
  875.  
  876. /**
  877.   * @brief  Receive an amount of data in blocking mode.
  878.   * @param  hsmartcard Pointer to a SMARTCARD_HandleTypeDef structure that contains
  879.   *                    the configuration information for the specified SMARTCARD module.
  880.   * @param  pData pointer to data buffer.
  881.   * @param  Size amount of data to be received.
  882.   * @param  Timeout Timeout duration.
  883.   * @retval HAL status
  884.   */
  885. HAL_StatusTypeDef HAL_SMARTCARD_Receive(SMARTCARD_HandleTypeDef *hsmartcard, uint8_t *pData, uint16_t Size,
  886.                                         uint32_t Timeout)
  887. {
  888.   uint32_t tickstart;
  889.   uint8_t  *ptmpdata = pData;
  890.  
  891.   /* Check that a Rx process is not already ongoing */
  892.   if (hsmartcard->RxState == HAL_SMARTCARD_STATE_READY)
  893.   {
  894.     if ((ptmpdata == NULL) || (Size == 0U))
  895.     {
  896.       return  HAL_ERROR;
  897.     }
  898.  
  899.     /* Process Locked */
  900.     __HAL_LOCK(hsmartcard);
  901.  
  902.     hsmartcard->ErrorCode = HAL_SMARTCARD_ERROR_NONE;
  903.     hsmartcard->RxState   = HAL_SMARTCARD_STATE_BUSY_RX;
  904.  
  905.     /* Init tickstart for timeout management */
  906.     tickstart = HAL_GetTick();
  907.  
  908.     hsmartcard->RxXferSize = Size;
  909.     hsmartcard->RxXferCount = Size;
  910.  
  911.     /* Check the remain data to be received */
  912.     while (hsmartcard->RxXferCount > 0U)
  913.     {
  914.       hsmartcard->RxXferCount--;
  915.  
  916.       if (SMARTCARD_WaitOnFlagUntilTimeout(hsmartcard, SMARTCARD_FLAG_RXNE, RESET, tickstart, Timeout) != HAL_OK)
  917.       {
  918.         return HAL_TIMEOUT;
  919.       }
  920.       *ptmpdata = (uint8_t)(hsmartcard->Instance->RDR & (uint8_t)0x00FF);
  921.       ptmpdata++;
  922.     }
  923.  
  924.     /* At end of Rx process, restore hsmartcard->RxState to Ready */
  925.     hsmartcard->RxState = HAL_SMARTCARD_STATE_READY;
  926.  
  927.     /* Process Unlocked */
  928.     __HAL_UNLOCK(hsmartcard);
  929.  
  930.     return HAL_OK;
  931.   }
  932.   else
  933.   {
  934.     return HAL_BUSY;
  935.   }
  936. }
  937.  
  938. /**
  939.   * @brief  Send an amount of data in interrupt mode.
  940.   * @param  hsmartcard Pointer to a SMARTCARD_HandleTypeDef structure that contains
  941.   *                    the configuration information for the specified SMARTCARD module.
  942.   * @param  pData pointer to data buffer.
  943.   * @param  Size amount of data to be sent.
  944.   * @retval HAL status
  945.   */
  946. HAL_StatusTypeDef HAL_SMARTCARD_Transmit_IT(SMARTCARD_HandleTypeDef *hsmartcard, uint8_t *pData, uint16_t Size)
  947. {
  948.   /* Check that a Tx process is not already ongoing */
  949.   if (hsmartcard->gState == HAL_SMARTCARD_STATE_READY)
  950.   {
  951.     if ((pData == NULL) || (Size == 0U))
  952.     {
  953.       return HAL_ERROR;
  954.     }
  955.  
  956.     /* Process Locked */
  957.     __HAL_LOCK(hsmartcard);
  958.  
  959.     hsmartcard->ErrorCode = HAL_SMARTCARD_ERROR_NONE;
  960.     hsmartcard->gState = HAL_SMARTCARD_STATE_BUSY_TX;
  961.  
  962.     hsmartcard->pTxBuffPtr  = pData;
  963.     hsmartcard->TxXferSize  = Size;
  964.     hsmartcard->TxXferCount = Size;
  965.     hsmartcard->TxISR       = NULL;
  966.  
  967.     /* Disable the Peripheral first to update mode for TX master */
  968.     CLEAR_BIT(hsmartcard->Instance->CR1, USART_CR1_UE);
  969.  
  970.     /* In case of TX only mode, if NACK is enabled, the USART must be able to monitor
  971.        the bidirectional line to detect a NACK signal in case of parity error.
  972.        Therefore, the receiver block must be enabled as well (RE bit must be set). */
  973.     if ((hsmartcard->Init.Mode == SMARTCARD_MODE_TX)
  974.         && (hsmartcard->Init.NACKEnable == SMARTCARD_NACK_ENABLE))
  975.     {
  976.       SET_BIT(hsmartcard->Instance->CR1, USART_CR1_RE);
  977.     }
  978.     /* Enable Tx */
  979.     SET_BIT(hsmartcard->Instance->CR1, USART_CR1_TE);
  980.  
  981.     /* Enable the Peripheral */
  982.     SET_BIT(hsmartcard->Instance->CR1, USART_CR1_UE);
  983.  
  984.     /* Perform a TX/RX FIFO Flush */
  985.     __HAL_SMARTCARD_FLUSH_DRREGISTER(hsmartcard);
  986.  
  987.     /* Configure Tx interrupt processing */
  988.     /* Set the Tx ISR function pointer */
  989.     hsmartcard->TxISR = SMARTCARD_TxISR;
  990.  
  991.     /* Process Unlocked */
  992.     __HAL_UNLOCK(hsmartcard);
  993.  
  994.     /* Enable the SMARTCARD Error Interrupt: (Frame error) */
  995.     SET_BIT(hsmartcard->Instance->CR3, USART_CR3_EIE);
  996.  
  997.     /* Enable the SMARTCARD Transmit Data Register Empty Interrupt */
  998.     SET_BIT(hsmartcard->Instance->CR1, USART_CR1_TXEIE);
  999.  
  1000.     return HAL_OK;
  1001.   }
  1002.   else
  1003.   {
  1004.     return HAL_BUSY;
  1005.   }
  1006. }
  1007.  
  1008. /**
  1009.   * @brief  Receive an amount of data in interrupt mode.
  1010.   * @param  hsmartcard Pointer to a SMARTCARD_HandleTypeDef structure that contains
  1011.   *                    the configuration information for the specified SMARTCARD module.
  1012.   * @param  pData pointer to data buffer.
  1013.   * @param  Size amount of data to be received.
  1014.   * @retval HAL status
  1015.   */
  1016. HAL_StatusTypeDef HAL_SMARTCARD_Receive_IT(SMARTCARD_HandleTypeDef *hsmartcard, uint8_t *pData, uint16_t Size)
  1017. {
  1018.   /* Check that a Rx process is not already ongoing */
  1019.   if (hsmartcard->RxState == HAL_SMARTCARD_STATE_READY)
  1020.   {
  1021.     if ((pData == NULL) || (Size == 0U))
  1022.     {
  1023.       return HAL_ERROR;
  1024.     }
  1025.  
  1026.     /* Process Locked */
  1027.     __HAL_LOCK(hsmartcard);
  1028.  
  1029.     hsmartcard->ErrorCode = HAL_SMARTCARD_ERROR_NONE;
  1030.     hsmartcard->RxState   = HAL_SMARTCARD_STATE_BUSY_RX;
  1031.  
  1032.     hsmartcard->pRxBuffPtr = pData;
  1033.     hsmartcard->RxXferSize = Size;
  1034.     hsmartcard->RxXferCount = Size;
  1035.  
  1036.     /* Configure Rx interrupt processing */
  1037.     /* Set the Rx ISR function pointer */
  1038.     hsmartcard->RxISR = SMARTCARD_RxISR;
  1039.  
  1040.     /* Process Unlocked */
  1041.     __HAL_UNLOCK(hsmartcard);
  1042.  
  1043.     /* Enable the SMARTCARD Parity Error and Data Register not empty Interrupts */
  1044.     SET_BIT(hsmartcard->Instance->CR1, USART_CR1_PEIE | USART_CR1_RXNEIE);
  1045.  
  1046.     /* Enable the SMARTCARD Error Interrupt: (Frame error, noise error, overrun error) */
  1047.     SET_BIT(hsmartcard->Instance->CR3, USART_CR3_EIE);
  1048.  
  1049.     return HAL_OK;
  1050.   }
  1051.   else
  1052.   {
  1053.     return HAL_BUSY;
  1054.   }
  1055. }
  1056.  
  1057. /**
  1058.   * @brief  Send an amount of data in DMA mode.
  1059.   * @param  hsmartcard Pointer to a SMARTCARD_HandleTypeDef structure that contains
  1060.   *                    the configuration information for the specified SMARTCARD module.
  1061.   * @param  pData pointer to data buffer.
  1062.   * @param  Size amount of data to be sent.
  1063.   * @retval HAL status
  1064.   */
  1065. HAL_StatusTypeDef HAL_SMARTCARD_Transmit_DMA(SMARTCARD_HandleTypeDef *hsmartcard, uint8_t *pData, uint16_t Size)
  1066. {
  1067.   /* Check that a Tx process is not already ongoing */
  1068.   if (hsmartcard->gState == HAL_SMARTCARD_STATE_READY)
  1069.   {
  1070.     if ((pData == NULL) || (Size == 0U))
  1071.     {
  1072.       return HAL_ERROR;
  1073.     }
  1074.  
  1075.     /* Process Locked */
  1076.     __HAL_LOCK(hsmartcard);
  1077.  
  1078.     hsmartcard->gState = HAL_SMARTCARD_STATE_BUSY_TX;
  1079.  
  1080.     hsmartcard->ErrorCode = HAL_SMARTCARD_ERROR_NONE;
  1081.     hsmartcard->pTxBuffPtr = pData;
  1082.     hsmartcard->TxXferSize = Size;
  1083.     hsmartcard->TxXferCount = Size;
  1084.  
  1085.     /* Disable the Peripheral first to update mode for TX master */
  1086.     CLEAR_BIT(hsmartcard->Instance->CR1, USART_CR1_UE);
  1087.  
  1088.     /* In case of TX only mode, if NACK is enabled, the USART must be able to monitor
  1089.        the bidirectional line to detect a NACK signal in case of parity error.
  1090.        Therefore, the receiver block must be enabled as well (RE bit must be set). */
  1091.     if ((hsmartcard->Init.Mode == SMARTCARD_MODE_TX)
  1092.         && (hsmartcard->Init.NACKEnable == SMARTCARD_NACK_ENABLE))
  1093.     {
  1094.       SET_BIT(hsmartcard->Instance->CR1, USART_CR1_RE);
  1095.     }
  1096.     /* Enable Tx */
  1097.     SET_BIT(hsmartcard->Instance->CR1, USART_CR1_TE);
  1098.  
  1099.     /* Enable the Peripheral */
  1100.     SET_BIT(hsmartcard->Instance->CR1, USART_CR1_UE);
  1101.  
  1102.     /* Perform a TX/RX FIFO Flush */
  1103.     __HAL_SMARTCARD_FLUSH_DRREGISTER(hsmartcard);
  1104.  
  1105.     /* Set the SMARTCARD DMA transfer complete callback */
  1106.     hsmartcard->hdmatx->XferCpltCallback = SMARTCARD_DMATransmitCplt;
  1107.  
  1108.     /* Set the SMARTCARD error callback */
  1109.     hsmartcard->hdmatx->XferErrorCallback = SMARTCARD_DMAError;
  1110.  
  1111.     /* Set the DMA abort callback */
  1112.     hsmartcard->hdmatx->XferAbortCallback = NULL;
  1113.  
  1114.     /* Enable the SMARTCARD transmit DMA channel */
  1115.     if (HAL_DMA_Start_IT(hsmartcard->hdmatx, (uint32_t)hsmartcard->pTxBuffPtr, (uint32_t)&hsmartcard->Instance->TDR,
  1116.                          Size) == HAL_OK)
  1117.     {
  1118.       /* Clear the TC flag in the ICR register */
  1119.       CLEAR_BIT(hsmartcard->Instance->ICR, USART_ICR_TCCF);
  1120.  
  1121.       /* Process Unlocked */
  1122.       __HAL_UNLOCK(hsmartcard);
  1123.  
  1124.       /* Enable the UART Error Interrupt: (Frame error) */
  1125.       SET_BIT(hsmartcard->Instance->CR3, USART_CR3_EIE);
  1126.  
  1127.       /* Enable the DMA transfer for transmit request by setting the DMAT bit
  1128.          in the SMARTCARD associated USART CR3 register */
  1129.       SET_BIT(hsmartcard->Instance->CR3, USART_CR3_DMAT);
  1130.  
  1131.       return HAL_OK;
  1132.     }
  1133.     else
  1134.     {
  1135.       /* Set error code to DMA */
  1136.       hsmartcard->ErrorCode = HAL_SMARTCARD_ERROR_DMA;
  1137.  
  1138.       /* Process Unlocked */
  1139.       __HAL_UNLOCK(hsmartcard);
  1140.  
  1141.       /* Restore hsmartcard->State to ready */
  1142.       hsmartcard->gState = HAL_SMARTCARD_STATE_READY;
  1143.  
  1144.       return HAL_ERROR;
  1145.     }
  1146.   }
  1147.   else
  1148.   {
  1149.     return HAL_BUSY;
  1150.   }
  1151. }
  1152.  
  1153. /**
  1154.   * @brief  Receive an amount of data in DMA mode.
  1155.   * @param  hsmartcard Pointer to a SMARTCARD_HandleTypeDef structure that contains
  1156.   *                    the configuration information for the specified SMARTCARD module.
  1157.   * @param  pData pointer to data buffer.
  1158.   * @param  Size amount of data to be received.
  1159.   * @note   The SMARTCARD-associated USART parity is enabled (PCE = 1),
  1160.   *         the received data contain the parity bit (MSB position).
  1161.   * @retval HAL status
  1162.   */
  1163. HAL_StatusTypeDef HAL_SMARTCARD_Receive_DMA(SMARTCARD_HandleTypeDef *hsmartcard, uint8_t *pData, uint16_t Size)
  1164. {
  1165.   /* Check that a Rx process is not already ongoing */
  1166.   if (hsmartcard->RxState == HAL_SMARTCARD_STATE_READY)
  1167.   {
  1168.     if ((pData == NULL) || (Size == 0U))
  1169.     {
  1170.       return HAL_ERROR;
  1171.     }
  1172.  
  1173.     /* Process Locked */
  1174.     __HAL_LOCK(hsmartcard);
  1175.  
  1176.     hsmartcard->ErrorCode = HAL_SMARTCARD_ERROR_NONE;
  1177.     hsmartcard->RxState   = HAL_SMARTCARD_STATE_BUSY_RX;
  1178.  
  1179.     hsmartcard->pRxBuffPtr = pData;
  1180.     hsmartcard->RxXferSize = Size;
  1181.  
  1182.     /* Set the SMARTCARD DMA transfer complete callback */
  1183.     hsmartcard->hdmarx->XferCpltCallback = SMARTCARD_DMAReceiveCplt;
  1184.  
  1185.     /* Set the SMARTCARD DMA error callback */
  1186.     hsmartcard->hdmarx->XferErrorCallback = SMARTCARD_DMAError;
  1187.  
  1188.     /* Set the DMA abort callback */
  1189.     hsmartcard->hdmarx->XferAbortCallback = NULL;
  1190.  
  1191.     /* Enable the DMA channel */
  1192.     if (HAL_DMA_Start_IT(hsmartcard->hdmarx, (uint32_t)&hsmartcard->Instance->RDR, (uint32_t)hsmartcard->pRxBuffPtr,
  1193.                          Size) == HAL_OK)
  1194.     {
  1195.       /* Process Unlocked */
  1196.       __HAL_UNLOCK(hsmartcard);
  1197.  
  1198.       /* Enable the SMARTCARD Parity Error Interrupt */
  1199.       SET_BIT(hsmartcard->Instance->CR1, USART_CR1_PEIE);
  1200.  
  1201.       /* Enable the SMARTCARD Error Interrupt: (Frame error, noise error, overrun error) */
  1202.       SET_BIT(hsmartcard->Instance->CR3, USART_CR3_EIE);
  1203.  
  1204.       /* Enable the DMA transfer for the receiver request by setting the DMAR bit
  1205.          in the SMARTCARD associated USART CR3 register */
  1206.       SET_BIT(hsmartcard->Instance->CR3, USART_CR3_DMAR);
  1207.  
  1208.       return HAL_OK;
  1209.     }
  1210.     else
  1211.     {
  1212.       /* Set error code to DMA */
  1213.       hsmartcard->ErrorCode = HAL_SMARTCARD_ERROR_DMA;
  1214.  
  1215.       /* Process Unlocked */
  1216.       __HAL_UNLOCK(hsmartcard);
  1217.  
  1218.       /* Restore hsmartcard->State to ready */
  1219.       hsmartcard->RxState = HAL_SMARTCARD_STATE_READY;
  1220.  
  1221.       return HAL_ERROR;
  1222.     }
  1223.   }
  1224.   else
  1225.   {
  1226.     return HAL_BUSY;
  1227.   }
  1228. }
  1229.  
  1230. /**
  1231.   * @brief  Abort ongoing transfers (blocking mode).
  1232.   * @param  hsmartcard Pointer to a SMARTCARD_HandleTypeDef structure that contains
  1233.   *                    the configuration information for the specified SMARTCARD module.
  1234.   * @note   This procedure could be used for aborting any ongoing transfer started in Interrupt or DMA mode.
  1235.   *         This procedure performs following operations :
  1236.   *           - Disable SMARTCARD Interrupts (Tx and Rx)
  1237.   *           - Disable the DMA transfer in the peripheral register (if enabled)
  1238.   *           - Abort DMA transfer by calling HAL_DMA_Abort (in case of transfer in DMA mode)
  1239.   *           - Set handle State to READY
  1240.   * @note   This procedure is executed in blocking mode : when exiting function, Abort is considered as completed.
  1241.   * @retval HAL status
  1242.   */
  1243. HAL_StatusTypeDef HAL_SMARTCARD_Abort(SMARTCARD_HandleTypeDef *hsmartcard)
  1244. {
  1245.   /* Disable RTOIE, EOBIE, TXEIE, TCIE, RXNE, PE and ERR (Frame error, noise error, overrun error) interrupts */
  1246.   CLEAR_BIT(hsmartcard->Instance->CR1,
  1247.             (USART_CR1_RXNEIE | USART_CR1_PEIE | USART_CR1_TXEIE | USART_CR1_TCIE | USART_CR1_RTOIE |
  1248.              USART_CR1_EOBIE));
  1249.   CLEAR_BIT(hsmartcard->Instance->CR3, USART_CR3_EIE);
  1250.  
  1251.   /* Disable the SMARTCARD DMA Tx request if enabled */
  1252.   if (HAL_IS_BIT_SET(hsmartcard->Instance->CR3, USART_CR3_DMAT))
  1253.   {
  1254.     CLEAR_BIT(hsmartcard->Instance->CR3, USART_CR3_DMAT);
  1255.  
  1256.     /* Abort the SMARTCARD DMA Tx channel : use blocking DMA Abort API (no callback) */
  1257.     if (hsmartcard->hdmatx != NULL)
  1258.     {
  1259.       /* Set the SMARTCARD DMA Abort callback to Null.
  1260.          No call back execution at end of DMA abort procedure */
  1261.       hsmartcard->hdmatx->XferAbortCallback = NULL;
  1262.  
  1263.       if (HAL_DMA_Abort(hsmartcard->hdmatx) != HAL_OK)
  1264.       {
  1265.         if (HAL_DMA_GetError(hsmartcard->hdmatx) == HAL_DMA_ERROR_TIMEOUT)
  1266.         {
  1267.           /* Set error code to DMA */
  1268.           hsmartcard->ErrorCode = HAL_SMARTCARD_ERROR_DMA;
  1269.  
  1270.           return HAL_TIMEOUT;
  1271.         }
  1272.       }
  1273.     }
  1274.   }
  1275.  
  1276.   /* Disable the SMARTCARD DMA Rx request if enabled */
  1277.   if (HAL_IS_BIT_SET(hsmartcard->Instance->CR3, USART_CR3_DMAR))
  1278.   {
  1279.     CLEAR_BIT(hsmartcard->Instance->CR3, USART_CR3_DMAR);
  1280.  
  1281.     /* Abort the SMARTCARD DMA Rx channel : use blocking DMA Abort API (no callback) */
  1282.     if (hsmartcard->hdmarx != NULL)
  1283.     {
  1284.       /* Set the SMARTCARD DMA Abort callback to Null.
  1285.          No call back execution at end of DMA abort procedure */
  1286.       hsmartcard->hdmarx->XferAbortCallback = NULL;
  1287.  
  1288.       if (HAL_DMA_Abort(hsmartcard->hdmarx) != HAL_OK)
  1289.       {
  1290.         if (HAL_DMA_GetError(hsmartcard->hdmarx) == HAL_DMA_ERROR_TIMEOUT)
  1291.         {
  1292.           /* Set error code to DMA */
  1293.           hsmartcard->ErrorCode = HAL_SMARTCARD_ERROR_DMA;
  1294.  
  1295.           return HAL_TIMEOUT;
  1296.         }
  1297.       }
  1298.     }
  1299.   }
  1300.  
  1301.   /* Reset Tx and Rx transfer counters */
  1302.   hsmartcard->TxXferCount = 0U;
  1303.   hsmartcard->RxXferCount = 0U;
  1304.  
  1305.   /* Clear the Error flags in the ICR register */
  1306.   __HAL_SMARTCARD_CLEAR_FLAG(hsmartcard,
  1307.                              SMARTCARD_CLEAR_OREF | SMARTCARD_CLEAR_NEF | SMARTCARD_CLEAR_PEF | SMARTCARD_CLEAR_FEF |
  1308.                              SMARTCARD_CLEAR_RTOF | SMARTCARD_CLEAR_EOBF);
  1309.  
  1310.   /* Restore hsmartcard->gState and hsmartcard->RxState to Ready */
  1311.   hsmartcard->gState  = HAL_SMARTCARD_STATE_READY;
  1312.   hsmartcard->RxState = HAL_SMARTCARD_STATE_READY;
  1313.  
  1314.   /* Reset Handle ErrorCode to No Error */
  1315.   hsmartcard->ErrorCode = HAL_SMARTCARD_ERROR_NONE;
  1316.  
  1317.   return HAL_OK;
  1318. }
  1319.  
  1320. /**
  1321.   * @brief  Abort ongoing Transmit transfer (blocking mode).
  1322.   * @param  hsmartcard Pointer to a SMARTCARD_HandleTypeDef structure that contains
  1323.   *                    the configuration information for the specified SMARTCARD module.
  1324.   * @note   This procedure could be used for aborting any ongoing Tx transfer started in Interrupt or DMA mode.
  1325.   *         This procedure performs following operations :
  1326.   *           - Disable SMARTCARD Interrupts (Tx)
  1327.   *           - Disable the DMA transfer in the peripheral register (if enabled)
  1328.   *           - Abort DMA transfer by calling HAL_DMA_Abort (in case of transfer in DMA mode)
  1329.   *           - Set handle State to READY
  1330.   * @note   This procedure is executed in blocking mode : when exiting function, Abort is considered as completed.
  1331.   * @retval HAL status
  1332.   */
  1333. HAL_StatusTypeDef HAL_SMARTCARD_AbortTransmit(SMARTCARD_HandleTypeDef *hsmartcard)
  1334. {
  1335.   /* Disable TXEIE and TCIE interrupts */
  1336.   CLEAR_BIT(hsmartcard->Instance->CR1, (USART_CR1_TXEIE | USART_CR1_TCIE));
  1337.  
  1338.   /* Check if a receive process is ongoing or not. If not disable ERR IT */
  1339.   if (hsmartcard->RxState == HAL_SMARTCARD_STATE_READY)
  1340.   {
  1341.     /* Disable the SMARTCARD Error Interrupt: (Frame error) */
  1342.     CLEAR_BIT(hsmartcard->Instance->CR3, USART_CR3_EIE);
  1343.   }
  1344.  
  1345.   /* Disable the SMARTCARD DMA Tx request if enabled */
  1346.   if (HAL_IS_BIT_SET(hsmartcard->Instance->CR3, USART_CR3_DMAT))
  1347.   {
  1348.     CLEAR_BIT(hsmartcard->Instance->CR3, USART_CR3_DMAT);
  1349.  
  1350.     /* Abort the SMARTCARD DMA Tx channel : use blocking DMA Abort API (no callback) */
  1351.     if (hsmartcard->hdmatx != NULL)
  1352.     {
  1353.       /* Set the SMARTCARD DMA Abort callback to Null.
  1354.          No call back execution at end of DMA abort procedure */
  1355.       hsmartcard->hdmatx->XferAbortCallback = NULL;
  1356.  
  1357.       if (HAL_DMA_Abort(hsmartcard->hdmatx) != HAL_OK)
  1358.       {
  1359.         if (HAL_DMA_GetError(hsmartcard->hdmatx) == HAL_DMA_ERROR_TIMEOUT)
  1360.         {
  1361.           /* Set error code to DMA */
  1362.           hsmartcard->ErrorCode = HAL_SMARTCARD_ERROR_DMA;
  1363.  
  1364.           return HAL_TIMEOUT;
  1365.         }
  1366.       }
  1367.     }
  1368.   }
  1369.  
  1370.   /* Reset Tx transfer counter */
  1371.   hsmartcard->TxXferCount = 0U;
  1372.  
  1373.   /* Clear the Error flags in the ICR register */
  1374.   __HAL_SMARTCARD_CLEAR_FLAG(hsmartcard, SMARTCARD_CLEAR_FEF);
  1375.  
  1376.   /* Restore hsmartcard->gState to Ready */
  1377.   hsmartcard->gState = HAL_SMARTCARD_STATE_READY;
  1378.  
  1379.   return HAL_OK;
  1380. }
  1381.  
  1382. /**
  1383.   * @brief  Abort ongoing Receive transfer (blocking mode).
  1384.   * @param  hsmartcard Pointer to a SMARTCARD_HandleTypeDef structure that contains
  1385.   *                    the configuration information for the specified SMARTCARD module.
  1386.   * @note   This procedure could be used for aborting any ongoing Rx transfer started in Interrupt or DMA mode.
  1387.   *         This procedure performs following operations :
  1388.   *           - Disable SMARTCARD Interrupts (Rx)
  1389.   *           - Disable the DMA transfer in the peripheral register (if enabled)
  1390.   *           - Abort DMA transfer by calling HAL_DMA_Abort (in case of transfer in DMA mode)
  1391.   *           - Set handle State to READY
  1392.   * @note   This procedure is executed in blocking mode : when exiting function, Abort is considered as completed.
  1393.   * @retval HAL status
  1394.   */
  1395. HAL_StatusTypeDef HAL_SMARTCARD_AbortReceive(SMARTCARD_HandleTypeDef *hsmartcard)
  1396. {
  1397.   /* Disable RTOIE, EOBIE, RXNE, PE and ERR (Frame error, noise error, overrun error) interrupts */
  1398.   CLEAR_BIT(hsmartcard->Instance->CR1, (USART_CR1_RXNEIE | USART_CR1_PEIE | USART_CR1_RTOIE | USART_CR1_EOBIE));
  1399.   CLEAR_BIT(hsmartcard->Instance->CR3, USART_CR3_EIE);
  1400.  
  1401.   /* Check if a Transmit process is ongoing or not. If not disable ERR IT */
  1402.   if (hsmartcard->gState == HAL_SMARTCARD_STATE_READY)
  1403.   {
  1404.     /* Disable the SMARTCARD Error Interrupt: (Frame error) */
  1405.     CLEAR_BIT(hsmartcard->Instance->CR3, USART_CR3_EIE);
  1406.   }
  1407.  
  1408.   /* Disable the SMARTCARD DMA Rx request if enabled */
  1409.   if (HAL_IS_BIT_SET(hsmartcard->Instance->CR3, USART_CR3_DMAR))
  1410.   {
  1411.     CLEAR_BIT(hsmartcard->Instance->CR3, USART_CR3_DMAR);
  1412.  
  1413.     /* Abort the SMARTCARD DMA Rx channel : use blocking DMA Abort API (no callback) */
  1414.     if (hsmartcard->hdmarx != NULL)
  1415.     {
  1416.       /* Set the SMARTCARD DMA Abort callback to Null.
  1417.          No call back execution at end of DMA abort procedure */
  1418.       hsmartcard->hdmarx->XferAbortCallback = NULL;
  1419.  
  1420.       if (HAL_DMA_Abort(hsmartcard->hdmarx) != HAL_OK)
  1421.       {
  1422.         if (HAL_DMA_GetError(hsmartcard->hdmarx) == HAL_DMA_ERROR_TIMEOUT)
  1423.         {
  1424.           /* Set error code to DMA */
  1425.           hsmartcard->ErrorCode = HAL_SMARTCARD_ERROR_DMA;
  1426.  
  1427.           return HAL_TIMEOUT;
  1428.         }
  1429.       }
  1430.     }
  1431.   }
  1432.  
  1433.   /* Reset Rx transfer counter */
  1434.   hsmartcard->RxXferCount = 0U;
  1435.  
  1436.   /* Clear the Error flags in the ICR register */
  1437.   __HAL_SMARTCARD_CLEAR_FLAG(hsmartcard,
  1438.                              SMARTCARD_CLEAR_OREF | SMARTCARD_CLEAR_NEF | SMARTCARD_CLEAR_PEF | SMARTCARD_CLEAR_FEF |
  1439.                              SMARTCARD_CLEAR_RTOF | SMARTCARD_CLEAR_EOBF);
  1440.  
  1441.   /* Restore hsmartcard->RxState to Ready */
  1442.   hsmartcard->RxState = HAL_SMARTCARD_STATE_READY;
  1443.  
  1444.   return HAL_OK;
  1445. }
  1446.  
  1447. /**
  1448.   * @brief  Abort ongoing transfers (Interrupt mode).
  1449.   * @param  hsmartcard Pointer to a SMARTCARD_HandleTypeDef structure that contains
  1450.   *                    the configuration information for the specified SMARTCARD module.
  1451.   * @note   This procedure could be used for aborting any ongoing transfer started in Interrupt or DMA mode.
  1452.   *         This procedure performs following operations :
  1453.   *           - Disable SMARTCARD Interrupts (Tx and Rx)
  1454.   *           - Disable the DMA transfer in the peripheral register (if enabled)
  1455.   *           - Abort DMA transfer by calling HAL_DMA_Abort_IT (in case of transfer in DMA mode)
  1456.   *           - Set handle State to READY
  1457.   *           - At abort completion, call user abort complete callback
  1458.   * @note   This procedure is executed in Interrupt mode, meaning that abort procedure could be
  1459.   *         considered as completed only when user abort complete callback is executed (not when exiting function).
  1460.   * @retval HAL status
  1461.   */
  1462. HAL_StatusTypeDef HAL_SMARTCARD_Abort_IT(SMARTCARD_HandleTypeDef *hsmartcard)
  1463. {
  1464.   uint32_t abortcplt = 1U;
  1465.  
  1466.   /* Disable RTOIE, EOBIE, TXEIE, TCIE, RXNE, PE and ERR (Frame error, noise error, overrun error) interrupts */
  1467.   CLEAR_BIT(hsmartcard->Instance->CR1,
  1468.             (USART_CR1_RXNEIE | USART_CR1_PEIE | USART_CR1_TXEIE | USART_CR1_TCIE | USART_CR1_RTOIE |
  1469.              USART_CR1_EOBIE));
  1470.   CLEAR_BIT(hsmartcard->Instance->CR3, USART_CR3_EIE);
  1471.  
  1472.   /* If DMA Tx and/or DMA Rx Handles are associated to SMARTCARD Handle,
  1473.      DMA Abort complete callbacks should be initialised before any call
  1474.      to DMA Abort functions */
  1475.   /* DMA Tx Handle is valid */
  1476.   if (hsmartcard->hdmatx != NULL)
  1477.   {
  1478.     /* Set DMA Abort Complete callback if SMARTCARD DMA Tx request if enabled.
  1479.        Otherwise, set it to NULL */
  1480.     if (HAL_IS_BIT_SET(hsmartcard->Instance->CR3, USART_CR3_DMAT))
  1481.     {
  1482.       hsmartcard->hdmatx->XferAbortCallback = SMARTCARD_DMATxAbortCallback;
  1483.     }
  1484.     else
  1485.     {
  1486.       hsmartcard->hdmatx->XferAbortCallback = NULL;
  1487.     }
  1488.   }
  1489.   /* DMA Rx Handle is valid */
  1490.   if (hsmartcard->hdmarx != NULL)
  1491.   {
  1492.     /* Set DMA Abort Complete callback if SMARTCARD DMA Rx request if enabled.
  1493.        Otherwise, set it to NULL */
  1494.     if (HAL_IS_BIT_SET(hsmartcard->Instance->CR3, USART_CR3_DMAR))
  1495.     {
  1496.       hsmartcard->hdmarx->XferAbortCallback = SMARTCARD_DMARxAbortCallback;
  1497.     }
  1498.     else
  1499.     {
  1500.       hsmartcard->hdmarx->XferAbortCallback = NULL;
  1501.     }
  1502.   }
  1503.  
  1504.   /* Disable the SMARTCARD DMA Tx request if enabled */
  1505.   if (HAL_IS_BIT_SET(hsmartcard->Instance->CR3, USART_CR3_DMAT))
  1506.   {
  1507.     /* Disable DMA Tx at UART level */
  1508.     CLEAR_BIT(hsmartcard->Instance->CR3, USART_CR3_DMAT);
  1509.  
  1510.     /* Abort the SMARTCARD DMA Tx channel : use non blocking DMA Abort API (callback) */
  1511.     if (hsmartcard->hdmatx != NULL)
  1512.     {
  1513.       /* SMARTCARD Tx DMA Abort callback has already been initialised :
  1514.          will lead to call HAL_SMARTCARD_AbortCpltCallback() at end of DMA abort procedure */
  1515.  
  1516.       /* Abort DMA TX */
  1517.       if (HAL_DMA_Abort_IT(hsmartcard->hdmatx) != HAL_OK)
  1518.       {
  1519.         hsmartcard->hdmatx->XferAbortCallback = NULL;
  1520.       }
  1521.       else
  1522.       {
  1523.         abortcplt = 0U;
  1524.       }
  1525.     }
  1526.   }
  1527.  
  1528.   /* Disable the SMARTCARD DMA Rx request if enabled */
  1529.   if (HAL_IS_BIT_SET(hsmartcard->Instance->CR3, USART_CR3_DMAR))
  1530.   {
  1531.     CLEAR_BIT(hsmartcard->Instance->CR3, USART_CR3_DMAR);
  1532.  
  1533.     /* Abort the SMARTCARD DMA Rx channel : use non blocking DMA Abort API (callback) */
  1534.     if (hsmartcard->hdmarx != NULL)
  1535.     {
  1536.       /* SMARTCARD Rx DMA Abort callback has already been initialised :
  1537.          will lead to call HAL_SMARTCARD_AbortCpltCallback() at end of DMA abort procedure */
  1538.  
  1539.       /* Abort DMA RX */
  1540.       if (HAL_DMA_Abort_IT(hsmartcard->hdmarx) != HAL_OK)
  1541.       {
  1542.         hsmartcard->hdmarx->XferAbortCallback = NULL;
  1543.         abortcplt = 1U;
  1544.       }
  1545.       else
  1546.       {
  1547.         abortcplt = 0U;
  1548.       }
  1549.     }
  1550.   }
  1551.  
  1552.   /* if no DMA abort complete callback execution is required => call user Abort Complete callback */
  1553.   if (abortcplt == 1U)
  1554.   {
  1555.     /* Reset Tx and Rx transfer counters */
  1556.     hsmartcard->TxXferCount = 0U;
  1557.     hsmartcard->RxXferCount = 0U;
  1558.  
  1559.     /* Clear ISR function pointers */
  1560.     hsmartcard->RxISR = NULL;
  1561.     hsmartcard->TxISR = NULL;
  1562.  
  1563.     /* Reset errorCode */
  1564.     hsmartcard->ErrorCode = HAL_SMARTCARD_ERROR_NONE;
  1565.  
  1566.     /* Clear the Error flags in the ICR register */
  1567.     __HAL_SMARTCARD_CLEAR_FLAG(hsmartcard,
  1568.                                SMARTCARD_CLEAR_OREF | SMARTCARD_CLEAR_NEF | SMARTCARD_CLEAR_PEF |
  1569.                                SMARTCARD_CLEAR_FEF | SMARTCARD_CLEAR_RTOF | SMARTCARD_CLEAR_EOBF);
  1570.  
  1571.     /* Restore hsmartcard->gState and hsmartcard->RxState to Ready */
  1572.     hsmartcard->gState  = HAL_SMARTCARD_STATE_READY;
  1573.     hsmartcard->RxState = HAL_SMARTCARD_STATE_READY;
  1574.  
  1575.     /* As no DMA to be aborted, call directly user Abort complete callback */
  1576. #if (USE_HAL_SMARTCARD_REGISTER_CALLBACKS == 1)
  1577.     /* Call registered Abort complete callback */
  1578.     hsmartcard->AbortCpltCallback(hsmartcard);
  1579. #else
  1580.     /* Call legacy weak Abort complete callback */
  1581.     HAL_SMARTCARD_AbortCpltCallback(hsmartcard);
  1582. #endif /* USE_HAL_SMARTCARD_REGISTER_CALLBACK */
  1583.   }
  1584.  
  1585.   return HAL_OK;
  1586. }
  1587.  
  1588. /**
  1589.   * @brief  Abort ongoing Transmit transfer (Interrupt mode).
  1590.   * @param  hsmartcard Pointer to a SMARTCARD_HandleTypeDef structure that contains
  1591.   *                    the configuration information for the specified SMARTCARD module.
  1592.   * @note   This procedure could be used for aborting any ongoing Tx transfer started in Interrupt or DMA mode.
  1593.   *         This procedure performs following operations :
  1594.   *           - Disable SMARTCARD Interrupts (Tx)
  1595.   *           - Disable the DMA transfer in the peripheral register (if enabled)
  1596.   *           - Abort DMA transfer by calling HAL_DMA_Abort_IT (in case of transfer in DMA mode)
  1597.   *           - Set handle State to READY
  1598.   *           - At abort completion, call user abort complete callback
  1599.   * @note   This procedure is executed in Interrupt mode, meaning that abort procedure could be
  1600.   *         considered as completed only when user abort complete callback is executed (not when exiting function).
  1601.   * @retval HAL status
  1602.   */
  1603. HAL_StatusTypeDef HAL_SMARTCARD_AbortTransmit_IT(SMARTCARD_HandleTypeDef *hsmartcard)
  1604. {
  1605.   /* Disable TXEIE and TCIE interrupts */
  1606.   CLEAR_BIT(hsmartcard->Instance->CR1, (USART_CR1_TXEIE | USART_CR1_TCIE));
  1607.  
  1608.   /* Check if a receive process is ongoing or not. If not disable ERR IT */
  1609.   if (hsmartcard->RxState == HAL_SMARTCARD_STATE_READY)
  1610.   {
  1611.     /* Disable the SMARTCARD Error Interrupt: (Frame error) */
  1612.     CLEAR_BIT(hsmartcard->Instance->CR3, USART_CR3_EIE);
  1613.   }
  1614.  
  1615.   /* Disable the SMARTCARD DMA Tx request if enabled */
  1616.   if (HAL_IS_BIT_SET(hsmartcard->Instance->CR3, USART_CR3_DMAT))
  1617.   {
  1618.     CLEAR_BIT(hsmartcard->Instance->CR3, USART_CR3_DMAT);
  1619.  
  1620.     /* Abort the SMARTCARD DMA Tx channel : use non blocking DMA Abort API (callback) */
  1621.     if (hsmartcard->hdmatx != NULL)
  1622.     {
  1623.       /* Set the SMARTCARD DMA Abort callback :
  1624.          will lead to call HAL_SMARTCARD_AbortCpltCallback() at end of DMA abort procedure */
  1625.       hsmartcard->hdmatx->XferAbortCallback = SMARTCARD_DMATxOnlyAbortCallback;
  1626.  
  1627.       /* Abort DMA TX */
  1628.       if (HAL_DMA_Abort_IT(hsmartcard->hdmatx) != HAL_OK)
  1629.       {
  1630.         /* Call Directly hsmartcard->hdmatx->XferAbortCallback function in case of error */
  1631.         hsmartcard->hdmatx->XferAbortCallback(hsmartcard->hdmatx);
  1632.       }
  1633.     }
  1634.     else
  1635.     {
  1636.       /* Reset Tx transfer counter */
  1637.       hsmartcard->TxXferCount = 0U;
  1638.  
  1639.       /* Clear TxISR function pointers */
  1640.       hsmartcard->TxISR = NULL;
  1641.  
  1642.       /* Restore hsmartcard->gState to Ready */
  1643.       hsmartcard->gState = HAL_SMARTCARD_STATE_READY;
  1644.  
  1645.       /* As no DMA to be aborted, call directly user Abort complete callback */
  1646. #if (USE_HAL_SMARTCARD_REGISTER_CALLBACKS == 1)
  1647.       /* Call registered Abort Transmit Complete Callback */
  1648.       hsmartcard->AbortTransmitCpltCallback(hsmartcard);
  1649. #else
  1650.       /* Call legacy weak Abort Transmit Complete Callback */
  1651.       HAL_SMARTCARD_AbortTransmitCpltCallback(hsmartcard);
  1652. #endif /* USE_HAL_SMARTCARD_REGISTER_CALLBACK */
  1653.     }
  1654.   }
  1655.   else
  1656.   {
  1657.     /* Reset Tx transfer counter */
  1658.     hsmartcard->TxXferCount = 0U;
  1659.  
  1660.     /* Clear TxISR function pointers */
  1661.     hsmartcard->TxISR = NULL;
  1662.  
  1663.     /* Clear the Error flags in the ICR register */
  1664.     __HAL_SMARTCARD_CLEAR_FLAG(hsmartcard, SMARTCARD_CLEAR_FEF);
  1665.  
  1666.     /* Restore hsmartcard->gState to Ready */
  1667.     hsmartcard->gState = HAL_SMARTCARD_STATE_READY;
  1668.  
  1669.     /* As no DMA to be aborted, call directly user Abort complete callback */
  1670. #if (USE_HAL_SMARTCARD_REGISTER_CALLBACKS == 1)
  1671.     /* Call registered Abort Transmit Complete Callback */
  1672.     hsmartcard->AbortTransmitCpltCallback(hsmartcard);
  1673. #else
  1674.     /* Call legacy weak Abort Transmit Complete Callback */
  1675.     HAL_SMARTCARD_AbortTransmitCpltCallback(hsmartcard);
  1676. #endif /* USE_HAL_SMARTCARD_REGISTER_CALLBACK */
  1677.   }
  1678.  
  1679.   return HAL_OK;
  1680. }
  1681.  
  1682. /**
  1683.   * @brief  Abort ongoing Receive transfer (Interrupt mode).
  1684.   * @param  hsmartcard Pointer to a SMARTCARD_HandleTypeDef structure that contains
  1685.   *                    the configuration information for the specified SMARTCARD module.
  1686.   * @note   This procedure could be used for aborting any ongoing Rx transfer started in Interrupt or DMA mode.
  1687.   *         This procedure performs following operations :
  1688.   *           - Disable SMARTCARD Interrupts (Rx)
  1689.   *           - Disable the DMA transfer in the peripheral register (if enabled)
  1690.   *           - Abort DMA transfer by calling HAL_DMA_Abort_IT (in case of transfer in DMA mode)
  1691.   *           - Set handle State to READY
  1692.   *           - At abort completion, call user abort complete callback
  1693.   * @note   This procedure is executed in Interrupt mode, meaning that abort procedure could be
  1694.   *         considered as completed only when user abort complete callback is executed (not when exiting function).
  1695.   * @retval HAL status
  1696.   */
  1697. HAL_StatusTypeDef HAL_SMARTCARD_AbortReceive_IT(SMARTCARD_HandleTypeDef *hsmartcard)
  1698. {
  1699.   /* Disable RTOIE, EOBIE, RXNE, PE and ERR (Frame error, noise error, overrun error) interrupts */
  1700.   CLEAR_BIT(hsmartcard->Instance->CR1, (USART_CR1_RXNEIE | USART_CR1_PEIE | USART_CR1_RTOIE | USART_CR1_EOBIE));
  1701.   CLEAR_BIT(hsmartcard->Instance->CR3, USART_CR3_EIE);
  1702.  
  1703.   /* Check if a Transmit process is ongoing or not. If not disable ERR IT */
  1704.   if (hsmartcard->gState == HAL_SMARTCARD_STATE_READY)
  1705.   {
  1706.     /* Disable the SMARTCARD Error Interrupt: (Frame error) */
  1707.     CLEAR_BIT(hsmartcard->Instance->CR3, USART_CR3_EIE);
  1708.   }
  1709.  
  1710.   /* Disable the SMARTCARD DMA Rx request if enabled */
  1711.   if (HAL_IS_BIT_SET(hsmartcard->Instance->CR3, USART_CR3_DMAR))
  1712.   {
  1713.     CLEAR_BIT(hsmartcard->Instance->CR3, USART_CR3_DMAR);
  1714.  
  1715.     /* Abort the SMARTCARD DMA Rx channel : use non blocking DMA Abort API (callback) */
  1716.     if (hsmartcard->hdmarx != NULL)
  1717.     {
  1718.       /* Set the SMARTCARD DMA Abort callback :
  1719.          will lead to call HAL_SMARTCARD_AbortCpltCallback() at end of DMA abort procedure */
  1720.       hsmartcard->hdmarx->XferAbortCallback = SMARTCARD_DMARxOnlyAbortCallback;
  1721.  
  1722.       /* Abort DMA RX */
  1723.       if (HAL_DMA_Abort_IT(hsmartcard->hdmarx) != HAL_OK)
  1724.       {
  1725.         /* Call Directly hsmartcard->hdmarx->XferAbortCallback function in case of error */
  1726.         hsmartcard->hdmarx->XferAbortCallback(hsmartcard->hdmarx);
  1727.       }
  1728.     }
  1729.     else
  1730.     {
  1731.       /* Reset Rx transfer counter */
  1732.       hsmartcard->RxXferCount = 0U;
  1733.  
  1734.       /* Clear RxISR function pointer */
  1735.       hsmartcard->RxISR = NULL;
  1736.  
  1737.       /* Clear the Error flags in the ICR register */
  1738.       __HAL_SMARTCARD_CLEAR_FLAG(hsmartcard,
  1739.                                  SMARTCARD_CLEAR_OREF | SMARTCARD_CLEAR_NEF | SMARTCARD_CLEAR_PEF |
  1740.                                  SMARTCARD_CLEAR_FEF | SMARTCARD_CLEAR_RTOF | SMARTCARD_CLEAR_EOBF);
  1741.  
  1742.       /* Restore hsmartcard->RxState to Ready */
  1743.       hsmartcard->RxState = HAL_SMARTCARD_STATE_READY;
  1744.  
  1745.       /* As no DMA to be aborted, call directly user Abort complete callback */
  1746. #if (USE_HAL_SMARTCARD_REGISTER_CALLBACKS == 1)
  1747.       /* Call registered Abort Receive Complete Callback */
  1748.       hsmartcard->AbortReceiveCpltCallback(hsmartcard);
  1749. #else
  1750.       /* Call legacy weak Abort Receive Complete Callback */
  1751.       HAL_SMARTCARD_AbortReceiveCpltCallback(hsmartcard);
  1752. #endif /* USE_HAL_SMARTCARD_REGISTER_CALLBACK */
  1753.     }
  1754.   }
  1755.   else
  1756.   {
  1757.     /* Reset Rx transfer counter */
  1758.     hsmartcard->RxXferCount = 0U;
  1759.  
  1760.     /* Clear RxISR function pointer */
  1761.     hsmartcard->RxISR = NULL;
  1762.  
  1763.     /* Clear the Error flags in the ICR register */
  1764.     __HAL_SMARTCARD_CLEAR_FLAG(hsmartcard,
  1765.                                SMARTCARD_CLEAR_OREF | SMARTCARD_CLEAR_NEF | SMARTCARD_CLEAR_PEF |
  1766.                                SMARTCARD_CLEAR_FEF | SMARTCARD_CLEAR_RTOF | SMARTCARD_CLEAR_EOBF);
  1767.  
  1768.     /* Restore hsmartcard->RxState to Ready */
  1769.     hsmartcard->RxState = HAL_SMARTCARD_STATE_READY;
  1770.  
  1771.     /* As no DMA to be aborted, call directly user Abort complete callback */
  1772. #if (USE_HAL_SMARTCARD_REGISTER_CALLBACKS == 1)
  1773.     /* Call registered Abort Receive Complete Callback */
  1774.     hsmartcard->AbortReceiveCpltCallback(hsmartcard);
  1775. #else
  1776.     /* Call legacy weak Abort Receive Complete Callback */
  1777.     HAL_SMARTCARD_AbortReceiveCpltCallback(hsmartcard);
  1778. #endif /* USE_HAL_SMARTCARD_REGISTER_CALLBACK */
  1779.   }
  1780.  
  1781.   return HAL_OK;
  1782. }
  1783.  
  1784. /**
  1785.   * @brief  Handle SMARTCARD interrupt requests.
  1786.   * @param  hsmartcard Pointer to a SMARTCARD_HandleTypeDef structure that contains
  1787.   *                    the configuration information for the specified SMARTCARD module.
  1788.   * @retval None
  1789.   */
  1790. void HAL_SMARTCARD_IRQHandler(SMARTCARD_HandleTypeDef *hsmartcard)
  1791. {
  1792.   uint32_t isrflags   = READ_REG(hsmartcard->Instance->ISR);
  1793.   uint32_t cr1its     = READ_REG(hsmartcard->Instance->CR1);
  1794.   uint32_t cr3its     = READ_REG(hsmartcard->Instance->CR3);
  1795.   uint32_t errorflags;
  1796.   uint32_t errorcode;
  1797.  
  1798.   /* If no error occurs */
  1799.   errorflags = (isrflags & (uint32_t)(USART_ISR_PE | USART_ISR_FE | USART_ISR_ORE | USART_ISR_NE | USART_ISR_RTOF));
  1800.   if (errorflags == 0U)
  1801.   {
  1802.     /* SMARTCARD in mode Receiver ---------------------------------------------------*/
  1803.     if (((isrflags & USART_ISR_RXNE) != 0U)
  1804.         && ((cr1its & USART_CR1_RXNEIE) != 0U))
  1805.     {
  1806.       if (hsmartcard->RxISR != NULL)
  1807.       {
  1808.         hsmartcard->RxISR(hsmartcard);
  1809.       }
  1810.       return;
  1811.     }
  1812.   }
  1813.  
  1814.   /* If some errors occur */
  1815.   if ((errorflags != 0U)
  1816.       && (((cr3its & USART_CR3_EIE) != 0U)
  1817.           || ((cr1its & (USART_CR1_RXNEIE | USART_CR1_PEIE)) != 0U)))
  1818.   {
  1819.     /* SMARTCARD parity error interrupt occurred -------------------------------------*/
  1820.     if (((isrflags & USART_ISR_PE) != 0U) && ((cr1its & USART_CR1_PEIE) != 0U))
  1821.     {
  1822.       __HAL_SMARTCARD_CLEAR_IT(hsmartcard, SMARTCARD_CLEAR_PEF);
  1823.  
  1824.       hsmartcard->ErrorCode |= HAL_SMARTCARD_ERROR_PE;
  1825.     }
  1826.  
  1827.     /* SMARTCARD frame error interrupt occurred --------------------------------------*/
  1828.     if (((isrflags & USART_ISR_FE) != 0U) && ((cr3its & USART_CR3_EIE) != 0U))
  1829.     {
  1830.       __HAL_SMARTCARD_CLEAR_IT(hsmartcard, SMARTCARD_CLEAR_FEF);
  1831.  
  1832.       hsmartcard->ErrorCode |= HAL_SMARTCARD_ERROR_FE;
  1833.     }
  1834.  
  1835.     /* SMARTCARD noise error interrupt occurred --------------------------------------*/
  1836.     if (((isrflags & USART_ISR_NE) != 0U) && ((cr3its & USART_CR3_EIE) != 0U))
  1837.     {
  1838.       __HAL_SMARTCARD_CLEAR_IT(hsmartcard, SMARTCARD_CLEAR_NEF);
  1839.  
  1840.       hsmartcard->ErrorCode |= HAL_SMARTCARD_ERROR_NE;
  1841.     }
  1842.  
  1843.     /* SMARTCARD Over-Run interrupt occurred -----------------------------------------*/
  1844.     if (((isrflags & USART_ISR_ORE) != 0U)
  1845.         && (((cr1its & USART_CR1_RXNEIE) != 0U)
  1846.             || ((cr3its & USART_CR3_EIE) != 0U)))
  1847.     {
  1848.       __HAL_SMARTCARD_CLEAR_IT(hsmartcard, SMARTCARD_CLEAR_OREF);
  1849.  
  1850.       hsmartcard->ErrorCode |= HAL_SMARTCARD_ERROR_ORE;
  1851.     }
  1852.  
  1853.     /* SMARTCARD receiver timeout interrupt occurred -----------------------------------------*/
  1854.     if (((isrflags & USART_ISR_RTOF) != 0U) && ((cr1its & USART_CR1_RTOIE) != 0U))
  1855.     {
  1856.       __HAL_SMARTCARD_CLEAR_IT(hsmartcard, SMARTCARD_CLEAR_RTOF);
  1857.  
  1858.       hsmartcard->ErrorCode |= HAL_SMARTCARD_ERROR_RTO;
  1859.     }
  1860.  
  1861.     /* Call SMARTCARD Error Call back function if need be --------------------------*/
  1862.     if (hsmartcard->ErrorCode != HAL_SMARTCARD_ERROR_NONE)
  1863.     {
  1864.       /* SMARTCARD in mode Receiver ---------------------------------------------------*/
  1865.       if (((isrflags & USART_ISR_RXNE) != 0U)
  1866.           && ((cr1its & USART_CR1_RXNEIE) != 0U))
  1867.       {
  1868.         if (hsmartcard->RxISR != NULL)
  1869.         {
  1870.           hsmartcard->RxISR(hsmartcard);
  1871.         }
  1872.       }
  1873.  
  1874.       /* If Error is to be considered as blocking :
  1875.           - Receiver Timeout error in Reception
  1876.           - Overrun error in Reception
  1877.           - any error occurs in DMA mode reception
  1878.       */
  1879.       errorcode = hsmartcard->ErrorCode;
  1880.       if ((HAL_IS_BIT_SET(hsmartcard->Instance->CR3, USART_CR3_DMAR))
  1881.           || ((errorcode & (HAL_SMARTCARD_ERROR_RTO | HAL_SMARTCARD_ERROR_ORE)) != 0U))
  1882.       {
  1883.         /* Blocking error : transfer is aborted
  1884.            Set the SMARTCARD state ready to be able to start again the process,
  1885.            Disable Rx Interrupts, and disable Rx DMA request, if ongoing */
  1886.         SMARTCARD_EndRxTransfer(hsmartcard);
  1887.  
  1888.         /* Disable the SMARTCARD DMA Rx request if enabled */
  1889.         if (HAL_IS_BIT_SET(hsmartcard->Instance->CR3, USART_CR3_DMAR))
  1890.         {
  1891.           CLEAR_BIT(hsmartcard->Instance->CR3, USART_CR3_DMAR);
  1892.  
  1893.           /* Abort the SMARTCARD DMA Rx channel */
  1894.           if (hsmartcard->hdmarx != NULL)
  1895.           {
  1896.             /* Set the SMARTCARD DMA Abort callback :
  1897.                will lead to call HAL_SMARTCARD_ErrorCallback() at end of DMA abort procedure */
  1898.             hsmartcard->hdmarx->XferAbortCallback = SMARTCARD_DMAAbortOnError;
  1899.  
  1900.             /* Abort DMA RX */
  1901.             if (HAL_DMA_Abort_IT(hsmartcard->hdmarx) != HAL_OK)
  1902.             {
  1903.               /* Call Directly hsmartcard->hdmarx->XferAbortCallback function in case of error */
  1904.               hsmartcard->hdmarx->XferAbortCallback(hsmartcard->hdmarx);
  1905.             }
  1906.           }
  1907.           else
  1908.           {
  1909. #if (USE_HAL_SMARTCARD_REGISTER_CALLBACKS == 1)
  1910.             /* Call registered user error callback */
  1911.             hsmartcard->ErrorCallback(hsmartcard);
  1912. #else
  1913.             /* Call legacy weak user error callback */
  1914.             HAL_SMARTCARD_ErrorCallback(hsmartcard);
  1915. #endif /* USE_HAL_SMARTCARD_REGISTER_CALLBACK */
  1916.           }
  1917.         }
  1918.         else
  1919.         {
  1920. #if (USE_HAL_SMARTCARD_REGISTER_CALLBACKS == 1)
  1921.           /* Call registered user error callback */
  1922.           hsmartcard->ErrorCallback(hsmartcard);
  1923. #else
  1924.           /* Call legacy weak user error callback */
  1925.           HAL_SMARTCARD_ErrorCallback(hsmartcard);
  1926. #endif /* USE_HAL_SMARTCARD_REGISTER_CALLBACK */
  1927.         }
  1928.       }
  1929.       /* other error type to be considered as blocking :
  1930.           - Frame error in Transmission
  1931.       */
  1932.       else if ((hsmartcard->gState == HAL_SMARTCARD_STATE_BUSY_TX)
  1933.                && ((errorcode & HAL_SMARTCARD_ERROR_FE) != 0U))
  1934.       {
  1935.         /* Blocking error : transfer is aborted
  1936.            Set the SMARTCARD state ready to be able to start again the process,
  1937.            Disable Tx Interrupts, and disable Tx DMA request, if ongoing */
  1938.         SMARTCARD_EndTxTransfer(hsmartcard);
  1939.  
  1940.         /* Disable the SMARTCARD DMA Tx request if enabled */
  1941.         if (HAL_IS_BIT_SET(hsmartcard->Instance->CR3, USART_CR3_DMAT))
  1942.         {
  1943.           CLEAR_BIT(hsmartcard->Instance->CR3, USART_CR3_DMAT);
  1944.  
  1945.           /* Abort the SMARTCARD DMA Tx channel */
  1946.           if (hsmartcard->hdmatx != NULL)
  1947.           {
  1948.             /* Set the SMARTCARD DMA Abort callback :
  1949.                will lead to call HAL_SMARTCARD_ErrorCallback() at end of DMA abort procedure */
  1950.             hsmartcard->hdmatx->XferAbortCallback = SMARTCARD_DMAAbortOnError;
  1951.  
  1952.             /* Abort DMA TX */
  1953.             if (HAL_DMA_Abort_IT(hsmartcard->hdmatx) != HAL_OK)
  1954.             {
  1955.               /* Call Directly hsmartcard->hdmatx->XferAbortCallback function in case of error */
  1956.               hsmartcard->hdmatx->XferAbortCallback(hsmartcard->hdmatx);
  1957.             }
  1958.           }
  1959.           else
  1960.           {
  1961. #if (USE_HAL_SMARTCARD_REGISTER_CALLBACKS == 1)
  1962.             /* Call registered user error callback */
  1963.             hsmartcard->ErrorCallback(hsmartcard);
  1964. #else
  1965.             /* Call legacy weak user error callback */
  1966.             HAL_SMARTCARD_ErrorCallback(hsmartcard);
  1967. #endif /* USE_HAL_SMARTCARD_REGISTER_CALLBACK */
  1968.           }
  1969.         }
  1970.         else
  1971.         {
  1972. #if (USE_HAL_SMARTCARD_REGISTER_CALLBACKS == 1)
  1973.           /* Call registered user error callback */
  1974.           hsmartcard->ErrorCallback(hsmartcard);
  1975. #else
  1976.           /* Call legacy weak user error callback */
  1977.           HAL_SMARTCARD_ErrorCallback(hsmartcard);
  1978. #endif /* USE_HAL_SMARTCARD_REGISTER_CALLBACK */
  1979.         }
  1980.       }
  1981.       else
  1982.       {
  1983.         /* Non Blocking error : transfer could go on.
  1984.            Error is notified to user through user error callback */
  1985. #if (USE_HAL_SMARTCARD_REGISTER_CALLBACKS == 1)
  1986.         /* Call registered user error callback */
  1987.         hsmartcard->ErrorCallback(hsmartcard);
  1988. #else
  1989.         /* Call legacy weak user error callback */
  1990.         HAL_SMARTCARD_ErrorCallback(hsmartcard);
  1991. #endif /* USE_HAL_SMARTCARD_REGISTER_CALLBACK */
  1992.         hsmartcard->ErrorCode = HAL_SMARTCARD_ERROR_NONE;
  1993.       }
  1994.     }
  1995.     return;
  1996.  
  1997.   } /* End if some error occurs */
  1998.  
  1999.   /* SMARTCARD in mode Receiver, end of block interruption ------------------------*/
  2000.   if (((isrflags & USART_ISR_EOBF) != 0U) && ((cr1its & USART_CR1_EOBIE) != 0U))
  2001.   {
  2002.     hsmartcard->RxState = HAL_SMARTCARD_STATE_READY;
  2003.     __HAL_UNLOCK(hsmartcard);
  2004. #if (USE_HAL_SMARTCARD_REGISTER_CALLBACKS == 1)
  2005.     /* Call registered Rx complete callback */
  2006.     hsmartcard->RxCpltCallback(hsmartcard);
  2007. #else
  2008.     /* Call legacy weak Rx complete callback */
  2009.     HAL_SMARTCARD_RxCpltCallback(hsmartcard);
  2010. #endif /* USE_HAL_SMARTCARD_REGISTER_CALLBACK */
  2011.     /* Clear EOBF interrupt after HAL_SMARTCARD_RxCpltCallback() call for the End of Block information
  2012.        to be available during HAL_SMARTCARD_RxCpltCallback() processing */
  2013.     __HAL_SMARTCARD_CLEAR_IT(hsmartcard, SMARTCARD_CLEAR_EOBF);
  2014.     return;
  2015.   }
  2016.  
  2017.   /* SMARTCARD in mode Transmitter ------------------------------------------------*/
  2018.   if (((isrflags & USART_ISR_TXE) != 0U)
  2019.       && ((cr1its & USART_CR1_TXEIE) != 0U))
  2020.   {
  2021.     if (hsmartcard->TxISR != NULL)
  2022.     {
  2023.       hsmartcard->TxISR(hsmartcard);
  2024.     }
  2025.     return;
  2026.   }
  2027.  
  2028.   /* SMARTCARD in mode Transmitter (transmission end) ------------------------*/
  2029.   if (__HAL_SMARTCARD_GET_IT(hsmartcard, hsmartcard->AdvancedInit.TxCompletionIndication) != RESET)
  2030.   {
  2031.     if (__HAL_SMARTCARD_GET_IT_SOURCE(hsmartcard, hsmartcard->AdvancedInit.TxCompletionIndication) != RESET)
  2032.     {
  2033.       SMARTCARD_EndTransmit_IT(hsmartcard);
  2034.       return;
  2035.     }
  2036.   }
  2037.  
  2038. }
  2039.  
  2040. /**
  2041.   * @brief  Tx Transfer completed callback.
  2042.   * @param  hsmartcard Pointer to a SMARTCARD_HandleTypeDef structure that contains
  2043.   *                    the configuration information for the specified SMARTCARD module.
  2044.   * @retval None
  2045.   */
  2046. __weak void HAL_SMARTCARD_TxCpltCallback(SMARTCARD_HandleTypeDef *hsmartcard)
  2047. {
  2048.   /* Prevent unused argument(s) compilation warning */
  2049.   UNUSED(hsmartcard);
  2050.  
  2051.   /* NOTE : This function should not be modified, when the callback is needed,
  2052.             the HAL_SMARTCARD_TxCpltCallback can be implemented in the user file.
  2053.    */
  2054. }
  2055.  
  2056. /**
  2057.   * @brief  Rx Transfer completed callback.
  2058.   * @param  hsmartcard Pointer to a SMARTCARD_HandleTypeDef structure that contains
  2059.   *                    the configuration information for the specified SMARTCARD module.
  2060.   * @retval None
  2061.   */
  2062. __weak void HAL_SMARTCARD_RxCpltCallback(SMARTCARD_HandleTypeDef *hsmartcard)
  2063. {
  2064.   /* Prevent unused argument(s) compilation warning */
  2065.   UNUSED(hsmartcard);
  2066.  
  2067.   /* NOTE : This function should not be modified, when the callback is needed,
  2068.             the HAL_SMARTCARD_RxCpltCallback can be implemented in the user file.
  2069.    */
  2070. }
  2071.  
  2072. /**
  2073.   * @brief  SMARTCARD error callback.
  2074.   * @param  hsmartcard Pointer to a SMARTCARD_HandleTypeDef structure that contains
  2075.   *                    the configuration information for the specified SMARTCARD module.
  2076.   * @retval None
  2077.   */
  2078. __weak void HAL_SMARTCARD_ErrorCallback(SMARTCARD_HandleTypeDef *hsmartcard)
  2079. {
  2080.   /* Prevent unused argument(s) compilation warning */
  2081.   UNUSED(hsmartcard);
  2082.  
  2083.   /* NOTE : This function should not be modified, when the callback is needed,
  2084.             the HAL_SMARTCARD_ErrorCallback can be implemented in the user file.
  2085.    */
  2086. }
  2087.  
  2088. /**
  2089.   * @brief  SMARTCARD Abort Complete callback.
  2090.   * @param  hsmartcard Pointer to a SMARTCARD_HandleTypeDef structure that contains
  2091.   *                    the configuration information for the specified SMARTCARD module.
  2092.   * @retval None
  2093.   */
  2094. __weak void HAL_SMARTCARD_AbortCpltCallback(SMARTCARD_HandleTypeDef *hsmartcard)
  2095. {
  2096.   /* Prevent unused argument(s) compilation warning */
  2097.   UNUSED(hsmartcard);
  2098.  
  2099.   /* NOTE : This function should not be modified, when the callback is needed,
  2100.             the HAL_SMARTCARD_AbortCpltCallback can be implemented in the user file.
  2101.    */
  2102. }
  2103.  
  2104. /**
  2105.   * @brief  SMARTCARD Abort Complete callback.
  2106.   * @param  hsmartcard Pointer to a SMARTCARD_HandleTypeDef structure that contains
  2107.   *                    the configuration information for the specified SMARTCARD module.
  2108.   * @retval None
  2109.   */
  2110. __weak void HAL_SMARTCARD_AbortTransmitCpltCallback(SMARTCARD_HandleTypeDef *hsmartcard)
  2111. {
  2112.   /* Prevent unused argument(s) compilation warning */
  2113.   UNUSED(hsmartcard);
  2114.  
  2115.   /* NOTE : This function should not be modified, when the callback is needed,
  2116.             the HAL_SMARTCARD_AbortTransmitCpltCallback can be implemented in the user file.
  2117.    */
  2118. }
  2119.  
  2120. /**
  2121.   * @brief  SMARTCARD Abort Receive Complete callback.
  2122.   * @param  hsmartcard Pointer to a SMARTCARD_HandleTypeDef structure that contains
  2123.   *                    the configuration information for the specified SMARTCARD module.
  2124.   * @retval None
  2125.   */
  2126. __weak void HAL_SMARTCARD_AbortReceiveCpltCallback(SMARTCARD_HandleTypeDef *hsmartcard)
  2127. {
  2128.   /* Prevent unused argument(s) compilation warning */
  2129.   UNUSED(hsmartcard);
  2130.  
  2131.   /* NOTE : This function should not be modified, when the callback is needed,
  2132.             the HAL_SMARTCARD_AbortReceiveCpltCallback can be implemented in the user file.
  2133.    */
  2134. }
  2135.  
  2136. /**
  2137.   * @}
  2138.   */
  2139.  
  2140. /** @defgroup SMARTCARD_Exported_Functions_Group4 Peripheral State and Errors functions
  2141.   * @brief    SMARTCARD State and Errors functions
  2142.   *
  2143. @verbatim
  2144.   ==============================================================================
  2145.                   ##### Peripheral State and Errors functions #####
  2146.   ==============================================================================
  2147.   [..]
  2148.     This subsection provides a set of functions allowing to return the State of SmartCard
  2149.     handle and also return Peripheral Errors occurred during communication process
  2150.      (+) HAL_SMARTCARD_GetState() API can be helpful to check in run-time the state
  2151.          of the SMARTCARD peripheral.
  2152.      (+) HAL_SMARTCARD_GetError() checks in run-time errors that could occur during
  2153.          communication.
  2154.  
  2155. @endverbatim
  2156.   * @{
  2157.   */
  2158.  
  2159. /**
  2160.   * @brief  Return the SMARTCARD handle state.
  2161.   * @param  hsmartcard Pointer to a SMARTCARD_HandleTypeDef structure that contains
  2162.   *                    the configuration information for the specified SMARTCARD module.
  2163.   * @retval SMARTCARD handle state
  2164.   */
  2165. HAL_SMARTCARD_StateTypeDef HAL_SMARTCARD_GetState(SMARTCARD_HandleTypeDef *hsmartcard)
  2166. {
  2167.   /* Return SMARTCARD handle state */
  2168.   uint32_t temp1;
  2169.   uint32_t temp2;
  2170.   temp1 = (uint32_t)hsmartcard->gState;
  2171.   temp2 = (uint32_t)hsmartcard->RxState;
  2172.  
  2173.   return (HAL_SMARTCARD_StateTypeDef)(temp1 | temp2);
  2174. }
  2175.  
  2176. /**
  2177.   * @brief  Return the SMARTCARD handle error code.
  2178.   * @param  hsmartcard Pointer to a SMARTCARD_HandleTypeDef structure that contains
  2179.   *                    the configuration information for the specified SMARTCARD module.
  2180.   * @retval SMARTCARD handle Error Code
  2181.   */
  2182. uint32_t HAL_SMARTCARD_GetError(SMARTCARD_HandleTypeDef *hsmartcard)
  2183. {
  2184.   return hsmartcard->ErrorCode;
  2185. }
  2186.  
  2187. /**
  2188.   * @}
  2189.   */
  2190.  
  2191. /**
  2192.   * @}
  2193.   */
  2194.  
  2195. /** @defgroup SMARTCARD_Private_Functions SMARTCARD Private Functions
  2196.   * @{
  2197.   */
  2198.  
  2199. #if (USE_HAL_SMARTCARD_REGISTER_CALLBACKS == 1)
  2200. /**
  2201.   * @brief  Initialize the callbacks to their default values.
  2202.   * @param  hsmartcard SMARTCARD handle.
  2203.   * @retval none
  2204.   */
  2205. void SMARTCARD_InitCallbacksToDefault(SMARTCARD_HandleTypeDef *hsmartcard)
  2206. {
  2207.   /* Init the SMARTCARD Callback settings */
  2208.   hsmartcard->TxCpltCallback            = HAL_SMARTCARD_TxCpltCallback;            /* Legacy weak TxCpltCallback    */
  2209.   hsmartcard->RxCpltCallback            = HAL_SMARTCARD_RxCpltCallback;            /* Legacy weak RxCpltCallback    */
  2210.   hsmartcard->ErrorCallback             = HAL_SMARTCARD_ErrorCallback;             /* Legacy weak ErrorCallback     */
  2211.   hsmartcard->AbortCpltCallback         = HAL_SMARTCARD_AbortCpltCallback;         /* Legacy weak AbortCpltCallback */
  2212.   hsmartcard->AbortTransmitCpltCallback = HAL_SMARTCARD_AbortTransmitCpltCallback; /* Legacy weak
  2213.                                                                                       AbortTransmitCpltCallback     */
  2214.   hsmartcard->AbortReceiveCpltCallback  = HAL_SMARTCARD_AbortReceiveCpltCallback;  /* Legacy weak
  2215.                                                                                       AbortReceiveCpltCallback      */
  2216.  
  2217. }
  2218. #endif /* USE_HAL_SMARTCARD_REGISTER_CALLBACKS */
  2219.  
  2220. /**
  2221.   * @brief  Configure the SMARTCARD associated USART peripheral.
  2222.   * @param  hsmartcard Pointer to a SMARTCARD_HandleTypeDef structure that contains
  2223.   *                    the configuration information for the specified SMARTCARD module.
  2224.   * @retval HAL status
  2225.   */
  2226. static HAL_StatusTypeDef SMARTCARD_SetConfig(SMARTCARD_HandleTypeDef *hsmartcard)
  2227. {
  2228.   uint32_t tmpreg;
  2229.   SMARTCARD_ClockSourceTypeDef clocksource;
  2230.   HAL_StatusTypeDef ret = HAL_OK;
  2231.   uint32_t pclk;
  2232.  
  2233.   /* Check the parameters */
  2234.   assert_param(IS_SMARTCARD_INSTANCE(hsmartcard->Instance));
  2235.   assert_param(IS_SMARTCARD_BAUDRATE(hsmartcard->Init.BaudRate));
  2236.   assert_param(IS_SMARTCARD_WORD_LENGTH(hsmartcard->Init.WordLength));
  2237.   assert_param(IS_SMARTCARD_STOPBITS(hsmartcard->Init.StopBits));
  2238.   assert_param(IS_SMARTCARD_PARITY(hsmartcard->Init.Parity));
  2239.   assert_param(IS_SMARTCARD_MODE(hsmartcard->Init.Mode));
  2240.   assert_param(IS_SMARTCARD_POLARITY(hsmartcard->Init.CLKPolarity));
  2241.   assert_param(IS_SMARTCARD_PHASE(hsmartcard->Init.CLKPhase));
  2242.   assert_param(IS_SMARTCARD_LASTBIT(hsmartcard->Init.CLKLastBit));
  2243.   assert_param(IS_SMARTCARD_ONE_BIT_SAMPLE(hsmartcard->Init.OneBitSampling));
  2244.   assert_param(IS_SMARTCARD_NACK(hsmartcard->Init.NACKEnable));
  2245.   assert_param(IS_SMARTCARD_TIMEOUT(hsmartcard->Init.TimeOutEnable));
  2246.   assert_param(IS_SMARTCARD_AUTORETRY_COUNT(hsmartcard->Init.AutoRetryCount));
  2247.  
  2248.   /*-------------------------- USART CR1 Configuration -----------------------*/
  2249.   /* In SmartCard mode, M and PCE are forced to 1 (8 bits + parity).
  2250.    * Oversampling is forced to 16 (OVER8 = 0).
  2251.    * Configure the Parity and Mode:
  2252.    *  set PS bit according to hsmartcard->Init.Parity value
  2253.    *  set TE and RE bits according to hsmartcard->Init.Mode value */
  2254.   tmpreg = (((uint32_t)hsmartcard->Init.Parity) | ((uint32_t)hsmartcard->Init.Mode) |
  2255.             ((uint32_t)hsmartcard->Init.WordLength));
  2256.   MODIFY_REG(hsmartcard->Instance->CR1, USART_CR1_FIELDS, tmpreg);
  2257.  
  2258.   /*-------------------------- USART CR2 Configuration -----------------------*/
  2259.   tmpreg = hsmartcard->Init.StopBits;
  2260.   /* Synchronous mode is activated by default */
  2261.   tmpreg |= (uint32_t) USART_CR2_CLKEN | hsmartcard->Init.CLKPolarity;
  2262.   tmpreg |= (uint32_t) hsmartcard->Init.CLKPhase | hsmartcard->Init.CLKLastBit;
  2263.   tmpreg |= (uint32_t) hsmartcard->Init.TimeOutEnable;
  2264.   MODIFY_REG(hsmartcard->Instance->CR2, USART_CR2_FIELDS, tmpreg);
  2265.  
  2266.   /*-------------------------- USART CR3 Configuration -----------------------*/
  2267.   /* Configure
  2268.    * - one-bit sampling method versus three samples' majority rule
  2269.    *   according to hsmartcard->Init.OneBitSampling
  2270.    * - NACK transmission in case of parity error according
  2271.    *   to hsmartcard->Init.NACKEnable
  2272.    * - autoretry counter according to hsmartcard->Init.AutoRetryCount */
  2273.  
  2274.   tmpreg = (uint32_t) hsmartcard->Init.OneBitSampling | hsmartcard->Init.NACKEnable;
  2275.   tmpreg |= ((uint32_t)hsmartcard->Init.AutoRetryCount << USART_CR3_SCARCNT_Pos);
  2276.   MODIFY_REG(hsmartcard->Instance->CR3, USART_CR3_FIELDS, tmpreg);
  2277.  
  2278.  
  2279.   /*-------------------------- USART GTPR Configuration ----------------------*/
  2280.   tmpreg = (hsmartcard->Init.Prescaler | ((uint32_t)hsmartcard->Init.GuardTime << USART_GTPR_GT_Pos));
  2281.   MODIFY_REG(hsmartcard->Instance->GTPR, (uint16_t)(USART_GTPR_GT | USART_GTPR_PSC), (uint16_t)tmpreg);
  2282.  
  2283.   /*-------------------------- USART RTOR Configuration ----------------------*/
  2284.   tmpreg = ((uint32_t)hsmartcard->Init.BlockLength << USART_RTOR_BLEN_Pos);
  2285.   if (hsmartcard->Init.TimeOutEnable == SMARTCARD_TIMEOUT_ENABLE)
  2286.   {
  2287.     assert_param(IS_SMARTCARD_TIMEOUT_VALUE(hsmartcard->Init.TimeOutValue));
  2288.     tmpreg |= (uint32_t) hsmartcard->Init.TimeOutValue;
  2289.   }
  2290.   MODIFY_REG(hsmartcard->Instance->RTOR, (USART_RTOR_RTO | USART_RTOR_BLEN), tmpreg);
  2291.  
  2292.   /*-------------------------- USART BRR Configuration -----------------------*/
  2293.   SMARTCARD_GETCLOCKSOURCE(hsmartcard, clocksource);
  2294.   tmpreg =   0U;
  2295.   switch (clocksource)
  2296.   {
  2297.     case SMARTCARD_CLOCKSOURCE_PCLK1:
  2298.       pclk = HAL_RCC_GetPCLK1Freq();
  2299.       tmpreg = (uint16_t)((pclk + (hsmartcard->Init.BaudRate / 2U)) / hsmartcard->Init.BaudRate);
  2300.       break;
  2301.     case SMARTCARD_CLOCKSOURCE_HSI:
  2302.       tmpreg = (uint16_t)((HSI_VALUE + (hsmartcard->Init.BaudRate / 2U)) / hsmartcard->Init.BaudRate);
  2303.       break;
  2304.     case SMARTCARD_CLOCKSOURCE_SYSCLK:
  2305.       pclk = HAL_RCC_GetSysClockFreq();
  2306.       tmpreg = (uint16_t)((pclk + (hsmartcard->Init.BaudRate / 2U)) / hsmartcard->Init.BaudRate);
  2307.       break;
  2308.     case SMARTCARD_CLOCKSOURCE_LSE:
  2309.       tmpreg = (uint16_t)((LSE_VALUE + (hsmartcard->Init.BaudRate / 2U)) / hsmartcard->Init.BaudRate);
  2310.       break;
  2311.     default:
  2312.       ret = HAL_ERROR;
  2313.       break;
  2314.   }
  2315.  
  2316.   /* USARTDIV must be greater than or equal to 0d16 */
  2317.   if ((tmpreg >= USART_BRR_MIN) && (tmpreg <= USART_BRR_MAX))
  2318.   {
  2319.     hsmartcard->Instance->BRR = tmpreg;
  2320.   }
  2321.   else
  2322.   {
  2323.     ret = HAL_ERROR;
  2324.   }
  2325.  
  2326.  
  2327.   /* Clear ISR function pointers */
  2328.   hsmartcard->RxISR   = NULL;
  2329.   hsmartcard->TxISR   = NULL;
  2330.  
  2331.   return ret;
  2332. }
  2333.  
  2334.  
  2335. /**
  2336.   * @brief Configure the SMARTCARD associated USART peripheral advanced features.
  2337.   * @param hsmartcard Pointer to a SMARTCARD_HandleTypeDef structure that contains
  2338.   *                   the configuration information for the specified SMARTCARD module.
  2339.   * @retval None
  2340.   */
  2341. static void SMARTCARD_AdvFeatureConfig(SMARTCARD_HandleTypeDef *hsmartcard)
  2342. {
  2343.   /* Check whether the set of advanced features to configure is properly set */
  2344.   assert_param(IS_SMARTCARD_ADVFEATURE_INIT(hsmartcard->AdvancedInit.AdvFeatureInit));
  2345.  
  2346.   /* if required, configure TX pin active level inversion */
  2347.   if (HAL_IS_BIT_SET(hsmartcard->AdvancedInit.AdvFeatureInit, SMARTCARD_ADVFEATURE_TXINVERT_INIT))
  2348.   {
  2349.     assert_param(IS_SMARTCARD_ADVFEATURE_TXINV(hsmartcard->AdvancedInit.TxPinLevelInvert));
  2350.     MODIFY_REG(hsmartcard->Instance->CR2, USART_CR2_TXINV, hsmartcard->AdvancedInit.TxPinLevelInvert);
  2351.   }
  2352.  
  2353.   /* if required, configure RX pin active level inversion */
  2354.   if (HAL_IS_BIT_SET(hsmartcard->AdvancedInit.AdvFeatureInit, SMARTCARD_ADVFEATURE_RXINVERT_INIT))
  2355.   {
  2356.     assert_param(IS_SMARTCARD_ADVFEATURE_RXINV(hsmartcard->AdvancedInit.RxPinLevelInvert));
  2357.     MODIFY_REG(hsmartcard->Instance->CR2, USART_CR2_RXINV, hsmartcard->AdvancedInit.RxPinLevelInvert);
  2358.   }
  2359.  
  2360.   /* if required, configure data inversion */
  2361.   if (HAL_IS_BIT_SET(hsmartcard->AdvancedInit.AdvFeatureInit, SMARTCARD_ADVFEATURE_DATAINVERT_INIT))
  2362.   {
  2363.     assert_param(IS_SMARTCARD_ADVFEATURE_DATAINV(hsmartcard->AdvancedInit.DataInvert));
  2364.     MODIFY_REG(hsmartcard->Instance->CR2, USART_CR2_DATAINV, hsmartcard->AdvancedInit.DataInvert);
  2365.   }
  2366.  
  2367.   /* if required, configure RX/TX pins swap */
  2368.   if (HAL_IS_BIT_SET(hsmartcard->AdvancedInit.AdvFeatureInit, SMARTCARD_ADVFEATURE_SWAP_INIT))
  2369.   {
  2370.     assert_param(IS_SMARTCARD_ADVFEATURE_SWAP(hsmartcard->AdvancedInit.Swap));
  2371.     MODIFY_REG(hsmartcard->Instance->CR2, USART_CR2_SWAP, hsmartcard->AdvancedInit.Swap);
  2372.   }
  2373.  
  2374.   /* if required, configure RX overrun detection disabling */
  2375.   if (HAL_IS_BIT_SET(hsmartcard->AdvancedInit.AdvFeatureInit, SMARTCARD_ADVFEATURE_RXOVERRUNDISABLE_INIT))
  2376.   {
  2377.     assert_param(IS_SMARTCARD_OVERRUN(hsmartcard->AdvancedInit.OverrunDisable));
  2378.     MODIFY_REG(hsmartcard->Instance->CR3, USART_CR3_OVRDIS, hsmartcard->AdvancedInit.OverrunDisable);
  2379.   }
  2380.  
  2381.   /* if required, configure DMA disabling on reception error */
  2382.   if (HAL_IS_BIT_SET(hsmartcard->AdvancedInit.AdvFeatureInit, SMARTCARD_ADVFEATURE_DMADISABLEONERROR_INIT))
  2383.   {
  2384.     assert_param(IS_SMARTCARD_ADVFEATURE_DMAONRXERROR(hsmartcard->AdvancedInit.DMADisableonRxError));
  2385.     MODIFY_REG(hsmartcard->Instance->CR3, USART_CR3_DDRE, hsmartcard->AdvancedInit.DMADisableonRxError);
  2386.   }
  2387.  
  2388.   /* if required, configure MSB first on communication line */
  2389.   if (HAL_IS_BIT_SET(hsmartcard->AdvancedInit.AdvFeatureInit, SMARTCARD_ADVFEATURE_MSBFIRST_INIT))
  2390.   {
  2391.     assert_param(IS_SMARTCARD_ADVFEATURE_MSBFIRST(hsmartcard->AdvancedInit.MSBFirst));
  2392.     MODIFY_REG(hsmartcard->Instance->CR2, USART_CR2_MSBFIRST, hsmartcard->AdvancedInit.MSBFirst);
  2393.   }
  2394.  
  2395. }
  2396.  
  2397. /**
  2398.   * @brief Check the SMARTCARD Idle State.
  2399.   * @param hsmartcard Pointer to a SMARTCARD_HandleTypeDef structure that contains
  2400.   *                   the configuration information for the specified SMARTCARD module.
  2401.   * @retval HAL status
  2402.   */
  2403. static HAL_StatusTypeDef SMARTCARD_CheckIdleState(SMARTCARD_HandleTypeDef *hsmartcard)
  2404. {
  2405.   uint32_t tickstart;
  2406.  
  2407.   /* Initialize the SMARTCARD ErrorCode */
  2408.   hsmartcard->ErrorCode = HAL_SMARTCARD_ERROR_NONE;
  2409.  
  2410.   /* Init tickstart for timeout management */
  2411.   tickstart = HAL_GetTick();
  2412.  
  2413.   /* Check if the Transmitter is enabled */
  2414.   if ((hsmartcard->Instance->CR1 & USART_CR1_TE) == USART_CR1_TE)
  2415.   {
  2416.     /* Wait until TEACK flag is set */
  2417.     if (SMARTCARD_WaitOnFlagUntilTimeout(hsmartcard, USART_ISR_TEACK, RESET, tickstart,
  2418.                                          SMARTCARD_TEACK_REACK_TIMEOUT) != HAL_OK)
  2419.     {
  2420.       /* Timeout occurred */
  2421.       return HAL_TIMEOUT;
  2422.     }
  2423.   }
  2424.   /* Check if the Receiver is enabled */
  2425.   if ((hsmartcard->Instance->CR1 & USART_CR1_RE) == USART_CR1_RE)
  2426.   {
  2427.     /* Wait until REACK flag is set */
  2428.     if (SMARTCARD_WaitOnFlagUntilTimeout(hsmartcard, USART_ISR_REACK, RESET, tickstart,
  2429.                                          SMARTCARD_TEACK_REACK_TIMEOUT) != HAL_OK)
  2430.     {
  2431.       /* Timeout occurred */
  2432.       return HAL_TIMEOUT;
  2433.     }
  2434.   }
  2435.  
  2436.   /* Initialize the SMARTCARD states */
  2437.   hsmartcard->gState  = HAL_SMARTCARD_STATE_READY;
  2438.   hsmartcard->RxState = HAL_SMARTCARD_STATE_READY;
  2439.  
  2440.   /* Process Unlocked */
  2441.   __HAL_UNLOCK(hsmartcard);
  2442.  
  2443.   return HAL_OK;
  2444. }
  2445.  
  2446. /**
  2447.   * @brief  Handle SMARTCARD Communication Timeout.
  2448.   * @param  hsmartcard Pointer to a SMARTCARD_HandleTypeDef structure that contains
  2449.   *                   the configuration information for the specified SMARTCARD module.
  2450.   * @param  Flag Specifies the SMARTCARD flag to check.
  2451.   * @param  Status The new Flag status (SET or RESET).
  2452.   * @param  Tickstart Tick start value
  2453.   * @param  Timeout Timeout duration.
  2454.   * @retval HAL status
  2455.   */
  2456. static HAL_StatusTypeDef SMARTCARD_WaitOnFlagUntilTimeout(SMARTCARD_HandleTypeDef *hsmartcard, uint32_t Flag,
  2457.                                                           FlagStatus Status, uint32_t Tickstart, uint32_t Timeout)
  2458. {
  2459.   /* Wait until flag is set */
  2460.   while ((__HAL_SMARTCARD_GET_FLAG(hsmartcard, Flag) ? SET : RESET) == Status)
  2461.   {
  2462.     /* Check for the Timeout */
  2463.     if (Timeout != HAL_MAX_DELAY)
  2464.     {
  2465.       if (((HAL_GetTick() - Tickstart) > Timeout) || (Timeout == 0U))
  2466.       {
  2467.         /* Disable TXE, RXNE, PE and ERR (Frame error, noise error, overrun error)
  2468.            interrupts for the interrupt process */
  2469.         CLEAR_BIT(hsmartcard->Instance->CR1, (USART_CR1_RXNEIE | USART_CR1_PEIE | USART_CR1_TXEIE));
  2470.         CLEAR_BIT(hsmartcard->Instance->CR3, USART_CR3_EIE);
  2471.  
  2472.         hsmartcard->gState  = HAL_SMARTCARD_STATE_READY;
  2473.         hsmartcard->RxState = HAL_SMARTCARD_STATE_READY;
  2474.  
  2475.         /* Process Unlocked */
  2476.         __HAL_UNLOCK(hsmartcard);
  2477.         return HAL_TIMEOUT;
  2478.       }
  2479.     }
  2480.   }
  2481.   return HAL_OK;
  2482. }
  2483.  
  2484.  
  2485. /**
  2486.   * @brief  End ongoing Tx transfer on SMARTCARD peripheral (following error detection or Transmit completion).
  2487.   * @param  hsmartcard Pointer to a SMARTCARD_HandleTypeDef structure that contains
  2488.   *                    the configuration information for the specified SMARTCARD module.
  2489.   * @retval None
  2490.   */
  2491. static void SMARTCARD_EndTxTransfer(SMARTCARD_HandleTypeDef *hsmartcard)
  2492. {
  2493.   /* Disable TXEIE, TCIE and ERR (Frame error, noise error, overrun error) interrupts */
  2494.   CLEAR_BIT(hsmartcard->Instance->CR1, (USART_CR1_TXEIE | USART_CR1_TCIE));
  2495.   CLEAR_BIT(hsmartcard->Instance->CR3, USART_CR3_EIE);
  2496.  
  2497.   /* At end of Tx process, restore hsmartcard->gState to Ready */
  2498.   hsmartcard->gState = HAL_SMARTCARD_STATE_READY;
  2499. }
  2500.  
  2501.  
  2502. /**
  2503.   * @brief  End ongoing Rx transfer on UART peripheral (following error detection or Reception completion).
  2504.   * @param  hsmartcard Pointer to a SMARTCARD_HandleTypeDef structure that contains
  2505.   *                    the configuration information for the specified SMARTCARD module.
  2506.   * @retval None
  2507.   */
  2508. static void SMARTCARD_EndRxTransfer(SMARTCARD_HandleTypeDef *hsmartcard)
  2509. {
  2510.   /* Disable RXNE, PE and ERR (Frame error, noise error, overrun error) interrupts */
  2511.   CLEAR_BIT(hsmartcard->Instance->CR1, (USART_CR1_RXNEIE | USART_CR1_PEIE));
  2512.   CLEAR_BIT(hsmartcard->Instance->CR3, USART_CR3_EIE);
  2513.  
  2514.   /* At end of Rx process, restore hsmartcard->RxState to Ready */
  2515.   hsmartcard->RxState = HAL_SMARTCARD_STATE_READY;
  2516. }
  2517.  
  2518.  
  2519. /**
  2520.   * @brief  DMA SMARTCARD transmit process complete callback.
  2521.   * @param  hdma Pointer to a DMA_HandleTypeDef structure that contains
  2522.   *              the configuration information for the specified DMA module.
  2523.   * @retval None
  2524.   */
  2525. static void SMARTCARD_DMATransmitCplt(DMA_HandleTypeDef *hdma)
  2526. {
  2527.   SMARTCARD_HandleTypeDef *hsmartcard = (SMARTCARD_HandleTypeDef *)(hdma->Parent);
  2528.   hsmartcard->TxXferCount = 0U;
  2529.  
  2530.   /* Disable the DMA transfer for transmit request by resetting the DMAT bit
  2531.   in the SMARTCARD associated USART CR3 register */
  2532.   CLEAR_BIT(hsmartcard->Instance->CR3, USART_CR3_DMAT);
  2533.  
  2534.   /* Enable the SMARTCARD Transmit Complete Interrupt */
  2535.   __HAL_SMARTCARD_ENABLE_IT(hsmartcard, hsmartcard->AdvancedInit.TxCompletionIndication);
  2536. }
  2537.  
  2538. /**
  2539.   * @brief  DMA SMARTCARD receive process complete callback.
  2540.   * @param  hdma Pointer to a DMA_HandleTypeDef structure that contains
  2541.   *              the configuration information for the specified DMA module.
  2542.   * @retval None
  2543.   */
  2544. static void SMARTCARD_DMAReceiveCplt(DMA_HandleTypeDef *hdma)
  2545. {
  2546.   SMARTCARD_HandleTypeDef *hsmartcard = (SMARTCARD_HandleTypeDef *)(hdma->Parent);
  2547.   hsmartcard->RxXferCount = 0U;
  2548.  
  2549.   /* Disable PE and ERR (Frame error, noise error, overrun error) interrupts */
  2550.   CLEAR_BIT(hsmartcard->Instance->CR1, USART_CR1_PEIE);
  2551.   CLEAR_BIT(hsmartcard->Instance->CR3, USART_CR3_EIE);
  2552.  
  2553.   /* Disable the DMA transfer for the receiver request by resetting the DMAR bit
  2554.      in the SMARTCARD associated USART CR3 register */
  2555.   CLEAR_BIT(hsmartcard->Instance->CR3, USART_CR3_DMAR);
  2556.  
  2557.   /* At end of Rx process, restore hsmartcard->RxState to Ready */
  2558.   hsmartcard->RxState = HAL_SMARTCARD_STATE_READY;
  2559.  
  2560. #if (USE_HAL_SMARTCARD_REGISTER_CALLBACKS == 1)
  2561.   /* Call registered Rx complete callback */
  2562.   hsmartcard->RxCpltCallback(hsmartcard);
  2563. #else
  2564.   /* Call legacy weak Rx complete callback */
  2565.   HAL_SMARTCARD_RxCpltCallback(hsmartcard);
  2566. #endif /* USE_HAL_SMARTCARD_REGISTER_CALLBACK */
  2567. }
  2568.  
  2569. /**
  2570.   * @brief  DMA SMARTCARD communication error callback.
  2571.   * @param  hdma Pointer to a DMA_HandleTypeDef structure that contains
  2572.   *              the configuration information for the specified DMA module.
  2573.   * @retval None
  2574.   */
  2575. static void SMARTCARD_DMAError(DMA_HandleTypeDef *hdma)
  2576. {
  2577.   SMARTCARD_HandleTypeDef *hsmartcard = (SMARTCARD_HandleTypeDef *)(hdma->Parent);
  2578.  
  2579.   /* Stop SMARTCARD DMA Tx request if ongoing */
  2580.   if (hsmartcard->gState == HAL_SMARTCARD_STATE_BUSY_TX)
  2581.   {
  2582.     if (HAL_IS_BIT_SET(hsmartcard->Instance->CR3, USART_CR3_DMAT))
  2583.     {
  2584.       hsmartcard->TxXferCount = 0U;
  2585.       SMARTCARD_EndTxTransfer(hsmartcard);
  2586.     }
  2587.   }
  2588.  
  2589.   /* Stop SMARTCARD DMA Rx request if ongoing */
  2590.   if (hsmartcard->RxState == HAL_SMARTCARD_STATE_BUSY_RX)
  2591.   {
  2592.     if (HAL_IS_BIT_SET(hsmartcard->Instance->CR3, USART_CR3_DMAR))
  2593.     {
  2594.       hsmartcard->RxXferCount = 0U;
  2595.       SMARTCARD_EndRxTransfer(hsmartcard);
  2596.     }
  2597.   }
  2598.  
  2599.   hsmartcard->ErrorCode |= HAL_SMARTCARD_ERROR_DMA;
  2600. #if (USE_HAL_SMARTCARD_REGISTER_CALLBACKS == 1)
  2601.   /* Call registered user error callback */
  2602.   hsmartcard->ErrorCallback(hsmartcard);
  2603. #else
  2604.   /* Call legacy weak user error callback */
  2605.   HAL_SMARTCARD_ErrorCallback(hsmartcard);
  2606. #endif /* USE_HAL_SMARTCARD_REGISTER_CALLBACK */
  2607. }
  2608.  
  2609. /**
  2610.   * @brief  DMA SMARTCARD communication abort callback, when initiated by HAL services on Error
  2611.   *         (To be called at end of DMA Abort procedure following error occurrence).
  2612.   * @param  hdma DMA handle.
  2613.   * @retval None
  2614.   */
  2615. static void SMARTCARD_DMAAbortOnError(DMA_HandleTypeDef *hdma)
  2616. {
  2617.   SMARTCARD_HandleTypeDef *hsmartcard = (SMARTCARD_HandleTypeDef *)(hdma->Parent);
  2618.   hsmartcard->RxXferCount = 0U;
  2619.   hsmartcard->TxXferCount = 0U;
  2620.  
  2621. #if (USE_HAL_SMARTCARD_REGISTER_CALLBACKS == 1)
  2622.   /* Call registered user error callback */
  2623.   hsmartcard->ErrorCallback(hsmartcard);
  2624. #else
  2625.   /* Call legacy weak user error callback */
  2626.   HAL_SMARTCARD_ErrorCallback(hsmartcard);
  2627. #endif /* USE_HAL_SMARTCARD_REGISTER_CALLBACK */
  2628. }
  2629.  
  2630. /**
  2631.   * @brief  DMA SMARTCARD Tx communication abort callback, when initiated by user
  2632.   *         (To be called at end of DMA Tx Abort procedure following user abort request).
  2633.   * @note   When this callback is executed, User Abort complete call back is called only if no
  2634.   *         Abort still ongoing for Rx DMA Handle.
  2635.   * @param  hdma DMA handle.
  2636.   * @retval None
  2637.   */
  2638. static void SMARTCARD_DMATxAbortCallback(DMA_HandleTypeDef *hdma)
  2639. {
  2640.   SMARTCARD_HandleTypeDef *hsmartcard = (SMARTCARD_HandleTypeDef *)(hdma->Parent);
  2641.  
  2642.   hsmartcard->hdmatx->XferAbortCallback = NULL;
  2643.  
  2644.   /* Check if an Abort process is still ongoing */
  2645.   if (hsmartcard->hdmarx != NULL)
  2646.   {
  2647.     if (hsmartcard->hdmarx->XferAbortCallback != NULL)
  2648.     {
  2649.       return;
  2650.     }
  2651.   }
  2652.  
  2653.   /* No Abort process still ongoing : All DMA channels are aborted, call user Abort Complete callback */
  2654.   hsmartcard->TxXferCount = 0U;
  2655.   hsmartcard->RxXferCount = 0U;
  2656.  
  2657.   /* Reset errorCode */
  2658.   hsmartcard->ErrorCode = HAL_SMARTCARD_ERROR_NONE;
  2659.  
  2660.   /* Clear the Error flags in the ICR register */
  2661.   __HAL_SMARTCARD_CLEAR_FLAG(hsmartcard,
  2662.                              SMARTCARD_CLEAR_OREF | SMARTCARD_CLEAR_NEF | SMARTCARD_CLEAR_PEF | SMARTCARD_CLEAR_FEF |
  2663.                              SMARTCARD_CLEAR_RTOF | SMARTCARD_CLEAR_EOBF);
  2664.  
  2665.   /* Restore hsmartcard->gState and hsmartcard->RxState to Ready */
  2666.   hsmartcard->gState  = HAL_SMARTCARD_STATE_READY;
  2667.   hsmartcard->RxState = HAL_SMARTCARD_STATE_READY;
  2668.  
  2669. #if (USE_HAL_SMARTCARD_REGISTER_CALLBACKS == 1)
  2670.   /* Call registered Abort complete callback */
  2671.   hsmartcard->AbortCpltCallback(hsmartcard);
  2672. #else
  2673.   /* Call legacy weak Abort complete callback */
  2674.   HAL_SMARTCARD_AbortCpltCallback(hsmartcard);
  2675. #endif /* USE_HAL_SMARTCARD_REGISTER_CALLBACK */
  2676. }
  2677.  
  2678.  
  2679. /**
  2680.   * @brief  DMA SMARTCARD Rx communication abort callback, when initiated by user
  2681.   *         (To be called at end of DMA Rx Abort procedure following user abort request).
  2682.   * @note   When this callback is executed, User Abort complete call back is called only if no
  2683.   *         Abort still ongoing for Tx DMA Handle.
  2684.   * @param  hdma DMA handle.
  2685.   * @retval None
  2686.   */
  2687. static void SMARTCARD_DMARxAbortCallback(DMA_HandleTypeDef *hdma)
  2688. {
  2689.   SMARTCARD_HandleTypeDef *hsmartcard = (SMARTCARD_HandleTypeDef *)(hdma->Parent);
  2690.  
  2691.   hsmartcard->hdmarx->XferAbortCallback = NULL;
  2692.  
  2693.   /* Check if an Abort process is still ongoing */
  2694.   if (hsmartcard->hdmatx != NULL)
  2695.   {
  2696.     if (hsmartcard->hdmatx->XferAbortCallback != NULL)
  2697.     {
  2698.       return;
  2699.     }
  2700.   }
  2701.  
  2702.   /* No Abort process still ongoing : All DMA channels are aborted, call user Abort Complete callback */
  2703.   hsmartcard->TxXferCount = 0U;
  2704.   hsmartcard->RxXferCount = 0U;
  2705.  
  2706.   /* Reset errorCode */
  2707.   hsmartcard->ErrorCode = HAL_SMARTCARD_ERROR_NONE;
  2708.  
  2709.   /* Clear the Error flags in the ICR register */
  2710.   __HAL_SMARTCARD_CLEAR_FLAG(hsmartcard,
  2711.                              SMARTCARD_CLEAR_OREF | SMARTCARD_CLEAR_NEF | SMARTCARD_CLEAR_PEF | SMARTCARD_CLEAR_FEF |
  2712.                              SMARTCARD_CLEAR_RTOF | SMARTCARD_CLEAR_EOBF);
  2713.  
  2714.   /* Restore hsmartcard->gState and hsmartcard->RxState to Ready */
  2715.   hsmartcard->gState  = HAL_SMARTCARD_STATE_READY;
  2716.   hsmartcard->RxState = HAL_SMARTCARD_STATE_READY;
  2717.  
  2718. #if (USE_HAL_SMARTCARD_REGISTER_CALLBACKS == 1)
  2719.   /* Call registered Abort complete callback */
  2720.   hsmartcard->AbortCpltCallback(hsmartcard);
  2721. #else
  2722.   /* Call legacy weak Abort complete callback */
  2723.   HAL_SMARTCARD_AbortCpltCallback(hsmartcard);
  2724. #endif /* USE_HAL_SMARTCARD_REGISTER_CALLBACK */
  2725. }
  2726.  
  2727.  
  2728. /**
  2729.   * @brief  DMA SMARTCARD Tx communication abort callback, when initiated by user by a call to
  2730.   *         HAL_SMARTCARD_AbortTransmit_IT API (Abort only Tx transfer)
  2731.   *         (This callback is executed at end of DMA Tx Abort procedure following user abort request,
  2732.   *         and leads to user Tx Abort Complete callback execution).
  2733.   * @param  hdma DMA handle.
  2734.   * @retval None
  2735.   */
  2736. static void SMARTCARD_DMATxOnlyAbortCallback(DMA_HandleTypeDef *hdma)
  2737. {
  2738.   SMARTCARD_HandleTypeDef *hsmartcard = (SMARTCARD_HandleTypeDef *)(hdma->Parent);
  2739.  
  2740.   hsmartcard->TxXferCount = 0U;
  2741.  
  2742.   /* Clear the Error flags in the ICR register */
  2743.   __HAL_SMARTCARD_CLEAR_FLAG(hsmartcard, SMARTCARD_CLEAR_FEF);
  2744.  
  2745.   /* Restore hsmartcard->gState to Ready */
  2746.   hsmartcard->gState = HAL_SMARTCARD_STATE_READY;
  2747.  
  2748. #if (USE_HAL_SMARTCARD_REGISTER_CALLBACKS == 1)
  2749.   /* Call registered Abort Transmit Complete Callback */
  2750.   hsmartcard->AbortTransmitCpltCallback(hsmartcard);
  2751. #else
  2752.   /* Call legacy weak Abort Transmit Complete Callback */
  2753.   HAL_SMARTCARD_AbortTransmitCpltCallback(hsmartcard);
  2754. #endif /* USE_HAL_SMARTCARD_REGISTER_CALLBACK */
  2755. }
  2756.  
  2757. /**
  2758.   * @brief  DMA SMARTCARD Rx communication abort callback, when initiated by user by a call to
  2759.   *         HAL_SMARTCARD_AbortReceive_IT API (Abort only Rx transfer)
  2760.   *         (This callback is executed at end of DMA Rx Abort procedure following user abort request,
  2761.   *         and leads to user Rx Abort Complete callback execution).
  2762.   * @param  hdma DMA handle.
  2763.   * @retval None
  2764.   */
  2765. static void SMARTCARD_DMARxOnlyAbortCallback(DMA_HandleTypeDef *hdma)
  2766. {
  2767.   SMARTCARD_HandleTypeDef *hsmartcard = (SMARTCARD_HandleTypeDef *)(hdma->Parent);
  2768.  
  2769.   hsmartcard->RxXferCount = 0U;
  2770.  
  2771.   /* Clear the Error flags in the ICR register */
  2772.   __HAL_SMARTCARD_CLEAR_FLAG(hsmartcard,
  2773.                              SMARTCARD_CLEAR_OREF | SMARTCARD_CLEAR_NEF | SMARTCARD_CLEAR_PEF | SMARTCARD_CLEAR_FEF |
  2774.                              SMARTCARD_CLEAR_RTOF | SMARTCARD_CLEAR_EOBF);
  2775.  
  2776.   /* Restore hsmartcard->RxState to Ready */
  2777.   hsmartcard->RxState = HAL_SMARTCARD_STATE_READY;
  2778.  
  2779. #if (USE_HAL_SMARTCARD_REGISTER_CALLBACKS == 1)
  2780.   /* Call registered Abort Receive Complete Callback */
  2781.   hsmartcard->AbortReceiveCpltCallback(hsmartcard);
  2782. #else
  2783.   /* Call legacy weak Abort Receive Complete Callback */
  2784.   HAL_SMARTCARD_AbortReceiveCpltCallback(hsmartcard);
  2785. #endif /* USE_HAL_SMARTCARD_REGISTER_CALLBACK */
  2786. }
  2787.  
  2788. /**
  2789.   * @brief  Send an amount of data in non-blocking mode.
  2790.   * @note   Function called under interruption only, once
  2791.   *         interruptions have been enabled by HAL_SMARTCARD_Transmit_IT().
  2792.   * @param  hsmartcard Pointer to a SMARTCARD_HandleTypeDef structure that contains
  2793.   *                    the configuration information for the specified SMARTCARD module.
  2794.   * @retval None
  2795.   */
  2796. static void SMARTCARD_TxISR(SMARTCARD_HandleTypeDef *hsmartcard)
  2797. {
  2798.   /* Check that a Tx process is ongoing */
  2799.   if (hsmartcard->gState == HAL_SMARTCARD_STATE_BUSY_TX)
  2800.   {
  2801.     if (hsmartcard->TxXferCount == 0U)
  2802.     {
  2803.       /* Disable the SMARTCARD Transmit Data Register Empty Interrupt */
  2804.       CLEAR_BIT(hsmartcard->Instance->CR1, USART_CR1_TXEIE);
  2805.  
  2806.       /* Enable the SMARTCARD Transmit Complete Interrupt */
  2807.       __HAL_SMARTCARD_ENABLE_IT(hsmartcard, hsmartcard->AdvancedInit.TxCompletionIndication);
  2808.     }
  2809.     else
  2810.     {
  2811.       hsmartcard->Instance->TDR = (uint8_t)(*hsmartcard->pTxBuffPtr & 0xFFU);
  2812.       hsmartcard->pTxBuffPtr++;
  2813.       hsmartcard->TxXferCount--;
  2814.     }
  2815.   }
  2816. }
  2817.  
  2818. /**
  2819.   * @brief  Wrap up transmission in non-blocking mode.
  2820.   * @param  hsmartcard Pointer to a SMARTCARD_HandleTypeDef structure that contains
  2821.   *                    the configuration information for the specified SMARTCARD module.
  2822.   * @retval None
  2823.   */
  2824. static void SMARTCARD_EndTransmit_IT(SMARTCARD_HandleTypeDef *hsmartcard)
  2825. {
  2826.   /* Disable the SMARTCARD Transmit Complete Interrupt */
  2827.   __HAL_SMARTCARD_DISABLE_IT(hsmartcard, hsmartcard->AdvancedInit.TxCompletionIndication);
  2828.  
  2829.   /* Check if a receive process is ongoing or not. If not disable ERR IT */
  2830.   if (hsmartcard->RxState == HAL_SMARTCARD_STATE_READY)
  2831.   {
  2832.     /* Disable the SMARTCARD Error Interrupt: (Frame error) */
  2833.     CLEAR_BIT(hsmartcard->Instance->CR3, USART_CR3_EIE);
  2834.   }
  2835.  
  2836.   /* Disable the Peripheral first to update mode */
  2837.   CLEAR_BIT(hsmartcard->Instance->CR1, USART_CR1_UE);
  2838.   if ((hsmartcard->Init.Mode == SMARTCARD_MODE_TX)
  2839.       && (hsmartcard->Init.NACKEnable == SMARTCARD_NACK_ENABLE))
  2840.   {
  2841.     /* In case of TX only mode, if NACK is enabled, receiver block has been enabled
  2842.        for Transmit phase. Disable this receiver block. */
  2843.     CLEAR_BIT(hsmartcard->Instance->CR1, USART_CR1_RE);
  2844.   }
  2845.   if ((hsmartcard->Init.Mode == SMARTCARD_MODE_TX_RX)
  2846.       || (hsmartcard->Init.NACKEnable == SMARTCARD_NACK_ENABLE))
  2847.   {
  2848.     /* Perform a TX FIFO Flush at end of Tx phase, as all sent bytes are appearing in Rx Data register */
  2849.     __HAL_SMARTCARD_FLUSH_DRREGISTER(hsmartcard);
  2850.   }
  2851.   SET_BIT(hsmartcard->Instance->CR1, USART_CR1_UE);
  2852.  
  2853.   /* Tx process is ended, restore hsmartcard->gState to Ready */
  2854.   hsmartcard->gState = HAL_SMARTCARD_STATE_READY;
  2855.  
  2856.   /* Clear TxISR function pointer */
  2857.   hsmartcard->TxISR = NULL;
  2858.  
  2859. #if (USE_HAL_SMARTCARD_REGISTER_CALLBACKS == 1)
  2860.   /* Call registered Tx complete callback */
  2861.   hsmartcard->TxCpltCallback(hsmartcard);
  2862. #else
  2863.   /* Call legacy weak Tx complete callback */
  2864.   HAL_SMARTCARD_TxCpltCallback(hsmartcard);
  2865. #endif /* USE_HAL_SMARTCARD_REGISTER_CALLBACK */
  2866. }
  2867.  
  2868. /**
  2869.   * @brief  Receive an amount of data in non-blocking mode.
  2870.   * @note   Function called under interruption only, once
  2871.   *         interruptions have been enabled by HAL_SMARTCARD_Receive_IT().
  2872.   * @param  hsmartcard Pointer to a SMARTCARD_HandleTypeDef structure that contains
  2873.   *                    the configuration information for the specified SMARTCARD module.
  2874.   * @retval None
  2875.   */
  2876. static void SMARTCARD_RxISR(SMARTCARD_HandleTypeDef *hsmartcard)
  2877. {
  2878.   /* Check that a Rx process is ongoing */
  2879.   if (hsmartcard->RxState == HAL_SMARTCARD_STATE_BUSY_RX)
  2880.   {
  2881.     *hsmartcard->pRxBuffPtr = (uint8_t)(hsmartcard->Instance->RDR & (uint8_t)0xFF);
  2882.     hsmartcard->pRxBuffPtr++;
  2883.  
  2884.     hsmartcard->RxXferCount--;
  2885.     if (hsmartcard->RxXferCount == 0U)
  2886.     {
  2887.       CLEAR_BIT(hsmartcard->Instance->CR1, USART_CR1_RXNEIE);
  2888.  
  2889.       /* Check if a transmit process is ongoing or not. If not disable ERR IT */
  2890.       if (hsmartcard->gState == HAL_SMARTCARD_STATE_READY)
  2891.       {
  2892.         /* Disable the SMARTCARD Error Interrupt: (Frame error, noise error, overrun error) */
  2893.         CLEAR_BIT(hsmartcard->Instance->CR3, USART_CR3_EIE);
  2894.       }
  2895.  
  2896.       /* Disable the SMARTCARD Parity Error Interrupt */
  2897.       CLEAR_BIT(hsmartcard->Instance->CR1, USART_CR1_PEIE);
  2898.  
  2899.       hsmartcard->RxState = HAL_SMARTCARD_STATE_READY;
  2900.  
  2901.       /* Clear RxISR function pointer */
  2902.       hsmartcard->RxISR = NULL;
  2903.  
  2904. #if (USE_HAL_SMARTCARD_REGISTER_CALLBACKS == 1)
  2905.       /* Call registered Rx complete callback */
  2906.       hsmartcard->RxCpltCallback(hsmartcard);
  2907. #else
  2908.       /* Call legacy weak Rx complete callback */
  2909.       HAL_SMARTCARD_RxCpltCallback(hsmartcard);
  2910. #endif /* USE_HAL_SMARTCARD_REGISTER_CALLBACK */
  2911.     }
  2912.   }
  2913.   else
  2914.   {
  2915.     /* Clear RXNE interrupt flag */
  2916.     __HAL_SMARTCARD_SEND_REQ(hsmartcard, SMARTCARD_RXDATA_FLUSH_REQUEST);
  2917.   }
  2918. }
  2919.  
  2920. /**
  2921.   * @}
  2922.   */
  2923.  
  2924. #endif /* HAL_SMARTCARD_MODULE_ENABLED */
  2925. /**
  2926.   * @}
  2927.   */
  2928.  
  2929. /**
  2930.   * @}
  2931.   */
  2932. #endif /* !defined(STM32F030x6) && !defined(STM32F030x8) && !defined(STM32F070x6) && !defined(STM32F070xB) && !defined(STM32F030xC)  */
  2933. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
  2934.