ald_smartcard.c 33.3 KB
Newer Older
W
wangyq2018 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
/**
  *********************************************************************************
  *
  * @file    ald_smartcard.c
  * @brief   SMARTCARD module driver.
  *          This file provides firmware functions to manage the following
  *          functionalities of the SMARTCARD peripheral:
  *           + Initialization functions
  *           + IO operation functions
  *           + Peripheral State and Errors functions
  *
  * @version V1.0
  * @date    25 Apr 2017
  * @author  AE Team
  * @note
  *
  * Copyright (C) Shanghai Eastsoft Microelectronics Co. Ltd. All rights reserved.
  *
  *********************************************************************************
  * @verbatim
  ==============================================================================
                        ##### How to use this driver #####
  ==============================================================================
  [..]
    The SMARTCARD driver can be used as follows:

    (#) Declare a smartcard_handle_t handle structure.
        (##) Enable the interface clock of the USARTx associated to the SMARTCARD.
        (##) SMARTCARD pins configuration:
             (+++) Enable the clock for the SMARTCARD GPIOs.
             (+++) Configure the USART pins (TX as alternate function pull-up, RX as alternate function Input).
32 33
        (##) NVIC configuration if you need to use interrupt process (ald_smartcard_send_by_it()
             and ald_smartcard_recv_by_it() APIs):
W
wangyq2018 已提交
34 35
            (+++) Configure the USARTx interrupt priority.
            (+++) Enable the NVIC USART IRQ handle.
36 37
        (##) DMA Configuration if you need to use DMA process (ald_smartcard_send_by_dma()
             and ald_smartcard_recv_by_dma() APIs):
W
wangyq2018 已提交
38 39 40 41 42 43 44 45 46 47 48 49
            (+++) Declare a DMA handle structure for the Tx/Rx channel.
            (+++) Enable the DMAx interface clock.
            (+++) Configure the declared DMA handle structure with the required Tx/Rx parameters.
            (+++) Configure the DMA Tx/Rx channel.
            (+++) Associate the initilalized DMA handle to the SMARTCARD DMA Tx/Rx handle.
            (+++) Configure the priority and enable the NVIC for the transfer complete interrupt on the DMA Tx/Rx channel.
            (+++) Configure the USARTx interrupt priority and enable the NVIC USART IRQ handle
                  (used for last byte sending completion detection in DMA non circular mode)

    (#) Program the Baud Rate, Word Length , Stop Bit, Parity, Hardware
        flow control and Mode(Receiver/Transmitter) in the SMARTCARD Init structure.

50
    (#) Initialize the SMARTCARD registers by calling the ald_smartcard_init() API.
W
wangyq2018 已提交
51 52 53 54 55 56

    (#) Three operation modes are available within this driver :

     *** Polling mode IO operation ***
     =================================
     [..]
57 58
       (+) Send an amount of data in blocking mode using ald_smartcard_send()
       (+) Receive an amount of data in blocking mode using ald_smartcard_recv()
W
wangyq2018 已提交
59 60 61 62

     *** Interrupt mode IO operation ***
     ===================================
     [..]
63
       (+) Send an amount of data in non blocking mode using ald_smartcard_send_by_it()
W
wangyq2018 已提交
64 65
       (+) At transmission end of transfer hperh->tx_cplt_cbk() is executed and user can
            add his own code by customization of function pointer hperh->tx_cplt_cbk()
66
       (+) Receive an amount of data in non blocking mode using ald_smartcard_recv_by_it()
W
wangyq2018 已提交
67 68 69 70 71 72 73 74
       (+) At reception end of transfer hperh->rx_cplt_cbk() is executed and user can
            add his own code by customization of function pointer hperh->rx_cplt_cbk()
       (+) In case of transfer Error, hperh->error_cbk() function is executed and user can
            add his own code by customization of function pointer hperh->error_cbk()

     *** DMA mode IO operation ***
     ==============================
     [..]
75
       (+) Send an amount of data in non blocking mode (DMA) using ald_smartcard_send_by_dma()
W
wangyq2018 已提交
76 77
       (+) At transmission end of transfer hperh->tx_cplt_cbk() is executed and user can
            add his own code by customization of function pointer hperh->tx_cplt_cbk()
78
       (+) Receive an amount of data in non blocking mode (DMA) using ald_smartcard_recv_by_dma()
W
wangyq2018 已提交
79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182
       (+) At reception end of transfer hperh->rx_cplt_cbk() is executed and user can
            add his own code by customization of function pointer hperh->rx_cplt_cbk()
       (+) In case of transfer Error, hperh->error_cbk()() function is executed and user can
            add his own code by customization of function pointer hperh->error_cbk()

     *** SMARTCARD ALD driver macros list ***
     ========================================
     [..]
       Below the list of most used macros in SMARTCARD ALD driver.

       (+) SMARTCARD_ENABLE: Enable the SmartCard peripheral.
       (+) SMARTCARD_DISABLE: Disable the SmartCard peripheral.
       (+) smartcard_reset_HANDLE_STATE : Reset SmartCard handle.
       (+) SMARTCARD_FLUSH_DRREGISTER : Flush SmartCard data.

     [..]
       (@) You can refer to the SMARTCARD library header file for more useful macros

    @endverbatim
  ******************************************************************************
  */


#include "ald_smartcard.h"
#include "ald_cmu.h"


/** @addtogroup ES32FXXX_ALD
  * @{
  */

