diff options
| author | Dario Nieuwenhuis <[email protected]> | 2021-06-08 16:46:12 +0200 |
|---|---|---|
| committer | GitHub <[email protected]> | 2021-06-08 16:46:12 +0200 |
| commit | af0f8082f079bb80d8b478331ff505f5462a9120 (patch) | |
| tree | 6de9b663d1d5be935a950b9436670ea75c935687 /examples/stm32l4/src | |
| parent | e6bd02d40eb2d2bdde1da463a6e79eebb63987b8 (diff) | |
| parent | a407558e3f0df9dd9e8f4899608a7ab5187a0af8 (diff) | |
Merge pull request #233 from bobmcwhirter/l4-examples
L4 examples
Diffstat (limited to 'examples/stm32l4/src')
| -rw-r--r-- | examples/stm32l4/src/bin/blinky.rs | 53 | ||||
| -rw-r--r-- | examples/stm32l4/src/bin/button.rs | 58 | ||||
| -rw-r--r-- | examples/stm32l4/src/bin/button_exti.rs | 84 | ||||
| -rw-r--r-- | examples/stm32l4/src/bin/spi.rs | 65 | ||||
| -rw-r--r-- | examples/stm32l4/src/example_common.rs | 17 |
5 files changed, 277 insertions, 0 deletions
diff --git a/examples/stm32l4/src/bin/blinky.rs b/examples/stm32l4/src/bin/blinky.rs new file mode 100644 index 000000000..42c9333f4 --- /dev/null +++ b/examples/stm32l4/src/bin/blinky.rs | |||
| @@ -0,0 +1,53 @@ | |||
| 1 | #![no_std] | ||
| 2 | #![no_main] | ||
| 3 | #![feature(trait_alias)] | ||
| 4 | #![feature(min_type_alias_impl_trait)] | ||
| 5 | #![feature(impl_trait_in_bindings)] | ||
| 6 | #![feature(type_alias_impl_trait)] | ||
| 7 | #![allow(incomplete_features)] | ||
| 8 | |||
| 9 | #[path = "../example_common.rs"] | ||
| 10 | mod example_common; | ||
| 11 | use embassy_stm32::gpio::{Level, Output}; | ||
| 12 | use embedded_hal::digital::v2::OutputPin; | ||
| 13 | use example_common::*; | ||
| 14 | |||
| 15 | use cortex_m_rt::entry; | ||
| 16 | use stm32l4::stm32l4x5 as pac; | ||
| 17 | |||
| 18 | #[entry] | ||
| 19 | fn main() -> ! { | ||
| 20 | info!("Hello World!"); | ||
| 21 | |||
| 22 | let pp = pac::Peripherals::take().unwrap(); | ||
| 23 | |||
| 24 | pp.DBGMCU.cr.modify(|_, w| { | ||
| 25 | w.dbg_sleep().set_bit(); | ||
| 26 | w.dbg_standby().set_bit(); | ||
| 27 | w.dbg_stop().set_bit() | ||
| 28 | }); | ||
| 29 | |||
| 30 | pp.RCC.ahb2enr.modify(|_, w| { | ||
| 31 | w.gpioaen().set_bit(); | ||
| 32 | w.gpioben().set_bit(); | ||
| 33 | w.gpiocen().set_bit(); | ||
| 34 | w.gpioden().set_bit(); | ||
| 35 | w.gpioeen().set_bit(); | ||
| 36 | w.gpiofen().set_bit(); | ||
| 37 | w | ||
| 38 | }); | ||
| 39 | |||
| 40 | let p = embassy_stm32::init(Default::default()); | ||
| 41 | |||
| 42 | let mut led = Output::new(p.PB14, Level::High); | ||
| 43 | |||
| 44 | loop { | ||
| 45 | info!("high"); | ||
| 46 | led.set_high().unwrap(); | ||
| 47 | cortex_m::asm::delay(10_000_000); | ||
| 48 | |||
| 49 | info!("low"); | ||
| 50 | led.set_low().unwrap(); | ||
| 51 | cortex_m::asm::delay(10_000_000); | ||
| 52 | } | ||
| 53 | } | ||
diff --git a/examples/stm32l4/src/bin/button.rs b/examples/stm32l4/src/bin/button.rs new file mode 100644 index 000000000..43d81715a --- /dev/null +++ b/examples/stm32l4/src/bin/button.rs | |||
| @@ -0,0 +1,58 @@ | |||
| 1 | #![no_std] | ||
| 2 | #![no_main] | ||
| 3 | #![feature(trait_alias)] | ||
| 4 | #![feature(min_type_alias_impl_trait)] | ||
| 5 | #![feature(impl_trait_in_bindings)] | ||
| 6 | #![feature(type_alias_impl_trait)] | ||
| 7 | #![allow(incomplete_features)] | ||
| 8 | |||
| 9 | #[path = "../example_common.rs"] | ||
| 10 | mod example_common; | ||
| 11 | use embassy_stm32::gpio::{Input, Level, Output, Pull}; | ||
| 12 | use embedded_hal::digital::v2::{InputPin, OutputPin}; | ||
| 13 | use example_common::*; | ||
| 14 | |||
| 15 | use cortex_m_rt::entry; | ||
| 16 | use stm32l4::stm32l4x5 as pac; | ||
| 17 | |||
| 18 | |||
| 19 | #[entry] | ||
| 20 | fn main() -> ! { | ||
| 21 | info!("Hello World!"); | ||
| 22 | |||
| 23 | let pp = pac::Peripherals::take().unwrap(); | ||
| 24 | |||
| 25 | pp.DBGMCU.cr.modify(|_, w| { | ||
| 26 | w.dbg_sleep().set_bit(); | ||
| 27 | w.dbg_standby().set_bit(); | ||
| 28 | w.dbg_stop().set_bit() | ||
| 29 | }); | ||
| 30 | |||
| 31 | pp.RCC.ahb2enr.modify(|_, w| { | ||
| 32 | w.gpioaen().set_bit(); | ||
| 33 | w.gpioben().set_bit(); | ||
| 34 | w.gpiocen().set_bit(); | ||
| 35 | w.gpioden().set_bit(); | ||
| 36 | w.gpioeen().set_bit(); | ||
| 37 | w.gpiofen().set_bit(); | ||
| 38 | w | ||
| 39 | }); | ||
| 40 | |||
| 41 | let p = embassy_stm32::init(Default::default()); | ||
| 42 | |||
| 43 | let button = Input::new(p.PC13, Pull::Up); | ||
| 44 | let mut led1 = Output::new(p.PA5, Level::High); | ||
| 45 | let mut led2 = Output::new(p.PB14, Level::High); | ||
| 46 | |||
| 47 | loop { | ||
| 48 | if button.is_high().unwrap() { | ||
| 49 | info!("high"); | ||
| 50 | led1.set_high().unwrap(); | ||
| 51 | led2.set_low().unwrap(); | ||
| 52 | } else { | ||
| 53 | info!("low"); | ||
| 54 | led1.set_low().unwrap(); | ||
| 55 | led2.set_high().unwrap(); | ||
| 56 | } | ||
| 57 | } | ||
| 58 | } | ||
diff --git a/examples/stm32l4/src/bin/button_exti.rs b/examples/stm32l4/src/bin/button_exti.rs new file mode 100644 index 000000000..caace8359 --- /dev/null +++ b/examples/stm32l4/src/bin/button_exti.rs | |||
| @@ -0,0 +1,84 @@ | |||
| 1 | #![no_std] | ||
| 2 | #![no_main] | ||
| 3 | #![feature(trait_alias)] | ||
| 4 | #![feature(min_type_alias_impl_trait)] | ||
| 5 | #![feature(impl_trait_in_bindings)] | ||
| 6 | #![feature(type_alias_impl_trait)] | ||
| 7 | #![allow(incomplete_features)] | ||
| 8 | |||
| 9 | #[path = "../example_common.rs"] | ||
| 10 | mod example_common; | ||
| 11 | use embassy::executor::Executor; | ||
| 12 | use embassy::time::Clock; | ||
| 13 | use embassy::util::Forever; | ||
| 14 | use embassy_stm32::exti::ExtiInput; | ||
| 15 | use embassy_stm32::gpio::{Input, Pull}; | ||
| 16 | use embassy_traits::gpio::{WaitForFallingEdge, WaitForRisingEdge}; | ||
| 17 | use example_common::*; | ||
| 18 | |||
| 19 | use cortex_m_rt::entry; | ||
| 20 | use stm32l4::stm32l4x5 as pac; | ||
| 21 | |||
| 22 | #[embassy::task] | ||
| 23 | async fn main_task() { | ||
| 24 | let p = embassy_stm32::init(Default::default()); | ||
| 25 | |||
| 26 | let button = Input::new(p.PC13, Pull::Up); | ||
| 27 | let mut button = ExtiInput::new(button, p.EXTI13); | ||
| 28 | |||
| 29 | info!("Press the USER button..."); | ||
| 30 | |||
| 31 | loop { | ||
| 32 | button.wait_for_falling_edge().await; | ||
| 33 | info!("Pressed!"); | ||
| 34 | button.wait_for_rising_edge().await; | ||
| 35 | info!("Released!"); | ||
| 36 | } | ||
| 37 | } | ||
| 38 | |||
| 39 | struct ZeroClock; | ||
| 40 | |||
| 41 | impl Clock for ZeroClock { | ||
| 42 | fn now(&self) -> u64 { | ||
| 43 | 0 | ||
| 44 | } | ||
| 45 | } | ||
| 46 | |||
| 47 | static EXECUTOR: Forever<Executor> = Forever::new(); | ||
| 48 | |||
| 49 | #[entry] | ||
| 50 | fn main() -> ! { | ||
| 51 | info!("Hello World!"); | ||
| 52 | |||
| 53 | let pp = pac::Peripherals::take().unwrap(); | ||
| 54 | |||
| 55 | pp.DBGMCU.cr.modify(|_, w| { | ||
| 56 | w.dbg_sleep().set_bit(); | ||
| 57 | w.dbg_standby().set_bit(); | ||
| 58 | w.dbg_stop().set_bit() | ||
| 59 | }); | ||
| 60 | |||
| 61 | pp.RCC.ahb2enr.modify(|_, w| { | ||
| 62 | w.gpioaen().set_bit(); | ||
| 63 | w.gpioben().set_bit(); | ||
| 64 | w.gpiocen().set_bit(); | ||
| 65 | w.gpioden().set_bit(); | ||
| 66 | w.gpioeen().set_bit(); | ||
| 67 | w.gpiofen().set_bit(); | ||
| 68 | w | ||
| 69 | }); | ||
| 70 | |||
| 71 | pp.RCC.apb2enr.modify(|_, w| { | ||
| 72 | w.syscfgen().set_bit(); | ||
| 73 | w | ||
| 74 | }); | ||
| 75 | |||
| 76 | unsafe { embassy::time::set_clock(&ZeroClock) }; | ||
| 77 | |||
| 78 | let executor = EXECUTOR.put(Executor::new()); | ||
| 79 | |||
| 80 | executor.run(|spawner| { | ||
| 81 | unwrap!(spawner.spawn(main_task())); | ||
| 82 | }) | ||
| 83 | |||
| 84 | } | ||
diff --git a/examples/stm32l4/src/bin/spi.rs b/examples/stm32l4/src/bin/spi.rs new file mode 100644 index 000000000..9db854dc3 --- /dev/null +++ b/examples/stm32l4/src/bin/spi.rs | |||
| @@ -0,0 +1,65 @@ | |||
| 1 | #![no_std] | ||
| 2 | #![no_main] | ||
| 3 | #![feature(trait_alias)] | ||
| 4 | #![feature(min_type_alias_impl_trait)] | ||
| 5 | #![feature(impl_trait_in_bindings)] | ||
| 6 | #![feature(type_alias_impl_trait)] | ||
| 7 | #![allow(incomplete_features)] | ||
| 8 | |||
| 9 | #[path = "../example_common.rs"] | ||
| 10 | mod example_common; | ||
| 11 | |||
| 12 | use embassy_stm32::gpio::{Level, Output}; | ||
| 13 | use embedded_hal::digital::v2::OutputPin; | ||
| 14 | use example_common::*; | ||
| 15 | |||
| 16 | use cortex_m_rt::entry; | ||
| 17 | use embassy_stm32::spi::{Config, Spi}; | ||
| 18 | use embassy_stm32::time::Hertz; | ||
| 19 | use embedded_hal::blocking::spi::Transfer; | ||
| 20 | use stm32l4::stm32l4x5 as pac; | ||
| 21 | |||
| 22 | #[entry] | ||
| 23 | fn main() -> ! { | ||
| 24 | info!("Hello World, dude!"); | ||
| 25 | |||
| 26 | let pp = pac::Peripherals::take().unwrap(); | ||
| 27 | |||
| 28 | pp.DBGMCU.cr.modify(|_, w| { | ||
| 29 | w.dbg_sleep().set_bit(); | ||
| 30 | w.dbg_standby().set_bit(); | ||
| 31 | w.dbg_stop().set_bit() | ||
| 32 | }); | ||
| 33 | |||
| 34 | pp.RCC.ahb2enr.modify(|_, w| { | ||
| 35 | w.gpioaen().set_bit(); | ||
| 36 | w.gpioben().set_bit(); | ||
| 37 | w.gpiocen().set_bit(); | ||
| 38 | w.gpioden().set_bit(); | ||
| 39 | w.gpioeen().set_bit(); | ||
| 40 | w.gpiofen().set_bit(); | ||
| 41 | w | ||
| 42 | }); | ||
| 43 | |||
| 44 | let p = embassy_stm32::init(Default::default()); | ||
| 45 | |||
| 46 | let mut spi = Spi::new( | ||
| 47 | Hertz(16_000_000), | ||
| 48 | p.SPI3, | ||
| 49 | p.PC10, | ||
| 50 | p.PC12, | ||
| 51 | p.PC11, | ||
| 52 | Hertz(1_000_000), | ||
| 53 | Config::default(), | ||
| 54 | ); | ||
| 55 | |||
| 56 | let mut cs = Output::new(p.PE0, Level::High); | ||
| 57 | |||
| 58 | loop { | ||
| 59 | let mut buf = [0x0A; 4]; | ||
| 60 | unwrap!(cs.set_low()); | ||
| 61 | unwrap!(spi.transfer(&mut buf)); | ||
| 62 | unwrap!(cs.set_high()); | ||
| 63 | info!("xfer {=[u8]:x}", buf); | ||
| 64 | } | ||
| 65 | } | ||
diff --git a/examples/stm32l4/src/example_common.rs b/examples/stm32l4/src/example_common.rs new file mode 100644 index 000000000..54d633837 --- /dev/null +++ b/examples/stm32l4/src/example_common.rs | |||
| @@ -0,0 +1,17 @@ | |||
| 1 | #![macro_use] | ||
| 2 | |||
| 3 | use defmt_rtt as _; // global logger | ||
| 4 | use panic_probe as _; | ||
| 5 | |||
| 6 | pub use defmt::*; | ||
| 7 | |||
| 8 | use core::sync::atomic::{AtomicUsize, Ordering}; | ||
| 9 | |||
| 10 | defmt::timestamp! {"{=u64}", { | ||
| 11 | static COUNT: AtomicUsize = AtomicUsize::new(0); | ||
| 12 | // NOTE(no-CAS) `timestamps` runs with interrupts disabled | ||
| 13 | let n = COUNT.load(Ordering::Relaxed); | ||
| 14 | COUNT.store(n + 1, Ordering::Relaxed); | ||
| 15 | n as u64 | ||
| 16 | } | ||
| 17 | } | ||
