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
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
//! Per-GPIO Pin Function Description for RP2350
//!
//! This module provides a complete description of each GPIO pin's available
//! peripheral functions, based on the RP2350 datasheet GPIO function select table.
//!
//! # Function Select (funcsel)
//!
//! Each GPIO pin's function is controlled by the `funcsel` field in
//! `IO_BANK0.GPIOx_CTRL`:
//!
//! | funcsel | Function | Notes |
//! |---------|-------------|---------------------------------------------------|
//! | 0 | HSTX | GPIO 12-19 only |
//! | 1 | SPI | Instance and signal depend on pin |
//! | 2 | UART | Instance and signal depend on pin |
//! | 3 | I2C | Instance and signal depend on pin |
//! | 4 | PWM | Slice and channel depend on pin |
//! | 5 | SIO | All pins — direct CPU I/O |
//! | 6 | PIO0 | All pins |
//! | 7 | PIO1 | All pins |
//! | 8 | PIO2 | All pins |
//! | 9 | GPCK | Clock in/out, GPIO 12-15 and 20-25 |
//! | 10 | USB muxing | All pins — VBUS detect/enable or overcurrent |
//! | 11 | UART aux | Alternate UART mapping on selected pins |
//! | 31 | NULL | All pins — disabled / hi-Z |
//!
//! ## funcsel 0 (HSTX)
//!
//! HSTX0-7 (`hstx_0` … `hstx_7`) are only available on GPIO 12-19. On
//! GPIO 0-3 the same funcsel selects JTAG (TCK/TMS/TDI/TDO), which this HAL
//! does not provide.
//!
//! ## funcsel 9 (general-purpose clock)
//!
//! Clock I/O (`clocks_gpin` / `clocks_gpout`) is available on GPIO 12-15 and
//! 20-25:
//!
//! | GPIO | Signal |
//! |---------|-------------|
//! | 12, 20 | Clock in 0 |
//! | 13, 21 | Clock out 0 |
//! | 14, 22 | Clock in 1 |
//! | 15, 23 | Clock out 1 |
//! | 24 | Clock out 2 |
//! | 25 | Clock out 3 |
//!
//! The same funcsel selects XIP CS1 (GPIO 0/8/19/47) or CoreSight trace
//! (GPIO 1-5) on other pins; neither is provided by this HAL.
//!
//! ## funcsel 11 (UART Aux)
//!
//! Pins where `GPIO & 0x2 != 0` expose the UART data signals at funcsel 11
//! instead of funcsel 2 (funcsel 2 keeps the flow-control signals there):
//!
//! | GPIO | funcsel 11 |
//! |-------|---------------------|
//! | 2/3 | UART0 TX / RX |
//! | 6/7 | UART1 TX / RX |
//! | 10/11 | UART1 TX / RX |
//! | 14/15 | UART0 TX / RX |
//! | 18/19 | UART0 TX / RX |
//! | 22/23 | UART1 TX / RX |
//! | 26/27 | UART1 TX / RX |
//! | 30/31 | UART0 TX / RX |
//! | 34/35 | UART0 TX / RX |
//! | 38/39 | UART1 TX / RX |
//! | 42/43 | UART1 TX / RX |
//! | 46/47 | UART0 TX / RX |
//!
//! GPIO 30-47 are only available on the RP2350B.
//!
//! # RP2350A Pin Function Table (GPIO 0–29)
//!
//! | GPIO | F1 (SPI) | F2 (UART) | F3 (I2C) | F4 (PWM) | ADC |
//! |------|-----------|------------|-----------|----------|------|
//! | 0 | SPI0 RX | UART0 TX | I2C0 SDA | PWM0 A | — |
//! | 1 | SPI0 CSn | UART0 RX | I2C0 SCL | PWM0 B | — |
//! | 2 | SPI0 SCK | UART0 CTS | I2C1 SDA | PWM1 A | — |
//! | 3 | SPI0 TX | UART0 RTS | I2C1 SCL | PWM1 B | — |
//! | 4 | SPI0 RX | UART1 TX | I2C0 SDA | PWM2 A | — |
//! | 5 | SPI0 CSn | UART1 RX | I2C0 SCL | PWM2 B | — |
//! | 6 | SPI0 SCK | UART1 CTS | I2C1 SDA | PWM3 A | — |
//! | 7 | SPI0 TX | UART1 RTS | I2C1 SCL | PWM3 B | — |
//! | 8 | SPI1 RX | UART1 TX | I2C0 SDA | PWM4 A | — |
//! | 9 | SPI1 CSn | UART1 RX | I2C0 SCL | PWM4 B | — |
//! | 10 | SPI1 SCK | UART1 CTS | I2C1 SDA | PWM5 A | — |
//! | 11 | SPI1 TX | UART1 RTS | I2C1 SCL | PWM5 B | — |
//! | 12 | SPI1 RX | UART0 TX | I2C0 SDA | PWM6 A | — |
//! | 13 | SPI1 CSn | UART0 RX | I2C0 SCL | PWM6 B | — |
//! | 14 | SPI1 SCK | UART0 CTS | I2C1 SDA | PWM7 A | — |
//! | 15 | SPI1 TX | UART0 RTS | I2C1 SCL | PWM7 B | — |
//! | 16 | SPI0 RX | UART0 TX | I2C0 SDA | PWM0 A | — |
//! | 17 | SPI0 CSn | UART0 RX | I2C0 SCL | PWM0 B | — |
//! | 18 | SPI0 SCK | UART0 CTS | I2C1 SDA | PWM1 A | — |
//! | 19 | SPI0 TX | UART0 RTS | I2C1 SCL | PWM1 B | — |
//! | 20 | SPI0 RX | UART1 TX | I2C0 SDA | PWM2 A | — |
//! | 21 | SPI0 CSn | UART1 RX | I2C0 SCL | PWM2 B | — |
//! | 22 | SPI0 SCK | UART1 CTS | I2C1 SDA | PWM3 A | — |
//! | 23 | SPI0 TX | UART1 RTS | I2C1 SCL | PWM3 B | — |
//! | 24 | SPI1 RX | UART1 TX | I2C0 SDA | PWM4 A | — |
//! | 25 | SPI1 CSn | UART1 RX | I2C0 SCL | PWM4 B | — |
//! | 26 | SPI1 SCK | UART1 CTS | I2C1 SDA | PWM5 A | ADC0¹|
//! | 27 | SPI1 TX | UART1 RTS | I2C1 SCL | PWM5 B | ADC1¹|
//! | 28 | SPI1 RX | UART0 TX | I2C0 SDA | PWM6 A | ADC2¹|
//! | 29 | SPI1 CSn | UART0 RX | I2C0 SCL | PWM6 B | ADC3¹|
//!
//! ¹ ADC on GPIO 26–29 is only available on RP2350A.
//!
//! *All pins also support: SIO (F5), PIO0 (F6), PIO1 (F7), PIO2 (F8).*
//!
//! # RP2350B Additional Pin Function Table (GPIO 30–47)
//!
//! The RP2350B (QFN-80) provides 18 additional GPIO pins (30–47) with the
//! same periodic function mapping pattern. PWM slices 8–11 are only available
//! on RP2350B. These additional pins do **not** have ADC capability.
//!
//! | GPIO | F1 (SPI) | F2 (UART) | F3 (I2C) | F4 (PWM) | ADC |
//! |------|-----------|------------|-----------|-----------| — |
//! | 30 | SPI1 SCK | UART0 CTS | I2C1 SDA | PWM7 A | — |
//! | 31 | SPI1 TX | UART0 RTS | I2C1 SCL | PWM7 B | — |
//! | 32 | SPI0 RX | UART0 TX | I2C0 SDA | PWM8 A | — |
//! | 33 | SPI0 CSn | UART0 RX | I2C0 SCL | PWM8 B | — |
//! | 34 | SPI0 SCK | UART0 CTS | I2C1 SDA | PWM9 A | — |
//! | 35 | SPI0 TX | UART0 RTS | I2C1 SCL | PWM9 B | — |
//! | 36 | SPI0 RX | UART1 TX | I2C0 SDA | PWM10 A | — |
//! | 37 | SPI0 CSn | UART1 RX | I2C0 SCL | PWM10 B | — |
//! | 38 | SPI0 SCK | UART1 CTS | I2C1 SDA | PWM11 A | — |
//! | 39 | SPI0 TX | UART1 RTS | I2C1 SCL | PWM11 B | — |
//! | 40 | SPI1 RX | UART1 TX | I2C0 SDA | PWM8 A | ADC0 |
//! | 41 | SPI1 CSn | UART1 RX | I2C0 SCL | PWM8 B | ADC1 |
//! | 42 | SPI1 SCK | UART1 CTS | I2C1 SDA | PWM9 A | ADC2 |
//! | 43 | SPI1 TX | UART1 RTS | I2C1 SCL | PWM9 B | ADC3 |
//! | 44 | SPI1 RX | UART0 TX | I2C0 SDA | PWM10 A | ADC4 |
//! | 45 | SPI1 CSn | UART0 RX | I2C0 SCL | PWM10 B | ADC5 |
//! | 46 | SPI1 SCK | UART0 CTS | I2C1 SDA | PWM11 A | ADC6 |
//! | 47 | SPI1 TX | UART0 RTS | I2C1 SCL | PWM11 B | ADC7 |
//!
//! *All pins also support: SIO (F5), PIO0 (F6), PIO1 (F7), PIO2 (F8).*
//!
//! # ADC Pin Mapping
//!
//! The RP2350 has a 12-bit SAR ADC with 500 ksps (at 48 MHz clock).
//! The ADC GPIO mapping differs between RP2350A and RP2350B:
//!
//! ## RP2350A (4 channels + temperature sensor)
//!
//! | ADC Channel | GPIO | Notes |
//! |-------------|------|----------------------------------|
//! | 0 | 26 | External analog input |
//! | 1 | 27 | External analog input |
//! | 2 | 28 | External analog input |
//! | 3 | 29 | External analog input |
//! | 4 | — | Internal temperature sensor |
//!
//! ## RP2350B (8 channels + temperature sensor)
//!
//! | ADC Channel | GPIO | Notes |
//! |-------------|------|----------------------------------|
//! | 0 | 40 | External analog input |
//! | 1 | 41 | External analog input |
//! | 2 | 42 | External analog input |
//! | 3 | 43 | External analog input |
//! | 4 | 44 | External analog input |
//! | 5 | 45 | External analog input |
//! | 6 | 46 | External analog input |
//! | 7 | 47 | External analog input |
//! | 8 | — | Internal temperature sensor |
//!
//! To use a pin as ADC input, the pad must be configured with input disabled
//! (`IE=0`) and output disabled (`OD=1`) to avoid digital interference.
//!
//! # Usage
//!
//! ```ignore
//! use rp2350::pin::{PinDesc, SpiFn, UartFn};
//!
//! // Compile-time: compute function for a specific GPIO
//! const GPIO4_SPI: SpiFn = PinDesc::new(4).spi; // SPI0 RX
//! const GPIO4_UART: UartFn = PinDesc::new(4).uart; // UART1 TX
//!
//! // Runtime: look up all functions via the pre-built table
//! let desc = &PIN_TABLE[4];
//! assert_eq!(desc.spi, SpiFn::Spi0Rx);
//! assert_eq!(desc.uart, UartFn::Uart1Tx);
//! ```
// ============================================================================
// SPI Function
// ============================================================================
/// SPI signal assigned to a specific GPIO pin at funcsel = 1.
///
/// The SPI instance (0 or 1) and signal (RX/CSn/SCK/TX) are determined by
/// the GPIO number:
/// - **Instance**: SPI0 for GPIO 0–7, 16–23 (and 32–39 on RP2350B);
/// SPI1 for GPIO 8–15, 24–31 (and 40–47 on RP2350B).
/// - **Signal**: `gpio % 4` → 0=RX, 1=CSn, 2=SCK, 3=TX.
// ============================================================================
// UART Function
// ============================================================================
/// UART signal assigned to a specific GPIO pin at funcsel = 2.
///
/// The UART instance (0 or 1) and signal (TX/RX/CTS/RTS) are determined by
/// the GPIO number:
/// - **Instance**: alternates in groups of 4, with XOR pattern
/// `((gpio/4) & 1) ^ ((gpio/8) & 1)` selecting the instance.
/// - **Signal**: `gpio % 4` → 0=TX, 1=RX, 2=CTS, 3=RTS.
// ============================================================================
// I2C Function
// ============================================================================
/// I2C signal assigned to a specific GPIO pin at funcsel = 3.
///
/// The I2C instance and signal are determined by `gpio % 4`:
/// - 0 → I2C0 SDA, 1 → I2C0 SCL, 2 → I2C1 SDA, 3 → I2C1 SCL.
// ============================================================================
// PWM Channel
// ============================================================================
/// PWM channel (A or B) within a slice.
///
/// - Even GPIO → Channel A
/// - Odd GPIO → Channel B
// ============================================================================
// Pin Description
// ============================================================================
/// Complete function description for a single GPIO pin.
///
/// Describes the pin-specific peripheral mappings. Functions that are available
/// on **all** GPIO pins (SIO, PIO0, PIO1, PIO2, NULL) are not included since
/// they don't vary per pin.
///
/// # ADC
///
/// Only GPIO 26–29 are ADC-capable (channels 0–3). The `adc_channel` field is
/// `None` for all other pins.
// ============================================================================
// Const computation helpers
// ============================================================================
/// Compute the SPI function for a GPIO pin.
///
/// - Instance: `(gpio / 8) % 2` — SPI0 for groups 0,2,4,… ; SPI1 for groups 1,3,5,…
/// - Signal: `gpio % 4` — 0=RX, 1=CSn, 2=SCK, 3=TX
const
/// Compute the UART function for a GPIO pin.
///
/// - Instance: `((gpio/4) & 1) ^ ((gpio/8) & 1)` — XOR of two bit positions
/// selects UART0 (=0) or UART1 (=1).
/// - Signal: `gpio % 4` — 0=TX, 1=RX, 2=CTS, 3=RTS
const
/// Compute the I2C function for a GPIO pin.
///
/// - `gpio % 4`: 0=I2C0 SDA, 1=I2C0 SCL, 2=I2C1 SDA, 3=I2C1 SCL
const
/// Compute the PWM slice number for a GPIO pin.
///
/// - RP2350A (GPIO 0–29): `(gpio / 2) % 8` → slices 0–7
/// - RP2350B (GPIO 30–31): same formula → slice 7
/// - RP2350B (GPIO 32–47): `8 + ((gpio - 32) / 2) % 4` → slices 8–11
const
/// Compute the PWM channel for a GPIO pin.
///
/// Even pins = Channel A, odd pins = Channel B.
const
/// Compute the ADC channel for a GPIO pin, if any.
///
/// - **RP2350A**: GPIO 26–29 → ADC channels 0–3
/// - **RP2350B**: GPIO 40–47 → ADC channels 0–7
const
// ============================================================================
// Pre-built Pin Table
// ============================================================================
/// Number of GPIO pins on RP2350A.
pub const GPIO_COUNT: usize = 30;
/// Number of GPIO pins on RP2350B.
pub const GPIO_COUNT: usize = 48;
/// Pre-computed pin function table for all GPIO pins.
///
/// Indexed by GPIO number: `PIN_TABLE[gpio_num]`.
///
/// ```ignore
/// use rp2350::pin::PIN_TABLE;
///
/// let gpio4 = &PIN_TABLE[4];
/// // SPI0 RX, UART1 TX, I2C0 SDA, PWM slice 2 channel A
/// ```
pub const PIN_TABLE: = ;
pub const PIN_TABLE: = ;
// ============================================================================
// Pin lookup helpers
// ============================================================================
/// Get the function description for a GPIO pin at runtime.
///
/// Returns `None` if `gpio` is out of range for the current chip variant.
pub const
/// Find all GPIO pins that can serve as UART TX for a given instance.
///
/// Returns a static slice of GPIO pin numbers.
///
/// ```ignore
/// let uart0_tx_pins = uart_tx_pins(0); // &[0, 12, 16, 28]
/// let uart1_tx_pins = uart_tx_pins(1); // &[4, 8, 20, 24]
/// ```
pub const
/// Find all GPIO pins that can serve as UART RX for a given instance.
pub const
/// Find all GPIO pins that can serve as I2C SDA for a given instance.
pub const
/// Find all GPIO pins that can serve as I2C SCL for a given instance.
pub const
/// Find all GPIO pins that map to a specific PWM slice.
///
/// Returns a static slice of `(gpio, PwmCh)` pairs.
///
/// ```ignore
/// let slice0_pins = pwm_slice_pins(0); // &[(0, A), (1, B), (16, A), (17, B)]
/// ```
pub const
/// Find all ADC-capable GPIO pins.
///
/// Returns `(gpio, adc_channel)` pairs.
///
/// - RP2350A: GPIO 26–29 (4 channels)
/// - RP2350B: GPIO 40–47 (8 channels)
pub const
/// Find all ADC-capable GPIO pins.
///
/// Returns `(gpio, adc_channel)` pairs.
///
/// - RP2350A: GPIO 26–29 (4 channels)
/// - RP2350B: GPIO 40–47 (8 channels)
pub const