From e36ae76e4518465953cf1adfb5c40f8cd91667d6 Mon Sep 17 00:00:00 2001 From: Bob McWhirter Date: Tue, 24 Aug 2021 14:44:31 -0400 Subject: [PATCH] Fix blocking-write for SPI. --- embassy-stm32/src/spi/v3.rs | 30 +++++++++++++++++++++++++++--- 1 file changed, 27 insertions(+), 3 deletions(-) diff --git a/embassy-stm32/src/spi/v3.rs b/embassy-stm32/src/spi/v3.rs index cfee54da..dd8c1c2f 100644 --- a/embassy-stm32/src/spi/v3.rs +++ b/embassy-stm32/src/spi/v3.rs @@ -171,6 +171,7 @@ impl<'d, T: Instance, Tx, Rx> Spi<'d, T, Tx, Rx> { w.set_dsize(word_size.dsize()); }); T::regs().cr1().modify(|w| { + w.set_csusp(false); w.set_spe(true); }); } @@ -375,8 +376,19 @@ impl<'d, T: Instance> embedded_hal::blocking::spi::Write for Spi<'d, T, NoDm } if !sr.txp() { // 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(()) @@ -469,8 +481,20 @@ impl<'d, T: Instance> embedded_hal::blocking::spi::Write for Spi<'d, T, NoD } if !sr.txp() { // 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(()) @@ -537,7 +561,7 @@ impl<'d, T: Instance, Tx, Rx> traits::Spi for Spi<'d, T, Tx, Rx> { impl<'d, T: Instance, Tx: TxDmaChannel, Rx> traits::Write for Spi<'d, T, Tx, Rx> { #[rustfmt::skip] - type WriteFuture<'a> where Self: 'a = impl Future> + 'a; + type WriteFuture<'a> where Self: 'a = impl Future> + 'a; fn write<'a>(&'a mut self, data: &'a [u8]) -> Self::WriteFuture<'a> { self.write_dma_u8(data) @@ -548,7 +572,7 @@ impl<'d, T: Instance, Tx: TxDmaChannel, Rx: RxDmaChannel> traits::Read for Spi<'d, T, Tx, Rx> { #[rustfmt::skip] - type ReadFuture<'a> where Self: 'a = impl Future> + 'a; + type ReadFuture<'a> where Self: 'a = impl Future> + 'a; fn read<'a>(&'a mut self, data: &'a mut [u8]) -> Self::ReadFuture<'a> { self.read_dma_u8(data) @@ -559,7 +583,7 @@ impl<'d, T: Instance, Tx: TxDmaChannel, Rx: RxDmaChannel> traits::FullDupl for Spi<'d, T, Tx, Rx> { #[rustfmt::skip] - type WriteReadFuture<'a> where Self: 'a = impl Future> + 'a; + type WriteReadFuture<'a> where Self: 'a = impl Future> + 'a; fn read_write<'a>( &'a mut self,