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