Skip to content

Commit

Permalink
Refactor I2C driver to use PeripheralRef
Browse files Browse the repository at this point in the history
  • Loading branch information
jessebraham committed Dec 13, 2022
1 parent c5dba25 commit 0c570c1
Showing 1 changed file with 19 additions and 17 deletions.
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

0 comments on commit 0c570c1

Please sign in to comment.