/** @defgroup SMARTCARD SMARTCARD
  * @brief SMARTCARD module driver
  * @{
  */

#ifdef ALD_SMARTCARD

/** @addtogroup SMARTCARD_Private_Functions   SMARTCARD Private Functions
  * @{
  */
static ald_status_t __smartcard_send_by_it(smartcard_handle_t *hperh);
static ald_status_t __smartcard_end_send_by_it(smartcard_handle_t *hsmartcard);
static ald_status_t __smartcard_recv_by_it(smartcard_handle_t *hperh);
static void smartcard_set_config(smartcard_handle_t *hperh);
#ifdef ALD_DMA
    static void smartcard_dma_send_cplt(void *arg);
    static void smartcard_dma_recv_cplt(void *arg);
    static void smartcard_dma_error(void *arg);
#endif
static ald_status_t smartcard_wait_flag(smartcard_handle_t *hperh, usart_flag_t flag, flag_status_t status, uint32_t timeout);
/**
  * @}
  */

/** @defgroup SMARTCARD_Public_Functions SMARTCARD Public Functions
  * @{
  */

/** @defgroup SMARTCARD_Public_Functions_Group1 Initialization functions
  * @brief   Initialization and Configuration functions
  *
  * @verbatim

  ==============================================================================
              ##### Initialization and Configuration functions #####
  ==============================================================================
  [..]
  This subsection provides a set of functions allowing to initialize the USART
  in Smartcard mode.
  [..]
  The Smartcard interface is designed to support asynchronous protocol Smartcards as
  defined in the ISO 7816-3 standard.
  [..]
  The USART can provide a clock to the smartcard through the SCLK output.
  In smartcard mode, SCLK is not associated to the communication but is simply derived
  from the internal peripheral input clock through a 5-bit prescaler.
  [..]
  (+) For the Smartcard mode only these parameters can be configured:
      (++) Baud Rate
      (++) Word Length => Should be 9 bits (8 bits + parity)
      (++) Stop Bit
      (++) Parity: => Should be enabled
      (++) USART polarity
      (++) USART phase
      (++) USART LastBit
      (++) Receiver/transmitter modes
      (++) Prescaler
      (++) GuardTime
      (++) NACKState: The Smartcard NACK state

     (+) Recommended SmartCard interface configuration to get the Answer to Reset from the Card:
        (++) word Length = 9 Bits
        (++) 1.5 Stop Bit
        (++) Even parity
        (++) BaudRate = 12096 baud
        (++) Tx and Rx enabled
  [..]
  Please refer to the ISO 7816-3 specification for more details.

    (@) It is also possible to choose 0.5 stop bit for receiving but it is recommended
        to use 1.5 stop bits for both transmitting and receiving to avoid switching
        between the two configurations.
  [..]
183
  The ald_smartcard_init() function follows the USART SmartCard configuration procedure.
W
wangyq2018 已提交
184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204

    @endverbatim
  * @{
  */

/*
  Additionnal remark on the smartcard frame:
   +-------------------------------------------------------------+
   |   M bit |  PCE bit  |        SMARTCARD frame                |
   |---------------------|---------------------------------------|
   |    1    |    1      |    | SB | 8 bit data | PB | STB |     |
   +-------------------------------------------------------------+
*/

/**
  * @brief  Initializes the SmartCard mode according to the specified
  *         parameters in the smartcard_handle_t and create the associated handle.
  * @param  hperh: Pointer to a smartcard_handle_t structure that contains
  *         the configuration information for the specified SMARTCARD module.
  * @retval Status, see @ref ald_status_t.
  */
205
ald_status_t ald_smartcard_init(smartcard_handle_t *hperh)
W
wangyq2018 已提交
206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241
{
    assert_param(IS_USART_WORD_LENGTH(hperh->init.word_length));
    assert_param(IS_USART_STOPBITS(hperh->init.stop_bits));
    assert_param(IS_USART_PARITY(hperh->init.parity));
    assert_param(IS_USART(hperh->perh));
    assert_param(IS_FUNC_STATE(hperh->init.nack));
    assert_param(IS_SMARTCARD_PRESCALER(hperh->init.prescaler));

    if (hperh->state == SMARTCARD_STATE_RESET)
        hperh->lock = UNLOCK;

    hperh->state = SMARTCARD_STATE_BUSY;
    SMARTCARD_DISABLE(hperh);

    MODIFY_REG(hperh->perh->GP, USART_GP_PSC_MSK, hperh->init.prescaler << USART_GP_PSC_POSS);
    MODIFY_REG(hperh->perh->GP, USART_GP_GTVAL_MSK, hperh->init.guard_time << USART_GP_GTVAL_POSS);
    smartcard_set_config(hperh);

    CLEAR_BIT(hperh->perh->CON2, USART_CON2_IREN_MSK);
    CLEAR_BIT(hperh->perh->CON2, USART_CON2_HDPSEL_MSK);

    SMARTCARD_ENABLE(hperh);
    MODIFY_REG(hperh->perh->CON2, USART_CON2_NACK_MSK, hperh->init.nack << USART_CON2_NACK_POS);
    SET_BIT(hperh->perh->CON2, USART_CON2_SMARTEN_MSK);

    hperh->err_code = SMARTCARD_ERROR_NONE;
    hperh->state    = SMARTCARD_STATE_READY;
    return OK;
}

