esp-idf-hal/examples/spi_loopback.rs
Frederick Vollbrecht 13ba61fb0b
RFC: Multi CS Spi Implementation (#150)
* initial draft

* working alternative without using i32 id's

* EspSpiDevice + SpiDriverMaster / no global statics

* start splitting implementation in spi & spi_pool

* impl SpiDevice for EspSpiDevice

* spi2_pool draft (spi with software based cs)

* pre master fetch up

* Driver accepts Optional DMA channel + fetch master

* replaced: std with core, mutex with hal's cs

* replaced HashMap with Array

* merged spi2 into spi

* clean up

* replace std with core

* updated loopback example,  spi takes dma directly

* moved criticalsection guard out of spi to spi_pool

* added paragraph to the release.md

* rm fn to add / rm device from bus or update config

* rm stored config from EspSpiDevice

* fmt'd

* grammer + changelog update

* implements inverted chipselect for spi.rs #119

* device with optional cs + guarded soft_cs wrapper

* fmt'd

* add pre / post cs delay functionality for wrapper

* no bus.finish for dev with no cs. partial fix #99

* spi driver renaming

* updated naming + locking with &mut SpiDeviceDriver

* updated naming + locking with &mut SpiDeviceDriver

* clean up

* updated CHANGELOG
2022-11-12 08:54:16 +02:00

69 lines
2.0 KiB
Rust

//! SPI loopback test
//!
//! Folowing pins are used:
//! SCLK GPIO6
//! SDI GPIO2
//! SDO GPIO7
//! CS_1 GPIO10
//! CS_2 GPIO3
//!
//! Depending on your target and the board you are using you have to change the pins.
//!
//! This example transfers data via SPI.
//! Connect SDI and SDO pins to see the outgoing data is read as incoming data.
use embedded_hal::spi::SpiDevice;
use esp_idf_hal::delay::FreeRtos;
use esp_idf_hal::peripherals::Peripherals;
use esp_idf_hal::prelude::*;
use esp_idf_hal::spi::*;
fn main() -> anyhow::Result<()> {
esp_idf_sys::link_patches();
let peripherals = Peripherals::take().unwrap();
let spi = peripherals.spi2;
let sclk = peripherals.pins.gpio6;
let serial_in = peripherals.pins.gpio2; // SDI
let serial_out = peripherals.pins.gpio7; // SDO
let cs_1 = peripherals.pins.gpio10;
let cs_2 = peripherals.pins.gpio3;
println!("Starting SPI loopback test");
let mut spi = SpiDriver::new::<SPI2>(spi, sclk, serial_out, Some(serial_in), Dma::Disabled)?;
let config_1 = config::Config::new().baudrate(26.MHz().into());
let mut device_1 = SpiDeviceDriver::new(&spi, cs_1, &config_2)?;
let config_2 = config::Config::new().baudrate(13.MHz().into());
let mut device_2 = SpiDeviceDriver::new(&spi, cs_2, &config_2)?;
let mut read = [0u8; 4];
let write = [0xde, 0xad, 0xbe, 0xef];
let mut in_place_buf = [0xde, 0xad, 0xbe, 0xef];
loop {
// we are using thread::sleep here to make sure the watchdog isn't triggered
FreeRtos::delay_ms(500);
device_1.transfer(&mut read, &write)?;
println!(
"Device {:?} : Wrote {:x?}, read {:x?}",
device_1.cs_gpio_number(),
write,
read
);
println!(
"Device {:?} : To Write {:x?} ... ",
device_2.cs_gpio_number(),
in_place_buf
);
device_2.transaction(|bus| bus.transfer_in_place(&mut in_place_buf));
println!("... read {:x?}", in_place_buf);
}
}