1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
use core::convert::TryInto;
use core::ops::Range;
use super::format::{DataFormat, FrameFormat};
use super::pac::spi1::i2scfgr::CHLEN_A;
use crate::Polarity;
const DIVISION_RANGE: Range<u16> = 4..512;
#[derive(Debug, Clone)]
pub enum MasterClock {
Enable,
Disable,
}
#[derive(Debug, Clone)]
pub struct MasterConfig<F> {
pub(crate) division: u16,
pub(crate) data_format: F,
pub(crate) frame_format: FrameFormat,
pub(crate) polarity: Polarity,
pub(crate) master_clock: MasterClock,
}
impl<F> MasterConfig<F>
where
F: DataFormat,
{
pub fn with_division(
division: u16,
data_format: F,
frame_format: FrameFormat,
polarity: Polarity,
master_clock: MasterClock,
) -> Self {
if !DIVISION_RANGE.contains(&division) {
panic!(
"I2S clock division {} outside allowed range {}..{}",
division, DIVISION_RANGE.start, DIVISION_RANGE.end
);
}
MasterConfig {
division,
data_format,
frame_format,
polarity,
master_clock,
}
}
pub fn with_sample_rate(
frequency_in: u32,
sample_rate: u32,
data_format: F,
frame_format: FrameFormat,
polarity: Polarity,
master_clock: MasterClock,
) -> Self {
let bits_per_sample: u32 = match F::CHLEN {
CHLEN_A::SIXTEENBIT => 16,
CHLEN_A::THIRTYTWOBIT => 32,
};
let master_clock_division: u32 = if matches!(master_clock, MasterClock::Enable) {
match F::CHLEN {
CHLEN_A::SIXTEENBIT => 8,
CHLEN_A::THIRTYTWOBIT => 4,
}
} else {
1
};
let bit_rate = sample_rate * 2 * bits_per_sample;
let division = frequency_in / (bit_rate * master_clock_division);
let division_options: [u32; 3] = [
division.saturating_sub(1),
division,
division.saturating_add(1),
];
let best_division: u32 = division_options
.iter()
.cloned()
.filter(is_valid_division)
.min_by_key(|possible_division| {
let real_sample_rate = frequency_in
/ (bits_per_sample * 2 * *possible_division * master_clock_division);
i32::abs((real_sample_rate as i32) - (sample_rate as i32))
})
.expect("Couldn't find a valid I2S division value");
Self::with_division(
best_division.try_into().unwrap(),
data_format,
frame_format,
polarity,
master_clock,
)
}
}
fn is_valid_division(division: &u32) -> bool {
*division >= u32::from(DIVISION_RANGE.start) && *division < u32::from(DIVISION_RANGE.end)
}
#[derive(Debug, Clone)]
pub struct SlaveConfig<F> {
pub(crate) data_format: F,
pub(crate) frame_format: FrameFormat,
pub(crate) polarity: Polarity,
}
impl<F> SlaveConfig<F> {
pub fn new(data_format: F, frame_format: FrameFormat, polarity: Polarity) -> Self {
SlaveConfig {
data_format,
frame_format,
polarity,
}
}
}