Skip to main content

es8311_registers/
types.rs

1//! Value types used in register fields.
2
3/// The serial audio data format, as set in [`SdpIn`](crate::SdpIn) and
4/// [`SdpOut`](crate::SdpOut).
5#[derive(Debug, Copy, Clone, PartialEq, Eq)]
6#[cfg_attr(feature = "defmt", derive(defmt::Format))]
7#[repr(u8)]
8pub enum SerialFormat {
9    /// I²S (default).
10    I2s = 0b00,
11    /// Left justified.
12    LeftJustified = 0b01,
13    /// Reserved.
14    Reserved = 0b10,
15    /// DSP/PCM mode. [`SdpIn::sdp_in_lrp`](crate::SdpIn) selects mode A or B.
16    DspPcm = 0b11,
17}
18
19impl SerialFormat {
20    /// Converts the value into its register representation.
21    pub const fn into_bits(self) -> u8 {
22        self as u8
23    }
24
25    /// Converts the two lowest bits of a register value into a format.
26    pub const fn from_bits(value: u8) -> Self {
27        match value & 0b11 {
28            0b00 => Self::I2s,
29            0b01 => Self::LeftJustified,
30            0b10 => Self::Reserved,
31            _ => Self::DspPcm,
32        }
33    }
34}
35
36/// The serial audio data word length, as set in [`SdpIn`](crate::SdpIn) and
37/// [`SdpOut`](crate::SdpOut).
38#[derive(Debug, Copy, Clone, PartialEq, Eq)]
39#[cfg_attr(feature = "defmt", derive(defmt::Format))]
40#[repr(u8)]
41pub enum WordLength {
42    /// 24 bits (default).
43    Bits24 = 0,
44    /// 20 bits.
45    Bits20 = 1,
46    /// 18 bits.
47    Bits18 = 2,
48    /// 16 bits.
49    Bits16 = 3,
50    /// 32 bits.
51    Bits32 = 4,
52    /// Undocumented value 5.
53    Reserved5 = 5,
54    /// Undocumented value 6.
55    Reserved6 = 6,
56    /// Undocumented value 7.
57    Reserved7 = 7,
58}
59
60impl WordLength {
61    /// Converts the value into its register representation.
62    pub const fn into_bits(self) -> u8 {
63        self as u8
64    }
65
66    /// Converts the three lowest bits of a register value into a word length.
67    pub const fn from_bits(value: u8) -> Self {
68        match value & 0b111 {
69            0 => Self::Bits24,
70            1 => Self::Bits20,
71            2 => Self::Bits18,
72            3 => Self::Bits16,
73            4 => Self::Bits32,
74            5 => Self::Reserved5,
75            6 => Self::Reserved6,
76            _ => Self::Reserved7,
77        }
78    }
79}
80
81/// The clock pre-multiplier, as set in [`ClockPrescaler`](crate::ClockPrescaler).
82#[derive(Debug, Copy, Clone, PartialEq, Eq)]
83#[cfg_attr(feature = "defmt", derive(defmt::Format))]
84#[repr(u8)]
85pub enum PreMultiplier {
86    /// `dig_mclk = mclk_prediv × 1` (default).
87    X1 = 0,
88    /// `dig_mclk = mclk_prediv × 2`.
89    X2 = 1,
90    /// `dig_mclk = mclk_prediv × 4`.
91    X4 = 2,
92    /// `dig_mclk = mclk_prediv × 8`.
93    X8 = 3,
94}
95
96impl PreMultiplier {
97    /// Converts the value into its register representation.
98    pub const fn into_bits(self) -> u8 {
99        self as u8
100    }
101
102    /// Converts the two lowest bits of a register value into a multiplier.
103    pub const fn from_bits(value: u8) -> Self {
104        match value & 0b11 {
105            0 => Self::X1,
106            1 => Self::X2,
107            2 => Self::X4,
108            _ => Self::X8,
109        }
110    }
111
112    /// The multiplication factor.
113    pub const fn factor(self) -> u32 {
114        1 << self as u32
115    }
116}
117
118/// The VMID mode, as set in [`SystemPower`](crate::SystemPower).
119#[derive(Debug, Copy, Clone, PartialEq, Eq)]
120#[cfg_attr(feature = "defmt", derive(defmt::Format))]
121#[repr(u8)]
122pub enum VmidSelect {
123    /// VMID powered down (default).
124    PowerDown = 0,
125    /// Start up VMID with normal speed charge.
126    NormalCharge = 1,
127    /// Normal VMID operation.
128    Normal = 2,
129    /// Start up VMID with fast speed charge.
130    FastCharge = 3,
131}
132
133impl VmidSelect {
134    /// Converts the value into its register representation.
135    pub const fn into_bits(self) -> u8 {
136        self as u8
137    }
138
139    /// Converts the two lowest bits of a register value into a VMID mode.
140    pub const fn from_bits(value: u8) -> Self {
141        match value & 0b11 {
142            0 => Self::PowerDown,
143            1 => Self::NormalCharge,
144            2 => Self::Normal,
145            _ => Self::FastCharge,
146        }
147    }
148}
149
150/// The data sent on `ASDOUT` (left + right slot), as set in
151/// [`GpioControl`](crate::GpioControl).
152///
153/// The values with `DACL` or `DACR` feed the DAC input back to the serial output
154/// ("digital feedback", user guide section 10.4).
155#[derive(Debug, Copy, Clone, PartialEq, Eq)]
156#[cfg_attr(feature = "defmt", derive(defmt::Format))]
157#[repr(u8)]
158pub enum AdcDataSelect {
159    /// ADC + ADC (default).
160    AdcAdc = 0,
161    /// ADC + 0.
162    AdcZero = 1,
163    /// 0 + ADC.
164    ZeroAdc = 2,
165    /// 0 + 0.
166    ZeroZero = 3,
167    /// DAC left + ADC.
168    DacLeftAdc = 4,
169    /// ADC + DAC right.
170    AdcDacRight = 5,
171    /// DAC left + DAC right.
172    DacLeftDacRight = 6,
173    /// Not available.
174    Reserved = 7,
175}
176
177impl AdcDataSelect {
178    /// Converts the value into its register representation.
179    pub const fn into_bits(self) -> u8 {
180        self as u8
181    }
182
183    /// Converts the three lowest bits of a register value into a selection.
184    pub const fn from_bits(value: u8) -> Self {
185        match value & 0b111 {
186            0 => Self::AdcAdc,
187            1 => Self::AdcZero,
188            2 => Self::ZeroAdc,
189            3 => Self::ZeroZero,
190            4 => Self::DacLeftAdc,
191            5 => Self::AdcDacRight,
192            6 => Self::DacLeftDacRight,
193            _ => Self::Reserved,
194        }
195    }
196}
197
198#[cfg(test)]
199mod tests {
200    use super::*;
201
202    #[test]
203    fn roundtrips() {
204        for v in 0..4 {
205            assert_eq!(SerialFormat::from_bits(v).into_bits(), v);
206            assert_eq!(PreMultiplier::from_bits(v).into_bits(), v);
207            assert_eq!(VmidSelect::from_bits(v).into_bits(), v);
208        }
209
210        for v in 0..8 {
211            assert_eq!(WordLength::from_bits(v).into_bits(), v);
212            assert_eq!(AdcDataSelect::from_bits(v).into_bits(), v);
213        }
214    }
215
216    #[test]
217    fn multiplier_factor() {
218        assert_eq!(PreMultiplier::X1.factor(), 1);
219        assert_eq!(PreMultiplier::X8.factor(), 8);
220    }
221}