Skip to content

Commit d4e41b1

Browse files
authored
Merge pull request #774 from stm32-rs/spi-clean
Clean SPI write impls
2 parents 5fc93ec + 9bee731 commit d4e41b1

File tree

2 files changed

+24
-39
lines changed

2 files changed

+24
-39
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
2121
- Use `stm32f4-staging` until `stm32f4` is released [#706]
2222
- RTIC2 monotonics fix: CC1 instead of CC3
2323
- Allow different lengths of buffers in hal_1 SpiBus impl [#566]
24+
- Clean SPI write impls
2425
- `steal` UART peripheral on `Rx::new`
2526

2627
[#248]: https://github.com/stm32-rs/stm32f4xx-hal/pull/248

src/spi.rs

Lines changed: 23 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -797,6 +797,26 @@ impl<SPI: Instance> Inner<SPI> {
797797
nb::Error::WouldBlock
798798
})
799799
}
800+
801+
fn spi_write<const BIDI: bool, W: FrameSize>(
802+
&mut self,
803+
words: impl IntoIterator<Item = W>,
804+
) -> Result<(), Error> {
805+
if BIDI {
806+
self.bidi_output();
807+
for word in words.into_iter() {
808+
nb::block!(self.check_send(word))?;
809+
}
810+
} else {
811+
for word in words.into_iter() {
812+
nb::block!(self.check_send(word))?;
813+
nb::block!(self.check_read::<W>())?;
814+
}
815+
}
816+
817+
Ok(())
818+
}
819+
800820
fn listen_event(&mut self, disable: Option<BitFlags<Event>>, enable: Option<BitFlags<Event>>) {
801821
self.spi.cr2().modify(|r, w| unsafe {
802822
w.bits({
@@ -985,35 +1005,11 @@ impl<SPI: Instance, const BIDI: bool, W: FrameSize> Spi<SPI, BIDI, W> {
9851005
}
9861006

9871007
pub fn write(&mut self, words: &[W]) -> Result<(), Error> {
988-
if BIDI {
989-
self.bidi_output();
990-
for word in words {
991-
nb::block!(self.check_send(*word))?;
992-
}
993-
} else {
994-
for word in words {
995-
nb::block!(self.check_send(*word))?;
996-
nb::block!(self.check_read::<W>())?;
997-
}
998-
}
999-
1000-
Ok(())
1008+
self.spi_write::<BIDI, W>(words.iter().copied())
10011009
}
10021010

10031011
pub fn write_iter(&mut self, words: impl IntoIterator<Item = W>) -> Result<(), Error> {
1004-
if BIDI {
1005-
self.bidi_output();
1006-
for word in words.into_iter() {
1007-
nb::block!(self.check_send(word))?;
1008-
}
1009-
} else {
1010-
for word in words.into_iter() {
1011-
nb::block!(self.check_send(word))?;
1012-
nb::block!(self.check_read::<W>())?;
1013-
}
1014-
}
1015-
1016-
Ok(())
1012+
self.spi_write::<BIDI, W>(words)
10171013
}
10181014

10191015
pub fn read(&mut self, words: &mut [W]) -> Result<(), Error> {
@@ -1093,19 +1089,7 @@ impl<SPI: Instance, const BIDI: bool, W: FrameSize> SpiSlave<SPI, BIDI, W> {
10931089
}
10941090

10951091
pub fn write(&mut self, words: &[W]) -> Result<(), Error> {
1096-
if BIDI {
1097-
self.bidi_output();
1098-
for word in words {
1099-
nb::block!(self.check_send(*word))?;
1100-
}
1101-
} else {
1102-
for word in words {
1103-
nb::block!(self.check_send(*word))?;
1104-
nb::block!(self.check_read::<W>())?;
1105-
}
1106-
}
1107-
1108-
Ok(())
1092+
self.spi_write::<BIDI, W>(words.iter().copied())
11091093
}
11101094

11111095
pub fn read(&mut self, words: &mut [W]) -> Result<(), Error> {

0 commit comments

Comments
 (0)