From 152d8ee0d9526a9b5d41350385ee2b2102c0c43f Mon Sep 17 00:00:00 2001 From: elagil Date: Thu, 28 Nov 2024 17:36:14 +0100 Subject: [PATCH 1/2] fix: make `write_immediate()` for ring buffers right-aligned --- embassy-stm32/src/dma/ringbuffer/mod.rs | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/embassy-stm32/src/dma/ringbuffer/mod.rs b/embassy-stm32/src/dma/ringbuffer/mod.rs index 25bdc7522..4dc1b51a9 100644 --- a/embassy-stm32/src/dma/ringbuffer/mod.rs +++ b/embassy-stm32/src/dma/ringbuffer/mod.rs @@ -252,9 +252,13 @@ impl<'a, W: Word> WritableDmaRingBuffer<'a, W> { } /// Write elements directly to the buffer. + /// + /// Data is aligned towards the end of the buffer. pub fn write_immediate(&mut self, buf: &[W]) -> Result<(usize, usize), Error> { + let start = self.cap() - buf.len(); + for (i, data) in buf.iter().enumerate() { - self.write_buf(i, *data) + self.write_buf(start + i, *data) } let written = buf.len().min(self.cap()); Ok((written, self.cap() - written)) From 5d2b38c979b045d69fee1e2b1bb7f209043ceece Mon Sep 17 00:00:00 2001 From: elagil Date: Thu, 28 Nov 2024 17:45:00 +0100 Subject: [PATCH 2/2] doc: improve comment --- embassy-stm32/src/dma/ringbuffer/mod.rs | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/embassy-stm32/src/dma/ringbuffer/mod.rs b/embassy-stm32/src/dma/ringbuffer/mod.rs index 4dc1b51a9..44ea497fe 100644 --- a/embassy-stm32/src/dma/ringbuffer/mod.rs +++ b/embassy-stm32/src/dma/ringbuffer/mod.rs @@ -253,10 +253,17 @@ impl<'a, W: Word> WritableDmaRingBuffer<'a, W> { /// Write elements directly to the buffer. /// + /// Subsequent writes will overwrite the content of the buffer, so it is not useful to call this more than once. /// Data is aligned towards the end of the buffer. + /// + /// In case of success, returns the written length, and the empty space in front of the written block. + /// Fails if the data to write exceeds the buffer capacity. pub fn write_immediate(&mut self, buf: &[W]) -> Result<(usize, usize), Error> { - let start = self.cap() - buf.len(); + if buf.len() > self.cap() { + return Err(Error::Overrun); + } + let start = self.cap() - buf.len(); for (i, data) in buf.iter().enumerate() { self.write_buf(start + i, *data) }