API referencestd/embedded

std/embedded/peripheral/designware_i2c

Public declarations, types, methods, and source contracts for std/embedded/peripheral/designware_i2c.

On this page

Library guide · API index and notation

This page is generated from the bundled library in this checkout. Signatures and adjacent source comments are reproduced below; the linked guide explains usage, storage, failures, and platform support.

import "std/embedded/peripheral/designware_i2c"

Names used in signatures

Unqualified names denote this package’s types (including other source files in the same package), language built-ins, or generic parameters such as T. Qualified names use the import aliases below. These aliases belong to the library source; import a dependency yourself to use its alias in your program.

Alias Package Source file
hal std/embedded/hal designware_i2c.dodo

Timing

Struct · Source

The controller settings for one SCL frequency, from timing.

pub struct Timing {
    // The controller's input clock, in hertz.
    pub clock_hz: u32
    // The SCL high and low count registers. SCL is high for
    // `high + spike + 7` input clock cycles and low for `low + 1`.
    pub high: u32
    pub low: u32
    // Longest glitch the input filters suppress, in input clock cycles.
    pub spike: u32
    // How long SDA is held after SCL falls, in input clock cycles.
    pub hold: u32
    // The resulting SCL frequency, in hertz, counting the controller's
    // extra cycles but not the bus's rise times, which stretch it further.
    pub frequency_hz: u32
}

timing

Function · Source

Controller settings for an SCL frequency of frequency_hz (1 Hz to hal.I2C_FAST_PLUS_HZ) from an input clock of clock_hz. SCL is low for 60% of each period, which meets the I2C specification’s minimum low times at 100 kHz, 400 kHz, and 1 MHz. Returns InvalidInput for a frequency the controller cannot produce from this clock: below about 1.2 kHz at 125 MHz, or 1 MHz from a clock below about 36 MHz.

pub fn timing(clock_hz: u32, frequency_hz: u32) -> Timing!hal.Error

Controller

Struct · Source

An I2C bus master on one DW_apb_i2c controller with registers R, any hal.Registers, such as a hal.RegisterBlock or a simulated controller in a test.

Transactions of any length run two ways. The blocking methods (write, read, write_read) return once the transaction has ended. The polling methods (poll_write, poll_read, poll_write_read) never wait: each call drains the bytes received into the caller’s buffer, tops up the transmit FIFO with bytes to send and read commands, and reports whether the transaction has ended, so a program does other work meanwhile, or an executor task returns Pending until the controller’s interrupt (with listen) or a timer wakes it. Both keep no more read commands outstanding than the receive FIFO holds, so it never overflows.

A polled transaction holds the bus between polls. When the transmit FIFO runs empty before the command that ends the transaction with STOP, the controller holds SCL low and waits for more commands instead of ending it: the RP2040 and RP2350 integrate the controller with IC_EMPTYFIFO_HOLD_MASTER_EN set, which the blocking methods rely on just as much whenever an interrupt delays them between two bytes. Every device accepts the held clock as clock stretching, but a polled transaction that is never polled again holds the bus forever: finish it or cancel it. A transaction that never ends, because a device holds SCL low, stays unfinished; bound the wait with a timer, as hal.BlockingI2c does with a poll count, and cancel it.

pub struct Controller<R: hal.Registers>: hal.I2c + hal.I2cPoll {
    // Private implementation fields omitted.
}

Controller.new

Function · Source

Configure a controller fresh out of reset as the bus’s only master, using timing from the same clock that feeds it. It is enabled by the first transaction.

pub fn new(registers: R, timing: Timing) -> Controller<R>

Controller.frequency_hz

Function · Source

The SCL frequency, in hertz, as computed by timing: the request, rounded to whole input clock cycles.

pub fn frequency_hz(&self) -> u32

Controller.set_frequency

Function · Source

Change the SCL frequency between transactions, for example to talk to a slower device. InvalidInput leaves the frequency unchanged; Busy while a polled transaction runs.

pub fn set_frequency(&mut self, frequency_hz: u32) -> void!hal.Error

Controller.write

Function · Source

Send bytes to the device at address, then a STOP condition. NoAcknowledge if the address or a byte is not acknowledged, Bus if another master won arbitration, Timeout if the bus is stuck, InvalidInput for an empty transfer or an address above 0x7F, and Busy while a polled transaction runs.

