humpback-dds/src/spi_slave.rs

66 lines
1.7 KiB
Rust
Raw Normal View History

2020-08-10 17:04:40 +08:00
use stm32h7xx_hal::{
hal::{
digital::v2::{
InputPin,
OutputPin,
},
blocking::spi::Transfer,
},
pac,
prelude::*,
spi,
};
use core::marker::PhantomData;
2020-08-10 18:06:15 +08:00
use crate::{DoOnGetRefMutData, Error, SelectChip};
2020-08-10 17:04:40 +08:00
pub struct SPISlave<'a, DEV: 'a, SPI, CS0, CS1, CS2> (
&'a DEV,
u8,
PhantomData<(SPI, CS0, CS1, CS2)>,
);
pub struct Parts<'a, DEV: 'a, SPI, CS0, CS1, CS2> {
pub spi1: SPISlave<'a, DEV, SPI, CS0, CS1, CS2>,
pub spi2: SPISlave<'a, DEV, SPI, CS0, CS1, CS2>,
pub spi3: SPISlave<'a, DEV, SPI, CS0, CS1, CS2>,
pub spi4: SPISlave<'a, DEV, SPI, CS0, CS1, CS2>,
pub spi5: SPISlave<'a, DEV, SPI, CS0, CS1, CS2>,
pub spi6: SPISlave<'a, DEV, SPI, CS0, CS1, CS2>,
pub spi7: SPISlave<'a, DEV, SPI, CS0, CS1, CS2>,
}
impl<'a, DEV, SPI, CS0, CS1, CS2> Parts<'a, DEV, SPI, CS0, CS1, CS2> {
pub(crate) fn new(cpld: &'a DEV) -> Self {
Parts {
// spi0: SPISlave(&cpld, 0, PhantomData),
spi1: SPISlave(&cpld, 1, PhantomData),
spi2: SPISlave(&cpld, 2, PhantomData),
spi3: SPISlave(&cpld, 3, PhantomData),
spi4: SPISlave(&cpld, 4, PhantomData),
spi5: SPISlave(&cpld, 5, PhantomData),
spi6: SPISlave(&cpld, 6, PhantomData),
spi7: SPISlave(&cpld, 7, PhantomData),
}
}
}
impl<'a, DEV, SPI, CS0, CS1, CS2, E> Transfer<u8> for SPISlave<'a, DEV, SPI, CS0, CS1, CS2>
where
CS2: OutputPin,
CS1: OutputPin,
CS0: OutputPin,
DEV: DoOnGetRefMutData<SPI, CS0, CS1, CS2>,
SPI: Transfer<u8, Error = E>,
{
type Error = Error<E>;
fn transfer<'w>(&mut self, words: &'w mut[u8]) -> Result<&'w [u8], Self::Error> {
self.0.do_on_get_ref_mut_data(|mut dev| {
dev.select_chip(self.1)
})?;
self.0.do_on_get_ref_mut_data(move |mut dev| dev.spi.transfer(words).map_err(Error::SPI))
}
}