1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
// Copyright lowRISC contributors (OpenTitan project).
// Licensed under the Apache License, Version 2.0, see LICENSE for details.
// SPDX-License-Identifier: Apache-2.0

use super::Bit;
use anyhow::{bail, Context, Result};
use arrayvec::ArrayVec;

#[derive(Debug, PartialEq)]
enum Symbol {
    Start,
    Stop,
    Byte { data: u8, nack: bool },
    Broken(ArrayVec<Bit, 8>),
}

impl Symbol {
    pub fn broken(data: u8, bits: usize) -> Result<Self> {
        if !(1..8).contains(&bits) {
            bail!("Samples must be between 1 and 7");
        }
        let buffer: ArrayVec<Bit, 8> = (0..bits).map(|bit| Bit::from(data << bit)).collect();

        Ok(Self::Broken(buffer))
    }

    // We must extend the transaction: in a given sample, if data is written to SDA at the same time as SCL
    // being driven high, then physical factors e.g. capacitance can cause the SDA write to appear before
    // the SCL, even though the order of bitbanging is reversed. This can then cause a mistaken "STOP" signal
    // to be received. We instead run at half frequency: on one cycle write SDA, and on the next write SCL.
    // This makes GPIO bitbanging of I2C signals reliable.
    // Assumes that SCL is low (0) when called.
    pub fn bitbanging<const SDA: u8, const SCL: u8>(&self, samples: &mut Vec<u8>) {
        match self {
            // Each sample is translated into 2 samples: the first changes the SDA, and
            // the second changes the SCL, to ensure correct ordering.
            Symbol::Start => samples.extend([
                // SDA high, SCL high
                0x01 << SDA | 0x00 << SCL,
                0x01 << SDA | 0x01 << SCL,
                // SDA low, SCL high
                0x00 << SDA | 0x01 << SCL,
                0x00 << SDA | 0x01 << SCL,
                // SDA low, SCL low
                0x00 << SDA | 0x01 << SCL,
                0x00 << SDA | 0x00 << SCL,
            ]),
            Symbol::Stop => samples.extend([
                // SDA low, SCL low
                0x00 << SDA | 0x00 << SCL,
                0x00 << SDA | 0x00 << SCL,
                // SDA low, SCL high
                0x00 << SDA | 0x00 << SCL,
                0x00 << SDA | 0x01 << SCL,
                // SDA high, SCL high
                0x01 << SDA | 0x01 << SCL,
                0x01 << SDA | 0x01 << SCL,
            ]),
            Symbol::Byte { data, nack } => Self::bitbanging_byte::<SDA, SCL>(*data, *nack, samples),
            Symbol::Broken(bits) => Self::bitbanging_bits::<SDA, SCL>(bits, samples),
        }
    }

    fn bitbanging_byte<const SDA: u8, const SCL: u8>(byte: u8, nack: bool, samples: &mut Vec<u8>) {
        let data: u16 = (byte as u16) << 1u16 | nack as u16;
        samples.extend((0..9u8).rev().flat_map(|bit| {
            [
                // Change SDA (to data), SCL high
                ((((data >> bit) & 0x01) << SDA) | 0x00 << SCL) as u8,
                ((((data >> bit) & 0x01) << SDA) | 0x01 << SCL) as u8,
                // Maintain SDA, SCL low
                ((((data >> bit) & 0x01) << SDA) | 0x01 << SCL) as u8,
                ((((data >> bit) & 0x01) << SDA) | 0x00 << SCL) as u8,
            ]
        }));
    }

    fn bitbanging_bits<const SDA: u8, const SCL: u8>(bits: &[Bit], samples: &mut Vec<u8>) {
        samples.extend(bits.iter().rev().flat_map(|bit| {
            [
                // Change SDA (to data), SCL high
                ((*bit as u8) << SDA) | 0x00 << SCL,
                ((*bit as u8) << SDA) | 0x01 << SCL,
                // Maintain SDA, SCL low
                ((*bit as u8) << SDA) | 0x01 << SCL,
                ((*bit as u8) << SDA) | 0x00 << SCL,
            ]
        }));
    }
}

pub mod encoder {
    use super::*;

    #[derive(Debug, PartialEq)]
    pub enum Transfer<'w> {
        Start,
        Stop,
        Addr { addr: u8, read: bool, nack: bool },
        Write(&'w [u8]),
        Read(usize),
        Broken(ArrayVec<Bit, 8>),
    }

