From 66aff9a87d0f6fed8d5cd9b53829213a51fc9b5d Mon Sep 17 00:00:00 2001 From: Paul van Tilburg Date: Tue, 5 Mar 2019 15:05:45 +0100 Subject: [PATCH] The rtfm example becomes the new main binary --- examples/rtfm.rs | 54 ----------------------------------- src/main.rs | 73 ++++++++++++++++++++++++++---------------------- 2 files changed, 39 insertions(+), 88 deletions(-) delete mode 100644 examples/rtfm.rs diff --git a/examples/rtfm.rs b/examples/rtfm.rs deleted file mode 100644 index 2585cff..0000000 --- a/examples/rtfm.rs +++ /dev/null @@ -1,54 +0,0 @@ -//! examples/rtfm.rs - -#![deny(unsafe_code)] -#![deny(warnings)] -#![no_main] -#![no_std] - -extern crate panic_semihosting; - -//use cortex_m_semihosting::hprintln; -use hal::gpio::{Output, PushPull}; -use hal::prelude::*; -use rtfm::{app, Instant}; - -type Led = hal::gpio::gpiod::PD>; - -const PERIOD: u32 = 8_000_000; - -#[app(device = hal::stm32)] -const APP: () = { - static mut index: usize = (); - static mut leds: [Led; 4] = (); - - #[init(schedule = [switch_leds])] - fn init() -> init::LateResources { - // Set up the LEDs. - let gpiod = device.GPIOD.split(); - let leds = [ - gpiod.pd12.into_push_pull_output().downgrade(), - gpiod.pd13.into_push_pull_output().downgrade(), - gpiod.pd14.into_push_pull_output().downgrade(), - gpiod.pd15.into_push_pull_output().downgrade(), - ]; - - schedule.switch_leds(Instant::now() + PERIOD.cycles()).unwrap(); - - init::LateResources { index: 0, leds } - } - - #[task(schedule = [switch_leds], resources = [index, leds])] - fn switch_leds() { - let index = *resources.index; - let num_leds = resources.leds.len(); - resources.leds[index].set_high(); - resources.leds[(index + 2) % num_leds].set_low(); - *resources.index = (index + 1) % 4; - - schedule.switch_leds(scheduled + PERIOD.cycles()).unwrap(); - } - - extern "C" { - fn UART4(); - } -}; diff --git a/src/main.rs b/src/main.rs index 550dffa..77b2bb9 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,47 +1,52 @@ #![deny(unsafe_code)] #![deny(warnings)] -#![no_std] #![no_main] +#![no_std] -#[cfg(debug_assertions)] extern crate panic_semihosting; -#[cfg(not(debug_assertions))] -extern crate panic_abort; - -use cortex_m_rt::entry; //use cortex_m_semihosting::hprintln; -use hal::delay::Delay; +use hal::gpio::{Output, PushPull}; use hal::prelude::*; +use rtfm::{app, Instant}; -#[entry] -fn main() -> ! { - // Get handles to the hardware. - let core = cortex_m::Peripherals::take().unwrap(); - let device = hal::stm32::Peripherals::take().unwrap(); +type Led = hal::gpio::gpiod::PD>; - // Get a clock for the delay. - let rcc = device.RCC.constrain(); - let clocks = rcc.cfgr.freeze(); - let mut delay = Delay::new(core.SYST, clocks); +const PERIOD: u32 = 8_000_000; - // Set up the LEDs. - let gpiod = device.GPIOD.split(); - let mut leds = [ - gpiod.pd12.into_push_pull_output().downgrade(), - gpiod.pd13.into_push_pull_output().downgrade(), - gpiod.pd14.into_push_pull_output().downgrade(), - gpiod.pd15.into_push_pull_output().downgrade(), - ]; - let num_leds = leds.len(); - assert_eq!(num_leds, 4); +#[app(device = hal::stm32)] +const APP: () = { + static mut index: usize = (); + static mut leds: [Led; 4] = (); - // Blink the LED... - loop { - for index in 0..num_leds { - leds[index].set_high(); - leds[(index + 1) % num_leds].set_low(); - delay.delay_ms(500u16); - } + #[init(schedule = [switch_leds])] + fn init() -> init::LateResources { + // Set up the LEDs. + let gpiod = device.GPIOD.split(); + let leds = [ + gpiod.pd12.into_push_pull_output().downgrade(), + gpiod.pd13.into_push_pull_output().downgrade(), + gpiod.pd14.into_push_pull_output().downgrade(), + gpiod.pd15.into_push_pull_output().downgrade(), + ]; + + schedule.switch_leds(Instant::now() + PERIOD.cycles()).unwrap(); + + init::LateResources { index: 0, leds } } -} + + #[task(schedule = [switch_leds], resources = [index, leds])] + fn switch_leds() { + let index = *resources.index; + let num_leds = resources.leds.len(); + resources.leds[index].set_high(); + resources.leds[(index + 2) % num_leds].set_low(); + *resources.index = (index + 1) % 4; + + schedule.switch_leds(scheduled + PERIOD.cycles()).unwrap(); + } + + extern "C" { + fn UART4(); + } +};