esp-hal/esp32s2-hal/examples/spi_eh1_loopback.rs
Scott Mabin 9064177e99
Initial embassy support (#225)
* wip: timg embassy driver

- read_raw on timg renamed to now()
- timg initialized and stored in static for use in the embassy driver
- timg sets alarm value
- untested whether alarms actually trigger

* TIMG timer driver for esp32, esp32s3

- Adds the timg timer block as a time driver for embassy
- Not enabled on the C3 as it only has one timer block, better to use
  systimer
- s2 example added but can't build due to atomic requirements in
  futures-core

* Add S2 atomic support with emulation, fixup embassy support for the S2

* Move executor & static-cell to dev deps. Make eha optional

* Add c2 support, run fmt

* Update to crates.io embassy releases

* Update eha

* update timg time driver to new trait

* Remove exception feature of esp-backtrace and use the user handler for backtracing

* Add async testing workflow

* Update systick example

* Fix S2 examples

* Update xtensa-toolchain

* set rustflags for s2 target

* Disable systick for esp32s2 until we can fix the noted issues

* review improvements

- Fix intr prio array being off by one
- emabssy time prio interrupt set to max prio
- use cfg instead of feature for systick detection

* Update example time delays
2022-11-09 08:04:38 -08:00

137 lines
3.9 KiB
Rust

//! SPI loopback test
//!
//! 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 embedded_hal_1::spi::SpiBus;
use esp32s2_hal::{
clock::ClockControl,
gpio::IO,
pac::Peripherals,
prelude::*,
spi::{Spi, SpiMode},
timer::TimerGroup,
Delay,
Rtc,
};
use esp_backtrace as _;
use esp_println::{print, println};
use xtensa_atomic_emulation_trap as _;
use xtensa_lx_rt::entry;
#[entry]
fn main() -> ! {
let peripherals = Peripherals::take().unwrap();
let mut system = peripherals.SYSTEM.split();
let clocks = ClockControl::boot_defaults(system.clock_control).freeze();
// Disable the watchdog timers. For the ESP32-C3, this includes the Super WDT,
// the RTC WDT, and the TIMG WDTs.
let mut rtc = Rtc::new(peripherals.RTC_CNTL);
let timer_group0 = TimerGroup::new(peripherals.TIMG0, &clocks);
let mut wdt = timer_group0.wdt;
wdt.disable();
rtc.rwdt.disable();
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 mut spi = Spi::new(
peripherals.SPI2,
sclk,
mosi,
miso,
cs,
1000u32.kHz(),
SpiMode::Mode0,
&mut system.peripheral_clock_control,
&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);
}
}
#[xtensa_lx_rt::exception]
fn exception(cause: xtensa_lx_rt::exception::ExceptionCause, frame: xtensa_lx_rt::exception::Context) {
use esp_println::*;
println!("\n\nException occured {:?} {:x?}", cause, frame);
let backtrace = esp_backtrace::arch::backtrace();
for b in backtrace.iter() {
if let Some(addr) = b {
println!("0x{:x}", addr)
}
}
}