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
#![no_std]

use embedded_hal::blocking::spi::{Transfer, Write};
use embedded_hal::digital::v2::OutputPin;
use evegfx::interface::Interface;

/// `EVEHALSPIInterface` is an implementation of `evegfx.Interface` that
/// commincates over SPI using the `embedded-hal` SPI and GPIO (for
/// "chip select") traits.
pub struct EVEHALSPIInterface<SPI, CS>
where
    SPI: Transfer<u8>,
    CS: OutputPin,
{
    spi: SPI,
    cs: CS,
}

impl<SPI, CS> EVEHALSPIInterface<SPI, CS>
where
    SPI: Transfer<u8> + Write<u8>,
    CS: OutputPin,
{
    /// Create a new EVE interface in terms of the given SPI bus and CS
    /// signal implementations.
    ///
    /// The given CS implementation must be a digital output pin which will be
    /// set to low to assert chip select, or high to unassert it, reflecting
    /// the physical characteristics of the CS pin on EVE IC packages.
    pub fn new(spi: SPI, cs: CS) -> Self {
        Self { spi: spi, cs: cs }
    }

    fn with_cs<F, R>(&mut self, func: F) -> Result<R, <Self as Interface>::Error>
    where
        F: FnOnce(&mut Self) -> Result<R, <Self as Interface>::Error>,
    {
        self.spi_select()?;
        let result = func(self);
        self.spi_unselect()?;
        result
    }

    fn spi_select(&mut self) -> Result<(), <Self as Interface>::Error> {
        <Self as Interface>::Error::cs_result(self.cs.set_low())
    }

    fn spi_unselect(&mut self) -> Result<(), <Self as Interface>::Error> {
        <Self as Interface>::Error::cs_result(self.cs.set_high())
    }

    fn spi_write(&mut self, words: &[u8]) -> Result<(), <Self as Interface>::Error> {
        let r = self.spi.write(words);
        self.spi_write_result(r)
    }

    fn spi_transfer<'w>(
        &mut self,
        words: &'w mut [u8],
    ) -> Result<&'w [u8], <Self as Interface>::Error> {
        let r = self.spi.transfer(words);
        self.spi_transfer_result(r)
    }

    fn spi_write_result<T>(
        &self,
        r: Result<T, <SPI as Write<u8>>::Error>,
    ) -> Result<T, <Self as Interface>::Error> {
        <Self as Interface>::Error::spi_write_result(r)
    }

    fn spi_transfer_result<T>(
        &self,
        r: Result<T, <SPI as Transfer<u8>>::Error>,
    ) -> Result<T, <Self as Interface>::Error> {
        <Self as Interface>::Error::spi_transfer_result(r)
    }
}

impl<SPI, CS> Interface for EVEHALSPIInterface<SPI, CS>
where
    SPI: Transfer<u8> + Write<u8>,
    CS: OutputPin,
{
    type Error = EVEHALSPIError<<SPI as Write<u8>>::Error, <SPI as Transfer<u8>>::Error, CS::Error>;

    fn begin_write(&mut self, addr: u32) -> Result<(), Self::Error> {
        self.spi_select()?;
        let mut addr_words: [u8; 3] = [0; 3];
        self.build_write_header(addr, &mut addr_words);
        self.spi_write(&addr_words)
    }

    fn continue_write(&mut self, v: &[u8]) -> Result<(), Self::Error> {
        self.spi_write(v)
    }

    fn end_write(&mut self) -> Result<(), Self::Error> {
        self.spi_unselect()
    }

    fn begin_read(&mut self, addr: u32) -> Result<(), Self::Error> {
        self.spi_select()?;
        let mut addr_words: [u8; 4] = [0; 4];
        self.build_read_header(addr, &mut addr_words);
        self.spi_write(&addr_words)
    }

    fn continue_read(&mut self, into: &mut [u8]) -> Result<(), Self::Error> {
        self.spi_transfer(into)?;
        Ok(())
    }

    fn end_read(&mut self) -> Result<(), Self::Error> {
        self.spi_unselect()
    }

    fn host_cmd(&mut self, cmd: u8, a0: u8, a1: u8) -> Result<(), Self::Error> {
        let mut cmd_words: [u8; 3] = [0; 3];
        self.build_host_cmd_msg(cmd, a0, a1, &mut cmd_words);
        self.with_cs(|ei| ei.spi_write(&cmd_words))
    }
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum EVEHALSPIError<SPIWriteError, SPITransferError, CSError> {
    SPIWrite(SPIWriteError),
    SPITransfer(SPITransferError),
    CS(CSError),
}

impl<SPIWriteError, SPITransferError, CSError>
    EVEHALSPIError<SPIWriteError, SPITransferError, CSError>
{
    fn spi_write_result<T>(r: Result<T, SPIWriteError>) -> Result<T, Self> {
        match r {
            Ok(v) => Ok(v),
            Err(e) => Err(Self::SPIWrite(e)),
        }
    }

    fn spi_transfer_result<T>(r: Result<T, SPITransferError>) -> Result<T, Self> {
        match r {
            Ok(v) => Ok(v),
            Err(e) => Err(Self::SPITransfer(e)),
        }
    }

    fn cs_result<T>(r: Result<T, CSError>) -> Result<T, Self> {
        match r {
            Ok(v) => Ok(v),
            Err(e) => Err(Self::CS(e)),
        }
    }
}