hal-mik32 0.2.2

Rust hardware abstraction layer for MIK32 microcontrollers.
Documentation
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
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
//! GPIO
use core::cell::Cell;
use core::convert::Infallible;
use core::marker::PhantomData;
use critical_section::Mutex;
use embedded_hal::digital::{ErrorType, InputPin, OutputPin, StatefulOutputPin};

use mik32_pac::Peripherals;

const GPIO_IRQ_LINE_SHIFT: u32 = 4;
const GPIO_IRQ_LINE_MUX_MASK: u32 = 0b1111;
const GPIO_MODE_BIT_LEVEL: u32 = 1 << 0;
const GPIO_MODE_BIT_EDGE: u32 = 1 << 1;
const GPIO_MODE_BIT_ANY_EDGE: u32 = 1 << 2;

static CURRENT_IRQ_LINE_MUX: Mutex<Cell<u32>> = Mutex::new(Cell::new(0));

/// Floating input (type state)
pub struct Floating;

/// Pulled down input (type state)
pub struct PullDown;

/// Pulled up input (type state)
pub struct PullUp;

/// Output mode (type state)
pub struct Output;

/// Func2Mode mode (type state)
pub struct Func2Mode;
pub struct Func3Mode;

/// Analog mode (type state)
pub struct Analog;

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[repr(u32)]
pub enum DriveStrength {
    Ma2 = 0b00,
    Ma4 = 0b01,
    Ma8 = 0b10,
}

impl DriveStrength {
    #[inline(always)]
    const fn bits(self) -> u32 {
        self as u32
    }
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[repr(u8)]
pub enum InterruptLine {
    Line0 = 0,
    Line1 = 1,
    Line2 = 2,
    Line3 = 3,
    Line4 = 4,
    Line5 = 5,
    Line6 = 6,
    Line7 = 7,
}

impl InterruptLine {
    #[inline(always)]
    const fn index(self) -> u32 {
        self as u32
    }

    #[inline(always)]
    const fn mask(self) -> u32 {
        1u32 << self.index()
    }

    #[inline(always)]
    const fn mux_shift(self) -> u32 {
        GPIO_IRQ_LINE_SHIFT * self.index()
    }
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[repr(u8)]
pub enum InterruptMode {
    Low = 0,
    High = GPIO_MODE_BIT_LEVEL as u8,
    Falling = GPIO_MODE_BIT_EDGE as u8,
    Rising = (GPIO_MODE_BIT_LEVEL | GPIO_MODE_BIT_EDGE) as u8,
    Change = (GPIO_MODE_BIT_EDGE | GPIO_MODE_BIT_ANY_EDGE) as u8,
}

impl InterruptMode {
    #[inline(always)]
    const fn bits(self) -> u32 {
        self as u32
    }
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[repr(u8)]
pub enum LineConfig {
    Line0Port0_0 = 0 | (0 << GPIO_IRQ_LINE_SHIFT),
    Line0Port0_8 = 1 | (0 << GPIO_IRQ_LINE_SHIFT),
    Line0Port1_0 = 2 | (0 << GPIO_IRQ_LINE_SHIFT),
    Line0Port1_8 = 3 | (0 << GPIO_IRQ_LINE_SHIFT),
    Line0Port2_0 = 4 | (0 << GPIO_IRQ_LINE_SHIFT),
    Line0Port0_4 = 5 | (0 << GPIO_IRQ_LINE_SHIFT),
    Line0Port0_12 = 6 | (0 << GPIO_IRQ_LINE_SHIFT),
    Line0Port1_4 = 7 | (0 << GPIO_IRQ_LINE_SHIFT),
    Line0Port1_12 = 8 | (0 << GPIO_IRQ_LINE_SHIFT),
    Line0Port2_4 = 9 | (0 << GPIO_IRQ_LINE_SHIFT),

    Line1Port0_1 = 0 | (1 << GPIO_IRQ_LINE_SHIFT),
    Line1Port0_9 = 1 | (1 << GPIO_IRQ_LINE_SHIFT),
    Line1Port1_1 = 2 | (1 << GPIO_IRQ_LINE_SHIFT),
    Line1Port1_9 = 3 | (1 << GPIO_IRQ_LINE_SHIFT),
    Line1Port2_1 = 4 | (1 << GPIO_IRQ_LINE_SHIFT),
    Line1Port0_5 = 5 | (1 << GPIO_IRQ_LINE_SHIFT),
    Line1Port0_13 = 6 | (1 << GPIO_IRQ_LINE_SHIFT),
    Line1Port1_5 = 7 | (1 << GPIO_IRQ_LINE_SHIFT),
    Line1Port1_13 = 8 | (1 << GPIO_IRQ_LINE_SHIFT),
    Line1Port2_5 = 9 | (1 << GPIO_IRQ_LINE_SHIFT),

    Line2Port0_2 = 0 | (2 << GPIO_IRQ_LINE_SHIFT),
    Line2Port0_10 = 1 | (2 << GPIO_IRQ_LINE_SHIFT),
    Line2Port1_2 = 2 | (2 << GPIO_IRQ_LINE_SHIFT),
    Line2Port1_10 = 3 | (2 << GPIO_IRQ_LINE_SHIFT),
    Line2Port2_2 = 4 | (2 << GPIO_IRQ_LINE_SHIFT),
    Line2Port0_6 = 5 | (2 << GPIO_IRQ_LINE_SHIFT),
    Line2Port0_14 = 6 | (2 << GPIO_IRQ_LINE_SHIFT),
    Line2Port1_6 = 7 | (2 << GPIO_IRQ_LINE_SHIFT),
    Line2Port1_14 = 8 | (2 << GPIO_IRQ_LINE_SHIFT),
    Line2Port2_6 = 9 | (2 << GPIO_IRQ_LINE_SHIFT),

