From ed8f4d71cfc0d1032022db8c6c97f49521f2c032 Mon Sep 17 00:00:00 2001 From: bunnie Date: Mon, 28 Mar 2022 01:19:02 +0800 Subject: [PATCH] fix suspend/resume issue with RTC (forgot to mask out bits on control register) --- services/llio/src/api/rtc_api.rs | 4 ++-- services/llio/src/main.rs | 29 +++++++++++++++++++++-------- services/status/src/time.rs | 2 +- 3 files changed, 24 insertions(+), 11 deletions(-) diff --git a/services/llio/src/api/rtc_api.rs b/services/llio/src/api/rtc_api.rs index 09f5d3c7e..2a2c1b2de 100644 --- a/services/llio/src/api/rtc_api.rs +++ b/services/llio/src/api/rtc_api.rs @@ -217,9 +217,9 @@ pub fn rtc_to_seconds(settings: &[u8]) -> Option { // note 5 is skipped - this is weekdays, and is unused const MONTHS: usize = 6; const YEARS: usize = 7; - if (settings[CTL3] != (Control3::BATT_STD_BL_EN).bits()) // power switchover setting should be initialized + if ((settings[CTL3] & 0xE0) != (Control3::BATT_STD_BL_EN).bits()) // power switchover setting should be initialized || (settings[SECS] & 0x80 != 0) { // clock integrity should be guaranteed - log::error!("RTC is in an uninitialized state!"); + log::error!("RTC is in an uninitialized state!, {:?}", settings); return None; } // this is a secondary check -- I have seen RTC return non-sensical time results before diff --git a/services/llio/src/main.rs b/services/llio/src/main.rs index d0732af35..6b087636b 100644 --- a/services/llio/src/main.rs +++ b/services/llio/src/main.rs @@ -161,6 +161,7 @@ fn xmain() -> ! { let mut i2c = llio::I2c::new(&xns); let mut rtc_alarm_enabled = false; let mut wakeup_alarm_enabled = false; + let tt = ticktimer_server::Ticktimer::new().unwrap(); log::trace!("starting main loop"); loop { @@ -522,14 +523,26 @@ fn xmain() -> ! { // Note that we start the RTC at somewhere between 0-10 years, so in practice, a user can expect between 90-100 years // of continuous uptime service out of the RTC. let mut settings = [0u8; 8]; - match i2c.i2c_read(ABRTCMC_I2C_ADR, ABRTCMC_CONTROL3, &mut settings, None) { - Ok(llio::I2cStatus::ResponseReadOk) => {}, - _ => { - log::error!("Couldn't read seconds from RTC!"); - xous::return_scalar2(msg.sender, 0x8000_0000, 0).expect("couldn't return to caller"); - continue; - }, - }; + let mut success = false; + while !success { + // retry loop is necessary because this function can get called during "congested" periods + match i2c.i2c_read(ABRTCMC_I2C_ADR, ABRTCMC_CONTROL3, &mut settings, None) { + Ok(llio::I2cStatus::ResponseReadOk) => success = true, + Err(xous::Error::ServerQueueFull) => { + success = false; + // give it a short pause before trying again, to avoid hammering the I2C bus at busy times + tt.sleep_ms(38).unwrap(); + }, + _ => { + log::error!("Couldn't read seconds from RTC!"); + xous::return_scalar2(msg.sender, 0x8000_0000, 0).expect("couldn't return to caller"); + break; + }, + }; + } + if !success { + continue + } log::debug!("GetRtcValue regs: {:?}", settings); let total_secs = match rtc_to_seconds(&settings) { Some(s) => s, diff --git a/services/status/src/time.rs b/services/status/src/time.rs index 95cfd3d47..7556a2508 100644 --- a/services/status/src/time.rs +++ b/services/status/src/time.rs @@ -362,7 +362,7 @@ pub fn start_time_server() { } fn is_rtc_invalid(settings: &[u8]) -> bool { - (settings[CTL3] != (Control3::BATT_STD_BL_EN).bits()) // power switchover setting should be initialized + ((settings[CTL3] & 0xE0) != (Control3::BATT_STD_BL_EN).bits()) // power switchover setting should be initialized || ((settings[SECS] & 0x80) != 0) // clock integrity should be guaranteed || (to_binary(settings[SECS]) > 59) || (to_binary(settings[MINS]) > 59)