esp-hal/esp32h2-hal/examples/embassy_spi.rs
Jesse Braham d86300f799
Add all SPI examples for the ESP32-H2 (#549)
* Update the `GDMA` driver to support the ESP32-H2

* Update the `SPI` driver to support the ESP32-H2

* Add `SPI` examples for ESP32-H2

* Update CHANGELOG

* Remove copy-pasted references to ESP32-C6

* Update GPIO pins used in SPI examples, add `qspi_flash` example

* Update SPI clock configuration to produce correct clock rate

* Correct comment regarding clock source frequency

Co-authored-by: Sergio Gasquez Arcos <sergio.gasquez@gmail.com>

* H2: Add PLL_48M_CLK src to ClockControl and RawClocks

* H2: Use PLL_48M_CLK as SPI clk src

* H2: cleanup commented block in SPI driver

* H2: update docs comment in embassy_spi example

* fmt

* Add a new line in embassy_spi example

---------

Co-authored-by: Sergio Gasquez Arcos <sergio.gasquez@gmail.com>
Co-authored-by: Juraj Sadel <juraj.sadel@espressif.com>
2023-05-31 09:49:13 +02:00

156 lines
4.0 KiB
Rust

//! Embassy SPI
//!
//! Folowing pins are used:
//! SCLK GPIO1
//! MISO GPIO2
//! MOSI GPIO3
//! CS GPIO11
//!
//! Depending on your target and the board you are using you have to change the
//! pins.
//!
//! Connect MISO and MOSI pins to see the outgoing data is read as incoming
//! data.
//!
//! This is an example of running the embassy executor with SPI.
#![no_std]
#![no_main]
#![feature(type_alias_impl_trait)]
use embassy_executor::Executor;
use embassy_time::{Duration, Timer};
use esp32h2_hal::{
clock::ClockControl,
dma::{DmaPriority, *},
embassy,
gdma::*,
peripherals::Peripherals,
prelude::*,
spi::{dma::SpiDma, FullDuplexMode, Spi, SpiMode},
timer::TimerGroup,
Rtc,
IO,
};
use esp_backtrace as _;
use static_cell::StaticCell;
macro_rules! singleton {
($val:expr) => {{
type T = impl Sized;
static STATIC_CELL: StaticCell<T> = StaticCell::new();
let (x,) = STATIC_CELL.init(($val,));
x
}};
}
pub type SpiType<'d> = SpiDma<
'd,
esp32h2_hal::peripherals::SPI2,
ChannelTx<'d, Channel0TxImpl, esp32h2_hal::gdma::Channel0>,
ChannelRx<'d, Channel0RxImpl, esp32h2_hal::gdma::Channel0>,
SuitablePeripheral0,
FullDuplexMode,
>;
#[embassy_executor::task]
async fn spi_task(spi: &'static mut SpiType<'static>) {
let send_buffer = [0, 1, 2, 3, 4, 5, 6, 7];
loop {
let mut buffer = [0; 8];
esp_println::println!("Sending bytes");
embedded_hal_async::spi::SpiBus::transfer(spi, &mut buffer, &send_buffer)
.await
.unwrap();
esp_println::println!("Bytes recieved: {:?}", buffer);
Timer::after(Duration::from_millis(5_000)).await;
}
}
static EXECUTOR: StaticCell<Executor> = StaticCell::new();
#[entry]
fn main() -> ! {
esp_println::println!("Init!");
let peripherals = Peripherals::take();
let mut system = peripherals.PCR.split();
let clocks = ClockControl::boot_defaults(system.clock_control).freeze();
// Disable the watchdog timers. For the ESP32-H2, this includes the Super WDT,
// and the TIMG WDTs.
let mut rtc = Rtc::new(peripherals.LP_CLKRST);
let timer_group0 = TimerGroup::new(
peripherals.TIMG0,
&clocks,
&mut system.peripheral_clock_control,
);
let mut wdt0 = timer_group0.wdt;
let timer_group1 = TimerGroup::new(
peripherals.TIMG1,
&clocks,
&mut system.peripheral_clock_control,
);
let mut wdt1 = timer_group1.wdt;
// Disable watchdog timers
rtc.swd.disable();
rtc.rwdt.disable();
wdt0.disable();
wdt1.disable();
#[cfg(feature = "embassy-time-systick")]
embassy::init(
&clocks,
esp32h2_hal::systimer::SystemTimer::new(peripherals.SYSTIMER),
);
#[cfg(feature = "embassy-time-timg0")]
embassy::init(&clocks, timer_group0.timer0);
esp32h2_hal::interrupt::enable(
esp32h2_hal::peripherals::Interrupt::DMA_IN_CH0,
esp32h2_hal::interrupt::Priority::Priority1,
)
.unwrap();
esp32h2_hal::interrupt::enable(
esp32h2_hal::peripherals::Interrupt::DMA_OUT_CH0,
esp32h2_hal::interrupt::Priority::Priority1,
)
.unwrap();
let io = IO::new(peripherals.GPIO, peripherals.IO_MUX);
let sclk = io.pins.gpio1;
let miso = io.pins.gpio2;
let mosi = io.pins.gpio3;
let cs = io.pins.gpio11;
let dma = Gdma::new(peripherals.DMA, &mut system.peripheral_clock_control);
let dma_channel = dma.channel0;
let descriptors = singleton!([0u32; 8 * 3]);
let rx_descriptors = singleton!([0u32; 8 * 3]);
let spi = singleton!(Spi::new(
peripherals.SPI2,
sclk,
mosi,
miso,
cs,
100u32.kHz(),
SpiMode::Mode0,
&mut system.peripheral_clock_control,
&clocks,
)
.with_dma(dma_channel.configure(
false,
descriptors,
rx_descriptors,
DmaPriority::Priority0,
)));
let executor = EXECUTOR.init(Executor::new());
executor.run(|spawner| {
spawner.spawn(spi_task(spi)).ok();
});
}