aboutsummaryrefslogtreecommitdiff
path: root/examples/stm32f0/src/bin
diff options
context:
space:
mode:
author@imrank03 <[email protected]>2022-12-21 11:52:40 +0530
committer@imrank03 <[email protected]>2022-12-21 11:52:40 +0530
commit63122f6d7e9b6a3ec9e246a7171444bde41ce9f0 (patch)
treec11971dbbc32e42887bfb0efb26e2936f37cf55d /examples/stm32f0/src/bin
parent46fd82d18404706be7b1de5203d4c0e7de3ae2cc (diff)
button controlled example
Diffstat (limited to 'examples/stm32f0/src/bin')
-rw-r--r--examples/stm32f0/src/bin/button_controlled_blink.rs64
1 files changed, 64 insertions, 0 deletions
diff --git a/examples/stm32f0/src/bin/button_controlled_blink.rs b/examples/stm32f0/src/bin/button_controlled_blink.rs
new file mode 100644
index 000000000..e1f223433
--- /dev/null
+++ b/examples/stm32f0/src/bin/button_controlled_blink.rs
@@ -0,0 +1,64 @@
1//! This example showcases how to create task
2
3#![no_std]
4#![no_main]
5#![feature(type_alias_impl_trait)]
6
7use core::sync::atomic::{AtomicU32, Ordering};
8
9use defmt::info;
10use embassy_executor::Spawner;
11use embassy_stm32::exti::ExtiInput;
12use embassy_stm32::gpio::{AnyPin, Input, Level, Output, Pin, Pull, Speed};
13use embassy_time::{Duration, Timer};
14use {defmt_rtt as _, panic_probe as _};
15
16static BLINK_MS: AtomicU32 = AtomicU32::new(0);
17
18#[embassy_executor::task]
19async fn led_task(led: AnyPin) {
20 // Configure the LED pin as a push pull ouput and obtain handler.
21 // On the Nucleo F091RC theres an on-board LED connected to pin PA5.
22 let mut led = Output::new(led, Level::Low, Speed::Low);
23
24 loop {
25 let del = BLINK_MS.load(Ordering::Relaxed);
26 info!("Value of del is {}", del);
27 Timer::after(Duration::from_millis(del.into())).await;
28 info!("LED toggling");
29 led.toggle();
30 }
31}
32
33#[embassy_executor::main]
34async fn main(spawner: Spawner) {
35 // Initialize and create handle for devicer peripherals
36 let p = embassy_stm32::init(Default::default());
37
38 // Configure the button pin and obtain handler.
39 // On the Nucleo F091RC there is a button connected to pin PC13.
40 let button = Input::new(p.PC13, Pull::None);
41 let mut button = ExtiInput::new(button, p.EXTI13);
42
43 // Create and initialize a delay variable to manage delay loop
44 let mut del_var = 2000;
45
46 // Blink duration value to global context
47 BLINK_MS.store(del_var, Ordering::Relaxed);
48
49 // Spawn LED blinking task
50 spawner.spawn(led_task(p.PA5.degrade())).unwrap();
51
52 loop {
53 // Check if button got pressed
54 button.wait_for_rising_edge().await;
55 info!("rising_edge");
56 del_var = del_var - 200;
57 // If updated delay value drops below 200 then reset it back to starting value
58 if del_var < 200 {
59 del_var = 2000;
60 }
61 // Updated delay value to global context
62 BLINK_MS.store(del_var, Ordering::Relaxed);
63 }
64}