Skip to content

Commit

Permalink
Merge pull request #774 from stm32-rs/spi-clean
Browse files Browse the repository at this point in the history
Clean SPI write impls
  • Loading branch information
burrbull authored Jul 27, 2024
2 parents 5fc93ec + 9bee731 commit d4e41b1
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 39 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
- Use `stm32f4-staging` until `stm32f4` is released [#706]
- RTIC2 monotonics fix: CC1 instead of CC3
- Allow different lengths of buffers in hal_1 SpiBus impl [#566]
- Clean SPI write impls
- `steal` UART peripheral on `Rx::new`

[#248]: https://github.com/stm32-rs/stm32f4xx-hal/pull/248
Expand Down
62 changes: 23 additions & 39 deletions src/spi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -797,6 +797,26 @@ impl<SPI: Instance> Inner<SPI> {
nb::Error::WouldBlock
})
}

fn spi_write<const BIDI: bool, W: FrameSize>(
&mut self,
words: impl IntoIterator<Item = W>,
) -> Result<(), Error> {
if BIDI {
self.bidi_output();
for word in words.into_iter() {
nb::block!(self.check_send(word))?;
}
} else {
for word in words.into_iter() {
nb::block!(self.check_send(word))?;
nb::block!(self.check_read::<W>())?;
}
}

Ok(())
}

fn listen_event(&mut self, disable: Option<BitFlags<Event>>, enable: Option<BitFlags<Event>>) {
self.spi.cr2().modify(|r, w| unsafe {
w.bits({
Expand Down Expand Up @@ -985,35 +1005,11 @@ impl<SPI: Instance, const BIDI: bool, W: FrameSize> Spi<SPI, BIDI, W> {
}

pub fn write(&mut self, words: &[W]) -> Result<(), Error> {
if BIDI {
self.bidi_output();
for word in words {
nb::block!(self.check_send(*word))?;
}
} else {
for word in words {
nb::block!(self.check_send(*word))?;
nb::block!(self.check_read::<W>())?;
}
}

Ok(())
self.spi_write::<BIDI, W>(words.iter().copied())
}

pub fn write_iter(&mut self, words: impl IntoIterator<Item = W>) -> Result<(), Error> {
if BIDI {
self.bidi_output();
for word in words.into_iter() {
nb::block!(self.check_send(word))?;
}
} else {
for word in words.into_iter() {
nb::block!(self.check_send(word))?;
nb::block!(self.check_read::<W>())?;
}
}

Ok(())
self.spi_write::<BIDI, W>(words)
}

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

pub fn write(&mut self, words: &[W]) -> Result<(), Error> {
if BIDI {
self.bidi_output();
for word in words {
nb::block!(self.check_send(*word))?;
}
} else {
for word in words {
nb::block!(self.check_send(*word))?;
nb::block!(self.check_read::<W>())?;
}
}

Ok(())
self.spi_write::<BIDI, W>(words.iter().copied())
}

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

0 comments on commit d4e41b1

Please sign in to comment.