aboutsummaryrefslogtreecommitdiff
path: root/examples/blink-rtic.rs
blob: e463f33f6110ee51c7febf0c3f8407a9ab404be5 (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
//! Slowly blink an LED when a timer interrupt fires.
//!
//! This example is a little more complex, and shows that the
//! vector table is placed and known to the processor.

#![no_std]
#![no_main]

use imxrt_rt as _;

/// A static that forces this binary to include a .data section.
/// This is checked in an automated test.
static mut DATA: u32 = 5;

#[unsafe(link_section = ".xip")]
#[unsafe(no_mangle)]
#[inline(never)]
fn increment_data() {
    unsafe { crate::DATA = crate::DATA.wrapping_add(1) };
}

#[rtic::app(device = board::rtic_support, peripherals = false)]
mod app {
    const PIT_PERIOD_US: u32 = 1_000_000;

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

    #[shared]
    struct Shared {}

    #[init]
    fn init(_: init::Context) -> (Shared, Local, init::Monotonics) {
        let board::Resources { mut pit, led, .. } = board::prepare(PIT_PERIOD_US).unwrap();
        pit.loop_with_interrupts();
        led.set();
        (Shared {}, Local { led, pit }, init::Monotonics())
    }

    #[task(binds = PIT, local = [led, pit])]
    fn pit(cx: pit::Context) {
        crate::increment_data();
        cx.local.led.toggle();
        cx.local.pit.clear_interrupts();
    }
}