2 ******************************************************************************
3 * @file stm32g4xx_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 (+) RxFifoFullCallback : Rx Fifo Full Callback.
119 (+) TxFifoEmptyCallback : Tx Fifo Empty Callback.
120 (+) MspInitCallback : SMARTCARD MspInit.
121 (+) MspDeInitCallback : SMARTCARD MspDeInit.
122 This function takes as parameters the HAL peripheral handle, the Callback ID
123 and a pointer to the user callback function.
126 Use function @ref HAL_SMARTCARD_UnRegisterCallback() to reset a callback to the default
127 weak (surcharged) function.
128 @ref HAL_SMARTCARD_UnRegisterCallback() takes as parameters the HAL peripheral handle,
130 This function allows to reset following callbacks:
131 (+) TxCpltCallback : Tx Complete Callback.
132 (+) RxCpltCallback : Rx Complete Callback.
133 (+) ErrorCallback : Error Callback.
134 (+) AbortCpltCallback : Abort Complete Callback.
135 (+) AbortTransmitCpltCallback : Abort Transmit Complete Callback.
136 (+) AbortReceiveCpltCallback : Abort Receive Complete Callback.
137 (+) RxFifoFullCallback : Rx Fifo Full Callback.
138 (+) TxFifoEmptyCallback : Tx Fifo Empty Callback.
139 (+) MspInitCallback : SMARTCARD MspInit.
140 (+) MspDeInitCallback : SMARTCARD MspDeInit.
143 By default, after the @ref HAL_SMARTCARD_Init() and when the state is HAL_SMARTCARD_STATE_RESET
144 all callbacks are set to the corresponding weak (surcharged) functions:
145 examples @ref HAL_SMARTCARD_TxCpltCallback(), @ref HAL_SMARTCARD_RxCpltCallback().
146 Exception done for MspInit and MspDeInit functions that are respectively
147 reset to the legacy weak (surcharged) functions in the @ref HAL_SMARTCARD_Init()
148 and @ref HAL_SMARTCARD_DeInit() only when these callbacks are null (not registered beforehand).
149 If not, MspInit or MspDeInit are not null, the @ref HAL_SMARTCARD_Init() and @ref HAL_SMARTCARD_DeInit()
150 keep and use the user MspInit/MspDeInit callbacks (registered beforehand).
153 Callbacks can be registered/unregistered in HAL_SMARTCARD_STATE_READY state only.
154 Exception done MspInit/MspDeInit that can be registered/unregistered
155 in HAL_SMARTCARD_STATE_READY or HAL_SMARTCARD_STATE_RESET state, thus registered (user)
156 MspInit/DeInit callbacks can be used during the Init/DeInit.
157 In that case first register the MspInit/MspDeInit user callbacks
158 using @ref HAL_SMARTCARD_RegisterCallback() before calling @ref HAL_SMARTCARD_DeInit()
159 or @ref HAL_SMARTCARD_Init() function.
162 When The compilation define USE_HAL_SMARTCARD_REGISTER_CALLBACKS is set to 0 or
163 not defined, the callback registration feature is not available
164 and weak (surcharged) callbacks are used.
168 ******************************************************************************
171 * <h2><center>© Copyright (c) 2017 STMicroelectronics.
172 * All rights reserved.</center></h2>
174 * This software component is licensed by ST under BSD 3-Clause license,
175 * the "License"; You may not use this file except in compliance with the
176 * License. You may obtain a copy of the License at:
177 * opensource.org/licenses/BSD-3-Clause
179 ******************************************************************************
182 /* Includes ------------------------------------------------------------------*/
183 #include "stm32g4xx_hal.h"
185 /** @addtogroup STM32G4xx_HAL_Driver
189 /** @defgroup SMARTCARD SMARTCARD
190 * @brief HAL SMARTCARD module driver
194 #ifdef HAL_SMARTCARD_MODULE_ENABLED
196 /* Private typedef -----------------------------------------------------------*/
197 /* Private define ------------------------------------------------------------*/
198 /** @defgroup SMARTCARD_Private_Constants SMARTCARD Private Constants
201 #define SMARTCARD_TEACK_REACK_TIMEOUT 1000U /*!< SMARTCARD TX or RX enable acknowledge time-out value */
203 #define USART_CR1_FIELDS ((uint32_t)(USART_CR1_M | USART_CR1_PCE | USART_CR1_PS | \
204 USART_CR1_TE | USART_CR1_RE | USART_CR1_OVER8| \
205 USART_CR1_FIFOEN )) /*!< USART CR1 fields of parameters set by SMARTCARD_SetConfig API */
207 #define USART_CR2_CLK_FIELDS ((uint32_t)(USART_CR2_CLKEN | USART_CR2_CPOL | USART_CR2_CPHA | \
208 USART_CR2_LBCL)) /*!< SMARTCARD clock-related USART CR2 fields of parameters */
210 #define USART_CR2_FIELDS ((uint32_t)(USART_CR2_RTOEN | USART_CR2_CLK_FIELDS | USART_CR2_STOP)) /*!< USART CR2 fields of parameters set by SMARTCARD_SetConfig API */
212 #define USART_CR3_FIELDS ((uint32_t)(USART_CR3_ONEBIT | USART_CR3_NACK | USART_CR3_SCARCNT | \
213 USART_CR3_TXFTCFG | USART_CR3_RXFTCFG )) /*!< USART CR3 fields of parameters set by SMARTCARD_SetConfig API */
215 #define USART_BRR_MIN 0x10U /*!< USART BRR minimum authorized value */
217 #define USART_BRR_MAX 0x0000FFFFU /*!< USART BRR maximum authorized value */
222 /* Private macros ------------------------------------------------------------*/
223 /* Private variables ---------------------------------------------------------*/
224 /* Private function prototypes -----------------------------------------------*/
225 /** @addtogroup SMARTCARD_Private_Functions
228 #if (USE_HAL_SMARTCARD_REGISTER_CALLBACKS == 1)
229 void SMARTCARD_InitCallbacksToDefault(SMARTCARD_HandleTypeDef
*hsmartcard
);
230 #endif /* USE_HAL_SMARTCARD_REGISTER_CALLBACKS */
231 static HAL_StatusTypeDef
SMARTCARD_SetConfig(SMARTCARD_HandleTypeDef
*hsmartcard
);
232 static void SMARTCARD_AdvFeatureConfig(SMARTCARD_HandleTypeDef
*hsmartcard
);
233 static HAL_StatusTypeDef
SMARTCARD_CheckIdleState(SMARTCARD_HandleTypeDef
*hsmartcard
);
234 static HAL_StatusTypeDef
SMARTCARD_WaitOnFlagUntilTimeout(SMARTCARD_HandleTypeDef
*hsmartcard
, uint32_t Flag
,
235 FlagStatus Status
, uint32_t Tickstart
, uint32_t Timeout
);
236 static void SMARTCARD_EndTxTransfer(SMARTCARD_HandleTypeDef
*hsmartcard
);
237 static void SMARTCARD_EndRxTransfer(SMARTCARD_HandleTypeDef
*hsmartcard
);
238 static void SMARTCARD_DMATransmitCplt(DMA_HandleTypeDef
*hdma
);
239 static void SMARTCARD_DMAReceiveCplt(DMA_HandleTypeDef
*hdma
);
240 static void SMARTCARD_DMAError(DMA_HandleTypeDef
*hdma
);
241 static void SMARTCARD_DMAAbortOnError(DMA_HandleTypeDef
*hdma
);
242 static void SMARTCARD_DMATxAbortCallback(DMA_HandleTypeDef
*hdma
);
243 static void SMARTCARD_DMARxAbortCallback(DMA_HandleTypeDef
*hdma
);
244 static void SMARTCARD_DMATxOnlyAbortCallback(DMA_HandleTypeDef
*hdma
);
245 static void SMARTCARD_DMARxOnlyAbortCallback(DMA_HandleTypeDef
*hdma
);
246 static void SMARTCARD_TxISR(SMARTCARD_HandleTypeDef
*hsmartcard
);
247 static void SMARTCARD_TxISR_FIFOEN(SMARTCARD_HandleTypeDef
*hsmartcard
);
248 static void SMARTCARD_EndTransmit_IT(SMARTCARD_HandleTypeDef
*hsmartcard
);
249 static void SMARTCARD_RxISR(SMARTCARD_HandleTypeDef
*hsmartcard
);
250 static void SMARTCARD_RxISR_FIFOEN(SMARTCARD_HandleTypeDef
*hsmartcard
);
255 /* Exported functions --------------------------------------------------------*/
257 /** @defgroup SMARTCARD_Exported_Functions SMARTCARD Exported Functions
261 /** @defgroup SMARTCARD_Exported_Functions_Group1 Initialization and de-initialization functions
262 * @brief Initialization and Configuration functions
265 ==============================================================================
266 ##### Initialization and Configuration functions #####
267 ==============================================================================
269 This subsection provides a set of functions allowing to initialize the USARTx
270 associated to the SmartCard.
271 (+) These parameters can be configured:
273 (++) Parity: parity should be enabled, frame Length is fixed to 8 bits plus parity
274 (++) Receiver/transmitter modes
275 (++) Synchronous mode (and if enabled, phase, polarity and last bit parameters)
278 (++) NACK enabling or disabling on transmission error
280 (+) The following advanced features can be configured as well:
281 (++) TX and/or RX pin level inversion
282 (++) data logical level inversion
283 (++) RX and TX pins swap
284 (++) RX overrun detection disabling
285 (++) DMA disabling on RX error
286 (++) MSB first on communication line
287 (++) Time out enabling (and if activated, timeout value)
289 (++) Auto-retry counter
291 The HAL_SMARTCARD_Init() API follows the USART synchronous configuration procedures
292 (details for the procedures are available in reference manual).
296 The USART frame format is given in the following table:
298 Table 1. USART frame format.
299 +---------------------------------------------------------------+
300 | M1M0 bits | PCE bit | USART frame |
301 |-----------------------|---------------------------------------|
302 | 01 | 1 | | SB | 8 bit data | PB | STB | |
303 +---------------------------------------------------------------+
310 * @brief Initialize the SMARTCARD mode according to the specified
311 * parameters in the SMARTCARD_HandleTypeDef and initialize the associated handle.
312 * @param hsmartcard Pointer to a SMARTCARD_HandleTypeDef structure that contains
313 * the configuration information for the specified SMARTCARD module.
316 HAL_StatusTypeDef
HAL_SMARTCARD_Init(SMARTCARD_HandleTypeDef
*hsmartcard
)
318 /* Check the SMARTCARD handle allocation */
319 if (hsmartcard
== NULL
)
324 /* Check the USART associated to the SMARTCARD handle */
325 assert_param(IS_SMARTCARD_INSTANCE(hsmartcard
->Instance
));
327 if (hsmartcard
->gState
== HAL_SMARTCARD_STATE_RESET
)
329 /* Allocate lock resource and initialize it */
330 hsmartcard
->Lock
= HAL_UNLOCKED
;
332 #if USE_HAL_SMARTCARD_REGISTER_CALLBACKS == 1
333 SMARTCARD_InitCallbacksToDefault(hsmartcard
);
335 if (hsmartcard
->MspInitCallback
== NULL
)
337 hsmartcard
->MspInitCallback
= HAL_SMARTCARD_MspInit
;
340 /* Init the low level hardware */
341 hsmartcard
->MspInitCallback(hsmartcard
);
343 /* Init the low level hardware : GPIO, CLOCK */
344 HAL_SMARTCARD_MspInit(hsmartcard
);
345 #endif /* USE_HAL_SMARTCARD_REGISTER_CALLBACKS */
348 hsmartcard
->gState
= HAL_SMARTCARD_STATE_BUSY
;
350 /* Disable the Peripheral to set smartcard mode */
351 CLEAR_BIT(hsmartcard
->Instance
->CR1
, USART_CR1_UE
);
353 /* In SmartCard mode, the following bits must be kept cleared:
354 - LINEN in the USART_CR2 register,
355 - HDSEL and IREN bits in the USART_CR3 register.*/
356 CLEAR_BIT(hsmartcard
->Instance
->CR2
, USART_CR2_LINEN
);
357 CLEAR_BIT(hsmartcard
->Instance
->CR3
, (USART_CR3_HDSEL
| USART_CR3_IREN
));
359 /* set the USART in SMARTCARD mode */
360 SET_BIT(hsmartcard
->Instance
->CR3
, USART_CR3_SCEN
);
362 /* Set the SMARTCARD Communication parameters */
363 if (SMARTCARD_SetConfig(hsmartcard
) == HAL_ERROR
)
368 /* Set the SMARTCARD transmission completion indication */
369 SMARTCARD_TRANSMISSION_COMPLETION_SETTING(hsmartcard
);
371 if (hsmartcard
->AdvancedInit
.AdvFeatureInit
!= SMARTCARD_ADVFEATURE_NO_INIT
)
373 SMARTCARD_AdvFeatureConfig(hsmartcard
);
376 /* Enable the Peripheral */
377 SET_BIT(hsmartcard
->Instance
->CR1
, USART_CR1_UE
);
379 /* TEACK and/or REACK to check before moving hsmartcard->gState and hsmartcard->RxState to Ready */
380 return (SMARTCARD_CheckIdleState(hsmartcard
));
384 * @brief DeInitialize the SMARTCARD peripheral.
385 * @param hsmartcard Pointer to a SMARTCARD_HandleTypeDef structure that contains
386 * the configuration information for the specified SMARTCARD module.
389 HAL_StatusTypeDef
HAL_SMARTCARD_DeInit(SMARTCARD_HandleTypeDef
*hsmartcard
)
391 /* Check the SMARTCARD handle allocation */
392 if (hsmartcard
== NULL
)
397 /* Check the USART/UART associated to the SMARTCARD handle */
398 assert_param(IS_SMARTCARD_INSTANCE(hsmartcard
->Instance
));
400 hsmartcard
->gState
= HAL_SMARTCARD_STATE_BUSY
;
402 /* Disable the Peripheral */
403 CLEAR_BIT(hsmartcard
->Instance
->CR1
, USART_CR1_UE
);
405 WRITE_REG(hsmartcard
->Instance
->CR1
, 0x0U
);
406 WRITE_REG(hsmartcard
->Instance
->CR2
, 0x0U
);
407 WRITE_REG(hsmartcard
->Instance
->CR3
, 0x0U
);
408 WRITE_REG(hsmartcard
->Instance
->RTOR
, 0x0U
);
409 WRITE_REG(hsmartcard
->Instance
->GTPR
, 0x0U
);
411 /* DeInit the low level hardware */
412 #if USE_HAL_SMARTCARD_REGISTER_CALLBACKS == 1
413 if (hsmartcard
->MspDeInitCallback
== NULL
)
415 hsmartcard
->MspDeInitCallback
= HAL_SMARTCARD_MspDeInit
;
417 /* DeInit the low level hardware */
418 hsmartcard
->MspDeInitCallback(hsmartcard
);
420 HAL_SMARTCARD_MspDeInit(hsmartcard
);
421 #endif /* USE_HAL_SMARTCARD_REGISTER_CALLBACKS */
423 hsmartcard
->ErrorCode
= HAL_SMARTCARD_ERROR_NONE
;
424 hsmartcard
->gState
= HAL_SMARTCARD_STATE_RESET
;
425 hsmartcard
->RxState
= HAL_SMARTCARD_STATE_RESET
;
428 __HAL_UNLOCK(hsmartcard
);
434 * @brief Initialize the SMARTCARD MSP.
435 * @param hsmartcard Pointer to a SMARTCARD_HandleTypeDef structure that contains
436 * the configuration information for the specified SMARTCARD module.
439 __weak
void HAL_SMARTCARD_MspInit(SMARTCARD_HandleTypeDef
*hsmartcard
)
441 /* Prevent unused argument(s) compilation warning */
444 /* NOTE : This function should not be modified, when the callback is needed,
445 the HAL_SMARTCARD_MspInit can be implemented in the user file
450 * @brief DeInitialize the SMARTCARD MSP.
451 * @param hsmartcard Pointer to a SMARTCARD_HandleTypeDef structure that contains
452 * the configuration information for the specified SMARTCARD module.
455 __weak
void HAL_SMARTCARD_MspDeInit(SMARTCARD_HandleTypeDef
*hsmartcard
)
457 /* Prevent unused argument(s) compilation warning */
460 /* NOTE : This function should not be modified, when the callback is needed,
461 the HAL_SMARTCARD_MspDeInit can be implemented in the user file
465 #if (USE_HAL_SMARTCARD_REGISTER_CALLBACKS == 1)
467 * @brief Register a User SMARTCARD Callback
468 * To be used instead of the weak predefined callback
469 * @param hsmartcard smartcard handle
470 * @param CallbackID ID of the callback to be registered
471 * This parameter can be one of the following values:
472 * @arg @ref HAL_SMARTCARD_TX_COMPLETE_CB_ID Tx Complete Callback ID
473 * @arg @ref HAL_SMARTCARD_RX_COMPLETE_CB_ID Rx Complete Callback ID
474 * @arg @ref HAL_SMARTCARD_ERROR_CB_ID Error Callback ID
475 * @arg @ref HAL_SMARTCARD_ABORT_COMPLETE_CB_ID Abort Complete Callback ID
476 * @arg @ref HAL_SMARTCARD_ABORT_TRANSMIT_COMPLETE_CB_ID Abort Transmit Complete Callback ID
477 * @arg @ref HAL_SMARTCARD_ABORT_RECEIVE_COMPLETE_CB_ID Abort Receive Complete Callback ID
478 * @arg @ref HAL_SMARTCARD_RX_FIFO_FULL_CB_ID Rx Fifo Full Callback ID
479 * @arg @ref HAL_SMARTCARD_TX_FIFO_EMPTY_CB_ID Tx Fifo Empty Callback ID
480 * @arg @ref HAL_SMARTCARD_MSPINIT_CB_ID MspInit Callback ID
481 * @arg @ref HAL_SMARTCARD_MSPDEINIT_CB_ID MspDeInit Callback ID
482 * @param pCallback pointer to the Callback function
485 HAL_StatusTypeDef
HAL_SMARTCARD_RegisterCallback(SMARTCARD_HandleTypeDef
*hsmartcard
,
486 HAL_SMARTCARD_CallbackIDTypeDef CallbackID
, pSMARTCARD_CallbackTypeDef pCallback
)
488 HAL_StatusTypeDef status
= HAL_OK
;
490 if (pCallback
== NULL
)
492 /* Update the error code */
493 hsmartcard
->ErrorCode
|= HAL_SMARTCARD_ERROR_INVALID_CALLBACK
;
498 __HAL_LOCK(hsmartcard
);
500 if (hsmartcard
->gState
== HAL_SMARTCARD_STATE_READY
)
505 case HAL_SMARTCARD_TX_COMPLETE_CB_ID
:
506 hsmartcard
->TxCpltCallback
= pCallback
;
509 case HAL_SMARTCARD_RX_COMPLETE_CB_ID
:
510 hsmartcard
->RxCpltCallback
= pCallback
;
513 case HAL_SMARTCARD_ERROR_CB_ID
:
514 hsmartcard
->ErrorCallback
= pCallback
;
517 case HAL_SMARTCARD_ABORT_COMPLETE_CB_ID
:
518 hsmartcard
->AbortCpltCallback
= pCallback
;
521 case HAL_SMARTCARD_ABORT_TRANSMIT_COMPLETE_CB_ID
:
522 hsmartcard
->AbortTransmitCpltCallback
= pCallback
;
525 case HAL_SMARTCARD_ABORT_RECEIVE_COMPLETE_CB_ID
:
526 hsmartcard
->AbortReceiveCpltCallback
= pCallback
;
529 case HAL_SMARTCARD_RX_FIFO_FULL_CB_ID
:
530 hsmartcard
->RxFifoFullCallback
= pCallback
;
533 case HAL_SMARTCARD_TX_FIFO_EMPTY_CB_ID
:
534 hsmartcard
->TxFifoEmptyCallback
= pCallback
;
537 case HAL_SMARTCARD_MSPINIT_CB_ID
:
538 hsmartcard
->MspInitCallback
= pCallback
;
541 case HAL_SMARTCARD_MSPDEINIT_CB_ID
:
542 hsmartcard
->MspDeInitCallback
= pCallback
;
546 /* Update the error code */
547 hsmartcard
->ErrorCode
|= HAL_SMARTCARD_ERROR_INVALID_CALLBACK
;
549 /* Return error status */
554 else if (hsmartcard
->gState
== HAL_SMARTCARD_STATE_RESET
)
558 case HAL_SMARTCARD_MSPINIT_CB_ID
:
559 hsmartcard
->MspInitCallback
= pCallback
;
562 case HAL_SMARTCARD_MSPDEINIT_CB_ID
:
563 hsmartcard
->MspDeInitCallback
= pCallback
;
567 /* Update the error code */
568 hsmartcard
->ErrorCode
|= HAL_SMARTCARD_ERROR_INVALID_CALLBACK
;
570 /* Return error status */
577 /* Update the error code */
578 hsmartcard
->ErrorCode
|= HAL_SMARTCARD_ERROR_INVALID_CALLBACK
;
580 /* Return error status */
585 __HAL_UNLOCK(hsmartcard
);
591 * @brief Unregister an SMARTCARD callback
592 * SMARTCARD callback is redirected to the weak predefined callback
593 * @param hsmartcard smartcard handle
594 * @param CallbackID ID of the callback to be unregistered
595 * This parameter can be one of the following values:
596 * @arg @ref HAL_SMARTCARD_TX_COMPLETE_CB_ID Tx Complete Callback ID
597 * @arg @ref HAL_SMARTCARD_RX_COMPLETE_CB_ID Rx Complete Callback ID
598 * @arg @ref HAL_SMARTCARD_ERROR_CB_ID Error Callback ID
599 * @arg @ref HAL_SMARTCARD_ABORT_COMPLETE_CB_ID Abort Complete Callback ID
600 * @arg @ref HAL_SMARTCARD_ABORT_TRANSMIT_COMPLETE_CB_ID Abort Transmit Complete Callback ID
601 * @arg @ref HAL_SMARTCARD_ABORT_RECEIVE_COMPLETE_CB_ID Abort Receive Complete Callback ID
602 * @arg @ref HAL_SMARTCARD_RX_FIFO_FULL_CB_ID Rx Fifo Full Callback ID
603 * @arg @ref HAL_SMARTCARD_TX_FIFO_EMPTY_CB_ID Tx Fifo Empty Callback ID
604 * @arg @ref HAL_SMARTCARD_MSPINIT_CB_ID MspInit Callback ID
605 * @arg @ref HAL_SMARTCARD_MSPDEINIT_CB_ID MspDeInit Callback ID
608 HAL_StatusTypeDef
HAL_SMARTCARD_UnRegisterCallback(SMARTCARD_HandleTypeDef
*hsmartcard
,
609 HAL_SMARTCARD_CallbackIDTypeDef CallbackID
)
611 HAL_StatusTypeDef status
= HAL_OK
;
614 __HAL_LOCK(hsmartcard
);
616 if (HAL_SMARTCARD_STATE_READY
== hsmartcard
->gState
)
620 case HAL_SMARTCARD_TX_COMPLETE_CB_ID
:
621 hsmartcard
->TxCpltCallback
= HAL_SMARTCARD_TxCpltCallback
; /* Legacy weak TxCpltCallback */
624 case HAL_SMARTCARD_RX_COMPLETE_CB_ID
:
625 hsmartcard
->RxCpltCallback
= HAL_SMARTCARD_RxCpltCallback
; /* Legacy weak RxCpltCallback */
628 case HAL_SMARTCARD_ERROR_CB_ID
:
629 hsmartcard
->ErrorCallback
= HAL_SMARTCARD_ErrorCallback
; /* Legacy weak ErrorCallback */
632 case HAL_SMARTCARD_ABORT_COMPLETE_CB_ID
:
633 hsmartcard
->AbortCpltCallback
= HAL_SMARTCARD_AbortCpltCallback
; /* Legacy weak AbortCpltCallback */
636 case HAL_SMARTCARD_ABORT_TRANSMIT_COMPLETE_CB_ID
:
637 hsmartcard
->AbortTransmitCpltCallback
= HAL_SMARTCARD_AbortTransmitCpltCallback
; /* Legacy weak AbortTransmitCpltCallback */
640 case HAL_SMARTCARD_ABORT_RECEIVE_COMPLETE_CB_ID
:
641 hsmartcard
->AbortReceiveCpltCallback
= HAL_SMARTCARD_AbortReceiveCpltCallback
; /* Legacy weak AbortReceiveCpltCallback */
644 case HAL_SMARTCARD_RX_FIFO_FULL_CB_ID
:
645 hsmartcard
->RxFifoFullCallback
= HAL_SMARTCARDEx_RxFifoFullCallback
; /* Legacy weak RxFifoFullCallback */
648 case HAL_SMARTCARD_TX_FIFO_EMPTY_CB_ID
:
649 hsmartcard
->TxFifoEmptyCallback
= HAL_SMARTCARDEx_TxFifoEmptyCallback
; /* Legacy weak TxFifoEmptyCallback */
652 case HAL_SMARTCARD_MSPINIT_CB_ID
:
653 hsmartcard
->MspInitCallback
= HAL_SMARTCARD_MspInit
; /* Legacy weak MspInitCallback */
656 case HAL_SMARTCARD_MSPDEINIT_CB_ID
:
657 hsmartcard
->MspDeInitCallback
= HAL_SMARTCARD_MspDeInit
; /* Legacy weak MspDeInitCallback */
661 /* Update the error code */
662 hsmartcard
->ErrorCode
|= HAL_SMARTCARD_ERROR_INVALID_CALLBACK
;
664 /* Return error status */
669 else if (HAL_SMARTCARD_STATE_RESET
== hsmartcard
->gState
)
673 case HAL_SMARTCARD_MSPINIT_CB_ID
:
674 hsmartcard
->MspInitCallback
= HAL_SMARTCARD_MspInit
;
677 case HAL_SMARTCARD_MSPDEINIT_CB_ID
:
678 hsmartcard
->MspDeInitCallback
= HAL_SMARTCARD_MspDeInit
;
682 /* Update the error code */
683 hsmartcard
->ErrorCode
|= HAL_SMARTCARD_ERROR_INVALID_CALLBACK
;
685 /* Return error status */
692 /* Update the error code */
693 hsmartcard
->ErrorCode
|= HAL_SMARTCARD_ERROR_INVALID_CALLBACK
;
695 /* Return error status */
700 __HAL_UNLOCK(hsmartcard
);
704 #endif /* USE_HAL_SMARTCARD_REGISTER_CALLBACKS */
710 /** @defgroup SMARTCARD_Exported_Functions_Group2 IO operation functions
711 * @brief SMARTCARD Transmit and Receive functions
714 ==============================================================================
715 ##### IO operation functions #####
716 ==============================================================================
718 This subsection provides a set of functions allowing to manage the SMARTCARD data transfers.
721 Smartcard is a single wire half duplex communication protocol.
722 The Smartcard interface is designed to support asynchronous protocol Smartcards as
723 defined in the ISO 7816-3 standard. The USART should be configured as:
724 (+) 8 bits plus parity: where M=1 and PCE=1 in the USART_CR1 register
725 (+) 1.5 stop bits when transmitting and receiving: where STOP=11 in the USART_CR2 register.
728 (+) There are two modes of transfer:
729 (++) Blocking mode: The communication is performed in polling mode.
730 The HAL status of all data processing is returned by the same function
731 after finishing transfer.
732 (++) Non-Blocking mode: The communication is performed using Interrupts
733 or DMA, the relevant API's return the HAL status.
734 The end of the data processing will be indicated through the
735 dedicated SMARTCARD IRQ when using Interrupt mode or the DMA IRQ when
737 (++) The HAL_SMARTCARD_TxCpltCallback(), HAL_SMARTCARD_RxCpltCallback() user callbacks
738 will be executed respectively at the end of the Transmit or Receive process
739 The HAL_SMARTCARD_ErrorCallback() user callback will be executed when a communication
742 (+) Blocking mode APIs are :
743 (++) HAL_SMARTCARD_Transmit()
744 (++) HAL_SMARTCARD_Receive()
746 (+) Non Blocking mode APIs with Interrupt are :
747 (++) HAL_SMARTCARD_Transmit_IT()
748 (++) HAL_SMARTCARD_Receive_IT()
749 (++) HAL_SMARTCARD_IRQHandler()
751 (+) Non Blocking mode functions with DMA are :
752 (++) HAL_SMARTCARD_Transmit_DMA()
753 (++) HAL_SMARTCARD_Receive_DMA()
755 (+) A set of Transfer Complete Callbacks are provided in non Blocking mode:
756 (++) HAL_SMARTCARD_TxCpltCallback()
757 (++) HAL_SMARTCARD_RxCpltCallback()
758 (++) HAL_SMARTCARD_ErrorCallback()
760 (#) Non-Blocking mode transfers could be aborted using Abort API's :
761 (+) HAL_SMARTCARD_Abort()
762 (+) HAL_SMARTCARD_AbortTransmit()
763 (+) HAL_SMARTCARD_AbortReceive()
764 (+) HAL_SMARTCARD_Abort_IT()
765 (+) HAL_SMARTCARD_AbortTransmit_IT()
766 (+) HAL_SMARTCARD_AbortReceive_IT()
768 (#) For Abort services based on interrupts (HAL_SMARTCARD_Abortxxx_IT), a set of Abort Complete Callbacks are provided:
769 (+) HAL_SMARTCARD_AbortCpltCallback()
770 (+) HAL_SMARTCARD_AbortTransmitCpltCallback()
771 (+) HAL_SMARTCARD_AbortReceiveCpltCallback()
773 (#) In Non-Blocking mode transfers, possible errors are split into 2 categories.
774 Errors are handled as follows :
775 (+) Error is considered as Recoverable and non blocking : Transfer could go till end, but error severity is
776 to be evaluated by user : this concerns Frame Error, Parity Error or Noise Error in Interrupt mode reception .
777 Received character is then retrieved and stored in Rx buffer, Error code is set to allow user to identify error type,
778 and HAL_SMARTCARD_ErrorCallback() user callback is executed. Transfer is kept ongoing on SMARTCARD side.
779 If user wants to abort it, Abort services should be called by user.
780 (+) Error is considered as Blocking : Transfer could not be completed properly and is aborted.
781 This concerns Frame Error in Interrupt mode tranmission, Overrun Error in Interrupt mode reception and all errors in DMA mode.
782 Error code is set to allow user to identify error type, and HAL_SMARTCARD_ErrorCallback() user callback is executed.
789 * @brief Send an amount of data in blocking mode.
790 * @note When FIFO mode is enabled, writing a data in the TDR register adds one
791 * data to the TXFIFO. Write operations to the TDR register are performed
792 * when TXFNF flag is set. From hardware perspective, TXFNF flag and
793 * TXE are mapped on the same bit-field.
794 * @param hsmartcard Pointer to a SMARTCARD_HandleTypeDef structure that contains
795 * the configuration information for the specified SMARTCARD module.
796 * @param pData pointer to data buffer.
797 * @param Size amount of data to be sent.
798 * @param Timeout Timeout duration.
801 HAL_StatusTypeDef
HAL_SMARTCARD_Transmit(SMARTCARD_HandleTypeDef
*hsmartcard
, uint8_t *pData
, uint16_t Size
,
805 uint8_t *ptmpdata
= pData
;
807 /* Check that a Tx process is not already ongoing */
808 if (hsmartcard
->gState
== HAL_SMARTCARD_STATE_READY
)
810 if ((ptmpdata
== NULL
) || (Size
== 0U))
816 __HAL_LOCK(hsmartcard
);
818 hsmartcard
->gState
= HAL_SMARTCARD_STATE_BUSY_TX
;
820 /* Init tickstart for timeout management */
821 tickstart
= HAL_GetTick();
823 /* Disable the Peripheral first to update mode for TX master */
824 CLEAR_BIT(hsmartcard
->Instance
->CR1
, USART_CR1_UE
);
826 /* Disable Rx, enable Tx */
827 CLEAR_BIT(hsmartcard
->Instance
->CR1
, USART_CR1_RE
);
828 SET_BIT(hsmartcard
->Instance
->RQR
, (uint16_t)SMARTCARD_RXDATA_FLUSH_REQUEST
);
829 SET_BIT(hsmartcard
->Instance
->CR1
, USART_CR1_TE
);
831 /* Enable the Peripheral */
832 SET_BIT(hsmartcard
->Instance
->CR1
, USART_CR1_UE
);
834 hsmartcard
->ErrorCode
= HAL_SMARTCARD_ERROR_NONE
;
835 hsmartcard
->TxXferSize
= Size
;
836 hsmartcard
->TxXferCount
= Size
;
838 while (hsmartcard
->TxXferCount
> 0U)
840 hsmartcard
->TxXferCount
--;
841 if (SMARTCARD_WaitOnFlagUntilTimeout(hsmartcard
, SMARTCARD_FLAG_TXE
, RESET
, tickstart
, Timeout
) != HAL_OK
)
845 hsmartcard
->Instance
->TDR
= (uint8_t)(*ptmpdata
& 0xFFU
);
848 if (SMARTCARD_WaitOnFlagUntilTimeout(hsmartcard
, SMARTCARD_TRANSMISSION_COMPLETION_FLAG(hsmartcard
), RESET
, tickstart
,
853 /* Re-enable Rx at end of transmission if initial mode is Rx/Tx */
854 if (hsmartcard
->Init
.Mode
== SMARTCARD_MODE_TX_RX
)
856 /* Disable the Peripheral first to update modes */
857 CLEAR_BIT(hsmartcard
->Instance
->CR1
, USART_CR1_UE
);
858 SET_BIT(hsmartcard
->Instance
->CR1
, USART_CR1_RE
);
859 /* Enable the Peripheral */
860 SET_BIT(hsmartcard
->Instance
->CR1
, USART_CR1_UE
);
863 /* At end of Tx process, restore hsmartcard->gState to Ready */
864 hsmartcard
->gState
= HAL_SMARTCARD_STATE_READY
;
866 /* Process Unlocked */
867 __HAL_UNLOCK(hsmartcard
);
878 * @brief Receive an amount of data in blocking mode.
879 * @note When FIFO mode is enabled, the RXFNE flag is set as long as the RXFIFO
880 * is not empty. Read operations from the RDR register are performed when
881 * RXFNE flag is set. From hardware perspective, RXFNE flag and
882 * RXNE are mapped on the same bit-field.
883 * @param hsmartcard Pointer to a SMARTCARD_HandleTypeDef structure that contains
884 * the configuration information for the specified SMARTCARD module.
885 * @param pData pointer to data buffer.
886 * @param Size amount of data to be received.
887 * @param Timeout Timeout duration.
890 HAL_StatusTypeDef
HAL_SMARTCARD_Receive(SMARTCARD_HandleTypeDef
*hsmartcard
, uint8_t *pData
, uint16_t Size
,
894 uint8_t *ptmpdata
= pData
;
896 /* Check that a Rx process is not already ongoing */
897 if (hsmartcard
->RxState
== HAL_SMARTCARD_STATE_READY
)
899 if ((ptmpdata
== NULL
) || (Size
== 0U))
905 __HAL_LOCK(hsmartcard
);
907 hsmartcard
->ErrorCode
= HAL_SMARTCARD_ERROR_NONE
;
908 hsmartcard
->RxState
= HAL_SMARTCARD_STATE_BUSY_RX
;
910 /* Init tickstart for timeout management */
911 tickstart
= HAL_GetTick();
913 hsmartcard
->RxXferSize
= Size
;
914 hsmartcard
->RxXferCount
= Size
;
916 /* Check the remain data to be received */
917 while (hsmartcard
->RxXferCount
> 0U)
919 hsmartcard
->RxXferCount
--;
921 if (SMARTCARD_WaitOnFlagUntilTimeout(hsmartcard
, SMARTCARD_FLAG_RXNE
, RESET
, tickstart
, Timeout
) != HAL_OK
)
925 *ptmpdata
= (uint8_t)(hsmartcard
->Instance
->RDR
& (uint8_t)0x00FF);
929 /* At end of Rx process, restore hsmartcard->RxState to Ready */
930 hsmartcard
->RxState
= HAL_SMARTCARD_STATE_READY
;
932 /* Process Unlocked */
933 __HAL_UNLOCK(hsmartcard
);
944 * @brief Send an amount of data in interrupt mode.
945 * @note When FIFO mode is disabled, USART interrupt is generated whenever
946 * USART_TDR register is empty, i.e one interrupt per data to transmit.
947 * @note When FIFO mode is enabled, USART interrupt is generated whenever
948 * TXFIFO threshold reached. In that case the interrupt rate depends on
949 * TXFIFO threshold configuration.
950 * @note This function sets the hsmartcard->TxIsr function pointer according to
951 * the FIFO mode (data transmission processing depends on FIFO mode).
952 * @param hsmartcard Pointer to a SMARTCARD_HandleTypeDef structure that contains
953 * the configuration information for the specified SMARTCARD module.
954 * @param pData pointer to data buffer.
955 * @param Size amount of data to be sent.
958 HAL_StatusTypeDef
HAL_SMARTCARD_Transmit_IT(SMARTCARD_HandleTypeDef
*hsmartcard
, uint8_t *pData
, uint16_t Size
)
960 /* Check that a Tx process is not already ongoing */
961 if (hsmartcard
->gState
== HAL_SMARTCARD_STATE_READY
)
963 if ((pData
== NULL
) || (Size
== 0U))
969 __HAL_LOCK(hsmartcard
);
971 hsmartcard
->ErrorCode
= HAL_SMARTCARD_ERROR_NONE
;
972 hsmartcard
->gState
= HAL_SMARTCARD_STATE_BUSY_TX
;
974 hsmartcard
->pTxBuffPtr
= pData
;
975 hsmartcard
->TxXferSize
= Size
;
976 hsmartcard
->TxXferCount
= Size
;
977 hsmartcard
->TxISR
= NULL
;
979 /* Disable the Peripheral first to update mode for TX master */
980 CLEAR_BIT(hsmartcard
->Instance
->CR1
, USART_CR1_UE
);
982 /* Disable Rx, enable Tx */
983 CLEAR_BIT(hsmartcard
->Instance
->CR1
, USART_CR1_RE
);
984 SET_BIT(hsmartcard
->Instance
->RQR
, (uint16_t)SMARTCARD_RXDATA_FLUSH_REQUEST
);
985 SET_BIT(hsmartcard
->Instance
->CR1
, USART_CR1_TE
);
987 /* Enable the Peripheral */
988 SET_BIT(hsmartcard
->Instance
->CR1
, USART_CR1_UE
);
990 /* Configure Tx interrupt processing */
991 if (hsmartcard
->FifoMode
== SMARTCARD_FIFOMODE_ENABLE
)
993 /* Set the Tx ISR function pointer */
994 hsmartcard
->TxISR
= SMARTCARD_TxISR_FIFOEN
;
996 /* Process Unlocked */
997 __HAL_UNLOCK(hsmartcard
);
999 /* Enable the SMARTCARD Error Interrupt: (Frame error) */
1000 SET_BIT(hsmartcard
->Instance
->CR3
, USART_CR3_EIE
);
1002 /* Enable the TX FIFO threshold interrupt */
1003 SET_BIT(hsmartcard
->Instance
->CR3
, USART_CR3_TXFTIE
);
1007 /* Set the Tx ISR function pointer */
1008 hsmartcard
->TxISR
= SMARTCARD_TxISR
;
1010 /* Process Unlocked */
1011 __HAL_UNLOCK(hsmartcard
);
1013 /* Enable the SMARTCARD Error Interrupt: (Frame error) */
1014 SET_BIT(hsmartcard
->Instance
->CR3
, USART_CR3_EIE
);
1016 /* Enable the SMARTCARD Transmit Data Register Empty Interrupt */
1017 SET_BIT(hsmartcard
->Instance
->CR1
, USART_CR1_TXEIE_TXFNFIE
);
1029 * @brief Receive an amount of data in interrupt mode.
1030 * @note When FIFO mode is disabled, USART interrupt is generated whenever
1031 * USART_RDR register can be read, i.e one interrupt per data to receive.
1032 * @note When FIFO mode is enabled, USART interrupt is generated whenever
1033 * RXFIFO threshold reached. In that case the interrupt rate depends on
1034 * RXFIFO threshold configuration.
1035 * @note This function sets the hsmartcard->RxIsr function pointer according to
1036 * the FIFO mode (data reception processing depends on FIFO mode).
1037 * @param hsmartcard Pointer to a SMARTCARD_HandleTypeDef structure that contains
1038 * the configuration information for the specified SMARTCARD module.
1039 * @param pData pointer to data buffer.
1040 * @param Size amount of data to be received.
1041 * @retval HAL status
1043 HAL_StatusTypeDef
HAL_SMARTCARD_Receive_IT(SMARTCARD_HandleTypeDef
*hsmartcard
, uint8_t *pData
, uint16_t Size
)
1045 /* Check that a Rx process is not already ongoing */
1046 if (hsmartcard
->RxState
== HAL_SMARTCARD_STATE_READY
)
1048 if ((pData
== NULL
) || (Size
== 0U))
1053 /* Process Locked */
1054 __HAL_LOCK(hsmartcard
);
1056 hsmartcard
->ErrorCode
= HAL_SMARTCARD_ERROR_NONE
;
1057 hsmartcard
->RxState
= HAL_SMARTCARD_STATE_BUSY_RX
;
1059 hsmartcard
->pRxBuffPtr
= pData
;
1060 hsmartcard
->RxXferSize
= Size
;
1061 hsmartcard
->RxXferCount
= Size
;
1063 /* Configure Rx interrupt processing */
1064 if ((hsmartcard
->FifoMode
== SMARTCARD_FIFOMODE_ENABLE
) && (Size
>= hsmartcard
->NbRxDataToProcess
))
1066 /* Set the Rx ISR function pointer */
1067 hsmartcard
->RxISR
= SMARTCARD_RxISR_FIFOEN
;
1069 /* Process Unlocked */
1070 __HAL_UNLOCK(hsmartcard
);
1072 /* Enable the SMARTCART Parity Error interrupt and RX FIFO Threshold interrupt */
1073 SET_BIT(hsmartcard
->Instance
->CR1
, USART_CR1_PEIE
);
1074 SET_BIT(hsmartcard
->Instance
->CR3
, USART_CR3_RXFTIE
);
1078 /* Set the Rx ISR function pointer */
1079 hsmartcard
->RxISR
= SMARTCARD_RxISR
;
1081 /* Process Unlocked */
1082 __HAL_UNLOCK(hsmartcard
);
1084 /* Enable the SMARTCARD Parity Error and Data Register not empty Interrupts */
1085 SET_BIT(hsmartcard
->Instance
->CR1
, USART_CR1_PEIE
| USART_CR1_RXNEIE_RXFNEIE
);
1088 /* Enable the SMARTCARD Error Interrupt: (Frame error, noise error, overrun error) */
1089 SET_BIT(hsmartcard
->Instance
->CR3
, USART_CR3_EIE
);
1100 * @brief Send 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 sent.
1105 * @retval HAL status
1107 HAL_StatusTypeDef
HAL_SMARTCARD_Transmit_DMA(SMARTCARD_HandleTypeDef
*hsmartcard
, uint8_t *pData
, uint16_t Size
)
1109 /* Check that a Tx process is not already ongoing */
1110 if (hsmartcard
->gState
== HAL_SMARTCARD_STATE_READY
)
1112 if ((pData
== NULL
) || (Size
== 0U))
1117 /* Process Locked */
1118 __HAL_LOCK(hsmartcard
);
1120 hsmartcard
->gState
= HAL_SMARTCARD_STATE_BUSY_TX
;
1122 hsmartcard
->ErrorCode
= HAL_SMARTCARD_ERROR_NONE
;
1123 hsmartcard
->pTxBuffPtr
= pData
;
1124 hsmartcard
->TxXferSize
= Size
;
1125 hsmartcard
->TxXferCount
= Size
;
1127 /* Disable the Peripheral first to update mode for TX master */
1128 CLEAR_BIT(hsmartcard
->Instance
->CR1
, USART_CR1_UE
);
1130 /* Disable Rx, enable Tx */
1131 CLEAR_BIT(hsmartcard
->Instance
->CR1
, USART_CR1_RE
);
1132 SET_BIT(hsmartcard
->Instance
->RQR
, (uint16_t)SMARTCARD_RXDATA_FLUSH_REQUEST
);
1133 SET_BIT(hsmartcard
->Instance
->CR1
, USART_CR1_TE
);
1135 /* Enable the Peripheral */
1136 SET_BIT(hsmartcard
->Instance
->CR1
, USART_CR1_UE
);
1138 /* Set the SMARTCARD DMA transfer complete callback */
1139 hsmartcard
->hdmatx
->XferCpltCallback
= SMARTCARD_DMATransmitCplt
;
1141 /* Set the SMARTCARD error callback */
1142 hsmartcard
->hdmatx
->XferErrorCallback
= SMARTCARD_DMAError
;
1144 /* Set the DMA abort callback */
1145 hsmartcard
->hdmatx
->XferAbortCallback
= NULL
;
1147 /* Enable the SMARTCARD transmit DMA channel */
1148 if (HAL_DMA_Start_IT(hsmartcard
->hdmatx
, (uint32_t)hsmartcard
->pTxBuffPtr
, (uint32_t)&hsmartcard
->Instance
->TDR
,
1151 /* Clear the TC flag in the ICR register */
1152 CLEAR_BIT(hsmartcard
->Instance
->ICR
, USART_ICR_TCCF
);
1154 /* Process Unlocked */
1155 __HAL_UNLOCK(hsmartcard
);
1157 /* Enable the UART Error Interrupt: (Frame error) */
1158 SET_BIT(hsmartcard
->Instance
->CR3
, USART_CR3_EIE
);
1160 /* Enable the DMA transfer for transmit request by setting the DMAT bit
1161 in the SMARTCARD associated USART CR3 register */
1162 SET_BIT(hsmartcard
->Instance
->CR3
, USART_CR3_DMAT
);
1168 /* Set error code to DMA */
1169 hsmartcard
->ErrorCode
= HAL_SMARTCARD_ERROR_DMA
;
1171 /* Process Unlocked */
1172 __HAL_UNLOCK(hsmartcard
);
1174 /* Restore hsmartcard->State to ready */
1175 hsmartcard
->gState
= HAL_SMARTCARD_STATE_READY
;
1187 * @brief Receive an amount of data in DMA mode.
1188 * @param hsmartcard Pointer to a SMARTCARD_HandleTypeDef structure that contains
1189 * the configuration information for the specified SMARTCARD module.
1190 * @param pData pointer to data buffer.
1191 * @param Size amount of data to be received.
1192 * @note The SMARTCARD-associated USART parity is enabled (PCE = 1),
1193 * the received data contain the parity bit (MSB position).
1194 * @retval HAL status
1196 HAL_StatusTypeDef
HAL_SMARTCARD_Receive_DMA(SMARTCARD_HandleTypeDef
*hsmartcard
, uint8_t *pData
, uint16_t Size
)
1198 /* Check that a Rx process is not already ongoing */
1199 if (hsmartcard
->RxState
== HAL_SMARTCARD_STATE_READY
)
1201 if ((pData
== NULL
) || (Size
== 0U))
1206 /* Process Locked */
1207 __HAL_LOCK(hsmartcard
);
1209 hsmartcard
->ErrorCode
= HAL_SMARTCARD_ERROR_NONE
;
1210 hsmartcard
->RxState
= HAL_SMARTCARD_STATE_BUSY_RX
;
1212 hsmartcard
->pRxBuffPtr
= pData
;
1213 hsmartcard
->RxXferSize
= Size
;
1215 /* Set the SMARTCARD DMA transfer complete callback */
1216 hsmartcard
->hdmarx
->XferCpltCallback
= SMARTCARD_DMAReceiveCplt
;
1218 /* Set the SMARTCARD DMA error callback */
1219 hsmartcard
->hdmarx
->XferErrorCallback
= SMARTCARD_DMAError
;
1221 /* Set the DMA abort callback */
1222 hsmartcard
->hdmarx
->XferAbortCallback
= NULL
;
1224 /* Enable the DMA channel */
1225 if (HAL_DMA_Start_IT(hsmartcard
->hdmarx
, (uint32_t)&hsmartcard
->Instance
->RDR
, (uint32_t)hsmartcard
->pRxBuffPtr
,
1228 /* Process Unlocked */
1229 __HAL_UNLOCK(hsmartcard
);
1231 /* Enable the SMARTCARD Parity Error Interrupt */
1232 SET_BIT(hsmartcard
->Instance
->CR1
, USART_CR1_PEIE
);
1234 /* Enable the SMARTCARD Error Interrupt: (Frame error, noise error, overrun error) */
1235 SET_BIT(hsmartcard
->Instance
->CR3
, USART_CR3_EIE
);
1237 /* Enable the DMA transfer for the receiver request by setting the DMAR bit
1238 in the SMARTCARD associated USART CR3 register */
1239 SET_BIT(hsmartcard
->Instance
->CR3
, USART_CR3_DMAR
);
1245 /* Set error code to DMA */
1246 hsmartcard
->ErrorCode
= HAL_SMARTCARD_ERROR_DMA
;
1248 /* Process Unlocked */
1249 __HAL_UNLOCK(hsmartcard
);
1251 /* Restore hsmartcard->State to ready */
1252 hsmartcard
->RxState
= HAL_SMARTCARD_STATE_READY
;
1264 * @brief Abort ongoing transfers (blocking mode).
1265 * @param hsmartcard Pointer to a SMARTCARD_HandleTypeDef structure that contains
1266 * the configuration information for the specified SMARTCARD module.
1267 * @note This procedure could be used for aborting any ongoing transfer started in Interrupt or DMA mode.
1268 * This procedure performs following operations :
1269 * - Disable SMARTCARD Interrupts (Tx and Rx)
1270 * - Disable the DMA transfer in the peripheral register (if enabled)
1271 * - Abort DMA transfer by calling HAL_DMA_Abort (in case of transfer in DMA mode)
1272 * - Set handle State to READY
1273 * @note This procedure is executed in blocking mode : when exiting function, Abort is considered as completed.
1274 * @retval HAL status
1276 HAL_StatusTypeDef
HAL_SMARTCARD_Abort(SMARTCARD_HandleTypeDef
*hsmartcard
)
1278 /* Disable RTOIE, EOBIE, TXEIE, TCIE, RXNE, PE, RXFT, TXFT and ERR (Frame error, noise error, overrun error) interrupts */
1279 CLEAR_BIT(hsmartcard
->Instance
->CR1
,
1280 (USART_CR1_RXNEIE_RXFNEIE
| USART_CR1_PEIE
| USART_CR1_TXEIE_TXFNFIE
| USART_CR1_TCIE
| USART_CR1_RTOIE
|
1282 CLEAR_BIT(hsmartcard
->Instance
->CR3
, (USART_CR3_EIE
| USART_CR3_RXFTIE
| USART_CR3_TXFTIE
));
1284 /* Disable the SMARTCARD DMA Tx request if enabled */
1285 if (HAL_IS_BIT_SET(hsmartcard
->Instance
->CR3
, USART_CR3_DMAT
))
1287 CLEAR_BIT(hsmartcard
->Instance
->CR3
, USART_CR3_DMAT
);
1289 /* Abort the SMARTCARD DMA Tx channel : use blocking DMA Abort API (no callback) */
1290 if (hsmartcard
->hdmatx
!= NULL
)
1292 /* Set the SMARTCARD DMA Abort callback to Null.
1293 No call back execution at end of DMA abort procedure */
1294 hsmartcard
->hdmatx
->XferAbortCallback
= NULL
;
1296 if (HAL_DMA_Abort(hsmartcard
->hdmatx
) != HAL_OK
)
1298 if (HAL_DMA_GetError(hsmartcard
->hdmatx
) == HAL_DMA_ERROR_TIMEOUT
)
1300 /* Set error code to DMA */
1301 hsmartcard
->ErrorCode
= HAL_SMARTCARD_ERROR_DMA
;
1309 /* Disable the SMARTCARD DMA Rx request if enabled */
1310 if (HAL_IS_BIT_SET(hsmartcard
->Instance
->CR3
, USART_CR3_DMAR
))
1312 CLEAR_BIT(hsmartcard
->Instance
->CR3
, USART_CR3_DMAR
);
1314 /* Abort the SMARTCARD DMA Rx channel : use blocking DMA Abort API (no callback) */
1315 if (hsmartcard
->hdmarx
!= NULL
)
1317 /* Set the SMARTCARD DMA Abort callback to Null.
1318 No call back execution at end of DMA abort procedure */
1319 hsmartcard
->hdmarx
->XferAbortCallback
= NULL
;
1321 if (HAL_DMA_Abort(hsmartcard
->hdmarx
) != HAL_OK
)
1323 if (HAL_DMA_GetError(hsmartcard
->hdmarx
) == HAL_DMA_ERROR_TIMEOUT
)
1325 /* Set error code to DMA */
1326 hsmartcard
->ErrorCode
= HAL_SMARTCARD_ERROR_DMA
;
1334 /* Reset Tx and Rx transfer counters */
1335 hsmartcard
->TxXferCount
= 0U;
1336 hsmartcard
->RxXferCount
= 0U;
1338 /* Clear the Error flags in the ICR register */
1339 __HAL_SMARTCARD_CLEAR_FLAG(hsmartcard
,
1340 SMARTCARD_CLEAR_OREF
| SMARTCARD_CLEAR_NEF
| SMARTCARD_CLEAR_PEF
| SMARTCARD_CLEAR_FEF
| SMARTCARD_CLEAR_RTOF
|
1341 SMARTCARD_CLEAR_EOBF
);
1343 /* Restore hsmartcard->gState and hsmartcard->RxState to Ready */
1344 hsmartcard
->gState
= HAL_SMARTCARD_STATE_READY
;
1345 hsmartcard
->RxState
= HAL_SMARTCARD_STATE_READY
;
1347 /* Reset Handle ErrorCode to No Error */
1348 hsmartcard
->ErrorCode
= HAL_SMARTCARD_ERROR_NONE
;
1354 * @brief Abort ongoing Transmit transfer (blocking mode).
1355 * @param hsmartcard Pointer to a SMARTCARD_HandleTypeDef structure that contains
1356 * the configuration information for the specified SMARTCARD module.
1357 * @note This procedure could be used for aborting any ongoing Tx transfer started in Interrupt or DMA mode.
1358 * This procedure performs following operations :
1359 * - Disable SMARTCARD Interrupts (Tx)
1360 * - Disable the DMA transfer in the peripheral register (if enabled)
1361 * - Abort DMA transfer by calling HAL_DMA_Abort (in case of transfer in DMA mode)
1362 * - Set handle State to READY
1363 * @note This procedure is executed in blocking mode : when exiting function, Abort is considered as completed.
1364 * @retval HAL status
1366 HAL_StatusTypeDef
HAL_SMARTCARD_AbortTransmit(SMARTCARD_HandleTypeDef
*hsmartcard
)
1368 /* Disable TCIE, TXEIE and TXFTIE interrupts */
1369 CLEAR_BIT(hsmartcard
->Instance
->CR1
, (USART_CR1_TXEIE_TXFNFIE
| USART_CR1_TCIE
));
1370 CLEAR_BIT(hsmartcard
->Instance
->CR3
, USART_CR3_TXFTIE
);
1372 /* Check if a receive process is ongoing or not. If not disable ERR IT */
1373 if (hsmartcard
->RxState
== HAL_SMARTCARD_STATE_READY
)
1375 /* Disable the SMARTCARD Error Interrupt: (Frame error) */
1376 CLEAR_BIT(hsmartcard
->Instance
->CR3
, USART_CR3_EIE
);
1379 /* Disable the SMARTCARD DMA Tx request if enabled */
1380 if (HAL_IS_BIT_SET(hsmartcard
->Instance
->CR3
, USART_CR3_DMAT
))
1382 CLEAR_BIT(hsmartcard
->Instance
->CR3
, USART_CR3_DMAT
);
1384 /* Abort the SMARTCARD DMA Tx channel : use blocking DMA Abort API (no callback) */
1385 if (hsmartcard
->hdmatx
!= NULL
)
1387 /* Set the SMARTCARD DMA Abort callback to Null.
1388 No call back execution at end of DMA abort procedure */
1389 hsmartcard
->hdmatx
->XferAbortCallback
= NULL
;
1391 if (HAL_DMA_Abort(hsmartcard
->hdmatx
) != HAL_OK
)
1393 if (HAL_DMA_GetError(hsmartcard
->hdmatx
) == HAL_DMA_ERROR_TIMEOUT
)
1395 /* Set error code to DMA */
1396 hsmartcard
->ErrorCode
= HAL_SMARTCARD_ERROR_DMA
;
1404 /* Reset Tx transfer counter */
1405 hsmartcard
->TxXferCount
= 0U;
1407 /* Clear the Error flags in the ICR register */
1408 __HAL_SMARTCARD_CLEAR_FLAG(hsmartcard
, SMARTCARD_CLEAR_FEF
);
1410 /* Restore hsmartcard->gState to Ready */
1411 hsmartcard
->gState
= HAL_SMARTCARD_STATE_READY
;
1417 * @brief Abort ongoing Receive transfer (blocking mode).
1418 * @param hsmartcard Pointer to a SMARTCARD_HandleTypeDef structure that contains
1419 * the configuration information for the specified SMARTCARD module.
1420 * @note This procedure could be used for aborting any ongoing Rx transfer started in Interrupt or DMA mode.
1421 * This procedure performs following operations :
1422 * - Disable SMARTCARD Interrupts (Rx)
1423 * - Disable the DMA transfer in the peripheral register (if enabled)
1424 * - Abort DMA transfer by calling HAL_DMA_Abort (in case of transfer in DMA mode)
1425 * - Set handle State to READY
1426 * @note This procedure is executed in blocking mode : when exiting function, Abort is considered as completed.
1427 * @retval HAL status
1429 HAL_StatusTypeDef
HAL_SMARTCARD_AbortReceive(SMARTCARD_HandleTypeDef
*hsmartcard
)
1431 /* Disable RTOIE, EOBIE, RXNE, PE, RXFT, TXFT and ERR (Frame error, noise error, overrun error) interrupts */
1432 CLEAR_BIT(hsmartcard
->Instance
->CR1
, (USART_CR1_RXNEIE_RXFNEIE
| USART_CR1_PEIE
| USART_CR1_RTOIE
| USART_CR1_EOBIE
));
1433 CLEAR_BIT(hsmartcard
->Instance
->CR3
, (USART_CR3_EIE
| USART_CR3_RXFTIE
));
1435 /* Check if a Transmit process is ongoing or not. If not disable ERR IT */
1436 if (hsmartcard
->gState
== HAL_SMARTCARD_STATE_READY
)
1438 /* Disable the SMARTCARD Error Interrupt: (Frame error) */
1439 CLEAR_BIT(hsmartcard
->Instance
->CR3
, USART_CR3_EIE
);
1442 /* Disable the SMARTCARD DMA Rx request if enabled */
1443 if (HAL_IS_BIT_SET(hsmartcard
->Instance
->CR3
, USART_CR3_DMAR
))
1445 CLEAR_BIT(hsmartcard
->Instance
->CR3
, USART_CR3_DMAR
);
1447 /* Abort the SMARTCARD DMA Rx channel : use blocking DMA Abort API (no callback) */
1448 if (hsmartcard
->hdmarx
!= NULL
)
1450 /* Set the SMARTCARD DMA Abort callback to Null.
1451 No call back execution at end of DMA abort procedure */
1452 hsmartcard
->hdmarx
->XferAbortCallback
= NULL
;
1454 if (HAL_DMA_Abort(hsmartcard
->hdmarx
) != HAL_OK
)
1456 if (HAL_DMA_GetError(hsmartcard
->hdmarx
) == HAL_DMA_ERROR_TIMEOUT
)
1458 /* Set error code to DMA */
1459 hsmartcard
->ErrorCode
= HAL_SMARTCARD_ERROR_DMA
;
1467 /* Reset Rx transfer counter */
1468 hsmartcard
->RxXferCount
= 0U;
1470 /* Clear the Error flags in the ICR register */
1471 __HAL_SMARTCARD_CLEAR_FLAG(hsmartcard
,
1472 SMARTCARD_CLEAR_OREF
| SMARTCARD_CLEAR_NEF
| SMARTCARD_CLEAR_PEF
| SMARTCARD_CLEAR_FEF
| SMARTCARD_CLEAR_RTOF
|
1473 SMARTCARD_CLEAR_EOBF
);
1475 /* Restore hsmartcard->RxState to Ready */
1476 hsmartcard
->RxState
= HAL_SMARTCARD_STATE_READY
;
1482 * @brief Abort ongoing transfers (Interrupt mode).
1483 * @param hsmartcard Pointer to a SMARTCARD_HandleTypeDef structure that contains
1484 * the configuration information for the specified SMARTCARD module.
1485 * @note This procedure could be used for aborting any ongoing transfer started in Interrupt or DMA mode.
1486 * This procedure performs following operations :
1487 * - Disable SMARTCARD Interrupts (Tx and Rx)
1488 * - Disable the DMA transfer in the peripheral register (if enabled)
1489 * - Abort DMA transfer by calling HAL_DMA_Abort_IT (in case of transfer in DMA mode)
1490 * - Set handle State to READY
1491 * - At abort completion, call user abort complete callback
1492 * @note This procedure is executed in Interrupt mode, meaning that abort procedure could be
1493 * considered as completed only when user abort complete callback is executed (not when exiting function).
1494 * @retval HAL status
1496 HAL_StatusTypeDef
HAL_SMARTCARD_Abort_IT(SMARTCARD_HandleTypeDef
*hsmartcard
)
1498 uint32_t abortcplt
= 1U;
1500 /* Disable RTOIE, EOBIE, TXEIE, TCIE, RXNE, PE, RXFT, TXFT and ERR (Frame error, noise error, overrun error) interrupts */
1501 CLEAR_BIT(hsmartcard
->Instance
->CR1
,
1502 (USART_CR1_RXNEIE_RXFNEIE
| USART_CR1_PEIE
| USART_CR1_TXEIE_TXFNFIE
| USART_CR1_TCIE
| USART_CR1_RTOIE
|
1504 CLEAR_BIT(hsmartcard
->Instance
->CR3
, (USART_CR3_EIE
| USART_CR3_RXFTIE
| USART_CR3_TXFTIE
));
1506 /* If DMA Tx and/or DMA Rx Handles are associated to SMARTCARD Handle, DMA Abort complete callbacks should be initialised
1507 before any call to DMA Abort functions */
1508 /* DMA Tx Handle is valid */
1509 if (hsmartcard
->hdmatx
!= NULL
)
1511 /* Set DMA Abort Complete callback if SMARTCARD DMA Tx request if enabled.
1512 Otherwise, set it to NULL */
1513 if (HAL_IS_BIT_SET(hsmartcard
->Instance
->CR3
, USART_CR3_DMAT
))
1515 hsmartcard
->hdmatx
->XferAbortCallback
= SMARTCARD_DMATxAbortCallback
;
1519 hsmartcard
->hdmatx
->XferAbortCallback
= NULL
;
1522 /* DMA Rx Handle is valid */
1523 if (hsmartcard
->hdmarx
!= NULL
)
1525 /* Set DMA Abort Complete callback if SMARTCARD DMA Rx request if enabled.
1526 Otherwise, set it to NULL */
1527 if (HAL_IS_BIT_SET(hsmartcard
->Instance
->CR3
, USART_CR3_DMAR
))
1529 hsmartcard
->hdmarx
->XferAbortCallback
= SMARTCARD_DMARxAbortCallback
;
1533 hsmartcard
->hdmarx
->XferAbortCallback
= NULL
;
1537 /* Disable the SMARTCARD DMA Tx request if enabled */
1538 if (HAL_IS_BIT_SET(hsmartcard
->Instance
->CR3
, USART_CR3_DMAT
))
1540 /* Disable DMA Tx at UART level */
1541 CLEAR_BIT(hsmartcard
->Instance
->CR3
, USART_CR3_DMAT
);
1543 /* Abort the SMARTCARD DMA Tx channel : use non blocking DMA Abort API (callback) */
1544 if (hsmartcard
->hdmatx
!= NULL
)
1546 /* SMARTCARD Tx DMA Abort callback has already been initialised :
1547 will lead to call HAL_SMARTCARD_AbortCpltCallback() at end of DMA abort procedure */
1550 if (HAL_DMA_Abort_IT(hsmartcard
->hdmatx
) != HAL_OK
)
1552 hsmartcard
->hdmatx
->XferAbortCallback
= NULL
;
1561 /* Disable the SMARTCARD DMA Rx request if enabled */
1562 if (HAL_IS_BIT_SET(hsmartcard
->Instance
->CR3
, USART_CR3_DMAR
))
1564 CLEAR_BIT(hsmartcard
->Instance
->CR3
, USART_CR3_DMAR
);
1566 /* Abort the SMARTCARD DMA Rx channel : use non blocking DMA Abort API (callback) */
1567 if (hsmartcard
->hdmarx
!= NULL
)
1569 /* SMARTCARD Rx DMA Abort callback has already been initialised :
1570 will lead to call HAL_SMARTCARD_AbortCpltCallback() at end of DMA abort procedure */
1573 if (HAL_DMA_Abort_IT(hsmartcard
->hdmarx
) != HAL_OK
)
1575 hsmartcard
->hdmarx
->XferAbortCallback
= NULL
;
1585 /* if no DMA abort complete callback execution is required => call user Abort Complete callback */
1586 if (abortcplt
== 1U)
1588 /* Reset Tx and Rx transfer counters */
1589 hsmartcard
->TxXferCount
= 0U;
1590 hsmartcard
->RxXferCount
= 0U;
1592 /* Clear ISR function pointers */
1593 hsmartcard
->RxISR
= NULL
;
1594 hsmartcard
->TxISR
= NULL
;
1596 /* Reset errorCode */
1597 hsmartcard
->ErrorCode
= HAL_SMARTCARD_ERROR_NONE
;
1599 /* Clear the Error flags in the ICR register */
1600 __HAL_SMARTCARD_CLEAR_FLAG(hsmartcard
,
1601 SMARTCARD_CLEAR_OREF
| SMARTCARD_CLEAR_NEF
| SMARTCARD_CLEAR_PEF
| SMARTCARD_CLEAR_FEF
| SMARTCARD_CLEAR_RTOF
|
1602 SMARTCARD_CLEAR_EOBF
);
1604 /* Restore hsmartcard->gState and hsmartcard->RxState to Ready */
1605 hsmartcard
->gState
= HAL_SMARTCARD_STATE_READY
;
1606 hsmartcard
->RxState
= HAL_SMARTCARD_STATE_READY
;
1608 /* As no DMA to be aborted, call directly user Abort complete callback */
1609 #if (USE_HAL_SMARTCARD_REGISTER_CALLBACKS == 1)
1610 /* Call registered Abort complete callback */
1611 hsmartcard
->AbortCpltCallback(hsmartcard
);
1613 /* Call legacy weak Abort complete callback */
1614 HAL_SMARTCARD_AbortCpltCallback(hsmartcard
);
1615 #endif /* USE_HAL_SMARTCARD_REGISTER_CALLBACK */
1622 * @brief Abort ongoing Transmit transfer (Interrupt mode).
1623 * @param hsmartcard Pointer to a SMARTCARD_HandleTypeDef structure that contains
1624 * the configuration information for the specified SMARTCARD module.
1625 * @note This procedure could be used for aborting any ongoing Tx transfer started in Interrupt or DMA mode.
1626 * This procedure performs following operations :
1627 * - Disable SMARTCARD Interrupts (Tx)
1628 * - Disable the DMA transfer in the peripheral register (if enabled)
1629 * - Abort DMA transfer by calling HAL_DMA_Abort_IT (in case of transfer in DMA mode)
1630 * - Set handle State to READY
1631 * - At abort completion, call user abort complete callback
1632 * @note This procedure is executed in Interrupt mode, meaning that abort procedure could be
1633 * considered as completed only when user abort complete callback is executed (not when exiting function).
1634 * @retval HAL status
1636 HAL_StatusTypeDef
HAL_SMARTCARD_AbortTransmit_IT(SMARTCARD_HandleTypeDef
*hsmartcard
)
1638 /* Disable TCIE, TXEIE and TXFTIE interrupts */
1639 CLEAR_BIT(hsmartcard
->Instance
->CR1
, (USART_CR1_TXEIE_TXFNFIE
| USART_CR1_TCIE
));
1640 CLEAR_BIT(hsmartcard
->Instance
->CR3
, USART_CR3_TXFTIE
);
1642 /* Check if a receive process is ongoing or not. If not disable ERR IT */
1643 if (hsmartcard
->RxState
== HAL_SMARTCARD_STATE_READY
)
1645 /* Disable the SMARTCARD Error Interrupt: (Frame error) */
1646 CLEAR_BIT(hsmartcard
->Instance
->CR3
, USART_CR3_EIE
);
1649 /* Disable the SMARTCARD DMA Tx request if enabled */
1650 if (HAL_IS_BIT_SET(hsmartcard
->Instance
->CR3
, USART_CR3_DMAT
))
1652 CLEAR_BIT(hsmartcard
->Instance
->CR3
, USART_CR3_DMAT
);
1654 /* Abort the SMARTCARD DMA Tx channel : use non blocking DMA Abort API (callback) */
1655 if (hsmartcard
->hdmatx
!= NULL
)
1657 /* Set the SMARTCARD DMA Abort callback :
1658 will lead to call HAL_SMARTCARD_AbortCpltCallback() at end of DMA abort procedure */
1659 hsmartcard
->hdmatx
->XferAbortCallback
= SMARTCARD_DMATxOnlyAbortCallback
;
1662 if (HAL_DMA_Abort_IT(hsmartcard
->hdmatx
) != HAL_OK
)
1664 /* Call Directly hsmartcard->hdmatx->XferAbortCallback function in case of error */
1665 hsmartcard
->hdmatx
->XferAbortCallback(hsmartcard
->hdmatx
);
1670 /* Reset Tx transfer counter */
1671 hsmartcard
->TxXferCount
= 0U;
1673 /* Clear TxISR function pointers */
1674 hsmartcard
->TxISR
= NULL
;
1676 /* Restore hsmartcard->gState to Ready */
1677 hsmartcard
->gState
= HAL_SMARTCARD_STATE_READY
;
1679 /* As no DMA to be aborted, call directly user Abort complete callback */
1680 #if (USE_HAL_SMARTCARD_REGISTER_CALLBACKS == 1)
1681 /* Call registered Abort Transmit Complete Callback */
1682 hsmartcard
->AbortTransmitCpltCallback(hsmartcard
);
1684 /* Call legacy weak Abort Transmit Complete Callback */
1685 HAL_SMARTCARD_AbortTransmitCpltCallback(hsmartcard
);
1686 #endif /* USE_HAL_SMARTCARD_REGISTER_CALLBACK */
1691 /* Reset Tx transfer counter */
1692 hsmartcard
->TxXferCount
= 0U;
1694 /* Clear TxISR function pointers */
1695 hsmartcard
->TxISR
= NULL
;
1697 /* Clear the Error flags in the ICR register */
1698 __HAL_SMARTCARD_CLEAR_FLAG(hsmartcard
, SMARTCARD_CLEAR_FEF
);
1700 /* Restore hsmartcard->gState to Ready */
1701 hsmartcard
->gState
= HAL_SMARTCARD_STATE_READY
;
1703 /* As no DMA to be aborted, call directly user Abort complete callback */
1704 #if (USE_HAL_SMARTCARD_REGISTER_CALLBACKS == 1)
1705 /* Call registered Abort Transmit Complete Callback */
1706 hsmartcard
->AbortTransmitCpltCallback(hsmartcard
);
1708 /* Call legacy weak Abort Transmit Complete Callback */
1709 HAL_SMARTCARD_AbortTransmitCpltCallback(hsmartcard
);
1710 #endif /* USE_HAL_SMARTCARD_REGISTER_CALLBACK */
1717 * @brief Abort ongoing Receive transfer (Interrupt mode).
1718 * @param hsmartcard Pointer to a SMARTCARD_HandleTypeDef structure that contains
1719 * the configuration information for the specified SMARTCARD module.
1720 * @note This procedure could be used for aborting any ongoing Rx transfer started in Interrupt or DMA mode.
1721 * This procedure performs following operations :
1722 * - Disable SMARTCARD Interrupts (Rx)
1723 * - Disable the DMA transfer in the peripheral register (if enabled)
1724 * - Abort DMA transfer by calling HAL_DMA_Abort_IT (in case of transfer in DMA mode)
1725 * - Set handle State to READY
1726 * - At abort completion, call user abort complete callback
1727 * @note This procedure is executed in Interrupt mode, meaning that abort procedure could be
1728 * considered as completed only when user abort complete callback is executed (not when exiting function).
1729 * @retval HAL status
1731 HAL_StatusTypeDef
HAL_SMARTCARD_AbortReceive_IT(SMARTCARD_HandleTypeDef
*hsmartcard
)
1733 /* Disable RTOIE, EOBIE, RXNE, PE, RXFT and ERR (Frame error, noise error, overrun error) interrupts */
1734 CLEAR_BIT(hsmartcard
->Instance
->CR1
, (USART_CR1_RXNEIE_RXFNEIE
| USART_CR1_PEIE
| USART_CR1_RTOIE
| USART_CR1_EOBIE
));
1735 CLEAR_BIT(hsmartcard
->Instance
->CR3
, (USART_CR3_EIE
| USART_CR3_RXFTIE
));
1737 /* Check if a Transmit process is ongoing or not. If not disable ERR IT */
1738 if (hsmartcard
->gState
== HAL_SMARTCARD_STATE_READY
)
1740 /* Disable the SMARTCARD Error Interrupt: (Frame error) */
1741 CLEAR_BIT(hsmartcard
->Instance
->CR3
, USART_CR3_EIE
);
1744 /* Disable the SMARTCARD DMA Rx request if enabled */
1745 if (HAL_IS_BIT_SET(hsmartcard
->Instance
->CR3
, USART_CR3_DMAR
))
1747 CLEAR_BIT(hsmartcard
->Instance
->CR3
, USART_CR3_DMAR
);
1749 /* Abort the SMARTCARD DMA Rx channel : use non blocking DMA Abort API (callback) */
1750 if (hsmartcard
->hdmarx
!= NULL
)
1752 /* Set the SMARTCARD DMA Abort callback :
1753 will lead to call HAL_SMARTCARD_AbortCpltCallback() at end of DMA abort procedure */
1754 hsmartcard
->hdmarx
->XferAbortCallback
= SMARTCARD_DMARxOnlyAbortCallback
;
1757 if (HAL_DMA_Abort_IT(hsmartcard
->hdmarx
) != HAL_OK
)
1759 /* Call Directly hsmartcard->hdmarx->XferAbortCallback function in case of error */
1760 hsmartcard
->hdmarx
->XferAbortCallback(hsmartcard
->hdmarx
);
1765 /* Reset Rx transfer counter */
1766 hsmartcard
->RxXferCount
= 0U;
1768 /* Clear RxISR function pointer */
1769 hsmartcard
->RxISR
= NULL
;
1771 /* Clear the Error flags in the ICR register */
1772 __HAL_SMARTCARD_CLEAR_FLAG(hsmartcard
,
1773 SMARTCARD_CLEAR_OREF
| SMARTCARD_CLEAR_NEF
| SMARTCARD_CLEAR_PEF
| SMARTCARD_CLEAR_FEF
| SMARTCARD_CLEAR_RTOF
|
1774 SMARTCARD_CLEAR_EOBF
);
1776 /* Restore hsmartcard->RxState to Ready */
1777 hsmartcard
->RxState
= HAL_SMARTCARD_STATE_READY
;
1779 /* As no DMA to be aborted, call directly user Abort complete callback */
1780 #if (USE_HAL_SMARTCARD_REGISTER_CALLBACKS == 1)
1781 /* Call registered Abort Receive Complete Callback */
1782 hsmartcard
->AbortReceiveCpltCallback(hsmartcard
);
1784 /* Call legacy weak Abort Receive Complete Callback */
1785 HAL_SMARTCARD_AbortReceiveCpltCallback(hsmartcard
);
1786 #endif /* USE_HAL_SMARTCARD_REGISTER_CALLBACK */
1791 /* Reset Rx transfer counter */
1792 hsmartcard
->RxXferCount
= 0U;
1794 /* Clear RxISR function pointer */
1795 hsmartcard
->RxISR
= NULL
;
1797 /* Clear the Error flags in the ICR register */
1798 __HAL_SMARTCARD_CLEAR_FLAG(hsmartcard
,
1799 SMARTCARD_CLEAR_OREF
| SMARTCARD_CLEAR_NEF
| SMARTCARD_CLEAR_PEF
| SMARTCARD_CLEAR_FEF
| SMARTCARD_CLEAR_RTOF
|
1800 SMARTCARD_CLEAR_EOBF
);
1802 /* Restore hsmartcard->RxState to Ready */
1803 hsmartcard
->RxState
= HAL_SMARTCARD_STATE_READY
;
1805 /* As no DMA to be aborted, call directly user Abort complete callback */
1806 #if (USE_HAL_SMARTCARD_REGISTER_CALLBACKS == 1)
1807 /* Call registered Abort Receive Complete Callback */
1808 hsmartcard
->AbortReceiveCpltCallback(hsmartcard
);
1810 /* Call legacy weak Abort Receive Complete Callback */
1811 HAL_SMARTCARD_AbortReceiveCpltCallback(hsmartcard
);
1812 #endif /* USE_HAL_SMARTCARD_REGISTER_CALLBACK */
1819 * @brief Handle SMARTCARD interrupt requests.
1820 * @param hsmartcard Pointer to a SMARTCARD_HandleTypeDef structure that contains
1821 * the configuration information for the specified SMARTCARD module.
1824 void HAL_SMARTCARD_IRQHandler(SMARTCARD_HandleTypeDef
*hsmartcard
)
1826 uint32_t isrflags
= READ_REG(hsmartcard
->Instance
->ISR
);
1827 uint32_t cr1its
= READ_REG(hsmartcard
->Instance
->CR1
);
1828 uint32_t cr3its
= READ_REG(hsmartcard
->Instance
->CR3
);
1829 uint32_t errorflags
;
1832 /* If no error occurs */
1833 errorflags
= (isrflags
& (uint32_t)(USART_ISR_PE
| USART_ISR_FE
| USART_ISR_ORE
| USART_ISR_NE
| USART_ISR_RTOF
));
1834 if (errorflags
== 0U)
1836 /* SMARTCARD in mode Receiver ---------------------------------------------------*/
1837 if (((isrflags
& USART_ISR_RXNE_RXFNE
) != 0U)
1838 && (((cr1its
& USART_CR1_RXNEIE_RXFNEIE
) != 0U)
1839 || ((cr3its
& USART_CR3_RXFTIE
) != 0U)))
1841 if (hsmartcard
->RxISR
!= NULL
)
1843 hsmartcard
->RxISR(hsmartcard
);
1849 /* If some errors occur */
1850 if ((errorflags
!= 0U)
1851 && ((((cr3its
& (USART_CR3_RXFTIE
| USART_CR3_EIE
)) != 0U)
1852 || ((cr1its
& (USART_CR1_RXNEIE_RXFNEIE
| USART_CR1_PEIE
)) != 0U))))
1854 /* SMARTCARD parity error interrupt occurred -------------------------------------*/
1855 if (((isrflags
& USART_ISR_PE
) != 0U) && ((cr1its
& USART_CR1_PEIE
) != 0U))
1857 __HAL_SMARTCARD_CLEAR_IT(hsmartcard
, SMARTCARD_CLEAR_PEF
);
1859 hsmartcard
->ErrorCode
|= HAL_SMARTCARD_ERROR_PE
;
1862 /* SMARTCARD frame error interrupt occurred --------------------------------------*/
1863 if (((isrflags
& USART_ISR_FE
) != 0U) && ((cr3its
& USART_CR3_EIE
) != 0U))
1865 __HAL_SMARTCARD_CLEAR_IT(hsmartcard
, SMARTCARD_CLEAR_FEF
);
1867 hsmartcard
->ErrorCode
|= HAL_SMARTCARD_ERROR_FE
;
1870 /* SMARTCARD noise error interrupt occurred --------------------------------------*/
1871 if (((isrflags
& USART_ISR_NE
) != 0U) && ((cr3its
& USART_CR3_EIE
) != 0U))
1873 __HAL_SMARTCARD_CLEAR_IT(hsmartcard
, SMARTCARD_CLEAR_NEF
);
1875 hsmartcard
->ErrorCode
|= HAL_SMARTCARD_ERROR_NE
;
1878 /* SMARTCARD Over-Run interrupt occurred -----------------------------------------*/
1879 if (((isrflags
& USART_ISR_ORE
) != 0U)
1880 && (((cr1its
& USART_CR1_RXNEIE_RXFNEIE
) != 0U)
1881 || ((cr3its
& USART_CR3_RXFTIE
) != 0U)
1882 || ((cr3its
& USART_CR3_EIE
) != 0U)))
1884 __HAL_SMARTCARD_CLEAR_IT(hsmartcard
, SMARTCARD_CLEAR_OREF
);
1886 hsmartcard
->ErrorCode
|= HAL_SMARTCARD_ERROR_ORE
;
1889 /* SMARTCARD receiver timeout interrupt occurred -----------------------------------------*/
1890 if (((isrflags
& USART_ISR_RTOF
) != 0U) && ((cr1its
& USART_CR1_RTOIE
) != 0U))
1892 __HAL_SMARTCARD_CLEAR_IT(hsmartcard
, SMARTCARD_CLEAR_RTOF
);
1894 hsmartcard
->ErrorCode
|= HAL_SMARTCARD_ERROR_RTO
;
1897 /* Call SMARTCARD Error Call back function if need be --------------------------*/
1898 if (hsmartcard
->ErrorCode
!= HAL_SMARTCARD_ERROR_NONE
)
1900 /* SMARTCARD in mode Receiver ---------------------------------------------------*/
1901 if (((isrflags
& USART_ISR_RXNE_RXFNE
) != 0U)
1902 && (((cr1its
& USART_CR1_RXNEIE_RXFNEIE
) != 0U)
1903 || ((cr3its
& USART_CR3_RXFTIE
) != 0U)))
1905 if (hsmartcard
->RxISR
!= NULL
)
1907 hsmartcard
->RxISR(hsmartcard
);
1911 /* If Error is to be considered as blocking :
1912 - Receiver Timeout error in Reception
1913 - Overrun error in Reception
1914 - any error occurs in DMA mode reception
1916 errorcode
= hsmartcard
->ErrorCode
;
1917 if ((HAL_IS_BIT_SET(hsmartcard
->Instance
->CR3
, USART_CR3_DMAR
))
1918 || ((errorcode
& (HAL_SMARTCARD_ERROR_RTO
| HAL_SMARTCARD_ERROR_ORE
)) != 0U))
1920 /* Blocking error : transfer is aborted
1921 Set the SMARTCARD state ready to be able to start again the process,
1922 Disable Rx Interrupts, and disable Rx DMA request, if ongoing */
1923 SMARTCARD_EndRxTransfer(hsmartcard
);
1925 /* Disable the SMARTCARD DMA Rx request if enabled */
1926 if (HAL_IS_BIT_SET(hsmartcard
->Instance
->CR3
, USART_CR3_DMAR
))
1928 CLEAR_BIT(hsmartcard
->Instance
->CR3
, USART_CR3_DMAR
);
1930 /* Abort the SMARTCARD DMA Rx channel */
1931 if (hsmartcard
->hdmarx
!= NULL
)
1933 /* Set the SMARTCARD DMA Abort callback :
1934 will lead to call HAL_SMARTCARD_ErrorCallback() at end of DMA abort procedure */
1935 hsmartcard
->hdmarx
->XferAbortCallback
= SMARTCARD_DMAAbortOnError
;
1938 if (HAL_DMA_Abort_IT(hsmartcard
->hdmarx
) != HAL_OK
)
1940 /* Call Directly hsmartcard->hdmarx->XferAbortCallback function in case of error */
1941 hsmartcard
->hdmarx
->XferAbortCallback(hsmartcard
->hdmarx
);
1946 #if (USE_HAL_SMARTCARD_REGISTER_CALLBACKS == 1)
1947 /* Call registered user error callback */
1948 hsmartcard
->ErrorCallback(hsmartcard
);
1950 /* Call legacy weak user error callback */
1951 HAL_SMARTCARD_ErrorCallback(hsmartcard
);
1952 #endif /* USE_HAL_SMARTCARD_REGISTER_CALLBACK */
1957 #if (USE_HAL_SMARTCARD_REGISTER_CALLBACKS == 1)
1958 /* Call registered user error callback */
1959 hsmartcard
->ErrorCallback(hsmartcard
);
1961 /* Call legacy weak user error callback */
1962 HAL_SMARTCARD_ErrorCallback(hsmartcard
);
1963 #endif /* USE_HAL_SMARTCARD_REGISTER_CALLBACK */
1966 /* other error type to be considered as blocking :
1967 - Frame error in Transmission
1969 else if ((hsmartcard
->gState
== HAL_SMARTCARD_STATE_BUSY_TX
)
1970 && ((errorcode
& HAL_SMARTCARD_ERROR_FE
) != 0U))
1972 /* Blocking error : transfer is aborted
1973 Set the SMARTCARD state ready to be able to start again the process,
1974 Disable Tx Interrupts, and disable Tx DMA request, if ongoing */
1975 SMARTCARD_EndTxTransfer(hsmartcard
);
1977 /* Disable the SMARTCARD DMA Tx request if enabled */
1978 if (HAL_IS_BIT_SET(hsmartcard
->Instance
->CR3
, USART_CR3_DMAT
))
1980 CLEAR_BIT(hsmartcard
->Instance
->CR3
, USART_CR3_DMAT
);
1982 /* Abort the SMARTCARD DMA Tx channel */
1983 if (hsmartcard
->hdmatx
!= NULL
)
1985 /* Set the SMARTCARD DMA Abort callback :
1986 will lead to call HAL_SMARTCARD_ErrorCallback() at end of DMA abort procedure */
1987 hsmartcard
->hdmatx
->XferAbortCallback
= SMARTCARD_DMAAbortOnError
;
1990 if (HAL_DMA_Abort_IT(hsmartcard
->hdmatx
) != HAL_OK
)
1992 /* Call Directly hsmartcard->hdmatx->XferAbortCallback function in case of error */
1993 hsmartcard
->hdmatx
->XferAbortCallback(hsmartcard
->hdmatx
);
1998 #if (USE_HAL_SMARTCARD_REGISTER_CALLBACKS == 1)
1999 /* Call registered user error callback */
2000 hsmartcard
->ErrorCallback(hsmartcard
);
2002 /* Call legacy weak user error callback */
2003 HAL_SMARTCARD_ErrorCallback(hsmartcard
);
2004 #endif /* USE_HAL_SMARTCARD_REGISTER_CALLBACK */
2009 #if (USE_HAL_SMARTCARD_REGISTER_CALLBACKS == 1)
2010 /* Call registered user error callback */
2011 hsmartcard
->ErrorCallback(hsmartcard
);
2013 /* Call legacy weak user error callback */
2014 HAL_SMARTCARD_ErrorCallback(hsmartcard
);
2015 #endif /* USE_HAL_SMARTCARD_REGISTER_CALLBACK */
2020 /* Non Blocking error : transfer could go on.
2021 Error is notified to user through user error callback */
2022 #if (USE_HAL_SMARTCARD_REGISTER_CALLBACKS == 1)
2023 /* Call registered user error callback */
2024 hsmartcard
->ErrorCallback(hsmartcard
);
2026 /* Call legacy weak user error callback */
2027 HAL_SMARTCARD_ErrorCallback(hsmartcard
);
2028 #endif /* USE_HAL_SMARTCARD_REGISTER_CALLBACK */
2029 hsmartcard
->ErrorCode
= HAL_SMARTCARD_ERROR_NONE
;
2034 } /* End if some error occurs */
2036 /* SMARTCARD in mode Receiver, end of block interruption ------------------------*/
2037 if (((isrflags
& USART_ISR_EOBF
) != 0U) && ((cr1its
& USART_CR1_EOBIE
) != 0U))
2039 hsmartcard
->RxState
= HAL_SMARTCARD_STATE_READY
;
2040 __HAL_UNLOCK(hsmartcard
);
2041 #if (USE_HAL_SMARTCARD_REGISTER_CALLBACKS == 1)
2042 /* Call registered Rx complete callback */
2043 hsmartcard
->RxCpltCallback(hsmartcard
);
2045 /* Call legacy weak Rx complete callback */
2046 HAL_SMARTCARD_RxCpltCallback(hsmartcard
);
2047 #endif /* USE_HAL_SMARTCARD_REGISTER_CALLBACK */
2048 /* Clear EOBF interrupt after HAL_SMARTCARD_RxCpltCallback() call for the End of Block information
2049 to be available during HAL_SMARTCARD_RxCpltCallback() processing */
2050 __HAL_SMARTCARD_CLEAR_IT(hsmartcard
, SMARTCARD_CLEAR_EOBF
);
2054 /* SMARTCARD in mode Transmitter ------------------------------------------------*/
2055 if (((isrflags
& USART_ISR_TXE_TXFNF
) != 0U)
2056 && (((cr1its
& USART_CR1_TXEIE_TXFNFIE
) != 0U)
2057 || ((cr3its
& USART_CR3_TXFTIE
) != 0U)))
2059 if (hsmartcard
->TxISR
!= NULL
)
2061 hsmartcard
->TxISR(hsmartcard
);
2066 /* SMARTCARD in mode Transmitter (transmission end) ------------------------*/
2067 if (__HAL_SMARTCARD_GET_IT(hsmartcard
, hsmartcard
->AdvancedInit
.TxCompletionIndication
) != RESET
)
2069 if (__HAL_SMARTCARD_GET_IT_SOURCE(hsmartcard
, hsmartcard
->AdvancedInit
.TxCompletionIndication
) != RESET
)
2071 SMARTCARD_EndTransmit_IT(hsmartcard
);
2076 /* SMARTCARD TX Fifo Empty occurred ----------------------------------------------*/
2077 if (((isrflags
& USART_ISR_TXFE
) != 0U) && ((cr1its
& USART_CR1_TXFEIE
) != 0U))
2079 #if (USE_HAL_SMARTCARD_REGISTER_CALLBACKS == 1)
2080 /* Call registered Tx Fifo Empty Callback */
2081 hsmartcard
->TxFifoEmptyCallback(hsmartcard
);
2083 /* Call legacy weak Tx Fifo Empty Callback */
2084 HAL_SMARTCARDEx_TxFifoEmptyCallback(hsmartcard
);
2085 #endif /* USE_HAL_SMARTCARD_REGISTER_CALLBACK */
2089 /* SMARTCARD RX Fifo Full occurred ----------------------------------------------*/
2090 if (((isrflags
& USART_ISR_RXFF
) != 0U) && ((cr1its
& USART_CR1_RXFFIE
) != 0U))
2092 #if (USE_HAL_SMARTCARD_REGISTER_CALLBACKS == 1)
2093 /* Call registered Rx Fifo Full Callback */
2094 hsmartcard
->RxFifoFullCallback(hsmartcard
);
2096 /* Call legacy weak Rx Fifo Full Callback */
2097 HAL_SMARTCARDEx_RxFifoFullCallback(hsmartcard
);
2098 #endif /* USE_HAL_SMARTCARD_REGISTER_CALLBACK */
2104 * @brief Tx Transfer completed callback.
2105 * @param hsmartcard Pointer to a SMARTCARD_HandleTypeDef structure that contains
2106 * the configuration information for the specified SMARTCARD module.
2109 __weak
void HAL_SMARTCARD_TxCpltCallback(SMARTCARD_HandleTypeDef
*hsmartcard
)
2111 /* Prevent unused argument(s) compilation warning */
2114 /* NOTE : This function should not be modified, when the callback is needed,
2115 the HAL_SMARTCARD_TxCpltCallback can be implemented in the user file.
2120 * @brief Rx Transfer completed callback.
2121 * @param hsmartcard Pointer to a SMARTCARD_HandleTypeDef structure that contains
2122 * the configuration information for the specified SMARTCARD module.
2125 __weak
void HAL_SMARTCARD_RxCpltCallback(SMARTCARD_HandleTypeDef
*hsmartcard
)
2127 /* Prevent unused argument(s) compilation warning */
2130 /* NOTE : This function should not be modified, when the callback is needed,
2131 the HAL_SMARTCARD_RxCpltCallback can be implemented in the user file.
2136 * @brief SMARTCARD error callback.
2137 * @param hsmartcard Pointer to a SMARTCARD_HandleTypeDef structure that contains
2138 * the configuration information for the specified SMARTCARD module.
2141 __weak
void HAL_SMARTCARD_ErrorCallback(SMARTCARD_HandleTypeDef
*hsmartcard
)
2143 /* Prevent unused argument(s) compilation warning */
2146 /* NOTE : This function should not be modified, when the callback is needed,
2147 the HAL_SMARTCARD_ErrorCallback can be implemented in the user file.
2152 * @brief SMARTCARD Abort Complete callback.
2153 * @param hsmartcard Pointer to a SMARTCARD_HandleTypeDef structure that contains
2154 * the configuration information for the specified SMARTCARD module.
2157 __weak
void HAL_SMARTCARD_AbortCpltCallback(SMARTCARD_HandleTypeDef
*hsmartcard
)
2159 /* Prevent unused argument(s) compilation warning */
2162 /* NOTE : This function should not be modified, when the callback is needed,
2163 the HAL_SMARTCARD_AbortCpltCallback can be implemented in the user file.
2168 * @brief SMARTCARD Abort Complete callback.
2169 * @param hsmartcard Pointer to a SMARTCARD_HandleTypeDef structure that contains
2170 * the configuration information for the specified SMARTCARD module.
2173 __weak
void HAL_SMARTCARD_AbortTransmitCpltCallback(SMARTCARD_HandleTypeDef
*hsmartcard
)
2175 /* Prevent unused argument(s) compilation warning */
2178 /* NOTE : This function should not be modified, when the callback is needed,
2179 the HAL_SMARTCARD_AbortTransmitCpltCallback can be implemented in the user file.
2184 * @brief SMARTCARD Abort Receive Complete callback.
2185 * @param hsmartcard Pointer to a SMARTCARD_HandleTypeDef structure that contains
2186 * the configuration information for the specified SMARTCARD module.
2189 __weak
void HAL_SMARTCARD_AbortReceiveCpltCallback(SMARTCARD_HandleTypeDef
*hsmartcard
)
2191 /* Prevent unused argument(s) compilation warning */
2194 /* NOTE : This function should not be modified, when the callback is needed,
2195 the HAL_SMARTCARD_AbortReceiveCpltCallback can be implemented in the user file.
2203 /** @defgroup SMARTCARD_Exported_Functions_Group4 Peripheral State and Errors functions
2204 * @brief SMARTCARD State and Errors functions
2207 ==============================================================================
2208 ##### Peripheral State and Errors functions #####
2209 ==============================================================================
2211 This subsection provides a set of functions allowing to return the State of SmartCard
2212 handle and also return Peripheral Errors occurred during communication process
2213 (+) HAL_SMARTCARD_GetState() API can be helpful to check in run-time the state
2214 of the SMARTCARD peripheral.
2215 (+) HAL_SMARTCARD_GetError() checks in run-time errors that could occur during
2223 * @brief Return the SMARTCARD handle state.
2224 * @param hsmartcard Pointer to a SMARTCARD_HandleTypeDef structure that contains
2225 * the configuration information for the specified SMARTCARD module.
2226 * @retval SMARTCARD handle state
2228 HAL_SMARTCARD_StateTypeDef
HAL_SMARTCARD_GetState(SMARTCARD_HandleTypeDef
*hsmartcard
)
2230 /* Return SMARTCARD handle state */
2233 temp1
= (uint32_t)hsmartcard
->gState
;
2234 temp2
= (uint32_t)hsmartcard
->RxState
;
2236 return (HAL_SMARTCARD_StateTypeDef
)(temp1
| temp2
);
2240 * @brief Return the SMARTCARD handle error code.
2241 * @param hsmartcard Pointer to a SMARTCARD_HandleTypeDef structure that contains
2242 * the configuration information for the specified SMARTCARD module.
2243 * @retval SMARTCARD handle Error Code
2245 uint32_t HAL_SMARTCARD_GetError(SMARTCARD_HandleTypeDef
*hsmartcard
)
2247 return hsmartcard
->ErrorCode
;
2258 /** @defgroup SMARTCARD_Private_Functions SMARTCARD Private Functions
2262 #if (USE_HAL_SMARTCARD_REGISTER_CALLBACKS == 1)
2264 * @brief Initialize the callbacks to their default values.
2265 * @param hsmartcard SMARTCARD handle.
2268 void SMARTCARD_InitCallbacksToDefault(SMARTCARD_HandleTypeDef
*hsmartcard
)
2270 /* Init the SMARTCARD Callback settings */
2271 hsmartcard
->TxCpltCallback
= HAL_SMARTCARD_TxCpltCallback
; /* Legacy weak TxCpltCallback */
2272 hsmartcard
->RxCpltCallback
= HAL_SMARTCARD_RxCpltCallback
; /* Legacy weak RxCpltCallback */
2273 hsmartcard
->ErrorCallback
= HAL_SMARTCARD_ErrorCallback
; /* Legacy weak ErrorCallback */
2274 hsmartcard
->AbortCpltCallback
= HAL_SMARTCARD_AbortCpltCallback
; /* Legacy weak AbortCpltCallback */
2275 hsmartcard
->AbortTransmitCpltCallback
= HAL_SMARTCARD_AbortTransmitCpltCallback
; /* Legacy weak AbortTransmitCpltCallback */
2276 hsmartcard
->AbortReceiveCpltCallback
= HAL_SMARTCARD_AbortReceiveCpltCallback
; /* Legacy weak AbortReceiveCpltCallback */
2277 hsmartcard
->RxFifoFullCallback
= HAL_SMARTCARDEx_RxFifoFullCallback
; /* Legacy weak RxFifoFullCallback */
2278 hsmartcard
->TxFifoEmptyCallback
= HAL_SMARTCARDEx_TxFifoEmptyCallback
; /* Legacy weak TxFifoEmptyCallback */
2281 #endif /* USE_HAL_SMARTCARD_REGISTER_CALLBACKS */
2284 * @brief Configure the SMARTCARD associated USART peripheral.
2285 * @param hsmartcard Pointer to a SMARTCARD_HandleTypeDef structure that contains
2286 * the configuration information for the specified SMARTCARD module.
2287 * @retval HAL status
2289 static HAL_StatusTypeDef
SMARTCARD_SetConfig(SMARTCARD_HandleTypeDef
*hsmartcard
)
2292 SMARTCARD_ClockSourceTypeDef clocksource
;
2293 HAL_StatusTypeDef ret
= HAL_OK
;
2294 const uint16_t SMARTCARDPrescTable
[12] = {1U, 2U, 4U, 6U, 8U, 10U, 12U, 16U, 32U, 64U, 128U, 256U};
2296 /* Check the parameters */
2297 assert_param(IS_SMARTCARD_INSTANCE(hsmartcard
->Instance
));
2298 assert_param(IS_SMARTCARD_BAUDRATE(hsmartcard
->Init
.BaudRate
));
2299 assert_param(IS_SMARTCARD_WORD_LENGTH(hsmartcard
->Init
.WordLength
));
2300 assert_param(IS_SMARTCARD_STOPBITS(hsmartcard
->Init
.StopBits
));
2301 assert_param(IS_SMARTCARD_PARITY(hsmartcard
->Init
.Parity
));
2302 assert_param(IS_SMARTCARD_MODE(hsmartcard
->Init
.Mode
));
2303 assert_param(IS_SMARTCARD_POLARITY(hsmartcard
->Init
.CLKPolarity
));
2304 assert_param(IS_SMARTCARD_PHASE(hsmartcard
->Init
.CLKPhase
));
2305 assert_param(IS_SMARTCARD_LASTBIT(hsmartcard
->Init
.CLKLastBit
));
2306 assert_param(IS_SMARTCARD_ONE_BIT_SAMPLE(hsmartcard
->Init
.OneBitSampling
));
2307 assert_param(IS_SMARTCARD_NACK(hsmartcard
->Init
.NACKEnable
));
2308 assert_param(IS_SMARTCARD_TIMEOUT(hsmartcard
->Init
.TimeOutEnable
));
2309 assert_param(IS_SMARTCARD_AUTORETRY_COUNT(hsmartcard
->Init
.AutoRetryCount
));
2310 assert_param(IS_SMARTCARD_CLOCKPRESCALER(hsmartcard
->Init
.ClockPrescaler
));
2312 /*-------------------------- USART CR1 Configuration -----------------------*/
2313 /* In SmartCard mode, M and PCE are forced to 1 (8 bits + parity).
2314 * Oversampling is forced to 16 (OVER8 = 0).
2315 * Configure the Parity and Mode:
2316 * set PS bit according to hsmartcard->Init.Parity value
2317 * set TE and RE bits according to hsmartcard->Init.Mode value */
2318 tmpreg
= (uint32_t) hsmartcard
->Init
.Parity
| hsmartcard
->Init
.Mode
;
2319 tmpreg
|= (uint32_t) hsmartcard
->Init
.WordLength
| hsmartcard
->FifoMode
;
2320 MODIFY_REG(hsmartcard
->Instance
->CR1
, USART_CR1_FIELDS
, tmpreg
);
2322 /*-------------------------- USART CR2 Configuration -----------------------*/
2323 tmpreg
= hsmartcard
->Init
.StopBits
;
2324 /* Synchronous mode is activated by default */
2325 tmpreg
|= (uint32_t) USART_CR2_CLKEN
| hsmartcard
->Init
.CLKPolarity
;
2326 tmpreg
|= (uint32_t) hsmartcard
->Init
.CLKPhase
| hsmartcard
->Init
.CLKLastBit
;
2327 tmpreg
|= (uint32_t) hsmartcard
->Init
.TimeOutEnable
;
2328 MODIFY_REG(hsmartcard
->Instance
->CR2
, USART_CR2_FIELDS
, tmpreg
);
2330 /*-------------------------- USART CR3 Configuration -----------------------*/
2332 * - one-bit sampling method versus three samples' majority rule
2333 * according to hsmartcard->Init.OneBitSampling
2334 * - NACK transmission in case of parity error according
2335 * to hsmartcard->Init.NACKEnable
2336 * - autoretry counter according to hsmartcard->Init.AutoRetryCount */
2338 tmpreg
= (uint32_t) hsmartcard
->Init
.OneBitSampling
| hsmartcard
->Init
.NACKEnable
;
2339 tmpreg
|= ((uint32_t)hsmartcard
->Init
.AutoRetryCount
<< USART_CR3_SCARCNT_Pos
);
2340 MODIFY_REG(hsmartcard
->Instance
->CR3
, USART_CR3_FIELDS
, tmpreg
);
2342 /*--------------------- SMARTCARD clock PRESC Configuration ----------------*/
2344 * - SMARTCARD Clock Prescaler: set PRESCALER according to hsmartcard->Init.ClockPrescaler value */
2345 MODIFY_REG(hsmartcard
->Instance
->PRESC
, USART_PRESC_PRESCALER
, hsmartcard
->Init
.ClockPrescaler
);
2347 /*-------------------------- USART GTPR Configuration ----------------------*/
2348 tmpreg
= (hsmartcard
->Init
.Prescaler
| ((uint32_t)hsmartcard
->Init
.GuardTime
<< USART_GTPR_GT_Pos
));
2349 MODIFY_REG(hsmartcard
->Instance
->GTPR
, (uint16_t)(USART_GTPR_GT
| USART_GTPR_PSC
), (uint16_t)tmpreg
);
2351 /*-------------------------- USART RTOR Configuration ----------------------*/
2352 tmpreg
= ((uint32_t)hsmartcard
->Init
.BlockLength
<< USART_RTOR_BLEN_Pos
);
2353 if (hsmartcard
->Init
.TimeOutEnable
== SMARTCARD_TIMEOUT_ENABLE
)
2355 assert_param(IS_SMARTCARD_TIMEOUT_VALUE(hsmartcard
->Init
.TimeOutValue
));
2356 tmpreg
|= (uint32_t) hsmartcard
->Init
.TimeOutValue
;
2358 MODIFY_REG(hsmartcard
->Instance
->RTOR
, (USART_RTOR_RTO
| USART_RTOR_BLEN
), tmpreg
);
2360 /*-------------------------- USART BRR Configuration -----------------------*/
2361 SMARTCARD_GETCLOCKSOURCE(hsmartcard
, clocksource
);
2363 switch (clocksource
)
2365 case SMARTCARD_CLOCKSOURCE_PCLK1
:
2366 tmpreg
= (uint16_t)(((HAL_RCC_GetPCLK1Freq() / SMARTCARDPrescTable
[hsmartcard
->Init
.ClockPrescaler
]) + (hsmartcard
->Init
.BaudRate
/ 2U)) / hsmartcard
->Init
.BaudRate
);
2368 case SMARTCARD_CLOCKSOURCE_PCLK2
:
2369 tmpreg
= (uint16_t)(((HAL_RCC_GetPCLK2Freq() / SMARTCARDPrescTable
[hsmartcard
->Init
.ClockPrescaler
]) + (hsmartcard
->Init
.BaudRate
/ 2U)) / hsmartcard
->Init
.BaudRate
);
2371 case SMARTCARD_CLOCKSOURCE_HSI
:
2372 tmpreg
= (uint16_t)(((HSI_VALUE
/ SMARTCARDPrescTable
[hsmartcard
->Init
.ClockPrescaler
]) + (hsmartcard
->Init
.BaudRate
/ 2U)) / hsmartcard
->Init
.BaudRate
);
2374 case SMARTCARD_CLOCKSOURCE_SYSCLK
:
2375 tmpreg
= (uint16_t)(((HAL_RCC_GetSysClockFreq() / SMARTCARDPrescTable
[hsmartcard
->Init
.ClockPrescaler
]) + (hsmartcard
->Init
.BaudRate
/ 2U)) / hsmartcard
->Init
.BaudRate
);
2377 case SMARTCARD_CLOCKSOURCE_LSE
:
2378 tmpreg
= (uint16_t)(((uint16_t)(LSE_VALUE
/ SMARTCARDPrescTable
[hsmartcard
->Init
.ClockPrescaler
]) + (hsmartcard
->Init
.BaudRate
/ 2U)) / hsmartcard
->Init
.BaudRate
);
2385 /* USARTDIV must be greater than or equal to 0d16 */
2386 if ((tmpreg
>= USART_BRR_MIN
) && (tmpreg
<= USART_BRR_MAX
))
2388 hsmartcard
->Instance
->BRR
= tmpreg
;
2395 /* Initialize the number of data to process during RX/TX ISR execution */
2396 hsmartcard
->NbTxDataToProcess
= 1U;
2397 hsmartcard
->NbRxDataToProcess
= 1U;
2399 /* Clear ISR function pointers */
2400 hsmartcard
->RxISR
= NULL
;
2401 hsmartcard
->TxISR
= NULL
;
2408 * @brief Configure the SMARTCARD associated USART peripheral advanced features.
2409 * @param hsmartcard Pointer to a SMARTCARD_HandleTypeDef structure that contains
2410 * the configuration information for the specified SMARTCARD module.
2413 static void SMARTCARD_AdvFeatureConfig(SMARTCARD_HandleTypeDef
*hsmartcard
)
2415 /* Check whether the set of advanced features to configure is properly set */
2416 assert_param(IS_SMARTCARD_ADVFEATURE_INIT(hsmartcard
->AdvancedInit
.AdvFeatureInit
));
2418 /* if required, configure TX pin active level inversion */
2419 if (HAL_IS_BIT_SET(hsmartcard
->AdvancedInit
.AdvFeatureInit
, SMARTCARD_ADVFEATURE_TXINVERT_INIT
))
2421 assert_param(IS_SMARTCARD_ADVFEATURE_TXINV(hsmartcard
->AdvancedInit
.TxPinLevelInvert
));
2422 MODIFY_REG(hsmartcard
->Instance
->CR2
, USART_CR2_TXINV
, hsmartcard
->AdvancedInit
.TxPinLevelInvert
);
2425 /* if required, configure RX pin active level inversion */
2426 if (HAL_IS_BIT_SET(hsmartcard
->AdvancedInit
.AdvFeatureInit
, SMARTCARD_ADVFEATURE_RXINVERT_INIT
))
2428 assert_param(IS_SMARTCARD_ADVFEATURE_RXINV(hsmartcard
->AdvancedInit
.RxPinLevelInvert
));
2429 MODIFY_REG(hsmartcard
->Instance
->CR2
, USART_CR2_RXINV
, hsmartcard
->AdvancedInit
.RxPinLevelInvert
);
2432 /* if required, configure data inversion */
2433 if (HAL_IS_BIT_SET(hsmartcard
->AdvancedInit
.AdvFeatureInit
, SMARTCARD_ADVFEATURE_DATAINVERT_INIT
))
2435 assert_param(IS_SMARTCARD_ADVFEATURE_DATAINV(hsmartcard
->AdvancedInit
.DataInvert
));
2436 MODIFY_REG(hsmartcard
->Instance
->CR2
, USART_CR2_DATAINV
, hsmartcard
->AdvancedInit
.DataInvert
);
2439 /* if required, configure RX/TX pins swap */
2440 if (HAL_IS_BIT_SET(hsmartcard
->AdvancedInit
.AdvFeatureInit
, SMARTCARD_ADVFEATURE_SWAP_INIT
))
2442 assert_param(IS_SMARTCARD_ADVFEATURE_SWAP(hsmartcard
->AdvancedInit
.Swap
));
2443 MODIFY_REG(hsmartcard
->Instance
->CR2
, USART_CR2_SWAP
, hsmartcard
->AdvancedInit
.Swap
);
2446 /* if required, configure RX overrun detection disabling */
2447 if (HAL_IS_BIT_SET(hsmartcard
->AdvancedInit
.AdvFeatureInit
, SMARTCARD_ADVFEATURE_RXOVERRUNDISABLE_INIT
))
2449 assert_param(IS_SMARTCARD_OVERRUN(hsmartcard
->AdvancedInit
.OverrunDisable
));
2450 MODIFY_REG(hsmartcard
->Instance
->CR3
, USART_CR3_OVRDIS
, hsmartcard
->AdvancedInit
.OverrunDisable
);
2453 /* if required, configure DMA disabling on reception error */
2454 if (HAL_IS_BIT_SET(hsmartcard
->AdvancedInit
.AdvFeatureInit
, SMARTCARD_ADVFEATURE_DMADISABLEONERROR_INIT
))
2456 assert_param(IS_SMARTCARD_ADVFEATURE_DMAONRXERROR(hsmartcard
->AdvancedInit
.DMADisableonRxError
));
2457 MODIFY_REG(hsmartcard
->Instance
->CR3
, USART_CR3_DDRE
, hsmartcard
->AdvancedInit
.DMADisableonRxError
);
2460 /* if required, configure MSB first on communication line */
2461 if (HAL_IS_BIT_SET(hsmartcard
->AdvancedInit
.AdvFeatureInit
, SMARTCARD_ADVFEATURE_MSBFIRST_INIT
))
2463 assert_param(IS_SMARTCARD_ADVFEATURE_MSBFIRST(hsmartcard
->AdvancedInit
.MSBFirst
));
2464 MODIFY_REG(hsmartcard
->Instance
->CR2
, USART_CR2_MSBFIRST
, hsmartcard
->AdvancedInit
.MSBFirst
);
2470 * @brief Check the SMARTCARD Idle State.
2471 * @param hsmartcard Pointer to a SMARTCARD_HandleTypeDef structure that contains
2472 * the configuration information for the specified SMARTCARD module.
2473 * @retval HAL status
2475 static HAL_StatusTypeDef
SMARTCARD_CheckIdleState(SMARTCARD_HandleTypeDef
*hsmartcard
)
2479 /* Initialize the SMARTCARD ErrorCode */
2480 hsmartcard
->ErrorCode
= HAL_SMARTCARD_ERROR_NONE
;
2482 /* Init tickstart for timeout management */
2483 tickstart
= HAL_GetTick();
2485 /* Check if the Transmitter is enabled */
2486 if ((hsmartcard
->Instance
->CR1
& USART_CR1_TE
) == USART_CR1_TE
)
2488 /* Wait until TEACK flag is set */
2489 if (SMARTCARD_WaitOnFlagUntilTimeout(hsmartcard
, USART_ISR_TEACK
, RESET
, tickstart
,
2490 SMARTCARD_TEACK_REACK_TIMEOUT
) != HAL_OK
)
2492 /* Timeout occurred */
2496 /* Check if the Receiver is enabled */
2497 if ((hsmartcard
->Instance
->CR1
& USART_CR1_RE
) == USART_CR1_RE
)
2499 /* Wait until REACK flag is set */
2500 if (SMARTCARD_WaitOnFlagUntilTimeout(hsmartcard
, USART_ISR_REACK
, RESET
, tickstart
,
2501 SMARTCARD_TEACK_REACK_TIMEOUT
) != HAL_OK
)
2503 /* Timeout occurred */
2508 /* Initialize the SMARTCARD states */
2509 hsmartcard
->gState
= HAL_SMARTCARD_STATE_READY
;
2510 hsmartcard
->RxState
= HAL_SMARTCARD_STATE_READY
;
2512 /* Process Unlocked */
2513 __HAL_UNLOCK(hsmartcard
);
2519 * @brief Handle SMARTCARD Communication Timeout.
2520 * @param hsmartcard Pointer to a SMARTCARD_HandleTypeDef structure that contains
2521 * the configuration information for the specified SMARTCARD module.
2522 * @param Flag Specifies the SMARTCARD flag to check.
2523 * @param Status The new Flag status (SET or RESET).
2524 * @param Tickstart Tick start value
2525 * @param Timeout Timeout duration.
2526 * @retval HAL status
2528 static HAL_StatusTypeDef
SMARTCARD_WaitOnFlagUntilTimeout(SMARTCARD_HandleTypeDef
*hsmartcard
, uint32_t Flag
,
2529 FlagStatus Status
, uint32_t Tickstart
, uint32_t Timeout
)
2531 /* Wait until flag is set */
2532 while ((__HAL_SMARTCARD_GET_FLAG(hsmartcard
, Flag
) ? SET
: RESET
) == Status
)
2534 /* Check for the Timeout */
2535 if (Timeout
!= HAL_MAX_DELAY
)
2537 if (((HAL_GetTick() - Tickstart
) > Timeout
) || (Timeout
== 0U))
2539 /* Disable TXE, RXNE, PE and ERR (Frame error, noise error, overrun error) interrupts for the interrupt process */
2540 CLEAR_BIT(hsmartcard
->Instance
->CR1
, (USART_CR1_RXNEIE_RXFNEIE
| USART_CR1_PEIE
| USART_CR1_TXEIE_TXFNFIE
));
2541 CLEAR_BIT(hsmartcard
->Instance
->CR3
, USART_CR3_EIE
);
2543 hsmartcard
->gState
= HAL_SMARTCARD_STATE_READY
;
2544 hsmartcard
->RxState
= HAL_SMARTCARD_STATE_READY
;
2546 /* Process Unlocked */
2547 __HAL_UNLOCK(hsmartcard
);
2557 * @brief End ongoing Tx transfer on SMARTCARD peripheral (following error detection or Transmit completion).
2558 * @param hsmartcard Pointer to a SMARTCARD_HandleTypeDef structure that contains
2559 * the configuration information for the specified SMARTCARD module.
2562 static void SMARTCARD_EndTxTransfer(SMARTCARD_HandleTypeDef
*hsmartcard
)
2564 /* Disable TXEIE, TCIE and ERR (Frame error, noise error, overrun error) interrupts */
2565 CLEAR_BIT(hsmartcard
->Instance
->CR1
, (USART_CR1_TXEIE_TXFNFIE
| USART_CR1_TCIE
));
2566 CLEAR_BIT(hsmartcard
->Instance
->CR3
, USART_CR3_EIE
);
2568 /* At end of Tx process, restore hsmartcard->gState to Ready */
2569 hsmartcard
->gState
= HAL_SMARTCARD_STATE_READY
;
2574 * @brief End ongoing Rx transfer on UART peripheral (following error detection or Reception completion).
2575 * @param hsmartcard Pointer to a SMARTCARD_HandleTypeDef structure that contains
2576 * the configuration information for the specified SMARTCARD module.
2579 static void SMARTCARD_EndRxTransfer(SMARTCARD_HandleTypeDef
*hsmartcard
)
2581 /* Disable RXNE, PE and ERR (Frame error, noise error, overrun error) interrupts */
2582 CLEAR_BIT(hsmartcard
->Instance
->CR1
, (USART_CR1_RXNEIE_RXFNEIE
| USART_CR1_PEIE
));
2583 CLEAR_BIT(hsmartcard
->Instance
->CR3
, USART_CR3_EIE
);
2585 /* At end of Rx process, restore hsmartcard->RxState to Ready */
2586 hsmartcard
->RxState
= HAL_SMARTCARD_STATE_READY
;
2591 * @brief DMA SMARTCARD transmit process complete callback.
2592 * @param hdma Pointer to a DMA_HandleTypeDef structure that contains
2593 * the configuration information for the specified DMA module.
2596 static void SMARTCARD_DMATransmitCplt(DMA_HandleTypeDef
*hdma
)
2598 SMARTCARD_HandleTypeDef
*hsmartcard
= (SMARTCARD_HandleTypeDef
*)(hdma
->Parent
);
2599 hsmartcard
->TxXferCount
= 0U;
2601 /* Disable the DMA transfer for transmit request by resetting the DMAT bit
2602 in the SMARTCARD associated USART CR3 register */
2603 CLEAR_BIT(hsmartcard
->Instance
->CR3
, USART_CR3_DMAT
);
2605 /* Enable the SMARTCARD Transmit Complete Interrupt */
2606 __HAL_SMARTCARD_ENABLE_IT(hsmartcard
, hsmartcard
->AdvancedInit
.TxCompletionIndication
);
2610 * @brief DMA SMARTCARD receive process complete callback.
2611 * @param hdma Pointer to a DMA_HandleTypeDef structure that contains
2612 * the configuration information for the specified DMA module.
2615 static void SMARTCARD_DMAReceiveCplt(DMA_HandleTypeDef
*hdma
)
2617 SMARTCARD_HandleTypeDef
*hsmartcard
= (SMARTCARD_HandleTypeDef
*)(hdma
->Parent
);
2618 hsmartcard
->RxXferCount
= 0U;
2620 /* Disable PE and ERR (Frame error, noise error, overrun error) interrupts */
2621 CLEAR_BIT(hsmartcard
->Instance
->CR1
, USART_CR1_PEIE
);
2622 CLEAR_BIT(hsmartcard
->Instance
->CR3
, USART_CR3_EIE
);
2624 /* Disable the DMA transfer for the receiver request by resetting the DMAR bit
2625 in the SMARTCARD associated USART CR3 register */
2626 CLEAR_BIT(hsmartcard
->Instance
->CR3
, USART_CR3_DMAR
);
2628 /* At end of Rx process, restore hsmartcard->RxState to Ready */
2629 hsmartcard
->RxState
= HAL_SMARTCARD_STATE_READY
;
2631 #if (USE_HAL_SMARTCARD_REGISTER_CALLBACKS == 1)
2632 /* Call registered Rx complete callback */
2633 hsmartcard
->RxCpltCallback(hsmartcard
);
2635 /* Call legacy weak Rx complete callback */
2636 HAL_SMARTCARD_RxCpltCallback(hsmartcard
);
2637 #endif /* USE_HAL_SMARTCARD_REGISTER_CALLBACK */
2641 * @brief DMA SMARTCARD communication error callback.
2642 * @param hdma Pointer to a DMA_HandleTypeDef structure that contains
2643 * the configuration information for the specified DMA module.
2646 static void SMARTCARD_DMAError(DMA_HandleTypeDef
*hdma
)
2648 SMARTCARD_HandleTypeDef
*hsmartcard
= (SMARTCARD_HandleTypeDef
*)(hdma
->Parent
);
2650 /* Stop SMARTCARD DMA Tx request if ongoing */
2651 if (hsmartcard
->gState
== HAL_SMARTCARD_STATE_BUSY_TX
)
2653 if (HAL_IS_BIT_SET(hsmartcard
->Instance
->CR3
, USART_CR3_DMAT
))
2655 hsmartcard
->TxXferCount
= 0U;
2656 SMARTCARD_EndTxTransfer(hsmartcard
);
2660 /* Stop SMARTCARD DMA Rx request if ongoing */
2661 if (hsmartcard
->RxState
== HAL_SMARTCARD_STATE_BUSY_RX
)
2663 if (HAL_IS_BIT_SET(hsmartcard
->Instance
->CR3
, USART_CR3_DMAR
))
2665 hsmartcard
->RxXferCount
= 0U;
2666 SMARTCARD_EndRxTransfer(hsmartcard
);
2670 hsmartcard
->ErrorCode
|= HAL_SMARTCARD_ERROR_DMA
;
2671 #if (USE_HAL_SMARTCARD_REGISTER_CALLBACKS == 1)
2672 /* Call registered user error callback */
2673 hsmartcard
->ErrorCallback(hsmartcard
);
2675 /* Call legacy weak user error callback */
2676 HAL_SMARTCARD_ErrorCallback(hsmartcard
);
2677 #endif /* USE_HAL_SMARTCARD_REGISTER_CALLBACK */
2681 * @brief DMA SMARTCARD communication abort callback, when initiated by HAL services on Error
2682 * (To be called at end of DMA Abort procedure following error occurrence).
2683 * @param hdma DMA handle.
2686 static void SMARTCARD_DMAAbortOnError(DMA_HandleTypeDef
*hdma
)
2688 SMARTCARD_HandleTypeDef
*hsmartcard
= (SMARTCARD_HandleTypeDef
*)(hdma
->Parent
);
2689 hsmartcard
->RxXferCount
= 0U;
2690 hsmartcard
->TxXferCount
= 0U;
2692 #if (USE_HAL_SMARTCARD_REGISTER_CALLBACKS == 1)
2693 /* Call registered user error callback */
2694 hsmartcard
->ErrorCallback(hsmartcard
);
2696 /* Call legacy weak user error callback */
2697 HAL_SMARTCARD_ErrorCallback(hsmartcard
);
2698 #endif /* USE_HAL_SMARTCARD_REGISTER_CALLBACK */
2702 * @brief DMA SMARTCARD Tx communication abort callback, when initiated by user
2703 * (To be called at end of DMA Tx Abort procedure following user abort request).
2704 * @note When this callback is executed, User Abort complete call back is called only if no
2705 * Abort still ongoing for Rx DMA Handle.
2706 * @param hdma DMA handle.
2709 static void SMARTCARD_DMATxAbortCallback(DMA_HandleTypeDef
*hdma
)
2711 SMARTCARD_HandleTypeDef
*hsmartcard
= (SMARTCARD_HandleTypeDef
*)(hdma
->Parent
);
2713 hsmartcard
->hdmatx
->XferAbortCallback
= NULL
;
2715 /* Check if an Abort process is still ongoing */
2716 if (hsmartcard
->hdmarx
!= NULL
)
2718 if (hsmartcard
->hdmarx
->XferAbortCallback
!= NULL
)
2724 /* No Abort process still ongoing : All DMA channels are aborted, call user Abort Complete callback */
2725 hsmartcard
->TxXferCount
= 0U;
2726 hsmartcard
->RxXferCount
= 0U;
2728 /* Reset errorCode */
2729 hsmartcard
->ErrorCode
= HAL_SMARTCARD_ERROR_NONE
;
2731 /* Clear the Error flags in the ICR register */
2732 __HAL_SMARTCARD_CLEAR_FLAG(hsmartcard
,
2733 SMARTCARD_CLEAR_OREF
| SMARTCARD_CLEAR_NEF
| SMARTCARD_CLEAR_PEF
| SMARTCARD_CLEAR_FEF
| SMARTCARD_CLEAR_RTOF
|
2734 SMARTCARD_CLEAR_EOBF
);
2736 /* Restore hsmartcard->gState and hsmartcard->RxState to Ready */
2737 hsmartcard
->gState
= HAL_SMARTCARD_STATE_READY
;
2738 hsmartcard
->RxState
= HAL_SMARTCARD_STATE_READY
;
2740 #if (USE_HAL_SMARTCARD_REGISTER_CALLBACKS == 1)
2741 /* Call registered Abort complete callback */
2742 hsmartcard
->AbortCpltCallback(hsmartcard
);
2744 /* Call legacy weak Abort complete callback */
2745 HAL_SMARTCARD_AbortCpltCallback(hsmartcard
);
2746 #endif /* USE_HAL_SMARTCARD_REGISTER_CALLBACK */
2751 * @brief DMA SMARTCARD Rx communication abort callback, when initiated by user
2752 * (To be called at end of DMA Rx Abort procedure following user abort request).
2753 * @note When this callback is executed, User Abort complete call back is called only if no
2754 * Abort still ongoing for Tx DMA Handle.
2755 * @param hdma DMA handle.
2758 static void SMARTCARD_DMARxAbortCallback(DMA_HandleTypeDef
*hdma
)
2760 SMARTCARD_HandleTypeDef
*hsmartcard
= (SMARTCARD_HandleTypeDef
*)(hdma
->Parent
);
2762 hsmartcard
->hdmarx
->XferAbortCallback
= NULL
;
2764 /* Check if an Abort process is still ongoing */
2765 if (hsmartcard
->hdmatx
!= NULL
)
2767 if (hsmartcard
->hdmatx
->XferAbortCallback
!= NULL
)
2773 /* No Abort process still ongoing : All DMA channels are aborted, call user Abort Complete callback */
2774 hsmartcard
->TxXferCount
= 0U;
2775 hsmartcard
->RxXferCount
= 0U;
2777 /* Reset errorCode */
2778 hsmartcard
->ErrorCode
= HAL_SMARTCARD_ERROR_NONE
;
2780 /* Clear the Error flags in the ICR register */
2781 __HAL_SMARTCARD_CLEAR_FLAG(hsmartcard
,
2782 SMARTCARD_CLEAR_OREF
| SMARTCARD_CLEAR_NEF
| SMARTCARD_CLEAR_PEF
| SMARTCARD_CLEAR_FEF
| SMARTCARD_CLEAR_RTOF
|
2783 SMARTCARD_CLEAR_EOBF
);
2785 /* Restore hsmartcard->gState and hsmartcard->RxState to Ready */
2786 hsmartcard
->gState
= HAL_SMARTCARD_STATE_READY
;
2787 hsmartcard
->RxState
= HAL_SMARTCARD_STATE_READY
;
2789 #if (USE_HAL_SMARTCARD_REGISTER_CALLBACKS == 1)
2790 /* Call registered Abort complete callback */
2791 hsmartcard
->AbortCpltCallback(hsmartcard
);
2793 /* Call legacy weak Abort complete callback */
2794 HAL_SMARTCARD_AbortCpltCallback(hsmartcard
);
2795 #endif /* USE_HAL_SMARTCARD_REGISTER_CALLBACK */
2800 * @brief DMA SMARTCARD Tx communication abort callback, when initiated by user by a call to
2801 * HAL_SMARTCARD_AbortTransmit_IT API (Abort only Tx transfer)
2802 * (This callback is executed at end of DMA Tx Abort procedure following user abort request,
2803 * and leads to user Tx Abort Complete callback execution).
2804 * @param hdma DMA handle.
2807 static void SMARTCARD_DMATxOnlyAbortCallback(DMA_HandleTypeDef
*hdma
)
2809 SMARTCARD_HandleTypeDef
*hsmartcard
= (SMARTCARD_HandleTypeDef
*)(hdma
->Parent
);
2811 hsmartcard
->TxXferCount
= 0U;
2813 /* Clear the Error flags in the ICR register */
2814 __HAL_SMARTCARD_CLEAR_FLAG(hsmartcard
, SMARTCARD_CLEAR_FEF
);
2816 /* Restore hsmartcard->gState to Ready */
2817 hsmartcard
->gState
= HAL_SMARTCARD_STATE_READY
;
2819 #if (USE_HAL_SMARTCARD_REGISTER_CALLBACKS == 1)
2820 /* Call registered Abort Transmit Complete Callback */
2821 hsmartcard
->AbortTransmitCpltCallback(hsmartcard
);
2823 /* Call legacy weak Abort Transmit Complete Callback */
2824 HAL_SMARTCARD_AbortTransmitCpltCallback(hsmartcard
);
2825 #endif /* USE_HAL_SMARTCARD_REGISTER_CALLBACK */
2829 * @brief DMA SMARTCARD Rx communication abort callback, when initiated by user by a call to
2830 * HAL_SMARTCARD_AbortReceive_IT API (Abort only Rx transfer)
2831 * (This callback is executed at end of DMA Rx Abort procedure following user abort request,
2832 * and leads to user Rx Abort Complete callback execution).
2833 * @param hdma DMA handle.
2836 static void SMARTCARD_DMARxOnlyAbortCallback(DMA_HandleTypeDef
*hdma
)
2838 SMARTCARD_HandleTypeDef
*hsmartcard
= (SMARTCARD_HandleTypeDef
*)(hdma
->Parent
);
2840 hsmartcard
->RxXferCount
= 0U;
2842 /* Clear the Error flags in the ICR register */
2843 __HAL_SMARTCARD_CLEAR_FLAG(hsmartcard
,
2844 SMARTCARD_CLEAR_OREF
| SMARTCARD_CLEAR_NEF
| SMARTCARD_CLEAR_PEF
| SMARTCARD_CLEAR_FEF
| SMARTCARD_CLEAR_RTOF
|
2845 SMARTCARD_CLEAR_EOBF
);
2847 /* Restore hsmartcard->RxState to Ready */
2848 hsmartcard
->RxState
= HAL_SMARTCARD_STATE_READY
;
2850 #if (USE_HAL_SMARTCARD_REGISTER_CALLBACKS == 1)
2851 /* Call registered Abort Receive Complete Callback */
2852 hsmartcard
->AbortReceiveCpltCallback(hsmartcard
);
2854 /* Call legacy weak Abort Receive Complete Callback */
2855 HAL_SMARTCARD_AbortReceiveCpltCallback(hsmartcard
);
2856 #endif /* USE_HAL_SMARTCARD_REGISTER_CALLBACK */
2860 * @brief Send an amount of data in non-blocking mode.
2861 * @note Function called under interruption only, once
2862 * interruptions have been enabled by HAL_SMARTCARD_Transmit_IT()
2863 * and when the FIFO mode is disabled.
2864 * @param hsmartcard Pointer to a SMARTCARD_HandleTypeDef structure that contains
2865 * the configuration information for the specified SMARTCARD module.
2868 static void SMARTCARD_TxISR(SMARTCARD_HandleTypeDef
*hsmartcard
)
2870 /* Check that a Tx process is ongoing */
2871 if (hsmartcard
->gState
== HAL_SMARTCARD_STATE_BUSY_TX
)
2873 if (hsmartcard
->TxXferCount
== 0U)
2875 /* Disable the SMARTCARD Transmit Data Register Empty Interrupt */
2876 CLEAR_BIT(hsmartcard
->Instance
->CR1
, USART_CR1_TXEIE_TXFNFIE
);
2878 /* Enable the SMARTCARD Transmit Complete Interrupt */
2879 __HAL_SMARTCARD_ENABLE_IT(hsmartcard
, hsmartcard
->AdvancedInit
.TxCompletionIndication
);
2883 hsmartcard
->Instance
->TDR
= (uint8_t)(*hsmartcard
->pTxBuffPtr
& 0xFFU
);
2884 hsmartcard
->pTxBuffPtr
++;
2885 hsmartcard
->TxXferCount
--;
2891 * @brief Send an amount of data in non-blocking mode.
2892 * @note Function called under interruption only, once
2893 * interruptions have been enabled by HAL_SMARTCARD_Transmit_IT()
2894 * and when the FIFO mode is enabled.
2895 * @param hsmartcard Pointer to a SMARTCARD_HandleTypeDef structure that contains
2896 * the configuration information for the specified SMARTCARD module.
2899 static void SMARTCARD_TxISR_FIFOEN(SMARTCARD_HandleTypeDef
*hsmartcard
)
2901 uint16_t nb_tx_data
;
2903 /* Check that a Tx process is ongoing */
2904 if (hsmartcard
->gState
== HAL_SMARTCARD_STATE_BUSY_TX
)
2906 for (nb_tx_data
= hsmartcard
->NbTxDataToProcess
; nb_tx_data
> 0U ; nb_tx_data
--)
2908 if (hsmartcard
->TxXferCount
== 0U)
2910 /* Disable the SMARTCARD Transmit Data Register Empty Interrupt */
2911 CLEAR_BIT(hsmartcard
->Instance
->CR1
, USART_CR1_TXEIE_TXFNFIE
);
2913 /* Enable the SMARTCARD Transmit Complete Interrupt */
2914 __HAL_SMARTCARD_ENABLE_IT(hsmartcard
, hsmartcard
->AdvancedInit
.TxCompletionIndication
);
2916 else if (READ_BIT(hsmartcard
->Instance
->ISR
, USART_ISR_TXE_TXFNF
) != 0U)
2918 hsmartcard
->Instance
->TDR
= (uint8_t)(*hsmartcard
->pTxBuffPtr
& 0xFFU
);
2919 hsmartcard
->pTxBuffPtr
++;
2920 hsmartcard
->TxXferCount
--;
2931 * @brief Wrap up transmission in non-blocking mode.
2932 * @param hsmartcard Pointer to a SMARTCARD_HandleTypeDef structure that contains
2933 * the configuration information for the specified SMARTCARD module.
2936 static void SMARTCARD_EndTransmit_IT(SMARTCARD_HandleTypeDef
*hsmartcard
)
2938 /* Disable the SMARTCARD Transmit Complete Interrupt */
2939 __HAL_SMARTCARD_DISABLE_IT(hsmartcard
, hsmartcard
->AdvancedInit
.TxCompletionIndication
);
2941 /* Check if a receive process is ongoing or not. If not disable ERR IT */
2942 if (hsmartcard
->RxState
== HAL_SMARTCARD_STATE_READY
)
2944 /* Disable the SMARTCARD Error Interrupt: (Frame error) */
2945 CLEAR_BIT(hsmartcard
->Instance
->CR3
, USART_CR3_EIE
);
2948 /* Re-enable Rx at end of transmission if initial mode is Rx/Tx */
2949 if (hsmartcard
->Init
.Mode
== SMARTCARD_MODE_TX_RX
)
2951 /* Disable the Peripheral first to update modes */
2952 CLEAR_BIT(hsmartcard
->Instance
->CR1
, USART_CR1_UE
);
2953 SET_BIT(hsmartcard
->Instance
->CR1
, USART_CR1_RE
);
2954 /* Enable the Peripheral */
2955 SET_BIT(hsmartcard
->Instance
->CR1
, USART_CR1_UE
);
2958 /* Tx process is ended, restore hsmartcard->gState to Ready */
2959 hsmartcard
->gState
= HAL_SMARTCARD_STATE_READY
;
2961 /* Clear TxISR function pointer */
2962 hsmartcard
->TxISR
= NULL
;
2964 #if (USE_HAL_SMARTCARD_REGISTER_CALLBACKS == 1)
2965 /* Call registered Tx complete callback */
2966 hsmartcard
->TxCpltCallback(hsmartcard
);
2968 /* Call legacy weak Tx complete callback */
2969 HAL_SMARTCARD_TxCpltCallback(hsmartcard
);
2970 #endif /* USE_HAL_SMARTCARD_REGISTER_CALLBACK */
2974 * @brief Receive an amount of data in non-blocking mode.
2975 * @note Function called under interruption only, once
2976 * interruptions have been enabled by HAL_SMARTCARD_Receive_IT()
2977 * and when the FIFO mode is disabled.
2978 * @param hsmartcard Pointer to a SMARTCARD_HandleTypeDef structure that contains
2979 * the configuration information for the specified SMARTCARD module.
2982 static void SMARTCARD_RxISR(SMARTCARD_HandleTypeDef
*hsmartcard
)
2984 /* Check that a Rx process is ongoing */
2985 if (hsmartcard
->RxState
== HAL_SMARTCARD_STATE_BUSY_RX
)
2987 *hsmartcard
->pRxBuffPtr
= (uint8_t)(hsmartcard
->Instance
->RDR
& (uint8_t)0xFF);
2988 hsmartcard
->pRxBuffPtr
++;
2990 hsmartcard
->RxXferCount
--;
2991 if (hsmartcard
->RxXferCount
== 0U)
2993 CLEAR_BIT(hsmartcard
->Instance
->CR1
, USART_CR1_RXNEIE_RXFNEIE
);
2995 /* Check if a transmit process is ongoing or not. If not disable ERR IT */
2996 if (hsmartcard
->gState
== HAL_SMARTCARD_STATE_READY
)
2998 /* Disable the SMARTCARD Error Interrupt: (Frame error, noise error, overrun error) */
2999 CLEAR_BIT(hsmartcard
->Instance
->CR3
, USART_CR3_EIE
);
3002 /* Disable the SMARTCARD Parity Error Interrupt */
3003 CLEAR_BIT(hsmartcard
->Instance
->CR1
, USART_CR1_PEIE
);
3005 hsmartcard
->RxState
= HAL_SMARTCARD_STATE_READY
;
3007 /* Clear RxISR function pointer */
3008 hsmartcard
->RxISR
= NULL
;
3010 #if (USE_HAL_SMARTCARD_REGISTER_CALLBACKS == 1)
3011 /* Call registered Rx complete callback */
3012 hsmartcard
->RxCpltCallback(hsmartcard
);
3014 /* Call legacy weak Rx complete callback */
3015 HAL_SMARTCARD_RxCpltCallback(hsmartcard
);
3016 #endif /* USE_HAL_SMARTCARD_REGISTER_CALLBACK */
3021 /* Clear RXNE interrupt flag */
3022 __HAL_SMARTCARD_SEND_REQ(hsmartcard
, SMARTCARD_RXDATA_FLUSH_REQUEST
);
3027 * @brief Receive an amount of data in non-blocking mode.
3028 * @note Function called under interruption only, once
3029 * interruptions have been enabled by HAL_SMARTCARD_Receive_IT()
3030 * and when the FIFO mode is enabled.
3031 * @param hsmartcard Pointer to a SMARTCARD_HandleTypeDef structure that contains
3032 * the configuration information for the specified SMARTCARD module.
3035 static void SMARTCARD_RxISR_FIFOEN(SMARTCARD_HandleTypeDef
*hsmartcard
)
3037 uint16_t nb_rx_data
;
3038 uint16_t rxdatacount
;
3040 /* Check that a Rx process is ongoing */
3041 if (hsmartcard
->RxState
== HAL_SMARTCARD_STATE_BUSY_RX
)
3043 for (nb_rx_data
= hsmartcard
->NbRxDataToProcess
; nb_rx_data
> 0U ; nb_rx_data
--)
3045 *hsmartcard
->pRxBuffPtr
= (uint8_t)(hsmartcard
->Instance
->RDR
& (uint8_t)0xFF);
3046 hsmartcard
->pRxBuffPtr
++;
3048 hsmartcard
->RxXferCount
--;
3049 if (hsmartcard
->RxXferCount
== 0U)
3051 CLEAR_BIT(hsmartcard
->Instance
->CR1
, USART_CR1_RXNEIE_RXFNEIE
);
3053 /* Check if a transmit process is ongoing or not. If not disable ERR IT */
3054 if (hsmartcard
->gState
== HAL_SMARTCARD_STATE_READY
)
3056 /* Disable the SMARTCARD Error Interrupt: (Frame error, noise error, overrun error) */
3057 CLEAR_BIT(hsmartcard
->Instance
->CR3
, USART_CR3_EIE
);
3060 /* Disable the SMARTCARD Parity Error Interrupt */
3061 CLEAR_BIT(hsmartcard
->Instance
->CR1
, USART_CR1_PEIE
);
3063 hsmartcard
->RxState
= HAL_SMARTCARD_STATE_READY
;
3065 /* Clear RxISR function pointer */
3066 hsmartcard
->RxISR
= NULL
;
3068 #if (USE_HAL_SMARTCARD_REGISTER_CALLBACKS == 1)
3069 /* Call registered Rx complete callback */
3070 hsmartcard
->RxCpltCallback(hsmartcard
);
3072 /* Call legacy weak Rx complete callback */
3073 HAL_SMARTCARD_RxCpltCallback(hsmartcard
);
3074 #endif /* USE_HAL_SMARTCARD_REGISTER_CALLBACK */
3078 /* When remaining number of bytes to receive is less than the RX FIFO
3079 threshold, next incoming frames are processed as if FIFO mode was
3080 disabled (i.e. one interrupt per received frame).
3082 rxdatacount
= hsmartcard
->RxXferCount
;
3083 if (((rxdatacount
!= 0U)) && (rxdatacount
< hsmartcard
->NbRxDataToProcess
))
3085 /* Disable the UART RXFT interrupt*/
3086 CLEAR_BIT(hsmartcard
->Instance
->CR3
, USART_CR3_RXFTIE
);
3088 /* Update the RxISR function pointer */
3089 hsmartcard
->RxISR
= SMARTCARD_RxISR
;
3091 /* Enable the UART Data Register Not Empty interrupt */
3092 SET_BIT(hsmartcard
->Instance
->CR1
, USART_CR1_RXNEIE_RXFNEIE
);
3097 /* Clear RXNE interrupt flag */
3098 __HAL_SMARTCARD_SEND_REQ(hsmartcard
, SMARTCARD_RXDATA_FLUSH_REQUEST
);
3106 #endif /* HAL_SMARTCARD_MODULE_ENABLED */
3115 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/