diff options
| author | Bob McWhirter <[email protected]> | 2021-06-08 10:36:47 -0400 |
|---|---|---|
| committer | Bob McWhirter <[email protected]> | 2021-06-08 10:37:11 -0400 |
| commit | cf3c021c3745c3f555b650f019fd8b1d4982443b (patch) | |
| tree | b91651e5af61fd57b591df4e4c15ffe74f58751e /examples/stm32l4/src/bin/button_exti.rs | |
| parent | b8690e5f5d7b6996e833e9866629f298f88289fe (diff) | |
Initial examples for STM32L4+
Diffstat (limited to 'examples/stm32l4/src/bin/button_exti.rs')
| -rw-r--r-- | examples/stm32l4/src/bin/button_exti.rs | 84 |
1 files changed, 84 insertions, 0 deletions
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 | } | ||
