source: trunk/firmware/Drivers/STM32G0xx_HAL_Driver/Src/stm32g0xx_hal_smartcard.c

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