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
use super::{hal, pac, pac::MCLK, pac::SERCOM2, pac::SERCOM6, pac::SERCOM7};
use hal::define_pins;
use hal::gpio::{self, *};
use hal::sercom::{
I2CMaster7, PadPin, SPIMaster6, Sercom2Pad0, Sercom2Pad1, Sercom6Pad0, Sercom6Pad1,
Sercom6Pad3, Sercom7Pad0, Sercom7Pad1, UART2,
};
use hal::time::Hertz;
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 sw0 = b31,
pin led = c18,
pin qt_button = a16,
pin tx = b25,
pin rx = b24,
pin sda = d8,
pin scl = d9,
pin mosi = c4,
pin miso = c7,
pin sck = c5,
pin pb04 = b4,
pin pb05 = b5,
pin pa06 = a6,
pin pa07 = a7,
pin pb08 = b8,
pin pb09 = b9,
pin pb07 = b7,
pin pa27 = a27,
pin uart0_rx = a5,
pin uart0_tx = a4,
pin spi4_mosi = b27,
pin spi4_sck = b26,
pin pb00 = b0,
pin pa03 = a3,
pin pb01 = b1,
pin pb06 = b6,
pin pb14 = b14,
pin pb15 = b15,
pin pd00 = d0,
pin pb02 = b2,
pin spi6_ss = c6,
pin pc02 = c2,
pin pc03 = c3,
pin pc01 = c1,
pin pc10 = c10,
pin pd10 = d10,
pin pd11 = d11,
pin pc30 = c30,
pin pc31 = c31,
pin uart1_rx = c23,
pin uart1_tx = c22,
pin pc14 = c14,
pin sd_d0 = b18,
pin sd_d1 = b19,
pin sd_d2 = b20,
pin sd_d3 = b21,
pin sd_clk_i2s_sdo = a21,
pin sd_cmd_i2s_fs0 = a20,
pin sd_cd = d20,
pin sd_wp = d21,
pin pdec_phase_a = c16,
pin pdec_phase_b = c17,
pin vbat = b3,
pin adc_dac = a2,
pin vbus_detecion = c0,
pin usb_id = c19,
pin usb_dm = a24,
pin usb_dp = a25,
pin swd = a31,
pin swc = a30,
pin swo = b30,
pin can_tx = b12,
pin can_rx = b13,
pin can_standby = c13,
pin gtxck = a14,
pin gtxen = a17,
pin gtx0 = a18,
pin gtx1 = a19,
pin grxdv = c20,
pin grx0 = a13,
pin grx1 = a12,
pin grxer = a15,
pin gmdc = c11,
pin gmdio = c12,
pin ethernet_interrupt = d12,
pin ethernet_reset = c21,
pin flash_io0 = a8,
pin flash_io1 = a9,
pin flash_io2 = a10,
pin flash_io3 = a11,
pin flash_sck = b10,
pin flash_cs = b11,
pin i2s_sck0 = b16,
pin i2s_mck0 = b17,
pin i2s_sdi = a22,
pin i2s_fs1 = a23,
pin i2s_sck1 = b28,
pin i2s_mck1 = b29,
pin dgi_ss = d1,
);
impl Pins {
pub fn split(self) -> Sets {
let analog = Analog {
};
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 {
d0: self.sd_d0,
d1: self.sd_d1,
d2: self.sd_d2,
d3: self.sd_d3,
clk: self.sd_clk_i2s_sdo,
cmd: self.sd_cmd_i2s_fs0,
cd: self.sd_cd,
wp: self.sd_wp,
};
let i2c = I2C {
sda: self.sda,
scl: self.scl,
};
let usb = USB {
dm: self.usb_dm,
dp: self.usb_dp,
};
let uart = UART_ {
rx: self.rx,
tx: self.tx,
};
Sets {
port: self.port,
analog,
spi,
usb,
flash,
sdcard,
i2c,
uart,
led: self.led,
sw0: self.sw0,
}
}
}
pub struct Sets {
pub led: Pc18<Input<Floating>>,
pub sw0: Pb31<Input<Floating>>,
pub analog: Analog,
pub spi: SPI,
pub sdcard: SdCard,
pub i2c: I2C,
pub flash: QSPIFlash,
pub usb: USB,
pub uart: UART_,
pub port: Port,
}
pub struct SPI {
pub mosi: gpio::Pc4<Input<Floating>>,
pub miso: gpio::Pc7<Input<Floating>>,
pub sck: gpio::Pc5<Input<Floating>>,
}
impl SPI {
#[allow(clippy::clippy::type_complexity)]
pub fn init<F: Into<Hertz>>(
self,
clocks: &mut GenericClockController,
bus_speed: F,
sercom6: SERCOM6,
mclk: &mut MCLK,
port: &mut Port,
) -> SPIMaster6<
Sercom6Pad3<gpio::Pc7<gpio::PfC>>,
Sercom6Pad0<gpio::Pc4<gpio::PfC>>,
Sercom6Pad1<gpio::Pc5<gpio::PfC>>,
> {
let gclk0 = clocks.gclk0();
SPIMaster6::new(
&clocks.sercom6_core(&gclk0).unwrap(),
bus_speed.into(),
hal::hal::spi::Mode {
phase: hal::hal::spi::Phase::CaptureOnFirstTransition,
polarity: hal::hal::spi::Polarity::IdleLow,
},
sercom6,
mclk,
(
self.miso.into_pad(port),
self.mosi.into_pad(port),
self.sck.into_pad(port),
),
)
}
}
pub struct I2C {
pub sda: Pd8<Input<Floating>>,
pub scl: Pd9<Input<Floating>>,
}
impl I2C {
pub fn init<F: Into<Hertz>>(
self,
clocks: &mut GenericClockController,
bus_speed: F,
sercom7: SERCOM7,
mclk: &mut MCLK,
port: &mut Port,
) -> I2CMaster7<Sercom7Pad0<Pd8<PfC>>, Sercom7Pad1<Pd9<PfC>>> {
let gclk0 = clocks.gclk0();
I2CMaster7::new(
&clocks.sercom7_core(&gclk0).unwrap(),
bus_speed.into(),
sercom7,
mclk,
self.sda.into_pad(port),
self.scl.into_pad(port),
)
}
}
pub struct SdCard {
pub d0: Pb18<Input<Floating>>,
pub d1: Pb19<Input<Floating>>,
pub d2: Pb20<Input<Floating>>,
pub d3: Pb21<Input<Floating>>,
pub clk: Pa21<Input<Floating>>,
pub cmd: Pa20<Input<Floating>>,
pub cd: Pd20<Input<Floating>>,
pub wp: Pd21<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 UART_ {
pub tx: Pb25<Input<Floating>>,
pub rx: Pb24<Input<Floating>>,
}
impl UART_ {
pub fn init<F: Into<Hertz>>(
self,
clocks: &mut GenericClockController,
baud: F,
sercom2: SERCOM2,
mclk: &mut MCLK,
port: &mut Port,
) -> UART2<Sercom2Pad1<gpio::Pb24<gpio::PfD>>, Sercom2Pad0<gpio::Pb25<gpio::PfD>>, (), ()> {
let gclk0 = clocks.gclk0();
UART2::new(
&clocks.sercom2_core(&gclk0).unwrap(),
baud.into(),
sercom2,
mclk,
(self.rx.into_pad(port), self.tx.into_pad(port)),
)
}
}
pub struct Analog {
}
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>>,
}