From aeb69a76651b2a06cc74b15ee926a0797a3536f4 Mon Sep 17 00:00:00 2001 From: Grant Miller Date: Mon, 6 Dec 2021 14:24:02 -0600 Subject: [PATCH] Track current word size in v2 and v3 also --- embassy-stm32/src/spi/v1.rs | 2 +- embassy-stm32/src/spi/v2.rs | 22 ++++++++++++++-------- embassy-stm32/src/spi/v3.rs | 22 ++++++++++++++-------- 3 files changed, 29 insertions(+), 17 deletions(-) diff --git a/embassy-stm32/src/spi/v1.rs b/embassy-stm32/src/spi/v1.rs index 08bee758b..a41c79913 100644 --- a/embassy-stm32/src/spi/v1.rs +++ b/embassy-stm32/src/spi/v1.rs @@ -140,8 +140,8 @@ impl<'d, T: Instance, Tx, Rx> Spi<'d, T, Tx, Rx> { T::regs().cr1().modify(|reg| { reg.set_spe(true); }); - self.current_word_size = word_size; } + self.current_word_size = word_size; } #[allow(unused)] diff --git a/embassy-stm32/src/spi/v2.rs b/embassy-stm32/src/spi/v2.rs index b281c7548..0c0d24ce9 100644 --- a/embassy-stm32/src/spi/v2.rs +++ b/embassy-stm32/src/spi/v2.rs @@ -24,6 +24,7 @@ pub struct Spi<'d, T: Instance, Tx, Rx> { miso: Option, txdma: Tx, rxdma: Rx, + current_word_size: WordSize, phantom: PhantomData<&'d mut T>, } @@ -107,6 +108,7 @@ impl<'d, T: Instance, Tx, Rx> Spi<'d, T, Tx, Rx> { miso, txdma, rxdma, + current_word_size: WordSize::EightBit, phantom: PhantomData, } } @@ -125,7 +127,10 @@ impl<'d, T: Instance, Tx, Rx> Spi<'d, T, Tx, Rx> { } } - fn set_word_size(word_size: WordSize) { + fn set_word_size(&mut self, word_size: WordSize) { + if self.current_word_size == word_size { + return; + } unsafe { T::regs().cr1().modify(|w| { w.set_spe(false); @@ -138,6 +143,7 @@ impl<'d, T: Instance, Tx, Rx> Spi<'d, T, Tx, Rx> { w.set_spe(true); }); } + self.current_word_size = word_size; } #[allow(unused)] @@ -150,7 +156,7 @@ impl<'d, T: Instance, Tx, Rx> Spi<'d, T, Tx, Rx> { w.set_spe(false); }); } - Self::set_word_size(WordSize::EightBit); + self.set_word_size(WordSize::EightBit); let request = self.txdma.request(); let dst = T::regs().dr().ptr() as *mut u8; @@ -192,7 +198,7 @@ impl<'d, T: Instance, Tx, Rx> Spi<'d, T, Tx, Rx> { reg.set_rxdmaen(true); }); } - Self::set_word_size(WordSize::EightBit); + self.set_word_size(WordSize::EightBit); let clock_byte_count = read.len(); @@ -252,7 +258,7 @@ impl<'d, T: Instance, Tx, Rx> Spi<'d, T, Tx, Rx> { let _ = T::regs().dr().read(); } } - Self::set_word_size(WordSize::EightBit); + self.set_word_size(WordSize::EightBit); let rx_request = self.rxdma.request(); let rx_src = T::regs().dr().ptr() as *mut u8; @@ -365,7 +371,7 @@ impl<'d, T: Instance, Rx> embedded_hal::blocking::spi::Write for Spi<'d, T, type Error = Error; fn write(&mut self, words: &[u8]) -> Result<(), Self::Error> { - Self::set_word_size(WordSize::EightBit); + self.set_word_size(WordSize::EightBit); let regs = T::regs(); for word in words.iter() { @@ -381,7 +387,7 @@ impl<'d, T: Instance> embedded_hal::blocking::spi::Transfer for Spi<'d, T, N type Error = Error; fn transfer<'w>(&mut self, words: &'w mut [u8]) -> Result<&'w [u8], Self::Error> { - Self::set_word_size(WordSize::EightBit); + self.set_word_size(WordSize::EightBit); let regs = T::regs(); for word in words.iter_mut() { @@ -397,7 +403,7 @@ impl<'d, T: Instance, Rx> embedded_hal::blocking::spi::Write for Spi<'d, T, type Error = Error; fn write(&mut self, words: &[u16]) -> Result<(), Self::Error> { - Self::set_word_size(WordSize::SixteenBit); + self.set_word_size(WordSize::SixteenBit); let regs = T::regs(); for word in words.iter() { @@ -413,7 +419,7 @@ impl<'d, T: Instance> embedded_hal::blocking::spi::Transfer for Spi<'d, T, type Error = Error; fn transfer<'w>(&mut self, words: &'w mut [u16]) -> Result<&'w [u16], Self::Error> { - Self::set_word_size(WordSize::SixteenBit); + self.set_word_size(WordSize::SixteenBit); let regs = T::regs(); for word in words.iter_mut() { diff --git a/embassy-stm32/src/spi/v3.rs b/embassy-stm32/src/spi/v3.rs index cd88acb7a..e2751ce20 100644 --- a/embassy-stm32/src/spi/v3.rs +++ b/embassy-stm32/src/spi/v3.rs @@ -26,6 +26,7 @@ pub struct Spi<'d, T: Instance, Tx = NoDma, Rx = NoDma> { miso: Option, txdma: Tx, rxdma: Rx, + current_word_size: WordSize, phantom: PhantomData<&'d mut T>, } @@ -120,6 +121,7 @@ impl<'d, T: Instance, Tx, Rx> Spi<'d, T, Tx, Rx> { miso, txdma, rxdma, + current_word_size: WordSize::EightBit, phantom: PhantomData, } } @@ -138,7 +140,10 @@ impl<'d, T: Instance, Tx, Rx> Spi<'d, T, Tx, Rx> { } } - fn set_word_size(word_size: WordSize) { + fn set_word_size(&mut self, word_size: WordSize) { + if self.current_word_size == word_size { + return; + } unsafe { T::regs().cr1().modify(|w| { w.set_csusp(true); @@ -155,6 +160,7 @@ impl<'d, T: Instance, Tx, Rx> Spi<'d, T, Tx, Rx> { w.set_spe(true); }); } + self.current_word_size = word_size; } #[allow(unused)] @@ -162,7 +168,7 @@ impl<'d, T: Instance, Tx, Rx> Spi<'d, T, Tx, Rx> { where Tx: TxDmaChannel, { - Self::set_word_size(WordSize::EightBit); + self.set_word_size(WordSize::EightBit); unsafe { T::regs().cr1().modify(|w| { w.set_spe(false); @@ -204,7 +210,7 @@ impl<'d, T: Instance, Tx, Rx> Spi<'d, T, Tx, Rx> { Tx: TxDmaChannel, Rx: RxDmaChannel, { - Self::set_word_size(WordSize::EightBit); + self.set_word_size(WordSize::EightBit); unsafe { T::regs().cr1().modify(|w| { w.set_spe(false); @@ -260,7 +266,7 @@ impl<'d, T: Instance, Tx, Rx> Spi<'d, T, Tx, Rx> { { assert!(read.len() >= write.len()); - Self::set_word_size(WordSize::EightBit); + self.set_word_size(WordSize::EightBit); unsafe { T::regs().cr1().modify(|w| { w.set_spe(false); @@ -336,7 +342,7 @@ impl<'d, T: Instance> embedded_hal::blocking::spi::Write for Spi<'d, T, NoDm type Error = Error; fn write(&mut self, words: &[u8]) -> Result<(), Self::Error> { - Self::set_word_size(WordSize::EightBit); + self.set_word_size(WordSize::EightBit); let regs = T::regs(); for word in words.iter() { @@ -384,7 +390,7 @@ impl<'d, T: Instance> embedded_hal::blocking::spi::Transfer for Spi<'d, T, N type Error = Error; fn transfer<'w>(&mut self, words: &'w mut [u8]) -> Result<&'w [u8], Self::Error> { - Self::set_word_size(WordSize::EightBit); + self.set_word_size(WordSize::EightBit); let regs = T::regs(); for word in words.iter_mut() { @@ -441,7 +447,7 @@ impl<'d, T: Instance> embedded_hal::blocking::spi::Write for Spi<'d, T, NoD type Error = Error; fn write(&mut self, words: &[u16]) -> Result<(), Self::Error> { - Self::set_word_size(WordSize::SixteenBit); + self.set_word_size(WordSize::SixteenBit); let regs = T::regs(); for word in words.iter() { @@ -490,7 +496,7 @@ impl<'d, T: Instance> embedded_hal::blocking::spi::Transfer for Spi<'d, T, type Error = Error; fn transfer<'w>(&mut self, words: &'w mut [u16]) -> Result<&'w [u16], Self::Error> { - Self::set_word_size(WordSize::SixteenBit); + self.set_word_size(WordSize::SixteenBit); let regs = T::regs(); for word in words.iter_mut() {