nrf-hal-common 0.18.0

Implementation details of the nRF HAL crates. Don't use this directly, use one of the specific HAL crates instead (`nrfXYZ-hal`).
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
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
//! HAL interface to the PWM peripheral.
//!
//! The pulse with modulation (PWM) module enables the generation of pulse width modulated signals on GPIO.

#[cfg(not(any(feature = "9160", feature = "5340-app")))]
use crate::pac::pwm0::*;
#[cfg(any(feature = "9160", feature = "5340-app"))]
use crate::pac::pwm0_ns::*;

use crate::{
    gpio::{Output, Pin, PushPull},
    pac::Interrupt,
    target_constants::{SRAM_LOWER, SRAM_UPPER},
    time::*,
};
use core::{
    convert::Infallible,
    ops::Deref,
    ptr::addr_of_mut,
    sync::atomic::{compiler_fence, Ordering},
};
use embedded_dma::*;
use embedded_hal::pwm::{ErrorType, SetDutyCycle};

const MAX_SEQ_LEN: usize = 0x7FFF;

/// A safe wrapper around the raw peripheral.
#[derive(Debug)]
pub struct Pwm<T: Instance> {
    pwm: T,
}

impl<T> Pwm<T>
where
    T: Instance,
{
    /// Takes ownership of the peripheral and applies sane defaults.
    pub fn new(pwm: T) -> Pwm<T> {
        compiler_fence(Ordering::SeqCst);
        pwm.enable.write(|w| w.enable().enabled());
        pwm.mode.write(|w| w.updown().up());
        pwm.prescaler.write(|w| w.prescaler().div_1());
        pwm.countertop
            .write(|w| unsafe { w.countertop().bits(32767) });
        pwm.loop_.write(|w| w.cnt().disabled());
        pwm.decoder.write(|w| {
            w.load().individual();
            w.mode().refresh_count()
        });
        pwm.seq0.refresh.write(|w| unsafe { w.bits(0) });
        pwm.seq0.enddelay.write(|w| unsafe { w.bits(0) });
        pwm.seq1.refresh.write(|w| unsafe { w.bits(0) });
        pwm.seq1.enddelay.write(|w| unsafe { w.bits(0) });

        Self { pwm }
    }

    /// Sets the PWM clock prescaler.
    #[inline(always)]
    pub fn set_prescaler(&self, div: Prescaler) -> &Self {
        self.pwm.prescaler.write(|w| w.prescaler().bits(div.into()));
        self
    }

    /// Sets the PWM clock prescaler.
    #[inline(always)]
    pub fn prescaler(&self) -> Prescaler {
        match self.pwm.prescaler.read().prescaler().bits() {
            0 => Prescaler::Div1,
            1 => Prescaler::Div2,
            2 => Prescaler::Div4,
            3 => Prescaler::Div8,
            4 => Prescaler::Div16,
            5 => Prescaler::Div32,
            6 => Prescaler::Div64,
            7 => Prescaler::Div128,
            _ => unreachable!(),
        }
    }

    /// Sets the maximum duty cycle value.
    #[inline(always)]
    pub fn set_max_duty(&self, duty: u16) -> &Self {
        self.pwm
            .countertop
            .write(|w| unsafe { w.countertop().bits(duty.min(32767u16)) });
        self
    }
    /// Returns the maximum duty cycle value.
    #[inline(always)]
    pub fn max_duty(&self) -> u16 {
        self.pwm.countertop.read().countertop().bits()
    }

    /// Sets the PWM output frequency.
    #[inline(always)]
    pub fn set_period(&self, freq: Hertz) -> &Self {
        let duty = match self.prescaler() {
            Prescaler::Div1 => 16_000_000u32 / freq.0,
            Prescaler::Div2 => 8_000_000u32 / freq.0,
            Prescaler::Div4 => 4_000_000u32 / freq.0,
            Prescaler::Div8 => 2_000_000u32 / freq.0,
            Prescaler::Div16 => 1_000_000u32 / freq.0,
            Prescaler::Div32 => 500_000u32 / freq.0,
            Prescaler::Div64 => 250_000u32 / freq.0,
            Prescaler::Div128 => 125_000u32 / freq.0,
        };
        match self.counter_mode() {
            CounterMode::Up => self.set_max_duty(duty.min(32767) as u16),
            CounterMode::UpAndDown => self.set_max_duty((duty / 2).min(32767) as u16),
        };
        self
    }

    /// Returns the PWM output frequency.
    #[inline(always)]
    pub fn period(&self) -> Hertz {
        let max_duty = self.max_duty() as u32;
        let freq = match self.prescaler() {
            Prescaler::Div1 => 16_000_000u32 / max_duty,
            Prescaler::Div2 => 8_000_000u32 / max_duty,
            Prescaler::Div4 => 4_000_000u32 / max_duty,
            Prescaler::Div8 => 2_000_000u32 / max_duty,
            Prescaler::Div16 => 1_000_000u32 / max_duty,
            Prescaler::Div32 => 500_000u32 / max_duty,
            Prescaler::Div64 => 250_000u32 / max_duty,
            Prescaler::Div128 => 125_000u32 / max_duty,
        };
        match self.counter_mode() {
            CounterMode::Up => freq.hz(),
            CounterMode::UpAndDown => (freq / 2).hz(),
        }
    }

    /// Sets the associated output pin for the PWM channel.
    ///
    /// Modifying the pin configuration while the PWM instance is enabled is not recommended.
    #[inline(always)]
    pub fn set_output_pin(&self, channel: Channel, pin: Pin<Output<PushPull>>) -> &Self {
        self.pwm.psel.out[usize::from(channel)].write(|w| {
            unsafe { w.bits(pin.psel_bits()) };
            w.connect().connected()
        });
        self
    }

    /// Sets the output pin of `channel`, and returns the old pin (if any).
    ///
    /// Modifying the pin configuration while the PWM instance is enabled is not recommended.
    pub fn swap_output_pin(
        &mut self,
        channel: Channel,
        pin: Pin<Output<PushPull>>,
    ) -> Option<Pin<Output<PushPull>>> {
        // (needs `&mut self` because it reads, then writes, to the register)
        let psel = &self.pwm.psel.out[usize::from(channel)];
        let old = psel.read();
        let old = if old.connect().is_connected() {
            unsafe { Some(Pin::from_psel_bits(old.bits())) }
        } else {
            None
        };
        self.set_output_pin(channel, pin);
        old
    }

    /// Disables the output pin of `channel`.
    ///
    /// The output pin is returned, if one was previously configured.
    ///
    /// Modifying the pin configuration while the PWM instance is enabled is not recommended.
    pub fn clear_output_pin(&mut self, channel: Channel) -> Option<Pin<Output<PushPull>>> {
        // (needs `&mut self` because it reads, then writes, to the register)
        let psel = &self.pwm.psel.out[usize::from(channel)];
        let old = psel.read();
        let old = if old.connect().is_connected() {
            unsafe { Some(Pin::from_psel_bits(old.bits())) }
        } else {
            None
        };
        psel.reset();
        old
    }

    /// Enables the PWM generator.
    #[inline(always)]
    pub fn enable(&self) {
        self.pwm.enable.write(|w| w.enable().enabled());
    }

    /// Disables the PWM generator.
    #[inline(always)]
    pub fn disable(&self) {
        self.pwm.enable.write(|w| w.enable().disabled());
    }

    /// Enables a PWM channel.
    #[inline(always)]
    pub fn enable_channel(&self, channel: Channel) -> &Self {
        self.pwm.psel.out[usize::from(channel)].modify(|_r, w| w.connect().connected());
        self
    }

    /// Disables a PWM channel.
    #[inline(always)]
    pub fn disable_channel(&self, channel: Channel) -> &Self {
        self.pwm.psel.out[usize::from(channel)].modify(|_r, w| w.connect().disconnected());
        self
    }

    /// Enables a PWM group.
    #[inline(always)]
    pub fn enable_group(&self, group: Group) -> &Self {
        match group {
            Group::G0 => {
                self.pwm.psel.out[0].modify(|_r, w| w.connect().connected());
                self.pwm.psel.out[1].modify(|_r, w| w.connect().connected());
            }
            Group::G1 => {
                self.pwm.psel.out[2].modify(|_r, w| w.connect().connected());
                self.pwm.psel.out[3].modify(|_r, w| w.connect().connected());
            }
        }
        self
    }

    /// Disables a PWM group.
    #[inline(always)]
    pub fn disable_group(&self, group: Group) -> &Self {
        match group {
            Group::G0 => {
                self.pwm.psel.out[0].modify(|_r, w| w.connect().disconnected());
                self.pwm.psel.out[1].modify(|_r, w| w.connect().disconnected());
            }
            Group::G1 => {
                self.pwm.psel.out[2].modify(|_r, w| w.connect().disconnected());
                self.pwm.psel.out[3].modify(|_r, w| w.connect().disconnected());
            }
        }
        self
    }

    /// Cofigures how a sequence is read from RAM and is spread to the compare register.
    #[inline(always)]
    pub fn set_load_mode(&self, mode: LoadMode) -> &Self {
        self.pwm.decoder.modify(|_r, w| w.load().bits(mode.into()));
        if mode == LoadMode::Waveform {
            self.disable_channel(Channel::C3);
        } else {
            self.enable_channel(Channel::C3);
        }
        self
    }

    /// Returns how a sequence is read from RAM and is spread to the compare register.
    #[inline(always)]
    pub fn load_mode(&self) -> LoadMode {
        match self.pwm.decoder.read().load().bits() {
            0 => LoadMode::Common,
            1 => LoadMode::Grouped,
            2 => LoadMode::Individual,
            3 => LoadMode::Waveform,
            _ => unreachable!(),
        }
    }

    /// Selects operating mode of the wave counter.
    #[inline(always)]
    pub fn set_counter_mode(&self, mode: CounterMode) -> &Self {
        self.pwm.mode.write(|w| w.updown().bit(mode.into()));
        self
    }

    /// Returns selected operating mode of the wave counter.
    #[inline(always)]
    pub fn counter_mode(&self) -> CounterMode {
        match self.pwm.mode.read().updown().bit() {
            false => CounterMode::Up,
            true => CounterMode::UpAndDown,
        }
    }

    /// Selects source for advancing the active sequence.
    #[inline(always)]
    pub fn set_step_mode(&self, mode: StepMode) -> &Self {
        self.pwm.decoder.modify(|_r, w| w.mode().bit(mode.into()));
        self
    }

    /// Returns selected source for advancing the active sequence.
    #[inline(always)]
    pub fn step_mode(&self) -> StepMode {
        match self.pwm.decoder.read().mode().bit() {
            false => StepMode::Auto,
            true => StepMode::NextStep,
        }
    }

    // Internal helper function that returns 15 bit duty cycle value.
    #[inline(always)]
    fn duty_on_value(&self, index: usize) -> u16 {
        let val = unsafe { (*T::buffer())[index] };
        let is_inverted = (val >> 15) & 1 == 0;
        match is_inverted {
            false => val,
            true => self.max_duty() - (val & 0x7FFF),
        }
    }

    // Internal helper function that returns 15 bit inverted duty cycle value.
    #[inline(always)]
    fn duty_off_value(&self, index: usize) -> u16 {
        let val = unsafe { (*T::buffer())[index] };
        let is_inverted = (val >> 15) & 1 == 0;
        match is_inverted {
            false => self.max_duty() - val,
            true => val & 0x7FFF,
        }
    }

    /// Sets duty cycle (15 bit) for all PWM channels.
    /// Will replace any ongoing sequence playback.
    pub fn set_duty_on_common(&self, duty: u16) {
        let buffer = T::buffer();
        unsafe {
            *buffer = [duty.min(self.max_duty()) & 0x7FFF; 4];
        }
        self.one_shot();
        self.set_load_mode(LoadMode::Common);
        self.pwm
            .seq0
            .ptr
            .write(|w| unsafe { w.bits(buffer as u32) });
        self.pwm.seq0.cnt.write(|w| unsafe { w.bits(1) });
        self.start_seq(Seq::Seq0);
    }

    /// Sets inverted duty cycle (15 bit) for all PWM channels.
    /// Will replace any ongoing sequence playback.
    pub fn set_duty_off_common(&self, duty: u16) {
        let buffer = T::buffer();
        unsafe {
            *buffer = [duty.min(self.max_duty()) | 0x8000; 4];
        }
        self.one_shot();
        self.set_load_mode(LoadMode::Common);
        self.pwm
            .seq0
            .ptr
            .write(|w| unsafe { w.bits(buffer as u32) });
        self.pwm.seq0.cnt.write(|w| unsafe { w.bits(1) });
        self.start_seq(Seq::Seq0);
    }

    /// Returns the common duty cycle value for all PWM channels in `Common` load mode.
    #[inline(always)]
    pub fn duty_on_common(&self) -> u16 {
        self.duty_on_value(0)
    }

    /// Returns the inverted common duty cycle value for all PWM channels in `Common` load mode.
    #[inline(always)]
    pub fn duty_off_common(&self) -> u16 {
        self.duty_off_value(0)
    }

    /// Sets duty cycle (15 bit) for a PWM group.
    /// Will replace any ongoing sequence playback.
    pub fn set_duty_on_group(&self, group: Group, duty: u16) {
        let buffer = T::buffer();
        unsafe {
            (*buffer)[usize::from(group)] = duty.min(self.max_duty()) & 0x7FFF;
        }
        self.one_shot();
        self.set_load_mode(LoadMode::Grouped);
        self.pwm
            .seq0
            .ptr
            .write(|w| unsafe { w.bits(buffer as u32) });
        self.pwm.seq0.cnt.write(|w| unsafe { w.bits(2) });
        self.start_seq(Seq::Seq0);
    }

    /// Sets inverted duty cycle (15 bit) for a PWM group.
    /// Will replace any ongoing sequence playback.
    pub fn set_duty_off_group(&self, group: Group, duty: u16) {
        let buffer = T::buffer();
        unsafe {
            (*buffer)[usize::from(group)] = duty.min(self.max_duty()) | 0x8000;
        }
        self.one_shot();
        self.set_load_mode(LoadMode::Grouped);
        self.pwm
            .seq0
            .ptr
            .write(|w| unsafe { w.bits(buffer as u32) });
        self.pwm.seq0.cnt.write(|w| unsafe { w.bits(2) });
        self.start_seq(Seq::Seq0);
    }

    /// Returns duty cycle value for a PWM group.
    #[inline(always)]
    pub fn duty_on_group(&self, group: Group) -> u16 {
        self.duty_on_value(usize::from(group))
    }

    /// Returns inverted duty cycle value for a PWM group.
    #[inline(always)]
    pub fn duty_off_group(&self, group: Group) -> u16 {
        self.duty_off_value(usize::from(group))
    }

    /// Sets duty cycle (15 bit) for a PWM channel.
    /// Will replace any ongoing sequence playback and the other channels will return to their previously set value.
    pub fn set_duty_on(&self, channel: Channel, duty: u16) {
        let buffer = T::buffer();
        unsafe {
            (*buffer)[usize::from(channel)] = duty.min(self.max_duty()) & 0x7FFF;
        }
        self.one_shot();
        self.set_load_mode(LoadMode::Individual);
        self.pwm
            .seq0
            .ptr
            .write(|w| unsafe { w.bits(buffer as u32) });
        self.pwm.seq0.cnt.write(|w| unsafe { w.bits(4) });
        self.start_seq(Seq::Seq0);
    }

    /// Sets inverted duty cycle (15 bit) for a PWM channel.
    /// Will replace any ongoing sequence playback and the other channels will return to their previously set value.
    pub fn set_duty_off(&self, channel: Channel, duty: u16) {
        let buffer = T::buffer();
        unsafe {
            (*buffer)[usize::from(channel)] = duty.min(self.max_duty()) | 0x8000;
        }
        self.one_shot();
        self.set_load_mode(LoadMode::Individual);
        self.pwm
            .seq0
            .ptr
            .write(|w| unsafe { w.bits(buffer as u32) });
        self.pwm.seq0.cnt.write(|w| unsafe { w.bits(4) });
        self.start_seq(Seq::Seq0);
    }

    /// Returns the duty cycle value for a PWM channel.
    #[inline(always)]
    pub fn duty_on(&self, channel: Channel) -> u16 {
        self.duty_on_value(usize::from(channel))
    }

    /// Returns the inverted duty cycle value for a PWM group.
    #[inline(always)]
    pub fn duty_off(&self, channel: Channel) -> u16 {
        self.duty_off_value(usize::from(channel))
    }

    /// Sets number of playbacks of sequences.
    #[inline(always)]
    pub fn set_loop(&self, mode: Loop) {
        self.pwm.loop_.write(|w| match mode {
            Loop::Disabled => w.cnt().disabled(),
            Loop::Times(n) => unsafe { w.cnt().bits(n) },
            Loop::Inf => unsafe { w.cnt().bits(2) },
        });
        self.pwm.shorts.write(|w| match mode {
            Loop::Inf => w.loopsdone_seqstart0().enabled(),
            _ => w.loopsdone_seqstart0().disabled(),
        });
    }

    /// Looping disabled (stop at the end of the sequence).
    #[inline(always)]
    pub fn one_shot(&self) -> &Self {
        self.set_loop(Loop::Disabled);
        self
    }

    /// Loops playback of sequences indefinitely.
    #[inline(always)]
    pub fn loop_inf(&self) -> &Self {
        self.set_loop(Loop::Inf);
        self
    }

    /// Sets number of playbacks of sequences.
    #[inline(always)]
    pub fn repeat(&self, times: u16) -> &Self {
        self.set_loop(Loop::Times(times));
        self
    }

    /// Sets number of additional PWM periods between samples loaded into compare register.
    #[inline(always)]
    pub fn set_seq_refresh(&self, seq: Seq, periods: u32) -> &Self {
        match seq {
            Seq::Seq0 => self.pwm.seq0.refresh.write(|w| unsafe { w.bits(periods) }),
            Seq::Seq1 => self.pwm.seq1.refresh.write(|w| unsafe { w.bits(periods) }),
        }
        self
    }

    /// Sets number of additional PWM periods after the sequence ends.
    #[inline(always)]
    pub fn set_seq_end_delay(&self, seq: Seq, periods: u32) -> &Self {
        match seq {
            Seq::Seq0 => self.pwm.seq0.enddelay.write(|w| unsafe { w.bits(periods) }),
            Seq::Seq1 => self.pwm.seq1.enddelay.write(|w| unsafe { w.bits(periods) }),
        }
        self
    }

    /// Loads the first PWM value on all enabled channels from a sequence and starts playing that sequence.
    /// Causes PWM generation to start if not running.
    #[inline(always)]
    pub fn start_seq(&self, seq: Seq) {
        compiler_fence(Ordering::SeqCst);
        self.pwm.enable.write(|w| w.enable().enabled());
        self.pwm.tasks_seqstart[usize::from(seq)].write(|w| unsafe { w.bits(1) });
        while self.pwm.events_seqstarted[usize::from(seq)].read().bits() == 0 {}
        self.pwm.events_seqend[0].write(|w| w);
        self.pwm.events_seqend[1].write(|w| w);
    }

    /// Steps by one value in the current sequence on all enabled channels, if the `NextStep` step mode is selected.
    /// Does not cause PWM generation to start if not running.
    #[inline(always)]
    pub fn next_step(&self) {
        self.pwm.tasks_nextstep.write(|w| unsafe { w.bits(1) });
    }

    /// Stops PWM pulse generation on all channels at the end of current PWM period, and stops sequence playback.
    #[inline(always)]
    pub fn stop(&self) {
        compiler_fence(Ordering::SeqCst);
        self.pwm.tasks_stop.write(|w| unsafe { w.bits(1) });
        while self.pwm.events_stopped.read().bits() == 0 {}
    }

    /// Loads the given sequence buffers and optionally (re-)starts sequence playback.
    /// Returns a `PemSeq`, containing `Pwm<T>` and the buffers.
    #[allow(unused_mut)]
    pub fn load<B0, B1>(
        mut self,
        seq0_buffer: Option<B0>,
        seq1_buffer: Option<B1>,
        start: bool,
    ) -> Result<PwmSeq<T, B0, B1>, (Error, Pwm<T>, Option<B0>, Option<B1>)>
    where
        B0: ReadBuffer<Word = u16> + 'static,
        B1: ReadBuffer<Word = u16> + 'static,
    {
        if let Some(buf) = &seq0_buffer {
            let (ptr, len) = unsafe { buf.read_buffer() };
            if (ptr as usize) < SRAM_LOWER || (ptr as usize) > SRAM_UPPER {
                return Err((
                    Error::DMABufferNotInDataMemory,
                    self,
                    seq0_buffer,
                    seq1_buffer,
                ));
            }
            if len > MAX_SEQ_LEN {
                return Err((Error::BufferTooLong, self, seq0_buffer, seq1_buffer));
            }
            compiler_fence(Ordering::SeqCst);
            self.pwm.seq0.ptr.write(|w| unsafe { w.bits(ptr as u32) });
            self.pwm.seq0.cnt.write(|w| unsafe { w.bits(len as u32) });
            if start {
                self.start_seq(Seq::Seq0);
            }
        } else {
            self.pwm.seq0.cnt.write(|w| unsafe { w.bits(0) });
        }

        if let Some(buf) = &seq1_buffer {
            let (ptr, len) = unsafe { buf.read_buffer() };
            if (ptr as usize) < SRAM_LOWER || (ptr as usize) > SRAM_UPPER {
                return Err((
                    Error::DMABufferNotInDataMemory,
                    self,
                    seq0_buffer,
                    seq1_buffer,
                ));
            }
            if len > MAX_SEQ_LEN {
                return Err((Error::BufferTooLong, self, seq0_buffer, seq1_buffer));
            }
            compiler_fence(Ordering::SeqCst);
            self.pwm.seq1.ptr.write(|w| unsafe { w.bits(ptr as u32) });
            self.pwm.seq1.cnt.write(|w| unsafe { w.bits(len as u32) });
            if start {
                self.start_seq(Seq::Seq1);
            }
        } else {
            self.pwm.seq1.cnt.write(|w| unsafe { w.bits(0) });
        }

        Ok(PwmSeq {
            inner: Some(Inner {
                seq0_buffer,
                seq1_buffer,
                pwm: self,
            }),
        })
    }

    /// Enables interrupt triggering on the specified event.
    #[inline(always)]
    pub fn enable_interrupt(&self, event: PwmEvent) -> &Self {
        match event {
            PwmEvent::Stopped => self.pwm.intenset.modify(|_r, w| w.stopped().set()),
            PwmEvent::LoopsDone => self.pwm.intenset.modify(|_r, w| w.loopsdone().set()),
            PwmEvent::PwmPeriodEnd => self.pwm.intenset.modify(|_r, w| w.pwmperiodend().set()),
            PwmEvent::SeqStarted(seq) => match seq {
                Seq::Seq0 => self.pwm.intenset.modify(|_r, w| w.seqstarted0().set()),
                Seq::Seq1 => self.pwm.intenset.modify(|_r, w| w.seqstarted1().set()),
            },
            PwmEvent::SeqEnd(seq) => match seq {
                Seq::Seq0 => self.pwm.intenset.modify(|_r, w| w.seqend0().set()),
                Seq::Seq1 => self.pwm.intenset.modify(|_r, w| w.seqend1().set()),
            },
        };
        self
    }

    /// Disables interrupt triggering on the specified event.
    #[inline(always)]
    pub fn disable_interrupt(&self, event: PwmEvent) -> &Self {
        match event {
            PwmEvent::Stopped => self.pwm.intenclr.modify(|_r, w| w.stopped().clear()),
            PwmEvent::LoopsDone => self.pwm.intenclr.modify(|_r, w| w.loopsdone().clear()),
            PwmEvent::PwmPeriodEnd => self.pwm.intenclr.modify(|_r, w| w.pwmperiodend().clear()),
            PwmEvent::SeqStarted(seq) => match seq {
                Seq::Seq0 => self.pwm.intenclr.modify(|_r, w| w.seqstarted0().clear()),
                Seq::Seq1 => self.pwm.intenclr.modify(|_r, w| w.seqstarted1().clear()),
            },
            PwmEvent::SeqEnd(seq) => match seq {
                Seq::Seq0 => self.pwm.intenclr.modify(|_r, w| w.seqend0().clear()),
                Seq::Seq1 => self.pwm.intenclr.modify(|_r, w| w.seqend1().clear()),
            },
        };
        self
    }

    /// Checks if an event has been triggered.
    #[inline(always)]
    pub fn is_event_triggered(&self, event: PwmEvent) -> bool {
        match event {
            PwmEvent::Stopped => self.pwm.events_stopped.read().bits() != 0,
            PwmEvent::LoopsDone => self.pwm.events_loopsdone.read().bits() != 0,
            PwmEvent::PwmPeriodEnd => self.pwm.events_pwmperiodend.read().bits() != 0,
            PwmEvent::SeqStarted(seq) => {
                self.pwm.events_seqstarted[usize::from(seq)].read().bits() != 0
            }
            PwmEvent::SeqEnd(seq) => self.pwm.events_seqend[usize::from(seq)].read().bits() != 0,
        }
    }

    /// Marks event as handled.
    #[inline(always)]
    pub fn reset_event(&self, event: PwmEvent) {
        match event {
            PwmEvent::Stopped => self.pwm.events_stopped.write(|w| w),
            PwmEvent::LoopsDone => self.pwm.events_loopsdone.write(|w| w),
            PwmEvent::PwmPeriodEnd => self.pwm.events_pwmperiodend.write(|w| w),
            PwmEvent::SeqStarted(seq) => self.pwm.events_seqstarted[usize::from(seq)].write(|w| w),
            PwmEvent::SeqEnd(seq) => self.pwm.events_seqend[usize::from(seq)].write(|w| w),
        }
    }

    /// Returns reference to `Stopped` event endpoint for PPI.
    #[inline(always)]
    pub fn event_stopped(&self) -> &EVENTS_STOPPED {
        &self.pwm.events_stopped
    }

    /// Returns reference to `LoopsDone` event endpoint for PPI.
    #[inline(always)]
    pub fn event_loops_done(&self) -> &EVENTS_LOOPSDONE {
        &self.pwm.events_loopsdone
    }

    /// Returns reference to `PwmPeriodEnd` event endpoint for PPI.
    #[inline(always)]
    pub fn event_pwm_period_end(&self) -> &EVENTS_PWMPERIODEND {
        &self.pwm.events_pwmperiodend
    }

    /// Returns reference to `Seq0 End` event endpoint for PPI.
    #[inline(always)]
    pub fn event_seq0_end(&self) -> &EVENTS_SEQEND {
        &self.pwm.events_seqend[0]
    }

    /// Returns reference to `Seq1 End` event endpoint for PPI.
    #[inline(always)]
    pub fn event_seq1_end(&self) -> &EVENTS_SEQEND {
        &self.pwm.events_seqend[1]
    }

    /// Returns reference to `Seq0 Started` event endpoint for PPI.
    #[inline(always)]
    pub fn event_seq0_started(&self) -> &EVENTS_SEQSTARTED {
        &self.pwm.events_seqstarted[0]
    }

    /// Returns reference to `Seq1 Started` event endpoint for PPI.
    #[inline(always)]
    pub fn event_seq1_started(&self) -> &EVENTS_SEQSTARTED {
        &self.pwm.events_seqstarted[1]
    }

    /// Returns reference to `Seq0 Start` task endpoint for PPI.
    #[inline(always)]
    pub fn task_start_seq0(&self) -> &TASKS_SEQSTART {
        &self.pwm.tasks_seqstart[0]
    }

    /// Returns reference to `Seq1 Started` task endpoint for PPI.
    #[inline(always)]
    pub fn task_start_seq1(&self) -> &TASKS_SEQSTART {
        &self.pwm.tasks_seqstart[1]
    }

    /// Returns reference to `NextStep` task endpoint for PPI.
    #[inline(always)]
    pub fn task_next_step(&self) -> &TASKS_NEXTSTEP {
        &self.pwm.tasks_nextstep
    }

    /// Returns reference to `Stop` task endpoint for PPI.
    #[inline(always)]
    pub fn task_stop(&self) -> &TASKS_STOP {
        &self.pwm.tasks_stop
    }

    /// Returns individual handles to the four PWM channels.
    #[inline(always)]
    pub fn split_channels(&self) -> (PwmChannel<T>, PwmChannel<T>, PwmChannel<T>, PwmChannel<T>) {
        (
            PwmChannel::new(self, Channel::C0),
            PwmChannel::new(self, Channel::C1),
            PwmChannel::new(self, Channel::C2),
            PwmChannel::new(self, Channel::C3),
        )
    }

    /// Returns individual handles to the two PWM groups.
    pub fn split_groups(&self) -> (PwmGroup<T>, PwmGroup<T>) {
        (
            PwmGroup::new(self, Group::G0),
            PwmGroup::new(self, Group::G1),
        )
    }

    /// Consumes `self` and returns back the raw peripheral.
    pub fn free(self) -> (T, Pins) {
        let ch0 = self.pwm.psel.out[0].read();
        let ch1 = self.pwm.psel.out[1].read();
        let ch2 = self.pwm.psel.out[2].read();
        let ch3 = self.pwm.psel.out[3].read();
        self.pwm.psel.out[0].reset();
        self.pwm.psel.out[1].reset();
        self.pwm.psel.out[2].reset();
        self.pwm.psel.out[3].reset();
        (
            self.pwm,
            Pins {
                ch0: if ch0.connect().is_connected() {
                    Some(unsafe { Pin::from_psel_bits(ch0.bits()) })
                } else {
                    None
                },
                ch1: if ch1.connect().is_connected() {
                    Some(unsafe { Pin::from_psel_bits(ch1.bits()) })
                } else {
                    None
                },
                ch2: if ch2.connect().is_connected() {
                    Some(unsafe { Pin::from_psel_bits(ch2.bits()) })
                } else {
                    None
                },
                ch3: if ch3.connect().is_connected() {
                    Some(unsafe { Pin::from_psel_bits(ch3.bits()) })
                } else {
                    None
                },
            },
        )
    }
}

