|
| 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 | +use core::future::Future; |
| 20 | +pub use embedded_hal::i2c::{ |
| 21 | + AddressMode, Error, ErrorKind, NoAcknowledgeSource, SevenBitAddress, TenBitAddress, |
| 22 | +}; |
| 23 | + |
| 24 | +/// Async read |
| 25 | +pub trait Read<A: AddressMode = SevenBitAddress> { |
| 26 | + /// Error type |
| 27 | + type Error: Error; |
| 28 | + /// The future associated with the `read` method. |
| 29 | + type ReadFuture<'a>: Future<Output = Result<(), Self::Error>> + 'a |
| 30 | + where |
| 31 | + Self: 'a; |
| 32 | + |
| 33 | + /// Reads enough bytes from slave with `address` to fill `buffer` |
| 34 | + /// |
| 35 | + /// # I2C Events (contract) |
| 36 | + /// |
| 37 | + /// ``` text |
| 38 | + /// Master: ST SAD+R MAK MAK ... NMAK SP |
| 39 | + /// Slave: SAK B0 B1 ... BN |
| 40 | + /// ``` |
| 41 | + /// |
| 42 | + /// Where |
| 43 | + /// |
| 44 | + /// - `ST` = start condition |
| 45 | + /// - `SAD+R` = slave address followed by bit 1 to indicate reading |
| 46 | + /// - `SAK` = slave acknowledge |
| 47 | + /// - `Bi` = ith byte of data |
| 48 | + /// - `MAK` = master acknowledge |
| 49 | + /// - `NMAK` = master no acknowledge |
| 50 | + /// - `SP` = stop condition |
| 51 | + fn read<'a>(&'a mut self, address: A, read: &'a mut [u8]) -> Self::ReadFuture<'a>; |
| 52 | +} |
| 53 | + |
| 54 | +impl<A: AddressMode, T: Read<A>> Read<A> for &mut T { |
| 55 | + type Error = T::Error; |
| 56 | + |
| 57 | + type ReadFuture<'a> |
| 58 | + where |
| 59 | + Self: 'a, |
| 60 | + = T::ReadFuture<'a>; |
| 61 | + |
| 62 | + fn read<'a>(&'a mut self, address: A, buffer: &'a mut [u8]) -> Self::ReadFuture<'a> { |
| 63 | + T::read(self, address, buffer) |
| 64 | + } |
| 65 | +} |
| 66 | + |
| 67 | +/// Async write |
| 68 | +pub trait Write<A: AddressMode = SevenBitAddress> { |
| 69 | + /// Error type |
| 70 | + type Error: Error; |
| 71 | + /// The future associated with the `write` method. |
| 72 | + type WriteFuture<'a>: Future<Output = Result<(), Self::Error>> + 'a |
| 73 | + where |
| 74 | + Self: 'a; |
| 75 | + |
| 76 | + /// Writes bytes to slave with address `address` |
| 77 | + /// |
| 78 | + /// # I2C Events (contract) |
| 79 | + /// |
| 80 | + /// ``` text |
| 81 | + /// Master: ST SAD+W B0 B1 ... BN SP |
| 82 | + /// Slave: SAK SAK SAK ... SAK |
| 83 | + /// ``` |
| 84 | + /// |
| 85 | + /// Where |
| 86 | + /// |
| 87 | + /// - `ST` = start condition |
| 88 | + /// - `SAD+W` = slave address followed by bit 0 to indicate writing |
| 89 | + /// - `SAK` = slave acknowledge |
| 90 | + /// - `Bi` = ith byte of data |
| 91 | + /// - `SP` = stop condition |
| 92 | + fn write<'a>(&'a mut self, address: A, write: &'a [u8]) -> Self::WriteFuture<'a>; |
| 93 | +} |
| 94 | + |
| 95 | +impl<A: AddressMode, T: Write<A>> Write<A> for &mut T { |
| 96 | + type Error = T::Error; |
| 97 | + |
| 98 | + type WriteFuture<'a> |
| 99 | + where |
| 100 | + Self: 'a, |
| 101 | + = T::WriteFuture<'a>; |
| 102 | + |
| 103 | + fn write<'a>(&'a mut self, address: A, bytes: &'a [u8]) -> Self::WriteFuture<'a> { |
| 104 | + T::write(self, address, bytes) |
| 105 | + } |
| 106 | +} |
| 107 | + |
| 108 | +/// Async write + read |
| 109 | +pub trait WriteRead<A: AddressMode = SevenBitAddress> { |
| 110 | + /// Error type |
| 111 | + type Error: Error; |
| 112 | + /// The future associated with the `write_read` method. |
| 113 | + type WriteReadFuture<'a>: Future<Output = Result<(), Self::Error>> + 'a |
| 114 | + where |
| 115 | + Self: 'a; |
| 116 | + |
| 117 | + /// Writes bytes to slave with address `address` and then reads enough bytes to fill `read` *in a |
| 118 | + /// single transaction*. |
| 119 | + /// |
| 120 | + /// # I2C Events (contract) |
| 121 | + /// |
| 122 | + /// ``` text |
| 123 | + /// Master: ST SAD+W O0 O1 ... OM SR SAD+R MAK MAK ... NMAK SP |
| 124 | + /// Slave: SAK SAK SAK ... SAK SAK I0 I1 ... IN |
| 125 | + /// ``` |
| 126 | + /// |
| 127 | + /// Where |
| 128 | + /// |
| 129 | + /// - `ST` = start condition |
| 130 | + /// - `SAD+W` = slave address followed by bit 0 to indicate writing |
| 131 | + /// - `SAK` = slave acknowledge |
| 132 | + /// - `Oi` = ith outgoing byte of data |
| 133 | + /// - `SR` = repeated start condition |
| 134 | + /// - `SAD+R` = slave address followed by bit 1 to indicate reading |
| 135 | + /// - `Ii` = ith incoming byte of data |
| 136 | + /// - `MAK` = master acknowledge |
| 137 | + /// - `NMAK` = master no acknowledge |
| 138 | + /// - `SP` = stop condition |
| 139 | + fn write_read<'a>( |
| 140 | + &'a mut self, |
| 141 | + address: A, |
| 142 | + write: &'a [u8], |
| 143 | + read: &'a mut [u8], |
| 144 | + ) -> Self::WriteReadFuture<'a>; |
| 145 | +} |
| 146 | + |
| 147 | +impl<A: AddressMode, T: WriteRead<A>> WriteRead<A> for &mut T { |
| 148 | + type Error = T::Error; |
| 149 | + |
| 150 | + type WriteReadFuture<'a> |
| 151 | + where |
| 152 | + Self: 'a, |
| 153 | + = T::WriteReadFuture<'a>; |
| 154 | + |
| 155 | + fn write_read<'a>( |
| 156 | + &'a mut self, |
| 157 | + address: A, |
| 158 | + bytes: &'a [u8], |
| 159 | + buffer: &'a mut [u8], |
| 160 | + ) -> Self::WriteReadFuture<'a> { |
| 161 | + T::write_read(self, address, bytes, buffer) |
| 162 | + } |
| 163 | +} |
0 commit comments