Skip to main content
The ANX SDK provides separate APIs for each serial communication protocol: UART for point-to-point serial communication with a host or another microcontroller, I2C for connecting multiple low-speed sensors and peripherals on a shared two-wire bus, and SPI for high-speed full-duplex data exchange with flash memory, displays, and other peripherals. Each protocol has its own header, configuration struct, and set of read/write functions — all returning anx_err_t for consistent error handling.

UART — anx/uart.h

UART is a point-to-point asynchronous serial protocol. Include the header to access the UART API:

Initialization

Configure a UART port by populating an anx_uart_config_t struct and passing it to anx_uart_init(). Call anx_uart_init() once per port during application startup before any read or write operations.

anx_uart_config_t members

anx_uart_port_t
required
The UART port to configure. Use ANX_UART0 for the primary serial port or ANX_UART1 for the secondary port.
uint32_t
required
Baud rate in bits per second. Common values: 9600, 19200, 57600, 115200, 230400, 921600. Both ends of the connection must use the same baud rate.
uint8_t
required
Number of data bits per frame. Typically 8; 7 is supported for legacy protocols.
uint8_t
required
Number of stop bits. Use 1 for standard operation; 2 for compatibility with older hardware.
anx_parity_t
required
Parity mode. Use ANX_PARITY_NONE for no parity, ANX_PARITY_EVEN for even parity, or ANX_PARITY_ODD for odd parity.
anx_err_t
ANX_OK on success. ANX_ERR_INVALID_ARG if any config member is out of range. ANX_ERR_FAIL if the hardware port is already in use.

anx_uart_write()

Writes len bytes from data to the specified UART port. The function blocks until all bytes have been placed in the hardware transmit buffer or the operation times out.
anx_uart_port_t
required
The initialized UART port to write to (ANX_UART0 or ANX_UART1).
const uint8_t *
required
Pointer to the byte array to transmit. Must not be NULL.
size_t
required
Number of bytes to transmit from data.
anx_err_t
ANX_OK when all bytes are sent. ANX_ERR_TIMEOUT if the transmit buffer does not drain within the hardware timeout. ANX_ERR_INVALID_ARG if data is NULL or len is zero.

anx_uart_read()

Reads up to len bytes from the specified UART port into buf, waiting up to timeout_ms milliseconds for data to arrive.
anx_uart_port_t
required
The initialized UART port to read from (ANX_UART0 or ANX_UART1).
uint8_t *
required
Pointer to the buffer that receives the incoming data. Must be at least len bytes long.
size_t
required
Maximum number of bytes to read into buf.
uint32_t
required
Maximum number of milliseconds to wait for data. Pass 0 to return immediately with whatever bytes are currently available in the receive buffer.
int
The number of bytes actually read (0 to len). Returns -1 on error (for example, if the port is not initialized).

Example: UART loopback test

uart_loopback.c
Connect the TX and RX pins together on your dev module to run a loopback test without needing a second device. It is a fast way to verify your baud rate and framing settings before integrating a real peripheral.