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
The UART port to configure. Use ANX_UART0 for the primary serial port or ANX_UART1 for the secondary port.
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.
Number of data bits per frame. Typically 8; 7 is supported for legacy protocols.
Number of stop bits. Use 1 for standard operation; 2 for compatibility with older hardware.
Parity mode. Use ANX_PARITY_NONE for no parity, ANX_PARITY_EVEN for even parity, or ANX_PARITY_ODD for odd parity.
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.The initialized UART port to write to (ANX_UART0 or ANX_UART1).
Pointer to the byte array to transmit. Must not be NULL.
Number of bytes to transmit from data.
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.The initialized UART port to read from (ANX_UART0 or ANX_UART1).
Pointer to the buffer that receives the incoming data. Must be at least len bytes long.
Maximum number of bytes to read into buf.
Maximum number of milliseconds to wait for data. Pass 0 to return immediately with whatever bytes are currently available in the receive buffer.
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
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.
I2C — anx/i2c.h
I2C is a two-wire, multi-device bus protocol. Include the header to access the I2C API:Initialization
Configure an I2C bus by populating an anx_i2c_config_t struct and passing it to anx_i2c_init(). The ANX dev module exposes two independent I2C buses.anx_i2c_config_t members
The I2C bus to configure. Use ANX_I2C0 for the primary bus or ANX_I2C1 for the secondary bus.
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).
ANX_OK on success. ANX_ERR_INVALID_ARG if speed is not a valid value. ANX_ERR_FAIL if the bus is already initialized.
anx_i2c_write()
Sends len bytes from data to the device at 7-bit address addr on the specified bus.The initialized I2C bus (ANX_I2C0 or ANX_I2C1).
The 7-bit I2C address of the target device (0x00–0x7F).
Pointer to the byte array to send. Must not be NULL.
ANX_OK on success. ANX_ERR_NOT_FOUND if no device acknowledges the address. ANX_ERR_TIMEOUT if the bus does not become idle within the timeout window.
anx_i2c_read()
Reads len bytes from the device at 7-bit address addr on the specified bus into buf.The initialized I2C bus (ANX_I2C0 or ANX_I2C1).
The 7-bit I2C address of the target device.
Pointer to the buffer that receives the read data. Must be at least len bytes long.
Number of bytes to read from the device.
ANX_OK on success. ANX_ERR_NOT_FOUND if the device does not acknowledge. ANX_ERR_TIMEOUT if the read does not complete in time.
anx_i2c_scan()
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.The initialized I2C bus to scan (ANX_I2C0 or ANX_I2C1).
ANX_OK after the scan completes, regardless of how many devices responded.
Example: register read pattern
Most I2C sensors use a register-based interface: write the register address you want to read, then read back the value in a separate transaction.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.
SPI — anx/spi.h
SPI is a synchronous, full-duplex serial protocol well suited for high-speed peripherals. Include the header to access the SPI API:Initialization
Configure an SPI bus by populating an anx_spi_config_t struct and passing it to anx_spi_init().anx_spi_config_t members
The SPI bus to configure. Use ANX_SPI0 for the primary SPI bus.
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.
SPI clock polarity and phase. Choose from:
ANX_SPI_MODE0 — CPOL=0, CPHA=0 (clock idles low, data sampled on rising edge)
ANX_SPI_MODE1 — CPOL=0, CPHA=1 (clock idles low, data sampled on falling edge)
ANX_SPI_MODE2 — CPOL=1, CPHA=0 (clock idles high, data sampled on falling edge)
ANX_SPI_MODE3 — CPOL=1, CPHA=1 (clock idles high, data sampled on rising edge)
ANX_OK on success. ANX_ERR_INVALID_ARG if speed exceeds 40 MHz or mode is not valid. ANX_ERR_FAIL if the bus is already initialized.
anx_spi_transfer()
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).The initialized SPI bus (ANX_SPI0).
Pointer to the transmit buffer. Pass NULL to send zeroed bytes (receive-only operation).
Pointer to the receive buffer. Must be at least len bytes long if non-NULL. Pass NULL to discard incoming data (transmit-only operation).
Number of bytes to transfer. Both tx and rx (when non-NULL) must be at least this long.
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.
Chip-Select Control
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.anx_spi_cs_active()
Asserts the chip-select line (drives it low) to begin a transaction.The initialized SPI bus whose CS line you want to assert.
anx_spi_cs_idle()
Deasserts the chip-select line (drives it high) to end a transaction.The initialized SPI bus whose CS line you want to release.
Example: SPI flash read
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.