2017-07-21 05:53:44 +02:00
|
|
|
//! An application with one task
|
|
|
|
#![deny(unsafe_code)]
|
2017-12-09 15:12:42 +01:00
|
|
|
#![deny(warnings)]
|
2017-07-21 05:53:44 +02:00
|
|
|
#![feature(proc_macro)]
|
|
|
|
#![no_std]
|
|
|
|
|
|
|
|
extern crate cortex_m;
|
|
|
|
extern crate cortex_m_rtfm as rtfm;
|
|
|
|
extern crate stm32f103xx;
|
|
|
|
|
2017-11-20 05:11:25 +01:00
|
|
|
use cortex_m::peripheral::syst::SystClkSource;
|
2017-07-21 05:53:44 +02:00
|
|
|
use rtfm::{app, Threshold};
|
2017-11-20 05:11:25 +01:00
|
|
|
use stm32f103xx::GPIOC;
|
2017-07-21 05:53:44 +02:00
|
|
|
|
|
|
|
app! {
|
|
|
|
device: stm32f103xx,
|
|
|
|
|
2017-07-28 05:40:47 +02:00
|
|
|
// Here data resources are declared
|
2017-07-27 18:40:15 +02:00
|
|
|
//
|
2017-07-28 05:40:47 +02:00
|
|
|
// Data resources are static variables that are safe to share across tasks
|
2017-07-27 18:40:15 +02:00
|
|
|
resources: {
|
2017-07-28 05:40:47 +02:00
|
|
|
// Declaration of resources looks exactly like declaration of static
|
2017-07-27 18:40:15 +02:00
|
|
|
// variables
|
|
|
|
static ON: bool = false;
|
|
|
|
},
|
|
|
|
|
2017-07-21 05:53:44 +02:00
|
|
|
// Here tasks are declared
|
|
|
|
//
|
|
|
|
// Each task corresponds to an interrupt or an exception. Every time the
|
|
|
|
// interrupt or exception becomes *pending* the corresponding task handler
|
|
|
|
// will be executed.
|
|
|
|
tasks: {
|
|
|
|
// Here we declare that we'll use the SYS_TICK exception as a task
|
|
|
|
SYS_TICK: {
|
2017-07-28 05:40:47 +02:00
|
|
|
// Path to the task handler
|
2017-07-27 18:40:15 +02:00
|
|
|
path: sys_tick,
|
|
|
|
|
2017-07-28 05:40:47 +02:00
|
|
|
// These are the resources this task has access to.
|
2017-07-21 05:53:44 +02:00
|
|
|
//
|
2017-11-20 05:11:25 +01:00
|
|
|
// The resources listed here must also appear in `app.resources`
|
|
|
|
resources: [ON],
|
2017-07-21 05:53:44 +02:00
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-07-29 07:34:00 +02:00
|
|
|
fn init(p: init::Peripherals, r: init::Resources) {
|
|
|
|
// `init` can modify all the `resources` declared in `app!`
|
|
|
|
r.ON;
|
|
|
|
|
2017-07-21 05:53:44 +02:00
|
|
|
// power on GPIOC
|
2017-11-20 05:11:25 +01:00
|
|
|
p.device.RCC.apb2enr.modify(|_, w| w.iopcen().enabled());
|
2017-07-21 05:53:44 +02:00
|
|
|
|
|
|
|
// configure PC13 as output
|
2017-11-20 05:11:25 +01:00
|
|
|
p.device.GPIOC.bsrr.write(|w| w.bs13().set());
|
|
|
|
p.device
|
|
|
|
.GPIOC
|
2017-07-21 05:53:44 +02:00
|
|
|
.crh
|
|
|
|
.modify(|_, w| w.mode13().output().cnf13().push());
|
|
|
|
|
|
|
|
// configure the system timer to generate one interrupt every second
|
2017-11-20 05:11:25 +01:00
|
|
|
p.core.SYST.set_clock_source(SystClkSource::Core);
|
|
|
|
p.core.SYST.set_reload(8_000_000); // 1s
|
|
|
|
p.core.SYST.enable_interrupt();
|
|
|
|
p.core.SYST.enable_counter();
|
2017-07-21 05:53:44 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
fn idle() -> ! {
|
|
|
|
loop {
|
|
|
|
rtfm::wfi();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// This is the task handler of the SYS_TICK exception
|
|
|
|
//
|
2017-07-28 05:40:47 +02:00
|
|
|
// `_t` is the preemption threshold token. We won't use it in this program.
|
|
|
|
//
|
2017-07-29 07:34:00 +02:00
|
|
|
// `r` is the set of resources this task has access to. `SYS_TICK::Resources`
|
2017-07-28 05:40:47 +02:00
|
|
|
// has one field per resource declared in `app!`.
|
2017-11-20 05:11:25 +01:00
|
|
|
#[allow(unsafe_code)]
|
2017-12-09 14:43:29 +01:00
|
|
|
fn sys_tick(_t: &mut Threshold, mut r: SYS_TICK::Resources) {
|
2017-07-21 05:53:44 +02:00
|
|
|
// toggle state
|
2017-12-09 13:38:41 +01:00
|
|
|
*r.ON = !*r.ON;
|
2017-07-21 05:53:44 +02:00
|
|
|
|
2017-12-09 13:38:41 +01:00
|
|
|
if *r.ON {
|
2017-07-21 05:53:44 +02:00
|
|
|
// set the pin PC13 high
|
2017-11-20 05:11:25 +01:00
|
|
|
// NOTE(unsafe) atomic write to a stateless register
|
|
|
|
unsafe {
|
|
|
|
(*GPIOC::ptr()).bsrr.write(|w| w.bs13().set());
|
|
|
|
}
|
2017-07-21 05:53:44 +02:00
|
|
|
} else {
|
|
|
|
// set the pin PC13 low
|
2017-11-20 05:11:25 +01:00
|
|
|
// NOTE(unsafe) atomic write to a stateless register
|
|
|
|
unsafe {
|
|
|
|
(*GPIOC::ptr()).bsrr.write(|w| w.br13().reset());
|
|
|
|
}
|
2017-07-21 05:53:44 +02:00
|
|
|
}
|
|
|
|
}
|