diff --git a/Cargo.lock b/Cargo.lock index 05a35e868..07a1e970b 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -329,6 +329,7 @@ dependencies = [ "rkyv 0.4.3", "susres", "ticktimer-server", + "trng", "utralib", "xous", "xous-ipc", diff --git a/services/codec/Cargo.toml b/services/codec/Cargo.toml index 432637f35..78af9d294 100644 --- a/services/codec/Cargo.toml +++ b/services/codec/Cargo.toml @@ -14,6 +14,7 @@ xous-names = { path = "../xous-names" } log = "0.4" susres = {path = "../susres"} llio = {path = "../llio"} +trng = {path = "../trng"} xous-ipc = {path="../../xous-ipc"} num-derive = {version = "0.3", default-features = false} diff --git a/services/codec/src/api.rs b/services/codec/src/api.rs index c87191b8e..6f9188269 100644 --- a/services/codec/src/api.rs +++ b/services/codec/src/api.rs @@ -101,6 +101,11 @@ impl FrameRing { self.wr_frame = 0; self.rec_ptr = 0; } + pub fn reset_ptrs(&mut self) { + self.rd_frame = 0; + self.wr_frame = 0; + self.rec_ptr = 0; + } /* empty: rd_frame == wr_frame full: wr_frame == (rd_frame - 1) || (rd_frame == 0) && (wr_frame == F-1) diff --git a/services/codec/src/backend/tlv320aic3100.rs b/services/codec/src/backend/tlv320aic3100.rs index 8b4d2815c..8bd6ad0c1 100644 --- a/services/codec/src/backend/tlv320aic3100.rs +++ b/services/codec/src/backend/tlv320aic3100.rs @@ -418,9 +418,9 @@ impl Codec { self.w(86, &[ 0b1_011_0000, // AGC enabled, target level = -12dB - 0b00_10001_0, // hysteresis 1dB, noise threshold = -((value-1)*2 + 30), 17 => -64dB + 0b00_10101_0, // hysteresis 1dB, noise threshold = -((value-1)*2 + 30): 21 => -70dB 100, // max gain = code/2 dB - 0b_00101_000, // attack time = 0b_acode_mul = (acode*32*mul)/Fs + 0b_00010_000, // attack time = 0b_acode_mul = (acode*32*mul)/Fs 0b_01101_000, // decay time = 0b_dcode_mul = (dcode*32*mul)/Fs 0x01, // noise debounce time = code*4 / fs 0x01, // signal debounce time = code*4 / fs @@ -430,13 +430,15 @@ impl Codec { /// set up the betrusted-side signals pub fn audio_i2s_start(&mut self) { + /* self.csr.wfo(utra::audio::RX_CTL_RESET, 1); self.csr.wfo(utra::audio::TX_CTL_RESET, 1); - + */ let volatile_audio = self.fifo.as_mut_ptr() as *mut u32; for _ in 0..FIFO_DEPTH*2 { unsafe { (volatile_audio).write(ZERO_PCM as u32 | (ZERO_PCM as u32) << 16); } // prefill TX fifo with zero's } + // enable interrupts on the RX_READY self.csr.wfo(utra::audio::EV_PENDING_RX_READY, 1); // clear any pending interrupt self.csr.wfo(utra::audio::EV_ENABLE_RX_READY, 1); diff --git a/services/codec/src/main.rs b/services/codec/src/main.rs index fce86eea0..eaa377fc6 100644 --- a/services/codec/src/main.rs +++ b/services/codec/src/main.rs @@ -39,6 +39,13 @@ fn xmain() -> ! { // register a suspend/resume listener let sr_cid = xous::connect(codec_sid).expect("couldn't create suspend callback connection"); let mut susres = susres::Susres::new(&xns, api::Opcode::SuspendResume as u32, sr_cid).expect("couldn't create suspend/resume object"); + /* + let trng = trng::Trng::new(&xns).unwrap(); + let mut noise: [u32; codec::FIFO_DEPTH] = [0; codec::FIFO_DEPTH]; + for i in 0..noise.len() { + noise[i] = trng.get_u32().unwrap(); + } + */ let mut audio_cb_conns: [Option; 32] = [None; 32]; loop { @@ -118,6 +125,7 @@ fn xmain() -> ! { } } + framering.reset_ptrs(); loop { if let Some(frame) = codec.dq_rec_frame() { if !framering.is_full() { diff --git a/services/shellchat/src/cmds/audio.rs b/services/shellchat/src/cmds/audio.rs index d3ee26858..fe2f57c69 100644 --- a/services/shellchat/src/cmds/audio.rs +++ b/services/shellchat/src/cmds/audio.rs @@ -24,9 +24,10 @@ pub struct Audio { impl Audio { pub fn new(xns: &xous_names::XousNames) -> Self { let sample = xous::syscall::map_memory( + // 0x2634_0000 is the long sample. 0x2600_0000 is the short sample. Some(core::num::NonZeroUsize::new(0x2600_0000).unwrap()), // it's here, because we know it's here! None, - 0x8_0000, + 0x8_0000, // 0x8_0000 is length of short sample, 0x1C4_0000 is the long sample xous::MemoryFlags::R, ).expect("couldn't map in the audio sample"); let mut codec = codec::Codec::new(xns).unwrap(); @@ -85,42 +86,26 @@ impl<'a> ShellCmdApi<'a> for Audio { self.callback_id = Some(cb_id); } - write!(ret, "Playing sample...").unwrap(); - // load the initial sample data - let (play_free, _) = self.codec.free_frames().unwrap(); - - let mut frames: FrameRing = FrameRing::new(); - let frames_to_push = if frames.writeable_count() < play_free { - frames.writeable_count() + if self.play_or_rec_n == true { + write!(ret, "Playing sample...").unwrap(); } else { - play_free - }; - log::info!("loading up {} frames", frames_to_push); + write!(ret, "Playing previous microphone recording...").unwrap(); + } self.play_ptr_bytes = 0; self.rec_ptr_words = 0; - self.framecount = frames_to_push as u32; - for i in 0..frames_to_push { - let mut frame: [u32; codec::FIFO_DEPTH] = [codec::ZERO_PCM as u32 | (codec::ZERO_PCM as u32) << 16; codec::FIFO_DEPTH]; - if self.play_or_rec_n { - for sample in frame.iter_mut() { - *sample = unsafe{self.raw_data.add(i).read_volatile()}; - } - } else { - for sample in frame.iter_mut() { - *sample = unsafe{self.rec_data.add(i).read_volatile()}; - } - } - self.play_ptr_bytes += codec::FIFO_DEPTH * 4; - frames.nq_frame(frame).unwrap(); - } - log::debug!("pushing frames"); - self.codec.swap_frames(&mut frames).unwrap(); - // start the playing + log::info!("starting playback"); self.codec.resume().unwrap(); // we'll get a callback that demands the next data... } + "stop" => { + self.codec.pause().unwrap(); // this should stop callbacks from occurring too. + write!(ret, "Playback stopped at {} frames.", self.framecount).unwrap(); + self.framecount = 0; + self.play_ptr_bytes = 0; + self.rec_ptr_words = 0; + } "fromrec" => { self.play_or_rec_n = false; write!(ret, "playing back from record buffer").unwrap(); @@ -196,6 +181,8 @@ impl<'a> ShellCmdApi<'a> for Audio { unsafe{*self.rec_data.add(i + self.rec_ptr_words as usize) = frame[i]}; } self.rec_ptr_words += codec::FIFO_DEPTH as u32; + } else { + // just silently toss any overrun for now } } else { break;