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