Skip to content

Commit ea695d5

Browse files
committed
Implement i2c traits of embedded_hal
1 parent 65a34da commit ea695d5

File tree

4 files changed

+53
-0
lines changed

4 files changed

+53
-0
lines changed

Cargo.lock

Lines changed: 23 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ bitflags = "1.0.3"
1515
volatile = "0.2.4"
1616
bit_field = "0.9.0"
1717
bare-metal = "0.2.3"
18+
embedded-hal = "0.2.1"
1819

1920
[dependencies.stm32f7]
2021
version = "0.3.2"

src/i2c.rs

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ use stm32f7::stm32f7x6::{
44
i2c1::{self, RegisterBlock},
55
RCC,
66
};
7+
use embedded_hal;
78

89
// TODO use &mut when svd2rust API has changed (modification should require &mut)
910
//pub struct I2C<'a>(&'a mut RegisterBlock);
@@ -355,6 +356,33 @@ impl<'a> I2C<'a> {
355356
}
356357
}
357358

359+
impl <'a> embedded_hal::blocking::i2c::Read for I2C<'a> {
360+
type Error = Error;
361+
362+
fn read(&mut self, address: u8, buffer: &mut [u8]) -> Result<(), Self::Error> {
363+
self.connect(Address::bits_7(address), |mut connection: I2cConnection<u8>| connection.read_bytes_raw(buffer.iter_mut()))
364+
}
365+
}
366+
367+
impl <'a> embedded_hal::blocking::i2c::Write for I2C<'a> {
368+
type Error = Error;
369+
370+
fn write(&mut self, address: u8, bytes: &[u8]) -> Result<(), Self::Error> {
371+
self.connect(Address::bits_7(address), |mut connection: I2cConnection<u8>| connection.write_bytes(bytes.iter().map(|b| *b)))
372+
}
373+
}
374+
375+
impl <'a> embedded_hal::blocking::i2c::WriteRead for I2C<'a> {
376+
type Error = Error;
377+
378+
fn write_read(&mut self, address: u8, bytes: &[u8], buffer: &mut [u8]) -> Result<(), Self::Error> {
379+
self.connect(Address::bits_7(address), |mut connection: I2cConnection<u8>| {
380+
connection.write_bytes(bytes.iter().map(|b| *b))?;
381+
connection.read_bytes_raw(buffer.iter_mut())
382+
})
383+
}
384+
}
385+
358386
pub fn init<'a>(i2c: &'a RegisterBlock, rcc: &mut RCC) -> I2C<'a> {
359387
// enable clocks
360388
rcc.apb1enr.modify(|_, w| w.i2c3en().enabled());

src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ extern crate byteorder;
1919
extern crate cortex_m_rt as rt;
2020
extern crate smoltcp;
2121
extern crate volatile;
22+
extern crate embedded_hal;
2223

2324
pub mod ethernet;
2425
pub mod gpio;

0 commit comments

Comments
 (0)