Skip to content

Commit 61d6b0f

Browse files
committed
Add qspi_write_read test
1 parent 04c9d72 commit 61d6b0f

File tree

2 files changed

+206
-0
lines changed

2 files changed

+206
-0
lines changed

hil-test/Cargo.toml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,10 @@ harness = false
7575
name = "qspi_write"
7676
harness = false
7777

78+
[[test]]
79+
name = "qspi_write_read"
80+
harness = false
81+
7882
[[test]]
7983
name = "spi_full_duplex"
8084
harness = false

hil-test/tests/qspi_write_read.rs

Lines changed: 202 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,202 @@
1+
//! QSPI Write + Read Test
2+
//!
3+
//! Make sure issue #1860 doesn't affect us
4+
//!
5+
//! Following pins are used:
6+
//! MOSI/MISO GPIO2
7+
//!
8+
//! GPIO GPIO3
9+
//!
10+
//! Connect MOSI/MISO (GPIO2) and GPIO (GPIO3) pins.
11+
12+
//% CHIPS: esp32 esp32c6 esp32h2 esp32s2 esp32s3
13+
14+
#![no_std]
15+
#![no_main]
16+
17+
use esp_hal::{
18+
clock::{ClockControl, Clocks},
19+
dma::{Channel, Dma, DmaPriority, DmaRxBuf, DmaTxBuf},
20+
dma_buffers,
21+
gpio::{GpioPin, Io, Level, Output},
22+
peripherals::Peripherals,
23+
prelude::*,
24+
spi::{
25+
master::{Address, Command, Spi, SpiDma},
26+
HalfDuplexMode,
27+
SpiDataMode,
28+
SpiMode,
29+
},
30+
system::SystemControl,
31+
Blocking,
32+
};
33+
use hil_test as _;
34+
35+
cfg_if::cfg_if! {
36+
if #[cfg(any(
37+
feature = "esp32",
38+
feature = "esp32s2",
39+
))] {
40+
use esp_hal::dma::Spi2DmaChannel as DmaChannel0;
41+
} else {
42+
use esp_hal::dma::DmaChannel0;
43+
}
44+
}
45+
46+
struct Context {
47+
spi: esp_hal::peripherals::SPI2,
48+
dma_channel: Channel<'static, DmaChannel0, Blocking>,
49+
mosi: esp_hal::gpio::GpioPin<2>,
50+
mosi_mirror: Output<'static, GpioPin<3>>,
51+
clocks: Clocks<'static>,
52+
}
53+
54+
fn execute(
55+
mut spi: SpiDma<'static, esp_hal::peripherals::SPI2, DmaChannel0, HalfDuplexMode, Blocking>,
56+
mut mosi_mirror: Output<'static, GpioPin<3>>,
57+
wanted: u8,
58+
) {
59+
const DMA_BUFFER_SIZE: usize = 4;
60+
61+
let (buffer, descriptors, rx_buffer, rx_descriptors) =
62+
dma_buffers!(DMA_BUFFER_SIZE, DMA_BUFFER_SIZE);
63+
let mut dma_tx_buf = DmaTxBuf::new(descriptors, buffer).unwrap();
64+
let mut dma_rx_buf = DmaRxBuf::new(rx_descriptors, rx_buffer).unwrap();
65+
66+
dma_tx_buf.fill(&[0xff; DMA_BUFFER_SIZE]);
67+
68+
let transfer = spi
69+
.write(
70+
SpiDataMode::Quad,
71+
Command::Command8(wanted as u16, SpiDataMode::Quad),
72+
Address::Address24(
73+
wanted as u32 | (wanted as u32) << 8 | (wanted as u32) << 16,
74+
SpiDataMode::Quad,
75+
),
76+
0,
77+
dma_tx_buf,
78+
)
79+
.map_err(|e| e.0)
80+
.unwrap();
81+
(spi, _) = transfer.wait();
82+
83+
mosi_mirror.set_low();
84+
85+
let transfer = spi
86+
.read(
87+
SpiDataMode::Quad,
88+
Command::None,
89+
Address::None,
90+
0,
91+
dma_rx_buf,
92+
)
93+
.map_err(|e| e.0)
94+
.unwrap();
95+
(_, dma_rx_buf) = transfer.wait();
96+
assert_eq!(dma_rx_buf.as_slice(), &[wanted; DMA_BUFFER_SIZE]);
97+
}
98+
99+
#[cfg(test)]
100+
#[embedded_test::tests]
101+
mod tests {
102+
use super::*;
103+
104+
#[init]
105+
fn init() -> Context {
106+
let peripherals = Peripherals::take();
107+
let system = SystemControl::new(peripherals.SYSTEM);
108+
let clocks = ClockControl::boot_defaults(system.clock_control).freeze();
109+
110+
let io = Io::new(peripherals.GPIO, peripherals.IO_MUX);
111+
let mosi = io.pins.gpio2;
112+
let mosi_mirror = Output::new(io.pins.gpio3, Level::High);
113+
114+
let dma = Dma::new(peripherals.DMA);
115+
116+
cfg_if::cfg_if! {
117+
if #[cfg(any(feature = "esp32", feature = "esp32s2"))] {
118+
let dma_channel = dma.spi2channel;
119+
} else {
120+
let dma_channel = dma.channel0;
121+
}
122+
}
123+
124+
let dma_channel = dma_channel.configure(false, DmaPriority::Priority0);
125+
126+
Context {
127+
spi: peripherals.SPI2,
128+
dma_channel,
129+
mosi,
130+
mosi_mirror,
131+
clocks,
132+
}
133+
}
134+
135+
#[test]
136+
#[timeout(3)]
137+
fn test_spi_writes_correctly_to_pin_0(ctx: Context) {
138+
let spi = Spi::new_half_duplex(ctx.spi, 100.kHz(), SpiMode::Mode0, &ctx.clocks)
139+
.with_pins(
140+
esp_hal::gpio::NO_PIN,
141+
Some(ctx.mosi),
142+
esp_hal::gpio::NO_PIN,
143+
esp_hal::gpio::NO_PIN,
144+
esp_hal::gpio::NO_PIN,
145+
esp_hal::gpio::NO_PIN,
146+
)
147+
.with_dma(ctx.dma_channel);
148+
149+
super::execute(spi, ctx.mosi_mirror, !0b0001_0001);
150+
}
151+
152+
#[test]
153+
#[timeout(3)]
154+
fn test_spi_writes_correctly_to_pin_1(ctx: Context) {
155+
let spi = Spi::new_half_duplex(ctx.spi, 100.kHz(), SpiMode::Mode0, &ctx.clocks)
156+
.with_pins(
157+
esp_hal::gpio::NO_PIN,
158+
esp_hal::gpio::NO_PIN,
159+
Some(ctx.mosi),
160+
esp_hal::gpio::NO_PIN,
161+
esp_hal::gpio::NO_PIN,
162+
esp_hal::gpio::NO_PIN,
163+
)
164+
.with_dma(ctx.dma_channel);
165+
166+
super::execute(spi, ctx.mosi_mirror, !0b0010_0010);
167+
}
168+
169+
#[test]
170+
#[timeout(3)]
171+
fn test_spi_writes_correctly_to_pin_2(ctx: Context) {
172+
let spi = Spi::new_half_duplex(ctx.spi, 100.kHz(), SpiMode::Mode0, &ctx.clocks)
173+
.with_pins(
174+
esp_hal::gpio::NO_PIN,
175+
esp_hal::gpio::NO_PIN,
176+
esp_hal::gpio::NO_PIN,
177+
Some(ctx.mosi),
178+
esp_hal::gpio::NO_PIN,
179+
esp_hal::gpio::NO_PIN,
180+
)
181+
.with_dma(ctx.dma_channel);
182+
183+
super::execute(spi, ctx.mosi_mirror, !0b0100_0100);
184+
}
185+
186+
#[test]
187+
#[timeout(3)]
188+
fn test_spi_writes_correctly_to_pin_3(ctx: Context) {
189+
let spi = Spi::new_half_duplex(ctx.spi, 100.kHz(), SpiMode::Mode0, &ctx.clocks)
190+
.with_pins(
191+
esp_hal::gpio::NO_PIN,
192+
esp_hal::gpio::NO_PIN,
193+
esp_hal::gpio::NO_PIN,
194+
esp_hal::gpio::NO_PIN,
195+
Some(ctx.mosi),
196+
esp_hal::gpio::NO_PIN,
197+
)
198+
.with_dma(ctx.dma_channel);
199+
200+
super::execute(spi, ctx.mosi_mirror, !0b1000_1000);
201+
}
202+
}

0 commit comments

Comments
 (0)