diff --git a/esp-hal/CHANGELOG.md b/esp-hal/CHANGELOG.md index 73023b7e91..fc019796ff 100644 --- a/esp-hal/CHANGELOG.md +++ b/esp-hal/CHANGELOG.md @@ -34,6 +34,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - GPIO ETM tasks and events now accept `InputSignal` and `OutputSignal` (#2427) - `spi::master::Config` and `{Spi, SpiDma, SpiDmaBus}::apply_config` (#2448) - `embassy_embedded_hal::SetConfig` is now implemented for `spi::master::{Spi, SpiDma, SpiDmaBus}`, `i2c::master::I2c` (#2448, #?) +- `I2c::{apply_config(), with_sda(), with_scl()}` (#?) ### Changed @@ -71,6 +72,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - The `i2s::{I2sWrite, I2sWriteDma, I2sRead, I2sReadDma, I2sWriteDmaAsync, I2sReadDmaAsync}` traits have been removed. (#2316) - The `ledc::ChannelHW` trait is no longer generic. (#2387) - The `I2c::new_with_timeout` constructors have been removed (#2361) +- `I2c::new()` no longer takes `frequency` and pins as parameters. (#?) - The `spi::master::HalfDuplexReadWrite` trait has been removed. (#2373) - The `Spi::with_pins` methods have been removed. (#2373) - The `Spi::new_half_duplex` constructor have been removed. (#2373) diff --git a/esp-hal/MIGRATING-0.21.md b/esp-hal/MIGRATING-0.21.md index fcd3fd54f4..8ef8fc20f4 100644 --- a/esp-hal/MIGRATING-0.21.md +++ b/esp-hal/MIGRATING-0.21.md @@ -72,14 +72,23 @@ The I2C master driver and related types have been moved to `esp_hal::i2c::master The `with_timeout` constructors have been removed. `new` and `new_typed` now take a `Config` struct with the available configuration options. +The constructors no longer take pins. Use `with_sda` and `with_scl` instead. + ```diff +-use esp_hal::i2c::I2c; ++use esp_hal::i2c::{Config, I2c}; -let i2c = I2c::new_with_timeout(peripherals.I2C0, io.pins.gpio4, io.pins.gpio5, 100.kHz(), timeout); -+let i2c = I2c::new(peripherals.I2C0, io.pins.gpio4, io.pins.gpio5, { -+ let mut config = Config::default(); -+ config.frequency = 100.kHz(); -+ config.timeout = timeout; -+ config -+}); ++I2c::new_with_config( ++ peripherals.I2C0, ++ { ++ let mut config = Config::default(); ++ config.frequency = 100.kHz(); ++ config.timeout = timeout; ++ config ++ }, ++) ++.with_sda(io.pins.gpio4) ++.with_scl(io.pins.gpio5); ``` ## Changes to half-duplex SPI diff --git a/esp-hal/src/i2c/master/mod.rs b/esp-hal/src/i2c/master/mod.rs index f2bc19fd39..88da8e5819 100644 --- a/esp-hal/src/i2c/master/mod.rs +++ b/esp-hal/src/i2c/master/mod.rs @@ -26,10 +26,10 @@ //! // and standard I2C clock speed. //! let mut i2c = I2c::new( //! peripherals.I2C0, -//! io.pins.gpio1, -//! io.pins.gpio2, //! Config::default(), -//! ); +//! ) +//! .with_sda(io.pins.gpio1) +//! .with_scl(io.pins.gpio2); //! //! loop { //! let mut data = [0u8; 22]; @@ -346,30 +346,6 @@ impl<'d, T, DM: Mode> I2c<'d, DM, T> where T: Instance, { - fn new_internal( - i2c: impl Peripheral
+ 'd, - sda: impl Peripheral
+ 'd, - scl: impl Peripheral
+ 'd, - config: Config, - ) -> Self { - crate::into_ref!(i2c); - crate::into_mapped_ref!(sda, scl); - - let i2c = I2c { - i2c, - phantom: PhantomData, - config, - }; - - PeripheralClockControl::reset(i2c.i2c.peripheral()); - PeripheralClockControl::enable(i2c.i2c.peripheral()); - - let i2c = i2c.with_sda(sda).with_scl(scl); - - unwrap!(i2c.info().setup(&i2c.config)); - i2c - } - fn info(&self) -> &Info { self.i2c.info() } @@ -446,14 +422,16 @@ where Ok(()) } - fn with_sda(self, sda: impl Peripheral
+ 'd) -> Self { + /// Connect a pin to the I2C SDA signal. + pub fn with_sda(self, sda: impl Peripheral
+ 'd) -> Self { let info = self.info(); let input = info.sda_input; let output = info.sda_output; self.with_pin(sda, input, output) } - fn with_scl(self, scl: impl Peripheral
+ 'd) -> Self { + /// Connect a pin to the I2C SCL signal. + pub fn with_scl(self, scl: impl Peripheral
+ 'd) -> Self { let info = self.info(); let input = info.scl_input; let output = info.scl_output; @@ -485,13 +463,8 @@ impl<'d> I2c<'d, Blocking> { /// Create a new I2C instance /// This will enable the peripheral but the peripheral won't get /// automatically disabled when this gets dropped. - pub fn new( - i2c: impl Peripheral
+ 'd, - sda: impl Peripheral
+ 'd, - scl: impl Peripheral
+ 'd, - config: Config, - ) -> Self { - Self::new_typed(i2c.map_into(), sda, scl, config) + pub fn new(i2c: impl Peripheral
+ 'd, config: Config) -> Self { + Self::new_typed(i2c.map_into(), config) } } @@ -502,13 +475,20 @@ where /// Create a new I2C instance /// This will enable the peripheral but the peripheral won't get /// automatically disabled when this gets dropped. - pub fn new_typed( - i2c: impl Peripheral
+ 'd, - sda: impl Peripheral
+ 'd, - scl: impl Peripheral
+ 'd, - config: Config, - ) -> Self { - Self::new_internal(i2c, sda, scl, config) + pub fn new_typed(i2c: impl Peripheral
+ 'd, config: Config) -> Self { + crate::into_ref!(i2c); + + let i2c = I2c { + i2c, + phantom: PhantomData, + config, + }; + + PeripheralClockControl::reset(i2c.i2c.peripheral()); + PeripheralClockControl::enable(i2c.i2c.peripheral()); + + unwrap!(i2c.info().setup(&i2c.config)); + i2c } // TODO: missing interrupt APIs diff --git a/examples/src/bin/embassy_i2c.rs b/examples/src/bin/embassy_i2c.rs index c3118707c1..64c046695a 100644 --- a/examples/src/bin/embassy_i2c.rs +++ b/examples/src/bin/embassy_i2c.rs @@ -36,11 +36,13 @@ async fn main(_spawner: Spawner) { let io = Io::new(peripherals.GPIO, peripherals.IO_MUX); - let i2c0 = I2c::new(peripherals.I2C0, io.pins.gpio4, io.pins.gpio5, { + let i2c0 = I2c::new(peripherals.I2C0, { let mut config = Config::default(); config.frequency = 400.kHz(); config }) + .with_sda(io.pins.gpio4) + .with_scl(io.pins.gpio5) .into_async(); let mut lis3dh = Lis3dh::new_i2c(i2c0, SlaveAddr::Alternate).await.unwrap(); diff --git a/examples/src/bin/embassy_i2c_bmp180_calibration_data.rs b/examples/src/bin/embassy_i2c_bmp180_calibration_data.rs index cbe5677bf2..204f9e61f3 100644 --- a/examples/src/bin/embassy_i2c_bmp180_calibration_data.rs +++ b/examples/src/bin/embassy_i2c_bmp180_calibration_data.rs @@ -36,11 +36,13 @@ async fn main(_spawner: Spawner) { let io = Io::new(peripherals.GPIO, peripherals.IO_MUX); - let mut i2c = I2c::new(peripherals.I2C0, io.pins.gpio4, io.pins.gpio5, { + let mut i2c = I2c::new(peripherals.I2C0, { let mut config = Config::default(); config.frequency = 400.kHz(); config }) + .with_sda(io.pins.gpio4) + .with_scl(io.pins.gpio5) .into_async(); loop { diff --git a/examples/src/bin/i2c_bmp180_calibration_data.rs b/examples/src/bin/i2c_bmp180_calibration_data.rs index 5ffe3a2e03..7d75aa73ad 100644 --- a/examples/src/bin/i2c_bmp180_calibration_data.rs +++ b/examples/src/bin/i2c_bmp180_calibration_data.rs @@ -27,12 +27,9 @@ fn main() -> ! { // Create a new peripheral object with the described wiring and standard // I2C clock speed: - let mut i2c = I2c::new( - peripherals.I2C0, - io.pins.gpio4, - io.pins.gpio5, - Config::default(), - ); + let mut i2c = I2c::new(peripherals.I2C0, Config::default()) + .with_sda(io.pins.gpio4) + .with_scl(io.pins.gpio5); loop { let mut data = [0u8; 22]; diff --git a/examples/src/bin/i2c_display.rs b/examples/src/bin/i2c_display.rs index 25836cb4fd..cf17fa0a02 100644 --- a/examples/src/bin/i2c_display.rs +++ b/examples/src/bin/i2c_display.rs @@ -39,12 +39,9 @@ fn main() -> ! { // Create a new peripheral object with the described wiring // and standard I2C clock speed - let i2c = I2c::new( - peripherals.I2C0, - io.pins.gpio4, - io.pins.gpio5, - Config::default(), - ); + let i2c = I2c::new(peripherals.I2C0, Config::default()) + .with_sda(io.pins.gpio4) + .with_scl(io.pins.gpio5); // Initialize display let interface = I2CDisplayInterface::new(i2c); diff --git a/examples/src/bin/lcd_cam_ov2640.rs b/examples/src/bin/lcd_cam_ov2640.rs index dafa262ec9..e9998d3932 100644 --- a/examples/src/bin/lcd_cam_ov2640.rs +++ b/examples/src/bin/lcd_cam_ov2640.rs @@ -82,7 +82,9 @@ fn main() -> ! { delay.delay_millis(500u32); - let i2c = I2c::new(peripherals.I2C0, cam_siod, cam_sioc, Config::default()); + let i2c = I2c::new(peripherals.I2C0, Config::default()) + .with_sda(cam_siod) + .with_scl(cam_sioc); let mut sccb = Sccb::new(i2c); diff --git a/hil-test/tests/i2c.rs b/hil-test/tests/i2c.rs index b0afb9fc78..09718abf15 100644 --- a/hil-test/tests/i2c.rs +++ b/hil-test/tests/i2c.rs @@ -39,7 +39,9 @@ mod tests { // Create a new peripheral object with the described wiring and standard // I2C clock speed: - let i2c = I2c::new(peripherals.I2C0, sda, scl, Config::default()); + let i2c = I2c::new(peripherals.I2C0, Config::default()) + .with_sda(sda) + .with_scl(scl); Context { i2c } }