2021-05-24 15:41:37 +00:00
|
|
|
#![macro_use]
|
|
|
|
|
2021-05-25 02:17:24 +00:00
|
|
|
#[cfg_attr(i2c_v1, path = "v1.rs")]
|
|
|
|
#[cfg_attr(i2c_v2, path = "v2.rs")]
|
2021-05-24 15:41:37 +00:00
|
|
|
mod _version;
|
2021-06-03 15:09:29 +00:00
|
|
|
use crate::peripherals;
|
2021-05-24 15:41:37 +00:00
|
|
|
pub use _version::*;
|
|
|
|
|
2021-07-08 22:53:47 +00:00
|
|
|
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
|
2021-05-24 15:41:37 +00:00
|
|
|
pub enum Error {
|
|
|
|
Bus,
|
|
|
|
Arbitration,
|
|
|
|
Nack,
|
2021-05-25 18:47:07 +00:00
|
|
|
Timeout,
|
|
|
|
Crc,
|
|
|
|
Overrun,
|
2021-05-24 15:41:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
pub(crate) mod sealed {
|
|
|
|
use crate::gpio::Pin;
|
2021-07-01 17:53:57 +00:00
|
|
|
use crate::rcc::RccPeripheral;
|
2021-05-24 15:41:37 +00:00
|
|
|
|
2021-07-01 17:53:57 +00:00
|
|
|
pub trait Instance: RccPeripheral {
|
2021-05-24 15:41:37 +00:00
|
|
|
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 {}
|
|
|
|
|
2021-06-03 15:09:29 +00:00
|
|
|
crate::pac::peripherals!(
|
|
|
|
(i2c, $inst:ident) => {
|
2021-06-03 18:25:17 +00:00
|
|
|
impl sealed::Instance for peripherals::$inst {
|
2021-05-24 15:41:37 +00:00
|
|
|
fn regs() -> &'static crate::pac::i2c::I2c {
|
|
|
|
&crate::pac::$inst
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-06-03 18:25:17 +00:00
|
|
|
impl Instance for peripherals::$inst {}
|
2021-06-03 15:09:29 +00:00
|
|
|
|
2021-05-24 15:41:37 +00:00
|
|
|
};
|
2021-06-03 15:09:29 +00:00
|
|
|
);
|
2021-05-24 15:41:37 +00:00
|
|
|
|
2021-06-03 15:29:29 +00:00
|
|
|
macro_rules! impl_pin {
|
|
|
|
($inst:ident, $pin:ident, $signal:ident, $af:expr) => {
|
|
|
|
impl $signal<peripherals::$inst> for peripherals::$pin {}
|
2021-05-24 15:41:37 +00:00
|
|
|
|
2021-06-03 15:29:29 +00:00
|
|
|
impl sealed::$signal<peripherals::$inst> for peripherals::$pin {
|
2021-05-24 15:41:37 +00:00
|
|
|
fn af_num(&self) -> u8 {
|
|
|
|
$af
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
2021-06-03 15:29:29 +00:00
|
|
|
}
|
2021-06-03 15:09:29 +00:00
|
|
|
|
2021-06-03 15:29:29 +00:00
|
|
|
crate::pac::peripheral_pins!(
|
|
|
|
($inst:ident, i2c, I2C, $pin:ident, SDA, $af:expr) => {
|
|
|
|
impl_pin!($inst, $pin, SdaPin, $af);
|
|
|
|
};
|
2021-06-03 15:09:29 +00:00
|
|
|
|
2021-06-03 15:29:29 +00:00
|
|
|
($inst:ident, i2c, I2C, $pin:ident, SCL, $af:expr) => {
|
|
|
|
impl_pin!($inst, $pin, SclPin, $af);
|
2021-06-03 15:09:29 +00:00
|
|
|
};
|
|
|
|
);
|