diff options
| author | Dario Nieuwenhuis <[email protected]> | 2022-03-15 04:13:33 +0100 |
|---|---|---|
| committer | Dario Nieuwenhuis <[email protected]> | 2022-03-15 04:13:33 +0100 |
| commit | 1dc618f0e4e045de63007893fb36fcaba82acfa4 (patch) | |
| tree | 50fb3b7175ceb0063dc440ec7221ff8399532981 | |
| parent | 49ef19c0b284eeb6d97cec597bbb1f82d18397c5 (diff) | |
stm32/spi: fix blocking transfer hanging after async.
| -rw-r--r-- | embassy-stm32/src/spi/mod.rs | 4 | ||||
| -rw-r--r-- | tests/stm32/src/bin/spi_dma.rs | 16 |
2 files changed, 20 insertions, 0 deletions
diff --git a/embassy-stm32/src/spi/mod.rs b/embassy-stm32/src/spi/mod.rs index a743a036c..764a967ca 100644 --- a/embassy-stm32/src/spi/mod.rs +++ b/embassy-stm32/src/spi/mod.rs | |||
| @@ -575,6 +575,7 @@ impl<'d, T: Instance, Tx, Rx> Spi<'d, T, Tx, Rx> { | |||
| 575 | } | 575 | } |
| 576 | 576 | ||
| 577 | pub fn blocking_write<W: Word>(&mut self, words: &[W]) -> Result<(), Error> { | 577 | pub fn blocking_write<W: Word>(&mut self, words: &[W]) -> Result<(), Error> { |
| 578 | unsafe { T::REGS.cr1().modify(|w| w.set_spe(true)) } | ||
| 578 | flush_rx_fifo(T::REGS); | 579 | flush_rx_fifo(T::REGS); |
| 579 | self.set_word_size(W::WORDSIZE); | 580 | self.set_word_size(W::WORDSIZE); |
| 580 | for word in words.iter() { | 581 | for word in words.iter() { |
| @@ -584,6 +585,7 @@ impl<'d, T: Instance, Tx, Rx> Spi<'d, T, Tx, Rx> { | |||
| 584 | } | 585 | } |
| 585 | 586 | ||
| 586 | pub fn blocking_read<W: Word>(&mut self, words: &mut [W]) -> Result<(), Error> { | 587 | pub fn blocking_read<W: Word>(&mut self, words: &mut [W]) -> Result<(), Error> { |
| 588 | unsafe { T::REGS.cr1().modify(|w| w.set_spe(true)) } | ||
| 587 | flush_rx_fifo(T::REGS); | 589 | flush_rx_fifo(T::REGS); |
| 588 | self.set_word_size(W::WORDSIZE); | 590 | self.set_word_size(W::WORDSIZE); |
| 589 | for word in words.iter_mut() { | 591 | for word in words.iter_mut() { |
| @@ -593,6 +595,7 @@ impl<'d, T: Instance, Tx, Rx> Spi<'d, T, Tx, Rx> { | |||
| 593 | } | 595 | } |
| 594 | 596 | ||
| 595 | pub fn blocking_transfer_in_place<W: Word>(&mut self, words: &mut [W]) -> Result<(), Error> { | 597 | pub fn blocking_transfer_in_place<W: Word>(&mut self, words: &mut [W]) -> Result<(), Error> { |
| 598 | unsafe { T::REGS.cr1().modify(|w| w.set_spe(true)) } | ||
| 596 | flush_rx_fifo(T::REGS); | 599 | flush_rx_fifo(T::REGS); |
| 597 | self.set_word_size(W::WORDSIZE); | 600 | self.set_word_size(W::WORDSIZE); |
| 598 | for word in words.iter_mut() { | 601 | for word in words.iter_mut() { |
| @@ -602,6 +605,7 @@ impl<'d, T: Instance, Tx, Rx> Spi<'d, T, Tx, Rx> { | |||
| 602 | } | 605 | } |
| 603 | 606 | ||
| 604 | pub fn blocking_transfer<W: Word>(&mut self, read: &mut [W], write: &[W]) -> Result<(), Error> { | 607 | pub fn blocking_transfer<W: Word>(&mut self, read: &mut [W], write: &[W]) -> Result<(), Error> { |
| 608 | unsafe { T::REGS.cr1().modify(|w| w.set_spe(true)) } | ||
| 605 | flush_rx_fifo(T::REGS); | 609 | flush_rx_fifo(T::REGS); |
| 606 | self.set_word_size(W::WORDSIZE); | 610 | self.set_word_size(W::WORDSIZE); |
| 607 | let len = read.len().max(write.len()); | 611 | let len = read.len().max(write.len()); |
diff --git a/tests/stm32/src/bin/spi_dma.rs b/tests/stm32/src/bin/spi_dma.rs index 3e9521ae7..f4f1994c0 100644 --- a/tests/stm32/src/bin/spi_dma.rs +++ b/tests/stm32/src/bin/spi_dma.rs | |||
| @@ -68,6 +68,22 @@ async fn main(_spawner: Spawner, p: Peripherals) { | |||
| 68 | spi.read::<u8>(&mut []).await.unwrap(); | 68 | spi.read::<u8>(&mut []).await.unwrap(); |
| 69 | spi.write::<u8>(&[]).await.unwrap(); | 69 | spi.write::<u8>(&[]).await.unwrap(); |
| 70 | 70 | ||
| 71 | // === Check mixing blocking with async. | ||
| 72 | spi.blocking_transfer(&mut buf, &data).unwrap(); | ||
| 73 | assert_eq!(buf, data); | ||
| 74 | spi.transfer(&mut buf, &data).await.unwrap(); | ||
| 75 | assert_eq!(buf, data); | ||
| 76 | spi.blocking_write(&buf).unwrap(); | ||
| 77 | spi.transfer(&mut buf, &data).await.unwrap(); | ||
| 78 | assert_eq!(buf, data); | ||
| 79 | spi.blocking_read(&mut buf).unwrap(); | ||
| 80 | spi.blocking_write(&buf).unwrap(); | ||
| 81 | spi.write(&buf).await.unwrap(); | ||
| 82 | spi.read(&mut buf).await.unwrap(); | ||
| 83 | spi.blocking_write(&buf).unwrap(); | ||
| 84 | spi.blocking_read(&mut buf).unwrap(); | ||
| 85 | spi.write(&buf).await.unwrap(); | ||
| 86 | |||
| 71 | info!("Test OK"); | 87 | info!("Test OK"); |
| 72 | cortex_m::asm::bkpt(); | 88 | cortex_m::asm::bkpt(); |
| 73 | } | 89 | } |