pub fn write(&mut self, address: u8, bytes: &[u8]) -> void!hal.Error

Controller.read

Function · Source

Fill buffer from the device at address, then a STOP condition. Errors as for write.

pub fn read(&mut self, address: u8, buffer: &mut[u8]) -> void!hal.Error

Controller.write_read

Function · Source

Send bytes, then fill buffer after a repeated START, all in one transaction, so no other master can get in between; the usual way to read a numbered register. With one of them empty it is a plain read or write. Errors as for write, and InvalidInput only when both are empty.

pub fn write_read(&mut self, address: u8, bytes: &[u8], buffer: &mut[u8]) -> void!hal.Error

Controller.write_ten_bit

Function · Source

write, read, and write_read for a device with a 10-bit address, 0 to 0x3FF. The controller sends the two address bytes the I2C specification prescribes (11110, the top two bits, then the low eight) and repeats the first with the read bit before reading. InvalidInput for an address above 0x3FF.

pub fn write_ten_bit(&mut self, address: u16, bytes: &[u8]) -> void!hal.Error

Controller.read_ten_bit

Function · Source

pub fn read_ten_bit(&mut self, address: u16, buffer: &mut[u8]) -> void!hal.Error

Controller.write_read_ten_bit

Function · Source

pub fn write_read_ten_bit(&mut self, address: u16, bytes: &[u8], buffer: &mut[u8]) -> void!hal.Error

Controller.probe

Function · Source

Whether a device acknowledges address, found by reading one byte from it, as i2cdetect -r does, since the controller cannot send an address alone. The byte is discarded. Errors other than NoAcknowledge as for read. A few write-only devices take an unexpected read badly; probe only addresses that may hold such a device if its datasheet allows reads.

pub fn probe(&mut self, address: u8) -> bool!hal.Error

Controller.poll_write

Function · Source

Advance a write of bytes to the device at address, then a STOP, without waiting: false while it runs, true once it has ended. The first call starts the transaction, as many bytes as the transmit FIFO holds; later calls pass the same address and bytes and queue the rest as the FIFO drains. Errors as for write, after which the transaction is over, except that a stuck bus is never reported: the transaction stays unfinished until cancel. InvalidInput for a call whose address or lengths differ from the running transaction’s, which keeps running. Starting a transaction to another address than the last waits for the controller to disable, which it does at once between transactions.

pub fn poll_write(&mut self, address: u8, bytes: &[u8]) -> bool!hal.Error

Controller.poll_read

Function · Source

Advance a read of buffer.len bytes, as for poll_write. Each call moves the bytes received so far into buffer; they are all there once it returns true.

pub fn poll_read(&mut self, address: u8, buffer: &mut[u8]) -> bool!hal.Error

Controller.poll_write_read

Function · Source

Advance write_read as one transaction, as for poll_write and poll_read: the bytes, a repeated START, and the read.

pub fn poll_write_read(&mut self, address: u8, bytes: &[u8], buffer: &mut[u8]) -> bool!hal.Error

Controller.poll_write_ten_bit

Function · Source

The polled transactions above for a device with a 10-bit address, as for write_ten_bit.

pub fn poll_write_ten_bit(&mut self, address: u16, bytes: &[u8]) -> bool!hal.Error

Controller.poll_read_ten_bit

Function · Source

pub fn poll_read_ten_bit(&mut self, address: u16, buffer: &mut[u8]) -> bool!hal.Error

Controller.poll_write_read_ten_bit

Function · Source

pub fn poll_write_read_ten_bit(&mut self, address: u16, bytes: &[u8], buffer: &mut[u8]) -> bool!hal.Error

Controller.is_busy

Function · Source

True while a polled transaction runs: from its first poll until a poll reports its end or it is cancelled.

pub fn is_busy(&self) -> bool

Controller.cancel

Function · Source

Abandon the running polled transaction, if any: the controller aborts it, which ends it with a STOP within a byte time, and is disabled, so the next transaction starts afresh. Waits for that, at most about a tenth of a second while a device holds SCL low.

pub fn cancel(&mut self)

Controller.listen

Function · Source

