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