refactor dac into a trait, add volume support
This commit is contained in:
+47
-33
@@ -36,11 +36,16 @@ use usbd_uac2::{
|
||||
descriptors::{ChannelConfig, ClockType, FormatType1, LockDelay},
|
||||
};
|
||||
|
||||
use crate::dac::DacImpl;
|
||||
use crate::traits::Dac;
|
||||
|
||||
#[cfg(feature = "ak4490")]
|
||||
mod ak4490;
|
||||
#[cfg(feature = "ak4490")]
|
||||
use ak4490 as dac;
|
||||
mod hw;
|
||||
pub mod dac {
|
||||
mod ak4490;
|
||||
pub use self::ak4490::Ak4490Dac as DacImpl;
|
||||
}
|
||||
|
||||
mod traits;
|
||||
|
||||
// Fo = M/(N*2*P) * Fin
|
||||
// Fo = 3072/(125*2*8) * 16MHz = 24.576MHz
|
||||
@@ -227,14 +232,20 @@ fn FLEXCOMM7() {
|
||||
}
|
||||
}
|
||||
|
||||
struct Audio<T: BbqHandle> {
|
||||
struct Audio<T: BbqHandle, D: Dac<I>, I> {
|
||||
running: AtomicBool,
|
||||
i2s: I2sTx,
|
||||
dac: D,
|
||||
producer: StreamProducer<T>,
|
||||
integrator: AtomicI32,
|
||||
filtered_fill: AtomicI32,
|
||||
_marker: core::marker::PhantomData<I>,
|
||||
}
|
||||
impl<T: BbqHandle> Audio<T> {
|
||||
impl<T: BbqHandle, D: Dac<I>, I> Audio<T, D, I> {
|
||||
fn init(&mut self) {
|
||||
self.dac.init();
|
||||
self.dac.change_rate(SAMPLE_RATE);
|
||||
}
|
||||
fn start(&self) {
|
||||
self.running.store(true, Ordering::Relaxed);
|
||||
defmt::info!("playback starting, enabling interrupts");
|
||||
@@ -255,7 +266,7 @@ impl<T: BbqHandle> Audio<T> {
|
||||
pac::NVIC::mask(pac::Interrupt::FLEXCOMM7);
|
||||
}
|
||||
}
|
||||
impl<T: BbqHandle, B: bus::UsbBus> UsbAudioClass<'_, B> for Audio<T> {
|
||||
impl<T: BbqHandle, D: Dac<I>, I, B: bus::UsbBus> UsbAudioClass<'_, B> for Audio<T, D, I> {
|
||||
fn alternate_setting_changed(&mut self, _terminal: usb_device::UsbDirection, alt_setting: u8) {
|
||||
match alt_setting {
|
||||
0 => self.stop(),
|
||||
@@ -477,7 +488,7 @@ fn main() -> ! {
|
||||
.system_frequency(96.MHz())
|
||||
.configure(&mut anactrl, &mut pmc, &mut syscon)
|
||||
.unwrap();
|
||||
let mut _delay_timer = Timer::new(
|
||||
let mut delay_timer = Timer::new(
|
||||
hal.ctimer
|
||||
.0
|
||||
.enabled(&mut syscon, clocks.support_1mhz_fro_token().unwrap()),
|
||||
@@ -490,11 +501,12 @@ fn main() -> ! {
|
||||
.4
|
||||
.enabled_as_i2c(&mut syscon, &clocks.support_flexcomm_token().unwrap());
|
||||
|
||||
let mut i2c_bus = I2cMaster::new(
|
||||
let i2c_bus = I2cMaster::new(
|
||||
i2c_peripheral,
|
||||
codec_i2c_pins,
|
||||
Hertz::try_from(400.kHz()).unwrap(),
|
||||
);
|
||||
let dac_impl = DacImpl::new(i2c_bus, codec_gpio_pins);
|
||||
|
||||
let i2s_peripheral = {
|
||||
let fc7 = hal.flexcomm.7.release();
|
||||
@@ -505,26 +517,41 @@ fn main() -> ! {
|
||||
&mut anactrl,
|
||||
&mut pmc,
|
||||
&mut syscon,
|
||||
&mut _delay_timer,
|
||||
&mut delay_timer,
|
||||
clocks.support_usbhs_token().unwrap(),
|
||||
);
|
||||
|
||||
defmt::info!("audio init");
|
||||
let mut audio = Audio {
|
||||
i2s: i2s_peripheral,
|
||||
dac: dac_impl,
|
||||
producer: QUEUE.stream_producer(),
|
||||
running: AtomicBool::new(false),
|
||||
integrator: AtomicI32::new(0),
|
||||
filtered_fill: AtomicI32::new(0),
|
||||
_marker: core::marker::PhantomData,
|
||||
};
|
||||
audio.init();
|
||||
|
||||
let usb_bus = UsbBus::new(usb_peripheral, usb0_vbus_pin);
|
||||
let mut clock = Clock {
|
||||
pins: clock_sel_pins,
|
||||
cur_rate: SAMPLE_RATE,
|
||||
};
|
||||
|
||||
defmt::debug!("codec init");
|
||||
dac::init_dac(&mut i2c_bus, codec_gpio_pins);
|
||||
|
||||
let mut audio = Audio {
|
||||
i2s: i2s_peripheral,
|
||||
producer: QUEUE.stream_producer(),
|
||||
running: AtomicBool::new(false),
|
||||
integrator: AtomicI32::new(0),
|
||||
filtered_fill: AtomicI32::new(0),
|
||||
};
|
||||
let mut usb_dev = UsbDeviceBuilder::new(&usb_bus, UsbVidPid(0x1209, 0xcc1d))
|
||||
.composite_with_iads()
|
||||
.strings(&[StringDescriptors::default()
|
||||
.manufacturer("VE7XEN")
|
||||
.product("Guac Tortilla")
|
||||
.serial_number("123456789")])
|
||||
.unwrap()
|
||||
.max_packet_size_0(64)
|
||||
.unwrap()
|
||||
.device_class(0xef)
|
||||
.device_sub_class(0x02)
|
||||
.device_protocol(0x01)
|
||||
.build();
|
||||
|
||||
let config = AudioClassConfig::new(UsbSpeed::High, FunctionCode::Other, &mut clock, &mut audio)
|
||||
.with_output_config(TerminalConfig::new(
|
||||
@@ -544,19 +571,6 @@ fn main() -> ! {
|
||||
|
||||
let mut uac2 = config.build(&usb_bus).unwrap();
|
||||
|
||||
let mut usb_dev = UsbDeviceBuilder::new(&usb_bus, UsbVidPid(0x1209, 0xcc1d))
|
||||
.composite_with_iads()
|
||||
.strings(&[StringDescriptors::default()
|
||||
.manufacturer("VE7XEN")
|
||||
.product("Guac Tortilla")
|
||||
.serial_number("123456789")])
|
||||
.unwrap()
|
||||
.max_packet_size_0(64)
|
||||
.unwrap()
|
||||
.device_class(0xef)
|
||||
.device_sub_class(0x02)
|
||||
.device_protocol(0x01)
|
||||
.build();
|
||||
|
||||
defmt::info!("main loop");
|
||||
|
||||
|
||||
Reference in New Issue
Block a user