ald_usart.c 82.5 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 32 33 34
/**
  *********************************************************************************
  *
  * @file    ald_usart.c
  * @brief   USART module driver.
  *          This file provides firmware functions to manage the following
  *          functionalities of the Universal Synchronous Asynchronous Receiver Transmitter (USART) peripheral:
  *           + Initialization functions
  *           + IO operation functions
  *           + Peripheral Control 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 USART ALD driver can be used as follows:

    (#) Declare a usart_handle_t handle structure.

    (#) Initialize the USART handle:
        (##) Enable the USARTx interface clock.
        (##) USART pins configuration:
            (+++) Enable the clock for the USART GPIOs.
            (+++) Configure the USART pins (TX as alternate function pull-up, RX as alternate function Input).
35 36
        (##) NVIC configuration if you need to use interrupt process (ald_usart_send_by_it()
             and ald_usart_recv_by_it() APIs):
W
wangyq2018 已提交
37 38
            (+++) Configure the USARTx interrupt priority.
            (+++) Enable the NVIC USART IRQ handle.
39 40
        (##) DMA Configuration if you need to use DMA process (ald_usart_send_by_dma()
             and ald_usart_recv_by_dma() APIs):
W
wangyq2018 已提交
41 42 43 44 45 46 47 48 49 50 51
            (+++) 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 initialized DMA handle to the USART DMA Tx/Rx handle.

    (#) Program the baud rate, word length, stop bit, parity, hardware
        flow control and mode(Receiver/Transmitter) in the hperh Init structure.

    (#) For the USART asynchronous mode, initialize the USART registers by calling
52
        the ald_usart_init() API.
W
wangyq2018 已提交
53 54

    (#) For the USART Half duplex mode, initialize the USART registers by calling
55
        the ald_usart_half_duplex_init() API.
W
wangyq2018 已提交
56 57 58 59

    (#) For the LIN mode, initialize the USART registers by calling the usart_lin_init() API.

    (#) For the Multi-Processor mode, initialize the USART registers by calling
60
        the ald_usart_multi_processor_init() API.
W
wangyq2018 已提交
61 62 63 64

     [..]
       (@) The specific USART interrupts (Transmission complete interrupt,
            RXNE interrupt and Error Interrupts) will be managed using the function
65
            ald_usart_interrupt_config inside the transmit and receive process.
W
wangyq2018 已提交
66 67 68 69 70 71 72

     [..]
        Three operation modes are available within this driver :

     *** Polling mode IO operation ***
     =================================
     [..] Asynchronous:
73 74
       (+) Send an amount of data in blocking mode using ald_usart_send()
       (+) Receive an amount of data in blocking mode using ald_usart_recv()
W
wangyq2018 已提交
75 76

     [..] Synchronous:
77 78
       (+) Send an amount of data in blocking mode using ald_usart_send_sync()
       (+) Receive an amount of data in blocking mode using ald_usart_recv_sync()
W
wangyq2018 已提交
79 80 81 82

     *** Interrupt mode IO operation ***
     ===================================
     [..] Asynchronous:
83
       (+) Send an amount of data in non blocking mode using ald_usart_send_by_it()
W
wangyq2018 已提交
84 85 86 87 88 89 90 91
       (+) 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()
       (+) Receive an amount of data in non blocking mode using USART_recv_by_it()
       (+) 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()
     [..] Synchronous:
92
       (+) Send an amount of data in non blocking mode using ald_usart_send_by_it_sync()
W
wangyq2018 已提交
93 94 95 96 97 98 99 100 101 102 103
       (+) 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()
       (+) Receive an amount of data in non blocking mode using USART_recv_by_it_sync()
       (+) 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 ***
     ==============================
     [..] Asynchronous:
104
       (+) Send an amount of data in non blocking mode (DMA) using ald_usart_send_by_dma()
W
wangyq2018 已提交
105 106
       (+) 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()
107
       (+) Receive an amount of data in non blocking mode (DMA) using ald_usart_recv_by_dma()
W
wangyq2018 已提交
108 109 110 111 112
       (+) 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()
     [..] Synchronous:
113
       (+) Send an amount of data in non blocking mode (DMA) using ald_usart_send_by_dma_sync()
W
wangyq2018 已提交
114 115
       (+) 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()
116
       (+) Receive an amount of data in non blocking mode (DMA) using ald_usart_recv_by_dma_sync()
W
wangyq2018 已提交
117 118 119 120 121
       (+) 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()
     [..] Utilities:
122 123 124
       (+) Pause the DMA Transfer using ald_usart_dma_pause()
       (+) Resume the DMA Transfer using ald_usart_dma_resume()
       (+) Stop the DMA Transfer using ald_usart_dma_stop()
W
wangyq2018 已提交
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 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214

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

      (+) USART_ENABLE: Enable the USART peripheral
      (+) USART_DISABLE: Disable the USART peripheral
      (+) USART_RESET_HANDLE_STATE : Reset USART handle
      (+) USART_CLEAR_PEFLAG : Clear PE flag
      (+) USART_CLEAR_FEFLAG: Clear FE flag
      (+) USART_CLEAR_NEFLAG: Clear NE flag
      (+) USART_CLEAR_OREFLAG: Clear voerrun flag
      (+) USART_CLEAR_IDLEFLAG : Clear IDLE flag
      (+) USART_HWCONTROL_CTS_ENABLE: Enable CTS flow control
      (+) USART_HWCONTROL_CTS_DISABLE: Disable CTS flow control
      (+) USART_HWCONTROL_RTS_ENABLE: Enable RTS flow control
      (+) USART_HWCONTROL_RTS_DISABLE: Disable RTS flow control

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

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

#include "ald_usart.h"
#include "ald_cmu.h"


/** @addtogroup ES32FXXX_ALD
  * @{
  */

/** @defgroup USART USART
  * @brief USART module driver
  * @{
  */
#ifdef ALD_USART

/** @defgroup USART_Private_Variables USART Private Variables
  * @{
  */
uint8_t __frame_mode = 0;
/**
  * @}
  */

/** @addtogroup USART_Private_Functions   USART Private Functions
  * @{
  */
static void usart_set_config(usart_handle_t *hperh);
static ald_status_t __usart_send_by_it(usart_handle_t *hperh);
static ald_status_t __usart_end_send_by_it(usart_handle_t *hperh);
static ald_status_t __usart_recv_by_it(usart_handle_t *hperh);
static ald_status_t __usart_recv_frame_cplt(usart_handle_t *hperh);
static ald_status_t __usart_recv_by_it_sync(usart_handle_t *hperh);
static ald_status_t __usart_send_recv_by_it_sync(usart_handle_t *hperh);
#ifdef ALD_DMA
    static void usart_dma_send_cplt(void *arg);
    static void usart_dma_recv_cplt(void *arg);
    static void usart_dma_error(void *arg);
#endif
static ald_status_t usart_wait_flag(usart_handle_t *hperh, usart_flag_t flag, flag_status_t status, uint32_t timeout);
/**
  * @}
  */

/** @defgroup USART_Public_Functions USART Public Functions
  * @{
  */

/** @defgroup USART_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 USARTx or the USARTy
    in asynchronous or synchronous mode.
      (+) For the asynchronous mode only these parameters can be configured:
        (++) Baud rate
        (++) Word length
        (++) Stop bit
        (++) Parity
        (++) Hardware flow control
        (++) Receiver/transmitter modes
    [..]
215 216
    The ald_usart_init(), ald_usart_half_duplex_init(), usart_lin_init(), ald_usart_multi_processor_init()
    and ald_usart_clock_init() APIs follow respectively the USART asynchronous, USART Half duplex,
W
wangyq2018 已提交
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 242 243 244 245 246 247
    LIN, Multi-Processor and synchronous configuration procedures.

    @endverbatim
  * @{
  */

/*
  Additionnal remark: If the parity is enabled, then the MSB bit of the data written
                      in the data register is transmitted but is changed by the parity bit.
                      Depending on the frame length defined by the M bit (8-bits or 9-bits),
                      the possible USART frame formats are as listed in the following table:
    +-------------------------------------------------------------+
    |   M bit |  PCE bit  |            USART frame                 |
    |---------------------|---------------------------------------|
    |    0    |    0      |    | SB | 8 bit data | STB |          |
    |---------|-----------|---------------------------------------|
    |    0    |    1      |    | SB | 7 bit data | PB | STB |     |
    |---------|-----------|---------------------------------------|
    |    1    |    0      |    | SB | 9 bit data | STB |          |
    |---------|-----------|---------------------------------------|
    |    1    |    1      |    | SB | 8 bit data | PB | STB |     |
    +-------------------------------------------------------------+
*/


/**
  * @brief  Reset the USART peripheral.
  * @param  hperh: Pointer to a usart_handle_t structure that contains
  *         the configuration information for the specified USART module.
  * @retval None
  */
248
void ald_usart_reset(usart_handle_t *hperh)
W
wangyq2018 已提交
249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272
{
    assert_param(IS_USART(hperh->perh));

    hperh->state = USART_STATE_BUSY;
    USART_DISABLE(hperh);

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

    hperh->err_code = USART_ERROR_NONE;
    hperh->state    = USART_STATE_RESET;

    __UNLOCK(hperh);
    return;
}

/**
  * @brief  Initializes the USART mode according to the specified parameters in
  *         the usart_init_t and create the associated handle.
  * @param  hperh: Pointer to a usart_handle_t structure that contains
  *         the configuration information for the specified USART module.
  * @retval Status, see @ref ald_status_t.
  */
273
ald_status_t ald_usart_init(usart_handle_t *hperh)
W
wangyq2018 已提交
274 275 276 277 278 279
{
    assert_param(IS_USART(hperh->perh));
    assert_param(IS_USART_HARDWARE_FLOW_CONTROL(hperh->init.fctl));
    assert_param(IS_USART_WORD_LENGTH(hperh->init.word_length));
    assert_param(IS_FUNC_STATE(hperh->init.over_sampling));

280
    ald_usart_reset(hperh);
W
wangyq2018 已提交
281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307
    hperh->state = USART_STATE_BUSY;
    USART_DISABLE(hperh);
    usart_set_config(hperh);

    /* In asynchronous mode, the following bits must be kept cleared:
     *   - LINEN and CLKEN bits in the USART_CR2 register,
     *   - SCEN, HDSEL and IREN  bits in the USART_CR3 register.
     */
    CLEAR_BIT(hperh->perh->CON1, USART_CON1_SCKEN_MSK);
    CLEAR_BIT(hperh->perh->CON2, USART_CON2_SMARTEN_MSK);
    CLEAR_BIT(hperh->perh->CON2, USART_CON2_HDPSEL_MSK);
    CLEAR_BIT(hperh->perh->CON2, USART_CON2_IREN_MSK);

    hperh->err_code = USART_ERROR_NONE;
    hperh->state    = USART_STATE_READY;
    USART_ENABLE(hperh);

    return OK;
}

