use super::{hal, pac, pac::MCLK, pac::SERCOM2, pac::SERCOM6, pac::SERCOM7};
use hal::define_pins;
use hal::gpio::{self, *};
use hal::sercom::{
I2CMaster7, PadPin, SPIMaster6, Sercom2Pad0, Sercom2Pad1, Sercom6Pad0, Sercom6Pad1,
Sercom6Pad3, Sercom7Pad0, Sercom7Pad1, UART2,
};
use hal::time::Hertz;
use hal::clock::GenericClockController;
#[cfg(feature = "usb")]
use super::pac::gclk::{genctrl::SRC_A, pchctrl::GEN_A};
#[cfg(feature = "usb")]
use hal::usb::usb_device::bus::UsbBusAllocator;
#[cfg(feature = "usb")]
pub use hal::usb::UsbBus;
define_pins!(
struct Pins,
pac: pac,
pin sw0 = b31,
pin led = c18,
pin qt_button = a16,
pin tx = b25, pin rx = b24,
pin sda = d8, pin scl = d9,
pin mosi = c4, pin miso = c7, pin sck = c5,
pin pb04 = b4, pin pb05 = b5, pin pa06 = a6, pin pa07 = a7, pin pb08 = b8, pin pb09 = b9, pin pb07 = b7, pin pa27 = a27, pin uart0_rx = a5, pin uart0_tx = a4, pin spi4_mosi = b27, pin spi4_sck = b26,
pin pb00 = b0, pin pa03 = a3, pin pb01 = b1, pin pb06 = b6, pin pb14 = b14, pin pb15 = b15, pin pd00 = d0, pin pb02 = b2, pin spi6_ss = c6,
pin pc02 = c2, pin pc03 = c3, pin pc01 = c1, pin pc10 = c10, pin pd10 = d10, pin pd11 = d11, pin pc30 = c30, pin pc31 = c31, pin uart1_rx = c23, pin uart1_tx = c22, pin pc14 = c14,
pin sd_d0 = b18,
pin sd_d1 = b19,
pin sd_d2 = b20,
pin sd_d3 = b21,
pin sd_clk_i2s_sdo = a21,
pin sd_cmd_i2s_fs0 = a20,
pin sd_cd = d20,
pin sd_wp = d21,
pin pdec_phase_a = c16,
pin pdec_phase_b = c17,
pin vbat = b3,
pin adc_dac = a2,
pin vbus_detecion = c0,
pin usb_id = c19,
pin usb_dm = a24,
pin usb_dp = a25,
pin swd = a31,
pin swc = a30,
pin swo = b30,
pin can_tx = b12,
pin can_rx = b13,
pin can_standby = c13,
pin gtxck = a14,
pin gtxen = a17,
pin gtx0 = a18,
pin gtx1 = a19,
pin grxdv = c20,
pin grx0 = a13,
pin grx1 = a12,
pin grxer = a15,
pin gmdc = c11,
pin gmdio = c12,
pin ethernet_interrupt = d12,
pin ethernet_reset = c21,
pin flash_io0 = a8,
pin flash_io1 = a9,
pin flash_io2 = a10,
pin flash_io3 = a11,
pin flash_sck = b10,
pin flash_cs = b11,
pin i2s_sck0 = b16,
pin i2s_mck0 = b17,
pin i2s_sdi = a22,
pin i2s_fs1 = a23,
pin i2s_sck1 = b28,
pin i2s_mck1 = b29,
pin dgi_ss = d1,
);
impl Pins {
pub fn split(self) -> Sets {
let analog = Analog {
};
let flash = QSPIFlash {
sck: self.flash_sck,
cs: self.flash_cs,
data0: self.flash_io0,
data1: self.flash_io1,
data2: self.flash_io2,
data3: self.flash_io3,
};
let spi = SPI {
sck: self.sck,
mosi: self.mosi,
miso: self.miso,
};
let sdcard = SdCard {
d0: self.sd_d0,
d1: self.sd_d1,
d2: self.sd_d2,
d3: self.sd_d3,
clk: self.sd_clk_i2s_sdo,
cmd: self.sd_cmd_i2s_fs0,
cd: self.sd_cd,
wp: self.sd_wp,
};
let i2c = I2C {
sda: self.sda,
scl: self.scl,
};
let usb = USB {
dm: self.usb_dm,
dp: self.usb_dp,
};
let uart = UART_ {
rx: self.rx,
tx: self.tx,
};
Sets {
port: self.port,
analog,
spi,
usb,
flash,
sdcard,
i2c,
uart,
led: self.led,
sw0: self.sw0,
}
}
}
pub struct Sets {
pub led: Pc18<Input<Floating>>,
pub sw0: Pb31<Input<Floating>>,
pub analog: Analog,
pub spi: SPI,
pub sdcard: SdCard,
pub i2c: I2C,
pub flash: QSPIFlash,
pub usb: USB,
pub uart: UART_,
pub port: Port,
}
pub struct SPI {
pub mosi: gpio::Pc4<Input<Floating>>,
pub miso: gpio::Pc7<Input<Floating>>,
pub sck: gpio::Pc5<Input<Floating>>,
}
impl SPI {
#[allow(clippy::clippy::type_complexity)]
pub fn init<F: Into<Hertz>>(
self,
clocks: &mut GenericClockController,
bus_speed: F,
sercom6: SERCOM6,
mclk: &mut MCLK,
port: &mut Port,
) -> SPIMaster6<
Sercom6Pad3<gpio::Pc7<gpio::PfC>>,
Sercom6Pad0<gpio::Pc4<gpio::PfC>>,
Sercom6Pad1<gpio::Pc5<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,
(
self.miso.into_pad(port),
self.mosi.into_pad(port),
self.sck.into_pad(port),
),
)
}
}
pub struct I2C {
pub sda: Pd8<Input<Floating>>,
pub scl: Pd9<Input<Floating>>,
}
impl I2C {
pub fn init<F: Into<Hertz>>(
self,
clocks: &mut GenericClockController,
bus_speed: F,
sercom7: SERCOM7,
mclk: &mut MCLK,
port: &mut Port,
) -> I2CMaster7<Sercom7Pad0<Pd8<PfC>>, Sercom7Pad1<Pd9<PfC>>> {
let gclk0 = clocks.gclk0();
I2CMaster7::new(
&clocks.sercom7_core(&gclk0).unwrap(),
bus_speed.into(),
sercom7,
mclk,
self.sda.into_pad(port),
self.scl.into_pad(port),
)
}
}
pub struct SdCard {
pub d0: Pb18<Input<Floating>>,
pub d1: Pb19<Input<Floating>>,
pub d2: Pb20<Input<Floating>>,
pub d3: Pb21<Input<Floating>>,
pub clk: Pa21<Input<Floating>>,
pub cmd: Pa20<Input<Floating>>,
pub cd: Pd20<Input<Floating>>,
pub wp: Pd21<Input<Floating>>,
}
pub struct USB {
pub dm: Pa24<Input<Floating>>,
pub dp: Pa25<Input<Floating>>,
}
impl USB {
#[cfg(feature = "usb")]
pub fn init(
self,
usb: super::pac::USB,
clocks: &mut GenericClockController,
mclk: &mut MCLK,
) -> UsbBusAllocator<UsbBus> {
clocks.configure_gclk_divider_and_source(GEN_A::GCLK2, 1, SRC_A::DFLL, false);
let usb_gclk = clocks.get_gclk(GEN_A::GCLK2).unwrap();
let usb_clock = &clocks.usb(&usb_gclk).unwrap();
UsbBusAllocator::new(UsbBus::new(usb_clock, mclk, self.dm, self.dp, usb))
}
}
pub struct UART_ {
pub tx: Pb25<Input<Floating>>,
pub rx: Pb24<Input<Floating>>,
}
impl UART_ {
pub fn init<F: Into<Hertz>>(
self,
clocks: &mut GenericClockController,
baud: F,
sercom2: SERCOM2,
mclk: &mut MCLK,
port: &mut Port,
) -> UART2<Sercom2Pad1<gpio::Pb24<gpio::PfD>>, Sercom2Pad0<gpio::Pb25<gpio::PfD>>, (), ()> {
let gclk0 = clocks.gclk0();
UART2::new(
&clocks.sercom2_core(&gclk0).unwrap(),
baud.into(),
sercom2,
mclk,
(self.rx.into_pad(port), self.tx.into_pad(port)),
)
}
}
pub struct Analog {
}
pub struct QSPIFlash {
pub sck: Pb10<Input<Floating>>,
pub cs: Pb11<Input<Floating>>,
pub data0: Pa8<Input<Floating>>,
pub data1: Pa9<Input<Floating>>,
pub data2: Pa10<Input<Floating>>,
pub data3: Pa11<Input<Floating>>,
}