aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDario Nieuwenhuis <[email protected]>2023-11-18 01:18:23 +0100
committerDario Nieuwenhuis <[email protected]>2023-11-24 23:55:45 +0100
commitbc65b8f7ec1df181c793846b7c0657f689963d3a (patch)
tree2df4ad2570808ba805f154f0dd5f7dea38131938
parente8ff5a2baf217ac7e52f119c0d17a9826ad60067 (diff)
stm32/i2c: add async, dual interrupt scaffolding.
-rw-r--r--embassy-stm32/Cargo.toml4
-rw-r--r--embassy-stm32/build.rs20
-rw-r--r--embassy-stm32/src/i2c/mod.rs157
-rw-r--r--embassy-stm32/src/i2c/v1.rs114
-rw-r--r--embassy-stm32/src/i2c/v2.rs157
-rw-r--r--examples/stm32f4/src/bin/i2c.rs3
-rw-r--r--examples/stm32h5/src/bin/i2c.rs3
-rw-r--r--examples/stm32h7/src/bin/camera.rs3
-rw-r--r--examples/stm32h7/src/bin/i2c.rs3
-rw-r--r--examples/stm32l4/src/bin/i2c.rs3
-rw-r--r--examples/stm32l4/src/bin/i2c_blocking_async.rs3
-rw-r--r--examples/stm32l4/src/bin/i2c_dma.rs3
-rw-r--r--examples/stm32l4/src/bin/spe_adin1110_http_server.rs3
13 files changed, 235 insertions, 241 deletions
diff --git a/embassy-stm32/Cargo.toml b/embassy-stm32/Cargo.toml
index 65434ceca..d04000a1d 100644
--- a/embassy-stm32/Cargo.toml
+++ b/embassy-stm32/Cargo.toml
@@ -58,7 +58,7 @@ rand_core = "0.6.3"
58sdio-host = "0.5.0" 58sdio-host = "0.5.0"
59embedded-sdmmc = { git = "https://github.com/embassy-rs/embedded-sdmmc-rs", rev = "a4f293d3a6f72158385f79c98634cb8a14d0d2fc", optional = true } 59embedded-sdmmc = { git = "https://github.com/embassy-rs/embedded-sdmmc-rs", rev = "a4f293d3a6f72158385f79c98634cb8a14d0d2fc", optional = true }
60critical-section = "1.1" 60critical-section = "1.1"
61stm32-metapac = { git = "https://github.com/embassy-rs/stm32-data-generated", tag = "stm32-data-fbb8f77326dd066aa6c0d66b3b46e76a569dda8b" } 61stm32-metapac = { git = "https://github.com/embassy-rs/stm32-data-generated", tag = "stm32-data-f6d1ffc1a25f208b5cd6b1024bff246592da1949" }
62vcell = "0.1.3" 62vcell = "0.1.3"
63bxcan = "0.7.0" 63bxcan = "0.7.0"
64nb = "1.0.0" 64nb = "1.0.0"
@@ -76,7 +76,7 @@ critical-section = { version = "1.1", features = ["std"] }
76[build-dependencies] 76[build-dependencies]
77proc-macro2 = "1.0.36" 77proc-macro2 = "1.0.36"
78quote = "1.0.15" 78quote = "1.0.15"
79stm32-metapac = { git = "https://github.com/embassy-rs/stm32-data-generated", tag = "stm32-data-fbb8f77326dd066aa6c0d66b3b46e76a569dda8b", default-features = false, features = ["metadata"]} 79stm32-metapac = { git = "https://github.com/embassy-rs/stm32-data-generated", tag = "stm32-data-f6d1ffc1a25f208b5cd6b1024bff246592da1949", default-features = false, features = ["metadata"]}
80 80
81 81
82[features] 82[features]
diff --git a/embassy-stm32/build.rs b/embassy-stm32/build.rs
index a7dac5f9d..4aae58229 100644
--- a/embassy-stm32/build.rs
+++ b/embassy-stm32/build.rs
@@ -1139,6 +1139,23 @@ fn main() {
1139 } 1139 }
1140 1140
1141 // ======== 1141 // ========
1142 // Write peripheral_interrupts module.
1143 let mut mt = TokenStream::new();
1144 for p in METADATA.peripherals {
1145 let mut pt = TokenStream::new();
1146
1147 for irq in p.interrupts {
1148 let iname = format_ident!("{}", irq.interrupt);
1149 let sname = format_ident!("{}", irq.signal);
1150 pt.extend(quote!(pub type #sname = crate::interrupt::typelevel::#iname;));
1151 }
1152
1153 let pname = format_ident!("{}", p.name);
1154 mt.extend(quote!(pub mod #pname { #pt }));
1155 }
1156 g.extend(quote!(#[allow(non_camel_case_types)] pub mod peripheral_interrupts { #mt }));
1157
1158 // ========
1142 // Write foreach_foo! macrotables 1159 // Write foreach_foo! macrotables
1143 1160
1144 let mut flash_regions_table: Vec<Vec<String>> = Vec::new(); 1161 let mut flash_regions_table: Vec<Vec<String>> = Vec::new();
@@ -1296,6 +1313,9 @@ fn main() {
1296 1313
1297 let mut m = String::new(); 1314 let mut m = String::new();
1298 1315
1316 // DO NOT ADD more macros like these.
1317 // These turned to be a bad idea!
1318 // Instead, make build.rs generate the final code.
1299 make_table(&mut m, "foreach_flash_region", &flash_regions_table); 1319 make_table(&mut m, "foreach_flash_region", &flash_regions_table);
1300 make_table(&mut m, "foreach_interrupt", &interrupts_table); 1320 make_table(&mut m, "foreach_interrupt", &interrupts_table);
1301 make_table(&mut m, "foreach_peripheral", &peripherals_table); 1321 make_table(&mut m, "foreach_peripheral", &peripherals_table);
diff --git a/embassy-stm32/src/i2c/mod.rs b/embassy-stm32/src/i2c/mod.rs
index dde1a5040..19346d707 100644
--- a/embassy-stm32/src/i2c/mod.rs
+++ b/embassy-stm32/src/i2c/mod.rs
@@ -1,11 +1,14 @@
1#![macro_use] 1#![macro_use]
2 2
3use core::marker::PhantomData;
4
3use crate::interrupt; 5use crate::interrupt;
4 6
5#[cfg_attr(i2c_v1, path = "v1.rs")] 7#[cfg_attr(i2c_v1, path = "v1.rs")]
6#[cfg_attr(i2c_v2, path = "v2.rs")] 8#[cfg_attr(i2c_v2, path = "v2.rs")]
7mod _version; 9mod _version;
8pub use _version::*; 10pub use _version::*;
11use embassy_sync::waitqueue::AtomicWaker;
9 12
10use crate::peripherals; 13use crate::peripherals;
11 14
@@ -23,6 +26,20 @@ pub enum Error {
23 26
24pub(crate) mod sealed { 27pub(crate) mod sealed {
25 use super::*; 28 use super::*;
29
30 pub struct State {
31 #[allow(unused)]
32 pub waker: AtomicWaker,
33 }
34
35 impl State {
36 pub const fn new() -> Self {
37 Self {
38 waker: AtomicWaker::new(),
39 }
40 }
41 }
42
26 pub trait Instance: crate::rcc::RccPeripheral { 43 pub trait Instance: crate::rcc::RccPeripheral {
27 fn regs() -> crate::pac::i2c::I2c; 44 fn regs() -> crate::pac::i2c::I2c;
28 fn state() -> &'static State; 45 fn state() -> &'static State;
@@ -30,7 +47,8 @@ pub(crate) mod sealed {
30} 47}
31 48
32pub trait Instance: sealed::Instance + 'static { 49pub trait Instance: sealed::Instance + 'static {
33 type Interrupt: interrupt::typelevel::Interrupt; 50 type EventInterrupt: interrupt::typelevel::Interrupt;
51 type ErrorInterrupt: interrupt::typelevel::Interrupt;
34} 52}
35 53
36pin_trait!(SclPin, Instance); 54pin_trait!(SclPin, Instance);
@@ -38,21 +56,148 @@ pin_trait!(SdaPin, Instance);
38dma_trait!(RxDma, Instance); 56dma_trait!(RxDma, Instance);
39dma_trait!(TxDma, Instance); 57dma_trait!(TxDma, Instance);
40 58
41foreach_interrupt!( 59/// Interrupt handler.
42 ($inst:ident, i2c, $block:ident, EV, $irq:ident) => { 60pub struct EventInterruptHandler<T: Instance> {
61 _phantom: PhantomData<T>,
62}
63
64impl<T: Instance> interrupt::typelevel::Handler<T::EventInterrupt> for EventInterruptHandler<T> {
65 unsafe fn on_interrupt() {
66 _version::on_interrupt::<T>()
67 }
68}
69
70pub struct ErrorInterruptHandler<T: Instance> {
71 _phantom: PhantomData<T>,
72}
73
74impl<T: Instance> interrupt::typelevel::Handler<T::ErrorInterrupt> for ErrorInterruptHandler<T> {
75 unsafe fn on_interrupt() {
76 _version::on_interrupt::<T>()
77 }
78}
79
80foreach_peripheral!(
81 (i2c, $inst:ident) => {
43 impl sealed::Instance for peripherals::$inst { 82 impl sealed::Instance for peripherals::$inst {
44 fn regs() -> crate::pac::i2c::I2c { 83 fn regs() -> crate::pac::i2c::I2c {
45 crate::pac::$inst 84 crate::pac::$inst
46 } 85 }
47 86
48 fn state() -> &'static State { 87 fn state() -> &'static sealed::State {
49 static STATE: State = State::new(); 88 static STATE: sealed::State = sealed::State::new();
50 &STATE 89 &STATE
51 } 90 }
52 } 91 }
53 92
54 impl Instance for peripherals::$inst { 93 impl Instance for peripherals::$inst {
55 type Interrupt = crate::interrupt::typelevel::$irq; 94 type EventInterrupt = crate::_generated::peripheral_interrupts::$inst::EV;
95 type ErrorInterrupt = crate::_generated::peripheral_interrupts::$inst::ER;
56 } 96 }
57 }; 97 };
58); 98);
99
100mod eh02 {
101 use super::*;
102
103 impl<'d, T: Instance> embedded_hal_02::blocking::i2c::Read for I2c<'d, T> {
104 type Error = Error;
105
106 fn read(&mut self, address: u8, buffer: &mut [u8]) -> Result<(), Self::Error> {
107 self.blocking_read(address, buffer)
108 }
109 }
110
111 impl<'d, T: Instance> embedded_hal_02::blocking::i2c::Write for I2c<'d, T> {
112 type Error = Error;
113
114 fn write(&mut self, address: u8, write: &[u8]) -> Result<(), Self::Error> {
115 self.blocking_write(address, write)
116 }
117 }
118
119 impl<'d, T: Instance> embedded_hal_02::blocking::i2c::WriteRead for I2c<'d, T> {
120 type Error = Error;
121
122 fn write_read(&mut self, address: u8, write: &[u8], read: &mut [u8]) -> Result<(), Self::Error> {
123 self.blocking_write_read(address, write, read)
124 }
125 }
126}
127
128#[cfg(feature = "unstable-traits")]
129mod eh1 {
130 use super::*;
131 use crate::dma::NoDma;
132
133 impl embedded_hal_1::i2c::Error for Error {
134 fn kind(&self) -> embedded_hal_1::i2c::ErrorKind {
135 match *self {
136 Self::Bus => embedded_hal_1::i2c::ErrorKind::Bus,
137 Self::Arbitration => embedded_hal_1::i2c::ErrorKind::ArbitrationLoss,
138 Self::Nack => {
139 embedded_hal_1::i2c::ErrorKind::NoAcknowledge(embedded_hal_1::i2c::NoAcknowledgeSource::Unknown)
140 }
141 Self::Timeout => embedded_hal_1::i2c::ErrorKind::Other,
142 Self::Crc => embedded_hal_1::i2c::ErrorKind::Other,
143 Self::Overrun => embedded_hal_1::i2c::ErrorKind::Overrun,
144 Self::ZeroLengthTransfer => embedded_hal_1::i2c::ErrorKind::Other,
145 }
146 }
147 }
148
149 impl<'d, T: Instance, TXDMA, RXDMA> embedded_hal_1::i2c::ErrorType for I2c<'d, T, TXDMA, RXDMA> {
150 type Error = Error;
151 }
152
153 impl<'d, T: Instance> embedded_hal_1::i2c::I2c for I2c<'d, T, NoDma, NoDma> {
154 fn read(&mut self, address: u8, read: &mut [u8]) -> Result<(), Self::Error> {
155 self.blocking_read(address, read)
156 }
157
158 fn write(&mut self, address: u8, write: &[u8]) -> Result<(), Self::Error> {
159 self.blocking_write(address, write)
160 }
161
162 fn write_read(&mut self, address: u8, write: &[u8], read: &mut [u8]) -> Result<(), Self::Error> {
163 self.blocking_write_read(address, write, read)
164 }
165
166 fn transaction(
167 &mut self,
168 _address: u8,
169 _operations: &mut [embedded_hal_1::i2c::Operation<'_>],
170 ) -> Result<(), Self::Error> {
171 todo!();
172 }
173 }
174}
175
176#[cfg(all(feature = "unstable-traits", feature = "nightly"))]
177mod eha {
178 use super::*;
179
180 impl<'d, T: Instance, TXDMA: TxDma<T>, RXDMA: RxDma<T>> embedded_hal_async::i2c::I2c for I2c<'d, T, TXDMA, RXDMA> {
181 async fn read(&mut self, address: u8, read: &mut [u8]) -> Result<(), Self::Error> {
182 self.read(address, read).await
183 }
184
185 async fn write(&mut self, address: u8, write: &[u8]) -> Result<(), Self::Error> {
186 self.write(address, write).await
187 }
188
189 async fn write_read(&mut self, address: u8, write: &[u8], read: &mut [u8]) -> Result<(), Self::Error> {
190 self.write_read(address, write, read).await
191 }
192
193 async fn transaction(
194 &mut self,
195 address: u8,
196 operations: &mut [embedded_hal_1::i2c::Operation<'_>],
197 ) -> Result<(), Self::Error> {
198 let _ = address;
199 let _ = operations;
200 todo!()
201 }
202 }
203}
diff --git a/embassy-stm32/src/i2c/v1.rs b/embassy-stm32/src/i2c/v1.rs
index ab59f5ab9..03f07c4fe 100644
--- a/embassy-stm32/src/i2c/v1.rs
+++ b/embassy-stm32/src/i2c/v1.rs
@@ -3,21 +3,17 @@ use core::marker::PhantomData;
3use embassy_embedded_hal::SetConfig; 3use embassy_embedded_hal::SetConfig;
4use embassy_hal_internal::{into_ref, PeripheralRef}; 4use embassy_hal_internal::{into_ref, PeripheralRef};
5 5
6use super::*;
6use crate::dma::NoDma; 7use crate::dma::NoDma;
7use crate::gpio::sealed::AFType; 8use crate::gpio::sealed::AFType;
8use crate::gpio::Pull; 9use crate::gpio::Pull;
9use crate::i2c::{Error, Instance, SclPin, SdaPin}; 10use crate::interrupt::typelevel::Interrupt;
10use crate::pac::i2c; 11use crate::pac::i2c;
11use crate::time::Hertz; 12use crate::time::Hertz;
12use crate::{interrupt, Peripheral}; 13use crate::{interrupt, Peripheral};
13 14
14/// Interrupt handler. 15pub unsafe fn on_interrupt<T: Instance>() {
15pub struct InterruptHandler<T: Instance> { 16 // todo
16 _phantom: PhantomData<T>,
17}
18
19impl<T: Instance> interrupt::typelevel::Handler<T::Interrupt> for InterruptHandler<T> {
20 unsafe fn on_interrupt() {}
21} 17}
22 18
23#[non_exhaustive] 19#[non_exhaustive]
@@ -27,14 +23,6 @@ pub struct Config {
27 pub scl_pullup: bool, 23 pub scl_pullup: bool,
28} 24}
29 25
30pub struct State {}
31
32impl State {
33 pub(crate) const fn new() -> Self {
34 Self {}
35 }
36}
37
38pub struct I2c<'d, T: Instance, TXDMA = NoDma, RXDMA = NoDma> { 26pub struct I2c<'d, T: Instance, TXDMA = NoDma, RXDMA = NoDma> {
39 phantom: PhantomData<&'d mut T>, 27 phantom: PhantomData<&'d mut T>,
40 #[allow(dead_code)] 28 #[allow(dead_code)]
@@ -48,7 +36,9 @@ impl<'d, T: Instance, TXDMA, RXDMA> I2c<'d, T, TXDMA, RXDMA> {
48 _peri: impl Peripheral<P = T> + 'd, 36 _peri: impl Peripheral<P = T> + 'd,
49 scl: impl Peripheral<P = impl SclPin<T>> + 'd, 37 scl: impl Peripheral<P = impl SclPin<T>> + 'd,
50 sda: impl Peripheral<P = impl SdaPin<T>> + 'd, 38 sda: impl Peripheral<P = impl SdaPin<T>> + 'd,
51 _irq: impl interrupt::typelevel::Binding<T::Interrupt, InterruptHandler<T>> + 'd, 39 _irq: impl interrupt::typelevel::Binding<T::EventInterrupt, EventInterruptHandler<T>>
40 + interrupt::typelevel::Binding<T::ErrorInterrupt, ErrorInterruptHandler<T>>
41 + 'd,
52 tx_dma: impl Peripheral<P = TXDMA> + 'd, 42 tx_dma: impl Peripheral<P = TXDMA> + 'd,
53 rx_dma: impl Peripheral<P = RXDMA> + 'd, 43 rx_dma: impl Peripheral<P = RXDMA> + 'd,
54 freq: Hertz, 44 freq: Hertz,
@@ -98,6 +88,9 @@ impl<'d, T: Instance, TXDMA, RXDMA> I2c<'d, T, TXDMA, RXDMA> {
98 reg.set_pe(true); 88 reg.set_pe(true);
99 }); 89 });
100 90
91 unsafe { T::EventInterrupt::enable() };
92 unsafe { T::ErrorInterrupt::enable() };
93
101 Self { 94 Self {
102 phantom: PhantomData, 95 phantom: PhantomData,
103 tx_dma, 96 tx_dma,
@@ -336,82 +329,35 @@ impl<'d, T: Instance, TXDMA, RXDMA> I2c<'d, T, TXDMA, RXDMA> {
336 pub fn blocking_write_read(&mut self, addr: u8, write: &[u8], read: &mut [u8]) -> Result<(), Error> { 329 pub fn blocking_write_read(&mut self, addr: u8, write: &[u8], read: &mut [u8]) -> Result<(), Error> {
337 self.blocking_write_read_timeout(addr, write, read, || Ok(())) 330 self.blocking_write_read_timeout(addr, write, read, || Ok(()))
338 } 331 }
339}
340
341impl<'d, T: Instance, TXDMA, RXDMA> Drop for I2c<'d, T, TXDMA, RXDMA> {
342 fn drop(&mut self) {
343 T::disable();
344 }
345}
346 332
347impl<'d, T: Instance> embedded_hal_02::blocking::i2c::Read for I2c<'d, T> { 333 // Async
348 type Error = Error;
349 334
350 fn read(&mut self, addr: u8, read: &mut [u8]) -> Result<(), Self::Error> { 335 pub async fn write(&mut self, _address: u8, _write: &[u8]) -> Result<(), Error>
351 self.blocking_read(addr, read) 336 where
337 TXDMA: crate::i2c::TxDma<T>,
338 {
339 todo!()
352 } 340 }
353}
354
355impl<'d, T: Instance> embedded_hal_02::blocking::i2c::Write for I2c<'d, T> {
356 type Error = Error;
357 341
358 fn write(&mut self, addr: u8, write: &[u8]) -> Result<(), Self::Error> { 342 pub async fn read(&mut self, _address: u8, _buffer: &mut [u8]) -> Result<(), Error>
359 self.blocking_write(addr, write) 343 where
344 RXDMA: crate::i2c::RxDma<T>,
345 {
346 todo!()
360 } 347 }
361}
362
363impl<'d, T: Instance> embedded_hal_02::blocking::i2c::WriteRead for I2c<'d, T> {
364 type Error = Error;
365 348
366 fn write_read(&mut self, addr: u8, write: &[u8], read: &mut [u8]) -> Result<(), Self::Error> { 349 pub async fn write_read(&mut self, _address: u8, _write: &[u8], _read: &mut [u8]) -> Result<(), Error>
367 self.blocking_write_read(addr, write, read) 350 where
351 RXDMA: crate::i2c::RxDma<T>,
352 TXDMA: crate::i2c::TxDma<T>,
353 {
354 todo!()
368 } 355 }
369} 356}
370 357
371#[cfg(feature = "unstable-traits")] 358impl<'d, T: Instance, TXDMA, RXDMA> Drop for I2c<'d, T, TXDMA, RXDMA> {
372mod eh1 { 359 fn drop(&mut self) {
373 use super::*; 360 T::disable();
374
375 impl embedded_hal_1::i2c::Error for Error {
376 fn kind(&self) -> embedded_hal_1::i2c::ErrorKind {
377 match *self {
378 Self::Bus => embedded_hal_1::i2c::ErrorKind::Bus,
379 Self::Arbitration => embedded_hal_1::i2c::ErrorKind::ArbitrationLoss,
380 Self::Nack => {
381 embedded_hal_1::i2c::ErrorKind::NoAcknowledge(embedded_hal_1::i2c::NoAcknowledgeSource::Unknown)
382 }
383 Self::Timeout => embedded_hal_1::i2c::ErrorKind::Other,
384 Self::Crc => embedded_hal_1::i2c::ErrorKind::Other,
385 Self::Overrun => embedded_hal_1::i2c::ErrorKind::Overrun,
386 Self::ZeroLengthTransfer => embedded_hal_1::i2c::ErrorKind::Other,
387 }
388 }
389 }
390
391 impl<'d, T: Instance> embedded_hal_1::i2c::ErrorType for I2c<'d, T> {
392 type Error = Error;
393 }
394
395 impl<'d, T: Instance> embedded_hal_1::i2c::I2c for I2c<'d, T> {
396 fn read(&mut self, address: u8, read: &mut [u8]) -> Result<(), Self::Error> {
397 self.blocking_read(address, read)
398 }
399
400 fn write(&mut self, address: u8, write: &[u8]) -> Result<(), Self::Error> {
401 self.blocking_write(address, write)
402 }
403
404 fn write_read(&mut self, address: u8, write: &[u8], read: &mut [u8]) -> Result<(), Self::Error> {
405 self.blocking_write_read(address, write, read)
406 }
407
408 fn transaction(
409 &mut self,
410 _address: u8,
411 _operations: &mut [embedded_hal_1::i2c::Operation<'_>],
412 ) -> Result<(), Self::Error> {
413 todo!();
414 }
415 } 361 }
416} 362}
417 363
diff --git a/embassy-stm32/src/i2c/v2.rs b/embassy-stm32/src/i2c/v2.rs
index 40bcaa9bc..8c20e1c54 100644
--- a/embassy-stm32/src/i2c/v2.rs
+++ b/embassy-stm32/src/i2c/v2.rs
@@ -1,19 +1,17 @@
1use core::cmp; 1use core::cmp;
2use core::future::poll_fn; 2use core::future::poll_fn;
3use core::marker::PhantomData;
4use core::task::Poll; 3use core::task::Poll;
5 4
6use embassy_embedded_hal::SetConfig; 5use embassy_embedded_hal::SetConfig;
7use embassy_hal_internal::drop::OnDrop; 6use embassy_hal_internal::drop::OnDrop;
8use embassy_hal_internal::{into_ref, PeripheralRef}; 7use embassy_hal_internal::{into_ref, PeripheralRef};
9use embassy_sync::waitqueue::AtomicWaker;
10#[cfg(feature = "time")] 8#[cfg(feature = "time")]
11use embassy_time::{Duration, Instant}; 9use embassy_time::{Duration, Instant};
12 10
11use super::*;
13use crate::dma::{NoDma, Transfer}; 12use crate::dma::{NoDma, Transfer};
14use crate::gpio::sealed::AFType; 13use crate::gpio::sealed::AFType;
15use crate::gpio::Pull; 14use crate::gpio::Pull;
16use crate::i2c::{Error, Instance, SclPin, SdaPin};
17use crate::interrupt::typelevel::Interrupt; 15use crate::interrupt::typelevel::Interrupt;
18use crate::pac::i2c; 16use crate::pac::i2c;
19use crate::time::Hertz; 17use crate::time::Hertz;
@@ -36,25 +34,18 @@ pub fn no_timeout_fn() -> impl Fn() -> Result<(), Error> {
36 move || Ok(()) 34 move || Ok(())
37} 35}
38 36
39/// Interrupt handler. 37pub unsafe fn on_interrupt<T: Instance>() {
40pub struct InterruptHandler<T: Instance> { 38 let regs = T::regs();
41 _phantom: PhantomData<T>, 39 let isr = regs.isr().read();
42}
43
44impl<T: Instance> interrupt::typelevel::Handler<T::Interrupt> for InterruptHandler<T> {
45 unsafe fn on_interrupt() {
46 let regs = T::regs();
47 let isr = regs.isr().read();
48 40
49 if isr.tcr() || isr.tc() { 41 if isr.tcr() || isr.tc() {
50 T::state().waker.wake(); 42 T::state().waker.wake();
51 }
52 // The flag can only be cleared by writting to nbytes, we won't do that here, so disable
53 // the interrupt
54 critical_section::with(|_| {
55 regs.cr1().modify(|w| w.set_tcie(false));
56 });
57 } 43 }
44 // The flag can only be cleared by writting to nbytes, we won't do that here, so disable
45 // the interrupt
46 critical_section::with(|_| {
47 regs.cr1().modify(|w| w.set_tcie(false));
48 });
58} 49}
59 50
60#[non_exhaustive] 51#[non_exhaustive]
@@ -77,18 +68,6 @@ impl Default for Config {
77 } 68 }
78} 69}
79 70
80pub struct State {
81 waker: AtomicWaker,
82}
83
84impl State {
85 pub(crate) const fn new() -> Self {
86 Self {
87 waker: AtomicWaker::new(),
88 }
89 }
90}
91
92pub struct I2c<'d, T: Instance, TXDMA = NoDma, RXDMA = NoDma> { 71pub struct I2c<'d, T: Instance, TXDMA = NoDma, RXDMA = NoDma> {
93 _peri: PeripheralRef<'d, T>, 72 _peri: PeripheralRef<'d, T>,
94 #[allow(dead_code)] 73 #[allow(dead_code)]
@@ -104,7 +83,9 @@ impl<'d, T: Instance, TXDMA, RXDMA> I2c<'d, T, TXDMA, RXDMA> {
104 peri: impl Peripheral<P = T> + 'd, 83 peri: impl Peripheral<P = T> + 'd,
105 scl: impl Peripheral<P = impl SclPin<T>> + 'd, 84 scl: impl Peripheral<P = impl SclPin<T>> + 'd,
106 sda: impl Peripheral<P = impl SdaPin<T>> + 'd, 85 sda: impl Peripheral<P = impl SdaPin<T>> + 'd,
107 _irq: impl interrupt::typelevel::Binding<T::Interrupt, InterruptHandler<T>> + 'd, 86 _irq: impl interrupt::typelevel::Binding<T::EventInterrupt, EventInterruptHandler<T>>
87 + interrupt::typelevel::Binding<T::ErrorInterrupt, ErrorInterruptHandler<T>>
88 + 'd,
108 tx_dma: impl Peripheral<P = TXDMA> + 'd, 89 tx_dma: impl Peripheral<P = TXDMA> + 'd,
109 rx_dma: impl Peripheral<P = RXDMA> + 'd, 90 rx_dma: impl Peripheral<P = RXDMA> + 'd,
110 freq: Hertz, 91 freq: Hertz,
@@ -150,8 +131,8 @@ impl<'d, T: Instance, TXDMA, RXDMA> I2c<'d, T, TXDMA, RXDMA> {
150 reg.set_pe(true); 131 reg.set_pe(true);
151 }); 132 });
152 133
153 T::Interrupt::unpend(); 134 unsafe { T::EventInterrupt::enable() };
154 unsafe { T::Interrupt::enable() }; 135 unsafe { T::ErrorInterrupt::enable() };
155 136
156 Self { 137 Self {
157 _peri: peri, 138 _peri: peri,
@@ -987,35 +968,6 @@ impl<'d, T: Instance, TXDMA, RXDMA> Drop for I2c<'d, T, TXDMA, RXDMA> {
987 } 968 }
988} 969}
989 970
990#[cfg(feature = "time")]
991mod eh02 {
992 use super::*;
993
994 impl<'d, T: Instance> embedded_hal_02::blocking::i2c::Read for I2c<'d, T> {
995 type Error = Error;
996
997 fn read(&mut self, address: u8, buffer: &mut [u8]) -> Result<(), Self::Error> {
998 self.blocking_read(address, buffer)
999 }
1000 }
1001
1002 impl<'d, T: Instance> embedded_hal_02::blocking::i2c::Write for I2c<'d, T> {
1003 type Error = Error;
1004
1005 fn write(&mut self, address: u8, write: &[u8]) -> Result<(), Self::Error> {
1006 self.blocking_write(address, write)
1007 }
1008 }
1009
1010 impl<'d, T: Instance> embedded_hal_02::blocking::i2c::WriteRead for I2c<'d, T> {
1011 type Error = Error;
1012
1013 fn write_read(&mut self, address: u8, write: &[u8], read: &mut [u8]) -> Result<(), Self::Error> {
1014 self.blocking_write_read(address, write, read)
1015 }
1016 }
1017}
1018
1019/// I2C Stop Configuration 971/// I2C Stop Configuration
1020/// 972///
1021/// Peripheral options for generating the STOP condition 973/// Peripheral options for generating the STOP condition
@@ -1140,83 +1092,6 @@ impl Timings {
1140 } 1092 }
1141} 1093}
1142 1094
1143#[cfg(feature = "unstable-traits")]
1144mod eh1 {
1145 use super::*;
1146
1147 impl embedded_hal_1::i2c::Error for Error {
1148 fn kind(&self) -> embedded_hal_1::i2c::ErrorKind {
1149 match *self {
1150 Self::Bus => embedded_hal_1::i2c::ErrorKind::Bus,
1151 Self::Arbitration => embedded_hal_1::i2c::ErrorKind::ArbitrationLoss,
1152 Self::Nack => {
1153 embedded_hal_1::i2c::ErrorKind::NoAcknowledge(embedded_hal_1::i2c::NoAcknowledgeSource::Unknown)
1154 }
1155 Self::Timeout => embedded_hal_1::i2c::ErrorKind::Other,
1156 Self::Crc => embedded_hal_1::i2c::ErrorKind::Other,
1157 Self::Overrun => embedded_hal_1::i2c::ErrorKind::Overrun,
1158 Self::ZeroLengthTransfer => embedded_hal_1::i2c::ErrorKind::Other,
1159 }
1160 }
1161 }
1162
1163 impl<'d, T: Instance, TXDMA, RXDMA> embedded_hal_1::i2c::ErrorType for I2c<'d, T, TXDMA, RXDMA> {
1164 type Error = Error;
1165 }
1166
1167 impl<'d, T: Instance> embedded_hal_1::i2c::I2c for I2c<'d, T, NoDma, NoDma> {
1168 fn read(&mut self, address: u8, read: &mut [u8]) -> Result<(), Self::Error> {
1169 self.blocking_read(address, read)
1170 }
1171
1172 fn write(&mut self, address: u8, write: &[u8]) -> Result<(), Self::Error> {
1173 self.blocking_write(address, write)
1174 }
1175
1176 fn write_read(&mut self, address: u8, write: &[u8], read: &mut [u8]) -> Result<(), Self::Error> {
1177 self.blocking_write_read(address, write, read)
1178 }
1179
1180 fn transaction(
1181 &mut self,
1182 _address: u8,
1183 _operations: &mut [embedded_hal_1::i2c::Operation<'_>],
1184 ) -> Result<(), Self::Error> {
1185 todo!();
1186 }
1187 }
1188}
1189
1190#[cfg(all(feature = "unstable-traits", feature = "nightly"))]
1191mod eha {
1192 use super::super::{RxDma, TxDma};
1193 use super::*;
1194
1195 impl<'d, T: Instance, TXDMA: TxDma<T>, RXDMA: RxDma<T>> embedded_hal_async::i2c::I2c for I2c<'d, T, TXDMA, RXDMA> {
1196 async fn read(&mut self, address: u8, read: &mut [u8]) -> Result<(), Self::Error> {
1197 self.read(address, read).await
1198 }
1199
1200 async fn write(&mut self, address: u8, write: &[u8]) -> Result<(), Self::Error> {
1201 self.write(address, write).await
1202 }
1203
1204 async fn write_read(&mut self, address: u8, write: &[u8], read: &mut [u8]) -> Result<(), Self::Error> {
1205 self.write_read(address, write, read).await
1206 }
1207
1208 async fn transaction(
1209 &mut self,
1210 address: u8,
1211 operations: &mut [embedded_hal_1::i2c::Operation<'_>],
1212 ) -> Result<(), Self::Error> {
1213 let _ = address;
1214 let _ = operations;
1215 todo!()
1216 }
1217 }
1218}
1219
1220impl<'d, T: Instance> SetConfig for I2c<'d, T> { 1095impl<'d, T: Instance> SetConfig for I2c<'d, T> {
1221 type Config = Hertz; 1096 type Config = Hertz;
1222 type ConfigError = (); 1097 type ConfigError = ();
diff --git a/examples/stm32f4/src/bin/i2c.rs b/examples/stm32f4/src/bin/i2c.rs
index 032bd97ee..4f4adde28 100644
--- a/examples/stm32f4/src/bin/i2c.rs
+++ b/examples/stm32f4/src/bin/i2c.rs
@@ -14,7 +14,8 @@ const ADDRESS: u8 = 0x5F;
14const WHOAMI: u8 = 0x0F; 14const WHOAMI: u8 = 0x0F;
15 15
16bind_interrupts!(struct Irqs { 16bind_interrupts!(struct Irqs {
17 I2C2_EV => i2c::InterruptHandler<peripherals::I2C2>; 17 I2C2_EV => i2c::EventInterruptHandler<peripherals::I2C2>;
18 I2C2_ER => i2c::ErrorInterruptHandler<peripherals::I2C2>;
18}); 19});
19 20
20#[embassy_executor::main] 21#[embassy_executor::main]
diff --git a/examples/stm32h5/src/bin/i2c.rs b/examples/stm32h5/src/bin/i2c.rs
index 8b1662f39..31783a2bf 100644
--- a/examples/stm32h5/src/bin/i2c.rs
+++ b/examples/stm32h5/src/bin/i2c.rs
@@ -13,7 +13,8 @@ const ADDRESS: u8 = 0x5F;
13const WHOAMI: u8 = 0x0F; 13const WHOAMI: u8 = 0x0F;
14 14
15bind_interrupts!(struct Irqs { 15bind_interrupts!(struct Irqs {
16 I2C2_EV => i2c::InterruptHandler<peripherals::I2C2>; 16 I2C2_EV => i2c::EventInterruptHandler<peripherals::I2C2>;
17 I2C2_ER => i2c::ErrorInterruptHandler<peripherals::I2C2>;
17}); 18});
18 19
19#[embassy_executor::main] 20#[embassy_executor::main]
diff --git a/examples/stm32h7/src/bin/camera.rs b/examples/stm32h7/src/bin/camera.rs
index 23ece1c38..489fb03dd 100644
--- a/examples/stm32h7/src/bin/camera.rs
+++ b/examples/stm32h7/src/bin/camera.rs
@@ -19,7 +19,8 @@ const HEIGHT: usize = 100;
19static mut FRAME: [u32; WIDTH * HEIGHT / 2] = [0u32; WIDTH * HEIGHT / 2]; 19static mut FRAME: [u32; WIDTH * HEIGHT / 2] = [0u32; WIDTH * HEIGHT / 2];
20 20
21bind_interrupts!(struct Irqs { 21bind_interrupts!(struct Irqs {
22 I2C1_EV => i2c::InterruptHandler<peripherals::I2C1>; 22 I2C1_EV => i2c::EventInterruptHandler<peripherals::I2C1>;
23 I2C1_ER => i2c::ErrorInterruptHandler<peripherals::I2C1>;
23 DCMI => dcmi::InterruptHandler<peripherals::DCMI>; 24 DCMI => dcmi::InterruptHandler<peripherals::DCMI>;
24}); 25});
25 26
diff --git a/examples/stm32h7/src/bin/i2c.rs b/examples/stm32h7/src/bin/i2c.rs
index 9aa0ca08b..aea21ec6f 100644
--- a/examples/stm32h7/src/bin/i2c.rs
+++ b/examples/stm32h7/src/bin/i2c.rs
@@ -13,7 +13,8 @@ const ADDRESS: u8 = 0x5F;
13const WHOAMI: u8 = 0x0F; 13const WHOAMI: u8 = 0x0F;
14 14
15bind_interrupts!(struct Irqs { 15bind_interrupts!(struct Irqs {
16 I2C2_EV => i2c::InterruptHandler<peripherals::I2C2>; 16 I2C2_EV => i2c::EventInterruptHandler<peripherals::I2C2>;
17 I2C2_ER => i2c::ErrorInterruptHandler<peripherals::I2C2>;
17}); 18});
18 19
19#[embassy_executor::main] 20#[embassy_executor::main]
diff --git a/examples/stm32l4/src/bin/i2c.rs b/examples/stm32l4/src/bin/i2c.rs
index d0060d20c..07dc12e8c 100644
--- a/examples/stm32l4/src/bin/i2c.rs
+++ b/examples/stm32l4/src/bin/i2c.rs
@@ -14,7 +14,8 @@ const ADDRESS: u8 = 0x5F;
14const WHOAMI: u8 = 0x0F; 14const WHOAMI: u8 = 0x0F;
15 15
16bind_interrupts!(struct Irqs { 16bind_interrupts!(struct Irqs {
17 I2C2_EV => i2c::InterruptHandler<peripherals::I2C2>; 17 I2C2_EV => i2c::EventInterruptHandler<peripherals::I2C2>;
18 I2C2_ER => i2c::ErrorInterruptHandler<peripherals::I2C2>;
18}); 19});
19 20
20#[embassy_executor::main] 21#[embassy_executor::main]
diff --git a/examples/stm32l4/src/bin/i2c_blocking_async.rs b/examples/stm32l4/src/bin/i2c_blocking_async.rs
index eca59087b..60a4e2eb3 100644
--- a/examples/stm32l4/src/bin/i2c_blocking_async.rs
+++ b/examples/stm32l4/src/bin/i2c_blocking_async.rs
@@ -16,7 +16,8 @@ const ADDRESS: u8 = 0x5F;
16const WHOAMI: u8 = 0x0F; 16const WHOAMI: u8 = 0x0F;
17 17
18bind_interrupts!(struct Irqs { 18bind_interrupts!(struct Irqs {
19 I2C2_EV => i2c::InterruptHandler<peripherals::I2C2>; 19 I2C2_EV => i2c::EventInterruptHandler<peripherals::I2C2>;
20 I2C2_ER => i2c::ErrorInterruptHandler<peripherals::I2C2>;
20}); 21});
21 22
22#[embassy_executor::main] 23#[embassy_executor::main]
diff --git a/examples/stm32l4/src/bin/i2c_dma.rs b/examples/stm32l4/src/bin/i2c_dma.rs
index cf6f3da67..4c2c224a6 100644
--- a/examples/stm32l4/src/bin/i2c_dma.rs
+++ b/examples/stm32l4/src/bin/i2c_dma.rs
@@ -13,7 +13,8 @@ const ADDRESS: u8 = 0x5F;
13const WHOAMI: u8 = 0x0F; 13const WHOAMI: u8 = 0x0F;
14 14
15bind_interrupts!(struct Irqs { 15bind_interrupts!(struct Irqs {
16 I2C2_EV => i2c::InterruptHandler<peripherals::I2C2>; 16 I2C2_EV => i2c::EventInterruptHandler<peripherals::I2C2>;
17 I2C2_ER => i2c::ErrorInterruptHandler<peripherals::I2C2>;
17}); 18});
18 19
19#[embassy_executor::main] 20#[embassy_executor::main]
diff --git a/examples/stm32l4/src/bin/spe_adin1110_http_server.rs b/examples/stm32l4/src/bin/spe_adin1110_http_server.rs
index 3a7e5370c..4826e0bed 100644
--- a/examples/stm32l4/src/bin/spe_adin1110_http_server.rs
+++ b/examples/stm32l4/src/bin/spe_adin1110_http_server.rs
@@ -40,7 +40,8 @@ use static_cell::make_static;
40use {embassy_stm32 as hal, panic_probe as _}; 40use {embassy_stm32 as hal, panic_probe as _};
41 41
42bind_interrupts!(struct Irqs { 42bind_interrupts!(struct Irqs {
43 I2C3_EV => i2c::InterruptHandler<peripherals::I2C3>; 43 I2C3_EV => i2c::EventInterruptHandler<peripherals::I2C3>;
44 I2C3_ER => i2c::ErrorInterruptHandler<peripherals::I2C3>;
44 RNG => rng::InterruptHandler<peripherals::RNG>; 45 RNG => rng::InterruptHandler<peripherals::RNG>;
45}); 46});
46 47