fix suspend/resume issue with RTC (forgot to mask out bits on control register)

This commit is contained in:
bunnie 2022-03-28 01:19:02 +08:00
parent ee695d0181
commit ed8f4d71cf
3 changed files with 24 additions and 11 deletions

View file

@ -217,9 +217,9 @@ pub fn rtc_to_seconds(settings: &[u8]) -> Option<u64> {
// note 5 is skipped - this is weekdays, and is unused // note 5 is skipped - this is weekdays, and is unused
const MONTHS: usize = 6; const MONTHS: usize = 6;
const YEARS: usize = 7; 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 || (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; return None;
} }
// this is a secondary check -- I have seen RTC return non-sensical time results before // this is a secondary check -- I have seen RTC return non-sensical time results before

View file

@ -161,6 +161,7 @@ fn xmain() -> ! {
let mut i2c = llio::I2c::new(&xns); let mut i2c = llio::I2c::new(&xns);
let mut rtc_alarm_enabled = false; let mut rtc_alarm_enabled = false;
let mut wakeup_alarm_enabled = false; let mut wakeup_alarm_enabled = false;
let tt = ticktimer_server::Ticktimer::new().unwrap();
log::trace!("starting main loop"); log::trace!("starting main loop");
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 // 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. // of continuous uptime service out of the RTC.
let mut settings = [0u8; 8]; let mut settings = [0u8; 8];
match i2c.i2c_read(ABRTCMC_I2C_ADR, ABRTCMC_CONTROL3, &mut settings, None) { let mut success = false;
Ok(llio::I2cStatus::ResponseReadOk) => {}, while !success {
_ => { // retry loop is necessary because this function can get called during "congested" periods
log::error!("Couldn't read seconds from RTC!"); match i2c.i2c_read(ABRTCMC_I2C_ADR, ABRTCMC_CONTROL3, &mut settings, None) {
xous::return_scalar2(msg.sender, 0x8000_0000, 0).expect("couldn't return to caller"); Ok(llio::I2cStatus::ResponseReadOk) => success = true,
continue; 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); log::debug!("GetRtcValue regs: {:?}", settings);
let total_secs = match rtc_to_seconds(&settings) { let total_secs = match rtc_to_seconds(&settings) {
Some(s) => s, Some(s) => s,

View file

@ -362,7 +362,7 @@ pub fn start_time_server() {
} }
fn is_rtc_invalid(settings: &[u8]) -> bool { 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 || ((settings[SECS] & 0x80) != 0) // clock integrity should be guaranteed
|| (to_binary(settings[SECS]) > 59) || (to_binary(settings[SECS]) > 59)
|| (to_binary(settings[MINS]) > 59) || (to_binary(settings[MINS]) > 59)