Skip to content

Implement i2c::TransactionIter trait #242

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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ as-slice = "0.2.1"
cast = { version = "0.3.0", default-features = false }
cortex-m = "0.7.0"
cortex-m-rt = "0.7.0"
embedded-hal = { version = "0.2.3", features = ["unproven"] }
embedded-hal = { version = "0.2.7", features = ["unproven"] }
embedded-time = "0.12.0"
nb = "1.0.0"
rtcc = { version = "0.3.0", optional = true }
Expand Down
23 changes: 22 additions & 1 deletion src/i2c.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ use core::{marker::PhantomData, ops::DerefMut, pin::Pin};

#[cfg(feature = "stm32l0x2")]
use as_slice::{AsMutSlice, AsSlice};
use embedded_hal::blocking::i2c::Operation;

#[cfg(feature = "stm32l0x2")]
use crate::dma::{self, Buffer};
Expand All @@ -19,7 +20,7 @@ use cast::u8;
use embedded_time::rate::Hertz;

// I²C traits
use crate::hal::blocking::i2c::{Read, Write, WriteRead};
use crate::hal::blocking::i2c::{Read, TransactionalIter, Write, WriteRead};

// I/O Imports
use crate::gpio::{AltMode, OpenDrain, Output};
Expand Down Expand Up @@ -455,6 +456,26 @@ where
}
}

impl<I, SDA, SCL> TransactionalIter for I2c<I, SDA, SCL>
where
I: Instance,
{
type Error = Error;

fn exec_iter<'a, O>(&mut self, address: u8, operations: O) -> Result<(), Self::Error>
where
O: IntoIterator<Item = Operation<'a>>,
{
for o in operations {
match o {
Operation::Read(bytes) => self.read(address, bytes)?,
Operation::Write(bytes) => self.write(address, bytes)?,
}
}
Ok(())
}
}

pub trait Instance: Deref<Target = RegisterBlock> + Enable + Reset {
fn ptr() -> *const RegisterBlock;
}
Expand Down
Loading