From 6677d798344e780bb2ac8f1ced0444d1af6eb275 Mon Sep 17 00:00:00 2001 From: bunnie Date: Thu, 18 Mar 2021 16:22:08 +0800 Subject: [PATCH] initial RTC framework --- Cargo.lock | 14 ++ Cargo.toml | 2 + services/rtc/Cargo.toml | 22 +++ services/rtc/src/api.rs | 73 +++++++ services/rtc/src/lib.rs | 22 +++ services/rtc/src/main.rs | 404 +++++++++++++++++++++++++++++++++++++++ xous-rs/src/names.rs | 1 + 7 files changed, 538 insertions(+) create mode 100644 services/rtc/Cargo.toml create mode 100644 services/rtc/src/api.rs create mode 100644 services/rtc/src/lib.rs create mode 100644 services/rtc/src/main.rs diff --git a/Cargo.lock b/Cargo.lock index eb688aa3d..89dbe8fc5 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1072,6 +1072,20 @@ dependencies = [ "syn 1.0.60", ] +[[package]] +name = "rtc" +version = "0.1.0" +dependencies = [ + "bitflags", + "heapless", + "log", + "log-server", + "ticktimer-server", + "utralib", + "xous", + "xous-names", +] + [[package]] name = "rustc-hash" version = "1.1.0" diff --git a/Cargo.toml b/Cargo.toml index 73b1e61b1..54c636f49 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -27,6 +27,7 @@ members = [ "services/ime-plugin-api", "services/content-plugin-api", "services/llio", + "services/rtc", "services/trng-tester", ] default-members = [ @@ -50,6 +51,7 @@ default-members = [ "services/content-plugin-api", "services/shellchat", "services/llio", + "services/rtc", "services/trng-tester", ] diff --git a/services/rtc/Cargo.toml b/services/rtc/Cargo.toml new file mode 100644 index 000000000..3d0a6efd6 --- /dev/null +++ b/services/rtc/Cargo.toml @@ -0,0 +1,22 @@ +[package] +name = "rtc" +version = "0.1.0" +authors = ["bunnie "] +edition = "2018" +description = "RTC server" + +[dependencies] +xous = { path = "../../xous-rs" } +heapless = "0.5.6" +log-server = { path = "../log-server" } +ticktimer-server = { path = "../ticktimer-server" } +xous-names = { path = "../xous-names" } +log = "0.4" +bitflags = "1.2.1" + +[target.'cfg(not(any(windows,unix)))'.dependencies] +utralib = { path = "../../utralib"} + +[features] +debugprint = [] +default = [] # "debugprint" diff --git a/services/rtc/src/api.rs b/services/rtc/src/api.rs new file mode 100644 index 000000000..e7102cef8 --- /dev/null +++ b/services/rtc/src/api.rs @@ -0,0 +1,73 @@ +use xous::{Message, ScalarMessage}; + +pub enum Weekday { + Sunday, + Monday, + Tuesday, + Wednesday, + Thursday, + Friday, + Saturday, +} + +pub enum TimeUnits { + Seconds, + Minutes, + Hours, +} + +pub struct DateTime { + pub seconds: u8, + pub minutes: u8, + pub hours: u8, + pub days: u8, + pub months: u8, + pub years: u8, + pub weekday: Weekday, +} + +#[allow(dead_code)] +#[derive(Debug)] +pub enum Opcode { + /// Get datetime. This will be accurate to +/-2s, as the RTC is updated from hardware + /// once a second. + GetDateTime, + + /// sets the datetime + SetDateTime(DateTime), + + /// sets a wake-up alarm. This forces the SoC into power-on state, if it happens to be off. + /// primarily used to trigger cold reboots, but could have other reasons + SetWakeupAlarm(u8, TimeUnits), + + /// clear any wakeup alarms that have been set + ClearWakeupAlarm, +} + +impl core::convert::TryFrom<& Message> for Opcode { + type Error = &'static str; + fn try_from(message: & Message) -> Result { + match message { + Message::BlockingScalar(m) => match m.id { + 0 => Ok(Opcode::GetTrng(m.arg1)), + _ => Err("TRNG api: unknown BlockingScalar ID"), + }, + _ => Err("TRNG api: unhandled message type"), + } + } +} + +impl Into for Opcode { + fn into(self) -> Message { + match self { + Opcode::GetTrng(count) => Message::BlockingScalar(ScalarMessage { + id: 0, + arg1: count.into(), + arg2: 0, + arg3: 0, + arg4: 0, + }), + // _ => panic!("TRNG api: Opcode type not handled by Into(), refer to helper method"), + } + } +} diff --git a/services/rtc/src/lib.rs b/services/rtc/src/lib.rs new file mode 100644 index 000000000..45f98edcb --- /dev/null +++ b/services/rtc/src/lib.rs @@ -0,0 +1,22 @@ +#![cfg_attr(target_os = "none", no_std)] + +pub mod api; +use xous::{CID, send_message}; + +pub fn get_u32(cid: CID) -> Result { + let response = send_message(cid, api::Opcode::GetTrng(1).into())?; + if let xous::Result::Scalar2(trng, _) = response { + Ok(trng as u32) + } else { + panic!("unexpected return value: {:#?}", response); + } +} + +pub fn get_u64(cid: CID) -> Result { + let response = send_message(cid, api::Opcode::GetTrng(2).into())?; + if let xous::Result::Scalar2(lo, hi) = response { + Ok( lo as u64 | ((hi as u64) << 32) ) + } else { + panic!("unexpected return value: {:#?}", response); + } +} diff --git a/services/rtc/src/main.rs b/services/rtc/src/main.rs new file mode 100644 index 000000000..bc5c4b123 --- /dev/null +++ b/services/rtc/src/main.rs @@ -0,0 +1,404 @@ +#![cfg_attr(target_os = "none", no_std)] +#![cfg_attr(target_os = "none", no_main)] + +mod api; +use api::*; + +use core::convert::TryFrom; + +use log::{error, info}; + + +#[cfg(target_os = "none")] +mod implementation { + use log::info; + #[macro_use] + use bitflags::*; + + const ABRTCMC_I2C_ADR: u8 = 0x68; + const ABRTCMC_CONTROL1: u8 = 0x00; + bitflags! { + pub struct Control1: u8 { + const CORRECTION_INT = 0b0000_0001; + const ALARM_INT = 0b0000_0010; + const SECONDS_INT = 0b0000_0100; + const HR_MODE_12 = 0b0000_1000; + const SOFT_RESET = 0b0001_0000; + const STOP = 0b0010_0000; + } + } + + const ABRTCMC_CONTROL2: u8 = 0x01; + bitflags! { + pub struct Control2: u8 { + const COUNTDOWN_B_INT = 0b0000_0001; + const COUNTDOWN_A_INT = 0b0000_0010; + const WATCHDOG_A_INT = 0b0000_0100; + const ALARM_HAPPENED = 0b0000_1000; + const SECONDS_HAPPENED= 0b0001_0000; + const COUNTB_HAPPENED = 0b0010_0000; + const COUNTA_HAPPENED = 0b0100_0000; + const WATCHA_HAPPENED = 0b1000_0000; + } + } + + const ABRTCMC_CONTROL3: u8 = 0x02; + bitflags! { + pub struct Control3: u8 { + const BATTLOW_INT = 0b0000_0001; + const BATTSWITCH_INT = 0b0000_0010; + const BATTLOW_STAT = 0b0000_0100; + const BATTSW_HAPPENED = 0b0000_1000; + + const BATT_STD_BL_EN = 0b0000_0000; + const BATT_DIR_BL_EN = 0b0010_0000; + const BATT_DIS_BL_EN = 0b0100_0000; + const BATT_STD_BL_DIS = 0b1000_0000; + const BATT_DI_BL_DIS = 0b1110_0000; + } + } + + const ABRTCMC_SECONDS: u8 = 0x3; + bitflags! { + pub struct Seconds: u8 { + const SECONDS_BCD = 0b0111_1111; + const CORRUPTED = 0b1000_0000; + } + } + + const ABRTCMC_MINUTES: u8 = 0x4; + // no bitflags, minutes are BCD whole register + + const ABRTCMC_HOURS: u8 = 0x5; + bitflags! { + pub struct Hours: u8 { + const HR12_HOURS_BCD = 0b0001_1111; + const HR12_PM_FLAG = 0b0010_0000; + const HR24_HOURS_BCD = 0b0011_1111; + } + } + + const ABRTCMC_DAYS: u8 = 0x6; + // no bitflags, days are BCD whole register + + const ABRTCMC_WEEKDAYS: u8 = 0x7; + bitflags! { + pub struct Weekdays: u8 { + const SUNDAY = 0b000; + const MONDAY = 0b001; + const TUESDAY = 0b010; + const WEDNESDAY= 0b011; + const THURSDAY = 0b100; + const FRIDAY = 0b101; + const SATURDAY = 0b110; + } + } + + const ABRTCMC_MONTHS: u8 = 0x8; + bitflags! { + pub struct Months: u8 { // BCD "months" + const JANUARY = 0b0_0001; + const FEBRUARY = 0b0_0010; + const MARCH = 0b0_0011; + const APRIL = 0b0_0100; + const MAY = 0b0_0101; + const JUNE = 0b0_0110; + const JULY = 0b0_0111; + const AUGUST = 0b0_1000; + const SEPTEMBER = 0b0_1001; + const OCTOBER = 0b1_0000; + const NOVEMBER = 0b1_0001; + const DECEMBER = 0b1_0010; + } + } + + const ABRTCMC_YEARS: u8 = 0x9; + // no bitflags, years are 00-99 in BCD format + + const ABRTCMC_MINUTE_ALARM: u8 = 0xA; + const ABRTCMC_HOUR_ALARM: u8 = 0xB; + const ABRTCMC_DAY_ALARM: u8 = 0xC; + const ABRTCMC_WEEKDAY_ALARM: u8 = 0xD; + bitflags! { + pub struct Alarm: u8 { + const ENABLE = 0b1000_0000; + // all others code minute/hour/day/weekday in BCD LSBs + const HR12_PM_FLAG = 0b0010_0000; // only used in hours alarm, 12-hour mode + } + } + + const ABRTCMC_CONFIG: u8 = 0xF; + bitflags! { + pub struct Config: u8 { + const TIMER_B_ENABLE = 0b0000_0001; + const TIMER_A_WATCHDOG = 0b0000_0100; + const TIMER_A_COUNTDWN = 0b0000_0010; + const TIMER_A_DISABLE = 0b0000_0000; + const TIMER_A_DISABLE2 = 0b0000_0110; + + const CLKOUT_32768_HZ = 0b0000_0000; + const CLKOUT_16384_HZ = 0b0000_1000; + const CLKOUT_8192_HZ = 0b0001_0000; + const CLKOUT_4096_HZ = 0b0001_1000; + const CLKOUT_1024_HZ = 0b0010_0000; + const CLKOUT_32_HZ = 0b0010_1000; + const CLKOUT_1_HZ = 0b0011_0000; + const CLKOUT_DISABLE = 0b0011_1000; + + const TIMERB_INT_PULSED = 0b0100_0000; + const TIMERA_SECONDS_INT_PULSED = 0b1000_0000; + } + } + + const ABRTCMC_TIMERA_CLK: u8 = 0x10; + const ABRTCMC_TIMERB_CLK: u8 = 0x12; + bitflags! { + pub struct TimerClk: u8 { + const CLK_3600_S = 0b0000_0100; + const CLK_60_S = 0b0000_0011; + const CLK_1_S = 0b0000_0010; + const CLK_64_HZ = 0b0000_0001; // 15.625ms + const CLK_4096_HZ = 0b0000_0000; // 0.2441ms + + const PULSE_46_MS = 0b0000_0000; + const PULSE_62_MS = 0b0001_0000; + const PULSE_78_MS = 0b0010_0000; + const PULSE_93_MS = 0b0011_0000; + const PULSE_125_MS = 0b0100_0000; + const PULSE_156_MS = 0b0101_0000; + const PULSE_187_MS = 0b0110_0000; + const PULSE_218_MS = 0b0111_0000; + } + } + + const ABRTCMC_TIMERA: u8 = 0x11; + // no bitflags, register is timer period in seconds, and the period is N / (source clock frequency) + const ABRTCMC_TIMERB: u8 = 0x13; + // no bitflags, register is timer period in seconds, and the period is N / (source clock frequency) + + /// convert binary to BCD + fn to_bcd(binary: u8) -> u8 { + let mut lsd: u8 = binary % 10; + if lsd > 9 { + lsd = 9; + } + let mut msd: u8 = binary / 10; + if msd > 9 { + msd = 9; + } + + (msd << 4) | lsd + } + + fn to_binary(bcd: u8) -> u8 { + (bcd & 0xf) + ((bcd >> 4) * 10) + } + + fn to_weekday(bcd: u8) -> Weekdays { + match bcd { + 0 => Weekdays::SUNDAY, + 1 => Weekdays::MONDAY, + 2 => Weekdays::TUESDAY, + 3 => Weekdays::WEDNESDAY, + 4 => Weekdays::THURSDAY, + 5 => Weekdays::FRIDAY, + 6 => Weekdays::SATURDAY, + _ => Weekdays::SUNDAY, + } + } + + pub struct Rtc { + pub seconds: u8, + pub minutes: u8, + pub hours: u8, + pub days: u8, + pub months: u8, + pub years: u8, + pub weekday: Weekdays, + updated_ticks: u64, + } + + impl Rtc { + pub fn new() -> Rtc { + info!("RTC: hardware initialized"); + + Rtc { + seconds: 0, + minutes: 0, + hours: 0, + days: 0, + months: 0, + years: 0, + weekday: Weekdays::SUNDAY, + updated_ticks: 0, + } + } + + + /// we only support 24 hour mode + /// TODO: sanity check arguments + /// TODO: write accesses should happen in a single block, to guarante atomicity of the operation + pub fn rtc_set(&mut self, secs: u8, mins: u8, hours: u8, days: u8, months: u8, years: u8, d: Weekdays) -> bool { + let mut txbuf: [u8; 2]; + + // make sure battery switchover is enabled, otherwise we won't keep time when power goes off + txbuf = [ABRTCMC_CONTROL3, (Control3::BATT_STD_BL_EN).bits()]; + i2c_controller(&self.p, ABRTCMC_I2C_ADR, Some(&txbuf), None, I2C_TIMEOUT); + + txbuf = [ABRTCMC_SECONDS, to_bcd(secs)]; + i2c_controller(&self.p, ABRTCMC_I2C_ADR, Some(&txbuf), None, I2C_TIMEOUT); + self.updated_ticks = get_ticks(&self.p); + self.seconds = secs; + + txbuf = [ABRTCMC_MINUTES, to_bcd(mins)]; + i2c_controller(&self.p, ABRTCMC_I2C_ADR, Some(&txbuf), None, I2C_TIMEOUT); + self.minutes = mins; + + txbuf = [ABRTCMC_HOURS, to_bcd(hours)]; + i2c_controller(&self.p, ABRTCMC_I2C_ADR, Some(&txbuf), None, I2C_TIMEOUT); + self.hours = hours; + + txbuf = [ABRTCMC_DAYS, to_bcd(days)]; + i2c_controller(&self.p, ABRTCMC_I2C_ADR, Some(&txbuf), None, I2C_TIMEOUT); + self.days = days; + + txbuf = [ABRTCMC_MONTHS, to_bcd(months)]; + i2c_controller(&self.p, ABRTCMC_I2C_ADR, Some(&txbuf), None, I2C_TIMEOUT); + self.months = months; + + txbuf = [ABRTCMC_YEARS, to_bcd(years)]; + i2c_controller(&self.p, ABRTCMC_I2C_ADR, Some(&txbuf), None, I2C_TIMEOUT); + self.years = years; + + txbuf = [ABRTCMC_WEEKDAYS, d.bits()]; + i2c_controller(&self.p, ABRTCMC_I2C_ADR, Some(&txbuf), None, I2C_TIMEOUT); + self.weekday = d; + + true // sanity check args would return false on fail + } + + pub fn rtc_update(&mut self) { + let txbuf: [u8; 1]; + let mut rxbuf: [u8; 7] = [0; 7]; + + // only update from RTC if more than 1 second has passed since the last update + if get_ticks(&self.p) - self.updated_ticks > 1000 { + // read as a single block to make the time readout atomic + txbuf = [ABRTCMC_SECONDS]; + i2c_controller(&self.p, ABRTCMC_I2C_ADR, Some(&txbuf), Some(&mut rxbuf), I2C_TIMEOUT); + + self.seconds = to_binary(rxbuf[0] & 0x7f); + self.minutes = to_binary(rxbuf[1] & 0x7f); + self.hours = to_binary(rxbuf[2] & 0x3f); + self.days = to_binary(rxbuf[3] & 0x3f); + self.weekday = to_weekday(rxbuf[4] & 0x7f); + self.months = to_binary(rxbuf[5] & 0x1f); + self.years = to_binary(rxbuf[6]); + + self.updated_ticks = get_ticks(&self.p); + } + } + + /// testing-only routine -- wakeup self after designated number of seconds + pub fn wakeup_alarm(&mut self, seconds: u8) { + let mut txbuf: [u8; 2]; + + // make sure battery switchover is enabled + txbuf = [ABRTCMC_CONTROL3, (Control3::BATT_STD_BL_EN).bits()]; + i2c_controller(&self.p, ABRTCMC_I2C_ADR, Some(&txbuf), None, I2C_TIMEOUT); + + // set clock units to 1 second, output pulse length to ~218ms + txbuf = [ABRTCMC_TIMERB_CLK, (TimerClk::CLK_1_S | TimerClk::PULSE_218_MS).bits()]; + i2c_controller(&self.p, ABRTCMC_I2C_ADR, Some(&txbuf), None, I2C_TIMEOUT); + + // program elapsed time + txbuf = [ABRTCMC_TIMERB, seconds]; + i2c_controller(&self.p, ABRTCMC_I2C_ADR, Some(&txbuf), None, I2C_TIMEOUT); + + // enable timerb countdown interrupt, also clears any prior interrupt flag + txbuf = [ABRTCMC_CONTROL2, (Control2::COUNTDOWN_B_INT).bits()]; + i2c_controller(&self.p, ABRTCMC_I2C_ADR, Some(&txbuf), None, I2C_TIMEOUT); + + // turn on the timer proper -- the system will restart in 5...4..3.... + txbuf = [ABRTCMC_CONFIG, (Config::TIMER_B_ENABLE | Config::CLKOUT_DISABLE).bits()]; + i2c_controller(&self.p, ABRTCMC_I2C_ADR, Some(&txbuf), None, I2C_TIMEOUT); + } + + pub fn clear_alarm(&mut self) { + // turn off RTC wakeup timer, in case previously set + let mut txbuf : [u8; 2] = [ABRTCMC_CONFIG, (Config::CLKOUT_DISABLE).bits()]; + i2c_controller(&self.p, ABRTCMC_I2C_ADR, Some(&txbuf), None, I2C_TIMEOUT); + + // clear all interrupts and flags + txbuf = [ABRTCMC_CONTROL2, 0]; + i2c_controller(&self.p, ABRTCMC_I2C_ADR, Some(&txbuf), None, I2C_TIMEOUT); + } + } +} + +// a stub to try to avoid breaking hosted mode for as long as possible. +#[cfg(not(target_os = "none"))] +mod implementation { + use log::info; + + pub struct Rtc { + pub seconds: u8, + pub minutes: u8, + pub hours: u8, + pub days: u8, + pub months: u8, + pub years: u8, + pub weekday: Weekdays, + updated_ticks: u64, + } + + impl Rtc { + pub fn new() -> Rtc { + Rtc { + seconds: 0, + minutes: 0, + hours: 0, + days: 0, + months: 0, + years: 0, + weekday: Weekdays::SUNDAY, + updated_ticks: 0, + } + } + + } +} + +#[xous::xous_main] +fn xmain() -> ! { + use crate::implementation::Rtc; + + log_server::init_wait().unwrap(); + info!("RTC: my PID is {}", xous::process::id()); + + let rtc_sid = xous_names::register_name(xous::names::SERVER_NAME_RTC).expect("RTC: can't register server"); + info!("RTC: registered with NS -- {:?}", rtc_sid); + + #[cfg(target_os = "none")] + let rtc = Rtc::new(); + + #[cfg(not(target_os = "none"))] + let mut rtc = Rtc::new(); + + info!("RTC: ready to accept requests"); + + loop { + let envelope = xous::receive_message(rtc_sid).unwrap(); + if let Ok(opcode) = Opcode::try_from(&envelope.body) { + match opcode { + Opcode::GetRtc(count) => { + let val: [u32; 2] = rtc.get_rtc(count); + xous::return_scalar2(envelope.sender, val[0] as _, val[1] as _) + .expect("RTC: couldn't return GetRtc request"); + }, + } + } else { + error!("KBD: couldn't convert opcode"); + } + } +} diff --git a/xous-rs/src/names.rs b/xous-rs/src/names.rs index 394d73af2..071ea843b 100644 --- a/xous-rs/src/names.rs +++ b/xous-rs/src/names.rs @@ -16,6 +16,7 @@ pub const SERVER_NAME_STATUS: &str = "_Status bar manager_"; pub const SERVER_NAME_IME_FRONT: &str = "_IME front end_"; pub const SERVER_NAME_IME_PLUGIN_SHELL: &str = "_IME shell plugin_"; pub const SERVER_NAME_SHELLCHAT: &str = "_Shell chat application_"; +pub const SERVER_NAME_RTC: &str = "_Real time clock application server_"; pub const SERVER_NAME_FCCAGENT: &str = "_Agent for EMC Testing_"; pub const SERVER_NAME_BENCHMARK: &str= "_Benchmark target_";