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