i2c_pio/
eh0_2.rs

1use crate::*;
2use embedded_hal_0_2::blocking::i2c::{self, Operation};
3impl<A, P, SMI, SDA, SCL> i2c::Read<A> for I2C<'_, P, SMI, SDA, SCL>
4where
5    A: ValidAddressMode,
6    P: PIOExt,
7    SMI: StateMachineIndex,
8    SDA: AnyPin,
9    SCL: AnyPin,
10{
11    type Error = Error;
12
13    fn read(&mut self, address: A, buffer: &mut [u8]) -> Result<(), Self::Error> {
14        let iter = super::setup(address, true, false).chain(buffer.iter_mut().map(CmdWord::read));
15        self.process_queue(iter)?;
16        self.generate_stop();
17        Ok(())
18    }
19}
20
21impl<A, P, SMI, SDA, SCL> i2c::WriteIter<A> for I2C<'_, P, SMI, SDA, SCL>
22where
23    A: ValidAddressMode,
24    P: PIOExt,
25    SMI: StateMachineIndex,
26    SDA: AnyPin,
27    SCL: AnyPin,
28{
29    type Error = Error;
30
31    fn write<B>(&mut self, address: A, bytes: B) -> Result<(), Self::Error>
32    where
33        B: IntoIterator<Item = u8>,
34    {
35        let iter = super::setup(address, false, false).chain(bytes.into_iter().map(CmdWord::write));
36        self.process_queue(iter)?;
37        self.generate_stop();
38        Ok(())
39    }
40}
41impl<A, P, SMI, SDA, SCL> i2c::Write<A> for I2C<'_, P, SMI, SDA, SCL>
42where
43    A: ValidAddressMode,
44    P: PIOExt,
45    SMI: StateMachineIndex,
46    SDA: AnyPin,
47    SCL: AnyPin,
48{
49    type Error = Error;
50
51    fn write(&mut self, address: A, buffer: &[u8]) -> Result<(), Self::Error> {
52        <Self as i2c::WriteIter<A>>::write(self, address, buffer.iter().cloned())
53    }
54}
55
56impl<A, P, SMI, SDA, SCL> i2c::WriteIterRead<A> for I2C<'_, P, SMI, SDA, SCL>
57where
58    A: ValidAddressMode,
59    P: PIOExt,
60    SMI: StateMachineIndex,
61    SDA: AnyPin,
62    SCL: AnyPin,
63{
64    type Error = Error;
65
66    fn write_iter_read<B>(
67        &mut self,
68        address: A,
69        bytes: B,
70        buffer: &mut [u8],
71    ) -> Result<(), Self::Error>
72    where
73        B: IntoIterator<Item = u8>,
74    {
75        self.process_queue(
76            super::setup(address, false, false)
77                .chain(bytes.into_iter().map(CmdWord::write))
78                .chain(super::setup(address, true, true))
79                .chain(buffer.iter_mut().map(CmdWord::read)),
80        )?;
81        self.generate_stop();
82        Ok(())
83    }
84}
85impl<A, P, SMI, SDA, SCL> i2c::WriteRead<A> for I2C<'_, P, SMI, SDA, SCL>
86where
87    A: ValidAddressMode,
88    P: PIOExt,
89    SMI: StateMachineIndex,
90    SDA: AnyPin,
91    SCL: AnyPin,
92{
93    type Error = Error;
94
95    fn write_read(
96        &mut self,
97        address: A,
98        bytes: &[u8],
99        buffer: &mut [u8],
100    ) -> Result<(), Self::Error> {
101        <Self as i2c::WriteIterRead<A>>::write_iter_read(
102            self,
103            address,
104            bytes.iter().cloned(),
105            buffer,
106        )
107    }
108}
109
110impl<A, P, SMI, SDA, SCL> i2c::TransactionalIter<A> for I2C<'_, P, SMI, SDA, SCL>
111where
112    A: ValidAddressMode,
113    P: PIOExt,
114    SMI: StateMachineIndex,
115    SDA: AnyPin,
116    SCL: AnyPin,
117{
118    type Error = Error;
119
120    fn exec_iter<'a, O>(&mut self, address: A, operations: O) -> Result<(), Self::Error>
121    where
122        O: IntoIterator<Item = Operation<'a>>,
123    {
124        let mut first = true;
125        for op in operations {
126            let iter = match op {
127                Operation::Read(buf) => Left(
128                    super::setup(address, true, !first).chain(buf.iter_mut().map(CmdWord::read)),
129                ),
130                Operation::Write(buf) => Right(
131                    super::setup(address, false, !first)
132                        .chain(buf.iter().cloned().map(CmdWord::write)),
133                ),
134            };
135            self.process_queue(iter)?;
136            first = false;
137        }
138        self.generate_stop();
139        Ok(())
140    }
141}
142
143impl<A, P, SMI, SDA, SCL> i2c::Transactional<A> for I2C<'_, P, SMI, SDA, SCL>
144where
145    A: ValidAddressMode,
146    P: PIOExt,
147    SMI: StateMachineIndex,
148    SDA: AnyPin,
149    SCL: AnyPin,
150{
151    type Error = Error;
152
153    fn exec(&mut self, address: A, operations: &mut [Operation<'_>]) -> Result<(), Self::Error> {
154        let mut first = true;
155        for op in operations {
156            let iter = match op {
157                Operation::Read(buf) => Left(
158                    super::setup(address, true, !first).chain(buf.iter_mut().map(CmdWord::read)),
159                ),
160                Operation::Write(buf) => Right(
161                    super::setup(address, false, !first)
162                        .chain(buf.iter().cloned().map(CmdWord::write)),
163                ),
164            };
165            self.process_queue(iter)?;
166            first = false;
167        }
168        self.generate_stop();
169        Ok(())
170    }
171}