#![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, Output, Port, PushPull};
use hal::clock::GenericClockController;
use hal::sercom::{I2CMaster3, PadPin, SPIMaster4, SPIMaster5};
use hal::time::Hertz;
define_pins!(
struct Pins,
target_device: atsamd21g18a,
pin a0 = a2,
pin a1 = b8,
pin a2 = b9,
pin a3 = a4,
pin a4 = a5,
pin a5 = b2,
pin d0 = a11,
pin d1 = a10,
pin d2 = a14,
pin d3 = a9,
pin d4 = a8,
pin d5 = a15,
pin d7 = a21,
pin d9 = a7,
pin d10 = a18,
pin d11 = a16,
pin d12 = a19,
pin d13 = a17,
pin mosi = b10,
pin miso = a12,
pin sck = b11,
pin flash_mosi = b22,
pin flash_miso = b3,
pin flash_sck = b23,
pin flash_cs = a27,
pin scl = a23,
pin sda = a22,
pin dotstar_ci = a0,
pin dotstar_di = a1,
pin swdio = a31,
pin swdclk = a30,
pin usb_dm = a24,
pin usb_dp = a25,
);
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),
},
)
}
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)
}
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,
))
}