From 40224621442a349f1ec8ef2822511a6702b74214 Mon Sep 17 00:00:00 2001 From: bunnie Date: Fri, 23 Apr 2021 17:51:12 +0800 Subject: [PATCH] fix RTC wakeup alarm code --- emulation/renode.svd | 297 +++++++++++++++++++++++++++++++------ services/gam/src/status.rs | 1 + services/rtc/src/lib.rs | 8 +- services/rtc/src/main.rs | 21 ++- svd2utra/examples/soc.svd | 8 +- 5 files changed, 275 insertions(+), 60 deletions(-) diff --git a/emulation/renode.svd b/emulation/renode.svd index 27260bd11..794d2937d 100644 --- a/emulation/renode.svd +++ b/emulation/renode.svd @@ -3,7 +3,7 @@ litex SOC - + 8 32 @@ -1070,10 +1070,10 @@ register to disable individual events.]]> GIT_GITREV 0x0014 - 0x7edc9bd9 + 0x4878d3c3 32 @@ -1090,7 +1090,7 @@ the exact git version used to build this firmware.]]> is ``1``, then the repository this was built from has one additional commit beyond the tag indicated in `MAJOR`, `MINOR`, and `REVISION`.]]> 0x0018 - 0x1b + 0x1f 32 @@ -2015,14 +2015,8 @@ register advances the read pointer automatically.]]> 0 [0:0] 0 - - - - pause - 1 - [1:1] - 1 - + @@ -2150,8 +2144,218 @@ register to disable individual events.]]> - POWER + SUSRES 0xF0012000 + SUSRES + + + CONTROL + 0x0000 + 0x00 + 32 + + + reset + 0 + [0:0] + 0 + + + + pause + 1 + [1:1] + 1 + + + + load + 2 + [2:2] + 2 + + + + + + RESUME_TIME1 + + 0x0004 + 0x00 + 32 + + + resume_time + 31 + [31:0] + 0 + + + + + RESUME_TIME0 + + 0x0008 + 0x00 + 32 + + + resume_time + 31 + [31:0] + 0 + + + + + TIME1 + + 0x000c + 0x00 + 32 + + + time + 31 + [31:0] + 0 + + + + + TIME0 + + 0x0010 + 0x00 + 32 + + + time + 31 + [31:0] + 0 + + + + + STATUS + 0x0014 + 0x00 + 32 + + + paused + 0 + [0:0] + 0 + + + + + + STATE + 0x0018 + 0x00 + 32 + + + resume + 0 + [0:0] + 0 + + + + + + INTERRUPT + 0x001c + 0x00 + 32 + + + interrupt + 0 + [0:0] + 0 + + + + + + EV_STATUS + + 0x0020 + 0x00 + 32 + + + soft_int + 0 + [0:0] + 0 + + + + + + EV_PENDING + + 0x0024 + 0x00 + 32 + + + soft_int + 0 + [0:0] + 0 + + + + + + EV_ENABLE + + 0x0028 + 0x00 + 32 + + + soft_int + 0 + [0:0] + 0 + + + + + + + 0 + 0x2c + registers + + + susres + 9 + + + + POWER + 0xF0013000 POWER @@ -2295,12 +2499,12 @@ reset fashion]]> power - 9 + 10 SPINOR - 0xF0013000 + 0xF0014000 SPINOR @@ -2581,7 +2785,7 @@ checked]]> KEYBOARD - 0xF0014000 + 0xF0015000 KEYBOARD @@ -2834,12 +3038,12 @@ again until the interrupt is serviced.]]> keyboard - 10 + 11 KEYINJECT - 0xF0015000 + 0xF0016000 KEYINJECT @@ -2875,14 +3079,14 @@ inject too fast.]]> SEED - 0xF0016000 + 0xF0017000 SEED SEED1 0x0000 - 0x470716fd + 0x468ba085 32 @@ -2897,7 +3101,7 @@ inject too fast.]]> SEED0 0x0004 - 0x2218afde + 0x5f84de45 32 @@ -2917,7 +3121,7 @@ inject too fast.]]> KEYROM - 0xF0017000 + 0xF0018000 KEYROM @@ -2959,7 +3163,7 @@ inject too fast.]]> AUDIO - 0xF0018000 + 0xF0019000 AUDIO @@ -3326,12 +3530,12 @@ I2S_FORMAT.I2S_STANDARD is I2S standard]]> audio - 11 + 12 TRNG_KERNEL - 0xF0019000 + 0xF001A000 TRNG_KERNEL @@ -3462,12 +3666,12 @@ I2S_FORMAT.I2S_STANDARD is I2S standard]]> trng_kernel - 12 + 13 TRNG_SERVER - 0xF001A000 + 0xF001B000 TRNG_SERVER @@ -3794,12 +3998,12 @@ to max field size]]> trng_server - 13 + 14 TRNG - 0xF001B000 + 0xF001C000 TRNG @@ -4112,7 +4316,7 @@ VCCBRAM (V) = ``Value`` x 3 / 4096.]]> SHA512 - 0xF001C000 + 0xF001D000 SHA512 @@ -4602,12 +4806,12 @@ in bits]]> sha512 - 14 + 15 ENGINE - 0xF001D000 + 0xF001E000 ENGINE @@ -4831,12 +5035,12 @@ exactly reflects the binary layout of an Engine instruction.]]> engine - 15 + 16 JTAG - 0xF001E000 + 0xF001F000 JTAG @@ -4894,7 +5098,7 @@ exactly reflects the binary layout of an Engine instruction.]]> WDT - 0xF001F000 + 0xF0020000 WDT @@ -5049,12 +5253,12 @@ register to disable individual events.]]> wdt - 16 + 17 IDENTIFIER_MEM - 0xF0020000 + 0xF0021000 IDENTIFIER_MEM @@ -5081,7 +5285,7 @@ register to disable individual events.]]> TIMER0 - 0xF0021000 + 0xF0022000 TIMER0 @@ -5293,22 +5497,23 @@ register to disable individual events.]]> - + - + - - - + + + + - - + + - + \ No newline at end of file diff --git a/services/gam/src/status.rs b/services/gam/src/status.rs index 9f2834258..31cdf7e1e 100644 --- a/services/gam/src/status.rs +++ b/services/gam/src/status.rs @@ -115,6 +115,7 @@ pub fn status_thread(canvas_gid_0: usize, canvas_gid_1: usize, canvas_gid_2: usi com.req_batt_stats().expect("Can't get battery stats from COM"); let mut rtc = rtc::Rtc::new(&xns).unwrap(); + rtc.clear_wakeup_alarm().unwrap(); // clear any wakeup alarm state, if it was set rtc.hook_rtc_callback(dt_callback).unwrap(); let mut datetime: Option = None; let mut dt_pump_modulus = 15; diff --git a/services/rtc/src/lib.rs b/services/rtc/src/lib.rs index ce6190d04..891c0cf8a 100644 --- a/services/rtc/src/lib.rs +++ b/services/rtc/src/lib.rs @@ -74,23 +74,23 @@ impl Rtc { /// wakeup alarm will force the system on if it is off, but does not trigger an interrupt on the CPU pub fn set_wakeup_alarm(&self, seconds_from_now: u8) -> Result<(), xous::Error> { send_message(self.conn, - Message::new_scalar(Opcode::SetWakeupAlarm.to_usize().unwrap(), seconds_from_now as _, 0, 0, 0) + Message::new_blocking_scalar(Opcode::SetWakeupAlarm.to_usize().unwrap(), seconds_from_now as _, 0, 0, 0) ).map(|_|()) } pub fn clear_wakeup_alarm(&self) -> Result<(), xous::Error> { send_message(self.conn, - Message::new_scalar(Opcode::ClearWakeupAlarm.to_usize().unwrap(), 0, 0, 0, 0) + Message::new_blocking_scalar(Opcode::ClearWakeupAlarm.to_usize().unwrap(), 0, 0, 0, 0) ).map(|_|()) } /// the rtc alarm will not turn the system on, but it will trigger an interrupt on the CPU pub fn set_rtc_alarm(&self, seconds_from_now: u8) -> Result<(), xous::Error> { send_message(self.conn, - Message::new_scalar(Opcode::SetRtcAlarm.to_usize().unwrap(), seconds_from_now as _, 0, 0, 0) + Message::new_blocking_scalar(Opcode::SetRtcAlarm.to_usize().unwrap(), seconds_from_now as _, 0, 0, 0) ).map(|_|()) } pub fn clear_rtc_alarm(&self) -> Result<(), xous::Error> { send_message(self.conn, - Message::new_scalar(Opcode::ClearRtcAlarm.to_usize().unwrap(), 0, 0, 0, 0) + Message::new_blocking_scalar(Opcode::ClearRtcAlarm.to_usize().unwrap(), 0, 0, 0, 0) ).map(|_|()) } } diff --git a/services/rtc/src/main.rs b/services/rtc/src/main.rs index da9154edc..4c842fe29 100644 --- a/services/rtc/src/main.rs +++ b/services/rtc/src/main.rs @@ -6,7 +6,7 @@ mod api; use num_traits::{FromPrimitive, ToPrimitive}; use xous_ipc::Buffer; use api::{Return, Opcode, DateTime}; -use xous::msg_scalar_unpack; +use xous::{msg_scalar_unpack, msg_blocking_scalar_unpack}; use core::sync::atomic::{AtomicU32, Ordering}; static CB_TO_MAIN_CONN: AtomicU32 = AtomicU32::new(0); @@ -360,6 +360,9 @@ mod implementation { } xous::yield_slice(); } + while self.llio.poll_i2c_busy().unwrap() { + xous::yield_slice(); + } true } @@ -367,15 +370,19 @@ mod implementation { pub fn wakeup_alarm(&mut self, seconds: u8) { self.wakeup_alarm_enabled = true; + log::trace!("wakeup: switchover"); // make sure battery switchover is enabled, otherwise we won't keep time when power goes off self.blocking_i2c_write2([ABRTCMC_CONTROL3, (Control3::BATT_STD_BL_EN).bits()]); + log::trace!("wakeup: timerb_clk"); // set clock units to 1 second, output pulse length to ~218ms self.blocking_i2c_write2([ABRTCMC_TIMERB_CLK, (TimerClk::CLK_1_S | TimerClk::PULSE_218_MS).bits()]); + log::trace!("wakeup: timerb"); // program elapsed time self.blocking_i2c_write2([ABRTCMC_TIMERB, seconds]); + log::trace!("wakeup: b_int"); // enable timerb countdown interrupt, also clears any prior interrupt flag let mut control2 = (Control2::COUNTDOWN_B_INT).bits(); if self.rtc_alarm_enabled { @@ -383,6 +390,7 @@ mod implementation { } self.blocking_i2c_write2([ABRTCMC_CONTROL2, control2]); + log::trace!("wakeup: config"); // turn on the timer proper -- the system will wakeup in 5..4..3.... let mut config = (Config::CLKOUT_DISABLE | Config::TIMER_B_ENABLE).bits(); if self.rtc_alarm_enabled { @@ -432,7 +440,7 @@ mod implementation { // turn on the timer proper -- interrupt in 5..4..3.... let mut config = (Config::CLKOUT_DISABLE | Config::TIMER_A_COUNTDWN | Config::TIMERA_SECONDS_INT_PULSED).bits(); if self.wakeup_alarm_enabled { - config |= (Config::TIMER_B_ENABLE | Config::TIMERB_INT_PULSED).bits(); + config |= (Config::TIMER_B_ENABLE).bits(); } self.blocking_i2c_write2([ABRTCMC_CONFIG, config]); } @@ -644,16 +652,17 @@ fn xmain() -> ! { } log::trace!("RequestDateTime completed"); } - Some(Opcode::SetWakeupAlarm) => msg_scalar_unpack!(msg, delay, _, _, _, { + Some(Opcode::SetWakeupAlarm) => msg_blocking_scalar_unpack!(msg, delay, _, _, _, { rtc.wakeup_alarm(delay as u8); // this will block until finished, no callbacks used + xous::return_scalar(msg.sender, 0).expect("couldn't return to caller"); }), - Some(Opcode::ClearWakeupAlarm) => msg_scalar_unpack!(msg, _, _, _, _, { + Some(Opcode::ClearWakeupAlarm) => msg_blocking_scalar_unpack!(msg, _, _, _, _, { rtc.clear_wakeup_alarm(); // blocks until transaction is finished }), - Some(Opcode::SetRtcAlarm) => msg_scalar_unpack!(msg, delay, _, _, _, { + Some(Opcode::SetRtcAlarm) => msg_blocking_scalar_unpack!(msg, delay, _, _, _, { rtc.rtc_alarm(delay as u8); // this will block until finished, no callbacks used }), - Some(Opcode::ClearRtcAlarm) => msg_scalar_unpack!(msg, _, _, _, _, { + Some(Opcode::ClearRtcAlarm) => msg_blocking_scalar_unpack!(msg, _, _, _, _, { rtc.clear_rtc_alarm(); // blocks until transaction is finished }), None => { diff --git a/svd2utra/examples/soc.svd b/svd2utra/examples/soc.svd index 52f10e91c..794d2937d 100644 --- a/svd2utra/examples/soc.svd +++ b/svd2utra/examples/soc.svd @@ -3,7 +3,7 @@ litex SOC - + 8 32 @@ -1104,7 +1104,7 @@ beyond the tag indicated in `MAJOR`, `MINOR`, and `REVISION`.]]> GIT_DIRTY 0x001c - 0x01 + 0x00 32 @@ -3086,7 +3086,7 @@ inject too fast.]]> SEED1 0x0000 - 0x5073fcc9 + 0x468ba085 32 @@ -3101,7 +3101,7 @@ inject too fast.]]> SEED0 0x0004 - 0x627a019a + 0x5f84de45 32