aboutsummaryrefslogtreecommitdiff
path: root/examples/schedule-after-from-event-task.rs
blob: c7190d3c995252f1453c8c479be8206933ee180b (plain) (blame)
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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
#![deny(warnings)]
#![feature(proc_macro)]
#![feature(proc_macro_gen)]
#![no_main]
#![no_std]

#[macro_use]
extern crate cortex_m;
#[macro_use]
extern crate cortex_m_rt as rt;
extern crate cortex_m_rtfm as rtfm;
extern crate panic_abort;
extern crate stm32f103xx;

use cortex_m::peripheral::{DWT, ITM};
use rt::ExceptionFrame;
use rtfm::app;

app! {
    device: stm32f103xx,

    resources: {
        static ITM: ITM;
    },

    free_interrupts: [EXTI1],

    tasks: {
        exti0: {
            interrupt: EXTI0,
            schedule_after: [a],
            resources: [ITM],
        },

        a: {
            resources: [ITM],
        },
    },
}

const S: u32 = 8_000_000;

#[inline(always)]
fn init(ctxt: init::Context) -> init::LateResources {
    rtfm::_impl::trigger(stm32f103xx::Interrupt::EXTI0);

    init::LateResources { ITM: ctxt.core.ITM }
}

fn exti0(mut ctxt: exti0::Context) {
    let now = DWT::get_cycle_count();
    iprintln!(
        &mut ctxt.resources.ITM.stim[0],
        "exti0(st={}, now={})",
        ctxt.start_time,
        now
    );

    let t = &mut ctxt.priority;
    ctxt.tasks.a.schedule_after(t, 1 * S).ok();
}

fn a(ctxt: a::Context) {
    let now = DWT::get_cycle_count();
    iprintln!(
        &mut ctxt.resources.ITM.stim[0],
        "a(st={}, now={})",
        ctxt.scheduled_time,
        now
    );
}

exception!(HardFault, hard_fault);

#[inline(always)]
fn hard_fault(ef: &ExceptionFrame) -> ! {
    panic!("HardFault at {:#?}", ef);
}

exception!(*, default_handler);

#[inline(always)]
fn default_handler(irqn: i16) {
    panic!("Unhandled exception (IRQn = {})", irqn);
}