source: S-port/trunk/Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_smartcard.c

Last change on this file was 1, checked in by AlexLir, 3 years ago
File size: 85.1 KB
Line 
1/**
2 ******************************************************************************
3 * @file stm32f4xx_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.
21 (#) Initialize the SMARTCARD low level resources by implementing the HAL_SMARTCARD_MspInit() API:
22 (##) Enable the interface clock of the USARTx associated to the SMARTCARD.
23 (##) SMARTCARD pins configuration:
24 (+++) Enable the clock for the SMARTCARD GPIOs.
25 (+++) Configure SMARTCARD pins as alternate function pull-up.
26 (##) NVIC configuration if you need to use interrupt process (HAL_SMARTCARD_Transmit_IT()
27 and HAL_SMARTCARD_Receive_IT() APIs):
28 (+++) Configure the USARTx interrupt priority.
29 (+++) Enable the NVIC USART IRQ handle.
30 (##) DMA Configuration if you need to use DMA process (HAL_SMARTCARD_Transmit_DMA()
31 and HAL_SMARTCARD_Receive_DMA() APIs):
32 (+++) Declare a DMA handle structure for the Tx/Rx stream.
33 (+++) Enable the DMAx interface clock.
34 (+++) Configure the declared DMA handle structure with the required Tx/Rx parameters.
35 (+++) Configure the DMA Tx/Rx stream.
36 (+++) Associate the initialized DMA handle to the SMARTCARD DMA Tx/Rx handle.
37 (+++) Configure the priority and enable the NVIC for the transfer complete interrupt on the DMA Tx/Rx stream.
38 (+++) Configure the USARTx interrupt priority and enable the NVIC USART IRQ handle
39 (used for last byte sending completion detection in DMA non circular mode)
40
41 (#) Program the Baud Rate, Word Length , Stop Bit, Parity, Hardware
42 flow control and Mode(Receiver/Transmitter) in the SMARTCARD Init structure.
43
44 (#) Initialize the SMARTCARD registers by calling the HAL_SMARTCARD_Init() API:
45 (++) These APIs configure also the low level Hardware GPIO, CLOCK, CORTEX...etc)
46 by calling the customized HAL_SMARTCARD_MspInit() API.
47 [..]
48 (@) The specific SMARTCARD interrupts (Transmission complete interrupt,
49 RXNE interrupt and Error Interrupts) will be managed using the macros
50 __HAL_SMARTCARD_ENABLE_IT() and __HAL_SMARTCARD_DISABLE_IT() inside the transmit and receive process.
51
52 [..]
53 Three operation modes are available within this driver :
54
55 *** Polling mode IO operation ***
56 =================================
57 [..]
58 (+) Send an amount of data in blocking mode using HAL_SMARTCARD_Transmit()
59 (+) Receive an amount of data in blocking mode using HAL_SMARTCARD_Receive()
60
61 *** Interrupt mode IO operation ***
62 ===================================
63 [..]
64 (+) Send an amount of data in non blocking mode using HAL_SMARTCARD_Transmit_IT()
65 (+) At transmission end of transfer HAL_SMARTCARD_TxCpltCallback is executed and user can
66 add his own code by customization of function pointer HAL_SMARTCARD_TxCpltCallback
67 (+) Receive an amount of data in non blocking mode using HAL_SMARTCARD_Receive_IT()
68 (+) At reception end of transfer HAL_SMARTCARD_RxCpltCallback is executed and user can
69 add his own code by customization of function pointer HAL_SMARTCARD_RxCpltCallback
70 (+) In case of transfer Error, HAL_SMARTCARD_ErrorCallback() function is executed and user can
71 add his own code by customization of function pointer HAL_SMARTCARD_ErrorCallback
72
73 *** DMA mode IO operation ***
74 ==============================
75 [..]
76 (+) Send an amount of data in non blocking mode (DMA) using HAL_SMARTCARD_Transmit_DMA()
77 (+) At transmission end of transfer HAL_SMARTCARD_TxCpltCallback is executed and user can
78 add his own code by customization of function pointer HAL_SMARTCARD_TxCpltCallback
79 (+) Receive an amount of data in non blocking mode (DMA) using HAL_SMARTCARD_Receive_DMA()
80 (+) At reception end of transfer HAL_SMARTCARD_RxCpltCallback is executed and user can
81 add his own code by customization of function pointer HAL_SMARTCARD_RxCpltCallback
82 (+) In case of transfer Error, HAL_SMARTCARD_ErrorCallback() function is executed and user can
83 add his own code by customization of function pointer HAL_SMARTCARD_ErrorCallback
84
85 *** SMARTCARD HAL driver macros list ***
86 ========================================
87 [..]
88 Below the list of most used macros in SMARTCARD HAL driver.
89
90 (+) __HAL_SMARTCARD_ENABLE: Enable the SMARTCARD peripheral
91 (+) __HAL_SMARTCARD_DISABLE: Disable the SMARTCARD peripheral
92 (+) __HAL_SMARTCARD_GET_FLAG : Check whether the specified SMARTCARD flag is set or not
93 (+) __HAL_SMARTCARD_CLEAR_FLAG : Clear the specified SMARTCARD pending flag
94 (+) __HAL_SMARTCARD_ENABLE_IT: Enable the specified SMARTCARD interrupt
95 (+) __HAL_SMARTCARD_DISABLE_IT: Disable the specified SMARTCARD interrupt
96
97 [..]
98 (@) You can refer to the SMARTCARD HAL driver header file for more useful macros
99
100 ##### Callback registration #####
101 ==================================
102
103 [..]
104 The compilation define USE_HAL_SMARTCARD_REGISTER_CALLBACKS when set to 1
105 allows the user to configure dynamically the driver callbacks.
106
107 [..]
108 Use Function @ref HAL_SMARTCARD_RegisterCallback() to register a user callback.
109 Function @ref HAL_SMARTCARD_RegisterCallback() allows to register following callbacks:
110 (+) TxCpltCallback : Tx Complete Callback.
111 (+) RxCpltCallback : Rx Complete Callback.
112 (+) ErrorCallback : Error Callback.
113 (+) AbortCpltCallback : Abort Complete Callback.
114 (+) AbortTransmitCpltCallback : Abort Transmit Complete Callback.
115 (+) AbortReceiveCpltCallback : Abort Receive Complete Callback.
116 (+) MspInitCallback : SMARTCARD MspInit.
117 (+) MspDeInitCallback : SMARTCARD MspDeInit.
118 This function takes as parameters the HAL peripheral handle, the Callback ID
119 and a pointer to the user callback function.
120
121 [..]
122 Use function @ref HAL_SMARTCARD_UnRegisterCallback() to reset a callback to the default
123 weak (surcharged) function.
124 @ref HAL_SMARTCARD_UnRegisterCallback() takes as parameters the HAL peripheral handle,
125 and the Callback ID.
126 This function allows to reset following callbacks:
127 (+) TxCpltCallback : Tx Complete Callback.
128 (+) RxCpltCallback : Rx Complete Callback.
129 (+) ErrorCallback : Error Callback.
130 (+) AbortCpltCallback : Abort Complete Callback.
131 (+) AbortTransmitCpltCallback : Abort Transmit Complete Callback.
132 (+) AbortReceiveCpltCallback : Abort Receive Complete Callback.
133 (+) MspInitCallback : SMARTCARD MspInit.
134 (+) MspDeInitCallback : SMARTCARD MspDeInit.
135
136 [..]
137 By default, after the @ref HAL_SMARTCARD_Init() and when the state is HAL_SMARTCARD_STATE_RESET
138 all callbacks are set to the corresponding weak (surcharged) functions:
139 examples @ref HAL_SMARTCARD_TxCpltCallback(), @ref HAL_SMARTCARD_RxCpltCallback().
140 Exception done for MspInit and MspDeInit functions that are respectively
141 reset to the legacy weak (surcharged) functions in the @ref HAL_SMARTCARD_Init()
142 and @ref HAL_SMARTCARD_DeInit() only when these callbacks are null (not registered beforehand).
143 If not, MspInit or MspDeInit are not null, the @ref HAL_SMARTCARD_Init() and @ref HAL_SMARTCARD_DeInit()
144 keep and use the user MspInit/MspDeInit callbacks (registered beforehand).
145
146 [..]
147 Callbacks can be registered/unregistered in HAL_SMARTCARD_STATE_READY state only.
148 Exception done MspInit/MspDeInit that can be registered/unregistered
149 in HAL_SMARTCARD_STATE_READY or HAL_SMARTCARD_STATE_RESET state, thus registered (user)
150 MspInit/DeInit callbacks can be used during the Init/DeInit.
151 In that case first register the MspInit/MspDeInit user callbacks
152 using @ref HAL_SMARTCARD_RegisterCallback() before calling @ref HAL_SMARTCARD_DeInit()
153 or @ref HAL_SMARTCARD_Init() function.
154
155 [..]
156 When The compilation define USE_HAL_SMARTCARD_REGISTER_CALLBACKS is set to 0 or
157 not defined, the callback registration feature is not available
158 and weak (surcharged) callbacks are used.
159
160 @endverbatim
161 ******************************************************************************
162 * @attention
163 *
164 * <h2><center>&copy; Copyright (c) 2016 STMicroelectronics.
165 * All rights reserved.</center></h2>
166 *
167 * This software component is licensed by ST under BSD 3-Clause license,
168 * the "License"; You may not use this file except in compliance with the
169 * License. You may obtain a copy of the License at:
170 * opensource.org/licenses/BSD-3-Clause
171 *
172 ******************************************************************************
173 */
174
175/* Includes ------------------------------------------------------------------*/
176#include "stm32f4xx_hal.h"
177
178/** @addtogroup STM32F4xx_HAL_Driver
179 * @{
180 */
181
182/** @defgroup SMARTCARD SMARTCARD
183 * @brief HAL SMARTCARD module driver
184 * @{
185 */
186#ifdef HAL_SMARTCARD_MODULE_ENABLED
187/* Private typedef -----------------------------------------------------------*/
188/* Private define ------------------------------------------------------------*/
189/** @addtogroup SMARTCARD_Private_Constants
190 * @{
191 */
192/**
193 * @}
194 */
195
196/* Private macro -------------------------------------------------------------*/
197/* Private variables ---------------------------------------------------------*/
198/* Private function prototypes -----------------------------------------------*/
199/** @addtogroup SMARTCARD_Private_Functions
200 * @{
201 */
202#if (USE_HAL_SMARTCARD_REGISTER_CALLBACKS == 1)
203void SMARTCARD_InitCallbacksToDefault(SMARTCARD_HandleTypeDef *hsc);
204#endif /* USE_HAL_SMARTCARD_REGISTER_CALLBACKS */
205static void SMARTCARD_EndTxTransfer(SMARTCARD_HandleTypeDef *hsc);
206static void SMARTCARD_EndRxTransfer(SMARTCARD_HandleTypeDef *hsc);
207static void SMARTCARD_SetConfig (SMARTCARD_HandleTypeDef *hsc);
208static HAL_StatusTypeDef SMARTCARD_Transmit_IT(SMARTCARD_HandleTypeDef *hsc);
209static HAL_StatusTypeDef SMARTCARD_EndTransmit_IT(SMARTCARD_HandleTypeDef *hsc);
210static HAL_StatusTypeDef SMARTCARD_Receive_IT(SMARTCARD_HandleTypeDef *hsc);
211static void SMARTCARD_DMATransmitCplt(DMA_HandleTypeDef *hdma);
212static void SMARTCARD_DMAReceiveCplt(DMA_HandleTypeDef *hdma);
213static void SMARTCARD_DMAError(DMA_HandleTypeDef *hdma);
214static void SMARTCARD_DMAAbortOnError(DMA_HandleTypeDef *hdma);
215static void SMARTCARD_DMATxAbortCallback(DMA_HandleTypeDef *hdma);
216static void SMARTCARD_DMARxAbortCallback(DMA_HandleTypeDef *hdma);
217static void SMARTCARD_DMATxOnlyAbortCallback(DMA_HandleTypeDef *hdma);
218static void SMARTCARD_DMARxOnlyAbortCallback(DMA_HandleTypeDef *hdma);
219static HAL_StatusTypeDef SMARTCARD_WaitOnFlagUntilTimeout(SMARTCARD_HandleTypeDef *hsc, uint32_t Flag, FlagStatus Status, uint32_t Tickstart, uint32_t Timeout);
220/**
221 * @}
222 */
223
224/* Exported functions --------------------------------------------------------*/
225/** @defgroup SMARTCARD_Exported_Functions SMARTCARD Exported Functions
226 * @{
227 */
228
229/** @defgroup SMARTCARD_Exported_Functions_Group1 SmartCard Initialization and de-initialization functions
230 * @brief Initialization and Configuration functions
231 *
232@verbatim
233 ==============================================================================
234 ##### Initialization and Configuration functions #####
235 ==============================================================================
236 [..]
237 This subsection provides a set of functions allowing to initialize the USART
238 in Smartcard mode.
239 [..]
240 The Smartcard interface is designed to support asynchronous protocol Smartcards as
241 defined in the ISO 7816-3 standard.
242 [..]
243 The USART can provide a clock to the smartcard through the SCLK output.
244 In smartcard mode, SCLK is not associated to the communication but is simply derived
245 from the internal peripheral input clock through a 5-bit prescaler.
246 [..]
247 (+) For the Smartcard mode only these parameters can be configured:
248 (++) Baud Rate
249 (++) Word Length => Should be 9 bits (8 bits + parity)
250 (++) Stop Bit
251 (++) Parity: => Should be enabled
252 (++) USART polarity
253 (++) USART phase
254 (++) USART LastBit
255 (++) Receiver/transmitter modes
256 (++) Prescaler
257 (++) GuardTime
258 (++) NACKState: The Smartcard NACK state
259
260 (+) Recommended SmartCard interface configuration to get the Answer to Reset from the Card:
261 (++) Word Length = 9 Bits
262 (++) 1.5 Stop Bit
263 (++) Even parity
264 (++) BaudRate = 12096 baud
265 (++) Tx and Rx enabled
266 [..]
267 Please refer to the ISO 7816-3 specification for more details.
268
269 [..]
270 (@) It is also possible to choose 0.5 stop bit for receiving but it is recommended
271 to use 1.5 stop bits for both transmitting and receiving to avoid switching
272 between the two configurations.
273 [..]
274 The HAL_SMARTCARD_Init() function follows the USART SmartCard configuration
275 procedures (details for the procedures are available in reference manual
276 (RM0430 for STM32F4X3xx MCUs and RM0402 for STM32F412xx MCUs
277 RM0383 for STM32F411xC/E MCUs and RM0401 for STM32F410xx MCUs
278 RM0090 for STM32F4X5xx/STM32F4X7xx/STM32F429xx/STM32F439xx MCUs
279 RM0390 for STM32F446xx MCUs and RM0386 for STM32F469xx/STM32F479xx MCUs)).
280
281@endverbatim
282
283 The SMARTCARD frame format is given in the following table:
284 +-------------------------------------------------------------+
285 | M bit | PCE bit | SMARTCARD frame |
286 |---------------------|---------------------------------------|
287 | 1 | 1 | | SB | 8 bit data | PB | STB | |
288 +-------------------------------------------------------------+
289 * @{
290 */
291
292/**
293 * @brief Initializes the SmartCard mode according to the specified
294 * parameters in the SMARTCARD_InitTypeDef and create the associated handle.
295 * @param hsc Pointer to a SMARTCARD_HandleTypeDef structure that contains
296 * the configuration information for SMARTCARD module.
297 * @retval HAL status
298 */
299HAL_StatusTypeDef HAL_SMARTCARD_Init(SMARTCARD_HandleTypeDef *hsc)
300{
301 /* Check the SMARTCARD handle allocation */
302 if(hsc == NULL)
303 {
304 return HAL_ERROR;
305 }
306
307 /* Check the parameters */
308 assert_param(IS_SMARTCARD_INSTANCE(hsc->Instance));
309 assert_param(IS_SMARTCARD_NACK_STATE(hsc->Init.NACKState));
310
311 if(hsc->gState == HAL_SMARTCARD_STATE_RESET)
312 {
313 /* Allocate lock resource and initialize it */
314 hsc->Lock = HAL_UNLOCKED;
315
316#if USE_HAL_SMARTCARD_REGISTER_CALLBACKS == 1
317 SMARTCARD_InitCallbacksToDefault(hsc);
318
319 if (hsc->MspInitCallback == NULL)
320 {
321 hsc->MspInitCallback = HAL_SMARTCARD_MspInit;
322 }
323
324 /* Init the low level hardware */
325 hsc->MspInitCallback(hsc);
326#else
327 /* Init the low level hardware : GPIO, CLOCK */
328 HAL_SMARTCARD_MspInit(hsc);
329#endif /* USE_HAL_SMARTCARD_REGISTER_CALLBACKS */
330 }
331
332 hsc->gState = HAL_SMARTCARD_STATE_BUSY;
333
334 /* Set the Prescaler */
335 MODIFY_REG(hsc->Instance->GTPR, USART_GTPR_PSC, hsc->Init.Prescaler);
336
337 /* Set the Guard Time */
338 MODIFY_REG(hsc->Instance->GTPR, USART_GTPR_GT, ((hsc->Init.GuardTime)<<8U));
339
340 /* Set the Smartcard Communication parameters */
341 SMARTCARD_SetConfig(hsc);
342
343 /* In SmartCard mode, the following bits must be kept cleared:
344 - LINEN bit in the USART_CR2 register
345 - HDSEL and IREN bits in the USART_CR3 register.*/
346 CLEAR_BIT(hsc->Instance->CR2, USART_CR2_LINEN);
347 CLEAR_BIT(hsc->Instance->CR3, (USART_CR3_IREN | USART_CR3_HDSEL));
348
349 /* Enable the SMARTCARD Parity Error Interrupt */
350 SET_BIT(hsc->Instance->CR1, USART_CR1_PEIE);
351
352 /* Enable the SMARTCARD Framing Error Interrupt */
353 SET_BIT(hsc->Instance->CR3, USART_CR3_EIE);
354
355 /* Enable the Peripheral */
356 __HAL_SMARTCARD_ENABLE(hsc);
357
358 /* Configure the Smartcard NACK state */
359 MODIFY_REG(hsc->Instance->CR3, USART_CR3_NACK, hsc->Init.NACKState);
360
361 /* Enable the SC mode by setting the SCEN bit in the CR3 register */
362 hsc->Instance->CR3 |= (USART_CR3_SCEN);
363
364 /* Initialize the SMARTCARD state*/
365 hsc->ErrorCode = HAL_SMARTCARD_ERROR_NONE;
366 hsc->gState= HAL_SMARTCARD_STATE_READY;
367 hsc->RxState= HAL_SMARTCARD_STATE_READY;
368
369 return HAL_OK;
370}
371
372/**
373 * @brief DeInitializes the USART SmartCard peripheral
374 * @param hsc Pointer to a SMARTCARD_HandleTypeDef structure that contains
375 * the configuration information for SMARTCARD module.
376 * @retval HAL status
377 */
378HAL_StatusTypeDef HAL_SMARTCARD_DeInit(SMARTCARD_HandleTypeDef *hsc)
379{
380 /* Check the SMARTCARD handle allocation */
381 if(hsc == NULL)
382 {
383 return HAL_ERROR;
384 }
385
386 /* Check the parameters */
387 assert_param(IS_SMARTCARD_INSTANCE(hsc->Instance));
388
389 hsc->gState = HAL_SMARTCARD_STATE_BUSY;
390
391 /* Disable the Peripheral */
392 __HAL_SMARTCARD_DISABLE(hsc);
393
394 /* DeInit the low level hardware */
395#if USE_HAL_SMARTCARD_REGISTER_CALLBACKS == 1
396 if (hsc->MspDeInitCallback == NULL)
397 {
398 hsc->MspDeInitCallback = HAL_SMARTCARD_MspDeInit;
399 }
400 /* DeInit the low level hardware */
401 hsc->MspDeInitCallback(hsc);
402#else
403 HAL_SMARTCARD_MspDeInit(hsc);
404#endif /* USE_HAL_SMARTCARD_REGISTER_CALLBACKS */
405
406 hsc->ErrorCode = HAL_SMARTCARD_ERROR_NONE;
407 hsc->gState = HAL_SMARTCARD_STATE_RESET;
408 hsc->RxState = HAL_SMARTCARD_STATE_RESET;
409
410 /* Release Lock */
411 __HAL_UNLOCK(hsc);
412
413 return HAL_OK;
414}
415
416/**
417 * @brief SMARTCARD MSP Init
418 * @param hsc Pointer to a SMARTCARD_HandleTypeDef structure that contains
419 * the configuration information for SMARTCARD module.
420 * @retval None
421 */
422__weak void HAL_SMARTCARD_MspInit(SMARTCARD_HandleTypeDef *hsc)
423{
424 /* Prevent unused argument(s) compilation warning */
425 UNUSED(hsc);
426
427 /* NOTE : This function should not be modified, when the callback is needed,
428 the HAL_SMARTCARD_MspInit can be implemented in the user file
429 */
430}
431
432/**
433 * @brief SMARTCARD MSP DeInit
434 * @param hsc Pointer to a SMARTCARD_HandleTypeDef structure that contains
435 * the configuration information for SMARTCARD module.
436 * @retval None
437 */
438__weak void HAL_SMARTCARD_MspDeInit(SMARTCARD_HandleTypeDef *hsc)
439{
440 /* Prevent unused argument(s) compilation warning */
441 UNUSED(hsc);
442
443 /* NOTE : This function should not be modified, when the callback is needed,
444 the HAL_SMARTCARD_MspDeInit can be implemented in the user file
445 */
446}
447
448#if (USE_HAL_SMARTCARD_REGISTER_CALLBACKS == 1)
449/**
450 * @brief Register a User SMARTCARD Callback
451 * To be used instead of the weak predefined callback
452 * @param hsc smartcard handle
453 * @param CallbackID ID of the callback to be registered
454 * This parameter can be one of the following values:
455 * @arg @ref HAL_SMARTCARD_TX_COMPLETE_CB_ID Tx Complete Callback ID
456 * @arg @ref HAL_SMARTCARD_RX_COMPLETE_CB_ID Rx Complete Callback ID
457 * @arg @ref HAL_SMARTCARD_ERROR_CB_ID Error Callback ID
458 * @arg @ref HAL_SMARTCARD_ABORT_COMPLETE_CB_ID Abort Complete Callback ID
459 * @arg @ref HAL_SMARTCARD_ABORT_TRANSMIT_COMPLETE_CB_ID Abort Transmit Complete Callback ID
460 * @arg @ref HAL_SMARTCARD_ABORT_RECEIVE_COMPLETE_CB_ID Abort Receive Complete Callback ID
461 * @arg @ref HAL_SMARTCARD_MSPINIT_CB_ID MspInit Callback ID
462 * @arg @ref HAL_SMARTCARD_MSPDEINIT_CB_ID MspDeInit Callback ID
463 * @param pCallback pointer to the Callback function
464 * @retval HAL status
465 */
466HAL_StatusTypeDef HAL_SMARTCARD_RegisterCallback(SMARTCARD_HandleTypeDef *hsc, HAL_SMARTCARD_CallbackIDTypeDef CallbackID, pSMARTCARD_CallbackTypeDef pCallback)
467{
468 HAL_StatusTypeDef status = HAL_OK;
469
470 if (pCallback == NULL)
471 {
472 /* Update the error code */
473 hsc->ErrorCode |= HAL_SMARTCARD_ERROR_INVALID_CALLBACK;
474
475 return HAL_ERROR;
476 }
477 /* Process locked */
478 __HAL_LOCK(hsc);
479
480 if (hsc->gState == HAL_SMARTCARD_STATE_READY)
481 {
482 switch (CallbackID)
483 {
484
485 case HAL_SMARTCARD_TX_COMPLETE_CB_ID :
486 hsc->TxCpltCallback = pCallback;
487 break;
488
489 case HAL_SMARTCARD_RX_COMPLETE_CB_ID :
490 hsc->RxCpltCallback = pCallback;
491 break;
492
493 case HAL_SMARTCARD_ERROR_CB_ID :
494 hsc->ErrorCallback = pCallback;
495 break;
496
497 case HAL_SMARTCARD_ABORT_COMPLETE_CB_ID :
498 hsc->AbortCpltCallback = pCallback;
499 break;
500
501 case HAL_SMARTCARD_ABORT_TRANSMIT_COMPLETE_CB_ID :
502 hsc->AbortTransmitCpltCallback = pCallback;
503 break;
504
505 case HAL_SMARTCARD_ABORT_RECEIVE_COMPLETE_CB_ID :
506 hsc->AbortReceiveCpltCallback = pCallback;
507 break;
508
509
510 case HAL_SMARTCARD_MSPINIT_CB_ID :
511 hsc->MspInitCallback = pCallback;
512 break;
513
514 case HAL_SMARTCARD_MSPDEINIT_CB_ID :
515 hsc->MspDeInitCallback = pCallback;
516 break;
517
518 default :
519 /* Update the error code */
520 hsc->ErrorCode |= HAL_SMARTCARD_ERROR_INVALID_CALLBACK;
521
522 /* Return error status */
523 status = HAL_ERROR;
524 break;
525 }
526 }
527 else if (hsc->gState == HAL_SMARTCARD_STATE_RESET)
528 {
529 switch (CallbackID)
530 {
531 case HAL_SMARTCARD_MSPINIT_CB_ID :
532 hsc->MspInitCallback = pCallback;
533 break;
534
535 case HAL_SMARTCARD_MSPDEINIT_CB_ID :
536 hsc->MspDeInitCallback = pCallback;
537 break;
538
539 default :
540 /* Update the error code */
541 hsc->ErrorCode |= HAL_SMARTCARD_ERROR_INVALID_CALLBACK;
542
543 /* Return error status */
544 status = HAL_ERROR;
545 break;
546 }
547 }
548 else
549 {
550 /* Update the error code */
551 hsc->ErrorCode |= HAL_SMARTCARD_ERROR_INVALID_CALLBACK;
552
553 /* Return error status */
554 status = HAL_ERROR;
555 }
556
557 /* Release Lock */
558 __HAL_UNLOCK(hsc);
559
560 return status;
561}
562
563/**
564 * @brief Unregister an SMARTCARD callback
565 * SMARTCARD callback is redirected to the weak predefined callback
566 * @param hsc smartcard handle
567 * @param CallbackID ID of the callback to be unregistered
568 * This parameter can be one of the following values:
569 * @arg @ref HAL_SMARTCARD_TX_COMPLETE_CB_ID Tx Complete Callback ID
570 * @arg @ref HAL_SMARTCARD_RX_COMPLETE_CB_ID Rx Complete Callback ID
571 * @arg @ref HAL_SMARTCARD_ERROR_CB_ID Error Callback ID
572 * @arg @ref HAL_SMARTCARD_ABORT_COMPLETE_CB_ID Abort Complete Callback ID
573 * @arg @ref HAL_SMARTCARD_ABORT_TRANSMIT_COMPLETE_CB_ID Abort Transmit Complete Callback ID
574 * @arg @ref HAL_SMARTCARD_ABORT_RECEIVE_COMPLETE_CB_ID Abort Receive Complete Callback ID
575 * @arg @ref HAL_SMARTCARD_MSPINIT_CB_ID MspInit Callback ID
576 * @arg @ref HAL_SMARTCARD_MSPDEINIT_CB_ID MspDeInit Callback ID
577 * @retval HAL status
578 */
579HAL_StatusTypeDef HAL_SMARTCARD_UnRegisterCallback(SMARTCARD_HandleTypeDef *hsc, HAL_SMARTCARD_CallbackIDTypeDef CallbackID)
580{
581 HAL_StatusTypeDef status = HAL_OK;
582
583 /* Process locked */
584 __HAL_LOCK(hsc);
585
586 if (HAL_SMARTCARD_STATE_READY == hsc->gState)
587 {
588 switch (CallbackID)
589 {
590 case HAL_SMARTCARD_TX_COMPLETE_CB_ID :
591 hsc->TxCpltCallback = HAL_SMARTCARD_TxCpltCallback; /* Legacy weak TxCpltCallback */
592 break;
593
594 case HAL_SMARTCARD_RX_COMPLETE_CB_ID :
595 hsc->RxCpltCallback = HAL_SMARTCARD_RxCpltCallback; /* Legacy weak RxCpltCallback */
596 break;
597
598 case HAL_SMARTCARD_ERROR_CB_ID :
599 hsc->ErrorCallback = HAL_SMARTCARD_ErrorCallback; /* Legacy weak ErrorCallback */
600 break;
601
602 case HAL_SMARTCARD_ABORT_COMPLETE_CB_ID :
603 hsc->AbortCpltCallback = HAL_SMARTCARD_AbortCpltCallback; /* Legacy weak AbortCpltCallback */
604 break;
605
606 case HAL_SMARTCARD_ABORT_TRANSMIT_COMPLETE_CB_ID :
607 hsc->AbortTransmitCpltCallback = HAL_SMARTCARD_AbortTransmitCpltCallback; /* Legacy weak AbortTransmitCpltCallback */
608 break;
609
610 case HAL_SMARTCARD_ABORT_RECEIVE_COMPLETE_CB_ID :
611 hsc->AbortReceiveCpltCallback = HAL_SMARTCARD_AbortReceiveCpltCallback; /* Legacy weak AbortReceiveCpltCallback */
612 break;
613
614
615 case HAL_SMARTCARD_MSPINIT_CB_ID :
616 hsc->MspInitCallback = HAL_SMARTCARD_MspInit; /* Legacy weak MspInitCallback */
617 break;
618
619 case HAL_SMARTCARD_MSPDEINIT_CB_ID :
620 hsc->MspDeInitCallback = HAL_SMARTCARD_MspDeInit; /* Legacy weak MspDeInitCallback */
621 break;
622
623 default :
624 /* Update the error code */
625 hsc->ErrorCode |= HAL_SMARTCARD_ERROR_INVALID_CALLBACK;
626
627 /* Return error status */
628 status = HAL_ERROR;
629 break;
630 }
631 }
632 else if (HAL_SMARTCARD_STATE_RESET == hsc->gState)
633 {
634 switch (CallbackID)
635 {
636 case HAL_SMARTCARD_MSPINIT_CB_ID :
637 hsc->MspInitCallback = HAL_SMARTCARD_MspInit;
638 break;
639
640 case HAL_SMARTCARD_MSPDEINIT_CB_ID :
641 hsc->MspDeInitCallback = HAL_SMARTCARD_MspDeInit;
642 break;
643
644 default :
645 /* Update the error code */
646 hsc->ErrorCode |= HAL_SMARTCARD_ERROR_INVALID_CALLBACK;
647
648 /* Return error status */
649 status = HAL_ERROR;
650 break;
651 }
652 }
653 else
654 {
655 /* Update the error code */
656 hsc->ErrorCode |= HAL_SMARTCARD_ERROR_INVALID_CALLBACK;
657
658 /* Return error status */
659 status = HAL_ERROR;
660 }
661
662 /* Release Lock */
663 __HAL_UNLOCK(hsc);
664
665 return status;
666}
667#endif /* USE_HAL_SMARTCARD_REGISTER_CALLBACKS */
668
669/**
670 * @}
671 */
672
673/** @defgroup SMARTCARD_Exported_Functions_Group2 IO operation functions
674 * @brief SMARTCARD Transmit and Receive functions
675 *
676@verbatim
677 ===============================================================================
678 ##### IO operation functions #####
679 ===============================================================================
680 [..]
681 This subsection provides a set of functions allowing to manage the SMARTCARD data transfers.
682
683 [..]
684 (#) Smartcard is a single wire half duplex communication protocol.
685 The Smartcard interface is designed to support asynchronous protocol Smartcards as
686 defined in the ISO 7816-3 standard.
687 (#) The USART should be configured as:
688 (++) 8 bits plus parity: where M=1 and PCE=1 in the USART_CR1 register
689 (++) 1.5 stop bits when transmitting and receiving: where STOP=11 in the USART_CR2 register.
690
691 (#) There are two modes of transfer:
692 (++) Blocking mode: The communication is performed in polling mode.
693 The HAL status of all data processing is returned by the same function
694 after finishing transfer.
695 (++) Non Blocking mode: The communication is performed using Interrupts
696 or DMA, These APIs return the HAL status.
697 The end of the data processing will be indicated through the
698 dedicated SMARTCARD IRQ when using Interrupt mode or the DMA IRQ when
699 using DMA mode.
700 The HAL_SMARTCARD_TxCpltCallback(), HAL_SMARTCARD_RxCpltCallback() user callbacks
701 will be executed respectively at the end of the Transmit or Receive process
702 The HAL_SMARTCARD_ErrorCallback() user callback will be executed when a communication error is detected
703
704 (#) Blocking mode APIs are :
705 (++) HAL_SMARTCARD_Transmit()
706 (++) HAL_SMARTCARD_Receive()
707
708 (#) Non Blocking mode APIs with Interrupt are :
709 (++) HAL_SMARTCARD_Transmit_IT()
710 (++) HAL_SMARTCARD_Receive_IT()
711 (++) HAL_SMARTCARD_IRQHandler()
712
713 (#) Non Blocking mode functions with DMA are :
714 (++) HAL_SMARTCARD_Transmit_DMA()
715 (++) HAL_SMARTCARD_Receive_DMA()
716
717 (#) A set of Transfer Complete Callbacks are provided in non Blocking mode:
718 (++) HAL_SMARTCARD_TxCpltCallback()
719 (++) HAL_SMARTCARD_RxCpltCallback()
720 (++) HAL_SMARTCARD_ErrorCallback()
721
722 (#) Non-Blocking mode transfers could be aborted using Abort API's :
723 (+) HAL_SMARTCARD_Abort()
724 (+) HAL_SMARTCARD_AbortTransmit()
725 (+) HAL_SMARTCARD_AbortReceive()
726 (+) HAL_SMARTCARD_Abort_IT()
727 (+) HAL_SMARTCARD_AbortTransmit_IT()
728 (+) HAL_SMARTCARD_AbortReceive_IT()
729
730 (#) For Abort services based on interrupts (HAL_SMARTCARD_Abortxxx_IT), a set of Abort Complete Callbacks are provided:
731 (+) HAL_SMARTCARD_AbortCpltCallback()
732 (+) HAL_SMARTCARD_AbortTransmitCpltCallback()
733 (+) HAL_SMARTCARD_AbortReceiveCpltCallback()
734
735 (#) In Non-Blocking mode transfers, possible errors are split into 2 categories.
736 Errors are handled as follows :
737 (+) Error is considered as Recoverable and non blocking : Transfer could go till end, but error severity is
738 to be evaluated by user : this concerns Frame Error, Parity Error or Noise Error in Interrupt mode reception .
739 Received character is then retrieved and stored in Rx buffer, Error code is set to allow user to identify error type,
740 and HAL_SMARTCARD_ErrorCallback() user callback is executed. Transfer is kept ongoing on SMARTCARD side.
741 If user wants to abort it, Abort services should be called by user.
742 (+) Error is considered as Blocking : Transfer could not be completed properly and is aborted.
743 This concerns Frame Error in Interrupt mode transmission, Overrun Error in Interrupt mode reception and all errors in DMA mode.
744 Error code is set to allow user to identify error type, and HAL_SMARTCARD_ErrorCallback() user callback is executed.
745
746@endverbatim
747 * @{
748 */
749
750/**
751 * @brief Send an amount of data in blocking mode
752 * @param hsc Pointer to a SMARTCARD_HandleTypeDef structure that contains
753 * the configuration information for SMARTCARD module.
754 * @param pData Pointer to data buffer
755 * @param Size Amount of data to be sent
756 * @param Timeout Timeout duration
757 * @retval HAL status
758 */
759HAL_StatusTypeDef HAL_SMARTCARD_Transmit(SMARTCARD_HandleTypeDef *hsc, uint8_t *pData, uint16_t Size, uint32_t Timeout)
760{
761 uint8_t *tmp = pData;
762 uint32_t tickstart = 0U;
763
764 if(hsc->gState == HAL_SMARTCARD_STATE_READY)
765 {
766 if((pData == NULL) || (Size == 0U))
767 {
768 return HAL_ERROR;
769 }
770
771 /* Process Locked */
772 __HAL_LOCK(hsc);
773
774 hsc->ErrorCode = HAL_SMARTCARD_ERROR_NONE;
775 hsc->gState = HAL_SMARTCARD_STATE_BUSY_TX;
776
777 /* Init tickstart for timeout management */
778 tickstart = HAL_GetTick();
779
780 hsc->TxXferSize = Size;
781 hsc->TxXferCount = Size;
782 while(hsc->TxXferCount > 0U)
783 {
784 hsc->TxXferCount--;
785 if(SMARTCARD_WaitOnFlagUntilTimeout(hsc, SMARTCARD_FLAG_TXE, RESET, tickstart, Timeout) != HAL_OK)
786 {
787 return HAL_TIMEOUT;
788 }
789 hsc->Instance->DR = (uint8_t)(*tmp & 0xFFU);
790 tmp++;
791 }
792
793 if(SMARTCARD_WaitOnFlagUntilTimeout(hsc, SMARTCARD_FLAG_TC, RESET, tickstart, Timeout) != HAL_OK)
794 {
795 return HAL_TIMEOUT;
796 }
797
798 /* At end of Tx process, restore hsc->gState to Ready */
799 hsc->gState = HAL_SMARTCARD_STATE_READY;
800
801 /* Process Unlocked */
802 __HAL_UNLOCK(hsc);
803
804 return HAL_OK;
805 }
806 else
807 {
808 return HAL_BUSY;
809 }
810}
811
812/**
813 * @brief Receive an amount of data in blocking mode
814 * @param hsc Pointer to a SMARTCARD_HandleTypeDef structure that contains
815 * the configuration information for SMARTCARD module.
816 * @param pData Pointer to data buffer
817 * @param Size Amount of data to be received
818 * @param Timeout Timeout duration
819 * @retval HAL status
820 */
821HAL_StatusTypeDef HAL_SMARTCARD_Receive(SMARTCARD_HandleTypeDef *hsc, uint8_t *pData, uint16_t Size, uint32_t Timeout)
822{
823 uint8_t *tmp = pData;
824 uint32_t tickstart = 0U;
825
826 if(hsc->RxState == HAL_SMARTCARD_STATE_READY)
827 {
828 if((pData == NULL) || (Size == 0U))
829 {
830 return HAL_ERROR;
831 }
832
833 /* Process Locked */
834 __HAL_LOCK(hsc);
835
836 hsc->ErrorCode = HAL_SMARTCARD_ERROR_NONE;
837 hsc->RxState = HAL_SMARTCARD_STATE_BUSY_RX;
838
839 /* Init tickstart for timeout management */
840 tickstart = HAL_GetTick();
841
842 hsc->RxXferSize = Size;
843 hsc->RxXferCount = Size;
844
845 /* Check the remain data to be received */
846 while(hsc->RxXferCount > 0U)
847 {
848 hsc->RxXferCount--;
849 if(SMARTCARD_WaitOnFlagUntilTimeout(hsc, SMARTCARD_FLAG_RXNE, RESET, tickstart, Timeout) != HAL_OK)
850 {
851 return HAL_TIMEOUT;
852 }
853 *tmp = (uint8_t)(hsc->Instance->DR & (uint8_t)0xFFU);
854 tmp++;
855 }
856
857 /* At end of Rx process, restore hsc->RxState to Ready */
858 hsc->RxState = HAL_SMARTCARD_STATE_READY;
859
860 /* Process Unlocked */
861 __HAL_UNLOCK(hsc);
862
863 return HAL_OK;
864 }
865 else
866 {
867 return HAL_BUSY;
868 }
869}
870
871/**
872 * @brief Send an amount of data in non blocking mode
873 * @param hsc Pointer to a SMARTCARD_HandleTypeDef structure that contains
874 * the configuration information for SMARTCARD module.
875 * @param pData Pointer to data buffer
876 * @param Size Amount of data to be sent
877 * @retval HAL status
878 */
879HAL_StatusTypeDef HAL_SMARTCARD_Transmit_IT(SMARTCARD_HandleTypeDef *hsc, uint8_t *pData, uint16_t Size)
880{
881 /* Check that a Tx process is not already ongoing */
882 if(hsc->gState == HAL_SMARTCARD_STATE_READY)
883 {
884 if((pData == NULL) || (Size == 0U))
885 {
886 return HAL_ERROR;
887 }
888
889 /* Process Locked */
890 __HAL_LOCK(hsc);
891
892 hsc->pTxBuffPtr = pData;
893 hsc->TxXferSize = Size;
894 hsc->TxXferCount = Size;
895
896 hsc->ErrorCode = HAL_SMARTCARD_ERROR_NONE;
897 hsc->gState = HAL_SMARTCARD_STATE_BUSY_TX;
898
899 /* Process Unlocked */
900 __HAL_UNLOCK(hsc);
901
902 /* Enable the SMARTCARD Parity Error Interrupt */
903 SET_BIT(hsc->Instance->CR1, USART_CR1_PEIE);
904
905 /* Disable the SMARTCARD Error Interrupt: (Frame error, noise error, overrun error) */
906 CLEAR_BIT(hsc->Instance->CR3, USART_CR3_EIE);
907
908 /* Enable the SMARTCARD Transmit data register empty Interrupt */
909 SET_BIT(hsc->Instance->CR1, USART_CR1_TXEIE);
910
911 return HAL_OK;
912 }
913 else
914 {
915 return HAL_BUSY;
916 }
917}
918
919/**
920 * @brief Receive an amount of data in non blocking mode
921 * @param hsc Pointer to a SMARTCARD_HandleTypeDef structure that contains
922 * the configuration information for SMARTCARD module.
923 * @param pData Pointer to data buffer
924 * @param Size Amount of data to be received
925 * @retval HAL status
926 */
927HAL_StatusTypeDef HAL_SMARTCARD_Receive_IT(SMARTCARD_HandleTypeDef *hsc, uint8_t *pData, uint16_t Size)
928{
929 /* Check that a Rx process is not already ongoing */
930 if(hsc->RxState == HAL_SMARTCARD_STATE_READY)
931 {
932 if((pData == NULL) || (Size == 0U))
933 {
934 return HAL_ERROR;
935 }
936
937 /* Process Locked */
938 __HAL_LOCK(hsc);
939
940 hsc->pRxBuffPtr = pData;
941 hsc->RxXferSize = Size;
942 hsc->RxXferCount = Size;
943
944 hsc->ErrorCode = HAL_SMARTCARD_ERROR_NONE;
945 hsc->RxState = HAL_SMARTCARD_STATE_BUSY_RX;
946
947 /* Process Unlocked */
948 __HAL_UNLOCK(hsc);
949
950 /* Enable the SMARTCARD Parity Error and Data Register not empty Interrupts */
951 SET_BIT(hsc->Instance->CR1, USART_CR1_PEIE| USART_CR1_RXNEIE);
952
953 /* Enable the SMARTCARD Error Interrupt: (Frame error, noise error, overrun error) */
954 SET_BIT(hsc->Instance->CR3, USART_CR3_EIE);
955
956 return HAL_OK;
957 }
958 else
959 {
960 return HAL_BUSY;
961 }
962}
963
964/**
965 * @brief Send an amount of data in non blocking mode
966 * @param hsc Pointer to a SMARTCARD_HandleTypeDef structure that contains
967 * the configuration information for SMARTCARD module.
968 * @param pData Pointer to data buffer
969 * @param Size Amount of data to be sent
970 * @retval HAL status
971 */
972HAL_StatusTypeDef HAL_SMARTCARD_Transmit_DMA(SMARTCARD_HandleTypeDef *hsc, uint8_t *pData, uint16_t Size)
973{
974 uint32_t *tmp;
975
976 /* Check that a Tx process is not already ongoing */
977 if(hsc->gState == HAL_SMARTCARD_STATE_READY)
978 {
979 if((pData == NULL) || (Size == 0U))
980 {
981 return HAL_ERROR;
982 }
983
984 /* Process Locked */
985 __HAL_LOCK(hsc);
986
987 hsc->pTxBuffPtr = pData;
988 hsc->TxXferSize = Size;
989 hsc->TxXferCount = Size;
990
991 hsc->ErrorCode = HAL_SMARTCARD_ERROR_NONE;
992 hsc->gState = HAL_SMARTCARD_STATE_BUSY_TX;
993
994 /* Set the SMARTCARD DMA transfer complete callback */
995 hsc->hdmatx->XferCpltCallback = SMARTCARD_DMATransmitCplt;
996
997 /* Set the DMA error callback */
998 hsc->hdmatx->XferErrorCallback = SMARTCARD_DMAError;
999
1000 /* Set the DMA abort callback */
1001 hsc->hdmatx->XferAbortCallback = NULL;
1002
1003 /* Enable the SMARTCARD transmit DMA stream */
1004 tmp = (uint32_t*)&pData;
1005 HAL_DMA_Start_IT(hsc->hdmatx, *(uint32_t*)tmp, (uint32_t)&hsc->Instance->DR, Size);
1006
1007 /* Clear the TC flag in the SR register by writing 0 to it */
1008 __HAL_SMARTCARD_CLEAR_FLAG(hsc, SMARTCARD_FLAG_TC);
1009
1010 /* Process Unlocked */
1011 __HAL_UNLOCK(hsc);
1012
1013 /* Enable the DMA transfer for transmit request by setting the DMAT bit
1014 in the SMARTCARD CR3 register */
1015 SET_BIT(hsc->Instance->CR3, USART_CR3_DMAT);
1016
1017 return HAL_OK;
1018 }
1019 else
1020 {
1021 return HAL_BUSY;
1022 }
1023}
1024
1025/**
1026 * @brief Receive an amount of data in non blocking mode
1027 * @param hsc Pointer to a SMARTCARD_HandleTypeDef structure that contains
1028 * the configuration information for SMARTCARD module.
1029 * @param pData Pointer to data buffer
1030 * @param Size Amount of data to be received
1031 * @note When the SMARTCARD parity is enabled (PCE = 1) the data received contain the parity bit.s
1032 * @retval HAL status
1033 */
1034HAL_StatusTypeDef HAL_SMARTCARD_Receive_DMA(SMARTCARD_HandleTypeDef *hsc, uint8_t *pData, uint16_t Size)
1035{
1036 uint32_t *tmp;
1037
1038 /* Check that a Rx process is not already ongoing */
1039 if(hsc->RxState == HAL_SMARTCARD_STATE_READY)
1040 {
1041 if((pData == NULL) || (Size == 0U))
1042 {
1043 return HAL_ERROR;
1044 }
1045
1046 /* Process Locked */
1047 __HAL_LOCK(hsc);
1048
1049 hsc->pRxBuffPtr = pData;
1050 hsc->RxXferSize = Size;
1051
1052 hsc->ErrorCode = HAL_SMARTCARD_ERROR_NONE;
1053 hsc->RxState = HAL_SMARTCARD_STATE_BUSY_RX;
1054
1055 /* Set the SMARTCARD DMA transfer complete callback */
1056 hsc->hdmarx->XferCpltCallback = SMARTCARD_DMAReceiveCplt;
1057
1058 /* Set the DMA error callback */
1059 hsc->hdmarx->XferErrorCallback = SMARTCARD_DMAError;
1060
1061 /* Set the DMA abort callback */
1062 hsc->hdmatx->XferAbortCallback = NULL;
1063
1064 /* Enable the DMA stream */
1065 tmp = (uint32_t*)&pData;
1066 HAL_DMA_Start_IT(hsc->hdmarx, (uint32_t)&hsc->Instance->DR, *(uint32_t*)tmp, Size);
1067
1068 /* Clear the Overrun flag just before enabling the DMA Rx request: can be mandatory for the second transfer */
1069 __HAL_SMARTCARD_CLEAR_OREFLAG(hsc);
1070
1071 /* Process Unlocked */
1072 __HAL_UNLOCK(hsc);
1073
1074 /* Enable the SMARTCARD Parity Error Interrupt */
1075 SET_BIT(hsc->Instance->CR1, USART_CR1_PEIE);
1076
1077 /* Enable the SMARTCARD Error Interrupt: (Frame error, noise error, overrun error) */
1078 SET_BIT(hsc->Instance->CR3, USART_CR3_EIE);
1079
1080 /* Enable the DMA transfer for the receiver request by setting the DMAR bit
1081 in the SMARTCARD CR3 register */
1082 SET_BIT(hsc->Instance->CR3, USART_CR3_DMAR);
1083
1084 return HAL_OK;
1085 }
1086 else
1087 {
1088 return HAL_BUSY;
1089 }
1090}
1091
1092/**
1093 * @brief Abort ongoing transfers (blocking mode).
1094 * @param hsc SMARTCARD handle.
1095 * @note This procedure could be used for aborting any ongoing transfer started in Interrupt or DMA mode.
1096 * This procedure performs following operations :
1097 * - Disable PPP Interrupts
1098 * - Disable the DMA transfer in the peripheral register (if enabled)
1099 * - Abort DMA transfer by calling HAL_DMA_Abort (in case of transfer in DMA mode)
1100 * - Set handle State to READY
1101 * @note This procedure is executed in blocking mode : when exiting function, Abort is considered as completed.
1102 * @retval HAL status
1103*/
1104HAL_StatusTypeDef HAL_SMARTCARD_Abort(SMARTCARD_HandleTypeDef *hsc)
1105{
1106 /* Disable TXEIE, TCIE, RXNE, PE and ERR (Frame error, noise error, overrun error) interrupts */
1107 CLEAR_BIT(hsc->Instance->CR1, (USART_CR1_RXNEIE | USART_CR1_PEIE | USART_CR1_TXEIE | USART_CR1_TCIE));
1108 CLEAR_BIT(hsc->Instance->CR3, USART_CR3_EIE);
1109
1110 /* Disable the SMARTCARD DMA Tx request if enabled */
1111 if(HAL_IS_BIT_SET(hsc->Instance->CR3, USART_CR3_DMAT))
1112 {
1113 CLEAR_BIT(hsc->Instance->CR3, USART_CR3_DMAT);
1114
1115 /* Abort the SMARTCARD DMA Tx channel : use blocking DMA Abort API (no callback) */
1116 if(hsc->hdmatx != NULL)
1117 {
1118 /* Set the SMARTCARD DMA Abort callback to Null.
1119 No call back execution at end of DMA abort procedure */
1120 hsc->hdmatx->XferAbortCallback = NULL;
1121
1122 HAL_DMA_Abort(hsc->hdmatx);
1123 }
1124 }
1125
1126 /* Disable the SMARTCARD DMA Rx request if enabled */
1127 if(HAL_IS_BIT_SET(hsc->Instance->CR3, USART_CR3_DMAR))
1128 {
1129 CLEAR_BIT(hsc->Instance->CR3, USART_CR3_DMAR);
1130
1131 /* Abort the SMARTCARD DMA Rx channel : use blocking DMA Abort API (no callback) */
1132 if(hsc->hdmarx != NULL)
1133 {
1134 /* Set the SMARTCARD DMA Abort callback to Null.
1135 No call back execution at end of DMA abort procedure */
1136 hsc->hdmarx->XferAbortCallback = NULL;
1137
1138 HAL_DMA_Abort(hsc->hdmarx);
1139 }
1140 }
1141
1142 /* Reset Tx and Rx transfer counters */
1143 hsc->TxXferCount = 0x00U;
1144 hsc->RxXferCount = 0x00U;
1145
1146 /* Reset ErrorCode */
1147 hsc->ErrorCode = HAL_SMARTCARD_ERROR_NONE;
1148
1149 /* Restore hsc->RxState and hsc->gState to Ready */
1150 hsc->RxState = HAL_SMARTCARD_STATE_READY;
1151 hsc->gState = HAL_SMARTCARD_STATE_READY;
1152
1153 return HAL_OK;
1154}
1155
1156/**
1157 * @brief Abort ongoing Transmit transfer (blocking mode).
1158 * @param hsc SMARTCARD handle.
1159 * @note This procedure could be used for aborting any ongoing transfer started in Interrupt or DMA mode.
1160 * This procedure performs following operations :
1161 * - Disable SMARTCARD Interrupts (Tx)
1162 * - Disable the DMA transfer in the peripheral register (if enabled)
1163 * - Abort DMA transfer by calling HAL_DMA_Abort (in case of transfer in DMA mode)
1164 * - Set handle State to READY
1165 * @note This procedure is executed in blocking mode : when exiting function, Abort is considered as completed.
1166 * @retval HAL status
1167*/
1168HAL_StatusTypeDef HAL_SMARTCARD_AbortTransmit(SMARTCARD_HandleTypeDef *hsc)
1169{
1170 /* Disable TXEIE and TCIE interrupts */
1171 CLEAR_BIT(hsc->Instance->CR1, (USART_CR1_TXEIE | USART_CR1_TCIE));
1172
1173 /* Disable the SMARTCARD DMA Tx request if enabled */
1174 if(HAL_IS_BIT_SET(hsc->Instance->CR3, USART_CR3_DMAT))
1175 {
1176 CLEAR_BIT(hsc->Instance->CR3, USART_CR3_DMAT);
1177
1178 /* Abort the SMARTCARD DMA Tx channel : use blocking DMA Abort API (no callback) */
1179 if(hsc->hdmatx != NULL)
1180 {
1181 /* Set the SMARTCARD DMA Abort callback to Null.
1182 No call back execution at end of DMA abort procedure */
1183 hsc->hdmatx->XferAbortCallback = NULL;
1184
1185 HAL_DMA_Abort(hsc->hdmatx);
1186 }
1187 }
1188
1189 /* Reset Tx transfer counter */
1190 hsc->TxXferCount = 0x00U;
1191
1192 /* Restore hsc->gState to Ready */
1193 hsc->gState = HAL_SMARTCARD_STATE_READY;
1194
1195 return HAL_OK;
1196}
1197
1198/**
1199 * @brief Abort ongoing Receive transfer (blocking mode).
1200 * @param hsc SMARTCARD handle.
1201 * @note This procedure could be used for aborting any ongoing transfer started in Interrupt or DMA mode.
1202 * This procedure performs following operations :
1203 * - Disable PPP Interrupts
1204 * - Disable the DMA transfer in the peripheral register (if enabled)
1205 * - Abort DMA transfer by calling HAL_DMA_Abort (in case of transfer in DMA mode)
1206 * - Set handle State to READY
1207 * @note This procedure is executed in blocking mode : when exiting function, Abort is considered as completed.
1208 * @retval HAL status
1209*/
1210HAL_StatusTypeDef HAL_SMARTCARD_AbortReceive(SMARTCARD_HandleTypeDef *hsc)
1211{
1212 /* Disable RXNE, PE and ERR (Frame error, noise error, overrun error) interrupts */
1213 CLEAR_BIT(hsc->Instance->CR1, (USART_CR1_RXNEIE | USART_CR1_PEIE));
1214 CLEAR_BIT(hsc->Instance->CR3, USART_CR3_EIE);
1215
1216 /* Disable the SMARTCARD DMA Rx request if enabled */
1217 if(HAL_IS_BIT_SET(hsc->Instance->CR3, USART_CR3_DMAR))
1218 {
1219 CLEAR_BIT(hsc->Instance->CR3, USART_CR3_DMAR);
1220
1221 /* Abort the SMARTCARD DMA Rx channel : use blocking DMA Abort API (no callback) */
1222 if(hsc->hdmarx != NULL)
1223 {
1224 /* Set the SMARTCARD DMA Abort callback to Null.
1225 No call back execution at end of DMA abort procedure */
1226 hsc->hdmarx->XferAbortCallback = NULL;
1227
1228 HAL_DMA_Abort(hsc->hdmarx);
1229 }
1230 }
1231
1232 /* Reset Rx transfer counter */
1233 hsc->RxXferCount = 0x00U;
1234
1235 /* Restore hsc->RxState to Ready */
1236 hsc->RxState = HAL_SMARTCARD_STATE_READY;
1237
1238 return HAL_OK;
1239}
1240
1241/**
1242 * @brief Abort ongoing transfers (Interrupt mode).
1243 * @param hsc SMARTCARD handle.
1244 * @note This procedure could be used for aborting any ongoing transfer started in Interrupt or DMA mode.
1245 * This procedure performs following operations :
1246 * - Disable PPP Interrupts
1247 * - Disable the DMA transfer in the peripheral register (if enabled)
1248 * - Abort DMA transfer by calling HAL_DMA_Abort_IT (in case of transfer in DMA mode)
1249 * - Set handle State to READY
1250 * - At abort completion, call user abort complete callback
1251 * @note This procedure is executed in Interrupt mode, meaning that abort procedure could be
1252 * considered as completed only when user abort complete callback is executed (not when exiting function).
1253 * @retval HAL status
1254*/
1255HAL_StatusTypeDef HAL_SMARTCARD_Abort_IT(SMARTCARD_HandleTypeDef *hsc)
1256{
1257 uint32_t AbortCplt = 0x01U;
1258
1259 /* Disable TXEIE, TCIE, RXNE, PE and ERR (Frame error, noise error, overrun error) interrupts */
1260 CLEAR_BIT(hsc->Instance->CR1, (USART_CR1_RXNEIE | USART_CR1_PEIE | USART_CR1_TXEIE | USART_CR1_TCIE));
1261 CLEAR_BIT(hsc->Instance->CR3, USART_CR3_EIE);
1262
1263 /* If DMA Tx and/or DMA Rx Handles are associated to SMARTCARD Handle, DMA Abort complete callbacks should be initialised
1264 before any call to DMA Abort functions */
1265 /* DMA Tx Handle is valid */
1266 if(hsc->hdmatx != NULL)
1267 {
1268 /* Set DMA Abort Complete callback if SMARTCARD DMA Tx request if enabled.
1269 Otherwise, set it to NULL */
1270 if(HAL_IS_BIT_SET(hsc->Instance->CR3, USART_CR3_DMAT))
1271 {
1272 hsc->hdmatx->XferAbortCallback = SMARTCARD_DMATxAbortCallback;
1273 }
1274 else
1275 {
1276 hsc->hdmatx->XferAbortCallback = NULL;
1277 }
1278 }
1279 /* DMA Rx Handle is valid */
1280 if(hsc->hdmarx != NULL)
1281 {
1282 /* Set DMA Abort Complete callback if SMARTCARD DMA Rx request if enabled.
1283 Otherwise, set it to NULL */
1284 if(HAL_IS_BIT_SET(hsc->Instance->CR3, USART_CR3_DMAR))
1285 {
1286 hsc->hdmarx->XferAbortCallback = SMARTCARD_DMARxAbortCallback;
1287 }
1288 else
1289 {
1290 hsc->hdmarx->XferAbortCallback = NULL;
1291 }
1292 }
1293
1294 /* Disable the SMARTCARD DMA Tx request if enabled */
1295 if(HAL_IS_BIT_SET(hsc->Instance->CR3, USART_CR3_DMAT))
1296 {
1297 /* Disable DMA Tx at SMARTCARD level */
1298 CLEAR_BIT(hsc->Instance->CR3, USART_CR3_DMAT);
1299
1300 /* Abort the SMARTCARD DMA Tx channel : use non blocking DMA Abort API (callback) */
1301 if(hsc->hdmatx != NULL)
1302 {
1303 /* SMARTCARD Tx DMA Abort callback has already been initialised :
1304 will lead to call HAL_SMARTCARD_AbortCpltCallback() at end of DMA abort procedure */
1305
1306 /* Abort DMA TX */
1307 if(HAL_DMA_Abort_IT(hsc->hdmatx) != HAL_OK)
1308 {
1309 hsc->hdmatx->XferAbortCallback = NULL;
1310 }
1311 else
1312 {
1313 AbortCplt = 0x00U;
1314 }
1315 }
1316 }
1317
1318 /* Disable the SMARTCARD DMA Rx request if enabled */
1319 if(HAL_IS_BIT_SET(hsc->Instance->CR3, USART_CR3_DMAR))
1320 {
1321 CLEAR_BIT(hsc->Instance->CR3, USART_CR3_DMAR);
1322
1323 /* Abort the SMARTCARD DMA Rx channel : use non blocking DMA Abort API (callback) */
1324 if(hsc->hdmarx != NULL)
1325 {
1326 /* SMARTCARD Rx DMA Abort callback has already been initialised :
1327 will lead to call HAL_SMARTCARD_AbortCpltCallback() at end of DMA abort procedure */
1328
1329 /* Abort DMA RX */
1330 if(HAL_DMA_Abort_IT(hsc->hdmarx) != HAL_OK)
1331 {
1332 hsc->hdmarx->XferAbortCallback = NULL;
1333 AbortCplt = 0x01U;
1334 }
1335 else
1336 {
1337 AbortCplt = 0x00U;
1338 }
1339 }
1340 }
1341
1342 /* if no DMA abort complete callback execution is required => call user Abort Complete callback */
1343 if(AbortCplt == 0x01U)
1344 {
1345 /* Reset Tx and Rx transfer counters */
1346 hsc->TxXferCount = 0x00U;
1347 hsc->RxXferCount = 0x00U;
1348
1349 /* Reset ErrorCode */
1350 hsc->ErrorCode = HAL_SMARTCARD_ERROR_NONE;
1351
1352 /* Restore hsc->gState and hsc->RxState to Ready */
1353 hsc->gState = HAL_SMARTCARD_STATE_READY;
1354 hsc->RxState = HAL_SMARTCARD_STATE_READY;
1355
1356 /* As no DMA to be aborted, call directly user Abort complete callback */
1357#if (USE_HAL_SMARTCARD_REGISTER_CALLBACKS == 1)
1358 /* Call registered Abort complete callback */
1359 hsc->AbortCpltCallback(hsc);
1360#else
1361 /* Call legacy weak Abort complete callback */
1362 HAL_SMARTCARD_AbortCpltCallback(hsc);
1363#endif /* USE_HAL_SMARTCARD_REGISTER_CALLBACK */
1364 }
1365 return HAL_OK;
1366}
1367
1368/**
1369 * @brief Abort ongoing Transmit transfer (Interrupt mode).
1370 * @param hsc SMARTCARD handle.
1371 * @note This procedure could be used for aborting any ongoing transfer started in Interrupt or DMA mode.
1372 * This procedure performs following operations :
1373 * - Disable SMARTCARD Interrupts (Tx)
1374 * - Disable the DMA transfer in the peripheral register (if enabled)
1375 * - Abort DMA transfer by calling HAL_DMA_Abort_IT (in case of transfer in DMA mode)
1376 * - Set handle State to READY
1377 * - At abort completion, call user abort complete callback
1378 * @note This procedure is executed in Interrupt mode, meaning that abort procedure could be
1379 * considered as completed only when user abort complete callback is executed (not when exiting function).
1380 * @retval HAL status
1381*/
1382HAL_StatusTypeDef HAL_SMARTCARD_AbortTransmit_IT(SMARTCARD_HandleTypeDef *hsc)
1383{
1384 /* Disable TXEIE and TCIE interrupts */
1385 CLEAR_BIT(hsc->Instance->CR1, (USART_CR1_TXEIE | USART_CR1_TCIE));
1386
1387 /* Disable the SMARTCARD DMA Tx request if enabled */
1388 if(HAL_IS_BIT_SET(hsc->Instance->CR3, USART_CR3_DMAT))
1389 {
1390 CLEAR_BIT(hsc->Instance->CR3, USART_CR3_DMAT);
1391
1392 /* Abort the SMARTCARD DMA Tx channel : use blocking DMA Abort API (no callback) */
1393 if(hsc->hdmatx != NULL)
1394 {
1395 /* Set the SMARTCARD DMA Abort callback :
1396 will lead to call HAL_SMARTCARD_AbortCpltCallback() at end of DMA abort procedure */
1397 hsc->hdmatx->XferAbortCallback = SMARTCARD_DMATxOnlyAbortCallback;
1398
1399 /* Abort DMA TX */
1400 if(HAL_DMA_Abort_IT(hsc->hdmatx) != HAL_OK)
1401 {
1402 /* Call Directly hsc->hdmatx->XferAbortCallback function in case of error */
1403 hsc->hdmatx->XferAbortCallback(hsc->hdmatx);
1404 }
1405 }
1406 else
1407 {
1408 /* Reset Tx transfer counter */
1409 hsc->TxXferCount = 0x00U;
1410
1411 /* Restore hsc->gState to Ready */
1412 hsc->gState = HAL_SMARTCARD_STATE_READY;
1413
1414 /* As no DMA to be aborted, call directly user Abort complete callback */
1415#if (USE_HAL_SMARTCARD_REGISTER_CALLBACKS == 1)
1416 /* Call registered Abort Transmit Complete Callback */
1417 hsc->AbortTransmitCpltCallback(hsc);
1418#else
1419 /* Call legacy weak Abort Transmit Complete Callback */
1420 HAL_SMARTCARD_AbortTransmitCpltCallback(hsc);
1421#endif /* USE_HAL_SMARTCARD_REGISTER_CALLBACK */
1422 }
1423 }
1424 else
1425 {
1426 /* Reset Tx transfer counter */
1427 hsc->TxXferCount = 0x00U;
1428
1429 /* Restore hsc->gState to Ready */
1430 hsc->gState = HAL_SMARTCARD_STATE_READY;
1431
1432 /* As no DMA to be aborted, call directly user Abort complete callback */
1433#if (USE_HAL_SMARTCARD_REGISTER_CALLBACKS == 1)
1434 /* Call registered Abort Transmit Complete Callback */
1435 hsc->AbortTransmitCpltCallback(hsc);
1436#else
1437 /* Call legacy weak Abort Transmit Complete Callback */
1438 HAL_SMARTCARD_AbortTransmitCpltCallback(hsc);
1439#endif /* USE_HAL_SMARTCARD_REGISTER_CALLBACK */
1440 }
1441
1442 return HAL_OK;
1443}
1444
1445/**
1446 * @brief Abort ongoing Receive transfer (Interrupt mode).
1447 * @param hsc SMARTCARD handle.
1448 * @note This procedure could be used for aborting any ongoing transfer started in Interrupt or DMA mode.
1449 * This procedure performs following operations :
1450 * - Disable SMARTCARD Interrupts (Rx)
1451 * - Disable the DMA transfer in the peripheral register (if enabled)
1452 * - Abort DMA transfer by calling HAL_DMA_Abort_IT (in case of transfer in DMA mode)
1453 * - Set handle State to READY
1454 * - At abort completion, call user abort complete callback
1455 * @note This procedure is executed in Interrupt mode, meaning that abort procedure could be
1456 * considered as completed only when user abort complete callback is executed (not when exiting function).
1457 * @retval HAL status
1458*/
1459HAL_StatusTypeDef HAL_SMARTCARD_AbortReceive_IT(SMARTCARD_HandleTypeDef *hsc)
1460{
1461 /* Disable RXNE, PE and ERR (Frame error, noise error, overrun error) interrupts */
1462 CLEAR_BIT(hsc->Instance->CR1, (USART_CR1_RXNEIE | USART_CR1_PEIE));
1463 CLEAR_BIT(hsc->Instance->CR3, USART_CR3_EIE);
1464
1465 /* Disable the SMARTCARD DMA Rx request if enabled */
1466 if(HAL_IS_BIT_SET(hsc->Instance->CR3, USART_CR3_DMAR))
1467 {
1468 CLEAR_BIT(hsc->Instance->CR3, USART_CR3_DMAR);
1469
1470 /* Abort the SMARTCARD DMA Rx channel : use blocking DMA Abort API (no callback) */
1471 if(hsc->hdmarx != NULL)
1472 {
1473 /* Set the SMARTCARD DMA Abort callback :
1474 will lead to call HAL_SMARTCARD_AbortCpltCallback() at end of DMA abort procedure */
1475 hsc->hdmarx->XferAbortCallback = SMARTCARD_DMARxOnlyAbortCallback;
1476
1477 /* Abort DMA RX */
1478 if(HAL_DMA_Abort_IT(hsc->hdmarx) != HAL_OK)
1479 {
1480 /* Call Directly hsc->hdmarx->XferAbortCallback function in case of error */
1481 hsc->hdmarx->XferAbortCallback(hsc->hdmarx);
1482 }
1483 }
1484 else
1485 {
1486 /* Reset Rx transfer counter */
1487 hsc->RxXferCount = 0x00U;
1488
1489 /* Restore hsc->RxState to Ready */
1490 hsc->RxState = HAL_SMARTCARD_STATE_READY;
1491
1492 /* As no DMA to be aborted, call directly user Abort complete callback */
1493#if (USE_HAL_SMARTCARD_REGISTER_CALLBACKS == 1)
1494 /* Call registered Abort Receive Complete Callback */
1495 hsc->AbortReceiveCpltCallback(hsc);
1496#else
1497 /* Call legacy weak Abort Receive Complete Callback */
1498 HAL_SMARTCARD_AbortReceiveCpltCallback(hsc);
1499#endif /* USE_HAL_SMARTCARD_REGISTER_CALLBACK */
1500 }
1501 }
1502 else
1503 {
1504 /* Reset Rx transfer counter */
1505 hsc->RxXferCount = 0x00U;
1506
1507 /* Restore hsc->RxState to Ready */
1508 hsc->RxState = HAL_SMARTCARD_STATE_READY;
1509
1510 /* As no DMA to be aborted, call directly user Abort complete callback */
1511#if (USE_HAL_SMARTCARD_REGISTER_CALLBACKS == 1)
1512 /* Call registered Abort Receive Complete Callback */
1513 hsc->AbortReceiveCpltCallback(hsc);
1514#else
1515 /* Call legacy weak Abort Receive Complete Callback */
1516 HAL_SMARTCARD_AbortReceiveCpltCallback(hsc);
1517#endif /* USE_HAL_SMARTCARD_REGISTER_CALLBACK */
1518 }
1519
1520 return HAL_OK;
1521}
1522
1523/**
1524 * @brief This function handles SMARTCARD interrupt request.
1525 * @param hsc Pointer to a SMARTCARD_HandleTypeDef structure that contains
1526 * the configuration information for SMARTCARD module.
1527 * @retval None
1528 */
1529void HAL_SMARTCARD_IRQHandler(SMARTCARD_HandleTypeDef *hsc)
1530{
1531 uint32_t isrflags = READ_REG(hsc->Instance->SR);
1532 uint32_t cr1its = READ_REG(hsc->Instance->CR1);
1533 uint32_t cr3its = READ_REG(hsc->Instance->CR3);
1534 uint32_t dmarequest = 0x00U;
1535 uint32_t errorflags = 0x00U;
1536
1537 /* If no error occurs */
1538 errorflags = (isrflags & (uint32_t)(USART_SR_PE | USART_SR_FE | USART_SR_ORE | USART_SR_NE));
1539 if(errorflags == RESET)
1540 {
1541 /* SMARTCARD in mode Receiver -------------------------------------------------*/
1542 if(((isrflags & USART_SR_RXNE) != RESET) && ((cr1its & USART_CR1_RXNEIE) != RESET))
1543 {
1544 SMARTCARD_Receive_IT(hsc);
1545 return;
1546 }
1547 }
1548
1549 /* If some errors occur */
1550 if((errorflags != RESET) && (((cr3its & USART_CR3_EIE) != RESET) || ((cr1its & (USART_CR1_RXNEIE | USART_CR1_PEIE)) != RESET)))
1551 {
1552 /* SMARTCARD parity error interrupt occurred ---------------------------*/
1553 if(((isrflags & SMARTCARD_FLAG_PE) != RESET) && ((cr1its & USART_CR1_PEIE) != RESET))
1554 {
1555 hsc->ErrorCode |= HAL_SMARTCARD_ERROR_PE;
1556 }
1557
1558 /* SMARTCARD frame error interrupt occurred ----------------------------*/
1559 if(((isrflags & SMARTCARD_FLAG_FE) != RESET) && ((cr3its & USART_CR3_EIE) != RESET))
1560 {
1561 hsc->ErrorCode |= HAL_SMARTCARD_ERROR_FE;
1562 }
1563
1564 /* SMARTCARD noise error interrupt occurred ----------------------------*/
1565 if(((isrflags & SMARTCARD_FLAG_NE) != RESET) && ((cr3its & USART_CR3_EIE) != RESET))
1566 {
1567 hsc->ErrorCode |= HAL_SMARTCARD_ERROR_NE;
1568 }
1569
1570 /* SMARTCARD Over-Run interrupt occurred -------------------------------*/
1571 if(((isrflags & SMARTCARD_FLAG_ORE) != RESET) && (((cr1its & USART_CR1_RXNEIE) != RESET) || ((cr3its & USART_CR3_EIE) != RESET)))
1572 {
1573 hsc->ErrorCode |= HAL_SMARTCARD_ERROR_ORE;
1574 }
1575 /* Call the Error call Back in case of Errors --------------------------*/
1576 if(hsc->ErrorCode != HAL_SMARTCARD_ERROR_NONE)
1577 {
1578 /* SMARTCARD in mode Receiver ----------------------------------------*/
1579 if(((isrflags & USART_SR_RXNE) != RESET) && ((cr1its & USART_CR1_RXNEIE) != RESET))
1580 {
1581 SMARTCARD_Receive_IT(hsc);
1582 }
1583
1584 /* If Overrun error occurs, or if any error occurs in DMA mode reception,
1585 consider error as blocking */
1586 dmarequest = HAL_IS_BIT_SET(hsc->Instance->CR3, USART_CR3_DMAR);
1587 if(((hsc->ErrorCode & HAL_SMARTCARD_ERROR_ORE) != RESET) || dmarequest)
1588 {
1589 /* Blocking error : transfer is aborted
1590 Set the SMARTCARD state ready to be able to start again the process,
1591 Disable Rx Interrupts, and disable Rx DMA request, if ongoing */
1592 SMARTCARD_EndRxTransfer(hsc);
1593 /* Disable the SMARTCARD DMA Rx request if enabled */
1594 if(HAL_IS_BIT_SET(hsc->Instance->CR3, USART_CR3_DMAR))
1595 {
1596 CLEAR_BIT(hsc->Instance->CR3, USART_CR3_DMAR);
1597
1598 /* Abort the SMARTCARD DMA Rx channel */
1599 if(hsc->hdmarx != NULL)
1600 {
1601 /* Set the SMARTCARD DMA Abort callback :
1602 will lead to call HAL_SMARTCARD_ErrorCallback() at end of DMA abort procedure */
1603 hsc->hdmarx->XferAbortCallback = SMARTCARD_DMAAbortOnError;
1604
1605 if(HAL_DMA_Abort_IT(hsc->hdmarx) != HAL_OK)
1606 {
1607 /* Call Directly XferAbortCallback function in case of error */
1608 hsc->hdmarx->XferAbortCallback(hsc->hdmarx);
1609 }
1610 }
1611 else
1612 {
1613#if (USE_HAL_SMARTCARD_REGISTER_CALLBACKS == 1)
1614 /* Call registered user error callback */
1615 hsc->ErrorCallback(hsc);
1616#else
1617 /* Call legacy weak user error callback */
1618 HAL_SMARTCARD_ErrorCallback(hsc);
1619#endif /* USE_HAL_SMARTCARD_REGISTER_CALLBACK */
1620 }
1621 }
1622 else
1623 {
1624#if (USE_HAL_SMARTCARD_REGISTER_CALLBACKS == 1)
1625 /* Call registered user error callback */
1626 hsc->ErrorCallback(hsc);
1627#else
1628 /* Call legacy weak user error callback */
1629 HAL_SMARTCARD_ErrorCallback(hsc);
1630#endif /* USE_HAL_SMARTCARD_REGISTER_CALLBACK */
1631 }
1632 }
1633 else
1634 {
1635 /* Non Blocking error : transfer could go on.
1636 Error is notified to user through user error callback */
1637#if (USE_HAL_SMARTCARD_REGISTER_CALLBACKS == 1)
1638 /* Call registered user error callback */
1639 hsc->ErrorCallback(hsc);
1640#else
1641 /* Call legacy weak user error callback */
1642 HAL_SMARTCARD_ErrorCallback(hsc);
1643#endif /* USE_HAL_SMARTCARD_REGISTER_CALLBACK */
1644 hsc->ErrorCode = HAL_SMARTCARD_ERROR_NONE;
1645 }
1646 }
1647 return;
1648 } /* End if some error occurs */
1649
1650 /* SMARTCARD in mode Transmitter ------------------------------------------*/
1651 if(((isrflags & SMARTCARD_FLAG_TXE) != RESET) && ((cr1its & USART_CR1_TXEIE) != RESET))
1652 {
1653 SMARTCARD_Transmit_IT(hsc);
1654 return;
1655 }
1656
1657 /* SMARTCARD in mode Transmitter (transmission end) -----------------------*/
1658 if(((isrflags & SMARTCARD_FLAG_TC) != RESET) && ((cr1its & USART_CR1_TCIE) != RESET))
1659 {
1660 SMARTCARD_EndTransmit_IT(hsc);
1661 return;
1662 }
1663}
1664
1665/**
1666 * @brief Tx Transfer completed callbacks
1667 * @param hsc Pointer to a SMARTCARD_HandleTypeDef structure that contains
1668 * the configuration information for SMARTCARD module.
1669 * @retval None
1670 */
1671__weak void HAL_SMARTCARD_TxCpltCallback(SMARTCARD_HandleTypeDef *hsc)
1672{
1673 /* Prevent unused argument(s) compilation warning */
1674 UNUSED(hsc);
1675
1676 /* NOTE : This function should not be modified, when the callback is needed,
1677 the HAL_SMARTCARD_TxCpltCallback can be implemented in the user file.
1678 */
1679}
1680
1681/**
1682 * @brief Rx Transfer completed callback
1683 * @param hsc Pointer to a SMARTCARD_HandleTypeDef structure that contains
1684 * the configuration information for SMARTCARD module.
1685 * @retval None
1686 */
1687__weak void HAL_SMARTCARD_RxCpltCallback(SMARTCARD_HandleTypeDef *hsc)
1688{
1689 /* Prevent unused argument(s) compilation warning */
1690 UNUSED(hsc);
1691
1692 /* NOTE : This function should not be modified, when the callback is needed,
1693 the HAL_SMARTCARD_RxCpltCallback can be implemented in the user file.
1694 */
1695}
1696
1697/**
1698 * @brief SMARTCARD error callback
1699 * @param hsc Pointer to a SMARTCARD_HandleTypeDef structure that contains
1700 * the configuration information for SMARTCARD module.
1701 * @retval None
1702 */
1703__weak void HAL_SMARTCARD_ErrorCallback(SMARTCARD_HandleTypeDef *hsc)
1704{
1705 /* Prevent unused argument(s) compilation warning */
1706 UNUSED(hsc);
1707
1708 /* NOTE : This function should not be modified, when the callback is needed,
1709 the HAL_SMARTCARD_ErrorCallback can be implemented in the user file.
1710 */
1711}
1712
1713/**
1714 * @brief SMARTCARD Abort Complete callback.
1715 * @param hsc SMARTCARD handle.
1716 * @retval None
1717 */
1718__weak void HAL_SMARTCARD_AbortCpltCallback (SMARTCARD_HandleTypeDef *hsc)
1719{
1720 /* Prevent unused argument(s) compilation warning */
1721 UNUSED(hsc);
1722
1723 /* NOTE : This function should not be modified, when the callback is needed,
1724 the HAL_SMARTCARD_AbortCpltCallback can be implemented in the user file.
1725 */
1726}
1727
1728/**
1729 * @brief SMARTCARD Abort Transmit Complete callback.
1730 * @param hsc SMARTCARD handle.
1731 * @retval None
1732 */
1733__weak void HAL_SMARTCARD_AbortTransmitCpltCallback (SMARTCARD_HandleTypeDef *hsc)
1734{
1735 /* Prevent unused argument(s) compilation warning */
1736 UNUSED(hsc);
1737
1738 /* NOTE : This function should not be modified, when the callback is needed,
1739 the HAL_SMARTCARD_AbortTransmitCpltCallback can be implemented in the user file.
1740 */
1741}
1742
1743/**
1744 * @brief SMARTCARD Abort Receive Complete callback.
1745 * @param hsc SMARTCARD handle.
1746 * @retval None
1747 */
1748__weak void HAL_SMARTCARD_AbortReceiveCpltCallback (SMARTCARD_HandleTypeDef *hsc)
1749{
1750 /* Prevent unused argument(s) compilation warning */
1751 UNUSED(hsc);
1752
1753 /* NOTE : This function should not be modified, when the callback is needed,
1754 the HAL_SMARTCARD_AbortReceiveCpltCallback can be implemented in the user file.
1755 */
1756}
1757
1758/**
1759 * @}
1760 */
1761
1762/** @defgroup SMARTCARD_Exported_Functions_Group3 Peripheral State and Errors functions
1763 * @brief SMARTCARD State and Errors functions
1764 *
1765@verbatim
1766 ===============================================================================
1767 ##### Peripheral State and Errors functions #####
1768 ===============================================================================
1769 [..]
1770 This subsection provides a set of functions allowing to control the SmartCard.
1771 (+) HAL_SMARTCARD_GetState() API can be helpful to check in run-time the state of the SmartCard peripheral.
1772 (+) HAL_SMARTCARD_GetError() check in run-time errors that could be occurred during communication.
1773@endverbatim
1774 * @{
1775 */
1776
1777/**
1778 * @brief Return the SMARTCARD handle state
1779 * @param hsc Pointer to a SMARTCARD_HandleTypeDef structure that contains
1780 * the configuration information for SMARTCARD module.
1781 * @retval HAL state
1782 */
1783HAL_SMARTCARD_StateTypeDef HAL_SMARTCARD_GetState(SMARTCARD_HandleTypeDef *hsc)
1784{
1785 uint32_t temp1= 0x00U, temp2 = 0x00U;
1786 temp1 = hsc->gState;
1787 temp2 = hsc->RxState;
1788
1789 return (HAL_SMARTCARD_StateTypeDef)(temp1 | temp2);
1790}
1791
1792/**
1793 * @brief Return the SMARTCARD error code
1794 * @param hsc Pointer to a SMARTCARD_HandleTypeDef structure that contains
1795 * the configuration information for the specified SMARTCARD.
1796 * @retval SMARTCARD Error Code
1797 */
1798uint32_t HAL_SMARTCARD_GetError(SMARTCARD_HandleTypeDef *hsc)
1799{
1800 return hsc->ErrorCode;
1801}
1802
1803/**
1804 * @}
1805 */
1806
1807/**
1808 * @}
1809 */
1810
1811/** @defgroup SMARTCARD_Private_Functions SMARTCARD Private Functions
1812 * @{
1813 */
1814
1815#if (USE_HAL_SMARTCARD_REGISTER_CALLBACKS == 1)
1816/**
1817 * @brief Initialize the callbacks to their default values.
1818 * @param hsc SMARTCARD handle.
1819 * @retval none
1820 */
1821void SMARTCARD_InitCallbacksToDefault(SMARTCARD_HandleTypeDef *hsc)
1822{
1823 /* Init the SMARTCARD Callback settings */
1824 hsc->TxCpltCallback = HAL_SMARTCARD_TxCpltCallback; /* Legacy weak TxCpltCallback */
1825 hsc->RxCpltCallback = HAL_SMARTCARD_RxCpltCallback; /* Legacy weak RxCpltCallback */
1826 hsc->ErrorCallback = HAL_SMARTCARD_ErrorCallback; /* Legacy weak ErrorCallback */
1827 hsc->AbortCpltCallback = HAL_SMARTCARD_AbortCpltCallback; /* Legacy weak AbortCpltCallback */
1828 hsc->AbortTransmitCpltCallback = HAL_SMARTCARD_AbortTransmitCpltCallback; /* Legacy weak AbortTransmitCpltCallback */
1829 hsc->AbortReceiveCpltCallback = HAL_SMARTCARD_AbortReceiveCpltCallback; /* Legacy weak AbortReceiveCpltCallback */
1830
1831}
1832#endif /* USE_HAL_SMARTCARD_REGISTER_CALLBACKS */
1833
1834/**
1835 * @brief DMA SMARTCARD transmit process complete callback
1836 * @param hdma Pointer to a DMA_HandleTypeDef structure that contains
1837 * the configuration information for the specified DMA module.
1838 * @retval None
1839 */
1840static void SMARTCARD_DMATransmitCplt(DMA_HandleTypeDef *hdma)
1841{
1842 SMARTCARD_HandleTypeDef* hsc = ( SMARTCARD_HandleTypeDef* )((DMA_HandleTypeDef* )hdma)->Parent;
1843
1844 hsc->TxXferCount = 0U;
1845
1846 /* Disable the DMA transfer for transmit request by setting the DMAT bit
1847 in the USART CR3 register */
1848 CLEAR_BIT(hsc->Instance->CR3, USART_CR3_DMAT);
1849
1850 /* Enable the SMARTCARD Transmit Complete Interrupt */
1851 SET_BIT(hsc->Instance->CR1, USART_CR1_TCIE);
1852}
1853
1854/**
1855 * @brief DMA SMARTCARD receive process complete callback
1856 * @param hdma Pointer to a DMA_HandleTypeDef structure that contains
1857 * the configuration information for the specified DMA module.
1858 * @retval None
1859 */
1860static void SMARTCARD_DMAReceiveCplt(DMA_HandleTypeDef *hdma)
1861{
1862 SMARTCARD_HandleTypeDef* hsc = ( SMARTCARD_HandleTypeDef* )((DMA_HandleTypeDef* )hdma)->Parent;
1863
1864 hsc->RxXferCount = 0U;
1865
1866 /* Disable RXNE, PE and ERR (Frame error, noise error, overrun error) interrupts */
1867 CLEAR_BIT(hsc->Instance->CR1, (USART_CR1_RXNEIE | USART_CR1_PEIE));
1868 CLEAR_BIT(hsc->Instance->CR3, USART_CR3_EIE);
1869
1870 /* Disable the DMA transfer for the receiver request by setting the DMAR bit
1871 in the USART CR3 register */
1872 CLEAR_BIT(hsc->Instance->CR3, USART_CR3_DMAR);
1873
1874 /* At end of Rx process, restore hsc->RxState to Ready */
1875 hsc->RxState = HAL_SMARTCARD_STATE_READY;
1876
1877#if (USE_HAL_SMARTCARD_REGISTER_CALLBACKS == 1)
1878 /* Call registered Rx complete callback */
1879 hsc->RxCpltCallback(hsc);
1880#else
1881 /* Call legacy weak Rx complete callback */
1882 HAL_SMARTCARD_RxCpltCallback(hsc);
1883#endif /* USE_HAL_SMARTCARD_REGISTER_CALLBACK */
1884}
1885
1886/**
1887 * @brief DMA SMARTCARD communication error callback
1888 * @param hdma Pointer to a DMA_HandleTypeDef structure that contains
1889 * the configuration information for the specified DMA module.
1890 * @retval None
1891 */
1892static void SMARTCARD_DMAError(DMA_HandleTypeDef *hdma)
1893{
1894 uint32_t dmarequest = 0x00U;
1895 SMARTCARD_HandleTypeDef* hsc = ( SMARTCARD_HandleTypeDef* )((DMA_HandleTypeDef* )hdma)->Parent;
1896 hsc->RxXferCount = 0U;
1897 hsc->TxXferCount = 0U;
1898 hsc->ErrorCode = HAL_SMARTCARD_ERROR_DMA;
1899
1900 /* Stop SMARTCARD DMA Tx request if ongoing */
1901 dmarequest = HAL_IS_BIT_SET(hsc->Instance->CR3, USART_CR3_DMAT);
1902 if((hsc->gState == HAL_SMARTCARD_STATE_BUSY_TX) && dmarequest)
1903 {
1904 SMARTCARD_EndTxTransfer(hsc);
1905 }
1906
1907 /* Stop SMARTCARD DMA Rx request if ongoing */
1908 dmarequest = HAL_IS_BIT_SET(hsc->Instance->CR3, USART_CR3_DMAR);
1909 if((hsc->RxState == HAL_SMARTCARD_STATE_BUSY_RX) && dmarequest)
1910 {
1911 SMARTCARD_EndRxTransfer(hsc);
1912 }
1913
1914#if (USE_HAL_SMARTCARD_REGISTER_CALLBACKS == 1)
1915 /* Call registered user error callback */
1916 hsc->ErrorCallback(hsc);
1917#else
1918 /* Call legacy weak user error callback */
1919 HAL_SMARTCARD_ErrorCallback(hsc);
1920#endif /* USE_HAL_SMARTCARD_REGISTER_CALLBACK */
1921}
1922
1923/**
1924 * @brief This function handles SMARTCARD Communication Timeout.
1925 * @param hsc Pointer to a SMARTCARD_HandleTypeDef structure that contains
1926 * the configuration information for SMARTCARD module.
1927 * @param Flag Specifies the SMARTCARD flag to check.
1928 * @param Status The new Flag status (SET or RESET).
1929 * @param Timeout Timeout duration
1930 * @param Tickstart Tick start value
1931 * @retval HAL status
1932 */
1933static HAL_StatusTypeDef SMARTCARD_WaitOnFlagUntilTimeout(SMARTCARD_HandleTypeDef *hsc, uint32_t Flag, FlagStatus Status, uint32_t Tickstart, uint32_t Timeout)
1934{
1935 /* Wait until flag is set */
1936 while((__HAL_SMARTCARD_GET_FLAG(hsc, Flag) ? SET : RESET) == Status)
1937 {
1938 /* Check for the Timeout */
1939 if(Timeout != HAL_MAX_DELAY)
1940 {
1941 if((Timeout == 0U)||((HAL_GetTick() - Tickstart ) > Timeout))
1942 {
1943 /* Disable TXE and RXNE interrupts for the interrupt process */
1944 CLEAR_BIT(hsc->Instance->CR1, USART_CR1_TXEIE);
1945 CLEAR_BIT(hsc->Instance->CR1, USART_CR1_RXNEIE);
1946
1947 hsc->gState= HAL_SMARTCARD_STATE_READY;
1948 hsc->RxState= HAL_SMARTCARD_STATE_READY;
1949
1950 /* Process Unlocked */
1951 __HAL_UNLOCK(hsc);
1952
1953 return HAL_TIMEOUT;
1954 }
1955 }
1956 }
1957 return HAL_OK;
1958}
1959
1960/**
1961 * @brief End ongoing Tx transfer on SMARTCARD peripheral (following error detection or Transmit completion).
1962 * @param hsc Pointer to a SMARTCARD_HandleTypeDef structure that contains
1963 * the configuration information for SMARTCARD module.
1964 * @retval None
1965 */
1966static void SMARTCARD_EndTxTransfer(SMARTCARD_HandleTypeDef *hsc)
1967{
1968 /* At end of Tx process, restore hsc->gState to Ready */
1969 hsc->gState = HAL_SMARTCARD_STATE_READY;
1970
1971 /* Disable TXEIE and TCIE interrupts */
1972 CLEAR_BIT(hsc->Instance->CR1, (USART_CR1_TXEIE | USART_CR1_TCIE));
1973}
1974
1975
1976/**
1977 * @brief End ongoing Rx transfer on SMARTCARD peripheral (following error detection or Reception completion).
1978 * @param hsc Pointer to a SMARTCARD_HandleTypeDef structure that contains
1979 * the configuration information for SMARTCARD module.
1980 * @retval None
1981 */
1982static void SMARTCARD_EndRxTransfer(SMARTCARD_HandleTypeDef *hsc)
1983{
1984 /* At end of Rx process, restore hsc->RxState to Ready */
1985 hsc->RxState = HAL_SMARTCARD_STATE_READY;
1986
1987 /* Disable RXNE, PE and ERR (Frame error, noise error, overrun error) interrupts */
1988 CLEAR_BIT(hsc->Instance->CR1, (USART_CR1_RXNEIE | USART_CR1_PEIE));
1989 CLEAR_BIT(hsc->Instance->CR3, USART_CR3_EIE);
1990}
1991
1992/**
1993 * @brief Send an amount of data in non blocking mode
1994 * @param hsc Pointer to a SMARTCARD_HandleTypeDef structure that contains
1995 * the configuration information for SMARTCARD module.
1996 * @retval HAL status
1997 */
1998static HAL_StatusTypeDef SMARTCARD_Transmit_IT(SMARTCARD_HandleTypeDef *hsc)
1999{
2000
2001 /* Check that a Tx process is ongoing */
2002 if(hsc->gState == HAL_SMARTCARD_STATE_BUSY_TX)
2003 {
2004 hsc->Instance->DR = (uint8_t)(*hsc->pTxBuffPtr & 0xFFU);
2005 hsc->pTxBuffPtr++;
2006
2007 if(--hsc->TxXferCount == 0U)
2008 {
2009 /* Disable the SMARTCARD Transmit data register empty Interrupt */
2010 CLEAR_BIT(hsc->Instance->CR1, USART_CR1_TXEIE);
2011
2012 /* Enable the SMARTCARD Transmit Complete Interrupt */
2013 SET_BIT(hsc->Instance->CR1, USART_CR1_TCIE);
2014 }
2015
2016 return HAL_OK;
2017 }
2018 else
2019 {
2020 return HAL_BUSY;
2021 }
2022}
2023
2024/**
2025 * @brief Wraps up transmission in non blocking mode.
2026 * @param hsc Pointer to a SMARTCARD_HandleTypeDef structure that contains
2027 * the configuration information for the specified SMARTCARD module.
2028 * @retval HAL status
2029 */
2030static HAL_StatusTypeDef SMARTCARD_EndTransmit_IT(SMARTCARD_HandleTypeDef *hsc)
2031{
2032 /* Disable the SMARTCARD Transmit Complete Interrupt */
2033 CLEAR_BIT(hsc->Instance->CR1, USART_CR1_TCIE);
2034
2035 /* Disable the SMARTCARD Error Interrupt: (Frame error, noise error, overrun error) */
2036 CLEAR_BIT(hsc->Instance->CR3, USART_CR3_EIE);
2037
2038 /* Tx process is ended, restore hsc->gState to Ready */
2039 hsc->gState = HAL_SMARTCARD_STATE_READY;
2040
2041#if (USE_HAL_SMARTCARD_REGISTER_CALLBACKS == 1)
2042 /* Call registered Tx complete callback */
2043 hsc->TxCpltCallback(hsc);
2044#else
2045 /* Call legacy weak Tx complete callback */
2046 HAL_SMARTCARD_TxCpltCallback(hsc);
2047#endif /* USE_HAL_SMARTCARD_REGISTER_CALLBACK */
2048
2049 return HAL_OK;
2050}
2051
2052/**
2053 * @brief Receive an amount of data in non blocking mode
2054 * @param hsc Pointer to a SMARTCARD_HandleTypeDef structure that contains
2055 * the configuration information for SMARTCARD module.
2056 * @retval HAL status
2057 */
2058static HAL_StatusTypeDef SMARTCARD_Receive_IT(SMARTCARD_HandleTypeDef *hsc)
2059{
2060
2061 /* Check that a Rx process is ongoing */
2062 if(hsc->RxState == HAL_SMARTCARD_STATE_BUSY_RX)
2063 {
2064 *hsc->pRxBuffPtr = (uint8_t)(hsc->Instance->DR & (uint8_t)0xFFU);
2065 hsc->pRxBuffPtr++;
2066
2067 if(--hsc->RxXferCount == 0U)
2068 {
2069 CLEAR_BIT(hsc->Instance->CR1, USART_CR1_RXNEIE);
2070
2071 /* Disable the SMARTCARD Parity Error Interrupt */
2072 CLEAR_BIT(hsc->Instance->CR1, USART_CR1_PEIE);
2073
2074 /* Disable the SMARTCARD Error Interrupt: (Frame error, noise error, overrun error) */
2075 CLEAR_BIT(hsc->Instance->CR3, USART_CR3_EIE);
2076
2077 /* Rx process is completed, restore hsc->RxState to Ready */
2078 hsc->RxState = HAL_SMARTCARD_STATE_READY;
2079
2080#if (USE_HAL_SMARTCARD_REGISTER_CALLBACKS == 1)
2081 /* Call registered Rx complete callback */
2082 hsc->RxCpltCallback(hsc);
2083#else
2084 /* Call legacy weak Rx complete callback */
2085 HAL_SMARTCARD_RxCpltCallback(hsc);
2086#endif /* USE_HAL_SMARTCARD_REGISTER_CALLBACK */
2087
2088 return HAL_OK;
2089 }
2090 return HAL_OK;
2091 }
2092 else
2093 {
2094 return HAL_BUSY;
2095 }
2096}
2097
2098/**
2099 * @brief DMA SMARTCARD communication abort callback, when initiated by HAL services on Error
2100 * (To be called at end of DMA Abort procedure following error occurrence).
2101 * @param hdma DMA handle.
2102 * @retval None
2103 */
2104static void SMARTCARD_DMAAbortOnError(DMA_HandleTypeDef *hdma)
2105{
2106 SMARTCARD_HandleTypeDef* hsc = (SMARTCARD_HandleTypeDef* )((DMA_HandleTypeDef* )hdma)->Parent;
2107 hsc->RxXferCount = 0x00U;
2108 hsc->TxXferCount = 0x00U;
2109
2110#if (USE_HAL_SMARTCARD_REGISTER_CALLBACKS == 1)
2111 /* Call registered user error callback */
2112 hsc->ErrorCallback(hsc);
2113#else
2114 /* Call legacy weak user error callback */
2115 HAL_SMARTCARD_ErrorCallback(hsc);
2116#endif /* USE_HAL_SMARTCARD_REGISTER_CALLBACK */
2117}
2118
2119/**
2120 * @brief DMA SMARTCARD Tx communication abort callback, when initiated by user
2121 * (To be called at end of DMA Tx Abort procedure following user abort request).
2122 * @note When this callback is executed, User Abort complete call back is called only if no
2123 * Abort still ongoing for Rx DMA Handle.
2124 * @param hdma DMA handle.
2125 * @retval None
2126 */
2127static void SMARTCARD_DMATxAbortCallback(DMA_HandleTypeDef *hdma)
2128{
2129 SMARTCARD_HandleTypeDef* hsc = ( SMARTCARD_HandleTypeDef* )((DMA_HandleTypeDef* )hdma)->Parent;
2130
2131 hsc->hdmatx->XferAbortCallback = NULL;
2132
2133 /* Check if an Abort process is still ongoing */
2134 if(hsc->hdmarx != NULL)
2135 {
2136 if(hsc->hdmarx->XferAbortCallback != NULL)
2137 {
2138 return;
2139 }
2140 }
2141
2142 /* No Abort process still ongoing : All DMA channels are aborted, call user Abort Complete callback */
2143 hsc->TxXferCount = 0x00U;
2144 hsc->RxXferCount = 0x00U;
2145
2146 /* Reset ErrorCode */
2147 hsc->ErrorCode = HAL_SMARTCARD_ERROR_NONE;
2148
2149 /* Restore hsc->gState and hsc->RxState to Ready */
2150 hsc->gState = HAL_SMARTCARD_STATE_READY;
2151 hsc->RxState = HAL_SMARTCARD_STATE_READY;
2152
2153#if (USE_HAL_SMARTCARD_REGISTER_CALLBACKS == 1)
2154 /* Call registered Abort complete callback */
2155 hsc->AbortCpltCallback(hsc);
2156#else
2157 /* Call legacy weak Abort complete callback */
2158 HAL_SMARTCARD_AbortCpltCallback(hsc);
2159#endif /* USE_HAL_SMARTCARD_REGISTER_CALLBACK */
2160}
2161
2162/**
2163 * @brief DMA SMARTCARD Rx communication abort callback, when initiated by user
2164 * (To be called at end of DMA Rx Abort procedure following user abort request).
2165 * @note When this callback is executed, User Abort complete call back is called only if no
2166 * Abort still ongoing for Tx DMA Handle.
2167 * @param hdma DMA handle.
2168 * @retval None
2169 */
2170static void SMARTCARD_DMARxAbortCallback(DMA_HandleTypeDef *hdma)
2171{
2172 SMARTCARD_HandleTypeDef* hsc = ( SMARTCARD_HandleTypeDef* )((DMA_HandleTypeDef* )hdma)->Parent;
2173
2174 hsc->hdmarx->XferAbortCallback = NULL;
2175
2176 /* Check if an Abort process is still ongoing */
2177 if(hsc->hdmatx != NULL)
2178 {
2179 if(hsc->hdmatx->XferAbortCallback != NULL)
2180 {
2181 return;
2182 }
2183 }
2184
2185 /* No Abort process still ongoing : All DMA channels are aborted, call user Abort Complete callback */
2186 hsc->TxXferCount = 0x00U;
2187 hsc->RxXferCount = 0x00U;
2188
2189 /* Reset ErrorCode */
2190 hsc->ErrorCode = HAL_SMARTCARD_ERROR_NONE;
2191
2192 /* Restore hsc->gState and hsc->RxState to Ready */
2193 hsc->gState = HAL_SMARTCARD_STATE_READY;
2194 hsc->RxState = HAL_SMARTCARD_STATE_READY;
2195
2196#if (USE_HAL_SMARTCARD_REGISTER_CALLBACKS == 1)
2197 /* Call registered Abort complete callback */
2198 hsc->AbortCpltCallback(hsc);
2199#else
2200 /* Call legacy weak Abort complete callback */
2201 HAL_SMARTCARD_AbortCpltCallback(hsc);
2202#endif /* USE_HAL_SMARTCARD_REGISTER_CALLBACK */
2203}
2204
2205/**
2206 * @brief DMA SMARTCARD Tx communication abort callback, when initiated by user by a call to
2207 * HAL_SMARTCARD_AbortTransmit_IT API (Abort only Tx transfer)
2208 * (This callback is executed at end of DMA Tx Abort procedure following user abort request,
2209 * and leads to user Tx Abort Complete callback execution).
2210 * @param hdma DMA handle.
2211 * @retval None
2212 */
2213static void SMARTCARD_DMATxOnlyAbortCallback(DMA_HandleTypeDef *hdma)
2214{
2215 SMARTCARD_HandleTypeDef* hsc = ( SMARTCARD_HandleTypeDef* )((DMA_HandleTypeDef* )hdma)->Parent;
2216
2217 hsc->TxXferCount = 0x00U;
2218
2219 /* Restore hsc->gState to Ready */
2220 hsc->gState = HAL_SMARTCARD_STATE_READY;
2221
2222#if (USE_HAL_SMARTCARD_REGISTER_CALLBACKS == 1)
2223 /* Call registered Abort Transmit Complete Callback */
2224 hsc->AbortTransmitCpltCallback(hsc);
2225#else
2226 /* Call legacy weak Abort Transmit Complete Callback */
2227 HAL_SMARTCARD_AbortTransmitCpltCallback(hsc);
2228#endif /* USE_HAL_SMARTCARD_REGISTER_CALLBACK */
2229}
2230
2231/**
2232 * @brief DMA SMARTCARD Rx communication abort callback, when initiated by user by a call to
2233 * HAL_SMARTCARD_AbortReceive_IT API (Abort only Rx transfer)
2234 * (This callback is executed at end of DMA Rx Abort procedure following user abort request,
2235 * and leads to user Rx Abort Complete callback execution).
2236 * @param hdma DMA handle.
2237 * @retval None
2238 */
2239static void SMARTCARD_DMARxOnlyAbortCallback(DMA_HandleTypeDef *hdma)
2240{
2241 SMARTCARD_HandleTypeDef* hsc = ( SMARTCARD_HandleTypeDef* )((DMA_HandleTypeDef* )hdma)->Parent;
2242
2243 hsc->RxXferCount = 0x00U;
2244
2245 /* Restore hsc->RxState to Ready */
2246 hsc->RxState = HAL_SMARTCARD_STATE_READY;
2247
2248#if (USE_HAL_SMARTCARD_REGISTER_CALLBACKS == 1)
2249 /* Call registered Abort Receive Complete Callback */
2250 hsc->AbortReceiveCpltCallback(hsc);
2251#else
2252 /* Call legacy weak Abort Receive Complete Callback */
2253 HAL_SMARTCARD_AbortReceiveCpltCallback(hsc);
2254#endif /* USE_HAL_SMARTCARD_REGISTER_CALLBACK */
2255}
2256
2257/**
2258 * @brief Configure the SMARTCARD peripheral
2259 * @param hsc Pointer to a SMARTCARD_HandleTypeDef structure that contains
2260 * the configuration information for SMARTCARD module.
2261 * @retval None
2262 */
2263static void SMARTCARD_SetConfig(SMARTCARD_HandleTypeDef *hsc)
2264{
2265 uint32_t tmpreg = 0x00U;
2266 uint32_t pclk;
2267
2268 /* Check the parameters */
2269 assert_param(IS_SMARTCARD_INSTANCE(hsc->Instance));
2270 assert_param(IS_SMARTCARD_POLARITY(hsc->Init.CLKPolarity));
2271 assert_param(IS_SMARTCARD_PHASE(hsc->Init.CLKPhase));
2272 assert_param(IS_SMARTCARD_LASTBIT(hsc->Init.CLKLastBit));
2273 assert_param(IS_SMARTCARD_BAUDRATE(hsc->Init.BaudRate));
2274 assert_param(IS_SMARTCARD_WORD_LENGTH(hsc->Init.WordLength));
2275 assert_param(IS_SMARTCARD_STOPBITS(hsc->Init.StopBits));
2276 assert_param(IS_SMARTCARD_PARITY(hsc->Init.Parity));
2277 assert_param(IS_SMARTCARD_MODE(hsc->Init.Mode));
2278 assert_param(IS_SMARTCARD_NACK_STATE(hsc->Init.NACKState));
2279
2280 /* The LBCL, CPOL and CPHA bits have to be selected when both the transmitter and the
2281 receiver are disabled (TE=RE=0) to ensure that the clock pulses function correctly. */
2282 CLEAR_BIT(hsc->Instance->CR1, (USART_CR1_TE | USART_CR1_RE));
2283
2284 /*---------------------------- USART CR2 Configuration ---------------------*/
2285 tmpreg = hsc->Instance->CR2;
2286 /* Clear CLKEN, CPOL, CPHA and LBCL bits */
2287 tmpreg &= (uint32_t)~((uint32_t)(USART_CR2_CPHA | USART_CR2_CPOL | USART_CR2_CLKEN | USART_CR2_LBCL));
2288 /* Configure the SMARTCARD Clock, CPOL, CPHA and LastBit -----------------------*/
2289 /* Set CPOL bit according to hsc->Init.CLKPolarity value */
2290 /* Set CPHA bit according to hsc->Init.CLKPhase value */
2291 /* Set LBCL bit according to hsc->Init.CLKLastBit value */
2292 /* Set Stop Bits: Set STOP[13:12] bits according to hsc->Init.StopBits value */
2293 tmpreg |= (uint32_t)(USART_CR2_CLKEN | hsc->Init.CLKPolarity |
2294 hsc->Init.CLKPhase| hsc->Init.CLKLastBit | hsc->Init.StopBits);
2295 /* Write to USART CR2 */
2296 WRITE_REG(hsc->Instance->CR2, (uint32_t)tmpreg);
2297
2298 tmpreg = hsc->Instance->CR2;
2299
2300 /* Clear STOP[13:12] bits */
2301 tmpreg &= (uint32_t)~((uint32_t)USART_CR2_STOP);
2302
2303 /* Set Stop Bits: Set STOP[13:12] bits according to hsc->Init.StopBits value */
2304 tmpreg |= (uint32_t)(hsc->Init.StopBits);
2305
2306 /* Write to USART CR2 */
2307 WRITE_REG(hsc->Instance->CR2, (uint32_t)tmpreg);
2308
2309 /*-------------------------- USART CR1 Configuration -----------------------*/
2310 tmpreg = hsc->Instance->CR1;
2311
2312 /* Clear M, PCE, PS, TE and RE bits */
2313 tmpreg &= (uint32_t)~((uint32_t)(USART_CR1_M | USART_CR1_PCE | USART_CR1_PS | USART_CR1_TE | \
2314 USART_CR1_RE));
2315
2316 /* Configure the SMARTCARD Word Length, Parity and mode:
2317 Set the M bits according to hsc->Init.WordLength value
2318 Set PCE and PS bits according to hsc->Init.Parity value
2319 Set TE and RE bits according to hsc->Init.Mode value */
2320 tmpreg |= (uint32_t)hsc->Init.WordLength | hsc->Init.Parity | hsc->Init.Mode;
2321
2322 /* Write to USART CR1 */
2323 WRITE_REG(hsc->Instance->CR1, (uint32_t)tmpreg);
2324
2325 /*-------------------------- USART CR3 Configuration -----------------------*/
2326 /* Clear CTSE and RTSE bits */
2327 CLEAR_BIT(hsc->Instance->CR3, (USART_CR3_RTSE | USART_CR3_CTSE));
2328
2329 /*-------------------------- USART BRR Configuration -----------------------*/
2330#if defined(USART6)
2331 if((hsc->Instance == USART1) || (hsc->Instance == USART6))
2332 {
2333 pclk = HAL_RCC_GetPCLK2Freq();
2334 hsc->Instance->BRR = SMARTCARD_BRR(pclk, hsc->Init.BaudRate);
2335 }
2336#else
2337 if(hsc->Instance == USART1)
2338 {
2339 pclk = HAL_RCC_GetPCLK2Freq();
2340 hsc->Instance->BRR = SMARTCARD_BRR(pclk, hsc->Init.BaudRate);
2341 }
2342#endif /* USART6 */
2343 else
2344 {
2345 pclk = HAL_RCC_GetPCLK1Freq();
2346 hsc->Instance->BRR = SMARTCARD_BRR(pclk, hsc->Init.BaudRate);
2347 }
2348}
2349
2350/**
2351 * @}
2352 */
2353
2354#endif /* HAL_SMARTCARD_MODULE_ENABLED */
2355/**
2356 * @}
2357 */
2358
2359/**
2360 * @}
2361 */
2362
2363/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
Note: See TracBrowser for help on using the repository browser.