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

extern crate atsamd21_hal as hal;

pub use hal::atsamd21g18a::*;
use hal::prelude::*;
pub use hal::*;

use gpio::{Floating, Input, Output, Port, PushPull};
use hal::clock::GenericClockController;
use hal::sercom::{I2CMaster3, PadPin, SPIMaster4, SPIMaster5};
use hal::time::Hertz;

/// Maps the pins to their arduino names and
/// the numbers printed on the board.
pub struct Pins {
    /// Opaque port reference
    pub port: Port,

    /// Analog pin 0.  Can act as a true analog output
    /// as it has a DAC (which is not currently supported
    /// by this hal) as well as input.
    pub a0: gpio::Pa2<Input<Floating>>,
    /// Analog Pin 1
    pub a1: gpio::Pb8<Input<Floating>>,
    /// Analog Pin 2
    pub a2: gpio::Pb9<Input<Floating>>,
    /// Analog Pin 3
    pub a3: gpio::Pa4<Input<Floating>>,
    /// Analog Pin 4
    pub a4: gpio::Pa5<Input<Floating>>,
    /// Analog Pin 5
    pub a5: gpio::Pb2<Input<Floating>>,

    /// Pin 0, rx.  Also analog input (A6)
    pub d0: gpio::Pa11<Input<Floating>>,
    /// Pin 1, tx.  Also analog input (A7)
    pub d1: gpio::Pa10<Input<Floating>>,
    /// Pin 2
    pub d2: gpio::Pa14<Input<Floating>>,
    /// Pin 3, PWM capable
    pub d3: gpio::Pa9<Input<Floating>>,
    /// Pin 4, PWM capable.  Also analog input (A8)
    pub d4: gpio::Pa8<Input<Floating>>,
    /// Pin 5, PWM capable.  Also analog input (A9)
    pub d5: gpio::Pa15<Input<Floating>>,
    /// Pin 6, PWM capable
    pub d6: gpio::Pa20<Input<Floating>>,
    /// Pin 7
    pub d7: gpio::Pa21<Input<Floating>>,
    /// Pin 8, PWM capable.  Also analog input (A10)
    pub d8: gpio::Pa6<Input<Floating>>,
    /// Pin 9, PWM capable.  Also analog input (A11)
    pub d9: gpio::Pa7<Input<Floating>>,
    /// Pin 10, PWM capable
    pub d10: gpio::Pa18<Input<Floating>>,
    /// Pin 11, PWM capable
    pub d11: gpio::Pa16<Input<Floating>>,
    /// Pin 12, PWM capable
    pub d12: gpio::Pa19<Input<Floating>>,
    /// Digital pin number 13, which is also attached to
    /// the red LED.  PWM capable.
    pub d13: gpio::Pa17<Input<Floating>>,
    pub sda: gpio::Pa22<Input<Floating>>,
    pub scl: gpio::Pa23<Input<Floating>>,

    /// The data line attached to the neopixel.
    /// Is also attached to SWCLK.
    pub neopixel: gpio::Pa30<Input<Floating>>,

    /// The SPI SCK attached the to 2x3 header
    pub sck: gpio::Pb11<Input<Floating>>,
    /// The SPI MOSI attached the to 2x3 header
    pub mosi: gpio::Pb10<Input<Floating>>,
    /// The SPI MISO attached the to 2x3 header
    pub miso: gpio::Pa12<Input<Floating>>,

    /// The SCK pin attached to the on-board SPI flash
    pub flash_sck: gpio::Pb23<Input<Floating>>,
    /// The MOSI pin attached to the on-board SPI flash
    pub flash_mosi: gpio::Pb22<Input<Floating>>,
    /// The MISO pin attached to the on-board SPI flash
    pub flash_miso: gpio::Pb3<Input<Floating>>,
    /// The CS pin attached to the on-board SPI flash
    pub flash_cs: gpio::Pa13<Input<Floating>>,
}