    Line3Port0_3 = 0 | (3 << GPIO_IRQ_LINE_SHIFT),
    Line3Port0_11 = 1 | (3 << GPIO_IRQ_LINE_SHIFT),
    Line3Port1_3 = 2 | (3 << GPIO_IRQ_LINE_SHIFT),
    Line3Port1_11 = 3 | (3 << GPIO_IRQ_LINE_SHIFT),
    Line3Port2_3 = 4 | (3 << GPIO_IRQ_LINE_SHIFT),
    Line3Port0_7 = 5 | (3 << GPIO_IRQ_LINE_SHIFT),
    Line3Port0_15 = 6 | (3 << GPIO_IRQ_LINE_SHIFT),
    Line3Port1_7 = 7 | (3 << GPIO_IRQ_LINE_SHIFT),
    Line3Port1_15 = 8 | (3 << GPIO_IRQ_LINE_SHIFT),
    Line3Port2_7 = 9 | (3 << GPIO_IRQ_LINE_SHIFT),

    Line4Port0_4 = 0 | (4 << GPIO_IRQ_LINE_SHIFT),
    Line4Port0_12 = 1 | (4 << GPIO_IRQ_LINE_SHIFT),
    Line4Port1_4 = 2 | (4 << GPIO_IRQ_LINE_SHIFT),
    Line4Port1_12 = 3 | (4 << GPIO_IRQ_LINE_SHIFT),
    Line4Port2_4 = 4 | (4 << GPIO_IRQ_LINE_SHIFT),
    Line4Port0_0 = 5 | (4 << GPIO_IRQ_LINE_SHIFT),
    Line4Port0_8 = 6 | (4 << GPIO_IRQ_LINE_SHIFT),
    Line4Port1_0 = 7 | (4 << GPIO_IRQ_LINE_SHIFT),
    Line4Port1_8 = 8 | (4 << GPIO_IRQ_LINE_SHIFT),
    Line4Port2_0 = 9 | (4 << GPIO_IRQ_LINE_SHIFT),

    Line5Port0_5 = 0 | (5 << GPIO_IRQ_LINE_SHIFT),
    Line5Port0_13 = 1 | (5 << GPIO_IRQ_LINE_SHIFT),
    Line5Port1_5 = 2 | (5 << GPIO_IRQ_LINE_SHIFT),
    Line5Port1_13 = 3 | (5 << GPIO_IRQ_LINE_SHIFT),
    Line5Port2_5 = 4 | (5 << GPIO_IRQ_LINE_SHIFT),
    Line5Port0_1 = 5 | (5 << GPIO_IRQ_LINE_SHIFT),
    Line5Port0_9 = 6 | (5 << GPIO_IRQ_LINE_SHIFT),
    Line5Port1_1 = 7 | (5 << GPIO_IRQ_LINE_SHIFT),
    Line5Port1_9 = 8 | (5 << GPIO_IRQ_LINE_SHIFT),
    Line5Port2_1 = 9 | (5 << GPIO_IRQ_LINE_SHIFT),

    Line6Port0_6 = 0 | (6 << GPIO_IRQ_LINE_SHIFT),
    Line6Port0_14 = 1 | (6 << GPIO_IRQ_LINE_SHIFT),
    Line6Port1_6 = 2 | (6 << GPIO_IRQ_LINE_SHIFT),
    Line6Port1_14 = 3 | (6 << GPIO_IRQ_LINE_SHIFT),
    Line6Port2_6 = 4 | (6 << GPIO_IRQ_LINE_SHIFT),
    Line6Port0_2 = 5 | (6 << GPIO_IRQ_LINE_SHIFT),
    Line6Port0_10 = 6 | (6 << GPIO_IRQ_LINE_SHIFT),
    Line6Port1_2 = 7 | (6 << GPIO_IRQ_LINE_SHIFT),
    Line6Port1_10 = 8 | (6 << GPIO_IRQ_LINE_SHIFT),
    Line6Port2_2 = 9 | (6 << GPIO_IRQ_LINE_SHIFT),

    Line7Port0_7 = 0 | (7 << GPIO_IRQ_LINE_SHIFT),
    Line7Port0_15 = 1 | (7 << GPIO_IRQ_LINE_SHIFT),
    Line7Port1_7 = 2 | (7 << GPIO_IRQ_LINE_SHIFT),
    Line7Port1_15 = 3 | (7 << GPIO_IRQ_LINE_SHIFT),
    Line7Port2_7 = 4 | (7 << GPIO_IRQ_LINE_SHIFT),
    Line7Port0_3 = 5 | (7 << GPIO_IRQ_LINE_SHIFT),
    Line7Port0_11 = 6 | (7 << GPIO_IRQ_LINE_SHIFT),
    Line7Port1_3 = 7 | (7 << GPIO_IRQ_LINE_SHIFT),
    Line7Port1_11 = 8 | (7 << GPIO_IRQ_LINE_SHIFT),
    Line7Port2_3 = 9 | (7 << GPIO_IRQ_LINE_SHIFT),
}

impl LineConfig {
    #[inline(always)]
    const fn bits(self) -> u32 {
        self as u32
    }

    #[inline(always)]
    const fn line(self) -> InterruptLine {
        match self.bits() >> GPIO_IRQ_LINE_SHIFT {
            0 => InterruptLine::Line0,
            1 => InterruptLine::Line1,
            2 => InterruptLine::Line2,
            3 => InterruptLine::Line3,
            4 => InterruptLine::Line4,
            5 => InterruptLine::Line5,
            6 => InterruptLine::Line6,
            _ => InterruptLine::Line7,
        }
    }

