Set blackbox file handler to NULL after closing file
[inav.git] / lib / main / STM32H7 / Drivers / STM32H7xx_HAL_Driver / Src / stm32h7xx_hal_smartcard.c
blob91b6bcb48c0c138384f3150a2f0fb821b49864ab
1 /**
2 ******************************************************************************
3 * @file stm32h7xx_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
13 @verbatim
14 ==============================================================================
15 ##### How to use this driver #####
16 ==============================================================================
17 [..]
18 The SMARTCARD HAL driver can be used as follows:
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.
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.
44 (#) If required, program SMARTCARD advanced features (TX/RX pins swap, TimeOut, auto-retry counter,...)
45 in the hsmartcard handle AdvancedInit structure.
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.
55 [..]
56 [..] Three operation modes are available within this driver :
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()
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()
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()
88 *** SMARTCARD HAL driver macros list ***
89 ========================================
90 [..]
91 Below the list of most used macros in SMARTCARD HAL driver.
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
99 [..]
100 (@) You can refer to the SMARTCARD HAL driver header file for more useful macros
102 ##### Callback registration #####
103 ==================================
105 [..]
106 The compilation define USE_HAL_SMARTCARD_REGISTER_CALLBACKS when set to 1
107 allows the user to configure dynamically the driver callbacks.
109 [..]
110 Use Function @ref HAL_SMARTCARD_RegisterCallback() to register a user callback.
111 Function @ref 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.
125 [..]
126 Use function @ref HAL_SMARTCARD_UnRegisterCallback() to reset a callback to the default
127 weak (surcharged) function.
128 @ref 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.
142 [..]
143 By default, after the @ref 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 @ref HAL_SMARTCARD_TxCpltCallback(), @ref HAL_SMARTCARD_RxCpltCallback().
146 Exception done for MspInit and MspDeInit functions that are respectively
147 reset to the legacy weak (surcharged) functions in the @ref HAL_SMARTCARD_Init()
148 and @ref HAL_SMARTCARD_DeInit() only when these callbacks are null (not registered beforehand).
149 If not, MspInit or MspDeInit are not null, the @ref HAL_SMARTCARD_Init() and @ref HAL_SMARTCARD_DeInit()
150 keep and use the user MspInit/MspDeInit callbacks (registered beforehand).
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 @ref HAL_SMARTCARD_RegisterCallback() before calling @ref HAL_SMARTCARD_DeInit()
159 or @ref HAL_SMARTCARD_Init() function.
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.
167 @endverbatim
168 ******************************************************************************
169 * @attention
171 * <h2><center>&copy; Copyright (c) 2017 STMicroelectronics.
172 * All rights reserved.</center></h2>
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
179 ******************************************************************************
182 /* Includes ------------------------------------------------------------------*/
183 #include "stm32h7xx_hal.h"
185 /** @addtogroup STM32H7xx_HAL_Driver
186 * @{
189 /** @defgroup SMARTCARD SMARTCARD
190 * @brief HAL SMARTCARD module driver
191 * @{
194 #ifdef HAL_SMARTCARD_MODULE_ENABLED
196 /* Private typedef -----------------------------------------------------------*/
197 /* Private define ------------------------------------------------------------*/
198 /** @defgroup SMARTCARD_Private_Constants SMARTCARD Private Constants
199 * @{
201 #define SMARTCARD_TEACK_REACK_TIMEOUT 1000U /*!< SMARTCARD TX or RX enable acknowledge time-out value */
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 */
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 */
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 */
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 */
215 #define USART_BRR_MIN 0x10U /*!< USART BRR minimum authorized value */
217 #define USART_BRR_MAX 0x0000FFFFU /*!< USART BRR maximum authorized value */
219 * @}
222 /* Private macros ------------------------------------------------------------*/
223 /* Private variables ---------------------------------------------------------*/
224 /* Private function prototypes -----------------------------------------------*/
225 /** @addtogroup SMARTCARD_Private_Functions
226 * @{
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);
252 * @}
255 /* Exported functions --------------------------------------------------------*/
257 /** @defgroup SMARTCARD_Exported_Functions SMARTCARD Exported Functions
258 * @{
261 /** @defgroup SMARTCARD_Exported_Functions_Group1 Initialization and de-initialization functions
262 * @brief Initialization and Configuration functions
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
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).
294 @endverbatim
296 The USART frame format is given in the following table:
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 +---------------------------------------------------------------+
306 * @{
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
316 HAL_StatusTypeDef HAL_SMARTCARD_Init(SMARTCARD_HandleTypeDef *hsmartcard)
318 /* Check the SMARTCARD handle allocation */
319 if (hsmartcard == NULL)
321 return HAL_ERROR;
324 /* Check the USART associated to the SMARTCARD handle */
325 assert_param(IS_SMARTCARD_INSTANCE(hsmartcard->Instance));
327 if (hsmartcard->gState == HAL_SMARTCARD_STATE_RESET)
329 /* Allocate lock resource and initialize it */
330 hsmartcard->Lock = HAL_UNLOCKED;
332 #if USE_HAL_SMARTCARD_REGISTER_CALLBACKS == 1
333 SMARTCARD_InitCallbacksToDefault(hsmartcard);
335 if (hsmartcard->MspInitCallback == NULL)
337 hsmartcard->MspInitCallback = HAL_SMARTCARD_MspInit;
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 */
348 hsmartcard->gState = HAL_SMARTCARD_STATE_BUSY;
350 /* Disable the Peripheral to set smartcard mode */
351 CLEAR_BIT(hsmartcard->Instance->CR1, USART_CR1_UE);
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));
359 /* set the USART in SMARTCARD mode */
360 SET_BIT(hsmartcard->Instance->CR3, USART_CR3_SCEN);
362 /* Set the SMARTCARD Communication parameters */
363 if (SMARTCARD_SetConfig(hsmartcard) == HAL_ERROR)
365 return HAL_ERROR;
368 /* Set the SMARTCARD transmission completion indication */
369 SMARTCARD_TRANSMISSION_COMPLETION_SETTING(hsmartcard);
371 if (hsmartcard->AdvancedInit.AdvFeatureInit != SMARTCARD_ADVFEATURE_NO_INIT)
373 SMARTCARD_AdvFeatureConfig(hsmartcard);
376 /* Enable the Peripheral */
377 SET_BIT(hsmartcard->Instance->CR1, USART_CR1_UE);
379 /* TEACK and/or REACK to check before moving hsmartcard->gState and hsmartcard->RxState to Ready */
380 return (SMARTCARD_CheckIdleState(hsmartcard));
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
389 HAL_StatusTypeDef HAL_SMARTCARD_DeInit(SMARTCARD_HandleTypeDef *hsmartcard)
391 /* Check the SMARTCARD handle allocation */
392 if (hsmartcard == NULL)
394 return HAL_ERROR;
397 /* Check the USART/UART associated to the SMARTCARD handle */
398 assert_param(IS_SMARTCARD_INSTANCE(hsmartcard->Instance));
400 hsmartcard->gState = HAL_SMARTCARD_STATE_BUSY;
402 /* Disable the Peripheral */
403 CLEAR_BIT(hsmartcard->Instance->CR1, USART_CR1_UE);
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);
411 /* DeInit the low level hardware */
412 #if USE_HAL_SMARTCARD_REGISTER_CALLBACKS == 1
413 if (hsmartcard->MspDeInitCallback == NULL)
415 hsmartcard->MspDeInitCallback = HAL_SMARTCARD_MspDeInit;
417 /* DeInit the low level hardware */
418 hsmartcard->MspDeInitCallback(hsmartcard);
419 #else
420 HAL_SMARTCARD_MspDeInit(hsmartcard);
421 #endif /* USE_HAL_SMARTCARD_REGISTER_CALLBACKS */
423 hsmartcard->ErrorCode = HAL_SMARTCARD_ERROR_NONE;
424 hsmartcard->gState = HAL_SMARTCARD_STATE_RESET;
425 hsmartcard->RxState = HAL_SMARTCARD_STATE_RESET;
427 /* Process Unlock */
428 __HAL_UNLOCK(hsmartcard);
430 return HAL_OK;
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
439 __weak void HAL_SMARTCARD_MspInit(SMARTCARD_HandleTypeDef *hsmartcard)
441 /* Prevent unused argument(s) compilation warning */
442 UNUSED(hsmartcard);
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
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
455 __weak void HAL_SMARTCARD_MspDeInit(SMARTCARD_HandleTypeDef *hsmartcard)
457 /* Prevent unused argument(s) compilation warning */
458 UNUSED(hsmartcard);
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
465 #if (USE_HAL_SMARTCARD_REGISTER_CALLBACKS == 1)
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
485 HAL_StatusTypeDef HAL_SMARTCARD_RegisterCallback(SMARTCARD_HandleTypeDef *hsmartcard,
486 HAL_SMARTCARD_CallbackIDTypeDef CallbackID, pSMARTCARD_CallbackTypeDef pCallback)
488 HAL_StatusTypeDef status = HAL_OK;
490 if (pCallback == NULL)
492 /* Update the error code */
493 hsmartcard->ErrorCode |= HAL_SMARTCARD_ERROR_INVALID_CALLBACK;
495 return HAL_ERROR;
497 /* Process locked */
498 __HAL_LOCK(hsmartcard);
500 if (hsmartcard->gState == HAL_SMARTCARD_STATE_READY)
502 switch (CallbackID)
505 case HAL_SMARTCARD_TX_COMPLETE_CB_ID :
506 hsmartcard->TxCpltCallback = pCallback;
507 break;
509 case HAL_SMARTCARD_RX_COMPLETE_CB_ID :
510 hsmartcard->RxCpltCallback = pCallback;
511 break;
513 case HAL_SMARTCARD_ERROR_CB_ID :
514 hsmartcard->ErrorCallback = pCallback;
515 break;
517 case HAL_SMARTCARD_ABORT_COMPLETE_CB_ID :
518 hsmartcard->AbortCpltCallback = pCallback;
519 break;
521 case HAL_SMARTCARD_ABORT_TRANSMIT_COMPLETE_CB_ID :
522 hsmartcard->AbortTransmitCpltCallback = pCallback;
523 break;
525 case HAL_SMARTCARD_ABORT_RECEIVE_COMPLETE_CB_ID :
526 hsmartcard->AbortReceiveCpltCallback = pCallback;
527 break;
529 case HAL_SMARTCARD_RX_FIFO_FULL_CB_ID :
530 hsmartcard->RxFifoFullCallback = pCallback;
531 break;
533 case HAL_SMARTCARD_TX_FIFO_EMPTY_CB_ID :
534 hsmartcard->TxFifoEmptyCallback = pCallback;
535 break;
537 case HAL_SMARTCARD_MSPINIT_CB_ID :
538 hsmartcard->MspInitCallback = pCallback;
539 break;
541 case HAL_SMARTCARD_MSPDEINIT_CB_ID :
542 hsmartcard->MspDeInitCallback = pCallback;
543 break;
545 default :
546 /* Update the error code */
547 hsmartcard->ErrorCode |= HAL_SMARTCARD_ERROR_INVALID_CALLBACK;
549 /* Return error status */
550 status = HAL_ERROR;
551 break;
554 else if (hsmartcard->gState == HAL_SMARTCARD_STATE_RESET)
556 switch (CallbackID)
558 case HAL_SMARTCARD_MSPINIT_CB_ID :
559 hsmartcard->MspInitCallback = pCallback;
560 break;
562 case HAL_SMARTCARD_MSPDEINIT_CB_ID :
563 hsmartcard->MspDeInitCallback = pCallback;
564 break;
566 default :
567 /* Update the error code */
568 hsmartcard->ErrorCode |= HAL_SMARTCARD_ERROR_INVALID_CALLBACK;
570 /* Return error status */
571 status = HAL_ERROR;
572 break;
575 else
577 /* Update the error code */
578 hsmartcard->ErrorCode |= HAL_SMARTCARD_ERROR_INVALID_CALLBACK;
580 /* Return error status */
581 status = HAL_ERROR;
584 /* Release Lock */
585 __HAL_UNLOCK(hsmartcard);
587 return status;
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
608 HAL_StatusTypeDef HAL_SMARTCARD_UnRegisterCallback(SMARTCARD_HandleTypeDef *hsmartcard,
609 HAL_SMARTCARD_CallbackIDTypeDef CallbackID)
611 HAL_StatusTypeDef status = HAL_OK;
613 /* Process locked */
614 __HAL_LOCK(hsmartcard);
616 if (HAL_SMARTCARD_STATE_READY == hsmartcard->gState)
618 switch (CallbackID)
620 case HAL_SMARTCARD_TX_COMPLETE_CB_ID :
621 hsmartcard->TxCpltCallback = HAL_SMARTCARD_TxCpltCallback; /* Legacy weak TxCpltCallback */
622 break;
624 case HAL_SMARTCARD_RX_COMPLETE_CB_ID :
625 hsmartcard->RxCpltCallback = HAL_SMARTCARD_RxCpltCallback; /* Legacy weak RxCpltCallback */
626 break;
628 case HAL_SMARTCARD_ERROR_CB_ID :
629 hsmartcard->ErrorCallback = HAL_SMARTCARD_ErrorCallback; /* Legacy weak ErrorCallback */
630 break;
632 case HAL_SMARTCARD_ABORT_COMPLETE_CB_ID :
633 hsmartcard->AbortCpltCallback = HAL_SMARTCARD_AbortCpltCallback; /* Legacy weak AbortCpltCallback */
634 break;
636 case HAL_SMARTCARD_ABORT_TRANSMIT_COMPLETE_CB_ID :
637 hsmartcard->AbortTransmitCpltCallback = HAL_SMARTCARD_AbortTransmitCpltCallback; /* Legacy weak AbortTransmitCpltCallback */
638 break;
640 case HAL_SMARTCARD_ABORT_RECEIVE_COMPLETE_CB_ID :
641 hsmartcard->AbortReceiveCpltCallback = HAL_SMARTCARD_AbortReceiveCpltCallback; /* Legacy weak AbortReceiveCpltCallback */
642 break;
644 case HAL_SMARTCARD_RX_FIFO_FULL_CB_ID :
645 hsmartcard->RxFifoFullCallback = HAL_SMARTCARDEx_RxFifoFullCallback; /* Legacy weak RxFifoFullCallback */
646 break;
648 case HAL_SMARTCARD_TX_FIFO_EMPTY_CB_ID :
649 hsmartcard->TxFifoEmptyCallback = HAL_SMARTCARDEx_TxFifoEmptyCallback; /* Legacy weak TxFifoEmptyCallback */
650 break;
652 case HAL_SMARTCARD_MSPINIT_CB_ID :
653 hsmartcard->MspInitCallback = HAL_SMARTCARD_MspInit; /* Legacy weak MspInitCallback */
654 break;
656 case HAL_SMARTCARD_MSPDEINIT_CB_ID :
657 hsmartcard->MspDeInitCallback = HAL_SMARTCARD_MspDeInit; /* Legacy weak MspDeInitCallback */
658 break;
660 default :
661 /* Update the error code */
662 hsmartcard->ErrorCode |= HAL_SMARTCARD_ERROR_INVALID_CALLBACK;
664 /* Return error status */
665 status = HAL_ERROR;
666 break;
669 else if (HAL_SMARTCARD_STATE_RESET == hsmartcard->gState)
671 switch (CallbackID)
673 case HAL_SMARTCARD_MSPINIT_CB_ID :
674 hsmartcard->MspInitCallback = HAL_SMARTCARD_MspInit;
675 break;
677 case HAL_SMARTCARD_MSPDEINIT_CB_ID :
678 hsmartcard->MspDeInitCallback = HAL_SMARTCARD_MspDeInit;
679 break;
681 default :
682 /* Update the error code */
683 hsmartcard->ErrorCode |= HAL_SMARTCARD_ERROR_INVALID_CALLBACK;
685 /* Return error status */
686 status = HAL_ERROR;
687 break;
690 else
692 /* Update the error code */
693 hsmartcard->ErrorCode |= HAL_SMARTCARD_ERROR_INVALID_CALLBACK;
695 /* Return error status */
696 status = HAL_ERROR;
699 /* Release Lock */
700 __HAL_UNLOCK(hsmartcard);
702 return status;
704 #endif /* USE_HAL_SMARTCARD_REGISTER_CALLBACKS */
707 * @}
710 /** @defgroup SMARTCARD_Exported_Functions_Group2 IO operation functions
711 * @brief SMARTCARD Transmit and Receive functions
713 @verbatim
714 ==============================================================================
715 ##### IO operation functions #####
716 ==============================================================================
717 [..]
718 This subsection provides a set of functions allowing to manage the SMARTCARD data transfers.
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.
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.
742 (+) Blocking mode APIs are :
743 (++) HAL_SMARTCARD_Transmit()
744 (++) HAL_SMARTCARD_Receive()
746 (+) Non Blocking mode APIs with Interrupt are :
747 (++) HAL_SMARTCARD_Transmit_IT()
748 (++) HAL_SMARTCARD_Receive_IT()
749 (++) HAL_SMARTCARD_IRQHandler()
751 (+) Non Blocking mode functions with DMA are :
752 (++) HAL_SMARTCARD_Transmit_DMA()
753 (++) HAL_SMARTCARD_Receive_DMA()
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()
760 [..]
761 (#) Non-Blocking mode transfers could be aborted using Abort API's :
762 (++) HAL_SMARTCARD_Abort()
763 (++) HAL_SMARTCARD_AbortTransmit()
764 (++) HAL_SMARTCARD_AbortReceive()
765 (++) HAL_SMARTCARD_Abort_IT()
766 (++) HAL_SMARTCARD_AbortTransmit_IT()
767 (++) HAL_SMARTCARD_AbortReceive_IT()
769 (#) For Abort services based on interrupts (HAL_SMARTCARD_Abortxxx_IT), a set of Abort Complete Callbacks are provided:
770 (++) HAL_SMARTCARD_AbortCpltCallback()
771 (++) HAL_SMARTCARD_AbortTransmitCpltCallback()
772 (++) HAL_SMARTCARD_AbortReceiveCpltCallback()
774 (#) In Non-Blocking mode transfers, possible errors are split into 2 categories.
775 Errors are handled as follows :
776 (++) Error is considered as Recoverable and non blocking : Transfer could go till end, but error severity is
777 to be evaluated by user : this concerns Frame Error, Parity Error or Noise Error in Interrupt mode reception .
778 Received character is then retrieved and stored in Rx buffer, Error code is set to allow user to identify error type,
779 and HAL_SMARTCARD_ErrorCallback() user callback is executed. Transfer is kept ongoing on SMARTCARD side.
780 If user wants to abort it, Abort services should be called by user.
781 (++) Error is considered as Blocking : Transfer could not be completed properly and is aborted.
782 This concerns Frame Error in Interrupt mode tranmission, Overrun Error in Interrupt mode reception and all errors in DMA mode.
783 Error code is set to allow user to identify error type, and HAL_SMARTCARD_ErrorCallback() user callback is executed.
785 @endverbatim
786 * @{
790 * @brief Send an amount of data in blocking mode.
791 * @note When FIFO mode is enabled, writing a data in the TDR register adds one
792 * data to the TXFIFO. Write operations to the TDR register are performed
793 * when TXFNF flag is set. From hardware perspective, TXFNF flag and
794 * TXE are mapped on the same bit-field.
795 * @param hsmartcard Pointer to a SMARTCARD_HandleTypeDef structure that contains
796 * the configuration information for the specified SMARTCARD module.
797 * @param pData pointer to data buffer.
798 * @param Size amount of data to be sent.
799 * @param Timeout Timeout duration.
800 * @retval HAL status
802 HAL_StatusTypeDef HAL_SMARTCARD_Transmit(SMARTCARD_HandleTypeDef *hsmartcard, uint8_t *pData, uint16_t Size,
803 uint32_t Timeout)
805 uint32_t tickstart;
806 uint8_t *ptmpdata = pData;
808 /* Check that a Tx process is not already ongoing */
809 if (hsmartcard->gState == HAL_SMARTCARD_STATE_READY)
811 if ((ptmpdata == NULL) || (Size == 0U))
813 return HAL_ERROR;
816 /* Process Locked */
817 __HAL_LOCK(hsmartcard);
819 hsmartcard->gState = HAL_SMARTCARD_STATE_BUSY_TX;
821 /* Init tickstart for timeout management */
822 tickstart = HAL_GetTick();
824 /* Disable the Peripheral first to update mode for TX master */
825 CLEAR_BIT(hsmartcard->Instance->CR1, USART_CR1_UE);
827 /* Disable Rx, enable Tx */
828 CLEAR_BIT(hsmartcard->Instance->CR1, USART_CR1_RE);
829 SET_BIT(hsmartcard->Instance->RQR, (uint16_t)SMARTCARD_RXDATA_FLUSH_REQUEST);
830 SET_BIT(hsmartcard->Instance->CR1, USART_CR1_TE);
832 /* Enable the Peripheral */
833 SET_BIT(hsmartcard->Instance->CR1, USART_CR1_UE);
835 hsmartcard->ErrorCode = HAL_SMARTCARD_ERROR_NONE;
836 hsmartcard->TxXferSize = Size;
837 hsmartcard->TxXferCount = Size;
839 while (hsmartcard->TxXferCount > 0U)
841 hsmartcard->TxXferCount--;
842 if (SMARTCARD_WaitOnFlagUntilTimeout(hsmartcard, SMARTCARD_FLAG_TXE, RESET, tickstart, Timeout) != HAL_OK)
844 return HAL_TIMEOUT;
846 hsmartcard->Instance->TDR = (uint8_t)(*ptmpdata & 0xFFU);
847 ptmpdata++;
849 if (SMARTCARD_WaitOnFlagUntilTimeout(hsmartcard, SMARTCARD_TRANSMISSION_COMPLETION_FLAG(hsmartcard), RESET, tickstart,
850 Timeout) != HAL_OK)
852 return HAL_TIMEOUT;
854 /* Re-enable Rx at end of transmission if initial mode is Rx/Tx */
855 if (hsmartcard->Init.Mode == SMARTCARD_MODE_TX_RX)
857 /* Disable the Peripheral first to update modes */
858 CLEAR_BIT(hsmartcard->Instance->CR1, USART_CR1_UE);
859 SET_BIT(hsmartcard->Instance->CR1, USART_CR1_RE);
860 /* Enable the Peripheral */
861 SET_BIT(hsmartcard->Instance->CR1, USART_CR1_UE);
864 /* At end of Tx process, restore hsmartcard->gState to Ready */
865 hsmartcard->gState = HAL_SMARTCARD_STATE_READY;
867 /* Process Unlocked */
868 __HAL_UNLOCK(hsmartcard);
870 return HAL_OK;
872 else
874 return HAL_BUSY;
879 * @brief Receive an amount of data in blocking mode.
880 * @note When FIFO mode is enabled, the RXFNE flag is set as long as the RXFIFO
881 * is not empty. Read operations from the RDR register are performed when
882 * RXFNE flag is set. From hardware perspective, RXFNE flag and
883 * RXNE are mapped on the same bit-field.
884 * @param hsmartcard Pointer to a SMARTCARD_HandleTypeDef structure that contains
885 * the configuration information for the specified SMARTCARD module.
886 * @param pData pointer to data buffer.
887 * @param Size amount of data to be received.
888 * @param Timeout Timeout duration.
889 * @retval HAL status
891 HAL_StatusTypeDef HAL_SMARTCARD_Receive(SMARTCARD_HandleTypeDef *hsmartcard, uint8_t *pData, uint16_t Size,
892 uint32_t Timeout)
894 uint32_t tickstart;
895 uint8_t *ptmpdata = pData;
897 /* Check that a Rx process is not already ongoing */
898 if (hsmartcard->RxState == HAL_SMARTCARD_STATE_READY)
900 if ((ptmpdata == NULL) || (Size == 0U))
902 return HAL_ERROR;
905 /* Process Locked */
906 __HAL_LOCK(hsmartcard);
908 hsmartcard->ErrorCode = HAL_SMARTCARD_ERROR_NONE;
909 hsmartcard->RxState = HAL_SMARTCARD_STATE_BUSY_RX;
911 /* Init tickstart for timeout management */
912 tickstart = HAL_GetTick();
914 hsmartcard->RxXferSize = Size;
915 hsmartcard->RxXferCount = Size;
917 /* Check the remain data to be received */
918 while (hsmartcard->RxXferCount > 0U)
920 hsmartcard->RxXferCount--;
922 if (SMARTCARD_WaitOnFlagUntilTimeout(hsmartcard, SMARTCARD_FLAG_RXNE, RESET, tickstart, Timeout) != HAL_OK)
924 return HAL_TIMEOUT;
926 *ptmpdata = (uint8_t)(hsmartcard->Instance->RDR & (uint8_t)0x00FF);
927 ptmpdata++;
930 /* At end of Rx process, restore hsmartcard->RxState to Ready */
931 hsmartcard->RxState = HAL_SMARTCARD_STATE_READY;
933 /* Process Unlocked */
934 __HAL_UNLOCK(hsmartcard);
936 return HAL_OK;
938 else
940 return HAL_BUSY;
945 * @brief Send an amount of data in interrupt mode.
946 * @note When FIFO mode is disabled, USART interrupt is generated whenever
947 * USART_TDR register is empty, i.e one interrupt per data to transmit.
948 * @note When FIFO mode is enabled, USART interrupt is generated whenever
949 * TXFIFO threshold reached. In that case the interrupt rate depends on
950 * TXFIFO threshold configuration.
951 * @note This function sets the hsmartcard->TxIsr function pointer according to
952 * the FIFO mode (data transmission processing depends on FIFO mode).
953 * @param hsmartcard Pointer to a SMARTCARD_HandleTypeDef structure that contains
954 * the configuration information for the specified SMARTCARD module.
955 * @param pData pointer to data buffer.
956 * @param Size amount of data to be sent.
957 * @retval HAL status
959 HAL_StatusTypeDef HAL_SMARTCARD_Transmit_IT(SMARTCARD_HandleTypeDef *hsmartcard, uint8_t *pData, uint16_t Size)
961 /* Check that a Tx process is not already ongoing */
962 if (hsmartcard->gState == HAL_SMARTCARD_STATE_READY)
964 if ((pData == NULL) || (Size == 0U))
966 return HAL_ERROR;
969 /* Process Locked */
970 __HAL_LOCK(hsmartcard);
972 hsmartcard->ErrorCode = HAL_SMARTCARD_ERROR_NONE;
973 hsmartcard->gState = HAL_SMARTCARD_STATE_BUSY_TX;
975 hsmartcard->pTxBuffPtr = pData;
976 hsmartcard->TxXferSize = Size;
977 hsmartcard->TxXferCount = Size;
978 hsmartcard->TxISR = NULL;
980 /* Disable the Peripheral first to update mode for TX master */
981 CLEAR_BIT(hsmartcard->Instance->CR1, USART_CR1_UE);
983 /* Disable Rx, enable Tx */
984 CLEAR_BIT(hsmartcard->Instance->CR1, USART_CR1_RE);
985 SET_BIT(hsmartcard->Instance->RQR, (uint16_t)SMARTCARD_RXDATA_FLUSH_REQUEST);
986 SET_BIT(hsmartcard->Instance->CR1, USART_CR1_TE);
988 /* Enable the Peripheral */
989 SET_BIT(hsmartcard->Instance->CR1, USART_CR1_UE);
991 /* Configure Tx interrupt processing */
992 if (hsmartcard->FifoMode == SMARTCARD_FIFOMODE_ENABLE)
994 /* Set the Tx ISR function pointer */
995 hsmartcard->TxISR = SMARTCARD_TxISR_FIFOEN;
997 /* Process Unlocked */
998 __HAL_UNLOCK(hsmartcard);
1000 /* Enable the SMARTCARD Error Interrupt: (Frame error) */
1001 SET_BIT(hsmartcard->Instance->CR3, USART_CR3_EIE);
1003 /* Enable the TX FIFO threshold interrupt */
1004 SET_BIT(hsmartcard->Instance->CR3, USART_CR3_TXFTIE);
1006 else
1008 /* Set the Tx ISR function pointer */
1009 hsmartcard->TxISR = SMARTCARD_TxISR;
1011 /* Process Unlocked */
1012 __HAL_UNLOCK(hsmartcard);
1014 /* Enable the SMARTCARD Error Interrupt: (Frame error) */
1015 SET_BIT(hsmartcard->Instance->CR3, USART_CR3_EIE);
1017 /* Enable the SMARTCARD Transmit Data Register Empty Interrupt */
1018 SET_BIT(hsmartcard->Instance->CR1, USART_CR1_TXEIE_TXFNFIE);
1021 return HAL_OK;
1023 else
1025 return HAL_BUSY;
1030 * @brief Receive an amount of data in interrupt mode.
1031 * @note When FIFO mode is disabled, USART interrupt is generated whenever
1032 * USART_RDR register can be read, i.e one interrupt per data to receive.
1033 * @note When FIFO mode is enabled, USART interrupt is generated whenever
1034 * RXFIFO threshold reached. In that case the interrupt rate depends on
1035 * RXFIFO threshold configuration.
1036 * @note This function sets the hsmartcard->RxIsr function pointer according to
1037 * the FIFO mode (data reception processing depends on FIFO mode).
1038 * @param hsmartcard Pointer to a SMARTCARD_HandleTypeDef structure that contains
1039 * the configuration information for the specified SMARTCARD module.
1040 * @param pData pointer to data buffer.
1041 * @param Size amount of data to be received.
1042 * @retval HAL status
1044 HAL_StatusTypeDef HAL_SMARTCARD_Receive_IT(SMARTCARD_HandleTypeDef *hsmartcard, uint8_t *pData, uint16_t Size)
1046 /* Check that a Rx process is not already ongoing */
1047 if (hsmartcard->RxState == HAL_SMARTCARD_STATE_READY)
1049 if ((pData == NULL) || (Size == 0U))
1051 return HAL_ERROR;
1054 /* Process Locked */
1055 __HAL_LOCK(hsmartcard);
1057 hsmartcard->ErrorCode = HAL_SMARTCARD_ERROR_NONE;
1058 hsmartcard->RxState = HAL_SMARTCARD_STATE_BUSY_RX;
1060 hsmartcard->pRxBuffPtr = pData;
1061 hsmartcard->RxXferSize = Size;
1062 hsmartcard->RxXferCount = Size;
1064 /* Configure Rx interrupt processing */
1065 if ((hsmartcard->FifoMode == SMARTCARD_FIFOMODE_ENABLE) && (Size >= hsmartcard->NbRxDataToProcess))
1067 /* Set the Rx ISR function pointer */
1068 hsmartcard->RxISR = SMARTCARD_RxISR_FIFOEN;
1070 /* Process Unlocked */
1071 __HAL_UNLOCK(hsmartcard);
1073 /* Enable the SMARTCART Parity Error interrupt and RX FIFO Threshold interrupt */
1074 SET_BIT(hsmartcard->Instance->CR1, USART_CR1_PEIE);
1075 SET_BIT(hsmartcard->Instance->CR3, USART_CR3_RXFTIE);
1077 else
1079 /* Set the Rx ISR function pointer */
1080 hsmartcard->RxISR = SMARTCARD_RxISR;
1082 /* Process Unlocked */
1083 __HAL_UNLOCK(hsmartcard);
1085 /* Enable the SMARTCARD Parity Error and Data Register not empty Interrupts */
1086 SET_BIT(hsmartcard->Instance->CR1, USART_CR1_PEIE | USART_CR1_RXNEIE_RXFNEIE);
1089 /* Enable the SMARTCARD Error Interrupt: (Frame error, noise error, overrun error) */
1090 SET_BIT(hsmartcard->Instance->CR3, USART_CR3_EIE);
1092 return HAL_OK;
1094 else
1096 return HAL_BUSY;
1101 * @brief Send an amount of data in DMA mode.
1102 * @param hsmartcard Pointer to a SMARTCARD_HandleTypeDef structure that contains
1103 * the configuration information for the specified SMARTCARD module.
1104 * @param pData pointer to data buffer.
1105 * @param Size amount of data to be sent.
1106 * @retval HAL status
1108 HAL_StatusTypeDef HAL_SMARTCARD_Transmit_DMA(SMARTCARD_HandleTypeDef *hsmartcard, uint8_t *pData, uint16_t Size)
1110 /* Check that a Tx process is not already ongoing */
1111 if (hsmartcard->gState == HAL_SMARTCARD_STATE_READY)
1113 if ((pData == NULL) || (Size == 0U))
1115 return HAL_ERROR;
1118 /* Process Locked */
1119 __HAL_LOCK(hsmartcard);
1121 hsmartcard->gState = HAL_SMARTCARD_STATE_BUSY_TX;
1123 hsmartcard->ErrorCode = HAL_SMARTCARD_ERROR_NONE;
1124 hsmartcard->pTxBuffPtr = pData;
1125 hsmartcard->TxXferSize = Size;
1126 hsmartcard->TxXferCount = Size;
1128 /* Disable the Peripheral first to update mode for TX master */
1129 CLEAR_BIT(hsmartcard->Instance->CR1, USART_CR1_UE);
1131 /* Disable Rx, enable Tx */
1132 CLEAR_BIT(hsmartcard->Instance->CR1, USART_CR1_RE);
1133 SET_BIT(hsmartcard->Instance->RQR, (uint16_t)SMARTCARD_RXDATA_FLUSH_REQUEST);
1134 SET_BIT(hsmartcard->Instance->CR1, USART_CR1_TE);
1136 /* Enable the Peripheral */
1137 SET_BIT(hsmartcard->Instance->CR1, USART_CR1_UE);
1139 /* Set the SMARTCARD DMA transfer complete callback */
1140 hsmartcard->hdmatx->XferCpltCallback = SMARTCARD_DMATransmitCplt;
1142 /* Set the SMARTCARD error callback */
1143 hsmartcard->hdmatx->XferErrorCallback = SMARTCARD_DMAError;
1145 /* Set the DMA abort callback */
1146 hsmartcard->hdmatx->XferAbortCallback = NULL;
1148 /* Enable the SMARTCARD transmit DMA channel */
1149 if (HAL_DMA_Start_IT(hsmartcard->hdmatx, (uint32_t)hsmartcard->pTxBuffPtr, (uint32_t)&hsmartcard->Instance->TDR,
1150 Size) == HAL_OK)
1152 /* Clear the TC flag in the ICR register */
1153 CLEAR_BIT(hsmartcard->Instance->ICR, USART_ICR_TCCF);
1155 /* Process Unlocked */
1156 __HAL_UNLOCK(hsmartcard);
1158 /* Enable the UART Error Interrupt: (Frame error) */
1159 SET_BIT(hsmartcard->Instance->CR3, USART_CR3_EIE);
1161 /* Enable the DMA transfer for transmit request by setting the DMAT bit
1162 in the SMARTCARD associated USART CR3 register */
1163 SET_BIT(hsmartcard->Instance->CR3, USART_CR3_DMAT);
1165 return HAL_OK;
1167 else
1169 /* Set error code to DMA */
1170 hsmartcard->ErrorCode = HAL_SMARTCARD_ERROR_DMA;
1172 /* Process Unlocked */
1173 __HAL_UNLOCK(hsmartcard);
1175 /* Restore hsmartcard->State to ready */
1176 hsmartcard->gState = HAL_SMARTCARD_STATE_READY;
1178 return HAL_ERROR;
1181 else
1183 return HAL_BUSY;
1188 * @brief Receive an amount of data in DMA mode.
1189 * @param hsmartcard Pointer to a SMARTCARD_HandleTypeDef structure that contains
1190 * the configuration information for the specified SMARTCARD module.
1191 * @param pData pointer to data buffer.
1192 * @param Size amount of data to be received.
1193 * @note The SMARTCARD-associated USART parity is enabled (PCE = 1),
1194 * the received data contain the parity bit (MSB position).
1195 * @retval HAL status
1197 HAL_StatusTypeDef HAL_SMARTCARD_Receive_DMA(SMARTCARD_HandleTypeDef *hsmartcard, uint8_t *pData, uint16_t Size)
1199 /* Check that a Rx process is not already ongoing */
1200 if (hsmartcard->RxState == HAL_SMARTCARD_STATE_READY)
1202 if ((pData == NULL) || (Size == 0U))
1204 return HAL_ERROR;
1207 /* Process Locked */
1208 __HAL_LOCK(hsmartcard);
1210 hsmartcard->ErrorCode = HAL_SMARTCARD_ERROR_NONE;
1211 hsmartcard->RxState = HAL_SMARTCARD_STATE_BUSY_RX;
1213 hsmartcard->pRxBuffPtr = pData;
1214 hsmartcard->RxXferSize = Size;
1216 /* Set the SMARTCARD DMA transfer complete callback */
1217 hsmartcard->hdmarx->XferCpltCallback = SMARTCARD_DMAReceiveCplt;
1219 /* Set the SMARTCARD DMA error callback */
1220 hsmartcard->hdmarx->XferErrorCallback = SMARTCARD_DMAError;
1222 /* Set the DMA abort callback */
1223 hsmartcard->hdmarx->XferAbortCallback = NULL;
1225 /* Enable the DMA channel */
1226 if (HAL_DMA_Start_IT(hsmartcard->hdmarx, (uint32_t)&hsmartcard->Instance->RDR, (uint32_t)hsmartcard->pRxBuffPtr,
1227 Size) == HAL_OK)
1229 /* Process Unlocked */
1230 __HAL_UNLOCK(hsmartcard);
1232 /* Enable the SMARTCARD Parity Error Interrupt */
1233 SET_BIT(hsmartcard->Instance->CR1, USART_CR1_PEIE);
1235 /* Enable the SMARTCARD Error Interrupt: (Frame error, noise error, overrun error) */
1236 SET_BIT(hsmartcard->Instance->CR3, USART_CR3_EIE);
1238 /* Enable the DMA transfer for the receiver request by setting the DMAR bit
1239 in the SMARTCARD associated USART CR3 register */
1240 SET_BIT(hsmartcard->Instance->CR3, USART_CR3_DMAR);
1242 return HAL_OK;
1244 else
1246 /* Set error code to DMA */
1247 hsmartcard->ErrorCode = HAL_SMARTCARD_ERROR_DMA;
1249 /* Process Unlocked */
1250 __HAL_UNLOCK(hsmartcard);
1252 /* Restore hsmartcard->State to ready */
1253 hsmartcard->RxState = HAL_SMARTCARD_STATE_READY;
1255 return HAL_ERROR;
1258 else
1260 return HAL_BUSY;
1265 * @brief Abort ongoing transfers (blocking mode).
1266 * @param hsmartcard Pointer to a SMARTCARD_HandleTypeDef structure that contains
1267 * the configuration information for the specified SMARTCARD module.
1268 * @note This procedure could be used for aborting any ongoing transfer started in Interrupt or DMA mode.
1269 * This procedure performs following operations :
1270 * - Disable SMARTCARD Interrupts (Tx and Rx)
1271 * - Disable the DMA transfer in the peripheral register (if enabled)
1272 * - Abort DMA transfer by calling HAL_DMA_Abort (in case of transfer in DMA mode)
1273 * - Set handle State to READY
1274 * @note This procedure is executed in blocking mode : when exiting function, Abort is considered as completed.
1275 * @retval HAL status
1277 HAL_StatusTypeDef HAL_SMARTCARD_Abort(SMARTCARD_HandleTypeDef *hsmartcard)
1279 /* Disable RTOIE, EOBIE, TXEIE, TCIE, RXNE, PE, RXFT, TXFT and ERR (Frame error, noise error, overrun error) interrupts */
1280 CLEAR_BIT(hsmartcard->Instance->CR1,
1281 (USART_CR1_RXNEIE_RXFNEIE | USART_CR1_PEIE | USART_CR1_TXEIE_TXFNFIE | USART_CR1_TCIE | USART_CR1_RTOIE |
1282 USART_CR1_EOBIE));
1283 CLEAR_BIT(hsmartcard->Instance->CR3, (USART_CR3_EIE | USART_CR3_RXFTIE | USART_CR3_TXFTIE));
1285 /* Disable the SMARTCARD DMA Tx request if enabled */
1286 if (HAL_IS_BIT_SET(hsmartcard->Instance->CR3, USART_CR3_DMAT))
1288 CLEAR_BIT(hsmartcard->Instance->CR3, USART_CR3_DMAT);
1290 /* Abort the SMARTCARD DMA Tx channel : use blocking DMA Abort API (no callback) */
1291 if (hsmartcard->hdmatx != NULL)
1293 /* Set the SMARTCARD DMA Abort callback to Null.
1294 No call back execution at end of DMA abort procedure */
1295 hsmartcard->hdmatx->XferAbortCallback = NULL;
1297 if (HAL_DMA_Abort(hsmartcard->hdmatx) != HAL_OK)
1299 if (HAL_DMA_GetError(hsmartcard->hdmatx) == HAL_DMA_ERROR_TIMEOUT)
1301 /* Set error code to DMA */
1302 hsmartcard->ErrorCode = HAL_SMARTCARD_ERROR_DMA;
1304 return HAL_TIMEOUT;
1310 /* Disable the SMARTCARD DMA Rx request if enabled */
1311 if (HAL_IS_BIT_SET(hsmartcard->Instance->CR3, USART_CR3_DMAR))
1313 CLEAR_BIT(hsmartcard->Instance->CR3, USART_CR3_DMAR);
1315 /* Abort the SMARTCARD DMA Rx channel : use blocking DMA Abort API (no callback) */
1316 if (hsmartcard->hdmarx != NULL)
1318 /* Set the SMARTCARD DMA Abort callback to Null.
1319 No call back execution at end of DMA abort procedure */
1320 hsmartcard->hdmarx->XferAbortCallback = NULL;
1322 if (HAL_DMA_Abort(hsmartcard->hdmarx) != HAL_OK)
1324 if (HAL_DMA_GetError(hsmartcard->hdmarx) == HAL_DMA_ERROR_TIMEOUT)
1326 /* Set error code to DMA */
1327 hsmartcard->ErrorCode = HAL_SMARTCARD_ERROR_DMA;
1329 return HAL_TIMEOUT;
1335 /* Reset Tx and Rx transfer counters */
1336 hsmartcard->TxXferCount = 0U;
1337 hsmartcard->RxXferCount = 0U;
1339 /* Clear the Error flags in the ICR register */
1340 __HAL_SMARTCARD_CLEAR_FLAG(hsmartcard,
1341 SMARTCARD_CLEAR_OREF | SMARTCARD_CLEAR_NEF | SMARTCARD_CLEAR_PEF | SMARTCARD_CLEAR_FEF | SMARTCARD_CLEAR_RTOF |
1342 SMARTCARD_CLEAR_EOBF);
1344 /* Restore hsmartcard->gState and hsmartcard->RxState to Ready */
1345 hsmartcard->gState = HAL_SMARTCARD_STATE_READY;
1346 hsmartcard->RxState = HAL_SMARTCARD_STATE_READY;
1348 /* Reset Handle ErrorCode to No Error */
1349 hsmartcard->ErrorCode = HAL_SMARTCARD_ERROR_NONE;
1351 return HAL_OK;
1355 * @brief Abort ongoing Transmit transfer (blocking mode).
1356 * @param hsmartcard Pointer to a SMARTCARD_HandleTypeDef structure that contains
1357 * the configuration information for the specified SMARTCARD module.
1358 * @note This procedure could be used for aborting any ongoing Tx transfer started in Interrupt or DMA mode.
1359 * This procedure performs following operations :
1360 * - Disable SMARTCARD Interrupts (Tx)
1361 * - Disable the DMA transfer in the peripheral register (if enabled)
1362 * - Abort DMA transfer by calling HAL_DMA_Abort (in case of transfer in DMA mode)
1363 * - Set handle State to READY
1364 * @note This procedure is executed in blocking mode : when exiting function, Abort is considered as completed.
1365 * @retval HAL status
1367 HAL_StatusTypeDef HAL_SMARTCARD_AbortTransmit(SMARTCARD_HandleTypeDef *hsmartcard)
1369 /* Disable TCIE, TXEIE and TXFTIE interrupts */
1370 CLEAR_BIT(hsmartcard->Instance->CR1, (USART_CR1_TXEIE_TXFNFIE | USART_CR1_TCIE));
1371 CLEAR_BIT(hsmartcard->Instance->CR3, USART_CR3_TXFTIE);
1373 /* Check if a receive process is ongoing or not. If not disable ERR IT */
1374 if (hsmartcard->RxState == HAL_SMARTCARD_STATE_READY)
1376 /* Disable the SMARTCARD Error Interrupt: (Frame error) */
1377 CLEAR_BIT(hsmartcard->Instance->CR3, USART_CR3_EIE);
1380 /* Disable the SMARTCARD DMA Tx request if enabled */
1381 if (HAL_IS_BIT_SET(hsmartcard->Instance->CR3, USART_CR3_DMAT))
1383 CLEAR_BIT(hsmartcard->Instance->CR3, USART_CR3_DMAT);
1385 /* Abort the SMARTCARD DMA Tx channel : use blocking DMA Abort API (no callback) */
1386 if (hsmartcard->hdmatx != NULL)
1388 /* Set the SMARTCARD DMA Abort callback to Null.
1389 No call back execution at end of DMA abort procedure */
1390 hsmartcard->hdmatx->XferAbortCallback = NULL;
1392 if (HAL_DMA_Abort(hsmartcard->hdmatx) != HAL_OK)
1394 if (HAL_DMA_GetError(hsmartcard->hdmatx) == HAL_DMA_ERROR_TIMEOUT)
1396 /* Set error code to DMA */
1397 hsmartcard->ErrorCode = HAL_SMARTCARD_ERROR_DMA;
1399 return HAL_TIMEOUT;
1405 /* Reset Tx transfer counter */
1406 hsmartcard->TxXferCount = 0U;
1408 /* Clear the Error flags in the ICR register */
1409 __HAL_SMARTCARD_CLEAR_FLAG(hsmartcard, SMARTCARD_CLEAR_FEF);
1411 /* Restore hsmartcard->gState to Ready */
1412 hsmartcard->gState = HAL_SMARTCARD_STATE_READY;
1414 return HAL_OK;
1418 * @brief Abort ongoing Receive transfer (blocking mode).
1419 * @param hsmartcard Pointer to a SMARTCARD_HandleTypeDef structure that contains
1420 * the configuration information for the specified SMARTCARD module.
1421 * @note This procedure could be used for aborting any ongoing Rx transfer started in Interrupt or DMA mode.
1422 * This procedure performs following operations :
1423 * - Disable SMARTCARD Interrupts (Rx)
1424 * - Disable the DMA transfer in the peripheral register (if enabled)
1425 * - Abort DMA transfer by calling HAL_DMA_Abort (in case of transfer in DMA mode)
1426 * - Set handle State to READY
1427 * @note This procedure is executed in blocking mode : when exiting function, Abort is considered as completed.
1428 * @retval HAL status
1430 HAL_StatusTypeDef HAL_SMARTCARD_AbortReceive(SMARTCARD_HandleTypeDef *hsmartcard)
1432 /* Disable RTOIE, EOBIE, RXNE, PE, RXFT, TXFT and ERR (Frame error, noise error, overrun error) interrupts */
1433 CLEAR_BIT(hsmartcard->Instance->CR1, (USART_CR1_RXNEIE_RXFNEIE | USART_CR1_PEIE | USART_CR1_RTOIE | USART_CR1_EOBIE));
1434 CLEAR_BIT(hsmartcard->Instance->CR3, (USART_CR3_EIE | USART_CR3_RXFTIE));
1436 /* Check if a Transmit process is ongoing or not. If not disable ERR IT */
1437 if (hsmartcard->gState == HAL_SMARTCARD_STATE_READY)
1439 /* Disable the SMARTCARD Error Interrupt: (Frame error) */
1440 CLEAR_BIT(hsmartcard->Instance->CR3, USART_CR3_EIE);
1443 /* Disable the SMARTCARD DMA Rx request if enabled */
1444 if (HAL_IS_BIT_SET(hsmartcard->Instance->CR3, USART_CR3_DMAR))
1446 CLEAR_BIT(hsmartcard->Instance->CR3, USART_CR3_DMAR);
1448 /* Abort the SMARTCARD DMA Rx channel : use blocking DMA Abort API (no callback) */
1449 if (hsmartcard->hdmarx != NULL)
1451 /* Set the SMARTCARD DMA Abort callback to Null.
1452 No call back execution at end of DMA abort procedure */
1453 hsmartcard->hdmarx->XferAbortCallback = NULL;
1455 if (HAL_DMA_Abort(hsmartcard->hdmarx) != HAL_OK)
1457 if (HAL_DMA_GetError(hsmartcard->hdmarx) == HAL_DMA_ERROR_TIMEOUT)
1459 /* Set error code to DMA */
1460 hsmartcard->ErrorCode = HAL_SMARTCARD_ERROR_DMA;
1462 return HAL_TIMEOUT;
1468 /* Reset Rx transfer counter */
1469 hsmartcard->RxXferCount = 0U;
1471 /* Clear the Error flags in the ICR register */
1472 __HAL_SMARTCARD_CLEAR_FLAG(hsmartcard,
1473 SMARTCARD_CLEAR_OREF | SMARTCARD_CLEAR_NEF | SMARTCARD_CLEAR_PEF | SMARTCARD_CLEAR_FEF | SMARTCARD_CLEAR_RTOF |
1474 SMARTCARD_CLEAR_EOBF);
1476 /* Restore hsmartcard->RxState to Ready */
1477 hsmartcard->RxState = HAL_SMARTCARD_STATE_READY;
1479 return HAL_OK;
1483 * @brief Abort ongoing transfers (Interrupt mode).
1484 * @param hsmartcard Pointer to a SMARTCARD_HandleTypeDef structure that contains
1485 * the configuration information for the specified SMARTCARD module.
1486 * @note This procedure could be used for aborting any ongoing transfer started in Interrupt or DMA mode.
1487 * This procedure performs following operations :
1488 * - Disable SMARTCARD Interrupts (Tx and Rx)
1489 * - Disable the DMA transfer in the peripheral register (if enabled)
1490 * - Abort DMA transfer by calling HAL_DMA_Abort_IT (in case of transfer in DMA mode)
1491 * - Set handle State to READY
1492 * - At abort completion, call user abort complete callback
1493 * @note This procedure is executed in Interrupt mode, meaning that abort procedure could be
1494 * considered as completed only when user abort complete callback is executed (not when exiting function).
1495 * @retval HAL status
1497 HAL_StatusTypeDef HAL_SMARTCARD_Abort_IT(SMARTCARD_HandleTypeDef *hsmartcard)
1499 uint32_t abortcplt = 1U;
1501 /* Disable RTOIE, EOBIE, TXEIE, TCIE, RXNE, PE, RXFT, TXFT and ERR (Frame error, noise error, overrun error) interrupts */
1502 CLEAR_BIT(hsmartcard->Instance->CR1,
1503 (USART_CR1_RXNEIE_RXFNEIE | USART_CR1_PEIE | USART_CR1_TXEIE_TXFNFIE | USART_CR1_TCIE | USART_CR1_RTOIE |
1504 USART_CR1_EOBIE));
1505 CLEAR_BIT(hsmartcard->Instance->CR3, (USART_CR3_EIE | USART_CR3_RXFTIE | USART_CR3_TXFTIE));
1507 /* If DMA Tx and/or DMA Rx Handles are associated to SMARTCARD Handle, DMA Abort complete callbacks should be initialised
1508 before any call to DMA Abort functions */
1509 /* DMA Tx Handle is valid */
1510 if (hsmartcard->hdmatx != NULL)
1512 /* Set DMA Abort Complete callback if SMARTCARD DMA Tx request if enabled.
1513 Otherwise, set it to NULL */
1514 if (HAL_IS_BIT_SET(hsmartcard->Instance->CR3, USART_CR3_DMAT))
1516 hsmartcard->hdmatx->XferAbortCallback = SMARTCARD_DMATxAbortCallback;
1518 else
1520 hsmartcard->hdmatx->XferAbortCallback = NULL;
1523 /* DMA Rx Handle is valid */
1524 if (hsmartcard->hdmarx != NULL)
1526 /* Set DMA Abort Complete callback if SMARTCARD DMA Rx request if enabled.
1527 Otherwise, set it to NULL */
1528 if (HAL_IS_BIT_SET(hsmartcard->Instance->CR3, USART_CR3_DMAR))
1530 hsmartcard->hdmarx->XferAbortCallback = SMARTCARD_DMARxAbortCallback;
1532 else
1534 hsmartcard->hdmarx->XferAbortCallback = NULL;
1538 /* Disable the SMARTCARD DMA Tx request if enabled */
1539 if (HAL_IS_BIT_SET(hsmartcard->Instance->CR3, USART_CR3_DMAT))
1541 /* Disable DMA Tx at UART level */
1542 CLEAR_BIT(hsmartcard->Instance->CR3, USART_CR3_DMAT);
1544 /* Abort the SMARTCARD DMA Tx channel : use non blocking DMA Abort API (callback) */
1545 if (hsmartcard->hdmatx != NULL)
1547 /* SMARTCARD Tx DMA Abort callback has already been initialised :
1548 will lead to call HAL_SMARTCARD_AbortCpltCallback() at end of DMA abort procedure */
1550 /* Abort DMA TX */
1551 if (HAL_DMA_Abort_IT(hsmartcard->hdmatx) != HAL_OK)
1553 hsmartcard->hdmatx->XferAbortCallback = NULL;
1555 else
1557 abortcplt = 0U;
1562 /* Disable the SMARTCARD DMA Rx request if enabled */
1563 if (HAL_IS_BIT_SET(hsmartcard->Instance->CR3, USART_CR3_DMAR))
1565 CLEAR_BIT(hsmartcard->Instance->CR3, USART_CR3_DMAR);
1567 /* Abort the SMARTCARD DMA Rx channel : use non blocking DMA Abort API (callback) */
1568 if (hsmartcard->hdmarx != NULL)
1570 /* SMARTCARD Rx DMA Abort callback has already been initialised :
1571 will lead to call HAL_SMARTCARD_AbortCpltCallback() at end of DMA abort procedure */
1573 /* Abort DMA RX */
1574 if (HAL_DMA_Abort_IT(hsmartcard->hdmarx) != HAL_OK)
1576 hsmartcard->hdmarx->XferAbortCallback = NULL;
1577 abortcplt = 1U;
1579 else
1581 abortcplt = 0U;
1586 /* if no DMA abort complete callback execution is required => call user Abort Complete callback */
1587 if (abortcplt == 1U)
1589 /* Reset Tx and Rx transfer counters */
1590 hsmartcard->TxXferCount = 0U;
1591 hsmartcard->RxXferCount = 0U;
1593 /* Clear ISR function pointers */
1594 hsmartcard->RxISR = NULL;
1595 hsmartcard->TxISR = NULL;
1597 /* Reset errorCode */
1598 hsmartcard->ErrorCode = HAL_SMARTCARD_ERROR_NONE;
1600 /* Clear the Error flags in the ICR register */
1601 __HAL_SMARTCARD_CLEAR_FLAG(hsmartcard,
1602 SMARTCARD_CLEAR_OREF | SMARTCARD_CLEAR_NEF | SMARTCARD_CLEAR_PEF | SMARTCARD_CLEAR_FEF | SMARTCARD_CLEAR_RTOF |
1603 SMARTCARD_CLEAR_EOBF);
1605 /* Restore hsmartcard->gState and hsmartcard->RxState to Ready */
1606 hsmartcard->gState = HAL_SMARTCARD_STATE_READY;
1607 hsmartcard->RxState = HAL_SMARTCARD_STATE_READY;
1609 /* As no DMA to be aborted, call directly user Abort complete callback */
1610 #if (USE_HAL_SMARTCARD_REGISTER_CALLBACKS == 1)
1611 /* Call registered Abort complete callback */
1612 hsmartcard->AbortCpltCallback(hsmartcard);
1613 #else
1614 /* Call legacy weak Abort complete callback */
1615 HAL_SMARTCARD_AbortCpltCallback(hsmartcard);
1616 #endif /* USE_HAL_SMARTCARD_REGISTER_CALLBACK */
1619 return HAL_OK;
1623 * @brief Abort ongoing Transmit transfer (Interrupt mode).
1624 * @param hsmartcard Pointer to a SMARTCARD_HandleTypeDef structure that contains
1625 * the configuration information for the specified SMARTCARD module.
1626 * @note This procedure could be used for aborting any ongoing Tx transfer started in Interrupt or DMA mode.
1627 * This procedure performs following operations :
1628 * - Disable SMARTCARD Interrupts (Tx)
1629 * - Disable the DMA transfer in the peripheral register (if enabled)
1630 * - Abort DMA transfer by calling HAL_DMA_Abort_IT (in case of transfer in DMA mode)
1631 * - Set handle State to READY
1632 * - At abort completion, call user abort complete callback
1633 * @note This procedure is executed in Interrupt mode, meaning that abort procedure could be
1634 * considered as completed only when user abort complete callback is executed (not when exiting function).
1635 * @retval HAL status
1637 HAL_StatusTypeDef HAL_SMARTCARD_AbortTransmit_IT(SMARTCARD_HandleTypeDef *hsmartcard)
1639 /* Disable TCIE, TXEIE and TXFTIE interrupts */
1640 CLEAR_BIT(hsmartcard->Instance->CR1, (USART_CR1_TXEIE_TXFNFIE | USART_CR1_TCIE));
1641 CLEAR_BIT(hsmartcard->Instance->CR3, USART_CR3_TXFTIE);
1643 /* Check if a receive process is ongoing or not. If not disable ERR IT */
1644 if (hsmartcard->RxState == HAL_SMARTCARD_STATE_READY)
1646 /* Disable the SMARTCARD Error Interrupt: (Frame error) */
1647 CLEAR_BIT(hsmartcard->Instance->CR3, USART_CR3_EIE);
1650 /* Disable the SMARTCARD DMA Tx request if enabled */
1651 if (HAL_IS_BIT_SET(hsmartcard->Instance->CR3, USART_CR3_DMAT))
1653 CLEAR_BIT(hsmartcard->Instance->CR3, USART_CR3_DMAT);
1655 /* Abort the SMARTCARD DMA Tx channel : use non blocking DMA Abort API (callback) */
1656 if (hsmartcard->hdmatx != NULL)
1658 /* Set the SMARTCARD DMA Abort callback :
1659 will lead to call HAL_SMARTCARD_AbortCpltCallback() at end of DMA abort procedure */
1660 hsmartcard->hdmatx->XferAbortCallback = SMARTCARD_DMATxOnlyAbortCallback;
1662 /* Abort DMA TX */
1663 if (HAL_DMA_Abort_IT(hsmartcard->hdmatx) != HAL_OK)
1665 /* Call Directly hsmartcard->hdmatx->XferAbortCallback function in case of error */
1666 hsmartcard->hdmatx->XferAbortCallback(hsmartcard->hdmatx);
1669 else
1671 /* Reset Tx transfer counter */
1672 hsmartcard->TxXferCount = 0U;
1674 /* Clear TxISR function pointers */
1675 hsmartcard->TxISR = NULL;
1677 /* Restore hsmartcard->gState to Ready */
1678 hsmartcard->gState = HAL_SMARTCARD_STATE_READY;
1680 /* As no DMA to be aborted, call directly user Abort complete callback */
1681 #if (USE_HAL_SMARTCARD_REGISTER_CALLBACKS == 1)
1682 /* Call registered Abort Transmit Complete Callback */
1683 hsmartcard->AbortTransmitCpltCallback(hsmartcard);
1684 #else
1685 /* Call legacy weak Abort Transmit Complete Callback */
1686 HAL_SMARTCARD_AbortTransmitCpltCallback(hsmartcard);
1687 #endif /* USE_HAL_SMARTCARD_REGISTER_CALLBACK */
1690 else
1692 /* Reset Tx transfer counter */
1693 hsmartcard->TxXferCount = 0U;
1695 /* Clear TxISR function pointers */
1696 hsmartcard->TxISR = NULL;
1698 /* Clear the Error flags in the ICR register */
1699 __HAL_SMARTCARD_CLEAR_FLAG(hsmartcard, SMARTCARD_CLEAR_FEF);
1701 /* Restore hsmartcard->gState to Ready */
1702 hsmartcard->gState = HAL_SMARTCARD_STATE_READY;
1704 /* As no DMA to be aborted, call directly user Abort complete callback */
1705 #if (USE_HAL_SMARTCARD_REGISTER_CALLBACKS == 1)
1706 /* Call registered Abort Transmit Complete Callback */
1707 hsmartcard->AbortTransmitCpltCallback(hsmartcard);
1708 #else
1709 /* Call legacy weak Abort Transmit Complete Callback */
1710 HAL_SMARTCARD_AbortTransmitCpltCallback(hsmartcard);
1711 #endif /* USE_HAL_SMARTCARD_REGISTER_CALLBACK */
1714 return HAL_OK;
1718 * @brief Abort ongoing Receive transfer (Interrupt mode).
1719 * @param hsmartcard Pointer to a SMARTCARD_HandleTypeDef structure that contains
1720 * the configuration information for the specified SMARTCARD module.
1721 * @note This procedure could be used for aborting any ongoing Rx transfer started in Interrupt or DMA mode.
1722 * This procedure performs following operations :
1723 * - Disable SMARTCARD Interrupts (Rx)
1724 * - Disable the DMA transfer in the peripheral register (if enabled)
1725 * - Abort DMA transfer by calling HAL_DMA_Abort_IT (in case of transfer in DMA mode)
1726 * - Set handle State to READY
1727 * - At abort completion, call user abort complete callback
1728 * @note This procedure is executed in Interrupt mode, meaning that abort procedure could be
1729 * considered as completed only when user abort complete callback is executed (not when exiting function).
1730 * @retval HAL status
1732 HAL_StatusTypeDef HAL_SMARTCARD_AbortReceive_IT(SMARTCARD_HandleTypeDef *hsmartcard)
1734 /* Disable RTOIE, EOBIE, RXNE, PE, RXFT and ERR (Frame error, noise error, overrun error) interrupts */
1735 CLEAR_BIT(hsmartcard->Instance->CR1, (USART_CR1_RXNEIE_RXFNEIE | USART_CR1_PEIE | USART_CR1_RTOIE | USART_CR1_EOBIE));
1736 CLEAR_BIT(hsmartcard->Instance->CR3, (USART_CR3_EIE | USART_CR3_RXFTIE));
1738 /* Check if a Transmit process is ongoing or not. If not disable ERR IT */
1739 if (hsmartcard->gState == HAL_SMARTCARD_STATE_READY)
1741 /* Disable the SMARTCARD Error Interrupt: (Frame error) */
1742 CLEAR_BIT(hsmartcard->Instance->CR3, USART_CR3_EIE);
1745 /* Disable the SMARTCARD DMA Rx request if enabled */
1746 if (HAL_IS_BIT_SET(hsmartcard->Instance->CR3, USART_CR3_DMAR))
1748 CLEAR_BIT(hsmartcard->Instance->CR3, USART_CR3_DMAR);
1750 /* Abort the SMARTCARD DMA Rx channel : use non blocking DMA Abort API (callback) */
1751 if (hsmartcard->hdmarx != NULL)
1753 /* Set the SMARTCARD DMA Abort callback :
1754 will lead to call HAL_SMARTCARD_AbortCpltCallback() at end of DMA abort procedure */
1755 hsmartcard->hdmarx->XferAbortCallback = SMARTCARD_DMARxOnlyAbortCallback;
1757 /* Abort DMA RX */
1758 if (HAL_DMA_Abort_IT(hsmartcard->hdmarx) != HAL_OK)
1760 /* Call Directly hsmartcard->hdmarx->XferAbortCallback function in case of error */
1761 hsmartcard->hdmarx->XferAbortCallback(hsmartcard->hdmarx);
1764 else
1766 /* Reset Rx transfer counter */
1767 hsmartcard->RxXferCount = 0U;
1769 /* Clear RxISR function pointer */
1770 hsmartcard->RxISR = NULL;
1772 /* Clear the Error flags in the ICR register */
1773 __HAL_SMARTCARD_CLEAR_FLAG(hsmartcard,
1774 SMARTCARD_CLEAR_OREF | SMARTCARD_CLEAR_NEF | SMARTCARD_CLEAR_PEF | SMARTCARD_CLEAR_FEF | SMARTCARD_CLEAR_RTOF |
1775 SMARTCARD_CLEAR_EOBF);
1777 /* Restore hsmartcard->RxState to Ready */
1778 hsmartcard->RxState = HAL_SMARTCARD_STATE_READY;
1780 /* As no DMA to be aborted, call directly user Abort complete callback */
1781 #if (USE_HAL_SMARTCARD_REGISTER_CALLBACKS == 1)
1782 /* Call registered Abort Receive Complete Callback */
1783 hsmartcard->AbortReceiveCpltCallback(hsmartcard);
1784 #else
1785 /* Call legacy weak Abort Receive Complete Callback */
1786 HAL_SMARTCARD_AbortReceiveCpltCallback(hsmartcard);
1787 #endif /* USE_HAL_SMARTCARD_REGISTER_CALLBACK */
1790 else
1792 /* Reset Rx transfer counter */
1793 hsmartcard->RxXferCount = 0U;
1795 /* Clear RxISR function pointer */
1796 hsmartcard->RxISR = NULL;
1798 /* Clear the Error flags in the ICR register */
1799 __HAL_SMARTCARD_CLEAR_FLAG(hsmartcard,
1800 SMARTCARD_CLEAR_OREF | SMARTCARD_CLEAR_NEF | SMARTCARD_CLEAR_PEF | SMARTCARD_CLEAR_FEF | SMARTCARD_CLEAR_RTOF |
1801 SMARTCARD_CLEAR_EOBF);
1803 /* Restore hsmartcard->RxState to Ready */
1804 hsmartcard->RxState = HAL_SMARTCARD_STATE_READY;
1806 /* As no DMA to be aborted, call directly user Abort complete callback */
1807 #if (USE_HAL_SMARTCARD_REGISTER_CALLBACKS == 1)
1808 /* Call registered Abort Receive Complete Callback */
1809 hsmartcard->AbortReceiveCpltCallback(hsmartcard);
1810 #else
1811 /* Call legacy weak Abort Receive Complete Callback */
1812 HAL_SMARTCARD_AbortReceiveCpltCallback(hsmartcard);
1813 #endif /* USE_HAL_SMARTCARD_REGISTER_CALLBACK */
1816 return HAL_OK;
1820 * @brief Handle SMARTCARD interrupt requests.
1821 * @param hsmartcard Pointer to a SMARTCARD_HandleTypeDef structure that contains
1822 * the configuration information for the specified SMARTCARD module.
1823 * @retval None
1825 void HAL_SMARTCARD_IRQHandler(SMARTCARD_HandleTypeDef *hsmartcard)
1827 uint32_t isrflags = READ_REG(hsmartcard->Instance->ISR);
1828 uint32_t cr1its = READ_REG(hsmartcard->Instance->CR1);
1829 uint32_t cr3its = READ_REG(hsmartcard->Instance->CR3);
1830 uint32_t errorflags;
1831 uint32_t errorcode;
1833 /* If no error occurs */
1834 errorflags = (isrflags & (uint32_t)(USART_ISR_PE | USART_ISR_FE | USART_ISR_ORE | USART_ISR_NE | USART_ISR_RTOF));
1835 if (errorflags == 0U)
1837 /* SMARTCARD in mode Receiver ---------------------------------------------------*/
1838 if (((isrflags & USART_ISR_RXNE_RXFNE) != 0U)
1839 && (((cr1its & USART_CR1_RXNEIE_RXFNEIE) != 0U)
1840 || ((cr3its & USART_CR3_RXFTIE) != 0U)))
1842 if (hsmartcard->RxISR != NULL)
1844 hsmartcard->RxISR(hsmartcard);
1846 return;
1850 /* If some errors occur */
1851 if ((errorflags != 0U)
1852 && ((((cr3its & (USART_CR3_RXFTIE | USART_CR3_EIE)) != 0U)
1853 || ((cr1its & (USART_CR1_RXNEIE_RXFNEIE | USART_CR1_PEIE)) != 0U))))
1855 /* SMARTCARD parity error interrupt occurred -------------------------------------*/
1856 if (((isrflags & USART_ISR_PE) != 0U) && ((cr1its & USART_CR1_PEIE) != 0U))
1858 __HAL_SMARTCARD_CLEAR_IT(hsmartcard, SMARTCARD_CLEAR_PEF);
1860 hsmartcard->ErrorCode |= HAL_SMARTCARD_ERROR_PE;
1863 /* SMARTCARD frame error interrupt occurred --------------------------------------*/
1864 if (((isrflags & USART_ISR_FE) != 0U) && ((cr3its & USART_CR3_EIE) != 0U))
1866 __HAL_SMARTCARD_CLEAR_IT(hsmartcard, SMARTCARD_CLEAR_FEF);
1868 hsmartcard->ErrorCode |= HAL_SMARTCARD_ERROR_FE;
1871 /* SMARTCARD noise error interrupt occurred --------------------------------------*/
1872 if (((isrflags & USART_ISR_NE) != 0U) && ((cr3its & USART_CR3_EIE) != 0U))
1874 __HAL_SMARTCARD_CLEAR_IT(hsmartcard, SMARTCARD_CLEAR_NEF);
1876 hsmartcard->ErrorCode |= HAL_SMARTCARD_ERROR_NE;
1879 /* SMARTCARD Over-Run interrupt occurred -----------------------------------------*/
1880 if (((isrflags & USART_ISR_ORE) != 0U)
1881 && (((cr1its & USART_CR1_RXNEIE_RXFNEIE) != 0U)
1882 || ((cr3its & USART_CR3_RXFTIE) != 0U)
1883 || ((cr3its & USART_CR3_EIE) != 0U)))
1885 __HAL_SMARTCARD_CLEAR_IT(hsmartcard, SMARTCARD_CLEAR_OREF);
1887 hsmartcard->ErrorCode |= HAL_SMARTCARD_ERROR_ORE;
1890 /* SMARTCARD receiver timeout interrupt occurred -----------------------------------------*/
1891 if (((isrflags & USART_ISR_RTOF) != 0U) && ((cr1its & USART_CR1_RTOIE) != 0U))
1893 __HAL_SMARTCARD_CLEAR_IT(hsmartcard, SMARTCARD_CLEAR_RTOF);
1895 hsmartcard->ErrorCode |= HAL_SMARTCARD_ERROR_RTO;
1898 /* Call SMARTCARD Error Call back function if need be --------------------------*/
1899 if (hsmartcard->ErrorCode != HAL_SMARTCARD_ERROR_NONE)
1901 /* SMARTCARD in mode Receiver ---------------------------------------------------*/
1902 if (((isrflags & USART_ISR_RXNE_RXFNE) != 0U)
1903 && (((cr1its & USART_CR1_RXNEIE_RXFNEIE) != 0U)
1904 || ((cr3its & USART_CR3_RXFTIE) != 0U)))
1906 if (hsmartcard->RxISR != NULL)
1908 hsmartcard->RxISR(hsmartcard);
1912 /* If Error is to be considered as blocking :
1913 - Receiver Timeout error in Reception
1914 - Overrun error in Reception
1915 - any error occurs in DMA mode reception
1917 errorcode = hsmartcard->ErrorCode;
1918 if ((HAL_IS_BIT_SET(hsmartcard->Instance->CR3, USART_CR3_DMAR))
1919 || ((errorcode & (HAL_SMARTCARD_ERROR_RTO | HAL_SMARTCARD_ERROR_ORE)) != 0U))
1921 /* Blocking error : transfer is aborted
1922 Set the SMARTCARD state ready to be able to start again the process,
1923 Disable Rx Interrupts, and disable Rx DMA request, if ongoing */
1924 SMARTCARD_EndRxTransfer(hsmartcard);
1926 /* Disable the SMARTCARD DMA Rx request if enabled */
1927 if (HAL_IS_BIT_SET(hsmartcard->Instance->CR3, USART_CR3_DMAR))
1929 CLEAR_BIT(hsmartcard->Instance->CR3, USART_CR3_DMAR);
1931 /* Abort the SMARTCARD DMA Rx channel */
1932 if (hsmartcard->hdmarx != NULL)
1934 /* Set the SMARTCARD DMA Abort callback :
1935 will lead to call HAL_SMARTCARD_ErrorCallback() at end of DMA abort procedure */
1936 hsmartcard->hdmarx->XferAbortCallback = SMARTCARD_DMAAbortOnError;
1938 /* Abort DMA RX */
1939 if (HAL_DMA_Abort_IT(hsmartcard->hdmarx) != HAL_OK)
1941 /* Call Directly hsmartcard->hdmarx->XferAbortCallback function in case of error */
1942 hsmartcard->hdmarx->XferAbortCallback(hsmartcard->hdmarx);
1945 else
1947 #if (USE_HAL_SMARTCARD_REGISTER_CALLBACKS == 1)
1948 /* Call registered user error callback */
1949 hsmartcard->ErrorCallback(hsmartcard);
1950 #else
1951 /* Call legacy weak user error callback */
1952 HAL_SMARTCARD_ErrorCallback(hsmartcard);
1953 #endif /* USE_HAL_SMARTCARD_REGISTER_CALLBACK */
1956 else
1958 #if (USE_HAL_SMARTCARD_REGISTER_CALLBACKS == 1)
1959 /* Call registered user error callback */
1960 hsmartcard->ErrorCallback(hsmartcard);
1961 #else
1962 /* Call legacy weak user error callback */
1963 HAL_SMARTCARD_ErrorCallback(hsmartcard);
1964 #endif /* USE_HAL_SMARTCARD_REGISTER_CALLBACK */
1967 /* other error type to be considered as blocking :
1968 - Frame error in Transmission
1970 else if ((hsmartcard->gState == HAL_SMARTCARD_STATE_BUSY_TX)
1971 && ((errorcode & HAL_SMARTCARD_ERROR_FE) != 0U))
1973 /* Blocking error : transfer is aborted
1974 Set the SMARTCARD state ready to be able to start again the process,
1975 Disable Tx Interrupts, and disable Tx DMA request, if ongoing */
1976 SMARTCARD_EndTxTransfer(hsmartcard);
1978 /* Disable the SMARTCARD DMA Tx request if enabled */
1979 if (HAL_IS_BIT_SET(hsmartcard->Instance->CR3, USART_CR3_DMAT))
1981 CLEAR_BIT(hsmartcard->Instance->CR3, USART_CR3_DMAT);
1983 /* Abort the SMARTCARD DMA Tx channel */
1984 if (hsmartcard->hdmatx != NULL)
1986 /* Set the SMARTCARD DMA Abort callback :
1987 will lead to call HAL_SMARTCARD_ErrorCallback() at end of DMA abort procedure */
1988 hsmartcard->hdmatx->XferAbortCallback = SMARTCARD_DMAAbortOnError;
1990 /* Abort DMA TX */
1991 if (HAL_DMA_Abort_IT(hsmartcard->hdmatx) != HAL_OK)
1993 /* Call Directly hsmartcard->hdmatx->XferAbortCallback function in case of error */
1994 hsmartcard->hdmatx->XferAbortCallback(hsmartcard->hdmatx);
1997 else
1999 #if (USE_HAL_SMARTCARD_REGISTER_CALLBACKS == 1)
2000 /* Call registered user error callback */
2001 hsmartcard->ErrorCallback(hsmartcard);
2002 #else
2003 /* Call legacy weak user error callback */
2004 HAL_SMARTCARD_ErrorCallback(hsmartcard);
2005 #endif /* USE_HAL_SMARTCARD_REGISTER_CALLBACK */
2008 else
2010 #if (USE_HAL_SMARTCARD_REGISTER_CALLBACKS == 1)
2011 /* Call registered user error callback */
2012 hsmartcard->ErrorCallback(hsmartcard);
2013 #else
2014 /* Call legacy weak user error callback */
2015 HAL_SMARTCARD_ErrorCallback(hsmartcard);
2016 #endif /* USE_HAL_SMARTCARD_REGISTER_CALLBACK */
2019 else
2021 /* Non Blocking error : transfer could go on.
2022 Error is notified to user through user error callback */
2023 #if (USE_HAL_SMARTCARD_REGISTER_CALLBACKS == 1)
2024 /* Call registered user error callback */
2025 hsmartcard->ErrorCallback(hsmartcard);
2026 #else
2027 /* Call legacy weak user error callback */
2028 HAL_SMARTCARD_ErrorCallback(hsmartcard);
2029 #endif /* USE_HAL_SMARTCARD_REGISTER_CALLBACK */
2030 hsmartcard->ErrorCode = HAL_SMARTCARD_ERROR_NONE;
2033 return;
2035 } /* End if some error occurs */
2037 /* SMARTCARD in mode Receiver, end of block interruption ------------------------*/
2038 if (((isrflags & USART_ISR_EOBF) != 0U) && ((cr1its & USART_CR1_EOBIE) != 0U))
2040 hsmartcard->RxState = HAL_SMARTCARD_STATE_READY;
2041 __HAL_UNLOCK(hsmartcard);
2042 #if (USE_HAL_SMARTCARD_REGISTER_CALLBACKS == 1)
2043 /* Call registered Rx complete callback */
2044 hsmartcard->RxCpltCallback(hsmartcard);
2045 #else
2046 /* Call legacy weak Rx complete callback */
2047 HAL_SMARTCARD_RxCpltCallback(hsmartcard);
2048 #endif /* USE_HAL_SMARTCARD_REGISTER_CALLBACK */
2049 /* Clear EOBF interrupt after HAL_SMARTCARD_RxCpltCallback() call for the End of Block information
2050 to be available during HAL_SMARTCARD_RxCpltCallback() processing */
2051 __HAL_SMARTCARD_CLEAR_IT(hsmartcard, SMARTCARD_CLEAR_EOBF);
2052 return;
2055 /* SMARTCARD in mode Transmitter ------------------------------------------------*/
2056 if (((isrflags & USART_ISR_TXE_TXFNF) != 0U)
2057 && (((cr1its & USART_CR1_TXEIE_TXFNFIE) != 0U)
2058 || ((cr3its & USART_CR3_TXFTIE) != 0U)))
2060 if (hsmartcard->TxISR != NULL)
2062 hsmartcard->TxISR(hsmartcard);
2064 return;
2067 /* SMARTCARD in mode Transmitter (transmission end) ------------------------*/
2068 if (__HAL_SMARTCARD_GET_IT(hsmartcard, hsmartcard->AdvancedInit.TxCompletionIndication) != RESET)
2070 if (__HAL_SMARTCARD_GET_IT_SOURCE(hsmartcard, hsmartcard->AdvancedInit.TxCompletionIndication) != RESET)
2072 SMARTCARD_EndTransmit_IT(hsmartcard);
2073 return;
2077 /* SMARTCARD TX Fifo Empty occurred ----------------------------------------------*/
2078 if (((isrflags & USART_ISR_TXFE) != 0U) && ((cr1its & USART_CR1_TXFEIE) != 0U))
2080 #if (USE_HAL_SMARTCARD_REGISTER_CALLBACKS == 1)
2081 /* Call registered Tx Fifo Empty Callback */
2082 hsmartcard->TxFifoEmptyCallback(hsmartcard);
2083 #else
2084 /* Call legacy weak Tx Fifo Empty Callback */
2085 HAL_SMARTCARDEx_TxFifoEmptyCallback(hsmartcard);
2086 #endif /* USE_HAL_SMARTCARD_REGISTER_CALLBACK */
2087 return;
2090 /* SMARTCARD RX Fifo Full occurred ----------------------------------------------*/
2091 if (((isrflags & USART_ISR_RXFF) != 0U) && ((cr1its & USART_CR1_RXFFIE) != 0U))
2093 #if (USE_HAL_SMARTCARD_REGISTER_CALLBACKS == 1)
2094 /* Call registered Rx Fifo Full Callback */
2095 hsmartcard->RxFifoFullCallback(hsmartcard);
2096 #else
2097 /* Call legacy weak Rx Fifo Full Callback */
2098 HAL_SMARTCARDEx_RxFifoFullCallback(hsmartcard);
2099 #endif /* USE_HAL_SMARTCARD_REGISTER_CALLBACK */
2100 return;
2105 * @brief Tx Transfer completed callback.
2106 * @param hsmartcard Pointer to a SMARTCARD_HandleTypeDef structure that contains
2107 * the configuration information for the specified SMARTCARD module.
2108 * @retval None
2110 __weak void HAL_SMARTCARD_TxCpltCallback(SMARTCARD_HandleTypeDef *hsmartcard)
2112 /* Prevent unused argument(s) compilation warning */
2113 UNUSED(hsmartcard);
2115 /* NOTE : This function should not be modified, when the callback is needed,
2116 the HAL_SMARTCARD_TxCpltCallback can be implemented in the user file.
2121 * @brief Rx Transfer completed callback.
2122 * @param hsmartcard Pointer to a SMARTCARD_HandleTypeDef structure that contains
2123 * the configuration information for the specified SMARTCARD module.
2124 * @retval None
2126 __weak void HAL_SMARTCARD_RxCpltCallback(SMARTCARD_HandleTypeDef *hsmartcard)
2128 /* Prevent unused argument(s) compilation warning */
2129 UNUSED(hsmartcard);
2131 /* NOTE : This function should not be modified, when the callback is needed,
2132 the HAL_SMARTCARD_RxCpltCallback can be implemented in the user file.
2137 * @brief SMARTCARD error callback.
2138 * @param hsmartcard Pointer to a SMARTCARD_HandleTypeDef structure that contains
2139 * the configuration information for the specified SMARTCARD module.
2140 * @retval None
2142 __weak void HAL_SMARTCARD_ErrorCallback(SMARTCARD_HandleTypeDef *hsmartcard)
2144 /* Prevent unused argument(s) compilation warning */
2145 UNUSED(hsmartcard);
2147 /* NOTE : This function should not be modified, when the callback is needed,
2148 the HAL_SMARTCARD_ErrorCallback can be implemented in the user file.
2153 * @brief SMARTCARD Abort Complete callback.
2154 * @param hsmartcard Pointer to a SMARTCARD_HandleTypeDef structure that contains
2155 * the configuration information for the specified SMARTCARD module.
2156 * @retval None
2158 __weak void HAL_SMARTCARD_AbortCpltCallback(SMARTCARD_HandleTypeDef *hsmartcard)
2160 /* Prevent unused argument(s) compilation warning */
2161 UNUSED(hsmartcard);
2163 /* NOTE : This function should not be modified, when the callback is needed,
2164 the HAL_SMARTCARD_AbortCpltCallback can be implemented in the user file.
2169 * @brief SMARTCARD Abort Complete callback.
2170 * @param hsmartcard Pointer to a SMARTCARD_HandleTypeDef structure that contains
2171 * the configuration information for the specified SMARTCARD module.
2172 * @retval None
2174 __weak void HAL_SMARTCARD_AbortTransmitCpltCallback(SMARTCARD_HandleTypeDef *hsmartcard)
2176 /* Prevent unused argument(s) compilation warning */
2177 UNUSED(hsmartcard);
2179 /* NOTE : This function should not be modified, when the callback is needed,
2180 the HAL_SMARTCARD_AbortTransmitCpltCallback can be implemented in the user file.
2185 * @brief SMARTCARD Abort Receive Complete callback.
2186 * @param hsmartcard Pointer to a SMARTCARD_HandleTypeDef structure that contains
2187 * the configuration information for the specified SMARTCARD module.
2188 * @retval None
2190 __weak void HAL_SMARTCARD_AbortReceiveCpltCallback(SMARTCARD_HandleTypeDef *hsmartcard)
2192 /* Prevent unused argument(s) compilation warning */
2193 UNUSED(hsmartcard);
2195 /* NOTE : This function should not be modified, when the callback is needed,
2196 the HAL_SMARTCARD_AbortReceiveCpltCallback can be implemented in the user file.
2201 * @}
2204 /** @defgroup SMARTCARD_Exported_Functions_Group4 Peripheral State and Errors functions
2205 * @brief SMARTCARD State and Errors functions
2207 @verbatim
2208 ==============================================================================
2209 ##### Peripheral State and Errors functions #####
2210 ==============================================================================
2211 [..]
2212 This subsection provides a set of functions allowing to return the State of SmartCard
2213 handle and also return Peripheral Errors occurred during communication process
2214 (+) HAL_SMARTCARD_GetState() API can be helpful to check in run-time the state
2215 of the SMARTCARD peripheral.
2216 (+) HAL_SMARTCARD_GetError() checks in run-time errors that could occur during
2217 communication.
2219 @endverbatim
2220 * @{
2224 * @brief Return the SMARTCARD handle state.
2225 * @param hsmartcard Pointer to a SMARTCARD_HandleTypeDef structure that contains
2226 * the configuration information for the specified SMARTCARD module.
2227 * @retval SMARTCARD handle state
2229 HAL_SMARTCARD_StateTypeDef HAL_SMARTCARD_GetState(SMARTCARD_HandleTypeDef *hsmartcard)
2231 /* Return SMARTCARD handle state */
2232 uint32_t temp1;
2233 uint32_t temp2;
2234 temp1 = (uint32_t)hsmartcard->gState;
2235 temp2 = (uint32_t)hsmartcard->RxState;
2237 return (HAL_SMARTCARD_StateTypeDef)(temp1 | temp2);
2241 * @brief Return the SMARTCARD handle error code.
2242 * @param hsmartcard Pointer to a SMARTCARD_HandleTypeDef structure that contains
2243 * the configuration information for the specified SMARTCARD module.
2244 * @retval SMARTCARD handle Error Code
2246 uint32_t HAL_SMARTCARD_GetError(SMARTCARD_HandleTypeDef *hsmartcard)
2248 return hsmartcard->ErrorCode;
2252 * @}
2256 * @}
2259 /** @defgroup SMARTCARD_Private_Functions SMARTCARD Private Functions
2260 * @{
2263 #if (USE_HAL_SMARTCARD_REGISTER_CALLBACKS == 1)
2265 * @brief Initialize the callbacks to their default values.
2266 * @param hsmartcard SMARTCARD handle.
2267 * @retval none
2269 void SMARTCARD_InitCallbacksToDefault(SMARTCARD_HandleTypeDef *hsmartcard)
2271 /* Init the SMARTCARD Callback settings */
2272 hsmartcard->TxCpltCallback = HAL_SMARTCARD_TxCpltCallback; /* Legacy weak TxCpltCallback */
2273 hsmartcard->RxCpltCallback = HAL_SMARTCARD_RxCpltCallback; /* Legacy weak RxCpltCallback */
2274 hsmartcard->ErrorCallback = HAL_SMARTCARD_ErrorCallback; /* Legacy weak ErrorCallback */
2275 hsmartcard->AbortCpltCallback = HAL_SMARTCARD_AbortCpltCallback; /* Legacy weak AbortCpltCallback */
2276 hsmartcard->AbortTransmitCpltCallback = HAL_SMARTCARD_AbortTransmitCpltCallback; /* Legacy weak AbortTransmitCpltCallback */
2277 hsmartcard->AbortReceiveCpltCallback = HAL_SMARTCARD_AbortReceiveCpltCallback; /* Legacy weak AbortReceiveCpltCallback */
2278 hsmartcard->RxFifoFullCallback = HAL_SMARTCARDEx_RxFifoFullCallback; /* Legacy weak RxFifoFullCallback */
2279 hsmartcard->TxFifoEmptyCallback = HAL_SMARTCARDEx_TxFifoEmptyCallback; /* Legacy weak TxFifoEmptyCallback */
2282 #endif /* USE_HAL_SMARTCARD_REGISTER_CALLBACKS */
2285 * @brief Configure the SMARTCARD associated USART peripheral.
2286 * @param hsmartcard Pointer to a SMARTCARD_HandleTypeDef structure that contains
2287 * the configuration information for the specified SMARTCARD module.
2288 * @retval HAL status
2290 static HAL_StatusTypeDef SMARTCARD_SetConfig(SMARTCARD_HandleTypeDef *hsmartcard)
2292 uint32_t tmpreg;
2293 SMARTCARD_ClockSourceTypeDef clocksource;
2294 HAL_StatusTypeDef ret = HAL_OK;
2295 const uint16_t SMARTCARDPrescTable[12] = {1U, 2U, 4U, 6U, 8U, 10U, 12U, 16U, 32U, 64U, 128U, 256U};
2296 PLL2_ClocksTypeDef pll2_clocks;
2297 PLL3_ClocksTypeDef pll3_clocks;
2298 uint32_t pclk;
2300 /* Check the parameters */
2301 assert_param(IS_SMARTCARD_INSTANCE(hsmartcard->Instance));
2302 assert_param(IS_SMARTCARD_BAUDRATE(hsmartcard->Init.BaudRate));
2303 assert_param(IS_SMARTCARD_WORD_LENGTH(hsmartcard->Init.WordLength));
2304 assert_param(IS_SMARTCARD_STOPBITS(hsmartcard->Init.StopBits));
2305 assert_param(IS_SMARTCARD_PARITY(hsmartcard->Init.Parity));
2306 assert_param(IS_SMARTCARD_MODE(hsmartcard->Init.Mode));
2307 assert_param(IS_SMARTCARD_POLARITY(hsmartcard->Init.CLKPolarity));
2308 assert_param(IS_SMARTCARD_PHASE(hsmartcard->Init.CLKPhase));
2309 assert_param(IS_SMARTCARD_LASTBIT(hsmartcard->Init.CLKLastBit));
2310 assert_param(IS_SMARTCARD_ONE_BIT_SAMPLE(hsmartcard->Init.OneBitSampling));
2311 assert_param(IS_SMARTCARD_NACK(hsmartcard->Init.NACKEnable));
2312 assert_param(IS_SMARTCARD_TIMEOUT(hsmartcard->Init.TimeOutEnable));
2313 assert_param(IS_SMARTCARD_AUTORETRY_COUNT(hsmartcard->Init.AutoRetryCount));
2314 assert_param(IS_SMARTCARD_CLOCKPRESCALER(hsmartcard->Init.ClockPrescaler));
2316 /*-------------------------- USART CR1 Configuration -----------------------*/
2317 /* In SmartCard mode, M and PCE are forced to 1 (8 bits + parity).
2318 * Oversampling is forced to 16 (OVER8 = 0).
2319 * Configure the Parity and Mode:
2320 * set PS bit according to hsmartcard->Init.Parity value
2321 * set TE and RE bits according to hsmartcard->Init.Mode value */
2322 tmpreg = (uint32_t) hsmartcard->Init.Parity | hsmartcard->Init.Mode;
2323 tmpreg |= (uint32_t) hsmartcard->Init.WordLength | hsmartcard->FifoMode;
2324 MODIFY_REG(hsmartcard->Instance->CR1, USART_CR1_FIELDS, tmpreg);
2326 /*-------------------------- USART CR2 Configuration -----------------------*/
2327 tmpreg = hsmartcard->Init.StopBits;
2328 /* Synchronous mode is activated by default */
2329 tmpreg |= (uint32_t) USART_CR2_CLKEN | hsmartcard->Init.CLKPolarity;
2330 tmpreg |= (uint32_t) hsmartcard->Init.CLKPhase | hsmartcard->Init.CLKLastBit;
2331 tmpreg |= (uint32_t) hsmartcard->Init.TimeOutEnable;
2332 MODIFY_REG(hsmartcard->Instance->CR2, USART_CR2_FIELDS, tmpreg);
2334 /*-------------------------- USART CR3 Configuration -----------------------*/
2335 /* Configure
2336 * - one-bit sampling method versus three samples' majority rule
2337 * according to hsmartcard->Init.OneBitSampling
2338 * - NACK transmission in case of parity error according
2339 * to hsmartcard->Init.NACKEnable
2340 * - autoretry counter according to hsmartcard->Init.AutoRetryCount */
2342 tmpreg = (uint32_t) hsmartcard->Init.OneBitSampling | hsmartcard->Init.NACKEnable;
2343 tmpreg |= ((uint32_t)hsmartcard->Init.AutoRetryCount << USART_CR3_SCARCNT_Pos);
2344 MODIFY_REG(hsmartcard->Instance->CR3, USART_CR3_FIELDS, tmpreg);
2346 /*--------------------- SMARTCARD clock PRESC Configuration ----------------*/
2347 /* Configure
2348 * - SMARTCARD Clock Prescaler: set PRESCALER according to hsmartcard->Init.ClockPrescaler value */
2349 MODIFY_REG(hsmartcard->Instance->PRESC, USART_PRESC_PRESCALER, hsmartcard->Init.ClockPrescaler);
2351 /*-------------------------- USART GTPR Configuration ----------------------*/
2352 tmpreg = (hsmartcard->Init.Prescaler | ((uint32_t)hsmartcard->Init.GuardTime << USART_GTPR_GT_Pos));
2353 MODIFY_REG(hsmartcard->Instance->GTPR, (uint16_t)(USART_GTPR_GT | USART_GTPR_PSC), (uint16_t)tmpreg);
2355 /*-------------------------- USART RTOR Configuration ----------------------*/
2356 tmpreg = ((uint32_t)hsmartcard->Init.BlockLength << USART_RTOR_BLEN_Pos);
2357 if (hsmartcard->Init.TimeOutEnable == SMARTCARD_TIMEOUT_ENABLE)
2359 assert_param(IS_SMARTCARD_TIMEOUT_VALUE(hsmartcard->Init.TimeOutValue));
2360 tmpreg |= (uint32_t) hsmartcard->Init.TimeOutValue;
2362 MODIFY_REG(hsmartcard->Instance->RTOR, (USART_RTOR_RTO | USART_RTOR_BLEN), tmpreg);
2364 /*-------------------------- USART BRR Configuration -----------------------*/
2365 SMARTCARD_GETCLOCKSOURCE(hsmartcard, clocksource);
2366 tmpreg = 0U;
2367 switch (clocksource)
2369 case SMARTCARD_CLOCKSOURCE_D2PCLK1:
2370 pclk = HAL_RCC_GetPCLK1Freq();
2371 tmpreg = (uint16_t)(((pclk / SMARTCARDPrescTable[hsmartcard->Init.ClockPrescaler]) + (hsmartcard->Init.BaudRate / 2U)) / hsmartcard->Init.BaudRate);
2372 break;
2373 case SMARTCARD_CLOCKSOURCE_D2PCLK2:
2374 pclk = HAL_RCC_GetPCLK2Freq();
2375 tmpreg = (uint16_t)(((pclk / SMARTCARDPrescTable[hsmartcard->Init.ClockPrescaler]) + (hsmartcard->Init.BaudRate / 2U)) / hsmartcard->Init.BaudRate);
2376 break;
2377 case SMARTCARD_CLOCKSOURCE_PLL2Q:
2378 HAL_RCCEx_GetPLL2ClockFreq(&pll2_clocks);
2379 tmpreg = (uint16_t)(((pll2_clocks.PLL2_Q_Frequency / SMARTCARDPrescTable[hsmartcard->Init.ClockPrescaler]) + (hsmartcard->Init.BaudRate / 2U)) / hsmartcard->Init.BaudRate);
2380 break;
2381 case SMARTCARD_CLOCKSOURCE_PLL3Q:
2382 HAL_RCCEx_GetPLL3ClockFreq(&pll3_clocks);
2383 tmpreg = (uint16_t)(((pll3_clocks.PLL3_Q_Frequency / SMARTCARDPrescTable[hsmartcard->Init.ClockPrescaler]) + (hsmartcard->Init.BaudRate / 2U)) / hsmartcard->Init.BaudRate);
2384 break;
2385 case SMARTCARD_CLOCKSOURCE_HSI:
2386 if (__HAL_RCC_GET_FLAG(RCC_FLAG_HSIDIV) != 0U)
2388 tmpreg = (uint16_t)((((HSI_VALUE >> (__HAL_RCC_GET_HSI_DIVIDER() >> 3U)) / SMARTCARDPrescTable[hsmartcard->Init.ClockPrescaler]) + (hsmartcard->Init.BaudRate / 2U)) / hsmartcard->Init.BaudRate);
2390 else
2392 tmpreg = (uint16_t)(((HSI_VALUE / SMARTCARDPrescTable[hsmartcard->Init.ClockPrescaler]) + (hsmartcard->Init.BaudRate / 2U)) / hsmartcard->Init.BaudRate);
2394 break;
2395 case SMARTCARD_CLOCKSOURCE_CSI:
2396 tmpreg = (uint16_t)(((CSI_VALUE / SMARTCARDPrescTable[hsmartcard->Init.ClockPrescaler]) + (hsmartcard->Init.BaudRate / 2U)) / hsmartcard->Init.BaudRate);
2397 break;
2398 case SMARTCARD_CLOCKSOURCE_LSE:
2399 tmpreg = (uint16_t)(((uint16_t)(LSE_VALUE / SMARTCARDPrescTable[hsmartcard->Init.ClockPrescaler]) + (hsmartcard->Init.BaudRate / 2U)) / hsmartcard->Init.BaudRate);
2400 break;
2401 default:
2402 ret = HAL_ERROR;
2403 break;
2406 /* USARTDIV must be greater than or equal to 0d16 */
2407 if ((tmpreg >= USART_BRR_MIN) && (tmpreg <= USART_BRR_MAX))
2409 hsmartcard->Instance->BRR = tmpreg;
2411 else
2413 ret = HAL_ERROR;
2416 /* Initialize the number of data to process during RX/TX ISR execution */
2417 hsmartcard->NbTxDataToProcess = 1U;
2418 hsmartcard->NbRxDataToProcess = 1U;
2420 /* Clear ISR function pointers */
2421 hsmartcard->RxISR = NULL;
2422 hsmartcard->TxISR = NULL;
2424 return ret;
2429 * @brief Configure the SMARTCARD associated USART peripheral advanced features.
2430 * @param hsmartcard Pointer to a SMARTCARD_HandleTypeDef structure that contains
2431 * the configuration information for the specified SMARTCARD module.
2432 * @retval None
2434 static void SMARTCARD_AdvFeatureConfig(SMARTCARD_HandleTypeDef *hsmartcard)
2436 /* Check whether the set of advanced features to configure is properly set */
2437 assert_param(IS_SMARTCARD_ADVFEATURE_INIT(hsmartcard->AdvancedInit.AdvFeatureInit));
2439 /* if required, configure TX pin active level inversion */
2440 if (HAL_IS_BIT_SET(hsmartcard->AdvancedInit.AdvFeatureInit, SMARTCARD_ADVFEATURE_TXINVERT_INIT))
2442 assert_param(IS_SMARTCARD_ADVFEATURE_TXINV(hsmartcard->AdvancedInit.TxPinLevelInvert));
2443 MODIFY_REG(hsmartcard->Instance->CR2, USART_CR2_TXINV, hsmartcard->AdvancedInit.TxPinLevelInvert);
2446 /* if required, configure RX pin active level inversion */
2447 if (HAL_IS_BIT_SET(hsmartcard->AdvancedInit.AdvFeatureInit, SMARTCARD_ADVFEATURE_RXINVERT_INIT))
2449 assert_param(IS_SMARTCARD_ADVFEATURE_RXINV(hsmartcard->AdvancedInit.RxPinLevelInvert));
2450 MODIFY_REG(hsmartcard->Instance->CR2, USART_CR2_RXINV, hsmartcard->AdvancedInit.RxPinLevelInvert);
2453 /* if required, configure data inversion */
2454 if (HAL_IS_BIT_SET(hsmartcard->AdvancedInit.AdvFeatureInit, SMARTCARD_ADVFEATURE_DATAINVERT_INIT))
2456 assert_param(IS_SMARTCARD_ADVFEATURE_DATAINV(hsmartcard->AdvancedInit.DataInvert));
2457 MODIFY_REG(hsmartcard->Instance->CR2, USART_CR2_DATAINV, hsmartcard->AdvancedInit.DataInvert);
2460 /* if required, configure RX/TX pins swap */
2461 if (HAL_IS_BIT_SET(hsmartcard->AdvancedInit.AdvFeatureInit, SMARTCARD_ADVFEATURE_SWAP_INIT))
2463 assert_param(IS_SMARTCARD_ADVFEATURE_SWAP(hsmartcard->AdvancedInit.Swap));
2464 MODIFY_REG(hsmartcard->Instance->CR2, USART_CR2_SWAP, hsmartcard->AdvancedInit.Swap);
2467 /* if required, configure RX overrun detection disabling */
2468 if (HAL_IS_BIT_SET(hsmartcard->AdvancedInit.AdvFeatureInit, SMARTCARD_ADVFEATURE_RXOVERRUNDISABLE_INIT))
2470 assert_param(IS_SMARTCARD_OVERRUN(hsmartcard->AdvancedInit.OverrunDisable));
2471 MODIFY_REG(hsmartcard->Instance->CR3, USART_CR3_OVRDIS, hsmartcard->AdvancedInit.OverrunDisable);
2474 /* if required, configure DMA disabling on reception error */
2475 if (HAL_IS_BIT_SET(hsmartcard->AdvancedInit.AdvFeatureInit, SMARTCARD_ADVFEATURE_DMADISABLEONERROR_INIT))
2477 assert_param(IS_SMARTCARD_ADVFEATURE_DMAONRXERROR(hsmartcard->AdvancedInit.DMADisableonRxError));
2478 MODIFY_REG(hsmartcard->Instance->CR3, USART_CR3_DDRE, hsmartcard->AdvancedInit.DMADisableonRxError);
2481 /* if required, configure MSB first on communication line */
2482 if (HAL_IS_BIT_SET(hsmartcard->AdvancedInit.AdvFeatureInit, SMARTCARD_ADVFEATURE_MSBFIRST_INIT))
2484 assert_param(IS_SMARTCARD_ADVFEATURE_MSBFIRST(hsmartcard->AdvancedInit.MSBFirst));
2485 MODIFY_REG(hsmartcard->Instance->CR2, USART_CR2_MSBFIRST, hsmartcard->AdvancedInit.MSBFirst);
2491 * @brief Check the SMARTCARD Idle State.
2492 * @param hsmartcard Pointer to a SMARTCARD_HandleTypeDef structure that contains
2493 * the configuration information for the specified SMARTCARD module.
2494 * @retval HAL status
2496 static HAL_StatusTypeDef SMARTCARD_CheckIdleState(SMARTCARD_HandleTypeDef *hsmartcard)
2498 uint32_t tickstart;
2500 /* Initialize the SMARTCARD ErrorCode */
2501 hsmartcard->ErrorCode = HAL_SMARTCARD_ERROR_NONE;
2503 /* Init tickstart for timeout management */
2504 tickstart = HAL_GetTick();
2506 /* Check if the Transmitter is enabled */
2507 if ((hsmartcard->Instance->CR1 & USART_CR1_TE) == USART_CR1_TE)
2509 /* Wait until TEACK flag is set */
2510 if (SMARTCARD_WaitOnFlagUntilTimeout(hsmartcard, USART_ISR_TEACK, RESET, tickstart,
2511 SMARTCARD_TEACK_REACK_TIMEOUT) != HAL_OK)
2513 /* Timeout occurred */
2514 return HAL_TIMEOUT;
2517 /* Check if the Receiver is enabled */
2518 if ((hsmartcard->Instance->CR1 & USART_CR1_RE) == USART_CR1_RE)
2520 /* Wait until REACK flag is set */
2521 if (SMARTCARD_WaitOnFlagUntilTimeout(hsmartcard, USART_ISR_REACK, RESET, tickstart,
2522 SMARTCARD_TEACK_REACK_TIMEOUT) != HAL_OK)
2524 /* Timeout occurred */
2525 return HAL_TIMEOUT;
2529 /* Initialize the SMARTCARD states */
2530 hsmartcard->gState = HAL_SMARTCARD_STATE_READY;
2531 hsmartcard->RxState = HAL_SMARTCARD_STATE_READY;
2533 /* Process Unlocked */
2534 __HAL_UNLOCK(hsmartcard);
2536 return HAL_OK;
2540 * @brief Handle SMARTCARD Communication Timeout.
2541 * @param hsmartcard Pointer to a SMARTCARD_HandleTypeDef structure that contains
2542 * the configuration information for the specified SMARTCARD module.
2543 * @param Flag Specifies the SMARTCARD flag to check.
2544 * @param Status The new Flag status (SET or RESET).
2545 * @param Tickstart Tick start value
2546 * @param Timeout Timeout duration.
2547 * @retval HAL status
2549 static HAL_StatusTypeDef SMARTCARD_WaitOnFlagUntilTimeout(SMARTCARD_HandleTypeDef *hsmartcard, uint32_t Flag,
2550 FlagStatus Status, uint32_t Tickstart, uint32_t Timeout)
2552 /* Wait until flag is set */
2553 while ((__HAL_SMARTCARD_GET_FLAG(hsmartcard, Flag) ? SET : RESET) == Status)
2555 /* Check for the Timeout */
2556 if (Timeout != HAL_MAX_DELAY)
2558 if (((HAL_GetTick() - Tickstart) > Timeout) || (Timeout == 0U))
2560 /* Disable TXE, RXNE, PE and ERR (Frame error, noise error, overrun error) interrupts for the interrupt process */
2561 CLEAR_BIT(hsmartcard->Instance->CR1, (USART_CR1_RXNEIE_RXFNEIE | USART_CR1_PEIE | USART_CR1_TXEIE_TXFNFIE));
2562 CLEAR_BIT(hsmartcard->Instance->CR3, USART_CR3_EIE);
2564 hsmartcard->gState = HAL_SMARTCARD_STATE_READY;
2565 hsmartcard->RxState = HAL_SMARTCARD_STATE_READY;
2567 /* Process Unlocked */
2568 __HAL_UNLOCK(hsmartcard);
2569 return HAL_TIMEOUT;
2573 return HAL_OK;
2578 * @brief End ongoing Tx transfer on SMARTCARD peripheral (following error detection or Transmit completion).
2579 * @param hsmartcard Pointer to a SMARTCARD_HandleTypeDef structure that contains
2580 * the configuration information for the specified SMARTCARD module.
2581 * @retval None
2583 static void SMARTCARD_EndTxTransfer(SMARTCARD_HandleTypeDef *hsmartcard)
2585 /* Disable TXEIE, TCIE and ERR (Frame error, noise error, overrun error) interrupts */
2586 CLEAR_BIT(hsmartcard->Instance->CR1, (USART_CR1_TXEIE_TXFNFIE | USART_CR1_TCIE));
2587 CLEAR_BIT(hsmartcard->Instance->CR3, USART_CR3_EIE);
2589 /* At end of Tx process, restore hsmartcard->gState to Ready */
2590 hsmartcard->gState = HAL_SMARTCARD_STATE_READY;
2595 * @brief End ongoing Rx transfer on UART peripheral (following error detection or Reception completion).
2596 * @param hsmartcard Pointer to a SMARTCARD_HandleTypeDef structure that contains
2597 * the configuration information for the specified SMARTCARD module.
2598 * @retval None
2600 static void SMARTCARD_EndRxTransfer(SMARTCARD_HandleTypeDef *hsmartcard)
2602 /* Disable RXNE, PE and ERR (Frame error, noise error, overrun error) interrupts */
2603 CLEAR_BIT(hsmartcard->Instance->CR1, (USART_CR1_RXNEIE_RXFNEIE | USART_CR1_PEIE));
2604 CLEAR_BIT(hsmartcard->Instance->CR3, USART_CR3_EIE);
2606 /* At end of Rx process, restore hsmartcard->RxState to Ready */
2607 hsmartcard->RxState = HAL_SMARTCARD_STATE_READY;
2612 * @brief DMA SMARTCARD transmit process complete callback.
2613 * @param hdma Pointer to a DMA_HandleTypeDef structure that contains
2614 * the configuration information for the specified DMA module.
2615 * @retval None
2617 static void SMARTCARD_DMATransmitCplt(DMA_HandleTypeDef *hdma)
2619 SMARTCARD_HandleTypeDef *hsmartcard = (SMARTCARD_HandleTypeDef *)(hdma->Parent);
2620 hsmartcard->TxXferCount = 0U;
2622 /* Disable the DMA transfer for transmit request by resetting the DMAT bit
2623 in the SMARTCARD associated USART CR3 register */
2624 CLEAR_BIT(hsmartcard->Instance->CR3, USART_CR3_DMAT);
2626 /* Enable the SMARTCARD Transmit Complete Interrupt */
2627 __HAL_SMARTCARD_ENABLE_IT(hsmartcard, hsmartcard->AdvancedInit.TxCompletionIndication);
2631 * @brief DMA SMARTCARD receive process complete callback.
2632 * @param hdma Pointer to a DMA_HandleTypeDef structure that contains
2633 * the configuration information for the specified DMA module.
2634 * @retval None
2636 static void SMARTCARD_DMAReceiveCplt(DMA_HandleTypeDef *hdma)
2638 SMARTCARD_HandleTypeDef *hsmartcard = (SMARTCARD_HandleTypeDef *)(hdma->Parent);
2639 hsmartcard->RxXferCount = 0U;
2641 /* Disable PE and ERR (Frame error, noise error, overrun error) interrupts */
2642 CLEAR_BIT(hsmartcard->Instance->CR1, USART_CR1_PEIE);
2643 CLEAR_BIT(hsmartcard->Instance->CR3, USART_CR3_EIE);
2645 /* Disable the DMA transfer for the receiver request by resetting the DMAR bit
2646 in the SMARTCARD associated USART CR3 register */
2647 CLEAR_BIT(hsmartcard->Instance->CR3, USART_CR3_DMAR);
2649 /* At end of Rx process, restore hsmartcard->RxState to Ready */
2650 hsmartcard->RxState = HAL_SMARTCARD_STATE_READY;
2652 #if (USE_HAL_SMARTCARD_REGISTER_CALLBACKS == 1)
2653 /* Call registered Rx complete callback */
2654 hsmartcard->RxCpltCallback(hsmartcard);
2655 #else
2656 /* Call legacy weak Rx complete callback */
2657 HAL_SMARTCARD_RxCpltCallback(hsmartcard);
2658 #endif /* USE_HAL_SMARTCARD_REGISTER_CALLBACK */
2662 * @brief DMA SMARTCARD communication error callback.
2663 * @param hdma Pointer to a DMA_HandleTypeDef structure that contains
2664 * the configuration information for the specified DMA module.
2665 * @retval None
2667 static void SMARTCARD_DMAError(DMA_HandleTypeDef *hdma)
2669 SMARTCARD_HandleTypeDef *hsmartcard = (SMARTCARD_HandleTypeDef *)(hdma->Parent);
2671 /* Stop SMARTCARD DMA Tx request if ongoing */
2672 if (hsmartcard->gState == HAL_SMARTCARD_STATE_BUSY_TX)
2674 if (HAL_IS_BIT_SET(hsmartcard->Instance->CR3, USART_CR3_DMAT))
2676 hsmartcard->TxXferCount = 0U;
2677 SMARTCARD_EndTxTransfer(hsmartcard);
2681 /* Stop SMARTCARD DMA Rx request if ongoing */
2682 if (hsmartcard->RxState == HAL_SMARTCARD_STATE_BUSY_RX)
2684 if (HAL_IS_BIT_SET(hsmartcard->Instance->CR3, USART_CR3_DMAR))
2686 hsmartcard->RxXferCount = 0U;
2687 SMARTCARD_EndRxTransfer(hsmartcard);
2691 hsmartcard->ErrorCode |= HAL_SMARTCARD_ERROR_DMA;
2692 #if (USE_HAL_SMARTCARD_REGISTER_CALLBACKS == 1)
2693 /* Call registered user error callback */
2694 hsmartcard->ErrorCallback(hsmartcard);
2695 #else
2696 /* Call legacy weak user error callback */
2697 HAL_SMARTCARD_ErrorCallback(hsmartcard);
2698 #endif /* USE_HAL_SMARTCARD_REGISTER_CALLBACK */
2702 * @brief DMA SMARTCARD communication abort callback, when initiated by HAL services on Error
2703 * (To be called at end of DMA Abort procedure following error occurrence).
2704 * @param hdma DMA handle.
2705 * @retval None
2707 static void SMARTCARD_DMAAbortOnError(DMA_HandleTypeDef *hdma)
2709 SMARTCARD_HandleTypeDef *hsmartcard = (SMARTCARD_HandleTypeDef *)(hdma->Parent);
2710 hsmartcard->RxXferCount = 0U;
2711 hsmartcard->TxXferCount = 0U;
2713 #if (USE_HAL_SMARTCARD_REGISTER_CALLBACKS == 1)
2714 /* Call registered user error callback */
2715 hsmartcard->ErrorCallback(hsmartcard);
2716 #else
2717 /* Call legacy weak user error callback */
2718 HAL_SMARTCARD_ErrorCallback(hsmartcard);
2719 #endif /* USE_HAL_SMARTCARD_REGISTER_CALLBACK */
2723 * @brief DMA SMARTCARD Tx communication abort callback, when initiated by user
2724 * (To be called at end of DMA Tx Abort procedure following user abort request).
2725 * @note When this callback is executed, User Abort complete call back is called only if no
2726 * Abort still ongoing for Rx DMA Handle.
2727 * @param hdma DMA handle.
2728 * @retval None
2730 static void SMARTCARD_DMATxAbortCallback(DMA_HandleTypeDef *hdma)
2732 SMARTCARD_HandleTypeDef *hsmartcard = (SMARTCARD_HandleTypeDef *)(hdma->Parent);
2734 hsmartcard->hdmatx->XferAbortCallback = NULL;
2736 /* Check if an Abort process is still ongoing */
2737 if (hsmartcard->hdmarx != NULL)
2739 if (hsmartcard->hdmarx->XferAbortCallback != NULL)
2741 return;
2745 /* No Abort process still ongoing : All DMA channels are aborted, call user Abort Complete callback */
2746 hsmartcard->TxXferCount = 0U;
2747 hsmartcard->RxXferCount = 0U;
2749 /* Reset errorCode */
2750 hsmartcard->ErrorCode = HAL_SMARTCARD_ERROR_NONE;
2752 /* Clear the Error flags in the ICR register */
2753 __HAL_SMARTCARD_CLEAR_FLAG(hsmartcard,
2754 SMARTCARD_CLEAR_OREF | SMARTCARD_CLEAR_NEF | SMARTCARD_CLEAR_PEF | SMARTCARD_CLEAR_FEF | SMARTCARD_CLEAR_RTOF |
2755 SMARTCARD_CLEAR_EOBF);
2757 /* Restore hsmartcard->gState and hsmartcard->RxState to Ready */
2758 hsmartcard->gState = HAL_SMARTCARD_STATE_READY;
2759 hsmartcard->RxState = HAL_SMARTCARD_STATE_READY;
2761 #if (USE_HAL_SMARTCARD_REGISTER_CALLBACKS == 1)
2762 /* Call registered Abort complete callback */
2763 hsmartcard->AbortCpltCallback(hsmartcard);
2764 #else
2765 /* Call legacy weak Abort complete callback */
2766 HAL_SMARTCARD_AbortCpltCallback(hsmartcard);
2767 #endif /* USE_HAL_SMARTCARD_REGISTER_CALLBACK */
2772 * @brief DMA SMARTCARD Rx communication abort callback, when initiated by user
2773 * (To be called at end of DMA Rx Abort procedure following user abort request).
2774 * @note When this callback is executed, User Abort complete call back is called only if no
2775 * Abort still ongoing for Tx DMA Handle.
2776 * @param hdma DMA handle.
2777 * @retval None
2779 static void SMARTCARD_DMARxAbortCallback(DMA_HandleTypeDef *hdma)
2781 SMARTCARD_HandleTypeDef *hsmartcard = (SMARTCARD_HandleTypeDef *)(hdma->Parent);
2783 hsmartcard->hdmarx->XferAbortCallback = NULL;
2785 /* Check if an Abort process is still ongoing */
2786 if (hsmartcard->hdmatx != NULL)
2788 if (hsmartcard->hdmatx->XferAbortCallback != NULL)
2790 return;
2794 /* No Abort process still ongoing : All DMA channels are aborted, call user Abort Complete callback */
2795 hsmartcard->TxXferCount = 0U;
2796 hsmartcard->RxXferCount = 0U;
2798 /* Reset errorCode */
2799 hsmartcard->ErrorCode = HAL_SMARTCARD_ERROR_NONE;
2801 /* Clear the Error flags in the ICR register */
2802 __HAL_SMARTCARD_CLEAR_FLAG(hsmartcard,
2803 SMARTCARD_CLEAR_OREF | SMARTCARD_CLEAR_NEF | SMARTCARD_CLEAR_PEF | SMARTCARD_CLEAR_FEF | SMARTCARD_CLEAR_RTOF |
2804 SMARTCARD_CLEAR_EOBF);
2806 /* Restore hsmartcard->gState and hsmartcard->RxState to Ready */
2807 hsmartcard->gState = HAL_SMARTCARD_STATE_READY;
2808 hsmartcard->RxState = HAL_SMARTCARD_STATE_READY;
2810 #if (USE_HAL_SMARTCARD_REGISTER_CALLBACKS == 1)
2811 /* Call registered Abort complete callback */
2812 hsmartcard->AbortCpltCallback(hsmartcard);
2813 #else
2814 /* Call legacy weak Abort complete callback */
2815 HAL_SMARTCARD_AbortCpltCallback(hsmartcard);
2816 #endif /* USE_HAL_SMARTCARD_REGISTER_CALLBACK */
2821 * @brief DMA SMARTCARD Tx communication abort callback, when initiated by user by a call to
2822 * HAL_SMARTCARD_AbortTransmit_IT API (Abort only Tx transfer)
2823 * (This callback is executed at end of DMA Tx Abort procedure following user abort request,
2824 * and leads to user Tx Abort Complete callback execution).
2825 * @param hdma DMA handle.
2826 * @retval None
2828 static void SMARTCARD_DMATxOnlyAbortCallback(DMA_HandleTypeDef *hdma)
2830 SMARTCARD_HandleTypeDef *hsmartcard = (SMARTCARD_HandleTypeDef *)(hdma->Parent);
2832 hsmartcard->TxXferCount = 0U;
2834 /* Clear the Error flags in the ICR register */
2835 __HAL_SMARTCARD_CLEAR_FLAG(hsmartcard, SMARTCARD_CLEAR_FEF);
2837 /* Restore hsmartcard->gState to Ready */
2838 hsmartcard->gState = HAL_SMARTCARD_STATE_READY;
2840 #if (USE_HAL_SMARTCARD_REGISTER_CALLBACKS == 1)
2841 /* Call registered Abort Transmit Complete Callback */
2842 hsmartcard->AbortTransmitCpltCallback(hsmartcard);
2843 #else
2844 /* Call legacy weak Abort Transmit Complete Callback */
2845 HAL_SMARTCARD_AbortTransmitCpltCallback(hsmartcard);
2846 #endif /* USE_HAL_SMARTCARD_REGISTER_CALLBACK */
2850 * @brief DMA SMARTCARD Rx communication abort callback, when initiated by user by a call to
2851 * HAL_SMARTCARD_AbortReceive_IT API (Abort only Rx transfer)
2852 * (This callback is executed at end of DMA Rx Abort procedure following user abort request,
2853 * and leads to user Rx Abort Complete callback execution).
2854 * @param hdma DMA handle.
2855 * @retval None
2857 static void SMARTCARD_DMARxOnlyAbortCallback(DMA_HandleTypeDef *hdma)
2859 SMARTCARD_HandleTypeDef *hsmartcard = (SMARTCARD_HandleTypeDef *)(hdma->Parent);
2861 hsmartcard->RxXferCount = 0U;
2863 /* Clear the Error flags in the ICR register */
2864 __HAL_SMARTCARD_CLEAR_FLAG(hsmartcard,
2865 SMARTCARD_CLEAR_OREF | SMARTCARD_CLEAR_NEF | SMARTCARD_CLEAR_PEF | SMARTCARD_CLEAR_FEF | SMARTCARD_CLEAR_RTOF |
2866 SMARTCARD_CLEAR_EOBF);
2868 /* Restore hsmartcard->RxState to Ready */
2869 hsmartcard->RxState = HAL_SMARTCARD_STATE_READY;
2871 #if (USE_HAL_SMARTCARD_REGISTER_CALLBACKS == 1)
2872 /* Call registered Abort Receive Complete Callback */
2873 hsmartcard->AbortReceiveCpltCallback(hsmartcard);
2874 #else
2875 /* Call legacy weak Abort Receive Complete Callback */
2876 HAL_SMARTCARD_AbortReceiveCpltCallback(hsmartcard);
2877 #endif /* USE_HAL_SMARTCARD_REGISTER_CALLBACK */
2881 * @brief Send an amount of data in non-blocking mode.
2882 * @note Function called under interruption only, once
2883 * interruptions have been enabled by HAL_SMARTCARD_Transmit_IT()
2884 * and when the FIFO mode is disabled.
2885 * @param hsmartcard Pointer to a SMARTCARD_HandleTypeDef structure that contains
2886 * the configuration information for the specified SMARTCARD module.
2887 * @retval None
2889 static void SMARTCARD_TxISR(SMARTCARD_HandleTypeDef *hsmartcard)
2891 /* Check that a Tx process is ongoing */
2892 if (hsmartcard->gState == HAL_SMARTCARD_STATE_BUSY_TX)
2894 if (hsmartcard->TxXferCount == 0U)
2896 /* Disable the SMARTCARD Transmit Data Register Empty Interrupt */
2897 CLEAR_BIT(hsmartcard->Instance->CR1, USART_CR1_TXEIE_TXFNFIE);
2899 /* Enable the SMARTCARD Transmit Complete Interrupt */
2900 __HAL_SMARTCARD_ENABLE_IT(hsmartcard, hsmartcard->AdvancedInit.TxCompletionIndication);
2902 else
2904 hsmartcard->Instance->TDR = (uint8_t)(*hsmartcard->pTxBuffPtr & 0xFFU);
2905 hsmartcard->pTxBuffPtr++;
2906 hsmartcard->TxXferCount--;
2912 * @brief Send an amount of data in non-blocking mode.
2913 * @note Function called under interruption only, once
2914 * interruptions have been enabled by HAL_SMARTCARD_Transmit_IT()
2915 * and when the FIFO mode is enabled.
2916 * @param hsmartcard Pointer to a SMARTCARD_HandleTypeDef structure that contains
2917 * the configuration information for the specified SMARTCARD module.
2918 * @retval None
2920 static void SMARTCARD_TxISR_FIFOEN(SMARTCARD_HandleTypeDef *hsmartcard)
2922 uint16_t nb_tx_data;
2924 /* Check that a Tx process is ongoing */
2925 if (hsmartcard->gState == HAL_SMARTCARD_STATE_BUSY_TX)
2927 for (nb_tx_data = hsmartcard->NbTxDataToProcess ; nb_tx_data > 0U ; nb_tx_data--)
2929 if (hsmartcard->TxXferCount == 0U)
2931 /* Disable the SMARTCARD Transmit Data Register Empty Interrupt */
2932 CLEAR_BIT(hsmartcard->Instance->CR1, USART_CR1_TXEIE_TXFNFIE);
2934 /* Enable the SMARTCARD Transmit Complete Interrupt */
2935 __HAL_SMARTCARD_ENABLE_IT(hsmartcard, hsmartcard->AdvancedInit.TxCompletionIndication);
2937 else if (READ_BIT(hsmartcard->Instance->ISR, USART_ISR_TXE_TXFNF) != 0U)
2939 hsmartcard->Instance->TDR = (uint8_t)(*hsmartcard->pTxBuffPtr & 0xFFU);
2940 hsmartcard->pTxBuffPtr++;
2941 hsmartcard->TxXferCount--;
2943 else
2945 /* Nothing to do */
2952 * @brief Wrap up transmission in non-blocking mode.
2953 * @param hsmartcard Pointer to a SMARTCARD_HandleTypeDef structure that contains
2954 * the configuration information for the specified SMARTCARD module.
2955 * @retval None
2957 static void SMARTCARD_EndTransmit_IT(SMARTCARD_HandleTypeDef *hsmartcard)
2959 /* Disable the SMARTCARD Transmit Complete Interrupt */
2960 __HAL_SMARTCARD_DISABLE_IT(hsmartcard, hsmartcard->AdvancedInit.TxCompletionIndication);
2962 /* Check if a receive process is ongoing or not. If not disable ERR IT */
2963 if (hsmartcard->RxState == HAL_SMARTCARD_STATE_READY)
2965 /* Disable the SMARTCARD Error Interrupt: (Frame error) */
2966 CLEAR_BIT(hsmartcard->Instance->CR3, USART_CR3_EIE);
2969 /* Re-enable Rx at end of transmission if initial mode is Rx/Tx */
2970 if (hsmartcard->Init.Mode == SMARTCARD_MODE_TX_RX)
2972 /* Disable the Peripheral first to update modes */
2973 CLEAR_BIT(hsmartcard->Instance->CR1, USART_CR1_UE);
2974 SET_BIT(hsmartcard->Instance->CR1, USART_CR1_RE);
2975 /* Enable the Peripheral */
2976 SET_BIT(hsmartcard->Instance->CR1, USART_CR1_UE);
2979 /* Tx process is ended, restore hsmartcard->gState to Ready */
2980 hsmartcard->gState = HAL_SMARTCARD_STATE_READY;
2982 /* Clear TxISR function pointer */
2983 hsmartcard->TxISR = NULL;
2985 #if (USE_HAL_SMARTCARD_REGISTER_CALLBACKS == 1)
2986 /* Call registered Tx complete callback */
2987 hsmartcard->TxCpltCallback(hsmartcard);
2988 #else
2989 /* Call legacy weak Tx complete callback */
2990 HAL_SMARTCARD_TxCpltCallback(hsmartcard);
2991 #endif /* USE_HAL_SMARTCARD_REGISTER_CALLBACK */
2995 * @brief Receive an amount of data in non-blocking mode.
2996 * @note Function called under interruption only, once
2997 * interruptions have been enabled by HAL_SMARTCARD_Receive_IT()
2998 * and when the FIFO mode is disabled.
2999 * @param hsmartcard Pointer to a SMARTCARD_HandleTypeDef structure that contains
3000 * the configuration information for the specified SMARTCARD module.
3001 * @retval None
3003 static void SMARTCARD_RxISR(SMARTCARD_HandleTypeDef *hsmartcard)
3005 /* Check that a Rx process is ongoing */
3006 if (hsmartcard->RxState == HAL_SMARTCARD_STATE_BUSY_RX)
3008 *hsmartcard->pRxBuffPtr = (uint8_t)(hsmartcard->Instance->RDR & (uint8_t)0xFF);
3009 hsmartcard->pRxBuffPtr++;
3011 hsmartcard->RxXferCount--;
3012 if (hsmartcard->RxXferCount == 0U)
3014 CLEAR_BIT(hsmartcard->Instance->CR1, USART_CR1_RXNEIE_RXFNEIE);
3016 /* Check if a transmit process is ongoing or not. If not disable ERR IT */
3017 if (hsmartcard->gState == HAL_SMARTCARD_STATE_READY)
3019 /* Disable the SMARTCARD Error Interrupt: (Frame error, noise error, overrun error) */
3020 CLEAR_BIT(hsmartcard->Instance->CR3, USART_CR3_EIE);
3023 /* Disable the SMARTCARD Parity Error Interrupt */
3024 CLEAR_BIT(hsmartcard->Instance->CR1, USART_CR1_PEIE);
3026 hsmartcard->RxState = HAL_SMARTCARD_STATE_READY;
3028 /* Clear RxISR function pointer */
3029 hsmartcard->RxISR = NULL;
3031 #if (USE_HAL_SMARTCARD_REGISTER_CALLBACKS == 1)
3032 /* Call registered Rx complete callback */
3033 hsmartcard->RxCpltCallback(hsmartcard);
3034 #else
3035 /* Call legacy weak Rx complete callback */
3036 HAL_SMARTCARD_RxCpltCallback(hsmartcard);
3037 #endif /* USE_HAL_SMARTCARD_REGISTER_CALLBACK */
3040 else
3042 /* Clear RXNE interrupt flag */
3043 __HAL_SMARTCARD_SEND_REQ(hsmartcard, SMARTCARD_RXDATA_FLUSH_REQUEST);
3048 * @brief Receive an amount of data in non-blocking mode.
3049 * @note Function called under interruption only, once
3050 * interruptions have been enabled by HAL_SMARTCARD_Receive_IT()
3051 * and when the FIFO mode is enabled.
3052 * @param hsmartcard Pointer to a SMARTCARD_HandleTypeDef structure that contains
3053 * the configuration information for the specified SMARTCARD module.
3054 * @retval None
3056 static void SMARTCARD_RxISR_FIFOEN(SMARTCARD_HandleTypeDef *hsmartcard)
3058 uint16_t nb_rx_data;
3059 uint16_t rxdatacount;
3061 /* Check that a Rx process is ongoing */
3062 if (hsmartcard->RxState == HAL_SMARTCARD_STATE_BUSY_RX)
3064 for (nb_rx_data = hsmartcard->NbRxDataToProcess ; nb_rx_data > 0U ; nb_rx_data--)
3066 *hsmartcard->pRxBuffPtr = (uint8_t)(hsmartcard->Instance->RDR & (uint8_t)0xFF);
3067 hsmartcard->pRxBuffPtr++;
3069 hsmartcard->RxXferCount--;
3070 if (hsmartcard->RxXferCount == 0U)
3072 CLEAR_BIT(hsmartcard->Instance->CR1, USART_CR1_RXNEIE_RXFNEIE);
3074 /* Check if a transmit process is ongoing or not. If not disable ERR IT */
3075 if (hsmartcard->gState == HAL_SMARTCARD_STATE_READY)
3077 /* Disable the SMARTCARD Error Interrupt: (Frame error, noise error, overrun error) */
3078 CLEAR_BIT(hsmartcard->Instance->CR3, USART_CR3_EIE);
3081 /* Disable the SMARTCARD Parity Error Interrupt */
3082 CLEAR_BIT(hsmartcard->Instance->CR1, USART_CR1_PEIE);
3084 hsmartcard->RxState = HAL_SMARTCARD_STATE_READY;
3086 /* Clear RxISR function pointer */
3087 hsmartcard->RxISR = NULL;
3089 #if (USE_HAL_SMARTCARD_REGISTER_CALLBACKS == 1)
3090 /* Call registered Rx complete callback */
3091 hsmartcard->RxCpltCallback(hsmartcard);
3092 #else
3093 /* Call legacy weak Rx complete callback */
3094 HAL_SMARTCARD_RxCpltCallback(hsmartcard);
3095 #endif /* USE_HAL_SMARTCARD_REGISTER_CALLBACK */
3099 /* When remaining number of bytes to receive is less than the RX FIFO
3100 threshold, next incoming frames are processed as if FIFO mode was
3101 disabled (i.e. one interrupt per received frame).
3103 rxdatacount = hsmartcard->RxXferCount;
3104 if (((rxdatacount != 0U)) && (rxdatacount < hsmartcard->NbRxDataToProcess))
3106 /* Disable the UART RXFT interrupt*/
3107 CLEAR_BIT(hsmartcard->Instance->CR3, USART_CR3_RXFTIE);
3109 /* Update the RxISR function pointer */
3110 hsmartcard->RxISR = SMARTCARD_RxISR;
3112 /* Enable the UART Data Register Not Empty interrupt */
3113 SET_BIT(hsmartcard->Instance->CR1, USART_CR1_RXNEIE_RXFNEIE);
3116 else
3118 /* Clear RXNE interrupt flag */
3119 __HAL_SMARTCARD_SEND_REQ(hsmartcard, SMARTCARD_RXDATA_FLUSH_REQUEST);
3124 * @}
3127 #endif /* HAL_SMARTCARD_MODULE_ENABLED */
3129 * @}
3133 * @}
3136 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/