diff --git a/Cargo.lock b/Cargo.lock index b37c958be..f7afb9b74 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1165,6 +1165,7 @@ dependencies = [ "log", "log-server", "rkyv-test-server", + "susres", "ticktimer-server", "xous", "xous-names", @@ -1439,6 +1440,7 @@ dependencies = [ "susres", "utralib", "xous", + "xous-names", ] [[package]] diff --git a/emulation/renode.svd b/emulation/renode.svd index b8dc503a8..939835df1 100644 --- a/emulation/renode.svd +++ b/emulation/renode.svd @@ -3,7 +3,7 @@ litex SOC - + 8 32 @@ -1070,10 +1070,10 @@ register to disable individual events.]]> GIT_GITREV 0x0014 - 0x18341437 + 0xd738663a 32 @@ -1090,7 +1090,7 @@ the exact git version used to build this firmware.]]> is ``1``, then the repository this was built from has one additional commit beyond the tag indicated in `MAJOR`, `MINOR`, and `REVISION`.]]> 0x0018 - 0x01 + 0x02 32 @@ -2272,6 +2272,14 @@ from the TickTimer]]> + + was_forced + 1 + [1:1] + 1 + + @@ -3143,7 +3151,7 @@ inject too fast.]]> SEED1 0x0000 - 0x40041b05 + 0x676324b6 32 @@ -3158,7 +3166,7 @@ inject too fast.]]> SEED0 0x0004 - 0x488ec687 + 0x57ada412 32 diff --git a/loader/src/main.rs b/loader/src/main.rs index f5b215e25..227a9e84a 100644 --- a/loader/src/main.rs +++ b/loader/src/main.rs @@ -1074,15 +1074,23 @@ fn boot_sequence(args: KernelArguments, _signature: u32) -> ! { }; read_initial_config(&mut cfg); - if !check_resume(&mut cfg) { + let (clean, was_forced_suspend) = check_resume(&mut cfg); + if !clean { phase_1(&mut cfg); phase_2(&mut cfg); } else { use utralib::generated::*; // setup for a resume let mut resume_csr = CSR::new(utra::susres::HW_SUSRES_BASE as *mut u32); - // set the resume marker for the SUSRES server - resume_csr.wfo(utra::susres::STATE_RESUME, 1); + // set the resume marker for the SUSRES server, noting the forced suspend status + if was_forced_suspend { + resume_csr.wo(utra::susres::STATE, + resume_csr.ms(utra::susres::STATE_RESUME, 1) | + resume_csr.ms(utra::susres::STATE_WAS_FORCED, 1) + ); + } else { + resume_csr.wfo(utra::susres::STATE_RESUME, 1); + } resume_csr.wfo(utra::susres::CONTROL_PAUSE, 1); // ensure that the ticktimer is paused before resuming // set the resume marker for the early kernel init @@ -1126,7 +1134,7 @@ fn boot_sequence(args: KernelArguments, _signature: u32) -> ! { } } -fn check_resume(cfg: &mut BootConfig) -> bool { +fn check_resume(cfg: &mut BootConfig) -> (bool, bool) { use utralib::generated::*; const WORDS_PER_SECTOR: usize = 127; const NUM_SECTORS: usize = 8; @@ -1156,13 +1164,14 @@ fn check_resume(cfg: &mut BootConfig) -> bool { if hash != unsafe{(*marker)[index * (WORDS_PER_SECTOR) + 1]} { clean = false; } + index += 1; } // zero out the clean suspend marker, so if something goes wrong during resume we don't try to resume again for i in 0..WORDS_PER_PAGE { unsafe{(*marker)[i] = 0;} } - clean + (clean, was_forced_suspend) } fn phase_1(cfg: &mut BootConfig) { diff --git a/precursors/soc.svd b/precursors/soc.svd index b8dc503a8..939835df1 100644 --- a/precursors/soc.svd +++ b/precursors/soc.svd @@ -3,7 +3,7 @@ litex SOC - + 8 32 @@ -1070,10 +1070,10 @@ register to disable individual events.]]> GIT_GITREV 0x0014 - 0x18341437 + 0xd738663a 32 @@ -1090,7 +1090,7 @@ the exact git version used to build this firmware.]]> is ``1``, then the repository this was built from has one additional commit beyond the tag indicated in `MAJOR`, `MINOR`, and `REVISION`.]]> 0x0018 - 0x01 + 0x02 32 @@ -2272,6 +2272,14 @@ from the TickTimer]]> + + was_forced + 1 + [1:1] + 1 + + @@ -3143,7 +3151,7 @@ inject too fast.]]> SEED1 0x0000 - 0x40041b05 + 0x676324b6 32 @@ -3158,7 +3166,7 @@ inject too fast.]]> SEED0 0x0004 - 0x488ec687 + 0x57ada412 32 diff --git a/precursors/soc_csr.bin b/precursors/soc_csr.bin index ac73f65e8..77b1dec39 100644 Binary files a/precursors/soc_csr.bin and b/precursors/soc_csr.bin differ diff --git a/services/rkyv-test-client/Cargo.toml b/services/rkyv-test-client/Cargo.toml index ca0f195d1..31f5e2cb2 100644 --- a/services/rkyv-test-client/Cargo.toml +++ b/services/rkyv-test-client/Cargo.toml @@ -13,6 +13,7 @@ ticktimer-server = {path = "../ticktimer-server"} xous = {path = "../../xous-rs"} com = {path = "../com"} xous-names = {path="../xous-names"} +susres = {path = "../susres"} keyboard = {path="../keyboard"} diff --git a/services/rkyv-test-client/src/main.rs b/services/rkyv-test-client/src/main.rs index b1d400011..c7b7d4395 100644 --- a/services/rkyv-test-client/src/main.rs +++ b/services/rkyv-test-client/src/main.rs @@ -49,6 +49,7 @@ fn rkyv_test_client() -> ! { let xns = xous_names::XousNames::new().unwrap(); + let mut susres = susres::Susres::new(&xns).unwrap(); /*let mut com = com::Com::new(&xns).unwrap(); com.hook_batt_stats(handle_battstats).unwrap(); @@ -74,6 +75,12 @@ fn rkyv_test_client() -> ! { //com.req_batt_stats().unwrap(); + // let the loop run a bit, then try a suspend + if idx == 2 { + // TODO: add a self-wakeup RTC alarm once we're beyond the touch-and-go phase + susres.initiate_suspend().unwrap(); + } + idx += 1; } } diff --git a/services/susres/src/lib.rs b/services/susres/src/lib.rs index 40f9107a5..298aa63cb 100644 --- a/services/susres/src/lib.rs +++ b/services/susres/src/lib.rs @@ -3,25 +3,90 @@ pub mod api; pub use api::*; -use xous::{CID, send_message}; -use num_traits::ToPrimitive; +use xous::{send_message, CID, Message, msg_scalar_unpack}; +use xous_ipc::Buffer; +use num_traits::{ToPrimitive, FromPrimitive}; pub struct Susres { conn: CID, + suspend_cb_sid: Option, + execution_gate_conn: CID, } impl Susres { pub fn new(xns: &xous_names::XousNames) -> Result { let conn = xns.request_connection_blocking(api::SERVER_NAME_SUSRES).expect("Can't connect to SUSRES"); + let execution_gate_conn = xns.request_connection_blocking(api::SERVER_NAME_EXEC_GATE).expect("Can't connect to the execution gate"); Ok(Susres { - conn + conn, + suspend_cb_sid: None, + execution_gate_conn, }) } + pub fn hook_suspend_callback(&mut self, id: u32, cid: CID) -> Result<(), xous::Error> { + if self.suspend_cb_sid.is_none() { + let sid = xous::create_server().unwrap(); + self.suspend_cb_sid = Some(sid); + let sid_tuple = sid.to_u32(); + xous::create_thread_4(suspend_cb_server, sid_tuple.0 as usize, sid_tuple.1 as usize, sid_tuple.2 as usize, sid_tuple.3 as usize).unwrap(); + let hookdata = ScalarHook { + sid: sid_tuple, + id, + cid, + }; + let buf = Buffer::into_buf(hookdata).or(Err(xous::Error::InternalError))?; + buf.lend(self.conn, Opcode::SuspendEventSubscribe.to_u32().unwrap()).map(|_|()) + } else { + Err(xous::Error::MemoryInUse) // can't hook it twice + } + } + pub fn initiate_suspend(&mut self) -> Result<(), xous::Error> { + send_message(self.conn, + Message::new_scalar(Opcode::SuspendRequest.to_usize().unwrap(), 0, 0, 0, 0) + ).map(|_|()) + } + pub fn suspend_until_resume(&mut self, token: usize) -> Result<(), xous::Error> { + // first tell the susres server that we're ready to suspend + send_message(self.conn, + Message::new_scalar(Opcode::SuspendReady.to_usize().unwrap(), token, 0, 0, 0) + ).map(|_|())?; + // now block until we've resumed + send_message(self.execution_gate_conn, + Message::new_blocking_scalar(ExecGateOpcode::SuspendingNow.to_usize().unwrap(), 0, 0, 0, 0) + ).map(|_|()) + } +} +fn drop_conn(sid: xous::SID) { + let cid = xous::connect(sid).unwrap(); + xous::send_message(cid, + Message::new_scalar(SuspendEventCallback::Drop.to_usize().unwrap(), 0, 0, 0, 0)).unwrap(); + unsafe{xous::disconnect(cid).unwrap();} } - impl Drop for Susres { fn drop(&mut self) { - // de-allocate myself. It's unsafe because we are responsible to make sure nobody else is using the connection. + if let Some(sid) = self.suspend_cb_sid.take() { + drop_conn(sid); + } unsafe{xous::disconnect(self.conn).unwrap();} } +} +/// handles callback messages that indicate a USB interrupt has happened, in the library user's process space. +fn suspend_cb_server(sid0: usize, sid1: usize, sid2: usize, sid3: usize) { + let sid = xous::SID::from_u32(sid0 as u32, sid1 as u32, sid2 as u32, sid3 as u32); + loop { + let msg = xous::receive_message(sid).unwrap(); + match FromPrimitive::from_usize(msg.body.id()) { + Some(SuspendEventCallback::Event) => msg_scalar_unpack!(msg, cid, id, token, _, { + // directly pass the scalar message onto the CID with the ID memorized in the original hook + send_message(cid as u32, + Message::new_scalar(id, token, 0, 0, 0) + ).unwrap(); + }), + Some(SuspendEventCallback::Drop) => { + break; // this exits the loop and kills the thread + } + None => (), + } + } + xous::destroy_server(sid).unwrap(); } \ No newline at end of file diff --git a/services/susres/src/main.rs b/services/susres/src/main.rs index aa03bc58c..36d89fc99 100644 --- a/services/susres/src/main.rs +++ b/services/susres/src/main.rs @@ -42,6 +42,12 @@ mod implementation { // clear the pending interrupt sr.csr.wfo(utra::susres::EV_PENDING_SOFT_INT, 1); + // set this to true to do a touch-and-go suspend/resume (no actual power off, but the whole prep cycle in play) + let touch_and_go = true; + if touch_and_go { + sr.csr.wfo(utra::susres::STATE_RESUME, 1); + } + if sr.csr.rf(utra::susres::STATE_RESUME) == 0 { // power the system down - this should result in an almost immediate loss of power sr.csr.wfo(utra::susres::POWERDOWN_POWERDOWN, 1); @@ -259,7 +265,7 @@ mod implementation { xous::yield_slice(); } } - pub fn do_resume(&mut self, _forced: bool) { + pub fn do_resume(&mut self) -> bool { // returns true if the previous suspend was forced // resume the ticktimer where it left off if let Some(time)= self.stored_time.take() { // zero out the clean-suspend marker @@ -300,6 +306,12 @@ mod implementation { } } else { panic!("Can't resume because the ticktimer value was not saved properly before suspend!") + }; + + if self.csr.rf(utra::susres::STATE_WAS_FORCED) == 0 { + false + } else { + true } } } @@ -484,7 +496,9 @@ fn xmain() -> ! { susres_hw.do_suspend(false); // when do_suspend() returns, it means we've resumed suspend_requested = false; - susres_hw.do_resume(false); + if susres_hw.do_resume() { + log::error!("We did a clean shut-down, but bootloader is saying previous suspend was forced. Some peripherals may be in an unclean state!"); + } // this now allows all other threads to commence RESUME_EXEC.store(true, Ordering::Relaxed); } @@ -518,7 +532,11 @@ fn xmain() -> ! { susres_hw.do_suspend(true); // when do_suspend() returns, it means we've resumed suspend_requested = false; - susres_hw.do_resume(true); + if susres_hw.do_resume() { + log::error!("We forced a suspend, some peripherals may be in an unclean state!"); + } else { + log::error!("We forced a suspend, but the bootloader is claiming we did a clean suspend. Internal state may be inconsistent."); + } RESUME_EXEC.store(true, Ordering::Relaxed); } else { // this means we did a clean suspend, we've resumed, and the timeout came back after the resume diff --git a/services/ticktimer-server/Cargo.toml b/services/ticktimer-server/Cargo.toml index c0fa7fcd1..be5572a2e 100644 --- a/services/ticktimer-server/Cargo.toml +++ b/services/ticktimer-server/Cargo.toml @@ -13,6 +13,7 @@ xous = {path = "../../xous-rs"} num-derive = {version = "0.3", default-features = false} num-traits = {version = "0.2", default-features = false} susres = {path = "../susres"} +xous-names = {path = "../xous-names"} [target.'cfg(not(any(windows,unix)))'.dependencies] utralib = {path = "../../utralib"} diff --git a/services/ticktimer-server/src/api.rs b/services/ticktimer-server/src/api.rs index 35d7d88f9..156e1090e 100644 --- a/services/ticktimer-server/src/api.rs +++ b/services/ticktimer-server/src/api.rs @@ -8,4 +8,7 @@ pub(crate) enum Opcode { /// Recalculate the sleep time RecalculateSleep, + + /// Suspend/resume callback + SuspendResume, } diff --git a/services/ticktimer-server/src/main.rs b/services/ticktimer-server/src/main.rs index 98e5db737..ed4627645 100644 --- a/services/ticktimer-server/src/main.rs +++ b/services/ticktimer-server/src/main.rs @@ -257,6 +257,12 @@ mod implementation { log::trace!("{} WDT is not disarmed, state: 0x{:x}", self.elapsed_ms(), state); } } + pub fn suspend(&mut self) { + self.susres.suspend(); + } + pub fn resume(&mut self) { + self.susres.resume(); + } } } @@ -392,6 +398,14 @@ mod implementation { pub fn reset_wdt(&self) { // dummy function, does nothing } + pub fn register_suspend_listener(&self, _opcode: u32, _cid: xous::CID) -> Result<(), xous::Error> { + Ok(()) + } + pub fn suspend(&self) { + } + pub fn resume(&self) { + } + } } @@ -460,6 +474,15 @@ fn xmain() -> ! { let mut ticktimer = XousTickTimer::new(ticktimer_client); ticktimer.reset(); // make sure the time starts from zero + // register a suspend/resume listener + let xns = xous_names::XousNames::new().unwrap(); + let mut susres = susres::Susres::new(&xns).expect("couldn't create suspend/resume object"); + let sr_cid = xous::connect(ticktimer_server).expect("couldn't create suspend callback connection"); + { + use num_traits::ToPrimitive; + susres.hook_suspend_callback(api::Opcode::SuspendResume.to_usize().unwrap() as u32, sr_cid).expect("couldn't register suspend/resume listener"); + } + loop { #[cfg(feature = "watchdog")] ticktimer.reset_wdt(); @@ -489,7 +512,12 @@ fn xmain() -> ! { }), Some(api::Opcode::RecalculateSleep) => { recalculate_sleep(&mut ticktimer, &mut sleep_heap, None); - } + }, + Some(api::Opcode::SuspendResume) => xous::msg_scalar_unpack!(msg, token, _, _, _, { + ticktimer.suspend(); + susres.suspend_until_resume(token).expect("couldn't execute suspend/resume"); + ticktimer.resume(); + }), None => { error!("couldn't convert opcode"); break diff --git a/svd2utra/examples/soc.svd b/svd2utra/examples/soc.svd index b8dc503a8..939835df1 100644 --- a/svd2utra/examples/soc.svd +++ b/svd2utra/examples/soc.svd @@ -3,7 +3,7 @@ litex SOC - + 8 32 @@ -1070,10 +1070,10 @@ register to disable individual events.]]> GIT_GITREV 0x0014 - 0x18341437 + 0xd738663a 32 @@ -1090,7 +1090,7 @@ the exact git version used to build this firmware.]]> is ``1``, then the repository this was built from has one additional commit beyond the tag indicated in `MAJOR`, `MINOR`, and `REVISION`.]]> 0x0018 - 0x01 + 0x02 32 @@ -2272,6 +2272,14 @@ from the TickTimer]]> + + was_forced + 1 + [1:1] + 1 + + @@ -3143,7 +3151,7 @@ inject too fast.]]> SEED1 0x0000 - 0x40041b05 + 0x676324b6 32 @@ -3158,7 +3166,7 @@ inject too fast.]]> SEED0 0x0004 - 0x488ec687 + 0x57ada412 32