Fix blocking-write for SPI.

This commit is contained in:
Bob McWhirter 2021-08-24 14:44:31 -04:00
parent 548593ea41
commit e36ae76e45

View file

@ -171,6 +171,7 @@ impl<'d, T: Instance, Tx, Rx> Spi<'d, T, Tx, Rx> {
w.set_dsize(word_size.dsize()); w.set_dsize(word_size.dsize());
}); });
T::regs().cr1().modify(|w| { T::regs().cr1().modify(|w| {
w.set_csusp(false);
w.set_spe(true); w.set_spe(true);
}); });
} }
@ -375,8 +376,19 @@ impl<'d, T: Instance> embedded_hal::blocking::spi::Write<u8> for Spi<'d, T, NoDm
} }
if !sr.txp() { if !sr.txp() {
// loop waiting for TXE // loop waiting for TXE
continue;
} }
break;
} }
unsafe {
let rxdr = regs.rxdr().ptr() as *const u8;
// discard read to prevent pverrun.
let _ = ptr::read_volatile(rxdr);
}
}
while unsafe { !regs.sr().read().txc() } {
// spin
} }
Ok(()) Ok(())
@ -469,8 +481,20 @@ impl<'d, T: Instance> embedded_hal::blocking::spi::Write<u16> for Spi<'d, T, NoD
} }
if !sr.txp() { if !sr.txp() {
// loop waiting for TXE // loop waiting for TXE
continue;
} }
break;
} }
unsafe {
let rxdr = regs.rxdr().ptr() as *const u8;
// discard read to prevent pverrun.
let _ = ptr::read_volatile(rxdr);
}
}
while unsafe { !regs.sr().read().txc() } {
// spin
} }
Ok(()) Ok(())
@ -537,7 +561,7 @@ impl<'d, T: Instance, Tx, Rx> traits::Spi<u8> for Spi<'d, T, Tx, Rx> {
impl<'d, T: Instance, Tx: TxDmaChannel<T>, Rx> traits::Write<u8> for Spi<'d, T, Tx, Rx> { impl<'d, T: Instance, Tx: TxDmaChannel<T>, Rx> traits::Write<u8> for Spi<'d, T, Tx, Rx> {
#[rustfmt::skip] #[rustfmt::skip]
type WriteFuture<'a> where Self: 'a = impl Future<Output = Result<(), Self::Error>> + 'a; type WriteFuture<'a> where Self: 'a = impl Future<Output=Result<(), Self::Error>> + 'a;
fn write<'a>(&'a mut self, data: &'a [u8]) -> Self::WriteFuture<'a> { fn write<'a>(&'a mut self, data: &'a [u8]) -> Self::WriteFuture<'a> {
self.write_dma_u8(data) self.write_dma_u8(data)
@ -548,7 +572,7 @@ impl<'d, T: Instance, Tx: TxDmaChannel<T>, Rx: RxDmaChannel<T>> traits::Read<u8>
for Spi<'d, T, Tx, Rx> for Spi<'d, T, Tx, Rx>
{ {
#[rustfmt::skip] #[rustfmt::skip]
type ReadFuture<'a> where Self: 'a = impl Future<Output = Result<(), Self::Error>> + 'a; type ReadFuture<'a> where Self: 'a = impl Future<Output=Result<(), Self::Error>> + 'a;
fn read<'a>(&'a mut self, data: &'a mut [u8]) -> Self::ReadFuture<'a> { fn read<'a>(&'a mut self, data: &'a mut [u8]) -> Self::ReadFuture<'a> {
self.read_dma_u8(data) self.read_dma_u8(data)
@ -559,7 +583,7 @@ impl<'d, T: Instance, Tx: TxDmaChannel<T>, Rx: RxDmaChannel<T>> traits::FullDupl
for Spi<'d, T, Tx, Rx> for Spi<'d, T, Tx, Rx>
{ {
#[rustfmt::skip] #[rustfmt::skip]
type WriteReadFuture<'a> where Self: 'a = impl Future<Output = Result<(), Self::Error>> + 'a; type WriteReadFuture<'a> where Self: 'a = impl Future<Output=Result<(), Self::Error>> + 'a;
fn read_write<'a>( fn read_write<'a>(
&'a mut self, &'a mut self,