From 53c407017f50d0fde17d38afed714b2fcb54194b Mon Sep 17 00:00:00 2001 From: Emil Fresk Date: Sun, 14 Mar 2021 21:27:21 +0100 Subject: Cancel and reschedule working Support cfgs in the imports Account for extern tasks --- src/tq.rs | 59 ++++++++++++++++++++++++++++++++++++++++++++--------------- 1 file changed, 44 insertions(+), 15 deletions(-) (limited to 'src/tq.rs') diff --git a/src/tq.rs b/src/tq.rs index f341d8c..3864025 100644 --- a/src/tq.rs +++ b/src/tq.rs @@ -1,11 +1,11 @@ use crate::{ + linked_list::{ArrayLength, LinkedList, Min, Node}, time::{Clock, Instant}, Monotonic, }; use core::cmp::Ordering; -use heapless::{binary_heap::Min, ArrayLength, BinaryHeap}; -#[inline] +#[inline(always)] fn unwrapper(val: Result) -> T { if let Ok(v) = val { v @@ -14,18 +14,22 @@ fn unwrapper(val: Result) -> T { } } -pub struct TimerQueue(pub BinaryHeap, N, Min>) +pub struct TimerQueue(pub LinkedList, Min, N>) where Mono: Monotonic, - N: ArrayLength>, + N: ArrayLength>>, Task: Copy; impl TimerQueue where Mono: Monotonic, - N: ArrayLength>, + N: ArrayLength>>, Task: Copy, { + pub fn new() -> Self { + TimerQueue(LinkedList::new()) + } + /// # Safety /// /// Writing to memory with a transmute in order to enable @@ -43,26 +47,20 @@ where F1: FnOnce(), F2: FnOnce(), { - let mut is_empty = true; // 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 .peek() - .map(|head| { - is_empty = false; - nr.instant < head.instant - }) + .map(|head| nr.instant < head.instant) .unwrap_or(true); + if if_heap_max_greater_than_nr { - if Mono::DISABLE_INTERRUPT_ON_EMPTY_QUEUE && is_empty { - // mem::transmute::<_, SYST>(()).enable_interrupt();A + if Mono::DISABLE_INTERRUPT_ON_EMPTY_QUEUE && self.0.is_empty() { mono.enable_timer(); enable_interrupt(); } - // Set SysTick pending - // SCB::set_pendst(); pend_handler(); } @@ -75,8 +73,39 @@ where self.0.is_empty() } + /// Cancel the marker value + pub fn cancel_marker(&mut self, marker: u32) -> Option<(Task, u8)> { + if let Some(val) = self.0.find_mut(|nr| nr.marker == marker) { + let nr = val.pop(); + + Some((nr.task, nr.index)) + } else { + None + } + } + + /// Update the instant at an marker value to a new instant + pub fn update_marker( + &mut self, + marker: u32, + new_marker: u32, + instant: Instant, + pend_handler: F, + ) -> Result<(), ()> { + if let Some(mut val) = self.0.find_mut(|nr| nr.marker == marker) { + val.instant = instant; + val.marker = new_marker; + + // On update pend the handler to reconfigure the next compare match + pend_handler(); + + Ok(()) + } else { + Err(()) + } + } + /// Dequeue a task from the TimerQueue - #[inline] pub fn dequeue(&mut self, disable_interrupt: F, mono: &mut Mono) -> Option<(Task, u8)> where F: FnOnce(), -- cgit v1.2.3