Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

I2C master: Read data does not match actually present on bus. #3034

Open
katyo opened this issue Jan 26, 2025 · 6 comments
Open

I2C master: Read data does not match actually present on bus. #3034

katyo opened this issue Jan 26, 2025 · 6 comments
Assignees
Labels
beta-blocker bug Something isn't working peripheral:i2c I2C peripheral
Milestone

Comments

@katyo
Copy link
Contributor

katyo commented Jan 26, 2025

Bug description

The data in read buffer does not match data which slave writes to the bus. For example it reads 0x83 while on a bus I see the 0xC3, it reads 0x03 while on a bus 0x83 is present.
I deals with a ESP32-C3. I use the default I2C config with SCL on a GPIO3, SDA on a GPIO4. The hardware pull-ups is a 4.7K both. If that matter the slave device is a relative humidity and temperature sensor HTU21D.

Image

Image

Looks like the 7th and 6th bits changes randomly.

To Reproduce

I use the following code snippet:

    esp_println::logger::init_logger_from_env();

    let config = esp_hal::Config::default().with_cpu_clock(CpuClock::max());
    let peripherals = esp_hal::init(config);

    let mut i2c = I2c::new(peripherals.I2C0, i2c::Config::default())
        .unwrap()
        .with_sda(peripherals.GPIO4)
        .with_scl(peripherals.GPIO3);

    loop {
        use embedded_hal::i2c::I2c;

        let mut d = [0u8];

        i2c.write_read(0x40, &[0xe7], &mut d).unwrap();
        println!("data: {d:x?}");

        Timer::after_secs(1).await;
    }

I tried both blocking and async modes with same results. I'm on a main esp-hal branch.

UPD:
Also I tried optimized (release) build but the problem is same.

Expected behavior

Read data same as an actually present on a bus.

Environment

  • Target device: ESP32-C3
    Chip type:         esp32c3 (revision v0.3)
    Crystal frequency: 40 MHz
    Flash size:        4MB
    Features:          WiFi, BLE
    MAC address:       60:55:f9:2b:8c:73
    
  • Crate name and version:
    esp-hal v0.23.1
@katyo katyo added bug Something isn't working status:needs-attention This should be prioritized labels Jan 26, 2025
@katyo katyo changed the title I2C master: Read data does not not match actually present on bus. I2C master: Read data does not match actually present on bus. Jan 26, 2025
@katyo
Copy link
Contributor Author

katyo commented Jan 26, 2025

If I lower the frequency to 50 kHz, the data is read correctly. It seems to be a hardware issue. May be it needs some configuration for input filters, but seems it still hasn't supported.

@MabezDev MabezDev removed the status:needs-attention This should be prioritized label Jan 27, 2025
@MabezDev MabezDev added this to the 1.0.0-beta.0 milestone Jan 27, 2025
@bugadani bugadani self-assigned this Jan 27, 2025
@bugadani
Copy link
Contributor

Thanks for reporting this! Just to confirm, the wavefor doesn't change in a meaningful way when you look at it, right?

@bugadani
Copy link
Contributor

Can you please check if #3040 solves your issue?

@lure23
Copy link
Contributor

lure23 commented Jan 29, 2025

@katyo Interested to follow this. Just making sure you know about potential problems if using all these together:

  • ESP32-C3
  • probe-rs and RTT (e.g. defmt) <-- edited
  • I2C bus, via esp-hal
    ?

The problem is that probe-rs occasionally needs to stop the MCU core from running (doesn't need to do that in same way on, say, ESP32-C6), and this can cause effects on time-critical things like I2C. Because of this, I moved my i2C project to use C6.

If you don't use probe-rs, just ignore this.

@bugadani
Copy link
Contributor

The problem is that probe-rs occasionally needs to stop the MCU core from running

This is only an issue if you use anything RTT-based, like defmt, but not true in general. I'm not sure if the I2C peripheral would be affected by this, though...

@lure23
Copy link
Contributor

lure23 commented Jan 29, 2025

The problem is that probe-rs occasionally needs to stop the MCU core from running

This is only an issue if you use anything RTT-based, like defmt, but not true in general. I'm not sure if the I2C peripheral would be affected by this, though...

Hmm.. so I likely should change the writing in the link I made. I'll try!

For me, I2C was indeed affected; that's why I brought this up. But it only occurred because my VL53L5CX peripheral takes a longish (>64kB) transfer, to upload its firmware.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
beta-blocker bug Something isn't working peripheral:i2c I2C peripheral
Projects
Status: Todo
Development

No branches or pull requests

4 participants