diff --git a/emulation/renode.svd b/emulation/renode.svd index 9d84e19a5..d901a040c 100644 --- a/emulation/renode.svd +++ b/emulation/renode.svd @@ -3,7 +3,7 @@ litex SOC - + 8 32 @@ -1070,10 +1070,10 @@ register to disable individual events.]]> GIT_GITREV 0x0014 - 0x1d8dd9bd + 0x45bbf751 32 @@ -1090,7 +1090,7 @@ the exact git version used to build this firmware.]]> is ``1``, then the repository this was built from has one additional commit beyond the tag indicated in `MAJOR`, `MINOR`, and `REVISION`.]]> 0x0018 - 0x09 + 0x0a 32 @@ -1104,7 +1104,7 @@ beyond the tag indicated in `MAJOR`, `MINOR`, and `REVISION`.]]> GIT_DIRTY 0x001c - 0x00 + 0x01 32 @@ -3117,7 +3117,7 @@ inject too fast.]]> SEED1 0x0000 - 0x2d8be885 + 0x1fda4c0d 32 @@ -3132,7 +3132,7 @@ inject too fast.]]> SEED0 0x0004 - 0x7ec869ac + 0x5bd9a073 32 diff --git a/precursors/soc.svd b/precursors/soc.svd index 9d84e19a5..d901a040c 100644 --- a/precursors/soc.svd +++ b/precursors/soc.svd @@ -3,7 +3,7 @@ litex SOC - + 8 32 @@ -1070,10 +1070,10 @@ register to disable individual events.]]> GIT_GITREV 0x0014 - 0x1d8dd9bd + 0x45bbf751 32 @@ -1090,7 +1090,7 @@ the exact git version used to build this firmware.]]> is ``1``, then the repository this was built from has one additional commit beyond the tag indicated in `MAJOR`, `MINOR`, and `REVISION`.]]> 0x0018 - 0x09 + 0x0a 32 @@ -1104,7 +1104,7 @@ beyond the tag indicated in `MAJOR`, `MINOR`, and `REVISION`.]]> GIT_DIRTY 0x001c - 0x00 + 0x01 32 @@ -3117,7 +3117,7 @@ inject too fast.]]> SEED1 0x0000 - 0x2d8be885 + 0x1fda4c0d 32 @@ -3132,7 +3132,7 @@ inject too fast.]]> SEED0 0x0004 - 0x7ec869ac + 0x5bd9a073 32 diff --git a/precursors/soc_csr.bin b/precursors/soc_csr.bin index 005007027..edcb9cbf7 100644 Binary files a/precursors/soc_csr.bin and b/precursors/soc_csr.bin differ diff --git a/services/codec/src/api.rs b/services/codec/src/api.rs index 538c58a1c..c87191b8e 100644 --- a/services/codec/src/api.rs +++ b/services/codec/src/api.rs @@ -95,6 +95,12 @@ impl FrameRing { rec_ptr: 0, } } + pub fn clear(&mut self) { + self.buffer = [[(ZERO_PCM as u32 | (ZERO_PCM as u32) << 16); FIFO_DEPTH]; FRAMES]; + 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 d29e9f6da..d2ac212a2 100644 --- a/services/codec/src/backend/tlv320aic3100.rs +++ b/services/codec/src/backend/tlv320aic3100.rs @@ -18,52 +18,75 @@ pub struct Codec { ticktimer: ticktimer_server::Ticktimer, play_buffer: FrameRing, play_frames_dropped: u32, + tx_stat_errors: u32, rec_buffer: FrameRing, rec_frames_dropped: u32, + rx_stat_errors: u32, powered_on: bool, initialized: bool, live: bool, conn: xous::CID, } +static SILENCE: [u32; FIFO_DEPTH] = [ZERO_PCM as u32 | (ZERO_PCM as u32) << 16; FIFO_DEPTH]; + fn audio_handler(_irq_no: usize, arg: *mut usize) { let codec = unsafe { &mut *(arg as *mut Codec) }; let volatile_audio = codec.fifo.as_mut_ptr() as *mut u32; // load the play buffer if let Some(frame) = codec.play_buffer.dq_frame() { - assert!(codec.csr.rf(utra::audio::TX_STAT_FREE) == 1, "interrupt was called, but not enough space to receive!"); + //assert!(codec.csr.rf(utra::audio::TX_STAT_FREE) == 1, "interrupt was called, but not enough space to receive!"); + if codec.csr.rf(utra::audio::TX_STAT_FREE) != 1 { + codec.tx_stat_errors += 1; + } for &stereo_sample in frame.iter() { - unsafe { *volatile_audio = stereo_sample }; + unsafe { volatile_audio.write_volatile((stereo_sample & 0xFFFF_0000) | ((stereo_sample & 0xFFFF) >> 1)) }; +// unsafe { volatile_audio.write_volatile(stereo_sample) }; + } + } else { + codec.play_frames_dropped += 1; + for &stereo_sample in SILENCE.iter() { + unsafe { volatile_audio.write_volatile(stereo_sample) }; } } // copy the record buffer //assert!(codec.csr.rf(utra::audio::RX_STAT_DATAREADY) == 1, "interrupt was called, but not enough data to read!"); + if codec.csr.rf(utra::audio::RX_STAT_DATAREADY) != 1 { + codec.rx_stat_errors += 1; + } let rx_rdcount = codec.csr.rf(utra::audio::RX_STAT_RDCOUNT) as usize; let rx_wrcount = codec.csr.rf(utra::audio::RX_STAT_WRCOUNT) as usize; if codec.rec_buffer.is_full() { codec.rec_frames_dropped += 1; + let mut _dummy = 0; + for _ in 0..codec::FIFO_DEPTH { + _dummy += unsafe{volatile_audio.read_volatile()}; // drain the record fifo to prevent interrupt from continuously firing + } } else { for _ in 0..codec::FIFO_DEPTH { - codec.rec_buffer.rec_sample(unsafe{*volatile_audio}); + codec.rec_buffer.rec_sample(unsafe{volatile_audio.read_volatile()}); } codec.rec_buffer.rec_advance(); } + + /* let mut rec_buf: [u32; FIFO_DEPTH] = [ZERO_PCM as u32 | (ZERO_PCM as u32) << 16; FIFO_DEPTH]; for stereo_sample in rec_buf.iter_mut() { - unsafe{ *stereo_sample = *volatile_audio; } + unsafe{ *stereo_sample = volatile_audio.read_volatile(); } } - match codec.rec_buffer.nq_frame(rec_buf) { Ok(()) => {}, Err(_buff) => codec.rec_frames_dropped += 1, + }*/ + + // if the buffer is low, let the audio handler know we used up another frame! + if codec.play_buffer.readable_count() < 6 { + xous::try_send_message(codec.conn, + xous::Message::new_scalar(Opcode::AnotherFrame.to_usize().unwrap(), rx_rdcount, rx_wrcount, 0, 0)).unwrap(); } - // let the audio handler know we used up another frame! - xous::try_send_message(codec.conn, - xous::Message::new_scalar(Opcode::AnotherFrame.to_usize().unwrap(), rx_rdcount, rx_wrcount, 0, 0)).unwrap(); - - codec.csr.wfo(utra::audio::EV_PENDING_TX_READY, 1); + codec.csr.wfo(utra::audio::EV_PENDING_RX_READY, 1); } impl Codec { @@ -99,6 +122,8 @@ impl Codec { initialized: false, live: false, conn, + tx_stat_errors: 0, + rx_stat_errors: 0, }; xous::claim_interrupt( @@ -107,7 +132,7 @@ impl Codec { (&mut codec) as *mut Codec as *mut usize, ) .expect("couldn't claim audio irq"); - codec.csr.wfo(utra::audio::EV_PENDING_TX_READY, 1); + codec.csr.wfo(utra::audio::EV_PENDING_RX_READY, 1); codec.susres_manager.push(RegOrField::Reg(utra::audio::RX_CTL), None); codec.susres_manager.push(RegOrField::Reg(utra::audio::TX_CTL), None); @@ -116,6 +141,13 @@ impl Codec { codec } + pub fn trace(&mut self) { + if self.tx_stat_errors > 0 || self.rx_stat_errors > 0 { + log::trace!("drop p:{} r:{} | staterr tx:{} rx:{}", self.play_frames_dropped, self.rec_frames_dropped, self.tx_stat_errors, self.rx_stat_errors); + self.tx_stat_errors = 0; + self.rx_stat_errors = 0; + } + } pub fn trace_rx(&self) { log::trace!("T rd {} wr {}", self.csr.rf(utra::audio::RX_STAT_RDCOUNT), self.csr.rf(utra::audio::RX_STAT_WRCOUNT)); } @@ -333,9 +365,9 @@ impl Codec { // DAC volume - neither DACs muted, independent volume controls self.w(64, &[0b0000_0_0_00]); // DAC left volume control - self.w(65, &[0b1]); // +0.5dB + self.w(65, &[0b1111_0110]); // -5dB // DAC right volume control - self.w(66, &[0b1]); // +0.5dB + self.w(66, &[0b1111_0110]); // -5dB ///////// VOLUME, PGA CONTROLS -- PAGE 1 self.w(0, &[1]); // select page 1 @@ -346,8 +378,8 @@ impl Codec { // internal volume control self.w(36, &[ - 0b1_001_0010, // HPL channel control on, -9dB - 0b1_001_0010, // HPR channel control on, -9dB + 0b1_001_1110, // HPL channel control on, -15dB + 0b1_001_1110, // HPR channel control on, -15dB 0b1_000_1100, // SPK control on, -6dB ]); @@ -355,7 +387,7 @@ impl Codec { self.w(40, &[ 0b0_0000_111, // HPL driver PGA = 0dB, not muted, all gains applied 0b0_0000_111, // HPR driver PGA = 0dB, not muted, all gains applied - 0b000_00_1_0_1, // SPK gain = 6 dB, driver not muted, all gains applied + 0b000_01_1_0_1, // SPK gain = 12 dB, driver not muted, all gains applied ]); // HP driver control -- 16us short circuit debounce, best DAC performance, HPL/HPR as headphone drivers @@ -413,9 +445,9 @@ impl Codec { 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 TX_READY - self.csr.wfo(utra::audio::EV_PENDING_TX_READY, 1); // clear any pending interrupt - self.csr.wfo(utra::audio::EV_ENABLE_TX_READY, 1); + // 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); // this sets everything running self.csr.wfo(utra::audio::RX_CTL_ENABLE, 1); @@ -424,13 +456,17 @@ impl Codec { } pub fn audio_i2s_stop(&mut self) { - self.csr.wfo(utra::audio::EV_ENABLE_TX_READY, 0); + self.csr.wfo(utra::audio::EV_ENABLE_RX_READY, 0); + self.csr.wfo(utra::audio::EV_PENDING_RX_READY, 1); + self.csr.wfo(utra::audio::RX_CTL_ENABLE, 0); self.csr.wfo(utra::audio::TX_CTL_ENABLE, 0); self.csr.wfo(utra::audio::RX_CTL_RESET, 1); self.csr.wfo(utra::audio::TX_CTL_RESET, 1); self.live = false; + self.play_buffer.clear(); + self.rec_buffer.clear(); } /// this is a testing-only function which does a double-buffered audio loopback diff --git a/services/codec/src/main.rs b/services/codec/src/main.rs index eac6b7655..265e8eaf8 100644 --- a/services/codec/src/main.rs +++ b/services/codec/src/main.rs @@ -23,7 +23,7 @@ struct ScalarCallback { #[xous::xous_main] fn xmain() -> ! { log_server::init_wait().unwrap(); - log::set_max_level(log::LevelFilter::Trace); + log::set_max_level(log::LevelFilter::Info); info!("my PID is {}", xous::process::id()); let xns = xous_names::XousNames::new().unwrap(); @@ -43,7 +43,7 @@ fn xmain() -> ! { let mut audio_cb_conns: [Option; 32] = [None; 32]; loop { let mut msg = xous::receive_message(codec_sid).unwrap(); - log::trace!("got message {:?}", msg); + //log::trace!("got message {:?}", msg); match FromPrimitive::from_usize(msg.body.id()) { Some(api::Opcode::SuspendResume) => msg_scalar_unpack!(msg, token, _, _, _, { codec.suspend(); @@ -64,6 +64,7 @@ fn xmain() -> ! { }), Some(api::Opcode::ResumeStream) => xous::msg_scalar_unpack!(msg, _, _, _, _, { codec.trace_rx(); + codec.trace(); if codec.is_on() && codec.is_init() { codec.audio_i2s_start(); } else { @@ -72,6 +73,7 @@ fn xmain() -> ! { }), Some(api::Opcode::PauseStream) => xous::msg_scalar_unpack!(msg, _, _, _, _, { codec.trace_rx(); + codec.trace(); if codec.is_on() && codec.is_init() { codec.audio_i2s_stop(); } else { @@ -130,8 +132,9 @@ fn xmain() -> ! { do_hook(hookdata, &mut audio_cb_conns); log::trace!("hook done, {:?}", audio_cb_conns); } - Some(api::Opcode::AnotherFrame) => xous::msg_scalar_unpack!(msg, rdcount, wrcount, _, _, { - log::trace!("A rd {} wr {}", rdcount, wrcount); + Some(api::Opcode::AnotherFrame) => xous::msg_scalar_unpack!(msg, _rdcount, _wrcount, _, _, { + codec.trace(); + //log::trace!("A rd {} wr {}", rdcount, wrcount); send_event(&audio_cb_conns, codec.free_play_frames(), codec.available_rec_frames()); }), None => { diff --git a/services/shellchat/src/cmds/audio.rs b/services/shellchat/src/cmds/audio.rs index a48443445..31229355d 100644 --- a/services/shellchat/src/cmds/audio.rs +++ b/services/shellchat/src/cmds/audio.rs @@ -24,12 +24,10 @@ impl Audio { xous::MemoryFlags::R, ).expect("couldn't map in the audio sample"); let mut codec = codec::Codec::new(xns).unwrap(); - let mut raw_header: [u8; 16] = [0; 16]; let samples: *const [u8; 16] = unsafe{sample.as_ptr().add(20)} as *const [u8; 16]; - log::trace!("header:"); + let mut raw_header: [u8; 16] = [0; 16]; for i in 0..16 { unsafe{ raw_header[i] = (*samples)[i] }; - log::trace!("0x{:x}", raw_header[i]); } log::trace!("setting up audio stream"); @@ -40,7 +38,7 @@ impl Audio { codec.hook_frame_callback(0xDEAD_BEEF, callback_conn).unwrap(); // any non-handled IDs get routed to our callback port log::trace!("returning from setup"); - Audio { + let mut audio = Audio { codec, sample, header: Header::from(raw_header), @@ -48,7 +46,34 @@ impl Audio { raw_len_bytes: unsafe{*(sample.as_ptr().add(40) as *const u32)}, play_ptr_bytes: 0, framecount: 0, + }; + /* + // load the initial sample data + let (play_free, _) = audio.codec.free_frames().unwrap(); + + let mut frames: FrameRing = FrameRing::new(); + let frames_to_push = if frames.writeable_count() < play_free { + frames.writeable_count() + } else { + play_free + }; + log::debug!("loading up {} frames", frames_to_push); + audio.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]; + for sample in frame.iter_mut() { + *sample = unsafe{audio.raw_data.add(i).read_volatile()}; + } + audio.play_ptr_bytes += codec::FIFO_DEPTH * 4; + frames.nq_frame(frame).unwrap(); } + log::debug!("pushing frames"); + audio.codec.swap_frames(&mut frames).unwrap(); + // start the playing + log::debug!("starting playback"); + audio.codec.resume().unwrap(); + */ + audio } } @@ -76,24 +101,39 @@ impl<'a> ShellCmdApi<'a> for Audio { } else { play_free }; - log::trace!("loading up {} frames", frames_to_push); + log::debug!("loading up {} frames", frames_to_push); 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]; for sample in frame.iter_mut() { - *sample = unsafe{*self.raw_data.add(i)}; + *sample = unsafe{self.raw_data.add(i).read_volatile()}; } self.play_ptr_bytes += codec::FIFO_DEPTH * 4; frames.nq_frame(frame).unwrap(); } - log::trace!("pushing frames"); + log::debug!("pushing frames"); self.codec.swap_frames(&mut frames).unwrap(); // start the playing - log::trace!("starting playback"); + log::debug!("starting playback"); self.codec.resume().unwrap(); // we'll get a callback that demands the next data... } + "dump" => { + let mut temp = String::<9>::new(); + for i in 0..8 { + write!(temp, "{:08x} ", unsafe{self.raw_data.add(i).read_volatile()}).unwrap(); + ret.append(temp.to_str()).unwrap(); + temp.clear(); + } + ret.append("\n").unwrap(); + ret.append("\n").unwrap(); + for i in 0..8 { + write!(temp, "{:08x} ", unsafe{self.raw_data.add(i + 0x2000).read_volatile()}).unwrap(); + ret.append(temp.to_str()).unwrap(); + temp.clear(); + } + } "info" => { write!(ret, "Loaded sample is {}kHz, {} channels, {} format, {} bytes", self.header.sampling_rate, self.header.channel_count, self.header.audio_format, self.raw_len_bytes).unwrap(); } @@ -109,9 +149,11 @@ impl<'a> ShellCmdApi<'a> for Audio { fn callback(&mut self, msg: &MessageEnvelope, _env: &mut CommonEnv) -> Result>, xous::Error> { use core::fmt::Write; + log::debug!("audio callback"); let mut ret = String::<1024>::new(); xous::msg_scalar_unpack!(msg, free_play, _avail_rec, _, _, { if self.play_ptr_bytes + codec::FIFO_DEPTH *4 < self.raw_len_bytes as usize { + 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() @@ -119,11 +161,11 @@ impl<'a> ShellCmdApi<'a> for Audio { free_play }; self.framecount += frames_to_push as u32; - log::trace!("frame {}", self.framecount); + log::debug!("frame {}", self.framecount); 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]; for i in 0..codec::FIFO_DEPTH { - frame[i] = unsafe{*self.raw_data.add(i)}; + frame[i] = unsafe{*self.raw_data.add(i + self.play_ptr_bytes/4)}; } self.play_ptr_bytes += codec::FIFO_DEPTH * 4; frames.nq_frame(frame).unwrap(); @@ -132,9 +174,16 @@ impl<'a> ShellCmdApi<'a> for Audio { return Ok(None) } else { - log::trace!("stopping playback"); - self.codec.pause().unwrap(); // this should stop callbacks from occurring too. - write!(ret, "{}", "Playback finished").unwrap(); + log::debug!("stopping playback"); + if self.framecount != 0 { + self.codec.pause().unwrap(); // this should stop callbacks from occurring too. + self.framecount = 0; + self.play_ptr_bytes = 0; + write!(ret, "{}", "Playback finished").unwrap(); + } else { + // we will get extra callbacks as the pipe clears + return Ok(None) + } } }); Ok(Some(ret)) diff --git a/services/shellchat/src/main.rs b/services/shellchat/src/main.rs index f05d5f320..81fa182dd 100644 --- a/services/shellchat/src/main.rs +++ b/services/shellchat/src/main.rs @@ -94,7 +94,7 @@ impl Repl{ } /// update the loop, in response to various inputs - fn update(&mut self) -> Result<(), xous::Error> { + fn update(&mut self, was_callback: bool) -> Result<(), xous::Error> { let debug1 = false; // if we had an input string, do something if let Some(local) = self.input { @@ -110,8 +110,11 @@ impl Repl{ // side effect our commands // redraw UI once upon accepting all input - self.redraw().expect("can't redraw"); + if !was_callback { // don't need to redraw on a callback, save some cycles + self.redraw().expect("can't redraw"); + } + let mut dirty = true; // take the input and pass it on to the various command parsers, and attach result if let Some(mut local) = self.input { log::trace!("processing line: {}", local); @@ -123,6 +126,8 @@ impl Repl{ is_input: false }; self.circular_push(output_history); + } else { + dirty = false; } } else if let Some(msg) = &self.msg { log::trace!("processing callback msg: {:?}", msg); @@ -134,6 +139,8 @@ impl Repl{ is_input: false }; self.circular_push(output_history); + } else { + dirty = false; } } @@ -141,7 +148,9 @@ impl Repl{ self.input = None; self.msg = None; // redraw UI now that we've responded - self.redraw().expect("can't redraw"); + if dirty { + self.redraw().expect("can't redraw"); + } if debug1 { for h in self.history.iter() { @@ -263,6 +272,7 @@ fn xmain() -> ! { let mut repl = Repl::new(&xns, SERVER_NAME_SHELLCHAT); let mut update_repl = false; + let mut was_callback = false; if false { xous::create_thread_0(test_thread).unwrap(); @@ -279,6 +289,7 @@ fn xmain() -> ! { log::trace!("shell got input line: {}", s.as_str()); repl.input(s.as_str()).expect("REPL couldn't accept input string"); update_repl = true; // set a flag, instead of calling here, so message can drop and calling server is released + was_callback = false; } Some(ShellOpcode::Redraw) => { log::trace!("got Redraw"); @@ -292,10 +303,11 @@ fn xmain() -> ! { log::trace!("got unknown message, treating as callback"); repl.msg(msg); update_repl = true; + was_callback = true; } } if update_repl { - repl.update().expect("REPL had problems updating"); + repl.update(was_callback).expect("REPL had problems updating"); update_repl = false; } log::trace!("reached bottom of main loop"); diff --git a/services/susres/src/main.rs b/services/susres/src/main.rs index 53bd192fd..bc0dda55a 100644 --- a/services/susres/src/main.rs +++ b/services/susres/src/main.rs @@ -37,7 +37,7 @@ mod implementation { use num_traits::ToPrimitive; const SYSTEM_CLOCK_FREQUENCY: u32 = 100_000_000; - const SYSTEM_TICK_INTERVAL_MS: u32 = 100; + const SYSTEM_TICK_INTERVAL_MS: u32 = 20; fn timer_tick(_irq_no: usize, arg: *mut usize) { let mut timer = CSR::new(arg as *mut u32); diff --git a/svd2utra/examples/soc.svd b/svd2utra/examples/soc.svd index 9d84e19a5..d901a040c 100644 --- a/svd2utra/examples/soc.svd +++ b/svd2utra/examples/soc.svd @@ -3,7 +3,7 @@ litex SOC - + 8 32 @@ -1070,10 +1070,10 @@ register to disable individual events.]]> GIT_GITREV 0x0014 - 0x1d8dd9bd + 0x45bbf751 32 @@ -1090,7 +1090,7 @@ the exact git version used to build this firmware.]]> is ``1``, then the repository this was built from has one additional commit beyond the tag indicated in `MAJOR`, `MINOR`, and `REVISION`.]]> 0x0018 - 0x09 + 0x0a 32 @@ -1104,7 +1104,7 @@ beyond the tag indicated in `MAJOR`, `MINOR`, and `REVISION`.]]> GIT_DIRTY 0x001c - 0x00 + 0x01 32 @@ -3117,7 +3117,7 @@ inject too fast.]]> SEED1 0x0000 - 0x2d8be885 + 0x1fda4c0d 32 @@ -3132,7 +3132,7 @@ inject too fast.]]> SEED0 0x0004 - 0x7ec869ac + 0x5bd9a073 32