* Rework hal initialization * Turn sw interrupt control into a virtual peripheral * Return a tuple instead of a named struct * Fix docs * Remove SystemClockControl * Move software interrupts under interrupt * Re-document what's left in system * Update time docs * Update sw int docs * Introduce Config * Fix tests * Remove redundant inits * Doc * Clean up examples&tests * Update tests * Add changelog entry * Start migration guide * Restore some convenience-imports * Remove Config from prelude
170 lines
4.2 KiB
Rust
170 lines
4.2 KiB
Rust
//! SPI Half Duplex Read Test
|
|
//!
|
|
//! Folowing pins are used:
|
|
//! SCLK GPIO0
|
|
//! MISO GPIO2
|
|
//!
|
|
//! GPIO GPIO3
|
|
//!
|
|
//! Connect MISO (GPIO2) and GPIO (GPIO3) pins.
|
|
|
|
//% CHIPS: esp32 esp32c2 esp32c3 esp32c6 esp32h2 esp32s2 esp32s3
|
|
|
|
#![no_std]
|
|
#![no_main]
|
|
|
|
use esp_hal::{
|
|
dma::{Dma, DmaPriority, DmaRxBuf, DmaTxBuf},
|
|
dma_buffers,
|
|
gpio::{GpioPin, Io, Level, Output},
|
|
peripherals::SPI2,
|
|
prelude::*,
|
|
spi::{
|
|
master::{Address, Command, HalfDuplexReadWrite, Spi, SpiDma},
|
|
HalfDuplexMode,
|
|
SpiDataMode,
|
|
SpiMode,
|
|
},
|
|
Blocking,
|
|
};
|
|
use hil_test as _;
|
|
|
|
cfg_if::cfg_if! {
|
|
if #[cfg(any(
|
|
feature = "esp32",
|
|
feature = "esp32s2",
|
|
))] {
|
|
use esp_hal::dma::Spi2DmaChannel as DmaChannel0;
|
|
} else {
|
|
use esp_hal::dma::DmaChannel0;
|
|
}
|
|
}
|
|
|
|
struct Context {
|
|
spi: SpiDma<'static, SPI2, DmaChannel0, HalfDuplexMode, Blocking>,
|
|
miso_mirror: Output<'static, GpioPin<3>>,
|
|
}
|
|
|
|
#[cfg(test)]
|
|
#[embedded_test::tests]
|
|
mod tests {
|
|
use defmt::assert_eq;
|
|
|
|
use super::*;
|
|
|
|
#[init]
|
|
fn init() -> Context {
|
|
let (peripherals, clocks) = esp_hal::init(esp_hal::Config::default());
|
|
|
|
let io = Io::new(peripherals.GPIO, peripherals.IO_MUX);
|
|
let sclk = io.pins.gpio0;
|
|
let miso = io.pins.gpio2;
|
|
|
|
let miso_mirror = Output::new(io.pins.gpio3, Level::High);
|
|
|
|
let dma = Dma::new(peripherals.DMA);
|
|
|
|
cfg_if::cfg_if! {
|
|
if #[cfg(any(feature = "esp32", feature = "esp32s2"))] {
|
|
let dma_channel = dma.spi2channel;
|
|
} else {
|
|
let dma_channel = dma.channel0;
|
|
}
|
|
}
|
|
|
|
let spi = Spi::new_half_duplex(peripherals.SPI2, 100.kHz(), SpiMode::Mode0, &clocks)
|
|
.with_sck(sclk)
|
|
.with_miso(miso)
|
|
.with_dma(dma_channel.configure(false, DmaPriority::Priority0));
|
|
|
|
Context { spi, miso_mirror }
|
|
}
|
|
|
|
#[test]
|
|
#[timeout(3)]
|
|
fn test_spi_reads_correctly_from_gpio_pin(mut ctx: Context) {
|
|
const DMA_BUFFER_SIZE: usize = 4;
|
|
|
|
let (buffer, descriptors, _, _) = dma_buffers!(DMA_BUFFER_SIZE, 0);
|
|
let mut dma_rx_buf = DmaRxBuf::new(descriptors, buffer).unwrap();
|
|
|
|
// SPI should read '0's from the MISO pin
|
|
ctx.miso_mirror.set_low();
|
|
|
|
let mut spi = ctx.spi;
|
|
|
|
let transfer = spi
|
|
.read(
|
|
SpiDataMode::Single,
|
|
Command::None,
|
|
Address::None,
|
|
0,
|
|
dma_rx_buf,
|
|
)
|
|
.map_err(|e| e.0)
|
|
.unwrap();
|
|
(spi, dma_rx_buf) = transfer.wait();
|
|
|
|
assert_eq!(dma_rx_buf.as_slice(), &[0x00; DMA_BUFFER_SIZE]);
|
|
|
|
// SPI should read '1's from the MISO pin
|
|
ctx.miso_mirror.set_high();
|
|
|
|
let transfer = spi
|
|
.read(
|
|
SpiDataMode::Single,
|
|
Command::None,
|
|
Address::None,
|
|
0,
|
|
dma_rx_buf,
|
|
)
|
|
.map_err(|e| e.0)
|
|
.unwrap();
|
|
|
|
(_, dma_rx_buf) = transfer.wait();
|
|
|
|
assert_eq!(dma_rx_buf.as_slice(), &[0xFF; DMA_BUFFER_SIZE]);
|
|
}
|
|
|
|
#[test]
|
|
#[timeout(3)]
|
|
fn test_spidmabus_reads_correctly_from_gpio_pin(mut ctx: Context) {
|
|
const DMA_BUFFER_SIZE: usize = 4;
|
|
|
|
let (buffer, descriptors, tx, txd) = dma_buffers!(DMA_BUFFER_SIZE, 1);
|
|
let dma_rx_buf = DmaRxBuf::new(descriptors, buffer).unwrap();
|
|
let dma_tx_buf = DmaTxBuf::new(txd, tx).unwrap();
|
|
|
|
let mut spi = ctx.spi.with_buffers(dma_tx_buf, dma_rx_buf);
|
|
|
|
// SPI should read '0's from the MISO pin
|
|
ctx.miso_mirror.set_low();
|
|
|
|
let mut buffer = [0xAA; DMA_BUFFER_SIZE];
|
|
spi.read(
|
|
SpiDataMode::Single,
|
|
Command::None,
|
|
Address::None,
|
|
0,
|
|
&mut buffer,
|
|
)
|
|
.unwrap();
|
|
|
|
assert_eq!(buffer.as_slice(), &[0x00; DMA_BUFFER_SIZE]);
|
|
|
|
// SPI should read '1's from the MISO pin
|
|
ctx.miso_mirror.set_high();
|
|
|
|
spi.read(
|
|
SpiDataMode::Single,
|
|
Command::None,
|
|
Address::None,
|
|
0,
|
|
&mut buffer,
|
|
)
|
|
.unwrap();
|
|
|
|
assert_eq!(buffer.as_slice(), &[0xFF; DMA_BUFFER_SIZE]);
|
|
}
|
|
}
|