1 /**
2   ******************************************************************************
3   * @file    stm32wb0x_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) 2024 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 "stm32wb0x_hal.h"
185 
186 /** @addtogroup STM32WB0x_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   HAL_StatusTypeDef ret = HAL_OK;
2363   static const uint16_t SMARTCARDPrescTable[12] = {1U, 2U, 4U, 6U, 8U, 10U, 12U, 16U, 32U, 64U, 128U, 256U};
2364 
2365   /* Check the parameters */
2366   assert_param(IS_SMARTCARD_INSTANCE(hsmartcard->Instance));
2367   assert_param(IS_SMARTCARD_BAUDRATE(hsmartcard->Init.BaudRate));
2368   assert_param(IS_SMARTCARD_WORD_LENGTH(hsmartcard->Init.WordLength));
2369   assert_param(IS_SMARTCARD_STOPBITS(hsmartcard->Init.StopBits));
2370   assert_param(IS_SMARTCARD_PARITY(hsmartcard->Init.Parity));
2371   assert_param(IS_SMARTCARD_MODE(hsmartcard->Init.Mode));
2372   assert_param(IS_SMARTCARD_POLARITY(hsmartcard->Init.CLKPolarity));
2373   assert_param(IS_SMARTCARD_PHASE(hsmartcard->Init.CLKPhase));
2374   assert_param(IS_SMARTCARD_LASTBIT(hsmartcard->Init.CLKLastBit));
2375   assert_param(IS_SMARTCARD_ONE_BIT_SAMPLE(hsmartcard->Init.OneBitSampling));
2376   assert_param(IS_SMARTCARD_NACK(hsmartcard->Init.NACKEnable));
2377   assert_param(IS_SMARTCARD_TIMEOUT(hsmartcard->Init.TimeOutEnable));
2378   assert_param(IS_SMARTCARD_AUTORETRY_COUNT(hsmartcard->Init.AutoRetryCount));
2379   assert_param(IS_SMARTCARD_CLOCKPRESCALER(hsmartcard->Init.ClockPrescaler));
2380 
2381   /*-------------------------- USART CR1 Configuration -----------------------*/
2382   /* In SmartCard mode, M and PCE are forced to 1 (8 bits + parity).
2383    * Oversampling is forced to 16 (OVER8 = 0).
2384    * Configure the Parity and Mode:
2385    *  set PS bit according to hsmartcard->Init.Parity value
2386    *  set TE and RE bits according to hsmartcard->Init.Mode value */
2387   tmpreg = (((uint32_t)hsmartcard->Init.Parity) | ((uint32_t)hsmartcard->Init.Mode) |
2388             ((uint32_t)hsmartcard->Init.WordLength));
2389   MODIFY_REG(hsmartcard->Instance->CR1, USART_CR1_FIELDS, tmpreg);
2390 
2391   /*-------------------------- USART CR2 Configuration -----------------------*/
2392   tmpreg = hsmartcard->Init.StopBits;
2393   /* Synchronous mode is activated by default */
2394   tmpreg |= (uint32_t) USART_CR2_CLKEN | hsmartcard->Init.CLKPolarity;
2395   tmpreg |= (uint32_t) hsmartcard->Init.CLKPhase | hsmartcard->Init.CLKLastBit;
2396   tmpreg |= (uint32_t) hsmartcard->Init.TimeOutEnable;
2397   MODIFY_REG(hsmartcard->Instance->CR2, USART_CR2_FIELDS, tmpreg);
2398 
2399   /*-------------------------- USART CR3 Configuration -----------------------*/
2400   /* Configure
2401    * - one-bit sampling method versus three samples' majority rule
2402    *   according to hsmartcard->Init.OneBitSampling
2403    * - NACK transmission in case of parity error according
2404    *   to hsmartcard->Init.NACKEnable
2405    * - autoretry counter according to hsmartcard->Init.AutoRetryCount */
2406 
2407   tmpreg = (uint32_t) hsmartcard->Init.OneBitSampling | hsmartcard->Init.NACKEnable;
2408   tmpreg |= ((uint32_t)hsmartcard->Init.AutoRetryCount << USART_CR3_SCARCNT_Pos);
2409   MODIFY_REG(hsmartcard->Instance->CR3, USART_CR3_FIELDS, tmpreg);
2410 
2411   /*--------------------- SMARTCARD clock PRESC Configuration ----------------*/
2412   /* Configure
2413   * - SMARTCARD Clock Prescaler: set PRESCALER according to hsmartcard->Init.ClockPrescaler value */
2414   MODIFY_REG(hsmartcard->Instance->PRESC, USART_PRESC_PRESCALER, hsmartcard->Init.ClockPrescaler);
2415 
2416   /*-------------------------- USART GTPR Configuration ----------------------*/
2417   tmpreg = (hsmartcard->Init.Prescaler | ((uint32_t)hsmartcard->Init.GuardTime << USART_GTPR_GT_Pos));
2418   MODIFY_REG(hsmartcard->Instance->GTPR, (uint16_t)(USART_GTPR_GT | USART_GTPR_PSC), (uint16_t)tmpreg);
2419 
2420   /*-------------------------- USART RTOR Configuration ----------------------*/
2421   tmpreg = ((uint32_t)hsmartcard->Init.BlockLength << USART_RTOR_BLEN_Pos);
2422   if (hsmartcard->Init.TimeOutEnable == SMARTCARD_TIMEOUT_ENABLE)
2423   {
2424     assert_param(IS_SMARTCARD_TIMEOUT_VALUE(hsmartcard->Init.TimeOutValue));
2425     tmpreg |= (uint32_t) hsmartcard->Init.TimeOutValue;
2426   }
2427   WRITE_REG(hsmartcard->Instance->RTOR, tmpreg);
2428 
2429   /*-------------------------- USART BRR Configuration -----------------------*/
2430   tmpreg = (uint32_t)(((SMARTCARD_PERIPHCLK / SMARTCARDPrescTable[hsmartcard->Init.ClockPrescaler]) +
2431                        (hsmartcard->Init.BaudRate / 2U)) / hsmartcard->Init.BaudRate);
2432 
2433   /* USARTDIV must be greater than or equal to 0d16 */
2434   if ((tmpreg >= USART_BRR_MIN) && (tmpreg <= USART_BRR_MAX))
2435   {
2436     hsmartcard->Instance->BRR = (uint16_t)tmpreg;
2437   }
2438   else
2439   {
2440     ret = HAL_ERROR;
2441   }
2442 
2443   /* Initialize the number of data to process during RX/TX ISR execution */
2444   hsmartcard->NbTxDataToProcess = 1U;
2445   hsmartcard->NbRxDataToProcess = 1U;
2446 
2447   /* Clear ISR function pointers */
2448   hsmartcard->RxISR   = NULL;
2449   hsmartcard->TxISR   = NULL;
2450 
2451   return ret;
2452 }
2453 
2454 
2455 /**
2456   * @brief Configure the SMARTCARD associated USART peripheral advanced features.
2457   * @param hsmartcard Pointer to a SMARTCARD_HandleTypeDef structure that contains
2458   *                   the configuration information for the specified SMARTCARD module.
2459   * @retval None
2460   */
SMARTCARD_AdvFeatureConfig(SMARTCARD_HandleTypeDef * hsmartcard)2461 static void SMARTCARD_AdvFeatureConfig(SMARTCARD_HandleTypeDef *hsmartcard)
2462 {
2463   /* Check whether the set of advanced features to configure is properly set */
2464   assert_param(IS_SMARTCARD_ADVFEATURE_INIT(hsmartcard->AdvancedInit.AdvFeatureInit));
2465 
2466   /* if required, configure TX pin active level inversion */
2467   if (HAL_IS_BIT_SET(hsmartcard->AdvancedInit.AdvFeatureInit, SMARTCARD_ADVFEATURE_TXINVERT_INIT))
2468   {
2469     assert_param(IS_SMARTCARD_ADVFEATURE_TXINV(hsmartcard->AdvancedInit.TxPinLevelInvert));
2470     MODIFY_REG(hsmartcard->Instance->CR2, USART_CR2_TXINV, hsmartcard->AdvancedInit.TxPinLevelInvert);
2471   }
2472 
2473   /* if required, configure RX pin active level inversion */
2474   if (HAL_IS_BIT_SET(hsmartcard->AdvancedInit.AdvFeatureInit, SMARTCARD_ADVFEATURE_RXINVERT_INIT))
2475   {
2476     assert_param(IS_SMARTCARD_ADVFEATURE_RXINV(hsmartcard->AdvancedInit.RxPinLevelInvert));
2477     MODIFY_REG(hsmartcard->Instance->CR2, USART_CR2_RXINV, hsmartcard->AdvancedInit.RxPinLevelInvert);
2478   }
2479 
2480   /* if required, configure data inversion */
2481   if (HAL_IS_BIT_SET(hsmartcard->AdvancedInit.AdvFeatureInit, SMARTCARD_ADVFEATURE_DATAINVERT_INIT))
2482   {
2483     assert_param(IS_SMARTCARD_ADVFEATURE_DATAINV(hsmartcard->AdvancedInit.DataInvert));
2484     MODIFY_REG(hsmartcard->Instance->CR2, USART_CR2_DATAINV, hsmartcard->AdvancedInit.DataInvert);
2485   }
2486 
2487   /* if required, configure RX/TX pins swap */
2488   if (HAL_IS_BIT_SET(hsmartcard->AdvancedInit.AdvFeatureInit, SMARTCARD_ADVFEATURE_SWAP_INIT))
2489   {
2490     assert_param(IS_SMARTCARD_ADVFEATURE_SWAP(hsmartcard->AdvancedInit.Swap));
2491     MODIFY_REG(hsmartcard->Instance->CR2, USART_CR2_SWAP, hsmartcard->AdvancedInit.Swap);
2492   }
2493 
2494   /* if required, configure RX overrun detection disabling */
2495   if (HAL_IS_BIT_SET(hsmartcard->AdvancedInit.AdvFeatureInit, SMARTCARD_ADVFEATURE_RXOVERRUNDISABLE_INIT))
2496   {
2497     assert_param(IS_SMARTCARD_OVERRUN(hsmartcard->AdvancedInit.OverrunDisable));
2498     MODIFY_REG(hsmartcard->Instance->CR3, USART_CR3_OVRDIS, hsmartcard->AdvancedInit.OverrunDisable);
2499   }
2500 
2501   /* if required, configure DMA disabling on reception error */
2502   if (HAL_IS_BIT_SET(hsmartcard->AdvancedInit.AdvFeatureInit, SMARTCARD_ADVFEATURE_DMADISABLEONERROR_INIT))
2503   {
2504     assert_param(IS_SMARTCARD_ADVFEATURE_DMAONRXERROR(hsmartcard->AdvancedInit.DMADisableonRxError));
2505     MODIFY_REG(hsmartcard->Instance->CR3, USART_CR3_DDRE, hsmartcard->AdvancedInit.DMADisableonRxError);
2506   }
2507 
2508   /* if required, configure MSB first on communication line */
2509   if (HAL_IS_BIT_SET(hsmartcard->AdvancedInit.AdvFeatureInit, SMARTCARD_ADVFEATURE_MSBFIRST_INIT))
2510   {
2511     assert_param(IS_SMARTCARD_ADVFEATURE_MSBFIRST(hsmartcard->AdvancedInit.MSBFirst));
2512     MODIFY_REG(hsmartcard->Instance->CR2, USART_CR2_MSBFIRST, hsmartcard->AdvancedInit.MSBFirst);
2513   }
2514 
2515 }
2516 
2517 /**
2518   * @brief Check the SMARTCARD Idle State.
2519   * @param hsmartcard Pointer to a SMARTCARD_HandleTypeDef structure that contains
2520   *                   the configuration information for the specified SMARTCARD module.
2521   * @retval HAL status
2522   */
SMARTCARD_CheckIdleState(SMARTCARD_HandleTypeDef * hsmartcard)2523 static HAL_StatusTypeDef SMARTCARD_CheckIdleState(SMARTCARD_HandleTypeDef *hsmartcard)
2524 {
2525   uint32_t tickstart;
2526 
2527   /* Initialize the SMARTCARD ErrorCode */
2528   hsmartcard->ErrorCode = HAL_SMARTCARD_ERROR_NONE;
2529 
2530   /* Init tickstart for timeout management */
2531   tickstart = HAL_GetTick();
2532 
2533   /* Check if the Transmitter is enabled */
2534   if ((hsmartcard->Instance->CR1 & USART_CR1_TE) == USART_CR1_TE)
2535   {
2536     /* Wait until TEACK flag is set */
2537     if (SMARTCARD_WaitOnFlagUntilTimeout(hsmartcard, USART_ISR_TEACK, RESET, tickstart,
2538                                          SMARTCARD_TEACK_REACK_TIMEOUT) != HAL_OK)
2539     {
2540       /* Timeout occurred */
2541       return HAL_TIMEOUT;
2542     }
2543   }
2544   /* Check if the Receiver is enabled */
2545   if ((hsmartcard->Instance->CR1 & USART_CR1_RE) == USART_CR1_RE)
2546   {
2547     /* Wait until REACK flag is set */
2548     if (SMARTCARD_WaitOnFlagUntilTimeout(hsmartcard, USART_ISR_REACK, RESET, tickstart,
2549                                          SMARTCARD_TEACK_REACK_TIMEOUT) != HAL_OK)
2550     {
2551       /* Timeout occurred */
2552       return HAL_TIMEOUT;
2553     }
2554   }
2555 
2556   /* Initialize the SMARTCARD states */
2557   hsmartcard->gState  = HAL_SMARTCARD_STATE_READY;
2558   hsmartcard->RxState = HAL_SMARTCARD_STATE_READY;
2559 
2560   /* Process Unlocked */
2561   __HAL_UNLOCK(hsmartcard);
2562 
2563   return HAL_OK;
2564 }
2565 
2566 /**
2567   * @brief  Handle SMARTCARD Communication Timeout. It waits
2568   *         until a flag is no longer in the specified status.
2569   * @param  hsmartcard Pointer to a SMARTCARD_HandleTypeDef structure that contains
2570   *                   the configuration information for the specified SMARTCARD module.
2571   * @param  Flag Specifies the SMARTCARD flag to check.
2572   * @param  Status The actual Flag status (SET or RESET).
2573   * @param  Tickstart Tick start value
2574   * @param  Timeout Timeout duration.
2575   * @retval HAL status
2576   */
SMARTCARD_WaitOnFlagUntilTimeout(SMARTCARD_HandleTypeDef * hsmartcard,uint32_t Flag,FlagStatus Status,uint32_t Tickstart,uint32_t Timeout)2577 static HAL_StatusTypeDef SMARTCARD_WaitOnFlagUntilTimeout(SMARTCARD_HandleTypeDef *hsmartcard, uint32_t Flag,
2578                                                           FlagStatus Status, uint32_t Tickstart, uint32_t Timeout)
2579 {
2580   /* Wait until flag is set */
2581   while ((__HAL_SMARTCARD_GET_FLAG(hsmartcard, Flag) ? SET : RESET) == Status)
2582   {
2583     /* Check for the Timeout */
2584     if (Timeout != HAL_MAX_DELAY)
2585     {
2586       if (((HAL_GetTick() - Tickstart) > Timeout) || (Timeout == 0U))
2587       {
2588         /* Disable TXE, RXNE, PE and ERR (Frame error, noise error, overrun error)
2589            interrupts for the interrupt process */
2590         CLEAR_BIT(hsmartcard->Instance->CR1, (USART_CR1_RXNEIE_RXFNEIE | USART_CR1_PEIE | USART_CR1_TXEIE_TXFNFIE));
2591         CLEAR_BIT(hsmartcard->Instance->CR3, USART_CR3_EIE);
2592 
2593         hsmartcard->gState  = HAL_SMARTCARD_STATE_READY;
2594         hsmartcard->RxState = HAL_SMARTCARD_STATE_READY;
2595 
2596         /* Process Unlocked */
2597         __HAL_UNLOCK(hsmartcard);
2598         return HAL_TIMEOUT;
2599       }
2600     }
2601   }
2602   return HAL_OK;
2603 }
2604 
2605 
2606 /**
2607   * @brief  End ongoing Tx transfer on SMARTCARD peripheral (following error detection or Transmit completion).
2608   * @param  hsmartcard Pointer to a SMARTCARD_HandleTypeDef structure that contains
2609   *                    the configuration information for the specified SMARTCARD module.
2610   * @retval None
2611   */
SMARTCARD_EndTxTransfer(SMARTCARD_HandleTypeDef * hsmartcard)2612 static void SMARTCARD_EndTxTransfer(SMARTCARD_HandleTypeDef *hsmartcard)
2613 {
2614   /* Disable TXEIE, TCIE and ERR (Frame error, noise error, overrun error) interrupts */
2615   CLEAR_BIT(hsmartcard->Instance->CR1, (USART_CR1_TXEIE_TXFNFIE | USART_CR1_TCIE));
2616   CLEAR_BIT(hsmartcard->Instance->CR3, USART_CR3_EIE);
2617 
2618   /* At end of Tx process, restore hsmartcard->gState to Ready */
2619   hsmartcard->gState = HAL_SMARTCARD_STATE_READY;
2620 }
2621 
2622 
2623 /**
2624   * @brief  End ongoing Rx transfer on UART peripheral (following error detection or Reception completion).
2625   * @param  hsmartcard Pointer to a SMARTCARD_HandleTypeDef structure that contains
2626   *                    the configuration information for the specified SMARTCARD module.
2627   * @retval None
2628   */
SMARTCARD_EndRxTransfer(SMARTCARD_HandleTypeDef * hsmartcard)2629 static void SMARTCARD_EndRxTransfer(SMARTCARD_HandleTypeDef *hsmartcard)
2630 {
2631   /* Disable RXNE, PE and ERR (Frame error, noise error, overrun error) interrupts */
2632   CLEAR_BIT(hsmartcard->Instance->CR1, (USART_CR1_RXNEIE_RXFNEIE | USART_CR1_PEIE));
2633   CLEAR_BIT(hsmartcard->Instance->CR3, USART_CR3_EIE);
2634 
2635   /* At end of Rx process, restore hsmartcard->RxState to Ready */
2636   hsmartcard->RxState = HAL_SMARTCARD_STATE_READY;
2637 }
2638 
2639 
2640 #if defined(HAL_DMA_MODULE_ENABLED)
2641 /**
2642   * @brief  DMA SMARTCARD transmit process complete callback.
2643   * @param  hdma Pointer to a DMA_HandleTypeDef structure that contains
2644   *              the configuration information for the specified DMA module.
2645   * @retval None
2646   */
SMARTCARD_DMATransmitCplt(DMA_HandleTypeDef * hdma)2647 static void SMARTCARD_DMATransmitCplt(DMA_HandleTypeDef *hdma)
2648 {
2649   SMARTCARD_HandleTypeDef *hsmartcard = (SMARTCARD_HandleTypeDef *)(hdma->Parent);
2650   hsmartcard->TxXferCount = 0U;
2651 
2652   /* Disable the DMA transfer for transmit request by resetting the DMAT bit
2653   in the SMARTCARD associated USART CR3 register */
2654   CLEAR_BIT(hsmartcard->Instance->CR3, USART_CR3_DMAT);
2655 
2656   /* Enable the SMARTCARD Transmit Complete Interrupt */
2657   __HAL_SMARTCARD_ENABLE_IT(hsmartcard, hsmartcard->AdvancedInit.TxCompletionIndication);
2658 }
2659 
2660 /**
2661   * @brief  DMA SMARTCARD receive process complete callback.
2662   * @param  hdma Pointer to a DMA_HandleTypeDef structure that contains
2663   *              the configuration information for the specified DMA module.
2664   * @retval None
2665   */
SMARTCARD_DMAReceiveCplt(DMA_HandleTypeDef * hdma)2666 static void SMARTCARD_DMAReceiveCplt(DMA_HandleTypeDef *hdma)
2667 {
2668   SMARTCARD_HandleTypeDef *hsmartcard = (SMARTCARD_HandleTypeDef *)(hdma->Parent);
2669   hsmartcard->RxXferCount = 0U;
2670 
2671   /* Disable PE and ERR (Frame error, noise error, overrun error) interrupts */
2672   CLEAR_BIT(hsmartcard->Instance->CR1, USART_CR1_PEIE);
2673   CLEAR_BIT(hsmartcard->Instance->CR3, USART_CR3_EIE);
2674 
2675   /* Disable the DMA transfer for the receiver request by resetting the DMAR bit
2676      in the SMARTCARD associated USART CR3 register */
2677   CLEAR_BIT(hsmartcard->Instance->CR3, USART_CR3_DMAR);
2678 
2679   /* At end of Rx process, restore hsmartcard->RxState to Ready */
2680   hsmartcard->RxState = HAL_SMARTCARD_STATE_READY;
2681 
2682 #if (USE_HAL_SMARTCARD_REGISTER_CALLBACKS == 1)
2683   /* Call registered Rx complete callback */
2684   hsmartcard->RxCpltCallback(hsmartcard);
2685 #else
2686   /* Call legacy weak Rx complete callback */
2687   HAL_SMARTCARD_RxCpltCallback(hsmartcard);
2688 #endif /* USE_HAL_SMARTCARD_REGISTER_CALLBACK */
2689 }
2690 
2691 /**
2692   * @brief  DMA SMARTCARD communication error callback.
2693   * @param  hdma Pointer to a DMA_HandleTypeDef structure that contains
2694   *              the configuration information for the specified DMA module.
2695   * @retval None
2696   */
SMARTCARD_DMAError(DMA_HandleTypeDef * hdma)2697 static void SMARTCARD_DMAError(DMA_HandleTypeDef *hdma)
2698 {
2699   SMARTCARD_HandleTypeDef *hsmartcard = (SMARTCARD_HandleTypeDef *)(hdma->Parent);
2700 
2701   /* Stop SMARTCARD DMA Tx request if ongoing */
2702   if (hsmartcard->gState == HAL_SMARTCARD_STATE_BUSY_TX)
2703   {
2704     if (HAL_IS_BIT_SET(hsmartcard->Instance->CR3, USART_CR3_DMAT))
2705     {
2706       hsmartcard->TxXferCount = 0U;
2707       SMARTCARD_EndTxTransfer(hsmartcard);
2708     }
2709   }
2710 
2711   /* Stop SMARTCARD DMA Rx request if ongoing */
2712   if (hsmartcard->RxState == HAL_SMARTCARD_STATE_BUSY_RX)
2713   {
2714     if (HAL_IS_BIT_SET(hsmartcard->Instance->CR3, USART_CR3_DMAR))
2715     {
2716       hsmartcard->RxXferCount = 0U;
2717       SMARTCARD_EndRxTransfer(hsmartcard);
2718     }
2719   }
2720 
2721   hsmartcard->ErrorCode |= HAL_SMARTCARD_ERROR_DMA;
2722 #if (USE_HAL_SMARTCARD_REGISTER_CALLBACKS == 1)
2723   /* Call registered user error callback */
2724   hsmartcard->ErrorCallback(hsmartcard);
2725 #else
2726   /* Call legacy weak user error callback */
2727   HAL_SMARTCARD_ErrorCallback(hsmartcard);
2728 #endif /* USE_HAL_SMARTCARD_REGISTER_CALLBACK */
2729 }
2730 
2731 /**
2732   * @brief  DMA SMARTCARD communication abort callback, when initiated by HAL services on Error
2733   *         (To be called at end of DMA Abort procedure following error occurrence).
2734   * @param  hdma DMA handle.
2735   * @retval None
2736   */
SMARTCARD_DMAAbortOnError(DMA_HandleTypeDef * hdma)2737 static void SMARTCARD_DMAAbortOnError(DMA_HandleTypeDef *hdma)
2738 {
2739   SMARTCARD_HandleTypeDef *hsmartcard = (SMARTCARD_HandleTypeDef *)(hdma->Parent);
2740   hsmartcard->RxXferCount = 0U;
2741   hsmartcard->TxXferCount = 0U;
2742 
2743 #if (USE_HAL_SMARTCARD_REGISTER_CALLBACKS == 1)
2744   /* Call registered user error callback */
2745   hsmartcard->ErrorCallback(hsmartcard);
2746 #else
2747   /* Call legacy weak user error callback */
2748   HAL_SMARTCARD_ErrorCallback(hsmartcard);
2749 #endif /* USE_HAL_SMARTCARD_REGISTER_CALLBACK */
2750 }
2751 
2752 /**
2753   * @brief  DMA SMARTCARD Tx communication abort callback, when initiated by user
2754   *         (To be called at end of DMA Tx Abort procedure following user abort request).
2755   * @note   When this callback is executed, User Abort complete call back is called only if no
2756   *         Abort still ongoing for Rx DMA Handle.
2757   * @param  hdma DMA handle.
2758   * @retval None
2759   */
SMARTCARD_DMATxAbortCallback(DMA_HandleTypeDef * hdma)2760 static void SMARTCARD_DMATxAbortCallback(DMA_HandleTypeDef *hdma)
2761 {
2762   SMARTCARD_HandleTypeDef *hsmartcard = (SMARTCARD_HandleTypeDef *)(hdma->Parent);
2763 
2764   hsmartcard->hdmatx->XferAbortCallback = NULL;
2765 
2766   /* Check if an Abort process is still ongoing */
2767   if (hsmartcard->hdmarx != NULL)
2768   {
2769     if (hsmartcard->hdmarx->XferAbortCallback != NULL)
2770     {
2771       return;
2772     }
2773   }
2774 
2775   /* No Abort process still ongoing : All DMA channels are aborted, call user Abort Complete callback */
2776   hsmartcard->TxXferCount = 0U;
2777   hsmartcard->RxXferCount = 0U;
2778 
2779   /* Reset errorCode */
2780   hsmartcard->ErrorCode = HAL_SMARTCARD_ERROR_NONE;
2781 
2782   /* Clear the Error flags in the ICR register */
2783   __HAL_SMARTCARD_CLEAR_FLAG(hsmartcard,
2784                              SMARTCARD_CLEAR_OREF | SMARTCARD_CLEAR_NEF | SMARTCARD_CLEAR_PEF | SMARTCARD_CLEAR_FEF |
2785                              SMARTCARD_CLEAR_RTOF | SMARTCARD_CLEAR_EOBF);
2786 
2787   /* Restore hsmartcard->gState and hsmartcard->RxState to Ready */
2788   hsmartcard->gState  = HAL_SMARTCARD_STATE_READY;
2789   hsmartcard->RxState = HAL_SMARTCARD_STATE_READY;
2790 
2791 #if (USE_HAL_SMARTCARD_REGISTER_CALLBACKS == 1)
2792   /* Call registered Abort complete callback */
2793   hsmartcard->AbortCpltCallback(hsmartcard);
2794 #else
2795   /* Call legacy weak Abort complete callback */
2796   HAL_SMARTCARD_AbortCpltCallback(hsmartcard);
2797 #endif /* USE_HAL_SMARTCARD_REGISTER_CALLBACK */
2798 }
2799 
2800 
2801 /**
2802   * @brief  DMA SMARTCARD Rx communication abort callback, when initiated by user
2803   *         (To be called at end of DMA Rx Abort procedure following user abort request).
2804   * @note   When this callback is executed, User Abort complete call back is called only if no
2805   *         Abort still ongoing for Tx DMA Handle.
2806   * @param  hdma DMA handle.
2807   * @retval None
2808   */
SMARTCARD_DMARxAbortCallback(DMA_HandleTypeDef * hdma)2809 static void SMARTCARD_DMARxAbortCallback(DMA_HandleTypeDef *hdma)
2810 {
2811   SMARTCARD_HandleTypeDef *hsmartcard = (SMARTCARD_HandleTypeDef *)(hdma->Parent);
2812 
2813   hsmartcard->hdmarx->XferAbortCallback = NULL;
2814 
2815   /* Check if an Abort process is still ongoing */
2816   if (hsmartcard->hdmatx != NULL)
2817   {
2818     if (hsmartcard->hdmatx->XferAbortCallback != NULL)
2819     {
2820       return;
2821     }
2822   }
2823 
2824   /* No Abort process still ongoing : All DMA channels are aborted, call user Abort Complete callback */
2825   hsmartcard->TxXferCount = 0U;
2826   hsmartcard->RxXferCount = 0U;
2827 
2828   /* Reset errorCode */
2829   hsmartcard->ErrorCode = HAL_SMARTCARD_ERROR_NONE;
2830 
2831   /* Clear the Error flags in the ICR register */
2832   __HAL_SMARTCARD_CLEAR_FLAG(hsmartcard,
2833                              SMARTCARD_CLEAR_OREF | SMARTCARD_CLEAR_NEF | SMARTCARD_CLEAR_PEF | SMARTCARD_CLEAR_FEF |
2834                              SMARTCARD_CLEAR_RTOF | SMARTCARD_CLEAR_EOBF);
2835 
2836   /* Restore hsmartcard->gState and hsmartcard->RxState to Ready */
2837   hsmartcard->gState  = HAL_SMARTCARD_STATE_READY;
2838   hsmartcard->RxState = HAL_SMARTCARD_STATE_READY;
2839 
2840 #if (USE_HAL_SMARTCARD_REGISTER_CALLBACKS == 1)
2841   /* Call registered Abort complete callback */
2842   hsmartcard->AbortCpltCallback(hsmartcard);
2843 #else
2844   /* Call legacy weak Abort complete callback */
2845   HAL_SMARTCARD_AbortCpltCallback(hsmartcard);
2846 #endif /* USE_HAL_SMARTCARD_REGISTER_CALLBACK */
2847 }
2848 
2849 
2850 /**
2851   * @brief  DMA SMARTCARD Tx communication abort callback, when initiated by user by a call to
2852   *         HAL_SMARTCARD_AbortTransmit_IT API (Abort only Tx transfer)
2853   *         (This callback is executed at end of DMA Tx Abort procedure following user abort request,
2854   *         and leads to user Tx Abort Complete callback execution).
2855   * @param  hdma DMA handle.
2856   * @retval None
2857   */
SMARTCARD_DMATxOnlyAbortCallback(DMA_HandleTypeDef * hdma)2858 static void SMARTCARD_DMATxOnlyAbortCallback(DMA_HandleTypeDef *hdma)
2859 {
2860   SMARTCARD_HandleTypeDef *hsmartcard = (SMARTCARD_HandleTypeDef *)(hdma->Parent);
2861 
2862   hsmartcard->TxXferCount = 0U;
2863 
2864   /* Clear the Error flags in the ICR register */
2865   __HAL_SMARTCARD_CLEAR_FLAG(hsmartcard, SMARTCARD_CLEAR_FEF);
2866 
2867   /* Restore hsmartcard->gState to Ready */
2868   hsmartcard->gState = HAL_SMARTCARD_STATE_READY;
2869 
2870 #if (USE_HAL_SMARTCARD_REGISTER_CALLBACKS == 1)
2871   /* Call registered Abort Transmit Complete Callback */
2872   hsmartcard->AbortTransmitCpltCallback(hsmartcard);
2873 #else
2874   /* Call legacy weak Abort Transmit Complete Callback */
2875   HAL_SMARTCARD_AbortTransmitCpltCallback(hsmartcard);
2876 #endif /* USE_HAL_SMARTCARD_REGISTER_CALLBACK */
2877 }
2878 
2879 /**
2880   * @brief  DMA SMARTCARD Rx communication abort callback, when initiated by user by a call to
2881   *         HAL_SMARTCARD_AbortReceive_IT API (Abort only Rx transfer)
2882   *         (This callback is executed at end of DMA Rx Abort procedure following user abort request,
2883   *         and leads to user Rx Abort Complete callback execution).
2884   * @param  hdma DMA handle.
2885   * @retval None
2886   */
SMARTCARD_DMARxOnlyAbortCallback(DMA_HandleTypeDef * hdma)2887 static void SMARTCARD_DMARxOnlyAbortCallback(DMA_HandleTypeDef *hdma)
2888 {
2889   SMARTCARD_HandleTypeDef *hsmartcard = (SMARTCARD_HandleTypeDef *)(hdma->Parent);
2890 
2891   hsmartcard->RxXferCount = 0U;
2892 
2893   /* Clear the Error flags in the ICR register */
2894   __HAL_SMARTCARD_CLEAR_FLAG(hsmartcard,
2895                              SMARTCARD_CLEAR_OREF | SMARTCARD_CLEAR_NEF | SMARTCARD_CLEAR_PEF | SMARTCARD_CLEAR_FEF |
2896                              SMARTCARD_CLEAR_RTOF | SMARTCARD_CLEAR_EOBF);
2897 
2898   /* Restore hsmartcard->RxState to Ready */
2899   hsmartcard->RxState = HAL_SMARTCARD_STATE_READY;
2900 
2901 #if (USE_HAL_SMARTCARD_REGISTER_CALLBACKS == 1)
2902   /* Call registered Abort Receive Complete Callback */
2903   hsmartcard->AbortReceiveCpltCallback(hsmartcard);
2904 #else
2905   /* Call legacy weak Abort Receive Complete Callback */
2906   HAL_SMARTCARD_AbortReceiveCpltCallback(hsmartcard);
2907 #endif /* USE_HAL_SMARTCARD_REGISTER_CALLBACK */
2908 }
2909 #endif /* HAL_DMA_MODULE_ENABLED */
2910 
2911 /**
2912   * @brief  Send an amount of data in non-blocking mode.
2913   * @note   Function called under interruption only, once
2914   *         interruptions have been enabled by HAL_SMARTCARD_Transmit_IT()
2915   *         and when the FIFO mode is disabled.
2916   * @param  hsmartcard Pointer to a SMARTCARD_HandleTypeDef structure that contains
2917   *                    the configuration information for the specified SMARTCARD module.
2918   * @retval None
2919   */
SMARTCARD_TxISR(SMARTCARD_HandleTypeDef * hsmartcard)2920 static void SMARTCARD_TxISR(SMARTCARD_HandleTypeDef *hsmartcard)
2921 {
2922   /* Check that a Tx process is ongoing */
2923   if (hsmartcard->gState == HAL_SMARTCARD_STATE_BUSY_TX)
2924   {
2925     if (hsmartcard->TxXferCount == 0U)
2926     {
2927       /* Disable the SMARTCARD Transmit Data Register Empty Interrupt */
2928       CLEAR_BIT(hsmartcard->Instance->CR1, USART_CR1_TXEIE_TXFNFIE);
2929 
2930       /* Enable the SMARTCARD Transmit Complete Interrupt */
2931       __HAL_SMARTCARD_ENABLE_IT(hsmartcard, hsmartcard->AdvancedInit.TxCompletionIndication);
2932     }
2933     else
2934     {
2935       hsmartcard->Instance->TDR = (uint8_t)(*hsmartcard->pTxBuffPtr & 0xFFU);
2936       hsmartcard->pTxBuffPtr++;
2937       hsmartcard->TxXferCount--;
2938     }
2939   }
2940 }
2941 
2942 /**
2943   * @brief  Send an amount of data in non-blocking mode.
2944   * @note   Function called under interruption only, once
2945   *         interruptions have been enabled by HAL_SMARTCARD_Transmit_IT()
2946   *         and when the FIFO mode is enabled.
2947   * @param  hsmartcard Pointer to a SMARTCARD_HandleTypeDef structure that contains
2948   *                    the configuration information for the specified SMARTCARD module.
2949   * @retval None
2950   */
SMARTCARD_TxISR_FIFOEN(SMARTCARD_HandleTypeDef * hsmartcard)2951 static void SMARTCARD_TxISR_FIFOEN(SMARTCARD_HandleTypeDef *hsmartcard)
2952 {
2953   uint16_t   nb_tx_data;
2954 
2955   /* Check that a Tx process is ongoing */
2956   if (hsmartcard->gState == HAL_SMARTCARD_STATE_BUSY_TX)
2957   {
2958     for (nb_tx_data = hsmartcard->NbTxDataToProcess ; nb_tx_data > 0U ; nb_tx_data--)
2959     {
2960       if (hsmartcard->TxXferCount == 0U)
2961       {
2962         /* Disable the SMARTCARD Transmit Data Register Empty Interrupt */
2963         CLEAR_BIT(hsmartcard->Instance->CR1, USART_CR1_TXEIE_TXFNFIE);
2964 
2965         /* Enable the SMARTCARD Transmit Complete Interrupt */
2966         __HAL_SMARTCARD_ENABLE_IT(hsmartcard, hsmartcard->AdvancedInit.TxCompletionIndication);
2967       }
2968       else if (READ_BIT(hsmartcard->Instance->ISR, USART_ISR_TXE_TXFNF) != 0U)
2969       {
2970         hsmartcard->Instance->TDR = (uint8_t)(*hsmartcard->pTxBuffPtr & 0xFFU);
2971         hsmartcard->pTxBuffPtr++;
2972         hsmartcard->TxXferCount--;
2973       }
2974       else
2975       {
2976         /* Nothing to do */
2977       }
2978     }
2979   }
2980 }
2981 
2982 /**
2983   * @brief  Wrap up transmission in non-blocking mode.
2984   * @param  hsmartcard Pointer to a SMARTCARD_HandleTypeDef structure that contains
2985   *                    the configuration information for the specified SMARTCARD module.
2986   * @retval None
2987   */
SMARTCARD_EndTransmit_IT(SMARTCARD_HandleTypeDef * hsmartcard)2988 static void SMARTCARD_EndTransmit_IT(SMARTCARD_HandleTypeDef *hsmartcard)
2989 {
2990   /* Disable the SMARTCARD Transmit Complete Interrupt */
2991   __HAL_SMARTCARD_DISABLE_IT(hsmartcard, hsmartcard->AdvancedInit.TxCompletionIndication);
2992 
2993   /* Check if a receive process is ongoing or not. If not disable ERR IT */
2994   if (hsmartcard->RxState == HAL_SMARTCARD_STATE_READY)
2995   {
2996     /* Disable the SMARTCARD Error Interrupt: (Frame error) */
2997     CLEAR_BIT(hsmartcard->Instance->CR3, USART_CR3_EIE);
2998   }
2999 
3000   /* Disable the Peripheral first to update mode */
3001   CLEAR_BIT(hsmartcard->Instance->CR1, USART_CR1_UE);
3002   if ((hsmartcard->Init.Mode == SMARTCARD_MODE_TX)
3003       && (hsmartcard->Init.NACKEnable == SMARTCARD_NACK_ENABLE))
3004   {
3005     /* In case of TX only mode, if NACK is enabled, receiver block has been enabled
3006        for Transmit phase. Disable this receiver block. */
3007     CLEAR_BIT(hsmartcard->Instance->CR1, USART_CR1_RE);
3008   }
3009   if ((hsmartcard->Init.Mode == SMARTCARD_MODE_TX_RX)
3010       || (hsmartcard->Init.NACKEnable == SMARTCARD_NACK_ENABLE))
3011   {
3012     /* Perform a TX FIFO Flush at end of Tx phase, as all sent bytes are appearing in Rx Data register */
3013     __HAL_SMARTCARD_FLUSH_DRREGISTER(hsmartcard);
3014   }
3015   SET_BIT(hsmartcard->Instance->CR1, USART_CR1_UE);
3016 
3017   /* Tx process is ended, restore hsmartcard->gState to Ready */
3018   hsmartcard->gState = HAL_SMARTCARD_STATE_READY;
3019 
3020   /* Clear TxISR function pointer */
3021   hsmartcard->TxISR = NULL;
3022 
3023 #if (USE_HAL_SMARTCARD_REGISTER_CALLBACKS == 1)
3024   /* Call registered Tx complete callback */
3025   hsmartcard->TxCpltCallback(hsmartcard);
3026 #else
3027   /* Call legacy weak Tx complete callback */
3028   HAL_SMARTCARD_TxCpltCallback(hsmartcard);
3029 #endif /* USE_HAL_SMARTCARD_REGISTER_CALLBACK */
3030 }
3031 
3032 /**
3033   * @brief  Receive an amount of data in non-blocking mode.
3034   * @note   Function called under interruption only, once
3035   *         interruptions have been enabled by HAL_SMARTCARD_Receive_IT()
3036   *         and when the FIFO mode is disabled.
3037   * @param  hsmartcard Pointer to a SMARTCARD_HandleTypeDef structure that contains
3038   *                    the configuration information for the specified SMARTCARD module.
3039   * @retval None
3040   */
SMARTCARD_RxISR(SMARTCARD_HandleTypeDef * hsmartcard)3041 static void SMARTCARD_RxISR(SMARTCARD_HandleTypeDef *hsmartcard)
3042 {
3043   /* Check that a Rx process is ongoing */
3044   if (hsmartcard->RxState == HAL_SMARTCARD_STATE_BUSY_RX)
3045   {
3046     *hsmartcard->pRxBuffPtr = (uint8_t)(hsmartcard->Instance->RDR & (uint8_t)0xFF);
3047     hsmartcard->pRxBuffPtr++;
3048 
3049     hsmartcard->RxXferCount--;
3050     if (hsmartcard->RxXferCount == 0U)
3051     {
3052       CLEAR_BIT(hsmartcard->Instance->CR1, USART_CR1_RXNEIE_RXFNEIE);
3053 
3054       /* Check if a transmit process is ongoing or not. If not disable ERR IT */
3055       if (hsmartcard->gState == HAL_SMARTCARD_STATE_READY)
3056       {
3057         /* Disable the SMARTCARD Error Interrupt: (Frame error, noise error, overrun error) */
3058         CLEAR_BIT(hsmartcard->Instance->CR3, USART_CR3_EIE);
3059       }
3060 
3061       /* Disable the SMARTCARD Parity Error Interrupt */
3062       CLEAR_BIT(hsmartcard->Instance->CR1, USART_CR1_PEIE);
3063 
3064       hsmartcard->RxState = HAL_SMARTCARD_STATE_READY;
3065 
3066       /* Clear RxISR function pointer */
3067       hsmartcard->RxISR = NULL;
3068 
3069 #if (USE_HAL_SMARTCARD_REGISTER_CALLBACKS == 1)
3070       /* Call registered Rx complete callback */
3071       hsmartcard->RxCpltCallback(hsmartcard);
3072 #else
3073       /* Call legacy weak Rx complete callback */
3074       HAL_SMARTCARD_RxCpltCallback(hsmartcard);
3075 #endif /* USE_HAL_SMARTCARD_REGISTER_CALLBACK */
3076     }
3077   }
3078   else
3079   {
3080     /* Clear RXNE interrupt flag */
3081     __HAL_SMARTCARD_SEND_REQ(hsmartcard, SMARTCARD_RXDATA_FLUSH_REQUEST);
3082   }
3083 }
3084 
3085 /**
3086   * @brief  Receive an amount of data in non-blocking mode.
3087   * @note   Function called under interruption only, once
3088   *         interruptions have been enabled by HAL_SMARTCARD_Receive_IT()
3089   *         and when the FIFO mode is enabled.
3090   * @param  hsmartcard Pointer to a SMARTCARD_HandleTypeDef structure that contains
3091   *                    the configuration information for the specified SMARTCARD module.
3092   * @retval None
3093   */
SMARTCARD_RxISR_FIFOEN(SMARTCARD_HandleTypeDef * hsmartcard)3094 static void SMARTCARD_RxISR_FIFOEN(SMARTCARD_HandleTypeDef *hsmartcard)
3095 {
3096   uint16_t   nb_rx_data;
3097   uint16_t rxdatacount;
3098 
3099   /* Check that a Rx process is ongoing */
3100   if (hsmartcard->RxState == HAL_SMARTCARD_STATE_BUSY_RX)
3101   {
3102     for (nb_rx_data = hsmartcard->NbRxDataToProcess ; nb_rx_data > 0U ; nb_rx_data--)
3103     {
3104       *hsmartcard->pRxBuffPtr = (uint8_t)(hsmartcard->Instance->RDR & (uint8_t)0xFF);
3105       hsmartcard->pRxBuffPtr++;
3106 
3107       hsmartcard->RxXferCount--;
3108       if (hsmartcard->RxXferCount == 0U)
3109       {
3110         CLEAR_BIT(hsmartcard->Instance->CR1, USART_CR1_RXNEIE_RXFNEIE);
3111 
3112         /* Check if a transmit process is ongoing or not. If not disable ERR IT */
3113         if (hsmartcard->gState == HAL_SMARTCARD_STATE_READY)
3114         {
3115           /* Disable the SMARTCARD Error Interrupt: (Frame error, noise error, overrun error) */
3116           CLEAR_BIT(hsmartcard->Instance->CR3, USART_CR3_EIE);
3117         }
3118 
3119         /* Disable the SMARTCARD Parity Error Interrupt */
3120         CLEAR_BIT(hsmartcard->Instance->CR1, USART_CR1_PEIE);
3121 
3122         hsmartcard->RxState = HAL_SMARTCARD_STATE_READY;
3123 
3124         /* Clear RxISR function pointer */
3125         hsmartcard->RxISR = NULL;
3126 
3127 #if (USE_HAL_SMARTCARD_REGISTER_CALLBACKS == 1)
3128         /* Call registered Rx complete callback */
3129         hsmartcard->RxCpltCallback(hsmartcard);
3130 #else
3131         /* Call legacy weak Rx complete callback */
3132         HAL_SMARTCARD_RxCpltCallback(hsmartcard);
3133 #endif /* USE_HAL_SMARTCARD_REGISTER_CALLBACK */
3134       }
3135     }
3136 
3137     /* When remaining number of bytes to receive is less than the RX FIFO
3138     threshold, next incoming frames are processed as if FIFO mode was
3139     disabled (i.e. one interrupt per received frame).
3140     */
3141     rxdatacount = hsmartcard->RxXferCount;
3142     if (((rxdatacount != 0U)) && (rxdatacount < hsmartcard->NbRxDataToProcess))
3143     {
3144       /* Disable the UART RXFT interrupt*/
3145       CLEAR_BIT(hsmartcard->Instance->CR3, USART_CR3_RXFTIE);
3146 
3147       /* Update the RxISR function pointer */
3148       hsmartcard->RxISR = SMARTCARD_RxISR;
3149 
3150       /* Enable the UART Data Register Not Empty interrupt */
3151       SET_BIT(hsmartcard->Instance->CR1, USART_CR1_RXNEIE_RXFNEIE);
3152     }
3153   }
3154   else
3155   {
3156     /* Clear RXNE interrupt flag */
3157     __HAL_SMARTCARD_SEND_REQ(hsmartcard, SMARTCARD_RXDATA_FLUSH_REQUEST);
3158   }
3159 }
3160 
3161 /**
3162   * @}
3163   */
3164 
3165 #endif /* HAL_SMARTCARD_MODULE_ENABLED */
3166 /**
3167   * @}
3168   */
3169 
3170 /**
3171   * @}
3172   */
3173