11
11
// you can put a breakpoint on `rust_begin_unwind` to catch panics
12
12
use panic_halt as _;
13
13
14
- use cortex_m:: asm:: wfi;
15
14
use rtfm:: app;
16
15
17
16
use stm32f1xx_hal:: {
@@ -22,15 +21,20 @@ use stm32f1xx_hal::{
22
21
} ;
23
22
use embedded_hal:: digital:: v2:: OutputPin ;
24
23
25
- #[ app( device = stm32f1xx_hal:: pac) ]
24
+ #[ app( device = stm32f1xx_hal:: pac, peripherals = true ) ]
26
25
const APP : ( ) = {
27
26
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
+
32
35
#[ init]
33
- fn init ( ) -> init:: LateResources {
36
+ fn init ( cx : init:: Context ) -> init:: LateResources {
37
+ let device = cx. device ;
34
38
35
39
// Take ownership over the raw flash and rcc devices and convert them into the corresponding
36
40
// HAL structs
@@ -54,48 +58,48 @@ const APP: () = {
54
58
55
59
// Init the static resources to use them later through RTFM
56
60
init:: LateResources {
57
- LED : led,
58
- TIMER_HANDLER : timer,
61
+ led,
62
+ timer_handler : timer,
59
63
}
60
64
}
61
65
66
+ // This is optional and can be removed. When removed, RTFM automatically creates an
67
+ // idle loop with WFI as its body.
62
68
#[ idle]
63
- fn idle ( ) -> ! {
64
-
69
+ fn idle ( _cx : idle:: Context ) -> ! {
65
70
loop {
66
- // Waits for interrupt
67
- wfi ( ) ;
71
+ cortex_m:: asm:: wfi ( ) ;
68
72
}
69
73
}
70
74
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 ) {
73
77
// Depending on the application, you could want to delegate some of the work done here to
74
78
// the idle task if you want to minimize the latency of interrupts with same priority (if
75
79
// you have any). That could be done with some kind of machine state, etc.
76
80
77
81
// Count used to change the timer update frequency
78
82
static mut COUNT : u8 = 0 ;
79
83
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 ;
84
88
} 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 ;
87
91
}
88
92
* COUNT += 1 ;
89
93
90
94
if * COUNT == 4 {
91
95
// Changes timer update frequency
92
- resources. TIMER_HANDLER . start ( 2 . hz ( ) ) ;
96
+ cx . resources . timer_handler . start ( 2 . hz ( ) ) ;
93
97
} else if * COUNT == 12 {
94
- resources. TIMER_HANDLER . start ( 1 . hz ( ) ) ;
98
+ cx . resources . timer_handler . start ( 1 . hz ( ) ) ;
95
99
* COUNT = 0 ;
96
100
}
97
101
98
102
// Clears the update flag
99
- resources. TIMER_HANDLER . clear_update_interrupt_flag ( ) ;
103
+ cx . resources . timer_handler . clear_update_interrupt_flag ( ) ;
100
104
}
101
105
} ;
0 commit comments