Browse Source

Add I2C Read implementation (#88)

* Add read implementation
* Add changelog
trying.tmp
James Munns 3 years ago committed by GitHub
parent
commit
b15fc1532f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 1
      CHANGELOG.md
  2. 37
      src/i2c.rs

1
CHANGELOG.md

@ -12,6 +12,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
- Added "bypass" parameter to Rcc HSE configuration (breaking change)
- Add "usbsrc" function to Rcc configuration, used for selecting USB clock source
- For STM32F030, require use more specific feature flag, e.g. "stm32f030xc"
- Add `embedded-hal` `blocking::i2c::Read` implementation for I2C
### Fixed
- RCC: Correct code to enable PLL.

37
src/i2c.rs

@ -1,6 +1,6 @@
use core::ops::Deref;
use embedded_hal::blocking::i2c::{Write, WriteRead};
use embedded_hal::blocking::i2c::{Write, WriteRead, Read};
use crate::{
gpio::*,
@ -365,6 +365,41 @@ where
}
}
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> {
// Set up current address for reading
self.i2c.cr2.modify(|_, w| {
w.sadd()
.bits(u16::from(addr) << 1)
.nbytes()
.bits(buffer.len() as u8)
.rd_wrn()
.set_bit()
});
// Send a START condition
self.i2c.cr2.modify(|_, w| w.start().set_bit());
// Send the autoend after setting the start to get a restart
self.i2c.cr2.modify(|_, w| w.autoend().set_bit());
// Now read in all bytes
for c in buffer.iter_mut() {
*c = self.recv_byte()?;
}
// Check and clear flags if they somehow ended up set
self.check_and_clear_error_flags(&self.i2c.isr.read())?;
Ok(())
}
}
impl<I2C, SCLPIN, SDAPIN> Write for I2c<I2C, SCLPIN, SDAPIN>
where
I2C: Deref<Target = I2cRegisterBlock>,

Loading…
Cancel
Save