stm32f4xx-hal icon indicating copy to clipboard operation
stm32f4xx-hal copied to clipboard

Allow different lengths of buffers in hal_1 SpiBus impl

Open wiktorwieclaw opened this issue 1 year ago • 2 comments

In SpiBus::transfer

fn transfer(&mut self, read: &mut [Word], write: &[Word]) -> Result<(), Self::Error>;

From embedded-hal docs:

It is allowed for read and write to have different lengths, even zero length. The transfer runs for max(read.len(), write.len()) words. If read is shorter, incoming words after read has been filled will be discarded. If write is shorter, the value of words sent in MOSI after all write has been sent is implementation-defined, typically 0x00, 0xFF, or configurable.

Current implementation requires the lengths to be equal: https://github.com/stm32-rs/stm32f4xx-hal/blob/master/src/spi/hal_1.rs#L88

fn transfer(&mut self, buff: &mut [W], data: &[W]) -> Result<(), Self::Error> {
    assert_eq!(data.len(), buff.len()); // precondition here!

    for (d, b) in data.iter().cloned().zip(buff.iter_mut()) {
        nb::block!(<Self as FullDuplex<W>>::write(self, d))?;
        *b = nb::block!(<Self as FullDuplex<W>>::read(self))?;
    }

    Ok(())
}

So I changed it accordingly. Please let me know if line 103 is needed.

wiktorwieclaw avatar Jan 07 '23 22:01 wiktorwieclaw