1 /**
2   ******************************************************************************
3   * @file    stm32f7xx_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) 2017 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 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 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 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 callbacks are used.
173 
174 
175   @endverbatim
176   ******************************************************************************
177   */
178 
179 /* Includes ------------------------------------------------------------------*/
180 #include "stm32f7xx_hal.h"
181 
182 /** @addtogroup STM32F7xx_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 to override 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 #if defined(USART_ISR_REACK)
2424   /* Check if the Receiver is enabled */
2425   if ((hsmartcard->Instance->CR1 & USART_CR1_RE) == USART_CR1_RE)
2426   {
2427     /* Wait until REACK flag is set */
2428     if (SMARTCARD_WaitOnFlagUntilTimeout(hsmartcard, USART_ISR_REACK, RESET, tickstart,
2429                                          SMARTCARD_TEACK_REACK_TIMEOUT) != HAL_OK)
2430     {
2431       /* Timeout occurred */
2432       return HAL_TIMEOUT;
2433     }
2434   }
2435 #endif /* USART_ISR_REACK */
2436 
2437   /* Initialize the SMARTCARD states */
2438   hsmartcard->gState  = HAL_SMARTCARD_STATE_READY;
2439   hsmartcard->RxState = HAL_SMARTCARD_STATE_READY;
2440 
2441   /* Process Unlocked */
2442   __HAL_UNLOCK(hsmartcard);
2443 
2444   return HAL_OK;
2445 }
2446 
2447 /**
2448   * @brief  Handle SMARTCARD Communication Timeout. It waits
2449   *         until a flag is no longer in the specified status.
2450   * @param  hsmartcard Pointer to a SMARTCARD_HandleTypeDef structure that contains
2451   *                   the configuration information for the specified SMARTCARD module.
2452   * @param  Flag Specifies the SMARTCARD flag to check.
2453   * @param  Status The actual Flag status (SET or RESET).
2454   * @param  Tickstart Tick start value
2455   * @param  Timeout Timeout duration.
2456   * @retval HAL status
2457   */
SMARTCARD_WaitOnFlagUntilTimeout(SMARTCARD_HandleTypeDef * hsmartcard,uint32_t Flag,FlagStatus Status,uint32_t Tickstart,uint32_t Timeout)2458 static HAL_StatusTypeDef SMARTCARD_WaitOnFlagUntilTimeout(SMARTCARD_HandleTypeDef *hsmartcard, uint32_t Flag,
2459                                                           FlagStatus Status, uint32_t Tickstart, uint32_t Timeout)
2460 {
2461   /* Wait until flag is set */
2462   while ((__HAL_SMARTCARD_GET_FLAG(hsmartcard, Flag) ? SET : RESET) == Status)
2463   {
2464     /* Check for the Timeout */
2465     if (Timeout != HAL_MAX_DELAY)
2466     {
2467       if (((HAL_GetTick() - Tickstart) > Timeout) || (Timeout == 0U))
2468       {
2469         /* Disable TXE, RXNE, PE and ERR (Frame error, noise error, overrun error)
2470            interrupts for the interrupt process */
2471         CLEAR_BIT(hsmartcard->Instance->CR1, (USART_CR1_RXNEIE | USART_CR1_PEIE | USART_CR1_TXEIE));
2472         CLEAR_BIT(hsmartcard->Instance->CR3, USART_CR3_EIE);
2473 
2474         hsmartcard->gState  = HAL_SMARTCARD_STATE_READY;
2475         hsmartcard->RxState = HAL_SMARTCARD_STATE_READY;
2476 
2477         /* Process Unlocked */
2478         __HAL_UNLOCK(hsmartcard);
2479         return HAL_TIMEOUT;
2480       }
2481     }
2482   }
2483   return HAL_OK;
2484 }
2485 
2486 
2487 /**
2488   * @brief  End ongoing Tx transfer on SMARTCARD peripheral (following error detection or Transmit completion).
2489   * @param  hsmartcard Pointer to a SMARTCARD_HandleTypeDef structure that contains
2490   *                    the configuration information for the specified SMARTCARD module.
2491   * @retval None
2492   */
SMARTCARD_EndTxTransfer(SMARTCARD_HandleTypeDef * hsmartcard)2493 static void SMARTCARD_EndTxTransfer(SMARTCARD_HandleTypeDef *hsmartcard)
2494 {
2495   /* Disable TXEIE, TCIE and ERR (Frame error, noise error, overrun error) interrupts */
2496   CLEAR_BIT(hsmartcard->Instance->CR1, (USART_CR1_TXEIE | USART_CR1_TCIE));
2497   CLEAR_BIT(hsmartcard->Instance->CR3, USART_CR3_EIE);
2498 
2499   /* At end of Tx process, restore hsmartcard->gState to Ready */
2500   hsmartcard->gState = HAL_SMARTCARD_STATE_READY;
2501 }
2502 
2503 
2504 /**
2505   * @brief  End ongoing Rx transfer on UART peripheral (following error detection or Reception completion).
2506   * @param  hsmartcard Pointer to a SMARTCARD_HandleTypeDef structure that contains
2507   *                    the configuration information for the specified SMARTCARD module.
2508   * @retval None
2509   */
SMARTCARD_EndRxTransfer(SMARTCARD_HandleTypeDef * hsmartcard)2510 static void SMARTCARD_EndRxTransfer(SMARTCARD_HandleTypeDef *hsmartcard)
2511 {
2512   /* Disable RXNE, PE and ERR (Frame error, noise error, overrun error) interrupts */
2513   CLEAR_BIT(hsmartcard->Instance->CR1, (USART_CR1_RXNEIE | USART_CR1_PEIE));
2514   CLEAR_BIT(hsmartcard->Instance->CR3, USART_CR3_EIE);
2515 
2516   /* At end of Rx process, restore hsmartcard->RxState to Ready */
2517   hsmartcard->RxState = HAL_SMARTCARD_STATE_READY;
2518 }
2519 
2520 
2521 /**
2522   * @brief  DMA SMARTCARD transmit process complete callback.
2523   * @param  hdma Pointer to a DMA_HandleTypeDef structure that contains
2524   *              the configuration information for the specified DMA module.
2525   * @retval None
2526   */
SMARTCARD_DMATransmitCplt(DMA_HandleTypeDef * hdma)2527 static void SMARTCARD_DMATransmitCplt(DMA_HandleTypeDef *hdma)
2528 {
2529   SMARTCARD_HandleTypeDef *hsmartcard = (SMARTCARD_HandleTypeDef *)(hdma->Parent);
2530   hsmartcard->TxXferCount = 0U;
2531 
2532   /* Disable the DMA transfer for transmit request by resetting the DMAT bit
2533   in the SMARTCARD associated USART CR3 register */
2534   CLEAR_BIT(hsmartcard->Instance->CR3, USART_CR3_DMAT);
2535 
2536   /* Enable the SMARTCARD Transmit Complete Interrupt */
2537   __HAL_SMARTCARD_ENABLE_IT(hsmartcard, hsmartcard->AdvancedInit.TxCompletionIndication);
2538 }
2539 
2540 /**
2541   * @brief  DMA SMARTCARD receive process complete callback.
2542   * @param  hdma Pointer to a DMA_HandleTypeDef structure that contains
2543   *              the configuration information for the specified DMA module.
2544   * @retval None
2545   */
SMARTCARD_DMAReceiveCplt(DMA_HandleTypeDef * hdma)2546 static void SMARTCARD_DMAReceiveCplt(DMA_HandleTypeDef *hdma)
2547 {
2548   SMARTCARD_HandleTypeDef *hsmartcard = (SMARTCARD_HandleTypeDef *)(hdma->Parent);
2549   hsmartcard->RxXferCount = 0U;
2550 
2551   /* Disable PE and ERR (Frame error, noise error, overrun error) interrupts */
2552   CLEAR_BIT(hsmartcard->Instance->CR1, USART_CR1_PEIE);
2553   CLEAR_BIT(hsmartcard->Instance->CR3, USART_CR3_EIE);
2554 
2555   /* Disable the DMA transfer for the receiver request by resetting the DMAR bit
2556      in the SMARTCARD associated USART CR3 register */
2557   CLEAR_BIT(hsmartcard->Instance->CR3, USART_CR3_DMAR);
2558 
2559   /* At end of Rx process, restore hsmartcard->RxState to Ready */
2560   hsmartcard->RxState = HAL_SMARTCARD_STATE_READY;
2561 
2562 #if (USE_HAL_SMARTCARD_REGISTER_CALLBACKS == 1)
2563   /* Call registered Rx complete callback */
2564   hsmartcard->RxCpltCallback(hsmartcard);
2565 #else
2566   /* Call legacy weak Rx complete callback */
2567   HAL_SMARTCARD_RxCpltCallback(hsmartcard);
2568 #endif /* USE_HAL_SMARTCARD_REGISTER_CALLBACK */
2569 }
2570 
2571 /**
2572   * @brief  DMA SMARTCARD communication error callback.
2573   * @param  hdma Pointer to a DMA_HandleTypeDef structure that contains
2574   *              the configuration information for the specified DMA module.
2575   * @retval None
2576   */
SMARTCARD_DMAError(DMA_HandleTypeDef * hdma)2577 static void SMARTCARD_DMAError(DMA_HandleTypeDef *hdma)
2578 {
2579   SMARTCARD_HandleTypeDef *hsmartcard = (SMARTCARD_HandleTypeDef *)(hdma->Parent);
2580 
2581   /* Stop SMARTCARD DMA Tx request if ongoing */
2582   if (hsmartcard->gState == HAL_SMARTCARD_STATE_BUSY_TX)
2583   {
2584     if (HAL_IS_BIT_SET(hsmartcard->Instance->CR3, USART_CR3_DMAT))
2585     {
2586       hsmartcard->TxXferCount = 0U;
2587       SMARTCARD_EndTxTransfer(hsmartcard);
2588     }
2589   }
2590 
2591   /* Stop SMARTCARD DMA Rx request if ongoing */
2592   if (hsmartcard->RxState == HAL_SMARTCARD_STATE_BUSY_RX)
2593   {
2594     if (HAL_IS_BIT_SET(hsmartcard->Instance->CR3, USART_CR3_DMAR))
2595     {
2596       hsmartcard->RxXferCount = 0U;
2597       SMARTCARD_EndRxTransfer(hsmartcard);
2598     }
2599   }
2600 
2601   hsmartcard->ErrorCode |= HAL_SMARTCARD_ERROR_DMA;
2602 #if (USE_HAL_SMARTCARD_REGISTER_CALLBACKS == 1)
2603   /* Call registered user error callback */
2604   hsmartcard->ErrorCallback(hsmartcard);
2605 #else
2606   /* Call legacy weak user error callback */
2607   HAL_SMARTCARD_ErrorCallback(hsmartcard);
2608 #endif /* USE_HAL_SMARTCARD_REGISTER_CALLBACK */
2609 }
2610 
2611 /**
2612   * @brief  DMA SMARTCARD communication abort callback, when initiated by HAL services on Error
2613   *         (To be called at end of DMA Abort procedure following error occurrence).
2614   * @param  hdma DMA handle.
2615   * @retval None
2616   */
SMARTCARD_DMAAbortOnError(DMA_HandleTypeDef * hdma)2617 static void SMARTCARD_DMAAbortOnError(DMA_HandleTypeDef *hdma)
2618 {
2619   SMARTCARD_HandleTypeDef *hsmartcard = (SMARTCARD_HandleTypeDef *)(hdma->Parent);
2620   hsmartcard->RxXferCount = 0U;
2621   hsmartcard->TxXferCount = 0U;
2622 
2623 #if (USE_HAL_SMARTCARD_REGISTER_CALLBACKS == 1)
2624   /* Call registered user error callback */
2625   hsmartcard->ErrorCallback(hsmartcard);
2626 #else
2627   /* Call legacy weak user error callback */
2628   HAL_SMARTCARD_ErrorCallback(hsmartcard);
2629 #endif /* USE_HAL_SMARTCARD_REGISTER_CALLBACK */
2630 }
2631 
2632 /**
2633   * @brief  DMA SMARTCARD Tx communication abort callback, when initiated by user
2634   *         (To be called at end of DMA Tx Abort procedure following user abort request).
2635   * @note   When this callback is executed, User Abort complete call back is called only if no
2636   *         Abort still ongoing for Rx DMA Handle.
2637   * @param  hdma DMA handle.
2638   * @retval None
2639   */
SMARTCARD_DMATxAbortCallback(DMA_HandleTypeDef * hdma)2640 static void SMARTCARD_DMATxAbortCallback(DMA_HandleTypeDef *hdma)
2641 {
2642   SMARTCARD_HandleTypeDef *hsmartcard = (SMARTCARD_HandleTypeDef *)(hdma->Parent);
2643 
2644   hsmartcard->hdmatx->XferAbortCallback = NULL;
2645 
2646   /* Check if an Abort process is still ongoing */
2647   if (hsmartcard->hdmarx != NULL)
2648   {
2649     if (hsmartcard->hdmarx->XferAbortCallback != NULL)
2650     {
2651       return;
2652     }
2653   }
2654 
2655   /* No Abort process still ongoing : All DMA channels are aborted, call user Abort Complete callback */
2656   hsmartcard->TxXferCount = 0U;
2657   hsmartcard->RxXferCount = 0U;
2658 
2659   /* Reset errorCode */
2660   hsmartcard->ErrorCode = HAL_SMARTCARD_ERROR_NONE;
2661 
2662   /* Clear the Error flags in the ICR register */
2663   __HAL_SMARTCARD_CLEAR_FLAG(hsmartcard,
2664                              SMARTCARD_CLEAR_OREF | SMARTCARD_CLEAR_NEF | SMARTCARD_CLEAR_PEF | SMARTCARD_CLEAR_FEF |
2665                              SMARTCARD_CLEAR_RTOF | SMARTCARD_CLEAR_EOBF);
2666 
2667   /* Restore hsmartcard->gState and hsmartcard->RxState to Ready */
2668   hsmartcard->gState  = HAL_SMARTCARD_STATE_READY;
2669   hsmartcard->RxState = HAL_SMARTCARD_STATE_READY;
2670 
2671 #if (USE_HAL_SMARTCARD_REGISTER_CALLBACKS == 1)
2672   /* Call registered Abort complete callback */
2673   hsmartcard->AbortCpltCallback(hsmartcard);
2674 #else
2675   /* Call legacy weak Abort complete callback */
2676   HAL_SMARTCARD_AbortCpltCallback(hsmartcard);
2677 #endif /* USE_HAL_SMARTCARD_REGISTER_CALLBACK */
2678 }
2679 
2680 
2681 /**
2682   * @brief  DMA SMARTCARD Rx communication abort callback, when initiated by user
2683   *         (To be called at end of DMA Rx Abort procedure following user abort request).
2684   * @note   When this callback is executed, User Abort complete call back is called only if no
2685   *         Abort still ongoing for Tx DMA Handle.
2686   * @param  hdma DMA handle.
2687   * @retval None
2688   */
SMARTCARD_DMARxAbortCallback(DMA_HandleTypeDef * hdma)2689 static void SMARTCARD_DMARxAbortCallback(DMA_HandleTypeDef *hdma)
2690 {
2691   SMARTCARD_HandleTypeDef *hsmartcard = (SMARTCARD_HandleTypeDef *)(hdma->Parent);
2692 
2693   hsmartcard->hdmarx->XferAbortCallback = NULL;
2694 
2695   /* Check if an Abort process is still ongoing */
2696   if (hsmartcard->hdmatx != NULL)
2697   {
2698     if (hsmartcard->hdmatx->XferAbortCallback != NULL)
2699     {
2700       return;
2701     }
2702   }
2703 
2704   /* No Abort process still ongoing : All DMA channels are aborted, call user Abort Complete callback */
2705   hsmartcard->TxXferCount = 0U;
2706   hsmartcard->RxXferCount = 0U;
2707 
2708   /* Reset errorCode */
2709   hsmartcard->ErrorCode = HAL_SMARTCARD_ERROR_NONE;
2710 
2711   /* Clear the Error flags in the ICR register */
2712   __HAL_SMARTCARD_CLEAR_FLAG(hsmartcard,
2713                              SMARTCARD_CLEAR_OREF | SMARTCARD_CLEAR_NEF | SMARTCARD_CLEAR_PEF | SMARTCARD_CLEAR_FEF |
2714                              SMARTCARD_CLEAR_RTOF | SMARTCARD_CLEAR_EOBF);
2715 
2716   /* Restore hsmartcard->gState and hsmartcard->RxState to Ready */
2717   hsmartcard->gState  = HAL_SMARTCARD_STATE_READY;
2718   hsmartcard->RxState = HAL_SMARTCARD_STATE_READY;
2719 
2720 #if (USE_HAL_SMARTCARD_REGISTER_CALLBACKS == 1)
2721   /* Call registered Abort complete callback */
2722   hsmartcard->AbortCpltCallback(hsmartcard);
2723 #else
2724   /* Call legacy weak Abort complete callback */
2725   HAL_SMARTCARD_AbortCpltCallback(hsmartcard);
2726 #endif /* USE_HAL_SMARTCARD_REGISTER_CALLBACK */
2727 }
2728 
2729 
2730 /**
2731   * @brief  DMA SMARTCARD Tx communication abort callback, when initiated by user by a call to
2732   *         HAL_SMARTCARD_AbortTransmit_IT API (Abort only Tx transfer)
2733   *         (This callback is executed at end of DMA Tx Abort procedure following user abort request,
2734   *         and leads to user Tx Abort Complete callback execution).
2735   * @param  hdma DMA handle.
2736   * @retval None
2737   */
SMARTCARD_DMATxOnlyAbortCallback(DMA_HandleTypeDef * hdma)2738 static void SMARTCARD_DMATxOnlyAbortCallback(DMA_HandleTypeDef *hdma)
2739 {
2740   SMARTCARD_HandleTypeDef *hsmartcard = (SMARTCARD_HandleTypeDef *)(hdma->Parent);
2741 
2742   hsmartcard->TxXferCount = 0U;
2743 
2744   /* Clear the Error flags in the ICR register */
2745   __HAL_SMARTCARD_CLEAR_FLAG(hsmartcard, SMARTCARD_CLEAR_FEF);
2746 
2747   /* Restore hsmartcard->gState to Ready */
2748   hsmartcard->gState = HAL_SMARTCARD_STATE_READY;
2749 
2750 #if (USE_HAL_SMARTCARD_REGISTER_CALLBACKS == 1)
2751   /* Call registered Abort Transmit Complete Callback */
2752   hsmartcard->AbortTransmitCpltCallback(hsmartcard);
2753 #else
2754   /* Call legacy weak Abort Transmit Complete Callback */
2755   HAL_SMARTCARD_AbortTransmitCpltCallback(hsmartcard);
2756 #endif /* USE_HAL_SMARTCARD_REGISTER_CALLBACK */
2757 }
2758 
2759 /**
2760   * @brief  DMA SMARTCARD Rx communication abort callback, when initiated by user by a call to
2761   *         HAL_SMARTCARD_AbortReceive_IT API (Abort only Rx transfer)
2762   *         (This callback is executed at end of DMA Rx Abort procedure following user abort request,
2763   *         and leads to user Rx Abort Complete callback execution).
2764   * @param  hdma DMA handle.
2765   * @retval None
2766   */
SMARTCARD_DMARxOnlyAbortCallback(DMA_HandleTypeDef * hdma)2767 static void SMARTCARD_DMARxOnlyAbortCallback(DMA_HandleTypeDef *hdma)
2768 {
2769   SMARTCARD_HandleTypeDef *hsmartcard = (SMARTCARD_HandleTypeDef *)(hdma->Parent);
2770 
2771   hsmartcard->RxXferCount = 0U;
2772 
2773   /* Clear the Error flags in the ICR register */
2774   __HAL_SMARTCARD_CLEAR_FLAG(hsmartcard,
2775                              SMARTCARD_CLEAR_OREF | SMARTCARD_CLEAR_NEF | SMARTCARD_CLEAR_PEF | SMARTCARD_CLEAR_FEF |
2776                              SMARTCARD_CLEAR_RTOF | SMARTCARD_CLEAR_EOBF);
2777 
2778   /* Restore hsmartcard->RxState to Ready */
2779   hsmartcard->RxState = HAL_SMARTCARD_STATE_READY;
2780 
2781 #if (USE_HAL_SMARTCARD_REGISTER_CALLBACKS == 1)
2782   /* Call registered Abort Receive Complete Callback */
2783   hsmartcard->AbortReceiveCpltCallback(hsmartcard);
2784 #else
2785   /* Call legacy weak Abort Receive Complete Callback */
2786   HAL_SMARTCARD_AbortReceiveCpltCallback(hsmartcard);
2787 #endif /* USE_HAL_SMARTCARD_REGISTER_CALLBACK */
2788 }
2789 
2790 /**
2791   * @brief  Send an amount of data in non-blocking mode.
2792   * @note   Function called under interruption only, once
2793   *         interruptions have been enabled by HAL_SMARTCARD_Transmit_IT().
2794   * @param  hsmartcard Pointer to a SMARTCARD_HandleTypeDef structure that contains
2795   *                    the configuration information for the specified SMARTCARD module.
2796   * @retval None
2797   */
SMARTCARD_TxISR(SMARTCARD_HandleTypeDef * hsmartcard)2798 static void SMARTCARD_TxISR(SMARTCARD_HandleTypeDef *hsmartcard)
2799 {
2800   /* Check that a Tx process is ongoing */
2801   if (hsmartcard->gState == HAL_SMARTCARD_STATE_BUSY_TX)
2802   {
2803     if (hsmartcard->TxXferCount == 0U)
2804     {
2805       /* Disable the SMARTCARD Transmit Data Register Empty Interrupt */
2806       CLEAR_BIT(hsmartcard->Instance->CR1, USART_CR1_TXEIE);
2807 
2808       /* Enable the SMARTCARD Transmit Complete Interrupt */
2809       __HAL_SMARTCARD_ENABLE_IT(hsmartcard, hsmartcard->AdvancedInit.TxCompletionIndication);
2810     }
2811     else
2812     {
2813       hsmartcard->Instance->TDR = (uint8_t)(*hsmartcard->pTxBuffPtr & 0xFFU);
2814       hsmartcard->pTxBuffPtr++;
2815       hsmartcard->TxXferCount--;
2816     }
2817   }
2818 }
2819 
2820 /**
2821   * @brief  Wrap up transmission in non-blocking mode.
2822   * @param  hsmartcard Pointer to a SMARTCARD_HandleTypeDef structure that contains
2823   *                    the configuration information for the specified SMARTCARD module.
2824   * @retval None
2825   */
SMARTCARD_EndTransmit_IT(SMARTCARD_HandleTypeDef * hsmartcard)2826 static void SMARTCARD_EndTransmit_IT(SMARTCARD_HandleTypeDef *hsmartcard)
2827 {
2828   /* Disable the SMARTCARD Transmit Complete Interrupt */
2829   __HAL_SMARTCARD_DISABLE_IT(hsmartcard, hsmartcard->AdvancedInit.TxCompletionIndication);
2830 
2831   /* Check if a receive process is ongoing or not. If not disable ERR IT */
2832   if (hsmartcard->RxState == HAL_SMARTCARD_STATE_READY)
2833   {
2834     /* Disable the SMARTCARD Error Interrupt: (Frame error) */
2835     CLEAR_BIT(hsmartcard->Instance->CR3, USART_CR3_EIE);
2836   }
2837 
2838   /* Disable the Peripheral first to update mode */
2839   CLEAR_BIT(hsmartcard->Instance->CR1, USART_CR1_UE);
2840   if ((hsmartcard->Init.Mode == SMARTCARD_MODE_TX)
2841       && (hsmartcard->Init.NACKEnable == SMARTCARD_NACK_ENABLE))
2842   {
2843     /* In case of TX only mode, if NACK is enabled, receiver block has been enabled
2844        for Transmit phase. Disable this receiver block. */
2845     CLEAR_BIT(hsmartcard->Instance->CR1, USART_CR1_RE);
2846   }
2847   if ((hsmartcard->Init.Mode == SMARTCARD_MODE_TX_RX)
2848       || (hsmartcard->Init.NACKEnable == SMARTCARD_NACK_ENABLE))
2849   {
2850     /* Perform a TX FIFO Flush at end of Tx phase, as all sent bytes are appearing in Rx Data register */
2851     __HAL_SMARTCARD_FLUSH_DRREGISTER(hsmartcard);
2852   }
2853   SET_BIT(hsmartcard->Instance->CR1, USART_CR1_UE);
2854 
2855   /* Tx process is ended, restore hsmartcard->gState to Ready */
2856   hsmartcard->gState = HAL_SMARTCARD_STATE_READY;
2857 
2858   /* Clear TxISR function pointer */
2859   hsmartcard->TxISR = NULL;
2860 
2861 #if (USE_HAL_SMARTCARD_REGISTER_CALLBACKS == 1)
2862   /* Call registered Tx complete callback */
2863   hsmartcard->TxCpltCallback(hsmartcard);
2864 #else
2865   /* Call legacy weak Tx complete callback */
2866   HAL_SMARTCARD_TxCpltCallback(hsmartcard);
2867 #endif /* USE_HAL_SMARTCARD_REGISTER_CALLBACK */
2868 }
2869 
2870 /**
2871   * @brief  Receive an amount of data in non-blocking mode.
2872   * @note   Function called under interruption only, once
2873   *         interruptions have been enabled by HAL_SMARTCARD_Receive_IT().
2874   * @param  hsmartcard Pointer to a SMARTCARD_HandleTypeDef structure that contains
2875   *                    the configuration information for the specified SMARTCARD module.
2876   * @retval None
2877   */
SMARTCARD_RxISR(SMARTCARD_HandleTypeDef * hsmartcard)2878 static void SMARTCARD_RxISR(SMARTCARD_HandleTypeDef *hsmartcard)
2879 {
2880   /* Check that a Rx process is ongoing */
2881   if (hsmartcard->RxState == HAL_SMARTCARD_STATE_BUSY_RX)
2882   {
2883     *hsmartcard->pRxBuffPtr = (uint8_t)(hsmartcard->Instance->RDR & (uint8_t)0xFF);
2884     hsmartcard->pRxBuffPtr++;
2885 
2886     hsmartcard->RxXferCount--;
2887     if (hsmartcard->RxXferCount == 0U)
2888     {
2889       CLEAR_BIT(hsmartcard->Instance->CR1, USART_CR1_RXNEIE);
2890 
2891       /* Check if a transmit process is ongoing or not. If not disable ERR IT */
2892       if (hsmartcard->gState == HAL_SMARTCARD_STATE_READY)
2893       {
2894         /* Disable the SMARTCARD Error Interrupt: (Frame error, noise error, overrun error) */
2895         CLEAR_BIT(hsmartcard->Instance->CR3, USART_CR3_EIE);
2896       }
2897 
2898       /* Disable the SMARTCARD Parity Error Interrupt */
2899       CLEAR_BIT(hsmartcard->Instance->CR1, USART_CR1_PEIE);
2900 
2901       hsmartcard->RxState = HAL_SMARTCARD_STATE_READY;
2902 
2903       /* Clear RxISR function pointer */
2904       hsmartcard->RxISR = NULL;
2905 
2906 #if (USE_HAL_SMARTCARD_REGISTER_CALLBACKS == 1)
2907       /* Call registered Rx complete callback */
2908       hsmartcard->RxCpltCallback(hsmartcard);
2909 #else
2910       /* Call legacy weak Rx complete callback */
2911       HAL_SMARTCARD_RxCpltCallback(hsmartcard);
2912 #endif /* USE_HAL_SMARTCARD_REGISTER_CALLBACK */
2913     }
2914   }
2915   else
2916   {
2917     /* Clear RXNE interrupt flag */
2918     __HAL_SMARTCARD_SEND_REQ(hsmartcard, SMARTCARD_RXDATA_FLUSH_REQUEST);
2919   }
2920 }
2921 
2922 /**
2923   * @}
2924   */
2925 
2926 #endif /* HAL_SMARTCARD_MODULE_ENABLED */
2927 /**
2928   * @}
2929   */
2930 
2931 /**
2932   * @}
2933   */
2934 
2935