Kleidos API Search

In development. It changes without notice.

Topic

I2C Bus

The board's internal I2C bus, and reading and writing the registers of a chip on it.

kleidos::drivers::bus::I2cDevice is how a driver reads, writes or changes a register, so each one does it the same way. The panels, on SPI, are reached by Display.

See also
GPIO & Buses

Files

file src/drivers/bus/bus_gated_probe.h
The memo of a chip probe, counting only what the chip said while its bus was up.
file src/drivers/bus/i2c_device.h
Thin RAII helper for register access to an internal-bus I2C device.
file src/drivers/bus/internal_i2c_bus.h
Selector and register I/O for the internal (on-board) I2C bus.

Classes

class kleidos::drivers::bus::BusGatedProbe< Chip, AttemptsWithBusUp >
A chip probe's memo, gated on the bus being up. More about kleidos::drivers::bus::BusGatedProbe< Chip, AttemptsWithBusUp >
class kleidos::drivers::bus::I2cDevice
RAII view of a single device on the internal I2C bus. More about kleidos::drivers::bus::I2cDevice
struct kleidos::drivers::bus::internal_i2c::Pins
Internal I2C pin map of this board. More about kleidos::drivers::bus::internal_i2c::Pins

Functions

Pinskleidos::drivers::bus::internal_i2c::pins ()
Get this board's internal I2C pin map.
boolkleidos::drivers::bus::internal_i2c::configured ()
Whether this board declares an internal I2C bus (its SDA, SCL and port all set in its configuration).
boolkleidos::drivers::bus::internal_i2c::ready ()
Whether the internal bus exists right now (a beginPlatform that took, and no teardown since).
boolkleidos::drivers::bus::internal_i2c::beginPlatform (uint32_t frequencyHz=kDefaultFrequencyHz, uint32_t timeoutMs=kDefaultTimeoutMs)
Start (or restart) the bus with the platform facade.
boolkleidos::drivers::bus::internal_i2c::readRegister (uint8_t address, uint8_t reg, uint8_t *data, size_t length, uint32_t frequencyHz, uint32_t timeoutMs=kDefaultTimeoutMs)
Read bytes from a register of an I2C device.
boolkleidos::drivers::bus::internal_i2c::writeRegister (uint8_t address, uint8_t reg, const uint8_t *data, size_t length, uint32_t frequencyHz, uint32_t timeoutMs=kDefaultTimeoutMs)
Write bytes to a register of an I2C device.
boolkleidos::drivers::bus::internal_i2c::setDeviceFrequency (uint8_t address, uint32_t frequencyHz)
Clock one device of the bus at a rate of its own.

Variables

constexpr uint32_tkleidos::drivers::bus::internal_i2c::kDefaultFrequencyHz
Default bus clock (100 kHz).
constexpr uint32_tkleidos::drivers::bus::internal_i2c::kDefaultTimeoutMs
constexpr size_tkleidos::drivers::bus::internal_i2c::kCopiedRegisterWriteBytes

Function Documentation

beginPlatform() #

bool kleidos::drivers::bus::internal_i2c::beginPlatform(uint32_t frequencyHz = kDefaultFrequencyHz, uint32_t timeoutMs = kDefaultTimeoutMs)

Start (or restart) the bus with the platform facade.

Parameters
NameDirectionMeaning
frequencyHzinBus clock in Hz.
timeoutMsinPer-transfer timeout in milliseconds.
Return values
ValueMeaning
trueif the bus started.
falseon driver install or configuration failure.

configured() #

bool kleidos::drivers::bus::internal_i2c::configured()

Whether this board declares an internal I2C bus (its SDA, SCL and port all set in its configuration).

Return values
ValueMeaning
trueThe board's configuration gives the bus pins and a controller port.
falseIt leaves any of them at -1, so beginPlatform has nothing to start (the LILYGO T-Deck's I2C devices are not on such a bus).

pins() #

Pins kleidos::drivers::bus::internal_i2c::pins()

Get this board's internal I2C pin map.

Returns
Struct containing sda, scl and port for this board.

readRegister() #

bool kleidos::drivers::bus::internal_i2c::readRegister(uint8_t address, uint8_t reg, uint8_t * data, size_t length, uint32_t frequencyHz, uint32_t timeoutMs = kDefaultTimeoutMs)

Read bytes from a register of an I2C device.

Parameters
NameDirectionMeaning
addressinDevice I2C address (7-bit).
reginRegister address to read from.
dataoutBuffer receiving the read bytes.
lengthinNumber of bytes to read.
frequencyHzinBus clock in Hz.
timeoutMsinPer-transfer timeout in milliseconds.
Return values
ValueMeaning
trueif the transfer completed without error.
falseon NACK, bus error, or timeout.

ready() #

bool kleidos::drivers::bus::internal_i2c::ready()

Whether the internal bus exists right now (a beginPlatform that took, and no teardown since).

A chip that does not answer while this is false has said nothing about itself: the bus it would answer on is not there yet, or no longer (bus::BusGatedProbe).

Return values
ValueMeaning
trueThe bus is up and transfers can reach its devices.
falseIt has not been started this boot, or was torn down.

setDeviceFrequency() #

bool kleidos::drivers::bus::internal_i2c::setDeviceFrequency(uint8_t address, uint32_t frequencyHz)

Clock one device of the bus at a rate of its own.

The frequencyHz the register calls take is not applied per call: every device runs at the rate beginPlatform gave the bus until its driver asks for another here (bus::I2cDevice::applyBusSpeed). The other devices keep their rate.

Parameters
NameDirectionMeaning
addressinDevice I2C address (7-bit).
frequencyHzinIts bus clock in Hz.
Return values
ValueMeaning
trueThe device is clocked at frequencyHz from its next transfer.
falseThe platform facade refused the rate (no device at address on a started bus, or a rate it cannot set).

writeRegister() #

bool kleidos::drivers::bus::internal_i2c::writeRegister(uint8_t address, uint8_t reg, const uint8_t * data, size_t length, uint32_t frequencyHz, uint32_t timeoutMs = kDefaultTimeoutMs)

Write bytes to a register of an I2C device.

Parameters
NameDirectionMeaning
addressinDevice I2C address (7-bit).
reginRegister address to write to.
datainData bytes to write.
lengthinNumber of bytes to write. The whole write is one transaction, so a long one must fit timeoutMs at the device's bus clock.
frequencyHzinBus clock in Hz.
timeoutMsinPer-transfer timeout in milliseconds.
Return values
ValueMeaning
trueif the transfer completed without error.
falseon NACK, bus error, or timeout.

Variable Documentation

kCopiedRegisterWriteBytes #

size_t kleidos::drivers::bus::internal_i2c::kCopiedRegisterWriteBytes
constexpr

Longest payload writeRegister copies behind the register address into one stack buffer. A longer one goes out uncopied, as the second buffer of the same transaction.

kDefaultTimeoutMs #

uint32_t kleidos::drivers::bus::internal_i2c::kDefaultTimeoutMs
constexpr

Default per-transfer timeout.