ProtoCore v1.0.16
Deterministic, zero-heap network stack for embedded targets
Loading...
Searching...
No Matches
i2c.h File Reference

The one owner of the shared I2C bus for the peripheral drivers. More...

Go to the source code of this file.

Macros

#define PROTOCORE_I2C_HZ   100000u
 Bus clock for the shared peripheral bus; 100 kHz standard mode.
#define PROTOCORE_I2C_TIMEOUT_MS   50u
 Per-transfer timeout in milliseconds.
#define PROTOCORE_I2C_BUS   0u
 Controller the plain verbs drive.
#define PROTOCORE_I2C_HZ_STANDARD   100000u
 Standard-mode, fast-mode and fast-mode-plus bus clocks.
#define PROTOCORE_I2C_HZ_FAST   400000u
#define PROTOCORE_I2C_HZ_FAST_PLUS   1000000u
#define PROTOCORE_I2C_ADDR10(a)
 Wrap a 10-bit address so a transfer verb puts the two-byte form on the wire.
#define PROTOCORE_I2C_SCAN_FIRST   0x08u
 Lowest and highest 7-bit addresses a scan reports; 0x00 - 0x07 and 0x78 - 0x7F are reserved.
#define PROTOCORE_I2C_SCAN_LAST   0x77u

Functions

EMBED_BEGIN_DECLS EMBED_INLINE embed_bool protocore_i2c_begin_on (uint8_t bus, int sda, int scl, uint32_t hz)
EMBED_INLINE embed_bool protocore_i2c_begin (void)
EMBED_INLINE embed_bool protocore_i2c_write_on (uint8_t bus, uint16_t addr, const uint8_t *buf, size_t len)
EMBED_INLINE embed_bool protocore_i2c_write (uint16_t addr, const uint8_t *buf, size_t len)
EMBED_INLINE embed_bool protocore_i2c_read_on (uint8_t bus, uint16_t addr, uint8_t *buf, size_t len)
EMBED_INLINE embed_bool protocore_i2c_read (uint16_t addr, uint8_t *buf, size_t len)
EMBED_INLINE embed_bool protocore_i2c_write_read_on (uint8_t bus, uint16_t addr, const uint8_t *w, size_t wlen, uint8_t *r, size_t rlen)
EMBED_INLINE embed_bool protocore_i2c_write_read (uint16_t addr, const uint8_t *w, size_t wlen, uint8_t *r, size_t rlen)
EMBED_INLINE embed_bool protocore_i2c_set_clock_on (uint8_t bus, uint32_t hz)
EMBED_INLINE embed_bool protocore_i2c_set_clock (uint32_t hz)
EMBED_INLINE embed_bool protocore_i2c_probe_on (uint8_t bus, uint16_t addr)
EMBED_INLINE embed_bool protocore_i2c_probe (uint16_t addr)
EMBED_INLINE size_t protocore_i2c_scan_on (uint8_t bus, uint8_t *out, size_t cap)
EMBED_INLINE size_t protocore_i2c_scan (uint8_t *out, size_t cap)
EMBED_INLINE embed_bool protocore_i2c_general_call_on (uint8_t bus, const uint8_t *buf, size_t len)
EMBED_INLINE embed_bool protocore_i2c_general_call (const uint8_t *buf, size_t len)
EMBED_INLINE embed_bool protocore_i2c_recover_on (uint8_t bus, int sda, int scl)
EMBED_INLINE embed_bool protocore_i2c_recover (void)

Detailed Description

The one owner of the shared I2C bus for the peripheral drivers.

The sensor / peripheral drivers (RTC, SHT3x, MPR121, ADS1115, INA219, PCA9685, VL53L0X, LDC1614, FDC2214) share one bus and bring it up through these verbs. The pins come from PROTOCORE_I2C_SDA_PIN / PROTOCORE_I2C_SCL_PIN (default -1 = the platform default GPIO 21 / 22). Re-begin is idempotent, so per-driver calls are harmless.