Raise the controller’s interrupt (I2C0_IRQ or I2C1_IRQ on the RP2040 and RP2350) whenever a poll of the running transaction can make progress: the transmit FIFO is half empty while commands remain to queue, the receive FIFO is half full, or the transaction ended with a STOP or an abort. Each poll sets INTR_MASK for what it waits on next, and an idle controller raises nothing. The condition lasts until a poll has dealt with it, so a handler masks the interrupt in the NVIC and raises an executor signal, and the task, woken, polls and enables the interrupt again.

pub fn listen(&mut self)

Controller.ignore

Function · Source

Stop raising the interrupt.

pub fn ignore(&mut self)

Controller.release

Function · Source

Disable the controller and give back its registers. A polled transaction still running is cut off.

pub fn release(self) -> R

target_address

Function · Source

The SAR value for a target address: a 7-bit address outside the ranges the I2C specification reserves (0x00 to 0x07 and 0x78 to 0x7F), or with ten_bit any 10-bit address, 0 to 0x3FF. InvalidInput otherwise.

pub fn target_address(address: u16, ten_bit: bool) -> u32!hal.Error

Target

Struct · Source

A target (slave) on an I2C bus, answering a master at one address with one DW_apb_i2c controller with registers R, any hal.Registers; it implements hal.I2cTarget. The program polls for what the master does: written bytes arrive in order, each read waits for respond, and a STOP ends the transaction.

match target.poll(&mut buffer)? { hal.I2cTargetEvent.Write(count, start) => { … } hal.I2cTargetEvent.Read => { target.respond(&reply)? } … }

The controller holds SCL low while its receive FIFO is full or a read waits, so a slow program delays the master but loses nothing; masters accept such clock stretching, unless they give up after a timeout of their own (SMBus allows 25 ms). Bytes queued by respond beyond what the master reads are dropped when it next reads. General calls (address 0) are not acknowledged.

pub struct Target<R: hal.Registers>: hal.I2cTarget {
    // Private implementation fields omitted.
}

Target.new

Function · Source

Configure a controller fresh out of reset as a target at address, a 10-bit address with ten_bit, and enable it. The address must pass target_address; chip packages check it before they take the controller. timing, for the bus’s SCL frequency from the clock that feeds the controller, sets the spike filter and SDA hold time; a target follows the master’s clock whatever its frequency.

pub fn new(registers: R, timing: Timing, address: u16, ten_bit: bool) -> Target<R>

Target.poll

Function · Source

What the master did since the last poll, in order: first the bytes it wrote, as many as fit buffer, then a STOP, then a read that waits for respond. Never waits. A program that polls more slowly than whole transactions arrive may see one Stop for several; start still marks where each write begins. Overrun if the receive FIFO overflowed and written bytes were lost (RX_OVER), which cannot happen while the controller holds SCL low on a full FIFO, as configured here, but would on an integration without that hold; the bytes still in the FIFO come with later polls. The controller detects no other failure a target could report.

pub fn poll(&mut self, buffer: &mut[u8]) -> hal.I2cTargetEvent!hal.Error

Target.respond

Function · Source

Answer a Read from poll with bytes, as many as the transmit FIFO holds; returns how many. The master reads them one by one and may stop early; if it reads on, the next poll reports another Read. An empty bytes sends 0xFF, which is what the master would read from an idle bus, so that it is never left waiting, and returns 0. Never fails: the error is there for hal.I2cTarget.

pub fn respond(&mut self, bytes: &[u8]) -> usize!hal.Error

Target.set_address

Function · Source

Answer at address from the next transaction on, a 10-bit address with ten_bit, as for target_address. Waits, at most about a second, for the current transaction to end; Timeout if it does not, InvalidInput for a reserved or too large address. Either leaves the address unchanged.

pub fn set_address(&mut self, address: u16, ten_bit: bool) -> void!hal.Error

Target.release

Function · Source

Stop answering, disable the controller, and give back its registers.

pub fn release(self) -> R

Bus

Struct · Source

A DW_apb_i2c on the chip, as chip packages hand it to applications. It implements hal.I2c and hal.I2cPoll like Controller but keeps the register block: RegisterBlock trusts its owner with every offset, which only the driver may be.

pub struct Bus: hal.I2c + hal.I2cPoll {
    // Private implementation fields omitted.
}

Bus.at

Function · Source

SAFETY: as for hal.RegisterBlock.at, with base a DW_apb_i2c whose reset and pins the caller has set up and that only this bus uses.

pub unsafe fn at(base: usize, timing: Timing) -> Bus

