hisi-hal 0.7.0-alpha.3

Hardware Abstraction Layer for HiSilicon WS63 (RISC-V RV32IMFC_Zicsr)
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
//! IO Configuration (Pin Mux and Pad Control) driver for WS63.
//!
//! The WS63 IO_CONFIG peripheral controls pin function selection (muxing)
//! and pad characteristics (drive strength, pull resistors, Schmitt trigger,
//! input enable) for all GPIO, UART, and SFC pads.
//!
//! # Pin mux
//!
//! Each GPIO pin can be assigned one of several functions via a 3-bit
//! mux select register. The exact function mapping depends on the chip
//! configuration.
//!
//! # Pad control
//!
//! Each pad has independent control over:
//! - Drive strength (3-bit, 000 = strongest, 111 = weakest)
//! - Pull resistor (none, pull-up, pull-down)
//! - Schmitt trigger (enable/disable)
//! - Input enable (enable/disable)

use crate::peripherals::IoConfig;

/// Pad drive strength.
///
/// DS[2:0] values: smaller = stronger drive.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum DriveStrength {
    /// Strongest drive.
    Strongest = 0,
    /// Medium-strong drive.
    Strong = 1,
    /// Medium drive.
    Medium = 2,
    /// Medium-weak drive.
    Weak = 3,
    /// Weaker drive.
    Weaker = 4,
    /// Very weak drive.
    VeryWeak = 5,
    /// Weakest drive.
    Weakest = 6,
    /// Minimum drive.
    Minimum = 7,
}

/// Pull resistor configuration.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum PullResistor {
    /// No pull.
    None,
    /// Pull-up resistor.
    Up,
    /// Pull-down resistor.
    Down,
}

/// GPIO pad selection for IO_CONFIG-controlled GPIO pads (0-14).
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum GpioPad {
    /// GPIO pin 0.
    Gpio00,
    /// GPIO pin 1.
    Gpio01,
    /// GPIO pin 2.
    Gpio02,
    /// GPIO pin 3.
    Gpio03,
    /// GPIO pin 4.
    Gpio04,
    /// GPIO pin 5.
    Gpio05,
    /// GPIO pin 6.
    Gpio06,
    /// GPIO pin 7.
    Gpio07,
    /// GPIO pin 8.
    Gpio08,
    /// GPIO pin 9.
    Gpio09,
    /// GPIO pin 10.
    Gpio10,
    /// GPIO pin 11.
    Gpio11,
    /// GPIO pin 12.
    Gpio12,
    /// GPIO pin 13.
    Gpio13,
    /// GPIO pin 14.
    Gpio14,
}

impl GpioPad {
    /// Build a GPIO pad from a raw pad index, rejecting pads that IO_CONFIG does not expose.
    pub const fn from_index(index: u8) -> Option<Self> {
        match index {
            0 => Some(Self::Gpio00),
            1 => Some(Self::Gpio01),
            2 => Some(Self::Gpio02),
            3 => Some(Self::Gpio03),
            4 => Some(Self::Gpio04),
            5 => Some(Self::Gpio05),
            6 => Some(Self::Gpio06),
            7 => Some(Self::Gpio07),
            8 => Some(Self::Gpio08),
            9 => Some(Self::Gpio09),
            10 => Some(Self::Gpio10),
            11 => Some(Self::Gpio11),
            12 => Some(Self::Gpio12),
            13 => Some(Self::Gpio13),
            14 => Some(Self::Gpio14),
            _ => None,
        }
    }

    /// The GPIO pad index (0-14).
    pub const fn index(self) -> u8 {
        match self {
            Self::Gpio00 => 0,
            Self::Gpio01 => 1,
            Self::Gpio02 => 2,
            Self::Gpio03 => 3,
            Self::Gpio04 => 4,
            Self::Gpio05 => 5,
            Self::Gpio06 => 6,
            Self::Gpio07 => 7,
            Self::Gpio08 => 8,
            Self::Gpio09 => 9,
            Self::Gpio10 => 10,
            Self::Gpio11 => 11,
            Self::Gpio12 => 12,
            Self::Gpio13 => 13,
            Self::Gpio14 => 14,
        }
    }
}

/// UART pad selection for IO_CONFIG-controlled UART pads.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum UartPad {
    /// UART0 transmit data pad.
    Uart0Txd,
    /// UART0 receive data pad.
    Uart0Rxd,
    /// UART1 transmit data pad.
    Uart1Txd,
    /// UART1 receive data pad.
    Uart1Rxd,
}

