aboutsummaryrefslogtreecommitdiff
path: root/examples/embassy-stm32g4/src/bin/blinky.rs
blob: 9cc48997bd5fddc87557af6bf536693bd80b4693 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
#![deny(unsafe_code)]
#![deny(warnings)]
#![no_main]
#![no_std]
#![feature(type_alias_impl_trait)]

use defmt::*;
use embassy_stm32::gpio::{Level, Output, Speed};
use rtic::app;
use rtic_monotonics::systick::*;
use {defmt_rtt as _, panic_probe as _};

pub mod pac {
    pub use embassy_stm32::pac::Interrupt as interrupt;
    pub use embassy_stm32::pac::*;
}

#[app(device = pac, peripherals = false, dispatchers = [SPI1])]
mod app {
    use super::*;

    #[shared]
    struct Shared {}

    #[local]
    struct Local {}

    #[init]
    fn init(cx: init::Context) -> (Shared, Local) {
        // Initialize the systick interrupt & obtain the token to prove that we did
        let systick_mono_token = rtic_monotonics::create_systick_token!();
        Systick::start(cx.core.SYST, 25_000_000, systick_mono_token);

        let p = embassy_stm32::init(Default::default());
        info!("Hello World!");

        let mut led = Output::new(p.PC6, Level::High, Speed::Low);
        info!("high");
        led.set_high();

        // Schedule the blinking task
        blink::spawn(led).ok();

        (Shared {}, Local {})
    }

    #[task()]
    async fn blink(_cx: blink::Context, mut led: Output<'static, embassy_stm32::peripherals::PC6>) {
        let mut state = true;
        loop {
            info!("blink");
            if state {
                led.set_high();
            } else {
                led.set_low();
            }
            state = !state;
            Systick::delay(1000.millis()).await;
        }
    }
}