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
#![no_std]

#[cfg(feature = "rt")]
pub use cortex_m_rt::entry;

pub use atsamd_hal as hal;
pub use hal::ehal;
pub use hal::pac;

use hal::clock::GenericClockController;
use hal::sercom::v2::{spi, uart, Sercom1, Sercom2, Sercom5};
use hal::sercom::I2CMaster4;
use hal::time::Hertz;

#[cfg(feature = "usb")]
use hal::usb::{usb_device::bus::UsbBusAllocator, UsbBus};

// The docs could be further improved with details of the specific channels etc
// Maps the pins to their arduino names and the numbers printed on the board.
// Information from: <https://github.com/arduino/ArduinoCore-samd/blob/master/variants/nano_33_iot/variant.cpp>
hal::bsp_pins!(
    PB23 {
        /// RX
        name: rx
        aliases: {
            AlternateD: Rx
        }
    }
    PB22 {
        /// TX
        name: tx
        aliases: {
            AlternateD: Tx
        }
    }
    PB10 {
        /// Digital 2: PWM, TC
        name: d2
    }
    PB11 {
        /// Digital 3: PWM, TC
        name: d3
    }
    PA07 {
        /// Digital 4: TCC
        name: d4
    }
    PA05 {
        /// Digital 5: PWM, TCC, ADC
        name: d5
    }
    PA04 {
        /// Digital 6: PWM, TCC, ADC
        name: d6
    }
    PA06 {
        /// Digital 7: ADC
        name: d7
    }
    PA18 {
        /// Digital 8
        name: d8
    }
    PA20 {
        /// Digital 9: PWM, TCC
        name: d9
    }
    PA21 {
        /// Digital 10: PWM, TCC
        name: d10
    }
    PA19 {
        /// Digital 11/SCI MISO: PWM, TCC
        name: miso
        aliases: {
            AlternateC: Miso
        }
    }
    PA16 {
        /// Digital 12/SCI MOSI: PWM, TCC
        name: mosi
        aliases: {
            AlternateC: Mosi
        }
    }
    PA17 {
        /// Digital 13/LED/SPI SCK: ON-BOARD-LED
        name: led_sck
        aliases: {
            PushPullOutput: Led,
            AlternateC: Sck
        }
    }
    PA02 {
        /// Analog 0: DAC
        name: a0
    }
    PB02 {
        /// Analog 1
        name: a1
    }
    PA11 {
        /// Analog 2: PWM, TCC
        name: a2
    }
    PA10 {
        /// Analog 3: PWM, TCC
        name: a3
    }
    PB08 {
        /// Analog 4/SDA
        name: sda
        aliases: {
            AlternateD: Sda
        }
    }
    PB09 {
        /// Analog 5/SCL: PWM< TCC
        name: scl
        aliases: {
            AlternateD: Scl
        }
    }
    PA09 {
        /// Analog 6
        name: a6
    }
    PB03 {
        /// Analog 7
        name: a7
    }
    PA03 {
        /// AREF
        name: aref
    }
    PA12 {
        /// SPI (Lefacy ICSP) 1 / NINA MOSI
        name: nina_mosi
        aliases: {
            AlternateC: NinaMosi
        }
    }
    PA13 {
        /// SPI (Lefacy ICSP) 2 / NINA MISO
        name: nina_miso
        aliases: {
            AlternateC: NinaMiso
        }
    }
    PA14 {
        /// SPI (Lefacy ICSP) 3 / NINA CS
        name: nina_cs
        aliases: {
            AlternateC: NinaCs
        }
    }
    PA15 {
        /// SPI (Lefacy ICSP) 4 / NINA SCK
        name: nina_sck
        aliases: {
            AlternateC: NinaSck
        }
    }
    PA27 {
        /// NINA GPIO0
        name: nina_gpio0
    }
    PA08 {
        /// NINA RESET_N
        name: nina_resetn
        aliases: {
            PushPullOutput: NinaResetN
        }
    }
    PA28 {
        /// NINA ACK
        name: nina_ack
        aliases: {
            FloatingInput: NinaAck
        }
    }
    PA22 {
        /// SerialNina 29: PWM, TC
        name: serial_nina29
    }
    PA23 {
        /// SerialNina 30: PWM, TC
        name: serial_nina30
    }
    PA24 {
        /// USB D- Pad
        name: usb_dm
        aliases: {
            AlternateG: UsbDm
        }
    }
    PA25 {
        /// USB D+ Pad
        name: usb_dp
        aliases: {
            AlternateG: UsbDp
        }
    }
    PA30 {
        /// SWCLK
        name: p34
    }
    PA31 {
        /// SWDIO
        name: p35
    }
);