    #[inline(always)]
    const fn mux(self) -> u32 {
        self.bits() & GPIO_IRQ_LINE_MUX_MASK
    }
}

pub struct GpioPort<const P: u8>;

pub struct GpioInterrupts;

pub fn init_port<const P: u8>() -> GpioPort<P> {
    let p = unsafe { Peripherals::steal() };
    enable_gpio_pin_clocks::<P>(&p);
    GpioPort
}

pub fn init_interrupts() -> GpioInterrupts {
    let p = unsafe { Peripherals::steal() };
    enable_gpio_irq_clock(&p);
    GpioInterrupts
}

pub fn init_interrupt_line(config: LineConfig, mode: InterruptMode) {
    let p = unsafe { Peripherals::steal() };

    let line = config.line();
    let line_mask = line.mask();
    let mode_bits = mode.bits();

    critical_section::with(|cs| {
        let mux = CURRENT_IRQ_LINE_MUX.borrow(cs);
        let shift = line.mux_shift();
        let mut current = mux.get();

        current &= !(GPIO_IRQ_LINE_MUX_MASK << shift);
        current |= config.mux() << shift;

        mux.set(current);
        p.gpio_irq.line_mux().write(|w| unsafe { w.bits(current) });
    });

    if mode_bits & GPIO_MODE_BIT_LEVEL != 0 {
        p.gpio_irq
            .level_set()
            .write(|w| unsafe { w.bits(line_mask) });
    } else {
        p.gpio_irq
            .level_clear()
            .write(|w| unsafe { w.bits(line_mask) });
    }

    if mode_bits & GPIO_MODE_BIT_EDGE != 0 {
        p.gpio_irq.edge().write(|w| unsafe { w.bits(line_mask) });
    } else {
        p.gpio_irq.level().write(|w| unsafe { w.bits(line_mask) });
    }

    if mode_bits & GPIO_MODE_BIT_ANY_EDGE != 0 {
        p.gpio_irq
            .any_edge_set()
            .write(|w| unsafe { w.bits(line_mask) });
    } else {
        p.gpio_irq
            .any_edge_clear()
            .write(|w| unsafe { w.bits(line_mask) });
    }

    enable_interrupt_line(line);
}

pub fn deinit_interrupt_line(line: InterruptLine) {
    let p = unsafe { Peripherals::steal() };

    let line_mask = line.mask();

    disable_interrupt_line(line);

    critical_section::with(|cs| {
        let mux = CURRENT_IRQ_LINE_MUX.borrow(cs);
        let shift = line.mux_shift();
        let current = mux.get() & !(GPIO_IRQ_LINE_MUX_MASK << shift);

        mux.set(current);
        p.gpio_irq.line_mux().write(|w| unsafe { w.bits(current) });
    });

    p.gpio_irq.level().write(|w| unsafe { w.bits(line_mask) });
    p.gpio_irq
        .level_clear()
        .write(|w| unsafe { w.bits(line_mask) });
    p.gpio_irq
        .any_edge_clear()
        .write(|w| unsafe { w.bits(line_mask) });
}

#[inline(always)]
pub fn enable_interrupt_line(line: InterruptLine) {
    let p = unsafe { Peripherals::steal() };

    p.gpio_irq
        .enable_set()
        .write(|w| unsafe { w.bits(line.mask()) });
}

#[inline(always)]
pub fn disable_interrupt_line(line: InterruptLine) {
    let p = unsafe { Peripherals::steal() };

    p.gpio_irq
        .enable_clear()
        .write(|w| unsafe { w.bits(line.mask()) });
}

#[inline(always)]
pub fn line_interrupt_state(line: InterruptLine) -> bool {
    let p = unsafe { Peripherals::steal() };

    p.gpio_irq.interrupt().read().bits() & line.mask() != 0
}

#[inline(always)]
pub fn line_pin_state(line: InterruptLine) -> bool {
    let p = unsafe { Peripherals::steal() };

    p.gpio_irq.state().read().bits() & line.mask() != 0
}

#[inline(always)]
pub fn clear_interrupt(line: InterruptLine) {
    let p = unsafe { Peripherals::steal() };

    p.gpio_irq.clear().write(|w| unsafe { w.bits(line.mask()) });
}

#[inline(always)]
pub fn clear_interrupts() {
    let p = unsafe { Peripherals::steal() };

    p.gpio_irq.clear().write(|w| unsafe { w.bits(0xff) });
}

pub trait InterruptSource<const LINE: u8> {
    const LINE_CONFIG: LineConfig;
}

pub struct InterruptPin<PIN, const LINE: u8>
where
    PIN: InterruptSource<LINE>,
{
    pin: PIN,
}

impl<PIN, const LINE: u8> InterruptPin<PIN, LINE>
where
    PIN: InterruptSource<LINE>,
{
    pub fn new(pin: PIN, mode: InterruptMode) -> Self {
        init_interrupt_line(PIN::LINE_CONFIG, mode);
        Self { pin }
    }

    pub fn release(self) -> PIN {
        self.pin
    }

    pub const fn line_config(&self) -> LineConfig {
        PIN::LINE_CONFIG
    }

    pub const fn line(&self) -> InterruptLine {
        PIN::LINE_CONFIG.line()
    }

    pub fn enable(&self) {
        enable_interrupt_line(self.line());
    }

    pub fn disable(&self) {
        disable_interrupt_line(self.line());
    }

    pub fn is_pending(&self) -> bool {
        line_interrupt_state(self.line())
    }

    pub fn is_high(&self) -> bool {
        line_pin_state(self.line())
    }

    pub fn is_low(&self) -> bool {
        !self.is_high()
    }

    pub fn clear_interrupt(&self) {
        clear_interrupt(self.line());
    }
}

macro_rules! impl_interrupt_source {
    ($port:literal, $pin:literal, $line:literal, $config:ident) => {
        impl<MODE> InterruptSource<$line> for Pin<$port, $pin, MODE> {
            const LINE_CONFIG: LineConfig = LineConfig::$config;
        }
    };
}

impl_interrupt_source!(0, 0, 0, Line0Port0_0);
impl_interrupt_source!(0, 8, 0, Line0Port0_8);
impl_interrupt_source!(1, 0, 0, Line0Port1_0);
impl_interrupt_source!(1, 8, 0, Line0Port1_8);
impl_interrupt_source!(2, 0, 0, Line0Port2_0);
impl_interrupt_source!(0, 4, 0, Line0Port0_4);
impl_interrupt_source!(0, 12, 0, Line0Port0_12);
impl_interrupt_source!(1, 4, 0, Line0Port1_4);
impl_interrupt_source!(1, 12, 0, Line0Port1_12);
impl_interrupt_source!(2, 4, 0, Line0Port2_4);

impl_interrupt_source!(0, 1, 1, Line1Port0_1);
impl_interrupt_source!(0, 9, 1, Line1Port0_9);
impl_interrupt_source!(1, 1, 1, Line1Port1_1);
impl_interrupt_source!(1, 9, 1, Line1Port1_9);
impl_interrupt_source!(2, 1, 1, Line1Port2_1);
impl_interrupt_source!(0, 5, 1, Line1Port0_5);
impl_interrupt_source!(0, 13, 1, Line1Port0_13);
impl_interrupt_source!(1, 5, 1, Line1Port1_5);
impl_interrupt_source!(1, 13, 1, Line1Port1_13);
impl_interrupt_source!(2, 5, 1, Line1Port2_5);

impl_interrupt_source!(0, 2, 2, Line2Port0_2);
impl_interrupt_source!(0, 10, 2, Line2Port0_10);
impl_interrupt_source!(1, 2, 2, Line2Port1_2);
impl_interrupt_source!(1, 10, 2, Line2Port1_10);
impl_interrupt_source!(2, 2, 2, Line2Port2_2);
impl_interrupt_source!(0, 6, 2, Line2Port0_6);
impl_interrupt_source!(0, 14, 2, Line2Port0_14);
impl_interrupt_source!(1, 6, 2, Line2Port1_6);
impl_interrupt_source!(1, 14, 2, Line2Port1_14);
impl_interrupt_source!(2, 6, 2, Line2Port2_6);

impl_interrupt_source!(0, 3, 3, Line3Port0_3);
impl_interrupt_source!(0, 11, 3, Line3Port0_11);
impl_interrupt_source!(1, 3, 3, Line3Port1_3);
impl_interrupt_source!(1, 11, 3, Line3Port1_11);
impl_interrupt_source!(2, 3, 3, Line3Port2_3);
impl_interrupt_source!(0, 7, 3, Line3Port0_7);
impl_interrupt_source!(0, 15, 3, Line3Port0_15);
impl_interrupt_source!(1, 7, 3, Line3Port1_7);
impl_interrupt_source!(1, 15, 3, Line3Port1_15);
impl_interrupt_source!(2, 7, 3, Line3Port2_7);

impl_interrupt_source!(0, 4, 4, Line4Port0_4);
impl_interrupt_source!(0, 12, 4, Line4Port0_12);
impl_interrupt_source!(1, 4, 4, Line4Port1_4);
impl_interrupt_source!(1, 12, 4, Line4Port1_12);
impl_interrupt_source!(2, 4, 4, Line4Port2_4);
impl_interrupt_source!(0, 0, 4, Line4Port0_0);
impl_interrupt_source!(0, 8, 4, Line4Port0_8);
impl_interrupt_source!(1, 0, 4, Line4Port1_0);
impl_interrupt_source!(1, 8, 4, Line4Port1_8);
impl_interrupt_source!(2, 0, 4, Line4Port2_0);

impl_interrupt_source!(0, 5, 5, Line5Port0_5);
impl_interrupt_source!(0, 13, 5, Line5Port0_13);
impl_interrupt_source!(1, 5, 5, Line5Port1_5);
impl_interrupt_source!(1, 13, 5, Line5Port1_13);
impl_interrupt_source!(2, 5, 5, Line5Port2_5);
impl_interrupt_source!(0, 1, 5, Line5Port0_1);
impl_interrupt_source!(0, 9, 5, Line5Port0_9);
impl_interrupt_source!(1, 1, 5, Line5Port1_1);
impl_interrupt_source!(1, 9, 5, Line5Port1_9);
impl_interrupt_source!(2, 1, 5, Line5Port2_1);

impl_interrupt_source!(0, 6, 6, Line6Port0_6);
impl_interrupt_source!(0, 14, 6, Line6Port0_14);
impl_interrupt_source!(1, 6, 6, Line6Port1_6);
impl_interrupt_source!(1, 14, 6, Line6Port1_14);
impl_interrupt_source!(2, 6, 6, Line6Port2_6);
impl_interrupt_source!(0, 2, 6, Line6Port0_2);
impl_interrupt_source!(0, 10, 6, Line6Port0_10);
impl_interrupt_source!(1, 2, 6, Line6Port1_2);
impl_interrupt_source!(1, 10, 6, Line6Port1_10);
impl_interrupt_source!(2, 2, 6, Line6Port2_2);

impl_interrupt_source!(0, 7, 7, Line7Port0_7);
impl_interrupt_source!(0, 15, 7, Line7Port0_15);
impl_interrupt_source!(1, 7, 7, Line7Port1_7);
impl_interrupt_source!(1, 15, 7, Line7Port1_15);
impl_interrupt_source!(2, 7, 7, Line7Port2_7);
impl_interrupt_source!(0, 3, 7, Line7Port0_3);
impl_interrupt_source!(0, 11, 7, Line7Port0_11);
impl_interrupt_source!(1, 3, 7, Line7Port1_3);
impl_interrupt_source!(1, 11, 7, Line7Port1_11);
impl_interrupt_source!(2, 3, 7, Line7Port2_3);

pub struct Pin<const P: u8, const N: u8, MODE = Floating> {
    _mode: PhantomData<MODE>,
}

impl<const P: u8, const N: u8, MODE> Pin<P, N, MODE> {
    pub const fn new() -> Self {
        Self { _mode: PhantomData }
    }

