Merge pull request #319 from timokroeger/stm32f4-embassymain

Use `embassy::main` macro for stm32f4 examples
This commit is contained in:
Dario Nieuwenhuis 2021-07-28 10:34:46 +02:00 committed by GitHub
commit 126407f8c8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 60 additions and 134 deletions

View File

@ -8,32 +8,32 @@
#[path = "../example_common.rs"] #[path = "../example_common.rs"]
mod example_common; mod example_common;
use defmt::panic;
use embassy::executor::Spawner;
use embassy::time::{Duration, Timer};
use embassy_stm32::dbgmcu::Dbgmcu; use embassy_stm32::dbgmcu::Dbgmcu;
use embassy_stm32::gpio::{Level, Output, Speed}; use embassy_stm32::gpio::{Level, Output, Speed};
use embassy_stm32::Peripherals;
use embedded_hal::digital::v2::OutputPin; use embedded_hal::digital::v2::OutputPin;
use example_common::*; use example_common::*;
use cortex_m_rt::entry; #[embassy::main]
async fn main(_spawner: Spawner, p: Peripherals) {
#[entry]
fn main() -> ! {
info!("Hello World!"); info!("Hello World!");
unsafe { unsafe {
Dbgmcu::enable_all(); Dbgmcu::enable_all();
} }
let p = embassy_stm32::init(Default::default());
let mut led = Output::new(p.PB7, Level::High, Speed::Low); let mut led = Output::new(p.PB7, Level::High, Speed::Low);
loop { loop {
info!("high"); info!("high");
led.set_high().unwrap(); led.set_high().unwrap();
cortex_m::asm::delay(10_000_000); Timer::after(Duration::from_millis(300)).await;
info!("low"); info!("low");
led.set_low().unwrap(); led.set_low().unwrap();
cortex_m::asm::delay(10_000_000); Timer::after(Duration::from_millis(300)).await;
} }
} }

View File

@ -8,19 +8,22 @@
#[path = "../example_common.rs"] #[path = "../example_common.rs"]
mod example_common; mod example_common;
use embassy::executor::Executor; use defmt::panic;
use embassy::util::Forever; use embassy::executor::Spawner;
use embassy_stm32::dbgmcu::Dbgmcu; use embassy_stm32::dbgmcu::Dbgmcu;
use embassy_stm32::exti::ExtiInput; use embassy_stm32::exti::ExtiInput;
use embassy_stm32::gpio::{Input, Pull}; use embassy_stm32::gpio::{Input, Pull};
use embassy_stm32::Peripherals;
use embassy_traits::gpio::{WaitForFallingEdge, WaitForRisingEdge}; use embassy_traits::gpio::{WaitForFallingEdge, WaitForRisingEdge};
use example_common::*; use example_common::*;
use cortex_m_rt::entry; #[embassy::main]
async fn main(_spawner: Spawner, p: Peripherals) {
info!("Hello World!");
#[embassy::task] unsafe {
async fn main_task() { Dbgmcu::enable_all();
let p = embassy_stm32::init(Default::default()); }
let button = Input::new(p.PC13, Pull::Down); let button = Input::new(p.PC13, Pull::Down);
let mut button = ExtiInput::new(button, p.EXTI13); let mut button = ExtiInput::new(button, p.EXTI13);
@ -34,18 +37,3 @@ async fn main_task() {
info!("Released!"); info!("Released!");
} }
} }
static EXECUTOR: Forever<Executor> = Forever::new();
#[entry]
fn main() -> ! {
info!("Hello World!");
unsafe { Dbgmcu::enable_all() }
let executor = EXECUTOR.put(Executor::new());
executor.run(|spawner| {
unwrap!(spawner.spawn(main_task()));
})
}

View File

