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