mirror of
				https://github.com/esp-rs/esp-hal.git
				synced 2025-11-04 06:43:55 +00:00 
			
		
		
		
	* Move SPI-related traits into their own preludes in the `spi` module * Remove the `embedded-hal-async` trait re-exports and the `eh1` module from the prelude * Update `CHANGELOG.md`
		
			
				
	
	
		
			112 lines
		
	
	
		
			3.1 KiB
		
	
	
	
		
			Rust
		
	
	
	
	
	
			
		
		
	
	
			112 lines
		
	
	
		
			3.1 KiB
		
	
	
	
		
			Rust
		
	
	
	
	
	
//! SPI loopback test
 | 
						|
//!
 | 
						|
//! Folowing pins are used:
 | 
						|
//! SCLK    GPIO6
 | 
						|
//! MISO    GPIO2
 | 
						|
//! MOSI    GPIO7
 | 
						|
//! CS      GPIO10
 | 
						|
//!
 | 
						|
//! 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 embedded_hal_1::spi::SpiBus;
 | 
						|
use esp32c6_hal::{
 | 
						|
    clock::ClockControl,
 | 
						|
    gpio::IO,
 | 
						|
    peripherals::Peripherals,
 | 
						|
    prelude::*,
 | 
						|
    spi::{
 | 
						|
        master::{prelude::*, Spi},
 | 
						|
        SpiMode,
 | 
						|
    },
 | 
						|
    Delay,
 | 
						|
};
 | 
						|
use esp_backtrace as _;
 | 
						|
use esp_println::{print, 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.gpio6;
 | 
						|
    let miso = io.pins.gpio2;
 | 
						|
    let mosi = io.pins.gpio7;
 | 
						|
    let cs = io.pins.gpio10;
 | 
						|
 | 
						|
    let mut spi = Spi::new(
 | 
						|
        peripherals.SPI2,
 | 
						|
        sclk,
 | 
						|
        mosi,
 | 
						|
        miso,
 | 
						|
        cs,
 | 
						|
        1000u32.kHz(),
 | 
						|
        SpiMode::Mode0,
 | 
						|
        &clocks,
 | 
						|
    );
 | 
						|
 | 
						|
    let mut delay = Delay::new(&clocks);
 | 
						|
    println!("=== SPI example with embedded-hal-1 traits ===");
 | 
						|
 | 
						|
    loop {
 | 
						|
        // --- Symmetric transfer (Read as much as we write) ---
 | 
						|
        print!("Starting symmetric transfer...");
 | 
						|
        let write = [0xde, 0xad, 0xbe, 0xef];
 | 
						|
        let mut read: [u8; 4] = [0x00u8; 4];
 | 
						|
 | 
						|
        SpiBus::transfer(&mut spi, &mut read[..], &write[..]).expect("Symmetric transfer failed");
 | 
						|
        assert_eq!(write, read);
 | 
						|
        println!(" SUCCESS");
 | 
						|
        delay.delay_ms(250u32);
 | 
						|
 | 
						|
        // --- Asymmetric transfer (Read more than we write) ---
 | 
						|
        print!("Starting asymetric transfer (read > write)...");
 | 
						|
        let mut read: [u8; 4] = [0x00; 4];
 | 
						|
 | 
						|
        SpiBus::transfer(&mut spi, &mut read[0..2], &write[..])
 | 
						|
            .expect("Asymmetric transfer failed");
 | 
						|
        assert_eq!(write[0], read[0]);
 | 
						|
        assert_eq!(read[2], 0x00u8);
 | 
						|
        println!(" SUCCESS");
 | 
						|
        delay.delay_ms(250u32);
 | 
						|
 | 
						|
        // --- Symmetric transfer with huge buffer ---
 | 
						|
        // Only your RAM is the limit!
 | 
						|
        print!("Starting huge transfer...");
 | 
						|
        let mut write = [0x55u8; 4096];
 | 
						|
        for byte in 0..write.len() {
 | 
						|
            write[byte] = byte as u8;
 | 
						|
        }
 | 
						|
        let mut read = [0x00u8; 4096];
 | 
						|
 | 
						|
        SpiBus::transfer(&mut spi, &mut read[..], &write[..]).expect("Huge transfer failed");
 | 
						|
        assert_eq!(write, read);
 | 
						|
        println!(" SUCCESS");
 | 
						|
        delay.delay_ms(250u32);
 | 
						|
 | 
						|
        // --- Symmetric transfer with huge buffer in-place (No additional allocation
 | 
						|
        // needed) ---
 | 
						|
        print!("Starting huge transfer (in-place)...");
 | 
						|
        let mut write = [0x55u8; 4096];
 | 
						|
        for byte in 0..write.len() {
 | 
						|
            write[byte] = byte as u8;
 | 
						|
        }
 | 
						|
 | 
						|
        SpiBus::transfer_in_place(&mut spi, &mut write[..]).expect("Huge transfer failed");
 | 
						|
        for byte in 0..write.len() {
 | 
						|
            assert_eq!(write[byte], byte as u8);
 | 
						|
        }
 | 
						|
        println!(" SUCCESS");
 | 
						|
        delay.delay_ms(250u32);
 | 
						|
    }
 | 
						|
}
 |