Skip to content

Commit

Permalink
Clean up the I2C impls.
Browse files Browse the repository at this point in the history
Also we have inherent methods, so you can ignore the Embedded HAL traits if you want (makes the examples simpler too).

Whilst doing this I found out we only support 7-bit I2C addresses. I'll come back and fix that later.
  • Loading branch information
jonathanpallant committed Jan 19, 2024
1 parent 1481429 commit 70a2fa2
Show file tree
Hide file tree
Showing 3 changed files with 129 additions and 79 deletions.
3 changes: 1 addition & 2 deletions rp2040-hal/examples/i2c.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ use panic_halt as _;
use rp2040_hal as hal;

// Some traits we need
use embedded_hal_0_2::blocking::i2c::Write;
use hal::fugit::RateExtU32;

// A shorter alias for the Peripheral Access Crate, which provides low-level
Expand Down Expand Up @@ -81,7 +80,7 @@ fn main() -> ! {
let scl_pin: Pin<_, FunctionI2C, _> = pins.gpio19.reconfigure();
// let not_an_scl_pin: Pin<_, FunctionI2C, PullUp> = pins.gpio20.reconfigure();

// Create the I²C drive, using the two pre-configured pins. This will fail
// Create the I²C driver, using the two pre-configured pins. This will fail
// at compile time if the pins are in the wrong mode, or if this I²C
// peripheral isn't available on these pins!
let mut i2c = hal::I2C::i2c1(
Expand Down
23 changes: 10 additions & 13 deletions rp2040-hal/src/i2c.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
//! );
//!
//! // Scan for devices on the bus by attempting to read from them
//! use embedded_hal_0_2::prelude::_embedded_hal_blocking_i2c_Read;
//! for i in 0..=127 {
//! let mut readbuf: [u8; 1] = [0; 1];
//! let result = i2c.read(i, &mut readbuf);
Expand All @@ -31,11 +30,9 @@
//! }
//!
//! // Write some data to a device at 0x2c
//! use embedded_hal_0_2::prelude::_embedded_hal_blocking_i2c_Write;
//! i2c.write(0x2c, &[1, 2, 3]).unwrap();
//!
//! // Write and then read from a device at 0x3a
//! use embedded_hal_0_2::prelude::_embedded_hal_blocking_i2c_WriteRead;
//! let mut readbuf: [u8; 1] = [0; 1];
//! i2c.write_read(0x2c, &[1, 2, 3], &mut readbuf).unwrap();
//! ```
Expand Down Expand Up @@ -70,26 +67,26 @@ impl I2cDevice for pac::I2C1 {
const ID: usize = 1;
}

/// I2C error
/// I²C Error
#[non_exhaustive]
pub enum Error {
/// I2C abort with error
/// I²C abort with error
Abort(u32),
/// User passed in a read buffer that was 0 length
///
/// This is a limitation of the RP2040 I2C peripheral.
/// If the slave ACKs its address, the I2C peripheral must read
/// This is a limitation of the RP2040 I²C peripheral.
/// If the slave ACKs its address, the I²C peripheral must read
/// at least one byte before sending the STOP condition.
InvalidReadBufferLength,
/// User passed in a write buffer that was 0 length
///
/// This is a limitation of the RP2040 I2C peripheral.
/// If the slave ACKs its address, the I2C peripheral must write
/// This is a limitation of the RP2040 I²C peripheral.
/// If the slave ACKs its address, the I²C peripheral must write
/// at least one byte before sending the STOP condition.
InvalidWriteBufferLength,
/// Target i2c address is out of range
/// Target I²C address is out of range
AddressOutOfRange(u16),
/// Target i2c address is reserved
/// Target I²C address is reserved
AddressReserved(u16),
}

Expand All @@ -99,8 +96,8 @@ impl core::fmt::Debug for Error {
match self {
Error::InvalidReadBufferLength => write!(fmt, "InvalidReadBufferLength"),
Error::InvalidWriteBufferLength => write!(fmt, "InvalidWriteBufferLength"),
Error::AddressOutOfRange(addr) => write!(fmt, "AddressOutOfRange({:x})", addr),
Error::AddressReserved(addr) => write!(fmt, "AddressReserved({:x})", addr),
Error::AddressOutOfRange(addr) => write!(fmt, "AddressOutOfRange({:?})", addr),
Error::AddressReserved(addr) => write!(fmt, "AddressReserved({:?})", addr),
Error::Abort(_) => {
write!(fmt, "{:?}", self.kind())
}
Expand Down
Loading

0 comments on commit 70a2fa2

Please sign in to comment.