    impl Transfer<'_> {
        fn bitbanging<const SDA: u8, const SCL: u8>(
            &self,
            is_next_stop: bool,
            samples: &mut Vec<u8>,
        ) {
            match self {
                Transfer::Start => Symbol::Start.bitbanging::<SDA, SCL>(samples),
                Transfer::Stop => Symbol::Stop.bitbanging::<SDA, SCL>(samples),
                Transfer::Addr { addr, read, nack } => Symbol::Byte {
                    data: (addr << 1) | *read as u8,
                    nack: *nack,
                }
                .bitbanging::<SDA, SCL>(samples),
                Transfer::Write(bytes) => {
                    for byte in bytes.iter() {
                        Symbol::Byte {
                            data: *byte,
                            nack: true,
                        }
                        .bitbanging::<SDA, SCL>(samples)
                    }
                }
                Transfer::Broken(bits) => {
                    Symbol::Broken(bits.clone()).bitbanging::<SDA, SCL>(samples)
                }
                Transfer::Read(len) => {
                    for index in 0..*len {
                        Symbol::Byte {
                            data: 0xff,
                            nack: index >= (len - 1) && is_next_stop,
                        }
                        .bitbanging::<SDA, SCL>(samples)
                    }
                }
            }
        }
    }

    pub struct Encoder<const SDA: u8, const SCL: u8> {}
    impl<const SDA: u8, const SCL: u8> Encoder<SDA, SCL> {
        // Note that this function will run I2C at half of the specified bitbanging sample frequency, because
        // two cycles must be used per sample to ensure that changes to SDA appear before the rise of SCL,
        // as otherwise I2C via GPIO bitbanging can be flaky.
        pub fn run(&self, transfer: &[Transfer]) -> Vec<u8> {
            let mut samples: Vec<u8> = Vec::new();
            for window in transfer.windows(2) {
                window[0]
                    .bitbanging::<SDA, SCL>(window.get(1) == Some(&Transfer::Stop), &mut samples)
            }

            // We missed the last element for using the windows function to peek, so we parse the last element here.
            transfer
                .iter()
                .last()
                .unwrap()
                .bitbanging::<SDA, SCL>(false, &mut samples);
            samples
        }
    }
}

pub mod decoder {
    use super::*;
    use std::iter::Peekable;

    #[derive(Debug, PartialEq)]
    pub enum Transfer<'b> {
        Start,
        Stop,
        Addr { addr: u8, read: bool, nack: bool },
        Bytes { data: &'b [u8], nack: bool },
        Broken(ArrayVec<Bit, 8>),
    }

    impl<'a> std::convert::From<Symbol> for Transfer<'a> {
        fn from(symbol: Symbol) -> Self {
            match symbol {
                Symbol::Start => Self::Start,
                Symbol::Stop => Self::Stop,
                Symbol::Broken(bits) => Self::Broken(bits),
                _ => panic!("Can't convert {:?} into Transfer", symbol),
            }
        }
    }

    enum DecodingState {
        Start,
        Bytes,
    }

    #[derive(Clone, Debug)]
    struct Sample<const SDA: u8, const SCL: u8> {
        raw: u8,
    }

    impl<const SDA: u8, const SCL: u8> Sample<SDA, SCL> {
        fn sda(&self) -> Bit {
            ((self.raw >> SDA) & 0x01).into()
        }

        fn scl(&self) -> Bit {
            ((self.raw >> SCL) & 0x01).into()
        }
    }
    pub struct Decoder<const SDA: u8, const SCL: u8> {
        pub buffer: [u8; 256],
    }

    impl<const SDA: u8, const SCL: u8> Decoder<SDA, SCL> {
        /// Loops until the clk transitions to low.
        /// Returns a symbol (Start|Stop) in case the sda transitions while the clk is high.
        /// The caller must make sure that the clock was high in the previous sample.
        fn sample_on_fall_edge<I>(samples: &mut I) -> Result<Option<Symbol>>
        where
            I: Iterator<Item = Sample<SDA, SCL>>,
        {
            let mut previous: Option<Sample<SDA, SCL>> = None;
            for sample in samples.by_ref() {
                if sample.scl() == Bit::Low {
                    return Ok(None); // No symbol found.
                }
                // If sda transitioned with the scl high it either means a stop or start symbol.
                if let Some(previous) = previous {
                    if previous.sda() != sample.sda() {
                        return Ok(Some(match sample.sda() {
                            Bit::High => Symbol::Stop,
                            Bit::Low => Symbol::Start,
                        }));
                    }
                }
                previous = Some(sample);
            }
            bail!("Ran out of samples and did not find fall edge")
        }