/**
  * @brief  Reset SMARTCARD register.
  * @param  hperh: Pointer to a smartcard_handle_t structure that contains
  *         the configuration information for the specified SMARTCARD module.
  * @retval Status, see @ref ald_status_t.
  */
242
ald_status_t ald_smartcard_reset(smartcard_handle_t *hperh)
W
wangyq2018 已提交
243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297
{
    assert_param(IS_USART(hperh->perh));

    hperh->state = SMARTCARD_STATE_BUSY;
    SMARTCARD_DISABLE(hperh);

    WRITE_REG(hperh->perh->CON0, 0x0);
    WRITE_REG(hperh->perh->CON1, 0x0);
    WRITE_REG(hperh->perh->CON2, 0x0);
    WRITE_REG(hperh->perh->BAUDCON, 0x0);
    WRITE_REG(hperh->perh->GP, 0x0);

    hperh->err_code = SMARTCARD_ERROR_NONE;
    hperh->state    = SMARTCARD_STATE_RESET;
    __UNLOCK(hperh);

    return OK;
}
/**
  * @}
  */

/** @defgroup SMARTCARD_Public_Functions_Group2 IO operation functions
  * @brief   SMARTCARD Transmit and Receive functions
  *
  * @verbatim
  ==============================================================================
                         ##### IO operation functions #####
  ==============================================================================
  [..]
    This subsection provides a set of functions allowing to manage the SMARTCARD data transfers.

  [..]
    (#) Smartcard is a single wire half duplex communication protocol.
    The Smartcard interface is designed to support asynchronous protocol Smartcards as
    defined in the ISO 7816-3 standard.
    (#) The USART should be configured as:
        (++) 8 bits plus parity: where M=1 and PCE=1 in the USART_CR1 register
        (++) 1.5 stop bits when transmitting and receiving: where STOP=11 in the USART_CR2 register.

    (#) There are two modes of transfer:
        (++) Blocking mode: The communication is performed in polling mode.
             The library status of all data processing is returned by the same function
             after finishing transfer.
        (++) No-Blocking mode: The communication is performed using Interrupts
             or DMA, the relevant API's return the library status.
             The end of the data processing will be indicated through the
             dedicated SMARTCARD IRQ when using Interrupt mode or the DMA IRQ when
             using DMA mode.
             The hperh->tx_cplt_cbk(), hperh->rx_cplt_cbk() user callbacks
             will be executed respectively at the end of the Transmit or Receive process
             The hperh->error_cbk() user callback will be executed when a communication
             error is detected.

    (#) Blocking mode APIs are :
298 299
        (++) ald_smartcard_send()
        (++) ald_smartcard_recv()
W
wangyq2018 已提交
300 301

    (#) Non Blocking mode APIs with Interrupt are :
302 303 304
        (++) ald_smartcard_send_by_it()
        (++) ald_smartcard_recv_by_it()
        (++) ald_smartcard_irq_handler()
W
wangyq2018 已提交
305 306

    (#) Non Blocking mode functions with DMA are :
307 308
        (++) ald_smartcard_send_by_dma()
        (++) ald_smartcard_recv_by_dma()
W
wangyq2018 已提交
309 310 311 312 313 314 315 316 317 318 319 320 321 322

  * @endverbatim
  * @{
  */

/**
  * @brief  Sends an amount of data in blocking mode.
  * @param  hperh: Pointer to a smartcard_handle_t structure that contains
  *         the configuration information for the specified SMARTCARD module.
  * @param  buf: Pointer to data buffer
  * @param  size: Amount of data to be sent
  * @param  timeout: Specify timeout value
  * @retval Status, see @ref ald_status_t.
  */
323
ald_status_t ald_smartcard_send(smartcard_handle_t *hperh, uint8_t *buf, uint16_t size, uint32_t timeout)
W
wangyq2018 已提交
324 325 326
{
    if ((hperh->state != SMARTCARD_STATE_READY) && (hperh->state != SMARTCARD_STATE_BUSY_RX))
        return BUSY;
327

W
wangyq2018 已提交
328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371
    if ((buf == NULL) || (size == 0))
        return  ERROR;

    __LOCK(hperh);
    hperh->err_code = SMARTCARD_ERROR_NONE;
    SET_BIT(hperh->state, USART_STATE_TX_MASK);

    hperh->tx_size  = size;
    hperh->tx_count = size;

    while (hperh->tx_count-- > 0)
    {
        if (smartcard_wait_flag(hperh, USART_FLAG_TXE, SET, timeout) != OK)
        {
            hperh->state = SMARTCARD_STATE_READY;
            __UNLOCK(hperh);
            return TIMEOUT;
        }

        WRITE_REG(hperh->perh->DATA, *buf++);
    }

    if (smartcard_wait_flag(hperh, USART_FLAG_TC, SET, timeout) != OK)
    {
        hperh->state = SMARTCARD_STATE_READY;
        __UNLOCK(hperh);
        return TIMEOUT;
    }

    CLEAR_BIT(hperh->state, USART_STATE_TX_MASK);
    __UNLOCK(hperh);

    return OK;
}

/**
  * @brief  Receive an amount of data in blocking mode.
  * @param  hperh: Pointer to a smartcard_handle_t structure that contains
  *         the configuration information for the specified SMARTCARD module.
  * @param  buf: Pointer to data buffer
  * @param  size: Amount of data to be received
  * @param  timeout: Specify timeout value
  * @retval Status, see @ref ald_status_t.
  */
