Skip to content

Commit fdd7331

Browse files
committed
use &'static mut
1 parent d449105 commit fdd7331

File tree

1 file changed

+19
-43
lines changed

1 file changed

+19
-43
lines changed

src/spi/dma.rs

Lines changed: 19 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use core::marker::PhantomData;
1+
use core::{cell::RefCell, marker::PhantomData, ops::{Deref, DerefMut}};
22

33
use embedded_dma::{ReadBuffer, WriteBuffer};
44

@@ -151,13 +151,14 @@ where
151151
}
152152
}
153153

154-
pub struct DuplexDmaTransfer<'a, SPI, W: FrameSize, TX, RX, S, D> {
155-
spi: &'a mut Spi<SPI, W>,
154+
pub struct DuplexDmaTransfer<SPI: 'static, W: FrameSize + 'static, TX, RX, S, D>
155+
{
156+
spi: &'static mut Spi<SPI, W>,
156157
tx_transfer: DmaTransfer<TX, S, DmaTx<SPI, W>, MemoryToPeripheral>,
157158
rx_transfer: DmaTransfer<RX, DmaRx<SPI, W>, D, PeripheralToMemory>,
158159
}
159160

160-
impl<'a, SPI, W, RX, TX, S, D> DuplexDmaTransfer<'a, SPI, W, TX, RX, S, D>
161+
impl<SPI, W, RX, TX, S, D> DuplexDmaTransfer<SPI, W, TX, RX, S, D>
161162
where
162163
SPI: Instance,
163164
W: FrameSize + Word,
@@ -167,7 +168,7 @@ where
167168
D: WriteBuffer<Word = W>,
168169
{
169170
pub fn new(
170-
spi: &'a mut Spi<SPI, W>,
171+
spi: &'static mut Spi<SPI, W>,
171172
tx_channel: TX,
172173
rx_channel: RX,
173174
source: S,
@@ -196,14 +197,22 @@ where
196197
}
197198
}
198199

200+
pub fn enable_dma_interrupts(&self) {
201+
self.rx_transfer.enable_interrupts()
202+
}
203+
204+
pub fn disable_dma_interrupts(&self) {
205+
self.rx_transfer.disable_interrupts()
206+
}
207+
199208
pub fn start(&mut self) -> Result<(), Error> {
200209
self.spi.enable_rx_dma();
210+
201211
self.rx_transfer.start()?;
202-
self.tx_transfer.start_with(|_, _| {
203-
self.spi.enable_tx_dma();
204-
self.spi.enable();
205-
self.spi.start_transfer();
206-
})?;
212+
self.tx_transfer.start_nb();
213+
self.spi.enable_tx_dma();
214+
self.spi.enable();
215+
self.spi.start_transfer();
207216
Ok(())
208217
}
209218

@@ -232,36 +241,3 @@ where
232241
Ok((tx, rx, s, d))
233242
}
234243
}
235-
236-
pub type DuplexInplaceDmaTransfer<'a, SPI, W, TX, RX> =
237-
DuplexDmaTransfer<'a, SPI, W, TX, RX, &'static [W], &'static mut [W]>;
238-
239-
impl<SPI: Instance, W: FrameSize + Word> Spi<SPI, W> {
240-
pub fn write_dma<TX: Channel>(
241-
&mut self,
242-
channel: TX,
243-
data: &'static [W],
244-
) -> Result<TxDmaTransfer<SPI, W, TX, &'static [W]>, Error> {
245-
let mut transfer = TxDmaTransfer::new(self, channel, data);
246-
transfer.start()?;
247-
Ok(transfer)
248-
}
249-
250-
pub fn transfer_inplace_dma<TX: Channel, RX: Channel>(
251-
&mut self,
252-
buffer: &'static mut [W],
253-
tx_channel: TX,
254-
rx_channel: RX,
255-
) -> Result<DuplexInplaceDmaTransfer<SPI, W, TX, RX>, Error> {
256-
// Note (unsafe): Data will be read from the start of the buffer before data is written
257-
// to those locations just like for blocking non-DMA in-place transfers
258-
let source = unsafe {
259-
core::slice::from_raw_parts(buffer.as_ptr(), buffer.len())
260-
};
261-
let mut transfer = DuplexDmaTransfer::new(
262-
self, tx_channel, rx_channel, source, buffer,
263-
);
264-
transfer.start()?;
265-
Ok(transfer)
266-
}
267-
}

0 commit comments

Comments
 (0)