/**
  * @brief  Initializes the half-duplex mode according to the specified
  *         parameters in the usart_init_t and create the associated handle.
  * @param  hperh: Pointer to a usart_handle_t structure that contains
  *         the configuration information for the specified USART module.
  * @retval Status, see @ref ald_status_t.
  */
308
ald_status_t ald_usart_half_duplex_init(usart_handle_t *hperh)
W
wangyq2018 已提交
309 310 311 312 313
{
    assert_param(IS_USART(hperh->perh));
    assert_param(IS_USART_WORD_LENGTH(hperh->init.word_length));
    assert_param(IS_FUNC_STATE(hperh->init.over_sampling));

314
    ald_usart_reset(hperh);
W
wangyq2018 已提交
315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345
    hperh->state = USART_STATE_BUSY;
    USART_DISABLE(hperh);
    usart_set_config(hperh);

    /* In half-duplex mode, the following bits must be kept cleared:
     *   - LINEN and CLKEN bits in the USART_CR2 register,
     *   - SCEN and IREN bits in the USART_CR3 register.*/
    CLEAR_BIT(hperh->perh->CON1, USART_CON1_SCKEN_MSK);
    CLEAR_BIT(hperh->perh->CON2, USART_CON2_SMARTEN_MSK);
    CLEAR_BIT(hperh->perh->CON2, USART_CON2_IREN_MSK);
    SET_BIT(hperh->perh->CON2, USART_CON2_HDPSEL_MSK);

    hperh->err_code = USART_ERROR_NONE;
    hperh->state    = USART_STATE_READY;
    USART_ENABLE(hperh);

    return OK;
}

/**
  * @brief  Initializes the Multi-Processor mode according to the specified
  *         parameters in the usart_init_t and create the associated handle.
  * @param  hperh: Pointer to a usart_handle_t structure that contains
  *         the configuration information for the specified USART module.
  * @param  addr: USART node address
  * @param  wakeup: specifies the USART wakeup method.
  *         This parameter can be one of the following values:
  *            @arg USART_WAKEUP_IDLE: Wakeup by an idle line detection
  *            @arg USART_WAKEUP_ADDR: Wakeup by an address mark
  * @retval Status, see @ref ald_status_t.
  */
346
ald_status_t ald_usart_multi_processor_init(usart_handle_t *hperh, uint8_t addr, usart_wakeup_t wakeup)
W
wangyq2018 已提交
347 348 349 350 351 352 353
{
    assert_param(IS_USART(hperh->perh));
    assert_param(IS_USART_WAKEUP(wakeup));
    assert_param(IS_USART_ADDRESS(addr));
    assert_param(IS_USART_WORD_LENGTH(hperh->init.word_length));
    assert_param(IS_FUNC_STATE(hperh->init.over_sampling));

354
    ald_usart_reset(hperh);
W
wangyq2018 已提交
355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383
    hperh->state = USART_STATE_BUSY;
    USART_DISABLE(hperh);
    usart_set_config(hperh);

    /* In Multi-Processor mode, the following bits must be kept cleared:
     *   - LINEN and CLKEN bits in the USART_CR2 register,
     *   - SCEN, HDSEL and IREN  bits in the USART_CR3 register */
    CLEAR_BIT(hperh->perh->CON1, USART_CON1_SCKEN_MSK);
    CLEAR_BIT(hperh->perh->CON2, USART_CON2_SMARTEN_MSK);
    CLEAR_BIT(hperh->perh->CON2, USART_CON2_HDPSEL_MSK);
    CLEAR_BIT(hperh->perh->CON2, USART_CON2_IREN_MSK);
    MODIFY_REG(hperh->perh->CON1, USART_CON1_ADDR_MSK, addr << USART_CON1_ADDR_POSS);
    MODIFY_REG(hperh->perh->CON0, USART_CON0_WKMOD_MSK, wakeup << USART_CON0_WKMOD_POS);

    hperh->err_code = USART_ERROR_NONE;
    hperh->state    = USART_STATE_READY;
    USART_ENABLE(hperh);

    return OK;
}

/**
  * @brief  Initializes the synchronization mode according to the specified
  *         parameters in the usart_init_t and usart_clock_init_t.
  * @param  hperh: Pointer to a usart_handle_t structure that contains
  *         the configuration information for the specified USART module.
  * @param  init: USART Clock Init Structure.
  * @retval Status, see @ref ald_status_t.
  */
384
ald_status_t ald_usart_clock_init(usart_handle_t *hperh, usart_clock_init_t *init)
W
wangyq2018 已提交
385 386 387 388 389
{
    assert_param(IS_USART(hperh->perh));
    assert_param(IS_USART_WORD_LENGTH(hperh->init.word_length));
    assert_param(IS_FUNC_STATE(hperh->init.over_sampling));

390
    ald_usart_reset(hperh);
W
wangyq2018 已提交
391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447
    hperh->state = USART_STATE_BUSY;
    USART_DISABLE(hperh);
    usart_set_config(hperh);

    /* In Multi-Processor mode, the following bits must be kept cleared:
     *   - LINEN and CLKEN bits in the USART_CR2 register,
     *   - SCEN, HDSEL and IREN  bits in the USART_CR3 register */
    CLEAR_BIT(hperh->perh->CON2, USART_CON2_SMARTEN_MSK);
    CLEAR_BIT(hperh->perh->CON2, USART_CON2_HDPSEL_MSK);
    CLEAR_BIT(hperh->perh->CON2, USART_CON2_IREN_MSK);
    MODIFY_REG(hperh->perh->CON1, USART_CON1_SCKEN_MSK, init->clk << USART_CON1_SCKEN_POS);
    MODIFY_REG(hperh->perh->CON1, USART_CON1_SCKPOL_MSK, init->polarity << USART_CON1_SCKPOL_POS);
    MODIFY_REG(hperh->perh->CON1, USART_CON1_SCKPHA_MSK, init->phase << USART_CON1_SCKPHA_POS);
    MODIFY_REG(hperh->perh->CON1, USART_CON1_LBCP_MSK, init->last_bit << USART_CON1_LBCP_POS);

    hperh->err_code = USART_ERROR_NONE;
    hperh->state    = USART_STATE_READY;
    USART_ENABLE(hperh);

    return OK;
}

/**
  * @}
  */

/** @defgroup USART_Public_Functions_Group2 IO operation functions
  * @brief USART Transmit and Receive functions
  * @{
  */

/** @defgroup USART_Public_Functions_Group2_1 Asynchronization IO operation functions
  * @brief Asynchronization IO operation functions
  *
  * @verbatim
  ==============================================================================
                      ##### IO operation functions #####
  ==============================================================================
  [..]
    This subsection provides a set of functions allowing to manage the USART asynchronous
    and Half duplex data transfers.

    (#) There are two modes of transfer:
       (++) Blocking mode: The communication is performed in polling mode.
            The Status of all data processing is returned by the same function
            after finishing transfer.
       (++) Non blocking mode: The communication is performed using Interrupts
            or DMA, these APIs return the Status.
            The end of the data processing will be indicated through the
            dedicated USART 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:
448 449
        (++) ald_usart_send()
        (++) ald_usart_recv()
W
wangyq2018 已提交
450 451

    (#) Non Blocking mode APIs with Interrupt are:
452 453
        (++) ald_usart_send_by_it()
        (++) ald_usart_recv_by_it()
W
wangyq2018 已提交
454 455 456
        (++) urart_irq_handle()

    (#) Non Blocking mode functions with DMA are:
457 458 459 460 461
        (++) ald_usart_send_by_dma()
        (++) ald_usart_recv_by_dma()
        (++) ald_usart_dma_pause()
        (++) ald_usart_dma_resume()
        (++) ald_usart_dma_stop()
W
wangyq2018 已提交
462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485

    (#) A set of Transfer Complete Callbacks are provided in non blocking mode:
        (++) hperh->tx_cplt_cbk()
        (++) hperh->rx_cplt_cbk()
        (++) hperh->error_cbk()

    [..]
      (@) In the Half duplex communication, it is forbidden to run the transmit
          and receive process in parallel, the USART state USART_STATE_BUSY_TX_RX
          can't be useful.

    @endverbatim
  * @{
  */

/**
  * @brief  Sends an amount of data in blocking mode.
  * @param  hperh: Pointer to a usart_handle_t structure that contains
  *         the configuration information for the specified USART module.
  * @param  buf: Pointer to data buffer
  * @param  size: Amount of data to be sent
  * @param  timeout: Timeout duration
  * @retval Status, see @ref ald_status_t.
  */
486
ald_status_t ald_usart_send(usart_handle_t *hperh, uint8_t *buf, uint16_t size, uint32_t timeout)
W
wangyq2018 已提交
487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549
{
    if ((hperh->state != USART_STATE_READY) && (hperh->state != USART_STATE_BUSY_RX))
        return BUSY;

    if ((buf == NULL) || (size == 0))
        return ERROR;

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

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

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

        if (hperh->init.word_length == USART_WORD_LENGTH_9B)
        {
            if (hperh->init.parity == USART_PARITY_NONE)
            {
                WRITE_REG(hperh->perh->DATA, (*(uint16_t *)buf & (uint16_t)0x01FF));
                buf += 2;
            }
            else
            {
                WRITE_REG(hperh->perh->DATA, *buf++);
            }
        }
        else
        {
            WRITE_REG(hperh->perh->DATA, *buf++);
        }
    }

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

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

    return OK;
}

/**
  * @brief  Receives an amount of data in blocking mode.
  * @param  hperh: Pointer to a usart_handle_t structure that contains
  *         the configuration information for the specified USART module.
  * @param  buf: Pointer to data buffer
  * @param  size: Amount of data to be received
  * @param  timeout: Timeout duration
  * @retval Status, see @ref ald_status_t.
  */
550
ald_status_t ald_usart_recv(usart_handle_t *hperh, uint8_t *buf, uint16_t size, uint32_t timeout)
W
wangyq2018 已提交
551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609
{
    if ((hperh->state != USART_STATE_READY) && (hperh->state != USART_STATE_BUSY_TX))
        return BUSY;

    if ((buf == NULL) || (size == 0))
        return  ERROR;

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

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

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

        if (hperh->init.word_length == USART_WORD_LENGTH_9B)
        {
            if (hperh->init.parity == USART_PARITY_NONE)
            {
                *(uint16_t *)buf = (uint16_t)(hperh->perh->DATA & 0x1FF);
                buf += 2;
            }
            else
            {
                *buf = (uint8_t)(hperh->perh->DATA & 0xFF);
                buf += 1;
            }
        }
        else
        {
            if (hperh->init.parity == USART_PARITY_NONE)
                *buf++ = (uint8_t)(hperh->perh->DATA & 0xFF);
            else
                *buf++ = (uint8_t)(hperh->perh->DATA & 0x7F);
        }
    }

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

    return OK;
}

/**
  * @brief  Sends an amount of data in non blocking mode.
  * @param  hperh: Pointer to a usart_handle_t structure that contains
  *         the configuration information for the specified USART module.
  * @param  buf: Pointer to data buffer
  * @param  size: Amount of data to be sent
  * @retval Status, see @ref ald_status_t.
  */
