UART

The hardware independent UART interface for Mynewt.

Description

Contains the basic operations to send and receive data over a UART (Universal Asynchronous Receiver Transmitter). It also includes the API to apply settings such as speed, parity etc. to the UART. The UART port should be closed before any reconfiguring.

Examples

This example shows a user writing a character to the uart in blocking mode where the UART has to block until character has been sent.

/* write to the console with blocking */
{
    char *str = "Hello World!";
    char *ptr = str;

    while(*ptr) {
        hal_uart_blocking_tx(MY_UART, *ptr++);
    }
    hal_uart_blocking_tx(MY_UART, '\n');
}

API

enum HALUart::hal_uart_parity

Values:

HAL_UART_PARITY_NONE = 0

No Parity.

HAL_UART_PARITY_ODD = 1

Odd parity.

HAL_UART_PARITY_EVEN = 2

Even parity.

enum HALUart::hal_uart_flow_ctl

Values:

HAL_UART_FLOW_CTL_NONE = 0

No Flow Control.

HAL_UART_FLOW_CTL_RTS_CTS = 1

RTS/CTS.

typedef int(* hal_uart_tx_char)(void *arg)

Function prototype for UART driver to ask for more data to send.

Returns -1 if no more data is available for TX. Driver must call this with interrupts disabled.

typedef void(* hal_uart_tx_done)(void *arg)

Function prototype for UART driver to report that transmission is complete.

This should be called when transmission of last byte is finished. Driver must call this with interrupts disabled.

typedef int(* hal_uart_rx_char)(void *arg, uint8_t byte)

Function prototype for UART driver to report incoming byte of data.

Returns -1 if data was dropped. Driver must call this with interrupts disabled.

int hal_uart_init_cbs(int uart, hal_uart_tx_char tx_func, hal_uart_tx_done tx_done, hal_uart_rx_char rx_func, void * arg)

Initializes given uart.

Mapping of logical UART number to physical UART/GPIO pins is in BSP.

int hal_uart_init(int uart, void * cfg)

Initialize the HAL uart.

Return
0 on success, non-zero error code on failure
Parameters
  • uart: The uart number to configure
  • cfg: Hardware specific uart configuration. This is passed from BSP directly to the MCU specific driver.

int hal_uart_config(int uart, int32_t speed, uint8_t databits, uint8_t stopbits, enum hal_uart_parity parity, enum hal_uart_flow_ctl flow_ctl)

Applies given configuration to UART.

Return
0 on success, non-zero error code on failure
Parameters
  • uart: The UART number to configure
  • speed: The baudrate in bps to configure
  • databits: The number of databits to send per byte
  • stopbits: The number of stop bits to send
  • parity: The UART parity
  • flow_ctl: Flow control settings on the UART

int hal_uart_close(int uart)

Close UART port.

Can call hal_uart_config() with different settings after calling this.

Parameters
  • uart: The UART number to close

void hal_uart_start_tx(int uart)

More data queued for transmission.

UART driver will start asking for that data.

Parameters
  • uart: The UART number to start TX on

void hal_uart_start_rx(int uart)

Upper layers have consumed some data, and are now ready to receive more.

This is meaningful after uart_rx_char callback has returned -1 telling that no more data can be accepted.

Parameters
  • uart: The UART number to begin RX on

void hal_uart_blocking_tx(int uart, uint8_t byte)

This is type of write where UART has to block until character has been sent.

Used when printing diag output from system crash. Must be called with interrupts disabled.

Parameters
  • uart: The UART number to TX on
  • byte: The byte to TX on the UART