2017-07-21 05:53:44 +02:00
|
|
|
//! Nesting claims and how the preemption threshold works
|
|
|
|
//!
|
|
|
|
//! If you run this program you'll hit the breakpoints as indicated by the
|
|
|
|
//! letters in the comments: A, then B, then C, etc.
|
|
|
|
#![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_rtfm as rtfm;
|
|
|
|
extern crate stm32f103xx;
|
|
|
|
|
|
|
|
use rtfm::{app, Resource, Threshold};
|
2018-04-16 21:32:52 +02:00
|
|
|
use stm32f103xx::Interrupt;
|
2017-07-21 05:53:44 +02:00
|
|
|
|
|
|
|
app! {
|
|
|
|
device: stm32f103xx,
|
|
|
|
|
|
|
|
resources: {
|
|
|
|
static LOW: u64 = 0;
|
|
|
|
static HIGH: u64 = 0;
|
|
|
|
},
|
|
|
|
|
|
|
|
tasks: {
|
|
|
|
EXTI0: {
|
2017-07-27 18:40:15 +02:00
|
|
|
path: exti0,
|
2017-07-21 05:53:44 +02:00
|
|
|
priority: 1,
|
|
|
|
resources: [LOW, HIGH],
|
|
|
|
},
|
|
|
|
|
|
|
|
EXTI1: {
|
2017-07-27 18:40:15 +02:00
|
|
|
path: exti1,
|
2017-07-21 05:53:44 +02:00
|
|
|
priority: 2,
|
|
|
|
resources: [LOW],
|
|
|
|
},
|
|
|
|
|
|
|
|
EXTI2: {
|
2017-07-27 18:40:15 +02:00
|
|
|
path: exti2,
|
2017-07-21 05:53:44 +02:00
|
|
|
priority: 3,
|
|
|
|
resources: [HIGH],
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
fn init(_p: init::Peripherals, _r: init::Resources) {}
|
|
|
|
|
|
|
|
fn idle() -> ! {
|
2017-07-28 05:40:47 +02:00
|
|
|
// A
|
|
|
|
rtfm::bkpt();
|
|
|
|
|
|
|
|
// Sets task `exti0` as pending
|
2017-07-21 05:53:44 +02:00
|
|
|
//
|
2017-07-28 05:40:47 +02:00
|
|
|
// Because `exti0` has higher priority than `idle` it will be executed
|
2017-07-21 05:53:44 +02:00
|
|
|
// immediately
|
|
|
|
rtfm::set_pending(Interrupt::EXTI0); // ~> exti0
|
|
|
|
|
|
|
|
loop {
|
|
|
|
rtfm::wfi();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-07-29 07:34:00 +02:00
|
|
|
#[allow(non_snake_case)]
|
2018-04-16 21:32:52 +02:00
|
|
|
fn exti0(t: &mut Threshold, EXTI0::Resources { mut LOW, mut HIGH }: EXTI0::Resources) {
|
2017-07-28 05:40:47 +02:00
|
|
|
// Because this task has a priority of 1 the preemption threshold `t` also
|
|
|
|
// starts at 1
|
2017-07-21 05:53:44 +02:00
|
|
|
|
2017-07-28 05:40:47 +02:00
|
|
|
// B
|
2017-07-21 05:53:44 +02:00
|
|
|
rtfm::bkpt();
|
|
|
|
|
2017-07-28 05:40:47 +02:00
|
|
|
// Because `exti1` has higher priority than `exti0` it can preempt it
|
2017-07-21 05:53:44 +02:00
|
|
|
rtfm::set_pending(Interrupt::EXTI1); // ~> exti1
|
|
|
|
|
2017-07-28 05:40:47 +02:00
|
|
|
// A claim creates a critical section
|
2017-07-29 07:34:00 +02:00
|
|
|
LOW.claim_mut(t, |_low, t| {
|
2017-07-28 05:40:47 +02:00
|
|
|
// This claim increases the preemption threshold to 2
|
|
|
|
//
|
|
|
|
// 2 is just high enough to not race with task `exti1` for access to the
|
2017-07-21 05:53:44 +02:00
|
|
|
// `LOW` resource
|
|
|
|
|
2017-07-28 05:40:47 +02:00
|
|
|
// D
|
2017-07-21 05:53:44 +02:00
|
|
|
rtfm::bkpt();
|
|
|
|
|
2017-07-28 05:40:47 +02:00
|
|
|
// Now `exti1` can't preempt this task because its priority is equal to
|
2017-07-21 05:53:44 +02:00
|
|
|
// the current preemption threshold
|
|
|
|
rtfm::set_pending(Interrupt::EXTI1);
|
|
|
|
|
2017-07-28 05:40:47 +02:00
|
|
|
// But `exti2` can, because its priority is higher than the current
|
2017-07-21 05:53:44 +02:00
|
|
|
// preemption threshold
|
|
|
|
rtfm::set_pending(Interrupt::EXTI2); // ~> exti2
|
|
|
|
|
2017-07-28 05:40:47 +02:00
|
|
|
// F
|
2017-07-21 05:53:44 +02:00
|
|
|
rtfm::bkpt();
|
|
|
|
|
2017-07-28 05:40:47 +02:00
|
|
|
// Claims can be nested
|
2017-07-29 07:34:00 +02:00
|
|
|
HIGH.claim_mut(t, |_high, _| {
|
2017-07-21 05:53:44 +02:00
|
|
|
// This claim increases the preemption threshold to 3
|
|
|
|
|
2017-07-28 05:40:47 +02:00
|
|
|
// Now `exti2` can't preempt this task
|
2017-07-21 05:53:44 +02:00
|
|
|
rtfm::set_pending(Interrupt::EXTI2);
|
|
|
|
|
2017-07-28 05:40:47 +02:00
|
|
|
// G
|
2017-07-21 05:53:44 +02:00
|
|
|
rtfm::bkpt();
|
|
|
|
});
|
|
|
|
|
2017-07-28 05:40:47 +02:00
|
|
|
// Upon leaving the critical section the preemption threshold drops back
|
|
|
|
// to 2 and `exti2` immediately preempts this task
|
2017-07-21 05:53:44 +02:00
|
|
|
// ~> exti2
|
|
|
|
});
|
|
|
|
|
2017-07-28 05:40:47 +02:00
|
|
|
// Once again the preemption threshold drops but this time to 1. Now the
|
|
|
|
// pending `exti1` task can preempt this task
|
2017-07-21 05:53:44 +02:00
|
|
|
// ~> exti1
|
|
|
|
}
|
|
|
|
|
|
|
|
fn exti1(_t: &mut Threshold, _r: EXTI1::Resources) {
|
2017-07-28 05:40:47 +02:00
|
|
|
// C, I
|
2017-07-21 05:53:44 +02:00
|
|
|
rtfm::bkpt();
|
|
|
|
}
|
|
|
|
|
|
|
|
fn exti2(_t: &mut Threshold, _r: EXTI2::Resources) {
|
2017-07-28 05:40:47 +02:00
|
|
|
// E, H
|
2017-07-21 05:53:44 +02:00
|
|
|
rtfm::bkpt();
|
|
|
|
}
|