2021-05-24 17:41:37 +02:00
|
|
|
#![macro_use]
|
|
|
|
|
2021-07-18 06:07:34 +02:00
|
|
|
use embassy::interrupt::Interrupt;
|
|
|
|
|
2021-05-25 04:17:24 +02:00
|
|
|
#[cfg_attr(i2c_v1, path = "v1.rs")]
|
|
|
|
#[cfg_attr(i2c_v2, path = "v2.rs")]
|
2021-05-24 17:41:37 +02:00
|
|
|
mod _version;
|
2021-07-18 06:07:34 +02:00
|
|
|
use crate::{dma, peripherals};
|
2021-05-24 17:41:37 +02:00
|
|
|
pub use _version::*;
|
|
|
|
|
2021-07-09 00:53:47 +02:00
|
|
|
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
|
2021-05-24 17:41:37 +02:00
|
|
|
pub enum Error {
|
|
|
|
Bus,
|
|
|
|
Arbitration,
|
|
|
|
Nack,
|
2021-05-25 20:47:07 +02:00
|
|
|
Timeout,
|
|
|
|
Crc,
|
|
|
|
Overrun,
|
2021-07-09 07:57:34 +02:00
|
|
|
ZeroLengthTransfer,
|
2021-05-24 17:41:37 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
pub(crate) mod sealed {
|
2021-07-18 06:07:34 +02:00
|
|
|
use super::dma;
|
2021-05-24 17:41:37 +02:00
|
|
|
use crate::gpio::Pin;
|
2021-07-01 19:53:57 +02:00
|
|
|
use crate::rcc::RccPeripheral;
|
2021-05-24 17:41:37 +02:00
|
|
|
|
2021-07-01 19:53:57 +02:00
|
|
|
pub trait Instance: RccPeripheral {
|
2021-07-30 00:13:57 +02:00
|
|
|
fn regs() -> crate::pac::i2c::I2c;
|
2021-07-18 06:07:34 +02:00
|
|
|
|
|
|
|
fn state_number() -> usize;
|
2021-05-24 17:41:37 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
pub trait SclPin<T: Instance>: Pin {
|
|
|
|
fn af_num(&self) -> u8;
|
|
|
|
}
|
|
|
|
|
|
|
|
pub trait SdaPin<T: Instance>: Pin {
|
|
|
|
fn af_num(&self) -> u8;
|
|
|
|
}
|
2021-07-18 06:07:34 +02:00
|
|
|
|
|
|
|
pub trait RxDma<T: Instance> {
|
|
|
|
fn request(&self) -> dma::Request;
|
|
|
|
}
|
|
|
|
|
|
|
|
pub trait TxDma<T: Instance> {
|
|
|
|
fn request(&self) -> dma::Request;
|
|
|
|
}
|
2021-05-24 17:41:37 +02:00
|
|
|
}
|
|
|
|
|
2021-07-18 06:07:34 +02:00
|
|
|
pub trait Instance: sealed::Instance + 'static {
|
|
|
|
type Interrupt: Interrupt;
|
|
|
|
}
|
2021-05-24 17:41:37 +02:00
|
|
|
|
|
|
|
pub trait SclPin<T: Instance>: sealed::SclPin<T> + 'static {}
|
|
|
|
|
|
|
|
pub trait SdaPin<T: Instance>: sealed::SdaPin<T> + 'static {}
|
|
|
|
|
2021-07-18 06:07:34 +02:00
|
|
|
pub trait RxDma<T: Instance>: sealed::RxDma<T> + dma::Channel {}
|
|
|
|
|
|
|
|
pub trait TxDma<T: Instance>: sealed::TxDma<T> + dma::Channel {}
|
|
|
|
|
|
|
|
macro_rules! i2c_state {
|
|
|
|
(I2C1) => {
|
|
|
|
0
|
|
|
|
};
|
|
|
|
(I2C2) => {
|
|
|
|
1
|
|
|
|
};
|
|
|
|
(I2C3) => {
|
|
|
|
2
|
|
|
|
};
|
|
|
|
(I2C4) => {
|
|
|
|
3
|
|
|
|
};
|
|
|
|
(I2C5) => {
|
|
|
|
4
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2021-07-30 00:13:57 +02:00
|
|
|
crate::pac::interrupts!(
|
|
|
|
($inst:ident, i2c, $block:ident, EV, $irq:ident) => {
|
2021-06-03 20:25:17 +02:00
|
|
|
impl sealed::Instance for peripherals::$inst {
|
2021-07-30 00:13:57 +02:00
|
|
|
fn regs() -> crate::pac::i2c::I2c {
|
|
|
|
crate::pac::$inst
|
2021-05-24 17:41:37 +02:00
|
|
|
}
|
2021-07-18 06:07:34 +02:00
|
|
|
|
|
|
|
fn state_number() -> usize {
|
|
|
|
i2c_state!($inst)
|
|
|
|
}
|
2021-05-24 17:41:37 +02:00
|
|
|
}
|
|
|
|
|
2021-07-18 06:07:34 +02:00
|
|
|
impl Instance for peripherals::$inst {
|
2021-07-30 00:13:57 +02:00
|
|
|
type Interrupt = crate::interrupt::$irq;
|
2021-07-18 06:07:34 +02:00
|
|
|
}
|
2021-06-03 17:09:29 +02:00
|
|
|
|
2021-05-24 17:41:37 +02:00
|
|
|
};
|
2021-06-03 17:09:29 +02:00
|
|
|
);
|
2021-05-24 17:41:37 +02:00
|
|
|
|
2021-06-03 17:29:29 +02:00
|
|
|
macro_rules! impl_pin {
|
|
|
|
($inst:ident, $pin:ident, $signal:ident, $af:expr) => {
|
|
|
|
impl $signal<peripherals::$inst> for peripherals::$pin {}
|
2021-05-24 17:41:37 +02:00
|
|
|
|
2021-06-03 17:29:29 +02:00
|
|
|
impl sealed::$signal<peripherals::$inst> for peripherals::$pin {
|
2021-05-24 17:41:37 +02:00
|
|
|
fn af_num(&self) -> u8 {
|
|
|
|
$af
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
2021-06-03 17:29:29 +02:00
|
|
|
}
|
2021-06-03 17:09:29 +02:00
|
|
|
|
2021-10-09 11:35:05 +02:00
|
|
|
#[cfg(not(rcc_f1))]
|
2021-06-03 17:29:29 +02:00
|
|
|
crate::pac::peripheral_pins!(
|
|
|
|
($inst:ident, i2c, I2C, $pin:ident, SDA, $af:expr) => {
|
|
|
|
impl_pin!($inst, $pin, SdaPin, $af);
|
|
|
|
};
|
2021-06-03 17:09:29 +02:00
|
|
|
|
2021-06-03 17:29:29 +02:00
|
|
|
($inst:ident, i2c, I2C, $pin:ident, SCL, $af:expr) => {
|
|
|
|
impl_pin!($inst, $pin, SclPin, $af);
|
2021-06-03 17:09:29 +02:00
|
|
|
};
|
|
|
|
);
|
2021-07-18 06:07:34 +02:00
|
|
|
|
2021-10-09 11:35:05 +02:00
|
|
|
#[cfg(rcc_f1)]
|
|
|
|
crate::pac::peripheral_pins!(
|
|
|
|
($inst:ident, i2c, I2C, $pin:ident, SDA) => {
|
|
|
|
impl_pin!($inst, $pin, SdaPin, 0);
|
|
|
|
};
|
|
|
|
|
|
|
|
($inst:ident, i2c, I2C, $pin:ident, SCL) => {
|
|
|
|
impl_pin!($inst, $pin, SclPin, 0);
|
|
|
|
};
|
|
|
|
);
|
|
|
|
|
2021-11-17 21:43:05 +01:00
|
|
|
#[allow(unused)]
|
2021-07-18 06:07:34 +02:00
|
|
|
macro_rules! impl_dma {
|
|
|
|
($inst:ident, {dmamux: $dmamux:ident}, $signal:ident, $request:expr) => {
|
|
|
|
impl<T> sealed::$signal<peripherals::$inst> for T
|
|
|
|
where
|
|
|
|
T: crate::dma::MuxChannel<Mux = crate::dma::$dmamux>,
|
|
|
|
{
|
|
|
|
fn request(&self) -> dma::Request {
|
|
|
|
$request
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<T> $signal<peripherals::$inst> for T where
|
|
|
|
T: crate::dma::MuxChannel<Mux = crate::dma::$dmamux>
|
|
|
|
{
|
|
|
|
}
|
|
|
|
};
|
|
|
|
($inst:ident, {channel: $channel:ident}, $signal:ident, $request:expr) => {
|
|
|
|
impl sealed::$signal<peripherals::$inst> for peripherals::$channel {
|
|
|
|
fn request(&self) -> dma::Request {
|
|
|
|
$request
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl $signal<peripherals::$inst> for peripherals::$channel {}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
crate::pac::peripheral_dma_channels! {
|
|
|
|
($peri:ident, i2c, $kind:ident, RX, $channel:tt, $request:expr) => {
|
|
|
|
impl_dma!($peri, $channel, RxDma, $request);
|
|
|
|
};
|
|
|
|
($peri:ident, i2c, $kind:ident, TX, $channel:tt, $request:expr) => {
|
|
|
|
impl_dma!($peri, $channel, TxDma, $request);
|
|
|
|
};
|
|
|
|
}
|