UART Driver

[中文]

Overview

UART (Universal Asynchronous Receiver/Transmitter) is a common serial interface supporting full-duplex asynchronous data transmission. BK7239N integrates multiple UART ports for flexible serial communications.

Functional Description

  • Multiple ports working concurrently

  • Configurable baud rate, data bits, stop bits, parity

  • Hardware/software flow control (RTS/CTS, XON/XOFF)

  • Interrupt support for RX/TX

  • DMA support to reduce CPU load

  • Optional software FIFO for smoother RX

  • Low power backup/restore support

Development Guide

1) Initialization - Call bk_uart_driver_init() once before using any UART APIs - Configure uart_config_t and call bk_uart_init(id, &config) - Optionally register ISR and enable interrupts/DMA

2) Basic I/O - bk_uart_write_bytes() / bk_uart_write_string() to transmit - bk_uart_read_bytes() to receive

3) Interrupts - Register RX ISR with bk_uart_register_rx_isr() - Enable RX interrupt via bk_uart_enable_rx_interrupt()

Notes

  • Ensure driver init before per-port init

  • Match baud rate and format with peer device

  • Consider RX timeout/threshold and flow control for heavy traffic

  • Mind cache/alignment when using DMA

API Reference

  • bk_uart_driver_init, bk_uart_driver_deinit

  • bk_uart_init, bk_uart_deinit

  • bk_uart_write_bytes, bk_uart_read_bytes, bk_uart_write_string

  • bk_uart_set_baud_rate, bk_uart_set_data_bits, bk_uart_set_stop_bits, bk_uart_set_parity, bk_uart_set_flow_ctrl

  • bk_uart_enable_rx_interrupt, bk_uart_disable_rx_interrupt, bk_uart_register_rx_isr

  • bk_uart_set_rx_threshold, bk_uart_set_rx_timeout

  • bk_uart_get_tx_over_status, bk_uart_clear_tx_over_status

  • bk_uart_power_save_backup, bk_uart_power_save_restore

Examples

Basic transmit/receive:

uart_config_t cfg = {
    .baud_rate = 115200,
    .data_bits = UART_DATA_8BITS,
    .stop_bits = UART_STOP_1BITS,
    .parity = UART_PARITY_NONE,
    .flow_ctrl = UART_HW_FLOWCTRL_NONE,
    .source_clk = UART_SCLK_APB,
};
bk_uart_driver_init();
bk_uart_init(UART_ID_0, &cfg);
bk_uart_write_string(UART_ID_0, "Hello UART!\n");
uint8_t buf[64];
bk_uart_read_bytes(UART_ID_0, buf, sizeof(buf), 1000);

RX interrupt example:

static void on_rx(uart_id_t id, void *param) {
    uint8_t ch;
    while (bk_uart_read_bytes(id, &ch, 1, 0) > 0) {}
}
bk_uart_register_rx_isr(UART_ID_0, on_rx, NULL);
bk_uart_enable_rx_interrupt(UART_ID_0);

FAQs

  • Baud not effective: verify init order and clock source capability

  • RX loss: tune RX threshold/timeout, enable flow control or DMA

  • ISR not triggered: confirm ISR registration and interrupt enable

Error Codes

  • BK_ERR_UART_NOT_INIT, BK_ERR_UART_INVALID_ID

  • BK_ERR_UART_BAUD_RATE_NOT_SUPPORT, BK_ERR_UART_ID_NOT_INIT