Skip to content

Commit 7c69941

Browse files
committed
Use references for all DMA transfers, add write_dma method
1 parent 296b677 commit 7c69941

File tree

1 file changed

+15
-9
lines changed

1 file changed

+15
-9
lines changed

src/spi/dma.rs

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -53,19 +53,19 @@ unsafe impl<SPI: Instance, W: FrameSize> WriteBuffer for DmaTx<SPI, W> {
5353
}
5454
}
5555

56-
pub struct RxDmaTransfer<SPI, W: FrameSize, CH, D> {
57-
spi: Spi<SPI, W>,
56+
pub struct RxDmaTransfer<'a, SPI, W: FrameSize, CH, D> {
57+
spi: &'a mut Spi<SPI, W>,
5858
transfer: DmaTransfer<CH, DmaRx<SPI, W>, D, PeripheralToMemory>,
5959
}
6060

61-
impl<SPI, W, CH, D> RxDmaTransfer<SPI, W, CH, D>
61+
impl<'a, SPI, W, CH, D> RxDmaTransfer<'a, SPI, W, CH, D>
6262
where
6363
SPI: Instance,
6464
W: FrameSize + Word,
6565
CH: Channel,
6666
D: WriteBuffer<Word = W>,
6767
{
68-
pub fn new(spi: Spi<SPI, W>, channel: CH, destination: D) -> Self {
68+
pub fn new(spi: &'a mut Spi<SPI, W>, channel: CH, destination: D) -> Self {
6969
let config = DmaConfig::new().with_request(SPI::rx_dma_request());
7070
let source = DmaRx::new();
7171
let transfer = DmaTransfer::peripheral_to_memory(
@@ -86,19 +86,19 @@ where
8686
}
8787
}
8888

89-
pub struct TxDmaTransfer<SPI, W: FrameSize, CH, S> {
90-
spi: Spi<SPI, W>,
89+
pub struct TxDmaTransfer<'a, SPI, W: FrameSize, CH, S> {
90+
spi: &'a mut Spi<SPI, W>,
9191
transfer: DmaTransfer<CH, S, DmaTx<SPI, W>, MemoryToPeripheral>,
9292
}
9393

94-
impl<SPI, W, CH, S> TxDmaTransfer<SPI, W, CH, S>
94+
impl<'a, SPI, W, CH, S> TxDmaTransfer<'a, SPI, W, CH, S>
9595
where
9696
SPI: Instance,
9797
W: FrameSize + Word,
9898
CH: Channel,
9999
S: ReadBuffer<Word = W>,
100100
{
101-
pub fn new(spi: Spi<SPI, W>, channel: CH, source: S) -> Self {
101+
pub fn new(spi: &'a mut Spi<SPI, W>, channel: CH, source: S) -> Self {
102102
let config = DmaConfig::new().with_request(SPI::tx_dma_request());
103103
let destination = DmaTx::new();
104104
let transfer = DmaTransfer::memory_to_peripheral(
@@ -205,7 +205,13 @@ type DuplexInplaceDmaTransfer<'a, SPI, W, TX, RX> =
205205
DuplexDmaTransfer<'a, SPI, W, TX, RX, &'static [W], &'static mut [W]>;
206206

207207
impl<SPI: Instance, W: FrameSize + Word> Spi<SPI, W> {
208-
pub fn dma_transfer_inplace<TX: Channel, RX: Channel>(
208+
pub fn write_dma<TX: Channel>(&mut self, channel: TX, data: &'static [W]) -> Result<TxDmaTransfer<SPI, W, TX, &'static [W]>, Error> {
209+
let mut transfer = TxDmaTransfer::new(self, channel, data);
210+
transfer.start()?;
211+
Ok(transfer)
212+
}
213+
214+
pub fn transfer_inplace_dma<TX: Channel, RX: Channel>(
209215
&mut self,
210216
buffer: &'static mut [W],
211217
tx_channel: TX,

0 commit comments

Comments
 (0)