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