2 ******************************************************************************
3 * @file stm32f3xx_hal_smartcard.c
4 * @author MCD Application Team
7 * @brief SMARTCARD HAL module driver.
9 * This file provides firmware functions to manage the following
10 * functionalities of the SmartCard.
11 * + Initialization and de-initialization functions
12 * + IO operation functions
13 * + Peripheral Control functions
17 ===============================================================================
18 ##### How to use this driver #####
19 ===============================================================================
21 The SMARTCARD HAL driver can be used as follows:
23 (#) Declare a SMARTCARD_HandleTypeDef handle structure.
24 (#) Associate a USART to the SMARTCARD handle hsmartcard.
25 (#) Initialize the SMARTCARD low level resources by implementing the HAL_SMARTCARD_MspInit ()API:
26 (##) Enable the USARTx interface clock.
27 (##) USART pins configuration:
28 (+) Enable the clock for the USART GPIOs.
29 (+) Configure these USART pins as alternate function pull-up.
30 (##) NVIC configuration if you need to use interrupt process (HAL_SMARTCARD_Transmit_IT()
31 and HAL_SMARTCARD_Receive_IT() APIs):
32 (+) Configure the USARTx interrupt priority.
33 (+) Enable the NVIC USART IRQ handle.
34 (@) The specific USART interrupts (Transmission complete interrupt,
35 RXNE interrupt and Error Interrupts) will be managed using the macros
36 __HAL_SMARTCARD_ENABLE_IT() and __HAL_SMARTCARD_DISABLE_IT() inside the transmit and receive process.
37 (##) DMA Configuration if you need to use DMA process (HAL_SMARTCARD_Transmit_DMA()
38 and HAL_SMARTCARD_Receive_DMA() APIs):
39 (+) Declare a DMA handle structure for the Tx/Rx channel.
40 (+) Enable the DMAx interface clock.
41 (+) Configure the declared DMA handle structure with the required Tx/Rx parameters.
42 (+) Configure the DMA Tx/Rx channel.
43 (+) Associate the initialized DMA handle to the SMARTCARD DMA Tx/Rx handle.
44 (+) Configure the priority and enable the NVIC for the transfer complete interrupt on the DMA Tx/Rx channel.
46 (#) Program the Baud Rate, Parity, Mode(Receiver/Transmitter), clock enabling/disabling and accordingly,
47 the clock parameters (parity, phase, last bit), prescaler value, guard time and NACK on transmission
48 error enabling or disabling in the hsmartcard Init structure.
50 (#) If required, program SMARTCARD advanced features (TX/RX pins swap, TimeOut, auto-retry counter,...)
51 in the hsmartcard AdvancedInit structure.
53 (#) Initialize the SMARTCARD associated USART registers by calling
54 the HAL_SMARTCARD_Init() API.
56 (@) HAL_SMARTCARD_Init() API also configure also the low level Hardware GPIO, CLOCK, CORTEX...etc) by
57 calling the customized HAL_SMARTCARD_MspInit() API.
60 ******************************************************************************
63 * <h2><center>© COPYRIGHT(c) 2014 STMicroelectronics</center></h2>
65 * Redistribution and use in source and binary forms, with or without modification,
66 * are permitted provided that the following conditions are met:
67 * 1. Redistributions of source code must retain the above copyright notice,
68 * this list of conditions and the following disclaimer.
69 * 2. Redistributions in binary form must reproduce the above copyright notice,
70 * this list of conditions and the following disclaimer in the documentation
71 * and/or other materials provided with the distribution.
72 * 3. Neither the name of STMicroelectronics nor the names of its contributors
73 * may be used to endorse or promote products derived from this software
74 * without specific prior written permission.
76 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
77 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
78 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
79 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
80 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
81 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
82 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
83 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
84 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
85 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
87 ******************************************************************************
90 /* Includes ------------------------------------------------------------------*/
91 #include "stm32f3xx_hal.h"
93 /** @addtogroup STM32F3xx_HAL_Driver
97 /** @defgroup SMARTCARD SMARTCARD HAL module driver
98 * @brief SMARTCARD HAL module driver
101 #ifdef HAL_SMARTCARD_MODULE_ENABLED
103 /* Private typedef -----------------------------------------------------------*/
104 /* Private define ------------------------------------------------------------*/
105 /** @defgroup SMARTCARD_Private_Define SMARTCARD Private Define
108 #define TEACK_REACK_TIMEOUT 1000
109 #define SMARTCARD_TXDMA_TIMEOUTVALUE 22000
110 #define SMARTCARD_TIMEOUT_VALUE 22000
111 #define USART_CR1_FIELDS ((uint32_t)(USART_CR1_M | USART_CR1_PCE | USART_CR1_PS | \
112 USART_CR1_TE | USART_CR1_RE | USART_CR1_OVER8))
113 #define USART_CR2_CLK_FIELDS ((uint32_t)(USART_CR2_CLKEN|USART_CR2_CPOL|USART_CR2_CPHA|USART_CR2_LBCL))
114 #define USART_CR2_FIELDS ((uint32_t)(USART_CR2_RTOEN|USART_CR2_CLK_FIELDS|USART_CR2_STOP))
115 #define USART_CR3_FIELDS ((uint32_t)(USART_CR3_ONEBIT|USART_CR3_NACK|USART_CR3_SCARCNT))
120 /* Private macro -------------------------------------------------------------*/
121 /* Private variables ---------------------------------------------------------*/
122 /* Private function prototypes -----------------------------------------------*/
123 /** @defgroup SMARTCARD_Private_Functions SMARTCARD Private Functions
126 static void SMARTCARD_DMATransmitCplt(DMA_HandleTypeDef *hdma);
127 static void SMARTCARD_DMAReceiveCplt(DMA_HandleTypeDef *hdma);
128 static void SMARTCARD_DMAError(DMA_HandleTypeDef *hdma);
129 static HAL_StatusTypeDef SMARTCARD_SetConfig(SMARTCARD_HandleTypeDef *hsmartcard);
130 static void SMARTCARD_AdvFeatureConfig(SMARTCARD_HandleTypeDef *hsmartcard);
131 static HAL_StatusTypeDef SMARTCARD_WaitOnFlagUntilTimeout(SMARTCARD_HandleTypeDef *hsmartcard, uint32_t Flag, FlagStatus Status, uint32_t Timeout);
132 static HAL_StatusTypeDef SMARTCARD_CheckIdleState(SMARTCARD_HandleTypeDef *hsmartcard);
133 static HAL_StatusTypeDef SMARTCARD_Transmit_IT(SMARTCARD_HandleTypeDef *hsmartcard);
134 static HAL_StatusTypeDef SMARTCARD_EndTransmit_IT(SMARTCARD_HandleTypeDef *hsmartcard);
135 static HAL_StatusTypeDef SMARTCARD_Receive_IT(SMARTCARD_HandleTypeDef *hsmartcard);
140 /* Exported functions ---------------------------------------------------------*/
142 /** @defgroup SMARTCARD_Exported_Functions SMARTCARD Exported Functions
146 /** @defgroup SMARTCARD_Exported_Functions_Group1 Initialization and de-initialization functions
147 * @brief Initialization and Configuration functions
150 ===============================================================================
151 ##### Initialization and Configuration functions #####
152 ===============================================================================
154 This subsection provides a set of functions allowing to initialize the USARTx
155 associated to the SmartCard.
156 (+) These parameters can be configured:
158 (++) Parity: parity should be enabled,
159 Frame Length is fixed to 8 bits plus parity:
160 the USART frame format is given in the following table:
161 +---------------------------------------------------------------+
162 | M bit | PCE bit | USART frame |
163 |---------------------|-----------------------------------------|
164 | 1 | 1 | | SB | 8 bit data | PB | STB | |
165 +---------------------------------------------------------------+
167 +---------------------------------------------------------------+
168 | M1M0 bits | PCE bit | USART frame |
169 |-----------------------|---------------------------------------|
170 | 01 | 1 | | SB | 8 bit data | PB | STB | |
171 +---------------------------------------------------------------+
173 (++) Receiver/transmitter modes
174 (++) Synchronous mode (and if enabled, phase, polarity and last bit parameters)
177 (++) NACK enabling or disabling on transmission error
179 (+) The following advanced features can be configured as well:
180 (++) TX and/or RX pin level inversion
181 (++) data logical level inversion
182 (++) RX and TX pins swap
183 (++) RX overrun detection disabling
184 (++) DMA disabling on RX error
185 (++) MSB first on communication line
186 (++) Time out enabling (and if activated, timeout value)
188 (++) Auto-retry counter
191 The HAL_SMARTCARD_Init() API follow respectively the USART (a)synchronous configuration procedures
192 (details for the procedures are available in reference manual).
199 * @brief Initializes the SMARTCARD mode according to the specified
200 * parameters in the SMARTCARD_InitTypeDef and creates the associated handle .
201 * @param hsmartcard: SMARTCARD handle
204 HAL_StatusTypeDef HAL_SMARTCARD_Init(SMARTCARD_HandleTypeDef *hsmartcard)
206 /* Check the SMARTCARD handle allocation */
207 if(hsmartcard == HAL_NULL)
212 /* Check the USART associated to the SmartCard */
213 assert_param(IS_SMARTCARD_INSTANCE(hsmartcard->Instance));
215 if(hsmartcard->State == HAL_SMARTCARD_STATE_RESET)
217 /* Init the low level hardware : GPIO, CLOCK */
218 HAL_SMARTCARD_MspInit(hsmartcard);
221 hsmartcard->State = HAL_SMARTCARD_STATE_BUSY;
223 /* Disable the Peripheral */
224 __HAL_SMARTCARD_DISABLE(hsmartcard);
226 /* Set the SMARTCARD Communication parameters */
227 if (SMARTCARD_SetConfig(hsmartcard) == HAL_ERROR)
232 if (hsmartcard->AdvancedInit.AdvFeatureInit != SMARTCARD_ADVFEATURE_NO_INIT)
234 SMARTCARD_AdvFeatureConfig(hsmartcard);
237 /* In SmartCard mode, the following bits must be kept cleared:
238 - LINEN in the USART_CR2 register,
239 - HDSEL and IREN bits in the USART_CR3 register.*/
240 hsmartcard->Instance->CR2 &= ~(USART_CR2_LINEN);
241 hsmartcard->Instance->CR3 &= ~(USART_CR3_HDSEL | USART_CR3_IREN);
243 /* set the USART in SMARTCARD mode */
244 hsmartcard->Instance->CR3 |= USART_CR3_SCEN;
246 /* Enable the Peripheral */
247 __HAL_SMARTCARD_ENABLE(hsmartcard);
249 /* TEACK and/or REACK to check before moving hsmartcard->State to Ready */
250 return (SMARTCARD_CheckIdleState(hsmartcard));
255 * @brief DeInitializes the SMARTCARD peripheral
256 * @param hsmartcard: SMARTCARD handle
259 HAL_StatusTypeDef HAL_SMARTCARD_DeInit(SMARTCARD_HandleTypeDef *hsmartcard)
261 /* Check the SMARTCARD handle allocation */
262 if(hsmartcard == HAL_NULL)
267 /* Check the parameters */
268 assert_param(IS_SMARTCARD_INSTANCE(hsmartcard->Instance));
270 hsmartcard->State = HAL_SMARTCARD_STATE_BUSY;
272 /* Disable the Peripheral */
273 __HAL_SMARTCARD_DISABLE(hsmartcard);
275 hsmartcard->Instance->CR1 = 0x0;
276 hsmartcard->Instance->CR2 = 0x0;
277 hsmartcard->Instance->CR3 = 0x0;
278 hsmartcard->Instance->RTOR = 0x0;
279 hsmartcard->Instance->GTPR = 0x0;
281 /* DeInit the low level hardware */
282 HAL_SMARTCARD_MspDeInit(hsmartcard);
284 hsmartcard->ErrorCode = HAL_SMARTCARD_ERROR_NONE;
285 hsmartcard->State = HAL_SMARTCARD_STATE_RESET;
288 __HAL_UNLOCK(hsmartcard);
294 * @brief SMARTCARD MSP Init
295 * @param hsmartcard: SMARTCARD handle
298 __weak void HAL_SMARTCARD_MspInit(SMARTCARD_HandleTypeDef *hsmartcard)
300 /* NOTE : This function should not be modified, when the callback is needed,
301 the HAL_SMARTCARD_MspInit can be implemented in the user file
306 * @brief SMARTCARD MSP DeInit
307 * @param hsmartcard: SMARTCARD handle
310 __weak void HAL_SMARTCARD_MspDeInit(SMARTCARD_HandleTypeDef *hsmartcard)
312 /* NOTE : This function should not be modified, when the callback is needed,
313 the HAL_SMARTCARD_MspDeInit can be implemented in the user file
321 /** @defgroup SMARTCARD_Exported_Functions_Group2 Input and Output operation functions
322 * @brief SMARTCARD Transmit/Receive functions
325 ===============================================================================
326 ##### I/O operation functions #####
327 ===============================================================================
328 This subsection provides a set of functions allowing to manage the SMARTCARD data transfers.
330 (#) There are two mode of transfer:
331 (+) Blocking mode: The communication is performed in polling mode.
332 The HAL status of all data processing is returned by the same function
333 after finishing transfer.
334 (+) No-Blocking mode: The communication is performed using Interrupts
335 or DMA, These API's return the HAL status.
336 The end of the data processing will be indicated through the
337 dedicated SMARTCARD IRQ when using Interrupt mode or the DMA IRQ when
339 The HAL_SMARTCARD_TxCpltCallback(), HAL_SMARTCARD_RxCpltCallback() user callbacks
340 will be executed respectivelly at the end of the transmit or Receive process
341 The HAL_SMARTCARD_ErrorCallback()user callback will be executed when a communication error is detected
343 (#) Blocking mode API's are :
344 (+) HAL_SMARTCARD_Transmit()
345 (+) HAL_SMARTCARD_Receive()
347 (#) Non-Blocking mode API's with Interrupt are :
348 (+) HAL_SMARTCARD_Transmit_IT()
349 (+) HAL_SMARTCARD_Receive_IT()
350 (+) HAL_SMARTCARD_IRQHandler()
352 (#) No-Blocking mode functions with DMA are :
353 (+) HAL_SMARTCARD_Transmit_DMA()
354 (+) HAL_SMARTCARD_Receive_DMA()
356 (#) A set of Transfer Complete Callbacks are provided in No_Blocking mode:
357 (+) HAL_SMARTCARD_TxCpltCallback()
358 (+) HAL_SMARTCARD_RxCpltCallback()
359 (+) HAL_SMARTCARD_ErrorCallback()
366 * @brief Send an amount of data in blocking mode
367 * @param hsmartcard: SMARTCARD handle
368 * @param pData: pointer to data buffer
369 * @param Size: amount of data to be sent
370 * @param Timeout : Timeout duration
373 HAL_StatusTypeDef HAL_SMARTCARD_Transmit(SMARTCARD_HandleTypeDef *hsmartcard, uint8_t *pData, uint16_t Size, uint32_t Timeout)
375 if ((hsmartcard->State == HAL_SMARTCARD_STATE_READY) || (hsmartcard->State == HAL_SMARTCARD_STATE_BUSY_RX))
377 if((pData == HAL_NULL) || (Size == 0))
383 __HAL_LOCK(hsmartcard);
384 hsmartcard->ErrorCode = HAL_SMARTCARD_ERROR_NONE;
385 /* Check if a non-blocking receive process is ongoing or not */
386 if(hsmartcard->State == HAL_SMARTCARD_STATE_BUSY_RX)
388 hsmartcard->State = HAL_SMARTCARD_STATE_BUSY_TX_RX;
392 hsmartcard->State = HAL_SMARTCARD_STATE_BUSY_TX;
395 hsmartcard->TxXferSize = Size;
396 hsmartcard->TxXferCount = Size;
397 while(hsmartcard->TxXferCount > 0)
399 hsmartcard->TxXferCount--;
400 if(SMARTCARD_WaitOnFlagUntilTimeout(hsmartcard, SMARTCARD_FLAG_TXE, RESET, Timeout) != HAL_OK)
404 hsmartcard->Instance->TDR = (*pData++ & (uint8_t)0xFF);
406 if(SMARTCARD_WaitOnFlagUntilTimeout(hsmartcard, SMARTCARD_FLAG_TC, RESET, Timeout) != HAL_OK)
410 /* Check if a non-blocking receive Process is ongoing or not */
411 if(hsmartcard->State == HAL_SMARTCARD_STATE_BUSY_TX_RX)
413 hsmartcard->State = HAL_SMARTCARD_STATE_BUSY_RX;
417 hsmartcard->State = HAL_SMARTCARD_STATE_READY;
420 /* Process Unlocked */
421 __HAL_UNLOCK(hsmartcard);
432 * @brief Receive an amount of data in blocking mode
433 * @param hsmartcard: SMARTCARD handle
434 * @param pData: pointer to data buffer
435 * @param Size: amount of data to be received
436 * @param Timeout : Timeout duration
439 HAL_StatusTypeDef HAL_SMARTCARD_Receive(SMARTCARD_HandleTypeDef *hsmartcard, uint8_t *pData, uint16_t Size, uint32_t Timeout)
441 if ((hsmartcard->State == HAL_SMARTCARD_STATE_READY) || (hsmartcard->State == HAL_SMARTCARD_STATE_BUSY_TX))
443 if((pData == HAL_NULL) || (Size == 0))
449 __HAL_LOCK(hsmartcard);
451 hsmartcard->ErrorCode = HAL_SMARTCARD_ERROR_NONE;
452 /* Check if a non-blocking transmit process is ongoing or not */
453 if(hsmartcard->State == HAL_SMARTCARD_STATE_BUSY_TX)
455 hsmartcard->State = HAL_SMARTCARD_STATE_BUSY_TX_RX;
459 hsmartcard->State = HAL_SMARTCARD_STATE_BUSY_RX;
462 hsmartcard->RxXferSize = Size;
463 hsmartcard->RxXferCount = Size;
464 /* Check the remain data to be received */
465 while(hsmartcard->RxXferCount > 0)
467 hsmartcard->RxXferCount--;
468 if(SMARTCARD_WaitOnFlagUntilTimeout(hsmartcard, SMARTCARD_FLAG_RXNE, RESET, Timeout) != HAL_OK)
472 *pData++ = (uint8_t)(hsmartcard->Instance->RDR & (uint8_t)0x00FF);
475 /* Check if a non-blocking transmit Process is ongoing or not */
476 if(hsmartcard->State == HAL_SMARTCARD_STATE_BUSY_TX_RX)
478 hsmartcard->State = HAL_SMARTCARD_STATE_BUSY_TX;
482 hsmartcard->State = HAL_SMARTCARD_STATE_READY;
485 /* Process Unlocked */
486 __HAL_UNLOCK(hsmartcard);
497 * @brief Send an amount of data in interrupt mode
498 * @param hsmartcard: SMARTCARD handle
499 * @param pData: pointer to data buffer
500 * @param Size: amount of data to be sent
503 HAL_StatusTypeDef HAL_SMARTCARD_Transmit_IT(SMARTCARD_HandleTypeDef *hsmartcard, uint8_t *pData, uint16_t Size)
505 if ((hsmartcard->State == HAL_SMARTCARD_STATE_READY) || (hsmartcard->State == HAL_SMARTCARD_STATE_BUSY_RX))
507 if((pData == HAL_NULL) || (Size == 0))
513 __HAL_LOCK(hsmartcard);
515 hsmartcard->pTxBuffPtr = pData;
516 hsmartcard->TxXferSize = Size;
517 hsmartcard->TxXferCount = Size;
519 hsmartcard->ErrorCode = HAL_SMARTCARD_ERROR_NONE;
520 /* Check if a receive process is ongoing or not */
521 if(hsmartcard->State == HAL_SMARTCARD_STATE_BUSY_RX)
523 hsmartcard->State = HAL_SMARTCARD_STATE_BUSY_TX_RX;
527 hsmartcard->State = HAL_SMARTCARD_STATE_BUSY_TX;
530 /* Enable the SMARTCARD Error Interrupt: (Frame error, noise error, overrun error) */
531 __HAL_SMARTCARD_ENABLE_IT(hsmartcard, SMARTCARD_IT_ERR);
533 /* Process Unlocked */
534 __HAL_UNLOCK(hsmartcard);
536 /* Enable the SMARTCARD Transmit Data Register Empty Interrupt */
537 __HAL_SMARTCARD_ENABLE_IT(hsmartcard, SMARTCARD_IT_TXE);
548 * @brief Receive an amount of data in interrupt mode
549 * @param hsmartcard: SMARTCARD handle
550 * @param pData: pointer to data buffer
551 * @param Size: amount of data to be received
554 HAL_StatusTypeDef HAL_SMARTCARD_Receive_IT(SMARTCARD_HandleTypeDef *hsmartcard, uint8_t *pData, uint16_t Size)
556 if ((hsmartcard->State == HAL_SMARTCARD_STATE_READY) || (hsmartcard->State == HAL_SMARTCARD_STATE_BUSY_TX))
558 if((pData == HAL_NULL) || (Size == 0))
564 __HAL_LOCK(hsmartcard);
566 hsmartcard->pRxBuffPtr = pData;
567 hsmartcard->RxXferSize = Size;
568 hsmartcard->RxXferCount = Size;
570 hsmartcard->ErrorCode = HAL_SMARTCARD_ERROR_NONE;
571 /* Check if a transmit process is ongoing or not */
572 if(hsmartcard->State == HAL_SMARTCARD_STATE_BUSY_TX)
574 hsmartcard->State = HAL_SMARTCARD_STATE_BUSY_TX_RX;
578 hsmartcard->State = HAL_SMARTCARD_STATE_BUSY_RX;
581 /* Enable the SMARTCARD Parity Error Interrupt */
582 __HAL_SMARTCARD_ENABLE_IT(hsmartcard, SMARTCARD_IT_PE);
584 /* Enable the SMARTCARD Error Interrupt: (Frame error, noise error, overrun error) */
585 __HAL_SMARTCARD_ENABLE_IT(hsmartcard, SMARTCARD_IT_ERR);
587 /* Process Unlocked */
588 __HAL_UNLOCK(hsmartcard);
590 /* Enable the SMARTCARD Data Register not empty Interrupt */
591 __HAL_SMARTCARD_ENABLE_IT(hsmartcard, SMARTCARD_IT_RXNE);
602 * @brief Send an amount of data in DMA mode
603 * @param hsmartcard: SMARTCARD handle
604 * @param pData: pointer to data buffer
605 * @param Size: amount of data to be sent
608 HAL_StatusTypeDef HAL_SMARTCARD_Transmit_DMA(SMARTCARD_HandleTypeDef *hsmartcard, uint8_t *pData, uint16_t Size)
612 if ((hsmartcard->State == HAL_SMARTCARD_STATE_READY) || (hsmartcard->State == HAL_SMARTCARD_STATE_BUSY_RX))
614 if((pData == HAL_NULL) || (Size == 0))
620 __HAL_LOCK(hsmartcard);
622 hsmartcard->pTxBuffPtr = pData;
623 hsmartcard->TxXferSize = Size;
624 hsmartcard->TxXferCount = Size;
626 hsmartcard->ErrorCode = HAL_SMARTCARD_ERROR_NONE;
627 /* Check if a receive process is ongoing or not */
628 if(hsmartcard->State == HAL_SMARTCARD_STATE_BUSY_RX)
630 hsmartcard->State = HAL_SMARTCARD_STATE_BUSY_TX_RX;
634 hsmartcard->State = HAL_SMARTCARD_STATE_BUSY_TX;
637 /* Set the SMARTCARD DMA transfer complete callback */
638 hsmartcard->hdmatx->XferCpltCallback = SMARTCARD_DMATransmitCplt;
640 /* Set the SMARTCARD error callback */
641 hsmartcard->hdmatx->XferErrorCallback = SMARTCARD_DMAError;
643 /* Enable the SMARTCARD transmit DMA channel */
644 tmp = (uint32_t*)&pData;
645 HAL_DMA_Start_IT(hsmartcard->hdmatx, *(uint32_t*)tmp, (uint32_t)&hsmartcard->Instance->TDR, Size);
647 /* Enable the DMA transfer for transmit request by setting the DMAT bit
648 in the SMARTCARD associated USART CR3 register */
649 hsmartcard->Instance->CR3 |= USART_CR3_DMAT;
651 /* Process Unlocked */
652 __HAL_UNLOCK(hsmartcard);
663 * @brief Receive an amount of data in DMA mode
664 * @param hsmartcard: SMARTCARD handle
665 * @param pData: pointer to data buffer
666 * @param Size: amount of data to be received
667 * @note The SMARTCARD-associated USART parity is enabled (PCE = 1),
668 * the received data contain the parity bit (MSB position)
671 HAL_StatusTypeDef HAL_SMARTCARD_Receive_DMA(SMARTCARD_HandleTypeDef *hsmartcard, uint8_t *pData, uint16_t Size)
675 if ((hsmartcard->State == HAL_SMARTCARD_STATE_READY) || (hsmartcard->State == HAL_SMARTCARD_STATE_BUSY_TX))
677 if((pData == HAL_NULL) || (Size == 0))
683 __HAL_LOCK(hsmartcard);
685 hsmartcard->pRxBuffPtr = pData;
686 hsmartcard->RxXferSize = Size;
688 hsmartcard->ErrorCode = HAL_SMARTCARD_ERROR_NONE;
689 /* Check if a transmit rocess is ongoing or not */
690 if(hsmartcard->State == HAL_SMARTCARD_STATE_BUSY_TX)
692 hsmartcard->State = HAL_SMARTCARD_STATE_BUSY_TX_RX;
696 hsmartcard->State = HAL_SMARTCARD_STATE_BUSY_RX;
699 /* Set the SMARTCARD DMA transfer complete callback */
700 hsmartcard->hdmarx->XferCpltCallback = SMARTCARD_DMAReceiveCplt;
702 /* Set the SMARTCARD DMA error callback */
703 hsmartcard->hdmarx->XferErrorCallback = SMARTCARD_DMAError;
705 /* Enable the DMA channel */
706 tmp = (uint32_t*)&pData;
707 HAL_DMA_Start_IT(hsmartcard->hdmarx, (uint32_t)&hsmartcard->Instance->RDR, *(uint32_t*)tmp, Size);
709 /* Enable the DMA transfer for the receiver request by setting the DMAR bit
710 in the SMARTCARD associated USART CR3 register */
711 hsmartcard->Instance->CR3 |= USART_CR3_DMAR;
713 /* Process Unlocked */
714 __HAL_UNLOCK(hsmartcard);
725 * @brief SMARTCARD interrupt requests handling.
726 * @param hsmartcard: SMARTCARD handle
729 void HAL_SMARTCARD_IRQHandler(SMARTCARD_HandleTypeDef *hsmartcard)
731 /* SMARTCARD parity error interrupt occurred -------------------------------------*/
732 if((__HAL_SMARTCARD_GET_IT(hsmartcard, SMARTCARD_IT_PE) != RESET) && (__HAL_SMARTCARD_GET_IT_SOURCE(hsmartcard, SMARTCARD_IT_PE) != RESET))
734 __HAL_SMARTCARD_CLEAR_IT(hsmartcard, SMARTCARD_CLEAR_PEF);
735 hsmartcard->ErrorCode |= HAL_SMARTCARD_ERROR_PE;
736 /* Set the SMARTCARD state ready to be able to start again the process */
737 hsmartcard->State = HAL_SMARTCARD_STATE_READY;
740 /* SMARTCARD frame error interrupt occured --------------------------------------*/
741 if((__HAL_SMARTCARD_GET_IT(hsmartcard, SMARTCARD_IT_FE) != RESET) && (__HAL_SMARTCARD_GET_IT_SOURCE(hsmartcard, SMARTCARD_IT_ERR) != RESET))
743 __HAL_SMARTCARD_CLEAR_IT(hsmartcard, SMARTCARD_CLEAR_FEF);
744 hsmartcard->ErrorCode |= HAL_SMARTCARD_ERROR_FE;
745 /* Set the SMARTCARD state ready to be able to start again the process */
746 hsmartcard->State = HAL_SMARTCARD_STATE_READY;
749 /* SMARTCARD noise error interrupt occured --------------------------------------*/
750 if((__HAL_SMARTCARD_GET_IT(hsmartcard, SMARTCARD_IT_NE) != RESET) && (__HAL_SMARTCARD_GET_IT_SOURCE(hsmartcard, SMARTCARD_IT_ERR) != RESET))
752 __HAL_SMARTCARD_CLEAR_IT(hsmartcard, SMARTCARD_CLEAR_NEF);
753 hsmartcard->ErrorCode |= HAL_SMARTCARD_ERROR_NE;
754 /* Set the SMARTCARD state ready to be able to start again the process */
755 hsmartcard->State = HAL_SMARTCARD_STATE_READY;
758 /* SMARTCARD Over-Run interrupt occured -----------------------------------------*/
759 if((__HAL_SMARTCARD_GET_IT(hsmartcard, SMARTCARD_IT_ORE) != RESET) && (__HAL_SMARTCARD_GET_IT_SOURCE(hsmartcard, SMARTCARD_IT_ERR) != RESET))
761 __HAL_SMARTCARD_CLEAR_IT(hsmartcard, SMARTCARD_CLEAR_OREF);
762 hsmartcard->ErrorCode |= HAL_SMARTCARD_ERROR_ORE;
763 /* Set the SMARTCARD state ready to be able to start again the process */
764 hsmartcard->State = HAL_SMARTCARD_STATE_READY;
767 /* SMARTCARD receiver timeout interrupt occured -----------------------------------------*/
768 if((__HAL_SMARTCARD_GET_IT(hsmartcard, SMARTCARD_IT_RTO) != RESET) && (__HAL_SMARTCARD_GET_IT_SOURCE(hsmartcard, SMARTCARD_IT_RTO) != RESET))
770 __HAL_SMARTCARD_CLEAR_IT(hsmartcard, SMARTCARD_CLEAR_RTOF);
771 hsmartcard->ErrorCode |= HAL_SMARTCARD_ERROR_RTO;
772 /* Set the SMARTCARD state ready to be able to start again the process */
773 hsmartcard->State = HAL_SMARTCARD_STATE_READY;
776 /* Call SMARTCARD Error Call back function if need be --------------------------*/
777 if(hsmartcard->ErrorCode != HAL_SMARTCARD_ERROR_NONE)
779 HAL_SMARTCARD_ErrorCallback(hsmartcard);
782 /* SMARTCARD in mode Receiver ---------------------------------------------------*/
783 if((__HAL_SMARTCARD_GET_IT(hsmartcard, SMARTCARD_IT_RXNE) != RESET) && (__HAL_SMARTCARD_GET_IT_SOURCE(hsmartcard, SMARTCARD_IT_RXNE) != RESET))
785 SMARTCARD_Receive_IT(hsmartcard);
786 /* Clear RXNE interrupt flag */
787 __HAL_SMARTCARD_SEND_REQ(hsmartcard, SMARTCARD_RXDATA_FLUSH_REQUEST);
790 /* SMARTCARD in mode Receiver, end of block interruption ------------------------*/
791 if((__HAL_SMARTCARD_GET_IT(hsmartcard, SMARTCARD_IT_EOB) != RESET) && (__HAL_SMARTCARD_GET_IT_SOURCE(hsmartcard, SMARTCARD_IT_EOB) != RESET))
793 hsmartcard->State = HAL_SMARTCARD_STATE_READY;
794 __HAL_UNLOCK(hsmartcard);
795 HAL_SMARTCARD_RxCpltCallback(hsmartcard);
796 /* Clear EOBF interrupt after HAL_SMARTCARD_RxCpltCallback() call for the End of Block information
797 * to be available during HAL_SMARTCARD_RxCpltCallback() processing */
798 __HAL_SMARTCARD_CLEAR_IT(hsmartcard, SMARTCARD_CLEAR_EOBF);
801 /* SMARTCARD in mode Transmitter ------------------------------------------------*/
802 if((__HAL_SMARTCARD_GET_IT(hsmartcard, SMARTCARD_IT_TXE) != RESET) &&(__HAL_SMARTCARD_GET_IT_SOURCE(hsmartcard, SMARTCARD_IT_TXE) != RESET))
804 SMARTCARD_Transmit_IT(hsmartcard);
807 /* SMARTCARD in mode Transmitter (transmission end) ------------------------*/
808 if((__HAL_SMARTCARD_GET_IT(hsmartcard, SMARTCARD_IT_TC) != RESET) &&(__HAL_SMARTCARD_GET_IT_SOURCE(hsmartcard, SMARTCARD_IT_TC) != RESET))
810 SMARTCARD_EndTransmit_IT(hsmartcard);
815 * @brief Tx Transfer completed callbacks
816 * @param hsmartcard: SMARTCARD handle
819 __weak void HAL_SMARTCARD_TxCpltCallback(SMARTCARD_HandleTypeDef *hsmartcard)
821 /* NOTE : This function should not be modified, when the callback is needed,
822 the HAL_SMARTCARD_TxCpltCallback can be implemented in the user file
827 * @brief Rx Transfer completed callbacks
828 * @param hsmartcard: SMARTCARD handle
831 __weak void HAL_SMARTCARD_RxCpltCallback(SMARTCARD_HandleTypeDef *hsmartcard)
833 /* NOTE : This function should not be modified, when the callback is needed,
834 the HAL_SMARTCARD_TxCpltCallback can be implemented in the user file
839 * @brief SMARTCARD error callbacks
840 * @param hsmartcard: SMARTCARD handle
843 __weak void HAL_SMARTCARD_ErrorCallback(SMARTCARD_HandleTypeDef *hsmartcard)
845 /* NOTE : This function should not be modified, when the callback is needed,
846 the HAL_SMARTCARD_ErrorCallback can be implemented in the user file
854 /** @defgroup SMARTCARD_Exported_Functions_Group3 Peripheral Control functions
855 * @brief SMARTCARD control functions
858 ===============================================================================
859 ##### Peripheral Control functions #####
860 ===============================================================================
862 This subsection provides a set of functions allowing to initialize the SMARTCARD.
863 (+) HAL_SMARTCARD_GetState() API is helpful to check in run-time the state of the SMARTCARD peripheral
864 (+) HAL_SMARTCARD_GetError() API is helpful to check in run-time the error of the SMARTCARD peripheral
872 * @brief return the SMARTCARD state
873 * @param hsmartcard: SMARTCARD handle
876 HAL_SMARTCARD_StateTypeDef HAL_SMARTCARD_GetState(SMARTCARD_HandleTypeDef *hsmartcard)
878 return hsmartcard->State;
882 * @brief Return the SMARTCARD error code
883 * @param hsmartcard : pointer to a SMARTCARD_HandleTypeDef structure that contains
884 * the configuration information for the specified SMARTCARD.
885 * @retval SMARTCARD Error Code
887 uint32_t HAL_SMARTCARD_GetError(SMARTCARD_HandleTypeDef *hsmartcard)
889 return hsmartcard->ErrorCode;
900 /** @addtogroup SMARTCARD_Private_Functions SMARTCARD Private Functions
904 /** @defgroup SMARTCARD_Private_Functions_Group2 Input and Output operation private functions
905 * @brief SMARTCARD Transmit/Receive private functions
907 ===============================================================================
908 ##### I/O operation private functions #####
909 ===============================================================================
910 This subsection provides a set of functions allowing to manage the SMARTCARD data transfers.
911 (#) No-Blocking mode private API's with Interrupt are :
912 (+) SMARTCARD_Transmit_IT()
913 (+) SMARTCARD_Receive_IT()
914 (+) SMARTCARD_WaitOnFlagUntilTimeout()
916 (#) No-Blocking mode private functions with DMA are :
917 (+) SMARTCARD_DMATransmitCplt()
918 (+) SMARTCARD_DMAReceiveCplt()
919 (+) SMARTCARD_DMAError()
926 * @brief Send an amount of data in non blocking mode
927 * @param hsmartcard: SMARTCARD handle.
928 * Function called under interruption only, once
929 * interruptions have been enabled by HAL_SMARTCARD_Transmit_IT()
932 static HAL_StatusTypeDef SMARTCARD_Transmit_IT(SMARTCARD_HandleTypeDef *hsmartcard)
934 if ((hsmartcard->State == HAL_SMARTCARD_STATE_BUSY_TX) || (hsmartcard->State == HAL_SMARTCARD_STATE_BUSY_TX_RX))
937 if(hsmartcard->TxXferCount == 0)
939 /* Disable the SMARTCARD Transmit Data Register Empty Interrupt */
940 __HAL_SMARTCARD_DISABLE_IT(hsmartcard, SMARTCARD_IT_TXE);
942 /* Enable the SMARTCARD Transmit Complete Interrupt */
943 __HAL_SMARTCARD_ENABLE_IT(hsmartcard, SMARTCARD_IT_TC);
949 hsmartcard->Instance->TDR = (*hsmartcard->pTxBuffPtr++ & (uint8_t)0xFF);
950 hsmartcard->TxXferCount--;
963 * @brief Wraps up transmission in non blocking mode.
964 * @param hsmartcard: pointer to a SMARTCARD_HandleTypeDef structure that contains
965 * the configuration information for the specified SMARTCARD module.
968 static HAL_StatusTypeDef SMARTCARD_EndTransmit_IT(SMARTCARD_HandleTypeDef *hsmartcard)
970 /* Disable the SMARTCARD Transmit Complete Interrupt */
971 __HAL_SMARTCARD_DISABLE_IT(hsmartcard, SMARTCARD_IT_TC);
973 /* Check if a receive process is ongoing or not */
974 if(hsmartcard->State == HAL_SMARTCARD_STATE_BUSY_TX_RX)
976 hsmartcard->State = HAL_SMARTCARD_STATE_BUSY_RX;
980 /* Disable the SMARTCARD Error Interrupt: (Frame error, noise error, overrun error) */
981 __HAL_SMARTCARD_DISABLE_IT(hsmartcard, SMARTCARD_IT_ERR);
983 hsmartcard->State = HAL_SMARTCARD_STATE_READY;
986 HAL_SMARTCARD_TxCpltCallback(hsmartcard);
993 * @brief Receive an amount of data in non blocking mode
994 * @param hsmartcard: SMARTCARD handle.
995 * Function called under interruption only, once
996 * interruptions have been enabled by HAL_SMARTCARD_Receive_IT()
999 static HAL_StatusTypeDef SMARTCARD_Receive_IT(SMARTCARD_HandleTypeDef *hsmartcard)
1001 if ((hsmartcard->State == HAL_SMARTCARD_STATE_BUSY_RX) || (hsmartcard->State == HAL_SMARTCARD_STATE_BUSY_TX_RX))
1004 *hsmartcard->pRxBuffPtr++ = (uint8_t)(hsmartcard->Instance->RDR & (uint8_t)0xFF);
1006 if(--hsmartcard->RxXferCount == 0)
1008 __HAL_SMARTCARD_DISABLE_IT(hsmartcard, SMARTCARD_IT_RXNE);
1010 /* Check if a transmit Process is ongoing or not */
1011 if(hsmartcard->State == HAL_SMARTCARD_STATE_BUSY_TX_RX)
1013 hsmartcard->State = HAL_SMARTCARD_STATE_BUSY_TX;
1017 /* Disable the SMARTCARD Parity Error Interrupt */
1018 __HAL_SMARTCARD_DISABLE_IT(hsmartcard, SMARTCARD_IT_PE);
1020 /* Disable the SMARTCARD Error Interrupt: (Frame error, noise error, overrun error) */
1021 __HAL_SMARTCARD_DISABLE_IT(hsmartcard, SMARTCARD_IT_ERR);
1023 hsmartcard->State = HAL_SMARTCARD_STATE_READY;
1026 HAL_SMARTCARD_RxCpltCallback(hsmartcard);
1040 * @brief This function handles SMARTCARD Communication Timeout.
1041 * @param hsmartcard: SMARTCARD handle
1042 * @param Flag: specifies the SMARTCARD flag to check.
1043 * @param Status: The new Flag status (SET or RESET).
1044 * @param Timeout: Timeout duration
1045 * @retval HAL status
1047 static HAL_StatusTypeDef SMARTCARD_WaitOnFlagUntilTimeout(SMARTCARD_HandleTypeDef *hsmartcard, uint32_t Flag, FlagStatus Status, uint32_t Timeout)
1049 uint32_t tickstart = HAL_GetTick();
1051 /* Wait until flag is set */
1054 while(__HAL_SMARTCARD_GET_FLAG(hsmartcard, Flag) == RESET)
1056 /* Check for the Timeout */
1057 if(Timeout != HAL_MAX_DELAY)
1059 if((Timeout == 0) || ((HAL_GetTick()-tickstart) > Timeout))
1061 /* Disable TXE, RXNE, PE and ERR (Frame error, noise error, overrun error) interrupts for the interrupt process */
1062 __HAL_SMARTCARD_DISABLE_IT(hsmartcard, SMARTCARD_IT_TXE);
1063 __HAL_SMARTCARD_DISABLE_IT(hsmartcard, SMARTCARD_IT_RXNE);
1064 __HAL_SMARTCARD_DISABLE_IT(hsmartcard, SMARTCARD_IT_PE);
1065 __HAL_SMARTCARD_DISABLE_IT(hsmartcard, SMARTCARD_IT_ERR);
1067 hsmartcard->State= HAL_SMARTCARD_STATE_TIMEOUT;
1069 /* Process Unlocked */
1070 __HAL_UNLOCK(hsmartcard);
1079 while(__HAL_SMARTCARD_GET_FLAG(hsmartcard, Flag) != RESET)
1081 /* Check for the Timeout */
1082 if(Timeout != HAL_MAX_DELAY)
1084 if((Timeout == 0) || ((HAL_GetTick()-tickstart) > Timeout))
1086 /* Disable TXE, RXNE, PE and ERR (Frame error, noise error, overrun error) interrupts for the interrupt process */
1087 __HAL_SMARTCARD_DISABLE_IT(hsmartcard, SMARTCARD_IT_TXE);
1088 __HAL_SMARTCARD_DISABLE_IT(hsmartcard, SMARTCARD_IT_RXNE);
1089 __HAL_SMARTCARD_DISABLE_IT(hsmartcard, SMARTCARD_IT_PE);
1090 __HAL_SMARTCARD_DISABLE_IT(hsmartcard, SMARTCARD_IT_ERR);
1092 hsmartcard->State= HAL_SMARTCARD_STATE_TIMEOUT;
1094 /* Process Unlocked */
1095 __HAL_UNLOCK(hsmartcard);
1107 * @brief DMA SMARTCARD transmit process complete callback
1108 * @param hdma: DMA handle
1111 static void SMARTCARD_DMATransmitCplt(DMA_HandleTypeDef *hdma)
1113 SMARTCARD_HandleTypeDef* hsmartcard = ( SMARTCARD_HandleTypeDef* )((DMA_HandleTypeDef* )hdma)->Parent;
1114 hsmartcard->TxXferCount = 0;
1116 /* Disable the DMA transfer for transmit request by setting the DMAT bit
1117 in the SMARTCARD associated USART CR3 register */
1118 hsmartcard->Instance->CR3 &= (uint16_t)~((uint16_t)USART_CR3_DMAT);
1120 /* Wait for SMARTCARD TC Flag */
1121 if(SMARTCARD_WaitOnFlagUntilTimeout(hsmartcard, SMARTCARD_FLAG_TC, RESET, SMARTCARD_TXDMA_TIMEOUTVALUE) != HAL_OK)
1123 /* Timeout Occured */
1124 HAL_SMARTCARD_ErrorCallback(hsmartcard);
1129 /* Check if a receive Process is ongoing or not */
1130 if(hsmartcard->State == HAL_SMARTCARD_STATE_BUSY_TX_RX)
1132 hsmartcard->State = HAL_SMARTCARD_STATE_BUSY_RX;
1136 hsmartcard->State = HAL_SMARTCARD_STATE_READY;
1138 HAL_SMARTCARD_TxCpltCallback(hsmartcard);
1143 * @brief DMA SMARTCARD receive process complete callback
1144 * @param hdma: DMA handle
1147 static void SMARTCARD_DMAReceiveCplt(DMA_HandleTypeDef *hdma)
1149 SMARTCARD_HandleTypeDef* hsmartcard = ( SMARTCARD_HandleTypeDef* )((DMA_HandleTypeDef* )hdma)->Parent;
1150 hsmartcard->RxXferCount = 0;
1152 /* Disable the DMA transfer for the receiver request by setting the DMAR bit
1153 in the SMARTCARD associated USART CR3 register */
1154 hsmartcard->Instance->CR3 &= (uint16_t)~((uint16_t)USART_CR3_DMAR);
1156 /* Check if a transmit Process is ongoing or not */
1157 if(hsmartcard->State == HAL_SMARTCARD_STATE_BUSY_TX_RX)
1159 hsmartcard->State = HAL_SMARTCARD_STATE_BUSY_TX;
1163 hsmartcard->State = HAL_SMARTCARD_STATE_READY;
1166 HAL_SMARTCARD_RxCpltCallback(hsmartcard);
1170 * @brief DMA SMARTCARD communication error callback
1171 * @param hdma: DMA handle
1174 static void SMARTCARD_DMAError(DMA_HandleTypeDef *hdma)
1176 SMARTCARD_HandleTypeDef* hsmartcard = ( SMARTCARD_HandleTypeDef* )((DMA_HandleTypeDef* )hdma)->Parent;
1177 hsmartcard->RxXferCount = 0;
1178 hsmartcard->TxXferCount = 0;
1179 hsmartcard->State= HAL_SMARTCARD_STATE_READY;
1180 hsmartcard->ErrorCode |= HAL_SMARTCARD_ERROR_DMA;
1181 HAL_SMARTCARD_ErrorCallback(hsmartcard);
1188 /** @defgroup SMARTCARD_Private_Functions_Group3 Peripheral Control private functions
1189 * @brief SMARTCARD control private functions
1191 ===============================================================================
1192 ##### Peripheral Control private functions #####
1193 ===============================================================================
1195 This subsection provides a set of private functions allowing to initialize the SMARTCARD.
1196 (+) SMARTCARD_SetConfig() API configures the SMARTCARD peripheral
1197 (+) SMARTCARD_AdvFeatureConfig() API optionally configures the SMARTCARD advanced features
1198 (+) SMARTCARD_CheckIdleState() API ensures that TEACK and/or REACK are set after initialization
1206 * @brief Configure the SMARTCARD associated USART peripheral
1207 * @param hsmartcard: SMARTCARD handle
1210 static HAL_StatusTypeDef SMARTCARD_SetConfig(SMARTCARD_HandleTypeDef *hsmartcard)
1212 uint32_t tmpreg = 0x00000000;
1213 SMARTCARD_ClockSourceTypeDef clocksource = SMARTCARD_CLOCKSOURCE_UNDEFINED;
1214 HAL_StatusTypeDef ret = HAL_OK;
1216 /* Check the parameters */
1217 assert_param(IS_SMARTCARD_INSTANCE(hsmartcard->Instance));
1218 assert_param(IS_SMARTCARD_BAUDRATE(hsmartcard->Init.BaudRate));
1219 assert_param(IS_SMARTCARD_WORD_LENGTH(hsmartcard->Init.WordLength));
1220 assert_param(IS_SMARTCARD_STOPBITS(hsmartcard->Init.StopBits));
1221 assert_param(IS_SMARTCARD_PARITY(hsmartcard->Init.Parity));
1222 assert_param(IS_SMARTCARD_MODE(hsmartcard->Init.Mode));
1223 assert_param(IS_SMARTCARD_POLARITY(hsmartcard->Init.CLKPolarity));
1224 assert_param(IS_SMARTCARD_PHASE(hsmartcard->Init.CLKPhase));
1225 assert_param(IS_SMARTCARD_LASTBIT(hsmartcard->Init.CLKLastBit));
1226 assert_param(IS_SMARTCARD_ONEBIT_SAMPLING(hsmartcard->Init.OneBitSampling));
1227 assert_param(IS_SMARTCARD_NACK(hsmartcard->Init.NACKEnable));
1228 assert_param(IS_SMARTCARD_TIMEOUT(hsmartcard->Init.TimeOutEnable));
1229 assert_param(IS_SMARTCARD_AUTORETRY_COUNT(hsmartcard->Init.AutoRetryCount));
1231 /*-------------------------- USART CR1 Configuration -----------------------*/
1232 /* In SmartCard mode, M and PCE are forced to 1 (8 bits + parity).
1233 * Oversampling is forced to 16 (OVER8 = 0).
1234 * Configure the Parity and Mode:
1235 * set PS bit according to hsmartcard->Init.Parity value
1236 * set TE and RE bits according to hsmartcard->Init.Mode value */
1237 tmpreg = (uint32_t) hsmartcard->Init.Parity | hsmartcard->Init.Mode;
1238 /* in case of TX-only mode, if NACK is enabled, the USART must be able to monitor
1239 the bidirectional line to detect a NACK signal in case of parity error.
1240 Therefore, the receiver block must be enabled as well (RE bit must be set). */
1241 if ((hsmartcard->Init.Mode == SMARTCARD_MODE_TX)
1242 && (hsmartcard->Init.NACKEnable == SMARTCARD_NACK_ENABLED))
1244 tmpreg |= USART_CR1_RE;
1246 tmpreg |= (uint32_t) hsmartcard->Init.WordLength;
1247 MODIFY_REG(hsmartcard->Instance->CR1, USART_CR1_FIELDS, tmpreg);
1249 /*-------------------------- USART CR2 Configuration -----------------------*/
1250 /* Stop bits are forced to 1.5 (STOP = 11) */
1251 tmpreg = hsmartcard->Init.StopBits;
1252 /* Synchronous mode is activated by default */
1253 tmpreg |= (uint32_t) USART_CR2_CLKEN | hsmartcard->Init.CLKPolarity;
1254 tmpreg |= (uint32_t) hsmartcard->Init.CLKPhase | hsmartcard->Init.CLKLastBit;
1255 tmpreg |= (uint32_t) hsmartcard->Init.TimeOutEnable;
1256 MODIFY_REG(hsmartcard->Instance->CR2, USART_CR2_FIELDS, tmpreg);
1258 /*-------------------------- USART CR3 Configuration -----------------------*/
1260 * - one-bit sampling method versus three samples' majority rule
1261 * according to hsmartcard->Init.OneBitSampling
1262 * - NACK transmission in case of parity error according
1263 * to hsmartcard->Init.NACKEnable
1264 * - autoretry counter according to hsmartcard->Init.AutoRetryCount */
1265 tmpreg = (uint32_t) hsmartcard->Init.OneBitSampling | hsmartcard->Init.NACKEnable;
1266 tmpreg |= ((uint32_t)hsmartcard->Init.AutoRetryCount << SMARTCARD_CR3_SCARCNT_LSB_POS);
1267 MODIFY_REG(hsmartcard->Instance-> CR3,USART_CR3_FIELDS, tmpreg);
1269 /*-------------------------- USART GTPR Configuration ----------------------*/
1270 tmpreg = (hsmartcard->Init.Prescaler | ((uint32_t)hsmartcard->Init.GuardTime << SMARTCARD_GTPR_GT_LSB_POS));
1271 MODIFY_REG(hsmartcard->Instance->GTPR, (USART_GTPR_GT|USART_GTPR_PSC), tmpreg);
1273 /*-------------------------- USART RTOR Configuration ----------------------*/
1274 tmpreg = ((uint32_t)hsmartcard->Init.BlockLength << SMARTCARD_RTOR_BLEN_LSB_POS);
1275 if (hsmartcard->Init.TimeOutEnable == SMARTCARD_TIMEOUT_ENABLED)
1277 assert_param(IS_SMARTCARD_TIMEOUT_VALUE(hsmartcard->Init.TimeOutValue));
1278 tmpreg |= (uint32_t) hsmartcard->Init.TimeOutValue;
1280 MODIFY_REG(hsmartcard->Instance->RTOR, (USART_RTOR_RTO|USART_RTOR_BLEN), tmpreg);
1282 /*-------------------------- USART BRR Configuration -----------------------*/
1283 __HAL_SMARTCARD_GETCLOCKSOURCE(hsmartcard, clocksource);
1284 switch (clocksource)
1286 case SMARTCARD_CLOCKSOURCE_PCLK1:
1287 hsmartcard->Instance->BRR = (uint16_t)(HAL_RCC_GetPCLK1Freq() / hsmartcard->Init.BaudRate);
1289 case SMARTCARD_CLOCKSOURCE_PCLK2:
1290 hsmartcard->Instance->BRR = (uint16_t)(HAL_RCC_GetPCLK2Freq() / hsmartcard->Init.BaudRate);
1292 case SMARTCARD_CLOCKSOURCE_HSI:
1293 hsmartcard->Instance->BRR = (uint16_t)(HSI_VALUE / hsmartcard->Init.BaudRate);
1295 case SMARTCARD_CLOCKSOURCE_SYSCLK:
1296 hsmartcard->Instance->BRR = (uint16_t)(HAL_RCC_GetSysClockFreq() / hsmartcard->Init.BaudRate);
1298 case SMARTCARD_CLOCKSOURCE_LSE:
1299 hsmartcard->Instance->BRR = (uint16_t)(LSE_VALUE / hsmartcard->Init.BaudRate);
1301 case SMARTCARD_CLOCKSOURCE_UNDEFINED:
1312 * @brief Configure the SMARTCARD associated USART peripheral advanced feautures
1313 * @param hsmartcard: SMARTCARD handle
1316 static void SMARTCARD_AdvFeatureConfig(SMARTCARD_HandleTypeDef *hsmartcard)
1318 /* Check whether the set of advanced features to configure is properly set */
1319 assert_param(IS_SMARTCARD_ADVFEATURE_INIT(hsmartcard->AdvancedInit.AdvFeatureInit));
1321 /* if required, configure TX pin active level inversion */
1322 if (HAL_IS_BIT_SET(hsmartcard->AdvancedInit.AdvFeatureInit, SMARTCARD_ADVFEATURE_TXINVERT_INIT))
1324 assert_param(IS_SMARTCARD_ADVFEATURE_TXINV(hsmartcard->AdvancedInit.TxPinLevelInvert));
1325 MODIFY_REG(hsmartcard->Instance->CR2, USART_CR2_TXINV, hsmartcard->AdvancedInit.TxPinLevelInvert);
1328 /* if required, configure RX pin active level inversion */
1329 if (HAL_IS_BIT_SET(hsmartcard->AdvancedInit.AdvFeatureInit, SMARTCARD_ADVFEATURE_RXINVERT_INIT))
1331 assert_param(IS_SMARTCARD_ADVFEATURE_RXINV(hsmartcard->AdvancedInit.RxPinLevelInvert));
1332 MODIFY_REG(hsmartcard->Instance->CR2, USART_CR2_RXINV, hsmartcard->AdvancedInit.RxPinLevelInvert);
1335 /* if required, configure data inversion */
1336 if (HAL_IS_BIT_SET(hsmartcard->AdvancedInit.AdvFeatureInit, SMARTCARD_ADVFEATURE_DATAINVERT_INIT))
1338 assert_param(IS_SMARTCARD_ADVFEATURE_DATAINV(hsmartcard->AdvancedInit.DataInvert));
1339 MODIFY_REG(hsmartcard->Instance->CR2, USART_CR2_DATAINV, hsmartcard->AdvancedInit.DataInvert);
1342 /* if required, configure RX/TX pins swap */
1343 if (HAL_IS_BIT_SET(hsmartcard->AdvancedInit.AdvFeatureInit, SMARTCARD_ADVFEATURE_SWAP_INIT))
1345 assert_param(IS_SMARTCARD_ADVFEATURE_SWAP(hsmartcard->AdvancedInit.Swap));
1346 MODIFY_REG(hsmartcard->Instance->CR2, USART_CR2_SWAP, hsmartcard->AdvancedInit.Swap);
1349 /* if required, configure RX overrun detection disabling */
1350 if (HAL_IS_BIT_SET(hsmartcard->AdvancedInit.AdvFeatureInit, SMARTCARD_ADVFEATURE_RXOVERRUNDISABLE_INIT))
1352 assert_param(IS_SMARTCARD_OVERRUN(hsmartcard->AdvancedInit.OverrunDisable));
1353 MODIFY_REG(hsmartcard->Instance->CR3, USART_CR3_OVRDIS, hsmartcard->AdvancedInit.OverrunDisable);
1356 /* if required, configure DMA disabling on reception error */
1357 if (HAL_IS_BIT_SET(hsmartcard->AdvancedInit.AdvFeatureInit, SMARTCARD_ADVFEATURE_DMADISABLEONERROR_INIT))
1359 assert_param(IS_SMARTCARD_ADVFEATURE_DMAONRXERROR(hsmartcard->AdvancedInit.DMADisableonRxError));
1360 MODIFY_REG(hsmartcard->Instance->CR3, USART_CR3_DDRE, hsmartcard->AdvancedInit.DMADisableonRxError);
1363 /* if required, configure MSB first on communication line */
1364 if (HAL_IS_BIT_SET(hsmartcard->AdvancedInit.AdvFeatureInit, SMARTCARD_ADVFEATURE_MSBFIRST_INIT))
1366 assert_param(IS_SMARTCARD_ADVFEATURE_MSBFIRST(hsmartcard->AdvancedInit.MSBFirst));
1367 MODIFY_REG(hsmartcard->Instance->CR2, USART_CR2_MSBFIRST, hsmartcard->AdvancedInit.MSBFirst);
1373 * @brief Check the SMARTCARD Idle State
1374 * @param hsmartcard: SMARTCARD handle
1375 * @retval HAL status
1377 static HAL_StatusTypeDef SMARTCARD_CheckIdleState(SMARTCARD_HandleTypeDef *hsmartcard)
1380 /* Initialize the SMARTCARD ErrorCode */
1381 hsmartcard->ErrorCode = HAL_SMARTCARD_ERROR_NONE;
1383 /* Check if the Transmitter is enabled */
1384 if((hsmartcard->Instance->CR1 & USART_CR1_TE) == USART_CR1_TE)
1386 /* Wait until TEACK flag is set */
1387 if(SMARTCARD_WaitOnFlagUntilTimeout(hsmartcard, USART_ISR_TEACK, RESET, TEACK_REACK_TIMEOUT) != HAL_OK)
1392 /* Check if the Receiver is enabled */
1393 if((hsmartcard->Instance->CR1 & USART_CR1_RE) == USART_CR1_RE)
1395 /* Wait until REACK flag is set */
1396 if(SMARTCARD_WaitOnFlagUntilTimeout(hsmartcard, USART_ISR_REACK, RESET, TEACK_REACK_TIMEOUT) != HAL_OK)
1402 /* Initialize the SMARTCARD state*/
1403 hsmartcard->State= HAL_SMARTCARD_STATE_READY;
1405 /* Process Unlocked */
1406 __HAL_UNLOCK(hsmartcard);
1419 #endif /* HAL_SMARTCARD_MODULE_ENABLED */
1428 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/