aboutsummaryrefslogtreecommitdiff
path: root/examples
diff options
context:
space:
mode:
authorbors[bot] <26634292+bors[bot]@users.noreply.github.com>2022-10-26 19:34:43 +0000
committerGitHub <[email protected]>2022-10-26 19:34:43 +0000
commit01e23bf9dd2f7d55a01d5a0ac18bc6f26ca05e30 (patch)
tree549f0565d84360ccb1fd076d31e7a4dfd930bf15 /examples
parente5097a8866c071c8b986757543a723b20b67fa03 (diff)
parent52c03cf0a4ae5a7a6374e6acac123670b83860fe (diff)
Merge #1025
1025: Implement I2C timeouts, second attempt r=Dirbaio a=chemicstry This is an alterrnative to #1022 as discussed there. Timeouts are implemented using suggested `check_timeout: impl Fn() -> Result<(), Error>` function, which does not depend on `embassy-time` by default and is a noop for regular I2C. This also adds `time` feature like in `embassy-nrf` to enable `embassy-time` dependencies. While at it, I also gated some other peripherals that depend on `embassy-time`, notably `usb` and (partially) `subghz`. `TimeoutI2c` is currently only implemented for i2cv1, because i2cv2 has additional complications: - Async methods still use a lot of busy waiting code in between DMA transfers, so simple `with_timeout()` will not work and it will have to use both types of timeouts. It could probably be rewritten to replace busy waits with IRQs, but that's outside the scope of this PR. - I2C definition `I2c<'d, T, TXDMA, RXDMA>` is different from i2cv1 `I2c<'d, T>` making it hard to share single `TimeoutI2c` wrapper. A couple of options here: - Duplicate `TimeoutI2c` code - Add dummy `TXDMA`, `RXDMA` types to i2cv1 considering that in the future it should also support DMA Co-authored-by: chemicstry <[email protected]>
Diffstat (limited to 'examples')
-rw-r--r--examples/stm32f4/src/bin/i2c.rs45
-rw-r--r--examples/stm32h7/src/bin/i2c.rs44
2 files changed, 89 insertions, 0 deletions
diff --git a/examples/stm32f4/src/bin/i2c.rs b/examples/stm32f4/src/bin/i2c.rs
new file mode 100644
index 000000000..6e51c211d
--- /dev/null
+++ b/examples/stm32f4/src/bin/i2c.rs
@@ -0,0 +1,45 @@
1#![no_std]
2#![no_main]
3#![feature(type_alias_impl_trait)]
4
5use defmt::*;
6use embassy_executor::Spawner;
7use embassy_stm32::dma::NoDma;
8use embassy_stm32::i2c::{Error, I2c, TimeoutI2c};
9use embassy_stm32::interrupt;
10use embassy_stm32::time::Hertz;
11use embassy_time::Duration;
12use {defmt_rtt as _, panic_probe as _};
13
14const ADDRESS: u8 = 0x5F;
15const WHOAMI: u8 = 0x0F;
16
17#[embassy_executor::main]
18async fn main(_spawner: Spawner) -> ! {
19 info!("Hello world!");
20 let p = embassy_stm32::init(Default::default());
21
22 let irq = interrupt::take!(I2C2_EV);
23 let mut i2c = I2c::new(
24 p.I2C2,
25 p.PB10,
26 p.PB11,
27 irq,
28 NoDma,
29 NoDma,
30 Hertz(100_000),
31 Default::default(),
32 );
33
34 // I2C bus can freeze if SCL line is shorted or due to a broken device that clock stretches for too long.
35 // TimeoutI2c allows recovering from such errors by throwing `Error::Timeout` after a given delay.
36 let mut timeout_i2c = TimeoutI2c::new(&mut i2c, Duration::from_millis(1000));
37
38 let mut data = [0u8; 1];
39
40 match timeout_i2c.blocking_write_read(ADDRESS, &[WHOAMI], &mut data) {
41 Ok(()) => info!("Whoami: {}", data[0]),
42 Err(Error::Timeout) => error!("Operation timed out"),
43 Err(e) => error!("I2c Error: {:?}", e),
44 }
45}
diff --git a/examples/stm32h7/src/bin/i2c.rs b/examples/stm32h7/src/bin/i2c.rs
new file mode 100644
index 000000000..d44319ae6
--- /dev/null
+++ b/examples/stm32h7/src/bin/i2c.rs
@@ -0,0 +1,44 @@
1#![no_std]
2#![no_main]
3#![feature(type_alias_impl_trait)]
4
5use defmt::*;
6use embassy_executor::Spawner;
7use embassy_stm32::i2c::{Error, I2c, TimeoutI2c};
8use embassy_stm32::interrupt;
9use embassy_stm32::time::Hertz;
10use embassy_time::Duration;
11use {defmt_rtt as _, panic_probe as _};
12
13const ADDRESS: u8 = 0x5F;
14const WHOAMI: u8 = 0x0F;
15
16#[embassy_executor::main]
17async fn main(_spawner: Spawner) -> ! {
18 info!("Hello world!");
19 let p = embassy_stm32::init(Default::default());
20
21 let irq = interrupt::take!(I2C2_EV);
22 let mut i2c = I2c::new(
23 p.I2C2,
24 p.PB10,
25 p.PB11,
26 irq,
27 p.DMA1_CH4,
28 p.DMA1_CH5,
29 Hertz(100_000),
30 Default::default(),
31 );
32
33 // I2C bus can freeze if SCL line is shorted or due to a broken device that clock stretches for too long.
34 // TimeoutI2c allows recovering from such errors by throwing `Error::Timeout` after a given delay.
35 let mut timeout_i2c = TimeoutI2c::new(&mut i2c, Duration::from_millis(1000));
36
37 let mut data = [0u8; 1];
38
39 match timeout_i2c.blocking_write_read(ADDRESS, &[WHOAMI], &mut data) {
40 Ok(()) => info!("Whoami: {}", data[0]),
41 Err(Error::Timeout) => error!("Operation timed out"),
42 Err(e) => error!("I2c Error: {:?}", e),
43 }
44}