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
use super::DeviceInterface;
use crate::Error;
use embedded_hal as hal;

use shufflebuf::ShuffleBuf;

/// This encapsulates the Serial UART peripheral
/// and associated pins such as
/// - DRDY: Data Ready: Sensor uses this to indicate it had data available for read
pub struct SerialInterface<SER> {
    /// the serial port to use when communicating
    serial: SER,
    shuffler: ShuffleBuf,
}

impl<SER, CommE> SerialInterface<SER>
where
    SER: hal::serial::Read<u8, Error = CommE>,
{
    pub fn new(serial_port: SER) -> Self {
        Self {
            serial: serial_port,
            shuffler: ShuffleBuf::default(),
        }
    }
}

impl<SER, CommE> DeviceInterface for SerialInterface<SER>
where
    SER: hal::serial::Read<u8, Error = CommE>,
{
    type InterfaceError = Error<CommE>;

    fn read(&mut self) -> Result<u8, Self::InterfaceError> {
        let (count, byte) = self.shuffler.read_one();
        if count > 0 {
            return Ok(byte);
        } else {
            let mut block_byte = [0u8; 1];
            //TODO in practice this hasn't failed yet, but we should handle the error
            self.read_many(&mut block_byte)?;
            Ok(block_byte[0])
        }
    }

    fn fill(&mut self) -> usize {
        let mut fetch_count = self.shuffler.vacant();
        let mut err_count = 0;

        while fetch_count > 0 {
            let rc = self.serial.read();
            match rc {
                Ok(byte) => {
                    err_count = 0; //reset
                    self.shuffler.push_one(byte);
                    fetch_count -= 1;
                }
                Err(nb::Error::WouldBlock) => {}
                Err(nb::Error::Other(_)) => {
                    // in practice this is returning Overrun a ton on stm32h7
                    err_count += 1;
                    if err_count > 100 {
                        break;
                    }
                }
            }
        }
        self.shuffler.available()
    }

    fn read_many(
        &mut self,
        buffer: &mut [u8],
    ) -> Result<usize, Self::InterfaceError> {
        let avail = self.shuffler.available();
        if avail >= buffer.len() {
            let final_read_count = self.shuffler.read_many(buffer);
            return Ok(final_read_count);
        }

        return Ok(0);
    }
}