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