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

impl embedded_hal_1::i2c::I2c for I2c #176

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
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!()
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can’t let that as is. It must be implemented.

}
}

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,
}
}
}
Loading