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

[3/3] DMA Move API: Introduce DMA buffer objects #1856

Merged
merged 15 commits into from
Aug 20, 2024
Merged
Changes from 1 commit
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
Prev Previous commit
Add with_buffers builder
Dominaezzz committed Aug 19, 2024
commit 42281ea0f7002f7ffa6f734c3e9ca37a307d2375
32 changes: 32 additions & 0 deletions esp-hal/src/spi/master.rs
Original file line number Diff line number Diff line change
@@ -862,6 +862,7 @@ pub mod dma {
SpiPeripheral,
TxPrivate,
},
Blocking,
InterruptConfigurable,
Mode,
};
@@ -1037,6 +1038,37 @@ pub mod dma {
}
}

impl<'d, T, C> SpiDma<'d, T, C, FullDuplexMode, Blocking>
where
T: InstanceDma,
C: DmaChannel,
C::P: SpiPeripheral,
{
pub fn with_buffers(
self,
dma_tx_buf: DmaTxBuf,
dma_rx_buf: DmaRxBuf,
) -> SpiDmaBus<'d, T, C> {
SpiDmaBus::new(self, dma_tx_buf, dma_rx_buf)
}
}

#[cfg(feature = "async")]
impl<'d, T, C> SpiDma<'d, T, C, FullDuplexMode, crate::Async>
where
T: InstanceDma,
C: DmaChannel,
C::P: SpiPeripheral,
{
pub fn with_buffers(
self,
dma_tx_buf: DmaTxBuf,
dma_rx_buf: DmaRxBuf,
) -> asynch::SpiDmaAsyncBus<'d, T, C> {
asynch::SpiDmaAsyncBus::new(self, dma_tx_buf, dma_rx_buf)
}
}