372
ald_status_t ald_smartcard_recv(smartcard_handle_t *hperh, uint8_t *buf, uint16_t size, uint32_t timeout)
W
wangyq2018 已提交
373 374 375
{
    if ((hperh->state != SMARTCARD_STATE_READY) && (hperh->state != SMARTCARD_STATE_BUSY_TX))
        return BUSY;
376

W
wangyq2018 已提交
377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412
    if ((buf == NULL) || (size == 0))
        return  ERROR;

    __LOCK(hperh);
    hperh->err_code = SMARTCARD_ERROR_NONE;
    SET_BIT(hperh->state, USART_STATE_RX_MASK);

    hperh->rx_size  = size;
    hperh->rx_count = size;

    while (hperh->rx_count-- > 0)
    {
        if (smartcard_wait_flag(hperh, USART_FLAG_RXNE, SET, timeout) != OK)
        {
            hperh->state = SMARTCARD_STATE_READY;
            __UNLOCK(hperh);
            return TIMEOUT;
        }

        *buf++ = (uint8_t)(hperh->perh->DATA & 0xFF);
    }

    __UNLOCK(hperh);
    CLEAR_BIT(hperh->state, USART_STATE_RX_MASK);

    return OK;
}

/**
  * @brief  Sends an amount of data in non-blocking mode.
  * @param  hperh: Pointer to a smartcard_handle_t structure that contains
  *         the configuration information for the specified SMARTCARD module.
  * @param  buf: Pointer to data buffer
  * @param  size: Amount of data to be sent
  * @retval Status, see @ref ald_status_t.
  */
413
ald_status_t ald_smartcard_send_by_it(smartcard_handle_t *hperh, uint8_t *buf, uint16_t size)
W
wangyq2018 已提交
414 415 416
{
    if ((hperh->state != SMARTCARD_STATE_READY) && (hperh->state != SMARTCARD_STATE_BUSY_RX))
        return BUSY;
417

W
wangyq2018 已提交
418 419 420 421 422 423 424 425 426 427 428 429
    if ((buf == NULL) || (size == 0))
        return ERROR;

    __LOCK(hperh);
    SET_BIT(hperh->state, USART_STATE_TX_MASK);

    hperh->tx_buf   = buf;
    hperh->tx_size  = size;
    hperh->tx_count = size;
    hperh->err_code = SMARTCARD_ERROR_NONE;

    __UNLOCK(hperh);
430 431
    ald_usart_interrupt_config((usart_handle_t *)hperh, USART_IT_ERR, ENABLE);
    ald_usart_interrupt_config((usart_handle_t *)hperh, USART_IT_TXE, ENABLE);
W
wangyq2018 已提交
432 433 434 435 436 437 438 439 440 441 442 443

    return OK;
}

/**
  * @brief  Receives an amount of data in non-blocking mode.
  * @param  hperh: Pointer to a smartcard_handle_t structure that contains
  *         the configuration information for the specified SMARTCARD module.
  * @param  buf: Pointer to data buffer
  * @param  size: Amount of data to be received
  * @retval Status, see @ref ald_status_t.
  */
444
ald_status_t ald_smartcard_recv_by_it(smartcard_handle_t *hperh, uint8_t *buf, uint16_t size)
W
wangyq2018 已提交
445 446 447
{
    if ((hperh->state != SMARTCARD_STATE_READY) && (hperh->state != SMARTCARD_STATE_BUSY_TX))
        return BUSY;
448

W
wangyq2018 已提交
449 450 451 452 453 454 455 456 457 458 459 460
    if ((buf == NULL) || (size == 0))
        return ERROR;

    __LOCK(hperh);
    SET_BIT(hperh->state, USART_STATE_RX_MASK);

    hperh->rx_buf   = buf;
    hperh->rx_size  = size;
    hperh->rx_count = size;
    hperh->err_code = SMARTCARD_ERROR_NONE;

    __UNLOCK(hperh);
461 462 463
    ald_usart_interrupt_config((usart_handle_t *)hperh, USART_IT_RXNE, ENABLE);
    ald_usart_interrupt_config((usart_handle_t *)hperh, USART_IT_PE, ENABLE);
    ald_usart_interrupt_config((usart_handle_t *)hperh, USART_IT_ERR, ENABLE);
W
wangyq2018 已提交
464 465 466 467 468 469 470 471 472 473 474 475 476 477

    return OK;
}

#ifdef ALD_DMA
/**
  * @brief  Sends an amount of data in non-blocking mode.
  * @param  hperh: Pointer to a smartcard_handle_t structure that contains
  *         the configuration information for the specified SMARTCARD module.
  * @param  buf: Pointer to data buffer
  * @param  size: Amount of data to be sent
  * @param  channel: DMA channel as USART transmit
  * @retval Status, see @ref ald_status_t.
  */