610
ald_status_t ald_usart_send_by_it(usart_handle_t *hperh, uint8_t *buf, uint16_t size)
W
wangyq2018 已提交
611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626
{
    if ((hperh->state != USART_STATE_READY) && (hperh->state != USART_STATE_BUSY_RX))
        return BUSY;

    if ((buf == NULL) || (size == 0))
        return ERROR;

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

    hperh->tx_buf   = buf;
    hperh->rx_size  = size;
    hperh->tx_count = size;
    hperh->err_code = USART_ERROR_NONE;

    __UNLOCK(hperh);
627
    ald_usart_interrupt_config(hperh, USART_IT_TXE, ENABLE);
W
wangyq2018 已提交
628 629 630 631 632 633 634 635 636 637 638 639

    return OK;
}

/**
  * @brief  Receives an amount of data in non blocking mode
  * @param  hperh: Pointer to a usart_handle_t structure that contains
  *         the configuration information for the specified USART module.
  * @param  buf: Pointer to data buffer
  * @param  size: Amount of data to be received
  * @retval Status, see @ref ald_status_t.
  */
640
ald_status_t ald_usart_recv_by_it(usart_handle_t *hperh, uint8_t *buf, uint16_t size)
W
wangyq2018 已提交
641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656
{
    if ((hperh->state != USART_STATE_READY) && (hperh->state != USART_STATE_BUSY_TX))
        return BUSY;

    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 = USART_ERROR_NONE;

    __UNLOCK(hperh);
657 658 659
    ald_usart_interrupt_config(hperh, USART_IT_PE, ENABLE);
    ald_usart_interrupt_config(hperh, USART_IT_ERR, ENABLE);
    ald_usart_interrupt_config(hperh, USART_IT_RXNE, ENABLE);
W
wangyq2018 已提交
660 661 662 663 664 665 666 667 668 669 670 671

    return OK;
}

/**
  * @brief  Receives an frame in interrupt mode
  * @param  hperh: Pointer to a usart_handle_t structure that contains
  *         the configuration information for the specified USART module.
  * @param  buf: Pointer to data buffer
  * @param  size: Maximum amount of data to be received
  * @retval Status, see @ref ald_status_t.
  */
672
ald_status_t ald_usart_recv_frame_by_it(usart_handle_t *hperh, uint8_t *buf, uint16_t size)
W
wangyq2018 已提交
673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688
{
    if ((hperh->state != USART_STATE_READY) && (hperh->state != USART_STATE_BUSY_TX))
        return BUSY;

    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 = USART_ERROR_NONE;

    __UNLOCK(hperh);
689 690 691
    ald_usart_interrupt_config(hperh, USART_IT_PE, ENABLE);
    ald_usart_interrupt_config(hperh, USART_IT_ERR, ENABLE);
    ald_usart_interrupt_config(hperh, USART_IT_RXNE, ENABLE);
W
wangyq2018 已提交
692 693 694 695 696 697 698 699 700 701 702 703 704 705 706
    __frame_mode = 1;

    return OK;
}

#ifdef ALD_DMA
/**
  * @brief  Sends an amount of data in non blocking mode.
  * @param  hperh: Pointer to a usart_handle_t structure that contains
  *         the configuration information for the specified USART 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.
  */
707
ald_status_t ald_usart_send_by_dma(usart_handle_t *hperh, uint8_t *buf, uint16_t size, uint8_t channel)
W
wangyq2018 已提交
708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732
{
    if ((hperh->state != USART_STATE_READY) && (hperh->state != USART_STATE_BUSY_RX))
        return BUSY;

    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 = USART_ERROR_NONE;

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

    /* Configure callback function */
    hperh->hdmatx.cplt_cbk = usart_dma_send_cplt;
    hperh->hdmatx.cplt_arg = (void *)hperh;
    hperh->hdmatx.err_cbk  = usart_dma_error;
    hperh->hdmatx.err_arg  = (void *)hperh;

    /* Configure USART DMA transmit */
733
    ald_dma_config_struct(&hperh->hdmatx.config);
W
wangyq2018 已提交
734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749
    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;

    if ((hperh->init.word_length == USART_WORD_LENGTH_9B)
            && (hperh->init.parity == USART_PARITY_NONE))
    {
        hperh->hdmatx.config.src_inc    = DMA_DATA_INC_HALFWORD;
        hperh->hdmatx.config.data_width = DMA_DATA_SIZE_HALFWORD;
    }

750
    ald_dma_config_basic(&hperh->hdmatx);
W
wangyq2018 已提交
751 752

    __UNLOCK(hperh);
753
    ald_usart_clear_flag_status(hperh, USART_FLAG_TC);
W
wangyq2018 已提交
754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769
    SET_BIT(hperh->perh->CON2, USART_CON2_TXDMAEN_MSK);

    return OK;
}

/**
  * @brief  Receives an amount of data in non blocking mode.
  * @param  hperh: Pointer to a usart_handle_t structure that contains
  *         the configuration information for the specified USART module.
  * @param  buf: Pointer to data buffer
  * @param  size: Amount of data to be received
  * @param  channel: DMA channel as USART receive
  * @note   When the USART parity is enabled (PCE = 1), the received data contain
  *         the parity bit (MSB position)
  * @retval Status, see @ref ald_status_t.
  */
770
ald_status_t ald_usart_recv_by_dma(usart_handle_t *hperh, uint8_t *buf, uint16_t size, uint8_t channel)
W
wangyq2018 已提交
771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794
{
    if ((hperh->state != USART_STATE_READY) && (hperh->state != USART_STATE_BUSY_TX))
        return BUSY;

    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->err_code = USART_ERROR_NONE;

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

    /* Configure callback function */
    hperh->hdmarx.cplt_cbk = usart_dma_recv_cplt;
    hperh->hdmarx.cplt_arg = (void *)hperh;
    hperh->hdmarx.err_cbk  = usart_dma_error;
    hperh->hdmarx.err_arg  = (void *)hperh;

    /* Configure DMA Receive */
795
    ald_dma_config_struct(&hperh->hdmarx.config);
W
wangyq2018 已提交
796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811
    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;

    if ((hperh->init.word_length == USART_WORD_LENGTH_9B)
            && (hperh->init.parity == USART_PARITY_NONE))
    {
        hperh->hdmarx.config.dst_inc    = DMA_DATA_INC_HALFWORD;
        hperh->hdmarx.config.data_width = DMA_DATA_SIZE_HALFWORD;
    }

812
    ald_dma_config_basic(&hperh->hdmarx);
W
wangyq2018 已提交
813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849

    __UNLOCK(hperh);
    SET_BIT(hperh->perh->CON2, USART_CON2_RXDMAEN_MSK);

    return OK;
}
#endif
/**
  * @}
  */

/** @defgroup USART_Public_Functions_Group2_2 Synchronization IO operation functions
  *  @brief   Synchronization IO operation functions
  *
  * @verbatim
  ==============================================================================
                         ##### IO operation functions #####
  ==============================================================================
  [..]
    This subsection provides a set of functions allowing to manage the USART synchronous
    data transfers.

  [..]
    The USART supports master mode only: it cannot receive or send data related to an input
    clock (SCLK is always an output).

    (#) There are two modes of transfer:
        (++) Blocking mode: The communication is performed in polling mode.
             The 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, These API's return the Status.
             The end of the data processing will be indicated through the
             dedicated USART IRQ when using Interrupt mode or the DMA IRQ when
             using DMA mode.
             The hperh->tx_cplt_cbk(), hperh->rx_cplt_cbk() and hperh->tx_rx_cplt_cbk()
             user callbacks will be executed respectively at the end of the transmit
850 851
	     or Receive process. The hperh->error_cbk() user callback will be
	     executed when a communication error is detected
W
wangyq2018 已提交
852 853

    (#) Blocking mode APIs are :
854 855 856
        (++) ald_usart_send_sync() in simplex mode
        (++) ald_usart_recv_sync() in full duplex receive only
        (++) ald_usart_send_recv_sync() in full duplex mode
W
wangyq2018 已提交
857 858

    (#) Non Blocking mode APIs with Interrupt are :
859 860 861 862
        (++) ald_usart_send_by_it_sync()in simplex mode
        (++) ald_usart_recv_by_it_sync() in full duplex receive only
        (++) ald_usart_send_recv_by_it_sync() in full duplex mode
        (++) ald_usart_irq_handler()
W
wangyq2018 已提交
863 864

    (#) Non Blocking mode functions with DMA are :
865 866
        (++) ald_usart_send_by_dma_sync()in simplex mode
        (++) ald_usart_recv_by_dma_sync() in full duplex receive only
W
wangyq2018 已提交
867
        (++) usart_send_recv_by_dma_symc() in full duplex mode
868 869 870
        (++) ald_usart_dma_pause()
        (++) ald_usart_dma_resume()
        (++) ald_usart_dma_stop()
W
wangyq2018 已提交
871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890

    (#) A set of Transfer Complete Callbacks are provided in non Blocking mode:
        (++) hperh->tx_cplt_cbk()
        (++) hperh->rx_cplt_cbk()
        (++) hperh->tx_rx_cplt_cbk()
        (++) hperh->error_cbk()

    @endverbatim
  * @{
  */

/**
  * @brief  Simplex Send an amount of data in blocking mode.
  * @param  hperh: Pointer to a usart_handle_t structure that contains
  *         the configuration information for the specified USART module.
  * @param  buf: Pointer to data buffer
  * @param  size: Amount of data to be sent
  * @param  timeout: Timeout duration
  * @retval Status, see @ref ald_status_t.
  */
891
ald_status_t ald_usart_send_sync(usart_handle_t *hperh, uint8_t *buf, uint16_t size, uint32_t timeout)
W
wangyq2018 已提交
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 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947
{
    if (hperh->state != USART_STATE_READY)
        return BUSY;

    if ((buf == NULL) || (size == 0))
        return  ERROR;

    __LOCK(hperh);

    hperh->tx_size  = size;
    hperh->tx_count = size;
    hperh->err_code = USART_ERROR_NONE;
    hperh->state    = USART_STATE_BUSY_TX;

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

        if ((hperh->init.word_length == USART_WORD_LENGTH_9B) && (hperh->init.parity == USART_PARITY_NONE))
        {
            WRITE_REG(hperh->perh->DATA, (*(uint16_t *)buf & 0x1FF));
            buf += 2;
        }
        else
        {
            WRITE_REG(hperh->perh->DATA, *buf++);
        }
    }

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

    hperh->state = USART_STATE_READY;
    __UNLOCK(hperh);

    return OK;
}

/**
  * @brief  Full-Duplex Receive an amount of data in blocking mode.
  * @param  hperh: Pointer to a usart_handle_t structure that contains
  *         the configuration information for the specified USART module.
  * @param  buf: Pointer to data buffer
  * @param  size: Amount of data to be received
  * @param  timeout: Timeout duration
  * @retval Status, see @ref ald_status_t.
  */
