diff --git a/Cargo.lock b/Cargo.lock index 930cba46d..813eb6116 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -531,6 +531,7 @@ dependencies = [ "heapless 0.7.0", "ime-plugin-api", "keyboard", + "llio", "log", "log-server", "num-derive", diff --git a/services/com/src/api.rs b/services/com/src/api.rs index ccca19787..8a35ae1b0 100644 --- a/services/com/src/api.rs +++ b/services/com/src/api.rs @@ -65,13 +65,13 @@ pub(crate) enum Opcode { ShipMode, /// Is the battery charging? - //IsCharging, + IsCharging, /// Set the backlight brightness SetBackLight, /// Request charging - //RequestCharging, + RequestCharging, /// Erase a region of EC FLASH //FlashErase, diff --git a/services/com/src/lib.rs b/services/com/src/lib.rs index 1da563343..0d9bf0dcc 100644 --- a/services/com/src/lib.rs +++ b/services/com/src/lib.rs @@ -200,6 +200,26 @@ impl Com { ) ).map(|_| ()) } + + pub fn is_charging(&self) -> Result { + if let xous::Result::Scalar1(state) = + send_message(self.conn, + Message::new_blocking_scalar(Opcode::IsCharging.to_usize().unwrap(), 0, 0, 0, 0)).unwrap() { + if state != 0 { + Ok(true) + } else { + Ok(false) + } + } else { + Err(xous::Error::InternalError) + } + } + + pub fn request_charging(&self) -> Result<(), xous::Error> { + send_message(self.conn, + Message::new_scalar(Opcode::RequestCharging.to_usize().unwrap(), 0, 0, 0, 0 + )).map(|_| ()) + } // note to future self: add other event listener registrations (such as network events) here } diff --git a/services/com/src/main.rs b/services/com/src/main.rs index 613cd0bcb..a15a719fb 100644 --- a/services/com/src/main.rs +++ b/services/com/src/main.rs @@ -329,6 +329,14 @@ fn xmain() -> ! { } } ), + Some(Opcode::IsCharging) => msg_blocking_scalar_unpack!(msg, _, _, _, _, { + com.txrx(ComState::POWER_CHARGER_STATE.verb); + let result = com.wait_txrx(ComState::LINK_READ.verb, Some(STD_TIMEOUT)); + xous::return_scalar(msg.sender, result as usize).expect("couldn't return charging state"); + }), + Some(Opcode::RequestCharging) => msg_scalar_unpack!(msg, _, _, _, _, { + com.txrx(ComState::CHG_START.verb); + }), Some(Opcode::StandbyCurrent) => msg_blocking_scalar_unpack!(msg, _, _, _, _, { if let Some(i) = com.stby_current() { xous::return_scalar2(msg.sender, 1, i as usize).expect("couldn't return StandbyCurrent"); diff --git a/services/gam/Cargo.toml b/services/gam/Cargo.toml index f0924ab9d..790d4e93c 100644 --- a/services/gam/Cargo.toml +++ b/services/gam/Cargo.toml @@ -19,6 +19,7 @@ log-server = {path = "../log-server"} ticktimer-server = {path = "../ticktimer-server"} trng = {path = "../trng"} rtc = {path = "../rtc"} +llio = {path = "../llio"} xous = {path = "../../xous-rs"} xous-ipc = {path = "../../xous-ipc"} xous-names = {path = "../xous-names"} diff --git a/services/gam/src/status.rs b/services/gam/src/status.rs index 2509e1c3b..9dda17458 100644 --- a/services/gam/src/status.rs +++ b/services/gam/src/status.rs @@ -98,7 +98,13 @@ pub fn status_thread(canvas_gid_0: usize, canvas_gid_1: usize, canvas_gid_2: usi battstats_tv.draw_border = false; battstats_tv.margin = Point::new(0, 0); - let mut stats: BattStats; + // initialize to some "sane" mid-point defaults, so we don't trigger errors later on before the first real battstat reading comes + let mut stats = BattStats { + voltage: 3700, + soc: 50, + current: 0, + remaining_capacity: 650, + }; let mut last_time: u64 = ticktimer.elapsed_ms(); let mut stats_phase: usize = 0; let mut last_seconds: usize = ((last_time / 1000) % 60) as usize; @@ -123,6 +129,9 @@ pub fn status_thread(canvas_gid_0: usize, canvas_gid_1: usize, canvas_gid_2: usi let mut datetime: Option = None; let mut dt_pump_modulus = 15; + let mut charger_pump_modulus = 0; + let llio = llio::Llio::new(&xns).unwrap(); + let secs_interval; let batt_interval; if cfg!(feature = "slowstatus") { @@ -155,6 +164,21 @@ pub fn status_thread(canvas_gid_0: usize, canvas_gid_1: usize, canvas_gid_2: usi let elapsed_time = ticktimer.elapsed_ms(); let now_seconds: usize = ((elapsed_time / 1000) % 60) as usize; if (now_seconds / secs_interval) != (last_seconds / secs_interval) { + charger_pump_modulus += 1; + if charger_pump_modulus > 60 { + // once a minute confirm that the charger is in the right state. + charger_pump_modulus = 0; + if stats.soc < 95 || stats.remaining_capacity < 1000 { // only request if we aren't fully charged, either by SOC or capacity metrics + if (llio.adc_vbus().unwrap() as f64) * 0.005033 > 4.45 { // 4.45V is our threshold for deciding if a cable is present + // charging cable is present + if !com.is_charging().expect("couldn't check charging state") { + // not charging, but cable is present + log::info!("Charger present, but not currently charging. Automatically requesting charge start."); + com.request_charging().expect("couldn't send charge request"); + } + } + } + } dt_pump_modulus += 1; if dt_pump_modulus > 15 { dt_pump_modulus = 0;