diff options
| author | xoviat <[email protected]> | 2021-03-03 09:29:18 -0600 |
|---|---|---|
| committer | xoviat <[email protected]> | 2021-03-03 09:29:18 -0600 |
| commit | a3a36517bd8beae30b4d9ca23b3da8db1a14de12 (patch) | |
| tree | d488111b83d347e452b7f10f9f92484257506ab7 | |
| parent | 492f7aeea6c309a0a8e60fb2028278fb906164cb (diff) | |
update i2c trait
| -rw-r--r-- | embassy-traits/src/i2c.rs | 54 |
1 files changed, 15 insertions, 39 deletions
diff --git a/embassy-traits/src/i2c.rs b/embassy-traits/src/i2c.rs index bd8c72ab5..02755b571 100644 --- a/embassy-traits/src/i2c.rs +++ b/embassy-traits/src/i2c.rs | |||
| @@ -1,4 +1,4 @@ | |||
| 1 | //! Blocking I2C API | 1 | //! Async I2C API |
| 2 | //! | 2 | //! |
| 3 | //! This API supports 7-bit and 10-bit addresses. Traits feature an `AddressMode` | 3 | //! This API supports 7-bit and 10-bit addresses. Traits feature an `AddressMode` |
| 4 | //! marker type parameter. Two implementation of the `AddressMode` exist: | 4 | //! marker type parameter. Two implementation of the `AddressMode` exist: |
| @@ -16,37 +16,6 @@ | |||
| 16 | //! Since 7-bit addressing is the mode of the majority of I2C devices, | 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. | 17 | //! `SevenBitAddress` has been set as default mode and thus can be omitted if desired. |
| 18 | //! | 18 | //! |
| 19 | //! ## Examples | ||
| 20 | //! | ||
| 21 | //! ### `embedded-hal` implementation for an MCU | ||
| 22 | //! Here is an example of an embedded-hal implementation of the `Write` trait | ||
| 23 | //! for both modes: | ||
| 24 | //! ``` | ||
| 25 | //! # use embedded_hal::blocking::i2c::{SevenBitAddress, TenBitAddress, Write}; | ||
| 26 | //! /// I2C0 hardware peripheral which supports both 7-bit and 10-bit addressing. | ||
| 27 | //! pub struct I2c0; | ||
| 28 | //! | ||
| 29 | //! impl Write<SevenBitAddress> for I2c0 | ||
| 30 | //! { | ||
| 31 | //! # type Error = (); | ||
| 32 | //! # | ||
| 33 | //! fn try_write(&mut self, addr: u8, output: &[u8]) -> Result<(), Self::Error> { | ||
| 34 | //! // ... | ||
| 35 | //! # Ok(()) | ||
| 36 | //! } | ||
| 37 | //! } | ||
| 38 | //! | ||
| 39 | //! impl Write<TenBitAddress> for I2c0 | ||
| 40 | //! { | ||
| 41 | //! # type Error = (); | ||
| 42 | //! # | ||
| 43 | //! fn try_write(&mut self, addr: u16, output: &[u8]) -> Result<(), Self::Error> { | ||
| 44 | //! // ... | ||
| 45 | //! # Ok(()) | ||
| 46 | //! } | ||
| 47 | //! } | ||
| 48 | //! ``` | ||
| 49 | //! | ||
| 50 | //! ### Device driver compatible only with 7-bit addresses | 19 | //! ### Device driver compatible only with 7-bit addresses |
| 51 | //! | 20 | //! |
| 52 | //! For demonstration purposes the address mode parameter has been omitted in this example. | 21 | //! For demonstration purposes the address mode parameter has been omitted in this example. |
| @@ -66,7 +35,8 @@ | |||
| 66 | //! pub fn read_temperature(&mut self) -> Result<u8, E> { | 35 | //! pub fn read_temperature(&mut self) -> Result<u8, E> { |
| 67 | //! let mut temp = [0]; | 36 | //! let mut temp = [0]; |
| 68 | //! self.i2c | 37 | //! self.i2c |
| 69 | //! .try_write_read(ADDR, &[TEMP_REGISTER], &mut temp) | 38 | //! .write_read(ADDR, &[TEMP_REGISTER], &mut temp) |
| 39 | //! .await | ||
| 70 | //! .and(Ok(temp[0])) | 40 | //! .and(Ok(temp[0])) |
| 71 | //! } | 41 | //! } |
| 72 | //! } | 42 | //! } |
| @@ -89,13 +59,15 @@ | |||
| 89 | //! pub fn read_temperature(&mut self) -> Result<u8, E> { | 59 | //! pub fn read_temperature(&mut self) -> Result<u8, E> { |
| 90 | //! let mut temp = [0]; | 60 | //! let mut temp = [0]; |
| 91 | //! self.i2c | 61 | //! self.i2c |
| 92 | //! .try_write_read(ADDR, &[TEMP_REGISTER], &mut temp) | 62 | //! .write_read(ADDR, &[TEMP_REGISTER], &mut temp) |
| 63 | //! .await | ||
| 93 | //! .and(Ok(temp[0])) | 64 | //! .and(Ok(temp[0])) |
| 94 | //! } | 65 | //! } |
| 95 | //! } | 66 | //! } |
| 96 | //! ``` | 67 | //! ``` |
| 97 | 68 | ||
| 98 | use core::future::Future; | 69 | use core::future::Future; |
| 70 | use core::pin::Pin; | ||
| 99 | 71 | ||
| 100 | mod private { | 72 | mod private { |
| 101 | pub trait Sealed {} | 73 | pub trait Sealed {} |
| @@ -144,7 +116,7 @@ pub trait Read<A: AddressMode = SevenBitAddress> { | |||
| 144 | /// - `MAK` = master acknowledge | 116 | /// - `MAK` = master acknowledge |
| 145 | /// - `NMAK` = master no acknowledge | 117 | /// - `NMAK` = master no acknowledge |
| 146 | /// - `SP` = stop condition | 118 | /// - `SP` = stop condition |
| 147 | fn read<'a>(&mut self, address: A, buffer: &mut [u8]) -> Self::ReadFuture<'a>; | 119 | fn read<'a>(self: Pin<&'a mut Self>, address: A, buffer: &mut [u8]) -> Self::ReadFuture<'a>; |
| 148 | } | 120 | } |
| 149 | 121 | ||
| 150 | /// Blocking write | 122 | /// Blocking write |
| @@ -170,7 +142,7 @@ pub trait Write<A: AddressMode = SevenBitAddress> { | |||
| 170 | /// - `SAK` = slave acknowledge | 142 | /// - `SAK` = slave acknowledge |
| 171 | /// - `Bi` = ith byte of data | 143 | /// - `Bi` = ith byte of data |
| 172 | /// - `SP` = stop condition | 144 | /// - `SP` = stop condition |
| 173 | fn write<'a>(&mut self, address: A, bytes: &[u8]) -> Self::WriteFuture<'a>; | 145 | fn write<'a>(self: Pin<&'a mut Self>, address: A, bytes: &[u8]) -> Self::WriteFuture<'a>; |
| 174 | } | 146 | } |
| 175 | 147 | ||
| 176 | /// Blocking write (iterator version) | 148 | /// Blocking write (iterator version) |
| @@ -185,7 +157,11 @@ pub trait WriteIter<A: AddressMode = SevenBitAddress> { | |||
| 185 | /// # I2C Events (contract) | 157 | /// # I2C Events (contract) |
| 186 | /// | 158 | /// |
| 187 | /// Same as `Write` | 159 | /// Same as `Write` |
| 188 | fn write_iter<'a, B>(&mut self, address: A, bytes: B) -> Self::WriteIterFuture<'a> | 160 | fn write_iter<'a, B>( |
| 161 | self: Pin<&'a mut Self>, | ||
| 162 | address: A, | ||
| 163 | bytes: B, | ||
| 164 | ) -> Self::WriteIterFuture<'a> | ||
| 189 | where | 165 | where |
| 190 | B: IntoIterator<Item = u8>; | 166 | B: IntoIterator<Item = u8>; |
| 191 | } | 167 | } |
| @@ -220,7 +196,7 @@ pub trait WriteRead<A: AddressMode = SevenBitAddress> { | |||
| 220 | /// - `NMAK` = master no acknowledge | 196 | /// - `NMAK` = master no acknowledge |
| 221 | /// - `SP` = stop condition | 197 | /// - `SP` = stop condition |
| 222 | fn write_read<'a>( | 198 | fn write_read<'a>( |
| 223 | &mut self, | 199 | self: Pin<&'a mut Self>, |
| 224 | address: A, | 200 | address: A, |
| 225 | bytes: &[u8], | 201 | bytes: &[u8], |
| 226 | buffer: &mut [u8], | 202 | buffer: &mut [u8], |
| @@ -241,7 +217,7 @@ pub trait WriteIterRead<A: AddressMode = SevenBitAddress> { | |||
| 241 | /// | 217 | /// |
| 242 | /// Same as the `WriteRead` trait | 218 | /// Same as the `WriteRead` trait |
| 243 | fn write_iter_read<'a, B>( | 219 | fn write_iter_read<'a, B>( |
| 244 | &mut self, | 220 | self: Pin<&'a mut Self>, |
| 245 | address: A, | 221 | address: A, |
| 246 | bytes: B, | 222 | bytes: B, |
| 247 | buffer: &mut [u8], | 223 | buffer: &mut [u8], |
