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