This covers the master half of the protocol. The three transfer shapes a driver reaches for are a write, a read, and a register read, which is a write and a read joined by a repeated start. The rest of what the bus can express is here too: a bus scan, an address probe, the general call, 10-bit addressing, a per-transfer clock, and recovery of a bus a device is holding low.

Every verb has a plain form on the default bus (PROTOCORE_I2C_BUS) and an _on form naming a controller, so a board with two I2C controllers drives both through one owner.

An address is 7-bit unless it is wrapped in PROTOCORE_I2C_ADDR10, which selects the 10-bit form.

The bodies compile wherever the platform states a bus.

Author
Douglas Quigg (dstroy0)
Date
2026

Definition in file i2c.h.

Macro Definition Documentation

◆ PROTOCORE_I2C_HZ

#define PROTOCORE_I2C_HZ   100000u

Bus clock for the shared peripheral bus; 100 kHz standard mode.

Definition at line 38 of file i2c.h.

◆ PROTOCORE_I2C_TIMEOUT_MS

#define PROTOCORE_I2C_TIMEOUT_MS   50u

Per-transfer timeout in milliseconds.

Definition at line 43 of file i2c.h.

◆ PROTOCORE_I2C_BUS

#define PROTOCORE_I2C_BUS   0u

◆ PROTOCORE_I2C_HZ_STANDARD

#define PROTOCORE_I2C_HZ_STANDARD   100000u

Standard-mode, fast-mode and fast-mode-plus bus clocks.

Definition at line 52 of file i2c.h.

◆ PROTOCORE_I2C_HZ_FAST

#define PROTOCORE_I2C_HZ_FAST   400000u

Definition at line 53 of file i2c.h.

◆ PROTOCORE_I2C_HZ_FAST_PLUS

#define PROTOCORE_I2C_HZ_FAST_PLUS   1000000u

Definition at line 54 of file i2c.h.

◆ PROTOCORE_I2C_ADDR10

#define PROTOCORE_I2C_ADDR10 ( a)
Value:
((uint16_t)(PROTOCORE_I2C_ADDR_10BIT | ((a) & PROTOCORE_I2C_ADDR_MASK)))

Wrap a 10-bit address so a transfer verb puts the two-byte form on the wire.

Definition at line 57 of file i2c.h.

◆ PROTOCORE_I2C_SCAN_FIRST

#define PROTOCORE_I2C_SCAN_FIRST   0x08u

Lowest and highest 7-bit addresses a scan reports; 0x00 - 0x07 and 0x78 - 0x7F are reserved.

Definition at line 60 of file i2c.h.

Referenced by protocore_i2c_scan_on().

◆ PROTOCORE_I2C_SCAN_LAST

#define PROTOCORE_I2C_SCAN_LAST   0x77u

Definition at line 61 of file i2c.h.

Referenced by protocore_i2c_scan_on().

Function Documentation

◆ protocore_i2c_begin_on()

EMBED_BEGIN_DECLS EMBED_INLINE embed_bool protocore_i2c_begin_on ( uint8_t bus,
int sda,
int scl,
uint32_t hz )

Definition at line 193 of file i2c.h.

◆ protocore_i2c_begin()

EMBED_INLINE embed_bool protocore_i2c_begin ( void )

Definition at line 202 of file i2c.h.

◆ protocore_i2c_write_on()

EMBED_INLINE embed_bool protocore_i2c_write_on ( uint8_t bus,
uint16_t addr,
const uint8_t * buf,
size_t len )

Definition at line 207 of file i2c.h.

Referenced by protocore_i2c_general_call_on(), and protocore_i2c_write().

◆ protocore_i2c_write()

EMBED_INLINE embed_bool protocore_i2c_write ( uint16_t addr,
const uint8_t * buf,
size_t len )

