2 ******************************************************************************
3 * @file stm32l0xx_hal_smartcard.c
4 * @author MCD Application Team
6 * @date 06-February-2015
7 * @brief SMARTCARD HAL module driver.
9 * This file provides firmware functions to manage the following
10 * functionalities of the SmartCard.
11 * + Initialization and de-initialization methods
12 * + IO operation methods
13 * + Peripheral Control methods
17 ===============================================================================
18 ##### How to use this driver #####
19 ===============================================================================
21 The SMARTCARD HAL driver can be used as follow:
23 (#) Declare a SMARTCARD_HandleTypeDef handle structure.
24 (#) Associate a USART to the SMARTCARD handle hsc.
25 (#) Initialize the SMARTCARD low level resources by implementing the HAL_SMARTCARD_MspInit ()API:
26 (##) Enable the USARTx interface clock.
27 (##) USART pins configuration:
28 (+) Enable the clock for the USART GPIOs.
29 (+) Configure these USART pins as alternate function pull-up.
30 (##) NVIC configuration if you need to use interrupt process (HAL_SMARTCARD_Transmit_IT()
31 and HAL_SMARTCARD_Receive_IT() APIs):
32 (+) Configure the USARTx interrupt priority.
33 (+) Enable the NVIC USART IRQ handle.
34 (@) The specific USART interrupts (Transmission complete interrupt,
35 RXNE interrupt and Error Interrupts) will be managed using the macros
36 __HAL_SMARTCARD_ENABLE_IT() and __HAL_SMARTCARD_DISABLE_IT() inside the transmit and receive process.
37 (##) DMA Configuration if you need to use DMA process (HAL_SMARTCARD_Transmit_DMA()
38 and HAL_SMARTCARD_Receive_DMA() APIs):
39 (+) Declare a DMA handle structure for the Tx/Rx stream.
40 (+) Enable the DMAx interface clock.
41 (+) Configure the declared DMA handle structure with the required Tx/Rx parameters.
42 (+) Configure the DMA Tx/Rx Stream.
43 (+) Associate the initialized DMA handle to the SMARTCARD DMA Tx/Rx handle.
44 (+) Configure the priority and enable the NVIC for the transfer complete interrupt on the DMA Tx/Rx Stream.
46 (#) Program the Baud Rate, Parity, Mode(Receiver/Transmitter), clock enabling/disabling and accordingly,
47 the clock parameters (parity, phase, last bit), prescaler value, guard time and NACK on transmission
48 error enabling or disabling in the hsc Init structure.
50 (#) If required, program SMARTCARD advanced features (TX/RX pins swap, TimeOut, auto-retry counter,...)
51 in the hsc AdvancedInit structure.
53 (#) Initialize the SMARTCARD associated USART registers by calling
54 the HAL_SMARTCARD_Init() API.
56 (@) HAL_SMARTCARD_Init() API also configure also the low level Hardware GPIO, CLOCK, CORTEX...etc) by
57 calling the customized HAL_SMARTCARD_MspInit() API.
60 ******************************************************************************
63 * <h2><center>© COPYRIGHT(c) 2015 STMicroelectronics</center></h2>
65 * Redistribution and use in source and binary forms, with or without modification,
66 * are permitted provided that the following conditions are met:
67 * 1. Redistributions of source code must retain the above copyright notice,
68 * this list of conditions and the following disclaimer.
69 * 2. Redistributions in binary form must reproduce the above copyright notice,
70 * this list of conditions and the following disclaimer in the documentation
71 * and/or other materials provided with the distribution.
72 * 3. Neither the name of STMicroelectronics nor the names of its contributors
73 * may be used to endorse or promote products derived from this software
74 * without specific prior written permission.
76 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
77 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
78 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
79 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
80 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
81 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
82 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
83 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
84 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
85 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
87 ******************************************************************************
90 /* Includes ------------------------------------------------------------------*/
91 #include "stm32l0xx_hal.h"
93 /** @addtogroup STM32L0xx_HAL_Driver
97 /** @addtogroup SMARTCARD
98 * @brief HAL SMARTCARD module driver
101 #ifdef HAL_SMARTCARD_MODULE_ENABLED
103 /* Private typedef -----------------------------------------------------------*/
104 /* Private define ------------------------------------------------------------*/
105 #define TEACK_REACK_TIMEOUT 1000
106 #define HAL_SMARTCARD_TXDMA_TIMEOUTVALUE 22000
107 #define USART_CR1_FIELDS ((uint32_t)(USART_CR1_M | USART_CR1_PCE | USART_CR1_PS | \
108 USART_CR1_TE | USART_CR1_RE | USART_CR1_OVER8))
109 #define USART_CR2_CLK_FIELDS ((uint32_t)(USART_CR2_CLKEN|USART_CR2_CPOL|USART_CR2_CPHA|USART_CR2_LBCL))
110 #define USART_CR2_FIELDS ((uint32_t)(USART_CR2_RTOEN|USART_CR2_CLK_FIELDS|USART_CR2_STOP))
111 #define USART_CR3_FIELDS ((uint32_t)(USART_CR3_ONEBIT|USART_CR3_NACK|USART_CR3_SCARCNT))
112 /* Private macro -------------------------------------------------------------*/
113 /* Private variables ---------------------------------------------------------*/
114 /* Private function prototypes -----------------------------------------------*/
115 static void SMARTCARD_DMATransmitCplt(DMA_HandleTypeDef *hdma);
116 static void SMARTCARD_DMAReceiveCplt(DMA_HandleTypeDef *hdma);
117 static void SMARTCARD_DMAError(DMA_HandleTypeDef *hdma);
118 static void SMARTCARD_SetConfig(SMARTCARD_HandleTypeDef *hsc);
119 static HAL_StatusTypeDef SMARTCARD_WaitOnFlagUntilTimeout(SMARTCARD_HandleTypeDef *hsc, uint32_t Flag, FlagStatus Status, uint32_t Timeout);
120 static HAL_StatusTypeDef SMARTCARD_CheckIdleState(SMARTCARD_HandleTypeDef *hsc);
121 static HAL_StatusTypeDef SMARTCARD_Transmit_IT(SMARTCARD_HandleTypeDef *hsc);
122 static HAL_StatusTypeDef SMARTCARD_Receive_IT(SMARTCARD_HandleTypeDef *hsc);
123 static void SMARTCARD_AdvFeatureConfig(SMARTCARD_HandleTypeDef *hsc);
124 /* Private functions ---------------------------------------------------------*/
126 /** @addtogroup SMARTCARD_Exported_Functions
130 /** @addtogroup SMARTCARD_Exported_Functions_Group1
131 * @brief Initialization and Configuration functions
134 ===============================================================================
135 ##### Initialization and Configuration functions #####
136 ===============================================================================
138 This subsection provides a set of functions allowing to initialize the USARTx
139 associated to the SmartCard.
140 (+) These parameters can be configured:
142 (++) Parity: parity should be enabled,
143 Frame Length is fixed to 8 bits plus parity:
144 the USART frame format is given in the following table:
145 +---------------------------------------------------------------+
146 | M1M0 bits | PCE bit | USART frame |
147 |-----------------------|---------------------------------------|
148 | 01 | 1 | | SB | 8 bit data | PB | STB | |
149 +---------------------------------------------------------------+
150 (++) Receiver/transmitter modes
151 (++) Synchronous mode (and if enabled, phase, polarity and last bit parameters)
154 (++) NACK enabling or disabling on transmission error
156 (+) The following advanced features can be configured as well:
157 (++) TX and/or RX pin level inversion
158 (++) data logical level inversion
159 (++) RX and TX pins swap
160 (++) RX overrun detection disabling
161 (++) DMA disabling on RX error
162 (++) MSB first on communication line
163 (++) Time out enabling (and if activated, timeout value)
165 (++) Auto-retry counter
168 The HAL_SMARTCARD_Init() API follow respectively the USART (a)synchronous configuration procedures
169 (details for the procedures are available in reference manual).
176 * @brief Initializes the SMARTCARD mode according to the specified
177 * parameters in the SMARTCARD_InitTypeDef and creates the associated handle .
178 * @param hsc: SMARTCARD handle
181 HAL_StatusTypeDef HAL_SMARTCARD_Init(SMARTCARD_HandleTypeDef *hsc)
183 /* Check the SMARTCARD handle allocation */
189 /* Check the USART associated to the SmartCard */
190 assert_param(IS_SMARTCARD_INSTANCE(hsc->Instance));
192 if(hsc->State == HAL_SMARTCARD_STATE_RESET)
194 /* Init the low level hardware : GPIO, CLOCK, CORTEX */
195 HAL_SMARTCARD_MspInit(hsc);
198 hsc->State = HAL_SMARTCARD_STATE_BUSY;
200 /* Disable the Peripheral */
201 __HAL_SMARTCARD_DISABLE(hsc);
203 /* Set the SMARTCARD Communication parameters */
204 SMARTCARD_SetConfig(hsc);
206 if(hsc->AdvancedInit.AdvFeatureInit != SMARTCARD_ADVFEATURE_NO_INIT)
208 SMARTCARD_AdvFeatureConfig(hsc);
211 /* In SmartCard mode, the following bits must be kept cleared:
212 - LINEN in the USART_CR2 register,
213 - HDSEL and IREN bits in the USART_CR3 register.*/
214 hsc->Instance->CR2 &= ~(USART_CR2_LINEN);
215 hsc->Instance->CR3 &= ~(USART_CR3_HDSEL | USART_CR3_IREN);
217 /* set the USART in SMARTCARD mode */
218 hsc->Instance->CR3 |= USART_CR3_SCEN;
220 /* Enable the Peripheral */
221 __HAL_SMARTCARD_ENABLE(hsc);
223 /* TEACK and/or REACK to check before moving hsc->State to Ready */
224 return (SMARTCARD_CheckIdleState(hsc));
228 * @brief DeInitializes the SMARTCARD peripheral
229 * @param hsc: SMARTCARD handle
232 HAL_StatusTypeDef HAL_SMARTCARD_DeInit(SMARTCARD_HandleTypeDef *hsc)
234 /* Check the SMARTCARD handle allocation */
240 /* Check the parameters */
241 assert_param(IS_SMARTCARD_INSTANCE(hsc->Instance));
243 hsc->State = HAL_SMARTCARD_STATE_BUSY;
245 /* DeInit the low level hardware */
246 HAL_SMARTCARD_MspDeInit(hsc);
248 hsc->ErrorCode = HAL_SMARTCARD_ERROR_NONE;
249 hsc->State = HAL_SMARTCARD_STATE_RESET;
258 * @brief SMARTCARD MSP Init
259 * @param hsc: SMARTCARD handle
262 __weak void HAL_SMARTCARD_MspInit(SMARTCARD_HandleTypeDef *hsc)
264 /* NOTE : This function Should not be modified, when the callback is needed,
265 the HAL_SMARTCARD_MspInit could be implenetd in the user file
270 * @brief SMARTCARD MSP DeInit
271 * @param hsc: SMARTCARD handle
274 __weak void HAL_SMARTCARD_MspDeInit(SMARTCARD_HandleTypeDef *hsc)
276 /* NOTE : This function Should not be modified, when the callback is needed,
277 the HAL_SMARTCARD_MspDeInit could be implenetd in the user file
285 /** @addtogroup SMARTCARD_Exported_Functions_Group2
286 * @brief SMARTCARD Transmit/Receive functions
289 ===============================================================================
290 ##### IO operation functions #####
291 ===============================================================================
292 This subsection provides a set of functions allowing to manage the SMARTCARD data transfers.
294 (#) There are two mode of transfer:
295 (+) Blocking mode: The communication is performed in polling mode.
296 The HAL status of all data processing is returned by the same function
297 after finishing transfer.
298 (+) No-Blocking mode: The communication is performed using Interrupts
299 or DMA, These API's return the HAL status.
300 The end of the data processing will be indicated through the
301 dedicated SMARTCARD IRQ when using Interrupt mode or the DMA IRQ when
303 The HAL_SMARTCARD_TxCpltCallback(), HAL_SMARTCARD_RxCpltCallback() user callbacks
304 will be executed respectivelly at the end of the transmit or Receive process
305 The HAL_SMARTCARD_ErrorCallback()user callback will be executed when a communication error is detected
307 (#) Blocking mode API's are :
308 (+) HAL_SMARTCARD_Transmit()
309 (+) HAL_SMARTCARD_Receive()
311 (#) Non-Blocking mode API's with Interrupt are :
312 (+) HAL_SMARTCARD_Transmit_IT()
313 (+) HAL_SMARTCARD_Receive_IT()
314 (+) HAL_SMARTCARD_IRQHandler()
315 (+) SMARTCARD_Transmit_IT()
316 (+) SMARTCARD_Receive_IT()
318 (#) No-Blocking mode functions with DMA are :
319 (+) HAL_SMARTCARD_Transmit_DMA()
320 (+) HAL_SMARTCARD_Receive_DMA()
322 (#) A set of Transfer Complete Callbacks are provided in No_Blocking mode:
323 (+) HAL_SMARTCARD_TxCpltCallback()
324 (+) HAL_SMARTCARD_RxCpltCallback()
325 (+) HAL_SMARTCARD_ErrorCallback()
332 * @brief Send an amount of data in blocking mode
333 * @param hsc: SMARTCARD handle
334 * @param pData: pointer to data buffer
335 * @param Size: amount of data to be sent
336 * @param Timeout : Timeout duration
339 HAL_StatusTypeDef HAL_SMARTCARD_Transmit(SMARTCARD_HandleTypeDef *hsc, uint8_t *pData, uint16_t Size, uint32_t Timeout)
341 if((hsc->State == HAL_SMARTCARD_STATE_READY) || (hsc->State == HAL_SMARTCARD_STATE_BUSY_RX))
343 if((pData == NULL) || (Size == 0))
350 hsc->ErrorCode = HAL_SMARTCARD_ERROR_NONE;
351 /* Check if a non-blocking receive process is ongoing or not */
352 if(hsc->State == HAL_SMARTCARD_STATE_BUSY_RX)
354 hsc->State = HAL_SMARTCARD_STATE_BUSY_TX_RX;
358 hsc->State = HAL_SMARTCARD_STATE_BUSY_TX;
361 hsc->TxXferSize = Size;
362 hsc->TxXferCount = Size;
363 while(hsc->TxXferCount > 0)
366 if(SMARTCARD_WaitOnFlagUntilTimeout(hsc, SMARTCARD_FLAG_TXE, RESET, Timeout) != HAL_OK)
370 hsc->Instance->TDR = (*pData++ & (uint8_t)0xFF);
372 if(SMARTCARD_WaitOnFlagUntilTimeout(hsc, SMARTCARD_FLAG_TC, RESET, Timeout) != HAL_OK)
376 /* Check if a non-blocking receive Process is ongoing or not */
377 if(hsc->State == HAL_SMARTCARD_STATE_BUSY_TX_RX)
379 hsc->State = HAL_SMARTCARD_STATE_BUSY_RX;
383 hsc->State = HAL_SMARTCARD_STATE_READY;
386 /* Process Unlocked */
398 * @brief Receive an amount of data in blocking mode
399 * @param hsc: SMARTCARD handle
400 * @param pData: pointer to data buffer
401 * @param Size: amount of data to be received
402 * @param Timeout : Timeout duration
405 HAL_StatusTypeDef HAL_SMARTCARD_Receive(SMARTCARD_HandleTypeDef *hsc, uint8_t *pData, uint16_t Size, uint32_t Timeout)
407 if((hsc->State == HAL_SMARTCARD_STATE_READY) || (hsc->State == HAL_SMARTCARD_STATE_BUSY_TX))
409 if((pData == NULL) || (Size == 0))
417 hsc->ErrorCode = HAL_SMARTCARD_ERROR_NONE;
418 /* Check if a non-blocking transmit process is ongoing or not */
419 if(hsc->State == HAL_SMARTCARD_STATE_BUSY_TX)
421 hsc->State = HAL_SMARTCARD_STATE_BUSY_TX_RX;
425 hsc->State = HAL_SMARTCARD_STATE_BUSY_RX;
428 hsc->RxXferSize = Size;
429 hsc->RxXferCount = Size;
430 /* Check the remain data to be received */
431 while(hsc->RxXferCount > 0)
434 if(SMARTCARD_WaitOnFlagUntilTimeout(hsc, SMARTCARD_FLAG_RXNE, RESET, Timeout) != HAL_OK)
438 *pData++ = (uint8_t)(hsc->Instance->RDR & (uint8_t)0x00FF);
441 /* Check if a non-blocking transmit Process is ongoing or not */
442 if(hsc->State == HAL_SMARTCARD_STATE_BUSY_TX_RX)
444 hsc->State = HAL_SMARTCARD_STATE_BUSY_TX;
448 hsc->State = HAL_SMARTCARD_STATE_READY;
451 /* Process Unlocked */
463 * @brief Send an amount of data in interrupt mode
464 * @param hsc: SMARTCARD handle
465 * @param pData: pointer to data buffer
466 * @param Size: amount of data to be sent
469 HAL_StatusTypeDef HAL_SMARTCARD_Transmit_IT(SMARTCARD_HandleTypeDef *hsc, uint8_t *pData, uint16_t Size)
471 if((hsc->State == HAL_SMARTCARD_STATE_READY) || (hsc->State == HAL_SMARTCARD_STATE_BUSY_RX))
473 if((pData == NULL) || (Size == 0))
481 hsc->pTxBuffPtr = pData;
482 hsc->TxXferSize = Size;
483 hsc->TxXferCount = Size;
485 hsc->ErrorCode = HAL_SMARTCARD_ERROR_NONE;
486 /* Check if a receive process is ongoing or not */
487 if(hsc->State == HAL_SMARTCARD_STATE_BUSY_RX)
489 hsc->State = HAL_SMARTCARD_STATE_BUSY_TX_RX;
493 hsc->State = HAL_SMARTCARD_STATE_BUSY_TX;
496 /* Enable the SMARTCARD Error Interrupt: (Frame error, noise error, overrun error) */
497 __HAL_SMARTCARD_ENABLE_IT(hsc, SMARTCARD_IT_ERR);
499 /* Process Unlocked */
502 /* Enable the SMARTCARD Transmit Complete Interrupt */
503 __HAL_SMARTCARD_ENABLE_IT(hsc, SMARTCARD_IT_TC);
514 * @brief Receive an amount of data in interrupt mode
515 * @param hsc: SMARTCARD handle
516 * @param pData: pointer to data buffer
517 * @param Size: amount of data to be received
520 HAL_StatusTypeDef HAL_SMARTCARD_Receive_IT(SMARTCARD_HandleTypeDef *hsc, uint8_t *pData, uint16_t Size)
522 if((hsc->State == HAL_SMARTCARD_STATE_READY) || (hsc->State == HAL_SMARTCARD_STATE_BUSY_TX))
524 if((pData == NULL) || (Size == 0))
532 hsc->pRxBuffPtr = pData;
533 hsc->RxXferSize = Size;
534 hsc->RxXferCount = Size;
536 hsc->ErrorCode = HAL_SMARTCARD_ERROR_NONE;
537 /* Check if a transmit process is ongoing or not */
538 if(hsc->State == HAL_SMARTCARD_STATE_BUSY_TX)
540 hsc->State = HAL_SMARTCARD_STATE_BUSY_TX_RX;
544 hsc->State = HAL_SMARTCARD_STATE_BUSY_RX;
547 /* Enable the SMARTCARD Parity Error Interrupt */
548 __HAL_SMARTCARD_ENABLE_IT(hsc, SMARTCARD_IT_PE);
550 /* Enable the SMARTCARD Error Interrupt: (Frame error, noise error, overrun error) */
551 __HAL_SMARTCARD_ENABLE_IT(hsc, SMARTCARD_IT_ERR);
553 /* Process Unlocked */
556 /* Enable the SMARTCARD Data Register not empty Interrupt */
557 __HAL_SMARTCARD_ENABLE_IT(hsc, SMARTCARD_IT_RXNE);
568 * @brief Send an amount of data in DMA mode
569 * @param hsc: SMARTCARD handle
570 * @param pData: pointer to data buffer
571 * @param Size: amount of data to be sent
574 HAL_StatusTypeDef HAL_SMARTCARD_Transmit_DMA(SMARTCARD_HandleTypeDef *hsc, uint8_t *pData, uint16_t Size)
578 if((hsc->State == HAL_SMARTCARD_STATE_READY) || (hsc->State == HAL_SMARTCARD_STATE_BUSY_RX))
580 if((pData == NULL) || (Size == 0))
588 hsc->pTxBuffPtr = pData;
589 hsc->TxXferSize = Size;
590 hsc->TxXferCount = Size;
592 hsc->ErrorCode = HAL_SMARTCARD_ERROR_NONE;
593 /* Check if a receive process is ongoing or not */
594 if(hsc->State == HAL_SMARTCARD_STATE_BUSY_RX)
596 hsc->State = HAL_SMARTCARD_STATE_BUSY_TX_RX;
600 hsc->State = HAL_SMARTCARD_STATE_BUSY_TX;
603 /* Set the SMARTCARD DMA transfert complete callback */
604 hsc->hdmatx->XferCpltCallback = SMARTCARD_DMATransmitCplt;
606 /* Set the SMARTCARD error callback */
607 hsc->hdmatx->XferErrorCallback = SMARTCARD_DMAError;
609 /* Enable the SMARTCARD transmit DMA Stream */
610 tmp = (uint32_t*)&pData;
611 HAL_DMA_Start_IT(hsc->hdmatx, *(uint32_t*)tmp, (uint32_t)&hsc->Instance->TDR, Size);
613 /* Clear the TC flag in the SR register by writing 0 to it */
614 __HAL_SMARTCARD_CLEAR_FLAG(hsc, SMARTCARD_FLAG_TC);
616 /* Enable the DMA transfer for transmit request by setting the DMAT bit
617 in the SMARTCARD associated USART CR3 register */
618 hsc->Instance->CR3 |= USART_CR3_DMAT;
620 /* Process Unlocked */
632 * @brief Receive an amount of data in DMA mode
633 * @param hsc: SMARTCARD handle
634 * @param pData: pointer to data buffer
635 * @param Size: amount of data to be received
636 * @note The SMARTCARD-associated USART parity is enabled (PCE = 1),
637 * the received data contain the parity bit (MSB position)
640 HAL_StatusTypeDef HAL_SMARTCARD_Receive_DMA(SMARTCARD_HandleTypeDef *hsc, uint8_t *pData, uint16_t Size)
644 if((hsc->State == HAL_SMARTCARD_STATE_READY) || (hsc->State == HAL_SMARTCARD_STATE_BUSY_TX))
646 if((pData == NULL) || (Size == 0))
654 hsc->pRxBuffPtr = pData;
655 hsc->RxXferSize = Size;
657 hsc->ErrorCode = HAL_SMARTCARD_ERROR_NONE;
658 /* Check if a transmit rocess is ongoing or not */
659 if(hsc->State == HAL_SMARTCARD_STATE_BUSY_TX)
661 hsc->State = HAL_SMARTCARD_STATE_BUSY_TX_RX;
665 hsc->State = HAL_SMARTCARD_STATE_BUSY_RX;
668 /* Set the SMARTCARD DMA transfert complete callback */
669 hsc->hdmarx->XferCpltCallback = SMARTCARD_DMAReceiveCplt;
671 /* Set the SMARTCARD DMA error callback */
672 hsc->hdmarx->XferErrorCallback = SMARTCARD_DMAError;
674 /* Enable the DMA Stream */
675 tmp = (uint32_t*)&pData;
676 HAL_DMA_Start_IT(hsc->hdmarx, (uint32_t)&hsc->Instance->RDR, *(uint32_t*)tmp, Size);
678 /* Enable the DMA transfer for the receiver request by setting the DMAR bit
679 in the SMARTCARD associated USART CR3 register */
680 hsc->Instance->CR3 |= USART_CR3_DMAR;
682 /* Process Unlocked */
694 * @brief SMARTCARD interrupt requests handling.
695 * @param hsc: SMARTCARD handle
698 void HAL_SMARTCARD_IRQHandler(SMARTCARD_HandleTypeDef *hsc)
700 /* SMARTCARD parity error interrupt occurred -------------------------------*/
701 if((__HAL_SMARTCARD_GET_IT(hsc, SMARTCARD_IT_PE) != RESET) && (__HAL_SMARTCARD_GET_IT_SOURCE(hsc, SMARTCARD_IT_PE) != RESET))
703 __HAL_SMARTCARD_CLEAR_PEFLAG(hsc);
704 hsc->ErrorCode |= HAL_SMARTCARD_ERROR_PE;
705 /* Set the SMARTCARD state ready to be able to start again the process */
706 hsc->State = HAL_SMARTCARD_STATE_READY;
709 /* SMARTCARD frame error interrupt occured ---------------------------------*/
710 if((__HAL_SMARTCARD_GET_IT(hsc, SMARTCARD_IT_FE) != RESET) && (__HAL_SMARTCARD_GET_IT_SOURCE(hsc, SMARTCARD_IT_ERR) != RESET))
712 __HAL_SMARTCARD_CLEAR_FEFLAG(hsc);
713 hsc->ErrorCode |= HAL_SMARTCARD_ERROR_FE;
714 /* Set the SMARTCARD state ready to be able to start again the process */
715 hsc->State = HAL_SMARTCARD_STATE_READY;
718 /* SMARTCARD noise error interrupt occured ---------------------------------*/
719 if((__HAL_SMARTCARD_GET_IT(hsc, SMARTCARD_IT_NE) != RESET) && (__HAL_SMARTCARD_GET_IT_SOURCE(hsc, SMARTCARD_IT_ERR) != RESET))
721 __HAL_SMARTCARD_CLEAR_NEFLAG(hsc);
722 hsc->ErrorCode |= HAL_SMARTCARD_ERROR_NE;
723 /* Set the SMARTCARD state ready to be able to start again the process */
724 hsc->State = HAL_SMARTCARD_STATE_READY;
727 /* SMARTCARD Over-Run interrupt occured ------------------------------------*/
728 if((__HAL_SMARTCARD_GET_IT(hsc, SMARTCARD_IT_ORE) != RESET) && (__HAL_SMARTCARD_GET_IT_SOURCE(hsc, SMARTCARD_IT_ERR) != RESET))
730 __HAL_SMARTCARD_CLEAR_OREFLAG(hsc);
731 hsc->ErrorCode |= HAL_SMARTCARD_ERROR_ORE;
732 /* Set the SMARTCARD state ready to be able to start again the process */
733 hsc->State = HAL_SMARTCARD_STATE_READY;
736 /* SMARTCARD receiver timeout interrupt occured ----------------------------*/
737 if((__HAL_SMARTCARD_GET_IT(hsc, SMARTCARD_IT_RTO) != RESET) && (__HAL_SMARTCARD_GET_IT_SOURCE(hsc, SMARTCARD_IT_RTO) != RESET))
739 __HAL_SMARTCARD_CLEAR_IT(hsc, SMARTCARD_CLEAR_RTOF);
740 hsc->ErrorCode |= HAL_SMARTCARD_ERROR_RTO;
741 /* Set the SMARTCARD state ready to be able to start again the process */
742 hsc->State = HAL_SMARTCARD_STATE_READY;
745 /* Call SMARTCARD Error Call back function if need be ----------------------*/
746 if(hsc->ErrorCode != HAL_SMARTCARD_ERROR_NONE)
748 HAL_SMARTCARD_ErrorCallback(hsc);
751 /* SMARTCARD in mode Receiver ----------------------------------------------*/
752 if((__HAL_SMARTCARD_GET_IT(hsc, SMARTCARD_IT_RXNE) != RESET) && (__HAL_SMARTCARD_GET_IT_SOURCE(hsc, SMARTCARD_IT_RXNE) != RESET))
754 SMARTCARD_Receive_IT(hsc);
755 /* Clear RXNE interrupt flag */
756 __HAL_SMARTCARD_SEND_REQ(hsc, SMARTCARD_RXDATA_FLUSH_REQUEST);
759 /* SMARTCARD in mode Receiver, end of block interruption -------------------*/
760 if((__HAL_SMARTCARD_GET_IT(hsc, SMARTCARD_IT_EOB) != RESET) && (__HAL_SMARTCARD_GET_IT_SOURCE(hsc, SMARTCARD_IT_EOB) != RESET))
762 hsc->State = HAL_SMARTCARD_STATE_READY;
763 HAL_SMARTCARD_RxCpltCallback(hsc);
764 /* Clear EOBF interrupt after HAL_SMARTCARD_RxCpltCallback() call for the End of Block information
765 * to be available during HAL_SMARTCARD_RxCpltCallback() processing */
766 __HAL_SMARTCARD_CLEAR_IT(hsc, SMARTCARD_CLEAR_EOBF);
769 /* SMARTCARD in mode Transmitter -------------------------------------------*/
770 if((__HAL_SMARTCARD_GET_IT(hsc, SMARTCARD_IT_TC) != RESET) && (__HAL_SMARTCARD_GET_IT_SOURCE(hsc, SMARTCARD_IT_TC) != RESET))
772 SMARTCARD_Transmit_IT(hsc);
777 * @brief Tx Transfer completed callbacks
778 * @param hsc: SMARTCARD handle
781 __weak void HAL_SMARTCARD_TxCpltCallback(SMARTCARD_HandleTypeDef *hsc)
783 /* NOTE : This function Should not be modified, when the callback is needed,
784 the HAL_SMARTCARD_TxCpltCallback could be implemented in the user file
789 * @brief Rx Transfer completed callbacks
790 * @param hsc: SMARTCARD handle
793 __weak void HAL_SMARTCARD_RxCpltCallback(SMARTCARD_HandleTypeDef *hsc)
795 /* NOTE : This function Should not be modified, when the callback is needed,
796 the HAL_SMARTCARD_TxCpltCallback could be implemented in the user file
801 * @brief SMARTCARD error callbacks
802 * @param hsc: SMARTCARD handle
805 __weak void HAL_SMARTCARD_ErrorCallback(SMARTCARD_HandleTypeDef *hsc)
807 /* NOTE : This function Should not be modified, when the callback is needed,
808 the HAL_SMARTCARD_ErrorCallback could be implemented in the user file
816 /** @addtogroup SMARTCARD_Exported_Functions_Group3
817 * @brief SMARTCARD State functions
820 ===============================================================================
821 ##### Peripheral State functions #####
822 ===============================================================================
824 This subsection provides a set of functions allowing to initialize the SMARTCARD.
825 (+) HAL_SMARTCARD_GetState() API is helpful to check in run-time the state of the SMARTCARD peripheral
826 (+) SMARTCARD_SetConfig() API configures the SMARTCARD peripheral
827 (+) SMARTCARD_CheckIdleState() API ensures that TEACK and/or REACK are set after initialization
835 * @brief return the SMARTCARD state
836 * @param hsc: SMARTCARD handle
839 HAL_SMARTCARD_StateTypeDef HAL_SMARTCARD_GetState(SMARTCARD_HandleTypeDef *hsc)
845 * @brief Return the SMARTCARD error code
846 * @param hsc : pointer to a SMARTCARD_HandleTypeDef structure that contains
847 * the configuration information for the specified SMARTCARD.
848 * @retval SMARTCARD Error Code
850 uint32_t HAL_SMARTCARD_GetError(SMARTCARD_HandleTypeDef *hsc)
852 return hsc->ErrorCode;
860 * @brief Send an amount of data in non blocking mode
861 * @param hsc: SMARTCARD handle.
862 * Function called under interruption only, once
863 * interruptions have been enabled by HAL_SMARTCARD_Transmit_IT()
866 static HAL_StatusTypeDef SMARTCARD_Transmit_IT(SMARTCARD_HandleTypeDef *hsc)
868 if((hsc->State == HAL_SMARTCARD_STATE_BUSY_TX) || (hsc->State == HAL_SMARTCARD_STATE_BUSY_TX_RX))
870 if(hsc->TxXferCount == 0)
872 /* Disable the SMARTCARD Transmit Complete Interrupt */
873 __HAL_SMARTCARD_DISABLE_IT(hsc, SMARTCARD_IT_TC);
875 /* Check if a receive Process is ongoing or not */
876 if(hsc->State == HAL_SMARTCARD_STATE_BUSY_TX_RX)
878 hsc->State = HAL_SMARTCARD_STATE_BUSY_RX;
882 /* Disable the SMARTCARD Error Interrupt: (Frame error, noise error, overrun error) */
883 __HAL_SMARTCARD_DISABLE_IT(hsc, SMARTCARD_IT_ERR);
885 hsc->State = HAL_SMARTCARD_STATE_READY;
888 HAL_SMARTCARD_TxCpltCallback(hsc);
894 hsc->Instance->TDR = (*hsc->pTxBuffPtr++ & (uint8_t)0xFF);
907 * @brief Receive an amount of data in non blocking mode
908 * @param hsc: SMARTCARD handle.
909 * Function called under interruption only, once
910 * interruptions have been enabled by HAL_SMARTCARD_Receive_IT()
913 static HAL_StatusTypeDef SMARTCARD_Receive_IT(SMARTCARD_HandleTypeDef *hsc)
915 if((hsc->State == HAL_SMARTCARD_STATE_BUSY_RX) || (hsc->State == HAL_SMARTCARD_STATE_BUSY_TX_RX))
917 *hsc->pRxBuffPtr++ = (uint8_t)(hsc->Instance->RDR & (uint8_t)0xFF);
919 if(--hsc->RxXferCount == 0)
921 while(HAL_IS_BIT_SET(hsc->Instance->ISR, SMARTCARD_FLAG_RXNE))
924 __HAL_SMARTCARD_DISABLE_IT(hsc, SMARTCARD_IT_RXNE);
926 /* Check if a transmit Process is ongoing or not */
927 if(hsc->State == HAL_SMARTCARD_STATE_BUSY_TX_RX)
929 hsc->State = HAL_SMARTCARD_STATE_BUSY_TX;
933 /* Disable the SMARTCARD Parity Error Interrupt */
934 __HAL_SMARTCARD_DISABLE_IT(hsc, SMARTCARD_IT_PE);
936 /* Disable the SMARTCARD Error Interrupt: (Frame error, noise error, overrun error) */
937 __HAL_SMARTCARD_DISABLE_IT(hsc, SMARTCARD_IT_ERR);
939 hsc->State = HAL_SMARTCARD_STATE_READY;
942 HAL_SMARTCARD_RxCpltCallback(hsc);
955 * @brief Configure the SMARTCARD associated USART peripheral
956 * @param hsc: SMARTCARD handle
959 static void SMARTCARD_SetConfig(SMARTCARD_HandleTypeDef *hsc)
961 uint32_t tmpreg = 0x00000000;
962 uint32_t clocksource = 0x00000000;
964 /* Check the parameters */
965 assert_param(IS_SMARTCARD_INSTANCE(hsc->Instance));
966 assert_param(IS_SMARTCARD_BAUDRATE(hsc->Init.BaudRate));
967 assert_param(IS_SMARTCARD_WORD_LENGTH(hsc->Init.WordLength));
968 assert_param(IS_SMARTCARD_STOPBITS(hsc->Init.StopBits));
969 assert_param(IS_SMARTCARD_PARITY(hsc->Init.Parity));
970 assert_param(IS_SMARTCARD_MODE(hsc->Init.Mode));
971 assert_param(IS_SMARTCARD_POLARITY(hsc->Init.CLKPolarity));
972 assert_param(IS_SMARTCARD_PHASE(hsc->Init.CLKPhase));
973 assert_param(IS_SMARTCARD_LASTBIT(hsc->Init.CLKLastBit));
974 assert_param(IS_SMARTCARD_ONE_BIT_SAMPLE(hsc->Init.OneBitSampling));
975 assert_param(IS_SMARTCARD_NACK(hsc->Init.NACKState));
976 assert_param(IS_SMARTCARD_TIMEOUT(hsc->Init.TimeOutEnable));
977 assert_param(IS_SMARTCARD_AUTORETRY_COUNT(hsc->Init.AutoRetryCount));
979 /*-------------------------- USART CR1 Configuration -----------------------*/
980 /* In SmartCard mode, M and PCE are forced to 1 (8 bits + parity).
981 * Oversampling is forced to 16 (OVER8 = 0).
982 * Configure the Parity and Mode:
983 * set PS bit according to hsc->Init.Parity value
984 * set TE and RE bits according to hsc->Init.Mode value */
985 tmpreg = (uint32_t) hsc->Init.Parity | hsc->Init.Mode;
986 /* in case of TX-only mode, if NACK is enabled, the USART must be able to monitor
987 the bidirectional line to detect a NACK signal in case of parity error.
988 Therefore, the receiver block must be enabled as well (RE bit must be set). */
989 if((hsc->Init.Mode == SMARTCARD_MODE_TX) && (hsc->Init.NACKState == SMARTCARD_NACK_ENABLE))
991 tmpreg |= USART_CR1_RE;
993 tmpreg |= (uint32_t) hsc->Init.WordLength;
994 MODIFY_REG(hsc->Instance->CR1, USART_CR1_FIELDS, tmpreg);
996 /*-------------------------- USART CR2 Configuration -----------------------*/
997 /* Stop bits are forced to 1.5 (STOP = 11) */
998 tmpreg = hsc->Init.StopBits;
999 /* Synchronous mode is activated by default */
1000 tmpreg |= (uint32_t) USART_CR2_CLKEN | hsc->Init.CLKPolarity;
1001 tmpreg |= (uint32_t) hsc->Init.CLKPhase | hsc->Init.CLKLastBit;
1002 tmpreg |= (uint32_t) hsc->Init.TimeOutEnable;
1003 MODIFY_REG(hsc->Instance->CR2, USART_CR2_FIELDS, tmpreg);
1005 /*-------------------------- USART CR3 Configuration -----------------------*/
1007 * - one-bit sampling method versus three samples' majority rule
1008 * according to hsc->Init.OneBitSampling
1009 * - NACK transmission in case of parity error according
1010 * to hsc->Init.NACKEnable
1011 * - autoretry counter according to hsc->Init.AutoRetryCount */
1012 tmpreg = (uint32_t) hsc->Init.OneBitSampling | hsc->Init.NACKState;
1013 tmpreg |= (uint32_t) (hsc->Init.AutoRetryCount << SMARTCARD_CR3_SCARCNT_LSB_POS);
1014 MODIFY_REG(hsc->Instance-> CR3,USART_CR3_FIELDS, tmpreg);
1016 /*-------------------------- USART GTPR Configuration ----------------------*/
1017 tmpreg = (uint32_t) (hsc->Init.Prescaler | (hsc->Init.GuardTime << SMARTCARD_GTPR_GT_LSB_POS));
1018 MODIFY_REG(hsc->Instance->GTPR, (uint32_t)(USART_GTPR_GT|USART_GTPR_PSC), tmpreg);
1020 /*-------------------------- USART RTOR Configuration ----------------------*/
1021 tmpreg = (uint32_t) (hsc->Init.BlockLength << SMARTCARD_RTOR_BLEN_LSB_POS);
1022 if(hsc->Init.TimeOutEnable == SMARTCARD_TIMEOUT_ENABLE)
1024 assert_param(IS_SMARTCARD_TIMEOUT_VALUE(hsc->Init.TimeOutValue));
1025 tmpreg |= (uint32_t) hsc->Init.TimeOutValue;
1027 MODIFY_REG(hsc->Instance->RTOR, (USART_RTOR_RTO|USART_RTOR_BLEN), tmpreg);
1029 /*-------------------------- USART BRR Configuration -----------------------*/
1030 SMARTCARD_GETCLOCKSOURCE(hsc, clocksource);
1031 switch (clocksource)
1033 case SMARTCARD_CLOCKSOURCE_PCLK1:
1034 hsc->Instance->BRR = (uint16_t)(HAL_RCC_GetPCLK1Freq() / hsc->Init.BaudRate);
1036 case SMARTCARD_CLOCKSOURCE_PCLK2:
1037 hsc->Instance->BRR = (uint16_t)(HAL_RCC_GetPCLK2Freq() / hsc->Init.BaudRate);
1039 case SMARTCARD_CLOCKSOURCE_HSI:
1040 hsc->Instance->BRR = (uint16_t)(HSI_VALUE / hsc->Init.BaudRate);
1042 case SMARTCARD_CLOCKSOURCE_SYSCLK:
1043 hsc->Instance->BRR = (uint16_t)(HAL_RCC_GetSysClockFreq() / hsc->Init.BaudRate);
1045 case SMARTCARD_CLOCKSOURCE_LSE:
1046 hsc->Instance->BRR = (uint16_t)(LSE_VALUE / hsc->Init.BaudRate);
1054 * @brief Check the SMARTCARD Idle State
1055 * @param hsc: SMARTCARD handle
1056 * @retval HAL status
1058 static HAL_StatusTypeDef SMARTCARD_CheckIdleState(SMARTCARD_HandleTypeDef *hsc)
1061 /* Initialize the SMARTCARD ErrorCode */
1062 hsc->ErrorCode = HAL_SMARTCARD_ERROR_NONE;
1064 /* Check if the Transmitter is enabled */
1065 if((hsc->Instance->CR1 & USART_CR1_TE) == USART_CR1_TE)
1067 /* Wait until TEACK flag is set */
1068 if(SMARTCARD_WaitOnFlagUntilTimeout(hsc, USART_ISR_TEACK, RESET, TEACK_REACK_TIMEOUT) != HAL_OK)
1073 /* Check if the Receiver is enabled */
1074 if((hsc->Instance->CR1 & USART_CR1_RE) == USART_CR1_RE)
1076 /* Wait until REACK flag is set */
1077 if(SMARTCARD_WaitOnFlagUntilTimeout(hsc, USART_ISR_REACK, RESET, TEACK_REACK_TIMEOUT) != HAL_OK)
1083 /* Process Unlocked */
1086 /* Initialize the SMARTCARD state*/
1087 hsc->State= HAL_SMARTCARD_STATE_READY;
1093 * @brief Configure the SMARTCARD associated USART peripheral advanced feautures
1094 * @param hsc: SMARTCARD handle
1097 static void SMARTCARD_AdvFeatureConfig(SMARTCARD_HandleTypeDef *hsc)
1099 /* Check whether the set of advanced features to configure is properly set */
1100 assert_param(IS_SMARTCARD_ADVFEATURE_INIT(hsc->AdvancedInit.AdvFeatureInit));
1102 /* if required, configure TX pin active level inversion */
1103 if(HAL_IS_BIT_SET(hsc->AdvancedInit.AdvFeatureInit, SMARTCARD_ADVFEATURE_TXINVERT_INIT))
1105 assert_param(IS_SMARTCARD_ADVFEATURE_TXINV(hsc->AdvancedInit.TxPinLevelInvert));
1106 MODIFY_REG(hsc->Instance->CR2, USART_CR2_TXINV, hsc->AdvancedInit.TxPinLevelInvert);
1109 /* if required, configure RX pin active level inversion */
1110 if(HAL_IS_BIT_SET(hsc->AdvancedInit.AdvFeatureInit, SMARTCARD_ADVFEATURE_RXINVERT_INIT))
1112 assert_param(IS_SMARTCARD_ADVFEATURE_RXINV(hsc->AdvancedInit.RxPinLevelInvert));
1113 MODIFY_REG(hsc->Instance->CR2, USART_CR2_RXINV, hsc->AdvancedInit.RxPinLevelInvert);
1116 /* if required, configure data inversion */
1117 if(HAL_IS_BIT_SET(hsc->AdvancedInit.AdvFeatureInit, SMARTCARD_ADVFEATURE_DATAINVERT_INIT))
1119 assert_param(IS_SMARTCARD_ADVFEATURE_DATAINV(hsc->AdvancedInit.DataInvert));
1120 MODIFY_REG(hsc->Instance->CR2, USART_CR2_DATAINV, hsc->AdvancedInit.DataInvert);
1123 /* if required, configure RX/TX pins swap */
1124 if(HAL_IS_BIT_SET(hsc->AdvancedInit.AdvFeatureInit, SMARTCARD_ADVFEATURE_SWAP_INIT))
1126 assert_param(IS_SMARTCARD_ADVFEATURE_SWAP(hsc->AdvancedInit.Swap));
1127 MODIFY_REG(hsc->Instance->CR2, USART_CR2_SWAP, hsc->AdvancedInit.Swap);
1130 /* if required, configure RX overrun detection disabling */
1131 if(HAL_IS_BIT_SET(hsc->AdvancedInit.AdvFeatureInit, SMARTCARD_ADVFEATURE_RXOVERRUNDISABLE_INIT))
1133 assert_param(IS_SMARTCARD_OVERRUN(hsc->AdvancedInit.OverrunDisable));
1134 MODIFY_REG(hsc->Instance->CR3, USART_CR3_OVRDIS, hsc->AdvancedInit.OverrunDisable);
1137 /* if required, configure DMA disabling on reception error */
1138 if(HAL_IS_BIT_SET(hsc->AdvancedInit.AdvFeatureInit, SMARTCARD_ADVFEATURE_DMADISABLEONERROR_INIT))
1140 assert_param(IS_SMARTCARD_ADVFEATURE_DMAONRXERROR(hsc->AdvancedInit.DMADisableonRxError));
1141 MODIFY_REG(hsc->Instance->CR3, USART_CR3_DDRE, hsc->AdvancedInit.DMADisableonRxError);
1144 /* if required, configure MSB first on communication line */
1145 if(HAL_IS_BIT_SET(hsc->AdvancedInit.AdvFeatureInit, SMARTCARD_ADVFEATURE_MSBFIRST_INIT))
1147 assert_param(IS_SMARTCARD_ADVFEATURE_MSBFIRST(hsc->AdvancedInit.MSBFirst));
1148 MODIFY_REG(hsc->Instance->CR2, USART_CR2_MSBFIRST, hsc->AdvancedInit.MSBFirst);
1153 * @brief This function handles SMARTCARD Communication Timeout.
1154 * @param hsc: SMARTCARD handle
1155 * @param Flag: specifies the SMARTCARD flag to check.
1156 * @param Status: The new Flag status (SET or RESET).
1157 * @param Timeout: Timeout duration
1158 * @retval HAL status
1160 static HAL_StatusTypeDef SMARTCARD_WaitOnFlagUntilTimeout(SMARTCARD_HandleTypeDef *hsc, uint32_t Flag, FlagStatus Status, uint32_t Timeout)
1162 uint32_t tickstart = 0x00;
1163 tickstart = HAL_GetTick();
1165 /* Wait until flag is set */
1168 while(__HAL_SMARTCARD_GET_FLAG(hsc, Flag) == RESET)
1170 /* Check for the Timeout */
1171 if(Timeout != HAL_MAX_DELAY)
1173 if((Timeout == 0)||((HAL_GetTick() - tickstart ) > Timeout))
1175 /* Disable TXE, RXNE, PE and ERR (Frame error, noise error, overrun error) interrupts for the interrupt process */
1176 __HAL_SMARTCARD_DISABLE_IT(hsc, SMARTCARD_IT_TXE);
1177 __HAL_SMARTCARD_DISABLE_IT(hsc, SMARTCARD_IT_RXNE);
1178 __HAL_SMARTCARD_DISABLE_IT(hsc, SMARTCARD_IT_PE);
1179 __HAL_SMARTCARD_DISABLE_IT(hsc, SMARTCARD_IT_ERR);
1181 hsc->State= HAL_SMARTCARD_STATE_READY;
1183 /* Process Unlocked */
1193 while(__HAL_SMARTCARD_GET_FLAG(hsc, Flag) != RESET)
1195 /* Check for the Timeout */
1196 if(Timeout != HAL_MAX_DELAY)
1198 if((Timeout == 0)||((HAL_GetTick() - tickstart ) > Timeout))
1200 /* Disable TXE, RXNE, PE and ERR (Frame error, noise error, overrun error) interrupts for the interrupt process */
1201 __HAL_SMARTCARD_DISABLE_IT(hsc, SMARTCARD_IT_TXE);
1202 __HAL_SMARTCARD_DISABLE_IT(hsc, SMARTCARD_IT_RXNE);
1203 __HAL_SMARTCARD_DISABLE_IT(hsc, SMARTCARD_IT_PE);
1204 __HAL_SMARTCARD_DISABLE_IT(hsc, SMARTCARD_IT_ERR);
1206 hsc->State= HAL_SMARTCARD_STATE_READY;
1208 /* Process Unlocked */
1220 * @brief DMA SMARTCARD transmit process complete callback
1221 * @param hdma: DMA handle
1224 static void SMARTCARD_DMATransmitCplt(DMA_HandleTypeDef *hdma)
1226 SMARTCARD_HandleTypeDef* hsmartcard = ( SMARTCARD_HandleTypeDef* )((DMA_HandleTypeDef* )hdma)->Parent;
1227 hsmartcard->TxXferCount = 0;
1229 /* Disable the DMA transfer for transmit request by resetting the DMAT bit
1230 in the SMARTCARD associated USART CR3 register */
1231 hsmartcard->Instance->CR3 &= (uint16_t)~((uint16_t)USART_CR3_DMAT);
1233 /* Enable the SMARTCARD Transmit Complete Interrupt */
1234 __HAL_SMARTCARD_ENABLE_IT(hsmartcard, SMARTCARD_IT_TC);
1238 * @brief DMA SMARTCARD receive process complete callback
1239 * @param hdma: DMA handle
1242 static void SMARTCARD_DMAReceiveCplt(DMA_HandleTypeDef *hdma)
1244 SMARTCARD_HandleTypeDef* hsc = ( SMARTCARD_HandleTypeDef* )((DMA_HandleTypeDef* )hdma)->Parent;
1245 hsc->RxXferCount = 0;
1247 /* Disable the DMA transfer for the receiver request by setting the DMAR bit
1248 in the SMARTCARD associated USART CR3 register */
1249 hsc->Instance->CR3 &= (uint16_t)~((uint16_t)USART_CR3_DMAR);
1251 /* Check if a transmit Process is ongoing or not */
1252 if(hsc->State == HAL_SMARTCARD_STATE_BUSY_TX_RX)
1254 hsc->State = HAL_SMARTCARD_STATE_BUSY_TX;
1258 hsc->State = HAL_SMARTCARD_STATE_READY;
1261 HAL_SMARTCARD_RxCpltCallback(hsc);
1265 * @brief DMA SMARTCARD communication error callback
1266 * @param hdma: DMA handle
1269 static void SMARTCARD_DMAError(DMA_HandleTypeDef *hdma)
1271 SMARTCARD_HandleTypeDef* hsc = ( SMARTCARD_HandleTypeDef* )((DMA_HandleTypeDef* )hdma)->Parent;
1272 hsc->RxXferCount = 0;
1273 hsc->TxXferCount = 0;
1274 hsc->State= HAL_SMARTCARD_STATE_READY;
1275 hsc->ErrorCode |= HAL_SMARTCARD_ERROR_DMA;
1276 HAL_SMARTCARD_ErrorCallback(hsc);
1282 #endif /* HAL_SMARTCARD_MODULE_ENABLED */
1291 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/