From 684ba69a318a4a497e3db57ee20a61ce352392ca Mon Sep 17 00:00:00 2001 From: Sean Cross Date: Fri, 27 Nov 2020 14:07:44 +0800 Subject: [PATCH] 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 --- services/ticktimer-server/src/main.rs | 5 + .../src/os_timer.rs} | 148 +++++++++--------- 2 files changed, 79 insertions(+), 74 deletions(-) rename services/{shell/src/timer.rs => ticktimer-server/src/os_timer.rs} (96%) diff --git a/services/ticktimer-server/src/main.rs b/services/ticktimer-server/src/main.rs index 5250d70f4..469fbd673 100644 --- a/services/ticktimer-server/src/main.rs +++ b/services/ticktimer-server/src/main.rs @@ -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 diff --git a/services/shell/src/timer.rs b/services/ticktimer-server/src/os_timer.rs similarity index 96% rename from services/shell/src/timer.rs rename to services/ticktimer-server/src/os_timer.rs index ff22221db..3eaf670d7 100644 --- a/services/shell/src/timer.rs +++ b/services/ticktimer-server/src/os_timer.rs @@ -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); + } +}