/// Pins for the Pwm
pub struct Pins {
    /// Channel 0 pin, `None` if it was unused
    pub ch0: Option<Pin<Output<PushPull>>>,
    /// Channel 1 pin, `None` if it was unused
    pub ch1: Option<Pin<Output<PushPull>>>,
    /// Channel 2 pin, `None` if it was unused
    pub ch2: Option<Pin<Output<PushPull>>>,
    /// Channel 3 pin, `None` if it was unused
    pub ch3: Option<Pin<Output<PushPull>>>,
}

/// A Pwm sequence wrapper
#[derive(Debug)]
pub struct PwmSeq<T: Instance, B0, B1> {
    inner: Option<Inner<T, B0, B1>>,
}

#[derive(Debug)]
struct Inner<T: Instance, B0, B1> {
    seq0_buffer: Option<B0>,
    seq1_buffer: Option<B1>,
    pwm: Pwm<T>,
}

impl<T: Instance, B0, B1> PwmSeq<T, B0, B1>
where
    B0: ReadBuffer<Word = u16> + 'static,
    B1: ReadBuffer<Word = u16> + 'static,
{
    /// Returns the wrapped contents.
    #[inline(always)]
    pub fn split(mut self) -> (Option<B0>, Option<B1>, Pwm<T>) {
        compiler_fence(Ordering::SeqCst);
        let inner = self
            .inner
            .take()
            .unwrap_or_else(|| unsafe { core::hint::unreachable_unchecked() });
        (inner.seq0_buffer, inner.seq1_buffer, inner.pwm)
    }

    /// Stops PWM generation.
    #[inline(always)]
    pub fn stop(&self) {
        let inner = self
            .inner
            .as_ref()
            .unwrap_or_else(|| unsafe { core::hint::unreachable_unchecked() });
        inner.pwm.stop();
    }

    /// Starts playing the given sequence.
    #[inline(always)]
    pub fn start_seq(&self, seq: Seq) {
        let inner = self
            .inner
            .as_ref()
            .unwrap_or_else(|| unsafe { core::hint::unreachable_unchecked() });
        inner.pwm.start_seq(seq);
    }

    /// Checks if the given event has been triggered.
    #[inline(always)]
    pub fn is_event_triggered(&self, event: PwmEvent) -> bool {
        let inner = self
            .inner
            .as_ref()
            .unwrap_or_else(|| unsafe { core::hint::unreachable_unchecked() });
        inner.pwm.is_event_triggered(event)
    }

    /// Marks the given event as handled.
    #[inline(always)]
    pub fn reset_event(&self, event: PwmEvent) {
        let inner = self
            .inner
            .as_ref()
            .unwrap_or_else(|| unsafe { core::hint::unreachable_unchecked() });
        inner.pwm.reset_event(event)
    }
}

