embassy/examples/stm32l4/src/bin/spi_dma.rs

117 lines
2.7 KiB
Rust
Raw Normal View History

2021-07-21 16:42:22 +02:00
#![no_std]
#![no_main]
#![feature(trait_alias)]
#![feature(min_type_alias_impl_trait)]
#![feature(impl_trait_in_bindings)]
#![feature(type_alias_impl_trait)]
#![allow(incomplete_features)]
#[path = "../example_common.rs"]
mod example_common;
use cortex_m_rt::entry;
use embassy::executor::Executor;
use embassy::time::Clock;
use embassy::util::Forever;
use embassy_stm32::pac;
use example_common::*;
use embassy_stm32::spi::{Spi, Config};
use embassy_traits::spi::FullDuplex;
use embassy_stm32::time::Hertz;
use embassy_stm32::gpio::{Output, Level, Speed, Input, Pull};
use embedded_hal::digital::v2::{OutputPin, InputPin};
2021-07-21 16:42:22 +02:00
#[embassy::task]
async fn main_task() {
let p = embassy_stm32::init(Default::default());
let mut spi = Spi::new(
p.SPI3,
p.PC10,
p.PC12,
p.PC11,
p.DMA1_CH0,
p.DMA1_CH1,
Hertz(1_000_000),
Config::default(),
);
// These are the pins for the Inventek eS-Wifi SPI Wifi Adapter.
2021-07-21 17:28:02 +02:00
let _boot = Output::new(p.PB12, Level::Low, Speed::VeryHigh);
let _wake = Output::new(p.PB13, Level::Low, Speed::VeryHigh);
let mut reset = Output::new(p.PE8, Level::Low, Speed::VeryHigh);
2021-07-21 16:42:22 +02:00
let mut cs = Output::new(p.PE0, Level::High, Speed::VeryHigh);
let ready = Input::new(p.PE1, Pull::Up);
2021-07-21 16:42:22 +02:00
cortex_m::asm::delay(100_000);
reset.set_high();
cortex_m::asm::delay(100_000);
while ready.is_low().unwrap() {
info!("waiting for ready");
2021-07-21 16:42:22 +02:00
}
let write = [0x0A; 10];
let mut read = [0; 10];
unwrap!(cs.set_low());
spi.read_write(&mut read, &write).await.ok();
unwrap!(cs.set_high());
info!("xfer {=[u8]:x}", read);
2021-07-21 16:42:22 +02:00
}
struct ZeroClock;
impl Clock for ZeroClock {
fn now(&self) -> u64 {
0
}
}
static EXECUTOR: Forever<Executor> = Forever::new();
#[entry]
fn main() -> ! {
info!("Hello World!");
unsafe {
pac::DBGMCU.cr().modify(|w| {
w.set_dbg_sleep(true);
w.set_dbg_standby(true);
w.set_dbg_stop(true);
});
//pac::RCC.apbenr().modify(|w| {
//w.set_spi3en(true);
// });
pac::RCC.apb2enr().modify(|w| {
w.set_syscfgen(true);
});
pac::RCC.ahb1enr().modify(|w| {
w.set_dmamux1en(true);
w.set_dma1en(true);
w.set_dma2en(true);
});
pac::RCC.ahb2enr().modify(|w| {
w.set_gpioaen(true);
w.set_gpioben(true);
w.set_gpiocen(true);
w.set_gpioden(true);
w.set_gpioeen(true);
w.set_gpiofen(true);
});
}
unsafe { embassy::time::set_clock(&ZeroClock) };
let executor = EXECUTOR.put(Executor::new());
executor.run(|spawner| {
unwrap!(spawner.spawn(main_task()));
})
}