Skip to content

Commit 70a2fa2

Browse files
Clean up the I2C impls.
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.
1 parent 1481429 commit 70a2fa2

File tree

3 files changed

+129
-79
lines changed

3 files changed

+129
-79
lines changed

rp2040-hal/examples/i2c.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ use panic_halt as _;
1717
use rp2040_hal as hal;
1818

1919
// Some traits we need
20-
use embedded_hal_0_2::blocking::i2c::Write;
2120
use hal::fugit::RateExtU32;
2221

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

84-
// Create the I²C drive, using the two pre-configured pins. This will fail
83+
// Create the I²C driver, using the two pre-configured pins. This will fail
8584
// at compile time if the pins are in the wrong mode, or if this I²C
8685
// peripheral isn't available on these pins!
8786
let mut i2c = hal::I2C::i2c1(

rp2040-hal/src/i2c.rs

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@
2020
//! );
2121
//!
2222
//! // Scan for devices on the bus by attempting to read from them
23-
//! use embedded_hal_0_2::prelude::_embedded_hal_blocking_i2c_Read;
2423
//! for i in 0..=127 {
2524
//! let mut readbuf: [u8; 1] = [0; 1];
2625
//! let result = i2c.read(i, &mut readbuf);
@@ -31,11 +30,9 @@
3130
//! }
3231
//!
3332
//! // Write some data to a device at 0x2c
34-
//! use embedded_hal_0_2::prelude::_embedded_hal_blocking_i2c_Write;
3533
//! i2c.write(0x2c, &[1, 2, 3]).unwrap();
3634
//!
3735
//! // Write and then read from a device at 0x3a
38-
//! use embedded_hal_0_2::prelude::_embedded_hal_blocking_i2c_WriteRead;
3936
//! let mut readbuf: [u8; 1] = [0; 1];
4037
//! i2c.write_read(0x2c, &[1, 2, 3], &mut readbuf).unwrap();
4138
//! ```
@@ -70,26 +67,26 @@ impl I2cDevice for pac::I2C1 {
7067
const ID: usize = 1;
7168
}
7269

73-
/// I2C error
70+
/// I²C Error
7471
#[non_exhaustive]
7572
pub enum Error {
76-
/// I2C abort with error
73+
/// I²C abort with error
7774
Abort(u32),
7875
/// User passed in a read buffer that was 0 length
7976
///
80-
/// This is a limitation of the RP2040 I2C peripheral.
81-
/// If the slave ACKs its address, the I2C peripheral must read
77+
/// This is a limitation of the RP2040 I²C peripheral.
78+
/// If the slave ACKs its address, the I²C peripheral must read
8279
/// at least one byte before sending the STOP condition.
8380
InvalidReadBufferLength,
8481
/// User passed in a write buffer that was 0 length
8582
///
86-
/// This is a limitation of the RP2040 I2C peripheral.
87-
/// If the slave ACKs its address, the I2C peripheral must write
83+
/// This is a limitation of the RP2040 I²C peripheral.
84+
/// If the slave ACKs its address, the I²C peripheral must write
8885
/// at least one byte before sending the STOP condition.
8986
InvalidWriteBufferLength,
90-
/// Target i2c address is out of range
87+
/// Target I²C address is out of range
9188
AddressOutOfRange(u16),
92-
/// Target i2c address is reserved
89+
/// Target I²C address is reserved
9390
AddressReserved(u16),
9491
}
9592

@@ -99,8 +96,8 @@ impl core::fmt::Debug for Error {
9996
match self {
10097
Error::InvalidReadBufferLength => write!(fmt, "InvalidReadBufferLength"),
10198
Error::InvalidWriteBufferLength => write!(fmt, "InvalidWriteBufferLength"),
102-
Error::AddressOutOfRange(addr) => write!(fmt, "AddressOutOfRange({:x})", addr),
103-
Error::AddressReserved(addr) => write!(fmt, "AddressReserved({:x})", addr),
99+
Error::AddressOutOfRange(addr) => write!(fmt, "AddressOutOfRange({:?})", addr),
100+
Error::AddressReserved(addr) => write!(fmt, "AddressReserved({:?})", addr),
104101
Error::Abort(_) => {
105102
write!(fmt, "{:?}", self.kind())
106103
}

0 commit comments

Comments
 (0)