#[cfg(feature = "embedded-hal-02")]
impl<T: Instance> embedded_hal_02::Pwm for Pwm<T> {
    type Channel = Channel;
    type Duty = u16;
    type Time = Hertz;

    fn enable(&mut self, channel: Self::Channel) {
        self.enable_channel(channel);
    }

    fn disable(&mut self, channel: Self::Channel) {
        self.disable_channel(channel);
    }

    fn get_duty(&self, channel: Self::Channel) -> Self::Duty {
        self.duty_on(channel)
    }

    fn set_duty(&mut self, channel: Self::Channel, duty: Self::Duty) {
        self.set_duty_on(channel, duty);
    }

    fn get_max_duty(&self) -> Self::Duty {
        self.max_duty()
    }

    fn get_period(&self) -> Self::Time {
        self.period()
    }

    fn set_period<P>(&mut self, period: P)
    where
        P: Into<Self::Time>,
    {
        Self::set_period(self, period.into());
    }
}

/// PWM channel
#[derive(Debug)]
pub struct PwmChannel<'a, T: Instance> {
    pwm: &'a Pwm<T>,
    channel: Channel,
}

impl<'a, T: Instance> PwmChannel<'a, T> {
    pub fn new(pwm: &'a Pwm<T>, channel: Channel) -> Self {
        Self { pwm, channel }
    }

