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