[303] | 1 | /**
|
---|
| 2 | ******************************************************************************
|
---|
| 3 | * @file stm32f4xx_hal_smartcard.c
|
---|
| 4 | * @author MCD Application Team
|
---|
| 5 | * @version V1.4.1
|
---|
| 6 | * @date 09-October-2015
|
---|
| 7 | * @brief SMARTCARD HAL module driver.
|
---|
| 8 | * This file provides firmware functions to manage the following
|
---|
| 9 | * functionalities of the SMARTCARD peripheral:
|
---|
| 10 | * + Initialization and de-initialization functions
|
---|
| 11 | * + IO operation functions
|
---|
| 12 | * + Peripheral State and Errors functions
|
---|
| 13 | *
|
---|
| 14 | @verbatim
|
---|
| 15 | ==============================================================================
|
---|
| 16 | ##### How to use this driver #####
|
---|
| 17 | ==============================================================================
|
---|
| 18 | [..]
|
---|
| 19 | The SMARTCARD HAL driver can be used as follows:
|
---|
| 20 |
|
---|
| 21 | (#) Declare a SMARTCARD_HandleTypeDef handle structure.
|
---|
| 22 | (#) Initialize the SMARTCARD low level resources by implementing the HAL_SMARTCARD_MspInit() API:
|
---|
| 23 | (##) Enable the USARTx interface clock.
|
---|
| 24 | (##) SMARTCARD pins configuration:
|
---|
| 25 | (+++) Enable the clock for the SMARTCARD GPIOs.
|
---|
| 26 | (+++) Configure these SMARTCARD pins as alternate function pull-up.
|
---|
| 27 | (##) NVIC configuration if you need to use interrupt process (HAL_SMARTCARD_Transmit_IT()
|
---|
| 28 | and HAL_SMARTCARD_Receive_IT() APIs):
|
---|
| 29 | (+++) Configure the USARTx interrupt priority.
|
---|
| 30 | (+++) Enable the NVIC USART IRQ handle.
|
---|
| 31 | (##) DMA Configuration if you need to use DMA process (HAL_SMARTCARD_Transmit_DMA()
|
---|
| 32 | and HAL_SMARTCARD_Receive_DMA() APIs):
|
---|
| 33 | (+++) Declare a DMA handle structure for the Tx/Rx stream.
|
---|
| 34 | (+++) Enable the DMAx interface clock.
|
---|
| 35 | (+++) Configure the declared DMA handle structure with the required Tx/Rx parameters.
|
---|
| 36 | (+++) Configure the DMA Tx/Rx Stream.
|
---|
| 37 | (+++) Associate the initialized DMA handle to the SMARTCARD DMA Tx/Rx handle.
|
---|
| 38 | (+++) Configure the priority and enable the NVIC for the transfer complete interrupt on the DMA Tx/Rx Stream.
|
---|
| 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 |
|
---|
| 60 | *** Interrupt mode IO operation ***
|
---|
| 61 | ===================================
|
---|
| 62 | [..]
|
---|
| 63 | (+) Send an amount of data in non blocking mode using HAL_SMARTCARD_Transmit_IT()
|
---|
| 64 | (+) At transmission end of transfer HAL_SMARTCARD_TxCpltCallback is executed and user can
|
---|
| 65 | add his own code by customization of function pointer HAL_SMARTCARD_TxCpltCallback
|
---|
| 66 | (+) Receive an amount of data in non blocking mode using HAL_SMARTCARD_Receive_IT()
|
---|
| 67 | (+) At reception end of transfer HAL_SMARTCARD_RxCpltCallback is executed and user can
|
---|
| 68 | add his own code by customization of function pointer HAL_SMARTCARD_RxCpltCallback
|
---|
| 69 | (+) In case of transfer Error, HAL_SMARTCARD_ErrorCallback() function is executed and user can
|
---|
| 70 | add his own code by customization of function pointer HAL_SMARTCARD_ErrorCallback
|
---|
| 71 |
|
---|
| 72 | *** DMA mode IO operation ***
|
---|
| 73 | ==============================
|
---|
| 74 | [..]
|
---|
| 75 | (+) Send an amount of data in non blocking mode (DMA) using HAL_SMARTCARD_Transmit_DMA()
|
---|
| 76 | (+) At transmission end of transfer HAL_SMARTCARD_TxCpltCallback is executed and user can
|
---|
| 77 | add his own code by customization of function pointer HAL_SMARTCARD_TxCpltCallback
|
---|
| 78 | (+) Receive an amount of data in non blocking mode (DMA) using HAL_SMARTCARD_Receive_DMA()
|
---|
| 79 | (+) At reception end of transfer HAL_SMARTCARD_RxCpltCallback is executed and user can
|
---|
| 80 | add his own code by customization of function pointer HAL_SMARTCARD_RxCpltCallback
|
---|
| 81 | (+) In case of transfer Error, HAL_SMARTCARD_ErrorCallback() function is executed and user can
|
---|
| 82 | add his own code by customization of function pointer HAL_SMARTCARD_ErrorCallback
|
---|
| 83 |
|
---|
| 84 | *** SMARTCARD HAL driver macros list ***
|
---|
| 85 | =============================================
|
---|
| 86 | [..]
|
---|
| 87 | Below the list of most used macros in SMARTCARD HAL driver.
|
---|
| 88 |
|
---|
| 89 | (+) __HAL_SMARTCARD_ENABLE: Enable the SMARTCARD peripheral
|
---|
| 90 | (+) __HAL_SMARTCARD_DISABLE: Disable the SMARTCARD peripheral
|
---|
| 91 | (+) __HAL_SMARTCARD_GET_FLAG : Check whether the specified SMARTCARD flag is set or not
|
---|
| 92 | (+) __HAL_SMARTCARD_CLEAR_FLAG : Clear the specified SMARTCARD pending flag
|
---|
| 93 | (+) __HAL_SMARTCARD_ENABLE_IT: Enable the specified SMARTCARD interrupt
|
---|
| 94 | (+) __HAL_SMARTCARD_DISABLE_IT: Disable the specified SMARTCARD interrupt
|
---|
| 95 |
|
---|
| 96 | [..]
|
---|
| 97 | (@) You can refer to the SMARTCARD HAL driver header file for more useful macros
|
---|
| 98 |
|
---|
| 99 | @endverbatim
|
---|
| 100 | ******************************************************************************
|
---|
| 101 | * @attention
|
---|
| 102 | *
|
---|
| 103 | * <h2><center>© COPYRIGHT(c) 2015 STMicroelectronics</center></h2>
|
---|
| 104 | *
|
---|
| 105 | * Redistribution and use in source and binary forms, with or without modification,
|
---|
| 106 | * are permitted provided that the following conditions are met:
|
---|
| 107 | * 1. Redistributions of source code must retain the above copyright notice,
|
---|
| 108 | * this list of conditions and the following disclaimer.
|
---|
| 109 | * 2. Redistributions in binary form must reproduce the above copyright notice,
|
---|
| 110 | * this list of conditions and the following disclaimer in the documentation
|
---|
| 111 | * and/or other materials provided with the distribution.
|
---|
| 112 | * 3. Neither the name of STMicroelectronics nor the names of its contributors
|
---|
| 113 | * may be used to endorse or promote products derived from this software
|
---|
| 114 | * without specific prior written permission.
|
---|
| 115 | *
|
---|
| 116 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
---|
| 117 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
---|
| 118 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
---|
| 119 | * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
|
---|
| 120 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
---|
| 121 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
---|
| 122 | * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
|
---|
| 123 | * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
---|
| 124 | * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
---|
| 125 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
---|
| 126 | *
|
---|
| 127 | ******************************************************************************
|
---|
| 128 | */
|
---|
| 129 |
|
---|
| 130 | /* Includes ------------------------------------------------------------------*/
|
---|
| 131 | #include "stm32f4xx_hal.h"
|
---|
| 132 |
|
---|
| 133 | /** @addtogroup STM32F4xx_HAL_Driver
|
---|
| 134 | * @{
|
---|
| 135 | */
|
---|
| 136 |
|
---|
| 137 | /** @defgroup SMARTCARD SMARTCARD
|
---|
| 138 | * @brief HAL USART SMARTCARD module driver
|
---|
| 139 | * @{
|
---|
| 140 | */
|
---|
| 141 | #ifdef HAL_SMARTCARD_MODULE_ENABLED
|
---|
| 142 | /* Private typedef -----------------------------------------------------------*/
|
---|
| 143 | /* Private define ------------------------------------------------------------*/
|
---|
| 144 | /** @addtogroup SMARTCARD_Private_Constants
|
---|
| 145 | * @{
|
---|
| 146 | */
|
---|
| 147 | #define SMARTCARD_TIMEOUT_VALUE 22000
|
---|
| 148 | /**
|
---|
| 149 | * @}
|
---|
| 150 | */
|
---|
| 151 | /* Private macro -------------------------------------------------------------*/
|
---|
| 152 | /* Private variables ---------------------------------------------------------*/
|
---|
| 153 | /* Private function prototypes -----------------------------------------------*/
|
---|
| 154 | /** @addtogroup SMARTCARD_Private_Functions
|
---|
| 155 | * @{
|
---|
| 156 | */
|
---|
| 157 | static void SMARTCARD_SetConfig (SMARTCARD_HandleTypeDef *hsc);
|
---|
| 158 | static HAL_StatusTypeDef SMARTCARD_Transmit_IT(SMARTCARD_HandleTypeDef *hsc);
|
---|
| 159 | static HAL_StatusTypeDef SMARTCARD_EndTransmit_IT(SMARTCARD_HandleTypeDef *hsmartcard);
|
---|
| 160 | static HAL_StatusTypeDef SMARTCARD_Receive_IT(SMARTCARD_HandleTypeDef *hsc);
|
---|
| 161 | static void SMARTCARD_DMATransmitCplt(DMA_HandleTypeDef *hdma);
|
---|
| 162 | static void SMARTCARD_DMAReceiveCplt(DMA_HandleTypeDef *hdma);
|
---|
| 163 | static void SMARTCARD_DMAError(DMA_HandleTypeDef *hdma);
|
---|
| 164 | static HAL_StatusTypeDef SMARTCARD_WaitOnFlagUntilTimeout(SMARTCARD_HandleTypeDef *hsc, uint32_t Flag, FlagStatus Status, uint32_t Timeout);
|
---|
| 165 | /**
|
---|
| 166 | * @}
|
---|
| 167 | */
|
---|
| 168 | /* Exported functions --------------------------------------------------------*/
|
---|
| 169 | /** @defgroup SMARTCARD_Exported_Functions SMARTCARD Exported Functions
|
---|
| 170 | * @{
|
---|
| 171 | */
|
---|
| 172 |
|
---|
| 173 | /** @defgroup SMARTCARD_Exported_Functions_Group1 SmartCard Initialization and de-initialization functions
|
---|
| 174 | * @brief Initialization and Configuration functions
|
---|
| 175 | *
|
---|
| 176 | @verbatim
|
---|
| 177 | ==============================================================================
|
---|
| 178 | ##### Initialization and Configuration functions #####
|
---|
| 179 | ==============================================================================
|
---|
| 180 | [..]
|
---|
| 181 | This subsection provides a set of functions allowing to initialize the USART
|
---|
| 182 | in Smartcard mode.
|
---|
| 183 | [..]
|
---|
| 184 | The Smartcard interface is designed to support asynchronous protocol Smartcards as
|
---|
| 185 | defined in the ISO 7816-3 standard.
|
---|
| 186 | [..]
|
---|
| 187 | The USART can provide a clock to the smartcard through the SCLK output.
|
---|
| 188 | In smartcard mode, SCLK is not associated to the communication but is simply derived
|
---|
| 189 | from the internal peripheral input clock through a 5-bit prescaler.
|
---|
| 190 | [..]
|
---|
| 191 | (+) For the Smartcard mode only these parameters can be configured:
|
---|
| 192 | (++) Baud Rate
|
---|
| 193 | (++) Word Length => Should be 9 bits (8 bits + parity)
|
---|
| 194 | (++) Stop Bit
|
---|
| 195 | (++) Parity: => Should be enabled
|
---|
| 196 | +-------------------------------------------------------------+
|
---|
| 197 | | M bit | PCE bit | SMARTCARD frame |
|
---|
| 198 | |---------------------|---------------------------------------|
|
---|
| 199 | | 1 | 1 | | SB | 8 bit data | PB | STB | |
|
---|
| 200 | +-------------------------------------------------------------+
|
---|
| 201 | (++) USART polarity
|
---|
| 202 | (++) USART phase
|
---|
| 203 | (++) USART LastBit
|
---|
| 204 | (++) Receiver/transmitter modes
|
---|
| 205 | (++) Prescaler
|
---|
| 206 | (++) GuardTime
|
---|
| 207 | (++) NACKState: The Smartcard NACK state
|
---|
| 208 |
|
---|
| 209 | (+) Recommended SmartCard interface configuration to get the Answer to Reset from the Card:
|
---|
| 210 | (++) Word Length = 9 Bits
|
---|
| 211 | (++) 1.5 Stop Bit
|
---|
| 212 | (++) Even parity
|
---|
| 213 | (++) BaudRate = 12096 baud
|
---|
| 214 | (++) Tx and Rx enabled
|
---|
| 215 | [..]
|
---|
| 216 | Please refer to the ISO 7816-3 specification for more details.
|
---|
| 217 |
|
---|
| 218 | -@- It is also possible to choose 0.5 stop bit for receiving but it is recommended
|
---|
| 219 | to use 1.5 stop bits for both transmitting and receiving to avoid switching
|
---|
| 220 | between the two configurations.
|
---|
| 221 | [..]
|
---|
| 222 | The HAL_SMARTCARD_Init() function follows the USART SmartCard configuration
|
---|
| 223 | procedure (details for the procedure are available in reference manual (RM0329)).
|
---|
| 224 |
|
---|
| 225 | @endverbatim
|
---|
| 226 | * @{
|
---|
| 227 | */
|
---|
| 228 |
|
---|
| 229 | /**
|
---|
| 230 | * @brief Initializes the SmartCard mode according to the specified
|
---|
| 231 | * parameters in the SMARTCARD_InitTypeDef and create the associated handle .
|
---|
| 232 | * @param hsc: pointer to a SMARTCARD_HandleTypeDef structure that contains
|
---|
| 233 | * the configuration information for SMARTCARD module.
|
---|
| 234 | * @retval HAL status
|
---|
| 235 | */
|
---|
| 236 | HAL_StatusTypeDef HAL_SMARTCARD_Init(SMARTCARD_HandleTypeDef *hsc)
|
---|
| 237 | {
|
---|
| 238 | /* Check the SMARTCARD handle allocation */
|
---|
| 239 | if(hsc == NULL)
|
---|
| 240 | {
|
---|
| 241 | return HAL_ERROR;
|
---|
| 242 | }
|
---|
| 243 |
|
---|
| 244 | /* Check the parameters */
|
---|
| 245 | assert_param(IS_SMARTCARD_INSTANCE(hsc->Instance));
|
---|
| 246 | assert_param(IS_SMARTCARD_NACK_STATE(hsc->Init.NACKState));
|
---|
| 247 |
|
---|
| 248 | if(hsc->State == HAL_SMARTCARD_STATE_RESET)
|
---|
| 249 | {
|
---|
| 250 | /* Allocate lock resource and initialize it */
|
---|
| 251 | hsc->Lock = HAL_UNLOCKED;
|
---|
| 252 | /* Init the low level hardware : GPIO, CLOCK, CORTEX...etc */
|
---|
| 253 | HAL_SMARTCARD_MspInit(hsc);
|
---|
| 254 | }
|
---|
| 255 |
|
---|
| 256 | hsc->State = HAL_SMARTCARD_STATE_BUSY;
|
---|
| 257 |
|
---|
| 258 | /* Set the Prescaler */
|
---|
| 259 | MODIFY_REG(hsc->Instance->GTPR, USART_GTPR_PSC, hsc->Init.Prescaler);
|
---|
| 260 |
|
---|
| 261 | /* Set the Guard Time */
|
---|
| 262 | MODIFY_REG(hsc->Instance->GTPR, USART_GTPR_GT, ((hsc->Init.GuardTime)<<8));
|
---|
| 263 |
|
---|
| 264 | /* Set the Smartcard Communication parameters */
|
---|
| 265 | SMARTCARD_SetConfig(hsc);
|
---|
| 266 |
|
---|
| 267 | /* In SmartCard mode, the following bits must be kept cleared:
|
---|
| 268 | - LINEN bit in the USART_CR2 register
|
---|
| 269 | - HDSEL and IREN bits in the USART_CR3 register.*/
|
---|
| 270 | hsc->Instance->CR2 &= ~USART_CR2_LINEN;
|
---|
| 271 | hsc->Instance->CR3 &= ~(USART_CR3_IREN | USART_CR3_HDSEL);
|
---|
| 272 |
|
---|
| 273 | /* Enable the SMARTCARD Parity Error Interrupt */
|
---|
| 274 | __HAL_SMARTCARD_ENABLE_IT(hsc, SMARTCARD_IT_PE);
|
---|
| 275 |
|
---|
| 276 | /* Enable the SMARTCARD Framing Error Interrupt */
|
---|
| 277 | __HAL_SMARTCARD_ENABLE_IT(hsc, SMARTCARD_IT_ERR);
|
---|
| 278 |
|
---|
| 279 | /* Enable the Peripheral */
|
---|
| 280 | __HAL_SMARTCARD_ENABLE(hsc);
|
---|
| 281 |
|
---|
| 282 | /* Configure the Smartcard NACK state */
|
---|
| 283 | MODIFY_REG(hsc->Instance->CR3, USART_CR3_NACK, hsc->Init.NACKState);
|
---|
| 284 |
|
---|
| 285 | /* Enable the SC mode by setting the SCEN bit in the CR3 register */
|
---|
| 286 | hsc->Instance->CR3 |= (USART_CR3_SCEN);
|
---|
| 287 |
|
---|
| 288 | /* Initialize the SMARTCARD state*/
|
---|
| 289 | hsc->ErrorCode = HAL_SMARTCARD_ERROR_NONE;
|
---|
| 290 | hsc->State= HAL_SMARTCARD_STATE_READY;
|
---|
| 291 |
|
---|
| 292 | return HAL_OK;
|
---|
| 293 | }
|
---|
| 294 |
|
---|
| 295 | /**
|
---|
| 296 | * @brief DeInitializes the USART SmartCard peripheral
|
---|
| 297 | * @param hsc: pointer to a SMARTCARD_HandleTypeDef structure that contains
|
---|
| 298 | * the configuration information for SMARTCARD module.
|
---|
| 299 | * @retval HAL status
|
---|
| 300 | */
|
---|
| 301 | HAL_StatusTypeDef HAL_SMARTCARD_DeInit(SMARTCARD_HandleTypeDef *hsc)
|
---|
| 302 | {
|
---|
| 303 | /* Check the SMARTCARD handle allocation */
|
---|
| 304 | if(hsc == NULL)
|
---|
| 305 | {
|
---|
| 306 | return HAL_ERROR;
|
---|
| 307 | }
|
---|
| 308 |
|
---|
| 309 | /* Check the parameters */
|
---|
| 310 | assert_param(IS_SMARTCARD_INSTANCE(hsc->Instance));
|
---|
| 311 |
|
---|
| 312 | hsc->State = HAL_SMARTCARD_STATE_BUSY;
|
---|
| 313 |
|
---|
| 314 | /* Disable the Peripheral */
|
---|
| 315 | __HAL_SMARTCARD_DISABLE(hsc);
|
---|
| 316 |
|
---|
| 317 | /* DeInit the low level hardware */
|
---|
| 318 | HAL_SMARTCARD_MspDeInit(hsc);
|
---|
| 319 |
|
---|
| 320 | hsc->ErrorCode = HAL_SMARTCARD_ERROR_NONE;
|
---|
| 321 | hsc->State = HAL_SMARTCARD_STATE_RESET;
|
---|
| 322 |
|
---|
| 323 | /* Release Lock */
|
---|
| 324 | __HAL_UNLOCK(hsc);
|
---|
| 325 |
|
---|
| 326 | return HAL_OK;
|
---|
| 327 | }
|
---|
| 328 |
|
---|
| 329 | /**
|
---|
| 330 | * @brief SMARTCARD MSP Init
|
---|
| 331 | * @param hsc: pointer to a SMARTCARD_HandleTypeDef structure that contains
|
---|
| 332 | * the configuration information for SMARTCARD module.
|
---|
| 333 | * @retval None
|
---|
| 334 | */
|
---|
| 335 | __weak void HAL_SMARTCARD_MspInit(SMARTCARD_HandleTypeDef *hsc)
|
---|
| 336 | {
|
---|
| 337 | /* NOTE : This function Should not be modified, when the callback is needed,
|
---|
| 338 | the HAL_SMARTCARD_MspInit could be implemented in the user file
|
---|
| 339 | */
|
---|
| 340 | }
|
---|
| 341 |
|
---|
| 342 | /**
|
---|
| 343 | * @brief SMARTCARD MSP DeInit
|
---|
| 344 | * @param hsc: pointer to a SMARTCARD_HandleTypeDef structure that contains
|
---|
| 345 | * the configuration information for SMARTCARD module.
|
---|
| 346 | * @retval None
|
---|
| 347 | */
|
---|
| 348 | __weak void HAL_SMARTCARD_MspDeInit(SMARTCARD_HandleTypeDef *hsc)
|
---|
| 349 | {
|
---|
| 350 | /* NOTE : This function Should not be modified, when the callback is needed,
|
---|
| 351 | the HAL_SMARTCARD_MspDeInit could be implemented in the user file
|
---|
| 352 | */
|
---|
| 353 | }
|
---|
| 354 |
|
---|
| 355 | /**
|
---|
| 356 | * @}
|
---|
| 357 | */
|
---|
| 358 |
|
---|
| 359 | /** @defgroup SMARTCARD_Exported_Functions_Group2 IO operation functions
|
---|
| 360 | * @brief SMARTCARD Transmit and Receive functions
|
---|
| 361 | *
|
---|
| 362 | @verbatim
|
---|
| 363 | ===============================================================================
|
---|
| 364 | ##### IO operation functions #####
|
---|
| 365 | ===============================================================================
|
---|
| 366 | This subsection provides a set of functions allowing to manage the SMARTCARD data transfers.
|
---|
| 367 | [..]
|
---|
| 368 | Smartcard is a single wire half duplex communication protocol.
|
---|
| 369 | The Smartcard interface is designed to support asynchronous protocol Smartcards as
|
---|
| 370 | defined in the ISO 7816-3 standard. The USART should be configured as:
|
---|
| 371 | (+) 8 bits plus parity: where M=1 and PCE=1 in the USART_CR1 register
|
---|
| 372 | (+) 1.5 stop bits when transmitting and receiving: where STOP=11 in the USART_CR2 register.
|
---|
| 373 |
|
---|
| 374 | (#) There are two modes of transfer:
|
---|
| 375 | (++) Blocking mode: The communication is performed in polling mode.
|
---|
| 376 | The HAL status of all data processing is returned by the same function
|
---|
| 377 | after finishing transfer.
|
---|
| 378 | (++) Non Blocking mode: The communication is performed using Interrupts
|
---|
| 379 | or DMA, These APIs return the HAL status.
|
---|
| 380 | The end of the data processing will be indicated through the
|
---|
| 381 | dedicated SMARTCARD IRQ when using Interrupt mode or the DMA IRQ when
|
---|
| 382 | using DMA mode.
|
---|
| 383 | The HAL_SMARTCARD_TxCpltCallback(), HAL_SMARTCARD_RxCpltCallback() user callbacks
|
---|
| 384 | will be executed respectively at the end of the Transmit or Receive process
|
---|
| 385 | The HAL_SMARTCARD_ErrorCallback() user callback will be executed when a communication error is detected
|
---|
| 386 |
|
---|
| 387 | (#) Blocking mode APIs are :
|
---|
| 388 | (++) HAL_SMARTCARD_Transmit()
|
---|
| 389 | (++) HAL_SMARTCARD_Receive()
|
---|
| 390 |
|
---|
| 391 | (#) Non Blocking mode APIs with Interrupt are :
|
---|
| 392 | (++) HAL_SMARTCARD_Transmit_IT()
|
---|
| 393 | (++) HAL_SMARTCARD_Receive_IT()
|
---|
| 394 | (++) HAL_SMARTCARD_IRQHandler()
|
---|
| 395 |
|
---|
| 396 | (#) Non Blocking mode functions with DMA are :
|
---|
| 397 | (++) HAL_SMARTCARD_Transmit_DMA()
|
---|
| 398 | (++) HAL_SMARTCARD_Receive_DMA()
|
---|
| 399 |
|
---|
| 400 | (#) A set of Transfer Complete Callbacks are provided in non Blocking mode:
|
---|
| 401 | (++) HAL_SMARTCARD_TxCpltCallback()
|
---|
| 402 | (++) HAL_SMARTCARD_RxCpltCallback()
|
---|
| 403 | (++) HAL_SMARTCARD_ErrorCallback()
|
---|
| 404 |
|
---|
| 405 | @endverbatim
|
---|
| 406 | * @{
|
---|
| 407 | */
|
---|
| 408 |
|
---|
| 409 | /**
|
---|
| 410 | * @brief Send an amount of data in blocking mode
|
---|
| 411 | * @param hsc: pointer to a SMARTCARD_HandleTypeDef structure that contains
|
---|
| 412 | * the configuration information for SMARTCARD module.
|
---|
| 413 | * @param pData: pointer to data buffer
|
---|
| 414 | * @param Size: amount of data to be sent
|
---|
| 415 | * @param Timeout: Timeout duration
|
---|
| 416 | * @retval HAL status
|
---|
| 417 | */
|
---|
| 418 | HAL_StatusTypeDef HAL_SMARTCARD_Transmit(SMARTCARD_HandleTypeDef *hsc, uint8_t *pData, uint16_t Size, uint32_t Timeout)
|
---|
| 419 | {
|
---|
| 420 | uint16_t* tmp;
|
---|
| 421 | uint32_t tmp1 = 0;
|
---|
| 422 |
|
---|
| 423 | tmp1 = hsc->State;
|
---|
| 424 | if((tmp1 == HAL_SMARTCARD_STATE_READY) || (tmp1 == HAL_SMARTCARD_STATE_BUSY_RX))
|
---|
| 425 | {
|
---|
| 426 | if((pData == NULL) || (Size == 0))
|
---|
| 427 | {
|
---|
| 428 | return HAL_ERROR;
|
---|
| 429 | }
|
---|
| 430 |
|
---|
| 431 | /* Process Locked */
|
---|
| 432 | __HAL_LOCK(hsc);
|
---|
| 433 |
|
---|
| 434 | hsc->ErrorCode = HAL_SMARTCARD_ERROR_NONE;
|
---|
| 435 | /* Check if a non-blocking receive process is ongoing or not */
|
---|
| 436 | if(hsc->State == HAL_SMARTCARD_STATE_BUSY_RX)
|
---|
| 437 | {
|
---|
| 438 | hsc->State = HAL_SMARTCARD_STATE_BUSY_TX_RX;
|
---|
| 439 | }
|
---|
| 440 | else
|
---|
| 441 | {
|
---|
| 442 | hsc->State = HAL_SMARTCARD_STATE_BUSY_TX;
|
---|
| 443 | }
|
---|
| 444 |
|
---|
| 445 | hsc->TxXferSize = Size;
|
---|
| 446 | hsc->TxXferCount = Size;
|
---|
| 447 | while(hsc->TxXferCount > 0)
|
---|
| 448 | {
|
---|
| 449 | hsc->TxXferCount--;
|
---|
| 450 | if(SMARTCARD_WaitOnFlagUntilTimeout(hsc, SMARTCARD_FLAG_TXE, RESET, Timeout) != HAL_OK)
|
---|
| 451 | {
|
---|
| 452 | return HAL_TIMEOUT;
|
---|
| 453 | }
|
---|
| 454 | tmp = (uint16_t*) pData;
|
---|
| 455 | hsc->Instance->DR = (*tmp & (uint16_t)0x01FF);
|
---|
| 456 | pData +=1;
|
---|
| 457 | }
|
---|
| 458 |
|
---|
| 459 | if(SMARTCARD_WaitOnFlagUntilTimeout(hsc, SMARTCARD_FLAG_TC, RESET, Timeout) != HAL_OK)
|
---|
| 460 | {
|
---|
| 461 | return HAL_TIMEOUT;
|
---|
| 462 | }
|
---|
| 463 |
|
---|
| 464 | /* Check if a non-blocking receive process is ongoing or not */
|
---|
| 465 | if(hsc->State == HAL_SMARTCARD_STATE_BUSY_TX_RX)
|
---|
| 466 | {
|
---|
| 467 | hsc->State = HAL_SMARTCARD_STATE_BUSY_RX;
|
---|
| 468 | }
|
---|
| 469 | else
|
---|
| 470 | {
|
---|
| 471 | hsc->State = HAL_SMARTCARD_STATE_READY;
|
---|
| 472 | }
|
---|
| 473 | /* Process Unlocked */
|
---|
| 474 | __HAL_UNLOCK(hsc);
|
---|
| 475 |
|
---|
| 476 | return HAL_OK;
|
---|
| 477 | }
|
---|
| 478 | else
|
---|
| 479 | {
|
---|
| 480 | return HAL_BUSY;
|
---|
| 481 | }
|
---|
| 482 | }
|
---|
| 483 |
|
---|
| 484 | /**
|
---|
| 485 | * @brief Receive an amount of data in blocking mode
|
---|
| 486 | * @param hsc: pointer to a SMARTCARD_HandleTypeDef structure that contains
|
---|
| 487 | * the configuration information for SMARTCARD module.
|
---|
| 488 | * @param pData: pointer to data buffer
|
---|
| 489 | * @param Size: amount of data to be received
|
---|
| 490 | * @param Timeout: Timeout duration
|
---|
| 491 | * @retval HAL status
|
---|
| 492 | */
|
---|
| 493 | HAL_StatusTypeDef HAL_SMARTCARD_Receive(SMARTCARD_HandleTypeDef *hsc, uint8_t *pData, uint16_t Size, uint32_t Timeout)
|
---|
| 494 | {
|
---|
| 495 | uint16_t* tmp;
|
---|
| 496 | uint32_t tmp1 = 0;
|
---|
| 497 |
|
---|
| 498 | tmp1 = hsc->State;
|
---|
| 499 | if((tmp1 == HAL_SMARTCARD_STATE_READY) || (tmp1 == HAL_SMARTCARD_STATE_BUSY_TX))
|
---|
| 500 | {
|
---|
| 501 | if((pData == NULL) || (Size == 0))
|
---|
| 502 | {
|
---|
| 503 | return HAL_ERROR;
|
---|
| 504 | }
|
---|
| 505 |
|
---|
| 506 | /* Process Locked */
|
---|
| 507 | __HAL_LOCK(hsc);
|
---|
| 508 |
|
---|
| 509 | hsc->ErrorCode = HAL_SMARTCARD_ERROR_NONE;
|
---|
| 510 |
|
---|
| 511 | /* Check if a non-blocking transmit process is ongoing or not */
|
---|
| 512 | if(hsc->State == HAL_SMARTCARD_STATE_BUSY_TX)
|
---|
| 513 | {
|
---|
| 514 | hsc->State = HAL_SMARTCARD_STATE_BUSY_TX_RX;
|
---|
| 515 | }
|
---|
| 516 | else
|
---|
| 517 | {
|
---|
| 518 | hsc->State = HAL_SMARTCARD_STATE_BUSY_RX;
|
---|
| 519 | }
|
---|
| 520 |
|
---|
| 521 | hsc->RxXferSize = Size;
|
---|
| 522 | hsc->RxXferCount = Size;
|
---|
| 523 |
|
---|
| 524 | /* Check the remain data to be received */
|
---|
| 525 | while(hsc->RxXferCount > 0)
|
---|
| 526 | {
|
---|
| 527 | hsc->RxXferCount--;
|
---|
| 528 | if(SMARTCARD_WaitOnFlagUntilTimeout(hsc, SMARTCARD_FLAG_RXNE, RESET, Timeout) != HAL_OK)
|
---|
| 529 | {
|
---|
| 530 | return HAL_TIMEOUT;
|
---|
| 531 | }
|
---|
| 532 | tmp = (uint16_t*) pData;
|
---|
| 533 | *tmp = (uint16_t)(hsc->Instance->DR & (uint16_t)0x00FF);
|
---|
| 534 | pData +=1;
|
---|
| 535 | }
|
---|
| 536 |
|
---|
| 537 | /* Check if a non-blocking transmit process is ongoing or not */
|
---|
| 538 | if(hsc->State == HAL_SMARTCARD_STATE_BUSY_TX_RX)
|
---|
| 539 | {
|
---|
| 540 | hsc->State = HAL_SMARTCARD_STATE_BUSY_TX;
|
---|
| 541 | }
|
---|
| 542 | else
|
---|
| 543 | {
|
---|
| 544 | hsc->State = HAL_SMARTCARD_STATE_READY;
|
---|
| 545 | }
|
---|
| 546 |
|
---|
| 547 | /* Process Unlocked */
|
---|
| 548 | __HAL_UNLOCK(hsc);
|
---|
| 549 |
|
---|
| 550 | return HAL_OK;
|
---|
| 551 | }
|
---|
| 552 | else
|
---|
| 553 | {
|
---|
| 554 | return HAL_BUSY;
|
---|
| 555 | }
|
---|
| 556 | }
|
---|
| 557 |
|
---|
| 558 | /**
|
---|
| 559 | * @brief Send an amount of data in non blocking mode
|
---|
| 560 | * @param hsc: pointer to a SMARTCARD_HandleTypeDef structure that contains
|
---|
| 561 | * the configuration information for SMARTCARD module.
|
---|
| 562 | * @param pData: pointer to data buffer
|
---|
| 563 | * @param Size: amount of data to be sent
|
---|
| 564 | * @retval HAL status
|
---|
| 565 | */
|
---|
| 566 | HAL_StatusTypeDef HAL_SMARTCARD_Transmit_IT(SMARTCARD_HandleTypeDef *hsc, uint8_t *pData, uint16_t Size)
|
---|
| 567 | {
|
---|
| 568 | uint32_t tmp1 = 0;
|
---|
| 569 |
|
---|
| 570 | tmp1 = hsc->State;
|
---|
| 571 | if((tmp1 == HAL_SMARTCARD_STATE_READY) || (tmp1 == HAL_SMARTCARD_STATE_BUSY_RX))
|
---|
| 572 | {
|
---|
| 573 | if((pData == NULL) || (Size == 0))
|
---|
| 574 | {
|
---|
| 575 | return HAL_ERROR;
|
---|
| 576 | }
|
---|
| 577 |
|
---|
| 578 | /* Process Locked */
|
---|
| 579 | __HAL_LOCK(hsc);
|
---|
| 580 |
|
---|
| 581 | hsc->pTxBuffPtr = pData;
|
---|
| 582 | hsc->TxXferSize = Size;
|
---|
| 583 | hsc->TxXferCount = Size;
|
---|
| 584 |
|
---|
| 585 | hsc->ErrorCode = HAL_SMARTCARD_ERROR_NONE;
|
---|
| 586 | /* Check if a non-blocking receive process is ongoing or not */
|
---|
| 587 | if(hsc->State == HAL_SMARTCARD_STATE_BUSY_RX)
|
---|
| 588 | {
|
---|
| 589 | hsc->State = HAL_SMARTCARD_STATE_BUSY_TX_RX;
|
---|
| 590 | }
|
---|
| 591 | else
|
---|
| 592 | {
|
---|
| 593 | hsc->State = HAL_SMARTCARD_STATE_BUSY_TX;
|
---|
| 594 | }
|
---|
| 595 |
|
---|
| 596 | /* Process Unlocked */
|
---|
| 597 | __HAL_UNLOCK(hsc);
|
---|
| 598 |
|
---|
| 599 | /* Enable the SMARTCARD Parity Error Interrupt */
|
---|
| 600 | __HAL_SMARTCARD_ENABLE_IT(hsc, SMARTCARD_IT_PE);
|
---|
| 601 |
|
---|
| 602 | /* Disable the SMARTCARD Error Interrupt: (Frame error, noise error, overrun error) */
|
---|
| 603 | __HAL_SMARTCARD_DISABLE_IT(hsc, SMARTCARD_IT_ERR);
|
---|
| 604 |
|
---|
| 605 | /* Enable the SMARTCARD Transmit data register empty Interrupt */
|
---|
| 606 | __HAL_SMARTCARD_ENABLE_IT(hsc, SMARTCARD_IT_TXE);
|
---|
| 607 |
|
---|
| 608 | return HAL_OK;
|
---|
| 609 | }
|
---|
| 610 | else
|
---|
| 611 | {
|
---|
| 612 | return HAL_BUSY;
|
---|
| 613 | }
|
---|
| 614 | }
|
---|
| 615 |
|
---|
| 616 | /**
|
---|
| 617 | * @brief Receive an amount of data in non blocking mode
|
---|
| 618 | * @param hsc: pointer to a SMARTCARD_HandleTypeDef structure that contains
|
---|
| 619 | * the configuration information for SMARTCARD module.
|
---|
| 620 | * @param pData: pointer to data buffer
|
---|
| 621 | * @param Size: amount of data to be received
|
---|
| 622 | * @retval HAL status
|
---|
| 623 | */
|
---|
| 624 | HAL_StatusTypeDef HAL_SMARTCARD_Receive_IT(SMARTCARD_HandleTypeDef *hsc, uint8_t *pData, uint16_t Size)
|
---|
| 625 | {
|
---|
| 626 | uint32_t tmp1 = 0;
|
---|
| 627 |
|
---|
| 628 | tmp1 = hsc->State;
|
---|
| 629 | if((tmp1 == HAL_SMARTCARD_STATE_READY) || (tmp1 == HAL_SMARTCARD_STATE_BUSY_TX))
|
---|
| 630 | {
|
---|
| 631 | if((pData == NULL) || (Size == 0))
|
---|
| 632 | {
|
---|
| 633 | return HAL_ERROR;
|
---|
| 634 | }
|
---|
| 635 |
|
---|
| 636 | /* Process Locked */
|
---|
| 637 | __HAL_LOCK(hsc);
|
---|
| 638 |
|
---|
| 639 | hsc->pRxBuffPtr = pData;
|
---|
| 640 | hsc->RxXferSize = Size;
|
---|
| 641 | hsc->RxXferCount = Size;
|
---|
| 642 |
|
---|
| 643 | hsc->ErrorCode = HAL_SMARTCARD_ERROR_NONE;
|
---|
| 644 | /* Check if a non-blocking transmit process is ongoing or not */
|
---|
| 645 | if(hsc->State == HAL_SMARTCARD_STATE_BUSY_TX)
|
---|
| 646 | {
|
---|
| 647 | hsc->State = HAL_SMARTCARD_STATE_BUSY_TX_RX;
|
---|
| 648 | }
|
---|
| 649 | else
|
---|
| 650 | {
|
---|
| 651 | hsc->State = HAL_SMARTCARD_STATE_BUSY_RX;
|
---|
| 652 | }
|
---|
| 653 | /* Process Unlocked */
|
---|
| 654 | __HAL_UNLOCK(hsc);
|
---|
| 655 |
|
---|
| 656 | /* Enable the SMARTCARD Data Register not empty Interrupt */
|
---|
| 657 | __HAL_SMARTCARD_ENABLE_IT(hsc, SMARTCARD_IT_RXNE);
|
---|
| 658 |
|
---|
| 659 | /* Enable the SMARTCARD Parity Error Interrupt */
|
---|
| 660 | __HAL_SMARTCARD_ENABLE_IT(hsc, SMARTCARD_IT_PE);
|
---|
| 661 |
|
---|
| 662 | /* Enable the SMARTCARD Error Interrupt: (Frame error, noise error, overrun error) */
|
---|
| 663 | __HAL_SMARTCARD_ENABLE_IT(hsc, SMARTCARD_IT_ERR);
|
---|
| 664 |
|
---|
| 665 | return HAL_OK;
|
---|
| 666 | }
|
---|
| 667 | else
|
---|
| 668 | {
|
---|
| 669 | return HAL_BUSY;
|
---|
| 670 | }
|
---|
| 671 | }
|
---|
| 672 |
|
---|
| 673 | /**
|
---|
| 674 | * @brief Send an amount of data in non blocking mode
|
---|
| 675 | * @param hsc: pointer to a SMARTCARD_HandleTypeDef structure that contains
|
---|
| 676 | * the configuration information for SMARTCARD module.
|
---|
| 677 | * @param pData: pointer to data buffer
|
---|
| 678 | * @param Size: amount of data to be sent
|
---|
| 679 | * @retval HAL status
|
---|
| 680 | */
|
---|
| 681 | HAL_StatusTypeDef HAL_SMARTCARD_Transmit_DMA(SMARTCARD_HandleTypeDef *hsc, uint8_t *pData, uint16_t Size)
|
---|
| 682 | {
|
---|
| 683 | uint32_t *tmp;
|
---|
| 684 | uint32_t tmp1 = 0;
|
---|
| 685 |
|
---|
| 686 | tmp1 = hsc->State;
|
---|
| 687 | if((tmp1 == HAL_SMARTCARD_STATE_READY) || (tmp1 == HAL_SMARTCARD_STATE_BUSY_RX))
|
---|
| 688 | {
|
---|
| 689 | if((pData == NULL) || (Size == 0))
|
---|
| 690 | {
|
---|
| 691 | return HAL_ERROR;
|
---|
| 692 | }
|
---|
| 693 |
|
---|
| 694 | /* Process Locked */
|
---|
| 695 | __HAL_LOCK(hsc);
|
---|
| 696 |
|
---|
| 697 | hsc->pTxBuffPtr = pData;
|
---|
| 698 | hsc->TxXferSize = Size;
|
---|
| 699 | hsc->TxXferCount = Size;
|
---|
| 700 |
|
---|
| 701 | hsc->ErrorCode = HAL_SMARTCARD_ERROR_NONE;
|
---|
| 702 | /* Check if a non-blocking receive process is ongoing or not */
|
---|
| 703 | if(hsc->State == HAL_SMARTCARD_STATE_BUSY_RX)
|
---|
| 704 | {
|
---|
| 705 | hsc->State = HAL_SMARTCARD_STATE_BUSY_TX_RX;
|
---|
| 706 | }
|
---|
| 707 | else
|
---|
| 708 | {
|
---|
| 709 | hsc->State = HAL_SMARTCARD_STATE_BUSY_TX;
|
---|
| 710 | }
|
---|
| 711 |
|
---|
| 712 | /* Set the SMARTCARD DMA transfer complete callback */
|
---|
| 713 | hsc->hdmatx->XferCpltCallback = SMARTCARD_DMATransmitCplt;
|
---|
| 714 |
|
---|
| 715 | /* Set the DMA error callback */
|
---|
| 716 | hsc->hdmatx->XferErrorCallback = SMARTCARD_DMAError;
|
---|
| 717 |
|
---|
| 718 | /* Enable the SMARTCARD transmit DMA Stream */
|
---|
| 719 | tmp = (uint32_t*)&pData;
|
---|
| 720 | HAL_DMA_Start_IT(hsc->hdmatx, *(uint32_t*)tmp, (uint32_t)&hsc->Instance->DR, Size);
|
---|
| 721 |
|
---|
| 722 | /* Clear the TC flag in the SR register by writing 0 to it */
|
---|
| 723 | __HAL_SMARTCARD_CLEAR_FLAG(hsc, SMARTCARD_FLAG_TC);
|
---|
| 724 |
|
---|
| 725 | /* Enable the DMA transfer for transmit request by setting the DMAT bit
|
---|
| 726 | in the SMARTCARD CR3 register */
|
---|
| 727 | hsc->Instance->CR3 |= USART_CR3_DMAT;
|
---|
| 728 |
|
---|
| 729 | /* Process Unlocked */
|
---|
| 730 | __HAL_UNLOCK(hsc);
|
---|
| 731 |
|
---|
| 732 | return HAL_OK;
|
---|
| 733 | }
|
---|
| 734 | else
|
---|
| 735 | {
|
---|
| 736 | return HAL_BUSY;
|
---|
| 737 | }
|
---|
| 738 | }
|
---|
| 739 |
|
---|
| 740 | /**
|
---|
| 741 | * @brief Receive an amount of data in non blocking mode
|
---|
| 742 | * @param hsc: pointer to a SMARTCARD_HandleTypeDef structure that contains
|
---|
| 743 | * the configuration information for SMARTCARD module.
|
---|
| 744 | * @param pData: pointer to data buffer
|
---|
| 745 | * @param Size: amount of data to be received
|
---|
| 746 | * @note When the SMARTCARD parity is enabled (PCE = 1) the data received contain the parity bit.s
|
---|
| 747 | * @retval HAL status
|
---|
| 748 | */
|
---|
| 749 | HAL_StatusTypeDef HAL_SMARTCARD_Receive_DMA(SMARTCARD_HandleTypeDef *hsc, uint8_t *pData, uint16_t Size)
|
---|
| 750 | {
|
---|
| 751 | uint32_t *tmp;
|
---|
| 752 | uint32_t tmp1 = 0;
|
---|
| 753 |
|
---|
| 754 | tmp1 = hsc->State;
|
---|
| 755 | if((tmp1 == HAL_SMARTCARD_STATE_READY) || (tmp1 == HAL_SMARTCARD_STATE_BUSY_TX))
|
---|
| 756 | {
|
---|
| 757 | if((pData == NULL) || (Size == 0))
|
---|
| 758 | {
|
---|
| 759 | return HAL_ERROR;
|
---|
| 760 | }
|
---|
| 761 |
|
---|
| 762 | /* Process Locked */
|
---|
| 763 | __HAL_LOCK(hsc);
|
---|
| 764 |
|
---|
| 765 | hsc->pRxBuffPtr = pData;
|
---|
| 766 | hsc->RxXferSize = Size;
|
---|
| 767 |
|
---|
| 768 | hsc->ErrorCode = HAL_SMARTCARD_ERROR_NONE;
|
---|
| 769 | /* Check if a non-blocking transmit process is ongoing or not */
|
---|
| 770 | if(hsc->State == HAL_SMARTCARD_STATE_BUSY_TX)
|
---|
| 771 | {
|
---|
| 772 | hsc->State = HAL_SMARTCARD_STATE_BUSY_TX_RX;
|
---|
| 773 | }
|
---|
| 774 | else
|
---|
| 775 | {
|
---|
| 776 | hsc->State = HAL_SMARTCARD_STATE_BUSY_RX;
|
---|
| 777 | }
|
---|
| 778 |
|
---|
| 779 | /* Set the SMARTCARD DMA transfer complete callback */
|
---|
| 780 | hsc->hdmarx->XferCpltCallback = SMARTCARD_DMAReceiveCplt;
|
---|
| 781 |
|
---|
| 782 | /* Set the DMA error callback */
|
---|
| 783 | hsc->hdmarx->XferErrorCallback = SMARTCARD_DMAError;
|
---|
| 784 |
|
---|
| 785 | /* Enable the DMA Stream */
|
---|
| 786 | tmp = (uint32_t*)&pData;
|
---|
| 787 | HAL_DMA_Start_IT(hsc->hdmarx, (uint32_t)&hsc->Instance->DR, *(uint32_t*)tmp, Size);
|
---|
| 788 |
|
---|
| 789 | /* Enable the DMA transfer for the receiver request by setting the DMAR bit
|
---|
| 790 | in the SMARTCARD CR3 register */
|
---|
| 791 | hsc->Instance->CR3 |= USART_CR3_DMAR;
|
---|
| 792 |
|
---|
| 793 | /* Process Unlocked */
|
---|
| 794 | __HAL_UNLOCK(hsc);
|
---|
| 795 |
|
---|
| 796 | return HAL_OK;
|
---|
| 797 | }
|
---|
| 798 | else
|
---|
| 799 | {
|
---|
| 800 | return HAL_BUSY;
|
---|
| 801 | }
|
---|
| 802 | }
|
---|
| 803 |
|
---|
| 804 | /**
|
---|
| 805 | * @brief This function handles SMARTCARD interrupt request.
|
---|
| 806 | * @param hsc: pointer to a SMARTCARD_HandleTypeDef structure that contains
|
---|
| 807 | * the configuration information for SMARTCARD module.
|
---|
| 808 | * @retval None
|
---|
| 809 | */
|
---|
| 810 | void HAL_SMARTCARD_IRQHandler(SMARTCARD_HandleTypeDef *hsc)
|
---|
| 811 | {
|
---|
| 812 | uint32_t tmp1 = 0, tmp2 = 0;
|
---|
| 813 |
|
---|
| 814 | tmp1 = hsc->Instance->SR;
|
---|
| 815 | tmp2 = __HAL_SMARTCARD_GET_IT_SOURCE(hsc, SMARTCARD_IT_PE);
|
---|
| 816 |
|
---|
| 817 | /* SMARTCARD parity error interrupt occurred --------------------------------*/
|
---|
| 818 | if(((tmp1 & SMARTCARD_FLAG_PE) != RESET) && (tmp2 != RESET))
|
---|
| 819 | {
|
---|
| 820 | __HAL_SMARTCARD_CLEAR_PEFLAG(hsc);
|
---|
| 821 | hsc->ErrorCode |= HAL_SMARTCARD_ERROR_PE;
|
---|
| 822 | }
|
---|
| 823 |
|
---|
| 824 | tmp2 = __HAL_SMARTCARD_GET_IT_SOURCE(hsc, SMARTCARD_IT_ERR);
|
---|
| 825 | /* SMARTCARD frame error interrupt occurred ---------------------------------*/
|
---|
| 826 | if(((tmp1 & SMARTCARD_FLAG_FE) != RESET) && (tmp2 != RESET))
|
---|
| 827 | {
|
---|
| 828 | __HAL_SMARTCARD_CLEAR_FEFLAG(hsc);
|
---|
| 829 | hsc->ErrorCode |= HAL_SMARTCARD_ERROR_FE;
|
---|
| 830 | }
|
---|
| 831 |
|
---|
| 832 | tmp2 = __HAL_SMARTCARD_GET_IT_SOURCE(hsc, SMARTCARD_IT_ERR);
|
---|
| 833 | /* SMARTCARD noise error interrupt occurred ---------------------------------*/
|
---|
| 834 | if(((tmp1 & SMARTCARD_FLAG_NE) != RESET) && (tmp2 != RESET))
|
---|
| 835 | {
|
---|
| 836 | __HAL_SMARTCARD_CLEAR_NEFLAG(hsc);
|
---|
| 837 | hsc->ErrorCode |= HAL_SMARTCARD_ERROR_NE;
|
---|
| 838 | }
|
---|
| 839 |
|
---|
| 840 | tmp2 = __HAL_SMARTCARD_GET_IT_SOURCE(hsc, SMARTCARD_IT_ERR);
|
---|
| 841 | /* SMARTCARD Over-Run interrupt occurred ------------------------------------*/
|
---|
| 842 | if(((tmp1 & SMARTCARD_FLAG_ORE) != RESET) && (tmp2 != RESET))
|
---|
| 843 | {
|
---|
| 844 | __HAL_SMARTCARD_CLEAR_OREFLAG(hsc);
|
---|
| 845 | hsc->ErrorCode |= HAL_SMARTCARD_ERROR_ORE;
|
---|
| 846 | }
|
---|
| 847 |
|
---|
| 848 | tmp2 = __HAL_SMARTCARD_GET_IT_SOURCE(hsc, SMARTCARD_IT_RXNE);
|
---|
| 849 | /* SMARTCARD in mode Receiver ----------------------------------------------*/
|
---|
| 850 | if(((tmp1 & SMARTCARD_FLAG_RXNE) != RESET) && (tmp2 != RESET))
|
---|
| 851 | {
|
---|
| 852 | SMARTCARD_Receive_IT(hsc);
|
---|
| 853 | }
|
---|
| 854 |
|
---|
| 855 | tmp2 = __HAL_SMARTCARD_GET_IT_SOURCE(hsc, SMARTCARD_IT_TXE);
|
---|
| 856 | /* SMARTCARD in mode Transmitter -------------------------------------------*/
|
---|
| 857 | if(((tmp1 & SMARTCARD_FLAG_TXE) != RESET) && (tmp2 != RESET))
|
---|
| 858 | {
|
---|
| 859 | SMARTCARD_Transmit_IT(hsc);
|
---|
| 860 | }
|
---|
| 861 |
|
---|
| 862 | tmp2 = __HAL_SMARTCARD_GET_IT_SOURCE(hsc, SMARTCARD_IT_TC);
|
---|
| 863 | /* SMARTCARD in mode Transmitter (transmission end) ------------------------*/
|
---|
| 864 | if(((tmp1 & SMARTCARD_FLAG_TC) != RESET) && (tmp2 != RESET))
|
---|
| 865 | {
|
---|
| 866 | SMARTCARD_EndTransmit_IT(hsc);
|
---|
| 867 | }
|
---|
| 868 |
|
---|
| 869 | /* Call the Error call Back in case of Errors */
|
---|
| 870 | if(hsc->ErrorCode != HAL_SMARTCARD_ERROR_NONE)
|
---|
| 871 | {
|
---|
| 872 | /* Set the SMARTCARD state ready to be able to start again the process */
|
---|
| 873 | hsc->State= HAL_SMARTCARD_STATE_READY;
|
---|
| 874 | HAL_SMARTCARD_ErrorCallback(hsc);
|
---|
| 875 | }
|
---|
| 876 | }
|
---|
| 877 |
|
---|
| 878 | /**
|
---|
| 879 | * @brief Tx Transfer completed callbacks
|
---|
| 880 | * @param hsc: pointer to a SMARTCARD_HandleTypeDef structure that contains
|
---|
| 881 | * the configuration information for SMARTCARD module.
|
---|
| 882 | * @retval None
|
---|
| 883 | */
|
---|
| 884 | __weak void HAL_SMARTCARD_TxCpltCallback(SMARTCARD_HandleTypeDef *hsc)
|
---|
| 885 | {
|
---|
| 886 | /* NOTE : This function Should not be modified, when the callback is needed,
|
---|
| 887 | the HAL_SMARTCARD_TxCpltCallback could be implemented in the user file
|
---|
| 888 | */
|
---|
| 889 | }
|
---|
| 890 |
|
---|
| 891 | /**
|
---|
| 892 | * @brief Rx Transfer completed callbacks
|
---|
| 893 | * @param hsc: pointer to a SMARTCARD_HandleTypeDef structure that contains
|
---|
| 894 | * the configuration information for SMARTCARD module.
|
---|
| 895 | * @retval None
|
---|
| 896 | */
|
---|
| 897 | __weak void HAL_SMARTCARD_RxCpltCallback(SMARTCARD_HandleTypeDef *hsc)
|
---|
| 898 | {
|
---|
| 899 | /* NOTE : This function Should not be modified, when the callback is needed,
|
---|
| 900 | the HAL_SMARTCARD_TxCpltCallback could be implemented in the user file
|
---|
| 901 | */
|
---|
| 902 | }
|
---|
| 903 |
|
---|
| 904 | /**
|
---|
| 905 | * @brief SMARTCARD error callbacks
|
---|
| 906 | * @param hsc: pointer to a SMARTCARD_HandleTypeDef structure that contains
|
---|
| 907 | * the configuration information for SMARTCARD module.
|
---|
| 908 | * @retval None
|
---|
| 909 | */
|
---|
| 910 | __weak void HAL_SMARTCARD_ErrorCallback(SMARTCARD_HandleTypeDef *hsc)
|
---|
| 911 | {
|
---|
| 912 | /* NOTE : This function Should not be modified, when the callback is needed,
|
---|
| 913 | the HAL_SMARTCARD_ErrorCallback could be implemented in the user file
|
---|
| 914 | */
|
---|
| 915 | }
|
---|
| 916 |
|
---|
| 917 | /**
|
---|
| 918 | * @}
|
---|
| 919 | */
|
---|
| 920 |
|
---|
| 921 | /** @defgroup SMARTCARD_Exported_Functions_Group3 Peripheral State and Errors functions
|
---|
| 922 | * @brief SMARTCARD State and Errors functions
|
---|
| 923 | *
|
---|
| 924 | @verbatim
|
---|
| 925 | ===============================================================================
|
---|
| 926 | ##### Peripheral State and Errors functions #####
|
---|
| 927 | ===============================================================================
|
---|
| 928 | [..]
|
---|
| 929 | This subsection provides a set of functions allowing to control the SmartCard.
|
---|
| 930 | (+) HAL_SMARTCARD_GetState() API can be helpful to check in run-time the state of the SmartCard peripheral.
|
---|
| 931 | (+) HAL_SMARTCARD_GetError() check in run-time errors that could be occurred during communication.
|
---|
| 932 | @endverbatim
|
---|
| 933 | * @{
|
---|
| 934 | */
|
---|
| 935 |
|
---|
| 936 | /**
|
---|
| 937 | * @brief return the SMARTCARD state
|
---|
| 938 | * @param hsc: pointer to a SMARTCARD_HandleTypeDef structure that contains
|
---|
| 939 | * the configuration information for SMARTCARD module.
|
---|
| 940 | * @retval HAL state
|
---|
| 941 | */
|
---|
| 942 | HAL_SMARTCARD_StateTypeDef HAL_SMARTCARD_GetState(SMARTCARD_HandleTypeDef *hsc)
|
---|
| 943 | {
|
---|
| 944 | return hsc->State;
|
---|
| 945 | }
|
---|
| 946 |
|
---|
| 947 | /**
|
---|
| 948 | * @brief Return the SMARTCARD error code
|
---|
| 949 | * @param hsc : pointer to a SMARTCARD_HandleTypeDef structure that contains
|
---|
| 950 | * the configuration information for the specified SMARTCARD.
|
---|
| 951 | * @retval SMARTCARD Error Code
|
---|
| 952 | */
|
---|
| 953 | uint32_t HAL_SMARTCARD_GetError(SMARTCARD_HandleTypeDef *hsc)
|
---|
| 954 | {
|
---|
| 955 | return hsc->ErrorCode;
|
---|
| 956 | }
|
---|
| 957 |
|
---|
| 958 | /**
|
---|
| 959 | * @}
|
---|
| 960 | */
|
---|
| 961 |
|
---|
| 962 | /**
|
---|
| 963 | * @brief DMA SMARTCARD transmit process complete callback
|
---|
| 964 | * @param hdma: pointer to a DMA_HandleTypeDef structure that contains
|
---|
| 965 | * the configuration information for the specified DMA module.
|
---|
| 966 | * @retval None
|
---|
| 967 | */
|
---|
| 968 | static void SMARTCARD_DMATransmitCplt(DMA_HandleTypeDef *hdma)
|
---|
| 969 | {
|
---|
| 970 | SMARTCARD_HandleTypeDef* hsc = ( SMARTCARD_HandleTypeDef* )((DMA_HandleTypeDef* )hdma)->Parent;
|
---|
| 971 |
|
---|
| 972 | hsc->TxXferCount = 0;
|
---|
| 973 |
|
---|
| 974 | /* Disable the DMA transfer for transmit request by setting the DMAT bit
|
---|
| 975 | in the USART CR3 register */
|
---|
| 976 | hsc->Instance->CR3 &= (uint32_t)~((uint32_t)USART_CR3_DMAT);
|
---|
| 977 |
|
---|
| 978 | /* Enable the SMARTCARD Transmit Complete Interrupt */
|
---|
| 979 | __HAL_SMARTCARD_ENABLE_IT(hsc, SMARTCARD_IT_TC);
|
---|
| 980 | }
|
---|
| 981 |
|
---|
| 982 | /**
|
---|
| 983 | * @brief DMA SMARTCARD receive process complete callback
|
---|
| 984 | * @param hdma: pointer to a DMA_HandleTypeDef structure that contains
|
---|
| 985 | * the configuration information for the specified DMA module.
|
---|
| 986 | * @retval None
|
---|
| 987 | */
|
---|
| 988 | static void SMARTCARD_DMAReceiveCplt(DMA_HandleTypeDef *hdma)
|
---|
| 989 | {
|
---|
| 990 | SMARTCARD_HandleTypeDef* hsc = ( SMARTCARD_HandleTypeDef* )((DMA_HandleTypeDef* )hdma)->Parent;
|
---|
| 991 |
|
---|
| 992 | hsc->RxXferCount = 0;
|
---|
| 993 |
|
---|
| 994 | /* Disable the DMA transfer for the receiver request by setting the DMAR bit
|
---|
| 995 | in the USART CR3 register */
|
---|
| 996 | hsc->Instance->CR3 &= (uint32_t)~((uint32_t)USART_CR3_DMAR);
|
---|
| 997 |
|
---|
| 998 | /* Check if a non-blocking transmit process is ongoing or not */
|
---|
| 999 | if(hsc->State == HAL_SMARTCARD_STATE_BUSY_TX_RX)
|
---|
| 1000 | {
|
---|
| 1001 | hsc->State = HAL_SMARTCARD_STATE_BUSY_TX;
|
---|
| 1002 | }
|
---|
| 1003 | else
|
---|
| 1004 | {
|
---|
| 1005 | hsc->State = HAL_SMARTCARD_STATE_READY;
|
---|
| 1006 | }
|
---|
| 1007 |
|
---|
| 1008 | HAL_SMARTCARD_RxCpltCallback(hsc);
|
---|
| 1009 | }
|
---|
| 1010 |
|
---|
| 1011 | /**
|
---|
| 1012 | * @brief DMA SMARTCARD communication error callback
|
---|
| 1013 | * @param hdma: pointer to a DMA_HandleTypeDef structure that contains
|
---|
| 1014 | * the configuration information for the specified DMA module.
|
---|
| 1015 | * @retval None
|
---|
| 1016 | */
|
---|
| 1017 | static void SMARTCARD_DMAError(DMA_HandleTypeDef *hdma)
|
---|
| 1018 | {
|
---|
| 1019 | SMARTCARD_HandleTypeDef* hsc = ( SMARTCARD_HandleTypeDef* )((DMA_HandleTypeDef* )hdma)->Parent;
|
---|
| 1020 |
|
---|
| 1021 | hsc->RxXferCount = 0;
|
---|
| 1022 | hsc->TxXferCount = 0;
|
---|
| 1023 | hsc->ErrorCode = HAL_SMARTCARD_ERROR_DMA;
|
---|
| 1024 | hsc->State= HAL_SMARTCARD_STATE_READY;
|
---|
| 1025 |
|
---|
| 1026 | HAL_SMARTCARD_ErrorCallback(hsc);
|
---|
| 1027 | }
|
---|
| 1028 |
|
---|
| 1029 | /**
|
---|
| 1030 | * @brief This function handles SMARTCARD Communication Timeout.
|
---|
| 1031 | * @param hsc: SMARTCARD handle
|
---|
| 1032 | * @param Flag: specifies the SMARTCARD flag to check.
|
---|
| 1033 | * @param Status: The new Flag status (SET or RESET).
|
---|
| 1034 | * @param Timeout: Timeout duration
|
---|
| 1035 | * @retval HAL status
|
---|
| 1036 | */
|
---|
| 1037 | static HAL_StatusTypeDef SMARTCARD_WaitOnFlagUntilTimeout(SMARTCARD_HandleTypeDef *hsc, uint32_t Flag, FlagStatus Status, uint32_t Timeout)
|
---|
| 1038 | {
|
---|
| 1039 | uint32_t tickstart = 0;
|
---|
| 1040 |
|
---|
| 1041 | /* Get tick */
|
---|
| 1042 | tickstart = HAL_GetTick();
|
---|
| 1043 |
|
---|
| 1044 | /* Wait until flag is set */
|
---|
| 1045 | if(Status == RESET)
|
---|
| 1046 | {
|
---|
| 1047 | while(__HAL_SMARTCARD_GET_FLAG(hsc, Flag) == RESET)
|
---|
| 1048 | {
|
---|
| 1049 | /* Check for the Timeout */
|
---|
| 1050 | if(Timeout != HAL_MAX_DELAY)
|
---|
| 1051 | {
|
---|
| 1052 | if((Timeout == 0)||((HAL_GetTick() - tickstart ) > Timeout))
|
---|
| 1053 | {
|
---|
| 1054 | /* Disable TXE and RXNE interrupts for the interrupt process */
|
---|
| 1055 | __HAL_SMARTCARD_DISABLE_IT(hsc, SMARTCARD_IT_TXE);
|
---|
| 1056 | __HAL_SMARTCARD_DISABLE_IT(hsc, SMARTCARD_IT_RXNE);
|
---|
| 1057 |
|
---|
| 1058 | hsc->State= HAL_SMARTCARD_STATE_READY;
|
---|
| 1059 |
|
---|
| 1060 | /* Process Unlocked */
|
---|
| 1061 | __HAL_UNLOCK(hsc);
|
---|
| 1062 |
|
---|
| 1063 | return HAL_TIMEOUT;
|
---|
| 1064 | }
|
---|
| 1065 | }
|
---|
| 1066 | }
|
---|
| 1067 | }
|
---|
| 1068 | else
|
---|
| 1069 | {
|
---|
| 1070 | while(__HAL_SMARTCARD_GET_FLAG(hsc, Flag) != RESET)
|
---|
| 1071 | {
|
---|
| 1072 | /* Check for the Timeout */
|
---|
| 1073 | if(Timeout != HAL_MAX_DELAY)
|
---|
| 1074 | {
|
---|
| 1075 | if((Timeout == 0)||((HAL_GetTick() - tickstart ) > Timeout))
|
---|
| 1076 | {
|
---|
| 1077 | /* Disable TXE and RXNE interrupts for the interrupt process */
|
---|
| 1078 | __HAL_SMARTCARD_DISABLE_IT(hsc, SMARTCARD_IT_TXE);
|
---|
| 1079 | __HAL_SMARTCARD_DISABLE_IT(hsc, SMARTCARD_IT_RXNE);
|
---|
| 1080 |
|
---|
| 1081 | hsc->State= HAL_SMARTCARD_STATE_READY;
|
---|
| 1082 |
|
---|
| 1083 | /* Process Unlocked */
|
---|
| 1084 | __HAL_UNLOCK(hsc);
|
---|
| 1085 |
|
---|
| 1086 | return HAL_TIMEOUT;
|
---|
| 1087 | }
|
---|
| 1088 | }
|
---|
| 1089 | }
|
---|
| 1090 | }
|
---|
| 1091 | return HAL_OK;
|
---|
| 1092 | }
|
---|
| 1093 |
|
---|
| 1094 | /**
|
---|
| 1095 | * @brief Send an amount of data in non blocking mode
|
---|
| 1096 | * @param hsc: pointer to a SMARTCARD_HandleTypeDef structure that contains
|
---|
| 1097 | * the configuration information for SMARTCARD module.
|
---|
| 1098 | * @retval HAL status
|
---|
| 1099 | */
|
---|
| 1100 | static HAL_StatusTypeDef SMARTCARD_Transmit_IT(SMARTCARD_HandleTypeDef *hsc)
|
---|
| 1101 | {
|
---|
| 1102 | uint16_t* tmp;
|
---|
| 1103 | uint32_t tmp1 = 0;
|
---|
| 1104 |
|
---|
| 1105 | tmp1 = hsc->State;
|
---|
| 1106 | if((tmp1 == HAL_SMARTCARD_STATE_BUSY_TX) || (tmp1 == HAL_SMARTCARD_STATE_BUSY_TX_RX))
|
---|
| 1107 | {
|
---|
| 1108 | tmp = (uint16_t*) hsc->pTxBuffPtr;
|
---|
| 1109 | hsc->Instance->DR = (uint16_t)(*tmp & (uint16_t)0x01FF);
|
---|
| 1110 | hsc->pTxBuffPtr += 1;
|
---|
| 1111 |
|
---|
| 1112 | if(--hsc->TxXferCount == 0)
|
---|
| 1113 | {
|
---|
| 1114 | /* Disable the SMARTCARD Transmit data register empty Interrupt */
|
---|
| 1115 | __HAL_SMARTCARD_DISABLE_IT(hsc, SMARTCARD_IT_TXE);
|
---|
| 1116 |
|
---|
| 1117 | /* Enable the SMARTCARD Transmit Complete Interrupt */
|
---|
| 1118 | __HAL_SMARTCARD_ENABLE_IT(hsc, SMARTCARD_IT_TC);
|
---|
| 1119 | }
|
---|
| 1120 |
|
---|
| 1121 | return HAL_OK;
|
---|
| 1122 | }
|
---|
| 1123 | else
|
---|
| 1124 | {
|
---|
| 1125 | return HAL_BUSY;
|
---|
| 1126 | }
|
---|
| 1127 | }
|
---|
| 1128 |
|
---|
| 1129 | /**
|
---|
| 1130 | * @brief Wraps up transmission in non blocking mode.
|
---|
| 1131 | * @param hsmartcard: pointer to a SMARTCARD_HandleTypeDef structure that contains
|
---|
| 1132 | * the configuration information for the specified SMARTCARD module.
|
---|
| 1133 | * @retval HAL status
|
---|
| 1134 | */
|
---|
| 1135 | static HAL_StatusTypeDef SMARTCARD_EndTransmit_IT(SMARTCARD_HandleTypeDef *hsmartcard)
|
---|
| 1136 | {
|
---|
| 1137 | /* Disable the SMARTCARD Transmit Complete Interrupt */
|
---|
| 1138 | __HAL_SMARTCARD_DISABLE_IT(hsmartcard, SMARTCARD_IT_TC);
|
---|
| 1139 |
|
---|
| 1140 | /* Check if a receive process is ongoing or not */
|
---|
| 1141 | if(hsmartcard->State == HAL_SMARTCARD_STATE_BUSY_TX_RX)
|
---|
| 1142 | {
|
---|
| 1143 | hsmartcard->State = HAL_SMARTCARD_STATE_BUSY_RX;
|
---|
| 1144 | }
|
---|
| 1145 | else
|
---|
| 1146 | {
|
---|
| 1147 | /* Disable the SMARTCARD Error Interrupt: (Frame error, noise error, overrun error) */
|
---|
| 1148 | __HAL_SMARTCARD_DISABLE_IT(hsmartcard, SMARTCARD_IT_ERR);
|
---|
| 1149 |
|
---|
| 1150 | hsmartcard->State = HAL_SMARTCARD_STATE_READY;
|
---|
| 1151 | }
|
---|
| 1152 |
|
---|
| 1153 | HAL_SMARTCARD_TxCpltCallback(hsmartcard);
|
---|
| 1154 |
|
---|
| 1155 | return HAL_OK;
|
---|
| 1156 | }
|
---|
| 1157 |
|
---|
| 1158 | /**
|
---|
| 1159 | * @brief Receive an amount of data in non blocking mode
|
---|
| 1160 | * @param hsc: pointer to a SMARTCARD_HandleTypeDef structure that contains
|
---|
| 1161 | * the configuration information for SMARTCARD module.
|
---|
| 1162 | * @retval HAL status
|
---|
| 1163 | */
|
---|
| 1164 | static HAL_StatusTypeDef SMARTCARD_Receive_IT(SMARTCARD_HandleTypeDef *hsc)
|
---|
| 1165 | {
|
---|
| 1166 | uint16_t* tmp;
|
---|
| 1167 | uint32_t tmp1 = 0;
|
---|
| 1168 |
|
---|
| 1169 | tmp1 = hsc->State;
|
---|
| 1170 | if((tmp1 == HAL_SMARTCARD_STATE_BUSY_RX) || (tmp1 == HAL_SMARTCARD_STATE_BUSY_TX_RX))
|
---|
| 1171 | {
|
---|
| 1172 | tmp = (uint16_t*) hsc->pRxBuffPtr;
|
---|
| 1173 | *tmp = (uint16_t)(hsc->Instance->DR & (uint16_t)0x00FF);
|
---|
| 1174 | hsc->pRxBuffPtr += 1;
|
---|
| 1175 |
|
---|
| 1176 | if(--hsc->RxXferCount == 0)
|
---|
| 1177 | {
|
---|
| 1178 | __HAL_SMARTCARD_DISABLE_IT(hsc, SMARTCARD_IT_RXNE);
|
---|
| 1179 |
|
---|
| 1180 | /* Disable the SMARTCARD Parity Error Interrupt */
|
---|
| 1181 | __HAL_SMARTCARD_DISABLE_IT(hsc, SMARTCARD_IT_PE);
|
---|
| 1182 |
|
---|
| 1183 | /* Disable the SMARTCARD Error Interrupt: (Frame error, noise error, overrun error) */
|
---|
| 1184 | __HAL_SMARTCARD_DISABLE_IT(hsc, SMARTCARD_IT_ERR);
|
---|
| 1185 |
|
---|
| 1186 | /* Check if a non-blocking transmit process is ongoing or not */
|
---|
| 1187 | if(hsc->State == HAL_SMARTCARD_STATE_BUSY_TX_RX)
|
---|
| 1188 | {
|
---|
| 1189 | hsc->State = HAL_SMARTCARD_STATE_BUSY_TX;
|
---|
| 1190 | }
|
---|
| 1191 | else
|
---|
| 1192 | {
|
---|
| 1193 | hsc->State = HAL_SMARTCARD_STATE_READY;
|
---|
| 1194 | }
|
---|
| 1195 |
|
---|
| 1196 | HAL_SMARTCARD_RxCpltCallback(hsc);
|
---|
| 1197 |
|
---|
| 1198 | return HAL_OK;
|
---|
| 1199 | }
|
---|
| 1200 | return HAL_OK;
|
---|
| 1201 | }
|
---|
| 1202 | else
|
---|
| 1203 | {
|
---|
| 1204 | return HAL_BUSY;
|
---|
| 1205 | }
|
---|
| 1206 | }
|
---|
| 1207 |
|
---|
| 1208 | /**
|
---|
| 1209 | * @brief Configure the SMARTCARD peripheral
|
---|
| 1210 | * @param hsc: pointer to a SMARTCARD_HandleTypeDef structure that contains
|
---|
| 1211 | * the configuration information for SMARTCARD module.
|
---|
| 1212 | * @retval None
|
---|
| 1213 | */
|
---|
| 1214 | static void SMARTCARD_SetConfig(SMARTCARD_HandleTypeDef *hsc)
|
---|
| 1215 | {
|
---|
| 1216 | uint32_t tmpreg = 0x00;
|
---|
| 1217 |
|
---|
| 1218 | /* Check the parameters */
|
---|
| 1219 | assert_param(IS_SMARTCARD_INSTANCE(hsc->Instance));
|
---|
| 1220 | assert_param(IS_SMARTCARD_POLARITY(hsc->Init.CLKPolarity));
|
---|
| 1221 | assert_param(IS_SMARTCARD_PHASE(hsc->Init.CLKPhase));
|
---|
| 1222 | assert_param(IS_SMARTCARD_LASTBIT(hsc->Init.CLKLastBit));
|
---|
| 1223 | assert_param(IS_SMARTCARD_BAUDRATE(hsc->Init.BaudRate));
|
---|
| 1224 | assert_param(IS_SMARTCARD_WORD_LENGTH(hsc->Init.WordLength));
|
---|
| 1225 | assert_param(IS_SMARTCARD_STOPBITS(hsc->Init.StopBits));
|
---|
| 1226 | assert_param(IS_SMARTCARD_PARITY(hsc->Init.Parity));
|
---|
| 1227 | assert_param(IS_SMARTCARD_MODE(hsc->Init.Mode));
|
---|
| 1228 | assert_param(IS_SMARTCARD_NACK_STATE(hsc->Init.NACKState));
|
---|
| 1229 |
|
---|
| 1230 | /* The LBCL, CPOL and CPHA bits have to be selected when both the transmitter and the
|
---|
| 1231 | receiver are disabled (TE=RE=0) to ensure that the clock pulses function correctly. */
|
---|
| 1232 | hsc->Instance->CR1 &= (uint32_t)~((uint32_t)(USART_CR1_TE | USART_CR1_RE));
|
---|
| 1233 |
|
---|
| 1234 | /*---------------------------- USART CR2 Configuration ---------------------*/
|
---|
| 1235 | tmpreg = hsc->Instance->CR2;
|
---|
| 1236 | /* Clear CLKEN, CPOL, CPHA and LBCL bits */
|
---|
| 1237 | tmpreg &= (uint32_t)~((uint32_t)(USART_CR2_CPHA | USART_CR2_CPOL | USART_CR2_CLKEN | USART_CR2_LBCL));
|
---|
| 1238 | /* Configure the SMARTCARD Clock, CPOL, CPHA and LastBit -----------------------*/
|
---|
| 1239 | /* Set CPOL bit according to hsc->Init.CLKPolarity value */
|
---|
| 1240 | /* Set CPHA bit according to hsc->Init.CLKPhase value */
|
---|
| 1241 | /* Set LBCL bit according to hsc->Init.CLKLastBit value */
|
---|
| 1242 | /* Set Stop Bits: Set STOP[13:12] bits according to hsc->Init.StopBits value */
|
---|
| 1243 | tmpreg |= (uint32_t)(USART_CR2_CLKEN | hsc->Init.CLKPolarity |
|
---|
| 1244 | hsc->Init.CLKPhase| hsc->Init.CLKLastBit | hsc->Init.StopBits);
|
---|
| 1245 | /* Write to USART CR2 */
|
---|
| 1246 | hsc->Instance->CR2 = (uint32_t)tmpreg;
|
---|
| 1247 |
|
---|
| 1248 | tmpreg = hsc->Instance->CR2;
|
---|
| 1249 |
|
---|
| 1250 | /* Clear STOP[13:12] bits */
|
---|
| 1251 | tmpreg &= (uint32_t)~((uint32_t)USART_CR2_STOP);
|
---|
| 1252 |
|
---|
| 1253 | /* Set Stop Bits: Set STOP[13:12] bits according to hsc->Init.StopBits value */
|
---|
| 1254 | tmpreg |= (uint32_t)(hsc->Init.StopBits);
|
---|
| 1255 |
|
---|
| 1256 | /* Write to USART CR2 */
|
---|
| 1257 | hsc->Instance->CR2 = (uint32_t)tmpreg;
|
---|
| 1258 |
|
---|
| 1259 | /*-------------------------- USART CR1 Configuration -----------------------*/
|
---|
| 1260 | tmpreg = hsc->Instance->CR1;
|
---|
| 1261 |
|
---|
| 1262 | /* Clear M, PCE, PS, TE and RE bits */
|
---|
| 1263 | tmpreg &= (uint32_t)~((uint32_t)(USART_CR1_M | USART_CR1_PCE | USART_CR1_PS | USART_CR1_TE | \
|
---|
| 1264 | USART_CR1_RE));
|
---|
| 1265 |
|
---|
| 1266 | /* Configure the SMARTCARD Word Length, Parity and mode:
|
---|
| 1267 | Set the M bits according to hsc->Init.WordLength value
|
---|
| 1268 | Set PCE and PS bits according to hsc->Init.Parity value
|
---|
| 1269 | Set TE and RE bits according to hsc->Init.Mode value */
|
---|
| 1270 | tmpreg |= (uint32_t)hsc->Init.WordLength | hsc->Init.Parity | hsc->Init.Mode;
|
---|
| 1271 |
|
---|
| 1272 | /* Write to USART CR1 */
|
---|
| 1273 | hsc->Instance->CR1 = (uint32_t)tmpreg;
|
---|
| 1274 |
|
---|
| 1275 | /*-------------------------- USART CR3 Configuration -----------------------*/
|
---|
| 1276 | /* Clear CTSE and RTSE bits */
|
---|
| 1277 | hsc->Instance->CR3 &= (uint32_t)~((uint32_t)(USART_CR3_RTSE | USART_CR3_CTSE));
|
---|
| 1278 |
|
---|
| 1279 | /*-------------------------- USART BRR Configuration -----------------------*/
|
---|
| 1280 | if((hsc->Instance == USART1) || (hsc->Instance == USART6))
|
---|
| 1281 | {
|
---|
| 1282 | hsc->Instance->BRR = SMARTCARD_BRR(HAL_RCC_GetPCLK2Freq(), hsc->Init.BaudRate);
|
---|
| 1283 | }
|
---|
| 1284 | else
|
---|
| 1285 | {
|
---|
| 1286 | hsc->Instance->BRR = SMARTCARD_BRR(HAL_RCC_GetPCLK1Freq(), hsc->Init.BaudRate);
|
---|
| 1287 | }
|
---|
| 1288 | }
|
---|
| 1289 |
|
---|
| 1290 | /**
|
---|
| 1291 | * @}
|
---|
| 1292 | */
|
---|
| 1293 |
|
---|
| 1294 | #endif /* HAL_SMARTCARD_MODULE_ENABLED */
|
---|
| 1295 | /**
|
---|
| 1296 | * @}
|
---|
| 1297 | */
|
---|
| 1298 |
|
---|
| 1299 | /**
|
---|
| 1300 | * @}
|
---|
| 1301 | */
|
---|
| 1302 |
|
---|
| 1303 | /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
|
---|