948
ald_status_t ald_usart_recv_sync(usart_handle_t *hperh, uint8_t *buf, uint16_t size, uint32_t timeout)
W
wangyq2018 已提交
949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026
{
    if (hperh->state != USART_STATE_READY)
        return BUSY;

    if ((buf == NULL) || (size == 0))
        return  ERROR;

    __LOCK(hperh);

    hperh->rx_size  = size;
    hperh->rx_count = size;
    hperh->err_code = USART_ERROR_NONE;
    hperh->state    = USART_STATE_BUSY_RX;

    while (hperh->rx_count-- > 0)
    {
        if (usart_wait_flag(hperh, USART_FLAG_TXE, SET, timeout) != OK)
        {
            __UNLOCK(hperh);
            hperh->state = USART_STATE_READY;
            return TIMEOUT;
        }

        if (hperh->init.word_length == USART_WORD_LENGTH_9B)
        {
            WRITE_REG(hperh->perh->DATA, (DUMMY_DATA & 0x1FF));

            if (usart_wait_flag(hperh, USART_FLAG_RXNE, SET, timeout) != OK)
            {
                __UNLOCK(hperh);
                hperh->state = USART_STATE_READY;
                return TIMEOUT;
            }

            if (hperh->init.parity == USART_PARITY_NONE)
            {
                *(uint16_t *)buf = (uint16_t)(hperh->perh->DATA & 0x1FF);
                buf += 2;
            }
            else
            {
                *buf++ = (uint8_t)(hperh->perh->DATA & 0xFF);
            }
        }
        else
        {
            WRITE_REG(hperh->perh->DATA, (DUMMY_DATA & 0xFF));

            if (usart_wait_flag(hperh, USART_FLAG_RXNE, SET, timeout) != OK)
            {
                __UNLOCK(hperh);
                hperh->state = USART_STATE_READY;
                return TIMEOUT;
            }

            if (hperh->init.parity == USART_PARITY_NONE)
                *buf++ = (uint8_t)(hperh->perh->DATA & 0xFF);
            else
                *buf++ = (uint8_t)(hperh->perh->DATA & 0x7F);
        }
    }

    hperh->state = USART_STATE_READY;
    __UNLOCK(hperh);

    return OK;
}

/**
  * @brief  Full-Duplex Send receive an amount of data in full-duplex mode (blocking mode).
  * @param  hperh: Pointer to a usart_handle_t structure that contains
  *         the configuration information for the specified USART module.
  * @param  tx_buf: Pointer to data transmitted buffer
  * @param  rx_buf: Pointer to data received buffer
  * @param  size: Amount of data to be sent
  * @param  timeout: Timeout duration
  * @retval Status, see @ref ald_status_t.
  */
1027
ald_status_t ald_usart_send_recv_sync(usart_handle_t *hperh, uint8_t *tx_buf, uint8_t *rx_buf, uint16_t size, uint32_t timeout)
W
wangyq2018 已提交
1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116
{
    if (hperh->state != USART_STATE_READY)
        return BUSY;

    if ((tx_buf == NULL) || (rx_buf == NULL) || (size == 0))
        return  ERROR;

    __LOCK(hperh);

    hperh->rx_size  = size;
    hperh->rx_count = size;
    hperh->tx_size  = size;
    hperh->tx_count = size;
    hperh->err_code = USART_ERROR_NONE;
    hperh->state    = USART_STATE_BUSY_RX;

    while (hperh->tx_count-- > 0)
    {
        --hperh->rx_count;

        if (usart_wait_flag(hperh, USART_FLAG_TXE, SET, timeout) != OK)
        {
            __UNLOCK(hperh);
            hperh->state = USART_STATE_READY;
            return TIMEOUT;
        }

        if (hperh->init.word_length == USART_WORD_LENGTH_9B)
        {
            if (hperh->init.parity == USART_PARITY_NONE)
            {
                WRITE_REG(hperh->perh->DATA, (*(uint16_t *)tx_buf & 0x1FF));
                tx_buf += 2;
            }
            else
            {
                WRITE_REG(hperh->perh->DATA, *tx_buf++);
            }

            if (usart_wait_flag(hperh, USART_FLAG_RXNE, SET, timeout) != OK)
            {
                __UNLOCK(hperh);
                hperh->state = USART_STATE_READY;
                return TIMEOUT;
            }

            if (hperh->init.parity == USART_PARITY_NONE)
            {
                *(uint16_t *)rx_buf = (uint16_t)(hperh->perh->DATA & 0x1FF);
                rx_buf += 2;
            }
            else
            {
                *rx_buf++ = (uint8_t)(hperh->perh->DATA & 0xFF);
            }
        }
        else
        {
            WRITE_REG(hperh->perh->DATA, *tx_buf++);

            if (usart_wait_flag(hperh, USART_FLAG_RXNE, SET, timeout) != OK)
            {
                __UNLOCK(hperh);
                hperh->state = USART_STATE_READY;
                return TIMEOUT;
            }

            if (hperh->init.parity == USART_PARITY_NONE)
                *rx_buf++ = (uint8_t)(hperh->perh->DATA & 0xFF);
            else
                *rx_buf++ = (uint8_t)(hperh->perh->DATA & 0x7F);
        }
    }

    hperh->state = USART_STATE_READY;
    __UNLOCK(hperh);

    return OK;
}

/**
  * @brief  Simplex Send an amount of data in non-blocking mode.
  * @param  hperh: Pointer to a usart_handle_t structure that contains
  *         the configuration information for the specified USART module.
  * @param  buf: Pointer to data buffer
  * @param  size: Amount of data to be sent
  * @retval Status, see @ref ald_status_t.
  * @note   The USART errors are not managed to avoid the overrun error.
  */
1117
ald_status_t ald_usart_send_by_it_sync(usart_handle_t *hperh, uint8_t *buf, uint16_t size)
W
wangyq2018 已提交
1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142
{
    if (hperh->state != USART_STATE_READY)
        return BUSY;

    if ((buf == NULL) || (size == 0))
        return ERROR;

    __LOCK(hperh);

    hperh->tx_buf   = buf;
    hperh->tx_size  = size;
    hperh->tx_count = size;
    hperh->err_code = USART_ERROR_NONE;
    hperh->state    = USART_STATE_BUSY_TX;

    /* The USART Error Interrupts: (Frame error, Noise error, Overrun error)
     * are not managed by the USART transmit process to avoid the overrun interrupt
     * when the USART mode is configured for transmit and receive "USART_MODE_TX_RX"
     * to benefit for the frame error and noise interrupts the USART mode should be
     * configured only for transmit "USART_MODE_TX"
     * The __ALD_USART_ENABLE_IT(hperh, USART_IT_ERR) can be used to enable the Frame error,
     * Noise error interrupt
     */

    __UNLOCK(hperh);
1143
    ald_usart_interrupt_config(hperh, USART_IT_TXE, ENABLE);
W
wangyq2018 已提交
1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155

    return OK;
}

/**
  * @brief  Simplex Receive an amount of data in non-blocking mode.
  * @param  hperh: Pointer to a usart_handle_t structure that contains
  *         the configuration information for the specified USART module.
  * @param  buf: Pointer to data buffer
  * @param  size: Amount of data to be received
  * @retval Status, see @ref ald_status_t.
  */
1156
ald_status_t ald_usart_recv_by_it_sync(usart_handle_t *hperh, uint8_t *buf, uint16_t size)
W
wangyq2018 已提交
1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172
{
    if (hperh->state != USART_STATE_READY)
        return BUSY;

    if ((buf == NULL) || (size == 0))
        return ERROR;

    __LOCK(hperh);

    hperh->rx_buf   = buf;
    hperh->rx_size  = size;
    hperh->rx_count = size;
    hperh->err_code = USART_ERROR_NONE;
    hperh->state    = USART_STATE_BUSY_RX;

    __UNLOCK(hperh);
1173 1174 1175
    ald_usart_interrupt_config(hperh, USART_IT_RXNE, ENABLE);
    ald_usart_interrupt_config(hperh, USART_IT_PE, ENABLE);
    ald_usart_interrupt_config(hperh, USART_IT_ERR, ENABLE);
W
wangyq2018 已提交
1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189

    WRITE_REG(hperh->perh->DATA, (DUMMY_DATA & (uint16_t)0x01FF));
    return OK;
}

/**
  * @brief  Full-Duplex Send receive an amount of data in full-duplex mode (non-blocking).
  * @param  hperh: Pointer to a usart_handle_t structure that contains
  *         the configuration information for the specified USART module.
  * @param  tx_buf: Pointer to data transmitted buffer
  * @param  rx_buf: Pointer to data received buffer
  * @param  size: Amount of data to be received
  * @retval Status, see @ref ald_status_t.
  */
1190
ald_status_t ald_usart_send_recv_by_it_sync(usart_handle_t *hperh, uint8_t *tx_buf, uint8_t *rx_buf,  uint16_t size)
W
wangyq2018 已提交
1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209
{
    if (hperh->state != USART_STATE_READY)
        return BUSY;

    if ((tx_buf == NULL) || (rx_buf == NULL) || (size == 0))
        return ERROR;

    __LOCK(hperh);

    hperh->rx_buf   = rx_buf;
    hperh->rx_size  = size;
    hperh->rx_count = size;
    hperh->tx_buf   = tx_buf;
    hperh->tx_size  = size;
    hperh->tx_count = size;
    hperh->err_code = USART_ERROR_NONE;
    hperh->state    = USART_STATE_BUSY_TX_RX;

    __UNLOCK(hperh);
1210 1211 1212 1213
    ald_usart_interrupt_config(hperh, USART_IT_RXNE, ENABLE);
    ald_usart_interrupt_config(hperh, USART_IT_PE, ENABLE);
    ald_usart_interrupt_config(hperh, USART_IT_ERR, ENABLE);
    ald_usart_interrupt_config(hperh, USART_IT_TXE, ENABLE);
W
wangyq2018 已提交
1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227

    return OK;
}

#ifdef ALD_DMA
/**
  * @brief  Simplex Send an amount of data in non-blocking mode.
  * @param  hperh: Pointer to a usart_handle_t structure that contains
  *         the configuration information for the specified USART 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.
  */
