rtic/src/tq.rs

157 lines
4 KiB
Rust
Raw Normal View History

use core::{
cmp::{self, Ordering},
convert::TryInto,
mem,
ops::Sub,
};
2018-11-03 17:02:41 +01:00
use cortex_m::peripheral::{SCB, SYST};
use heapless::{binary_heap::Min, ArrayLength, BinaryHeap};
use crate::Monotonic;
2018-11-03 17:02:41 +01:00
pub struct TimerQueue<M, T, N>(pub BinaryHeap<NotReady<M, T>, N, Min>)
2018-11-03 17:02:41 +01:00
where
M: Monotonic,
<M::Instant as Sub>::Output: TryInto<u32>,
N: ArrayLength<NotReady<M, T>>,
T: Copy;
2018-11-03 17:02:41 +01:00
impl<M, T, N> TimerQueue<M, T, N>
2018-11-03 17:02:41 +01:00
where
M: Monotonic,
<M::Instant as Sub>::Output: TryInto<u32>,
N: ArrayLength<NotReady<M, T>>,
2018-11-03 17:02:41 +01:00
T: Copy,
{
2020-10-13 16:16:33 +02:00
/// # Safety
///
/// Writing to memory with a transmute in order to enable
/// interrupts of the SysTick timer
///
/// Enqueue a task without checking if it is full
2018-11-03 17:02:41 +01:00
#[inline]
pub unsafe fn enqueue_unchecked(&mut self, nr: NotReady<M, T>) {
2018-11-03 17:02:41 +01:00
let mut is_empty = true;
2020-10-13 16:16:33 +02:00
// Check if the top contains a non-empty element and if that element is
// greater than nr
let if_heap_max_greater_than_nr = self
.0
2018-11-03 17:02:41 +01:00
.peek()
.map(|head| {
is_empty = false;
nr.instant < head.instant
})
2020-10-13 16:16:33 +02:00
.unwrap_or(true);
if if_heap_max_greater_than_nr {
2018-11-03 17:02:41 +01:00
if is_empty {
mem::transmute::<_, SYST>(()).enable_interrupt();
2018-11-03 17:02:41 +01:00
}
2020-09-01 19:04:55 +02:00
// Set SysTick pending
SCB::set_pendst();
2018-11-03 17:02:41 +01:00
}
self.0.push_unchecked(nr);
2018-11-03 17:02:41 +01:00
}
2020-10-13 16:16:33 +02:00
/// Dequeue a task from the TimerQueue
#[inline]
pub fn dequeue(&mut self) -> Option<(T, u8)> {
unsafe {
if let Some(instant) = self.0.peek().map(|p| p.instant) {
let now = M::now();
if instant < now {
// task became ready
let nr = self.0.pop_unchecked();
Some((nr.task, nr.index))
} else {
// set a new timeout
const MAX: u32 = 0x00ffffff;
let ratio = M::ratio();
let dur = match (instant - now).try_into().ok().and_then(|x| {
x.checked_mul(ratio.numerator)
.map(|x| x / ratio.denominator)
}) {
None => MAX,
// ARM Architecture Reference Manual says:
// "Setting SYST_RVR to zero has the effect of
// disabling the SysTick counter independently
// of the counter enable bit."
Some(0) => 1,
Some(x) => cmp::min(MAX, x),
};
mem::transmute::<_, SYST>(()).set_reload(dur);
2020-09-01 19:04:55 +02:00
// Start counting down from the new reload
mem::transmute::<_, SYST>(()).clear_current();
None
}
} else {
2020-09-01 19:04:55 +02:00
// The queue is empty
mem::transmute::<_, SYST>(()).disable_interrupt();
None
}
}
}
2018-11-03 17:02:41 +01:00
}
pub struct NotReady<M, T>
2018-11-03 17:02:41 +01:00
where
T: Copy,
M: Monotonic,
<M::Instant as Sub>::Output: TryInto<u32>,
2018-11-03 17:02:41 +01:00
{
pub index: u8,
pub instant: M::Instant,
2018-11-03 17:02:41 +01:00
pub task: T,
}
impl<M, T> Eq for NotReady<M, T>
where
T: Copy,
M: Monotonic,
<M::Instant as Sub>::Output: TryInto<u32>,
{
}
2018-11-03 17:02:41 +01:00
impl<M, T> Ord for NotReady<M, T>
2018-11-03 17:02:41 +01:00
where
T: Copy,
M: Monotonic,
<M::Instant as Sub>::Output: TryInto<u32>,
2018-11-03 17:02:41 +01:00
{
fn cmp(&self, other: &Self) -> Ordering {
self.instant.cmp(&other.instant)
}
}
impl<M, T> PartialEq for NotReady<M, T>
2018-11-03 17:02:41 +01:00
where
T: Copy,
M: Monotonic,
<M::Instant as Sub>::Output: TryInto<u32>,
2018-11-03 17:02:41 +01:00
{
fn eq(&self, other: &Self) -> bool {
self.instant == other.instant
}
}
impl<M, T> PartialOrd for NotReady<M, T>
2018-11-03 17:02:41 +01:00
where
T: Copy,
M: Monotonic,
<M::Instant as Sub>::Output: TryInto<u32>,
2018-11-03 17:02:41 +01:00
{
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
Some(self.cmp(&other))
}
}