2021-06-18 14:37:08 +00:00
|
|
|
#![cfg_attr(not(target_os = "none"), allow(dead_code))]
|
2020-10-12 11:08:16 +00:00
|
|
|
#![cfg_attr(target_os = "none", no_std)]
|
|
|
|
|
#![cfg_attr(target_os = "none", no_main)]
|
|
|
|
|
|
|
|
|
|
mod api;
|
|
|
|
|
|
2021-08-03 17:25:32 +00:00
|
|
|
use std::cmp::Reverse;
|
2021-08-19 15:48:53 +00:00
|
|
|
use std::collections::BinaryHeap;
|
2020-11-11 08:09:01 +00:00
|
|
|
|
|
|
|
|
use log::{error, info};
|
|
|
|
|
|
2021-03-23 13:41:30 +00:00
|
|
|
#[derive(Eq)]
|
|
|
|
|
pub struct SleepRequest {
|
|
|
|
|
msec: i64,
|
2020-11-11 08:09:01 +00:00
|
|
|
sender: xous::MessageSender,
|
|
|
|
|
}
|
|
|
|
|
|
2021-03-23 13:41:30 +00:00
|
|
|
impl core::fmt::Display for SleepRequest {
|
|
|
|
|
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
|
|
|
|
|
write!(f, "SleepRequest {{ msec: {}, {} }}", self.msec, self.sender)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl core::fmt::Debug for SleepRequest {
|
|
|
|
|
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
|
|
|
|
|
write!(f, "SleepRequest {{ msec: {}, {} }}", self.msec, self.sender)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl core::cmp::Ord for SleepRequest {
|
2020-11-11 08:09:01 +00:00
|
|
|
fn cmp(&self, other: &Self) -> core::cmp::Ordering {
|
2021-03-23 13:41:30 +00:00
|
|
|
if self.msec < other.msec {
|
|
|
|
|
core::cmp::Ordering::Less
|
|
|
|
|
} else if self.msec > other.msec {
|
|
|
|
|
core::cmp::Ordering::Greater
|
|
|
|
|
} else {
|
|
|
|
|
self.sender.cmp(&other.sender)
|
|
|
|
|
}
|
2020-11-11 08:09:01 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-03-23 13:41:30 +00:00
|
|
|
impl core::cmp::PartialOrd for SleepRequest {
|
2020-11-11 08:09:01 +00:00
|
|
|
fn partial_cmp(&self, other: &Self) -> Option<core::cmp::Ordering> {
|
|
|
|
|
Some(self.cmp(other))
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-03-23 13:41:30 +00:00
|
|
|
impl core::cmp::PartialEq for SleepRequest {
|
2020-11-11 08:09:01 +00:00
|
|
|
fn eq(&self, other: &Self) -> bool {
|
|
|
|
|
self.msec == other.msec && self.sender == other.sender
|
|
|
|
|
}
|
|
|
|
|
}
|
2020-10-12 11:08:16 +00:00
|
|
|
|
2021-08-02 10:11:48 +00:00
|
|
|
#[cfg(any(target_os = "none", target_os = "xous"))]
|
2020-10-28 03:14:26 +00:00
|
|
|
mod implementation {
|
|
|
|
|
const TICKS_PER_MS: u64 = 1;
|
2021-03-23 13:41:30 +00:00
|
|
|
use super::SleepRequest;
|
2021-04-29 08:37:43 +00:00
|
|
|
use susres::{RegManager, RegOrField, SuspendResume};
|
2021-08-19 15:48:53 +00:00
|
|
|
use utralib::generated::*;
|
2020-11-11 08:09:01 +00:00
|
|
|
|
2020-10-28 03:14:26 +00:00
|
|
|
pub struct XousTickTimer {
|
2020-11-11 08:09:01 +00:00
|
|
|
csr: utralib::CSR<u32>,
|
2021-03-23 13:41:30 +00:00
|
|
|
current_response: Option<SleepRequest>,
|
2020-11-11 08:09:01 +00:00
|
|
|
connection: xous::CID,
|
2021-08-19 15:48:53 +00:00
|
|
|
ticktimer_sr_manager: RegManager<{ utra::ticktimer::TICKTIMER_NUMREGS }>,
|
|
|
|
|
wdt_sr_manager: RegManager<{ utra::wdt::WDT_NUMREGS }>,
|
2021-05-30 15:07:16 +00:00
|
|
|
wdt: utralib::CSR<u32>,
|
2020-11-11 08:09:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn handle_irq(_irq_no: usize, arg: *mut usize) {
|
|
|
|
|
let xtt = unsafe { &mut *(arg as *mut XousTickTimer) };
|
2020-11-27 06:00:19 +00:00
|
|
|
// println!("In IRQ, connection: {}", xtt.connection);
|
2020-11-11 08:09:01 +00:00
|
|
|
|
|
|
|
|
// Safe because we're in an interrupt, and this interrupt is only
|
|
|
|
|
// enabled when this value is not None.
|
|
|
|
|
let response = xtt.current_response.take().unwrap();
|
2021-03-23 13:41:30 +00:00
|
|
|
|
2020-11-11 08:09:01 +00:00
|
|
|
xous::return_scalar(response.sender, 0).expect("couldn't send response");
|
|
|
|
|
|
2021-03-23 13:41:30 +00:00
|
|
|
// Disable the timer
|
|
|
|
|
xtt.csr.wfo(utra::ticktimer::EV_ENABLE_ALARM, 0);
|
|
|
|
|
xtt.csr.wfo(utra::ticktimer::EV_PENDING_ALARM, 1);
|
2020-11-11 08:09:01 +00:00
|
|
|
|
2021-03-23 13:41:30 +00:00
|
|
|
// This is dangerous and may return an error if the queue is full.
|
|
|
|
|
// Which is fine, because the queue is always recalculated any time a message arrives.
|
2021-03-28 10:14:41 +00:00
|
|
|
use num_traits::ToPrimitive;
|
2021-08-19 15:48:53 +00:00
|
|
|
xous::try_send_message(
|
|
|
|
|
xtt.connection,
|
2021-03-28 10:14:41 +00:00
|
|
|
xous::Message::Scalar(xous::ScalarMessage {
|
|
|
|
|
id: crate::api::Opcode::RecalculateSleep.to_usize().unwrap(),
|
2021-08-19 15:48:53 +00:00
|
|
|
arg1: 0,
|
|
|
|
|
arg2: 0,
|
|
|
|
|
arg3: 0,
|
|
|
|
|
arg4: 0,
|
|
|
|
|
}),
|
|
|
|
|
)
|
|
|
|
|
.ok();
|
2020-10-28 03:14:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl XousTickTimer {
|
2020-11-11 08:09:01 +00:00
|
|
|
pub fn new(connection: xous::CID) -> XousTickTimer {
|
2020-11-27 06:00:19 +00:00
|
|
|
// println!("Connection: {}", connection);
|
2020-11-11 08:09:01 +00:00
|
|
|
let csr = xous::syscall::map_memory(
|
2020-10-28 03:14:26 +00:00
|
|
|
xous::MemoryAddress::new(utra::ticktimer::HW_TICKTIMER_BASE),
|
|
|
|
|
None,
|
|
|
|
|
4096,
|
|
|
|
|
xous::MemoryFlags::R | xous::MemoryFlags::W,
|
|
|
|
|
)
|
|
|
|
|
.expect("couldn't map Tick Timer CSR range");
|
2020-12-12 17:54:25 +00:00
|
|
|
let wdt = xous::syscall::map_memory(
|
|
|
|
|
xous::MemoryAddress::new(utra::wdt::HW_WDT_BASE),
|
|
|
|
|
None,
|
|
|
|
|
4096,
|
|
|
|
|
xous::MemoryFlags::R | xous::MemoryFlags::W,
|
|
|
|
|
)
|
|
|
|
|
.expect("couldn't map Watchdog timer CSR range");
|
2020-10-12 11:08:16 +00:00
|
|
|
|
2021-04-29 08:37:43 +00:00
|
|
|
let ticktimer_sr_manager = RegManager::new(csr.as_mut_ptr() as *mut u32);
|
|
|
|
|
let wdt_sr_manager = RegManager::new(wdt.as_mut_ptr() as *mut u32);
|
2021-04-26 16:40:50 +00:00
|
|
|
|
2020-11-11 08:09:01 +00:00
|
|
|
let mut xtt = XousTickTimer {
|
|
|
|
|
csr: CSR::new(csr.as_mut_ptr() as *mut u32),
|
|
|
|
|
current_response: None,
|
|
|
|
|
connection,
|
2021-04-29 08:37:43 +00:00
|
|
|
ticktimer_sr_manager,
|
|
|
|
|
wdt_sr_manager,
|
2021-05-30 15:07:16 +00:00
|
|
|
wdt: CSR::new(wdt.as_mut_ptr() as *mut u32),
|
2020-11-11 08:09:01 +00:00
|
|
|
};
|
|
|
|
|
|
2021-03-23 13:41:30 +00:00
|
|
|
#[cfg(feature = "watchdog")]
|
2021-04-26 16:40:50 +00:00
|
|
|
{
|
|
|
|
|
xtt.wdt.wfo(utra::wdt::WATCHDOG_ENABLE, 1);
|
2021-04-29 08:37:43 +00:00
|
|
|
// this is a write-once field that is lost later on, so it must be explicitly managed
|
|
|
|
|
// xtt.wdt_sr_manager.push(RegOrField::Field(utra::wdt::WATCHDOG_ENABLE), None);
|
2021-04-26 16:40:50 +00:00
|
|
|
}
|
2020-12-12 17:54:25 +00:00
|
|
|
|
2020-11-11 08:09:01 +00:00
|
|
|
xous::claim_interrupt(
|
2020-11-23 09:34:18 +00:00
|
|
|
utra::ticktimer::TICKTIMER_IRQ,
|
2020-11-11 08:09:01 +00:00
|
|
|
handle_irq,
|
|
|
|
|
(&mut xtt) as *mut XousTickTimer as *mut usize,
|
|
|
|
|
)
|
|
|
|
|
.expect("couldn't claim irq");
|
2020-12-17 20:35:39 +00:00
|
|
|
|
2021-08-19 15:48:53 +00:00
|
|
|
xtt.ticktimer_sr_manager
|
|
|
|
|
.push(RegOrField::Reg(utra::ticktimer::MSLEEP_TARGET0), None);
|
|
|
|
|
xtt.ticktimer_sr_manager
|
|
|
|
|
.push(RegOrField::Reg(utra::ticktimer::MSLEEP_TARGET1), None);
|
|
|
|
|
xtt.ticktimer_sr_manager
|
|
|
|
|
.push_fixed_value(RegOrField::Reg(utra::ticktimer::EV_PENDING), 0xFFFF_FFFF);
|
|
|
|
|
xtt.ticktimer_sr_manager
|
|
|
|
|
.push(RegOrField::Reg(utra::ticktimer::EV_ENABLE), None);
|
2021-04-29 08:37:43 +00:00
|
|
|
|
2020-11-11 08:09:01 +00:00
|
|
|
xtt
|
2020-10-28 03:14:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn reset(&mut self) {
|
2020-11-11 08:09:01 +00:00
|
|
|
self.csr.wfo(utra::ticktimer::CONTROL_RESET, 0b1);
|
|
|
|
|
self.csr.wo(utra::ticktimer::CONTROL, 0); // not paused, not reset -> free-run
|
2020-10-28 03:14:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn raw_ticktime(&self) -> u64 {
|
2020-11-11 08:09:01 +00:00
|
|
|
let mut time: u64 = self.csr.r(utra::ticktimer::TIME0) as u64;
|
|
|
|
|
time |= (self.csr.r(utra::ticktimer::TIME1) as u64) << 32;
|
2020-10-28 03:14:26 +00:00
|
|
|
|
|
|
|
|
time
|
|
|
|
|
}
|
2020-10-12 11:08:16 +00:00
|
|
|
|
2020-10-28 03:14:26 +00:00
|
|
|
pub fn elapsed_ms(&self) -> u64 {
|
|
|
|
|
self.raw_ticktime() / TICKS_PER_MS
|
|
|
|
|
}
|
2020-11-11 08:09:01 +00:00
|
|
|
|
2021-03-23 13:41:30 +00:00
|
|
|
pub fn stop_interrupt(&mut self) -> Option<SleepRequest> {
|
|
|
|
|
// Disable the timer
|
|
|
|
|
self.csr.wfo(utra::ticktimer::EV_ENABLE_ALARM, 0);
|
|
|
|
|
|
|
|
|
|
// Now that the interrupt is disabled, we can see if the interrupt handler has a current response.
|
|
|
|
|
// If it exists, then that means that an interrupt did NOT fire, and an existing interrupt
|
|
|
|
|
// is in place.
|
2020-11-11 08:09:01 +00:00
|
|
|
if let Some(sr) = self.current_response.take() {
|
2021-03-23 13:41:30 +00:00
|
|
|
#[cfg(feature = "debug-print")]
|
|
|
|
|
{
|
|
|
|
|
log::info!(
|
2021-04-01 19:10:13 +00:00
|
|
|
"Stopping currently-running timer sr.msec: {} elapsed_ms: {}",
|
2021-03-23 13:41:30 +00:00
|
|
|
sr.msec,
|
|
|
|
|
self.elapsed_ms()
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
Some(sr)
|
2020-11-11 08:09:01 +00:00
|
|
|
} else {
|
|
|
|
|
None
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-03-23 13:41:30 +00:00
|
|
|
pub fn schedule_response(&mut self, request: SleepRequest) {
|
|
|
|
|
let irq_target = request.msec;
|
2021-04-30 11:29:36 +00:00
|
|
|
log::trace!(
|
2021-04-01 19:10:13 +00:00
|
|
|
"setting a response at {} ms (current time: {} ms)",
|
2020-11-11 08:09:01 +00:00
|
|
|
irq_target,
|
2021-03-23 13:41:30 +00:00
|
|
|
self.elapsed_ms()
|
2020-11-11 08:09:01 +00:00
|
|
|
);
|
2021-03-23 13:41:30 +00:00
|
|
|
|
|
|
|
|
// Disable the timer interrupt
|
|
|
|
|
assert!(self.csr.rf(utra::ticktimer::EV_ENABLE_ALARM) == 0);
|
|
|
|
|
self.csr.wfo(utra::ticktimer::EV_ENABLE_ALARM, 0);
|
|
|
|
|
|
|
|
|
|
// Save a copy of the current sleep request
|
|
|
|
|
self.current_response = Some(request);
|
|
|
|
|
|
|
|
|
|
// Set the new target time
|
2020-11-11 08:09:01 +00:00
|
|
|
self.csr
|
|
|
|
|
.wo(utra::ticktimer::MSLEEP_TARGET1, (irq_target >> 32) as _);
|
|
|
|
|
self.csr
|
|
|
|
|
.wo(utra::ticktimer::MSLEEP_TARGET0, irq_target as _);
|
2021-03-23 13:41:30 +00:00
|
|
|
|
|
|
|
|
// Clear previous interrupt (if any)
|
|
|
|
|
self.csr.wfo(utra::ticktimer::EV_PENDING_ALARM, 1);
|
|
|
|
|
|
|
|
|
|
// Enable the interrupt
|
|
|
|
|
self.csr.wfo(utra::ticktimer::EV_ENABLE_ALARM, 1);
|
2020-11-11 08:09:01 +00:00
|
|
|
}
|
2020-12-12 17:54:25 +00:00
|
|
|
|
2021-05-30 15:07:16 +00:00
|
|
|
#[allow(dead_code)]
|
2020-12-12 17:54:25 +00:00
|
|
|
pub fn reset_wdt(&mut self) {
|
2021-08-22 21:07:21 +00:00
|
|
|
self.wdt.wfo(utra::wdt::WATCHDOG_RESET_WDT, 1);
|
2020-12-12 17:54:25 +00:00
|
|
|
}
|
2021-04-15 10:40:00 +00:00
|
|
|
|
2021-04-15 17:13:56 +00:00
|
|
|
#[allow(dead_code)]
|
2021-04-15 10:40:00 +00:00
|
|
|
pub fn check_wdt(&mut self) {
|
|
|
|
|
let state = self.wdt.r(utra::wdt::STATE);
|
|
|
|
|
if state & self.wdt.ms(utra::wdt::STATE_DISARMED, 1) == 0 {
|
2021-08-19 15:48:53 +00:00
|
|
|
log::info!(
|
|
|
|
|
"{} WDT is not disarmed, state: 0x{:x}",
|
|
|
|
|
self.elapsed_ms(),
|
|
|
|
|
state
|
|
|
|
|
);
|
2021-04-15 10:40:00 +00:00
|
|
|
}
|
|
|
|
|
}
|
2021-04-29 08:37:43 +00:00
|
|
|
|
|
|
|
|
// the ticktimer suspend/resume routines are a bit trickier than normal, so this isn't a great
|
|
|
|
|
// example of a generic suspend/resume template
|
2021-04-28 18:56:53 +00:00
|
|
|
pub fn suspend(&mut self) {
|
2021-04-29 08:37:43 +00:00
|
|
|
log::trace!("suspending");
|
|
|
|
|
self.ticktimer_sr_manager.suspend();
|
|
|
|
|
self.wdt_sr_manager.suspend();
|
|
|
|
|
|
|
|
|
|
// by writing this after suspend(), resume will get the prior value
|
|
|
|
|
self.csr.wfo(utra::ticktimer::EV_ENABLE_ALARM, 0);
|
2021-04-28 18:56:53 +00:00
|
|
|
}
|
|
|
|
|
pub fn resume(&mut self) {
|
2021-04-29 08:37:43 +00:00
|
|
|
// this is a write-once bit that's later erased, so it can't be managed automatically
|
|
|
|
|
// thus we have to restore in manually on a resume
|
|
|
|
|
#[cfg(feature = "watchdog")]
|
2021-05-01 19:36:54 +00:00
|
|
|
{
|
|
|
|
|
self.wdt.wfo(utra::wdt::WATCHDOG_ENABLE, 1);
|
|
|
|
|
}
|
2021-04-29 08:37:43 +00:00
|
|
|
|
|
|
|
|
// manually clear any pending ticktimer events. This is mainly releveant for a "touch-and-go" simulated suspend.
|
|
|
|
|
self.csr.wfo(utra::ticktimer::EV_PENDING_ALARM, 1);
|
|
|
|
|
|
|
|
|
|
self.wdt_sr_manager.resume();
|
|
|
|
|
self.ticktimer_sr_manager.resume();
|
2021-04-30 11:29:36 +00:00
|
|
|
|
2021-08-19 15:48:53 +00:00
|
|
|
log::trace!(
|
|
|
|
|
"ticktimer enable: {}",
|
|
|
|
|
self.csr.r(utra::ticktimer::EV_ENABLE)
|
|
|
|
|
);
|
|
|
|
|
log::trace!(
|
|
|
|
|
"ticktimer time/target: {}/{}",
|
|
|
|
|
self.csr.r(utra::ticktimer::TIME0),
|
|
|
|
|
self.csr.r(utra::ticktimer::MSLEEP_TARGET0)
|
|
|
|
|
);
|
2021-04-28 18:56:53 +00:00
|
|
|
}
|
2020-10-12 11:08:16 +00:00
|
|
|
}
|
2020-10-28 03:14:26 +00:00
|
|
|
}
|
2020-10-12 11:08:16 +00:00
|
|
|
|
2021-08-02 10:11:48 +00:00
|
|
|
#[cfg(not(any(target_os = "none", target_os = "xous")))]
|
2020-10-28 03:14:26 +00:00
|
|
|
mod implementation {
|
2021-03-23 13:41:30 +00:00
|
|
|
use super::SleepRequest;
|
2021-03-31 19:47:10 +00:00
|
|
|
use num_traits::ToPrimitive;
|
2021-08-19 15:48:53 +00:00
|
|
|
use std::convert::TryInto;
|
2020-11-11 08:09:01 +00:00
|
|
|
|
|
|
|
|
#[derive(Debug)]
|
|
|
|
|
enum SleepComms {
|
|
|
|
|
InterruptSleep,
|
2021-03-23 13:41:30 +00:00
|
|
|
StartSleep(
|
|
|
|
|
xous::MessageSender,
|
|
|
|
|
i64, /* ms */
|
|
|
|
|
u64, /* elapsed */
|
|
|
|
|
),
|
2020-11-11 08:09:01 +00:00
|
|
|
}
|
2020-10-28 03:14:26 +00:00
|
|
|
pub struct XousTickTimer {
|
|
|
|
|
start: std::time::Instant,
|
2020-11-11 08:09:01 +00:00
|
|
|
sleep_comms: std::sync::mpsc::Sender<SleepComms>,
|
2021-03-23 13:41:30 +00:00
|
|
|
time_remaining_receiver: std::sync::mpsc::Receiver<Option<SleepRequest>>,
|
2020-10-12 11:08:16 +00:00
|
|
|
}
|
|
|
|
|
|
2020-10-28 03:14:26 +00:00
|
|
|
impl XousTickTimer {
|
2020-11-11 08:09:01 +00:00
|
|
|
pub fn new(cid: xous::CID) -> XousTickTimer {
|
|
|
|
|
let (sleep_sender, sleep_receiver) = std::sync::mpsc::channel();
|
|
|
|
|
let (time_remaining_sender, time_remaining_receiver) = std::sync::mpsc::channel();
|
|
|
|
|
xous::create_thread(move || {
|
|
|
|
|
let mut timeout = None;
|
2021-03-23 13:41:30 +00:00
|
|
|
let mut current_response: Option<SleepRequest> = None;
|
2020-11-11 08:09:01 +00:00
|
|
|
loop {
|
|
|
|
|
let result = match timeout {
|
2020-11-11 08:23:23 +00:00
|
|
|
None => sleep_receiver
|
|
|
|
|
.recv()
|
|
|
|
|
.map_err(|_| std::sync::mpsc::RecvTimeoutError::Disconnected),
|
|
|
|
|
Some(s) => sleep_receiver.recv_timeout(s),
|
2020-11-11 08:09:01 +00:00
|
|
|
};
|
|
|
|
|
match result {
|
|
|
|
|
Err(std::sync::mpsc::RecvTimeoutError::Timeout) => {
|
2021-03-23 13:41:30 +00:00
|
|
|
let sender = current_response.take().unwrap().sender;
|
|
|
|
|
#[cfg(feature = "debug-print")]
|
|
|
|
|
log::info!("Returning scalar to {}", sender);
|
2020-11-11 08:09:01 +00:00
|
|
|
xous::return_scalar(sender, 0).expect("couldn't send response");
|
|
|
|
|
|
|
|
|
|
// This is dangerous and may panic if the queue is full.
|
|
|
|
|
xous::try_send_message(
|
|
|
|
|
cid,
|
2021-03-31 19:47:10 +00:00
|
|
|
xous::Message::Scalar(xous::ScalarMessage {
|
|
|
|
|
id: crate::api::Opcode::RecalculateSleep.to_usize().unwrap(),
|
2021-08-19 15:48:53 +00:00
|
|
|
arg1: 0,
|
|
|
|
|
arg2: 0,
|
|
|
|
|
arg3: 0,
|
|
|
|
|
arg4: 0,
|
|
|
|
|
}),
|
2020-11-11 08:09:01 +00:00
|
|
|
)
|
|
|
|
|
.unwrap();
|
|
|
|
|
timeout = None;
|
|
|
|
|
}
|
|
|
|
|
Err(std::sync::mpsc::RecvTimeoutError::Disconnected) => {
|
|
|
|
|
return;
|
|
|
|
|
}
|
2021-03-23 13:41:30 +00:00
|
|
|
Ok(SleepComms::InterruptSleep) => {
|
|
|
|
|
timeout = None;
|
|
|
|
|
time_remaining_sender.send(current_response.take()).unwrap()
|
|
|
|
|
}
|
|
|
|
|
Ok(SleepComms::StartSleep(new_sender, expiry, elapsed)) => {
|
|
|
|
|
let mut duration = expiry - (elapsed as i64);
|
|
|
|
|
if duration > 0 {
|
|
|
|
|
#[cfg(feature = "debug-print")]
|
|
|
|
|
log::info!(
|
|
|
|
|
"Starting sleep for {} ms, returning to {}",
|
|
|
|
|
duration,
|
|
|
|
|
new_sender
|
|
|
|
|
);
|
2020-11-11 08:09:01 +00:00
|
|
|
} else {
|
2021-03-23 13:41:30 +00:00
|
|
|
#[cfg(feature = "debug-print")]
|
|
|
|
|
log::info!(
|
|
|
|
|
"Clamping duration to 0 (was: {})m returning to {}",
|
|
|
|
|
duration,
|
|
|
|
|
new_sender
|
|
|
|
|
);
|
|
|
|
|
duration = 0;
|
|
|
|
|
}
|
|
|
|
|
timeout = Some(std::time::Duration::from_millis(
|
|
|
|
|
duration.try_into().unwrap(),
|
|
|
|
|
));
|
|
|
|
|
current_response = Some(SleepRequest {
|
|
|
|
|
sender: new_sender,
|
|
|
|
|
msec: expiry,
|
|
|
|
|
});
|
2020-11-11 08:09:01 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
.unwrap();
|
|
|
|
|
|
2020-10-28 03:14:26 +00:00
|
|
|
XousTickTimer {
|
|
|
|
|
start: std::time::Instant::now(),
|
2020-11-11 08:09:01 +00:00
|
|
|
time_remaining_receiver,
|
|
|
|
|
sleep_comms: sleep_sender,
|
2020-10-28 03:14:26 +00:00
|
|
|
}
|
|
|
|
|
}
|
2020-10-12 11:08:16 +00:00
|
|
|
|
2020-10-28 03:14:26 +00:00
|
|
|
pub fn reset(&mut self) {
|
|
|
|
|
self.start = std::time::Instant::now();
|
|
|
|
|
}
|
2020-10-12 11:08:16 +00:00
|
|
|
|
2020-10-28 03:14:26 +00:00
|
|
|
pub fn elapsed_ms(&self) -> u64 {
|
2020-10-30 02:29:12 +00:00
|
|
|
self.start.elapsed().as_millis().try_into().unwrap()
|
2020-10-28 03:14:26 +00:00
|
|
|
}
|
2020-11-11 08:09:01 +00:00
|
|
|
|
2021-03-23 13:41:30 +00:00
|
|
|
pub fn stop_interrupt(&mut self) -> Option<SleepRequest> {
|
2020-11-11 08:09:01 +00:00
|
|
|
self.sleep_comms.send(SleepComms::InterruptSleep).unwrap();
|
|
|
|
|
self.time_remaining_receiver.recv().unwrap()
|
|
|
|
|
}
|
|
|
|
|
|
2021-03-23 13:41:30 +00:00
|
|
|
pub fn schedule_response(&mut self, request: SleepRequest) {
|
|
|
|
|
#[cfg(feature = "debug-print")]
|
|
|
|
|
log::info!(
|
|
|
|
|
"request.msec: {} self.elapsed_ms: {} returning to: {}",
|
|
|
|
|
request.msec,
|
|
|
|
|
self.elapsed_ms(),
|
|
|
|
|
request.sender
|
|
|
|
|
);
|
2020-11-11 08:09:01 +00:00
|
|
|
self.sleep_comms
|
2021-03-23 13:41:30 +00:00
|
|
|
.send(SleepComms::StartSleep(
|
|
|
|
|
request.sender,
|
|
|
|
|
request.msec as i64,
|
|
|
|
|
self.elapsed_ms(),
|
|
|
|
|
))
|
2020-11-11 08:09:01 +00:00
|
|
|
.unwrap();
|
|
|
|
|
}
|
2020-12-12 17:54:25 +00:00
|
|
|
|
2021-05-30 15:07:16 +00:00
|
|
|
#[allow(dead_code)]
|
2020-12-12 18:03:15 +00:00
|
|
|
pub fn reset_wdt(&self) {
|
2020-12-12 17:54:25 +00:00
|
|
|
// dummy function, does nothing
|
|
|
|
|
}
|
2021-08-19 15:48:53 +00:00
|
|
|
pub fn register_suspend_listener(
|
|
|
|
|
&self,
|
|
|
|
|
_opcode: u32,
|
|
|
|
|
_cid: xous::CID,
|
|
|
|
|
) -> Result<(), xous::Error> {
|
2021-04-28 18:56:53 +00:00
|
|
|
Ok(())
|
|
|
|
|
}
|
2021-08-19 15:48:53 +00:00
|
|
|
pub fn suspend(&self) {}
|
|
|
|
|
pub fn resume(&self) {}
|
2020-10-12 11:08:16 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-10-28 03:14:26 +00:00
|
|
|
use implementation::*;
|
|
|
|
|
|
2020-11-11 08:09:01 +00:00
|
|
|
fn recalculate_sleep(
|
|
|
|
|
ticktimer: &mut XousTickTimer,
|
2021-08-03 17:25:32 +00:00
|
|
|
sleep_heap: &mut BinaryHeap<Reverse<SleepRequest>>, // min-heap with Reverse
|
2021-03-23 13:41:30 +00:00
|
|
|
new: Option<SleepRequest>,
|
2020-11-11 08:09:01 +00:00
|
|
|
) {
|
2021-03-23 13:41:30 +00:00
|
|
|
// If there's a sleep request ongoing now, grab it.
|
2020-11-11 08:09:01 +00:00
|
|
|
if let Some(current) = ticktimer.stop_interrupt() {
|
2021-03-23 13:41:30 +00:00
|
|
|
#[cfg(feature = "debug-print")]
|
2021-04-01 19:10:13 +00:00
|
|
|
info!("Existing request was {:?}", current);
|
2021-08-03 17:25:32 +00:00
|
|
|
sleep_heap.push(Reverse(current));
|
2021-03-23 13:41:30 +00:00
|
|
|
} else {
|
|
|
|
|
#[cfg(feature = "debug-print")]
|
2021-04-01 19:10:13 +00:00
|
|
|
info!("There was no existing request");
|
2020-11-11 08:09:01 +00:00
|
|
|
}
|
|
|
|
|
|
2021-03-23 13:41:30 +00:00
|
|
|
// If we have a new sleep request, add it to the heap.
|
|
|
|
|
if let Some(mut request) = new {
|
|
|
|
|
#[cfg(feature = "debug-print")]
|
2021-04-01 19:10:13 +00:00
|
|
|
info!("New sleep request was: {:?}", request);
|
2021-03-23 13:41:30 +00:00
|
|
|
|
|
|
|
|
request.msec += ticktimer.elapsed_ms() as i64;
|
|
|
|
|
|
|
|
|
|
#[cfg(feature = "debug-print")]
|
2021-04-01 19:10:13 +00:00
|
|
|
info!("Modified, the request was: {:?}", request);
|
2021-08-19 15:48:53 +00:00
|
|
|
sleep_heap.push(Reverse(request));
|
2021-03-23 13:41:30 +00:00
|
|
|
} else {
|
|
|
|
|
#[cfg(feature = "debug-print")]
|
2021-04-01 19:10:13 +00:00
|
|
|
info!("No new sleep request");
|
2020-11-11 08:09:01 +00:00
|
|
|
}
|
2021-03-23 13:41:30 +00:00
|
|
|
|
|
|
|
|
// If there are items in the sleep heap, take the next item that will expire.
|
2021-08-03 17:25:32 +00:00
|
|
|
if let Some(Reverse(next_response)) = sleep_heap.pop() {
|
2021-03-23 13:41:30 +00:00
|
|
|
#[cfg(feature = "debug-print")]
|
|
|
|
|
info!(
|
2021-04-01 19:10:13 +00:00
|
|
|
"scheduling a response at {} to {} (heap: {:?})",
|
2021-03-23 13:41:30 +00:00
|
|
|
next_response.msec, next_response.sender, sleep_heap
|
|
|
|
|
);
|
|
|
|
|
ticktimer.schedule_response(next_response);
|
2020-11-11 08:09:01 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-10-12 11:08:16 +00:00
|
|
|
#[xous::xous_main]
|
|
|
|
|
fn xmain() -> ! {
|
2021-08-03 17:25:32 +00:00
|
|
|
let mut sleep_heap: BinaryHeap<Reverse<SleepRequest>> = BinaryHeap::new(); // Reverse wrapping makes this a min-heap
|
2021-03-23 13:41:30 +00:00
|
|
|
|
2020-11-11 08:09:01 +00:00
|
|
|
log_server::init_wait().unwrap();
|
2021-05-01 11:13:22 +00:00
|
|
|
log::set_max_level(log::LevelFilter::Info);
|
2021-04-01 19:10:13 +00:00
|
|
|
info!("my PID is {}", xous::process::id());
|
2020-11-11 08:09:01 +00:00
|
|
|
|
2021-01-03 10:31:51 +00:00
|
|
|
let ticktimer_server = xous::create_server_with_address(b"ticktimer-server")
|
|
|
|
|
.expect("Couldn't create Ticktimer server");
|
2021-04-01 19:10:13 +00:00
|
|
|
info!("Server started with SID {:?}", ticktimer_server);
|
2020-11-11 08:09:01 +00:00
|
|
|
|
|
|
|
|
// Connect to our own server so we can send the "Recalculate" message
|
|
|
|
|
let ticktimer_client = xous::connect(xous::SID::from_bytes(b"ticktimer-server").unwrap())
|
|
|
|
|
.expect("couldn't connect to self");
|
|
|
|
|
|
|
|
|
|
// Create a new ticktimer object
|
|
|
|
|
let mut ticktimer = XousTickTimer::new(ticktimer_client);
|
2021-01-04 20:40:34 +00:00
|
|
|
ticktimer.reset(); // make sure the time starts from zero
|
2020-11-11 08:09:01 +00:00
|
|
|
|
2021-04-28 18:56:53 +00:00
|
|
|
// register a suspend/resume listener
|
|
|
|
|
let xns = xous_names::XousNames::new().unwrap();
|
2021-08-19 15:48:53 +00:00
|
|
|
let sr_cid =
|
|
|
|
|
xous::connect(ticktimer_server).expect("couldn't create suspend callback connection");
|
|
|
|
|
let mut susres = susres::Susres::new(&xns, api::Opcode::SuspendResume as u32, sr_cid)
|
|
|
|
|
.expect("couldn't create suspend/resume object");
|
2021-04-28 18:56:53 +00:00
|
|
|
|
2020-10-12 11:08:16 +00:00
|
|
|
loop {
|
2021-03-23 13:41:30 +00:00
|
|
|
#[cfg(feature = "watchdog")]
|
2020-12-12 17:54:25 +00:00
|
|
|
ticktimer.reset_wdt();
|
2021-08-22 21:07:21 +00:00
|
|
|
#[cfg(feature = "watchdog")] // for debugging the watchdog
|
|
|
|
|
ticktimer.check_wdt();
|
2020-12-12 17:54:25 +00:00
|
|
|
|
2021-03-28 10:14:41 +00:00
|
|
|
let msg = xous::receive_message(ticktimer_server).unwrap();
|
2021-04-30 11:29:36 +00:00
|
|
|
log::trace!("msg: {:?}", msg);
|
2021-03-28 10:14:41 +00:00
|
|
|
match num_traits::FromPrimitive::from_usize(msg.body.id()) {
|
|
|
|
|
Some(api::Opcode::ElapsedMs) => {
|
|
|
|
|
let time = ticktimer.elapsed_ms() as i64;
|
|
|
|
|
xous::return_scalar2(
|
|
|
|
|
msg.sender,
|
|
|
|
|
(time & 0xFFFF_FFFFi64) as usize,
|
|
|
|
|
((time >> 32) & 0xFFF_FFFFi64) as usize,
|
|
|
|
|
)
|
2021-04-01 19:10:13 +00:00
|
|
|
.expect("couldn't return time request");
|
2021-03-28 10:14:41 +00:00
|
|
|
}
|
2021-04-02 07:38:38 +00:00
|
|
|
Some(api::Opcode::SleepMs) => xous::msg_blocking_scalar_unpack!(msg, ms, _, _, _, {
|
2021-08-19 15:48:53 +00:00
|
|
|
recalculate_sleep(
|
|
|
|
|
&mut ticktimer,
|
|
|
|
|
&mut sleep_heap,
|
|
|
|
|
Some(SleepRequest {
|
|
|
|
|
msec: ms as i64,
|
|
|
|
|
sender: msg.sender,
|
|
|
|
|
}),
|
|
|
|
|
)
|
2021-04-02 07:38:38 +00:00
|
|
|
}),
|
2021-03-28 10:14:41 +00:00
|
|
|
Some(api::Opcode::RecalculateSleep) => {
|
|
|
|
|
recalculate_sleep(&mut ticktimer, &mut sleep_heap, None);
|
2021-08-19 15:48:53 +00:00
|
|
|
}
|
2021-04-28 18:56:53 +00:00
|
|
|
Some(api::Opcode::SuspendResume) => xous::msg_scalar_unpack!(msg, token, _, _, _, {
|
|
|
|
|
ticktimer.suspend();
|
2021-08-19 15:48:53 +00:00
|
|
|
susres
|
|
|
|
|
.suspend_until_resume(token)
|
|
|
|
|
.expect("couldn't execute suspend/resume");
|
2021-04-28 18:56:53 +00:00
|
|
|
ticktimer.resume();
|
|
|
|
|
}),
|
2021-07-15 15:00:50 +00:00
|
|
|
Some(api::Opcode::PingWdt) => {
|
|
|
|
|
ticktimer.reset_wdt();
|
2021-08-19 15:48:53 +00:00
|
|
|
}
|
2021-03-28 10:14:41 +00:00
|
|
|
None => {
|
2021-04-01 19:10:13 +00:00
|
|
|
error!("couldn't convert opcode");
|
2021-08-19 15:48:53 +00:00
|
|
|
break;
|
2021-03-28 10:14:41 +00:00
|
|
|
}
|
2020-10-12 11:08:16 +00:00
|
|
|
}
|
|
|
|
|
}
|
2021-04-05 15:50:00 +00:00
|
|
|
// clean up our program
|
|
|
|
|
log::trace!("main loop exit, destroying servers");
|
|
|
|
|
xous::destroy_server(ticktimer_server).unwrap();
|
|
|
|
|
log::trace!("quitting");
|
2021-05-20 03:01:00 +00:00
|
|
|
xous::terminate_process(0);
|
2020-10-12 11:08:16 +00:00
|
|
|
}
|