1228
ald_status_t ald_usart_send_by_dma_sync(usart_handle_t *hperh, uint8_t *buf, uint16_t size, uint8_t channel)
W
wangyq2018 已提交
1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253
{
    if (hperh->state != USART_STATE_READY)
        return BUSY;

    if ((buf == NULL) || (size == 0))
        return ERROR;

    __LOCK(hperh);

    hperh->tx_buf   = buf;
    hperh->tx_size  = size;
    hperh->tx_count = size;
    hperh->err_code = USART_ERROR_NONE;
    hperh->state    = USART_STATE_BUSY_TX;

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

    /* Configure callback function */
    hperh->hdmatx.cplt_cbk = usart_dma_send_cplt;
    hperh->hdmatx.cplt_arg = (void *)hperh;
    hperh->hdmatx.err_cbk  = usart_dma_error;
    hperh->hdmatx.err_arg  = (void *)hperh;

    /* Configure DMA transmit */
1254
    ald_dma_config_struct(&hperh->hdmatx.config);
W
wangyq2018 已提交
1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270
    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;

    if ((hperh->init.word_length == USART_WORD_LENGTH_9B)
            && (hperh->init.parity == USART_PARITY_NONE))
    {
        hperh->hdmatx.config.src_inc    = DMA_DATA_INC_HALFWORD;
        hperh->hdmatx.config.data_width = DMA_DATA_SIZE_HALFWORD;
    }

1271
    ald_dma_config_basic(&hperh->hdmatx);
W
wangyq2018 已提交
1272 1273

    __UNLOCK(hperh);
1274
    ald_usart_clear_flag_status(hperh, USART_FLAG_TC);
W
wangyq2018 已提交
1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291
    SET_BIT(hperh->perh->CON2, USART_CON2_TXDMAEN_MSK);

    return OK;
}

/**
  * @brief  Full-Duplex Receive an amount of data in non-blocking mode.
  * @param  hperh: Pointer to a usart_handle_t structure that contains
  *         the configuration information for the specified USART module.
  * @param  buf: Pointer to data buffer
  * @param  size: Amount of data to be received
  * @param  tx_channel: DMA channel as USART transmit
  * @param  rx_channel: DMA channel as USART receive
  * @retval Status, see @ref ald_status_t.
  * @note   The USART DMA transmit channel must be configured in order to generate the clock for the slave.
  * @note   When the USART parity is enabled (PCE = 1) the data received contain the parity bit.
  */
1292
ald_status_t ald_usart_recv_by_dma_sync(usart_handle_t *hperh, uint8_t *buf, uint16_t size, uint8_t tx_channel, uint8_t rx_channel)
W
wangyq2018 已提交
1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310
{
    if (hperh->state != USART_STATE_READY)
        return BUSY;

    if ((buf == NULL) || (size == 0))
        return ERROR;

    __LOCK(hperh);

    hperh->rx_buf   = buf;
    hperh->rx_size  = size;
    hperh->tx_buf   = buf;
    hperh->tx_size  = size;
    hperh->err_code = USART_ERROR_NONE;
    hperh->state    = USART_STATE_BUSY_RX;

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

W
wangyq2018 已提交
1312 1313 1314 1315 1316 1317 1318 1319 1320 1321
    if (hperh->hdmarx.perh == NULL)
        hperh->hdmarx.perh = DMA0;

    /* Configure DMA callback function */
    hperh->hdmarx.cplt_cbk = usart_dma_recv_cplt;
    hperh->hdmarx.cplt_arg = (void *)hperh;
    hperh->hdmarx.err_cbk  = usart_dma_error;
    hperh->hdmarx.err_arg  = (void *)hperh;

    /* Configure DMA receive*/
1322
    ald_dma_config_struct(&hperh->hdmarx.config);
W
wangyq2018 已提交
1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338
    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 = rx_channel;

    if ((hperh->init.word_length == USART_WORD_LENGTH_9B)
            && (hperh->init.parity == USART_PARITY_NONE))
    {
        hperh->hdmarx.config.dst_inc    = DMA_DATA_INC_HALFWORD;
        hperh->hdmarx.config.data_width = DMA_DATA_SIZE_HALFWORD;
    }

1339
    ald_dma_config_basic(&hperh->hdmarx);
W
wangyq2018 已提交
1340 1341 1342 1343 1344

    /* Enable the USART transmit DMA channel: the transmit channel is used in order
     * to generate in the non-blocking mode the clock to the slave device,
     * this mode isn't a simplex receive mode but a full-duplex receive one
     */
1345
    ald_dma_config_struct(&hperh->hdmatx.config);
W
wangyq2018 已提交
1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361
    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 = tx_channel;

    if ((hperh->init.word_length == USART_WORD_LENGTH_9B)
            && (hperh->init.parity == USART_PARITY_NONE))
    {
        hperh->hdmatx.config.src_inc    = DMA_DATA_INC_HALFWORD;
        hperh->hdmatx.config.data_width = DMA_DATA_SIZE_HALFWORD;
    }

1362
    ald_dma_config_basic(&hperh->hdmatx);
W
wangyq2018 已提交
1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383

    USART_CLEAR_OREFLAG(hperh);
    __UNLOCK(hperh);
    SET_BIT(hperh->perh->CON2, USART_CON2_RXDMAEN_MSK);
    SET_BIT(hperh->perh->CON2, USART_CON2_TXDMAEN_MSK);

    return OK;
}

/**
  * @brief  Full-Duplex Transmit Receive an amount of data in non-blocking mode.
  * @param  hperh: Pointer to a usart_handle_t structure that contains
  *         the configuration information for the specified USART module.
  * @param  tx_buf: Pointer to data transmitted buffer
  * @param  rx_buf: Pointer to data received buffer
  * @param  size: Amount of data to be received
  * @param  tx_channel: DMA channel as USART transmit
  * @param  rx_channel: DMA channel as USART receive
  * @note   When the USART parity is enabled (PCE = 1) the data received contain the parity bit.
  * @retval Status, see @ref ald_status_t.
  */
1384
ald_status_t ald_usart_send_recv_by_dma_sync(usart_handle_t *hperh, uint8_t *tx_buf,
W
wangyq2018 已提交
1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403
        uint8_t *rx_buf, uint16_t size, uint8_t tx_channel, uint8_t rx_channel)
{
    if (hperh->state != USART_STATE_READY)
        return BUSY;

    if ((tx_buf == NULL) || (rx_buf == NULL) || (size == 0))
        return ERROR;

    __LOCK(hperh);

    hperh->rx_buf   = rx_buf;
    hperh->rx_size  = size;
    hperh->tx_buf   = tx_buf;
    hperh->tx_size  = size;
    hperh->err_code = USART_ERROR_NONE;
    hperh->state    = USART_STATE_BUSY_TX_RX;

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

W
wangyq2018 已提交
1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418
    if (hperh->hdmarx.perh == NULL)
        hperh->hdmarx.perh = DMA0;

    /* Configure DMA callback function */
    hperh->hdmarx.cplt_cbk = usart_dma_recv_cplt;
    hperh->hdmarx.cplt_arg = (void *)hperh;
    hperh->hdmatx.cplt_cbk = usart_dma_send_cplt;
    hperh->hdmatx.cplt_arg = (void *)hperh;
    hperh->hdmatx.err_cbk  = usart_dma_error;
    hperh->hdmatx.err_arg  = (void *)hperh;
    hperh->hdmarx.err_cbk  = usart_dma_error;
    hperh->hdmarx.err_arg  = (void *)hperh;

    /* Configure DMA receive */
1419
    ald_dma_config_struct(&hperh->hdmarx.config);
W
wangyq2018 已提交
1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435
    hperh->hdmarx.config.src     = (void *)&hperh->perh->DATA;
    hperh->hdmarx.config.dst     = (void *)rx_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 = rx_channel;

    if ((hperh->init.word_length == USART_WORD_LENGTH_9B)
            && (hperh->init.parity == USART_PARITY_NONE))
    {
        hperh->hdmarx.config.dst_inc    = DMA_DATA_INC_HALFWORD;
        hperh->hdmarx.config.data_width = DMA_DATA_SIZE_HALFWORD;
    }

1436
    ald_dma_config_basic(&hperh->hdmarx);
W
wangyq2018 已提交
1437 1438

    /* Configure DMA transmit*/
1439
    ald_dma_config_struct(&hperh->hdmatx.config);
W
wangyq2018 已提交
1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455
    hperh->hdmatx.config.src     = (void *)tx_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 = tx_channel;

    if ((hperh->init.word_length == USART_WORD_LENGTH_9B)
            && (hperh->init.parity == USART_PARITY_NONE))
    {
        hperh->hdmatx.config.src_inc    = DMA_DATA_INC_HALFWORD;
        hperh->hdmatx.config.data_width = DMA_DATA_SIZE_HALFWORD;
    }

1456
    ald_dma_config_basic(&hperh->hdmatx);
W
wangyq2018 已提交
1457

1458
    ald_usart_clear_flag_status(hperh, USART_FLAG_TC);
W
wangyq2018 已提交
1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481
    USART_CLEAR_OREFLAG(hperh);
    __UNLOCK(hperh);
    SET_BIT(hperh->perh->CON2, USART_CON2_RXDMAEN_MSK);
    SET_BIT(hperh->perh->CON2, USART_CON2_TXDMAEN_MSK);

    return OK;
}
#endif
/**
  * @}
  */

/** @defgroup USART_Public_Functions_Group2_3 Utilities functions
  * @brief    Utilities functions
  * @{
  */
#ifdef ALD_DMA
/**
  * @brief  Pauses the DMA Transfer.
  * @param  hperh: Pointer to a usart_handle_t structure that contains
  *         the configuration information for the specified USART module.
  * @retval Status, see @ref ald_status_t.
  */
1482
ald_status_t ald_usart_dma_pause(usart_handle_t *hperh)
W
wangyq2018 已提交
1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514
{
    __LOCK(hperh);

    if (hperh->state == USART_STATE_BUSY_TX)
    {
        CLEAR_BIT(hperh->perh->CON2, USART_CON2_TXDMAEN_MSK);
    }
    else if (hperh->state == USART_STATE_BUSY_RX)
    {
        CLEAR_BIT(hperh->perh->CON2, USART_CON2_RXDMAEN_MSK);
    }
    else if (hperh->state == USART_STATE_BUSY_TX_RX)
    {
        CLEAR_BIT(hperh->perh->CON2, USART_CON2_TXDMAEN_MSK);
        CLEAR_BIT(hperh->perh->CON2, USART_CON2_RXDMAEN_MSK);
    }
    else
    {
        __UNLOCK(hperh);
        return ERROR;
    }

    __UNLOCK(hperh);
    return OK;
}

/**
  * @brief  Resumes the DMA Transfer.
  * @param  hperh: Pointer to a usart_handle_t structure that contains
  *         the configuration information for the specified USART module.
  * @retval Status, see @ref ald_status_t.
  */
1515
ald_status_t ald_usart_dma_resume(usart_handle_t *hperh)
W
wangyq2018 已提交
1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549
{
    __LOCK(hperh);

    if (hperh->state == USART_STATE_BUSY_TX)
    {
        SET_BIT(hperh->perh->CON2, USART_CON2_TXDMAEN_MSK);
    }
    else if (hperh->state == USART_STATE_BUSY_RX)
    {
        USART_CLEAR_OREFLAG(hperh);
        SET_BIT(hperh->perh->CON2, USART_CON2_RXDMAEN_MSK);
    }
    else if (hperh->state == USART_STATE_BUSY_TX_RX)
    {
        USART_CLEAR_OREFLAG(hperh);
        SET_BIT(hperh->perh->CON2, USART_CON2_TXDMAEN_MSK);
        SET_BIT(hperh->perh->CON2, USART_CON2_RXDMAEN_MSK);
    }
    else
    {
        __UNLOCK(hperh);
        return ERROR;
    }

    __UNLOCK(hperh);
    return OK;
}