    pub fn set_drive_strength(&mut self, drive_strength: DriveStrength) {
        let p = unsafe { Peripherals::steal() };

        set_drive_strength::<P, N>(&p, drive_strength);
    }

    pub fn with_drive_strength(mut self, drive_strength: DriveStrength) -> Self {
        self.set_drive_strength(drive_strength);
        self
    }
}

pub trait OutputPermitted {}
pub trait SerialPermitted {}
pub trait TimerSerialPermitted {}
pub trait AnalogPermitted {}
pub trait InputMode {}

impl InputMode for Floating {}
impl InputMode for PullDown {}
impl InputMode for PullUp {}

impl<const P: u8, const N: u8, MODE> Pin<P, N, MODE>
where
    MODE: InputMode,
{
    pub fn into_interrupt_pin<const LINE: u8>(self, mode: InterruptMode) -> InterruptPin<Self, LINE>
    where
        Self: InterruptSource<LINE>,
    {
        InterruptPin::new(self, mode)
    }
}

#[inline(always)]
fn set_gpio_function<const P: u8, const N: u8>(p: &Peripherals) {
    let shift = 2 * N;
    let mask = 0b11u32 << shift;

    match P {
        0 => p
            .pad_config
            .pad0_cfg()
            .modify(|r, w| unsafe { w.bits(r.bits() & !mask) }),
        1 => p
            .pad_config
            .pad1_cfg()
            .modify(|r, w| unsafe { w.bits(r.bits() & !mask) }),
        2 => p
            .pad_config
            .pad2_cfg()
            .modify(|r, w| unsafe { w.bits(r.bits() & !mask) }),
        _ => panic!("Invalid port number {}", P),
    };
}

#[inline(always)]
fn set_alternate_function<const P: u8, const N: u8>(p: &Peripherals, function: u32) {
    let shift = 2 * N;
    let mask = 0b11u32 << shift;
    let value = function << shift;

    match P {
        0 => p
            .pad_config
            .pad0_cfg()
            .modify(|r, w| unsafe { w.bits((r.bits() & !mask) | value) }),
        1 => p
            .pad_config
            .pad1_cfg()
            .modify(|r, w| unsafe { w.bits((r.bits() & !mask) | value) }),
        2 => p
            .pad_config
            .pad2_cfg()
            .modify(|r, w| unsafe { w.bits((r.bits() & !mask) | value) }),
        _ => panic!("Invalid port number {}", P),
    };
}

#[inline(always)]
fn set_pull<const P: u8, const N: u8>(p: &Peripherals, pull: u32) {
    let shift = 2 * N;
    let mask = 0b11u32 << shift;

    match P {
        0 => p
            .pad_config
            .pad0_pupd()
            .modify(|r, w| unsafe { w.bits((r.bits() & !mask) | (pull << shift)) }),
        1 => p
            .pad_config
            .pad1_pupd()
            .modify(|r, w| unsafe { w.bits((r.bits() & !mask) | (pull << shift)) }),
        2 => p
            .pad_config
            .pad2_pupd()
            .modify(|r, w| unsafe { w.bits((r.bits() & !mask) | (pull << shift)) }),
        _ => panic!("Invalid port number {}", P),
    };
}

#[inline(always)]
fn set_drive_strength<const P: u8, const N: u8>(p: &Peripherals, drive_strength: DriveStrength) {
    let shift = 2 * N;
    let mask = 0b11u32 << shift;
    let value = drive_strength.bits() << shift;

    match P {
        0 => p
            .pad_config
            .pad0_ds()
            .modify(|r, w| unsafe { w.bits((r.bits() & !mask) | value) }),
        1 => p
            .pad_config
            .pad1_ds()
            .modify(|r, w| unsafe { w.bits((r.bits() & !mask) | value) }),
        2 => p
            .pad_config
            .pad2_ds()
            .modify(|r, w| unsafe { w.bits((r.bits() & !mask) | value) }),
        _ => panic!("Invalid port number {}", P),
    };
}

#[inline(always)]
fn set_direction_in<const P: u8, const N: u8>(p: &Peripherals) {
    match P {
        0 => p
            .gpio16_0
            .direction_in()
            .modify(|r, w| unsafe { w.bits(r.bits() | (1u32 << N)) }),
        1 => p
            .gpio16_1
            .direction_in()
            .modify(|r, w| unsafe { w.bits(r.bits() | (1u32 << N)) }),
        2 => p
            .gpio8_2
            .direction_in()
            .modify(|r, w| unsafe { w.bits(r.bits() | (1u32 << N)) }),
        _ => panic!("Invalid port number {}", P),
    };
}

#[inline(always)]
fn enable_pad_config_clock(p: &Peripherals) {
    p.pm.clk_apb_m_set()
        .modify(|_, w| w.pad_config().enable().pm().enable());
}

#[inline(always)]
fn enable_gpio_clock<const P: u8>(p: &Peripherals) {
    match P {
        0 => p.pm.clk_apb_p_set().modify(|_, w| w.gpio_0().enable()),
        1 => p.pm.clk_apb_p_set().modify(|_, w| w.gpio_1().enable()),
        2 => p.pm.clk_apb_p_set().modify(|_, w| w.gpio_2().enable()),
        _ => panic!("Invalid port number {}", P),
    };
}

#[inline(always)]
fn enable_gpio_pin_clocks<const P: u8>(p: &Peripherals) {
    enable_pad_config_clock(p);
    enable_gpio_clock::<P>(p);
}

#[inline(always)]
fn enable_gpio_irq_clock(p: &Peripherals) {
    p.pm.clk_apb_p_set().modify(|_, w| w.gpio_irq().enable());
}

impl<const P: u8, const N: u8, MODE> Pin<P, N, MODE> {
    pub fn into_output(self) -> Pin<P, N, Output>
    where
        Pin<P, N>: OutputPermitted,
    {
        let p = unsafe { Peripherals::steal() };

        set_gpio_function::<P, N>(&p);
        set_pull::<P, N>(&p, 0);

        match P {
            0 => {
                p.gpio16_0
                    .direction_out()
                    .modify(|r, w| unsafe { w.bits(r.bits() | (1u32 << N)) });
            }
            1 => {
                p.gpio16_1
                    .direction_out()
                    .modify(|r, w| unsafe { w.bits(r.bits() | (1u32 << N)) });
            }
            2 => {
                p.gpio8_2
                    .direction_out()
                    .modify(|r, w| unsafe { w.bits(r.bits() | (1u32 << N)) });
            }
            _ => panic!("Invalid port number {}", P),
        }

        Pin::new()
    }

