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

Peripheral ref/i2c #303

Merged
merged 2 commits into from
Dec 13, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 19 additions & 17 deletions esp-hal-common/src/i2c.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ use crate::{
clock::Clocks,
gpio::{InputPin, OutputPin},
pac::i2c0::{RegisterBlock, COMD},
peripheral::{Peripheral, PeripheralRef},
system::PeripheralClockControl,
types::{InputSignal, OutputSignal},
};
Expand Down Expand Up @@ -152,11 +153,11 @@ enum Opcode {
}

/// I2C peripheral container (I2C)
pub struct I2C<T> {
peripheral: T,
pub struct I2C<'d, T> {
peripheral: PeripheralRef<'d, T>,
}

impl<T> embedded_hal::blocking::i2c::Read for I2C<T>
impl<T> embedded_hal::blocking::i2c::Read for I2C<'_, T>
where
T: Instance,
{
Expand All @@ -167,7 +168,7 @@ where
}
}

impl<T> embedded_hal::blocking::i2c::Write for I2C<T>
impl<T> embedded_hal::blocking::i2c::Write for I2C<'_, T>
where
T: Instance,
{
Expand All @@ -178,7 +179,7 @@ where
}
}

impl<T> embedded_hal::blocking::i2c::WriteRead for I2C<T>
impl<T> embedded_hal::blocking::i2c::WriteRead for I2C<'_, T>
where
T: Instance,
{
Expand All @@ -195,12 +196,12 @@ where
}

#[cfg(feature = "eh1")]
impl<T> embedded_hal_1::i2c::ErrorType for I2C<T> {
impl<T> embedded_hal_1::i2c::ErrorType for I2C<'_, T> {
type Error = Error;
}

#[cfg(feature = "eh1")]
impl<T> embedded_hal_1::i2c::I2c for I2C<T>
impl<T> embedded_hal_1::i2c::I2c for I2C<'_, T>
where
T: Instance,
{
Expand Down Expand Up @@ -256,21 +257,22 @@ where
}
}

impl<T> I2C<T>
impl<'d, T> I2C<'d, T>
where
T: Instance,
{
/// 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<SDA: OutputPin + InputPin, SCL: OutputPin + InputPin>(
i2c: T,
i2c: impl Peripheral<P = T> + '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 };
Expand All @@ -292,14 +294,14 @@ where

i2c
}

/// Return the raw interface to the underlying peripheral
pub fn free(self) -> T {
self.peripheral
}
}

fn enable_peripheral<T: Instance>(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),
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down
8 changes: 5 additions & 3 deletions esp-hal-common/src/peripherals/esp32.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
}
}
5 changes: 3 additions & 2 deletions esp-hal-common/src/peripherals/esp32c2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,12 @@ mod peripherals {
pub use super::pac::*;

crate::create_peripherals! {
UART0,
UART1,
I2C0,
SPI0,
SPI1,
SPI2,
SYSTIMER,
UART0,
UART1,
}
}
5 changes: 3 additions & 2 deletions esp-hal-common/src/peripherals/esp32c3.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,11 +45,12 @@ mod peripherals {
pub use super::pac::*;

crate::create_peripherals! {
UART0,
UART1,
I2C0,
SPI0,
SPI1,
SPI2,
SYSTIMER,
UART0,
UART1,
}
}
6 changes: 4 additions & 2 deletions esp-hal-common/src/peripherals/esp32s2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
}
}
8 changes: 5 additions & 3 deletions esp-hal-common/src/peripherals/esp32s3.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
}
}