    pub fn enable(&self) {
        self.pwm.enable_channel(self.channel);
    }

    pub fn disable(&self) {
        self.pwm.disable_channel(self.channel);
    }

    pub fn max_duty(&self) -> u16 {
        self.pwm.max_duty()
    }
    pub fn set_duty(&self, duty: u16) {
        self.pwm.set_duty_on(self.channel, duty);
    }
    pub fn set_duty_on(&self, duty: u16) {
        self.pwm.set_duty_on(self.channel, duty);
    }
    pub fn set_duty_off(&self, duty: u16) {
        self.pwm.set_duty_off(self.channel, duty);
    }
    pub fn duty_on(&self) -> u16 {
        self.pwm.duty_on(self.channel)
    }
    pub fn duty_off(&self) -> u16 {
        self.pwm.duty_off(self.channel)
    }
}

#[cfg(feature = "embedded-hal-02")]
impl<'a, T: Instance> embedded_hal_02::PwmPin for PwmChannel<'a, T> {
    type Duty = u16;

    fn disable(&mut self) {
        Self::disable(self);
    }

    fn enable(&mut self) {
        Self::enable(self);
    }

    fn get_duty(&self) -> Self::Duty {
        self.duty_on()
    }

    fn get_max_duty(&self) -> Self::Duty {
        self.max_duty()
    }