/// A 3-bit IO_CONFIG mux-function selector.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct MuxFunction(u8);

impl MuxFunction {
    /// Function select value 0.
    pub const F0: Self = Self(0);
    /// Function select value 1.
    pub const F1: Self = Self(1);
    /// Function select value 2.
    pub const F2: Self = Self(2);
    /// Function select value 3.
    pub const F3: Self = Self(3);
    /// Function select value 4.
    pub const F4: Self = Self(4);
    /// Function select value 5.
    pub const F5: Self = Self(5);
    /// Function select value 6.
    pub const F6: Self = Self(6);
    /// Function select value 7.
    pub const F7: Self = Self(7);

    /// Build a mux selector from raw bits, rejecting values outside the 3-bit field.
    pub const fn from_bits(bits: u8) -> Option<Self> {
        if bits <= 0x07 { Some(Self(bits)) } else { None }
    }

    /// Raw 3-bit function select value.
    pub const fn bits(self) -> u8 {
        self.0
    }
}

/// SFC pad selection.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[instability::unstable]
pub enum SfcPad {
    /// SFC clock pad.
    Clk,
    /// SFC chip-select (active low) pad.
    Csn,
    /// SFC data line 0 pad.
    Io0,
    /// SFC data line 1 pad.
    Io1,
    /// SFC data line 2 pad.
    Io2,
    /// SFC data line 3 pad.
    Io3,
}

/// IO Configuration driver.
pub struct IoConfigDriver<'d> {
    _io_config: IoConfig<'d>,
}

impl<'d> IoConfigDriver<'d> {
    /// Create a new IO configuration driver.
    pub fn new(io_config: IoConfig<'d>) -> Self {
        Self { _io_config: io_config }
    }

