From 1afbb8e58a4c19a3c24e7a4b2c62d0f29de78a63 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Henrik=20Alse=CC=81r?= <> Date: Wed, 24 Jun 2020 19:33:13 +0200 Subject: [PATCH] Spim transfer: Implicitly copy buffer into RAM if needed when using embedded hal trait --- nrf-hal-common/src/spim.rs | 25 ++++++++++++++++++------- 1 file changed, 18 insertions(+), 7 deletions(-) diff --git a/nrf-hal-common/src/spim.rs b/nrf-hal-common/src/spim.rs index 83331268..915675e5 100644 --- a/nrf-hal-common/src/spim.rs +++ b/nrf-hal-common/src/spim.rs @@ -41,13 +41,24 @@ where type Error = Error; fn transfer<'w>(&mut self, words: &'w mut [u8]) -> Result<&'w [u8], Error> { - // If the slice isn't in RAM, we can't write back to it at all - slice_in_ram_or(words, Error::DMABufferNotInDataMemory)?; - - words.chunks(EASY_DMA_SIZE).try_for_each(|chunk| { - self.do_spi_dma_transfer(DmaSlice::from_slice(chunk), DmaSlice::from_slice(chunk)) - })?; - + if slice_in_ram(words) { + words.chunks(EASY_DMA_SIZE).try_for_each(|chunk| { + self.do_spi_dma_transfer(DmaSlice::from_slice(chunk), DmaSlice::from_slice(chunk)) + })?; + } else { + words + .chunks_mut(FORCE_COPY_BUFFER_SIZE) + .try_for_each(|chunk| { + let mut buf = [0u8; FORCE_COPY_BUFFER_SIZE]; + buf[..chunk.len()].copy_from_slice(chunk); + self.do_spi_dma_transfer( + DmaSlice::from_slice(&buf[..chunk.len()]), + DmaSlice::from_slice(&buf[..chunk.len()]), + )?; + chunk.copy_from_slice(&buf[..chunk.len()]); + Ok(()) + })?; + } Ok(words) } }