2017-04-27 22:37:27 +02:00
|
|
|
//! Real Time For the Masses (RTFM), a framework for building concurrent
|
|
|
|
//! applications, for ARM Cortex-M microcontrollers
|
2017-04-21 07:24:54 +02:00
|
|
|
//!
|
2017-04-26 20:01:16 +02:00
|
|
|
//! This crate is based on [the RTFM framework] created by the Embedded Systems
|
2017-04-27 22:37:27 +02:00
|
|
|
//! group at [Luleå University of Technology][ltu], led by Prof. Per Lindgren,
|
|
|
|
//! and uses a simplified version of the Stack Resource Policy as scheduling
|
|
|
|
//! policy (check the [references] for details).
|
2017-04-21 07:24:54 +02:00
|
|
|
//!
|
2017-04-26 04:51:07 +02:00
|
|
|
//! [the RTFM framework]: http://www.rtfm-lang.org/
|
2017-04-27 22:37:27 +02:00
|
|
|
//! [ltu]: https://www.ltu.se/?l=en
|
2017-04-21 07:24:54 +02:00
|
|
|
//! [per]: https://www.ltu.se/staff/p/pln-1.11258?l=en
|
|
|
|
//! [references]: ./index.html#references
|
|
|
|
//!
|
|
|
|
//! # Features
|
|
|
|
//!
|
2017-04-25 16:23:27 +02:00
|
|
|
//! - **Event triggered tasks** as the unit of concurrency.
|
|
|
|
//! - Supports prioritization of tasks and, thus, **preemptive multitasking**.
|
2017-04-27 22:37:27 +02:00
|
|
|
//! - **Efficient and data race free memory sharing** through fine grained *non
|
2017-04-26 04:51:07 +02:00
|
|
|
//! global* critical sections.
|
2017-04-27 22:37:27 +02:00
|
|
|
//! - **Deadlock free execution**, guaranteed at compile time.
|
2017-04-26 04:51:07 +02:00
|
|
|
//! - **Minimal scheduling overhead** as the scheduler has no "software
|
|
|
|
//! component"; the hardware does all the scheduling.
|
|
|
|
//! - **Highly efficient memory usage**. All the tasks share the call stack and
|
|
|
|
//! there's no hard dependency on a dynamic allocator.
|
|
|
|
//! - **All Cortex M3, M4 and M7 devices are fully supported**. M0(+) is
|
|
|
|
//! partially supported as the whole API is not available (due to missing
|
|
|
|
//! hardware features).
|
2017-04-22 05:02:49 +02:00
|
|
|
//! - The number of task priority levels is configurable at compile time through
|
|
|
|
//! the `P2` (4 levels), `P3` (8 levels), etc. Cargo features. The number of
|
2017-04-21 07:24:54 +02:00
|
|
|
//! priority levels supported by the hardware is device specific but this
|
|
|
|
//! crate defaults to 16 as that's the most common scenario.
|
|
|
|
//! - This task model is amenable to known WCET (Worst Case Execution Time)
|
2017-04-22 05:02:49 +02:00
|
|
|
//! analysis and scheduling analysis techniques. (Though we haven't yet
|
2017-04-25 16:23:27 +02:00
|
|
|
//! developed Rust friendly tooling for that.)
|
2017-04-21 07:24:54 +02:00
|
|
|
//!
|
2017-04-27 22:37:27 +02:00
|
|
|
//! # Requirements
|
|
|
|
//!
|
|
|
|
//! - Tasks must run to completion. That's it, tasks can't contain endless
|
|
|
|
//! loops.
|
2017-04-25 16:23:27 +02:00
|
|
|
//! - Task priorities must remain constant at runtime.
|
2017-04-21 07:24:54 +02:00
|
|
|
//!
|
|
|
|
//! # Dependencies
|
|
|
|
//!
|
2017-04-26 02:46:33 +02:00
|
|
|
//! - A device crate generated using [`svd2rust`] v0.7.x
|
2017-04-21 07:24:54 +02:00
|
|
|
//! - A `start` lang time: Vanilla `main` must be supported in binary crates.
|
|
|
|
//! You can use the [`cortex-m-rt`] crate to fulfill the requirement
|
|
|
|
//!
|
2017-04-26 02:46:33 +02:00
|
|
|
//! [`svd2rust`]: https://docs.rs/svd2rust/0.7.0/svd2rust/
|
2017-04-21 07:24:54 +02:00
|
|
|
//! [`cortex-m-rt`]: https://docs.rs/cortex-m-rt/0.1.1/cortex_m_rt/
|
|
|
|
//!
|
|
|
|
//! # Examples
|
|
|
|
//!
|
|
|
|
//! Ordered in increasing level of complexity:
|
|
|
|
//!
|
|
|
|
//! - [Zero tasks](./index.html#zero-tasks)
|
|
|
|
//! - [One task](./index.html#one-task)
|
|
|
|
//! - [Two "serial" tasks](./index.html#two-serial-tasks)
|
|
|
|
//! - [Preemptive multitasking](./index.html#preemptive-multitasking)
|
|
|
|
//! - [Peripherals as resources](./index.html#peripherals-as-resources)
|
|
|
|
//!
|
|
|
|
//! ## Zero tasks
|
|
|
|
//!
|
|
|
|
//! ``` ignore
|
2017-04-26 04:51:07 +02:00
|
|
|
//! #![feature(used)]
|
2017-04-21 07:24:54 +02:00
|
|
|
//! #![no_std]
|
|
|
|
//!
|
2017-04-22 05:45:23 +02:00
|
|
|
//! #[macro_use] // for the `hprintln!` macro
|
2017-04-21 07:24:54 +02:00
|
|
|
//! extern crate cortex_m;
|
|
|
|
//!
|
|
|
|
//! // before main initialization + `start` lang item
|
|
|
|
//! extern crate cortex_m_rt;
|
|
|
|
//!
|
2017-04-22 05:45:23 +02:00
|
|
|
//! #[macro_use] // for the `tasks!` macro
|
2017-04-21 07:24:54 +02:00
|
|
|
//! extern crate cortex_m_rtfm as rtfm;
|
|
|
|
//!
|
|
|
|
//! // device crate generated using svd2rust
|
2017-04-25 16:23:27 +02:00
|
|
|
//! extern crate stm32f30x;
|
2017-04-21 07:24:54 +02:00
|
|
|
//!
|
2017-04-28 16:23:30 +02:00
|
|
|
//! use rtfm::{C0, C16, P0};
|
2017-04-21 07:24:54 +02:00
|
|
|
//!
|
2017-04-25 16:23:27 +02:00
|
|
|
//! // TASKS (None in this example)
|
|
|
|
//! tasks!(stm32f30x, {});
|
2017-04-21 07:24:54 +02:00
|
|
|
//!
|
|
|
|
//! // INITIALIZATION PHASE
|
|
|
|
//! fn init(_priority: P0, _ceiling: &C16) {
|
|
|
|
//! hprintln!("INIT");
|
|
|
|
//! }
|
|
|
|
//!
|
|
|
|
//! // IDLE LOOP
|
2017-04-28 16:23:30 +02:00
|
|
|
//! fn idle(_priority: P0, _ceiling: C0) -> ! {
|
2017-04-21 07:24:54 +02:00
|
|
|
//! hprintln!("IDLE");
|
|
|
|
//!
|
|
|
|
//! // Sleep
|
|
|
|
//! loop {
|
|
|
|
//! rtfm::wfi();
|
|
|
|
//! }
|
|
|
|
//! }
|
|
|
|
//! ```
|
|
|
|
//!
|
|
|
|
//! Expected output:
|
|
|
|
//!
|
|
|
|
//! ``` text
|
|
|
|
//! INIT
|
|
|
|
//! IDLE
|
|
|
|
//! ```
|
|
|
|
//!
|
2017-04-22 05:45:23 +02:00
|
|
|
//! The `tasks!` macro overrides the `main` function and imposes the following
|
|
|
|
//! structure into your program:
|
2017-04-21 07:24:54 +02:00
|
|
|
//!
|
2017-04-25 16:23:27 +02:00
|
|
|
//! - `init`, the initialization phase, runs first. This function is executed
|
2017-04-26 04:51:07 +02:00
|
|
|
//! "atomically", in the sense that no task / interrupt can preempt it.
|
2017-04-21 07:24:54 +02:00
|
|
|
//!
|
|
|
|
//! - `idle`, a never ending function that runs after `init`.
|
|
|
|
//!
|
|
|
|
//! Note that both `init` and `idle` have priority 0, the lowest priority.
|
|
|
|
//!
|
|
|
|
//! # One task
|
|
|
|
//!
|
|
|
|
//! ``` ignore
|
2017-04-26 04:51:07 +02:00
|
|
|
//! #![feature(const_fn)]
|
|
|
|
//! #![feature(used)]
|
2017-04-21 07:24:54 +02:00
|
|
|
//! #![no_std]
|
|
|
|
//!
|
|
|
|
//! extern crate cortex_m_rt;
|
|
|
|
//! #[macro_use]
|
|
|
|
//! extern crate cortex_m_rtfm as rtfm;
|
2017-04-25 16:23:27 +02:00
|
|
|
//! extern crate stm32f30x;
|
2017-04-21 07:24:54 +02:00
|
|
|
//!
|
2017-04-25 16:23:27 +02:00
|
|
|
//! use stm32f30x::interrupt::Tim7;
|
2017-04-28 16:23:30 +02:00
|
|
|
//! use rtfm::{C0, C1, C16, Local, P0, P1};
|
2017-04-21 07:24:54 +02:00
|
|
|
//!
|
|
|
|
//! // INITIALIZATION PHASE
|
|
|
|
//! fn init(_priority: P0, _ceiling: &C16) {
|
|
|
|
//! // Configure TIM7 for periodic interrupts
|
|
|
|
//! // Configure GPIO for LED driving
|
|
|
|
//! }
|
|
|
|
//!
|
|
|
|
//! // IDLE LOOP
|
2017-04-28 16:23:30 +02:00
|
|
|
//! fn idle(_priority: P0, _ceiling: C0) -> ! {
|
2017-04-21 07:24:54 +02:00
|
|
|
//! // Sleep
|
2017-04-26 04:51:07 +02:00
|
|
|
//! loop {
|
|
|
|
//! rtfm::wfi();
|
|
|
|
//! }
|
2017-04-21 07:24:54 +02:00
|
|
|
//! }
|
|
|
|
//!
|
|
|
|
//! // TASKS
|
2017-04-25 16:23:27 +02:00
|
|
|
//! tasks!(stm32f30x, {
|
2017-04-25 17:24:17 +02:00
|
|
|
//! periodic: Task {
|
|
|
|
//! interrupt: Tim7,
|
|
|
|
//! priority: P1,
|
|
|
|
//! enabled: true,
|
|
|
|
//! },
|
2017-04-21 07:24:54 +02:00
|
|
|
//! });
|
|
|
|
//!
|
2017-04-28 16:23:30 +02:00
|
|
|
//! fn periodic(mut task: Tim7, _priority: P1, _ceiling: C1) {
|
2017-04-21 07:24:54 +02:00
|
|
|
//! // Task local data
|
2017-04-25 16:23:27 +02:00
|
|
|
//! static STATE: Local<bool, Tim7> = Local::new(false);
|
2017-04-21 07:24:54 +02:00
|
|
|
//!
|
2017-04-22 05:45:23 +02:00
|
|
|
//! let state = STATE.borrow_mut(&mut task);
|
2017-04-21 07:24:54 +02:00
|
|
|
//!
|
|
|
|
//! // Toggle state
|
2017-04-22 05:45:23 +02:00
|
|
|
//! *state = !*state;
|
2017-04-21 07:24:54 +02:00
|
|
|
//!
|
|
|
|
//! // Blink an LED
|
2017-04-22 05:45:23 +02:00
|
|
|
//! if *state {
|
2017-04-21 07:24:54 +02:00
|
|
|
//! LED.on();
|
|
|
|
//! } else {
|
|
|
|
//! LED.off();
|
|
|
|
//! }
|
|
|
|
//! }
|
|
|
|
//! ```
|
|
|
|
//!
|
2017-04-25 16:23:27 +02:00
|
|
|
//! Here we define a task named `periodic` and bind it to the `Tim7`
|
|
|
|
//! interrupt. The `periodic` task will run every time the `Tim7` interrupt
|
2017-04-22 05:45:23 +02:00
|
|
|
//! is triggered. We assign to this task a priority of 1 (`P1`); this is the
|
2017-04-21 07:24:54 +02:00
|
|
|
//! lowest priority that a task can have.
|
|
|
|
//!
|
|
|
|
//! We use the [`Local`](./struct.Local.html) abstraction to add state to the
|
|
|
|
//! task; this task local data will be preserved across runs of the `periodic`
|
|
|
|
//! task. Note that `STATE` is owned by the `periodic` task, in the sense that
|
|
|
|
//! no other task can access it; this is reflected in its type signature (the
|
2017-04-25 16:23:27 +02:00
|
|
|
//! `Tim7` type parameter).
|
2017-04-21 07:24:54 +02:00
|
|
|
//!
|
|
|
|
//! # Two "serial" tasks
|
|
|
|
//!
|
|
|
|
//! ``` ignore
|
2017-04-26 04:51:07 +02:00
|
|
|
//! #![feature(const_fn)]
|
|
|
|
//! #![feature(used)]
|
2017-04-21 07:24:54 +02:00
|
|
|
//! #![no_std]
|
|
|
|
//!
|
|
|
|
//! extern crate cortex_m_rt;
|
|
|
|
//! #[macro_use]
|
|
|
|
//! extern crate cortex_m_rtfm as rtfm;
|
2017-04-25 16:23:27 +02:00
|
|
|
//! extern crate stm32f30x;
|
2017-04-21 07:24:54 +02:00
|
|
|
//!
|
|
|
|
//! use core::cell::Cell;
|
|
|
|
//!
|
2017-04-25 16:23:27 +02:00
|
|
|
//! use stm32f30x::interrupt::{Tim6Dacunder, Tim7};
|
2017-04-28 16:23:30 +02:00
|
|
|
//! use rtfm::{C0, C1, C16, P0, P1, Resource};
|
2017-04-21 07:24:54 +02:00
|
|
|
//!
|
|
|
|
//! // omitted: `idle`, `init`
|
|
|
|
//!
|
2017-04-25 16:23:27 +02:00
|
|
|
//! tasks!(stm32f30x, {
|
2017-04-25 17:24:17 +02:00
|
|
|
//! t1: Task {
|
|
|
|
//! interrupt: Tim6Dacunder,
|
|
|
|
//! priority: P1,
|
|
|
|
//! enabled: true,
|
|
|
|
//! },
|
|
|
|
//! t2: Task {
|
|
|
|
//! interrupt: Tim7,
|
|
|
|
//! priority: P1,
|
|
|
|
//! enabled: true,
|
|
|
|
//! },
|
2017-04-21 07:24:54 +02:00
|
|
|
//! });
|
|
|
|
//!
|
|
|
|
//! // Data shared between tasks `t1` and `t2`
|
|
|
|
//! static COUNTER: Resource<Cell<u32>, C1> = Resource::new(Cell::new(0));
|
|
|
|
//!
|
2017-04-26 04:51:07 +02:00
|
|
|
//! fn init(priority: P0, ceiling: &C16) {
|
|
|
|
//! // ..
|
|
|
|
//! }
|
|
|
|
//!
|
2017-04-28 16:23:30 +02:00
|
|
|
//! fn idle(priority: P0, ceiling: C0) -> ! {
|
2017-04-26 04:51:07 +02:00
|
|
|
//! // Sleep
|
|
|
|
//! loop {
|
|
|
|
//! rtfm::wfi();
|
|
|
|
//! }
|
|
|
|
//! }
|
|
|
|
//!
|
2017-04-28 16:23:30 +02:00
|
|
|
//! fn t1(_task: Tim6Dacunder, priority: P1, ceiling: C1) {
|
|
|
|
//! let counter = COUNTER.access(&priority, &ceiling);
|
2017-04-21 07:24:54 +02:00
|
|
|
//!
|
|
|
|
//! counter.set(counter.get() + 1);
|
|
|
|
//! }
|
|
|
|
//!
|
2017-04-28 16:23:30 +02:00
|
|
|
//! fn t2(_task: Tim7, priority: P1, ceiling: C1) {
|
|
|
|
//! let counter = COUNTER.access(&priority, &ceiling);
|
2017-04-21 07:24:54 +02:00
|
|
|
//!
|
|
|
|
//! counter.set(counter.get() + 2);
|
|
|
|
//! }
|
|
|
|
//! ```
|
|
|
|
//!
|
|
|
|
//! Here we declare two tasks, `t1` and `t2`; both with a priority of 1 (`P1`).
|
|
|
|
//! As both tasks have the same priority, we say that they are *serial* tasks in
|
|
|
|
//! the sense that `t1` can only run *after* `t2` is done and vice versa; i.e.
|
2017-04-26 04:51:07 +02:00
|
|
|
//! no preemption between them is possible.
|
2017-04-21 07:24:54 +02:00
|
|
|
//!
|
|
|
|
//! To share data between these two tasks, we use the
|
|
|
|
//! [`Resource`](./struct.Resource.html) abstraction. As the tasks can't preempt
|
|
|
|
//! each other, they can access the `COUNTER` resource using the zero cost
|
2017-04-22 05:45:23 +02:00
|
|
|
//! [`access`](./struct.Resource.html#method.access) method -- no
|
2017-04-26 04:51:07 +02:00
|
|
|
//! synchronization is required.
|
2017-04-21 07:24:54 +02:00
|
|
|
//!
|
|
|
|
//! `COUNTER` has an extra type parameter: `C1`. This is the *ceiling* of the
|
|
|
|
//! resource. For now suffices to say that the ceiling must be the maximum of
|
|
|
|
//! the priorities of all the tasks that access the resource -- in this case,
|
|
|
|
//! `C1 == max(P1, P1)`. If you try a smaller value like `C0`, you'll find out
|
|
|
|
//! that your program doesn't compile.
|
|
|
|
//!
|
|
|
|
//! # Preemptive multitasking
|
|
|
|
//!
|
|
|
|
//! ``` ignore
|
2017-04-26 04:51:07 +02:00
|
|
|
//! #![feature(const_fn)]
|
|
|
|
//! #![feature(used)]
|
2017-04-21 07:24:54 +02:00
|
|
|
//! #![no_std]
|
|
|
|
//!
|
|
|
|
//! extern crate cortex_m_rt;
|
|
|
|
//! #[macro_use]
|
|
|
|
//! extern crate cortex_m_rtfm as rtfm;
|
2017-04-25 16:23:27 +02:00
|
|
|
//! extern crate stm32f30x;
|
2017-04-21 07:24:54 +02:00
|
|
|
//!
|
|
|
|
//! use core::cell::Cell;
|
|
|
|
//!
|
2017-04-25 16:23:27 +02:00
|
|
|
//! use stm32f30x::interrupt::{Tim6Dacunder, Tim7};
|
2017-04-28 16:23:30 +02:00
|
|
|
//! use rtfm::{C0, C1, C16, C2, P0, P1, P2, Resource};
|
2017-04-21 07:24:54 +02:00
|
|
|
//!
|
2017-04-25 16:23:27 +02:00
|
|
|
//! tasks!(stm32f30x, {
|
2017-04-25 17:24:17 +02:00
|
|
|
//! t1: Task {
|
|
|
|
//! interrupt: Tim6Dacunder,
|
|
|
|
//! priority: P1,
|
|
|
|
//! enabled: true,
|
|
|
|
//! },
|
|
|
|
//! t2: Task {
|
|
|
|
//! interrupt: Tim7,
|
|
|
|
//! priority: P2,
|
|
|
|
//! enabled: true,
|
|
|
|
//! },
|
2017-04-21 07:24:54 +02:00
|
|
|
//! });
|
|
|
|
//!
|
|
|
|
//! static COUNTER: Resource<Cell<u32>, C2> = Resource::new(Cell::new(0));
|
|
|
|
//!
|
2017-04-26 04:51:07 +02:00
|
|
|
//! fn init(priority: P0, ceiling: &C16) {
|
|
|
|
//! // ..
|
|
|
|
//! }
|
|
|
|
//!
|
2017-04-28 16:23:30 +02:00
|
|
|
//! fn idle(priority: P0, ceiling: C0) -> ! {
|
2017-04-26 04:51:07 +02:00
|
|
|
//! // Sleep
|
|
|
|
//! loop {
|
|
|
|
//! rtfm::wfi();
|
|
|
|
//! }
|
|
|
|
//! }
|
|
|
|
//!
|
2017-04-28 16:23:30 +02:00
|
|
|
//! fn t1(_task: Tim6Dacunder, priority: P1, ceiling: C1) {
|
2017-04-21 07:24:54 +02:00
|
|
|
//! // ..
|
|
|
|
//!
|
2017-04-26 04:51:07 +02:00
|
|
|
//! ceiling.raise(
|
|
|
|
//! &COUNTER, |ceiling: &C2| {
|
|
|
|
//! let counter = COUNTER.access(&priority, ceiling);
|
2017-04-22 05:45:23 +02:00
|
|
|
//!
|
2017-04-26 04:51:07 +02:00
|
|
|
//! counter.set(counter.get() + 1);
|
|
|
|
//! }
|
|
|
|
//! );
|
2017-04-21 07:24:54 +02:00
|
|
|
//!
|
|
|
|
//! // ..
|
|
|
|
//! }
|
|
|
|
//!
|
2017-04-28 16:23:30 +02:00
|
|
|
//! fn t2(_task: Tim7, priority: P2, ceiling: C2) {
|
|
|
|
//! let counter = COUNTER.access(&priority, &ceiling);
|
2017-04-21 07:24:54 +02:00
|
|
|
//!
|
|
|
|
//! counter.set(counter.get() + 2);
|
|
|
|
//! }
|
|
|
|
//! ```
|
|
|
|
//!
|
|
|
|
//! Now we have a variation of the previous example. Like before, `t1` has a
|
|
|
|
//! priority of 1 (`P1`) but `t2` now has a priority of 2 (`P2`). This means
|
2017-04-25 16:23:27 +02:00
|
|
|
//! that `t2` can preempt `t1` if a `Tim7` interrupt occurs while `t1` is
|
2017-04-21 07:24:54 +02:00
|
|
|
//! being executed.
|
|
|
|
//!
|
|
|
|
//! To avoid data races, `t1` must modify `COUNTER` in an atomic way; i.e. `t2`
|
|
|
|
//! most not preempt `t1` while `COUNTER` is being modified. This is
|
2017-04-22 05:45:23 +02:00
|
|
|
//! accomplished by [`raise`](./struct.C.html#method.raise)-ing the `ceiling`.
|
2017-04-26 04:51:07 +02:00
|
|
|
//! This creates a critical section, denoted by a closure; for whose execution,
|
2017-04-21 07:24:54 +02:00
|
|
|
//! `COUNTER` is accessible but `t2` is blocked from preempting `t1`.
|
|
|
|
//!
|
|
|
|
//! How `t2` accesses `COUNTER` remains unchanged. Since `t1` can't preempt `t2`
|
2017-04-22 05:45:23 +02:00
|
|
|
//! due to the differences in priority; no critical section is needed in `t2`.
|
2017-04-21 07:24:54 +02:00
|
|
|
//!
|
2017-04-26 04:51:07 +02:00
|
|
|
//! Note that the ceiling of `COUNTER` had to be changed to `C2`. This is
|
|
|
|
//! required because the ceiling must be the maximum between `P1` and `P2`.
|
2017-04-21 07:24:54 +02:00
|
|
|
//!
|
2017-04-22 05:45:23 +02:00
|
|
|
//! Finally, it should be noted that the critical section in `t1` will only
|
|
|
|
//! block tasks with a priority of 2 or lower. This is exactly what the ceiling
|
|
|
|
//! represents: it's the "bar" that a task priority must pass in order to be
|
2017-04-26 04:51:07 +02:00
|
|
|
//! able to preempt the current task / critical section. Note that a task with
|
|
|
|
//! e.g. a priority of 3 (`P3`) effectively imposes a ceiling of 3 (`C3`)
|
|
|
|
//! because only other task with a priority of 4 or greater can preempt it.
|
2017-04-21 07:24:54 +02:00
|
|
|
//!
|
|
|
|
//! # Peripherals as resources
|
|
|
|
//!
|
|
|
|
//! ``` ignore
|
2017-04-26 04:51:07 +02:00
|
|
|
//! #![feature(const_fn)]
|
|
|
|
//! #![feature(used)]
|
2017-04-21 07:24:54 +02:00
|
|
|
//! #![no_std]
|
|
|
|
//!
|
|
|
|
//! extern crate cortex_m_rt;
|
|
|
|
//! #[macro_use]
|
|
|
|
//! extern crate cortex_m_rtfm as rtfm;
|
2017-04-26 04:51:07 +02:00
|
|
|
//! extern crate stm32f30x;
|
2017-04-21 07:24:54 +02:00
|
|
|
//!
|
|
|
|
//! use rtfm::{C0, C16, P0, Peripheral};
|
|
|
|
//!
|
2017-04-26 04:51:07 +02:00
|
|
|
//! peripherals!(stm32f30x, {
|
|
|
|
//! GPIOA: Peripheral {
|
|
|
|
//! register_block: Gpioa,
|
|
|
|
//! ceiling: C0,
|
|
|
|
//! },
|
|
|
|
//! RCC: Peripheral {
|
|
|
|
//! register_block: Rcc,
|
|
|
|
//! ceiling: C0,
|
|
|
|
//! },
|
|
|
|
//! });
|
2017-04-21 07:24:54 +02:00
|
|
|
//!
|
2017-04-26 04:51:07 +02:00
|
|
|
//! tasks!(stm32f30x, {});
|
2017-04-21 07:24:54 +02:00
|
|
|
//!
|
|
|
|
//! fn init(priority: P0, ceiling: &C16) {
|
2017-04-22 05:45:23 +02:00
|
|
|
//! let gpioa = GPIOA.access(&priority, &ceiling);
|
|
|
|
//! let rcc = RCC.access(&priority, &ceiling);
|
2017-04-21 07:24:54 +02:00
|
|
|
//!
|
|
|
|
//! // ..
|
|
|
|
//! }
|
|
|
|
//!
|
|
|
|
//! fn idle(_priority: P0) -> ! {
|
|
|
|
//! // Sleep
|
2017-04-26 04:51:07 +02:00
|
|
|
//! loop {
|
|
|
|
//! rtfm::wfi();
|
|
|
|
//! }
|
2017-04-21 07:24:54 +02:00
|
|
|
//! }
|
|
|
|
//! ```
|
|
|
|
//!
|
|
|
|
//! Peripherals are global resources too and as such they can be protected in
|
|
|
|
//! the same way as `Resource`s using the
|
|
|
|
//! [`Peripheral`](./struct.Peripheral.html) abstraction.
|
|
|
|
//!
|
|
|
|
//! `Peripheral` and `Resource` has pretty much the same API except that
|
2017-04-26 04:51:07 +02:00
|
|
|
//! `Peripheral` instances must be declared using the
|
|
|
|
//! [`peripherals!`](./macro.peripherals.html) macro.
|
2017-04-21 07:24:54 +02:00
|
|
|
//!
|
|
|
|
//! # References
|
|
|
|
//!
|
|
|
|
//! - Baker, T. P. (1991). Stack-based scheduling of realtime processes.
|
|
|
|
//! *Real-Time Systems*, 3(1), 67-99.
|
|
|
|
//!
|
2017-04-21 22:31:02 +02:00
|
|
|
//! > The original Stack Resource Policy paper. [PDF].
|
2017-04-21 07:24:54 +02:00
|
|
|
//!
|
|
|
|
//! [PDF]: http://www.cs.fsu.edu/~baker/papers/mstacks3.pdf
|
|
|
|
//!
|
|
|
|
//! - Eriksson, J., Häggström, F., Aittamaa, S., Kruglyak, A., & Lindgren, P.
|
|
|
|
//! (2013, June). Real-time for the masses, step 1: Programming API and static
|
|
|
|
//! priority SRP kernel primitives. In Industrial Embedded Systems (SIES),
|
|
|
|
//! 2013 8th IEEE International Symposium on (pp. 110-113). IEEE.
|
|
|
|
//!
|
|
|
|
//! > A description of the RTFM task and resource model. [PDF]
|
|
|
|
//!
|
|
|
|
//! [PDF]: http://www.diva-portal.org/smash/get/diva2:1005680/FULLTEXT01.pdf
|
2017-04-14 07:33:07 +02:00
|
|
|
|
|
|
|
#![deny(missing_docs)]
|
2017-03-08 14:10:58 +01:00
|
|
|
#![deny(warnings)]
|
2017-03-05 06:26:14 +01:00
|
|
|
#![feature(asm)]
|
|
|
|
#![feature(const_fn)]
|
|
|
|
#![no_std]
|
|
|
|
|
|
|
|
extern crate cortex_m;
|
2017-04-14 16:45:50 +02:00
|
|
|
extern crate static_ref;
|
2017-04-10 05:42:17 +02:00
|
|
|
extern crate typenum;
|
2017-03-05 06:26:14 +01:00
|
|
|
|
2017-04-10 05:42:17 +02:00
|
|
|
use core::cell::UnsafeCell;
|
2017-04-12 22:27:48 +02:00
|
|
|
use core::marker::PhantomData;
|
2017-04-28 02:06:22 +02:00
|
|
|
use core::ptr;
|
2017-04-03 23:18:26 +02:00
|
|
|
|
2017-04-14 02:01:46 +02:00
|
|
|
use cortex_m::ctxt::Context;
|
2017-04-10 05:42:17 +02:00
|
|
|
use cortex_m::interrupt::Nr;
|
2017-04-12 06:15:05 +02:00
|
|
|
#[cfg(not(thumbv6m))]
|
2017-04-10 05:42:17 +02:00
|
|
|
use cortex_m::register::{basepri, basepri_max};
|
2017-04-19 23:35:07 +02:00
|
|
|
use static_ref::Ref;
|
2017-04-21 22:31:02 +02:00
|
|
|
use typenum::{Cmp, Greater, U0, Unsigned};
|
2017-04-12 06:15:05 +02:00
|
|
|
#[cfg(not(thumbv6m))]
|
2017-04-21 22:31:02 +02:00
|
|
|
use typenum::Less;
|
2017-04-03 23:18:26 +02:00
|
|
|
|
2017-04-14 17:05:24 +02:00
|
|
|
pub use cortex_m::asm::{bkpt, wfi};
|
2017-04-14 02:01:46 +02:00
|
|
|
|
2017-04-10 05:42:17 +02:00
|
|
|
#[doc(hidden)]
|
2017-04-21 07:24:54 +02:00
|
|
|
pub use cortex_m::peripheral::NVIC as _NVIC;
|
2017-03-05 06:26:14 +01:00
|
|
|
|
2017-04-21 07:24:54 +02:00
|
|
|
/// Compiler barrier
|
2017-04-10 05:42:17 +02:00
|
|
|
macro_rules! barrier {
|
|
|
|
() => {
|
2017-04-08 01:14:48 +02:00
|
|
|
asm!(""
|
|
|
|
:
|
|
|
|
:
|
|
|
|
: "memory"
|
|
|
|
: "volatile");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-04-21 07:24:54 +02:00
|
|
|
/// Task local data
|
|
|
|
///
|
|
|
|
/// This data can only be accessed by the task `T`
|
|
|
|
pub struct Local<D, T> {
|
|
|
|
_task: PhantomData<T>,
|
|
|
|
data: UnsafeCell<D>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<T, TASK> Local<T, TASK> {
|
|
|
|
/// Creates a task local variable with some initial `value`
|
|
|
|
pub const fn new(value: T) -> Self {
|
|
|
|
Local {
|
|
|
|
_task: PhantomData,
|
|
|
|
data: UnsafeCell::new(value),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Borrows the task local data for the duration of the task
|
|
|
|
pub fn borrow<'task>(&'static self, _task: &'task TASK) -> &'task T {
|
|
|
|
unsafe { &*self.data.get() }
|
2017-04-22 04:25:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Mutably borrows the task local data for the duration of the task
|
|
|
|
pub fn borrow_mut<'task>(
|
|
|
|
&'static self,
|
|
|
|
_task: &'task mut TASK,
|
|
|
|
) -> &'task mut T {
|
|
|
|
unsafe { &mut *self.data.get() }
|
2017-04-21 07:24:54 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
unsafe impl<T, TASK> Sync for Local<T, TASK> {}
|
|
|
|
|
|
|
|
/// A resource with ceiling `C`
|
|
|
|
pub struct Resource<T, C> {
|
|
|
|
_ceiling: PhantomData<C>,
|
2017-04-10 05:42:17 +02:00
|
|
|
data: UnsafeCell<T>,
|
2017-03-05 06:26:14 +01:00
|
|
|
}
|
|
|
|
|
2017-04-22 04:38:39 +02:00
|
|
|
impl<T, RC> Resource<T, C<RC>>
|
2017-04-21 07:24:54 +02:00
|
|
|
where
|
2017-04-22 04:38:39 +02:00
|
|
|
RC: GreaterThanOrEqual<U0>,
|
|
|
|
RC: LessThanOrEqual<UMAX>,
|
2017-04-21 07:24:54 +02:00
|
|
|
{
|
2017-04-22 04:38:39 +02:00
|
|
|
/// Creates a new resource
|
2017-04-21 07:24:54 +02:00
|
|
|
pub const fn new(data: T) -> Self {
|
2017-04-10 05:42:17 +02:00
|
|
|
Resource {
|
|
|
|
_ceiling: PhantomData,
|
|
|
|
data: UnsafeCell::new(data),
|
2017-03-05 06:26:14 +01:00
|
|
|
}
|
|
|
|
}
|
2017-03-10 05:59:50 +01:00
|
|
|
}
|
2017-03-05 06:26:14 +01:00
|
|
|
|
2017-04-22 04:38:39 +02:00
|
|
|
impl<T, RC> Resource<T, C<RC>> {
|
|
|
|
/// Grants data race free and deadlock free access to the resource data
|
2017-03-11 22:12:58 +01:00
|
|
|
///
|
2017-04-21 07:24:54 +02:00
|
|
|
/// This operation is zero cost and doesn't impose any additional blocking.
|
|
|
|
///
|
2017-04-22 04:38:39 +02:00
|
|
|
/// # Requirements
|
|
|
|
///
|
|
|
|
/// To access the resource data these conditions must be met:
|
|
|
|
///
|
|
|
|
/// - The resource ceiling must be greater than or equal to the task
|
|
|
|
/// priority
|
|
|
|
/// - The system ceiling must be greater than or equal to the resource
|
|
|
|
/// ceiling
|
|
|
|
pub fn access<'cs, TP, SC>(
|
2017-04-17 18:59:56 +02:00
|
|
|
&'static self,
|
2017-04-22 04:38:39 +02:00
|
|
|
_priority: &P<TP>,
|
|
|
|
_current_ceiling: &'cs C<SC>,
|
2017-04-17 18:59:56 +02:00
|
|
|
) -> Ref<'cs, T>
|
|
|
|
where
|
2017-04-22 04:38:39 +02:00
|
|
|
RC: GreaterThanOrEqual<TP>,
|
|
|
|
SC: GreaterThanOrEqual<RC>,
|
2017-03-05 06:26:14 +01:00
|
|
|
{
|
2017-04-14 16:45:50 +02:00
|
|
|
unsafe { Ref::new(&*self.data.get()) }
|
2017-04-04 23:37:01 +02:00
|
|
|
}
|
2017-04-10 05:42:17 +02:00
|
|
|
}
|
2017-03-22 20:33:52 +01:00
|
|
|
|
2017-04-21 07:24:54 +02:00
|
|
|
unsafe impl<T, C> Sync for Resource<T, C> {}
|
2017-03-22 20:33:52 +01:00
|
|
|
|
2017-04-10 05:42:17 +02:00
|
|
|
/// A hardware peripheral as a resource
|
2017-04-26 02:46:33 +02:00
|
|
|
///
|
|
|
|
/// To assign a ceiling to a peripheral, use the
|
|
|
|
/// [`peripherals!`](./macro.peripherals.html) macro
|
|
|
|
pub struct Peripheral<P, PC>
|
2017-04-17 18:59:56 +02:00
|
|
|
where
|
|
|
|
P: 'static,
|
2017-04-10 05:42:17 +02:00
|
|
|
{
|
|
|
|
peripheral: cortex_m::peripheral::Peripheral<P>,
|
2017-04-26 02:46:33 +02:00
|
|
|
_ceiling: PhantomData<PC>,
|
2017-03-10 05:59:50 +01:00
|
|
|
}
|
2017-03-05 06:26:14 +01:00
|
|
|
|
2017-04-21 07:24:54 +02:00
|
|
|
impl<P, CEILING> Peripheral<P, C<CEILING>>
|
2017-04-17 18:59:56 +02:00
|
|
|
where
|
2017-04-21 07:24:54 +02:00
|
|
|
CEILING: GreaterThanOrEqual<U0>,
|
|
|
|
CEILING: LessThanOrEqual<UMAX>,
|
2017-03-10 05:59:50 +01:00
|
|
|
{
|
2017-04-26 02:46:33 +02:00
|
|
|
#[doc(hidden)]
|
|
|
|
pub const unsafe fn _new(peripheral: cortex_m::peripheral::Peripheral<P>,)
|
2017-04-17 18:59:56 +02:00
|
|
|
-> Self {
|
2017-04-10 05:42:17 +02:00
|
|
|
Peripheral {
|
|
|
|
_ceiling: PhantomData,
|
|
|
|
peripheral: peripheral,
|
|
|
|
}
|
2017-04-03 03:42:38 +02:00
|
|
|
}
|
2017-04-10 05:42:17 +02:00
|
|
|
}
|
2017-04-03 03:42:38 +02:00
|
|
|
|
2017-04-22 04:38:39 +02:00
|
|
|
impl<Periph, RC> Peripheral<Periph, C<RC>> {
|
|
|
|
/// See [Resource.access](./struct.Resource.html#method.access)
|
|
|
|
pub fn access<'cs, TP, SC>(
|
2017-04-17 18:59:56 +02:00
|
|
|
&'static self,
|
2017-04-22 04:38:39 +02:00
|
|
|
_priority: &P<TP>,
|
|
|
|
_system_ceiling: &'cs C<SC>,
|
2017-04-17 18:59:56 +02:00
|
|
|
) -> Ref<'cs, Periph>
|
|
|
|
where
|
2017-04-22 04:38:39 +02:00
|
|
|
RC: GreaterThanOrEqual<TP>,
|
|
|
|
SC: GreaterThanOrEqual<RC>,
|
2017-04-03 03:42:38 +02:00
|
|
|
{
|
2017-04-14 16:45:50 +02:00
|
|
|
unsafe { Ref::new(&*self.peripheral.get()) }
|
2017-04-03 03:42:38 +02:00
|
|
|
}
|
2017-03-05 06:26:14 +01:00
|
|
|
}
|
|
|
|
|
2017-04-21 07:24:54 +02:00
|
|
|
unsafe impl<T, C> Sync for Peripheral<T, C> {}
|
2017-04-10 05:42:17 +02:00
|
|
|
|
2017-04-26 04:51:07 +02:00
|
|
|
/// Runs the closure `f` "atomically"
|
2017-04-14 06:52:02 +02:00
|
|
|
///
|
2017-04-26 04:51:07 +02:00
|
|
|
/// No task can preempt the execution of the closure
|
|
|
|
pub fn atomic<R, F>(f: F) -> R
|
2017-04-17 18:59:56 +02:00
|
|
|
where
|
2017-04-19 22:03:49 +02:00
|
|
|
F: FnOnce(&CMAX) -> R,
|
2017-04-14 06:52:02 +02:00
|
|
|
{
|
|
|
|
let primask = ::cortex_m::register::primask::read();
|
|
|
|
::cortex_m::interrupt::disable();
|
|
|
|
|
2017-04-19 22:03:49 +02:00
|
|
|
let r = f(&C { _marker: PhantomData });
|
2017-04-14 06:52:02 +02:00
|
|
|
|
|
|
|
// If the interrupts were active before our `disable` call, then re-enable
|
|
|
|
// them. Otherwise, keep them disabled
|
|
|
|
if primask.is_active() {
|
|
|
|
::cortex_m::interrupt::enable();
|
|
|
|
}
|
|
|
|
|
|
|
|
r
|
|
|
|
}
|
|
|
|
|
2017-04-26 04:51:07 +02:00
|
|
|
/// Disables a `task`
|
2017-04-22 05:02:49 +02:00
|
|
|
///
|
|
|
|
/// The task won't run even if the underlying interrupt is raised
|
2017-04-28 02:06:22 +02:00
|
|
|
pub fn disable<T, TP>(_task: fn(T, P<TP>, C<TP>))
|
2017-04-26 02:46:33 +02:00
|
|
|
where
|
2017-04-22 05:02:49 +02:00
|
|
|
T: Context + Nr,
|
|
|
|
{
|
|
|
|
// NOTE(safe) zero sized type
|
|
|
|
let _task = unsafe { ptr::read(0x0 as *const T) };
|
|
|
|
|
|
|
|
// NOTE(safe) atomic write
|
|
|
|
unsafe { (*_NVIC.get()).disable(_task) }
|
|
|
|
}
|
|
|
|
|
2017-04-26 04:51:07 +02:00
|
|
|
/// Enables a `task`
|
2017-04-28 02:06:22 +02:00
|
|
|
pub fn enable<T, TP>(_task: fn(T, P<TP>, C<TP>))
|
2017-04-22 05:02:49 +02:00
|
|
|
where
|
|
|
|
T: Context + Nr,
|
|
|
|
{
|
|
|
|
// NOTE(safe) zero sized type
|
|
|
|
let _task = unsafe { ptr::read(0x0 as *const T) };
|
|
|
|
|
|
|
|
// NOTE(safe) atomic write
|
|
|
|
unsafe { (*_NVIC.get()).enable(_task) }
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Converts a shifted hardware priority into a logical priority
|
|
|
|
pub fn hw2logical(hw: u8) -> u8 {
|
|
|
|
(1 << PRIORITY_BITS) - (hw >> (8 - PRIORITY_BITS))
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Converts a logical priority into a shifted hardware priority, as used by the
|
|
|
|
/// NVIC and the BASEPRI register
|
|
|
|
///
|
|
|
|
/// # Panics
|
|
|
|
///
|
|
|
|
/// This function panics if `logical` is outside the closed range
|
|
|
|
/// `[1, 1 << PRIORITY_BITS]`. Where `PRIORITY_BITS` is the number of priority
|
|
|
|
/// bits used by the device specific NVIC implementation.
|
|
|
|
pub fn logical2hw(logical: u8) -> u8 {
|
|
|
|
assert!(logical >= 1 && logical <= (1 << PRIORITY_BITS));
|
|
|
|
|
|
|
|
((1 << PRIORITY_BITS) - logical) << (8 - PRIORITY_BITS)
|
|
|
|
}
|
|
|
|
|
2017-04-14 07:33:07 +02:00
|
|
|
/// Requests the execution of a `task`
|
2017-04-28 02:06:22 +02:00
|
|
|
pub fn request<T, TP>(_task: fn(T, P<TP>, C<TP>))
|
2017-04-17 18:59:56 +02:00
|
|
|
where
|
|
|
|
T: Context + Nr,
|
2017-04-10 05:42:17 +02:00
|
|
|
{
|
2017-04-21 07:24:54 +02:00
|
|
|
let nvic = unsafe { &*_NVIC.get() };
|
2017-04-10 05:42:17 +02:00
|
|
|
|
|
|
|
match () {
|
|
|
|
#[cfg(debug_assertions)]
|
|
|
|
() => {
|
2017-04-10 06:18:00 +02:00
|
|
|
// NOTE(safe) zero sized type
|
|
|
|
let task = unsafe { core::ptr::read(0x0 as *const T) };
|
2017-04-10 05:42:17 +02:00
|
|
|
// NOTE(safe) atomic read
|
|
|
|
assert!(!nvic.is_pending(task),
|
|
|
|
"Task is already in the pending state");
|
|
|
|
}
|
|
|
|
#[cfg(not(debug_assertions))]
|
|
|
|
() => {}
|
|
|
|
}
|
2017-03-05 06:26:14 +01:00
|
|
|
|
2017-04-10 06:18:00 +02:00
|
|
|
// NOTE(safe) zero sized type
|
|
|
|
let task = unsafe { core::ptr::read(0x0 as *const T) };
|
2017-04-21 07:24:54 +02:00
|
|
|
|
2017-04-10 05:42:17 +02:00
|
|
|
// NOTE(safe) atomic write
|
|
|
|
nvic.set_pending(task);
|
2017-04-03 23:18:26 +02:00
|
|
|
}
|
|
|
|
|
2017-04-21 07:24:54 +02:00
|
|
|
#[doc(hidden)]
|
2017-04-22 05:02:49 +02:00
|
|
|
pub fn _validate_priority<TP>(_: &P<TP>)
|
2017-04-21 07:24:54 +02:00
|
|
|
where
|
2017-04-22 05:02:49 +02:00
|
|
|
TP: Cmp<U0, Output = Greater> + LessThanOrEqual<UMAX>,
|
2017-04-21 07:24:54 +02:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2017-04-10 05:42:17 +02:00
|
|
|
/// A type-level ceiling
|
|
|
|
pub struct C<T> {
|
|
|
|
_marker: PhantomData<T>,
|
|
|
|
}
|
2017-03-08 14:10:58 +01:00
|
|
|
|
2017-04-22 04:38:39 +02:00
|
|
|
impl<SC> C<SC> {
|
|
|
|
/// Raises the system ceiling to match the `resource` ceiling
|
2017-04-25 18:09:51 +02:00
|
|
|
#[cfg(not(thumbv6m))]
|
2017-04-22 04:38:39 +02:00
|
|
|
pub fn raise<RC, RES, R, F>(&self, _resource: &'static RES, f: F) -> R
|
2017-04-21 22:41:03 +02:00
|
|
|
where
|
2017-04-22 04:38:39 +02:00
|
|
|
RES: ResourceLike<Ceiling = RC>,
|
|
|
|
RC: Cmp<SC, Output = Greater> + Cmp<UMAX, Output = Less> + Unsigned,
|
|
|
|
F: FnOnce(&C<RC>) -> R,
|
2017-04-21 22:41:03 +02:00
|
|
|
{
|
|
|
|
unsafe {
|
|
|
|
let old_basepri = basepri::read();
|
2017-04-22 04:38:39 +02:00
|
|
|
basepri_max::write(logical2hw(RC::to_u8()));
|
2017-04-21 22:41:03 +02:00
|
|
|
barrier!();
|
|
|
|
let ret = f(&C { _marker: PhantomData });
|
|
|
|
barrier!();
|
|
|
|
basepri::write(old_basepri);
|
|
|
|
ret
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-04-10 05:42:17 +02:00
|
|
|
/// A type-level priority
|
|
|
|
pub struct P<T> {
|
|
|
|
_marker: PhantomData<T>,
|
2017-03-05 06:26:14 +01:00
|
|
|
}
|
2017-03-10 05:59:50 +01:00
|
|
|
|
2017-04-10 05:42:17 +02:00
|
|
|
impl<T> P<T>
|
2017-04-17 18:59:56 +02:00
|
|
|
where
|
2017-04-21 07:24:54 +02:00
|
|
|
T: Unsigned,
|
2017-04-04 23:37:01 +02:00
|
|
|
{
|
2017-04-14 07:33:07 +02:00
|
|
|
#[doc(hidden)]
|
2017-04-21 07:24:54 +02:00
|
|
|
pub fn _hw() -> u8 {
|
|
|
|
logical2hw(T::to_u8())
|
2017-04-04 22:36:23 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-04-21 22:31:02 +02:00
|
|
|
/// Maps a `Resource` / `Peripheral` to its ceiling
|
|
|
|
///
|
|
|
|
/// Do not implement this trait yourself. This is an implementation detail.
|
|
|
|
pub unsafe trait ResourceLike {
|
|
|
|
/// The ceiling of the resource
|
|
|
|
type Ceiling;
|
|
|
|
}
|
|
|
|
|
2017-04-22 04:38:39 +02:00
|
|
|
unsafe impl<P, RC> ResourceLike for Peripheral<P, C<RC>> {
|
|
|
|
type Ceiling = RC;
|
2017-04-21 22:31:02 +02:00
|
|
|
}
|
|
|
|
|
2017-04-22 04:38:39 +02:00
|
|
|
unsafe impl<T, RC> ResourceLike for Resource<T, C<RC>> {
|
|
|
|
type Ceiling = RC;
|
2017-04-21 22:31:02 +02:00
|
|
|
}
|
|
|
|
|
2017-04-10 05:42:17 +02:00
|
|
|
/// Type-level `>=` operator
|
|
|
|
///
|
2017-04-21 07:24:54 +02:00
|
|
|
/// Do not implement this trait yourself. This is an implementation detail.
|
2017-04-10 05:42:17 +02:00
|
|
|
pub unsafe trait GreaterThanOrEqual<RHS> {}
|
|
|
|
|
2017-04-21 07:24:54 +02:00
|
|
|
/// Type-level `<=` operator
|
2017-04-10 05:42:17 +02:00
|
|
|
///
|
2017-04-21 07:24:54 +02:00
|
|
|
/// Do not implement this trait yourself. This is an implementation detail.
|
|
|
|
pub unsafe trait LessThanOrEqual<RHS> {}
|
2017-03-10 05:59:50 +01:00
|
|
|
|
2017-04-26 02:46:33 +02:00
|
|
|
/// A macro to assign ceilings to peripherals
|
|
|
|
///
|
|
|
|
/// **NOTE** A peripheral instance, like RCC, can only be bound to a *single*
|
|
|
|
/// ceiling. Trying to use this macro to bind the same peripheral to several
|
|
|
|
/// ceiling will result in a compiler error.
|
|
|
|
///
|
|
|
|
/// # Example
|
|
|
|
///
|
|
|
|
/// ``` ignore
|
|
|
|
/// #[macro_use]
|
|
|
|
/// extern crate cortex_m_rtfm;
|
|
|
|
/// // device crate generated using `svd2rust`
|
|
|
|
/// extern crate stm32f30x;
|
|
|
|
///
|
|
|
|
/// peripherals!(stm32f30x, {
|
|
|
|
/// GPIOA: Peripheral {
|
|
|
|
/// register_block: Gpioa,
|
|
|
|
/// ceiling: C1,
|
|
|
|
/// },
|
|
|
|
/// RCC: Peripheral {
|
|
|
|
/// register_block: Rcc,
|
|
|
|
/// ceiling: C0,
|
|
|
|
/// },
|
|
|
|
/// });
|
|
|
|
/// ```
|
2017-04-25 21:13:46 +02:00
|
|
|
#[macro_export]
|
|
|
|
macro_rules! peripherals {
|
|
|
|
($device:ident, {
|
|
|
|
$($PERIPHERAL:ident: Peripheral {
|
|
|
|
register_block: $RegisterBlock:ident,
|
|
|
|
ceiling: $C:ident,
|
|
|
|
},)+
|
|
|
|
}) => {
|
|
|
|
$(
|
2017-04-25 22:27:34 +02:00
|
|
|
#[allow(private_no_mangle_statics)]
|
2017-04-25 21:13:46 +02:00
|
|
|
#[no_mangle]
|
|
|
|
static $PERIPHERAL:
|
|
|
|
$crate::Peripheral<::$device::$RegisterBlock, $crate::$C> =
|
2017-04-26 02:46:33 +02:00
|
|
|
unsafe { $crate::Peripheral::_new(::$device::$PERIPHERAL) };
|
2017-04-25 21:13:46 +02:00
|
|
|
)+
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-04-21 07:24:54 +02:00
|
|
|
/// A macro to declare tasks
|
|
|
|
///
|
2017-04-22 05:02:49 +02:00
|
|
|
/// **NOTE** This macro will expand to a `main` function.
|
|
|
|
///
|
2017-04-21 07:24:54 +02:00
|
|
|
/// Each `$task` is bound to an `$Interrupt` handler and has a priority `$P`.
|
2017-04-26 02:46:33 +02:00
|
|
|
/// The minimum priority of a task is `P1`. `$enabled` indicates whether the
|
|
|
|
/// task will be enabled before `idle` runs.
|
2017-04-21 07:24:54 +02:00
|
|
|
///
|
|
|
|
/// The `$Interrupt` handlers are defined in the `$device` crate.
|
|
|
|
///
|
|
|
|
/// Apart from defining the listed `$tasks`, the `init` and `idle` functions
|
2017-04-26 02:46:33 +02:00
|
|
|
/// must be defined as well. `init` has signature `fn(P0, &C16)`, and `idle` has
|
|
|
|
/// signature `fn(P0) -> !`.
|
|
|
|
///
|
|
|
|
/// # Example
|
|
|
|
///
|
|
|
|
/// ``` ignore
|
|
|
|
/// #[macro_use]
|
|
|
|
/// extern crate cortex_m_rtfm as rtfm;
|
|
|
|
/// // device crate generated using `svd2rust`
|
|
|
|
/// extern crate stm32f30x;
|
|
|
|
///
|
|
|
|
/// use rtfm::{C16, P0, P1, P2};
|
|
|
|
/// use stm32f30x::interrupt::{Exti0, Tim7};
|
|
|
|
///
|
|
|
|
/// tasks!(stm32f30x, {
|
|
|
|
/// periodic: Task {
|
|
|
|
/// interrupt: Tim7,
|
|
|
|
/// priority: P1,
|
|
|
|
/// enabled: true,
|
|
|
|
/// },
|
|
|
|
/// button: Task {
|
|
|
|
/// interrupt: Exti0,
|
|
|
|
/// priority: P2,
|
|
|
|
/// enabled: true,
|
|
|
|
/// },
|
|
|
|
/// });
|
|
|
|
///
|
|
|
|
/// fn init(priority: P0, ceiling: C16) {
|
|
|
|
/// // ..
|
|
|
|
/// }
|
|
|
|
///
|
|
|
|
/// fn idle(priority: P0) -> ! {
|
|
|
|
/// // Sleep
|
2017-04-26 04:51:07 +02:00
|
|
|
/// loop {
|
|
|
|
/// rtfm::wfi();
|
|
|
|
/// }
|
2017-04-26 02:46:33 +02:00
|
|
|
/// }
|
|
|
|
///
|
|
|
|
/// // NOTE signature must match the tasks! declaration
|
|
|
|
/// fn periodic(task: Tim7, priority: P1) {
|
|
|
|
/// // ..
|
|
|
|
/// }
|
|
|
|
///
|
|
|
|
/// fn button(task: Exti0, priority: P2) {
|
|
|
|
/// // ..
|
|
|
|
/// }
|
|
|
|
/// ```
|
2017-04-10 05:42:17 +02:00
|
|
|
#[macro_export]
|
|
|
|
macro_rules! tasks {
|
2017-04-21 07:24:54 +02:00
|
|
|
($device:ident, {
|
2017-04-25 16:29:29 +02:00
|
|
|
$($task:ident: Task {
|
|
|
|
interrupt:$Interrupt:ident,
|
|
|
|
priority: $P:ident,
|
|
|
|
enabled: $enabled:expr,
|
|
|
|
},)*
|
2017-04-10 05:42:17 +02:00
|
|
|
}) => {
|
|
|
|
fn main() {
|
2017-04-26 04:51:07 +02:00
|
|
|
$crate::atomic(|cmax| {
|
2017-04-21 07:24:54 +02:00
|
|
|
fn validate_signature(_: fn($crate::P0, &$crate::CMAX)) {}
|
2017-04-14 07:39:12 +02:00
|
|
|
|
2017-04-21 07:24:54 +02:00
|
|
|
validate_signature(init);
|
|
|
|
let p0 = unsafe { ::core::mem::transmute::<_, P0>(()) };
|
2017-04-14 07:15:49 +02:00
|
|
|
init(p0, cmax);
|
2017-04-10 05:42:17 +02:00
|
|
|
set_priorities();
|
|
|
|
enable_tasks();
|
|
|
|
});
|
|
|
|
|
2017-04-28 02:06:22 +02:00
|
|
|
fn validate_signature(_: fn($crate::P0, $crate::C0) -> !) {}
|
2017-04-14 07:39:12 +02:00
|
|
|
|
2017-04-21 07:24:54 +02:00
|
|
|
validate_signature(idle);
|
|
|
|
let p0 = unsafe { ::core::mem::transmute::<_, P0>(()) };
|
2017-04-28 02:06:22 +02:00
|
|
|
let c0 = unsafe { ::core::mem::transmute::<_, C0>(()) };
|
|
|
|
idle(p0, c0);
|
2017-04-10 05:42:17 +02:00
|
|
|
|
|
|
|
fn set_priorities() {
|
|
|
|
// NOTE(safe) this function runs in an interrupt free context
|
2017-04-21 07:24:54 +02:00
|
|
|
let _nvic = unsafe { &*$crate::_NVIC.get() };
|
2017-04-10 05:42:17 +02:00
|
|
|
|
|
|
|
$(
|
|
|
|
{
|
2017-04-21 07:24:54 +02:00
|
|
|
let hw = $crate::$P::_hw();
|
|
|
|
unsafe {
|
|
|
|
_nvic.set_priority(
|
|
|
|
::$device::interrupt::Interrupt::$Interrupt,
|
|
|
|
hw,
|
|
|
|
);
|
2017-04-10 05:42:17 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
)*
|
|
|
|
|
|
|
|
// TODO freeze the NVIC.IPR register using the MPU, if available
|
|
|
|
}
|
2017-04-03 23:18:26 +02:00
|
|
|
|
2017-04-10 05:42:17 +02:00
|
|
|
fn enable_tasks() {
|
|
|
|
// NOTE(safe) this function runs in an interrupt free context
|
2017-04-21 07:24:54 +02:00
|
|
|
let _nvic = unsafe { &*$crate::_NVIC.get() };
|
2017-04-03 23:18:26 +02:00
|
|
|
|
2017-04-10 05:42:17 +02:00
|
|
|
$(
|
2017-04-22 05:02:49 +02:00
|
|
|
if $enabled {
|
|
|
|
$crate::enable(::$task);
|
|
|
|
}
|
2017-04-10 05:42:17 +02:00
|
|
|
)*
|
2017-04-03 23:18:26 +02:00
|
|
|
}
|
|
|
|
|
2017-04-10 05:42:17 +02:00
|
|
|
#[allow(dead_code)]
|
|
|
|
#[link_section = ".rodata.interrupts"]
|
|
|
|
#[used]
|
2017-04-21 07:24:54 +02:00
|
|
|
static INTERRUPTS: ::$device::interrupt::Handlers =
|
|
|
|
::$device::interrupt::Handlers {
|
2017-04-10 05:42:17 +02:00
|
|
|
$(
|
2017-04-10 06:18:00 +02:00
|
|
|
$Interrupt: {
|
2017-04-10 05:42:17 +02:00
|
|
|
extern "C" fn $task(
|
2017-04-21 07:24:54 +02:00
|
|
|
task: ::$device::interrupt::$Interrupt
|
2017-04-10 05:42:17 +02:00
|
|
|
) {
|
2017-04-28 02:06:22 +02:00
|
|
|
fn validate_signature<N>(
|
|
|
|
_: fn(::$device::interrupt::$Interrupt,
|
|
|
|
$crate::P<N>,
|
|
|
|
$crate::C<N>)) {}
|
|
|
|
validate_signature(::$task);
|
2017-04-21 07:24:54 +02:00
|
|
|
let p = unsafe {
|
|
|
|
::core::mem::transmute::<_, $crate::$P>(())
|
|
|
|
};
|
2017-04-28 02:06:22 +02:00
|
|
|
let c = unsafe {
|
|
|
|
::core::mem::transmute(())
|
|
|
|
};
|
2017-04-21 07:24:54 +02:00
|
|
|
$crate::_validate_priority(&p);
|
2017-04-28 02:06:22 +02:00
|
|
|
::$task(task, p, c)
|
2017-04-10 05:42:17 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
$task
|
|
|
|
},
|
|
|
|
)*
|
2017-04-21 07:24:54 +02:00
|
|
|
..::$device::interrupt::DEFAULT_HANDLERS
|
2017-04-10 05:42:17 +02:00
|
|
|
};
|
2017-04-03 23:18:26 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-04-10 05:42:17 +02:00
|
|
|
include!(concat!(env!("OUT_DIR"), "/prio.rs"));
|