Skip to content

Commit fbec3cc

Browse files
committed
Use TSIZE for DMA transactions
1 parent fdd7331 commit fbec3cc

File tree

2 files changed

+37
-15
lines changed

2 files changed

+37
-15
lines changed

src/spi.rs

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -185,7 +185,7 @@ pub enum Error {
185185
/// SimplexTransmitter)
186186
InvalidOperation,
187187
/// A DMA error occurred during processing
188-
DmaError(DmaError)
188+
DmaError(DmaError),
189189
}
190190

191191
impl From<DmaError> for Error {
@@ -508,6 +508,14 @@ impl<SPI: Instance, W: Word> Inner<SPI, W> {
508508
self.spi.cr1().modify(|_, w| w.cstart().started());
509509
}
510510

511+
fn set_transfer_word_count(&self, words: u16) {
512+
self.spi.cr2().modify(|_, w| w.tsize().set(words));
513+
}
514+
515+
fn reset_transfer_word_count(&self) {
516+
self.spi.cr2().modify(|_, w| w.tsize().set(0));
517+
}
518+
511519
/// Disable SPI
512520
fn disable(&mut self) {
513521
self.spi.cr1().modify(|_, w| w.spe().disabled());
@@ -600,7 +608,9 @@ impl<SPI: Instance, W: Word> Inner<SPI, W> {
600608
/// Disable DMA for both Rx and Tx
601609
#[inline]
602610
pub fn disable_dma(&self) {
603-
self.spi.cfg1().modify(|_, w| w.rxdmaen().disabled().txdmaen().disabled());
611+
self.spi
612+
.cfg1()
613+
.modify(|_, w| w.rxdmaen().disabled().txdmaen().disabled());
604614
}
605615

606616
/// Read a single word from the receive data register
@@ -838,7 +848,7 @@ impl<SPI: Instance, W: Word> Spi<SPI, W> {
838848
u16::MAX
839849
);
840850

841-
self.spi().cr2().write(|w| w.tsize().set(length as u16));
851+
self.set_transfer_word_count(length.get() as u16);
842852

843853
// Re-enable
844854
self.inner.clear_modf();
@@ -872,6 +882,7 @@ impl<SPI: Instance, W: Word> Spi<SPI, W> {
872882
.write(|w| w.txtfc().clear().eotc().clear().suspc().clear());
873883

874884
self.inner.disable();
885+
self.inner.reset_transfer_word_count();
875886
true
876887
}
877888

@@ -885,6 +896,7 @@ impl<SPI: Instance, W: Word> Spi<SPI, W> {
885896

886897
fn abort_transaction(&mut self) {
887898
self.inner.disable();
899+
self.inner.reset_transfer_word_count();
888900
}
889901

890902
/// Deconstructs the SPI peripheral and returns the component parts.

src/spi/dma.rs

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

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

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

154-
pub struct DuplexDmaTransfer<SPI: 'static, W: FrameSize + 'static, TX, RX, S, D>
155-
{
156-
spi: &'static mut Spi<SPI, W>,
154+
pub struct DuplexDmaTransfer<'a, SPI, W: FrameSize, TX, RX, S, D> {
155+
spi: &'a mut Spi<SPI, W>,
157156
tx_transfer: DmaTransfer<TX, S, DmaTx<SPI, W>, MemoryToPeripheral>,
158157
rx_transfer: DmaTransfer<RX, DmaRx<SPI, W>, D, PeripheralToMemory>,
158+
len: usize,
159159
}
160160

161-
impl<SPI, W, RX, TX, S, D> DuplexDmaTransfer<SPI, W, TX, RX, S, D>
161+
impl<'a, SPI, W, RX, TX, S, D> DuplexDmaTransfer<'a, SPI, W, TX, RX, S, D>
162162
where
163163
SPI: Instance,
164164
W: FrameSize + Word,
@@ -168,12 +168,14 @@ where
168168
D: WriteBuffer<Word = W>,
169169
{
170170
pub fn new(
171-
spi: &'static mut Spi<SPI, W>,
171+
spi: &'a mut Spi<SPI, W>,
172172
tx_channel: TX,
173173
rx_channel: RX,
174174
source: S,
175-
destination: D,
175+
mut destination: D,
176176
) -> Self {
177+
let (_, dest_len) = unsafe { destination.write_buffer() };
178+
177179
let tx_config = DmaConfig::new().with_request(SPI::tx_dma_request());
178180
let tx_destination = DmaTx::new();
179181
let tx_transfer = DmaTransfer::memory_to_peripheral(
@@ -194,6 +196,7 @@ where
194196
spi,
195197
tx_transfer,
196198
rx_transfer,
199+
len: dest_len,
197200
}
198201
}
199202

@@ -207,12 +210,11 @@ where
207210

208211
pub fn start(&mut self) -> Result<(), Error> {
209212
self.spi.enable_rx_dma();
210-
211-
self.rx_transfer.start()?;
213+
self.rx_transfer.start_nb();
212214
self.tx_transfer.start_nb();
213215
self.spi.enable_tx_dma();
214-
self.spi.enable();
215-
self.spi.start_transfer();
216+
self.spi
217+
.setup_transaction(NonZeroUsize::new(self.len).unwrap())?;
216218
Ok(())
217219
}
218220

@@ -235,7 +237,15 @@ where
235237
self.spi.disable_dma();
236238
}
237239

238-
pub fn free(self) -> Result<(TX, RX, S, D), Error> {
240+
pub fn abort(&mut self) -> Result<(), Error> {
241+
self.end_transfer();
242+
self.tx_transfer.abort()?;
243+
self.rx_transfer.abort()?;
244+
Ok(())
245+
}
246+
247+
pub fn free(mut self) -> Result<(TX, RX, S, D), Error> {
248+
self.end_transfer();
239249
let (tx, s, _) = self.tx_transfer.free()?;
240250
let (rx, _, d) = self.rx_transfer.free()?;
241251
Ok((tx, rx, s, d))

0 commit comments

Comments
 (0)