Expand description
Inter-Integrated Circuit (I2C) bus
See Section 12.2 for more details
§Usage
use fugit::RateExtU32;
use rp235x_hal::{self as hal, gpio::Pins, i2c::I2C, Sio};
let mut peripherals = hal::pac::Peripherals::take().unwrap();
let sio = Sio::new(peripherals.SIO);
let pins = Pins::new(
peripherals.IO_BANK0,
peripherals.PADS_BANK0,
sio.gpio_bank0,
&mut peripherals.RESETS,
);
let mut i2c = I2C::i2c1(
peripherals.I2C1,
pins.gpio18.reconfigure(), // sda
pins.gpio19.reconfigure(), // scl
400.kHz(),
&mut peripherals.RESETS,
125_000_000.Hz(),
);
// Scan for devices on the bus by attempting to read from them
use embedded_hal_0_2::prelude::_embedded_hal_blocking_i2c_Read;
for i in 0..=127u8 {
let mut readbuf: [u8; 1] = [0; 1];
let result = i2c.read(i, &mut readbuf);
if let Ok(d) = result {
// Do whatever work you want to do with found devices
// writeln!(uart, "Device found at address{:?}", i).unwrap();
}
}
// Write some data to a device at 0x2c
use embedded_hal_0_2::prelude::_embedded_hal_blocking_i2c_Write;
i2c.write(0x2Cu8, &[1, 2, 3]).unwrap();
// Write and then read from a device at 0x3a
use embedded_hal_0_2::prelude::_embedded_hal_blocking_i2c_WriteRead;
let mut readbuf: [u8; 1] = [0; 1];
i2c.write_read(0x2Cu8, &[1, 2, 3], &mut readbuf).unwrap();See examples/i2c.rs for a complete example
§Async Usage
See examples/i2c_async.rs and examples/i2c_async_irq.rs for complete examples.
Modules§
- peripheral
- I2C Peripheral (slave) implementation
Structs§
- Controller
- Marker for an I2C peripheral operating as a controller.
- I2C
- I2C peripheral
- Peripheral
- Marker for an I2C block operating as a peripehral.
- Validated
PinScl - A runtime validated Scl pin for I2C.
- Validated
PinSda - A runtime validated Sda pin for I2C.
Enums§
- Error
- I2C error
Traits§
- I2CMode
- Operational mode of the I2C peripheral.
- I2cDevice
- Pac I2C device
- Valid
Address - Marks valid/supported address types
- Valid
PinId Scl - Marker for PinId that can serve as Scl
- Valid
PinId Sda - Marker for PinId that can serve as Sda
- Valid
PinScl - Valid Scl
- Valid
PinSda - Valid Sda