use crate::Config;
use crate::clock::ClockCoefficients;
use es8311_registers::RegisterAddress as Reg;
use es8311_registers::{
AdcOversampling, AdcPower, Bias, ClockDivider, ClockPrescaler, ClockSwitch, DacControl,
DacOversampling, DacPower, DacRamp, GpControl, GpioControl, OutputDrive, PowerUpTiming1,
PowerUpTiming2, Reset, SdpIn, SdpOut, SystemPower, VmidSelect, Vsel,
};
pub(crate) type Write = (Reg, u8);
pub(crate) const RESET: [Write; 1] = [(Reg::RESET, Reset::new().into_bits())];
const MCLK_AND_BCLK: u8 = ClockSwitch::new()
.with_mclk_on(true)
.with_bclk_on(true)
.into_bits();
const ALL_CLOCKS: u8 = ClockSwitch::new()
.with_mclk_on(true)
.with_bclk_on(true)
.with_clkadc_on(true)
.with_clkdac_on(true)
.with_anaclkadc_on(true)
.with_anaclkdac_on(true)
.into_bits();
pub(crate) const DAC_MUTED: DacControl = DacControl::new()
.with_dac_dsmmute(true)
.with_dac_demmute(true);
const NO_LOOPBACK: u8 = GpioControl::new().with_i2c_wl(true).into_bits();
const ANALOG_DOWN: u8 = SystemPower::new().into_bits();
const DAC_DOWN: u8 = DacPower::new().into_bits();
pub(crate) fn configure(config: &Config, clock: &ClockCoefficients) -> [Write; 23] {
let prescaler = ClockPrescaler::new()
.with_div_pre(clock.div_pre)
.with_mult_pre(clock.mult_pre);
let divider = ClockDivider::new()
.with_div_clkadc(clock.div_clk)
.with_div_clkdac(clock.div_clk);
let dac_osr = DacOversampling::new().with_dac_osr(clock.dac_osr);
let sdp_in = SdpIn::new()
.with_sdp_in_wl(config.word_length)
.with_sdp_in_fmt(config.format);
let sdp_out = SdpOut::new()
.with_sdp_out_mute(true)
.with_sdp_out_wl(config.word_length)
.with_sdp_out_fmt(config.format);
let bias = Bias::new().with_ibias_sw(3);
let vsel = Vsel::new().with_vsel(0x7F);
let output = OutputDrive::new().with_reserved(0).with_hpsw(true);
let running = Reset::new()
.with_csm_on(true)
.with_rst_dig(false)
.with_rst_cmg(false)
.with_rst_mst(false)
.with_rst_adc_dig(false)
.with_rst_dac_dig(false);
[
(Reg::GPIO_44, NO_LOOPBACK),
(Reg::GPIO_44, NO_LOOPBACK),
(Reg::CLK_MANAGER_01, MCLK_AND_BCLK),
(Reg::CLK_MANAGER_02, prescaler.into_bits()),
(Reg::CLK_MANAGER_05, divider.into_bits()),
(Reg::CLK_MANAGER_03, AdcOversampling::new().into_bits()),
(Reg::CLK_MANAGER_04, dac_osr.into_bits()),
(Reg::SYSTEM_0B, PowerUpTiming1::new().into_bits()),
(
Reg::SYSTEM_0C,
PowerUpTiming2::new().with_pwrup_c(0).into_bits(),
),
(Reg::SYSTEM_10, bias.into_bits()),
(Reg::SYSTEM_11, vsel.into_bits()),
(Reg::SDP_IN, sdp_in.into_bits()),
(Reg::SDP_OUT, sdp_out.into_bits()),
(Reg::SYSTEM_13, output.into_bits()),
(Reg::DAC_31, DAC_MUTED.into_bits()),
(Reg::DAC_32, 0x00),
(Reg::DAC_37, DacRamp::new().into_bits()),
(Reg::SYSTEM_12, DAC_DOWN),
(Reg::SYSTEM_0E, AdcPower::new().into_bits()),
(Reg::SYSTEM_0D, ANALOG_DOWN),
(Reg::GP_45, GpControl::new().into_bits()),
(Reg::RESET, running.into_bits()),
(Reg::CLK_MANAGER_01, ALL_CLOCKS),
]
}
pub(crate) const DAC_POWER_UP: [Write; 2] = [
(
Reg::SYSTEM_0D,
SystemPower::new()
.with_pdn_ana(false)
.with_pdn_ibiasgen(false)
.with_pdn_adcbiasgen(false)
.with_pdn_adcvrefgen(false)
.with_pdn_dacvrefgen(false)
.with_pdn_vref(false)
.with_vmidsel(VmidSelect::NormalCharge)
.into_bits(),
),
(
Reg::SYSTEM_12,
DacPower::new().with_pdn_dac(false).into_bits(),
),
];
pub(crate) const DAC_POWER_DOWN: [Write; 2] =
[(Reg::SYSTEM_12, DAC_DOWN), (Reg::SYSTEM_0D, ANALOG_DOWN)];
pub(crate) const STANDBY: [Write; 6] = [
(Reg::DAC_32, 0x00),
(
Reg::SYSTEM_0E,
AdcPower::new().with_rst_mod(true).into_bits(),
),
(Reg::SYSTEM_12, DAC_DOWN),
(Reg::SYSTEM_0D, ANALOG_DOWN),
(Reg::RESET, Reset::new().into_bits()),
(Reg::CLK_MANAGER_01, ClockSwitch::new().into_bits()),
];
#[cfg(test)]
mod tests {
use super::*;
use es8311_registers::{SerialFormat, WordLength};
#[test]
fn values() {
assert_eq!(RESET[0].1, 0x1F);
assert_eq!(MCLK_AND_BCLK, 0x30);
assert_eq!(ALL_CLOCKS, 0x3F);
assert_eq!(NO_LOOPBACK, 0x08);
assert_eq!(DAC_MUTED.into_bits(), 0x60);
assert_eq!(
DAC_POWER_UP,
[(Reg::SYSTEM_0D, 0x01), (Reg::SYSTEM_12, 0x00)]
);
assert_eq!(
DAC_POWER_DOWN,
[(Reg::SYSTEM_12, 0x02), (Reg::SYSTEM_0D, 0xFC)]
);
assert_eq!(STANDBY[1].1, 0x7A);
}
#[test]
fn configure_48k_16bit() {
let config = Config::new(48_000)
.with_format(SerialFormat::I2s)
.with_word_length(WordLength::Bits16);
let clock = crate::clock::lookup(config.mclk_hz, config.sample_rate_hz).unwrap();
let writes = configure(&config, clock);
let value = |reg| writes.iter().rev().find(|w| w.0 == reg).unwrap().1;
assert_eq!(value(Reg::CLK_MANAGER_02), 0x00);
assert_eq!(value(Reg::CLK_MANAGER_05), 0x00);
assert_eq!(value(Reg::CLK_MANAGER_03), 0x10);
assert_eq!(value(Reg::CLK_MANAGER_04), 0x10);
assert_eq!(value(Reg::SYSTEM_0C), 0x00);
assert_eq!(value(Reg::SYSTEM_10), 0x1F);
assert_eq!(value(Reg::SYSTEM_11), 0x7F);
assert_eq!(value(Reg::SDP_IN), 0x0C);
assert_eq!(value(Reg::SDP_OUT), 0x4C);
assert_eq!(value(Reg::SYSTEM_13), 0x10);
assert_eq!(value(Reg::RESET), 0x80);
assert_eq!(writes[21], (Reg::RESET, 0x80));
assert_eq!(writes[22], (Reg::CLK_MANAGER_01, 0x3F));
}
}