rust-embedded/linux-embedded-hal

Remote I/O error when using i2c and two addresses

psiphi75 opened this issue · 1 comments

I get the following error when I try to access two different devices on the same i2c bus.

thread 'main' panicked at 'called `Result::unwrap()` on an `Err` value: Io(Os { code: 121, 
kind: Other, message: "Remote I/O error" })', libcore/result.rs:1009:5

Here is my code:

extern crate embedded_hal;
extern crate linux_embedded_hal as hal;

use self::embedded_hal::blocking::i2c::{Read, Write};

use hal::I2cdev;

fn main() {
  let mut i2c = I2cdev::new("/dev/i2c-2").unwrap();
  const ADDR_1: u8 = 0x68;
  const ADDR_2: u8 = 0x0c;

  // Soft reset
  i2c.write(ADDR_1, &[0x6b]).unwrap();
  i2c.write(ADDR_1, &[0x80]).unwrap();

  // send WHO_AM_I command
  i2c.write(ADDR_1, &[0x75]).unwrap();
  let bytes: &mut [u8] = &mut [0; 1];
  i2c.read(ADDR_1, bytes).unwrap();
  debug_assert_eq!(bytes[0], 0x71); // Ok

  // Send WHO_AM_I command to other device
  i2c.write(ADDR_2, &[0x00]).unwrap(); // CRASH!
}

Any ideas?

This was my mistake. I used the incorrect Address value. Fixing the address fixes the issue.

For my own future reference using the following i2c-tools command will show which devices are available on the bus:

i2cdetect -y -r 2