diff --git a/esp-hal-common/src/i2c.rs b/esp-hal-common/src/i2c.rs index 406482f4fde..35d4d113b5c 100644 --- a/esp-hal-common/src/i2c.rs +++ b/esp-hal-common/src/i2c.rs @@ -8,6 +8,7 @@ use crate::{ clock::Clocks, gpio::{InputPin, OutputPin}, pac::i2c0::{RegisterBlock, COMD}, + peripheral::{Peripheral, PeripheralRef}, system::PeripheralClockControl, types::{InputSignal, OutputSignal}, }; @@ -152,11 +153,11 @@ enum Opcode { } /// I2C peripheral container (I2C) -pub struct I2C { - peripheral: T, +pub struct I2C<'d, T> { + peripheral: PeripheralRef<'d, T>, } -impl embedded_hal::blocking::i2c::Read for I2C +impl embedded_hal::blocking::i2c::Read for I2C<'_, T> where T: Instance, { @@ -167,7 +168,7 @@ where } } -impl embedded_hal::blocking::i2c::Write for I2C +impl embedded_hal::blocking::i2c::Write for I2C<'_, T> where T: Instance, { @@ -178,7 +179,7 @@ where } } -impl embedded_hal::blocking::i2c::WriteRead for I2C +impl embedded_hal::blocking::i2c::WriteRead for I2C<'_, T> where T: Instance, { @@ -195,12 +196,12 @@ where } #[cfg(feature = "eh1")] -impl embedded_hal_1::i2c::ErrorType for I2C { +impl embedded_hal_1::i2c::ErrorType for I2C<'_, T> { type Error = Error; } #[cfg(feature = "eh1")] -impl embedded_hal_1::i2c::I2c for I2C +impl embedded_hal_1::i2c::I2c for I2C<'_, T> where T: Instance, { @@ -256,7 +257,7 @@ where } } -impl I2C +impl<'d, T> I2C<'d, T> where T: Instance, { @@ -264,13 +265,14 @@ where /// This will enable the peripheral but the peripheral won't get /// automatically disabled when this gets dropped. pub fn new( - i2c: T, + i2c: impl Peripheral

+ 'd, mut sda: SDA, mut scl: SCL, frequency: HertzU32, peripheral_clock_control: &mut PeripheralClockControl, clocks: &Clocks, ) -> Self { + crate::into_ref!(i2c); enable_peripheral(&i2c, peripheral_clock_control); let mut i2c = I2C { peripheral: i2c }; @@ -292,14 +294,14 @@ where i2c } - - /// Return the raw interface to the underlying peripheral - pub fn free(self) -> T { - self.peripheral - } } -fn enable_peripheral(i2c: &T, peripheral_clock_control: &mut PeripheralClockControl) { +fn enable_peripheral<'d, T>( + i2c: &PeripheralRef<'d, T>, + peripheral_clock_control: &mut PeripheralClockControl, +) where + T: Instance, +{ // enable peripheral match i2c.i2c_number() { 0 => peripheral_clock_control.enable(crate::system::Peripheral::I2cExt0), @@ -1222,7 +1224,7 @@ fn write_fifo(register_block: &RegisterBlock, data: u8) { } } -impl Instance for crate::pac::I2C0 { +impl Instance for crate::peripherals::I2C0 { #[inline(always)] fn register_block(&self) -> &RegisterBlock { self @@ -1235,7 +1237,7 @@ impl Instance for crate::pac::I2C0 { } #[cfg(i2c1)] -impl Instance for crate::pac::I2C1 { +impl Instance for crate::peripherals::I2C1 { #[inline(always)] fn register_block(&self) -> &RegisterBlock { self diff --git a/esp-hal-common/src/peripherals/esp32.rs b/esp-hal-common/src/peripherals/esp32.rs index 704d90415c9..e7cf346bfe7 100644 --- a/esp-hal-common/src/peripherals/esp32.rs +++ b/esp-hal-common/src/peripherals/esp32.rs @@ -52,12 +52,14 @@ mod peripherals { pub use super::pac::*; crate::create_peripherals! { - UART0, - UART1, - UART2, + I2C0, + I2C1, SPI0, SPI1, SPI2, SPI3, + UART0, + UART1, + UART2, } } diff --git a/esp-hal-common/src/peripherals/esp32c2.rs b/esp-hal-common/src/peripherals/esp32c2.rs index b67329b2cd6..6c96fccab3d 100644 --- a/esp-hal-common/src/peripherals/esp32c2.rs +++ b/esp-hal-common/src/peripherals/esp32c2.rs @@ -34,11 +34,12 @@ mod peripherals { pub use super::pac::*; crate::create_peripherals! { - UART0, - UART1, + I2C0, SPI0, SPI1, SPI2, SYSTIMER, + UART0, + UART1, } } diff --git a/esp-hal-common/src/peripherals/esp32c3.rs b/esp-hal-common/src/peripherals/esp32c3.rs index 0d41129d242..4bbd50a8d8c 100644 --- a/esp-hal-common/src/peripherals/esp32c3.rs +++ b/esp-hal-common/src/peripherals/esp32c3.rs @@ -45,11 +45,12 @@ mod peripherals { pub use super::pac::*; crate::create_peripherals! { - UART0, - UART1, + I2C0, SPI0, SPI1, SPI2, SYSTIMER, + UART0, + UART1, } } diff --git a/esp-hal-common/src/peripherals/esp32s2.rs b/esp-hal-common/src/peripherals/esp32s2.rs index e4b224d0d06..3befeb7e410 100644 --- a/esp-hal-common/src/peripherals/esp32s2.rs +++ b/esp-hal-common/src/peripherals/esp32s2.rs @@ -50,13 +50,15 @@ mod peripherals { pub use super::pac::*; crate::create_peripherals! { - UART0, - UART1, + I2C0, + I2C1, SPI0, SPI1, SPI2, SPI3, SPI4, SYSTIMER, + UART0, + UART1, } } diff --git a/esp-hal-common/src/peripherals/esp32s3.rs b/esp-hal-common/src/peripherals/esp32s3.rs index 3c312ba6b3c..1b9e8e3c8d6 100644 --- a/esp-hal-common/src/peripherals/esp32s3.rs +++ b/esp-hal-common/src/peripherals/esp32s3.rs @@ -61,13 +61,15 @@ mod peripherals { pub use super::pac::*; crate::create_peripherals! { - UART0, - UART1, - UART2, + I2C0, + I2C1, SPI0, SPI1, SPI2, SPI3, SYSTIMER, + UART0, + UART1, + UART2, } }