    pub fn into_floating_input(self) -> Pin<P, N, Floating> {
        let p = unsafe { Peripherals::steal() };

        set_gpio_function::<P, N>(&p);
        set_pull::<P, N>(&p, 0);
        set_direction_in::<P, N>(&p);

        Pin::new()
    }

    pub fn into_pull_up_input(self) -> Pin<P, N, PullUp> {
        let p = unsafe { Peripherals::steal() };

        set_gpio_function::<P, N>(&p);
        set_pull::<P, N>(&p, 1);
        set_direction_in::<P, N>(&p);

        Pin::new()
    }

    pub fn into_pull_down_input(self) -> Pin<P, N, PullDown> {
        let p = unsafe { Peripherals::steal() };

        set_gpio_function::<P, N>(&p);
        set_pull::<P, N>(&p, 2);
        set_direction_in::<P, N>(&p);

        Pin::new()
    }

    pub fn into_serial_port(self) -> Pin<P, N, Func2Mode>
    where
        Pin<P, N>: SerialPermitted,
    {
        let p = unsafe { Peripherals::steal() };

        set_alternate_function::<P, N>(&p, 0b01);
        set_pull::<P, N>(&p, 0);

        Pin::new()
    }

    pub fn into_serial_port_pull_up(self) -> Pin<P, N, Func2Mode>
    where
        Pin<P, N>: SerialPermitted,
    {
        let p = unsafe { Peripherals::steal() };

        set_alternate_function::<P, N>(&p, 0b01);
        set_pull::<P, N>(&p, 1);

        Pin::new()
    }

