Skip to content

Commit

Permalink
impl embedded_hal_1::i2c::I2c for I2c
Browse files Browse the repository at this point in the history
  • Loading branch information
dimpolo committed Mar 26, 2024
1 parent 72b0bea commit ff1e01f
Show file tree
Hide file tree
Showing 3 changed files with 95 additions and 24 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
- PWM complementary output capability for TIM1 with new example to demonstrate
- Implement interface for reading and writing to the internal flash memory and an example for demonstration.
- PWM output on complementary channels only for single channel timers (TIM16 + TIM17)
- impl embedded_hal_1::i2c::I2c for I2c

### Fixed

Expand Down
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ bare-metal = { version = "1.0.0" }
cast = "0.3"
cortex-m = "0.7"
embedded-hal = { version = "0.2", features = ["unproven"] }
embedded-hal-1 = { package = "embedded-hal", version = "1.0" }
stm32f0 = "0.14"
nb = "1"
void = { version = "1.0", default-features = false }
Expand Down
117 changes: 93 additions & 24 deletions src/i2c.rs
Original file line number Diff line number Diff line change
Expand Up @@ -313,15 +313,8 @@ where
let value = self.i2c.rxdr.read().bits() as u8;
Ok(value)
}
}

impl<I2C, SCLPIN, SDAPIN> WriteRead for I2c<I2C, SCLPIN, SDAPIN>
where
I2C: Deref<Target = I2cRegisterBlock>,
{
type Error = Error;

fn write_read(&mut self, addr: u8, bytes: &[u8], buffer: &mut [u8]) -> Result<(), Error> {
fn write_read_impl(&mut self, addr: u8, bytes: &[u8], buffer: &mut [u8]) -> Result<(), Error> {
// Set up current slave address for writing and disable autoending
self.i2c.cr2.modify(|_, w| {
w.sadd()
Expand Down Expand Up @@ -386,15 +379,8 @@ where

Ok(())
}
}

impl<I2C, SCLPIN, SDAPIN> Read for I2c<I2C, SCLPIN, SDAPIN>
where
I2C: Deref<Target = I2cRegisterBlock>,
{
type Error = Error;

fn read(&mut self, addr: u8, buffer: &mut [u8]) -> Result<(), Error> {
fn read_impl(&mut self, addr: u8, buffer: &mut [u8]) -> Result<(), Error> {
// Set up current address for reading
self.i2c.cr2.modify(|_, w| {
w.sadd()
Expand All @@ -421,15 +407,8 @@ where

Ok(())
}
}

impl<I2C, SCLPIN, SDAPIN> Write for I2c<I2C, SCLPIN, SDAPIN>
where
I2C: Deref<Target = I2cRegisterBlock>,
{
type Error = Error;

fn write(&mut self, addr: u8, bytes: &[u8]) -> Result<(), Error> {
fn write_impl(&mut self, addr: u8, bytes: &[u8]) -> Result<(), Error> {
// Set up current slave address for writing and enable autoending
self.i2c.cr2.modify(|_, w| {
w.sadd()
Expand All @@ -456,3 +435,93 @@ where
Ok(())
}
}

impl<I2C, SCLPIN, SDAPIN> WriteRead for I2c<I2C, SCLPIN, SDAPIN>
where
I2C: Deref<Target = I2cRegisterBlock>,
{
type Error = Error;

fn write_read(&mut self, addr: u8, bytes: &[u8], buffer: &mut [u8]) -> Result<(), Error> {
self.write_read_impl(addr, bytes, buffer)
}
}

impl<I2C, SCLPIN, SDAPIN> Read for I2c<I2C, SCLPIN, SDAPIN>
where
I2C: Deref<Target = I2cRegisterBlock>,
{
type Error = Error;

fn read(&mut self, addr: u8, buffer: &mut [u8]) -> Result<(), Error> {
self.read_impl(addr, buffer)
}
}

impl<I2C, SCLPIN, SDAPIN> Write for I2c<I2C, SCLPIN, SDAPIN>
where
I2C: Deref<Target = I2cRegisterBlock>,
{
type Error = Error;

fn write(&mut self, addr: u8, bytes: &[u8]) -> Result<(), Error> {
self.write_impl(addr, bytes)
}
}

impl<I2C, SCLPIN, SDAPIN> embedded_hal_1::i2c::I2c for I2c<I2C, SCLPIN, SDAPIN>
where
I2C: Deref<Target = I2cRegisterBlock>,
{
fn read(
&mut self,
address: embedded_hal_1::i2c::SevenBitAddress,
read: &mut [u8],
) -> Result<(), Self::Error> {
self.read_impl(address, read)
}

fn write(
&mut self,
address: embedded_hal_1::i2c::SevenBitAddress,
write: &[u8],
) -> Result<(), Self::Error> {
self.write_impl(address, write)
}

fn write_read(
&mut self,
address: embedded_hal_1::i2c::SevenBitAddress,
write: &[u8],
read: &mut [u8],
) -> Result<(), Self::Error> {
self.write_read_impl(address, write, read)
}

fn transaction(
&mut self,
_address: embedded_hal_1::i2c::SevenBitAddress,
_operations: &mut [embedded_hal_1::i2c::Operation<'_>],
) -> Result<(), Self::Error> {
todo!()
}
}

impl<I2C, SCLPIN, SDAPIN> embedded_hal_1::i2c::ErrorType for I2c<I2C, SCLPIN, SDAPIN>
where
I2C: Deref<Target = I2cRegisterBlock>,
{
type Error = Error;
}

impl embedded_hal_1::i2c::Error for Error {
fn kind(&self) -> embedded_hal_1::i2c::ErrorKind {
match self {
Error::OVERRUN => embedded_hal_1::i2c::ErrorKind::Overrun,
Error::NACK => embedded_hal_1::i2c::ErrorKind::NoAcknowledge(
embedded_hal_1::i2c::NoAcknowledgeSource::Unknown,
),
Error::BUS => embedded_hal_1::i2c::ErrorKind::Bus,
}
}
}

0 comments on commit ff1e01f

Please sign in to comment.