2 ******************************************************************************
3 * @file stm32f7xx_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
14 ==============================================================================
15 ##### How to use this driver #####
16 ==============================================================================
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.
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.
56 [..] Three operation modes are available within this driver :
58 *** Polling mode IO operation ***
59 =================================
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 ===================================
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 ==============================
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 ========================================
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
100 (@) You can refer to the SMARTCARD HAL driver header file for more useful macros
102 ##### Callback registration #####
103 ==================================
106 The compilation define USE_HAL_SMARTCARD_REGISTER_CALLBACKS when set to 1
107 allows the user to configure dynamically the driver callbacks.
110 Use Function @ref HAL_SMARTCARD_RegisterCallback() to register a user callback.
111 Function @ref HAL_SMARTCARD_RegisterCallback() allows to register following callbacks:
112 (+) TxCpltCallback : Tx Complete Callback.
113 (+) RxCpltCallback : Rx Complete Callback.
114 (+) ErrorCallback : Error Callback.
115 (+) AbortCpltCallback : Abort Complete Callback.
116 (+) AbortTransmitCpltCallback : Abort Transmit Complete Callback.
117 (+) AbortReceiveCpltCallback : Abort Receive Complete Callback.
118 (+) MspInitCallback : SMARTCARD MspInit.
119 (+) MspDeInitCallback : SMARTCARD MspDeInit.
120 This function takes as parameters the HAL peripheral handle, the Callback ID
121 and a pointer to the user callback function.
124 Use function @ref HAL_SMARTCARD_UnRegisterCallback() to reset a callback to the default
125 weak (surcharged) function.
126 @ref HAL_SMARTCARD_UnRegisterCallback() takes as parameters the HAL peripheral handle,
128 This function allows to reset following callbacks:
129 (+) TxCpltCallback : Tx Complete Callback.
130 (+) RxCpltCallback : Rx Complete Callback.
131 (+) ErrorCallback : Error Callback.
132 (+) AbortCpltCallback : Abort Complete Callback.
133 (+) AbortTransmitCpltCallback : Abort Transmit Complete Callback.
134 (+) AbortReceiveCpltCallback : Abort Receive Complete Callback.
135 (+) MspInitCallback : SMARTCARD MspInit.
136 (+) MspDeInitCallback : SMARTCARD MspDeInit.
139 By default, after the @ref HAL_SMARTCARD_Init() and when the state is HAL_SMARTCARD_STATE_RESET
140 all callbacks are set to the corresponding weak (surcharged) functions:
141 examples @ref HAL_SMARTCARD_TxCpltCallback(), @ref HAL_SMARTCARD_RxCpltCallback().
142 Exception done for MspInit and MspDeInit functions that are respectively
143 reset to the legacy weak (surcharged) functions in the @ref HAL_SMARTCARD_Init()
144 and @ref HAL_SMARTCARD_DeInit() only when these callbacks are null (not registered beforehand).
145 If not, MspInit or MspDeInit are not null, the @ref HAL_SMARTCARD_Init() and @ref HAL_SMARTCARD_DeInit()
146 keep and use the user MspInit/MspDeInit callbacks (registered beforehand).
149 Callbacks can be registered/unregistered in HAL_SMARTCARD_STATE_READY state only.
150 Exception done MspInit/MspDeInit that can be registered/unregistered
151 in HAL_SMARTCARD_STATE_READY or HAL_SMARTCARD_STATE_RESET state, thus registered (user)
152 MspInit/DeInit callbacks can be used during the Init/DeInit.
153 In that case first register the MspInit/MspDeInit user callbacks
154 using @ref HAL_SMARTCARD_RegisterCallback() before calling @ref HAL_SMARTCARD_DeInit()
155 or @ref HAL_SMARTCARD_Init() function.
158 When The compilation define USE_HAL_SMARTCARD_REGISTER_CALLBACKS is set to 0 or
159 not defined, the callback registration feature is not available
160 and weak (surcharged) callbacks are used.
164 ******************************************************************************
167 * <h2><center>© Copyright (c) 2017 STMicroelectronics.
168 * All rights reserved.</center></h2>
170 * This software component is licensed by ST under BSD 3-Clause license,
171 * the "License"; You may not use this file except in compliance with the
172 * License. You may obtain a copy of the License at:
173 * opensource.org/licenses/BSD-3-Clause
175 ******************************************************************************
178 /* Includes ------------------------------------------------------------------*/
179 #include "stm32f7xx_hal.h"
181 /** @addtogroup STM32F7xx_HAL_Driver
185 /** @defgroup SMARTCARD SMARTCARD
186 * @brief HAL SMARTCARD module driver
190 #ifdef HAL_SMARTCARD_MODULE_ENABLED
192 /* Private typedef -----------------------------------------------------------*/
193 /* Private define ------------------------------------------------------------*/
194 /** @defgroup SMARTCARD_Private_Constants SMARTCARD Private Constants
197 #define SMARTCARD_TEACK_REACK_TIMEOUT 1000U /*!< SMARTCARD TX or RX enable acknowledge time-out value */
199 #define USART_CR1_FIELDS ((uint32_t)(USART_CR1_M | USART_CR1_PCE | USART_CR1_PS | \
200 USART_CR1_TE | USART_CR1_RE | USART_CR1_OVER8)) /*!< USART CR1 fields of parameters set by SMARTCARD_SetConfig API */
202 #define USART_CR2_CLK_FIELDS ((uint32_t)(USART_CR2_CLKEN | USART_CR2_CPOL | USART_CR2_CPHA | \
203 USART_CR2_LBCL)) /*!< SMARTCARD clock-related USART CR2 fields of parameters */
205 #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 */
207 #define USART_CR3_FIELDS ((uint32_t)(USART_CR3_ONEBIT | USART_CR3_NACK | USART_CR3_SCARCNT)) /*!< USART CR3 fields of parameters set by SMARTCARD_SetConfig API */
209 #define USART_BRR_MIN 0x10U /*!< USART BRR minimum authorized value */
211 #define USART_BRR_MAX 0x0000FFFFU /*!< USART BRR maximum authorized value */
216 /* Private macros ------------------------------------------------------------*/
217 /* Private variables ---------------------------------------------------------*/
218 /* Private function prototypes -----------------------------------------------*/
219 /** @addtogroup SMARTCARD_Private_Functions
222 #if (USE_HAL_SMARTCARD_REGISTER_CALLBACKS == 1)
223 void SMARTCARD_InitCallbacksToDefault(SMARTCARD_HandleTypeDef
*hsmartcard
);
224 #endif /* USE_HAL_SMARTCARD_REGISTER_CALLBACKS */
225 static HAL_StatusTypeDef
SMARTCARD_SetConfig(SMARTCARD_HandleTypeDef
*hsmartcard
);
226 static void SMARTCARD_AdvFeatureConfig(SMARTCARD_HandleTypeDef
*hsmartcard
);
227 static HAL_StatusTypeDef
SMARTCARD_CheckIdleState(SMARTCARD_HandleTypeDef
*hsmartcard
);
228 static HAL_StatusTypeDef
SMARTCARD_WaitOnFlagUntilTimeout(SMARTCARD_HandleTypeDef
*hsmartcard
, uint32_t Flag
, FlagStatus Status
, uint32_t Tickstart
, uint32_t Timeout
);
229 static void SMARTCARD_EndTxTransfer(SMARTCARD_HandleTypeDef
*hsmartcard
);
230 static void SMARTCARD_EndRxTransfer(SMARTCARD_HandleTypeDef
*hsmartcard
);
231 static void SMARTCARD_DMATransmitCplt(DMA_HandleTypeDef
*hdma
);
232 static void SMARTCARD_DMAReceiveCplt(DMA_HandleTypeDef
*hdma
);
233 static void SMARTCARD_DMAError(DMA_HandleTypeDef
*hdma
);
234 static void SMARTCARD_DMAAbortOnError(DMA_HandleTypeDef
*hdma
);
235 static void SMARTCARD_DMATxAbortCallback(DMA_HandleTypeDef
*hdma
);
236 static void SMARTCARD_DMARxAbortCallback(DMA_HandleTypeDef
*hdma
);
237 static void SMARTCARD_DMATxOnlyAbortCallback(DMA_HandleTypeDef
*hdma
);
238 static void SMARTCARD_DMARxOnlyAbortCallback(DMA_HandleTypeDef
*hdma
);
239 static void SMARTCARD_TxISR(SMARTCARD_HandleTypeDef
*hsmartcard
);
240 static void SMARTCARD_EndTransmit_IT(SMARTCARD_HandleTypeDef
*hsmartcard
);
241 static void SMARTCARD_RxISR(SMARTCARD_HandleTypeDef
*hsmartcard
);
246 /* Exported functions --------------------------------------------------------*/
248 /** @defgroup SMARTCARD_Exported_Functions SMARTCARD Exported Functions
252 /** @defgroup SMARTCARD_Exported_Functions_Group1 Initialization and de-initialization functions
253 * @brief Initialization and Configuration functions
256 ==============================================================================
257 ##### Initialization and Configuration functions #####
258 ==============================================================================
260 This subsection provides a set of functions allowing to initialize the USARTx
261 associated to the SmartCard.
262 (+) These parameters can be configured:
264 (++) Parity: parity should be enabled, frame Length is fixed to 8 bits plus parity
265 (++) Receiver/transmitter modes
266 (++) Synchronous mode (and if enabled, phase, polarity and last bit parameters)
269 (++) NACK enabling or disabling on transmission error
271 (+) The following advanced features can be configured as well:
272 (++) TX and/or RX pin level inversion
273 (++) data logical level inversion
274 (++) RX and TX pins swap
275 (++) RX overrun detection disabling
276 (++) DMA disabling on RX error
277 (++) MSB first on communication line
278 (++) Time out enabling (and if activated, timeout value)
280 (++) Auto-retry counter
282 The HAL_SMARTCARD_Init() API follows the USART synchronous configuration procedures
283 (details for the procedures are available in reference manual).
287 The USART frame format is given in the following table:
289 Table 1. USART frame format.
290 +---------------------------------------------------------------+
291 | M1M0 bits | PCE bit | USART frame |
292 |-----------------------|---------------------------------------|
293 | 01 | 1 | | SB | 8 bit data | PB | STB | |
294 +---------------------------------------------------------------+
301 * @brief Initialize the SMARTCARD mode according to the specified
302 * parameters in the SMARTCARD_HandleTypeDef and initialize the associated handle.
303 * @param hsmartcard Pointer to a SMARTCARD_HandleTypeDef structure that contains
304 * the configuration information for the specified SMARTCARD module.
307 HAL_StatusTypeDef
HAL_SMARTCARD_Init(SMARTCARD_HandleTypeDef
*hsmartcard
)
309 /* Check the SMARTCARD handle allocation */
310 if (hsmartcard
== NULL
)
315 /* Check the USART associated to the SMARTCARD handle */
316 assert_param(IS_SMARTCARD_INSTANCE(hsmartcard
->Instance
));
318 if (hsmartcard
->gState
== HAL_SMARTCARD_STATE_RESET
)
320 /* Allocate lock resource and initialize it */
321 hsmartcard
->Lock
= HAL_UNLOCKED
;
323 #if USE_HAL_SMARTCARD_REGISTER_CALLBACKS == 1
324 SMARTCARD_InitCallbacksToDefault(hsmartcard
);
326 if (hsmartcard
->MspInitCallback
== NULL
)
328 hsmartcard
->MspInitCallback
= HAL_SMARTCARD_MspInit
;
331 /* Init the low level hardware */
332 hsmartcard
->MspInitCallback(hsmartcard
);
334 /* Init the low level hardware : GPIO, CLOCK */
335 HAL_SMARTCARD_MspInit(hsmartcard
);
336 #endif /* USE_HAL_SMARTCARD_REGISTER_CALLBACKS */
339 hsmartcard
->gState
= HAL_SMARTCARD_STATE_BUSY
;
341 /* Disable the Peripheral to set smartcard mode */
342 CLEAR_BIT(hsmartcard
->Instance
->CR1
, USART_CR1_UE
);
344 /* In SmartCard mode, the following bits must be kept cleared:
345 - LINEN in the USART_CR2 register,
346 - HDSEL and IREN bits in the USART_CR3 register.*/
347 CLEAR_BIT(hsmartcard
->Instance
->CR2
, USART_CR2_LINEN
);
348 CLEAR_BIT(hsmartcard
->Instance
->CR3
, (USART_CR3_HDSEL
| USART_CR3_IREN
));
350 /* set the USART in SMARTCARD mode */
351 SET_BIT(hsmartcard
->Instance
->CR3
, USART_CR3_SCEN
);
353 /* Set the SMARTCARD Communication parameters */
354 if (SMARTCARD_SetConfig(hsmartcard
) == HAL_ERROR
)
359 /* Set the SMARTCARD transmission completion indication */
360 SMARTCARD_TRANSMISSION_COMPLETION_SETTING(hsmartcard
);
362 if (hsmartcard
->AdvancedInit
.AdvFeatureInit
!= SMARTCARD_ADVFEATURE_NO_INIT
)
364 SMARTCARD_AdvFeatureConfig(hsmartcard
);
367 /* Enable the Peripheral */
368 SET_BIT(hsmartcard
->Instance
->CR1
, USART_CR1_UE
);
370 /* TEACK and/or REACK to check before moving hsmartcard->gState and hsmartcard->RxState to Ready */
371 return (SMARTCARD_CheckIdleState(hsmartcard
));
375 * @brief DeInitialize the SMARTCARD peripheral.
376 * @param hsmartcard Pointer to a SMARTCARD_HandleTypeDef structure that contains
377 * the configuration information for the specified SMARTCARD module.
380 HAL_StatusTypeDef
HAL_SMARTCARD_DeInit(SMARTCARD_HandleTypeDef
*hsmartcard
)
382 /* Check the SMARTCARD handle allocation */
383 if (hsmartcard
== NULL
)
388 /* Check the USART/UART associated to the SMARTCARD handle */
389 assert_param(IS_SMARTCARD_INSTANCE(hsmartcard
->Instance
));
391 hsmartcard
->gState
= HAL_SMARTCARD_STATE_BUSY
;
393 /* Disable the Peripheral */
394 CLEAR_BIT(hsmartcard
->Instance
->CR1
, USART_CR1_UE
);
396 WRITE_REG(hsmartcard
->Instance
->CR1
, 0x0U
);
397 WRITE_REG(hsmartcard
->Instance
->CR2
, 0x0U
);
398 WRITE_REG(hsmartcard
->Instance
->CR3
, 0x0U
);
399 WRITE_REG(hsmartcard
->Instance
->RTOR
, 0x0U
);
400 WRITE_REG(hsmartcard
->Instance
->GTPR
, 0x0U
);
402 /* DeInit the low level hardware */
403 #if USE_HAL_SMARTCARD_REGISTER_CALLBACKS == 1
404 if (hsmartcard
->MspDeInitCallback
== NULL
)
406 hsmartcard
->MspDeInitCallback
= HAL_SMARTCARD_MspDeInit
;
408 /* DeInit the low level hardware */
409 hsmartcard
->MspDeInitCallback(hsmartcard
);
411 HAL_SMARTCARD_MspDeInit(hsmartcard
);
412 #endif /* USE_HAL_SMARTCARD_REGISTER_CALLBACKS */
414 hsmartcard
->ErrorCode
= HAL_SMARTCARD_ERROR_NONE
;
415 hsmartcard
->gState
= HAL_SMARTCARD_STATE_RESET
;
416 hsmartcard
->RxState
= HAL_SMARTCARD_STATE_RESET
;
419 __HAL_UNLOCK(hsmartcard
);
425 * @brief Initialize the SMARTCARD MSP.
426 * @param hsmartcard Pointer to a SMARTCARD_HandleTypeDef structure that contains
427 * the configuration information for the specified SMARTCARD module.
430 __weak
void HAL_SMARTCARD_MspInit(SMARTCARD_HandleTypeDef
*hsmartcard
)
432 /* Prevent unused argument(s) compilation warning */
435 /* NOTE : This function should not be modified, when the callback is needed,
436 the HAL_SMARTCARD_MspInit can be implemented in the user file
441 * @brief DeInitialize the SMARTCARD MSP.
442 * @param hsmartcard Pointer to a SMARTCARD_HandleTypeDef structure that contains
443 * the configuration information for the specified SMARTCARD module.
446 __weak
void HAL_SMARTCARD_MspDeInit(SMARTCARD_HandleTypeDef
*hsmartcard
)
448 /* Prevent unused argument(s) compilation warning */
451 /* NOTE : This function should not be modified, when the callback is needed,
452 the HAL_SMARTCARD_MspDeInit can be implemented in the user file
456 #if (USE_HAL_SMARTCARD_REGISTER_CALLBACKS == 1)
458 * @brief Register a User SMARTCARD Callback
459 * To be used instead of the weak predefined callback
460 * @param hsmartcard smartcard handle
461 * @param CallbackID ID of the callback to be registered
462 * This parameter can be one of the following values:
463 * @arg @ref HAL_SMARTCARD_TX_COMPLETE_CB_ID Tx Complete Callback ID
464 * @arg @ref HAL_SMARTCARD_RX_COMPLETE_CB_ID Rx Complete Callback ID
465 * @arg @ref HAL_SMARTCARD_ERROR_CB_ID Error Callback ID
466 * @arg @ref HAL_SMARTCARD_ABORT_COMPLETE_CB_ID Abort Complete Callback ID
467 * @arg @ref HAL_SMARTCARD_ABORT_TRANSMIT_COMPLETE_CB_ID Abort Transmit Complete Callback ID
468 * @arg @ref HAL_SMARTCARD_ABORT_RECEIVE_COMPLETE_CB_ID Abort Receive Complete Callback ID
469 * @arg @ref HAL_SMARTCARD_MSPINIT_CB_ID MspInit Callback ID
470 * @arg @ref HAL_SMARTCARD_MSPDEINIT_CB_ID MspDeInit Callback ID
471 * @param pCallback pointer to the Callback function
474 HAL_StatusTypeDef
HAL_SMARTCARD_RegisterCallback(SMARTCARD_HandleTypeDef
*hsmartcard
, HAL_SMARTCARD_CallbackIDTypeDef CallbackID
, pSMARTCARD_CallbackTypeDef pCallback
)
476 HAL_StatusTypeDef status
= HAL_OK
;
478 if (pCallback
== NULL
)
480 /* Update the error code */
481 hsmartcard
->ErrorCode
|= HAL_SMARTCARD_ERROR_INVALID_CALLBACK
;
486 __HAL_LOCK(hsmartcard
);
488 if (hsmartcard
->gState
== HAL_SMARTCARD_STATE_READY
)
493 case HAL_SMARTCARD_TX_COMPLETE_CB_ID
:
494 hsmartcard
->TxCpltCallback
= pCallback
;
497 case HAL_SMARTCARD_RX_COMPLETE_CB_ID
:
498 hsmartcard
->RxCpltCallback
= pCallback
;
501 case HAL_SMARTCARD_ERROR_CB_ID
:
502 hsmartcard
->ErrorCallback
= pCallback
;
505 case HAL_SMARTCARD_ABORT_COMPLETE_CB_ID
:
506 hsmartcard
->AbortCpltCallback
= pCallback
;
509 case HAL_SMARTCARD_ABORT_TRANSMIT_COMPLETE_CB_ID
:
510 hsmartcard
->AbortTransmitCpltCallback
= pCallback
;
513 case HAL_SMARTCARD_ABORT_RECEIVE_COMPLETE_CB_ID
:
514 hsmartcard
->AbortReceiveCpltCallback
= pCallback
;
518 case HAL_SMARTCARD_MSPINIT_CB_ID
:
519 hsmartcard
->MspInitCallback
= pCallback
;
522 case HAL_SMARTCARD_MSPDEINIT_CB_ID
:
523 hsmartcard
->MspDeInitCallback
= pCallback
;
527 /* Update the error code */
528 hsmartcard
->ErrorCode
|= HAL_SMARTCARD_ERROR_INVALID_CALLBACK
;
530 /* Return error status */
535 else if (hsmartcard
->gState
== HAL_SMARTCARD_STATE_RESET
)
539 case HAL_SMARTCARD_MSPINIT_CB_ID
:
540 hsmartcard
->MspInitCallback
= pCallback
;
543 case HAL_SMARTCARD_MSPDEINIT_CB_ID
:
544 hsmartcard
->MspDeInitCallback
= pCallback
;
548 /* Update the error code */
549 hsmartcard
->ErrorCode
|= HAL_SMARTCARD_ERROR_INVALID_CALLBACK
;
551 /* Return error status */
558 /* Update the error code */
559 hsmartcard
->ErrorCode
|= HAL_SMARTCARD_ERROR_INVALID_CALLBACK
;
561 /* Return error status */
566 __HAL_UNLOCK(hsmartcard
);
572 * @brief Unregister an SMARTCARD callback
573 * SMARTCARD callback is redirected to the weak predefined callback
574 * @param hsmartcard smartcard handle
575 * @param CallbackID ID of the callback to be unregistered
576 * This parameter can be one of the following values:
577 * @arg @ref HAL_SMARTCARD_TX_COMPLETE_CB_ID Tx Complete Callback ID
578 * @arg @ref HAL_SMARTCARD_RX_COMPLETE_CB_ID Rx Complete Callback ID
579 * @arg @ref HAL_SMARTCARD_ERROR_CB_ID Error Callback ID
580 * @arg @ref HAL_SMARTCARD_ABORT_COMPLETE_CB_ID Abort Complete Callback ID
581 * @arg @ref HAL_SMARTCARD_ABORT_TRANSMIT_COMPLETE_CB_ID Abort Transmit Complete Callback ID
582 * @arg @ref HAL_SMARTCARD_ABORT_RECEIVE_COMPLETE_CB_ID Abort Receive Complete Callback ID
583 * @arg @ref HAL_SMARTCARD_MSPINIT_CB_ID MspInit Callback ID
584 * @arg @ref HAL_SMARTCARD_MSPDEINIT_CB_ID MspDeInit Callback ID
587 HAL_StatusTypeDef
HAL_SMARTCARD_UnRegisterCallback(SMARTCARD_HandleTypeDef
*hsmartcard
, HAL_SMARTCARD_CallbackIDTypeDef CallbackID
)
589 HAL_StatusTypeDef status
= HAL_OK
;
592 __HAL_LOCK(hsmartcard
);
594 if (HAL_SMARTCARD_STATE_READY
== hsmartcard
->gState
)
598 case HAL_SMARTCARD_TX_COMPLETE_CB_ID
:
599 hsmartcard
->TxCpltCallback
= HAL_SMARTCARD_TxCpltCallback
; /* Legacy weak TxCpltCallback */
602 case HAL_SMARTCARD_RX_COMPLETE_CB_ID
:
603 hsmartcard
->RxCpltCallback
= HAL_SMARTCARD_RxCpltCallback
; /* Legacy weak RxCpltCallback */
606 case HAL_SMARTCARD_ERROR_CB_ID
:
607 hsmartcard
->ErrorCallback
= HAL_SMARTCARD_ErrorCallback
; /* Legacy weak ErrorCallback */
610 case HAL_SMARTCARD_ABORT_COMPLETE_CB_ID
:
611 hsmartcard
->AbortCpltCallback
= HAL_SMARTCARD_AbortCpltCallback
; /* Legacy weak AbortCpltCallback */
614 case HAL_SMARTCARD_ABORT_TRANSMIT_COMPLETE_CB_ID
:
615 hsmartcard
->AbortTransmitCpltCallback
= HAL_SMARTCARD_AbortTransmitCpltCallback
; /* Legacy weak AbortTransmitCpltCallback */
618 case HAL_SMARTCARD_ABORT_RECEIVE_COMPLETE_CB_ID
:
619 hsmartcard
->AbortReceiveCpltCallback
= HAL_SMARTCARD_AbortReceiveCpltCallback
; /* Legacy weak AbortReceiveCpltCallback */
623 case HAL_SMARTCARD_MSPINIT_CB_ID
:
624 hsmartcard
->MspInitCallback
= HAL_SMARTCARD_MspInit
; /* Legacy weak MspInitCallback */
627 case HAL_SMARTCARD_MSPDEINIT_CB_ID
:
628 hsmartcard
->MspDeInitCallback
= HAL_SMARTCARD_MspDeInit
; /* Legacy weak MspDeInitCallback */
632 /* Update the error code */
633 hsmartcard
->ErrorCode
|= HAL_SMARTCARD_ERROR_INVALID_CALLBACK
;
635 /* Return error status */
640 else if (HAL_SMARTCARD_STATE_RESET
== hsmartcard
->gState
)
644 case HAL_SMARTCARD_MSPINIT_CB_ID
:
645 hsmartcard
->MspInitCallback
= HAL_SMARTCARD_MspInit
;
648 case HAL_SMARTCARD_MSPDEINIT_CB_ID
:
649 hsmartcard
->MspDeInitCallback
= HAL_SMARTCARD_MspDeInit
;
653 /* Update the error code */
654 hsmartcard
->ErrorCode
|= HAL_SMARTCARD_ERROR_INVALID_CALLBACK
;
656 /* Return error status */
663 /* Update the error code */
664 hsmartcard
->ErrorCode
|= HAL_SMARTCARD_ERROR_INVALID_CALLBACK
;
666 /* Return error status */
671 __HAL_UNLOCK(hsmartcard
);
675 #endif /* USE_HAL_SMARTCARD_REGISTER_CALLBACKS */
681 /** @defgroup SMARTCARD_Exported_Functions_Group2 IO operation functions
682 * @brief SMARTCARD Transmit and Receive functions
685 ==============================================================================
686 ##### IO operation functions #####
687 ==============================================================================
689 This subsection provides a set of functions allowing to manage the SMARTCARD data transfers.
692 Smartcard is a single wire half duplex communication protocol.
693 The Smartcard interface is designed to support asynchronous protocol Smartcards as
694 defined in the ISO 7816-3 standard. The USART should be configured as:
695 (+) 8 bits plus parity: where M=1 and PCE=1 in the USART_CR1 register
696 (+) 1.5 stop bits when transmitting and receiving: where STOP=11 in the USART_CR2 register.
699 (+) There are two modes of transfer:
700 (++) Blocking mode: The communication is performed in polling mode.
701 The HAL status of all data processing is returned by the same function
702 after finishing transfer.
703 (++) Non-Blocking mode: The communication is performed using Interrupts
704 or DMA, the relevant API's return the HAL status.
705 The end of the data processing will be indicated through the
706 dedicated SMARTCARD IRQ when using Interrupt mode or the DMA IRQ when
708 (++) The HAL_SMARTCARD_TxCpltCallback(), HAL_SMARTCARD_RxCpltCallback() user callbacks
709 will be executed respectively at the end of the Transmit or Receive process
710 The HAL_SMARTCARD_ErrorCallback() user callback will be executed when a communication
713 (+) Blocking mode APIs are :
714 (++) HAL_SMARTCARD_Transmit()
715 (++) HAL_SMARTCARD_Receive()
717 (+) Non Blocking mode APIs with Interrupt are :
718 (++) HAL_SMARTCARD_Transmit_IT()
719 (++) HAL_SMARTCARD_Receive_IT()
720 (++) HAL_SMARTCARD_IRQHandler()
722 (+) Non Blocking mode functions with DMA are :
723 (++) HAL_SMARTCARD_Transmit_DMA()
724 (++) HAL_SMARTCARD_Receive_DMA()
726 (+) A set of Transfer Complete Callbacks are provided in non Blocking mode:
727 (++) HAL_SMARTCARD_TxCpltCallback()
728 (++) HAL_SMARTCARD_RxCpltCallback()
729 (++) HAL_SMARTCARD_ErrorCallback()
731 (#) Non-Blocking mode transfers could be aborted using Abort API's :
732 (+) HAL_SMARTCARD_Abort()
733 (+) HAL_SMARTCARD_AbortTransmit()
734 (+) HAL_SMARTCARD_AbortReceive()
735 (+) HAL_SMARTCARD_Abort_IT()
736 (+) HAL_SMARTCARD_AbortTransmit_IT()
737 (+) HAL_SMARTCARD_AbortReceive_IT()
739 (#) For Abort services based on interrupts (HAL_SMARTCARD_Abortxxx_IT), a set of Abort Complete Callbacks are provided:
740 (+) HAL_SMARTCARD_AbortCpltCallback()
741 (+) HAL_SMARTCARD_AbortTransmitCpltCallback()
742 (+) HAL_SMARTCARD_AbortReceiveCpltCallback()
744 (#) In Non-Blocking mode transfers, possible errors are split into 2 categories.
745 Errors are handled as follows :
746 (+) Error is considered as Recoverable and non blocking : Transfer could go till end, but error severity is
747 to be evaluated by user : this concerns Frame Error, Parity Error or Noise Error in Interrupt mode reception .
748 Received character is then retrieved and stored in Rx buffer, Error code is set to allow user to identify error type,
749 and HAL_SMARTCARD_ErrorCallback() user callback is executed. Transfer is kept ongoing on SMARTCARD side.
750 If user wants to abort it, Abort services should be called by user.
751 (+) Error is considered as Blocking : Transfer could not be completed properly and is aborted.
752 This concerns Frame Error in Interrupt mode tranmission, Overrun Error in Interrupt mode reception and all errors in DMA mode.
753 Error code is set to allow user to identify error type, and HAL_SMARTCARD_ErrorCallback() user callback is executed.
760 * @brief Send an amount of data in blocking mode.
761 * @param hsmartcard Pointer to a SMARTCARD_HandleTypeDef structure that contains
762 * the configuration information for the specified SMARTCARD module.
763 * @param pData pointer to data buffer.
764 * @param Size amount of data to be sent.
765 * @param Timeout Timeout duration.
768 HAL_StatusTypeDef
HAL_SMARTCARD_Transmit(SMARTCARD_HandleTypeDef
*hsmartcard
, uint8_t *pData
, uint16_t Size
, uint32_t Timeout
)
771 uint8_t *ptmpdata
= pData
;
773 /* Check that a Tx process is not already ongoing */
774 if (hsmartcard
->gState
== HAL_SMARTCARD_STATE_READY
)
776 if ((ptmpdata
== NULL
) || (Size
== 0U))
782 __HAL_LOCK(hsmartcard
);
784 hsmartcard
->gState
= HAL_SMARTCARD_STATE_BUSY_TX
;
786 /* Init tickstart for timeout management */
787 tickstart
= HAL_GetTick();
789 /* Disable the Peripheral first to update mode for TX master */
790 CLEAR_BIT(hsmartcard
->Instance
->CR1
, USART_CR1_UE
);
792 /* Disable Rx, enable Tx */
793 CLEAR_BIT(hsmartcard
->Instance
->CR1
, USART_CR1_RE
);
794 SET_BIT(hsmartcard
->Instance
->RQR
, (uint16_t)SMARTCARD_RXDATA_FLUSH_REQUEST
);
795 SET_BIT(hsmartcard
->Instance
->CR1
, USART_CR1_TE
);
797 /* Enable the Peripheral */
798 SET_BIT(hsmartcard
->Instance
->CR1
, USART_CR1_UE
);
800 hsmartcard
->ErrorCode
= HAL_SMARTCARD_ERROR_NONE
;
801 hsmartcard
->TxXferSize
= Size
;
802 hsmartcard
->TxXferCount
= Size
;
804 while (hsmartcard
->TxXferCount
> 0U)
806 hsmartcard
->TxXferCount
--;
807 if (SMARTCARD_WaitOnFlagUntilTimeout(hsmartcard
, SMARTCARD_FLAG_TXE
, RESET
, tickstart
, Timeout
) != HAL_OK
)
811 hsmartcard
->Instance
->TDR
= (uint8_t)(*ptmpdata
& 0xFFU
);
814 if (SMARTCARD_WaitOnFlagUntilTimeout(hsmartcard
, SMARTCARD_TRANSMISSION_COMPLETION_FLAG(hsmartcard
), RESET
, tickstart
, Timeout
) != HAL_OK
)
818 /* Re-enable Rx at end of transmission if initial mode is Rx/Tx */
819 if (hsmartcard
->Init
.Mode
== SMARTCARD_MODE_TX_RX
)
821 /* Disable the Peripheral first to update modes */
822 CLEAR_BIT(hsmartcard
->Instance
->CR1
, USART_CR1_UE
);
823 SET_BIT(hsmartcard
->Instance
->CR1
, USART_CR1_RE
);
824 /* Enable the Peripheral */
825 SET_BIT(hsmartcard
->Instance
->CR1
, USART_CR1_UE
);
828 /* At end of Tx process, restore hsmartcard->gState to Ready */
829 hsmartcard
->gState
= HAL_SMARTCARD_STATE_READY
;
831 /* Process Unlocked */
832 __HAL_UNLOCK(hsmartcard
);
843 * @brief Receive an amount of data in blocking mode.
844 * @param hsmartcard Pointer to a SMARTCARD_HandleTypeDef structure that contains
845 * the configuration information for the specified SMARTCARD module.
846 * @param pData pointer to data buffer.
847 * @param Size amount of data to be received.
848 * @param Timeout Timeout duration.
851 HAL_StatusTypeDef
HAL_SMARTCARD_Receive(SMARTCARD_HandleTypeDef
*hsmartcard
, uint8_t *pData
, uint16_t Size
, uint32_t Timeout
)
854 uint8_t *ptmpdata
= pData
;
856 /* Check that a Rx process is not already ongoing */
857 if (hsmartcard
->RxState
== HAL_SMARTCARD_STATE_READY
)
859 if ((ptmpdata
== NULL
) || (Size
== 0U))
865 __HAL_LOCK(hsmartcard
);
867 hsmartcard
->ErrorCode
= HAL_SMARTCARD_ERROR_NONE
;
868 hsmartcard
->RxState
= HAL_SMARTCARD_STATE_BUSY_RX
;
870 /* Init tickstart for timeout management */
871 tickstart
= HAL_GetTick();
873 hsmartcard
->RxXferSize
= Size
;
874 hsmartcard
->RxXferCount
= Size
;
876 /* Check the remain data to be received */
877 while (hsmartcard
->RxXferCount
> 0U)
879 hsmartcard
->RxXferCount
--;
881 if (SMARTCARD_WaitOnFlagUntilTimeout(hsmartcard
, SMARTCARD_FLAG_RXNE
, RESET
, tickstart
, Timeout
) != HAL_OK
)
885 *ptmpdata
= (uint8_t)(hsmartcard
->Instance
->RDR
& (uint8_t)0x00FF);
889 /* At end of Rx process, restore hsmartcard->RxState to Ready */
890 hsmartcard
->RxState
= HAL_SMARTCARD_STATE_READY
;
892 /* Process Unlocked */
893 __HAL_UNLOCK(hsmartcard
);
904 * @brief Send an amount of data in interrupt mode.
905 * @param hsmartcard Pointer to a SMARTCARD_HandleTypeDef structure that contains
906 * the configuration information for the specified SMARTCARD module.
907 * @param pData pointer to data buffer.
908 * @param Size amount of data to be sent.
911 HAL_StatusTypeDef
HAL_SMARTCARD_Transmit_IT(SMARTCARD_HandleTypeDef
*hsmartcard
, uint8_t *pData
, uint16_t Size
)
913 /* Check that a Tx process is not already ongoing */
914 if (hsmartcard
->gState
== HAL_SMARTCARD_STATE_READY
)
916 if ((pData
== NULL
) || (Size
== 0U))
922 __HAL_LOCK(hsmartcard
);
924 hsmartcard
->ErrorCode
= HAL_SMARTCARD_ERROR_NONE
;
925 hsmartcard
->gState
= HAL_SMARTCARD_STATE_BUSY_TX
;
927 hsmartcard
->pTxBuffPtr
= pData
;
928 hsmartcard
->TxXferSize
= Size
;
929 hsmartcard
->TxXferCount
= Size
;
930 hsmartcard
->TxISR
= NULL
;
932 /* Disable the Peripheral first to update mode for TX master */
933 CLEAR_BIT(hsmartcard
->Instance
->CR1
, USART_CR1_UE
);
935 /* Disable Rx, enable Tx */
936 CLEAR_BIT(hsmartcard
->Instance
->CR1
, USART_CR1_RE
);
937 SET_BIT(hsmartcard
->Instance
->RQR
, (uint16_t)SMARTCARD_RXDATA_FLUSH_REQUEST
);
938 SET_BIT(hsmartcard
->Instance
->CR1
, USART_CR1_TE
);
940 /* Enable the Peripheral */
941 SET_BIT(hsmartcard
->Instance
->CR1
, USART_CR1_UE
);
943 /* Configure Tx interrupt processing */
944 /* Set the Tx ISR function pointer */
945 hsmartcard
->TxISR
= SMARTCARD_TxISR
;
947 /* Process Unlocked */
948 __HAL_UNLOCK(hsmartcard
);
950 /* Enable the SMARTCARD Error Interrupt: (Frame error) */
951 SET_BIT(hsmartcard
->Instance
->CR3
, USART_CR3_EIE
);
953 /* Enable the SMARTCARD Transmit Data Register Empty Interrupt */
954 SET_BIT(hsmartcard
->Instance
->CR1
, USART_CR1_TXEIE
);
965 * @brief Receive an amount of data in interrupt mode.
966 * @param hsmartcard Pointer to a SMARTCARD_HandleTypeDef structure that contains
967 * the configuration information for the specified SMARTCARD module.
968 * @param pData pointer to data buffer.
969 * @param Size amount of data to be received.
972 HAL_StatusTypeDef
HAL_SMARTCARD_Receive_IT(SMARTCARD_HandleTypeDef
*hsmartcard
, uint8_t *pData
, uint16_t Size
)
974 /* Check that a Rx process is not already ongoing */
975 if (hsmartcard
->RxState
== HAL_SMARTCARD_STATE_READY
)
977 if ((pData
== NULL
) || (Size
== 0U))
983 __HAL_LOCK(hsmartcard
);
985 hsmartcard
->ErrorCode
= HAL_SMARTCARD_ERROR_NONE
;
986 hsmartcard
->RxState
= HAL_SMARTCARD_STATE_BUSY_RX
;
988 hsmartcard
->pRxBuffPtr
= pData
;
989 hsmartcard
->RxXferSize
= Size
;
990 hsmartcard
->RxXferCount
= Size
;
992 /* Configure Rx interrupt processing */
993 /* Set the Rx ISR function pointer */
994 hsmartcard
->RxISR
= SMARTCARD_RxISR
;
996 /* Process Unlocked */
997 __HAL_UNLOCK(hsmartcard
);
999 /* Enable the SMARTCARD Parity Error and Data Register not empty Interrupts */
1000 SET_BIT(hsmartcard
->Instance
->CR1
, USART_CR1_PEIE
| USART_CR1_RXNEIE
);
1002 /* Enable the SMARTCARD Error Interrupt: (Frame error, noise error, overrun error) */
1003 SET_BIT(hsmartcard
->Instance
->CR3
, USART_CR3_EIE
);
1014 * @brief Send an amount of data in DMA mode.
1015 * @param hsmartcard Pointer to a SMARTCARD_HandleTypeDef structure that contains
1016 * the configuration information for the specified SMARTCARD module.
1017 * @param pData pointer to data buffer.
1018 * @param Size amount of data to be sent.
1019 * @retval HAL status
1021 HAL_StatusTypeDef
HAL_SMARTCARD_Transmit_DMA(SMARTCARD_HandleTypeDef
*hsmartcard
, uint8_t *pData
, uint16_t Size
)
1023 /* Check that a Tx process is not already ongoing */
1024 if (hsmartcard
->gState
== HAL_SMARTCARD_STATE_READY
)
1026 if ((pData
== NULL
) || (Size
== 0U))
1031 /* Process Locked */
1032 __HAL_LOCK(hsmartcard
);
1034 hsmartcard
->gState
= HAL_SMARTCARD_STATE_BUSY_TX
;
1036 hsmartcard
->ErrorCode
= HAL_SMARTCARD_ERROR_NONE
;
1037 hsmartcard
->pTxBuffPtr
= pData
;
1038 hsmartcard
->TxXferSize
= Size
;
1039 hsmartcard
->TxXferCount
= Size
;
1041 /* Disable the Peripheral first to update mode for TX master */
1042 CLEAR_BIT(hsmartcard
->Instance
->CR1
, USART_CR1_UE
);
1044 /* Disable Rx, enable Tx */
1045 CLEAR_BIT(hsmartcard
->Instance
->CR1
, USART_CR1_RE
);
1046 SET_BIT(hsmartcard
->Instance
->RQR
, (uint16_t)SMARTCARD_RXDATA_FLUSH_REQUEST
);
1047 SET_BIT(hsmartcard
->Instance
->CR1
, USART_CR1_TE
);
1049 /* Enable the Peripheral */
1050 SET_BIT(hsmartcard
->Instance
->CR1
, USART_CR1_UE
);
1052 /* Set the SMARTCARD DMA transfer complete callback */
1053 hsmartcard
->hdmatx
->XferCpltCallback
= SMARTCARD_DMATransmitCplt
;
1055 /* Set the SMARTCARD error callback */
1056 hsmartcard
->hdmatx
->XferErrorCallback
= SMARTCARD_DMAError
;
1058 /* Set the DMA abort callback */
1059 hsmartcard
->hdmatx
->XferAbortCallback
= NULL
;
1061 /* Enable the SMARTCARD transmit DMA channel */
1062 if (HAL_DMA_Start_IT(hsmartcard
->hdmatx
, (uint32_t)hsmartcard
->pTxBuffPtr
, (uint32_t)&hsmartcard
->Instance
->TDR
, Size
) == HAL_OK
)
1064 /* Clear the TC flag in the ICR register */
1065 CLEAR_BIT(hsmartcard
->Instance
->ICR
, USART_ICR_TCCF
);
1067 /* Process Unlocked */
1068 __HAL_UNLOCK(hsmartcard
);
1070 /* Enable the UART Error Interrupt: (Frame error) */
1071 SET_BIT(hsmartcard
->Instance
->CR3
, USART_CR3_EIE
);
1073 /* Enable the DMA transfer for transmit request by setting the DMAT bit
1074 in the SMARTCARD associated USART CR3 register */
1075 SET_BIT(hsmartcard
->Instance
->CR3
, USART_CR3_DMAT
);
1081 /* Set error code to DMA */
1082 hsmartcard
->ErrorCode
= HAL_SMARTCARD_ERROR_DMA
;
1084 /* Process Unlocked */
1085 __HAL_UNLOCK(hsmartcard
);
1087 /* Restore hsmartcard->State to ready */
1088 hsmartcard
->gState
= HAL_SMARTCARD_STATE_READY
;
1100 * @brief Receive an amount of data in DMA mode.
1101 * @param hsmartcard Pointer to a SMARTCARD_HandleTypeDef structure that contains
1102 * the configuration information for the specified SMARTCARD module.
1103 * @param pData pointer to data buffer.
1104 * @param Size amount of data to be received.
1105 * @note The SMARTCARD-associated USART parity is enabled (PCE = 1),
1106 * the received data contain the parity bit (MSB position).
1107 * @retval HAL status
1109 HAL_StatusTypeDef
HAL_SMARTCARD_Receive_DMA(SMARTCARD_HandleTypeDef
*hsmartcard
, uint8_t *pData
, uint16_t Size
)
1111 /* Check that a Rx process is not already ongoing */
1112 if (hsmartcard
->RxState
== HAL_SMARTCARD_STATE_READY
)
1114 if ((pData
== NULL
) || (Size
== 0U))
1119 /* Process Locked */
1120 __HAL_LOCK(hsmartcard
);
1122 hsmartcard
->ErrorCode
= HAL_SMARTCARD_ERROR_NONE
;
1123 hsmartcard
->RxState
= HAL_SMARTCARD_STATE_BUSY_RX
;
1125 hsmartcard
->pRxBuffPtr
= pData
;
1126 hsmartcard
->RxXferSize
= Size
;
1128 /* Set the SMARTCARD DMA transfer complete callback */
1129 hsmartcard
->hdmarx
->XferCpltCallback
= SMARTCARD_DMAReceiveCplt
;
1131 /* Set the SMARTCARD DMA error callback */
1132 hsmartcard
->hdmarx
->XferErrorCallback
= SMARTCARD_DMAError
;
1134 /* Set the DMA abort callback */
1135 hsmartcard
->hdmarx
->XferAbortCallback
= NULL
;
1137 /* Enable the DMA channel */
1138 if (HAL_DMA_Start_IT(hsmartcard
->hdmarx
, (uint32_t)&hsmartcard
->Instance
->RDR
, (uint32_t)hsmartcard
->pRxBuffPtr
, Size
) == HAL_OK
)
1140 /* Process Unlocked */
1141 __HAL_UNLOCK(hsmartcard
);
1143 /* Enable the SMARTCARD Parity Error Interrupt */
1144 SET_BIT(hsmartcard
->Instance
->CR1
, USART_CR1_PEIE
);
1146 /* Enable the SMARTCARD Error Interrupt: (Frame error, noise error, overrun error) */
1147 SET_BIT(hsmartcard
->Instance
->CR3
, USART_CR3_EIE
);
1149 /* Enable the DMA transfer for the receiver request by setting the DMAR bit
1150 in the SMARTCARD associated USART CR3 register */
1151 SET_BIT(hsmartcard
->Instance
->CR3
, USART_CR3_DMAR
);
1157 /* Set error code to DMA */
1158 hsmartcard
->ErrorCode
= HAL_SMARTCARD_ERROR_DMA
;
1160 /* Process Unlocked */
1161 __HAL_UNLOCK(hsmartcard
);
1163 /* Restore hsmartcard->State to ready */
1164 hsmartcard
->RxState
= HAL_SMARTCARD_STATE_READY
;
1176 * @brief Abort ongoing transfers (blocking mode).
1177 * @param hsmartcard Pointer to a SMARTCARD_HandleTypeDef structure that contains
1178 * the configuration information for the specified SMARTCARD module.
1179 * @note This procedure could be used for aborting any ongoing transfer started in Interrupt or DMA mode.
1180 * This procedure performs following operations :
1181 * - Disable SMARTCARD Interrupts (Tx and Rx)
1182 * - Disable the DMA transfer in the peripheral register (if enabled)
1183 * - Abort DMA transfer by calling HAL_DMA_Abort (in case of transfer in DMA mode)
1184 * - Set handle State to READY
1185 * @note This procedure is executed in blocking mode : when exiting function, Abort is considered as completed.
1186 * @retval HAL status
1188 HAL_StatusTypeDef
HAL_SMARTCARD_Abort(SMARTCARD_HandleTypeDef
*hsmartcard
)
1190 /* Disable RTOIE, EOBIE, TXEIE, TCIE, RXNE, PE and ERR (Frame error, noise error, overrun error) interrupts */
1191 CLEAR_BIT(hsmartcard
->Instance
->CR1
, (USART_CR1_RXNEIE
| USART_CR1_PEIE
| USART_CR1_TXEIE
| USART_CR1_TCIE
| USART_CR1_RTOIE
| USART_CR1_EOBIE
));
1192 CLEAR_BIT(hsmartcard
->Instance
->CR3
, USART_CR3_EIE
);
1194 /* Disable the SMARTCARD DMA Tx request if enabled */
1195 if (HAL_IS_BIT_SET(hsmartcard
->Instance
->CR3
, USART_CR3_DMAT
))
1197 CLEAR_BIT(hsmartcard
->Instance
->CR3
, USART_CR3_DMAT
);
1199 /* Abort the SMARTCARD DMA Tx channel : use blocking DMA Abort API (no callback) */
1200 if (hsmartcard
->hdmatx
!= NULL
)
1202 /* Set the SMARTCARD DMA Abort callback to Null.
1203 No call back execution at end of DMA abort procedure */
1204 hsmartcard
->hdmatx
->XferAbortCallback
= NULL
;
1206 if (HAL_DMA_Abort(hsmartcard
->hdmatx
) != HAL_OK
)
1208 if (HAL_DMA_GetError(hsmartcard
->hdmatx
) == HAL_DMA_ERROR_TIMEOUT
)
1210 /* Set error code to DMA */
1211 hsmartcard
->ErrorCode
= HAL_SMARTCARD_ERROR_DMA
;
1219 /* Disable the SMARTCARD DMA Rx request if enabled */
1220 if (HAL_IS_BIT_SET(hsmartcard
->Instance
->CR3
, USART_CR3_DMAR
))
1222 CLEAR_BIT(hsmartcard
->Instance
->CR3
, USART_CR3_DMAR
);
1224 /* Abort the SMARTCARD DMA Rx channel : use blocking DMA Abort API (no callback) */
1225 if (hsmartcard
->hdmarx
!= NULL
)
1227 /* Set the SMARTCARD DMA Abort callback to Null.
1228 No call back execution at end of DMA abort procedure */
1229 hsmartcard
->hdmarx
->XferAbortCallback
= NULL
;
1231 if (HAL_DMA_Abort(hsmartcard
->hdmarx
) != HAL_OK
)
1233 if (HAL_DMA_GetError(hsmartcard
->hdmarx
) == HAL_DMA_ERROR_TIMEOUT
)
1235 /* Set error code to DMA */
1236 hsmartcard
->ErrorCode
= HAL_SMARTCARD_ERROR_DMA
;
1244 /* Reset Tx and Rx transfer counters */
1245 hsmartcard
->TxXferCount
= 0U;
1246 hsmartcard
->RxXferCount
= 0U;
1248 /* Clear the Error flags in the ICR register */
1249 __HAL_SMARTCARD_CLEAR_FLAG(hsmartcard
, SMARTCARD_CLEAR_OREF
| SMARTCARD_CLEAR_NEF
| SMARTCARD_CLEAR_PEF
| SMARTCARD_CLEAR_FEF
| SMARTCARD_CLEAR_RTOF
| SMARTCARD_CLEAR_EOBF
);
1251 /* Restore hsmartcard->gState and hsmartcard->RxState to Ready */
1252 hsmartcard
->gState
= HAL_SMARTCARD_STATE_READY
;
1253 hsmartcard
->RxState
= HAL_SMARTCARD_STATE_READY
;
1255 /* Reset Handle ErrorCode to No Error */
1256 hsmartcard
->ErrorCode
= HAL_SMARTCARD_ERROR_NONE
;
1262 * @brief Abort ongoing Transmit transfer (blocking mode).
1263 * @param hsmartcard Pointer to a SMARTCARD_HandleTypeDef structure that contains
1264 * the configuration information for the specified SMARTCARD module.
1265 * @note This procedure could be used for aborting any ongoing Tx transfer started in Interrupt or DMA mode.
1266 * This procedure performs following operations :
1267 * - Disable SMARTCARD Interrupts (Tx)
1268 * - Disable the DMA transfer in the peripheral register (if enabled)
1269 * - Abort DMA transfer by calling HAL_DMA_Abort (in case of transfer in DMA mode)
1270 * - Set handle State to READY
1271 * @note This procedure is executed in blocking mode : when exiting function, Abort is considered as completed.
1272 * @retval HAL status
1274 HAL_StatusTypeDef
HAL_SMARTCARD_AbortTransmit(SMARTCARD_HandleTypeDef
*hsmartcard
)
1276 /* Disable TXEIE and TCIE interrupts */
1277 CLEAR_BIT(hsmartcard
->Instance
->CR1
, (USART_CR1_TXEIE
| USART_CR1_TCIE
));
1279 /* Check if a receive process is ongoing or not. If not disable ERR IT */
1280 if (hsmartcard
->RxState
== HAL_SMARTCARD_STATE_READY
)
1282 /* Disable the SMARTCARD Error Interrupt: (Frame error) */
1283 CLEAR_BIT(hsmartcard
->Instance
->CR3
, USART_CR3_EIE
);
1286 /* Disable the SMARTCARD DMA Tx request if enabled */
1287 if (HAL_IS_BIT_SET(hsmartcard
->Instance
->CR3
, USART_CR3_DMAT
))
1289 CLEAR_BIT(hsmartcard
->Instance
->CR3
, USART_CR3_DMAT
);
1291 /* Abort the SMARTCARD DMA Tx channel : use blocking DMA Abort API (no callback) */
1292 if (hsmartcard
->hdmatx
!= NULL
)
1294 /* Set the SMARTCARD DMA Abort callback to Null.
1295 No call back execution at end of DMA abort procedure */
1296 hsmartcard
->hdmatx
->XferAbortCallback
= NULL
;
1298 if (HAL_DMA_Abort(hsmartcard
->hdmatx
) != HAL_OK
)
1300 if (HAL_DMA_GetError(hsmartcard
->hdmatx
) == HAL_DMA_ERROR_TIMEOUT
)
1302 /* Set error code to DMA */
1303 hsmartcard
->ErrorCode
= HAL_SMARTCARD_ERROR_DMA
;
1311 /* Reset Tx transfer counter */
1312 hsmartcard
->TxXferCount
= 0U;
1314 /* Clear the Error flags in the ICR register */
1315 __HAL_SMARTCARD_CLEAR_FLAG(hsmartcard
, SMARTCARD_CLEAR_FEF
);
1317 /* Restore hsmartcard->gState to Ready */
1318 hsmartcard
->gState
= HAL_SMARTCARD_STATE_READY
;
1324 * @brief Abort ongoing Receive transfer (blocking mode).
1325 * @param hsmartcard Pointer to a SMARTCARD_HandleTypeDef structure that contains
1326 * the configuration information for the specified SMARTCARD module.
1327 * @note This procedure could be used for aborting any ongoing Rx transfer started in Interrupt or DMA mode.
1328 * This procedure performs following operations :
1329 * - Disable SMARTCARD Interrupts (Rx)
1330 * - Disable the DMA transfer in the peripheral register (if enabled)
1331 * - Abort DMA transfer by calling HAL_DMA_Abort (in case of transfer in DMA mode)
1332 * - Set handle State to READY
1333 * @note This procedure is executed in blocking mode : when exiting function, Abort is considered as completed.
1334 * @retval HAL status
1336 HAL_StatusTypeDef
HAL_SMARTCARD_AbortReceive(SMARTCARD_HandleTypeDef
*hsmartcard
)
1338 /* Disable RTOIE, EOBIE, RXNE, PE and ERR (Frame error, noise error, overrun error) interrupts */
1339 CLEAR_BIT(hsmartcard
->Instance
->CR1
, (USART_CR1_RXNEIE
| USART_CR1_PEIE
| USART_CR1_RTOIE
| USART_CR1_EOBIE
));
1340 CLEAR_BIT(hsmartcard
->Instance
->CR3
, USART_CR3_EIE
);
1342 /* Check if a Transmit process is ongoing or not. If not disable ERR IT */
1343 if (hsmartcard
->gState
== HAL_SMARTCARD_STATE_READY
)
1345 /* Disable the SMARTCARD Error Interrupt: (Frame error) */
1346 CLEAR_BIT(hsmartcard
->Instance
->CR3
, USART_CR3_EIE
);
1349 /* Disable the SMARTCARD DMA Rx request if enabled */
1350 if (HAL_IS_BIT_SET(hsmartcard
->Instance
->CR3
, USART_CR3_DMAR
))
1352 CLEAR_BIT(hsmartcard
->Instance
->CR3
, USART_CR3_DMAR
);
1354 /* Abort the SMARTCARD DMA Rx channel : use blocking DMA Abort API (no callback) */
1355 if (hsmartcard
->hdmarx
!= NULL
)
1357 /* Set the SMARTCARD DMA Abort callback to Null.
1358 No call back execution at end of DMA abort procedure */
1359 hsmartcard
->hdmarx
->XferAbortCallback
= NULL
;
1361 if (HAL_DMA_Abort(hsmartcard
->hdmarx
) != HAL_OK
)
1363 if (HAL_DMA_GetError(hsmartcard
->hdmarx
) == HAL_DMA_ERROR_TIMEOUT
)
1365 /* Set error code to DMA */
1366 hsmartcard
->ErrorCode
= HAL_SMARTCARD_ERROR_DMA
;
1374 /* Reset Rx transfer counter */
1375 hsmartcard
->RxXferCount
= 0U;
1377 /* Clear the Error flags in the ICR register */
1378 __HAL_SMARTCARD_CLEAR_FLAG(hsmartcard
, SMARTCARD_CLEAR_OREF
| SMARTCARD_CLEAR_NEF
| SMARTCARD_CLEAR_PEF
| SMARTCARD_CLEAR_FEF
| SMARTCARD_CLEAR_RTOF
| SMARTCARD_CLEAR_EOBF
);
1380 /* Restore hsmartcard->RxState to Ready */
1381 hsmartcard
->RxState
= HAL_SMARTCARD_STATE_READY
;
1387 * @brief Abort ongoing transfers (Interrupt mode).
1388 * @param hsmartcard Pointer to a SMARTCARD_HandleTypeDef structure that contains
1389 * the configuration information for the specified SMARTCARD module.
1390 * @note This procedure could be used for aborting any ongoing transfer started in Interrupt or DMA mode.
1391 * This procedure performs following operations :
1392 * - Disable SMARTCARD Interrupts (Tx and Rx)
1393 * - Disable the DMA transfer in the peripheral register (if enabled)
1394 * - Abort DMA transfer by calling HAL_DMA_Abort_IT (in case of transfer in DMA mode)
1395 * - Set handle State to READY
1396 * - At abort completion, call user abort complete callback
1397 * @note This procedure is executed in Interrupt mode, meaning that abort procedure could be
1398 * considered as completed only when user abort complete callback is executed (not when exiting function).
1399 * @retval HAL status
1401 HAL_StatusTypeDef
HAL_SMARTCARD_Abort_IT(SMARTCARD_HandleTypeDef
*hsmartcard
)
1403 uint32_t abortcplt
= 1U;
1405 /* Disable RTOIE, EOBIE, TXEIE, TCIE, RXNE, PE and ERR (Frame error, noise error, overrun error) interrupts */
1406 CLEAR_BIT(hsmartcard
->Instance
->CR1
, (USART_CR1_RXNEIE
| USART_CR1_PEIE
| USART_CR1_TXEIE
| USART_CR1_TCIE
| USART_CR1_RTOIE
| USART_CR1_EOBIE
));
1407 CLEAR_BIT(hsmartcard
->Instance
->CR3
, USART_CR3_EIE
);
1409 /* If DMA Tx and/or DMA Rx Handles are associated to SMARTCARD Handle, DMA Abort complete callbacks should be initialised
1410 before any call to DMA Abort functions */
1411 /* DMA Tx Handle is valid */
1412 if (hsmartcard
->hdmatx
!= NULL
)
1414 /* Set DMA Abort Complete callback if SMARTCARD DMA Tx request if enabled.
1415 Otherwise, set it to NULL */
1416 if (HAL_IS_BIT_SET(hsmartcard
->Instance
->CR3
, USART_CR3_DMAT
))
1418 hsmartcard
->hdmatx
->XferAbortCallback
= SMARTCARD_DMATxAbortCallback
;
1422 hsmartcard
->hdmatx
->XferAbortCallback
= NULL
;
1425 /* DMA Rx Handle is valid */
1426 if (hsmartcard
->hdmarx
!= NULL
)
1428 /* Set DMA Abort Complete callback if SMARTCARD DMA Rx request if enabled.
1429 Otherwise, set it to NULL */
1430 if (HAL_IS_BIT_SET(hsmartcard
->Instance
->CR3
, USART_CR3_DMAR
))
1432 hsmartcard
->hdmarx
->XferAbortCallback
= SMARTCARD_DMARxAbortCallback
;
1436 hsmartcard
->hdmarx
->XferAbortCallback
= NULL
;
1440 /* Disable the SMARTCARD DMA Tx request if enabled */
1441 if (HAL_IS_BIT_SET(hsmartcard
->Instance
->CR3
, USART_CR3_DMAT
))
1443 /* Disable DMA Tx at UART level */
1444 CLEAR_BIT(hsmartcard
->Instance
->CR3
, USART_CR3_DMAT
);
1446 /* Abort the SMARTCARD DMA Tx channel : use non blocking DMA Abort API (callback) */
1447 if (hsmartcard
->hdmatx
!= NULL
)
1449 /* SMARTCARD Tx DMA Abort callback has already been initialised :
1450 will lead to call HAL_SMARTCARD_AbortCpltCallback() at end of DMA abort procedure */
1453 if (HAL_DMA_Abort_IT(hsmartcard
->hdmatx
) != HAL_OK
)
1455 hsmartcard
->hdmatx
->XferAbortCallback
= NULL
;
1464 /* Disable the SMARTCARD DMA Rx request if enabled */
1465 if (HAL_IS_BIT_SET(hsmartcard
->Instance
->CR3
, USART_CR3_DMAR
))
1467 CLEAR_BIT(hsmartcard
->Instance
->CR3
, USART_CR3_DMAR
);
1469 /* Abort the SMARTCARD DMA Rx channel : use non blocking DMA Abort API (callback) */
1470 if (hsmartcard
->hdmarx
!= NULL
)
1472 /* SMARTCARD Rx DMA Abort callback has already been initialised :
1473 will lead to call HAL_SMARTCARD_AbortCpltCallback() at end of DMA abort procedure */
1476 if (HAL_DMA_Abort_IT(hsmartcard
->hdmarx
) != HAL_OK
)
1478 hsmartcard
->hdmarx
->XferAbortCallback
= NULL
;
1488 /* if no DMA abort complete callback execution is required => call user Abort Complete callback */
1489 if (abortcplt
== 1U)
1491 /* Reset Tx and Rx transfer counters */
1492 hsmartcard
->TxXferCount
= 0U;
1493 hsmartcard
->RxXferCount
= 0U;
1495 /* Clear ISR function pointers */
1496 hsmartcard
->RxISR
= NULL
;
1497 hsmartcard
->TxISR
= NULL
;
1499 /* Reset errorCode */
1500 hsmartcard
->ErrorCode
= HAL_SMARTCARD_ERROR_NONE
;
1502 /* Clear the Error flags in the ICR register */
1503 __HAL_SMARTCARD_CLEAR_FLAG(hsmartcard
, SMARTCARD_CLEAR_OREF
| SMARTCARD_CLEAR_NEF
| SMARTCARD_CLEAR_PEF
| SMARTCARD_CLEAR_FEF
| SMARTCARD_CLEAR_RTOF
| SMARTCARD_CLEAR_EOBF
);
1505 /* Restore hsmartcard->gState and hsmartcard->RxState to Ready */
1506 hsmartcard
->gState
= HAL_SMARTCARD_STATE_READY
;
1507 hsmartcard
->RxState
= HAL_SMARTCARD_STATE_READY
;
1509 /* As no DMA to be aborted, call directly user Abort complete callback */
1510 #if (USE_HAL_SMARTCARD_REGISTER_CALLBACKS == 1)
1511 /* Call registered Abort complete callback */
1512 hsmartcard
->AbortCpltCallback(hsmartcard
);
1514 /* Call legacy weak Abort complete callback */
1515 HAL_SMARTCARD_AbortCpltCallback(hsmartcard
);
1516 #endif /* USE_HAL_SMARTCARD_REGISTER_CALLBACK */
1523 * @brief Abort ongoing Transmit transfer (Interrupt mode).
1524 * @param hsmartcard Pointer to a SMARTCARD_HandleTypeDef structure that contains
1525 * the configuration information for the specified SMARTCARD module.
1526 * @note This procedure could be used for aborting any ongoing Tx transfer started in Interrupt or DMA mode.
1527 * This procedure performs following operations :
1528 * - Disable SMARTCARD Interrupts (Tx)
1529 * - Disable the DMA transfer in the peripheral register (if enabled)
1530 * - Abort DMA transfer by calling HAL_DMA_Abort_IT (in case of transfer in DMA mode)
1531 * - Set handle State to READY
1532 * - At abort completion, call user abort complete callback
1533 * @note This procedure is executed in Interrupt mode, meaning that abort procedure could be
1534 * considered as completed only when user abort complete callback is executed (not when exiting function).
1535 * @retval HAL status
1537 HAL_StatusTypeDef
HAL_SMARTCARD_AbortTransmit_IT(SMARTCARD_HandleTypeDef
*hsmartcard
)
1539 /* Disable TXEIE and TCIE interrupts */
1540 CLEAR_BIT(hsmartcard
->Instance
->CR1
, (USART_CR1_TXEIE
| USART_CR1_TCIE
));
1542 /* Check if a receive process is ongoing or not. If not disable ERR IT */
1543 if (hsmartcard
->RxState
== HAL_SMARTCARD_STATE_READY
)
1545 /* Disable the SMARTCARD Error Interrupt: (Frame error) */
1546 CLEAR_BIT(hsmartcard
->Instance
->CR3
, USART_CR3_EIE
);
1549 /* Disable the SMARTCARD DMA Tx request if enabled */
1550 if (HAL_IS_BIT_SET(hsmartcard
->Instance
->CR3
, USART_CR3_DMAT
))
1552 CLEAR_BIT(hsmartcard
->Instance
->CR3
, USART_CR3_DMAT
);
1554 /* Abort the SMARTCARD DMA Tx channel : use non blocking DMA Abort API (callback) */
1555 if (hsmartcard
->hdmatx
!= NULL
)
1557 /* Set the SMARTCARD DMA Abort callback :
1558 will lead to call HAL_SMARTCARD_AbortCpltCallback() at end of DMA abort procedure */
1559 hsmartcard
->hdmatx
->XferAbortCallback
= SMARTCARD_DMATxOnlyAbortCallback
;
1562 if (HAL_DMA_Abort_IT(hsmartcard
->hdmatx
) != HAL_OK
)
1564 /* Call Directly hsmartcard->hdmatx->XferAbortCallback function in case of error */
1565 hsmartcard
->hdmatx
->XferAbortCallback(hsmartcard
->hdmatx
);
1570 /* Reset Tx transfer counter */
1571 hsmartcard
->TxXferCount
= 0U;
1573 /* Clear TxISR function pointers */
1574 hsmartcard
->TxISR
= NULL
;
1576 /* Restore hsmartcard->gState to Ready */
1577 hsmartcard
->gState
= HAL_SMARTCARD_STATE_READY
;
1579 /* As no DMA to be aborted, call directly user Abort complete callback */
1580 #if (USE_HAL_SMARTCARD_REGISTER_CALLBACKS == 1)
1581 /* Call registered Abort Transmit Complete Callback */
1582 hsmartcard
->AbortTransmitCpltCallback(hsmartcard
);
1584 /* Call legacy weak Abort Transmit Complete Callback */
1585 HAL_SMARTCARD_AbortTransmitCpltCallback(hsmartcard
);
1586 #endif /* USE_HAL_SMARTCARD_REGISTER_CALLBACK */
1591 /* Reset Tx transfer counter */
1592 hsmartcard
->TxXferCount
= 0U;
1594 /* Clear TxISR function pointers */
1595 hsmartcard
->TxISR
= NULL
;
1597 /* Clear the Error flags in the ICR register */
1598 __HAL_SMARTCARD_CLEAR_FLAG(hsmartcard
, SMARTCARD_CLEAR_FEF
);
1600 /* Restore hsmartcard->gState to Ready */
1601 hsmartcard
->gState
= HAL_SMARTCARD_STATE_READY
;
1603 /* As no DMA to be aborted, call directly user Abort complete callback */
1604 #if (USE_HAL_SMARTCARD_REGISTER_CALLBACKS == 1)
1605 /* Call registered Abort Transmit Complete Callback */
1606 hsmartcard
->AbortTransmitCpltCallback(hsmartcard
);
1608 /* Call legacy weak Abort Transmit Complete Callback */
1609 HAL_SMARTCARD_AbortTransmitCpltCallback(hsmartcard
);
1610 #endif /* USE_HAL_SMARTCARD_REGISTER_CALLBACK */
1617 * @brief Abort ongoing Receive transfer (Interrupt mode).
1618 * @param hsmartcard Pointer to a SMARTCARD_HandleTypeDef structure that contains
1619 * the configuration information for the specified SMARTCARD module.
1620 * @note This procedure could be used for aborting any ongoing Rx transfer started in Interrupt or DMA mode.
1621 * This procedure performs following operations :
1622 * - Disable SMARTCARD Interrupts (Rx)
1623 * - Disable the DMA transfer in the peripheral register (if enabled)
1624 * - Abort DMA transfer by calling HAL_DMA_Abort_IT (in case of transfer in DMA mode)
1625 * - Set handle State to READY
1626 * - At abort completion, call user abort complete callback
1627 * @note This procedure is executed in Interrupt mode, meaning that abort procedure could be
1628 * considered as completed only when user abort complete callback is executed (not when exiting function).
1629 * @retval HAL status
1631 HAL_StatusTypeDef
HAL_SMARTCARD_AbortReceive_IT(SMARTCARD_HandleTypeDef
*hsmartcard
)
1633 /* Disable RTOIE, EOBIE, RXNE, PE and ERR (Frame error, noise error, overrun error) interrupts */
1634 CLEAR_BIT(hsmartcard
->Instance
->CR1
, (USART_CR1_RXNEIE
| USART_CR1_PEIE
| USART_CR1_RTOIE
| USART_CR1_EOBIE
));
1635 CLEAR_BIT(hsmartcard
->Instance
->CR3
, USART_CR3_EIE
);
1637 /* Check if a Transmit process is ongoing or not. If not disable ERR IT */
1638 if (hsmartcard
->gState
== HAL_SMARTCARD_STATE_READY
)
1640 /* Disable the SMARTCARD Error Interrupt: (Frame error) */
1641 CLEAR_BIT(hsmartcard
->Instance
->CR3
, USART_CR3_EIE
);
1644 /* Disable the SMARTCARD DMA Rx request if enabled */
1645 if (HAL_IS_BIT_SET(hsmartcard
->Instance
->CR3
, USART_CR3_DMAR
))
1647 CLEAR_BIT(hsmartcard
->Instance
->CR3
, USART_CR3_DMAR
);
1649 /* Abort the SMARTCARD DMA Rx channel : use non blocking DMA Abort API (callback) */
1650 if (hsmartcard
->hdmarx
!= NULL
)
1652 /* Set the SMARTCARD DMA Abort callback :
1653 will lead to call HAL_SMARTCARD_AbortCpltCallback() at end of DMA abort procedure */
1654 hsmartcard
->hdmarx
->XferAbortCallback
= SMARTCARD_DMARxOnlyAbortCallback
;
1657 if (HAL_DMA_Abort_IT(hsmartcard
->hdmarx
) != HAL_OK
)
1659 /* Call Directly hsmartcard->hdmarx->XferAbortCallback function in case of error */
1660 hsmartcard
->hdmarx
->XferAbortCallback(hsmartcard
->hdmarx
);
1665 /* Reset Rx transfer counter */
1666 hsmartcard
->RxXferCount
= 0U;
1668 /* Clear RxISR function pointer */
1669 hsmartcard
->RxISR
= NULL
;
1671 /* Clear the Error flags in the ICR register */
1672 __HAL_SMARTCARD_CLEAR_FLAG(hsmartcard
, SMARTCARD_CLEAR_OREF
| SMARTCARD_CLEAR_NEF
| SMARTCARD_CLEAR_PEF
| SMARTCARD_CLEAR_FEF
| SMARTCARD_CLEAR_RTOF
| SMARTCARD_CLEAR_EOBF
);
1674 /* Restore hsmartcard->RxState to Ready */
1675 hsmartcard
->RxState
= HAL_SMARTCARD_STATE_READY
;
1677 /* As no DMA to be aborted, call directly user Abort complete callback */
1678 #if (USE_HAL_SMARTCARD_REGISTER_CALLBACKS == 1)
1679 /* Call registered Abort Receive Complete Callback */
1680 hsmartcard
->AbortReceiveCpltCallback(hsmartcard
);
1682 /* Call legacy weak Abort Receive Complete Callback */
1683 HAL_SMARTCARD_AbortReceiveCpltCallback(hsmartcard
);
1684 #endif /* USE_HAL_SMARTCARD_REGISTER_CALLBACK */
1689 /* Reset Rx transfer counter */
1690 hsmartcard
->RxXferCount
= 0U;
1692 /* Clear RxISR function pointer */
1693 hsmartcard
->RxISR
= NULL
;
1695 /* Clear the Error flags in the ICR register */
1696 __HAL_SMARTCARD_CLEAR_FLAG(hsmartcard
, SMARTCARD_CLEAR_OREF
| SMARTCARD_CLEAR_NEF
| SMARTCARD_CLEAR_PEF
| SMARTCARD_CLEAR_FEF
| SMARTCARD_CLEAR_RTOF
| SMARTCARD_CLEAR_EOBF
);
1698 /* Restore hsmartcard->RxState to Ready */
1699 hsmartcard
->RxState
= HAL_SMARTCARD_STATE_READY
;
1701 /* As no DMA to be aborted, call directly user Abort complete callback */
1702 #if (USE_HAL_SMARTCARD_REGISTER_CALLBACKS == 1)
1703 /* Call registered Abort Receive Complete Callback */
1704 hsmartcard
->AbortReceiveCpltCallback(hsmartcard
);
1706 /* Call legacy weak Abort Receive Complete Callback */
1707 HAL_SMARTCARD_AbortReceiveCpltCallback(hsmartcard
);
1708 #endif /* USE_HAL_SMARTCARD_REGISTER_CALLBACK */
1715 * @brief Handle SMARTCARD interrupt requests.
1716 * @param hsmartcard Pointer to a SMARTCARD_HandleTypeDef structure that contains
1717 * the configuration information for the specified SMARTCARD module.
1720 void HAL_SMARTCARD_IRQHandler(SMARTCARD_HandleTypeDef
*hsmartcard
)
1722 uint32_t isrflags
= READ_REG(hsmartcard
->Instance
->ISR
);
1723 uint32_t cr1its
= READ_REG(hsmartcard
->Instance
->CR1
);
1724 uint32_t cr3its
= READ_REG(hsmartcard
->Instance
->CR3
);
1725 uint32_t errorflags
;
1728 /* If no error occurs */
1729 errorflags
= (isrflags
& (uint32_t)(USART_ISR_PE
| USART_ISR_FE
| USART_ISR_ORE
| USART_ISR_NE
| USART_ISR_RTOF
));
1730 if (errorflags
== 0U)
1732 /* SMARTCARD in mode Receiver ---------------------------------------------------*/
1733 if (((isrflags
& USART_ISR_RXNE
) != 0U)
1734 && ((cr1its
& USART_CR1_RXNEIE
) != 0U))
1736 if (hsmartcard
->RxISR
!= NULL
)
1738 hsmartcard
->RxISR(hsmartcard
);
1744 /* If some errors occur */
1745 if ((errorflags
!= 0U)
1746 && (((cr3its
& USART_CR3_EIE
) != 0U)
1747 || ((cr1its
& (USART_CR1_RXNEIE
| USART_CR1_PEIE
)) != 0U)))
1749 /* SMARTCARD parity error interrupt occurred -------------------------------------*/
1750 if (((isrflags
& USART_ISR_PE
) != 0U) && ((cr1its
& USART_CR1_PEIE
) != 0U))
1752 __HAL_SMARTCARD_CLEAR_IT(hsmartcard
, SMARTCARD_CLEAR_PEF
);
1754 hsmartcard
->ErrorCode
|= HAL_SMARTCARD_ERROR_PE
;
1757 /* SMARTCARD frame error interrupt occurred --------------------------------------*/
1758 if (((isrflags
& USART_ISR_FE
) != 0U) && ((cr3its
& USART_CR3_EIE
) != 0U))
1760 __HAL_SMARTCARD_CLEAR_IT(hsmartcard
, SMARTCARD_CLEAR_FEF
);
1762 hsmartcard
->ErrorCode
|= HAL_SMARTCARD_ERROR_FE
;
1765 /* SMARTCARD noise error interrupt occurred --------------------------------------*/
1766 if (((isrflags
& USART_ISR_NE
) != 0U) && ((cr3its
& USART_CR3_EIE
) != 0U))
1768 __HAL_SMARTCARD_CLEAR_IT(hsmartcard
, SMARTCARD_CLEAR_NEF
);
1770 hsmartcard
->ErrorCode
|= HAL_SMARTCARD_ERROR_NE
;
1773 /* SMARTCARD Over-Run interrupt occurred -----------------------------------------*/
1774 if (((isrflags
& USART_ISR_ORE
) != 0U)
1775 && (((cr1its
& USART_CR1_RXNEIE
) != 0U)
1776 || ((cr3its
& USART_CR3_EIE
) != 0U)))
1778 __HAL_SMARTCARD_CLEAR_IT(hsmartcard
, SMARTCARD_CLEAR_OREF
);
1780 hsmartcard
->ErrorCode
|= HAL_SMARTCARD_ERROR_ORE
;
1783 /* SMARTCARD receiver timeout interrupt occurred -----------------------------------------*/
1784 if (((isrflags
& USART_ISR_RTOF
) != 0U) && ((cr1its
& USART_CR1_RTOIE
) != 0U))
1786 __HAL_SMARTCARD_CLEAR_IT(hsmartcard
, SMARTCARD_CLEAR_RTOF
);
1788 hsmartcard
->ErrorCode
|= HAL_SMARTCARD_ERROR_RTO
;
1791 /* Call SMARTCARD Error Call back function if need be --------------------------*/
1792 if (hsmartcard
->ErrorCode
!= HAL_SMARTCARD_ERROR_NONE
)
1794 /* SMARTCARD in mode Receiver ---------------------------------------------------*/
1795 if (((isrflags
& USART_ISR_RXNE
) != 0U)
1796 && ((cr1its
& USART_CR1_RXNEIE
) != 0U))
1798 if (hsmartcard
->RxISR
!= NULL
)
1800 hsmartcard
->RxISR(hsmartcard
);
1804 /* If Error is to be considered as blocking :
1805 - Receiver Timeout error in Reception
1806 - Overrun error in Reception
1807 - any error occurs in DMA mode reception
1809 errorcode
= hsmartcard
->ErrorCode
;
1810 if ((HAL_IS_BIT_SET(hsmartcard
->Instance
->CR3
, USART_CR3_DMAR
))
1811 || ((errorcode
& (HAL_SMARTCARD_ERROR_RTO
| HAL_SMARTCARD_ERROR_ORE
)) != 0U))
1813 /* Blocking error : transfer is aborted
1814 Set the SMARTCARD state ready to be able to start again the process,
1815 Disable Rx Interrupts, and disable Rx DMA request, if ongoing */
1816 SMARTCARD_EndRxTransfer(hsmartcard
);
1818 /* Disable the SMARTCARD DMA Rx request if enabled */
1819 if (HAL_IS_BIT_SET(hsmartcard
->Instance
->CR3
, USART_CR3_DMAR
))
1821 CLEAR_BIT(hsmartcard
->Instance
->CR3
, USART_CR3_DMAR
);
1823 /* Abort the SMARTCARD DMA Rx channel */
1824 if (hsmartcard
->hdmarx
!= NULL
)
1826 /* Set the SMARTCARD DMA Abort callback :
1827 will lead to call HAL_SMARTCARD_ErrorCallback() at end of DMA abort procedure */
1828 hsmartcard
->hdmarx
->XferAbortCallback
= SMARTCARD_DMAAbortOnError
;
1831 if (HAL_DMA_Abort_IT(hsmartcard
->hdmarx
) != HAL_OK
)
1833 /* Call Directly hsmartcard->hdmarx->XferAbortCallback function in case of error */
1834 hsmartcard
->hdmarx
->XferAbortCallback(hsmartcard
->hdmarx
);
1839 #if (USE_HAL_SMARTCARD_REGISTER_CALLBACKS == 1)
1840 /* Call registered user error callback */
1841 hsmartcard
->ErrorCallback(hsmartcard
);
1843 /* Call legacy weak user error callback */
1844 HAL_SMARTCARD_ErrorCallback(hsmartcard
);
1845 #endif /* USE_HAL_SMARTCARD_REGISTER_CALLBACK */
1850 #if (USE_HAL_SMARTCARD_REGISTER_CALLBACKS == 1)
1851 /* Call registered user error callback */
1852 hsmartcard
->ErrorCallback(hsmartcard
);
1854 /* Call legacy weak user error callback */
1855 HAL_SMARTCARD_ErrorCallback(hsmartcard
);
1856 #endif /* USE_HAL_SMARTCARD_REGISTER_CALLBACK */
1859 /* other error type to be considered as blocking :
1860 - Frame error in Transmission
1862 else if ((hsmartcard
->gState
== HAL_SMARTCARD_STATE_BUSY_TX
)
1863 && ((errorcode
& HAL_SMARTCARD_ERROR_FE
) != 0U))
1865 /* Blocking error : transfer is aborted
1866 Set the SMARTCARD state ready to be able to start again the process,
1867 Disable Tx Interrupts, and disable Tx DMA request, if ongoing */
1868 SMARTCARD_EndTxTransfer(hsmartcard
);
1870 /* Disable the SMARTCARD DMA Tx request if enabled */
1871 if (HAL_IS_BIT_SET(hsmartcard
->Instance
->CR3
, USART_CR3_DMAT
))
1873 CLEAR_BIT(hsmartcard
->Instance
->CR3
, USART_CR3_DMAT
);
1875 /* Abort the SMARTCARD DMA Tx channel */
1876 if (hsmartcard
->hdmatx
!= NULL
)
1878 /* Set the SMARTCARD DMA Abort callback :
1879 will lead to call HAL_SMARTCARD_ErrorCallback() at end of DMA abort procedure */
1880 hsmartcard
->hdmatx
->XferAbortCallback
= SMARTCARD_DMAAbortOnError
;
1883 if (HAL_DMA_Abort_IT(hsmartcard
->hdmatx
) != HAL_OK
)
1885 /* Call Directly hsmartcard->hdmatx->XferAbortCallback function in case of error */
1886 hsmartcard
->hdmatx
->XferAbortCallback(hsmartcard
->hdmatx
);
1891 #if (USE_HAL_SMARTCARD_REGISTER_CALLBACKS == 1)
1892 /* Call registered user error callback */
1893 hsmartcard
->ErrorCallback(hsmartcard
);
1895 /* Call legacy weak user error callback */
1896 HAL_SMARTCARD_ErrorCallback(hsmartcard
);
1897 #endif /* USE_HAL_SMARTCARD_REGISTER_CALLBACK */
1902 #if (USE_HAL_SMARTCARD_REGISTER_CALLBACKS == 1)
1903 /* Call registered user error callback */
1904 hsmartcard
->ErrorCallback(hsmartcard
);
1906 /* Call legacy weak user error callback */
1907 HAL_SMARTCARD_ErrorCallback(hsmartcard
);
1908 #endif /* USE_HAL_SMARTCARD_REGISTER_CALLBACK */
1913 /* Non Blocking error : transfer could go on.
1914 Error is notified to user through user error callback */
1915 #if (USE_HAL_SMARTCARD_REGISTER_CALLBACKS == 1)
1916 /* Call registered user error callback */
1917 hsmartcard
->ErrorCallback(hsmartcard
);
1919 /* Call legacy weak user error callback */
1920 HAL_SMARTCARD_ErrorCallback(hsmartcard
);
1921 #endif /* USE_HAL_SMARTCARD_REGISTER_CALLBACK */
1922 hsmartcard
->ErrorCode
= HAL_SMARTCARD_ERROR_NONE
;
1927 } /* End if some error occurs */
1929 /* SMARTCARD in mode Receiver, end of block interruption ------------------------*/
1930 if (((isrflags
& USART_ISR_EOBF
) != 0U) && ((cr1its
& USART_CR1_EOBIE
) != 0U))
1932 hsmartcard
->RxState
= HAL_SMARTCARD_STATE_READY
;
1933 __HAL_UNLOCK(hsmartcard
);
1934 #if (USE_HAL_SMARTCARD_REGISTER_CALLBACKS == 1)
1935 /* Call registered Rx complete callback */
1936 hsmartcard
->RxCpltCallback(hsmartcard
);
1938 /* Call legacy weak Rx complete callback */
1939 HAL_SMARTCARD_RxCpltCallback(hsmartcard
);
1940 #endif /* USE_HAL_SMARTCARD_REGISTER_CALLBACK */
1941 /* Clear EOBF interrupt after HAL_SMARTCARD_RxCpltCallback() call for the End of Block information
1942 to be available during HAL_SMARTCARD_RxCpltCallback() processing */
1943 __HAL_SMARTCARD_CLEAR_IT(hsmartcard
, SMARTCARD_CLEAR_EOBF
);
1947 /* SMARTCARD in mode Transmitter ------------------------------------------------*/
1948 if (((isrflags
& USART_ISR_TXE
) != 0U)
1949 && ((cr1its
& USART_CR1_TXEIE
) != 0U))
1951 if (hsmartcard
->TxISR
!= NULL
)
1953 hsmartcard
->TxISR(hsmartcard
);
1958 /* SMARTCARD in mode Transmitter (transmission end) ------------------------*/
1959 if (__HAL_SMARTCARD_GET_IT(hsmartcard
, hsmartcard
->AdvancedInit
.TxCompletionIndication
) != RESET
)
1961 if(__HAL_SMARTCARD_GET_IT_SOURCE(hsmartcard
, hsmartcard
->AdvancedInit
.TxCompletionIndication
) != RESET
)
1963 SMARTCARD_EndTransmit_IT(hsmartcard
);
1971 * @brief Tx Transfer completed callback.
1972 * @param hsmartcard Pointer to a SMARTCARD_HandleTypeDef structure that contains
1973 * the configuration information for the specified SMARTCARD module.
1976 __weak
void HAL_SMARTCARD_TxCpltCallback(SMARTCARD_HandleTypeDef
*hsmartcard
)
1978 /* Prevent unused argument(s) compilation warning */
1981 /* NOTE : This function should not be modified, when the callback is needed,
1982 the HAL_SMARTCARD_TxCpltCallback can be implemented in the user file.
1987 * @brief Rx Transfer completed callback.
1988 * @param hsmartcard Pointer to a SMARTCARD_HandleTypeDef structure that contains
1989 * the configuration information for the specified SMARTCARD module.
1992 __weak
void HAL_SMARTCARD_RxCpltCallback(SMARTCARD_HandleTypeDef
*hsmartcard
)
1994 /* Prevent unused argument(s) compilation warning */
1997 /* NOTE : This function should not be modified, when the callback is needed,
1998 the HAL_SMARTCARD_RxCpltCallback can be implemented in the user file.
2003 * @brief SMARTCARD error callback.
2004 * @param hsmartcard Pointer to a SMARTCARD_HandleTypeDef structure that contains
2005 * the configuration information for the specified SMARTCARD module.
2008 __weak
void HAL_SMARTCARD_ErrorCallback(SMARTCARD_HandleTypeDef
*hsmartcard
)
2010 /* Prevent unused argument(s) compilation warning */
2013 /* NOTE : This function should not be modified, when the callback is needed,
2014 the HAL_SMARTCARD_ErrorCallback can be implemented in the user file.
2019 * @brief SMARTCARD Abort Complete callback.
2020 * @param hsmartcard Pointer to a SMARTCARD_HandleTypeDef structure that contains
2021 * the configuration information for the specified SMARTCARD module.
2024 __weak
void HAL_SMARTCARD_AbortCpltCallback(SMARTCARD_HandleTypeDef
*hsmartcard
)
2026 /* Prevent unused argument(s) compilation warning */
2029 /* NOTE : This function should not be modified, when the callback is needed,
2030 the HAL_SMARTCARD_AbortCpltCallback can be implemented in the user file.
2035 * @brief SMARTCARD Abort Complete callback.
2036 * @param hsmartcard Pointer to a SMARTCARD_HandleTypeDef structure that contains
2037 * the configuration information for the specified SMARTCARD module.
2040 __weak
void HAL_SMARTCARD_AbortTransmitCpltCallback(SMARTCARD_HandleTypeDef
*hsmartcard
)
2042 /* Prevent unused argument(s) compilation warning */
2045 /* NOTE : This function should not be modified, when the callback is needed,
2046 the HAL_SMARTCARD_AbortTransmitCpltCallback can be implemented in the user file.
2051 * @brief SMARTCARD Abort Receive Complete callback.
2052 * @param hsmartcard Pointer to a SMARTCARD_HandleTypeDef structure that contains
2053 * the configuration information for the specified SMARTCARD module.
2056 __weak
void HAL_SMARTCARD_AbortReceiveCpltCallback(SMARTCARD_HandleTypeDef
*hsmartcard
)
2058 /* Prevent unused argument(s) compilation warning */
2061 /* NOTE : This function should not be modified, when the callback is needed,
2062 the HAL_SMARTCARD_AbortReceiveCpltCallback can be implemented in the user file.
2070 /** @defgroup SMARTCARD_Exported_Functions_Group4 Peripheral State and Errors functions
2071 * @brief SMARTCARD State and Errors functions
2074 ==============================================================================
2075 ##### Peripheral State and Errors functions #####
2076 ==============================================================================
2078 This subsection provides a set of functions allowing to return the State of SmartCard
2079 handle and also return Peripheral Errors occurred during communication process
2080 (+) HAL_SMARTCARD_GetState() API can be helpful to check in run-time the state
2081 of the SMARTCARD peripheral.
2082 (+) HAL_SMARTCARD_GetError() checks in run-time errors that could occur during
2090 * @brief Return the SMARTCARD handle state.
2091 * @param hsmartcard Pointer to a SMARTCARD_HandleTypeDef structure that contains
2092 * the configuration information for the specified SMARTCARD module.
2093 * @retval SMARTCARD handle state
2095 HAL_SMARTCARD_StateTypeDef
HAL_SMARTCARD_GetState(SMARTCARD_HandleTypeDef
*hsmartcard
)
2097 /* Return SMARTCARD handle state */
2098 uint32_t temp1
, temp2
;
2099 temp1
= (uint32_t)hsmartcard
->gState
;
2100 temp2
= (uint32_t)hsmartcard
->RxState
;
2102 return (HAL_SMARTCARD_StateTypeDef
)(temp1
| temp2
);
2106 * @brief Return the SMARTCARD handle error code.
2107 * @param hsmartcard Pointer to a SMARTCARD_HandleTypeDef structure that contains
2108 * the configuration information for the specified SMARTCARD module.
2109 * @retval SMARTCARD handle Error Code
2111 uint32_t HAL_SMARTCARD_GetError(SMARTCARD_HandleTypeDef
*hsmartcard
)
2113 return hsmartcard
->ErrorCode
;
2124 /** @defgroup SMARTCARD_Private_Functions SMARTCARD Private Functions
2128 #if (USE_HAL_SMARTCARD_REGISTER_CALLBACKS == 1)
2130 * @brief Initialize the callbacks to their default values.
2131 * @param hsmartcard SMARTCARD handle.
2134 void SMARTCARD_InitCallbacksToDefault(SMARTCARD_HandleTypeDef
*hsmartcard
)
2136 /* Init the SMARTCARD Callback settings */
2137 hsmartcard
->TxCpltCallback
= HAL_SMARTCARD_TxCpltCallback
; /* Legacy weak TxCpltCallback */
2138 hsmartcard
->RxCpltCallback
= HAL_SMARTCARD_RxCpltCallback
; /* Legacy weak RxCpltCallback */
2139 hsmartcard
->ErrorCallback
= HAL_SMARTCARD_ErrorCallback
; /* Legacy weak ErrorCallback */
2140 hsmartcard
->AbortCpltCallback
= HAL_SMARTCARD_AbortCpltCallback
; /* Legacy weak AbortCpltCallback */
2141 hsmartcard
->AbortTransmitCpltCallback
= HAL_SMARTCARD_AbortTransmitCpltCallback
; /* Legacy weak AbortTransmitCpltCallback */
2142 hsmartcard
->AbortReceiveCpltCallback
= HAL_SMARTCARD_AbortReceiveCpltCallback
; /* Legacy weak AbortReceiveCpltCallback */
2145 #endif /* USE_HAL_SMARTCARD_REGISTER_CALLBACKS */
2148 * @brief Configure the SMARTCARD associated USART peripheral.
2149 * @param hsmartcard Pointer to a SMARTCARD_HandleTypeDef structure that contains
2150 * the configuration information for the specified SMARTCARD module.
2151 * @retval HAL status
2153 static HAL_StatusTypeDef
SMARTCARD_SetConfig(SMARTCARD_HandleTypeDef
*hsmartcard
)
2156 SMARTCARD_ClockSourceTypeDef clocksource
;
2157 HAL_StatusTypeDef ret
= HAL_OK
;
2159 /* Check the parameters */
2160 assert_param(IS_SMARTCARD_INSTANCE(hsmartcard
->Instance
));
2161 assert_param(IS_SMARTCARD_BAUDRATE(hsmartcard
->Init
.BaudRate
));
2162 assert_param(IS_SMARTCARD_WORD_LENGTH(hsmartcard
->Init
.WordLength
));
2163 assert_param(IS_SMARTCARD_STOPBITS(hsmartcard
->Init
.StopBits
));
2164 assert_param(IS_SMARTCARD_PARITY(hsmartcard
->Init
.Parity
));
2165 assert_param(IS_SMARTCARD_MODE(hsmartcard
->Init
.Mode
));
2166 assert_param(IS_SMARTCARD_POLARITY(hsmartcard
->Init
.CLKPolarity
));
2167 assert_param(IS_SMARTCARD_PHASE(hsmartcard
->Init
.CLKPhase
));
2168 assert_param(IS_SMARTCARD_LASTBIT(hsmartcard
->Init
.CLKLastBit
));
2169 assert_param(IS_SMARTCARD_ONE_BIT_SAMPLE(hsmartcard
->Init
.OneBitSampling
));
2170 assert_param(IS_SMARTCARD_NACK(hsmartcard
->Init
.NACKEnable
));
2171 assert_param(IS_SMARTCARD_TIMEOUT(hsmartcard
->Init
.TimeOutEnable
));
2172 assert_param(IS_SMARTCARD_AUTORETRY_COUNT(hsmartcard
->Init
.AutoRetryCount
));
2174 /*-------------------------- USART CR1 Configuration -----------------------*/
2175 /* In SmartCard mode, M and PCE are forced to 1 (8 bits + parity).
2176 * Oversampling is forced to 16 (OVER8 = 0).
2177 * Configure the Parity and Mode:
2178 * set PS bit according to hsmartcard->Init.Parity value
2179 * set TE and RE bits according to hsmartcard->Init.Mode value */
2180 tmpreg
= (uint32_t) (hsmartcard
->Init
.Parity
| hsmartcard
->Init
.Mode
| hsmartcard
->Init
.WordLength
);
2181 MODIFY_REG(hsmartcard
->Instance
->CR1
, USART_CR1_FIELDS
, tmpreg
);
2183 /*-------------------------- USART CR2 Configuration -----------------------*/
2184 tmpreg
= hsmartcard
->Init
.StopBits
;
2185 /* Synchronous mode is activated by default */
2186 tmpreg
|= (uint32_t) USART_CR2_CLKEN
| hsmartcard
->Init
.CLKPolarity
;
2187 tmpreg
|= (uint32_t) hsmartcard
->Init
.CLKPhase
| hsmartcard
->Init
.CLKLastBit
;
2188 tmpreg
|= (uint32_t) hsmartcard
->Init
.TimeOutEnable
;
2189 MODIFY_REG(hsmartcard
->Instance
->CR2
, USART_CR2_FIELDS
, tmpreg
);
2191 /*-------------------------- USART CR3 Configuration -----------------------*/
2193 * - one-bit sampling method versus three samples' majority rule
2194 * according to hsmartcard->Init.OneBitSampling
2195 * - NACK transmission in case of parity error according
2196 * to hsmartcard->Init.NACKEnable
2197 * - autoretry counter according to hsmartcard->Init.AutoRetryCount */
2199 tmpreg
= (uint32_t) hsmartcard
->Init
.OneBitSampling
| hsmartcard
->Init
.NACKEnable
;
2200 tmpreg
|= ((uint32_t)hsmartcard
->Init
.AutoRetryCount
<< USART_CR3_SCARCNT_Pos
);
2201 MODIFY_REG(hsmartcard
->Instance
->CR3
, USART_CR3_FIELDS
, tmpreg
);
2204 /*-------------------------- USART GTPR Configuration ----------------------*/
2205 tmpreg
= (hsmartcard
->Init
.Prescaler
| ((uint32_t)hsmartcard
->Init
.GuardTime
<< USART_GTPR_GT_Pos
));
2206 MODIFY_REG(hsmartcard
->Instance
->GTPR
, (uint16_t)(USART_GTPR_GT
| USART_GTPR_PSC
), (uint16_t)tmpreg
);
2208 /*-------------------------- USART RTOR Configuration ----------------------*/
2209 tmpreg
= ((uint32_t)hsmartcard
->Init
.BlockLength
<< USART_RTOR_BLEN_Pos
);
2210 if (hsmartcard
->Init
.TimeOutEnable
== SMARTCARD_TIMEOUT_ENABLE
)
2212 assert_param(IS_SMARTCARD_TIMEOUT_VALUE(hsmartcard
->Init
.TimeOutValue
));
2213 tmpreg
|= (uint32_t) hsmartcard
->Init
.TimeOutValue
;
2215 MODIFY_REG(hsmartcard
->Instance
->RTOR
, (USART_RTOR_RTO
| USART_RTOR_BLEN
), tmpreg
);
2217 /*-------------------------- USART BRR Configuration -----------------------*/
2218 SMARTCARD_GETCLOCKSOURCE(hsmartcard
, clocksource
);
2220 switch (clocksource
)
2222 case SMARTCARD_CLOCKSOURCE_PCLK1
:
2223 tmpreg
= (uint16_t)((HAL_RCC_GetPCLK1Freq() + (hsmartcard
->Init
.BaudRate
/ 2U)) / hsmartcard
->Init
.BaudRate
);
2225 case SMARTCARD_CLOCKSOURCE_PCLK2
:
2226 tmpreg
= (uint16_t)((HAL_RCC_GetPCLK2Freq() + (hsmartcard
->Init
.BaudRate
/ 2U)) / hsmartcard
->Init
.BaudRate
);
2228 case SMARTCARD_CLOCKSOURCE_HSI
:
2229 tmpreg
= (uint16_t)((HSI_VALUE
+ (hsmartcard
->Init
.BaudRate
/ 2U)) / hsmartcard
->Init
.BaudRate
);
2231 case SMARTCARD_CLOCKSOURCE_SYSCLK
:
2232 tmpreg
= (uint16_t)((HAL_RCC_GetSysClockFreq() + (hsmartcard
->Init
.BaudRate
/ 2U)) / hsmartcard
->Init
.BaudRate
);
2234 case SMARTCARD_CLOCKSOURCE_LSE
:
2235 tmpreg
= (uint16_t)((LSE_VALUE
+ (hsmartcard
->Init
.BaudRate
/ 2U)) / hsmartcard
->Init
.BaudRate
);
2242 /* USARTDIV must be greater than or equal to 0d16 */
2243 if ((tmpreg
>= USART_BRR_MIN
) && (tmpreg
<= USART_BRR_MAX
))
2245 hsmartcard
->Instance
->BRR
= tmpreg
;
2253 /* Clear ISR function pointers */
2254 hsmartcard
->RxISR
= NULL
;
2255 hsmartcard
->TxISR
= NULL
;
2262 * @brief Configure the SMARTCARD associated USART peripheral advanced features.
2263 * @param hsmartcard Pointer to a SMARTCARD_HandleTypeDef structure that contains
2264 * the configuration information for the specified SMARTCARD module.
2267 static void SMARTCARD_AdvFeatureConfig(SMARTCARD_HandleTypeDef
*hsmartcard
)
2269 /* Check whether the set of advanced features to configure is properly set */
2270 assert_param(IS_SMARTCARD_ADVFEATURE_INIT(hsmartcard
->AdvancedInit
.AdvFeatureInit
));
2272 /* if required, configure TX pin active level inversion */
2273 if (HAL_IS_BIT_SET(hsmartcard
->AdvancedInit
.AdvFeatureInit
, SMARTCARD_ADVFEATURE_TXINVERT_INIT
))
2275 assert_param(IS_SMARTCARD_ADVFEATURE_TXINV(hsmartcard
->AdvancedInit
.TxPinLevelInvert
));
2276 MODIFY_REG(hsmartcard
->Instance
->CR2
, USART_CR2_TXINV
, hsmartcard
->AdvancedInit
.TxPinLevelInvert
);
2279 /* if required, configure RX pin active level inversion */
2280 if (HAL_IS_BIT_SET(hsmartcard
->AdvancedInit
.AdvFeatureInit
, SMARTCARD_ADVFEATURE_RXINVERT_INIT
))
2282 assert_param(IS_SMARTCARD_ADVFEATURE_RXINV(hsmartcard
->AdvancedInit
.RxPinLevelInvert
));
2283 MODIFY_REG(hsmartcard
->Instance
->CR2
, USART_CR2_RXINV
, hsmartcard
->AdvancedInit
.RxPinLevelInvert
);
2286 /* if required, configure data inversion */
2287 if (HAL_IS_BIT_SET(hsmartcard
->AdvancedInit
.AdvFeatureInit
, SMARTCARD_ADVFEATURE_DATAINVERT_INIT
))
2289 assert_param(IS_SMARTCARD_ADVFEATURE_DATAINV(hsmartcard
->AdvancedInit
.DataInvert
));
2290 MODIFY_REG(hsmartcard
->Instance
->CR2
, USART_CR2_DATAINV
, hsmartcard
->AdvancedInit
.DataInvert
);
2293 /* if required, configure RX/TX pins swap */
2294 if (HAL_IS_BIT_SET(hsmartcard
->AdvancedInit
.AdvFeatureInit
, SMARTCARD_ADVFEATURE_SWAP_INIT
))
2296 assert_param(IS_SMARTCARD_ADVFEATURE_SWAP(hsmartcard
->AdvancedInit
.Swap
));
2297 MODIFY_REG(hsmartcard
->Instance
->CR2
, USART_CR2_SWAP
, hsmartcard
->AdvancedInit
.Swap
);
2300 /* if required, configure RX overrun detection disabling */
2301 if (HAL_IS_BIT_SET(hsmartcard
->AdvancedInit
.AdvFeatureInit
, SMARTCARD_ADVFEATURE_RXOVERRUNDISABLE_INIT
))
2303 assert_param(IS_SMARTCARD_OVERRUN(hsmartcard
->AdvancedInit
.OverrunDisable
));
2304 MODIFY_REG(hsmartcard
->Instance
->CR3
, USART_CR3_OVRDIS
, hsmartcard
->AdvancedInit
.OverrunDisable
);
2307 /* if required, configure DMA disabling on reception error */
2308 if (HAL_IS_BIT_SET(hsmartcard
->AdvancedInit
.AdvFeatureInit
, SMARTCARD_ADVFEATURE_DMADISABLEONERROR_INIT
))
2310 assert_param(IS_SMARTCARD_ADVFEATURE_DMAONRXERROR(hsmartcard
->AdvancedInit
.DMADisableonRxError
));
2311 MODIFY_REG(hsmartcard
->Instance
->CR3
, USART_CR3_DDRE
, hsmartcard
->AdvancedInit
.DMADisableonRxError
);
2314 /* if required, configure MSB first on communication line */
2315 if (HAL_IS_BIT_SET(hsmartcard
->AdvancedInit
.AdvFeatureInit
, SMARTCARD_ADVFEATURE_MSBFIRST_INIT
))
2317 assert_param(IS_SMARTCARD_ADVFEATURE_MSBFIRST(hsmartcard
->AdvancedInit
.MSBFirst
));
2318 MODIFY_REG(hsmartcard
->Instance
->CR2
, USART_CR2_MSBFIRST
, hsmartcard
->AdvancedInit
.MSBFirst
);
2324 * @brief Check the SMARTCARD Idle State.
2325 * @param hsmartcard Pointer to a SMARTCARD_HandleTypeDef structure that contains
2326 * the configuration information for the specified SMARTCARD module.
2327 * @retval HAL status
2329 static HAL_StatusTypeDef
SMARTCARD_CheckIdleState(SMARTCARD_HandleTypeDef
*hsmartcard
)
2333 /* Initialize the SMARTCARD ErrorCode */
2334 hsmartcard
->ErrorCode
= HAL_SMARTCARD_ERROR_NONE
;
2336 /* Init tickstart for timeout management */
2337 tickstart
= HAL_GetTick();
2339 /* Check if the Transmitter is enabled */
2340 if ((hsmartcard
->Instance
->CR1
& USART_CR1_TE
) == USART_CR1_TE
)
2342 /* Wait until TEACK flag is set */
2343 if (SMARTCARD_WaitOnFlagUntilTimeout(hsmartcard
, USART_ISR_TEACK
, RESET
, tickstart
, SMARTCARD_TEACK_REACK_TIMEOUT
) != HAL_OK
)
2345 /* Timeout occurred */
2350 /* Initialize the SMARTCARD states */
2351 hsmartcard
->gState
= HAL_SMARTCARD_STATE_READY
;
2352 hsmartcard
->RxState
= HAL_SMARTCARD_STATE_READY
;
2354 /* Process Unlocked */
2355 __HAL_UNLOCK(hsmartcard
);
2361 * @brief Handle SMARTCARD Communication Timeout.
2362 * @param hsmartcard Pointer to a SMARTCARD_HandleTypeDef structure that contains
2363 * the configuration information for the specified SMARTCARD module.
2364 * @param Flag Specifies the SMARTCARD flag to check.
2365 * @param Status The new Flag status (SET or RESET).
2366 * @param Tickstart Tick start value
2367 * @param Timeout Timeout duration.
2368 * @retval HAL status
2370 static HAL_StatusTypeDef
SMARTCARD_WaitOnFlagUntilTimeout(SMARTCARD_HandleTypeDef
*hsmartcard
, uint32_t Flag
, FlagStatus Status
, uint32_t Tickstart
, uint32_t Timeout
)
2372 /* Wait until flag is set */
2373 while ((__HAL_SMARTCARD_GET_FLAG(hsmartcard
, Flag
) ? SET
: RESET
) == Status
)
2375 /* Check for the Timeout */
2376 if (Timeout
!= HAL_MAX_DELAY
)
2378 if (((HAL_GetTick() - Tickstart
) > Timeout
) || (Timeout
== 0U))
2380 /* Disable TXE, RXNE, PE and ERR (Frame error, noise error, overrun error) interrupts for the interrupt process */
2381 CLEAR_BIT(hsmartcard
->Instance
->CR1
, (USART_CR1_RXNEIE
| USART_CR1_PEIE
| USART_CR1_TXEIE
));
2382 CLEAR_BIT(hsmartcard
->Instance
->CR3
, USART_CR3_EIE
);
2384 hsmartcard
->gState
= HAL_SMARTCARD_STATE_READY
;
2385 hsmartcard
->RxState
= HAL_SMARTCARD_STATE_READY
;
2387 /* Process Unlocked */
2388 __HAL_UNLOCK(hsmartcard
);
2398 * @brief End ongoing Tx transfer on SMARTCARD peripheral (following error detection or Transmit completion).
2399 * @param hsmartcard Pointer to a SMARTCARD_HandleTypeDef structure that contains
2400 * the configuration information for the specified SMARTCARD module.
2403 static void SMARTCARD_EndTxTransfer(SMARTCARD_HandleTypeDef
*hsmartcard
)
2405 /* Disable TXEIE, TCIE and ERR (Frame error, noise error, overrun error) interrupts */
2406 CLEAR_BIT(hsmartcard
->Instance
->CR1
, (USART_CR1_TXEIE
| USART_CR1_TCIE
));
2407 CLEAR_BIT(hsmartcard
->Instance
->CR3
, USART_CR3_EIE
);
2409 /* At end of Tx process, restore hsmartcard->gState to Ready */
2410 hsmartcard
->gState
= HAL_SMARTCARD_STATE_READY
;
2415 * @brief End ongoing Rx transfer on UART peripheral (following error detection or Reception completion).
2416 * @param hsmartcard Pointer to a SMARTCARD_HandleTypeDef structure that contains
2417 * the configuration information for the specified SMARTCARD module.
2420 static void SMARTCARD_EndRxTransfer(SMARTCARD_HandleTypeDef
*hsmartcard
)
2422 /* Disable RXNE, PE and ERR (Frame error, noise error, overrun error) interrupts */
2423 CLEAR_BIT(hsmartcard
->Instance
->CR1
, (USART_CR1_RXNEIE
| USART_CR1_PEIE
));
2424 CLEAR_BIT(hsmartcard
->Instance
->CR3
, USART_CR3_EIE
);
2426 /* At end of Rx process, restore hsmartcard->RxState to Ready */
2427 hsmartcard
->RxState
= HAL_SMARTCARD_STATE_READY
;
2432 * @brief DMA SMARTCARD transmit process complete callback.
2433 * @param hdma Pointer to a DMA_HandleTypeDef structure that contains
2434 * the configuration information for the specified DMA module.
2437 static void SMARTCARD_DMATransmitCplt(DMA_HandleTypeDef
*hdma
)
2439 SMARTCARD_HandleTypeDef
*hsmartcard
= (SMARTCARD_HandleTypeDef
*)(hdma
->Parent
);
2440 hsmartcard
->TxXferCount
= 0U;
2442 /* Disable the DMA transfer for transmit request by resetting the DMAT bit
2443 in the SMARTCARD associated USART CR3 register */
2444 CLEAR_BIT(hsmartcard
->Instance
->CR3
, USART_CR3_DMAT
);
2446 /* Enable the SMARTCARD Transmit Complete Interrupt */
2447 __HAL_SMARTCARD_ENABLE_IT(hsmartcard
, hsmartcard
->AdvancedInit
.TxCompletionIndication
);
2451 * @brief DMA SMARTCARD receive process complete callback.
2452 * @param hdma Pointer to a DMA_HandleTypeDef structure that contains
2453 * the configuration information for the specified DMA module.
2456 static void SMARTCARD_DMAReceiveCplt(DMA_HandleTypeDef
*hdma
)
2458 SMARTCARD_HandleTypeDef
*hsmartcard
= (SMARTCARD_HandleTypeDef
*)(hdma
->Parent
);
2459 hsmartcard
->RxXferCount
= 0U;
2461 /* Disable PE and ERR (Frame error, noise error, overrun error) interrupts */
2462 CLEAR_BIT(hsmartcard
->Instance
->CR1
, USART_CR1_PEIE
);
2463 CLEAR_BIT(hsmartcard
->Instance
->CR3
, USART_CR3_EIE
);
2465 /* Disable the DMA transfer for the receiver request by resetting the DMAR bit
2466 in the SMARTCARD associated USART CR3 register */
2467 CLEAR_BIT(hsmartcard
->Instance
->CR3
, USART_CR3_DMAR
);
2469 /* At end of Rx process, restore hsmartcard->RxState to Ready */
2470 hsmartcard
->RxState
= HAL_SMARTCARD_STATE_READY
;
2472 #if (USE_HAL_SMARTCARD_REGISTER_CALLBACKS == 1)
2473 /* Call registered Rx complete callback */
2474 hsmartcard
->RxCpltCallback(hsmartcard
);
2476 /* Call legacy weak Rx complete callback */
2477 HAL_SMARTCARD_RxCpltCallback(hsmartcard
);
2478 #endif /* USE_HAL_SMARTCARD_REGISTER_CALLBACK */
2482 * @brief DMA SMARTCARD communication error callback.
2483 * @param hdma Pointer to a DMA_HandleTypeDef structure that contains
2484 * the configuration information for the specified DMA module.
2487 static void SMARTCARD_DMAError(DMA_HandleTypeDef
*hdma
)
2489 SMARTCARD_HandleTypeDef
*hsmartcard
= (SMARTCARD_HandleTypeDef
*)(hdma
->Parent
);
2491 /* Stop SMARTCARD DMA Tx request if ongoing */
2492 if (hsmartcard
->gState
== HAL_SMARTCARD_STATE_BUSY_TX
)
2494 if(HAL_IS_BIT_SET(hsmartcard
->Instance
->CR3
, USART_CR3_DMAT
))
2496 hsmartcard
->TxXferCount
= 0U;
2497 SMARTCARD_EndTxTransfer(hsmartcard
);
2501 /* Stop SMARTCARD DMA Rx request if ongoing */
2502 if (hsmartcard
->RxState
== HAL_SMARTCARD_STATE_BUSY_RX
)
2504 if (HAL_IS_BIT_SET(hsmartcard
->Instance
->CR3
, USART_CR3_DMAR
))
2506 hsmartcard
->RxXferCount
= 0U;
2507 SMARTCARD_EndRxTransfer(hsmartcard
);
2511 hsmartcard
->ErrorCode
|= HAL_SMARTCARD_ERROR_DMA
;
2512 #if (USE_HAL_SMARTCARD_REGISTER_CALLBACKS == 1)
2513 /* Call registered user error callback */
2514 hsmartcard
->ErrorCallback(hsmartcard
);
2516 /* Call legacy weak user error callback */
2517 HAL_SMARTCARD_ErrorCallback(hsmartcard
);
2518 #endif /* USE_HAL_SMARTCARD_REGISTER_CALLBACK */
2522 * @brief DMA SMARTCARD communication abort callback, when initiated by HAL services on Error
2523 * (To be called at end of DMA Abort procedure following error occurrence).
2524 * @param hdma DMA handle.
2527 static void SMARTCARD_DMAAbortOnError(DMA_HandleTypeDef
*hdma
)
2529 SMARTCARD_HandleTypeDef
*hsmartcard
= (SMARTCARD_HandleTypeDef
*)(hdma
->Parent
);
2530 hsmartcard
->RxXferCount
= 0U;
2531 hsmartcard
->TxXferCount
= 0U;
2533 #if (USE_HAL_SMARTCARD_REGISTER_CALLBACKS == 1)
2534 /* Call registered user error callback */
2535 hsmartcard
->ErrorCallback(hsmartcard
);
2537 /* Call legacy weak user error callback */
2538 HAL_SMARTCARD_ErrorCallback(hsmartcard
);
2539 #endif /* USE_HAL_SMARTCARD_REGISTER_CALLBACK */
2543 * @brief DMA SMARTCARD Tx communication abort callback, when initiated by user
2544 * (To be called at end of DMA Tx Abort procedure following user abort request).
2545 * @note When this callback is executed, User Abort complete call back is called only if no
2546 * Abort still ongoing for Rx DMA Handle.
2547 * @param hdma DMA handle.
2550 static void SMARTCARD_DMATxAbortCallback(DMA_HandleTypeDef
*hdma
)
2552 SMARTCARD_HandleTypeDef
*hsmartcard
= (SMARTCARD_HandleTypeDef
*)(hdma
->Parent
);
2554 hsmartcard
->hdmatx
->XferAbortCallback
= NULL
;
2556 /* Check if an Abort process is still ongoing */
2557 if (hsmartcard
->hdmarx
!= NULL
)
2559 if (hsmartcard
->hdmarx
->XferAbortCallback
!= NULL
)
2565 /* No Abort process still ongoing : All DMA channels are aborted, call user Abort Complete callback */
2566 hsmartcard
->TxXferCount
= 0U;
2567 hsmartcard
->RxXferCount
= 0U;
2569 /* Reset errorCode */
2570 hsmartcard
->ErrorCode
= HAL_SMARTCARD_ERROR_NONE
;
2572 /* Clear the Error flags in the ICR register */
2573 __HAL_SMARTCARD_CLEAR_FLAG(hsmartcard
, SMARTCARD_CLEAR_OREF
| SMARTCARD_CLEAR_NEF
| SMARTCARD_CLEAR_PEF
| SMARTCARD_CLEAR_FEF
| SMARTCARD_CLEAR_RTOF
| SMARTCARD_CLEAR_EOBF
);
2575 /* Restore hsmartcard->gState and hsmartcard->RxState to Ready */
2576 hsmartcard
->gState
= HAL_SMARTCARD_STATE_READY
;
2577 hsmartcard
->RxState
= HAL_SMARTCARD_STATE_READY
;
2579 #if (USE_HAL_SMARTCARD_REGISTER_CALLBACKS == 1)
2580 /* Call registered Abort complete callback */
2581 hsmartcard
->AbortCpltCallback(hsmartcard
);
2583 /* Call legacy weak Abort complete callback */
2584 HAL_SMARTCARD_AbortCpltCallback(hsmartcard
);
2585 #endif /* USE_HAL_SMARTCARD_REGISTER_CALLBACK */
2590 * @brief DMA SMARTCARD Rx communication abort callback, when initiated by user
2591 * (To be called at end of DMA Rx Abort procedure following user abort request).
2592 * @note When this callback is executed, User Abort complete call back is called only if no
2593 * Abort still ongoing for Tx DMA Handle.
2594 * @param hdma DMA handle.
2597 static void SMARTCARD_DMARxAbortCallback(DMA_HandleTypeDef
*hdma
)
2599 SMARTCARD_HandleTypeDef
*hsmartcard
= (SMARTCARD_HandleTypeDef
*)(hdma
->Parent
);
2601 hsmartcard
->hdmarx
->XferAbortCallback
= NULL
;
2603 /* Check if an Abort process is still ongoing */
2604 if (hsmartcard
->hdmatx
!= NULL
)
2606 if (hsmartcard
->hdmatx
->XferAbortCallback
!= NULL
)
2612 /* No Abort process still ongoing : All DMA channels are aborted, call user Abort Complete callback */
2613 hsmartcard
->TxXferCount
= 0U;
2614 hsmartcard
->RxXferCount
= 0U;
2616 /* Reset errorCode */
2617 hsmartcard
->ErrorCode
= HAL_SMARTCARD_ERROR_NONE
;
2619 /* Clear the Error flags in the ICR register */
2620 __HAL_SMARTCARD_CLEAR_FLAG(hsmartcard
, SMARTCARD_CLEAR_OREF
| SMARTCARD_CLEAR_NEF
| SMARTCARD_CLEAR_PEF
| SMARTCARD_CLEAR_FEF
| SMARTCARD_CLEAR_RTOF
| SMARTCARD_CLEAR_EOBF
);
2622 /* Restore hsmartcard->gState and hsmartcard->RxState to Ready */
2623 hsmartcard
->gState
= HAL_SMARTCARD_STATE_READY
;
2624 hsmartcard
->RxState
= HAL_SMARTCARD_STATE_READY
;
2626 #if (USE_HAL_SMARTCARD_REGISTER_CALLBACKS == 1)
2627 /* Call registered Abort complete callback */
2628 hsmartcard
->AbortCpltCallback(hsmartcard
);
2630 /* Call legacy weak Abort complete callback */
2631 HAL_SMARTCARD_AbortCpltCallback(hsmartcard
);
2632 #endif /* USE_HAL_SMARTCARD_REGISTER_CALLBACK */
2637 * @brief DMA SMARTCARD Tx communication abort callback, when initiated by user by a call to
2638 * HAL_SMARTCARD_AbortTransmit_IT API (Abort only Tx transfer)
2639 * (This callback is executed at end of DMA Tx Abort procedure following user abort request,
2640 * and leads to user Tx Abort Complete callback execution).
2641 * @param hdma DMA handle.
2644 static void SMARTCARD_DMATxOnlyAbortCallback(DMA_HandleTypeDef
*hdma
)
2646 SMARTCARD_HandleTypeDef
*hsmartcard
= (SMARTCARD_HandleTypeDef
*)(hdma
->Parent
);
2648 hsmartcard
->TxXferCount
= 0U;
2650 /* Clear the Error flags in the ICR register */
2651 __HAL_SMARTCARD_CLEAR_FLAG(hsmartcard
, SMARTCARD_CLEAR_FEF
);
2653 /* Restore hsmartcard->gState to Ready */
2654 hsmartcard
->gState
= HAL_SMARTCARD_STATE_READY
;
2656 #if (USE_HAL_SMARTCARD_REGISTER_CALLBACKS == 1)
2657 /* Call registered Abort Transmit Complete Callback */
2658 hsmartcard
->AbortTransmitCpltCallback(hsmartcard
);
2660 /* Call legacy weak Abort Transmit Complete Callback */
2661 HAL_SMARTCARD_AbortTransmitCpltCallback(hsmartcard
);
2662 #endif /* USE_HAL_SMARTCARD_REGISTER_CALLBACK */
2666 * @brief DMA SMARTCARD Rx communication abort callback, when initiated by user by a call to
2667 * HAL_SMARTCARD_AbortReceive_IT API (Abort only Rx transfer)
2668 * (This callback is executed at end of DMA Rx Abort procedure following user abort request,
2669 * and leads to user Rx Abort Complete callback execution).
2670 * @param hdma DMA handle.
2673 static void SMARTCARD_DMARxOnlyAbortCallback(DMA_HandleTypeDef
*hdma
)
2675 SMARTCARD_HandleTypeDef
*hsmartcard
= (SMARTCARD_HandleTypeDef
*)(hdma
->Parent
);
2677 hsmartcard
->RxXferCount
= 0U;
2679 /* Clear the Error flags in the ICR register */
2680 __HAL_SMARTCARD_CLEAR_FLAG(hsmartcard
, SMARTCARD_CLEAR_OREF
| SMARTCARD_CLEAR_NEF
| SMARTCARD_CLEAR_PEF
| SMARTCARD_CLEAR_FEF
| SMARTCARD_CLEAR_RTOF
| SMARTCARD_CLEAR_EOBF
);
2682 /* Restore hsmartcard->RxState to Ready */
2683 hsmartcard
->RxState
= HAL_SMARTCARD_STATE_READY
;
2685 #if (USE_HAL_SMARTCARD_REGISTER_CALLBACKS == 1)
2686 /* Call registered Abort Receive Complete Callback */
2687 hsmartcard
->AbortReceiveCpltCallback(hsmartcard
);
2689 /* Call legacy weak Abort Receive Complete Callback */
2690 HAL_SMARTCARD_AbortReceiveCpltCallback(hsmartcard
);
2691 #endif /* USE_HAL_SMARTCARD_REGISTER_CALLBACK */
2695 * @brief Send an amount of data in non-blocking mode.
2696 * @note Function called under interruption only, once
2697 * interruptions have been enabled by HAL_SMARTCARD_Transmit_IT().
2698 * @param hsmartcard Pointer to a SMARTCARD_HandleTypeDef structure that contains
2699 * the configuration information for the specified SMARTCARD module.
2702 static void SMARTCARD_TxISR(SMARTCARD_HandleTypeDef
*hsmartcard
)
2704 /* Check that a Tx process is ongoing */
2705 if (hsmartcard
->gState
== HAL_SMARTCARD_STATE_BUSY_TX
)
2707 if (hsmartcard
->TxXferCount
== 0U)
2709 /* Disable the SMARTCARD Transmit Data Register Empty Interrupt */
2710 CLEAR_BIT(hsmartcard
->Instance
->CR1
, USART_CR1_TXEIE
);
2712 /* Enable the SMARTCARD Transmit Complete Interrupt */
2713 __HAL_SMARTCARD_ENABLE_IT(hsmartcard
, hsmartcard
->AdvancedInit
.TxCompletionIndication
);
2717 hsmartcard
->Instance
->TDR
= (uint8_t)(*hsmartcard
->pTxBuffPtr
& 0xFFU
);
2718 hsmartcard
->pTxBuffPtr
++;
2719 hsmartcard
->TxXferCount
--;
2725 * @brief Wrap up transmission in non-blocking mode.
2726 * @param hsmartcard Pointer to a SMARTCARD_HandleTypeDef structure that contains
2727 * the configuration information for the specified SMARTCARD module.
2730 static void SMARTCARD_EndTransmit_IT(SMARTCARD_HandleTypeDef
*hsmartcard
)
2732 /* Disable the SMARTCARD Transmit Complete Interrupt */
2733 __HAL_SMARTCARD_DISABLE_IT(hsmartcard
, hsmartcard
->AdvancedInit
.TxCompletionIndication
);
2735 /* Check if a receive process is ongoing or not. If not disable ERR IT */
2736 if (hsmartcard
->RxState
== HAL_SMARTCARD_STATE_READY
)
2738 /* Disable the SMARTCARD Error Interrupt: (Frame error) */
2739 CLEAR_BIT(hsmartcard
->Instance
->CR3
, USART_CR3_EIE
);
2742 /* Re-enable Rx at end of transmission if initial mode is Rx/Tx */
2743 if (hsmartcard
->Init
.Mode
== SMARTCARD_MODE_TX_RX
)
2745 /* Disable the Peripheral first to update modes */
2746 CLEAR_BIT(hsmartcard
->Instance
->CR1
, USART_CR1_UE
);
2747 SET_BIT(hsmartcard
->Instance
->CR1
, USART_CR1_RE
);
2748 /* Enable the Peripheral */
2749 SET_BIT(hsmartcard
->Instance
->CR1
, USART_CR1_UE
);
2752 /* Tx process is ended, restore hsmartcard->gState to Ready */
2753 hsmartcard
->gState
= HAL_SMARTCARD_STATE_READY
;
2755 /* Clear TxISR function pointer */
2756 hsmartcard
->TxISR
= NULL
;
2758 #if (USE_HAL_SMARTCARD_REGISTER_CALLBACKS == 1)
2759 /* Call registered Tx complete callback */
2760 hsmartcard
->TxCpltCallback(hsmartcard
);
2762 /* Call legacy weak Tx complete callback */
2763 HAL_SMARTCARD_TxCpltCallback(hsmartcard
);
2764 #endif /* USE_HAL_SMARTCARD_REGISTER_CALLBACK */
2768 * @brief Receive an amount of data in non-blocking mode.
2769 * @note Function called under interruption only, once
2770 * interruptions have been enabled by HAL_SMARTCARD_Receive_IT().
2771 * @param hsmartcard Pointer to a SMARTCARD_HandleTypeDef structure that contains
2772 * the configuration information for the specified SMARTCARD module.
2775 static void SMARTCARD_RxISR(SMARTCARD_HandleTypeDef
*hsmartcard
)
2777 /* Check that a Rx process is ongoing */
2778 if (hsmartcard
->RxState
== HAL_SMARTCARD_STATE_BUSY_RX
)
2780 *hsmartcard
->pRxBuffPtr
= (uint8_t)(hsmartcard
->Instance
->RDR
& (uint8_t)0xFF);
2781 hsmartcard
->pRxBuffPtr
++;
2783 hsmartcard
->RxXferCount
--;
2784 if (hsmartcard
->RxXferCount
== 0U)
2786 CLEAR_BIT(hsmartcard
->Instance
->CR1
, USART_CR1_RXNEIE
);
2788 /* Check if a transmit process is ongoing or not. If not disable ERR IT */
2789 if (hsmartcard
->gState
== HAL_SMARTCARD_STATE_READY
)
2791 /* Disable the SMARTCARD Error Interrupt: (Frame error, noise error, overrun error) */
2792 CLEAR_BIT(hsmartcard
->Instance
->CR3
, USART_CR3_EIE
);
2795 /* Disable the SMARTCARD Parity Error Interrupt */
2796 CLEAR_BIT(hsmartcard
->Instance
->CR1
, USART_CR1_PEIE
);
2798 hsmartcard
->RxState
= HAL_SMARTCARD_STATE_READY
;
2800 /* Clear RxISR function pointer */
2801 hsmartcard
->RxISR
= NULL
;
2803 #if (USE_HAL_SMARTCARD_REGISTER_CALLBACKS == 1)
2804 /* Call registered Rx complete callback */
2805 hsmartcard
->RxCpltCallback(hsmartcard
);
2807 /* Call legacy weak Rx complete callback */
2808 HAL_SMARTCARD_RxCpltCallback(hsmartcard
);
2809 #endif /* USE_HAL_SMARTCARD_REGISTER_CALLBACK */
2814 /* Clear RXNE interrupt flag */
2815 __HAL_SMARTCARD_SEND_REQ(hsmartcard
, SMARTCARD_RXDATA_FLUSH_REQUEST
);
2823 #endif /* HAL_SMARTCARD_MODULE_ENABLED */
2832 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/