    pub fn into_timer_serial_port(self) -> Pin<P, N, Func3Mode>
    where
        Pin<P, N>: TimerSerialPermitted,
    {
        let p = unsafe { Peripherals::steal() };

        set_alternate_function::<P, N>(&p, 0b10);
        set_pull::<P, N>(&p, 0);
        Pin::new()
    }

    pub fn into_analog(self) -> Pin<P, N, Analog>
    where
        Pin<P, N>: AnalogPermitted,
    {
        let p = unsafe { Peripherals::steal() };

        set_alternate_function::<P, N>(&p, 0b11);
        set_pull::<P, N>(&p, 0);
        Pin::new()
    }
}

impl<const P: u8, const N: u8, MODE> ErrorType for Pin<P, N, MODE> {
    type Error = Infallible;
}

/// Single digital push-pull output pin.
impl<const P: u8, const N: u8> OutputPin for Pin<P, N, Output> {
    /// Drives the pin high.
    #[inline(always)]
    fn set_high(&mut self) -> Result<(), Self::Error> {
        let p = unsafe { Peripherals::steal() };

        match P {
            0 => {
                p.gpio16_0.set().write(|w| unsafe { w.bits(1u32 << N) });
            }
            1 => {
                p.gpio16_1.set().write(|w| unsafe { w.bits(1u32 << N) });
            }
            2 => {
                p.gpio8_2.set().write(|w| unsafe { w.bits(1u32 << N) });
            }
            _ => panic!("Invalid port number {}", P),
        }

        Ok(())
    }

    /// Drives the pin low.
    #[inline(always)]
    fn set_low(&mut self) -> Result<(), Self::Error> {
        let p = unsafe { Peripherals::steal() };

        match P {
            0 => {
                p.gpio16_0.clear().write(|w| unsafe { w.bits(1u32 << N) });
            }
            1 => {
                p.gpio16_1.clear().write(|w| unsafe { w.bits(1u32 << N) });
            }
            2 => {
                p.gpio8_2.clear().write(|w| unsafe { w.bits(1u32 << N) });
            }
            _ => panic!("Invalid port number {}", P),
        }

        Ok(())
    }
}

impl<const P: u8, const N: u8> StatefulOutputPin for Pin<P, N, Output> {
    #[inline(always)]
    fn is_set_high(&mut self) -> Result<bool, Self::Error> {
        let p = unsafe { Peripherals::steal() };

        let mask = 1u32 << N;

        let is_set = match P {
            0 => p.gpio16_0.output().read().bits() & mask != 0,
            1 => p.gpio16_1.output().read().bits() & mask != 0,
            2 => p.gpio8_2.output().read().bits() & mask != 0,
            _ => panic!("Invalid port number {}", P),
        };

        Ok(is_set)
    }

