mirror of
https://github.com/saymrwulf/xous-core.git
synced 2026-11-17 08:40:52 +00:00
refactor os_timer to be integral to susres
This commit is contained in:
parent
3043769170
commit
88f5199ba2
3 changed files with 53 additions and 81 deletions
|
|
@ -1,7 +1,7 @@
|
||||||
#![cfg_attr(target_os = "none", no_std)]
|
#![cfg_attr(target_os = "none", no_std)]
|
||||||
#![cfg_attr(target_os = "none", no_main)]
|
#![cfg_attr(target_os = "none", no_main)]
|
||||||
|
|
||||||
mod os_timer;
|
//mod os_timer;
|
||||||
|
|
||||||
mod api;
|
mod api;
|
||||||
use api::*;
|
use api::*;
|
||||||
|
|
@ -18,6 +18,20 @@ mod implementation {
|
||||||
use crate::SHOULD_RESUME;
|
use crate::SHOULD_RESUME;
|
||||||
use core::sync::atomic::{AtomicBool, Ordering};
|
use core::sync::atomic::{AtomicBool, Ordering};
|
||||||
|
|
||||||
|
const SYSTEM_CLOCK_FREQUENCY: u32 = 100_000_000;
|
||||||
|
|
||||||
|
fn timer_tick(_irq_no: usize, arg: *mut usize) {
|
||||||
|
let mut timer = CSR::new(arg as *mut u32);
|
||||||
|
// this call forces pre-emption every timer tick
|
||||||
|
// rsyscalls are "raw syscalls" -- used for syscalls that don't have a friendly wrapper around them
|
||||||
|
// since ReturnToParent is only used here, we haven't wrapped it, so we use an rsyscall
|
||||||
|
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);
|
||||||
|
}
|
||||||
|
|
||||||
// we do all the suspend/resume coordination in an interrupt context
|
// we do all the suspend/resume coordination in an interrupt context
|
||||||
// the process state is pushed to main memory prior to entering this routine, so it's staged for a resume
|
// the process state is pushed to main memory prior to entering this routine, so it's staged for a resume
|
||||||
// on a clean suspend then resume, the loader will queue up this interrupt to be the first thing to
|
// on a clean suspend then resume, the loader will queue up this interrupt to be the first thing to
|
||||||
|
|
@ -41,6 +55,8 @@ mod implementation {
|
||||||
pub struct SusResHw {
|
pub struct SusResHw {
|
||||||
/// our CSR
|
/// our CSR
|
||||||
csr: utralib::CSR<u32>,
|
csr: utralib::CSR<u32>,
|
||||||
|
/// OS timer CSR
|
||||||
|
os_timer: utralib::CSR<u32>,
|
||||||
/// memory region for the "clean suspend" marker
|
/// memory region for the "clean suspend" marker
|
||||||
marker: xous::MemoryRange,
|
marker: xous::MemoryRange,
|
||||||
/// loader stack region -- this data is dirtied on every resume; claim it in this process so no others accidentally use it
|
/// loader stack region -- this data is dirtied on every resume; claim it in this process so no others accidentally use it
|
||||||
|
|
@ -50,6 +66,21 @@ mod implementation {
|
||||||
}
|
}
|
||||||
impl SusResHw {
|
impl SusResHw {
|
||||||
pub fn new() -> Self {
|
pub fn new() -> Self {
|
||||||
|
// os timer initializations
|
||||||
|
let ostimer_csr = xous::syscall::map_memory(
|
||||||
|
xous::MemoryAddress::new(utra::timer0::HW_TIMER0_BASE),
|
||||||
|
None,
|
||||||
|
4096,
|
||||||
|
xous::MemoryFlags::R | xous::MemoryFlags::W,
|
||||||
|
)
|
||||||
|
.expect("couldn't map Ticktimer CSR range");
|
||||||
|
xous::claim_interrupt(
|
||||||
|
utra::timer0::TIMER0_IRQ,
|
||||||
|
timer_tick,
|
||||||
|
ostimer_csr.as_mut_ptr() as *mut usize,
|
||||||
|
).expect("couldn't claim IRQ");
|
||||||
|
|
||||||
|
// everything else
|
||||||
let csr = xous::syscall::map_memory(
|
let csr = xous::syscall::map_memory(
|
||||||
xous::MemoryAddress::new(utra::susres::HW_SUSRES_BASE),
|
xous::MemoryAddress::new(utra::susres::HW_SUSRES_BASE),
|
||||||
None,
|
None,
|
||||||
|
|
@ -72,17 +103,28 @@ mod implementation {
|
||||||
).expect("couldn't map clean suspend page");
|
).expect("couldn't map clean suspend page");
|
||||||
let mut sr = SusResHw {
|
let mut sr = SusResHw {
|
||||||
csr: CSR::new(csr.as_mut_ptr() as *mut u32),
|
csr: CSR::new(csr.as_mut_ptr() as *mut u32),
|
||||||
|
os_timer: CSR::new(ostimer_csr.as_mut_ptr() as *mut u32),
|
||||||
stored_time: None,
|
stored_time: None,
|
||||||
marker,
|
marker,
|
||||||
loader_stack,
|
loader_stack,
|
||||||
};
|
};
|
||||||
|
// start the OS timer running
|
||||||
|
let ms = 10; // tick every 10 ms
|
||||||
|
sr.os_timer.wfo(utra::timer0::EN_EN, 0b0); // disable the timer
|
||||||
|
// load its values
|
||||||
|
sr.os_timer.wfo(utra::timer0::LOAD_LOAD, (SYSTEM_CLOCK_FREQUENCY / 1_000) * ms);
|
||||||
|
sr.os_timer.wfo(utra::timer0::RELOAD_RELOAD, (SYSTEM_CLOCK_FREQUENCY / 1_000) * ms);
|
||||||
|
// enable the timer
|
||||||
|
sr.os_timer.wfo(utra::timer0::EN_EN, 0b1);
|
||||||
|
|
||||||
|
// Set EV_ENABLE, this starts pre-emption
|
||||||
|
sr.os_timer.wfo(utra::timer0::EV_ENABLE_ZERO, 0b1);
|
||||||
|
|
||||||
// check that the marker has been zero'd by map_memory. Once this passes we can probably get rid of this code.
|
// check that the marker has been zero'd by map_memory. Once this passes we can probably get rid of this code.
|
||||||
let check_marker = marker.as_ptr() as *const [u32; 1024];
|
let check_marker = marker.as_ptr() as *const [u32; 1024];
|
||||||
for words in 0..1024 {
|
for words in 0..1024 {
|
||||||
if unsafe{(*check_marker)[words]} != 0 {
|
// note to self: don't use log:: here because we're upstream of logging being initialized
|
||||||
log::error!("marker had non-zero entry: 0x{:x} @ 0x{:x}", unsafe{(*check_marker)[words]}, words);
|
assert!(unsafe{(*check_marker)[words]} == 0, "marker had non-zero entry!");
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
xous::claim_interrupt(
|
xous::claim_interrupt(
|
||||||
|
|
@ -260,7 +302,8 @@ pub fn execution_gate() {
|
||||||
#[xous::xous_main]
|
#[xous::xous_main]
|
||||||
fn xmain() -> ! {
|
fn xmain() -> ! {
|
||||||
// Start the OS timer which is responsible for setting up preemption.
|
// Start the OS timer which is responsible for setting up preemption.
|
||||||
os_timer::init();
|
// os_timer::init();
|
||||||
|
let mut susres_hw = implementation::SusResHw::new();
|
||||||
|
|
||||||
log_server::init_wait().unwrap();
|
log_server::init_wait().unwrap();
|
||||||
log::set_max_level(log::LevelFilter::Trace);
|
log::set_max_level(log::LevelFilter::Trace);
|
||||||
|
|
@ -279,7 +322,6 @@ fn xmain() -> ! {
|
||||||
xous::create_thread_4(timeout_thread, sid0 as usize, sid1 as usize, sid2 as usize, sid3 as usize).expect("couldn't create timeout thread");
|
xous::create_thread_4(timeout_thread, sid0 as usize, sid1 as usize, sid2 as usize, sid3 as usize).expect("couldn't create timeout thread");
|
||||||
let timeout_outgoing_conn = xous::connect(timeout_sid).expect("couldn't connect to our timeout thread");
|
let timeout_outgoing_conn = xous::connect(timeout_sid).expect("couldn't connect to our timeout thread");
|
||||||
|
|
||||||
let mut susres_hw = implementation::SusResHw::new();
|
|
||||||
let mut suspend_requested = false;
|
let mut suspend_requested = false;
|
||||||
let mut timeout_pending = false;
|
let mut timeout_pending = false;
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -1,75 +0,0 @@
|
||||||
#[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 = 10; // tick every 10 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);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
@ -50,6 +50,7 @@ fn try_main() -> Result<(), DynError> {
|
||||||
"trng",
|
"trng",
|
||||||
"llio",
|
"llio",
|
||||||
"rtc",
|
"rtc",
|
||||||
|
"susres",
|
||||||
];
|
];
|
||||||
let fcc_pkgs = [
|
let fcc_pkgs = [
|
||||||
"fcc-agent",
|
"fcc-agent",
|
||||||
|
|
@ -60,6 +61,7 @@ fn try_main() -> Result<(), DynError> {
|
||||||
"xous-names",
|
"xous-names",
|
||||||
"keyboard",
|
"keyboard",
|
||||||
"trng",
|
"trng",
|
||||||
|
"susres",
|
||||||
];
|
];
|
||||||
let benchmark_pkgs = [
|
let benchmark_pkgs = [
|
||||||
"benchmark",
|
"benchmark",
|
||||||
|
|
@ -70,6 +72,7 @@ fn try_main() -> Result<(), DynError> {
|
||||||
"xous-names",
|
"xous-names",
|
||||||
"keyboard",
|
"keyboard",
|
||||||
"trng",
|
"trng",
|
||||||
|
"susres",
|
||||||
];
|
];
|
||||||
let minimal_pkgs = [
|
let minimal_pkgs = [
|
||||||
"ticktimer-server",
|
"ticktimer-server",
|
||||||
|
|
@ -79,6 +82,7 @@ fn try_main() -> Result<(), DynError> {
|
||||||
"llio",
|
"llio",
|
||||||
"rkyv-test-client",
|
"rkyv-test-client",
|
||||||
"rkyv-test-server",
|
"rkyv-test-server",
|
||||||
|
"susres",
|
||||||
];
|
];
|
||||||
let cbtest_pkgs = [
|
let cbtest_pkgs = [
|
||||||
"ticktimer-server",
|
"ticktimer-server",
|
||||||
|
|
@ -89,6 +93,7 @@ fn try_main() -> Result<(), DynError> {
|
||||||
"cb-test-srv",
|
"cb-test-srv",
|
||||||
"cb-test-c1",
|
"cb-test-c1",
|
||||||
"cb-test-c2",
|
"cb-test-c2",
|
||||||
|
"susres",
|
||||||
];
|
];
|
||||||
let sr_pkgs = [
|
let sr_pkgs = [
|
||||||
"ticktimer-server",
|
"ticktimer-server",
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue