xous-core/services/ticktimer-server/src/main.rs

116 lines
3.3 KiB
Rust
Raw Normal View History

2020-10-12 11:08:16 +00:00
#![cfg_attr(target_os = "none", no_std)]
#![cfg_attr(target_os = "none", no_main)]
#[cfg(target_os = "none")]
2020-10-13 14:49:12 +00:00
#[macro_use]
mod debug;
2020-10-12 11:08:16 +00:00
mod api;
use api::Opcode;
use core::convert::TryFrom;
#[cfg(target_os = "none")]
mod implementation {
const TICKS_PER_MS: u64 = 1;
use utralib::generated::*;
pub struct XousTickTimer {
csr: xous::MemoryRange,
}
impl XousTickTimer {
pub fn new() -> XousTickTimer {
let ctrl = xous::syscall::map_memory(
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-10-12 11:08:16 +00:00
XousTickTimer { csr: ctrl }
}
pub fn reset(&mut self) {
let mut tt = CSR::new(self.csr.as_mut_ptr() as *mut u32);
tt.wfo(utra::ticktimer::CONTROL_RESET, 0b1);
tt.wo(utra::ticktimer::CONTROL, 0); // not paused, not reset -> free-run
}
pub fn raw_ticktime(&self) -> u64 {
let tt = CSR::new(self.csr.as_mut_ptr() as *mut u32);
let mut time: u64 = tt.r(utra::ticktimer::TIME0) as u64;
time |= (tt.r(utra::ticktimer::TIME1) as u64) << 32;
time
}
2020-10-12 11:08:16 +00:00
pub fn elapsed_ms(&self) -> u64 {
self.raw_ticktime() / TICKS_PER_MS
}
2020-10-12 11:08:16 +00:00
}
}
2020-10-12 11:08:16 +00:00
#[cfg(not(target_os = "none"))]
mod implementation {
use std::convert::TryInto;
pub struct XousTickTimer {
start: std::time::Instant,
2020-10-12 11:08:16 +00:00
}
impl XousTickTimer {
pub fn new() -> XousTickTimer {
XousTickTimer {
start: std::time::Instant::now(),
}
}
2020-10-12 11:08:16 +00:00
pub fn reset(&mut self) {
self.start = std::time::Instant::now();
}
2020-10-12 11:08:16 +00:00
pub fn elapsed_ms(&self) -> u64 {
self.start.elapsed().as_millis().try_into().unwrap()
}
2020-10-12 11:08:16 +00:00
}
}
use implementation::*;
2020-10-12 11:08:16 +00:00
#[xous::xous_main]
fn xmain() -> ! {
// Create a new ticktimer object
let mut ticktimer = XousTickTimer::new();
2020-10-12 11:08:16 +00:00
let ticktimer_server =
xous::create_server(b"ticktimer-server").expect("Couldn't create Ticktimer server");
2020-10-12 11:08:16 +00:00
loop {
2020-10-13 14:49:12 +00:00
println!("TickTimer: waiting for message");
2020-10-12 11:08:16 +00:00
let envelope = xous::receive_message(ticktimer_server).unwrap();
println!("TickTimer: Message: {:?}", envelope);
2020-10-12 11:08:16 +00:00
if let Ok(opcode) = Opcode::try_from(&envelope.body) {
println!("TickTimer: Opcode: {:?}", opcode);
2020-10-12 11:08:16 +00:00
match opcode {
Opcode::Reset => {
println!("TickTimer: reset called");
2020-10-12 11:08:16 +00:00
ticktimer.reset();
}
2020-10-12 11:08:16 +00:00
Opcode::ElapsedMs => {
let time = ticktimer.elapsed_ms();
2020-10-13 14:49:12 +00:00
println!("TickTimer: returning time of {:?}", time);
xous::return_scalar2(
envelope.sender,
2020-10-12 11:08:16 +00:00
(time & 0xFFFF_FFFFu64) as usize,
((time >> 32) & 0xFFF_FFFFu64) as usize,
)
.expect("TickTimer: couldn't return time request");
println!("TickTimer: done returning value");
2020-10-12 11:08:16 +00:00
}
}
} else {
println!("Couldn't convert opcode");
2020-10-12 11:08:16 +00:00
}
}
}