flesh out a simple touch-and-go suspend/resume demo loop

now the debuggening begins. lord help us.
This commit is contained in:
bunnie 2021-04-29 02:56:53 +08:00
parent e481df71da
commit bbcf698ff3
13 changed files with 190 additions and 32 deletions

2
Cargo.lock generated
View file

@ -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]]

View file

@ -3,7 +3,7 @@
<device schemaVersion="1.1" xmlns:xs="http://www.w3.org/2001/XMLSchema-instance" xs:noNamespaceSchemaLocation="CMSIS-SVD.xsd" >
<vendor>litex</vendor>
<name>SOC</name>
<description><![CDATA[Litex SoC 2021-04-29 00:33:45]]></description>
<description><![CDATA[Litex SoC 2021-04-29 02:33:22]]></description>
<addressUnitBits>8</addressUnitBits>
<width>32</width>
@ -1070,10 +1070,10 @@ register to disable individual events.]]></description>
<register>
<name>GIT_GITREV</name>
<description><![CDATA[First 32-bits of the git revision. This documentation was built from git rev
``18341437``, so this value is 406066231, which should be enough to check out
``d738663a``, so this value is 3610797626, which should be enough to check out
the exact git version used to build this firmware.]]></description>
<addressOffset>0x0014</addressOffset>
<resetValue>0x18341437</resetValue>
<resetValue>0xd738663a</resetValue>
<size>32</size>
<fields>
<field>
@ -1090,7 +1090,7 @@ the exact git version used to build this firmware.]]></description>
is ``1``, then the repository this was built from has one additional commit
beyond the tag indicated in `MAJOR`, `MINOR`, and `REVISION`.]]></description>
<addressOffset>0x0018</addressOffset>
<resetValue>0x01</resetValue>
<resetValue>0x02</resetValue>
<size>32</size>
<fields>
<field>
@ -2272,6 +2272,14 @@ from the TickTimer]]></description>
<description><![CDATA[Used to transfer the resume state information from the loader to Xous. If set,
indicates we are on the resume half of a suspend/resume.]]></description>
</field>
<field>
<name>was_forced</name>
<msb>1</msb>
<bitRange>[1:1]</bitRange>
<lsb>1</lsb>
<description><![CDATA[Used by the bootloader to indicate to the kernel if the current resume was from
a forced suspend (e.g. a timeout happened and a server may be unclean.]]></description>
</field>
</fields>
</register>
<register>
@ -3143,7 +3151,7 @@ inject too fast.]]></description>
<name>SEED1</name>
<description><![CDATA[Bits 32-63 of `SEED_SEED`. Seed used for the build]]></description>
<addressOffset>0x0000</addressOffset>
<resetValue>0x40041b05</resetValue>
<resetValue>0x676324b6</resetValue>
<size>32</size>
<fields>
<field>
@ -3158,7 +3166,7 @@ inject too fast.]]></description>
<name>SEED0</name>
<description><![CDATA[Bits 0-31 of `SEED_SEED`.]]></description>
<addressOffset>0x0004</addressOffset>
<resetValue>0x488ec687</resetValue>
<resetValue>0x57ada412</resetValue>
<size>32</size>
<fields>
<field>

View file

@ -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) {

View file

@ -3,7 +3,7 @@
<device schemaVersion="1.1" xmlns:xs="http://www.w3.org/2001/XMLSchema-instance" xs:noNamespaceSchemaLocation="CMSIS-SVD.xsd" >
<vendor>litex</vendor>
<name>SOC</name>
<description><![CDATA[Litex SoC 2021-04-29 00:33:45]]></description>
<description><![CDATA[Litex SoC 2021-04-29 02:33:22]]></description>
<addressUnitBits>8</addressUnitBits>
<width>32</width>
@ -1070,10 +1070,10 @@ register to disable individual events.]]></description>
<register>
<name>GIT_GITREV</name>
<description><![CDATA[First 32-bits of the git revision. This documentation was built from git rev
``18341437``, so this value is 406066231, which should be enough to check out
``d738663a``, so this value is 3610797626, which should be enough to check out
the exact git version used to build this firmware.]]></description>
<addressOffset>0x0014</addressOffset>
<resetValue>0x18341437</resetValue>
<resetValue>0xd738663a</resetValue>
<size>32</size>
<fields>
<field>
@ -1090,7 +1090,7 @@ the exact git version used to build this firmware.]]></description>
is ``1``, then the repository this was built from has one additional commit
beyond the tag indicated in `MAJOR`, `MINOR`, and `REVISION`.]]></description>
<addressOffset>0x0018</addressOffset>
<resetValue>0x01</resetValue>
<resetValue>0x02</resetValue>
<size>32</size>
<fields>
<field>
@ -2272,6 +2272,14 @@ from the TickTimer]]></description>
<description><![CDATA[Used to transfer the resume state information from the loader to Xous. If set,
indicates we are on the resume half of a suspend/resume.]]></description>
</field>
<field>
<name>was_forced</name>
<msb>1</msb>
<bitRange>[1:1]</bitRange>
<lsb>1</lsb>
<description><![CDATA[Used by the bootloader to indicate to the kernel if the current resume was from
a forced suspend (e.g. a timeout happened and a server may be unclean.]]></description>
</field>
</fields>
</register>
<register>
@ -3143,7 +3151,7 @@ inject too fast.]]></description>
<name>SEED1</name>
<description><![CDATA[Bits 32-63 of `SEED_SEED`. Seed used for the build]]></description>
<addressOffset>0x0000</addressOffset>
<resetValue>0x40041b05</resetValue>
<resetValue>0x676324b6</resetValue>
<size>32</size>
<fields>
<field>
@ -3158,7 +3166,7 @@ inject too fast.]]></description>
<name>SEED0</name>
<description><![CDATA[Bits 0-31 of `SEED_SEED`.]]></description>
<addressOffset>0x0004</addressOffset>
<resetValue>0x488ec687</resetValue>
<resetValue>0x57ada412</resetValue>
<size>32</size>
<fields>
<field>

