Send and receive data over UART, I2C, and SPI using the ANX SDK communication APIs. Includes configuration structs, read/write functions, and error handling.
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.
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.
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_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.
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.
Bus clock speed. Use ANX_I2C_STANDARD for 100 kHz (compatible with all I2C devices) or ANX_I2C_FAST for 400 kHz (requires all devices on the bus to support fast mode).
Scans the bus and logs the 7-bit address of every device that sends an acknowledgement. Use this during development to verify that all sensors are wired correctly and that their addresses match your expectations.
Some I2C devices require a repeated-start condition between the write and read phases. If anx_i2c_read() returns ANX_ERR_NOT_FOUND immediately after a successful write, check the device datasheet to see whether it supports repeated-start or requires a full stop/start sequence.
Clock frequency in Hz. The maximum supported speed is 40000000 (40 MHz). Start at a lower speed (4–8 MHz) when debugging and raise it once the connection is verified stable.
Performs a full-duplex SPI transfer, simultaneously clocking out len bytes from tx and clocking in len bytes into rx. Pass NULL for tx to receive only (MOSI held low), or NULL for rx to transmit only (MISO ignored).
ANX_OK on success. ANX_ERR_INVALID_ARG if both tx and rx are NULL, or if len is zero. ANX_ERR_TIMEOUT if the transfer does not complete within the hardware timeout.
The ANX SPI API gives you manual control over the chip-select (CS) line so you can compose multi-transfer transactions without deselecting the device between calls.
Always call anx_spi_cs_idle() after every transaction — even if anx_spi_transfer() returns an error. Leaving the CS line asserted prevents other devices that share the same SPI bus from communicating and may lock up the bus until the next reboot.
Choosing the correct SPI mode
Consult your peripheral’s datasheet and look for the CPOL and CPHA timing diagrams in the electrical characteristics section. Most SPI flash and display controllers use ANX_SPI_MODE0. If you see corrupted data at higher clock speeds, try lowering the frequency first before switching the mode — a marginal PCB trace is more commonly the culprit than a mode mismatch.