esp-hal/examples/src/bin/spi_loopback.rs
Dániel Buga fbc57542a8
Remove pins from Io (#2508)
* Split pins off of Io

* Remove the GPIO peripheral

* p.GPIO
2024-11-12 10:36:25 +00:00

63 lines
1.3 KiB
Rust

//! SPI loopback test
//!
//! The following wiring is assumed:
//! - SCLK => GPIO0
//! - MISO/MOSI => GPIO2
//! - CS => GPIO5
//!
//! Depending on your target and the board you are using you have to change the
//! pins.
//!
//! This example transfers data via SPI.
//% CHIPS: esp32 esp32c2 esp32c3 esp32c6 esp32h2 esp32s2 esp32s3
#![no_std]
#![no_main]
use esp_backtrace as _;
use esp_hal::{
delay::Delay,
peripheral::Peripheral,
prelude::*,
spi::{
master::{Config, Spi},
SpiMode,
},
};
use esp_println::println;
#[entry]
fn main() -> ! {
let peripherals = esp_hal::init(esp_hal::Config::default());
let sclk = peripherals.GPIO0;
let miso_mosi = peripherals.GPIO2;
let cs = peripherals.GPIO5;
let miso = unsafe { miso_mosi.clone_unchecked() };
let mut spi = Spi::new_with_config(
peripherals.SPI2,
Config {
frequency: 100.kHz(),
mode: SpiMode::Mode0,
..Config::default()
},
)
.with_sck(sclk)
.with_mosi(miso_mosi)
.with_miso(miso)
.with_cs(cs);
let delay = Delay::new();
loop {
let mut data = [0xde, 0xca, 0xfb, 0xad];
spi.transfer(&mut data).unwrap();
println!("{:x?}", data);
delay.delay_millis(250);
}
}