stm32/spi: fix blocking transfer hanging after async.

This commit is contained in:
Dario Nieuwenhuis 2022-03-15 04:13:33 +01:00
parent 49ef19c0b2
commit 1dc618f0e4
2 changed files with 20 additions and 0 deletions

View File

@ -575,6 +575,7 @@ impl<'d, T: Instance, Tx, Rx> Spi<'d, T, Tx, Rx> {
} }
pub fn blocking_write<W: Word>(&mut self, words: &[W]) -> Result<(), Error> { pub fn blocking_write<W: Word>(&mut self, words: &[W]) -> Result<(), Error> {
unsafe { T::REGS.cr1().modify(|w| w.set_spe(true)) }
flush_rx_fifo(T::REGS); flush_rx_fifo(T::REGS);
self.set_word_size(W::WORDSIZE); self.set_word_size(W::WORDSIZE);
for word in words.iter() { for word in words.iter() {
@ -584,6 +585,7 @@ impl<'d, T: Instance, Tx, Rx> Spi<'d, T, Tx, Rx> {
} }
pub fn blocking_read<W: Word>(&mut self, words: &mut [W]) -> Result<(), Error> { pub fn blocking_read<W: Word>(&mut self, words: &mut [W]) -> Result<(), Error> {
unsafe { T::REGS.cr1().modify(|w| w.set_spe(true)) }
flush_rx_fifo(T::REGS); flush_rx_fifo(T::REGS);
self.set_word_size(W::WORDSIZE); self.set_word_size(W::WORDSIZE);
for word in words.iter_mut() { for word in words.iter_mut() {
@ -593,6 +595,7 @@ impl<'d, T: Instance, Tx, Rx> Spi<'d, T, Tx, Rx> {
} }
pub fn blocking_transfer_in_place<W: Word>(&mut self, words: &mut [W]) -> Result<(), Error> { pub fn blocking_transfer_in_place<W: Word>(&mut self, words: &mut [W]) -> Result<(), Error> {
unsafe { T::REGS.cr1().modify(|w| w.set_spe(true)) }
flush_rx_fifo(T::REGS); flush_rx_fifo(T::REGS);
self.set_word_size(W::WORDSIZE); self.set_word_size(W::WORDSIZE);
for word in words.iter_mut() { for word in words.iter_mut() {
@ -602,6 +605,7 @@ impl<'d, T: Instance, Tx, Rx> Spi<'d, T, Tx, Rx> {
} }
pub fn blocking_transfer<W: Word>(&mut self, read: &mut [W], write: &[W]) -> Result<(), Error> { pub fn blocking_transfer<W: Word>(&mut self, read: &mut [W], write: &[W]) -> Result<(), Error> {
unsafe { T::REGS.cr1().modify(|w| w.set_spe(true)) }
flush_rx_fifo(T::REGS); flush_rx_fifo(T::REGS);
self.set_word_size(W::WORDSIZE); self.set_word_size(W::WORDSIZE);
let len = read.len().max(write.len()); let len = read.len().max(write.len());

View File

@ -68,6 +68,22 @@ async fn main(_spawner: Spawner, p: Peripherals) {
spi.read::<u8>(&mut []).await.unwrap(); spi.read::<u8>(&mut []).await.unwrap();
spi.write::<u8>(&[]).await.unwrap(); spi.write::<u8>(&[]).await.unwrap();
// === Check mixing blocking with async.
spi.blocking_transfer(&mut buf, &data).unwrap();
assert_eq!(buf, data);
spi.transfer(&mut buf, &data).await.unwrap();
assert_eq!(buf, data);
spi.blocking_write(&buf).unwrap();
spi.transfer(&mut buf, &data).await.unwrap();
assert_eq!(buf, data);
spi.blocking_read(&mut buf).unwrap();
spi.blocking_write(&buf).unwrap();
spi.write(&buf).await.unwrap();
spi.read(&mut buf).await.unwrap();
spi.blocking_write(&buf).unwrap();
spi.blocking_read(&mut buf).unwrap();
spi.write(&buf).await.unwrap();
info!("Test OK"); info!("Test OK");
cortex_m::asm::bkpt(); cortex_m::asm::bkpt();
} }