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
//! System Configuration (SYSCFG) HAL for RP2350
//!
//! The SYSCFG block provides control over various chip-level features including:
//! - Processor status monitoring
//! - GPIO input synchronizer bypass
//! - Debug port control
//! - Memory power management
//!
//! ## Example
//!
//! ```ignore
//! use rp2350::syscfg::{MemoryBank, Syscfg};
//! use rp2350_pac::SYSCFG;
//!
//! let syscfg_pac = unsafe { SYSCFG::steal() };
//! let mut syscfg = Syscfg::init(syscfg_pac);
//!
//! // Check processor status
//! if syscfg.is_proc0_halted() {
//! // Handle processor 0 halted
//! }
//!
//! // Put SRAM bank 5 into low power mode
//! syscfg.set_memory_power_down(MemoryBank::Sram5, true);
//! ```
/// Write a single-bit bypass field based on a `bypass` flag.
macro_rules! set_bypass {
($reg:expr, $set:ident, $clr:ident, $bypass:expr) => {
if $bypass {
$reg.$set();
} else {
$reg.$clr();
}
};
}
use crate::Steal;
use crate::reset::Resettable;
/// Memory bank identifiers for power control
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
pub enum MemoryBank {
/// SRAM bank 0
Sram0 = 0,
/// SRAM bank 1
Sram1 = 1,
/// SRAM bank 2
Sram2 = 2,
/// SRAM bank 3
Sram3 = 3,
/// SRAM bank 4
Sram4 = 4,
/// SRAM bank 5
Sram5 = 5,
/// SRAM bank 6
Sram6 = 6,
/// SRAM bank 7
Sram7 = 7,
/// SRAM bank 8
Sram8 = 8,
/// SRAM bank 9
Sram9 = 9,
/// USB memory
Usb = 10,
/// ROM
Rom = 11,
/// Boot RAM
BootRam = 12,
}
impl MemoryBank {
/// Get the bit mask for this memory bank
#[inline]
pub const fn mask(self) -> u32 {
1 << (self as u32)
}
}
/// System Configuration driver
///
/// Owns the PAC SYSCFG peripheral to enforce proper ownership semantics.
pub struct Syscfg {
syscfg: rp2350_pac::SYSCFG,
}
impl Syscfg {
/// Initialize the SYSCFG driver.
///
/// Takes ownership of the [`rp2350_pac::SYSCFG`] register block, resets
/// the peripheral, and brings it out of reset.
///
/// # Arguments
/// * `syscfg` - The SYSCFG peripheral from PAC
#[inline]
pub fn init(syscfg: rp2350_pac::SYSCFG) -> Self {
// Reset then unreset to guarantee clean register state
<rp2350_pac::SYSCFG as Resettable>::init();
Self { syscfg }
}
/// Obtain a Syscfg handle without ownership checks.
///
/// # Safety
///
/// The caller must ensure [`Syscfg::init()`] has been called first
/// and that no data races occur.
#[inline]
pub unsafe fn steal() -> Steal<Self> {
Steal(Self {
syscfg: unsafe { rp2350_pac::SYSCFG::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::syscfg::RegisterBlock {
&self.syscfg
}
/// Release the SYSCFG peripheral
#[inline]
pub fn release(self) -> rp2350_pac::SYSCFG {
self.syscfg
}
// ========================================================================
// Processor Status
// ========================================================================
/// Check if processor 0 has halted
#[inline]
pub fn is_proc0_halted(&self) -> bool {
self.syscfg.proc_config.proc0_halted()
}
/// Check if processor 1 has halted
#[inline]
pub fn is_proc1_halted(&self) -> bool {
self.syscfg.proc_config.proc1_halted()
}
// ========================================================================
// GPIO Input Synchronizer Bypass
// ========================================================================
/// Bypass the input synchronizer for a GPIO pin.
///
/// The synchronizer-bypass bits are scattered across two registers:
///
/// | GPIO | Register / field |
/// |-------------------|-------------------------------------|
/// | 0..=31 | `PROC_IN_SYNC_BYPASS.GPIO` |
/// | 32..=47 | `PROC_IN_SYNC_BYPASS_HI.GPIO` |
/// | 56 (USB D+) | `PROC_IN_SYNC_BYPASS_HI.USB_DP` |
/// | 57 (USB D-) | `PROC_IN_SYNC_BYPASS_HI.USB_DM` |
/// | 58 (QSPI SCK) | `PROC_IN_SYNC_BYPASS_HI.QSPI_SCK` |
/// | 59 (QSPI CSn) | `PROC_IN_SYNC_BYPASS_HI.QSPI_CSN` |
/// | 60..=63 (QSPI SD) | `PROC_IN_SYNC_BYPASS_HI.QSPI_SD` |
///
/// GPIO 48..=55 do not exist and will panic.
///
/// # Warning
/// Bypassing the synchronizer can inject metastabilities into the
/// processor. Only use this to save two cycles of input latency.
#[inline]
pub fn set_sync_bypass(&self, pin: u8, bypass: bool) {
match pin {
0..=31 => {
let bit = 1u32 << pin;
if bypass {
self.syscfg.proc_in_sync_bypass.set_bits(bit);
} else {
self.syscfg.proc_in_sync_bypass.clr_bits(bit);
}
}
32..=47 => {
let bit = 1u32 << (pin - 32);
if bypass {
self.syscfg.proc_in_sync_bypass_hi.set_bits(bit);
} else {
self.syscfg.proc_in_sync_bypass_hi.clr_bits(bit);
}
}
56 => set_bypass!(
self.syscfg.proc_in_sync_bypass_hi,
set_usb_dp,
clr_usb_dp,
bypass
),
57 => set_bypass!(
self.syscfg.proc_in_sync_bypass_hi,
set_usb_dm,
clr_usb_dm,
bypass
),
58 => set_bypass!(
self.syscfg.proc_in_sync_bypass_hi,
set_qspi_sck,
clr_qspi_sck,
bypass
),
59 => set_bypass!(
self.syscfg.proc_in_sync_bypass_hi,
set_qspi_csn,
clr_qspi_csn,
bypass
),
60..=63 => {
// QSPI SD0..SD3 occupy bits 28..31.
let bit = 1u32 << (28 + (pin - 60));
if bypass {
self.syscfg.proc_in_sync_bypass_hi.set_bits(bit);
} else {
self.syscfg.proc_in_sync_bypass_hi.clr_bits(bit);
}
}
_ => panic!("invalid GPIO pin for sync bypass: {pin}"),
}
}
/// Get the sync bypass state for a GPIO pin.
#[inline]
pub fn sync_bypass(&self, pin: u8) -> bool {
match pin {
0..=31 => (self.syscfg.proc_in_sync_bypass.gpio() & (1u32 << pin)) != 0,
32..=47 => (self.syscfg.proc_in_sync_bypass_hi.gpio() & (1u32 << (pin - 32))) != 0,
56 => self.syscfg.proc_in_sync_bypass_hi.usb_dp(),
57 => self.syscfg.proc_in_sync_bypass_hi.usb_dm(),
58 => self.syscfg.proc_in_sync_bypass_hi.qspi_sck(),
59 => self.syscfg.proc_in_sync_bypass_hi.qspi_csn(),
60..=63 => (self.syscfg.proc_in_sync_bypass_hi.qspi_sd() & (1u32 << (pin - 60))) != 0,
_ => panic!("invalid GPIO pin for sync bypass: {pin}"),
}
}
// ========================================================================
// Debug Port Control
// ========================================================================
/// Attach the debug port to SYSCFG controls
///
/// When attached, the SWD pins are controlled by SYSCFG instead of external pins.
#[inline]
pub fn debug_attach(&self) {
self.syscfg.dbgforce.set_attach();
}
/// Detach the debug port from SYSCFG controls
///
/// Returns control of SWD pins to external connections.
#[inline]
pub fn debug_detach(&self) {
self.syscfg.dbgforce.clr_attach();
}
/// Check if debug port is attached to SYSCFG
#[inline]
pub fn is_debug_attached(&self) -> bool {
self.syscfg.dbgforce.attach()
}
/// Set the SWCLK signal (only effective when attached)
#[inline]
pub fn set_swclk(&self, high: bool) {
if high {
self.syscfg.dbgforce.set_swclk();
} else {
self.syscfg.dbgforce.clr_swclk();
}
}
/// Set the SWDI (SWDIO input) signal (only effective when attached)
#[inline]
pub fn set_swdi(&self, high: bool) {
if high {
self.syscfg.dbgforce.set_swdi();
} else {
self.syscfg.dbgforce.clr_swdi();
}
}
/// Read the SWDO (SWDIO output) signal
#[inline]
pub fn swdo(&self) -> bool {
self.syscfg.dbgforce.swdo()
}
// ========================================================================
// Memory Power Control
// ========================================================================
/// Set the power-down state of a single memory bank.
///
/// When powered down, memories retain their contents but cannot be
/// accessed. Use with caution!
#[inline]
pub fn set_memory_power_down(&self, bank: MemoryBank, power_down: bool) {
if power_down {
self.syscfg.mempowerdown.set_bits(bank.mask());
} else {
self.syscfg.mempowerdown.clr_bits(bank.mask());
}
}
/// Check whether a memory bank is powered down.
#[inline]
pub fn is_memory_powered_down(&self, bank: MemoryBank) -> bool {
(self.syscfg.mempowerdown.read_bits() & bank.mask()) != 0
}
/// Set the power-down state of multiple memory banks at once.
///
/// `mask` is a bitmask over [`MemoryBank`] positions
/// (`1 << bank as u32`).
#[inline]
pub fn set_memory_power_down_mask(&self, mask: u32, power_down: bool) {
if power_down {
self.syscfg.mempowerdown.set_bits(mask);
} else {
self.syscfg.mempowerdown.clr_bits(mask);
}
}
/// Get the raw memory power-down register value.
#[inline]
pub fn memory_power_down_mask(&self) -> u32 {
self.syscfg.mempowerdown.read_bits()
}
// ========================================================================
// Auxiliary Control
// ========================================================================
/// Set the auxiliary control register
///
/// Bit 2: Mask OTP power analogue power supply detection
#[inline]
pub fn set_auxctrl(&self, value: u8) {
self.syscfg.auxctrl.write_auxctrl(value as u32);
}
/// Get the auxiliary control register value
#[inline]
pub fn auxctrl(&self) -> u8 {
self.syscfg.auxctrl.auxctrl() as u8
}
}