|
| 1 | +//! Reproduction and regression test for a sneaky issue. |
| 2 | +
|
| 3 | +//% CHIPS: esp32 esp32s2 esp32s3 |
| 4 | +//% FEATURES: integrated-timers |
| 5 | +//% FEATURES: generic-queue |
| 6 | + |
| 7 | +#![no_std] |
| 8 | +#![no_main] |
| 9 | + |
| 10 | +use embassy_time::{Duration, Instant, Ticker}; |
| 11 | +use esp_hal::{ |
| 12 | + dma::{Dma, DmaPriority, DmaRxBuf, DmaTxBuf}, |
| 13 | + dma_buffers, |
| 14 | + interrupt::{software::SoftwareInterruptControl, Priority}, |
| 15 | + peripherals::SPI3, |
| 16 | + prelude::*, |
| 17 | + spi::{ |
| 18 | + master::{Spi, SpiDma}, |
| 19 | + FullDuplexMode, |
| 20 | + SpiMode, |
| 21 | + }, |
| 22 | + timer::{timg::TimerGroup, ErasedTimer}, |
| 23 | + Async, |
| 24 | +}; |
| 25 | +use esp_hal_embassy::InterruptExecutor; |
| 26 | +use hil_test as _; |
| 27 | + |
| 28 | +cfg_if::cfg_if! { |
| 29 | + if #[cfg(any( |
| 30 | + feature = "esp32", |
| 31 | + feature = "esp32s2", |
| 32 | + ))] { |
| 33 | + use esp_hal::dma::Spi3DmaChannel as DmaChannel1; |
| 34 | + } else { |
| 35 | + use esp_hal::dma::DmaChannel1; |
| 36 | + } |
| 37 | +} |
| 38 | + |
| 39 | +macro_rules! mk_static { |
| 40 | + ($t:ty,$val:expr) => {{ |
| 41 | + static STATIC_CELL: static_cell::StaticCell<$t> = static_cell::StaticCell::new(); |
| 42 | + #[deny(unused_attributes)] |
| 43 | + let x = STATIC_CELL.uninit().write(($val)); |
| 44 | + x |
| 45 | + }}; |
| 46 | +} |
| 47 | + |
| 48 | +#[embassy_executor::task] |
| 49 | +async fn interrupt_driven_task(spi: SpiDma<'static, SPI3, DmaChannel1, FullDuplexMode, Async>) { |
| 50 | + let mut ticker = Ticker::every(Duration::from_millis(1)); |
| 51 | + |
| 52 | + let (tx_buffer, tx_descriptors, rx_buffer, rx_descriptors) = dma_buffers!(128); |
| 53 | + let dma_tx_buf = DmaTxBuf::new(tx_descriptors, tx_buffer).unwrap(); |
| 54 | + let dma_rx_buf = DmaRxBuf::new(rx_descriptors, rx_buffer).unwrap(); |
| 55 | + |
| 56 | + let mut spi = spi.with_buffers(dma_tx_buf, dma_rx_buf); |
| 57 | + |
| 58 | + loop { |
| 59 | + let mut buffer: [u8; 8] = [0; 8]; |
| 60 | + |
| 61 | + spi.transfer_in_place_async(&mut buffer).await.unwrap(); |
| 62 | + |
| 63 | + ticker.next().await; |
| 64 | + } |
| 65 | +} |
| 66 | + |
| 67 | +#[cfg(test)] |
| 68 | +#[embedded_test::tests(executor = esp_hal_embassy::Executor::new())] |
| 69 | +mod test { |
| 70 | + use super::*; |
| 71 | + |
| 72 | + #[test] |
| 73 | + #[timeout(3)] |
| 74 | + async fn run_interrupt_executor_test() { |
| 75 | + let (peripherals, clocks) = esp_hal::init(esp_hal::Config::default()); |
| 76 | + |
| 77 | + let timg0 = TimerGroup::new(peripherals.TIMG0, &clocks); |
| 78 | + esp_hal_embassy::init( |
| 79 | + &clocks, |
| 80 | + [ |
| 81 | + ErasedTimer::from(timg0.timer0), |
| 82 | + ErasedTimer::from(timg0.timer1), |
| 83 | + ], |
| 84 | + ); |
| 85 | + |
| 86 | + let dma = Dma::new(peripherals.DMA); |
| 87 | + |
| 88 | + cfg_if::cfg_if! { |
| 89 | + if #[cfg(any(feature = "esp32", feature = "esp32s2"))] { |
| 90 | + let dma_channel1 = dma.spi2channel; |
| 91 | + let dma_channel2 = dma.spi3channel; |
| 92 | + } else { |
| 93 | + let dma_channel1 = dma.channel0; |
| 94 | + let dma_channel2 = dma.channel1; |
| 95 | + } |
| 96 | + } |
| 97 | + |
| 98 | + let (tx_buffer, tx_descriptors, rx_buffer, rx_descriptors) = dma_buffers!(1024); |
| 99 | + let dma_tx_buf = DmaTxBuf::new(tx_descriptors, tx_buffer).unwrap(); |
| 100 | + let dma_rx_buf = DmaRxBuf::new(rx_descriptors, rx_buffer).unwrap(); |
| 101 | + |
| 102 | + let mut spi = Spi::new(peripherals.SPI2, 100.kHz(), SpiMode::Mode0, &clocks) |
| 103 | + .with_dma(dma_channel1.configure_for_async(false, DmaPriority::Priority0)) |
| 104 | + .with_buffers(dma_tx_buf, dma_rx_buf); |
| 105 | + |
| 106 | + let spi2 = Spi::new(peripherals.SPI3, 100.kHz(), SpiMode::Mode0, &clocks) |
| 107 | + .with_dma(dma_channel2.configure_for_async(false, DmaPriority::Priority0)); |
| 108 | + |
| 109 | + let sw_ints = SoftwareInterruptControl::new(peripherals.SW_INTERRUPT); |
| 110 | + |
| 111 | + let interrupt_executor = mk_static!( |
| 112 | + InterruptExecutor<1>, |
| 113 | + InterruptExecutor::new(sw_ints.software_interrupt1) |
| 114 | + ); |
| 115 | + |
| 116 | + let spawner = interrupt_executor.start(Priority::Priority3); |
| 117 | + |
| 118 | + spawner.spawn(interrupt_driven_task(spi2)).unwrap(); |
| 119 | + |
| 120 | + let start = Instant::now(); |
| 121 | + let mut buffer: [u8; 1024] = [0; 1024]; |
| 122 | + loop { |
| 123 | + spi.transfer_in_place_async(&mut buffer).await.unwrap(); |
| 124 | + |
| 125 | + if start.elapsed() > Duration::from_secs(1) { |
| 126 | + break; |
| 127 | + } |
| 128 | + } |
| 129 | + } |
| 130 | +} |
0 commit comments