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
//! EPPB (Extended Private Peripheral Bus) HAL for RP2350
//!
//! This module provides access to the Cortex-M33 EPPB vendor register block
//! for RP2350, including NMI mask control and sleep configuration.
//!
//! ## Features
//!
//! - NMI (Non-Maskable Interrupt) mask configuration for IRQs 0-51
//! - Sleep control (light sleep vs deep sleep)
//! - WIC (Wake-up Interrupt Controller) enable
//!
//! ## Example
//!
//! ```ignore
//! use rp2350::eppb::Eppb;
//! use rp2350::Peripherals;
//!
//! let dp = Peripherals::take().unwrap();
//! let eppb = Eppb::init(dp.EPPB);
//!
//! // Mask IRQ 5 as NMI
//! eppb.set_nmi_mask0(1 << 5);
//!
//! // Enable light sleep mode
//! eppb.set_light_sleep(true);
//! ```
//!
//! ## Note
//!
//! These registers are core-local and are reset by a processor warm reset.
use rp2350_pac::EPPB;
use crate::Steal;
/// EPPB peripheral wrapper
///
/// Provides access to Extended Private Peripheral Bus registers
/// for NMI masking and sleep control.
pub struct Eppb {
eppb: EPPB,
}
impl Eppb {
/// Initialize the Eppb driver.
///
/// # Arguments
/// * `eppb` - The EPPB peripheral from PAC
#[inline]
pub fn init(eppb: EPPB) -> Self {
Self { eppb }
}
/// Obtain an Eppb 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::init(unsafe { EPPB::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::eppb::RegisterBlock {
&self.eppb
}
/// Release the EPPB peripheral
#[inline]
pub fn release(self) -> EPPB {
self.eppb
}
// ========================================================================
// NMI Mask Control
// ========================================================================
/// Get NMI mask for IRQs 0-31
#[inline]
pub fn nmi_mask0(&self) -> u32 {
self.eppb.nmi_mask0.nmi_mask0()
}
/// Set NMI mask for IRQs 0-31
///
/// Each bit corresponds to an IRQ. Setting a bit causes that IRQ
/// to be escalated to an NMI (Non-Maskable Interrupt).
#[inline]
pub fn set_nmi_mask0(&self, mask: u32) {
self.eppb.nmi_mask0.write_nmi_mask0(mask);
}
/// Enable NMI for a specific IRQ (0-51).
///
/// IRQ 0-31 map to `NMI_MASK0`, IRQ 32-51 map to `NMI_MASK1`.
///
/// # Panics
/// Panics if `irq > 51`.
#[inline]
pub fn enable_nmi(&self, irq: u8) {
match irq {
0..=31 => self.eppb.nmi_mask0.modify_bits(|r| r | (1 << irq)),
32..=51 => self.eppb.nmi_mask1.modify_bits(|r| r | (1 << (irq - 32))),
_ => panic!("invalid IRQ for NMI: {irq}"),
}
}
/// Disable NMI for a specific IRQ (0-51).
///
/// # Panics
/// Panics if `irq > 51`.
#[inline]
pub fn disable_nmi(&self, irq: u8) {
match irq {
0..=31 => self.eppb.nmi_mask0.modify_bits(|r| r & !(1 << irq)),
32..=51 => self.eppb.nmi_mask1.modify_bits(|r| r & !(1 << (irq - 32))),
_ => panic!("invalid IRQ for NMI: {irq}"),
}
}
/// Get NMI mask for IRQs 32-51
#[inline]
pub fn nmi_mask1(&self) -> u32 {
self.eppb.nmi_mask1.nmi_mask1()
}
/// Set NMI mask for IRQs 32-51
///
/// Only bits [19:0] are valid (for IRQs 32-51).
#[inline]
pub fn set_nmi_mask1(&self, mask: u32) {
self.eppb.nmi_mask1.write_nmi_mask1(mask & 0x000F_FFFF);
}
// ========================================================================
// Sleep Control
// ========================================================================
/// Get light sleep status
///
/// When true, processor sleep will NOT deassert the system-level
/// clock request (light sleep mode).
#[inline]
pub fn is_light_sleep(&self) -> bool {
self.eppb.sleepctrl.light_sleep()
}
/// Set light sleep mode
///
/// When enabled, any processor sleep will NOT deassert the system-level
/// clock request, keeping clocks running during WFI/WFE.
/// When disabled (default), processor sleep deasserts clock request.
#[inline]
pub fn set_light_sleep(&self, enable: bool) {
if enable {
self.eppb.sleepctrl.set_light_sleep();
} else {
self.eppb.sleepctrl.clr_light_sleep();
}
}
/// Get WIC enable request status
///
/// Returns true if WIC (Wake-up Interrupt Controller) sleep is requested.
#[inline]
pub fn is_wicenreq(&self) -> bool {
self.eppb.sleepctrl.wicenreq()
}
/// Request WIC sleep for next deep sleep
///
/// After setting this, poll `is_wicenack()` until it returns true
/// to confirm WIC mode is ready, then issue WFI/WFE.
#[inline]
pub fn set_wicenreq(&self, enable: bool) {
if enable {
self.eppb.sleepctrl.set_wicenreq();
} else {
self.eppb.sleepctrl.clr_wicenreq();
}
}
/// Get WIC enable acknowledge status
///
/// Returns true when the interrupt controller has acknowledged
/// the WIC enable request. Poll this after setting `set_wicenreq(true)`
/// before entering deep sleep.
#[inline]
pub fn is_wicenack(&self) -> bool {
self.eppb.sleepctrl.wicenack()
}
/// Wait for WIC enable to be acknowledged
///
/// Blocks until WIC mode is ready after requesting WIC sleep.
///
/// # Panics
///
/// Panics if WIC is not acknowledged within
/// [`crate::spin::SPIN_TIMEOUT`] iterations.
#[inline]
pub fn wait_wicenack(&self) {
crate::spin::spin_wait("WIC enable acknowledgement", || self.is_wicenack());
}
/// Prepare for WIC sleep
///
/// Convenience method that requests WIC sleep and waits for acknowledgment.
#[inline]
pub fn prepare_wic_sleep(&self) {
self.set_wicenreq(true);
self.wait_wicenack();
}
}