    fn set_duty(&mut self, duty: u16) {
        self.set_duty_on(duty)
    }
}

impl<'a, T: Instance> ErrorType for PwmChannel<'a, T> {
    type Error = Infallible;
}

impl<'a, T: Instance> SetDutyCycle for PwmChannel<'a, T> {
    fn max_duty_cycle(&self) -> u16 {
        self.max_duty()
    }

    fn set_duty_cycle(&mut self, duty: u16) -> Result<(), Self::Error> {
        self.set_duty_on(duty);
        Ok(())
    }
}

/// PWM group
#[derive(Debug)]
pub struct PwmGroup<'a, T: Instance> {
    pwm: &'a Pwm<T>,
    group: Group,
}

impl<'a, T: Instance> PwmGroup<'a, T> {
    pub fn new(pwm: &'a Pwm<T>, group: Group) -> Self {
        Self { pwm, group }
    }

    pub fn enable(&self) {
        self.pwm.enable_group(self.group);
    }

    pub fn disable(&self) {
        self.pwm.disable_group(self.group);
    }
    pub fn max_duty(&self) -> u16 {
        self.pwm.max_duty()
    }
    pub fn set_duty(&self, duty: u16) {
        self.pwm.set_duty_on_group(self.group, duty);
    }
    pub fn set_duty_on(&self, duty: u16) {
        self.pwm.set_duty_on_group(self.group, duty);
    }
    pub fn set_duty_off(&self, duty: u16) {
        self.pwm.set_duty_off_group(self.group, duty);
    }
    pub fn duty_on(&self) -> u16 {
        self.pwm.duty_on_group(self.group)
    }
    pub fn duty_off(&self) -> u16 {
        self.pwm.duty_off_group(self.group)
    }
}