@ -9,16 +9,15 @@
#[path = "../example_common.rs"] #[path = "../example_common.rs"]
mod example_common; mod example_common;
use embassy_stm32::gpio::{Level, Output, Speed};
use embedded_hal::digital::v2::OutputPin;
use example_common::*;
use cortex_m_rt::entry; use cortex_m_rt::entry;
use embassy_stm32::dbgmcu::Dbgmcu; use embassy_stm32::dbgmcu::Dbgmcu;
use embassy_stm32::dma::NoDma;
use embassy_stm32::gpio::{Level, Output, Speed};
use embassy_stm32::spi::{Config, Spi}; use embassy_stm32::spi::{Config, Spi};
use embassy_stm32::time::Hertz; use embassy_stm32::time::Hertz;
use embedded_hal::blocking::spi::Transfer; use embedded_hal::blocking::spi::Transfer;
use embassy_stm32::dma::NoDma; use embedded_hal::digital::v2::OutputPin;
use example_common::*;
#[entry] #[entry]
fn main() -> ! { fn main() -> ! {

View File

@ -9,21 +9,24 @@
#[path = "../example_common.rs"] #[path = "../example_common.rs"]
mod example_common; mod example_common;
use core::fmt::Write; use core::fmt::Write;
use cortex_m_rt::entry;
use embassy::executor::Executor;
use embassy::time::Clock;
use embassy::util::Forever;
use example_common::*;
use embassy_traits::spi::FullDuplex;
use heapless::String;
use embassy_stm32::spi::{Spi, Config};
use embassy_stm32::pac;
use embassy_stm32::time::Hertz;
use core::str::from_utf8; use core::str::from_utf8;
use defmt::panic;
use embassy::executor::Spawner;
use embassy_stm32::dbgmcu::Dbgmcu;
use embassy_stm32::spi::{Config, Spi};
use embassy_stm32::time::Hertz;
use embassy_stm32::Peripherals;
use embassy_traits::spi::FullDuplex;
use example_common::*;
use heapless::String;
#[embassy::task] #[embassy::main]
async fn main_task() { async fn main(_spawner: Spawner, p: Peripherals) {
let p = embassy_stm32::init(Default::default()); info!("Hello World!");
unsafe {
Dbgmcu::enable_all();
}
let mut spi = Spi::new( let mut spi = Spi::new(
p.SPI1, p.SPI1,
@ -40,46 +43,9 @@ async fn main_task() {
let mut write: String<128> = String::new(); let mut write: String<128> = String::new();
let mut read = [0; 128]; let mut read = [0; 128];
core::write!(&mut write, "Hello DMA World {}!\r\n", n).unwrap(); core::write!(&mut write, "Hello DMA World {}!\r\n", n).unwrap();
spi.read_write(&mut read[0..write.len()], write.as_bytes()).await.ok(); spi.read_write(&mut read[0..write.len()], write.as_bytes())
.await
.ok();
info!("read via spi+dma: {}", from_utf8(&read).unwrap()); info!("read via spi+dma: {}", from_utf8(&read).unwrap());
} }
} }
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.ahb1enr().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()));
})
}

View File

@ -8,18 +8,21 @@
#[path = "../example_common.rs"] #[path = "../example_common.rs"]
mod example_common; mod example_common;
use cortex_m::prelude::_embedded_hal_blocking_serial_Write; use cortex_m_rt::entry;
use embassy::executor::Executor;
use embassy::util::Forever;
use embassy_stm32::dbgmcu::Dbgmcu; use embassy_stm32::dbgmcu::Dbgmcu;
use embassy_stm32::dma::NoDma; use embassy_stm32::dma::NoDma;
use embassy_stm32::usart::{Config, Uart}; use embassy_stm32::usart::{Config, Uart};
use embedded_hal::blocking::serial::Write;
use example_common::*; use example_common::*;
use cortex_m_rt::entry; #[entry]
fn main() -> ! {
info!("Hello World!");
unsafe {
Dbgmcu::enable_all();
}
#[embassy::task]
async fn main_task() {
let p = embassy_stm32::init(Default::default()); let p = embassy_stm32::init(Default::default());
let config = Config::default(); let config = Config::default();
@ -34,20 +37,3 @@ async fn main_task() {
usart.bwrite_all(&buf).unwrap(); usart.bwrite_all(&buf).unwrap();
} }
} }
static EXECUTOR: Forever<Executor> = Forever::new();
#[entry]
fn main() -> ! {
info!("Hello World!");
unsafe {
Dbgmcu::enable_all();
}
let executor = EXECUTOR.put(Executor::new());
executor.run(|spawner| {
unwrap!(spawner.spawn(main_task()));
})
}

View File

@ -9,19 +9,23 @@
#[path = "../example_common.rs"] #[path = "../example_common.rs"]
mod example_common; mod example_common;
use core::fmt::Write; use core::fmt::Write;
use cortex_m_rt::entry; use defmt::panic;
use embassy::executor::Executor; use embassy::executor::Spawner;
use embassy::util::Forever;
use embassy_stm32::dbgmcu::Dbgmcu; use embassy_stm32::dbgmcu::Dbgmcu;
use embassy_stm32::dma::NoDma; use embassy_stm32::dma::NoDma;
use embassy_stm32::usart::{Config, Uart}; use embassy_stm32::usart::{Config, Uart};
use embassy_stm32::Peripherals;
use embassy_traits::uart::Write as _; use embassy_traits::uart::Write as _;
use example_common::*; use example_common::*;
use heapless::String; use heapless::String;
#[embassy::task] #[embassy::main]
async fn main_task() { async fn main(_spawner: Spawner, p: Peripherals) {
let p = embassy_stm32::init(Default::default()); info!("Hello World!");
unsafe {
Dbgmcu::enable_all();
}
let config = Config::default(); let config = Config::default();
let mut usart = Uart::new(p.USART3, p.PD9, p.PD8, p.DMA1_CH3, NoDma, config); let mut usart = Uart::new(p.USART3, p.PD9, p.PD8, p.DMA1_CH3, NoDma, config);
@ -34,20 +38,3 @@ async fn main_task() {
info!("wrote DMA"); info!("wrote DMA");
} }
} }
static EXECUTOR: Forever<Executor> = Forever::new();
#[entry]
fn main() -> ! {
info!("Hello World!");
unsafe {
Dbgmcu::enable_all();
}
let executor = EXECUTOR.put(Executor::new());
executor.run(|spawner| {
unwrap!(spawner.spawn(main_task()));
})
}