Skip to content

Commit 70a32c5

Browse files
committed
Update timer-interrupt-rtfm example
1 parent 7632a31 commit 70a32c5

File tree

1 file changed

+28
-24
lines changed

1 file changed

+28
-24
lines changed

examples/timer-interrupt-rtfm.rs

Lines changed: 28 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111
// you can put a breakpoint on `rust_begin_unwind` to catch panics
1212
use panic_halt as _;
1313

14-
use cortex_m::asm::wfi;
1514
use rtfm::app;
1615

1716
use stm32f1xx_hal::{
@@ -22,15 +21,20 @@ use stm32f1xx_hal::{
2221
};
2322
use embedded_hal::digital::v2::OutputPin;
2423

25-
#[app(device = stm32f1xx_hal::pac)]
24+
#[app(device = stm32f1xx_hal::pac, peripherals = true)]
2625
const APP: () = {
2726

28-
static mut LED: PC13<Output<PushPull>> = ();
29-
static mut TIMER_HANDLER: CountDownTimer<pac::TIM1> = ();
30-
static mut LED_STATE: bool = false;
31-
27+
struct Resources {
28+
led: PC13<Output<PushPull>>,
29+
timer_handler: CountDownTimer<pac::TIM1>,
30+
31+
#[init(false)]
32+
led_state: bool,
33+
}
34+
3235
#[init]
33-
fn init() -> init::LateResources {
36+
fn init(cx: init::Context) -> init::LateResources {
37+
let device = cx.device;
3438

3539
// Take ownership over the raw flash and rcc devices and convert them into the corresponding
3640
// HAL structs
@@ -54,48 +58,48 @@ const APP: () = {
5458

5559
// Init the static resources to use them later through RTFM
5660
init::LateResources {
57-
LED: led,
58-
TIMER_HANDLER: timer,
61+
led,
62+
timer_handler: timer,
5963
}
6064
}
6165

66+
// This is optional and can be removed. When removed, RTFM automatically creates an
67+
// idle loop with WFI as its body.
6268
#[idle]
63-
fn idle() -> ! {
64-
69+
fn idle(_cx: idle::Context) -> ! {
6570
loop {
66-
// Waits for interrupt
67-
wfi();
71+
cortex_m::asm::wfi();
6872
}
6973
}
7074

71-
#[interrupt(priority = 1, resources = [LED, TIMER_HANDLER, LED_STATE])]
72-
fn TIM1_UP() {
75+
#[task(binds = TIM1_UP, priority = 1, resources = [led, timer_handler, led_state])]
76+
fn tim1_up(cx: tim1_up::Context) {
7377
// Depending on the application, you could want to delegate some of the work done here to
7478
// the idle task if you want to minimize the latency of interrupts with same priority (if
7579
// you have any). That could be done with some kind of machine state, etc.
7680

7781
// Count used to change the timer update frequency
7882
static mut COUNT: u8 = 0;
7983

80-
if *resources.LED_STATE {
81-
// Uses resourcers managed by rtfm to turn led off (on bluepill)
82-
resources.LED.set_high().unwrap();
83-
*resources.LED_STATE = false;
84+
if *cx.resources.led_state {
85+
// Uses resources managed by rtfm to turn led off (on bluepill)
86+
cx.resources.led.set_high().unwrap();
87+
*cx.resources.led_state = false;
8488
} else {
85-
resources.LED.set_low().unwrap();
86-
*resources.LED_STATE = true;
89+
cx.resources.led.set_low().unwrap();
90+
*cx.resources.led_state = true;
8791
}
8892
*COUNT += 1;
8993

9094
if *COUNT == 4 {
9195
// Changes timer update frequency
92-
resources.TIMER_HANDLER.start(2.hz());
96+
cx.resources.timer_handler.start(2.hz());
9397
} else if *COUNT == 12 {
94-
resources.TIMER_HANDLER.start(1.hz());
98+
cx.resources.timer_handler.start(1.hz());
9599
*COUNT = 0;
96100
}
97101

98102
// Clears the update flag
99-
resources.TIMER_HANDLER.clear_update_interrupt_flag();
103+
cx.resources.timer_handler.clear_update_interrupt_flag();
100104
}
101105
};

0 commit comments

Comments
 (0)