Move DAC, I2C, SPI and RNG to macro-tables.

This commit is contained in:
Bob McWhirter
2021-06-03 11:09:29 -04:00
parent d4d914ea50
commit 6958091b50
6 changed files with 162 additions and 62 deletions

View File

@ -3,6 +3,7 @@
#[cfg_attr(dac_v2, path = "v2.rs")]
mod _version;
use crate::gpio::NoPin;
use crate::peripherals;
pub use _version::*;
pub(crate) mod sealed {
@ -23,8 +24,8 @@ pub trait DacPin<T: Instance, const C: u8>: sealed::DacPin<T, C> + 'static {}
impl<T: Instance, const C: u8> DacPin<T, C> for NoPin {}
impl<T: Instance, const C: u8> sealed::DacPin<T, C> for NoPin {}
macro_rules! impl_dac {
($inst:ident) => {
crate::pac::peripherals!(
(dac, $inst:ident) => {
impl crate::dac::sealed::Instance for peripherals::$inst {
fn regs() -> &'static crate::pac::dac::Dac {
&crate::pac::$inst
@ -33,16 +34,21 @@ macro_rules! impl_dac {
impl crate::dac::Instance for peripherals::$inst {}
};
}
);
macro_rules! impl_dac_pin {
($inst:ident, $channel:expr, $pin:ident ) => {
impl crate::dac::DacPin<peripherals::$inst, $channel> for peripherals::$pin {}
crate::pac::peripheral_pins!(
($inst:ident, dac, DAC, $pin:ident, OUT1) => {
impl DacPin<peripherals::$inst, 1> for peripherals::$pin {}
impl crate::dac::sealed::DacPin<peripherals::$inst, $channel> for peripherals::$pin {
//fn af_num(&self) -> u8 {
//$af
//}
impl sealed::DacPin<peripherals::$inst, 1> for peripherals::$pin {
}
};
($inst:ident, dac, DAC, $pin:ident, OUT2) => {
impl DacPin<peripherals::$inst, 2> for peripherals::$pin {}
impl sealed::DacPin<peripherals::$inst, 2> for peripherals::$pin {
}
};
}
);

View File

@ -3,6 +3,7 @@
#[cfg_attr(i2c_v1, path = "v1.rs")]
#[cfg_attr(i2c_v2, path = "v2.rs")]
mod _version;
use crate::peripherals;
pub use _version::*;
pub enum Error {
@ -37,8 +38,8 @@ pub trait SclPin<T: Instance>: sealed::SclPin<T> + 'static {}
pub trait SdaPin<T: Instance>: sealed::SdaPin<T> + 'static {}
macro_rules! impl_i2c {
($inst:ident) => {
crate::pac::peripherals!(
(i2c, $inst:ident) => {
impl crate::i2c::sealed::Instance for peripherals::$inst {
fn regs() -> &'static crate::pac::i2c::I2c {
&crate::pac::$inst
@ -46,17 +47,28 @@ macro_rules! impl_i2c {
}
impl crate::i2c::Instance for peripherals::$inst {}
};
}
);
macro_rules! impl_i2c_pin {
($inst:ident, $pin_func:ident, $pin:ident, $af:expr) => {
impl crate::i2c::$pin_func<peripherals::$inst> for peripherals::$pin {}
crate::pac::peripheral_pins!(
($inst:ident, i2c, I2C, $pin:ident, SDA, $af:expr) => {
impl SdaPin<peripherals::$inst> for peripherals::$pin {}
impl crate::i2c::sealed::$pin_func<peripherals::$inst> for peripherals::$pin {
impl sealed::SdaPin<peripherals::$inst> for peripherals::$pin {
fn af_num(&self) -> u8 {
$af
}
}
};
}
($inst:ident, i2c, I2C, $pin:ident, SCL, $af:expr) => {
impl SclPin<peripherals::$inst> for peripherals::$pin {}
impl sealed::SclPin<peripherals::$inst> for peripherals::$pin {
fn af_num(&self) -> u8 {
$af
}
}
};
);

View File

@ -9,6 +9,7 @@ use futures::future::poll_fn;
use rand_core::{CryptoRng, RngCore};
use crate::pac;
use crate::peripherals;
pub(crate) static RNG_WAKER: AtomicWaker = AtomicWaker::new();
@ -134,6 +135,58 @@ pub(crate) mod sealed {
pub trait Instance: sealed::Instance {}
crate::pac::peripherals!(
(rng, $inst:ident) => {
impl Instance for peripherals::$inst {}
impl sealed::Instance for peripherals::$inst {
fn regs() -> crate::pac::rng::Rng {
crate::pac::RNG
}
}
};
);
macro_rules! irq {
($irq:ident) => {
mod rng_irq {
use crate::interrupt;
#[interrupt]
unsafe fn $irq() {
let bits = $crate::pac::RNG.sr().read();
if bits.drdy() || bits.seis() || bits.ceis() {
$crate::pac::RNG.cr().write(|reg| reg.set_ie(false));
$crate::rng::RNG_WAKER.wake();
}
}
}
};
}
crate::pac::interrupts!(
(RNG) => {
irq!(RNG);
};
(RNG_LPUART1) => {
irq!(RNG_LPUART1);
};
(AES_RNG_LPUART1) => {
irq!(AES_RNG_LPUART1);
};
(AES_RNG) => {
irq!(AES_RNG);
};
(HASH_RNG) => {
irq!(HASH_RNG);
};
);
/*
macro_rules! impl_rng {
($inst:ident, $irq:ident) => {
impl crate::rng::sealed::Instance for peripherals::RNG {
@ -158,3 +211,5 @@ macro_rules! impl_rng {
}
};
}
*/

View File

@ -4,6 +4,7 @@
#[cfg_attr(spi_v2, path = "v2.rs")]
#[cfg_attr(spi_v3, path = "v3.rs")]
mod _version;
use crate::peripherals;
pub use _version::*;
use crate::gpio::Pin;
@ -71,26 +72,46 @@ pub trait MosiPin<T: Instance>: sealed::MosiPin<T> + 'static {}
pub trait MisoPin<T: Instance>: sealed::MisoPin<T> + 'static {}
macro_rules! impl_spi {
($inst:ident, $clk:ident) => {
impl crate::spi::sealed::Instance for peripherals::$inst {
crate::pac::peripherals!(
(spi, $inst:ident) => {
impl sealed::Instance for peripherals::$inst {
fn regs() -> &'static crate::pac::spi::Spi {
&crate::pac::$inst
}
}
impl crate::spi::Instance for peripherals::$inst {}
impl Instance for peripherals::$inst {}
};
}
);
macro_rules! impl_spi_pin {
($inst:ident, $pin_func:ident, $pin:ident, $af:expr) => {
impl crate::spi::$pin_func<peripherals::$inst> for peripherals::$pin {}
crate::pac::peripheral_pins!(
($inst:ident, spi, SPI, $pin:ident, SCK, $af:expr) => {
impl SckPin<peripherals::$inst> for peripherals::$pin {}
impl crate::spi::sealed::$pin_func<peripherals::$inst> for peripherals::$pin {
impl sealed::SckPin<peripherals::$inst> for peripherals::$pin {
fn af_num(&self) -> u8 {
$af
}
}
};
}
($inst:ident, spi, SPI, $pin:ident, MOSI, $af:expr) => {
impl MosiPin<peripherals::$inst> for peripherals::$pin {}
impl sealed::MosiPin<peripherals::$inst> for peripherals::$pin {
fn af_num(&self) -> u8 {
$af
}
}
};
($inst:ident, spi, SPI, $pin:ident, MISO, $af:expr) => {
impl MisoPin<peripherals::$inst> for peripherals::$pin {}
impl sealed::MisoPin<peripherals::$inst> for peripherals::$pin {
fn af_num(&self) -> u8 {
$af
}
}
};
);