Merge pull request #11198 from SteveCEvans/sce_rc2
[betaflight.git] / lib / main / STM32F4 / Drivers / STM32F4xx_HAL_Driver / Src / stm32f4xx_hal_smartcard.c
blob5acd5c89bc5ff4fb084be86c81d5a183e1ff20f2
1 /**
2 ******************************************************************************
3 * @file stm32f4xx_hal_smartcard.c
4 * @author MCD Application Team
5 * @version V1.7.1
6 * @date 14-April-2017
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
14 @verbatim
15 ==============================================================================
16 ##### How to use this driver #####
17 ==============================================================================
18 [..]
19 The SMARTCARD HAL driver can be used as follows:
21 (#) Declare a SMARTCARD_HandleTypeDef handle structure.
22 (#) Initialize the SMARTCARD low level resources by implementing the HAL_SMARTCARD_MspInit() API:
23 (##) Enable the USARTx interface clock.
24 (##) SMARTCARD pins configuration:
25 (+++) Enable the clock for the SMARTCARD GPIOs.
26 (+++) Configure these SMARTCARD pins as alternate function pull-up.
27 (##) NVIC configuration if you need to use interrupt process (HAL_SMARTCARD_Transmit_IT()
28 and HAL_SMARTCARD_Receive_IT() APIs):
29 (+++) Configure the USARTx interrupt priority.
30 (+++) Enable the NVIC USART IRQ handle.
31 (##) DMA Configuration if you need to use DMA process (HAL_SMARTCARD_Transmit_DMA()
32 and HAL_SMARTCARD_Receive_DMA() APIs):
33 (+++) Declare a DMA handle structure for the Tx/Rx stream.
34 (+++) Enable the DMAx interface clock.
35 (+++) Configure the declared DMA handle structure with the required Tx/Rx parameters.
36 (+++) Configure the DMA Tx/Rx Stream.
37 (+++) Associate the initialized DMA handle to the SMARTCARD DMA Tx/Rx handle.
38 (+++) Configure the priority and enable the NVIC for the transfer complete interrupt on the DMA Tx/Rx Stream.
40 (#) Program the Baud Rate, Word Length , Stop Bit, Parity, Hardware
41 flow control and Mode(Receiver/Transmitter) in the SMARTCARD Init structure.
43 (#) Initialize the SMARTCARD registers by calling the HAL_SMARTCARD_Init() API:
44 (++) These APIs configure also the low level Hardware GPIO, CLOCK, CORTEX...etc)
45 by calling the customized HAL_SMARTCARD_MspInit() API.
46 [..]
47 (@) The specific SMARTCARD interrupts (Transmission complete interrupt,
48 RXNE interrupt and Error Interrupts) will be managed using the macros
49 __HAL_SMARTCARD_ENABLE_IT() and __HAL_SMARTCARD_DISABLE_IT() inside the transmit and receive process.
51 [..]
52 Three operation modes are available within this driver :
54 *** Polling mode IO operation ***
55 =================================
56 [..]
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 ===================================
62 [..]
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 ==============================
74 [..]
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 =============================================
86 [..]
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
96 [..]
97 (@) You can refer to the SMARTCARD HAL driver header file for more useful macros
99 @endverbatim
100 ******************************************************************************
101 * @attention
103 * <h2><center>&copy; COPYRIGHT(c) 2017 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 "stm32f4xx_hal.h"
133 /** @addtogroup STM32F4xx_HAL_Driver
134 * @{
137 /** @defgroup SMARTCARD SMARTCARD
138 * @brief HAL USART SMARTCARD module driver
139 * @{
141 #ifdef HAL_SMARTCARD_MODULE_ENABLED
142 /* Private typedef -----------------------------------------------------------*/
143 /* Private define ------------------------------------------------------------*/
144 /** @addtogroup SMARTCARD_Private_Constants
145 * @{
148 * @}
150 /* Private macro -------------------------------------------------------------*/
151 /* Private variables ---------------------------------------------------------*/
152 /* Private function prototypes -----------------------------------------------*/
153 /** @addtogroup SMARTCARD_Private_Functions
154 * @{
156 static void SMARTCARD_EndTxTransfer(SMARTCARD_HandleTypeDef *hsc);
157 static void SMARTCARD_EndRxTransfer(SMARTCARD_HandleTypeDef *hsc);
158 static void SMARTCARD_SetConfig (SMARTCARD_HandleTypeDef *hsc);
159 static HAL_StatusTypeDef SMARTCARD_Transmit_IT(SMARTCARD_HandleTypeDef *hsc);
160 static HAL_StatusTypeDef SMARTCARD_EndTransmit_IT(SMARTCARD_HandleTypeDef *hsmartcard);
161 static HAL_StatusTypeDef SMARTCARD_Receive_IT(SMARTCARD_HandleTypeDef *hsc);
162 static void SMARTCARD_DMATransmitCplt(DMA_HandleTypeDef *hdma);
163 static void SMARTCARD_DMAReceiveCplt(DMA_HandleTypeDef *hdma);
164 static void SMARTCARD_DMAError(DMA_HandleTypeDef *hdma);
165 static void SMARTCARD_DMAAbortOnError(DMA_HandleTypeDef *hdma);
166 static void SMARTCARD_DMATxAbortCallback(DMA_HandleTypeDef *hdma);
167 static void SMARTCARD_DMARxAbortCallback(DMA_HandleTypeDef *hdma);
168 static void SMARTCARD_DMATxOnlyAbortCallback(DMA_HandleTypeDef *hdma);
169 static void SMARTCARD_DMARxOnlyAbortCallback(DMA_HandleTypeDef *hdma);
170 static HAL_StatusTypeDef SMARTCARD_WaitOnFlagUntilTimeout(SMARTCARD_HandleTypeDef *hsc, uint32_t Flag, FlagStatus Status, uint32_t Tickstart, uint32_t Timeout);
172 * @}
174 /* Exported functions --------------------------------------------------------*/
175 /** @defgroup SMARTCARD_Exported_Functions SMARTCARD Exported Functions
176 * @{
179 /** @defgroup SMARTCARD_Exported_Functions_Group1 SmartCard Initialization and de-initialization functions
180 * @brief Initialization and Configuration functions
182 @verbatim
183 ==============================================================================
184 ##### Initialization and Configuration functions #####
185 ==============================================================================
186 [..]
187 This subsection provides a set of functions allowing to initialize the USART
188 in Smartcard mode.
189 [..]
190 The Smartcard interface is designed to support asynchronous protocol Smartcards as
191 defined in the ISO 7816-3 standard.
192 [..]
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.
196 [..]
197 (+) For the Smartcard mode only these parameters can be configured:
198 (++) Baud Rate
199 (++) Word Length => Should be 9 bits (8 bits + parity)
200 (++) Stop Bit
201 (++) Parity: => Should be enabled
202 (++) USART polarity
203 (++) USART phase
204 (++) USART LastBit
205 (++) Receiver/transmitter modes
206 (++) Prescaler
207 (++) GuardTime
208 (++) NACKState: The Smartcard NACK state
210 (+) Recommended SmartCard interface configuration to get the Answer to Reset from the Card:
211 (++) Word Length = 9 Bits
212 (++) 1.5 Stop Bit
213 (++) Even parity
214 (++) BaudRate = 12096 baud
215 (++) Tx and Rx enabled
216 [..]
217 Please refer to the ISO 7816-3 specification for more details.
219 [..]
220 (@) It is also possible to choose 0.5 stop bit for receiving but it is recommended
221 to use 1.5 stop bits for both transmitting and receiving to avoid switching
222 between the two configurations.
223 [..]
224 The HAL_SMARTCARD_Init() function follows the USART SmartCard configuration
225 procedure (details for the procedure are available in reference manual (RM0329)).
227 @endverbatim
229 The SMARTCARD frame format is given in the following table:
230 +-------------------------------------------------------------+
231 | M bit | PCE bit | SMARTCARD frame |
232 |---------------------|---------------------------------------|
233 | 1 | 1 | | SB | 8 bit data | PB | STB | |
234 +-------------------------------------------------------------+
235 * @{
239 * @brief Initializes the SmartCard mode according to the specified
240 * parameters in the SMARTCARD_InitTypeDef and create the associated handle .
241 * @param hsc: pointer to a SMARTCARD_HandleTypeDef structure that contains
242 * the configuration information for SMARTCARD module.
243 * @retval HAL status
245 HAL_StatusTypeDef HAL_SMARTCARD_Init(SMARTCARD_HandleTypeDef *hsc)
247 /* Check the SMARTCARD handle allocation */
248 if(hsc == NULL)
250 return HAL_ERROR;
253 /* Check the parameters */
254 assert_param(IS_SMARTCARD_INSTANCE(hsc->Instance));
255 assert_param(IS_SMARTCARD_NACK_STATE(hsc->Init.NACKState));
257 if(hsc->gState == HAL_SMARTCARD_STATE_RESET)
259 /* Allocate lock resource and initialize it */
260 hsc->Lock = HAL_UNLOCKED;
261 /* Init the low level hardware : GPIO, CLOCK, CORTEX...etc */
262 HAL_SMARTCARD_MspInit(hsc);
265 hsc->gState = HAL_SMARTCARD_STATE_BUSY;
267 /* Set the Prescaler */
268 MODIFY_REG(hsc->Instance->GTPR, USART_GTPR_PSC, hsc->Init.Prescaler);
270 /* Set the Guard Time */
271 MODIFY_REG(hsc->Instance->GTPR, USART_GTPR_GT, ((hsc->Init.GuardTime)<<8U));
273 /* Set the Smartcard Communication parameters */
274 SMARTCARD_SetConfig(hsc);
276 /* In SmartCard mode, the following bits must be kept cleared:
277 - LINEN bit in the USART_CR2 register
278 - HDSEL and IREN bits in the USART_CR3 register.*/
279 CLEAR_BIT(hsc->Instance->CR2, USART_CR2_LINEN);
280 CLEAR_BIT(hsc->Instance->CR3, (USART_CR3_IREN | USART_CR3_HDSEL));
282 /* Enable the SMARTCARD Parity Error Interrupt */
283 SET_BIT(hsc->Instance->CR1, USART_CR1_PEIE);
285 /* Enable the SMARTCARD Framing Error Interrupt */
286 SET_BIT(hsc->Instance->CR3, USART_CR3_EIE);
288 /* Enable the Peripheral */
289 __HAL_SMARTCARD_ENABLE(hsc);
291 /* Configure the Smartcard NACK state */
292 MODIFY_REG(hsc->Instance->CR3, USART_CR3_NACK, hsc->Init.NACKState);
294 /* Enable the SC mode by setting the SCEN bit in the CR3 register */
295 hsc->Instance->CR3 |= (USART_CR3_SCEN);
297 /* Initialize the SMARTCARD state*/
298 hsc->ErrorCode = HAL_SMARTCARD_ERROR_NONE;
299 hsc->gState= HAL_SMARTCARD_STATE_READY;
300 hsc->RxState= HAL_SMARTCARD_STATE_READY;
302 return HAL_OK;
306 * @brief DeInitializes the USART SmartCard peripheral
307 * @param hsc: pointer to a SMARTCARD_HandleTypeDef structure that contains
308 * the configuration information for SMARTCARD module.
309 * @retval HAL status
311 HAL_StatusTypeDef HAL_SMARTCARD_DeInit(SMARTCARD_HandleTypeDef *hsc)
313 /* Check the SMARTCARD handle allocation */
314 if(hsc == NULL)
316 return HAL_ERROR;
319 /* Check the parameters */
320 assert_param(IS_SMARTCARD_INSTANCE(hsc->Instance));
322 hsc->gState = HAL_SMARTCARD_STATE_BUSY;
324 /* Disable the Peripheral */
325 __HAL_SMARTCARD_DISABLE(hsc);
327 /* DeInit the low level hardware */
328 HAL_SMARTCARD_MspDeInit(hsc);
330 hsc->ErrorCode = HAL_SMARTCARD_ERROR_NONE;
331 hsc->gState = HAL_SMARTCARD_STATE_RESET;
332 hsc->RxState = HAL_SMARTCARD_STATE_RESET;
334 /* Release Lock */
335 __HAL_UNLOCK(hsc);
337 return HAL_OK;
341 * @brief SMARTCARD MSP Init
342 * @param hsc: pointer to a SMARTCARD_HandleTypeDef structure that contains
343 * the configuration information for SMARTCARD module.
344 * @retval None
346 __weak void HAL_SMARTCARD_MspInit(SMARTCARD_HandleTypeDef *hsc)
348 /* Prevent unused argument(s) compilation warning */
349 UNUSED(hsc);
350 /* NOTE : This function Should not be modified, when the callback is needed,
351 the HAL_SMARTCARD_MspInit could be implemented in the user file
356 * @brief SMARTCARD MSP DeInit
357 * @param hsc: pointer to a SMARTCARD_HandleTypeDef structure that contains
358 * the configuration information for SMARTCARD module.
359 * @retval None
361 __weak void HAL_SMARTCARD_MspDeInit(SMARTCARD_HandleTypeDef *hsc)
363 /* Prevent unused argument(s) compilation warning */
364 UNUSED(hsc);
365 /* NOTE : This function Should not be modified, when the callback is needed,
366 the HAL_SMARTCARD_MspDeInit could be implemented in the user file
371 * @}
374 /** @defgroup SMARTCARD_Exported_Functions_Group2 IO operation functions
375 * @brief SMARTCARD Transmit and Receive functions
377 @verbatim
378 ===============================================================================
379 ##### IO operation functions #####
380 ===============================================================================
381 [..] This subsection provides a set of functions allowing to manage the SMARTCARD data transfers.
383 [..]
384 (#) Smartcard is a single wire half duplex communication protocol.
385 The Smartcard interface is designed to support asynchronous protocol Smartcards as
386 defined in the ISO 7816-3 standard.
387 (#) The USART should be configured as:
388 (++) 8 bits plus parity: where M=1 and PCE=1 in the USART_CR1 register
389 (++) 1.5 stop bits when transmitting and receiving: where STOP=11 in the USART_CR2 register.
391 (#) There are two modes of transfer:
392 (++) Blocking mode: The communication is performed in polling mode.
393 The HAL status of all data processing is returned by the same function
394 after finishing transfer.
395 (++) Non Blocking mode: The communication is performed using Interrupts
396 or DMA, These APIs return the HAL status.
397 The end of the data processing will be indicated through the
398 dedicated SMARTCARD IRQ when using Interrupt mode or the DMA IRQ when
399 using DMA mode.
400 The HAL_SMARTCARD_TxCpltCallback(), HAL_SMARTCARD_RxCpltCallback() user callbacks
401 will be executed respectively at the end of the Transmit or Receive process
402 The HAL_SMARTCARD_ErrorCallback() user callback will be executed when a communication error is detected
404 (#) Blocking mode APIs are :
405 (++) HAL_SMARTCARD_Transmit()
406 (++) HAL_SMARTCARD_Receive()
408 (#) Non Blocking mode APIs with Interrupt are :
409 (++) HAL_SMARTCARD_Transmit_IT()
410 (++) HAL_SMARTCARD_Receive_IT()
411 (++) HAL_SMARTCARD_IRQHandler()
413 (#) Non Blocking mode functions with DMA are :
414 (++) HAL_SMARTCARD_Transmit_DMA()
415 (++) HAL_SMARTCARD_Receive_DMA()
417 (#) A set of Transfer Complete Callbacks are provided in non Blocking mode:
418 (++) HAL_SMARTCARD_TxCpltCallback()
419 (++) HAL_SMARTCARD_RxCpltCallback()
420 (++) HAL_SMARTCARD_ErrorCallback()
422 @endverbatim
423 * @{
427 * @brief Send an amount of data in blocking mode
428 * @param hsc: pointer to a SMARTCARD_HandleTypeDef structure that contains
429 * the configuration information for SMARTCARD module.
430 * @param pData: pointer to data buffer
431 * @param Size: amount of data to be sent
432 * @param Timeout: Timeout duration
433 * @retval HAL status
435 HAL_StatusTypeDef HAL_SMARTCARD_Transmit(SMARTCARD_HandleTypeDef *hsc, uint8_t *pData, uint16_t Size, uint32_t Timeout)
437 uint16_t* tmp;
438 uint32_t tickstart = 0U;
440 if(hsc->gState == HAL_SMARTCARD_STATE_READY)
442 if((pData == NULL) || (Size == 0))
444 return HAL_ERROR;
447 /* Process Locked */
448 __HAL_LOCK(hsc);
450 hsc->ErrorCode = HAL_SMARTCARD_ERROR_NONE;
451 hsc->gState = HAL_SMARTCARD_STATE_BUSY_TX;
453 /* Init tickstart for timeout managment */
454 tickstart = HAL_GetTick();
456 hsc->TxXferSize = Size;
457 hsc->TxXferCount = Size;
458 while(hsc->TxXferCount > 0U)
460 hsc->TxXferCount--;
461 if(SMARTCARD_WaitOnFlagUntilTimeout(hsc, SMARTCARD_FLAG_TXE, RESET, tickstart, Timeout) != HAL_OK)
463 return HAL_TIMEOUT;
465 tmp = (uint16_t*) pData;
466 hsc->Instance->DR = (*tmp & (uint16_t)0x01FF);
467 pData +=1U;
470 if(SMARTCARD_WaitOnFlagUntilTimeout(hsc, SMARTCARD_FLAG_TC, RESET, tickstart, Timeout) != HAL_OK)
472 return HAL_TIMEOUT;
475 /* At end of Tx process, restore hsc->gState to Ready */
476 hsc->gState = HAL_SMARTCARD_STATE_READY;
478 /* Process Unlocked */
479 __HAL_UNLOCK(hsc);
481 return HAL_OK;
483 else
485 return HAL_BUSY;
490 * @brief Receive an amount of data in blocking mode
491 * @param hsc: pointer to a SMARTCARD_HandleTypeDef structure that contains
492 * the configuration information for SMARTCARD module.
493 * @param pData: pointer to data buffer
494 * @param Size: amount of data to be received
495 * @param Timeout: Timeout duration
496 * @retval HAL status
498 HAL_StatusTypeDef HAL_SMARTCARD_Receive(SMARTCARD_HandleTypeDef *hsc, uint8_t *pData, uint16_t Size, uint32_t Timeout)
500 uint16_t* tmp;
501 uint32_t tickstart = 0U;
503 if(hsc->RxState == HAL_SMARTCARD_STATE_READY)
505 if((pData == NULL) || (Size == 0))
507 return HAL_ERROR;
510 /* Process Locked */
511 __HAL_LOCK(hsc);
513 hsc->ErrorCode = HAL_SMARTCARD_ERROR_NONE;
514 hsc->RxState = HAL_SMARTCARD_STATE_BUSY_RX;
516 /* Init tickstart for timeout managment */
517 tickstart = HAL_GetTick();
519 hsc->RxXferSize = Size;
520 hsc->RxXferCount = Size;
522 /* Check the remain data to be received */
523 while(hsc->RxXferCount > 0U)
525 hsc->RxXferCount--;
526 if(SMARTCARD_WaitOnFlagUntilTimeout(hsc, SMARTCARD_FLAG_RXNE, RESET, tickstart, Timeout) != HAL_OK)
528 return HAL_TIMEOUT;
530 tmp = (uint16_t*) pData;
531 *tmp = (uint8_t)(hsc->Instance->DR & (uint8_t)0xFF);
532 pData +=1U;
535 /* At end of Rx process, restore hsc->RxState to Ready */
536 hsc->RxState = HAL_SMARTCARD_STATE_READY;
538 /* Process Unlocked */
539 __HAL_UNLOCK(hsc);
541 return HAL_OK;
543 else
545 return HAL_BUSY;
550 * @brief Send an amount of data in non blocking mode
551 * @param hsc: pointer to a SMARTCARD_HandleTypeDef structure that contains
552 * the configuration information for SMARTCARD module.
553 * @param pData: pointer to data buffer
554 * @param Size: amount of data to be sent
555 * @retval HAL status
557 HAL_StatusTypeDef HAL_SMARTCARD_Transmit_IT(SMARTCARD_HandleTypeDef *hsc, uint8_t *pData, uint16_t Size)
559 /* Check that a Tx process is not already ongoing */
560 if(hsc->gState == HAL_SMARTCARD_STATE_READY)
562 if((pData == NULL) || (Size == 0))
564 return HAL_ERROR;
567 /* Process Locked */
568 __HAL_LOCK(hsc);
570 hsc->pTxBuffPtr = pData;
571 hsc->TxXferSize = Size;
572 hsc->TxXferCount = Size;
574 hsc->ErrorCode = HAL_SMARTCARD_ERROR_NONE;
575 hsc->gState = HAL_SMARTCARD_STATE_BUSY_TX;
577 /* Process Unlocked */
578 __HAL_UNLOCK(hsc);
580 /* Enable the SMARTCARD Parity Error Interrupt */
581 SET_BIT(hsc->Instance->CR1, USART_CR1_PEIE);
583 /* Disable the SMARTCARD Error Interrupt: (Frame error, noise error, overrun error) */
584 CLEAR_BIT(hsc->Instance->CR3, USART_CR3_EIE);
586 /* Enable the SMARTCARD Transmit data register empty Interrupt */
587 SET_BIT(hsc->Instance->CR1, USART_CR1_TXEIE);
589 return HAL_OK;
591 else
593 return HAL_BUSY;
598 * @brief Receive an amount of data in non blocking mode
599 * @param hsc: pointer to a SMARTCARD_HandleTypeDef structure that contains
600 * the configuration information for SMARTCARD module.
601 * @param pData: pointer to data buffer
602 * @param Size: amount of data to be received
603 * @retval HAL status
605 HAL_StatusTypeDef HAL_SMARTCARD_Receive_IT(SMARTCARD_HandleTypeDef *hsc, uint8_t *pData, uint16_t Size)
607 /* Check that a Rx process is not already ongoing */
608 if(hsc->RxState == HAL_SMARTCARD_STATE_READY)
610 if((pData == NULL) || (Size == 0))
612 return HAL_ERROR;
615 /* Process Locked */
616 __HAL_LOCK(hsc);
618 hsc->pRxBuffPtr = pData;
619 hsc->RxXferSize = Size;
620 hsc->RxXferCount = Size;
622 hsc->ErrorCode = HAL_SMARTCARD_ERROR_NONE;
623 hsc->RxState = HAL_SMARTCARD_STATE_BUSY_RX;
625 /* Process Unlocked */
626 __HAL_UNLOCK(hsc);
628 /* Enable the SMARTCARD Parity Error and Data Register not empty Interrupts */
629 SET_BIT(hsc->Instance->CR1, USART_CR1_PEIE| USART_CR1_RXNEIE);
631 /* Enable the SMARTCARD Error Interrupt: (Frame error, noise error, overrun error) */
632 SET_BIT(hsc->Instance->CR3, USART_CR3_EIE);
634 return HAL_OK;
636 else
638 return HAL_BUSY;
643 * @brief Send an amount of data in non blocking mode
644 * @param hsc: pointer to a SMARTCARD_HandleTypeDef structure that contains
645 * the configuration information for SMARTCARD module.
646 * @param pData: pointer to data buffer
647 * @param Size: amount of data to be sent
648 * @retval HAL status
650 HAL_StatusTypeDef HAL_SMARTCARD_Transmit_DMA(SMARTCARD_HandleTypeDef *hsc, uint8_t *pData, uint16_t Size)
652 uint32_t *tmp;
654 /* Check that a Tx process is not already ongoing */
655 if(hsc->gState == HAL_SMARTCARD_STATE_READY)
657 if((pData == NULL) || (Size == 0))
659 return HAL_ERROR;
662 /* Process Locked */
663 __HAL_LOCK(hsc);
665 hsc->pTxBuffPtr = pData;
666 hsc->TxXferSize = Size;
667 hsc->TxXferCount = Size;
669 hsc->ErrorCode = HAL_SMARTCARD_ERROR_NONE;
670 hsc->gState = HAL_SMARTCARD_STATE_BUSY_TX;
672 /* Set the SMARTCARD DMA transfer complete callback */
673 hsc->hdmatx->XferCpltCallback = SMARTCARD_DMATransmitCplt;
675 /* Set the DMA error callback */
676 hsc->hdmatx->XferErrorCallback = SMARTCARD_DMAError;
678 /* Set the DMA abort callback */
679 hsc->hdmatx->XferAbortCallback = NULL;
681 /* Enable the SMARTCARD transmit DMA Stream */
682 tmp = (uint32_t*)&pData;
683 HAL_DMA_Start_IT(hsc->hdmatx, *(uint32_t*)tmp, (uint32_t)&hsc->Instance->DR, Size);
685 /* Clear the TC flag in the SR register by writing 0 to it */
686 __HAL_SMARTCARD_CLEAR_FLAG(hsc, SMARTCARD_FLAG_TC);
688 /* Process Unlocked */
689 __HAL_UNLOCK(hsc);
691 /* Enable the DMA transfer for transmit request by setting the DMAT bit
692 in the SMARTCARD CR3 register */
693 SET_BIT(hsc->Instance->CR3, USART_CR3_DMAT);
695 return HAL_OK;
697 else
699 return HAL_BUSY;
704 * @brief Receive an amount of data in non blocking mode
705 * @param hsc: pointer to a SMARTCARD_HandleTypeDef structure that contains
706 * the configuration information for SMARTCARD module.
707 * @param pData: pointer to data buffer
708 * @param Size: amount of data to be received
709 * @note When the SMARTCARD parity is enabled (PCE = 1) the data received contain the parity bit.s
710 * @retval HAL status
712 HAL_StatusTypeDef HAL_SMARTCARD_Receive_DMA(SMARTCARD_HandleTypeDef *hsc, uint8_t *pData, uint16_t Size)
714 uint32_t *tmp;
716 /* Check that a Rx process is not already ongoing */
717 if(hsc->RxState == HAL_SMARTCARD_STATE_READY)
719 if((pData == NULL) || (Size == 0))
721 return HAL_ERROR;
724 /* Process Locked */
725 __HAL_LOCK(hsc);
727 hsc->pRxBuffPtr = pData;
728 hsc->RxXferSize = Size;
730 hsc->ErrorCode = HAL_SMARTCARD_ERROR_NONE;
731 hsc->RxState = HAL_SMARTCARD_STATE_BUSY_RX;
733 /* Set the SMARTCARD DMA transfer complete callback */
734 hsc->hdmarx->XferCpltCallback = SMARTCARD_DMAReceiveCplt;
736 /* Set the DMA error callback */
737 hsc->hdmarx->XferErrorCallback = SMARTCARD_DMAError;
739 /* Set the DMA abort callback */
740 hsc->hdmatx->XferAbortCallback = NULL;
742 /* Enable the DMA Stream */
743 tmp = (uint32_t*)&pData;
744 HAL_DMA_Start_IT(hsc->hdmarx, (uint32_t)&hsc->Instance->DR, *(uint32_t*)tmp, Size);
746 /* Clear the Overrun flag just before enabling the DMA Rx request: can be mandatory for the second transfer */
747 __HAL_SMARTCARD_CLEAR_OREFLAG(hsc);
749 /* Process Unlocked */
750 __HAL_UNLOCK(hsc);
752 /* Enable the SMARTCARD Parity Error Interrupt */
753 SET_BIT(hsc->Instance->CR1, USART_CR1_PEIE);
755 /* Enable the SMARTCARD Error Interrupt: (Frame error, noise error, overrun error) */
756 SET_BIT(hsc->Instance->CR3, USART_CR3_EIE);
758 /* Enable the DMA transfer for the receiver request by setting the DMAR bit
759 in the SMARTCARD CR3 register */
760 SET_BIT(hsc->Instance->CR3, USART_CR3_DMAR);
762 return HAL_OK;
764 else
766 return HAL_BUSY;
771 * @brief Abort ongoing transfers (blocking mode).
772 * @param hsc SMARTCARD handle.
773 * @note This procedure could be used for aborting any ongoing transfer started in Interrupt or DMA mode.
774 * This procedure performs following operations :
775 * - Disable PPP Interrupts
776 * - Disable the DMA transfer in the peripheral register (if enabled)
777 * - Abort DMA transfer by calling HAL_DMA_Abort (in case of transfer in DMA mode)
778 * - Set handle State to READY
779 * @note This procedure is executed in blocking mode : when exiting function, Abort is considered as completed.
780 * @retval HAL status
782 HAL_StatusTypeDef HAL_SMARTCARD_Abort(SMARTCARD_HandleTypeDef *hsc)
784 /* Disable TXEIE, TCIE, RXNE, PE and ERR (Frame error, noise error, overrun error) interrupts */
785 CLEAR_BIT(hsc->Instance->CR1, (USART_CR1_RXNEIE | USART_CR1_PEIE | USART_CR1_TXEIE | USART_CR1_TCIE));
786 CLEAR_BIT(hsc->Instance->CR3, USART_CR3_EIE);
788 /* Disable the SMARTCARD DMA Tx request if enabled */
789 if(HAL_IS_BIT_SET(hsc->Instance->CR3, USART_CR3_DMAT))
791 CLEAR_BIT(hsc->Instance->CR3, USART_CR3_DMAT);
793 /* Abort the SMARTCARD DMA Tx channel : use blocking DMA Abort API (no callback) */
794 if(hsc->hdmatx != NULL)
796 /* Set the SMARTCARD DMA Abort callback to Null.
797 No call back execution at end of DMA abort procedure */
798 hsc->hdmatx->XferAbortCallback = NULL;
800 HAL_DMA_Abort(hsc->hdmatx);
804 /* Disable the SMARTCARD DMA Rx request if enabled */
805 if(HAL_IS_BIT_SET(hsc->Instance->CR3, USART_CR3_DMAR))
807 CLEAR_BIT(hsc->Instance->CR3, USART_CR3_DMAR);
809 /* Abort the SMARTCARD DMA Rx channel : use blocking DMA Abort API (no callback) */
810 if(hsc->hdmarx != NULL)
812 /* Set the SMARTCARD DMA Abort callback to Null.
813 No call back execution at end of DMA abort procedure */
814 hsc->hdmarx->XferAbortCallback = NULL;
816 HAL_DMA_Abort(hsc->hdmarx);
820 /* Reset Tx and Rx transfer counters */
821 hsc->TxXferCount = 0x00U;
822 hsc->RxXferCount = 0x00U;
824 /* Reset ErrorCode */
825 hsc->ErrorCode = HAL_SMARTCARD_ERROR_NONE;
827 /* Restore hsc->RxState and hsc->gState to Ready */
828 hsc->RxState = HAL_SMARTCARD_STATE_READY;
829 hsc->gState = HAL_SMARTCARD_STATE_READY;
831 return HAL_OK;
835 * @brief Abort ongoing Transmit transfer (blocking mode).
836 * @param hsc SMARTCARD handle.
837 * @note This procedure could be used for aborting any ongoing transfer started in Interrupt or DMA mode.
838 * This procedure performs following operations :
839 * - Disable PPP Interrupts
840 * - Disable the DMA transfer in the peripheral register (if enabled)
841 * - Abort DMA transfer by calling HAL_DMA_Abort (in case of transfer in DMA mode)
842 * - Set handle State to READY
843 * @note This procedure is executed in blocking mode : when exiting function, Abort is considered as completed.
844 * @retval HAL status
846 HAL_StatusTypeDef HAL_SMARTCARD_AbortTransmit(SMARTCARD_HandleTypeDef *hsc)
848 /* Disable TXEIE and TCIE interrupts */
849 CLEAR_BIT(hsc->Instance->CR1, (USART_CR1_TXEIE | USART_CR1_TCIE));
851 /* Disable the SMARTCARD DMA Tx request if enabled */
852 if(HAL_IS_BIT_SET(hsc->Instance->CR3, USART_CR3_DMAT))
854 CLEAR_BIT(hsc->Instance->CR3, USART_CR3_DMAT);
856 /* Abort the SMARTCARD DMA Tx channel : use blocking DMA Abort API (no callback) */
857 if(hsc->hdmatx != NULL)
859 /* Set the SMARTCARD DMA Abort callback to Null.
860 No call back execution at end of DMA abort procedure */
861 hsc->hdmatx->XferAbortCallback = NULL;
863 HAL_DMA_Abort(hsc->hdmatx);
867 /* Reset Tx transfer counter */
868 hsc->TxXferCount = 0x00U;
870 /* Restore hsc->gState to Ready */
871 hsc->gState = HAL_SMARTCARD_STATE_READY;
873 return HAL_OK;
877 * @brief Abort ongoing Receive transfer (blocking mode).
878 * @param hsc SMARTCARD handle.
879 * @note This procedure could be used for aborting any ongoing transfer started in Interrupt or DMA mode.
880 * This procedure performs following operations :
881 * - Disable PPP Interrupts
882 * - Disable the DMA transfer in the peripheral register (if enabled)
883 * - Abort DMA transfer by calling HAL_DMA_Abort (in case of transfer in DMA mode)
884 * - Set handle State to READY
885 * @note This procedure is executed in blocking mode : when exiting function, Abort is considered as completed.
886 * @retval HAL status
888 HAL_StatusTypeDef HAL_SMARTCARD_AbortReceive(SMARTCARD_HandleTypeDef *hsc)
890 /* Disable RXNE, PE and ERR (Frame error, noise error, overrun error) interrupts */
891 CLEAR_BIT(hsc->Instance->CR1, (USART_CR1_RXNEIE | USART_CR1_PEIE));
892 CLEAR_BIT(hsc->Instance->CR3, USART_CR3_EIE);
894 /* Disable the SMARTCARD DMA Rx request if enabled */
895 if(HAL_IS_BIT_SET(hsc->Instance->CR3, USART_CR3_DMAR))
897 CLEAR_BIT(hsc->Instance->CR3, USART_CR3_DMAR);
899 /* Abort the SMARTCARD DMA Rx channel : use blocking DMA Abort API (no callback) */
900 if(hsc->hdmarx != NULL)
902 /* Set the SMARTCARD DMA Abort callback to Null.
903 No call back execution at end of DMA abort procedure */
904 hsc->hdmarx->XferAbortCallback = NULL;
906 HAL_DMA_Abort(hsc->hdmarx);
910 /* Reset Rx transfer counter */
911 hsc->RxXferCount = 0x00U;
913 /* Restore hsc->RxState to Ready */
914 hsc->RxState = HAL_SMARTCARD_STATE_READY;
916 return HAL_OK;
920 * @brief Abort ongoing transfers (Interrupt mode).
921 * @param hsc SMARTCARD handle.
922 * @note This procedure could be used for aborting any ongoing transfer started in Interrupt or DMA mode.
923 * This procedure performs following operations :
924 * - Disable PPP Interrupts
925 * - Disable the DMA transfer in the peripheral register (if enabled)
926 * - Abort DMA transfer by calling HAL_DMA_Abort_IT (in case of transfer in DMA mode)
927 * - Set handle State to READY
928 * - At abort completion, call user abort complete callback
929 * @note This procedure is executed in Interrupt mode, meaning that abort procedure could be
930 * considered as completed only when user abort complete callback is executed (not when exiting function).
931 * @retval HAL status
933 HAL_StatusTypeDef HAL_SMARTCARD_Abort_IT(SMARTCARD_HandleTypeDef *hsc)
935 uint32_t AbortCplt = 0x01U;
937 /* Disable TXEIE, TCIE, RXNE, PE and ERR (Frame error, noise error, overrun error) interrupts */
938 CLEAR_BIT(hsc->Instance->CR1, (USART_CR1_RXNEIE | USART_CR1_PEIE | USART_CR1_TXEIE | USART_CR1_TCIE));
939 CLEAR_BIT(hsc->Instance->CR3, USART_CR3_EIE);
941 /* If DMA Tx and/or DMA Rx Handles are associated to SMARTCARD Handle, DMA Abort complete callbacks should be initialised
942 before any call to DMA Abort functions */
943 /* DMA Tx Handle is valid */
944 if(hsc->hdmatx != NULL)
946 /* Set DMA Abort Complete callback if SMARTCARD DMA Tx request if enabled.
947 Otherwise, set it to NULL */
948 if(HAL_IS_BIT_SET(hsc->Instance->CR3, USART_CR3_DMAT))
950 hsc->hdmatx->XferAbortCallback = SMARTCARD_DMATxAbortCallback;
952 else
954 hsc->hdmatx->XferAbortCallback = NULL;
957 /* DMA Rx Handle is valid */
958 if(hsc->hdmarx != NULL)
960 /* Set DMA Abort Complete callback if SMARTCARD DMA Rx request if enabled.
961 Otherwise, set it to NULL */
962 if(HAL_IS_BIT_SET(hsc->Instance->CR3, USART_CR3_DMAR))
964 hsc->hdmarx->XferAbortCallback = SMARTCARD_DMARxAbortCallback;
966 else
968 hsc->hdmarx->XferAbortCallback = NULL;
972 /* Disable the SMARTCARD DMA Tx request if enabled */
973 if(HAL_IS_BIT_SET(hsc->Instance->CR3, USART_CR3_DMAT))
975 /* Disable DMA Tx at SMARTCARD level */
976 CLEAR_BIT(hsc->Instance->CR3, USART_CR3_DMAT);
978 /* Abort the SMARTCARD DMA Tx channel : use non blocking DMA Abort API (callback) */
979 if(hsc->hdmatx != NULL)
981 /* SMARTCARD Tx DMA Abort callback has already been initialised :
982 will lead to call HAL_SMARTCARD_AbortCpltCallback() at end of DMA abort procedure */
984 /* Abort DMA TX */
985 if(HAL_DMA_Abort_IT(hsc->hdmatx) != HAL_OK)
987 hsc->hdmatx->XferAbortCallback = NULL;
989 else
991 AbortCplt = 0x00U;
996 /* Disable the SMARTCARD DMA Rx request if enabled */
997 if(HAL_IS_BIT_SET(hsc->Instance->CR3, USART_CR3_DMAR))
999 CLEAR_BIT(hsc->Instance->CR3, USART_CR3_DMAR);
1001 /* Abort the SMARTCARD DMA Rx channel : use non blocking DMA Abort API (callback) */
1002 if(hsc->hdmarx != NULL)
1004 /* SMARTCARD Rx DMA Abort callback has already been initialised :
1005 will lead to call HAL_SMARTCARD_AbortCpltCallback() at end of DMA abort procedure */
1007 /* Abort DMA RX */
1008 if(HAL_DMA_Abort_IT(hsc->hdmarx) != HAL_OK)
1010 hsc->hdmarx->XferAbortCallback = NULL;
1011 AbortCplt = 0x01U;
1013 else
1015 AbortCplt = 0x00U;
1020 /* if no DMA abort complete callback execution is required => call user Abort Complete callback */
1021 if(AbortCplt == 0x01U)
1023 /* Reset Tx and Rx transfer counters */
1024 hsc->TxXferCount = 0x00U;
1025 hsc->RxXferCount = 0x00U;
1027 /* Reset ErrorCode */
1028 hsc->ErrorCode = HAL_SMARTCARD_ERROR_NONE;
1030 /* Restore hsc->gState and hsc->RxState to Ready */
1031 hsc->gState = HAL_SMARTCARD_STATE_READY;
1032 hsc->RxState = HAL_SMARTCARD_STATE_READY;
1034 /* As no DMA to be aborted, call directly user Abort complete callback */
1035 HAL_SMARTCARD_AbortCpltCallback(hsc);
1037 return HAL_OK;
1041 * @brief Abort ongoing Transmit transfer (Interrupt mode).
1042 * @param hsc SMARTCARD handle.
1043 * @note This procedure could be used for aborting any ongoing transfer started in Interrupt or DMA mode.
1044 * This procedure performs following operations :
1045 * - Disable PPP Interrupts
1046 * - Disable the DMA transfer in the peripheral register (if enabled)
1047 * - Abort DMA transfer by calling HAL_DMA_Abort_IT (in case of transfer in DMA mode)
1048 * - Set handle State to READY
1049 * - At abort completion, call user abort complete callback
1050 * @note This procedure is executed in Interrupt mode, meaning that abort procedure could be
1051 * considered as completed only when user abort complete callback is executed (not when exiting function).
1052 * @retval HAL status
1054 HAL_StatusTypeDef HAL_SMARTCARD_AbortTransmit_IT(SMARTCARD_HandleTypeDef *hsc)
1056 /* Disable TXEIE and TCIE interrupts */
1057 CLEAR_BIT(hsc->Instance->CR1, (USART_CR1_TXEIE | USART_CR1_TCIE));
1059 /* Disable the SMARTCARD DMA Tx request if enabled */
1060 if(HAL_IS_BIT_SET(hsc->Instance->CR3, USART_CR3_DMAT))
1062 CLEAR_BIT(hsc->Instance->CR3, USART_CR3_DMAT);
1064 /* Abort the SMARTCARD DMA Tx channel : use blocking DMA Abort API (no callback) */
1065 if(hsc->hdmatx != NULL)
1067 /* Set the SMARTCARD DMA Abort callback :
1068 will lead to call HAL_SMARTCARD_AbortCpltCallback() at end of DMA abort procedure */
1069 hsc->hdmatx->XferAbortCallback = SMARTCARD_DMATxOnlyAbortCallback;
1071 /* Abort DMA TX */
1072 if(HAL_DMA_Abort_IT(hsc->hdmatx) != HAL_OK)
1074 /* Call Directly hsc->hdmatx->XferAbortCallback function in case of error */
1075 hsc->hdmatx->XferAbortCallback(hsc->hdmatx);
1078 else
1080 /* Reset Tx transfer counter */
1081 hsc->TxXferCount = 0x00U;
1083 /* Restore hsc->gState to Ready */
1084 hsc->gState = HAL_SMARTCARD_STATE_READY;
1086 /* As no DMA to be aborted, call directly user Abort complete callback */
1087 HAL_SMARTCARD_AbortTransmitCpltCallback(hsc);
1090 else
1092 /* Reset Tx transfer counter */
1093 hsc->TxXferCount = 0x00U;
1095 /* Restore hsc->gState to Ready */
1096 hsc->gState = HAL_SMARTCARD_STATE_READY;
1098 /* As no DMA to be aborted, call directly user Abort complete callback */
1099 HAL_SMARTCARD_AbortTransmitCpltCallback(hsc);
1102 return HAL_OK;
1106 * @brief Abort ongoing Receive transfer (Interrupt mode).
1107 * @param hsc SMARTCARD handle.
1108 * @note This procedure could be used for aborting any ongoing transfer started in Interrupt or DMA mode.
1109 * This procedure performs following operations :
1110 * - Disable PPP Interrupts
1111 * - Disable the DMA transfer in the peripheral register (if enabled)
1112 * - Abort DMA transfer by calling HAL_DMA_Abort_IT (in case of transfer in DMA mode)
1113 * - Set handle State to READY
1114 * - At abort completion, call user abort complete callback
1115 * @note This procedure is executed in Interrupt mode, meaning that abort procedure could be
1116 * considered as completed only when user abort complete callback is executed (not when exiting function).
1117 * @retval HAL status
1119 HAL_StatusTypeDef HAL_SMARTCARD_AbortReceive_IT(SMARTCARD_HandleTypeDef *hsc)
1121 /* Disable RXNE, PE and ERR (Frame error, noise error, overrun error) interrupts */
1122 CLEAR_BIT(hsc->Instance->CR1, (USART_CR1_RXNEIE | USART_CR1_PEIE));
1123 CLEAR_BIT(hsc->Instance->CR3, USART_CR3_EIE);
1125 /* Disable the SMARTCARD DMA Rx request if enabled */
1126 if(HAL_IS_BIT_SET(hsc->Instance->CR3, USART_CR3_DMAR))
1128 CLEAR_BIT(hsc->Instance->CR3, USART_CR3_DMAR);
1130 /* Abort the SMARTCARD DMA Rx channel : use blocking DMA Abort API (no callback) */
1131 if(hsc->hdmarx != NULL)
1133 /* Set the SMARTCARD DMA Abort callback :
1134 will lead to call HAL_SMARTCARD_AbortCpltCallback() at end of DMA abort procedure */
1135 hsc->hdmarx->XferAbortCallback = SMARTCARD_DMARxOnlyAbortCallback;
1137 /* Abort DMA RX */
1138 if(HAL_DMA_Abort_IT(hsc->hdmarx) != HAL_OK)
1140 /* Call Directly hsc->hdmarx->XferAbortCallback function in case of error */
1141 hsc->hdmarx->XferAbortCallback(hsc->hdmarx);
1144 else
1146 /* Reset Rx transfer counter */
1147 hsc->RxXferCount = 0x00U;
1149 /* Restore hsc->RxState to Ready */
1150 hsc->RxState = HAL_SMARTCARD_STATE_READY;
1152 /* As no DMA to be aborted, call directly user Abort complete callback */
1153 HAL_SMARTCARD_AbortReceiveCpltCallback(hsc);
1156 else
1158 /* Reset Rx transfer counter */
1159 hsc->RxXferCount = 0x00U;
1161 /* Restore hsc->RxState to Ready */
1162 hsc->RxState = HAL_SMARTCARD_STATE_READY;
1164 /* As no DMA to be aborted, call directly user Abort complete callback */
1165 HAL_SMARTCARD_AbortReceiveCpltCallback(hsc);
1168 return HAL_OK;
1172 * @brief This function handles SMARTCARD interrupt request.
1173 * @param hsc: pointer to a SMARTCARD_HandleTypeDef structure that contains
1174 * the configuration information for SMARTCARD module.
1175 * @retval None
1177 void HAL_SMARTCARD_IRQHandler(SMARTCARD_HandleTypeDef *hsc)
1179 uint32_t isrflags = READ_REG(hsc->Instance->SR);
1180 uint32_t cr1its = READ_REG(hsc->Instance->CR1);
1181 uint32_t cr3its = READ_REG(hsc->Instance->CR3);
1182 uint32_t dmarequest = 0x00U;
1183 uint32_t errorflags = 0x00U;
1185 /* If no error occurs */
1186 errorflags = (isrflags & (uint32_t)(USART_SR_PE | USART_SR_FE | USART_SR_ORE | USART_SR_NE));
1187 if(errorflags == RESET)
1189 /* SMARTCARD in mode Receiver -------------------------------------------------*/
1190 if(((isrflags & USART_SR_RXNE) != RESET) && ((cr1its & USART_CR1_RXNEIE) != RESET))
1192 SMARTCARD_Receive_IT(hsc);
1193 return;
1197 /* If some errors occur */
1198 if((errorflags != RESET) && (((cr3its & USART_CR3_EIE) != RESET) || ((cr1its & (USART_CR1_RXNEIE | USART_CR1_PEIE)) != RESET)))
1200 /* SMARTCARD parity error interrupt occurred ---------------------------*/
1201 if(((isrflags & SMARTCARD_FLAG_PE) != RESET) && ((cr1its & USART_CR1_PEIE) != RESET))
1203 hsc->ErrorCode |= HAL_SMARTCARD_ERROR_PE;
1206 /* SMARTCARD frame error interrupt occurred ----------------------------*/
1207 if(((isrflags & SMARTCARD_FLAG_FE) != RESET) && ((cr3its & USART_CR3_EIE) != RESET))
1209 hsc->ErrorCode |= HAL_SMARTCARD_ERROR_FE;
1212 /* SMARTCARD noise error interrupt occurred ----------------------------*/
1213 if(((isrflags & SMARTCARD_FLAG_NE) != RESET) && ((cr3its & USART_CR3_EIE) != RESET))
1215 hsc->ErrorCode |= HAL_SMARTCARD_ERROR_NE;
1218 /* SMARTCARD Over-Run interrupt occurred -------------------------------*/
1219 if(((isrflags & SMARTCARD_FLAG_ORE) != RESET) && ((cr3its & USART_CR3_EIE) != RESET))
1221 hsc->ErrorCode |= HAL_SMARTCARD_ERROR_ORE;
1223 /* Call the Error call Back in case of Errors */
1224 if(hsc->ErrorCode != HAL_SMARTCARD_ERROR_NONE)
1226 /* SMARTCARD in mode Receiver -----------------------------------------------*/
1227 if(((isrflags & USART_SR_RXNE) != RESET) && ((cr1its & USART_CR1_RXNEIE) != RESET))
1229 SMARTCARD_Receive_IT(hsc);
1232 /* If Overrun error occurs, or if any error occurs in DMA mode reception,
1233 consider error as blocking */
1234 dmarequest = HAL_IS_BIT_SET(hsc->Instance->CR3, USART_CR3_DMAR);
1235 if(((hsc->ErrorCode & HAL_SMARTCARD_ERROR_ORE) != RESET) || dmarequest)
1237 /* Blocking error : transfer is aborted
1238 Set the SMARTCARD state ready to be able to start again the process,
1239 Disable Rx Interrupts, and disable Rx DMA request, if ongoing */
1240 SMARTCARD_EndRxTransfer(hsc);
1241 /* Disable the SMARTCARD DMA Rx request if enabled */
1242 if(HAL_IS_BIT_SET(hsc->Instance->CR3, USART_CR3_DMAR))
1244 CLEAR_BIT(hsc->Instance->CR3, USART_CR3_DMAR);
1246 /* Abort the SMARTCARD DMA Rx channel */
1247 if(hsc->hdmarx != NULL)
1249 /* Set the SMARTCARD DMA Abort callback :
1250 will lead to call HAL_SMARTCARD_ErrorCallback() at end of DMA abort procedure */
1251 hsc->hdmarx->XferAbortCallback = SMARTCARD_DMAAbortOnError;
1253 if(HAL_DMA_Abort_IT(hsc->hdmarx) != HAL_OK)
1255 /* Call Directly XferAbortCallback function in case of error */
1256 hsc->hdmarx->XferAbortCallback(hsc->hdmarx);
1259 else
1261 /* Call user error callback */
1262 HAL_SMARTCARD_ErrorCallback(hsc);
1265 else
1267 /* Call user error callback */
1268 HAL_SMARTCARD_ErrorCallback(hsc);
1271 else
1273 /* Call user error callback */
1274 HAL_SMARTCARD_ErrorCallback(hsc);
1275 hsc->ErrorCode = HAL_SMARTCARD_ERROR_NONE;
1278 return;
1279 } /* End if some error occurs */
1281 /* SMARTCARD in mode Transmitter -------------------------------------------*/
1282 if(((isrflags & SMARTCARD_FLAG_TXE) != RESET) && ((cr1its & USART_CR1_TXEIE) != RESET))
1284 SMARTCARD_Transmit_IT(hsc);
1285 return;
1288 /* SMARTCARD in mode Transmitter (transmission end) ------------------------*/
1289 if(((isrflags & SMARTCARD_FLAG_TC) != RESET) && ((cr1its & USART_CR1_TCIE) != RESET))
1291 SMARTCARD_EndTransmit_IT(hsc);
1292 return;
1297 * @brief Tx Transfer completed callbacks
1298 * @param hsc: pointer to a SMARTCARD_HandleTypeDef structure that contains
1299 * the configuration information for SMARTCARD module.
1300 * @retval None
1302 __weak void HAL_SMARTCARD_TxCpltCallback(SMARTCARD_HandleTypeDef *hsc)
1304 /* Prevent unused argument(s) compilation warning */
1305 UNUSED(hsc);
1306 /* NOTE : This function Should not be modified, when the callback is needed,
1307 the HAL_SMARTCARD_TxCpltCallback could be implemented in the user file
1312 * @brief Rx Transfer completed callbacks
1313 * @param hsc: pointer to a SMARTCARD_HandleTypeDef structure that contains
1314 * the configuration information for SMARTCARD module.
1315 * @retval None
1317 __weak void HAL_SMARTCARD_RxCpltCallback(SMARTCARD_HandleTypeDef *hsc)
1319 /* Prevent unused argument(s) compilation warning */
1320 UNUSED(hsc);
1321 /* NOTE : This function Should not be modified, when the callback is needed,
1322 the HAL_SMARTCARD_TxCpltCallback could be implemented in the user file
1327 * @brief SMARTCARD error callbacks
1328 * @param hsc: pointer to a SMARTCARD_HandleTypeDef structure that contains
1329 * the configuration information for SMARTCARD module.
1330 * @retval None
1332 __weak void HAL_SMARTCARD_ErrorCallback(SMARTCARD_HandleTypeDef *hsc)
1334 /* Prevent unused argument(s) compilation warning */
1335 UNUSED(hsc);
1336 /* NOTE : This function Should not be modified, when the callback is needed,
1337 the HAL_SMARTCARD_ErrorCallback could be implemented in the user file
1342 * @brief SMARTCARD Abort Complete callback.
1343 * @param hsc SMARTCARD handle.
1344 * @retval None
1346 __weak void HAL_SMARTCARD_AbortCpltCallback (SMARTCARD_HandleTypeDef *hsc)
1348 /* Prevent unused argument(s) compilation warning */
1349 UNUSED(hsc);
1351 /* NOTE : This function should not be modified, when the callback is needed,
1352 the HAL_SMARTCARD_AbortCpltCallback can be implemented in the user file.
1357 * @brief SMARTCARD Abort Transmit Complete callback.
1358 * @param hsc SMARTCARD handle.
1359 * @retval None
1361 __weak void HAL_SMARTCARD_AbortTransmitCpltCallback (SMARTCARD_HandleTypeDef *hsc)
1363 /* Prevent unused argument(s) compilation warning */
1364 UNUSED(hsc);
1366 /* NOTE : This function should not be modified, when the callback is needed,
1367 the HAL_SMARTCARD_AbortTransmitCpltCallback can be implemented in the user file.
1372 * @brief SMARTCARD Abort ReceiveComplete callback.
1373 * @param hsc SMARTCARD handle.
1374 * @retval None
1376 __weak void HAL_SMARTCARD_AbortReceiveCpltCallback (SMARTCARD_HandleTypeDef *hsc)
1378 /* Prevent unused argument(s) compilation warning */
1379 UNUSED(hsc);
1381 /* NOTE : This function should not be modified, when the callback is needed,
1382 the HAL_SMARTCARD_AbortReceiveCpltCallback can be implemented in the user file.
1387 * @}
1390 /** @defgroup SMARTCARD_Exported_Functions_Group3 Peripheral State and Errors functions
1391 * @brief SMARTCARD State and Errors functions
1393 @verbatim
1394 ===============================================================================
1395 ##### Peripheral State and Errors functions #####
1396 ===============================================================================
1397 [..]
1398 This subsection provides a set of functions allowing to control the SmartCard.
1399 (+) HAL_SMARTCARD_GetState() API can be helpful to check in run-time the state of the SmartCard peripheral.
1400 (+) HAL_SMARTCARD_GetError() check in run-time errors that could be occurred during communication.
1401 @endverbatim
1402 * @{
1406 * @brief return the SMARTCARD state
1407 * @param hsc: pointer to a SMARTCARD_HandleTypeDef structure that contains
1408 * the configuration information for SMARTCARD module.
1409 * @retval HAL state
1411 HAL_SMARTCARD_StateTypeDef HAL_SMARTCARD_GetState(SMARTCARD_HandleTypeDef *hsc)
1413 uint32_t temp1= 0x00U, temp2 = 0x00U;
1414 temp1 = hsc->gState;
1415 temp2 = hsc->RxState;
1417 return (HAL_SMARTCARD_StateTypeDef)(temp1 | temp2);
1421 * @brief Return the SMARTCARD error code
1422 * @param hsc : pointer to a SMARTCARD_HandleTypeDef structure that contains
1423 * the configuration information for the specified SMARTCARD.
1424 * @retval SMARTCARD Error Code
1426 uint32_t HAL_SMARTCARD_GetError(SMARTCARD_HandleTypeDef *hsc)
1428 return hsc->ErrorCode;
1432 * @}
1436 * @brief DMA SMARTCARD transmit process complete callback
1437 * @param hdma: pointer to a DMA_HandleTypeDef structure that contains
1438 * the configuration information for the specified DMA module.
1439 * @retval None
1441 static void SMARTCARD_DMATransmitCplt(DMA_HandleTypeDef *hdma)
1443 SMARTCARD_HandleTypeDef* hsc = ( SMARTCARD_HandleTypeDef* )((DMA_HandleTypeDef* )hdma)->Parent;
1445 hsc->TxXferCount = 0U;
1447 /* Disable the DMA transfer for transmit request by setting the DMAT bit
1448 in the USART CR3 register */
1449 CLEAR_BIT(hsc->Instance->CR3, USART_CR3_DMAT);
1451 /* Enable the SMARTCARD Transmit Complete Interrupt */
1452 SET_BIT(hsc->Instance->CR1, USART_CR1_TCIE);
1456 * @brief DMA SMARTCARD receive process complete callback
1457 * @param hdma: pointer to a DMA_HandleTypeDef structure that contains
1458 * the configuration information for the specified DMA module.
1459 * @retval None
1461 static void SMARTCARD_DMAReceiveCplt(DMA_HandleTypeDef *hdma)
1463 SMARTCARD_HandleTypeDef* hsc = ( SMARTCARD_HandleTypeDef* )((DMA_HandleTypeDef* )hdma)->Parent;
1465 hsc->RxXferCount = 0U;
1467 /* Disable RXNE, PE and ERR (Frame error, noise error, overrun error) interrupts */
1468 CLEAR_BIT(hsc->Instance->CR1, (USART_CR1_RXNEIE | USART_CR1_PEIE));
1469 CLEAR_BIT(hsc->Instance->CR3, USART_CR3_EIE);
1471 /* Disable the DMA transfer for the receiver request by setting the DMAR bit
1472 in the USART CR3 register */
1473 CLEAR_BIT(hsc->Instance->CR3, USART_CR3_DMAR);
1475 /* At end of Rx process, restore hsc->RxState to Ready */
1476 hsc->RxState = HAL_SMARTCARD_STATE_READY;
1478 HAL_SMARTCARD_RxCpltCallback(hsc);
1482 * @brief DMA SMARTCARD communication error callback
1483 * @param hdma: pointer to a DMA_HandleTypeDef structure that contains
1484 * the configuration information for the specified DMA module.
1485 * @retval None
1487 static void SMARTCARD_DMAError(DMA_HandleTypeDef *hdma)
1489 uint32_t dmarequest = 0x00U;
1490 SMARTCARD_HandleTypeDef* hsc = ( SMARTCARD_HandleTypeDef* )((DMA_HandleTypeDef* )hdma)->Parent;
1491 hsc->RxXferCount = 0U;
1492 hsc->TxXferCount = 0U;
1493 hsc->ErrorCode = HAL_SMARTCARD_ERROR_DMA;
1495 /* Stop SMARTCARD DMA Tx request if ongoing */
1496 dmarequest = HAL_IS_BIT_SET(hsc->Instance->CR3, USART_CR3_DMAT);
1497 if((hsc->gState == HAL_SMARTCARD_STATE_BUSY_TX) && dmarequest)
1499 SMARTCARD_EndTxTransfer(hsc);
1502 /* Stop SMARTCARD DMA Rx request if ongoing */
1503 dmarequest = HAL_IS_BIT_SET(hsc->Instance->CR3, USART_CR3_DMAR);
1504 if((hsc->RxState == HAL_SMARTCARD_STATE_BUSY_RX) && dmarequest)
1506 SMARTCARD_EndRxTransfer(hsc);
1509 HAL_SMARTCARD_ErrorCallback(hsc);
1513 * @brief This function handles SMARTCARD Communication Timeout.
1514 * @param hsc: SMARTCARD handle
1515 * @param Flag: specifies the SMARTCARD flag to check.
1516 * @param Status: The new Flag status (SET or RESET).
1517 * @param Timeout: Timeout duration
1518 * @param Tickstart: tick start value
1519 * @retval HAL status
1521 static HAL_StatusTypeDef SMARTCARD_WaitOnFlagUntilTimeout(SMARTCARD_HandleTypeDef *hsc, uint32_t Flag, FlagStatus Status, uint32_t Tickstart, uint32_t Timeout)
1523 /* Wait until flag is set */
1524 while((__HAL_SMARTCARD_GET_FLAG(hsc, Flag) ? SET : RESET) == Status)
1526 /* Check for the Timeout */
1527 if(Timeout != HAL_MAX_DELAY)
1529 if((Timeout == 0U)||((HAL_GetTick() - Tickstart ) > Timeout))
1531 /* Disable TXE and RXNE interrupts for the interrupt process */
1532 CLEAR_BIT(hsc->Instance->CR1, USART_CR1_TXEIE);
1533 CLEAR_BIT(hsc->Instance->CR1, USART_CR1_RXNEIE);
1535 hsc->gState= HAL_SMARTCARD_STATE_READY;
1536 hsc->RxState= HAL_SMARTCARD_STATE_READY;
1538 /* Process Unlocked */
1539 __HAL_UNLOCK(hsc);
1541 return HAL_TIMEOUT;
1545 return HAL_OK;
1549 * @brief End ongoing Tx transfer on SMARTCARD peripheral (following error detection or Transmit completion).
1550 * @param hsc: SMARTCARD handle.
1551 * @retval None
1553 static void SMARTCARD_EndTxTransfer(SMARTCARD_HandleTypeDef *hsc)
1555 /* At end of Tx process, restore hsc->gState to Ready */
1556 hsc->gState = HAL_SMARTCARD_STATE_READY;
1558 /* Disable TXEIE and TCIE interrupts */
1559 CLEAR_BIT(hsc->Instance->CR1, (USART_CR1_TXEIE | USART_CR1_TCIE));
1564 * @brief End ongoing Rx transfer on SMARTCARD peripheral (following error detection or Reception completion).
1565 * @param hsc: SMARTCARD handle.
1566 * @retval None
1568 static void SMARTCARD_EndRxTransfer(SMARTCARD_HandleTypeDef *hsc)
1570 /* At end of Rx process, restore hsc->RxState to Ready */
1571 hsc->RxState = HAL_SMARTCARD_STATE_READY;
1573 /* Disable RXNE, PE and ERR (Frame error, noise error, overrun error) interrupts */
1574 CLEAR_BIT(hsc->Instance->CR1, (USART_CR1_RXNEIE | USART_CR1_PEIE));
1575 CLEAR_BIT(hsc->Instance->CR3, USART_CR3_EIE);
1579 * @brief Send an amount of data in non blocking mode
1580 * @param hsc: pointer to a SMARTCARD_HandleTypeDef structure that contains
1581 * the configuration information for SMARTCARD module.
1582 * @retval HAL status
1584 static HAL_StatusTypeDef SMARTCARD_Transmit_IT(SMARTCARD_HandleTypeDef *hsc)
1586 uint16_t* tmp;
1588 /* Check that a Tx process is ongoing */
1589 if(hsc->gState == HAL_SMARTCARD_STATE_BUSY_TX)
1591 tmp = (uint16_t*) hsc->pTxBuffPtr;
1592 hsc->Instance->DR = (uint16_t)(*tmp & (uint16_t)0x01FF);
1593 hsc->pTxBuffPtr += 1U;
1595 if(--hsc->TxXferCount == 0U)
1597 /* Disable the SMARTCARD Transmit data register empty Interrupt */
1598 CLEAR_BIT(hsc->Instance->CR1, USART_CR1_TXEIE);
1600 /* Enable the SMARTCARD Transmit Complete Interrupt */
1601 SET_BIT(hsc->Instance->CR1, USART_CR1_TCIE);
1604 return HAL_OK;
1606 else
1608 return HAL_BUSY;
1613 * @brief Wraps up transmission in non blocking mode.
1614 * @param hsmartcard: pointer to a SMARTCARD_HandleTypeDef structure that contains
1615 * the configuration information for the specified SMARTCARD module.
1616 * @retval HAL status
1618 static HAL_StatusTypeDef SMARTCARD_EndTransmit_IT(SMARTCARD_HandleTypeDef *hsmartcard)
1620 /* Disable the SMARTCARD Transmit Complete Interrupt */
1621 CLEAR_BIT(hsmartcard->Instance->CR1, USART_CR1_TCIE);
1623 /* Disable the SMARTCARD Error Interrupt: (Frame error, noise error, overrun error) */
1624 CLEAR_BIT(hsmartcard->Instance->CR3, USART_CR3_EIE);
1626 /* Tx process is ended, restore hsmartcard->gState to Ready */
1627 hsmartcard->gState = HAL_SMARTCARD_STATE_READY;
1629 HAL_SMARTCARD_TxCpltCallback(hsmartcard);
1631 return HAL_OK;
1635 * @brief Receive an amount of data in non blocking mode
1636 * @param hsc: pointer to a SMARTCARD_HandleTypeDef structure that contains
1637 * the configuration information for SMARTCARD module.
1638 * @retval HAL status
1640 static HAL_StatusTypeDef SMARTCARD_Receive_IT(SMARTCARD_HandleTypeDef *hsc)
1642 uint16_t* tmp;
1644 /* Check that a Rx process is ongoing */
1645 if(hsc->RxState == HAL_SMARTCARD_STATE_BUSY_RX)
1647 tmp = (uint16_t*) hsc->pRxBuffPtr;
1648 *tmp = (uint8_t)(hsc->Instance->DR & (uint8_t)0x00FF);
1649 hsc->pRxBuffPtr += 1U;
1651 if(--hsc->RxXferCount == 0U)
1653 CLEAR_BIT(hsc->Instance->CR1, USART_CR1_RXNEIE);
1655 /* Disable the SMARTCARD Parity Error Interrupt */
1656 CLEAR_BIT(hsc->Instance->CR1, USART_CR1_PEIE);
1658 /* Disable the SMARTCARD Error Interrupt: (Frame error, noise error, overrun error) */
1659 CLEAR_BIT(hsc->Instance->CR3, USART_CR3_EIE);
1661 /* Rx process is completed, restore hsc->RxState to Ready */
1662 hsc->RxState = HAL_SMARTCARD_STATE_READY;
1664 HAL_SMARTCARD_RxCpltCallback(hsc);
1666 return HAL_OK;
1668 return HAL_OK;
1670 else
1672 return HAL_BUSY;
1677 * @brief DMA SMARTCARD communication abort callback, when initiated by HAL services on Error
1678 * (To be called at end of DMA Abort procedure following error occurrence).
1679 * @param hdma DMA handle.
1680 * @retval None
1682 static void SMARTCARD_DMAAbortOnError(DMA_HandleTypeDef *hdma)
1684 SMARTCARD_HandleTypeDef* hsc = (SMARTCARD_HandleTypeDef* )((DMA_HandleTypeDef* )hdma)->Parent;
1685 hsc->RxXferCount = 0x00U;
1686 hsc->TxXferCount = 0x00U;
1688 HAL_SMARTCARD_ErrorCallback(hsc);
1692 * @brief DMA SMARTCARD Tx communication abort callback, when initiated by user
1693 * (To be called at end of DMA Tx Abort procedure following user abort request).
1694 * @note When this callback is executed, User Abort complete call back is called only if no
1695 * Abort still ongoing for Rx DMA Handle.
1696 * @param hdma DMA handle.
1697 * @retval None
1699 static void SMARTCARD_DMATxAbortCallback(DMA_HandleTypeDef *hdma)
1701 SMARTCARD_HandleTypeDef* hsc = ( SMARTCARD_HandleTypeDef* )((DMA_HandleTypeDef* )hdma)->Parent;
1703 hsc->hdmatx->XferAbortCallback = NULL;
1705 /* Check if an Abort process is still ongoing */
1706 if(hsc->hdmarx != NULL)
1708 if(hsc->hdmarx->XferAbortCallback != NULL)
1710 return;
1714 /* No Abort process still ongoing : All DMA channels are aborted, call user Abort Complete callback */
1715 hsc->TxXferCount = 0x00U;
1716 hsc->RxXferCount = 0x00U;
1718 /* Reset ErrorCode */
1719 hsc->ErrorCode = HAL_SMARTCARD_ERROR_NONE;
1721 /* Restore hsc->gState and hsc->RxState to Ready */
1722 hsc->gState = HAL_SMARTCARD_STATE_READY;
1723 hsc->RxState = HAL_SMARTCARD_STATE_READY;
1725 /* Call user Abort complete callback */
1726 HAL_SMARTCARD_AbortCpltCallback(hsc);
1730 * @brief DMA SMARTCARD Rx communication abort callback, when initiated by user
1731 * (To be called at end of DMA Rx Abort procedure following user abort request).
1732 * @note When this callback is executed, User Abort complete call back is called only if no
1733 * Abort still ongoing for Tx DMA Handle.
1734 * @param hdma DMA handle.
1735 * @retval None
1737 static void SMARTCARD_DMARxAbortCallback(DMA_HandleTypeDef *hdma)
1739 SMARTCARD_HandleTypeDef* hsc = ( SMARTCARD_HandleTypeDef* )((DMA_HandleTypeDef* )hdma)->Parent;
1741 hsc->hdmarx->XferAbortCallback = NULL;
1743 /* Check if an Abort process is still ongoing */
1744 if(hsc->hdmatx != NULL)
1746 if(hsc->hdmatx->XferAbortCallback != NULL)
1748 return;
1752 /* No Abort process still ongoing : All DMA channels are aborted, call user Abort Complete callback */
1753 hsc->TxXferCount = 0x00U;
1754 hsc->RxXferCount = 0x00U;
1756 /* Reset ErrorCode */
1757 hsc->ErrorCode = HAL_SMARTCARD_ERROR_NONE;
1759 /* Restore hsc->gState and hsc->RxState to Ready */
1760 hsc->gState = HAL_SMARTCARD_STATE_READY;
1761 hsc->RxState = HAL_SMARTCARD_STATE_READY;
1763 /* Call user Abort complete callback */
1764 HAL_SMARTCARD_AbortCpltCallback(hsc);
1768 * @brief DMA SMARTCARD Tx communication abort callback, when initiated by user by a call to
1769 * HAL_SMARTCARD_AbortTransmit_IT API (Abort only Tx transfer)
1770 * (This callback is executed at end of DMA Tx Abort procedure following user abort request,
1771 * and leads to user Tx Abort Complete callback execution).
1772 * @param hdma DMA handle.
1773 * @retval None
1775 static void SMARTCARD_DMATxOnlyAbortCallback(DMA_HandleTypeDef *hdma)
1777 SMARTCARD_HandleTypeDef* hsc = ( SMARTCARD_HandleTypeDef* )((DMA_HandleTypeDef* )hdma)->Parent;
1779 hsc->TxXferCount = 0x00U;
1781 /* Restore hsc->gState to Ready */
1782 hsc->gState = HAL_SMARTCARD_STATE_READY;
1784 /* Call user Abort complete callback */
1785 HAL_SMARTCARD_AbortTransmitCpltCallback(hsc);
1789 * @brief DMA SMARTCARD Rx communication abort callback, when initiated by user by a call to
1790 * HAL_SMARTCARD_AbortReceive_IT API (Abort only Rx transfer)
1791 * (This callback is executed at end of DMA Rx Abort procedure following user abort request,
1792 * and leads to user Rx Abort Complete callback execution).
1793 * @param hdma DMA handle.
1794 * @retval None
1796 static void SMARTCARD_DMARxOnlyAbortCallback(DMA_HandleTypeDef *hdma)
1798 SMARTCARD_HandleTypeDef* hsc = ( SMARTCARD_HandleTypeDef* )((DMA_HandleTypeDef* )hdma)->Parent;
1800 hsc->RxXferCount = 0x00U;
1802 /* Restore hsc->RxState to Ready */
1803 hsc->RxState = HAL_SMARTCARD_STATE_READY;
1805 /* Call user Abort complete callback */
1806 HAL_SMARTCARD_AbortReceiveCpltCallback(hsc);
1810 * @brief Configure the SMARTCARD peripheral
1811 * @param hsc: pointer to a SMARTCARD_HandleTypeDef structure that contains
1812 * the configuration information for SMARTCARD module.
1813 * @retval None
1815 static void SMARTCARD_SetConfig(SMARTCARD_HandleTypeDef *hsc)
1817 uint32_t tmpreg = 0x00U;
1819 /* Check the parameters */
1820 assert_param(IS_SMARTCARD_INSTANCE(hsc->Instance));
1821 assert_param(IS_SMARTCARD_POLARITY(hsc->Init.CLKPolarity));
1822 assert_param(IS_SMARTCARD_PHASE(hsc->Init.CLKPhase));
1823 assert_param(IS_SMARTCARD_LASTBIT(hsc->Init.CLKLastBit));
1824 assert_param(IS_SMARTCARD_BAUDRATE(hsc->Init.BaudRate));
1825 assert_param(IS_SMARTCARD_WORD_LENGTH(hsc->Init.WordLength));
1826 assert_param(IS_SMARTCARD_STOPBITS(hsc->Init.StopBits));
1827 assert_param(IS_SMARTCARD_PARITY(hsc->Init.Parity));
1828 assert_param(IS_SMARTCARD_MODE(hsc->Init.Mode));
1829 assert_param(IS_SMARTCARD_NACK_STATE(hsc->Init.NACKState));
1831 /* The LBCL, CPOL and CPHA bits have to be selected when both the transmitter and the
1832 receiver are disabled (TE=RE=0) to ensure that the clock pulses function correctly. */
1833 CLEAR_BIT(hsc->Instance->CR1, (USART_CR1_TE | USART_CR1_RE));
1835 /*---------------------------- USART CR2 Configuration ---------------------*/
1836 tmpreg = hsc->Instance->CR2;
1837 /* Clear CLKEN, CPOL, CPHA and LBCL bits */
1838 tmpreg &= (uint32_t)~((uint32_t)(USART_CR2_CPHA | USART_CR2_CPOL | USART_CR2_CLKEN | USART_CR2_LBCL));
1839 /* Configure the SMARTCARD Clock, CPOL, CPHA and LastBit -----------------------*/
1840 /* Set CPOL bit according to hsc->Init.CLKPolarity value */
1841 /* Set CPHA bit according to hsc->Init.CLKPhase value */
1842 /* Set LBCL bit according to hsc->Init.CLKLastBit value */
1843 /* Set Stop Bits: Set STOP[13:12] bits according to hsc->Init.StopBits value */
1844 tmpreg |= (uint32_t)(USART_CR2_CLKEN | hsc->Init.CLKPolarity |
1845 hsc->Init.CLKPhase| hsc->Init.CLKLastBit | hsc->Init.StopBits);
1846 /* Write to USART CR2 */
1847 WRITE_REG(hsc->Instance->CR2, (uint32_t)tmpreg);
1849 tmpreg = hsc->Instance->CR2;
1851 /* Clear STOP[13:12] bits */
1852 tmpreg &= (uint32_t)~((uint32_t)USART_CR2_STOP);
1854 /* Set Stop Bits: Set STOP[13:12] bits according to hsc->Init.StopBits value */
1855 tmpreg |= (uint32_t)(hsc->Init.StopBits);
1857 /* Write to USART CR2 */
1858 WRITE_REG(hsc->Instance->CR2, (uint32_t)tmpreg);
1860 /*-------------------------- USART CR1 Configuration -----------------------*/
1861 tmpreg = hsc->Instance->CR1;
1863 /* Clear M, PCE, PS, TE and RE bits */
1864 tmpreg &= (uint32_t)~((uint32_t)(USART_CR1_M | USART_CR1_PCE | USART_CR1_PS | USART_CR1_TE | \
1865 USART_CR1_RE));
1867 /* Configure the SMARTCARD Word Length, Parity and mode:
1868 Set the M bits according to hsc->Init.WordLength value
1869 Set PCE and PS bits according to hsc->Init.Parity value
1870 Set TE and RE bits according to hsc->Init.Mode value */
1871 tmpreg |= (uint32_t)hsc->Init.WordLength | hsc->Init.Parity | hsc->Init.Mode;
1873 /* Write to USART CR1 */
1874 WRITE_REG(hsc->Instance->CR1, (uint32_t)tmpreg);
1876 /*-------------------------- USART CR3 Configuration -----------------------*/
1877 /* Clear CTSE and RTSE bits */
1878 CLEAR_BIT(hsc->Instance->CR3, (USART_CR3_RTSE | USART_CR3_CTSE));
1880 /*-------------------------- USART BRR Configuration -----------------------*/
1881 #if defined(USART6)
1882 if((hsc->Instance == USART1) || (hsc->Instance == USART6))
1884 hsc->Instance->BRR = SMARTCARD_BRR(HAL_RCC_GetPCLK2Freq(), hsc->Init.BaudRate);
1886 #else
1887 if(hsc->Instance == USART1)
1889 hsc->Instance->BRR = SMARTCARD_BRR(HAL_RCC_GetPCLK2Freq(), hsc->Init.BaudRate);
1891 #endif /* USART6 */
1892 else
1894 hsc->Instance->BRR = SMARTCARD_BRR(HAL_RCC_GetPCLK1Freq(), hsc->Init.BaudRate);
1899 * @}
1902 #endif /* HAL_SMARTCARD_MODULE_ENABLED */
1904 * @}
1908 * @}
1911 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/