/**
  * @brief  Stops the DMA Transfer.
  * @param  hperh: Pointer to a usart_handle_t structure that contains
  *         the configuration information for the specified USART module.
  * @retval Status, see @ref ald_status_t.
  */
1550
ald_status_t ald_usart_dma_stop(usart_handle_t *hperh)
W
wangyq2018 已提交
1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564
{
    CLEAR_BIT(hperh->perh->CON2, USART_CON2_TXDMAEN_MSK);
    CLEAR_BIT(hperh->perh->CON2, USART_CON2_RXDMAEN_MSK);

    hperh->state = USART_STATE_READY;
    return OK;
}
#endif
/**
  * @brief  This function handles USART interrupt request.
  * @param  hperh: Pointer to a usart_handle_t structure that contains
  *         the configuration information for the specified USART module.
  * @retval None
  */
1565
void ald_usart_irq_handler(usart_handle_t *hperh)
W
wangyq2018 已提交
1566 1567 1568 1569 1570
{
    uint32_t flag;
    uint32_t source;

    /* Handle parity error */
1571 1572 1573
    flag   = ald_usart_get_flag_status(hperh, USART_FLAG_PE);
    source = ald_usart_get_it_status(hperh, USART_IT_PE);

W
wangyq2018 已提交
1574 1575 1576 1577
    if ((flag != RESET) && (source != RESET))
        hperh->err_code |= USART_ERROR_PE;

    /* Handle frame error */
1578 1579 1580
    flag   = ald_usart_get_flag_status(hperh, USART_FLAG_FE);
    source = ald_usart_get_it_status(hperh, USART_IT_ERR);

W
wangyq2018 已提交
1581 1582 1583 1584
    if ((flag != RESET) && (source != RESET))
        hperh->err_code |= USART_ERROR_FE;

    /* Handle noise error */
1585 1586
    flag = ald_usart_get_flag_status(hperh, USART_FLAG_NE);

W
wangyq2018 已提交
1587 1588 1589 1590
    if ((flag != RESET) && (source != RESET))
        hperh->err_code |= USART_ERROR_NE;

    /* Handle overrun error */
1591 1592
    flag = ald_usart_get_flag_status(hperh, USART_FLAG_ORE);

W
wangyq2018 已提交
1593 1594 1595 1596
    if ((flag != RESET) && (source != RESET))
        hperh->err_code |= USART_ERROR_ORE;

    /* Handle idle error */
1597 1598 1599
    flag   = ald_usart_get_flag_status(hperh, USART_FLAG_IDLE);
    source = ald_usart_get_it_status(hperh, USART_IT_IDLE);

W
wangyq2018 已提交
1600 1601 1602 1603 1604 1605 1606
    if ((flag != RESET) && (source != RESET))
        __usart_recv_frame_cplt(hperh);

    /* Handle asynchronous */
    if (READ_BIT(hperh->perh->CON1, USART_CON1_SCKEN_MSK) == 0)
    {
        /* Receiver */
1607 1608 1609
        flag   = ald_usart_get_flag_status(hperh, USART_FLAG_RXNE);
        source = ald_usart_get_it_status(hperh, USART_IT_RXNE);

W
wangyq2018 已提交
1610 1611 1612 1613
        if ((flag != RESET) && (source != RESET))
            __usart_recv_by_it(hperh);

        /* Transmitter */
1614 1615 1616
        flag   = ald_usart_get_flag_status(hperh, USART_FLAG_TXE);
        source = ald_usart_get_it_status(hperh, USART_IT_TXE);

W
wangyq2018 已提交
1617 1618 1619
        if ((flag != RESET) && (source != RESET))
            __usart_send_by_it(hperh);
    }
1620
    else  	/* Handle synchronous */
W
wangyq2018 已提交
1621 1622
    {
        /* Receiver */
1623 1624 1625
        flag   = ald_usart_get_flag_status(hperh, USART_FLAG_RXNE);
        source = ald_usart_get_it_status(hperh, USART_IT_RXNE);

W
wangyq2018 已提交
1626 1627 1628 1629 1630 1631 1632 1633 1634
        if ((flag != RESET) && (source != RESET))
        {
            if (hperh->state == USART_STATE_BUSY_RX)
                __usart_recv_by_it_sync(hperh);
            else
                __usart_send_recv_by_it_sync(hperh);
        }

        /* Transmitter */
1635 1636 1637
        flag   = ald_usart_get_flag_status(hperh, USART_FLAG_TXE);
        source = ald_usart_get_it_status(hperh, USART_IT_TXE);

W
wangyq2018 已提交
1638 1639 1640 1641 1642 1643 1644 1645 1646 1647
        if ((flag != RESET) && (source != RESET))
        {
            if (hperh->state == USART_STATE_BUSY_TX)
                __usart_send_by_it(hperh);
            else
                __usart_send_recv_by_it_sync(hperh);
        }
    }

    /* Handle transmitter end */
1648 1649 1650
    flag   = ald_usart_get_flag_status(hperh, USART_FLAG_TC);
    source = ald_usart_get_it_status(hperh, USART_IT_TC);

W
wangyq2018 已提交
1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682
    if ((flag != RESET) && (source != RESET))
        __usart_end_send_by_it(hperh);

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

        if (hperh->error_cbk != NULL)
            hperh->error_cbk(hperh);
    }
}

/**
  * @}
  */

/**
  * @}
  */

/** @defgroup USART_Public_Functions_Group3 Peripheral Control functions
  *  @brief   USART control functions
  *
  * @verbatim
  ==============================================================================
                      ##### Peripheral Control functions #####
  ==============================================================================
  [..]
    This subsection provides a set of functions allowing to control the USART:
    (+) usart_lin_send_break() API can be helpful to transmit the break character.
1683 1684 1685 1686 1687 1688 1689 1690
    (+) ald_usart_multi_processor_enter_mute_mode() API can be helpful to enter the USART in mute mode.
    (+) ald_usart_multi_processor_exit_mute_mode() API can be helpful to exit the USART mute mode by software.
    (+) ald_usart_half_duplex_enable_send() API to enable the USART transmitter and disables the USART receiver in Half Duplex mode
    (+) ald_usart_half_duplex_enable_recv() API to enable the USART receiver and disables the USART transmitter in Half Duplex mode
    (+) ald_usart_interrupt_config() API to Enables/Disables the specified USART interrupts
    (+) ald_usart_get_flag_status() API to get USART flag status
    (+) ald_usart_clear_flag_status() API to clear USART flag status
    (+) ald_usart_get_it_status() API to Checks whether the specified USART interrupt has occurred or not
W
wangyq2018 已提交
1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701

    @endverbatim
  * @{
  */

/**
  * @brief  Enters the USART in mute mode.
  * @param  hperh: Pointer to a usart_handle_t structure that contains
  *         the configuration information for the specified USART module.
  * @retval Status, see @ref ald_status_t.
  */
1702
ald_status_t ald_usart_multi_processor_enter_mute_mode(usart_handle_t *hperh)
W
wangyq2018 已提交
1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721
{
    assert_param(IS_USART(hperh->perh));

    __LOCK(hperh);

    hperh->state = USART_STATE_BUSY;
    SET_BIT(hperh->perh->CON0, USART_CON0_RXWK_MSK);
    hperh->state = USART_STATE_READY;

    __UNLOCK(hperh);
    return OK;
}

/**
  * @brief  Exits the USART mute mode: wake up software.
  * @param  hperh: Pointer to a usart_handle_t structure that contains
  *         the configuration information for the specified USART module.
  * @retval Status, see @ref ald_status_t.
  */
1722
ald_status_t ald_usart_multi_processor_exit_mute_mode(usart_handle_t *hperh)
W
wangyq2018 已提交
1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741
{
    assert_param(IS_USART(hperh->perh));

    __LOCK(hperh);

    hperh->state = USART_STATE_BUSY;
    CLEAR_BIT(hperh->perh->CON0, USART_CON0_RXWK_MSK);
    hperh->state = USART_STATE_READY;

    __UNLOCK(hperh);
    return OK;
}

/**
  * @brief  Enables the USART transmitter and disables the USART receiver.
  * @param  hperh: Pointer to a usart_handle_t structure that contains
  *         the configuration information for the specified USART module.
  * @retval Status, see @ref ald_status_t.
  */
1742
ald_status_t ald_usart_half_duplex_enable_send(usart_handle_t *hperh)
W
wangyq2018 已提交
1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760
{
    __LOCK(hperh);

    hperh->state = USART_STATE_BUSY;
    SET_BIT(hperh->perh->CON0, USART_CON0_RXEN_MSK);
    SET_BIT(hperh->perh->CON0, USART_CON0_TXEN_MSK);
    hperh->state = USART_STATE_READY;

    __UNLOCK(hperh);
    return OK;
}

/**
  * @brief  Enables the USART receiver and disables the USART transmitter.
  * @param  hperh: Pointer to a usart_handle_t structure that contains
  *         the configuration information for the specified USART module.
  * @retval Status, see @ref ald_status_t.
  */
1761
ald_status_t ald_usart_half_duplex_enable_recv(usart_handle_t *hperh)
W
wangyq2018 已提交
1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778
{
    __LOCK(hperh);

    hperh->state = USART_STATE_BUSY;
    SET_BIT(hperh->perh->CON0, USART_CON0_RXEN_MSK);
    SET_BIT(hperh->perh->CON0, USART_CON0_TXEN_MSK);
    hperh->state = USART_STATE_READY;

    __UNLOCK(hperh);
    return OK;
}

/**
  * @brief  Enables or disables the USART's DMA request.
  * @param  hperh: Pointer to a usart_handle_t structure that contains
  *         the configuration information for the specified USART module.
  * @param  req: specifies the DMA request.
1779 1780
  *	      @arg USART_dma_req_tx: USART DMA transmit request
  *	      @arg USART_dma_req_rx: USART DMA receive request
W
wangyq2018 已提交
1781
  * @param  state: New state of the DMA Request sources.
1782
  *	      @arg ENABLE
W
wangyq2018 已提交
1783 1784 1785
  *           @arg DISABLE
  * @return: None
  */
1786
void ald_usart_dma_req_config(usart_handle_t *hperh, usart_dma_req_t req, type_func_t state)
W
wangyq2018 已提交
1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818
{
    assert_param(IS_USART(hperh->perh));
    assert_param(IS_USART_DMAREQ(req));
    assert_param(IS_FUNC_STATE(state));

    if (state != DISABLE)
        SET_BIT(hperh->perh->CON2, req);
    else
        CLEAR_BIT(hperh->perh->CON2, req);

    return;
}