#[cfg(feature = "embedded-hal-02")]
impl<'a, T: Instance> embedded_hal_02::PwmPin for PwmGroup<'a, T> {
    type Duty = u16;

    fn disable(&mut self) {
        Self::disable(self);
    }

    fn enable(&mut self) {
        Self::enable(self);
    }

    fn get_duty(&self) -> Self::Duty {
        self.duty_on()
    }

    fn get_max_duty(&self) -> Self::Duty {
        self.max_duty()
    }

    fn set_duty(&mut self, duty: u16) {
        self.set_duty_on(duty)
    }
}

impl<'a, T: Instance> ErrorType for PwmGroup<'a, T> {
    type Error = Infallible;
}

impl<'a, T: Instance> SetDutyCycle for PwmGroup<'a, T> {
    fn max_duty_cycle(&self) -> u16 {
        self.max_duty()
    }

    fn set_duty_cycle(&mut self, duty: u16) -> Result<(), Self::Error> {
        self.set_duty_on(duty);
        Ok(())
    }
}

#[derive(Debug, Eq, PartialEq, Clone, Copy)]
pub enum Channel {
    C0,
    C1,
    C2,
    C3,
}
impl From<Channel> for usize {
    fn from(variant: Channel) -> Self {
        variant as _
    }
}