478
ald_status_t ald_smartcard_send_by_dma(smartcard_handle_t *hperh, uint8_t *buf, uint16_t size, uint8_t channel)
W
wangyq2018 已提交
479 480 481
{
    if ((hperh->state != SMARTCARD_STATE_READY) && (hperh->state != SMARTCARD_STATE_BUSY_RX))
        return BUSY;
482

W
wangyq2018 已提交
483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501
    if ((buf == NULL) || (size == 0))
        return ERROR;

    __LOCK(hperh);
    SET_BIT(hperh->state, USART_STATE_TX_MASK);

    hperh->tx_buf   = buf;
    hperh->tx_size  = size;
    hperh->tx_count = size;
    hperh->err_code = SMARTCARD_ERROR_NONE;

    if (hperh->hdmatx.perh == NULL)
        hperh->hdmatx.perh = DMA0;

    hperh->hdmatx.cplt_cbk = smartcard_dma_send_cplt;
    hperh->hdmatx.cplt_arg = (void *)hperh;
    hperh->hdmatx.err_cbk  = smartcard_dma_error;
    hperh->hdmatx.err_arg  = (void *)hperh;

502
    ald_dma_config_struct(&hperh->hdmatx.config);
W
wangyq2018 已提交
503 504 505 506 507 508 509 510
    hperh->hdmatx.config.src     = (void *)buf;
    hperh->hdmatx.config.dst     = (void *)&hperh->perh->DATA;
    hperh->hdmatx.config.size    = size;
    hperh->hdmatx.config.src_inc = DMA_DATA_INC_BYTE;
    hperh->hdmatx.config.dst_inc = DMA_DATA_INC_NONE;
    hperh->hdmatx.config.msel    = hperh->perh == USART0 ? DMA_MSEL_USART0 : DMA_MSEL_USART1;
    hperh->hdmatx.config.msigsel = DMA_MSIGSEL_USART_TXEMPTY;
    hperh->hdmatx.config.channel = channel;
511
    ald_dma_config_basic(&hperh->hdmatx);
W
wangyq2018 已提交
512

513
    ald_usart_clear_flag_status((usart_handle_t *)hperh, USART_FLAG_TC);
W
wangyq2018 已提交
514
    __UNLOCK(hperh);
515
    ald_usart_dma_req_config((usart_handle_t *)hperh, USART_DMA_REQ_TX, ENABLE);
W
wangyq2018 已提交
516 517 518 519 520 521 522 523 524 525 526 527 528 529

    return OK;
}

/**
  * @brief  Receive an amount of data in non-blocking mode.
  * @param  hperh: Pointer to a smartcard_handle_t structure that contains
  *         the configuration information for the specified SMARTCARD module.
  * @param  buf: Pointer to data buffer
  * @param  size: Amount of data to be received
  * @param  channel: DMA channel as USART transmit
  * @note   When the SMARTCARD parity is enabled (PCE = 1) the data received contain the parity bit.
  * @retval Status, see @ref ald_status_t.
  */
530
ald_status_t ald_smartcard_recv_by_dma(smartcard_handle_t *hperh, uint8_t *buf, uint16_t size, uint8_t channel)
W
wangyq2018 已提交
531 532 533
{
    if ((hperh->state != SMARTCARD_STATE_READY) && (hperh->state != SMARTCARD_STATE_BUSY_TX))
        return BUSY;
534

W
wangyq2018 已提交
535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553
    if ((buf == NULL) || (size == 0))
        return ERROR;

    __LOCK(hperh);
    SET_BIT(hperh->state, USART_STATE_RX_MASK);

    hperh->rx_buf   = buf;
    hperh->rx_size  = size;
    hperh->rx_count = size;
    hperh->err_code = SMARTCARD_ERROR_NONE;

    if (hperh->hdmarx.perh == NULL)
        hperh->hdmarx.perh = DMA0;

    hperh->hdmarx.cplt_cbk = smartcard_dma_recv_cplt;
    hperh->hdmarx.cplt_arg = (void *)hperh;
    hperh->hdmarx.err_cbk  = smartcard_dma_error;
    hperh->hdmarx.err_arg  = (void *)hperh;

554
    ald_dma_config_struct(&hperh->hdmarx.config);
W
wangyq2018 已提交
555 556 557 558 559 560 561 562
    hperh->hdmarx.config.src     = (void *)&hperh->perh->DATA;
    hperh->hdmarx.config.dst     = (void *)buf;
    hperh->hdmarx.config.size    = size;
    hperh->hdmarx.config.src_inc = DMA_DATA_INC_NONE;
    hperh->hdmarx.config.dst_inc = DMA_DATA_INC_BYTE;
    hperh->hdmarx.config.msel    = hperh->perh == USART0 ? DMA_MSEL_USART0 : DMA_MSEL_USART1;
    hperh->hdmarx.config.msigsel = DMA_MSIGSEL_USART_RNR;
    hperh->hdmarx.config.channel = channel;
563
    ald_dma_config_basic(&hperh->hdmarx);
W
wangyq2018 已提交
564 565

    __UNLOCK(hperh);
566
    ald_usart_dma_req_config((usart_handle_t *)hperh, USART_DMA_REQ_RX, ENABLE);
W
wangyq2018 已提交
567 568 569 570 571 572 573 574 575 576 577

    return OK;
}
#endif

/**
  * @brief  This function handles SMARTCARD interrupt request.
  * @param  hperh: Pointer to a smartcard_handle_t structure that contains
  *         the configuration information for the specified SMARTCARD module.
  * @retval None
  */
