rp2350 0.1.0

Hardware Abstraction Layer for RP2350 microcontrollers
Documentation
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
//! PLL (Phase-Locked Loop) HAL for RP2350
//!
//! This module provides access to the PLL_SYS and PLL_USB peripherals
//! for generating high-frequency clocks from the crystal oscillator.
//!
//! ## Features
//!
//! - Configure VCO frequency (750-1600 MHz)
//! - Configurable reference divider (1-63)
//! - Configurable feedback divider (16-320)
//! - Post dividers for final output frequency
//! - Lock detection
//!
//! ## PLL Output Frequency
//!
//! The PLL output frequency is calculated as:
//! ```text
//! VCO = (FREF / REFDIV) * FBDIV
//! Output = VCO / (POSTDIV1 * POSTDIV2)
//! ```
//!
//! ## Example
//!
//! ```ignore
//! use rp2350::clocks::pll::{PllSys, PllUsb};
//! use rp2350_pac::PLL_SYS;
//!
//! // Get PLL_SYS peripheral
//! let pll_sys_pac = unsafe { PLL_SYS::steal() };
//! let pll_sys = PllSys::init(pll_sys_pac);
//!
//! // Configure for 150 MHz (assuming 12 MHz XOSC)
//! // VCO = 12 MHz / 1 * 125 = 1500 MHz
//! // Output = 1500 MHz / (5 * 2) = 150 MHz
//! pll_sys.configure(1, 125, 5, 2);
//!
//! // Wait for lock
//! while !pll_sys.is_locked() {}
//! ```

use crate::Steal;
use rp2350_pac::{PLL_SYS, PLL_USB};

