Module rp2040_hal::i2c
source · Expand description
Inter-Integrated Circuit (I2C) bus
See Chapter 4 Section 3 for more details
Usage
use fugit::RateExtU32;
use rp2040_hal::{i2c::I2C, gpio::Pins, pac, Sio};
let mut peripherals = 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.into_mode(), // sda
pins.gpio19.into_mode(), // 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::prelude::_embedded_hal_blocking_i2c_Read;
for i in 0..=127 {
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::prelude::_embedded_hal_blocking_i2c_Write;
i2c.write(0x2c, &[1, 2, 3]).unwrap();
// Write and then read from a device at 0x3a
use embedded_hal::prelude::_embedded_hal_blocking_i2c_WriteRead;
let mut readbuf: [u8; 1] = [0; 1];
i2c.write_read(0x2c, &[1, 2, 3], &mut readbuf).unwrap();
See examples/i2c.rs for a complete example
Modules
Controller implementaion
Peripheral implementation
Structs
I2C peripheral
Enums
Marker for an I2C peripheral operating as a controller.
I2C error
Marker for an I2C peripheral operating as a peripehral.