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::digital::v2::OutputPin;
use embedded_hal::spi::FullDuplex; 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> { impl<Spi: FullDuplex<u8>, ChipSelect: OutputPin> ActiveBus for ActiveFourWire<Spi, ChipSelect> {
type Error = FourWireError<Spi::Error, ChipSelect::Error>; type Error = FourWireError<Spi::Error, ChipSelect::Error>;
fn transfer_frame<'a>( fn transfer_frame<'a>(
address_phase: [u8; 2], &mut self,
control_phase: u8, address_phase: u16,
mut control_phase: u8,
data_phase: &'a mut [u8], data_phase: &'a mut [u8],
) -> Result<&'a mut [u8], nb::Error<Self::Error>> { ) -> 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) Ok(data_phase)
} }
} }

View file

@ -1,3 +1,4 @@
use embedded_hal::spi::FullDuplex;
use nb::Result; use nb::Result;
mod four_wire; mod four_wire;
@ -12,9 +13,29 @@ pub trait Bus {}
pub trait ActiveBus { pub trait ActiveBus {
type Error; type Error;
fn transfer_frame<'a>( fn transfer_frame<'a>(
address_phase: [u8; 2], &mut self,
address_phase: u16,
control_phase: u8, control_phase: u8,
data_phase: &'a mut [u8], data_phase: &'a mut [u8],
) -> Result<&'a mut [u8], Self::Error>; ) -> Result<&'a mut [u8], Self::Error>;
fn transfer_bytes<'a, Spi: FullDuplex<u8>>(
spi: &mut Spi,
bytes: &'a mut [u8],
) -> Result<&'a mut [u8], Spi::Error> {
for byte in bytes.iter_mut() {
Self::transfer_byte(spi, byte)?;
}
Ok(bytes)
}
fn transfer_byte<'a, Spi: FullDuplex<u8>>(
spi: &mut Spi,
byte: &'a mut u8,
) -> Result<&'a mut u8, Spi::Error> {
*byte = spi.send(*byte).and_then(|_| spi.read())?;
Ok(byte)
}
} }

View file

@ -25,7 +25,8 @@ pub struct ActiveThreeWire<Spi: FullDuplex<u8>> {
impl<Spi: FullDuplex<u8>> ActiveBus for ActiveThreeWire<Spi> { impl<Spi: FullDuplex<u8>> ActiveBus for ActiveThreeWire<Spi> {
type Error = Spi::Error; type Error = Spi::Error;
fn transfer_frame<'a>( fn transfer_frame<'a>(
address_phase: [u8; 2], &mut self,
address_phase: u16,
control_phase: u8, control_phase: u8,
data_phase: &'a mut [u8], data_phase: &'a mut [u8],
) -> Result<&'a mut [u8], nb::Error<Self::Error>> { ) -> Result<&'a mut [u8], nb::Error<Self::Error>> {

View file

@ -2,10 +2,10 @@
// #![allow(unused)] // #![allow(unused)]
#![deny(broken_intra_doc_links)] #![deny(broken_intra_doc_links)]
// extern crate byteorder; extern crate byteorder;
extern crate embedded_hal; extern crate embedded_hal;
// #[macro_use(block)] #[macro_use(block)]
extern crate nb; extern crate nb;
// use hal::digital::v2::OutputPin; // use hal::digital::v2::OutputPin;