    #[inline(always)]
    fn is_set_low(&mut self) -> Result<bool, Self::Error> {
        self.is_set_high().map(|is_high| !is_high)
    }
}

impl<const P: u8, const N: u8, MODE> InputPin for Pin<P, N, MODE>
where
    MODE: InputMode,
{
    #[inline(always)]
    fn is_high(&mut self) -> Result<bool, Self::Error> {
        let p = unsafe { Peripherals::steal() };

        let mask = 1u32 << N;

        let is_set = match P {
            0 => p.gpio16_0.state().read().bits() & mask != 0,
            1 => p.gpio16_1.state().read().bits() & mask != 0,
            2 => p.gpio8_2.state().read().bits() & mask != 0,
            _ => panic!("Invalid port number {}", P),
        };

        Ok(is_set)
    }

    #[inline(always)]
    fn is_low(&mut self) -> Result<bool, Self::Error> {
        self.is_high().map(|is_high| !is_high)
    }
}

pub mod port_0 {
    use super::{AnalogPermitted, OutputPermitted, Pin, SerialPermitted, TimerSerialPermitted};

    pub type Pin00 = Pin<0, 0>;
    impl OutputPermitted for Pin<0, 0> {}
    impl SerialPermitted for Pin<0, 0> {}
    impl TimerSerialPermitted for Pin<0, 0> {}

    pub type Pin01 = Pin<0, 1>;
    impl OutputPermitted for Pin<0, 1> {}
    impl SerialPermitted for Pin<0, 1> {}
    impl TimerSerialPermitted for Pin<0, 1> {}

    pub type Pin02 = Pin<0, 2>;
    impl OutputPermitted for Pin<0, 2> {}
    impl SerialPermitted for Pin<0, 2> {}
    impl TimerSerialPermitted for Pin<0, 2> {}
    impl AnalogPermitted for Pin<0, 2> {}

    pub type Pin03 = Pin<0, 3>;
    impl OutputPermitted for Pin<0, 3> {}
    impl SerialPermitted for Pin<0, 3> {}
    impl TimerSerialPermitted for Pin<0, 3> {}

    pub type Pin04 = Pin<0, 4>;
    impl OutputPermitted for Pin<0, 4> {}
    impl SerialPermitted for Pin<0, 4> {}
    impl TimerSerialPermitted for Pin<0, 4> {}
    impl AnalogPermitted for Pin<0, 4> {}

    // GPIO
    // UART 0 RX
    pub type Pin05 = Pin<0, 5>;
    impl OutputPermitted for Pin<0, 5> {}
    impl SerialPermitted for Pin<0, 5> {}
    impl TimerSerialPermitted for Pin<0, 5> {}

    // GPIO
    // UART 0 TX
    pub type Pin06 = Pin<0, 6>;
    impl OutputPermitted for Pin<0, 6> {}
    impl SerialPermitted for Pin<0, 6> {}
    impl TimerSerialPermitted for Pin<0, 6> {}

    // GPIO
    // UART 0 NCTS
    pub type Pin07 = Pin<0, 7>;
    impl OutputPermitted for Pin<0, 7> {}
    impl SerialPermitted for Pin<0, 7> {}
    impl TimerSerialPermitted for Pin<0, 7> {}
    impl AnalogPermitted for Pin<0, 7> {}

    // GPIO
    // UART 0 NRTS
    pub type Pin08 = Pin<0, 8>;
    impl OutputPermitted for Pin<0, 8> {}
    impl SerialPermitted for Pin<0, 8> {}
    impl TimerSerialPermitted for Pin<0, 8> {}

    pub type Pin09 = Pin<0, 9>;
    impl OutputPermitted for Pin<0, 9> {}
    impl SerialPermitted for Pin<0, 9> {}
    impl TimerSerialPermitted for Pin<0, 9> {}
    impl AnalogPermitted for Pin<0, 9> {}

    pub type Pin10 = Pin<0, 10>;
    impl OutputPermitted for Pin<0, 10> {}
    impl SerialPermitted for Pin<0, 10> {}
    impl TimerSerialPermitted for Pin<0, 10> {}

    pub type Pin11 = Pin<0, 11>;
    impl OutputPermitted for Pin<0, 11> {}
    impl TimerSerialPermitted for Pin<0, 11> {}
    impl AnalogPermitted for Pin<0, 11> {}

    pub type Pin12 = Pin<0, 12>;
    impl OutputPermitted for Pin<0, 12> {}
    impl TimerSerialPermitted for Pin<0, 12> {}

    pub type Pin13 = Pin<0, 13>;
    impl OutputPermitted for Pin<0, 13> {}
    impl TimerSerialPermitted for Pin<0, 13> {}
    impl AnalogPermitted for Pin<0, 13> {}

    pub type Pin14 = Pin<0, 14>;
    impl OutputPermitted for Pin<0, 14> {}

    pub type Pin15 = Pin<0, 15>;
    impl OutputPermitted for Pin<0, 15> {}
}

pub mod port_1 {
    use super::{AnalogPermitted, OutputPermitted, Pin, SerialPermitted, TimerSerialPermitted};

