2 ******************************************************************************
\r
3 * @file stm32f7xx_hal_smartcard.c
\r
4 * @author MCD Application Team
\r
6 * @date 06-March-2015
\r
7 * @brief SMARTCARD HAL module driver.
\r
8 * This file provides firmware functions to manage the following
\r
9 * functionalities of the SMARTCARD peripheral:
\r
10 * + Initialization and de-initialization functions
\r
11 * + IO operation functions
\r
12 * + Peripheral State and Errors functions
\r
15 ==============================================================================
\r
16 ##### How to use this driver #####
\r
17 ==============================================================================
\r
19 The SMARTCARD HAL driver can be used as follow:
\r
21 (#) Declare a SMARTCARD_HandleTypeDef handle structure.
\r
22 (#) Associate a USART to the SMARTCARD handle hsc.
\r
23 (#) Initialize the SMARTCARD low level resources by implementing the HAL_SMARTCARD_MspInit() API:
\r
24 (##) Enable the USARTx interface clock.
\r
25 (##) SMARTCARD pins configuration:
\r
26 (+++) Enable the clock for the SMARTCARD GPIOs.
\r
27 (+++) Configure these SMARTCARD pins as alternate function pull-up.
\r
28 (##) NVIC configuration if you need to use interrupt process (HAL_SMARTCARD_Transmit_IT()
\r
29 and HAL_SMARTCARD_Receive_IT() APIs):
\r
30 (+++) Configure the USARTx interrupt priority.
\r
31 (+++) Enable the NVIC USART IRQ handle.
\r
32 (@) The specific USART interrupts (Transmission complete interrupt,
\r
33 RXNE interrupt and Error Interrupts) will be managed using the macros
\r
34 __HAL_SMARTCARD_ENABLE_IT() and __HAL_SMARTCARD_DISABLE_IT() inside the transmit and receive process.
\r
35 (##) DMA Configuration if you need to use DMA process (HAL_SMARTCARD_Transmit_DMA()
\r
36 and HAL_SMARTCARD_Receive_DMA() APIs):
\r
37 (+++) Declare a DMA handle structure for the Tx/Rx stream.
\r
38 (+++) Enable the DMAx interface clock.
\r
39 (+++) Configure the declared DMA handle structure with the required Tx/Rx parameters.
\r
40 (+++) Configure the DMA Tx/Rx Stream.
\r
41 (+++) Associate the initialized DMA handle to the SMARTCARD DMA Tx/Rx handle.
\r
42 (+++) Configure the priority and enable the NVIC for the transfer complete interrupt on the DMA Tx/Rx Stream.
\r
44 (#) Program the Baud Rate, Parity, Mode(Receiver/Transmitter), clock enabling/disabling and accordingly,
\r
45 the clock parameters (parity, phase, last bit), prescaler value, guard time and NACK on transmission
\r
46 error enabling or disabling in the hsc Init structure.
\r
48 (#) If required, program SMARTCARD advanced features (TX/RX pins swap, TimeOut, auto-retry counter,...)
\r
49 in the hsc AdvancedInit structure.
\r
51 (#) Initialize the SMARTCARD associated USART registers by calling
\r
52 the HAL_SMARTCARD_Init() API.
\r
54 (@) HAL_SMARTCARD_Init() API also configure also the low level Hardware GPIO, CLOCK, CORTEX...etc) by
\r
55 calling the customized HAL_SMARTCARD_MspInit() API.
\r
58 ******************************************************************************
\r
61 * <h2><center>© COPYRIGHT(c) 2015 STMicroelectronics</center></h2>
\r
63 * Redistribution and use in source and binary forms, with or without modification,
\r
64 * are permitted provided that the following conditions are met:
\r
65 * 1. Redistributions of source code must retain the above copyright notice,
\r
66 * this list of conditions and the following disclaimer.
\r
67 * 2. Redistributions in binary form must reproduce the above copyright notice,
\r
68 * this list of conditions and the following disclaimer in the documentation
\r
69 * and/or other materials provided with the distribution.
\r
70 * 3. Neither the name of STMicroelectronics nor the names of its contributors
\r
71 * may be used to endorse or promote products derived from this software
\r
72 * without specific prior written permission.
\r
74 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
\r
75 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
\r
76 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
\r
77 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
\r
78 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
\r
79 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
\r
80 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
\r
81 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
\r
82 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
\r
83 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
\r
85 ******************************************************************************
\r
88 /* Includes ------------------------------------------------------------------*/
\r
89 #include "stm32f7xx_hal.h"
\r
91 /** @addtogroup STM32F7xx_HAL_Driver
\r
95 /** @defgroup SMARTCARD SMARTCARD
\r
96 * @brief HAL USART SMARTCARD module driver
\r
99 #ifdef HAL_SMARTCARD_MODULE_ENABLED
\r
100 /* Private typedef -----------------------------------------------------------*/
\r
101 /* Private define ------------------------------------------------------------*/
\r
102 /** @defgroup SMARTCARD_Private_Constants SMARTCARD Private Constants
\r
105 #define TEACK_REACK_TIMEOUT 1000
\r
106 #define HAL_SMARTCARD_TXDMA_TIMEOUTVALUE 22000
\r
107 #define USART_CR1_FIELDS ((uint32_t)(USART_CR1_M | USART_CR1_PCE | USART_CR1_PS | \
\r
108 USART_CR1_TE | USART_CR1_RE | USART_CR1_OVER8))
\r
109 #define USART_CR2_CLK_FIELDS ((uint32_t)(USART_CR2_CLKEN|USART_CR2_CPOL|USART_CR2_CPHA|USART_CR2_LBCL))
\r
110 #define USART_CR2_FIELDS ((uint32_t)(USART_CR2_RTOEN|USART_CR2_CLK_FIELDS|USART_CR2_STOP))
\r
111 #define USART_CR3_FIELDS ((uint32_t)(USART_CR3_ONEBIT|USART_CR3_NACK|USART_CR3_SCARCNT))
\r
115 /* Private macros -------------------------------------------------------------*/
\r
116 /* Private variables ---------------------------------------------------------*/
\r
117 /* Private function prototypes -----------------------------------------------*/
\r
118 /** @addtogroup SMARTCARD_Private_Functions
\r
121 static void SMARTCARD_DMATransmitCplt(DMA_HandleTypeDef *hdma);
\r
122 static void SMARTCARD_DMAReceiveCplt(DMA_HandleTypeDef *hdma);
\r
123 static void SMARTCARD_DMAError(DMA_HandleTypeDef *hdma);
\r
124 static void SMARTCARD_SetConfig(SMARTCARD_HandleTypeDef *hsc);
\r
125 static HAL_StatusTypeDef SMARTCARD_WaitOnFlagUntilTimeout(SMARTCARD_HandleTypeDef *hsc, uint32_t Flag, FlagStatus Status, uint32_t Timeout);
\r
126 static HAL_StatusTypeDef SMARTCARD_CheckIdleState(SMARTCARD_HandleTypeDef *hsc);
\r
127 static HAL_StatusTypeDef SMARTCARD_Transmit_IT(SMARTCARD_HandleTypeDef *hsc);
\r
128 static HAL_StatusTypeDef SMARTCARD_Receive_IT(SMARTCARD_HandleTypeDef *hsc);
\r
129 static void SMARTCARD_AdvFeatureConfig(SMARTCARD_HandleTypeDef *hsc);
\r
133 /* Exported functions --------------------------------------------------------*/
\r
134 /** @defgroup SMARTCARD_Exported_Functions SMARTCARD Exported Functions
\r
138 /** @defgroup SMARTCARD_Exported_Functions_Group1 SmartCard Initialization and de-initialization functions
\r
139 * @brief Initialization and Configuration functions
\r
142 ===============================================================================
\r
143 ##### Initialization and Configuration functions #####
\r
144 ===============================================================================
\r
146 This subsection provides a set of functions allowing to initialize the USART
\r
147 associated to the SmartCard.
\r
148 (+) These parameters can be configured:
\r
150 (++) Parity: parity should be enabled,
\r
151 Frame Length is fixed to 8 bits plus parity:
\r
152 the USART frame format is given in the following table:
\r
153 +---------------------------------------------------------------+
\r
154 | M1M0 bits | PCE bit | USART frame |
\r
155 |-----------------------|---------------------------------------|
\r
156 | 01 | 1 | | SB | 8 bit data | PB | STB | |
\r
157 +---------------------------------------------------------------+
\r
158 (++) Receiver/transmitter modes
\r
159 (++) Synchronous mode (and if enabled, phase, polarity and last bit parameters)
\r
160 (++) Prescaler value
\r
161 (++) Guard bit time
\r
162 (++) NACK enabling or disabling on transmission error
\r
164 (+) The following advanced features can be configured as well:
\r
165 (++) TX and/or RX pin level inversion
\r
166 (++) data logical level inversion
\r
167 (++) RX and TX pins swap
\r
168 (++) RX overrun detection disabling
\r
169 (++) DMA disabling on RX error
\r
170 (++) MSB first on communication line
\r
171 (++) Time out enabling (and if activated, timeout value)
\r
173 (++) Auto-retry counter
\r
176 The HAL_SMARTCARD_Init() API follow respectively the USART (a)synchronous configuration procedures
\r
177 (details for the procedures are available in reference manual).
\r
184 * @brief Initializes the SMARTCARD mode according to the specified
\r
185 * parameters in the SMARTCARD_InitTypeDef and create the associated handle .
\r
186 * @param hsc: SMARTCARD handle
\r
187 * @retval HAL status
\r
189 HAL_StatusTypeDef HAL_SMARTCARD_Init(SMARTCARD_HandleTypeDef *hsc)
\r
191 /* Check the SMARTCARD handle allocation */
\r
197 /* Check the parameters */
\r
198 assert_param(IS_SMARTCARD_INSTANCE(hsc->Instance));
\r
200 if(hsc->State == HAL_SMARTCARD_STATE_RESET)
\r
202 /* Init the low level hardware : GPIO, CLOCK, CORTEX */
\r
203 HAL_SMARTCARD_MspInit(hsc);
\r
206 hsc->State = HAL_SMARTCARD_STATE_BUSY;
\r
208 /* Disable the Peripheral */
\r
209 __HAL_SMARTCARD_DISABLE(hsc);
\r
211 /* Set the SMARTCARD Communication parameters */
\r
212 SMARTCARD_SetConfig(hsc);
\r
214 if(hsc->AdvancedInit.AdvFeatureInit != SMARTCARD_ADVFEATURE_NO_INIT)
\r
216 SMARTCARD_AdvFeatureConfig(hsc);
\r
219 /* In SmartCard mode, the following bits must be kept cleared:
\r
220 - LINEN in the USART_CR2 register,
\r
221 - HDSEL and IREN bits in the USART_CR3 register.*/
\r
222 hsc->Instance->CR2 &= ~(USART_CR2_LINEN);
\r
223 hsc->Instance->CR3 &= ~(USART_CR3_HDSEL | USART_CR3_IREN);
\r
225 /* set the USART in SMARTCARD mode */
\r
226 hsc->Instance->CR3 |= USART_CR3_SCEN;
\r
228 /* Enable the Peripheral */
\r
229 __HAL_SMARTCARD_ENABLE(hsc);
\r
231 /* TEACK and/or REACK to check before moving hsc->State to Ready */
\r
232 return (SMARTCARD_CheckIdleState(hsc));
\r
236 * @brief DeInitializes the SMARTCARD peripheral
\r
237 * @param hsc: SMARTCARD handle
\r
238 * @retval HAL status
\r
240 HAL_StatusTypeDef HAL_SMARTCARD_DeInit(SMARTCARD_HandleTypeDef *hsc)
\r
242 /* Check the SMARTCARD handle allocation */
\r
248 /* Check the parameters */
\r
249 assert_param(IS_SMARTCARD_INSTANCE(hsc->Instance));
\r
251 hsc->State = HAL_SMARTCARD_STATE_BUSY;
\r
253 /* DeInit the low level hardware */
\r
254 HAL_SMARTCARD_MspDeInit(hsc);
\r
256 hsc->ErrorCode = HAL_SMARTCARD_ERROR_NONE;
\r
257 hsc->State = HAL_SMARTCARD_STATE_RESET;
\r
266 * @brief SMARTCARD MSP Init
\r
267 * @param hsc: SMARTCARD handle
\r
270 __weak void HAL_SMARTCARD_MspInit(SMARTCARD_HandleTypeDef *hsc)
\r
272 /* NOTE : This function Should not be modified, when the callback is needed,
\r
273 the HAL_SMARTCARD_MspInit could be implemented in the user file
\r
278 * @brief SMARTCARD MSP DeInit
\r
279 * @param hsc: SMARTCARD handle
\r
282 __weak void HAL_SMARTCARD_MspDeInit(SMARTCARD_HandleTypeDef *hsc)
\r
284 /* NOTE : This function Should not be modified, when the callback is needed,
\r
285 the HAL_SMARTCARD_MspDeInit could be implemented in the user file
\r
293 /** @defgroup SMARTCARD_Exported_Functions_Group2 IO operation functions
\r
294 * @brief SMARTCARD Transmit and Receive functions
\r
297 ===============================================================================
\r
298 ##### IO operation functions #####
\r
299 ===============================================================================
\r
300 This subsection provides a set of functions allowing to manage the SMARTCARD data transfers.
\r
302 (#) There are two modes of transfer:
\r
303 (+) Blocking mode: The communication is performed in polling mode.
\r
304 The HAL status of all data processing is returned by the same function
\r
305 after finishing transfer.
\r
306 (+) No-Blocking mode: The communication is performed using Interrupts
\r
307 or DMA, These API's return the HAL status.
\r
308 The end of the data processing will be indicated through the
\r
309 dedicated SMARTCARD IRQ when using Interrupt mode or the DMA IRQ when
\r
311 The HAL_SMARTCARD_TxCpltCallback(), HAL_SMARTCARD_RxCpltCallback() user callbacks
\r
312 will be executed respectively at the end of the Transmit or Receive process
\r
313 The HAL_SMARTCARD_ErrorCallback()user callback will be executed when a communication error is detected
\r
315 (#) Blocking mode API's are :
\r
316 (+) HAL_SMARTCARD_Transmit()
\r
317 (+) HAL_SMARTCARD_Receive()
\r
319 (#) Non-Blocking mode API's with Interrupt are :
\r
320 (+) HAL_SMARTCARD_Transmit_IT()
\r
321 (+) HAL_SMARTCARD_Receive_IT()
\r
322 (+) HAL_SMARTCARD_IRQHandler()
\r
323 (+) SMARTCARD_Transmit_IT()
\r
324 (+) SMARTCARD_Receive_IT()
\r
326 (#) No-Blocking mode functions with DMA are :
\r
327 (+) HAL_SMARTCARD_Transmit_DMA()
\r
328 (+) HAL_SMARTCARD_Receive_DMA()
\r
330 (#) A set of Transfer Complete Callbacks are provided in No_Blocking mode:
\r
331 (+) HAL_SMARTCARD_TxCpltCallback()
\r
332 (+) HAL_SMARTCARD_RxCpltCallback()
\r
333 (+) HAL_SMARTCARD_ErrorCallback()
\r
340 * @brief Send an amount of data in blocking mode
\r
341 * @param hsc: SMARTCARD handle
\r
342 * @param pData: pointer to data buffer
\r
343 * @param Size: amount of data to be sent
\r
344 * @param Timeout: Timeout duration
\r
345 * @retval HAL status
\r
347 HAL_StatusTypeDef HAL_SMARTCARD_Transmit(SMARTCARD_HandleTypeDef *hsc, uint8_t *pData, uint16_t Size, uint32_t Timeout)
\r
349 if((hsc->State == HAL_SMARTCARD_STATE_READY) || (hsc->State == HAL_SMARTCARD_STATE_BUSY_RX))
\r
351 if((pData == NULL) || (Size == 0))
\r
356 /* Process Locked */
\r
358 hsc->ErrorCode = HAL_SMARTCARD_ERROR_NONE;
\r
359 /* Check if a non-blocking receive process is ongoing or not */
\r
360 if(hsc->State == HAL_SMARTCARD_STATE_BUSY_RX)
\r
362 hsc->State = HAL_SMARTCARD_STATE_BUSY_TX_RX;
\r
366 hsc->State = HAL_SMARTCARD_STATE_BUSY_TX;
\r
369 hsc->TxXferSize = Size;
\r
370 hsc->TxXferCount = Size;
\r
371 while(hsc->TxXferCount > 0)
\r
373 hsc->TxXferCount--;
\r
374 if(SMARTCARD_WaitOnFlagUntilTimeout(hsc, SMARTCARD_FLAG_TXE, RESET, Timeout) != HAL_OK)
\r
376 return HAL_TIMEOUT;
\r
378 hsc->Instance->TDR = (*pData++ & (uint8_t)0xFF);
\r
380 if(SMARTCARD_WaitOnFlagUntilTimeout(hsc, SMARTCARD_FLAG_TC, RESET, Timeout) != HAL_OK)
\r
382 return HAL_TIMEOUT;
\r
384 /* Check if a non-blocking receive Process is ongoing or not */
\r
385 if(hsc->State == HAL_SMARTCARD_STATE_BUSY_TX_RX)
\r
387 hsc->State = HAL_SMARTCARD_STATE_BUSY_RX;
\r
391 hsc->State = HAL_SMARTCARD_STATE_READY;
\r
393 /* Process Unlocked */
\r
405 * @brief Receive an amount of data in blocking mode
\r
406 * @param hsc: SMARTCARD handle
\r
407 * @param pData: pointer to data buffer
\r
408 * @param Size: amount of data to be received
\r
409 * @param Timeout: Timeout duration
\r
410 * @retval HAL status
\r
412 HAL_StatusTypeDef HAL_SMARTCARD_Receive(SMARTCARD_HandleTypeDef *hsc, uint8_t *pData, uint16_t Size, uint32_t Timeout)
\r
414 if((hsc->State == HAL_SMARTCARD_STATE_READY) || (hsc->State == HAL_SMARTCARD_STATE_BUSY_TX))
\r
416 if((pData == NULL) || (Size == 0))
\r
421 /* Process Locked */
\r
424 hsc->ErrorCode = HAL_SMARTCARD_ERROR_NONE;
\r
425 /* Check if a non-blocking transmit process is ongoing or not */
\r
426 if(hsc->State == HAL_SMARTCARD_STATE_BUSY_TX)
\r
428 hsc->State = HAL_SMARTCARD_STATE_BUSY_TX_RX;
\r
432 hsc->State = HAL_SMARTCARD_STATE_BUSY_RX;
\r
435 hsc->RxXferSize = Size;
\r
436 hsc->RxXferCount = Size;
\r
437 /* Check the remain data to be received */
\r
438 while(hsc->RxXferCount > 0)
\r
440 hsc->RxXferCount--;
\r
441 if(SMARTCARD_WaitOnFlagUntilTimeout(hsc, SMARTCARD_FLAG_RXNE, RESET, Timeout) != HAL_OK)
\r
443 return HAL_TIMEOUT;
\r
445 *pData++ = (uint8_t)(hsc->Instance->RDR & (uint8_t)0x00FF);
\r
448 /* Check if a non-blocking transmit process is ongoing or not */
\r
449 if(hsc->State == HAL_SMARTCARD_STATE_BUSY_TX_RX)
\r
451 hsc->State = HAL_SMARTCARD_STATE_BUSY_TX;
\r
455 hsc->State = HAL_SMARTCARD_STATE_READY;
\r
458 /* Process Unlocked */
\r
470 * @brief Send an amount of data in interrupt mode
\r
471 * @param hsc: SMARTCARD handle
\r
472 * @param pData: pointer to data buffer
\r
473 * @param Size: amount of data to be sent
\r
474 * @retval HAL status
\r
476 HAL_StatusTypeDef HAL_SMARTCARD_Transmit_IT(SMARTCARD_HandleTypeDef *hsc, uint8_t *pData, uint16_t Size)
\r
478 if((hsc->State == HAL_SMARTCARD_STATE_READY) || (hsc->State == HAL_SMARTCARD_STATE_BUSY_RX))
\r
480 if((pData == NULL) || (Size == 0))
\r
485 /* Process Locked */
\r
488 hsc->pTxBuffPtr = pData;
\r
489 hsc->TxXferSize = Size;
\r
490 hsc->TxXferCount = Size;
\r
492 hsc->ErrorCode = HAL_SMARTCARD_ERROR_NONE;
\r
493 /* Check if a receive process is ongoing or not */
\r
494 if(hsc->State == HAL_SMARTCARD_STATE_BUSY_RX)
\r
496 hsc->State = HAL_SMARTCARD_STATE_BUSY_TX_RX;
\r
500 hsc->State = HAL_SMARTCARD_STATE_BUSY_TX;
\r
503 /* Enable the SMARTCARD Error Interrupt: (Frame error, noise error, overrun error) */
\r
504 __HAL_SMARTCARD_ENABLE_IT(hsc, SMARTCARD_IT_ERR);
\r
506 /* Process Unlocked */
\r
509 /* Enable the SMARTCARD Transmit Complete Interrupt */
\r
510 __HAL_SMARTCARD_ENABLE_IT(hsc, SMARTCARD_IT_TC);
\r
521 * @brief Receive an amount of data in interrupt mode
\r
522 * @param hsc: SMARTCARD handle
\r
523 * @param pData: pointer to data buffer
\r
524 * @param Size: amount of data to be received
\r
525 * @retval HAL status
\r
527 HAL_StatusTypeDef HAL_SMARTCARD_Receive_IT(SMARTCARD_HandleTypeDef *hsc, uint8_t *pData, uint16_t Size)
\r
529 if((hsc->State == HAL_SMARTCARD_STATE_READY) || (hsc->State == HAL_SMARTCARD_STATE_BUSY_TX))
\r
531 if((pData == NULL) || (Size == 0))
\r
536 /* Process Locked */
\r
539 hsc->pRxBuffPtr = pData;
\r
540 hsc->RxXferSize = Size;
\r
541 hsc->RxXferCount = Size;
\r
543 hsc->ErrorCode = HAL_SMARTCARD_ERROR_NONE;
\r
544 /* Check if a transmit process is ongoing or not */
\r
545 if(hsc->State == HAL_SMARTCARD_STATE_BUSY_TX)
\r
547 hsc->State = HAL_SMARTCARD_STATE_BUSY_TX_RX;
\r
551 hsc->State = HAL_SMARTCARD_STATE_BUSY_RX;
\r
554 /* Enable the SMARTCARD Parity Error Interrupt */
\r
555 __HAL_SMARTCARD_ENABLE_IT(hsc, SMARTCARD_IT_PE);
\r
557 /* Enable the SMARTCARD Error Interrupt: (Frame error, noise error, overrun error) */
\r
558 __HAL_SMARTCARD_ENABLE_IT(hsc, SMARTCARD_IT_ERR);
\r
560 /* Process Unlocked */
\r
563 /* Enable the SMARTCARD Data Register not empty Interrupt */
\r
564 __HAL_SMARTCARD_ENABLE_IT(hsc, SMARTCARD_IT_RXNE);
\r
575 * @brief Send an amount of data in DMA mode
\r
576 * @param hsc: SMARTCARD handle
\r
577 * @param pData: pointer to data buffer
\r
578 * @param Size: amount of data to be sent
\r
579 * @retval HAL status
\r
581 HAL_StatusTypeDef HAL_SMARTCARD_Transmit_DMA(SMARTCARD_HandleTypeDef *hsc, uint8_t *pData, uint16_t Size)
\r
585 if((hsc->State == HAL_SMARTCARD_STATE_READY) || (hsc->State == HAL_SMARTCARD_STATE_BUSY_RX))
\r
587 if((pData == NULL) || (Size == 0))
\r
592 /* Process Locked */
\r
595 hsc->pTxBuffPtr = pData;
\r
596 hsc->TxXferSize = Size;
\r
597 hsc->TxXferCount = Size;
\r
599 hsc->ErrorCode = HAL_SMARTCARD_ERROR_NONE;
\r
600 /* Check if a receive process is ongoing or not */
\r
601 if(hsc->State == HAL_SMARTCARD_STATE_BUSY_RX)
\r
603 hsc->State = HAL_SMARTCARD_STATE_BUSY_TX_RX;
\r
607 hsc->State = HAL_SMARTCARD_STATE_BUSY_TX;
\r
610 /* Set the SMARTCARD DMA transfer complete callback */
\r
611 hsc->hdmatx->XferCpltCallback = SMARTCARD_DMATransmitCplt;
\r
613 /* Set the SMARTCARD error callback */
\r
614 hsc->hdmatx->XferErrorCallback = SMARTCARD_DMAError;
\r
616 /* Enable the SMARTCARD transmit DMA Stream */
\r
617 tmp = (uint32_t*)&pData;
\r
618 HAL_DMA_Start_IT(hsc->hdmatx, *(uint32_t*)tmp, (uint32_t)&hsc->Instance->TDR, Size);
\r
620 /* Clear the TC flag in the SR register by writing 0 to it */
\r
621 __HAL_SMARTCARD_CLEAR_IT(hsc, SMARTCARD_FLAG_TC);
\r
623 /* Enable the DMA transfer for transmit request by setting the DMAT bit
\r
624 in the SMARTCARD associated USART CR3 register */
\r
625 hsc->Instance->CR3 |= USART_CR3_DMAT;
\r
627 /* Process Unlocked */
\r
639 * @brief Receive an amount of data in DMA mode
\r
640 * @param hsc: SMARTCARD handle
\r
641 * @param pData: pointer to data buffer
\r
642 * @param Size: amount of data to be received
\r
643 * @note The SMARTCARD-associated USART parity is enabled (PCE = 1),
\r
644 * the received data contain the parity bit (MSB position)
\r
645 * @retval HAL status
\r
647 HAL_StatusTypeDef HAL_SMARTCARD_Receive_DMA(SMARTCARD_HandleTypeDef *hsc, uint8_t *pData, uint16_t Size)
\r
651 if((hsc->State == HAL_SMARTCARD_STATE_READY) || (hsc->State == HAL_SMARTCARD_STATE_BUSY_TX))
\r
653 if((pData == NULL) || (Size == 0))
\r
658 /* Process Locked */
\r
661 hsc->pRxBuffPtr = pData;
\r
662 hsc->RxXferSize = Size;
\r
664 hsc->ErrorCode = HAL_SMARTCARD_ERROR_NONE;
\r
665 /* Check if a transmit process is ongoing or not */
\r
666 if(hsc->State == HAL_SMARTCARD_STATE_BUSY_TX)
\r
668 hsc->State = HAL_SMARTCARD_STATE_BUSY_TX_RX;
\r
672 hsc->State = HAL_SMARTCARD_STATE_BUSY_RX;
\r
675 /* Set the SMARTCARD DMA transfer complete callback */
\r
676 hsc->hdmarx->XferCpltCallback = SMARTCARD_DMAReceiveCplt;
\r
678 /* Set the SMARTCARD DMA error callback */
\r
679 hsc->hdmarx->XferErrorCallback = SMARTCARD_DMAError;
\r
681 /* Enable the DMA Stream */
\r
682 tmp = (uint32_t*)&pData;
\r
683 HAL_DMA_Start_IT(hsc->hdmarx, (uint32_t)&hsc->Instance->RDR, *(uint32_t*)tmp, Size);
\r
685 /* Enable the DMA transfer for the receiver request by setting the DMAR bit
\r
686 in the SMARTCARD associated USART CR3 register */
\r
687 hsc->Instance->CR3 |= USART_CR3_DMAR;
\r
689 /* Process Unlocked */
\r
701 * @brief SMARTCARD interrupt requests handling.
\r
702 * @param hsc: SMARTCARD handle
\r
705 void HAL_SMARTCARD_IRQHandler(SMARTCARD_HandleTypeDef *hsc)
\r
707 /* SMARTCARD parity error interrupt occurred -------------------------------*/
\r
708 if((__HAL_SMARTCARD_GET_IT(hsc, SMARTCARD_IT_PE) != RESET) && (__HAL_SMARTCARD_GET_IT_SOURCE(hsc, SMARTCARD_IT_PE) != RESET))
\r
710 __HAL_SMARTCARD_CLEAR_IT(hsc, SMARTCARD_CLEAR_PEF);
\r
711 hsc->ErrorCode |= HAL_SMARTCARD_ERROR_PE;
\r
712 /* Set the SMARTCARD state ready to be able to start again the process */
\r
713 hsc->State = HAL_SMARTCARD_STATE_READY;
\r
716 /* SMARTCARD frame error interrupt occurred ---------------------------------*/
\r
717 if((__HAL_SMARTCARD_GET_IT(hsc, SMARTCARD_IT_FE) != RESET) && (__HAL_SMARTCARD_GET_IT_SOURCE(hsc, SMARTCARD_IT_ERR) != RESET))
\r
719 __HAL_SMARTCARD_CLEAR_IT(hsc, SMARTCARD_CLEAR_FEF);
\r
720 hsc->ErrorCode |= HAL_SMARTCARD_ERROR_FE;
\r
721 /* Set the SMARTCARD state ready to be able to start again the process */
\r
722 hsc->State = HAL_SMARTCARD_STATE_READY;
\r
725 /* SMARTCARD noise error interrupt occurred ---------------------------------*/
\r
726 if((__HAL_SMARTCARD_GET_IT(hsc, SMARTCARD_IT_NE) != RESET) && (__HAL_SMARTCARD_GET_IT_SOURCE(hsc, SMARTCARD_IT_ERR) != RESET))
\r
728 __HAL_SMARTCARD_CLEAR_IT(hsc, SMARTCARD_CLEAR_NEF);
\r
729 hsc->ErrorCode |= HAL_SMARTCARD_ERROR_NE;
\r
730 /* Set the SMARTCARD state ready to be able to start again the process */
\r
731 hsc->State = HAL_SMARTCARD_STATE_READY;
\r
734 /* SMARTCARD Over-Run interrupt occurred ------------------------------------*/
\r
735 if((__HAL_SMARTCARD_GET_IT(hsc, SMARTCARD_IT_ORE) != RESET) && (__HAL_SMARTCARD_GET_IT_SOURCE(hsc, SMARTCARD_IT_ERR) != RESET))
\r
737 __HAL_SMARTCARD_CLEAR_IT(hsc, SMARTCARD_CLEAR_OREF);
\r
738 hsc->ErrorCode |= HAL_SMARTCARD_ERROR_ORE;
\r
739 /* Set the SMARTCARD state ready to be able to start again the process */
\r
740 hsc->State = HAL_SMARTCARD_STATE_READY;
\r
743 /* SMARTCARD receiver timeout interrupt occurred ----------------------------*/
\r
744 if((__HAL_SMARTCARD_GET_IT(hsc, SMARTCARD_IT_RTO) != RESET) && (__HAL_SMARTCARD_GET_IT_SOURCE(hsc, SMARTCARD_IT_RTO) != RESET))
\r
746 __HAL_SMARTCARD_CLEAR_IT(hsc, SMARTCARD_CLEAR_RTOF);
\r
747 hsc->ErrorCode |= HAL_SMARTCARD_ERROR_RTO;
\r
748 /* Set the SMARTCARD state ready to be able to start again the process */
\r
749 hsc->State = HAL_SMARTCARD_STATE_READY;
\r
752 /* Call SMARTCARD Error Call back function if need be ----------------------*/
\r
753 if(hsc->ErrorCode != HAL_SMARTCARD_ERROR_NONE)
\r
755 HAL_SMARTCARD_ErrorCallback(hsc);
\r
758 /* SMARTCARD in mode Receiver ----------------------------------------------*/
\r
759 if((__HAL_SMARTCARD_GET_IT(hsc, SMARTCARD_IT_RXNE) != RESET) && (__HAL_SMARTCARD_GET_IT_SOURCE(hsc, SMARTCARD_IT_RXNE) != RESET))
\r
761 SMARTCARD_Receive_IT(hsc);
\r
762 /* Clear RXNE interrupt flag */
\r
763 __HAL_SMARTCARD_SEND_REQ(hsc, SMARTCARD_RXDATA_FLUSH_REQUEST);
\r
766 /* SMARTCARD in mode Receiver, end of block interruption -------------------*/
\r
767 if((__HAL_SMARTCARD_GET_IT(hsc, SMARTCARD_IT_EOB) != RESET) && (__HAL_SMARTCARD_GET_IT_SOURCE(hsc, SMARTCARD_IT_EOB) != RESET))
\r
769 hsc->State = HAL_SMARTCARD_STATE_READY;
\r
770 HAL_SMARTCARD_RxCpltCallback(hsc);
\r
771 /* Clear EOBF interrupt after HAL_SMARTCARD_RxCpltCallback() call for the End of Block information
\r
772 * to be available during HAL_SMARTCARD_RxCpltCallback() processing */
\r
773 __HAL_SMARTCARD_CLEAR_IT(hsc, SMARTCARD_CLEAR_EOBF);
\r
776 /* SMARTCARD in mode Transmitter -------------------------------------------*/
\r
777 if((__HAL_SMARTCARD_GET_IT(hsc, SMARTCARD_IT_TC) != RESET) && (__HAL_SMARTCARD_GET_IT_SOURCE(hsc, SMARTCARD_IT_TC) != RESET))
\r
779 SMARTCARD_Transmit_IT(hsc);
\r
784 * @brief Tx Transfer completed callbacks
\r
785 * @param hsc: SMARTCARD handle
\r
788 __weak void HAL_SMARTCARD_TxCpltCallback(SMARTCARD_HandleTypeDef *hsc)
\r
790 /* NOTE : This function Should not be modified, when the callback is needed,
\r
791 the HAL_SMARTCARD_TxCpltCallback could be implemented in the user file
\r
796 * @brief Rx Transfer completed callbacks
\r
797 * @param hsc: SMARTCARD handle
\r
800 __weak void HAL_SMARTCARD_RxCpltCallback(SMARTCARD_HandleTypeDef *hsc)
\r
802 /* NOTE : This function Should not be modified, when the callback is needed,
\r
803 the HAL_SMARTCARD_TxCpltCallback could be implemented in the user file
\r
808 * @brief SMARTCARD error callbacks
\r
809 * @param hsc: SMARTCARD handle
\r
812 __weak void HAL_SMARTCARD_ErrorCallback(SMARTCARD_HandleTypeDef *hsc)
\r
814 /* NOTE : This function Should not be modified, when the callback is needed,
\r
815 the HAL_SMARTCARD_ErrorCallback could be implemented in the user file
\r
823 /** @defgroup SMARTCARD_Exported_Functions_Group3 Peripheral State functions
\r
824 * @brief SMARTCARD State functions
\r
827 ===============================================================================
\r
828 ##### Peripheral State and Errors functions #####
\r
829 ===============================================================================
\r
831 This subsection provides a set of functions allowing to initialize the SMARTCARD.
\r
832 (+) HAL_SMARTCARD_GetState() API is helpful to check in run-time the state of the SMARTCARD peripheral
\r
833 (+) SMARTCARD_SetConfig() API configures the SMARTCARD peripheral
\r
834 (+) SMARTCARD_CheckIdleState() API ensures that TEACK and/or REACK are set after initialization
\r
842 * @brief return the SMARTCARD state
\r
843 * @param hsc: SMARTCARD handle
\r
844 * @retval HAL state
\r
846 HAL_SMARTCARD_StateTypeDef HAL_SMARTCARD_GetState(SMARTCARD_HandleTypeDef *hsc)
\r
852 * @brief Return the SMARTCARD error code
\r
853 * @param hsc : pointer to a SMARTCARD_HandleTypeDef structure that contains
\r
854 * the configuration information for the specified SMARTCARD.
\r
855 * @retval SMARTCARD Error Code
\r
857 uint32_t HAL_SMARTCARD_GetError(SMARTCARD_HandleTypeDef *hsc)
\r
859 return hsc->ErrorCode;
\r
867 * @brief Send an amount of data in non blocking mode
\r
868 * @param hsc: SMARTCARD handle.
\r
869 * Function called under interruption only, once
\r
870 * interruptions have been enabled by HAL_SMARTCARD_Transmit_IT()
\r
871 * @retval HAL status
\r
873 static HAL_StatusTypeDef SMARTCARD_Transmit_IT(SMARTCARD_HandleTypeDef *hsc)
\r
875 if((hsc->State == HAL_SMARTCARD_STATE_BUSY_TX) || (hsc->State == HAL_SMARTCARD_STATE_BUSY_TX_RX))
\r
877 if(hsc->TxXferCount == 0)
\r
879 /* Disable the SMARTCARD Transmit Complete Interrupt */
\r
880 __HAL_SMARTCARD_DISABLE_IT(hsc, SMARTCARD_IT_TC);
\r
882 /* Check if a receive Process is ongoing or not */
\r
883 if(hsc->State == HAL_SMARTCARD_STATE_BUSY_TX_RX)
\r
885 hsc->State = HAL_SMARTCARD_STATE_BUSY_RX;
\r
889 /* Disable the SMARTCARD Error Interrupt: (Frame error, noise error, overrun error) */
\r
890 __HAL_SMARTCARD_DISABLE_IT(hsc, SMARTCARD_IT_ERR);
\r
892 hsc->State = HAL_SMARTCARD_STATE_READY;
\r
895 HAL_SMARTCARD_TxCpltCallback(hsc);
\r
901 hsc->Instance->TDR = (*hsc->pTxBuffPtr++ & (uint8_t)0xFF);
\r
902 hsc->TxXferCount--;
\r
914 * @brief Receive an amount of data in non blocking mode
\r
915 * @param hsc: SMARTCARD handle.
\r
916 * Function called under interruption only, once
\r
917 * interruptions have been enabled by HAL_SMARTCARD_Receive_IT()
\r
918 * @retval HAL status
\r
920 static HAL_StatusTypeDef SMARTCARD_Receive_IT(SMARTCARD_HandleTypeDef *hsc)
\r
922 if((hsc->State == HAL_SMARTCARD_STATE_BUSY_RX) || (hsc->State == HAL_SMARTCARD_STATE_BUSY_TX_RX))
\r
924 *hsc->pRxBuffPtr++ = (uint8_t)(hsc->Instance->RDR & (uint8_t)0xFF);
\r
926 if(--hsc->RxXferCount == 0)
\r
928 while(HAL_IS_BIT_SET(hsc->Instance->ISR, SMARTCARD_FLAG_RXNE))
\r
931 __HAL_SMARTCARD_DISABLE_IT(hsc, SMARTCARD_IT_RXNE);
\r
933 /* Check if a transmit Process is ongoing or not */
\r
934 if(hsc->State == HAL_SMARTCARD_STATE_BUSY_TX_RX)
\r
936 hsc->State = HAL_SMARTCARD_STATE_BUSY_TX;
\r
940 /* Disable the SMARTCARD Parity Error Interrupt */
\r
941 __HAL_SMARTCARD_DISABLE_IT(hsc, SMARTCARD_IT_PE);
\r
943 /* Disable the SMARTCARD Error Interrupt: (Frame error, noise error, overrun error) */
\r
944 __HAL_SMARTCARD_DISABLE_IT(hsc, SMARTCARD_IT_ERR);
\r
946 hsc->State = HAL_SMARTCARD_STATE_READY;
\r
949 HAL_SMARTCARD_RxCpltCallback(hsc);
\r
962 * @brief Configure the SMARTCARD associated USART peripheral
\r
963 * @param hsc: SMARTCARD handle
\r
966 static void SMARTCARD_SetConfig(SMARTCARD_HandleTypeDef *hsc)
\r
968 uint32_t tmpreg = 0x00000000;
\r
969 uint32_t clocksource = 0x00000000;
\r
971 /* Check the parameters */
\r
972 assert_param(IS_SMARTCARD_INSTANCE(hsc->Instance));
\r
973 assert_param(IS_SMARTCARD_BAUDRATE(hsc->Init.BaudRate));
\r
974 assert_param(IS_SMARTCARD_WORD_LENGTH(hsc->Init.WordLength));
\r
975 assert_param(IS_SMARTCARD_STOPBITS(hsc->Init.StopBits));
\r
976 assert_param(IS_SMARTCARD_PARITY(hsc->Init.Parity));
\r
977 assert_param(IS_SMARTCARD_MODE(hsc->Init.Mode));
\r
978 assert_param(IS_SMARTCARD_POLARITY(hsc->Init.CLKPolarity));
\r
979 assert_param(IS_SMARTCARD_PHASE(hsc->Init.CLKPhase));
\r
980 assert_param(IS_SMARTCARD_LASTBIT(hsc->Init.CLKLastBit));
\r
981 assert_param(IS_SMARTCARD_ONE_BIT_SAMPLE(hsc->Init.OneBitSampling));
\r
982 assert_param(IS_SMARTCARD_NACK(hsc->Init.NACKState));
\r
983 assert_param(IS_SMARTCARD_TIMEOUT(hsc->Init.TimeOutEnable));
\r
984 assert_param(IS_SMARTCARD_AUTORETRY_COUNT(hsc->Init.AutoRetryCount));
\r
986 /*-------------------------- USART CR1 Configuration -----------------------*/
\r
987 /* In SmartCard mode, M and PCE are forced to 1 (8 bits + parity).
\r
988 * Oversampling is forced to 16 (OVER8 = 0).
\r
989 * Configure the Parity and Mode:
\r
990 * set PS bit according to hsc->Init.Parity value
\r
991 * set TE and RE bits according to hsc->Init.Mode value */
\r
992 tmpreg = (uint32_t) hsc->Init.Parity | hsc->Init.Mode;
\r
993 /* in case of TX-only mode, if NACK is enabled, the USART must be able to monitor
\r
994 the bidirectional line to detect a NACK signal in case of parity error.
\r
995 Therefore, the receiver block must be enabled as well (RE bit must be set). */
\r
996 if((hsc->Init.Mode == SMARTCARD_MODE_TX) && (hsc->Init.NACKState == SMARTCARD_NACK_ENABLE))
\r
998 tmpreg |= USART_CR1_RE;
\r
1000 tmpreg |= (uint32_t) hsc->Init.WordLength;
\r
1001 MODIFY_REG(hsc->Instance->CR1, USART_CR1_FIELDS, tmpreg);
\r
1003 /*-------------------------- USART CR2 Configuration -----------------------*/
\r
1004 /* Stop bits are forced to 1.5 (STOP = 11) */
\r
1005 tmpreg = hsc->Init.StopBits;
\r
1006 /* Synchronous mode is activated by default */
\r
1007 tmpreg |= (uint32_t) USART_CR2_CLKEN | hsc->Init.CLKPolarity;
\r
1008 tmpreg |= (uint32_t) hsc->Init.CLKPhase | hsc->Init.CLKLastBit;
\r
1009 tmpreg |= (uint32_t) hsc->Init.TimeOutEnable;
\r
1010 MODIFY_REG(hsc->Instance->CR2, USART_CR2_FIELDS, tmpreg);
\r
1012 /*-------------------------- USART CR3 Configuration -----------------------*/
\r
1014 * - one-bit sampling method versus three samples' majority rule
\r
1015 * according to hsc->Init.OneBitSampling
\r
1016 * - NACK transmission in case of parity error according
\r
1017 * to hsc->Init.NACKEnable
\r
1018 * - autoretry counter according to hsc->Init.AutoRetryCount */
\r
1019 tmpreg = (uint32_t) hsc->Init.OneBitSampling | hsc->Init.NACKState;
\r
1020 tmpreg |= (uint32_t) (hsc->Init.AutoRetryCount << SMARTCARD_CR3_SCARCNT_LSB_POS);
\r
1021 MODIFY_REG(hsc->Instance-> CR3,USART_CR3_FIELDS, tmpreg);
\r
1023 /*-------------------------- USART GTPR Configuration ----------------------*/
\r
1024 tmpreg = (uint32_t) (hsc->Init.Prescaler | (hsc->Init.GuardTime << SMARTCARD_GTPR_GT_LSB_POS));
\r
1025 MODIFY_REG(hsc->Instance->GTPR, (uint32_t)(USART_GTPR_GT|USART_GTPR_PSC), tmpreg);
\r
1027 /*-------------------------- USART RTOR Configuration ----------------------*/
\r
1028 tmpreg = (uint32_t) (hsc->Init.BlockLength << SMARTCARD_RTOR_BLEN_LSB_POS);
\r
1029 if(hsc->Init.TimeOutEnable == SMARTCARD_TIMEOUT_ENABLE)
\r
1031 assert_param(IS_SMARTCARD_TIMEOUT_VALUE(hsc->Init.TimeOutValue));
\r
1032 tmpreg |= (uint32_t) hsc->Init.TimeOutValue;
\r
1034 MODIFY_REG(hsc->Instance->RTOR, (USART_RTOR_RTO|USART_RTOR_BLEN), tmpreg);
\r
1036 /*-------------------------- USART BRR Configuration -----------------------*/
\r
1037 SMARTCARD_GETCLOCKSOURCE(hsc, clocksource);
\r
1038 switch (clocksource)
\r
1040 case SMARTCARD_CLOCKSOURCE_PCLK1:
\r
1041 hsc->Instance->BRR = (uint16_t)(HAL_RCC_GetPCLK1Freq() / hsc->Init.BaudRate);
\r
1043 case SMARTCARD_CLOCKSOURCE_PCLK2:
\r
1044 hsc->Instance->BRR = (uint16_t)(HAL_RCC_GetPCLK2Freq() / hsc->Init.BaudRate);
\r
1046 case SMARTCARD_CLOCKSOURCE_HSI:
\r
1047 hsc->Instance->BRR = (uint16_t)(HSI_VALUE / hsc->Init.BaudRate);
\r
1049 case SMARTCARD_CLOCKSOURCE_SYSCLK:
\r
1050 hsc->Instance->BRR = (uint16_t)(HAL_RCC_GetSysClockFreq() / hsc->Init.BaudRate);
\r
1052 case SMARTCARD_CLOCKSOURCE_LSE:
\r
1053 hsc->Instance->BRR = (uint16_t)(LSE_VALUE / hsc->Init.BaudRate);
\r
1061 * @brief Check the SMARTCARD Idle State
\r
1062 * @param hsc: SMARTCARD handle
\r
1063 * @retval HAL status
\r
1065 static HAL_StatusTypeDef SMARTCARD_CheckIdleState(SMARTCARD_HandleTypeDef *hsc)
\r
1068 /* Initialize the SMARTCARD ErrorCode */
\r
1069 hsc->ErrorCode = HAL_SMARTCARD_ERROR_NONE;
\r
1071 /* Check if the Transmitter is enabled */
\r
1072 if((hsc->Instance->CR1 & USART_CR1_TE) == USART_CR1_TE)
\r
1074 /* Wait until TEACK flag is set */
\r
1075 if(SMARTCARD_WaitOnFlagUntilTimeout(hsc, USART_ISR_TEACK, RESET, TEACK_REACK_TIMEOUT) != HAL_OK)
\r
1077 return HAL_TIMEOUT;
\r
1080 /* Check if the Receiver is enabled */
\r
1081 if((hsc->Instance->CR1 & USART_CR1_RE) == USART_CR1_RE)
\r
1083 /* Wait until REACK flag is set */
\r
1084 if(SMARTCARD_WaitOnFlagUntilTimeout(hsc, USART_ISR_REACK, RESET, TEACK_REACK_TIMEOUT) != HAL_OK)
\r
1086 return HAL_TIMEOUT;
\r
1090 /* Process Unlocked */
\r
1091 __HAL_UNLOCK(hsc);
\r
1093 /* Initialize the SMARTCARD state*/
\r
1094 hsc->State= HAL_SMARTCARD_STATE_READY;
\r
1100 * @brief Configure the SMARTCARD associated USART peripheral advanced features
\r
1101 * @param hsc: SMARTCARD handle
\r
1104 static void SMARTCARD_AdvFeatureConfig(SMARTCARD_HandleTypeDef *hsc)
\r
1106 /* Check whether the set of advanced features to configure is properly set */
\r
1107 assert_param(IS_SMARTCARD_ADVFEATURE_INIT(hsc->AdvancedInit.AdvFeatureInit));
\r
1109 /* if required, configure TX pin active level inversion */
\r
1110 if(HAL_IS_BIT_SET(hsc->AdvancedInit.AdvFeatureInit, SMARTCARD_ADVFEATURE_TXINVERT_INIT))
\r
1112 assert_param(IS_SMARTCARD_ADVFEATURE_TXINV(hsc->AdvancedInit.TxPinLevelInvert));
\r
1113 MODIFY_REG(hsc->Instance->CR2, USART_CR2_TXINV, hsc->AdvancedInit.TxPinLevelInvert);
\r
1116 /* if required, configure RX pin active level inversion */
\r
1117 if(HAL_IS_BIT_SET(hsc->AdvancedInit.AdvFeatureInit, SMARTCARD_ADVFEATURE_RXINVERT_INIT))
\r
1119 assert_param(IS_SMARTCARD_ADVFEATURE_RXINV(hsc->AdvancedInit.RxPinLevelInvert));
\r
1120 MODIFY_REG(hsc->Instance->CR2, USART_CR2_RXINV, hsc->AdvancedInit.RxPinLevelInvert);
\r
1123 /* if required, configure data inversion */
\r
1124 if(HAL_IS_BIT_SET(hsc->AdvancedInit.AdvFeatureInit, SMARTCARD_ADVFEATURE_DATAINVERT_INIT))
\r
1126 assert_param(IS_SMARTCARD_ADVFEATURE_DATAINV(hsc->AdvancedInit.DataInvert));
\r
1127 MODIFY_REG(hsc->Instance->CR2, USART_CR2_DATAINV, hsc->AdvancedInit.DataInvert);
\r
1130 /* if required, configure RX/TX pins swap */
\r
1131 if(HAL_IS_BIT_SET(hsc->AdvancedInit.AdvFeatureInit, SMARTCARD_ADVFEATURE_SWAP_INIT))
\r
1133 assert_param(IS_SMARTCARD_ADVFEATURE_SWAP(hsc->AdvancedInit.Swap));
\r
1134 MODIFY_REG(hsc->Instance->CR2, USART_CR2_SWAP, hsc->AdvancedInit.Swap);
\r
1137 /* if required, configure RX overrun detection disabling */
\r
1138 if(HAL_IS_BIT_SET(hsc->AdvancedInit.AdvFeatureInit, SMARTCARD_ADVFEATURE_RXOVERRUNDISABLE_INIT))
\r
1140 assert_param(IS_SMARTCARD_OVERRUN(hsc->AdvancedInit.OverrunDisable));
\r
1141 MODIFY_REG(hsc->Instance->CR3, USART_CR3_OVRDIS, hsc->AdvancedInit.OverrunDisable);
\r
1144 /* if required, configure DMA disabling on reception error */
\r
1145 if(HAL_IS_BIT_SET(hsc->AdvancedInit.AdvFeatureInit, SMARTCARD_ADVFEATURE_DMADISABLEONERROR_INIT))
\r
1147 assert_param(IS_SMARTCARD_ADVFEATURE_DMAONRXERROR(hsc->AdvancedInit.DMADisableonRxError));
\r
1148 MODIFY_REG(hsc->Instance->CR3, USART_CR3_DDRE, hsc->AdvancedInit.DMADisableonRxError);
\r
1151 /* if required, configure MSB first on communication line */
\r
1152 if(HAL_IS_BIT_SET(hsc->AdvancedInit.AdvFeatureInit, SMARTCARD_ADVFEATURE_MSBFIRST_INIT))
\r
1154 assert_param(IS_SMARTCARD_ADVFEATURE_MSBFIRST(hsc->AdvancedInit.MSBFirst));
\r
1155 MODIFY_REG(hsc->Instance->CR2, USART_CR2_MSBFIRST, hsc->AdvancedInit.MSBFirst);
\r
1160 * @brief This function handles SMARTCARD Communication Timeout.
\r
1161 * @param hsc: SMARTCARD handle
\r
1162 * @param Flag: specifies the SMARTCARD flag to check.
\r
1163 * @param Status: The new Flag status (SET or RESET).
\r
1164 * @param Timeout: Timeout duration
\r
1165 * @retval HAL status
\r
1167 static HAL_StatusTypeDef SMARTCARD_WaitOnFlagUntilTimeout(SMARTCARD_HandleTypeDef *hsc, uint32_t Flag, FlagStatus Status, uint32_t Timeout)
\r
1169 uint32_t tickstart = 0x00;
\r
1172 tickstart = HAL_GetTick();
\r
1174 /* Wait until flag is set */
\r
1175 if(Status == RESET)
\r
1177 while(__HAL_SMARTCARD_GET_FLAG(hsc, Flag) == RESET)
\r
1179 /* Check for the Timeout */
\r
1180 if(Timeout != HAL_MAX_DELAY)
\r
1182 if((Timeout == 0)||((HAL_GetTick() - tickstart ) > Timeout))
\r
1184 /* Disable TXE, RXNE, PE and ERR (Frame error, noise error, overrun error) interrupts for the interrupt process */
\r
1185 __HAL_SMARTCARD_DISABLE_IT(hsc, SMARTCARD_IT_TXE);
\r
1186 __HAL_SMARTCARD_DISABLE_IT(hsc, SMARTCARD_IT_RXNE);
\r
1187 __HAL_SMARTCARD_DISABLE_IT(hsc, SMARTCARD_IT_PE);
\r
1188 __HAL_SMARTCARD_DISABLE_IT(hsc, SMARTCARD_IT_ERR);
\r
1190 hsc->State= HAL_SMARTCARD_STATE_READY;
\r
1192 /* Process Unlocked */
\r
1193 __HAL_UNLOCK(hsc);
\r
1195 return HAL_TIMEOUT;
\r
1202 while(__HAL_SMARTCARD_GET_FLAG(hsc, Flag) != RESET)
\r
1204 /* Check for the Timeout */
\r
1205 if(Timeout != HAL_MAX_DELAY)
\r
1207 if((Timeout == 0)||((HAL_GetTick() - tickstart ) > Timeout))
\r
1209 /* Disable TXE, RXNE, PE and ERR (Frame error, noise error, overrun error) interrupts for the interrupt process */
\r
1210 __HAL_SMARTCARD_DISABLE_IT(hsc, SMARTCARD_IT_TXE);
\r
1211 __HAL_SMARTCARD_DISABLE_IT(hsc, SMARTCARD_IT_RXNE);
\r
1212 __HAL_SMARTCARD_DISABLE_IT(hsc, SMARTCARD_IT_PE);
\r
1213 __HAL_SMARTCARD_DISABLE_IT(hsc, SMARTCARD_IT_ERR);
\r
1215 hsc->State= HAL_SMARTCARD_STATE_READY;
\r
1217 /* Process Unlocked */
\r
1218 __HAL_UNLOCK(hsc);
\r
1220 return HAL_TIMEOUT;
\r
1229 * @brief DMA SMARTCARD transmit process complete callback
\r
1230 * @param hdma: DMA handle
\r
1233 static void SMARTCARD_DMATransmitCplt(DMA_HandleTypeDef *hdma)
\r
1235 SMARTCARD_HandleTypeDef* hsc = ( SMARTCARD_HandleTypeDef* )((DMA_HandleTypeDef* )hdma)->Parent;
\r
1236 hsc->TxXferCount = 0;
\r
1238 /* Disable the DMA transfer for transmit request by setting the DMAT bit
\r
1239 in the SMARTCARD associated USART CR3 register */
\r
1240 hsc->Instance->CR3 &= (uint16_t)~((uint16_t)USART_CR3_DMAT);
\r
1242 /* Wait for SMARTCARD TC Flag */
\r
1243 if(SMARTCARD_WaitOnFlagUntilTimeout(hsc, SMARTCARD_FLAG_TC, RESET, HAL_SMARTCARD_TXDMA_TIMEOUTVALUE) != HAL_OK)
\r
1245 /* Timeout Occurred */
\r
1246 hsc->State = HAL_SMARTCARD_STATE_TIMEOUT;
\r
1247 HAL_SMARTCARD_ErrorCallback(hsc);
\r
1252 /* Check if a receive Process is ongoing or not */
\r
1253 if(hsc->State == HAL_SMARTCARD_STATE_BUSY_TX_RX)
\r
1255 hsc->State = HAL_SMARTCARD_STATE_BUSY_RX;
\r
1259 hsc->State = HAL_SMARTCARD_STATE_READY;
\r
1261 HAL_SMARTCARD_TxCpltCallback(hsc);
\r
1266 * @brief DMA SMARTCARD receive process complete callback
\r
1267 * @param hdma: DMA handle
\r
1270 static void SMARTCARD_DMAReceiveCplt(DMA_HandleTypeDef *hdma)
\r
1272 SMARTCARD_HandleTypeDef* hsc = ( SMARTCARD_HandleTypeDef* )((DMA_HandleTypeDef* )hdma)->Parent;
\r
1273 hsc->RxXferCount = 0;
\r
1275 /* Disable the DMA transfer for the receiver request by setting the DMAR bit
\r
1276 in the SMARTCARD associated USART CR3 register */
\r
1277 hsc->Instance->CR3 &= (uint16_t)~((uint16_t)USART_CR3_DMAR);
\r
1279 /* Check if a transmit Process is ongoing or not */
\r
1280 if(hsc->State == HAL_SMARTCARD_STATE_BUSY_TX_RX)
\r
1282 hsc->State = HAL_SMARTCARD_STATE_BUSY_TX;
\r
1286 hsc->State = HAL_SMARTCARD_STATE_READY;
\r
1289 HAL_SMARTCARD_RxCpltCallback(hsc);
\r
1293 * @brief DMA SMARTCARD communication error callback
\r
1294 * @param hdma: DMA handle
\r
1297 static void SMARTCARD_DMAError(DMA_HandleTypeDef *hdma)
\r
1299 SMARTCARD_HandleTypeDef* hsc = ( SMARTCARD_HandleTypeDef* )((DMA_HandleTypeDef* )hdma)->Parent;
\r
1300 hsc->RxXferCount = 0;
\r
1301 hsc->TxXferCount = 0;
\r
1302 hsc->State= HAL_SMARTCARD_STATE_READY;
\r
1303 hsc->ErrorCode |= HAL_SMARTCARD_ERROR_DMA;
\r
1304 HAL_SMARTCARD_ErrorCallback(hsc);
\r
1310 #endif /* HAL_SMARTCARD_MODULE_ENABLED */
\r
1319 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
\r