pub struct SpiDmaTransfer<'d, T, C, M, DmaMode, Buf>
where
C: DmaChannel,
11 changes: 5 additions & 6 deletions examples/src/bin/embassy_spi.rs
Original file line number Diff line number Diff line change
@@ -29,7 +29,7 @@ use esp_hal::{
peripherals::Peripherals,
prelude::*,
spi::{
master::{dma::asynch::SpiDmaAsyncBus, prelude::*, Spi},
master::{prelude::*, Spi},
SpiMode,
},
system::SystemControl,
@@ -63,17 +63,16 @@ async fn main(_spawner: Spawner) {
let dma_tx_buf = DmaTxBuf::new(tx_descriptors, tx_buffer).unwrap();
let dma_rx_buf = DmaRxBuf::new(rx_descriptors, rx_buffer).unwrap();

let spi = Spi::new(peripherals.SPI2, 100.kHz(), SpiMode::Mode0, &clocks)
let mut spi = Spi::new(peripherals.SPI2, 100.kHz(), SpiMode::Mode0, &clocks)
.with_pins(Some(sclk), Some(mosi), Some(miso), Some(cs))
.with_dma(dma_channel.configure_for_async(false, DmaPriority::Priority0));

let mut spi_bus = SpiDmaAsyncBus::new(spi, dma_tx_buf, dma_rx_buf);
.with_dma(dma_channel.configure_for_async(false, DmaPriority::Priority0))
.with_buffers(dma_tx_buf, dma_rx_buf);

let send_buffer = [0, 1, 2, 3, 4, 5, 6, 7];
loop {
let mut buffer = [0; 8];
esp_println::println!("Sending bytes");
embedded_hal_async::spi::SpiBus::transfer(&mut spi_bus, &mut buffer, &send_buffer)
embedded_hal_async::spi::SpiBus::transfer(&mut spi, &mut buffer, &send_buffer)
.await
.unwrap();
esp_println::println!("Bytes received: {:?}", buffer);
27 changes: 12 additions & 15 deletions hil-test/tests/spi_full_duplex_dma.rs
Original file line number Diff line number Diff line change
@@ -348,16 +348,15 @@ mod tests {
let dma_tx_buf = DmaTxBuf::new(tx_descriptors, tx_buffer).unwrap();
let dma_rx_buf = DmaRxBuf::new(rx_descriptors, rx_buffer).unwrap();

let spi = Spi::new(peripherals.SPI2, 100.kHz(), SpiMode::Mode0, &clocks)
let mut spi = Spi::new(peripherals.SPI2, 100.kHz(), SpiMode::Mode0, &clocks)
.with_pins(Some(sclk), Some(mosi), Some(miso), Some(cs))
.with_dma(dma_channel.configure(false, DmaPriority::Priority0));

let mut spi_bus = SpiDmaBus::new(spi, dma_tx_buf, dma_rx_buf);
.with_dma(dma_channel.configure(false, DmaPriority::Priority0))
.with_buffers(dma_tx_buf, dma_rx_buf);

let tx_buf = [0xde, 0xad, 0xbe, 0xef];
let mut rx_buf = [0; 4];

spi_bus.transfer(&mut rx_buf, &tx_buf).unwrap();
spi.transfer(&mut rx_buf, &tx_buf).unwrap();

assert_eq!(tx_buf, rx_buf);
}
@@ -386,16 +385,15 @@ mod tests {
let dma_tx_buf = DmaTxBuf::new(tx_descriptors, tx_buffer).unwrap();
let dma_rx_buf = DmaRxBuf::new(rx_descriptors, rx_buffer).unwrap();

let spi = Spi::new(peripherals.SPI2, 100.kHz(), SpiMode::Mode0, &clocks)
let mut spi = Spi::new(peripherals.SPI2, 100.kHz(), SpiMode::Mode0, &clocks)
.with_pins(Some(sclk), Some(mosi), Some(miso), Some(cs))
.with_dma(dma_channel.configure(false, DmaPriority::Priority0));

let mut spi_bus = SpiDmaBus::new(spi, dma_tx_buf, dma_rx_buf);
.with_dma(dma_channel.configure(false, DmaPriority::Priority0))
.with_buffers(dma_tx_buf, dma_rx_buf);

let tx_buf = [0xde, 0xad, 0xbe, 0xef];
let mut rx_buf = [0; 4];

spi_bus.transfer(&mut rx_buf, &tx_buf).unwrap();
spi.transfer(&mut rx_buf, &tx_buf).unwrap();

assert_eq!(&tx_buf[0..1], &rx_buf[0..1]);
}
@@ -426,16 +424,15 @@ mod tests {
let dma_tx_buf = DmaTxBuf::new(tx_descriptors, tx_buffer).unwrap();
let dma_rx_buf = DmaRxBuf::new(rx_descriptors, rx_buffer).unwrap();

let spi = Spi::new(peripherals.SPI2, 100.kHz(), SpiMode::Mode0, &clocks)
let mut spi = Spi::new(peripherals.SPI2, 100.kHz(), SpiMode::Mode0, &clocks)
.with_pins(Some(sclk), Some(mosi), Some(miso), Some(cs))
.with_dma(dma_channel.configure(false, DmaPriority::Priority0));

let mut spi_bus = SpiDmaBus::new(spi, dma_tx_buf, dma_rx_buf);
.with_dma(dma_channel.configure(false, DmaPriority::Priority0))
.with_buffers(dma_tx_buf, dma_rx_buf);

let tx_buf = core::array::from_fn(|i| i as _);
let mut rx_buf = [0; DMA_BUFFER_SIZE];

spi_bus.transfer(&mut rx_buf, &tx_buf).unwrap();
spi.transfer(&mut rx_buf, &tx_buf).unwrap();

assert_eq!(tx_buf, rx_buf);
}
18 changes: 7 additions & 11 deletions hil-test/tests/spi_full_duplex_dma_async.rs
Original file line number Diff line number Diff line change
@@ -43,10 +43,7 @@ use hil_test as _;
#[embedded_test::tests(executor = esp_hal_embassy::Executor::new())]
mod tests {
use defmt::assert_eq;
use esp_hal::{
dma::{DmaRxBuf, DmaTxBuf},
spi::master::dma::asynch::SpiDmaAsyncBus,
};
use esp_hal::dma::{DmaRxBuf, DmaTxBuf};

use super::*;

@@ -82,11 +79,10 @@ mod tests {
let dma_tx_buf = DmaTxBuf::new(tx_descriptors, tx_buffer).unwrap();
let dma_rx_buf = DmaRxBuf::new(rx_descriptors, rx_buffer).unwrap();

let spi = Spi::new(peripherals.SPI2, 100.kHz(), SpiMode::Mode0, &clocks)
let mut spi = Spi::new(peripherals.SPI2, 100.kHz(), SpiMode::Mode0, &clocks)
.with_pins(Some(sclk), Some(mosi), Some(miso), Some(cs))
.with_dma(dma_channel.configure_for_async(false, DmaPriority::Priority0));

let mut spi = SpiDmaAsyncBus::new(spi, dma_tx_buf, dma_rx_buf);
.with_dma(dma_channel.configure_for_async(false, DmaPriority::Priority0))
.with_buffers(dma_tx_buf, dma_rx_buf);

let unit = pcnt.unit0;
unit.channel0.set_edge_signal(PcntSource::from_pin(
@@ -145,10 +141,10 @@ mod tests {
let dma_tx_buf = DmaTxBuf::new(tx_descriptors, tx_buffer).unwrap();
let dma_rx_buf = DmaRxBuf::new(rx_descriptors, rx_buffer).unwrap();

let spi = Spi::new(peripherals.SPI2, 100.kHz(), SpiMode::Mode0, &clocks)
let mut spi = Spi::new(peripherals.SPI2, 100.kHz(), SpiMode::Mode0, &clocks)
.with_pins(Some(sclk), Some(mosi), Some(miso), Some(cs))
.with_dma(dma_channel.configure_for_async(false, DmaPriority::Priority0));
let mut spi = SpiDmaAsyncBus::new(spi, dma_tx_buf, dma_rx_buf);
.with_dma(dma_channel.configure_for_async(false, DmaPriority::Priority0))
.with_buffers(dma_tx_buf, dma_rx_buf);

let unit = pcnt.unit0;
unit.channel0.set_edge_signal(PcntSource::from_pin(