2020-10-13 08:15:41 +00:00
|
|
|
use utralib::generated::*;
|
2020-06-03 07:39:48 +00:00
|
|
|
|
2020-06-15 10:13:10 +00:00
|
|
|
#[cfg(baremetal)]
|
2020-10-04 11:07:34 +00:00
|
|
|
const SYSTEM_CLOCK_FREQUENCY: u32 = 100_000_000;
|
2020-06-15 10:13:10 +00:00
|
|
|
#[cfg(baremetal)]
|
2020-10-13 08:15:41 +00:00
|
|
|
const TIMER_BASE: usize = utra::timer0::HW_TIMER0_BASE; // claim the same address in virt as in phys
|
2020-10-11 19:56:10 +00:00
|
|
|
|
2020-06-15 10:13:10 +00:00
|
|
|
#[cfg(baremetal)]
|
2020-03-10 04:46:05 +00:00
|
|
|
fn timer_tick(_irq_no: usize, _arg: *mut usize) {
|
2020-10-13 14:49:12 +00:00
|
|
|
//println!(">>> Timer tick");
|
2020-10-12 09:26:34 +00:00
|
|
|
let mut timer = CSR::new(TIMER_BASE as *mut u32);
|
2020-03-10 04:46:05 +00:00
|
|
|
|
2020-08-10 15:04:36 +00:00
|
|
|
xous::rsyscall(xous::SysCall::ReturnToParentI(xous::PID::new(1).unwrap(), 0)).expect("couldn't return to parent");
|
2020-04-06 07:52:07 +00:00
|
|
|
|
2020-03-10 04:46:05 +00:00
|
|
|
// acknowledge the timer
|
2020-10-12 09:26:34 +00:00
|
|
|
timer.wfo(utra::timer0::EV_PENDING_PENDING, 0b1);
|
2020-10-13 14:49:12 +00:00
|
|
|
//println!("<<< Returning from timer_tick()");
|
2020-03-10 04:46:05 +00:00
|
|
|
}
|
|
|
|
|
|
2020-06-15 10:13:10 +00:00
|
|
|
#[cfg(baremetal)]
|
2020-03-10 04:46:05 +00:00
|
|
|
pub fn init() {
|
2020-06-15 10:13:10 +00:00
|
|
|
use xous::{MemoryAddress, MemorySize};
|
2020-03-10 04:46:05 +00:00
|
|
|
println!("Allocating timer...");
|
2020-03-13 12:33:08 +00:00
|
|
|
xous::rsyscall(xous::SysCall::MapMemory(
|
2020-10-12 09:26:34 +00:00
|
|
|
MemoryAddress::new(utra::timer0::HW_TIMER0_BASE),
|
2020-06-03 07:39:48 +00:00
|
|
|
MemoryAddress::new(TIMER_BASE),
|
|
|
|
|
MemorySize::new(4096).unwrap(),
|
2020-03-10 04:46:05 +00:00
|
|
|
xous::MemoryFlags::R | xous::MemoryFlags::W,
|
|
|
|
|
))
|
|
|
|
|
.expect("timer: couldn't map timer");
|
|
|
|
|
|
|
|
|
|
xous::rsyscall(xous::SysCall::ClaimInterrupt(
|
2020-10-13 08:15:41 +00:00
|
|
|
utra::timer0::TIMER0_IRQ,
|
2020-06-03 07:39:48 +00:00
|
|
|
MemoryAddress::new(timer_tick as *mut usize as usize).unwrap(),
|
|
|
|
|
None,
|
2020-03-10 04:46:05 +00:00
|
|
|
))
|
|
|
|
|
.expect("timer: couldn't claim interrupt");
|
|
|
|
|
|
2020-10-04 11:07:34 +00:00
|
|
|
let ms = 100; // tick every 100 ms
|
2020-03-10 04:46:05 +00:00
|
|
|
en(false);
|
2020-10-12 09:26:34 +00:00
|
|
|
load((SYSTEM_CLOCK_FREQUENCY / 1_000) * ms);
|
|
|
|
|
reload((SYSTEM_CLOCK_FREQUENCY / 1_000) * ms);
|
2020-03-10 04:46:05 +00:00
|
|
|
en(true);
|
|
|
|
|
|
|
|
|
|
// Set EV_ENABLE
|
2020-10-11 19:56:10 +00:00
|
|
|
let mut timer = CSR::new(TIMER_BASE as *mut u32);
|
2020-10-12 09:26:34 +00:00
|
|
|
timer.wfo(utra::timer0::EV_ENABLE_ENABLE, 0b1);
|
2020-03-10 04:46:05 +00:00
|
|
|
}
|
|
|
|
|
|
2020-06-15 10:13:10 +00:00
|
|
|
#[cfg(not(baremetal))]
|
|
|
|
|
pub fn init() {}
|
2020-03-10 04:46:05 +00:00
|
|
|
|
2020-08-10 15:04:36 +00:00
|
|
|
#[cfg(baremetal)]
|
|
|
|
|
pub fn load(value: u32) {
|
2020-10-12 09:26:34 +00:00
|
|
|
let mut timer = CSR::new(TIMER_BASE as *mut u32);
|
|
|
|
|
timer.wfo(utra::timer0::LOAD_LOAD, value);
|
2020-08-10 15:04:36 +00:00
|
|
|
}
|
2020-03-10 04:46:05 +00:00
|
|
|
|
2020-08-10 15:04:36 +00:00
|
|
|
#[cfg(baremetal)]
|
|
|
|
|
pub fn reload(value: u32) {
|
2020-10-12 09:26:34 +00:00
|
|
|
let mut timer = CSR::new(TIMER_BASE as *mut u32);
|
|
|
|
|
timer.wfo(utra::timer0::RELOAD_RELOAD, value);
|
2020-08-10 15:04:36 +00:00
|
|
|
}
|
2020-06-15 10:13:10 +00:00
|
|
|
|
2020-08-10 15:04:36 +00:00
|
|
|
#[cfg(baremetal)]
|
|
|
|
|
pub fn en(en: bool) {
|
2020-10-12 09:26:34 +00:00
|
|
|
let mut timer = CSR::new(TIMER_BASE as *mut u32);
|
2020-08-10 15:04:36 +00:00
|
|
|
if en {
|
2020-10-12 09:26:34 +00:00
|
|
|
timer.wfo(utra::timer0::EN_EN, 0b1);
|
2020-08-10 15:04:36 +00:00
|
|
|
} else {
|
2020-10-12 09:26:34 +00:00
|
|
|
timer.wfo(utra::timer0::EN_EN, 0b0);
|
2020-08-10 15:04:36 +00:00
|
|
|
}
|
|
|
|
|
}
|