578
void ald_smartcard_irq_handler(smartcard_handle_t *hperh)
W
wangyq2018 已提交
579 580 581 582 583
{
    uint32_t flag;
    uint32_t source;

    /* Handle parity error */
584 585 586
    flag   = ald_usart_get_flag_status((usart_handle_t *)hperh, USART_FLAG_PE);
    source = ald_usart_get_it_status((usart_handle_t *)hperh, USART_IT_PE);

W
wangyq2018 已提交
587 588 589 590
    if ((flag != RESET) && (source != RESET))
        hperh->err_code |= SMARTCARD_ERROR_PE;

    /* Handle frame error */
591 592 593
    flag   = ald_usart_get_flag_status((usart_handle_t *)hperh, USART_FLAG_FE);
    source = ald_usart_get_it_status((usart_handle_t *)hperh, USART_IT_ERR);

W
wangyq2018 已提交
594 595 596 597
    if ((flag != RESET) && (source != RESET))
        hperh->err_code |= SMARTCARD_ERROR_FE;

    /* Handle noise error */
598 599
    flag = ald_usart_get_flag_status((usart_handle_t *)hperh, USART_FLAG_NE);

W
wangyq2018 已提交
600 601 602 603
    if ((flag != RESET) && (source != RESET))
        hperh->err_code |= SMARTCARD_ERROR_NE;

    /* Handle overrun error */
604 605
    flag = ald_usart_get_flag_status((usart_handle_t *)hperh, USART_FLAG_ORE);

W
wangyq2018 已提交
606 607 608 609
    if ((flag != RESET) && (source != RESET))
        hperh->err_code |= SMARTCARD_ERROR_ORE;

    /* Handle receive */
610 611 612
    flag   = ald_usart_get_flag_status((usart_handle_t *)hperh, USART_FLAG_RXNE);
    source = ald_usart_get_it_status((usart_handle_t *)hperh, USART_IT_RXNE);

W
wangyq2018 已提交
613 614 615 616
    if ((flag != RESET) && (source != RESET))
        __smartcard_recv_by_it(hperh);

    /* Handle transmit */
617 618 619
    flag   = ald_usart_get_flag_status((usart_handle_t *)hperh, USART_FLAG_TXE);
    source = ald_usart_get_it_status((usart_handle_t *)hperh, USART_IT_TXE);

W
wangyq2018 已提交
620 621 622 623
    if ((flag != RESET) && (source != RESET))
        __smartcard_send_by_it(hperh);

    /* Handle transmit complete */
624 625 626
    flag   = ald_usart_get_flag_status((usart_handle_t *)hperh, USART_FLAG_TC);
    source = ald_usart_get_it_status((usart_handle_t *)hperh, USART_IT_TC);

W
wangyq2018 已提交
627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653
    if ((flag != RESET) && (source != RESET))
        __smartcard_end_send_by_it(hperh);

    /* Handle error */
    if (hperh->err_code != SMARTCARD_ERROR_NONE)
    {
        USART_CLEAR_PEFLAG(hperh);
        hperh->state = SMARTCARD_STATE_READY;

        if (hperh->error_cbk)
            hperh->error_cbk(hperh);
    }
}
/**
  * @}
  */

/** @defgroup SMARTCARD_Public_Functions_Group3 Peripheral State and Errors functions
  *  @brief   SMARTCARD State and Errors functions
  *
@verbatim
  ==============================================================================
                  ##### Peripheral State and Errors functions #####
  ==============================================================================
  [..]
    This subsection provides a set of functions allowing to return the State of SmartCard
    communication process and also return Peripheral Errors occurred during communication process
654
     (+) ald_smartcard_get_state() API can be helpful to check in run-time the state
W
wangyq2018 已提交
655
         of the SMARTCARD peripheral.
656
     (+) ald_smartcard_get_error() check in run-time errors that could be occurred during
W
wangyq2018 已提交
657 658 659 660 661 662 663 664 665 666 667 668
         communication.

@endverbatim
  * @{
  */

/**
  * @brief  Returns the SMARTCARD state.
  * @param  hperh: Pointer to a smartcard_handle_t structure that contains
  *         the configuration information for the specified SMARTCARD module.
  * @retval ALD state
  */
669
smartcard_state_t ald_smartcard_get_state(smartcard_handle_t *hperh)
W
wangyq2018 已提交
670 671 672 673 674 675 676 677 678 679
{
    return hperh->state;
}

/**
  * @brief  Return the SMARTCARD error code
  * @param  hperh: Pointer to a smartcard_handle_t structure that contains
  *         the configuration information for the specified SMARTCARD module.
  * @retval SMARTCARD Error Code
  */
680
uint32_t ald_smartcard_get_error(smartcard_handle_t *hperh)
W
wangyq2018 已提交
681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709
{
    return hperh->err_code;
}

/**
  * @}
  */

/**
  * @}
  */

/** @defgroup SMARTCARD_Private_Functions   SMARTCARD Private Functions
  * @brief    SMARTCARD Private functions
  * @{
  */

#ifdef ALD_DMA
/**
  * @brief  DMA SMARTCARD transmit process complete callback.
  * @param  arg: Pointer to a smartcard_handle_t structure that contains
  *         the configuration information for the specified DMA module.
  * @retval None
  */
static void smartcard_dma_send_cplt(void *arg)
{
    smartcard_handle_t *hperh = (smartcard_handle_t *)arg;

    hperh->tx_count = 0;
710 711
    ald_usart_dma_req_config((usart_handle_t *)hperh, USART_DMA_REQ_TX, DISABLE);
    ald_usart_interrupt_config((usart_handle_t *)hperh, USART_IT_TC, ENABLE);
W
wangyq2018 已提交
712 713 714 715 716 717 718 719 720 721 722 723 724 725 726

    return;
}