    fn regs(&self) -> &'static crate::soc::pac::io_config::RegisterBlock {
        // SAFETY: PAC peripheral pointer is a static physical MMIO address, always valid
        unsafe { &*IoConfig::ptr() }
    }

    // ── Pin mux ───────────────────────────────────────────────────

    /// Set the function select value for a GPIO pad.
    ///
    /// * `pin` — GPIO pad index (0-14), represented as [`GpioPad`].
    /// * `function` — validated 3-bit function select value.
    pub fn set_gpio_mux(&mut self, pin: GpioPad, function: MuxFunction) {
        let val = function.bits() as u32;
        let r = self.regs();
        match pin {
            GpioPad::Gpio00 => unsafe {
                r.gpio_00_sel().write(|w| w.bits(val));
            },
            GpioPad::Gpio01 => unsafe {
                r.gpio_01_sel().write(|w| w.bits(val));
            },
            GpioPad::Gpio02 => unsafe {
                r.gpio_02_sel().write(|w| w.bits(val));
            },
            GpioPad::Gpio03 => unsafe {
                r.gpio_03_sel().write(|w| w.bits(val));
            },
            GpioPad::Gpio04 => unsafe {
                r.gpio_04_sel().write(|w| w.bits(val));
            },
            GpioPad::Gpio05 => unsafe {
                r.gpio_05_sel().write(|w| w.bits(val));
            },
            GpioPad::Gpio06 => unsafe {
                r.gpio_06_sel().write(|w| w.bits(val));
            },
            GpioPad::Gpio07 => unsafe {
                r.gpio_07_sel().write(|w| w.bits(val));
            },
            GpioPad::Gpio08 => unsafe {
                r.gpio_08_sel().write(|w| w.bits(val));
            },
            GpioPad::Gpio09 => unsafe {
                r.gpio_09_sel().write(|w| w.bits(val));
            },
            GpioPad::Gpio10 => unsafe {
                r.gpio_10_sel().write(|w| w.bits(val));
            },
            GpioPad::Gpio11 => unsafe {
                r.gpio_11_sel().write(|w| w.bits(val));
            },
            GpioPad::Gpio12 => unsafe {
                r.gpio_12_sel().write(|w| w.bits(val));
            },
            GpioPad::Gpio13 => unsafe {
                r.gpio_13_sel().write(|w| w.bits(val));
            },
            GpioPad::Gpio14 => unsafe {
                r.gpio_14_sel().write(|w| w.bits(val));
            },
        }
    }

    /// Get the function select value for a GPIO pad.
    pub fn gpio_mux(&self, pin: GpioPad) -> MuxFunction {
        let r = self.regs();
        let bits = match pin {
            GpioPad::Gpio00 => r.gpio_00_sel().read().bits(),
            GpioPad::Gpio01 => r.gpio_01_sel().read().bits(),
            GpioPad::Gpio02 => r.gpio_02_sel().read().bits(),
            GpioPad::Gpio03 => r.gpio_03_sel().read().bits(),
            GpioPad::Gpio04 => r.gpio_04_sel().read().bits(),
            GpioPad::Gpio05 => r.gpio_05_sel().read().bits(),
            GpioPad::Gpio06 => r.gpio_06_sel().read().bits(),
            GpioPad::Gpio07 => r.gpio_07_sel().read().bits(),
            GpioPad::Gpio08 => r.gpio_08_sel().read().bits(),
            GpioPad::Gpio09 => r.gpio_09_sel().read().bits(),
            GpioPad::Gpio10 => r.gpio_10_sel().read().bits(),
            GpioPad::Gpio11 => r.gpio_11_sel().read().bits(),
            GpioPad::Gpio12 => r.gpio_12_sel().read().bits(),
            GpioPad::Gpio13 => r.gpio_13_sel().read().bits(),
            GpioPad::Gpio14 => r.gpio_14_sel().read().bits(),
        };
        MuxFunction((bits & 0x07) as u8)
    }

    /// Set the function select for a UART pad.
    pub fn set_uart_mux(&mut self, pin: UartPad, function: MuxFunction) {
        let val = function.bits() as u32;
        let r = self.regs();
        match pin {
            UartPad::Uart0Txd => unsafe {
                r.uart0_txd_sel().write(|w| w.bits(val));
            },
            UartPad::Uart0Rxd => unsafe {
                r.uart0_rxd_sel().write(|w| w.bits(val));
            },
            UartPad::Uart1Txd => unsafe {
                r.uart1_txd_sel().write(|w| w.bits(val));
            },
            UartPad::Uart1Rxd => unsafe {
                r.uart1_rxd_sel().write(|w| w.bits(val));
            },
        }
    }

    // ── Pad control ────────────────────────────────────────────────

    /// Configure pad characteristics for a GPIO pad.
    ///
    /// * `pin` — GPIO pad index (0-14).
    pub fn configure_gpio_pad(
        &mut self,
        pin: GpioPad,
        drive: DriveStrength,
        pull: PullResistor,
        schmitt_trigger: bool,
        input_enable: bool,
    ) {
        let val = build_pad_ctrl(drive, pull, schmitt_trigger, input_enable);
        let r = self.regs();
        match pin {
            GpioPad::Gpio00 => unsafe {
                r.pad_gpio_00_ctrl().write(|w| w.bits(val));
            },
            GpioPad::Gpio01 => unsafe {
                r.pad_gpio_01_ctrl().write(|w| w.bits(val));
            },
            GpioPad::Gpio02 => unsafe {
                r.pad_gpio_02_ctrl().write(|w| w.bits(val));
            },
            GpioPad::Gpio03 => unsafe {
                r.pad_gpio_03_ctrl().write(|w| w.bits(val));
            },
            GpioPad::Gpio04 => unsafe {
                r.pad_gpio_04_ctrl().write(|w| w.bits(val));
            },
            GpioPad::Gpio05 => unsafe {
                r.pad_gpio_05_ctrl().write(|w| w.bits(val));
            },
            GpioPad::Gpio06 => unsafe {
                r.pad_gpio_06_ctrl().write(|w| w.bits(val));
            },
            GpioPad::Gpio07 => unsafe {
                r.pad_gpio_07_ctrl().write(|w| w.bits(val));
            },
            GpioPad::Gpio08 => unsafe {
                r.pad_gpio_08_ctrl().write(|w| w.bits(val));
            },
            GpioPad::Gpio09 => unsafe {
                r.pad_gpio_09_ctrl().write(|w| w.bits(val));
            },
            GpioPad::Gpio10 => unsafe {
                r.pad_gpio_10_ctrl().write(|w| w.bits(val));
            },
            GpioPad::Gpio11 => unsafe {
                r.pad_gpio_11_ctrl().write(|w| w.bits(val));
            },
            GpioPad::Gpio12 => unsafe {
                r.pad_gpio_12_ctrl().write(|w| w.bits(val));
            },
            GpioPad::Gpio13 => unsafe {
                r.pad_gpio_13_ctrl().write(|w| w.bits(val));
            },
            GpioPad::Gpio14 => unsafe {
                r.pad_gpio_14_ctrl().write(|w| w.bits(val));
            },
        }
    }

    /// Configure pad characteristics for a UART pad.
    pub fn configure_uart_pad(
        &mut self,
        uart_pad: UartPad,
        drive: DriveStrength,
        pull: PullResistor,
        schmitt_trigger: bool,
        input_enable: bool,
    ) {
        let val = build_pad_ctrl(drive, pull, schmitt_trigger, input_enable);
        let r = self.regs();
        match uart_pad {
            UartPad::Uart0Txd => unsafe {
                r.pad_uart0_txd_ctrl().write(|w| w.bits(val));
            },
            UartPad::Uart0Rxd => unsafe {
                r.pad_uart0_rxd_ctrl().write(|w| w.bits(val));
            },
            UartPad::Uart1Txd => unsafe {
                r.pad_uart1_txd_ctrl().write(|w| w.bits(val));
            },
            UartPad::Uart1Rxd => unsafe {
                r.pad_uart1_rxd_ctrl().write(|w| w.bits(val));
            },
        }
    }

    /// Configure pad characteristics for an SFC pad.
    #[instability::unstable]
    pub fn configure_sfc_pad(
        &mut self,
        sfc_pad: SfcPad,
        drive: DriveStrength,
        pull: PullResistor,
        schmitt_trigger: bool,
        input_enable: bool,
    ) {
        let val = build_pad_ctrl(drive, pull, schmitt_trigger, input_enable);
        let r = self.regs();
        match sfc_pad {
            SfcPad::Clk => unsafe {
                r.pad_sfc_clk_ctrl().write(|w| w.bits(val));
            },
            SfcPad::Csn => unsafe {
                r.pad_sfc_csn_ctrl().write(|w| w.bits(val));
            },
            SfcPad::Io0 => unsafe {
                r.pad_sfc_io0_ctrl().write(|w| w.bits(val));
            },
            SfcPad::Io1 => unsafe {
                r.pad_sfc_io1_ctrl().write(|w| w.bits(val));
            },
            SfcPad::Io2 => unsafe {
                r.pad_sfc_io2_ctrl().write(|w| w.bits(val));
            },
            SfcPad::Io3 => unsafe {
                r.pad_sfc_io3_ctrl().write(|w| w.bits(val));
            },
        }
    }

    /// Read the current pad control value for a GPIO pad.
    pub fn read_gpio_pad(&self, pin: GpioPad) -> u32 {
        let r = self.regs();
        match pin {
            GpioPad::Gpio00 => r.pad_gpio_00_ctrl().read().bits(),
            GpioPad::Gpio01 => r.pad_gpio_01_ctrl().read().bits(),
            GpioPad::Gpio02 => r.pad_gpio_02_ctrl().read().bits(),
            GpioPad::Gpio03 => r.pad_gpio_03_ctrl().read().bits(),
            GpioPad::Gpio04 => r.pad_gpio_04_ctrl().read().bits(),
            GpioPad::Gpio05 => r.pad_gpio_05_ctrl().read().bits(),
            GpioPad::Gpio06 => r.pad_gpio_06_ctrl().read().bits(),
            GpioPad::Gpio07 => r.pad_gpio_07_ctrl().read().bits(),
            GpioPad::Gpio08 => r.pad_gpio_08_ctrl().read().bits(),
            GpioPad::Gpio09 => r.pad_gpio_09_ctrl().read().bits(),
            GpioPad::Gpio10 => r.pad_gpio_10_ctrl().read().bits(),
            GpioPad::Gpio11 => r.pad_gpio_11_ctrl().read().bits(),
            GpioPad::Gpio12 => r.pad_gpio_12_ctrl().read().bits(),
            GpioPad::Gpio13 => r.pad_gpio_13_ctrl().read().bits(),
            GpioPad::Gpio14 => r.pad_gpio_14_ctrl().read().bits(),
        }
    }
}

