Skip to content

Commit 8b52514

Browse files
eupnTeXitoi
authored andcommitted
Update RTFM-based examples (#173)
1 parent b9ce574 commit 8b52514

File tree

3 files changed

+56
-47
lines changed

3 files changed

+56
-47
lines changed

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ optional = true
5050
panic-halt = "0.2.0"
5151
panic-semihosting = "0.5.2"
5252
panic-itm = "0.4.1"
53-
cortex-m-rtfm = "0.4.3"
53+
cortex-m-rtfm = "0.5"
5454
cortex-m-semihosting = "0.3.3"
5555
heapless = "0.4.3"
5656
m = "0.1.1"

examples/timer-interrupt-rtfm.rs

Lines changed: 34 additions & 29 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,80 +21,86 @@ 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-
32-
#[init]
33-
fn init() -> init::LateResources {
27+
struct Resources {
28+
led: PC13<Output<PushPull>>,
29+
timer_handler: CountDownTimer<pac::TIM1>,
30+
31+
#[init(false)]
32+
led_state: bool,
33+
}
3434

35+
#[init]
36+
fn init(cx: init::Context) -> init::LateResources {
3537
// Take ownership over the raw flash and rcc devices and convert them into the corresponding
3638
// HAL structs
37-
let mut flash = device.FLASH.constrain();
38-
let mut rcc = device.RCC.constrain();
39+
let mut flash = cx.device.FLASH.constrain();
40+
let mut rcc = cx.device.RCC.constrain();
3941

4042
// Freeze the configuration of all the clocks in the system and store the frozen frequencies
4143
// in `clocks`
4244
let clocks = rcc.cfgr.freeze(&mut flash.acr);
4345

4446
// Acquire the GPIOC peripheral
45-
let mut gpioc = device.GPIOC.split(&mut rcc.apb2);
47+
let mut gpioc = cx.device.GPIOC.split(&mut rcc.apb2);
4648

4749
// Configure gpio C pin 13 as a push-pull output. The `crh` register is passed to the
4850
// function in order to configure the port. For pins 0-7, crl should be passed instead
4951
let led = gpioc.pc13.into_push_pull_output_with_state(&mut gpioc.crh, State::High);
5052
// Configure the syst timer to trigger an update every second and enables interrupt
51-
let mut timer = Timer::tim1(device.TIM1, &clocks, &mut rcc.apb2)
53+
let mut timer = Timer::tim1(cx.device.TIM1, &clocks, &mut rcc.apb2)
5254
.start_count_down(1.hz());
5355
timer.listen(Event::Update);
5456

5557
// Init the static resources to use them later through RTFM
5658
init::LateResources {
57-
LED: led,
58-
TIMER_HANDLER: timer,
59+
led,
60+
timer_handler: timer,
5961
}
6062
}
6163

64+
// Optional.
65+
//
66+
// https://rtfm.rs/0.5/book/en/by-example/app.html#idle
67+
// > When no idle function is declared, the runtime sets the SLEEPONEXIT bit and then
68+
// > sends the microcontroller to sleep after running init.
6269
#[idle]
63-
fn idle() -> ! {
64-
70+
fn idle(_cx: idle::Context) -> ! {
6571
loop {
66-
// Waits for interrupt
67-
wfi();
72+
cortex_m::asm::wfi();
6873
}
6974
}
7075

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

7782
// Count used to change the timer update frequency
7883
static mut COUNT: u8 = 0;
7984

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;
85+
if *cx.resources.led_state {
86+
// Uses resources managed by rtfm to turn led off (on bluepill)
87+
cx.resources.led.set_high().unwrap();
88+
*cx.resources.led_state = false;
8489
} else {
85-
resources.LED.set_low().unwrap();
86-
*resources.LED_STATE = true;
90+
cx.resources.led.set_low().unwrap();
91+
*cx.resources.led_state = true;
8792
}
8893
*COUNT += 1;
8994

9095
if *COUNT == 4 {
9196
// Changes timer update frequency
92-
resources.TIMER_HANDLER.start(2.hz());
97+
cx.resources.timer_handler.start(2.hz());
9398
} else if *COUNT == 12 {
94-
resources.TIMER_HANDLER.start(1.hz());
99+
cx.resources.timer_handler.start(1.hz());
95100
*COUNT = 0;
96101
}
97102

98103
// Clears the update flag
99-
resources.TIMER_HANDLER.clear_update_interrupt_flag();
104+
cx.resources.timer_handler.clear_update_interrupt_flag();
100105
}
101106
};

examples/usb_serial_rtfm.rs

Lines changed: 21 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -15,17 +15,19 @@ use usb_device::bus;
1515
use usb_device::prelude::*;
1616
use usbd_serial::{SerialPort, USB_CLASS_CDC};
1717

18-
#[app(device = stm32f1xx_hal::stm32)]
18+
#[app(device = stm32f1xx_hal::stm32, peripherals = true)]
1919
const APP: () = {
20-
static mut USB_DEV: UsbDevice<'static, UsbBusType> = ();
21-
static mut SERIAL: SerialPort<'static, UsbBusType> = ();
20+
struct Resources {
21+
usb_dev: UsbDevice<'static, UsbBusType>,
22+
serial: SerialPort<'static, UsbBusType>,
23+
}
2224

2325
#[init]
24-
fn init() {
26+
fn init(cx: init::Context) -> init::LateResources {
2527
static mut USB_BUS: Option<bus::UsbBusAllocator<UsbBusType>> = None;
2628

27-
let mut flash = device.FLASH.constrain();
28-
let mut rcc = device.RCC.constrain();
29+
let mut flash = cx.device.FLASH.constrain();
30+
let mut rcc = cx.device.RCC.constrain();
2931

3032
let clocks = rcc
3133
.cfgr
@@ -36,21 +38,21 @@ const APP: () = {
3638

3739
assert!(clocks.usbclk_valid());
3840

39-
let mut gpioa = device.GPIOA.split(&mut rcc.apb2);
41+
let mut gpioa = cx.device.GPIOA.split(&mut rcc.apb2);
4042

4143
// BluePill board has a pull-up resistor on the D+ line.
4244
// Pull the D+ pin down to send a RESET condition to the USB bus.
4345
// This forced reset is needed only for development, without it host
4446
// will not reset your device when you upload new firmware.
4547
let mut usb_dp = gpioa.pa12.into_push_pull_output(&mut gpioa.crh);
46-
usb_dp.set_low();
48+
usb_dp.set_low().unwrap();
4749
delay(clocks.sysclk().0 / 100);
4850

4951
let usb_dm = gpioa.pa11;
5052
let usb_dp = usb_dp.into_floating_input(&mut gpioa.crh);
5153

5254
let usb = Peripheral {
53-
usb: device.USB,
55+
usb: cx.device.USB,
5456
pin_dm: usb_dm,
5557
pin_dp: usb_dp,
5658
};
@@ -66,18 +68,20 @@ const APP: () = {
6668
.device_class(USB_CLASS_CDC)
6769
.build();
6870

69-
USB_DEV = usb_dev;
70-
SERIAL = serial;
71+
init::LateResources {
72+
usb_dev,
73+
serial,
74+
}
7175
}
7276

73-
#[interrupt(resources = [USB_DEV, SERIAL])]
74-
fn USB_HP_CAN_TX() {
75-
usb_poll(&mut resources.USB_DEV, &mut resources.SERIAL);
77+
#[task(binds = USB_HP_CAN_TX, resources = [usb_dev, serial])]
78+
fn usb_tx(mut cx: usb_tx::Context) {
79+
usb_poll(&mut cx.resources.usb_dev, &mut cx.resources.serial);
7680
}
7781

78-
#[interrupt(resources = [USB_DEV, SERIAL])]
79-
fn USB_LP_CAN_RX0() {
80-
usb_poll(&mut resources.USB_DEV, &mut resources.SERIAL);
82+
#[task(binds = USB_LP_CAN_RX0, resources = [usb_dev, serial])]
83+
fn usb_rx0(mut cx: usb_rx0::Context) {
84+
usb_poll(&mut cx.resources.usb_dev, &mut cx.resources.serial);
8185
}
8286
};
8387

0 commit comments

Comments
 (0)