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

Add SPI Half Duplex Read HIL test #1782

Merged
merged 1 commit into from
Jul 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
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
4 changes: 4 additions & 0 deletions hil-test/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,10 @@ harness = false
name = "spi_full_duplex_dma"
harness = false

[[test]]
name = "spi_half_duplex_read"
harness = false

[[test]]
name = "pcnt"
harness = false
Expand Down
108 changes: 108 additions & 0 deletions hil-test/tests/spi_half_duplex_read.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
//! 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 esp32s3

#![no_std]
#![no_main]

use defmt_rtt as _;
use esp_backtrace as _;

#[cfg(test)]
#[embedded_test::tests]
mod tests {
use esp_hal::{
clock::ClockControl,
dma::{Dma, DmaPriority},
dma_buffers,
gpio::{Io, Level, Output},
peripherals::Peripherals,
prelude::_fugit_RateExtU32,
spi::{
master::{prelude::*, Address, Command, Spi},
SpiDataMode,
SpiMode,
},
system::SystemControl,
};

#[init]
fn init() {}

#[test]
#[timeout(3)]
fn test_spi_reads_correctly_from_gpio_pin() {
const DMA_BUFFER_SIZE: usize = 4;

let peripherals = Peripherals::take();
let system = SystemControl::new(peripherals.SYSTEM);
let clocks = ClockControl::boot_defaults(system.clock_control).freeze();

let io = Io::new(peripherals.GPIO, peripherals.IO_MUX);
let sclk = io.pins.gpio0;
let miso = io.pins.gpio2;

let mut miso_mirror = Output::new(io.pins.gpio3, Level::High);

let dma = Dma::new(peripherals.DMA);

#[cfg(any(feature = "esp32", feature = "esp32s2"))]
let dma_channel = dma.spi2channel;
#[cfg(not(any(feature = "esp32", feature = "esp32s2")))]
let dma_channel = dma.channel0;

let (_, tx_descriptors, mut rx_buffer, rx_descriptors) = dma_buffers!(0, DMA_BUFFER_SIZE);

let mut 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),
tx_descriptors,
rx_descriptors,
);

// Fill with neither 0x00 nor 0xFF.
rx_buffer.fill(5);

// SPI should read '0's from the MISO pin
miso_mirror.set_low();

let transfer = spi
.read(
SpiDataMode::Single,
Command::None,
Address::None,
0,
&mut rx_buffer,
)
.unwrap();
transfer.wait().unwrap();

assert_eq!(rx_buffer, &[0x00; DMA_BUFFER_SIZE]);

// SPI should read '1's from the MISO pin
miso_mirror.set_high();

let transfer = spi
.read(
SpiDataMode::Single,
Command::None,
Address::None,
0,
&mut rx_buffer,
)
.unwrap();
transfer.wait().unwrap();

assert_eq!(rx_buffer, &[0xFF; DMA_BUFFER_SIZE]);
}
}