1#[derive(Debug, Copy, Clone, PartialEq, Eq)]
6#[cfg_attr(feature = "defmt", derive(defmt::Format))]
7#[repr(u8)]
8pub enum SerialFormat {
9 I2s = 0b00,
11 LeftJustified = 0b01,
13 Reserved = 0b10,
15 DspPcm = 0b11,
17}
18
19impl SerialFormat {
20 pub const fn into_bits(self) -> u8 {
22 self as u8
23 }
24
25 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#[derive(Debug, Copy, Clone, PartialEq, Eq)]
39#[cfg_attr(feature = "defmt", derive(defmt::Format))]
40#[repr(u8)]
41pub enum WordLength {
42 Bits24 = 0,
44 Bits20 = 1,
46 Bits18 = 2,
48 Bits16 = 3,
50 Bits32 = 4,
52 Reserved5 = 5,
54 Reserved6 = 6,
56 Reserved7 = 7,
58}
59
60impl WordLength {
61 pub const fn into_bits(self) -> u8 {
63 self as u8
64 }
65
66 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#[derive(Debug, Copy, Clone, PartialEq, Eq)]
83#[cfg_attr(feature = "defmt", derive(defmt::Format))]
84#[repr(u8)]
85pub enum PreMultiplier {
86 X1 = 0,
88 X2 = 1,
90 X4 = 2,
92 X8 = 3,
94}
95
96impl PreMultiplier {
97 pub const fn into_bits(self) -> u8 {
99 self as u8
100 }
101
102 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 pub const fn factor(self) -> u32 {
114 1 << self as u32
115 }
116}
117
118#[derive(Debug, Copy, Clone, PartialEq, Eq)]
120#[cfg_attr(feature = "defmt", derive(defmt::Format))]
121#[repr(u8)]
122pub enum VmidSelect {
123 PowerDown = 0,
125 NormalCharge = 1,
127 Normal = 2,
129 FastCharge = 3,
131}
132
133impl VmidSelect {
134 pub const fn into_bits(self) -> u8 {
136 self as u8
137 }
138
139 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#[derive(Debug, Copy, Clone, PartialEq, Eq)]
156#[cfg_attr(feature = "defmt", derive(defmt::Format))]
157#[repr(u8)]
158pub enum AdcDataSelect {
159 AdcAdc = 0,
161 AdcZero = 1,
163 ZeroAdc = 2,
165 ZeroZero = 3,
167 DacLeftAdc = 4,
169 AdcDacRight = 5,
171 DacLeftDacRight = 6,
173 Reserved = 7,
175}
176
177impl AdcDataSelect {
178 pub const fn into_bits(self) -> u8 {
180 self as u8
181 }
182
183 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}