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
#![no_std]
extern crate atsamd21_hal as hal;
#[cfg(feature = "rt")]
extern crate cortex_m_rt;
#[cfg(feature = "rt")]
pub use cortex_m_rt::entry;
pub use hal::atsamd21g18a::*;
use hal::prelude::*;
pub use hal::*;
use gpio::{Floating, Input, Port};
use hal::clock::GenericClockController;
use hal::sercom::{I2CMaster3, PadPin, SPIMaster0};
use hal::time::Hertz;
#[cfg(feature = "usb")]
pub use hal::usb::UsbBus;
#[cfg(feature = "usb")]
use usb_device::bus::UsbBusWrapper;
define_pins!(
struct Pins,
target_device: atsamd21g18a,
pin d0 = a2,
pin d1 = a3,
pin d2 = b8,
pin d3 = b9,
pin accel_int1 = a21,
pin accel_int2 = a20,
pin d6 = a6,
pin d7 = a7,
pin d8 = a8,
pin d9 = a9,
pin d10 = a10,
pin d11 = a11,
pin d12 = b2,
pin d13 = b3,
pin led_red = a15,
pin led_green = b10,
pin led_blue = b11,
pin gps_timepulse = a14,
pin gps_enable = a18,
pin button = a16,
pin enable_pin_io = b22,
pin switch_sense = a17,
pin bat_volt = a5,
pin usb_dm = a24,
pin usb_dp = a25,
pin serial1_rx = a13,
pin serial1_tx = a12,
pin i2c_sda = a22,
pin i2c_scl = a23,
pin lora_reset = a4,
pin mag_int = a19,
);
pub fn spi_master<F: Into<Hertz>>(
clocks: &mut GenericClockController,
bus_speed: F,
sercom0: SERCOM0,
pm: &mut PM,
sck: gpio::Pa11<Input<Floating>>,
mosi: gpio::Pa10<Input<Floating>>,
miso: gpio::Pa8<Input<Floating>>,
port: &mut Port,
) -> SPIMaster0 {
let gclk0 = clocks.gclk0();
SPIMaster0::new(
&clocks.sercom0_core(&gclk0).unwrap(),
bus_speed.into(),
hal::hal::spi::Mode {
phase: hal::hal::spi::Phase::CaptureOnFirstTransition,
polarity: hal::hal::spi::Polarity::IdleLow,
},
sercom0,
pm,
hal::sercom::SPI0Pinout::Dipo0Dopo1 {
miso: miso.into_pad(port),
mosi: mosi.into_pad(port),
sck: sck.into_pad(port),
},
)
}
pub fn i2c_master<F: Into<Hertz>>(
clocks: &mut GenericClockController,
bus_speed: F,
sercom3: SERCOM3,
pm: &mut PM,
sda: gpio::Pa22<Input<Floating>>,
scl: gpio::Pa23<Input<Floating>>,
port: &mut Port,
) -> I2CMaster3 {
let gclk0 = clocks.gclk0();
I2CMaster3::new(
&clocks.sercom3_core(&gclk0).unwrap(),
bus_speed.into(),
sercom3,
pm,
sda.into_pad(port),
scl.into_pad(port),
)
}
#[cfg(feature = "usb")]
pub fn usb_bus(
usb: USB,
clocks: &mut GenericClockController,
pm: &mut PM,
dm: gpio::Pa24<Input<Floating>>,
dp: gpio::Pa25<Input<Floating>>,
port: &mut Port,
) -> UsbBusWrapper<UsbBus> {
let gclk0 = clocks.gclk0();
dbgprint!("making usb clock");
let usb_clock = &clocks.usb(&gclk0).unwrap();
dbgprint!("got clock");
UsbBusWrapper::new(UsbBus::new(
usb_clock,
pm,
dm.into_function(port),
dp.into_function(port),
usb,
))
}