#[cfg(gpio_v2)]
use crate::gpio::Pull;
use crate::gpio::{AfType, OutputType, Speed};
use crate::time::Hertz;
#[repr(u8)]
#[derive(Debug, Copy, Clone)]
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
pub enum AddrMask {
NOMASK,
MASK1,
MASK2,
MASK3,
MASK4,
MASK5,
MASK6,
MASK7,
}
#[derive(Debug, Copy, Clone, PartialEq, Eq)]
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
pub enum Address {
SevenBit(u8),
TenBit(u16),
}
impl From<u8> for Address {
fn from(value: u8) -> Self {
Address::SevenBit(value)
}
}
impl From<u16> for Address {
fn from(value: u16) -> Self {
assert!(value < 0x400, "Ten bit address must be less than 0x400");
Address::TenBit(value)
}
}
impl Address {
pub fn addr(&self) -> u16 {
match self {
Address::SevenBit(addr) => *addr as u16,
Address::TenBit(addr) => *addr,
}
}
}
#[derive(Debug, Copy, Clone)]
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
pub struct OA2 {
pub addr: u8,
pub mask: AddrMask,
}
#[derive(Debug, Copy, Clone)]
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
pub enum OwnAddresses {
OA1(Address),
OA2(OA2),
Both {
oa1: Address,
oa2: OA2,
},
}
#[derive(Debug, Copy, Clone)]
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
pub struct SlaveAddrConfig {
pub addr: OwnAddresses,
pub general_call: bool,
}
impl SlaveAddrConfig {
pub fn basic(addr: u8) -> Self {
Self {
addr: OwnAddresses::OA1(Address::SevenBit(addr)),
general_call: false,
}
}
}
#[non_exhaustive]
#[derive(Copy, Clone)]
pub struct Config {
pub frequency: Hertz,
pub gpio_speed: Speed,
#[cfg(gpio_v2)]
pub sda_pullup: bool,
#[cfg(gpio_v2)]
pub scl_pullup: bool,
#[cfg(feature = "time")]
pub timeout: embassy_time::Duration,
}
impl Default for Config {
fn default() -> Self {
Self {
frequency: Hertz::khz(100),
gpio_speed: Speed::Medium,
#[cfg(gpio_v2)]
sda_pullup: false,
#[cfg(gpio_v2)]
scl_pullup: false,
#[cfg(feature = "time")]
timeout: embassy_time::Duration::from_millis(1000),
}
}
}
impl Config {
pub(super) fn scl_af(&self) -> AfType {
#[cfg(gpio_v1)]
return AfType::output(OutputType::OpenDrain, self.gpio_speed);
#[cfg(gpio_v2)]
return AfType::output_pull(
OutputType::OpenDrain,
self.gpio_speed,
match self.scl_pullup {
true => Pull::Up,
false => Pull::None,
},
);
}
pub(super) fn sda_af(&self) -> AfType {
#[cfg(gpio_v1)]
return AfType::output(OutputType::OpenDrain, self.gpio_speed);
#[cfg(gpio_v2)]
return AfType::output_pull(
OutputType::OpenDrain,
self.gpio_speed,
match self.sda_pullup {
true => Pull::Up,
false => Pull::None,
},
);
}
}