Skip to content

Commit 1afbb8e

Browse files
author
Henrik Alsér
committed
Spim transfer: Implicitly copy buffer into RAM if needed when using embedded hal trait
1 parent bc4f549 commit 1afbb8e

File tree

1 file changed

+18
-7
lines changed

1 file changed

+18
-7
lines changed

nrf-hal-common/src/spim.rs

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -41,13 +41,24 @@ where
4141
type Error = Error;
4242

4343
fn transfer<'w>(&mut self, words: &'w mut [u8]) -> Result<&'w [u8], Error> {
44-
// If the slice isn't in RAM, we can't write back to it at all
45-
slice_in_ram_or(words, Error::DMABufferNotInDataMemory)?;
46-
47-
words.chunks(EASY_DMA_SIZE).try_for_each(|chunk| {
48-
self.do_spi_dma_transfer(DmaSlice::from_slice(chunk), DmaSlice::from_slice(chunk))
49-
})?;
50-
44+
if slice_in_ram(words) {
45+
words.chunks(EASY_DMA_SIZE).try_for_each(|chunk| {
46+
self.do_spi_dma_transfer(DmaSlice::from_slice(chunk), DmaSlice::from_slice(chunk))
47+
})?;
48+
} else {
49+
words
50+
.chunks_mut(FORCE_COPY_BUFFER_SIZE)
51+
.try_for_each(|chunk| {
52+
let mut buf = [0u8; FORCE_COPY_BUFFER_SIZE];
53+
buf[..chunk.len()].copy_from_slice(chunk);
54+
self.do_spi_dma_transfer(
55+
DmaSlice::from_slice(&buf[..chunk.len()]),
56+
DmaSlice::from_slice(&buf[..chunk.len()]),
57+
)?;
58+
chunk.copy_from_slice(&buf[..chunk.len()]);
59+
Ok(())
60+
})?;
61+
}
5162
Ok(words)
5263
}
5364
}

0 commit comments

Comments
 (0)