/// Macro to implement PLL peripheral
macro_rules! impl_pll {
    ($Pll:ident, $PLL:ident, $pll_mod:ident) => {
        /// PLL peripheral wrapper
        pub struct $Pll {
            pll: $PLL,
        }

        impl $Pll {
            /// Initialize the PLL driver.
            ///
            /// # Arguments
            /// * `pll` - The PLL peripheral from PAC
            #[inline]
            pub fn init(pll: $PLL) -> Self {
                Self { pll }
            }

            /// Obtain a PLL handle without ownership checks.
            ///
            /// # Safety
            ///
            /// The caller must ensure the peripheral has been initialised
            /// and that no data races occur.
            #[inline]
            pub unsafe fn steal() -> Steal<Self> {
                Steal(Self {
                    pll: unsafe { $PLL::steal() },
                })
            }

            /// Get reference to the underlying register block
            ///
            /// # Safety
            /// Direct register access can bypass the HAL's safety guarantees.
            /// The caller must ensure operations do not conflict with the HAL state.
            #[inline]
            pub unsafe fn regs(&self) -> &rp2350_pac::$pll_mod::RegisterBlock {
                &self.pll
            }

            /// Release the PLL peripheral
            #[inline]
            pub fn release(self) -> $PLL {
                self.pll
            }

            // ================================================================
            // Status
            // ================================================================

            /// Check if PLL is locked
            #[inline]
            pub fn is_locked(&self) -> bool {
                self.pll.cs.lock()
            }

            /// Check if PLL is NOT locked (sticky flag)
            ///
            /// This flag is set when PLL loses lock and stays set until cleared.
            #[inline]
            pub fn is_lock_n(&self) -> bool {
                self.pll.cs.lock_n()
            }

            /// Clear the lock_n sticky flag
            #[inline]
            pub fn clear_lock_n(&mut self) {
                self.pll.cs.set_lock_n();
            }

            /// Check if bypass mode is enabled
            #[inline]
            pub fn is_bypass(&self) -> bool {
                self.pll.cs.bypass()
            }

            /// Enable or disable bypass mode
            ///
            /// In bypass mode, the reference clock passes directly to output.
            #[inline]
            pub fn set_bypass(&mut self, bypass: bool) {
                if bypass {
                    self.pll.cs.set_bypass();
                } else {
                    self.pll.cs.clr_bypass();
                }
            }

            /// Get reference divider value
            #[inline]
            pub fn refdiv(&self) -> u8 {
                self.pll.cs.refdiv() as u8
            }

            /// Set reference divider (1-63)
            ///
            /// The reference frequency (XOSC) is divided by this before the PLL.
            /// FREF = XOSC / REFDIV
            #[inline]
            pub fn set_refdiv(&mut self, div: u8) {
                let div = div.clamp(1, 63) as u32;
                self.pll.cs.write_refdiv(div);
            }

            // ================================================================
            // Power Control
            // ================================================================

            /// Power down the entire PLL
            #[inline]
            pub fn power_down(&mut self) {
                self.pll.pwr.set_pd();
            }

            /// Power up the PLL (clear power-down bit)
            #[inline]
            pub fn power_up(&mut self) {
                self.pll.pwr.clr_pd();
            }

            /// Check if PLL is powered down
            #[inline]
            pub fn is_powered_down(&self) -> bool {
                self.pll.pwr.pd()
            }

            /// Power down the VCO
            #[inline]
            pub fn vco_power_down(&mut self) {
                self.pll.pwr.set_vcopd();
            }

            /// Power up the VCO
            #[inline]
            pub fn vco_power_up(&mut self) {
                self.pll.pwr.clr_vcopd();
            }

            /// Check if VCO is powered down
            #[inline]
            pub fn is_vco_powered_down(&self) -> bool {
                self.pll.pwr.vcopd()
            }

            /// Power down the post dividers
            #[inline]
            pub fn postdiv_power_down(&mut self) {
                self.pll.pwr.set_postdivpd();
            }

            /// Power up the post dividers
            #[inline]
            pub fn postdiv_power_up(&mut self) {
                self.pll.pwr.clr_postdivpd();
            }

            /// Check if post dividers are powered down
            #[inline]
            pub fn is_postdiv_powered_down(&self) -> bool {
                self.pll.pwr.postdivpd()
            }

            /// Power down the DSM (delta-sigma modulator)
            #[inline]
            pub fn dsm_power_down(&mut self) {
                self.pll.pwr.set_dsmpd();
            }

            /// Power up the DSM
            #[inline]
            pub fn dsm_power_up(&mut self) {
                self.pll.pwr.clr_dsmpd();
            }

            // ================================================================
            // Divider Configuration
            // ================================================================

            /// Get feedback divider value
            #[inline]
            pub fn fbdiv(&self) -> u16 {
                self.pll.fbdiv_int.fbdiv_int() as u16
            }

            /// Set feedback divider (16-320)
            ///
            /// VCO frequency = (FREF / REFDIV) * FBDIV
            /// VCO must be 750-1600 MHz
            #[inline]
            pub fn set_fbdiv(&mut self, div: u16) {
                let div = div.clamp(16, 320) as u32;
                self.pll.fbdiv_int.write_fbdiv_int(div);
            }

            /// Get post divider 1 value
            #[inline]
            pub fn postdiv1(&self) -> u8 {
                self.pll.prim.postdiv1() as u8
            }

            /// Get post divider 2 value
            #[inline]
            pub fn postdiv2(&self) -> u8 {
                self.pll.prim.postdiv2() as u8
            }

            /// Set post dividers (1-7 each)
            ///
            /// Output = VCO / (POSTDIV1 * POSTDIV2)
            /// For best performance, use POSTDIV1 >= POSTDIV2
            #[inline]
            pub fn set_postdiv(&mut self, postdiv1: u8, postdiv2: u8) {
                let postdiv1 = postdiv1.clamp(1, 7) as u32;
                let postdiv2 = postdiv2.clamp(1, 7) as u32;
                self.pll.prim.write_postdiv1(postdiv1);
                self.pll.prim.write_postdiv2(postdiv2);
            }

            // ================================================================
            // Interrupts
            // ================================================================

            /// Check if lock_n interrupt is pending (raw)
            #[inline]
            pub fn is_intr_lock_n(&self) -> bool {
                self.pll.intr.lock_n_sticky()
            }

            /// Clear lock_n interrupt
            #[inline]
            pub fn clear_intr_lock_n(&mut self) {
                self.pll.intr.set_lock_n_sticky();
            }

            /// Enable lock_n interrupt
            #[inline]
            pub fn enable_intr_lock_n(&mut self) {
                self.pll.inte.set_lock_n_sticky();
            }

            /// Disable lock_n interrupt
            #[inline]
            pub fn disable_intr_lock_n(&mut self) {
                self.pll.inte.clr_lock_n_sticky();
            }

            /// Check if lock_n interrupt is enabled
            #[inline]
            pub fn is_intr_lock_n_enabled(&self) -> bool {
                self.pll.inte.lock_n_sticky()
            }

            /// Check masked interrupt status
            #[inline]
            pub fn is_ints_lock_n(&self) -> bool {
                self.pll.ints.lock_n_sticky()
            }

            // ================================================================
            // High-Level Configuration
            // ================================================================

            /// Configure the PLL with all parameters
            ///
            /// # Arguments
            /// * `refdiv` - Reference divider (1-63)
            /// * `fbdiv` - Feedback divider (16-320)
            /// * `postdiv1` - Post divider 1 (1-7)
            /// * `postdiv2` - Post divider 2 (1-7)
            ///
            /// # Example
            ///
            /// For 150 MHz output from 12 MHz XOSC:
            /// ```ignore
            /// pll.configure(1, 125, 5, 2);
            /// // VCO = 12 / 1 * 125 = 1500 MHz
            /// // Out = 1500 / (5 * 2) = 150 MHz
            /// ```
            pub fn configure(&mut self, refdiv: u8, fbdiv: u16, postdiv1: u8, postdiv2: u8) {
                // After reset, all power bits should be set (PLL off)
                // Configure dividers first while PLL is off
                self.set_refdiv(refdiv);
                self.set_fbdiv(fbdiv);

                // Clear PD and VCOPD to turn on PLL and VCO
                // Keep POSTDIVPD set (post dividers still off)
                // Keep DSMPD set (DSM not used for integer mode)
                let power_on_mask = (1 << 0) | (1 << 5); // PD | VCOPD
                self.pll.pwr.clr_bits(power_on_mask);

                // Wait for VCO to lock (bounded)
                crate::spin::spin_wait("PLL lock", || self.is_locked());

                // Configure post dividers
                self.set_postdiv(postdiv1, postdiv2);

                // Power up post dividers
                self.postdiv_power_up();

                // Clear any lock loss indication
                self.clear_lock_n();
            }

            /// Wait for PLL to lock
            #[inline]
            pub fn wait_lock(&self) {
                while !self.is_locked() {
                    rp2350_rt::asm::nop();
                }
            }

            /// Disable the PLL (power down all components)
            pub fn disable(&mut self) {
                self.postdiv_power_down();
                self.vco_power_down();
                self.power_down();
            }
        }
    };
}

