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
#![no_std]
#![recursion_limit = "1024"]

extern crate atsamd_hal as hal;

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

use hal::prelude::*;
use hal::*;

pub use hal::common::*;
pub use hal::same54::*;
pub use hal::target_device as pac;

use gpio::{Floating, Input, Port};
use hal::clock::GenericClockController;
use hal::sercom::{I2CMaster7, PadPin, SPIMaster6};
use hal::time::Hertz;

define_pins!(
    /// Maps the pins to their names
    struct Pins,
    target_device: target_device,

    /// LED Pin
    pin led0 = b21,

    /// RFM95
    pin rfm95_cap_bank = c13,
    pin rfm95_power_supply = b9,
    pin rfm95_radio_reset = a6,
    pin rfm95_spi_not = a7,
    pin rfm95_dio0 = d1,
    pin rfm95_dio1 = b6,
    pin rfm95_dio2 = b7,
    pin rfm95_dio3 = b8,
    pin rfm95_dio4 = a4,
    pin rfm95_dio5 = a5,

    /// ADXL313
    pin adxl313_power_supply = b10,
    pin adxl313_irq0 = b11,
    pin adxl313_irq1 = b12,
    pin adxl313_spi_not = b13,

    /// D7S
    pin d7s_power_supply = a8,
    pin d7s_irq0 = a9,
    pin d7s_irq1 = a10,

    /// TC72
    pin tc72_power_supply = d10,
    pin tc72_spi_not = d11,

    /// TODO ADC

    /// Button
    pin button = b20,

    /// The I2C data line
    pin sda = c12,
    /// The I2C clock line
    pin scl = d9,

    /// The SPI SCK
    pin sck = c17,
    /// The SPI MOSI
    pin mosi = c16,
    /// The SPI MISO
    pin miso = c19,
);

/// Convenience for setting up the labelled SPI peripheral.
/// This powers up SERCOM6 and configures it for use as an
/// SPI Master in SPI Mode 0.
pub fn spi_master<F: Into<Hertz>>(
    clocks: &mut GenericClockController,
    bus_speed: F,
    sercom6: pac::SERCOM6,
    mclk: &mut pac::MCLK,
    sck: gpio::Pc17<Input<Floating>>,
    mosi: gpio::Pc16<Input<Floating>>,
    miso: gpio::Pc19<Input<Floating>>,
    port: &mut Port,
) -> SPIMaster6<
    hal::sercom::Sercom6Pad3<gpio::Pc19<gpio::PfC>>,
    hal::sercom::Sercom6Pad0<gpio::Pc16<gpio::PfC>>,
    hal::sercom::Sercom6Pad1<gpio::Pc17<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,
        (miso.into_pad(port), mosi.into_pad(port), sck.into_pad(port)),
    )
}

/// 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,
    sercom7: pac::SERCOM7,
    mclk: &mut pac::MCLK,
    sda: gpio::Pc12<Input<Floating>>,
    scl: gpio::Pd9<Input<Floating>>,
    port: &mut Port,
) -> I2CMaster7<
    hal::sercom::Sercom7Pad0<gpio::Pc12<gpio::PfC>>,
    hal::sercom::Sercom7Pad1<gpio::Pd9<gpio::PfC>>,
> {
    let gclk0 = clocks.gclk0();
    I2CMaster7::new(
        &clocks.sercom7_core(&gclk0).unwrap(),
        bus_speed.into(),
        sercom7,
        mclk,
        sda.into_pad(port),
        scl.into_pad(port),
    )
}