2021-03-10 17:45:02 +00:00
|
|
|
use crate::{ShellCmdApi,CommonEnv};
|
2021-03-26 12:22:33 +00:00
|
|
|
use xous_ipc::String;
|
2021-03-10 17:45:02 +00:00
|
|
|
|
|
|
|
|
#[derive(Debug)]
|
|
|
|
|
pub struct Sleep {
|
2021-04-22 10:08:19 +00:00
|
|
|
rtc: rtc::Rtc,
|
2021-05-01 17:38:44 +00:00
|
|
|
susres: susres::Susres,
|
2021-03-10 17:45:02 +00:00
|
|
|
}
|
|
|
|
|
impl Sleep {
|
2021-04-22 10:08:19 +00:00
|
|
|
pub fn new(xns: &xous_names::XousNames) -> Self {
|
|
|
|
|
let rtc = rtc::Rtc::new(&xns).unwrap();
|
|
|
|
|
Sleep {
|
|
|
|
|
rtc,
|
2021-05-01 17:38:44 +00:00
|
|
|
susres: susres::Susres::new_without_hook(&xns).unwrap(),
|
2021-04-22 10:08:19 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-04-22 17:49:03 +00:00
|
|
|
fn kill_thread(bounce: usize) {
|
2021-04-22 10:08:19 +00:00
|
|
|
log::info!("Self destruct thread active.");
|
|
|
|
|
|
|
|
|
|
let xns = xous_names::XousNames::new().unwrap();
|
|
|
|
|
let llio = llio::Llio::new(&xns).unwrap();
|
2021-04-22 17:49:03 +00:00
|
|
|
let com = com::Com::new(&xns).unwrap();
|
2021-04-22 10:08:19 +00:00
|
|
|
let ticktimer = ticktimer_server::Ticktimer::new().unwrap();
|
|
|
|
|
ticktimer.sleep_ms(3000).unwrap();
|
|
|
|
|
|
|
|
|
|
loop {
|
|
|
|
|
log::info!("Initiating self destruct sequence...");
|
2021-04-22 17:49:03 +00:00
|
|
|
if bounce != 0 {
|
|
|
|
|
// slip in a ship mode command. should take long enough to execute that the kill goes through
|
|
|
|
|
com.ship_mode().unwrap();
|
|
|
|
|
ticktimer.sleep_ms(100).unwrap();
|
|
|
|
|
}
|
2021-04-22 10:08:19 +00:00
|
|
|
llio.self_destruct(0x2718_2818).unwrap();
|
|
|
|
|
llio.self_destruct(0x3141_5926).unwrap();
|
2021-04-22 17:49:03 +00:00
|
|
|
com.power_off_soc().unwrap();
|
|
|
|
|
ticktimer.sleep_ms(100).unwrap();
|
2021-04-22 10:08:19 +00:00
|
|
|
log::info!("If you can read this, we failed to destroy ourselves!");
|
2021-03-10 17:45:02 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-05-01 19:36:54 +00:00
|
|
|
fn susres_loop() {
|
|
|
|
|
log::info!("suspend/resume stress test active");
|
|
|
|
|
|
|
|
|
|
let xns = xous_names::XousNames::new().unwrap();
|
|
|
|
|
let mut susres = susres::Susres::new_without_hook(&xns).unwrap();
|
|
|
|
|
let rtc = rtc::Rtc::new(&xns).unwrap();
|
|
|
|
|
let ticktimer = ticktimer_server::Ticktimer::new().unwrap();
|
|
|
|
|
|
|
|
|
|
ticktimer.sleep_ms(1500).unwrap();
|
|
|
|
|
|
|
|
|
|
let mut iters = 0;
|
|
|
|
|
loop {
|
|
|
|
|
log::info!("suspend/resume cycle: {}", iters);
|
|
|
|
|
rtc.set_wakeup_alarm(4).unwrap();
|
|
|
|
|
susres.initiate_suspend().unwrap();
|
|
|
|
|
|
|
|
|
|
ticktimer.sleep_ms(8000).unwrap();
|
|
|
|
|
iters += 1;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-03-10 17:45:02 +00:00
|
|
|
impl<'a> ShellCmdApi<'a> for Sleep {
|
2021-03-11 14:31:38 +00:00
|
|
|
cmd_api!(sleep); // inserts boilerplate for command API
|
|
|
|
|
|
2021-04-16 11:58:05 +00:00
|
|
|
fn process(&mut self, args: String::<1024>, env: &mut CommonEnv) -> Result<Option<String::<1024>>, xous::Error> {
|
2021-03-10 17:45:02 +00:00
|
|
|
use core::fmt::Write;
|
|
|
|
|
|
|
|
|
|
let mut ret = String::<1024>::new();
|
2021-06-01 09:02:30 +00:00
|
|
|
let helpstring = "sleep [now] [current] [ship] [kill] [coldboot] [killbounce] [sus] [stress] [crypton] [cryptoff] [wfioff] [wfion] [debugwfi]";
|
2021-04-16 11:58:05 +00:00
|
|
|
|
|
|
|
|
let mut tokens = args.as_str().unwrap().split(' ');
|
|
|
|
|
|
debugging BOOST mode glitch issue
Here's an arcane bug for you:
When powering down, the Spartan 7 3.3V I/O drivers will
output a roughly 800us wide glitch as 3.3V falls below around
1.7V, if the 0.95V core voltage comes down first (which it does
because we have an active discharge on 0.95V, but not on 3.3V).
In this case, it's just wide and high enough to cause the
BOOST mode line to engage, which takes the BOOST output
and ties it to VBUS, which is just enough for the system to
think you're powering it back on again. Which is not what
you want when powering down.
The ultimate fix was to put a heftier pull-down on the BOOST
line so that it can't trip the BOOST output into the VBUS.
Another option could be more active discharge FETs, but that
would require a full board spin and we are already at certification,
so we'll stick with the stronger pull-down on BOOST. It seems to
be a robust fix in the worst corner cases we can find.
The downside is that when powering an external USB device,
you are burning an extra 30mW of power overcoming the pull-down,
but typically, any external USB devices plugged into Precursor
will burn far in excess of that, so the delta should be negligible.
2021-04-23 07:33:14 +00:00
|
|
|
// in all cases, we want the boost to be off to ensure a clean shutdown
|
|
|
|
|
env.com.set_boost(false).unwrap();
|
|
|
|
|
env.llio.boost_on(false).unwrap();
|
|
|
|
|
|
2021-04-16 11:58:05 +00:00
|
|
|
if let Some(sub_cmd) = tokens.next() {
|
|
|
|
|
match sub_cmd {
|
2021-05-29 16:12:18 +00:00
|
|
|
"crypton" => {
|
|
|
|
|
env.llio.crypto_on(true).unwrap();
|
|
|
|
|
write!(ret, "crypto power is now on").unwrap();
|
|
|
|
|
}
|
|
|
|
|
"cryptoff" => {
|
|
|
|
|
env.llio.crypto_on(false).unwrap();
|
|
|
|
|
write!(ret, "crypto power is now off").unwrap();
|
|
|
|
|
}
|
2021-05-01 17:38:44 +00:00
|
|
|
"sus" => {
|
|
|
|
|
self.susres.initiate_suspend().unwrap();
|
|
|
|
|
// the message below is sent after we wake up
|
|
|
|
|
write!(ret, "Resumed from sleep!").unwrap();
|
|
|
|
|
}
|
2021-05-01 19:36:54 +00:00
|
|
|
"stress" => {
|
|
|
|
|
xous::create_thread_0(susres_loop).unwrap();
|
|
|
|
|
write!(ret, "Starting suspend/resume stress test. Hard reboot required to exit.").unwrap();
|
|
|
|
|
}
|
2021-04-16 11:58:05 +00:00
|
|
|
"now" => {
|
|
|
|
|
if ((env.llio.adc_vbus().unwrap() as f64) * 0.005033) > 1.5 {
|
|
|
|
|
// if power is plugged in, deny powerdown request
|
|
|
|
|
write!(ret, "System can't sleep while charging. Unplug charging cable and try again.").unwrap();
|
|
|
|
|
} else {
|
|
|
|
|
if Ok(true) == env.gam.powerdown_request() {
|
|
|
|
|
env.ticktimer.sleep_ms(500).unwrap(); // let the screen redraw
|
|
|
|
|
|
|
|
|
|
// allow EC to snoop, so that it can wake up the system
|
|
|
|
|
env.llio.allow_ec_snoop(true).unwrap();
|
|
|
|
|
// allow the EC to power me down
|
|
|
|
|
env.llio.allow_power_off(true).unwrap();
|
|
|
|
|
// now send the power off command
|
|
|
|
|
env.com.power_off_soc().unwrap();
|
|
|
|
|
|
|
|
|
|
log::info!("CMD: powering down now!");
|
|
|
|
|
// pause execution, nothing after this should be reachable
|
|
|
|
|
env.ticktimer.sleep_ms(2000).unwrap(); // should power off within 2 seconds
|
|
|
|
|
log::info!("CMD: if you can read this, power down failed!");
|
|
|
|
|
}
|
|
|
|
|
write!(ret, "Powerdown request denied").unwrap();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
"current" => {
|
|
|
|
|
if let Some(i) = env.com.get_standby_current().unwrap() {
|
|
|
|
|
write!(ret, "Last standby current was {}mA", i).unwrap();
|
|
|
|
|
} else {
|
|
|
|
|
write!(ret, "Standby current measurement not initialized.").unwrap();
|
|
|
|
|
}
|
|
|
|
|
}
|
2021-04-22 10:08:19 +00:00
|
|
|
"ship" => {
|
|
|
|
|
if ((env.llio.adc_vbus().unwrap() as f64) * 0.005033) > 1.5 {
|
|
|
|
|
// if power is plugged in, deny powerdown request
|
|
|
|
|
write!(ret, "System can't go into ship mode while charging. Unplug charging cable and try again.").unwrap();
|
|
|
|
|
} else {
|
|
|
|
|
if Ok(true) == env.gam.shipmode_blank_request() {
|
|
|
|
|
env.ticktimer.sleep_ms(500).unwrap(); // let the screen redraw
|
|
|
|
|
|
|
|
|
|
// allow EC to snoop, so that it can wake up the system
|
|
|
|
|
env.llio.allow_ec_snoop(true).unwrap();
|
|
|
|
|
// allow the EC to power me down
|
|
|
|
|
env.llio.allow_power_off(true).unwrap();
|
|
|
|
|
// now send the power off command
|
|
|
|
|
env.com.ship_mode().unwrap();
|
|
|
|
|
|
|
|
|
|
// now send the power off command
|
|
|
|
|
env.com.power_off_soc().unwrap();
|
|
|
|
|
|
|
|
|
|
log::info!("CMD: ship mode now!");
|
|
|
|
|
// pause execution, nothing after this should be reachable
|
|
|
|
|
env.ticktimer.sleep_ms(10000).unwrap(); // ship mode happens in 10 seconds
|
|
|
|
|
log::info!("CMD: if you can read this, ship mode failed!");
|
|
|
|
|
}
|
|
|
|
|
write!(ret, "Ship mode request denied").unwrap();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
"coldboot" => {
|
|
|
|
|
if ((env.llio.adc_vbus().unwrap() as f64) * 0.005033) > 1.5 {
|
|
|
|
|
// if power is plugged in, deny powerdown request
|
|
|
|
|
write!(ret, "System can't cold boot while charging. Unplug charging cable and try again.").unwrap();
|
|
|
|
|
} else {
|
|
|
|
|
if Ok(true) == env.gam.powerdown_request() {
|
|
|
|
|
env.ticktimer.sleep_ms(500).unwrap(); // let the screen redraw
|
|
|
|
|
|
|
|
|
|
// set a wakeup alarm a couple seconds from now -- this is the coldboot
|
debugging BOOST mode glitch issue
Here's an arcane bug for you:
When powering down, the Spartan 7 3.3V I/O drivers will
output a roughly 800us wide glitch as 3.3V falls below around
1.7V, if the 0.95V core voltage comes down first (which it does
because we have an active discharge on 0.95V, but not on 3.3V).
In this case, it's just wide and high enough to cause the
BOOST mode line to engage, which takes the BOOST output
and ties it to VBUS, which is just enough for the system to
think you're powering it back on again. Which is not what
you want when powering down.
The ultimate fix was to put a heftier pull-down on the BOOST
line so that it can't trip the BOOST output into the VBUS.
Another option could be more active discharge FETs, but that
would require a full board spin and we are already at certification,
so we'll stick with the stronger pull-down on BOOST. It seems to
be a robust fix in the worst corner cases we can find.
The downside is that when powering an external USB device,
you are burning an extra 30mW of power overcoming the pull-down,
but typically, any external USB devices plugged into Precursor
will burn far in excess of that, so the delta should be negligible.
2021-04-23 07:33:14 +00:00
|
|
|
self.rtc.set_wakeup_alarm(4).unwrap();
|
2021-04-22 10:08:19 +00:00
|
|
|
|
|
|
|
|
// allow EC to snoop, so that it can wake up the system
|
|
|
|
|
env.llio.allow_ec_snoop(true).unwrap();
|
|
|
|
|
// allow the EC to power me down
|
|
|
|
|
env.llio.allow_power_off(true).unwrap();
|
|
|
|
|
// now send the power off command
|
|
|
|
|
env.com.power_off_soc().unwrap();
|
|
|
|
|
|
|
|
|
|
log::info!("CMD: reboot in 3 seconds!");
|
|
|
|
|
// pause execution, nothing after this should be reachable
|
|
|
|
|
env.ticktimer.sleep_ms(3000).unwrap();
|
|
|
|
|
log::info!("CMD: if you can read this, reboot failed!");
|
|
|
|
|
}
|
|
|
|
|
write!(ret, "Cold boot request denied").unwrap();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
"kill" => {
|
|
|
|
|
write!(ret, "Killing this device in 3 seconds.\nGoodbye cruel world!").unwrap();
|
2021-04-22 17:49:03 +00:00
|
|
|
xous::create_thread_1(kill_thread, 0).unwrap();
|
|
|
|
|
}
|
|
|
|
|
"killbounce" => {
|
|
|
|
|
if ((env.llio.adc_vbus().unwrap() as f64) * 0.005033) > 1.5 {
|
|
|
|
|
// if power is plugged in, deny powerdown request
|
|
|
|
|
write!(ret, "Unplug charging cable and try again.").unwrap();
|
|
|
|
|
} else {
|
|
|
|
|
write!(ret, "Killing this device in 3 seconds, then bouncing into ship mode\n").unwrap();
|
|
|
|
|
xous::create_thread_1(kill_thread, 1).unwrap();
|
|
|
|
|
}
|
2021-04-16 11:58:05 +00:00
|
|
|
}
|
2021-06-01 09:02:30 +00:00
|
|
|
"wfioff" => {
|
|
|
|
|
env.llio.wfi_override(true).unwrap();
|
|
|
|
|
write!(ret, "Overriding WFI signal, forcing always ON").unwrap();
|
|
|
|
|
}
|
|
|
|
|
"wfion" => {
|
|
|
|
|
env.llio.wfi_override(false).unwrap();
|
|
|
|
|
write!(ret, "Allowing WFI auto-control by kernel").unwrap();
|
|
|
|
|
}
|
|
|
|
|
"debugwfi" => {
|
|
|
|
|
env.llio.gpio_data_direction(0x3).unwrap(); // set bits 0 and 1 to output
|
|
|
|
|
env.llio.gpio_debug_powerdown(true).unwrap();
|
|
|
|
|
env.llio.gpio_debug_wakeup(true).unwrap();
|
|
|
|
|
write!(ret, "Connecting CRG powerdown to GPIO0, wakeup interrupt to GPIO1").unwrap();
|
|
|
|
|
}
|
2021-04-16 11:58:05 +00:00
|
|
|
_ => write!(ret, "{}", helpstring).unwrap(),
|
2021-03-10 20:09:47 +00:00
|
|
|
}
|
2021-04-16 11:58:05 +00:00
|
|
|
} else {
|
|
|
|
|
write!(ret, "{}", helpstring).unwrap();
|
2021-03-10 20:09:47 +00:00
|
|
|
}
|
2021-04-16 11:58:05 +00:00
|
|
|
Ok(Some(ret))
|
2021-03-10 17:45:02 +00:00
|
|
|
}
|
2021-03-26 12:22:33 +00:00
|
|
|
}
|