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