FIX: Flash page size check is STM (or clone) specific (#14130)
[betaflight.git] / lib / main / STM32H7 / Drivers / STM32H7xx_HAL_Driver / Src / stm32h7xx_hal_smartcard.c
blob2440595f458dfe5c54332f20e56f2a446b8efbdf
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 HAL_SMARTCARD_RegisterCallback() to register a user callback.
111 Function HAL_SMARTCARD_RegisterCallback() allows to register following callbacks:
112 (+) TxCpltCallback : Tx Complete Callback.
113 (+) RxCpltCallback : Rx Complete Callback.
114 (+) ErrorCallback : Error Callback.
115 (+) AbortCpltCallback : Abort Complete Callback.
116 (+) AbortTransmitCpltCallback : Abort Transmit Complete Callback.
117 (+) AbortReceiveCpltCallback : Abort Receive Complete Callback.
118 (+) RxFifoFullCallback : Rx Fifo Full Callback.
119 (+) TxFifoEmptyCallback : Tx Fifo Empty Callback.
120 (+) MspInitCallback : SMARTCARD MspInit.
121 (+) MspDeInitCallback : SMARTCARD MspDeInit.
122 This function takes as parameters the HAL peripheral handle, the Callback ID
123 and a pointer to the user callback function.
125 [..]
126 Use function HAL_SMARTCARD_UnRegisterCallback() to reset a callback to the default
127 weak (surcharged) function.
128 HAL_SMARTCARD_UnRegisterCallback() takes as parameters the HAL peripheral handle,
129 and the Callback ID.
130 This function allows to reset following callbacks:
131 (+) TxCpltCallback : Tx Complete Callback.
132 (+) RxCpltCallback : Rx Complete Callback.
133 (+) ErrorCallback : Error Callback.
134 (+) AbortCpltCallback : Abort Complete Callback.
135 (+) AbortTransmitCpltCallback : Abort Transmit Complete Callback.
136 (+) AbortReceiveCpltCallback : Abort Receive Complete Callback.
137 (+) RxFifoFullCallback : Rx Fifo Full Callback.
138 (+) TxFifoEmptyCallback : Tx Fifo Empty Callback.
139 (+) MspInitCallback : SMARTCARD MspInit.
140 (+) MspDeInitCallback : SMARTCARD MspDeInit.
142 [..]
143 By default, after the HAL_SMARTCARD_Init() and when the state is HAL_SMARTCARD_STATE_RESET
144 all callbacks are set to the corresponding weak (surcharged) functions:
145 examples HAL_SMARTCARD_TxCpltCallback(), HAL_SMARTCARD_RxCpltCallback().
146 Exception done for MspInit and MspDeInit functions that are respectively
147 reset to the legacy weak (surcharged) functions in the HAL_SMARTCARD_Init()
148 and HAL_SMARTCARD_DeInit() only when these callbacks are null (not registered beforehand).
149 If not, MspInit or MspDeInit are not null, the HAL_SMARTCARD_Init() and HAL_SMARTCARD_DeInit()
150 keep and use the user MspInit/MspDeInit callbacks (registered beforehand).
152 [..]
153 Callbacks can be registered/unregistered in HAL_SMARTCARD_STATE_READY state only.
154 Exception done MspInit/MspDeInit that can be registered/unregistered
155 in HAL_SMARTCARD_STATE_READY or HAL_SMARTCARD_STATE_RESET state, thus registered (user)
156 MspInit/DeInit callbacks can be used during the Init/DeInit.
157 In that case first register the MspInit/MspDeInit user callbacks
158 using HAL_SMARTCARD_RegisterCallback() before calling HAL_SMARTCARD_DeInit()
159 or HAL_SMARTCARD_Init() function.
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 /* In case of TX only mode, if NACK is enabled, the USART must be able to monitor
828 the bidirectional line to detect a NACK signal in case of parity error.
829 Therefore, the receiver block must be enabled as well (RE bit must be set). */
830 if ((hsmartcard->Init.Mode == SMARTCARD_MODE_TX)
831 && (hsmartcard->Init.NACKEnable == SMARTCARD_NACK_ENABLE))
833 SET_BIT(hsmartcard->Instance->CR1, USART_CR1_RE);
835 /* Enable Tx */
836 SET_BIT(hsmartcard->Instance->CR1, USART_CR1_TE);
838 /* Enable the Peripheral */
839 SET_BIT(hsmartcard->Instance->CR1, USART_CR1_UE);
841 /* Perform a TX/RX FIFO Flush */
842 __HAL_SMARTCARD_FLUSH_DRREGISTER(hsmartcard);
844 hsmartcard->ErrorCode = HAL_SMARTCARD_ERROR_NONE;
845 hsmartcard->TxXferSize = Size;
846 hsmartcard->TxXferCount = Size;
848 while (hsmartcard->TxXferCount > 0U)
850 hsmartcard->TxXferCount--;
851 if (SMARTCARD_WaitOnFlagUntilTimeout(hsmartcard, SMARTCARD_FLAG_TXE, RESET, tickstart, Timeout) != HAL_OK)
853 return HAL_TIMEOUT;
855 hsmartcard->Instance->TDR = (uint8_t)(*ptmpdata & 0xFFU);
856 ptmpdata++;
858 if (SMARTCARD_WaitOnFlagUntilTimeout(hsmartcard, SMARTCARD_TRANSMISSION_COMPLETION_FLAG(hsmartcard), RESET, tickstart,
859 Timeout) != HAL_OK)
861 return HAL_TIMEOUT;
864 /* Disable the Peripheral first to update mode */
865 CLEAR_BIT(hsmartcard->Instance->CR1, USART_CR1_UE);
866 if ((hsmartcard->Init.Mode == SMARTCARD_MODE_TX)
867 && (hsmartcard->Init.NACKEnable == SMARTCARD_NACK_ENABLE))
869 /* In case of TX only mode, if NACK is enabled, receiver block has been enabled
870 for Transmit phase. Disable this receiver block. */
871 CLEAR_BIT(hsmartcard->Instance->CR1, USART_CR1_RE);
873 if ((hsmartcard->Init.Mode == SMARTCARD_MODE_TX_RX)
874 || (hsmartcard->Init.NACKEnable == SMARTCARD_NACK_ENABLE))
876 /* Perform a TX FIFO Flush at end of Tx phase, as all sent bytes are appearing in Rx Data register */
877 __HAL_SMARTCARD_FLUSH_DRREGISTER(hsmartcard);
879 SET_BIT(hsmartcard->Instance->CR1, USART_CR1_UE);
881 /* At end of Tx process, restore hsmartcard->gState to Ready */
882 hsmartcard->gState = HAL_SMARTCARD_STATE_READY;
884 /* Process Unlocked */
885 __HAL_UNLOCK(hsmartcard);
887 return HAL_OK;
889 else
891 return HAL_BUSY;
896 * @brief Receive an amount of data in blocking mode.
897 * @note When FIFO mode is enabled, the RXFNE flag is set as long as the RXFIFO
898 * is not empty. Read operations from the RDR register are performed when
899 * RXFNE flag is set. From hardware perspective, RXFNE flag and
900 * RXNE are mapped on the same bit-field.
901 * @param hsmartcard Pointer to a SMARTCARD_HandleTypeDef structure that contains
902 * the configuration information for the specified SMARTCARD module.
903 * @param pData pointer to data buffer.
904 * @param Size amount of data to be received.
905 * @param Timeout Timeout duration.
906 * @retval HAL status
908 HAL_StatusTypeDef HAL_SMARTCARD_Receive(SMARTCARD_HandleTypeDef *hsmartcard, uint8_t *pData, uint16_t Size,
909 uint32_t Timeout)
911 uint32_t tickstart;
912 uint8_t *ptmpdata = pData;
914 /* Check that a Rx process is not already ongoing */
915 if (hsmartcard->RxState == HAL_SMARTCARD_STATE_READY)
917 if ((ptmpdata == NULL) || (Size == 0U))
919 return HAL_ERROR;
922 /* Process Locked */
923 __HAL_LOCK(hsmartcard);
925 hsmartcard->ErrorCode = HAL_SMARTCARD_ERROR_NONE;
926 hsmartcard->RxState = HAL_SMARTCARD_STATE_BUSY_RX;
928 /* Init tickstart for timeout management */
929 tickstart = HAL_GetTick();
931 hsmartcard->RxXferSize = Size;
932 hsmartcard->RxXferCount = Size;
934 /* Check the remain data to be received */
935 while (hsmartcard->RxXferCount > 0U)
937 hsmartcard->RxXferCount--;
939 if (SMARTCARD_WaitOnFlagUntilTimeout(hsmartcard, SMARTCARD_FLAG_RXNE, RESET, tickstart, Timeout) != HAL_OK)
941 return HAL_TIMEOUT;
943 *ptmpdata = (uint8_t)(hsmartcard->Instance->RDR & (uint8_t)0x00FF);
944 ptmpdata++;
947 /* At end of Rx process, restore hsmartcard->RxState to Ready */
948 hsmartcard->RxState = HAL_SMARTCARD_STATE_READY;
950 /* Process Unlocked */
951 __HAL_UNLOCK(hsmartcard);
953 return HAL_OK;
955 else
957 return HAL_BUSY;
962 * @brief Send an amount of data in interrupt mode.
963 * @note When FIFO mode is disabled, USART interrupt is generated whenever
964 * USART_TDR register is empty, i.e one interrupt per data to transmit.
965 * @note When FIFO mode is enabled, USART interrupt is generated whenever
966 * TXFIFO threshold reached. In that case the interrupt rate depends on
967 * TXFIFO threshold configuration.
968 * @note This function sets the hsmartcard->TxIsr function pointer according to
969 * the FIFO mode (data transmission processing depends on FIFO mode).
970 * @param hsmartcard Pointer to a SMARTCARD_HandleTypeDef structure that contains
971 * the configuration information for the specified SMARTCARD module.
972 * @param pData pointer to data buffer.
973 * @param Size amount of data to be sent.
974 * @retval HAL status
976 HAL_StatusTypeDef HAL_SMARTCARD_Transmit_IT(SMARTCARD_HandleTypeDef *hsmartcard, uint8_t *pData, uint16_t Size)
978 /* Check that a Tx process is not already ongoing */
979 if (hsmartcard->gState == HAL_SMARTCARD_STATE_READY)
981 if ((pData == NULL) || (Size == 0U))
983 return HAL_ERROR;
986 /* Process Locked */
987 __HAL_LOCK(hsmartcard);
989 hsmartcard->ErrorCode = HAL_SMARTCARD_ERROR_NONE;
990 hsmartcard->gState = HAL_SMARTCARD_STATE_BUSY_TX;
992 hsmartcard->pTxBuffPtr = pData;
993 hsmartcard->TxXferSize = Size;
994 hsmartcard->TxXferCount = Size;
995 hsmartcard->TxISR = NULL;
997 /* Disable the Peripheral first to update mode for TX master */
998 CLEAR_BIT(hsmartcard->Instance->CR1, USART_CR1_UE);
1000 /* In case of TX only mode, if NACK is enabled, the USART must be able to monitor
1001 the bidirectional line to detect a NACK signal in case of parity error.
1002 Therefore, the receiver block must be enabled as well (RE bit must be set). */
1003 if ((hsmartcard->Init.Mode == SMARTCARD_MODE_TX)
1004 && (hsmartcard->Init.NACKEnable == SMARTCARD_NACK_ENABLE))
1006 SET_BIT(hsmartcard->Instance->CR1, USART_CR1_RE);
1008 /* Enable Tx */
1009 SET_BIT(hsmartcard->Instance->CR1, USART_CR1_TE);
1011 /* Enable the Peripheral */
1012 SET_BIT(hsmartcard->Instance->CR1, USART_CR1_UE);
1014 /* Perform a TX/RX FIFO Flush */
1015 __HAL_SMARTCARD_FLUSH_DRREGISTER(hsmartcard);
1017 /* Configure Tx interrupt processing */
1018 if (hsmartcard->FifoMode == SMARTCARD_FIFOMODE_ENABLE)
1020 /* Set the Tx ISR function pointer */
1021 hsmartcard->TxISR = SMARTCARD_TxISR_FIFOEN;
1023 /* Process Unlocked */
1024 __HAL_UNLOCK(hsmartcard);
1026 /* Enable the SMARTCARD Error Interrupt: (Frame error) */
1027 SET_BIT(hsmartcard->Instance->CR3, USART_CR3_EIE);
1029 /* Enable the TX FIFO threshold interrupt */
1030 SET_BIT(hsmartcard->Instance->CR3, USART_CR3_TXFTIE);
1032 else
1034 /* Set the Tx ISR function pointer */
1035 hsmartcard->TxISR = SMARTCARD_TxISR;
1037 /* Process Unlocked */
1038 __HAL_UNLOCK(hsmartcard);
1040 /* Enable the SMARTCARD Error Interrupt: (Frame error) */
1041 SET_BIT(hsmartcard->Instance->CR3, USART_CR3_EIE);
1043 /* Enable the SMARTCARD Transmit Data Register Empty Interrupt */
1044 SET_BIT(hsmartcard->Instance->CR1, USART_CR1_TXEIE_TXFNFIE);
1047 return HAL_OK;
1049 else
1051 return HAL_BUSY;
1056 * @brief Receive an amount of data in interrupt mode.
1057 * @note When FIFO mode is disabled, USART interrupt is generated whenever
1058 * USART_RDR register can be read, i.e one interrupt per data to receive.
1059 * @note When FIFO mode is enabled, USART interrupt is generated whenever
1060 * RXFIFO threshold reached. In that case the interrupt rate depends on
1061 * RXFIFO threshold configuration.
1062 * @note This function sets the hsmartcard->RxIsr function pointer according to
1063 * the FIFO mode (data reception processing depends on FIFO mode).
1064 * @param hsmartcard Pointer to a SMARTCARD_HandleTypeDef structure that contains
1065 * the configuration information for the specified SMARTCARD module.
1066 * @param pData pointer to data buffer.
1067 * @param Size amount of data to be received.
1068 * @retval HAL status
1070 HAL_StatusTypeDef HAL_SMARTCARD_Receive_IT(SMARTCARD_HandleTypeDef *hsmartcard, uint8_t *pData, uint16_t Size)
1072 /* Check that a Rx process is not already ongoing */
1073 if (hsmartcard->RxState == HAL_SMARTCARD_STATE_READY)
1075 if ((pData == NULL) || (Size == 0U))
1077 return HAL_ERROR;
1080 /* Process Locked */
1081 __HAL_LOCK(hsmartcard);
1083 hsmartcard->ErrorCode = HAL_SMARTCARD_ERROR_NONE;
1084 hsmartcard->RxState = HAL_SMARTCARD_STATE_BUSY_RX;
1086 hsmartcard->pRxBuffPtr = pData;
1087 hsmartcard->RxXferSize = Size;
1088 hsmartcard->RxXferCount = Size;
1090 /* Configure Rx interrupt processing */
1091 if ((hsmartcard->FifoMode == SMARTCARD_FIFOMODE_ENABLE) && (Size >= hsmartcard->NbRxDataToProcess))
1093 /* Set the Rx ISR function pointer */
1094 hsmartcard->RxISR = SMARTCARD_RxISR_FIFOEN;
1096 /* Process Unlocked */
1097 __HAL_UNLOCK(hsmartcard);
1099 /* Enable the SMARTCART Parity Error interrupt and RX FIFO Threshold interrupt */
1100 SET_BIT(hsmartcard->Instance->CR1, USART_CR1_PEIE);
1101 SET_BIT(hsmartcard->Instance->CR3, USART_CR3_RXFTIE);
1103 else
1105 /* Set the Rx ISR function pointer */
1106 hsmartcard->RxISR = SMARTCARD_RxISR;
1108 /* Process Unlocked */
1109 __HAL_UNLOCK(hsmartcard);
1111 /* Enable the SMARTCARD Parity Error and Data Register not empty Interrupts */
1112 SET_BIT(hsmartcard->Instance->CR1, USART_CR1_PEIE | USART_CR1_RXNEIE_RXFNEIE);
1115 /* Enable the SMARTCARD Error Interrupt: (Frame error, noise error, overrun error) */
1116 SET_BIT(hsmartcard->Instance->CR3, USART_CR3_EIE);
1118 return HAL_OK;
1120 else
1122 return HAL_BUSY;
1127 * @brief Send an amount of data in DMA mode.
1128 * @param hsmartcard Pointer to a SMARTCARD_HandleTypeDef structure that contains
1129 * the configuration information for the specified SMARTCARD module.
1130 * @param pData pointer to data buffer.
1131 * @param Size amount of data to be sent.
1132 * @retval HAL status
1134 HAL_StatusTypeDef HAL_SMARTCARD_Transmit_DMA(SMARTCARD_HandleTypeDef *hsmartcard, uint8_t *pData, uint16_t Size)
1136 /* Check that a Tx process is not already ongoing */
1137 if (hsmartcard->gState == HAL_SMARTCARD_STATE_READY)
1139 if ((pData == NULL) || (Size == 0U))
1141 return HAL_ERROR;
1144 /* Process Locked */
1145 __HAL_LOCK(hsmartcard);
1147 hsmartcard->gState = HAL_SMARTCARD_STATE_BUSY_TX;
1149 hsmartcard->ErrorCode = HAL_SMARTCARD_ERROR_NONE;
1150 hsmartcard->pTxBuffPtr = pData;
1151 hsmartcard->TxXferSize = Size;
1152 hsmartcard->TxXferCount = Size;
1154 /* Disable the Peripheral first to update mode for TX master */
1155 CLEAR_BIT(hsmartcard->Instance->CR1, USART_CR1_UE);
1157 /* In case of TX only mode, if NACK is enabled, the USART must be able to monitor
1158 the bidirectional line to detect a NACK signal in case of parity error.
1159 Therefore, the receiver block must be enabled as well (RE bit must be set). */
1160 if ((hsmartcard->Init.Mode == SMARTCARD_MODE_TX)
1161 && (hsmartcard->Init.NACKEnable == SMARTCARD_NACK_ENABLE))
1163 SET_BIT(hsmartcard->Instance->CR1, USART_CR1_RE);
1165 /* Enable Tx */
1166 SET_BIT(hsmartcard->Instance->CR1, USART_CR1_TE);
1168 /* Enable the Peripheral */
1169 SET_BIT(hsmartcard->Instance->CR1, USART_CR1_UE);
1171 /* Perform a TX/RX FIFO Flush */
1172 __HAL_SMARTCARD_FLUSH_DRREGISTER(hsmartcard);
1174 /* Set the SMARTCARD DMA transfer complete callback */
1175 hsmartcard->hdmatx->XferCpltCallback = SMARTCARD_DMATransmitCplt;
1177 /* Set the SMARTCARD error callback */
1178 hsmartcard->hdmatx->XferErrorCallback = SMARTCARD_DMAError;
1180 /* Set the DMA abort callback */
1181 hsmartcard->hdmatx->XferAbortCallback = NULL;
1183 /* Enable the SMARTCARD transmit DMA channel */
1184 if (HAL_DMA_Start_IT(hsmartcard->hdmatx, (uint32_t)hsmartcard->pTxBuffPtr, (uint32_t)&hsmartcard->Instance->TDR,
1185 Size) == HAL_OK)
1187 /* Clear the TC flag in the ICR register */
1188 CLEAR_BIT(hsmartcard->Instance->ICR, USART_ICR_TCCF);
1190 /* Process Unlocked */
1191 __HAL_UNLOCK(hsmartcard);
1193 /* Enable the UART Error Interrupt: (Frame error) */
1194 SET_BIT(hsmartcard->Instance->CR3, USART_CR3_EIE);
1196 /* Enable the DMA transfer for transmit request by setting the DMAT bit
1197 in the SMARTCARD associated USART CR3 register */
1198 SET_BIT(hsmartcard->Instance->CR3, USART_CR3_DMAT);
1200 return HAL_OK;
1202 else
1204 /* Set error code to DMA */
1205 hsmartcard->ErrorCode = HAL_SMARTCARD_ERROR_DMA;
1207 /* Process Unlocked */
1208 __HAL_UNLOCK(hsmartcard);
1210 /* Restore hsmartcard->State to ready */
1211 hsmartcard->gState = HAL_SMARTCARD_STATE_READY;
1213 return HAL_ERROR;
1216 else
1218 return HAL_BUSY;
1223 * @brief Receive an amount of data in DMA mode.
1224 * @param hsmartcard Pointer to a SMARTCARD_HandleTypeDef structure that contains
1225 * the configuration information for the specified SMARTCARD module.
1226 * @param pData pointer to data buffer.
1227 * @param Size amount of data to be received.
1228 * @note The SMARTCARD-associated USART parity is enabled (PCE = 1),
1229 * the received data contain the parity bit (MSB position).
1230 * @retval HAL status
1232 HAL_StatusTypeDef HAL_SMARTCARD_Receive_DMA(SMARTCARD_HandleTypeDef *hsmartcard, uint8_t *pData, uint16_t Size)
1234 /* Check that a Rx process is not already ongoing */
1235 if (hsmartcard->RxState == HAL_SMARTCARD_STATE_READY)
1237 if ((pData == NULL) || (Size == 0U))
1239 return HAL_ERROR;
1242 /* Process Locked */
1243 __HAL_LOCK(hsmartcard);
1245 hsmartcard->ErrorCode = HAL_SMARTCARD_ERROR_NONE;
1246 hsmartcard->RxState = HAL_SMARTCARD_STATE_BUSY_RX;
1248 hsmartcard->pRxBuffPtr = pData;
1249 hsmartcard->RxXferSize = Size;
1251 /* Set the SMARTCARD DMA transfer complete callback */
1252 hsmartcard->hdmarx->XferCpltCallback = SMARTCARD_DMAReceiveCplt;
1254 /* Set the SMARTCARD DMA error callback */
1255 hsmartcard->hdmarx->XferErrorCallback = SMARTCARD_DMAError;
1257 /* Set the DMA abort callback */
1258 hsmartcard->hdmarx->XferAbortCallback = NULL;
1260 /* Enable the DMA channel */
1261 if (HAL_DMA_Start_IT(hsmartcard->hdmarx, (uint32_t)&hsmartcard->Instance->RDR, (uint32_t)hsmartcard->pRxBuffPtr,
1262 Size) == HAL_OK)
1264 /* Process Unlocked */
1265 __HAL_UNLOCK(hsmartcard);
1267 /* Enable the SMARTCARD Parity Error Interrupt */
1268 SET_BIT(hsmartcard->Instance->CR1, USART_CR1_PEIE);
1270 /* Enable the SMARTCARD Error Interrupt: (Frame error, noise error, overrun error) */
1271 SET_BIT(hsmartcard->Instance->CR3, USART_CR3_EIE);
1273 /* Enable the DMA transfer for the receiver request by setting the DMAR bit
1274 in the SMARTCARD associated USART CR3 register */
1275 SET_BIT(hsmartcard->Instance->CR3, USART_CR3_DMAR);
1277 return HAL_OK;
1279 else
1281 /* Set error code to DMA */
1282 hsmartcard->ErrorCode = HAL_SMARTCARD_ERROR_DMA;
1284 /* Process Unlocked */
1285 __HAL_UNLOCK(hsmartcard);
1287 /* Restore hsmartcard->State to ready */
1288 hsmartcard->RxState = HAL_SMARTCARD_STATE_READY;
1290 return HAL_ERROR;
1293 else
1295 return HAL_BUSY;
1300 * @brief Abort ongoing transfers (blocking mode).
1301 * @param hsmartcard Pointer to a SMARTCARD_HandleTypeDef structure that contains
1302 * the configuration information for the specified SMARTCARD module.
1303 * @note This procedure could be used for aborting any ongoing transfer started in Interrupt or DMA mode.
1304 * This procedure performs following operations :
1305 * - Disable SMARTCARD Interrupts (Tx and Rx)
1306 * - Disable the DMA transfer in the peripheral register (if enabled)
1307 * - Abort DMA transfer by calling HAL_DMA_Abort (in case of transfer in DMA mode)
1308 * - Set handle State to READY
1309 * @note This procedure is executed in blocking mode : when exiting function, Abort is considered as completed.
1310 * @retval HAL status
1312 HAL_StatusTypeDef HAL_SMARTCARD_Abort(SMARTCARD_HandleTypeDef *hsmartcard)
1314 /* Disable RTOIE, EOBIE, TXEIE, TCIE, RXNE, PE, RXFT, TXFT and ERR (Frame error, noise error, overrun error) interrupts */
1315 CLEAR_BIT(hsmartcard->Instance->CR1,
1316 (USART_CR1_RXNEIE_RXFNEIE | USART_CR1_PEIE | USART_CR1_TXEIE_TXFNFIE | USART_CR1_TCIE | USART_CR1_RTOIE |
1317 USART_CR1_EOBIE));
1318 CLEAR_BIT(hsmartcard->Instance->CR3, (USART_CR3_EIE | USART_CR3_RXFTIE | USART_CR3_TXFTIE));
1320 /* Disable the SMARTCARD DMA Tx request if enabled */
1321 if (HAL_IS_BIT_SET(hsmartcard->Instance->CR3, USART_CR3_DMAT))
1323 CLEAR_BIT(hsmartcard->Instance->CR3, USART_CR3_DMAT);
1325 /* Abort the SMARTCARD DMA Tx channel : use blocking DMA Abort API (no callback) */
1326 if (hsmartcard->hdmatx != NULL)
1328 /* Set the SMARTCARD DMA Abort callback to Null.
1329 No call back execution at end of DMA abort procedure */
1330 hsmartcard->hdmatx->XferAbortCallback = NULL;
1332 if (HAL_DMA_Abort(hsmartcard->hdmatx) != HAL_OK)
1334 if (HAL_DMA_GetError(hsmartcard->hdmatx) == HAL_DMA_ERROR_TIMEOUT)
1336 /* Set error code to DMA */
1337 hsmartcard->ErrorCode = HAL_SMARTCARD_ERROR_DMA;
1339 return HAL_TIMEOUT;
1345 /* Disable the SMARTCARD DMA Rx request if enabled */
1346 if (HAL_IS_BIT_SET(hsmartcard->Instance->CR3, USART_CR3_DMAR))
1348 CLEAR_BIT(hsmartcard->Instance->CR3, USART_CR3_DMAR);
1350 /* Abort the SMARTCARD DMA Rx channel : use blocking DMA Abort API (no callback) */
1351 if (hsmartcard->hdmarx != NULL)
1353 /* Set the SMARTCARD DMA Abort callback to Null.
1354 No call back execution at end of DMA abort procedure */
1355 hsmartcard->hdmarx->XferAbortCallback = NULL;
1357 if (HAL_DMA_Abort(hsmartcard->hdmarx) != HAL_OK)
1359 if (HAL_DMA_GetError(hsmartcard->hdmarx) == HAL_DMA_ERROR_TIMEOUT)
1361 /* Set error code to DMA */
1362 hsmartcard->ErrorCode = HAL_SMARTCARD_ERROR_DMA;
1364 return HAL_TIMEOUT;
1370 /* Reset Tx and Rx transfer counters */
1371 hsmartcard->TxXferCount = 0U;
1372 hsmartcard->RxXferCount = 0U;
1374 /* Clear the Error flags in the ICR register */
1375 __HAL_SMARTCARD_CLEAR_FLAG(hsmartcard,
1376 SMARTCARD_CLEAR_OREF | SMARTCARD_CLEAR_NEF | SMARTCARD_CLEAR_PEF | SMARTCARD_CLEAR_FEF | SMARTCARD_CLEAR_RTOF |
1377 SMARTCARD_CLEAR_EOBF);
1379 /* Restore hsmartcard->gState and hsmartcard->RxState to Ready */
1380 hsmartcard->gState = HAL_SMARTCARD_STATE_READY;
1381 hsmartcard->RxState = HAL_SMARTCARD_STATE_READY;
1383 /* Reset Handle ErrorCode to No Error */
1384 hsmartcard->ErrorCode = HAL_SMARTCARD_ERROR_NONE;
1386 return HAL_OK;
1390 * @brief Abort ongoing Transmit transfer (blocking mode).
1391 * @param hsmartcard Pointer to a SMARTCARD_HandleTypeDef structure that contains
1392 * the configuration information for the specified SMARTCARD module.
1393 * @note This procedure could be used for aborting any ongoing Tx transfer started in Interrupt or DMA mode.
1394 * This procedure performs following operations :
1395 * - Disable SMARTCARD Interrupts (Tx)
1396 * - Disable the DMA transfer in the peripheral register (if enabled)
1397 * - Abort DMA transfer by calling HAL_DMA_Abort (in case of transfer in DMA mode)
1398 * - Set handle State to READY
1399 * @note This procedure is executed in blocking mode : when exiting function, Abort is considered as completed.
1400 * @retval HAL status
1402 HAL_StatusTypeDef HAL_SMARTCARD_AbortTransmit(SMARTCARD_HandleTypeDef *hsmartcard)
1404 /* Disable TCIE, TXEIE and TXFTIE interrupts */
1405 CLEAR_BIT(hsmartcard->Instance->CR1, (USART_CR1_TXEIE_TXFNFIE | USART_CR1_TCIE));
1406 CLEAR_BIT(hsmartcard->Instance->CR3, USART_CR3_TXFTIE);
1408 /* Check if a receive process is ongoing or not. If not disable ERR IT */
1409 if (hsmartcard->RxState == HAL_SMARTCARD_STATE_READY)
1411 /* Disable the SMARTCARD Error Interrupt: (Frame error) */
1412 CLEAR_BIT(hsmartcard->Instance->CR3, USART_CR3_EIE);
1415 /* Disable the SMARTCARD DMA Tx request if enabled */
1416 if (HAL_IS_BIT_SET(hsmartcard->Instance->CR3, USART_CR3_DMAT))
1418 CLEAR_BIT(hsmartcard->Instance->CR3, USART_CR3_DMAT);
1420 /* Abort the SMARTCARD DMA Tx channel : use blocking DMA Abort API (no callback) */
1421 if (hsmartcard->hdmatx != NULL)
1423 /* Set the SMARTCARD DMA Abort callback to Null.
1424 No call back execution at end of DMA abort procedure */
1425 hsmartcard->hdmatx->XferAbortCallback = NULL;
1427 if (HAL_DMA_Abort(hsmartcard->hdmatx) != HAL_OK)
1429 if (HAL_DMA_GetError(hsmartcard->hdmatx) == HAL_DMA_ERROR_TIMEOUT)
1431 /* Set error code to DMA */
1432 hsmartcard->ErrorCode = HAL_SMARTCARD_ERROR_DMA;
1434 return HAL_TIMEOUT;
1440 /* Reset Tx transfer counter */
1441 hsmartcard->TxXferCount = 0U;
1443 /* Clear the Error flags in the ICR register */
1444 __HAL_SMARTCARD_CLEAR_FLAG(hsmartcard, SMARTCARD_CLEAR_FEF);
1446 /* Restore hsmartcard->gState to Ready */
1447 hsmartcard->gState = HAL_SMARTCARD_STATE_READY;
1449 return HAL_OK;
1453 * @brief Abort ongoing Receive transfer (blocking mode).
1454 * @param hsmartcard Pointer to a SMARTCARD_HandleTypeDef structure that contains
1455 * the configuration information for the specified SMARTCARD module.
1456 * @note This procedure could be used for aborting any ongoing Rx transfer started in Interrupt or DMA mode.
1457 * This procedure performs following operations :
1458 * - Disable SMARTCARD Interrupts (Rx)
1459 * - Disable the DMA transfer in the peripheral register (if enabled)
1460 * - Abort DMA transfer by calling HAL_DMA_Abort (in case of transfer in DMA mode)
1461 * - Set handle State to READY
1462 * @note This procedure is executed in blocking mode : when exiting function, Abort is considered as completed.
1463 * @retval HAL status
1465 HAL_StatusTypeDef HAL_SMARTCARD_AbortReceive(SMARTCARD_HandleTypeDef *hsmartcard)
1467 /* Disable RTOIE, EOBIE, RXNE, PE, RXFT, TXFT and ERR (Frame error, noise error, overrun error) interrupts */
1468 CLEAR_BIT(hsmartcard->Instance->CR1, (USART_CR1_RXNEIE_RXFNEIE | USART_CR1_PEIE | USART_CR1_RTOIE | USART_CR1_EOBIE));
1469 CLEAR_BIT(hsmartcard->Instance->CR3, (USART_CR3_EIE | USART_CR3_RXFTIE));
1471 /* Check if a Transmit process is ongoing or not. If not disable ERR IT */
1472 if (hsmartcard->gState == HAL_SMARTCARD_STATE_READY)
1474 /* Disable the SMARTCARD Error Interrupt: (Frame error) */
1475 CLEAR_BIT(hsmartcard->Instance->CR3, USART_CR3_EIE);
1478 /* Disable the SMARTCARD DMA Rx request if enabled */
1479 if (HAL_IS_BIT_SET(hsmartcard->Instance->CR3, USART_CR3_DMAR))
1481 CLEAR_BIT(hsmartcard->Instance->CR3, USART_CR3_DMAR);
1483 /* Abort the SMARTCARD DMA Rx channel : use blocking DMA Abort API (no callback) */
1484 if (hsmartcard->hdmarx != NULL)
1486 /* Set the SMARTCARD DMA Abort callback to Null.
1487 No call back execution at end of DMA abort procedure */
1488 hsmartcard->hdmarx->XferAbortCallback = NULL;
1490 if (HAL_DMA_Abort(hsmartcard->hdmarx) != HAL_OK)
1492 if (HAL_DMA_GetError(hsmartcard->hdmarx) == HAL_DMA_ERROR_TIMEOUT)
1494 /* Set error code to DMA */
1495 hsmartcard->ErrorCode = HAL_SMARTCARD_ERROR_DMA;
1497 return HAL_TIMEOUT;
1503 /* Reset Rx transfer counter */
1504 hsmartcard->RxXferCount = 0U;
1506 /* Clear the Error flags in the ICR register */
1507 __HAL_SMARTCARD_CLEAR_FLAG(hsmartcard,
1508 SMARTCARD_CLEAR_OREF | SMARTCARD_CLEAR_NEF | SMARTCARD_CLEAR_PEF | SMARTCARD_CLEAR_FEF | SMARTCARD_CLEAR_RTOF |
1509 SMARTCARD_CLEAR_EOBF);
1511 /* Restore hsmartcard->RxState to Ready */
1512 hsmartcard->RxState = HAL_SMARTCARD_STATE_READY;
1514 return HAL_OK;
1518 * @brief Abort ongoing transfers (Interrupt mode).
1519 * @param hsmartcard Pointer to a SMARTCARD_HandleTypeDef structure that contains
1520 * the configuration information for the specified SMARTCARD module.
1521 * @note This procedure could be used for aborting any ongoing transfer started in Interrupt or DMA mode.
1522 * This procedure performs following operations :
1523 * - Disable SMARTCARD Interrupts (Tx and Rx)
1524 * - Disable the DMA transfer in the peripheral register (if enabled)
1525 * - Abort DMA transfer by calling HAL_DMA_Abort_IT (in case of transfer in DMA mode)
1526 * - Set handle State to READY
1527 * - At abort completion, call user abort complete callback
1528 * @note This procedure is executed in Interrupt mode, meaning that abort procedure could be
1529 * considered as completed only when user abort complete callback is executed (not when exiting function).
1530 * @retval HAL status
1532 HAL_StatusTypeDef HAL_SMARTCARD_Abort_IT(SMARTCARD_HandleTypeDef *hsmartcard)
1534 uint32_t abortcplt = 1U;
1536 /* Disable RTOIE, EOBIE, TXEIE, TCIE, RXNE, PE, RXFT, TXFT and ERR (Frame error, noise error, overrun error) interrupts */
1537 CLEAR_BIT(hsmartcard->Instance->CR1,
1538 (USART_CR1_RXNEIE_RXFNEIE | USART_CR1_PEIE | USART_CR1_TXEIE_TXFNFIE | USART_CR1_TCIE | USART_CR1_RTOIE |
1539 USART_CR1_EOBIE));
1540 CLEAR_BIT(hsmartcard->Instance->CR3, (USART_CR3_EIE | USART_CR3_RXFTIE | USART_CR3_TXFTIE));
1542 /* If DMA Tx and/or DMA Rx Handles are associated to SMARTCARD Handle, DMA Abort complete callbacks should be initialised
1543 before any call to DMA Abort functions */
1544 /* DMA Tx Handle is valid */
1545 if (hsmartcard->hdmatx != NULL)
1547 /* Set DMA Abort Complete callback if SMARTCARD DMA Tx request if enabled.
1548 Otherwise, set it to NULL */
1549 if (HAL_IS_BIT_SET(hsmartcard->Instance->CR3, USART_CR3_DMAT))
1551 hsmartcard->hdmatx->XferAbortCallback = SMARTCARD_DMATxAbortCallback;
1553 else
1555 hsmartcard->hdmatx->XferAbortCallback = NULL;
1558 /* DMA Rx Handle is valid */
1559 if (hsmartcard->hdmarx != NULL)
1561 /* Set DMA Abort Complete callback if SMARTCARD DMA Rx request if enabled.
1562 Otherwise, set it to NULL */
1563 if (HAL_IS_BIT_SET(hsmartcard->Instance->CR3, USART_CR3_DMAR))
1565 hsmartcard->hdmarx->XferAbortCallback = SMARTCARD_DMARxAbortCallback;
1567 else
1569 hsmartcard->hdmarx->XferAbortCallback = NULL;
1573 /* Disable the SMARTCARD DMA Tx request if enabled */
1574 if (HAL_IS_BIT_SET(hsmartcard->Instance->CR3, USART_CR3_DMAT))
1576 /* Disable DMA Tx at UART level */
1577 CLEAR_BIT(hsmartcard->Instance->CR3, USART_CR3_DMAT);
1579 /* Abort the SMARTCARD DMA Tx channel : use non blocking DMA Abort API (callback) */
1580 if (hsmartcard->hdmatx != NULL)
1582 /* SMARTCARD Tx DMA Abort callback has already been initialised :
1583 will lead to call HAL_SMARTCARD_AbortCpltCallback() at end of DMA abort procedure */
1585 /* Abort DMA TX */
1586 if (HAL_DMA_Abort_IT(hsmartcard->hdmatx) != HAL_OK)
1588 hsmartcard->hdmatx->XferAbortCallback = NULL;
1590 else
1592 abortcplt = 0U;
1597 /* Disable the SMARTCARD DMA Rx request if enabled */
1598 if (HAL_IS_BIT_SET(hsmartcard->Instance->CR3, USART_CR3_DMAR))
1600 CLEAR_BIT(hsmartcard->Instance->CR3, USART_CR3_DMAR);
1602 /* Abort the SMARTCARD DMA Rx channel : use non blocking DMA Abort API (callback) */
1603 if (hsmartcard->hdmarx != NULL)
1605 /* SMARTCARD Rx DMA Abort callback has already been initialised :
1606 will lead to call HAL_SMARTCARD_AbortCpltCallback() at end of DMA abort procedure */
1608 /* Abort DMA RX */
1609 if (HAL_DMA_Abort_IT(hsmartcard->hdmarx) != HAL_OK)
1611 hsmartcard->hdmarx->XferAbortCallback = NULL;
1612 abortcplt = 1U;
1614 else
1616 abortcplt = 0U;
1621 /* if no DMA abort complete callback execution is required => call user Abort Complete callback */
1622 if (abortcplt == 1U)
1624 /* Reset Tx and Rx transfer counters */
1625 hsmartcard->TxXferCount = 0U;
1626 hsmartcard->RxXferCount = 0U;
1628 /* Clear ISR function pointers */
1629 hsmartcard->RxISR = NULL;
1630 hsmartcard->TxISR = NULL;
1632 /* Reset errorCode */
1633 hsmartcard->ErrorCode = HAL_SMARTCARD_ERROR_NONE;
1635 /* Clear the Error flags in the ICR register */
1636 __HAL_SMARTCARD_CLEAR_FLAG(hsmartcard,
1637 SMARTCARD_CLEAR_OREF | SMARTCARD_CLEAR_NEF | SMARTCARD_CLEAR_PEF | SMARTCARD_CLEAR_FEF | SMARTCARD_CLEAR_RTOF |
1638 SMARTCARD_CLEAR_EOBF);
1640 /* Restore hsmartcard->gState and hsmartcard->RxState to Ready */
1641 hsmartcard->gState = HAL_SMARTCARD_STATE_READY;
1642 hsmartcard->RxState = HAL_SMARTCARD_STATE_READY;
1644 /* As no DMA to be aborted, call directly user Abort complete callback */
1645 #if (USE_HAL_SMARTCARD_REGISTER_CALLBACKS == 1)
1646 /* Call registered Abort complete callback */
1647 hsmartcard->AbortCpltCallback(hsmartcard);
1648 #else
1649 /* Call legacy weak Abort complete callback */
1650 HAL_SMARTCARD_AbortCpltCallback(hsmartcard);
1651 #endif /* USE_HAL_SMARTCARD_REGISTER_CALLBACK */
1654 return HAL_OK;
1658 * @brief Abort ongoing Transmit transfer (Interrupt mode).
1659 * @param hsmartcard Pointer to a SMARTCARD_HandleTypeDef structure that contains
1660 * the configuration information for the specified SMARTCARD module.
1661 * @note This procedure could be used for aborting any ongoing Tx transfer started in Interrupt or DMA mode.
1662 * This procedure performs following operations :
1663 * - Disable SMARTCARD Interrupts (Tx)
1664 * - Disable the DMA transfer in the peripheral register (if enabled)
1665 * - Abort DMA transfer by calling HAL_DMA_Abort_IT (in case of transfer in DMA mode)
1666 * - Set handle State to READY
1667 * - At abort completion, call user abort complete callback
1668 * @note This procedure is executed in Interrupt mode, meaning that abort procedure could be
1669 * considered as completed only when user abort complete callback is executed (not when exiting function).
1670 * @retval HAL status
1672 HAL_StatusTypeDef HAL_SMARTCARD_AbortTransmit_IT(SMARTCARD_HandleTypeDef *hsmartcard)
1674 /* Disable TCIE, TXEIE and TXFTIE interrupts */
1675 CLEAR_BIT(hsmartcard->Instance->CR1, (USART_CR1_TXEIE_TXFNFIE | USART_CR1_TCIE));
1676 CLEAR_BIT(hsmartcard->Instance->CR3, USART_CR3_TXFTIE);
1678 /* Check if a receive process is ongoing or not. If not disable ERR IT */
1679 if (hsmartcard->RxState == HAL_SMARTCARD_STATE_READY)
1681 /* Disable the SMARTCARD Error Interrupt: (Frame error) */
1682 CLEAR_BIT(hsmartcard->Instance->CR3, USART_CR3_EIE);
1685 /* Disable the SMARTCARD DMA Tx request if enabled */
1686 if (HAL_IS_BIT_SET(hsmartcard->Instance->CR3, USART_CR3_DMAT))
1688 CLEAR_BIT(hsmartcard->Instance->CR3, USART_CR3_DMAT);
1690 /* Abort the SMARTCARD DMA Tx channel : use non blocking DMA Abort API (callback) */
1691 if (hsmartcard->hdmatx != NULL)
1693 /* Set the SMARTCARD DMA Abort callback :
1694 will lead to call HAL_SMARTCARD_AbortCpltCallback() at end of DMA abort procedure */
1695 hsmartcard->hdmatx->XferAbortCallback = SMARTCARD_DMATxOnlyAbortCallback;
1697 /* Abort DMA TX */
1698 if (HAL_DMA_Abort_IT(hsmartcard->hdmatx) != HAL_OK)
1700 /* Call Directly hsmartcard->hdmatx->XferAbortCallback function in case of error */
1701 hsmartcard->hdmatx->XferAbortCallback(hsmartcard->hdmatx);
1704 else
1706 /* Reset Tx transfer counter */
1707 hsmartcard->TxXferCount = 0U;
1709 /* Clear TxISR function pointers */
1710 hsmartcard->TxISR = NULL;
1712 /* Restore hsmartcard->gState to Ready */
1713 hsmartcard->gState = HAL_SMARTCARD_STATE_READY;
1715 /* As no DMA to be aborted, call directly user Abort complete callback */
1716 #if (USE_HAL_SMARTCARD_REGISTER_CALLBACKS == 1)
1717 /* Call registered Abort Transmit Complete Callback */
1718 hsmartcard->AbortTransmitCpltCallback(hsmartcard);
1719 #else
1720 /* Call legacy weak Abort Transmit Complete Callback */
1721 HAL_SMARTCARD_AbortTransmitCpltCallback(hsmartcard);
1722 #endif /* USE_HAL_SMARTCARD_REGISTER_CALLBACK */
1725 else
1727 /* Reset Tx transfer counter */
1728 hsmartcard->TxXferCount = 0U;
1730 /* Clear TxISR function pointers */
1731 hsmartcard->TxISR = NULL;
1733 /* Clear the Error flags in the ICR register */
1734 __HAL_SMARTCARD_CLEAR_FLAG(hsmartcard, SMARTCARD_CLEAR_FEF);
1736 /* Restore hsmartcard->gState to Ready */
1737 hsmartcard->gState = HAL_SMARTCARD_STATE_READY;
1739 /* As no DMA to be aborted, call directly user Abort complete callback */
1740 #if (USE_HAL_SMARTCARD_REGISTER_CALLBACKS == 1)
1741 /* Call registered Abort Transmit Complete Callback */
1742 hsmartcard->AbortTransmitCpltCallback(hsmartcard);
1743 #else
1744 /* Call legacy weak Abort Transmit Complete Callback */
1745 HAL_SMARTCARD_AbortTransmitCpltCallback(hsmartcard);
1746 #endif /* USE_HAL_SMARTCARD_REGISTER_CALLBACK */
1749 return HAL_OK;
1753 * @brief Abort ongoing Receive transfer (Interrupt mode).
1754 * @param hsmartcard Pointer to a SMARTCARD_HandleTypeDef structure that contains
1755 * the configuration information for the specified SMARTCARD module.
1756 * @note This procedure could be used for aborting any ongoing Rx transfer started in Interrupt or DMA mode.
1757 * This procedure performs following operations :
1758 * - Disable SMARTCARD Interrupts (Rx)
1759 * - Disable the DMA transfer in the peripheral register (if enabled)
1760 * - Abort DMA transfer by calling HAL_DMA_Abort_IT (in case of transfer in DMA mode)
1761 * - Set handle State to READY
1762 * - At abort completion, call user abort complete callback
1763 * @note This procedure is executed in Interrupt mode, meaning that abort procedure could be
1764 * considered as completed only when user abort complete callback is executed (not when exiting function).
1765 * @retval HAL status
1767 HAL_StatusTypeDef HAL_SMARTCARD_AbortReceive_IT(SMARTCARD_HandleTypeDef *hsmartcard)
1769 /* Disable RTOIE, EOBIE, RXNE, PE, RXFT and ERR (Frame error, noise error, overrun error) interrupts */
1770 CLEAR_BIT(hsmartcard->Instance->CR1, (USART_CR1_RXNEIE_RXFNEIE | USART_CR1_PEIE | USART_CR1_RTOIE | USART_CR1_EOBIE));
1771 CLEAR_BIT(hsmartcard->Instance->CR3, (USART_CR3_EIE | USART_CR3_RXFTIE));
1773 /* Check if a Transmit process is ongoing or not. If not disable ERR IT */
1774 if (hsmartcard->gState == HAL_SMARTCARD_STATE_READY)
1776 /* Disable the SMARTCARD Error Interrupt: (Frame error) */
1777 CLEAR_BIT(hsmartcard->Instance->CR3, USART_CR3_EIE);
1780 /* Disable the SMARTCARD DMA Rx request if enabled */
1781 if (HAL_IS_BIT_SET(hsmartcard->Instance->CR3, USART_CR3_DMAR))
1783 CLEAR_BIT(hsmartcard->Instance->CR3, USART_CR3_DMAR);
1785 /* Abort the SMARTCARD DMA Rx channel : use non blocking DMA Abort API (callback) */
1786 if (hsmartcard->hdmarx != NULL)
1788 /* Set the SMARTCARD DMA Abort callback :
1789 will lead to call HAL_SMARTCARD_AbortCpltCallback() at end of DMA abort procedure */
1790 hsmartcard->hdmarx->XferAbortCallback = SMARTCARD_DMARxOnlyAbortCallback;
1792 /* Abort DMA RX */
1793 if (HAL_DMA_Abort_IT(hsmartcard->hdmarx) != HAL_OK)
1795 /* Call Directly hsmartcard->hdmarx->XferAbortCallback function in case of error */
1796 hsmartcard->hdmarx->XferAbortCallback(hsmartcard->hdmarx);
1799 else
1801 /* Reset Rx transfer counter */
1802 hsmartcard->RxXferCount = 0U;
1804 /* Clear RxISR function pointer */
1805 hsmartcard->RxISR = NULL;
1807 /* Clear the Error flags in the ICR register */
1808 __HAL_SMARTCARD_CLEAR_FLAG(hsmartcard,
1809 SMARTCARD_CLEAR_OREF | SMARTCARD_CLEAR_NEF | SMARTCARD_CLEAR_PEF | SMARTCARD_CLEAR_FEF | SMARTCARD_CLEAR_RTOF |
1810 SMARTCARD_CLEAR_EOBF);
1812 /* Restore hsmartcard->RxState to Ready */
1813 hsmartcard->RxState = HAL_SMARTCARD_STATE_READY;
1815 /* As no DMA to be aborted, call directly user Abort complete callback */
1816 #if (USE_HAL_SMARTCARD_REGISTER_CALLBACKS == 1)
1817 /* Call registered Abort Receive Complete Callback */
1818 hsmartcard->AbortReceiveCpltCallback(hsmartcard);
1819 #else
1820 /* Call legacy weak Abort Receive Complete Callback */
1821 HAL_SMARTCARD_AbortReceiveCpltCallback(hsmartcard);
1822 #endif /* USE_HAL_SMARTCARD_REGISTER_CALLBACK */
1825 else
1827 /* Reset Rx transfer counter */
1828 hsmartcard->RxXferCount = 0U;
1830 /* Clear RxISR function pointer */
1831 hsmartcard->RxISR = NULL;
1833 /* Clear the Error flags in the ICR register */
1834 __HAL_SMARTCARD_CLEAR_FLAG(hsmartcard,
1835 SMARTCARD_CLEAR_OREF | SMARTCARD_CLEAR_NEF | SMARTCARD_CLEAR_PEF | SMARTCARD_CLEAR_FEF | SMARTCARD_CLEAR_RTOF |
1836 SMARTCARD_CLEAR_EOBF);
1838 /* Restore hsmartcard->RxState to Ready */
1839 hsmartcard->RxState = HAL_SMARTCARD_STATE_READY;
1841 /* As no DMA to be aborted, call directly user Abort complete callback */
1842 #if (USE_HAL_SMARTCARD_REGISTER_CALLBACKS == 1)
1843 /* Call registered Abort Receive Complete Callback */
1844 hsmartcard->AbortReceiveCpltCallback(hsmartcard);
1845 #else
1846 /* Call legacy weak Abort Receive Complete Callback */
1847 HAL_SMARTCARD_AbortReceiveCpltCallback(hsmartcard);
1848 #endif /* USE_HAL_SMARTCARD_REGISTER_CALLBACK */
1851 return HAL_OK;
1855 * @brief Handle SMARTCARD interrupt requests.
1856 * @param hsmartcard Pointer to a SMARTCARD_HandleTypeDef structure that contains
1857 * the configuration information for the specified SMARTCARD module.
1858 * @retval None
1860 void HAL_SMARTCARD_IRQHandler(SMARTCARD_HandleTypeDef *hsmartcard)
1862 uint32_t isrflags = READ_REG(hsmartcard->Instance->ISR);
1863 uint32_t cr1its = READ_REG(hsmartcard->Instance->CR1);
1864 uint32_t cr3its = READ_REG(hsmartcard->Instance->CR3);
1865 uint32_t errorflags;
1866 uint32_t errorcode;
1868 /* If no error occurs */
1869 errorflags = (isrflags & (uint32_t)(USART_ISR_PE | USART_ISR_FE | USART_ISR_ORE | USART_ISR_NE | USART_ISR_RTOF));
1870 if (errorflags == 0U)
1872 /* SMARTCARD in mode Receiver ---------------------------------------------------*/
1873 if (((isrflags & USART_ISR_RXNE_RXFNE) != 0U)
1874 && (((cr1its & USART_CR1_RXNEIE_RXFNEIE) != 0U)
1875 || ((cr3its & USART_CR3_RXFTIE) != 0U)))
1877 if (hsmartcard->RxISR != NULL)
1879 hsmartcard->RxISR(hsmartcard);
1881 return;
1885 /* If some errors occur */
1886 if ((errorflags != 0U)
1887 && ((((cr3its & (USART_CR3_RXFTIE | USART_CR3_EIE)) != 0U)
1888 || ((cr1its & (USART_CR1_RXNEIE_RXFNEIE | USART_CR1_PEIE)) != 0U))))
1890 /* SMARTCARD parity error interrupt occurred -------------------------------------*/
1891 if (((isrflags & USART_ISR_PE) != 0U) && ((cr1its & USART_CR1_PEIE) != 0U))
1893 __HAL_SMARTCARD_CLEAR_IT(hsmartcard, SMARTCARD_CLEAR_PEF);
1895 hsmartcard->ErrorCode |= HAL_SMARTCARD_ERROR_PE;
1898 /* SMARTCARD frame error interrupt occurred --------------------------------------*/
1899 if (((isrflags & USART_ISR_FE) != 0U) && ((cr3its & USART_CR3_EIE) != 0U))
1901 __HAL_SMARTCARD_CLEAR_IT(hsmartcard, SMARTCARD_CLEAR_FEF);
1903 hsmartcard->ErrorCode |= HAL_SMARTCARD_ERROR_FE;
1906 /* SMARTCARD noise error interrupt occurred --------------------------------------*/
1907 if (((isrflags & USART_ISR_NE) != 0U) && ((cr3its & USART_CR3_EIE) != 0U))
1909 __HAL_SMARTCARD_CLEAR_IT(hsmartcard, SMARTCARD_CLEAR_NEF);
1911 hsmartcard->ErrorCode |= HAL_SMARTCARD_ERROR_NE;
1914 /* SMARTCARD Over-Run interrupt occurred -----------------------------------------*/
1915 if (((isrflags & USART_ISR_ORE) != 0U)
1916 && (((cr1its & USART_CR1_RXNEIE_RXFNEIE) != 0U)
1917 || ((cr3its & USART_CR3_RXFTIE) != 0U)
1918 || ((cr3its & USART_CR3_EIE) != 0U)))
1920 __HAL_SMARTCARD_CLEAR_IT(hsmartcard, SMARTCARD_CLEAR_OREF);
1922 hsmartcard->ErrorCode |= HAL_SMARTCARD_ERROR_ORE;
1925 /* SMARTCARD receiver timeout interrupt occurred -----------------------------------------*/
1926 if (((isrflags & USART_ISR_RTOF) != 0U) && ((cr1its & USART_CR1_RTOIE) != 0U))
1928 __HAL_SMARTCARD_CLEAR_IT(hsmartcard, SMARTCARD_CLEAR_RTOF);
1930 hsmartcard->ErrorCode |= HAL_SMARTCARD_ERROR_RTO;
1933 /* Call SMARTCARD Error Call back function if need be --------------------------*/
1934 if (hsmartcard->ErrorCode != HAL_SMARTCARD_ERROR_NONE)
1936 /* SMARTCARD in mode Receiver ---------------------------------------------------*/
1937 if (((isrflags & USART_ISR_RXNE_RXFNE) != 0U)
1938 && (((cr1its & USART_CR1_RXNEIE_RXFNEIE) != 0U)
1939 || ((cr3its & USART_CR3_RXFTIE) != 0U)))
1941 if (hsmartcard->RxISR != NULL)
1943 hsmartcard->RxISR(hsmartcard);
1947 /* If Error is to be considered as blocking :
1948 - Receiver Timeout error in Reception
1949 - Overrun error in Reception
1950 - any error occurs in DMA mode reception
1952 errorcode = hsmartcard->ErrorCode;
1953 if ((HAL_IS_BIT_SET(hsmartcard->Instance->CR3, USART_CR3_DMAR))
1954 || ((errorcode & (HAL_SMARTCARD_ERROR_RTO | HAL_SMARTCARD_ERROR_ORE)) != 0U))
1956 /* Blocking error : transfer is aborted
1957 Set the SMARTCARD state ready to be able to start again the process,
1958 Disable Rx Interrupts, and disable Rx DMA request, if ongoing */
1959 SMARTCARD_EndRxTransfer(hsmartcard);
1961 /* Disable the SMARTCARD DMA Rx request if enabled */
1962 if (HAL_IS_BIT_SET(hsmartcard->Instance->CR3, USART_CR3_DMAR))
1964 CLEAR_BIT(hsmartcard->Instance->CR3, USART_CR3_DMAR);
1966 /* Abort the SMARTCARD DMA Rx channel */
1967 if (hsmartcard->hdmarx != NULL)
1969 /* Set the SMARTCARD DMA Abort callback :
1970 will lead to call HAL_SMARTCARD_ErrorCallback() at end of DMA abort procedure */
1971 hsmartcard->hdmarx->XferAbortCallback = SMARTCARD_DMAAbortOnError;
1973 /* Abort DMA RX */
1974 if (HAL_DMA_Abort_IT(hsmartcard->hdmarx) != HAL_OK)
1976 /* Call Directly hsmartcard->hdmarx->XferAbortCallback function in case of error */
1977 hsmartcard->hdmarx->XferAbortCallback(hsmartcard->hdmarx);
1980 else
1982 #if (USE_HAL_SMARTCARD_REGISTER_CALLBACKS == 1)
1983 /* Call registered user error callback */
1984 hsmartcard->ErrorCallback(hsmartcard);
1985 #else
1986 /* Call legacy weak user error callback */
1987 HAL_SMARTCARD_ErrorCallback(hsmartcard);
1988 #endif /* USE_HAL_SMARTCARD_REGISTER_CALLBACK */
1991 else
1993 #if (USE_HAL_SMARTCARD_REGISTER_CALLBACKS == 1)
1994 /* Call registered user error callback */
1995 hsmartcard->ErrorCallback(hsmartcard);
1996 #else
1997 /* Call legacy weak user error callback */
1998 HAL_SMARTCARD_ErrorCallback(hsmartcard);
1999 #endif /* USE_HAL_SMARTCARD_REGISTER_CALLBACK */
2002 /* other error type to be considered as blocking :
2003 - Frame error in Transmission
2005 else if ((hsmartcard->gState == HAL_SMARTCARD_STATE_BUSY_TX)
2006 && ((errorcode & HAL_SMARTCARD_ERROR_FE) != 0U))
2008 /* Blocking error : transfer is aborted
2009 Set the SMARTCARD state ready to be able to start again the process,
2010 Disable Tx Interrupts, and disable Tx DMA request, if ongoing */
2011 SMARTCARD_EndTxTransfer(hsmartcard);
2013 /* Disable the SMARTCARD DMA Tx request if enabled */
2014 if (HAL_IS_BIT_SET(hsmartcard->Instance->CR3, USART_CR3_DMAT))
2016 CLEAR_BIT(hsmartcard->Instance->CR3, USART_CR3_DMAT);
2018 /* Abort the SMARTCARD DMA Tx channel */
2019 if (hsmartcard->hdmatx != NULL)
2021 /* Set the SMARTCARD DMA Abort callback :
2022 will lead to call HAL_SMARTCARD_ErrorCallback() at end of DMA abort procedure */
2023 hsmartcard->hdmatx->XferAbortCallback = SMARTCARD_DMAAbortOnError;
2025 /* Abort DMA TX */
2026 if (HAL_DMA_Abort_IT(hsmartcard->hdmatx) != HAL_OK)
2028 /* Call Directly hsmartcard->hdmatx->XferAbortCallback function in case of error */
2029 hsmartcard->hdmatx->XferAbortCallback(hsmartcard->hdmatx);
2032 else
2034 #if (USE_HAL_SMARTCARD_REGISTER_CALLBACKS == 1)
2035 /* Call registered user error callback */
2036 hsmartcard->ErrorCallback(hsmartcard);
2037 #else
2038 /* Call legacy weak user error callback */
2039 HAL_SMARTCARD_ErrorCallback(hsmartcard);
2040 #endif /* USE_HAL_SMARTCARD_REGISTER_CALLBACK */
2043 else
2045 #if (USE_HAL_SMARTCARD_REGISTER_CALLBACKS == 1)
2046 /* Call registered user error callback */
2047 hsmartcard->ErrorCallback(hsmartcard);
2048 #else
2049 /* Call legacy weak user error callback */
2050 HAL_SMARTCARD_ErrorCallback(hsmartcard);
2051 #endif /* USE_HAL_SMARTCARD_REGISTER_CALLBACK */
2054 else
2056 /* Non Blocking error : transfer could go on.
2057 Error is notified to user through user error callback */
2058 #if (USE_HAL_SMARTCARD_REGISTER_CALLBACKS == 1)
2059 /* Call registered user error callback */
2060 hsmartcard->ErrorCallback(hsmartcard);
2061 #else
2062 /* Call legacy weak user error callback */
2063 HAL_SMARTCARD_ErrorCallback(hsmartcard);
2064 #endif /* USE_HAL_SMARTCARD_REGISTER_CALLBACK */
2065 hsmartcard->ErrorCode = HAL_SMARTCARD_ERROR_NONE;
2068 return;
2070 } /* End if some error occurs */
2072 /* SMARTCARD in mode Receiver, end of block interruption ------------------------*/
2073 if (((isrflags & USART_ISR_EOBF) != 0U) && ((cr1its & USART_CR1_EOBIE) != 0U))
2075 hsmartcard->RxState = HAL_SMARTCARD_STATE_READY;
2076 __HAL_UNLOCK(hsmartcard);
2077 #if (USE_HAL_SMARTCARD_REGISTER_CALLBACKS == 1)
2078 /* Call registered Rx complete callback */
2079 hsmartcard->RxCpltCallback(hsmartcard);
2080 #else
2081 /* Call legacy weak Rx complete callback */
2082 HAL_SMARTCARD_RxCpltCallback(hsmartcard);
2083 #endif /* USE_HAL_SMARTCARD_REGISTER_CALLBACK */
2084 /* Clear EOBF interrupt after HAL_SMARTCARD_RxCpltCallback() call for the End of Block information
2085 to be available during HAL_SMARTCARD_RxCpltCallback() processing */
2086 __HAL_SMARTCARD_CLEAR_IT(hsmartcard, SMARTCARD_CLEAR_EOBF);
2087 return;
2090 /* SMARTCARD in mode Transmitter ------------------------------------------------*/
2091 if (((isrflags & USART_ISR_TXE_TXFNF) != 0U)
2092 && (((cr1its & USART_CR1_TXEIE_TXFNFIE) != 0U)
2093 || ((cr3its & USART_CR3_TXFTIE) != 0U)))
2095 if (hsmartcard->TxISR != NULL)
2097 hsmartcard->TxISR(hsmartcard);
2099 return;
2102 /* SMARTCARD in mode Transmitter (transmission end) ------------------------*/
2103 if (__HAL_SMARTCARD_GET_IT(hsmartcard, hsmartcard->AdvancedInit.TxCompletionIndication) != RESET)
2105 if (__HAL_SMARTCARD_GET_IT_SOURCE(hsmartcard, hsmartcard->AdvancedInit.TxCompletionIndication) != RESET)
2107 SMARTCARD_EndTransmit_IT(hsmartcard);
2108 return;
2112 /* SMARTCARD TX Fifo Empty occurred ----------------------------------------------*/
2113 if (((isrflags & USART_ISR_TXFE) != 0U) && ((cr1its & USART_CR1_TXFEIE) != 0U))
2115 #if (USE_HAL_SMARTCARD_REGISTER_CALLBACKS == 1)
2116 /* Call registered Tx Fifo Empty Callback */
2117 hsmartcard->TxFifoEmptyCallback(hsmartcard);
2118 #else
2119 /* Call legacy weak Tx Fifo Empty Callback */
2120 HAL_SMARTCARDEx_TxFifoEmptyCallback(hsmartcard);
2121 #endif /* USE_HAL_SMARTCARD_REGISTER_CALLBACK */
2122 return;
2125 /* SMARTCARD RX Fifo Full occurred ----------------------------------------------*/
2126 if (((isrflags & USART_ISR_RXFF) != 0U) && ((cr1its & USART_CR1_RXFFIE) != 0U))
2128 #if (USE_HAL_SMARTCARD_REGISTER_CALLBACKS == 1)
2129 /* Call registered Rx Fifo Full Callback */
2130 hsmartcard->RxFifoFullCallback(hsmartcard);
2131 #else
2132 /* Call legacy weak Rx Fifo Full Callback */
2133 HAL_SMARTCARDEx_RxFifoFullCallback(hsmartcard);
2134 #endif /* USE_HAL_SMARTCARD_REGISTER_CALLBACK */
2135 return;
2140 * @brief Tx Transfer completed callback.
2141 * @param hsmartcard Pointer to a SMARTCARD_HandleTypeDef structure that contains
2142 * the configuration information for the specified SMARTCARD module.
2143 * @retval None
2145 __weak void HAL_SMARTCARD_TxCpltCallback(SMARTCARD_HandleTypeDef *hsmartcard)
2147 /* Prevent unused argument(s) compilation warning */
2148 UNUSED(hsmartcard);
2150 /* NOTE : This function should not be modified, when the callback is needed,
2151 the HAL_SMARTCARD_TxCpltCallback can be implemented in the user file.
2156 * @brief Rx Transfer completed callback.
2157 * @param hsmartcard Pointer to a SMARTCARD_HandleTypeDef structure that contains
2158 * the configuration information for the specified SMARTCARD module.
2159 * @retval None
2161 __weak void HAL_SMARTCARD_RxCpltCallback(SMARTCARD_HandleTypeDef *hsmartcard)
2163 /* Prevent unused argument(s) compilation warning */
2164 UNUSED(hsmartcard);
2166 /* NOTE : This function should not be modified, when the callback is needed,
2167 the HAL_SMARTCARD_RxCpltCallback can be implemented in the user file.
2172 * @brief SMARTCARD error callback.
2173 * @param hsmartcard Pointer to a SMARTCARD_HandleTypeDef structure that contains
2174 * the configuration information for the specified SMARTCARD module.
2175 * @retval None
2177 __weak void HAL_SMARTCARD_ErrorCallback(SMARTCARD_HandleTypeDef *hsmartcard)
2179 /* Prevent unused argument(s) compilation warning */
2180 UNUSED(hsmartcard);
2182 /* NOTE : This function should not be modified, when the callback is needed,
2183 the HAL_SMARTCARD_ErrorCallback can be implemented in the user file.
2188 * @brief SMARTCARD Abort Complete callback.
2189 * @param hsmartcard Pointer to a SMARTCARD_HandleTypeDef structure that contains
2190 * the configuration information for the specified SMARTCARD module.
2191 * @retval None
2193 __weak void HAL_SMARTCARD_AbortCpltCallback(SMARTCARD_HandleTypeDef *hsmartcard)
2195 /* Prevent unused argument(s) compilation warning */
2196 UNUSED(hsmartcard);
2198 /* NOTE : This function should not be modified, when the callback is needed,
2199 the HAL_SMARTCARD_AbortCpltCallback can be implemented in the user file.
2204 * @brief SMARTCARD Abort Complete callback.
2205 * @param hsmartcard Pointer to a SMARTCARD_HandleTypeDef structure that contains
2206 * the configuration information for the specified SMARTCARD module.
2207 * @retval None
2209 __weak void HAL_SMARTCARD_AbortTransmitCpltCallback(SMARTCARD_HandleTypeDef *hsmartcard)
2211 /* Prevent unused argument(s) compilation warning */
2212 UNUSED(hsmartcard);
2214 /* NOTE : This function should not be modified, when the callback is needed,
2215 the HAL_SMARTCARD_AbortTransmitCpltCallback can be implemented in the user file.
2220 * @brief SMARTCARD Abort Receive Complete callback.
2221 * @param hsmartcard Pointer to a SMARTCARD_HandleTypeDef structure that contains
2222 * the configuration information for the specified SMARTCARD module.
2223 * @retval None
2225 __weak void HAL_SMARTCARD_AbortReceiveCpltCallback(SMARTCARD_HandleTypeDef *hsmartcard)
2227 /* Prevent unused argument(s) compilation warning */
2228 UNUSED(hsmartcard);
2230 /* NOTE : This function should not be modified, when the callback is needed,
2231 the HAL_SMARTCARD_AbortReceiveCpltCallback can be implemented in the user file.
2236 * @}
2239 /** @defgroup SMARTCARD_Exported_Functions_Group4 Peripheral State and Errors functions
2240 * @brief SMARTCARD State and Errors functions
2242 @verbatim
2243 ==============================================================================
2244 ##### Peripheral State and Errors functions #####
2245 ==============================================================================
2246 [..]
2247 This subsection provides a set of functions allowing to return the State of SmartCard
2248 handle and also return Peripheral Errors occurred during communication process
2249 (+) HAL_SMARTCARD_GetState() API can be helpful to check in run-time the state
2250 of the SMARTCARD peripheral.
2251 (+) HAL_SMARTCARD_GetError() checks in run-time errors that could occur during
2252 communication.
2254 @endverbatim
2255 * @{
2259 * @brief Return the SMARTCARD handle state.
2260 * @param hsmartcard Pointer to a SMARTCARD_HandleTypeDef structure that contains
2261 * the configuration information for the specified SMARTCARD module.
2262 * @retval SMARTCARD handle state
2264 HAL_SMARTCARD_StateTypeDef HAL_SMARTCARD_GetState(SMARTCARD_HandleTypeDef *hsmartcard)
2266 /* Return SMARTCARD handle state */
2267 uint32_t temp1;
2268 uint32_t temp2;
2269 temp1 = (uint32_t)hsmartcard->gState;
2270 temp2 = (uint32_t)hsmartcard->RxState;
2272 return (HAL_SMARTCARD_StateTypeDef)(temp1 | temp2);
2276 * @brief Return the SMARTCARD handle error code.
2277 * @param hsmartcard Pointer to a SMARTCARD_HandleTypeDef structure that contains
2278 * the configuration information for the specified SMARTCARD module.
2279 * @retval SMARTCARD handle Error Code
2281 uint32_t HAL_SMARTCARD_GetError(SMARTCARD_HandleTypeDef *hsmartcard)
2283 return hsmartcard->ErrorCode;
2287 * @}
2291 * @}
2294 /** @defgroup SMARTCARD_Private_Functions SMARTCARD Private Functions
2295 * @{
2298 #if (USE_HAL_SMARTCARD_REGISTER_CALLBACKS == 1)
2300 * @brief Initialize the callbacks to their default values.
2301 * @param hsmartcard SMARTCARD handle.
2302 * @retval none
2304 void SMARTCARD_InitCallbacksToDefault(SMARTCARD_HandleTypeDef *hsmartcard)
2306 /* Init the SMARTCARD Callback settings */
2307 hsmartcard->TxCpltCallback = HAL_SMARTCARD_TxCpltCallback; /* Legacy weak TxCpltCallback */
2308 hsmartcard->RxCpltCallback = HAL_SMARTCARD_RxCpltCallback; /* Legacy weak RxCpltCallback */
2309 hsmartcard->ErrorCallback = HAL_SMARTCARD_ErrorCallback; /* Legacy weak ErrorCallback */
2310 hsmartcard->AbortCpltCallback = HAL_SMARTCARD_AbortCpltCallback; /* Legacy weak AbortCpltCallback */
2311 hsmartcard->AbortTransmitCpltCallback = HAL_SMARTCARD_AbortTransmitCpltCallback; /* Legacy weak AbortTransmitCpltCallback */
2312 hsmartcard->AbortReceiveCpltCallback = HAL_SMARTCARD_AbortReceiveCpltCallback; /* Legacy weak AbortReceiveCpltCallback */
2313 hsmartcard->RxFifoFullCallback = HAL_SMARTCARDEx_RxFifoFullCallback; /* Legacy weak RxFifoFullCallback */
2314 hsmartcard->TxFifoEmptyCallback = HAL_SMARTCARDEx_TxFifoEmptyCallback; /* Legacy weak TxFifoEmptyCallback */
2317 #endif /* USE_HAL_SMARTCARD_REGISTER_CALLBACKS */
2320 * @brief Configure the SMARTCARD associated USART peripheral.
2321 * @param hsmartcard Pointer to a SMARTCARD_HandleTypeDef structure that contains
2322 * the configuration information for the specified SMARTCARD module.
2323 * @retval HAL status
2325 static HAL_StatusTypeDef SMARTCARD_SetConfig(SMARTCARD_HandleTypeDef *hsmartcard)
2327 uint32_t tmpreg;
2328 SMARTCARD_ClockSourceTypeDef clocksource;
2329 HAL_StatusTypeDef ret = HAL_OK;
2330 const uint16_t SMARTCARDPrescTable[12] = {1U, 2U, 4U, 6U, 8U, 10U, 12U, 16U, 32U, 64U, 128U, 256U};
2331 PLL2_ClocksTypeDef pll2_clocks;
2332 PLL3_ClocksTypeDef pll3_clocks;
2333 uint32_t pclk;
2335 /* Check the parameters */
2336 assert_param(IS_SMARTCARD_INSTANCE(hsmartcard->Instance));
2337 assert_param(IS_SMARTCARD_BAUDRATE(hsmartcard->Init.BaudRate));
2338 assert_param(IS_SMARTCARD_WORD_LENGTH(hsmartcard->Init.WordLength));
2339 assert_param(IS_SMARTCARD_STOPBITS(hsmartcard->Init.StopBits));
2340 assert_param(IS_SMARTCARD_PARITY(hsmartcard->Init.Parity));
2341 assert_param(IS_SMARTCARD_MODE(hsmartcard->Init.Mode));
2342 assert_param(IS_SMARTCARD_POLARITY(hsmartcard->Init.CLKPolarity));
2343 assert_param(IS_SMARTCARD_PHASE(hsmartcard->Init.CLKPhase));
2344 assert_param(IS_SMARTCARD_LASTBIT(hsmartcard->Init.CLKLastBit));
2345 assert_param(IS_SMARTCARD_ONE_BIT_SAMPLE(hsmartcard->Init.OneBitSampling));
2346 assert_param(IS_SMARTCARD_NACK(hsmartcard->Init.NACKEnable));
2347 assert_param(IS_SMARTCARD_TIMEOUT(hsmartcard->Init.TimeOutEnable));
2348 assert_param(IS_SMARTCARD_AUTORETRY_COUNT(hsmartcard->Init.AutoRetryCount));
2349 assert_param(IS_SMARTCARD_CLOCKPRESCALER(hsmartcard->Init.ClockPrescaler));
2351 /*-------------------------- USART CR1 Configuration -----------------------*/
2352 /* In SmartCard mode, M and PCE are forced to 1 (8 bits + parity).
2353 * Oversampling is forced to 16 (OVER8 = 0).
2354 * Configure the Parity and Mode:
2355 * set PS bit according to hsmartcard->Init.Parity value
2356 * set TE and RE bits according to hsmartcard->Init.Mode value */
2357 tmpreg = (uint32_t) hsmartcard->Init.Parity | hsmartcard->Init.Mode;
2358 tmpreg |= (uint32_t) hsmartcard->Init.WordLength | hsmartcard->FifoMode;
2359 MODIFY_REG(hsmartcard->Instance->CR1, USART_CR1_FIELDS, tmpreg);
2361 /*-------------------------- USART CR2 Configuration -----------------------*/
2362 tmpreg = hsmartcard->Init.StopBits;
2363 /* Synchronous mode is activated by default */
2364 tmpreg |= (uint32_t) USART_CR2_CLKEN | hsmartcard->Init.CLKPolarity;
2365 tmpreg |= (uint32_t) hsmartcard->Init.CLKPhase | hsmartcard->Init.CLKLastBit;
2366 tmpreg |= (uint32_t) hsmartcard->Init.TimeOutEnable;
2367 MODIFY_REG(hsmartcard->Instance->CR2, USART_CR2_FIELDS, tmpreg);
2369 /*-------------------------- USART CR3 Configuration -----------------------*/
2370 /* Configure
2371 * - one-bit sampling method versus three samples' majority rule
2372 * according to hsmartcard->Init.OneBitSampling
2373 * - NACK transmission in case of parity error according
2374 * to hsmartcard->Init.NACKEnable
2375 * - autoretry counter according to hsmartcard->Init.AutoRetryCount */
2377 tmpreg = (uint32_t) hsmartcard->Init.OneBitSampling | hsmartcard->Init.NACKEnable;
2378 tmpreg |= ((uint32_t)hsmartcard->Init.AutoRetryCount << USART_CR3_SCARCNT_Pos);
2379 MODIFY_REG(hsmartcard->Instance->CR3, USART_CR3_FIELDS, tmpreg);
2381 /*--------------------- SMARTCARD clock PRESC Configuration ----------------*/
2382 /* Configure
2383 * - SMARTCARD Clock Prescaler: set PRESCALER according to hsmartcard->Init.ClockPrescaler value */
2384 MODIFY_REG(hsmartcard->Instance->PRESC, USART_PRESC_PRESCALER, hsmartcard->Init.ClockPrescaler);
2386 /*-------------------------- USART GTPR Configuration ----------------------*/
2387 tmpreg = (hsmartcard->Init.Prescaler | ((uint32_t)hsmartcard->Init.GuardTime << USART_GTPR_GT_Pos));
2388 MODIFY_REG(hsmartcard->Instance->GTPR, (uint16_t)(USART_GTPR_GT | USART_GTPR_PSC), (uint16_t)tmpreg);
2390 /*-------------------------- USART RTOR Configuration ----------------------*/
2391 tmpreg = ((uint32_t)hsmartcard->Init.BlockLength << USART_RTOR_BLEN_Pos);
2392 if (hsmartcard->Init.TimeOutEnable == SMARTCARD_TIMEOUT_ENABLE)
2394 assert_param(IS_SMARTCARD_TIMEOUT_VALUE(hsmartcard->Init.TimeOutValue));
2395 tmpreg |= (uint32_t) hsmartcard->Init.TimeOutValue;
2397 MODIFY_REG(hsmartcard->Instance->RTOR, (USART_RTOR_RTO | USART_RTOR_BLEN), tmpreg);
2399 /*-------------------------- USART BRR Configuration -----------------------*/
2400 SMARTCARD_GETCLOCKSOURCE(hsmartcard, clocksource);
2401 tmpreg = 0U;
2402 switch (clocksource)
2404 case SMARTCARD_CLOCKSOURCE_D2PCLK1:
2405 pclk = HAL_RCC_GetPCLK1Freq();
2406 tmpreg = (uint16_t)(((pclk / SMARTCARDPrescTable[hsmartcard->Init.ClockPrescaler]) + (hsmartcard->Init.BaudRate / 2U)) / hsmartcard->Init.BaudRate);
2407 break;
2408 case SMARTCARD_CLOCKSOURCE_D2PCLK2:
2409 pclk = HAL_RCC_GetPCLK2Freq();
2410 tmpreg = (uint16_t)(((pclk / SMARTCARDPrescTable[hsmartcard->Init.ClockPrescaler]) + (hsmartcard->Init.BaudRate / 2U)) / hsmartcard->Init.BaudRate);
2411 break;
2412 case SMARTCARD_CLOCKSOURCE_PLL2Q:
2413 HAL_RCCEx_GetPLL2ClockFreq(&pll2_clocks);
2414 tmpreg = (uint16_t)(((pll2_clocks.PLL2_Q_Frequency / SMARTCARDPrescTable[hsmartcard->Init.ClockPrescaler]) + (hsmartcard->Init.BaudRate / 2U)) / hsmartcard->Init.BaudRate);
2415 break;
2416 case SMARTCARD_CLOCKSOURCE_PLL3Q:
2417 HAL_RCCEx_GetPLL3ClockFreq(&pll3_clocks);
2418 tmpreg = (uint16_t)(((pll3_clocks.PLL3_Q_Frequency / SMARTCARDPrescTable[hsmartcard->Init.ClockPrescaler]) + (hsmartcard->Init.BaudRate / 2U)) / hsmartcard->Init.BaudRate);
2419 break;
2420 case SMARTCARD_CLOCKSOURCE_HSI:
2421 if (__HAL_RCC_GET_FLAG(RCC_FLAG_HSIDIV) != 0U)
2423 tmpreg = (uint16_t)((((HSI_VALUE >> (__HAL_RCC_GET_HSI_DIVIDER() >> 3U)) / SMARTCARDPrescTable[hsmartcard->Init.ClockPrescaler]) + (hsmartcard->Init.BaudRate / 2U)) / hsmartcard->Init.BaudRate);
2425 else
2427 tmpreg = (uint16_t)(((HSI_VALUE / SMARTCARDPrescTable[hsmartcard->Init.ClockPrescaler]) + (hsmartcard->Init.BaudRate / 2U)) / hsmartcard->Init.BaudRate);
2429 break;
2430 case SMARTCARD_CLOCKSOURCE_CSI:
2431 tmpreg = (uint16_t)(((CSI_VALUE / SMARTCARDPrescTable[hsmartcard->Init.ClockPrescaler]) + (hsmartcard->Init.BaudRate / 2U)) / hsmartcard->Init.BaudRate);
2432 break;
2433 case SMARTCARD_CLOCKSOURCE_LSE:
2434 tmpreg = (uint16_t)(((uint16_t)(LSE_VALUE / SMARTCARDPrescTable[hsmartcard->Init.ClockPrescaler]) + (hsmartcard->Init.BaudRate / 2U)) / hsmartcard->Init.BaudRate);
2435 break;
2436 default:
2437 ret = HAL_ERROR;
2438 break;
2441 /* USARTDIV must be greater than or equal to 0d16 */
2442 if ((tmpreg >= USART_BRR_MIN) && (tmpreg <= USART_BRR_MAX))
2444 hsmartcard->Instance->BRR = tmpreg;
2446 else
2448 ret = HAL_ERROR;
2451 /* Initialize the number of data to process during RX/TX ISR execution */
2452 hsmartcard->NbTxDataToProcess = 1U;
2453 hsmartcard->NbRxDataToProcess = 1U;
2455 /* Clear ISR function pointers */
2456 hsmartcard->RxISR = NULL;
2457 hsmartcard->TxISR = NULL;
2459 return ret;
2464 * @brief Configure the SMARTCARD associated USART peripheral advanced features.
2465 * @param hsmartcard Pointer to a SMARTCARD_HandleTypeDef structure that contains
2466 * the configuration information for the specified SMARTCARD module.
2467 * @retval None
2469 static void SMARTCARD_AdvFeatureConfig(SMARTCARD_HandleTypeDef *hsmartcard)
2471 /* Check whether the set of advanced features to configure is properly set */
2472 assert_param(IS_SMARTCARD_ADVFEATURE_INIT(hsmartcard->AdvancedInit.AdvFeatureInit));
2474 /* if required, configure TX pin active level inversion */
2475 if (HAL_IS_BIT_SET(hsmartcard->AdvancedInit.AdvFeatureInit, SMARTCARD_ADVFEATURE_TXINVERT_INIT))
2477 assert_param(IS_SMARTCARD_ADVFEATURE_TXINV(hsmartcard->AdvancedInit.TxPinLevelInvert));
2478 MODIFY_REG(hsmartcard->Instance->CR2, USART_CR2_TXINV, hsmartcard->AdvancedInit.TxPinLevelInvert);
2481 /* if required, configure RX pin active level inversion */
2482 if (HAL_IS_BIT_SET(hsmartcard->AdvancedInit.AdvFeatureInit, SMARTCARD_ADVFEATURE_RXINVERT_INIT))
2484 assert_param(IS_SMARTCARD_ADVFEATURE_RXINV(hsmartcard->AdvancedInit.RxPinLevelInvert));
2485 MODIFY_REG(hsmartcard->Instance->CR2, USART_CR2_RXINV, hsmartcard->AdvancedInit.RxPinLevelInvert);
2488 /* if required, configure data inversion */
2489 if (HAL_IS_BIT_SET(hsmartcard->AdvancedInit.AdvFeatureInit, SMARTCARD_ADVFEATURE_DATAINVERT_INIT))
2491 assert_param(IS_SMARTCARD_ADVFEATURE_DATAINV(hsmartcard->AdvancedInit.DataInvert));
2492 MODIFY_REG(hsmartcard->Instance->CR2, USART_CR2_DATAINV, hsmartcard->AdvancedInit.DataInvert);
2495 /* if required, configure RX/TX pins swap */
2496 if (HAL_IS_BIT_SET(hsmartcard->AdvancedInit.AdvFeatureInit, SMARTCARD_ADVFEATURE_SWAP_INIT))
2498 assert_param(IS_SMARTCARD_ADVFEATURE_SWAP(hsmartcard->AdvancedInit.Swap));
2499 MODIFY_REG(hsmartcard->Instance->CR2, USART_CR2_SWAP, hsmartcard->AdvancedInit.Swap);
2502 /* if required, configure RX overrun detection disabling */
2503 if (HAL_IS_BIT_SET(hsmartcard->AdvancedInit.AdvFeatureInit, SMARTCARD_ADVFEATURE_RXOVERRUNDISABLE_INIT))
2505 assert_param(IS_SMARTCARD_OVERRUN(hsmartcard->AdvancedInit.OverrunDisable));
2506 MODIFY_REG(hsmartcard->Instance->CR3, USART_CR3_OVRDIS, hsmartcard->AdvancedInit.OverrunDisable);
2509 /* if required, configure DMA disabling on reception error */
2510 if (HAL_IS_BIT_SET(hsmartcard->AdvancedInit.AdvFeatureInit, SMARTCARD_ADVFEATURE_DMADISABLEONERROR_INIT))
2512 assert_param(IS_SMARTCARD_ADVFEATURE_DMAONRXERROR(hsmartcard->AdvancedInit.DMADisableonRxError));
2513 MODIFY_REG(hsmartcard->Instance->CR3, USART_CR3_DDRE, hsmartcard->AdvancedInit.DMADisableonRxError);
2516 /* if required, configure MSB first on communication line */
2517 if (HAL_IS_BIT_SET(hsmartcard->AdvancedInit.AdvFeatureInit, SMARTCARD_ADVFEATURE_MSBFIRST_INIT))
2519 assert_param(IS_SMARTCARD_ADVFEATURE_MSBFIRST(hsmartcard->AdvancedInit.MSBFirst));
2520 MODIFY_REG(hsmartcard->Instance->CR2, USART_CR2_MSBFIRST, hsmartcard->AdvancedInit.MSBFirst);
2526 * @brief Check the SMARTCARD Idle State.
2527 * @param hsmartcard Pointer to a SMARTCARD_HandleTypeDef structure that contains
2528 * the configuration information for the specified SMARTCARD module.
2529 * @retval HAL status
2531 static HAL_StatusTypeDef SMARTCARD_CheckIdleState(SMARTCARD_HandleTypeDef *hsmartcard)
2533 uint32_t tickstart;
2535 /* Initialize the SMARTCARD ErrorCode */
2536 hsmartcard->ErrorCode = HAL_SMARTCARD_ERROR_NONE;
2538 /* Init tickstart for timeout management */
2539 tickstart = HAL_GetTick();
2541 /* Check if the Transmitter is enabled */
2542 if ((hsmartcard->Instance->CR1 & USART_CR1_TE) == USART_CR1_TE)
2544 /* Wait until TEACK flag is set */
2545 if (SMARTCARD_WaitOnFlagUntilTimeout(hsmartcard, USART_ISR_TEACK, RESET, tickstart,
2546 SMARTCARD_TEACK_REACK_TIMEOUT) != HAL_OK)
2548 /* Timeout occurred */
2549 return HAL_TIMEOUT;
2552 /* Check if the Receiver is enabled */
2553 if ((hsmartcard->Instance->CR1 & USART_CR1_RE) == USART_CR1_RE)
2555 /* Wait until REACK flag is set */
2556 if (SMARTCARD_WaitOnFlagUntilTimeout(hsmartcard, USART_ISR_REACK, RESET, tickstart,
2557 SMARTCARD_TEACK_REACK_TIMEOUT) != HAL_OK)
2559 /* Timeout occurred */
2560 return HAL_TIMEOUT;
2564 /* Initialize the SMARTCARD states */
2565 hsmartcard->gState = HAL_SMARTCARD_STATE_READY;
2566 hsmartcard->RxState = HAL_SMARTCARD_STATE_READY;
2568 /* Process Unlocked */
2569 __HAL_UNLOCK(hsmartcard);
2571 return HAL_OK;
2575 * @brief Handle SMARTCARD Communication Timeout.
2576 * @param hsmartcard Pointer to a SMARTCARD_HandleTypeDef structure that contains
2577 * the configuration information for the specified SMARTCARD module.
2578 * @param Flag Specifies the SMARTCARD flag to check.
2579 * @param Status The new Flag status (SET or RESET).
2580 * @param Tickstart Tick start value
2581 * @param Timeout Timeout duration.
2582 * @retval HAL status
2584 static HAL_StatusTypeDef SMARTCARD_WaitOnFlagUntilTimeout(SMARTCARD_HandleTypeDef *hsmartcard, uint32_t Flag,
2585 FlagStatus Status, uint32_t Tickstart, uint32_t Timeout)
2587 /* Wait until flag is set */
2588 while ((__HAL_SMARTCARD_GET_FLAG(hsmartcard, Flag) ? SET : RESET) == Status)
2590 /* Check for the Timeout */
2591 if (Timeout != HAL_MAX_DELAY)
2593 if (((HAL_GetTick() - Tickstart) > Timeout) || (Timeout == 0U))
2595 /* Disable TXE, RXNE, PE and ERR (Frame error, noise error, overrun error) interrupts for the interrupt process */
2596 CLEAR_BIT(hsmartcard->Instance->CR1, (USART_CR1_RXNEIE_RXFNEIE | USART_CR1_PEIE | USART_CR1_TXEIE_TXFNFIE));
2597 CLEAR_BIT(hsmartcard->Instance->CR3, USART_CR3_EIE);
2599 hsmartcard->gState = HAL_SMARTCARD_STATE_READY;
2600 hsmartcard->RxState = HAL_SMARTCARD_STATE_READY;
2602 /* Process Unlocked */
2603 __HAL_UNLOCK(hsmartcard);
2604 return HAL_TIMEOUT;
2608 return HAL_OK;
2613 * @brief End ongoing Tx transfer on SMARTCARD peripheral (following error detection or Transmit completion).
2614 * @param hsmartcard Pointer to a SMARTCARD_HandleTypeDef structure that contains
2615 * the configuration information for the specified SMARTCARD module.
2616 * @retval None
2618 static void SMARTCARD_EndTxTransfer(SMARTCARD_HandleTypeDef *hsmartcard)
2620 /* Disable TXEIE, TCIE and ERR (Frame error, noise error, overrun error) interrupts */
2621 CLEAR_BIT(hsmartcard->Instance->CR1, (USART_CR1_TXEIE_TXFNFIE | USART_CR1_TCIE));
2622 CLEAR_BIT(hsmartcard->Instance->CR3, USART_CR3_EIE);
2624 /* At end of Tx process, restore hsmartcard->gState to Ready */
2625 hsmartcard->gState = HAL_SMARTCARD_STATE_READY;
2630 * @brief End ongoing Rx transfer on UART peripheral (following error detection or Reception completion).
2631 * @param hsmartcard Pointer to a SMARTCARD_HandleTypeDef structure that contains
2632 * the configuration information for the specified SMARTCARD module.
2633 * @retval None
2635 static void SMARTCARD_EndRxTransfer(SMARTCARD_HandleTypeDef *hsmartcard)
2637 /* Disable RXNE, PE and ERR (Frame error, noise error, overrun error) interrupts */
2638 CLEAR_BIT(hsmartcard->Instance->CR1, (USART_CR1_RXNEIE_RXFNEIE | USART_CR1_PEIE));
2639 CLEAR_BIT(hsmartcard->Instance->CR3, USART_CR3_EIE);
2641 /* At end of Rx process, restore hsmartcard->RxState to Ready */
2642 hsmartcard->RxState = HAL_SMARTCARD_STATE_READY;
2647 * @brief DMA SMARTCARD transmit process complete callback.
2648 * @param hdma Pointer to a DMA_HandleTypeDef structure that contains
2649 * the configuration information for the specified DMA module.
2650 * @retval None
2652 static void SMARTCARD_DMATransmitCplt(DMA_HandleTypeDef *hdma)
2654 SMARTCARD_HandleTypeDef *hsmartcard = (SMARTCARD_HandleTypeDef *)(hdma->Parent);
2655 hsmartcard->TxXferCount = 0U;
2657 /* Disable the DMA transfer for transmit request by resetting the DMAT bit
2658 in the SMARTCARD associated USART CR3 register */
2659 CLEAR_BIT(hsmartcard->Instance->CR3, USART_CR3_DMAT);
2661 /* Enable the SMARTCARD Transmit Complete Interrupt */
2662 __HAL_SMARTCARD_ENABLE_IT(hsmartcard, hsmartcard->AdvancedInit.TxCompletionIndication);
2666 * @brief DMA SMARTCARD receive process complete callback.
2667 * @param hdma Pointer to a DMA_HandleTypeDef structure that contains
2668 * the configuration information for the specified DMA module.
2669 * @retval None
2671 static void SMARTCARD_DMAReceiveCplt(DMA_HandleTypeDef *hdma)
2673 SMARTCARD_HandleTypeDef *hsmartcard = (SMARTCARD_HandleTypeDef *)(hdma->Parent);
2674 hsmartcard->RxXferCount = 0U;
2676 /* Disable PE and ERR (Frame error, noise error, overrun error) interrupts */
2677 CLEAR_BIT(hsmartcard->Instance->CR1, USART_CR1_PEIE);
2678 CLEAR_BIT(hsmartcard->Instance->CR3, USART_CR3_EIE);
2680 /* Disable the DMA transfer for the receiver request by resetting the DMAR bit
2681 in the SMARTCARD associated USART CR3 register */
2682 CLEAR_BIT(hsmartcard->Instance->CR3, USART_CR3_DMAR);
2684 /* At end of Rx process, restore hsmartcard->RxState to Ready */
2685 hsmartcard->RxState = HAL_SMARTCARD_STATE_READY;
2687 #if (USE_HAL_SMARTCARD_REGISTER_CALLBACKS == 1)
2688 /* Call registered Rx complete callback */
2689 hsmartcard->RxCpltCallback(hsmartcard);
2690 #else
2691 /* Call legacy weak Rx complete callback */
2692 HAL_SMARTCARD_RxCpltCallback(hsmartcard);
2693 #endif /* USE_HAL_SMARTCARD_REGISTER_CALLBACK */
2697 * @brief DMA SMARTCARD communication error callback.
2698 * @param hdma Pointer to a DMA_HandleTypeDef structure that contains
2699 * the configuration information for the specified DMA module.
2700 * @retval None
2702 static void SMARTCARD_DMAError(DMA_HandleTypeDef *hdma)
2704 SMARTCARD_HandleTypeDef *hsmartcard = (SMARTCARD_HandleTypeDef *)(hdma->Parent);
2706 /* Stop SMARTCARD DMA Tx request if ongoing */
2707 if (hsmartcard->gState == HAL_SMARTCARD_STATE_BUSY_TX)
2709 if (HAL_IS_BIT_SET(hsmartcard->Instance->CR3, USART_CR3_DMAT))
2711 hsmartcard->TxXferCount = 0U;
2712 SMARTCARD_EndTxTransfer(hsmartcard);
2716 /* Stop SMARTCARD DMA Rx request if ongoing */
2717 if (hsmartcard->RxState == HAL_SMARTCARD_STATE_BUSY_RX)
2719 if (HAL_IS_BIT_SET(hsmartcard->Instance->CR3, USART_CR3_DMAR))
2721 hsmartcard->RxXferCount = 0U;
2722 SMARTCARD_EndRxTransfer(hsmartcard);
2726 hsmartcard->ErrorCode |= HAL_SMARTCARD_ERROR_DMA;
2727 #if (USE_HAL_SMARTCARD_REGISTER_CALLBACKS == 1)
2728 /* Call registered user error callback */
2729 hsmartcard->ErrorCallback(hsmartcard);
2730 #else
2731 /* Call legacy weak user error callback */
2732 HAL_SMARTCARD_ErrorCallback(hsmartcard);
2733 #endif /* USE_HAL_SMARTCARD_REGISTER_CALLBACK */
2737 * @brief DMA SMARTCARD communication abort callback, when initiated by HAL services on Error
2738 * (To be called at end of DMA Abort procedure following error occurrence).
2739 * @param hdma DMA handle.
2740 * @retval None
2742 static void SMARTCARD_DMAAbortOnError(DMA_HandleTypeDef *hdma)
2744 SMARTCARD_HandleTypeDef *hsmartcard = (SMARTCARD_HandleTypeDef *)(hdma->Parent);
2745 hsmartcard->RxXferCount = 0U;
2746 hsmartcard->TxXferCount = 0U;
2748 #if (USE_HAL_SMARTCARD_REGISTER_CALLBACKS == 1)
2749 /* Call registered user error callback */
2750 hsmartcard->ErrorCallback(hsmartcard);
2751 #else
2752 /* Call legacy weak user error callback */
2753 HAL_SMARTCARD_ErrorCallback(hsmartcard);
2754 #endif /* USE_HAL_SMARTCARD_REGISTER_CALLBACK */
2758 * @brief DMA SMARTCARD Tx communication abort callback, when initiated by user
2759 * (To be called at end of DMA Tx Abort procedure following user abort request).
2760 * @note When this callback is executed, User Abort complete call back is called only if no
2761 * Abort still ongoing for Rx DMA Handle.
2762 * @param hdma DMA handle.
2763 * @retval None
2765 static void SMARTCARD_DMATxAbortCallback(DMA_HandleTypeDef *hdma)
2767 SMARTCARD_HandleTypeDef *hsmartcard = (SMARTCARD_HandleTypeDef *)(hdma->Parent);
2769 hsmartcard->hdmatx->XferAbortCallback = NULL;
2771 /* Check if an Abort process is still ongoing */
2772 if (hsmartcard->hdmarx != NULL)
2774 if (hsmartcard->hdmarx->XferAbortCallback != NULL)
2776 return;
2780 /* No Abort process still ongoing : All DMA channels are aborted, call user Abort Complete callback */
2781 hsmartcard->TxXferCount = 0U;
2782 hsmartcard->RxXferCount = 0U;
2784 /* Reset errorCode */
2785 hsmartcard->ErrorCode = HAL_SMARTCARD_ERROR_NONE;
2787 /* Clear the Error flags in the ICR register */
2788 __HAL_SMARTCARD_CLEAR_FLAG(hsmartcard,
2789 SMARTCARD_CLEAR_OREF | SMARTCARD_CLEAR_NEF | SMARTCARD_CLEAR_PEF | SMARTCARD_CLEAR_FEF | SMARTCARD_CLEAR_RTOF |
2790 SMARTCARD_CLEAR_EOBF);
2792 /* Restore hsmartcard->gState and hsmartcard->RxState to Ready */
2793 hsmartcard->gState = HAL_SMARTCARD_STATE_READY;
2794 hsmartcard->RxState = HAL_SMARTCARD_STATE_READY;
2796 #if (USE_HAL_SMARTCARD_REGISTER_CALLBACKS == 1)
2797 /* Call registered Abort complete callback */
2798 hsmartcard->AbortCpltCallback(hsmartcard);
2799 #else
2800 /* Call legacy weak Abort complete callback */
2801 HAL_SMARTCARD_AbortCpltCallback(hsmartcard);
2802 #endif /* USE_HAL_SMARTCARD_REGISTER_CALLBACK */
2807 * @brief DMA SMARTCARD Rx communication abort callback, when initiated by user
2808 * (To be called at end of DMA Rx Abort procedure following user abort request).
2809 * @note When this callback is executed, User Abort complete call back is called only if no
2810 * Abort still ongoing for Tx DMA Handle.
2811 * @param hdma DMA handle.
2812 * @retval None
2814 static void SMARTCARD_DMARxAbortCallback(DMA_HandleTypeDef *hdma)
2816 SMARTCARD_HandleTypeDef *hsmartcard = (SMARTCARD_HandleTypeDef *)(hdma->Parent);
2818 hsmartcard->hdmarx->XferAbortCallback = NULL;
2820 /* Check if an Abort process is still ongoing */
2821 if (hsmartcard->hdmatx != NULL)
2823 if (hsmartcard->hdmatx->XferAbortCallback != NULL)
2825 return;
2829 /* No Abort process still ongoing : All DMA channels are aborted, call user Abort Complete callback */
2830 hsmartcard->TxXferCount = 0U;
2831 hsmartcard->RxXferCount = 0U;
2833 /* Reset errorCode */
2834 hsmartcard->ErrorCode = HAL_SMARTCARD_ERROR_NONE;
2836 /* Clear the Error flags in the ICR register */
2837 __HAL_SMARTCARD_CLEAR_FLAG(hsmartcard,
2838 SMARTCARD_CLEAR_OREF | SMARTCARD_CLEAR_NEF | SMARTCARD_CLEAR_PEF | SMARTCARD_CLEAR_FEF | SMARTCARD_CLEAR_RTOF |
2839 SMARTCARD_CLEAR_EOBF);
2841 /* Restore hsmartcard->gState and hsmartcard->RxState to Ready */
2842 hsmartcard->gState = HAL_SMARTCARD_STATE_READY;
2843 hsmartcard->RxState = HAL_SMARTCARD_STATE_READY;
2845 #if (USE_HAL_SMARTCARD_REGISTER_CALLBACKS == 1)
2846 /* Call registered Abort complete callback */
2847 hsmartcard->AbortCpltCallback(hsmartcard);
2848 #else
2849 /* Call legacy weak Abort complete callback */
2850 HAL_SMARTCARD_AbortCpltCallback(hsmartcard);
2851 #endif /* USE_HAL_SMARTCARD_REGISTER_CALLBACK */
2856 * @brief DMA SMARTCARD Tx communication abort callback, when initiated by user by a call to
2857 * HAL_SMARTCARD_AbortTransmit_IT API (Abort only Tx transfer)
2858 * (This callback is executed at end of DMA Tx Abort procedure following user abort request,
2859 * and leads to user Tx Abort Complete callback execution).
2860 * @param hdma DMA handle.
2861 * @retval None
2863 static void SMARTCARD_DMATxOnlyAbortCallback(DMA_HandleTypeDef *hdma)
2865 SMARTCARD_HandleTypeDef *hsmartcard = (SMARTCARD_HandleTypeDef *)(hdma->Parent);
2867 hsmartcard->TxXferCount = 0U;
2869 /* Clear the Error flags in the ICR register */
2870 __HAL_SMARTCARD_CLEAR_FLAG(hsmartcard, SMARTCARD_CLEAR_FEF);
2872 /* Restore hsmartcard->gState to Ready */
2873 hsmartcard->gState = HAL_SMARTCARD_STATE_READY;
2875 #if (USE_HAL_SMARTCARD_REGISTER_CALLBACKS == 1)
2876 /* Call registered Abort Transmit Complete Callback */
2877 hsmartcard->AbortTransmitCpltCallback(hsmartcard);
2878 #else
2879 /* Call legacy weak Abort Transmit Complete Callback */
2880 HAL_SMARTCARD_AbortTransmitCpltCallback(hsmartcard);
2881 #endif /* USE_HAL_SMARTCARD_REGISTER_CALLBACK */
2885 * @brief DMA SMARTCARD Rx communication abort callback, when initiated by user by a call to
2886 * HAL_SMARTCARD_AbortReceive_IT API (Abort only Rx transfer)
2887 * (This callback is executed at end of DMA Rx Abort procedure following user abort request,
2888 * and leads to user Rx Abort Complete callback execution).
2889 * @param hdma DMA handle.
2890 * @retval None
2892 static void SMARTCARD_DMARxOnlyAbortCallback(DMA_HandleTypeDef *hdma)
2894 SMARTCARD_HandleTypeDef *hsmartcard = (SMARTCARD_HandleTypeDef *)(hdma->Parent);
2896 hsmartcard->RxXferCount = 0U;
2898 /* Clear the Error flags in the ICR register */
2899 __HAL_SMARTCARD_CLEAR_FLAG(hsmartcard,
2900 SMARTCARD_CLEAR_OREF | SMARTCARD_CLEAR_NEF | SMARTCARD_CLEAR_PEF | SMARTCARD_CLEAR_FEF | SMARTCARD_CLEAR_RTOF |
2901 SMARTCARD_CLEAR_EOBF);
2903 /* Restore hsmartcard->RxState to Ready */
2904 hsmartcard->RxState = HAL_SMARTCARD_STATE_READY;
2906 #if (USE_HAL_SMARTCARD_REGISTER_CALLBACKS == 1)
2907 /* Call registered Abort Receive Complete Callback */
2908 hsmartcard->AbortReceiveCpltCallback(hsmartcard);
2909 #else
2910 /* Call legacy weak Abort Receive Complete Callback */
2911 HAL_SMARTCARD_AbortReceiveCpltCallback(hsmartcard);
2912 #endif /* USE_HAL_SMARTCARD_REGISTER_CALLBACK */
2916 * @brief Send an amount of data in non-blocking mode.
2917 * @note Function called under interruption only, once
2918 * interruptions have been enabled by HAL_SMARTCARD_Transmit_IT()
2919 * and when the FIFO mode is disabled.
2920 * @param hsmartcard Pointer to a SMARTCARD_HandleTypeDef structure that contains
2921 * the configuration information for the specified SMARTCARD module.
2922 * @retval None
2924 static void SMARTCARD_TxISR(SMARTCARD_HandleTypeDef *hsmartcard)
2926 /* Check that a Tx process is ongoing */
2927 if (hsmartcard->gState == HAL_SMARTCARD_STATE_BUSY_TX)
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
2939 hsmartcard->Instance->TDR = (uint8_t)(*hsmartcard->pTxBuffPtr & 0xFFU);
2940 hsmartcard->pTxBuffPtr++;
2941 hsmartcard->TxXferCount--;
2947 * @brief Send an amount of data in non-blocking mode.
2948 * @note Function called under interruption only, once
2949 * interruptions have been enabled by HAL_SMARTCARD_Transmit_IT()
2950 * and when the FIFO mode is enabled.
2951 * @param hsmartcard Pointer to a SMARTCARD_HandleTypeDef structure that contains
2952 * the configuration information for the specified SMARTCARD module.
2953 * @retval None
2955 static void SMARTCARD_TxISR_FIFOEN(SMARTCARD_HandleTypeDef *hsmartcard)
2957 uint16_t nb_tx_data;
2959 /* Check that a Tx process is ongoing */
2960 if (hsmartcard->gState == HAL_SMARTCARD_STATE_BUSY_TX)
2962 for (nb_tx_data = hsmartcard->NbTxDataToProcess ; nb_tx_data > 0U ; nb_tx_data--)
2964 if (hsmartcard->TxXferCount == 0U)
2966 /* Disable the SMARTCARD Transmit Data Register Empty Interrupt */
2967 CLEAR_BIT(hsmartcard->Instance->CR1, USART_CR1_TXEIE_TXFNFIE);
2969 /* Enable the SMARTCARD Transmit Complete Interrupt */
2970 __HAL_SMARTCARD_ENABLE_IT(hsmartcard, hsmartcard->AdvancedInit.TxCompletionIndication);
2972 else if (READ_BIT(hsmartcard->Instance->ISR, USART_ISR_TXE_TXFNF) != 0U)
2974 hsmartcard->Instance->TDR = (uint8_t)(*hsmartcard->pTxBuffPtr & 0xFFU);
2975 hsmartcard->pTxBuffPtr++;
2976 hsmartcard->TxXferCount--;
2978 else
2980 /* Nothing to do */
2987 * @brief Wrap up transmission in non-blocking mode.
2988 * @param hsmartcard Pointer to a SMARTCARD_HandleTypeDef structure that contains
2989 * the configuration information for the specified SMARTCARD module.
2990 * @retval None
2992 static void SMARTCARD_EndTransmit_IT(SMARTCARD_HandleTypeDef *hsmartcard)
2994 /* Disable the SMARTCARD Transmit Complete Interrupt */
2995 __HAL_SMARTCARD_DISABLE_IT(hsmartcard, hsmartcard->AdvancedInit.TxCompletionIndication);
2997 /* Check if a receive process is ongoing or not. If not disable ERR IT */
2998 if (hsmartcard->RxState == HAL_SMARTCARD_STATE_READY)
3000 /* Disable the SMARTCARD Error Interrupt: (Frame error) */
3001 CLEAR_BIT(hsmartcard->Instance->CR3, USART_CR3_EIE);
3004 /* Disable the Peripheral first to update mode */
3005 CLEAR_BIT(hsmartcard->Instance->CR1, USART_CR1_UE);
3006 if ((hsmartcard->Init.Mode == SMARTCARD_MODE_TX)
3007 && (hsmartcard->Init.NACKEnable == SMARTCARD_NACK_ENABLE))
3009 /* In case of TX only mode, if NACK is enabled, receiver block has been enabled
3010 for Transmit phase. Disable this receiver block. */
3011 CLEAR_BIT(hsmartcard->Instance->CR1, USART_CR1_RE);
3013 if ((hsmartcard->Init.Mode == SMARTCARD_MODE_TX_RX)
3014 || (hsmartcard->Init.NACKEnable == SMARTCARD_NACK_ENABLE))
3016 /* Perform a TX FIFO Flush at end of Tx phase, as all sent bytes are appearing in Rx Data register */
3017 __HAL_SMARTCARD_FLUSH_DRREGISTER(hsmartcard);
3019 SET_BIT(hsmartcard->Instance->CR1, USART_CR1_UE);
3021 /* Tx process is ended, restore hsmartcard->gState to Ready */
3022 hsmartcard->gState = HAL_SMARTCARD_STATE_READY;
3024 /* Clear TxISR function pointer */
3025 hsmartcard->TxISR = NULL;
3027 #if (USE_HAL_SMARTCARD_REGISTER_CALLBACKS == 1)
3028 /* Call registered Tx complete callback */
3029 hsmartcard->TxCpltCallback(hsmartcard);
3030 #else
3031 /* Call legacy weak Tx complete callback */
3032 HAL_SMARTCARD_TxCpltCallback(hsmartcard);
3033 #endif /* USE_HAL_SMARTCARD_REGISTER_CALLBACK */
3037 * @brief Receive an amount of data in non-blocking mode.
3038 * @note Function called under interruption only, once
3039 * interruptions have been enabled by HAL_SMARTCARD_Receive_IT()
3040 * and when the FIFO mode is disabled.
3041 * @param hsmartcard Pointer to a SMARTCARD_HandleTypeDef structure that contains
3042 * the configuration information for the specified SMARTCARD module.
3043 * @retval None
3045 static void SMARTCARD_RxISR(SMARTCARD_HandleTypeDef *hsmartcard)
3047 /* Check that a Rx process is ongoing */
3048 if (hsmartcard->RxState == HAL_SMARTCARD_STATE_BUSY_RX)
3050 *hsmartcard->pRxBuffPtr = (uint8_t)(hsmartcard->Instance->RDR & (uint8_t)0xFF);
3051 hsmartcard->pRxBuffPtr++;
3053 hsmartcard->RxXferCount--;
3054 if (hsmartcard->RxXferCount == 0U)
3056 CLEAR_BIT(hsmartcard->Instance->CR1, USART_CR1_RXNEIE_RXFNEIE);
3058 /* Check if a transmit process is ongoing or not. If not disable ERR IT */
3059 if (hsmartcard->gState == HAL_SMARTCARD_STATE_READY)
3061 /* Disable the SMARTCARD Error Interrupt: (Frame error, noise error, overrun error) */
3062 CLEAR_BIT(hsmartcard->Instance->CR3, USART_CR3_EIE);
3065 /* Disable the SMARTCARD Parity Error Interrupt */
3066 CLEAR_BIT(hsmartcard->Instance->CR1, USART_CR1_PEIE);
3068 hsmartcard->RxState = HAL_SMARTCARD_STATE_READY;
3070 /* Clear RxISR function pointer */
3071 hsmartcard->RxISR = NULL;
3073 #if (USE_HAL_SMARTCARD_REGISTER_CALLBACKS == 1)
3074 /* Call registered Rx complete callback */
3075 hsmartcard->RxCpltCallback(hsmartcard);
3076 #else
3077 /* Call legacy weak Rx complete callback */
3078 HAL_SMARTCARD_RxCpltCallback(hsmartcard);
3079 #endif /* USE_HAL_SMARTCARD_REGISTER_CALLBACK */
3082 else
3084 /* Clear RXNE interrupt flag */
3085 __HAL_SMARTCARD_SEND_REQ(hsmartcard, SMARTCARD_RXDATA_FLUSH_REQUEST);
3090 * @brief Receive an amount of data in non-blocking mode.
3091 * @note Function called under interruption only, once
3092 * interruptions have been enabled by HAL_SMARTCARD_Receive_IT()
3093 * and when the FIFO mode is enabled.
3094 * @param hsmartcard Pointer to a SMARTCARD_HandleTypeDef structure that contains
3095 * the configuration information for the specified SMARTCARD module.
3096 * @retval None
3098 static void SMARTCARD_RxISR_FIFOEN(SMARTCARD_HandleTypeDef *hsmartcard)
3100 uint16_t nb_rx_data;
3101 uint16_t rxdatacount;
3103 /* Check that a Rx process is ongoing */
3104 if (hsmartcard->RxState == HAL_SMARTCARD_STATE_BUSY_RX)
3106 for (nb_rx_data = hsmartcard->NbRxDataToProcess ; nb_rx_data > 0U ; nb_rx_data--)
3108 *hsmartcard->pRxBuffPtr = (uint8_t)(hsmartcard->Instance->RDR & (uint8_t)0xFF);
3109 hsmartcard->pRxBuffPtr++;
3111 hsmartcard->RxXferCount--;
3112 if (hsmartcard->RxXferCount == 0U)
3114 CLEAR_BIT(hsmartcard->Instance->CR1, USART_CR1_RXNEIE_RXFNEIE);
3116 /* Check if a transmit process is ongoing or not. If not disable ERR IT */
3117 if (hsmartcard->gState == HAL_SMARTCARD_STATE_READY)
3119 /* Disable the SMARTCARD Error Interrupt: (Frame error, noise error, overrun error) */
3120 CLEAR_BIT(hsmartcard->Instance->CR3, USART_CR3_EIE);
3123 /* Disable the SMARTCARD Parity Error Interrupt */
3124 CLEAR_BIT(hsmartcard->Instance->CR1, USART_CR1_PEIE);
3126 hsmartcard->RxState = HAL_SMARTCARD_STATE_READY;
3128 /* Clear RxISR function pointer */
3129 hsmartcard->RxISR = NULL;
3131 #if (USE_HAL_SMARTCARD_REGISTER_CALLBACKS == 1)
3132 /* Call registered Rx complete callback */
3133 hsmartcard->RxCpltCallback(hsmartcard);
3134 #else
3135 /* Call legacy weak Rx complete callback */
3136 HAL_SMARTCARD_RxCpltCallback(hsmartcard);
3137 #endif /* USE_HAL_SMARTCARD_REGISTER_CALLBACK */
3141 /* When remaining number of bytes to receive is less than the RX FIFO
3142 threshold, next incoming frames are processed as if FIFO mode was
3143 disabled (i.e. one interrupt per received frame).
3145 rxdatacount = hsmartcard->RxXferCount;
3146 if (((rxdatacount != 0U)) && (rxdatacount < hsmartcard->NbRxDataToProcess))
3148 /* Disable the UART RXFT interrupt*/
3149 CLEAR_BIT(hsmartcard->Instance->CR3, USART_CR3_RXFTIE);
3151 /* Update the RxISR function pointer */
3152 hsmartcard->RxISR = SMARTCARD_RxISR;
3154 /* Enable the UART Data Register Not Empty interrupt */
3155 SET_BIT(hsmartcard->Instance->CR1, USART_CR1_RXNEIE_RXFNEIE);
3158 else
3160 /* Clear RXNE interrupt flag */
3161 __HAL_SMARTCARD_SEND_REQ(hsmartcard, SMARTCARD_RXDATA_FLUSH_REQUEST);
3166 * @}
3169 #endif /* HAL_SMARTCARD_MODULE_ENABLED */
3171 * @}
3175 * @}
3178 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/