Skip to content

Commit

Permalink
checkpoint: clean up some things
Browse files Browse the repository at this point in the history
  • Loading branch information
antoinevg committed Sep 10, 2022
1 parent bafd506 commit 7878c56
Show file tree
Hide file tree
Showing 4 changed files with 258 additions and 235 deletions.
9 changes: 5 additions & 4 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,12 @@ edition = "2021"

[dependencies]
#riscv = { version = "=0.8.0" }
#riscv-rt = "=0.9.0"
riscv = { git = "https://github.com/rust-embedded/riscv" }
riscv-rt = "=0.9.0"
#d1-pac = { version = "=0.0.27", features = ["rt"] }
d1-pac = { path = "../upstream/d1-temp", features = ["rt"] }
#d1-pac = { git = "https://github.com/antoinevg/aw-pac", branch = "antoinevg/audio_codec", features = ["rt"] }
riscv-rt = { git = "https://github.com/rust-embedded/riscv-rt", rev = "28806e0b0ca3b3980ef3f886d5b56283d47daef1" }
#d1-pac = { version = "=0.0.27" } #, features = ["rt"] }
#d1-pac = { path = "../upstream/d1-temp" }
d1-pac = { git = "https://github.com/antoinevg/aw-pac", branch = "antoinevg/audio_codec" }
panic-halt = "=0.2.0"

#twiddle = { path = "../twiddle.git" }
Expand Down
74 changes: 26 additions & 48 deletions examples/audio_testsignal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ mod dsp;

use d1_pac as pac;
use panic_halt as _;
use twiddle;

