From 00cde67abe23aa299f94fb6e1aa6cb2c3de69788 Mon Sep 17 00:00:00 2001 From: Dario Nieuwenhuis Date: Mon, 1 May 2023 19:10:00 +0200 Subject: [PATCH] stm32/dma: solve overlapping impl on DmaCtrl on stm32h7 --- embassy-stm32/src/dma/bdma.rs | 14 ++++++++------ embassy-stm32/src/dma/dma.rs | 14 ++++++++------ embassy-stm32/src/dma/ringbuffer.rs | 6 +++--- 3 files changed, 19 insertions(+), 15 deletions(-) diff --git a/embassy-stm32/src/dma/bdma.rs b/embassy-stm32/src/dma/bdma.rs index 88df76ba..0202ec37 100644 --- a/embassy-stm32/src/dma/bdma.rs +++ b/embassy-stm32/src/dma/bdma.rs @@ -368,18 +368,20 @@ impl<'a, C: Channel> Future for Transfer<'a, C> { // ============================== -impl DmaCtrl for C { +struct DmaCtrlImpl<'a, C: Channel>(PeripheralRef<'a, C>); + +impl<'a, C: Channel> DmaCtrl for DmaCtrlImpl<'a, C> { fn ndtr(&self) -> usize { - let ch = self.regs().ch(self.num()); + let ch = self.0.regs().ch(self.0.num()); unsafe { ch.ndtr().read() }.ndt() as usize } fn get_complete_count(&self) -> usize { - STATE.complete_count[self.index()].load(Ordering::Acquire) + STATE.complete_count[self.0.index()].load(Ordering::Acquire) } fn reset_complete_count(&mut self) -> usize { - STATE.complete_count[self.index()].swap(0, Ordering::AcqRel) + STATE.complete_count[self.0.index()].swap(0, Ordering::AcqRel) } } @@ -451,13 +453,13 @@ impl<'a, C: Channel, W: Word> RingBuffer<'a, C, W> { } pub fn clear(&mut self) { - self.ringbuf.clear(&mut *self.channel); + self.ringbuf.clear(DmaCtrlImpl(self.channel.reborrow())); } /// Read bytes from the ring buffer /// OverrunError is returned if the portion to be read was overwritten by the DMA controller. pub fn read(&mut self, buf: &mut [W]) -> Result { - self.ringbuf.read(&mut *self.channel, buf) + self.ringbuf.read(DmaCtrlImpl(self.channel.reborrow()), buf) } pub fn is_empty(&self) -> bool { diff --git a/embassy-stm32/src/dma/dma.rs b/embassy-stm32/src/dma/dma.rs index 69a5ec4e..7b17d9e4 100644 --- a/embassy-stm32/src/dma/dma.rs +++ b/embassy-stm32/src/dma/dma.rs @@ -609,18 +609,20 @@ impl<'a, C: Channel, W: Word> Drop for DoubleBuffered<'a, C, W> { // ============================== -impl DmaCtrl for C { +struct DmaCtrlImpl<'a, C: Channel>(PeripheralRef<'a, C>); + +impl<'a, C: Channel> DmaCtrl for DmaCtrlImpl<'a, C> { fn ndtr(&self) -> usize { - let ch = self.regs().st(self.num()); + let ch = self.0.regs().st(self.0.num()); unsafe { ch.ndtr().read() }.ndt() as usize } fn get_complete_count(&self) -> usize { - STATE.complete_count[self.index()].load(Ordering::Acquire) + STATE.complete_count[self.0.index()].load(Ordering::Acquire) } fn reset_complete_count(&mut self) -> usize { - STATE.complete_count[self.index()].swap(0, Ordering::AcqRel) + STATE.complete_count[self.0.index()].swap(0, Ordering::AcqRel) } } @@ -707,13 +709,13 @@ impl<'a, C: Channel, W: Word> RingBuffer<'a, C, W> { } pub fn clear(&mut self) { - self.ringbuf.clear(&mut *self.channel); + self.ringbuf.clear(DmaCtrlImpl(self.channel.reborrow())); } /// Read bytes from the ring buffer /// OverrunError is returned if the portion to be read was overwritten by the DMA controller. pub fn read(&mut self, buf: &mut [W]) -> Result { - self.ringbuf.read(&mut *self.channel, buf) + self.ringbuf.read(DmaCtrlImpl(self.channel.reborrow()), buf) } pub fn is_empty(&self) -> bool { diff --git a/embassy-stm32/src/dma/ringbuffer.rs b/embassy-stm32/src/dma/ringbuffer.rs index 02964eb6..38cc87ae 100644 --- a/embassy-stm32/src/dma/ringbuffer.rs +++ b/embassy-stm32/src/dma/ringbuffer.rs @@ -63,7 +63,7 @@ impl<'a, W: Word> DmaRingBuffer<'a, W> { } /// Reset the ring buffer to its initial state - pub fn clear(&mut self, dma: &mut impl DmaCtrl) { + pub fn clear(&mut self, mut dma: impl DmaCtrl) { self.first = 0; self.ndtr = self.dma_buf.len(); dma.reset_complete_count(); @@ -94,7 +94,7 @@ impl<'a, W: Word> DmaRingBuffer<'a, W> { /// Read bytes from the ring buffer /// OverrunError is returned if the portion to be read was overwritten by the DMA controller. - pub fn read(&mut self, dma: &mut impl DmaCtrl, buf: &mut [W]) -> Result { + pub fn read(&mut self, mut dma: impl DmaCtrl, buf: &mut [W]) -> Result { let end = self.end(); compiler_fence(Ordering::SeqCst); @@ -244,7 +244,7 @@ mod tests { } } - impl DmaCtrl for TestCtrl { + impl DmaCtrl for &mut TestCtrl { fn ndtr(&self) -> usize { self.next_ndtr.borrow_mut().unwrap() }