    pub type Pin00 = Pin<1, 0>;
    impl OutputPermitted for Pin<1, 0> {}
    impl SerialPermitted for Pin<1, 0> {}
    impl TimerSerialPermitted for Pin<1, 0> {}

    pub type Pin01 = Pin<1, 1>;
    impl OutputPermitted for Pin<1, 1> {}
    impl SerialPermitted for Pin<1, 1> {}
    impl TimerSerialPermitted for Pin<1, 1> {}

    pub type Pin02 = Pin<1, 2>;
    impl OutputPermitted for Pin<1, 2> {}
    impl SerialPermitted for Pin<1, 2> {}
    impl TimerSerialPermitted for Pin<1, 2> {}

    pub type Pin03 = Pin<1, 3>;
    impl OutputPermitted for Pin<1, 3> {}
    impl SerialPermitted for Pin<1, 3> {}
    impl TimerSerialPermitted for Pin<1, 3> {}

    pub type Pin04 = Pin<1, 4>;
    impl OutputPermitted for Pin<1, 4> {}
    impl SerialPermitted for Pin<1, 4> {}
    impl TimerSerialPermitted for Pin<1, 4> {}

    pub type Pin05 = Pin<1, 5>;
    impl OutputPermitted for Pin<1, 5> {}
    impl SerialPermitted for Pin<1, 5> {}
    impl TimerSerialPermitted for Pin<1, 5> {}
    impl AnalogPermitted for Pin<1, 5> {}

    pub type Pin06 = Pin<1, 6>;
    impl OutputPermitted for Pin<1, 6> {}
    impl SerialPermitted for Pin<1, 6> {}
    impl TimerSerialPermitted for Pin<1, 6> {}

    pub type Pin07 = Pin<1, 7>;
    impl OutputPermitted for Pin<1, 7> {}
    impl SerialPermitted for Pin<1, 7> {}
    impl AnalogPermitted for Pin<1, 7> {}

    // GPIO
    // UART 1 RX
    pub type Pin08 = Pin<1, 8>;
    impl OutputPermitted for Pin<1, 8> {}
    impl SerialPermitted for Pin<1, 8> {}

    // GPIO
    // UART 1 TX
    pub type Pin09 = Pin<1, 9>;
    impl OutputPermitted for Pin<1, 9> {}
    impl SerialPermitted for Pin<1, 9> {}

    pub type Pin10 = Pin<1, 10>;
    impl OutputPermitted for Pin<1, 10> {}
    impl SerialPermitted for Pin<1, 10> {}
    impl AnalogPermitted for Pin<1, 10> {}

    pub type Pin11 = Pin<1, 11>;
    impl OutputPermitted for Pin<1, 11> {}
    impl SerialPermitted for Pin<1, 11> {}
    impl AnalogPermitted for Pin<1, 11> {}

    pub type Pin12 = Pin<1, 12>;
    impl OutputPermitted for Pin<1, 12> {}
    impl SerialPermitted for Pin<1, 12> {}
    impl TimerSerialPermitted for Pin<1, 12> {}
    impl AnalogPermitted for Pin<1, 12> {}

    pub type Pin13 = Pin<1, 13>;
    impl OutputPermitted for Pin<1, 13> {}
    impl SerialPermitted for Pin<1, 13> {}
    impl TimerSerialPermitted for Pin<1, 13> {}
    impl AnalogPermitted for Pin<1, 13> {}

    pub type Pin14 = Pin<1, 14>;
    impl OutputPermitted for Pin<1, 14> {}
    impl SerialPermitted for Pin<1, 14> {}
    impl TimerSerialPermitted for Pin<1, 14> {}

    pub type Pin15 = Pin<1, 15>;
    impl OutputPermitted for Pin<1, 15> {}
    impl SerialPermitted for Pin<1, 15> {}
    impl TimerSerialPermitted for Pin<1, 15> {}
}

pub mod port_2 {
    use super::{OutputPermitted, Pin, SerialPermitted, TimerSerialPermitted};

    pub type Pin00 = Pin<2, 0>;
    impl OutputPermitted for Pin<2, 0> {}
    impl SerialPermitted for Pin<2, 0> {}
    impl TimerSerialPermitted for Pin<2, 0> {}

    pub type Pin01 = Pin<2, 1>;
    impl OutputPermitted for Pin<2, 1> {}
    impl SerialPermitted for Pin<2, 1> {}
    impl TimerSerialPermitted for Pin<2, 1> {}

    pub type Pin02 = Pin<2, 2>;
    impl OutputPermitted for Pin<2, 2> {}
    impl SerialPermitted for Pin<2, 2> {}
    impl TimerSerialPermitted for Pin<2, 2> {}

    pub type Pin03 = Pin<2, 3>;
    impl OutputPermitted for Pin<2, 3> {}
    impl SerialPermitted for Pin<2, 3> {}
    impl TimerSerialPermitted for Pin<2, 3> {}

    pub type Pin04 = Pin<2, 4>;
    impl OutputPermitted for Pin<2, 4> {}
    impl SerialPermitted for Pin<2, 4> {}

    pub type Pin05 = Pin<2, 5>;
    impl OutputPermitted for Pin<2, 5> {}
    impl SerialPermitted for Pin<2, 5> {}

    pub type Pin06 = Pin<2, 6>;
    impl OutputPermitted for Pin<2, 6> {}
    impl SerialPermitted for Pin<2, 6> {}
    impl TimerSerialPermitted for Pin<2, 6> {}

    pub type Pin07 = Pin<2, 7>;
    impl OutputPermitted for Pin<2, 7> {}
    impl TimerSerialPermitted for Pin<2, 7> {}
}