2019-06-13 23:56:59 +02:00
|
|
|
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};
|
|
|
|
|
2019-06-13 23:56:59 +02:00
|
|
|
use crate::Monotonic;
|
2018-11-03 17:02:41 +01:00
|
|
|
|
2019-06-13 23:56:59 +02:00
|
|
|
pub struct TimerQueue<M, T, N>(pub BinaryHeap<NotReady<M, T>, N, Min>)
|
2018-11-03 17:02:41 +01:00
|
|
|
where
|
2019-06-13 23:56:59 +02:00
|
|
|
M: Monotonic,
|
|
|
|
<M::Instant as Sub>::Output: TryInto<u32>,
|
|
|
|
N: ArrayLength<NotReady<M, T>>,
|
|
|
|
T: Copy;
|
2018-11-03 17:02:41 +01:00
|
|
|
|
2019-06-13 23:56:59 +02:00
|
|
|
impl<M, T, N> TimerQueue<M, T, N>
|
2018-11-03 17:02:41 +01:00
|
|
|
where
|
2019-06-13 23:56:59 +02:00
|
|
|
M: Monotonic,
|
|
|
|
<M::Instant as Sub>::Output: TryInto<u32>,
|
|
|
|
N: ArrayLength<NotReady<M, T>>,
|
2018-11-03 17:02:41 +01:00
|
|
|
T: Copy,
|
|
|
|
{
|
|
|
|
#[inline]
|
2019-06-13 23:56:59 +02:00
|
|
|
pub unsafe fn enqueue_unchecked(&mut self, nr: NotReady<M, T>) {
|
2018-11-03 17:02:41 +01:00
|
|
|
let mut is_empty = true;
|
|
|
|
if self
|
2019-06-13 23:56:59 +02:00
|
|
|
.0
|
2018-11-03 17:02:41 +01:00
|
|
|
.peek()
|
|
|
|
.map(|head| {
|
|
|
|
is_empty = false;
|
|
|
|
nr.instant < head.instant
|
|
|
|
})
|
|
|
|
.unwrap_or(true)
|
|
|
|
{
|
|
|
|
if is_empty {
|
2019-06-13 23:56:59 +02:00
|
|
|
mem::transmute::<_, SYST>(()).enable_interrupt();
|
2018-11-03 17:02:41 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// set SysTick pending
|
2019-04-21 20:02:59 +02:00
|
|
|
SCB::set_pendst();
|
2018-11-03 17:02:41 +01:00
|
|
|
}
|
|
|
|
|
2019-06-13 23:56:59 +02:00
|
|
|
self.0.push_unchecked(nr);
|
2018-11-03 17:02:41 +01:00
|
|
|
}
|
2019-04-21 20:02:59 +02:00
|
|
|
|
|
|
|
#[inline]
|
|
|
|
pub fn dequeue(&mut self) -> Option<(T, u8)> {
|
2019-06-13 23:56:59 +02:00
|
|
|
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 dur = match (instant - now)
|
|
|
|
.try_into()
|
|
|
|
.ok()
|
|
|
|
.and_then(|x| x.checked_mul(M::ratio()))
|
|
|
|
{
|
|
|
|
None => MAX,
|
|
|
|
Some(x) => cmp::min(MAX, x),
|
|
|
|
};
|
|
|
|
mem::transmute::<_, SYST>(()).set_reload(dur);
|
|
|
|
|
|
|
|
// start counting down from the new reload
|
|
|
|
mem::transmute::<_, SYST>(()).clear_current();
|
|
|
|
|
|
|
|
None
|
|
|
|
}
|
2019-04-21 20:02:59 +02:00
|
|
|
} else {
|
2019-06-13 23:56:59 +02:00
|
|
|
// the queue is empty
|
|
|
|
mem::transmute::<_, SYST>(()).disable_interrupt();
|
2019-04-21 20:02:59 +02:00
|
|
|
|
|
|
|
None
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-11-03 17:02:41 +01:00
|
|
|
}
|
|
|
|
|
2019-06-13 23:56:59 +02:00
|
|
|
pub struct NotReady<M, T>
|
2018-11-03 17:02:41 +01:00
|
|
|
where
|
|
|
|
T: Copy,
|
2019-06-13 23:56:59 +02:00
|
|
|
M: Monotonic,
|
|
|
|
<M::Instant as Sub>::Output: TryInto<u32>,
|
2018-11-03 17:02:41 +01:00
|
|
|
{
|
|
|
|
pub index: u8,
|
2019-06-13 23:56:59 +02:00
|
|
|
pub instant: M::Instant,
|
2018-11-03 17:02:41 +01:00
|
|
|
pub task: T,
|
|
|
|
}
|
|
|
|
|
2019-06-13 23:56:59 +02:00
|
|
|
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
|
|
|
|
2019-06-13 23:56:59 +02:00
|
|
|
impl<M, T> Ord for NotReady<M, T>
|
2018-11-03 17:02:41 +01:00
|
|
|
where
|
|
|
|
T: Copy,
|
2019-06-13 23:56:59 +02:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-13 23:56:59 +02:00
|
|
|
impl<M, T> PartialEq for NotReady<M, T>
|
2018-11-03 17:02:41 +01:00
|
|
|
where
|
|
|
|
T: Copy,
|
2019-06-13 23:56:59 +02:00
|
|
|
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
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-13 23:56:59 +02:00
|
|
|
impl<M, T> PartialOrd for NotReady<M, T>
|
2018-11-03 17:02:41 +01:00
|
|
|
where
|
|
|
|
T: Copy,
|
2019-06-13 23:56:59 +02:00
|
|
|
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))
|
|
|
|
}
|
|
|
|
}
|