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