esp-rs / esp-idf-sys

Bindings for ESP-IDF (Espressif's IoT Development Framework)
Apache License 2.0
273 stars 123 forks source link

I2C NoAcknowledge(Unknown) On esp32c3 with MPU9250 #93

Closed Gazedo closed 2 years ago

Gazedo commented 2 years ago

Hi All, I'm trying to get a esp32c3 (m5stack c3u board) to communicate with a mpu9250, its connected with:

But no matter what I keep getting the error:

thread 'main' panicked at 'called `Result::unwrap()` on an `Err` value: I2cError { kind: NoAcknowledge(Unknown), cause: EspError(-1) }', src/main.rs:62:37

I've tried using the write_read function and just directly using a mpu9250 crate so I think the issue lays with the esp_idf_sys crate. I was initially using version 0.37.4 and I tried to go back but I can't get version 0.35.2 to compile. I've confirmed the mpu9250 is functional by downloading a demo arduino program to the esp32c3 and everything reads just fine.

Below is the test code I've been using. Am I doing something wrong?

use embedded_hal::blocking::i2c::{Read, Write};
use esp_idf_hal::i2c;
use esp_idf_hal::prelude::*;
use esp_idf_hal::units::FromValueType;
use esp_idf_sys as _;
use std::thread;
use std::time::Duration;

fn main() {
    // Temporary. Will disappear once ESP-IDF 4.4 is released, but for now it is necessary to call this function once,
    // or else some patches to the runtime implemented by esp-idf-sys might not link properly.
    esp_idf_sys::link_patches();
    esp_idf_svc::log::EspLogger::initialize_default();
    let per = Peripherals::take().unwrap();
    let sda = per.pins.gpio4.into_input_output().unwrap();
    let scl = per.pins.gpio5.into_output().unwrap();
    let i2c = per.i2c0;

    let config = <i2c::config::MasterConfig as Default>::default().baudrate(400.kHz().into());
    let mut i2cdev =
        i2c::Master::<i2c::I2C0, _, _>::new(i2c, i2c::MasterPins { sda, scl }, config).unwrap();

    loop {
        let mut buff: [u8; 6] = [0; 6];
        i2cdev.write(0x68, &[0x75]).unwrap();
        i2cdev.read(0x68, &mut buff).unwrap();
        log::info!("wai value is {:?}", buff);
        thread::sleep(Duration::from_millis(100));
    }
}

P.S. the line numbers are off because I elimated a ton of comments. The line that is failing is: i2cdev.write(0x68, &[0x75]).unwrap();

Gazedo commented 2 years ago

Upon further investigation I suspect the issue is with the esp_idf_hal crate as that seems to own the i2c commands. I will create an issue there.