        /// Returns a sample when a raise clock edge is detected.
        /// This function will not consume the sample where the raise clock is detected.
        /// The caller must make sure that the clock was low in the previous sample.
        fn sample_on_raise_edge<I>(samples: &mut Peekable<I>) -> Option<Sample<SDA, SCL>>
        where
            I: Iterator<Item = Sample<SDA, SCL>>,
        {
            while samples.next_if(|sample| sample.scl() == Bit::Low).is_some() {}
            samples.peek().cloned()
        }

        fn loop_until<I>(samples: &mut I, sda: Bit, scl: Bit) -> Option<Sample<SDA, SCL>>
        where
            I: Iterator<Item = Sample<SDA, SCL>>,
        {
            samples
                .by_ref()
                .find(|sample| sample.sda() == sda && sample.scl() == scl)
        }

        fn find_start<I>(samples: &mut I) -> Result<Sample<SDA, SCL>>
        where
            I: Iterator<Item = Sample<SDA, SCL>>,
        {
            'outer: loop {
                // While clock and sda is not high.
                Self::loop_until(samples, Bit::High, Bit::High)
                    .context("Beginning of start bit not found")?;

                // SDA should transition to low while scl is high, marking the beginning of start condition.
                for sample in samples.by_ref() {
                    if sample.scl() == Bit::Low {
                        continue 'outer;
                    }

                    if sample.sda() == Bit::Low {
                        return Ok(sample);
                    }
                }
                bail!("Start bit condition not found")
            }
        }

        fn decode_symbol<I>(samples: &mut Peekable<I>) -> Result<Symbol>
        where
            I: Iterator<Item = Sample<SDA, SCL>>,
        {
            let mut byte = 0u16;
            // 8 bits data + 1 bit ack/nack
            for index in 0..9 {
                let Ok(fall_sample) = Self::sample_on_fall_edge(samples) else {
                    return Symbol::broken(byte as u8, index);
                };

                // Return in case a symbol was detected during fall sampling.
                if let Some(symbol) = fall_sample {
                    return Ok(symbol);
                }

                let Some(sample) = Self::sample_on_raise_edge(samples) else {
                    return Symbol::broken(byte as u8, index);
                };
                byte <<= 1;
                byte |= sample.sda() as u16;
            }

            Ok(Symbol::Byte {
                data: (byte >> 1) as u8,
                nack: byte & 0x01 == 1,
            })
        }

        pub fn run(&mut self, samples: Vec<u8>) -> Result<Vec<Transfer<'_>>> {
            let mut samples = samples
                .into_iter()
                .map(|raw| Sample::<SDA, SCL> { raw })
                .peekable();
            Self::find_start(&mut samples)?;
            let mut trans = vec![Transfer::Start];
            let mut state = DecodingState::Start;
            let mut head_offset = 0usize;
            let mut buffer = &mut self.buffer[..];

            while let Ok(symbol) = Self::decode_symbol(&mut samples) {
                state = match state {
                    DecodingState::Start => match symbol {
                        Symbol::Byte { data, nack } => {
                            let read = (data & 1) == 1;
                            trans.push(Transfer::Addr {
                                addr: data >> 1,
                                read,
                                nack,
                            });
                            DecodingState::Bytes
                        }
                        _ => {
                            trans.push(symbol.into());
                            DecodingState::Start
                        }
                    },
                    DecodingState::Bytes => match symbol {
                        Symbol::Byte { data, nack } => {
                            buffer[head_offset] = data;
                            head_offset += 1;
                            assert!(head_offset < buffer.len());
                            if nack {
                                let (filled, empty) = buffer.split_at_mut(head_offset);
                                buffer = empty;
                                head_offset = 0;
                                trans.push(Transfer::Bytes { data: filled, nack });
                                DecodingState::Start
                            } else {
                                DecodingState::Bytes
                            }
                        }
                        Symbol::Start | Symbol::Stop => {
                            if head_offset > 0 {
                                let (filled, empty) = buffer.split_at_mut(head_offset);
                                buffer = empty;
                                head_offset = 0;
                                trans.push(Transfer::Bytes {
                                    data: filled,
                                    nack: false,
                                });
                            }
                            trans.push(symbol.into());
                            DecodingState::Start
                        }
                        Symbol::Broken(_) => {
                            trans.push(symbol.into());
                            DecodingState::Start
                        }
                    },
                }
            }
            Ok(trans)
        }
    }
}