From dd1ec8ebec3e02af803c8d06b5c18aeda5b8cff6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Timo=20Kr=C3=B6ger?= Date: Tue, 27 Jul 2021 15:03:18 +0200 Subject: [PATCH] Use `embassy::main` macro for stm32f4 examples --- examples/stm32f4/src/bin/blinky.rs | 16 +++--- examples/stm32f4/src/bin/button_exti.rs | 30 +++------- examples/stm32f4/src/bin/spi.rs | 9 ++- examples/stm32f4/src/bin/spi_dma.rs | 74 +++++++------------------ examples/stm32f4/src/bin/usart.rs | 32 +++-------- examples/stm32f4/src/bin/usart_dma.rs | 33 ++++------- 6 files changed, 60 insertions(+), 134 deletions(-) diff --git a/examples/stm32f4/src/bin/blinky.rs b/examples/stm32f4/src/bin/blinky.rs index 9e50c95c..9f86515e 100644 --- a/examples/stm32f4/src/bin/blinky.rs +++ b/examples/stm32f4/src/bin/blinky.rs @@ -8,32 +8,32 @@ #[path = "../example_common.rs"] mod example_common; +use defmt::panic; +use embassy::executor::Spawner; +use embassy::time::{Duration, Timer}; use embassy_stm32::dbgmcu::Dbgmcu; use embassy_stm32::gpio::{Level, Output, Speed}; +use embassy_stm32::Peripherals; use embedded_hal::digital::v2::OutputPin; use example_common::*; -use cortex_m_rt::entry; - -#[entry] -fn main() -> ! { +#[embassy::main] +async fn main(_spawner: Spawner, p: Peripherals) { info!("Hello World!"); unsafe { Dbgmcu::enable_all(); } - let p = embassy_stm32::init(Default::default()); - let mut led = Output::new(p.PB7, Level::High, Speed::Low); loop { info!("high"); led.set_high().unwrap(); - cortex_m::asm::delay(10_000_000); + Timer::after(Duration::from_millis(300)).await; info!("low"); led.set_low().unwrap(); - cortex_m::asm::delay(10_000_000); + Timer::after(Duration::from_millis(300)).await; } } diff --git a/examples/stm32f4/src/bin/button_exti.rs b/examples/stm32f4/src/bin/button_exti.rs index bc48c2b1..d685b660 100644 --- a/examples/stm32f4/src/bin/button_exti.rs +++ b/examples/stm32f4/src/bin/button_exti.rs @@ -8,19 +8,22 @@ #[path = "../example_common.rs"] mod example_common; -use embassy::executor::Executor; -use embassy::util::Forever; +use defmt::panic; +use embassy::executor::Spawner; use embassy_stm32::dbgmcu::Dbgmcu; use embassy_stm32::exti::ExtiInput; use embassy_stm32::gpio::{Input, Pull}; +use embassy_stm32::Peripherals; use embassy_traits::gpio::{WaitForFallingEdge, WaitForRisingEdge}; use example_common::*; -use cortex_m_rt::entry; +#[embassy::main] +async fn main(_spawner: Spawner, p: Peripherals) { + info!("Hello World!"); -#[embassy::task] -async fn main_task() { - let p = embassy_stm32::init(Default::default()); + unsafe { + Dbgmcu::enable_all(); + } let button = Input::new(p.PC13, Pull::Down); let mut button = ExtiInput::new(button, p.EXTI13); @@ -34,18 +37,3 @@ async fn main_task() { info!("Released!"); } } - -static EXECUTOR: Forever = 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())); - }) -} diff --git a/examples/stm32f4/src/bin/spi.rs b/examples/stm32f4/src/bin/spi.rs index 60428387..6e2b703f 100644 --- a/examples/stm32f4/src/bin/spi.rs +++ b/examples/stm32f4/src/bin/spi.rs @@ -9,16 +9,15 @@ #[path = "../example_common.rs"] 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 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::time::Hertz; use embedded_hal::blocking::spi::Transfer; -use embassy_stm32::dma::NoDma; +use embedded_hal::digital::v2::OutputPin; +use example_common::*; #[entry] fn main() -> ! { diff --git a/examples/stm32f4/src/bin/spi_dma.rs b/examples/stm32f4/src/bin/spi_dma.rs index 10a419fd..ccabbed9 100644 --- a/examples/stm32f4/src/bin/spi_dma.rs +++ b/examples/stm32f4/src/bin/spi_dma.rs @@ -9,21 +9,24 @@ #[path = "../example_common.rs"] mod example_common; 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 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] -async fn main_task() { - let p = embassy_stm32::init(Default::default()); +#[embassy::main] +async fn main(_spawner: Spawner, p: Peripherals) { + info!("Hello World!"); + + unsafe { + Dbgmcu::enable_all(); + } let mut spi = Spi::new( p.SPI1, @@ -38,48 +41,11 @@ async fn main_task() { for n in 0u32.. { 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(); - 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()); } } - -struct ZeroClock; - -impl Clock for ZeroClock { - fn now(&self) -> u64 { - 0 - } -} - -static EXECUTOR: Forever = 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())); - }) -} diff --git a/examples/stm32f4/src/bin/usart.rs b/examples/stm32f4/src/bin/usart.rs index 7cac33e9..8f0e04e5 100644 --- a/examples/stm32f4/src/bin/usart.rs +++ b/examples/stm32f4/src/bin/usart.rs @@ -8,18 +8,21 @@ #[path = "../example_common.rs"] mod example_common; -use cortex_m::prelude::_embedded_hal_blocking_serial_Write; -use embassy::executor::Executor; -use embassy::util::Forever; +use cortex_m_rt::entry; use embassy_stm32::dbgmcu::Dbgmcu; use embassy_stm32::dma::NoDma; use embassy_stm32::usart::{Config, Uart}; +use embedded_hal::blocking::serial::Write; 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 config = Config::default(); @@ -34,20 +37,3 @@ async fn main_task() { usart.bwrite_all(&buf).unwrap(); } } - -static EXECUTOR: Forever = 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())); - }) -} diff --git a/examples/stm32f4/src/bin/usart_dma.rs b/examples/stm32f4/src/bin/usart_dma.rs index 552f8367..3b86f78c 100644 --- a/examples/stm32f4/src/bin/usart_dma.rs +++ b/examples/stm32f4/src/bin/usart_dma.rs @@ -9,19 +9,23 @@ #[path = "../example_common.rs"] mod example_common; use core::fmt::Write; -use cortex_m_rt::entry; -use embassy::executor::Executor; -use embassy::util::Forever; +use defmt::panic; +use embassy::executor::Spawner; use embassy_stm32::dbgmcu::Dbgmcu; use embassy_stm32::dma::NoDma; use embassy_stm32::usart::{Config, Uart}; +use embassy_stm32::Peripherals; use embassy_traits::uart::Write as _; use example_common::*; use heapless::String; -#[embassy::task] -async fn main_task() { - let p = embassy_stm32::init(Default::default()); +#[embassy::main] +async fn main(_spawner: Spawner, p: Peripherals) { + info!("Hello World!"); + + unsafe { + Dbgmcu::enable_all(); + } let config = Config::default(); 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"); } } - -static EXECUTOR: Forever = 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())); - }) -}