embassy_stm32/i2c/
config.rs1#[cfg(gpio_v2)]
2use crate::gpio::Pull;
3use crate::gpio::{AfType, OutputType, Speed};
4use crate::time::Hertz;
5
6#[repr(u8)]
7#[derive(Copy, Clone)]
8#[cfg_attr(feature = "defmt", derive(defmt::Format))]
9pub enum AddrMask {
11 NOMASK,
13 MASK1,
15 MASK2,
17 MASK3,
19 MASK4,
21 MASK5,
23 MASK6,
25 MASK7,
27}
28
29#[derive(Debug, Copy, Clone, PartialEq, Eq)]
30#[cfg_attr(feature = "defmt", derive(defmt::Format))]
31pub enum Address {
33 SevenBit(u8),
35 TenBit(u16),
39}
40impl From<u8> for Address {
41 fn from(value: u8) -> Self {
42 Address::SevenBit(value)
43 }
44}
45impl From<u16> for Address {
46 fn from(value: u16) -> Self {
47 assert!(value < 0x400, "Ten bit address must be less than 0x400");
48 Address::TenBit(value)
49 }
50}
51impl Address {
52 pub fn addr(&self) -> u16 {
56 match self {
57 Address::SevenBit(addr) => *addr as u16,
58 Address::TenBit(addr) => *addr,
59 }
60 }
61}
62
63#[derive(Copy, Clone)]
64#[cfg_attr(feature = "defmt", derive(defmt::Format))]
65pub struct OA2 {
67 pub addr: u8,
69 pub mask: AddrMask,
71}
72
73#[derive(Copy, Clone)]
74#[cfg_attr(feature = "defmt", derive(defmt::Format))]
75pub enum OwnAddresses {
77 OA1(Address),
79 OA2(OA2),
81 Both {
83 oa1: Address,
85 oa2: OA2,
87 },
88}
89
90#[derive(Copy, Clone)]
92#[cfg_attr(feature = "defmt", derive(defmt::Format))]
93pub struct SlaveAddrConfig {
94 pub addr: OwnAddresses,
96 pub general_call: bool,
98}
99impl SlaveAddrConfig {
100 pub fn basic(addr: u8) -> Self {
102 Self {
103 addr: OwnAddresses::OA1(Address::SevenBit(addr)),
104 general_call: false,
105 }
106 }
107}
108
109#[non_exhaustive]
111#[derive(Copy, Clone)]
112pub struct Config {
113 pub frequency: Hertz,
115 pub gpio_speed: Speed,
117 #[cfg(gpio_v2)]
122 pub sda_pullup: bool,
123 #[cfg(gpio_v2)]
128 pub scl_pullup: bool,
129 #[cfg(feature = "time")]
131 pub timeout: embassy_time::Duration,
132}
133
134impl Default for Config {
135 fn default() -> Self {
136 Self {
137 frequency: Hertz::khz(100),
138 gpio_speed: Speed::Medium,
139 #[cfg(gpio_v2)]
140 sda_pullup: false,
141 #[cfg(gpio_v2)]
142 scl_pullup: false,
143 #[cfg(feature = "time")]
144 timeout: embassy_time::Duration::from_millis(1000),
145 }
146 }
147}
148
149impl Config {
150 pub(super) fn scl_af(&self) -> AfType {
151 #[cfg(gpio_v1)]
152 return AfType::output(OutputType::OpenDrain, self.gpio_speed);
153 #[cfg(gpio_v2)]
154 return AfType::output_pull(
155 OutputType::OpenDrain,
156 self.gpio_speed,
157 match self.scl_pullup {
158 true => Pull::Up,
159 false => Pull::None,
160 },
161 );
162 }
163
164 pub(super) fn sda_af(&self) -> AfType {
165 #[cfg(gpio_v1)]
166 return AfType::output(OutputType::OpenDrain, self.gpio_speed);
167 #[cfg(gpio_v2)]
168 return AfType::output_pull(
169 OutputType::OpenDrain,
170 self.gpio_speed,
171 match self.sda_pullup {
172 true => Pull::Up,
173 false => Pull::None,
174 },
175 );
176 }
177}