esp-hal/examples/src/bin/spi_loopback.rs
Dániel Buga 92910bf1cb
Constructor consistency update (#2610)
* UART: only implement constructors with config, define ConfigError

* UART: only implement interrupt functions for Blocking

* I2C: fallible constructors

* Lcd/Cam

* SPI

* Update tests and examples

* Changelog

* Add note about ConfigError

* Fmt
2024-11-28 09:28:50 +00:00

62 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(
peripherals.SPI2,
Config::default()
.with_frequency(100.kHz())
.with_mode(SpiMode::Mode0),
)
.unwrap()
.with_sck(sclk)
.with_miso(miso) // order matters
.with_mosi(miso_mosi) // order matters
.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);
}
}