os_timer: move preemption from shell to ticktimer server

This is responsible for preemption in the system. Move it to the timer
server since it seems to make more sense to have there.

Signed-off-by: Sean Cross <sean@xobs.io>
This commit is contained in:
Sean Cross 2020-11-27 14:07:44 +08:00
parent 513b99f916
commit 684ba69a31
2 changed files with 79 additions and 74 deletions

View file

@ -4,6 +4,8 @@
mod api;
use api::Opcode;
mod os_timer;
use core::convert::TryFrom;
use heapless::binary_heap::{BinaryHeap, Min};
@ -260,6 +262,9 @@ fn recalculate_sleep(
#[xous::xous_main]
fn xmain() -> ! {
// Start the OS timer which is responsible for setting up preemption.
os_timer::init();
log_server::init_wait().unwrap();
// "Sleep" commands get put in here and are ordered as necessary

View file

@ -1,74 +1,74 @@
#[cfg(baremetal)]
use utralib::generated::*;
#[cfg(baremetal)]
const SYSTEM_CLOCK_FREQUENCY: u32 = 100_000_000;
#[cfg(baremetal)]
const TIMER_BASE: usize = utra::timer0::HW_TIMER0_BASE; // claim the same address in virt as in phys
#[cfg(baremetal)]
fn timer_tick(_irq_no: usize, _arg: *mut usize) {
//println!(">>> Timer tick");
let mut timer = CSR::new(TIMER_BASE as *mut u32);
xous::rsyscall(xous::SysCall::ReturnToParent(xous::PID::new(1).unwrap(), 0)).expect("couldn't return to parent");
// acknowledge the timer
timer.wfo(utra::timer0::EV_PENDING_ZERO, 0b1);
//println!("<<< Returning from timer_tick()");
}
#[cfg(baremetal)]
pub fn init() {
use xous::{MemoryAddress, MemorySize};
println!("Allocating timer...");
xous::rsyscall(xous::SysCall::MapMemory(
MemoryAddress::new(utra::timer0::HW_TIMER0_BASE),
MemoryAddress::new(TIMER_BASE),
MemorySize::new(4096).unwrap(),
xous::MemoryFlags::R | xous::MemoryFlags::W,
))
.expect("timer: couldn't map timer");
xous::rsyscall(xous::SysCall::ClaimInterrupt(
utra::timer0::TIMER0_IRQ,
MemoryAddress::new(timer_tick as *mut usize as usize).unwrap(),
None,
))
.expect("timer: couldn't claim interrupt");
let ms = 100; // tick every 100 ms
en(false);
load((SYSTEM_CLOCK_FREQUENCY / 1_000) * ms);
reload((SYSTEM_CLOCK_FREQUENCY / 1_000) * ms);
en(true);
// Set EV_ENABLE
let mut timer = CSR::new(TIMER_BASE as *mut u32);
timer.wfo(utra::timer0::EV_ENABLE_ZERO, 0b1);
}
#[cfg(not(baremetal))]
pub fn init() {}
#[cfg(baremetal)]
pub fn load(value: u32) {
let mut timer = CSR::new(TIMER_BASE as *mut u32);
timer.wfo(utra::timer0::LOAD_LOAD, value);
}
#[cfg(baremetal)]
pub fn reload(value: u32) {
let mut timer = CSR::new(TIMER_BASE as *mut u32);
timer.wfo(utra::timer0::RELOAD_RELOAD, value);
}
#[cfg(baremetal)]
pub fn en(en: bool) {
let mut timer = CSR::new(TIMER_BASE as *mut u32);
if en {
timer.wfo(utra::timer0::EN_EN, 0b1);
} else {
timer.wfo(utra::timer0::EN_EN, 0b0);
}
}
#[cfg(baremetal)]
use utralib::generated::*;
#[cfg(baremetal)]
const SYSTEM_CLOCK_FREQUENCY: u32 = 100_000_000;
#[cfg(baremetal)]
const TIMER_BASE: usize = utra::timer0::HW_TIMER0_BASE; // claim the same address in virt as in phys
#[cfg(baremetal)]
fn timer_tick(_irq_no: usize, _arg: *mut usize) {
//println!(">>> Timer tick");
let mut timer = CSR::new(TIMER_BASE as *mut u32);
xous::rsyscall(xous::SysCall::ReturnToParent(xous::PID::new(1).unwrap(), 0)).expect("couldn't return to parent");
// acknowledge the timer
timer.wfo(utra::timer0::EV_PENDING_ZERO, 0b1);
//println!("<<< Returning from timer_tick()");
}
#[cfg(baremetal)]
pub fn init() {
use xous::{MemoryAddress, MemorySize};
println!("Allocating timer...");
xous::rsyscall(xous::SysCall::MapMemory(
MemoryAddress::new(utra::timer0::HW_TIMER0_BASE),
MemoryAddress::new(TIMER_BASE),
MemorySize::new(4096).unwrap(),
xous::MemoryFlags::R | xous::MemoryFlags::W,
))
.expect("timer: couldn't map timer");
xous::rsyscall(xous::SysCall::ClaimInterrupt(
utra::timer0::TIMER0_IRQ,
MemoryAddress::new(timer_tick as *mut usize as usize).unwrap(),
None,
))
.expect("timer: couldn't claim interrupt");
let ms = 100; // tick every 100 ms
en(false);
load((SYSTEM_CLOCK_FREQUENCY / 1_000) * ms);
reload((SYSTEM_CLOCK_FREQUENCY / 1_000) * ms);
en(true);
// Set EV_ENABLE
let mut timer = CSR::new(TIMER_BASE as *mut u32);
timer.wfo(utra::timer0::EV_ENABLE_ZERO, 0b1);
}
#[cfg(not(baremetal))]
pub fn init() {}
#[cfg(baremetal)]
pub fn load(value: u32) {
let mut timer = CSR::new(TIMER_BASE as *mut u32);
timer.wfo(utra::timer0::LOAD_LOAD, value);
}
#[cfg(baremetal)]
pub fn reload(value: u32) {
let mut timer = CSR::new(TIMER_BASE as *mut u32);
timer.wfo(utra::timer0::RELOAD_RELOAD, value);
}
#[cfg(baremetal)]
pub fn en(en: bool) {
let mut timer = CSR::new(TIMER_BASE as *mut u32);
if en {
timer.wfo(utra::timer0::EN_EN, 0b1);
} else {
timer.wfo(utra::timer0::EN_EN, 0b0);
}
}