/// Build a pad control register value.
fn build_pad_ctrl(drive: DriveStrength, pull: PullResistor, schmitt_trigger: bool, input_enable: bool) -> u32 {
    let mut val: u32 = 0;

    // Drive strength: ds2=bit6, ds1=bit5, ds0=bit4
    let ds = drive as u32;
    val |= ((ds & 0x04) << 4) | ((ds & 0x02) << 4) | ((ds & 0x01) << 4); // ds2:6, ds1:5, ds0:4

    // Pull resistor: PE=bit9, PS=bit10
    match pull {
        PullResistor::None => {} // PE=0, PS=0
        PullResistor::Up => {
            val |= (1 << 9) | (1 << 10); // PE=1, PS=1
        }
        PullResistor::Down => {
            val |= 1 << 9; // PE=1, PS=0
        }
    }

    // Schmitt trigger: ST=bit3
    if schmitt_trigger {
        val |= 1 << 3;
    }

    // Input enable: IE=bit11
    if input_enable {
        val |= 1 << 11;
    }

    val
}

// ── Tests ──────────────────────────────────────────────────────

#[cfg(all(test, not(target_arch = "riscv32")))]
mod tests {
    use super::*;

    // Bit positions in the pad-control register (mirrors build_pad_ctrl).
    const ST_BIT: u32 = 1 << 3; // Schmitt trigger
    const DS_MASK: u32 = 0x7 << 4; // drive strength ds[2:0] -> bits [6:4]
    const PE_BIT: u32 = 1 << 9; // pull enable
    const PS_BIT: u32 = 1 << 10; // pull select (1 = up, 0 = down)
    const IE_BIT: u32 = 1 << 11; // input enable