// Implement PLL for both PLL_SYS and PLL_USB
impl_pll!(PllSys, PLL_SYS, pll_sys);
impl_pll!(PllUsb, PLL_USB, pll_usb);

/// Common PLL configuration for typical use cases
pub struct PllConfig {
    /// Reference divider
    pub refdiv: u8,
    /// Feedback divider
    pub fbdiv: u16,
    /// Post divider 1
    pub postdiv1: u8,
    /// Post divider 2
    pub postdiv2: u8,
}

impl PllConfig {
    /// Create config for 125 MHz from 12 MHz XOSC
    pub const fn sys_125mhz() -> Self {
        Self {
            refdiv: 1,
            fbdiv: 125,
            postdiv1: 6,
            postdiv2: 2,
        }
    }

    /// Create config for 133 MHz from 12 MHz XOSC
    pub const fn sys_133mhz() -> Self {
        Self {
            refdiv: 1,
            fbdiv: 133,
            postdiv1: 6,
            postdiv2: 2,
        }
    }

    /// Create config for 150 MHz from 12 MHz XOSC
    pub const fn sys_150mhz() -> Self {
        Self {
            refdiv: 1,
            fbdiv: 125,
            postdiv1: 5,
            postdiv2: 2,
        }
    }

    /// Create config for 48 MHz from 12 MHz XOSC (USB)
    pub const fn usb_48mhz() -> Self {
        Self {
            refdiv: 1,
            fbdiv: 100,
            postdiv1: 5,
            postdiv2: 5,
        }
    }

    /// Calculate VCO frequency in kHz
    pub const fn vco_freq_khz(&self, xosc_khz: u32) -> u32 {
        (xosc_khz / self.refdiv as u32) * self.fbdiv as u32
    }

    /// Calculate output frequency in kHz
    pub const fn output_freq_khz(&self, xosc_khz: u32) -> u32 {
        self.vco_freq_khz(xosc_khz) / (self.postdiv1 as u32 * self.postdiv2 as u32)
    }
}