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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
|
//! Peripheral Helpers
//!
//! The purpose of this module is to define the per-peripheral special handling
//! required from a clocking perspective. Different peripherals have different
//! selectable source clocks, and some peripherals have additional pre-dividers
//! that can be used.
//!
//! See the docs of [`SPConfHelper`] for more details.
use super::{ClockError, Clocks, PoweredClock};
use crate::pac;
/// Sealed Peripheral Configuration Helper
///
/// NOTE: the name "sealed" doesn't *totally* make sense because its not sealed yet in the
/// embassy-mcxa project, but it derives from embassy-imxrt where it is. We should
/// fix the name, or actually do the sealing of peripherals.
///
/// This trait serves to act as a per-peripheral customization for clocking behavior.
///
/// This trait should be implemented on a configuration type for a given peripheral, and
/// provide the methods that will be called by the higher level operations like
/// `embassy_mcxa::clocks::enable_and_reset()`.
pub trait SPConfHelper {
/// This method is called AFTER a given MRCC peripheral has been enabled (e.g. un-gated),
/// but BEFORE the peripheral reset line is reset.
///
/// This function should check that any relevant upstream clocks are enabled, are in a
/// reasonable power state, and that the requested configuration can be made. If any of
/// these checks fail, an `Err(ClockError)` should be returned, likely `ClockError::BadConfig`.
///
/// This function SHOULD NOT make any changes to the system clock configuration, even
/// unsafely, as this should remain static for the duration of the program.
///
/// This function WILL be called in a critical section, care should be taken not to delay
/// for an unreasonable amount of time.
///
/// On success, this function MUST return an `Ok(freq)`, where `freq` is the frequency
/// fed into the peripheral, taking into account the selected source clock, as well as
/// any pre-divisors.
fn post_enable_config(&self, clocks: &Clocks) -> Result<u32, ClockError>;
}
/// Copy and paste macro that:
///
/// * Sets the clocksel mux to `$selvar`
/// * Resets and halts the div, and applies the calculated div4 bits
/// * Releases reset + halt
/// * Waits for the div to stabilize
/// * Returns `Ok($freq / $conf.div.into_divisor())`
///
/// Assumes:
///
/// * self is a configuration struct that has a field called `div`, which
/// is a `Div4`
///
/// usage:
///
/// ```rust
/// apply_div4!(self, clksel, clkdiv, variant, freq)
/// ```
///
/// In the future if we make all the clksel+clkdiv pairs into commonly derivedFrom
/// registers, or if we put some kind of simple trait around those regs, we could
/// do this with something other than a macro, but for now, this is harm-reduction
/// to avoid incorrect copy + paste
macro_rules! apply_div4 {
($conf:ident, $selreg:ident, $divreg:ident, $selvar:ident, $freq:ident) => {{
// set clksel
$selreg.modify(|_r, w| w.mux().variant($selvar));
// Set up clkdiv
$divreg.modify(|_r, w| {
unsafe { w.div().bits($conf.div.into_bits()) }
.halt()
.asserted()
.reset()
.asserted()
});
$divreg.modify(|_r, w| w.halt().deasserted().reset().deasserted());
while $divreg.read().unstab().is_unstable() {}
Ok($freq / $conf.div.into_divisor())
}};
}
// config types
/// This type represents a divider in the range 1..=16.
///
/// At a hardware level, this is an 8-bit register from 0..=15,
/// which adds one.
///
/// While the *clock* domain seems to use 8-bit dividers, the *peripheral* domain
/// seems to use 4 bit dividers!
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
pub struct Div4(pub(super) u8);
impl Div4 {
/// Divide by one, or no division
pub const fn no_div() -> Self {
Self(0)
}
/// Store a "raw" divisor value that will divide the source by
/// `(n + 1)`, e.g. `Div4::from_raw(0)` will divide the source
/// by 1, and `Div4::from_raw(15)` will divide the source by
/// 16.
pub const fn from_raw(n: u8) -> Option<Self> {
if n > 0b1111 { None } else { Some(Self(n)) }
}
/// Store a specific divisor value that will divide the source
/// by `n`. e.g. `Div4::from_divisor(1)` will divide the source
/// by 1, and `Div4::from_divisor(16)` will divide the source
/// by 16.
///
/// Will return `None` if `n` is not in the range `1..=16`.
/// Consider [`Self::from_raw`] for an infallible version.
pub const fn from_divisor(n: u8) -> Option<Self> {
let Some(n) = n.checked_sub(1) else {
return None;
};
if n > 0b1111 {
return None;
}
Some(Self(n))
}
/// Convert into "raw" bits form
#[inline(always)]
pub const fn into_bits(self) -> u8 {
self.0
}
/// Convert into "divisor" form, as a u32 for convenient frequency math
#[inline(always)]
pub const fn into_divisor(self) -> u32 {
self.0 as u32 + 1
}
}
/// A basic type that always returns an error when `post_enable_config` is called.
///
/// Should only be used as a placeholder.
pub struct UnimplementedConfig;
impl SPConfHelper for UnimplementedConfig {
fn post_enable_config(&self, _clocks: &Clocks) -> Result<u32, ClockError> {
Err(ClockError::UnimplementedConfig)
}
}
/// A basic type that always returns `Ok(0)` when `post_enable_config` is called.
///
/// This should only be used for peripherals that are "ambiently" clocked, like `PORTn`
/// peripherals, which have no selectable/configurable source clock.
pub struct NoConfig;
impl SPConfHelper for NoConfig {
fn post_enable_config(&self, _clocks: &Clocks) -> Result<u32, ClockError> {
Ok(0)
}
}
//
// LPI2c
//
/// Selectable clocks for `Lpi2c` peripherals
#[derive(Debug, Clone, Copy)]
pub enum Lpi2cClockSel {
/// FRO12M/FRO_LF/SIRC clock source, passed through divider
/// "fro_lf_div"
FroLfDiv,
/// FRO180M/FRO_HF/FIRC clock source, passed through divider
/// "fro_hf_div"
FroHfDiv,
/// SOSC/XTAL/EXTAL clock source
ClkIn,
/// clk_1m/FRO_LF divided by 12
Clk1M,
/// Output of PLL1, passed through clock divider,
/// "pll1_clk_div", maybe "pll1_lf_div"?
Pll1ClkDiv,
/// Disabled
None,
}
/// Which instance of the `Lpi2c` is this?
///
/// Should not be directly selectable by end-users.
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
pub enum Lpi2cInstance {
/// Instance 0
Lpi2c0,
/// Instance 1
Lpi2c1,
/// Instance 2
Lpi2c2,
/// Instance 3
Lpi2c3,
}
/// Top level configuration for `Lpi2c` instances.
pub struct Lpi2cConfig {
/// Power state required for this peripheral
pub power: PoweredClock,
/// Clock source
pub source: Lpi2cClockSel,
/// Clock divisor
pub div: Div4,
/// Which instance is this?
// NOTE: should not be user settable
pub(crate) instance: Lpi2cInstance,
}
impl SPConfHelper for Lpi2cConfig {
fn post_enable_config(&self, clocks: &Clocks) -> Result<u32, ClockError> {
// check that source is suitable
let mrcc0 = unsafe { pac::Mrcc0::steal() };
use mcxa_pac::mrcc0::mrcc_lpi2c0_clksel::Mux;
let (clkdiv, clksel) = match self.instance {
Lpi2cInstance::Lpi2c0 => (mrcc0.mrcc_lpi2c0_clkdiv(), mrcc0.mrcc_lpi2c0_clksel()),
Lpi2cInstance::Lpi2c1 => (mrcc0.mrcc_lpi2c1_clkdiv(), mrcc0.mrcc_lpi2c1_clksel()),
Lpi2cInstance::Lpi2c2 => (mrcc0.mrcc_lpi2c2_clkdiv(), mrcc0.mrcc_lpi2c2_clksel()),
Lpi2cInstance::Lpi2c3 => (mrcc0.mrcc_lpi2c3_clkdiv(), mrcc0.mrcc_lpi2c3_clksel()),
};
let (freq, variant) = match self.source {
Lpi2cClockSel::FroLfDiv => {
let freq = clocks.ensure_fro_lf_div_active(&self.power)?;
(freq, Mux::ClkrootFunc0)
}
Lpi2cClockSel::FroHfDiv => {
let freq = clocks.ensure_fro_hf_div_active(&self.power)?;
(freq, Mux::ClkrootFunc2)
}
Lpi2cClockSel::ClkIn => {
let freq = clocks.ensure_clk_in_active(&self.power)?;
(freq, Mux::ClkrootFunc3)
}
Lpi2cClockSel::Clk1M => {
let freq = clocks.ensure_clk_1m_active(&self.power)?;
(freq, Mux::ClkrootFunc5)
}
Lpi2cClockSel::Pll1ClkDiv => {
let freq = clocks.ensure_pll1_clk_div_active(&self.power)?;
(freq, Mux::ClkrootFunc6)
}
Lpi2cClockSel::None => unsafe {
// no ClkrootFunc7, just write manually for now
clksel.write(|w| w.bits(0b111));
clkdiv.modify(|_r, w| w.reset().asserted().halt().asserted());
return Ok(0);
},
};
apply_div4!(self, clksel, clkdiv, variant, freq)
}
}
//
// LPUart
//
/// Selectable clocks for Lpuart peripherals
#[derive(Debug, Clone, Copy)]
pub enum LpuartClockSel {
/// FRO12M/FRO_LF/SIRC clock source, passed through divider
/// "fro_lf_div"
FroLfDiv,
/// FRO180M/FRO_HF/FIRC clock source, passed through divider
/// "fro_hf_div"
FroHfDiv,
/// SOSC/XTAL/EXTAL clock source
ClkIn,
/// FRO16K/clk_16k source
Clk16K,
/// clk_1m/FRO_LF divided by 12
Clk1M,
/// Output of PLL1, passed through clock divider,
/// "pll1_clk_div", maybe "pll1_lf_div"?
Pll1ClkDiv,
/// Disabled
None,
}
/// Which instance of the Lpuart is this?
///
/// Should not be directly selectable by end-users.
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
pub enum LpuartInstance {
/// Instance 0
Lpuart0,
/// Instance 1
Lpuart1,
/// Instance 2
Lpuart2,
/// Instance 3
Lpuart3,
/// Instance 4
Lpuart4,
/// Instance 5
Lpuart5,
}
/// Top level configuration for `Lpuart` instances.
pub struct LpuartConfig {
/// Power state required for this peripheral
pub power: PoweredClock,
/// Clock source
pub source: LpuartClockSel,
/// Clock divisor
pub div: Div4,
/// Which instance is this?
// NOTE: should not be user settable
pub(crate) instance: LpuartInstance,
}
impl SPConfHelper for LpuartConfig {
fn post_enable_config(&self, clocks: &Clocks) -> Result<u32, ClockError> {
// check that source is suitable
let mrcc0 = unsafe { pac::Mrcc0::steal() };
use mcxa_pac::mrcc0::mrcc_lpuart0_clksel::Mux;
let (clkdiv, clksel) = match self.instance {
LpuartInstance::Lpuart0 => (mrcc0.mrcc_lpuart0_clkdiv(), mrcc0.mrcc_lpuart0_clksel()),
LpuartInstance::Lpuart1 => (mrcc0.mrcc_lpuart1_clkdiv(), mrcc0.mrcc_lpuart1_clksel()),
LpuartInstance::Lpuart2 => (mrcc0.mrcc_lpuart2_clkdiv(), mrcc0.mrcc_lpuart2_clksel()),
LpuartInstance::Lpuart3 => (mrcc0.mrcc_lpuart3_clkdiv(), mrcc0.mrcc_lpuart3_clksel()),
LpuartInstance::Lpuart4 => (mrcc0.mrcc_lpuart4_clkdiv(), mrcc0.mrcc_lpuart4_clksel()),
LpuartInstance::Lpuart5 => (mrcc0.mrcc_lpuart5_clkdiv(), mrcc0.mrcc_lpuart5_clksel()),
};
let (freq, variant) = match self.source {
LpuartClockSel::FroLfDiv => {
let freq = clocks.ensure_fro_lf_div_active(&self.power)?;
(freq, Mux::ClkrootFunc0)
}
LpuartClockSel::FroHfDiv => {
let freq = clocks.ensure_fro_hf_div_active(&self.power)?;
(freq, Mux::ClkrootFunc2)
}
LpuartClockSel::ClkIn => {
let freq = clocks.ensure_clk_in_active(&self.power)?;
(freq, Mux::ClkrootFunc3)
}
LpuartClockSel::Clk16K => {
let freq = clocks.ensure_clk_16k_vdd_core_active(&self.power)?;
(freq, Mux::ClkrootFunc4)
}
LpuartClockSel::Clk1M => {
let freq = clocks.ensure_clk_1m_active(&self.power)?;
(freq, Mux::ClkrootFunc5)
}
LpuartClockSel::Pll1ClkDiv => {
let freq = clocks.ensure_pll1_clk_div_active(&self.power)?;
(freq, Mux::ClkrootFunc6)
}
LpuartClockSel::None => unsafe {
// no ClkrootFunc7, just write manually for now
clksel.write(|w| w.bits(0b111));
clkdiv.modify(|_r, w| {
w.reset().asserted();
w.halt().asserted();
w
});
return Ok(0);
},
};
// set clksel
apply_div4!(self, clksel, clkdiv, variant, freq)
}
}
//
// OSTimer
//
/// Selectable clocks for the OSTimer peripheral
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
pub enum OstimerClockSel {
/// 16k clock, sourced from FRO16K (Vdd Core)
Clk16kVddCore,
/// 1 MHz Clock sourced from FRO12M
Clk1M,
/// Disabled
None,
}
/// Top level configuration for the `OSTimer` peripheral
pub struct OsTimerConfig {
/// Power state required for this peripheral
pub power: PoweredClock,
/// Selected clock source for this peripheral
pub source: OstimerClockSel,
}
impl SPConfHelper for OsTimerConfig {
fn post_enable_config(&self, clocks: &Clocks) -> Result<u32, ClockError> {
let mrcc0 = unsafe { pac::Mrcc0::steal() };
Ok(match self.source {
OstimerClockSel::Clk16kVddCore => {
let freq = clocks.ensure_clk_16k_vdd_core_active(&self.power)?;
mrcc0.mrcc_ostimer0_clksel().write(|w| w.mux().clkroot_16k());
freq
}
OstimerClockSel::Clk1M => {
let freq = clocks.ensure_clk_1m_active(&self.power)?;
mrcc0.mrcc_ostimer0_clksel().write(|w| w.mux().clkroot_1m());
freq
}
OstimerClockSel::None => {
mrcc0.mrcc_ostimer0_clksel().write(|w| unsafe { w.mux().bits(0b11) });
0
}
})
}
}
//
// Adc
//
/// Selectable clocks for the ADC peripheral
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
pub enum AdcClockSel {
/// Divided `fro_lf`/`clk_12m`/FRO12M source
FroLfDiv,
/// Gated `fro_hf`/`FRO180M` source
FroHf,
/// External Clock Source
ClkIn,
/// 1MHz clock sourced by a divided `fro_lf`/`clk_12m`
Clk1M,
/// Internal PLL output, with configurable divisor
Pll1ClkDiv,
/// No clock/disabled
None,
}
/// Top level configuration for the ADC peripheral
pub struct AdcConfig {
/// Power state required for this peripheral
pub power: PoweredClock,
/// Selected clock-source for this peripheral
pub source: AdcClockSel,
/// Pre-divisor, applied to the upstream clock output
pub div: Div4,
}
impl SPConfHelper for AdcConfig {
fn post_enable_config(&self, clocks: &Clocks) -> Result<u32, ClockError> {
use mcxa_pac::mrcc0::mrcc_adc_clksel::Mux;
let mrcc0 = unsafe { pac::Mrcc0::steal() };
let (freq, variant) = match self.source {
AdcClockSel::FroLfDiv => {
let freq = clocks.ensure_fro_lf_div_active(&self.power)?;
(freq, Mux::ClkrootFunc0)
}
AdcClockSel::FroHf => {
let freq = clocks.ensure_fro_hf_active(&self.power)?;
(freq, Mux::ClkrootFunc1)
}
AdcClockSel::ClkIn => {
let freq = clocks.ensure_clk_in_active(&self.power)?;
(freq, Mux::ClkrootFunc3)
}
AdcClockSel::Clk1M => {
let freq = clocks.ensure_clk_1m_active(&self.power)?;
(freq, Mux::ClkrootFunc5)
}
AdcClockSel::Pll1ClkDiv => {
let freq = clocks.ensure_pll1_clk_div_active(&self.power)?;
(freq, Mux::ClkrootFunc6)
}
AdcClockSel::None => {
mrcc0.mrcc_adc_clksel().write(|w| unsafe {
// no ClkrootFunc7, just write manually for now
w.mux().bits(0b111)
});
mrcc0.mrcc_adc_clkdiv().modify(|_r, w| {
w.reset().asserted();
w.halt().asserted();
w
});
return Ok(0);
}
};
let clksel = mrcc0.mrcc_adc_clksel();
let clkdiv = mrcc0.mrcc_adc_clkdiv();
apply_div4!(self, clksel, clkdiv, variant, freq)
}
}
|