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