#[derive(Debug, Eq, PartialEq, Clone, Copy)]
pub enum Group {
    G0,
    G1,
}
impl From<Group> for usize {
    fn from(variant: Group) -> Self {
        variant as _
    }
}

#[derive(Debug, Eq, PartialEq, Clone, Copy)]
pub enum LoadMode {
    Common,
    Grouped,
    Individual,
    Waveform,
}
impl From<LoadMode> for u8 {
    fn from(variant: LoadMode) -> Self {
        variant as _
    }
}

#[derive(Debug, Eq, PartialEq, Clone, Copy)]
pub enum Seq {
    Seq0,
    Seq1,
}
impl From<Seq> for usize {
    fn from(variant: Seq) -> Self {
        variant as _
    }
}

#[derive(Debug, Eq, PartialEq, Clone, Copy)]
pub enum Prescaler {
    Div1,
    Div2,
    Div4,
    Div8,
    Div16,
    Div32,
    Div64,
    Div128,
}
impl From<Prescaler> for u8 {
    fn from(variant: Prescaler) -> Self {
        variant as _
    }
}

#[derive(Debug, Eq, PartialEq, Clone, Copy)]
pub enum CounterMode {
    Up,
    UpAndDown,
}
impl From<CounterMode> for bool {
    fn from(variant: CounterMode) -> Self {
        match variant {
            CounterMode::Up => false,
            CounterMode::UpAndDown => true,
        }
    }
}

