2 ******************************************************************************
3 * @file stm32l1xx_hal_smartcard.c
4 * @author MCD Application Team
6 * @date 5-September-2014
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 State and Errors functions
13 * + Peripheral Control functions
16 ==============================================================================
17 ##### How to use this driver #####
18 ==============================================================================
20 The SMARTCARD HAL driver can be used as follows:
22 (#) Declare a SMARTCARD_HandleTypeDef handle structure.
23 (#) Initialize the SMARTCARD low level resources by implementing the HAL_SMARTCARD_MspInit() API:
24 (##) Enable the USARTx interface clock.
25 (##) SMARTCARD pins configuration:
26 (+++) Enable the clock for the SMARTCARD GPIOs.
27 (+++) Configure these SMARTCARD pins as alternate function pull-up.
28 (##) NVIC configuration if you need to use interrupt process (HAL_SMARTCARD_Transmit_IT()
29 and HAL_SMARTCARD_Receive_IT() APIs):
30 (+++) Configure the USARTx interrupt priority.
31 (+++) Enable the NVIC USART IRQ handle.
32 (##) DMA Configuration if you need to use DMA process (HAL_SMARTCARD_Transmit_DMA()
33 and HAL_SMARTCARD_Receive_DMA() APIs):
34 (+++) Declare a DMA handle structure for the Tx/Rx channel.
35 (+++) Enable the DMAx interface clock.
36 (+++) Configure the declared DMA handle structure with the required Tx/Rx parameters.
37 (+++) Configure the DMA Tx/Rx channel.
38 (+++) Associate the initilalized DMA handle to the SMARTCARD DMA Tx/Rx handle.
39 (+++) Configure the priority and enable the NVIC for the transfer complete interrupt on the DMA Tx/Rx channel.
41 (#) Program the Baud Rate, Word Length , Stop Bit, Parity, Hardware
42 flow control and Mode(Receiver/Transmitter) in the SMARTCARD Init structure.
44 (#) Initialize the SMARTCARD registers by calling the HAL_SMARTCARD_Init() API:
45 (++) This API configures also the low level Hardware GPIO, CLOCK, CORTEX...etc)
46 by calling the customed HAL_SMARTCARD_MspInit(&hsc) API.
48 -@@- The specific SMARTCARD interrupts (Transmission complete interrupt,
49 RXNE interrupt and Error Interrupts) will be managed using the macros
50 __HAL_SMARTCARD_ENABLE_IT() and __HAL_SMARTCARD_DISABLE_IT() inside the transmit and receive process.
52 (#) Three operation modes are available within this driver :
54 *** Polling mode IO operation ***
55 =================================
57 (+) Send an amount of data in blocking mode using HAL_SMARTCARD_Transmit()
58 (+) Receive an amount of data in blocking mode using HAL_SMARTCARD_Receive()
60 *** Interrupt mode IO operation ***
61 ===================================
63 (+) Send an amount of data in non blocking mode using HAL_SMARTCARD_Transmit_IT()
64 (+) At transmission end of transfer HAL_SMARTCARD_TxCpltCallback is executed and user can
65 add his own code by customization of function pointer HAL_SMARTCARD_TxCpltCallback
66 (+) Receive an amount of data in non blocking mode using HAL_SMARTCARD_Receive_IT()
67 (+) At reception end of transfer HAL_SMARTCARD_RxCpltCallback is executed and user can
68 add his own code by customization of function pointer HAL_SMARTCARD_RxCpltCallback
69 (+) In case of transfer Error, HAL_SMARTCARD_ErrorCallback() function is executed and user can
70 add his own code by customization of function pointer HAL_SMARTCARD_ErrorCallback
72 *** DMA mode IO operation ***
73 ==============================
75 (+) Send an amount of data in non blocking mode (DMA) using HAL_SMARTCARD_Transmit_DMA()
76 (+) At transmission end of transfer HAL_SMARTCARD_TxCpltCallback is executed and user can
77 add his own code by customization of function pointer HAL_SMARTCARD_TxCpltCallback
78 (+) Receive an amount of data in non blocking mode (DMA) using HAL_SMARTCARD_Receive_DMA()
79 (+) At reception end of transfer HAL_SMARTCARD_RxCpltCallback is executed and user can
80 add his own code by customization of function pointer HAL_SMARTCARD_RxCpltCallback
81 (+) In case of transfer Error, HAL_SMARTCARD_ErrorCallback() function is executed and user can
82 add his own code by customization of function pointer HAL_SMARTCARD_ErrorCallback
84 *** SMARTCARD HAL driver macros list ***
85 ========================================
87 Below the list of most used macros in SMARTCARD HAL driver.
89 (+) __HAL_SMARTCARD_ENABLE: Enable the SMARTCARD peripheral
90 (+) __HAL_SMARTCARD_DISABLE: Disable the SMARTCARD peripheral
91 (+) __HAL_SMARTCARD_GET_FLAG : Check whether the specified SMARTCARD flag is set or not
92 (+) __HAL_SMARTCARD_CLEAR_FLAG : Clear the specified SMARTCARD pending flag
93 (+) __HAL_SMARTCARD_ENABLE_IT: Enable the specified SMARTCARD interrupt
94 (+) __HAL_SMARTCARD_DISABLE_IT: Disable the specified SMARTCARD interrupt
97 (@) You can refer to the SMARTCARD HAL driver header file for more useful macros
100 ******************************************************************************
103 * <h2><center>© COPYRIGHT(c) 2014 STMicroelectronics</center></h2>
105 * Redistribution and use in source and binary forms, with or without modification,
106 * are permitted provided that the following conditions are met:
107 * 1. Redistributions of source code must retain the above copyright notice,
108 * this list of conditions and the following disclaimer.
109 * 2. Redistributions in binary form must reproduce the above copyright notice,
110 * this list of conditions and the following disclaimer in the documentation
111 * and/or other materials provided with the distribution.
112 * 3. Neither the name of STMicroelectronics nor the names of its contributors
113 * may be used to endorse or promote products derived from this software
114 * without specific prior written permission.
116 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
117 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
118 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
119 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
120 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
121 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
122 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
123 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
124 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
125 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
127 ******************************************************************************
130 /* Includes ------------------------------------------------------------------*/
131 #include "stm32l1xx_hal.h"
133 /** @addtogroup STM32L1xx_HAL_Driver
137 /** @defgroup SMARTCARD SMARTCARD
138 * @brief HAL SMARTCARD module driver
142 #ifdef HAL_SMARTCARD_MODULE_ENABLED
144 /* Private typedef -----------------------------------------------------------*/
145 /* Private define ------------------------------------------------------------*/
146 /** @defgroup SMARTCARD_Private_Constants SMARTCARD Private Constants
149 #define SMARTCARD_TIMEOUT_VALUE 22000
154 /* Private macro -------------------------------------------------------------*/
155 /* Private variables ---------------------------------------------------------*/
156 /* Private function prototypes -----------------------------------------------*/
157 /** @addtogroup SMARTCARD_Private_Functions SMARTCARD Private Functions
160 static HAL_StatusTypeDef
SMARTCARD_Transmit_IT(SMARTCARD_HandleTypeDef
*hsc
);
161 static HAL_StatusTypeDef
SMARTCARD_EndTransmit_IT(SMARTCARD_HandleTypeDef
*hsmartcard
);
162 static HAL_StatusTypeDef
SMARTCARD_Receive_IT(SMARTCARD_HandleTypeDef
*hsc
);
163 static void SMARTCARD_SetConfig (SMARTCARD_HandleTypeDef
*hsc
);
164 static void SMARTCARD_DMATransmitCplt(DMA_HandleTypeDef
*hdma
);
165 static void SMARTCARD_DMAReceiveCplt(DMA_HandleTypeDef
*hdma
);
166 static void SMARTCARD_DMAError(DMA_HandleTypeDef
*hdma
);
167 static HAL_StatusTypeDef
SMARTCARD_WaitOnFlagUntilTimeout(SMARTCARD_HandleTypeDef
*hsc
, uint32_t Flag
, FlagStatus Status
, uint32_t Timeout
);
172 /* Exported functions ---------------------------------------------------------*/
174 /** @defgroup SMARTCARD_Exported_Functions SMARTCARD Exported Functions
178 /** @defgroup SMARTCARD_Exported_Functions_Group1 SmartCard Initialization and de-initialization functions
179 * @brief Initialization and Configuration functions
183 ==============================================================================
184 ##### Initialization and Configuration functions #####
185 ==============================================================================
187 This subsection provides a set of functions allowing to initialize the USART
190 The Smartcard interface is designed to support asynchronous protocol Smartcards as
191 defined in the ISO 7816-3 standard.
193 The USART can provide a clock to the smartcard through the SCLK output.
194 In smartcard mode, SCLK is not associated to the communication but is simply derived
195 from the internal peripheral input clock through a 5-bit prescaler.
197 (+) For the Smartcard mode only these parameters can be configured:
199 (++) Word Length => Should be 9 bits (8 bits + parity)
201 (++) Parity: => Should be enabled
202 +-------------------------------------------------------------+
203 | M bit | PCE bit | SMARTCARD frame |
204 |---------------------|---------------------------------------|
205 | 1 | 1 | | SB | 8 bit data | PB | STB | |
206 +-------------------------------------------------------------+
210 (++) Receiver/transmitter modes
213 (++) NACKState: The Smartcard NACK state
215 (+) Recommended SmartCard interface configuration to get the Answer to Reset from the Card:
216 (++) Word Length = 9 Bits
219 (++) BaudRate = 12096 baud
220 (++) Tx and Rx enabled
222 Please refer to the ISO 7816-3 specification for more details.
224 -@- It is also possible to choose 0.5 stop bit for receiving but it is recommended
225 to use 1.5 stop bits for both transmitting and receiving to avoid switching
226 between the two configurations.
228 The HAL_SMARTCARD_Init() function follows the USART SmartCard configuration
229 procedure (details for the procedure are available in reference manual (RM0038)).
236 * @brief Initializes the SmartCard mode according to the specified
237 * parameters in the SMARTCARD_HandleTypeDef and create the associated handle.
238 * @param hsc: Pointer to a SMARTCARD_HandleTypeDef structure that contains
239 * the configuration information for the specified SMARTCARD module.
242 HAL_StatusTypeDef
HAL_SMARTCARD_Init(SMARTCARD_HandleTypeDef
*hsc
)
244 /* Check the SMARTCARD handle allocation */
250 /* Check the parameters */
251 assert_param(IS_SMARTCARD_INSTANCE(hsc
->Instance
));
252 assert_param(IS_SMARTCARD_NACK_STATE(hsc
->Init
.NACKState
));
254 if(hsc
->State
== HAL_SMARTCARD_STATE_RESET
)
256 /* Init the low level hardware */
257 HAL_SMARTCARD_MspInit(hsc
);
260 hsc
->State
= HAL_SMARTCARD_STATE_BUSY
;
262 /* Set the Prescaler */
263 MODIFY_REG(hsc
->Instance
->GTPR
, USART_GTPR_PSC
, hsc
->Init
.Prescaler
);
265 /* Set the Guard Time */
266 MODIFY_REG(hsc
->Instance
->GTPR
, USART_GTPR_GT
, ((hsc
->Init
.GuardTime
)<<8));
268 /* Set the Smartcard Communication parameters */
269 SMARTCARD_SetConfig(hsc
);
271 /* In SmartCard mode, the following bits must be kept cleared:
272 - LINEN bit in the USART_CR2 register
273 - HDSEL and IREN bits in the USART_CR3 register.*/
274 CLEAR_BIT(hsc
->Instance
->CR2
, USART_CR2_LINEN
);
275 CLEAR_BIT(hsc
->Instance
->CR3
, (USART_CR3_IREN
| USART_CR3_HDSEL
));
277 /* Enable the SMARTCARD Parity Error Interrupt */
278 __HAL_SMARTCARD_ENABLE_IT(hsc
, SMARTCARD_IT_PE
);
280 /* Enable the SMARTCARD Framing Error Interrupt */
281 __HAL_SMARTCARD_ENABLE_IT(hsc
, SMARTCARD_IT_ERR
);
283 /* Enable the Peripharal */
284 __HAL_SMARTCARD_ENABLE(hsc
);
286 /* Configure the Smartcard NACK state */
287 MODIFY_REG(hsc
->Instance
->CR3
, USART_CR3_NACK
, hsc
->Init
.NACKState
);
289 /* Enable the SC mode by setting the SCEN bit in the CR3 register */
290 SET_BIT(hsc
->Instance
->CR3
, USART_CR3_SCEN
);
292 /* Initialize the SMARTCARD state*/
293 hsc
->ErrorCode
= HAL_SMARTCARD_ERROR_NONE
;
294 hsc
->State
= HAL_SMARTCARD_STATE_READY
;
300 * @brief DeInitializes the SMARTCARD peripheral
301 * @param hsc: Pointer to a SMARTCARD_HandleTypeDef structure that contains
302 * the configuration information for the specified SMARTCARD module.
305 HAL_StatusTypeDef
HAL_SMARTCARD_DeInit(SMARTCARD_HandleTypeDef
*hsc
)
307 /* Check the SMARTCARD handle allocation */
313 /* Check the parameters */
314 assert_param(IS_SMARTCARD_INSTANCE(hsc
->Instance
));
316 hsc
->State
= HAL_SMARTCARD_STATE_BUSY
;
318 /* Disable the Peripheral */
319 __HAL_SMARTCARD_DISABLE(hsc
);
321 /* DeInit the low level hardware */
322 HAL_SMARTCARD_MspDeInit(hsc
);
324 hsc
->ErrorCode
= HAL_SMARTCARD_ERROR_NONE
;
325 hsc
->State
= HAL_SMARTCARD_STATE_RESET
;
334 * @brief SMARTCARD MSP Init.
335 * @param hsc: Pointer to a SMARTCARD_HandleTypeDef structure that contains
336 * the configuration information for the specified SMARTCARD module.
339 __weak
void HAL_SMARTCARD_MspInit(SMARTCARD_HandleTypeDef
*hsc
)
341 /* NOTE: This function should not be modified, when the callback is needed,
342 the HAL_SMARTCARD_MspInit can be implemented in the user file
347 * @brief SMARTCARD MSP DeInit.
348 * @param hsc: Pointer to a SMARTCARD_HandleTypeDef structure that contains
349 * the configuration information for the specified SMARTCARD module.
352 __weak
void HAL_SMARTCARD_MspDeInit(SMARTCARD_HandleTypeDef
*hsc
)
354 /* NOTE: This function should not be modified, when the callback is needed,
355 the HAL_SMARTCARD_MspDeInit can be implemented in the user file
363 /** @defgroup SMARTCARD_Exported_Functions_Group2 IO operation functions
364 * @brief SMARTCARD Transmit and Receive functions
367 ==============================================================================
368 ##### IO operation functions #####
369 ==============================================================================
371 This subsection provides a set of functions allowing to manage the SMARTCARD data transfers.
374 Smartcard is a single wire half duplex communication protocol.
375 The Smartcard interface is designed to support asynchronous protocol Smartcards as
376 defined in the ISO 7816-3 standard. The USART should be configured as:
377 - 8 bits plus parity: where M=1 and PCE=1 in the USART_CR1 register
378 - 1.5 stop bits when transmitting and receiving: where STOP=11 in the USART_CR2 register.
380 (#) There are two modes of transfer:
381 (++) Blocking mode: The communication is performed in polling mode.
382 The HAL status of all data processing is returned by the same function
383 after finishing transfer.
384 (++) No-Blocking mode: The communication is performed using Interrupts
385 or DMA, These API's return the HAL status.
386 The end of the data processing will be indicated through the
387 dedicated SMARTCARD IRQ when using Interrupt mode or the DMA IRQ when
389 The HAL_SMARTCARD_TxCpltCallback(), HAL_SMARTCARD_RxCpltCallback() user callbacks
390 will be executed respectivelly at the end of the Transmit or Receive process
391 The HAL_SMARTCARD_ErrorCallback() user callback will be executed when a communication
394 (#) Blocking mode APIs are :
395 (++) HAL_SMARTCARD_Transmit()
396 (++) HAL_SMARTCARD_Receive()
398 (#) Non Blocking mode APIs with Interrupt are :
399 (++) HAL_SMARTCARD_Transmit_IT()
400 (++) HAL_SMARTCARD_Receive_IT()
401 (++) HAL_SMARTCARD_IRQHandler()
403 (#) Non Blocking mode functions with DMA are :
404 (++) HAL_SMARTCARD_Transmit_DMA()
405 (++) HAL_SMARTCARD_Receive_DMA()
407 (#) A set of Transfer Complete Callbacks are provided in non Blocking mode:
408 (++) HAL_SMARTCARD_TxCpltCallback()
409 (++) HAL_SMARTCARD_RxCpltCallback()
410 (++) HAL_SMARTCARD_ErrorCallback()
417 * @brief Sends an amount of data in blocking mode.
418 * @param hsc: Pointer to a SMARTCARD_HandleTypeDef structure that contains
419 * the configuration information for the specified SMARTCARD module.
420 * @param pData: Pointer to data buffer
421 * @param Size: Amount of data to be sent
422 * @param Timeout: Specify timeout value
425 HAL_StatusTypeDef
HAL_SMARTCARD_Transmit(SMARTCARD_HandleTypeDef
*hsc
, uint8_t *pData
, uint16_t Size
, uint32_t Timeout
)
431 if((tmp1
== HAL_SMARTCARD_STATE_READY
) || (tmp1
== HAL_SMARTCARD_STATE_BUSY_RX
))
433 if((pData
== HAL_NULL
) || (Size
== 0))
441 hsc
->ErrorCode
= HAL_SMARTCARD_ERROR_NONE
;
442 /* Check if a non-blocking receive process is ongoing or not */
443 if(hsc
->State
== HAL_SMARTCARD_STATE_BUSY_RX
)
445 hsc
->State
= HAL_SMARTCARD_STATE_BUSY_TX_RX
;
449 hsc
->State
= HAL_SMARTCARD_STATE_BUSY_TX
;
452 hsc
->TxXferSize
= Size
;
453 hsc
->TxXferCount
= Size
;
454 while(hsc
->TxXferCount
> 0)
456 if(hsc
->Init
.WordLength
== SMARTCARD_WORDLENGTH_9B
)
458 if(SMARTCARD_WaitOnFlagUntilTimeout(hsc
, SMARTCARD_FLAG_TXE
, RESET
, Timeout
) != HAL_OK
)
462 tmp
= (uint16_t*) pData
;
463 WRITE_REG(hsc
->Instance
->DR
, (*tmp
& (uint16_t)0x01FF));
464 if(hsc
->Init
.Parity
== SMARTCARD_PARITY_NONE
)
475 if(SMARTCARD_WaitOnFlagUntilTimeout(hsc
, SMARTCARD_FLAG_TXE
, RESET
, Timeout
) != HAL_OK
)
479 WRITE_REG(hsc
->Instance
->DR
, (*pData
++ & (uint8_t)0xFF));
484 if(SMARTCARD_WaitOnFlagUntilTimeout(hsc
, SMARTCARD_FLAG_TC
, RESET
, Timeout
) != HAL_OK
)
489 /* Check if a non-blocking receive process is ongoing or not */
490 if(hsc
->State
== HAL_SMARTCARD_STATE_BUSY_TX_RX
)
492 hsc
->State
= HAL_SMARTCARD_STATE_BUSY_RX
;
496 hsc
->State
= HAL_SMARTCARD_STATE_READY
;
498 /* Process Unlocked */
510 * @brief Receive an amount of data in blocking mode.
511 * @param hsc: Pointer to a SMARTCARD_HandleTypeDef structure that contains
512 * the configuration information for the specified SMARTCARD module.
513 * @param pData: Pointer to data buffer
514 * @param Size: Amount of data to be received
515 * @param Timeout: Specify timeout value
518 HAL_StatusTypeDef
HAL_SMARTCARD_Receive(SMARTCARD_HandleTypeDef
*hsc
, uint8_t *pData
, uint16_t Size
, uint32_t Timeout
)
524 if((tmp1
== HAL_SMARTCARD_STATE_READY
) || (tmp1
== HAL_SMARTCARD_STATE_BUSY_TX
))
526 if((pData
== HAL_NULL
) || (Size
== 0))
534 hsc
->ErrorCode
= HAL_SMARTCARD_ERROR_NONE
;
536 /* Check if a non-blocking transmit process is ongoing or not */
537 if(hsc
->State
== HAL_SMARTCARD_STATE_BUSY_TX
)
539 hsc
->State
= HAL_SMARTCARD_STATE_BUSY_TX_RX
;
543 hsc
->State
= HAL_SMARTCARD_STATE_BUSY_RX
;
546 hsc
->RxXferSize
= Size
;
547 hsc
->RxXferCount
= Size
;
548 /* Check the remain data to be received */
549 while(hsc
->RxXferCount
> 0)
551 if(hsc
->Init
.WordLength
== SMARTCARD_WORDLENGTH_9B
)
553 if(SMARTCARD_WaitOnFlagUntilTimeout(hsc
, SMARTCARD_FLAG_RXNE
, RESET
, Timeout
) != HAL_OK
)
557 tmp
= (uint16_t*) pData
;
558 if(hsc
->Init
.Parity
== SMARTCARD_PARITY_NONE
)
560 *tmp
= (uint16_t)(hsc
->Instance
->DR
& (uint16_t)0x01FF);
565 *tmp
= (uint16_t)(hsc
->Instance
->DR
& (uint16_t)0x00FF);
571 if(SMARTCARD_WaitOnFlagUntilTimeout(hsc
, SMARTCARD_FLAG_RXNE
, RESET
, Timeout
) != HAL_OK
)
575 if(hsc
->Init
.Parity
== SMARTCARD_PARITY_NONE
)
577 *pData
++ = (uint8_t)(hsc
->Instance
->DR
& (uint8_t)0x00FF);
581 *pData
++ = (uint8_t)(hsc
->Instance
->DR
& (uint8_t)0x007F);
587 /* Check if a non-blocking transmit process is ongoing or not */
588 if(hsc
->State
== HAL_SMARTCARD_STATE_BUSY_TX_RX
)
590 hsc
->State
= HAL_SMARTCARD_STATE_BUSY_TX
;
594 hsc
->State
= HAL_SMARTCARD_STATE_READY
;
597 /* Process Unlocked */
609 * @brief Sends an amount of data in non-blocking mode.
610 * @param hsc: Pointer to a SMARTCARD_HandleTypeDef structure that contains
611 * the configuration information for the specified SMARTCARD module.
612 * @param pData: Pointer to data buffer
613 * @param Size: Amount of data to be sent
616 HAL_StatusTypeDef
HAL_SMARTCARD_Transmit_IT(SMARTCARD_HandleTypeDef
*hsc
, uint8_t *pData
, uint16_t Size
)
621 if((tmp1
== HAL_SMARTCARD_STATE_READY
) || (tmp1
== HAL_SMARTCARD_STATE_BUSY_RX
))
623 if((pData
== HAL_NULL
) || (Size
== 0))
631 hsc
->pTxBuffPtr
= pData
;
632 hsc
->TxXferSize
= Size
;
633 hsc
->TxXferCount
= Size
;
635 hsc
->ErrorCode
= HAL_SMARTCARD_ERROR_NONE
;
636 /* Check if a non-blocking receive process is ongoing or not */
637 if(hsc
->State
== HAL_SMARTCARD_STATE_BUSY_RX
)
639 hsc
->State
= HAL_SMARTCARD_STATE_BUSY_TX_RX
;
643 hsc
->State
= HAL_SMARTCARD_STATE_BUSY_TX
;
646 /* Enable the SMARTCARD Parity Error Interrupt */
647 __HAL_SMARTCARD_ENABLE_IT(hsc
, SMARTCARD_IT_PE
);
649 /* Enable the SMARTCARD Error Interrupt: (Frame error, noise error, overrun error) */
650 __HAL_SMARTCARD_ENABLE_IT(hsc
, SMARTCARD_IT_ERR
);
652 /* Process Unlocked */
655 /* Enable the SMARTCARD Transmit data register empty Interrupt */
656 __HAL_SMARTCARD_ENABLE_IT(hsc
, SMARTCARD_IT_TXE
);
667 * @brief Receives an amount of data in non-blocking mode.
668 * @param hsc: Pointer to a SMARTCARD_HandleTypeDef structure that contains
669 * the configuration information for the specified SMARTCARD module.
670 * @param pData: Pointer to data buffer
671 * @param Size: Amount of data to be received
674 HAL_StatusTypeDef
HAL_SMARTCARD_Receive_IT(SMARTCARD_HandleTypeDef
*hsc
, uint8_t *pData
, uint16_t Size
)
679 if((tmp1
== HAL_SMARTCARD_STATE_READY
) || (tmp1
== HAL_SMARTCARD_STATE_BUSY_TX
))
681 if((pData
== HAL_NULL
) || (Size
== 0))
689 hsc
->pRxBuffPtr
= pData
;
690 hsc
->RxXferSize
= Size
;
691 hsc
->RxXferCount
= Size
;
693 hsc
->ErrorCode
= HAL_SMARTCARD_ERROR_NONE
;
694 /* Check if a non-blocking transmit process is ongoing or not */
695 if(hsc
->State
== HAL_SMARTCARD_STATE_BUSY_TX
)
697 hsc
->State
= HAL_SMARTCARD_STATE_BUSY_TX_RX
;
701 hsc
->State
= HAL_SMARTCARD_STATE_BUSY_RX
;
704 /* Enable the SMARTCARD Data Register not empty Interrupt */
705 __HAL_SMARTCARD_ENABLE_IT(hsc
, SMARTCARD_IT_RXNE
);
707 /* Enable the SMARTCARD Parity Error Interrupt */
708 __HAL_SMARTCARD_ENABLE_IT(hsc
, SMARTCARD_IT_PE
);
710 /* Enable the SMARTCARD Error Interrupt: (Frame error, noise error, overrun error) */
711 __HAL_SMARTCARD_ENABLE_IT(hsc
, SMARTCARD_IT_ERR
);
713 /* Process Unlocked */
725 * @brief Sends an amount of data in non-blocking mode.
726 * @param hsc: Pointer to a SMARTCARD_HandleTypeDef structure that contains
727 * the configuration information for the specified SMARTCARD module.
728 * @param pData: Pointer to data buffer
729 * @param Size: Amount of data to be sent
732 HAL_StatusTypeDef
HAL_SMARTCARD_Transmit_DMA(SMARTCARD_HandleTypeDef
*hsc
, uint8_t *pData
, uint16_t Size
)
738 if((tmp1
== HAL_SMARTCARD_STATE_READY
) || (tmp1
== HAL_SMARTCARD_STATE_BUSY_RX
))
740 if((pData
== HAL_NULL
) || (Size
== 0))
748 hsc
->pTxBuffPtr
= pData
;
749 hsc
->TxXferSize
= Size
;
750 hsc
->TxXferCount
= Size
;
752 hsc
->ErrorCode
= HAL_SMARTCARD_ERROR_NONE
;
753 /* Check if a non-blocking receive process is ongoing or not */
754 if(hsc
->State
== HAL_SMARTCARD_STATE_BUSY_RX
)
756 hsc
->State
= HAL_SMARTCARD_STATE_BUSY_TX_RX
;
760 hsc
->State
= HAL_SMARTCARD_STATE_BUSY_TX
;
763 /* Set the SMARTCARD DMA transfer complete callback */
764 hsc
->hdmatx
->XferCpltCallback
= SMARTCARD_DMATransmitCplt
;
766 /* Set the DMA error callback */
767 hsc
->hdmatx
->XferErrorCallback
= SMARTCARD_DMAError
;
769 /* Enable the SMARTCARD transmit DMA channel */
770 tmp
= (uint32_t*)&pData
;
771 HAL_DMA_Start_IT(hsc
->hdmatx
, *(uint32_t*)tmp
, (uint32_t)&hsc
->Instance
->DR
, Size
);
773 /* Enable the DMA transfer for transmit request by setting the DMAT bit
774 in the SMARTCARD CR3 register */
775 SET_BIT(hsc
->Instance
->CR3
,USART_CR3_DMAT
);
777 /* Process Unlocked */
789 * @brief Receive an amount of data in non-blocking mode.
790 * @param hsc: Pointer to a SMARTCARD_HandleTypeDef structure that contains
791 * the configuration information for the specified SMARTCARD module.
792 * @param pData: Pointer to data buffer
793 * @param Size: Amount of data to be received
794 * @note When the SMARTCARD parity is enabled (PCE = 1) the data received contain the parity bit.
797 HAL_StatusTypeDef
HAL_SMARTCARD_Receive_DMA(SMARTCARD_HandleTypeDef
*hsc
, uint8_t *pData
, uint16_t Size
)
803 if((tmp1
== HAL_SMARTCARD_STATE_READY
) || (tmp1
== HAL_SMARTCARD_STATE_BUSY_TX
))
805 if((pData
== HAL_NULL
) || (Size
== 0))
813 hsc
->pRxBuffPtr
= pData
;
814 hsc
->RxXferSize
= Size
;
816 hsc
->ErrorCode
= HAL_SMARTCARD_ERROR_NONE
;
817 /* Check if a non-blocking transmit process is ongoing or not */
818 if(hsc
->State
== HAL_SMARTCARD_STATE_BUSY_TX
)
820 hsc
->State
= HAL_SMARTCARD_STATE_BUSY_TX_RX
;
824 hsc
->State
= HAL_SMARTCARD_STATE_BUSY_RX
;
827 /* Set the SMARTCARD DMA transfer complete callback */
828 hsc
->hdmarx
->XferCpltCallback
= SMARTCARD_DMAReceiveCplt
;
830 /* Set the DMA error callback */
831 hsc
->hdmarx
->XferErrorCallback
= SMARTCARD_DMAError
;
833 /* Enable the DMA channel */
834 tmp
= (uint32_t*)&pData
;
835 HAL_DMA_Start_IT(hsc
->hdmarx
, (uint32_t)&hsc
->Instance
->DR
, *(uint32_t*)tmp
, Size
);
837 /* Enable the DMA transfer for the receiver request by setting the DMAR bit
838 in the SMARTCARD CR3 register */
839 SET_BIT(hsc
->Instance
->CR3
,USART_CR3_DMAR
);
841 /* Process Unlocked */
853 * @brief This function handles SMARTCARD interrupt request.
854 * @param hsc: Pointer to a SMARTCARD_HandleTypeDef structure that contains
855 * the configuration information for the specified SMARTCARD module.
858 void HAL_SMARTCARD_IRQHandler(SMARTCARD_HandleTypeDef
*hsc
)
860 uint32_t tmp1
= 0, tmp2
= 0;
862 tmp1
= __HAL_SMARTCARD_GET_FLAG(hsc
, SMARTCARD_FLAG_PE
);
863 tmp2
= __HAL_SMARTCARD_GET_IT_SOURCE(hsc
, SMARTCARD_IT_PE
);
864 /* SMARTCARD parity error interrupt occurred -----------------------------------*/
865 if((tmp1
!= RESET
) && (tmp2
!= RESET
))
867 __HAL_SMARTCARD_CLEAR_PEFLAG(hsc
);
868 hsc
->ErrorCode
|= HAL_SMARTCARD_ERROR_PE
;
871 tmp1
= __HAL_SMARTCARD_GET_FLAG(hsc
, SMARTCARD_FLAG_FE
);
872 tmp2
= __HAL_SMARTCARD_GET_IT_SOURCE(hsc
, SMARTCARD_IT_ERR
);
873 /* SMARTCARD frame error interrupt occurred ------------------------------------*/
874 if((tmp1
!= RESET
) && (tmp2
!= RESET
))
876 __HAL_SMARTCARD_CLEAR_FEFLAG(hsc
);
877 hsc
->ErrorCode
|= HAL_SMARTCARD_ERROR_FE
;
880 tmp1
= __HAL_SMARTCARD_GET_FLAG(hsc
, SMARTCARD_FLAG_NE
);
881 tmp2
= __HAL_SMARTCARD_GET_IT_SOURCE(hsc
, SMARTCARD_IT_ERR
);
882 /* SMARTCARD noise error interrupt occurred ------------------------------------*/
883 if((tmp1
!= RESET
) && (tmp2
!= RESET
))
885 __HAL_SMARTCARD_CLEAR_NEFLAG(hsc
);
886 hsc
->ErrorCode
|= HAL_SMARTCARD_ERROR_NE
;
889 tmp1
= __HAL_SMARTCARD_GET_FLAG(hsc
, SMARTCARD_FLAG_ORE
);
890 tmp2
= __HAL_SMARTCARD_GET_IT_SOURCE(hsc
, SMARTCARD_IT_ERR
);
891 /* SMARTCARD Over-Run interrupt occurred ---------------------------------------*/
892 if((tmp1
!= RESET
) && (tmp2
!= RESET
))
894 __HAL_SMARTCARD_CLEAR_OREFLAG(hsc
);
895 hsc
->ErrorCode
|= HAL_SMARTCARD_ERROR_ORE
;
898 tmp1
= __HAL_SMARTCARD_GET_FLAG(hsc
, SMARTCARD_FLAG_RXNE
);
899 tmp2
= __HAL_SMARTCARD_GET_IT_SOURCE(hsc
, SMARTCARD_IT_RXNE
);
900 /* SMARTCARD in mode Receiver --------------------------------------------------*/
901 if((tmp1
!= RESET
) && (tmp2
!= RESET
))
903 SMARTCARD_Receive_IT(hsc
);
906 tmp1
= __HAL_SMARTCARD_GET_FLAG(hsc
, SMARTCARD_FLAG_TXE
);
907 tmp2
= __HAL_SMARTCARD_GET_IT_SOURCE(hsc
, SMARTCARD_IT_TXE
);
908 /* SMARTCARD in mode Transmitter -----------------------------------------------*/
909 if((tmp1
!= RESET
) && (tmp2
!= RESET
))
911 SMARTCARD_Transmit_IT(hsc
);
914 tmp1
= __HAL_SMARTCARD_GET_FLAG(hsc
, SMARTCARD_FLAG_TC
);
915 tmp2
= __HAL_SMARTCARD_GET_IT_SOURCE(hsc
, SMARTCARD_IT_TC
);
916 /* SMARTCARD in mode Transmitter (transmission end) ------------------------*/
917 if((tmp1
!= RESET
) && (tmp2
!= RESET
))
919 SMARTCARD_EndTransmit_IT(hsc
);
922 /* Call the Error call Back in case of Errors */
923 if(hsc
->ErrorCode
!= HAL_SMARTCARD_ERROR_NONE
)
925 /* Set the SMARTCARD state ready to be able to start again the process */
926 hsc
->State
= HAL_SMARTCARD_STATE_READY
;
927 HAL_SMARTCARD_ErrorCallback(hsc
);
932 * @brief Tx Transfer completed callbacks.
933 * @param hsc: Pointer to a SMARTCARD_HandleTypeDef structure that contains
934 * the configuration information for the specified SMARTCARD module.
937 __weak
void HAL_SMARTCARD_TxCpltCallback(SMARTCARD_HandleTypeDef
*hsc
)
939 /* NOTE: This function should not be modified, when the callback is needed,
940 the HAL_SMARTCARD_TxCpltCallback can be implemented in the user file
945 * @brief Rx Transfer completed callbacks.
946 * @param hsc: Pointer to a SMARTCARD_HandleTypeDef structure that contains
947 * the configuration information for the specified SMARTCARD module.
950 __weak
void HAL_SMARTCARD_RxCpltCallback(SMARTCARD_HandleTypeDef
*hsc
)
952 /* NOTE: This function should not be modified, when the callback is needed,
953 the HAL_SMARTCARD_RxCpltCallback can be implemented in the user file
958 * @brief SMARTCARD error callbacks.
959 * @param hsc: Pointer to a SMARTCARD_HandleTypeDef structure that contains
960 * the configuration information for the specified SMARTCARD module.
963 __weak
void HAL_SMARTCARD_ErrorCallback(SMARTCARD_HandleTypeDef
*hsc
)
965 /* NOTE: This function should not be modified, when the callback is needed,
966 the HAL_SMARTCARD_ErrorCallback can be implemented in the user file
974 /** @defgroup SMARTCARD_Exported_Functions_Group3 Peripheral State and Errors functions
975 * @brief SMARTCARD State and Errors functions
978 ==============================================================================
979 ##### Peripheral State and Errors functions #####
980 ==============================================================================
982 This subsection provides a set of functions allowing to return the State of SmartCard
983 communication process and also return Peripheral Errors occurred during communication process
984 (+) HAL_SMARTCARD_GetState() API can be helpful to check in run-time the state
985 of the SMARTCARD peripheral.
986 (+) HAL_SMARTCARD_GetError() check in run-time errors that could be occurred during
994 * @brief Returns the SMARTCARD state.
995 * @param hsc: Pointer to a SMARTCARD_HandleTypeDef structure that contains
996 * the configuration information for the specified SMARTCARD module.
999 HAL_SMARTCARD_StateTypeDef
HAL_SMARTCARD_GetState(SMARTCARD_HandleTypeDef
*hsc
)
1005 * @brief Return the SMARTCARD error code
1006 * @param hsc: Pointer to a SMARTCARD_HandleTypeDef structure that contains
1007 * the configuration information for the specified SMARTCARD module.
1008 * @retval SMARTCARD Error Code
1010 uint32_t HAL_SMARTCARD_GetError(SMARTCARD_HandleTypeDef
*hsc
)
1012 return hsc
->ErrorCode
;
1023 /** @defgroup SMARTCARD_Private_Functions SMARTCARD Private Functions
1024 * @brief SMARTCARD Private functions
1028 * @brief DMA SMARTCARD transmit process complete callback.
1029 * @param hdma: Pointer to a DMA_HandleTypeDef structure that contains
1030 * the configuration information for the specified DMA module.
1033 static void SMARTCARD_DMATransmitCplt(DMA_HandleTypeDef
*hdma
)
1035 SMARTCARD_HandleTypeDef
* hsc
= ( SMARTCARD_HandleTypeDef
* )((DMA_HandleTypeDef
* )hdma
)->Parent
;
1037 hsc
->TxXferCount
= 0;
1039 /* Disable the DMA transfer for transmit request by setting the DMAT bit
1040 in the SMARTCARD CR3 register */
1041 CLEAR_BIT(hsc
->Instance
->CR3
, USART_CR3_DMAT
);
1043 /* Wait for SMARTCARD TC Flag */
1044 if(SMARTCARD_WaitOnFlagUntilTimeout(hsc
, SMARTCARD_FLAG_TC
, RESET
, SMARTCARD_TIMEOUT_VALUE
) != HAL_OK
)
1046 /* Timeout occurred */
1047 hsc
->State
= HAL_SMARTCARD_STATE_TIMEOUT
;
1048 HAL_SMARTCARD_ErrorCallback(hsc
);
1053 /* Check if a non-blocking receive process is ongoing or not */
1054 if(hsc
->State
== HAL_SMARTCARD_STATE_BUSY_TX_RX
)
1056 hsc
->State
= HAL_SMARTCARD_STATE_BUSY_RX
;
1060 hsc
->State
= HAL_SMARTCARD_STATE_READY
;
1062 HAL_SMARTCARD_TxCpltCallback(hsc
);
1067 * @brief DMA SMARTCARD receive process complete callback.
1068 * @param hdma: Pointer to a DMA_HandleTypeDef structure that contains
1069 * the configuration information for the specified DMA module.
1072 static void SMARTCARD_DMAReceiveCplt(DMA_HandleTypeDef
*hdma
)
1074 SMARTCARD_HandleTypeDef
* hsc
= ( SMARTCARD_HandleTypeDef
* )((DMA_HandleTypeDef
* )hdma
)->Parent
;
1076 hsc
->RxXferCount
= 0;
1078 /* Disable the DMA transfer for the receiver request by setting the DMAR bit
1079 in the USART CR3 register */
1080 CLEAR_BIT(hsc
->Instance
->CR3
, USART_CR3_DMAR
);
1082 /* Check if a non-blocking transmit process is ongoing or not */
1083 if(hsc
->State
== HAL_SMARTCARD_STATE_BUSY_TX_RX
)
1085 hsc
->State
= HAL_SMARTCARD_STATE_BUSY_TX
;
1089 hsc
->State
= HAL_SMARTCARD_STATE_READY
;
1092 HAL_SMARTCARD_RxCpltCallback(hsc
);
1096 * @brief DMA SMARTCARD communication error callback.
1097 * @param hdma: Pointer to a DMA_HandleTypeDef structure that contains
1098 * the configuration information for the specified DMA module.
1101 static void SMARTCARD_DMAError(DMA_HandleTypeDef
*hdma
)
1103 SMARTCARD_HandleTypeDef
* hsc
= ( SMARTCARD_HandleTypeDef
* )((DMA_HandleTypeDef
* )hdma
)->Parent
;
1105 hsc
->RxXferCount
= 0;
1106 hsc
->TxXferCount
= 0;
1107 hsc
->ErrorCode
= HAL_SMARTCARD_ERROR_DMA
;
1108 hsc
->State
= HAL_SMARTCARD_STATE_READY
;
1110 HAL_SMARTCARD_ErrorCallback(hsc
);
1114 * @brief This function handles SMARTCARD Communication Timeout.
1115 * @param hsc: Pointer to a SMARTCARD_HandleTypeDef structure that contains
1116 * the configuration information for the specified SMARTCARD module.
1117 * @param Flag: specifies the SMARTCARD flag to check.
1118 * @param Status: The new Flag status (SET or RESET).
1119 * @param Timeout: Timeout duration
1120 * @retval HAL status
1122 static HAL_StatusTypeDef
SMARTCARD_WaitOnFlagUntilTimeout(SMARTCARD_HandleTypeDef
*hsc
, uint32_t Flag
, FlagStatus Status
, uint32_t Timeout
)
1124 uint32_t tickstart
= 0;
1127 tickstart
= HAL_GetTick();
1129 /* Wait until flag is set */
1132 while(__HAL_SMARTCARD_GET_FLAG(hsc
, Flag
) == RESET
)
1134 /* Check for the Timeout */
1135 if(Timeout
!= HAL_MAX_DELAY
)
1137 if((Timeout
== 0)||((HAL_GetTick() - tickstart
) > Timeout
))
1139 /* Disable TXE and RXNE interrupts for the interrupt process */
1140 __HAL_SMARTCARD_DISABLE_IT(hsc
, SMARTCARD_IT_TXE
);
1141 __HAL_SMARTCARD_DISABLE_IT(hsc
, SMARTCARD_IT_RXNE
);
1143 hsc
->State
= HAL_SMARTCARD_STATE_READY
;
1145 /* Process Unlocked */
1155 while(__HAL_SMARTCARD_GET_FLAG(hsc
, Flag
) != RESET
)
1157 /* Check for the Timeout */
1158 if(Timeout
!= HAL_MAX_DELAY
)
1160 if((Timeout
== 0)||((HAL_GetTick() - tickstart
) > Timeout
))
1162 /* Disable TXE and RXNE interrupts for the interrupt process */
1163 __HAL_SMARTCARD_DISABLE_IT(hsc
, SMARTCARD_IT_TXE
);
1164 __HAL_SMARTCARD_DISABLE_IT(hsc
, SMARTCARD_IT_RXNE
);
1166 hsc
->State
= HAL_SMARTCARD_STATE_READY
;
1168 /* Process Unlocked */
1180 * @brief Send an amount of data in non-blocking mode.
1181 * @param hsc: Pointer to a SMARTCARD_HandleTypeDef structure that contains
1182 * the configuration information for the specified SMARTCARD module.
1183 * Function called under interruption only, once
1184 * interruptions have been enabled by HAL_SMARTCARD_Transmit_IT()
1185 * @retval HAL status
1187 static HAL_StatusTypeDef
SMARTCARD_Transmit_IT(SMARTCARD_HandleTypeDef
*hsc
)
1193 if((tmp1
== HAL_SMARTCARD_STATE_BUSY_TX
) || (tmp1
== HAL_SMARTCARD_STATE_BUSY_TX_RX
))
1195 if(hsc
->Init
.WordLength
== SMARTCARD_WORDLENGTH_9B
)
1197 tmp
= (uint16_t*) hsc
->pTxBuffPtr
;
1198 WRITE_REG(hsc
->Instance
->DR
, (uint16_t)(*tmp
& (uint16_t)0x01FF));
1199 if(hsc
->Init
.Parity
== SMARTCARD_PARITY_NONE
)
1201 hsc
->pTxBuffPtr
+= 2;
1205 hsc
->pTxBuffPtr
+= 1;
1210 WRITE_REG(hsc
->Instance
->DR
, (uint8_t)(*hsc
->pTxBuffPtr
++ & (uint8_t)0x00FF));
1213 if(--hsc
->TxXferCount
== 0)
1215 /* Disable the SMARTCARD Transmit Data Register Empty Interrupt */
1216 __HAL_SMARTCARD_DISABLE_IT(hsc
, SMARTCARD_IT_TXE
);
1218 /* Enable the SMARTCARD Transmit Complete Interrupt */
1219 __HAL_SMARTCARD_ENABLE_IT(hsc
, SMARTCARD_IT_TC
);
1232 * @brief Wraps up transmission in non blocking mode.
1233 * @param hsmartcard: pointer to a SMARTCARD_HandleTypeDef structure that contains
1234 * the configuration information for the specified SMARTCARD module.
1235 * @retval HAL status
1237 static HAL_StatusTypeDef
SMARTCARD_EndTransmit_IT(SMARTCARD_HandleTypeDef
*hsmartcard
)
1239 /* Disable the SMARTCARD Transmit Complete Interrupt */
1240 __HAL_SMARTCARD_DISABLE_IT(hsmartcard
, SMARTCARD_IT_TC
);
1242 /* Check if a receive process is ongoing or not */
1243 if(hsmartcard
->State
== HAL_SMARTCARD_STATE_BUSY_TX_RX
)
1245 hsmartcard
->State
= HAL_SMARTCARD_STATE_BUSY_RX
;
1249 /* Disable the SMARTCARD Error Interrupt: (Frame error, noise error, overrun error) */
1250 __HAL_SMARTCARD_DISABLE_IT(hsmartcard
, SMARTCARD_IT_ERR
);
1252 /* Disable the SMARTCARD Parity Error Interrupt */
1253 __HAL_SMARTCARD_DISABLE_IT(hsmartcard
, SMARTCARD_IT_PE
);
1255 hsmartcard
->State
= HAL_SMARTCARD_STATE_READY
;
1258 HAL_SMARTCARD_TxCpltCallback(hsmartcard
);
1265 * @brief Receive an amount of data in non-blocking mode.
1266 * @param hsc: Pointer to a SMARTCARD_HandleTypeDef structure that contains
1267 * the configuration information for the specified SMARTCARD module.
1268 * @retval HAL status
1270 static HAL_StatusTypeDef
SMARTCARD_Receive_IT(SMARTCARD_HandleTypeDef
*hsc
)
1276 if((tmp1
== HAL_SMARTCARD_STATE_BUSY_RX
) || (tmp1
== HAL_SMARTCARD_STATE_BUSY_TX_RX
))
1278 if(hsc
->Init
.WordLength
== SMARTCARD_WORDLENGTH_9B
)
1280 tmp
= (uint16_t*) hsc
->pRxBuffPtr
;
1281 if(hsc
->Init
.Parity
== SMARTCARD_PARITY_NONE
)
1283 *tmp
= (uint16_t)(hsc
->Instance
->DR
& (uint16_t)0x01FF);
1284 hsc
->pRxBuffPtr
+= 2;
1288 *tmp
= (uint16_t)(hsc
->Instance
->DR
& (uint16_t)0x00FF);
1289 hsc
->pRxBuffPtr
+= 1;
1294 if(hsc
->Init
.Parity
== SMARTCARD_PARITY_NONE
)
1296 *hsc
->pRxBuffPtr
++ = (uint8_t)(hsc
->Instance
->DR
& (uint8_t)0x00FF);
1300 *hsc
->pRxBuffPtr
++ = (uint8_t)(hsc
->Instance
->DR
& (uint8_t)0x007F);
1304 if(--hsc
->RxXferCount
== 0)
1306 __HAL_SMARTCARD_DISABLE_IT(hsc
, SMARTCARD_IT_RXNE
);
1308 /* Disable the SMARTCARD Parity Error Interrupt */
1309 __HAL_SMARTCARD_DISABLE_IT(hsc
, SMARTCARD_IT_PE
);
1311 /* Disable the SMARTCARD Error Interrupt: (Frame error, noise error, overrun error) */
1312 __HAL_SMARTCARD_DISABLE_IT(hsc
, SMARTCARD_IT_ERR
);
1314 /* Check if a non-blocking transmit process is ongoing or not */
1315 if(hsc
->State
== HAL_SMARTCARD_STATE_BUSY_TX_RX
)
1317 hsc
->State
= HAL_SMARTCARD_STATE_BUSY_TX
;
1321 hsc
->State
= HAL_SMARTCARD_STATE_READY
;
1324 HAL_SMARTCARD_RxCpltCallback(hsc
);
1337 * @brief Configures the SMARTCARD peripheral.
1338 * @param hsc: Pointer to a SMARTCARD_HandleTypeDef structure that contains
1339 * the configuration information for the specified SMARTCARD module.
1342 static void SMARTCARD_SetConfig(SMARTCARD_HandleTypeDef
*hsc
)
1344 /* Check the parameters */
1345 assert_param(IS_SMARTCARD_INSTANCE(hsc
->Instance
));
1346 assert_param(IS_SMARTCARD_POLARITY(hsc
->Init
.CLKPolarity
));
1347 assert_param(IS_SMARTCARD_PHASE(hsc
->Init
.CLKPhase
));
1348 assert_param(IS_SMARTCARD_LASTBIT(hsc
->Init
.CLKLastBit
));
1349 assert_param(IS_SMARTCARD_BAUDRATE(hsc
->Init
.BaudRate
));
1350 assert_param(IS_SMARTCARD_WORD_LENGTH(hsc
->Init
.WordLength
));
1351 assert_param(IS_SMARTCARD_STOPBITS(hsc
->Init
.StopBits
));
1352 assert_param(IS_SMARTCARD_PARITY(hsc
->Init
.Parity
));
1353 assert_param(IS_SMARTCARD_MODE(hsc
->Init
.Mode
));
1354 assert_param(IS_SMARTCARD_NACK_STATE(hsc
->Init
.NACKState
));
1356 /* The LBCL, CPOL and CPHA bits have to be selected when both the transmitter and the
1357 receiver are disabled (TE=RE=0) to ensure that the clock pulses function correctly. */
1358 CLEAR_BIT(hsc
->Instance
->CR1
, (uint32_t)(USART_CR1_TE
| USART_CR1_RE
));
1360 /*-------------------------- SMARTCARD CR2 Configuration ------------------------*/
1361 /* Clear CLKEN, CPOL, CPHA and LBCL bits */
1362 /* Configure the SMARTCARD Clock, CPOL, CPHA and LastBit -----------------------*/
1363 /* Set CPOL bit according to hsc->Init.CLKPolarity value */
1364 /* Set CPHA bit according to hsc->Init.CLKPhase value */
1365 /* Set LBCL bit according to hsc->Init.CLKLastBit value */
1366 MODIFY_REG(hsc
->Instance
->CR2
,
1367 ((uint32_t)(USART_CR2_CPHA
| USART_CR2_CPOL
| USART_CR2_CLKEN
| USART_CR2_LBCL
)),
1368 ((uint32_t)(USART_CR2_CLKEN
| hsc
->Init
.CLKPolarity
| hsc
->Init
.CLKPhase
| hsc
->Init
.CLKLastBit
)) );
1370 /* Set Stop Bits: Set STOP[13:12] bits according to hsc->Init.StopBits value */
1371 MODIFY_REG(hsc
->Instance
->CR2
, USART_CR2_STOP
,(uint32_t)(hsc
->Init
.StopBits
));
1373 /*-------------------------- SMARTCARD CR1 Configuration -----------------------*/
1374 /* Clear M, PCE, PS, TE and RE bits */
1375 /* Configure the SMARTCARD Word Length, Parity and mode:
1376 Set the M bits according to hsc->Init.WordLength value
1377 Set PCE and PS bits according to hsc->Init.Parity value
1378 Set TE and RE bits according to hsc->Init.Mode value */
1379 MODIFY_REG(hsc
->Instance
->CR1
,
1380 ((uint32_t)(USART_CR1_M
| USART_CR1_PCE
| USART_CR1_PS
| USART_CR1_TE
| USART_CR1_RE
)),
1381 ((uint32_t)(hsc
->Init
.WordLength
| hsc
->Init
.Parity
| hsc
->Init
.Mode
)) );
1383 /*-------------------------- USART CR3 Configuration -----------------------*/
1384 /* Clear CTSE and RTSE bits */
1385 CLEAR_BIT(hsc
->Instance
->CR3
, (USART_CR3_RTSE
| USART_CR3_CTSE
));
1387 /*-------------------------- USART BRR Configuration -----------------------*/
1388 if(hsc
->Instance
== USART1
)
1390 hsc
->Instance
->BRR
= SMARTCARD_BRR(HAL_RCC_GetPCLK2Freq(), hsc
->Init
.BaudRate
);
1394 hsc
->Instance
->BRR
= SMARTCARD_BRR(HAL_RCC_GetPCLK1Freq(), hsc
->Init
.BaudRate
);
1402 #endif /* HAL_SMARTCARD_MODULE_ENABLED */
1411 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/