Implemented frame transfer for four-wire bus

This commit is contained in:
Jonah Dahlquist 2019-08-07 19:39:28 -05:00 committed by Jonah Dahlquist
commit 16e813e45b
4 changed files with 42 additions and 7 deletions

View file

@ -1,3 +1,4 @@
use byteorder::{BigEndian, ByteOrder};
use embedded_hal::digital::v2::OutputPin;
use embedded_hal::spi::FullDuplex;
@ -32,11 +33,23 @@ pub struct ActiveFourWire<Spi: FullDuplex<u8>, ChipSelect: OutputPin> {
impl<Spi: FullDuplex<u8>, ChipSelect: OutputPin> ActiveBus for ActiveFourWire<Spi, ChipSelect> {
type Error = FourWireError<Spi::Error, ChipSelect::Error>;
fn transfer_frame<'a>(
address_phase: [u8; 2],
control_phase: u8,
&mut self,
address_phase: u16,
mut control_phase: u8,
data_phase: &'a mut [u8],
) -> Result<&'a mut [u8], nb::Error<Self::Error>> {
// TODO implement transfer
let mut address_bytes = [0u8; 2];
BigEndian::write_u16(&mut address_bytes, address_phase);
self.cs
.set_high()
.map_err(|e| Self::Error::ChipSelectError(e))?;
block!(Self::transfer_bytes(&mut self.spi, &mut address_bytes)
.and_then(|_| Self::transfer_byte(&mut self.spi, &mut control_phase))
.and_then(|_| Self::transfer_bytes(&mut self.spi, data_phase)))
.map_err(|e| Self::Error::SpiError(e))?;
self.cs
.set_low()
.map_err(|e| Self::Error::ChipSelectError(e))?;
Ok(data_phase)
}
}