2021-06-03 13:12:38 -04:00

75 lines
1.6 KiB
Rust

#![macro_use]
#[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 {
Bus,
Arbitration,
Nack,
Timeout,
Crc,
Overrun,
}
pub(crate) mod sealed {
use super::*;
use crate::gpio::Pin;
pub trait Instance {
fn regs() -> &'static crate::pac::i2c::I2c;
}
pub trait SclPin<T: Instance>: Pin {
fn af_num(&self) -> u8;
}
pub trait SdaPin<T: Instance>: Pin {
fn af_num(&self) -> u8;
}
}
pub trait Instance: sealed::Instance + 'static {}
pub trait SclPin<T: Instance>: sealed::SclPin<T> + 'static {}
pub trait SdaPin<T: Instance>: sealed::SdaPin<T> + 'static {}
crate::pac::peripherals!(
(i2c, $inst:ident) => {
impl crate::i2c::sealed::Instance for peripherals::$inst {
fn regs() -> &'static crate::pac::i2c::I2c {
&crate::pac::$inst
}
}
impl crate::i2c::Instance for peripherals::$inst {}
};
);
crate::pac::peripheral_pins!(
($inst:ident, i2c, I2C, $pin:ident, SDA, $af:expr) => {
impl SdaPin<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
}
}
};
);