Skip to content

Commit

Permalink
Add test case
Browse files Browse the repository at this point in the history
  • Loading branch information
bugadani committed Oct 22, 2024
1 parent 4c49fd6 commit 820aa2a
Showing 1 changed file with 112 additions and 2 deletions.
114 changes: 112 additions & 2 deletions hil-test/tests/embassy_interrupt_spi_dma.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ mod test {

#[test]
#[timeout(3)]
async fn run_interrupt_executor_test() {
async fn dma_does_not_lock_up_when_used_in_different_executors() {
let peripherals = esp_hal::init(esp_hal::Config::default());

let timg0 = TimerGroup::new(peripherals.TIMG0);
Expand All @@ -69,7 +69,7 @@ mod test {
let dma = Dma::new(peripherals.DMA);

cfg_if::cfg_if! {
if #[cfg(any(feature = "esp32", feature = "esp32s2"))] {
if #[cfg(pdma)] {
let dma_channel1 = dma.spi2channel;
let dma_channel2 = dma.spi3channel;
} else {
Expand Down Expand Up @@ -110,4 +110,114 @@ mod test {
}
}
}

// Reproducer of https://github.com/esp-rs/esp-hal/issues/2369
#[cfg(multi_core)]
#[test]
#[timeout(3)]
async fn dma_does_not_lock_up_on_core_1() {
use embassy_time::Timer;
use portable_atomic::{Ordering, AtomicU32};
use esp_hal::peripherals::SPI2;

cfg_if::cfg_if! {
if #[cfg(pdma)] {
use esp_hal::dma::Spi2DmaChannelCreator as DmaChannelCreator;
} else {
use esp_hal::dma::ChannelCreator as DmaChannelCreator;
}
}

const BUFFER_SIZE: usize = 256;
static LOOP_COUNT: AtomicU32 = AtomicU32::new(0);

pub struct SpiPeripherals {
pub spi: SPI2,
pub dma_channel: DmaChannelCreator,
}

#[embassy_executor::task]
async fn run_spi(peripherals: SpiPeripherals) {
let (rx_buffer, rx_descriptors, tx_buffer, tx_descriptors) = dma_buffers!(3200);
let dma_rx_buf = DmaRxBuf::new(rx_descriptors, rx_buffer).unwrap();
let dma_tx_buf = DmaTxBuf::new(tx_descriptors, tx_buffer).unwrap();

let mut spi = Spi::new(peripherals.spi, 100.kHz(), SpiMode::Mode0)
.with_dma(
peripherals
.dma_channel
.configure_for_async(false, DmaPriority::Priority0),
)
.with_buffers(dma_rx_buf, dma_tx_buf);

let send_buffer = mk_static!([u8; BUFFER_SIZE], [0u8; BUFFER_SIZE]);
loop {
let mut buffer = [0; 8];
embedded_hal_async::spi::SpiBus::transfer(&mut spi, &mut buffer, send_buffer)
.await
.unwrap();
LOOP_COUNT.fetch_add(1, Ordering::Relaxed);
}
}

let peripherals = esp_hal::init(esp_hal::Config::default());
let dma = Dma::new(peripherals.DMA);

let timg0 = TimerGroup::new(peripherals.TIMG0);
esp_hal_embassy::init(timg0.timer0);

cfg_if::cfg_if! {
if #[cfg(pdma)] {
let dma_channel = dma.spi2channel;
} else {
let dma_channel = dma.channel0;
}
}

let spi_peripherals = SpiPeripherals {
spi: peripherals.SPI2,
dma_channel,
};

let cpu1_fnctn = {
move || {
use esp_hal::interrupt::Priority;
use esp_hal_embassy::InterruptExecutor;
let sw_ints = esp_hal::interrupt::software::SoftwareInterruptControl::new(
peripherals.SW_INTERRUPT,
);
let software_interrupt = sw_ints.software_interrupt2;
let hp_executor = mk_static!(
InterruptExecutor<2>,
InterruptExecutor::new(software_interrupt)
);
let high_pri_spawner = hp_executor.start(Priority::Priority2);

// hub75 runs as high priority task
high_pri_spawner.spawn(run_spi(spi_peripherals)).ok();

loop {}
}
};

use esp_hal::cpu_control::{CpuControl, Stack};
const DISPLAY_STACK_SIZE: usize = 8192;
let app_core_stack = mk_static!(Stack<DISPLAY_STACK_SIZE>, Stack::new());
let cpu_control = CpuControl::new(peripherals.CPU_CTRL);
let mut _cpu_control = cpu_control;

#[allow(static_mut_refs)]
let _guard = _cpu_control
.start_app_core(app_core_stack, cpu1_fnctn)
.unwrap();

let mut last = 0u32;
for _ in 0..5 {
Timer::after(Duration::from_millis(200)).await;

let next = LOOP_COUNT.load(Ordering::Relaxed);
assert_ne!(next, last, "stuck");
last = next;
}
}
}

0 comments on commit 820aa2a

Please sign in to comment.