From 34d1698f94da1ff690413b28d5fa403d611d8b42 Mon Sep 17 00:00:00 2001 From: Per Lindgren <per.lindgren@ltu.se> Date: Mon, 15 Feb 2021 12:24:07 +0100 Subject: [PATCH] added rtt_rtic_blinky, rtt_rtic_hello --- examples/rtt_rtic_blinky.rs | 78 +++++++++++++++++++++++++++++++++++++ examples/rtt_rtic_hello.rs | 27 +++++++++++++ 2 files changed, 105 insertions(+) create mode 100644 examples/rtt_rtic_blinky.rs create mode 100644 examples/rtt_rtic_hello.rs diff --git a/examples/rtt_rtic_blinky.rs b/examples/rtt_rtic_blinky.rs new file mode 100644 index 0000000..67e5eaa --- /dev/null +++ b/examples/rtt_rtic_blinky.rs @@ -0,0 +1,78 @@ +#![deny(unsafe_code)] +#![deny(warnings)] +#![no_main] +#![no_std] + +use cortex_m::peripheral::DWT; +use panic_rtt_target as _; +use rtic::cyccnt::{Instant, U32Ext as _}; +use rtt_target::{rprintln, rtt_init_print}; +use stm32f4xx_hal::stm32; + +#[rtic::app(device = stm32f4xx_hal::stm32, monotonic = rtic::cyccnt::CYCCNT, peripherals = true)] +const APP: () = { + struct Resources { + // late resources + GPIOA: stm32::GPIOA, + } + #[init(schedule = [toggle])] + fn init(cx: init::Context) -> init::LateResources { + rtt_init_print!(); + rprintln!("init"); + + let mut core = cx.core; + let device = cx.device; + + // Initialize (enable) the monotonic timer (CYCCNT) + core.DCB.enable_trace(); + // required on Cortex-M7 devices that software lock the DWT (e.g. STM32F7) + DWT::unlock(); + core.DWT.enable_cycle_counter(); + + // semantically, the monotonic timer is frozen at time "zero" during `init` + // NOTE do *not* call `Instant::now` in this context; it will return a nonsense value + let now = cx.start; // the start time of the system + + // Schedule `toggle` to run 8e6 cycles (clock cycles) in the future + cx.schedule.toggle(now + 8_000_000.cycles()).unwrap(); + + // power on GPIOA, RM0368 6.3.11 + device.RCC.ahb1enr.modify(|_, w| w.gpioaen().set_bit()); + // configure PA5 as output, RM0368 8.4.1 + device.GPIOA.moder.modify(|_, w| w.moder5().bits(1)); + + // pass on late resources + init::LateResources { + GPIOA: device.GPIOA, + } + } + + #[idle] + fn idle(_cx: idle::Context) -> ! { + rprintln!("idle"); + loop { + continue; + } + } + + #[task(resources = [GPIOA], schedule = [toggle])] + fn toggle(cx: toggle::Context) { + static mut TOGGLE: bool = false; + rprintln!("toggle @ {:?}", Instant::now()); + + if *TOGGLE { + cx.resources.GPIOA.bsrr.write(|w| w.bs5().set_bit()); + } else { + cx.resources.GPIOA.bsrr.write(|w| w.br5().set_bit()); + } + + *TOGGLE = !*TOGGLE; + cx.schedule + .toggle(cx.scheduled + 8_000_000.cycles()) + .unwrap(); + } + + extern "C" { + fn EXTI0(); + } +}; diff --git a/examples/rtt_rtic_hello.rs b/examples/rtt_rtic_hello.rs new file mode 100644 index 0000000..1eb75e3 --- /dev/null +++ b/examples/rtt_rtic_hello.rs @@ -0,0 +1,27 @@ +// cargo run --example rtt_rtic_hello + +#![no_main] +#![no_std] + +use panic_halt as _; +use rtt_target::{rprintln, rtt_init_print}; +use stm32f4; + +#[rtic::app(device = stm32f4)] +const APP: () = { + #[init] + fn init(_cx: init::Context) { + rtt_init_print!(); + for i in 0..11 { + rprintln!("RTIC Says Hello, world {}!!", i); + } + } + + #[idle] + fn idle(_cx: idle::Context) -> ! { + rprintln!("lets get lazy"); + loop { + continue; + } + } +}; -- GitLab