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

View file

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