use hello_d1::audio_codec::{self, BLOCK_LENGTH};
use hello_d1::dmac;
Expand All @@ -23,7 +22,7 @@ fn main() -> ! {
println!("🦀 Hello Lichee RV D1!");

// trim bandgap reference voltage ???
//audio_codec::trim_bandgap_ref_voltage();
// audio_codec::trim_bandgap_ref_voltage();

// gpio - led
let gpio = &p.GPIO;
Expand All @@ -34,17 +33,16 @@ fn main() -> ! {
let mut ccu = p.CCU;

// audio_codec
let audio_codec = audio_codec::AudioCodec::new(p.AUDIO_CODEC, &mut ccu);
let mut audio_codec = audio_codec::AudioCodec::new(p.AUDIO_CODEC, &mut ccu);

// dmac
let mut dmac = dmac::Dmac::new(p.DMAC, &mut ccu);

// plic
let plic = plic::Plic::new(p.PLIC);
unsafe {
plic.set_priority(pac::Interrupt::DMAC_NS, plic::Priority::P10);
plic.set_priority(pac::Interrupt::DMAC_NS, plic::Priority::P1);
plic.set_priority(pac::Interrupt::AUDIO_CODEC, plic::Priority::P1);

plic.unmask(pac::Interrupt::DMAC_NS);
plic.unmask(pac::Interrupt::AUDIO_CODEC);
}
Expand Down Expand Up @@ -78,66 +76,56 @@ static mut COUNTER: usize = 0;
#[export_name = "MachineExternal"]
extern "C" fn MachineExternal() {
let plic = unsafe { plic::Plic::summon() };
let gpio = unsafe { &*pac::GPIO::PTR };
let audio_codec = unsafe { audio_codec::AudioCodec::summon() };

// Claim interrupt
let claim = plic.claim();
//println!("claim: {:?}", claim);

match claim {
pac::Interrupt::GPIOE_NS => {
let gpio = unsafe { &*pac::GPIO::PTR };

// clear pending interrupts
gpio.pe_eint_status
.modify(|_r, w| w.eint8_status().set_bit());
// Wait for the interrupt to clear to avoid repeat interrupts
while gpio.pe_eint_status.read().eint8_status().bit_is_set() {}
}
pac::Interrupt::DMAC_NS => {
let dmac = unsafe { &*pac::DMAC::PTR };

// get some stats
let packages = dmac.dmac_pkg_num2.read().bits();
let left = dmac.dmac_bcnt_left2.read().bits();
//let bits = dmac.dmac_sta.read().bits();
//let mbus = twiddle::bit(bits, 31);
//let chans = twiddle::range(bits, 0..15);
let packages = dmac.dmac_pkg_num0.read().bits();
let left = dmac.dmac_bcnt_left0.read().bits();

// get pending interrupts
let bits = dmac.dmac_irq_pend0.read().bits();
let pending = twiddle::range(bits, 8..10);
let pending = dmac.dmac_irq_pend0.read();

// clear all pending interrupts
if twiddle::bit(pending, 0) {
if pending.dma0_hlaf_irq_pend().is_pending() {
// half package
dmac.dmac_irq_pend0
.write(|w| w.dma2_hlaf_irq_pend().set_bit());
while dmac.dmac_irq_pend0.read().dma2_hlaf_irq_pend().bit_is_set() {}
.write(|w| w.dma0_hlaf_irq_pend().set_bit());
while dmac.dmac_irq_pend0.read().dma0_hlaf_irq_pend().bit_is_set() {}
}
if twiddle::bit(pending, 1) {
if pending.dma0_pkg_irq_pend().is_pending() {
// end of package
dmac.dmac_irq_pend0
.write(|w| w.dma2_pkg_irq_pend().set_bit());
while dmac.dmac_irq_pend0.read().dma2_pkg_irq_pend().bit_is_set() {}
.write(|w| w.dma0_pkg_irq_pend().set_bit());
while dmac.dmac_irq_pend0.read().dma0_pkg_irq_pend().bit_is_set() {}

}
if twiddle::bit(pending, 2) {
if pending.dma0_queue_irq_pend().is_pending() {
// end of queue
dmac.dmac_irq_pend0
.write(|w| w.dma2_queue_irq_pend().set_bit());
while dmac
.dmac_irq_pend0
.read()
.dma2_queue_irq_pend()
.bit_is_set()
{}
.write(|w| w.dma0_queue_irq_pend().set_bit());
while dmac.dmac_irq_pend0.read().dma0_queue_irq_pend().bit_is_set() {}
}

// dump some debug info
let counter = unsafe { COUNTER };
if counter % 1000 == 0 {
println!("DMAC_NS pend:{:#05b}", pending); // 0: idle, 1: busy
println!("DMAC_NS pend:{:#05b}", pending.bits()); // 0: idle, 1: busy
println!("DMAC_NS pkg:{}", packages);
println!("DMAC_NS left:{}\n", left);
//println!("DMAC_NS mbus:{} chans:{:#018b}", mbus, chans); // 0: idle, 1: busy
}
}
pac::Interrupt::AUDIO_CODEC => {
Expand All @@ -149,38 +137,28 @@ extern "C" fn MachineExternal() {
// clear all pending interrupts
if pending.txe_int().is_pending() {
audio_codec.ac_dac_fifos.write(|w| w.txe_int().set_bit());
// while audio_codec.ac_dac_fifos.read().txe_int().bit_is_set() {}
while audio_codec.ac_dac_fifos.read().txe_int().bit_is_set() {}
}
if pending.txo_int().is_pending() {
println!("overrun");
audio_codec.ac_dac_fifos.write(|w| w.txo_int().set_bit());
// while audio_codec.ac_dac_fifos.read().txo_int().bit_is_set() {}
while audio_codec.ac_dac_fifos.read().txo_int().bit_is_set() {}
}
if pending.txu_int().is_pending() {
println!("underrun");
audio_codec.ac_dac_fifos.write(|w| w.txu_int().set_bit());
// while audio_codec.ac_dac_fifos.read().txu_int().bit_is_set() {}
}

// TODO drop this and use loops above
loop {
let bits = audio_codec.ac_dac_fifos.read().bits();
if twiddle::range(bits, 1..3) == 0 {
break;
} else {
println!("WHELP: {:#034b}", bits);
}
while audio_codec.ac_dac_fifos.read().txu_int().bit_is_set() {}
}

// dump some debug info
let counter = unsafe { COUNTER };
if counter % 30000 == 0 {
let count = audio_codec.ac_dac_cnt.read().bits();
//println!("ac_dac_cnt: {:#034b} -> {}", count, counter);
println!("ac_dac_cnt: {}", count);
println!("ac_dac_cnt: {} / {}", count, count / 48_000);
}
}
x => {
println!("Unexpected claim: {:?}", x);
//panic!("unexpected claim");
}
}

Expand Down
Loading

0 comments on commit 7878c56

Please sign in to comment.