UART
概述
通用异步收发传输器(Universal Asynchronous Receiver/Transmitter,通常称为UART)是一种异步收发传输器,提供了与外部设备进行全双工数据交换的灵活方式。 BL602共有2组UART口(UART0和UART1),通过配合DMA使用,可以实现高效的数据通信。
注解
需要注意的是gpio的pin脚作为uart功能使用时,选取的gpio pin脚对8取余不能相等,如gpio0和gpio8不能同时做为uart的引脚,uart更多资料请参考 BL602参考手册 , BL602参考手册的3.2.8 GPIO功能设定 。
应用实例
串口实例: customer_app/peripheral/demo_uart
。
API参考
Header File
- components/platform/hosal/include/hosal_uart.h
Functions
-
int
hosal_uart_abr_get
(hosal_uart_dev_t *uart, uint8_t mode) Get auto baudrate on a UART interface.
- Return
- = 0 on success
- EIO if an error occurred with any step
- Parameters
uart
: the UART interfacemode
: auto baudrate detection mode(codeword 0x55 or start bit)
-
int
hosal_uart_init
(hosal_uart_dev_t *uart) Initialises a UART interface.
- Return
- 0 on success
- EIO if an error occurred with any step
- Parameters
uart
: the interface which should be initialised
-
int
hosal_uart_send
(hosal_uart_dev_t *uart, const void *txbuf, uint32_t size) Poll transmit data on a UART interface.
- Return
- > 0 on success
- EIO if an error occurred with any step
- Parameters
uart
: the UART interfacetxbuf
: pointer to the start of datasize
: number of bytes to transmit
-
int
hosal_uart_receive
(hosal_uart_dev_t *uart, void *data, uint32_t expect_size) Poll receive data on a UART interface.
- Return
- > 0 number of bytes to receive
- EIO if an error occurred with any step
- Parameters
uart
: the UART interfacerxbuf
: pointer to the buffer which will store incoming dataexpect_size
: expect number of bytes to receive
-
int
hosal_uart_ioctl
(hosal_uart_dev_t *uart, int ctl, void *p_arg) hal uart ioctl
- Return
- 0 on success
- EIO if an error occurred with any step
- Parameters
uart
: the UART interfacectl
: Control request- HOSAL_UART_BAUD_SET : baud set, p_arg is baud
- HOSAL_UART_BAUD_GET : baud get, p_arg is baud's pointer
- HOSAL_UART_DATA_WIDTH_SET : set data width,p_arg is hosal_uart_data_width_t
- HOSAL_UART_DATA_WIDTH_GET : get data width,p_arg is hosal_uart_data_width_t's pointer
- HOSAL_UART_STOP_BITS_SET : set stop bits, p_arg is hosal_uart_stop_bits_t
- HOSAL_UART_STOP_BITS_GET : get stop bits, p_arg is hosal_uart_stop_bits_t's pointer
- HOSAL_UART_PARITY_SET : set parity, p_arg is hosal_uart_parity_t
- HOSAL_UART_PARITY_GET : get parity, p_arg is hosal_uart_parity_t's pointer
- HOSAL_UART_MODE_SET : UART mode set, p_arg is hosal_uart_mode_t
- HOSAL_UART_MODE_GET : UART mode get, p_arg is hosal_uart_mode_t's pointer
- HOSAL_UART_FLOWMODE_SET : UART flow mode set, p_arg is hosal_uart_flow_control_t
- HOSAL_UART_FLOWSTAT_GET : UART flow state get, p_arg is hosal_uart_flow_control_t's pointer
- HOSAL_UART_FREE_TXFIFO_GET : get uart free tx fifo size (bytes)
- HOSAL_UART_FREE_RXFIFO_GET : get uart free rx fifo size (bytes)
- HOSAL_UART_FLUSH : Wait for the send to complete
- HOSAL_UART_TX_TRIGGER_ON : UART TX trigger on
- HOSAL_UART_TX_TRIGGER_OFF : UART TX trigger off
- HOSAL_UART_DMA_CONFIG : p_arg is hosal_uart_dma_cfg_t's pointer
- HOSAL_UART_DMA_TX_START : UART DMA TX start trans p_arg is hosal_uart_dma_cfg_t's pointer
- HOSAL_UART_DMA_RX_START : UART DMA RX start trans p_arg is hosal_uart_dma_cfg_t's pointer
p_arg
: parameter
-
int
hosal_uart_callback_set
(hosal_uart_dev_t *uart, int callback_type, hosal_uart_callback_t pfn_callback, void *arg) hal uart callback set
- Return
- 0 on success
- EIO if an error occurred with any step
- Parameters
uart
: the UART interfacecallback_type
: callback type- HOSAL_UART_TX_CALLBACK
- HOSAL_UART_RX_CALLBACK
- HOSAL_UART_TX_DMA_CALLBACK
- HOSAL_UART_RX_DMA_CALLBACK
pfn_callback
: callback functionarg
: callback function parameter
-
int
hosal_uart_finalize
(hosal_uart_dev_t *uart) Deinitialises a UART interface.
- Return
- 0 on success
- EIO if an error occurred with any step
- Parameters
uart
: the interface which should be deinitialised
Structures
-
struct
hosal_uart_dma_cfg_t
UART DMA configuration.
-
struct
hosal_uart_config_t
UART configuration.
Public Members
-
uint8_t
uart_id
UART id.
-
uint8_t
tx_pin
UART tx pin.
-
uint8_t
rx_pin
UART rx pin.
-
uint8_t
cts_pin
UART cts pin.
-
uint8_t
rts_pin
UART rts pin.
-
uint32_t
baud_rate
UART baud rate.
-
hosal_uart_data_width_t
data_width
UART data width.
-
hosal_uart_parity_t
parity
UART parity bit.
-
hosal_uart_stop_bits_t
stop_bits
UART stop btis.
-
hosal_uart_flow_control_t
flow_control
UART flow control.
-
hosal_uart_mode_t
mode
UART int or pull mode.
-
uint8_t
-
struct
hosal_uart_dev_t
UART device type.
Public Members
-
uint8_t
port
UART port.
-
hosal_uart_config_t
config
UART config.
-
hosal_uart_callback_t
tx_cb
UART tx callback.
-
void *
p_txarg
UART tx callback arg.
-
hosal_uart_callback_t
rx_cb
UART rx callback.
-
void *
p_rxarg
UART rx callback arg.
-
hosal_uart_callback_t
txdma_cb
UART tx dma callback.
-
void *
p_txdma_arg
UART tx dma callback arg.
-
hosal_uart_callback_t
rxdma_cb
UART rx dma callback.
-
void *
p_rxdma_arg
UART rx dma callback arg.
-
hosal_dma_chan_t
dma_tx_chan
UART dma tx channel.
-
hosal_dma_chan_t
dma_rx_chan
UART dma rx channel.
-
void *
priv
UART private data.
-
uint8_t
Macros
-
HOSAL_UART_AUTOBAUD_0X55
UART auto baudrate detection using codeword 0x55.
-
HOSAL_UART_AUTOBAUD_STARTBIT
UART auto baudrate detection using start bit.
-
HOSAL_UART_TX_CALLBACK
UART tx idle interrupt callback.
-
HOSAL_UART_RX_CALLBACK
UART rx complete callback.
-
HOSAL_UART_TX_DMA_CALLBACK
UART tx DMA trans complete callback.
-
HOSAL_UART_RX_DMA_CALLBACK
UART rx DMA trans complete callback.
-
HOSAL_UART_BAUD_SET
UART baud set.
-
HOSAL_UART_BAUD_GET
UART baud get.
-
HOSAL_UART_DATA_WIDTH_SET
UART data width set.
-
HOSAL_UART_DATA_WIDTH_GET
UART data width get.
-
HOSAL_UART_STOP_BITS_SET
UART stop bits set.
-
HOSAL_UART_STOP_BITS_GET
UART stop bits get.
-
HOSAL_UART_FLOWMODE_SET
UART flow mode set.
-
HOSAL_UART_FLOWSTAT_GET
UART flow state get.
-
HOSAL_UART_PARITY_SET
UART flow mode set.
-
HOSAL_UART_PARITY_GET
UART flow state get.
-
HOSAL_UART_MODE_SET
UART mode set.
-
HOSAL_UART_MODE_GET
UART mode get.
-
HOSAL_UART_FREE_TXFIFO_GET
UART free tx fifo get.
-
HOSAL_UART_FREE_RXFIFO_GET
UART free rx fifo get.
-
HOSAL_UART_FLUSH
Wait for the send to complete.
-
HOSAL_UART_TX_TRIGGER_ON
UART TX trigger on.
-
HOSAL_UART_TX_TRIGGER_OFF
UART TX trigger off.
-
HOSAL_UART_DMA_TX_START
UART DMA TX start trans.
-
HOSAL_UART_DMA_RX_START
UART DMA RX start trans.
-
HOSAL_UART_CFG_DECL
(cfg, id, tx, rx, baud) define a UART default config
- Parameters
cfg
: config defineid
: uart idtx
: uart tx pinrx
: uart rx pinbaud
: uart baud
-
HOSAL_UART_DEV_DECL
(dev, id, tx, rx, baud) define a UART device
- Parameters
dev
: uart deviceid
: uart idtx
: uart tx pinrx
: uart rx pinbaud
: uart baud
Type Definitions
-
typedef int (*
hosal_uart_callback_t
)(void *p_arg) hosal uart callback
- Return
- 0 : on success
- EIO : if an error occurred with any step
- Parameters
p_arg
: Set the custom parameters specified when the callback function is set
Enumerations
-
enum
hosal_uart_data_width_t
UART data width.
Values:
-
HOSAL_DATA_WIDTH_5BIT
-
HOSAL_DATA_WIDTH_6BIT
-
HOSAL_DATA_WIDTH_7BIT
-
HOSAL_DATA_WIDTH_8BIT
-
HOSAL_DATA_WIDTH_9BIT
-
-
enum
hosal_uart_flow_control_t
UART flow control.
Values:
-
HOSAL_FLOW_CONTROL_DISABLED
-
HOSAL_FLOW_CONTROL_RTS
-
HOSAL_FLOW_CONTROL_CTS
-
HOSAL_FLOW_CONTROL_CTS_RTS
-