001

Committer:
ganlikun
Date:
Sun Jun 12 14:02:44 2022 +0000
Revision:
0:13413ea9a877
00

Who changed what in which revision?

UserRevisionLine numberNew contents of line
ganlikun 0:13413ea9a877 1 /**
ganlikun 0:13413ea9a877 2 ******************************************************************************
ganlikun 0:13413ea9a877 3 * @file stm32f4xx_hal_smartcard.c
ganlikun 0:13413ea9a877 4 * @author MCD Application Team
ganlikun 0:13413ea9a877 5 * @version V1.7.1
ganlikun 0:13413ea9a877 6 * @date 14-April-2017
ganlikun 0:13413ea9a877 7 * @brief SMARTCARD HAL module driver.
ganlikun 0:13413ea9a877 8 * This file provides firmware functions to manage the following
ganlikun 0:13413ea9a877 9 * functionalities of the SMARTCARD peripheral:
ganlikun 0:13413ea9a877 10 * + Initialization and de-initialization functions
ganlikun 0:13413ea9a877 11 * + IO operation functions
ganlikun 0:13413ea9a877 12 * + Peripheral State and Errors functions
ganlikun 0:13413ea9a877 13 *
ganlikun 0:13413ea9a877 14 @verbatim
ganlikun 0:13413ea9a877 15 ==============================================================================
ganlikun 0:13413ea9a877 16 ##### How to use this driver #####
ganlikun 0:13413ea9a877 17 ==============================================================================
ganlikun 0:13413ea9a877 18 [..]
ganlikun 0:13413ea9a877 19 The SMARTCARD HAL driver can be used as follows:
ganlikun 0:13413ea9a877 20
ganlikun 0:13413ea9a877 21 (#) Declare a SMARTCARD_HandleTypeDef handle structure.
ganlikun 0:13413ea9a877 22 (#) Initialize the SMARTCARD low level resources by implementing the HAL_SMARTCARD_MspInit() API:
ganlikun 0:13413ea9a877 23 (##) Enable the USARTx interface clock.
ganlikun 0:13413ea9a877 24 (##) SMARTCARD pins configuration:
ganlikun 0:13413ea9a877 25 (+++) Enable the clock for the SMARTCARD GPIOs.
ganlikun 0:13413ea9a877 26 (+++) Configure these SMARTCARD pins as alternate function pull-up.
ganlikun 0:13413ea9a877 27 (##) NVIC configuration if you need to use interrupt process (HAL_SMARTCARD_Transmit_IT()
ganlikun 0:13413ea9a877 28 and HAL_SMARTCARD_Receive_IT() APIs):
ganlikun 0:13413ea9a877 29 (+++) Configure the USARTx interrupt priority.
ganlikun 0:13413ea9a877 30 (+++) Enable the NVIC USART IRQ handle.
ganlikun 0:13413ea9a877 31 (##) DMA Configuration if you need to use DMA process (HAL_SMARTCARD_Transmit_DMA()
ganlikun 0:13413ea9a877 32 and HAL_SMARTCARD_Receive_DMA() APIs):
ganlikun 0:13413ea9a877 33 (+++) Declare a DMA handle structure for the Tx/Rx stream.
ganlikun 0:13413ea9a877 34 (+++) Enable the DMAx interface clock.
ganlikun 0:13413ea9a877 35 (+++) Configure the declared DMA handle structure with the required Tx/Rx parameters.
ganlikun 0:13413ea9a877 36 (+++) Configure the DMA Tx/Rx Stream.
ganlikun 0:13413ea9a877 37 (+++) Associate the initialized DMA handle to the SMARTCARD DMA Tx/Rx handle.
ganlikun 0:13413ea9a877 38 (+++) Configure the priority and enable the NVIC for the transfer complete interrupt on the DMA Tx/Rx Stream.
ganlikun 0:13413ea9a877 39
ganlikun 0:13413ea9a877 40 (#) Program the Baud Rate, Word Length , Stop Bit, Parity, Hardware
ganlikun 0:13413ea9a877 41 flow control and Mode(Receiver/Transmitter) in the SMARTCARD Init structure.
ganlikun 0:13413ea9a877 42
ganlikun 0:13413ea9a877 43 (#) Initialize the SMARTCARD registers by calling the HAL_SMARTCARD_Init() API:
ganlikun 0:13413ea9a877 44 (++) These APIs configure also the low level Hardware GPIO, CLOCK, CORTEX...etc)
ganlikun 0:13413ea9a877 45 by calling the customized HAL_SMARTCARD_MspInit() API.
ganlikun 0:13413ea9a877 46 [..]
ganlikun 0:13413ea9a877 47 (@) The specific SMARTCARD interrupts (Transmission complete interrupt,
ganlikun 0:13413ea9a877 48 RXNE interrupt and Error Interrupts) will be managed using the macros
ganlikun 0:13413ea9a877 49 __HAL_SMARTCARD_ENABLE_IT() and __HAL_SMARTCARD_DISABLE_IT() inside the transmit and receive process.
ganlikun 0:13413ea9a877 50
ganlikun 0:13413ea9a877 51 [..]
ganlikun 0:13413ea9a877 52 Three operation modes are available within this driver :
ganlikun 0:13413ea9a877 53
ganlikun 0:13413ea9a877 54 *** Polling mode IO operation ***
ganlikun 0:13413ea9a877 55 =================================
ganlikun 0:13413ea9a877 56 [..]
ganlikun 0:13413ea9a877 57 (+) Send an amount of data in blocking mode using HAL_SMARTCARD_Transmit()
ganlikun 0:13413ea9a877 58 (+) Receive an amount of data in blocking mode using HAL_SMARTCARD_Receive()
ganlikun 0:13413ea9a877 59
ganlikun 0:13413ea9a877 60 *** Interrupt mode IO operation ***
ganlikun 0:13413ea9a877 61 ===================================
ganlikun 0:13413ea9a877 62 [..]
ganlikun 0:13413ea9a877 63 (+) Send an amount of data in non blocking mode using HAL_SMARTCARD_Transmit_IT()
ganlikun 0:13413ea9a877 64 (+) At transmission end of transfer HAL_SMARTCARD_TxCpltCallback is executed and user can
ganlikun 0:13413ea9a877 65 add his own code by customization of function pointer HAL_SMARTCARD_TxCpltCallback
ganlikun 0:13413ea9a877 66 (+) Receive an amount of data in non blocking mode using HAL_SMARTCARD_Receive_IT()
ganlikun 0:13413ea9a877 67 (+) At reception end of transfer HAL_SMARTCARD_RxCpltCallback is executed and user can
ganlikun 0:13413ea9a877 68 add his own code by customization of function pointer HAL_SMARTCARD_RxCpltCallback
ganlikun 0:13413ea9a877 69 (+) In case of transfer Error, HAL_SMARTCARD_ErrorCallback() function is executed and user can
ganlikun 0:13413ea9a877 70 add his own code by customization of function pointer HAL_SMARTCARD_ErrorCallback
ganlikun 0:13413ea9a877 71
ganlikun 0:13413ea9a877 72 *** DMA mode IO operation ***
ganlikun 0:13413ea9a877 73 ==============================
ganlikun 0:13413ea9a877 74 [..]
ganlikun 0:13413ea9a877 75 (+) Send an amount of data in non blocking mode (DMA) using HAL_SMARTCARD_Transmit_DMA()
ganlikun 0:13413ea9a877 76 (+) At transmission end of transfer HAL_SMARTCARD_TxCpltCallback is executed and user can
ganlikun 0:13413ea9a877 77 add his own code by customization of function pointer HAL_SMARTCARD_TxCpltCallback
ganlikun 0:13413ea9a877 78 (+) Receive an amount of data in non blocking mode (DMA) using HAL_SMARTCARD_Receive_DMA()
ganlikun 0:13413ea9a877 79 (+) At reception end of transfer HAL_SMARTCARD_RxCpltCallback is executed and user can
ganlikun 0:13413ea9a877 80 add his own code by customization of function pointer HAL_SMARTCARD_RxCpltCallback
ganlikun 0:13413ea9a877 81 (+) In case of transfer Error, HAL_SMARTCARD_ErrorCallback() function is executed and user can
ganlikun 0:13413ea9a877 82 add his own code by customization of function pointer HAL_SMARTCARD_ErrorCallback
ganlikun 0:13413ea9a877 83
ganlikun 0:13413ea9a877 84 *** SMARTCARD HAL driver macros list ***
ganlikun 0:13413ea9a877 85 =============================================
ganlikun 0:13413ea9a877 86 [..]
ganlikun 0:13413ea9a877 87 Below the list of most used macros in SMARTCARD HAL driver.
ganlikun 0:13413ea9a877 88
ganlikun 0:13413ea9a877 89 (+) __HAL_SMARTCARD_ENABLE: Enable the SMARTCARD peripheral
ganlikun 0:13413ea9a877 90 (+) __HAL_SMARTCARD_DISABLE: Disable the SMARTCARD peripheral
ganlikun 0:13413ea9a877 91 (+) __HAL_SMARTCARD_GET_FLAG : Check whether the specified SMARTCARD flag is set or not
ganlikun 0:13413ea9a877 92 (+) __HAL_SMARTCARD_CLEAR_FLAG : Clear the specified SMARTCARD pending flag
ganlikun 0:13413ea9a877 93 (+) __HAL_SMARTCARD_ENABLE_IT: Enable the specified SMARTCARD interrupt
ganlikun 0:13413ea9a877 94 (+) __HAL_SMARTCARD_DISABLE_IT: Disable the specified SMARTCARD interrupt
ganlikun 0:13413ea9a877 95
ganlikun 0:13413ea9a877 96 [..]
ganlikun 0:13413ea9a877 97 (@) You can refer to the SMARTCARD HAL driver header file for more useful macros
ganlikun 0:13413ea9a877 98
ganlikun 0:13413ea9a877 99 @endverbatim
ganlikun 0:13413ea9a877 100 ******************************************************************************
ganlikun 0:13413ea9a877 101 * @attention
ganlikun 0:13413ea9a877 102 *
ganlikun 0:13413ea9a877 103 * <h2><center>&copy; COPYRIGHT(c) 2017 STMicroelectronics</center></h2>
ganlikun 0:13413ea9a877 104 *
ganlikun 0:13413ea9a877 105 * Redistribution and use in source and binary forms, with or without modification,
ganlikun 0:13413ea9a877 106 * are permitted provided that the following conditions are met:
ganlikun 0:13413ea9a877 107 * 1. Redistributions of source code must retain the above copyright notice,
ganlikun 0:13413ea9a877 108 * this list of conditions and the following disclaimer.
ganlikun 0:13413ea9a877 109 * 2. Redistributions in binary form must reproduce the above copyright notice,
ganlikun 0:13413ea9a877 110 * this list of conditions and the following disclaimer in the documentation
ganlikun 0:13413ea9a877 111 * and/or other materials provided with the distribution.
ganlikun 0:13413ea9a877 112 * 3. Neither the name of STMicroelectronics nor the names of its contributors
ganlikun 0:13413ea9a877 113 * may be used to endorse or promote products derived from this software
ganlikun 0:13413ea9a877 114 * without specific prior written permission.
ganlikun 0:13413ea9a877 115 *
ganlikun 0:13413ea9a877 116 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
ganlikun 0:13413ea9a877 117 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
ganlikun 0:13413ea9a877 118 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
ganlikun 0:13413ea9a877 119 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
ganlikun 0:13413ea9a877 120 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
ganlikun 0:13413ea9a877 121 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
ganlikun 0:13413ea9a877 122 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
ganlikun 0:13413ea9a877 123 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
ganlikun 0:13413ea9a877 124 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
ganlikun 0:13413ea9a877 125 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
ganlikun 0:13413ea9a877 126 *
ganlikun 0:13413ea9a877 127 ******************************************************************************
ganlikun 0:13413ea9a877 128 */
ganlikun 0:13413ea9a877 129
ganlikun 0:13413ea9a877 130 /* Includes ------------------------------------------------------------------*/
ganlikun 0:13413ea9a877 131 #include "stm32f4xx_hal.h"
ganlikun 0:13413ea9a877 132
ganlikun 0:13413ea9a877 133 /** @addtogroup STM32F4xx_HAL_Driver
ganlikun 0:13413ea9a877 134 * @{
ganlikun 0:13413ea9a877 135 */
ganlikun 0:13413ea9a877 136
ganlikun 0:13413ea9a877 137 /** @defgroup SMARTCARD SMARTCARD
ganlikun 0:13413ea9a877 138 * @brief HAL USART SMARTCARD module driver
ganlikun 0:13413ea9a877 139 * @{
ganlikun 0:13413ea9a877 140 */
ganlikun 0:13413ea9a877 141 #ifdef HAL_SMARTCARD_MODULE_ENABLED
ganlikun 0:13413ea9a877 142 /* Private typedef -----------------------------------------------------------*/
ganlikun 0:13413ea9a877 143 /* Private define ------------------------------------------------------------*/
ganlikun 0:13413ea9a877 144 /** @addtogroup SMARTCARD_Private_Constants
ganlikun 0:13413ea9a877 145 * @{
ganlikun 0:13413ea9a877 146 */
ganlikun 0:13413ea9a877 147 /**
ganlikun 0:13413ea9a877 148 * @}
ganlikun 0:13413ea9a877 149 */
ganlikun 0:13413ea9a877 150 /* Private macro -------------------------------------------------------------*/
ganlikun 0:13413ea9a877 151 /* Private variables ---------------------------------------------------------*/
ganlikun 0:13413ea9a877 152 /* Private function prototypes -----------------------------------------------*/
ganlikun 0:13413ea9a877 153 /** @addtogroup SMARTCARD_Private_Functions
ganlikun 0:13413ea9a877 154 * @{
ganlikun 0:13413ea9a877 155 */
ganlikun 0:13413ea9a877 156 static void SMARTCARD_EndTxTransfer(SMARTCARD_HandleTypeDef *hsc);
ganlikun 0:13413ea9a877 157 static void SMARTCARD_EndRxTransfer(SMARTCARD_HandleTypeDef *hsc);
ganlikun 0:13413ea9a877 158 static void SMARTCARD_SetConfig (SMARTCARD_HandleTypeDef *hsc);
ganlikun 0:13413ea9a877 159 static HAL_StatusTypeDef SMARTCARD_Transmit_IT(SMARTCARD_HandleTypeDef *hsc);
ganlikun 0:13413ea9a877 160 static HAL_StatusTypeDef SMARTCARD_EndTransmit_IT(SMARTCARD_HandleTypeDef *hsmartcard);
ganlikun 0:13413ea9a877 161 static HAL_StatusTypeDef SMARTCARD_Receive_IT(SMARTCARD_HandleTypeDef *hsc);
ganlikun 0:13413ea9a877 162 static void SMARTCARD_DMATransmitCplt(DMA_HandleTypeDef *hdma);
ganlikun 0:13413ea9a877 163 static void SMARTCARD_DMAReceiveCplt(DMA_HandleTypeDef *hdma);
ganlikun 0:13413ea9a877 164 static void SMARTCARD_DMAError(DMA_HandleTypeDef *hdma);
ganlikun 0:13413ea9a877 165 static void SMARTCARD_DMAAbortOnError(DMA_HandleTypeDef *hdma);
ganlikun 0:13413ea9a877 166 static void SMARTCARD_DMATxAbortCallback(DMA_HandleTypeDef *hdma);
ganlikun 0:13413ea9a877 167 static void SMARTCARD_DMARxAbortCallback(DMA_HandleTypeDef *hdma);
ganlikun 0:13413ea9a877 168 static void SMARTCARD_DMATxOnlyAbortCallback(DMA_HandleTypeDef *hdma);
ganlikun 0:13413ea9a877 169 static void SMARTCARD_DMARxOnlyAbortCallback(DMA_HandleTypeDef *hdma);
ganlikun 0:13413ea9a877 170 static HAL_StatusTypeDef SMARTCARD_WaitOnFlagUntilTimeout(SMARTCARD_HandleTypeDef *hsc, uint32_t Flag, FlagStatus Status, uint32_t Tickstart, uint32_t Timeout);
ganlikun 0:13413ea9a877 171 /**
ganlikun 0:13413ea9a877 172 * @}
ganlikun 0:13413ea9a877 173 */
ganlikun 0:13413ea9a877 174 /* Exported functions --------------------------------------------------------*/
ganlikun 0:13413ea9a877 175 /** @defgroup SMARTCARD_Exported_Functions SMARTCARD Exported Functions
ganlikun 0:13413ea9a877 176 * @{
ganlikun 0:13413ea9a877 177 */
ganlikun 0:13413ea9a877 178
ganlikun 0:13413ea9a877 179 /** @defgroup SMARTCARD_Exported_Functions_Group1 SmartCard Initialization and de-initialization functions
ganlikun 0:13413ea9a877 180 * @brief Initialization and Configuration functions
ganlikun 0:13413ea9a877 181 *
ganlikun 0:13413ea9a877 182 @verbatim
ganlikun 0:13413ea9a877 183 ==============================================================================
ganlikun 0:13413ea9a877 184 ##### Initialization and Configuration functions #####
ganlikun 0:13413ea9a877 185 ==============================================================================
ganlikun 0:13413ea9a877 186 [..]
ganlikun 0:13413ea9a877 187 This subsection provides a set of functions allowing to initialize the USART
ganlikun 0:13413ea9a877 188 in Smartcard mode.
ganlikun 0:13413ea9a877 189 [..]
ganlikun 0:13413ea9a877 190 The Smartcard interface is designed to support asynchronous protocol Smartcards as
ganlikun 0:13413ea9a877 191 defined in the ISO 7816-3 standard.
ganlikun 0:13413ea9a877 192 [..]
ganlikun 0:13413ea9a877 193 The USART can provide a clock to the smartcard through the SCLK output.
ganlikun 0:13413ea9a877 194 In smartcard mode, SCLK is not associated to the communication but is simply derived
ganlikun 0:13413ea9a877 195 from the internal peripheral input clock through a 5-bit prescaler.
ganlikun 0:13413ea9a877 196 [..]
ganlikun 0:13413ea9a877 197 (+) For the Smartcard mode only these parameters can be configured:
ganlikun 0:13413ea9a877 198 (++) Baud Rate
ganlikun 0:13413ea9a877 199 (++) Word Length => Should be 9 bits (8 bits + parity)
ganlikun 0:13413ea9a877 200 (++) Stop Bit
ganlikun 0:13413ea9a877 201 (++) Parity: => Should be enabled
ganlikun 0:13413ea9a877 202 (++) USART polarity
ganlikun 0:13413ea9a877 203 (++) USART phase
ganlikun 0:13413ea9a877 204 (++) USART LastBit
ganlikun 0:13413ea9a877 205 (++) Receiver/transmitter modes
ganlikun 0:13413ea9a877 206 (++) Prescaler
ganlikun 0:13413ea9a877 207 (++) GuardTime
ganlikun 0:13413ea9a877 208 (++) NACKState: The Smartcard NACK state
ganlikun 0:13413ea9a877 209
ganlikun 0:13413ea9a877 210 (+) Recommended SmartCard interface configuration to get the Answer to Reset from the Card:
ganlikun 0:13413ea9a877 211 (++) Word Length = 9 Bits
ganlikun 0:13413ea9a877 212 (++) 1.5 Stop Bit
ganlikun 0:13413ea9a877 213 (++) Even parity
ganlikun 0:13413ea9a877 214 (++) BaudRate = 12096 baud
ganlikun 0:13413ea9a877 215 (++) Tx and Rx enabled
ganlikun 0:13413ea9a877 216 [..]
ganlikun 0:13413ea9a877 217 Please refer to the ISO 7816-3 specification for more details.
ganlikun 0:13413ea9a877 218
ganlikun 0:13413ea9a877 219 [..]
ganlikun 0:13413ea9a877 220 (@) It is also possible to choose 0.5 stop bit for receiving but it is recommended
ganlikun 0:13413ea9a877 221 to use 1.5 stop bits for both transmitting and receiving to avoid switching
ganlikun 0:13413ea9a877 222 between the two configurations.
ganlikun 0:13413ea9a877 223 [..]
ganlikun 0:13413ea9a877 224 The HAL_SMARTCARD_Init() function follows the USART SmartCard configuration
ganlikun 0:13413ea9a877 225 procedure (details for the procedure are available in reference manual (RM0329)).
ganlikun 0:13413ea9a877 226
ganlikun 0:13413ea9a877 227 @endverbatim
ganlikun 0:13413ea9a877 228
ganlikun 0:13413ea9a877 229 The SMARTCARD frame format is given in the following table:
ganlikun 0:13413ea9a877 230 +-------------------------------------------------------------+
ganlikun 0:13413ea9a877 231 | M bit | PCE bit | SMARTCARD frame |
ganlikun 0:13413ea9a877 232 |---------------------|---------------------------------------|
ganlikun 0:13413ea9a877 233 | 1 | 1 | | SB | 8 bit data | PB | STB | |
ganlikun 0:13413ea9a877 234 +-------------------------------------------------------------+
ganlikun 0:13413ea9a877 235 * @{
ganlikun 0:13413ea9a877 236 */
ganlikun 0:13413ea9a877 237
ganlikun 0:13413ea9a877 238 /**
ganlikun 0:13413ea9a877 239 * @brief Initializes the SmartCard mode according to the specified
ganlikun 0:13413ea9a877 240 * parameters in the SMARTCARD_InitTypeDef and create the associated handle .
ganlikun 0:13413ea9a877 241 * @param hsc: pointer to a SMARTCARD_HandleTypeDef structure that contains
ganlikun 0:13413ea9a877 242 * the configuration information for SMARTCARD module.
ganlikun 0:13413ea9a877 243 * @retval HAL status
ganlikun 0:13413ea9a877 244 */
ganlikun 0:13413ea9a877 245 HAL_StatusTypeDef HAL_SMARTCARD_Init(SMARTCARD_HandleTypeDef *hsc)
ganlikun 0:13413ea9a877 246 {
ganlikun 0:13413ea9a877 247 /* Check the SMARTCARD handle allocation */
ganlikun 0:13413ea9a877 248 if(hsc == NULL)
ganlikun 0:13413ea9a877 249 {
ganlikun 0:13413ea9a877 250 return HAL_ERROR;
ganlikun 0:13413ea9a877 251 }
ganlikun 0:13413ea9a877 252
ganlikun 0:13413ea9a877 253 /* Check the parameters */
ganlikun 0:13413ea9a877 254 assert_param(IS_SMARTCARD_INSTANCE(hsc->Instance));
ganlikun 0:13413ea9a877 255 assert_param(IS_SMARTCARD_NACK_STATE(hsc->Init.NACKState));
ganlikun 0:13413ea9a877 256
ganlikun 0:13413ea9a877 257 if(hsc->gState == HAL_SMARTCARD_STATE_RESET)
ganlikun 0:13413ea9a877 258 {
ganlikun 0:13413ea9a877 259 /* Allocate lock resource and initialize it */
ganlikun 0:13413ea9a877 260 hsc->Lock = HAL_UNLOCKED;
ganlikun 0:13413ea9a877 261 /* Init the low level hardware : GPIO, CLOCK, CORTEX...etc */
ganlikun 0:13413ea9a877 262 HAL_SMARTCARD_MspInit(hsc);
ganlikun 0:13413ea9a877 263 }
ganlikun 0:13413ea9a877 264
ganlikun 0:13413ea9a877 265 hsc->gState = HAL_SMARTCARD_STATE_BUSY;
ganlikun 0:13413ea9a877 266
ganlikun 0:13413ea9a877 267 /* Set the Prescaler */
ganlikun 0:13413ea9a877 268 MODIFY_REG(hsc->Instance->GTPR, USART_GTPR_PSC, hsc->Init.Prescaler);
ganlikun 0:13413ea9a877 269
ganlikun 0:13413ea9a877 270 /* Set the Guard Time */
ganlikun 0:13413ea9a877 271 MODIFY_REG(hsc->Instance->GTPR, USART_GTPR_GT, ((hsc->Init.GuardTime)<<8U));
ganlikun 0:13413ea9a877 272
ganlikun 0:13413ea9a877 273 /* Set the Smartcard Communication parameters */
ganlikun 0:13413ea9a877 274 SMARTCARD_SetConfig(hsc);
ganlikun 0:13413ea9a877 275
ganlikun 0:13413ea9a877 276 /* In SmartCard mode, the following bits must be kept cleared:
ganlikun 0:13413ea9a877 277 - LINEN bit in the USART_CR2 register
ganlikun 0:13413ea9a877 278 - HDSEL and IREN bits in the USART_CR3 register.*/
ganlikun 0:13413ea9a877 279 CLEAR_BIT(hsc->Instance->CR2, USART_CR2_LINEN);
ganlikun 0:13413ea9a877 280 CLEAR_BIT(hsc->Instance->CR3, (USART_CR3_IREN | USART_CR3_HDSEL));
ganlikun 0:13413ea9a877 281
ganlikun 0:13413ea9a877 282 /* Enable the SMARTCARD Parity Error Interrupt */
ganlikun 0:13413ea9a877 283 SET_BIT(hsc->Instance->CR1, USART_CR1_PEIE);
ganlikun 0:13413ea9a877 284
ganlikun 0:13413ea9a877 285 /* Enable the SMARTCARD Framing Error Interrupt */
ganlikun 0:13413ea9a877 286 SET_BIT(hsc->Instance->CR3, USART_CR3_EIE);
ganlikun 0:13413ea9a877 287
ganlikun 0:13413ea9a877 288 /* Enable the Peripheral */
ganlikun 0:13413ea9a877 289 __HAL_SMARTCARD_ENABLE(hsc);
ganlikun 0:13413ea9a877 290
ganlikun 0:13413ea9a877 291 /* Configure the Smartcard NACK state */
ganlikun 0:13413ea9a877 292 MODIFY_REG(hsc->Instance->CR3, USART_CR3_NACK, hsc->Init.NACKState);
ganlikun 0:13413ea9a877 293
ganlikun 0:13413ea9a877 294 /* Enable the SC mode by setting the SCEN bit in the CR3 register */
ganlikun 0:13413ea9a877 295 hsc->Instance->CR3 |= (USART_CR3_SCEN);
ganlikun 0:13413ea9a877 296
ganlikun 0:13413ea9a877 297 /* Initialize the SMARTCARD state*/
ganlikun 0:13413ea9a877 298 hsc->ErrorCode = HAL_SMARTCARD_ERROR_NONE;
ganlikun 0:13413ea9a877 299 hsc->gState= HAL_SMARTCARD_STATE_READY;
ganlikun 0:13413ea9a877 300 hsc->RxState= HAL_SMARTCARD_STATE_READY;
ganlikun 0:13413ea9a877 301
ganlikun 0:13413ea9a877 302 return HAL_OK;
ganlikun 0:13413ea9a877 303 }
ganlikun 0:13413ea9a877 304
ganlikun 0:13413ea9a877 305 /**
ganlikun 0:13413ea9a877 306 * @brief DeInitializes the USART SmartCard peripheral
ganlikun 0:13413ea9a877 307 * @param hsc: pointer to a SMARTCARD_HandleTypeDef structure that contains
ganlikun 0:13413ea9a877 308 * the configuration information for SMARTCARD module.
ganlikun 0:13413ea9a877 309 * @retval HAL status
ganlikun 0:13413ea9a877 310 */
ganlikun 0:13413ea9a877 311 HAL_StatusTypeDef HAL_SMARTCARD_DeInit(SMARTCARD_HandleTypeDef *hsc)
ganlikun 0:13413ea9a877 312 {
ganlikun 0:13413ea9a877 313 /* Check the SMARTCARD handle allocation */
ganlikun 0:13413ea9a877 314 if(hsc == NULL)
ganlikun 0:13413ea9a877 315 {
ganlikun 0:13413ea9a877 316 return HAL_ERROR;
ganlikun 0:13413ea9a877 317 }
ganlikun 0:13413ea9a877 318
ganlikun 0:13413ea9a877 319 /* Check the parameters */
ganlikun 0:13413ea9a877 320 assert_param(IS_SMARTCARD_INSTANCE(hsc->Instance));
ganlikun 0:13413ea9a877 321
ganlikun 0:13413ea9a877 322 hsc->gState = HAL_SMARTCARD_STATE_BUSY;
ganlikun 0:13413ea9a877 323
ganlikun 0:13413ea9a877 324 /* Disable the Peripheral */
ganlikun 0:13413ea9a877 325 __HAL_SMARTCARD_DISABLE(hsc);
ganlikun 0:13413ea9a877 326
ganlikun 0:13413ea9a877 327 /* DeInit the low level hardware */
ganlikun 0:13413ea9a877 328 HAL_SMARTCARD_MspDeInit(hsc);
ganlikun 0:13413ea9a877 329
ganlikun 0:13413ea9a877 330 hsc->ErrorCode = HAL_SMARTCARD_ERROR_NONE;
ganlikun 0:13413ea9a877 331 hsc->gState = HAL_SMARTCARD_STATE_RESET;
ganlikun 0:13413ea9a877 332 hsc->RxState = HAL_SMARTCARD_STATE_RESET;
ganlikun 0:13413ea9a877 333
ganlikun 0:13413ea9a877 334 /* Release Lock */
ganlikun 0:13413ea9a877 335 __HAL_UNLOCK(hsc);
ganlikun 0:13413ea9a877 336
ganlikun 0:13413ea9a877 337 return HAL_OK;
ganlikun 0:13413ea9a877 338 }
ganlikun 0:13413ea9a877 339
ganlikun 0:13413ea9a877 340 /**
ganlikun 0:13413ea9a877 341 * @brief SMARTCARD MSP Init
ganlikun 0:13413ea9a877 342 * @param hsc: pointer to a SMARTCARD_HandleTypeDef structure that contains
ganlikun 0:13413ea9a877 343 * the configuration information for SMARTCARD module.
ganlikun 0:13413ea9a877 344 * @retval None
ganlikun 0:13413ea9a877 345 */
ganlikun 0:13413ea9a877 346 __weak void HAL_SMARTCARD_MspInit(SMARTCARD_HandleTypeDef *hsc)
ganlikun 0:13413ea9a877 347 {
ganlikun 0:13413ea9a877 348 /* Prevent unused argument(s) compilation warning */
ganlikun 0:13413ea9a877 349 UNUSED(hsc);
ganlikun 0:13413ea9a877 350 /* NOTE : This function Should not be modified, when the callback is needed,
ganlikun 0:13413ea9a877 351 the HAL_SMARTCARD_MspInit could be implemented in the user file
ganlikun 0:13413ea9a877 352 */
ganlikun 0:13413ea9a877 353 }
ganlikun 0:13413ea9a877 354
ganlikun 0:13413ea9a877 355 /**
ganlikun 0:13413ea9a877 356 * @brief SMARTCARD MSP DeInit
ganlikun 0:13413ea9a877 357 * @param hsc: pointer to a SMARTCARD_HandleTypeDef structure that contains
ganlikun 0:13413ea9a877 358 * the configuration information for SMARTCARD module.
ganlikun 0:13413ea9a877 359 * @retval None
ganlikun 0:13413ea9a877 360 */
ganlikun 0:13413ea9a877 361 __weak void HAL_SMARTCARD_MspDeInit(SMARTCARD_HandleTypeDef *hsc)
ganlikun 0:13413ea9a877 362 {
ganlikun 0:13413ea9a877 363 /* Prevent unused argument(s) compilation warning */
ganlikun 0:13413ea9a877 364 UNUSED(hsc);
ganlikun 0:13413ea9a877 365 /* NOTE : This function Should not be modified, when the callback is needed,
ganlikun 0:13413ea9a877 366 the HAL_SMARTCARD_MspDeInit could be implemented in the user file
ganlikun 0:13413ea9a877 367 */
ganlikun 0:13413ea9a877 368 }
ganlikun 0:13413ea9a877 369
ganlikun 0:13413ea9a877 370 /**
ganlikun 0:13413ea9a877 371 * @}
ganlikun 0:13413ea9a877 372 */
ganlikun 0:13413ea9a877 373
ganlikun 0:13413ea9a877 374 /** @defgroup SMARTCARD_Exported_Functions_Group2 IO operation functions
ganlikun 0:13413ea9a877 375 * @brief SMARTCARD Transmit and Receive functions
ganlikun 0:13413ea9a877 376 *
ganlikun 0:13413ea9a877 377 @verbatim
ganlikun 0:13413ea9a877 378 ===============================================================================
ganlikun 0:13413ea9a877 379 ##### IO operation functions #####
ganlikun 0:13413ea9a877 380 ===============================================================================
ganlikun 0:13413ea9a877 381 [..] This subsection provides a set of functions allowing to manage the SMARTCARD data transfers.
ganlikun 0:13413ea9a877 382
ganlikun 0:13413ea9a877 383 [..]
ganlikun 0:13413ea9a877 384 (#) Smartcard is a single wire half duplex communication protocol.
ganlikun 0:13413ea9a877 385 The Smartcard interface is designed to support asynchronous protocol Smartcards as
ganlikun 0:13413ea9a877 386 defined in the ISO 7816-3 standard.
ganlikun 0:13413ea9a877 387 (#) The USART should be configured as:
ganlikun 0:13413ea9a877 388 (++) 8 bits plus parity: where M=1 and PCE=1 in the USART_CR1 register
ganlikun 0:13413ea9a877 389 (++) 1.5 stop bits when transmitting and receiving: where STOP=11 in the USART_CR2 register.
ganlikun 0:13413ea9a877 390
ganlikun 0:13413ea9a877 391 (#) There are two modes of transfer:
ganlikun 0:13413ea9a877 392 (++) Blocking mode: The communication is performed in polling mode.
ganlikun 0:13413ea9a877 393 The HAL status of all data processing is returned by the same function
ganlikun 0:13413ea9a877 394 after finishing transfer.
ganlikun 0:13413ea9a877 395 (++) Non Blocking mode: The communication is performed using Interrupts
ganlikun 0:13413ea9a877 396 or DMA, These APIs return the HAL status.
ganlikun 0:13413ea9a877 397 The end of the data processing will be indicated through the
ganlikun 0:13413ea9a877 398 dedicated SMARTCARD IRQ when using Interrupt mode or the DMA IRQ when
ganlikun 0:13413ea9a877 399 using DMA mode.
ganlikun 0:13413ea9a877 400 The HAL_SMARTCARD_TxCpltCallback(), HAL_SMARTCARD_RxCpltCallback() user callbacks
ganlikun 0:13413ea9a877 401 will be executed respectively at the end of the Transmit or Receive process
ganlikun 0:13413ea9a877 402 The HAL_SMARTCARD_ErrorCallback() user callback will be executed when a communication error is detected
ganlikun 0:13413ea9a877 403
ganlikun 0:13413ea9a877 404 (#) Blocking mode APIs are :
ganlikun 0:13413ea9a877 405 (++) HAL_SMARTCARD_Transmit()
ganlikun 0:13413ea9a877 406 (++) HAL_SMARTCARD_Receive()
ganlikun 0:13413ea9a877 407
ganlikun 0:13413ea9a877 408 (#) Non Blocking mode APIs with Interrupt are :
ganlikun 0:13413ea9a877 409 (++) HAL_SMARTCARD_Transmit_IT()
ganlikun 0:13413ea9a877 410 (++) HAL_SMARTCARD_Receive_IT()
ganlikun 0:13413ea9a877 411 (++) HAL_SMARTCARD_IRQHandler()
ganlikun 0:13413ea9a877 412
ganlikun 0:13413ea9a877 413 (#) Non Blocking mode functions with DMA are :
ganlikun 0:13413ea9a877 414 (++) HAL_SMARTCARD_Transmit_DMA()
ganlikun 0:13413ea9a877 415 (++) HAL_SMARTCARD_Receive_DMA()
ganlikun 0:13413ea9a877 416
ganlikun 0:13413ea9a877 417 (#) A set of Transfer Complete Callbacks are provided in non Blocking mode:
ganlikun 0:13413ea9a877 418 (++) HAL_SMARTCARD_TxCpltCallback()
ganlikun 0:13413ea9a877 419 (++) HAL_SMARTCARD_RxCpltCallback()
ganlikun 0:13413ea9a877 420 (++) HAL_SMARTCARD_ErrorCallback()
ganlikun 0:13413ea9a877 421
ganlikun 0:13413ea9a877 422 @endverbatim
ganlikun 0:13413ea9a877 423 * @{
ganlikun 0:13413ea9a877 424 */
ganlikun 0:13413ea9a877 425
ganlikun 0:13413ea9a877 426 /**
ganlikun 0:13413ea9a877 427 * @brief Send an amount of data in blocking mode
ganlikun 0:13413ea9a877 428 * @param hsc: pointer to a SMARTCARD_HandleTypeDef structure that contains
ganlikun 0:13413ea9a877 429 * the configuration information for SMARTCARD module.
ganlikun 0:13413ea9a877 430 * @param pData: pointer to data buffer
ganlikun 0:13413ea9a877 431 * @param Size: amount of data to be sent
ganlikun 0:13413ea9a877 432 * @param Timeout: Timeout duration
ganlikun 0:13413ea9a877 433 * @retval HAL status
ganlikun 0:13413ea9a877 434 */
ganlikun 0:13413ea9a877 435 HAL_StatusTypeDef HAL_SMARTCARD_Transmit(SMARTCARD_HandleTypeDef *hsc, uint8_t *pData, uint16_t Size, uint32_t Timeout)
ganlikun 0:13413ea9a877 436 {
ganlikun 0:13413ea9a877 437 uint16_t* tmp;
ganlikun 0:13413ea9a877 438 uint32_t tickstart = 0U;
ganlikun 0:13413ea9a877 439
ganlikun 0:13413ea9a877 440 if(hsc->gState == HAL_SMARTCARD_STATE_READY)
ganlikun 0:13413ea9a877 441 {
ganlikun 0:13413ea9a877 442 if((pData == NULL) || (Size == 0))
ganlikun 0:13413ea9a877 443 {
ganlikun 0:13413ea9a877 444 return HAL_ERROR;
ganlikun 0:13413ea9a877 445 }
ganlikun 0:13413ea9a877 446
ganlikun 0:13413ea9a877 447 /* Process Locked */
ganlikun 0:13413ea9a877 448 __HAL_LOCK(hsc);
ganlikun 0:13413ea9a877 449
ganlikun 0:13413ea9a877 450 hsc->ErrorCode = HAL_SMARTCARD_ERROR_NONE;
ganlikun 0:13413ea9a877 451 hsc->gState = HAL_SMARTCARD_STATE_BUSY_TX;
ganlikun 0:13413ea9a877 452
ganlikun 0:13413ea9a877 453 /* Init tickstart for timeout managment */
ganlikun 0:13413ea9a877 454 tickstart = HAL_GetTick();
ganlikun 0:13413ea9a877 455
ganlikun 0:13413ea9a877 456 hsc->TxXferSize = Size;
ganlikun 0:13413ea9a877 457 hsc->TxXferCount = Size;
ganlikun 0:13413ea9a877 458 while(hsc->TxXferCount > 0U)
ganlikun 0:13413ea9a877 459 {
ganlikun 0:13413ea9a877 460 hsc->TxXferCount--;
ganlikun 0:13413ea9a877 461 if(SMARTCARD_WaitOnFlagUntilTimeout(hsc, SMARTCARD_FLAG_TXE, RESET, tickstart, Timeout) != HAL_OK)
ganlikun 0:13413ea9a877 462 {
ganlikun 0:13413ea9a877 463 return HAL_TIMEOUT;
ganlikun 0:13413ea9a877 464 }
ganlikun 0:13413ea9a877 465 tmp = (uint16_t*) pData;
ganlikun 0:13413ea9a877 466 hsc->Instance->DR = (*tmp & (uint16_t)0x01FF);
ganlikun 0:13413ea9a877 467 pData +=1U;
ganlikun 0:13413ea9a877 468 }
ganlikun 0:13413ea9a877 469
ganlikun 0:13413ea9a877 470 if(SMARTCARD_WaitOnFlagUntilTimeout(hsc, SMARTCARD_FLAG_TC, RESET, tickstart, Timeout) != HAL_OK)
ganlikun 0:13413ea9a877 471 {
ganlikun 0:13413ea9a877 472 return HAL_TIMEOUT;
ganlikun 0:13413ea9a877 473 }
ganlikun 0:13413ea9a877 474
ganlikun 0:13413ea9a877 475 /* At end of Tx process, restore hsc->gState to Ready */
ganlikun 0:13413ea9a877 476 hsc->gState = HAL_SMARTCARD_STATE_READY;
ganlikun 0:13413ea9a877 477
ganlikun 0:13413ea9a877 478 /* Process Unlocked */
ganlikun 0:13413ea9a877 479 __HAL_UNLOCK(hsc);
ganlikun 0:13413ea9a877 480
ganlikun 0:13413ea9a877 481 return HAL_OK;
ganlikun 0:13413ea9a877 482 }
ganlikun 0:13413ea9a877 483 else
ganlikun 0:13413ea9a877 484 {
ganlikun 0:13413ea9a877 485 return HAL_BUSY;
ganlikun 0:13413ea9a877 486 }
ganlikun 0:13413ea9a877 487 }
ganlikun 0:13413ea9a877 488
ganlikun 0:13413ea9a877 489 /**
ganlikun 0:13413ea9a877 490 * @brief Receive an amount of data in blocking mode
ganlikun 0:13413ea9a877 491 * @param hsc: pointer to a SMARTCARD_HandleTypeDef structure that contains
ganlikun 0:13413ea9a877 492 * the configuration information for SMARTCARD module.
ganlikun 0:13413ea9a877 493 * @param pData: pointer to data buffer
ganlikun 0:13413ea9a877 494 * @param Size: amount of data to be received
ganlikun 0:13413ea9a877 495 * @param Timeout: Timeout duration
ganlikun 0:13413ea9a877 496 * @retval HAL status
ganlikun 0:13413ea9a877 497 */
ganlikun 0:13413ea9a877 498 HAL_StatusTypeDef HAL_SMARTCARD_Receive(SMARTCARD_HandleTypeDef *hsc, uint8_t *pData, uint16_t Size, uint32_t Timeout)
ganlikun 0:13413ea9a877 499 {
ganlikun 0:13413ea9a877 500 uint16_t* tmp;
ganlikun 0:13413ea9a877 501 uint32_t tickstart = 0U;
ganlikun 0:13413ea9a877 502
ganlikun 0:13413ea9a877 503 if(hsc->RxState == HAL_SMARTCARD_STATE_READY)
ganlikun 0:13413ea9a877 504 {
ganlikun 0:13413ea9a877 505 if((pData == NULL) || (Size == 0))
ganlikun 0:13413ea9a877 506 {
ganlikun 0:13413ea9a877 507 return HAL_ERROR;
ganlikun 0:13413ea9a877 508 }
ganlikun 0:13413ea9a877 509
ganlikun 0:13413ea9a877 510 /* Process Locked */
ganlikun 0:13413ea9a877 511 __HAL_LOCK(hsc);
ganlikun 0:13413ea9a877 512
ganlikun 0:13413ea9a877 513 hsc->ErrorCode = HAL_SMARTCARD_ERROR_NONE;
ganlikun 0:13413ea9a877 514 hsc->RxState = HAL_SMARTCARD_STATE_BUSY_RX;
ganlikun 0:13413ea9a877 515
ganlikun 0:13413ea9a877 516 /* Init tickstart for timeout managment */
ganlikun 0:13413ea9a877 517 tickstart = HAL_GetTick();
ganlikun 0:13413ea9a877 518
ganlikun 0:13413ea9a877 519 hsc->RxXferSize = Size;
ganlikun 0:13413ea9a877 520 hsc->RxXferCount = Size;
ganlikun 0:13413ea9a877 521
ganlikun 0:13413ea9a877 522 /* Check the remain data to be received */
ganlikun 0:13413ea9a877 523 while(hsc->RxXferCount > 0U)
ganlikun 0:13413ea9a877 524 {
ganlikun 0:13413ea9a877 525 hsc->RxXferCount--;
ganlikun 0:13413ea9a877 526 if(SMARTCARD_WaitOnFlagUntilTimeout(hsc, SMARTCARD_FLAG_RXNE, RESET, tickstart, Timeout) != HAL_OK)
ganlikun 0:13413ea9a877 527 {
ganlikun 0:13413ea9a877 528 return HAL_TIMEOUT;
ganlikun 0:13413ea9a877 529 }
ganlikun 0:13413ea9a877 530 tmp = (uint16_t*) pData;
ganlikun 0:13413ea9a877 531 *tmp = (uint8_t)(hsc->Instance->DR & (uint8_t)0xFF);
ganlikun 0:13413ea9a877 532 pData +=1U;
ganlikun 0:13413ea9a877 533 }
ganlikun 0:13413ea9a877 534
ganlikun 0:13413ea9a877 535 /* At end of Rx process, restore hsc->RxState to Ready */
ganlikun 0:13413ea9a877 536 hsc->RxState = HAL_SMARTCARD_STATE_READY;
ganlikun 0:13413ea9a877 537
ganlikun 0:13413ea9a877 538 /* Process Unlocked */
ganlikun 0:13413ea9a877 539 __HAL_UNLOCK(hsc);
ganlikun 0:13413ea9a877 540
ganlikun 0:13413ea9a877 541 return HAL_OK;
ganlikun 0:13413ea9a877 542 }
ganlikun 0:13413ea9a877 543 else
ganlikun 0:13413ea9a877 544 {
ganlikun 0:13413ea9a877 545 return HAL_BUSY;
ganlikun 0:13413ea9a877 546 }
ganlikun 0:13413ea9a877 547 }
ganlikun 0:13413ea9a877 548
ganlikun 0:13413ea9a877 549 /**
ganlikun 0:13413ea9a877 550 * @brief Send an amount of data in non blocking mode
ganlikun 0:13413ea9a877 551 * @param hsc: pointer to a SMARTCARD_HandleTypeDef structure that contains
ganlikun 0:13413ea9a877 552 * the configuration information for SMARTCARD module.
ganlikun 0:13413ea9a877 553 * @param pData: pointer to data buffer
ganlikun 0:13413ea9a877 554 * @param Size: amount of data to be sent
ganlikun 0:13413ea9a877 555 * @retval HAL status
ganlikun 0:13413ea9a877 556 */
ganlikun 0:13413ea9a877 557 HAL_StatusTypeDef HAL_SMARTCARD_Transmit_IT(SMARTCARD_HandleTypeDef *hsc, uint8_t *pData, uint16_t Size)
ganlikun 0:13413ea9a877 558 {
ganlikun 0:13413ea9a877 559 /* Check that a Tx process is not already ongoing */
ganlikun 0:13413ea9a877 560 if(hsc->gState == HAL_SMARTCARD_STATE_READY)
ganlikun 0:13413ea9a877 561 {
ganlikun 0:13413ea9a877 562 if((pData == NULL) || (Size == 0))
ganlikun 0:13413ea9a877 563 {
ganlikun 0:13413ea9a877 564 return HAL_ERROR;
ganlikun 0:13413ea9a877 565 }
ganlikun 0:13413ea9a877 566
ganlikun 0:13413ea9a877 567 /* Process Locked */
ganlikun 0:13413ea9a877 568 __HAL_LOCK(hsc);
ganlikun 0:13413ea9a877 569
ganlikun 0:13413ea9a877 570 hsc->pTxBuffPtr = pData;
ganlikun 0:13413ea9a877 571 hsc->TxXferSize = Size;
ganlikun 0:13413ea9a877 572 hsc->TxXferCount = Size;
ganlikun 0:13413ea9a877 573
ganlikun 0:13413ea9a877 574 hsc->ErrorCode = HAL_SMARTCARD_ERROR_NONE;
ganlikun 0:13413ea9a877 575 hsc->gState = HAL_SMARTCARD_STATE_BUSY_TX;
ganlikun 0:13413ea9a877 576
ganlikun 0:13413ea9a877 577 /* Process Unlocked */
ganlikun 0:13413ea9a877 578 __HAL_UNLOCK(hsc);
ganlikun 0:13413ea9a877 579
ganlikun 0:13413ea9a877 580 /* Enable the SMARTCARD Parity Error Interrupt */
ganlikun 0:13413ea9a877 581 SET_BIT(hsc->Instance->CR1, USART_CR1_PEIE);
ganlikun 0:13413ea9a877 582
ganlikun 0:13413ea9a877 583 /* Disable the SMARTCARD Error Interrupt: (Frame error, noise error, overrun error) */
ganlikun 0:13413ea9a877 584 CLEAR_BIT(hsc->Instance->CR3, USART_CR3_EIE);
ganlikun 0:13413ea9a877 585
ganlikun 0:13413ea9a877 586 /* Enable the SMARTCARD Transmit data register empty Interrupt */
ganlikun 0:13413ea9a877 587 SET_BIT(hsc->Instance->CR1, USART_CR1_TXEIE);
ganlikun 0:13413ea9a877 588
ganlikun 0:13413ea9a877 589 return HAL_OK;
ganlikun 0:13413ea9a877 590 }
ganlikun 0:13413ea9a877 591 else
ganlikun 0:13413ea9a877 592 {
ganlikun 0:13413ea9a877 593 return HAL_BUSY;
ganlikun 0:13413ea9a877 594 }
ganlikun 0:13413ea9a877 595 }
ganlikun 0:13413ea9a877 596
ganlikun 0:13413ea9a877 597 /**
ganlikun 0:13413ea9a877 598 * @brief Receive an amount of data in non blocking mode
ganlikun 0:13413ea9a877 599 * @param hsc: pointer to a SMARTCARD_HandleTypeDef structure that contains
ganlikun 0:13413ea9a877 600 * the configuration information for SMARTCARD module.
ganlikun 0:13413ea9a877 601 * @param pData: pointer to data buffer
ganlikun 0:13413ea9a877 602 * @param Size: amount of data to be received
ganlikun 0:13413ea9a877 603 * @retval HAL status
ganlikun 0:13413ea9a877 604 */
ganlikun 0:13413ea9a877 605 HAL_StatusTypeDef HAL_SMARTCARD_Receive_IT(SMARTCARD_HandleTypeDef *hsc, uint8_t *pData, uint16_t Size)
ganlikun 0:13413ea9a877 606 {
ganlikun 0:13413ea9a877 607 /* Check that a Rx process is not already ongoing */
ganlikun 0:13413ea9a877 608 if(hsc->RxState == HAL_SMARTCARD_STATE_READY)
ganlikun 0:13413ea9a877 609 {
ganlikun 0:13413ea9a877 610 if((pData == NULL) || (Size == 0))
ganlikun 0:13413ea9a877 611 {
ganlikun 0:13413ea9a877 612 return HAL_ERROR;
ganlikun 0:13413ea9a877 613 }
ganlikun 0:13413ea9a877 614
ganlikun 0:13413ea9a877 615 /* Process Locked */
ganlikun 0:13413ea9a877 616 __HAL_LOCK(hsc);
ganlikun 0:13413ea9a877 617
ganlikun 0:13413ea9a877 618 hsc->pRxBuffPtr = pData;
ganlikun 0:13413ea9a877 619 hsc->RxXferSize = Size;
ganlikun 0:13413ea9a877 620 hsc->RxXferCount = Size;
ganlikun 0:13413ea9a877 621
ganlikun 0:13413ea9a877 622 hsc->ErrorCode = HAL_SMARTCARD_ERROR_NONE;
ganlikun 0:13413ea9a877 623 hsc->RxState = HAL_SMARTCARD_STATE_BUSY_RX;
ganlikun 0:13413ea9a877 624
ganlikun 0:13413ea9a877 625 /* Process Unlocked */
ganlikun 0:13413ea9a877 626 __HAL_UNLOCK(hsc);
ganlikun 0:13413ea9a877 627
ganlikun 0:13413ea9a877 628 /* Enable the SMARTCARD Parity Error and Data Register not empty Interrupts */
ganlikun 0:13413ea9a877 629 SET_BIT(hsc->Instance->CR1, USART_CR1_PEIE| USART_CR1_RXNEIE);
ganlikun 0:13413ea9a877 630
ganlikun 0:13413ea9a877 631 /* Enable the SMARTCARD Error Interrupt: (Frame error, noise error, overrun error) */
ganlikun 0:13413ea9a877 632 SET_BIT(hsc->Instance->CR3, USART_CR3_EIE);
ganlikun 0:13413ea9a877 633
ganlikun 0:13413ea9a877 634 return HAL_OK;
ganlikun 0:13413ea9a877 635 }
ganlikun 0:13413ea9a877 636 else
ganlikun 0:13413ea9a877 637 {
ganlikun 0:13413ea9a877 638 return HAL_BUSY;
ganlikun 0:13413ea9a877 639 }
ganlikun 0:13413ea9a877 640 }
ganlikun 0:13413ea9a877 641
ganlikun 0:13413ea9a877 642 /**
ganlikun 0:13413ea9a877 643 * @brief Send an amount of data in non blocking mode
ganlikun 0:13413ea9a877 644 * @param hsc: pointer to a SMARTCARD_HandleTypeDef structure that contains
ganlikun 0:13413ea9a877 645 * the configuration information for SMARTCARD module.
ganlikun 0:13413ea9a877 646 * @param pData: pointer to data buffer
ganlikun 0:13413ea9a877 647 * @param Size: amount of data to be sent
ganlikun 0:13413ea9a877 648 * @retval HAL status
ganlikun 0:13413ea9a877 649 */
ganlikun 0:13413ea9a877 650 HAL_StatusTypeDef HAL_SMARTCARD_Transmit_DMA(SMARTCARD_HandleTypeDef *hsc, uint8_t *pData, uint16_t Size)
ganlikun 0:13413ea9a877 651 {
ganlikun 0:13413ea9a877 652 uint32_t *tmp;
ganlikun 0:13413ea9a877 653
ganlikun 0:13413ea9a877 654 /* Check that a Tx process is not already ongoing */
ganlikun 0:13413ea9a877 655 if(hsc->gState == HAL_SMARTCARD_STATE_READY)
ganlikun 0:13413ea9a877 656 {
ganlikun 0:13413ea9a877 657 if((pData == NULL) || (Size == 0))
ganlikun 0:13413ea9a877 658 {
ganlikun 0:13413ea9a877 659 return HAL_ERROR;
ganlikun 0:13413ea9a877 660 }
ganlikun 0:13413ea9a877 661
ganlikun 0:13413ea9a877 662 /* Process Locked */
ganlikun 0:13413ea9a877 663 __HAL_LOCK(hsc);
ganlikun 0:13413ea9a877 664
ganlikun 0:13413ea9a877 665 hsc->pTxBuffPtr = pData;
ganlikun 0:13413ea9a877 666 hsc->TxXferSize = Size;
ganlikun 0:13413ea9a877 667 hsc->TxXferCount = Size;
ganlikun 0:13413ea9a877 668
ganlikun 0:13413ea9a877 669 hsc->ErrorCode = HAL_SMARTCARD_ERROR_NONE;
ganlikun 0:13413ea9a877 670 hsc->gState = HAL_SMARTCARD_STATE_BUSY_TX;
ganlikun 0:13413ea9a877 671
ganlikun 0:13413ea9a877 672 /* Set the SMARTCARD DMA transfer complete callback */
ganlikun 0:13413ea9a877 673 hsc->hdmatx->XferCpltCallback = SMARTCARD_DMATransmitCplt;
ganlikun 0:13413ea9a877 674
ganlikun 0:13413ea9a877 675 /* Set the DMA error callback */
ganlikun 0:13413ea9a877 676 hsc->hdmatx->XferErrorCallback = SMARTCARD_DMAError;
ganlikun 0:13413ea9a877 677
ganlikun 0:13413ea9a877 678 /* Set the DMA abort callback */
ganlikun 0:13413ea9a877 679 hsc->hdmatx->XferAbortCallback = NULL;
ganlikun 0:13413ea9a877 680
ganlikun 0:13413ea9a877 681 /* Enable the SMARTCARD transmit DMA Stream */
ganlikun 0:13413ea9a877 682 tmp = (uint32_t*)&pData;
ganlikun 0:13413ea9a877 683 HAL_DMA_Start_IT(hsc->hdmatx, *(uint32_t*)tmp, (uint32_t)&hsc->Instance->DR, Size);
ganlikun 0:13413ea9a877 684
ganlikun 0:13413ea9a877 685 /* Clear the TC flag in the SR register by writing 0 to it */
ganlikun 0:13413ea9a877 686 __HAL_SMARTCARD_CLEAR_FLAG(hsc, SMARTCARD_FLAG_TC);
ganlikun 0:13413ea9a877 687
ganlikun 0:13413ea9a877 688 /* Process Unlocked */
ganlikun 0:13413ea9a877 689 __HAL_UNLOCK(hsc);
ganlikun 0:13413ea9a877 690
ganlikun 0:13413ea9a877 691 /* Enable the DMA transfer for transmit request by setting the DMAT bit
ganlikun 0:13413ea9a877 692 in the SMARTCARD CR3 register */
ganlikun 0:13413ea9a877 693 SET_BIT(hsc->Instance->CR3, USART_CR3_DMAT);
ganlikun 0:13413ea9a877 694
ganlikun 0:13413ea9a877 695 return HAL_OK;
ganlikun 0:13413ea9a877 696 }
ganlikun 0:13413ea9a877 697 else
ganlikun 0:13413ea9a877 698 {
ganlikun 0:13413ea9a877 699 return HAL_BUSY;
ganlikun 0:13413ea9a877 700 }
ganlikun 0:13413ea9a877 701 }
ganlikun 0:13413ea9a877 702
ganlikun 0:13413ea9a877 703 /**
ganlikun 0:13413ea9a877 704 * @brief Receive an amount of data in non blocking mode
ganlikun 0:13413ea9a877 705 * @param hsc: pointer to a SMARTCARD_HandleTypeDef structure that contains
ganlikun 0:13413ea9a877 706 * the configuration information for SMARTCARD module.
ganlikun 0:13413ea9a877 707 * @param pData: pointer to data buffer
ganlikun 0:13413ea9a877 708 * @param Size: amount of data to be received
ganlikun 0:13413ea9a877 709 * @note When the SMARTCARD parity is enabled (PCE = 1) the data received contain the parity bit.s
ganlikun 0:13413ea9a877 710 * @retval HAL status
ganlikun 0:13413ea9a877 711 */
ganlikun 0:13413ea9a877 712 HAL_StatusTypeDef HAL_SMARTCARD_Receive_DMA(SMARTCARD_HandleTypeDef *hsc, uint8_t *pData, uint16_t Size)
ganlikun 0:13413ea9a877 713 {
ganlikun 0:13413ea9a877 714 uint32_t *tmp;
ganlikun 0:13413ea9a877 715
ganlikun 0:13413ea9a877 716 /* Check that a Rx process is not already ongoing */
ganlikun 0:13413ea9a877 717 if(hsc->RxState == HAL_SMARTCARD_STATE_READY)
ganlikun 0:13413ea9a877 718 {
ganlikun 0:13413ea9a877 719 if((pData == NULL) || (Size == 0))
ganlikun 0:13413ea9a877 720 {
ganlikun 0:13413ea9a877 721 return HAL_ERROR;
ganlikun 0:13413ea9a877 722 }
ganlikun 0:13413ea9a877 723
ganlikun 0:13413ea9a877 724 /* Process Locked */
ganlikun 0:13413ea9a877 725 __HAL_LOCK(hsc);
ganlikun 0:13413ea9a877 726
ganlikun 0:13413ea9a877 727 hsc->pRxBuffPtr = pData;
ganlikun 0:13413ea9a877 728 hsc->RxXferSize = Size;
ganlikun 0:13413ea9a877 729
ganlikun 0:13413ea9a877 730 hsc->ErrorCode = HAL_SMARTCARD_ERROR_NONE;
ganlikun 0:13413ea9a877 731 hsc->RxState = HAL_SMARTCARD_STATE_BUSY_RX;
ganlikun 0:13413ea9a877 732
ganlikun 0:13413ea9a877 733 /* Set the SMARTCARD DMA transfer complete callback */
ganlikun 0:13413ea9a877 734 hsc->hdmarx->XferCpltCallback = SMARTCARD_DMAReceiveCplt;
ganlikun 0:13413ea9a877 735
ganlikun 0:13413ea9a877 736 /* Set the DMA error callback */
ganlikun 0:13413ea9a877 737 hsc->hdmarx->XferErrorCallback = SMARTCARD_DMAError;
ganlikun 0:13413ea9a877 738
ganlikun 0:13413ea9a877 739 /* Set the DMA abort callback */
ganlikun 0:13413ea9a877 740 hsc->hdmatx->XferAbortCallback = NULL;
ganlikun 0:13413ea9a877 741
ganlikun 0:13413ea9a877 742 /* Enable the DMA Stream */
ganlikun 0:13413ea9a877 743 tmp = (uint32_t*)&pData;
ganlikun 0:13413ea9a877 744 HAL_DMA_Start_IT(hsc->hdmarx, (uint32_t)&hsc->Instance->DR, *(uint32_t*)tmp, Size);
ganlikun 0:13413ea9a877 745
ganlikun 0:13413ea9a877 746 /* Clear the Overrun flag just before enabling the DMA Rx request: can be mandatory for the second transfer */
ganlikun 0:13413ea9a877 747 __HAL_SMARTCARD_CLEAR_OREFLAG(hsc);
ganlikun 0:13413ea9a877 748
ganlikun 0:13413ea9a877 749 /* Process Unlocked */
ganlikun 0:13413ea9a877 750 __HAL_UNLOCK(hsc);
ganlikun 0:13413ea9a877 751
ganlikun 0:13413ea9a877 752 /* Enable the SMARTCARD Parity Error Interrupt */
ganlikun 0:13413ea9a877 753 SET_BIT(hsc->Instance->CR1, USART_CR1_PEIE);
ganlikun 0:13413ea9a877 754
ganlikun 0:13413ea9a877 755 /* Enable the SMARTCARD Error Interrupt: (Frame error, noise error, overrun error) */
ganlikun 0:13413ea9a877 756 SET_BIT(hsc->Instance->CR3, USART_CR3_EIE);
ganlikun 0:13413ea9a877 757
ganlikun 0:13413ea9a877 758 /* Enable the DMA transfer for the receiver request by setting the DMAR bit
ganlikun 0:13413ea9a877 759 in the SMARTCARD CR3 register */
ganlikun 0:13413ea9a877 760 SET_BIT(hsc->Instance->CR3, USART_CR3_DMAR);
ganlikun 0:13413ea9a877 761
ganlikun 0:13413ea9a877 762 return HAL_OK;
ganlikun 0:13413ea9a877 763 }
ganlikun 0:13413ea9a877 764 else
ganlikun 0:13413ea9a877 765 {
ganlikun 0:13413ea9a877 766 return HAL_BUSY;
ganlikun 0:13413ea9a877 767 }
ganlikun 0:13413ea9a877 768 }
ganlikun 0:13413ea9a877 769
ganlikun 0:13413ea9a877 770 /**
ganlikun 0:13413ea9a877 771 * @brief Abort ongoing transfers (blocking mode).
ganlikun 0:13413ea9a877 772 * @param hsc SMARTCARD handle.
ganlikun 0:13413ea9a877 773 * @note This procedure could be used for aborting any ongoing transfer started in Interrupt or DMA mode.
ganlikun 0:13413ea9a877 774 * This procedure performs following operations :
ganlikun 0:13413ea9a877 775 * - Disable PPP Interrupts
ganlikun 0:13413ea9a877 776 * - Disable the DMA transfer in the peripheral register (if enabled)
ganlikun 0:13413ea9a877 777 * - Abort DMA transfer by calling HAL_DMA_Abort (in case of transfer in DMA mode)
ganlikun 0:13413ea9a877 778 * - Set handle State to READY
ganlikun 0:13413ea9a877 779 * @note This procedure is executed in blocking mode : when exiting function, Abort is considered as completed.
ganlikun 0:13413ea9a877 780 * @retval HAL status
ganlikun 0:13413ea9a877 781 */
ganlikun 0:13413ea9a877 782 HAL_StatusTypeDef HAL_SMARTCARD_Abort(SMARTCARD_HandleTypeDef *hsc)
ganlikun 0:13413ea9a877 783 {
ganlikun 0:13413ea9a877 784 /* Disable TXEIE, TCIE, RXNE, PE and ERR (Frame error, noise error, overrun error) interrupts */
ganlikun 0:13413ea9a877 785 CLEAR_BIT(hsc->Instance->CR1, (USART_CR1_RXNEIE | USART_CR1_PEIE | USART_CR1_TXEIE | USART_CR1_TCIE));
ganlikun 0:13413ea9a877 786 CLEAR_BIT(hsc->Instance->CR3, USART_CR3_EIE);
ganlikun 0:13413ea9a877 787
ganlikun 0:13413ea9a877 788 /* Disable the SMARTCARD DMA Tx request if enabled */
ganlikun 0:13413ea9a877 789 if(HAL_IS_BIT_SET(hsc->Instance->CR3, USART_CR3_DMAT))
ganlikun 0:13413ea9a877 790 {
ganlikun 0:13413ea9a877 791 CLEAR_BIT(hsc->Instance->CR3, USART_CR3_DMAT);
ganlikun 0:13413ea9a877 792
ganlikun 0:13413ea9a877 793 /* Abort the SMARTCARD DMA Tx channel : use blocking DMA Abort API (no callback) */
ganlikun 0:13413ea9a877 794 if(hsc->hdmatx != NULL)
ganlikun 0:13413ea9a877 795 {
ganlikun 0:13413ea9a877 796 /* Set the SMARTCARD DMA Abort callback to Null.
ganlikun 0:13413ea9a877 797 No call back execution at end of DMA abort procedure */
ganlikun 0:13413ea9a877 798 hsc->hdmatx->XferAbortCallback = NULL;
ganlikun 0:13413ea9a877 799
ganlikun 0:13413ea9a877 800 HAL_DMA_Abort(hsc->hdmatx);
ganlikun 0:13413ea9a877 801 }
ganlikun 0:13413ea9a877 802 }
ganlikun 0:13413ea9a877 803
ganlikun 0:13413ea9a877 804 /* Disable the SMARTCARD DMA Rx request if enabled */
ganlikun 0:13413ea9a877 805 if(HAL_IS_BIT_SET(hsc->Instance->CR3, USART_CR3_DMAR))
ganlikun 0:13413ea9a877 806 {
ganlikun 0:13413ea9a877 807 CLEAR_BIT(hsc->Instance->CR3, USART_CR3_DMAR);
ganlikun 0:13413ea9a877 808
ganlikun 0:13413ea9a877 809 /* Abort the SMARTCARD DMA Rx channel : use blocking DMA Abort API (no callback) */
ganlikun 0:13413ea9a877 810 if(hsc->hdmarx != NULL)
ganlikun 0:13413ea9a877 811 {
ganlikun 0:13413ea9a877 812 /* Set the SMARTCARD DMA Abort callback to Null.
ganlikun 0:13413ea9a877 813 No call back execution at end of DMA abort procedure */
ganlikun 0:13413ea9a877 814 hsc->hdmarx->XferAbortCallback = NULL;
ganlikun 0:13413ea9a877 815
ganlikun 0:13413ea9a877 816 HAL_DMA_Abort(hsc->hdmarx);
ganlikun 0:13413ea9a877 817 }
ganlikun 0:13413ea9a877 818 }
ganlikun 0:13413ea9a877 819
ganlikun 0:13413ea9a877 820 /* Reset Tx and Rx transfer counters */
ganlikun 0:13413ea9a877 821 hsc->TxXferCount = 0x00U;
ganlikun 0:13413ea9a877 822 hsc->RxXferCount = 0x00U;
ganlikun 0:13413ea9a877 823
ganlikun 0:13413ea9a877 824 /* Reset ErrorCode */
ganlikun 0:13413ea9a877 825 hsc->ErrorCode = HAL_SMARTCARD_ERROR_NONE;
ganlikun 0:13413ea9a877 826
ganlikun 0:13413ea9a877 827 /* Restore hsc->RxState and hsc->gState to Ready */
ganlikun 0:13413ea9a877 828 hsc->RxState = HAL_SMARTCARD_STATE_READY;
ganlikun 0:13413ea9a877 829 hsc->gState = HAL_SMARTCARD_STATE_READY;
ganlikun 0:13413ea9a877 830
ganlikun 0:13413ea9a877 831 return HAL_OK;
ganlikun 0:13413ea9a877 832 }
ganlikun 0:13413ea9a877 833
ganlikun 0:13413ea9a877 834 /**
ganlikun 0:13413ea9a877 835 * @brief Abort ongoing Transmit transfer (blocking mode).
ganlikun 0:13413ea9a877 836 * @param hsc SMARTCARD handle.
ganlikun 0:13413ea9a877 837 * @note This procedure could be used for aborting any ongoing transfer started in Interrupt or DMA mode.
ganlikun 0:13413ea9a877 838 * This procedure performs following operations :
ganlikun 0:13413ea9a877 839 * - Disable PPP Interrupts
ganlikun 0:13413ea9a877 840 * - Disable the DMA transfer in the peripheral register (if enabled)
ganlikun 0:13413ea9a877 841 * - Abort DMA transfer by calling HAL_DMA_Abort (in case of transfer in DMA mode)
ganlikun 0:13413ea9a877 842 * - Set handle State to READY
ganlikun 0:13413ea9a877 843 * @note This procedure is executed in blocking mode : when exiting function, Abort is considered as completed.
ganlikun 0:13413ea9a877 844 * @retval HAL status
ganlikun 0:13413ea9a877 845 */
ganlikun 0:13413ea9a877 846 HAL_StatusTypeDef HAL_SMARTCARD_AbortTransmit(SMARTCARD_HandleTypeDef *hsc)
ganlikun 0:13413ea9a877 847 {
ganlikun 0:13413ea9a877 848 /* Disable TXEIE and TCIE interrupts */
ganlikun 0:13413ea9a877 849 CLEAR_BIT(hsc->Instance->CR1, (USART_CR1_TXEIE | USART_CR1_TCIE));
ganlikun 0:13413ea9a877 850
ganlikun 0:13413ea9a877 851 /* Disable the SMARTCARD DMA Tx request if enabled */
ganlikun 0:13413ea9a877 852 if(HAL_IS_BIT_SET(hsc->Instance->CR3, USART_CR3_DMAT))
ganlikun 0:13413ea9a877 853 {
ganlikun 0:13413ea9a877 854 CLEAR_BIT(hsc->Instance->CR3, USART_CR3_DMAT);
ganlikun 0:13413ea9a877 855
ganlikun 0:13413ea9a877 856 /* Abort the SMARTCARD DMA Tx channel : use blocking DMA Abort API (no callback) */
ganlikun 0:13413ea9a877 857 if(hsc->hdmatx != NULL)
ganlikun 0:13413ea9a877 858 {
ganlikun 0:13413ea9a877 859 /* Set the SMARTCARD DMA Abort callback to Null.
ganlikun 0:13413ea9a877 860 No call back execution at end of DMA abort procedure */
ganlikun 0:13413ea9a877 861 hsc->hdmatx->XferAbortCallback = NULL;
ganlikun 0:13413ea9a877 862
ganlikun 0:13413ea9a877 863 HAL_DMA_Abort(hsc->hdmatx);
ganlikun 0:13413ea9a877 864 }
ganlikun 0:13413ea9a877 865 }
ganlikun 0:13413ea9a877 866
ganlikun 0:13413ea9a877 867 /* Reset Tx transfer counter */
ganlikun 0:13413ea9a877 868 hsc->TxXferCount = 0x00U;
ganlikun 0:13413ea9a877 869
ganlikun 0:13413ea9a877 870 /* Restore hsc->gState to Ready */
ganlikun 0:13413ea9a877 871 hsc->gState = HAL_SMARTCARD_STATE_READY;
ganlikun 0:13413ea9a877 872
ganlikun 0:13413ea9a877 873 return HAL_OK;
ganlikun 0:13413ea9a877 874 }
ganlikun 0:13413ea9a877 875
ganlikun 0:13413ea9a877 876 /**
ganlikun 0:13413ea9a877 877 * @brief Abort ongoing Receive transfer (blocking mode).
ganlikun 0:13413ea9a877 878 * @param hsc SMARTCARD handle.
ganlikun 0:13413ea9a877 879 * @note This procedure could be used for aborting any ongoing transfer started in Interrupt or DMA mode.
ganlikun 0:13413ea9a877 880 * This procedure performs following operations :
ganlikun 0:13413ea9a877 881 * - Disable PPP Interrupts
ganlikun 0:13413ea9a877 882 * - Disable the DMA transfer in the peripheral register (if enabled)
ganlikun 0:13413ea9a877 883 * - Abort DMA transfer by calling HAL_DMA_Abort (in case of transfer in DMA mode)
ganlikun 0:13413ea9a877 884 * - Set handle State to READY
ganlikun 0:13413ea9a877 885 * @note This procedure is executed in blocking mode : when exiting function, Abort is considered as completed.
ganlikun 0:13413ea9a877 886 * @retval HAL status
ganlikun 0:13413ea9a877 887 */
ganlikun 0:13413ea9a877 888 HAL_StatusTypeDef HAL_SMARTCARD_AbortReceive(SMARTCARD_HandleTypeDef *hsc)
ganlikun 0:13413ea9a877 889 {
ganlikun 0:13413ea9a877 890 /* Disable RXNE, PE and ERR (Frame error, noise error, overrun error) interrupts */
ganlikun 0:13413ea9a877 891 CLEAR_BIT(hsc->Instance->CR1, (USART_CR1_RXNEIE | USART_CR1_PEIE));
ganlikun 0:13413ea9a877 892 CLEAR_BIT(hsc->Instance->CR3, USART_CR3_EIE);
ganlikun 0:13413ea9a877 893
ganlikun 0:13413ea9a877 894 /* Disable the SMARTCARD DMA Rx request if enabled */
ganlikun 0:13413ea9a877 895 if(HAL_IS_BIT_SET(hsc->Instance->CR3, USART_CR3_DMAR))
ganlikun 0:13413ea9a877 896 {
ganlikun 0:13413ea9a877 897 CLEAR_BIT(hsc->Instance->CR3, USART_CR3_DMAR);
ganlikun 0:13413ea9a877 898
ganlikun 0:13413ea9a877 899 /* Abort the SMARTCARD DMA Rx channel : use blocking DMA Abort API (no callback) */
ganlikun 0:13413ea9a877 900 if(hsc->hdmarx != NULL)
ganlikun 0:13413ea9a877 901 {
ganlikun 0:13413ea9a877 902 /* Set the SMARTCARD DMA Abort callback to Null.
ganlikun 0:13413ea9a877 903 No call back execution at end of DMA abort procedure */
ganlikun 0:13413ea9a877 904 hsc->hdmarx->XferAbortCallback = NULL;
ganlikun 0:13413ea9a877 905
ganlikun 0:13413ea9a877 906 HAL_DMA_Abort(hsc->hdmarx);
ganlikun 0:13413ea9a877 907 }
ganlikun 0:13413ea9a877 908 }
ganlikun 0:13413ea9a877 909
ganlikun 0:13413ea9a877 910 /* Reset Rx transfer counter */
ganlikun 0:13413ea9a877 911 hsc->RxXferCount = 0x00U;
ganlikun 0:13413ea9a877 912
ganlikun 0:13413ea9a877 913 /* Restore hsc->RxState to Ready */
ganlikun 0:13413ea9a877 914 hsc->RxState = HAL_SMARTCARD_STATE_READY;
ganlikun 0:13413ea9a877 915
ganlikun 0:13413ea9a877 916 return HAL_OK;
ganlikun 0:13413ea9a877 917 }
ganlikun 0:13413ea9a877 918
ganlikun 0:13413ea9a877 919 /**
ganlikun 0:13413ea9a877 920 * @brief Abort ongoing transfers (Interrupt mode).
ganlikun 0:13413ea9a877 921 * @param hsc SMARTCARD handle.
ganlikun 0:13413ea9a877 922 * @note This procedure could be used for aborting any ongoing transfer started in Interrupt or DMA mode.
ganlikun 0:13413ea9a877 923 * This procedure performs following operations :
ganlikun 0:13413ea9a877 924 * - Disable PPP Interrupts
ganlikun 0:13413ea9a877 925 * - Disable the DMA transfer in the peripheral register (if enabled)
ganlikun 0:13413ea9a877 926 * - Abort DMA transfer by calling HAL_DMA_Abort_IT (in case of transfer in DMA mode)
ganlikun 0:13413ea9a877 927 * - Set handle State to READY
ganlikun 0:13413ea9a877 928 * - At abort completion, call user abort complete callback
ganlikun 0:13413ea9a877 929 * @note This procedure is executed in Interrupt mode, meaning that abort procedure could be
ganlikun 0:13413ea9a877 930 * considered as completed only when user abort complete callback is executed (not when exiting function).
ganlikun 0:13413ea9a877 931 * @retval HAL status
ganlikun 0:13413ea9a877 932 */
ganlikun 0:13413ea9a877 933 HAL_StatusTypeDef HAL_SMARTCARD_Abort_IT(SMARTCARD_HandleTypeDef *hsc)
ganlikun 0:13413ea9a877 934 {
ganlikun 0:13413ea9a877 935 uint32_t AbortCplt = 0x01U;
ganlikun 0:13413ea9a877 936
ganlikun 0:13413ea9a877 937 /* Disable TXEIE, TCIE, RXNE, PE and ERR (Frame error, noise error, overrun error) interrupts */
ganlikun 0:13413ea9a877 938 CLEAR_BIT(hsc->Instance->CR1, (USART_CR1_RXNEIE | USART_CR1_PEIE | USART_CR1_TXEIE | USART_CR1_TCIE));
ganlikun 0:13413ea9a877 939 CLEAR_BIT(hsc->Instance->CR3, USART_CR3_EIE);
ganlikun 0:13413ea9a877 940
ganlikun 0:13413ea9a877 941 /* If DMA Tx and/or DMA Rx Handles are associated to SMARTCARD Handle, DMA Abort complete callbacks should be initialised
ganlikun 0:13413ea9a877 942 before any call to DMA Abort functions */
ganlikun 0:13413ea9a877 943 /* DMA Tx Handle is valid */
ganlikun 0:13413ea9a877 944 if(hsc->hdmatx != NULL)
ganlikun 0:13413ea9a877 945 {
ganlikun 0:13413ea9a877 946 /* Set DMA Abort Complete callback if SMARTCARD DMA Tx request if enabled.
ganlikun 0:13413ea9a877 947 Otherwise, set it to NULL */
ganlikun 0:13413ea9a877 948 if(HAL_IS_BIT_SET(hsc->Instance->CR3, USART_CR3_DMAT))
ganlikun 0:13413ea9a877 949 {
ganlikun 0:13413ea9a877 950 hsc->hdmatx->XferAbortCallback = SMARTCARD_DMATxAbortCallback;
ganlikun 0:13413ea9a877 951 }
ganlikun 0:13413ea9a877 952 else
ganlikun 0:13413ea9a877 953 {
ganlikun 0:13413ea9a877 954 hsc->hdmatx->XferAbortCallback = NULL;
ganlikun 0:13413ea9a877 955 }
ganlikun 0:13413ea9a877 956 }
ganlikun 0:13413ea9a877 957 /* DMA Rx Handle is valid */
ganlikun 0:13413ea9a877 958 if(hsc->hdmarx != NULL)
ganlikun 0:13413ea9a877 959 {
ganlikun 0:13413ea9a877 960 /* Set DMA Abort Complete callback if SMARTCARD DMA Rx request if enabled.
ganlikun 0:13413ea9a877 961 Otherwise, set it to NULL */
ganlikun 0:13413ea9a877 962 if(HAL_IS_BIT_SET(hsc->Instance->CR3, USART_CR3_DMAR))
ganlikun 0:13413ea9a877 963 {
ganlikun 0:13413ea9a877 964 hsc->hdmarx->XferAbortCallback = SMARTCARD_DMARxAbortCallback;
ganlikun 0:13413ea9a877 965 }
ganlikun 0:13413ea9a877 966 else
ganlikun 0:13413ea9a877 967 {
ganlikun 0:13413ea9a877 968 hsc->hdmarx->XferAbortCallback = NULL;
ganlikun 0:13413ea9a877 969 }
ganlikun 0:13413ea9a877 970 }
ganlikun 0:13413ea9a877 971
ganlikun 0:13413ea9a877 972 /* Disable the SMARTCARD DMA Tx request if enabled */
ganlikun 0:13413ea9a877 973 if(HAL_IS_BIT_SET(hsc->Instance->CR3, USART_CR3_DMAT))
ganlikun 0:13413ea9a877 974 {
ganlikun 0:13413ea9a877 975 /* Disable DMA Tx at SMARTCARD level */
ganlikun 0:13413ea9a877 976 CLEAR_BIT(hsc->Instance->CR3, USART_CR3_DMAT);
ganlikun 0:13413ea9a877 977
ganlikun 0:13413ea9a877 978 /* Abort the SMARTCARD DMA Tx channel : use non blocking DMA Abort API (callback) */
ganlikun 0:13413ea9a877 979 if(hsc->hdmatx != NULL)
ganlikun 0:13413ea9a877 980 {
ganlikun 0:13413ea9a877 981 /* SMARTCARD Tx DMA Abort callback has already been initialised :
ganlikun 0:13413ea9a877 982 will lead to call HAL_SMARTCARD_AbortCpltCallback() at end of DMA abort procedure */
ganlikun 0:13413ea9a877 983
ganlikun 0:13413ea9a877 984 /* Abort DMA TX */
ganlikun 0:13413ea9a877 985 if(HAL_DMA_Abort_IT(hsc->hdmatx) != HAL_OK)
ganlikun 0:13413ea9a877 986 {
ganlikun 0:13413ea9a877 987 hsc->hdmatx->XferAbortCallback = NULL;
ganlikun 0:13413ea9a877 988 }
ganlikun 0:13413ea9a877 989 else
ganlikun 0:13413ea9a877 990 {
ganlikun 0:13413ea9a877 991 AbortCplt = 0x00U;
ganlikun 0:13413ea9a877 992 }
ganlikun 0:13413ea9a877 993 }
ganlikun 0:13413ea9a877 994 }
ganlikun 0:13413ea9a877 995
ganlikun 0:13413ea9a877 996 /* Disable the SMARTCARD DMA Rx request if enabled */
ganlikun 0:13413ea9a877 997 if(HAL_IS_BIT_SET(hsc->Instance->CR3, USART_CR3_DMAR))
ganlikun 0:13413ea9a877 998 {
ganlikun 0:13413ea9a877 999 CLEAR_BIT(hsc->Instance->CR3, USART_CR3_DMAR);
ganlikun 0:13413ea9a877 1000
ganlikun 0:13413ea9a877 1001 /* Abort the SMARTCARD DMA Rx channel : use non blocking DMA Abort API (callback) */
ganlikun 0:13413ea9a877 1002 if(hsc->hdmarx != NULL)
ganlikun 0:13413ea9a877 1003 {
ganlikun 0:13413ea9a877 1004 /* SMARTCARD Rx DMA Abort callback has already been initialised :
ganlikun 0:13413ea9a877 1005 will lead to call HAL_SMARTCARD_AbortCpltCallback() at end of DMA abort procedure */
ganlikun 0:13413ea9a877 1006
ganlikun 0:13413ea9a877 1007 /* Abort DMA RX */
ganlikun 0:13413ea9a877 1008 if(HAL_DMA_Abort_IT(hsc->hdmarx) != HAL_OK)
ganlikun 0:13413ea9a877 1009 {
ganlikun 0:13413ea9a877 1010 hsc->hdmarx->XferAbortCallback = NULL;
ganlikun 0:13413ea9a877 1011 AbortCplt = 0x01U;
ganlikun 0:13413ea9a877 1012 }
ganlikun 0:13413ea9a877 1013 else
ganlikun 0:13413ea9a877 1014 {
ganlikun 0:13413ea9a877 1015 AbortCplt = 0x00U;
ganlikun 0:13413ea9a877 1016 }
ganlikun 0:13413ea9a877 1017 }
ganlikun 0:13413ea9a877 1018 }
ganlikun 0:13413ea9a877 1019
ganlikun 0:13413ea9a877 1020 /* if no DMA abort complete callback execution is required => call user Abort Complete callback */
ganlikun 0:13413ea9a877 1021 if(AbortCplt == 0x01U)
ganlikun 0:13413ea9a877 1022 {
ganlikun 0:13413ea9a877 1023 /* Reset Tx and Rx transfer counters */
ganlikun 0:13413ea9a877 1024 hsc->TxXferCount = 0x00U;
ganlikun 0:13413ea9a877 1025 hsc->RxXferCount = 0x00U;
ganlikun 0:13413ea9a877 1026
ganlikun 0:13413ea9a877 1027 /* Reset ErrorCode */
ganlikun 0:13413ea9a877 1028 hsc->ErrorCode = HAL_SMARTCARD_ERROR_NONE;
ganlikun 0:13413ea9a877 1029
ganlikun 0:13413ea9a877 1030 /* Restore hsc->gState and hsc->RxState to Ready */
ganlikun 0:13413ea9a877 1031 hsc->gState = HAL_SMARTCARD_STATE_READY;
ganlikun 0:13413ea9a877 1032 hsc->RxState = HAL_SMARTCARD_STATE_READY;
ganlikun 0:13413ea9a877 1033
ganlikun 0:13413ea9a877 1034 /* As no DMA to be aborted, call directly user Abort complete callback */
ganlikun 0:13413ea9a877 1035 HAL_SMARTCARD_AbortCpltCallback(hsc);
ganlikun 0:13413ea9a877 1036 }
ganlikun 0:13413ea9a877 1037 return HAL_OK;
ganlikun 0:13413ea9a877 1038 }
ganlikun 0:13413ea9a877 1039
ganlikun 0:13413ea9a877 1040 /**
ganlikun 0:13413ea9a877 1041 * @brief Abort ongoing Transmit transfer (Interrupt mode).
ganlikun 0:13413ea9a877 1042 * @param hsc SMARTCARD handle.
ganlikun 0:13413ea9a877 1043 * @note This procedure could be used for aborting any ongoing transfer started in Interrupt or DMA mode.
ganlikun 0:13413ea9a877 1044 * This procedure performs following operations :
ganlikun 0:13413ea9a877 1045 * - Disable PPP Interrupts
ganlikun 0:13413ea9a877 1046 * - Disable the DMA transfer in the peripheral register (if enabled)
ganlikun 0:13413ea9a877 1047 * - Abort DMA transfer by calling HAL_DMA_Abort_IT (in case of transfer in DMA mode)
ganlikun 0:13413ea9a877 1048 * - Set handle State to READY
ganlikun 0:13413ea9a877 1049 * - At abort completion, call user abort complete callback
ganlikun 0:13413ea9a877 1050 * @note This procedure is executed in Interrupt mode, meaning that abort procedure could be
ganlikun 0:13413ea9a877 1051 * considered as completed only when user abort complete callback is executed (not when exiting function).
ganlikun 0:13413ea9a877 1052 * @retval HAL status
ganlikun 0:13413ea9a877 1053 */
ganlikun 0:13413ea9a877 1054 HAL_StatusTypeDef HAL_SMARTCARD_AbortTransmit_IT(SMARTCARD_HandleTypeDef *hsc)
ganlikun 0:13413ea9a877 1055 {
ganlikun 0:13413ea9a877 1056 /* Disable TXEIE and TCIE interrupts */
ganlikun 0:13413ea9a877 1057 CLEAR_BIT(hsc->Instance->CR1, (USART_CR1_TXEIE | USART_CR1_TCIE));
ganlikun 0:13413ea9a877 1058
ganlikun 0:13413ea9a877 1059 /* Disable the SMARTCARD DMA Tx request if enabled */
ganlikun 0:13413ea9a877 1060 if(HAL_IS_BIT_SET(hsc->Instance->CR3, USART_CR3_DMAT))
ganlikun 0:13413ea9a877 1061 {
ganlikun 0:13413ea9a877 1062 CLEAR_BIT(hsc->Instance->CR3, USART_CR3_DMAT);
ganlikun 0:13413ea9a877 1063
ganlikun 0:13413ea9a877 1064 /* Abort the SMARTCARD DMA Tx channel : use blocking DMA Abort API (no callback) */
ganlikun 0:13413ea9a877 1065 if(hsc->hdmatx != NULL)
ganlikun 0:13413ea9a877 1066 {
ganlikun 0:13413ea9a877 1067 /* Set the SMARTCARD DMA Abort callback :
ganlikun 0:13413ea9a877 1068 will lead to call HAL_SMARTCARD_AbortCpltCallback() at end of DMA abort procedure */
ganlikun 0:13413ea9a877 1069 hsc->hdmatx->XferAbortCallback = SMARTCARD_DMATxOnlyAbortCallback;
ganlikun 0:13413ea9a877 1070
ganlikun 0:13413ea9a877 1071 /* Abort DMA TX */
ganlikun 0:13413ea9a877 1072 if(HAL_DMA_Abort_IT(hsc->hdmatx) != HAL_OK)
ganlikun 0:13413ea9a877 1073 {
ganlikun 0:13413ea9a877 1074 /* Call Directly hsc->hdmatx->XferAbortCallback function in case of error */
ganlikun 0:13413ea9a877 1075 hsc->hdmatx->XferAbortCallback(hsc->hdmatx);
ganlikun 0:13413ea9a877 1076 }
ganlikun 0:13413ea9a877 1077 }
ganlikun 0:13413ea9a877 1078 else
ganlikun 0:13413ea9a877 1079 {
ganlikun 0:13413ea9a877 1080 /* Reset Tx transfer counter */
ganlikun 0:13413ea9a877 1081 hsc->TxXferCount = 0x00U;
ganlikun 0:13413ea9a877 1082
ganlikun 0:13413ea9a877 1083 /* Restore hsc->gState to Ready */
ganlikun 0:13413ea9a877 1084 hsc->gState = HAL_SMARTCARD_STATE_READY;
ganlikun 0:13413ea9a877 1085
ganlikun 0:13413ea9a877 1086 /* As no DMA to be aborted, call directly user Abort complete callback */
ganlikun 0:13413ea9a877 1087 HAL_SMARTCARD_AbortTransmitCpltCallback(hsc);
ganlikun 0:13413ea9a877 1088 }
ganlikun 0:13413ea9a877 1089 }
ganlikun 0:13413ea9a877 1090 else
ganlikun 0:13413ea9a877 1091 {
ganlikun 0:13413ea9a877 1092 /* Reset Tx transfer counter */
ganlikun 0:13413ea9a877 1093 hsc->TxXferCount = 0x00U;
ganlikun 0:13413ea9a877 1094
ganlikun 0:13413ea9a877 1095 /* Restore hsc->gState to Ready */
ganlikun 0:13413ea9a877 1096 hsc->gState = HAL_SMARTCARD_STATE_READY;
ganlikun 0:13413ea9a877 1097
ganlikun 0:13413ea9a877 1098 /* As no DMA to be aborted, call directly user Abort complete callback */
ganlikun 0:13413ea9a877 1099 HAL_SMARTCARD_AbortTransmitCpltCallback(hsc);
ganlikun 0:13413ea9a877 1100 }
ganlikun 0:13413ea9a877 1101
ganlikun 0:13413ea9a877 1102 return HAL_OK;
ganlikun 0:13413ea9a877 1103 }
ganlikun 0:13413ea9a877 1104
ganlikun 0:13413ea9a877 1105 /**
ganlikun 0:13413ea9a877 1106 * @brief Abort ongoing Receive transfer (Interrupt mode).
ganlikun 0:13413ea9a877 1107 * @param hsc SMARTCARD handle.
ganlikun 0:13413ea9a877 1108 * @note This procedure could be used for aborting any ongoing transfer started in Interrupt or DMA mode.
ganlikun 0:13413ea9a877 1109 * This procedure performs following operations :
ganlikun 0:13413ea9a877 1110 * - Disable PPP Interrupts
ganlikun 0:13413ea9a877 1111 * - Disable the DMA transfer in the peripheral register (if enabled)
ganlikun 0:13413ea9a877 1112 * - Abort DMA transfer by calling HAL_DMA_Abort_IT (in case of transfer in DMA mode)
ganlikun 0:13413ea9a877 1113 * - Set handle State to READY
ganlikun 0:13413ea9a877 1114 * - At abort completion, call user abort complete callback
ganlikun 0:13413ea9a877 1115 * @note This procedure is executed in Interrupt mode, meaning that abort procedure could be
ganlikun 0:13413ea9a877 1116 * considered as completed only when user abort complete callback is executed (not when exiting function).
ganlikun 0:13413ea9a877 1117 * @retval HAL status
ganlikun 0:13413ea9a877 1118 */
ganlikun 0:13413ea9a877 1119 HAL_StatusTypeDef HAL_SMARTCARD_AbortReceive_IT(SMARTCARD_HandleTypeDef *hsc)
ganlikun 0:13413ea9a877 1120 {
ganlikun 0:13413ea9a877 1121 /* Disable RXNE, PE and ERR (Frame error, noise error, overrun error) interrupts */
ganlikun 0:13413ea9a877 1122 CLEAR_BIT(hsc->Instance->CR1, (USART_CR1_RXNEIE | USART_CR1_PEIE));
ganlikun 0:13413ea9a877 1123 CLEAR_BIT(hsc->Instance->CR3, USART_CR3_EIE);
ganlikun 0:13413ea9a877 1124
ganlikun 0:13413ea9a877 1125 /* Disable the SMARTCARD DMA Rx request if enabled */
ganlikun 0:13413ea9a877 1126 if(HAL_IS_BIT_SET(hsc->Instance->CR3, USART_CR3_DMAR))
ganlikun 0:13413ea9a877 1127 {
ganlikun 0:13413ea9a877 1128 CLEAR_BIT(hsc->Instance->CR3, USART_CR3_DMAR);
ganlikun 0:13413ea9a877 1129
ganlikun 0:13413ea9a877 1130 /* Abort the SMARTCARD DMA Rx channel : use blocking DMA Abort API (no callback) */
ganlikun 0:13413ea9a877 1131 if(hsc->hdmarx != NULL)
ganlikun 0:13413ea9a877 1132 {
ganlikun 0:13413ea9a877 1133 /* Set the SMARTCARD DMA Abort callback :
ganlikun 0:13413ea9a877 1134 will lead to call HAL_SMARTCARD_AbortCpltCallback() at end of DMA abort procedure */
ganlikun 0:13413ea9a877 1135 hsc->hdmarx->XferAbortCallback = SMARTCARD_DMARxOnlyAbortCallback;
ganlikun 0:13413ea9a877 1136
ganlikun 0:13413ea9a877 1137 /* Abort DMA RX */
ganlikun 0:13413ea9a877 1138 if(HAL_DMA_Abort_IT(hsc->hdmarx) != HAL_OK)
ganlikun 0:13413ea9a877 1139 {
ganlikun 0:13413ea9a877 1140 /* Call Directly hsc->hdmarx->XferAbortCallback function in case of error */
ganlikun 0:13413ea9a877 1141 hsc->hdmarx->XferAbortCallback(hsc->hdmarx);
ganlikun 0:13413ea9a877 1142 }
ganlikun 0:13413ea9a877 1143 }
ganlikun 0:13413ea9a877 1144 else
ganlikun 0:13413ea9a877 1145 {
ganlikun 0:13413ea9a877 1146 /* Reset Rx transfer counter */
ganlikun 0:13413ea9a877 1147 hsc->RxXferCount = 0x00U;
ganlikun 0:13413ea9a877 1148
ganlikun 0:13413ea9a877 1149 /* Restore hsc->RxState to Ready */
ganlikun 0:13413ea9a877 1150 hsc->RxState = HAL_SMARTCARD_STATE_READY;
ganlikun 0:13413ea9a877 1151
ganlikun 0:13413ea9a877 1152 /* As no DMA to be aborted, call directly user Abort complete callback */
ganlikun 0:13413ea9a877 1153 HAL_SMARTCARD_AbortReceiveCpltCallback(hsc);
ganlikun 0:13413ea9a877 1154 }
ganlikun 0:13413ea9a877 1155 }
ganlikun 0:13413ea9a877 1156 else
ganlikun 0:13413ea9a877 1157 {
ganlikun 0:13413ea9a877 1158 /* Reset Rx transfer counter */
ganlikun 0:13413ea9a877 1159 hsc->RxXferCount = 0x00U;
ganlikun 0:13413ea9a877 1160
ganlikun 0:13413ea9a877 1161 /* Restore hsc->RxState to Ready */
ganlikun 0:13413ea9a877 1162 hsc->RxState = HAL_SMARTCARD_STATE_READY;
ganlikun 0:13413ea9a877 1163
ganlikun 0:13413ea9a877 1164 /* As no DMA to be aborted, call directly user Abort complete callback */
ganlikun 0:13413ea9a877 1165 HAL_SMARTCARD_AbortReceiveCpltCallback(hsc);
ganlikun 0:13413ea9a877 1166 }
ganlikun 0:13413ea9a877 1167
ganlikun 0:13413ea9a877 1168 return HAL_OK;
ganlikun 0:13413ea9a877 1169 }
ganlikun 0:13413ea9a877 1170
ganlikun 0:13413ea9a877 1171 /**
ganlikun 0:13413ea9a877 1172 * @brief This function handles SMARTCARD interrupt request.
ganlikun 0:13413ea9a877 1173 * @param hsc: pointer to a SMARTCARD_HandleTypeDef structure that contains
ganlikun 0:13413ea9a877 1174 * the configuration information for SMARTCARD module.
ganlikun 0:13413ea9a877 1175 * @retval None
ganlikun 0:13413ea9a877 1176 */
ganlikun 0:13413ea9a877 1177 void HAL_SMARTCARD_IRQHandler(SMARTCARD_HandleTypeDef *hsc)
ganlikun 0:13413ea9a877 1178 {
ganlikun 0:13413ea9a877 1179 uint32_t isrflags = READ_REG(hsc->Instance->SR);
ganlikun 0:13413ea9a877 1180 uint32_t cr1its = READ_REG(hsc->Instance->CR1);
ganlikun 0:13413ea9a877 1181 uint32_t cr3its = READ_REG(hsc->Instance->CR3);
ganlikun 0:13413ea9a877 1182 uint32_t dmarequest = 0x00U;
ganlikun 0:13413ea9a877 1183 uint32_t errorflags = 0x00U;
ganlikun 0:13413ea9a877 1184
ganlikun 0:13413ea9a877 1185 /* If no error occurs */
ganlikun 0:13413ea9a877 1186 errorflags = (isrflags & (uint32_t)(USART_SR_PE | USART_SR_FE | USART_SR_ORE | USART_SR_NE));
ganlikun 0:13413ea9a877 1187 if(errorflags == RESET)
ganlikun 0:13413ea9a877 1188 {
ganlikun 0:13413ea9a877 1189 /* SMARTCARD in mode Receiver -------------------------------------------------*/
ganlikun 0:13413ea9a877 1190 if(((isrflags & USART_SR_RXNE) != RESET) && ((cr1its & USART_CR1_RXNEIE) != RESET))
ganlikun 0:13413ea9a877 1191 {
ganlikun 0:13413ea9a877 1192 SMARTCARD_Receive_IT(hsc);
ganlikun 0:13413ea9a877 1193 return;
ganlikun 0:13413ea9a877 1194 }
ganlikun 0:13413ea9a877 1195 }
ganlikun 0:13413ea9a877 1196
ganlikun 0:13413ea9a877 1197 /* If some errors occur */
ganlikun 0:13413ea9a877 1198 if((errorflags != RESET) && (((cr3its & USART_CR3_EIE) != RESET) || ((cr1its & (USART_CR1_RXNEIE | USART_CR1_PEIE)) != RESET)))
ganlikun 0:13413ea9a877 1199 {
ganlikun 0:13413ea9a877 1200 /* SMARTCARD parity error interrupt occurred ---------------------------*/
ganlikun 0:13413ea9a877 1201 if(((isrflags & SMARTCARD_FLAG_PE) != RESET) && ((cr1its & USART_CR1_PEIE) != RESET))
ganlikun 0:13413ea9a877 1202 {
ganlikun 0:13413ea9a877 1203 hsc->ErrorCode |= HAL_SMARTCARD_ERROR_PE;
ganlikun 0:13413ea9a877 1204 }
ganlikun 0:13413ea9a877 1205
ganlikun 0:13413ea9a877 1206 /* SMARTCARD frame error interrupt occurred ----------------------------*/
ganlikun 0:13413ea9a877 1207 if(((isrflags & SMARTCARD_FLAG_FE) != RESET) && ((cr3its & USART_CR3_EIE) != RESET))
ganlikun 0:13413ea9a877 1208 {
ganlikun 0:13413ea9a877 1209 hsc->ErrorCode |= HAL_SMARTCARD_ERROR_FE;
ganlikun 0:13413ea9a877 1210 }
ganlikun 0:13413ea9a877 1211
ganlikun 0:13413ea9a877 1212 /* SMARTCARD noise error interrupt occurred ----------------------------*/
ganlikun 0:13413ea9a877 1213 if(((isrflags & SMARTCARD_FLAG_NE) != RESET) && ((cr3its & USART_CR3_EIE) != RESET))
ganlikun 0:13413ea9a877 1214 {
ganlikun 0:13413ea9a877 1215 hsc->ErrorCode |= HAL_SMARTCARD_ERROR_NE;
ganlikun 0:13413ea9a877 1216 }
ganlikun 0:13413ea9a877 1217
ganlikun 0:13413ea9a877 1218 /* SMARTCARD Over-Run interrupt occurred -------------------------------*/
ganlikun 0:13413ea9a877 1219 if(((isrflags & SMARTCARD_FLAG_ORE) != RESET) && ((cr3its & USART_CR3_EIE) != RESET))
ganlikun 0:13413ea9a877 1220 {
ganlikun 0:13413ea9a877 1221 hsc->ErrorCode |= HAL_SMARTCARD_ERROR_ORE;
ganlikun 0:13413ea9a877 1222 }
ganlikun 0:13413ea9a877 1223 /* Call the Error call Back in case of Errors */
ganlikun 0:13413ea9a877 1224 if(hsc->ErrorCode != HAL_SMARTCARD_ERROR_NONE)
ganlikun 0:13413ea9a877 1225 {
ganlikun 0:13413ea9a877 1226 /* SMARTCARD in mode Receiver -----------------------------------------------*/
ganlikun 0:13413ea9a877 1227 if(((isrflags & USART_SR_RXNE) != RESET) && ((cr1its & USART_CR1_RXNEIE) != RESET))
ganlikun 0:13413ea9a877 1228 {
ganlikun 0:13413ea9a877 1229 SMARTCARD_Receive_IT(hsc);
ganlikun 0:13413ea9a877 1230 }
ganlikun 0:13413ea9a877 1231
ganlikun 0:13413ea9a877 1232 /* If Overrun error occurs, or if any error occurs in DMA mode reception,
ganlikun 0:13413ea9a877 1233 consider error as blocking */
ganlikun 0:13413ea9a877 1234 dmarequest = HAL_IS_BIT_SET(hsc->Instance->CR3, USART_CR3_DMAR);
ganlikun 0:13413ea9a877 1235 if(((hsc->ErrorCode & HAL_SMARTCARD_ERROR_ORE) != RESET) || dmarequest)
ganlikun 0:13413ea9a877 1236 {
ganlikun 0:13413ea9a877 1237 /* Blocking error : transfer is aborted
ganlikun 0:13413ea9a877 1238 Set the SMARTCARD state ready to be able to start again the process,
ganlikun 0:13413ea9a877 1239 Disable Rx Interrupts, and disable Rx DMA request, if ongoing */
ganlikun 0:13413ea9a877 1240 SMARTCARD_EndRxTransfer(hsc);
ganlikun 0:13413ea9a877 1241 /* Disable the SMARTCARD DMA Rx request if enabled */
ganlikun 0:13413ea9a877 1242 if(HAL_IS_BIT_SET(hsc->Instance->CR3, USART_CR3_DMAR))
ganlikun 0:13413ea9a877 1243 {
ganlikun 0:13413ea9a877 1244 CLEAR_BIT(hsc->Instance->CR3, USART_CR3_DMAR);
ganlikun 0:13413ea9a877 1245
ganlikun 0:13413ea9a877 1246 /* Abort the SMARTCARD DMA Rx channel */
ganlikun 0:13413ea9a877 1247 if(hsc->hdmarx != NULL)
ganlikun 0:13413ea9a877 1248 {
ganlikun 0:13413ea9a877 1249 /* Set the SMARTCARD DMA Abort callback :
ganlikun 0:13413ea9a877 1250 will lead to call HAL_SMARTCARD_ErrorCallback() at end of DMA abort procedure */
ganlikun 0:13413ea9a877 1251 hsc->hdmarx->XferAbortCallback = SMARTCARD_DMAAbortOnError;
ganlikun 0:13413ea9a877 1252
ganlikun 0:13413ea9a877 1253 if(HAL_DMA_Abort_IT(hsc->hdmarx) != HAL_OK)
ganlikun 0:13413ea9a877 1254 {
ganlikun 0:13413ea9a877 1255 /* Call Directly XferAbortCallback function in case of error */
ganlikun 0:13413ea9a877 1256 hsc->hdmarx->XferAbortCallback(hsc->hdmarx);
ganlikun 0:13413ea9a877 1257 }
ganlikun 0:13413ea9a877 1258 }
ganlikun 0:13413ea9a877 1259 else
ganlikun 0:13413ea9a877 1260 {
ganlikun 0:13413ea9a877 1261 /* Call user error callback */
ganlikun 0:13413ea9a877 1262 HAL_SMARTCARD_ErrorCallback(hsc);
ganlikun 0:13413ea9a877 1263 }
ganlikun 0:13413ea9a877 1264 }
ganlikun 0:13413ea9a877 1265 else
ganlikun 0:13413ea9a877 1266 {
ganlikun 0:13413ea9a877 1267 /* Call user error callback */
ganlikun 0:13413ea9a877 1268 HAL_SMARTCARD_ErrorCallback(hsc);
ganlikun 0:13413ea9a877 1269 }
ganlikun 0:13413ea9a877 1270 }
ganlikun 0:13413ea9a877 1271 else
ganlikun 0:13413ea9a877 1272 {
ganlikun 0:13413ea9a877 1273 /* Call user error callback */
ganlikun 0:13413ea9a877 1274 HAL_SMARTCARD_ErrorCallback(hsc);
ganlikun 0:13413ea9a877 1275 hsc->ErrorCode = HAL_SMARTCARD_ERROR_NONE;
ganlikun 0:13413ea9a877 1276 }
ganlikun 0:13413ea9a877 1277 }
ganlikun 0:13413ea9a877 1278 return;
ganlikun 0:13413ea9a877 1279 } /* End if some error occurs */
ganlikun 0:13413ea9a877 1280
ganlikun 0:13413ea9a877 1281 /* SMARTCARD in mode Transmitter -------------------------------------------*/
ganlikun 0:13413ea9a877 1282 if(((isrflags & SMARTCARD_FLAG_TXE) != RESET) && ((cr1its & USART_CR1_TXEIE) != RESET))
ganlikun 0:13413ea9a877 1283 {
ganlikun 0:13413ea9a877 1284 SMARTCARD_Transmit_IT(hsc);
ganlikun 0:13413ea9a877 1285 return;
ganlikun 0:13413ea9a877 1286 }
ganlikun 0:13413ea9a877 1287
ganlikun 0:13413ea9a877 1288 /* SMARTCARD in mode Transmitter (transmission end) ------------------------*/
ganlikun 0:13413ea9a877 1289 if(((isrflags & SMARTCARD_FLAG_TC) != RESET) && ((cr1its & USART_CR1_TCIE) != RESET))
ganlikun 0:13413ea9a877 1290 {
ganlikun 0:13413ea9a877 1291 SMARTCARD_EndTransmit_IT(hsc);
ganlikun 0:13413ea9a877 1292 return;
ganlikun 0:13413ea9a877 1293 }
ganlikun 0:13413ea9a877 1294 }
ganlikun 0:13413ea9a877 1295
ganlikun 0:13413ea9a877 1296 /**
ganlikun 0:13413ea9a877 1297 * @brief Tx Transfer completed callbacks
ganlikun 0:13413ea9a877 1298 * @param hsc: pointer to a SMARTCARD_HandleTypeDef structure that contains
ganlikun 0:13413ea9a877 1299 * the configuration information for SMARTCARD module.
ganlikun 0:13413ea9a877 1300 * @retval None
ganlikun 0:13413ea9a877 1301 */
ganlikun 0:13413ea9a877 1302 __weak void HAL_SMARTCARD_TxCpltCallback(SMARTCARD_HandleTypeDef *hsc)
ganlikun 0:13413ea9a877 1303 {
ganlikun 0:13413ea9a877 1304 /* Prevent unused argument(s) compilation warning */
ganlikun 0:13413ea9a877 1305 UNUSED(hsc);
ganlikun 0:13413ea9a877 1306 /* NOTE : This function Should not be modified, when the callback is needed,
ganlikun 0:13413ea9a877 1307 the HAL_SMARTCARD_TxCpltCallback could be implemented in the user file
ganlikun 0:13413ea9a877 1308 */
ganlikun 0:13413ea9a877 1309 }
ganlikun 0:13413ea9a877 1310
ganlikun 0:13413ea9a877 1311 /**
ganlikun 0:13413ea9a877 1312 * @brief Rx Transfer completed callbacks
ganlikun 0:13413ea9a877 1313 * @param hsc: pointer to a SMARTCARD_HandleTypeDef structure that contains
ganlikun 0:13413ea9a877 1314 * the configuration information for SMARTCARD module.
ganlikun 0:13413ea9a877 1315 * @retval None
ganlikun 0:13413ea9a877 1316 */
ganlikun 0:13413ea9a877 1317 __weak void HAL_SMARTCARD_RxCpltCallback(SMARTCARD_HandleTypeDef *hsc)
ganlikun 0:13413ea9a877 1318 {
ganlikun 0:13413ea9a877 1319 /* Prevent unused argument(s) compilation warning */
ganlikun 0:13413ea9a877 1320 UNUSED(hsc);
ganlikun 0:13413ea9a877 1321 /* NOTE : This function Should not be modified, when the callback is needed,
ganlikun 0:13413ea9a877 1322 the HAL_SMARTCARD_TxCpltCallback could be implemented in the user file
ganlikun 0:13413ea9a877 1323 */
ganlikun 0:13413ea9a877 1324 }
ganlikun 0:13413ea9a877 1325
ganlikun 0:13413ea9a877 1326 /**
ganlikun 0:13413ea9a877 1327 * @brief SMARTCARD error callbacks
ganlikun 0:13413ea9a877 1328 * @param hsc: pointer to a SMARTCARD_HandleTypeDef structure that contains
ganlikun 0:13413ea9a877 1329 * the configuration information for SMARTCARD module.
ganlikun 0:13413ea9a877 1330 * @retval None
ganlikun 0:13413ea9a877 1331 */
ganlikun 0:13413ea9a877 1332 __weak void HAL_SMARTCARD_ErrorCallback(SMARTCARD_HandleTypeDef *hsc)
ganlikun 0:13413ea9a877 1333 {
ganlikun 0:13413ea9a877 1334 /* Prevent unused argument(s) compilation warning */
ganlikun 0:13413ea9a877 1335 UNUSED(hsc);
ganlikun 0:13413ea9a877 1336 /* NOTE : This function Should not be modified, when the callback is needed,
ganlikun 0:13413ea9a877 1337 the HAL_SMARTCARD_ErrorCallback could be implemented in the user file
ganlikun 0:13413ea9a877 1338 */
ganlikun 0:13413ea9a877 1339 }
ganlikun 0:13413ea9a877 1340
ganlikun 0:13413ea9a877 1341 /**
ganlikun 0:13413ea9a877 1342 * @brief SMARTCARD Abort Complete callback.
ganlikun 0:13413ea9a877 1343 * @param hsc SMARTCARD handle.
ganlikun 0:13413ea9a877 1344 * @retval None
ganlikun 0:13413ea9a877 1345 */
ganlikun 0:13413ea9a877 1346 __weak void HAL_SMARTCARD_AbortCpltCallback (SMARTCARD_HandleTypeDef *hsc)
ganlikun 0:13413ea9a877 1347 {
ganlikun 0:13413ea9a877 1348 /* Prevent unused argument(s) compilation warning */
ganlikun 0:13413ea9a877 1349 UNUSED(hsc);
ganlikun 0:13413ea9a877 1350
ganlikun 0:13413ea9a877 1351 /* NOTE : This function should not be modified, when the callback is needed,
ganlikun 0:13413ea9a877 1352 the HAL_SMARTCARD_AbortCpltCallback can be implemented in the user file.
ganlikun 0:13413ea9a877 1353 */
ganlikun 0:13413ea9a877 1354 }
ganlikun 0:13413ea9a877 1355
ganlikun 0:13413ea9a877 1356 /**
ganlikun 0:13413ea9a877 1357 * @brief SMARTCARD Abort Transmit Complete callback.
ganlikun 0:13413ea9a877 1358 * @param hsc SMARTCARD handle.
ganlikun 0:13413ea9a877 1359 * @retval None
ganlikun 0:13413ea9a877 1360 */
ganlikun 0:13413ea9a877 1361 __weak void HAL_SMARTCARD_AbortTransmitCpltCallback (SMARTCARD_HandleTypeDef *hsc)
ganlikun 0:13413ea9a877 1362 {
ganlikun 0:13413ea9a877 1363 /* Prevent unused argument(s) compilation warning */
ganlikun 0:13413ea9a877 1364 UNUSED(hsc);
ganlikun 0:13413ea9a877 1365
ganlikun 0:13413ea9a877 1366 /* NOTE : This function should not be modified, when the callback is needed,
ganlikun 0:13413ea9a877 1367 the HAL_SMARTCARD_AbortTransmitCpltCallback can be implemented in the user file.
ganlikun 0:13413ea9a877 1368 */
ganlikun 0:13413ea9a877 1369 }
ganlikun 0:13413ea9a877 1370
ganlikun 0:13413ea9a877 1371 /**
ganlikun 0:13413ea9a877 1372 * @brief SMARTCARD Abort ReceiveComplete callback.
ganlikun 0:13413ea9a877 1373 * @param hsc SMARTCARD handle.
ganlikun 0:13413ea9a877 1374 * @retval None
ganlikun 0:13413ea9a877 1375 */
ganlikun 0:13413ea9a877 1376 __weak void HAL_SMARTCARD_AbortReceiveCpltCallback (SMARTCARD_HandleTypeDef *hsc)
ganlikun 0:13413ea9a877 1377 {
ganlikun 0:13413ea9a877 1378 /* Prevent unused argument(s) compilation warning */
ganlikun 0:13413ea9a877 1379 UNUSED(hsc);
ganlikun 0:13413ea9a877 1380
ganlikun 0:13413ea9a877 1381 /* NOTE : This function should not be modified, when the callback is needed,
ganlikun 0:13413ea9a877 1382 the HAL_SMARTCARD_AbortReceiveCpltCallback can be implemented in the user file.
ganlikun 0:13413ea9a877 1383 */
ganlikun 0:13413ea9a877 1384 }
ganlikun 0:13413ea9a877 1385
ganlikun 0:13413ea9a877 1386 /**
ganlikun 0:13413ea9a877 1387 * @}
ganlikun 0:13413ea9a877 1388 */
ganlikun 0:13413ea9a877 1389
ganlikun 0:13413ea9a877 1390 /** @defgroup SMARTCARD_Exported_Functions_Group3 Peripheral State and Errors functions
ganlikun 0:13413ea9a877 1391 * @brief SMARTCARD State and Errors functions
ganlikun 0:13413ea9a877 1392 *
ganlikun 0:13413ea9a877 1393 @verbatim
ganlikun 0:13413ea9a877 1394 ===============================================================================
ganlikun 0:13413ea9a877 1395 ##### Peripheral State and Errors functions #####
ganlikun 0:13413ea9a877 1396 ===============================================================================
ganlikun 0:13413ea9a877 1397 [..]
ganlikun 0:13413ea9a877 1398 This subsection provides a set of functions allowing to control the SmartCard.
ganlikun 0:13413ea9a877 1399 (+) HAL_SMARTCARD_GetState() API can be helpful to check in run-time the state of the SmartCard peripheral.
ganlikun 0:13413ea9a877 1400 (+) HAL_SMARTCARD_GetError() check in run-time errors that could be occurred during communication.
ganlikun 0:13413ea9a877 1401 @endverbatim
ganlikun 0:13413ea9a877 1402 * @{
ganlikun 0:13413ea9a877 1403 */
ganlikun 0:13413ea9a877 1404
ganlikun 0:13413ea9a877 1405 /**
ganlikun 0:13413ea9a877 1406 * @brief return the SMARTCARD state
ganlikun 0:13413ea9a877 1407 * @param hsc: pointer to a SMARTCARD_HandleTypeDef structure that contains
ganlikun 0:13413ea9a877 1408 * the configuration information for SMARTCARD module.
ganlikun 0:13413ea9a877 1409 * @retval HAL state
ganlikun 0:13413ea9a877 1410 */
ganlikun 0:13413ea9a877 1411 HAL_SMARTCARD_StateTypeDef HAL_SMARTCARD_GetState(SMARTCARD_HandleTypeDef *hsc)
ganlikun 0:13413ea9a877 1412 {
ganlikun 0:13413ea9a877 1413 uint32_t temp1= 0x00U, temp2 = 0x00U;
ganlikun 0:13413ea9a877 1414 temp1 = hsc->gState;
ganlikun 0:13413ea9a877 1415 temp2 = hsc->RxState;
ganlikun 0:13413ea9a877 1416
ganlikun 0:13413ea9a877 1417 return (HAL_SMARTCARD_StateTypeDef)(temp1 | temp2);
ganlikun 0:13413ea9a877 1418 }
ganlikun 0:13413ea9a877 1419
ganlikun 0:13413ea9a877 1420 /**
ganlikun 0:13413ea9a877 1421 * @brief Return the SMARTCARD error code
ganlikun 0:13413ea9a877 1422 * @param hsc : pointer to a SMARTCARD_HandleTypeDef structure that contains
ganlikun 0:13413ea9a877 1423 * the configuration information for the specified SMARTCARD.
ganlikun 0:13413ea9a877 1424 * @retval SMARTCARD Error Code
ganlikun 0:13413ea9a877 1425 */
ganlikun 0:13413ea9a877 1426 uint32_t HAL_SMARTCARD_GetError(SMARTCARD_HandleTypeDef *hsc)
ganlikun 0:13413ea9a877 1427 {
ganlikun 0:13413ea9a877 1428 return hsc->ErrorCode;
ganlikun 0:13413ea9a877 1429 }
ganlikun 0:13413ea9a877 1430
ganlikun 0:13413ea9a877 1431 /**
ganlikun 0:13413ea9a877 1432 * @}
ganlikun 0:13413ea9a877 1433 */
ganlikun 0:13413ea9a877 1434
ganlikun 0:13413ea9a877 1435 /**
ganlikun 0:13413ea9a877 1436 * @brief DMA SMARTCARD transmit process complete callback
ganlikun 0:13413ea9a877 1437 * @param hdma: pointer to a DMA_HandleTypeDef structure that contains
ganlikun 0:13413ea9a877 1438 * the configuration information for the specified DMA module.
ganlikun 0:13413ea9a877 1439 * @retval None
ganlikun 0:13413ea9a877 1440 */
ganlikun 0:13413ea9a877 1441 static void SMARTCARD_DMATransmitCplt(DMA_HandleTypeDef *hdma)
ganlikun 0:13413ea9a877 1442 {
ganlikun 0:13413ea9a877 1443 SMARTCARD_HandleTypeDef* hsc = ( SMARTCARD_HandleTypeDef* )((DMA_HandleTypeDef* )hdma)->Parent;
ganlikun 0:13413ea9a877 1444
ganlikun 0:13413ea9a877 1445 hsc->TxXferCount = 0U;
ganlikun 0:13413ea9a877 1446
ganlikun 0:13413ea9a877 1447 /* Disable the DMA transfer for transmit request by setting the DMAT bit
ganlikun 0:13413ea9a877 1448 in the USART CR3 register */
ganlikun 0:13413ea9a877 1449 CLEAR_BIT(hsc->Instance->CR3, USART_CR3_DMAT);
ganlikun 0:13413ea9a877 1450
ganlikun 0:13413ea9a877 1451 /* Enable the SMARTCARD Transmit Complete Interrupt */
ganlikun 0:13413ea9a877 1452 SET_BIT(hsc->Instance->CR1, USART_CR1_TCIE);
ganlikun 0:13413ea9a877 1453 }
ganlikun 0:13413ea9a877 1454
ganlikun 0:13413ea9a877 1455 /**
ganlikun 0:13413ea9a877 1456 * @brief DMA SMARTCARD receive process complete callback
ganlikun 0:13413ea9a877 1457 * @param hdma: pointer to a DMA_HandleTypeDef structure that contains
ganlikun 0:13413ea9a877 1458 * the configuration information for the specified DMA module.
ganlikun 0:13413ea9a877 1459 * @retval None
ganlikun 0:13413ea9a877 1460 */
ganlikun 0:13413ea9a877 1461 static void SMARTCARD_DMAReceiveCplt(DMA_HandleTypeDef *hdma)
ganlikun 0:13413ea9a877 1462 {
ganlikun 0:13413ea9a877 1463 SMARTCARD_HandleTypeDef* hsc = ( SMARTCARD_HandleTypeDef* )((DMA_HandleTypeDef* )hdma)->Parent;
ganlikun 0:13413ea9a877 1464
ganlikun 0:13413ea9a877 1465 hsc->RxXferCount = 0U;
ganlikun 0:13413ea9a877 1466
ganlikun 0:13413ea9a877 1467 /* Disable RXNE, PE and ERR (Frame error, noise error, overrun error) interrupts */
ganlikun 0:13413ea9a877 1468 CLEAR_BIT(hsc->Instance->CR1, (USART_CR1_RXNEIE | USART_CR1_PEIE));
ganlikun 0:13413ea9a877 1469 CLEAR_BIT(hsc->Instance->CR3, USART_CR3_EIE);
ganlikun 0:13413ea9a877 1470
ganlikun 0:13413ea9a877 1471 /* Disable the DMA transfer for the receiver request by setting the DMAR bit
ganlikun 0:13413ea9a877 1472 in the USART CR3 register */
ganlikun 0:13413ea9a877 1473 CLEAR_BIT(hsc->Instance->CR3, USART_CR3_DMAR);
ganlikun 0:13413ea9a877 1474
ganlikun 0:13413ea9a877 1475 /* At end of Rx process, restore hsc->RxState to Ready */
ganlikun 0:13413ea9a877 1476 hsc->RxState = HAL_SMARTCARD_STATE_READY;
ganlikun 0:13413ea9a877 1477
ganlikun 0:13413ea9a877 1478 HAL_SMARTCARD_RxCpltCallback(hsc);
ganlikun 0:13413ea9a877 1479 }
ganlikun 0:13413ea9a877 1480
ganlikun 0:13413ea9a877 1481 /**
ganlikun 0:13413ea9a877 1482 * @brief DMA SMARTCARD communication error callback
ganlikun 0:13413ea9a877 1483 * @param hdma: pointer to a DMA_HandleTypeDef structure that contains
ganlikun 0:13413ea9a877 1484 * the configuration information for the specified DMA module.
ganlikun 0:13413ea9a877 1485 * @retval None
ganlikun 0:13413ea9a877 1486 */
ganlikun 0:13413ea9a877 1487 static void SMARTCARD_DMAError(DMA_HandleTypeDef *hdma)
ganlikun 0:13413ea9a877 1488 {
ganlikun 0:13413ea9a877 1489 uint32_t dmarequest = 0x00U;
ganlikun 0:13413ea9a877 1490 SMARTCARD_HandleTypeDef* hsc = ( SMARTCARD_HandleTypeDef* )((DMA_HandleTypeDef* )hdma)->Parent;
ganlikun 0:13413ea9a877 1491 hsc->RxXferCount = 0U;
ganlikun 0:13413ea9a877 1492 hsc->TxXferCount = 0U;
ganlikun 0:13413ea9a877 1493 hsc->ErrorCode = HAL_SMARTCARD_ERROR_DMA;
ganlikun 0:13413ea9a877 1494
ganlikun 0:13413ea9a877 1495 /* Stop SMARTCARD DMA Tx request if ongoing */
ganlikun 0:13413ea9a877 1496 dmarequest = HAL_IS_BIT_SET(hsc->Instance->CR3, USART_CR3_DMAT);
ganlikun 0:13413ea9a877 1497 if((hsc->gState == HAL_SMARTCARD_STATE_BUSY_TX) && dmarequest)
ganlikun 0:13413ea9a877 1498 {
ganlikun 0:13413ea9a877 1499 SMARTCARD_EndTxTransfer(hsc);
ganlikun 0:13413ea9a877 1500 }
ganlikun 0:13413ea9a877 1501
ganlikun 0:13413ea9a877 1502 /* Stop SMARTCARD DMA Rx request if ongoing */
ganlikun 0:13413ea9a877 1503 dmarequest = HAL_IS_BIT_SET(hsc->Instance->CR3, USART_CR3_DMAR);
ganlikun 0:13413ea9a877 1504 if((hsc->RxState == HAL_SMARTCARD_STATE_BUSY_RX) && dmarequest)
ganlikun 0:13413ea9a877 1505 {
ganlikun 0:13413ea9a877 1506 SMARTCARD_EndRxTransfer(hsc);
ganlikun 0:13413ea9a877 1507 }
ganlikun 0:13413ea9a877 1508
ganlikun 0:13413ea9a877 1509 HAL_SMARTCARD_ErrorCallback(hsc);
ganlikun 0:13413ea9a877 1510 }
ganlikun 0:13413ea9a877 1511
ganlikun 0:13413ea9a877 1512 /**
ganlikun 0:13413ea9a877 1513 * @brief This function handles SMARTCARD Communication Timeout.
ganlikun 0:13413ea9a877 1514 * @param hsc: SMARTCARD handle
ganlikun 0:13413ea9a877 1515 * @param Flag: specifies the SMARTCARD flag to check.
ganlikun 0:13413ea9a877 1516 * @param Status: The new Flag status (SET or RESET).
ganlikun 0:13413ea9a877 1517 * @param Timeout: Timeout duration
ganlikun 0:13413ea9a877 1518 * @param Tickstart: tick start value
ganlikun 0:13413ea9a877 1519 * @retval HAL status
ganlikun 0:13413ea9a877 1520 */
ganlikun 0:13413ea9a877 1521 static HAL_StatusTypeDef SMARTCARD_WaitOnFlagUntilTimeout(SMARTCARD_HandleTypeDef *hsc, uint32_t Flag, FlagStatus Status, uint32_t Tickstart, uint32_t Timeout)
ganlikun 0:13413ea9a877 1522 {
ganlikun 0:13413ea9a877 1523 /* Wait until flag is set */
ganlikun 0:13413ea9a877 1524 while((__HAL_SMARTCARD_GET_FLAG(hsc, Flag) ? SET : RESET) == Status)
ganlikun 0:13413ea9a877 1525 {
ganlikun 0:13413ea9a877 1526 /* Check for the Timeout */
ganlikun 0:13413ea9a877 1527 if(Timeout != HAL_MAX_DELAY)
ganlikun 0:13413ea9a877 1528 {
ganlikun 0:13413ea9a877 1529 if((Timeout == 0U)||((HAL_GetTick() - Tickstart ) > Timeout))
ganlikun 0:13413ea9a877 1530 {
ganlikun 0:13413ea9a877 1531 /* Disable TXE and RXNE interrupts for the interrupt process */
ganlikun 0:13413ea9a877 1532 CLEAR_BIT(hsc->Instance->CR1, USART_CR1_TXEIE);
ganlikun 0:13413ea9a877 1533 CLEAR_BIT(hsc->Instance->CR1, USART_CR1_RXNEIE);
ganlikun 0:13413ea9a877 1534
ganlikun 0:13413ea9a877 1535 hsc->gState= HAL_SMARTCARD_STATE_READY;
ganlikun 0:13413ea9a877 1536 hsc->RxState= HAL_SMARTCARD_STATE_READY;
ganlikun 0:13413ea9a877 1537
ganlikun 0:13413ea9a877 1538 /* Process Unlocked */
ganlikun 0:13413ea9a877 1539 __HAL_UNLOCK(hsc);
ganlikun 0:13413ea9a877 1540
ganlikun 0:13413ea9a877 1541 return HAL_TIMEOUT;
ganlikun 0:13413ea9a877 1542 }
ganlikun 0:13413ea9a877 1543 }
ganlikun 0:13413ea9a877 1544 }
ganlikun 0:13413ea9a877 1545 return HAL_OK;
ganlikun 0:13413ea9a877 1546 }
ganlikun 0:13413ea9a877 1547
ganlikun 0:13413ea9a877 1548 /**
ganlikun 0:13413ea9a877 1549 * @brief End ongoing Tx transfer on SMARTCARD peripheral (following error detection or Transmit completion).
ganlikun 0:13413ea9a877 1550 * @param hsc: SMARTCARD handle.
ganlikun 0:13413ea9a877 1551 * @retval None
ganlikun 0:13413ea9a877 1552 */
ganlikun 0:13413ea9a877 1553 static void SMARTCARD_EndTxTransfer(SMARTCARD_HandleTypeDef *hsc)
ganlikun 0:13413ea9a877 1554 {
ganlikun 0:13413ea9a877 1555 /* At end of Tx process, restore hsc->gState to Ready */
ganlikun 0:13413ea9a877 1556 hsc->gState = HAL_SMARTCARD_STATE_READY;
ganlikun 0:13413ea9a877 1557
ganlikun 0:13413ea9a877 1558 /* Disable TXEIE and TCIE interrupts */
ganlikun 0:13413ea9a877 1559 CLEAR_BIT(hsc->Instance->CR1, (USART_CR1_TXEIE | USART_CR1_TCIE));
ganlikun 0:13413ea9a877 1560 }
ganlikun 0:13413ea9a877 1561
ganlikun 0:13413ea9a877 1562
ganlikun 0:13413ea9a877 1563 /**
ganlikun 0:13413ea9a877 1564 * @brief End ongoing Rx transfer on SMARTCARD peripheral (following error detection or Reception completion).
ganlikun 0:13413ea9a877 1565 * @param hsc: SMARTCARD handle.
ganlikun 0:13413ea9a877 1566 * @retval None
ganlikun 0:13413ea9a877 1567 */
ganlikun 0:13413ea9a877 1568 static void SMARTCARD_EndRxTransfer(SMARTCARD_HandleTypeDef *hsc)
ganlikun 0:13413ea9a877 1569 {
ganlikun 0:13413ea9a877 1570 /* At end of Rx process, restore hsc->RxState to Ready */
ganlikun 0:13413ea9a877 1571 hsc->RxState = HAL_SMARTCARD_STATE_READY;
ganlikun 0:13413ea9a877 1572
ganlikun 0:13413ea9a877 1573 /* Disable RXNE, PE and ERR (Frame error, noise error, overrun error) interrupts */
ganlikun 0:13413ea9a877 1574 CLEAR_BIT(hsc->Instance->CR1, (USART_CR1_RXNEIE | USART_CR1_PEIE));
ganlikun 0:13413ea9a877 1575 CLEAR_BIT(hsc->Instance->CR3, USART_CR3_EIE);
ganlikun 0:13413ea9a877 1576 }
ganlikun 0:13413ea9a877 1577
ganlikun 0:13413ea9a877 1578 /**
ganlikun 0:13413ea9a877 1579 * @brief Send an amount of data in non blocking mode
ganlikun 0:13413ea9a877 1580 * @param hsc: pointer to a SMARTCARD_HandleTypeDef structure that contains
ganlikun 0:13413ea9a877 1581 * the configuration information for SMARTCARD module.
ganlikun 0:13413ea9a877 1582 * @retval HAL status
ganlikun 0:13413ea9a877 1583 */
ganlikun 0:13413ea9a877 1584 static HAL_StatusTypeDef SMARTCARD_Transmit_IT(SMARTCARD_HandleTypeDef *hsc)
ganlikun 0:13413ea9a877 1585 {
ganlikun 0:13413ea9a877 1586 uint16_t* tmp;
ganlikun 0:13413ea9a877 1587
ganlikun 0:13413ea9a877 1588 /* Check that a Tx process is ongoing */
ganlikun 0:13413ea9a877 1589 if(hsc->gState == HAL_SMARTCARD_STATE_BUSY_TX)
ganlikun 0:13413ea9a877 1590 {
ganlikun 0:13413ea9a877 1591 tmp = (uint16_t*) hsc->pTxBuffPtr;
ganlikun 0:13413ea9a877 1592 hsc->Instance->DR = (uint16_t)(*tmp & (uint16_t)0x01FF);
ganlikun 0:13413ea9a877 1593 hsc->pTxBuffPtr += 1U;
ganlikun 0:13413ea9a877 1594
ganlikun 0:13413ea9a877 1595 if(--hsc->TxXferCount == 0U)
ganlikun 0:13413ea9a877 1596 {
ganlikun 0:13413ea9a877 1597 /* Disable the SMARTCARD Transmit data register empty Interrupt */
ganlikun 0:13413ea9a877 1598 CLEAR_BIT(hsc->Instance->CR1, USART_CR1_TXEIE);
ganlikun 0:13413ea9a877 1599
ganlikun 0:13413ea9a877 1600 /* Enable the SMARTCARD Transmit Complete Interrupt */
ganlikun 0:13413ea9a877 1601 SET_BIT(hsc->Instance->CR1, USART_CR1_TCIE);
ganlikun 0:13413ea9a877 1602 }
ganlikun 0:13413ea9a877 1603
ganlikun 0:13413ea9a877 1604 return HAL_OK;
ganlikun 0:13413ea9a877 1605 }
ganlikun 0:13413ea9a877 1606 else
ganlikun 0:13413ea9a877 1607 {
ganlikun 0:13413ea9a877 1608 return HAL_BUSY;
ganlikun 0:13413ea9a877 1609 }
ganlikun 0:13413ea9a877 1610 }
ganlikun 0:13413ea9a877 1611
ganlikun 0:13413ea9a877 1612 /**
ganlikun 0:13413ea9a877 1613 * @brief Wraps up transmission in non blocking mode.
ganlikun 0:13413ea9a877 1614 * @param hsmartcard: pointer to a SMARTCARD_HandleTypeDef structure that contains
ganlikun 0:13413ea9a877 1615 * the configuration information for the specified SMARTCARD module.
ganlikun 0:13413ea9a877 1616 * @retval HAL status
ganlikun 0:13413ea9a877 1617 */
ganlikun 0:13413ea9a877 1618 static HAL_StatusTypeDef SMARTCARD_EndTransmit_IT(SMARTCARD_HandleTypeDef *hsmartcard)
ganlikun 0:13413ea9a877 1619 {
ganlikun 0:13413ea9a877 1620 /* Disable the SMARTCARD Transmit Complete Interrupt */
ganlikun 0:13413ea9a877 1621 CLEAR_BIT(hsmartcard->Instance->CR1, USART_CR1_TCIE);
ganlikun 0:13413ea9a877 1622
ganlikun 0:13413ea9a877 1623 /* Disable the SMARTCARD Error Interrupt: (Frame error, noise error, overrun error) */
ganlikun 0:13413ea9a877 1624 CLEAR_BIT(hsmartcard->Instance->CR3, USART_CR3_EIE);
ganlikun 0:13413ea9a877 1625
ganlikun 0:13413ea9a877 1626 /* Tx process is ended, restore hsmartcard->gState to Ready */
ganlikun 0:13413ea9a877 1627 hsmartcard->gState = HAL_SMARTCARD_STATE_READY;
ganlikun 0:13413ea9a877 1628
ganlikun 0:13413ea9a877 1629 HAL_SMARTCARD_TxCpltCallback(hsmartcard);
ganlikun 0:13413ea9a877 1630
ganlikun 0:13413ea9a877 1631 return HAL_OK;
ganlikun 0:13413ea9a877 1632 }
ganlikun 0:13413ea9a877 1633
ganlikun 0:13413ea9a877 1634 /**
ganlikun 0:13413ea9a877 1635 * @brief Receive an amount of data in non blocking mode
ganlikun 0:13413ea9a877 1636 * @param hsc: pointer to a SMARTCARD_HandleTypeDef structure that contains
ganlikun 0:13413ea9a877 1637 * the configuration information for SMARTCARD module.
ganlikun 0:13413ea9a877 1638 * @retval HAL status
ganlikun 0:13413ea9a877 1639 */
ganlikun 0:13413ea9a877 1640 static HAL_StatusTypeDef SMARTCARD_Receive_IT(SMARTCARD_HandleTypeDef *hsc)
ganlikun 0:13413ea9a877 1641 {
ganlikun 0:13413ea9a877 1642 uint16_t* tmp;
ganlikun 0:13413ea9a877 1643
ganlikun 0:13413ea9a877 1644 /* Check that a Rx process is ongoing */
ganlikun 0:13413ea9a877 1645 if(hsc->RxState == HAL_SMARTCARD_STATE_BUSY_RX)
ganlikun 0:13413ea9a877 1646 {
ganlikun 0:13413ea9a877 1647 tmp = (uint16_t*) hsc->pRxBuffPtr;
ganlikun 0:13413ea9a877 1648 *tmp = (uint8_t)(hsc->Instance->DR & (uint8_t)0x00FF);
ganlikun 0:13413ea9a877 1649 hsc->pRxBuffPtr += 1U;
ganlikun 0:13413ea9a877 1650
ganlikun 0:13413ea9a877 1651 if(--hsc->RxXferCount == 0U)
ganlikun 0:13413ea9a877 1652 {
ganlikun 0:13413ea9a877 1653 CLEAR_BIT(hsc->Instance->CR1, USART_CR1_RXNEIE);
ganlikun 0:13413ea9a877 1654
ganlikun 0:13413ea9a877 1655 /* Disable the SMARTCARD Parity Error Interrupt */
ganlikun 0:13413ea9a877 1656 CLEAR_BIT(hsc->Instance->CR1, USART_CR1_PEIE);
ganlikun 0:13413ea9a877 1657
ganlikun 0:13413ea9a877 1658 /* Disable the SMARTCARD Error Interrupt: (Frame error, noise error, overrun error) */
ganlikun 0:13413ea9a877 1659 CLEAR_BIT(hsc->Instance->CR3, USART_CR3_EIE);
ganlikun 0:13413ea9a877 1660
ganlikun 0:13413ea9a877 1661 /* Rx process is completed, restore hsc->RxState to Ready */
ganlikun 0:13413ea9a877 1662 hsc->RxState = HAL_SMARTCARD_STATE_READY;
ganlikun 0:13413ea9a877 1663
ganlikun 0:13413ea9a877 1664 HAL_SMARTCARD_RxCpltCallback(hsc);
ganlikun 0:13413ea9a877 1665
ganlikun 0:13413ea9a877 1666 return HAL_OK;
ganlikun 0:13413ea9a877 1667 }
ganlikun 0:13413ea9a877 1668 return HAL_OK;
ganlikun 0:13413ea9a877 1669 }
ganlikun 0:13413ea9a877 1670 else
ganlikun 0:13413ea9a877 1671 {
ganlikun 0:13413ea9a877 1672 return HAL_BUSY;
ganlikun 0:13413ea9a877 1673 }
ganlikun 0:13413ea9a877 1674 }
ganlikun 0:13413ea9a877 1675
ganlikun 0:13413ea9a877 1676 /**
ganlikun 0:13413ea9a877 1677 * @brief DMA SMARTCARD communication abort callback, when initiated by HAL services on Error
ganlikun 0:13413ea9a877 1678 * (To be called at end of DMA Abort procedure following error occurrence).
ganlikun 0:13413ea9a877 1679 * @param hdma DMA handle.
ganlikun 0:13413ea9a877 1680 * @retval None
ganlikun 0:13413ea9a877 1681 */
ganlikun 0:13413ea9a877 1682 static void SMARTCARD_DMAAbortOnError(DMA_HandleTypeDef *hdma)
ganlikun 0:13413ea9a877 1683 {
ganlikun 0:13413ea9a877 1684 SMARTCARD_HandleTypeDef* hsc = (SMARTCARD_HandleTypeDef* )((DMA_HandleTypeDef* )hdma)->Parent;
ganlikun 0:13413ea9a877 1685 hsc->RxXferCount = 0x00U;
ganlikun 0:13413ea9a877 1686 hsc->TxXferCount = 0x00U;
ganlikun 0:13413ea9a877 1687
ganlikun 0:13413ea9a877 1688 HAL_SMARTCARD_ErrorCallback(hsc);
ganlikun 0:13413ea9a877 1689 }
ganlikun 0:13413ea9a877 1690
ganlikun 0:13413ea9a877 1691 /**
ganlikun 0:13413ea9a877 1692 * @brief DMA SMARTCARD Tx communication abort callback, when initiated by user
ganlikun 0:13413ea9a877 1693 * (To be called at end of DMA Tx Abort procedure following user abort request).
ganlikun 0:13413ea9a877 1694 * @note When this callback is executed, User Abort complete call back is called only if no
ganlikun 0:13413ea9a877 1695 * Abort still ongoing for Rx DMA Handle.
ganlikun 0:13413ea9a877 1696 * @param hdma DMA handle.
ganlikun 0:13413ea9a877 1697 * @retval None
ganlikun 0:13413ea9a877 1698 */
ganlikun 0:13413ea9a877 1699 static void SMARTCARD_DMATxAbortCallback(DMA_HandleTypeDef *hdma)
ganlikun 0:13413ea9a877 1700 {
ganlikun 0:13413ea9a877 1701 SMARTCARD_HandleTypeDef* hsc = ( SMARTCARD_HandleTypeDef* )((DMA_HandleTypeDef* )hdma)->Parent;
ganlikun 0:13413ea9a877 1702
ganlikun 0:13413ea9a877 1703 hsc->hdmatx->XferAbortCallback = NULL;
ganlikun 0:13413ea9a877 1704
ganlikun 0:13413ea9a877 1705 /* Check if an Abort process is still ongoing */
ganlikun 0:13413ea9a877 1706 if(hsc->hdmarx != NULL)
ganlikun 0:13413ea9a877 1707 {
ganlikun 0:13413ea9a877 1708 if(hsc->hdmarx->XferAbortCallback != NULL)
ganlikun 0:13413ea9a877 1709 {
ganlikun 0:13413ea9a877 1710 return;
ganlikun 0:13413ea9a877 1711 }
ganlikun 0:13413ea9a877 1712 }
ganlikun 0:13413ea9a877 1713
ganlikun 0:13413ea9a877 1714 /* No Abort process still ongoing : All DMA channels are aborted, call user Abort Complete callback */
ganlikun 0:13413ea9a877 1715 hsc->TxXferCount = 0x00U;
ganlikun 0:13413ea9a877 1716 hsc->RxXferCount = 0x00U;
ganlikun 0:13413ea9a877 1717
ganlikun 0:13413ea9a877 1718 /* Reset ErrorCode */
ganlikun 0:13413ea9a877 1719 hsc->ErrorCode = HAL_SMARTCARD_ERROR_NONE;
ganlikun 0:13413ea9a877 1720
ganlikun 0:13413ea9a877 1721 /* Restore hsc->gState and hsc->RxState to Ready */
ganlikun 0:13413ea9a877 1722 hsc->gState = HAL_SMARTCARD_STATE_READY;
ganlikun 0:13413ea9a877 1723 hsc->RxState = HAL_SMARTCARD_STATE_READY;
ganlikun 0:13413ea9a877 1724
ganlikun 0:13413ea9a877 1725 /* Call user Abort complete callback */
ganlikun 0:13413ea9a877 1726 HAL_SMARTCARD_AbortCpltCallback(hsc);
ganlikun 0:13413ea9a877 1727 }
ganlikun 0:13413ea9a877 1728
ganlikun 0:13413ea9a877 1729 /**
ganlikun 0:13413ea9a877 1730 * @brief DMA SMARTCARD Rx communication abort callback, when initiated by user
ganlikun 0:13413ea9a877 1731 * (To be called at end of DMA Rx Abort procedure following user abort request).
ganlikun 0:13413ea9a877 1732 * @note When this callback is executed, User Abort complete call back is called only if no
ganlikun 0:13413ea9a877 1733 * Abort still ongoing for Tx DMA Handle.
ganlikun 0:13413ea9a877 1734 * @param hdma DMA handle.
ganlikun 0:13413ea9a877 1735 * @retval None
ganlikun 0:13413ea9a877 1736 */
ganlikun 0:13413ea9a877 1737 static void SMARTCARD_DMARxAbortCallback(DMA_HandleTypeDef *hdma)
ganlikun 0:13413ea9a877 1738 {
ganlikun 0:13413ea9a877 1739 SMARTCARD_HandleTypeDef* hsc = ( SMARTCARD_HandleTypeDef* )((DMA_HandleTypeDef* )hdma)->Parent;
ganlikun 0:13413ea9a877 1740
ganlikun 0:13413ea9a877 1741 hsc->hdmarx->XferAbortCallback = NULL;
ganlikun 0:13413ea9a877 1742
ganlikun 0:13413ea9a877 1743 /* Check if an Abort process is still ongoing */
ganlikun 0:13413ea9a877 1744 if(hsc->hdmatx != NULL)
ganlikun 0:13413ea9a877 1745 {
ganlikun 0:13413ea9a877 1746 if(hsc->hdmatx->XferAbortCallback != NULL)
ganlikun 0:13413ea9a877 1747 {
ganlikun 0:13413ea9a877 1748 return;
ganlikun 0:13413ea9a877 1749 }
ganlikun 0:13413ea9a877 1750 }
ganlikun 0:13413ea9a877 1751
ganlikun 0:13413ea9a877 1752 /* No Abort process still ongoing : All DMA channels are aborted, call user Abort Complete callback */
ganlikun 0:13413ea9a877 1753 hsc->TxXferCount = 0x00U;
ganlikun 0:13413ea9a877 1754 hsc->RxXferCount = 0x00U;
ganlikun 0:13413ea9a877 1755
ganlikun 0:13413ea9a877 1756 /* Reset ErrorCode */
ganlikun 0:13413ea9a877 1757 hsc->ErrorCode = HAL_SMARTCARD_ERROR_NONE;
ganlikun 0:13413ea9a877 1758
ganlikun 0:13413ea9a877 1759 /* Restore hsc->gState and hsc->RxState to Ready */
ganlikun 0:13413ea9a877 1760 hsc->gState = HAL_SMARTCARD_STATE_READY;
ganlikun 0:13413ea9a877 1761 hsc->RxState = HAL_SMARTCARD_STATE_READY;
ganlikun 0:13413ea9a877 1762
ganlikun 0:13413ea9a877 1763 /* Call user Abort complete callback */
ganlikun 0:13413ea9a877 1764 HAL_SMARTCARD_AbortCpltCallback(hsc);
ganlikun 0:13413ea9a877 1765 }
ganlikun 0:13413ea9a877 1766
ganlikun 0:13413ea9a877 1767 /**
ganlikun 0:13413ea9a877 1768 * @brief DMA SMARTCARD Tx communication abort callback, when initiated by user by a call to
ganlikun 0:13413ea9a877 1769 * HAL_SMARTCARD_AbortTransmit_IT API (Abort only Tx transfer)
ganlikun 0:13413ea9a877 1770 * (This callback is executed at end of DMA Tx Abort procedure following user abort request,
ganlikun 0:13413ea9a877 1771 * and leads to user Tx Abort Complete callback execution).
ganlikun 0:13413ea9a877 1772 * @param hdma DMA handle.
ganlikun 0:13413ea9a877 1773 * @retval None
ganlikun 0:13413ea9a877 1774 */
ganlikun 0:13413ea9a877 1775 static void SMARTCARD_DMATxOnlyAbortCallback(DMA_HandleTypeDef *hdma)
ganlikun 0:13413ea9a877 1776 {
ganlikun 0:13413ea9a877 1777 SMARTCARD_HandleTypeDef* hsc = ( SMARTCARD_HandleTypeDef* )((DMA_HandleTypeDef* )hdma)->Parent;
ganlikun 0:13413ea9a877 1778
ganlikun 0:13413ea9a877 1779 hsc->TxXferCount = 0x00U;
ganlikun 0:13413ea9a877 1780
ganlikun 0:13413ea9a877 1781 /* Restore hsc->gState to Ready */
ganlikun 0:13413ea9a877 1782 hsc->gState = HAL_SMARTCARD_STATE_READY;
ganlikun 0:13413ea9a877 1783
ganlikun 0:13413ea9a877 1784 /* Call user Abort complete callback */
ganlikun 0:13413ea9a877 1785 HAL_SMARTCARD_AbortTransmitCpltCallback(hsc);
ganlikun 0:13413ea9a877 1786 }
ganlikun 0:13413ea9a877 1787
ganlikun 0:13413ea9a877 1788 /**
ganlikun 0:13413ea9a877 1789 * @brief DMA SMARTCARD Rx communication abort callback, when initiated by user by a call to
ganlikun 0:13413ea9a877 1790 * HAL_SMARTCARD_AbortReceive_IT API (Abort only Rx transfer)
ganlikun 0:13413ea9a877 1791 * (This callback is executed at end of DMA Rx Abort procedure following user abort request,
ganlikun 0:13413ea9a877 1792 * and leads to user Rx Abort Complete callback execution).
ganlikun 0:13413ea9a877 1793 * @param hdma DMA handle.
ganlikun 0:13413ea9a877 1794 * @retval None
ganlikun 0:13413ea9a877 1795 */
ganlikun 0:13413ea9a877 1796 static void SMARTCARD_DMARxOnlyAbortCallback(DMA_HandleTypeDef *hdma)
ganlikun 0:13413ea9a877 1797 {
ganlikun 0:13413ea9a877 1798 SMARTCARD_HandleTypeDef* hsc = ( SMARTCARD_HandleTypeDef* )((DMA_HandleTypeDef* )hdma)->Parent;
ganlikun 0:13413ea9a877 1799
ganlikun 0:13413ea9a877 1800 hsc->RxXferCount = 0x00U;
ganlikun 0:13413ea9a877 1801
ganlikun 0:13413ea9a877 1802 /* Restore hsc->RxState to Ready */
ganlikun 0:13413ea9a877 1803 hsc->RxState = HAL_SMARTCARD_STATE_READY;
ganlikun 0:13413ea9a877 1804
ganlikun 0:13413ea9a877 1805 /* Call user Abort complete callback */
ganlikun 0:13413ea9a877 1806 HAL_SMARTCARD_AbortReceiveCpltCallback(hsc);
ganlikun 0:13413ea9a877 1807 }
ganlikun 0:13413ea9a877 1808
ganlikun 0:13413ea9a877 1809 /**
ganlikun 0:13413ea9a877 1810 * @brief Configure the SMARTCARD peripheral
ganlikun 0:13413ea9a877 1811 * @param hsc: pointer to a SMARTCARD_HandleTypeDef structure that contains
ganlikun 0:13413ea9a877 1812 * the configuration information for SMARTCARD module.
ganlikun 0:13413ea9a877 1813 * @retval None
ganlikun 0:13413ea9a877 1814 */
ganlikun 0:13413ea9a877 1815 static void SMARTCARD_SetConfig(SMARTCARD_HandleTypeDef *hsc)
ganlikun 0:13413ea9a877 1816 {
ganlikun 0:13413ea9a877 1817 uint32_t tmpreg = 0x00U;
ganlikun 0:13413ea9a877 1818
ganlikun 0:13413ea9a877 1819 /* Check the parameters */
ganlikun 0:13413ea9a877 1820 assert_param(IS_SMARTCARD_INSTANCE(hsc->Instance));
ganlikun 0:13413ea9a877 1821 assert_param(IS_SMARTCARD_POLARITY(hsc->Init.CLKPolarity));
ganlikun 0:13413ea9a877 1822 assert_param(IS_SMARTCARD_PHASE(hsc->Init.CLKPhase));
ganlikun 0:13413ea9a877 1823 assert_param(IS_SMARTCARD_LASTBIT(hsc->Init.CLKLastBit));
ganlikun 0:13413ea9a877 1824 assert_param(IS_SMARTCARD_BAUDRATE(hsc->Init.BaudRate));
ganlikun 0:13413ea9a877 1825 assert_param(IS_SMARTCARD_WORD_LENGTH(hsc->Init.WordLength));
ganlikun 0:13413ea9a877 1826 assert_param(IS_SMARTCARD_STOPBITS(hsc->Init.StopBits));
ganlikun 0:13413ea9a877 1827 assert_param(IS_SMARTCARD_PARITY(hsc->Init.Parity));
ganlikun 0:13413ea9a877 1828 assert_param(IS_SMARTCARD_MODE(hsc->Init.Mode));
ganlikun 0:13413ea9a877 1829 assert_param(IS_SMARTCARD_NACK_STATE(hsc->Init.NACKState));
ganlikun 0:13413ea9a877 1830
ganlikun 0:13413ea9a877 1831 /* The LBCL, CPOL and CPHA bits have to be selected when both the transmitter and the
ganlikun 0:13413ea9a877 1832 receiver are disabled (TE=RE=0) to ensure that the clock pulses function correctly. */
ganlikun 0:13413ea9a877 1833 CLEAR_BIT(hsc->Instance->CR1, (USART_CR1_TE | USART_CR1_RE));
ganlikun 0:13413ea9a877 1834
ganlikun 0:13413ea9a877 1835 /*---------------------------- USART CR2 Configuration ---------------------*/
ganlikun 0:13413ea9a877 1836 tmpreg = hsc->Instance->CR2;
ganlikun 0:13413ea9a877 1837 /* Clear CLKEN, CPOL, CPHA and LBCL bits */
ganlikun 0:13413ea9a877 1838 tmpreg &= (uint32_t)~((uint32_t)(USART_CR2_CPHA | USART_CR2_CPOL | USART_CR2_CLKEN | USART_CR2_LBCL));
ganlikun 0:13413ea9a877 1839 /* Configure the SMARTCARD Clock, CPOL, CPHA and LastBit -----------------------*/
ganlikun 0:13413ea9a877 1840 /* Set CPOL bit according to hsc->Init.CLKPolarity value */
ganlikun 0:13413ea9a877 1841 /* Set CPHA bit according to hsc->Init.CLKPhase value */
ganlikun 0:13413ea9a877 1842 /* Set LBCL bit according to hsc->Init.CLKLastBit value */
ganlikun 0:13413ea9a877 1843 /* Set Stop Bits: Set STOP[13:12] bits according to hsc->Init.StopBits value */
ganlikun 0:13413ea9a877 1844 tmpreg |= (uint32_t)(USART_CR2_CLKEN | hsc->Init.CLKPolarity |
ganlikun 0:13413ea9a877 1845 hsc->Init.CLKPhase| hsc->Init.CLKLastBit | hsc->Init.StopBits);
ganlikun 0:13413ea9a877 1846 /* Write to USART CR2 */
ganlikun 0:13413ea9a877 1847 WRITE_REG(hsc->Instance->CR2, (uint32_t)tmpreg);
ganlikun 0:13413ea9a877 1848
ganlikun 0:13413ea9a877 1849 tmpreg = hsc->Instance->CR2;
ganlikun 0:13413ea9a877 1850
ganlikun 0:13413ea9a877 1851 /* Clear STOP[13:12] bits */
ganlikun 0:13413ea9a877 1852 tmpreg &= (uint32_t)~((uint32_t)USART_CR2_STOP);
ganlikun 0:13413ea9a877 1853
ganlikun 0:13413ea9a877 1854 /* Set Stop Bits: Set STOP[13:12] bits according to hsc->Init.StopBits value */
ganlikun 0:13413ea9a877 1855 tmpreg |= (uint32_t)(hsc->Init.StopBits);
ganlikun 0:13413ea9a877 1856
ganlikun 0:13413ea9a877 1857 /* Write to USART CR2 */
ganlikun 0:13413ea9a877 1858 WRITE_REG(hsc->Instance->CR2, (uint32_t)tmpreg);
ganlikun 0:13413ea9a877 1859
ganlikun 0:13413ea9a877 1860 /*-------------------------- USART CR1 Configuration -----------------------*/
ganlikun 0:13413ea9a877 1861 tmpreg = hsc->Instance->CR1;
ganlikun 0:13413ea9a877 1862
ganlikun 0:13413ea9a877 1863 /* Clear M, PCE, PS, TE and RE bits */
ganlikun 0:13413ea9a877 1864 tmpreg &= (uint32_t)~((uint32_t)(USART_CR1_M | USART_CR1_PCE | USART_CR1_PS | USART_CR1_TE | \
ganlikun 0:13413ea9a877 1865 USART_CR1_RE));
ganlikun 0:13413ea9a877 1866
ganlikun 0:13413ea9a877 1867 /* Configure the SMARTCARD Word Length, Parity and mode:
ganlikun 0:13413ea9a877 1868 Set the M bits according to hsc->Init.WordLength value
ganlikun 0:13413ea9a877 1869 Set PCE and PS bits according to hsc->Init.Parity value
ganlikun 0:13413ea9a877 1870 Set TE and RE bits according to hsc->Init.Mode value */
ganlikun 0:13413ea9a877 1871 tmpreg |= (uint32_t)hsc->Init.WordLength | hsc->Init.Parity | hsc->Init.Mode;
ganlikun 0:13413ea9a877 1872
ganlikun 0:13413ea9a877 1873 /* Write to USART CR1 */
ganlikun 0:13413ea9a877 1874 WRITE_REG(hsc->Instance->CR1, (uint32_t)tmpreg);
ganlikun 0:13413ea9a877 1875
ganlikun 0:13413ea9a877 1876 /*-------------------------- USART CR3 Configuration -----------------------*/
ganlikun 0:13413ea9a877 1877 /* Clear CTSE and RTSE bits */
ganlikun 0:13413ea9a877 1878 CLEAR_BIT(hsc->Instance->CR3, (USART_CR3_RTSE | USART_CR3_CTSE));
ganlikun 0:13413ea9a877 1879
ganlikun 0:13413ea9a877 1880 /*-------------------------- USART BRR Configuration -----------------------*/
ganlikun 0:13413ea9a877 1881 #if defined(USART6)
ganlikun 0:13413ea9a877 1882 if((hsc->Instance == USART1) || (hsc->Instance == USART6))
ganlikun 0:13413ea9a877 1883 {
ganlikun 0:13413ea9a877 1884 hsc->Instance->BRR = SMARTCARD_BRR(HAL_RCC_GetPCLK2Freq(), hsc->Init.BaudRate);
ganlikun 0:13413ea9a877 1885 }
ganlikun 0:13413ea9a877 1886 #else
ganlikun 0:13413ea9a877 1887 if(hsc->Instance == USART1)
ganlikun 0:13413ea9a877 1888 {
ganlikun 0:13413ea9a877 1889 hsc->Instance->BRR = SMARTCARD_BRR(HAL_RCC_GetPCLK2Freq(), hsc->Init.BaudRate);
ganlikun 0:13413ea9a877 1890 }
ganlikun 0:13413ea9a877 1891 #endif /* USART6 */
ganlikun 0:13413ea9a877 1892 else
ganlikun 0:13413ea9a877 1893 {
ganlikun 0:13413ea9a877 1894 hsc->Instance->BRR = SMARTCARD_BRR(HAL_RCC_GetPCLK1Freq(), hsc->Init.BaudRate);
ganlikun 0:13413ea9a877 1895 }
ganlikun 0:13413ea9a877 1896 }
ganlikun 0:13413ea9a877 1897
ganlikun 0:13413ea9a877 1898 /**
ganlikun 0:13413ea9a877 1899 * @}
ganlikun 0:13413ea9a877 1900 */
ganlikun 0:13413ea9a877 1901
ganlikun 0:13413ea9a877 1902 #endif /* HAL_SMARTCARD_MODULE_ENABLED */
ganlikun 0:13413ea9a877 1903 /**
ganlikun 0:13413ea9a877 1904 * @}
ganlikun 0:13413ea9a877 1905 */
ganlikun 0:13413ea9a877 1906
ganlikun 0:13413ea9a877 1907 /**
ganlikun 0:13413ea9a877 1908 * @}
ganlikun 0:13413ea9a877 1909 */
ganlikun 0:13413ea9a877 1910
ganlikun 0:13413ea9a877 1911 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
ganlikun 0:13413ea9a877 1912