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
//! Clock type definitions for RP2350
//!
//! This module contains all enumerations used to select clock sources,
//! auxiliary sources, frequency counter sources, and debug clock sources
//! throughout the clock tree.
//!
//! # Clock Tree Layers
//!
//! Types are grouped by their position in the clock tree hierarchy:
//!
//! - **Layer 0 (Sources)**: The fundamental oscillators and PLLs are defined
//! in the sibling `xosc`, `rosc`, `lposc`, and `pll` modules.
//!
//! - **Layer 1 (Reference & System Clocks)**: [`ClkRefSrc`], [`ClkRefAuxSrc`],
//! [`ClkSysAuxSrc`]
//!
//! - **Layer 2 (Peripheral Clocks)**: [`ClkPeriAuxSrc`], [`ClkUsbAuxSrc`],
//! [`ClkAdcAuxSrc`], [`ClkHstxAuxSrc`]
//!
//! - **GPIO Outputs**: [`ClkGpOutAuxSrc`], [`GpOutId`]
//!
//! - **Miscellaneous**: [`ClockId`], [`FcSrc`], [`DftClkSrc`]
// ============================================================================
// Clock Identifiers
// ============================================================================
/// Number of core clock generators tracked by [`Clocks`].
///
/// The four GPIO clock outputs are tracked separately via [`GpOutId`].
pub const CLK_COUNT: usize = 6;
/// Identifiers for the six core clock generators.
///
/// The clocks are organised by their position in the clock tree:
/// - Layer 0: Clock sources (XOSC, ROSC, PLLs) — not in this enum
/// - Layer 1: Reference and System clocks ([`ClockId::Ref`], [`ClockId::Sys`])
/// - Layer 2: Peripheral clocks ([`ClockId::Peri`], [`ClockId::Usb`],
/// [`ClockId::Adc`], [`ClockId::Hstx`])
///
/// GPIO clock outputs are handled by the separate [`GpOutId`] enum.
// ============================================================================
// GPIO Clock Output Identifier
// ============================================================================
/// Identifies one of the four GPIO clock output generators (GPOUT0–3)
///
/// Replaces bare `u8` indices to provide compile-time safety when configuring
/// or disabling GPIO clock outputs.
///
/// # Example
/// ```ignore
/// clocks.configure_gpout(GpOutId::GpOut0, ClkGpOutAuxSrc::ClkSys, sys_freq, sys_freq / 4);
/// ```
// ============================================================================
// Clock Tree Layer 1: Reference Clock (CLK_REF) Sources
// ============================================================================
//
// CLK_REF provides a stable clock for the system clock mux and watchdog timer.
//
// ROSC ────┐
// LPOSC ────┼──► [CLK_REF glitchless MUX] ──► ÷DIV ──► clk_ref
// XOSC ────┤
// AUX ────┘ (AUX = PLL_USB, GPIN0/1; selected via ClkRefAuxSrc)
//
// ============================================================================
/// Reference clock (CLK_REF) primary source selection
///
/// These options go through the glitchless mux. Use [`ClkRefAuxSrc`] together
/// with [`ClkRefSrc::Aux`] to select auxiliary sources such as PLL_USB or GPIN.
/// Reference clock (CLK_REF) auxiliary source selection
///
/// Active when [`ClkRefSrc::Aux`] is selected as the primary source.
// ============================================================================
// Clock Tree Layer 1: System Clock (CLK_SYS) Sources
// ============================================================================
//
// CLK_SYS drives the CPU (Cortex-M33), AHB bus, and APB bus.
//
// clk_ref ────┐
// ├──► [CLK_SYS glitchless MUX] ──► ÷DIV (16.16) ──► clk_sys
// AUX ────┘ (AUX selected via ClkSysAuxSrc)
//
// ============================================================================
/// System clock (CLK_SYS) auxiliary source selection
///
/// Selects the source fed through the auxiliary mux. Use [`Clocks::configure_sys_aux`]
/// to switch clk_sys to one of these sources. PLL_SYS is recommended for maximum
/// performance (125–150 MHz); PLL_USB (48 MHz) is useful for low-power operation.
///
/// [`Clocks::configure_sys_aux`]: super::controller::Clocks::configure_sys_aux
// ============================================================================
// Clock Tree Layer 2: Peripheral Clocks
// ============================================================================
//
// These clocks derive from Layer 1 or directly from PLLs / oscillators.
// Key constraints:
// CLK_USB — MUST be exactly 48 MHz
// CLK_ADC — MUST be exactly 48 MHz
// CLK_PERI — Flexible; clk_sys is simplest
// CLK_HSTX — High-speed clock for DVI/HSTX
//
// ============================================================================
/// Peripheral clock (CLK_PERI) auxiliary source selection
///
/// CLK_PERI supplies UART, SPI, I2C and similar peripherals.
///
/// | Choice | Trade-off |
/// |--------------|-----------------------------------------------------|
/// | `ClkSys` | Simplest, but baud rates change with clk_sys |
/// | `PllUsb` | 48 MHz fixed — stable baud rates |
/// | `PllSys` | Direct PLL — independent of clk_sys divider |
/// USB clock (CLK_USB) auxiliary source selection
///
/// **USB requires exactly 48 MHz.** Use `PllUsb` for correct operation.
/// ADC clock (CLK_ADC) auxiliary source selection
///
/// **ADC requires exactly 48 MHz.** Use `PllUsb` for correct operation.
/// HSTX clock (CLK_HSTX) auxiliary source selection
///
/// High-speed TX clock for DVI and other high-speed serial interfaces.
// ============================================================================
// GPIO Clock Outputs (CLK_GPOUT0–3)
// ============================================================================
//
// GPOUT clocks export any internal clock to GPIO pins. Each output has a
// 16.16 fractional divider for precise frequency generation.
//
// ============================================================================
/// GPOUT clock (CLK_GPOUT0–3) auxiliary source selection
///
/// GPOUT outputs can expose almost any internal clock on a GPIO pin — useful
/// for debugging clock configuration or providing a reference to external ICs.
// ============================================================================
// Frequency Counter Sources
// ============================================================================
/// Frequency counter (FC0) source selection
///
/// The FC0 counter can measure any of these sources against `clk_ref`.
/// Useful for verifying clock configuration and debugging.
///
/// # Usage
/// See [`Clocks::frequency_count_khz`](super::controller::Clocks::frequency_count_khz).
// ============================================================================
// DFT Debug Clock Sources
// ============================================================================
/// DFT (Design-for-Test) debug clock source for oscillator bypass
///
/// Each oscillator (XOSC, ROSC, LPOSC) can have its output replaced by a
/// debug source for silicon testing and fault isolation. The exact source
/// depends on which oscillator is being configured:
///
/// | Oscillator | [`DebugClk`](DftClkSrc::DebugClk) source | [`GpIn`](DftClkSrc::GpIn) source |
/// |------------|------------------------------------------|----------------------------------|
/// | XOSC | PLL_USB primary | GPIN0 |
/// | ROSC | PLL_SYS primary | GPIN1 |
/// | LPOSC | PLL_USB primary | GPIN1 |
///
/// **Warning**: Misuse of DFT clocks can cause unstable operation.