Skip to content

Commit d5ad656

Browse files
committed
Add I2C address modes
1 parent f88ddc5 commit d5ad656

File tree

2 files changed

+42
-10
lines changed

2 files changed

+42
-10
lines changed

src/blocking/i2c.rs

+34-10
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,32 @@
55
//! Operations on 10-bit slave addresses are not supported by the API yet (but applications might
66
//! be able to emulate some operations).
77
8+
use crate::private;
9+
10+
/// Address mode (7-bit / 10-bit)
11+
///
12+
/// Note: This trait is sealed and should not be implemented outside of this crate.
13+
pub trait AddressMode: private::Sealed {
14+
/// Address value type
15+
type Address;
16+
}
17+
18+
/// 7-bit address mode type
19+
pub struct SevenBitAddress(());
20+
21+
/// 10-bit address mode type
22+
pub struct TenBitAddress(());
23+
24+
impl AddressMode for SevenBitAddress {
25+
type Address = u8;
26+
}
27+
28+
impl AddressMode for TenBitAddress {
29+
type Address = u16;
30+
}
31+
832
/// Blocking read
9-
pub trait Read {
33+
pub trait Read<A: AddressMode> {
1034
/// Error type
1135
type Error;
1236

@@ -28,11 +52,11 @@ pub trait Read {
2852
/// - `MAK` = master acknowledge
2953
/// - `NMAK` = master no acknowledge
3054
/// - `SP` = stop condition
31-
fn try_read(&mut self, address: u8, buffer: &mut [u8]) -> Result<(), Self::Error>;
55+
fn try_read(&mut self, address: A::Address, buffer: &mut [u8]) -> Result<(), Self::Error>;
3256
}
3357

3458
/// Blocking write
35-
pub trait Write {
59+
pub trait Write<A: AddressMode> {
3660
/// Error type
3761
type Error;
3862

@@ -52,11 +76,11 @@ pub trait Write {
5276
/// - `SAK` = slave acknowledge
5377
/// - `Bi` = ith byte of data
5478
/// - `SP` = stop condition
55-
fn try_write(&mut self, address: u8, bytes: &[u8]) -> Result<(), Self::Error>;
79+
fn try_write(&mut self, address: A::Address, bytes: &[u8]) -> Result<(), Self::Error>;
5680
}
5781

5882
/// Blocking write (iterator version)
59-
pub trait WriteIter {
83+
pub trait WriteIter<A: AddressMode> {
6084
/// Error type
6185
type Error;
6286

@@ -65,13 +89,13 @@ pub trait WriteIter {
6589
/// # I2C Events (contract)
6690
///
6791
/// Same as `Write`
68-
fn try_write<B>(&mut self, address: u8, bytes: B) -> Result<(), Self::Error>
92+
fn try_write<B>(&mut self, address: A::Address, bytes: B) -> Result<(), Self::Error>
6993
where
7094
B: IntoIterator<Item = u8>;
7195
}
7296

7397
/// Blocking write + read
74-
pub trait WriteRead {
98+
pub trait WriteRead<A: AddressMode> {
7599
/// Error type
76100
type Error;
77101

@@ -99,14 +123,14 @@ pub trait WriteRead {
99123
/// - `SP` = stop condition
100124
fn try_write_read(
101125
&mut self,
102-
address: u8,
126+
address: A::Address,
103127
bytes: &[u8],
104128
buffer: &mut [u8],
105129
) -> Result<(), Self::Error>;
106130
}
107131

108132
/// Blocking write (iterator version) + read
109-
pub trait WriteIterRead {
133+
pub trait WriteIterRead<A: AddressMode> {
110134
/// Error type
111135
type Error;
112136

@@ -118,7 +142,7 @@ pub trait WriteIterRead {
118142
/// Same as the `WriteRead` trait
119143
fn try_write_iter_read<B>(
120144
&mut self,
121-
address: u8,
145+
address: A::Address,
122146
bytes: B,
123147
buffer: &mut [u8],
124148
) -> Result<(), Self::Error>

src/lib.rs

+8
Original file line numberDiff line numberDiff line change
@@ -700,3 +700,11 @@ pub mod serial;
700700
pub mod spi;
701701
pub mod timer;
702702
pub mod watchdog;
703+
704+
mod private {
705+
use crate::blocking::i2c::{SevenBitAddress, TenBitAddress};
706+
pub trait Sealed {}
707+
708+
impl Sealed for SevenBitAddress {}
709+
impl Sealed for TenBitAddress {}
710+
}

0 commit comments

Comments
 (0)