Skip to content

Commit

Permalink
Remove pins from constructors
Browse files Browse the repository at this point in the history
  • Loading branch information
bugadani committed Nov 7, 2024
1 parent 4e105da commit 5704ec8
Show file tree
Hide file tree
Showing 9 changed files with 58 additions and 65 deletions.
2 changes: 2 additions & 0 deletions esp-hal/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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)
Expand Down
21 changes: 15 additions & 6 deletions esp-hal/MIGRATING-0.21.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
66 changes: 23 additions & 43 deletions esp-hal/src/i2c/master/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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];
Expand Down Expand Up @@ -346,30 +346,6 @@ impl<'d, T, DM: Mode> I2c<'d, DM, T>
where
T: Instance,
{
fn new_internal(
i2c: impl Peripheral<P = T> + 'd,
sda: impl Peripheral<P = impl PeripheralOutput> + 'd,
scl: impl Peripheral<P = impl PeripheralOutput> + '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()
}
Expand Down Expand Up @@ -446,14 +422,16 @@ where
Ok(())
}

fn with_sda(self, sda: impl Peripheral<P = impl PeripheralOutput> + 'd) -> Self {
/// Connect a pin to the I2C SDA signal.
pub fn with_sda(self, sda: impl Peripheral<P = impl PeripheralOutput> + '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<P = impl PeripheralOutput> + 'd) -> Self {
/// Connect a pin to the I2C SCL signal.
pub fn with_scl(self, scl: impl Peripheral<P = impl PeripheralOutput> + 'd) -> Self {
let info = self.info();
let input = info.scl_input;
let output = info.scl_output;
Expand Down Expand Up @@ -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<P = impl Instance> + 'd,
sda: impl Peripheral<P = impl PeripheralOutput> + 'd,
scl: impl Peripheral<P = impl PeripheralOutput> + 'd,
config: Config,
) -> Self {
Self::new_typed(i2c.map_into(), sda, scl, config)
pub fn new(i2c: impl Peripheral<P = impl Instance> + 'd, config: Config) -> Self {
Self::new_typed(i2c.map_into(), config)
}
}

Expand All @@ -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<P = T> + 'd,
sda: impl Peripheral<P = impl PeripheralOutput> + 'd,
scl: impl Peripheral<P = impl PeripheralOutput> + 'd,
config: Config,
) -> Self {
Self::new_internal(i2c, sda, scl, config)
pub fn new_typed(i2c: impl Peripheral<P = T> + '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
Expand Down
4 changes: 3 additions & 1 deletion examples/src/bin/embassy_i2c.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
4 changes: 3 additions & 1 deletion examples/src/bin/embassy_i2c_bmp180_calibration_data.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
9 changes: 3 additions & 6 deletions examples/src/bin/i2c_bmp180_calibration_data.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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];
Expand Down
9 changes: 3 additions & 6 deletions examples/src/bin/i2c_display.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
4 changes: 3 additions & 1 deletion examples/src/bin/lcd_cam_ov2640.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
4 changes: 3 additions & 1 deletion hil-test/tests/i2c.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
}
Expand Down

0 comments on commit 5704ec8

Please sign in to comment.