mirror of
https://github.com/saymrwulf/xous-core.git
synced 2026-11-16 08:38:26 +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]
|
||||
debugprint = []
|
||||
default = [] # "debugprint"
|
||||
audio_tests = []
|
||||
default = ["audio_tests"] # "debugprint"
|
||||
|
||||
[target.'cfg(any(windows,unix))'.dependencies]
|
||||
debug-here = "*"
|
||||
|
|
|
|||
|
|
@ -1,10 +1,12 @@
|
|||
use crate::{ShellCmdApi,CommonEnv};
|
||||
use xous_ipc::String;
|
||||
use xous::MessageEnvelope;
|
||||
use xous::{MessageEnvelope, Message};
|
||||
use llio::I2cStatus;
|
||||
|
||||
use codec::*;
|
||||
use spectrum_analyzer::{samples_fft_to_spectrum, FrequencyLimit};
|
||||
use spectrum_analyzer::windows::hann_window;
|
||||
use rtc::{DateTime, Weekday};
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct Test {
|
||||
|
|
@ -16,6 +18,10 @@ pub struct Test {
|
|||
framecount: u32,
|
||||
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
|
||||
start_time: Option<DateTime>,
|
||||
end_time: Option<DateTime>,
|
||||
start_elapsed: Option<u64>,
|
||||
end_elapsed: Option<u64>,
|
||||
}
|
||||
impl Test {
|
||||
pub fn new(xns: &xous_names::XousNames) -> Self {
|
||||
|
|
@ -28,15 +34,21 @@ impl Test {
|
|||
xous::MemoryFlags::R | xous::MemoryFlags::W,
|
||||
).expect("couldn't allocate record buffer");
|
||||
|
||||
let callback_conn = xns.request_connection_blocking(crate::SERVER_NAME_SHELLCHAT).unwrap();
|
||||
|
||||
Test {
|
||||
codec,
|
||||
recbuf,
|
||||
state: 0,
|
||||
callback_id: None,
|
||||
callback_conn: xns.request_connection_blocking(crate::SERVER_NAME_SHELLCHAT).unwrap(),
|
||||
callback_conn,
|
||||
framecount: 0,
|
||||
play_sample: 0.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() {
|
||||
match sub_cmd {
|
||||
"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)
|
||||
env.llio.set_uart_mux(llio::UartType::Log).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();
|
||||
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);
|
||||
write!(ret, "Factory test script has run, check serial terminal for output").unwrap();
|
||||
env.llio.wfi_override(false).unwrap();
|
||||
}
|
||||
#[cfg(feature="audio_tests")]
|
||||
"audio" => {
|
||||
self.codec.setup_8k_stream().expect("couldn't set the CODEC to expected defaults");
|
||||
env.ticktimer.sleep_ms(50).unwrap();
|
||||
|
|
@ -169,67 +225,166 @@ impl<'a> ShellCmdApi<'a> for Test {
|
|||
const OMEGA3: f32 = 261.63 * 2.0 * std::f32::consts::PI / SAMPLE_RATE_HZ; // C4
|
||||
const AMPLITUDE: f32 = 0.707;
|
||||
|
||||
log::debug!("audio callback");
|
||||
xous::msg_scalar_unpack!(msg, free_play, _avail_rec, _, _, {
|
||||
log::debug!("{} extending playback", free_play);
|
||||
let mut frames: FrameRing = FrameRing::new();
|
||||
let frames_to_push = if frames.writeable_count() < free_play {
|
||||
frames.writeable_count()
|
||||
} else {
|
||||
free_play
|
||||
};
|
||||
self.framecount += frames_to_push as u32;
|
||||
log::debug!("f{} p{}", self.framecount, frames_to_push);
|
||||
for _ in 0..frames_to_push {
|
||||
let mut frame: [u32; codec::FIFO_DEPTH] = [ZERO_PCM as u32 | (ZERO_PCM as u32) << 16; codec::FIFO_DEPTH];
|
||||
// put the "expensive" f32 comparison outside the cosine wave table computation loop
|
||||
if self.play_sample < SAMPLE_RATE_HZ { // 1 second A4
|
||||
for sample in frame.iter_mut() {
|
||||
// left channel, A4 note
|
||||
let raw_sine: i16 = (AMPLITUDE * f32::cos( self.play_sample * OMEGA1 ) * i16::MAX as f32) as i16;
|
||||
*sample = raw_sine as u32 | (ZERO_PCM as u32) << 16;
|
||||
self.play_sample += 1.0;
|
||||
}
|
||||
} else if self.play_sample < SAMPLE_RATE_HZ * 2.0 {
|
||||
for sample in frame.iter_mut() { // next second E4
|
||||
// right channel, E4 note
|
||||
let raw_sine: i16 = (AMPLITUDE * f32::cos( self.play_sample * OMEGA2 ) * i16::MAX as f32) as i16;
|
||||
*sample = ZERO_PCM as u32 | (raw_sine as u32) << 16;
|
||||
self.play_sample += 1.0;
|
||||
}
|
||||
match &msg.body {
|
||||
#[cfg(feature="audio_tests")]
|
||||
Message::Scalar(xous::ScalarMessage{id: _, arg1: free_play, arg2: _avail_rec, arg3: _, arg4: _}) => {
|
||||
log::debug!("{} extending playback", free_play);
|
||||
let mut frames: FrameRing = FrameRing::new();
|
||||
let frames_to_push = if frames.writeable_count() < *free_play {
|
||||
frames.writeable_count()
|
||||
} else {
|
||||
for sample in frame.iter_mut() { // rest C4
|
||||
// both channels, C4 note
|
||||
let raw_sine: i16 = (AMPLITUDE * f32::cos( self.play_sample * OMEGA3 ) * i16::MAX as f32) as i16;
|
||||
*sample = raw_sine as u32 | (raw_sine as u32) << 16;
|
||||
self.play_sample += 1.0;
|
||||
}
|
||||
}
|
||||
|
||||
frames.nq_frame(frame).unwrap();
|
||||
|
||||
}
|
||||
self.codec.swap_frames(&mut frames).unwrap();
|
||||
|
||||
let rec_samples = self.recbuf.as_slice_mut::<u32>();
|
||||
let rec_len = rec_samples.len();
|
||||
loop {
|
||||
if let Some(frame) = frames.dq_frame() {
|
||||
for sample in rec_samples.iter_mut() {
|
||||
*sample = frame[self.rec_sample];
|
||||
// increment and wrap around on overflow
|
||||
// we should be sampling a continuous tone, so we'll get a small phase discontinutity once in the buffer.
|
||||
// should be no problem for the analysis phase.
|
||||
self.rec_sample += 1;
|
||||
if self.rec_sample > rec_len {
|
||||
self.rec_sample = 0;
|
||||
*free_play
|
||||
};
|
||||
self.framecount += frames_to_push as u32;
|
||||
log::debug!("f{} p{}", self.framecount, frames_to_push);
|
||||
for _ in 0..frames_to_push {
|
||||
let mut frame: [u32; codec::FIFO_DEPTH] = [ZERO_PCM as u32 | (ZERO_PCM as u32) << 16; codec::FIFO_DEPTH];
|
||||
// put the "expensive" f32 comparison outside the cosine wave table computation loop
|
||||
if self.play_sample < SAMPLE_RATE_HZ { // 1 second A4
|
||||
for sample in frame.iter_mut() {
|
||||
// left channel, A4 note
|
||||
let raw_sine: i16 = (AMPLITUDE * f32::cos( self.play_sample * OMEGA1 ) * i16::MAX as f32) as i16;
|
||||
*sample = raw_sine as u32 | (ZERO_PCM as u32) << 16;
|
||||
self.play_sample += 1.0;
|
||||
}
|
||||
} else if self.play_sample < SAMPLE_RATE_HZ * 2.0 {
|
||||
for sample in frame.iter_mut() { // next second E4
|
||||
// right channel, E4 note
|
||||
let raw_sine: i16 = (AMPLITUDE * f32::cos( self.play_sample * OMEGA2 ) * i16::MAX as f32) as i16;
|
||||
*sample = ZERO_PCM as u32 | (raw_sine as u32) << 16;
|
||||
self.play_sample += 1.0;
|
||||
}
|
||||
} else {
|
||||
for sample in frame.iter_mut() { // rest C4
|
||||
// both channels, C4 note
|
||||
let raw_sine: i16 = (AMPLITUDE * f32::cos( self.play_sample * OMEGA3 ) * i16::MAX as f32) as i16;
|
||||
*sample = raw_sine as u32 | (raw_sine as u32) << 16;
|
||||
self.play_sample += 1.0;
|
||||
}
|
||||
}
|
||||
} else {
|
||||
break;
|
||||
};
|
||||
|
||||
frames.nq_frame(frame).unwrap();
|
||||
|
||||
}
|
||||
self.codec.swap_frames(&mut frames).unwrap();
|
||||
|
||||
let rec_samples = self.recbuf.as_slice_mut::<u32>();
|
||||
let rec_len = rec_samples.len();
|
||||
loop {
|
||||
if let Some(frame) = frames.dq_frame() {
|
||||
for sample in rec_samples.iter_mut() {
|
||||
*sample = frame[self.rec_sample];
|
||||
// increment and wrap around on overflow
|
||||
// we should be sampling a continuous tone, so we'll get a small phase discontinutity once in the buffer.
|
||||
// should be no problem for the analysis phase.
|
||||
self.rec_sample += 1;
|
||||
if self.rec_sample > rec_len {
|
||||
self.rec_sample = 0;
|
||||
}
|
||||
}
|
||||
} else {
|
||||
break;
|
||||
};
|
||||
}
|
||||
},
|
||||
Message::Move(_mm) => {
|
||||
log::error!("received memory message when not expected")
|
||||
},
|
||||
_ => {
|
||||
log::error!("received unknown callback type")
|
||||
}
|
||||
});
|
||||
}
|
||||
log::debug!("audio callback");
|
||||
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