UART

[English]

UART API Status

UART Port Number

Capability

BK7258

Port Number

3

UART Pin and GPIO Map

UART Pin

BK7258

UART0 TX

11

UART0 RX

10

UART1 TX

0

UART1 RX

1

UART2 TX

41

UART2 RX

40

UART API Reference

Header File

Functions

bk_err_t bk_uart_driver_init(void)

Init the UART driver.

This API init the resoure common to all UART id:

  • Init UART driver control memory

Attention

1. This API should be called before any other UART APIs.

Returns

  • BK_OK: succeed

  • others: other errors.

bk_err_t bk_uart_driver_deinit(void)

Deinit the UART driver.

This API free all resource related to UART and power down all UART id.

Returns

  • BK_OK: succeed

  • others: other errors.

int bk_uart_is_in_used(uart_id_t id)

Get the UART whether in used.

Parameters

id – UART id

Returns

  • 1: in used

  • 0: not in used

int bk_uart_is_rx_dma_enabled(uart_id_t id)

Get the UART whether rx dma is enabled.

Parameters

id – UART id

Returns

  • 1: rx dma is enabled

  • 0: rx dma is disabled

bk_err_t bk_uart_init(uart_id_t id, const uart_config_t *config)

Init the UART id.

This API init the UART id:

  • Power up the UART id

  • Configure the UART id clock

  • Map the UART id to dedicated GPIO port

  • Set the UART baud rate, data bits, parity, stop bits,flow ctrl and source clock

  • Start the UART id

Parameters
  • id – UART id

  • config – UART parameter settings

Returns

  • BK_OK: succeed

  • BK_ERR_UART_NOT_INIT: UART driver not init

  • BK_ERR_NULL_PARAM: UART config parameter is NULL

  • BK_ERR_UART_INVALID_ID: UART id is invalid

  • BK_ERR_UART_BAUD_RATE_NOT_SUPPORT: UART baud rate is not support

  • others: other errors.

bk_err_t bk_uart_deinit(uart_id_t id)

Deinit the UART id.

This API deinit the UART id:

  • Stop the UART id

  • Reset all configuration of UART id to default value

  • Disable the UART id interrupt

  • Power down the UART id

Parameters

id – UART id

Returns

  • BK_OK: succeed

  • others: other errors.

bk_err_t bk_uart_set_baud_rate(uart_id_t id, uint32_t baud_rate)

Set the UART baud rate.

Parameters
  • id – UART id

  • baud_rate – UART baud rate

Returns

  • BK_OK: succeed

  • others: other errors.

bk_err_t bk_uart_set_data_bits(uart_id_t id, uart_data_bits_t data_bits)

Set the UART data bits.

Parameters
  • id – UART id

  • data_bits – UART data bits

Returns

  • BK_OK: succeed

  • others: other errors.

bk_err_t bk_uart_set_stop_bits(uart_id_t id, uart_stop_bits_t stop_bits)

Set the UART stop bits.

Parameters
  • id – UART id

  • stop_bits – UART stop bits

Returns

  • BK_OK: succeed

  • others: other errors.

bk_err_t bk_uart_set_parity(uart_id_t id, uart_parity_t partiy)

Set the UART partiy.

Parameters
  • id – UART id

  • partiy – UART partiy

Returns

  • BK_OK: succeed

  • others: other errors.

bk_err_t bk_uart_set_hw_flow_ctrl(uart_id_t id, uint8_t rx_threshold)

Set the UART hardware flow control.

Parameters
  • id – UART id

  • rx_threshold – Threshold of Hardware RX flow control

Returns

  • BK_OK: succeed

  • others: other errors.

bk_err_t bk_uart_set_rx_full_threshold(uart_id_t id, uint8_t threshold)

Set the UART threshold value for RX fifo full.

Parameters
  • id – UART id

  • threshold – Threshold value above which RX fifo full interrupt is generated

Returns

  • BK_OK: succeed

  • others: other errors.

bk_err_t bk_uart_set_tx_empty_threshold(uart_id_t id, uint8_t threshold)

Set the UART threshold value for TX fifo empty.

Parameters
  • id – UART id

  • threshold – Threshold value below which TX fifo empty interrupt is generated

Returns

  • BK_OK: succeed

  • others: other errors.

bk_err_t bk_uart_set_rx_timeout(uart_id_t id, uart_rx_stop_detect_time_t timeout_thresh)

Set the UART threshold timeout for receive data, unit is bit.

UART finish receiving data when the periods is more than timeout threshold. If the time is expired, the UART_RX_STOP_END interrupt is triggered.

Parameters
  • id – UART id

  • timeout_thresh – timeout threshold

Returns

  • BK_OK: succeed

  • others: other errors.

bk_err_t bk_uart_disable_hw_flow_ctrl(uart_id_t id)

Disable the UART hardware flow control.

Parameters

id – UART id

Returns

  • BK_OK: succeed

  • others: other errors.

bk_err_t bk_uart_enable_tx_interrupt(uart_id_t id)

Enable TX interrupt of UART id.

Parameters

id – UART id

Returns

  • BK_OK: succeed

  • others: other errors.

bk_err_t bk_uart_disable_tx_interrupt(uart_id_t id)

Disable TX interrupt of UART id.

Parameters

id – UART id

Returns

  • BK_OK: succeed

  • others: other errors.

bk_err_t bk_uart_enable_rx_interrupt(uart_id_t id)

Enable RX interrupt of UART id.

Parameters

id – UART id

Returns

  • BK_OK: succeed

  • others: other errors.

bk_err_t bk_uart_disable_rx_interrupt(uart_id_t id)

Disable RX interrupt of UART id.

Parameters

id – UART id

Returns

  • BK_OK: succeed

  • others: other errors.

bk_err_t bk_uart_register_rx_isr(uart_id_t id, uart_isr_t isr, void *param)

Register the RX interrupt service routine for UART id.

Parameters
  • id – UART id

  • isr – UART RX callback

  • param – UART RX callback parameter

Returns

  • BK_OK: succeed

  • others: other errors.

bk_err_t bk_uart_register_tx_isr(uart_id_t id, uart_isr_t isr, void *param)

Register the TX interrupt service routine for UART id.

Parameters
  • id – UART id

  • isr – UART TX callback

  • param – UART TX callback parameter

Returns

  • BK_OK: succeed

  • others: other errors.

bk_err_t bk_uart_write_bytes(uart_id_t id, const void *data, uint32_t size)

Send data to the UART port from a given buffer and length.

Parameters
  • id – UART id

  • data – data buffer address

  • size – data length to send

Returns

  • BK_OK: succeed

  • others: other errors.

int bk_uart_read_bytes(uart_id_t id, void *data, uint32_t size, uint32_t timeout_ms)

UART read bytes from UART buffer.

Parameters
  • id – UART id

  • data – pointer to the buffer

  • size – data length to read

  • timeout_ms – timeout ms, if set BEKEN_WAIT_FOREVER, read will wait forever

Returns

  • BK_ERR_UART_NOT_INIT: UART driver not init

  • BK_ERR_UART_INVALID_ID: UART id number is invalid

  • BK_ERR_UART_ID_NOT_INIT: UART id not init

  • BK_ERR_UART_RX_TIMEOUT: UART receive data timeout

  • others (>=0): The number of bytes read from UART FIFO

bk_err_t bk_uart_disable_rx(uart_id_t id)

Disable UART TX pin and set as GPIO.

Parameters

id – UART id

Returns

  • BK_OK: succeed

  • others: other errors.

bk_err_t bk_uart_disable_tx(uart_id_t id)

Disable UART TX pin and set as GPIO.

Parameters

id – UART id

Returns

  • BK_OK: succeed

  • others: other errors.

bk_err_t bk_uart_set_enable_rx(uart_id_t id, bool enable)

Just set UART RX enable/disable, doesn’t set pin.

Parameters
  • id – UART id

  • enable – true, enable, flase disable

Returns

  • BK_OK: succeed

  • others: other errors.

bk_err_t bk_uart_set_enable_tx(uart_id_t id, bool enable)

Just set UART TX enable/disable, doesn’t set pin.

Parameters
  • id – UART id

  • enable – true, enable, flase disable

Returns

  • BK_OK: succeed

  • others: other errors.

bk_err_t bk_uart_enable_sw_fifo(uart_id_t id)

Enable UART software fifo.

Parameters

id – UART id

Returns

  • BK_OK: succeed

  • others: other errors.

bk_err_t bk_uart_disable_sw_fifo(uart_id_t id)

Disable UART software fifo.

Parameters

id – UART id

Returns

  • BK_OK: succeed

  • others: other errors.

uint32_t bk_uart_get_ate_detect_gpio(void)

Get the gpio pin when in ate detect.

Returns

the ate detect gpio pin

bk_err_t bk_uart_take_rx_isr(uart_id_t id, uart_isr_t isr, void *param)

Register uart rx isr and disable software fifo.

Parameters
  • id – UART id

  • isr – UART RX callback

  • param – UART RX callback parameter

Returns

  • BK_OK: succeed

  • others: other errors.

