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
use super::{
hal, pac, pac::MCLK, pac::SERCOM0, pac::SERCOM1, pac::SERCOM4, pac::SERCOM5, pac::SERCOM6,
pac::SERCOM7,
};
use hal::define_pins;
use hal::gpio::{self, *};
use hal::sercom::{
I2CMaster6, PadPin, SPIMaster7, Sercom0Pad0, Sercom0Pad1, Sercom1Pad0, Sercom1Pad1,
Sercom4Pad0, Sercom4Pad1, Sercom5Pad0, Sercom5Pad1, Sercom6Pad0, Sercom6Pad1, UART0, UART1,
UART4, UART5,
};
use hal::time::Hertz;
use embedded_hal::{digital::v1_compat::OldOutputPin, timer::CountDown, timer::Periodic};
use ws2812_timer_delay as ws2812;
use hal::clock::GenericClockController;
#[cfg(feature = "usb")]
use super::pac::gclk::{genctrl::SRC_A, pchctrl::GEN_A};
#[cfg(feature = "usb")]
use hal::usb::usb_device::bus::UsbBusAllocator;
#[cfg(feature = "usb")]
pub use hal::usb::UsbBus;
define_pins!(
struct Pins,
pac: pac,
pin tx_led = c30,
pin rx_led = c31,
pin a0 = a2,
pin a1 = a5,
pin a2 = b3,
pin a3 = c0,
pin a4 = c1,
pin a5 = c2,
pin a6 = c3,
pin a7 = b4,
pin a8 = b5,
pin a9 = b6,
pin a10 = b7,
pin a11 = b8,
pin a12 = b9,
pin a13 = a4,
pin a14 = a6,
pin a15 = a7,
pin uart0_rx = b25,
pin uart0_tx = b24,
pin d2 = c18,
pin d3 = c19,
pin d4 = c20,
pin d5 = c21,
pin d6 = d20,
pin d7 = d21,
pin d8 = b18,
pin d9 = b2,
pin d10 = b22,
pin d11 = b23,
pin d12 = b0,
pin red_led = b1,
pin uart3_tx = b16,
pin uart3_rx = b17,
pin uart2_tx = c22,
pin uart2_rx = c23,
pin uart1_tx = b12,
pin uart1_rx = b13,
pin sda = c16,
pin scl = c17,
pin d22 = d12,
pin d23 = a15,
pin usb_dm = a24,
pin usb_dp = a25,
pin d26 = a12,
pin d27 = a13,
pin d28 = a14,
pin d29 = b19,
pin d30 = a23,
pin d31 = a22,
pin d32 = a21,
pin d33 = a20,
pin d34 = a19,
pin d35 = a18,
pin d36 = a17,
pin d37 = a16,
pin d38 = b15,
pin d39 = b14,
pin d40 = c13,
pin d41 = c12,
pin d42 = c15,
pin d43 = c14,
pin d44 = c11,
pin d45 = c10,
pin d46 = c6,
pin d47 = c7,
pin d48 = c4,
pin d49 = c5,
pin miso = d11,
pin mosi = d8,
pin sck = d9,
pin ss = d10,
pin flash_sck = b10,
pin flash_io0 = a8,
pin flash_io1 = a9,
pin flash_io2 = a10,
pin flash_io3 = a11,
pin flash_cs = b11,
pin neopixel = c24,
pin sd_mosi = b26,
pin sd_sck = b27,
pin sd_cs = b28,
pin sd_miso = b29,
pin swo = b30,
pin sd_cd = b31,
pin aref = a3,
);
impl Pins {
pub fn split(self) -> Sets {
let analog = Analog {
a0: self.a0,
a1: self.a1,
a2: self.a2,
a3: self.a3,
a4: self.a4,
a5: self.a5,
a6: self.a6,
a7: self.a7,
a8: self.a8,
a9: self.a9,
a10: self.a10,
a11: self.a11,
a12: self.a12,
a13: self.a13,
a14: self.a14,
a15: self.a15,
};
let flash = QSPIFlash {
sck: self.flash_sck,
cs: self.flash_cs,
data0: self.flash_io0,
data1: self.flash_io1,
data2: self.flash_io2,
data3: self.flash_io3,
};
let spi = SPI {
sck: self.sck,
mosi: self.mosi,
miso: self.miso,
};
let sdcard = SdCard {
cs: self.sd_cs,
cd: self.sd_cd,
};
let i2c = I2C {
sda: self.sda,
scl: self.scl,
};
let usb = USB {
dm: self.usb_dm,
dp: self.usb_dp,
};
let uart0 = UART0_ {
rx: self.uart0_rx,
tx: self.uart0_tx,
};
let uart1 = UART1_ {
rx: self.uart1_rx,
tx: self.uart1_tx,
};
let uart2 = UART2_ {
rx: self.uart2_rx,
tx: self.uart2_tx,
};
let uart3 = UART3_ {
rx: self.uart3_rx,
tx: self.uart3_tx,
};
let neopixel = Neopixel {
neopixel: self.neopixel,
};
Sets {
port: self.port,
analog,
spi,
usb,
flash,
sdcard,
i2c,
uart0,
uart1,
uart2,
uart3,
tx_led: self.tx_led,
rx_led: self.rx_led,
neopixel,
}
}
}
pub struct Sets {
pub tx_led: Pc30<Input<Floating>>,
pub rx_led: Pc31<Input<Floating>>,
pub analog: Analog,
pub spi: SPI,
pub sdcard: SdCard,
pub i2c: I2C,
pub flash: QSPIFlash,
pub neopixel: Neopixel,
pub usb: USB,
pub uart0: UART0_,
pub uart1: UART1_,
pub uart2: UART2_,
pub uart3: UART3_,
pub port: Port,
}
pub struct SPI {
pub mosi: gpio::Pd8<Input<Floating>>,
pub miso: gpio::Pd11<Input<Floating>>,
pub sck: gpio::Pd9<Input<Floating>>,
}
impl SPI {
pub fn init<F: Into<Hertz>>(
self,
clocks: &mut GenericClockController,
bus_speed: F,
sercom7: SERCOM7,
mclk: &mut MCLK,
port: &mut Port,
) -> SPIMaster7<
hal::sercom::Sercom7Pad3<gpio::Pd11<gpio::PfC>>,
hal::sercom::Sercom7Pad0<gpio::Pd8<gpio::PfC>>,
hal::sercom::Sercom7Pad1<gpio::Pd9<gpio::PfC>>,
> {
let gclk0 = clocks.gclk0();
SPIMaster7::new(
&clocks.sercom7_core(&gclk0).unwrap(),
bus_speed.into(),
hal::hal::spi::Mode {
phase: hal::hal::spi::Phase::CaptureOnFirstTransition,
polarity: hal::hal::spi::Polarity::IdleLow,
},
sercom7,
mclk,
(
self.miso.into_pad(port),
self.mosi.into_pad(port),
self.sck.into_pad(port),
),
)
}
}
pub struct I2C {
pub sda: Pc16<Input<Floating>>,
pub scl: Pc17<Input<Floating>>,
}
impl I2C {
pub fn init<F: Into<Hertz>>(
self,
clocks: &mut GenericClockController,
bus_speed: F,
sercom3: SERCOM6,
mclk: &mut MCLK,
port: &mut Port,
) -> I2CMaster6<Sercom6Pad0<Pc16<PfC>>, Sercom6Pad1<Pc17<PfC>>> {
let gclk0 = clocks.gclk0();
I2CMaster6::new(
&clocks.sercom6_core(&gclk0).unwrap(),
bus_speed.into(),
sercom3,
mclk,
self.sda.into_pad(port),
self.scl.into_pad(port),
)
}
}
pub struct SdCard {
pub cs: Pb28<Input<Floating>>,
pub cd: Pb31<Input<Floating>>,
}
pub struct USB {
pub dm: Pa24<Input<Floating>>,
pub dp: Pa25<Input<Floating>>,
}
impl USB {
#[cfg(feature = "usb")]
pub fn init(
self,
usb: super::pac::USB,
clocks: &mut GenericClockController,
mclk: &mut MCLK,
) -> UsbBusAllocator<UsbBus> {
clocks.configure_gclk_divider_and_source(GEN_A::GCLK2, 1, SRC_A::DFLL, false);
let usb_gclk = clocks.get_gclk(GEN_A::GCLK2).unwrap();
let usb_clock = &clocks.usb(&usb_gclk).unwrap();
UsbBusAllocator::new(UsbBus::new(usb_clock, mclk, self.dm, self.dp, usb))
}
}
pub struct UART0_ {
pub tx: Pb24<Input<Floating>>,
pub rx: Pb25<Input<Floating>>,
}
impl UART0_ {
pub fn init<F: Into<Hertz>>(
self,
clocks: &mut GenericClockController,
baud: F,
sercom0: SERCOM0,
mclk: &mut MCLK,
port: &mut Port,
) -> UART0<Sercom0Pad1<gpio::Pb25<gpio::PfC>>, Sercom0Pad0<gpio::Pb24<gpio::PfC>>, (), ()> {
let gclk0 = clocks.gclk0();
UART0::new(
&clocks.sercom0_core(&gclk0).unwrap(),
baud.into(),
sercom0,
mclk,
(self.rx.into_pad(port), self.tx.into_pad(port)),
)
}
}
pub struct UART1_ {
pub tx: Pb12<Input<Floating>>,
pub rx: Pb13<Input<Floating>>,
}
impl UART1_ {
pub fn init<F: Into<Hertz>>(
self,
clocks: &mut GenericClockController,
baud: F,
sercom4: SERCOM4,
mclk: &mut MCLK,
port: &mut Port,
) -> UART4<Sercom4Pad1<gpio::Pb13<gpio::PfC>>, Sercom4Pad0<gpio::Pb12<gpio::PfC>>, (), ()> {
let gclk0 = clocks.gclk0();
UART4::new(
&clocks.sercom4_core(&gclk0).unwrap(),
baud.into(),
sercom4,
mclk,
(self.rx.into_pad(port), self.tx.into_pad(port)),
)
}
}
pub struct UART2_ {
pub tx: Pc22<Input<Floating>>,
pub rx: Pc23<Input<Floating>>,
}
impl UART2_ {
pub fn init<F: Into<Hertz>>(
self,
clocks: &mut GenericClockController,
baud: F,
sercom1: SERCOM1,
mclk: &mut MCLK,
port: &mut Port,
) -> UART1<Sercom1Pad1<gpio::Pc23<gpio::PfC>>, Sercom1Pad0<gpio::Pc22<gpio::PfC>>, (), ()> {
let gclk0 = clocks.gclk0();
UART1::new(
&clocks.sercom1_core(&gclk0).unwrap(),
baud.into(),
sercom1,
mclk,
(self.rx.into_pad(port), self.tx.into_pad(port)),
)
}
}
pub struct UART3_ {
pub tx: Pb16<Input<Floating>>,
pub rx: Pb17<Input<Floating>>,
}
impl UART3_ {
pub fn init<F: Into<Hertz>>(
self,
clocks: &mut GenericClockController,
baud: F,
sercom5: SERCOM5,
mclk: &mut MCLK,
port: &mut Port,
) -> UART5<Sercom5Pad1<gpio::Pb17<gpio::PfC>>, Sercom5Pad0<gpio::Pb16<gpio::PfC>>, (), ()> {
let gclk0 = clocks.gclk0();
UART5::new(
&clocks.sercom5_core(&gclk0).unwrap(),
baud.into(),
sercom5,
mclk,
(self.rx.into_pad(port), self.tx.into_pad(port)),
)
}
}
pub struct Analog {
pub a0: Pa2<Input<Floating>>,
pub a1: Pa5<Input<Floating>>,
pub a2: Pb3<Input<Floating>>,
pub a3: Pc0<Input<Floating>>,
pub a4: Pc1<Input<Floating>>,
pub a5: Pc2<Input<Floating>>,
pub a6: Pc3<Input<Floating>>,
pub a7: Pb4<Input<Floating>>,
pub a8: Pb5<Input<Floating>>,
pub a9: Pb6<Input<Floating>>,
pub a10: Pb7<Input<Floating>>,
pub a11: Pb8<Input<Floating>>,
pub a12: Pb9<Input<Floating>>,
pub a13: Pa4<Input<Floating>>,
pub a14: Pa6<Input<Floating>>,
pub a15: Pa7<Input<Floating>>,
}
pub struct QSPIFlash {
pub sck: Pb10<Input<Floating>>,
pub cs: Pb11<Input<Floating>>,
pub data0: Pa8<Input<Floating>>,
pub data1: Pa9<Input<Floating>>,
pub data2: Pa10<Input<Floating>>,
pub data3: Pa11<Input<Floating>>,
}
pub struct Neopixel {
pub neopixel: Pc24<Input<Floating>>,
}
impl Neopixel {
pub fn init<T: CountDown + Periodic>(
self,
timer: T,
port: &mut Port,
) -> ws2812::Ws2812<
T,
embedded_hal::digital::v1_compat::OldOutputPin<
atsamd_hal::common::gpio::Pc24<
atsamd_hal::common::gpio::Output<atsamd_hal::common::gpio::PushPull>,
>,
>,
> {
let neopixel_pin: OldOutputPin<_> = self.neopixel.into_push_pull_output(port).into();
ws2812::Ws2812::new(timer, neopixel_pin)
}
}