2 ******************************************************************************
3 * @file stm32f1xx_hal_smartcard.c
4 * @author MCD Application Team
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 Errors functions
15 ==============================================================================
16 ##### How to use this driver #####
17 ==============================================================================
19 The SMARTCARD HAL driver can be used as follows:
21 (#) Declare a SMARTCARD_HandleTypeDef handle structure.
22 (#) Initialize the SMARTCARD low level resources by implementing the HAL_SMARTCARD_MspInit() API:
23 (##) Enable the USARTx interface clock.
24 (##) SMARTCARD pins configuration:
25 (+++) Enable the clock for the SMARTCARD GPIOs.
26 (+++) Configure the SMARTCARD pins as alternate function pull-up.
27 (##) NVIC configuration if you need to use interrupt process (HAL_SMARTCARD_Transmit_IT()
28 and HAL_SMARTCARD_Receive_IT() APIs):
29 (+++) Configure the USARTx interrupt priority.
30 (+++) Enable the NVIC USART IRQ handle.
31 (##) DMA Configuration if you need to use DMA process (HAL_SMARTCARD_Transmit_DMA()
32 and HAL_SMARTCARD_Receive_DMA() APIs):
33 (+++) Declare a DMA handle structure for the Tx/Rx channel.
34 (+++) Enable the DMAx interface clock.
35 (+++) Configure the declared DMA handle structure with the required Tx/Rx parameters.
36 (+++) Configure the DMA Tx/Rx channel.
37 (+++) Associate the initialized DMA handle to the SMARTCARD DMA Tx/Rx handle.
38 (+++) Configure the priority and enable the NVIC for the transfer complete interrupt on the DMA Tx/Rx channel.
39 (+++) Configure the USARTx interrupt priority and enable the NVIC USART IRQ handle
40 (used for last byte sending completion detection in DMA non circular mode)
42 (#) Program the Baud Rate, Word Length, Stop Bit, Parity, Hardware
43 flow control and Mode(Receiver/Transmitter) in the SMARTCARD Init structure.
45 (#) Initialize the SMARTCARD registers by calling the HAL_SMARTCARD_Init() API:
46 (++) These APIs configure also the low level Hardware GPIO, CLOCK, CORTEX...etc)
47 by calling the customized HAL_SMARTCARD_MspInit() API.
49 (@)The specific SMARTCARD interrupts (Transmission complete interrupt,
50 RXNE interrupt and Error Interrupts) will be managed using the macros
51 __HAL_SMARTCARD_ENABLE_IT() and __HAL_SMARTCARD_DISABLE_IT() inside the transmit and receive process.
54 Three operation modes are available within this driver:
56 *** Polling mode IO operation ***
57 =================================
59 (+) Send an amount of data in blocking mode using HAL_SMARTCARD_Transmit()
60 (+) Receive an amount of data in blocking mode using HAL_SMARTCARD_Receive()
61 *** Interrupt mode IO operation ***
62 ===================================
64 (+) Send an amount of data in non blocking mode using HAL_SMARTCARD_Transmit_IT()
65 (+) At transmission end of transfer HAL_SMARTCARD_TxCpltCallback is executed and user can
66 add his own code by customization of function pointer HAL_SMARTCARD_TxCpltCallback
67 (+) Receive an amount of data in non blocking mode using HAL_SMARTCARD_Receive_IT()
68 (+) At reception end of transfer HAL_SMARTCARD_RxCpltCallback is executed and user can
69 add his own code by customization of function pointer HAL_SMARTCARD_RxCpltCallback
70 (+) In case of transfer Error, HAL_SMARTCARD_ErrorCallback() function is executed and user can
71 add his own code by customization of function pointer HAL_SMARTCARD_ErrorCallback
73 *** DMA mode IO operation ***
74 ==============================
76 (+) Send an amount of data in non blocking mode (DMA) using HAL_SMARTCARD_Transmit_DMA()
77 (+) At transmission end of transfer HAL_SMARTCARD_TxCpltCallback is executed and user can
78 add his own code by customization of function pointer HAL_SMARTCARD_TxCpltCallback
79 (+) Receive an amount of data in non blocking mode (DMA) using HAL_SMARTCARD_Receive_DMA()
80 (+) At reception end of transfer HAL_SMARTCARD_RxCpltCallback is executed and user can
81 add his own code by customization of function pointer HAL_SMARTCARD_RxCpltCallback
82 (+) In case of transfer Error, HAL_SMARTCARD_ErrorCallback() function is executed and user can
83 add his own code by customization of function pointer HAL_SMARTCARD_ErrorCallback
85 *** SMARTCARD HAL driver macros list ***
86 =============================================
88 Below the list of most used macros in SMARTCARD HAL driver.
90 (+) __HAL_SMARTCARD_ENABLE: Enable the SMARTCARD peripheral
91 (+) __HAL_SMARTCARD_DISABLE: Disable the SMARTCARD peripheral
92 (+) __HAL_SMARTCARD_GET_FLAG : Check whether the specified SMARTCARD flag is set or not
93 (+) __HAL_SMARTCARD_CLEAR_FLAG : Clear the specified SMARTCARD pending flag
94 (+) __HAL_SMARTCARD_ENABLE_IT: Enable the specified SMARTCARD interrupt
95 (+) __HAL_SMARTCARD_DISABLE_IT: Disable the specified SMARTCARD interrupt
98 (@) You can refer to the SMARTCARD HAL driver header file for more useful macros
102 (@) Additionnal remark: If the parity is enabled, then the MSB bit of the data written
103 in the data register is transmitted but is changed by the parity bit.
104 Depending on the frame length defined by the M bit (8-bits or 9-bits),
105 the possible SMARTCARD frame formats are as listed in the following table:
106 +-------------------------------------------------------------+
107 | M bit | PCE bit | SMARTCARD frame |
108 |---------------------|---------------------------------------|
109 | 1 | 1 | | SB | 8 bit data | PB | STB | |
110 +-------------------------------------------------------------+
111 ******************************************************************************
114 * <h2><center>© COPYRIGHT(c) 2017 STMicroelectronics</center></h2>
116 * Redistribution and use in source and binary forms, with or without modification,
117 * are permitted provided that the following conditions are met:
118 * 1. Redistributions of source code must retain the above copyright notice,
119 * this list of conditions and the following disclaimer.
120 * 2. Redistributions in binary form must reproduce the above copyright notice,
121 * this list of conditions and the following disclaimer in the documentation
122 * and/or other materials provided with the distribution.
123 * 3. Neither the name of STMicroelectronics nor the names of its contributors
124 * may be used to endorse or promote products derived from this software
125 * without specific prior written permission.
127 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
128 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
129 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
130 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
131 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
132 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
133 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
134 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
135 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
136 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
138 ******************************************************************************
141 /* Includes ------------------------------------------------------------------*/
142 #include "stm32f1xx_hal.h"
144 /** @addtogroup STM32F1xx_HAL_Driver
148 /** @defgroup SMARTCARD SMARTCARD
149 * @brief HAL SMARTCARD module driver
152 #ifdef HAL_SMARTCARD_MODULE_ENABLED
153 /* Private typedef -----------------------------------------------------------*/
154 /* Private define ------------------------------------------------------------*/
155 /** @addtogroup SMARTCARD_Private_Constants
161 /* Private macro -------------------------------------------------------------*/
162 /* Private variables ---------------------------------------------------------*/
163 /* Private function prototypes -----------------------------------------------*/
164 /** @addtogroup SMARTCARD_Private_Functions
167 static void SMARTCARD_EndTxTransfer(SMARTCARD_HandleTypeDef
*hsc
);
168 static void SMARTCARD_EndRxTransfer(SMARTCARD_HandleTypeDef
*hsc
);
169 static void SMARTCARD_SetConfig (SMARTCARD_HandleTypeDef
*hsc
);
170 static HAL_StatusTypeDef
SMARTCARD_Transmit_IT(SMARTCARD_HandleTypeDef
*hsc
);
171 static HAL_StatusTypeDef
SMARTCARD_EndTransmit_IT(SMARTCARD_HandleTypeDef
*hsmartcard
);
172 static HAL_StatusTypeDef
SMARTCARD_Receive_IT(SMARTCARD_HandleTypeDef
*hsc
);
173 static void SMARTCARD_DMATransmitCplt(DMA_HandleTypeDef
*hdma
);
174 static void SMARTCARD_DMAReceiveCplt(DMA_HandleTypeDef
*hdma
);
175 static void SMARTCARD_DMAError(DMA_HandleTypeDef
*hdma
);
176 static void SMARTCARD_DMAAbortOnError(DMA_HandleTypeDef
*hdma
);
177 static void SMARTCARD_DMATxAbortCallback(DMA_HandleTypeDef
*hdma
);
178 static void SMARTCARD_DMARxAbortCallback(DMA_HandleTypeDef
*hdma
);
179 static void SMARTCARD_DMATxOnlyAbortCallback(DMA_HandleTypeDef
*hdma
);
180 static void SMARTCARD_DMARxOnlyAbortCallback(DMA_HandleTypeDef
*hdma
);
181 static HAL_StatusTypeDef
SMARTCARD_WaitOnFlagUntilTimeout(SMARTCARD_HandleTypeDef
*hsc
, uint32_t Flag
, FlagStatus Status
, uint32_t Tickstart
, uint32_t Timeout
);
185 /* Exported functions --------------------------------------------------------*/
186 /** @defgroup SMARTCARD_Exported_Functions SMARTCARD Exported Functions
190 /** @defgroup SMARTCARD_Exported_Functions_Group1 SmartCard Initialization and de-initialization functions
191 * @brief Initialization and Configuration functions
194 ==============================================================================
195 ##### Initialization and Configuration functions #####
196 ==============================================================================
198 This subsection provides a set of functions allowing to initialize the USART
201 The Smartcard interface is designed to support asynchronous protocol Smartcards as
202 defined in the ISO 7816-3 standard.
204 The USART can provide a clock to the smartcard through the SCLK output.
205 In smartcard mode, SCLK is not associated to the communication but is simply derived
206 from the internal peripheral input clock through a 5-bit prescaler.
208 (+) For the Smartcard mode only these parameters can be configured:
210 (++) Word Length => Should be 9 bits (8 bits + parity)
212 (++) Parity: => Should be enabled
216 (++) Receiver/transmitter modes
219 (++) NACKState: The Smartcard NACK state
221 (+) Recommended SmartCard interface configuration to get the Answer to Reset from the Card:
222 (++) Word Length = 9 Bits
225 (++) BaudRate = 12096 baud
226 (++) Tx and Rx enabled
228 Please refer to the ISO 7816-3 specification for more details.
231 (@) It is also possible to choose 0.5 stop bit for receiving but it is recommended
232 to use 1.5 stop bits for both transmitting and receiving to avoid switching
233 between the two configurations.
235 The HAL_SMARTCARD_Init() function follows the USART SmartCard configuration
236 procedure (details for the procedure are available in reference manual (RM0329)).
243 * @brief Initializes the SmartCard mode according to the specified
244 * parameters in the SMARTCARD_InitTypeDef and create the associated handle.
245 * @param hsc: pointer to a SMARTCARD_HandleTypeDef structure that contains
246 * the configuration information for SMARTCARD module.
249 HAL_StatusTypeDef
HAL_SMARTCARD_Init(SMARTCARD_HandleTypeDef
*hsc
)
251 /* Check the SMARTCARD handle allocation */
257 /* Check the parameters */
258 assert_param(IS_SMARTCARD_INSTANCE(hsc
->Instance
));
261 if(hsc
->gState
== HAL_SMARTCARD_STATE_RESET
)
263 /* Allocate lock resource and initialize it */
264 hsc
->Lock
= HAL_UNLOCKED
;
266 /* Init the low level hardware : GPIO, CLOCK, CORTEX...etc */
267 HAL_SMARTCARD_MspInit(hsc
);
270 hsc
->gState
= HAL_SMARTCARD_STATE_BUSY
;
272 /* Set the Prescaler */
273 MODIFY_REG(hsc
->Instance
->GTPR
, USART_GTPR_PSC
, hsc
->Init
.Prescaler
);
275 /* Set the Guard Time */
276 MODIFY_REG(hsc
->Instance
->GTPR
, USART_GTPR_GT
, ((hsc
->Init
.GuardTime
)<<8U));
278 /* Set the Smartcard Communication parameters */
279 SMARTCARD_SetConfig(hsc
);
281 /* In SmartCard mode, the following bits must be kept cleared:
282 - LINEN bit in the USART_CR2 register
283 - HDSEL and IREN bits in the USART_CR3 register.*/
284 CLEAR_BIT(hsc
->Instance
->CR2
, USART_CR2_LINEN
);
285 CLEAR_BIT(hsc
->Instance
->CR3
, (USART_CR3_IREN
| USART_CR3_HDSEL
));
287 /* Enable the SMARTCARD Parity Error Interrupt */
288 SET_BIT(hsc
->Instance
->CR1
, USART_CR1_PEIE
);
290 /* Enable the SMARTCARD Framing Error Interrupt */
291 SET_BIT(hsc
->Instance
->CR3
, USART_CR3_EIE
);
293 /* Enable the Peripheral */
294 __HAL_SMARTCARD_ENABLE(hsc
);
296 /* Configure the Smartcard NACK state */
297 MODIFY_REG(hsc
->Instance
->CR3
, USART_CR3_NACK
, hsc
->Init
.NACKState
);
299 /* Enable the SC mode by setting the SCEN bit in the CR3 register */
300 hsc
->Instance
->CR3
|= (USART_CR3_SCEN
);
302 /* Initialize the SMARTCARD state*/
303 hsc
->ErrorCode
= HAL_SMARTCARD_ERROR_NONE
;
304 hsc
->gState
= HAL_SMARTCARD_STATE_READY
;
305 hsc
->RxState
= HAL_SMARTCARD_STATE_READY
;
311 * @brief DeInitializes the USART SmartCard peripheral
312 * @param hsc: pointer to a SMARTCARD_HandleTypeDef structure that contains
313 * the configuration information for SMARTCARD module.
316 HAL_StatusTypeDef
HAL_SMARTCARD_DeInit(SMARTCARD_HandleTypeDef
*hsc
)
318 /* Check the SMARTCARD handle allocation */
324 /* Check the parameters */
325 assert_param(IS_SMARTCARD_INSTANCE(hsc
->Instance
));
327 hsc
->gState
= HAL_SMARTCARD_STATE_BUSY
;
329 /* DeInit the low level hardware */
330 HAL_SMARTCARD_MspDeInit(hsc
);
332 hsc
->ErrorCode
= HAL_SMARTCARD_ERROR_NONE
;
333 hsc
->gState
= HAL_SMARTCARD_STATE_RESET
;
334 hsc
->RxState
= HAL_SMARTCARD_STATE_RESET
;
343 * @brief SMARTCARD MSP Init.
344 * @param hsc: pointer to a SMARTCARD_HandleTypeDef structure that contains
345 * the configuration information for SMARTCARD module.
348 __weak
void HAL_SMARTCARD_MspInit(SMARTCARD_HandleTypeDef
*hsc
)
350 /* Prevent unused argument(s) compilation warning */
352 /* NOTE: This function should not be modified, when the callback is needed,
353 the HAL_SMARTCARD_MspInit could be implemented in the user file
358 * @brief SMARTCARD MSP DeInit
359 * @param hsc: pointer to a SMARTCARD_HandleTypeDef structure that contains
360 * the configuration information for SMARTCARD module.
363 __weak
void HAL_SMARTCARD_MspDeInit(SMARTCARD_HandleTypeDef
*hsc
)
365 /* Prevent unused argument(s) compilation warning */
367 /* NOTE: This function should not be modified, when the callback is needed,
368 the HAL_SMARTCARD_MspDeInit could be implemented in the user file
376 /** @defgroup SMARTCARD_Exported_Functions_Group2 IO operation functions
377 * @brief SMARTCARD Transmit and Receive functions
380 ===============================================================================
381 ##### IO operation functions #####
382 ===============================================================================
384 This subsection provides a set of functions allowing to manage the SMARTCARD data transfers.
387 (#) Smartcard is a single wire half duplex communication protocol.
388 The Smartcard interface is designed to support asynchronous protocol Smartcards as
389 defined in the ISO 7816-3 standard.
390 (#) The USART should be configured as:
391 (++) 8 bits plus parity: where M=1 and PCE=1 in the USART_CR1 register
392 (++) 1.5 stop bits when transmitting and receiving: where STOP=11 in the USART_CR2 register.
394 (#) There are two modes of transfer:
395 (++) Blocking mode: The communication is performed in polling mode.
396 The HAL status of all data processing is returned by the same function
397 after finishing transfer.
398 (++) Non Blocking mode: The communication is performed using Interrupts
399 or DMA, These APIs return the HAL status.
400 The end of the data processing will be indicated through the
401 dedicated SMARTCARD IRQ when using Interrupt mode or the DMA IRQ when
403 The HAL_SMARTCARD_TxCpltCallback(), HAL_SMARTCARD_RxCpltCallback() user callbacks
404 will be executed respectively at the end of the Transmit or Receive process
405 The HAL_SMARTCARD_ErrorCallback() user callback will be executed when a communication error is detected
407 (#) Blocking mode APIs are :
408 (++) HAL_SMARTCARD_Transmit()
409 (++) HAL_SMARTCARD_Receive()
411 (#) Non Blocking mode APIs with Interrupt are :
412 (++) HAL_SMARTCARD_Transmit_IT()
413 (++) HAL_SMARTCARD_Receive_IT()
414 (++) HAL_SMARTCARD_IRQHandler()
416 (#) Non Blocking mode functions with DMA are :
417 (++) HAL_SMARTCARD_Transmit_DMA()
418 (++) HAL_SMARTCARD_Receive_DMA()
420 (#) A set of Transfer Complete Callbacks are provided in non Blocking mode:
421 (++) HAL_SMARTCARD_TxCpltCallback()
422 (++) HAL_SMARTCARD_RxCpltCallback()
423 (++) HAL_SMARTCARD_ErrorCallback()
430 * @brief Send an amount of data in blocking mode
431 * @param hsc: pointer to a SMARTCARD_HandleTypeDef structure that contains
432 * the configuration information for SMARTCARD module.
433 * @param pData: pointer to data buffer
434 * @param Size: amount of data to be sent
435 * @param Timeout: Timeout duration
438 HAL_StatusTypeDef
HAL_SMARTCARD_Transmit(SMARTCARD_HandleTypeDef
*hsc
, uint8_t *pData
, uint16_t Size
, uint32_t Timeout
)
441 uint32_t tickstart
= 0U;
443 if(hsc
->gState
== HAL_SMARTCARD_STATE_READY
)
445 if((pData
== NULL
) || (Size
== 0U))
453 hsc
->ErrorCode
= HAL_SMARTCARD_ERROR_NONE
;
454 hsc
->gState
= HAL_SMARTCARD_STATE_BUSY_TX
;
456 /* Init tickstart for timeout managment */
457 tickstart
= HAL_GetTick();
459 hsc
->TxXferSize
= Size
;
460 hsc
->TxXferCount
= Size
;
461 while(hsc
->TxXferCount
> 0U)
464 if(SMARTCARD_WaitOnFlagUntilTimeout(hsc
, SMARTCARD_FLAG_TXE
, RESET
, tickstart
, Timeout
) != HAL_OK
)
468 tmp
= (uint16_t*) pData
;
469 hsc
->Instance
->DR
= (*tmp
& (uint16_t)0x01FF);
473 if(SMARTCARD_WaitOnFlagUntilTimeout(hsc
, SMARTCARD_FLAG_TC
, RESET
, tickstart
, Timeout
) != HAL_OK
)
478 /* At end of Tx process, restore hsc->gState to Ready */
479 hsc
->gState
= HAL_SMARTCARD_STATE_READY
;
481 /* Process Unlocked */
493 * @brief Receive an amount of data in blocking mode
494 * @param hsc: pointer to a SMARTCARD_HandleTypeDef structure that contains
495 * the configuration information for SMARTCARD module.
496 * @param pData: pointer to data buffer
497 * @param Size: amount of data to be received
498 * @param Timeout: Timeout duration
501 HAL_StatusTypeDef
HAL_SMARTCARD_Receive(SMARTCARD_HandleTypeDef
*hsc
, uint8_t *pData
, uint16_t Size
, uint32_t Timeout
)
504 uint32_t tickstart
= 0U;
506 if(hsc
->RxState
== HAL_SMARTCARD_STATE_READY
)
508 if((pData
== NULL
) || (Size
== 0U))
516 hsc
->ErrorCode
= HAL_SMARTCARD_ERROR_NONE
;
517 hsc
->RxState
= HAL_SMARTCARD_STATE_BUSY_RX
;
519 /* Init tickstart for timeout managment */
520 tickstart
= HAL_GetTick();
522 hsc
->RxXferSize
= Size
;
523 hsc
->RxXferCount
= Size
;
525 /* Check the remain data to be received */
526 while(hsc
->RxXferCount
> 0U)
529 if(SMARTCARD_WaitOnFlagUntilTimeout(hsc
, SMARTCARD_FLAG_RXNE
, RESET
, tickstart
, Timeout
) != HAL_OK
)
533 tmp
= (uint16_t*) pData
;
534 *tmp
= (uint8_t)(hsc
->Instance
->DR
& (uint8_t)0xFF);
538 /* At end of Rx process, restore hsc->RxState to Ready */
539 hsc
->RxState
= HAL_SMARTCARD_STATE_READY
;
541 /* Process Unlocked */
553 * @brief Send an amount of data in non blocking mode
554 * @param hsc: pointer to a SMARTCARD_HandleTypeDef structure that contains
555 * the configuration information for SMARTCARD module.
556 * @param pData: pointer to data buffer
557 * @param Size: amount of data to be sent
560 HAL_StatusTypeDef
HAL_SMARTCARD_Transmit_IT(SMARTCARD_HandleTypeDef
*hsc
, uint8_t *pData
, uint16_t Size
)
562 /* Check that a Tx process is not already ongoing */
563 if(hsc
->gState
== HAL_SMARTCARD_STATE_READY
)
565 if((pData
== NULL
) || (Size
== 0U))
572 hsc
->pTxBuffPtr
= pData
;
573 hsc
->TxXferSize
= Size
;
574 hsc
->TxXferCount
= Size
;
576 hsc
->ErrorCode
= HAL_SMARTCARD_ERROR_NONE
;
577 hsc
->gState
= HAL_SMARTCARD_STATE_BUSY_TX
;
579 /* Process Unlocked */
582 /* Enable the SMARTCARD Parity Error Interrupt */
583 SET_BIT(hsc
->Instance
->CR1
, USART_CR1_PEIE
);
585 /* Disable the SMARTCARD Error Interrupt: (Frame error, noise error, overrun error) */
586 CLEAR_BIT(hsc
->Instance
->CR3
, USART_CR3_EIE
);
588 /* Enable the SMARTCARD Transmit data register empty Interrupt */
589 SET_BIT(hsc
->Instance
->CR1
, USART_CR1_TXEIE
);
600 * @brief Receive an amount of data in non blocking mode
601 * @param hsc: pointer to a SMARTCARD_HandleTypeDef structure that contains
602 * the configuration information for SMARTCARD module.
603 * @param pData: pointer to data buffer
604 * @param Size: amount of data to be received
607 HAL_StatusTypeDef
HAL_SMARTCARD_Receive_IT(SMARTCARD_HandleTypeDef
*hsc
, uint8_t *pData
, uint16_t Size
)
609 /* Check that a Rx process is not already ongoing */
610 if(hsc
->RxState
== HAL_SMARTCARD_STATE_READY
)
612 if((pData
== NULL
) || (Size
== 0U))
620 hsc
->pRxBuffPtr
= pData
;
621 hsc
->RxXferSize
= Size
;
622 hsc
->RxXferCount
= Size
;
624 hsc
->ErrorCode
= HAL_SMARTCARD_ERROR_NONE
;
625 hsc
->RxState
= HAL_SMARTCARD_STATE_BUSY_RX
;
627 /* Process Unlocked */
630 /* Enable the SMARTCARD Parity Error and Data Register not empty Interrupts */
631 SET_BIT(hsc
->Instance
->CR1
, USART_CR1_PEIE
| USART_CR1_RXNEIE
);
633 /* Enable the SMARTCARD Error Interrupt: (Frame error, noise error, overrun error) */
634 SET_BIT(hsc
->Instance
->CR3
, USART_CR3_EIE
);
645 * @brief Send an amount of data in non blocking mode
646 * @param hsc: pointer to a SMARTCARD_HandleTypeDef structure that contains
647 * the configuration information for SMARTCARD module.
648 * @param pData: pointer to data buffer
649 * @param Size: amount of data to be sent
652 HAL_StatusTypeDef
HAL_SMARTCARD_Transmit_DMA(SMARTCARD_HandleTypeDef
*hsc
, uint8_t *pData
, uint16_t Size
)
656 /* Check that a Tx process is not already ongoing */
657 if(hsc
->gState
== HAL_SMARTCARD_STATE_READY
)
659 if((pData
== NULL
) || (Size
== 0U))
667 hsc
->pTxBuffPtr
= pData
;
668 hsc
->TxXferSize
= Size
;
669 hsc
->TxXferCount
= Size
;
671 hsc
->ErrorCode
= HAL_SMARTCARD_ERROR_NONE
;
672 hsc
->gState
= HAL_SMARTCARD_STATE_BUSY_TX
;
674 /* Set the SMARTCARD DMA transfer complete callback */
675 hsc
->hdmatx
->XferCpltCallback
= SMARTCARD_DMATransmitCplt
;
677 /* Set the DMA error callback */
678 hsc
->hdmatx
->XferErrorCallback
= SMARTCARD_DMAError
;
680 /* Set the DMA abort callback */
681 hsc
->hdmatx
->XferAbortCallback
= NULL
;
683 /* Enable the SMARTCARD transmit DMA Channel */
684 tmp
= (uint32_t*)&pData
;
685 HAL_DMA_Start_IT(hsc
->hdmatx
, *(uint32_t*)tmp
, (uint32_t)&hsc
->Instance
->DR
, Size
);
687 /* Clear the TC flag in the SR register by writing 0 to it */
688 __HAL_SMARTCARD_CLEAR_FLAG(hsc
, SMARTCARD_FLAG_TC
);
690 /* Process Unlocked */
693 /* Enable the DMA transfer for transmit request by setting the DMAT bit
694 in the SMARTCARD CR3 register */
695 SET_BIT(hsc
->Instance
->CR3
, USART_CR3_DMAT
);
706 * @brief Receive an amount of data in non blocking mode
707 * @param hsc: pointer to a SMARTCARD_HandleTypeDef structure that contains
708 * the configuration information for SMARTCARD module.
709 * @param pData: pointer to data buffer
710 * @param Size: amount of data to be received
711 * @note When the SMARTCARD parity is enabled (PCE = 1) the data received contain the parity bit.s
714 HAL_StatusTypeDef
HAL_SMARTCARD_Receive_DMA(SMARTCARD_HandleTypeDef
*hsc
, uint8_t *pData
, uint16_t Size
)
718 /* Check that a Rx process is not already ongoing */
719 if(hsc
->RxState
== HAL_SMARTCARD_STATE_READY
)
721 if((pData
== NULL
) || (Size
== 0U))
729 hsc
->pRxBuffPtr
= pData
;
730 hsc
->RxXferSize
= Size
;
732 hsc
->ErrorCode
= HAL_SMARTCARD_ERROR_NONE
;
733 hsc
->RxState
= HAL_SMARTCARD_STATE_BUSY_RX
;
735 /* Set the SMARTCARD DMA transfer complete callback */
736 hsc
->hdmarx
->XferCpltCallback
= SMARTCARD_DMAReceiveCplt
;
738 /* Set the DMA error callback */
739 hsc
->hdmarx
->XferErrorCallback
= SMARTCARD_DMAError
;
741 /* Set the DMA abort callback */
742 hsc
->hdmatx
->XferAbortCallback
= NULL
;
744 /* Enable the DMA Channel */
745 tmp
= (uint32_t*)&pData
;
746 HAL_DMA_Start_IT(hsc
->hdmarx
, (uint32_t)&hsc
->Instance
->DR
, *(uint32_t*)tmp
, Size
);
748 /* Clear the Overrun flag just before enabling the DMA Rx request: can be mandatory for the second transfer */
749 __HAL_SMARTCARD_CLEAR_OREFLAG(hsc
);
751 /* Process Unlocked */
754 /* Enable the SMARTCARD Parity Error Interrupt */
755 SET_BIT(hsc
->Instance
->CR1
, USART_CR1_PEIE
);
757 /* Enable the SMARTCARD Error Interrupt: (Frame error, noise error, overrun error) */
758 SET_BIT(hsc
->Instance
->CR3
, USART_CR3_EIE
);
760 /* Enable the DMA transfer for the receiver request by setting the DMAR bit
761 in the SMARTCARD CR3 register */
762 SET_BIT(hsc
->Instance
->CR3
, USART_CR3_DMAR
);
773 * @brief Abort ongoing transfers (blocking mode).
774 * @param hsc SMARTCARD handle.
775 * @note This procedure could be used for aborting any ongoing transfer started in Interrupt or DMA mode.
776 * This procedure performs following operations :
777 * - Disable PPP Interrupts
778 * - Disable the DMA transfer in the peripheral register (if enabled)
779 * - Abort DMA transfer by calling HAL_DMA_Abort (in case of transfer in DMA mode)
780 * - Set handle State to READY
781 * @note This procedure is executed in blocking mode : when exiting function, Abort is considered as completed.
784 HAL_StatusTypeDef
HAL_SMARTCARD_Abort(SMARTCARD_HandleTypeDef
*hsc
)
786 /* Disable TXEIE, TCIE, RXNE, PE and ERR (Frame error, noise error, overrun error) interrupts */
787 CLEAR_BIT(hsc
->Instance
->CR1
, (USART_CR1_RXNEIE
| USART_CR1_PEIE
| USART_CR1_TXEIE
| USART_CR1_TCIE
));
788 CLEAR_BIT(hsc
->Instance
->CR3
, USART_CR3_EIE
);
790 /* Disable the SMARTCARD DMA Tx request if enabled */
791 if(HAL_IS_BIT_SET(hsc
->Instance
->CR3
, USART_CR3_DMAT
))
793 CLEAR_BIT(hsc
->Instance
->CR3
, USART_CR3_DMAT
);
795 /* Abort the SMARTCARD DMA Tx channel: use blocking DMA Abort API (no callback) */
796 if(hsc
->hdmatx
!= NULL
)
798 /* Set the SMARTCARD DMA Abort callback to Null.
799 No call back execution at end of DMA abort procedure */
800 hsc
->hdmatx
->XferAbortCallback
= NULL
;
802 HAL_DMA_Abort(hsc
->hdmatx
);
806 /* Disable the SMARTCARD DMA Rx request if enabled */
807 if(HAL_IS_BIT_SET(hsc
->Instance
->CR3
, USART_CR3_DMAR
))
809 CLEAR_BIT(hsc
->Instance
->CR3
, USART_CR3_DMAR
);
811 /* Abort the SMARTCARD DMA Rx channel: use blocking DMA Abort API (no callback) */
812 if(hsc
->hdmarx
!= NULL
)
814 /* Set the SMARTCARD DMA Abort callback to Null.
815 No call back execution at end of DMA abort procedure */
816 hsc
->hdmarx
->XferAbortCallback
= NULL
;
818 HAL_DMA_Abort(hsc
->hdmarx
);
822 /* Reset Tx and Rx transfer counters */
823 hsc
->TxXferCount
= 0x00U
;
824 hsc
->RxXferCount
= 0x00U
;
826 /* Reset ErrorCode */
827 hsc
->ErrorCode
= HAL_SMARTCARD_ERROR_NONE
;
829 /* Restore hsc->RxState and hsc->gState to Ready */
830 hsc
->RxState
= HAL_SMARTCARD_STATE_READY
;
831 hsc
->gState
= HAL_SMARTCARD_STATE_READY
;
837 * @brief Abort ongoing Transmit transfer (blocking mode).
838 * @param hsc SMARTCARD handle.
839 * @note This procedure could be used for aborting any ongoing transfer started in Interrupt or DMA mode.
840 * This procedure performs following operations :
841 * - Disable PPP Interrupts
842 * - Disable the DMA transfer in the peripheral register (if enabled)
843 * - Abort DMA transfer by calling HAL_DMA_Abort (in case of transfer in DMA mode)
844 * - Set handle State to READY
845 * @note This procedure is executed in blocking mode : when exiting function, Abort is considered as completed.
848 HAL_StatusTypeDef
HAL_SMARTCARD_AbortTransmit(SMARTCARD_HandleTypeDef
*hsc
)
850 /* Disable TXEIE and TCIE interrupts */
851 CLEAR_BIT(hsc
->Instance
->CR1
, (USART_CR1_TXEIE
| USART_CR1_TCIE
));
853 /* Disable the SMARTCARD DMA Tx request if enabled */
854 if(HAL_IS_BIT_SET(hsc
->Instance
->CR3
, USART_CR3_DMAT
))
856 CLEAR_BIT(hsc
->Instance
->CR3
, USART_CR3_DMAT
);
858 /* Abort the SMARTCARD DMA Tx channel: use blocking DMA Abort API (no callback) */
859 if(hsc
->hdmatx
!= NULL
)
861 /* Set the SMARTCARD DMA Abort callback to Null.
862 No call back execution at end of DMA abort procedure */
863 hsc
->hdmatx
->XferAbortCallback
= NULL
;
865 HAL_DMA_Abort(hsc
->hdmatx
);
869 /* Reset Tx transfer counter */
870 hsc
->TxXferCount
= 0x00U
;
872 /* Restore hsc->gState to Ready */
873 hsc
->gState
= HAL_SMARTCARD_STATE_READY
;
879 * @brief Abort ongoing Receive transfer (blocking mode).
880 * @param hsc SMARTCARD handle.
881 * @note This procedure could be used for aborting any ongoing transfer started in Interrupt or DMA mode.
882 * This procedure performs following operations :
883 * - Disable PPP Interrupts
884 * - Disable the DMA transfer in the peripheral register (if enabled)
885 * - Abort DMA transfer by calling HAL_DMA_Abort (in case of transfer in DMA mode)
886 * - Set handle State to READY
887 * @note This procedure is executed in blocking mode : when exiting function, Abort is considered as completed.
890 HAL_StatusTypeDef
HAL_SMARTCARD_AbortReceive(SMARTCARD_HandleTypeDef
*hsc
)
892 /* Disable RXNE, PE and ERR (Frame error, noise error, overrun error) interrupts */
893 CLEAR_BIT(hsc
->Instance
->CR1
, (USART_CR1_RXNEIE
| USART_CR1_PEIE
));
894 CLEAR_BIT(hsc
->Instance
->CR3
, USART_CR3_EIE
);
896 /* Disable the SMARTCARD DMA Rx request if enabled */
897 if(HAL_IS_BIT_SET(hsc
->Instance
->CR3
, USART_CR3_DMAR
))
899 CLEAR_BIT(hsc
->Instance
->CR3
, USART_CR3_DMAR
);
901 /* Abort the SMARTCARD DMA Rx channel: use blocking DMA Abort API (no callback) */
902 if(hsc
->hdmarx
!= NULL
)
904 /* Set the SMARTCARD DMA Abort callback to Null.
905 No call back execution at end of DMA abort procedure */
906 hsc
->hdmarx
->XferAbortCallback
= NULL
;
908 HAL_DMA_Abort(hsc
->hdmarx
);
912 /* Reset Rx transfer counter */
913 hsc
->RxXferCount
= 0x00U
;
915 /* Restore hsc->RxState to Ready */
916 hsc
->RxState
= HAL_SMARTCARD_STATE_READY
;
922 * @brief Abort ongoing transfers (Interrupt mode).
923 * @param hsc SMARTCARD handle.
924 * @note This procedure could be used for aborting any ongoing transfer started in Interrupt or DMA mode.
925 * This procedure performs following operations :
926 * - Disable PPP Interrupts
927 * - Disable the DMA transfer in the peripheral register (if enabled)
928 * - Abort DMA transfer by calling HAL_DMA_Abort_IT (in case of transfer in DMA mode)
929 * - Set handle State to READY
930 * - At abort completion, call user abort complete callback
931 * @note This procedure is executed in Interrupt mode, meaning that abort procedure could be
932 * considered as completed only when user abort complete callback is executed (not when exiting function).
935 HAL_StatusTypeDef
HAL_SMARTCARD_Abort_IT(SMARTCARD_HandleTypeDef
*hsc
)
937 uint32_t AbortCplt
= 0x01U
;
939 /* Disable TXEIE, TCIE, RXNE, PE and ERR (Frame error, noise error, overrun error) interrupts */
940 CLEAR_BIT(hsc
->Instance
->CR1
, (USART_CR1_RXNEIE
| USART_CR1_PEIE
| USART_CR1_TXEIE
| USART_CR1_TCIE
));
941 CLEAR_BIT(hsc
->Instance
->CR3
, USART_CR3_EIE
);
943 /* If DMA Tx and/or DMA Rx Handles are associated to SMARTCARD Handle, DMA Abort complete callbacks should be initialised
944 before any call to DMA Abort functions */
945 /* DMA Tx Handle is valid */
946 if(hsc
->hdmatx
!= NULL
)
948 /* Set DMA Abort Complete callback if SMARTCARD DMA Tx request if enabled.
949 Otherwise, set it to NULL */
950 if(HAL_IS_BIT_SET(hsc
->Instance
->CR3
, USART_CR3_DMAT
))
952 hsc
->hdmatx
->XferAbortCallback
= SMARTCARD_DMATxAbortCallback
;
956 hsc
->hdmatx
->XferAbortCallback
= NULL
;
959 /* DMA Rx Handle is valid */
960 if(hsc
->hdmarx
!= NULL
)
962 /* Set DMA Abort Complete callback if SMARTCARD DMA Rx request if enabled.
963 Otherwise, set it to NULL */
964 if(HAL_IS_BIT_SET(hsc
->Instance
->CR3
, USART_CR3_DMAR
))
966 hsc
->hdmarx
->XferAbortCallback
= SMARTCARD_DMARxAbortCallback
;
970 hsc
->hdmarx
->XferAbortCallback
= NULL
;
974 /* Disable the SMARTCARD DMA Tx request if enabled */
975 if(HAL_IS_BIT_SET(hsc
->Instance
->CR3
, USART_CR3_DMAT
))
977 /* Disable DMA Tx at SMARTCARD level */
978 CLEAR_BIT(hsc
->Instance
->CR3
, USART_CR3_DMAT
);
980 /* Abort the SMARTCARD DMA Tx channel : use non blocking DMA Abort API (callback) */
981 if(hsc
->hdmatx
!= NULL
)
983 /* SMARTCARD Tx DMA Abort callback has already been initialised :
984 will lead to call HAL_SMARTCARD_AbortCpltCallback() at end of DMA abort procedure */
987 if(HAL_DMA_Abort_IT(hsc
->hdmatx
) != HAL_OK
)
989 hsc
->hdmatx
->XferAbortCallback
= NULL
;
998 /* Disable the SMARTCARD DMA Rx request if enabled */
999 if(HAL_IS_BIT_SET(hsc
->Instance
->CR3
, USART_CR3_DMAR
))
1001 CLEAR_BIT(hsc
->Instance
->CR3
, USART_CR3_DMAR
);
1003 /* Abort the SMARTCARD DMA Rx channel : use non blocking DMA Abort API (callback) */
1004 if(hsc
->hdmarx
!= NULL
)
1006 /* SMARTCARD Rx DMA Abort callback has already been initialised :
1007 will lead to call HAL_SMARTCARD_AbortCpltCallback() at end of DMA abort procedure */
1010 if(HAL_DMA_Abort_IT(hsc
->hdmarx
) != HAL_OK
)
1012 hsc
->hdmarx
->XferAbortCallback
= NULL
;
1022 /* if no DMA abort complete callback execution is required => call user Abort Complete callback */
1023 if(AbortCplt
== 0x01U
)
1025 /* Reset Tx and Rx transfer counters */
1026 hsc
->TxXferCount
= 0x00U
;
1027 hsc
->RxXferCount
= 0x00U
;
1029 /* Reset ErrorCode */
1030 hsc
->ErrorCode
= HAL_SMARTCARD_ERROR_NONE
;
1032 /* Restore hsc->gState and hsc->RxState to Ready */
1033 hsc
->gState
= HAL_SMARTCARD_STATE_READY
;
1034 hsc
->RxState
= HAL_SMARTCARD_STATE_READY
;
1036 /* As no DMA to be aborted, call directly user Abort complete callback */
1037 HAL_SMARTCARD_AbortCpltCallback(hsc
);
1043 * @brief Abort ongoing Transmit transfer (Interrupt mode).
1044 * @param hsc SMARTCARD handle.
1045 * @note This procedure could be used for aborting any ongoing transfer started in Interrupt or DMA mode.
1046 * This procedure performs following operations :
1047 * - Disable PPP Interrupts
1048 * - Disable the DMA transfer in the peripheral register (if enabled)
1049 * - Abort DMA transfer by calling HAL_DMA_Abort_IT (in case of transfer in DMA mode)
1050 * - Set handle State to READY
1051 * - At abort completion, call user abort complete callback
1052 * @note This procedure is executed in Interrupt mode, meaning that abort procedure could be
1053 * considered as completed only when user abort complete callback is executed (not when exiting function).
1054 * @retval HAL status
1056 HAL_StatusTypeDef
HAL_SMARTCARD_AbortTransmit_IT(SMARTCARD_HandleTypeDef
*hsc
)
1058 /* Disable TXEIE and TCIE interrupts */
1059 CLEAR_BIT(hsc
->Instance
->CR1
, (USART_CR1_TXEIE
| USART_CR1_TCIE
));
1061 /* Disable the SMARTCARD DMA Tx request if enabled */
1062 if(HAL_IS_BIT_SET(hsc
->Instance
->CR3
, USART_CR3_DMAT
))
1064 CLEAR_BIT(hsc
->Instance
->CR3
, USART_CR3_DMAT
);
1066 /* Abort the SMARTCARD DMA Tx channel : use blocking DMA Abort API (no callback) */
1067 if(hsc
->hdmatx
!= NULL
)
1069 /* Set the SMARTCARD DMA Abort callback :
1070 will lead to call HAL_SMARTCARD_AbortCpltCallback() at end of DMA abort procedure */
1071 hsc
->hdmatx
->XferAbortCallback
= SMARTCARD_DMATxOnlyAbortCallback
;
1074 if(HAL_DMA_Abort_IT(hsc
->hdmatx
) != HAL_OK
)
1076 /* Call Directly hsc->hdmatx->XferAbortCallback function in case of error */
1077 hsc
->hdmatx
->XferAbortCallback(hsc
->hdmatx
);
1082 /* Reset Tx transfer counter */
1083 hsc
->TxXferCount
= 0x00U
;
1085 /* Restore hsc->gState to Ready */
1086 hsc
->gState
= HAL_SMARTCARD_STATE_READY
;
1088 /* As no DMA to be aborted, call directly user Abort complete callback */
1089 HAL_SMARTCARD_AbortTransmitCpltCallback(hsc
);
1094 /* Reset Tx transfer counter */
1095 hsc
->TxXferCount
= 0x00U
;
1097 /* Restore hsc->gState to Ready */
1098 hsc
->gState
= HAL_SMARTCARD_STATE_READY
;
1100 /* As no DMA to be aborted, call directly user Abort complete callback */
1101 HAL_SMARTCARD_AbortTransmitCpltCallback(hsc
);
1108 * @brief Abort ongoing Receive transfer (Interrupt mode).
1109 * @param hsc SMARTCARD handle.
1110 * @note This procedure could be used for aborting any ongoing transfer started in Interrupt or DMA mode.
1111 * This procedure performs following operations :
1112 * - Disable PPP Interrupts
1113 * - Disable the DMA transfer in the peripheral register (if enabled)
1114 * - Abort DMA transfer by calling HAL_DMA_Abort_IT (in case of transfer in DMA mode)
1115 * - Set handle State to READY
1116 * - At abort completion, call user abort complete callback
1117 * @note This procedure is executed in Interrupt mode, meaning that abort procedure could be
1118 * considered as completed only when user abort complete callback is executed (not when exiting function).
1119 * @retval HAL status
1121 HAL_StatusTypeDef
HAL_SMARTCARD_AbortReceive_IT(SMARTCARD_HandleTypeDef
*hsc
)
1123 /* Disable RXNE, PE and ERR (Frame error, noise error, overrun error) interrupts */
1124 CLEAR_BIT(hsc
->Instance
->CR1
, (USART_CR1_RXNEIE
| USART_CR1_PEIE
));
1125 CLEAR_BIT(hsc
->Instance
->CR3
, USART_CR3_EIE
);
1127 /* Disable the SMARTCARD DMA Rx request if enabled */
1128 if(HAL_IS_BIT_SET(hsc
->Instance
->CR3
, USART_CR3_DMAR
))
1130 CLEAR_BIT(hsc
->Instance
->CR3
, USART_CR3_DMAR
);
1132 /* Abort the SMARTCARD DMA Rx channel : use blocking DMA Abort API (no callback) */
1133 if(hsc
->hdmarx
!= NULL
)
1135 /* Set the SMARTCARD DMA Abort callback :
1136 will lead to call HAL_SMARTCARD_AbortCpltCallback() at end of DMA abort procedure */
1137 hsc
->hdmarx
->XferAbortCallback
= SMARTCARD_DMARxOnlyAbortCallback
;
1140 if(HAL_DMA_Abort_IT(hsc
->hdmarx
) != HAL_OK
)
1142 /* Call Directly hsc->hdmarx->XferAbortCallback function in case of error */
1143 hsc
->hdmarx
->XferAbortCallback(hsc
->hdmarx
);
1148 /* Reset Rx transfer counter */
1149 hsc
->RxXferCount
= 0x00U
;
1151 /* Restore hsc->RxState to Ready */
1152 hsc
->RxState
= HAL_SMARTCARD_STATE_READY
;
1154 /* As no DMA to be aborted, call directly user Abort complete callback */
1155 HAL_SMARTCARD_AbortReceiveCpltCallback(hsc
);
1160 /* Reset Rx transfer counter */
1161 hsc
->RxXferCount
= 0x00U
;
1163 /* Restore hsc->RxState to Ready */
1164 hsc
->RxState
= HAL_SMARTCARD_STATE_READY
;
1166 /* As no DMA to be aborted, call directly user Abort complete callback */
1167 HAL_SMARTCARD_AbortReceiveCpltCallback(hsc
);
1174 * @brief This function handles SMARTCARD interrupt request.
1175 * @param hsc: pointer to a SMARTCARD_HandleTypeDef structure that contains
1176 * the configuration information for SMARTCARD module.
1179 void HAL_SMARTCARD_IRQHandler(SMARTCARD_HandleTypeDef
*hsc
)
1181 uint32_t isrflags
= READ_REG(hsc
->Instance
->SR
);
1182 uint32_t cr1its
= READ_REG(hsc
->Instance
->CR1
);
1183 uint32_t cr3its
= READ_REG(hsc
->Instance
->CR3
);
1184 uint32_t dmarequest
= 0x00U
;
1185 uint32_t errorflags
= 0x00U
;
1187 /* If no error occurs */
1188 errorflags
= (isrflags
& (uint32_t)(USART_SR_PE
| USART_SR_FE
| USART_SR_ORE
| USART_SR_NE
));
1189 if(errorflags
== RESET
)
1191 /* SMARTCARD in mode Receiver -------------------------------------------------*/
1192 if(((isrflags
& USART_SR_RXNE
) != RESET
) && ((cr1its
& USART_CR1_RXNEIE
) != RESET
))
1194 SMARTCARD_Receive_IT(hsc
);
1199 /* If some errors occur */
1200 if((errorflags
!= RESET
) && (((cr3its
& USART_CR3_EIE
) != RESET
) || ((cr1its
& (USART_CR1_RXNEIE
| USART_CR1_PEIE
)) != RESET
)))
1202 /* SMARTCARD parity error interrupt occurred ---------------------------*/
1203 if(((isrflags
& SMARTCARD_FLAG_PE
) != RESET
) && ((cr1its
& USART_CR1_PEIE
) != RESET
))
1205 hsc
->ErrorCode
|= HAL_SMARTCARD_ERROR_PE
;
1208 /* SMARTCARD noise error interrupt occurred ----------------------------*/
1209 if(((isrflags
& SMARTCARD_FLAG_NE
) != RESET
) && ((cr3its
& USART_CR3_EIE
) != RESET
))
1211 hsc
->ErrorCode
|= HAL_SMARTCARD_ERROR_NE
;
1214 /* SMARTCARD frame error interrupt occurred ----------------------------*/
1215 if(((isrflags
& SMARTCARD_FLAG_FE
) != RESET
) && ((cr3its
& USART_CR3_EIE
) != RESET
))
1217 hsc
->ErrorCode
|= HAL_SMARTCARD_ERROR_FE
;
1220 /* SMARTCARD Over-Run interrupt occurred -------------------------------*/
1221 if(((isrflags
& SMARTCARD_FLAG_ORE
) != RESET
) && ((cr3its
& USART_CR3_EIE
) != RESET
))
1223 hsc
->ErrorCode
|= HAL_SMARTCARD_ERROR_ORE
;
1226 /* Call SMARTCARD Error Call back function if need be ------------------*/
1227 if(hsc
->ErrorCode
!= HAL_SMARTCARD_ERROR_NONE
)
1229 /* SMARTCARD in mode Receiver ----------------------------------------*/
1230 if(((isrflags
& USART_SR_RXNE
) != RESET
) && ((cr1its
& USART_CR1_RXNEIE
) != RESET
))
1232 SMARTCARD_Receive_IT(hsc
);
1235 /* If Overrun error occurs, or if any error occurs in DMA mode reception,
1236 consider error as blocking */
1237 dmarequest
= HAL_IS_BIT_SET(hsc
->Instance
->CR3
, USART_CR3_DMAR
);
1238 if(((hsc
->ErrorCode
& HAL_SMARTCARD_ERROR_ORE
) != RESET
) || dmarequest
)
1240 /* Blocking error : transfer is aborted
1241 Set the SMARTCARD state ready to be able to start again the process,
1242 Disable Rx Interrupts, and disable Rx DMA request, if ongoing */
1243 SMARTCARD_EndRxTransfer(hsc
);
1245 /* Disable the SMARTCARD DMA Rx request if enabled */
1246 if(HAL_IS_BIT_SET(hsc
->Instance
->CR3
, USART_CR3_DMAR
))
1248 CLEAR_BIT(hsc
->Instance
->CR3
, USART_CR3_DMAR
);
1250 /* Abort the SMARTCARD DMA Rx channel */
1251 if(hsc
->hdmarx
!= NULL
)
1253 /* Set the SMARTCARD DMA Abort callback :
1254 will lead to call HAL_SMARTCARD_ErrorCallback() at end of DMA abort procedure */
1255 hsc
->hdmarx
->XferAbortCallback
= SMARTCARD_DMAAbortOnError
;
1256 if(HAL_DMA_Abort_IT(hsc
->hdmarx
) != HAL_OK
)
1258 /* Call Directly XferAbortCallback function in case of error */
1259 hsc
->hdmarx
->XferAbortCallback(hsc
->hdmarx
);
1264 /* Call user error callback */
1265 HAL_SMARTCARD_ErrorCallback(hsc
);
1270 /* Call user error callback */
1271 HAL_SMARTCARD_ErrorCallback(hsc
);
1276 /* Non Blocking error : transfer could go on.
1277 Error is notified to user through user error callback */
1278 HAL_SMARTCARD_ErrorCallback(hsc
);
1279 hsc
->ErrorCode
= HAL_SMARTCARD_ERROR_NONE
;
1283 } /* End if some error occurs */
1285 /* SMARTCARD in mode Transmitter -------------------------------------------*/
1286 if(((isrflags
& SMARTCARD_FLAG_TXE
) != RESET
) && ((cr1its
& USART_CR1_TXEIE
) != RESET
))
1288 SMARTCARD_Transmit_IT(hsc
);
1292 /* SMARTCARD in mode Transmitter (transmission end) ------------------------*/
1293 if(((isrflags
& SMARTCARD_FLAG_TC
) != RESET
) && ((cr1its
& USART_CR1_TCIE
) != RESET
))
1295 SMARTCARD_EndTransmit_IT(hsc
);
1301 * @brief Tx Transfer completed callbacks
1302 * @param hsc: pointer to a SMARTCARD_HandleTypeDef structure that contains
1303 * the configuration information for SMARTCARD module.
1306 __weak
void HAL_SMARTCARD_TxCpltCallback(SMARTCARD_HandleTypeDef
*hsc
)
1308 /* Prevent unused argument(s) compilation warning */
1310 /* NOTE : This function Should not be modified, when the callback is needed,
1311 the HAL_SMARTCARD_TxCpltCallback could be implemented in the user file
1316 * @brief Rx Transfer completed callbacks
1317 * @param hsc: pointer to a SMARTCARD_HandleTypeDef structure that contains
1318 * the configuration information for SMARTCARD module.
1321 __weak
void HAL_SMARTCARD_RxCpltCallback(SMARTCARD_HandleTypeDef
*hsc
)
1323 /* Prevent unused argument(s) compilation warning */
1325 /* NOTE : This function Should not be modified, when the callback is needed,
1326 the HAL_SMARTCARD_RxCpltCallback could be implemented in the user file
1331 * @brief SMARTCARD error callbacks
1332 * @param hsc: pointer to a SMARTCARD_HandleTypeDef structure that contains
1333 * the configuration information for SMARTCARD module.
1336 __weak
void HAL_SMARTCARD_ErrorCallback(SMARTCARD_HandleTypeDef
*hsc
)
1338 /* Prevent unused argument(s) compilation warning */
1340 /* NOTE : This function Should not be modified, when the callback is needed,
1341 the HAL_SMARTCARD_ErrorCallback could be implemented in the user file
1346 * @brief SMARTCARD Abort Complete callback.
1347 * @param hsc SMARTCARD handle.
1350 __weak
void HAL_SMARTCARD_AbortCpltCallback (SMARTCARD_HandleTypeDef
*hsc
)
1352 /* Prevent unused argument(s) compilation warning */
1355 /* NOTE : This function should not be modified, when the callback is needed,
1356 the HAL_SMARTCARD_AbortCpltCallback can be implemented in the user file.
1361 * @brief SMARTCARD Abort Transmit Complete callback.
1362 * @param hsc SMARTCARD handle.
1365 __weak
void HAL_SMARTCARD_AbortTransmitCpltCallback (SMARTCARD_HandleTypeDef
*hsc
)
1367 /* Prevent unused argument(s) compilation warning */
1370 /* NOTE : This function should not be modified, when the callback is needed,
1371 the HAL_SMARTCARD_AbortTransmitCpltCallback can be implemented in the user file.
1376 * @brief SMARTCARD Abort ReceiveComplete callback.
1377 * @param hsc SMARTCARD handle.
1380 __weak
void HAL_SMARTCARD_AbortReceiveCpltCallback (SMARTCARD_HandleTypeDef
*hsc
)
1382 /* Prevent unused argument(s) compilation warning */
1385 /* NOTE : This function should not be modified, when the callback is needed,
1386 the HAL_SMARTCARD_AbortReceiveCpltCallback can be implemented in the user file.
1394 /** @defgroup SMARTCARD_Exported_Functions_Group3 Peripheral State and Errors functions
1395 * @brief SMARTCARD State and Errors functions
1398 ===============================================================================
1399 ##### Peripheral State and Errors functions #####
1400 ===============================================================================
1402 This subsection provides a set of functions allowing to control the SmartCard.
1403 (+) HAL_SMARTCARD_GetState() API can be helpful to check in run-time the state of the SmartCard peripheral.
1404 (+) HAL_SMARTCARD_GetError() check in run-time errors that could be occurred during communication.
1410 * @brief return the SMARTCARD state
1411 * @param hsc: pointer to a SMARTCARD_HandleTypeDef structure that contains
1412 * the configuration information for SMARTCARD module.
1415 HAL_SMARTCARD_StateTypeDef
HAL_SMARTCARD_GetState(SMARTCARD_HandleTypeDef
*hsc
)
1417 uint32_t temp1
= 0x00U
, temp2
= 0x00U
;
1418 temp1
= hsc
->gState
;
1419 temp2
= hsc
->RxState
;
1421 return (HAL_SMARTCARD_StateTypeDef
)(temp1
| temp2
);
1425 * @brief Return the SMARTCARD error code
1426 * @param hsc : pointer to a SMARTCARD_HandleTypeDef structure that contains
1427 * the configuration information for the specified SMARTCARD.
1428 * @retval SMARTCARD Error Code
1430 uint32_t HAL_SMARTCARD_GetError(SMARTCARD_HandleTypeDef
*hsc
)
1432 return hsc
->ErrorCode
;
1440 * @brief DMA SMARTCARD transmit process complete callback
1441 * @param hdma: pointer to a DMA_HandleTypeDef structure that contains
1442 * the configuration information for the specified DMA module.
1445 static void SMARTCARD_DMATransmitCplt(DMA_HandleTypeDef
*hdma
)
1447 SMARTCARD_HandleTypeDef
* hsc
= ( SMARTCARD_HandleTypeDef
* )((DMA_HandleTypeDef
* )hdma
)->Parent
;
1449 hsc
->TxXferCount
= 0U;
1451 /* Disable the DMA transfer for transmit request by setting the DMAT bit
1452 in the USART CR3 register */
1453 CLEAR_BIT(hsc
->Instance
->CR3
, USART_CR3_DMAT
);
1455 /* Enable the SMARTCARD Transmit Complete Interrupt */
1456 SET_BIT(hsc
->Instance
->CR1
, USART_CR1_TCIE
);
1460 * @brief DMA SMARTCARD receive process complete callback
1461 * @param hdma: pointer to a DMA_HandleTypeDef structure that contains
1462 * the configuration information for the specified DMA module.
1465 static void SMARTCARD_DMAReceiveCplt(DMA_HandleTypeDef
*hdma
)
1467 SMARTCARD_HandleTypeDef
* hsc
= ( SMARTCARD_HandleTypeDef
* )((DMA_HandleTypeDef
* )hdma
)->Parent
;
1469 hsc
->RxXferCount
= 0U;
1471 /* Disable RXNE, PE and ERR (Frame error, noise error, overrun error) interrupts */
1472 CLEAR_BIT(hsc
->Instance
->CR1
, (USART_CR1_RXNEIE
| USART_CR1_PEIE
));
1473 CLEAR_BIT(hsc
->Instance
->CR3
, USART_CR3_EIE
);
1475 /* Disable the DMA transfer for the receiver request by setting the DMAR bit
1476 in the USART CR3 register */
1477 CLEAR_BIT(hsc
->Instance
->CR3
, USART_CR3_DMAR
);
1479 /* At end of Rx process, restore hsc->RxState to Ready */
1480 hsc
->RxState
= HAL_SMARTCARD_STATE_READY
;
1482 HAL_SMARTCARD_RxCpltCallback(hsc
);
1486 * @brief DMA SMARTCARD communication error callback
1487 * @param hdma: pointer to a DMA_HandleTypeDef structure that contains
1488 * the configuration information for the specified DMA module.
1491 static void SMARTCARD_DMAError(DMA_HandleTypeDef
*hdma
)
1493 uint32_t dmarequest
= 0x00U
;
1494 SMARTCARD_HandleTypeDef
* hsc
= ( SMARTCARD_HandleTypeDef
* )((DMA_HandleTypeDef
* )hdma
)->Parent
;
1495 hsc
->RxXferCount
= 0U;
1496 hsc
->TxXferCount
= 0U;
1497 hsc
->ErrorCode
= HAL_SMARTCARD_ERROR_DMA
;
1499 /* Stop SMARTCARD DMA Tx request if ongoing */
1500 dmarequest
= HAL_IS_BIT_SET(hsc
->Instance
->CR3
, USART_CR3_DMAT
);
1501 if((hsc
->gState
== HAL_SMARTCARD_STATE_BUSY_TX
) && dmarequest
)
1503 SMARTCARD_EndTxTransfer(hsc
);
1506 /* Stop SMARTCARD DMA Rx request if ongoing */
1507 dmarequest
= HAL_IS_BIT_SET(hsc
->Instance
->CR3
, USART_CR3_DMAR
);
1508 if((hsc
->RxState
== HAL_SMARTCARD_STATE_BUSY_RX
) && dmarequest
)
1510 SMARTCARD_EndRxTransfer(hsc
);
1513 HAL_SMARTCARD_ErrorCallback(hsc
);
1517 * @brief This function handles SMARTCARD Communication Timeout.
1518 * @param hsc: SMARTCARD handle
1519 * @param Flag: specifies the SMARTCARD flag to check.
1520 * @param Status: The new Flag status (SET or RESET).
1521 * @param Timeout: Timeout duration
1522 * @param Tickstart: tick start value
1523 * @retval HAL status
1525 static HAL_StatusTypeDef
SMARTCARD_WaitOnFlagUntilTimeout(SMARTCARD_HandleTypeDef
*hsc
, uint32_t Flag
, FlagStatus Status
, uint32_t Tickstart
, uint32_t Timeout
)
1527 /* Wait until flag is set */
1528 while((__HAL_SMARTCARD_GET_FLAG(hsc
, Flag
) ? SET
: RESET
) == Status
)
1530 /* Check for the Timeout */
1531 if(Timeout
!= HAL_MAX_DELAY
)
1533 if((Timeout
== 0U)||((HAL_GetTick() - Tickstart
) > Timeout
))
1535 /* Disable TXE and RXNE interrupts for the interrupt process */
1536 CLEAR_BIT(hsc
->Instance
->CR1
, USART_CR1_TXEIE
);
1537 CLEAR_BIT(hsc
->Instance
->CR1
, USART_CR1_RXNEIE
);
1539 hsc
->gState
= HAL_SMARTCARD_STATE_READY
;
1540 hsc
->RxState
= HAL_SMARTCARD_STATE_READY
;
1542 /* Process Unlocked */
1553 * @brief End ongoing Tx transfer on SMARTCARD peripheral (following error detection or Transmit completion).
1554 * @param hsc: SMARTCARD handle.
1557 static void SMARTCARD_EndTxTransfer(SMARTCARD_HandleTypeDef
*hsc
)
1559 /* At end of Tx process, restore hsc->gState to Ready */
1560 hsc
->gState
= HAL_SMARTCARD_STATE_READY
;
1562 /* Disable TXEIE and TCIE interrupts */
1563 CLEAR_BIT(hsc
->Instance
->CR1
, (USART_CR1_TXEIE
| USART_CR1_TCIE
));
1568 * @brief End ongoing Rx transfer on SMARTCARD peripheral (following error detection or Reception completion).
1569 * @param hsc: SMARTCARD handle.
1572 static void SMARTCARD_EndRxTransfer(SMARTCARD_HandleTypeDef
*hsc
)
1574 /* At end of Rx process, restore hsc->RxState to Ready */
1575 hsc
->RxState
= HAL_SMARTCARD_STATE_READY
;
1577 /* Disable RXNE, PE and ERR (Frame error, noise error, overrun error) interrupts */
1578 CLEAR_BIT(hsc
->Instance
->CR1
, (USART_CR1_RXNEIE
| USART_CR1_PEIE
));
1579 CLEAR_BIT(hsc
->Instance
->CR3
, USART_CR3_EIE
);
1585 * @brief DMA SMARTCARD communication abort callback, when initiated by HAL services on Error
1586 * (To be called at end of DMA Abort procedure following error occurrence).
1587 * @param hdma DMA handle.
1590 static void SMARTCARD_DMAAbortOnError(DMA_HandleTypeDef
*hdma
)
1592 SMARTCARD_HandleTypeDef
* hsc
= (SMARTCARD_HandleTypeDef
* )((DMA_HandleTypeDef
* )hdma
)->Parent
;
1593 hsc
->RxXferCount
= 0x00U
;
1594 hsc
->TxXferCount
= 0x00U
;
1596 HAL_SMARTCARD_ErrorCallback(hsc
);
1600 * @brief DMA SMARTCARD Tx communication abort callback, when initiated by user
1601 * (To be called at end of DMA Tx Abort procedure following user abort request).
1602 * @note When this callback is executed, User Abort complete call back is called only if no
1603 * Abort still ongoing for Rx DMA Handle.
1604 * @param hdma DMA handle.
1607 static void SMARTCARD_DMATxAbortCallback(DMA_HandleTypeDef
*hdma
)
1609 SMARTCARD_HandleTypeDef
* hsc
= ( SMARTCARD_HandleTypeDef
* )((DMA_HandleTypeDef
* )hdma
)->Parent
;
1611 hsc
->hdmatx
->XferAbortCallback
= NULL
;
1613 /* Check if an Abort process is still ongoing */
1614 if(hsc
->hdmarx
!= NULL
)
1616 if(hsc
->hdmarx
->XferAbortCallback
!= NULL
)
1622 /* No Abort process still ongoing : All DMA channels are aborted, call user Abort Complete callback */
1623 hsc
->TxXferCount
= 0x00U
;
1624 hsc
->RxXferCount
= 0x00U
;
1626 /* Reset ErrorCode */
1627 hsc
->ErrorCode
= HAL_SMARTCARD_ERROR_NONE
;
1629 /* Restore hsc->gState and hsc->RxState to Ready */
1630 hsc
->gState
= HAL_SMARTCARD_STATE_READY
;
1631 hsc
->RxState
= HAL_SMARTCARD_STATE_READY
;
1633 /* Call user Abort complete callback */
1634 HAL_SMARTCARD_AbortCpltCallback(hsc
);
1638 * @brief DMA SMARTCARD Rx communication abort callback, when initiated by user
1639 * (To be called at end of DMA Rx Abort procedure following user abort request).
1640 * @note When this callback is executed, User Abort complete call back is called only if no
1641 * Abort still ongoing for Tx DMA Handle.
1642 * @param hdma DMA handle.
1645 static void SMARTCARD_DMARxAbortCallback(DMA_HandleTypeDef
*hdma
)
1647 SMARTCARD_HandleTypeDef
* hsc
= ( SMARTCARD_HandleTypeDef
* )((DMA_HandleTypeDef
* )hdma
)->Parent
;
1649 hsc
->hdmarx
->XferAbortCallback
= NULL
;
1651 /* Check if an Abort process is still ongoing */
1652 if(hsc
->hdmatx
!= NULL
)
1654 if(hsc
->hdmatx
->XferAbortCallback
!= NULL
)
1660 /* No Abort process still ongoing : All DMA channels are aborted, call user Abort Complete callback */
1661 hsc
->TxXferCount
= 0x00U
;
1662 hsc
->RxXferCount
= 0x00U
;
1664 /* Reset ErrorCode */
1665 hsc
->ErrorCode
= HAL_SMARTCARD_ERROR_NONE
;
1667 /* Restore hsc->gState and hsc->RxState to Ready */
1668 hsc
->gState
= HAL_SMARTCARD_STATE_READY
;
1669 hsc
->RxState
= HAL_SMARTCARD_STATE_READY
;
1671 /* Call user Abort complete callback */
1672 HAL_SMARTCARD_AbortCpltCallback(hsc
);
1676 * @brief DMA SMARTCARD Tx communication abort callback, when initiated by user by a call to
1677 * HAL_SMARTCARD_AbortTransmit_IT API (Abort only Tx transfer)
1678 * (This callback is executed at end of DMA Tx Abort procedure following user abort request,
1679 * and leads to user Tx Abort Complete callback execution).
1680 * @param hdma DMA handle.
1683 static void SMARTCARD_DMATxOnlyAbortCallback(DMA_HandleTypeDef
*hdma
)
1685 SMARTCARD_HandleTypeDef
* hsc
= ( SMARTCARD_HandleTypeDef
* )((DMA_HandleTypeDef
* )hdma
)->Parent
;
1687 hsc
->TxXferCount
= 0x00U
;
1689 /* Restore hsc->gState to Ready */
1690 hsc
->gState
= HAL_SMARTCARD_STATE_READY
;
1692 /* Call user Abort complete callback */
1693 HAL_SMARTCARD_AbortTransmitCpltCallback(hsc
);
1697 * @brief DMA SMARTCARD Rx communication abort callback, when initiated by user by a call to
1698 * HAL_SMARTCARD_AbortReceive_IT API (Abort only Rx transfer)
1699 * (This callback is executed at end of DMA Rx Abort procedure following user abort request,
1700 * and leads to user Rx Abort Complete callback execution).
1701 * @param hdma DMA handle.
1704 static void SMARTCARD_DMARxOnlyAbortCallback(DMA_HandleTypeDef
*hdma
)
1706 SMARTCARD_HandleTypeDef
* hsc
= ( SMARTCARD_HandleTypeDef
* )((DMA_HandleTypeDef
* )hdma
)->Parent
;
1708 hsc
->RxXferCount
= 0x00U
;
1710 /* Restore hsc->RxState to Ready */
1711 hsc
->RxState
= HAL_SMARTCARD_STATE_READY
;
1713 /* Call user Abort complete callback */
1714 HAL_SMARTCARD_AbortReceiveCpltCallback(hsc
);
1718 * @brief Send an amount of data in non blocking mode
1719 * @param hsc: pointer to a SMARTCARD_HandleTypeDef structure that contains
1720 * the configuration information for SMARTCARD module.
1721 * @retval HAL status
1723 static HAL_StatusTypeDef
SMARTCARD_Transmit_IT(SMARTCARD_HandleTypeDef
*hsc
)
1727 /* Check that a Tx process is ongoing */
1728 if(hsc
->gState
== HAL_SMARTCARD_STATE_BUSY_TX
)
1730 tmp
= (uint16_t*) hsc
->pTxBuffPtr
;
1731 hsc
->Instance
->DR
= (uint16_t)(*tmp
& (uint16_t)0x01FF);
1732 hsc
->pTxBuffPtr
+= 1U;
1734 if(--hsc
->TxXferCount
== 0U)
1736 /* Disable the SMARTCARD Transmit data register empty Interrupt */
1737 CLEAR_BIT(hsc
->Instance
->CR1
, USART_CR1_TXEIE
);
1739 /* Enable the SMARTCARD Transmit Complete Interrupt */
1740 SET_BIT(hsc
->Instance
->CR1
, USART_CR1_TCIE
);
1752 * @brief Wraps up transmission in non blocking mode.
1753 * @param hsmartcard: pointer to a SMARTCARD_HandleTypeDef structure that contains
1754 * the configuration information for the specified SMARTCARD module.
1755 * @retval HAL status
1757 static HAL_StatusTypeDef
SMARTCARD_EndTransmit_IT(SMARTCARD_HandleTypeDef
*hsmartcard
)
1759 /* Disable the SMARTCARD Transmit Complete Interrupt */
1760 CLEAR_BIT(hsmartcard
->Instance
->CR1
, USART_CR1_TCIE
);
1762 /* Disable the SMARTCARD Error Interrupt: (Frame error, noise error, overrun error) */
1763 CLEAR_BIT(hsmartcard
->Instance
->CR3
, USART_CR3_EIE
);
1765 /* Tx process is ended, restore hsmartcard->gState to Ready */
1766 hsmartcard
->gState
= HAL_SMARTCARD_STATE_READY
;
1768 HAL_SMARTCARD_TxCpltCallback(hsmartcard
);
1774 * @brief Receive an amount of data in non blocking mode
1775 * @param hsc: pointer to a SMARTCARD_HandleTypeDef structure that contains
1776 * the configuration information for SMARTCARD module.
1777 * @retval HAL status
1779 static HAL_StatusTypeDef
SMARTCARD_Receive_IT(SMARTCARD_HandleTypeDef
*hsc
)
1783 /* Check that a Rx process is ongoing */
1784 if(hsc
->RxState
== HAL_SMARTCARD_STATE_BUSY_RX
)
1786 tmp
= (uint16_t*) hsc
->pRxBuffPtr
;
1787 *tmp
= (uint8_t)(hsc
->Instance
->DR
& (uint8_t)0x00FF);
1788 hsc
->pRxBuffPtr
+= 1U;
1790 if(--hsc
->RxXferCount
== 0U)
1792 CLEAR_BIT(hsc
->Instance
->CR1
, USART_CR1_RXNEIE
);
1794 /* Disable the SMARTCARD Parity Error Interrupt */
1795 CLEAR_BIT(hsc
->Instance
->CR1
, USART_CR1_PEIE
);
1797 /* Disable the SMARTCARD Error Interrupt: (Frame error, noise error, overrun error) */
1798 CLEAR_BIT(hsc
->Instance
->CR3
, USART_CR3_EIE
);
1800 /* Rx process is completed, restore hsc->RxState to Ready */
1801 hsc
->RxState
= HAL_SMARTCARD_STATE_READY
;
1803 HAL_SMARTCARD_RxCpltCallback(hsc
);
1816 * @brief Configure the SMARTCARD peripheral
1817 * @param hsc: pointer to a SMARTCARD_HandleTypeDef structure that contains
1818 * the configuration information for SMARTCARD module.
1821 static void SMARTCARD_SetConfig(SMARTCARD_HandleTypeDef
*hsc
)
1823 uint32_t tmpreg
= 0x00U
;
1825 /* Check the parameters */
1826 assert_param(IS_SMARTCARD_INSTANCE(hsc
->Instance
));
1827 assert_param(IS_SMARTCARD_POLARITY(hsc
->Init
.CLKPolarity
));
1828 assert_param(IS_SMARTCARD_PHASE(hsc
->Init
.CLKPhase
));
1829 assert_param(IS_SMARTCARD_LASTBIT(hsc
->Init
.CLKLastBit
));
1830 assert_param(IS_SMARTCARD_BAUDRATE(hsc
->Init
.BaudRate
));
1831 assert_param(IS_SMARTCARD_WORD_LENGTH(hsc
->Init
.WordLength
));
1832 assert_param(IS_SMARTCARD_STOPBITS(hsc
->Init
.StopBits
));
1833 assert_param(IS_SMARTCARD_PARITY(hsc
->Init
.Parity
));
1834 assert_param(IS_SMARTCARD_MODE(hsc
->Init
.Mode
));
1835 assert_param(IS_SMARTCARD_NACK_STATE(hsc
->Init
.NACKState
));
1838 /* The LBCL, CPOL and CPHA bits have to be selected when both the transmitter and the
1839 receiver are disabled (TE=RE=0) to ensure that the clock pulses function correctly. */
1840 CLEAR_BIT(hsc
->Instance
->CR1
, (USART_CR1_TE
| USART_CR1_RE
));
1842 /*---------------------------- USART CR2 Configuration ---------------------*/
1843 tmpreg
= hsc
->Instance
->CR2
;
1844 /* Clear CLKEN, CPOL, CPHA and LBCL bits */
1845 tmpreg
&= (uint32_t)~((uint32_t)(USART_CR2_CPHA
| USART_CR2_CPOL
| USART_CR2_CLKEN
| USART_CR2_LBCL
));
1846 /* Configure the SMARTCARD Clock, CPOL, CPHA and LastBit -----------------------*/
1847 /* Set CPOL bit according to hsc->Init.CLKPolarity value */
1848 /* Set CPHA bit according to hsc->Init.CLKPhase value */
1849 /* Set LBCL bit according to hsc->Init.CLKLastBit value */
1850 /* Set Stop Bits: Set STOP[13:12] bits according to hsc->Init.StopBits value */
1851 tmpreg
|= (uint32_t)(USART_CR2_CLKEN
| hsc
->Init
.CLKPolarity
|
1852 hsc
->Init
.CLKPhase
| hsc
->Init
.CLKLastBit
| hsc
->Init
.StopBits
);
1853 /* Write to USART CR2 */
1854 WRITE_REG(hsc
->Instance
->CR2
, (uint32_t)tmpreg
);
1856 tmpreg
= hsc
->Instance
->CR2
;
1858 /* Clear STOP[13:12] bits */
1859 tmpreg
&= (uint32_t)~((uint32_t)USART_CR2_STOP
);
1861 /* Set Stop Bits: Set STOP[13:12] bits according to hsc->Init.StopBits value */
1862 tmpreg
|= (uint32_t)(hsc
->Init
.StopBits
);
1864 /* Write to USART CR2 */
1865 WRITE_REG(hsc
->Instance
->CR2
, (uint32_t)tmpreg
);
1867 /*-------------------------- USART CR1 Configuration -----------------------*/
1868 tmpreg
= hsc
->Instance
->CR1
;
1870 /* Clear M, PCE, PS, TE and RE bits */
1871 tmpreg
&= (uint32_t)~((uint32_t)(USART_CR1_M
| USART_CR1_PCE
| USART_CR1_PS
| USART_CR1_TE
| \
1874 /* Configure the SMARTCARD Word Length, Parity and mode:
1875 Set the M bits according to hsc->Init.WordLength value
1876 Set PCE and PS bits according to hsc->Init.Parity value
1877 Set TE and RE bits according to hsc->Init.Mode value */
1878 tmpreg
|= (uint32_t)hsc
->Init
.WordLength
| hsc
->Init
.Parity
| hsc
->Init
.Mode
;
1880 /* Write to USART CR1 */
1881 WRITE_REG(hsc
->Instance
->CR1
, (uint32_t)tmpreg
);
1883 /*-------------------------- USART CR3 Configuration -----------------------*/
1884 /* Clear CTSE and RTSE bits */
1885 CLEAR_BIT(hsc
->Instance
->CR3
, (USART_CR3_RTSE
| USART_CR3_CTSE
));
1887 /*-------------------------- USART BRR Configuration -----------------------*/
1889 if(hsc
->Instance
== USART1
)
1891 hsc
->Instance
->BRR
= SMARTCARD_BRR(HAL_RCC_GetPCLK2Freq(), hsc
->Init
.BaudRate
);
1895 hsc
->Instance
->BRR
= SMARTCARD_BRR(HAL_RCC_GetPCLK1Freq(), hsc
->Init
.BaudRate
);
1903 #endif /* HAL_SMARTCARD_MODULE_ENABLED */
1912 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/