    #[test]
    fn drive_strength_discriminants_are_dense() {
        // The enum encodes DS[2:0] directly; values must be 0..=7 contiguous.
        assert_eq!(DriveStrength::Strongest as u32, 0);
        assert_eq!(DriveStrength::Strong as u32, 1);
        assert_eq!(DriveStrength::Medium as u32, 2);
        assert_eq!(DriveStrength::Weak as u32, 3);
        assert_eq!(DriveStrength::Weaker as u32, 4);
        assert_eq!(DriveStrength::VeryWeak as u32, 5);
        assert_eq!(DriveStrength::Weakest as u32, 6);
        assert_eq!(DriveStrength::Minimum as u32, 7);
    }

    #[test]
    fn drive_strength_lands_in_bits_4_to_6() {
        // ds[2:0] must be shifted intact into register bits [6:4] (val == ds << 4).
        for (ds, expect) in [
            (DriveStrength::Strongest, 0u32),
            (DriveStrength::Strong, 1 << 4),
            (DriveStrength::Medium, 2 << 4),
            (DriveStrength::Minimum, 7 << 4),
        ] {
            let val = build_pad_ctrl(ds, PullResistor::None, false, false);
            assert_eq!(val & DS_MASK, expect, "ds {ds:?} mis-placed");
            // Drive strength must not bleed into any other field.
            assert_eq!(val & !DS_MASK, 0);
        }
    }

    #[test]
    fn drive_strength_covers_full_mask() {
        // The strongest..minimum span must exactly fill the 3-bit DS field.
        let strongest = build_pad_ctrl(DriveStrength::Strongest, PullResistor::None, false, false);
        let minimum = build_pad_ctrl(DriveStrength::Minimum, PullResistor::None, false, false);
        assert_eq!(strongest & DS_MASK, 0);
        assert_eq!(minimum & DS_MASK, DS_MASK);
    }

    #[test]
    fn pull_none_sets_neither_pe_nor_ps() {
        // No pull => both PE and PS clear.
        let val = build_pad_ctrl(DriveStrength::Strongest, PullResistor::None, false, false);
        assert_eq!(val & (PE_BIT | PS_BIT), 0);
    }

    #[test]
    fn pull_up_sets_pe_and_ps() {
        // Pull-up => PE=1, PS=1.
        let val = build_pad_ctrl(DriveStrength::Strongest, PullResistor::Up, false, false);
        assert_eq!(val & PE_BIT, PE_BIT);
        assert_eq!(val & PS_BIT, PS_BIT);
    }

    #[test]
    fn pull_down_sets_pe_only() {
        // Pull-down => PE=1, PS=0 (selecting the down resistor).
        let val = build_pad_ctrl(DriveStrength::Strongest, PullResistor::Down, false, false);
        assert_eq!(val & PE_BIT, PE_BIT);
        assert_eq!(val & PS_BIT, 0);
    }

    #[test]
    fn schmitt_and_input_enable_are_independent_bits() {
        // ST (bit3) and IE (bit11) toggle only their own bits.
        let none = build_pad_ctrl(DriveStrength::Strongest, PullResistor::None, false, false);
        assert_eq!(none & (ST_BIT | IE_BIT), 0);

        let st = build_pad_ctrl(DriveStrength::Strongest, PullResistor::None, true, false);
        assert_eq!(st, ST_BIT);

        let ie = build_pad_ctrl(DriveStrength::Strongest, PullResistor::None, false, true);
        assert_eq!(ie, IE_BIT);

        let both = build_pad_ctrl(DriveStrength::Strongest, PullResistor::None, true, true);
        assert_eq!(both, ST_BIT | IE_BIT);
    }

