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