2021-05-10 20:17:58 +00:00
|
|
|
#![macro_use]
|
|
|
|
|
2021-05-10 19:21:57 +00:00
|
|
|
pub use embedded_hal::spi::{Mode, Phase, Polarity, MODE_0, MODE_1, MODE_2, MODE_3};
|
|
|
|
use core::marker::PhantomData;
|
|
|
|
use embassy::interrupt::Interrupt;
|
2021-05-10 20:17:58 +00:00
|
|
|
use embedded_hal::blocking::spi::{Write, Transfer};
|
|
|
|
//use crate::pac::spi;
|
2021-05-10 19:21:57 +00:00
|
|
|
|
|
|
|
pub struct Spi<'d, T: Instance> {
|
|
|
|
peri: T,
|
2021-05-10 20:17:58 +00:00
|
|
|
//irq: T::Interrupt,
|
2021-05-10 19:21:57 +00:00
|
|
|
phantom: PhantomData<&'d mut T>,
|
|
|
|
}
|
|
|
|
|
|
|
|
pub enum Error {
|
2021-05-10 20:17:58 +00:00
|
|
|
Framing,
|
|
|
|
Crc,
|
|
|
|
Overrun,
|
2021-05-10 19:21:57 +00:00
|
|
|
}
|
|
|
|
|
2021-05-10 20:17:58 +00:00
|
|
|
impl<'d, T: Instance> embedded_hal::blocking::spi::Write<u8> for Spi<'d, T> {
|
2021-05-10 19:21:57 +00:00
|
|
|
type Error = Error;
|
|
|
|
|
|
|
|
fn write(&mut self, words: &[u8]) -> Result<(), Self::Error> {
|
2021-05-10 20:17:58 +00:00
|
|
|
let regs = T::regs();
|
2021-05-10 19:21:57 +00:00
|
|
|
|
2021-05-10 20:17:58 +00:00
|
|
|
for word in words.iter() {
|
|
|
|
while unsafe { !regs.sr().read().txe() } {
|
|
|
|
// spin
|
|
|
|
}
|
|
|
|
unsafe {
|
|
|
|
regs.dr().write(|reg| reg.0 = *word as u32);
|
|
|
|
}
|
|
|
|
loop {
|
|
|
|
let sr = unsafe { regs.sr().read() };
|
|
|
|
if sr.fre() {
|
|
|
|
return Err(Error::Framing);
|
|
|
|
}
|
|
|
|
if sr.ovr() {
|
|
|
|
return Err(Error::Overrun);
|
|
|
|
}
|
|
|
|
if sr.crcerr() {
|
|
|
|
return Err(Error::Crc);
|
|
|
|
}
|
|
|
|
if !sr.txe() {
|
|
|
|
// loop waiting for TXE
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(())
|
2021-05-10 19:21:57 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-05-10 20:17:58 +00:00
|
|
|
impl<'d, T: Instance> embedded_hal::blocking::spi::Transfer<u8> for Spi<'d, T> {
|
|
|
|
type Error = Error;
|
2021-05-10 19:21:57 +00:00
|
|
|
|
2021-05-10 20:17:58 +00:00
|
|
|
fn transfer<'w>(&mut self, words: &'w mut [u8]) -> Result<&'w [u8], Self::Error> {
|
|
|
|
let regs = T::regs();
|
2021-05-10 19:21:57 +00:00
|
|
|
|
2021-05-10 20:17:58 +00:00
|
|
|
for word in words.iter_mut() {
|
|
|
|
while unsafe { !regs.sr().read().txe() } {
|
|
|
|
// spin
|
|
|
|
}
|
|
|
|
unsafe {
|
|
|
|
regs.dr().write(|reg| reg.0 = *word as u32);
|
|
|
|
}
|
|
|
|
while unsafe { !regs.sr().read().rxne() } {
|
|
|
|
// spin waiting for inbound to shift in.
|
|
|
|
}
|
|
|
|
*word = unsafe { regs.dr().read().0 as u8 };
|
|
|
|
loop {
|
|
|
|
let sr = unsafe { regs.sr().read() };
|
|
|
|
if sr.fre() {
|
|
|
|
return Err(Error::Framing);
|
|
|
|
}
|
|
|
|
if sr.ovr() {
|
|
|
|
return Err(Error::Overrun);
|
|
|
|
}
|
|
|
|
if sr.crcerr() {
|
|
|
|
return Err(Error::Crc);
|
|
|
|
}
|
|
|
|
if !sr.txe() {
|
|
|
|
// loop waiting for TXE
|
|
|
|
}
|
2021-05-10 19:21:57 +00:00
|
|
|
}
|
|
|
|
}
|
2021-05-10 20:17:58 +00:00
|
|
|
|
|
|
|
Ok(words)
|
2021-05-10 19:21:57 +00:00
|
|
|
}
|
2021-05-10 20:17:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
pub(crate) mod sealed {
|
|
|
|
use super::*;
|
|
|
|
use embassy::util::AtomicWaker;
|
|
|
|
|
|
|
|
//pub struct State {
|
|
|
|
//pub end_waker: AtomicWaker,
|
|
|
|
//}
|
|
|
|
|
|
|
|
//impl State {
|
|
|
|
//pub const fn new() -> Self {
|
|
|
|
//Self {
|
|
|
|
//end_waker: AtomicWaker::new(),
|
|
|
|
//}
|
|
|
|
//}
|
|
|
|
//}
|
2021-05-10 19:21:57 +00:00
|
|
|
|
|
|
|
pub trait Instance {
|
|
|
|
fn regs() -> &'static crate::pac::spi::Spi;
|
2021-05-10 20:17:58 +00:00
|
|
|
//fn state() -> &'static State;
|
2021-05-10 19:21:57 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub trait Instance: sealed::Instance + 'static {
|
2021-05-10 20:17:58 +00:00
|
|
|
//type Interrupt: Interrupt;
|
2021-05-10 19:21:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
macro_rules! impl_spi {
|
|
|
|
($inst:ident) => {
|
|
|
|
impl crate::spi::sealed::Instance for peripherals::$inst {
|
2021-05-10 20:17:58 +00:00
|
|
|
fn regs() -> &'static crate::pac::spi::Spi {
|
|
|
|
&crate::pac::$inst
|
2021-05-10 19:21:57 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl crate::spi::Instance for peripherals::$inst {}
|
|
|
|
};
|
|
|
|
}
|