Merge pull request #10558 from iNavFlight/MrD_Correct-comments-on-OSD-symbols
[inav.git] / lib / main / STM32F7 / Drivers / STM32F7xx_HAL_Driver / Src / stm32f7xx_hal_smartcard.c
blob39aa945121827a768d1e8aeae8a7b44fe7c17116
1 /**
2 ******************************************************************************
3 * @file stm32f7xx_hal_smartcard.c
4 * @author MCD Application Team
5 * @version V1.2.2
6 * @date 14-April-2017
7 * @brief SMARTCARD HAL module driver.
8 * This file provides firmware functions to manage the following
9 * functionalities of the SMARTCARD peripheral:
10 * + Initialization and de-initialization functions
11 * + IO operation functions
12 * + Peripheral Control functions
13 * + Peripheral State and Error functions
15 @verbatim
16 ==============================================================================
17 ##### How to use this driver #####
18 ==============================================================================
19 [..]
20 The SMARTCARD HAL driver can be used as follows:
22 (#) Declare a SMARTCARD_HandleTypeDef handle structure (eg. SMARTCARD_HandleTypeDef hsmartcard).
23 (#) Associate a USART to the SMARTCARD handle hsmartcard.
24 (#) Initialize the SMARTCARD low level resources by implementing the HAL_SMARTCARD_MspInit() API:
25 (##) Enable the USARTx interface clock.
26 (##) SMARTCARD pins configuration:
27 (+++) Enable the clock for the SMARTCARD GPIOs.
28 (+++) Configure these SMARTCARD pins as alternate function pull-up.
29 (##) NVIC configuration if you need to use interrupt process (HAL_SMARTCARD_Transmit_IT()
30 and HAL_SMARTCARD_Receive_IT() APIs):
31 (+++) Configure the USARTx interrupt priority.
32 (+++) Enable the NVIC USART IRQ handle.
33 (+++) The specific USART interrupts (Transmission complete interrupt,
34 RXNE interrupt and Error Interrupts) will be managed using the macros
35 __HAL_SMARTCARD_ENABLE_IT() and __HAL_SMARTCARD_DISABLE_IT() inside the transmit and receive process.
36 (##) DMA Configuration if you need to use DMA process (HAL_SMARTCARD_Transmit_DMA()
37 and HAL_SMARTCARD_Receive_DMA() APIs):
38 (+++) Declare a DMA handle structure for the Tx/Rx stream.
39 (+++) Enable the DMAx interface clock.
40 (+++) Configure the declared DMA handle structure with the required Tx/Rx parameters.
41 (+++) Configure the DMA Tx/Rx Stream.
42 (+++) Associate the initialized DMA handle to the SMARTCARD DMA Tx/Rx handle.
43 (+++) Configure the priority and enable the NVIC for the transfer complete interrupt on the DMA Tx/Rx Stream.
45 (#) Program the Baud Rate, Parity, Mode(Receiver/Transmitter), clock enabling/disabling and accordingly,
46 the clock parameters (parity, phase, last bit), prescaler value, guard time and NACK on transmission
47 error enabling or disabling in the hsmartcard handle Init structure.
49 (#) If required, program SMARTCARD advanced features (TX/RX pins swap, TimeOut, auto-retry counter,...)
50 in the hsmartcard handle AdvancedInit structure.
52 (#) Initialize the SMARTCARD registers by calling the HAL_SMARTCARD_Init() API:
53 (++) This API configures also the low level Hardware GPIO, CLOCK, CORTEX...etc)
54 by calling the customized HAL_SMARTCARD_MspInit() API.
55 [..]
56 (@) The specific SMARTCARD interrupts (Transmission complete interrupt,
57 RXNE interrupt and Error Interrupts) will be managed using the macros
58 __HAL_SMARTCARD_ENABLE_IT() and __HAL_SMARTCARD_DISABLE_IT() inside the transmit and receive process.
60 [..]
61 [..] Three operation modes are available within this driver :
63 *** Polling mode IO operation ***
64 =================================
65 [..]
66 (+) Send an amount of data in blocking mode using HAL_SMARTCARD_Transmit()
67 (+) Receive an amount of data in blocking mode using HAL_SMARTCARD_Receive()
69 *** Interrupt mode IO operation ***
70 ===================================
71 [..]
72 (+) Send an amount of data in non-blocking mode using HAL_SMARTCARD_Transmit_IT()
73 (+) At transmission end of transfer HAL_SMARTCARD_TxCpltCallback() is executed and user can
74 add his own code by customization of function pointer HAL_SMARTCARD_TxCpltCallback()
75 (+) Receive an amount of data in non-blocking mode using HAL_SMARTCARD_Receive_IT()
76 (+) At reception end of transfer HAL_SMARTCARD_RxCpltCallback() is executed and user can
77 add his own code by customization of function pointer HAL_SMARTCARD_RxCpltCallback()
78 (+) In case of transfer Error, HAL_SMARTCARD_ErrorCallback() function is executed and user can
79 add his own code by customization of function pointer HAL_SMARTCARD_ErrorCallback()
81 *** DMA mode IO operation ***
82 ==============================
83 [..]
84 (+) Send an amount of data in non-blocking mode (DMA) using HAL_SMARTCARD_Transmit_DMA()
85 (+) At transmission end of transfer HAL_SMARTCARD_TxCpltCallback() is executed and user can
86 add his own code by customization of function pointer HAL_SMARTCARD_TxCpltCallback()
87 (+) Receive an amount of data in non-blocking mode (DMA) using HAL_SMARTCARD_Receive_DMA()
88 (+) At reception end of transfer HAL_SMARTCARD_RxCpltCallback() is executed and user can
89 add his own code by customization of function pointer HAL_SMARTCARD_RxCpltCallback()
90 (+) In case of transfer Error, HAL_SMARTCARD_ErrorCallback() function is executed and user can
91 add his own code by customization of function pointer HAL_SMARTCARD_ErrorCallback()
93 *** SMARTCARD HAL driver macros list ***
94 ========================================
95 [..]
96 Below the list of most used macros in SMARTCARD HAL driver.
98 (+) __HAL_SMARTCARD_GET_FLAG : Check whether or not the specified SMARTCARD flag is set
99 (+) __HAL_SMARTCARD_CLEAR_FLAG : Clear the specified SMARTCARD pending flag
100 (+) __HAL_SMARTCARD_ENABLE_IT: Enable the specified SMARTCARD interrupt
101 (+) __HAL_SMARTCARD_DISABLE_IT: Disable the specified SMARTCARD interrupt
102 (+) __HAL_SMARTCARD_GET_IT_SOURCE: Check whether or not the specified SMARTCARD interrupt is enabled
104 [..]
105 (@) You can refer to the SMARTCARD HAL driver header file for more useful macros
107 @endverbatim
108 ******************************************************************************
109 * @attention
111 * <h2><center>&copy; COPYRIGHT(c) 2017 STMicroelectronics</center></h2>
113 * Redistribution and use in source and binary forms, with or without modification,
114 * are permitted provided that the following conditions are met:
115 * 1. Redistributions of source code must retain the above copyright notice,
116 * this list of conditions and the following disclaimer.
117 * 2. Redistributions in binary form must reproduce the above copyright notice,
118 * this list of conditions and the following disclaimer in the documentation
119 * and/or other materials provided with the distribution.
120 * 3. Neither the name of STMicroelectronics nor the names of its contributors
121 * may be used to endorse or promote products derived from this software
122 * without specific prior written permission.
124 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
125 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
126 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
127 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
128 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
129 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
130 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
131 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
132 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
133 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
135 ******************************************************************************
138 /* Includes ------------------------------------------------------------------*/
139 #include "stm32f7xx_hal.h"
141 /** @addtogroup STM32F7xx_HAL_Driver
142 * @{
145 /** @defgroup SMARTCARD SMARTCARD
146 * @brief HAL SMARTCARD module driver
147 * @{
150 #ifdef HAL_SMARTCARD_MODULE_ENABLED
152 /* Private typedef -----------------------------------------------------------*/
153 /* Private define ------------------------------------------------------------*/
154 /** @defgroup SMARTCARD_Private_Constants SMARTCARD Private Constants
155 * @{
157 #define SMARTCARD_TEACK_REACK_TIMEOUT 1000U
158 #define HAL_SMARTCARD_TXDMA_TIMEOUTVALUE 22000U
159 #define USART_CR1_FIELDS ((uint32_t)(USART_CR1_M | USART_CR1_PCE | USART_CR1_PS | \
160 USART_CR1_TE | USART_CR1_RE | USART_CR1_OVER8)) /*!< USART CR1 fields of parameters set by SMARTCARD_SetConfig API */
161 #define USART_CR2_CLK_FIELDS ((uint32_t)(USART_CR2_CLKEN|USART_CR2_CPOL|USART_CR2_CPHA|USART_CR2_LBCL)) /*!< SMARTCARD clock-related USART CR2 fields of parameters */
162 #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 */
163 #define USART_CR3_FIELDS ((uint32_t)(USART_CR3_ONEBIT|USART_CR3_NACK|USART_CR3_SCARCNT)) /*!< USART CR3 fields of parameters set by SMARTCARD_SetConfig API */
165 * @}
168 /* Private macros ------------------------------------------------------------*/
169 /* Private variables ---------------------------------------------------------*/
170 /* Private function prototypes -----------------------------------------------*/
171 /** @addtogroup SMARTCARD_Private_Functions
172 * @{
174 static HAL_StatusTypeDef SMARTCARD_SetConfig(SMARTCARD_HandleTypeDef *hsmartcard);
175 static void SMARTCARD_AdvFeatureConfig(SMARTCARD_HandleTypeDef *hsmartcard);
176 static HAL_StatusTypeDef SMARTCARD_CheckIdleState(SMARTCARD_HandleTypeDef *hsmartcard);
177 static HAL_StatusTypeDef SMARTCARD_WaitOnFlagUntilTimeout(SMARTCARD_HandleTypeDef *hsmartcard, uint32_t Flag, FlagStatus Status, uint32_t Tickstart, uint32_t Timeout);
178 static void SMARTCARD_EndTxTransfer(SMARTCARD_HandleTypeDef *hsmartcard);
179 static void SMARTCARD_EndRxTransfer(SMARTCARD_HandleTypeDef *hsmartcard);
180 static void SMARTCARD_DMATransmitCplt(DMA_HandleTypeDef *hdma);
181 static void SMARTCARD_DMAReceiveCplt(DMA_HandleTypeDef *hdma);
182 static void SMARTCARD_DMAError(DMA_HandleTypeDef *hdma);
183 static void SMARTCARD_DMAAbortOnError(DMA_HandleTypeDef *hdma);
184 static void SMARTCARD_DMATxAbortCallback(DMA_HandleTypeDef *hdma);
185 static void SMARTCARD_DMARxAbortCallback(DMA_HandleTypeDef *hdma);
186 static void SMARTCARD_DMATxOnlyAbortCallback(DMA_HandleTypeDef *hdma);
187 static void SMARTCARD_DMARxOnlyAbortCallback(DMA_HandleTypeDef *hdma);
188 static HAL_StatusTypeDef SMARTCARD_Transmit_IT(SMARTCARD_HandleTypeDef *hsmartcard);
189 static HAL_StatusTypeDef SMARTCARD_EndTransmit_IT(SMARTCARD_HandleTypeDef *hsmartcard);
190 static HAL_StatusTypeDef SMARTCARD_Receive_IT(SMARTCARD_HandleTypeDef *hsmartcard);
192 * @}
195 /* Exported functions --------------------------------------------------------*/
197 /** @defgroup SMARTCARD_Exported_Functions SMARTCARD Exported Functions
198 * @{
201 /** @defgroup SMARTCARD_Exported_Functions_Group1 Initialization and de-initialization functions
202 * @brief Initialization and Configuration functions
204 @verbatim
205 ==============================================================================
206 ##### Initialization and Configuration functions #####
207 ==============================================================================
208 [..]
209 This subsection provides a set of functions allowing to initialize the USARTx
210 associated to the SmartCard.
211 (+) These parameters can be configured:
212 (++) Baud Rate
213 (++) Parity: parity should be enabled, frame Length is fixed to 8 bits plus parity
214 (++) Receiver/transmitter modes
215 (++) Synchronous mode (and if enabled, phase, polarity and last bit parameters)
216 (++) Prescaler value
217 (++) Guard bit time
218 (++) NACK enabling or disabling on transmission error
220 (+) The following advanced features can be configured as well:
221 (++) TX and/or RX pin level inversion
222 (++) data logical level inversion
223 (++) RX and TX pins swap
224 (++) RX overrun detection disabling
225 (++) DMA disabling on RX error
226 (++) MSB first on communication line
227 (++) Time out enabling (and if activated, timeout value)
228 (++) Block length
229 (++) Auto-retry counter
230 [..]
231 The HAL_SMARTCARD_Init() API follows the USART synchronous configuration procedures
232 (details for the procedures are available in reference manual).
234 @endverbatim
236 The USART frame format is given in the following table:
238 Table 1. USART frame format.
239 +---------------------------------------------------------------+
240 | M1M0 bits | PCE bit | USART frame |
241 |-----------------------|---------------------------------------|
242 | 01 | 1 | | SB | 8 bit data | PB | STB | |
243 +---------------------------------------------------------------+
246 * @{
250 * @brief Initialize the SMARTCARD mode according to the specified
251 * parameters in the SMARTCARD_HandleTypeDef and initialize the associated handle.
252 * @param hsmartcard Pointer to a SMARTCARD_HandleTypeDef structure that contains
253 * the configuration information for the specified SMARTCARD module.
254 * @retval HAL status
256 HAL_StatusTypeDef HAL_SMARTCARD_Init(SMARTCARD_HandleTypeDef *hsmartcard)
258 /* Check the SMARTCARD handle allocation */
259 if(hsmartcard == NULL)
261 return HAL_ERROR;
264 /* Check the USART associated to the SMARTCARD handle */
265 assert_param(IS_SMARTCARD_INSTANCE(hsmartcard->Instance));
267 if(hsmartcard->gState == HAL_SMARTCARD_STATE_RESET)
269 /* Allocate lock resource and initialize it */
270 hsmartcard->Lock = HAL_UNLOCKED;
272 /* Init the low level hardware : GPIO, CLOCK */
273 HAL_SMARTCARD_MspInit(hsmartcard);
276 hsmartcard->gState = HAL_SMARTCARD_STATE_BUSY;
278 /* Disable the Peripheral to set smartcard mode */
279 CLEAR_BIT(hsmartcard->Instance->CR1, USART_CR1_UE);
281 /* In SmartCard mode, the following bits must be kept cleared:
282 - LINEN in the USART_CR2 register,
283 - HDSEL and IREN bits in the USART_CR3 register.*/
284 CLEAR_BIT(hsmartcard->Instance->CR2, USART_CR2_LINEN);
285 CLEAR_BIT(hsmartcard->Instance->CR3, (USART_CR3_HDSEL | USART_CR3_IREN));
287 /* set the USART in SMARTCARD mode */
288 SET_BIT(hsmartcard->Instance->CR3, USART_CR3_SCEN);
290 /* Set the SMARTCARD Communication parameters */
291 if (SMARTCARD_SetConfig(hsmartcard) == HAL_ERROR)
293 return HAL_ERROR;
296 /* Set the SMARTCARD transmission completion indication */
297 SMARTCARD_TRANSMISSION_COMPLETION_SETTING(hsmartcard);
299 if (hsmartcard->AdvancedInit.AdvFeatureInit != SMARTCARD_ADVFEATURE_NO_INIT)
301 SMARTCARD_AdvFeatureConfig(hsmartcard);
304 /* Enable the Peripheral */
305 SET_BIT(hsmartcard->Instance->CR1, USART_CR1_UE);
307 /* TEACK and/or REACK to check before moving hsmartcard->gState and hsmartcard->RxState to Ready */
308 return (SMARTCARD_CheckIdleState(hsmartcard));
312 * @brief DeInitialize the SMARTCARD peripheral.
313 * @param hsmartcard Pointer to a SMARTCARD_HandleTypeDef structure that contains
314 * the configuration information for the specified SMARTCARD module.
315 * @retval HAL status
317 HAL_StatusTypeDef HAL_SMARTCARD_DeInit(SMARTCARD_HandleTypeDef *hsmartcard)
319 /* Check the SMARTCARD handle allocation */
320 if(hsmartcard == NULL)
322 return HAL_ERROR;
325 /* Check the USART/UART associated to the SMARTCARD handle */
326 assert_param(IS_SMARTCARD_INSTANCE(hsmartcard->Instance));
328 hsmartcard->gState = HAL_SMARTCARD_STATE_BUSY;
330 /* Disable the Peripheral */
331 CLEAR_BIT(hsmartcard->Instance->CR1, USART_CR1_UE);
333 WRITE_REG(hsmartcard->Instance->CR1, 0x0);
334 WRITE_REG(hsmartcard->Instance->CR2, 0x0);
335 WRITE_REG(hsmartcard->Instance->CR3, 0x0);
336 WRITE_REG(hsmartcard->Instance->RTOR, 0x0);
337 WRITE_REG(hsmartcard->Instance->GTPR, 0x0);
339 /* DeInit the low level hardware */
340 HAL_SMARTCARD_MspDeInit(hsmartcard);
342 hsmartcard->ErrorCode = HAL_SMARTCARD_ERROR_NONE;
343 hsmartcard->gState = HAL_SMARTCARD_STATE_RESET;
344 hsmartcard->RxState = HAL_SMARTCARD_STATE_RESET;
346 /* Process Unlock */
347 __HAL_UNLOCK(hsmartcard);
349 return HAL_OK;
353 * @brief Initialize the SMARTCARD MSP.
354 * @param hsmartcard Pointer to a SMARTCARD_HandleTypeDef structure that contains
355 * the configuration information for the specified SMARTCARD module.
356 * @retval None
358 __weak void HAL_SMARTCARD_MspInit(SMARTCARD_HandleTypeDef *hsmartcard)
360 /* Prevent unused argument(s) compilation warning */
361 UNUSED(hsmartcard);
363 /* NOTE : This function should not be modified, when the callback is needed,
364 the HAL_SMARTCARD_MspInit can be implemented in the user file
369 * @brief DeInitialize the SMARTCARD MSP.
370 * @param hsmartcard Pointer to a SMARTCARD_HandleTypeDef structure that contains
371 * the configuration information for the specified SMARTCARD module.
372 * @retval None
374 __weak void HAL_SMARTCARD_MspDeInit(SMARTCARD_HandleTypeDef *hsmartcard)
376 /* Prevent unused argument(s) compilation warning */
377 UNUSED(hsmartcard);
379 /* NOTE : This function should not be modified, when the callback is needed,
380 the HAL_SMARTCARD_MspDeInit can be implemented in the user file
385 * @}
388 /** @defgroup SMARTCARD_Exported_Functions_Group2 IO operation functions
389 * @brief SMARTCARD Transmit and Receive functions
391 @verbatim
392 ==============================================================================
393 ##### IO operation functions #####
394 ==============================================================================
395 [..]
396 This subsection provides a set of functions allowing to manage the SMARTCARD data transfers.
398 [..]
399 (#) Smartcard is a single wire half duplex communication protocol.
400 The Smartcard interface is designed to support asynchronous protocol Smartcards as
401 defined in the ISO 7816-3 standard.
402 (#) The USART should be configured as:
403 (++) 8 bits plus parity: where M=1 and PCE=1 in the USART_CR1 register
404 (++) 1.5 stop bits when transmitting and receiving: where STOP=11 in the USART_CR2 register.
406 (#) There are two modes of transfer:
407 (++) Blocking mode: The communication is performed in polling mode.
408 The HAL status of all data processing is returned by the same function
409 after finishing transfer.
410 (++) Non-Blocking mode: The communication is performed using Interrupts
411 or DMA, the relevant API's return the HAL status.
412 The end of the data processing will be indicated through the
413 dedicated SMARTCARD IRQ when using Interrupt mode or the DMA IRQ when
414 using DMA mode.
415 (++) The HAL_SMARTCARD_TxCpltCallback(), HAL_SMARTCARD_RxCpltCallback() user callbacks
416 will be executed respectively at the end of the Transmit or Receive process
417 The HAL_SMARTCARD_ErrorCallback() user callback will be executed when a communication
418 error is detected.
420 (#) Blocking mode APIs are :
421 (++) HAL_SMARTCARD_Transmit()
422 (++) HAL_SMARTCARD_Receive()
424 (#) Non Blocking mode APIs with Interrupt are :
425 (++) HAL_SMARTCARD_Transmit_IT()
426 (++) HAL_SMARTCARD_Receive_IT()
427 (++) HAL_SMARTCARD_IRQHandler()
429 (#) Non Blocking mode functions with DMA are :
430 (++) HAL_SMARTCARD_Transmit_DMA()
431 (++) HAL_SMARTCARD_Receive_DMA()
433 (#) A set of Transfer Complete Callbacks are provided in non Blocking mode:
434 (++) HAL_SMARTCARD_TxCpltCallback()
435 (++) HAL_SMARTCARD_RxCpltCallback()
436 (++) HAL_SMARTCARD_ErrorCallback()
438 (#) Non-Blocking mode transfers could be aborted using Abort API's :
439 (++) HAL_SMARTCARD_Abort()
440 (++) HAL_SMARTCARD_AbortTransmit()
441 (++) HAL_SMARTCARD_AbortReceive()
442 (++) HAL_SMARTCARD_Abort_IT()
443 (++) HAL_SMARTCARD_AbortTransmit_IT()
444 (++) HAL_SMARTCARD_AbortReceive_IT()
446 (#) For Abort services based on interrupts (HAL_SMARTCARD_Abortxxx_IT), a set of Abort Complete Callbacks are provided:
447 (++) HAL_SMARTCARD_AbortCpltCallback()
448 (++) HAL_SMARTCARD_AbortTransmitCpltCallback()
449 (++) HAL_SMARTCARD_AbortReceiveCpltCallback()
451 (#) In Non-Blocking mode transfers, possible errors are split into 2 categories.
452 Errors are handled as follows :
453 (++) Error is considered as Recoverable and non blocking : Transfer could go till end, but error severity is
454 to be evaluated by user : this concerns Frame Error, Parity Error or Noise Error in Interrupt mode reception .
455 Received character is then retrieved and stored in Rx buffer, Error code is set to allow user to identify error type,
456 and HAL_SMARTCARD_ErrorCallback() user callback is executed. Transfer is kept ongoing on SMARTCARD side.
457 If user wants to abort it, Abort services should be called by user.
458 (++) Error is considered as Blocking : Transfer could not be completed properly and is aborted.
459 This concerns Frame Error in Interrupt mode tranmission, Overrun Error in Interrupt mode reception and all errors in DMA mode.
460 Error code is set to allow user to identify error type, and HAL_SMARTCARD_ErrorCallback() user callback is executed.
462 @endverbatim
463 * @{
467 * @brief Send an amount of data in blocking mode.
468 * @param hsmartcard Pointer to a SMARTCARD_HandleTypeDef structure that contains
469 * the configuration information for the specified SMARTCARD module.
470 * @param pData pointer to data buffer.
471 * @param Size amount of data to be sent.
472 * @param Timeout Timeout duration.
473 * @retval HAL status
475 HAL_StatusTypeDef HAL_SMARTCARD_Transmit(SMARTCARD_HandleTypeDef *hsmartcard, uint8_t *pData, uint16_t Size, uint32_t Timeout)
477 uint32_t tickstart = 0;
479 /* Check that a Tx process is not already ongoing */
480 if (hsmartcard->gState == HAL_SMARTCARD_STATE_READY)
482 if((pData == NULL) || (Size == 0))
484 return HAL_ERROR;
487 /* Process Locked */
488 __HAL_LOCK(hsmartcard);
490 hsmartcard->gState = HAL_SMARTCARD_STATE_BUSY_TX;
492 /* Init tickstart for timeout managment*/
493 tickstart = HAL_GetTick();
495 /* Disable the Peripheral first to update mode for TX master */
496 CLEAR_BIT(hsmartcard->Instance->CR1, USART_CR1_UE);
498 /* Disable Rx, enable Tx */
499 CLEAR_BIT(hsmartcard->Instance->CR1, USART_CR1_RE);
500 SET_BIT(hsmartcard->Instance->RQR, SMARTCARD_RXDATA_FLUSH_REQUEST);
501 SET_BIT(hsmartcard->Instance->CR1, USART_CR1_TE);
503 /* Enable the Peripheral */
504 SET_BIT(hsmartcard->Instance->CR1, USART_CR1_UE);
506 hsmartcard->ErrorCode = HAL_SMARTCARD_ERROR_NONE;
507 hsmartcard->TxXferSize = Size;
508 hsmartcard->TxXferCount = Size;
510 while(hsmartcard->TxXferCount > 0)
512 hsmartcard->TxXferCount--;
513 if(SMARTCARD_WaitOnFlagUntilTimeout(hsmartcard, SMARTCARD_FLAG_TXE, RESET, tickstart, Timeout) != HAL_OK)
515 return HAL_TIMEOUT;
517 hsmartcard->Instance->TDR = (*pData++ & (uint8_t)0xFF);
519 if(SMARTCARD_WaitOnFlagUntilTimeout(hsmartcard, SMARTCARD_TRANSMISSION_COMPLETION_FLAG(hsmartcard), RESET, tickstart, Timeout) != HAL_OK)
521 return HAL_TIMEOUT;
523 /* Re-enable Rx at end of transmission if initial mode is Rx/Tx */
524 if(hsmartcard->Init.Mode == SMARTCARD_MODE_TX_RX)
526 /* Disable the Peripheral first to update modes */
527 CLEAR_BIT(hsmartcard->Instance->CR1, USART_CR1_UE);
528 SET_BIT(hsmartcard->Instance->CR1, USART_CR1_RE);
529 /* Enable the Peripheral */
530 SET_BIT(hsmartcard->Instance->CR1, USART_CR1_UE);
533 /* At end of Tx process, restore hsmartcard->gState to Ready */
534 hsmartcard->gState = HAL_SMARTCARD_STATE_READY;
536 /* Process Unlocked */
537 __HAL_UNLOCK(hsmartcard);
539 return HAL_OK;
541 else
543 return HAL_BUSY;
548 * @brief Receive an amount of data in blocking mode.
549 * @param hsmartcard Pointer to a SMARTCARD_HandleTypeDef structure that contains
550 * the configuration information for the specified SMARTCARD module.
551 * @param pData pointer to data buffer.
552 * @param Size amount of data to be received.
553 * @param Timeout Timeout duration.
554 * @retval HAL status
556 HAL_StatusTypeDef HAL_SMARTCARD_Receive(SMARTCARD_HandleTypeDef *hsmartcard, uint8_t *pData, uint16_t Size, uint32_t Timeout)
558 uint32_t tickstart = 0;
560 /* Check that a Rx process is not already ongoing */
561 if(hsmartcard->RxState == HAL_SMARTCARD_STATE_READY)
563 if((pData == NULL) || (Size == 0))
565 return HAL_ERROR;
568 /* Process Locked */
569 __HAL_LOCK(hsmartcard);
571 hsmartcard->ErrorCode = HAL_SMARTCARD_ERROR_NONE;
572 hsmartcard->RxState = HAL_SMARTCARD_STATE_BUSY_RX;
574 /* Init tickstart for timeout managment*/
575 tickstart = HAL_GetTick();
577 hsmartcard->RxXferSize = Size;
578 hsmartcard->RxXferCount = Size;
580 /* Check the remain data to be received */
581 while(hsmartcard->RxXferCount > 0)
583 hsmartcard->RxXferCount--;
585 if(SMARTCARD_WaitOnFlagUntilTimeout(hsmartcard, SMARTCARD_FLAG_RXNE, RESET, tickstart, Timeout) != HAL_OK)
587 return HAL_TIMEOUT;
589 *pData++ = (uint8_t)(hsmartcard->Instance->RDR & (uint8_t)0x00FF);
592 /* At end of Rx process, restore hsmartcard->RxState to Ready */
593 hsmartcard->RxState = HAL_SMARTCARD_STATE_READY;
595 /* Process Unlocked */
596 __HAL_UNLOCK(hsmartcard);
598 return HAL_OK;
600 else
602 return HAL_BUSY;
607 * @brief Send an amount of data in interrupt mode.
608 * @param hsmartcard Pointer to a SMARTCARD_HandleTypeDef structure that contains
609 * the configuration information for the specified SMARTCARD module.
610 * @param pData pointer to data buffer.
611 * @param Size amount of data to be sent.
612 * @retval HAL status
614 HAL_StatusTypeDef HAL_SMARTCARD_Transmit_IT(SMARTCARD_HandleTypeDef *hsmartcard, uint8_t *pData, uint16_t Size)
616 /* Check that a Tx process is not already ongoing */
617 if (hsmartcard->gState == HAL_SMARTCARD_STATE_READY)
619 if((pData == NULL) || (Size == 0))
621 return HAL_ERROR;
624 /* Process Locked */
625 __HAL_LOCK(hsmartcard);
627 hsmartcard->ErrorCode = HAL_SMARTCARD_ERROR_NONE;
628 hsmartcard->gState = HAL_SMARTCARD_STATE_BUSY_TX;
630 hsmartcard->pTxBuffPtr = pData;
631 hsmartcard->TxXferSize = Size;
632 hsmartcard->TxXferCount = Size;
634 /* Disable the Peripheral first to update mode for TX master */
635 CLEAR_BIT(hsmartcard->Instance->CR1, USART_CR1_UE);
637 /* Disable Rx, enable Tx */
638 CLEAR_BIT(hsmartcard->Instance->CR1, USART_CR1_RE);
639 SET_BIT(hsmartcard->Instance->RQR, SMARTCARD_RXDATA_FLUSH_REQUEST);
640 SET_BIT(hsmartcard->Instance->CR1, USART_CR1_TE);
642 /* Enable the Peripheral */
643 SET_BIT(hsmartcard->Instance->CR1, USART_CR1_UE);
645 /* Process Unlocked */
646 __HAL_UNLOCK(hsmartcard);
648 /* Enable the SMARTCARD Error Interrupt: (Frame error) */
649 SET_BIT(hsmartcard->Instance->CR3, USART_CR3_EIE);
651 /* Enable the SMARTCARD Transmit Data Register Empty Interrupt */
652 SET_BIT(hsmartcard->Instance->CR1, USART_CR1_TXEIE);
654 return HAL_OK;
656 else
658 return HAL_BUSY;
663 * @brief Receive an amount of data in interrupt mode.
664 * @param hsmartcard Pointer to a SMARTCARD_HandleTypeDef structure that contains
665 * the configuration information for the specified SMARTCARD module.
666 * @param pData pointer to data buffer.
667 * @param Size amount of data to be received.
668 * @retval HAL status
670 HAL_StatusTypeDef HAL_SMARTCARD_Receive_IT(SMARTCARD_HandleTypeDef *hsmartcard, uint8_t *pData, uint16_t Size)
672 /* Check that a Rx process is not already ongoing */
673 if(hsmartcard->RxState == HAL_SMARTCARD_STATE_READY)
675 if((pData == NULL) || (Size == 0))
677 return HAL_ERROR;
680 /* Process Locked */
681 __HAL_LOCK(hsmartcard);
683 hsmartcard->ErrorCode = HAL_SMARTCARD_ERROR_NONE;
684 hsmartcard->RxState = HAL_SMARTCARD_STATE_BUSY_RX;
686 hsmartcard->pRxBuffPtr = pData;
687 hsmartcard->RxXferSize = Size;
688 hsmartcard->RxXferCount = Size;
690 /* Process Unlocked */
691 __HAL_UNLOCK(hsmartcard);
693 /* Enable the SMARTCARD Parity Error and Data Register not empty Interrupts */
694 SET_BIT(hsmartcard->Instance->CR1, USART_CR1_PEIE| USART_CR1_RXNEIE);
696 /* Enable the SMARTCARD Error Interrupt: (Frame error, noise error, overrun error) */
697 SET_BIT(hsmartcard->Instance->CR3, USART_CR3_EIE);
699 return HAL_OK;
701 else
703 return HAL_BUSY;
708 * @brief Send an amount of data in DMA mode.
709 * @param hsmartcard Pointer to a SMARTCARD_HandleTypeDef structure that contains
710 * the configuration information for the specified SMARTCARD module.
711 * @param pData pointer to data buffer.
712 * @param Size amount of data to be sent.
713 * @retval HAL status
715 HAL_StatusTypeDef HAL_SMARTCARD_Transmit_DMA(SMARTCARD_HandleTypeDef *hsmartcard, uint8_t *pData, uint16_t Size)
717 /* Check that a Tx process is not already ongoing */
718 if (hsmartcard->gState == HAL_SMARTCARD_STATE_READY)
720 if((pData == NULL) || (Size == 0))
722 return HAL_ERROR;
725 /* Process Locked */
726 __HAL_LOCK(hsmartcard);
728 hsmartcard->gState = HAL_SMARTCARD_STATE_BUSY_TX;
730 hsmartcard->ErrorCode = HAL_SMARTCARD_ERROR_NONE;
731 hsmartcard->pTxBuffPtr = pData;
732 hsmartcard->TxXferSize = Size;
733 hsmartcard->TxXferCount = Size;
735 /* Disable the Peripheral first to update mode for TX master */
736 CLEAR_BIT(hsmartcard->Instance->CR1, USART_CR1_UE);
738 /* Disable Rx, enable Tx */
739 CLEAR_BIT(hsmartcard->Instance->CR1, USART_CR1_RE);
740 SET_BIT(hsmartcard->Instance->RQR, SMARTCARD_RXDATA_FLUSH_REQUEST);
741 SET_BIT(hsmartcard->Instance->CR1, USART_CR1_TE);
743 /* Enable the Peripheral */
744 SET_BIT(hsmartcard->Instance->CR1, USART_CR1_UE);
746 /* Set the SMARTCARD DMA transfer complete callback */
747 hsmartcard->hdmatx->XferCpltCallback = SMARTCARD_DMATransmitCplt;
749 /* Set the SMARTCARD error callback */
750 hsmartcard->hdmatx->XferErrorCallback = SMARTCARD_DMAError;
752 /* Set the DMA abort callback */
753 hsmartcard->hdmatx->XferAbortCallback = NULL;
755 /* Enable the SMARTCARD transmit DMA channel */
756 HAL_DMA_Start_IT(hsmartcard->hdmatx, (uint32_t)hsmartcard->pTxBuffPtr, (uint32_t)&hsmartcard->Instance->TDR, Size);
758 /* Clear the TC flag in the ICR register */
759 CLEAR_BIT(hsmartcard->Instance->ICR, USART_ICR_TCCF);
761 /* Process Unlocked */
762 __HAL_UNLOCK(hsmartcard);
764 /* Enable the UART Error Interrupt: (Frame error) */
765 SET_BIT(hsmartcard->Instance->CR3, USART_CR3_EIE);
767 /* Enable the DMA transfer for transmit request by setting the DMAT bit
768 in the SMARTCARD associated USART CR3 register */
769 SET_BIT(hsmartcard->Instance->CR3, USART_CR3_DMAT);
771 return HAL_OK;
773 else
775 return HAL_BUSY;
780 * @brief Receive an amount of data in DMA mode.
781 * @param hsmartcard Pointer to a SMARTCARD_HandleTypeDef structure that contains
782 * the configuration information for the specified SMARTCARD module.
783 * @param pData pointer to data buffer.
784 * @param Size amount of data to be received.
785 * @note The SMARTCARD-associated USART parity is enabled (PCE = 1),
786 * the received data contain the parity bit (MSB position).
787 * @retval HAL status
789 HAL_StatusTypeDef HAL_SMARTCARD_Receive_DMA(SMARTCARD_HandleTypeDef *hsmartcard, uint8_t *pData, uint16_t Size)
791 /* Check that a Rx process is not already ongoing */
792 if(hsmartcard->RxState == HAL_SMARTCARD_STATE_READY)
794 if((pData == NULL) || (Size == 0))
796 return HAL_ERROR;
799 /* Process Locked */
800 __HAL_LOCK(hsmartcard);
802 hsmartcard->ErrorCode = HAL_SMARTCARD_ERROR_NONE;
803 hsmartcard->RxState = HAL_SMARTCARD_STATE_BUSY_RX;
805 hsmartcard->pRxBuffPtr = pData;
806 hsmartcard->RxXferSize = Size;
808 /* Set the SMARTCARD DMA transfer complete callback */
809 hsmartcard->hdmarx->XferCpltCallback = SMARTCARD_DMAReceiveCplt;
811 /* Set the SMARTCARD DMA error callback */
812 hsmartcard->hdmarx->XferErrorCallback = SMARTCARD_DMAError;
814 /* Set the DMA abort callback */
815 hsmartcard->hdmarx->XferAbortCallback = NULL;
817 /* Enable the DMA channel */
818 HAL_DMA_Start_IT(hsmartcard->hdmarx, (uint32_t)&hsmartcard->Instance->RDR, (uint32_t)hsmartcard->pRxBuffPtr, Size);
820 /* Process Unlocked */
821 __HAL_UNLOCK(hsmartcard);
823 /* Enable the UART Parity Error Interrupt */
824 SET_BIT(hsmartcard->Instance->CR1, USART_CR1_PEIE);
826 /* Enable the UART Error Interrupt: (Frame error, noise error, overrun error) */
827 SET_BIT(hsmartcard->Instance->CR3, USART_CR3_EIE);
829 /* Enable the DMA transfer for the receiver request by setting the DMAR bit
830 in the SMARTCARD associated USART CR3 register */
831 SET_BIT(hsmartcard->Instance->CR3, USART_CR3_DMAR);
833 return HAL_OK;
835 else
837 return HAL_BUSY;
842 * @brief Abort ongoing transfers (blocking mode).
843 * @param hsmartcard Pointer to a SMARTCARD_HandleTypeDef structure that contains
844 * the configuration information for the specified SMARTCARD module.
845 * @note This procedure could be used for aborting any ongoing transfer started in Interrupt or DMA mode.
846 * This procedure performs following operations :
847 * - Disable SMARTCARD Interrupts (Tx and Rx)
848 * - Disable the DMA transfer in the peripheral register (if enabled)
849 * - Abort DMA transfer by calling HAL_DMA_Abort (in case of transfer in DMA mode)
850 * - Set handle State to READY
851 * @note This procedure is executed in blocking mode : when exiting function, Abort is considered as completed.
852 * @retval HAL status
854 HAL_StatusTypeDef HAL_SMARTCARD_Abort(SMARTCARD_HandleTypeDef *hsmartcard)
856 /* Disable RTOIE, EOBIE, TXEIE, TCIE, RXNE, PE and ERR (Frame error, noise error, overrun error) interrupts */
857 CLEAR_BIT(hsmartcard->Instance->CR1, (USART_CR1_RXNEIE | USART_CR1_PEIE | USART_CR1_TXEIE | USART_CR1_TCIE | USART_CR1_RTOIE | USART_CR1_EOBIE));
858 CLEAR_BIT(hsmartcard->Instance->CR3, USART_CR3_EIE);
860 /* Disable the SMARTCARD DMA Tx request if enabled */
861 if (HAL_IS_BIT_SET(hsmartcard->Instance->CR3, USART_CR3_DMAT))
863 CLEAR_BIT(hsmartcard->Instance->CR3, USART_CR3_DMAT);
865 /* Abort the SMARTCARD DMA Tx channel : use blocking DMA Abort API (no callback) */
866 if(hsmartcard->hdmatx != NULL)
868 /* Set the SMARTCARD DMA Abort callback to Null.
869 No call back execution at end of DMA abort procedure */
870 hsmartcard->hdmatx->XferAbortCallback = NULL;
872 HAL_DMA_Abort(hsmartcard->hdmatx);
876 /* Disable the SMARTCARD DMA Rx request if enabled */
877 if (HAL_IS_BIT_SET(hsmartcard->Instance->CR3, USART_CR3_DMAR))
879 CLEAR_BIT(hsmartcard->Instance->CR3, USART_CR3_DMAR);
881 /* Abort the SMARTCARD DMA Rx channel : use blocking DMA Abort API (no callback) */
882 if(hsmartcard->hdmarx != NULL)
884 /* Set the SMARTCARD DMA Abort callback to Null.
885 No call back execution at end of DMA abort procedure */
886 hsmartcard->hdmarx->XferAbortCallback = NULL;
888 HAL_DMA_Abort(hsmartcard->hdmarx);
892 /* Reset Tx and Rx transfer counters */
893 hsmartcard->TxXferCount = 0;
894 hsmartcard->RxXferCount = 0;
896 /* Clear the Error flags in the ICR register */
897 __HAL_SMARTCARD_CLEAR_FLAG(hsmartcard, SMARTCARD_CLEAR_OREF | SMARTCARD_CLEAR_NEF | SMARTCARD_CLEAR_PEF | SMARTCARD_CLEAR_FEF | SMARTCARD_CLEAR_RTOF | SMARTCARD_CLEAR_EOBF);
899 /* Restore hsmartcard->gState and hsmartcard->RxState to Ready */
900 hsmartcard->gState = HAL_SMARTCARD_STATE_READY;
901 hsmartcard->RxState = HAL_SMARTCARD_STATE_READY;
903 /* Reset Handle ErrorCode to No Error */
904 hsmartcard->ErrorCode = HAL_SMARTCARD_ERROR_NONE;
906 return HAL_OK;
910 * @brief Abort ongoing Transmit transfer (blocking mode).
911 * @param hsmartcard Pointer to a SMARTCARD_HandleTypeDef structure that contains
912 * the configuration information for the specified SMARTCARD module.
913 * @note This procedure could be used for aborting any ongoing Tx transfer started in Interrupt or DMA mode.
914 * This procedure performs following operations :
915 * - Disable SMARTCARD Interrupts (Tx)
916 * - Disable the DMA transfer in the peripheral register (if enabled)
917 * - Abort DMA transfer by calling HAL_DMA_Abort (in case of transfer in DMA mode)
918 * - Set handle State to READY
919 * @note This procedure is executed in blocking mode : when exiting function, Abort is considered as completed.
920 * @retval HAL status
922 HAL_StatusTypeDef HAL_SMARTCARD_AbortTransmit(SMARTCARD_HandleTypeDef *hsmartcard)
924 /* Disable TXEIE and TCIE interrupts */
925 CLEAR_BIT(hsmartcard->Instance->CR1, (USART_CR1_TXEIE | USART_CR1_TCIE));
927 /* Check if a receive process is ongoing or not. If not disable ERR IT */
928 if(hsmartcard->RxState == HAL_SMARTCARD_STATE_READY)
930 /* Disable the SMARTCARD Error Interrupt: (Frame error) */
931 CLEAR_BIT(hsmartcard->Instance->CR3, USART_CR3_EIE);
934 /* Disable the SMARTCARD DMA Tx request if enabled */
935 if (HAL_IS_BIT_SET(hsmartcard->Instance->CR3, USART_CR3_DMAT))
937 CLEAR_BIT(hsmartcard->Instance->CR3, USART_CR3_DMAT);
939 /* Abort the SMARTCARD DMA Tx channel : use blocking DMA Abort API (no callback) */
940 if(hsmartcard->hdmatx != NULL)
942 /* Set the SMARTCARD DMA Abort callback to Null.
943 No call back execution at end of DMA abort procedure */
944 hsmartcard->hdmatx->XferAbortCallback = NULL;
946 HAL_DMA_Abort(hsmartcard->hdmatx);
950 /* Reset Tx transfer counter */
951 hsmartcard->TxXferCount = 0;
953 /* Clear the Error flags in the ICR register */
954 __HAL_SMARTCARD_CLEAR_FLAG(hsmartcard, SMARTCARD_CLEAR_FEF);
956 /* Restore hsmartcard->gState to Ready */
957 hsmartcard->gState = HAL_SMARTCARD_STATE_READY;
959 return HAL_OK;
963 * @brief Abort ongoing Receive transfer (blocking mode).
964 * @param hsmartcard Pointer to a SMARTCARD_HandleTypeDef structure that contains
965 * the configuration information for the specified SMARTCARD module.
966 * @note This procedure could be used for aborting any ongoing Rx transfer started in Interrupt or DMA mode.
967 * This procedure performs following operations :
968 * - Disable SMARTCARD Interrupts (Rx)
969 * - Disable the DMA transfer in the peripheral register (if enabled)
970 * - Abort DMA transfer by calling HAL_DMA_Abort (in case of transfer in DMA mode)
971 * - Set handle State to READY
972 * @note This procedure is executed in blocking mode : when exiting function, Abort is considered as completed.
973 * @retval HAL status
975 HAL_StatusTypeDef HAL_SMARTCARD_AbortReceive(SMARTCARD_HandleTypeDef *hsmartcard)
977 /* Disable RTOIE, EOBIE, RXNE, PE and ERR (Frame error, noise error, overrun error) interrupts */
978 CLEAR_BIT(hsmartcard->Instance->CR1, (USART_CR1_RXNEIE | USART_CR1_PEIE | USART_CR1_RTOIE | USART_CR1_EOBIE));
980 /* Check if a Transmit process is ongoing or not. If not disable ERR IT */
981 if(hsmartcard->gState == HAL_SMARTCARD_STATE_READY)
983 /* Disable the SMARTCARD Error Interrupt: (Frame error) */
984 CLEAR_BIT(hsmartcard->Instance->CR3, USART_CR3_EIE);
987 /* Disable the SMARTCARD DMA Rx request if enabled */
988 if (HAL_IS_BIT_SET(hsmartcard->Instance->CR3, USART_CR3_DMAR))
990 CLEAR_BIT(hsmartcard->Instance->CR3, USART_CR3_DMAR);
992 /* Abort the SMARTCARD DMA Rx channel : use blocking DMA Abort API (no callback) */
993 if(hsmartcard->hdmarx != NULL)
995 /* Set the SMARTCARD DMA Abort callback to Null.
996 No call back execution at end of DMA abort procedure */
997 hsmartcard->hdmarx->XferAbortCallback = NULL;
999 HAL_DMA_Abort(hsmartcard->hdmarx);
1003 /* Reset Rx transfer counter */
1004 hsmartcard->RxXferCount = 0;
1006 /* Clear the Error flags in the ICR register */
1007 __HAL_SMARTCARD_CLEAR_FLAG(hsmartcard, SMARTCARD_CLEAR_OREF | SMARTCARD_CLEAR_NEF | SMARTCARD_CLEAR_PEF | SMARTCARD_CLEAR_FEF | SMARTCARD_CLEAR_RTOF | SMARTCARD_CLEAR_EOBF);
1009 /* Restore hsmartcard->RxState to Ready */
1010 hsmartcard->RxState = HAL_SMARTCARD_STATE_READY;
1012 return HAL_OK;
1016 * @brief Abort ongoing transfers (Interrupt mode).
1017 * @param hsmartcard Pointer to a SMARTCARD_HandleTypeDef structure that contains
1018 * the configuration information for the specified SMARTCARD module.
1019 * @note This procedure could be used for aborting any ongoing transfer started in Interrupt or DMA mode.
1020 * This procedure performs following operations :
1021 * - Disable SMARTCARD Interrupts (Tx and Rx)
1022 * - Disable the DMA transfer in the peripheral register (if enabled)
1023 * - Abort DMA transfer by calling HAL_DMA_Abort_IT (in case of transfer in DMA mode)
1024 * - Set handle State to READY
1025 * - At abort completion, call user abort complete callback
1026 * @note This procedure is executed in Interrupt mode, meaning that abort procedure could be
1027 * considered as completed only when user abort complete callback is executed (not when exiting function).
1028 * @retval HAL status
1030 HAL_StatusTypeDef HAL_SMARTCARD_Abort_IT(SMARTCARD_HandleTypeDef *hsmartcard)
1032 uint32_t abortcplt = 1;
1034 /* Disable RTOIE, EOBIE, TXEIE, TCIE, RXNE, PE and ERR (Frame error, noise error, overrun error) interrupts */
1035 CLEAR_BIT(hsmartcard->Instance->CR1, (USART_CR1_RXNEIE | USART_CR1_PEIE | USART_CR1_TXEIE | USART_CR1_TCIE | USART_CR1_RTOIE | USART_CR1_EOBIE));
1036 CLEAR_BIT(hsmartcard->Instance->CR3, USART_CR3_EIE);
1038 /* If DMA Tx and/or DMA Rx Handles are associated to SMARTCARD Handle, DMA Abort complete callbacks should be initialised
1039 before any call to DMA Abort functions */
1040 /* DMA Tx Handle is valid */
1041 if(hsmartcard->hdmatx != NULL)
1043 /* Set DMA Abort Complete callback if SMARTCARD DMA Tx request if enabled.
1044 Otherwise, set it to NULL */
1045 if(HAL_IS_BIT_SET(hsmartcard->Instance->CR3, USART_CR3_DMAT))
1047 hsmartcard->hdmatx->XferAbortCallback = SMARTCARD_DMATxAbortCallback;
1049 else
1051 hsmartcard->hdmatx->XferAbortCallback = NULL;
1054 /* DMA Rx Handle is valid */
1055 if(hsmartcard->hdmarx != NULL)
1057 /* Set DMA Abort Complete callback if SMARTCARD DMA Rx request if enabled.
1058 Otherwise, set it to NULL */
1059 if(HAL_IS_BIT_SET(hsmartcard->Instance->CR3, USART_CR3_DMAR))
1061 hsmartcard->hdmarx->XferAbortCallback = SMARTCARD_DMARxAbortCallback;
1063 else
1065 hsmartcard->hdmarx->XferAbortCallback = NULL;
1069 /* Disable the SMARTCARD DMA Tx request if enabled */
1070 if(HAL_IS_BIT_SET(hsmartcard->Instance->CR3, USART_CR3_DMAT))
1072 /* Disable DMA Tx at UART level */
1073 CLEAR_BIT(hsmartcard->Instance->CR3, USART_CR3_DMAT);
1075 /* Abort the SMARTCARD DMA Tx channel : use non blocking DMA Abort API (callback) */
1076 if(hsmartcard->hdmatx != NULL)
1078 /* SMARTCARD Tx DMA Abort callback has already been initialised :
1079 will lead to call HAL_SMARTCARD_AbortCpltCallback() at end of DMA abort procedure */
1081 /* Abort DMA TX */
1082 if(HAL_DMA_Abort_IT(hsmartcard->hdmatx) != HAL_OK)
1084 hsmartcard->hdmatx->XferAbortCallback = NULL;
1086 else
1088 abortcplt = 0;
1093 /* Disable the SMARTCARD DMA Rx request if enabled */
1094 if (HAL_IS_BIT_SET(hsmartcard->Instance->CR3, USART_CR3_DMAR))
1096 CLEAR_BIT(hsmartcard->Instance->CR3, USART_CR3_DMAR);
1098 /* Abort the SMARTCARD DMA Rx channel : use non blocking DMA Abort API (callback) */
1099 if(hsmartcard->hdmarx != NULL)
1101 /* SMARTCARD Rx DMA Abort callback has already been initialised :
1102 will lead to call HAL_SMARTCARD_AbortCpltCallback() at end of DMA abort procedure */
1104 /* Abort DMA RX */
1105 if(HAL_DMA_Abort_IT(hsmartcard->hdmarx) != HAL_OK)
1107 hsmartcard->hdmarx->XferAbortCallback = NULL;
1108 abortcplt = 1;
1110 else
1112 abortcplt = 0;
1117 /* if no DMA abort complete callback execution is required => call user Abort Complete callback */
1118 if (abortcplt == 1)
1120 /* Reset Tx and Rx transfer counters */
1121 hsmartcard->TxXferCount = 0;
1122 hsmartcard->RxXferCount = 0;
1124 /* Reset errorCode */
1125 hsmartcard->ErrorCode = HAL_SMARTCARD_ERROR_NONE;
1127 /* Clear the Error flags in the ICR register */
1128 __HAL_SMARTCARD_CLEAR_FLAG(hsmartcard, SMARTCARD_CLEAR_OREF | SMARTCARD_CLEAR_NEF | SMARTCARD_CLEAR_PEF | SMARTCARD_CLEAR_FEF | SMARTCARD_CLEAR_RTOF | SMARTCARD_CLEAR_EOBF);
1130 /* Restore hsmartcard->gState and hsmartcard->RxState to Ready */
1131 hsmartcard->gState = HAL_SMARTCARD_STATE_READY;
1132 hsmartcard->RxState = HAL_SMARTCARD_STATE_READY;
1134 /* As no DMA to be aborted, call directly user Abort complete callback */
1135 HAL_SMARTCARD_AbortCpltCallback(hsmartcard);
1138 return HAL_OK;
1142 * @brief Abort ongoing Transmit transfer (Interrupt mode).
1143 * @param hsmartcard Pointer to a SMARTCARD_HandleTypeDef structure that contains
1144 * the configuration information for the specified SMARTCARD module.
1145 * @note This procedure could be used for aborting any ongoing Tx transfer started in Interrupt or DMA mode.
1146 * This procedure performs following operations :
1147 * - Disable SMARTCARD Interrupts (Tx)
1148 * - Disable the DMA transfer in the peripheral register (if enabled)
1149 * - Abort DMA transfer by calling HAL_DMA_Abort_IT (in case of transfer in DMA mode)
1150 * - Set handle State to READY
1151 * - At abort completion, call user abort complete callback
1152 * @note This procedure is executed in Interrupt mode, meaning that abort procedure could be
1153 * considered as completed only when user abort complete callback is executed (not when exiting function).
1154 * @retval HAL status
1156 HAL_StatusTypeDef HAL_SMARTCARD_AbortTransmit_IT(SMARTCARD_HandleTypeDef *hsmartcard)
1158 /* Disable TXEIE and TCIE interrupts */
1159 CLEAR_BIT(hsmartcard->Instance->CR1, (USART_CR1_TXEIE | USART_CR1_TCIE));
1161 /* Check if a receive process is ongoing or not. If not disable ERR IT */
1162 if(hsmartcard->RxState == HAL_SMARTCARD_STATE_READY)
1164 /* Disable the SMARTCARD Error Interrupt: (Frame error) */
1165 CLEAR_BIT(hsmartcard->Instance->CR3, USART_CR3_EIE);
1168 /* Disable the SMARTCARD DMA Tx request if enabled */
1169 if (HAL_IS_BIT_SET(hsmartcard->Instance->CR3, USART_CR3_DMAT))
1171 CLEAR_BIT(hsmartcard->Instance->CR3, USART_CR3_DMAT);
1173 /* Abort the SMARTCARD DMA Tx channel : use non blocking DMA Abort API (callback) */
1174 if(hsmartcard->hdmatx != NULL)
1176 /* Set the SMARTCARD DMA Abort callback :
1177 will lead to call HAL_SMARTCARD_AbortCpltCallback() at end of DMA abort procedure */
1178 hsmartcard->hdmatx->XferAbortCallback = SMARTCARD_DMATxOnlyAbortCallback;
1180 /* Abort DMA TX */
1181 if(HAL_DMA_Abort_IT(hsmartcard->hdmatx) != HAL_OK)
1183 /* Call Directly hsmartcard->hdmatx->XferAbortCallback function in case of error */
1184 hsmartcard->hdmatx->XferAbortCallback(hsmartcard->hdmatx);
1187 else
1189 /* Reset Tx transfer counter */
1190 hsmartcard->TxXferCount = 0;
1192 /* Restore hsmartcard->gState to Ready */
1193 hsmartcard->gState = HAL_SMARTCARD_STATE_READY;
1195 /* As no DMA to be aborted, call directly user Abort complete callback */
1196 HAL_SMARTCARD_AbortTransmitCpltCallback(hsmartcard);
1199 else
1201 /* Reset Tx transfer counter */
1202 hsmartcard->TxXferCount = 0;
1204 /* Clear the Error flags in the ICR register */
1205 __HAL_SMARTCARD_CLEAR_FLAG(hsmartcard, SMARTCARD_CLEAR_FEF);
1207 /* Restore hsmartcard->gState to Ready */
1208 hsmartcard->gState = HAL_SMARTCARD_STATE_READY;
1210 /* As no DMA to be aborted, call directly user Abort complete callback */
1211 HAL_SMARTCARD_AbortTransmitCpltCallback(hsmartcard);
1214 return HAL_OK;
1218 * @brief Abort ongoing Receive transfer (Interrupt mode).
1219 * @param hsmartcard Pointer to a SMARTCARD_HandleTypeDef structure that contains
1220 * the configuration information for the specified SMARTCARD module.
1221 * @note This procedure could be used for aborting any ongoing Rx transfer started in Interrupt or DMA mode.
1222 * This procedure performs following operations :
1223 * - Disable SMARTCARD Interrupts (Rx)
1224 * - Disable the DMA transfer in the peripheral register (if enabled)
1225 * - Abort DMA transfer by calling HAL_DMA_Abort_IT (in case of transfer in DMA mode)
1226 * - Set handle State to READY
1227 * - At abort completion, call user abort complete callback
1228 * @note This procedure is executed in Interrupt mode, meaning that abort procedure could be
1229 * considered as completed only when user abort complete callback is executed (not when exiting function).
1230 * @retval HAL status
1232 HAL_StatusTypeDef HAL_SMARTCARD_AbortReceive_IT(SMARTCARD_HandleTypeDef *hsmartcard)
1234 /* Disable RTOIE, EOBIE, RXNE, PE and ERR (Frame error, noise error, overrun error) interrupts */
1235 CLEAR_BIT(hsmartcard->Instance->CR1, (USART_CR1_RXNEIE | USART_CR1_PEIE | USART_CR1_RTOIE | USART_CR1_EOBIE));
1237 /* Check if a Transmit process is ongoing or not. If not disable ERR IT */
1238 if(hsmartcard->gState == HAL_SMARTCARD_STATE_READY)
1240 /* Disable the SMARTCARD Error Interrupt: (Frame error) */
1241 CLEAR_BIT(hsmartcard->Instance->CR3, USART_CR3_EIE);
1244 /* Disable the SMARTCARD DMA Rx request if enabled */
1245 if (HAL_IS_BIT_SET(hsmartcard->Instance->CR3, USART_CR3_DMAR))
1247 CLEAR_BIT(hsmartcard->Instance->CR3, USART_CR3_DMAR);
1249 /* Abort the SMARTCARD DMA Rx channel : use non blocking DMA Abort API (callback) */
1250 if(hsmartcard->hdmarx != NULL)
1252 /* Set the SMARTCARD DMA Abort callback :
1253 will lead to call HAL_SMARTCARD_AbortCpltCallback() at end of DMA abort procedure */
1254 hsmartcard->hdmarx->XferAbortCallback = SMARTCARD_DMARxOnlyAbortCallback;
1256 /* Abort DMA RX */
1257 if(HAL_DMA_Abort_IT(hsmartcard->hdmarx) != HAL_OK)
1259 /* Call Directly hsmartcard->hdmarx->XferAbortCallback function in case of error */
1260 hsmartcard->hdmarx->XferAbortCallback(hsmartcard->hdmarx);
1263 else
1265 /* Reset Rx transfer counter */
1266 hsmartcard->RxXferCount = 0;
1268 /* Clear the Error flags in the ICR register */
1269 __HAL_SMARTCARD_CLEAR_FLAG(hsmartcard, SMARTCARD_CLEAR_OREF | SMARTCARD_CLEAR_NEF | SMARTCARD_CLEAR_PEF | SMARTCARD_CLEAR_FEF | SMARTCARD_CLEAR_RTOF | SMARTCARD_CLEAR_EOBF);
1271 /* Restore hsmartcard->RxState to Ready */
1272 hsmartcard->RxState = HAL_SMARTCARD_STATE_READY;
1274 /* As no DMA to be aborted, call directly user Abort complete callback */
1275 HAL_SMARTCARD_AbortReceiveCpltCallback(hsmartcard);
1278 else
1280 /* Reset Rx transfer counter */
1281 hsmartcard->RxXferCount = 0;
1283 /* Clear the Error flags in the ICR register */
1284 __HAL_SMARTCARD_CLEAR_FLAG(hsmartcard, SMARTCARD_CLEAR_OREF | SMARTCARD_CLEAR_NEF | SMARTCARD_CLEAR_PEF | SMARTCARD_CLEAR_FEF | SMARTCARD_CLEAR_RTOF | SMARTCARD_CLEAR_EOBF);
1286 /* Restore hsmartcard->RxState to Ready */
1287 hsmartcard->RxState = HAL_SMARTCARD_STATE_READY;
1289 /* As no DMA to be aborted, call directly user Abort complete callback */
1290 HAL_SMARTCARD_AbortReceiveCpltCallback(hsmartcard);
1293 return HAL_OK;
1297 * @brief Handle SMARTCARD interrupt requests.
1298 * @param hsmartcard: Pointer to a SMARTCARD_HandleTypeDef structure that contains
1299 * the configuration information for the specified SMARTCARD module.
1300 * @retval None
1302 void HAL_SMARTCARD_IRQHandler(SMARTCARD_HandleTypeDef *hsmartcard)
1304 uint32_t isrflags = READ_REG(hsmartcard->Instance->ISR);
1305 uint32_t cr1its = READ_REG(hsmartcard->Instance->CR1);
1306 uint32_t cr3its;
1307 uint32_t errorflags;
1309 /* If no error occurs */
1310 errorflags = (isrflags & (uint32_t)(USART_ISR_PE | USART_ISR_FE | USART_ISR_ORE | USART_ISR_NE | USART_ISR_RTOF));
1311 if (errorflags == RESET)
1313 /* SMARTCARD in mode Receiver ---------------------------------------------------*/
1314 if(((isrflags & USART_ISR_RXNE) != RESET) && ((cr1its & USART_CR1_RXNEIE) != RESET))
1316 SMARTCARD_Receive_IT(hsmartcard);
1317 /* Clear RXNE interrupt flag done by reading RDR in SMARTCARD_Receive_IT() */
1318 return;
1322 /* If some errors occur */
1323 cr3its = READ_REG(hsmartcard->Instance->CR3);
1324 if( (errorflags != RESET)
1325 && ( ((cr3its & USART_CR3_EIE) != RESET)
1326 || ((cr1its & (USART_CR1_RXNEIE | USART_CR1_PEIE | USART_CR1_RTOIE)) != RESET)) )
1328 /* SMARTCARD parity error interrupt occurred -------------------------------------*/
1329 if(((isrflags & USART_ISR_PE) != RESET) && ((cr1its & USART_CR1_PEIE) != RESET))
1331 __HAL_SMARTCARD_CLEAR_IT(hsmartcard, SMARTCARD_CLEAR_PEF);
1333 hsmartcard->ErrorCode |= HAL_SMARTCARD_ERROR_PE;
1336 /* SMARTCARD frame error interrupt occurred --------------------------------------*/
1337 if(((isrflags & USART_ISR_FE) != RESET) && ((cr3its & USART_CR3_EIE) != RESET))
1339 __HAL_SMARTCARD_CLEAR_IT(hsmartcard, SMARTCARD_CLEAR_FEF);
1341 hsmartcard->ErrorCode |= HAL_SMARTCARD_ERROR_FE;
1344 /* SMARTCARD noise error interrupt occurred --------------------------------------*/
1345 if(((isrflags & USART_ISR_NE) != RESET) && ((cr3its & USART_CR3_EIE) != RESET))
1347 __HAL_SMARTCARD_CLEAR_IT(hsmartcard, SMARTCARD_CLEAR_NEF);
1349 hsmartcard->ErrorCode |= HAL_SMARTCARD_ERROR_NE;
1352 /* SMARTCARD Over-Run interrupt occurred -----------------------------------------*/
1353 if(((isrflags & USART_ISR_ORE) != RESET) &&
1354 (((cr1its & USART_CR1_RXNEIE) != RESET) || ((cr3its & USART_CR3_EIE) != RESET)))
1356 __HAL_SMARTCARD_CLEAR_IT(hsmartcard, SMARTCARD_CLEAR_OREF);
1358 hsmartcard->ErrorCode |= HAL_SMARTCARD_ERROR_ORE;
1361 /* SMARTCARD receiver timeout interrupt occurred -----------------------------------------*/
1362 if(((isrflags & USART_ISR_RTOF) != RESET) && ((cr1its & USART_CR1_RTOIE) != RESET))
1364 __HAL_SMARTCARD_CLEAR_IT(hsmartcard, SMARTCARD_CLEAR_RTOF);
1366 hsmartcard->ErrorCode |= HAL_SMARTCARD_ERROR_RTO;
1369 /* Call SMARTCARD Error Call back function if need be --------------------------*/
1370 if(hsmartcard->ErrorCode != HAL_SMARTCARD_ERROR_NONE)
1372 /* SMARTCARD in mode Receiver ---------------------------------------------------*/
1373 if(((isrflags & USART_ISR_RXNE) != RESET) && ((cr1its & USART_CR1_RXNEIE) != RESET))
1375 SMARTCARD_Receive_IT(hsmartcard);
1378 /* If Error is to be considered as blocking :
1379 - Receiver Timeout error in Reception
1380 - Overrun error in Reception
1381 - any error occurs in DMA mode reception
1383 if ( ((hsmartcard->ErrorCode & (HAL_SMARTCARD_ERROR_RTO | HAL_SMARTCARD_ERROR_ORE)) != RESET)
1384 || (HAL_IS_BIT_SET(hsmartcard->Instance->CR3, USART_CR3_DMAR)))
1386 /* Blocking error : transfer is aborted
1387 Set the SMARTCARD state ready to be able to start again the process,
1388 Disable Rx Interrupts, and disable Rx DMA request, if ongoing */
1389 SMARTCARD_EndRxTransfer(hsmartcard);
1391 /* Disable the SMARTCARD DMA Rx request if enabled */
1392 if (HAL_IS_BIT_SET(hsmartcard->Instance->CR3, USART_CR3_DMAR))
1394 CLEAR_BIT(hsmartcard->Instance->CR3, USART_CR3_DMAR);
1396 /* Abort the SMARTCARD DMA Rx channel */
1397 if(hsmartcard->hdmarx != NULL)
1399 /* Set the SMARTCARD DMA Abort callback :
1400 will lead to call HAL_SMARTCARD_ErrorCallback() at end of DMA abort procedure */
1401 hsmartcard->hdmarx->XferAbortCallback = SMARTCARD_DMAAbortOnError;
1403 /* Abort DMA RX */
1404 if(HAL_DMA_Abort_IT(hsmartcard->hdmarx) != HAL_OK)
1406 /* Call Directly hsmartcard->hdmarx->XferAbortCallback function in case of error */
1407 hsmartcard->hdmarx->XferAbortCallback(hsmartcard->hdmarx);
1410 else
1412 /* Call user error callback */
1413 HAL_SMARTCARD_ErrorCallback(hsmartcard);
1416 else
1418 /* Call user error callback */
1419 HAL_SMARTCARD_ErrorCallback(hsmartcard);
1422 /* other error type to be considered as blocking :
1423 - Frame error in Transmission
1425 else if ((hsmartcard->gState == HAL_SMARTCARD_STATE_BUSY_TX) && ((hsmartcard->ErrorCode & HAL_SMARTCARD_ERROR_FE) != RESET))
1427 /* Blocking error : transfer is aborted
1428 Set the SMARTCARD state ready to be able to start again the process,
1429 Disable Tx Interrupts, and disable Tx DMA request, if ongoing */
1430 SMARTCARD_EndTxTransfer(hsmartcard);
1432 /* Disable the SMARTCARD DMA Tx request if enabled */
1433 if (HAL_IS_BIT_SET(hsmartcard->Instance->CR3, USART_CR3_DMAT))
1435 CLEAR_BIT(hsmartcard->Instance->CR3, USART_CR3_DMAT);
1437 /* Abort the SMARTCARD DMA Tx channel */
1438 if(hsmartcard->hdmatx != NULL)
1440 /* Set the SMARTCARD DMA Abort callback :
1441 will lead to call HAL_SMARTCARD_ErrorCallback() at end of DMA abort procedure */
1442 hsmartcard->hdmatx->XferAbortCallback = SMARTCARD_DMAAbortOnError;
1444 /* Abort DMA TX */
1445 if(HAL_DMA_Abort_IT(hsmartcard->hdmatx) != HAL_OK)
1447 /* Call Directly hsmartcard->hdmatx->XferAbortCallback function in case of error */
1448 hsmartcard->hdmatx->XferAbortCallback(hsmartcard->hdmatx);
1451 else
1453 /* Call user error callback */
1454 HAL_SMARTCARD_ErrorCallback(hsmartcard);
1457 else
1459 /* Call user error callback */
1460 HAL_SMARTCARD_ErrorCallback(hsmartcard);
1463 else
1465 /* Non Blocking error : transfer could go on.
1466 Error is notified to user through user error callback */
1467 HAL_SMARTCARD_ErrorCallback(hsmartcard);
1468 hsmartcard->ErrorCode = HAL_SMARTCARD_ERROR_NONE;
1471 return;
1473 } /* End if some error occurs */
1475 /* SMARTCARD in mode Receiver, end of block interruption ------------------------*/
1476 if(((isrflags & USART_ISR_EOBF) != RESET) && ((cr1its & USART_CR1_EOBIE) != RESET))
1478 hsmartcard->RxState = HAL_SMARTCARD_STATE_READY;
1479 __HAL_UNLOCK(hsmartcard);
1480 HAL_SMARTCARD_RxCpltCallback(hsmartcard);
1481 /* Clear EOBF interrupt after HAL_SMARTCARD_RxCpltCallback() call for the End of Block information
1482 * to be available during HAL_SMARTCARD_RxCpltCallback() processing */
1483 __HAL_SMARTCARD_CLEAR_IT(hsmartcard, SMARTCARD_CLEAR_EOBF);
1484 return;
1487 /* SMARTCARD in mode Transmitter ------------------------------------------------*/
1488 if(((isrflags & USART_ISR_TXE) != RESET) && ((cr1its & USART_CR1_TXEIE) != RESET))
1490 SMARTCARD_Transmit_IT(hsmartcard);
1491 return;
1494 /* SMARTCARD in mode Transmitter (transmission end) ------------------------*/
1495 if((__HAL_SMARTCARD_GET_IT(hsmartcard, hsmartcard->AdvancedInit.TxCompletionIndication) != RESET) &&(__HAL_SMARTCARD_GET_IT_SOURCE(hsmartcard, hsmartcard->AdvancedInit.TxCompletionIndication) != RESET))
1497 SMARTCARD_EndTransmit_IT(hsmartcard);
1498 return;
1503 * @brief Tx Transfer completed callback.
1504 * @param hsmartcard Pointer to a SMARTCARD_HandleTypeDef structure that contains
1505 * the configuration information for the specified SMARTCARD module.
1506 * @retval None
1508 __weak void HAL_SMARTCARD_TxCpltCallback(SMARTCARD_HandleTypeDef *hsmartcard)
1510 /* Prevent unused argument(s) compilation warning */
1511 UNUSED(hsmartcard);
1513 /* NOTE : This function should not be modified, when the callback is needed,
1514 the HAL_SMARTCARD_TxCpltCallback can be implemented in the user file.
1519 * @brief Rx Transfer completed callback.
1520 * @param hsmartcard Pointer to a SMARTCARD_HandleTypeDef structure that contains
1521 * the configuration information for the specified SMARTCARD module.
1522 * @retval None
1524 __weak void HAL_SMARTCARD_RxCpltCallback(SMARTCARD_HandleTypeDef *hsmartcard)
1526 /* Prevent unused argument(s) compilation warning */
1527 UNUSED(hsmartcard);
1529 /* NOTE : This function should not be modified, when the callback is needed,
1530 the HAL_SMARTCARD_RxCpltCallback can be implemented in the user file.
1535 * @brief SMARTCARD error callback.
1536 * @param hsmartcard Pointer to a SMARTCARD_HandleTypeDef structure that contains
1537 * the configuration information for the specified SMARTCARD module.
1538 * @retval None
1540 __weak void HAL_SMARTCARD_ErrorCallback(SMARTCARD_HandleTypeDef *hsmartcard)
1542 /* Prevent unused argument(s) compilation warning */
1543 UNUSED(hsmartcard);
1545 /* NOTE : This function should not be modified, when the callback is needed,
1546 the HAL_SMARTCARD_ErrorCallback can be implemented in the user file.
1551 * @brief SMARTCARD Abort Complete callback.
1552 * @param hsmartcard Pointer to a SMARTCARD_HandleTypeDef structure that contains
1553 * the configuration information for the specified SMARTCARD module.
1554 * @retval None
1556 __weak void HAL_SMARTCARD_AbortCpltCallback (SMARTCARD_HandleTypeDef *hsmartcard)
1558 /* Prevent unused argument(s) compilation warning */
1559 UNUSED(hsmartcard);
1561 /* NOTE : This function should not be modified, when the callback is needed,
1562 the HAL_SMARTCARD_AbortCpltCallback can be implemented in the user file.
1567 * @brief SMARTCARD Abort Complete callback.
1568 * @param hsmartcard Pointer to a SMARTCARD_HandleTypeDef structure that contains
1569 * the configuration information for the specified SMARTCARD module.
1570 * @retval None
1572 __weak void HAL_SMARTCARD_AbortTransmitCpltCallback (SMARTCARD_HandleTypeDef *hsmartcard)
1574 /* Prevent unused argument(s) compilation warning */
1575 UNUSED(hsmartcard);
1577 /* NOTE : This function should not be modified, when the callback is needed,
1578 the HAL_SMARTCARD_AbortTransmitCpltCallback can be implemented in the user file.
1583 * @brief SMARTCARD Abort Receive Complete callback.
1584 * @param hsmartcard Pointer to a SMARTCARD_HandleTypeDef structure that contains
1585 * the configuration information for the specified SMARTCARD module.
1586 * @retval None
1588 __weak void HAL_SMARTCARD_AbortReceiveCpltCallback (SMARTCARD_HandleTypeDef *hsmartcard)
1590 /* Prevent unused argument(s) compilation warning */
1591 UNUSED(hsmartcard);
1593 /* NOTE : This function should not be modified, when the callback is needed,
1594 the HAL_SMARTCARD_AbortReceiveCpltCallback can be implemented in the user file.
1599 * @}
1602 /** @defgroup SMARTCARD_Exported_Functions_Group4 Peripheral State and Errors functions
1603 * @brief SMARTCARD State and Errors functions
1605 @verbatim
1606 ==============================================================================
1607 ##### Peripheral State and Errors functions #####
1608 ==============================================================================
1609 [..]
1610 This subsection provides a set of functions allowing to return the State of SmartCard
1611 handle and also return Peripheral Errors occurred during communication process
1612 (+) HAL_SMARTCARD_GetState() API can be helpful to check in run-time the state
1613 of the SMARTCARD peripheral.
1614 (+) HAL_SMARTCARD_GetError() checks in run-time errors that could occur during
1615 communication.
1617 @endverbatim
1618 * @{
1622 * @brief Return the SMARTCARD handle state.
1623 * @param hsmartcard Pointer to a SMARTCARD_HandleTypeDef structure that contains
1624 * the configuration information for the specified SMARTCARD module.
1625 * @retval SMARTCARD handle state
1627 HAL_SMARTCARD_StateTypeDef HAL_SMARTCARD_GetState(SMARTCARD_HandleTypeDef *hsmartcard)
1629 /* Return SMARTCARD handle state */
1630 uint32_t temp1= 0x00, temp2 = 0x00;
1631 temp1 = hsmartcard->gState;
1632 temp2 = hsmartcard->RxState;
1634 return (HAL_SMARTCARD_StateTypeDef)(temp1 | temp2);
1638 * @brief Return the SMARTCARD handle error code.
1639 * @param hsmartcard Pointer to a SMARTCARD_HandleTypeDef structure that contains
1640 * the configuration information for the specified SMARTCARD module.
1641 * @retval SMARTCARD handle Error Code
1643 uint32_t HAL_SMARTCARD_GetError(SMARTCARD_HandleTypeDef *hsmartcard)
1645 return hsmartcard->ErrorCode;
1649 * @}
1653 * @}
1656 /** @defgroup SMARTCARD_Private_Functions SMARTCARD Private Functions
1657 * @{
1661 * @brief Configure the SMARTCARD associated USART peripheral.
1662 * @param hsmartcard: Pointer to a SMARTCARD_HandleTypeDef structure that contains
1663 * the configuration information for the specified SMARTCARD module.
1664 * @retval None
1666 static HAL_StatusTypeDef SMARTCARD_SetConfig(SMARTCARD_HandleTypeDef *hsmartcard)
1668 uint32_t tmpreg = 0x00000000;
1669 SMARTCARD_ClockSourceTypeDef clocksource = SMARTCARD_CLOCKSOURCE_UNDEFINED;
1670 HAL_StatusTypeDef ret = HAL_OK;
1672 /* Check the parameters */
1673 assert_param(IS_SMARTCARD_INSTANCE(hsmartcard->Instance));
1674 assert_param(IS_SMARTCARD_BAUDRATE(hsmartcard->Init.BaudRate));
1675 assert_param(IS_SMARTCARD_WORD_LENGTH(hsmartcard->Init.WordLength));
1676 assert_param(IS_SMARTCARD_STOPBITS(hsmartcard->Init.StopBits));
1677 assert_param(IS_SMARTCARD_PARITY(hsmartcard->Init.Parity));
1678 assert_param(IS_SMARTCARD_MODE(hsmartcard->Init.Mode));
1679 assert_param(IS_SMARTCARD_POLARITY(hsmartcard->Init.CLKPolarity));
1680 assert_param(IS_SMARTCARD_PHASE(hsmartcard->Init.CLKPhase));
1681 assert_param(IS_SMARTCARD_LASTBIT(hsmartcard->Init.CLKLastBit));
1682 assert_param(IS_SMARTCARD_ONE_BIT_SAMPLE(hsmartcard->Init.OneBitSampling));
1683 assert_param(IS_SMARTCARD_NACK(hsmartcard->Init.NACKEnable));
1684 assert_param(IS_SMARTCARD_TIMEOUT(hsmartcard->Init.TimeOutEnable));
1685 assert_param(IS_SMARTCARD_AUTORETRY_COUNT(hsmartcard->Init.AutoRetryCount));
1687 /*-------------------------- USART CR1 Configuration -----------------------*/
1688 /* In SmartCard mode, M and PCE are forced to 1 (8 bits + parity).
1689 * Oversampling is forced to 16 (OVER8 = 0).
1690 * Configure the Parity and Mode:
1691 * set PS bit according to hsmartcard->Init.Parity value
1692 * set TE and RE bits according to hsmartcard->Init.Mode value */
1693 tmpreg = (uint32_t) hsmartcard->Init.Parity | hsmartcard->Init.Mode;
1694 tmpreg |= (uint32_t) hsmartcard->Init.WordLength;
1695 MODIFY_REG(hsmartcard->Instance->CR1, USART_CR1_FIELDS, tmpreg);
1697 /*-------------------------- USART CR2 Configuration -----------------------*/
1698 tmpreg = hsmartcard->Init.StopBits;
1699 /* Synchronous mode is activated by default */
1700 tmpreg |= (uint32_t) USART_CR2_CLKEN | hsmartcard->Init.CLKPolarity;
1701 tmpreg |= (uint32_t) hsmartcard->Init.CLKPhase | hsmartcard->Init.CLKLastBit;
1702 tmpreg |= (uint32_t) hsmartcard->Init.TimeOutEnable;
1703 MODIFY_REG(hsmartcard->Instance->CR2, USART_CR2_FIELDS, tmpreg);
1705 /*-------------------------- USART CR3 Configuration -----------------------*/
1706 /* Configure
1707 * - one-bit sampling method versus three samples' majority rule
1708 * according to hsmartcard->Init.OneBitSampling
1709 * - NACK transmission in case of parity error according
1710 * to hsmartcard->Init.NACKEnable
1711 * - autoretry counter according to hsmartcard->Init.AutoRetryCount */
1712 tmpreg = (uint32_t) hsmartcard->Init.OneBitSampling | hsmartcard->Init.NACKEnable;
1713 tmpreg |= ((uint32_t)hsmartcard->Init.AutoRetryCount << SMARTCARD_CR3_SCARCNT_LSB_POS);
1714 MODIFY_REG(hsmartcard->Instance-> CR3,USART_CR3_FIELDS, tmpreg);
1716 /*-------------------------- USART GTPR Configuration ----------------------*/
1717 tmpreg = (hsmartcard->Init.Prescaler | ((uint32_t)hsmartcard->Init.GuardTime << SMARTCARD_GTPR_GT_LSB_POS));
1718 MODIFY_REG(hsmartcard->Instance->GTPR, (USART_GTPR_GT|USART_GTPR_PSC), tmpreg);
1720 /*-------------------------- USART RTOR Configuration ----------------------*/
1721 tmpreg = ((uint32_t)hsmartcard->Init.BlockLength << SMARTCARD_RTOR_BLEN_LSB_POS);
1722 if (hsmartcard->Init.TimeOutEnable == SMARTCARD_TIMEOUT_ENABLE)
1724 assert_param(IS_SMARTCARD_TIMEOUT_VALUE(hsmartcard->Init.TimeOutValue));
1725 tmpreg |= (uint32_t) hsmartcard->Init.TimeOutValue;
1727 MODIFY_REG(hsmartcard->Instance->RTOR, (USART_RTOR_RTO|USART_RTOR_BLEN), tmpreg);
1729 /*-------------------------- USART BRR Configuration -----------------------*/
1730 SMARTCARD_GETCLOCKSOURCE(hsmartcard, clocksource);
1731 switch (clocksource)
1733 case SMARTCARD_CLOCKSOURCE_PCLK1:
1734 hsmartcard->Instance->BRR = (uint16_t)((HAL_RCC_GetPCLK1Freq() + (hsmartcard->Init.BaudRate/2)) / hsmartcard->Init.BaudRate);
1735 break;
1736 case SMARTCARD_CLOCKSOURCE_PCLK2:
1737 hsmartcard->Instance->BRR = (uint16_t)((HAL_RCC_GetPCLK2Freq() + (hsmartcard->Init.BaudRate/2)) / hsmartcard->Init.BaudRate);
1738 break;
1739 case SMARTCARD_CLOCKSOURCE_HSI:
1740 hsmartcard->Instance->BRR = (uint16_t)((HSI_VALUE + (hsmartcard->Init.BaudRate/2)) / hsmartcard->Init.BaudRate);
1741 break;
1742 case SMARTCARD_CLOCKSOURCE_SYSCLK:
1743 hsmartcard->Instance->BRR = (uint16_t)((HAL_RCC_GetSysClockFreq() + (hsmartcard->Init.BaudRate/2)) / hsmartcard->Init.BaudRate);
1744 break;
1745 case SMARTCARD_CLOCKSOURCE_LSE:
1746 hsmartcard->Instance->BRR = (uint16_t)((LSE_VALUE + (hsmartcard->Init.BaudRate/2)) / hsmartcard->Init.BaudRate);
1747 break;
1748 case SMARTCARD_CLOCKSOURCE_UNDEFINED:
1749 default:
1750 ret = HAL_ERROR;
1751 break;
1754 return ret;
1759 * @brief Configure the SMARTCARD associated USART peripheral advanced features.
1760 * @param hsmartcard: Pointer to a SMARTCARD_HandleTypeDef structure that contains
1761 * the configuration information for the specified SMARTCARD module.
1762 * @retval None
1764 static void SMARTCARD_AdvFeatureConfig(SMARTCARD_HandleTypeDef *hsmartcard)
1766 /* Check whether the set of advanced features to configure is properly set */
1767 assert_param(IS_SMARTCARD_ADVFEATURE_INIT(hsmartcard->AdvancedInit.AdvFeatureInit));
1769 /* if required, configure TX pin active level inversion */
1770 if (HAL_IS_BIT_SET(hsmartcard->AdvancedInit.AdvFeatureInit, SMARTCARD_ADVFEATURE_TXINVERT_INIT))
1772 assert_param(IS_SMARTCARD_ADVFEATURE_TXINV(hsmartcard->AdvancedInit.TxPinLevelInvert));
1773 MODIFY_REG(hsmartcard->Instance->CR2, USART_CR2_TXINV, hsmartcard->AdvancedInit.TxPinLevelInvert);
1776 /* if required, configure RX pin active level inversion */
1777 if (HAL_IS_BIT_SET(hsmartcard->AdvancedInit.AdvFeatureInit, SMARTCARD_ADVFEATURE_RXINVERT_INIT))
1779 assert_param(IS_SMARTCARD_ADVFEATURE_RXINV(hsmartcard->AdvancedInit.RxPinLevelInvert));
1780 MODIFY_REG(hsmartcard->Instance->CR2, USART_CR2_RXINV, hsmartcard->AdvancedInit.RxPinLevelInvert);
1783 /* if required, configure data inversion */
1784 if (HAL_IS_BIT_SET(hsmartcard->AdvancedInit.AdvFeatureInit, SMARTCARD_ADVFEATURE_DATAINVERT_INIT))
1786 assert_param(IS_SMARTCARD_ADVFEATURE_DATAINV(hsmartcard->AdvancedInit.DataInvert));
1787 MODIFY_REG(hsmartcard->Instance->CR2, USART_CR2_DATAINV, hsmartcard->AdvancedInit.DataInvert);
1790 /* if required, configure RX/TX pins swap */
1791 if (HAL_IS_BIT_SET(hsmartcard->AdvancedInit.AdvFeatureInit, SMARTCARD_ADVFEATURE_SWAP_INIT))
1793 assert_param(IS_SMARTCARD_ADVFEATURE_SWAP(hsmartcard->AdvancedInit.Swap));
1794 MODIFY_REG(hsmartcard->Instance->CR2, USART_CR2_SWAP, hsmartcard->AdvancedInit.Swap);
1797 /* if required, configure RX overrun detection disabling */
1798 if (HAL_IS_BIT_SET(hsmartcard->AdvancedInit.AdvFeatureInit, SMARTCARD_ADVFEATURE_RXOVERRUNDISABLE_INIT))
1800 assert_param(IS_SMARTCARD_OVERRUN(hsmartcard->AdvancedInit.OverrunDisable));
1801 MODIFY_REG(hsmartcard->Instance->CR3, USART_CR3_OVRDIS, hsmartcard->AdvancedInit.OverrunDisable);
1804 /* if required, configure DMA disabling on reception error */
1805 if (HAL_IS_BIT_SET(hsmartcard->AdvancedInit.AdvFeatureInit, SMARTCARD_ADVFEATURE_DMADISABLEONERROR_INIT))
1807 assert_param(IS_SMARTCARD_ADVFEATURE_DMAONRXERROR(hsmartcard->AdvancedInit.DMADisableonRxError));
1808 MODIFY_REG(hsmartcard->Instance->CR3, USART_CR3_DDRE, hsmartcard->AdvancedInit.DMADisableonRxError);
1811 /* if required, configure MSB first on communication line */
1812 if (HAL_IS_BIT_SET(hsmartcard->AdvancedInit.AdvFeatureInit, SMARTCARD_ADVFEATURE_MSBFIRST_INIT))
1814 assert_param(IS_SMARTCARD_ADVFEATURE_MSBFIRST(hsmartcard->AdvancedInit.MSBFirst));
1815 MODIFY_REG(hsmartcard->Instance->CR2, USART_CR2_MSBFIRST, hsmartcard->AdvancedInit.MSBFirst);
1821 * @brief Check the SMARTCARD Idle State.
1822 * @param hsmartcard: Pointer to a SMARTCARD_HandleTypeDef structure that contains
1823 * the configuration information for the specified SMARTCARD module.
1824 * @retval HAL status
1826 static HAL_StatusTypeDef SMARTCARD_CheckIdleState(SMARTCARD_HandleTypeDef *hsmartcard)
1828 uint32_t tickstart = 0;
1830 /* Initialize the SMARTCARD ErrorCode */
1831 hsmartcard->ErrorCode = HAL_SMARTCARD_ERROR_NONE;
1833 /* Init tickstart for timeout managment*/
1834 tickstart = HAL_GetTick();
1836 /* Check if the Transmitter is enabled */
1837 if((hsmartcard->Instance->CR1 & USART_CR1_TE) == USART_CR1_TE)
1839 /* Wait until TEACK flag is set */
1840 if(SMARTCARD_WaitOnFlagUntilTimeout(hsmartcard, USART_ISR_TEACK, RESET, tickstart, SMARTCARD_TEACK_REACK_TIMEOUT) != HAL_OK)
1842 /* Timeout occurred */
1843 return HAL_TIMEOUT;
1847 /* Initialize the SMARTCARD states */
1848 hsmartcard->gState = HAL_SMARTCARD_STATE_READY;
1849 hsmartcard->RxState = HAL_SMARTCARD_STATE_READY;
1851 /* Process Unlocked */
1852 __HAL_UNLOCK(hsmartcard);
1854 return HAL_OK;
1858 * @brief Handle SMARTCARD Communication Timeout.
1859 * @param hsmartcard Pointer to a SMARTCARD_HandleTypeDef structure that contains
1860 * the configuration information for the specified SMARTCARD module.
1861 * @param Flag Specifies the SMARTCARD flag to check.
1862 * @param Status The new Flag status (SET or RESET).
1863 * @param Tickstart Tick start value
1864 * @param Timeout Timeout duration.
1865 * @retval HAL status
1867 static HAL_StatusTypeDef SMARTCARD_WaitOnFlagUntilTimeout(SMARTCARD_HandleTypeDef *hsmartcard, uint32_t Flag, FlagStatus Status, uint32_t Tickstart, uint32_t Timeout)
1869 /* Wait until flag is set */
1870 while((__HAL_SMARTCARD_GET_FLAG(hsmartcard, Flag) ? SET : RESET) == Status)
1872 /* Check for the Timeout */
1873 if(Timeout != HAL_MAX_DELAY)
1875 if((Timeout == 0) || ((HAL_GetTick()-Tickstart) > Timeout))
1877 /* Disable TXE, RXNE, PE and ERR (Frame error, noise error, overrun error) interrupts for the interrupt process */
1878 CLEAR_BIT(hsmartcard->Instance->CR1, (USART_CR1_RXNEIE | USART_CR1_PEIE | USART_CR1_TXEIE));
1879 CLEAR_BIT(hsmartcard->Instance->CR3, USART_CR3_EIE);
1881 hsmartcard->gState = HAL_SMARTCARD_STATE_READY;
1882 hsmartcard->RxState = HAL_SMARTCARD_STATE_READY;
1884 /* Process Unlocked */
1885 __HAL_UNLOCK(hsmartcard);
1886 return HAL_TIMEOUT;
1890 return HAL_OK;
1895 * @brief End ongoing Tx transfer on SMARTCARD peripheral (following error detection or Transmit completion).
1896 * @param hsmartcard Pointer to a SMARTCARD_HandleTypeDef structure that contains
1897 * the configuration information for the specified SMARTCARD module.
1898 * @retval None
1900 static void SMARTCARD_EndTxTransfer(SMARTCARD_HandleTypeDef *hsmartcard)
1902 /* Disable TXEIE, TCIE and ERR (Frame error, noise error, overrun error) interrupts */
1903 CLEAR_BIT(hsmartcard->Instance->CR1, (USART_CR1_TXEIE | USART_CR1_TCIE));
1904 CLEAR_BIT(hsmartcard->Instance->CR3, USART_CR3_EIE);
1906 /* At end of Tx process, restore hsmartcard->gState to Ready */
1907 hsmartcard->gState = HAL_SMARTCARD_STATE_READY;
1912 * @brief End ongoing Rx transfer on UART peripheral (following error detection or Reception completion).
1913 * @param hsmartcard Pointer to a SMARTCARD_HandleTypeDef structure that contains
1914 * the configuration information for the specified SMARTCARD module.
1915 * @retval None
1917 static void SMARTCARD_EndRxTransfer(SMARTCARD_HandleTypeDef *hsmartcard)
1919 /* Disable RXNE, PE and ERR (Frame error, noise error, overrun error) interrupts */
1920 CLEAR_BIT(hsmartcard->Instance->CR1, (USART_CR1_RXNEIE | USART_CR1_PEIE));
1921 CLEAR_BIT(hsmartcard->Instance->CR3, USART_CR3_EIE);
1923 /* At end of Rx process, restore hsmartcard->RxState to Ready */
1924 hsmartcard->RxState = HAL_SMARTCARD_STATE_READY;
1929 * @brief DMA SMARTCARD transmit process complete callback.
1930 * @param hdma Pointer to a DMA_HandleTypeDef structure that contains
1931 * the configuration information for the specified DMA module.
1932 * @retval None
1934 static void SMARTCARD_DMATransmitCplt(DMA_HandleTypeDef *hdma)
1936 SMARTCARD_HandleTypeDef* hsmartcard = (SMARTCARD_HandleTypeDef*)(hdma->Parent);
1937 hsmartcard->TxXferCount = 0;
1939 /* Disable the DMA transfer for transmit request by resetting the DMAT bit
1940 in the SMARTCARD associated USART CR3 register */
1941 CLEAR_BIT(hsmartcard->Instance->CR3, USART_CR3_DMAT);
1943 /* Enable the SMARTCARD Transmit Complete Interrupt */
1944 __HAL_SMARTCARD_ENABLE_IT(hsmartcard, hsmartcard->AdvancedInit.TxCompletionIndication);
1948 * @brief DMA SMARTCARD receive process complete callback.
1949 * @param hdma Pointer to a DMA_HandleTypeDef structure that contains
1950 * the configuration information for the specified DMA module.
1951 * @retval None
1953 static void SMARTCARD_DMAReceiveCplt(DMA_HandleTypeDef *hdma)
1955 SMARTCARD_HandleTypeDef* hsmartcard = (SMARTCARD_HandleTypeDef*)(hdma->Parent);
1956 hsmartcard->RxXferCount = 0;
1958 /* Disable PE and ERR (Frame error, noise error, overrun error) interrupts */
1959 CLEAR_BIT(hsmartcard->Instance->CR1, USART_CR1_PEIE);
1960 CLEAR_BIT(hsmartcard->Instance->CR3, USART_CR3_EIE);
1962 /* Disable the DMA transfer for the receiver request by resetting the DMAR bit
1963 in the SMARTCARD associated USART CR3 register */
1964 CLEAR_BIT(hsmartcard->Instance->CR3, USART_CR3_DMAR);
1966 /* At end of Rx process, restore hsmartcard->RxState to Ready */
1967 hsmartcard->RxState = HAL_SMARTCARD_STATE_READY;
1969 HAL_SMARTCARD_RxCpltCallback(hsmartcard);
1973 * @brief DMA SMARTCARD communication error callback.
1974 * @param hdma Pointer to a DMA_HandleTypeDef structure that contains
1975 * the configuration information for the specified DMA module.
1976 * @retval None
1978 static void SMARTCARD_DMAError(DMA_HandleTypeDef *hdma)
1980 SMARTCARD_HandleTypeDef* hsmartcard = (SMARTCARD_HandleTypeDef*)(hdma->Parent);
1982 /* Stop SMARTCARD DMA Tx request if ongoing */
1983 if ( (hsmartcard->gState == HAL_SMARTCARD_STATE_BUSY_TX)
1984 &&(HAL_IS_BIT_SET(hsmartcard->Instance->CR3, USART_CR3_DMAT)) )
1986 hsmartcard->TxXferCount = 0;
1987 SMARTCARD_EndTxTransfer(hsmartcard);
1990 /* Stop SMARTCARD DMA Rx request if ongoing */
1991 if ( (hsmartcard->RxState == HAL_SMARTCARD_STATE_BUSY_RX)
1992 &&(HAL_IS_BIT_SET(hsmartcard->Instance->CR3, USART_CR3_DMAR)) )
1994 hsmartcard->RxXferCount = 0;
1995 SMARTCARD_EndRxTransfer(hsmartcard);
1998 hsmartcard->ErrorCode |= HAL_SMARTCARD_ERROR_DMA;
1999 HAL_SMARTCARD_ErrorCallback(hsmartcard);
2003 * @brief DMA SMARTCARD communication abort callback, when initiated by HAL services on Error
2004 * (To be called at end of DMA Abort procedure following error occurrence).
2005 * @param hdma DMA handle.
2006 * @retval None
2008 static void SMARTCARD_DMAAbortOnError(DMA_HandleTypeDef *hdma)
2010 SMARTCARD_HandleTypeDef* hsmartcard = (SMARTCARD_HandleTypeDef*)(hdma->Parent);
2011 hsmartcard->RxXferCount = 0;
2012 hsmartcard->TxXferCount = 0;
2014 HAL_SMARTCARD_ErrorCallback(hsmartcard);
2018 * @brief DMA SMARTCARD Tx communication abort callback, when initiated by user
2019 * (To be called at end of DMA Tx Abort procedure following user abort request).
2020 * @note When this callback is executed, User Abort complete call back is called only if no
2021 * Abort still ongoing for Rx DMA Handle.
2022 * @param hdma DMA handle.
2023 * @retval None
2025 static void SMARTCARD_DMATxAbortCallback(DMA_HandleTypeDef *hdma)
2027 SMARTCARD_HandleTypeDef* hsmartcard = (SMARTCARD_HandleTypeDef* )(hdma->Parent);
2029 hsmartcard->hdmatx->XferAbortCallback = NULL;
2031 /* Check if an Abort process is still ongoing */
2032 if(hsmartcard->hdmarx != NULL)
2034 if(hsmartcard->hdmarx->XferAbortCallback != NULL)
2036 return;
2040 /* No Abort process still ongoing : All DMA channels are aborted, call user Abort Complete callback */
2041 hsmartcard->TxXferCount = 0;
2042 hsmartcard->RxXferCount = 0;
2044 /* Reset errorCode */
2045 hsmartcard->ErrorCode = HAL_SMARTCARD_ERROR_NONE;
2047 /* Clear the Error flags in the ICR register */
2048 __HAL_SMARTCARD_CLEAR_FLAG(hsmartcard, SMARTCARD_CLEAR_OREF | SMARTCARD_CLEAR_NEF | SMARTCARD_CLEAR_PEF | SMARTCARD_CLEAR_FEF | SMARTCARD_CLEAR_RTOF | SMARTCARD_CLEAR_EOBF);
2050 /* Restore hsmartcard->gState and hsmartcard->RxState to Ready */
2051 hsmartcard->gState = HAL_SMARTCARD_STATE_READY;
2052 hsmartcard->RxState = HAL_SMARTCARD_STATE_READY;
2054 /* Call user Abort complete callback */
2055 HAL_SMARTCARD_AbortCpltCallback(hsmartcard);
2060 * @brief DMA SMARTCARD Rx communication abort callback, when initiated by user
2061 * (To be called at end of DMA Rx Abort procedure following user abort request).
2062 * @note When this callback is executed, User Abort complete call back is called only if no
2063 * Abort still ongoing for Tx DMA Handle.
2064 * @param hdma DMA handle.
2065 * @retval None
2067 static void SMARTCARD_DMARxAbortCallback(DMA_HandleTypeDef *hdma)
2069 SMARTCARD_HandleTypeDef* hsmartcard = (SMARTCARD_HandleTypeDef* )(hdma->Parent);
2071 hsmartcard->hdmarx->XferAbortCallback = NULL;
2073 /* Check if an Abort process is still ongoing */
2074 if(hsmartcard->hdmatx != NULL)
2076 if(hsmartcard->hdmatx->XferAbortCallback != NULL)
2078 return;
2082 /* No Abort process still ongoing : All DMA channels are aborted, call user Abort Complete callback */
2083 hsmartcard->TxXferCount = 0;
2084 hsmartcard->RxXferCount = 0;
2086 /* Reset errorCode */
2087 hsmartcard->ErrorCode = HAL_SMARTCARD_ERROR_NONE;
2089 /* Clear the Error flags in the ICR register */
2090 __HAL_SMARTCARD_CLEAR_FLAG(hsmartcard, SMARTCARD_CLEAR_OREF | SMARTCARD_CLEAR_NEF | SMARTCARD_CLEAR_PEF | SMARTCARD_CLEAR_FEF | SMARTCARD_CLEAR_RTOF | SMARTCARD_CLEAR_EOBF);
2092 /* Restore hsmartcard->gState and hsmartcard->RxState to Ready */
2093 hsmartcard->gState = HAL_SMARTCARD_STATE_READY;
2094 hsmartcard->RxState = HAL_SMARTCARD_STATE_READY;
2096 /* Call user Abort complete callback */
2097 HAL_SMARTCARD_AbortCpltCallback(hsmartcard);
2102 * @brief DMA SMARTCARD Tx communication abort callback, when initiated by user by a call to
2103 * HAL_SMARTCARD_AbortTransmit_IT API (Abort only Tx transfer)
2104 * (This callback is executed at end of DMA Tx Abort procedure following user abort request,
2105 * and leads to user Tx Abort Complete callback execution).
2106 * @param hdma DMA handle.
2107 * @retval None
2109 static void SMARTCARD_DMATxOnlyAbortCallback(DMA_HandleTypeDef *hdma)
2111 SMARTCARD_HandleTypeDef* hsmartcard = (SMARTCARD_HandleTypeDef*)(hdma->Parent);
2113 hsmartcard->TxXferCount = 0;
2115 /* Clear the Error flags in the ICR register */
2116 __HAL_SMARTCARD_CLEAR_FLAG(hsmartcard, SMARTCARD_CLEAR_FEF);
2118 /* Restore hsmartcard->gState to Ready */
2119 hsmartcard->gState = HAL_SMARTCARD_STATE_READY;
2121 /* Call user Abort complete callback */
2122 HAL_SMARTCARD_AbortTransmitCpltCallback(hsmartcard);
2126 * @brief DMA SMARTCARD Rx communication abort callback, when initiated by user by a call to
2127 * HAL_SMARTCARD_AbortReceive_IT API (Abort only Rx transfer)
2128 * (This callback is executed at end of DMA Rx Abort procedure following user abort request,
2129 * and leads to user Rx Abort Complete callback execution).
2130 * @param hdma DMA handle.
2131 * @retval None
2133 static void SMARTCARD_DMARxOnlyAbortCallback(DMA_HandleTypeDef *hdma)
2135 SMARTCARD_HandleTypeDef* hsmartcard = ( SMARTCARD_HandleTypeDef* )(hdma->Parent);
2137 hsmartcard->RxXferCount = 0;
2139 /* Clear the Error flags in the ICR register */
2140 __HAL_SMARTCARD_CLEAR_FLAG(hsmartcard, SMARTCARD_CLEAR_OREF | SMARTCARD_CLEAR_NEF | SMARTCARD_CLEAR_PEF | SMARTCARD_CLEAR_FEF | SMARTCARD_CLEAR_RTOF | SMARTCARD_CLEAR_EOBF);
2142 /* Restore hsmartcard->RxState to Ready */
2143 hsmartcard->RxState = HAL_SMARTCARD_STATE_READY;
2145 /* Call user Abort complete callback */
2146 HAL_SMARTCARD_AbortReceiveCpltCallback(hsmartcard);
2150 * @brief Send an amount of data in non-blocking mode.
2151 * @param hsmartcard: Pointer to a SMARTCARD_HandleTypeDef structure that contains
2152 * the configuration information for the specified SMARTCARD module.
2153 * Function called under interruption only, once
2154 * interruptions have been enabled by HAL_SMARTCARD_Transmit_IT()
2155 * @retval HAL status
2157 static HAL_StatusTypeDef SMARTCARD_Transmit_IT(SMARTCARD_HandleTypeDef *hsmartcard)
2159 /* Check that a Tx process is ongoing */
2160 if (hsmartcard->gState == HAL_SMARTCARD_STATE_BUSY_TX)
2162 if(hsmartcard->TxXferCount == 0)
2164 /* Disable the SMARTCARD Transmit Data Register Empty Interrupt */
2165 CLEAR_BIT(hsmartcard->Instance->CR1, USART_CR1_TXEIE);
2167 /* Enable the SMARTCARD Transmit Complete Interrupt */
2168 __HAL_SMARTCARD_ENABLE_IT(hsmartcard, hsmartcard->AdvancedInit.TxCompletionIndication);
2170 return HAL_OK;
2172 else
2174 hsmartcard->Instance->TDR = (*hsmartcard->pTxBuffPtr++ & (uint8_t)0xFF);
2175 hsmartcard->TxXferCount--;
2177 return HAL_OK;
2180 else
2182 return HAL_BUSY;
2187 * @brief Wrap up transmission in non-blocking mode.
2188 * @param hsmartcard Pointer to a SMARTCARD_HandleTypeDef structure that contains
2189 * the configuration information for the specified SMARTCARD module.
2190 * @retval HAL status
2192 static HAL_StatusTypeDef SMARTCARD_EndTransmit_IT(SMARTCARD_HandleTypeDef *hsmartcard)
2194 /* Disable the SMARTCARD Transmit Complete Interrupt */
2195 __HAL_SMARTCARD_DISABLE_IT(hsmartcard, hsmartcard->AdvancedInit.TxCompletionIndication);
2197 /* Check if a receive process is ongoing or not. If not disable ERR IT */
2198 if(hsmartcard->RxState == HAL_SMARTCARD_STATE_READY)
2200 /* Disable the SMARTCARD Error Interrupt: (Frame error) */
2201 CLEAR_BIT(hsmartcard->Instance->CR3, USART_CR3_EIE);
2204 /* Re-enable Rx at end of transmission if initial mode is Rx/Tx */
2205 if(hsmartcard->Init.Mode == SMARTCARD_MODE_TX_RX)
2207 /* Disable the Peripheral first to update modes */
2208 CLEAR_BIT(hsmartcard->Instance->CR1, USART_CR1_UE);
2209 SET_BIT(hsmartcard->Instance->CR1, USART_CR1_RE);
2210 /* Enable the Peripheral */
2211 SET_BIT(hsmartcard->Instance->CR1, USART_CR1_UE);
2214 /* Tx process is ended, restore hsmartcard->gState to Ready */
2215 hsmartcard->gState = HAL_SMARTCARD_STATE_READY;
2217 HAL_SMARTCARD_TxCpltCallback(hsmartcard);
2219 return HAL_OK;
2223 * @brief Receive an amount of data in non-blocking mode.
2224 * @param hsmartcard: Pointer to a SMARTCARD_HandleTypeDef structure that contains
2225 * the configuration information for the specified SMARTCARD module.
2226 * Function called under interruption only, once
2227 * interruptions have been enabled by HAL_SMARTCARD_Receive_IT().
2228 * @retval HAL status
2230 static HAL_StatusTypeDef SMARTCARD_Receive_IT(SMARTCARD_HandleTypeDef *hsmartcard)
2232 /* Check that a Rx process is ongoing */
2233 if (hsmartcard->RxState == HAL_SMARTCARD_STATE_BUSY_RX)
2235 *hsmartcard->pRxBuffPtr++ = (uint8_t)(hsmartcard->Instance->RDR & (uint8_t)0xFF);
2237 if(--hsmartcard->RxXferCount == 0)
2239 CLEAR_BIT(hsmartcard->Instance->CR1, USART_CR1_RXNEIE);
2241 /* Check if a transmit process is ongoing or not. If not disable ERR IT */
2242 if(hsmartcard->gState == HAL_SMARTCARD_STATE_READY)
2244 /* Disable the SMARTCARD Error Interrupt: (Frame error, noise error, overrun error) */
2245 CLEAR_BIT(hsmartcard->Instance->CR3, USART_CR3_EIE);
2248 /* Disable the SMARTCARD Parity Error Interrupt */
2249 CLEAR_BIT(hsmartcard->Instance->CR1, USART_CR1_PEIE);
2251 hsmartcard->RxState = HAL_SMARTCARD_STATE_READY;
2253 HAL_SMARTCARD_RxCpltCallback(hsmartcard);
2255 return HAL_OK;
2258 return HAL_OK;
2260 else
2262 /* Clear RXNE interrupt flag */
2263 __HAL_SMARTCARD_SEND_REQ(hsmartcard, SMARTCARD_RXDATA_FLUSH_REQUEST);
2265 return HAL_BUSY;
2270 * @}
2273 #endif /* HAL_SMARTCARD_MODULE_ENABLED */
2275 * @}
2279 * @}
2282 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/