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
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
//! GPIO Coprocessor (GPIOC) for RP2350 Cortex-M33
//!
//! The GPIO coprocessor is a Cortex-M33 custom coprocessor (CP0) that provides
//! single-cycle GPIO access via dedicated coprocessor instructions. It replaces
//! SIO memory-mapped register access for GPIO output, output-enable, and input
//! operations.
//!
//! # Advantages over SIO
//!
//! | Aspect | SIO (0xd0000000) | GPIOC (MCR/MRC) |
//! |-------------------|-----------------------|------------------------|
//! | Access latency | Multi-cycle bus | Single-cycle |
//! | Atomic 64-bit | Two 32-bit operations | One `MCRR` instruction |
//! | Interrupt safety | TOGL alias needed | Inherently atomic |
//!
//! # Architecture
//!
//! GPIOC uses the Cortex-M33 coprocessor interface with CP0 (`p0`):
//!
//! ```text
//! Instruction format:
//! MCR p0, #opc, Rd, c0, cRm — 32-bit write
//! MRC p0, #opc, Rd, c0, cRm — 32-bit read
//! MCRR p0, #opc, Rd, Rn, cRm — 64-bit write
//! MRRC p0, #opc, Rd, Rn, cRm — 64-bit read
//!
//! Register groups (cRm):
//! c0 — OUT low 32 bits (GPIO 0–31)
//! c1 — OUT high 32 bits (GPIO 32–63)
//! c4 — OE low 32 bits
//! c5 — OE high 32 bits
//! c8 — IN low 32 bits
//! c9 — IN high 32 bits
//!
//! Operations (#opc):
//! #0 — put (write value)
//! #1 — xor (toggle mask)
//! #2 — set (set mask bits to 1)
//! #3 — clr (clear mask bits to 0)
//! #4 — bit_put (single bit write)
//! #5 — bit_xor (single bit toggle)
//! #6 — bit_set (single bit set)
//! #7 — bit_clr (single bit clear)
//! #8 — index_put (dynamic register index)
//! #9 — index_xor
//! #10 — index_set
//! #11 — index_clr
//! ```
//!
//! # Usage
//!
//! ```ignore
//! use rp2350::gpioc;
//!
//! // Output — pin-level (single-cycle coprocessor)
//! unsafe { gpioc::set_high(25) }; // drive GPIO 25 high
//! unsafe { gpioc::set_low(25) }; // drive GPIO 25 low
//! unsafe { gpioc::toggle(25) }; // toggle GPIO 25
//!
//! // Direction
//! unsafe { gpioc::enable_output(25) }; // set GPIO 25 to output
//! unsafe { gpioc::disable_output(25) }; // set GPIO 25 to input
//!
//! // Read
//! let input = unsafe { gpioc::get(25) };
//!
//! // Mask-level (batch control)
//! unsafe { gpioc::mask_set_low(1 << 25) };
//! let pins = unsafe { gpioc::mask_input_low() };
//! ```
//!
//! # Safety
//!
//! All public functions in this module are `unsafe` because the caller must
//! ensure that the GPIO subsystem has been initialized and pins are configured
//! for SIO — matching the contract of the equivalent functions in
//! [`gpio`](crate::gpio).
//!
//! The low-level coprocessor instructions themselves are inherently atomic and
//! concurrency-safe, but they are private implementation details in
//! [`imp`](self::imp).
//!
//! # Availability
//!
//! Available on all RP2350 variants (Cortex-M33). The GPIO coprocessor is an
//! RP2350-specific feature and is not present on RP2040 (Cortex-M0+).
// ============================================================================
// Unified high-level API — same names as gpio
// ============================================================================
// ============================================================================
//
// These functions mirror the [`gpio`](crate::gpio) module API but
// use the GPIOC coprocessor instead of SIO MMIO registers. Users can choose
// between the two modules for identical operations:
//
// ```ignore
// use rp2350::gpio;
// use rp2350::gpioc;
//
// // SIO path (MMIO):
// unsafe { gpio::set_high(25) };
//
// // GPIOC path (coprocessor, single-cycle):
// unsafe { gpioc::set_high(25) };
// ```
//
// # Safety
//
// All functions are `unsafe` because the caller must ensure:
// - The GPIO subsystem was initialized via [`gpio::Pins::init()`](crate::gpio::Pins::init).
// - Each pin has been configured for SIO function via [`Pin<N>`](crate::gpio::Pin).
// - No concurrent configuration changes happen while data operations are in flight.
/// Read the input value of a single pin.
///
/// Returns `true` if the pin is high.
///
/// # Safety
///
/// See the [module-level safety contract](crate::gpioc#safety).
pub unsafe
/// Set a single pin output high.
///
/// The pin must already be configured for SIO output.
///
/// # Safety
///
/// See the [module-level safety contract](crate::gpioc#safety).
pub unsafe
/// Set a single pin output low.
///
/// The pin must already be configured for SIO output.
///
/// # Safety
///
/// See the [module-level safety contract](crate::gpioc#safety).
pub unsafe
/// Toggle a single pin output.
///
/// The pin must already be configured for SIO output.
///
/// # Safety
///
/// See the [module-level safety contract](crate::gpioc#safety).
pub unsafe
/// Set a single pin output to a specific value.
///
/// # Safety
///
/// See the [module-level safety contract](crate::gpioc#safety).
pub unsafe
/// Read the programmed output level of a single pin.
///
/// This reads the output register, not the actual pad input state. Use
/// [`get`] if you need the live pin level.
///
/// # Safety
///
/// See the [module-level safety contract](crate::gpioc#safety).
pub unsafe
/// Enable output for a single pin.
///
/// Unconditionally sets the OE bit to 1 (output). See [`set_output_enable`] for a
/// conditional version that accepts a `bool`.
///
/// # Safety
///
/// See the [module-level safety contract](crate::gpioc#safety).
pub unsafe
/// Disable output for a single pin.
///
/// Unconditionally clears the OE bit to 0 (input). See [`set_output_enable`] for a
/// conditional version that accepts a `bool`.
///
/// # Safety
///
/// See the [module-level safety contract](crate::gpioc#safety).
pub unsafe
/// Write the OE bit to a specific value.
///
/// Sets to output if `output` is `true`, input if `false`.
/// More efficient than a separate `if` + [`enable_output`]/[`disable_output`]
/// because GPIOC uses a single `MCRR` instruction.
///
/// # Safety
///
/// See the [module-level safety contract](crate::gpioc#safety).
pub unsafe
/// Toggle the OE bit for a single pin.
///
/// If the pin was output it becomes input, and vice versa.
///
/// # Safety
///
/// See the [module-level safety contract](crate::gpioc#safety).
pub unsafe
/// Check whether output is enabled for a single pin.
///
/// # Safety
///
/// See the [module-level safety contract](crate::gpioc#safety).
pub unsafe
// Mask-level operations — low bank -------------------------------------------
/// Read the live input state of GPIO 0-31 as a bitmask.
///
/// # Safety
///
/// See the [module-level safety contract](crate::gpioc#safety).
pub unsafe
/// Read the programmed output register for GPIO 0-31.
///
/// # Safety
///
/// See the [module-level safety contract](crate::gpioc#safety).
pub unsafe
/// Replace the entire output register for GPIO 0-31.
///
/// # Safety
///
/// See the [module-level safety contract](crate::gpioc#safety).
pub unsafe
/// Atomically drive selected GPIO 0-31 outputs high.
///
/// # Safety
///
/// See the [module-level safety contract](crate::gpioc#safety).
pub unsafe
/// Atomically drive selected GPIO 0-31 outputs low.
///
/// # Safety
///
/// See the [module-level safety contract](crate::gpioc#safety).
pub unsafe
/// Atomically toggle selected GPIO 0-31 outputs.
///
/// # Safety
///
/// See the [module-level safety contract](crate::gpioc#safety).
pub unsafe
/// Read the output-enable register for GPIO 0-31.
///
/// # Safety
///
/// See the [module-level safety contract](crate::gpioc#safety).
pub unsafe
/// Replace the entire output-enable register for GPIO 0-31.
///
/// # Safety
///
/// See the [module-level safety contract](crate::gpioc#safety).
pub unsafe
/// Atomically enable output for selected GPIO 0-31 pins.
///
/// # Safety
///
/// See the [module-level safety contract](crate::gpioc#safety).
pub unsafe
/// Atomically disable output for selected GPIO 0-31 pins.
///
/// # Safety
///
/// See the [module-level safety contract](crate::gpioc#safety).
pub unsafe
/// Atomically toggle output mode for selected GPIO 0-31 pins.
///
/// # Safety
///
/// See the [module-level safety contract](crate::gpioc#safety).
pub unsafe
// Mask-level operations — high bank ------------------------------------------
/// Read the live input state of GPIO 32-63 as a compact bitmask.
///
/// # Safety
///
/// See the [module-level safety contract](crate::gpioc#safety).
pub unsafe
/// Read the programmed output register for GPIO 32-63.
///
/// # Safety
///
/// See the [module-level safety contract](crate::gpioc#safety).
pub unsafe
/// Replace the entire output register for GPIO 32-63.
///
/// # Safety
///
/// See the [module-level safety contract](crate::gpioc#safety).
pub unsafe
/// Atomically drive selected GPIO 32-63 outputs high.
///
/// # Safety
///
/// See the [module-level safety contract](crate::gpioc#safety).
pub unsafe
/// Atomically drive selected GPIO 32-63 outputs low.
///
/// # Safety
///
/// See the [module-level safety contract](crate::gpioc#safety).
pub unsafe
/// Atomically toggle selected GPIO 32-63 outputs.
///
/// # Safety
///
/// See the [module-level safety contract](crate::gpioc#safety).
pub unsafe
/// Read the output-enable register for GPIO 32-63.
///
/// # Safety
///
/// See the [module-level safety contract](crate::gpioc#safety).
pub unsafe
/// Replace the entire output-enable register for GPIO 32-63.
///
/// # Safety
///
/// See the [module-level safety contract](crate::gpioc#safety).
pub unsafe
/// Atomically enable output for selected GPIO 32-63 pins.
///
/// # Safety
///
/// See the [module-level safety contract](crate::gpioc#safety).
pub unsafe
/// Atomically disable output for selected GPIO 32-63 pins.
///
/// # Safety
///
/// See the [module-level safety contract](crate::gpioc#safety).
pub unsafe
/// Atomically toggle output mode for selected GPIO 32-63 pins.
///
/// # Safety
///
/// See the [module-level safety contract](crate::gpioc#safety).
pub unsafe
// ============================================================================
// 64-bit atomic operations — single-cycle read/write of both low + high banks
// ============================================================================
//
// On GPIOC these are a single `MCRR`/`MRRC` instruction. On SIO they are two
// sequential 32-bit writes — still convenient for API symmetry, but not atomic
// across the 64-bit boundary on the SIO path.
/// Write all 64 output bits in a single GPIOC instruction.
///
/// Bits 0-31 → GPIO 0-31, bits 32-63 → GPIO 32-63.
///
/// # Safety
/// See the [module-level safety contract](crate::gpioc#safety).
pub unsafe
/// Toggle selected output bits across all 64 GPIOs.
///
/// # Safety
/// See the [module-level safety contract](crate::gpioc#safety).
pub unsafe
/// Set selected output bits high across all 64 GPIOs.
///
/// # Safety
/// See the [module-level safety contract](crate::gpioc#safety).
pub unsafe
/// Clear selected output bits low across all 64 GPIOs.
///
/// # Safety
/// See the [module-level safety contract](crate::gpioc#safety).
pub unsafe
/// Read all 64 output bits in a single GPIOC instruction.
///
/// # Safety
/// See the [module-level safety contract](crate::gpioc#safety).
pub unsafe
/// Write all 64 output-enable bits in a single GPIOC instruction.
///
/// # Safety
/// See the [module-level safety contract](crate::gpioc#safety).
pub unsafe
/// Toggle selected output-enable bits across all 64 GPIOs.
///
/// # Safety
/// See the [module-level safety contract](crate::gpioc#safety).
pub unsafe
/// Set selected output-enable bits (to output) across all 64 GPIOs.
///
/// # Safety
/// See the [module-level safety contract](crate::gpioc#safety).
pub unsafe
/// Clear selected output-enable bits (to input) across all 64 GPIOs.
///
/// # Safety
/// See the [module-level safety contract](crate::gpioc#safety).
pub unsafe
/// Read all 64 output-enable bits in a single GPIOC instruction.
///
/// # Safety
/// See the [module-level safety contract](crate::gpioc#safety).
pub unsafe
// ============================================================================
// Combined-input & single-bank optimizations
// ============================================================================
/// Read all 64 input pins in a single GPIOC instruction.
///
/// # Safety
/// See the [module-level safety contract](crate::gpioc#safety).
pub unsafe