Requires an unsafe context. Follow the source safety preconditions and the linked guide before calling this API.

Bus.write

Function · Source

pub fn write(&mut self, address: u8, bytes: &[u8]) -> void!hal.Error

Bus.read

Function · Source

pub fn read(&mut self, address: u8, buffer: &mut[u8]) -> void!hal.Error

Bus.write_read

Function · Source

pub fn write_read(&mut self, address: u8, bytes: &[u8], buffer: &mut[u8]) -> void!hal.Error

Bus.write_ten_bit

Function · Source

The transactions above for a device with a 10-bit address.

pub fn write_ten_bit(&mut self, address: u16, bytes: &[u8]) -> void!hal.Error

Bus.read_ten_bit

Function · Source

pub fn read_ten_bit(&mut self, address: u16, buffer: &mut[u8]) -> void!hal.Error

Bus.write_read_ten_bit

Function · Source

pub fn write_read_ten_bit(&mut self, address: u16, bytes: &[u8], buffer: &mut[u8]) -> void!hal.Error

Bus.probe

Function · Source

Whether a device acknowledges address, from a one-byte read.

pub fn probe(&mut self, address: u8) -> bool!hal.Error

Bus.poll_write

Function · Source

The transactions without waiting, for programs that do other work meanwhile; see Controller.poll_write.

pub fn poll_write(&mut self, address: u8, bytes: &[u8]) -> bool!hal.Error

Bus.poll_read

Function · Source

pub fn poll_read(&mut self, address: u8, buffer: &mut[u8]) -> bool!hal.Error

Bus.poll_write_read

Function · Source

pub fn poll_write_read(&mut self, address: u8, bytes: &[u8], buffer: &mut[u8]) -> bool!hal.Error

Bus.poll_write_ten_bit

Function · Source

pub fn poll_write_ten_bit(&mut self, address: u16, bytes: &[u8]) -> bool!hal.Error

Bus.poll_read_ten_bit

Function · Source

pub fn poll_read_ten_bit(&mut self, address: u16, buffer: &mut[u8]) -> bool!hal.Error

Bus.poll_write_read_ten_bit

Function · Source

pub fn poll_write_read_ten_bit(&mut self, address: u16, bytes: &[u8], buffer: &mut[u8]) -> bool!hal.Error

Bus.is_busy

Function · Source

pub fn is_busy(&self) -> bool

Bus.cancel

Function · Source

pub fn cancel(&mut self)

Bus.listen

Function · Source

Raise the bus’s interrupt whenever a poll can make progress; see Controller.listen.

pub fn listen(&mut self)

Bus.ignore

Function · Source

pub fn ignore(&mut self)

Bus.frequency_hz

Function · Source

The SCL frequency, in hertz.

pub fn frequency_hz(&self) -> u32

Bus.set_frequency

Function · Source

Change the SCL frequency between transactions.

pub fn set_frequency(&mut self, frequency_hz: u32) -> void!hal.Error

Bus.release

Function · Source

Disable the controller and return its base address, for the chip package to take the bus back.

pub fn release(self) -> usize

TargetBus

Struct · Source

A DW_apb_i2c on the chip as a target, as chip packages hand it to applications. It implements hal.I2cTarget; see Target.

pub struct TargetBus: hal.I2cTarget {
    // Private implementation fields omitted.
}

TargetBus.at

Function · Source

SAFETY: as for Bus.at. address and ten_bit must pass target_address.

pub unsafe fn at(base: usize, timing: Timing, address: u16, ten_bit: bool) -> TargetBus

Requires an unsafe context. Follow the source safety preconditions and the linked guide before calling this API.

TargetBus.poll

Function · Source

What the master did since the last poll; see Target.poll.

pub fn poll(&mut self, buffer: &mut[u8]) -> hal.I2cTargetEvent!hal.Error

TargetBus.respond

Function · Source

Answer a Read with bytes; see Target.respond.

pub fn respond(&mut self, bytes: &[u8]) -> usize!hal.Error

TargetBus.set_address

Function · Source

Answer at another address; see Target.set_address.

pub fn set_address(&mut self, address: u16, ten_bit: bool) -> void!hal.Error

TargetBus.release

Function · Source

Disable the controller and return its base address, for the chip package to take it back.

pub fn release(self) -> usize

Search every guide, reference page, and API declaration.