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);
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+
9593impl 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+
107106impl 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]
118121pub 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