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