/**
  * @brief  Enables or disables the specified USART interrupts.
  * @param  hperh: Pointer to a usart_handle_t structure that contains
  *         the configuration information for the specified USART module.
  * @param  it: Specifies the USART interrupt sources to be enabled or disabled.
  *           This parameter can be one of the following values:
  *           @arg USART_IT_CTS:  CTS change interrupt
  *           @arg USART_IT_LBD:  LIN Break detection interrupt
  *           @arg USART_IT_TXE:  Transmit Data Register empty interrupt
  *           @arg USART_IT_TC:   Transmission complete interrupt
  *           @arg USART_IT_RXNE: Receive Data register not empty interrupt
  *           @arg USART_IT_IDLE: Idle line detection interrupt
  *           @arg USART_IT_PE:   Parity Error interrupt
  *           @arg USART_IT_ERR:  Error interrupt(Frame error, noise error, overrun error)
  * @param  state: New status
  *           - ENABLE
  *           - DISABLE
  * @retval None
  */
1819
void ald_usart_interrupt_config(usart_handle_t *hperh, usart_it_t it, type_func_t state)
W
wangyq2018 已提交
1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864
{
    uint8_t idx;

    assert_param(IS_USART(hperh->perh));
    assert_param(IS_USART_CONFIG_IT(it));
    assert_param(IS_FUNC_STATE(state));

    idx = (it >> 16) & 0x7;
    it &= 0xFFFF;

    if (state)
    {
        if (idx == 1)
            SET_BIT(hperh->perh->CON0, it);
        else if (idx == 2)
            SET_BIT(hperh->perh->CON1, it);
        else if (idx == 4)
            SET_BIT(hperh->perh->CON2, it);
        else
            ;
    }
    else
    {
        if (idx == 1)
            CLEAR_BIT(hperh->perh->CON0, it);
        else if (idx == 2)
            CLEAR_BIT(hperh->perh->CON1, it);
        else if (idx == 4)
            CLEAR_BIT(hperh->perh->CON2, it);
        else
            ;
    }

    return;
}

/** @brief  Check whether the specified USART flag is set or not.
  * @param  hperh: Pointer to a usart_handle_t structure that contains
  *         the configuration information for the specified USART module.
  * @param  flag: specifies the flag to check.
  *         This parameter can be one of the @ref usart_flag_t.
  * @retval Status
  *           - SET
  *           - RESET
  */
1865
flag_status_t ald_usart_get_flag_status(usart_handle_t *hperh, usart_flag_t flag)
W
wangyq2018 已提交
1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894
{
    flag_status_t status = RESET;

    assert_param(IS_USART(hperh->perh));
    assert_param(IS_USART_FLAG(flag));

    if (READ_BIT(hperh->perh->STAT, flag))
        status = SET;

    return status;
}

/** @brief  Clear the specified USART pending flags.
  * @param  hperh: Pointer to a usart_handle_t structure that contains
  *         the configuration information for the specified USART module.
  * @param  flag: specifies the flag to check.
  *          This parameter can be any combination of the following values:
  *            @arg USART_FLAG_TC:   Transmission Complete flag.
  *            @arg USART_FLAG_RXNE: Receive data register not empty flag.
  * @note   PE (Parity error), FE (Framing error), NE (Noise error), ORE (OverRun
  *          error) and IDLE (Idle line detected) flags are cleared by software
  *          sequence: a read operation to USART_SR register followed by a read
  *          operation to USART_DR register.
  * @note   RXNE flag can be also cleared by a read to the USART_DR register.
  * @note   TC flag can be also cleared by software sequence: a read operation to
  *         USART_SR register followed by a write operation to USART_DR register.
  * @note   TXE flag is cleared only by a write to the USART_DR register.
  * @retval None
  */
1895
void ald_usart_clear_flag_status(usart_handle_t *hperh, usart_flag_t flag)
W
wangyq2018 已提交
1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907
{
    assert_param(IS_USART(hperh->perh));
    assert_param(IS_USART_CLEAR_FLAG(flag));

    CLEAR_BIT(hperh->perh->STAT, flag);
}

/**
  * @brief  Checks whether the specified USART interrupt has occurred or not.
  * @param  hperh: Pointer to a usart_handle_t structure that contains
  *         the configuration information for the specified USART module.
  * @param  it: Specifies the USART interrupt source to check.
1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918
  *	       This parameter can be one of the following values:
  *	       @arg USART_IT_CTS:  CTS change interrupt
  *	       @arg USART_IT_LBD:  LIN Break detection interrupt
  *	       @arg USART_IT_TXE:  Tansmit Data Register empty interrupt
  *	       @arg USART_IT_TC:   Transmission complete interrupt
  *	       @arg USART_IT_RXNE: Receive Data register not empty interrupt
  *	       @arg USART_IT_IDLE: Idle line detection interrupt
  *	       @arg USART_IT_ORE:  OverRun Error interrupt
  *	       @arg USART_IT_NE:   Noise Error interrupt
  *	       @arg USART_IT_FE:   Framing Error interrupt
  *	       @arg USART_IT_PE:   Parity Error interrupt
W
wangyq2018 已提交
1919 1920 1921 1922
  * @retval Status
  *           - SET
  *           - RESET
  */
1923
it_status_t ald_usart_get_it_status(usart_handle_t *hperh, usart_it_t it)
W
wangyq2018 已提交
1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977
{
    uint8_t idx;
    it_status_t status = RESET;

    /* Check the parameters */
    assert_param(IS_USART(hperh->perh));
    assert_param(IS_USART_GET_IT(it));

    idx = (it >> 16) & 0x7;
    it &= 0xFFFF;

    if (idx == 0)
    {
        if (READ_BIT(hperh->perh->STAT, it))
            status = SET;
    }
    else if (idx == 1)
    {
        if (READ_BIT(hperh->perh->CON0, it))
            status = SET;
    }
    else if (idx == 2)
    {
        if (READ_BIT(hperh->perh->CON1, it))
            status = SET;
    }
    else if (idx == 4)
    {
        if (READ_BIT(hperh->perh->CON2, it))
            status = SET;
    }
    else
    {
        /* do nothing */
    }

    return status;
}

/**
  * @}
  */

/** @defgroup USART_Public_Functions_Group4 Peripheral State and Errors functions
  *  @brief   USART State and Errors functions
  *
@verbatim
  ==============================================================================
                 ##### Peripheral State and Errors functions #####
  ==============================================================================
 [..]
   This subsection provides a set of functions allowing to return the State of
   USART communication process, return Peripheral Errors occurred during communication
   process
1978 1979
   (+) ald_usart_get_state() API can be helpful to check in run-time the state of the USART peripheral.
   (+) ald_usart_get_error() check in run-time errors that could be occurred during communication.
W
wangyq2018 已提交
1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990

@endverbatim
  * @{
  */

/**
  * @brief  Returns the USART state.
  * @param  hperh: Pointer to a usart_handle_t structure that contains
  *         the configuration information for the specified USART module.
  * @retval USART state
  */
1991
usart_state_t ald_usart_get_state(usart_handle_t *hperh)
W
wangyq2018 已提交
1992 1993 1994 1995 1996 1997 1998 1999 2000 2001
{
    return hperh->state;
}

/**
  * @brief  Return the USART error code
  * @param  hperh: Pointer to a usart_handle_t structure that contains
  *         the configuration information for the specified USART.
  * @retval USART Error Code
  */
2002
uint32_t ald_usart_get_error(usart_handle_t *hperh)
W
wangyq2018 已提交
2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031
{
    return hperh->err_code;
}

/**
  * @}
  */

/**
  * @}
  */

/** @defgroup USART_Private_Functions   USART Private Functions
  *  @brief   USART Private functions
  * @{
  */
#ifdef ALD_DMA
/**
  * @brief  DMA USART transmit process complete callback.
  * @param  arg: Pointer to a usart_handle_t structure that contains
  *         the configuration information for the specified USART module.
  * @retval None
  */
static void usart_dma_send_cplt(void *arg)
{
    usart_handle_t *hperh = (usart_handle_t *)arg;

    hperh->tx_count = 0;
    CLEAR_BIT(hperh->perh->CON2, USART_CON2_TXDMAEN_MSK);
2032
    ald_usart_interrupt_config(hperh, USART_IT_TC, ENABLE);
W
wangyq2018 已提交
2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090
}

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

    hperh->rx_count = 0;
    CLEAR_BIT(hperh->perh->CON2, USART_CON2_RXDMAEN_MSK);
    CLEAR_BIT(hperh->state, USART_STATE_RX_MASK);

    if (hperh->rx_cplt_cbk != NULL)
        hperh->rx_cplt_cbk(hperh);
}

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

    hperh->rx_count  = 0;
    hperh->tx_count  = 0;
    hperh->state     = USART_STATE_READY;
    hperh->err_code |= USART_ERROR_DMA;

    CLEAR_BIT(hperh->perh->CON2, USART_CON2_TXDMAEN_MSK);
    CLEAR_BIT(hperh->perh->CON2, USART_CON2_RXDMAEN_MSK);

    if (hperh->error_cbk != NULL)
        hperh->error_cbk(hperh);
}
#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 usart_wait_flag(usart_handle_t *hperh, usart_flag_t flag, flag_status_t status, uint32_t timeout)
{
    uint32_t tick;

    if (timeout == 0)
        return OK;

2091
    tick = ald_get_tick();
W
wangyq2018 已提交
2092

2093
    while ((ald_usart_get_flag_status(hperh, flag)) != status)
W
wangyq2018 已提交
2094
    {
2095
        if (((ald_get_tick()) - tick) > timeout)
W
wangyq2018 已提交
2096
        {
2097 2098 2099 2100
            ald_usart_interrupt_config(hperh, USART_IT_TXE, DISABLE);
            ald_usart_interrupt_config(hperh, USART_IT_RXNE, DISABLE);
            ald_usart_interrupt_config(hperh, USART_IT_PE, DISABLE);
            ald_usart_interrupt_config(hperh, USART_IT_ERR, DISABLE);
W
wangyq2018 已提交
2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131

            return TIMEOUT;
        }
    }

    return OK;
}

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

    if ((hperh->init.word_length == USART_WORD_LENGTH_9B) && (hperh->init.parity == USART_PARITY_NONE))
    {
        WRITE_REG(hperh->perh->DATA, (uint16_t)(*(uint16_t *)hperh->tx_buf & (uint16_t)0x01FF));
        hperh->tx_buf += 2;
    }
    else
    {
        WRITE_REG(hperh->perh->DATA, *hperh->tx_buf++);
    }

    if (--hperh->tx_count == 0)
    {
2132 2133
        ald_usart_interrupt_config(hperh, USART_IT_TXE, DISABLE);
        ald_usart_interrupt_config(hperh, USART_IT_TC, ENABLE);
W
wangyq2018 已提交
2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147
    }

    return OK;
}


/**
  * @brief  Wraps up transmission in non blocking mode.
  * @param  hperh: pointer to a usart_handle_t structure that contains
  *         the configuration information for the specified USART module.
  * @retval Status, see @ref ald_status_t.
  */
