* Unify the system peripheral Whilst the PCR, SYSTEM and DPORT peripherals are different, we currently use them all in the same way. This PR unifies the peripheral name in the hal to `SYSTEM`. The idea is that they all do the same sort of thing, so we can collect them under the same name, and later down the line we can being to expose differences under an extended API. The benifits to this are imo quite big, the examples now are all identical, which makes things easier for esp-wifi, and paves a path towards the multichip hal. Why not do this in the PAC? Imo the pac should be as close to the hardware as possible, and the HAL is where we should abstractions such as this. * changelog
102 lines
2.6 KiB
Rust
102 lines
2.6 KiB
Rust
//! Embassy SPI
|
|
//!
|
|
//! Folowing pins are used:
|
|
//! SCLK GPIO19
|
|
//! MISO GPIO25
|
|
//! MOSI GPIO23
|
|
//! CS GPIO22
|
|
//!
|
|
//! 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_time::{Duration, Timer};
|
|
use esp32_hal::{
|
|
clock::ClockControl,
|
|
dma::DmaPriority,
|
|
embassy::{self, executor::Executor},
|
|
pdma::*,
|
|
peripherals::Peripherals,
|
|
prelude::*,
|
|
spi::{dma::SpiDma, FullDuplexMode, Spi, SpiMode},
|
|
timer::TimerGroup,
|
|
IO,
|
|
};
|
|
use esp_backtrace as _;
|
|
use static_cell::make_static;
|
|
|
|
pub type SpiType<'d> = SpiDma<'d, esp32_hal::peripherals::SPI2, Spi2DmaChannel, 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;
|
|
}
|
|
}
|
|
|
|
#[entry]
|
|
fn main() -> ! {
|
|
esp_println::println!("Init!");
|
|
let peripherals = Peripherals::take();
|
|
let system = peripherals.SYSTEM.split();
|
|
let clocks = ClockControl::boot_defaults(system.clock_control).freeze();
|
|
|
|
let timer_group0 = TimerGroup::new(peripherals.TIMG0, &clocks);
|
|
embassy::init(&clocks, timer_group0.timer0);
|
|
|
|
esp32_hal::interrupt::enable(
|
|
esp32_hal::peripherals::Interrupt::SPI2_DMA,
|
|
esp32_hal::interrupt::Priority::Priority1,
|
|
)
|
|
.unwrap();
|
|
|
|
let io = IO::new(peripherals.GPIO, peripherals.IO_MUX);
|
|
let sclk = io.pins.gpio19;
|
|
let miso = io.pins.gpio25;
|
|
let mosi = io.pins.gpio23;
|
|
let cs = io.pins.gpio22;
|
|
|
|
let dma = Dma::new(system.dma);
|
|
let dma_channel = dma.spi2channel;
|
|
|
|
let descriptors = make_static!([0u32; 8 * 3]);
|
|
let rx_descriptors = make_static!([0u32; 8 * 3]);
|
|
|
|
let spi = make_static!(Spi::new(
|
|
peripherals.SPI2,
|
|
sclk,
|
|
mosi,
|
|
miso,
|
|
cs,
|
|
100u32.kHz(),
|
|
SpiMode::Mode0,
|
|
&clocks,
|
|
)
|
|
.with_dma(dma_channel.configure(
|
|
false,
|
|
descriptors,
|
|
rx_descriptors,
|
|
DmaPriority::Priority0,
|
|
)));
|
|
|
|
let executor = make_static!(Executor::new());
|
|
executor.run(|spawner| {
|
|
spawner.spawn(spi_task(spi)).ok();
|
|
});
|
|
}
|