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
//! Clock Management HAL for RP2350
//!
//! This module provides a safe interface for configuring the RP2350 clock system.
//! The clock system provides clocks for various peripherals from different sources
//! including crystal oscillators, PLLs, and internal oscillators.
//!
//! # Clock Tree Architecture
//!
//! The RP2350 clock tree has a hierarchical structure with clock sources at the top,
//! feeding into configurable clock generators that supply various peripherals.
//!
//! ```text
//! ╔══════════════════════════════════════════════════════════════════════════════╗
//! ║ CLOCK SOURCES (Layer 0) ║
//! ╠══════════════════════════════════════════════════════════════════════════════╣
//! ║ ┌────────┐ ┌────────┐ ┌────────┐ ┌─────────┐ ┌─────────┐ ┌──────────┐ ║
//! ║ │ XOSC │ │ ROSC │ │ LPOSC │ │ PLL_SYS │ │ PLL_USB │ │ GPIN0/1 │ ║
//! ║ │ 12 MHz │ │ ~6 MHz │ │ 32 kHz │ │ 125-150 │ │ 48 MHz │ │ External │ ║
//! ║ └───┬────┘ └───┬────┘ └───┬────┘ └────┬────┘ └────┬────┘ └────┬─────┘ ║
//! ║ │ │ │ │ │ │ ║
//! ╠══════╪═══════════╪═══════════╪════════════╪════════════╪════════════╪════════╣
//! ║ │ │ │ │ │ │ ║
//! ║ ▼ ▼ ▼ │ ▼ │ ║
//! ║ ┌─────────────────────────────────┐ │ ┌──────────────┐ │ ║
//! ║ │ CLK_REF MUX │◄─────┼─────│ (auxsrc) │◄───┤ ║
//! ║ │ src: XOSC|ROSC|LPOSC|AUX │ │ └──────────────┘ │ ║
//! ║ └───────────────┬─────────────────┘ │ │ ║
//! ║ │ ÷DIV (1-255) │ │ ║
//! ║ ▼ │ │ ║
//! ║ ┌──────────┐ │ │ ║
//! ║ │ clk_ref │ ─────────────────────────────────────────────────► ║
//! ║ │ (stable) │ │ │ ║
//! ║ └────┬─────┘ │ │ ║
//! ║ │ │ │ ║
//! ╠════════════════╪══════════════════════════╪═════════════════════════╪════════╣
//! ║ SYSTEM │ │ │ ║
//! ║ CLOCK ▼ ▼ ▼ ║
//! ║ (Layer 1) ┌─────────────────────────────────────────────────────────┐ ║
//! ║ │ CLK_SYS MUX │ ║
//! ║ │ src: CLK_REF (glitchless) | AUX │ ║
//! ║ │ auxsrc: PLL_SYS|PLL_USB|ROSC|XOSC|GPIN0|GPIN1 │ ║
//! ║ └───────────────────────┬─────────────────────────────────┘ ║
//! ║ │ ÷DIV (16.16 fixed-point) ║
//! ║ ▼ ║
//! ║ ┌──────────────┐ ║
//! ║ │ clk_sys │ ──────► CPU, Cortex-M33 ║
//! ║ │ (up to 150 │ ──────► AHB Bus ║
//! ║ │ MHz) │ ──────► APB Bus ║
//! ║ └──────┬───────┘ ║
//! ║ │ ║
//! ╠═════════════════════════════════════╪════════════════════════════════════════╣
//! ║ PERIPHERAL CLOCKS (Layer 2) │ ║
//! ║ ▼ ║
//! ║ ┌──────────────────────────────────────────────────────────────────────┐ ║
//! ║ │ ┌─────────────┐ ┌─────────────┐ ┌─────────────┐ ┌──────────┐ │ ║
//! ║ │ │ CLK_PERI │ │ CLK_USB │ │ CLK_ADC │ │ CLK_HSTX │ │ ║
//! ║ │ └──────┬──────┘ └──────┬──────┘ └──────┬──────┘ └────┬─────┘ │ ║
//! ║ │ ▼ ▼ ▼ ▼ │ ║
//! ║ │ UART, SPI, USB Device/ ADC Module DVI/HSTX │ ║
//! ║ │ I2C, etc. Host │ ║
//! ║ └──────────────────────────────────────────────────────────────────────┘ ║
//! ║ ║
//! ╠══════════════════════════════════════════════════════════════════════════════╣
//! ║ GPIO CLOCK OUTPUTS (Optional) ║
//! ║ ┌──────────────────────────────────────────────────────────────────────┐ ║
//! ║ │ CLK_GPOUT0-3: Can output any internal clock to GPIO pins │ ║
//! ║ │ Sources: All clocks + PLL outputs + oscillators │ ║
//! ║ │ Features: 16.16 fractional divider for precise frequencies │ ║
//! ║ └──────────────────────────────────────────────────────────────────────┘ ║
//! ╚══════════════════════════════════════════════════════════════════════════════╝
//! ```
//!
//! # Clock Configuration Flow
//!
//! ```text
//! ┌─────────────────────────────────────────────────────────────────┐
//! │ 1. Boot: ROSC running (~6.5 MHz) │
//! │ ↓ │
//! │ 2. Start XOSC (12 MHz external crystal) │
//! │ ↓ wait for stable │
//! │ 3. Switch clk_ref to XOSC │
//! │ ↓ │
//! │ 4. Switch clk_sys to clk_ref (safe intermediate state) │
//! │ ↓ │
//! │ 5. Configure PLL_SYS (e.g., 150 MHz) and PLL_USB (48 MHz) │
//! │ ↓ wait for lock │
//! │ 6. Switch clk_sys to PLL_SYS │
//! │ ↓ │
//! │ 7. Configure peripheral clocks (clk_peri, clk_usb, clk_adc...) │
//! └─────────────────────────────────────────────────────────────────┘
//! ```
//!
//! # Important Constraints
//!
//! | Clock | Requirement | Typical Source |
//! |-----------|------------------------------|----------------|
//! | clk_usb | Must be exactly 48 MHz | PLL_USB |
//! | clk_adc | Must be exactly 48 MHz | PLL_USB |
//! | clk_ref | Should be stable (use XOSC) | XOSC |
//! | clk_sys | Max 150 MHz | PLL_SYS |
//!
//! # Example
//!
//! ```ignore
//! use rp2350::clocks::ClockBuilder;
//!
//! let dp = rp2350_pac::Peripherals::take().unwrap();
//!
//! // Recommended: use ClockBuilder
//! let clocks = ClockBuilder::new()
//! .enable_usb()
//! .enable_adc()
//! .build(dp.CLOCKS, dp.XOSC, dp.PLL_SYS, dp.PLL_USB);
//! // clk_sys = 150 MHz, clk_usb = 48 MHz, clk_peri = 150 MHz
//!
//! // Custom: 125 MHz sys, stable 48 MHz for UART baud rates
//! let clocks = ClockBuilder::new()
//! .sys_freq_mhz(125)
//! .peri_stable_48mhz()
//! .build(dp.CLOCKS, dp.XOSC, dp.PLL_SYS, dp.PLL_USB);
//! ```
// ============================================================================
// Sub-modules
// ============================================================================
// Clock sources (Layer 0)
// Clock tree logic (private sub-modules, types re-exported below)
// ============================================================================
// Re-exports — public API
// ============================================================================
// Clock sources
pub use Lposc;
pub use ;
pub use Rosc;
pub use Xosc;
// Clock tree types
pub use ;
// Clock gate / sleep control
pub use ClkPeripheral;
// Core clock controller
pub use Clocks;
// Clock builder
pub use ClockBuilder;