/**
  * @brief  DMA SMARTCARD receive process complete callback.
  * @param  arg: Pointer to a smartcard_handle_t structure that contains
  *         the configuration information for the specified DMA module.
  * @retval None
  */
static void smartcard_dma_recv_cplt(void *arg)
{
    smartcard_handle_t *hperh = (smartcard_handle_t *)arg;

    hperh->rx_count = 0;
727
    ald_usart_dma_req_config((usart_handle_t *)hperh, USART_DMA_REQ_RX, DISABLE);
W
wangyq2018 已提交
728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750
    CLEAR_BIT(hperh->state, USART_STATE_RX_MASK);

    if (hperh->rx_cplt_cbk)
        hperh->rx_cplt_cbk(hperh);

    return;
}

/**
  * @brief  DMA SMARTCARD communication error callback.
  * @param  arg: Pointer to a smartcard_handle_t structure that contains
  *         the configuration information for the specified DMA module.
  * @retval None
  */
static void smartcard_dma_error(void *arg)
{
    smartcard_handle_t *hperh = (smartcard_handle_t *)arg;

    hperh->rx_count = 0;
    hperh->tx_count = 0;
    hperh->err_code = SMARTCARD_ERROR_DMA;
    hperh->state    = SMARTCARD_STATE_READY;

751 752
    ald_usart_dma_req_config((usart_handle_t *)hperh, USART_DMA_REQ_TX, DISABLE);
    ald_usart_dma_req_config((usart_handle_t *)hperh, USART_DMA_REQ_RX, DISABLE);
W
wangyq2018 已提交
753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776

    if (hperh->error_cbk)
        hperh->error_cbk(hperh);

    return;
}
#endif

/**
  * @brief  This function handles USART Communication Timeout.
  * @param  hperh: Pointer to a usart_handle_t structure that contains
  *         the configuration information for the specified USART module.
  * @param  flag: specifies the USART flag to check.
  * @param  status: The new Flag status (SET or RESET).
  * @param  timeout: Timeout duration
  * @retval Status, see @ref ald_status_t.
  */
static ald_status_t smartcard_wait_flag(smartcard_handle_t *hperh, usart_flag_t flag, flag_status_t status, uint32_t timeout)
{
    uint32_t tick;

    if (timeout == 0)
        return OK;

777
    tick = ald_get_tick();
W
wangyq2018 已提交
778

779
    while ((ald_usart_get_flag_status((usart_handle_t *)hperh, flag)) != status)
W
wangyq2018 已提交
780
    {
781
        if (((ald_get_tick()) - tick) > timeout)
W
wangyq2018 已提交
782
        {
783 784 785 786
            ald_usart_interrupt_config((usart_handle_t *)hperh, USART_IT_TXE, DISABLE);
            ald_usart_interrupt_config((usart_handle_t *)hperh, USART_IT_RXNE, DISABLE);
            ald_usart_interrupt_config((usart_handle_t *)hperh, USART_IT_PE, DISABLE);
            ald_usart_interrupt_config((usart_handle_t *)hperh, USART_IT_ERR, DISABLE);
W
wangyq2018 已提交
787 788 789 790 791 792 793 794 795 796 797 798 799

            return TIMEOUT;
        }
    }

    return OK;
}

/**
  * @brief  Send an amount of data in non-blocking mode.
  * @param  hperh: Pointer to a smartcard_handle_t structure that contains
  *         the configuration information for the specified SMARTCARD module.
  *         Function called under interruption only, once
800
  *         interruptions have been enabled by ald_smartcard_send_by_it()
W
wangyq2018 已提交
801 802 803 804 805 806 807 808 809 810 811
  * @retval Status, see @ref ald_status_t.
  */
static ald_status_t __smartcard_send_by_it(smartcard_handle_t *hperh)
{
    if ((hperh->state != SMARTCARD_STATE_BUSY_TX) && (hperh->state != SMARTCARD_STATE_BUSY_TX_RX))
        return BUSY;

    WRITE_REG(hperh->perh->DATA, *hperh->tx_buf++);

    if (--hperh->tx_count == 0)
    {
812 813
        ald_usart_interrupt_config((usart_handle_t *)hperh, USART_IT_TXE, DISABLE);
        ald_usart_interrupt_config((usart_handle_t *)hperh, USART_IT_TC, ENABLE);
W
wangyq2018 已提交
814 815 816 817 818 819 820 821 822 823 824 825 826 827
    }

    return OK;
}


/**
  * @brief  Wraps up transmission in non blocking mode.
  * @param  hperh: pointer to a smartcard_handle_t structure that contains
  *         the configuration information for the specified SMARTCARD module.
  * @retval Status, see @ref ald_status_t.
  */
static ald_status_t __smartcard_end_send_by_it(smartcard_handle_t *hperh)
{
828
    ald_usart_interrupt_config((usart_handle_t *)hperh, USART_IT_TC, DISABLE);
W
wangyq2018 已提交
829 830 831
    CLEAR_BIT(hperh->state, USART_STATE_TX_MASK);

    if (hperh->state == SMARTCARD_STATE_READY)
832
        ald_usart_interrupt_config((usart_handle_t *)hperh, USART_IT_ERR, DISABLE);
W
wangyq2018 已提交
833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855

    if (hperh->tx_cplt_cbk)
        hperh->tx_cplt_cbk(hperh);

    return OK;
}


