Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

embassy-rp: pio: Add access to DMA engine byte swapping #3935

Merged
merged 1 commit into from
Mar 6, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 8 additions & 5 deletions cyw43-pio/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -169,12 +169,12 @@ where

self.sm.set_enable(true);

self.sm.tx().dma_push(self.dma.reborrow(), write).await;
self.sm.tx().dma_push(self.dma.reborrow(), write, false).await;

let mut status = 0;
self.sm
.rx()
.dma_pull(self.dma.reborrow(), slice::from_mut(&mut status))
.dma_pull(self.dma.reborrow(), slice::from_mut(&mut status), false)
.await;
status
}
Expand All @@ -201,13 +201,16 @@ where
// self.cs.set_low();
self.sm.set_enable(true);

self.sm.tx().dma_push(self.dma.reborrow(), slice::from_ref(&cmd)).await;
self.sm.rx().dma_pull(self.dma.reborrow(), read).await;
self.sm
.tx()
.dma_push(self.dma.reborrow(), slice::from_ref(&cmd), false)
.await;
self.sm.rx().dma_pull(self.dma.reborrow(), read, false).await;

let mut status = 0;
self.sm
.rx()
.dma_pull(self.dma.reborrow(), slice::from_mut(&mut status))
.dma_pull(self.dma.reborrow(), slice::from_mut(&mut status), false)
.await;

#[cfg(feature = "defmt")]
Expand Down
10 changes: 9 additions & 1 deletion embassy-rp/src/pio/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -362,6 +362,7 @@ impl<'d, PIO: Instance, const SM: usize> StateMachineRx<'d, PIO, SM> {
&'a mut self,
ch: PeripheralRef<'a, C>,
data: &'a mut [W],
bswap: bool,
) -> Transfer<'a, C> {
let pio_no = PIO::PIO_NO;
let p = ch.regs();
Expand All @@ -379,6 +380,7 @@ impl<'d, PIO: Instance, const SM: usize> StateMachineRx<'d, PIO, SM> {
w.set_chain_to(ch.number());
w.set_incr_read(false);
w.set_incr_write(true);
w.set_bswap(bswap);
w.set_en(true);
});
compiler_fence(Ordering::SeqCst);
Expand Down Expand Up @@ -447,7 +449,12 @@ impl<'d, PIO: Instance, const SM: usize> StateMachineTx<'d, PIO, SM> {
}

/// Prepare a DMA transfer to TX FIFO.
pub fn dma_push<'a, C: Channel, W: Word>(&'a mut self, ch: PeripheralRef<'a, C>, data: &'a [W]) -> Transfer<'a, C> {
pub fn dma_push<'a, C: Channel, W: Word>(
&'a mut self,
ch: PeripheralRef<'a, C>,
data: &'a [W],
bswap: bool,
) -> Transfer<'a, C> {
let pio_no = PIO::PIO_NO;
let p = ch.regs();
p.read_addr().write_value(data.as_ptr() as u32);
Expand All @@ -464,6 +471,7 @@ impl<'d, PIO: Instance, const SM: usize> StateMachineTx<'d, PIO, SM> {
w.set_chain_to(ch.number());
w.set_incr_read(true);
w.set_incr_write(false);
w.set_bswap(bswap);
w.set_en(true);
});
compiler_fence(Ordering::SeqCst);
Expand Down
4 changes: 2 additions & 2 deletions embassy-rp/src/pio_programs/hd44780.rs
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ impl<'l, P: Instance, const S: usize> PioHD44780<'l, P, S> {
sm.set_enable(true);

// display on and cursor on and blinking, reset display
sm.tx().dma_push(dma.reborrow(), &[0x81u8, 0x0f, 1]).await;
sm.tx().dma_push(dma.reborrow(), &[0x81u8, 0x0f, 1], false).await;

Self {
dma: dma.map_into(),
Expand All @@ -198,6 +198,6 @@ impl<'l, P: Instance, const S: usize> PioHD44780<'l, P, S> {
// set cursor to 1:15
self.buf[38..].copy_from_slice(&[0x80, 0xcf]);

self.sm.tx().dma_push(self.dma.reborrow(), &self.buf).await;
self.sm.tx().dma_push(self.dma.reborrow(), &self.buf, false).await;
}
}
2 changes: 1 addition & 1 deletion embassy-rp/src/pio_programs/i2s.rs
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,6 @@ impl<'a, P: Instance, const S: usize> PioI2sOut<'a, P, S> {

/// Return an in-prograss dma transfer future. Awaiting it will guarentee a complete transfer.
pub fn write<'b>(&'b mut self, buff: &'b [u32]) -> Transfer<'b, AnyChannel> {
self.sm.tx().dma_push(self.dma.reborrow(), buff)
self.sm.tx().dma_push(self.dma.reborrow(), buff, false)
}
}
2 changes: 1 addition & 1 deletion embassy-rp/src/pio_programs/ws2812.rs
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ impl<'d, P: Instance, const S: usize, const N: usize> PioWs2812<'d, P, S, N> {
}

// DMA transfer
self.sm.tx().dma_push(self.dma.reborrow(), &words).await;
self.sm.tx().dma_push(self.dma.reborrow(), &words, false).await;

Timer::after_micros(55).await;
}
Expand Down
4 changes: 2 additions & 2 deletions examples/rp/src/bin/pio_dma.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,8 @@ async fn main(_spawner: Spawner) {
loop {
let (rx, tx) = sm.rx_tx();
join(
tx.dma_push(dma_out_ref.reborrow(), &dout),
rx.dma_pull(dma_in_ref.reborrow(), &mut din),
tx.dma_push(dma_out_ref.reborrow(), &dout, false),
rx.dma_pull(dma_in_ref.reborrow(), &mut din, false),
)
.await;
for i in 0..din.len() {
Expand Down
4 changes: 2 additions & 2 deletions examples/rp235x/src/bin/pio_dma.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,8 @@ async fn main(_spawner: Spawner) {
loop {
let (rx, tx) = sm.rx_tx();
join(
tx.dma_push(dma_out_ref.reborrow(), &dout),
rx.dma_pull(dma_in_ref.reborrow(), &mut din),
tx.dma_push(dma_out_ref.reborrow(), &dout, false),
rx.dma_pull(dma_in_ref.reborrow(), &mut din, false),
)
.await;
for i in 0..din.len() {
Expand Down