#[derive(Debug, Eq, PartialEq, Clone, Copy)]
pub enum Loop {
    Disabled,
    Times(u16),
    Inf,
}

#[derive(Debug, Eq, PartialEq, Clone, Copy)]
pub enum PwmEvent {
    Stopped,
    LoopsDone,
    PwmPeriodEnd,
    SeqStarted(Seq),
    SeqEnd(Seq),
}

#[derive(Debug, Eq, PartialEq, Clone, Copy)]
pub enum StepMode {
    Auto,
    NextStep,
}
impl From<StepMode> for bool {
    fn from(variant: StepMode) -> Self {
        match variant {
            StepMode::Auto => false,
            StepMode::NextStep => true,
        }
    }
}

#[derive(Debug)]
pub enum Error {
    DMABufferNotInDataMemory,
    BufferTooLong,
}

pub trait Instance: sealed::Sealed + Deref<Target = RegisterBlock> {
    const INTERRUPT: Interrupt;

    /// Provides access to the associated internal duty buffer for the instance.
    fn buffer() -> *mut [u16; 4];
}

// Internal static duty buffers. One per instance.
static mut BUF0: [u16; 4] = [0; 4];
#[cfg(not(any(feature = "52810", feature = "52811")))]
static mut BUF1: [u16; 4] = [0; 4];
#[cfg(not(any(feature = "52810", feature = "52811")))]
static mut BUF2: [u16; 4] = [0; 4];
#[cfg(not(any(feature = "52810", feature = "52811", feature = "52832")))]
static mut BUF3: [u16; 4] = [0; 4];

#[cfg(not(any(feature = "9160", feature = "5340-app")))]
impl Instance for crate::pac::PWM0 {
    const INTERRUPT: Interrupt = Interrupt::PWM0;
    #[inline(always)]
    fn buffer() -> *mut [u16; 4] {
        unsafe { addr_of_mut!(BUF0) }
    }
}

#[cfg(not(any(
    feature = "52810",
    feature = "52811",
    feature = "9160",
    feature = "5340-app"
)))]
impl Instance for crate::pac::PWM1 {
    const INTERRUPT: Interrupt = Interrupt::PWM1;
    fn buffer() -> *mut [u16; 4] {
        unsafe { addr_of_mut!(BUF1) }
    }
}

#[cfg(not(any(
    feature = "52810",
    feature = "52811",
    feature = "9160",
    feature = "5340-app"
)))]
impl Instance for crate::pac::PWM2 {
    const INTERRUPT: Interrupt = Interrupt::PWM2;
    fn buffer() -> *mut [u16; 4] {
        unsafe { addr_of_mut!(BUF2) }
    }
}

#[cfg(not(any(
    feature = "52810",
    feature = "52811",
    feature = "52832",
    feature = "5340-app",
    feature = "9160"
)))]
impl Instance for crate::pac::PWM3 {
    const INTERRUPT: Interrupt = Interrupt::PWM3;
    fn buffer() -> *mut [u16; 4] {
        unsafe { addr_of_mut!(BUF3) }
    }
}

#[cfg(any(feature = "9160", feature = "5340-app"))]
impl Instance for crate::pac::PWM0_NS {
    const INTERRUPT: Interrupt = Interrupt::PWM0;
    #[inline(always)]
    fn buffer() -> *mut [u16; 4] {
        unsafe { addr_of_mut!(BUF0) }
    }
}

#[cfg(any(feature = "9160", feature = "5340-app"))]
impl Instance for crate::pac::PWM1_NS {
    const INTERRUPT: Interrupt = Interrupt::PWM1;
    fn buffer() -> *mut [u16; 4] {
        unsafe { addr_of_mut!(BUF1) }
    }
}

#[cfg(any(feature = "9160", feature = "5340-app"))]
impl Instance for crate::pac::PWM2_NS {
    const INTERRUPT: Interrupt = Interrupt::PWM2;
    fn buffer() -> *mut [u16; 4] {
        unsafe { addr_of_mut!(BUF2) }
    }
}

#[cfg(any(feature = "9160", feature = "5340-app"))]
impl Instance for crate::pac::PWM3_NS {
    const INTERRUPT: Interrupt = Interrupt::PWM3;
    fn buffer() -> *mut [u16; 4] {
        unsafe { addr_of_mut!(BUF3) }
    }
}
mod sealed {
    pub trait Sealed {}

    #[cfg(not(any(feature = "5340-app", feature = "9160")))]
    impl Sealed for crate::pac::PWM0 {}

    #[cfg(not(any(
        feature = "52810",
        feature = "52811",
        feature = "5340-app",
        feature = "9160"
    )))]
    impl Sealed for crate::pac::PWM1 {}

    #[cfg(not(any(
        feature = "52810",
        feature = "52811",
        feature = "5340-app",
        feature = "9160"
    )))]
    impl Sealed for crate::pac::PWM2 {}

    #[cfg(not(any(
        feature = "52810",
        feature = "52811",
        feature = "52832",
        feature = "5340-app",
        feature = "9160"
    )))]
    impl Sealed for crate::pac::PWM3 {}

    #[cfg(any(feature = "9160", feature = "5340-app"))]
    impl Sealed for crate::pac::PWM0_NS {}

    #[cfg(any(feature = "9160", feature = "5340-app"))]
    impl Sealed for crate::pac::PWM1_NS {}

    #[cfg(any(feature = "9160", feature = "5340-app"))]
    impl Sealed for crate::pac::PWM2_NS {}

    #[cfg(any(feature = "9160", feature = "5340-app"))]
    impl Sealed for crate::pac::PWM3_NS {}
}