    #[test]
    fn all_fields_compose_without_collision() {
        // A fully-populated config must be the bitwise OR of every field — no
        // field may share or stomp another's bits.
        let val = build_pad_ctrl(DriveStrength::Minimum, PullResistor::Up, true, true);
        assert_eq!(val, DS_MASK | PE_BIT | PS_BIT | ST_BIT | IE_BIT);
    }

    #[test]
    fn no_bits_set_outside_known_fields() {
        // Every legal combination must stay within the defined field mask;
        // nothing should ever set a reserved bit.
        let defined = DS_MASK | PE_BIT | PS_BIT | ST_BIT | IE_BIT;
        for &drive in &[DriveStrength::Strongest, DriveStrength::Medium, DriveStrength::Minimum] {
            for &pull in &[PullResistor::None, PullResistor::Up, PullResistor::Down] {
                for st in [false, true] {
                    for ie in [false, true] {
                        let val = build_pad_ctrl(drive, pull, st, ie);
                        assert_eq!(val & !defined, 0, "reserved bit set for {drive:?}/{pull:?}/{st}/{ie}");
                    }
                }
            }
        }
    }
}

// ── Property-based fuzz tests ──────────────────────────────────

#[cfg(all(test, not(target_arch = "riscv32")))]
mod proptests {
    use super::*;
    use proptest::prelude::*;

    const ST_BIT: u32 = 1 << 3;
    const DS_MASK: u32 = 0x7 << 4;
    const PE_BIT: u32 = 1 << 9;
    const PS_BIT: u32 = 1 << 10;
    const IE_BIT: u32 = 1 << 11;
    const DEFINED: u32 = DS_MASK | PE_BIT | PS_BIT | ST_BIT | IE_BIT;

    fn drive_from(i: u8) -> DriveStrength {
        match i % 8 {
            0 => DriveStrength::Strongest,
            1 => DriveStrength::Strong,
            2 => DriveStrength::Medium,
            3 => DriveStrength::Weak,
            4 => DriveStrength::Weaker,
            5 => DriveStrength::VeryWeak,
            6 => DriveStrength::Weakest,
            _ => DriveStrength::Minimum,
        }
    }

    fn pull_from(i: u8) -> PullResistor {
        match i % 3 {
            0 => PullResistor::None,
            1 => PullResistor::Up,
            _ => PullResistor::Down,
        }
    }

    proptest! {
        /// Fuzz: any field combination only ever sets defined bits, never reserved ones.
        #[test]
        fn never_sets_reserved_bits(d in any::<u8>(), p in any::<u8>(), st: bool, ie: bool) {
            let val = build_pad_ctrl(drive_from(d), pull_from(p), st, ie);
            prop_assert_eq!(val & !DEFINED, 0);
        }

        /// Fuzz: the DS field always equals the drive enum value shifted left by 4.
        #[test]
        fn ds_field_equals_discriminant_shifted(d in any::<u8>(), p in any::<u8>(), st: bool, ie: bool) {
            let drive = drive_from(d);
            let val = build_pad_ctrl(drive, pull_from(p), st, ie);
            prop_assert_eq!(val & DS_MASK, (drive as u32) << 4);
        }

        /// Fuzz: ST and IE bits track their boolean inputs exactly, regardless of other fields.
        #[test]
        fn st_ie_track_inputs(d in any::<u8>(), p in any::<u8>(), st: bool, ie: bool) {
            let val = build_pad_ctrl(drive_from(d), pull_from(p), st, ie);
            prop_assert_eq!(val & ST_BIT != 0, st);
            prop_assert_eq!(val & IE_BIT != 0, ie);
        }

        /// Fuzz: PE is set iff a pull is requested; PS is set iff that pull is up.
        #[test]
        fn pull_encoding_is_consistent(d in any::<u8>(), p in any::<u8>(), st: bool, ie: bool) {
            let pull = pull_from(p);
            let val = build_pad_ctrl(drive_from(d), pull, st, ie);
            let pe = val & PE_BIT != 0;
            let ps = val & PS_BIT != 0;
            match pull {
                PullResistor::None => { prop_assert!(!pe); prop_assert!(!ps); }
                PullResistor::Up => { prop_assert!(pe); prop_assert!(ps); }
                PullResistor::Down => { prop_assert!(pe); prop_assert!(!ps); }
            }
        }
    }
}