|
| 1 | +use embedded_hal_one::i2c::ErrorType; |
| 2 | + |
| 3 | +impl<I2C, PINS> ErrorType for super::FMPI2c<I2C, PINS> { |
| 4 | + type Error = super::Error; |
| 5 | +} |
| 6 | + |
1 | 7 | mod blocking {
|
2 |
| - use super::super::{fmpi2c1, Error, FMPI2c}; |
| 8 | + use super::super::{fmpi2c1, FMPI2c}; |
3 | 9 | use core::ops::Deref;
|
4 |
| - use embedded_hal_one::i2c::blocking::{Read, Write, WriteRead}; |
| 10 | + use embedded_hal_one::i2c::blocking::Operation; |
5 | 11 |
|
6 |
| - impl<I2C, PINS> WriteRead for FMPI2c<I2C, PINS> |
| 12 | + impl<I2C, PINS> embedded_hal_one::i2c::blocking::I2c for FMPI2c<I2C, PINS> |
7 | 13 | where
|
8 | 14 | I2C: Deref<Target = fmpi2c1::RegisterBlock>,
|
9 | 15 | {
|
10 |
| - type Error = Error; |
11 |
| - |
12 |
| - fn write_read(&mut self, addr: u8, bytes: &[u8], buffer: &mut [u8]) -> Result<(), Error> { |
13 |
| - // Set up current slave address for writing and disable autoending |
14 |
| - self.i2c.cr2.modify(|_, w| { |
15 |
| - w.sadd() |
16 |
| - .bits(u16::from(addr) << 1) |
17 |
| - .nbytes() |
18 |
| - .bits(bytes.len() as u8) |
19 |
| - .rd_wrn() |
20 |
| - .clear_bit() |
21 |
| - .autoend() |
22 |
| - .clear_bit() |
23 |
| - }); |
24 |
| - |
25 |
| - // Send a START condition |
26 |
| - self.i2c.cr2.modify(|_, w| w.start().set_bit()); |
27 |
| - |
28 |
| - // Wait until the transmit buffer is empty and there hasn't been any error condition |
29 |
| - while { |
30 |
| - let isr = self.i2c.isr.read(); |
31 |
| - self.check_and_clear_error_flags(&isr) |
32 |
| - .map_err(Error::nack_addr)?; |
33 |
| - isr.txis().bit_is_clear() && isr.tc().bit_is_clear() |
34 |
| - } {} |
35 |
| - |
36 |
| - // Send out all individual bytes |
37 |
| - for c in bytes { |
38 |
| - self.send_byte(*c)?; |
39 |
| - } |
40 |
| - |
41 |
| - // Wait until data was sent |
42 |
| - while { |
43 |
| - let isr = self.i2c.isr.read(); |
44 |
| - self.check_and_clear_error_flags(&isr) |
45 |
| - .map_err(Error::nack_data)?; |
46 |
| - isr.tc().bit_is_clear() |
47 |
| - } {} |
48 |
| - |
49 |
| - // Set up current address for reading |
50 |
| - self.i2c.cr2.modify(|_, w| { |
51 |
| - w.sadd() |
52 |
| - .bits(u16::from(addr) << 1) |
53 |
| - .nbytes() |
54 |
| - .bits(buffer.len() as u8) |
55 |
| - .rd_wrn() |
56 |
| - .set_bit() |
57 |
| - }); |
58 |
| - |
59 |
| - // Send another START condition |
60 |
| - self.i2c.cr2.modify(|_, w| w.start().set_bit()); |
61 |
| - |
62 |
| - // Send the autoend after setting the start to get a restart |
63 |
| - self.i2c.cr2.modify(|_, w| w.autoend().set_bit()); |
64 |
| - |
65 |
| - // Now read in all bytes |
66 |
| - for c in buffer.iter_mut() { |
67 |
| - *c = self.recv_byte()?; |
68 |
| - } |
69 |
| - |
70 |
| - // Check and clear flags if they somehow ended up set |
71 |
| - self.check_and_clear_error_flags(&self.i2c.isr.read()) |
72 |
| - .map_err(Error::nack_data)?; |
73 |
| - |
74 |
| - Ok(()) |
| 16 | + fn read(&mut self, addr: u8, buffer: &mut [u8]) -> Result<(), Self::Error> { |
| 17 | + self.read(addr, buffer) |
75 | 18 | }
|
76 |
| - } |
77 | 19 |
|
78 |
| - impl<I2C, PINS> Read for FMPI2c<I2C, PINS> |
79 |
| - where |
80 |
| - I2C: Deref<Target = fmpi2c1::RegisterBlock>, |
81 |
| - { |
82 |
| - type Error = Error; |
83 |
| - |
84 |
| - fn read(&mut self, addr: u8, buffer: &mut [u8]) -> Result<(), Error> { |
85 |
| - // Set up current address for reading |
86 |
| - self.i2c.cr2.modify(|_, w| { |
87 |
| - w.sadd() |
88 |
| - .bits(u16::from(addr) << 1) |
89 |
| - .nbytes() |
90 |
| - .bits(buffer.len() as u8) |
91 |
| - .rd_wrn() |
92 |
| - .set_bit() |
93 |
| - }); |
94 |
| - |
95 |
| - // Send a START condition |
96 |
| - self.i2c.cr2.modify(|_, w| w.start().set_bit()); |
97 |
| - |
98 |
| - // Send the autoend after setting the start to get a restart |
99 |
| - self.i2c.cr2.modify(|_, w| w.autoend().set_bit()); |
100 |
| - |
101 |
| - // Now read in all bytes |
102 |
| - for c in buffer.iter_mut() { |
103 |
| - *c = self.recv_byte()?; |
104 |
| - } |
105 |
| - |
106 |
| - // Check and clear flags if they somehow ended up set |
107 |
| - self.check_and_clear_error_flags(&self.i2c.isr.read()) |
108 |
| - .map_err(Error::nack_data)?; |
109 |
| - |
110 |
| - Ok(()) |
| 20 | + fn write(&mut self, addr: u8, bytes: &[u8]) -> Result<(), Self::Error> { |
| 21 | + self.write(addr, bytes) |
111 | 22 | }
|
112 |
| - } |
113 | 23 |
|
114 |
| - impl<I2C, PINS> Write for FMPI2c<I2C, PINS> |
115 |
| - where |
116 |
| - I2C: Deref<Target = fmpi2c1::RegisterBlock>, |
117 |
| - { |
118 |
| - type Error = Error; |
119 |
| - |
120 |
| - fn write(&mut self, addr: u8, bytes: &[u8]) -> Result<(), Error> { |
121 |
| - // Set up current slave address for writing and enable autoending |
122 |
| - self.i2c.cr2.modify(|_, w| { |
123 |
| - w.sadd() |
124 |
| - .bits(u16::from(addr) << 1) |
125 |
| - .nbytes() |
126 |
| - .bits(bytes.len() as u8) |
127 |
| - .rd_wrn() |
128 |
| - .clear_bit() |
129 |
| - .autoend() |
130 |
| - .set_bit() |
131 |
| - }); |
| 24 | + fn write_iter<B>(&mut self, _addr: u8, _bytes: B) -> Result<(), Self::Error> |
| 25 | + where |
| 26 | + B: IntoIterator<Item = u8>, |
| 27 | + { |
| 28 | + todo!() |
| 29 | + } |
132 | 30 |
|
133 |
| - // Send a START condition |
134 |
| - self.i2c.cr2.modify(|_, w| w.start().set_bit()); |
| 31 | + fn write_read( |
| 32 | + &mut self, |
| 33 | + addr: u8, |
| 34 | + bytes: &[u8], |
| 35 | + buffer: &mut [u8], |
| 36 | + ) -> Result<(), Self::Error> { |
| 37 | + self.write_read(addr, bytes, buffer) |
| 38 | + } |
135 | 39 |
|
136 |
| - // Send out all individual bytes |
137 |
| - for c in bytes { |
138 |
| - self.send_byte(*c)?; |
139 |
| - } |
| 40 | + fn write_iter_read<B>( |
| 41 | + &mut self, |
| 42 | + _addr: u8, |
| 43 | + _bytes: B, |
| 44 | + _buffer: &mut [u8], |
| 45 | + ) -> Result<(), Self::Error> |
| 46 | + where |
| 47 | + B: IntoIterator<Item = u8>, |
| 48 | + { |
| 49 | + todo!() |
| 50 | + } |
140 | 51 |
|
141 |
| - // Check and clear flags if they somehow ended up set |
142 |
| - self.check_and_clear_error_flags(&self.i2c.isr.read()) |
143 |
| - .map_err(Error::nack_data)?; |
| 52 | + fn transaction<'a>( |
| 53 | + &mut self, |
| 54 | + _addr: u8, |
| 55 | + _operations: &mut [Operation<'a>], |
| 56 | + ) -> Result<(), Self::Error> { |
| 57 | + todo!() |
| 58 | + } |
144 | 59 |
|
145 |
| - Ok(()) |
| 60 | + fn transaction_iter<'a, O>(&mut self, _addr: u8, _operations: O) -> Result<(), Self::Error> |
| 61 | + where |
| 62 | + O: IntoIterator<Item = Operation<'a>>, |
| 63 | + { |
| 64 | + todo!() |
146 | 65 | }
|
147 | 66 | }
|
148 | 67 | }
|
0 commit comments