aboutsummaryrefslogtreecommitdiff
path: root/examples/teensy4_blinky/src/main.rs
blob: b3b1773577344722f34ae9d15a033c188520308c (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
#![deny(unsafe_code)]
#![deny(warnings)]
#![no_main]
#![no_std]

#[panic_handler]
fn panic(_: &::core::panic::PanicInfo) -> ! {
    ::teensy4_panic::sos()
}

use teensy4_bsp::{board, hal};

use rtic_monotonics::imxrt::prelude::*;
imxrt_gpt1_monotonic!(Mono, board::PERCLK_FREQUENCY);

#[rtic::app(device = teensy4_bsp, dispatchers = [LPSPI1])]
mod app {
    use super::*;

    #[shared]
    struct Shared {}

    #[local]
    struct Local {
        led: board::Led,
    }

    #[init]
    fn init(cx: init::Context) -> (Shared, Local) {
        let board::Resources {
            pins,
            mut gpio2,
            mut gpt1,
            ..
        } = board::t40(cx.device);

        // Initialize Monotonic
        gpt1.set_clock_source(hal::gpt::ClockSource::PeripheralClock);
        Mono::start(gpt1.release());

        // Setup LED
        let led = board::led(&mut gpio2, pins.p13);
        led.set();

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

        (Shared {}, Local { led })
    }

    #[task(local = [led])]
    async fn blink(cx: blink::Context) {
        let blink::LocalResources { led, .. } = cx.local;

        loop {
            led.toggle();
            Mono::delay(1000.millis()).await;
        }
    }
}