begin debugging of the RTC

This commit is contained in:
bunnie 2021-04-08 04:38:01 +08:00
parent 45c90659e5
commit 0fa3d3c969
9 changed files with 172 additions and 8 deletions

1
Cargo.lock generated
View file

@ -1233,6 +1233,7 @@ dependencies = [
"num-derive",
"num-traits",
"rkyv 0.4.3",
"rtc",
"ticktimer-server",
"utralib",
"xous",

View file

@ -26,6 +26,7 @@ impl I2cStateMachine {
}
pub fn handler(&mut self) {
}
pub fn is_busy(&self) -> bool { false }
}
#[cfg(target_os = "none")]
pub struct I2cStateMachine {
@ -60,6 +61,7 @@ impl I2cStateMachine {
}
}
pub fn initiate(&mut self, transaction: I2cTransaction ) -> I2cStatus {
log::trace!("I2C initated with {:?}", transaction);
// sanity-check the bounds limits
if transaction.txlen > 258 || transaction.rxlen > 258 {
return I2cStatus::ResponseFormatError
@ -157,6 +159,7 @@ impl I2cStateMachine {
}
}
pub fn handler(&mut self) {
log::trace!("I2C handler called");
// check if the transaction had actually timed out
let now = self.ticktimer.elapsed_ms();
if now - self.timestamp > self.transaction.timeout_ms as u64 {
@ -172,6 +175,7 @@ impl I2cStateMachine {
match self.state {
I2cState::Write => {
log::trace!("I2C handler: WRITE");
if let Some(txbuf) = self.transaction.txbuf {
// check ack bit
if self.i2c_csr.rf(utra::i2c::STATUS_RXACK) != 1 {
@ -213,6 +217,7 @@ impl I2cStateMachine {
}
},
I2cState::Read => {
log::trace!("I2C handler: READ");
if let Some(mut rxbuf) = self.transaction.rxbuf {
if self.index > 0 {
// we are re-entering from a previous call, store the read value from the previous call
@ -230,6 +235,7 @@ impl I2cStateMachine {
}
self.index += 1;
} else {
log::trace!("I2C handler: read done {:?}", self.transaction);
self.report_read_done();
self.state = I2cState::Idle;
self.listener = None;

View file

@ -423,6 +423,7 @@ mod implementation {
use llio::api::*;
use log::{error, info};
#[derive(Copy, Clone, Debug)]
pub struct Llio {
}
pub fn log_init() -> *mut u32 { 0 as *mut u32 }
@ -511,7 +512,7 @@ fn xmain() -> ! {
let gpio_base = crate::implementation::log_init();
log_server::init_wait().unwrap();
log::set_max_level(log::LevelFilter::Info);
log::set_max_level(log::LevelFilter::Trace);
info!("my PID is {}", xous::process::id());
let xns = xous_names::XousNames::new().unwrap();

View file

@ -2,11 +2,13 @@
pub mod api;
use api::{Return, Opcode, DateTime}; // if you prefer to map the api into your local namespace
pub use api::*;
use api::{Return, Opcode};
use xous::{send_message, CID, Message};
use xous_ipc::Buffer;
use num_traits::ToPrimitive;
#[derive(Debug)]
pub struct Rtc {
conn: CID,
callback_sid: Option<xous::SID>,
@ -28,6 +30,7 @@ impl Rtc {
buf.lend(self.conn, Opcode::SetDateTime.to_u32().unwrap()).map(|_| ())
}
pub fn hook_rtc_callback(&mut self, cb: fn(DateTime)) -> Result<(), xous::Error> {
log::trace!("hooking rtc callback");
if unsafe{RTC_CB}.is_some() {
return Err(xous::Error::MemoryInUse)
}
@ -101,6 +104,7 @@ fn rtc_cb_server(sid0: usize, sid1: usize, sid2: usize, sid3: usize) {
let sid = xous::SID::from_u32(sid0 as u32, sid1 as u32, sid2 as u32, sid3 as u32);
loop {
let msg = xous::receive_message(sid).unwrap();
log::trace!("rtc callback got msg: {:?}", msg);
let buffer = unsafe { Buffer::from_memory_message(msg.body.memory_message().unwrap()) };
let response = buffer.to_original::<Return,_>().unwrap();
match response {

View file

@ -455,6 +455,7 @@ mod implementation {
#[cfg(not(target_os = "none"))]
mod implementation {
use log::info;
use crate::api::Weekday;
pub struct Rtc {
pub seconds: u8,
@ -463,12 +464,12 @@ mod implementation {
pub days: u8,
pub months: u8,
pub years: u8,
pub weekday: Weekdays,
pub weekday: Weekday,
updated_ticks: u64,
}
impl Rtc {
pub fn new() -> Rtc {
pub fn new(_xns: &xous_names::XousNames) -> Rtc {
Rtc {
seconds: 0,
minutes: 0,
@ -476,11 +477,19 @@ mod implementation {
days: 0,
months: 0,
years: 0,
weekday: Weekdays::SUNDAY,
weekday: Weekday::Sunday,
updated_ticks: 0,
}
}
pub fn rtc_set(&mut self, _secs: u8, _mins: u8, _hours: u8, _days: u8, _months: u8, _years: u8, _day: Weekday)
-> Result<bool, xous::Error> {
Ok(true)
}
pub fn rtc_get(&mut self) -> Result<(), xous::Error> { Ok(()) }
pub fn wakeup_alarm(&mut self, _seconds: u8) { }
pub fn clear_wakeup_alarm(&mut self) { }
pub fn rtc_alarm(&mut self, _seconds: u8) { }
pub fn clear_rtc_alarm(&mut self) { }
}
}
@ -502,7 +511,7 @@ fn xmain() -> ! {
let mut rtc = Rtc::new(&xns);
#[cfg(not(target_os = "none"))]
let mut rtc = Rtc::new();
let mut rtc = Rtc::new(&xns);
let ticktimer = ticktimer_server::Ticktimer::new().expect("can't connect to ticktimer");
let mut dt_cb_conns: [Option<CID>; 32] = [None; 32];
@ -538,6 +547,7 @@ fn xmain() -> ! {
}
}
}
log::trace!("rtc_set of {:?} successful", dt);
},
Some(Opcode::RegisterDateTimeCallback) => msg_scalar_unpack!(msg, sid0, sid1, sid2, sid3, {
let sid = xous::SID::from_u32(sid0 as _, sid1 as _, sid2 as _, sid3 as _);
@ -552,11 +562,14 @@ fn xmain() -> ! {
}
if !found {
log::error!("RegisterDateTimeCallback listener ran out of space registering callback");
} else {
log::trace!("RegisterDateTimeCallback completed");
}
}),
Some(Opcode::ResponseDateTime) => {
let incoming_buffer = unsafe { Buffer::from_memory_message(msg.body.memory_message().unwrap()) };
let dt = incoming_buffer.to_original::<DateTime, _>().unwrap();
log::trace!("ResponseDateTime received: {:?}", dt);
let ret = Return::ReturnDateTime(dt);
let outgoing_buf = Buffer::into_buf(ret).or(Err(xous::Error::InternalError)).unwrap();
for maybe_conn in dt_cb_conns.iter_mut() {
@ -587,6 +600,7 @@ fn xmain() -> ! {
}
}
}
log::trace!("RequestDateTime completed");
}
Some(Opcode::SetWakeupAlarm) => msg_scalar_unpack!(msg, delay, _, _, _, {
rtc.wakeup_alarm(delay as u8); // this will block until finished, no callbacks used

View file

@ -13,6 +13,7 @@ graphics-server = {path = "../graphics-server"}
heapless = "0.5.6"
ime-plugin-api = {path = "../ime-plugin-api"}
llio = {path = "../llio"}
rtc = {path = "../rtc"}
log = "0.4"
log-server = {path = "../log-server"}
ticktimer-server = {path = "../ticktimer-server"}

View file

@ -63,6 +63,7 @@ mod test; use test::*;
mod sleep; use sleep::*;
mod sensors; use sensors::*;
mod callback; use callback::*;
mod rtc_cmd; use rtc_cmd::*;
#[derive(Debug)]
pub struct CmdEnv {
@ -73,6 +74,7 @@ pub struct CmdEnv {
sleep_cmd: Sleep,
sensors_cmd: Sensors,
callback_cmd: CallBack,
rtc_cmd: RtcCmd,
}
impl CmdEnv {
pub fn new(xns: &xous_names::XousNames) -> CmdEnv {
@ -90,6 +92,7 @@ impl CmdEnv {
sleep_cmd: Sleep::new(),
sensors_cmd: Sensors::new(),
callback_cmd: CallBack::new(),
rtc_cmd: RtcCmd::new(&xns),
}
}
@ -104,6 +107,7 @@ impl CmdEnv {
&mut self.sleep_cmd,
&mut self.sensors_cmd,
&mut self.callback_cmd,
&mut self.rtc_cmd,
];
if let Some(cmdline) = maybe_cmdline {

View file

@ -0,0 +1,133 @@
use crate::{ShellCmdApi,CommonEnv};
use xous_ipc::String;
#[derive(Debug)]
pub struct RtcCmd {
rtc: rtc::Rtc,
}
impl RtcCmd {
pub fn new(xns: &xous_names::XousNames) -> Self {
let mut rtc = rtc::Rtc::new(&xns).unwrap();
RtcCmd {
rtc: rtc,
}
}
}
impl<'a> ShellCmdApi<'a> for RtcCmd {
cmd_api!(rtc);
fn process(&mut self, args: String::<1024>, _env: &mut CommonEnv) -> Result<Option<String::<1024>>, xous::Error> {
use core::fmt::Write;
let mut ret = String::<1024>::new();
let helpstring = "rtc options: set, get, hook";
let mut tokens = args.as_str().unwrap().split(' ');
if let Some(sub_cmd) = tokens.next() {
match sub_cmd {
"hook" => {
write!(ret, "{}", "Hooked RTC responder...").unwrap();
//self.rtc.hook_rtc_callback(dt_callback).unwrap();
}
"get" => {
write!(ret, "{}", "Requesting DateTime from RTC...").unwrap();
self.rtc.request_datetime().unwrap();
},
"set" => {
let mut success = true;
let mut hour: u8 = 0;
let mut min: u8 = 0;
let mut sec: u8 = 0;
let mut day: u8 = 0;
let mut month: u8 = 0;
let mut year: u8 = 0;
let mut weekday: rtc::Weekday = rtc::Weekday::Sunday;
if let Some(tok_str) = tokens.next() {
hour = if let Ok(n) = tok_str.parse::<u8>() { n } else { success = false; 0 }
} else {
success = false;
}
if let Some(tok_str) = tokens.next() {
min = if let Ok(n) = tok_str.parse::<u8>() { n } else { success = false; 0 }
} else {
success = false;
}
if let Some(tok_str) = tokens.next() {
sec = if let Ok(n) = tok_str.parse::<u8>() { n } else { success = false; 0 }
} else {
success = false;
}
if let Some(tok_str) = tokens.next() {
day = if let Ok(n) = tok_str.parse::<u8>() { n } else { success = false; 0 }
} else {
success = false;
}
if let Some(tok_str) = tokens.next() {
month = if let Ok(n) = tok_str.parse::<u8>() { n } else { success = false; 0 }
} else {
success = false;
}
if let Some(tok_str) = tokens.next() {
year = if let Ok(n) = tok_str.parse::<u8>() { n } else { success = false; 0 }
} else {
success = false;
}
if let Some(tok_str) = tokens.next() {
match tok_str {
"mon" => weekday = rtc::Weekday::Monday,
"tue" => weekday = rtc::Weekday::Tuesday,
"wed" => weekday = rtc::Weekday::Wednesday,
"thu" => weekday = rtc::Weekday::Thursday,
"fri" => weekday = rtc::Weekday::Friday,
"sat" => weekday = rtc::Weekday::Saturday,
"sun" => weekday = rtc::Weekday::Sunday,
_ => success = false,
}
} else {
success = false;
}
if !success {
write!(ret, "{}", "usage: rtc set hh mm ss DD MM YY day\n'day' is three-day code, eg. mon tue").unwrap();
} else {
let dt = rtc::DateTime {
seconds: sec,
minutes: min,
hours: hour,
days: day,
months: month,
years: year,
weekday,
};
write!(ret, "Setting {}:{}:{}, {}/{}/{}, {:?}", hour, min, sec, day, month, year, weekday).unwrap();
self.rtc.set_rtc(dt).unwrap();
}
},
_ => {
write!(ret, "{}", helpstring).unwrap();
}
}
} else {
write!(ret, "{}", helpstring).unwrap();
}
Ok(Some(ret))
}
}
static mut CB_CONN: Option<xous::CID> = None;
pub fn dt_callback(dt: rtc::DateTime) {
if let Some(conn) = unsafe{CB_CONN} {
log::info!("got datetime: {:?}", dt);
let buf = xous_ipc::Buffer::into_buf(dt).or(Err(xous::Error::InternalError)).unwrap();
buf.lend(conn, 0xdeadbeef).unwrap();
} else {
log::info!("callback not set, but got datetime: {:?}", dt);
}
}

View file

@ -237,7 +237,7 @@ pub(crate) const SERVER_NAME_SHELLCHAT: &str = "_Shell chat application_";
#[xous::xous_main]
fn xmain() -> ! {
log_server::init_wait().unwrap();
log::set_max_level(log::LevelFilter::Info);
log::set_max_level(log::LevelFilter::Trace);
info!("my PID is {}", xous::process::id());
let xns = xous_names::XousNames::new().unwrap();