From 5b0ae5c25b527069da176afaf1ab45a06b156df4 Mon Sep 17 00:00:00 2001 From: Dario Nieuwenhuis Date: Sat, 24 Jul 2021 13:57:11 +0200 Subject: [PATCH] stm32/examples: cleanup L4 --- examples/stm32l4/Cargo.toml | 1 - examples/stm32l4/src/bin/adc.rs | 40 ++++--------- examples/stm32l4/src/bin/blinky.rs | 19 +++--- examples/stm32l4/src/bin/button.rs | 21 +++---- examples/stm32l4/src/bin/button_exti.rs | 53 +++++------------ examples/stm32l4/src/bin/dac.rs | 32 +++------- examples/stm32l4/src/bin/spi.rs | 13 ++-- examples/stm32l4/src/bin/spi_dma.rs | 79 ++++++------------------- examples/stm32l4/src/bin/usart.rs | 61 +++++-------------- examples/stm32l4/src/bin/usart_dma.rs | 60 +++++-------------- 10 files changed, 100 insertions(+), 279 deletions(-) diff --git a/examples/stm32l4/Cargo.toml b/examples/stm32l4/Cargo.toml index 41c189c6..cbf00229 100644 --- a/examples/stm32l4/Cargo.toml +++ b/examples/stm32l4/Cargo.toml @@ -21,7 +21,6 @@ embassy = { version = "0.1.0", path = "../../embassy", features = ["defmt", "def embassy-traits = { version = "0.1.0", path = "../../embassy-traits", features = ["defmt"] } embassy-stm32 = { version = "0.1.0", path = "../../embassy-stm32", features = ["defmt", "defmt-trace", "unstable-pac", "stm32l4s5vi"] } embassy-extras = {version = "0.1.0", path = "../../embassy-extras" } -stm32l4xx-hal = { version = "0.6.0", features = ["stm32l4x5"] } defmt = "0.2.0" defmt-rtt = "0.2.0" diff --git a/examples/stm32l4/src/bin/adc.rs b/examples/stm32l4/src/bin/adc.rs index 6894d664..f2a8bfb7 100644 --- a/examples/stm32l4/src/bin/adc.rs +++ b/examples/stm32l4/src/bin/adc.rs @@ -9,35 +9,16 @@ #[path = "../example_common.rs"] mod example_common; +use defmt::panic; +use embassy::executor::Spawner; +use embassy::time::Delay; +use embassy_stm32::adc::{Adc, Resolution}; +use embassy_stm32::{pac, Peripherals}; use example_common::*; -use cortex_m::delay::Delay; -use cortex_m_rt::entry; -use embassy_stm32::adc::{Adc, Resolution}; -use embassy_stm32::pac; -use stm32l4xx_hal::prelude::*; -use stm32l4xx_hal::rcc::PllSource; - -#[entry] -fn main() -> ! { - info!("Hello World, dude!"); - //let pp = pac::Peripherals::take().unwrap(); - let cp = cortex_m::Peripherals::take().unwrap(); - let pp = stm32l4xx_hal::stm32::Peripherals::take().unwrap(); - let mut flash = pp.FLASH.constrain(); - let mut rcc = pp.RCC.constrain(); - let mut pwr = pp.PWR.constrain(&mut rcc.apb1r1); - - let mut delay = Delay::new(cp.SYST, 80_000_000); - - // TRY the other clock configuration - // let clocks = rcc.cfgr.freeze(&mut flash.acr); - rcc.cfgr - .sysclk(80.mhz()) - .pclk1(80.mhz()) - .pclk2(80.mhz()) - .pll_source(PllSource::HSI16) - .freeze(&mut flash.acr, &mut pwr); +#[embassy::main] +async fn main(_spawner: Spawner, p: Peripherals) { + info!("Hello World!"); unsafe { pac::RCC.ccipr().modify(|w| { @@ -48,11 +29,10 @@ fn main() -> ! { w.set_dbg_standby(true); w.set_dbg_stop(true); }); + pac::RCC.ahb2enr().modify(|w| w.set_adcen(true)); } - let p = embassy_stm32::init(Default::default()); - - let mut adc = Adc::new(p.ADC1, &mut delay); + let mut adc = Adc::new(p.ADC1, &mut Delay); //adc.enable_vref(); adc.set_resolution(Resolution::EightBit); let mut channel = p.PC0; diff --git a/examples/stm32l4/src/bin/blinky.rs b/examples/stm32l4/src/bin/blinky.rs index ba271b9e..1064bcd5 100644 --- a/examples/stm32l4/src/bin/blinky.rs +++ b/examples/stm32l4/src/bin/blinky.rs @@ -8,14 +8,16 @@ #[path = "../example_common.rs"] mod example_common; -use cortex_m_rt::entry; +use defmt::panic; +use embassy::executor::Spawner; +use embassy::time::{Duration, Timer}; use embassy_stm32::gpio::{Level, Output, Speed}; -use embassy_stm32::pac; +use embassy_stm32::{pac, Peripherals}; use embedded_hal::digital::v2::OutputPin; use example_common::*; -#[entry] -fn main() -> ! { +#[embassy::main] +async fn main(_spawner: Spawner, p: Peripherals) { info!("Hello World!"); unsafe { @@ -26,17 +28,12 @@ fn main() -> ! { }); } - let p = embassy_stm32::init(Default::default()); - let mut led = Output::new(p.PB14, Level::High, Speed::Low); loop { - info!("high"); led.set_high().unwrap(); - cortex_m::asm::delay(10_000_000); - - info!("low"); + Timer::after(Duration::from_millis(300)).await; led.set_low().unwrap(); - cortex_m::asm::delay(10_000_000); + Timer::after(Duration::from_millis(300)).await; } } diff --git a/examples/stm32l4/src/bin/button.rs b/examples/stm32l4/src/bin/button.rs index eff9189e..08c20ce4 100644 --- a/examples/stm32l4/src/bin/button.rs +++ b/examples/stm32l4/src/bin/button.rs @@ -8,14 +8,15 @@ #[path = "../example_common.rs"] mod example_common; -use cortex_m_rt::entry; -use embassy_stm32::gpio::{Input, Level, Output, Pull, Speed}; -use embassy_stm32::pac; -use embedded_hal::digital::v2::{InputPin, OutputPin}; +use defmt::panic; +use embassy::executor::Spawner; +use embassy_stm32::gpio::{Input, Pull}; +use embassy_stm32::{pac, Peripherals}; +use embedded_hal::digital::v2::InputPin; use example_common::*; -#[entry] -fn main() -> ! { +#[embassy::main] +async fn main(_spawner: Spawner, p: Peripherals) { info!("Hello World!"); unsafe { @@ -26,21 +27,13 @@ fn main() -> ! { }); } - let p = embassy_stm32::init(Default::default()); - let button = Input::new(p.PC13, Pull::Up); - let mut led1 = Output::new(p.PA5, Level::High, Speed::Low); - let mut led2 = Output::new(p.PB14, Level::High, Speed::Low); loop { if button.is_high().unwrap() { info!("high"); - led1.set_high().unwrap(); - led2.set_low().unwrap(); } else { info!("low"); - led1.set_low().unwrap(); - led2.set_high().unwrap(); } } } diff --git a/examples/stm32l4/src/bin/button_exti.rs b/examples/stm32l4/src/bin/button_exti.rs index a7e08afd..5ab7dd9a 100644 --- a/examples/stm32l4/src/bin/button_exti.rs +++ b/examples/stm32l4/src/bin/button_exti.rs @@ -8,19 +8,25 @@ #[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 defmt::panic; +use embassy::executor::Spawner; use embassy_stm32::exti::ExtiInput; use embassy_stm32::gpio::{Input, Pull}; -use embassy_stm32::pac; +use embassy_stm32::{pac, Peripherals}; use embassy_traits::gpio::{WaitForFallingEdge, WaitForRisingEdge}; use example_common::*; -#[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 { + pac::DBGMCU.cr().modify(|w| { + w.set_dbg_sleep(true); + w.set_dbg_standby(true); + w.set_dbg_stop(true); + }); + } let button = Input::new(p.PC13, Pull::Up); let mut button = ExtiInput::new(button, p.EXTI13); @@ -34,34 +40,3 @@ async fn main_task() { info!("Released!"); } } - -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); - }); - } - - unsafe { embassy::time::set_clock(&ZeroClock) }; - - let executor = EXECUTOR.put(Executor::new()); - - executor.run(|spawner| { - unwrap!(spawner.spawn(main_task())); - }) -} diff --git a/examples/stm32l4/src/bin/dac.rs b/examples/stm32l4/src/bin/dac.rs index 1f7f8d46..46f59ec1 100644 --- a/examples/stm32l4/src/bin/dac.rs +++ b/examples/stm32l4/src/bin/dac.rs @@ -9,32 +9,16 @@ #[path = "../example_common.rs"] mod example_common; +use defmt::panic; +use embassy::executor::Spawner; +use embassy_stm32::dac::{Channel, Dac, Value}; use embassy_stm32::gpio::NoPin; +use embassy_stm32::{pac, Peripherals}; use example_common::*; -use cortex_m_rt::entry; -use embassy_stm32::dac::{Channel, Dac, Value}; -use embassy_stm32::pac; -use stm32l4xx_hal::prelude::*; -use stm32l4xx_hal::rcc::PllSource; - -#[entry] -fn main() -> ! { - info!("Hello World, dude!"); - //let pp = pac::Peripherals::take().unwrap(); - let pp = stm32l4xx_hal::stm32::Peripherals::take().unwrap(); - let mut flash = pp.FLASH.constrain(); - let mut rcc = pp.RCC.constrain(); - let mut pwr = pp.PWR.constrain(&mut rcc.apb1r1); - - // TRY the other clock configuration - // let clocks = rcc.cfgr.freeze(&mut flash.acr); - rcc.cfgr - .sysclk(80.mhz()) - .pclk1(80.mhz()) - .pclk2(80.mhz()) - .pll_source(PllSource::HSI16) - .freeze(&mut flash.acr, &mut pwr); +#[embassy::main] +async fn main(_spawner: Spawner, p: Peripherals) { + info!("Hello World!"); unsafe { pac::DBGMCU.cr().modify(|w| { @@ -47,8 +31,6 @@ fn main() -> ! { }); } - let p = embassy_stm32::init(Default::default()); - let mut dac = Dac::new(p.DAC1, p.PA4, NoPin); loop { diff --git a/examples/stm32l4/src/bin/spi.rs b/examples/stm32l4/src/bin/spi.rs index 1830fcb5..14bfae51 100644 --- a/examples/stm32l4/src/bin/spi.rs +++ b/examples/stm32l4/src/bin/spi.rs @@ -9,19 +9,20 @@ #[path = "../example_common.rs"] mod example_common; -use cortex_m_rt::entry; +use defmt::panic; +use embassy::executor::Spawner; use embassy_stm32::dma::NoDma; use embassy_stm32::gpio::{Level, Output, Speed}; -use embassy_stm32::pac; use embassy_stm32::spi::{Config, Spi}; use embassy_stm32::time::Hertz; +use embassy_stm32::{pac, Peripherals}; use embedded_hal::blocking::spi::Transfer; use embedded_hal::digital::v2::OutputPin; use example_common::*; -#[entry] -fn main() -> ! { - info!("Hello World, dude!"); +#[embassy::main] +async fn main(_spawner: Spawner, p: Peripherals) { + info!("Hello World!"); unsafe { pac::DBGMCU.cr().modify(|w| { @@ -31,8 +32,6 @@ fn main() -> ! { }); } - let p = embassy_stm32::init(Default::default()); - let mut spi = Spi::new( p.SPI3, p.PC10, diff --git a/examples/stm32l4/src/bin/spi_dma.rs b/examples/stm32l4/src/bin/spi_dma.rs index 0b66f574..9672e245 100644 --- a/examples/stm32l4/src/bin/spi_dma.rs +++ b/examples/stm32l4/src/bin/spi_dma.rs @@ -9,21 +9,27 @@ #[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 defmt::panic; +use embassy::executor::Spawner; +use embassy_stm32::gpio::{Input, Level, Output, Pull, Speed}; +use embassy_stm32::spi::{Config, Spi}; use embassy_stm32::time::Hertz; -use embassy_stm32::gpio::{Output, Level, Speed, Input, Pull}; -use embedded_hal::digital::v2::{OutputPin, InputPin}; +use embassy_stm32::{pac, Peripherals}; +use embassy_traits::spi::FullDuplex; +use embedded_hal::digital::v2::{InputPin, OutputPin}; +use example_common::*; -#[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 { + pac::DBGMCU.cr().modify(|w| { + w.set_dbg_sleep(true); + w.set_dbg_standby(true); + w.set_dbg_stop(true); + }); + } let mut spi = Spi::new( p.SPI3, @@ -36,7 +42,6 @@ async fn main_task() { Config::default(), ); - // These are the pins for the Inventek eS-Wifi SPI Wifi Adapter. let _boot = Output::new(p.PB12, Level::Low, Speed::VeryHigh); @@ -60,49 +65,3 @@ async fn main_task() { unwrap!(cs.set_high()); info!("xfer {=[u8]:x}", read); } - -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_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())); - }) -} diff --git a/examples/stm32l4/src/bin/usart.rs b/examples/stm32l4/src/bin/usart.rs index 8b1180a5..b93b5ab5 100644 --- a/examples/stm32l4/src/bin/usart.rs +++ b/examples/stm32l4/src/bin/usart.rs @@ -9,18 +9,24 @@ #[path = "../example_common.rs"] mod example_common; use cortex_m::prelude::_embedded_hal_blocking_serial_Write; -use cortex_m_rt::entry; -use embassy::executor::Executor; -use embassy::time::Clock; -use embassy::util::Forever; +use defmt::panic; +use embassy::executor::Spawner; use embassy_stm32::dma::NoDma; -use embassy_stm32::pac; use embassy_stm32::usart::{Config, Uart}; +use embassy_stm32::{pac, Peripherals}; use example_common::*; -#[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 { + pac::DBGMCU.cr().modify(|w| { + w.set_dbg_sleep(true); + w.set_dbg_standby(true); + w.set_dbg_stop(true); + }); + } let config = Config::default(); let mut usart = Uart::new(p.UART4, p.PA1, p.PA0, NoDma, NoDma, config); @@ -34,42 +40,3 @@ async fn main_task() { usart.bwrite_all(&buf).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_dma1en(true); - }); - - pac::RCC.apb1enr1().modify(|w| { - w.set_uart4en(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/stm32l4/src/bin/usart_dma.rs b/examples/stm32l4/src/bin/usart_dma.rs index 6262b6c8..cb01a2b5 100644 --- a/examples/stm32l4/src/bin/usart_dma.rs +++ b/examples/stm32l4/src/bin/usart_dma.rs @@ -9,46 +9,17 @@ #[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 defmt::panic; +use embassy::executor::Spawner; use embassy_stm32::dma::NoDma; -use embassy_stm32::pac; use embassy_stm32::usart::{Config, Uart}; +use embassy_stm32::{pac, 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()); - - let config = Config::default(); - let mut usart = Uart::new(p.UART4, p.PA1, p.PA0, p.DMA1_CH3, NoDma, config); - - for n in 0u32.. { - let mut s: String<128> = String::new(); - core::write!(&mut s, "Hello DMA World {}!\r\n", n).unwrap(); - - usart.write(s.as_bytes()).await.ok(); - - info!("wrote DMA"); - } -} - -struct ZeroClock; - -impl Clock for ZeroClock { - fn now(&self) -> u64 { - 0 - } -} - -static EXECUTOR: Forever = Forever::new(); - -#[entry] -fn main() -> ! { +#[embassy::main] +async fn main(_spawner: Spawner, p: Peripherals) { info!("Hello World!"); unsafe { @@ -57,19 +28,18 @@ fn main() -> ! { w.set_dbg_standby(true); w.set_dbg_stop(true); }); - - pac::RCC.ahb1enr().modify(|w| { - w.set_dmamux1en(true); - w.set_dma1en(true); - w.set_dma2en(true); - }); } - unsafe { embassy::time::set_clock(&ZeroClock) }; + let config = Config::default(); + let mut usart = Uart::new(p.UART4, p.PA1, p.PA0, p.DMA1_CH3, NoDma, config); - let executor = EXECUTOR.put(Executor::new()); + for n in 0u32.. { + let mut s: String<128> = String::new(); + core::write!(&mut s, "Hello DMA World {}!\r\n", n).unwrap(); - executor.run(|spawner| { - unwrap!(spawner.spawn(main_task())); - }) + info!("Writing..."); + usart.write(s.as_bytes()).await.ok(); + + info!("wrote DMA"); + } }