The latest development version of this page may be more current than this released 0.4.3 version.

Uart API Reference

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

TX transfer end

enumerator WM_DRV_UART_RX_READY

RX data ready

enumerator WM_DRV_UART_RX_ERROR

RX error

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

struct wm_drv_uart_evt_t

Event information type.

struct wm_drv_uart_config_t

Uart configuration.

typedef void (*wm_drv_uart_callback_t)(wm_device_t *dev, wm_drv_uart_evt_t *evt, void *user_data)

Uart callback type.

Note

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

Warning

Repeated initialization will return NULL if uart has been initialized before.

Parameters:
  • 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

  • tx_buf_size[in] tx buffer size, 0 or WM_DRV_UART_TX_MIN_BUF_SIZE <= tx_buf_size < 64K

Returns:

  • !NULL : Initialize success or initialized before

  • NULL : Param error or initialize failed

int wm_drv_uart_deinit(wm_device_t *dev)

deinitialize uart driver

Parameters:

dev[in] uart device,type of wm_device_t *

Returns:

  • 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

Parameters:
  • dev[in] uart device,type of wm_device_t *

  • cmd[in] uart ioctrl command.

  • param[in] command param.

  • arg[inout] command argument.

Returns:

  • 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

Parameters:
  • 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.

Returns:

  • 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

Note

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

Warning

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.

Warning

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.

Parameters:
  • dev[in] uart device,type of wm_device_t *

  • data[in] data to be send

  • size[in] the data size.

Returns:

  • < 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

Note

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.

Parameters:
  • 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.

Returns:

  • < 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.

Warning

The receive buffer pointer and size must release by wm_drv_uart_release_data.

Parameters:
  • dev[in] uart device,type of wm_device_t *

  • buf[out] data pointer from buffer

  • size[out] the continuous data size

Returns:

  • 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.

Parameters:
  • dev[in] uart device,type of wm_device_t *

  • buf[in] data pointer from buffer

  • size[in] the continuous data size

Returns:

  • 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.

Parameters:
Returns:

  • 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.

Parameters:
Returns:

  • 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.

Parameters:
  • dev[in] uart device,type of wm_device_t *

  • baudrate[in] uart baudrate, type of wm_uart_baudrate_t

Returns:

  • 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.

Parameters:
  • dev[in] uart device,type of wm_device_t *

  • baudrate[out] uart baudrate, type of wm_uart_baudrate_t

Returns:

  • 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.

Parameters:
  • dev[in] uart device,type of wm_device_t *

  • data_bits[in] uart data_bits, type of wm_uart_data_bits_t

Returns:

  • 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.

Parameters:
  • dev[in] uart device,type of wm_device_t *

  • data_bits[out] uart data_bits, type of wm_uart_data_bits_t

Returns:

  • 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.

Parameters:
  • dev[in] uart device,type of wm_device_t *

  • stop_bits[in] uart stop_bits, type of wm_uart_stop_bits_t

Returns:

  • 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.

Parameters:
  • dev[in] uart device,type of wm_device_t *

  • stop_bits[out] uart stop_bits, type of wm_uart_stop_bits_t

Returns:

  • 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.

Parameters:
  • dev[in] uart device,type of wm_device_t *

  • parity[in] uart parity, type of wm_uart_parity_t

Returns:

  • 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.

Parameters:
  • dev[in] uart device,type of wm_device_t *

  • parity[out] uart parity, type of wm_uart_parity_t

Returns:

  • 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.

Parameters:
  • dev[in] uart device,type of wm_device_t *

  • flow_ctrl[in] uart flow_ctrl, type of wm_uart_flowctrl_t

Returns:

  • 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.

Parameters:
  • dev[in] uart device,type of wm_device_t *

  • flow_ctrl[out] uart flow_ctrl, type of wm_uart_flowctrl_t

Returns:

  • WM_ERR_SUCCESS: succeed

  • WM_ERR_INVALID_PARAM: invalid param