Skip to content

Commit f795258

Browse files
committed
impl embedded_hal_1::i2c::I2c for I2c
1 parent b2e2e9d commit f795258

File tree

3 files changed

+95
-24
lines changed

3 files changed

+95
-24
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
1818
- PWM complementary output capability for TIM1 with new example to demonstrate
1919
- Implement interface for reading and writing to the internal flash memory and an example for demonstration.
2020
- PWM output on complementary channels only for single channel timers (TIM16 + TIM17)
21+
- impl embedded_hal_1::i2c::I2c for I2c
2122

2223
### Fixed
2324

Cargo.toml

+1
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ bare-metal = { version = "1.0.0" }
3434
cast = "0.3"
3535
cortex-m = "0.7"
3636
embedded-hal = { version = "0.2", features = ["unproven"] }
37+
embedded-hal-1 = { package = "embedded-hal", version = "1.0" }
3738
stm32f0 = "0.14"
3839
nb = "1"
3940
void = { version = "1.0", default-features = false }

src/i2c.rs

+93-24
Original file line numberDiff line numberDiff line change
@@ -313,15 +313,8 @@ where
313313
let value = self.i2c.rxdr.read().bits() as u8;
314314
Ok(value)
315315
}
316-
}
317-
318-
impl<I2C, SCLPIN, SDAPIN> WriteRead for I2c<I2C, SCLPIN, SDAPIN>
319-
where
320-
I2C: Deref<Target = I2cRegisterBlock>,
321-
{
322-
type Error = Error;
323316

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

387380
Ok(())
388381
}
389-
}
390382

391-
impl<I2C, SCLPIN, SDAPIN> Read for I2c<I2C, SCLPIN, SDAPIN>
392-
where
393-
I2C: Deref<Target = I2cRegisterBlock>,
394-
{
395-
type Error = Error;
396-
397-
fn read(&mut self, addr: u8, buffer: &mut [u8]) -> Result<(), Error> {
383+
fn read_impl(&mut self, addr: u8, buffer: &mut [u8]) -> Result<(), Error> {
398384
// Set up current address for reading
399385
self.i2c.cr2.modify(|_, w| {
400386
w.sadd()
@@ -421,15 +407,8 @@ where
421407

422408
Ok(())
423409
}
424-
}
425-
426-
impl<I2C, SCLPIN, SDAPIN> Write for I2c<I2C, SCLPIN, SDAPIN>
427-
where
428-
I2C: Deref<Target = I2cRegisterBlock>,
429-
{
430-
type Error = Error;
431410

432-
fn write(&mut self, addr: u8, bytes: &[u8]) -> Result<(), Error> {
411+
fn write_impl(&mut self, addr: u8, bytes: &[u8]) -> Result<(), Error> {
433412
// Set up current slave address for writing and enable autoending
434413
self.i2c.cr2.modify(|_, w| {
435414
w.sadd()
@@ -456,3 +435,93 @@ where
456435
Ok(())
457436
}
458437
}
438+
439+
impl<I2C, SCLPIN, SDAPIN> WriteRead for I2c<I2C, SCLPIN, SDAPIN>
440+
where
441+
I2C: Deref<Target = I2cRegisterBlock>,
442+
{
443+
type Error = Error;
444+
445+
fn write_read(&mut self, addr: u8, bytes: &[u8], buffer: &mut [u8]) -> Result<(), Error> {
446+
self.write_read_impl(addr, bytes, buffer)
447+
}
448+
}
449+
450+
impl<I2C, SCLPIN, SDAPIN> Read for I2c<I2C, SCLPIN, SDAPIN>
451+
where
452+
I2C: Deref<Target = I2cRegisterBlock>,
453+
{
454+
type Error = Error;
455+
456+
fn read(&mut self, addr: u8, buffer: &mut [u8]) -> Result<(), Error> {
457+
self.read_impl(addr, buffer)
458+
}
459+
}
460+
461+
impl<I2C, SCLPIN, SDAPIN> Write for I2c<I2C, SCLPIN, SDAPIN>
462+
where
463+
I2C: Deref<Target = I2cRegisterBlock>,
464+
{
465+
type Error = Error;
466+
467+
fn write(&mut self, addr: u8, bytes: &[u8]) -> Result<(), Error> {
468+
self.write_impl(addr, bytes)
469+
}
470+
}
471+
472+
impl<I2C, SCLPIN, SDAPIN> embedded_hal_1::i2c::I2c for I2c<I2C, SCLPIN, SDAPIN>
473+
where
474+
I2C: Deref<Target = I2cRegisterBlock>,
475+
{
476+
fn read(
477+
&mut self,
478+
address: embedded_hal_1::i2c::SevenBitAddress,
479+
read: &mut [u8],
480+
) -> Result<(), Self::Error> {
481+
self.read_impl(address, read)
482+
}
483+
484+
fn write(
485+
&mut self,
486+
address: embedded_hal_1::i2c::SevenBitAddress,
487+
write: &[u8],
488+
) -> Result<(), Self::Error> {
489+
self.write_impl(address, write)
490+
}
491+
492+
fn write_read(
493+
&mut self,
494+
address: embedded_hal_1::i2c::SevenBitAddress,
495+
write: &[u8],
496+
read: &mut [u8],
497+
) -> Result<(), Self::Error> {
498+
self.write_read_impl(address, write, read)
499+
}
500+
501+
fn transaction(
502+
&mut self,
503+
_address: embedded_hal_1::i2c::SevenBitAddress,
504+
_operations: &mut [embedded_hal_1::i2c::Operation<'_>],
505+
) -> Result<(), Self::Error> {
506+
todo!()
507+
}
508+
}
509+
510+
impl<I2C, SCLPIN, SDAPIN> embedded_hal_1::i2c::ErrorType for I2c<I2C, SCLPIN, SDAPIN>
511+
where
512+
I2C: Deref<Target = I2cRegisterBlock>,
513+
{
514+
type Error = Error;
515+
}
516+
517+
impl embedded_hal_1::i2c::Error for Error {
518+
fn kind(&self) -> embedded_hal_1::i2c::ErrorKind {
519+
match self {
520+
Error::OVERRUN => embedded_hal_1::i2c::ErrorKind::Overrun,
521+
Error::NACK => embedded_hal_1::i2c::ErrorKind::NoAcknowledge(
522+
embedded_hal_1::i2c::NoAcknowledgeSource::Unknown,
523+
),
524+
Error::BUS => embedded_hal_1::i2c::ErrorKind::Bus,
525+
}
526+
}
527+
}

0 commit comments

Comments
 (0)