Skip to content

Commit 0f69060

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 413420a commit 0f69060

File tree

5 files changed

+193
-50
lines changed

5 files changed

+193
-50
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/examples/i2c_async.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ async fn demo() {
109109
}
110110

111111
// Asynchronously write three bytes to the I²C device with 7-bit address 0x2C
112-
i2c.write(0x76u8, &[1, 2, 3]).await.unwrap();
112+
I2c::write(&mut i2c, 0x76u8, &[1, 2, 3]).await.unwrap();
113113

114114
// Demo finish - just loop until reset
115115
core::future::pending().await

rp2040-hal/examples/i2c_async_cancelled.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -128,12 +128,12 @@ async fn demo() {
128128

129129
// Asynchronously write three bytes to the I²C device with 7-bit address 0x2C
130130
futures::select_biased! {
131-
r = i2c.write(0x76u8, &v).fuse() => r.unwrap(),
131+
r = I2c::write(&mut i2c, 0x76u8, &v).fuse() => r.unwrap(),
132132
_ = timeout.fuse() => {
133133
defmt::info!("Timed out.");
134134
}
135135
}
136-
i2c.write(0x76u8, &v).await.unwrap();
136+
I2c::write(&mut i2c, 0x76u8, &v).await.unwrap();
137137

138138
// Demo finish - just loop until reset
139139
core::future::pending().await

rp2040-hal/src/i2c.rs

Lines changed: 21 additions & 18 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..=127u8 {
2524
//! let mut readbuf: [u8; 1] = [0; 1];
2625
//! let result = i2c.read(i, &mut readbuf);
@@ -30,14 +29,12 @@
3029
//! }
3130
//! }
3231
//!
33-
//! // Write some data to a device at 0x2c
34-
//! use embedded_hal_0_2::prelude::_embedded_hal_blocking_i2c_Write;
35-
//! i2c.write(0x2Cu8, &[1, 2, 3]).unwrap();
32+
//! // Write some data to a device with 7-bit address 0x2c
33+
//! i2c.write(0x2c_u8, &[1, 2, 3]).unwrap();
3634
//!
37-
//! // Write and then read from a device at 0x3a
38-
//! use embedded_hal_0_2::prelude::_embedded_hal_blocking_i2c_WriteRead;
35+
//! // Write and then read from a device with 7-bit address 0x3a
3936
//! let mut readbuf: [u8; 1] = [0; 1];
40-
//! i2c.write_read(0x2Cu8, &[1, 2, 3], &mut readbuf).unwrap();
37+
//! i2c.write_read(0x3a_u8, &[1, 2, 3], &mut readbuf).unwrap();
4138
//! ```
4239
//!
4340
//! See [examples/i2c.rs](https://github.com/rp-rs/rp-hal/tree/main/rp2040-hal/examples/i2c.rs)
@@ -92,6 +89,7 @@ pub trait ValidAddress:
9289
/// Validates the address against address ranges supported by the hardware.
9390
fn is_valid(self) -> Result<(), Error>;
9491
}
92+
9593
impl ValidAddress for u8 {
9694
const BIT_ADDR_M: IC_10BITADDR_MASTER_A = IC_10BITADDR_MASTER_A::ADDR_7BITS;
9795
const BIT_ADDR_S: IC_10BITADDR_SLAVE_A = IC_10BITADDR_SLAVE_A::ADDR_7BITS;
@@ -104,35 +102,40 @@ impl ValidAddress for u8 {
104102
}
105103
}
106104
}
105+
107106
impl ValidAddress for u16 {
108107
const BIT_ADDR_M: IC_10BITADDR_MASTER_A = IC_10BITADDR_MASTER_A::ADDR_10BITS;
109108
const BIT_ADDR_S: IC_10BITADDR_SLAVE_A = IC_10BITADDR_SLAVE_A::ADDR_10BITS;
110109

111110
fn is_valid(self) -> Result<(), Error> {
112-
Ok(())
111+
if self >= 0x400 {
112+
Err(Error::AddressOutOfRange(self.into()))
113+
} else {
114+
Ok(())
115+
}
113116
}
114117
}
115118

116-
/// I2C error
119+
/// I²C Error
117120
#[non_exhaustive]
118121
pub enum Error {
119-
/// I2C abort with error
122+
/// I²C abort with error
120123
Abort(u32),
121124
/// User passed in a read buffer that was 0 length
122125
///
123-
/// This is a limitation of the RP2040 I2C peripheral.
124-
/// If the slave ACKs its address, the I2C peripheral must read
126+
/// This is a limitation of the RP2040 I²C peripheral.
127+
/// If the slave ACKs its address, the I²C peripheral must read
125128
/// at least one byte before sending the STOP condition.
126129
InvalidReadBufferLength,
127130
/// User passed in a write buffer that was 0 length
128131
///
129-
/// This is a limitation of the RP2040 I2C peripheral.
130-
/// If the slave ACKs its address, the I2C peripheral must write
132+
/// This is a limitation of the RP2040 I²C peripheral.
133+
/// If the slave ACKs its address, the I²C peripheral must write
131134
/// at least one byte before sending the STOP condition.
132135
InvalidWriteBufferLength,
133-
/// Target i2c address is out of range
136+
/// Target I²C address is out of range
134137
AddressOutOfRange(u16),
135-
/// Target i2c address is reserved
138+
/// Target I²C address is reserved
136139
AddressReserved(u16),
137140
}
138141

@@ -142,8 +145,8 @@ impl core::fmt::Debug for Error {
142145
match self {
143146
Error::InvalidReadBufferLength => write!(fmt, "InvalidReadBufferLength"),
144147
Error::InvalidWriteBufferLength => write!(fmt, "InvalidWriteBufferLength"),
145-
Error::AddressOutOfRange(addr) => write!(fmt, "AddressOutOfRange({:x})", addr),
146-
Error::AddressReserved(addr) => write!(fmt, "AddressReserved({:x})", addr),
148+
Error::AddressOutOfRange(addr) => write!(fmt, "AddressOutOfRange({:?})", addr),
149+
Error::AddressReserved(addr) => write!(fmt, "AddressReserved({:?})", addr),
147150
Error::Abort(_) => {
148151
write!(fmt, "{:?}", self.kind())
149152
}

0 commit comments

Comments
 (0)