add status thread mechanism to check charger state

This commit is contained in:
bunnie 2021-05-16 16:30:58 +08:00
parent 87b6d9b6d7
commit 5d4e716e1b
6 changed files with 57 additions and 3 deletions

1
Cargo.lock generated
View file

@ -531,6 +531,7 @@ dependencies = [
"heapless 0.7.0",
"ime-plugin-api",
"keyboard",
"llio",
"log",
"log-server",
"num-derive",

View file

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

View file

@ -200,6 +200,26 @@ impl Com {
)
).map(|_| ())
}
pub fn is_charging(&self) -> Result<bool, xous::Error> {
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
}

View file

@ -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");

View file

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

View file

@ -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<rtc::DateTime> = 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;