bk_err_t bk_uart_recover_rx_isr(uart_id_t id)

Recover uart rx isr.

Parameters

id – UART id

Returns

  • BK_OK: succeed

  • others: other errors.

bool bk_uart_is_tx_over(uart_id_t id)

Get whether the uart tx over(tx fifo empty)

Parameters

id – UART id

Returns

  • true: uart tx over

  • false: uart tx not over

bk_err_t bk_uart_pm_backup(uart_id_t id)

Backup uart config.

Parameters

id – UART id

Returns

  • BK_OK: succeed

  • others: other errors.

UART API Typedefs

Header File

Macros

BK_ERR_UART_NOT_INIT

UART driver not init

BK_ERR_UART_INVALID_ID

UART id number is invalid

BK_ERR_UART_INVALID_SIZE

UART read bytes length is more than fifo length

BK_ERR_UART_RX_TIMEOUT

UART receive data timeout

BK_ERR_UART_BAUD_RATE_NOT_SUPPORT

UART baud rate not support

BK_ERR_UART_ID_NOT_INIT

UART id not init

BK_ERR_UART_MUTEX_INIT

UART mutex init failed

Type Definitions

typedef void (*uart_isr_t)(uart_id_t id, void *param)

UART interrupt service routine.

Header File

Structures

struct uart_config_t

Public Members

uint32_t baud_rate

UART baud rate

uart_data_bits_t data_bits

UART data bits

uart_parity_t parity

UART parity

uart_stop_bits_t stop_bits

UART stop bits

uart_flow_control_t flow_ctrl

UART flow control

uart_src_clk_t src_clk

UART source clock

Macros

UART_BAUDRATE_3250000
UART_BAUDRATE_2000000
UART_BAUDRATE_921600
UART_BAUDRATE_460800
UART_BAUDRATE_230400
UART_BAUDRATE_115200
UART_BAUDRATE_3000
UART_BAUDRATE_19200
UART_BAUDRATE_9600
UART_BAUD_RATE
UART_CLOCK_FREQ_10M
UART_CLOCK_FREQ_48M
UART_CLOCK_FREQ_24M
UART_CLOCK_FREQ_26M
UART_CLOCK_FREQ_52M
UART_CLOCK_FREQ_120M
UART_CLOCK
UART_TX_FIFO_THRESHOLD
UART_RX_FIFO_THRESHOLD

Type Definitions

typedef uint8_t uart_unit_t

uart uint id

Enumerations

enum uart_id_t

Values:

enumerator UART_ID_0

UART id 0

enumerator UART_ID_1

UART id 1

enumerator UART_ID_2

UART id 2

enumerator UART_ID_MAX

UART id max

enum uart_src_clk_t

Values:

enumerator UART_SCLK_DCO

UART source clock dco

enumerator UART_SCLK_XTAL_26M

UART source clock xtal 26M

enumerator UART_SCLK_APLL

UART source clock apll

enum uart_frame_mode_t

Values:

enumerator UART_MODE_UART

UART frame mode uart

enumerator UART_MODE_IDRA

UART frame mode idra

enum uart_data_bits_t

Values:

enumerator UART_DATA_5_BITS

UART data bits 5bits

enumerator UART_DATA_6_BITS

UART data bits 6bits

enumerator UART_DATA_7_BITS

UART data bits 7bits

enumerator UART_DATA_8_BITS

UART data bits 8bits

enum uart_parity_t

Values:

enumerator UART_PARITY_NONE

UART parity none

enumerator UART_PARITY_ODD

UART parity odd

enumerator UART_PARITY_EVEN

UART parity even

enum uart_stop_bits_t

Values:

enumerator UART_STOP_BITS_1

UART stop bits 1bit

enumerator UART_STOP_BITS_2

UART stop bits 2bits

enum uart_flow_control_t

Values:

enumerator UART_FLOWCTRL_DISABLE

UART flow control disable

enumerator UART_FLOWCTRL_CTS_RTS

UART flow control cts rts

enum uart_rx_stop_detect_time_t

Values:

enumerator UART_RX_STOP_DETECT_TIME_32_BITS

UART rx stop time 32 bits

enumerator UART_RX_STOP_DETECT_TIME_64_BITS

UART rx stop time 64 bits

enumerator UART_RX_STOP_DETECT_TIME_128_BITS

UART rx stop time 128 bits

enumerator UART_RX_STOP_DETECT_TIME_256_BITS

UART rx stop time 256 bits

enum uart_dma_enable_t

Values:

enumerator UART_DMA_DISABLE
enumerator UART_DMA_ENABLE