/**
  * @brief  Receive an amount of data in non-blocking mode.
  * @param  hperh: Pointer to a smartcard_handle_t structure that contains
  *         the configuration information for the specified SMARTCARD module.
  * @retval Status, see @ref ald_status_t.
  */
static ald_status_t __smartcard_recv_by_it(smartcard_handle_t *hperh)
{
    if ((hperh->state != SMARTCARD_STATE_BUSY_RX) && (hperh->state != SMARTCARD_STATE_BUSY_TX_RX))
        return BUSY;

    *hperh->rx_buf++ = (uint8_t)(hperh->perh->DATA & 0xFF);

    if (--hperh->rx_count == 0)
    {
856 857 858
        ald_usart_interrupt_config((usart_handle_t *)hperh, USART_IT_RXNE, DISABLE);
        ald_usart_interrupt_config((usart_handle_t *)hperh, USART_IT_PE, DISABLE);
        ald_usart_interrupt_config((usart_handle_t *)hperh, USART_IT_ERR, DISABLE);
W
wangyq2018 已提交
859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916
        CLEAR_BIT(hperh->state, USART_STATE_RX_MASK);

        if (hperh->rx_cplt_cbk)
            hperh->rx_cplt_cbk(hperh);
    }

    return OK;
}

/**
  * @brief  Configures the SMARTCARD peripheral.
  * @param  hperh: Pointer to a smartcard_handle_t structure that contains
  *         the configuration information for the specified SMARTCARD module.
  * @retval None
  */
static void smartcard_set_config(smartcard_handle_t *hperh)
{
    uint32_t tmp;
    uint32_t integer;
    uint32_t fractional;

    /* Check the parameters */
    assert_param(IS_USART(hperh->perh));
    assert_param(IS_USART_BAUDRATE(hperh->init.baud));
    assert_param(IS_USART_WORD_LENGTH(hperh->init.word_length));
    assert_param(IS_USART_STOPBITS(hperh->init.stop_bits));
    assert_param(IS_USART_PARITY(hperh->init.parity));
    assert_param(IS_USART_MODE(hperh->init.mode));

    MODIFY_REG(hperh->perh->CON1, USART_CON1_STPLEN_MSK, hperh->init.stop_bits << USART_CON1_STPLEN_POSS);
    tmp = READ_REG(hperh->perh->CON0);
    MODIFY_REG(tmp, USART_CON0_DLEN_MSK, hperh->init.word_length << USART_CON0_DLEN_POS);

    if (hperh->init.parity == USART_PARITY_NONE)
        CLEAR_BIT(tmp, USART_CON0_PEN_MSK);
    else
        SET_BIT(tmp, USART_CON0_PEN_MSK);

    if (hperh->init.parity == USART_PARITY_ODD)
        SET_BIT(tmp, USART_CON0_PSEL_MSK);
    else
        CLEAR_BIT(tmp, USART_CON0_PSEL_MSK);

    WRITE_REG(hperh->perh->CON0, tmp);
    CLEAR_BIT(hperh->perh->CON2, USART_CON2_RTSEN_MSK);
    CLEAR_BIT(hperh->perh->CON2, USART_CON2_CTSEN_MSK);
    MODIFY_REG(hperh->perh->CON0, USART_CON0_RXEN_MSK, (hperh->init.mode & 0x1) << USART_CON0_RXEN_POS);
    MODIFY_REG(hperh->perh->CON0, USART_CON0_TXEN_MSK, ((hperh->init.mode >> 1) & 0x1) << USART_CON0_TXEN_POS);
    tmp = READ_REG(hperh->perh->CON1);
    SET_BIT(tmp, USART_CON1_SCKEN_MSK);
    MODIFY_REG(tmp, USART_CON1_SCKPOL_MSK, hperh->init.polarity << USART_CON1_SCKPOL_POS);
    MODIFY_REG(tmp, USART_CON1_SCKPHA_MSK, hperh->init.phase << USART_CON1_SCKPHA_POS);
    MODIFY_REG(tmp, USART_CON1_LBCP_MSK, hperh->init.last_bit << USART_CON1_LBCP_POS);

    /* Determine the integer part */
    if (READ_BIT(hperh->perh->CON0, (1 << 15)))
    {
        /* Integer part computing in case Oversampling mode is 8 Samples */
917
        integer = ((25 * ald_cmu_get_pclk1_clock()) / (2 * (hperh->init.baud)));
W
wangyq2018 已提交
918 919 920 921
    }
    else
    {
        /* Integer part computing in case Oversampling mode is 16 Samples */
922
        integer = ((25 * ald_cmu_get_pclk1_clock()) / (4 * (hperh->init.baud)));
W
wangyq2018 已提交
923
    }
924

W
wangyq2018 已提交
925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951
    tmp = (integer / 100) << 4;

    /* Determine the fractional part */
    fractional = integer - (100 * (tmp >> 4));

    /* Implement the fractional part in the register */
    if (READ_BIT(hperh->perh->CON0, (1 << 15)))
        tmp |= ((((fractional * 8) + 50) / 100)) & ((uint8_t)0x07);
    else
        tmp |= ((((fractional * 16) + 50) / 100)) & ((uint8_t)0x0F);

    WRITE_REG(hperh->perh->BAUDCON, (uint16_t)tmp);
    return;
}

/**
  * @}
  */

#endif /* ALD_SMARTCARD */
/**
  * @}
  */

/**
  * @}
  */