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
//! QSPI pad configuration (`PADS_QSPI`) for RP2350
//!
//! The QSPI interface uses dedicated pads (SCLK, CSn, SD0..SD3) whose drive
//! strength, slew rate, Schmitt trigger and pull resistors are configured
//! here — separately from the GPIO bank-0 pads in [`crate::gpio`].
//!
//! ## Example
//!
//! ```ignore
//! use rp2350::gpio::{DriveStrength, SlewRate};
//! use rp2350::pads_qspi::PadsQspi;
//! use rp2350_pac::PADS_QSPI;
//!
//! let pads = PadsQspi::init(unsafe { PADS_QSPI::steal() });
//!
//! // High-speed flash: fast slew, high drive on the clock.
//! pads.sclk().set_drive_strength(DriveStrength::_12mA);
//! pads.sclk().set_slew_rate(SlewRate::Fast);
//! ```
use crate::Steal;
use crate::gpio::{DriveStrength, Pull, SlewRate};
/// QSPI bank supply voltage.
#[derive(Clone, Copy, PartialEq, Eq, Debug)]
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
#[repr(u32)]
pub enum Voltage {
/// 3.3 V supply (DVDD ≥ 2.5 V)
V3_3 = 0,
/// 1.8 V supply (DVDD ≤ 1.8 V)
V1_8 = 1,
}
/// Common interface implemented by every QSPI pad control register.
///
/// The PAC generates a distinct type per pad (`GpioQspiSclk`, `GpioQspiSd0`,
/// ...) with identical accessors; this trait lets [`Pad`] address any of them.
trait QspiPadReg {
fn write_drive(&self, value: u32);
fn set_slewfast(&self);
fn clr_slewfast(&self);
fn set_schmitt(&self);
fn clr_schmitt(&self);
fn set_pue(&self);
fn clr_pue(&self);
fn set_pde(&self);
fn clr_pde(&self);
fn set_ie(&self);
fn clr_ie(&self);
}
macro_rules! impl_qspi_pad_reg {
($($ty:ty),+ $(,)?) => {
$(
impl QspiPadReg for $ty {
#[inline]
fn write_drive(&self, value: u32) {
self.write_drive(value);
}
#[inline]
fn set_slewfast(&self) {
self.set_slewfast();
}
#[inline]
fn clr_slewfast(&self) {
self.clr_slewfast();
}
#[inline]
fn set_schmitt(&self) {
self.set_schmitt();
}
#[inline]
fn clr_schmitt(&self) {
self.clr_schmitt();
}
#[inline]
fn set_pue(&self) {
self.set_pue();
}
#[inline]
fn clr_pue(&self) {
self.clr_pue();
}
#[inline]
fn set_pde(&self) {
self.set_pde();
}
#[inline]
fn clr_pde(&self) {
self.clr_pde();
}
#[inline]
fn set_ie(&self) {
self.set_ie();
}
#[inline]
fn clr_ie(&self) {
self.clr_ie();
}
}
)+
};
}
impl_qspi_pad_reg!(
rp2350_pac::pads_qspi::GpioQspiSclk,
rp2350_pac::pads_qspi::GpioQspiSd0,
rp2350_pac::pads_qspi::GpioQspiSd1,
rp2350_pac::pads_qspi::GpioQspiSd2,
rp2350_pac::pads_qspi::GpioQspiSd3,
rp2350_pac::pads_qspi::GpioQspiSs,
);
/// Handle to a single QSPI pad's control register.
pub struct Pad<'a> {
reg: &'a dyn QspiPadReg,
}
impl Pad<'_> {
/// Set the output drive strength.
#[inline]
pub fn set_drive_strength(&self, strength: DriveStrength) {
self.reg.write_drive(strength as u32);
}
/// Set the output slew rate.
#[inline]
pub fn set_slew_rate(&self, rate: SlewRate) {
match rate {
SlewRate::Slow => self.reg.clr_slewfast(),
SlewRate::Fast => self.reg.set_slewfast(),
}
}
/// Enable or disable the Schmitt trigger on the input.
#[inline]
pub fn set_schmitt(&self, enable: bool) {
if enable {
self.reg.set_schmitt();
} else {
self.reg.clr_schmitt();
}
}
/// Configure the pull resistor.
#[inline]
pub fn set_pull(&self, pull: Pull) {
match pull {
Pull::Up => {
self.reg.set_pue();
self.reg.clr_pde();
}
Pull::Down => {
self.reg.clr_pue();
self.reg.set_pde();
}
Pull::None => {
self.reg.clr_pue();
self.reg.clr_pde();
}
}
}
/// Enable the input receiver.
#[inline]
pub fn enable_input(&self) {
self.reg.set_ie();
}
/// Disable the input receiver.
#[inline]
pub fn disable_input(&self) {
self.reg.clr_ie();
}
}
/// `PADS_QSPI` peripheral wrapper.
///
/// Owns the PAC `PADS_QSPI` peripheral. The QSPI pads are dedicated to the
/// QSPI interface (SCLK, CSn, SD0..SD3) and are configured independently of
/// the GPIO bank-0 pads.
pub struct PadsQspi {
pads_qspi: rp2350_pac::PADS_QSPI,
}
impl PadsQspi {
/// Take ownership of the `PADS_QSPI` peripheral.
///
/// No reset is performed: resetting the flash pads while executing from
/// flash would disrupt XIP access (same policy as [`crate::qspi::Qspi`]).
#[inline]
pub fn init(pads_qspi: rp2350_pac::PADS_QSPI) -> Self {
Self { pads_qspi }
}
/// Obtain a handle without ownership checks.
///
/// # Safety
///
/// The caller must ensure no other handle to `PADS_QSPI` is live.
#[inline]
pub unsafe fn steal() -> Steal<Self> {
Steal(Self {
pads_qspi: unsafe { rp2350_pac::PADS_QSPI::steal() },
})
}
/// Get a reference to the underlying register block.
///
/// # Safety
/// Direct register access can bypass the HAL's safety guarantees.
#[inline]
pub unsafe fn regs(&self) -> &rp2350_pac::pads_qspi::RegisterBlock {
&self.pads_qspi
}
/// Release the `PADS_QSPI` peripheral.
#[inline]
pub fn release(self) -> rp2350_pac::PADS_QSPI {
self.pads_qspi
}
/// Set the QSPI bank supply voltage.
#[inline]
pub fn set_voltage(&self, voltage: Voltage) {
match voltage {
Voltage::V3_3 => self.pads_qspi.voltage_select.clr_voltage_select(),
Voltage::V1_8 => self.pads_qspi.voltage_select.set_voltage_select(),
}
}
/// Get the QSPI bank supply voltage.
#[inline]
pub fn voltage(&self) -> Voltage {
if self.pads_qspi.voltage_select.voltage_select() {
Voltage::V1_8
} else {
Voltage::V3_3
}
}
/// Serial clock pad.
#[inline]
pub fn sclk(&self) -> Pad<'_> {
Pad {
reg: &self.pads_qspi.gpio_qspi_sclk,
}
}
/// Serial data 0 (MOSI in single-SPI mode).
#[inline]
pub fn sd0(&self) -> Pad<'_> {
Pad {
reg: &self.pads_qspi.gpio_qspi_sd0,
}
}
/// Serial data 1 (MISO in single-SPI mode).
#[inline]
pub fn sd1(&self) -> Pad<'_> {
Pad {
reg: &self.pads_qspi.gpio_qspi_sd1,
}
}
/// Serial data 2 (active in quad mode).
#[inline]
pub fn sd2(&self) -> Pad<'_> {
Pad {
reg: &self.pads_qspi.gpio_qspi_sd2,
}
}
/// Serial data 3 (active in quad mode).
#[inline]
pub fn sd3(&self) -> Pad<'_> {
Pad {
reg: &self.pads_qspi.gpio_qspi_sd3,
}
}
/// Chip select.
#[inline]
pub fn csn(&self) -> Pad<'_> {
Pad {
reg: &self.pads_qspi.gpio_qspi_ss,
}
}
}