esp-hal/esp32s2-hal/examples/spi_loopback_dma.rs
sanxus c196b67587
Improve spi master constructor (#901)
* make pins optional for Spi::new (master, full-duplex mode)

* add additional method to setup spi pins (master, full-duplex mode)

* add additional method to setup spi pins (master, half-duplex mode)

* remove generic type parameters for Spi::new

* update documentation

* make `with_pins` could be chained

* update CHANGELOG.md

* update CHANGELOG.md

* make the return value of a method like `with_{*}` owned

* fix (maybe?) all broken examples caused by the change (esp32-hal)

* fix (maybe?) all broken examples caused by the change (esp32c2-hal)

* fix (maybe?) all broken examples caused by the change (esp32c3-hal)

* fix (maybe?) all broken examples caused by the change (esp32c6-hal)

* fix (maybe?) all broken examples caused by the change (esp32h2-hal)

* fix (maybe?) all broken examples caused by the change (esp32s2-hal)

* fix (maybe?) all broken examples caused by the change (esp32s3-hal)

* rerun 'cargo fmt' for esp-hal-common

* rerun 'cargo fmt' for the rest of examples
2023-11-08 12:11:58 +01:00

109 lines
2.8 KiB
Rust

//! SPI loopback test using DMA
//!
//! Folowing pins are used:
//! SCLK GPIO36
//! MISO GPIO37
//! MOSI GPIO35
//! CS GPIO34
//!
//! Depending on your target and the board you are using you have to change the
//! pins.
//!
//! This example transfers data via SPI.
//! Connect MISO and MOSI pins to see the outgoing data is read as incoming
//! data.
#![no_std]
#![no_main]
use esp32s2_hal::{
clock::ClockControl,
dma::DmaPriority,
gpio::IO,
pdma::Dma,
peripherals::Peripherals,
prelude::*,
spi::{
master::{prelude::*, Spi},
SpiMode,
},
Delay,
};
use esp_backtrace as _;
use esp_println::println;
#[entry]
fn main() -> ! {
let peripherals = Peripherals::take();
let system = peripherals.SYSTEM.split();
let clocks = ClockControl::boot_defaults(system.clock_control).freeze();
let io = IO::new(peripherals.GPIO, peripherals.IO_MUX);
let sclk = io.pins.gpio36;
let miso = io.pins.gpio37;
let mosi = io.pins.gpio35;
let cs = io.pins.gpio34;
let dma = Dma::new(system.dma);
let dma_channel = dma.spi2channel;
let mut descriptors = [0u32; 8 * 3];
let mut rx_descriptors = [0u32; 8 * 3];
let mut spi = Spi::new(peripherals.SPI2, 100u32.kHz(), SpiMode::Mode0, &clocks)
.with_pins(Some(sclk), Some(mosi), Some(miso), Some(cs))
.with_dma(dma_channel.configure(
false,
&mut descriptors,
&mut rx_descriptors,
DmaPriority::Priority0,
));
let mut delay = Delay::new(&clocks);
// DMA buffer require a static life-time
let mut send = buffer1();
let mut receive = buffer2();
let mut i = 0;
for (i, v) in send.iter_mut().enumerate() {
*v = (i % 255) as u8;
}
loop {
send[0] = i;
send[send.len() - 1] = i;
i = i.wrapping_add(1);
let transfer = spi.dma_transfer(send, receive).unwrap();
// here we could do something else while DMA transfer is in progress
let mut n = 0;
// Check is_done until the transfer is almost done (32000 bytes at 100kHz is
// 2.56 seconds), then move to wait().
while !transfer.is_done() && n < 10 {
delay.delay_ms(250u32);
n += 1;
}
// the buffers and spi is moved into the transfer and we can get it back via
// `wait`
(receive, send, spi) = transfer.wait().unwrap();
println!(
"{:x?} .. {:x?}",
&receive[..10],
&receive[receive.len() - 10..]
);
delay.delay_ms(250u32);
}
}
fn buffer1() -> &'static mut [u8; 32000] {
static mut BUFFER: [u8; 32000] = [0u8; 32000];
unsafe { &mut BUFFER }
}
fn buffer2() -> &'static mut [u8; 32000] {
static mut BUFFER: [u8; 32000] = [0u8; 32000];
unsafe { &mut BUFFER }
}