1 /**
2   ******************************************************************************
3   * @file    stm32h5xx_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) 2023 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 "stm32h5xx_hal.h"
185 
186 /** @addtogroup STM32H5xx_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 #if defined(USART_DMAREQUESTS_SW_WA)
832     /* Disable the USART DMA Tx request if enabled */
833     if (HAL_IS_BIT_SET(hsmartcard->Instance->CR3, USART_CR3_DMAT))
834     {
835       CLEAR_BIT(hsmartcard->Instance->CR3, USART_CR3_DMAT);
836     }
837 
838 #endif /* USART_DMAREQUESTS_SW_WA */
839     /* Disable the Peripheral first to update mode for TX master */
840     CLEAR_BIT(hsmartcard->Instance->CR1, USART_CR1_UE);
841 
842     /* In case of TX only mode, if NACK is enabled, the USART must be able to monitor
843        the bidirectional line to detect a NACK signal in case of parity error.
844        Therefore, the receiver block must be enabled as well (RE bit must be set). */
845     if ((hsmartcard->Init.Mode == SMARTCARD_MODE_TX)
846         && (hsmartcard->Init.NACKEnable == SMARTCARD_NACK_ENABLE))
847     {
848       SET_BIT(hsmartcard->Instance->CR1, USART_CR1_RE);
849     }
850     /* Enable Tx */
851     SET_BIT(hsmartcard->Instance->CR1, USART_CR1_TE);
852 
853     /* Enable the Peripheral */
854     SET_BIT(hsmartcard->Instance->CR1, USART_CR1_UE);
855 
856     /* Perform a TX/RX FIFO Flush */
857     __HAL_SMARTCARD_FLUSH_DRREGISTER(hsmartcard);
858 
859     hsmartcard->ErrorCode = HAL_SMARTCARD_ERROR_NONE;
860     hsmartcard->TxXferSize = Size;
861     hsmartcard->TxXferCount = Size;
862 
863     while (hsmartcard->TxXferCount > 0U)
864     {
865       hsmartcard->TxXferCount--;
866       if (SMARTCARD_WaitOnFlagUntilTimeout(hsmartcard, SMARTCARD_FLAG_TXE, RESET, tickstart, Timeout) != HAL_OK)
867       {
868         return HAL_TIMEOUT;
869       }
870       hsmartcard->Instance->TDR = (uint8_t)(*ptmpdata & 0xFFU);
871       ptmpdata++;
872     }
873     if (SMARTCARD_WaitOnFlagUntilTimeout(hsmartcard, SMARTCARD_TRANSMISSION_COMPLETION_FLAG(hsmartcard), RESET,
874                                          tickstart, Timeout) != HAL_OK)
875     {
876       return HAL_TIMEOUT;
877     }
878 
879     /* Disable the Peripheral first to update mode */
880     CLEAR_BIT(hsmartcard->Instance->CR1, USART_CR1_UE);
881     if ((hsmartcard->Init.Mode == SMARTCARD_MODE_TX)
882         && (hsmartcard->Init.NACKEnable == SMARTCARD_NACK_ENABLE))
883     {
884       /* In case of TX only mode, if NACK is enabled, receiver block has been enabled
885          for Transmit phase. Disable this receiver block. */
886       CLEAR_BIT(hsmartcard->Instance->CR1, USART_CR1_RE);
887     }
888     if ((hsmartcard->Init.Mode == SMARTCARD_MODE_TX_RX)
889         || (hsmartcard->Init.NACKEnable == SMARTCARD_NACK_ENABLE))
890     {
891       /* Perform a TX FIFO Flush at end of Tx phase, as all sent bytes are appearing in Rx Data register */
892       __HAL_SMARTCARD_FLUSH_DRREGISTER(hsmartcard);
893     }
894     SET_BIT(hsmartcard->Instance->CR1, USART_CR1_UE);
895 
896     /* At end of Tx process, restore hsmartcard->gState to Ready */
897     hsmartcard->gState = HAL_SMARTCARD_STATE_READY;
898 
899     /* Process Unlocked */
900     __HAL_UNLOCK(hsmartcard);
901 
902     return HAL_OK;
903   }
904   else
905   {
906     return HAL_BUSY;
907   }
908 }
909 
910 /**
911   * @brief  Receive an amount of data in blocking mode.
912   * @note   When FIFO mode is enabled, the RXFNE flag is set as long as the RXFIFO
913   *         is not empty. Read operations from the RDR register are performed when
914   *         RXFNE flag is set. From hardware perspective, RXFNE flag and
915   *         RXNE are mapped on the same bit-field.
916   * @param  hsmartcard Pointer to a SMARTCARD_HandleTypeDef structure that contains
917   *                    the configuration information for the specified SMARTCARD module.
918   * @param  pData pointer to data buffer.
919   * @param  Size amount of data to be received.
920   * @param  Timeout Timeout duration.
921   * @retval HAL status
922   */
HAL_SMARTCARD_Receive(SMARTCARD_HandleTypeDef * hsmartcard,uint8_t * pData,uint16_t Size,uint32_t Timeout)923 HAL_StatusTypeDef HAL_SMARTCARD_Receive(SMARTCARD_HandleTypeDef *hsmartcard, uint8_t *pData, uint16_t Size,
924                                         uint32_t Timeout)
925 {
926   uint32_t tickstart;
927   uint8_t  *ptmpdata = pData;
928 
929   /* Check that a Rx process is not already ongoing */
930   if (hsmartcard->RxState == HAL_SMARTCARD_STATE_READY)
931   {
932     if ((ptmpdata == NULL) || (Size == 0U))
933     {
934       return  HAL_ERROR;
935     }
936 
937     /* Process Locked */
938     __HAL_LOCK(hsmartcard);
939 
940     hsmartcard->ErrorCode = HAL_SMARTCARD_ERROR_NONE;
941     hsmartcard->RxState   = HAL_SMARTCARD_STATE_BUSY_RX;
942 
943     /* Init tickstart for timeout management */
944     tickstart = HAL_GetTick();
945 
946 #if defined(USART_DMAREQUESTS_SW_WA)
947     /* Disable the USART DMA Tx request if enabled */
948     if (HAL_IS_BIT_SET(hsmartcard->Instance->CR3, USART_CR3_DMAR))
949     {
950       CLEAR_BIT(hsmartcard->Instance->CR3, USART_CR3_DMAR);
951     }
952 
953 #endif /* USART_DMAREQUESTS_SW_WA */
954     hsmartcard->RxXferSize = Size;
955     hsmartcard->RxXferCount = Size;
956 
957     /* Check the remain data to be received */
958     while (hsmartcard->RxXferCount > 0U)
959     {
960       hsmartcard->RxXferCount--;
961 
962       if (SMARTCARD_WaitOnFlagUntilTimeout(hsmartcard, SMARTCARD_FLAG_RXNE, RESET, tickstart, Timeout) != HAL_OK)
963       {
964         return HAL_TIMEOUT;
965       }
966       *ptmpdata = (uint8_t)(hsmartcard->Instance->RDR & (uint8_t)0x00FF);
967       ptmpdata++;
968     }
969 
970     /* At end of Rx process, restore hsmartcard->RxState to Ready */
971     hsmartcard->RxState = HAL_SMARTCARD_STATE_READY;
972 
973     /* Process Unlocked */
974     __HAL_UNLOCK(hsmartcard);
975 
976     return HAL_OK;
977   }
978   else
979   {
980     return HAL_BUSY;
981   }
982 }
983 
984 /**
985   * @brief  Send an amount of data in interrupt mode.
986   * @note   When FIFO mode is disabled, USART interrupt is generated whenever
987   *         USART_TDR register is empty, i.e one interrupt per data to transmit.
988   * @note   When FIFO mode is enabled, USART interrupt is generated whenever
989   *         TXFIFO threshold reached. In that case the interrupt rate depends on
990   *         TXFIFO threshold configuration.
991   * @note   This function sets the hsmartcard->TxIsr function pointer according to
992   *         the FIFO mode (data transmission processing depends on FIFO mode).
993   * @param  hsmartcard Pointer to a SMARTCARD_HandleTypeDef structure that contains
994   *                    the configuration information for the specified SMARTCARD module.
995   * @param  pData pointer to data buffer.
996   * @param  Size amount of data to be sent.
997   * @retval HAL status
998   */
HAL_SMARTCARD_Transmit_IT(SMARTCARD_HandleTypeDef * hsmartcard,const uint8_t * pData,uint16_t Size)999 HAL_StatusTypeDef HAL_SMARTCARD_Transmit_IT(SMARTCARD_HandleTypeDef *hsmartcard, const uint8_t *pData, uint16_t Size)
1000 {
1001   /* Check that a Tx process is not already ongoing */
1002   if (hsmartcard->gState == HAL_SMARTCARD_STATE_READY)
1003   {
1004     if ((pData == NULL) || (Size == 0U))
1005     {
1006       return HAL_ERROR;
1007     }
1008 
1009     /* Process Locked */
1010     __HAL_LOCK(hsmartcard);
1011 
1012     hsmartcard->ErrorCode = HAL_SMARTCARD_ERROR_NONE;
1013     hsmartcard->gState = HAL_SMARTCARD_STATE_BUSY_TX;
1014 
1015     hsmartcard->pTxBuffPtr  = pData;
1016     hsmartcard->TxXferSize  = Size;
1017     hsmartcard->TxXferCount = Size;
1018     hsmartcard->TxISR       = NULL;
1019 
1020 #if defined(USART_DMAREQUESTS_SW_WA)
1021     /* Disable the USART DMA Tx request if enabled */
1022     if (HAL_IS_BIT_SET(hsmartcard->Instance->CR3, USART_CR3_DMAT))
1023     {
1024       CLEAR_BIT(hsmartcard->Instance->CR3, USART_CR3_DMAT);
1025     }
1026 
1027 #endif /* USART_DMAREQUESTS_SW_WA */
1028     /* Disable the Peripheral first to update mode for TX master */
1029     CLEAR_BIT(hsmartcard->Instance->CR1, USART_CR1_UE);
1030 
1031     /* In case of TX only mode, if NACK is enabled, the USART must be able to monitor
1032        the bidirectional line to detect a NACK signal in case of parity error.
1033        Therefore, the receiver block must be enabled as well (RE bit must be set). */
1034     if ((hsmartcard->Init.Mode == SMARTCARD_MODE_TX)
1035         && (hsmartcard->Init.NACKEnable == SMARTCARD_NACK_ENABLE))
1036     {
1037       SET_BIT(hsmartcard->Instance->CR1, USART_CR1_RE);
1038     }
1039     /* Enable Tx */
1040     SET_BIT(hsmartcard->Instance->CR1, USART_CR1_TE);
1041 
1042     /* Enable the Peripheral */
1043     SET_BIT(hsmartcard->Instance->CR1, USART_CR1_UE);
1044 
1045     /* Perform a TX/RX FIFO Flush */
1046     __HAL_SMARTCARD_FLUSH_DRREGISTER(hsmartcard);
1047 
1048     /* Configure Tx interrupt processing */
1049     if (hsmartcard->FifoMode == SMARTCARD_FIFOMODE_ENABLE)
1050     {
1051       /* Set the Tx ISR function pointer */
1052       hsmartcard->TxISR = SMARTCARD_TxISR_FIFOEN;
1053 
1054       /* Process Unlocked */
1055       __HAL_UNLOCK(hsmartcard);
1056 
1057       /* Enable the SMARTCARD Error Interrupt: (Frame error) */
1058       SET_BIT(hsmartcard->Instance->CR3, USART_CR3_EIE);
1059 
1060       /* Enable the TX FIFO threshold interrupt */
1061       SET_BIT(hsmartcard->Instance->CR3, USART_CR3_TXFTIE);
1062     }
1063     else
1064     {
1065       /* Set the Tx ISR function pointer */
1066       hsmartcard->TxISR = SMARTCARD_TxISR;
1067 
1068       /* Process Unlocked */
1069       __HAL_UNLOCK(hsmartcard);
1070 
1071       /* Enable the SMARTCARD Error Interrupt: (Frame error) */
1072       SET_BIT(hsmartcard->Instance->CR3, USART_CR3_EIE);
1073 
1074       /* Enable the SMARTCARD Transmit Data Register Empty Interrupt */
1075       SET_BIT(hsmartcard->Instance->CR1, USART_CR1_TXEIE_TXFNFIE);
1076     }
1077 
1078     return HAL_OK;
1079   }
1080   else
1081   {
1082     return HAL_BUSY;
1083   }
1084 }
1085 
1086 /**
1087   * @brief  Receive an amount of data in interrupt mode.
1088   * @note   When FIFO mode is disabled, USART interrupt is generated whenever
1089   *         USART_RDR register can be read, i.e one interrupt per data to receive.
1090   * @note   When FIFO mode is enabled, USART interrupt is generated whenever
1091   *         RXFIFO threshold reached. In that case the interrupt rate depends on
1092   *         RXFIFO threshold configuration.
1093   * @note   This function sets the hsmartcard->RxIsr function pointer according to
1094   *         the FIFO mode (data reception processing depends on FIFO mode).
1095   * @param  hsmartcard Pointer to a SMARTCARD_HandleTypeDef structure that contains
1096   *                    the configuration information for the specified SMARTCARD module.
1097   * @param  pData pointer to data buffer.
1098   * @param  Size amount of data to be received.
1099   * @retval HAL status
1100   */
HAL_SMARTCARD_Receive_IT(SMARTCARD_HandleTypeDef * hsmartcard,uint8_t * pData,uint16_t Size)1101 HAL_StatusTypeDef HAL_SMARTCARD_Receive_IT(SMARTCARD_HandleTypeDef *hsmartcard, uint8_t *pData, uint16_t Size)
1102 {
1103   /* Check that a Rx process is not already ongoing */
1104   if (hsmartcard->RxState == HAL_SMARTCARD_STATE_READY)
1105   {
1106     if ((pData == NULL) || (Size == 0U))
1107     {
1108       return HAL_ERROR;
1109     }
1110 
1111     /* Process Locked */
1112     __HAL_LOCK(hsmartcard);
1113 
1114     hsmartcard->ErrorCode = HAL_SMARTCARD_ERROR_NONE;
1115     hsmartcard->RxState   = HAL_SMARTCARD_STATE_BUSY_RX;
1116 
1117     hsmartcard->pRxBuffPtr = pData;
1118     hsmartcard->RxXferSize = Size;
1119     hsmartcard->RxXferCount = Size;
1120 
1121 #if defined(USART_DMAREQUESTS_SW_WA)
1122     /* Disable the USART DMA Tx request if enabled */
1123     if (HAL_IS_BIT_SET(hsmartcard->Instance->CR3, USART_CR3_DMAR))
1124     {
1125       CLEAR_BIT(hsmartcard->Instance->CR3, USART_CR3_DMAR);
1126     }
1127 
1128 #endif /* USART_DMAREQUESTS_SW_WA */
1129     /* Configure Rx interrupt processing */
1130     if ((hsmartcard->FifoMode == SMARTCARD_FIFOMODE_ENABLE) && (Size >= hsmartcard->NbRxDataToProcess))
1131     {
1132       /* Set the Rx ISR function pointer */
1133       hsmartcard->RxISR = SMARTCARD_RxISR_FIFOEN;
1134 
1135       /* Process Unlocked */
1136       __HAL_UNLOCK(hsmartcard);
1137 
1138       /* Enable the SMARTCART Parity Error interrupt and RX FIFO Threshold interrupt */
1139       SET_BIT(hsmartcard->Instance->CR1, USART_CR1_PEIE);
1140       SET_BIT(hsmartcard->Instance->CR3, USART_CR3_RXFTIE);
1141     }
1142     else
1143     {
1144       /* Set the Rx ISR function pointer */
1145       hsmartcard->RxISR = SMARTCARD_RxISR;
1146 
1147       /* Process Unlocked */
1148       __HAL_UNLOCK(hsmartcard);
1149 
1150       /* Enable the SMARTCARD Parity Error and Data Register not empty Interrupts */
1151       SET_BIT(hsmartcard->Instance->CR1, USART_CR1_PEIE | USART_CR1_RXNEIE_RXFNEIE);
1152     }
1153 
1154     /* Enable the SMARTCARD Error Interrupt: (Frame error, noise error, overrun error) */
1155     SET_BIT(hsmartcard->Instance->CR3, USART_CR3_EIE);
1156 
1157     return HAL_OK;
1158   }
1159   else
1160   {
1161     return HAL_BUSY;
1162   }
1163 }
1164 
1165 #if defined(HAL_DMA_MODULE_ENABLED)
1166 /**
1167   * @brief  Send an amount of data in DMA mode.
1168   * @param  hsmartcard Pointer to a SMARTCARD_HandleTypeDef structure that contains
1169   *                    the configuration information for the specified SMARTCARD module.
1170   * @param  pData pointer to data buffer.
1171   * @param  Size amount of data to be sent.
1172   * @retval HAL status
1173   */
HAL_SMARTCARD_Transmit_DMA(SMARTCARD_HandleTypeDef * hsmartcard,const uint8_t * pData,uint16_t Size)1174 HAL_StatusTypeDef HAL_SMARTCARD_Transmit_DMA(SMARTCARD_HandleTypeDef *hsmartcard, const uint8_t *pData, uint16_t Size)
1175 {
1176   HAL_StatusTypeDef status;
1177 
1178   /* Check that a Tx process is not already ongoing */
1179   if (hsmartcard->gState == HAL_SMARTCARD_STATE_READY)
1180   {
1181     if ((pData == NULL) || (Size == 0U))
1182     {
1183       return HAL_ERROR;
1184     }
1185 
1186     /* Process Locked */
1187     __HAL_LOCK(hsmartcard);
1188 
1189     hsmartcard->gState = HAL_SMARTCARD_STATE_BUSY_TX;
1190 
1191     hsmartcard->ErrorCode = HAL_SMARTCARD_ERROR_NONE;
1192     hsmartcard->pTxBuffPtr = pData;
1193     hsmartcard->TxXferSize = Size;
1194     hsmartcard->TxXferCount = Size;
1195 
1196     /* Disable the Peripheral first to update mode for TX master */
1197     CLEAR_BIT(hsmartcard->Instance->CR1, USART_CR1_UE);
1198 
1199     /* In case of TX only mode, if NACK is enabled, the USART must be able to monitor
1200        the bidirectional line to detect a NACK signal in case of parity error.
1201        Therefore, the receiver block must be enabled as well (RE bit must be set). */
1202     if ((hsmartcard->Init.Mode == SMARTCARD_MODE_TX)
1203         && (hsmartcard->Init.NACKEnable == SMARTCARD_NACK_ENABLE))
1204     {
1205       SET_BIT(hsmartcard->Instance->CR1, USART_CR1_RE);
1206     }
1207     /* Enable Tx */
1208     SET_BIT(hsmartcard->Instance->CR1, USART_CR1_TE);
1209 
1210     /* Enable the Peripheral */
1211     SET_BIT(hsmartcard->Instance->CR1, USART_CR1_UE);
1212 
1213     /* Perform a TX/RX FIFO Flush */
1214     __HAL_SMARTCARD_FLUSH_DRREGISTER(hsmartcard);
1215 
1216     /* Set the SMARTCARD DMA transfer complete callback */
1217     hsmartcard->hdmatx->XferCpltCallback = SMARTCARD_DMATransmitCplt;
1218 
1219     /* Set the SMARTCARD error callback */
1220     hsmartcard->hdmatx->XferErrorCallback = SMARTCARD_DMAError;
1221 
1222     /* Set the DMA abort callback */
1223     hsmartcard->hdmatx->XferAbortCallback = NULL;
1224 
1225     /* Check linked list mode */
1226     if ((hsmartcard->hdmatx->Mode & DMA_LINKEDLIST) == DMA_LINKEDLIST)
1227     {
1228       if ((hsmartcard->hdmatx->LinkedListQueue != NULL) && (hsmartcard->hdmatx->LinkedListQueue->Head != NULL))
1229       {
1230         /* Set DMA data size */
1231         hsmartcard->hdmatx->LinkedListQueue->Head->LinkRegisters[NODE_CBR1_DEFAULT_OFFSET] = Size;
1232 
1233         /* Set DMA source address */
1234         hsmartcard->hdmatx->LinkedListQueue->Head->LinkRegisters[NODE_CSAR_DEFAULT_OFFSET] =
1235           (uint32_t)hsmartcard->pTxBuffPtr;
1236 
1237         /* Set DMA destination address */
1238         hsmartcard->hdmatx->LinkedListQueue->Head->LinkRegisters[NODE_CDAR_DEFAULT_OFFSET] =
1239           (uint32_t)&hsmartcard->Instance->TDR;
1240 
1241         /* Enable the SMARTCARD transmit DMA channel */
1242         status = HAL_DMAEx_List_Start_IT(hsmartcard->hdmatx);
1243       }
1244       else
1245       {
1246         /* Update status */
1247         status = HAL_ERROR;
1248       }
1249     }
1250     else
1251     {
1252       /* Enable the SMARTCARD transmit DMA channel */
1253       status = HAL_DMA_Start_IT(hsmartcard->hdmatx, (uint32_t)hsmartcard->pTxBuffPtr,
1254                                 (uint32_t)&hsmartcard->Instance->TDR, Size);
1255     }
1256 
1257     if (status == HAL_OK)
1258     {
1259       /* Clear the TC flag in the ICR register */
1260       CLEAR_BIT(hsmartcard->Instance->ICR, USART_ICR_TCCF);
1261 
1262       /* Process Unlocked */
1263       __HAL_UNLOCK(hsmartcard);
1264 
1265       /* Enable the UART Error Interrupt: (Frame error) */
1266       SET_BIT(hsmartcard->Instance->CR3, USART_CR3_EIE);
1267 
1268       /* Enable the DMA transfer for transmit request by setting the DMAT bit
1269          in the SMARTCARD associated USART CR3 register */
1270       SET_BIT(hsmartcard->Instance->CR3, USART_CR3_DMAT);
1271 
1272       return HAL_OK;
1273     }
1274     else
1275     {
1276       /* Set error code to DMA */
1277       hsmartcard->ErrorCode = HAL_SMARTCARD_ERROR_DMA;
1278 
1279       /* Process Unlocked */
1280       __HAL_UNLOCK(hsmartcard);
1281 
1282       /* Restore hsmartcard->State to ready */
1283       hsmartcard->gState = HAL_SMARTCARD_STATE_READY;
1284 
1285       return HAL_ERROR;
1286     }
1287   }
1288   else
1289   {
1290     return HAL_BUSY;
1291   }
1292 }
1293 
1294 /**
1295   * @brief  Receive an amount of data in DMA mode.
1296   * @param  hsmartcard Pointer to a SMARTCARD_HandleTypeDef structure that contains
1297   *                    the configuration information for the specified SMARTCARD module.
1298   * @param  pData pointer to data buffer.
1299   * @param  Size amount of data to be received.
1300   * @note   The SMARTCARD-associated USART parity is enabled (PCE = 1),
1301   *         the received data contain the parity bit (MSB position).
1302   * @retval HAL status
1303   */
HAL_SMARTCARD_Receive_DMA(SMARTCARD_HandleTypeDef * hsmartcard,uint8_t * pData,uint16_t Size)1304 HAL_StatusTypeDef HAL_SMARTCARD_Receive_DMA(SMARTCARD_HandleTypeDef *hsmartcard, uint8_t *pData, uint16_t Size)
1305 {
1306   HAL_StatusTypeDef status;
1307 
1308   /* Check that a Rx process is not already ongoing */
1309   if (hsmartcard->RxState == HAL_SMARTCARD_STATE_READY)
1310   {
1311     if ((pData == NULL) || (Size == 0U))
1312     {
1313       return HAL_ERROR;
1314     }
1315 
1316     /* Process Locked */
1317     __HAL_LOCK(hsmartcard);
1318 
1319     hsmartcard->ErrorCode = HAL_SMARTCARD_ERROR_NONE;
1320     hsmartcard->RxState   = HAL_SMARTCARD_STATE_BUSY_RX;
1321 
1322     hsmartcard->pRxBuffPtr = pData;
1323     hsmartcard->RxXferSize = Size;
1324 
1325     /* Set the SMARTCARD DMA transfer complete callback */
1326     hsmartcard->hdmarx->XferCpltCallback = SMARTCARD_DMAReceiveCplt;
1327 
1328     /* Set the SMARTCARD DMA error callback */
1329     hsmartcard->hdmarx->XferErrorCallback = SMARTCARD_DMAError;
1330 
1331     /* Set the DMA abort callback */
1332     hsmartcard->hdmarx->XferAbortCallback = NULL;
1333 
1334     /* Check linked list mode  */
1335     if ((hsmartcard->hdmarx->Mode & DMA_LINKEDLIST) == DMA_LINKEDLIST)
1336     {
1337       if ((hsmartcard->hdmarx->LinkedListQueue != NULL) && (hsmartcard->hdmarx->LinkedListQueue->Head != NULL))
1338       {
1339         /* Set DMA data size */
1340         hsmartcard->hdmarx->LinkedListQueue->Head->LinkRegisters[NODE_CBR1_DEFAULT_OFFSET] = Size;
1341 
1342         /* Set DMA source address */
1343         hsmartcard->hdmarx->LinkedListQueue->Head->LinkRegisters[NODE_CSAR_DEFAULT_OFFSET] =
1344           (uint32_t)&hsmartcard->Instance->RDR;
1345 
1346         /* Set DMA destination address */
1347         hsmartcard->hdmarx->LinkedListQueue->Head->LinkRegisters[NODE_CDAR_DEFAULT_OFFSET] =
1348           (uint32_t)hsmartcard->pRxBuffPtr;
1349 
1350         /* Enable the SMARTCARD receive DMA channel */
1351         status = HAL_DMAEx_List_Start_IT(hsmartcard->hdmarx);
1352       }
1353       else
1354       {
1355         /* Update status */
1356         status = HAL_ERROR;
1357       }
1358     }
1359     else
1360     {
1361       /* Enable the DMA channel */
1362       status = HAL_DMA_Start_IT(hsmartcard->hdmarx, (uint32_t)&hsmartcard->Instance->RDR,
1363                                 (uint32_t)hsmartcard->pRxBuffPtr, Size);
1364     }
1365 
1366     if (status == HAL_OK)
1367     {
1368       /* Process Unlocked */
1369       __HAL_UNLOCK(hsmartcard);
1370 
1371       /* Enable the SMARTCARD Parity Error Interrupt */
1372       SET_BIT(hsmartcard->Instance->CR1, USART_CR1_PEIE);
1373 
1374       /* Enable the SMARTCARD Error Interrupt: (Frame error, noise error, overrun error) */
1375       SET_BIT(hsmartcard->Instance->CR3, USART_CR3_EIE);
1376 
1377       /* Enable the DMA transfer for the receiver request by setting the DMAR bit
1378          in the SMARTCARD associated USART CR3 register */
1379       SET_BIT(hsmartcard->Instance->CR3, USART_CR3_DMAR);
1380 
1381       return HAL_OK;
1382     }
1383     else
1384     {
1385       /* Set error code to DMA */
1386       hsmartcard->ErrorCode = HAL_SMARTCARD_ERROR_DMA;
1387 
1388       /* Process Unlocked */
1389       __HAL_UNLOCK(hsmartcard);
1390 
1391       /* Restore hsmartcard->State to ready */
1392       hsmartcard->RxState = HAL_SMARTCARD_STATE_READY;
1393 
1394       return HAL_ERROR;
1395     }
1396   }
1397   else
1398   {
1399     return HAL_BUSY;
1400   }
1401 }
1402 #endif /* HAL_DMA_MODULE_ENABLED */
1403 
1404 /**
1405   * @brief  Abort ongoing transfers (blocking mode).
1406   * @param  hsmartcard Pointer to a SMARTCARD_HandleTypeDef structure that contains
1407   *                    the configuration information for the specified SMARTCARD module.
1408   * @note   This procedure could be used for aborting any ongoing transfer started in Interrupt or DMA mode.
1409   *         This procedure performs following operations :
1410   *           - Disable SMARTCARD Interrupts (Tx and Rx)
1411   *           - Disable the DMA transfer in the peripheral register (if enabled)
1412   *           - Abort DMA transfer by calling HAL_DMA_Abort (in case of transfer in DMA mode)
1413   *           - Set handle State to READY
1414   * @note   This procedure is executed in blocking mode : when exiting function, Abort is considered as completed.
1415   * @retval HAL status
1416   */
HAL_SMARTCARD_Abort(SMARTCARD_HandleTypeDef * hsmartcard)1417 HAL_StatusTypeDef HAL_SMARTCARD_Abort(SMARTCARD_HandleTypeDef *hsmartcard)
1418 {
1419   /* Disable RTOIE, EOBIE, TXEIE, TCIE, RXNE, PE, RXFT, TXFT and
1420      ERR (Frame error, noise error, overrun error) interrupts */
1421   CLEAR_BIT(hsmartcard->Instance->CR1,
1422             (USART_CR1_RXNEIE_RXFNEIE | USART_CR1_PEIE | USART_CR1_TXEIE_TXFNFIE | USART_CR1_TCIE | USART_CR1_RTOIE |
1423              USART_CR1_EOBIE));
1424   CLEAR_BIT(hsmartcard->Instance->CR3, (USART_CR3_EIE | USART_CR3_RXFTIE | USART_CR3_TXFTIE));
1425 
1426 #if defined(HAL_DMA_MODULE_ENABLED)
1427   /* Disable the SMARTCARD DMA Tx request if enabled */
1428   if (HAL_IS_BIT_SET(hsmartcard->Instance->CR3, USART_CR3_DMAT))
1429   {
1430 #if !defined(USART_DMAREQUESTS_SW_WA)
1431     CLEAR_BIT(hsmartcard->Instance->CR3, USART_CR3_DMAT);
1432 
1433 #endif /* USART_DMAREQUESTS_SW_WA */
1434     /* Abort the SMARTCARD DMA Tx channel : use blocking DMA Abort API (no callback) */
1435     if (hsmartcard->hdmatx != NULL)
1436     {
1437       /* Set the SMARTCARD DMA Abort callback to Null.
1438          No call back execution at end of DMA abort procedure */
1439       hsmartcard->hdmatx->XferAbortCallback = NULL;
1440 
1441       if (HAL_DMA_Abort(hsmartcard->hdmatx) != HAL_OK)
1442       {
1443         if (HAL_DMA_GetError(hsmartcard->hdmatx) == HAL_DMA_ERROR_TIMEOUT)
1444         {
1445           /* Set error code to DMA */
1446           hsmartcard->ErrorCode = HAL_SMARTCARD_ERROR_DMA;
1447 
1448           return HAL_TIMEOUT;
1449         }
1450       }
1451     }
1452   }
1453 
1454   /* Disable the SMARTCARD DMA Rx request if enabled */
1455   if (HAL_IS_BIT_SET(hsmartcard->Instance->CR3, USART_CR3_DMAR))
1456   {
1457 #if !defined(USART_DMAREQUESTS_SW_WA)
1458     CLEAR_BIT(hsmartcard->Instance->CR3, USART_CR3_DMAR);
1459 
1460 #endif /* USART_DMAREQUESTS_SW_WA */
1461     /* Abort the SMARTCARD DMA Rx channel : use blocking DMA Abort API (no callback) */
1462     if (hsmartcard->hdmarx != NULL)
1463     {
1464       /* Set the SMARTCARD DMA Abort callback to Null.
1465          No call back execution at end of DMA abort procedure */
1466       hsmartcard->hdmarx->XferAbortCallback = NULL;
1467 
1468       if (HAL_DMA_Abort(hsmartcard->hdmarx) != HAL_OK)
1469       {
1470         if (HAL_DMA_GetError(hsmartcard->hdmarx) == HAL_DMA_ERROR_TIMEOUT)
1471         {
1472           /* Set error code to DMA */
1473           hsmartcard->ErrorCode = HAL_SMARTCARD_ERROR_DMA;
1474 
1475           return HAL_TIMEOUT;
1476         }
1477       }
1478     }
1479   }
1480 #endif /* HAL_DMA_MODULE_ENABLED */
1481 
1482   /* Reset Tx and Rx transfer counters */
1483   hsmartcard->TxXferCount = 0U;
1484   hsmartcard->RxXferCount = 0U;
1485 
1486   /* Clear the Error flags in the ICR register */
1487   __HAL_SMARTCARD_CLEAR_FLAG(hsmartcard,
1488                              SMARTCARD_CLEAR_OREF | SMARTCARD_CLEAR_NEF | SMARTCARD_CLEAR_PEF | SMARTCARD_CLEAR_FEF |
1489                              SMARTCARD_CLEAR_RTOF | SMARTCARD_CLEAR_EOBF);
1490 
1491   /* Restore hsmartcard->gState and hsmartcard->RxState to Ready */
1492   hsmartcard->gState  = HAL_SMARTCARD_STATE_READY;
1493   hsmartcard->RxState = HAL_SMARTCARD_STATE_READY;
1494 
1495   /* Reset Handle ErrorCode to No Error */
1496   hsmartcard->ErrorCode = HAL_SMARTCARD_ERROR_NONE;
1497 
1498   return HAL_OK;
1499 }
1500 
1501 /**
1502   * @brief  Abort ongoing Transmit transfer (blocking mode).
1503   * @param  hsmartcard Pointer to a SMARTCARD_HandleTypeDef structure that contains
1504   *                    the configuration information for the specified SMARTCARD module.
1505   * @note   This procedure could be used for aborting any ongoing Tx transfer started in Interrupt or DMA mode.
1506   *         This procedure performs following operations :
1507   *           - Disable SMARTCARD Interrupts (Tx)
1508   *           - Disable the DMA transfer in the peripheral register (if enabled)
1509   *           - Abort DMA transfer by calling HAL_DMA_Abort (in case of transfer in DMA mode)
1510   *           - Set handle State to READY
1511   * @note   This procedure is executed in blocking mode : when exiting function, Abort is considered as completed.
1512   * @retval HAL status
1513   */
HAL_SMARTCARD_AbortTransmit(SMARTCARD_HandleTypeDef * hsmartcard)1514 HAL_StatusTypeDef HAL_SMARTCARD_AbortTransmit(SMARTCARD_HandleTypeDef *hsmartcard)
1515 {
1516   /* Disable TCIE, TXEIE and TXFTIE interrupts */
1517   CLEAR_BIT(hsmartcard->Instance->CR1, (USART_CR1_TXEIE_TXFNFIE | USART_CR1_TCIE));
1518   CLEAR_BIT(hsmartcard->Instance->CR3, USART_CR3_TXFTIE);
1519 
1520   /* Check if a receive process is ongoing or not. If not disable ERR IT */
1521   if (hsmartcard->RxState == HAL_SMARTCARD_STATE_READY)
1522   {
1523     /* Disable the SMARTCARD Error Interrupt: (Frame error) */
1524     CLEAR_BIT(hsmartcard->Instance->CR3, USART_CR3_EIE);
1525   }
1526 
1527 #if defined(HAL_DMA_MODULE_ENABLED)
1528   /* Disable the SMARTCARD DMA Tx request if enabled */
1529   if (HAL_IS_BIT_SET(hsmartcard->Instance->CR3, USART_CR3_DMAT))
1530   {
1531 #if !defined(USART_DMAREQUESTS_SW_WA)
1532     CLEAR_BIT(hsmartcard->Instance->CR3, USART_CR3_DMAT);
1533 
1534 #endif /* USART_DMAREQUESTS_SW_WA */
1535     /* Abort the SMARTCARD DMA Tx channel : use blocking DMA Abort API (no callback) */
1536     if (hsmartcard->hdmatx != NULL)
1537     {
1538       /* Set the SMARTCARD DMA Abort callback to Null.
1539          No call back execution at end of DMA abort procedure */
1540       hsmartcard->hdmatx->XferAbortCallback = NULL;
1541 
1542       if (HAL_DMA_Abort(hsmartcard->hdmatx) != HAL_OK)
1543       {
1544         if (HAL_DMA_GetError(hsmartcard->hdmatx) == HAL_DMA_ERROR_TIMEOUT)
1545         {
1546           /* Set error code to DMA */
1547           hsmartcard->ErrorCode = HAL_SMARTCARD_ERROR_DMA;
1548 
1549           return HAL_TIMEOUT;
1550         }
1551       }
1552     }
1553   }
1554 #endif /* HAL_DMA_MODULE_ENABLED */
1555 
1556   /* Reset Tx transfer counter */
1557   hsmartcard->TxXferCount = 0U;
1558 
1559   /* Clear the Error flags in the ICR register */
1560   __HAL_SMARTCARD_CLEAR_FLAG(hsmartcard, SMARTCARD_CLEAR_FEF);
1561 
1562   /* Restore hsmartcard->gState to Ready */
1563   hsmartcard->gState = HAL_SMARTCARD_STATE_READY;
1564 
1565   return HAL_OK;
1566 }
1567 
1568 /**
1569   * @brief  Abort ongoing Receive transfer (blocking mode).
1570   * @param  hsmartcard Pointer to a SMARTCARD_HandleTypeDef structure that contains
1571   *                    the configuration information for the specified SMARTCARD module.
1572   * @note   This procedure could be used for aborting any ongoing Rx transfer started in Interrupt or DMA mode.
1573   *         This procedure performs following operations :
1574   *           - Disable SMARTCARD Interrupts (Rx)
1575   *           - Disable the DMA transfer in the peripheral register (if enabled)
1576   *           - Abort DMA transfer by calling HAL_DMA_Abort (in case of transfer in DMA mode)
1577   *           - Set handle State to READY
1578   * @note   This procedure is executed in blocking mode : when exiting function, Abort is considered as completed.
1579   * @retval HAL status
1580   */
HAL_SMARTCARD_AbortReceive(SMARTCARD_HandleTypeDef * hsmartcard)1581 HAL_StatusTypeDef HAL_SMARTCARD_AbortReceive(SMARTCARD_HandleTypeDef *hsmartcard)
1582 {
1583   /* Disable RTOIE, EOBIE, RXNE, PE, RXFT, TXFT and  ERR (Frame error, noise error, overrun error) interrupts */
1584   CLEAR_BIT(hsmartcard->Instance->CR1, (USART_CR1_RXNEIE_RXFNEIE | USART_CR1_PEIE | USART_CR1_RTOIE |
1585                                         USART_CR1_EOBIE));
1586   CLEAR_BIT(hsmartcard->Instance->CR3, (USART_CR3_EIE | USART_CR3_RXFTIE));
1587 
1588   /* Check if a Transmit process is ongoing or not. If not disable ERR IT */
1589   if (hsmartcard->gState == HAL_SMARTCARD_STATE_READY)
1590   {
1591     /* Disable the SMARTCARD Error Interrupt: (Frame error) */
1592     CLEAR_BIT(hsmartcard->Instance->CR3, USART_CR3_EIE);
1593   }
1594 
1595 #if defined(HAL_DMA_MODULE_ENABLED)
1596   /* Disable the SMARTCARD DMA Rx request if enabled */
1597   if (HAL_IS_BIT_SET(hsmartcard->Instance->CR3, USART_CR3_DMAR))
1598   {
1599 #if !defined(USART_DMAREQUESTS_SW_WA)
1600     CLEAR_BIT(hsmartcard->Instance->CR3, USART_CR3_DMAR);
1601 
1602 #endif /* USART_DMAREQUESTS_SW_WA */
1603     /* Abort the SMARTCARD DMA Rx channel : use blocking DMA Abort API (no callback) */
1604     if (hsmartcard->hdmarx != NULL)
1605     {
1606       /* Set the SMARTCARD DMA Abort callback to Null.
1607          No call back execution at end of DMA abort procedure */
1608       hsmartcard->hdmarx->XferAbortCallback = NULL;
1609 
1610       if (HAL_DMA_Abort(hsmartcard->hdmarx) != HAL_OK)
1611       {
1612         if (HAL_DMA_GetError(hsmartcard->hdmarx) == HAL_DMA_ERROR_TIMEOUT)
1613         {
1614           /* Set error code to DMA */
1615           hsmartcard->ErrorCode = HAL_SMARTCARD_ERROR_DMA;
1616 
1617           return HAL_TIMEOUT;
1618         }
1619       }
1620     }
1621   }
1622 #endif /* HAL_DMA_MODULE_ENABLED */
1623 
1624   /* Reset Rx transfer counter */
1625   hsmartcard->RxXferCount = 0U;
1626 
1627   /* Clear the Error flags in the ICR register */
1628   __HAL_SMARTCARD_CLEAR_FLAG(hsmartcard,
1629                              SMARTCARD_CLEAR_OREF | SMARTCARD_CLEAR_NEF | SMARTCARD_CLEAR_PEF | SMARTCARD_CLEAR_FEF |
1630                              SMARTCARD_CLEAR_RTOF | SMARTCARD_CLEAR_EOBF);
1631 
1632   /* Restore hsmartcard->RxState to Ready */
1633   hsmartcard->RxState = HAL_SMARTCARD_STATE_READY;
1634 
1635   return HAL_OK;
1636 }
1637 
1638 /**
1639   * @brief  Abort ongoing transfers (Interrupt mode).
1640   * @param  hsmartcard Pointer to a SMARTCARD_HandleTypeDef structure that contains
1641   *                    the configuration information for the specified SMARTCARD module.
1642   * @note   This procedure could be used for aborting any ongoing transfer started in Interrupt or DMA mode.
1643   *         This procedure performs following operations :
1644   *           - Disable SMARTCARD Interrupts (Tx and Rx)
1645   *           - Disable the DMA transfer in the peripheral register (if enabled)
1646   *           - Abort DMA transfer by calling HAL_DMA_Abort_IT (in case of transfer in DMA mode)
1647   *           - Set handle State to READY
1648   *           - At abort completion, call user abort complete callback
1649   * @note   This procedure is executed in Interrupt mode, meaning that abort procedure could be
1650   *         considered as completed only when user abort complete callback is executed (not when exiting function).
1651   * @retval HAL status
1652   */
HAL_SMARTCARD_Abort_IT(SMARTCARD_HandleTypeDef * hsmartcard)1653 HAL_StatusTypeDef HAL_SMARTCARD_Abort_IT(SMARTCARD_HandleTypeDef *hsmartcard)
1654 {
1655   uint32_t abortcplt = 1U;
1656 
1657   /* Disable RTOIE, EOBIE, TXEIE, TCIE, RXNE, PE, RXFT, TXFT and
1658      ERR (Frame error, noise error, overrun error) interrupts */
1659   CLEAR_BIT(hsmartcard->Instance->CR1,
1660             (USART_CR1_RXNEIE_RXFNEIE | USART_CR1_PEIE | USART_CR1_TXEIE_TXFNFIE | USART_CR1_TCIE | USART_CR1_RTOIE |
1661              USART_CR1_EOBIE));
1662   CLEAR_BIT(hsmartcard->Instance->CR3, (USART_CR3_EIE | USART_CR3_RXFTIE | USART_CR3_TXFTIE));
1663 
1664 #if defined(HAL_DMA_MODULE_ENABLED)
1665   /* If DMA Tx and/or DMA Rx Handles are associated to SMARTCARD Handle,
1666      DMA Abort complete callbacks should be initialised before any call
1667      to DMA Abort functions */
1668   /* DMA Tx Handle is valid */
1669   if (hsmartcard->hdmatx != NULL)
1670   {
1671     /* Set DMA Abort Complete callback if SMARTCARD DMA Tx request if enabled.
1672        Otherwise, set it to NULL */
1673     if (HAL_IS_BIT_SET(hsmartcard->Instance->CR3, USART_CR3_DMAT))
1674     {
1675       hsmartcard->hdmatx->XferAbortCallback = SMARTCARD_DMATxAbortCallback;
1676     }
1677     else
1678     {
1679       hsmartcard->hdmatx->XferAbortCallback = NULL;
1680     }
1681   }
1682   /* DMA Rx Handle is valid */
1683   if (hsmartcard->hdmarx != NULL)
1684   {
1685     /* Set DMA Abort Complete callback if SMARTCARD DMA Rx request if enabled.
1686        Otherwise, set it to NULL */
1687     if (HAL_IS_BIT_SET(hsmartcard->Instance->CR3, USART_CR3_DMAR))
1688     {
1689       hsmartcard->hdmarx->XferAbortCallback = SMARTCARD_DMARxAbortCallback;
1690     }
1691     else
1692     {
1693       hsmartcard->hdmarx->XferAbortCallback = NULL;
1694     }
1695   }
1696 
1697   /* Disable the SMARTCARD DMA Tx request if enabled */
1698   if (HAL_IS_BIT_SET(hsmartcard->Instance->CR3, USART_CR3_DMAT))
1699   {
1700 #if !defined(USART_DMAREQUESTS_SW_WA)
1701     /* Disable DMA Tx at UART level */
1702     CLEAR_BIT(hsmartcard->Instance->CR3, USART_CR3_DMAT);
1703 
1704 #endif /* USART_DMAREQUESTS_SW_WA */
1705     /* Abort the SMARTCARD DMA Tx channel : use non blocking DMA Abort API (callback) */
1706     if (hsmartcard->hdmatx != NULL)
1707     {
1708       /* SMARTCARD Tx DMA Abort callback has already been initialised :
1709          will lead to call HAL_SMARTCARD_AbortCpltCallback() at end of DMA abort procedure */
1710 
1711       /* Abort DMA TX */
1712       if (HAL_DMA_Abort_IT(hsmartcard->hdmatx) != HAL_OK)
1713       {
1714         hsmartcard->hdmatx->XferAbortCallback = NULL;
1715       }
1716       else
1717       {
1718         abortcplt = 0U;
1719       }
1720     }
1721   }
1722 
1723   /* Disable the SMARTCARD DMA Rx request if enabled */
1724   if (HAL_IS_BIT_SET(hsmartcard->Instance->CR3, USART_CR3_DMAR))
1725   {
1726 #if !defined(USART_DMAREQUESTS_SW_WA)
1727     CLEAR_BIT(hsmartcard->Instance->CR3, USART_CR3_DMAR);
1728 
1729 #endif /* USART_DMAREQUESTS_SW_WA */
1730     /* Abort the SMARTCARD DMA Rx channel : use non blocking DMA Abort API (callback) */
1731     if (hsmartcard->hdmarx != NULL)
1732     {
1733       /* SMARTCARD Rx DMA Abort callback has already been initialised :
1734          will lead to call HAL_SMARTCARD_AbortCpltCallback() at end of DMA abort procedure */
1735 
1736       /* Abort DMA RX */
1737       if (HAL_DMA_Abort_IT(hsmartcard->hdmarx) != HAL_OK)
1738       {
1739         hsmartcard->hdmarx->XferAbortCallback = NULL;
1740         abortcplt = 1U;
1741       }
1742       else
1743       {
1744         abortcplt = 0U;
1745       }
1746     }
1747   }
1748 #endif /* HAL_DMA_MODULE_ENABLED */
1749 
1750   /* if no DMA abort complete callback execution is required => call user Abort Complete callback */
1751   if (abortcplt == 1U)
1752   {
1753     /* Reset Tx and Rx transfer counters */
1754     hsmartcard->TxXferCount = 0U;
1755     hsmartcard->RxXferCount = 0U;
1756 
1757     /* Clear ISR function pointers */
1758     hsmartcard->RxISR = NULL;
1759     hsmartcard->TxISR = NULL;
1760 
1761     /* Reset errorCode */
1762     hsmartcard->ErrorCode = HAL_SMARTCARD_ERROR_NONE;
1763 
1764     /* Clear the Error flags in the ICR register */
1765     __HAL_SMARTCARD_CLEAR_FLAG(hsmartcard,
1766                                SMARTCARD_CLEAR_OREF | SMARTCARD_CLEAR_NEF | SMARTCARD_CLEAR_PEF |
1767                                SMARTCARD_CLEAR_FEF | SMARTCARD_CLEAR_RTOF | SMARTCARD_CLEAR_EOBF);
1768 
1769     /* Restore hsmartcard->gState and hsmartcard->RxState to Ready */
1770     hsmartcard->gState  = HAL_SMARTCARD_STATE_READY;
1771     hsmartcard->RxState = HAL_SMARTCARD_STATE_READY;
1772 
1773     /* As no DMA to be aborted, call directly user Abort complete callback */
1774 #if (USE_HAL_SMARTCARD_REGISTER_CALLBACKS == 1)
1775     /* Call registered Abort complete callback */
1776     hsmartcard->AbortCpltCallback(hsmartcard);
1777 #else
1778     /* Call legacy weak Abort complete callback */
1779     HAL_SMARTCARD_AbortCpltCallback(hsmartcard);
1780 #endif /* USE_HAL_SMARTCARD_REGISTER_CALLBACK */
1781   }
1782 
1783   return HAL_OK;
1784 }
1785 
1786 /**
1787   * @brief  Abort ongoing Transmit transfer (Interrupt mode).
1788   * @param  hsmartcard Pointer to a SMARTCARD_HandleTypeDef structure that contains
1789   *                    the configuration information for the specified SMARTCARD module.
1790   * @note   This procedure could be used for aborting any ongoing Tx transfer started in Interrupt or DMA mode.
1791   *         This procedure performs following operations :
1792   *           - Disable SMARTCARD Interrupts (Tx)
1793   *           - Disable the DMA transfer in the peripheral register (if enabled)
1794   *           - Abort DMA transfer by calling HAL_DMA_Abort_IT (in case of transfer in DMA mode)
1795   *           - Set handle State to READY
1796   *           - At abort completion, call user abort complete callback
1797   * @note   This procedure is executed in Interrupt mode, meaning that abort procedure could be
1798   *         considered as completed only when user abort complete callback is executed (not when exiting function).
1799   * @retval HAL status
1800   */
HAL_SMARTCARD_AbortTransmit_IT(SMARTCARD_HandleTypeDef * hsmartcard)1801 HAL_StatusTypeDef HAL_SMARTCARD_AbortTransmit_IT(SMARTCARD_HandleTypeDef *hsmartcard)
1802 {
1803   /* Disable TCIE, TXEIE and TXFTIE interrupts */
1804   CLEAR_BIT(hsmartcard->Instance->CR1, (USART_CR1_TXEIE_TXFNFIE | USART_CR1_TCIE));
1805   CLEAR_BIT(hsmartcard->Instance->CR3, USART_CR3_TXFTIE);
1806 
1807   /* Check if a receive process is ongoing or not. If not disable ERR IT */
1808   if (hsmartcard->RxState == HAL_SMARTCARD_STATE_READY)
1809   {
1810     /* Disable the SMARTCARD Error Interrupt: (Frame error) */
1811     CLEAR_BIT(hsmartcard->Instance->CR3, USART_CR3_EIE);
1812   }
1813 
1814 #if defined(HAL_DMA_MODULE_ENABLED)
1815   /* Disable the SMARTCARD DMA Tx request if enabled */
1816   if (HAL_IS_BIT_SET(hsmartcard->Instance->CR3, USART_CR3_DMAT))
1817   {
1818 #if !defined(USART_DMAREQUESTS_SW_WA)
1819     CLEAR_BIT(hsmartcard->Instance->CR3, USART_CR3_DMAT);
1820 
1821 #endif /* USART_DMAREQUESTS_SW_WA */
1822     /* Abort the SMARTCARD DMA Tx channel : use non blocking DMA Abort API (callback) */
1823     if (hsmartcard->hdmatx != NULL)
1824     {
1825       /* Set the SMARTCARD DMA Abort callback :
1826          will lead to call HAL_SMARTCARD_AbortCpltCallback() at end of DMA abort procedure */
1827       hsmartcard->hdmatx->XferAbortCallback = SMARTCARD_DMATxOnlyAbortCallback;
1828 
1829       /* Abort DMA TX */
1830       if (HAL_DMA_Abort_IT(hsmartcard->hdmatx) != HAL_OK)
1831       {
1832         /* Call Directly hsmartcard->hdmatx->XferAbortCallback function in case of error */
1833         hsmartcard->hdmatx->XferAbortCallback(hsmartcard->hdmatx);
1834       }
1835     }
1836     else
1837     {
1838       /* Reset Tx transfer counter */
1839       hsmartcard->TxXferCount = 0U;
1840 
1841       /* Clear TxISR function pointers */
1842       hsmartcard->TxISR = NULL;
1843 
1844       /* Restore hsmartcard->gState to Ready */
1845       hsmartcard->gState = HAL_SMARTCARD_STATE_READY;
1846 
1847       /* As no DMA to be aborted, call directly user Abort complete callback */
1848 #if (USE_HAL_SMARTCARD_REGISTER_CALLBACKS == 1)
1849       /* Call registered Abort Transmit Complete Callback */
1850       hsmartcard->AbortTransmitCpltCallback(hsmartcard);
1851 #else
1852       /* Call legacy weak Abort Transmit Complete Callback */
1853       HAL_SMARTCARD_AbortTransmitCpltCallback(hsmartcard);
1854 #endif /* USE_HAL_SMARTCARD_REGISTER_CALLBACK */
1855     }
1856   }
1857   else
1858 #endif /* HAL_DMA_MODULE_ENABLED */
1859   {
1860     /* Reset Tx transfer counter */
1861     hsmartcard->TxXferCount = 0U;
1862 
1863     /* Clear TxISR function pointers */
1864     hsmartcard->TxISR = NULL;
1865 
1866     /* Clear the Error flags in the ICR register */
1867     __HAL_SMARTCARD_CLEAR_FLAG(hsmartcard, SMARTCARD_CLEAR_FEF);
1868 
1869     /* Restore hsmartcard->gState to Ready */
1870     hsmartcard->gState = HAL_SMARTCARD_STATE_READY;
1871 
1872     /* As no DMA to be aborted, call directly user Abort complete callback */
1873 #if (USE_HAL_SMARTCARD_REGISTER_CALLBACKS == 1)
1874     /* Call registered Abort Transmit Complete Callback */
1875     hsmartcard->AbortTransmitCpltCallback(hsmartcard);
1876 #else
1877     /* Call legacy weak Abort Transmit Complete Callback */
1878     HAL_SMARTCARD_AbortTransmitCpltCallback(hsmartcard);
1879 #endif /* USE_HAL_SMARTCARD_REGISTER_CALLBACK */
1880   }
1881 
1882   return HAL_OK;
1883 }
1884 
1885 /**
1886   * @brief  Abort ongoing Receive transfer (Interrupt mode).
1887   * @param  hsmartcard Pointer to a SMARTCARD_HandleTypeDef structure that contains
1888   *                    the configuration information for the specified SMARTCARD module.
1889   * @note   This procedure could be used for aborting any ongoing Rx transfer started in Interrupt or DMA mode.
1890   *         This procedure performs following operations :
1891   *           - Disable SMARTCARD Interrupts (Rx)
1892   *           - Disable the DMA transfer in the peripheral register (if enabled)
1893   *           - Abort DMA transfer by calling HAL_DMA_Abort_IT (in case of transfer in DMA mode)
1894   *           - Set handle State to READY
1895   *           - At abort completion, call user abort complete callback
1896   * @note   This procedure is executed in Interrupt mode, meaning that abort procedure could be
1897   *         considered as completed only when user abort complete callback is executed (not when exiting function).
1898   * @retval HAL status
1899   */
HAL_SMARTCARD_AbortReceive_IT(SMARTCARD_HandleTypeDef * hsmartcard)1900 HAL_StatusTypeDef HAL_SMARTCARD_AbortReceive_IT(SMARTCARD_HandleTypeDef *hsmartcard)
1901 {
1902   /* Disable RTOIE, EOBIE, RXNE, PE, RXFT and  ERR (Frame error, noise error, overrun error) interrupts */
1903   CLEAR_BIT(hsmartcard->Instance->CR1, (USART_CR1_RXNEIE_RXFNEIE | USART_CR1_PEIE | USART_CR1_RTOIE |
1904                                         USART_CR1_EOBIE));
1905   CLEAR_BIT(hsmartcard->Instance->CR3, (USART_CR3_EIE | USART_CR3_RXFTIE));
1906 
1907   /* Check if a Transmit process is ongoing or not. If not disable ERR IT */
1908   if (hsmartcard->gState == HAL_SMARTCARD_STATE_READY)
1909   {
1910     /* Disable the SMARTCARD Error Interrupt: (Frame error) */
1911     CLEAR_BIT(hsmartcard->Instance->CR3, USART_CR3_EIE);
1912   }
1913 
1914 #if defined(HAL_DMA_MODULE_ENABLED)
1915   /* Disable the SMARTCARD DMA Rx request if enabled */
1916   if (HAL_IS_BIT_SET(hsmartcard->Instance->CR3, USART_CR3_DMAR))
1917   {
1918 #if !defined(USART_DMAREQUESTS_SW_WA)
1919     CLEAR_BIT(hsmartcard->Instance->CR3, USART_CR3_DMAR);
1920 
1921 #endif /* USART_DMAREQUESTS_SW_WA */
1922     /* Abort the SMARTCARD DMA Rx channel : use non blocking DMA Abort API (callback) */
1923     if (hsmartcard->hdmarx != NULL)
1924     {
1925       /* Set the SMARTCARD DMA Abort callback :
1926          will lead to call HAL_SMARTCARD_AbortCpltCallback() at end of DMA abort procedure */
1927       hsmartcard->hdmarx->XferAbortCallback = SMARTCARD_DMARxOnlyAbortCallback;
1928 
1929       /* Abort DMA RX */
1930       if (HAL_DMA_Abort_IT(hsmartcard->hdmarx) != HAL_OK)
1931       {
1932         /* Call Directly hsmartcard->hdmarx->XferAbortCallback function in case of error */
1933         hsmartcard->hdmarx->XferAbortCallback(hsmartcard->hdmarx);
1934       }
1935     }
1936     else
1937     {
1938       /* Reset Rx transfer counter */
1939       hsmartcard->RxXferCount = 0U;
1940 
1941       /* Clear RxISR function pointer */
1942       hsmartcard->RxISR = NULL;
1943 
1944       /* Clear the Error flags in the ICR register */
1945       __HAL_SMARTCARD_CLEAR_FLAG(hsmartcard,
1946                                  SMARTCARD_CLEAR_OREF | SMARTCARD_CLEAR_NEF | SMARTCARD_CLEAR_PEF |
1947                                  SMARTCARD_CLEAR_FEF | SMARTCARD_CLEAR_RTOF | SMARTCARD_CLEAR_EOBF);
1948 
1949       /* Restore hsmartcard->RxState to Ready */
1950       hsmartcard->RxState = HAL_SMARTCARD_STATE_READY;
1951 
1952       /* As no DMA to be aborted, call directly user Abort complete callback */
1953 #if (USE_HAL_SMARTCARD_REGISTER_CALLBACKS == 1)
1954       /* Call registered Abort Receive Complete Callback */
1955       hsmartcard->AbortReceiveCpltCallback(hsmartcard);
1956 #else
1957       /* Call legacy weak Abort Receive Complete Callback */
1958       HAL_SMARTCARD_AbortReceiveCpltCallback(hsmartcard);
1959 #endif /* USE_HAL_SMARTCARD_REGISTER_CALLBACK */
1960     }
1961   }
1962   else
1963 #endif /* HAL_DMA_MODULE_ENABLED */
1964   {
1965     /* Reset Rx transfer counter */
1966     hsmartcard->RxXferCount = 0U;
1967 
1968     /* Clear RxISR function pointer */
1969     hsmartcard->RxISR = NULL;
1970 
1971     /* Clear the Error flags in the ICR register */
1972     __HAL_SMARTCARD_CLEAR_FLAG(hsmartcard,
1973                                SMARTCARD_CLEAR_OREF | SMARTCARD_CLEAR_NEF | SMARTCARD_CLEAR_PEF |
1974                                SMARTCARD_CLEAR_FEF | SMARTCARD_CLEAR_RTOF | SMARTCARD_CLEAR_EOBF);
1975 
1976     /* Restore hsmartcard->RxState to Ready */
1977     hsmartcard->RxState = HAL_SMARTCARD_STATE_READY;
1978 
1979     /* As no DMA to be aborted, call directly user Abort complete callback */
1980 #if (USE_HAL_SMARTCARD_REGISTER_CALLBACKS == 1)
1981     /* Call registered Abort Receive Complete Callback */
1982     hsmartcard->AbortReceiveCpltCallback(hsmartcard);
1983 #else
1984     /* Call legacy weak Abort Receive Complete Callback */
1985     HAL_SMARTCARD_AbortReceiveCpltCallback(hsmartcard);
1986 #endif /* USE_HAL_SMARTCARD_REGISTER_CALLBACK */
1987   }
1988 
1989   return HAL_OK;
1990 }
1991 
1992 /**
1993   * @brief  Handle SMARTCARD interrupt requests.
1994   * @param  hsmartcard Pointer to a SMARTCARD_HandleTypeDef structure that contains
1995   *                    the configuration information for the specified SMARTCARD module.
1996   * @retval None
1997   */
HAL_SMARTCARD_IRQHandler(SMARTCARD_HandleTypeDef * hsmartcard)1998 void HAL_SMARTCARD_IRQHandler(SMARTCARD_HandleTypeDef *hsmartcard)
1999 {
2000   uint32_t isrflags   = READ_REG(hsmartcard->Instance->ISR);
2001   uint32_t cr1its     = READ_REG(hsmartcard->Instance->CR1);
2002   uint32_t cr3its     = READ_REG(hsmartcard->Instance->CR3);
2003   uint32_t errorflags;
2004   uint32_t errorcode;
2005 
2006   /* If no error occurs */
2007   errorflags = (isrflags & (uint32_t)(USART_ISR_PE | USART_ISR_FE | USART_ISR_ORE | USART_ISR_NE | USART_ISR_RTOF));
2008   if (errorflags == 0U)
2009   {
2010     /* SMARTCARD in mode Receiver ---------------------------------------------------*/
2011     if (((isrflags & USART_ISR_RXNE_RXFNE) != 0U)
2012         && (((cr1its & USART_CR1_RXNEIE_RXFNEIE) != 0U)
2013             || ((cr3its & USART_CR3_RXFTIE) != 0U)))
2014     {
2015       if (hsmartcard->RxISR != NULL)
2016       {
2017         hsmartcard->RxISR(hsmartcard);
2018       }
2019       return;
2020     }
2021   }
2022 
2023   /* If some errors occur */
2024   if ((errorflags != 0U)
2025       && ((((cr3its & (USART_CR3_RXFTIE | USART_CR3_EIE)) != 0U)
2026            || ((cr1its & (USART_CR1_RXNEIE_RXFNEIE | USART_CR1_PEIE)) != 0U))))
2027   {
2028     /* SMARTCARD parity error interrupt occurred -------------------------------------*/
2029     if (((isrflags & USART_ISR_PE) != 0U) && ((cr1its & USART_CR1_PEIE) != 0U))
2030     {
2031       __HAL_SMARTCARD_CLEAR_IT(hsmartcard, SMARTCARD_CLEAR_PEF);
2032 
2033       hsmartcard->ErrorCode |= HAL_SMARTCARD_ERROR_PE;
2034     }
2035 
2036     /* SMARTCARD frame error interrupt occurred --------------------------------------*/
2037     if (((isrflags & USART_ISR_FE) != 0U) && ((cr3its & USART_CR3_EIE) != 0U))
2038     {
2039       __HAL_SMARTCARD_CLEAR_IT(hsmartcard, SMARTCARD_CLEAR_FEF);
2040 
2041       hsmartcard->ErrorCode |= HAL_SMARTCARD_ERROR_FE;
2042     }
2043 
2044     /* SMARTCARD noise error interrupt occurred --------------------------------------*/
2045     if (((isrflags & USART_ISR_NE) != 0U) && ((cr3its & USART_CR3_EIE) != 0U))
2046     {
2047       __HAL_SMARTCARD_CLEAR_IT(hsmartcard, SMARTCARD_CLEAR_NEF);
2048 
2049       hsmartcard->ErrorCode |= HAL_SMARTCARD_ERROR_NE;
2050     }
2051 
2052     /* SMARTCARD Over-Run interrupt occurred -----------------------------------------*/
2053     if (((isrflags & USART_ISR_ORE) != 0U)
2054         && (((cr1its & USART_CR1_RXNEIE_RXFNEIE) != 0U)
2055             || ((cr3its & USART_CR3_RXFTIE) != 0U)
2056             || ((cr3its & USART_CR3_EIE) != 0U)))
2057     {
2058       __HAL_SMARTCARD_CLEAR_IT(hsmartcard, SMARTCARD_CLEAR_OREF);
2059 
2060       hsmartcard->ErrorCode |= HAL_SMARTCARD_ERROR_ORE;
2061     }
2062 
2063     /* SMARTCARD receiver timeout interrupt occurred -----------------------------------------*/
2064     if (((isrflags & USART_ISR_RTOF) != 0U) && ((cr1its & USART_CR1_RTOIE) != 0U))
2065     {
2066       __HAL_SMARTCARD_CLEAR_IT(hsmartcard, SMARTCARD_CLEAR_RTOF);
2067 
2068       hsmartcard->ErrorCode |= HAL_SMARTCARD_ERROR_RTO;
2069     }
2070 
2071     /* Call SMARTCARD Error Call back function if need be --------------------------*/
2072     if (hsmartcard->ErrorCode != HAL_SMARTCARD_ERROR_NONE)
2073     {
2074       /* SMARTCARD in mode Receiver ---------------------------------------------------*/
2075       if (((isrflags & USART_ISR_RXNE_RXFNE) != 0U)
2076           && (((cr1its & USART_CR1_RXNEIE_RXFNEIE) != 0U)
2077               || ((cr3its & USART_CR3_RXFTIE) != 0U)))
2078       {
2079         if (hsmartcard->RxISR != NULL)
2080         {
2081           hsmartcard->RxISR(hsmartcard);
2082         }
2083       }
2084 
2085       /* If Error is to be considered as blocking :
2086           - Receiver Timeout error in Reception
2087           - Overrun error in Reception
2088           - any error occurs in DMA mode reception
2089       */
2090       errorcode = hsmartcard->ErrorCode;
2091       if ((HAL_IS_BIT_SET(hsmartcard->Instance->CR3, USART_CR3_DMAR))
2092           || ((errorcode & (HAL_SMARTCARD_ERROR_RTO | HAL_SMARTCARD_ERROR_ORE)) != 0U))
2093       {
2094         /* Blocking error : transfer is aborted
2095            Set the SMARTCARD state ready to be able to start again the process,
2096            Disable Rx Interrupts, and disable Rx DMA request, if ongoing */
2097         SMARTCARD_EndRxTransfer(hsmartcard);
2098 
2099 #if defined(HAL_DMA_MODULE_ENABLED)
2100         /* Disable the SMARTCARD DMA Rx request if enabled */
2101         if (HAL_IS_BIT_SET(hsmartcard->Instance->CR3, USART_CR3_DMAR))
2102         {
2103 #if !defined(USART_DMAREQUESTS_SW_WA)
2104           CLEAR_BIT(hsmartcard->Instance->CR3, USART_CR3_DMAR);
2105 
2106 #endif /* USART_DMAREQUESTS_SW_WA */
2107           /* Abort the SMARTCARD DMA Rx channel */
2108           if (hsmartcard->hdmarx != NULL)
2109           {
2110             /* Set the SMARTCARD DMA Abort callback :
2111                will lead to call HAL_SMARTCARD_ErrorCallback() at end of DMA abort procedure */
2112             hsmartcard->hdmarx->XferAbortCallback = SMARTCARD_DMAAbortOnError;
2113 
2114             /* Abort DMA RX */
2115             if (HAL_DMA_Abort_IT(hsmartcard->hdmarx) != HAL_OK)
2116             {
2117               /* Call Directly hsmartcard->hdmarx->XferAbortCallback function in case of error */
2118               hsmartcard->hdmarx->XferAbortCallback(hsmartcard->hdmarx);
2119             }
2120           }
2121           else
2122           {
2123 #if (USE_HAL_SMARTCARD_REGISTER_CALLBACKS == 1)
2124             /* Call registered user error callback */
2125             hsmartcard->ErrorCallback(hsmartcard);
2126 #else
2127             /* Call legacy weak user error callback */
2128             HAL_SMARTCARD_ErrorCallback(hsmartcard);
2129 #endif /* USE_HAL_SMARTCARD_REGISTER_CALLBACK */
2130           }
2131         }
2132         else
2133 #endif /* HAL_DMA_MODULE_ENABLED */
2134         {
2135 #if (USE_HAL_SMARTCARD_REGISTER_CALLBACKS == 1)
2136           /* Call registered user error callback */
2137           hsmartcard->ErrorCallback(hsmartcard);
2138 #else
2139           /* Call legacy weak user error callback */
2140           HAL_SMARTCARD_ErrorCallback(hsmartcard);
2141 #endif /* USE_HAL_SMARTCARD_REGISTER_CALLBACK */
2142         }
2143       }
2144       /* other error type to be considered as blocking :
2145           - Frame error in Transmission
2146       */
2147       else if ((hsmartcard->gState == HAL_SMARTCARD_STATE_BUSY_TX)
2148                && ((errorcode & HAL_SMARTCARD_ERROR_FE) != 0U))
2149       {
2150         /* Blocking error : transfer is aborted
2151            Set the SMARTCARD state ready to be able to start again the process,
2152            Disable Tx Interrupts, and disable Tx DMA request, if ongoing */
2153         SMARTCARD_EndTxTransfer(hsmartcard);
2154 
2155 #if defined(HAL_DMA_MODULE_ENABLED)
2156         /* Disable the SMARTCARD DMA Tx request if enabled */
2157         if (HAL_IS_BIT_SET(hsmartcard->Instance->CR3, USART_CR3_DMAT))
2158         {
2159 #if !defined(USART_DMAREQUESTS_SW_WA)
2160           CLEAR_BIT(hsmartcard->Instance->CR3, USART_CR3_DMAT);
2161 
2162 #endif /* USART_DMAREQUESTS_SW_WA */
2163           /* Abort the SMARTCARD DMA Tx channel */
2164           if (hsmartcard->hdmatx != NULL)
2165           {
2166             /* Set the SMARTCARD DMA Abort callback :
2167                will lead to call HAL_SMARTCARD_ErrorCallback() at end of DMA abort procedure */
2168             hsmartcard->hdmatx->XferAbortCallback = SMARTCARD_DMAAbortOnError;
2169 
2170             /* Abort DMA TX */
2171             if (HAL_DMA_Abort_IT(hsmartcard->hdmatx) != HAL_OK)
2172             {
2173               /* Call Directly hsmartcard->hdmatx->XferAbortCallback function in case of error */
2174               hsmartcard->hdmatx->XferAbortCallback(hsmartcard->hdmatx);
2175             }
2176           }
2177           else
2178           {
2179 #if (USE_HAL_SMARTCARD_REGISTER_CALLBACKS == 1)
2180             /* Call registered user error callback */
2181             hsmartcard->ErrorCallback(hsmartcard);
2182 #else
2183             /* Call legacy weak user error callback */
2184             HAL_SMARTCARD_ErrorCallback(hsmartcard);
2185 #endif /* USE_HAL_SMARTCARD_REGISTER_CALLBACK */
2186           }
2187         }
2188         else
2189 #endif /* HAL_DMA_MODULE_ENABLED */
2190         {
2191 #if (USE_HAL_SMARTCARD_REGISTER_CALLBACKS == 1)
2192           /* Call registered user error callback */
2193           hsmartcard->ErrorCallback(hsmartcard);
2194 #else
2195           /* Call legacy weak user error callback */
2196           HAL_SMARTCARD_ErrorCallback(hsmartcard);
2197 #endif /* USE_HAL_SMARTCARD_REGISTER_CALLBACK */
2198         }
2199       }
2200       else
2201       {
2202         /* Non Blocking error : transfer could go on.
2203            Error is notified to user through user error callback */
2204 #if (USE_HAL_SMARTCARD_REGISTER_CALLBACKS == 1)
2205         /* Call registered user error callback */
2206         hsmartcard->ErrorCallback(hsmartcard);
2207 #else
2208         /* Call legacy weak user error callback */
2209         HAL_SMARTCARD_ErrorCallback(hsmartcard);
2210 #endif /* USE_HAL_SMARTCARD_REGISTER_CALLBACK */
2211         hsmartcard->ErrorCode = HAL_SMARTCARD_ERROR_NONE;
2212       }
2213     }
2214     return;
2215 
2216   } /* End if some error occurs */
2217 
2218   /* SMARTCARD in mode Receiver, end of block interruption ------------------------*/
2219   if (((isrflags & USART_ISR_EOBF) != 0U) && ((cr1its & USART_CR1_EOBIE) != 0U))
2220   {
2221     hsmartcard->RxState = HAL_SMARTCARD_STATE_READY;
2222     __HAL_UNLOCK(hsmartcard);
2223 #if (USE_HAL_SMARTCARD_REGISTER_CALLBACKS == 1)
2224     /* Call registered Rx complete callback */
2225     hsmartcard->RxCpltCallback(hsmartcard);
2226 #else
2227     /* Call legacy weak Rx complete callback */
2228     HAL_SMARTCARD_RxCpltCallback(hsmartcard);
2229 #endif /* USE_HAL_SMARTCARD_REGISTER_CALLBACK */
2230     /* Clear EOBF interrupt after HAL_SMARTCARD_RxCpltCallback() call for the End of Block information
2231        to be available during HAL_SMARTCARD_RxCpltCallback() processing */
2232     __HAL_SMARTCARD_CLEAR_IT(hsmartcard, SMARTCARD_CLEAR_EOBF);
2233     return;
2234   }
2235 
2236   /* SMARTCARD in mode Transmitter ------------------------------------------------*/
2237   if (((isrflags & USART_ISR_TXE_TXFNF) != 0U)
2238       && (((cr1its & USART_CR1_TXEIE_TXFNFIE) != 0U)
2239           || ((cr3its & USART_CR3_TXFTIE) != 0U)))
2240   {
2241     if (hsmartcard->TxISR != NULL)
2242     {
2243       hsmartcard->TxISR(hsmartcard);
2244     }
2245     return;
2246   }
2247 
2248   /* SMARTCARD in mode Transmitter (transmission end) ------------------------*/
2249   if (__HAL_SMARTCARD_GET_IT(hsmartcard, hsmartcard->AdvancedInit.TxCompletionIndication) != RESET)
2250   {
2251     if (__HAL_SMARTCARD_GET_IT_SOURCE(hsmartcard, hsmartcard->AdvancedInit.TxCompletionIndication) != RESET)
2252     {
2253       SMARTCARD_EndTransmit_IT(hsmartcard);
2254       return;
2255     }
2256   }
2257 
2258   /* SMARTCARD TX Fifo Empty occurred ----------------------------------------------*/
2259   if (((isrflags & USART_ISR_TXFE) != 0U) && ((cr1its & USART_CR1_TXFEIE) != 0U))
2260   {
2261 #if (USE_HAL_SMARTCARD_REGISTER_CALLBACKS == 1)
2262     /* Call registered Tx Fifo Empty Callback */
2263     hsmartcard->TxFifoEmptyCallback(hsmartcard);
2264 #else
2265     /* Call legacy weak Tx Fifo Empty Callback */
2266     HAL_SMARTCARDEx_TxFifoEmptyCallback(hsmartcard);
2267 #endif /* USE_HAL_SMARTCARD_REGISTER_CALLBACK */
2268     return;
2269   }
2270 
2271   /* SMARTCARD RX Fifo Full occurred ----------------------------------------------*/
2272   if (((isrflags & USART_ISR_RXFF) != 0U) && ((cr1its & USART_CR1_RXFFIE) != 0U))
2273   {
2274 #if (USE_HAL_SMARTCARD_REGISTER_CALLBACKS == 1)
2275     /* Call registered Rx Fifo Full Callback */
2276     hsmartcard->RxFifoFullCallback(hsmartcard);
2277 #else
2278     /* Call legacy weak Rx Fifo Full Callback */
2279     HAL_SMARTCARDEx_RxFifoFullCallback(hsmartcard);
2280 #endif /* USE_HAL_SMARTCARD_REGISTER_CALLBACK */
2281     return;
2282   }
2283 }
2284 
2285 /**
2286   * @brief  Tx Transfer completed callback.
2287   * @param  hsmartcard Pointer to a SMARTCARD_HandleTypeDef structure that contains
2288   *                    the configuration information for the specified SMARTCARD module.
2289   * @retval None
2290   */
HAL_SMARTCARD_TxCpltCallback(SMARTCARD_HandleTypeDef * hsmartcard)2291 __weak void HAL_SMARTCARD_TxCpltCallback(SMARTCARD_HandleTypeDef *hsmartcard)
2292 {
2293   /* Prevent unused argument(s) compilation warning */
2294   UNUSED(hsmartcard);
2295 
2296   /* NOTE : This function should not be modified, when the callback is needed,
2297             the HAL_SMARTCARD_TxCpltCallback can be implemented in the user file.
2298    */
2299 }
2300 
2301 /**
2302   * @brief  Rx Transfer completed callback.
2303   * @param  hsmartcard Pointer to a SMARTCARD_HandleTypeDef structure that contains
2304   *                    the configuration information for the specified SMARTCARD module.
2305   * @retval None
2306   */
HAL_SMARTCARD_RxCpltCallback(SMARTCARD_HandleTypeDef * hsmartcard)2307 __weak void HAL_SMARTCARD_RxCpltCallback(SMARTCARD_HandleTypeDef *hsmartcard)
2308 {
2309   /* Prevent unused argument(s) compilation warning */
2310   UNUSED(hsmartcard);
2311 
2312   /* NOTE : This function should not be modified, when the callback is needed,
2313             the HAL_SMARTCARD_RxCpltCallback can be implemented in the user file.
2314    */
2315 }
2316 
2317 /**
2318   * @brief  SMARTCARD error callback.
2319   * @param  hsmartcard Pointer to a SMARTCARD_HandleTypeDef structure that contains
2320   *                    the configuration information for the specified SMARTCARD module.
2321   * @retval None
2322   */
HAL_SMARTCARD_ErrorCallback(SMARTCARD_HandleTypeDef * hsmartcard)2323 __weak void HAL_SMARTCARD_ErrorCallback(SMARTCARD_HandleTypeDef *hsmartcard)
2324 {
2325   /* Prevent unused argument(s) compilation warning */
2326   UNUSED(hsmartcard);
2327 
2328   /* NOTE : This function should not be modified, when the callback is needed,
2329             the HAL_SMARTCARD_ErrorCallback can be implemented in the user file.
2330    */
2331 }
2332 
2333 /**
2334   * @brief  SMARTCARD Abort Complete callback.
2335   * @param  hsmartcard Pointer to a SMARTCARD_HandleTypeDef structure that contains
2336   *                    the configuration information for the specified SMARTCARD module.
2337   * @retval None
2338   */
HAL_SMARTCARD_AbortCpltCallback(SMARTCARD_HandleTypeDef * hsmartcard)2339 __weak void HAL_SMARTCARD_AbortCpltCallback(SMARTCARD_HandleTypeDef *hsmartcard)
2340 {
2341   /* Prevent unused argument(s) compilation warning */
2342   UNUSED(hsmartcard);
2343 
2344   /* NOTE : This function should not be modified, when the callback is needed,
2345             the HAL_SMARTCARD_AbortCpltCallback can be implemented in the user file.
2346    */
2347 }
2348 
2349 /**
2350   * @brief  SMARTCARD Abort Complete callback.
2351   * @param  hsmartcard Pointer to a SMARTCARD_HandleTypeDef structure that contains
2352   *                    the configuration information for the specified SMARTCARD module.
2353   * @retval None
2354   */
HAL_SMARTCARD_AbortTransmitCpltCallback(SMARTCARD_HandleTypeDef * hsmartcard)2355 __weak void HAL_SMARTCARD_AbortTransmitCpltCallback(SMARTCARD_HandleTypeDef *hsmartcard)
2356 {
2357   /* Prevent unused argument(s) compilation warning */
2358   UNUSED(hsmartcard);
2359 
2360   /* NOTE : This function should not be modified, when the callback is needed,
2361             the HAL_SMARTCARD_AbortTransmitCpltCallback can be implemented in the user file.
2362    */
2363 }
2364 
2365 /**
2366   * @brief  SMARTCARD Abort Receive Complete callback.
2367   * @param  hsmartcard Pointer to a SMARTCARD_HandleTypeDef structure that contains
2368   *                    the configuration information for the specified SMARTCARD module.
2369   * @retval None
2370   */
HAL_SMARTCARD_AbortReceiveCpltCallback(SMARTCARD_HandleTypeDef * hsmartcard)2371 __weak void HAL_SMARTCARD_AbortReceiveCpltCallback(SMARTCARD_HandleTypeDef *hsmartcard)
2372 {
2373   /* Prevent unused argument(s) compilation warning */
2374   UNUSED(hsmartcard);
2375 
2376   /* NOTE : This function should not be modified, when the callback is needed,
2377             the HAL_SMARTCARD_AbortReceiveCpltCallback can be implemented in the user file.
2378    */
2379 }
2380 
2381 /**
2382   * @}
2383   */
2384 
2385 /** @defgroup SMARTCARD_Exported_Functions_Group4 Peripheral State and Errors functions
2386   * @brief    SMARTCARD State and Errors functions
2387   *
2388 @verbatim
2389   ==============================================================================
2390                   ##### Peripheral State and Errors functions #####
2391   ==============================================================================
2392   [..]
2393     This subsection provides a set of functions allowing to return the State of SmartCard
2394     handle and also return Peripheral Errors occurred during communication process
2395      (+) HAL_SMARTCARD_GetState() API can be helpful to check in run-time the state
2396          of the SMARTCARD peripheral.
2397      (+) HAL_SMARTCARD_GetError() checks in run-time errors that could occur during
2398          communication.
2399 
2400 @endverbatim
2401   * @{
2402   */
2403 
2404 /**
2405   * @brief  Return the SMARTCARD handle state.
2406   * @param  hsmartcard Pointer to a SMARTCARD_HandleTypeDef structure that contains
2407   *                    the configuration information for the specified SMARTCARD module.
2408   * @retval SMARTCARD handle state
2409   */
HAL_SMARTCARD_GetState(const SMARTCARD_HandleTypeDef * hsmartcard)2410 HAL_SMARTCARD_StateTypeDef HAL_SMARTCARD_GetState(const SMARTCARD_HandleTypeDef *hsmartcard)
2411 {
2412   /* Return SMARTCARD handle state */
2413   uint32_t temp1;
2414   uint32_t temp2;
2415   temp1 = (uint32_t)hsmartcard->gState;
2416   temp2 = (uint32_t)hsmartcard->RxState;
2417 
2418   return (HAL_SMARTCARD_StateTypeDef)(temp1 | temp2);
2419 }
2420 
2421 /**
2422   * @brief  Return the SMARTCARD handle error code.
2423   * @param  hsmartcard Pointer to a SMARTCARD_HandleTypeDef structure that contains
2424   *                    the configuration information for the specified SMARTCARD module.
2425   * @retval SMARTCARD handle Error Code
2426   */
HAL_SMARTCARD_GetError(const SMARTCARD_HandleTypeDef * hsmartcard)2427 uint32_t HAL_SMARTCARD_GetError(const SMARTCARD_HandleTypeDef *hsmartcard)
2428 {
2429   return hsmartcard->ErrorCode;
2430 }
2431 
2432 /**
2433   * @}
2434   */
2435 
2436 /**
2437   * @}
2438   */
2439 
2440 /** @defgroup SMARTCARD_Private_Functions SMARTCARD Private Functions
2441   * @{
2442   */
2443 
2444 #if (USE_HAL_SMARTCARD_REGISTER_CALLBACKS == 1)
2445 /**
2446   * @brief  Initialize the callbacks to their default values.
2447   * @param  hsmartcard SMARTCARD handle.
2448   * @retval none
2449   */
SMARTCARD_InitCallbacksToDefault(SMARTCARD_HandleTypeDef * hsmartcard)2450 void SMARTCARD_InitCallbacksToDefault(SMARTCARD_HandleTypeDef *hsmartcard)
2451 {
2452   /* Init the SMARTCARD Callback settings */
2453   hsmartcard->TxCpltCallback            = HAL_SMARTCARD_TxCpltCallback;            /* Legacy weak TxCpltCallback    */
2454   hsmartcard->RxCpltCallback            = HAL_SMARTCARD_RxCpltCallback;            /* Legacy weak RxCpltCallback    */
2455   hsmartcard->ErrorCallback             = HAL_SMARTCARD_ErrorCallback;             /* Legacy weak ErrorCallback     */
2456   hsmartcard->AbortCpltCallback         = HAL_SMARTCARD_AbortCpltCallback;         /* Legacy weak AbortCpltCallback */
2457   hsmartcard->AbortTransmitCpltCallback = HAL_SMARTCARD_AbortTransmitCpltCallback; /* Legacy weak
2458                                                                                       AbortTransmitCpltCallback     */
2459   hsmartcard->AbortReceiveCpltCallback  = HAL_SMARTCARD_AbortReceiveCpltCallback;  /* Legacy weak
2460                                                                                       AbortReceiveCpltCallback      */
2461   hsmartcard->RxFifoFullCallback        = HAL_SMARTCARDEx_RxFifoFullCallback;      /* Legacy weak
2462                                                                                       RxFifoFullCallback            */
2463   hsmartcard->TxFifoEmptyCallback       = HAL_SMARTCARDEx_TxFifoEmptyCallback;     /* Legacy weak
2464                                                                                       TxFifoEmptyCallback           */
2465 
2466 }
2467 #endif /* USE_HAL_SMARTCARD_REGISTER_CALLBACKS */
2468 
2469 /**
2470   * @brief  Configure the SMARTCARD associated USART peripheral.
2471   * @param  hsmartcard Pointer to a SMARTCARD_HandleTypeDef structure that contains
2472   *                    the configuration information for the specified SMARTCARD module.
2473   * @retval HAL status
2474   */
SMARTCARD_SetConfig(SMARTCARD_HandleTypeDef * hsmartcard)2475 static HAL_StatusTypeDef SMARTCARD_SetConfig(SMARTCARD_HandleTypeDef *hsmartcard)
2476 {
2477   uint32_t tmpreg;
2478   SMARTCARD_ClockSourceTypeDef clocksource;
2479   HAL_StatusTypeDef ret = HAL_OK;
2480   static const uint16_t SMARTCARDPrescTable[12] = {1U, 2U, 4U, 6U, 8U, 10U, 12U, 16U, 32U, 64U, 128U, 256U};
2481   PLL2_ClocksTypeDef pll2_clocks;
2482 #if defined(RCC_CR_PLL3ON)
2483   PLL3_ClocksTypeDef pll3_clocks;
2484 #endif /* RCC_CR_PLL3ON */
2485   uint32_t pclk;
2486 
2487   /* Check the parameters */
2488   assert_param(IS_SMARTCARD_INSTANCE(hsmartcard->Instance));
2489   assert_param(IS_SMARTCARD_BAUDRATE(hsmartcard->Init.BaudRate));
2490   assert_param(IS_SMARTCARD_WORD_LENGTH(hsmartcard->Init.WordLength));
2491   assert_param(IS_SMARTCARD_STOPBITS(hsmartcard->Init.StopBits));
2492   assert_param(IS_SMARTCARD_PARITY(hsmartcard->Init.Parity));
2493   assert_param(IS_SMARTCARD_MODE(hsmartcard->Init.Mode));
2494   assert_param(IS_SMARTCARD_POLARITY(hsmartcard->Init.CLKPolarity));
2495   assert_param(IS_SMARTCARD_PHASE(hsmartcard->Init.CLKPhase));
2496   assert_param(IS_SMARTCARD_LASTBIT(hsmartcard->Init.CLKLastBit));
2497   assert_param(IS_SMARTCARD_ONE_BIT_SAMPLE(hsmartcard->Init.OneBitSampling));
2498   assert_param(IS_SMARTCARD_NACK(hsmartcard->Init.NACKEnable));
2499   assert_param(IS_SMARTCARD_TIMEOUT(hsmartcard->Init.TimeOutEnable));
2500   assert_param(IS_SMARTCARD_AUTORETRY_COUNT(hsmartcard->Init.AutoRetryCount));
2501   assert_param(IS_SMARTCARD_CLOCKPRESCALER(hsmartcard->Init.ClockPrescaler));
2502 
2503   /*-------------------------- USART CR1 Configuration -----------------------*/
2504   /* In SmartCard mode, M and PCE are forced to 1 (8 bits + parity).
2505    * Oversampling is forced to 16 (OVER8 = 0).
2506    * Configure the Parity and Mode:
2507    *  set PS bit according to hsmartcard->Init.Parity value
2508    *  set TE and RE bits according to hsmartcard->Init.Mode value */
2509   tmpreg = (((uint32_t)hsmartcard->Init.Parity) | ((uint32_t)hsmartcard->Init.Mode) |
2510             ((uint32_t)hsmartcard->Init.WordLength));
2511   MODIFY_REG(hsmartcard->Instance->CR1, USART_CR1_FIELDS, tmpreg);
2512 
2513   /*-------------------------- USART CR2 Configuration -----------------------*/
2514   tmpreg = hsmartcard->Init.StopBits;
2515   /* Synchronous mode is activated by default */
2516   tmpreg |= (uint32_t) USART_CR2_CLKEN | hsmartcard->Init.CLKPolarity;
2517   tmpreg |= (uint32_t) hsmartcard->Init.CLKPhase | hsmartcard->Init.CLKLastBit;
2518   tmpreg |= (uint32_t) hsmartcard->Init.TimeOutEnable;
2519   MODIFY_REG(hsmartcard->Instance->CR2, USART_CR2_FIELDS, tmpreg);
2520 
2521   /*-------------------------- USART CR3 Configuration -----------------------*/
2522   /* Configure
2523    * - one-bit sampling method versus three samples' majority rule
2524    *   according to hsmartcard->Init.OneBitSampling
2525    * - NACK transmission in case of parity error according
2526    *   to hsmartcard->Init.NACKEnable
2527    * - autoretry counter according to hsmartcard->Init.AutoRetryCount */
2528 
2529   tmpreg = (uint32_t) hsmartcard->Init.OneBitSampling | hsmartcard->Init.NACKEnable;
2530   tmpreg |= ((uint32_t)hsmartcard->Init.AutoRetryCount << USART_CR3_SCARCNT_Pos);
2531   MODIFY_REG(hsmartcard->Instance->CR3, USART_CR3_FIELDS, tmpreg);
2532 
2533   /*--------------------- SMARTCARD clock PRESC Configuration ----------------*/
2534   /* Configure
2535   * - SMARTCARD Clock Prescaler: set PRESCALER according to hsmartcard->Init.ClockPrescaler value */
2536   MODIFY_REG(hsmartcard->Instance->PRESC, USART_PRESC_PRESCALER, hsmartcard->Init.ClockPrescaler);
2537 
2538   /*-------------------------- USART GTPR Configuration ----------------------*/
2539   tmpreg = (hsmartcard->Init.Prescaler | ((uint32_t)hsmartcard->Init.GuardTime << USART_GTPR_GT_Pos));
2540   MODIFY_REG(hsmartcard->Instance->GTPR, (uint16_t)(USART_GTPR_GT | USART_GTPR_PSC), (uint16_t)tmpreg);
2541 
2542   /*-------------------------- USART RTOR Configuration ----------------------*/
2543   tmpreg = ((uint32_t)hsmartcard->Init.BlockLength << USART_RTOR_BLEN_Pos);
2544   if (hsmartcard->Init.TimeOutEnable == SMARTCARD_TIMEOUT_ENABLE)
2545   {
2546     assert_param(IS_SMARTCARD_TIMEOUT_VALUE(hsmartcard->Init.TimeOutValue));
2547     tmpreg |= (uint32_t) hsmartcard->Init.TimeOutValue;
2548   }
2549   WRITE_REG(hsmartcard->Instance->RTOR, tmpreg);
2550 
2551   /*-------------------------- USART BRR Configuration -----------------------*/
2552   SMARTCARD_GETCLOCKSOURCE(hsmartcard, clocksource);
2553   tmpreg =   0U;
2554   switch (clocksource)
2555   {
2556     case SMARTCARD_CLOCKSOURCE_PCLK1:
2557       pclk = HAL_RCC_GetPCLK1Freq();
2558       tmpreg = (uint32_t)(((pclk / SMARTCARDPrescTable[hsmartcard->Init.ClockPrescaler]) +
2559                            (hsmartcard->Init.BaudRate / 2U)) / hsmartcard->Init.BaudRate);
2560       break;
2561     case SMARTCARD_CLOCKSOURCE_PCLK2:
2562       pclk = HAL_RCC_GetPCLK2Freq();
2563       tmpreg = (uint32_t)(((pclk / SMARTCARDPrescTable[hsmartcard->Init.ClockPrescaler]) +
2564                            (hsmartcard->Init.BaudRate / 2U)) / hsmartcard->Init.BaudRate);
2565       break;
2566     case SMARTCARD_CLOCKSOURCE_PLL2Q:
2567       HAL_RCCEx_GetPLL2ClockFreq(&pll2_clocks);
2568       tmpreg = (uint32_t)(((pll2_clocks.PLL2_Q_Frequency / SMARTCARDPrescTable[hsmartcard->Init.ClockPrescaler]) +
2569                            (hsmartcard->Init.BaudRate / 2U)) / hsmartcard->Init.BaudRate);
2570       break;
2571 #if defined(RCC_CR_PLL3ON)
2572     case SMARTCARD_CLOCKSOURCE_PLL3Q:
2573       HAL_RCCEx_GetPLL3ClockFreq(&pll3_clocks);
2574       tmpreg = (uint32_t)(((pll3_clocks.PLL3_Q_Frequency / SMARTCARDPrescTable[hsmartcard->Init.ClockPrescaler]) +
2575                            (hsmartcard->Init.BaudRate / 2U)) / hsmartcard->Init.BaudRate);
2576       break;
2577 #endif /* RCC_CR_PLL3ON */
2578     case SMARTCARD_CLOCKSOURCE_HSI:
2579       tmpreg = (uint32_t)(((HSI_VALUE / SMARTCARDPrescTable[hsmartcard->Init.ClockPrescaler]) +
2580                            (hsmartcard->Init.BaudRate / 2U)) / hsmartcard->Init.BaudRate);
2581       break;
2582     case SMARTCARD_CLOCKSOURCE_CSI:
2583       tmpreg = (uint32_t)(((CSI_VALUE / SMARTCARDPrescTable[hsmartcard->Init.ClockPrescaler]) +
2584                            (hsmartcard->Init.BaudRate / 2U)) / hsmartcard->Init.BaudRate);
2585       break;
2586     case SMARTCARD_CLOCKSOURCE_LSE:
2587       tmpreg = (uint32_t)(((uint16_t)(LSE_VALUE / SMARTCARDPrescTable[hsmartcard->Init.ClockPrescaler]) +
2588                            (hsmartcard->Init.BaudRate / 2U)) / hsmartcard->Init.BaudRate);
2589       break;
2590     default:
2591       ret = HAL_ERROR;
2592       break;
2593   }
2594 
2595   /* USARTDIV must be greater than or equal to 0d16 */
2596   if ((tmpreg >= USART_BRR_MIN) && (tmpreg <= USART_BRR_MAX))
2597   {
2598     hsmartcard->Instance->BRR = (uint16_t)tmpreg;
2599   }
2600   else
2601   {
2602     ret = HAL_ERROR;
2603   }
2604 
2605   /* Initialize the number of data to process during RX/TX ISR execution */
2606   hsmartcard->NbTxDataToProcess = 1U;
2607   hsmartcard->NbRxDataToProcess = 1U;
2608 
2609   /* Clear ISR function pointers */
2610   hsmartcard->RxISR   = NULL;
2611   hsmartcard->TxISR   = NULL;
2612 
2613   return ret;
2614 }
2615 
2616 
2617 /**
2618   * @brief Configure the SMARTCARD associated USART peripheral advanced features.
2619   * @param hsmartcard Pointer to a SMARTCARD_HandleTypeDef structure that contains
2620   *                   the configuration information for the specified SMARTCARD module.
2621   * @retval None
2622   */
SMARTCARD_AdvFeatureConfig(SMARTCARD_HandleTypeDef * hsmartcard)2623 static void SMARTCARD_AdvFeatureConfig(SMARTCARD_HandleTypeDef *hsmartcard)
2624 {
2625   /* Check whether the set of advanced features to configure is properly set */
2626   assert_param(IS_SMARTCARD_ADVFEATURE_INIT(hsmartcard->AdvancedInit.AdvFeatureInit));
2627 
2628   /* if required, configure TX pin active level inversion */
2629   if (HAL_IS_BIT_SET(hsmartcard->AdvancedInit.AdvFeatureInit, SMARTCARD_ADVFEATURE_TXINVERT_INIT))
2630   {
2631     assert_param(IS_SMARTCARD_ADVFEATURE_TXINV(hsmartcard->AdvancedInit.TxPinLevelInvert));
2632     MODIFY_REG(hsmartcard->Instance->CR2, USART_CR2_TXINV, hsmartcard->AdvancedInit.TxPinLevelInvert);
2633   }
2634 
2635   /* if required, configure RX pin active level inversion */
2636   if (HAL_IS_BIT_SET(hsmartcard->AdvancedInit.AdvFeatureInit, SMARTCARD_ADVFEATURE_RXINVERT_INIT))
2637   {
2638     assert_param(IS_SMARTCARD_ADVFEATURE_RXINV(hsmartcard->AdvancedInit.RxPinLevelInvert));
2639     MODIFY_REG(hsmartcard->Instance->CR2, USART_CR2_RXINV, hsmartcard->AdvancedInit.RxPinLevelInvert);
2640   }
2641 
2642   /* if required, configure data inversion */
2643   if (HAL_IS_BIT_SET(hsmartcard->AdvancedInit.AdvFeatureInit, SMARTCARD_ADVFEATURE_DATAINVERT_INIT))
2644   {
2645     assert_param(IS_SMARTCARD_ADVFEATURE_DATAINV(hsmartcard->AdvancedInit.DataInvert));
2646     MODIFY_REG(hsmartcard->Instance->CR2, USART_CR2_DATAINV, hsmartcard->AdvancedInit.DataInvert);
2647   }
2648 
2649   /* if required, configure RX/TX pins swap */
2650   if (HAL_IS_BIT_SET(hsmartcard->AdvancedInit.AdvFeatureInit, SMARTCARD_ADVFEATURE_SWAP_INIT))
2651   {
2652     assert_param(IS_SMARTCARD_ADVFEATURE_SWAP(hsmartcard->AdvancedInit.Swap));
2653     MODIFY_REG(hsmartcard->Instance->CR2, USART_CR2_SWAP, hsmartcard->AdvancedInit.Swap);
2654   }
2655 
2656   /* if required, configure RX overrun detection disabling */
2657   if (HAL_IS_BIT_SET(hsmartcard->AdvancedInit.AdvFeatureInit, SMARTCARD_ADVFEATURE_RXOVERRUNDISABLE_INIT))
2658   {
2659     assert_param(IS_SMARTCARD_OVERRUN(hsmartcard->AdvancedInit.OverrunDisable));
2660     MODIFY_REG(hsmartcard->Instance->CR3, USART_CR3_OVRDIS, hsmartcard->AdvancedInit.OverrunDisable);
2661   }
2662 
2663   /* if required, configure DMA disabling on reception error */
2664   if (HAL_IS_BIT_SET(hsmartcard->AdvancedInit.AdvFeatureInit, SMARTCARD_ADVFEATURE_DMADISABLEONERROR_INIT))
2665   {
2666     assert_param(IS_SMARTCARD_ADVFEATURE_DMAONRXERROR(hsmartcard->AdvancedInit.DMADisableonRxError));
2667     MODIFY_REG(hsmartcard->Instance->CR3, USART_CR3_DDRE, hsmartcard->AdvancedInit.DMADisableonRxError);
2668   }
2669 
2670   /* if required, configure MSB first on communication line */
2671   if (HAL_IS_BIT_SET(hsmartcard->AdvancedInit.AdvFeatureInit, SMARTCARD_ADVFEATURE_MSBFIRST_INIT))
2672   {
2673     assert_param(IS_SMARTCARD_ADVFEATURE_MSBFIRST(hsmartcard->AdvancedInit.MSBFirst));
2674     MODIFY_REG(hsmartcard->Instance->CR2, USART_CR2_MSBFIRST, hsmartcard->AdvancedInit.MSBFirst);
2675   }
2676 
2677 }
2678 
2679 /**
2680   * @brief Check the SMARTCARD Idle State.
2681   * @param hsmartcard Pointer to a SMARTCARD_HandleTypeDef structure that contains
2682   *                   the configuration information for the specified SMARTCARD module.
2683   * @retval HAL status
2684   */
SMARTCARD_CheckIdleState(SMARTCARD_HandleTypeDef * hsmartcard)2685 static HAL_StatusTypeDef SMARTCARD_CheckIdleState(SMARTCARD_HandleTypeDef *hsmartcard)
2686 {
2687   uint32_t tickstart;
2688 
2689   /* Initialize the SMARTCARD ErrorCode */
2690   hsmartcard->ErrorCode = HAL_SMARTCARD_ERROR_NONE;
2691 
2692   /* Init tickstart for timeout management */
2693   tickstart = HAL_GetTick();
2694 
2695   /* Check if the Transmitter is enabled */
2696   if ((hsmartcard->Instance->CR1 & USART_CR1_TE) == USART_CR1_TE)
2697   {
2698     /* Wait until TEACK flag is set */
2699     if (SMARTCARD_WaitOnFlagUntilTimeout(hsmartcard, USART_ISR_TEACK, RESET, tickstart,
2700                                          SMARTCARD_TEACK_REACK_TIMEOUT) != HAL_OK)
2701     {
2702       /* Timeout occurred */
2703       return HAL_TIMEOUT;
2704     }
2705   }
2706   /* Check if the Receiver is enabled */
2707   if ((hsmartcard->Instance->CR1 & USART_CR1_RE) == USART_CR1_RE)
2708   {
2709     /* Wait until REACK flag is set */
2710     if (SMARTCARD_WaitOnFlagUntilTimeout(hsmartcard, USART_ISR_REACK, RESET, tickstart,
2711                                          SMARTCARD_TEACK_REACK_TIMEOUT) != HAL_OK)
2712     {
2713       /* Timeout occurred */
2714       return HAL_TIMEOUT;
2715     }
2716   }
2717 
2718   /* Initialize the SMARTCARD states */
2719   hsmartcard->gState  = HAL_SMARTCARD_STATE_READY;
2720   hsmartcard->RxState = HAL_SMARTCARD_STATE_READY;
2721 
2722   /* Process Unlocked */
2723   __HAL_UNLOCK(hsmartcard);
2724 
2725   return HAL_OK;
2726 }
2727 
2728 /**
2729   * @brief  Handle SMARTCARD Communication Timeout. It waits
2730   *         until a flag is no longer in the specified status.
2731   * @param  hsmartcard Pointer to a SMARTCARD_HandleTypeDef structure that contains
2732   *                   the configuration information for the specified SMARTCARD module.
2733   * @param  Flag Specifies the SMARTCARD flag to check.
2734   * @param  Status The actual Flag status (SET or RESET).
2735   * @param  Tickstart Tick start value
2736   * @param  Timeout Timeout duration.
2737   * @retval HAL status
2738   */
SMARTCARD_WaitOnFlagUntilTimeout(SMARTCARD_HandleTypeDef * hsmartcard,uint32_t Flag,FlagStatus Status,uint32_t Tickstart,uint32_t Timeout)2739 static HAL_StatusTypeDef SMARTCARD_WaitOnFlagUntilTimeout(SMARTCARD_HandleTypeDef *hsmartcard, uint32_t Flag,
2740                                                           FlagStatus Status, uint32_t Tickstart, uint32_t Timeout)
2741 {
2742   /* Wait until flag is set */
2743   while ((__HAL_SMARTCARD_GET_FLAG(hsmartcard, Flag) ? SET : RESET) == Status)
2744   {
2745     /* Check for the Timeout */
2746     if (Timeout != HAL_MAX_DELAY)
2747     {
2748       if (((HAL_GetTick() - Tickstart) > Timeout) || (Timeout == 0U))
2749       {
2750         /* Disable TXE, RXNE, PE and ERR (Frame error, noise error, overrun error)
2751            interrupts for the interrupt process */
2752         CLEAR_BIT(hsmartcard->Instance->CR1, (USART_CR1_RXNEIE_RXFNEIE | USART_CR1_PEIE | USART_CR1_TXEIE_TXFNFIE));
2753         CLEAR_BIT(hsmartcard->Instance->CR3, USART_CR3_EIE);
2754 
2755         hsmartcard->gState  = HAL_SMARTCARD_STATE_READY;
2756         hsmartcard->RxState = HAL_SMARTCARD_STATE_READY;
2757 
2758         /* Process Unlocked */
2759         __HAL_UNLOCK(hsmartcard);
2760         return HAL_TIMEOUT;
2761       }
2762     }
2763   }
2764   return HAL_OK;
2765 }
2766 
2767 
2768 /**
2769   * @brief  End ongoing Tx transfer on SMARTCARD peripheral (following error detection or Transmit completion).
2770   * @param  hsmartcard Pointer to a SMARTCARD_HandleTypeDef structure that contains
2771   *                    the configuration information for the specified SMARTCARD module.
2772   * @retval None
2773   */
SMARTCARD_EndTxTransfer(SMARTCARD_HandleTypeDef * hsmartcard)2774 static void SMARTCARD_EndTxTransfer(SMARTCARD_HandleTypeDef *hsmartcard)
2775 {
2776   /* Disable TXEIE, TCIE and ERR (Frame error, noise error, overrun error) interrupts */
2777   CLEAR_BIT(hsmartcard->Instance->CR1, (USART_CR1_TXEIE_TXFNFIE | USART_CR1_TCIE));
2778   CLEAR_BIT(hsmartcard->Instance->CR3, USART_CR3_EIE);
2779 
2780   /* At end of Tx process, restore hsmartcard->gState to Ready */
2781   hsmartcard->gState = HAL_SMARTCARD_STATE_READY;
2782 }
2783 
2784 
2785 /**
2786   * @brief  End ongoing Rx transfer on UART peripheral (following error detection or Reception completion).
2787   * @param  hsmartcard Pointer to a SMARTCARD_HandleTypeDef structure that contains
2788   *                    the configuration information for the specified SMARTCARD module.
2789   * @retval None
2790   */
SMARTCARD_EndRxTransfer(SMARTCARD_HandleTypeDef * hsmartcard)2791 static void SMARTCARD_EndRxTransfer(SMARTCARD_HandleTypeDef *hsmartcard)
2792 {
2793   /* Disable RXNE, PE and ERR (Frame error, noise error, overrun error) interrupts */
2794   CLEAR_BIT(hsmartcard->Instance->CR1, (USART_CR1_RXNEIE_RXFNEIE | USART_CR1_PEIE));
2795   CLEAR_BIT(hsmartcard->Instance->CR3, USART_CR3_EIE);
2796 
2797   /* At end of Rx process, restore hsmartcard->RxState to Ready */
2798   hsmartcard->RxState = HAL_SMARTCARD_STATE_READY;
2799 }
2800 
2801 
2802 #if defined(HAL_DMA_MODULE_ENABLED)
2803 /**
2804   * @brief  DMA SMARTCARD transmit process complete callback.
2805   * @param  hdma Pointer to a DMA_HandleTypeDef structure that contains
2806   *              the configuration information for the specified DMA module.
2807   * @retval None
2808   */
SMARTCARD_DMATransmitCplt(DMA_HandleTypeDef * hdma)2809 static void SMARTCARD_DMATransmitCplt(DMA_HandleTypeDef *hdma)
2810 {
2811   SMARTCARD_HandleTypeDef *hsmartcard = (SMARTCARD_HandleTypeDef *)(hdma->Parent);
2812   hsmartcard->TxXferCount = 0U;
2813 
2814 #if !defined(USART_DMAREQUESTS_SW_WA)
2815   /* Disable the DMA transfer for transmit request by resetting the DMAT bit
2816   in the SMARTCARD associated USART CR3 register */
2817   CLEAR_BIT(hsmartcard->Instance->CR3, USART_CR3_DMAT);
2818 
2819 #endif /* USART_DMAREQUESTS_SW_WA */
2820   /* Enable the SMARTCARD Transmit Complete Interrupt */
2821   __HAL_SMARTCARD_ENABLE_IT(hsmartcard, hsmartcard->AdvancedInit.TxCompletionIndication);
2822 }
2823 
2824 /**
2825   * @brief  DMA SMARTCARD receive process complete callback.
2826   * @param  hdma Pointer to a DMA_HandleTypeDef structure that contains
2827   *              the configuration information for the specified DMA module.
2828   * @retval None
2829   */
SMARTCARD_DMAReceiveCplt(DMA_HandleTypeDef * hdma)2830 static void SMARTCARD_DMAReceiveCplt(DMA_HandleTypeDef *hdma)
2831 {
2832   SMARTCARD_HandleTypeDef *hsmartcard = (SMARTCARD_HandleTypeDef *)(hdma->Parent);
2833   hsmartcard->RxXferCount = 0U;
2834 
2835   /* Disable PE and ERR (Frame error, noise error, overrun error) interrupts */
2836   CLEAR_BIT(hsmartcard->Instance->CR1, USART_CR1_PEIE);
2837   CLEAR_BIT(hsmartcard->Instance->CR3, USART_CR3_EIE);
2838 
2839 #if !defined(USART_DMAREQUESTS_SW_WA)
2840   /* Disable the DMA transfer for the receiver request by resetting the DMAR bit
2841      in the SMARTCARD associated USART CR3 register */
2842   CLEAR_BIT(hsmartcard->Instance->CR3, USART_CR3_DMAR);
2843 
2844 #endif /* USART_DMAREQUESTS_SW_WA */
2845   /* At end of Rx process, restore hsmartcard->RxState to Ready */
2846   hsmartcard->RxState = HAL_SMARTCARD_STATE_READY;
2847 
2848 #if (USE_HAL_SMARTCARD_REGISTER_CALLBACKS == 1)
2849   /* Call registered Rx complete callback */
2850   hsmartcard->RxCpltCallback(hsmartcard);
2851 #else
2852   /* Call legacy weak Rx complete callback */
2853   HAL_SMARTCARD_RxCpltCallback(hsmartcard);
2854 #endif /* USE_HAL_SMARTCARD_REGISTER_CALLBACK */
2855 }
2856 
2857 /**
2858   * @brief  DMA SMARTCARD communication error callback.
2859   * @param  hdma Pointer to a DMA_HandleTypeDef structure that contains
2860   *              the configuration information for the specified DMA module.
2861   * @retval None
2862   */
SMARTCARD_DMAError(DMA_HandleTypeDef * hdma)2863 static void SMARTCARD_DMAError(DMA_HandleTypeDef *hdma)
2864 {
2865   SMARTCARD_HandleTypeDef *hsmartcard = (SMARTCARD_HandleTypeDef *)(hdma->Parent);
2866 
2867   /* Stop SMARTCARD DMA Tx request if ongoing */
2868   if (hsmartcard->gState == HAL_SMARTCARD_STATE_BUSY_TX)
2869   {
2870     if (HAL_IS_BIT_SET(hsmartcard->Instance->CR3, USART_CR3_DMAT))
2871     {
2872       hsmartcard->TxXferCount = 0U;
2873       SMARTCARD_EndTxTransfer(hsmartcard);
2874     }
2875   }
2876 
2877   /* Stop SMARTCARD DMA Rx request if ongoing */
2878   if (hsmartcard->RxState == HAL_SMARTCARD_STATE_BUSY_RX)
2879   {
2880     if (HAL_IS_BIT_SET(hsmartcard->Instance->CR3, USART_CR3_DMAR))
2881     {
2882       hsmartcard->RxXferCount = 0U;
2883       SMARTCARD_EndRxTransfer(hsmartcard);
2884     }
2885   }
2886 
2887   hsmartcard->ErrorCode |= HAL_SMARTCARD_ERROR_DMA;
2888 #if (USE_HAL_SMARTCARD_REGISTER_CALLBACKS == 1)
2889   /* Call registered user error callback */
2890   hsmartcard->ErrorCallback(hsmartcard);
2891 #else
2892   /* Call legacy weak user error callback */
2893   HAL_SMARTCARD_ErrorCallback(hsmartcard);
2894 #endif /* USE_HAL_SMARTCARD_REGISTER_CALLBACK */
2895 }
2896 
2897 /**
2898   * @brief  DMA SMARTCARD communication abort callback, when initiated by HAL services on Error
2899   *         (To be called at end of DMA Abort procedure following error occurrence).
2900   * @param  hdma DMA handle.
2901   * @retval None
2902   */
SMARTCARD_DMAAbortOnError(DMA_HandleTypeDef * hdma)2903 static void SMARTCARD_DMAAbortOnError(DMA_HandleTypeDef *hdma)
2904 {
2905   SMARTCARD_HandleTypeDef *hsmartcard = (SMARTCARD_HandleTypeDef *)(hdma->Parent);
2906   hsmartcard->RxXferCount = 0U;
2907   hsmartcard->TxXferCount = 0U;
2908 
2909 #if (USE_HAL_SMARTCARD_REGISTER_CALLBACKS == 1)
2910   /* Call registered user error callback */
2911   hsmartcard->ErrorCallback(hsmartcard);
2912 #else
2913   /* Call legacy weak user error callback */
2914   HAL_SMARTCARD_ErrorCallback(hsmartcard);
2915 #endif /* USE_HAL_SMARTCARD_REGISTER_CALLBACK */
2916 }
2917 
2918 /**
2919   * @brief  DMA SMARTCARD Tx communication abort callback, when initiated by user
2920   *         (To be called at end of DMA Tx Abort procedure following user abort request).
2921   * @note   When this callback is executed, User Abort complete call back is called only if no
2922   *         Abort still ongoing for Rx DMA Handle.
2923   * @param  hdma DMA handle.
2924   * @retval None
2925   */
SMARTCARD_DMATxAbortCallback(DMA_HandleTypeDef * hdma)2926 static void SMARTCARD_DMATxAbortCallback(DMA_HandleTypeDef *hdma)
2927 {
2928   SMARTCARD_HandleTypeDef *hsmartcard = (SMARTCARD_HandleTypeDef *)(hdma->Parent);
2929 
2930   hsmartcard->hdmatx->XferAbortCallback = NULL;
2931 
2932   /* Check if an Abort process is still ongoing */
2933   if (hsmartcard->hdmarx != NULL)
2934   {
2935     if (hsmartcard->hdmarx->XferAbortCallback != NULL)
2936     {
2937       return;
2938     }
2939   }
2940 
2941   /* No Abort process still ongoing : All DMA channels are aborted, call user Abort Complete callback */
2942   hsmartcard->TxXferCount = 0U;
2943   hsmartcard->RxXferCount = 0U;
2944 
2945   /* Reset errorCode */
2946   hsmartcard->ErrorCode = HAL_SMARTCARD_ERROR_NONE;
2947 
2948   /* Clear the Error flags in the ICR register */
2949   __HAL_SMARTCARD_CLEAR_FLAG(hsmartcard,
2950                              SMARTCARD_CLEAR_OREF | SMARTCARD_CLEAR_NEF | SMARTCARD_CLEAR_PEF | SMARTCARD_CLEAR_FEF |
2951                              SMARTCARD_CLEAR_RTOF | SMARTCARD_CLEAR_EOBF);
2952 
2953   /* Restore hsmartcard->gState and hsmartcard->RxState to Ready */
2954   hsmartcard->gState  = HAL_SMARTCARD_STATE_READY;
2955   hsmartcard->RxState = HAL_SMARTCARD_STATE_READY;
2956 
2957 #if (USE_HAL_SMARTCARD_REGISTER_CALLBACKS == 1)
2958   /* Call registered Abort complete callback */
2959   hsmartcard->AbortCpltCallback(hsmartcard);
2960 #else
2961   /* Call legacy weak Abort complete callback */
2962   HAL_SMARTCARD_AbortCpltCallback(hsmartcard);
2963 #endif /* USE_HAL_SMARTCARD_REGISTER_CALLBACK */
2964 }
2965 
2966 
2967 /**
2968   * @brief  DMA SMARTCARD Rx communication abort callback, when initiated by user
2969   *         (To be called at end of DMA Rx Abort procedure following user abort request).
2970   * @note   When this callback is executed, User Abort complete call back is called only if no
2971   *         Abort still ongoing for Tx DMA Handle.
2972   * @param  hdma DMA handle.
2973   * @retval None
2974   */
SMARTCARD_DMARxAbortCallback(DMA_HandleTypeDef * hdma)2975 static void SMARTCARD_DMARxAbortCallback(DMA_HandleTypeDef *hdma)
2976 {
2977   SMARTCARD_HandleTypeDef *hsmartcard = (SMARTCARD_HandleTypeDef *)(hdma->Parent);
2978 
2979   hsmartcard->hdmarx->XferAbortCallback = NULL;
2980 
2981   /* Check if an Abort process is still ongoing */
2982   if (hsmartcard->hdmatx != NULL)
2983   {
2984     if (hsmartcard->hdmatx->XferAbortCallback != NULL)
2985     {
2986       return;
2987     }
2988   }
2989 
2990   /* No Abort process still ongoing : All DMA channels are aborted, call user Abort Complete callback */
2991   hsmartcard->TxXferCount = 0U;
2992   hsmartcard->RxXferCount = 0U;
2993 
2994   /* Reset errorCode */
2995   hsmartcard->ErrorCode = HAL_SMARTCARD_ERROR_NONE;
2996 
2997   /* Clear the Error flags in the ICR register */
2998   __HAL_SMARTCARD_CLEAR_FLAG(hsmartcard,
2999                              SMARTCARD_CLEAR_OREF | SMARTCARD_CLEAR_NEF | SMARTCARD_CLEAR_PEF | SMARTCARD_CLEAR_FEF |
3000                              SMARTCARD_CLEAR_RTOF | SMARTCARD_CLEAR_EOBF);
3001 
3002   /* Restore hsmartcard->gState and hsmartcard->RxState to Ready */
3003   hsmartcard->gState  = HAL_SMARTCARD_STATE_READY;
3004   hsmartcard->RxState = HAL_SMARTCARD_STATE_READY;
3005 
3006 #if (USE_HAL_SMARTCARD_REGISTER_CALLBACKS == 1)
3007   /* Call registered Abort complete callback */
3008   hsmartcard->AbortCpltCallback(hsmartcard);
3009 #else
3010   /* Call legacy weak Abort complete callback */
3011   HAL_SMARTCARD_AbortCpltCallback(hsmartcard);
3012 #endif /* USE_HAL_SMARTCARD_REGISTER_CALLBACK */
3013 }
3014 
3015 
3016 /**
3017   * @brief  DMA SMARTCARD Tx communication abort callback, when initiated by user by a call to
3018   *         HAL_SMARTCARD_AbortTransmit_IT API (Abort only Tx transfer)
3019   *         (This callback is executed at end of DMA Tx Abort procedure following user abort request,
3020   *         and leads to user Tx Abort Complete callback execution).
3021   * @param  hdma DMA handle.
3022   * @retval None
3023   */
SMARTCARD_DMATxOnlyAbortCallback(DMA_HandleTypeDef * hdma)3024 static void SMARTCARD_DMATxOnlyAbortCallback(DMA_HandleTypeDef *hdma)
3025 {
3026   SMARTCARD_HandleTypeDef *hsmartcard = (SMARTCARD_HandleTypeDef *)(hdma->Parent);
3027 
3028   hsmartcard->TxXferCount = 0U;
3029 
3030   /* Clear the Error flags in the ICR register */
3031   __HAL_SMARTCARD_CLEAR_FLAG(hsmartcard, SMARTCARD_CLEAR_FEF);
3032 
3033   /* Restore hsmartcard->gState to Ready */
3034   hsmartcard->gState = HAL_SMARTCARD_STATE_READY;
3035 
3036 #if (USE_HAL_SMARTCARD_REGISTER_CALLBACKS == 1)
3037   /* Call registered Abort Transmit Complete Callback */
3038   hsmartcard->AbortTransmitCpltCallback(hsmartcard);
3039 #else
3040   /* Call legacy weak Abort Transmit Complete Callback */
3041   HAL_SMARTCARD_AbortTransmitCpltCallback(hsmartcard);
3042 #endif /* USE_HAL_SMARTCARD_REGISTER_CALLBACK */
3043 }
3044 
3045 /**
3046   * @brief  DMA SMARTCARD Rx communication abort callback, when initiated by user by a call to
3047   *         HAL_SMARTCARD_AbortReceive_IT API (Abort only Rx transfer)
3048   *         (This callback is executed at end of DMA Rx Abort procedure following user abort request,
3049   *         and leads to user Rx Abort Complete callback execution).
3050   * @param  hdma DMA handle.
3051   * @retval None
3052   */
SMARTCARD_DMARxOnlyAbortCallback(DMA_HandleTypeDef * hdma)3053 static void SMARTCARD_DMARxOnlyAbortCallback(DMA_HandleTypeDef *hdma)
3054 {
3055   SMARTCARD_HandleTypeDef *hsmartcard = (SMARTCARD_HandleTypeDef *)(hdma->Parent);
3056 
3057   hsmartcard->RxXferCount = 0U;
3058 
3059   /* Clear the Error flags in the ICR register */
3060   __HAL_SMARTCARD_CLEAR_FLAG(hsmartcard,
3061                              SMARTCARD_CLEAR_OREF | SMARTCARD_CLEAR_NEF | SMARTCARD_CLEAR_PEF | SMARTCARD_CLEAR_FEF |
3062                              SMARTCARD_CLEAR_RTOF | SMARTCARD_CLEAR_EOBF);
3063 
3064   /* Restore hsmartcard->RxState to Ready */
3065   hsmartcard->RxState = HAL_SMARTCARD_STATE_READY;
3066 
3067 #if (USE_HAL_SMARTCARD_REGISTER_CALLBACKS == 1)
3068   /* Call registered Abort Receive Complete Callback */
3069   hsmartcard->AbortReceiveCpltCallback(hsmartcard);
3070 #else
3071   /* Call legacy weak Abort Receive Complete Callback */
3072   HAL_SMARTCARD_AbortReceiveCpltCallback(hsmartcard);
3073 #endif /* USE_HAL_SMARTCARD_REGISTER_CALLBACK */
3074 }
3075 #endif /* HAL_DMA_MODULE_ENABLED */
3076 
3077 /**
3078   * @brief  Send an amount of data in non-blocking mode.
3079   * @note   Function called under interruption only, once
3080   *         interruptions have been enabled by HAL_SMARTCARD_Transmit_IT()
3081   *         and when the FIFO mode is disabled.
3082   * @param  hsmartcard Pointer to a SMARTCARD_HandleTypeDef structure that contains
3083   *                    the configuration information for the specified SMARTCARD module.
3084   * @retval None
3085   */
SMARTCARD_TxISR(SMARTCARD_HandleTypeDef * hsmartcard)3086 static void SMARTCARD_TxISR(SMARTCARD_HandleTypeDef *hsmartcard)
3087 {
3088   /* Check that a Tx process is ongoing */
3089   if (hsmartcard->gState == HAL_SMARTCARD_STATE_BUSY_TX)
3090   {
3091     if (hsmartcard->TxXferCount == 0U)
3092     {
3093       /* Disable the SMARTCARD Transmit Data Register Empty Interrupt */
3094       CLEAR_BIT(hsmartcard->Instance->CR1, USART_CR1_TXEIE_TXFNFIE);
3095 
3096       /* Enable the SMARTCARD Transmit Complete Interrupt */
3097       __HAL_SMARTCARD_ENABLE_IT(hsmartcard, hsmartcard->AdvancedInit.TxCompletionIndication);
3098     }
3099     else
3100     {
3101       hsmartcard->Instance->TDR = (uint8_t)(*hsmartcard->pTxBuffPtr & 0xFFU);
3102       hsmartcard->pTxBuffPtr++;
3103       hsmartcard->TxXferCount--;
3104     }
3105   }
3106 }
3107 
3108 /**
3109   * @brief  Send an amount of data in non-blocking mode.
3110   * @note   Function called under interruption only, once
3111   *         interruptions have been enabled by HAL_SMARTCARD_Transmit_IT()
3112   *         and when the FIFO mode is enabled.
3113   * @param  hsmartcard Pointer to a SMARTCARD_HandleTypeDef structure that contains
3114   *                    the configuration information for the specified SMARTCARD module.
3115   * @retval None
3116   */
SMARTCARD_TxISR_FIFOEN(SMARTCARD_HandleTypeDef * hsmartcard)3117 static void SMARTCARD_TxISR_FIFOEN(SMARTCARD_HandleTypeDef *hsmartcard)
3118 {
3119   uint16_t   nb_tx_data;
3120 
3121   /* Check that a Tx process is ongoing */
3122   if (hsmartcard->gState == HAL_SMARTCARD_STATE_BUSY_TX)
3123   {
3124     for (nb_tx_data = hsmartcard->NbTxDataToProcess ; nb_tx_data > 0U ; nb_tx_data--)
3125     {
3126       if (hsmartcard->TxXferCount == 0U)
3127       {
3128         /* Disable the SMARTCARD Transmit Data Register Empty Interrupt */
3129         CLEAR_BIT(hsmartcard->Instance->CR1, USART_CR1_TXEIE_TXFNFIE);
3130 
3131         /* Enable the SMARTCARD Transmit Complete Interrupt */
3132         __HAL_SMARTCARD_ENABLE_IT(hsmartcard, hsmartcard->AdvancedInit.TxCompletionIndication);
3133       }
3134       else if (READ_BIT(hsmartcard->Instance->ISR, USART_ISR_TXE_TXFNF) != 0U)
3135       {
3136         hsmartcard->Instance->TDR = (uint8_t)(*hsmartcard->pTxBuffPtr & 0xFFU);
3137         hsmartcard->pTxBuffPtr++;
3138         hsmartcard->TxXferCount--;
3139       }
3140       else
3141       {
3142         /* Nothing to do */
3143       }
3144     }
3145   }
3146 }
3147 
3148 /**
3149   * @brief  Wrap up transmission in non-blocking mode.
3150   * @param  hsmartcard Pointer to a SMARTCARD_HandleTypeDef structure that contains
3151   *                    the configuration information for the specified SMARTCARD module.
3152   * @retval None
3153   */
SMARTCARD_EndTransmit_IT(SMARTCARD_HandleTypeDef * hsmartcard)3154 static void SMARTCARD_EndTransmit_IT(SMARTCARD_HandleTypeDef *hsmartcard)
3155 {
3156   /* Disable the SMARTCARD Transmit Complete Interrupt */
3157   __HAL_SMARTCARD_DISABLE_IT(hsmartcard, hsmartcard->AdvancedInit.TxCompletionIndication);
3158 
3159   /* Check if a receive process is ongoing or not. If not disable ERR IT */
3160   if (hsmartcard->RxState == HAL_SMARTCARD_STATE_READY)
3161   {
3162     /* Disable the SMARTCARD Error Interrupt: (Frame error) */
3163     CLEAR_BIT(hsmartcard->Instance->CR3, USART_CR3_EIE);
3164   }
3165 
3166   /* Disable the Peripheral first to update mode */
3167   CLEAR_BIT(hsmartcard->Instance->CR1, USART_CR1_UE);
3168   if ((hsmartcard->Init.Mode == SMARTCARD_MODE_TX)
3169       && (hsmartcard->Init.NACKEnable == SMARTCARD_NACK_ENABLE))
3170   {
3171     /* In case of TX only mode, if NACK is enabled, receiver block has been enabled
3172        for Transmit phase. Disable this receiver block. */
3173     CLEAR_BIT(hsmartcard->Instance->CR1, USART_CR1_RE);
3174   }
3175   if ((hsmartcard->Init.Mode == SMARTCARD_MODE_TX_RX)
3176       || (hsmartcard->Init.NACKEnable == SMARTCARD_NACK_ENABLE))
3177   {
3178     /* Perform a TX FIFO Flush at end of Tx phase, as all sent bytes are appearing in Rx Data register */
3179     __HAL_SMARTCARD_FLUSH_DRREGISTER(hsmartcard);
3180   }
3181   SET_BIT(hsmartcard->Instance->CR1, USART_CR1_UE);
3182 
3183   /* Tx process is ended, restore hsmartcard->gState to Ready */
3184   hsmartcard->gState = HAL_SMARTCARD_STATE_READY;
3185 
3186   /* Clear TxISR function pointer */
3187   hsmartcard->TxISR = NULL;
3188 
3189 #if (USE_HAL_SMARTCARD_REGISTER_CALLBACKS == 1)
3190   /* Call registered Tx complete callback */
3191   hsmartcard->TxCpltCallback(hsmartcard);
3192 #else
3193   /* Call legacy weak Tx complete callback */
3194   HAL_SMARTCARD_TxCpltCallback(hsmartcard);
3195 #endif /* USE_HAL_SMARTCARD_REGISTER_CALLBACK */
3196 }
3197 
3198 /**
3199   * @brief  Receive an amount of data in non-blocking mode.
3200   * @note   Function called under interruption only, once
3201   *         interruptions have been enabled by HAL_SMARTCARD_Receive_IT()
3202   *         and when the FIFO mode is disabled.
3203   * @param  hsmartcard Pointer to a SMARTCARD_HandleTypeDef structure that contains
3204   *                    the configuration information for the specified SMARTCARD module.
3205   * @retval None
3206   */
SMARTCARD_RxISR(SMARTCARD_HandleTypeDef * hsmartcard)3207 static void SMARTCARD_RxISR(SMARTCARD_HandleTypeDef *hsmartcard)
3208 {
3209   /* Check that a Rx process is ongoing */
3210   if (hsmartcard->RxState == HAL_SMARTCARD_STATE_BUSY_RX)
3211   {
3212     *hsmartcard->pRxBuffPtr = (uint8_t)(hsmartcard->Instance->RDR & (uint8_t)0xFF);
3213     hsmartcard->pRxBuffPtr++;
3214 
3215     hsmartcard->RxXferCount--;
3216     if (hsmartcard->RxXferCount == 0U)
3217     {
3218       CLEAR_BIT(hsmartcard->Instance->CR1, USART_CR1_RXNEIE_RXFNEIE);
3219 
3220       /* Check if a transmit process is ongoing or not. If not disable ERR IT */
3221       if (hsmartcard->gState == HAL_SMARTCARD_STATE_READY)
3222       {
3223         /* Disable the SMARTCARD Error Interrupt: (Frame error, noise error, overrun error) */
3224         CLEAR_BIT(hsmartcard->Instance->CR3, USART_CR3_EIE);
3225       }
3226 
3227       /* Disable the SMARTCARD Parity Error Interrupt */
3228       CLEAR_BIT(hsmartcard->Instance->CR1, USART_CR1_PEIE);
3229 
3230       hsmartcard->RxState = HAL_SMARTCARD_STATE_READY;
3231 
3232       /* Clear RxISR function pointer */
3233       hsmartcard->RxISR = NULL;
3234 
3235 #if (USE_HAL_SMARTCARD_REGISTER_CALLBACKS == 1)
3236       /* Call registered Rx complete callback */
3237       hsmartcard->RxCpltCallback(hsmartcard);
3238 #else
3239       /* Call legacy weak Rx complete callback */
3240       HAL_SMARTCARD_RxCpltCallback(hsmartcard);
3241 #endif /* USE_HAL_SMARTCARD_REGISTER_CALLBACK */
3242     }
3243   }
3244   else
3245   {
3246     /* Clear RXNE interrupt flag */
3247     __HAL_SMARTCARD_SEND_REQ(hsmartcard, SMARTCARD_RXDATA_FLUSH_REQUEST);
3248   }
3249 }
3250 
3251 /**
3252   * @brief  Receive an amount of data in non-blocking mode.
3253   * @note   Function called under interruption only, once
3254   *         interruptions have been enabled by HAL_SMARTCARD_Receive_IT()
3255   *         and when the FIFO mode is enabled.
3256   * @param  hsmartcard Pointer to a SMARTCARD_HandleTypeDef structure that contains
3257   *                    the configuration information for the specified SMARTCARD module.
3258   * @retval None
3259   */
SMARTCARD_RxISR_FIFOEN(SMARTCARD_HandleTypeDef * hsmartcard)3260 static void SMARTCARD_RxISR_FIFOEN(SMARTCARD_HandleTypeDef *hsmartcard)
3261 {
3262   uint16_t   nb_rx_data;
3263   uint16_t rxdatacount;
3264 
3265   /* Check that a Rx process is ongoing */
3266   if (hsmartcard->RxState == HAL_SMARTCARD_STATE_BUSY_RX)
3267   {
3268     for (nb_rx_data = hsmartcard->NbRxDataToProcess ; nb_rx_data > 0U ; nb_rx_data--)
3269     {
3270       *hsmartcard->pRxBuffPtr = (uint8_t)(hsmartcard->Instance->RDR & (uint8_t)0xFF);
3271       hsmartcard->pRxBuffPtr++;
3272 
3273       hsmartcard->RxXferCount--;
3274       if (hsmartcard->RxXferCount == 0U)
3275       {
3276         CLEAR_BIT(hsmartcard->Instance->CR1, USART_CR1_RXNEIE_RXFNEIE);
3277 
3278         /* Check if a transmit process is ongoing or not. If not disable ERR IT */
3279         if (hsmartcard->gState == HAL_SMARTCARD_STATE_READY)
3280         {
3281           /* Disable the SMARTCARD Error Interrupt: (Frame error, noise error, overrun error) */
3282           CLEAR_BIT(hsmartcard->Instance->CR3, USART_CR3_EIE);
3283         }
3284 
3285         /* Disable the SMARTCARD Parity Error Interrupt */
3286         CLEAR_BIT(hsmartcard->Instance->CR1, USART_CR1_PEIE);
3287 
3288         hsmartcard->RxState = HAL_SMARTCARD_STATE_READY;
3289 
3290         /* Clear RxISR function pointer */
3291         hsmartcard->RxISR = NULL;
3292 
3293 #if (USE_HAL_SMARTCARD_REGISTER_CALLBACKS == 1)
3294         /* Call registered Rx complete callback */
3295         hsmartcard->RxCpltCallback(hsmartcard);
3296 #else
3297         /* Call legacy weak Rx complete callback */
3298         HAL_SMARTCARD_RxCpltCallback(hsmartcard);
3299 #endif /* USE_HAL_SMARTCARD_REGISTER_CALLBACK */
3300       }
3301     }
3302 
3303     /* When remaining number of bytes to receive is less than the RX FIFO
3304     threshold, next incoming frames are processed as if FIFO mode was
3305     disabled (i.e. one interrupt per received frame).
3306     */
3307     rxdatacount = hsmartcard->RxXferCount;
3308     if (((rxdatacount != 0U)) && (rxdatacount < hsmartcard->NbRxDataToProcess))
3309     {
3310       /* Disable the UART RXFT interrupt*/
3311       CLEAR_BIT(hsmartcard->Instance->CR3, USART_CR3_RXFTIE);
3312 
3313       /* Update the RxISR function pointer */
3314       hsmartcard->RxISR = SMARTCARD_RxISR;
3315 
3316       /* Enable the UART Data Register Not Empty interrupt */
3317       SET_BIT(hsmartcard->Instance->CR1, USART_CR1_RXNEIE_RXFNEIE);
3318     }
3319   }
3320   else
3321   {
3322     /* Clear RXNE interrupt flag */
3323     __HAL_SMARTCARD_SEND_REQ(hsmartcard, SMARTCARD_RXDATA_FLUSH_REQUEST);
3324   }
3325 }
3326 
3327 /**
3328   * @}
3329   */
3330 
3331 #endif /* HAL_SMARTCARD_MODULE_ENABLED */
3332 /**
3333   * @}
3334   */
3335 
3336 /**
3337   * @}
3338   */
3339