#[cfg(feature = "usb")]
pub fn usb_allocator(
    usb: pac::USB,
    clocks: &mut GenericClockController,
    pm: &mut pac::PM,
    dm: impl Into<UsbDm>,
    dp: impl Into<UsbDp>,
) -> UsbBusAllocator<UsbBus> {
    let gclk0 = clocks.gclk0();
    let usb_clock = &clocks.usb(&gclk0).unwrap();
    let (dm, dp) = (dm.into(), dp.into());
    UsbBusAllocator::new(UsbBus::new(usb_clock, pm, dm, dp, usb))
}

/// Convenience for setting up the labelled SDA, SCL pins to
/// operate as an I2C master running at the specified frequency.
pub fn i2c_master(
    clocks: &mut GenericClockController,
    bus_speed: impl Into<Hertz>,
    sercom4: pac::SERCOM4,
    pm: &mut pac::PM,
    sda: impl Into<Sda>,
    scl: impl Into<Scl>,
) -> I2CMaster4<Sda, Scl> {
    let gclk0 = &clocks.gclk0();
    let clock = &clocks.sercom4_core(&gclk0).unwrap();
    let (bus_speed, sda, scl) = (bus_speed.into(), sda.into(), scl.into());

    I2CMaster4::new(clock, bus_speed, sercom4, pm, sda, scl)
}

/// UART pads
pub type UartPads = uart::Pads<Sercom5, Rx, Tx>;
/// UART device for the labelled RX & TX pins
pub type Uart = uart::Uart<uart::Config<UartPads>, uart::Duplex>;

/// Convenience for setting up the labelled RX, TX pins to
/// operate as a UART device running at the specified baud.
pub fn uart(
    clocks: &mut GenericClockController,
    baud: impl Into<Hertz>,
    sercom5: pac::SERCOM5,
    pm: &mut pac::PM,
    rx: impl Into<Rx>,
    tx: impl Into<Tx>,
) -> Uart {
    let gclk0 = clocks.gclk0();
    let clock = &clocks.sercom5_core(&gclk0).unwrap();
    let baud = baud.into();
    let pads = uart::Pads::default().rx(rx.into()).tx(tx.into());

    uart::Config::new(pm, sercom5, pads, clock.freq())
        .baud(baud, uart::BaudMode::Fractional(uart::Oversampling::Bits16))
        .enable()
}

// SPI pads for the labelled SPI peripheral
///
/// You can use these pads with other, user-defined [`spi::Config`]urations.
pub type SpiPads = spi::Pads<Sercom1, Miso, Mosi, Sck>;

/// SPI master for the labelled SPI peripheral
///
/// This type implements [`FullDuplex<u8>`](ehal::spi::FullDuplex).
pub type Spi = spi::Spi<spi::Config<SpiPads>, spi::Duplex>;

/// Convenience for setting up the labelled SPI peripheral.
/// This powers up SERCOM1 and configures it for use as an
/// SPI Master in SPI Mode 0.
pub fn spi_master(
    clocks: &mut GenericClockController,
    baud: impl Into<Hertz>,
    sercom1: pac::SERCOM1,
    pm: &mut pac::PM,
    sck: impl Into<Sck>,
    mosi: impl Into<Mosi>,
    miso: impl Into<Miso>,
) -> Spi {
    let gclk0 = clocks.gclk0();
    let clock = clocks.sercom1_core(&gclk0).unwrap();
    let (miso, mosi, sclk) = (miso.into(), mosi.into(), sck.into());
    let pads = spi::Pads::default().data_in(miso).data_out(mosi).sclk(sclk);

    spi::Config::new(pm, sercom1, pads, clock.freq())
        .baud(baud)
        .spi_mode(spi::MODE_0)
        .enable()
}

// SPI pads for the Nina periphral
///
/// You can use these pads with other, user-defined [`spi::Config`]urations.
pub type NinaSpiPads = spi::Pads<Sercom2, NinaMiso, NinaMosi, NinaSck>;

/// SPI master for the Nina peripheral
///
/// This type implements [`FullDuplex<u8>`](ehal::spi::FullDuplex).
pub type NinaSpi = spi::Spi<spi::Config<NinaSpiPads>, spi::Duplex>;

/// Convenience for setting up the Nina peripheral.
/// This powers up SERCOM2 and configures it for use as an
/// SPI Master in SPI Mode 0.
pub fn nina_spi_master(
    clocks: &mut GenericClockController,
    baud: impl Into<Hertz>,
    sercom2: pac::SERCOM2,
    pm: &mut pac::PM,
    sck: impl Into<NinaSck>,
    mosi: impl Into<NinaMosi>,
    miso: impl Into<NinaMiso>,
) -> NinaSpi {
    let gclk0 = clocks.gclk0();
    let clock = clocks.sercom2_core(&gclk0).unwrap();
    let (miso, mosi, sclk) = (miso.into(), mosi.into(), sck.into());
    let pads = spi::Pads::default().data_in(miso).data_out(mosi).sclk(sclk);

    spi::Config::new(pm, sercom2, pads, clock.freq())
        .baud(baud)
        .spi_mode(spi::MODE_0)
        .enable()
}