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

//! SPI over UART
//!
//! The `UARTSPI` struct implements an SPI interface on top of a `UART`, using the protocol
//! used by Grove.
//!
//! Also see: https://github.com/Seeed-Studio/Grove_LoRa_433MHz_and_915MHz_RF/blob/master/examples/Grove_LoRa_firmware/Grove_LoRa_firmware.ino
//!
//! ## Protocol
//!
//! When writing, this is:
//!
//! ~~~
//! | 'W' | <reg> | <len> | <data>... |
//! ~~~
//!
//! There is no response after a write request has been processed.
//!
//! When reading, it is:
//!
//! ~~~
//! | 'R' | <reg> | <len> |
//! ~~~
//!
//! This will follow <len> bytes, which is the data read from SPI.
//!

use embedded_hal::blocking::spi::{Transfer, Write};
use embedded_hal::digital::v2::OutputPin;
use embedded_hal::serial;

/// SPI over UART
pub struct UARTSPI<UART> {
    uart: UART,
}

impl<UART> UARTSPI<UART> {
    /// Wrap the SPI protocol around the UART interface.
    pub fn new(uart: UART) -> Self {
        UARTSPI { uart }
    }

    /// Free the SPI interface and get back the UART
    pub fn free(self) -> UART {
        self.uart
    }
}

impl<UART, E> Transfer<u8> for UARTSPI<UART>
where
    UART: serial::Read<u8, Error = E> + serial::Write<u8, Error = E>,
{
    type Error = nb::Error<E>;

    fn transfer<'w>(&mut self, words: &'w mut [u8]) -> Result<&'w [u8], Self::Error> {
        let mut len = words.len();
        if len == 0 {
            return Ok(words);
        }

        // the buffer also contains the register as the first byte, but this is treated specially
        // on the protocol level
        len -= 1;

        // FIXME: handle with grace
        assert!(len <= 255);

        let reg = words[0];

        // write read request
        nb::block!(self.uart.write(b'R'))?;
        nb::block!(self.uart.write(reg))?;
        nb::block!(self.uart.write(len as u8))?;
        // flush
        nb::block!(self.uart.flush())?;

        #[cfg(feature = "dump")]
        log::info!("Sent R-request (reg: 0x{:02x}, len: {})", reg, len);

        // read len bytes

        for i in 0..len {
            words[i + 1] = nb::block!(self.uart.read())?;
            #[cfg(feature = "dump")]
            log::info!("Received: {:02x}", words[i + 1]);
        }

        Ok(words)
    }
}

impl<UART> Write<u8> for UARTSPI<UART>
where
    UART: serial::Write<u8>,
{
    type Error = nb::Error<UART::Error>;

    fn write(&mut self, words: &[u8]) -> Result<(), Self::Error> {
        let mut len = words.len();
        if len == 0 {
            return Ok(());
        }

        // the buffer also contains the register as the first byte, but this is treated specially
        // on the protocol level
        len -= 1;

        // FIXME: handle with grace
        assert!(len <= 255);

        let reg = words[0];
        nb::block!(self.uart.write(b'W'))?;
        nb::block!(self.uart.write(reg))?;
        nb::block!(self.uart.write(len as u8))?;
        for b in &words[1..] {
            nb::block!(self.uart.write(*b))?;
        }
        nb::block!(self.uart.flush())?;

        #[cfg(feature = "dump")]
        log::info!("Sent W-request (reg: 0x{:02x}, len: {})", reg, len);

        Ok(())
    }
}

/// A no-op pin, which does nothing.
///
/// This may be useful in the context of [`UARTSPI`], to provide e.g. a CS or reset pin, which does
/// nothing in this case.
pub struct NoOpPin;

impl OutputPin for NoOpPin {
    type Error = ();

    fn set_low(&mut self) -> Result<(), Self::Error> {
        Ok(())
    }

    fn set_high(&mut self) -> Result<(), Self::Error> {
        Ok(())
    }
}