Definition at line 216 of file i2c.h.

References PROTOCORE_I2C_BUS, and protocore_i2c_write_on().

◆ protocore_i2c_read_on()

EMBED_INLINE embed_bool protocore_i2c_read_on ( uint8_t bus,
uint16_t addr,
uint8_t * buf,
size_t len )

Definition at line 221 of file i2c.h.

Referenced by protocore_i2c_read().

◆ protocore_i2c_read()

EMBED_INLINE embed_bool protocore_i2c_read ( uint16_t addr,
uint8_t * buf,
size_t len )

Definition at line 230 of file i2c.h.

References PROTOCORE_I2C_BUS, and protocore_i2c_read_on().

◆ protocore_i2c_write_read_on()

EMBED_INLINE embed_bool protocore_i2c_write_read_on ( uint8_t bus,
uint16_t addr,
const uint8_t * w,
size_t wlen,
uint8_t * r,
size_t rlen )

Definition at line 235 of file i2c.h.

Referenced by protocore_i2c_write_read().

◆ protocore_i2c_write_read()

EMBED_INLINE embed_bool protocore_i2c_write_read ( uint16_t addr,
const uint8_t * w,
size_t wlen,
uint8_t * r,
size_t rlen )

Definition at line 247 of file i2c.h.

References PROTOCORE_I2C_BUS, and protocore_i2c_write_read_on().

◆ protocore_i2c_set_clock_on()

EMBED_INLINE embed_bool protocore_i2c_set_clock_on ( uint8_t bus,
uint32_t hz )

Definition at line 252 of file i2c.h.

◆ protocore_i2c_set_clock()

EMBED_INLINE embed_bool protocore_i2c_set_clock ( uint32_t hz)

Definition at line 259 of file i2c.h.

◆ protocore_i2c_probe_on()

EMBED_INLINE embed_bool protocore_i2c_probe_on ( uint8_t bus,
uint16_t addr )

Definition at line 265 of file i2c.h.

Referenced by protocore_i2c_probe(), and protocore_i2c_scan_on().

◆ protocore_i2c_probe()

EMBED_INLINE embed_bool protocore_i2c_probe ( uint16_t addr)

Definition at line 272 of file i2c.h.

References PROTOCORE_I2C_BUS, and protocore_i2c_probe_on().

◆ protocore_i2c_scan_on()

EMBED_INLINE size_t protocore_i2c_scan_on ( uint8_t bus,
uint8_t * out,
size_t cap )

Definition at line 277 of file i2c.h.

References protocore_i2c_probe_on(), PROTOCORE_I2C_SCAN_FIRST, and PROTOCORE_I2C_SCAN_LAST.

Referenced by protocore_i2c_scan().

◆ protocore_i2c_scan()

EMBED_INLINE size_t protocore_i2c_scan ( uint8_t * out,
size_t cap )

Definition at line 291 of file i2c.h.

References PROTOCORE_I2C_BUS, and protocore_i2c_scan_on().

◆ protocore_i2c_general_call_on()

EMBED_INLINE embed_bool protocore_i2c_general_call_on ( uint8_t bus,
const uint8_t * buf,
size_t len )

Definition at line 296 of file i2c.h.

References protocore_i2c_write_on().

Referenced by protocore_i2c_general_call().

◆ protocore_i2c_general_call()

EMBED_INLINE embed_bool protocore_i2c_general_call ( const uint8_t * buf,
size_t len )

Definition at line 301 of file i2c.h.

References PROTOCORE_I2C_BUS, and protocore_i2c_general_call_on().

◆ protocore_i2c_recover_on()

EMBED_INLINE embed_bool protocore_i2c_recover_on ( uint8_t bus,
int sda,
int scl )

Definition at line 306 of file i2c.h.

◆ protocore_i2c_recover()

EMBED_INLINE embed_bool protocore_i2c_recover ( void )

Definition at line 314 of file i2c.h.