From ee841499ee8ee4d073930b2645eb70a0e652b383 Mon Sep 17 00:00:00 2001 From: Ben Gamari Date: Sat, 31 Jul 2021 10:01:32 -0400 Subject: Add STM32G0 examples --- examples/stm32g0/src/bin/blinky.rs | 31 +++++++++++++++++++++++++++++++ examples/stm32g0/src/bin/button.rs | 29 +++++++++++++++++++++++++++++ examples/stm32g0/src/bin/button_exti.rs | 31 +++++++++++++++++++++++++++++++ examples/stm32g0/src/example_common.rs | 17 +++++++++++++++++ 4 files changed, 108 insertions(+) create mode 100644 examples/stm32g0/src/bin/blinky.rs create mode 100644 examples/stm32g0/src/bin/button.rs create mode 100644 examples/stm32g0/src/bin/button_exti.rs create mode 100644 examples/stm32g0/src/example_common.rs (limited to 'examples/stm32g0/src') diff --git a/examples/stm32g0/src/bin/blinky.rs b/examples/stm32g0/src/bin/blinky.rs new file mode 100644 index 000000000..a30887f7d --- /dev/null +++ b/examples/stm32g0/src/bin/blinky.rs @@ -0,0 +1,31 @@ +#![no_std] +#![no_main] +#![feature(trait_alias)] +#![feature(type_alias_impl_trait)] +#![allow(incomplete_features)] + +#[path = "../example_common.rs"] +mod example_common; +use embassy::executor::Spawner; +use embassy::time::{Duration, Timer}; +use embassy_stm32::gpio::{Level, Output, Speed}; +use embassy_stm32::Peripherals; +use embedded_hal::digital::v2::OutputPin; +use example_common::*; + +#[embassy::main] +async fn main(_spawner: Spawner, p: Peripherals) { + info!("Hello World!"); + + let mut led = Output::new(p.PB7, Level::High, Speed::Low); + + loop { + info!("high"); + unwrap!(led.set_high()); + Timer::after(Duration::from_millis(300)).await; + + info!("low"); + unwrap!(led.set_low()); + Timer::after(Duration::from_millis(300)).await; + } +} diff --git a/examples/stm32g0/src/bin/button.rs b/examples/stm32g0/src/bin/button.rs new file mode 100644 index 000000000..a834b2fce --- /dev/null +++ b/examples/stm32g0/src/bin/button.rs @@ -0,0 +1,29 @@ +#![no_std] +#![no_main] +#![feature(trait_alias)] +#![feature(type_alias_impl_trait)] +#![allow(incomplete_features)] + +#[path = "../example_common.rs"] +mod example_common; +use cortex_m_rt::entry; +use embassy_stm32::gpio::{Input, Pull}; +use embedded_hal::digital::v2::InputPin; +use example_common::*; + +#[entry] +fn main() -> ! { + info!("Hello World!"); + + let p = embassy_stm32::init(Default::default()); + + let button = Input::new(p.PC13, Pull::Up); + + loop { + if unwrap!(button.is_high()) { + info!("high"); + } else { + info!("low"); + } + } +} diff --git a/examples/stm32g0/src/bin/button_exti.rs b/examples/stm32g0/src/bin/button_exti.rs new file mode 100644 index 000000000..c55d6408c --- /dev/null +++ b/examples/stm32g0/src/bin/button_exti.rs @@ -0,0 +1,31 @@ +#![no_std] +#![no_main] +#![feature(trait_alias)] +#![feature(type_alias_impl_trait)] +#![allow(incomplete_features)] + +#[path = "../example_common.rs"] +mod example_common; +use embassy::executor::Spawner; +use embassy_stm32::exti::ExtiInput; +use embassy_stm32::gpio::{Input, Pull}; +use embassy_stm32::Peripherals; +use embassy_traits::gpio::{WaitForFallingEdge, WaitForRisingEdge}; +use example_common::*; + +#[embassy::main] +async fn main(_spawner: Spawner, p: Peripherals) { + info!("Hello World!"); + + let button = Input::new(p.PC13, Pull::Up); + let mut button = ExtiInput::new(button, p.EXTI13); + + info!("Press the USER button..."); + + loop { + button.wait_for_falling_edge().await; + info!("Pressed!"); + button.wait_for_rising_edge().await; + info!("Released!"); + } +} diff --git a/examples/stm32g0/src/example_common.rs b/examples/stm32g0/src/example_common.rs new file mode 100644 index 000000000..54d633837 --- /dev/null +++ b/examples/stm32g0/src/example_common.rs @@ -0,0 +1,17 @@ +#![macro_use] + +use defmt_rtt as _; // global logger +use panic_probe as _; + +pub use defmt::*; + +use core::sync::atomic::{AtomicUsize, Ordering}; + +defmt::timestamp! {"{=u64}", { + static COUNT: AtomicUsize = AtomicUsize::new(0); + // NOTE(no-CAS) `timestamps` runs with interrupts disabled + let n = COUNT.load(Ordering::Relaxed); + COUNT.store(n + 1, Ordering::Relaxed); + n as u64 + } +} -- cgit