Uart API 参考
Uart
-
WM_DRV_UART_RX_MIN_BUF_SIZE
RX minimum buffer size.
-
WM_DRV_UART_TX_MIN_BUF_SIZE
TX minimum buffer size.
-
WM_DRV_UART_BUF_BLOCK_MAX_SIZE
MAX buffer size.
-
enum wm_drv_uart_evt_type_t
Event type, used in callback.
Values:
-
enumerator WM_DRV_UART_TX_DONE
If tx_buf_size is specified, the Uart driver has sent some buffer data to the hardware fifo. The user can now write another packet to the uart buffer. If tx_buf_size is not specified, the Uart driver has sent the written data to the hardware fifo.
-
enumerator WM_DRV_UART_RX_READY
RX data ready ,some data is cached in uart buffer, The user can read it now
-
enumerator WM_DRV_UART_RX_ERROR
RX error
-
enumerator WM_DRV_UART_TX_DONE
-
enum wm_drv_uart_evt_error_t
Event error type, used in callback.
Values:
-
enumerator WM_DRV_UART_ERROR_OVERRUN
fifo overrun
-
enumerator WM_DRV_UART_ERROR_PARITY
parity error
-
enumerator WM_DRV_UART_ERROR_FRAMING
frame error
-
enumerator WM_DRV_UART_BREAK
break event
-
enumerator WM_DRV_UART_ERROR_COLLISION
485 half duplex error
-
enumerator WM_DRV_UART_ERROR_OVERRUN
-
struct wm_drv_uart_evt_t
Event information type.
Public Members
-
wm_drv_uart_evt_type_t type
Event type
-
uint32_t len
rx data len or tx free buffer size
-
wm_drv_uart_evt_error_t error
rx error
-
wm_drv_uart_evt_type_t type
-
struct wm_drv_uart_config_t
Uart configuration.
Public Members
-
wm_uart_baudrate_t baudrate
baudrate
-
wm_uart_data_bits_t data_bits
data bits
-
wm_uart_stop_bits_t stop_bits
stop bits
-
wm_uart_parity_t parity
parity type
-
wm_uart_flowctrl_t flow_ctrl
flow ctrl
-
wm_uart_baudrate_t baudrate
-
typedef void (*wm_drv_uart_callback_t)(wm_device_t *dev, wm_drv_uart_evt_t *evt, void *user_data)
Uart callback type.
备注
Do not call printf,wm_log_xxx and other uart functions in the interrupt function
-
wm_device_t *wm_drv_uart_init(const char *dev_name, uint32_t rx_buf_size, uint32_t tx_buf_size)
initialize uart driver
备注
Repeated initialization will return NULL.
- 参数:
dev_name – [in] uart name: [uart0,uart1,uart2,uart3,uart4,uart5], it must same to the name in device table
rx_buf_size – [in] rx buffer size, WM_DRV_UART_RX_MIN_BUF_SIZE <= rx_buf_size < 64K The more baudrate set , the more rx buffer size needed. At least 4K when the baudrate is 2M
tx_buf_size – [in] tx buffer size, 0 or WM_DRV_UART_TX_MIN_BUF_SIZE <= tx_buf_size < 64K When the wm_drv_uart_write interface has enough tx buffer space, it will return immediately after copying the data to the tx buffer. If you need the write interface to return quickly, you need to set the TX buffer.
- 返回:
!NULL : Initialize success or initialized before
NULL : Param error or initialize failed
-
int wm_drv_uart_deinit(wm_device_t *dev)
deinitialize uart driver
- 参数:
dev – [in] uart device,type of wm_device_t *
- 返回:
WM_ERR_SUCCESS: succeed
WM_ERR_INVALID_PARAM: invalid param
-
int wm_drv_uart_ioctrl(wm_device_t *dev, int cmd, uint32_t param, void *arg)
control uart driver by command
- 参数:
dev – [in] uart device,type of wm_device_t *
cmd – [in] uart ioctrl command.
param – [in] command param.
arg – [inout] command argument.
- 返回:
WM_ERR_SUCCESS: succeed
WM_ERR_INVALID_PARAM: invalid param
-
int wm_drv_uart_register_callback(wm_device_t *dev, wm_drv_uart_callback_t cb, void *user_data)
register callback to receive uart event
- 参数:
dev – [in] uart device,type of wm_device_t *
cb – [in] user callback fucntion, type of wm_drv_uart_callback_t
user_data – [in] user data, it will transfer to user in callback.
- 返回:
WM_ERR_SUCCESS: succeed
WM_ERR_INVALID_PARAM: invalid param
-
int wm_drv_uart_write(wm_device_t *dev, const uint8_t *data, uint32_t size)
write data to uart port
备注
If tx_buf_size is specified during initialization, the data will be copied to the buffer until all copies can be completed before returning. If tx_buf_size is not specified, data is transferred directly, and then returned after the transfer is completed
警告
If tx_buf_size is not specified,the data address is on flash and the uart in the device table is configured with DMA mode, it will not output correctly.
警告
Call printing in the interrupt. It will use polling mode to put character to hardware TX fifo, the interrup will be blocked until print end. So it is not recommended to print in interrupt processing function because too much printing in the interrupt affects the ability of other tasks to be scheduled in time. Each callback of driver module is called from the interrupt, and it is not recommended to print in the driver callback.
- 参数:
dev – [in] uart device,type of wm_device_t *
data – [in] data to be send
size – [in] the data size.
- 返回:
< 0: WM_ERR_INVALID_PARAM, param error.
> 0: Real wirte bytes.
-
int wm_drv_uart_read(wm_device_t *dev, uint8_t *buf, uint32_t size, uint32_t timeout_ms)
read data from uart port
备注
the data will be copied from the receiving buffer. There are three conditions that will trigger the return: a: The read length reaches the length of the buffer size. b: The read time reaches the end of the timeout time. c: The other end does not send continuously then the hardware trigger a rx fifo timeout.
- 参数:
dev – [in] uart device,type of wm_device_t *
buf – [out] buffer for receiving data
size – [in] the buffer size.
timeout_ms – [in] receiving timeout.
- 返回:
< 0: WM_ERR_INVALID_PARAM, param error.
>= 0: Real read bytes.
-
int wm_drv_uart_receive_data(wm_device_t *dev, uint8_t **buf, uint32_t *size)
Get RX data pointer and size from buffer.
警告
The receive buffer pointer and size must release by wm_drv_uart_release_data.
警告
The internal RX buffer will be divided into four sub buffers. Sometimes the received data packet will be divided into two blocks and put into two sub buffers. In this case, the interface needs to call twice to get the complete data.
- 参数:
dev – [in] uart device,type of wm_device_t *
buf – [out] data pointer from buffer
size – [out] the continuous data size
- 返回:
WM_ERR_SUCCESS: succeed
WM_ERR_INVALID_PARAM: invalid param
WM_ERR_FAILED: no data in the driver buffer
-
int wm_drv_uart_release_data(wm_device_t *dev, uint8_t *buf, uint32_t size)
Release RX data pointer and size from buffer.
- 参数:
dev – [in] uart device,type of wm_device_t *
buf – [in] data pointer from buffer
size – [in] the continuous data size
- 返回:
WM_ERR_SUCCESS: succeed
WM_ERR_INVALID_PARAM: invalid param
-
int wm_drv_uart_set_config(wm_device_t *dev, wm_drv_uart_config_t *config)
Set uart config, include baudrate,data bits,stop bits, parity, flow ctrl.
- 参数:
dev – [in] uart device,type of wm_device_t *
config – [in] uart config, type of wm_drv_uart_config_t *
- 返回:
WM_ERR_SUCCESS: succeed
WM_ERR_INVALID_PARAM: invalid param
-
int wm_drv_uart_get_config(wm_device_t *dev, wm_drv_uart_config_t *config)
Get uart config, include baudrate,data bits,stop bits, parity, flow ctrl.
- 参数:
dev – [in] uart device,type of wm_device_t *
config – [out] uart config, type of wm_drv_uart_config_t *
- 返回:
WM_ERR_SUCCESS: succeed
WM_ERR_INVALID_PARAM: invalid param
-
int wm_drv_uart_set_baudrate(wm_device_t *dev, uint32_t baudrate)
Set uart baudrate.
- 参数:
dev – [in] uart device,type of wm_device_t *
baudrate – [in] uart baudrate, only support the value enumeration in wm_uart_baudrate_t
- 返回:
WM_ERR_SUCCESS: succeed
WM_ERR_INVALID_PARAM: invalid param
-
int wm_drv_uart_get_baudrate(wm_device_t *dev, uint32_t *baudrate)
Get uart baudrate.
- 参数:
dev – [in] uart device,type of wm_device_t *
baudrate – [out] uart baudrate, type of wm_uart_baudrate_t
- 返回:
WM_ERR_SUCCESS: succeed
WM_ERR_INVALID_PARAM: invalid param
-
int wm_drv_uart_set_data_bits(wm_device_t *dev, wm_uart_data_bits_t data_bits)
Set uart data bits.
- 参数:
dev – [in] uart device,type of wm_device_t *
data_bits – [in] uart data_bits, type of wm_uart_data_bits_t
- 返回:
WM_ERR_SUCCESS: succeed
WM_ERR_INVALID_PARAM: invalid param
-
int wm_drv_uart_get_data_bits(wm_device_t *dev, wm_uart_data_bits_t *data_bits)
Get uart data bits.
- 参数:
dev – [in] uart device,type of wm_device_t *
data_bits – [out] uart data_bits, type of wm_uart_data_bits_t
- 返回:
WM_ERR_SUCCESS: succeed
WM_ERR_INVALID_PARAM: invalid param
-
int wm_drv_uart_set_stop_bits(wm_device_t *dev, wm_uart_stop_bits_t stop_bits)
Set uart stop bits.
- 参数:
dev – [in] uart device,type of wm_device_t *
stop_bits – [in] uart stop_bits, type of wm_uart_stop_bits_t
- 返回:
WM_ERR_SUCCESS: succeed
WM_ERR_INVALID_PARAM: invalid param
-
int wm_drv_uart_get_stop_bits(wm_device_t *dev, wm_uart_stop_bits_t *stop_bits)
Get uart stop bits.
- 参数:
dev – [in] uart device,type of wm_device_t *
stop_bits – [out] uart stop_bits, type of wm_uart_stop_bits_t
- 返回:
WM_ERR_SUCCESS: succeed
WM_ERR_INVALID_PARAM: invalid param
-
int wm_drv_uart_set_parity(wm_device_t *dev, wm_uart_parity_t parity)
Set uart parity.
- 参数:
dev – [in] uart device,type of wm_device_t *
parity – [in] uart parity, type of wm_uart_parity_t
- 返回:
WM_ERR_SUCCESS: succeed
WM_ERR_INVALID_PARAM: invalid param
-
int wm_drv_uart_get_parity(wm_device_t *dev, wm_uart_parity_t *parity)
Get uart parity.
- 参数:
dev – [in] uart device,type of wm_device_t *
parity – [out] uart parity, type of wm_uart_parity_t
- 返回:
WM_ERR_SUCCESS: succeed
WM_ERR_INVALID_PARAM: invalid param
-
int wm_drv_uart_set_flow_ctrl(wm_device_t *dev, wm_uart_flowctrl_t flow_ctrl)
Set uart flow_ctrl.
- 参数:
dev – [in] uart device,type of wm_device_t *
flow_ctrl – [in] uart flow_ctrl, type of wm_uart_flowctrl_t
- 返回:
WM_ERR_SUCCESS: succeed
WM_ERR_INVALID_PARAM: invalid param
-
int wm_drv_uart_get_flow_ctrl(wm_device_t *dev, wm_uart_flowctrl_t *flow_ctrl)
Get uart flow_ctrl.
- 参数:
dev – [in] uart device,type of wm_device_t *
flow_ctrl – [out] uart flow_ctrl, type of wm_uart_flowctrl_t
- 返回:
WM_ERR_SUCCESS: succeed
WM_ERR_INVALID_PARAM: invalid param