Binary file not shown.

View file

@ -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"}

View file

@ -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;
}
}

View file

@ -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<xous::SID>,
execution_gate_conn: CID,
}
impl Susres {
pub fn new(xns: &xous_names::XousNames) -> Result<Self, xous::Error> {
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();
}

View file

@ -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

View file

@ -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"}

View file

@ -8,4 +8,7 @@ pub(crate) enum Opcode {
/// Recalculate the sleep time
RecalculateSleep,
/// Suspend/resume callback
SuspendResume,
}

View file

@ -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

View file

@ -3,7 +3,7 @@
<device schemaVersion="1.1" xmlns:xs="http://www.w3.org/2001/XMLSchema-instance" xs:noNamespaceSchemaLocation="CMSIS-SVD.xsd" >
<vendor>litex</vendor>
<name>SOC</name>
<description><![CDATA[Litex SoC 2021-04-29 00:33:45]]></description>
<description><![CDATA[Litex SoC 2021-04-29 02:33:22]]></description>
<addressUnitBits>8</addressUnitBits>
<width>32</width>
@ -1070,10 +1070,10 @@ register to disable individual events.]]></description>
<register>
<name>GIT_GITREV</name>
<description><![CDATA[First 32-bits of the git revision. This documentation was built from git rev
``18341437``, so this value is 406066231, which should be enough to check out
``d738663a``, so this value is 3610797626, which should be enough to check out
the exact git version used to build this firmware.]]></description>
<addressOffset>0x0014</addressOffset>
<resetValue>0x18341437</resetValue>
<resetValue>0xd738663a</resetValue>
<size>32</size>
<fields>
<field>
@ -1090,7 +1090,7 @@ the exact git version used to build this firmware.]]></description>
is ``1``, then the repository this was built from has one additional commit
beyond the tag indicated in `MAJOR`, `MINOR`, and `REVISION`.]]></description>
<addressOffset>0x0018</addressOffset>
<resetValue>0x01</resetValue>
<resetValue>0x02</resetValue>
<size>32</size>
<fields>
<field>
@ -2272,6 +2272,14 @@ from the TickTimer]]></description>
<description><![CDATA[Used to transfer the resume state information from the loader to Xous. If set,
indicates we are on the resume half of a suspend/resume.]]></description>
</field>
<field>
<name>was_forced</name>
<msb>1</msb>
<bitRange>[1:1]</bitRange>
<lsb>1</lsb>
<description><![CDATA[Used by the bootloader to indicate to the kernel if the current resume was from
a forced suspend (e.g. a timeout happened and a server may be unclean.]]></description>
</field>
</fields>
</register>
<register>
@ -3143,7 +3151,7 @@ inject too fast.]]></description>
<name>SEED1</name>
<description><![CDATA[Bits 32-63 of `SEED_SEED`. Seed used for the build]]></description>
<addressOffset>0x0000</addressOffset>
<resetValue>0x40041b05</resetValue>
<resetValue>0x676324b6</resetValue>
<size>32</size>
<fields>
<field>
@ -3158,7 +3166,7 @@ inject too fast.]]></description>
<name>SEED0</name>
<description><![CDATA[Bits 0-31 of `SEED_SEED`.]]></description>
<addressOffset>0x0004</addressOffset>
<resetValue>0x488ec687</resetValue>
<resetValue>0x57ada412</resetValue>
<size>32</size>
<fields>
<field>