Go to most recent revision | Details | Last modification | View Log | RSS feed
| Rev | Author | Line No. | Line |
|---|---|---|---|
| 2 | mjames | 1 | /** |
| 2 | ****************************************************************************** |
||
| 3 | * @file stm32f0xx_hal_smartcard.c |
||
| 4 | * @author MCD Application Team |
||
| 5 | * @brief SMARTCARD HAL module driver. |
||
| 6 | * This file provides firmware functions to manage the following |
||
| 7 | * functionalities of the SMARTCARD peripheral: |
||
| 8 | * + Initialization and de-initialization functions |
||
| 9 | * + IO operation functions |
||
| 10 | * + Peripheral Control functions |
||
| 11 | * + Peripheral State and Error functions |
||
| 12 | * |
||
| 13 | @verbatim |
||
| 14 | ============================================================================== |
||
| 15 | ##### How to use this driver ##### |
||
| 16 | ============================================================================== |
||
| 17 | [..] |
||
| 18 | The SMARTCARD HAL driver can be used as follows: |
||
| 19 | |||
| 20 | (#) Declare a SMARTCARD_HandleTypeDef handle structure (eg. SMARTCARD_HandleTypeDef hsmartcard). |
||
| 21 | (#) Associate a USART to the SMARTCARD handle hsmartcard. |
||
| 22 | (#) Initialize the SMARTCARD low level resources by implementing the HAL_SMARTCARD_MspInit() API: |
||
| 23 | (++) Enable the USARTx interface clock. |
||
| 24 | (++) USART pins configuration: |
||
| 25 | (+++) Enable the clock for the USART GPIOs. |
||
| 26 | (+++) Configure the USART pins (TX as alternate function pull-up, RX as alternate function Input). |
||
| 27 | (++) NVIC configuration if you need to use interrupt process (HAL_SMARTCARD_Transmit_IT() |
||
| 28 | and HAL_SMARTCARD_Receive_IT() APIs): |
||
| 29 | (+++) Configure the USARTx interrupt priority. |
||
| 30 | (+++) Enable the NVIC USART IRQ handle. |
||
| 31 | (++) DMA Configuration if you need to use DMA process (HAL_SMARTCARD_Transmit_DMA() |
||
| 32 | and HAL_SMARTCARD_Receive_DMA() APIs): |
||
| 33 | (+++) Declare a DMA handle structure for the Tx/Rx channel. |
||
| 34 | (+++) Enable the DMAx interface clock. |
||
| 35 | (+++) Configure the declared DMA handle structure with the required Tx/Rx parameters. |
||
| 36 | (+++) Configure the DMA Tx/Rx channel. |
||
| 37 | (+++) Associate the initialized DMA handle to the SMARTCARD DMA Tx/Rx handle. |
||
| 38 | (+++) Configure the priority and enable the NVIC for the transfer complete interrupt on the DMA Tx/Rx channel. |
||
| 39 | |||
| 40 | (#) Program the Baud Rate, Parity, Mode(Receiver/Transmitter), clock enabling/disabling and accordingly, |
||
| 41 | the clock parameters (parity, phase, last bit), prescaler value, guard time and NACK on transmission |
||
| 42 | error enabling or disabling in the hsmartcard handle Init structure. |
||
| 43 | |||
| 44 | (#) If required, program SMARTCARD advanced features (TX/RX pins swap, TimeOut, auto-retry counter,...) |
||
| 45 | in the hsmartcard handle AdvancedInit structure. |
||
| 46 | |||
| 47 | (#) Initialize the SMARTCARD registers by calling the HAL_SMARTCARD_Init() API: |
||
| 48 | (++) This API configures also the low level Hardware GPIO, CLOCK, CORTEX...etc) |
||
| 49 | by calling the customized HAL_SMARTCARD_MspInit() API. |
||
| 50 | [..] |
||
| 51 | (@) The specific SMARTCARD interrupts (Transmission complete interrupt, |
||
| 52 | RXNE interrupt and Error Interrupts) will be managed using the macros |
||
| 53 | __HAL_SMARTCARD_ENABLE_IT() and __HAL_SMARTCARD_DISABLE_IT() inside the transmit and receive process. |
||
| 54 | |||
| 55 | [..] |
||
| 56 | [..] Three operation modes are available within this driver : |
||
| 57 | |||
| 58 | *** Polling mode IO operation *** |
||
| 59 | ================================= |
||
| 60 | [..] |
||
| 61 | (+) Send an amount of data in blocking mode using HAL_SMARTCARD_Transmit() |
||
| 62 | (+) Receive an amount of data in blocking mode using HAL_SMARTCARD_Receive() |
||
| 63 | |||
| 64 | *** Interrupt mode IO operation *** |
||
| 65 | =================================== |
||
| 66 | [..] |
||
| 67 | (+) Send an amount of data in non-blocking mode using HAL_SMARTCARD_Transmit_IT() |
||
| 68 | (+) At transmission end of transfer HAL_SMARTCARD_TxCpltCallback() is executed and user can |
||
| 69 | add his own code by customization of function pointer HAL_SMARTCARD_TxCpltCallback() |
||
| 70 | (+) Receive an amount of data in non-blocking mode using HAL_SMARTCARD_Receive_IT() |
||
| 71 | (+) At reception end of transfer HAL_SMARTCARD_RxCpltCallback() is executed and user can |
||
| 72 | add his own code by customization of function pointer HAL_SMARTCARD_RxCpltCallback() |
||
| 73 | (+) In case of transfer Error, HAL_SMARTCARD_ErrorCallback() function is executed and user can |
||
| 74 | add his own code by customization of function pointer HAL_SMARTCARD_ErrorCallback() |
||
| 75 | |||
| 76 | *** DMA mode IO operation *** |
||
| 77 | ============================== |
||
| 78 | [..] |
||
| 79 | (+) Send an amount of data in non-blocking mode (DMA) using HAL_SMARTCARD_Transmit_DMA() |
||
| 80 | (+) At transmission end of transfer HAL_SMARTCARD_TxCpltCallback() is executed and user can |
||
| 81 | add his own code by customization of function pointer HAL_SMARTCARD_TxCpltCallback() |
||
| 82 | (+) Receive an amount of data in non-blocking mode (DMA) using HAL_SMARTCARD_Receive_DMA() |
||
| 83 | (+) At reception end of transfer HAL_SMARTCARD_RxCpltCallback() is executed and user can |
||
| 84 | add his own code by customization of function pointer HAL_SMARTCARD_RxCpltCallback() |
||
| 85 | (+) In case of transfer Error, HAL_SMARTCARD_ErrorCallback() function is executed and user can |
||
| 86 | add his own code by customization of function pointer HAL_SMARTCARD_ErrorCallback() |
||
| 87 | |||
| 88 | *** SMARTCARD HAL driver macros list *** |
||
| 89 | ======================================== |
||
| 90 | [..] |
||
| 91 | Below the list of most used macros in SMARTCARD HAL driver. |
||
| 92 | |||
| 93 | (+) __HAL_SMARTCARD_GET_FLAG : Check whether or not the specified SMARTCARD flag is set |
||
| 94 | (+) __HAL_SMARTCARD_CLEAR_FLAG : Clear the specified SMARTCARD pending flag |
||
| 95 | (+) __HAL_SMARTCARD_ENABLE_IT: Enable the specified SMARTCARD interrupt |
||
| 96 | (+) __HAL_SMARTCARD_DISABLE_IT: Disable the specified SMARTCARD interrupt |
||
| 97 | (+) __HAL_SMARTCARD_GET_IT_SOURCE: Check whether or not the specified SMARTCARD interrupt is enabled |
||
| 98 | |||
| 99 | [..] |
||
| 100 | (@) You can refer to the SMARTCARD HAL driver header file for more useful macros |
||
| 101 | |||
| 102 | ##### Callback registration ##### |
||
| 103 | ================================== |
||
| 104 | |||
| 105 | [..] |
||
| 106 | The compilation define USE_HAL_SMARTCARD_REGISTER_CALLBACKS when set to 1 |
||
| 107 | allows the user to configure dynamically the driver callbacks. |
||
| 108 | |||
| 109 | [..] |
||
| 110 | Use Function @ref HAL_SMARTCARD_RegisterCallback() to register a user callback. |
||
| 111 | Function @ref HAL_SMARTCARD_RegisterCallback() allows to register following callbacks: |
||
| 112 | (+) TxCpltCallback : Tx Complete Callback. |
||
| 113 | (+) RxCpltCallback : Rx Complete Callback. |
||
| 114 | (+) ErrorCallback : Error Callback. |
||
| 115 | (+) AbortCpltCallback : Abort Complete Callback. |
||
| 116 | (+) AbortTransmitCpltCallback : Abort Transmit Complete Callback. |
||
| 117 | (+) AbortReceiveCpltCallback : Abort Receive Complete Callback. |
||
| 118 | (+) MspInitCallback : SMARTCARD MspInit. |
||
| 119 | (+) MspDeInitCallback : SMARTCARD MspDeInit. |
||
| 120 | This function takes as parameters the HAL peripheral handle, the Callback ID |
||
| 121 | and a pointer to the user callback function. |
||
| 122 | |||
| 123 | [..] |
||
| 124 | Use function @ref HAL_SMARTCARD_UnRegisterCallback() to reset a callback to the default |
||
| 125 | weak (surcharged) function. |
||
| 126 | @ref HAL_SMARTCARD_UnRegisterCallback() takes as parameters the HAL peripheral handle, |
||
| 127 | and the Callback ID. |
||
| 128 | This function allows to reset following callbacks: |
||
| 129 | (+) TxCpltCallback : Tx Complete Callback. |
||
| 130 | (+) RxCpltCallback : Rx Complete Callback. |
||
| 131 | (+) ErrorCallback : Error Callback. |
||
| 132 | (+) AbortCpltCallback : Abort Complete Callback. |
||
| 133 | (+) AbortTransmitCpltCallback : Abort Transmit Complete Callback. |
||
| 134 | (+) AbortReceiveCpltCallback : Abort Receive Complete Callback. |
||
| 135 | (+) MspInitCallback : SMARTCARD MspInit. |
||
| 136 | (+) MspDeInitCallback : SMARTCARD MspDeInit. |
||
| 137 | |||
| 138 | [..] |
||
| 139 | By default, after the @ref HAL_SMARTCARD_Init() and when the state is HAL_SMARTCARD_STATE_RESET |
||
| 140 | all callbacks are set to the corresponding weak (surcharged) functions: |
||
| 141 | examples @ref HAL_SMARTCARD_TxCpltCallback(), @ref HAL_SMARTCARD_RxCpltCallback(). |
||
| 142 | Exception done for MspInit and MspDeInit functions that are respectively |
||
| 143 | reset to the legacy weak (surcharged) functions in the @ref HAL_SMARTCARD_Init() |
||
| 144 | and @ref HAL_SMARTCARD_DeInit() only when these callbacks are null (not registered beforehand). |
||
| 145 | If not, MspInit or MspDeInit are not null, the @ref HAL_SMARTCARD_Init() and @ref HAL_SMARTCARD_DeInit() |
||
| 146 | keep and use the user MspInit/MspDeInit callbacks (registered beforehand). |
||
| 147 | |||
| 148 | [..] |
||
| 149 | Callbacks can be registered/unregistered in HAL_SMARTCARD_STATE_READY state only. |
||
| 150 | Exception done MspInit/MspDeInit that can be registered/unregistered |
||
| 151 | in HAL_SMARTCARD_STATE_READY or HAL_SMARTCARD_STATE_RESET state, thus registered (user) |
||
| 152 | MspInit/DeInit callbacks can be used during the Init/DeInit. |
||
| 153 | In that case first register the MspInit/MspDeInit user callbacks |
||
| 154 | using @ref HAL_SMARTCARD_RegisterCallback() before calling @ref HAL_SMARTCARD_DeInit() |
||
| 155 | or @ref HAL_SMARTCARD_Init() function. |
||
| 156 | |||
| 157 | [..] |
||
| 158 | When The compilation define USE_HAL_SMARTCARD_REGISTER_CALLBACKS is set to 0 or |
||
| 159 | not defined, the callback registration feature is not available |
||
| 160 | and weak (surcharged) callbacks are used. |
||
| 161 | |||
| 162 | |||
| 163 | @endverbatim |
||
| 164 | ****************************************************************************** |
||
| 165 | * @attention |
||
| 166 | * |
||
| 167 | * <h2><center>© Copyright (c) 2016 STMicroelectronics. |
||
| 168 | * All rights reserved.</center></h2> |
||
| 169 | * |
||
| 170 | * This software component is licensed by ST under BSD 3-Clause license, |
||
| 171 | * the "License"; You may not use this file except in compliance with the |
||
| 172 | * License. You may obtain a copy of the License at: |
||
| 173 | * opensource.org/licenses/BSD-3-Clause |
||
| 174 | * |
||
| 175 | ****************************************************************************** |
||
| 176 | */ |
||
| 177 | #if !defined(STM32F030x6) && !defined(STM32F030x8) && !defined(STM32F070x6) && !defined(STM32F070xB) && !defined(STM32F030xC) |
||
| 178 | /* Includes ------------------------------------------------------------------*/ |
||
| 179 | #include "stm32f0xx_hal.h" |
||
| 180 | |||
| 181 | /** @addtogroup STM32F0xx_HAL_Driver |
||
| 182 | * @{ |
||
| 183 | */ |
||
| 184 | |||
| 185 | /** @defgroup SMARTCARD SMARTCARD |
||
| 186 | * @brief HAL SMARTCARD module driver |
||
| 187 | * @{ |
||
| 188 | */ |
||
| 189 | |||
| 190 | #ifdef HAL_SMARTCARD_MODULE_ENABLED |
||
| 191 | |||
| 192 | /* Private typedef -----------------------------------------------------------*/ |
||
| 193 | /* Private define ------------------------------------------------------------*/ |
||
| 194 | /** @defgroup SMARTCARD_Private_Constants SMARTCARD Private Constants |
||
| 195 | * @{ |
||
| 196 | */ |
||
| 197 | #define SMARTCARD_TEACK_REACK_TIMEOUT 1000U /*!< SMARTCARD TX or RX enable acknowledge time-out value */ |
||
| 198 | |||
| 199 | #define USART_CR1_FIELDS ((uint32_t)(USART_CR1_M | USART_CR1_PCE | USART_CR1_PS | \ |
||
| 200 | USART_CR1_TE | USART_CR1_RE | USART_CR1_OVER8)) /*!< USART CR1 fields of parameters set by SMARTCARD_SetConfig API */ |
||
| 201 | |||
| 202 | #define USART_CR2_CLK_FIELDS ((uint32_t)(USART_CR2_CLKEN | USART_CR2_CPOL | USART_CR2_CPHA | \ |
||
| 203 | USART_CR2_LBCL)) /*!< SMARTCARD clock-related USART CR2 fields of parameters */ |
||
| 204 | |||
| 205 | #define USART_CR2_FIELDS ((uint32_t)(USART_CR2_RTOEN | USART_CR2_CLK_FIELDS | USART_CR2_STOP)) /*!< USART CR2 fields of parameters set by SMARTCARD_SetConfig API */ |
||
| 206 | |||
| 207 | #define USART_CR3_FIELDS ((uint32_t)(USART_CR3_ONEBIT | USART_CR3_NACK | USART_CR3_SCARCNT)) /*!< USART CR3 fields of parameters set by SMARTCARD_SetConfig API */ |
||
| 208 | |||
| 209 | #define USART_BRR_MIN 0x10U /*!< USART BRR minimum authorized value */ |
||
| 210 | |||
| 211 | #define USART_BRR_MAX 0x0000FFFFU /*!< USART BRR maximum authorized value */ |
||
| 212 | /** |
||
| 213 | * @} |
||
| 214 | */ |
||
| 215 | |||
| 216 | /* Private macros ------------------------------------------------------------*/ |
||
| 217 | /* Private variables ---------------------------------------------------------*/ |
||
| 218 | /* Private function prototypes -----------------------------------------------*/ |
||
| 219 | /** @addtogroup SMARTCARD_Private_Functions |
||
| 220 | * @{ |
||
| 221 | */ |
||
| 222 | #if (USE_HAL_SMARTCARD_REGISTER_CALLBACKS == 1) |
||
| 223 | void SMARTCARD_InitCallbacksToDefault(SMARTCARD_HandleTypeDef *hsmartcard); |
||
| 224 | #endif /* USE_HAL_SMARTCARD_REGISTER_CALLBACKS */ |
||
| 225 | static HAL_StatusTypeDef SMARTCARD_SetConfig(SMARTCARD_HandleTypeDef *hsmartcard); |
||
| 226 | static void SMARTCARD_AdvFeatureConfig(SMARTCARD_HandleTypeDef *hsmartcard); |
||
| 227 | static HAL_StatusTypeDef SMARTCARD_CheckIdleState(SMARTCARD_HandleTypeDef *hsmartcard); |
||
| 228 | static HAL_StatusTypeDef SMARTCARD_WaitOnFlagUntilTimeout(SMARTCARD_HandleTypeDef *hsmartcard, uint32_t Flag, |
||
| 229 | FlagStatus Status, uint32_t Tickstart, uint32_t Timeout); |
||
| 230 | static void SMARTCARD_EndTxTransfer(SMARTCARD_HandleTypeDef *hsmartcard); |
||
| 231 | static void SMARTCARD_EndRxTransfer(SMARTCARD_HandleTypeDef *hsmartcard); |
||
| 232 | static void SMARTCARD_DMATransmitCplt(DMA_HandleTypeDef *hdma); |
||
| 233 | static void SMARTCARD_DMAReceiveCplt(DMA_HandleTypeDef *hdma); |
||
| 234 | static void SMARTCARD_DMAError(DMA_HandleTypeDef *hdma); |
||
| 235 | static void SMARTCARD_DMAAbortOnError(DMA_HandleTypeDef *hdma); |
||
| 236 | static void SMARTCARD_DMATxAbortCallback(DMA_HandleTypeDef *hdma); |
||
| 237 | static void SMARTCARD_DMARxAbortCallback(DMA_HandleTypeDef *hdma); |
||
| 238 | static void SMARTCARD_DMATxOnlyAbortCallback(DMA_HandleTypeDef *hdma); |
||
| 239 | static void SMARTCARD_DMARxOnlyAbortCallback(DMA_HandleTypeDef *hdma); |
||
| 240 | static void SMARTCARD_TxISR(SMARTCARD_HandleTypeDef *hsmartcard); |
||
| 241 | static void SMARTCARD_EndTransmit_IT(SMARTCARD_HandleTypeDef *hsmartcard); |
||
| 242 | static void SMARTCARD_RxISR(SMARTCARD_HandleTypeDef *hsmartcard); |
||
| 243 | /** |
||
| 244 | * @} |
||
| 245 | */ |
||
| 246 | |||
| 247 | /* Exported functions --------------------------------------------------------*/ |
||
| 248 | |||
| 249 | /** @defgroup SMARTCARD_Exported_Functions SMARTCARD Exported Functions |
||
| 250 | * @{ |
||
| 251 | */ |
||
| 252 | |||
| 253 | /** @defgroup SMARTCARD_Exported_Functions_Group1 Initialization and de-initialization functions |
||
| 254 | * @brief Initialization and Configuration functions |
||
| 255 | * |
||
| 256 | @verbatim |
||
| 257 | ============================================================================== |
||
| 258 | ##### Initialization and Configuration functions ##### |
||
| 259 | ============================================================================== |
||
| 260 | [..] |
||
| 261 | This subsection provides a set of functions allowing to initialize the USARTx |
||
| 262 | associated to the SmartCard. |
||
| 263 | (+) These parameters can be configured: |
||
| 264 | (++) Baud Rate |
||
| 265 | (++) Parity: parity should be enabled, frame Length is fixed to 8 bits plus parity |
||
| 266 | (++) Receiver/transmitter modes |
||
| 267 | (++) Synchronous mode (and if enabled, phase, polarity and last bit parameters) |
||
| 268 | (++) Prescaler value |
||
| 269 | (++) Guard bit time |
||
| 270 | (++) NACK enabling or disabling on transmission error |
||
| 271 | |||
| 272 | (+) The following advanced features can be configured as well: |
||
| 273 | (++) TX and/or RX pin level inversion |
||
| 274 | (++) data logical level inversion |
||
| 275 | (++) RX and TX pins swap |
||
| 276 | (++) RX overrun detection disabling |
||
| 277 | (++) DMA disabling on RX error |
||
| 278 | (++) MSB first on communication line |
||
| 279 | (++) Time out enabling (and if activated, timeout value) |
||
| 280 | (++) Block length |
||
| 281 | (++) Auto-retry counter |
||
| 282 | [..] |
||
| 283 | The HAL_SMARTCARD_Init() API follows the USART synchronous configuration procedures |
||
| 284 | (details for the procedures are available in reference manual). |
||
| 285 | |||
| 286 | @endverbatim |
||
| 287 | |||
| 288 | The USART frame format is given in the following table: |
||
| 289 | |||
| 290 | Table 1. USART frame format. |
||
| 291 | +---------------------------------------------------------------+ |
||
| 292 | | M1M0 bits | PCE bit | USART frame | |
||
| 293 | |-----------------------|---------------------------------------| |
||
| 294 | | 01 | 1 | | SB | 8 bit data | PB | STB | | |
||
| 295 | +---------------------------------------------------------------+ |
||
| 296 | |||
| 297 | |||
| 298 | * @{ |
||
| 299 | */ |
||
| 300 | |||
| 301 | /** |
||
| 302 | * @brief Initialize the SMARTCARD mode according to the specified |
||
| 303 | * parameters in the SMARTCARD_HandleTypeDef and initialize the associated handle. |
||
| 304 | * @param hsmartcard Pointer to a SMARTCARD_HandleTypeDef structure that contains |
||
| 305 | * the configuration information for the specified SMARTCARD module. |
||
| 306 | * @retval HAL status |
||
| 307 | */ |
||
| 308 | HAL_StatusTypeDef HAL_SMARTCARD_Init(SMARTCARD_HandleTypeDef *hsmartcard) |
||
| 309 | { |
||
| 310 | /* Check the SMARTCARD handle allocation */ |
||
| 311 | if (hsmartcard == NULL) |
||
| 312 | { |
||
| 313 | return HAL_ERROR; |
||
| 314 | } |
||
| 315 | |||
| 316 | /* Check the USART associated to the SMARTCARD handle */ |
||
| 317 | assert_param(IS_SMARTCARD_INSTANCE(hsmartcard->Instance)); |
||
| 318 | |||
| 319 | if (hsmartcard->gState == HAL_SMARTCARD_STATE_RESET) |
||
| 320 | { |
||
| 321 | /* Allocate lock resource and initialize it */ |
||
| 322 | hsmartcard->Lock = HAL_UNLOCKED; |
||
| 323 | |||
| 324 | #if USE_HAL_SMARTCARD_REGISTER_CALLBACKS == 1 |
||
| 325 | SMARTCARD_InitCallbacksToDefault(hsmartcard); |
||
| 326 | |||
| 327 | if (hsmartcard->MspInitCallback == NULL) |
||
| 328 | { |
||
| 329 | hsmartcard->MspInitCallback = HAL_SMARTCARD_MspInit; |
||
| 330 | } |
||
| 331 | |||
| 332 | /* Init the low level hardware */ |
||
| 333 | hsmartcard->MspInitCallback(hsmartcard); |
||
| 334 | #else |
||
| 335 | /* Init the low level hardware : GPIO, CLOCK */ |
||
| 336 | HAL_SMARTCARD_MspInit(hsmartcard); |
||
| 337 | #endif /* USE_HAL_SMARTCARD_REGISTER_CALLBACKS */ |
||
| 338 | } |
||
| 339 | |||
| 340 | hsmartcard->gState = HAL_SMARTCARD_STATE_BUSY; |
||
| 341 | |||
| 342 | /* Disable the Peripheral to set smartcard mode */ |
||
| 343 | CLEAR_BIT(hsmartcard->Instance->CR1, USART_CR1_UE); |
||
| 344 | |||
| 345 | /* In SmartCard mode, the following bits must be kept cleared: |
||
| 346 | - LINEN in the USART_CR2 register, |
||
| 347 | - HDSEL and IREN bits in the USART_CR3 register.*/ |
||
| 348 | CLEAR_BIT(hsmartcard->Instance->CR2, USART_CR2_LINEN); |
||
| 349 | CLEAR_BIT(hsmartcard->Instance->CR3, (USART_CR3_HDSEL | USART_CR3_IREN)); |
||
| 350 | |||
| 351 | /* set the USART in SMARTCARD mode */ |
||
| 352 | SET_BIT(hsmartcard->Instance->CR3, USART_CR3_SCEN); |
||
| 353 | |||
| 354 | /* Set the SMARTCARD Communication parameters */ |
||
| 355 | if (SMARTCARD_SetConfig(hsmartcard) == HAL_ERROR) |
||
| 356 | { |
||
| 357 | return HAL_ERROR; |
||
| 358 | } |
||
| 359 | |||
| 360 | /* Set the SMARTCARD transmission completion indication */ |
||
| 361 | SMARTCARD_TRANSMISSION_COMPLETION_SETTING(hsmartcard); |
||
| 362 | |||
| 363 | if (hsmartcard->AdvancedInit.AdvFeatureInit != SMARTCARD_ADVFEATURE_NO_INIT) |
||
| 364 | { |
||
| 365 | SMARTCARD_AdvFeatureConfig(hsmartcard); |
||
| 366 | } |
||
| 367 | |||
| 368 | /* Enable the Peripheral */ |
||
| 369 | SET_BIT(hsmartcard->Instance->CR1, USART_CR1_UE); |
||
| 370 | |||
| 371 | /* TEACK and/or REACK to check before moving hsmartcard->gState and hsmartcard->RxState to Ready */ |
||
| 372 | return (SMARTCARD_CheckIdleState(hsmartcard)); |
||
| 373 | } |
||
| 374 | |||
| 375 | /** |
||
| 376 | * @brief DeInitialize the SMARTCARD peripheral. |
||
| 377 | * @param hsmartcard Pointer to a SMARTCARD_HandleTypeDef structure that contains |
||
| 378 | * the configuration information for the specified SMARTCARD module. |
||
| 379 | * @retval HAL status |
||
| 380 | */ |
||
| 381 | HAL_StatusTypeDef HAL_SMARTCARD_DeInit(SMARTCARD_HandleTypeDef *hsmartcard) |
||
| 382 | { |
||
| 383 | /* Check the SMARTCARD handle allocation */ |
||
| 384 | if (hsmartcard == NULL) |
||
| 385 | { |
||
| 386 | return HAL_ERROR; |
||
| 387 | } |
||
| 388 | |||
| 389 | /* Check the USART/UART associated to the SMARTCARD handle */ |
||
| 390 | assert_param(IS_SMARTCARD_INSTANCE(hsmartcard->Instance)); |
||
| 391 | |||
| 392 | hsmartcard->gState = HAL_SMARTCARD_STATE_BUSY; |
||
| 393 | |||
| 394 | /* Disable the Peripheral */ |
||
| 395 | CLEAR_BIT(hsmartcard->Instance->CR1, USART_CR1_UE); |
||
| 396 | |||
| 397 | WRITE_REG(hsmartcard->Instance->CR1, 0x0U); |
||
| 398 | WRITE_REG(hsmartcard->Instance->CR2, 0x0U); |
||
| 399 | WRITE_REG(hsmartcard->Instance->CR3, 0x0U); |
||
| 400 | WRITE_REG(hsmartcard->Instance->RTOR, 0x0U); |
||
| 401 | WRITE_REG(hsmartcard->Instance->GTPR, 0x0U); |
||
| 402 | |||
| 403 | /* DeInit the low level hardware */ |
||
| 404 | #if USE_HAL_SMARTCARD_REGISTER_CALLBACKS == 1 |
||
| 405 | if (hsmartcard->MspDeInitCallback == NULL) |
||
| 406 | { |
||
| 407 | hsmartcard->MspDeInitCallback = HAL_SMARTCARD_MspDeInit; |
||
| 408 | } |
||
| 409 | /* DeInit the low level hardware */ |
||
| 410 | hsmartcard->MspDeInitCallback(hsmartcard); |
||
| 411 | #else |
||
| 412 | HAL_SMARTCARD_MspDeInit(hsmartcard); |
||
| 413 | #endif /* USE_HAL_SMARTCARD_REGISTER_CALLBACKS */ |
||
| 414 | |||
| 415 | hsmartcard->ErrorCode = HAL_SMARTCARD_ERROR_NONE; |
||
| 416 | hsmartcard->gState = HAL_SMARTCARD_STATE_RESET; |
||
| 417 | hsmartcard->RxState = HAL_SMARTCARD_STATE_RESET; |
||
| 418 | |||
| 419 | /* Process Unlock */ |
||
| 420 | __HAL_UNLOCK(hsmartcard); |
||
| 421 | |||
| 422 | return HAL_OK; |
||
| 423 | } |
||
| 424 | |||
| 425 | /** |
||
| 426 | * @brief Initialize the SMARTCARD MSP. |
||
| 427 | * @param hsmartcard Pointer to a SMARTCARD_HandleTypeDef structure that contains |
||
| 428 | * the configuration information for the specified SMARTCARD module. |
||
| 429 | * @retval None |
||
| 430 | */ |
||
| 431 | __weak void HAL_SMARTCARD_MspInit(SMARTCARD_HandleTypeDef *hsmartcard) |
||
| 432 | { |
||
| 433 | /* Prevent unused argument(s) compilation warning */ |
||
| 434 | UNUSED(hsmartcard); |
||
| 435 | |||
| 436 | /* NOTE : This function should not be modified, when the callback is needed, |
||
| 437 | the HAL_SMARTCARD_MspInit can be implemented in the user file |
||
| 438 | */ |
||
| 439 | } |
||
| 440 | |||
| 441 | /** |
||
| 442 | * @brief DeInitialize the SMARTCARD MSP. |
||
| 443 | * @param hsmartcard Pointer to a SMARTCARD_HandleTypeDef structure that contains |
||
| 444 | * the configuration information for the specified SMARTCARD module. |
||
| 445 | * @retval None |
||
| 446 | */ |
||
| 447 | __weak void HAL_SMARTCARD_MspDeInit(SMARTCARD_HandleTypeDef *hsmartcard) |
||
| 448 | { |
||
| 449 | /* Prevent unused argument(s) compilation warning */ |
||
| 450 | UNUSED(hsmartcard); |
||
| 451 | |||
| 452 | /* NOTE : This function should not be modified, when the callback is needed, |
||
| 453 | the HAL_SMARTCARD_MspDeInit can be implemented in the user file |
||
| 454 | */ |
||
| 455 | } |
||
| 456 | |||
| 457 | #if (USE_HAL_SMARTCARD_REGISTER_CALLBACKS == 1) |
||
| 458 | /** |
||
| 459 | * @brief Register a User SMARTCARD Callback |
||
| 460 | * To be used instead of the weak predefined callback |
||
| 461 | * @param hsmartcard smartcard handle |
||
| 462 | * @param CallbackID ID of the callback to be registered |
||
| 463 | * This parameter can be one of the following values: |
||
| 464 | * @arg @ref HAL_SMARTCARD_TX_COMPLETE_CB_ID Tx Complete Callback ID |
||
| 465 | * @arg @ref HAL_SMARTCARD_RX_COMPLETE_CB_ID Rx Complete Callback ID |
||
| 466 | * @arg @ref HAL_SMARTCARD_ERROR_CB_ID Error Callback ID |
||
| 467 | * @arg @ref HAL_SMARTCARD_ABORT_COMPLETE_CB_ID Abort Complete Callback ID |
||
| 468 | * @arg @ref HAL_SMARTCARD_ABORT_TRANSMIT_COMPLETE_CB_ID Abort Transmit Complete Callback ID |
||
| 469 | * @arg @ref HAL_SMARTCARD_ABORT_RECEIVE_COMPLETE_CB_ID Abort Receive Complete Callback ID |
||
| 470 | * @arg @ref HAL_SMARTCARD_MSPINIT_CB_ID MspInit Callback ID |
||
| 471 | * @arg @ref HAL_SMARTCARD_MSPDEINIT_CB_ID MspDeInit Callback ID |
||
| 472 | * @param pCallback pointer to the Callback function |
||
| 473 | * @retval HAL status |
||
| 474 | */ |
||
| 475 | HAL_StatusTypeDef HAL_SMARTCARD_RegisterCallback(SMARTCARD_HandleTypeDef *hsmartcard, |
||
| 476 | HAL_SMARTCARD_CallbackIDTypeDef CallbackID, pSMARTCARD_CallbackTypeDef pCallback) |
||
| 477 | { |
||
| 478 | HAL_StatusTypeDef status = HAL_OK; |
||
| 479 | |||
| 480 | if (pCallback == NULL) |
||
| 481 | { |
||
| 482 | /* Update the error code */ |
||
| 483 | hsmartcard->ErrorCode |= HAL_SMARTCARD_ERROR_INVALID_CALLBACK; |
||
| 484 | |||
| 485 | return HAL_ERROR; |
||
| 486 | } |
||
| 487 | /* Process locked */ |
||
| 488 | __HAL_LOCK(hsmartcard); |
||
| 489 | |||
| 490 | if (hsmartcard->gState == HAL_SMARTCARD_STATE_READY) |
||
| 491 | { |
||
| 492 | switch (CallbackID) |
||
| 493 | { |
||
| 494 | |||
| 495 | case HAL_SMARTCARD_TX_COMPLETE_CB_ID : |
||
| 496 | hsmartcard->TxCpltCallback = pCallback; |
||
| 497 | break; |
||
| 498 | |||
| 499 | case HAL_SMARTCARD_RX_COMPLETE_CB_ID : |
||
| 500 | hsmartcard->RxCpltCallback = pCallback; |
||
| 501 | break; |
||
| 502 | |||
| 503 | case HAL_SMARTCARD_ERROR_CB_ID : |
||
| 504 | hsmartcard->ErrorCallback = pCallback; |
||
| 505 | break; |
||
| 506 | |||
| 507 | case HAL_SMARTCARD_ABORT_COMPLETE_CB_ID : |
||
| 508 | hsmartcard->AbortCpltCallback = pCallback; |
||
| 509 | break; |
||
| 510 | |||
| 511 | case HAL_SMARTCARD_ABORT_TRANSMIT_COMPLETE_CB_ID : |
||
| 512 | hsmartcard->AbortTransmitCpltCallback = pCallback; |
||
| 513 | break; |
||
| 514 | |||
| 515 | case HAL_SMARTCARD_ABORT_RECEIVE_COMPLETE_CB_ID : |
||
| 516 | hsmartcard->AbortReceiveCpltCallback = pCallback; |
||
| 517 | break; |
||
| 518 | |||
| 519 | |||
| 520 | case HAL_SMARTCARD_MSPINIT_CB_ID : |
||
| 521 | hsmartcard->MspInitCallback = pCallback; |
||
| 522 | break; |
||
| 523 | |||
| 524 | case HAL_SMARTCARD_MSPDEINIT_CB_ID : |
||
| 525 | hsmartcard->MspDeInitCallback = pCallback; |
||
| 526 | break; |
||
| 527 | |||
| 528 | default : |
||
| 529 | /* Update the error code */ |
||
| 530 | hsmartcard->ErrorCode |= HAL_SMARTCARD_ERROR_INVALID_CALLBACK; |
||
| 531 | |||
| 532 | /* Return error status */ |
||
| 533 | status = HAL_ERROR; |
||
| 534 | break; |
||
| 535 | } |
||
| 536 | } |
||
| 537 | else if (hsmartcard->gState == HAL_SMARTCARD_STATE_RESET) |
||
| 538 | { |
||
| 539 | switch (CallbackID) |
||
| 540 | { |
||
| 541 | case HAL_SMARTCARD_MSPINIT_CB_ID : |
||
| 542 | hsmartcard->MspInitCallback = pCallback; |
||
| 543 | break; |
||
| 544 | |||
| 545 | case HAL_SMARTCARD_MSPDEINIT_CB_ID : |
||
| 546 | hsmartcard->MspDeInitCallback = pCallback; |
||
| 547 | break; |
||
| 548 | |||
| 549 | default : |
||
| 550 | /* Update the error code */ |
||
| 551 | hsmartcard->ErrorCode |= HAL_SMARTCARD_ERROR_INVALID_CALLBACK; |
||
| 552 | |||
| 553 | /* Return error status */ |
||
| 554 | status = HAL_ERROR; |
||
| 555 | break; |
||
| 556 | } |
||
| 557 | } |
||
| 558 | else |
||
| 559 | { |
||
| 560 | /* Update the error code */ |
||
| 561 | hsmartcard->ErrorCode |= HAL_SMARTCARD_ERROR_INVALID_CALLBACK; |
||
| 562 | |||
| 563 | /* Return error status */ |
||
| 564 | status = HAL_ERROR; |
||
| 565 | } |
||
| 566 | |||
| 567 | /* Release Lock */ |
||
| 568 | __HAL_UNLOCK(hsmartcard); |
||
| 569 | |||
| 570 | return status; |
||
| 571 | } |
||
| 572 | |||
| 573 | /** |
||
| 574 | * @brief Unregister an SMARTCARD callback |
||
| 575 | * SMARTCARD callback is redirected to the weak predefined callback |
||
| 576 | * @param hsmartcard smartcard handle |
||
| 577 | * @param CallbackID ID of the callback to be unregistered |
||
| 578 | * This parameter can be one of the following values: |
||
| 579 | * @arg @ref HAL_SMARTCARD_TX_COMPLETE_CB_ID Tx Complete Callback ID |
||
| 580 | * @arg @ref HAL_SMARTCARD_RX_COMPLETE_CB_ID Rx Complete Callback ID |
||
| 581 | * @arg @ref HAL_SMARTCARD_ERROR_CB_ID Error Callback ID |
||
| 582 | * @arg @ref HAL_SMARTCARD_ABORT_COMPLETE_CB_ID Abort Complete Callback ID |
||
| 583 | * @arg @ref HAL_SMARTCARD_ABORT_TRANSMIT_COMPLETE_CB_ID Abort Transmit Complete Callback ID |
||
| 584 | * @arg @ref HAL_SMARTCARD_ABORT_RECEIVE_COMPLETE_CB_ID Abort Receive Complete Callback ID |
||
| 585 | * @arg @ref HAL_SMARTCARD_MSPINIT_CB_ID MspInit Callback ID |
||
| 586 | * @arg @ref HAL_SMARTCARD_MSPDEINIT_CB_ID MspDeInit Callback ID |
||
| 587 | * @retval HAL status |
||
| 588 | */ |
||
| 589 | HAL_StatusTypeDef HAL_SMARTCARD_UnRegisterCallback(SMARTCARD_HandleTypeDef *hsmartcard, |
||
| 590 | HAL_SMARTCARD_CallbackIDTypeDef CallbackID) |
||
| 591 | { |
||
| 592 | HAL_StatusTypeDef status = HAL_OK; |
||
| 593 | |||
| 594 | /* Process locked */ |
||
| 595 | __HAL_LOCK(hsmartcard); |
||
| 596 | |||
| 597 | if (HAL_SMARTCARD_STATE_READY == hsmartcard->gState) |
||
| 598 | { |
||
| 599 | switch (CallbackID) |
||
| 600 | { |
||
| 601 | case HAL_SMARTCARD_TX_COMPLETE_CB_ID : |
||
| 602 | hsmartcard->TxCpltCallback = HAL_SMARTCARD_TxCpltCallback; /* Legacy weak TxCpltCallback */ |
||
| 603 | break; |
||
| 604 | |||
| 605 | case HAL_SMARTCARD_RX_COMPLETE_CB_ID : |
||
| 606 | hsmartcard->RxCpltCallback = HAL_SMARTCARD_RxCpltCallback; /* Legacy weak RxCpltCallback */ |
||
| 607 | break; |
||
| 608 | |||
| 609 | case HAL_SMARTCARD_ERROR_CB_ID : |
||
| 610 | hsmartcard->ErrorCallback = HAL_SMARTCARD_ErrorCallback; /* Legacy weak ErrorCallback */ |
||
| 611 | break; |
||
| 612 | |||
| 613 | case HAL_SMARTCARD_ABORT_COMPLETE_CB_ID : |
||
| 614 | hsmartcard->AbortCpltCallback = HAL_SMARTCARD_AbortCpltCallback; /* Legacy weak AbortCpltCallback */ |
||
| 615 | break; |
||
| 616 | |||
| 617 | case HAL_SMARTCARD_ABORT_TRANSMIT_COMPLETE_CB_ID : |
||
| 618 | hsmartcard->AbortTransmitCpltCallback = HAL_SMARTCARD_AbortTransmitCpltCallback; /* Legacy weak AbortTransmitCpltCallback */ |
||
| 619 | break; |
||
| 620 | |||
| 621 | case HAL_SMARTCARD_ABORT_RECEIVE_COMPLETE_CB_ID : |
||
| 622 | hsmartcard->AbortReceiveCpltCallback = HAL_SMARTCARD_AbortReceiveCpltCallback; /* Legacy weak AbortReceiveCpltCallback */ |
||
| 623 | break; |
||
| 624 | |||
| 625 | |||
| 626 | case HAL_SMARTCARD_MSPINIT_CB_ID : |
||
| 627 | hsmartcard->MspInitCallback = HAL_SMARTCARD_MspInit; /* Legacy weak MspInitCallback */ |
||
| 628 | break; |
||
| 629 | |||
| 630 | case HAL_SMARTCARD_MSPDEINIT_CB_ID : |
||
| 631 | hsmartcard->MspDeInitCallback = HAL_SMARTCARD_MspDeInit; /* Legacy weak MspDeInitCallback */ |
||
| 632 | break; |
||
| 633 | |||
| 634 | default : |
||
| 635 | /* Update the error code */ |
||
| 636 | hsmartcard->ErrorCode |= HAL_SMARTCARD_ERROR_INVALID_CALLBACK; |
||
| 637 | |||
| 638 | /* Return error status */ |
||
| 639 | status = HAL_ERROR; |
||
| 640 | break; |
||
| 641 | } |
||
| 642 | } |
||
| 643 | else if (HAL_SMARTCARD_STATE_RESET == hsmartcard->gState) |
||
| 644 | { |
||
| 645 | switch (CallbackID) |
||
| 646 | { |
||
| 647 | case HAL_SMARTCARD_MSPINIT_CB_ID : |
||
| 648 | hsmartcard->MspInitCallback = HAL_SMARTCARD_MspInit; |
||
| 649 | break; |
||
| 650 | |||
| 651 | case HAL_SMARTCARD_MSPDEINIT_CB_ID : |
||
| 652 | hsmartcard->MspDeInitCallback = HAL_SMARTCARD_MspDeInit; |
||
| 653 | break; |
||
| 654 | |||
| 655 | default : |
||
| 656 | /* Update the error code */ |
||
| 657 | hsmartcard->ErrorCode |= HAL_SMARTCARD_ERROR_INVALID_CALLBACK; |
||
| 658 | |||
| 659 | /* Return error status */ |
||
| 660 | status = HAL_ERROR; |
||
| 661 | break; |
||
| 662 | } |
||
| 663 | } |
||
| 664 | else |
||
| 665 | { |
||
| 666 | /* Update the error code */ |
||
| 667 | hsmartcard->ErrorCode |= HAL_SMARTCARD_ERROR_INVALID_CALLBACK; |
||
| 668 | |||
| 669 | /* Return error status */ |
||
| 670 | status = HAL_ERROR; |
||
| 671 | } |
||
| 672 | |||
| 673 | /* Release Lock */ |
||
| 674 | __HAL_UNLOCK(hsmartcard); |
||
| 675 | |||
| 676 | return status; |
||
| 677 | } |
||
| 678 | #endif /* USE_HAL_SMARTCARD_REGISTER_CALLBACKS */ |
||
| 679 | |||
| 680 | /** |
||
| 681 | * @} |
||
| 682 | */ |
||
| 683 | |||
| 684 | /** @defgroup SMARTCARD_Exported_Functions_Group2 IO operation functions |
||
| 685 | * @brief SMARTCARD Transmit and Receive functions |
||
| 686 | * |
||
| 687 | @verbatim |
||
| 688 | ============================================================================== |
||
| 689 | ##### IO operation functions ##### |
||
| 690 | ============================================================================== |
||
| 691 | [..] |
||
| 692 | This subsection provides a set of functions allowing to manage the SMARTCARD data transfers. |
||
| 693 | |||
| 694 | [..] |
||
| 695 | Smartcard is a single wire half duplex communication protocol. |
||
| 696 | The Smartcard interface is designed to support asynchronous protocol Smartcards as |
||
| 697 | defined in the ISO 7816-3 standard. The USART should be configured as: |
||
| 698 | (+) 8 bits plus parity: where M=1 and PCE=1 in the USART_CR1 register |
||
| 699 | (+) 1.5 stop bits when transmitting and receiving: where STOP=11 in the USART_CR2 register. |
||
| 700 | |||
| 701 | [..] |
||
| 702 | (+) There are two modes of transfer: |
||
| 703 | (++) Blocking mode: The communication is performed in polling mode. |
||
| 704 | The HAL status of all data processing is returned by the same function |
||
| 705 | after finishing transfer. |
||
| 706 | (++) Non-Blocking mode: The communication is performed using Interrupts |
||
| 707 | or DMA, the relevant API's return the HAL status. |
||
| 708 | The end of the data processing will be indicated through the |
||
| 709 | dedicated SMARTCARD IRQ when using Interrupt mode or the DMA IRQ when |
||
| 710 | using DMA mode. |
||
| 711 | (++) The HAL_SMARTCARD_TxCpltCallback(), HAL_SMARTCARD_RxCpltCallback() user callbacks |
||
| 712 | will be executed respectively at the end of the Transmit or Receive process |
||
| 713 | The HAL_SMARTCARD_ErrorCallback() user callback will be executed when a communication |
||
| 714 | error is detected. |
||
| 715 | |||
| 716 | (+) Blocking mode APIs are : |
||
| 717 | (++) HAL_SMARTCARD_Transmit() |
||
| 718 | (++) HAL_SMARTCARD_Receive() |
||
| 719 | |||
| 720 | (+) Non Blocking mode APIs with Interrupt are : |
||
| 721 | (++) HAL_SMARTCARD_Transmit_IT() |
||
| 722 | (++) HAL_SMARTCARD_Receive_IT() |
||
| 723 | (++) HAL_SMARTCARD_IRQHandler() |
||
| 724 | |||
| 725 | (+) Non Blocking mode functions with DMA are : |
||
| 726 | (++) HAL_SMARTCARD_Transmit_DMA() |
||
| 727 | (++) HAL_SMARTCARD_Receive_DMA() |
||
| 728 | |||
| 729 | (+) A set of Transfer Complete Callbacks are provided in non Blocking mode: |
||
| 730 | (++) HAL_SMARTCARD_TxCpltCallback() |
||
| 731 | (++) HAL_SMARTCARD_RxCpltCallback() |
||
| 732 | (++) HAL_SMARTCARD_ErrorCallback() |
||
| 733 | |||
| 734 | [..] |
||
| 735 | (#) Non-Blocking mode transfers could be aborted using Abort API's : |
||
| 736 | (++) HAL_SMARTCARD_Abort() |
||
| 737 | (++) HAL_SMARTCARD_AbortTransmit() |
||
| 738 | (++) HAL_SMARTCARD_AbortReceive() |
||
| 739 | (++) HAL_SMARTCARD_Abort_IT() |
||
| 740 | (++) HAL_SMARTCARD_AbortTransmit_IT() |
||
| 741 | (++) HAL_SMARTCARD_AbortReceive_IT() |
||
| 742 | |||
| 743 | (#) For Abort services based on interrupts (HAL_SMARTCARD_Abortxxx_IT), a set of Abort Complete Callbacks are provided: |
||
| 744 | (++) HAL_SMARTCARD_AbortCpltCallback() |
||
| 745 | (++) HAL_SMARTCARD_AbortTransmitCpltCallback() |
||
| 746 | (++) HAL_SMARTCARD_AbortReceiveCpltCallback() |
||
| 747 | |||
| 748 | (#) In Non-Blocking mode transfers, possible errors are split into 2 categories. |
||
| 749 | Errors are handled as follows : |
||
| 750 | (++) Error is considered as Recoverable and non blocking : Transfer could go till end, but error severity is |
||
| 751 | to be evaluated by user : this concerns Frame Error, Parity Error or Noise Error in Interrupt mode reception . |
||
| 752 | Received character is then retrieved and stored in Rx buffer, Error code is set to allow user to identify error type, |
||
| 753 | and HAL_SMARTCARD_ErrorCallback() user callback is executed. Transfer is kept ongoing on SMARTCARD side. |
||
| 754 | If user wants to abort it, Abort services should be called by user. |
||
| 755 | (++) Error is considered as Blocking : Transfer could not be completed properly and is aborted. |
||
| 756 | This concerns Frame Error in Interrupt mode tranmission, Overrun Error in Interrupt mode reception and all errors in DMA mode. |
||
| 757 | Error code is set to allow user to identify error type, and HAL_SMARTCARD_ErrorCallback() user callback is executed. |
||
| 758 | |||
| 759 | @endverbatim |
||
| 760 | * @{ |
||
| 761 | */ |
||
| 762 | |||
| 763 | /** |
||
| 764 | * @brief Send an amount of data in blocking mode. |
||
| 765 | * @param hsmartcard Pointer to a SMARTCARD_HandleTypeDef structure that contains |
||
| 766 | * the configuration information for the specified SMARTCARD module. |
||
| 767 | * @param pData pointer to data buffer. |
||
| 768 | * @param Size amount of data to be sent. |
||
| 769 | * @param Timeout Timeout duration. |
||
| 770 | * @retval HAL status |
||
| 771 | */ |
||
| 772 | HAL_StatusTypeDef HAL_SMARTCARD_Transmit(SMARTCARD_HandleTypeDef *hsmartcard, uint8_t *pData, uint16_t Size, |
||
| 773 | uint32_t Timeout) |
||
| 774 | { |
||
| 775 | uint32_t tickstart; |
||
| 776 | uint8_t *ptmpdata = pData; |
||
| 777 | |||
| 778 | /* Check that a Tx process is not already ongoing */ |
||
| 779 | if (hsmartcard->gState == HAL_SMARTCARD_STATE_READY) |
||
| 780 | { |
||
| 781 | if ((ptmpdata == NULL) || (Size == 0U)) |
||
| 782 | { |
||
| 783 | return HAL_ERROR; |
||
| 784 | } |
||
| 785 | |||
| 786 | /* Process Locked */ |
||
| 787 | __HAL_LOCK(hsmartcard); |
||
| 788 | |||
| 789 | hsmartcard->gState = HAL_SMARTCARD_STATE_BUSY_TX; |
||
| 790 | |||
| 791 | /* Init tickstart for timeout management */ |
||
| 792 | tickstart = HAL_GetTick(); |
||
| 793 | |||
| 794 | /* Disable the Peripheral first to update mode for TX master */ |
||
| 795 | CLEAR_BIT(hsmartcard->Instance->CR1, USART_CR1_UE); |
||
| 796 | |||
| 797 | /* Disable Rx, enable Tx */ |
||
| 798 | CLEAR_BIT(hsmartcard->Instance->CR1, USART_CR1_RE); |
||
| 799 | SET_BIT(hsmartcard->Instance->RQR, (uint16_t)SMARTCARD_RXDATA_FLUSH_REQUEST); |
||
| 800 | SET_BIT(hsmartcard->Instance->CR1, USART_CR1_TE); |
||
| 801 | |||
| 802 | /* Enable the Peripheral */ |
||
| 803 | SET_BIT(hsmartcard->Instance->CR1, USART_CR1_UE); |
||
| 804 | |||
| 805 | hsmartcard->ErrorCode = HAL_SMARTCARD_ERROR_NONE; |
||
| 806 | hsmartcard->TxXferSize = Size; |
||
| 807 | hsmartcard->TxXferCount = Size; |
||
| 808 | |||
| 809 | while (hsmartcard->TxXferCount > 0U) |
||
| 810 | { |
||
| 811 | hsmartcard->TxXferCount--; |
||
| 812 | if (SMARTCARD_WaitOnFlagUntilTimeout(hsmartcard, SMARTCARD_FLAG_TXE, RESET, tickstart, Timeout) != HAL_OK) |
||
| 813 | { |
||
| 814 | return HAL_TIMEOUT; |
||
| 815 | } |
||
| 816 | hsmartcard->Instance->TDR = (uint8_t)(*ptmpdata & 0xFFU); |
||
| 817 | ptmpdata++; |
||
| 818 | } |
||
| 819 | if (SMARTCARD_WaitOnFlagUntilTimeout(hsmartcard, SMARTCARD_TRANSMISSION_COMPLETION_FLAG(hsmartcard), RESET, tickstart, |
||
| 820 | Timeout) != HAL_OK) |
||
| 821 | { |
||
| 822 | return HAL_TIMEOUT; |
||
| 823 | } |
||
| 824 | /* Re-enable Rx at end of transmission if initial mode is Rx/Tx */ |
||
| 825 | if (hsmartcard->Init.Mode == SMARTCARD_MODE_TX_RX) |
||
| 826 | { |
||
| 827 | /* Disable the Peripheral first to update modes */ |
||
| 828 | CLEAR_BIT(hsmartcard->Instance->CR1, USART_CR1_UE); |
||
| 829 | SET_BIT(hsmartcard->Instance->CR1, USART_CR1_RE); |
||
| 830 | /* Enable the Peripheral */ |
||
| 831 | SET_BIT(hsmartcard->Instance->CR1, USART_CR1_UE); |
||
| 832 | } |
||
| 833 | |||
| 834 | /* At end of Tx process, restore hsmartcard->gState to Ready */ |
||
| 835 | hsmartcard->gState = HAL_SMARTCARD_STATE_READY; |
||
| 836 | |||
| 837 | /* Process Unlocked */ |
||
| 838 | __HAL_UNLOCK(hsmartcard); |
||
| 839 | |||
| 840 | return HAL_OK; |
||
| 841 | } |
||
| 842 | else |
||
| 843 | { |
||
| 844 | return HAL_BUSY; |
||
| 845 | } |
||
| 846 | } |
||
| 847 | |||
| 848 | /** |
||
| 849 | * @brief Receive an amount of data in blocking mode. |
||
| 850 | * @param hsmartcard Pointer to a SMARTCARD_HandleTypeDef structure that contains |
||
| 851 | * the configuration information for the specified SMARTCARD module. |
||
| 852 | * @param pData pointer to data buffer. |
||
| 853 | * @param Size amount of data to be received. |
||
| 854 | * @param Timeout Timeout duration. |
||
| 855 | * @retval HAL status |
||
| 856 | */ |
||
| 857 | HAL_StatusTypeDef HAL_SMARTCARD_Receive(SMARTCARD_HandleTypeDef *hsmartcard, uint8_t *pData, uint16_t Size, |
||
| 858 | uint32_t Timeout) |
||
| 859 | { |
||
| 860 | uint32_t tickstart; |
||
| 861 | uint8_t *ptmpdata = pData; |
||
| 862 | |||
| 863 | /* Check that a Rx process is not already ongoing */ |
||
| 864 | if (hsmartcard->RxState == HAL_SMARTCARD_STATE_READY) |
||
| 865 | { |
||
| 866 | if ((ptmpdata == NULL) || (Size == 0U)) |
||
| 867 | { |
||
| 868 | return HAL_ERROR; |
||
| 869 | } |
||
| 870 | |||
| 871 | /* Process Locked */ |
||
| 872 | __HAL_LOCK(hsmartcard); |
||
| 873 | |||
| 874 | hsmartcard->ErrorCode = HAL_SMARTCARD_ERROR_NONE; |
||
| 875 | hsmartcard->RxState = HAL_SMARTCARD_STATE_BUSY_RX; |
||
| 876 | |||
| 877 | /* Init tickstart for timeout management */ |
||
| 878 | tickstart = HAL_GetTick(); |
||
| 879 | |||
| 880 | hsmartcard->RxXferSize = Size; |
||
| 881 | hsmartcard->RxXferCount = Size; |
||
| 882 | |||
| 883 | /* Check the remain data to be received */ |
||
| 884 | while (hsmartcard->RxXferCount > 0U) |
||
| 885 | { |
||
| 886 | hsmartcard->RxXferCount--; |
||
| 887 | |||
| 888 | if (SMARTCARD_WaitOnFlagUntilTimeout(hsmartcard, SMARTCARD_FLAG_RXNE, RESET, tickstart, Timeout) != HAL_OK) |
||
| 889 | { |
||
| 890 | return HAL_TIMEOUT; |
||
| 891 | } |
||
| 892 | *ptmpdata = (uint8_t)(hsmartcard->Instance->RDR & (uint8_t)0x00FF); |
||
| 893 | ptmpdata++; |
||
| 894 | } |
||
| 895 | |||
| 896 | /* At end of Rx process, restore hsmartcard->RxState to Ready */ |
||
| 897 | hsmartcard->RxState = HAL_SMARTCARD_STATE_READY; |
||
| 898 | |||
| 899 | /* Process Unlocked */ |
||
| 900 | __HAL_UNLOCK(hsmartcard); |
||
| 901 | |||
| 902 | return HAL_OK; |
||
| 903 | } |
||
| 904 | else |
||
| 905 | { |
||
| 906 | return HAL_BUSY; |
||
| 907 | } |
||
| 908 | } |
||
| 909 | |||
| 910 | /** |
||
| 911 | * @brief Send an amount of data in interrupt mode. |
||
| 912 | * @param hsmartcard Pointer to a SMARTCARD_HandleTypeDef structure that contains |
||
| 913 | * the configuration information for the specified SMARTCARD module. |
||
| 914 | * @param pData pointer to data buffer. |
||
| 915 | * @param Size amount of data to be sent. |
||
| 916 | * @retval HAL status |
||
| 917 | */ |
||
| 918 | HAL_StatusTypeDef HAL_SMARTCARD_Transmit_IT(SMARTCARD_HandleTypeDef *hsmartcard, uint8_t *pData, uint16_t Size) |
||
| 919 | { |
||
| 920 | /* Check that a Tx process is not already ongoing */ |
||
| 921 | if (hsmartcard->gState == HAL_SMARTCARD_STATE_READY) |
||
| 922 | { |
||
| 923 | if ((pData == NULL) || (Size == 0U)) |
||
| 924 | { |
||
| 925 | return HAL_ERROR; |
||
| 926 | } |
||
| 927 | |||
| 928 | /* Process Locked */ |
||
| 929 | __HAL_LOCK(hsmartcard); |
||
| 930 | |||
| 931 | hsmartcard->ErrorCode = HAL_SMARTCARD_ERROR_NONE; |
||
| 932 | hsmartcard->gState = HAL_SMARTCARD_STATE_BUSY_TX; |
||
| 933 | |||
| 934 | hsmartcard->pTxBuffPtr = pData; |
||
| 935 | hsmartcard->TxXferSize = Size; |
||
| 936 | hsmartcard->TxXferCount = Size; |
||
| 937 | hsmartcard->TxISR = NULL; |
||
| 938 | |||
| 939 | /* Disable the Peripheral first to update mode for TX master */ |
||
| 940 | CLEAR_BIT(hsmartcard->Instance->CR1, USART_CR1_UE); |
||
| 941 | |||
| 942 | /* Disable Rx, enable Tx */ |
||
| 943 | CLEAR_BIT(hsmartcard->Instance->CR1, USART_CR1_RE); |
||
| 944 | SET_BIT(hsmartcard->Instance->RQR, (uint16_t)SMARTCARD_RXDATA_FLUSH_REQUEST); |
||
| 945 | SET_BIT(hsmartcard->Instance->CR1, USART_CR1_TE); |
||
| 946 | |||
| 947 | /* Enable the Peripheral */ |
||
| 948 | SET_BIT(hsmartcard->Instance->CR1, USART_CR1_UE); |
||
| 949 | |||
| 950 | /* Configure Tx interrupt processing */ |
||
| 951 | /* Set the Tx ISR function pointer */ |
||
| 952 | hsmartcard->TxISR = SMARTCARD_TxISR; |
||
| 953 | |||
| 954 | /* Process Unlocked */ |
||
| 955 | __HAL_UNLOCK(hsmartcard); |
||
| 956 | |||
| 957 | /* Enable the SMARTCARD Error Interrupt: (Frame error) */ |
||
| 958 | SET_BIT(hsmartcard->Instance->CR3, USART_CR3_EIE); |
||
| 959 | |||
| 960 | /* Enable the SMARTCARD Transmit Data Register Empty Interrupt */ |
||
| 961 | SET_BIT(hsmartcard->Instance->CR1, USART_CR1_TXEIE); |
||
| 962 | |||
| 963 | return HAL_OK; |
||
| 964 | } |
||
| 965 | else |
||
| 966 | { |
||
| 967 | return HAL_BUSY; |
||
| 968 | } |
||
| 969 | } |
||
| 970 | |||
| 971 | /** |
||
| 972 | * @brief Receive an amount of data in interrupt mode. |
||
| 973 | * @param hsmartcard Pointer to a SMARTCARD_HandleTypeDef structure that contains |
||
| 974 | * the configuration information for the specified SMARTCARD module. |
||
| 975 | * @param pData pointer to data buffer. |
||
| 976 | * @param Size amount of data to be received. |
||
| 977 | * @retval HAL status |
||
| 978 | */ |
||
| 979 | HAL_StatusTypeDef HAL_SMARTCARD_Receive_IT(SMARTCARD_HandleTypeDef *hsmartcard, uint8_t *pData, uint16_t Size) |
||
| 980 | { |
||
| 981 | /* Check that a Rx process is not already ongoing */ |
||
| 982 | if (hsmartcard->RxState == HAL_SMARTCARD_STATE_READY) |
||
| 983 | { |
||
| 984 | if ((pData == NULL) || (Size == 0U)) |
||
| 985 | { |
||
| 986 | return HAL_ERROR; |
||
| 987 | } |
||
| 988 | |||
| 989 | /* Process Locked */ |
||
| 990 | __HAL_LOCK(hsmartcard); |
||
| 991 | |||
| 992 | hsmartcard->ErrorCode = HAL_SMARTCARD_ERROR_NONE; |
||
| 993 | hsmartcard->RxState = HAL_SMARTCARD_STATE_BUSY_RX; |
||
| 994 | |||
| 995 | hsmartcard->pRxBuffPtr = pData; |
||
| 996 | hsmartcard->RxXferSize = Size; |
||
| 997 | hsmartcard->RxXferCount = Size; |
||
| 998 | |||
| 999 | /* Configure Rx interrupt processing */ |
||
| 1000 | /* Set the Rx ISR function pointer */ |
||
| 1001 | hsmartcard->RxISR = SMARTCARD_RxISR; |
||
| 1002 | |||
| 1003 | /* Process Unlocked */ |
||
| 1004 | __HAL_UNLOCK(hsmartcard); |
||
| 1005 | |||
| 1006 | /* Enable the SMARTCARD Parity Error and Data Register not empty Interrupts */ |
||
| 1007 | SET_BIT(hsmartcard->Instance->CR1, USART_CR1_PEIE | USART_CR1_RXNEIE); |
||
| 1008 | |||
| 1009 | /* Enable the SMARTCARD Error Interrupt: (Frame error, noise error, overrun error) */ |
||
| 1010 | SET_BIT(hsmartcard->Instance->CR3, USART_CR3_EIE); |
||
| 1011 | |||
| 1012 | return HAL_OK; |
||
| 1013 | } |
||
| 1014 | else |
||
| 1015 | { |
||
| 1016 | return HAL_BUSY; |
||
| 1017 | } |
||
| 1018 | } |
||
| 1019 | |||
| 1020 | /** |
||
| 1021 | * @brief Send an amount of data in DMA mode. |
||
| 1022 | * @param hsmartcard Pointer to a SMARTCARD_HandleTypeDef structure that contains |
||
| 1023 | * the configuration information for the specified SMARTCARD module. |
||
| 1024 | * @param pData pointer to data buffer. |
||
| 1025 | * @param Size amount of data to be sent. |
||
| 1026 | * @retval HAL status |
||
| 1027 | */ |
||
| 1028 | HAL_StatusTypeDef HAL_SMARTCARD_Transmit_DMA(SMARTCARD_HandleTypeDef *hsmartcard, uint8_t *pData, uint16_t Size) |
||
| 1029 | { |
||
| 1030 | /* Check that a Tx process is not already ongoing */ |
||
| 1031 | if (hsmartcard->gState == HAL_SMARTCARD_STATE_READY) |
||
| 1032 | { |
||
| 1033 | if ((pData == NULL) || (Size == 0U)) |
||
| 1034 | { |
||
| 1035 | return HAL_ERROR; |
||
| 1036 | } |
||
| 1037 | |||
| 1038 | /* Process Locked */ |
||
| 1039 | __HAL_LOCK(hsmartcard); |
||
| 1040 | |||
| 1041 | hsmartcard->gState = HAL_SMARTCARD_STATE_BUSY_TX; |
||
| 1042 | |||
| 1043 | hsmartcard->ErrorCode = HAL_SMARTCARD_ERROR_NONE; |
||
| 1044 | hsmartcard->pTxBuffPtr = pData; |
||
| 1045 | hsmartcard->TxXferSize = Size; |
||
| 1046 | hsmartcard->TxXferCount = Size; |
||
| 1047 | |||
| 1048 | /* Disable the Peripheral first to update mode for TX master */ |
||
| 1049 | CLEAR_BIT(hsmartcard->Instance->CR1, USART_CR1_UE); |
||
| 1050 | |||
| 1051 | /* Disable Rx, enable Tx */ |
||
| 1052 | CLEAR_BIT(hsmartcard->Instance->CR1, USART_CR1_RE); |
||
| 1053 | SET_BIT(hsmartcard->Instance->RQR, (uint16_t)SMARTCARD_RXDATA_FLUSH_REQUEST); |
||
| 1054 | SET_BIT(hsmartcard->Instance->CR1, USART_CR1_TE); |
||
| 1055 | |||
| 1056 | /* Enable the Peripheral */ |
||
| 1057 | SET_BIT(hsmartcard->Instance->CR1, USART_CR1_UE); |
||
| 1058 | |||
| 1059 | /* Set the SMARTCARD DMA transfer complete callback */ |
||
| 1060 | hsmartcard->hdmatx->XferCpltCallback = SMARTCARD_DMATransmitCplt; |
||
| 1061 | |||
| 1062 | /* Set the SMARTCARD error callback */ |
||
| 1063 | hsmartcard->hdmatx->XferErrorCallback = SMARTCARD_DMAError; |
||
| 1064 | |||
| 1065 | /* Set the DMA abort callback */ |
||
| 1066 | hsmartcard->hdmatx->XferAbortCallback = NULL; |
||
| 1067 | |||
| 1068 | /* Enable the SMARTCARD transmit DMA channel */ |
||
| 1069 | if (HAL_DMA_Start_IT(hsmartcard->hdmatx, (uint32_t)hsmartcard->pTxBuffPtr, (uint32_t)&hsmartcard->Instance->TDR, |
||
| 1070 | Size) == HAL_OK) |
||
| 1071 | { |
||
| 1072 | /* Clear the TC flag in the ICR register */ |
||
| 1073 | CLEAR_BIT(hsmartcard->Instance->ICR, USART_ICR_TCCF); |
||
| 1074 | |||
| 1075 | /* Process Unlocked */ |
||
| 1076 | __HAL_UNLOCK(hsmartcard); |
||
| 1077 | |||
| 1078 | /* Enable the UART Error Interrupt: (Frame error) */ |
||
| 1079 | SET_BIT(hsmartcard->Instance->CR3, USART_CR3_EIE); |
||
| 1080 | |||
| 1081 | /* Enable the DMA transfer for transmit request by setting the DMAT bit |
||
| 1082 | in the SMARTCARD associated USART CR3 register */ |
||
| 1083 | SET_BIT(hsmartcard->Instance->CR3, USART_CR3_DMAT); |
||
| 1084 | |||
| 1085 | return HAL_OK; |
||
| 1086 | } |
||
| 1087 | else |
||
| 1088 | { |
||
| 1089 | /* Set error code to DMA */ |
||
| 1090 | hsmartcard->ErrorCode = HAL_SMARTCARD_ERROR_DMA; |
||
| 1091 | |||
| 1092 | /* Process Unlocked */ |
||
| 1093 | __HAL_UNLOCK(hsmartcard); |
||
| 1094 | |||
| 1095 | /* Restore hsmartcard->State to ready */ |
||
| 1096 | hsmartcard->gState = HAL_SMARTCARD_STATE_READY; |
||
| 1097 | |||
| 1098 | return HAL_ERROR; |
||
| 1099 | } |
||
| 1100 | } |
||
| 1101 | else |
||
| 1102 | { |
||
| 1103 | return HAL_BUSY; |
||
| 1104 | } |
||
| 1105 | } |
||
| 1106 | |||
| 1107 | /** |
||
| 1108 | * @brief Receive an amount of data in DMA mode. |
||
| 1109 | * @param hsmartcard Pointer to a SMARTCARD_HandleTypeDef structure that contains |
||
| 1110 | * the configuration information for the specified SMARTCARD module. |
||
| 1111 | * @param pData pointer to data buffer. |
||
| 1112 | * @param Size amount of data to be received. |
||
| 1113 | * @note The SMARTCARD-associated USART parity is enabled (PCE = 1), |
||
| 1114 | * the received data contain the parity bit (MSB position). |
||
| 1115 | * @retval HAL status |
||
| 1116 | */ |
||
| 1117 | HAL_StatusTypeDef HAL_SMARTCARD_Receive_DMA(SMARTCARD_HandleTypeDef *hsmartcard, uint8_t *pData, uint16_t Size) |
||
| 1118 | { |
||
| 1119 | /* Check that a Rx process is not already ongoing */ |
||
| 1120 | if (hsmartcard->RxState == HAL_SMARTCARD_STATE_READY) |
||
| 1121 | { |
||
| 1122 | if ((pData == NULL) || (Size == 0U)) |
||
| 1123 | { |
||
| 1124 | return HAL_ERROR; |
||
| 1125 | } |
||
| 1126 | |||
| 1127 | /* Process Locked */ |
||
| 1128 | __HAL_LOCK(hsmartcard); |
||
| 1129 | |||
| 1130 | hsmartcard->ErrorCode = HAL_SMARTCARD_ERROR_NONE; |
||
| 1131 | hsmartcard->RxState = HAL_SMARTCARD_STATE_BUSY_RX; |
||
| 1132 | |||
| 1133 | hsmartcard->pRxBuffPtr = pData; |
||
| 1134 | hsmartcard->RxXferSize = Size; |
||
| 1135 | |||
| 1136 | /* Set the SMARTCARD DMA transfer complete callback */ |
||
| 1137 | hsmartcard->hdmarx->XferCpltCallback = SMARTCARD_DMAReceiveCplt; |
||
| 1138 | |||
| 1139 | /* Set the SMARTCARD DMA error callback */ |
||
| 1140 | hsmartcard->hdmarx->XferErrorCallback = SMARTCARD_DMAError; |
||
| 1141 | |||
| 1142 | /* Set the DMA abort callback */ |
||
| 1143 | hsmartcard->hdmarx->XferAbortCallback = NULL; |
||
| 1144 | |||
| 1145 | /* Enable the DMA channel */ |
||
| 1146 | if (HAL_DMA_Start_IT(hsmartcard->hdmarx, (uint32_t)&hsmartcard->Instance->RDR, (uint32_t)hsmartcard->pRxBuffPtr, |
||
| 1147 | Size) == HAL_OK) |
||
| 1148 | { |
||
| 1149 | /* Process Unlocked */ |
||
| 1150 | __HAL_UNLOCK(hsmartcard); |
||
| 1151 | |||
| 1152 | /* Enable the SMARTCARD Parity Error Interrupt */ |
||
| 1153 | SET_BIT(hsmartcard->Instance->CR1, USART_CR1_PEIE); |
||
| 1154 | |||
| 1155 | /* Enable the SMARTCARD Error Interrupt: (Frame error, noise error, overrun error) */ |
||
| 1156 | SET_BIT(hsmartcard->Instance->CR3, USART_CR3_EIE); |
||
| 1157 | |||
| 1158 | /* Enable the DMA transfer for the receiver request by setting the DMAR bit |
||
| 1159 | in the SMARTCARD associated USART CR3 register */ |
||
| 1160 | SET_BIT(hsmartcard->Instance->CR3, USART_CR3_DMAR); |
||
| 1161 | |||
| 1162 | return HAL_OK; |
||
| 1163 | } |
||
| 1164 | else |
||
| 1165 | { |
||
| 1166 | /* Set error code to DMA */ |
||
| 1167 | hsmartcard->ErrorCode = HAL_SMARTCARD_ERROR_DMA; |
||
| 1168 | |||
| 1169 | /* Process Unlocked */ |
||
| 1170 | __HAL_UNLOCK(hsmartcard); |
||
| 1171 | |||
| 1172 | /* Restore hsmartcard->State to ready */ |
||
| 1173 | hsmartcard->RxState = HAL_SMARTCARD_STATE_READY; |
||
| 1174 | |||
| 1175 | return HAL_ERROR; |
||
| 1176 | } |
||
| 1177 | } |
||
| 1178 | else |
||
| 1179 | { |
||
| 1180 | return HAL_BUSY; |
||
| 1181 | } |
||
| 1182 | } |
||
| 1183 | |||
| 1184 | /** |
||
| 1185 | * @brief Abort ongoing transfers (blocking mode). |
||
| 1186 | * @param hsmartcard Pointer to a SMARTCARD_HandleTypeDef structure that contains |
||
| 1187 | * the configuration information for the specified SMARTCARD module. |
||
| 1188 | * @note This procedure could be used for aborting any ongoing transfer started in Interrupt or DMA mode. |
||
| 1189 | * This procedure performs following operations : |
||
| 1190 | * - Disable SMARTCARD Interrupts (Tx and Rx) |
||
| 1191 | * - Disable the DMA transfer in the peripheral register (if enabled) |
||
| 1192 | * - Abort DMA transfer by calling HAL_DMA_Abort (in case of transfer in DMA mode) |
||
| 1193 | * - Set handle State to READY |
||
| 1194 | * @note This procedure is executed in blocking mode : when exiting function, Abort is considered as completed. |
||
| 1195 | * @retval HAL status |
||
| 1196 | */ |
||
| 1197 | HAL_StatusTypeDef HAL_SMARTCARD_Abort(SMARTCARD_HandleTypeDef *hsmartcard) |
||
| 1198 | { |
||
| 1199 | /* Disable RTOIE, EOBIE, TXEIE, TCIE, RXNE, PE and ERR (Frame error, noise error, overrun error) interrupts */ |
||
| 1200 | CLEAR_BIT(hsmartcard->Instance->CR1, |
||
| 1201 | (USART_CR1_RXNEIE | USART_CR1_PEIE | USART_CR1_TXEIE | USART_CR1_TCIE | USART_CR1_RTOIE | USART_CR1_EOBIE)); |
||
| 1202 | CLEAR_BIT(hsmartcard->Instance->CR3, USART_CR3_EIE); |
||
| 1203 | |||
| 1204 | /* Disable the SMARTCARD DMA Tx request if enabled */ |
||
| 1205 | if (HAL_IS_BIT_SET(hsmartcard->Instance->CR3, USART_CR3_DMAT)) |
||
| 1206 | { |
||
| 1207 | CLEAR_BIT(hsmartcard->Instance->CR3, USART_CR3_DMAT); |
||
| 1208 | |||
| 1209 | /* Abort the SMARTCARD DMA Tx channel : use blocking DMA Abort API (no callback) */ |
||
| 1210 | if (hsmartcard->hdmatx != NULL) |
||
| 1211 | { |
||
| 1212 | /* Set the SMARTCARD DMA Abort callback to Null. |
||
| 1213 | No call back execution at end of DMA abort procedure */ |
||
| 1214 | hsmartcard->hdmatx->XferAbortCallback = NULL; |
||
| 1215 | |||
| 1216 | if (HAL_DMA_Abort(hsmartcard->hdmatx) != HAL_OK) |
||
| 1217 | { |
||
| 1218 | if (HAL_DMA_GetError(hsmartcard->hdmatx) == HAL_DMA_ERROR_TIMEOUT) |
||
| 1219 | { |
||
| 1220 | /* Set error code to DMA */ |
||
| 1221 | hsmartcard->ErrorCode = HAL_SMARTCARD_ERROR_DMA; |
||
| 1222 | |||
| 1223 | return HAL_TIMEOUT; |
||
| 1224 | } |
||
| 1225 | } |
||
| 1226 | } |
||
| 1227 | } |
||
| 1228 | |||
| 1229 | /* Disable the SMARTCARD DMA Rx request if enabled */ |
||
| 1230 | if (HAL_IS_BIT_SET(hsmartcard->Instance->CR3, USART_CR3_DMAR)) |
||
| 1231 | { |
||
| 1232 | CLEAR_BIT(hsmartcard->Instance->CR3, USART_CR3_DMAR); |
||
| 1233 | |||
| 1234 | /* Abort the SMARTCARD DMA Rx channel : use blocking DMA Abort API (no callback) */ |
||
| 1235 | if (hsmartcard->hdmarx != NULL) |
||
| 1236 | { |
||
| 1237 | /* Set the SMARTCARD DMA Abort callback to Null. |
||
| 1238 | No call back execution at end of DMA abort procedure */ |
||
| 1239 | hsmartcard->hdmarx->XferAbortCallback = NULL; |
||
| 1240 | |||
| 1241 | if (HAL_DMA_Abort(hsmartcard->hdmarx) != HAL_OK) |
||
| 1242 | { |
||
| 1243 | if (HAL_DMA_GetError(hsmartcard->hdmarx) == HAL_DMA_ERROR_TIMEOUT) |
||
| 1244 | { |
||
| 1245 | /* Set error code to DMA */ |
||
| 1246 | hsmartcard->ErrorCode = HAL_SMARTCARD_ERROR_DMA; |
||
| 1247 | |||
| 1248 | return HAL_TIMEOUT; |
||
| 1249 | } |
||
| 1250 | } |
||
| 1251 | } |
||
| 1252 | } |
||
| 1253 | |||
| 1254 | /* Reset Tx and Rx transfer counters */ |
||
| 1255 | hsmartcard->TxXferCount = 0U; |
||
| 1256 | hsmartcard->RxXferCount = 0U; |
||
| 1257 | |||
| 1258 | /* Clear the Error flags in the ICR register */ |
||
| 1259 | __HAL_SMARTCARD_CLEAR_FLAG(hsmartcard, |
||
| 1260 | SMARTCARD_CLEAR_OREF | SMARTCARD_CLEAR_NEF | SMARTCARD_CLEAR_PEF | SMARTCARD_CLEAR_FEF | SMARTCARD_CLEAR_RTOF | |
||
| 1261 | SMARTCARD_CLEAR_EOBF); |
||
| 1262 | |||
| 1263 | /* Restore hsmartcard->gState and hsmartcard->RxState to Ready */ |
||
| 1264 | hsmartcard->gState = HAL_SMARTCARD_STATE_READY; |
||
| 1265 | hsmartcard->RxState = HAL_SMARTCARD_STATE_READY; |
||
| 1266 | |||
| 1267 | /* Reset Handle ErrorCode to No Error */ |
||
| 1268 | hsmartcard->ErrorCode = HAL_SMARTCARD_ERROR_NONE; |
||
| 1269 | |||
| 1270 | return HAL_OK; |
||
| 1271 | } |
||
| 1272 | |||
| 1273 | /** |
||
| 1274 | * @brief Abort ongoing Transmit transfer (blocking mode). |
||
| 1275 | * @param hsmartcard Pointer to a SMARTCARD_HandleTypeDef structure that contains |
||
| 1276 | * the configuration information for the specified SMARTCARD module. |
||
| 1277 | * @note This procedure could be used for aborting any ongoing Tx transfer started in Interrupt or DMA mode. |
||
| 1278 | * This procedure performs following operations : |
||
| 1279 | * - Disable SMARTCARD Interrupts (Tx) |
||
| 1280 | * - Disable the DMA transfer in the peripheral register (if enabled) |
||
| 1281 | * - Abort DMA transfer by calling HAL_DMA_Abort (in case of transfer in DMA mode) |
||
| 1282 | * - Set handle State to READY |
||
| 1283 | * @note This procedure is executed in blocking mode : when exiting function, Abort is considered as completed. |
||
| 1284 | * @retval HAL status |
||
| 1285 | */ |
||
| 1286 | HAL_StatusTypeDef HAL_SMARTCARD_AbortTransmit(SMARTCARD_HandleTypeDef *hsmartcard) |
||
| 1287 | { |
||
| 1288 | /* Disable TXEIE and TCIE interrupts */ |
||
| 1289 | CLEAR_BIT(hsmartcard->Instance->CR1, (USART_CR1_TXEIE | USART_CR1_TCIE)); |
||
| 1290 | |||
| 1291 | /* Check if a receive process is ongoing or not. If not disable ERR IT */ |
||
| 1292 | if (hsmartcard->RxState == HAL_SMARTCARD_STATE_READY) |
||
| 1293 | { |
||
| 1294 | /* Disable the SMARTCARD Error Interrupt: (Frame error) */ |
||
| 1295 | CLEAR_BIT(hsmartcard->Instance->CR3, USART_CR3_EIE); |
||
| 1296 | } |
||
| 1297 | |||
| 1298 | /* Disable the SMARTCARD DMA Tx request if enabled */ |
||
| 1299 | if (HAL_IS_BIT_SET(hsmartcard->Instance->CR3, USART_CR3_DMAT)) |
||
| 1300 | { |
||
| 1301 | CLEAR_BIT(hsmartcard->Instance->CR3, USART_CR3_DMAT); |
||
| 1302 | |||
| 1303 | /* Abort the SMARTCARD DMA Tx channel : use blocking DMA Abort API (no callback) */ |
||
| 1304 | if (hsmartcard->hdmatx != NULL) |
||
| 1305 | { |
||
| 1306 | /* Set the SMARTCARD DMA Abort callback to Null. |
||
| 1307 | No call back execution at end of DMA abort procedure */ |
||
| 1308 | hsmartcard->hdmatx->XferAbortCallback = NULL; |
||
| 1309 | |||
| 1310 | if (HAL_DMA_Abort(hsmartcard->hdmatx) != HAL_OK) |
||
| 1311 | { |
||
| 1312 | if (HAL_DMA_GetError(hsmartcard->hdmatx) == HAL_DMA_ERROR_TIMEOUT) |
||
| 1313 | { |
||
| 1314 | /* Set error code to DMA */ |
||
| 1315 | hsmartcard->ErrorCode = HAL_SMARTCARD_ERROR_DMA; |
||
| 1316 | |||
| 1317 | return HAL_TIMEOUT; |
||
| 1318 | } |
||
| 1319 | } |
||
| 1320 | } |
||
| 1321 | } |
||
| 1322 | |||
| 1323 | /* Reset Tx transfer counter */ |
||
| 1324 | hsmartcard->TxXferCount = 0U; |
||
| 1325 | |||
| 1326 | /* Clear the Error flags in the ICR register */ |
||
| 1327 | __HAL_SMARTCARD_CLEAR_FLAG(hsmartcard, SMARTCARD_CLEAR_FEF); |
||
| 1328 | |||
| 1329 | /* Restore hsmartcard->gState to Ready */ |
||
| 1330 | hsmartcard->gState = HAL_SMARTCARD_STATE_READY; |
||
| 1331 | |||
| 1332 | return HAL_OK; |
||
| 1333 | } |
||
| 1334 | |||
| 1335 | /** |
||
| 1336 | * @brief Abort ongoing Receive transfer (blocking mode). |
||
| 1337 | * @param hsmartcard Pointer to a SMARTCARD_HandleTypeDef structure that contains |
||
| 1338 | * the configuration information for the specified SMARTCARD module. |
||
| 1339 | * @note This procedure could be used for aborting any ongoing Rx transfer started in Interrupt or DMA mode. |
||
| 1340 | * This procedure performs following operations : |
||
| 1341 | * - Disable SMARTCARD Interrupts (Rx) |
||
| 1342 | * - Disable the DMA transfer in the peripheral register (if enabled) |
||
| 1343 | * - Abort DMA transfer by calling HAL_DMA_Abort (in case of transfer in DMA mode) |
||
| 1344 | * - Set handle State to READY |
||
| 1345 | * @note This procedure is executed in blocking mode : when exiting function, Abort is considered as completed. |
||
| 1346 | * @retval HAL status |
||
| 1347 | */ |
||
| 1348 | HAL_StatusTypeDef HAL_SMARTCARD_AbortReceive(SMARTCARD_HandleTypeDef *hsmartcard) |
||
| 1349 | { |
||
| 1350 | /* Disable RTOIE, EOBIE, RXNE, PE and ERR (Frame error, noise error, overrun error) interrupts */ |
||
| 1351 | CLEAR_BIT(hsmartcard->Instance->CR1, (USART_CR1_RXNEIE | USART_CR1_PEIE | USART_CR1_RTOIE | USART_CR1_EOBIE)); |
||
| 1352 | CLEAR_BIT(hsmartcard->Instance->CR3, USART_CR3_EIE); |
||
| 1353 | |||
| 1354 | /* Check if a Transmit process is ongoing or not. If not disable ERR IT */ |
||
| 1355 | if (hsmartcard->gState == HAL_SMARTCARD_STATE_READY) |
||
| 1356 | { |
||
| 1357 | /* Disable the SMARTCARD Error Interrupt: (Frame error) */ |
||
| 1358 | CLEAR_BIT(hsmartcard->Instance->CR3, USART_CR3_EIE); |
||
| 1359 | } |
||
| 1360 | |||
| 1361 | /* Disable the SMARTCARD DMA Rx request if enabled */ |
||
| 1362 | if (HAL_IS_BIT_SET(hsmartcard->Instance->CR3, USART_CR3_DMAR)) |
||
| 1363 | { |
||
| 1364 | CLEAR_BIT(hsmartcard->Instance->CR3, USART_CR3_DMAR); |
||
| 1365 | |||
| 1366 | /* Abort the SMARTCARD DMA Rx channel : use blocking DMA Abort API (no callback) */ |
||
| 1367 | if (hsmartcard->hdmarx != NULL) |
||
| 1368 | { |
||
| 1369 | /* Set the SMARTCARD DMA Abort callback to Null. |
||
| 1370 | No call back execution at end of DMA abort procedure */ |
||
| 1371 | hsmartcard->hdmarx->XferAbortCallback = NULL; |
||
| 1372 | |||
| 1373 | if (HAL_DMA_Abort(hsmartcard->hdmarx) != HAL_OK) |
||
| 1374 | { |
||
| 1375 | if (HAL_DMA_GetError(hsmartcard->hdmarx) == HAL_DMA_ERROR_TIMEOUT) |
||
| 1376 | { |
||
| 1377 | /* Set error code to DMA */ |
||
| 1378 | hsmartcard->ErrorCode = HAL_SMARTCARD_ERROR_DMA; |
||
| 1379 | |||
| 1380 | return HAL_TIMEOUT; |
||
| 1381 | } |
||
| 1382 | } |
||
| 1383 | } |
||
| 1384 | } |
||
| 1385 | |||
| 1386 | /* Reset Rx transfer counter */ |
||
| 1387 | hsmartcard->RxXferCount = 0U; |
||
| 1388 | |||
| 1389 | /* Clear the Error flags in the ICR register */ |
||
| 1390 | __HAL_SMARTCARD_CLEAR_FLAG(hsmartcard, |
||
| 1391 | SMARTCARD_CLEAR_OREF | SMARTCARD_CLEAR_NEF | SMARTCARD_CLEAR_PEF | SMARTCARD_CLEAR_FEF | SMARTCARD_CLEAR_RTOF | |
||
| 1392 | SMARTCARD_CLEAR_EOBF); |
||
| 1393 | |||
| 1394 | /* Restore hsmartcard->RxState to Ready */ |
||
| 1395 | hsmartcard->RxState = HAL_SMARTCARD_STATE_READY; |
||
| 1396 | |||
| 1397 | return HAL_OK; |
||
| 1398 | } |
||
| 1399 | |||
| 1400 | /** |
||
| 1401 | * @brief Abort ongoing transfers (Interrupt mode). |
||
| 1402 | * @param hsmartcard Pointer to a SMARTCARD_HandleTypeDef structure that contains |
||
| 1403 | * the configuration information for the specified SMARTCARD module. |
||
| 1404 | * @note This procedure could be used for aborting any ongoing transfer started in Interrupt or DMA mode. |
||
| 1405 | * This procedure performs following operations : |
||
| 1406 | * - Disable SMARTCARD Interrupts (Tx and Rx) |
||
| 1407 | * - Disable the DMA transfer in the peripheral register (if enabled) |
||
| 1408 | * - Abort DMA transfer by calling HAL_DMA_Abort_IT (in case of transfer in DMA mode) |
||
| 1409 | * - Set handle State to READY |
||
| 1410 | * - At abort completion, call user abort complete callback |
||
| 1411 | * @note This procedure is executed in Interrupt mode, meaning that abort procedure could be |
||
| 1412 | * considered as completed only when user abort complete callback is executed (not when exiting function). |
||
| 1413 | * @retval HAL status |
||
| 1414 | */ |
||
| 1415 | HAL_StatusTypeDef HAL_SMARTCARD_Abort_IT(SMARTCARD_HandleTypeDef *hsmartcard) |
||
| 1416 | { |
||
| 1417 | uint32_t abortcplt = 1U; |
||
| 1418 | |||
| 1419 | /* Disable RTOIE, EOBIE, TXEIE, TCIE, RXNE, PE and ERR (Frame error, noise error, overrun error) interrupts */ |
||
| 1420 | CLEAR_BIT(hsmartcard->Instance->CR1, |
||
| 1421 | (USART_CR1_RXNEIE | USART_CR1_PEIE | USART_CR1_TXEIE | USART_CR1_TCIE | USART_CR1_RTOIE | USART_CR1_EOBIE)); |
||
| 1422 | CLEAR_BIT(hsmartcard->Instance->CR3, USART_CR3_EIE); |
||
| 1423 | |||
| 1424 | /* If DMA Tx and/or DMA Rx Handles are associated to SMARTCARD Handle, DMA Abort complete callbacks should be initialised |
||
| 1425 | before any call to DMA Abort functions */ |
||
| 1426 | /* DMA Tx Handle is valid */ |
||
| 1427 | if (hsmartcard->hdmatx != NULL) |
||
| 1428 | { |
||
| 1429 | /* Set DMA Abort Complete callback if SMARTCARD DMA Tx request if enabled. |
||
| 1430 | Otherwise, set it to NULL */ |
||
| 1431 | if (HAL_IS_BIT_SET(hsmartcard->Instance->CR3, USART_CR3_DMAT)) |
||
| 1432 | { |
||
| 1433 | hsmartcard->hdmatx->XferAbortCallback = SMARTCARD_DMATxAbortCallback; |
||
| 1434 | } |
||
| 1435 | else |
||
| 1436 | { |
||
| 1437 | hsmartcard->hdmatx->XferAbortCallback = NULL; |
||
| 1438 | } |
||
| 1439 | } |
||
| 1440 | /* DMA Rx Handle is valid */ |
||
| 1441 | if (hsmartcard->hdmarx != NULL) |
||
| 1442 | { |
||
| 1443 | /* Set DMA Abort Complete callback if SMARTCARD DMA Rx request if enabled. |
||
| 1444 | Otherwise, set it to NULL */ |
||
| 1445 | if (HAL_IS_BIT_SET(hsmartcard->Instance->CR3, USART_CR3_DMAR)) |
||
| 1446 | { |
||
| 1447 | hsmartcard->hdmarx->XferAbortCallback = SMARTCARD_DMARxAbortCallback; |
||
| 1448 | } |
||
| 1449 | else |
||
| 1450 | { |
||
| 1451 | hsmartcard->hdmarx->XferAbortCallback = NULL; |
||
| 1452 | } |
||
| 1453 | } |
||
| 1454 | |||
| 1455 | /* Disable the SMARTCARD DMA Tx request if enabled */ |
||
| 1456 | if (HAL_IS_BIT_SET(hsmartcard->Instance->CR3, USART_CR3_DMAT)) |
||
| 1457 | { |
||
| 1458 | /* Disable DMA Tx at UART level */ |
||
| 1459 | CLEAR_BIT(hsmartcard->Instance->CR3, USART_CR3_DMAT); |
||
| 1460 | |||
| 1461 | /* Abort the SMARTCARD DMA Tx channel : use non blocking DMA Abort API (callback) */ |
||
| 1462 | if (hsmartcard->hdmatx != NULL) |
||
| 1463 | { |
||
| 1464 | /* SMARTCARD Tx DMA Abort callback has already been initialised : |
||
| 1465 | will lead to call HAL_SMARTCARD_AbortCpltCallback() at end of DMA abort procedure */ |
||
| 1466 | |||
| 1467 | /* Abort DMA TX */ |
||
| 1468 | if (HAL_DMA_Abort_IT(hsmartcard->hdmatx) != HAL_OK) |
||
| 1469 | { |
||
| 1470 | hsmartcard->hdmatx->XferAbortCallback = NULL; |
||
| 1471 | } |
||
| 1472 | else |
||
| 1473 | { |
||
| 1474 | abortcplt = 0U; |
||
| 1475 | } |
||
| 1476 | } |
||
| 1477 | } |
||
| 1478 | |||
| 1479 | /* Disable the SMARTCARD DMA Rx request if enabled */ |
||
| 1480 | if (HAL_IS_BIT_SET(hsmartcard->Instance->CR3, USART_CR3_DMAR)) |
||
| 1481 | { |
||
| 1482 | CLEAR_BIT(hsmartcard->Instance->CR3, USART_CR3_DMAR); |
||
| 1483 | |||
| 1484 | /* Abort the SMARTCARD DMA Rx channel : use non blocking DMA Abort API (callback) */ |
||
| 1485 | if (hsmartcard->hdmarx != NULL) |
||
| 1486 | { |
||
| 1487 | /* SMARTCARD Rx DMA Abort callback has already been initialised : |
||
| 1488 | will lead to call HAL_SMARTCARD_AbortCpltCallback() at end of DMA abort procedure */ |
||
| 1489 | |||
| 1490 | /* Abort DMA RX */ |
||
| 1491 | if (HAL_DMA_Abort_IT(hsmartcard->hdmarx) != HAL_OK) |
||
| 1492 | { |
||
| 1493 | hsmartcard->hdmarx->XferAbortCallback = NULL; |
||
| 1494 | abortcplt = 1U; |
||
| 1495 | } |
||
| 1496 | else |
||
| 1497 | { |
||
| 1498 | abortcplt = 0U; |
||
| 1499 | } |
||
| 1500 | } |
||
| 1501 | } |
||
| 1502 | |||
| 1503 | /* if no DMA abort complete callback execution is required => call user Abort Complete callback */ |
||
| 1504 | if (abortcplt == 1U) |
||
| 1505 | { |
||
| 1506 | /* Reset Tx and Rx transfer counters */ |
||
| 1507 | hsmartcard->TxXferCount = 0U; |
||
| 1508 | hsmartcard->RxXferCount = 0U; |
||
| 1509 | |||
| 1510 | /* Clear ISR function pointers */ |
||
| 1511 | hsmartcard->RxISR = NULL; |
||
| 1512 | hsmartcard->TxISR = NULL; |
||
| 1513 | |||
| 1514 | /* Reset errorCode */ |
||
| 1515 | hsmartcard->ErrorCode = HAL_SMARTCARD_ERROR_NONE; |
||
| 1516 | |||
| 1517 | /* Clear the Error flags in the ICR register */ |
||
| 1518 | __HAL_SMARTCARD_CLEAR_FLAG(hsmartcard, |
||
| 1519 | SMARTCARD_CLEAR_OREF | SMARTCARD_CLEAR_NEF | SMARTCARD_CLEAR_PEF | SMARTCARD_CLEAR_FEF | SMARTCARD_CLEAR_RTOF | |
||
| 1520 | SMARTCARD_CLEAR_EOBF); |
||
| 1521 | |||
| 1522 | /* Restore hsmartcard->gState and hsmartcard->RxState to Ready */ |
||
| 1523 | hsmartcard->gState = HAL_SMARTCARD_STATE_READY; |
||
| 1524 | hsmartcard->RxState = HAL_SMARTCARD_STATE_READY; |
||
| 1525 | |||
| 1526 | /* As no DMA to be aborted, call directly user Abort complete callback */ |
||
| 1527 | #if (USE_HAL_SMARTCARD_REGISTER_CALLBACKS == 1) |
||
| 1528 | /* Call registered Abort complete callback */ |
||
| 1529 | hsmartcard->AbortCpltCallback(hsmartcard); |
||
| 1530 | #else |
||
| 1531 | /* Call legacy weak Abort complete callback */ |
||
| 1532 | HAL_SMARTCARD_AbortCpltCallback(hsmartcard); |
||
| 1533 | #endif /* USE_HAL_SMARTCARD_REGISTER_CALLBACK */ |
||
| 1534 | } |
||
| 1535 | |||
| 1536 | return HAL_OK; |
||
| 1537 | } |
||
| 1538 | |||
| 1539 | /** |
||
| 1540 | * @brief Abort ongoing Transmit transfer (Interrupt mode). |
||
| 1541 | * @param hsmartcard Pointer to a SMARTCARD_HandleTypeDef structure that contains |
||
| 1542 | * the configuration information for the specified SMARTCARD module. |
||
| 1543 | * @note This procedure could be used for aborting any ongoing Tx transfer started in Interrupt or DMA mode. |
||
| 1544 | * This procedure performs following operations : |
||
| 1545 | * - Disable SMARTCARD Interrupts (Tx) |
||
| 1546 | * - Disable the DMA transfer in the peripheral register (if enabled) |
||
| 1547 | * - Abort DMA transfer by calling HAL_DMA_Abort_IT (in case of transfer in DMA mode) |
||
| 1548 | * - Set handle State to READY |
||
| 1549 | * - At abort completion, call user abort complete callback |
||
| 1550 | * @note This procedure is executed in Interrupt mode, meaning that abort procedure could be |
||
| 1551 | * considered as completed only when user abort complete callback is executed (not when exiting function). |
||
| 1552 | * @retval HAL status |
||
| 1553 | */ |
||
| 1554 | HAL_StatusTypeDef HAL_SMARTCARD_AbortTransmit_IT(SMARTCARD_HandleTypeDef *hsmartcard) |
||
| 1555 | { |
||
| 1556 | /* Disable TXEIE and TCIE interrupts */ |
||
| 1557 | CLEAR_BIT(hsmartcard->Instance->CR1, (USART_CR1_TXEIE | USART_CR1_TCIE)); |
||
| 1558 | |||
| 1559 | /* Check if a receive process is ongoing or not. If not disable ERR IT */ |
||
| 1560 | if (hsmartcard->RxState == HAL_SMARTCARD_STATE_READY) |
||
| 1561 | { |
||
| 1562 | /* Disable the SMARTCARD Error Interrupt: (Frame error) */ |
||
| 1563 | CLEAR_BIT(hsmartcard->Instance->CR3, USART_CR3_EIE); |
||
| 1564 | } |
||
| 1565 | |||
| 1566 | /* Disable the SMARTCARD DMA Tx request if enabled */ |
||
| 1567 | if (HAL_IS_BIT_SET(hsmartcard->Instance->CR3, USART_CR3_DMAT)) |
||
| 1568 | { |
||
| 1569 | CLEAR_BIT(hsmartcard->Instance->CR3, USART_CR3_DMAT); |
||
| 1570 | |||
| 1571 | /* Abort the SMARTCARD DMA Tx channel : use non blocking DMA Abort API (callback) */ |
||
| 1572 | if (hsmartcard->hdmatx != NULL) |
||
| 1573 | { |
||
| 1574 | /* Set the SMARTCARD DMA Abort callback : |
||
| 1575 | will lead to call HAL_SMARTCARD_AbortCpltCallback() at end of DMA abort procedure */ |
||
| 1576 | hsmartcard->hdmatx->XferAbortCallback = SMARTCARD_DMATxOnlyAbortCallback; |
||
| 1577 | |||
| 1578 | /* Abort DMA TX */ |
||
| 1579 | if (HAL_DMA_Abort_IT(hsmartcard->hdmatx) != HAL_OK) |
||
| 1580 | { |
||
| 1581 | /* Call Directly hsmartcard->hdmatx->XferAbortCallback function in case of error */ |
||
| 1582 | hsmartcard->hdmatx->XferAbortCallback(hsmartcard->hdmatx); |
||
| 1583 | } |
||
| 1584 | } |
||
| 1585 | else |
||
| 1586 | { |
||
| 1587 | /* Reset Tx transfer counter */ |
||
| 1588 | hsmartcard->TxXferCount = 0U; |
||
| 1589 | |||
| 1590 | /* Clear TxISR function pointers */ |
||
| 1591 | hsmartcard->TxISR = NULL; |
||
| 1592 | |||
| 1593 | /* Restore hsmartcard->gState to Ready */ |
||
| 1594 | hsmartcard->gState = HAL_SMARTCARD_STATE_READY; |
||
| 1595 | |||
| 1596 | /* As no DMA to be aborted, call directly user Abort complete callback */ |
||
| 1597 | #if (USE_HAL_SMARTCARD_REGISTER_CALLBACKS == 1) |
||
| 1598 | /* Call registered Abort Transmit Complete Callback */ |
||
| 1599 | hsmartcard->AbortTransmitCpltCallback(hsmartcard); |
||
| 1600 | #else |
||
| 1601 | /* Call legacy weak Abort Transmit Complete Callback */ |
||
| 1602 | HAL_SMARTCARD_AbortTransmitCpltCallback(hsmartcard); |
||
| 1603 | #endif /* USE_HAL_SMARTCARD_REGISTER_CALLBACK */ |
||
| 1604 | } |
||
| 1605 | } |
||
| 1606 | else |
||
| 1607 | { |
||
| 1608 | /* Reset Tx transfer counter */ |
||
| 1609 | hsmartcard->TxXferCount = 0U; |
||
| 1610 | |||
| 1611 | /* Clear TxISR function pointers */ |
||
| 1612 | hsmartcard->TxISR = NULL; |
||
| 1613 | |||
| 1614 | /* Clear the Error flags in the ICR register */ |
||
| 1615 | __HAL_SMARTCARD_CLEAR_FLAG(hsmartcard, SMARTCARD_CLEAR_FEF); |
||
| 1616 | |||
| 1617 | /* Restore hsmartcard->gState to Ready */ |
||
| 1618 | hsmartcard->gState = HAL_SMARTCARD_STATE_READY; |
||
| 1619 | |||
| 1620 | /* As no DMA to be aborted, call directly user Abort complete callback */ |
||
| 1621 | #if (USE_HAL_SMARTCARD_REGISTER_CALLBACKS == 1) |
||
| 1622 | /* Call registered Abort Transmit Complete Callback */ |
||
| 1623 | hsmartcard->AbortTransmitCpltCallback(hsmartcard); |
||
| 1624 | #else |
||
| 1625 | /* Call legacy weak Abort Transmit Complete Callback */ |
||
| 1626 | HAL_SMARTCARD_AbortTransmitCpltCallback(hsmartcard); |
||
| 1627 | #endif /* USE_HAL_SMARTCARD_REGISTER_CALLBACK */ |
||
| 1628 | } |
||
| 1629 | |||
| 1630 | return HAL_OK; |
||
| 1631 | } |
||
| 1632 | |||
| 1633 | /** |
||
| 1634 | * @brief Abort ongoing Receive transfer (Interrupt mode). |
||
| 1635 | * @param hsmartcard Pointer to a SMARTCARD_HandleTypeDef structure that contains |
||
| 1636 | * the configuration information for the specified SMARTCARD module. |
||
| 1637 | * @note This procedure could be used for aborting any ongoing Rx transfer started in Interrupt or DMA mode. |
||
| 1638 | * This procedure performs following operations : |
||
| 1639 | * - Disable SMARTCARD Interrupts (Rx) |
||
| 1640 | * - Disable the DMA transfer in the peripheral register (if enabled) |
||
| 1641 | * - Abort DMA transfer by calling HAL_DMA_Abort_IT (in case of transfer in DMA mode) |
||
| 1642 | * - Set handle State to READY |
||
| 1643 | * - At abort completion, call user abort complete callback |
||
| 1644 | * @note This procedure is executed in Interrupt mode, meaning that abort procedure could be |
||
| 1645 | * considered as completed only when user abort complete callback is executed (not when exiting function). |
||
| 1646 | * @retval HAL status |
||
| 1647 | */ |
||
| 1648 | HAL_StatusTypeDef HAL_SMARTCARD_AbortReceive_IT(SMARTCARD_HandleTypeDef *hsmartcard) |
||
| 1649 | { |
||
| 1650 | /* Disable RTOIE, EOBIE, RXNE, PE and ERR (Frame error, noise error, overrun error) interrupts */ |
||
| 1651 | CLEAR_BIT(hsmartcard->Instance->CR1, (USART_CR1_RXNEIE | USART_CR1_PEIE | USART_CR1_RTOIE | USART_CR1_EOBIE)); |
||
| 1652 | CLEAR_BIT(hsmartcard->Instance->CR3, USART_CR3_EIE); |
||
| 1653 | |||
| 1654 | /* Check if a Transmit process is ongoing or not. If not disable ERR IT */ |
||
| 1655 | if (hsmartcard->gState == HAL_SMARTCARD_STATE_READY) |
||
| 1656 | { |
||
| 1657 | /* Disable the SMARTCARD Error Interrupt: (Frame error) */ |
||
| 1658 | CLEAR_BIT(hsmartcard->Instance->CR3, USART_CR3_EIE); |
||
| 1659 | } |
||
| 1660 | |||
| 1661 | /* Disable the SMARTCARD DMA Rx request if enabled */ |
||
| 1662 | if (HAL_IS_BIT_SET(hsmartcard->Instance->CR3, USART_CR3_DMAR)) |
||
| 1663 | { |
||
| 1664 | CLEAR_BIT(hsmartcard->Instance->CR3, USART_CR3_DMAR); |
||
| 1665 | |||
| 1666 | /* Abort the SMARTCARD DMA Rx channel : use non blocking DMA Abort API (callback) */ |
||
| 1667 | if (hsmartcard->hdmarx != NULL) |
||
| 1668 | { |
||
| 1669 | /* Set the SMARTCARD DMA Abort callback : |
||
| 1670 | will lead to call HAL_SMARTCARD_AbortCpltCallback() at end of DMA abort procedure */ |
||
| 1671 | hsmartcard->hdmarx->XferAbortCallback = SMARTCARD_DMARxOnlyAbortCallback; |
||
| 1672 | |||
| 1673 | /* Abort DMA RX */ |
||
| 1674 | if (HAL_DMA_Abort_IT(hsmartcard->hdmarx) != HAL_OK) |
||
| 1675 | { |
||
| 1676 | /* Call Directly hsmartcard->hdmarx->XferAbortCallback function in case of error */ |
||
| 1677 | hsmartcard->hdmarx->XferAbortCallback(hsmartcard->hdmarx); |
||
| 1678 | } |
||
| 1679 | } |
||
| 1680 | else |
||
| 1681 | { |
||
| 1682 | /* Reset Rx transfer counter */ |
||
| 1683 | hsmartcard->RxXferCount = 0U; |
||
| 1684 | |||
| 1685 | /* Clear RxISR function pointer */ |
||
| 1686 | hsmartcard->RxISR = NULL; |
||
| 1687 | |||
| 1688 | /* Clear the Error flags in the ICR register */ |
||
| 1689 | __HAL_SMARTCARD_CLEAR_FLAG(hsmartcard, |
||
| 1690 | SMARTCARD_CLEAR_OREF | SMARTCARD_CLEAR_NEF | SMARTCARD_CLEAR_PEF | SMARTCARD_CLEAR_FEF | SMARTCARD_CLEAR_RTOF | |
||
| 1691 | SMARTCARD_CLEAR_EOBF); |
||
| 1692 | |||
| 1693 | /* Restore hsmartcard->RxState to Ready */ |
||
| 1694 | hsmartcard->RxState = HAL_SMARTCARD_STATE_READY; |
||
| 1695 | |||
| 1696 | /* As no DMA to be aborted, call directly user Abort complete callback */ |
||
| 1697 | #if (USE_HAL_SMARTCARD_REGISTER_CALLBACKS == 1) |
||
| 1698 | /* Call registered Abort Receive Complete Callback */ |
||
| 1699 | hsmartcard->AbortReceiveCpltCallback(hsmartcard); |
||
| 1700 | #else |
||
| 1701 | /* Call legacy weak Abort Receive Complete Callback */ |
||
| 1702 | HAL_SMARTCARD_AbortReceiveCpltCallback(hsmartcard); |
||
| 1703 | #endif /* USE_HAL_SMARTCARD_REGISTER_CALLBACK */ |
||
| 1704 | } |
||
| 1705 | } |
||
| 1706 | else |
||
| 1707 | { |
||
| 1708 | /* Reset Rx transfer counter */ |
||
| 1709 | hsmartcard->RxXferCount = 0U; |
||
| 1710 | |||
| 1711 | /* Clear RxISR function pointer */ |
||
| 1712 | hsmartcard->RxISR = NULL; |
||
| 1713 | |||
| 1714 | /* Clear the Error flags in the ICR register */ |
||
| 1715 | __HAL_SMARTCARD_CLEAR_FLAG(hsmartcard, |
||
| 1716 | SMARTCARD_CLEAR_OREF | SMARTCARD_CLEAR_NEF | SMARTCARD_CLEAR_PEF | SMARTCARD_CLEAR_FEF | SMARTCARD_CLEAR_RTOF | |
||
| 1717 | SMARTCARD_CLEAR_EOBF); |
||
| 1718 | |||
| 1719 | /* Restore hsmartcard->RxState to Ready */ |
||
| 1720 | hsmartcard->RxState = HAL_SMARTCARD_STATE_READY; |
||
| 1721 | |||
| 1722 | /* As no DMA to be aborted, call directly user Abort complete callback */ |
||
| 1723 | #if (USE_HAL_SMARTCARD_REGISTER_CALLBACKS == 1) |
||
| 1724 | /* Call registered Abort Receive Complete Callback */ |
||
| 1725 | hsmartcard->AbortReceiveCpltCallback(hsmartcard); |
||
| 1726 | #else |
||
| 1727 | /* Call legacy weak Abort Receive Complete Callback */ |
||
| 1728 | HAL_SMARTCARD_AbortReceiveCpltCallback(hsmartcard); |
||
| 1729 | #endif /* USE_HAL_SMARTCARD_REGISTER_CALLBACK */ |
||
| 1730 | } |
||
| 1731 | |||
| 1732 | return HAL_OK; |
||
| 1733 | } |
||
| 1734 | |||
| 1735 | /** |
||
| 1736 | * @brief Handle SMARTCARD interrupt requests. |
||
| 1737 | * @param hsmartcard Pointer to a SMARTCARD_HandleTypeDef structure that contains |
||
| 1738 | * the configuration information for the specified SMARTCARD module. |
||
| 1739 | * @retval None |
||
| 1740 | */ |
||
| 1741 | void HAL_SMARTCARD_IRQHandler(SMARTCARD_HandleTypeDef *hsmartcard) |
||
| 1742 | { |
||
| 1743 | uint32_t isrflags = READ_REG(hsmartcard->Instance->ISR); |
||
| 1744 | uint32_t cr1its = READ_REG(hsmartcard->Instance->CR1); |
||
| 1745 | uint32_t cr3its = READ_REG(hsmartcard->Instance->CR3); |
||
| 1746 | uint32_t errorflags; |
||
| 1747 | uint32_t errorcode; |
||
| 1748 | |||
| 1749 | /* If no error occurs */ |
||
| 1750 | errorflags = (isrflags & (uint32_t)(USART_ISR_PE | USART_ISR_FE | USART_ISR_ORE | USART_ISR_NE | USART_ISR_RTOF)); |
||
| 1751 | if (errorflags == 0U) |
||
| 1752 | { |
||
| 1753 | /* SMARTCARD in mode Receiver ---------------------------------------------------*/ |
||
| 1754 | if (((isrflags & USART_ISR_RXNE) != 0U) |
||
| 1755 | && ((cr1its & USART_CR1_RXNEIE) != 0U)) |
||
| 1756 | { |
||
| 1757 | if (hsmartcard->RxISR != NULL) |
||
| 1758 | { |
||
| 1759 | hsmartcard->RxISR(hsmartcard); |
||
| 1760 | } |
||
| 1761 | return; |
||
| 1762 | } |
||
| 1763 | } |
||
| 1764 | |||
| 1765 | /* If some errors occur */ |
||
| 1766 | if ((errorflags != 0U) |
||
| 1767 | && (((cr3its & USART_CR3_EIE) != 0U) |
||
| 1768 | || ((cr1its & (USART_CR1_RXNEIE | USART_CR1_PEIE)) != 0U))) |
||
| 1769 | { |
||
| 1770 | /* SMARTCARD parity error interrupt occurred -------------------------------------*/ |
||
| 1771 | if (((isrflags & USART_ISR_PE) != 0U) && ((cr1its & USART_CR1_PEIE) != 0U)) |
||
| 1772 | { |
||
| 1773 | __HAL_SMARTCARD_CLEAR_IT(hsmartcard, SMARTCARD_CLEAR_PEF); |
||
| 1774 | |||
| 1775 | hsmartcard->ErrorCode |= HAL_SMARTCARD_ERROR_PE; |
||
| 1776 | } |
||
| 1777 | |||
| 1778 | /* SMARTCARD frame error interrupt occurred --------------------------------------*/ |
||
| 1779 | if (((isrflags & USART_ISR_FE) != 0U) && ((cr3its & USART_CR3_EIE) != 0U)) |
||
| 1780 | { |
||
| 1781 | __HAL_SMARTCARD_CLEAR_IT(hsmartcard, SMARTCARD_CLEAR_FEF); |
||
| 1782 | |||
| 1783 | hsmartcard->ErrorCode |= HAL_SMARTCARD_ERROR_FE; |
||
| 1784 | } |
||
| 1785 | |||
| 1786 | /* SMARTCARD noise error interrupt occurred --------------------------------------*/ |
||
| 1787 | if (((isrflags & USART_ISR_NE) != 0U) && ((cr3its & USART_CR3_EIE) != 0U)) |
||
| 1788 | { |
||
| 1789 | __HAL_SMARTCARD_CLEAR_IT(hsmartcard, SMARTCARD_CLEAR_NEF); |
||
| 1790 | |||
| 1791 | hsmartcard->ErrorCode |= HAL_SMARTCARD_ERROR_NE; |
||
| 1792 | } |
||
| 1793 | |||
| 1794 | /* SMARTCARD Over-Run interrupt occurred -----------------------------------------*/ |
||
| 1795 | if (((isrflags & USART_ISR_ORE) != 0U) |
||
| 1796 | && (((cr1its & USART_CR1_RXNEIE) != 0U) |
||
| 1797 | || ((cr3its & USART_CR3_EIE) != 0U))) |
||
| 1798 | { |
||
| 1799 | __HAL_SMARTCARD_CLEAR_IT(hsmartcard, SMARTCARD_CLEAR_OREF); |
||
| 1800 | |||
| 1801 | hsmartcard->ErrorCode |= HAL_SMARTCARD_ERROR_ORE; |
||
| 1802 | } |
||
| 1803 | |||
| 1804 | /* SMARTCARD receiver timeout interrupt occurred -----------------------------------------*/ |
||
| 1805 | if (((isrflags & USART_ISR_RTOF) != 0U) && ((cr1its & USART_CR1_RTOIE) != 0U)) |
||
| 1806 | { |
||
| 1807 | __HAL_SMARTCARD_CLEAR_IT(hsmartcard, SMARTCARD_CLEAR_RTOF); |
||
| 1808 | |||
| 1809 | hsmartcard->ErrorCode |= HAL_SMARTCARD_ERROR_RTO; |
||
| 1810 | } |
||
| 1811 | |||
| 1812 | /* Call SMARTCARD Error Call back function if need be --------------------------*/ |
||
| 1813 | if (hsmartcard->ErrorCode != HAL_SMARTCARD_ERROR_NONE) |
||
| 1814 | { |
||
| 1815 | /* SMARTCARD in mode Receiver ---------------------------------------------------*/ |
||
| 1816 | if (((isrflags & USART_ISR_RXNE) != 0U) |
||
| 1817 | && ((cr1its & USART_CR1_RXNEIE) != 0U)) |
||
| 1818 | { |
||
| 1819 | if (hsmartcard->RxISR != NULL) |
||
| 1820 | { |
||
| 1821 | hsmartcard->RxISR(hsmartcard); |
||
| 1822 | } |
||
| 1823 | } |
||
| 1824 | |||
| 1825 | /* If Error is to be considered as blocking : |
||
| 1826 | - Receiver Timeout error in Reception |
||
| 1827 | - Overrun error in Reception |
||
| 1828 | - any error occurs in DMA mode reception |
||
| 1829 | */ |
||
| 1830 | errorcode = hsmartcard->ErrorCode; |
||
| 1831 | if ((HAL_IS_BIT_SET(hsmartcard->Instance->CR3, USART_CR3_DMAR)) |
||
| 1832 | || ((errorcode & (HAL_SMARTCARD_ERROR_RTO | HAL_SMARTCARD_ERROR_ORE)) != 0U)) |
||
| 1833 | { |
||
| 1834 | /* Blocking error : transfer is aborted |
||
| 1835 | Set the SMARTCARD state ready to be able to start again the process, |
||
| 1836 | Disable Rx Interrupts, and disable Rx DMA request, if ongoing */ |
||
| 1837 | SMARTCARD_EndRxTransfer(hsmartcard); |
||
| 1838 | |||
| 1839 | /* Disable the SMARTCARD DMA Rx request if enabled */ |
||
| 1840 | if (HAL_IS_BIT_SET(hsmartcard->Instance->CR3, USART_CR3_DMAR)) |
||
| 1841 | { |
||
| 1842 | CLEAR_BIT(hsmartcard->Instance->CR3, USART_CR3_DMAR); |
||
| 1843 | |||
| 1844 | /* Abort the SMARTCARD DMA Rx channel */ |
||
| 1845 | if (hsmartcard->hdmarx != NULL) |
||
| 1846 | { |
||
| 1847 | /* Set the SMARTCARD DMA Abort callback : |
||
| 1848 | will lead to call HAL_SMARTCARD_ErrorCallback() at end of DMA abort procedure */ |
||
| 1849 | hsmartcard->hdmarx->XferAbortCallback = SMARTCARD_DMAAbortOnError; |
||
| 1850 | |||
| 1851 | /* Abort DMA RX */ |
||
| 1852 | if (HAL_DMA_Abort_IT(hsmartcard->hdmarx) != HAL_OK) |
||
| 1853 | { |
||
| 1854 | /* Call Directly hsmartcard->hdmarx->XferAbortCallback function in case of error */ |
||
| 1855 | hsmartcard->hdmarx->XferAbortCallback(hsmartcard->hdmarx); |
||
| 1856 | } |
||
| 1857 | } |
||
| 1858 | else |
||
| 1859 | { |
||
| 1860 | #if (USE_HAL_SMARTCARD_REGISTER_CALLBACKS == 1) |
||
| 1861 | /* Call registered user error callback */ |
||
| 1862 | hsmartcard->ErrorCallback(hsmartcard); |
||
| 1863 | #else |
||
| 1864 | /* Call legacy weak user error callback */ |
||
| 1865 | HAL_SMARTCARD_ErrorCallback(hsmartcard); |
||
| 1866 | #endif /* USE_HAL_SMARTCARD_REGISTER_CALLBACK */ |
||
| 1867 | } |
||
| 1868 | } |
||
| 1869 | else |
||
| 1870 | { |
||
| 1871 | #if (USE_HAL_SMARTCARD_REGISTER_CALLBACKS == 1) |
||
| 1872 | /* Call registered user error callback */ |
||
| 1873 | hsmartcard->ErrorCallback(hsmartcard); |
||
| 1874 | #else |
||
| 1875 | /* Call legacy weak user error callback */ |
||
| 1876 | HAL_SMARTCARD_ErrorCallback(hsmartcard); |
||
| 1877 | #endif /* USE_HAL_SMARTCARD_REGISTER_CALLBACK */ |
||
| 1878 | } |
||
| 1879 | } |
||
| 1880 | /* other error type to be considered as blocking : |
||
| 1881 | - Frame error in Transmission |
||
| 1882 | */ |
||
| 1883 | else if ((hsmartcard->gState == HAL_SMARTCARD_STATE_BUSY_TX) |
||
| 1884 | && ((errorcode & HAL_SMARTCARD_ERROR_FE) != 0U)) |
||
| 1885 | { |
||
| 1886 | /* Blocking error : transfer is aborted |
||
| 1887 | Set the SMARTCARD state ready to be able to start again the process, |
||
| 1888 | Disable Tx Interrupts, and disable Tx DMA request, if ongoing */ |
||
| 1889 | SMARTCARD_EndTxTransfer(hsmartcard); |
||
| 1890 | |||
| 1891 | /* Disable the SMARTCARD DMA Tx request if enabled */ |
||
| 1892 | if (HAL_IS_BIT_SET(hsmartcard->Instance->CR3, USART_CR3_DMAT)) |
||
| 1893 | { |
||
| 1894 | CLEAR_BIT(hsmartcard->Instance->CR3, USART_CR3_DMAT); |
||
| 1895 | |||
| 1896 | /* Abort the SMARTCARD DMA Tx channel */ |
||
| 1897 | if (hsmartcard->hdmatx != NULL) |
||
| 1898 | { |
||
| 1899 | /* Set the SMARTCARD DMA Abort callback : |
||
| 1900 | will lead to call HAL_SMARTCARD_ErrorCallback() at end of DMA abort procedure */ |
||
| 1901 | hsmartcard->hdmatx->XferAbortCallback = SMARTCARD_DMAAbortOnError; |
||
| 1902 | |||
| 1903 | /* Abort DMA TX */ |
||
| 1904 | if (HAL_DMA_Abort_IT(hsmartcard->hdmatx) != HAL_OK) |
||
| 1905 | { |
||
| 1906 | /* Call Directly hsmartcard->hdmatx->XferAbortCallback function in case of error */ |
||
| 1907 | hsmartcard->hdmatx->XferAbortCallback(hsmartcard->hdmatx); |
||
| 1908 | } |
||
| 1909 | } |
||
| 1910 | else |
||
| 1911 | { |
||
| 1912 | #if (USE_HAL_SMARTCARD_REGISTER_CALLBACKS == 1) |
||
| 1913 | /* Call registered user error callback */ |
||
| 1914 | hsmartcard->ErrorCallback(hsmartcard); |
||
| 1915 | #else |
||
| 1916 | /* Call legacy weak user error callback */ |
||
| 1917 | HAL_SMARTCARD_ErrorCallback(hsmartcard); |
||
| 1918 | #endif /* USE_HAL_SMARTCARD_REGISTER_CALLBACK */ |
||
| 1919 | } |
||
| 1920 | } |
||
| 1921 | else |
||
| 1922 | { |
||
| 1923 | #if (USE_HAL_SMARTCARD_REGISTER_CALLBACKS == 1) |
||
| 1924 | /* Call registered user error callback */ |
||
| 1925 | hsmartcard->ErrorCallback(hsmartcard); |
||
| 1926 | #else |
||
| 1927 | /* Call legacy weak user error callback */ |
||
| 1928 | HAL_SMARTCARD_ErrorCallback(hsmartcard); |
||
| 1929 | #endif /* USE_HAL_SMARTCARD_REGISTER_CALLBACK */ |
||
| 1930 | } |
||
| 1931 | } |
||
| 1932 | else |
||
| 1933 | { |
||
| 1934 | /* Non Blocking error : transfer could go on. |
||
| 1935 | Error is notified to user through user error callback */ |
||
| 1936 | #if (USE_HAL_SMARTCARD_REGISTER_CALLBACKS == 1) |
||
| 1937 | /* Call registered user error callback */ |
||
| 1938 | hsmartcard->ErrorCallback(hsmartcard); |
||
| 1939 | #else |
||
| 1940 | /* Call legacy weak user error callback */ |
||
| 1941 | HAL_SMARTCARD_ErrorCallback(hsmartcard); |
||
| 1942 | #endif /* USE_HAL_SMARTCARD_REGISTER_CALLBACK */ |
||
| 1943 | hsmartcard->ErrorCode = HAL_SMARTCARD_ERROR_NONE; |
||
| 1944 | } |
||
| 1945 | } |
||
| 1946 | return; |
||
| 1947 | |||
| 1948 | } /* End if some error occurs */ |
||
| 1949 | |||
| 1950 | /* SMARTCARD in mode Receiver, end of block interruption ------------------------*/ |
||
| 1951 | if (((isrflags & USART_ISR_EOBF) != 0U) && ((cr1its & USART_CR1_EOBIE) != 0U)) |
||
| 1952 | { |
||
| 1953 | hsmartcard->RxState = HAL_SMARTCARD_STATE_READY; |
||
| 1954 | __HAL_UNLOCK(hsmartcard); |
||
| 1955 | #if (USE_HAL_SMARTCARD_REGISTER_CALLBACKS == 1) |
||
| 1956 | /* Call registered Rx complete callback */ |
||
| 1957 | hsmartcard->RxCpltCallback(hsmartcard); |
||
| 1958 | #else |
||
| 1959 | /* Call legacy weak Rx complete callback */ |
||
| 1960 | HAL_SMARTCARD_RxCpltCallback(hsmartcard); |
||
| 1961 | #endif /* USE_HAL_SMARTCARD_REGISTER_CALLBACK */ |
||
| 1962 | /* Clear EOBF interrupt after HAL_SMARTCARD_RxCpltCallback() call for the End of Block information |
||
| 1963 | to be available during HAL_SMARTCARD_RxCpltCallback() processing */ |
||
| 1964 | __HAL_SMARTCARD_CLEAR_IT(hsmartcard, SMARTCARD_CLEAR_EOBF); |
||
| 1965 | return; |
||
| 1966 | } |
||
| 1967 | |||
| 1968 | /* SMARTCARD in mode Transmitter ------------------------------------------------*/ |
||
| 1969 | if (((isrflags & USART_ISR_TXE) != 0U) |
||
| 1970 | && ((cr1its & USART_CR1_TXEIE) != 0U)) |
||
| 1971 | { |
||
| 1972 | if (hsmartcard->TxISR != NULL) |
||
| 1973 | { |
||
| 1974 | hsmartcard->TxISR(hsmartcard); |
||
| 1975 | } |
||
| 1976 | return; |
||
| 1977 | } |
||
| 1978 | |||
| 1979 | /* SMARTCARD in mode Transmitter (transmission end) ------------------------*/ |
||
| 1980 | if (__HAL_SMARTCARD_GET_IT(hsmartcard, hsmartcard->AdvancedInit.TxCompletionIndication) != RESET) |
||
| 1981 | { |
||
| 1982 | if (__HAL_SMARTCARD_GET_IT_SOURCE(hsmartcard, hsmartcard->AdvancedInit.TxCompletionIndication) != RESET) |
||
| 1983 | { |
||
| 1984 | SMARTCARD_EndTransmit_IT(hsmartcard); |
||
| 1985 | return; |
||
| 1986 | } |
||
| 1987 | } |
||
| 1988 | |||
| 1989 | } |
||
| 1990 | |||
| 1991 | /** |
||
| 1992 | * @brief Tx Transfer completed callback. |
||
| 1993 | * @param hsmartcard Pointer to a SMARTCARD_HandleTypeDef structure that contains |
||
| 1994 | * the configuration information for the specified SMARTCARD module. |
||
| 1995 | * @retval None |
||
| 1996 | */ |
||
| 1997 | __weak void HAL_SMARTCARD_TxCpltCallback(SMARTCARD_HandleTypeDef *hsmartcard) |
||
| 1998 | { |
||
| 1999 | /* Prevent unused argument(s) compilation warning */ |
||
| 2000 | UNUSED(hsmartcard); |
||
| 2001 | |||
| 2002 | /* NOTE : This function should not be modified, when the callback is needed, |
||
| 2003 | the HAL_SMARTCARD_TxCpltCallback can be implemented in the user file. |
||
| 2004 | */ |
||
| 2005 | } |
||
| 2006 | |||
| 2007 | /** |
||
| 2008 | * @brief Rx Transfer completed callback. |
||
| 2009 | * @param hsmartcard Pointer to a SMARTCARD_HandleTypeDef structure that contains |
||
| 2010 | * the configuration information for the specified SMARTCARD module. |
||
| 2011 | * @retval None |
||
| 2012 | */ |
||
| 2013 | __weak void HAL_SMARTCARD_RxCpltCallback(SMARTCARD_HandleTypeDef *hsmartcard) |
||
| 2014 | { |
||
| 2015 | /* Prevent unused argument(s) compilation warning */ |
||
| 2016 | UNUSED(hsmartcard); |
||
| 2017 | |||
| 2018 | /* NOTE : This function should not be modified, when the callback is needed, |
||
| 2019 | the HAL_SMARTCARD_RxCpltCallback can be implemented in the user file. |
||
| 2020 | */ |
||
| 2021 | } |
||
| 2022 | |||
| 2023 | /** |
||
| 2024 | * @brief SMARTCARD error callback. |
||
| 2025 | * @param hsmartcard Pointer to a SMARTCARD_HandleTypeDef structure that contains |
||
| 2026 | * the configuration information for the specified SMARTCARD module. |
||
| 2027 | * @retval None |
||
| 2028 | */ |
||
| 2029 | __weak void HAL_SMARTCARD_ErrorCallback(SMARTCARD_HandleTypeDef *hsmartcard) |
||
| 2030 | { |
||
| 2031 | /* Prevent unused argument(s) compilation warning */ |
||
| 2032 | UNUSED(hsmartcard); |
||
| 2033 | |||
| 2034 | /* NOTE : This function should not be modified, when the callback is needed, |
||
| 2035 | the HAL_SMARTCARD_ErrorCallback can be implemented in the user file. |
||
| 2036 | */ |
||
| 2037 | } |
||
| 2038 | |||
| 2039 | /** |
||
| 2040 | * @brief SMARTCARD Abort Complete callback. |
||
| 2041 | * @param hsmartcard Pointer to a SMARTCARD_HandleTypeDef structure that contains |
||
| 2042 | * the configuration information for the specified SMARTCARD module. |
||
| 2043 | * @retval None |
||
| 2044 | */ |
||
| 2045 | __weak void HAL_SMARTCARD_AbortCpltCallback(SMARTCARD_HandleTypeDef *hsmartcard) |
||
| 2046 | { |
||
| 2047 | /* Prevent unused argument(s) compilation warning */ |
||
| 2048 | UNUSED(hsmartcard); |
||
| 2049 | |||
| 2050 | /* NOTE : This function should not be modified, when the callback is needed, |
||
| 2051 | the HAL_SMARTCARD_AbortCpltCallback can be implemented in the user file. |
||
| 2052 | */ |
||
| 2053 | } |
||
| 2054 | |||
| 2055 | /** |
||
| 2056 | * @brief SMARTCARD Abort Complete callback. |
||
| 2057 | * @param hsmartcard Pointer to a SMARTCARD_HandleTypeDef structure that contains |
||
| 2058 | * the configuration information for the specified SMARTCARD module. |
||
| 2059 | * @retval None |
||
| 2060 | */ |
||
| 2061 | __weak void HAL_SMARTCARD_AbortTransmitCpltCallback(SMARTCARD_HandleTypeDef *hsmartcard) |
||
| 2062 | { |
||
| 2063 | /* Prevent unused argument(s) compilation warning */ |
||
| 2064 | UNUSED(hsmartcard); |
||
| 2065 | |||
| 2066 | /* NOTE : This function should not be modified, when the callback is needed, |
||
| 2067 | the HAL_SMARTCARD_AbortTransmitCpltCallback can be implemented in the user file. |
||
| 2068 | */ |
||
| 2069 | } |
||
| 2070 | |||
| 2071 | /** |
||
| 2072 | * @brief SMARTCARD Abort Receive Complete callback. |
||
| 2073 | * @param hsmartcard Pointer to a SMARTCARD_HandleTypeDef structure that contains |
||
| 2074 | * the configuration information for the specified SMARTCARD module. |
||
| 2075 | * @retval None |
||
| 2076 | */ |
||
| 2077 | __weak void HAL_SMARTCARD_AbortReceiveCpltCallback(SMARTCARD_HandleTypeDef *hsmartcard) |
||
| 2078 | { |
||
| 2079 | /* Prevent unused argument(s) compilation warning */ |
||
| 2080 | UNUSED(hsmartcard); |
||
| 2081 | |||
| 2082 | /* NOTE : This function should not be modified, when the callback is needed, |
||
| 2083 | the HAL_SMARTCARD_AbortReceiveCpltCallback can be implemented in the user file. |
||
| 2084 | */ |
||
| 2085 | } |
||
| 2086 | |||
| 2087 | /** |
||
| 2088 | * @} |
||
| 2089 | */ |
||
| 2090 | |||
| 2091 | /** @defgroup SMARTCARD_Exported_Functions_Group4 Peripheral State and Errors functions |
||
| 2092 | * @brief SMARTCARD State and Errors functions |
||
| 2093 | * |
||
| 2094 | @verbatim |
||
| 2095 | ============================================================================== |
||
| 2096 | ##### Peripheral State and Errors functions ##### |
||
| 2097 | ============================================================================== |
||
| 2098 | [..] |
||
| 2099 | This subsection provides a set of functions allowing to return the State of SmartCard |
||
| 2100 | handle and also return Peripheral Errors occurred during communication process |
||
| 2101 | (+) HAL_SMARTCARD_GetState() API can be helpful to check in run-time the state |
||
| 2102 | of the SMARTCARD peripheral. |
||
| 2103 | (+) HAL_SMARTCARD_GetError() checks in run-time errors that could occur during |
||
| 2104 | communication. |
||
| 2105 | |||
| 2106 | @endverbatim |
||
| 2107 | * @{ |
||
| 2108 | */ |
||
| 2109 | |||
| 2110 | /** |
||
| 2111 | * @brief Return the SMARTCARD handle state. |
||
| 2112 | * @param hsmartcard Pointer to a SMARTCARD_HandleTypeDef structure that contains |
||
| 2113 | * the configuration information for the specified SMARTCARD module. |
||
| 2114 | * @retval SMARTCARD handle state |
||
| 2115 | */ |
||
| 2116 | HAL_SMARTCARD_StateTypeDef HAL_SMARTCARD_GetState(SMARTCARD_HandleTypeDef *hsmartcard) |
||
| 2117 | { |
||
| 2118 | /* Return SMARTCARD handle state */ |
||
| 2119 | uint32_t temp1; |
||
| 2120 | uint32_t temp2; |
||
| 2121 | temp1 = (uint32_t)hsmartcard->gState; |
||
| 2122 | temp2 = (uint32_t)hsmartcard->RxState; |
||
| 2123 | |||
| 2124 | return (HAL_SMARTCARD_StateTypeDef)(temp1 | temp2); |
||
| 2125 | } |
||
| 2126 | |||
| 2127 | /** |
||
| 2128 | * @brief Return the SMARTCARD handle error code. |
||
| 2129 | * @param hsmartcard Pointer to a SMARTCARD_HandleTypeDef structure that contains |
||
| 2130 | * the configuration information for the specified SMARTCARD module. |
||
| 2131 | * @retval SMARTCARD handle Error Code |
||
| 2132 | */ |
||
| 2133 | uint32_t HAL_SMARTCARD_GetError(SMARTCARD_HandleTypeDef *hsmartcard) |
||
| 2134 | { |
||
| 2135 | return hsmartcard->ErrorCode; |
||
| 2136 | } |
||
| 2137 | |||
| 2138 | /** |
||
| 2139 | * @} |
||
| 2140 | */ |
||
| 2141 | |||
| 2142 | /** |
||
| 2143 | * @} |
||
| 2144 | */ |
||
| 2145 | |||
| 2146 | /** @defgroup SMARTCARD_Private_Functions SMARTCARD Private Functions |
||
| 2147 | * @{ |
||
| 2148 | */ |
||
| 2149 | |||
| 2150 | #if (USE_HAL_SMARTCARD_REGISTER_CALLBACKS == 1) |
||
| 2151 | /** |
||
| 2152 | * @brief Initialize the callbacks to their default values. |
||
| 2153 | * @param hsmartcard SMARTCARD handle. |
||
| 2154 | * @retval none |
||
| 2155 | */ |
||
| 2156 | void SMARTCARD_InitCallbacksToDefault(SMARTCARD_HandleTypeDef *hsmartcard) |
||
| 2157 | { |
||
| 2158 | /* Init the SMARTCARD Callback settings */ |
||
| 2159 | hsmartcard->TxCpltCallback = HAL_SMARTCARD_TxCpltCallback; /* Legacy weak TxCpltCallback */ |
||
| 2160 | hsmartcard->RxCpltCallback = HAL_SMARTCARD_RxCpltCallback; /* Legacy weak RxCpltCallback */ |
||
| 2161 | hsmartcard->ErrorCallback = HAL_SMARTCARD_ErrorCallback; /* Legacy weak ErrorCallback */ |
||
| 2162 | hsmartcard->AbortCpltCallback = HAL_SMARTCARD_AbortCpltCallback; /* Legacy weak AbortCpltCallback */ |
||
| 2163 | hsmartcard->AbortTransmitCpltCallback = HAL_SMARTCARD_AbortTransmitCpltCallback; /* Legacy weak AbortTransmitCpltCallback */ |
||
| 2164 | hsmartcard->AbortReceiveCpltCallback = HAL_SMARTCARD_AbortReceiveCpltCallback; /* Legacy weak AbortReceiveCpltCallback */ |
||
| 2165 | |||
| 2166 | } |
||
| 2167 | #endif /* USE_HAL_SMARTCARD_REGISTER_CALLBACKS */ |
||
| 2168 | |||
| 2169 | /** |
||
| 2170 | * @brief Configure the SMARTCARD associated USART peripheral. |
||
| 2171 | * @param hsmartcard Pointer to a SMARTCARD_HandleTypeDef structure that contains |
||
| 2172 | * the configuration information for the specified SMARTCARD module. |
||
| 2173 | * @retval HAL status |
||
| 2174 | */ |
||
| 2175 | static HAL_StatusTypeDef SMARTCARD_SetConfig(SMARTCARD_HandleTypeDef *hsmartcard) |
||
| 2176 | { |
||
| 2177 | uint32_t tmpreg; |
||
| 2178 | SMARTCARD_ClockSourceTypeDef clocksource; |
||
| 2179 | HAL_StatusTypeDef ret = HAL_OK; |
||
| 2180 | uint32_t pclk; |
||
| 2181 | |||
| 2182 | /* Check the parameters */ |
||
| 2183 | assert_param(IS_SMARTCARD_INSTANCE(hsmartcard->Instance)); |
||
| 2184 | assert_param(IS_SMARTCARD_BAUDRATE(hsmartcard->Init.BaudRate)); |
||
| 2185 | assert_param(IS_SMARTCARD_WORD_LENGTH(hsmartcard->Init.WordLength)); |
||
| 2186 | assert_param(IS_SMARTCARD_STOPBITS(hsmartcard->Init.StopBits)); |
||
| 2187 | assert_param(IS_SMARTCARD_PARITY(hsmartcard->Init.Parity)); |
||
| 2188 | assert_param(IS_SMARTCARD_MODE(hsmartcard->Init.Mode)); |
||
| 2189 | assert_param(IS_SMARTCARD_POLARITY(hsmartcard->Init.CLKPolarity)); |
||
| 2190 | assert_param(IS_SMARTCARD_PHASE(hsmartcard->Init.CLKPhase)); |
||
| 2191 | assert_param(IS_SMARTCARD_LASTBIT(hsmartcard->Init.CLKLastBit)); |
||
| 2192 | assert_param(IS_SMARTCARD_ONE_BIT_SAMPLE(hsmartcard->Init.OneBitSampling)); |
||
| 2193 | assert_param(IS_SMARTCARD_NACK(hsmartcard->Init.NACKEnable)); |
||
| 2194 | assert_param(IS_SMARTCARD_TIMEOUT(hsmartcard->Init.TimeOutEnable)); |
||
| 2195 | assert_param(IS_SMARTCARD_AUTORETRY_COUNT(hsmartcard->Init.AutoRetryCount)); |
||
| 2196 | |||
| 2197 | /*-------------------------- USART CR1 Configuration -----------------------*/ |
||
| 2198 | /* In SmartCard mode, M and PCE are forced to 1 (8 bits + parity). |
||
| 2199 | * Oversampling is forced to 16 (OVER8 = 0). |
||
| 2200 | * Configure the Parity and Mode: |
||
| 2201 | * set PS bit according to hsmartcard->Init.Parity value |
||
| 2202 | * set TE and RE bits according to hsmartcard->Init.Mode value */ |
||
| 2203 | tmpreg = (uint32_t)(hsmartcard->Init.Parity | hsmartcard->Init.Mode | hsmartcard->Init.WordLength); |
||
| 2204 | MODIFY_REG(hsmartcard->Instance->CR1, USART_CR1_FIELDS, tmpreg); |
||
| 2205 | |||
| 2206 | /*-------------------------- USART CR2 Configuration -----------------------*/ |
||
| 2207 | tmpreg = hsmartcard->Init.StopBits; |
||
| 2208 | /* Synchronous mode is activated by default */ |
||
| 2209 | tmpreg |= (uint32_t) USART_CR2_CLKEN | hsmartcard->Init.CLKPolarity; |
||
| 2210 | tmpreg |= (uint32_t) hsmartcard->Init.CLKPhase | hsmartcard->Init.CLKLastBit; |
||
| 2211 | tmpreg |= (uint32_t) hsmartcard->Init.TimeOutEnable; |
||
| 2212 | MODIFY_REG(hsmartcard->Instance->CR2, USART_CR2_FIELDS, tmpreg); |
||
| 2213 | |||
| 2214 | /*-------------------------- USART CR3 Configuration -----------------------*/ |
||
| 2215 | /* Configure |
||
| 2216 | * - one-bit sampling method versus three samples' majority rule |
||
| 2217 | * according to hsmartcard->Init.OneBitSampling |
||
| 2218 | * - NACK transmission in case of parity error according |
||
| 2219 | * to hsmartcard->Init.NACKEnable |
||
| 2220 | * - autoretry counter according to hsmartcard->Init.AutoRetryCount */ |
||
| 2221 | |||
| 2222 | tmpreg = (uint32_t) hsmartcard->Init.OneBitSampling | hsmartcard->Init.NACKEnable; |
||
| 2223 | tmpreg |= ((uint32_t)hsmartcard->Init.AutoRetryCount << USART_CR3_SCARCNT_Pos); |
||
| 2224 | MODIFY_REG(hsmartcard->Instance->CR3, USART_CR3_FIELDS, tmpreg); |
||
| 2225 | |||
| 2226 | |||
| 2227 | /*-------------------------- USART GTPR Configuration ----------------------*/ |
||
| 2228 | tmpreg = (hsmartcard->Init.Prescaler | ((uint32_t)hsmartcard->Init.GuardTime << USART_GTPR_GT_Pos)); |
||
| 2229 | MODIFY_REG(hsmartcard->Instance->GTPR, (uint16_t)(USART_GTPR_GT | USART_GTPR_PSC), (uint16_t)tmpreg); |
||
| 2230 | |||
| 2231 | /*-------------------------- USART RTOR Configuration ----------------------*/ |
||
| 2232 | tmpreg = ((uint32_t)hsmartcard->Init.BlockLength << USART_RTOR_BLEN_Pos); |
||
| 2233 | if (hsmartcard->Init.TimeOutEnable == SMARTCARD_TIMEOUT_ENABLE) |
||
| 2234 | { |
||
| 2235 | assert_param(IS_SMARTCARD_TIMEOUT_VALUE(hsmartcard->Init.TimeOutValue)); |
||
| 2236 | tmpreg |= (uint32_t) hsmartcard->Init.TimeOutValue; |
||
| 2237 | } |
||
| 2238 | MODIFY_REG(hsmartcard->Instance->RTOR, (USART_RTOR_RTO | USART_RTOR_BLEN), tmpreg); |
||
| 2239 | |||
| 2240 | /*-------------------------- USART BRR Configuration -----------------------*/ |
||
| 2241 | SMARTCARD_GETCLOCKSOURCE(hsmartcard, clocksource); |
||
| 2242 | tmpreg = 0U; |
||
| 2243 | switch (clocksource) |
||
| 2244 | { |
||
| 2245 | case SMARTCARD_CLOCKSOURCE_PCLK1: |
||
| 2246 | pclk = HAL_RCC_GetPCLK1Freq(); |
||
| 2247 | tmpreg = (uint16_t)((pclk + (hsmartcard->Init.BaudRate / 2U)) / hsmartcard->Init.BaudRate); |
||
| 2248 | break; |
||
| 2249 | case SMARTCARD_CLOCKSOURCE_HSI: |
||
| 2250 | tmpreg = (uint16_t)((HSI_VALUE + (hsmartcard->Init.BaudRate / 2U)) / hsmartcard->Init.BaudRate); |
||
| 2251 | break; |
||
| 2252 | case SMARTCARD_CLOCKSOURCE_SYSCLK: |
||
| 2253 | pclk = HAL_RCC_GetSysClockFreq(); |
||
| 2254 | tmpreg = (uint16_t)((pclk + (hsmartcard->Init.BaudRate / 2U)) / hsmartcard->Init.BaudRate); |
||
| 2255 | break; |
||
| 2256 | case SMARTCARD_CLOCKSOURCE_LSE: |
||
| 2257 | tmpreg = (uint16_t)((LSE_VALUE + (hsmartcard->Init.BaudRate / 2U)) / hsmartcard->Init.BaudRate); |
||
| 2258 | break; |
||
| 2259 | default: |
||
| 2260 | ret = HAL_ERROR; |
||
| 2261 | break; |
||
| 2262 | } |
||
| 2263 | |||
| 2264 | /* USARTDIV must be greater than or equal to 0d16 */ |
||
| 2265 | if ((tmpreg >= USART_BRR_MIN) && (tmpreg <= USART_BRR_MAX)) |
||
| 2266 | { |
||
| 2267 | hsmartcard->Instance->BRR = tmpreg; |
||
| 2268 | } |
||
| 2269 | else |
||
| 2270 | { |
||
| 2271 | ret = HAL_ERROR; |
||
| 2272 | } |
||
| 2273 | |||
| 2274 | |||
| 2275 | /* Clear ISR function pointers */ |
||
| 2276 | hsmartcard->RxISR = NULL; |
||
| 2277 | hsmartcard->TxISR = NULL; |
||
| 2278 | |||
| 2279 | return ret; |
||
| 2280 | } |
||
| 2281 | |||
| 2282 | |||
| 2283 | /** |
||
| 2284 | * @brief Configure the SMARTCARD associated USART peripheral advanced features. |
||
| 2285 | * @param hsmartcard Pointer to a SMARTCARD_HandleTypeDef structure that contains |
||
| 2286 | * the configuration information for the specified SMARTCARD module. |
||
| 2287 | * @retval None |
||
| 2288 | */ |
||
| 2289 | static void SMARTCARD_AdvFeatureConfig(SMARTCARD_HandleTypeDef *hsmartcard) |
||
| 2290 | { |
||
| 2291 | /* Check whether the set of advanced features to configure is properly set */ |
||
| 2292 | assert_param(IS_SMARTCARD_ADVFEATURE_INIT(hsmartcard->AdvancedInit.AdvFeatureInit)); |
||
| 2293 | |||
| 2294 | /* if required, configure TX pin active level inversion */ |
||
| 2295 | if (HAL_IS_BIT_SET(hsmartcard->AdvancedInit.AdvFeatureInit, SMARTCARD_ADVFEATURE_TXINVERT_INIT)) |
||
| 2296 | { |
||
| 2297 | assert_param(IS_SMARTCARD_ADVFEATURE_TXINV(hsmartcard->AdvancedInit.TxPinLevelInvert)); |
||
| 2298 | MODIFY_REG(hsmartcard->Instance->CR2, USART_CR2_TXINV, hsmartcard->AdvancedInit.TxPinLevelInvert); |
||
| 2299 | } |
||
| 2300 | |||
| 2301 | /* if required, configure RX pin active level inversion */ |
||
| 2302 | if (HAL_IS_BIT_SET(hsmartcard->AdvancedInit.AdvFeatureInit, SMARTCARD_ADVFEATURE_RXINVERT_INIT)) |
||
| 2303 | { |
||
| 2304 | assert_param(IS_SMARTCARD_ADVFEATURE_RXINV(hsmartcard->AdvancedInit.RxPinLevelInvert)); |
||
| 2305 | MODIFY_REG(hsmartcard->Instance->CR2, USART_CR2_RXINV, hsmartcard->AdvancedInit.RxPinLevelInvert); |
||
| 2306 | } |
||
| 2307 | |||
| 2308 | /* if required, configure data inversion */ |
||
| 2309 | if (HAL_IS_BIT_SET(hsmartcard->AdvancedInit.AdvFeatureInit, SMARTCARD_ADVFEATURE_DATAINVERT_INIT)) |
||
| 2310 | { |
||
| 2311 | assert_param(IS_SMARTCARD_ADVFEATURE_DATAINV(hsmartcard->AdvancedInit.DataInvert)); |
||
| 2312 | MODIFY_REG(hsmartcard->Instance->CR2, USART_CR2_DATAINV, hsmartcard->AdvancedInit.DataInvert); |
||
| 2313 | } |
||
| 2314 | |||
| 2315 | /* if required, configure RX/TX pins swap */ |
||
| 2316 | if (HAL_IS_BIT_SET(hsmartcard->AdvancedInit.AdvFeatureInit, SMARTCARD_ADVFEATURE_SWAP_INIT)) |
||
| 2317 | { |
||
| 2318 | assert_param(IS_SMARTCARD_ADVFEATURE_SWAP(hsmartcard->AdvancedInit.Swap)); |
||
| 2319 | MODIFY_REG(hsmartcard->Instance->CR2, USART_CR2_SWAP, hsmartcard->AdvancedInit.Swap); |
||
| 2320 | } |
||
| 2321 | |||
| 2322 | /* if required, configure RX overrun detection disabling */ |
||
| 2323 | if (HAL_IS_BIT_SET(hsmartcard->AdvancedInit.AdvFeatureInit, SMARTCARD_ADVFEATURE_RXOVERRUNDISABLE_INIT)) |
||
| 2324 | { |
||
| 2325 | assert_param(IS_SMARTCARD_OVERRUN(hsmartcard->AdvancedInit.OverrunDisable)); |
||
| 2326 | MODIFY_REG(hsmartcard->Instance->CR3, USART_CR3_OVRDIS, hsmartcard->AdvancedInit.OverrunDisable); |
||
| 2327 | } |
||
| 2328 | |||
| 2329 | /* if required, configure DMA disabling on reception error */ |
||
| 2330 | if (HAL_IS_BIT_SET(hsmartcard->AdvancedInit.AdvFeatureInit, SMARTCARD_ADVFEATURE_DMADISABLEONERROR_INIT)) |
||
| 2331 | { |
||
| 2332 | assert_param(IS_SMARTCARD_ADVFEATURE_DMAONRXERROR(hsmartcard->AdvancedInit.DMADisableonRxError)); |
||
| 2333 | MODIFY_REG(hsmartcard->Instance->CR3, USART_CR3_DDRE, hsmartcard->AdvancedInit.DMADisableonRxError); |
||
| 2334 | } |
||
| 2335 | |||
| 2336 | /* if required, configure MSB first on communication line */ |
||
| 2337 | if (HAL_IS_BIT_SET(hsmartcard->AdvancedInit.AdvFeatureInit, SMARTCARD_ADVFEATURE_MSBFIRST_INIT)) |
||
| 2338 | { |
||
| 2339 | assert_param(IS_SMARTCARD_ADVFEATURE_MSBFIRST(hsmartcard->AdvancedInit.MSBFirst)); |
||
| 2340 | MODIFY_REG(hsmartcard->Instance->CR2, USART_CR2_MSBFIRST, hsmartcard->AdvancedInit.MSBFirst); |
||
| 2341 | } |
||
| 2342 | |||
| 2343 | } |
||
| 2344 | |||
| 2345 | /** |
||
| 2346 | * @brief Check the SMARTCARD Idle State. |
||
| 2347 | * @param hsmartcard Pointer to a SMARTCARD_HandleTypeDef structure that contains |
||
| 2348 | * the configuration information for the specified SMARTCARD module. |
||
| 2349 | * @retval HAL status |
||
| 2350 | */ |
||
| 2351 | static HAL_StatusTypeDef SMARTCARD_CheckIdleState(SMARTCARD_HandleTypeDef *hsmartcard) |
||
| 2352 | { |
||
| 2353 | uint32_t tickstart; |
||
| 2354 | |||
| 2355 | /* Initialize the SMARTCARD ErrorCode */ |
||
| 2356 | hsmartcard->ErrorCode = HAL_SMARTCARD_ERROR_NONE; |
||
| 2357 | |||
| 2358 | /* Init tickstart for timeout management */ |
||
| 2359 | tickstart = HAL_GetTick(); |
||
| 2360 | |||
| 2361 | /* Check if the Transmitter is enabled */ |
||
| 2362 | if ((hsmartcard->Instance->CR1 & USART_CR1_TE) == USART_CR1_TE) |
||
| 2363 | { |
||
| 2364 | /* Wait until TEACK flag is set */ |
||
| 2365 | if (SMARTCARD_WaitOnFlagUntilTimeout(hsmartcard, USART_ISR_TEACK, RESET, tickstart, |
||
| 2366 | SMARTCARD_TEACK_REACK_TIMEOUT) != HAL_OK) |
||
| 2367 | { |
||
| 2368 | /* Timeout occurred */ |
||
| 2369 | return HAL_TIMEOUT; |
||
| 2370 | } |
||
| 2371 | } |
||
| 2372 | /* Check if the Receiver is enabled */ |
||
| 2373 | if ((hsmartcard->Instance->CR1 & USART_CR1_RE) == USART_CR1_RE) |
||
| 2374 | { |
||
| 2375 | /* Wait until REACK flag is set */ |
||
| 2376 | if (SMARTCARD_WaitOnFlagUntilTimeout(hsmartcard, USART_ISR_REACK, RESET, tickstart, |
||
| 2377 | SMARTCARD_TEACK_REACK_TIMEOUT) != HAL_OK) |
||
| 2378 | { |
||
| 2379 | /* Timeout occurred */ |
||
| 2380 | return HAL_TIMEOUT; |
||
| 2381 | } |
||
| 2382 | } |
||
| 2383 | |||
| 2384 | /* Initialize the SMARTCARD states */ |
||
| 2385 | hsmartcard->gState = HAL_SMARTCARD_STATE_READY; |
||
| 2386 | hsmartcard->RxState = HAL_SMARTCARD_STATE_READY; |
||
| 2387 | |||
| 2388 | /* Process Unlocked */ |
||
| 2389 | __HAL_UNLOCK(hsmartcard); |
||
| 2390 | |||
| 2391 | return HAL_OK; |
||
| 2392 | } |
||
| 2393 | |||
| 2394 | /** |
||
| 2395 | * @brief Handle SMARTCARD Communication Timeout. |
||
| 2396 | * @param hsmartcard Pointer to a SMARTCARD_HandleTypeDef structure that contains |
||
| 2397 | * the configuration information for the specified SMARTCARD module. |
||
| 2398 | * @param Flag Specifies the SMARTCARD flag to check. |
||
| 2399 | * @param Status The new Flag status (SET or RESET). |
||
| 2400 | * @param Tickstart Tick start value |
||
| 2401 | * @param Timeout Timeout duration. |
||
| 2402 | * @retval HAL status |
||
| 2403 | */ |
||
| 2404 | static HAL_StatusTypeDef SMARTCARD_WaitOnFlagUntilTimeout(SMARTCARD_HandleTypeDef *hsmartcard, uint32_t Flag, |
||
| 2405 | FlagStatus Status, uint32_t Tickstart, uint32_t Timeout) |
||
| 2406 | { |
||
| 2407 | /* Wait until flag is set */ |
||
| 2408 | while ((__HAL_SMARTCARD_GET_FLAG(hsmartcard, Flag) ? SET : RESET) == Status) |
||
| 2409 | { |
||
| 2410 | /* Check for the Timeout */ |
||
| 2411 | if (Timeout != HAL_MAX_DELAY) |
||
| 2412 | { |
||
| 2413 | if (((HAL_GetTick() - Tickstart) > Timeout) || (Timeout == 0U)) |
||
| 2414 | { |
||
| 2415 | /* Disable TXE, RXNE, PE and ERR (Frame error, noise error, overrun error) interrupts for the interrupt process */ |
||
| 2416 | CLEAR_BIT(hsmartcard->Instance->CR1, (USART_CR1_RXNEIE | USART_CR1_PEIE | USART_CR1_TXEIE)); |
||
| 2417 | CLEAR_BIT(hsmartcard->Instance->CR3, USART_CR3_EIE); |
||
| 2418 | |||
| 2419 | hsmartcard->gState = HAL_SMARTCARD_STATE_READY; |
||
| 2420 | hsmartcard->RxState = HAL_SMARTCARD_STATE_READY; |
||
| 2421 | |||
| 2422 | /* Process Unlocked */ |
||
| 2423 | __HAL_UNLOCK(hsmartcard); |
||
| 2424 | return HAL_TIMEOUT; |
||
| 2425 | } |
||
| 2426 | } |
||
| 2427 | } |
||
| 2428 | return HAL_OK; |
||
| 2429 | } |
||
| 2430 | |||
| 2431 | |||
| 2432 | /** |
||
| 2433 | * @brief End ongoing Tx transfer on SMARTCARD peripheral (following error detection or Transmit completion). |
||
| 2434 | * @param hsmartcard Pointer to a SMARTCARD_HandleTypeDef structure that contains |
||
| 2435 | * the configuration information for the specified SMARTCARD module. |
||
| 2436 | * @retval None |
||
| 2437 | */ |
||
| 2438 | static void SMARTCARD_EndTxTransfer(SMARTCARD_HandleTypeDef *hsmartcard) |
||
| 2439 | { |
||
| 2440 | /* Disable TXEIE, TCIE and ERR (Frame error, noise error, overrun error) interrupts */ |
||
| 2441 | CLEAR_BIT(hsmartcard->Instance->CR1, (USART_CR1_TXEIE | USART_CR1_TCIE)); |
||
| 2442 | CLEAR_BIT(hsmartcard->Instance->CR3, USART_CR3_EIE); |
||
| 2443 | |||
| 2444 | /* At end of Tx process, restore hsmartcard->gState to Ready */ |
||
| 2445 | hsmartcard->gState = HAL_SMARTCARD_STATE_READY; |
||
| 2446 | } |
||
| 2447 | |||
| 2448 | |||
| 2449 | /** |
||
| 2450 | * @brief End ongoing Rx transfer on UART peripheral (following error detection or Reception completion). |
||
| 2451 | * @param hsmartcard Pointer to a SMARTCARD_HandleTypeDef structure that contains |
||
| 2452 | * the configuration information for the specified SMARTCARD module. |
||
| 2453 | * @retval None |
||
| 2454 | */ |
||
| 2455 | static void SMARTCARD_EndRxTransfer(SMARTCARD_HandleTypeDef *hsmartcard) |
||
| 2456 | { |
||
| 2457 | /* Disable RXNE, PE and ERR (Frame error, noise error, overrun error) interrupts */ |
||
| 2458 | CLEAR_BIT(hsmartcard->Instance->CR1, (USART_CR1_RXNEIE | USART_CR1_PEIE)); |
||
| 2459 | CLEAR_BIT(hsmartcard->Instance->CR3, USART_CR3_EIE); |
||
| 2460 | |||
| 2461 | /* At end of Rx process, restore hsmartcard->RxState to Ready */ |
||
| 2462 | hsmartcard->RxState = HAL_SMARTCARD_STATE_READY; |
||
| 2463 | } |
||
| 2464 | |||
| 2465 | |||
| 2466 | /** |
||
| 2467 | * @brief DMA SMARTCARD transmit process complete callback. |
||
| 2468 | * @param hdma Pointer to a DMA_HandleTypeDef structure that contains |
||
| 2469 | * the configuration information for the specified DMA module. |
||
| 2470 | * @retval None |
||
| 2471 | */ |
||
| 2472 | static void SMARTCARD_DMATransmitCplt(DMA_HandleTypeDef *hdma) |
||
| 2473 | { |
||
| 2474 | SMARTCARD_HandleTypeDef *hsmartcard = (SMARTCARD_HandleTypeDef *)(hdma->Parent); |
||
| 2475 | hsmartcard->TxXferCount = 0U; |
||
| 2476 | |||
| 2477 | /* Disable the DMA transfer for transmit request by resetting the DMAT bit |
||
| 2478 | in the SMARTCARD associated USART CR3 register */ |
||
| 2479 | CLEAR_BIT(hsmartcard->Instance->CR3, USART_CR3_DMAT); |
||
| 2480 | |||
| 2481 | /* Enable the SMARTCARD Transmit Complete Interrupt */ |
||
| 2482 | __HAL_SMARTCARD_ENABLE_IT(hsmartcard, hsmartcard->AdvancedInit.TxCompletionIndication); |
||
| 2483 | } |
||
| 2484 | |||
| 2485 | /** |
||
| 2486 | * @brief DMA SMARTCARD receive process complete callback. |
||
| 2487 | * @param hdma Pointer to a DMA_HandleTypeDef structure that contains |
||
| 2488 | * the configuration information for the specified DMA module. |
||
| 2489 | * @retval None |
||
| 2490 | */ |
||
| 2491 | static void SMARTCARD_DMAReceiveCplt(DMA_HandleTypeDef *hdma) |
||
| 2492 | { |
||
| 2493 | SMARTCARD_HandleTypeDef *hsmartcard = (SMARTCARD_HandleTypeDef *)(hdma->Parent); |
||
| 2494 | hsmartcard->RxXferCount = 0U; |
||
| 2495 | |||
| 2496 | /* Disable PE and ERR (Frame error, noise error, overrun error) interrupts */ |
||
| 2497 | CLEAR_BIT(hsmartcard->Instance->CR1, USART_CR1_PEIE); |
||
| 2498 | CLEAR_BIT(hsmartcard->Instance->CR3, USART_CR3_EIE); |
||
| 2499 | |||
| 2500 | /* Disable the DMA transfer for the receiver request by resetting the DMAR bit |
||
| 2501 | in the SMARTCARD associated USART CR3 register */ |
||
| 2502 | CLEAR_BIT(hsmartcard->Instance->CR3, USART_CR3_DMAR); |
||
| 2503 | |||
| 2504 | /* At end of Rx process, restore hsmartcard->RxState to Ready */ |
||
| 2505 | hsmartcard->RxState = HAL_SMARTCARD_STATE_READY; |
||
| 2506 | |||
| 2507 | #if (USE_HAL_SMARTCARD_REGISTER_CALLBACKS == 1) |
||
| 2508 | /* Call registered Rx complete callback */ |
||
| 2509 | hsmartcard->RxCpltCallback(hsmartcard); |
||
| 2510 | #else |
||
| 2511 | /* Call legacy weak Rx complete callback */ |
||
| 2512 | HAL_SMARTCARD_RxCpltCallback(hsmartcard); |
||
| 2513 | #endif /* USE_HAL_SMARTCARD_REGISTER_CALLBACK */ |
||
| 2514 | } |
||
| 2515 | |||
| 2516 | /** |
||
| 2517 | * @brief DMA SMARTCARD communication error callback. |
||
| 2518 | * @param hdma Pointer to a DMA_HandleTypeDef structure that contains |
||
| 2519 | * the configuration information for the specified DMA module. |
||
| 2520 | * @retval None |
||
| 2521 | */ |
||
| 2522 | static void SMARTCARD_DMAError(DMA_HandleTypeDef *hdma) |
||
| 2523 | { |
||
| 2524 | SMARTCARD_HandleTypeDef *hsmartcard = (SMARTCARD_HandleTypeDef *)(hdma->Parent); |
||
| 2525 | |||
| 2526 | /* Stop SMARTCARD DMA Tx request if ongoing */ |
||
| 2527 | if (hsmartcard->gState == HAL_SMARTCARD_STATE_BUSY_TX) |
||
| 2528 | { |
||
| 2529 | if (HAL_IS_BIT_SET(hsmartcard->Instance->CR3, USART_CR3_DMAT)) |
||
| 2530 | { |
||
| 2531 | hsmartcard->TxXferCount = 0U; |
||
| 2532 | SMARTCARD_EndTxTransfer(hsmartcard); |
||
| 2533 | } |
||
| 2534 | } |
||
| 2535 | |||
| 2536 | /* Stop SMARTCARD DMA Rx request if ongoing */ |
||
| 2537 | if (hsmartcard->RxState == HAL_SMARTCARD_STATE_BUSY_RX) |
||
| 2538 | { |
||
| 2539 | if (HAL_IS_BIT_SET(hsmartcard->Instance->CR3, USART_CR3_DMAR)) |
||
| 2540 | { |
||
| 2541 | hsmartcard->RxXferCount = 0U; |
||
| 2542 | SMARTCARD_EndRxTransfer(hsmartcard); |
||
| 2543 | } |
||
| 2544 | } |
||
| 2545 | |||
| 2546 | hsmartcard->ErrorCode |= HAL_SMARTCARD_ERROR_DMA; |
||
| 2547 | #if (USE_HAL_SMARTCARD_REGISTER_CALLBACKS == 1) |
||
| 2548 | /* Call registered user error callback */ |
||
| 2549 | hsmartcard->ErrorCallback(hsmartcard); |
||
| 2550 | #else |
||
| 2551 | /* Call legacy weak user error callback */ |
||
| 2552 | HAL_SMARTCARD_ErrorCallback(hsmartcard); |
||
| 2553 | #endif /* USE_HAL_SMARTCARD_REGISTER_CALLBACK */ |
||
| 2554 | } |
||
| 2555 | |||
| 2556 | /** |
||
| 2557 | * @brief DMA SMARTCARD communication abort callback, when initiated by HAL services on Error |
||
| 2558 | * (To be called at end of DMA Abort procedure following error occurrence). |
||
| 2559 | * @param hdma DMA handle. |
||
| 2560 | * @retval None |
||
| 2561 | */ |
||
| 2562 | static void SMARTCARD_DMAAbortOnError(DMA_HandleTypeDef *hdma) |
||
| 2563 | { |
||
| 2564 | SMARTCARD_HandleTypeDef *hsmartcard = (SMARTCARD_HandleTypeDef *)(hdma->Parent); |
||
| 2565 | hsmartcard->RxXferCount = 0U; |
||
| 2566 | hsmartcard->TxXferCount = 0U; |
||
| 2567 | |||
| 2568 | #if (USE_HAL_SMARTCARD_REGISTER_CALLBACKS == 1) |
||
| 2569 | /* Call registered user error callback */ |
||
| 2570 | hsmartcard->ErrorCallback(hsmartcard); |
||
| 2571 | #else |
||
| 2572 | /* Call legacy weak user error callback */ |
||
| 2573 | HAL_SMARTCARD_ErrorCallback(hsmartcard); |
||
| 2574 | #endif /* USE_HAL_SMARTCARD_REGISTER_CALLBACK */ |
||
| 2575 | } |
||
| 2576 | |||
| 2577 | /** |
||
| 2578 | * @brief DMA SMARTCARD Tx communication abort callback, when initiated by user |
||
| 2579 | * (To be called at end of DMA Tx Abort procedure following user abort request). |
||
| 2580 | * @note When this callback is executed, User Abort complete call back is called only if no |
||
| 2581 | * Abort still ongoing for Rx DMA Handle. |
||
| 2582 | * @param hdma DMA handle. |
||
| 2583 | * @retval None |
||
| 2584 | */ |
||
| 2585 | static void SMARTCARD_DMATxAbortCallback(DMA_HandleTypeDef *hdma) |
||
| 2586 | { |
||
| 2587 | SMARTCARD_HandleTypeDef *hsmartcard = (SMARTCARD_HandleTypeDef *)(hdma->Parent); |
||
| 2588 | |||
| 2589 | hsmartcard->hdmatx->XferAbortCallback = NULL; |
||
| 2590 | |||
| 2591 | /* Check if an Abort process is still ongoing */ |
||
| 2592 | if (hsmartcard->hdmarx != NULL) |
||
| 2593 | { |
||
| 2594 | if (hsmartcard->hdmarx->XferAbortCallback != NULL) |
||
| 2595 | { |
||
| 2596 | return; |
||
| 2597 | } |
||
| 2598 | } |
||
| 2599 | |||
| 2600 | /* No Abort process still ongoing : All DMA channels are aborted, call user Abort Complete callback */ |
||
| 2601 | hsmartcard->TxXferCount = 0U; |
||
| 2602 | hsmartcard->RxXferCount = 0U; |
||
| 2603 | |||
| 2604 | /* Reset errorCode */ |
||
| 2605 | hsmartcard->ErrorCode = HAL_SMARTCARD_ERROR_NONE; |
||
| 2606 | |||
| 2607 | /* Clear the Error flags in the ICR register */ |
||
| 2608 | __HAL_SMARTCARD_CLEAR_FLAG(hsmartcard, |
||
| 2609 | SMARTCARD_CLEAR_OREF | SMARTCARD_CLEAR_NEF | SMARTCARD_CLEAR_PEF | SMARTCARD_CLEAR_FEF | SMARTCARD_CLEAR_RTOF | |
||
| 2610 | SMARTCARD_CLEAR_EOBF); |
||
| 2611 | |||
| 2612 | /* Restore hsmartcard->gState and hsmartcard->RxState to Ready */ |
||
| 2613 | hsmartcard->gState = HAL_SMARTCARD_STATE_READY; |
||
| 2614 | hsmartcard->RxState = HAL_SMARTCARD_STATE_READY; |
||
| 2615 | |||
| 2616 | #if (USE_HAL_SMARTCARD_REGISTER_CALLBACKS == 1) |
||
| 2617 | /* Call registered Abort complete callback */ |
||
| 2618 | hsmartcard->AbortCpltCallback(hsmartcard); |
||
| 2619 | #else |
||
| 2620 | /* Call legacy weak Abort complete callback */ |
||
| 2621 | HAL_SMARTCARD_AbortCpltCallback(hsmartcard); |
||
| 2622 | #endif /* USE_HAL_SMARTCARD_REGISTER_CALLBACK */ |
||
| 2623 | } |
||
| 2624 | |||
| 2625 | |||
| 2626 | /** |
||
| 2627 | * @brief DMA SMARTCARD Rx communication abort callback, when initiated by user |
||
| 2628 | * (To be called at end of DMA Rx Abort procedure following user abort request). |
||
| 2629 | * @note When this callback is executed, User Abort complete call back is called only if no |
||
| 2630 | * Abort still ongoing for Tx DMA Handle. |
||
| 2631 | * @param hdma DMA handle. |
||
| 2632 | * @retval None |
||
| 2633 | */ |
||
| 2634 | static void SMARTCARD_DMARxAbortCallback(DMA_HandleTypeDef *hdma) |
||
| 2635 | { |
||
| 2636 | SMARTCARD_HandleTypeDef *hsmartcard = (SMARTCARD_HandleTypeDef *)(hdma->Parent); |
||
| 2637 | |||
| 2638 | hsmartcard->hdmarx->XferAbortCallback = NULL; |
||
| 2639 | |||
| 2640 | /* Check if an Abort process is still ongoing */ |
||
| 2641 | if (hsmartcard->hdmatx != NULL) |
||
| 2642 | { |
||
| 2643 | if (hsmartcard->hdmatx->XferAbortCallback != NULL) |
||
| 2644 | { |
||
| 2645 | return; |
||
| 2646 | } |
||
| 2647 | } |
||
| 2648 | |||
| 2649 | /* No Abort process still ongoing : All DMA channels are aborted, call user Abort Complete callback */ |
||
| 2650 | hsmartcard->TxXferCount = 0U; |
||
| 2651 | hsmartcard->RxXferCount = 0U; |
||
| 2652 | |||
| 2653 | /* Reset errorCode */ |
||
| 2654 | hsmartcard->ErrorCode = HAL_SMARTCARD_ERROR_NONE; |
||
| 2655 | |||
| 2656 | /* Clear the Error flags in the ICR register */ |
||
| 2657 | __HAL_SMARTCARD_CLEAR_FLAG(hsmartcard, |
||
| 2658 | SMARTCARD_CLEAR_OREF | SMARTCARD_CLEAR_NEF | SMARTCARD_CLEAR_PEF | SMARTCARD_CLEAR_FEF | SMARTCARD_CLEAR_RTOF | |
||
| 2659 | SMARTCARD_CLEAR_EOBF); |
||
| 2660 | |||
| 2661 | /* Restore hsmartcard->gState and hsmartcard->RxState to Ready */ |
||
| 2662 | hsmartcard->gState = HAL_SMARTCARD_STATE_READY; |
||
| 2663 | hsmartcard->RxState = HAL_SMARTCARD_STATE_READY; |
||
| 2664 | |||
| 2665 | #if (USE_HAL_SMARTCARD_REGISTER_CALLBACKS == 1) |
||
| 2666 | /* Call registered Abort complete callback */ |
||
| 2667 | hsmartcard->AbortCpltCallback(hsmartcard); |
||
| 2668 | #else |
||
| 2669 | /* Call legacy weak Abort complete callback */ |
||
| 2670 | HAL_SMARTCARD_AbortCpltCallback(hsmartcard); |
||
| 2671 | #endif /* USE_HAL_SMARTCARD_REGISTER_CALLBACK */ |
||
| 2672 | } |
||
| 2673 | |||
| 2674 | |||
| 2675 | /** |
||
| 2676 | * @brief DMA SMARTCARD Tx communication abort callback, when initiated by user by a call to |
||
| 2677 | * HAL_SMARTCARD_AbortTransmit_IT API (Abort only Tx transfer) |
||
| 2678 | * (This callback is executed at end of DMA Tx Abort procedure following user abort request, |
||
| 2679 | * and leads to user Tx Abort Complete callback execution). |
||
| 2680 | * @param hdma DMA handle. |
||
| 2681 | * @retval None |
||
| 2682 | */ |
||
| 2683 | static void SMARTCARD_DMATxOnlyAbortCallback(DMA_HandleTypeDef *hdma) |
||
| 2684 | { |
||
| 2685 | SMARTCARD_HandleTypeDef *hsmartcard = (SMARTCARD_HandleTypeDef *)(hdma->Parent); |
||
| 2686 | |||
| 2687 | hsmartcard->TxXferCount = 0U; |
||
| 2688 | |||
| 2689 | /* Clear the Error flags in the ICR register */ |
||
| 2690 | __HAL_SMARTCARD_CLEAR_FLAG(hsmartcard, SMARTCARD_CLEAR_FEF); |
||
| 2691 | |||
| 2692 | /* Restore hsmartcard->gState to Ready */ |
||
| 2693 | hsmartcard->gState = HAL_SMARTCARD_STATE_READY; |
||
| 2694 | |||
| 2695 | #if (USE_HAL_SMARTCARD_REGISTER_CALLBACKS == 1) |
||
| 2696 | /* Call registered Abort Transmit Complete Callback */ |
||
| 2697 | hsmartcard->AbortTransmitCpltCallback(hsmartcard); |
||
| 2698 | #else |
||
| 2699 | /* Call legacy weak Abort Transmit Complete Callback */ |
||
| 2700 | HAL_SMARTCARD_AbortTransmitCpltCallback(hsmartcard); |
||
| 2701 | #endif /* USE_HAL_SMARTCARD_REGISTER_CALLBACK */ |
||
| 2702 | } |
||
| 2703 | |||
| 2704 | /** |
||
| 2705 | * @brief DMA SMARTCARD Rx communication abort callback, when initiated by user by a call to |
||
| 2706 | * HAL_SMARTCARD_AbortReceive_IT API (Abort only Rx transfer) |
||
| 2707 | * (This callback is executed at end of DMA Rx Abort procedure following user abort request, |
||
| 2708 | * and leads to user Rx Abort Complete callback execution). |
||
| 2709 | * @param hdma DMA handle. |
||
| 2710 | * @retval None |
||
| 2711 | */ |
||
| 2712 | static void SMARTCARD_DMARxOnlyAbortCallback(DMA_HandleTypeDef *hdma) |
||
| 2713 | { |
||
| 2714 | SMARTCARD_HandleTypeDef *hsmartcard = (SMARTCARD_HandleTypeDef *)(hdma->Parent); |
||
| 2715 | |||
| 2716 | hsmartcard->RxXferCount = 0U; |
||
| 2717 | |||
| 2718 | /* Clear the Error flags in the ICR register */ |
||
| 2719 | __HAL_SMARTCARD_CLEAR_FLAG(hsmartcard, |
||
| 2720 | SMARTCARD_CLEAR_OREF | SMARTCARD_CLEAR_NEF | SMARTCARD_CLEAR_PEF | SMARTCARD_CLEAR_FEF | SMARTCARD_CLEAR_RTOF | |
||
| 2721 | SMARTCARD_CLEAR_EOBF); |
||
| 2722 | |||
| 2723 | /* Restore hsmartcard->RxState to Ready */ |
||
| 2724 | hsmartcard->RxState = HAL_SMARTCARD_STATE_READY; |
||
| 2725 | |||
| 2726 | #if (USE_HAL_SMARTCARD_REGISTER_CALLBACKS == 1) |
||
| 2727 | /* Call registered Abort Receive Complete Callback */ |
||
| 2728 | hsmartcard->AbortReceiveCpltCallback(hsmartcard); |
||
| 2729 | #else |
||
| 2730 | /* Call legacy weak Abort Receive Complete Callback */ |
||
| 2731 | HAL_SMARTCARD_AbortReceiveCpltCallback(hsmartcard); |
||
| 2732 | #endif /* USE_HAL_SMARTCARD_REGISTER_CALLBACK */ |
||
| 2733 | } |
||
| 2734 | |||
| 2735 | /** |
||
| 2736 | * @brief Send an amount of data in non-blocking mode. |
||
| 2737 | * @note Function called under interruption only, once |
||
| 2738 | * interruptions have been enabled by HAL_SMARTCARD_Transmit_IT(). |
||
| 2739 | * @param hsmartcard Pointer to a SMARTCARD_HandleTypeDef structure that contains |
||
| 2740 | * the configuration information for the specified SMARTCARD module. |
||
| 2741 | * @retval None |
||
| 2742 | */ |
||
| 2743 | static void SMARTCARD_TxISR(SMARTCARD_HandleTypeDef *hsmartcard) |
||
| 2744 | { |
||
| 2745 | /* Check that a Tx process is ongoing */ |
||
| 2746 | if (hsmartcard->gState == HAL_SMARTCARD_STATE_BUSY_TX) |
||
| 2747 | { |
||
| 2748 | if (hsmartcard->TxXferCount == 0U) |
||
| 2749 | { |
||
| 2750 | /* Disable the SMARTCARD Transmit Data Register Empty Interrupt */ |
||
| 2751 | CLEAR_BIT(hsmartcard->Instance->CR1, USART_CR1_TXEIE); |
||
| 2752 | |||
| 2753 | /* Enable the SMARTCARD Transmit Complete Interrupt */ |
||
| 2754 | __HAL_SMARTCARD_ENABLE_IT(hsmartcard, hsmartcard->AdvancedInit.TxCompletionIndication); |
||
| 2755 | } |
||
| 2756 | else |
||
| 2757 | { |
||
| 2758 | hsmartcard->Instance->TDR = (uint8_t)(*hsmartcard->pTxBuffPtr & 0xFFU); |
||
| 2759 | hsmartcard->pTxBuffPtr++; |
||
| 2760 | hsmartcard->TxXferCount--; |
||
| 2761 | } |
||
| 2762 | } |
||
| 2763 | } |
||
| 2764 | |||
| 2765 | /** |
||
| 2766 | * @brief Wrap up transmission in non-blocking mode. |
||
| 2767 | * @param hsmartcard Pointer to a SMARTCARD_HandleTypeDef structure that contains |
||
| 2768 | * the configuration information for the specified SMARTCARD module. |
||
| 2769 | * @retval None |
||
| 2770 | */ |
||
| 2771 | static void SMARTCARD_EndTransmit_IT(SMARTCARD_HandleTypeDef *hsmartcard) |
||
| 2772 | { |
||
| 2773 | /* Disable the SMARTCARD Transmit Complete Interrupt */ |
||
| 2774 | __HAL_SMARTCARD_DISABLE_IT(hsmartcard, hsmartcard->AdvancedInit.TxCompletionIndication); |
||
| 2775 | |||
| 2776 | /* Check if a receive process is ongoing or not. If not disable ERR IT */ |
||
| 2777 | if (hsmartcard->RxState == HAL_SMARTCARD_STATE_READY) |
||
| 2778 | { |
||
| 2779 | /* Disable the SMARTCARD Error Interrupt: (Frame error) */ |
||
| 2780 | CLEAR_BIT(hsmartcard->Instance->CR3, USART_CR3_EIE); |
||
| 2781 | } |
||
| 2782 | |||
| 2783 | /* Re-enable Rx at end of transmission if initial mode is Rx/Tx */ |
||
| 2784 | if (hsmartcard->Init.Mode == SMARTCARD_MODE_TX_RX) |
||
| 2785 | { |
||
| 2786 | /* Disable the Peripheral first to update modes */ |
||
| 2787 | CLEAR_BIT(hsmartcard->Instance->CR1, USART_CR1_UE); |
||
| 2788 | SET_BIT(hsmartcard->Instance->CR1, USART_CR1_RE); |
||
| 2789 | /* Enable the Peripheral */ |
||
| 2790 | SET_BIT(hsmartcard->Instance->CR1, USART_CR1_UE); |
||
| 2791 | } |
||
| 2792 | |||
| 2793 | /* Tx process is ended, restore hsmartcard->gState to Ready */ |
||
| 2794 | hsmartcard->gState = HAL_SMARTCARD_STATE_READY; |
||
| 2795 | |||
| 2796 | /* Clear TxISR function pointer */ |
||
| 2797 | hsmartcard->TxISR = NULL; |
||
| 2798 | |||
| 2799 | #if (USE_HAL_SMARTCARD_REGISTER_CALLBACKS == 1) |
||
| 2800 | /* Call registered Tx complete callback */ |
||
| 2801 | hsmartcard->TxCpltCallback(hsmartcard); |
||
| 2802 | #else |
||
| 2803 | /* Call legacy weak Tx complete callback */ |
||
| 2804 | HAL_SMARTCARD_TxCpltCallback(hsmartcard); |
||
| 2805 | #endif /* USE_HAL_SMARTCARD_REGISTER_CALLBACK */ |
||
| 2806 | } |
||
| 2807 | |||
| 2808 | /** |
||
| 2809 | * @brief Receive an amount of data in non-blocking mode. |
||
| 2810 | * @note Function called under interruption only, once |
||
| 2811 | * interruptions have been enabled by HAL_SMARTCARD_Receive_IT(). |
||
| 2812 | * @param hsmartcard Pointer to a SMARTCARD_HandleTypeDef structure that contains |
||
| 2813 | * the configuration information for the specified SMARTCARD module. |
||
| 2814 | * @retval None |
||
| 2815 | */ |
||
| 2816 | static void SMARTCARD_RxISR(SMARTCARD_HandleTypeDef *hsmartcard) |
||
| 2817 | { |
||
| 2818 | /* Check that a Rx process is ongoing */ |
||
| 2819 | if (hsmartcard->RxState == HAL_SMARTCARD_STATE_BUSY_RX) |
||
| 2820 | { |
||
| 2821 | *hsmartcard->pRxBuffPtr = (uint8_t)(hsmartcard->Instance->RDR & (uint8_t)0xFF); |
||
| 2822 | hsmartcard->pRxBuffPtr++; |
||
| 2823 | |||
| 2824 | hsmartcard->RxXferCount--; |
||
| 2825 | if (hsmartcard->RxXferCount == 0U) |
||
| 2826 | { |
||
| 2827 | CLEAR_BIT(hsmartcard->Instance->CR1, USART_CR1_RXNEIE); |
||
| 2828 | |||
| 2829 | /* Check if a transmit process is ongoing or not. If not disable ERR IT */ |
||
| 2830 | if (hsmartcard->gState == HAL_SMARTCARD_STATE_READY) |
||
| 2831 | { |
||
| 2832 | /* Disable the SMARTCARD Error Interrupt: (Frame error, noise error, overrun error) */ |
||
| 2833 | CLEAR_BIT(hsmartcard->Instance->CR3, USART_CR3_EIE); |
||
| 2834 | } |
||
| 2835 | |||
| 2836 | /* Disable the SMARTCARD Parity Error Interrupt */ |
||
| 2837 | CLEAR_BIT(hsmartcard->Instance->CR1, USART_CR1_PEIE); |
||
| 2838 | |||
| 2839 | hsmartcard->RxState = HAL_SMARTCARD_STATE_READY; |
||
| 2840 | |||
| 2841 | /* Clear RxISR function pointer */ |
||
| 2842 | hsmartcard->RxISR = NULL; |
||
| 2843 | |||
| 2844 | #if (USE_HAL_SMARTCARD_REGISTER_CALLBACKS == 1) |
||
| 2845 | /* Call registered Rx complete callback */ |
||
| 2846 | hsmartcard->RxCpltCallback(hsmartcard); |
||
| 2847 | #else |
||
| 2848 | /* Call legacy weak Rx complete callback */ |
||
| 2849 | HAL_SMARTCARD_RxCpltCallback(hsmartcard); |
||
| 2850 | #endif /* USE_HAL_SMARTCARD_REGISTER_CALLBACK */ |
||
| 2851 | } |
||
| 2852 | } |
||
| 2853 | else |
||
| 2854 | { |
||
| 2855 | /* Clear RXNE interrupt flag */ |
||
| 2856 | __HAL_SMARTCARD_SEND_REQ(hsmartcard, SMARTCARD_RXDATA_FLUSH_REQUEST); |
||
| 2857 | } |
||
| 2858 | } |
||
| 2859 | |||
| 2860 | /** |
||
| 2861 | * @} |
||
| 2862 | */ |
||
| 2863 | |||
| 2864 | #endif /* HAL_SMARTCARD_MODULE_ENABLED */ |
||
| 2865 | /** |
||
| 2866 | * @} |
||
| 2867 | */ |
||
| 2868 | |||
| 2869 | /** |
||
| 2870 | * @} |
||
| 2871 | */ |
||
| 2872 | #endif /* !defined(STM32F030x6) && !defined(STM32F030x8) && !defined(STM32F070x6) && !defined(STM32F070xB) && !defined(STM32F030xC) */ |
||
| 2873 | /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/ |