Skip to main content

i2c-master

Discrete I²C master that reads a known device at a known address. The pattern: open the bus, init the chip driver, loop reading the sensor every second, close cleanly.

Contrasts with i2c-scanner — the scanner probes every 7-bit address for ACKs without knowing what's behind them; this example reads a known sensor through its real driver.

Source: examples/peripheral-io/i2c-master/.

What it does

  1. alp_i2c_open() on BOARD_I2C_SENSORS at 400 kHz (I²C Fast-mode). BOARD_I2C_SENSORS is a portable cross-EVK alias from <alp/board.h>: it resolves to ALP_E1M_I2C0 on the E1M EVK and ALP_E1M_X_I2C0 on the E1M-X EVK.
  2. tmp112_init() — writes the CONF register to put the chip in continuous-conversion mode, then reads CONF back. The readback is what catches the "wrong address" case (NACK on probe) up front.
  3. tmp112_set_rate(TMP112_RATE_4_HZ) — non-fatal if it fails; the chip stays at whatever rate init set.
  4. Five tmp112_read_temp_milli_c() calls, one per second. The print splits integer and fractional parts so there's no float printf on an M-class target — and takes the absolute value of the fraction so -1750 milli-°C prints as -1.750 degC, not -1.-750 degC.
  5. tmp112_deinit() + alp_i2c_close().

Read errors mid-loop are logged and the loop continues rather than aborting — a steady-state read failure is usually a transient bus glitch.

Hardware

The TMP112 ±0.5 °C temperature sensor is populated on the BRD_I2C management bus of every member of the AEN, V2N, and V2N-M1 families. Its 7-bit address depends on the ADD0 strap:

ADD0 strapAddressSoM defaults
GND0x48E1M-AEN + E1M-V2N families (this default)
V+0x49(none today)
SDA0x4A(none today)
SCL0x4B(none today)

All current SoM families strap ADD0 to GND, so TMP112_I2C_ADDR_GND works unchanged across them.

board.yaml

som:
sku: E1M-AEN801

preset: e1m-evk
supported_boards:
- e1m-evk
- e1m-x-evk

pins:
- { e1m: E1M_I2C0, macro: EVK_I2C_BUS_SENSORS, doc: "Shared sensor + IO-expander + INA236 bus" }

cores:
m55_hp:
app: ./src
peripherals:
- i2c

chips:
- tmp112

diagnostics:
log_level: info

Because the TMP112 is populated on every AEN / V2N / V2N-M1 SoM, the SDK's portability checker classifies this example as Ring 2 — chip-bound, but multi-family.

Expected output

Real hardware, TMP112 populated, room temperature:

[i2c-master] open BOARD_I2C_SENSORS @ 400 kHz
[i2c-master] tmp112_init @ 0x48 -> 0 (OK)
[i2c-master] sample 0: 23.625 degC
[i2c-master] sample 1: 23.687 degC
[i2c-master] sample 2: 23.625 degC
[i2c-master] sample 3: 23.687 degC
[i2c-master] sample 4: 23.750 degC
[i2c-master] done

native_sim maps alp-i2c0 to the emul I²C driver with no TMP112 registered as a target, so init gets NACKed:

[i2c-master] open BOARD_I2C_SENSORS @ 400 kHz
[i2c-master] tmp112_init @ 0x48 -> -5 (populated? right address?)
[i2c-master] done

Either way [i2c-master] done latches the harness.

Troubleshooting

SymptomCause / fix
tmp112_init -> -5 (ALP_ERR_IO / NACK)Chip not populated, wrong address, or the bus is held low (missing pull-ups, stuck target). Run i2c-scanner to see what actually ACKs.
open failed (NULL handle)The alp-i2c0 devicetree alias isn't set. On native_sim check CONFIG_EMUL=y / CONFIG_I2C_EMUL=y and the shipped overlay.
Garbled readingsWrong bitrate for the bus capacitance. Drop 400 kHz → 100 kHz to confirm.

See also

Questions about this page? Discuss in Community Forum