aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--embassy-traits/src/i2c.rs169
-rw-r--r--embassy-traits/src/lib.rs2
2 files changed, 171 insertions, 0 deletions
diff --git a/embassy-traits/src/i2c.rs b/embassy-traits/src/i2c.rs
new file mode 100644
index 000000000..4dc8865e3
--- /dev/null
+++ b/embassy-traits/src/i2c.rs
@@ -0,0 +1,169 @@
1//! Async I2C API
2//!
3//! This API supports 7-bit and 10-bit addresses. Traits feature an `AddressMode`
4//! marker type parameter. Two implementation of the `AddressMode` exist:
5//! `SevenBitAddress` and `TenBitAddress`.
6//!
7//! Through this marker types it is possible to implement each address mode for
8//! the traits independently in `embedded-hal` implementations and device drivers
9//! can depend only on the mode that they support.
10//!
11//! Additionally, the I2C 10-bit address mode has been developed to be fully
12//! backwards compatible with the 7-bit address mode. This allows for a
13//! software-emulated 10-bit addressing implementation if the address mode
14//! is not supported by the hardware.
15//!
16//! Since 7-bit addressing is the mode of the majority of I2C devices,
17//! `SevenBitAddress` has been set as default mode and thus can be omitted if desired.
18//!
19//! ### Device driver compatible only with 7-bit addresses
20//!
21//! For demonstration purposes the address mode parameter has been omitted in this example.
22//!
23//! ```
24//! # use embedded_hal::blocking::i2c::WriteRead;
25//! const ADDR: u8 = 0x15;
26//! # const TEMP_REGISTER: u8 = 0x1;
27//! pub struct TemperatureSensorDriver<I2C> {
28//! i2c: I2C,
29//! }
30//!
31//! impl<I2C, E> TemperatureSensorDriver<I2C>
32//! where
33//! I2C: WriteRead<Error = E>,
34//! {
35//! pub fn read_temperature(&mut self) -> Result<u8, E> {
36//! let mut temp = [0];
37//! self.i2c
38//! .write_read(ADDR, &[TEMP_REGISTER], &mut temp)
39//! .await
40//! .and(Ok(temp[0]))
41//! }
42//! }
43//! ```
44//!
45//! ### Device driver compatible only with 10-bit addresses
46//!
47//! ```
48//! # use embedded_hal::blocking::i2c::{TenBitAddress, WriteRead};
49//! const ADDR: u16 = 0x158;
50//! # const TEMP_REGISTER: u8 = 0x1;
51//! pub struct TemperatureSensorDriver<I2C> {
52//! i2c: I2C,
53//! }
54//!
55//! impl<I2C, E> TemperatureSensorDriver<I2C>
56//! where
57//! I2C: WriteRead<TenBitAddress, Error = E>,
58//! {
59//! pub fn read_temperature(&mut self) -> Result<u8, E> {
60//! let mut temp = [0];
61//! self.i2c
62//! .write_read(ADDR, &[TEMP_REGISTER], &mut temp)
63//! .await
64//! .and(Ok(temp[0]))
65//! }
66//! }
67//! ```
68
69use core::future::Future;
70use core::pin::Pin;
71
72mod private {
73 pub trait Sealed {}
74}
75
76/// Address mode (7-bit / 10-bit)
77///
78/// Note: This trait is sealed and should not be implemented outside of this crate.
79pub trait AddressMode: private::Sealed {}
80
81/// 7-bit address mode type
82pub type SevenBitAddress = u8;
83
84/// 10-bit address mode type
85pub type TenBitAddress = u16;
86
87impl private::Sealed for SevenBitAddress {}
88impl private::Sealed for TenBitAddress {}
89
90impl AddressMode for SevenBitAddress {}
91
92impl AddressMode for TenBitAddress {}
93
94/// Blocking read
95pub trait Read<A: AddressMode = SevenBitAddress> {
96 /// Error type
97 type Error;
98
99 type ReadFuture<'a>: Future<Output = Result<(), Self::Error>> + 'a;
100 type WriteFuture<'a>: Future<Output = Result<(), Self::Error>> + 'a;
101 type WriteReadFuture<'a>: Future<Output = Result<(), Self::Error>> + 'a;
102
103 /// Reads enough bytes from slave with `address` to fill `buffer`
104 ///
105 /// # I2C Events (contract)
106 ///
107 /// ``` text
108 /// Master: ST SAD+R MAK MAK ... NMAK SP
109 /// Slave: SAK B0 B1 ... BN
110 /// ```
111 ///
112 /// Where
113 ///
114 /// - `ST` = start condition
115 /// - `SAD+R` = slave address followed by bit 1 to indicate reading
116 /// - `SAK` = slave acknowledge
117 /// - `Bi` = ith byte of data
118 /// - `MAK` = master acknowledge
119 /// - `NMAK` = master no acknowledge
120 /// - `SP` = stop condition
121 fn read<'a>(self: Pin<&'a mut Self>, address: A, buffer: &mut [u8]) -> Self::ReadFuture<'a>;
122
123 /// Sends bytes to slave with address `address`
124 ///
125 /// # I2C Events (contract)
126 ///
127 /// ``` text
128 /// Master: ST SAD+W B0 B1 ... BN SP
129 /// Slave: SAK SAK SAK ... SAK
130 /// ```
131 ///
132 /// Where
133 ///
134 /// - `ST` = start condition
135 /// - `SAD+W` = slave address followed by bit 0 to indicate writing
136 /// - `SAK` = slave acknowledge
137 /// - `Bi` = ith byte of data
138 /// - `SP` = stop condition
139 fn write<'a>(self: Pin<&'a mut Self>, address: A, bytes: &[u8]) -> Self::WriteFuture<'a>;
140
141 /// Sends bytes to slave with address `address` and then reads enough bytes to fill `buffer` *in a
142 /// single transaction*
143 ///
144 /// # I2C Events (contract)
145 ///
146 /// ``` text
147 /// Master: ST SAD+W O0 O1 ... OM SR SAD+R MAK MAK ... NMAK SP
148 /// Slave: SAK SAK SAK ... SAK SAK I0 I1 ... IN
149 /// ```
150 ///
151 /// Where
152 ///
153 /// - `ST` = start condition
154 /// - `SAD+W` = slave address followed by bit 0 to indicate writing
155 /// - `SAK` = slave acknowledge
156 /// - `Oi` = ith outgoing byte of data
157 /// - `SR` = repeated start condition
158 /// - `SAD+R` = slave address followed by bit 1 to indicate reading
159 /// - `Ii` = ith incoming byte of data
160 /// - `MAK` = master acknowledge
161 /// - `NMAK` = master no acknowledge
162 /// - `SP` = stop condition
163 fn write_read<'a>(
164 self: Pin<&'a mut Self>,
165 address: A,
166 bytes: &[u8],
167 buffer: &mut [u8],
168 ) -> Self::WriteReadFuture<'a>;
169}
diff --git a/embassy-traits/src/lib.rs b/embassy-traits/src/lib.rs
index 849bbaec3..10d44d9de 100644
--- a/embassy-traits/src/lib.rs
+++ b/embassy-traits/src/lib.rs
@@ -4,8 +4,10 @@
4#![feature(const_fn_fn_ptr_basics)] 4#![feature(const_fn_fn_ptr_basics)]
5#![feature(const_option)] 5#![feature(const_option)]
6#![allow(incomplete_features)] 6#![allow(incomplete_features)]
7#![feature(type_alias_impl_trait)]
7 8
8pub mod delay; 9pub mod delay;
9pub mod flash; 10pub mod flash;
10pub mod gpio; 11pub mod gpio;
12pub mod i2c;
11pub mod uart; 13pub mod uart;