#![deny(missing_docs)]
#![no_std]
#![forbid(unsafe_code)]
#![cfg_attr(docsrs, feature(doc_cfg))]
#[cfg(feature = "async")]
#[cfg_attr(docsrs, doc(cfg(feature = "async")))]
pub mod asynch;
mod clock;
mod sequence;
pub use es8311_registers as registers;
pub use es8311_registers::{
ALTERNATE_DEVICE_ADDRESS, DEFAULT_DEVICE_ADDRESS, SerialFormat, WordLength,
};
use embedded_hal::delay::DelayNs;
use embedded_hal::i2c::I2c;
use es8311_registers::prelude::*;
use es8311_registers::{ChipId1, ChipId2, ChipVersion, DacControl, DacVolume};
use sequence::Write;
pub const ES8311_CHIP_ID: u16 =
u16::from_be_bytes([registers::ES8311_CHIP_ID1, registers::ES8311_CHIP_ID2]);
pub const DAC_VOLUME_MAX: u8 = 0xFF;
pub const DAC_VOLUME_0DB: u8 = 0xBF;
pub const RESET_DELAY_MS: u32 = 5;
#[derive(Debug, Copy, Clone, PartialEq, Eq)]
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
pub struct Config {
pub mclk_hz: u32,
pub sample_rate_hz: u32,
pub format: SerialFormat,
pub word_length: WordLength,
}
impl Config {
pub const fn new(sample_rate_hz: u32) -> Self {
Self {
mclk_hz: sample_rate_hz * 256,
sample_rate_hz,
format: SerialFormat::I2s,
word_length: WordLength::Bits16,
}
}
pub const fn with_mclk(self, mclk_hz: u32) -> Self {
Self { mclk_hz, ..self }
}
pub const fn with_format(self, format: SerialFormat) -> Self {
Self { format, ..self }
}
pub const fn with_word_length(self, word_length: WordLength) -> Self {
Self {
word_length,
..self
}
}
}
#[derive(Debug, Copy, Clone, PartialEq, Eq)]
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
pub enum Error<E> {
I2c(E),
UnknownChip(u16),
UnsupportedClock,
}
fn coefficients<E>(config: &Config) -> Result<&'static clock::ClockCoefficients, Error<E>> {
clock::lookup(config.mclk_hz, config.sample_rate_hz).ok_or(Error::UnsupportedClock)
}
#[derive(Debug)]
pub struct Es8311<I2C> {
i2c: I2C,
address: u8,
max_volume: u8,
}
impl<I2C> Es8311<I2C>
where
I2C: I2c,
{
pub fn new(i2c: I2C) -> Self {
Self::with_address(i2c, DEFAULT_DEVICE_ADDRESS)
}
pub fn with_address(i2c: I2C, address: u8) -> Self {
Self {
i2c,
address,
max_volume: DAC_VOLUME_MAX,
}
}
pub fn release(self) -> I2C {
self.i2c
}
pub fn chip_id(&mut self) -> Result<u16, I2C::Error> {
let high = self.read_register::<ChipId1>()?.value();
let low = self.read_register::<ChipId2>()?.value();
Ok(u16::from_be_bytes([high, low]))
}
pub fn chip_version(&mut self) -> Result<u8, I2C::Error> {
Ok(self.read_register::<ChipVersion>()?.value())
}
pub fn identify(&mut self) -> Result<bool, I2C::Error> {
Ok(self.chip_id()? == ES8311_CHIP_ID)
}
pub fn init<D: DelayNs>(
&mut self,
delay: &mut D,
config: &Config,
) -> Result<(), Error<I2C::Error>> {
let clock = coefficients(config)?;
let id = self.chip_id().map_err(Error::I2c)?;
if id != ES8311_CHIP_ID {
return Err(Error::UnknownChip(id));
}
self.write_all(&sequence::RESET).map_err(Error::I2c)?;
delay.delay_ms(RESET_DELAY_MS);
self.write_all(&sequence::configure(config, clock))
.map_err(Error::I2c)
}
pub fn dac_power_up(&mut self) -> Result<(), I2C::Error> {
self.write_all(&sequence::DAC_POWER_UP)
}
pub fn dac_power_down(&mut self) -> Result<(), I2C::Error> {
self.set_mute(true)?;
self.write_all(&sequence::DAC_POWER_DOWN)
}
pub fn set_mute(&mut self, mute: bool) -> Result<(), I2C::Error> {
self.modify_register(|r: DacControl| r.with_dac_dsmmute(mute).with_dac_demmute(mute))
}
pub fn max_volume(&self) -> u8 {
self.max_volume
}
pub fn set_max_volume(&mut self, max: u8) -> Result<(), I2C::Error> {
self.max_volume = max;
if self.volume()? > max {
self.write_register(DacVolume::new().with_value(max))?;
}
Ok(())
}
pub fn set_volume(&mut self, volume: u8) -> Result<(), I2C::Error> {
self.write_register(DacVolume::new().with_value(volume.min(self.max_volume)))
}
pub fn volume(&mut self) -> Result<u8, I2C::Error> {
Ok(self.read_register::<DacVolume>()?.value())
}
pub fn standby(&mut self) -> Result<(), I2C::Error> {
self.set_mute(true)?;
self.write_all(&sequence::STANDBY)
}
pub fn read_register<R>(&mut self) -> Result<R, I2C::Error>
where
R: ReadableRegister,
{
let mut buffer = [0];
self.i2c
.write_read(self.address, &[*R::REGISTER_ADDRESS], &mut buffer)?;
Ok(R::from_bits(buffer[0]))
}
pub fn write_register<R>(&mut self, register: R) -> Result<(), I2C::Error>
where
R: WritableRegister,
{
self.i2c
.write(self.address, &[*R::REGISTER_ADDRESS, register.to_bits()])
}
pub fn modify_register<R, F>(&mut self, f: F) -> Result<(), I2C::Error>
where
R: ReadableRegister + WritableRegister,
F: FnOnce(R) -> R,
{
let register = self.read_register::<R>()?;
self.write_register(f(register))
}
fn write_all(&mut self, writes: &[Write]) -> Result<(), I2C::Error> {
for &(reg, value) in writes {
self.i2c.write(self.address, &[reg.addr(), value])?;
}
Ok(())
}
}