static ald_status_t __usart_end_send_by_it(usart_handle_t *hperh)
{
2148
    ald_usart_interrupt_config(hperh, USART_IT_TC, DISABLE);
W
wangyq2018 已提交
2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187
    CLEAR_BIT(hperh->state, USART_STATE_TX_MASK);

    if (hperh->tx_cplt_cbk != NULL)
        hperh->tx_cplt_cbk(hperh);

    return OK;
}

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

    if (hperh->init.word_length == USART_WORD_LENGTH_9B)
    {
        if (hperh->init.parity == USART_PARITY_NONE)
        {
            *(uint16_t *)hperh->rx_buf = (uint16_t)(hperh->perh->DATA & (uint16_t)0x01FF);
            hperh->rx_buf += 2;
        }
        else
        {
            *hperh->rx_buf++ = (uint8_t)(hperh->perh->DATA & 0xFF);
        }
    }
    else
    {
        if (hperh->init.parity == USART_PARITY_NONE)
            *hperh->rx_buf++ = (uint8_t)(hperh->perh->DATA & 0xFF);
        else
            *hperh->rx_buf++ = (uint8_t)(hperh->perh->DATA & 0x7F);
    }

2188 2189
    if (__frame_mode && ((ald_usart_get_it_status(hperh, USART_IT_IDLE)) == RESET))
        ald_usart_interrupt_config(hperh, USART_IT_IDLE, ENABLE);
W
wangyq2018 已提交
2190 2191 2192

    if (--hperh->rx_count == 0)
    {
2193
        ald_usart_interrupt_config(hperh, USART_IT_RXNE, DISABLE);
W
wangyq2018 已提交
2194 2195 2196 2197 2198
        CLEAR_BIT(hperh->state, USART_STATE_RX_MASK);
        __frame_mode = 0;

        if (hperh->state == USART_STATE_READY)
        {
2199 2200
            ald_usart_interrupt_config(hperh, USART_IT_PE, DISABLE);
            ald_usart_interrupt_config(hperh, USART_IT_ERR, DISABLE);
W
wangyq2018 已提交
2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 2217 2218 2219 2220
        }

        if (hperh->rx_cplt_cbk != NULL)
            hperh->rx_cplt_cbk(hperh);
    }

    return OK;
}

/**
  * @brief  Receives an frame complete in non blocking mode
  * @param  hperh: Pointer to a usart_handle_t structure that contains
  *         the configuration information for the specified USART module.
  * @retval Status, see @ref ald_status_t.
  */
static ald_status_t __usart_recv_frame_cplt(usart_handle_t *hperh)
{
    if ((hperh->state != USART_STATE_BUSY_RX) && (hperh->state != USART_STATE_BUSY_TX_RX))
        return BUSY;

2221 2222
    ald_usart_interrupt_config(hperh, USART_IT_IDLE, DISABLE);
    ald_usart_interrupt_config(hperh, USART_IT_RXNE, DISABLE);
W
wangyq2018 已提交
2223 2224 2225 2226 2227 2228 2229
    CLEAR_BIT(hperh->state, USART_STATE_RX_MASK);

    __frame_mode    = 0;
    hperh->rx_size -= hperh->rx_count;

    if (hperh->state == USART_STATE_READY)
    {
2230 2231
        ald_usart_interrupt_config(hperh, USART_IT_PE, DISABLE);
        ald_usart_interrupt_config(hperh, USART_IT_ERR, DISABLE);
W
wangyq2018 已提交
2232 2233 2234 2235 2236 2237 2238 2239 2240 2241 2242 2243 2244 2245 2246 2247 2248 2249 2250 2251 2252 2253 2254 2255 2256 2257 2258 2259 2260 2261 2262 2263 2264 2265 2266 2267 2268 2269 2270 2271 2272 2273 2274 2275 2276 2277 2278 2279 2280 2281
    }

    if (hperh->rx_cplt_cbk != NULL)
        hperh->rx_cplt_cbk(hperh);

    return OK;
}



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

    if (hperh->init.word_length == USART_WORD_LENGTH_9B)
    {

        if (hperh->init.parity == USART_PARITY_NONE)
        {
            *(uint16_t *)hperh->rx_buf = (uint16_t)(hperh->perh->DATA & 0x1FF);
            hperh->rx_buf += 2;
        }
        else
        {
            *hperh->rx_buf++ = (uint8_t)(hperh->perh->DATA & 0xFF);
        }

        if (--hperh->rx_count != 0x00)
            WRITE_REG(hperh->perh->DATA, (DUMMY_DATA & 0x1FF));
    }
    else
    {
        if (hperh->init.parity == USART_PARITY_NONE)
            *hperh->rx_buf++ = (uint8_t)(hperh->perh->DATA & 0xFF);
        else
            *hperh->rx_buf++ = (uint8_t)(hperh->perh->DATA & 0x7F);

        if (--hperh->rx_count != 0x00)
            hperh->perh->DATA = (DUMMY_DATA & 0xFF);
    }

    if (hperh->rx_count == 0)
    {
2282 2283 2284
        ald_usart_interrupt_config(hperh, USART_IT_RXNE, DISABLE);
        ald_usart_interrupt_config(hperh, USART_IT_PE, DISABLE);
        ald_usart_interrupt_config(hperh, USART_IT_ERR, DISABLE);
W
wangyq2018 已提交
2285 2286 2287 2288 2289 2290 2291 2292 2293 2294 2295 2296 2297 2298 2299 2300 2301 2302 2303 2304 2305 2306
        hperh->state = USART_STATE_READY;

        if (hperh->rx_cplt_cbk != NULL)
            hperh->rx_cplt_cbk(hperh);
    }

    return OK;
}

/**
  * @brief  Full-Duplex Send receive an amount of data in full-duplex mode (non-blocking).
  * @param  hperh: Pointer to a usart_handle_t structure that contains
  *         the configuration information for the specified USART module.
  * @retval Status, see @ref ald_status_t.
  */
static ald_status_t __usart_send_recv_by_it_sync(usart_handle_t *hperh)
{
    if (hperh->state != USART_STATE_BUSY_TX_RX)
        return BUSY;

    if (hperh->tx_count != 0)
    {
2307
        if (ald_usart_get_flag_status(hperh, USART_FLAG_TXE) != RESET)
W
wangyq2018 已提交
2308 2309 2310 2311 2312 2313 2314 2315 2316 2317 2318 2319 2320 2321 2322 2323 2324 2325 2326
        {
            if (hperh->init.word_length == USART_WORD_LENGTH_9B)
            {
                if (hperh->init.parity == USART_PARITY_NONE)
                {
                    WRITE_REG(hperh->perh->DATA, (uint16_t)(*(uint16_t *)hperh->tx_buf & 0x1FF));
                    hperh->tx_buf += 2;
                }
                else
                {
                    WRITE_REG(hperh->perh->DATA, *hperh->tx_buf++);
                }
            }
            else
            {
                WRITE_REG(hperh->perh->DATA, *hperh->tx_buf++);
            }

            if (--hperh->tx_count == 0)
2327
                ald_usart_interrupt_config(hperh, USART_IT_TXE, DISABLE);
W
wangyq2018 已提交
2328 2329 2330 2331 2332
        }
    }

    if (hperh->rx_count != 0)
    {
2333
        if (ald_usart_get_flag_status(hperh, USART_FLAG_RXNE) != RESET)
W
wangyq2018 已提交
2334 2335 2336 2337 2338 2339 2340 2341 2342 2343 2344 2345 2346 2347 2348 2349 2350 2351 2352 2353 2354 2355 2356 2357 2358 2359 2360
        {
            if (hperh->init.word_length == USART_WORD_LENGTH_9B)
            {
                if (hperh->init.parity == USART_PARITY_NONE)
                {
                    *(uint16_t *)hperh->rx_buf = (uint16_t)(hperh->perh->DATA & 0x1FF);
                    hperh->rx_buf += 2;
                }
                else
                {
                    *hperh->rx_buf++ = (uint8_t)(hperh->perh->DATA & 0xFF);
                }
            }
            else
            {
                if (hperh->init.parity == USART_PARITY_NONE)
                    *hperh->rx_buf++ = (uint8_t)(hperh->perh->DATA & 0xFF);
                else
                    *hperh->rx_buf++ = (uint8_t)(hperh->perh->DATA & 0x7F);
            }

            --hperh->rx_count;
        }
    }

    if (hperh->rx_count == 0)
    {
2361 2362 2363
        ald_usart_interrupt_config(hperh, USART_IT_RXNE, DISABLE);
        ald_usart_interrupt_config(hperh, USART_IT_PE, DISABLE);
        ald_usart_interrupt_config(hperh, USART_IT_ERR, DISABLE);
W
wangyq2018 已提交
2364 2365 2366 2367 2368 2369 2370 2371 2372 2373 2374 2375 2376 2377 2378 2379 2380 2381 2382 2383 2384 2385 2386 2387 2388 2389 2390 2391 2392 2393 2394 2395 2396 2397 2398 2399 2400 2401 2402 2403 2404 2405 2406 2407 2408 2409 2410 2411 2412 2413 2414 2415 2416 2417 2418 2419 2420 2421

        hperh->state = USART_STATE_READY;

        if (hperh->tx_rx_cplt_cbk != NULL)
            hperh->tx_rx_cplt_cbk(hperh);
    }

    return OK;
}

/**
  * @brief  Configures the USART peripheral.
  * @param  hperh: Pointer to a usart_handle_t structure that contains
  *         the configuration information for the specified USART module.
  * @retval None
  */
static void usart_set_config(usart_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));
    assert_param(IS_USART_HARDWARE_FLOW_CONTROL(hperh->init.fctl));

    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);
    MODIFY_REG(hperh->perh->CON2, USART_CON2_RTSEN_MSK, (hperh->init.fctl & 0x1) << USART_CON2_RTSEN_POS);
    MODIFY_REG(hperh->perh->CON2, USART_CON2_CTSEN_MSK, ((hperh->init.fctl >> 1) & 0x1) << USART_CON2_CTSEN_POS);
    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);

    if (hperh->init.over_sampling)
        SET_BIT(hperh->perh->CON0, (1 << 15));

    /* Determine the integer part */
    if (READ_BIT(hperh->perh->CON0, (1 << 15)))
    {
        /* Integer part computing in case Oversampling mode is 8 Samples */
2422
        integer = ((25 * ald_cmu_get_pclk1_clock()) / (2 * (hperh->init.baud)));
W
wangyq2018 已提交
2423 2424 2425 2426
    }
    else
    {
        /* Integer part computing in case Oversampling mode is 16 Samples */
2427
        integer = ((25 * ald_cmu_get_pclk1_clock()) / (4 * (hperh->init.baud)));
W
wangyq2018 已提交
2428
    }
2429

W
wangyq2018 已提交
2430 2431 2432 2433 2434 2435 2436 2437 2438 2439 2440 2441 2442 2443 2444 2445 2446 2447 2448 2449 2450 2451 2452 2453 2454 2455
    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_USART */
/**
  * @}
  */

/**
  * @}
  */