mirror of
https://github.com/saymrwulf/xous-core.git
synced 2026-11-21 09:14:34 +00:00
add rtc test
This commit is contained in:
parent
7ac8b9100d
commit
549912c9d5
2 changed files with 216 additions and 60 deletions
|
|
@ -68,7 +68,8 @@ utralib = {path = "../../utralib"}
|
||||||
|
|
||||||
[features]
|
[features]
|
||||||
debugprint = []
|
debugprint = []
|
||||||
default = [] # "debugprint"
|
audio_tests = []
|
||||||
|
default = ["audio_tests"] # "debugprint"
|
||||||
|
|
||||||
[target.'cfg(any(windows,unix))'.dependencies]
|
[target.'cfg(any(windows,unix))'.dependencies]
|
||||||
debug-here = "*"
|
debug-here = "*"
|
||||||
|
|
|
||||||
|
|
@ -1,10 +1,12 @@
|
||||||
use crate::{ShellCmdApi,CommonEnv};
|
use crate::{ShellCmdApi,CommonEnv};
|
||||||
use xous_ipc::String;
|
use xous_ipc::String;
|
||||||
use xous::MessageEnvelope;
|
use xous::{MessageEnvelope, Message};
|
||||||
|
use llio::I2cStatus;
|
||||||
|
|
||||||
use codec::*;
|
use codec::*;
|
||||||
use spectrum_analyzer::{samples_fft_to_spectrum, FrequencyLimit};
|
use spectrum_analyzer::{samples_fft_to_spectrum, FrequencyLimit};
|
||||||
use spectrum_analyzer::windows::hann_window;
|
use spectrum_analyzer::windows::hann_window;
|
||||||
|
use rtc::{DateTime, Weekday};
|
||||||
|
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
pub struct Test {
|
pub struct Test {
|
||||||
|
|
@ -16,6 +18,10 @@ pub struct Test {
|
||||||
framecount: u32,
|
framecount: u32,
|
||||||
play_sample: f32, // count of play samples generated. in f32 to avoid int<->f32 conversions
|
play_sample: f32, // count of play samples generated. in f32 to avoid int<->f32 conversions
|
||||||
rec_sample: usize, // count of record samples recorded. in usize because we're not doing f32 wave table computations on this
|
rec_sample: usize, // count of record samples recorded. in usize because we're not doing f32 wave table computations on this
|
||||||
|
start_time: Option<DateTime>,
|
||||||
|
end_time: Option<DateTime>,
|
||||||
|
start_elapsed: Option<u64>,
|
||||||
|
end_elapsed: Option<u64>,
|
||||||
}
|
}
|
||||||
impl Test {
|
impl Test {
|
||||||
pub fn new(xns: &xous_names::XousNames) -> Self {
|
pub fn new(xns: &xous_names::XousNames) -> Self {
|
||||||
|
|
@ -28,15 +34,21 @@ impl Test {
|
||||||
xous::MemoryFlags::R | xous::MemoryFlags::W,
|
xous::MemoryFlags::R | xous::MemoryFlags::W,
|
||||||
).expect("couldn't allocate record buffer");
|
).expect("couldn't allocate record buffer");
|
||||||
|
|
||||||
|
let callback_conn = xns.request_connection_blocking(crate::SERVER_NAME_SHELLCHAT).unwrap();
|
||||||
|
|
||||||
Test {
|
Test {
|
||||||
codec,
|
codec,
|
||||||
recbuf,
|
recbuf,
|
||||||
state: 0,
|
state: 0,
|
||||||
callback_id: None,
|
callback_id: None,
|
||||||
callback_conn: xns.request_connection_blocking(crate::SERVER_NAME_SHELLCHAT).unwrap(),
|
callback_conn,
|
||||||
framecount: 0,
|
framecount: 0,
|
||||||
play_sample: 0.0,
|
play_sample: 0.0,
|
||||||
rec_sample: 0,
|
rec_sample: 0,
|
||||||
|
start_time: None,
|
||||||
|
end_time: None,
|
||||||
|
start_elapsed: None,
|
||||||
|
end_elapsed: None,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -60,6 +72,14 @@ impl<'a> ShellCmdApi<'a> for Test {
|
||||||
if let Some(sub_cmd) = tokens.next() {
|
if let Some(sub_cmd) = tokens.next() {
|
||||||
match sub_cmd {
|
match sub_cmd {
|
||||||
"factory" => {
|
"factory" => {
|
||||||
|
// force a specified time to make sure the elapsed time computation later on works
|
||||||
|
if !rtc_set(&mut env.llio, 0, 0, 10, 1, 6, 21) {
|
||||||
|
log::info!("{}|RTC|FAIL|SET|", SENTINEL);
|
||||||
|
}
|
||||||
|
|
||||||
|
self.start_elapsed = Some(env.ticktimer.elapsed_ms());
|
||||||
|
self.start_time = rtc_get(&mut env.llio);
|
||||||
|
|
||||||
// set uart MUX, and turn off WFI so UART reports are "clean" (no stuck characters when CPU is in WFI)
|
// set uart MUX, and turn off WFI so UART reports are "clean" (no stuck characters when CPU is in WFI)
|
||||||
env.llio.set_uart_mux(llio::UartType::Log).unwrap();
|
env.llio.set_uart_mux(llio::UartType::Log).unwrap();
|
||||||
env.llio.wfi_override(true).unwrap();
|
env.llio.wfi_override(true).unwrap();
|
||||||
|
|
@ -74,10 +94,46 @@ impl<'a> ShellCmdApi<'a> for Test {
|
||||||
let (usbcc_event, usbcc_regs, usbcc_rev) = env.com.poll_usb_cc().unwrap();
|
let (usbcc_event, usbcc_regs, usbcc_rev) = env.com.poll_usb_cc().unwrap();
|
||||||
log::info!("{}|USBCC|{:?}|{:?}|{}|", SENTINEL, usbcc_event, usbcc_regs, usbcc_rev);
|
log::info!("{}|USBCC|{:?}|{:?}|{}|", SENTINEL, usbcc_event, usbcc_regs, usbcc_rev);
|
||||||
|
|
||||||
|
env.ticktimer.sleep_ms(3000).unwrap(); // wait so we have some realistic delta on the datetime function
|
||||||
|
self.end_elapsed = Some(env.ticktimer.elapsed_ms());
|
||||||
|
self.end_time = rtc_get(&mut env.llio);
|
||||||
|
|
||||||
|
let exact_time_secs = ((self.end_elapsed.unwrap() - self.start_elapsed.unwrap()) / 1000) as i32;
|
||||||
|
if let Some(end_dt) = self.end_time {
|
||||||
|
if let Some(start_dt) = self.start_time {
|
||||||
|
// this method of computation fails if the test was started just before midnight
|
||||||
|
// on the last day of the month and completes just after. we set the time so we ensure this
|
||||||
|
// can't happen.
|
||||||
|
let end_secs =
|
||||||
|
end_dt.days as i32 * 24 * 60 * 60 +
|
||||||
|
end_dt.hours as i32 * 60 * 60 +
|
||||||
|
end_dt.minutes as i32 * 60 +
|
||||||
|
end_dt.seconds as i32;
|
||||||
|
let start_secs =
|
||||||
|
start_dt.days as i32 * 24 * 60 * 60 +
|
||||||
|
start_dt.hours as i32 * 60 * 60 +
|
||||||
|
start_dt.minutes as i32 * 60 +
|
||||||
|
start_dt.seconds as i32;
|
||||||
|
let elapsed_secs = end_secs - start_secs;
|
||||||
|
|
||||||
|
let delta = exact_time_secs - elapsed_secs;
|
||||||
|
if delta.abs() > 2 {
|
||||||
|
log::info!("{}|RTC|FAIL|{}|{}|", SENTINEL, exact_time_secs, elapsed_secs);
|
||||||
|
} else {
|
||||||
|
log::info!("{}|RTC|PASS|{}|{}|", SENTINEL, exact_time_secs, elapsed_secs);
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
log::info!("{}|RTC|FAIL|NO_START|", SENTINEL);
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
log::info!("{}|RTC|FAIL|NO_END|", SENTINEL);
|
||||||
|
}
|
||||||
|
|
||||||
log::info!("{}|DONE|", SENTINEL);
|
log::info!("{}|DONE|", SENTINEL);
|
||||||
write!(ret, "Factory test script has run, check serial terminal for output").unwrap();
|
write!(ret, "Factory test script has run, check serial terminal for output").unwrap();
|
||||||
env.llio.wfi_override(false).unwrap();
|
env.llio.wfi_override(false).unwrap();
|
||||||
}
|
}
|
||||||
|
#[cfg(feature="audio_tests")]
|
||||||
"audio" => {
|
"audio" => {
|
||||||
self.codec.setup_8k_stream().expect("couldn't set the CODEC to expected defaults");
|
self.codec.setup_8k_stream().expect("couldn't set the CODEC to expected defaults");
|
||||||
env.ticktimer.sleep_ms(50).unwrap();
|
env.ticktimer.sleep_ms(50).unwrap();
|
||||||
|
|
@ -169,14 +225,15 @@ impl<'a> ShellCmdApi<'a> for Test {
|
||||||
const OMEGA3: f32 = 261.63 * 2.0 * std::f32::consts::PI / SAMPLE_RATE_HZ; // C4
|
const OMEGA3: f32 = 261.63 * 2.0 * std::f32::consts::PI / SAMPLE_RATE_HZ; // C4
|
||||||
const AMPLITUDE: f32 = 0.707;
|
const AMPLITUDE: f32 = 0.707;
|
||||||
|
|
||||||
log::debug!("audio callback");
|
match &msg.body {
|
||||||
xous::msg_scalar_unpack!(msg, free_play, _avail_rec, _, _, {
|
#[cfg(feature="audio_tests")]
|
||||||
|
Message::Scalar(xous::ScalarMessage{id: _, arg1: free_play, arg2: _avail_rec, arg3: _, arg4: _}) => {
|
||||||
log::debug!("{} extending playback", free_play);
|
log::debug!("{} extending playback", free_play);
|
||||||
let mut frames: FrameRing = FrameRing::new();
|
let mut frames: FrameRing = FrameRing::new();
|
||||||
let frames_to_push = if frames.writeable_count() < free_play {
|
let frames_to_push = if frames.writeable_count() < *free_play {
|
||||||
frames.writeable_count()
|
frames.writeable_count()
|
||||||
} else {
|
} else {
|
||||||
free_play
|
*free_play
|
||||||
};
|
};
|
||||||
self.framecount += frames_to_push as u32;
|
self.framecount += frames_to_push as u32;
|
||||||
log::debug!("f{} p{}", self.framecount, frames_to_push);
|
log::debug!("f{} p{}", self.framecount, frames_to_push);
|
||||||
|
|
@ -229,7 +286,105 @@ impl<'a> ShellCmdApi<'a> for Test {
|
||||||
break;
|
break;
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
});
|
},
|
||||||
|
Message::Move(_mm) => {
|
||||||
|
log::error!("received memory message when not expected")
|
||||||
|
},
|
||||||
|
_ => {
|
||||||
|
log::error!("received unknown callback type")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
log::debug!("audio callback");
|
||||||
Ok(None)
|
Ok(None)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
/// 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) -> Weekday {
|
||||||
|
match bcd {
|
||||||
|
0 => Weekday::Sunday,
|
||||||
|
1 => Weekday::Monday,
|
||||||
|
2 => Weekday::Tuesday,
|
||||||
|
3 => Weekday::Wednesday,
|
||||||
|
4 => Weekday::Thursday,
|
||||||
|
5 => Weekday::Friday,
|
||||||
|
6 => Weekday::Saturday,
|
||||||
|
_ => Weekday::Sunday,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
const ABRTCMC_I2C_ADR: u8 = 0x68;
|
||||||
|
const ABRTCMC_CONTROL3: u8 = 0x02;
|
||||||
|
const ABRTCMC_SECONDS: u8 = 0x3;
|
||||||
|
|
||||||
|
// vendor in the RTC code -- the RTC system was not architected to do our test sets
|
||||||
|
// in particular we need a synchronous callback on the date/time, which is not terribly useful in most other contexts
|
||||||
|
// so instead of burdening the OS with it, we just incorprate it specifically into this test function
|
||||||
|
fn rtc_set(llio: &mut llio::Llio, secs: u8, mins: u8, hours: u8, days: u8, months: u8, years: u8) -> bool {
|
||||||
|
let mut txbuf: [u8; 8] = [0; 8];
|
||||||
|
|
||||||
|
// convert enum to bitfields
|
||||||
|
let d = 1;
|
||||||
|
|
||||||
|
// make sure battery switchover is enabled, otherwise we won't keep time when power goes off
|
||||||
|
txbuf[0] = 0;
|
||||||
|
txbuf[1] = to_bcd(secs);
|
||||||
|
txbuf[2] = to_bcd(mins);
|
||||||
|
txbuf[3] = to_bcd(hours);
|
||||||
|
txbuf[4] = to_bcd(days);
|
||||||
|
txbuf[5] = to_bcd(d);
|
||||||
|
txbuf[6] = to_bcd(months);
|
||||||
|
txbuf[7] = to_bcd(years);
|
||||||
|
|
||||||
|
match llio.i2c_write(ABRTCMC_I2C_ADR, ABRTCMC_CONTROL3, &txbuf, None) {
|
||||||
|
Ok(status) => {
|
||||||
|
match status {
|
||||||
|
I2cStatus::ResponseWriteOk => true,
|
||||||
|
I2cStatus::ResponseBusy => {log::error!("i2c server busy on RTC test function"); false},
|
||||||
|
_ => {log::error!("try_send_i2c unhandled response: {:?}", status); false},
|
||||||
|
}
|
||||||
|
}
|
||||||
|
_ => {log::error!("try_send_i2c unhandled error"); false}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
fn rtc_get(llio: &mut llio::Llio) -> Option<DateTime> {
|
||||||
|
let mut rxbuf = [0; 7];
|
||||||
|
match llio.i2c_read(ABRTCMC_I2C_ADR, ABRTCMC_SECONDS, &mut rxbuf, None) {
|
||||||
|
Ok(status) => {
|
||||||
|
match status {
|
||||||
|
I2cStatus::ResponseReadOk => {
|
||||||
|
let dt = DateTime {
|
||||||
|
seconds: to_binary(rxbuf[0] & 0x7f),
|
||||||
|
minutes: to_binary(rxbuf[1] & 0x7f),
|
||||||
|
hours: to_binary(rxbuf[2] & 0x3f),
|
||||||
|
days: to_binary(rxbuf[3] & 0x3f),
|
||||||
|
weekday: to_weekday(rxbuf[4] & 0x7f),
|
||||||
|
months: to_binary(rxbuf[5] & 0x1f),
|
||||||
|
years: to_binary(rxbuf[6]),
|
||||||
|
};
|
||||||
|
Some(dt)
|
||||||
|
},
|
||||||
|
_ => None,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
_ => None
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue