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 error recovery #2141

Merged
merged 7 commits into from
Sep 11, 2024
Merged

I2C error recovery #2141

merged 7 commits into from
Sep 11, 2024

Conversation

bjoernQ
Copy link
Contributor

@bjoernQ bjoernQ commented Sep 11, 2024

Thank you for your contribution!

We appreciate the time and effort you've put into this pull request.
To help us review it efficiently, please ensure you've gone through the following checklist:

Submission Checklist 📝

  • I have updated existing examples or added new ones (if applicable).
  • I have used cargo xtask fmt-packages command to ensure that all changed code is formatted correctly.
  • My changes were added to the CHANGELOG.md in the proper section.
  • I have added necessary changes to user code to the Migration Guide.
  • My changes are in accordance to the esp-rs API guidelines

Extra:

Pull Request Details 📖

Description

Fixes #1790

Recovering from an error didn't work as expected for I2C - this does it "the hard way" by resetting and re-initializing the whole peripheral (since nothing else seemed to help).

In theory a user could drop the driver and re-create it but in real-life code that can become very cumbersome - so now after running into an error the peripheral is always reset and re-initialized

Testing

I adapted the test to check the undesired behavior.

Additionally having this in examples resembles the code from the linked issue and can be used to test this locally (make sure to have something detectable connected)

//! - SDA => GPIO4
//! - SCL => GPIO5

//% CHIPS: esp32 esp32c2 esp32c3 esp32c6 esp32h2 esp32s2 esp32s3

#![no_std]
#![no_main]

use esp_backtrace as _;
use esp_hal::{gpio::Io, i2c::I2C, prelude::*};
use esp_println::println;

#[entry]
fn main() -> ! {
    let mut peripherals = esp_hal::init(esp_hal::Config::default());

    let mut io = Io::new(peripherals.GPIO, peripherals.IO_MUX);

    // Create a new peripheral object with the described wiring and standard
    // I2C clock speed:
    let mut i2c0 = I2C::new(
        &mut peripherals.I2C0,
        &mut io.pins.gpio4,
        &mut io.pins.gpio5,
        100.kHz(),
    );

    let delay = esp_hal::delay::Delay::new();

    for addr in 0..=127 {
        println!("Scan {}", addr as u8);
        let res = i2c0.read(addr, &mut [0]);

        match res {
            Ok(_) => println!("Dev Found @ {}", addr as u8),
            Err(e) => {
                println!("No Dev {:?}", e);
            }
        }

        delay.delay_millis(5);
    }

    loop {}
}

esp-hal/src/i2c.rs Outdated Show resolved Hide resolved
@bjoernQ bjoernQ marked this pull request as ready for review September 11, 2024 12:22
esp-hal/src/i2c.rs Outdated Show resolved Hide resolved
Copy link
Member

@MabezDev MabezDev left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I looked at how esp-idf handles time outs etc, and they also just queue up full resets of the peripheral when an error occurs.

LGTM

Copy link
Contributor

@bugadani bugadani left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks!

@bugadani bugadani added this pull request to the merge queue Sep 11, 2024
Merged via the queue into esp-rs:main with commit 24c545d Sep 11, 2024
27 checks passed
@bjoernQ bjoernQ deleted the esp-hal/i2c-error-recovery branch November 26, 2024 08:42
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
3 participants