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.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§

Structs§

Enums§

Traits§