/// Returns the pins for the device
pub fn pins(port: atsamd21g18a::PORT) -> Pins {
    let pins = port.split();
    Pins {
        port: pins.port,
        a0: pins.pa2,
        a1: pins.pb8,
        a2: pins.pb9,
        a3: pins.pa4,
        a4: pins.pa5,
        a5: pins.pb2,
        d0: pins.pa11,
        d1: pins.pa10,
        d2: pins.pa14,
        d3: pins.pa9,
        d4: pins.pa8,
        d5: pins.pa15,
        d6: pins.pa20,
        d7: pins.pa21,
        d8: pins.pa6,
        d9: pins.pa7,
        d10: pins.pa18,
        d11: pins.pa16,
        d12: pins.pa19,
        d13: pins.pa17,
        sda: pins.pa22,
        scl: pins.pa23,
        neopixel: pins.pa30,
        sck: pins.pb11,
        mosi: pins.pb10,
        miso: pins.pa12,

        flash_sck: pins.pb23,
        flash_mosi: pins.pb22,
        flash_miso: pins.pb3,
        flash_cs: pins.pa13,
    }
}

/// Convenience for setting up the 2x3 header block for SPI.
/// This powers up SERCOM4 and configures it for use as an
/// SPI Master in SPI Mode 0.
/// Unlike the `flash_spi_master` function, this
/// one does not accept a CS pin; configuring a pin for CS
/// is the responsibility of the caller, because it could be
/// any OutputPin, or even a pulled up line on the slave.
pub fn spi_master<F: Into<Hertz>>(
    clocks: &mut GenericClockController,
    bus_speed: F,
    sercom4: SERCOM4,
    pm: &mut PM,
    sck: gpio::Pb11<Input<Floating>>,
    mosi: gpio::Pb10<Input<Floating>>,
    miso: gpio::Pa12<Input<Floating>>,
    port: &mut Port,
) -> SPIMaster4 {
    let gclk0 = clocks.gclk0();
    SPIMaster4::new(
        &clocks.sercom4_core(&gclk0).unwrap(),
        bus_speed.into(),
        hal::hal::spi::Mode {
            phase: hal::hal::spi::Phase::CaptureOnFirstTransition,
            polarity: hal::hal::spi::Polarity::IdleLow,
        },
        sercom4,
        pm,
        hal::sercom::SPI4Pinout::Dipo0Dopo1 {
            miso: miso.into_pad(port),
            mosi: mosi.into_pad(port),
            sck: sck.into_pad(port),
        },
    )
}

/// Convenience for accessing the on-board SPI Flash device.
/// This powers up SERCOM5 and configures it for use as an
/// SPI Master.
pub fn flash_spi_master(
    clocks: &mut GenericClockController,
    sercom5: SERCOM5,
    pm: &mut PM,
    sck: gpio::Pb23<Input<Floating>>,
    mosi: gpio::Pb22<Input<Floating>>,
    miso: gpio::Pb3<Input<Floating>>,
    cs: gpio::Pa13<Input<Floating>>,
    port: &mut Port,
) -> (SPIMaster5, gpio::Pa13<Output<PushPull>>) {
    let gclk0 = clocks.gclk0();
    let flash = SPIMaster5::new(
        &clocks.sercom5_core(&gclk0).unwrap(),
        48.mhz(),
        hal::hal::spi::Mode {
            phase: hal::hal::spi::Phase::CaptureOnFirstTransition,
            polarity: hal::hal::spi::Polarity::IdleLow,
        },
        sercom5,
        pm,
        hal::sercom::SPI5Pinout::Dipo1Dopo1 {
            miso: miso.into_pad(port),
            mosi: mosi.into_pad(port),
            sck: sck.into_pad(port),
        },
    );

    let mut cs = cs.into_push_pull_output(port);
    cs.set_high();

    (flash, cs)
}

/// Convenience for setting up the labelled SDA, SCL pins to
/// operate as an I2C master running at the specified frequency.
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),
    )
}