ald_uart.c 36.8 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_uart.c
  * @brief   UART module driver.
  *          This file provides firmware functions to manage the following
  *          functionalities of the Universal Asynchronous Receiver Transmitter (UART) peripheral:
  *           + Initialization and Configuration functions
  *           + IO operation functions
  *           + Peripheral Control functions
  *           + Peripheral State and Errors functions
  *
  * @version V1.0
  * @date    21 Nov 2017
  * @author  AE Team
  * @note
  *
  * Copyright (C) Shanghai Eastsoft Microelectronics Co. Ltd. All rights reserved.
  *
  *********************************************************************************
  * @verbatim
  ==============================================================================
                        ##### How to use this driver #####
  ==============================================================================
  [..]
    The UART driver can be used as follows:

    (#) Declare a uart_handle_t handle structure.

    (#) Initialize the UART low level resources:
        (##) Enable the UARTx interface clock.
        (##) UART pins configuration:
            (+++) Enable the clock for the UART GPIOs.
            (+++) Configure the UART pins (TX as alternate function pull-up, RX as alternate function Input).
35 36
        (##) NVIC configuration if you need to use interrupt process (ald_uart_send_by_it()
             and ald_uart_recv_by_it() APIs):
W
wangyq2018 已提交
37 38
            (+++) Configure the uart interrupt priority.
            (+++) Enable the NVIC UART IRQ handle.
39 40
        (##) DMA Configuration if you need to use DMA process (ald_uart_send_by_dma()
             and ald_uart_recv_by_dma() APIs):
W
wangyq2018 已提交
41 42 43 44 45 46
            (+++) Select the DMA Tx/Rx channel.
            (+++) Associate the initialized DMA handle to the UART 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.

47
    (#) Initialize the UART registers by calling the ald_uart_init() API.
W
wangyq2018 已提交
48 49 50 51 52 53 54

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

     *** Polling mode IO operation ***
     =================================
     [..]
55 56
       (+) Send an amount of data in blocking mode using ald_uart_send()
       (+) Receive an amount of data in blocking mode using ald_uart_recv()
W
wangyq2018 已提交
57 58 59 60

     *** Interrupt mode IO operation ***
     ===================================
     [..]
61
       (+) Send an amount of data in non blocking mode using ald_uart_send_by_it()
W
wangyq2018 已提交
62 63
       (+) 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()
64
       (+) Receive an amount of data in non blocking mode using ald_uart_recv_by_it()
W
wangyq2018 已提交
65 66 67 68 69 70 71 72
       (+) 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 ***
     ==============================
     [..]
73
       (+) Send an amount of data in non blocking mode (DMA) using ald_uart_send_by_dma()
W
wangyq2018 已提交
74 75
       (+) 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()
76
       (+) Receive an amount of data in non blocking mode (DMA) using ald_uart_recv_by_dma()
W
wangyq2018 已提交
77 78 79 80
       (+) 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()
81 82 83
       (+) Pause the DMA Transfer using ald_uart_dma_pause()
       (+) Resume the DMA Transfer using ald_uart_dma_resume()
       (+) Stop the DMA Transfer using ald_uart_dma_stop()
W
wangyq2018 已提交
84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117

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

#include "ald_uart.h"
#include "ald_cmu.h"


/** @addtogroup ES32FXXX_ALD
  * @{
  */

/** @defgroup UART UART
  * @brief UART module driver
  * @{
  */
#ifdef ALD_UART

/** @defgroup UART_Private_Functions   UART Private Functions
  *  @brief   UART Private functions
  * @{
  */
#ifdef ALD_DMA
/**
  * @brief  DMA uart transmit process complete callback.
  * @param  arg: Pointer to a uart_handle_t structure.
  * @retval None
  */
static void uart_dma_send_cplt(void *arg)
{
    uart_handle_t *hperh = (uart_handle_t *)arg;

    if (hperh->state == UART_STATE_BUSY_TX)
118
        ald_uart_dma_req_config(hperh, DISABLE);
W
wangyq2018 已提交
119 120

    hperh->tx_count = 0;
121
    ald_uart_interrupt_config(hperh, UART_IT_TC, ENABLE);
W
wangyq2018 已提交
122 123 124 125 126 127 128 129 130 131 132 133 134
    return;
}

/**
  * @brief  DMA uart receive process complete callback.
  * @param  arg: Pointer to a uart_handle_t structure.
  * @retval None
  */
static void uart_dma_recv_cplt(void *arg)
{
    uart_handle_t *hperh = (uart_handle_t *)arg;

    if (hperh->state == UART_STATE_BUSY_RX)
135
        ald_uart_dma_req_config(hperh, DISABLE);
W
wangyq2018 已提交
136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158

    hperh->rx_count = 0;
    CLEAR_BIT(hperh->state, UART_STATE_RX_MASK);

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

    return;
}

/**
  * @brief  DMA uart communication error callback.
  * @param  arg: Pointer to a uart_handle_t structure.
  * @retval None
  */
static void uart_dma_error(void *arg)
{
    uart_handle_t *hperh = (uart_handle_t *)arg;

    hperh->rx_count  = 0;
    hperh->tx_count  = 0;
    hperh->state     = UART_STATE_READY;
    hperh->err_code |= UART_ERROR_DMA;
159
    ald_uart_dma_req_config(hperh, DISABLE);
W
wangyq2018 已提交
160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182

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

    return;
}
#endif

/**
  * @brief  This function handles uart Communication Timeout.
  * @param  hperh: Pointer to a uart_handle_t structure.
  * @param  flag: specifies the uart 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 uart_wait_flag(uart_handle_t *hperh, uart_status_t flag, flag_status_t status, uint32_t timeout)
{
    uint32_t tick;

    if (timeout == 0)
        return ERROR;

183
    tick = ald_get_tick();
W
wangyq2018 已提交
184 185

    /* Waiting for flag */
186
    while ((ald_uart_get_status(hperh, flag)) != status)
W
wangyq2018 已提交
187
    {
188
        if (((ald_get_tick()) - tick) > timeout)
W
wangyq2018 已提交
189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208
            return TIMEOUT;
    }

    return OK;
}

/**
  * @brief  Sends an amount of data in non blocking mode.
  * @param  hperh: Pointer to a uart_handle_t structure.
  * @retval Status, see @ref ald_status_t.
  */
static ald_status_t __uart_send_by_it(uart_handle_t *hperh)
{
    if ((hperh->state & UART_STATE_TX_MASK) == 0x0)
        return BUSY;

    WRITE_REG(hperh->perh->TBR, (uint8_t)(*hperh->tx_buf++ & 0x00FF));

    if (--hperh->tx_count == 0)
    {
209 210 211
        ald_uart_clear_flag_status(hperh, UART_IF_TC);
        ald_uart_interrupt_config(hperh, UART_IT_TXS, DISABLE);
        ald_uart_interrupt_config(hperh, UART_IT_TC, ENABLE);
W
wangyq2018 已提交
212 213 214 215 216 217 218 219 220 221 222 223 224 225 226
    }

    return OK;
}

/**
  * @brief  Wraps up transmission in non blocking mode.
  * @param  hperh: pointer to a uart_handle_t structure.
  * @retval Status, see @ref ald_status_t.
  */
static ald_status_t __uart_end_send_by_it(uart_handle_t *hperh)
{
    if (!(READ_BIT(hperh->perh->SR, UART_SR_TEM_MSK)))
        return OK;

227
    ald_uart_interrupt_config(hperh, UART_IT_TC, DISABLE);
W
wangyq2018 已提交
228
    CLEAR_BIT(hperh->state, UART_STATE_TX_MASK);
229

W
wangyq2018 已提交
230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249
    if (hperh->tx_cplt_cbk)
        hperh->tx_cplt_cbk(hperh);

    return OK;
}

/**
  * @brief  Receives an amount of data in non blocking mode
  * @param  hperh: Pointer to a uart_handle_t structure.
  * @retval Status, see @ref ald_status_t.
  */
static ald_status_t __uart_recv_by_it(uart_handle_t *hperh)
{
    if ((hperh->state & UART_STATE_RX_MASK) == 0x0)
        return BUSY;

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

    if (--hperh->rx_count == 0)
    {
250
        ald_uart_interrupt_config(hperh, UART_IT_RXRD, DISABLE);
W
wangyq2018 已提交
251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283
        CLEAR_BIT(hperh->state, UART_STATE_RX_MASK);

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

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

/** @defgroup UART_Public_Functions UART Public Functions
  * @{
  */

/** @defgroup UART_Public_Functions_Group1 Initialization and Configuration functions
  * @brief    Initialization and Configuration functions
  *
  * @verbatim
===============================================================================
            ##### Initialization and Configuration functions #####
 ===============================================================================
    [..]
    This subsection provides a set of functions allowing to initialize the UARTx
    and configure UARTx param.
      (+) For the UARTx only these parameters can be configured:
        (++) Baud Rate
        (++) Word Length
        (++) Stop Bit
        (++) Parity
        (++) Hardware flow control
      (+) For RS485 mode, user also need configure some parameters by
284
          ald_uart_rs485_config():
W
wangyq2018 已提交
285
        (++) Enable/disable normal point mode
286 287 288
	(++) Enable/disable auto-direction
	(++) Enable/disable address detection invert
	(++) Enable/disable address for compare
W
wangyq2018 已提交
289 290 291 292 293 294 295 296 297 298 299

    @endverbatim
  * @{
  */

/**
  * @brief  Reset UART peripheral
  * @param  hperh: Pointer to a uart_handle_t structure that contains
  *         the configuration information for the specified uart module.
  * @retval None
  */
300
void ald_uart_reset(uart_handle_t *hperh)
W
wangyq2018 已提交
301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325
{
    assert_param(IS_UART_ALL(hperh->perh));

    WRITE_REG(hperh->perh->BRR, 0x0);
    WRITE_REG(hperh->perh->LCR, 0x0);
    WRITE_REG(hperh->perh->MCR, 0x0);
    WRITE_REG(hperh->perh->CR, 0x0);
    WRITE_REG(hperh->perh->RTOR, 0x0);
    WRITE_REG(hperh->perh->FCR, 0x0);
    WRITE_REG(hperh->perh->IDR, 0xFFF);

    hperh->err_code = UART_ERROR_NONE;
    hperh->state    = UART_STATE_RESET;

    __UNLOCK(hperh);
    return;
}

/**
  * @brief  Initializes the UARTx according to the specified
  *         parameters in the uart_handle_t.
  * @param  hperh: Pointer to a uart_handle_t structure that contains
  *         the configuration information for the specified UART module.
  * @retval None
  */
326
void ald_uart_init(uart_handle_t *hperh)
W
wangyq2018 已提交
327 328 329 330 331 332 333 334 335 336 337
{
    uint32_t tmp;

    assert_param(IS_UART_ALL(hperh->perh));
    assert_param(IS_UART_BAUDRATE(hperh->init.baud));
    assert_param(IS_UART_WORD_LENGTH(hperh->init.word_length));
    assert_param(IS_UART_STOPBITS(hperh->init.stop_bits));
    assert_param(IS_UART_PARITY(hperh->init.parity));
    assert_param(IS_UART_MODE(hperh->init.mode));
    assert_param(IS_UART_HARDWARE_FLOW_CONTROL(hperh->init.fctl));

338
    ald_uart_reset(hperh);
W
wangyq2018 已提交
339 340 341 342 343 344 345 346 347

    tmp = READ_REG(hperh->perh->LCR);
    MODIFY_REG(tmp, UART_LCR_DLS_MSK, hperh->init.word_length << UART_LCR_DLS_POSS);
    MODIFY_REG(tmp, UART_LCR_STOP_MSK, hperh->init.stop_bits << UART_LCR_STOP_POS);
    MODIFY_REG(tmp, UART_LCR_PEN_MSK, (hperh->init.parity == UART_PARITY_NONE ? 0 : 1) << UART_LCR_PEN_POS);
    MODIFY_REG(tmp, UART_LCR_PS_MSK, (hperh->init.parity == UART_PARITY_EVEN ? 1 : 0) << UART_LCR_PS_POS);
    WRITE_REG(hperh->perh->LCR, tmp);
    MODIFY_REG(hperh->perh->MCR, UART_MCR_AFCEN_MSK, hperh->init.fctl << UART_MCR_AFCEN_POS);
    SET_BIT(hperh->perh->LCR, UART_LCR_BRWEN_MSK);
348
    WRITE_REG(hperh->perh->BRR, ald_cmu_get_pclk1_clock() / hperh->init.baud);
W
wangyq2018 已提交
349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369
    CLEAR_BIT(hperh->perh->LCR, UART_LCR_BRWEN_MSK);
    SET_BIT(hperh->perh->FCR, UART_FCR_FIFOEN_MSK);
    SET_BIT(hperh->perh->FCR, UART_FCR_RFRST_MSK);
    SET_BIT(hperh->perh->FCR, UART_FCR_TFRST_MSK);
    MODIFY_REG(hperh->perh->FCR, UART_FCR_RXTL_MSK, 0 << UART_FCR_RXTL_POSS);
    MODIFY_REG(hperh->perh->FCR, UART_FCR_TXTL_MSK, 0 << UART_FCR_TXTL_POSS);
    SET_BIT(hperh->perh->LCR, UART_LCR_RXEN_MSK);

    if (hperh->init.mode == UART_MODE_LIN)
        SET_BIT(hperh->perh->MCR, UART_MCR_LINEN_MSK);
    else if (hperh->init.mode == UART_MODE_IrDA)
        SET_BIT(hperh->perh->MCR, UART_MCR_IREN_MSK);
    else if (hperh->init.mode == UART_MODE_RS485)
        SET_BIT(hperh->perh->MCR, UART_MCR_AADEN_MSK);
    else if (hperh->init.mode == UART_MODE_HDSEL)
        SET_BIT(hperh->perh->MCR, UART_MCR_HDSEL_MSK);
    else
        ;/* do nothing */

    if (hperh->init.fctl)
        SET_BIT(hperh->perh->MCR, UART_MCR_RTSCTRL_MSK);
370

W
wangyq2018 已提交
371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386
    if (hperh->init.mode == UART_MODE_IrDA)
        SET_BIT(hperh->perh->LCR, UART_LCR_RXINV_MSK);

    hperh->state    = UART_STATE_READY;
    hperh->err_code = UART_ERROR_NONE;
    return;
}

/**
  * @brief  Configure the RS485 mode according to the specified
  *         parameters in the uart_rs485_config_t.
  * @param  hperh: Pointer to a uart_handle_t structure that contains
  *         the configuration information for the specified UART module.
  * @param  config: Specifies the RS485 parameters.
  * @retval None
  */
387
void ald_uart_rs485_config(uart_handle_t *hperh, uart_rs485_config_t *config)
W
wangyq2018 已提交
388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428
{
    assert_param(IS_UART_ALL(hperh->perh));
    assert_param(IS_FUNC_STATE(config->normal));
    assert_param(IS_FUNC_STATE(config->dir));
    assert_param(IS_FUNC_STATE(config->invert));

    MODIFY_REG(hperh->perh->MCR, UART_MCR_AADNOR_MSK, config->normal << UART_MCR_AADNOR_POS);
    MODIFY_REG(hperh->perh->MCR, UART_MCR_AADDIR_MSK, config->dir << UART_MCR_AADDIR_POS);
    MODIFY_REG(hperh->perh->MCR, UART_MCR_AADINV_MSK, config->invert << UART_MCR_AADINV_POS);
    MODIFY_REG(hperh->perh->CR, UART_CR_ADDR_MSK, config->addr << UART_CR_ADDR_POSS);

    return;
}
/**
  * @}
  */

/** @defgroup UART_Public_Functions_Group2 IO operation functions
  * @brief UART Transmit and Receive functions
  * @verbatim
  ==============================================================================
                      # IO operation functions #
  ==============================================================================
  [..]
    This subsection provides a set of functions allowing to manage the UART 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 UART 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:
429 430
        (++) ald_uart_send()
        (++) ald_uart_recv()
W
wangyq2018 已提交
431 432

    (#) Non Blocking mode APIs with Interrupt are:
433 434 435
        (++) ald_uart_send_by_it()
        (++) ald_uart_recv_by_it()
        (++) ald_uart_irq_handler()
W
wangyq2018 已提交
436 437

    (#) Non Blocking mode functions with DMA are:
438 439 440 441 442
        (++) ald_uart_send_by_dma()
        (++) ald_uart_recv_by_dma()
        (++) ald_uart_dma_pause()
        (++) ald_uart_dma_resume()
        (++) ald_uart_dma_stop()
W
wangyq2018 已提交
443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460

    (#) A set of transfer complete callbacks are provided in non blocking mode:
        (++) hperh->tx_cplt_cbk()
        (++) hperh->rx_cplt_cbk()
        (++) hperh->error_cbk()

    @endverbatim
  * @{
  */

/**
  * @brief  Sends an amount of data in blocking mode.
  * @param  hperh: Pointer to a uart_handle_t structure.
  * @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.
  */
461
ald_status_t ald_uart_send(uart_handle_t *hperh, uint8_t *buf, uint16_t size, uint32_t timeout)
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 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510
{
    assert_param(IS_UART_ALL(hperh->perh));

    if ((hperh->state != UART_STATE_READY) && (hperh->state != UART_STATE_BUSY_RX))
        return BUSY;

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

    __LOCK(hperh);
    hperh->err_code = UART_ERROR_NONE;
    SET_BIT(hperh->state, UART_STATE_TX_MASK);

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

    while (hperh->tx_count-- > 0)
    {
        if (uart_wait_flag(hperh, UART_STATUS_TBEM, SET, timeout) != OK)
        {
            __UNLOCK(hperh);
            hperh->state = UART_STATE_READY;
            return TIMEOUT;
        }

        WRITE_REG(hperh->perh->TBR, (*buf++ & 0xFF));
    }

    if (uart_wait_flag(hperh, UART_STATUS_TEM, SET, timeout) != OK)
    {
        __UNLOCK(hperh);
        hperh->state = UART_STATE_READY;
        return TIMEOUT;
    }

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

    return OK;
}

/**
  * @brief  Receives an amount of data in blocking mode.
  * @param  hperh: Pointer to a uart_handle_t structure.
  * @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.
  */
511
ald_status_t ald_uart_recv(uart_handle_t *hperh, uint8_t *buf, uint16_t size, uint32_t timeout)
W
wangyq2018 已提交
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 550 551 552 553
{
    assert_param(IS_UART_ALL(hperh->perh));

    if ((hperh->state != UART_STATE_READY) && (hperh->state != UART_STATE_BUSY_TX))
        return BUSY;

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

    __LOCK(hperh);

    hperh->err_code = UART_ERROR_NONE;
    SET_BIT(hperh->state, UART_STATE_RX_MASK);

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

    while (hperh->rx_count-- > 0)
    {
        if (uart_wait_flag(hperh, UART_STATUS_DR, SET, timeout) != OK)
        {
            __UNLOCK(hperh);
            hperh->state = UART_STATE_READY;
            return TIMEOUT;
        }

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

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

    return OK;
}

/**
  * @brief  Sends an amount of data in non blocking mode.
  * @param  hperh: Pointer to a uart_handle_t structure.
  * @param  buf: Pointer to data buffer
  * @param  size: Amount of data to be sent
  * @retval Status, see @ref ald_status_t.
  */
554
ald_status_t ald_uart_send_by_it(uart_handle_t *hperh, uint8_t *buf, uint16_t size)
W
wangyq2018 已提交
555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572
{
    assert_param(IS_UART_ALL(hperh->perh));

    if ((hperh->state != UART_STATE_READY) && (hperh->state != UART_STATE_BUSY_RX))
        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 = UART_ERROR_NONE;
    SET_BIT(hperh->state, UART_STATE_TX_MASK);
    __UNLOCK(hperh);

573 574
    if (((ald_uart_get_status(hperh, UART_STATUS_TBEM)) == SET)
            && ((ald_uart_get_flag_status(hperh, UART_IF_TXS)) == RESET))
W
wangyq2018 已提交
575 576 577 578 579 580 581
    {
        WRITE_REG(hperh->perh->TBR, (*hperh->tx_buf++ & 0xFF));
        --hperh->tx_count;
    }

    if (hperh->tx_count == 0)
    {
582
        ald_uart_interrupt_config(hperh, UART_IT_TC, ENABLE);
W
wangyq2018 已提交
583 584 585
        return OK;
    }

586
    ald_uart_interrupt_config(hperh, UART_IT_TXS, ENABLE);
W
wangyq2018 已提交
587 588 589 590 591 592 593 594 595 596
    return OK;
}

/**
  * @brief  Receives an amount of data in non blocking mode
  * @param  hperh: Pointer to a uart_handle_t structure.
  * @param  buf: Pointer to data buffer
  * @param  size: Amount of data to be received
  * @retval Status, see @ref ald_status_t.
  */
597
ald_status_t ald_uart_recv_by_it(uart_handle_t *hperh, uint8_t *buf, uint16_t size)
W
wangyq2018 已提交
598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614
{
    assert_param(IS_UART_ALL(hperh->perh));

    if ((hperh->state != UART_STATE_READY) && (hperh->state != UART_STATE_BUSY_TX))
        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 = UART_ERROR_NONE;
    SET_BIT(hperh->state, UART_STATE_RX_MASK);
    __UNLOCK(hperh);

615
    ald_uart_interrupt_config(hperh, UART_IT_RXRD, ENABLE);
W
wangyq2018 已提交
616 617 618 619 620 621 622 623 624 625 626
    return OK;
}
#ifdef ALD_DMA
/**
  * @brief  Sends an amount of data in non blocking mode.
  * @param  hperh: Pointer to a uart_handle_t structure.
  * @param  buf: Pointer to data buffer
  * @param  size: Amount of data to be sent
  * @param  channel: DMA channel as UART transmit
  * @retval Status, see @ref ald_status_t.
  */
627
ald_status_t ald_uart_send_by_dma(uart_handle_t *hperh, uint8_t *buf, uint16_t size, uint8_t channel)
W
wangyq2018 已提交
628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652
{
    assert_param(IS_UART_ALL(hperh->perh));

    if ((hperh->state != UART_STATE_READY) && (hperh->state != UART_STATE_BUSY_RX))
        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 = UART_ERROR_NONE;
    SET_BIT(hperh->state, UART_STATE_TX_MASK);

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

    hperh->hdmatx.cplt_cbk = uart_dma_send_cplt;
    hperh->hdmatx.cplt_arg = (void *)hperh;
    hperh->hdmatx.err_cbk  = uart_dma_error;
    hperh->hdmatx.err_arg  = (void *)hperh;

653
    ald_dma_config_struct(&hperh->hdmatx.config);
W
wangyq2018 已提交
654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677
    hperh->hdmatx.config.src     = (void *)buf;
    hperh->hdmatx.config.dst     = (void *)&hperh->perh->TBR;
    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.msigsel = DMA_MSIGSEL_UART_TXEMPTY;
    hperh->hdmatx.config.burst   = ENABLE;
    hperh->hdmatx.config.channel = channel;

    if (hperh->init.mode == UART_MODE_RS485)
    {
        hperh->hdmatx.config.src_inc    = DMA_DATA_INC_HALFWORD;
        hperh->hdmatx.config.data_width = DMA_DATA_SIZE_HALFWORD;
    }

    if (hperh->perh == UART0)
        hperh->hdmatx.config.msel = DMA_MSEL_UART0;
    else if (hperh->perh == UART1)
        hperh->hdmatx.config.msel = DMA_MSEL_UART1;
    else if (hperh->perh == UART2)
        hperh->hdmatx.config.msel = DMA_MSEL_UART2;
    else if (hperh->perh == UART3)
        hperh->hdmatx.config.msel = DMA_MSEL_UART3;
    else
678
        ;	/* do nothing */
W
wangyq2018 已提交
679

680
    ald_dma_config_basic(&hperh->hdmatx);
W
wangyq2018 已提交
681 682

    __UNLOCK(hperh);
683 684
    ald_uart_clear_flag_status(hperh, UART_IF_TC);
    ald_uart_dma_req_config(hperh, ENABLE);
W
wangyq2018 已提交
685 686 687 688 689 690 691 692 693 694 695 696

    return OK;
}

/**
  * @brief  Receives an amount of data in non blocking mode.
  * @param  hperh: Pointer to a uart_handle_t structure.
  * @param  buf: Pointer to data buffer
  * @param  size: Amount of data to be received
  * @param  channel: DMA channel as UART receive
  * @retval Status, see @ref ald_status_t.
  */
697
ald_status_t ald_uart_recv_by_dma(uart_handle_t *hperh, uint8_t *buf, uint16_t size, uint8_t channel)
W
wangyq2018 已提交
698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721
{
    assert_param(IS_UART_ALL(hperh->perh));

    if ((hperh->state != UART_STATE_READY) && (hperh->state != UART_STATE_BUSY_TX))
        return BUSY;

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

    __LOCK(hperh);

    hperh->rx_buf   = buf;
    hperh->rx_size  = size;
    hperh->err_code = UART_ERROR_NONE;
    SET_BIT(hperh->state, UART_STATE_RX_MASK);

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

    hperh->hdmarx.cplt_cbk = uart_dma_recv_cplt;
    hperh->hdmarx.cplt_arg = (void *)hperh;
    hperh->hdmarx.err_cbk  = uart_dma_error;
    hperh->hdmarx.err_arg  = (void *)hperh;

722
    ald_dma_config_struct(&hperh->hdmarx.config);
W
wangyq2018 已提交
723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748
    hperh->hdmarx.config.src     = (void *)&hperh->perh->RBR;
    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.msigsel = DMA_MSIGSEL_UART_RNR;
    hperh->hdmarx.config.burst   = ENABLE;
    hperh->hdmarx.config.channel = channel;

    if (hperh->init.mode == UART_MODE_RS485)
    {
        hperh->hdmarx.config.dst_inc    = DMA_DATA_INC_HALFWORD;
        hperh->hdmarx.config.data_width = DMA_DATA_SIZE_HALFWORD;
    }

    if (hperh->perh == UART0)
        hperh->hdmarx.config.msel = DMA_MSEL_UART0;
    else if (hperh->perh == UART1)
        hperh->hdmarx.config.msel = DMA_MSEL_UART1;
    else if (hperh->perh == UART2)
        hperh->hdmarx.config.msel = DMA_MSEL_UART2;
    else if (hperh->perh == UART3)
        hperh->hdmarx.config.msel = DMA_MSEL_UART3;
    else
        ;

749
    ald_dma_config_basic(&hperh->hdmarx);
W
wangyq2018 已提交
750
    __UNLOCK(hperh);
751
    ald_uart_dma_req_config(hperh, ENABLE);
W
wangyq2018 已提交
752 753 754 755 756 757 758 759 760

    return OK;
}

/**
  * @brief  Pauses the DMA Transfer.
  * @param  hperh: Pointer to a uart_handle_t structure.
  * @retval Status, see @ref ald_status_t.
  */
761
ald_status_t ald_uart_dma_pause(uart_handle_t *hperh)
W
wangyq2018 已提交
762 763 764
{
    assert_param(IS_UART_ALL(hperh->perh));

765
    ald_uart_dma_req_config(hperh, DISABLE);
W
wangyq2018 已提交
766 767 768 769 770 771 772 773
    return OK;
}

/**
  * @brief  Resumes the DMA Transfer.
  * @param  hperh: Pointer to a uart_handle_t structure.
  * @retval Status, see @ref ald_status_t.
  */
774
ald_status_t ald_uart_dma_resume(uart_handle_t *hperh)
W
wangyq2018 已提交
775 776 777
{
    assert_param(IS_UART_ALL(hperh->perh));

778
    ald_uart_dma_req_config(hperh, ENABLE);
W
wangyq2018 已提交
779 780 781 782 783 784 785 786
    return OK;
}

/**
  * @brief  Stops the DMA Transfer.
  * @param  hperh: Pointer to a uart_handle_t structure.
  * @retval Status, see @ref ald_status_t.
  */
787
ald_status_t ald_uart_dma_stop(uart_handle_t *hperh)
W
wangyq2018 已提交
788 789 790
{
    assert_param(IS_UART_ALL(hperh->perh));

791
    ald_uart_dma_req_config(hperh, DISABLE);
W
wangyq2018 已提交
792 793 794 795 796 797 798 799 800 801
    hperh->state = UART_STATE_READY;
    return OK;
}
#endif

/**
  * @brief  This function handles UART interrupt request.
  * @param  hperh: Pointer to a uart_handle_t structure.
  * @retval None
  */
802
void ald_uart_irq_handler(uart_handle_t *hperh)
W
wangyq2018 已提交
803 804 805 806
{
    assert_param(IS_UART_ALL(hperh->perh));

    /* Handle parity error */
807
    if ((ald_uart_get_status(hperh, UART_STATUS_PE)) != RESET)
W
wangyq2018 已提交
808 809 810
        hperh->err_code |= UART_ERROR_PE;

    /* Handle frame error */
811
    if ((ald_uart_get_status(hperh, UART_STATUS_FE)) != RESET)
W
wangyq2018 已提交
812 813 814
        hperh->err_code |= UART_ERROR_FE;

    /* Handle overflow error */
815
    if ((ald_uart_get_status(hperh, UART_STATUS_OE)) != RESET)
W
wangyq2018 已提交
816 817 818
        hperh->err_code |= UART_ERROR_ORE;

    /* Receive */
819
    if ((ald_uart_get_mask_flag_status(hperh, UART_IF_RXRD)) != RESET)
W
wangyq2018 已提交
820
    {
821
        ald_uart_clear_flag_status(hperh, UART_IF_RXRD);
W
wangyq2018 已提交
822 823 824
        __uart_recv_by_it(hperh);
    }

825 826
    /* Transmit */
    if ((ald_uart_get_mask_flag_status(hperh, UART_IF_TXS)) != RESET)
W
wangyq2018 已提交
827
    {
828
        ald_uart_clear_flag_status(hperh, UART_IF_TXS);
W
wangyq2018 已提交
829 830 831
        __uart_send_by_it(hperh);
    }

832 833
    /* End Transmit */
    if ((ald_uart_get_mask_flag_status(hperh, UART_IF_TC)) != RESET)
W
wangyq2018 已提交
834
    {
835
        ald_uart_clear_flag_status(hperh, UART_IF_TC);
W
wangyq2018 已提交
836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860
        __uart_end_send_by_it(hperh);
    }

    /* Handle error state */
    if (hperh->err_code != UART_ERROR_NONE)
    {
        hperh->state = UART_STATE_READY;

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

/** @defgroup UART_Public_Functions_Group3 Peripheral Control functions
  *  @brief   UART control functions
  *
  * @verbatim
  ==============================================================================
                      ##### Peripheral Control functions #####
  ==============================================================================
  [..]
    This subsection provides a set of functions allowing to control the UART:
861 862 863 864 865 866 867 868 869 870 871 872
    (+) ald_uart_interrupt_config() API can be helpful to configure UART interrupt source.
    (+) ald_uart_dma_req_config() API can be helpful to configure UART DMA request.
    (+) ald_uart_tx_fifo_config() API can be helpful to configure UART TX FIFO paramters.
    (+) ald_uart_rx_fifo_config() API can be helpful to configure UART RX FIFO paramters.
    (+) ald_uart_lin_send_break() API can send a frame of break in LIN mode.
    (+) ald_uart_lin_detect_break_len_config() API can be helpful to configure the length of break frame.
    (+) ald_uart_auto_baud_config() API can be helpful to configure detection data mode.
    (+) ald_uart_get_it_status() API can get the status of interrupt source.
    (+) ald_uart_get_status() API can get the status of UART_SR register.
    (+) ald_uart_get_flag_status() API can get the status of UART flag.
    (+) ald_uart_get_mask_flag_status() API can get status os flag and interrupt source.
    (+) ald_uart_clear_flag_status() API can clear UART flag.
W
wangyq2018 已提交
873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888

    @endverbatim
  * @{
  */

/**
  * @brief  Enable/disable the specified UART interrupts.
  * @param  hperh: Pointer to a uart_handle_t structure.
  * @param  it: Specifies the UART interrupt sources to be enabled or disabled.
  *         This parameter can be one of the @ref uart_it_t.
  * @param  state: New state of the specified UART interrupts.
  *         This parameter can be:
  *             @arg ENABLE
  *             @arg DISABLE
  * @retval None
  */
889
void ald_uart_interrupt_config(uart_handle_t *hperh, uart_it_t it, type_func_t state)
W
wangyq2018 已提交
890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911
{
    assert_param(IS_UART_ALL(hperh->perh));
    assert_param(IS_UART_IT(it));
    assert_param(IS_FUNC_STATE(state));

    if (state == ENABLE)
        WRITE_REG(hperh->perh->IER, it);
    else
        WRITE_REG(hperh->perh->IDR, it);

    return;
}

/**
  * @brief  Configure UART DMA request.
  * @param  hperh: Pointer to a uart_handle_t structure.
  * @param  state: New state of the specified DMA request.
  *         This parameter can be:
  *             @arg ENABLE
  *             @arg DISABLE
  * @retval None
  */
912
void ald_uart_dma_req_config(uart_handle_t *hperh, type_func_t state)
W
wangyq2018 已提交
913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931
{
    assert_param(IS_UART_ALL(hperh->perh));
    assert_param(IS_FUNC_STATE(state));

    if (state == ENABLE)
        SET_BIT(hperh->perh->MCR, UART_MCR_DMAEN_MSK);
    else
        CLEAR_BIT(hperh->perh->MCR, UART_MCR_DMAEN_MSK);

    return;
}

/**
  * @brief  Configure transmit fifo parameters.
  * @param  hperh: Pointer to a uart_handle_t structure.
  * @param  config: Transmit fifo trigger level.
  * @param  level: Transmit fifo level.
  * @retval None
  */
932
void ald_uart_tx_fifo_config(uart_handle_t *hperh, uart_rxfifo_t config, uint8_t level)
W
wangyq2018 已提交
933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951
{
    assert_param(IS_UART_ALL(hperh->perh));
    assert_param(IS_UART_TXFIFO_TYPE(config));

    SET_BIT(hperh->perh->FCR, UART_FCR_TFRST_MSK);
    MODIFY_REG(hperh->perh->FCR, UART_FCR_TXTL_MSK, config << UART_FCR_TXTL_POSS);
    MODIFY_REG(hperh->perh->FCR, UART_FCR_TXFL_MSK, level << UART_FCR_TXFL_POSS);
    SET_BIT(hperh->perh->FCR, UART_FCR_FIFOEN_MSK);

    return;
}

/**
  * @brief  Configure receive fifo parameters.
  * @param  hperh: Pointer to a uart_handle_t structure.
  * @param  config: Receive fifo trigger level.
  * @param  level: Receive fifo level.
  * @retval None
  */
952
void ald_uart_rx_fifo_config(uart_handle_t *hperh, uart_rxfifo_t config, uint8_t level)
W
wangyq2018 已提交
953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969
{
    assert_param(IS_UART_ALL(hperh->perh));
    assert_param(IS_UART_RXFIFO_TYPE(config));

    SET_BIT(hperh->perh->FCR, UART_FCR_RFRST_MSK);
    MODIFY_REG(hperh->perh->FCR, UART_FCR_RXTL_MSK, config << UART_FCR_RXTL_POSS);
    MODIFY_REG(hperh->perh->FCR, UART_FCR_RXFL_MSK, level << UART_FCR_RXFL_POSS);
    SET_BIT(hperh->perh->FCR, UART_FCR_FIFOEN_MSK);

    return;
}

/**
  * @brief  request to send a frame of break.
  * @param  hperh: Pointer to a uart_handle_t structure.
  * @retval None
  */
970
void ald_uart_lin_send_break(uart_handle_t *hperh)
W
wangyq2018 已提交
971 972 973 974 975 976 977 978 979 980 981 982 983 984 985
{
    assert_param(IS_UART_ALL(hperh->perh));

    SET_BIT(hperh->perh->MCR, UART_MCR_BKREQ_MSK);
    return;
}

/**
  * @brief  Configure the length of break frame to be detect.
  * @param  hperh: Pointer to a uart_handle_t structure.
  * @param  len: Length of break frame.
  *           @arg LIN_BREAK_LEN_10B
  *           @arg LIN_BREAK_LEN_11B
  * @retval None
  */
986
void ald_uart_lin_detect_break_len_config(uart_handle_t *hperh, uart_lin_break_len_t len)
W
wangyq2018 已提交
987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003
{
    assert_param(IS_UART_ALL(hperh->perh));
    assert_param(IS_UART_LIN_BREAK_LEN(len));

    MODIFY_REG(hperh->perh->MCR, UART_MCR_LINBDL_MSK, len << UART_MCR_LINBDL_POS);
    return;
}

/**
  * @brief  Configure the mode of auto-baud-rate detect.
  * @param  hperh: Pointer to a uart_handle_t structure.
  * @param  mode: The mode of auto-baud-rate detect.
  *           @arg UART_ABRMOD_1_TO_0
  *           @arg UART_ABRMOD_1
  *           @arg UART_ABRMOD_0_TO_1
  * @retval None
  */
1004
void ald_uart_auto_baud_config(uart_handle_t *hperh, uart_auto_baud_mode_t mode)
W
wangyq2018 已提交
1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020
{
    assert_param(IS_UART_ALL(hperh->perh));
    assert_param(IS_UART_AUTO_BAUD_MODE(mode));

    MODIFY_REG(hperh->perh->MCR, UART_MCR_ABRMOD_MSK, mode << UART_MCR_ABRMOD_POSS);
    return;
}

/**
  * @brief  Send address in RS485 mode.
  * @param  hperh: Pointer to a uart_handle_t structure that contains
  *         the configuration information for the specified UART module.
  * @param  addr: the address of RS485 device.
  * @param  timeout: Timeout duration
  * @retval The ALD status.
  */
1021
ald_status_t ald_uart_rs485_send_addr(uart_handle_t *hperh, uint16_t addr, uint32_t timeout)
W
wangyq2018 已提交
1022 1023 1024 1025 1026 1027 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
{
    assert_param(IS_UART_ALL(hperh->perh));

    if ((hperh->state != UART_STATE_READY) && (hperh->state != UART_STATE_BUSY_RX))
        return BUSY;

    SET_BIT(hperh->state, UART_STATE_TX_MASK);

    if (uart_wait_flag(hperh, UART_STATUS_TBEM, SET, timeout) != OK)
    {
        hperh->state = UART_STATE_READY;
        return TIMEOUT;
    }

    WRITE_REG(hperh->perh->TBR, (addr | 0x100));

    if (uart_wait_flag(hperh, UART_STATUS_TEM, SET, timeout) != OK)
    {
        hperh->state = UART_STATE_READY;
        return TIMEOUT;
    }

    CLEAR_BIT(hperh->state, UART_STATE_TX_MASK);

    return OK;
}

/**
  * @brief  Get the status of UART interrupt source.
  * @param  hperh: Pointer to a uart_handle_t structure.
  * @param  it: Specifies the UART interrupt source.
  *         This parameter can be one of the @ref uart_it_t.
  * @retval Status:
  *           - 0: RESET
  *           - 1: SET
  */
1058
it_status_t ald_uart_get_it_status(uart_handle_t *hperh, uart_it_t it)
W
wangyq2018 已提交
1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077
{
    assert_param(IS_UART_ALL(hperh->perh));
    assert_param(IS_UART_IT(it));

    if (READ_BIT(hperh->perh->IVS, it))
        return SET;

    return RESET;
}

/**
  * @brief  Get the status of UART_SR register.
  * @param  hperh: Pointer to a uart_handle_t structure.
  * @param  status: Specifies the UART status type.
  *         This parameter can be one of the @ref uart_status_t.
  * @retval Status:
  *           - 0: RESET
  *           - 1: SET
  */
1078
flag_status_t ald_uart_get_status(uart_handle_t *hperh, uart_status_t status)
W
wangyq2018 已提交
1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098
{
    assert_param(IS_UART_ALL(hperh->perh));
    assert_param(IS_UART_STATUS(status));

    if (READ_BIT(hperh->perh->SR, status))
        return SET;

    return RESET;
}


/**
  * @brief  Get the status of UART interrupt flag.
  * @param  hperh: Pointer to a uart_handle_t structure.
  * @param  flag: Specifies the UART interrupt flag.
  *         This parameter can be one of the @ref uart_flag_t.
  * @retval Status:
  *           - 0: RESET
  *           - 1: SET
  */
1099
flag_status_t ald_uart_get_flag_status(uart_handle_t *hperh, uart_flag_t flag)
W
wangyq2018 已提交
1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118
{
    assert_param(IS_UART_ALL(hperh->perh));
    assert_param(IS_UART_IF(flag));

    if (READ_BIT(hperh->perh->RIF, flag))
        return SET;

    return RESET;
}

/**
  * @brief  Get the status of interrupt flag and interupt source.
  * @param  hperh: Pointer to a uart_handle_t structure.
  * @param  flag: Specifies the UART interrupt flag.
  *         This parameter can be one of the @ref uart_flag_t.
  * @retval Status:
  *           - 0: RESET
  *           - 1: SET
  */
1119
flag_status_t ald_uart_get_mask_flag_status(uart_handle_t *hperh, uart_flag_t flag)
W
wangyq2018 已提交
1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136
{
    assert_param(IS_UART_ALL(hperh->perh));
    assert_param(IS_UART_IF(flag));

    if (READ_BIT(hperh->perh->IFM, flag))
        return SET;

    return RESET;
}

/**
  * @brief  Clear the UART interrupt flag.
  * @param  hperh: Pointer to a uart_handle_t structure.
  * @param  flag: Specifies the UART interrupt flag.
  *         This parameter can be one of the @ref uart_flag_t.
  * @retval None
  */
1137
void ald_uart_clear_flag_status(uart_handle_t *hperh, uart_flag_t flag)
W
wangyq2018 已提交
1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159
{
    assert_param(IS_UART_ALL(hperh->perh));
    assert_param(IS_UART_IF(flag));

    WRITE_REG(hperh->perh->ICR, flag);
    return;
}
/**
  * @}
  */

/** @defgroup UART_Public_Functions_Group4 Peripheral State and Errors functions
  *  @brief   UART State and Errors functions
  *
@verbatim
  ==============================================================================
                 ##### Peripheral State and Errors functions #####
  ==============================================================================
 [..]
   This subsection provides a set of functions allowing to return the State of
   UART communication process, return Peripheral Errors occurred during communication
   process
1160 1161
   (+) ald_uart_get_state() API can be helpful to check in run-time the state of the UART peripheral.
   (+) ald_uart_get_error() check in run-time errors that could be occurred during communication.
W
wangyq2018 已提交
1162 1163 1164 1165 1166 1167 1168 1169 1170 1171

@endverbatim
  * @{
  */

/**
  * @brief  Returns the UART state.
  * @param  hperh: Pointer to a uart_handle_t structure.
  * @retval ALD state
  */
1172
uart_state_t ald_uart_get_state(uart_handle_t *hperh)
W
wangyq2018 已提交
1173 1174 1175 1176 1177 1178 1179 1180 1181
{
    return hperh->state;
}

/**
  * @brief  Return the UART error code
  * @param  hperh: Pointer to a uart_handle_t structure.
  * @retval UART Error Code
  */
1182
uint32_t ald_uart_get_error(uart_handle_t *hperh)
W
wangyq2018 已提交
1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202
{
    return hperh->err_code;
}

/**
  * @}
  */

/**
  * @}
  */
#endif /* ALD_UART */

/**
  * @}
  */

/**
  * @}
  */