diff options
| author | Felipe Balbi <[email protected]> | 2024-05-16 08:12:18 -0700 |
|---|---|---|
| committer | Felipe Balbi <[email protected]> | 2024-05-16 09:38:13 -0700 |
| commit | 3ef62eef5370b34792a737dd5a4b296fe3fd8c7d (patch) | |
| tree | e4578efdb2b43656e3d714791f0c86c62e9d9a01 /examples/stm32h7 | |
| parent | ea70b440cd1035f28c3f332a2f72d7fa42ac995d (diff) | |
stm32h7: add shared bus example
Diffstat (limited to 'examples/stm32h7')
| -rw-r--r-- | examples/stm32h7/Cargo.toml | 1 | ||||
| -rw-r--r-- | examples/stm32h7/src/bin/i2c_shared.rs | 112 |
2 files changed, 113 insertions, 0 deletions
diff --git a/examples/stm32h7/Cargo.toml b/examples/stm32h7/Cargo.toml index a09af4b97..c6c2d1354 100644 --- a/examples/stm32h7/Cargo.toml +++ b/examples/stm32h7/Cargo.toml | |||
| @@ -8,6 +8,7 @@ license = "MIT OR Apache-2.0" | |||
| 8 | # Change stm32h743bi to your chip name, if necessary. | 8 | # Change stm32h743bi to your chip name, if necessary. |
| 9 | embassy-stm32 = { version = "0.1.0", path = "../../embassy-stm32", features = ["defmt", "stm32h743bi", "time-driver-tim2", "exti", "memory-x", "unstable-pac", "chrono"] } | 9 | embassy-stm32 = { version = "0.1.0", path = "../../embassy-stm32", features = ["defmt", "stm32h743bi", "time-driver-tim2", "exti", "memory-x", "unstable-pac", "chrono"] } |
| 10 | embassy-sync = { version = "0.5.0", path = "../../embassy-sync", features = ["defmt"] } | 10 | embassy-sync = { version = "0.5.0", path = "../../embassy-sync", features = ["defmt"] } |
| 11 | embassy-embedded-hal = { version = "0.1.0", path = "../../embassy-embedded-hal" } | ||
| 11 | embassy-executor = { version = "0.5.0", path = "../../embassy-executor", features = ["task-arena-size-32768", "arch-cortex-m", "executor-thread", "executor-interrupt", "defmt", "integrated-timers"] } | 12 | embassy-executor = { version = "0.5.0", path = "../../embassy-executor", features = ["task-arena-size-32768", "arch-cortex-m", "executor-thread", "executor-interrupt", "defmt", "integrated-timers"] } |
| 12 | embassy-time = { version = "0.3.0", path = "../../embassy-time", features = ["defmt", "defmt-timestamp-uptime", "tick-hz-32_768"] } | 13 | embassy-time = { version = "0.3.0", path = "../../embassy-time", features = ["defmt", "defmt-timestamp-uptime", "tick-hz-32_768"] } |
| 13 | embassy-net = { version = "0.4.0", path = "../../embassy-net", features = ["defmt", "tcp", "dhcpv4", "medium-ethernet", "proto-ipv6", "dns"] } | 14 | embassy-net = { version = "0.4.0", path = "../../embassy-net", features = ["defmt", "tcp", "dhcpv4", "medium-ethernet", "proto-ipv6", "dns"] } |
diff --git a/examples/stm32h7/src/bin/i2c_shared.rs b/examples/stm32h7/src/bin/i2c_shared.rs new file mode 100644 index 000000000..79d213ae4 --- /dev/null +++ b/examples/stm32h7/src/bin/i2c_shared.rs | |||
| @@ -0,0 +1,112 @@ | |||
| 1 | #![no_std] | ||
| 2 | #![no_main] | ||
| 3 | |||
| 4 | use core::cell::RefCell; | ||
| 5 | |||
| 6 | use defmt::*; | ||
| 7 | use embassy_embedded_hal::shared_bus::blocking::i2c::I2cDevice; | ||
| 8 | use embassy_executor::Spawner; | ||
| 9 | use embassy_stm32::bind_interrupts; | ||
| 10 | use embassy_stm32::i2c::{self, I2c}; | ||
| 11 | use embassy_stm32::mode::Async; | ||
| 12 | use embassy_stm32::peripherals::{self, I2C1}; | ||
| 13 | use embassy_stm32::time::Hertz; | ||
| 14 | use embassy_sync::blocking_mutex::NoopMutex; | ||
| 15 | use embassy_time::{Duration, Timer}; | ||
| 16 | use static_cell::StaticCell; | ||
| 17 | use {defmt_rtt as _, panic_probe as _}; | ||
| 18 | |||
| 19 | const TMP117_ADDR: u8 = 0x48; | ||
| 20 | const TMP117_TEMP_RESULT: u8 = 0x00; | ||
| 21 | |||
| 22 | const SHTC3_ADDR: u8 = 0x70; | ||
| 23 | const SHTC3_WAKEUP: [u8; 2] = [0x35, 0x17]; | ||
| 24 | const SHTC3_MEASURE_RH_FIRST: [u8; 2] = [0x5c, 0x24]; | ||
| 25 | const SHTC3_SLEEP: [u8; 2] = [0xb0, 0x98]; | ||
| 26 | |||
| 27 | static I2C_BUS: StaticCell<NoopMutex<RefCell<I2c<'static, I2C1, Async>>>> = StaticCell::new(); | ||
| 28 | |||
| 29 | bind_interrupts!(struct Irqs { | ||
| 30 | I2C1_EV => i2c::EventInterruptHandler<peripherals::I2C1>; | ||
| 31 | I2C1_ER => i2c::ErrorInterruptHandler<peripherals::I2C1>; | ||
| 32 | }); | ||
| 33 | |||
| 34 | #[embassy_executor::task] | ||
| 35 | async fn temperature(mut i2c: impl embedded_hal_1::i2c::I2c + 'static) { | ||
| 36 | let mut data = [0u8; 2]; | ||
| 37 | |||
| 38 | loop { | ||
| 39 | match i2c.write_read(TMP117_ADDR, &[TMP117_TEMP_RESULT], &mut data) { | ||
| 40 | Ok(()) => { | ||
| 41 | let temp = f32::from(i16::from_be_bytes(data)) * 7.8125 / 1000.0; | ||
| 42 | info!("Temperature {}", temp); | ||
| 43 | } | ||
| 44 | Err(_) => error!("I2C Error"), | ||
| 45 | } | ||
| 46 | |||
| 47 | Timer::after(Duration::from_millis(1000)).await; | ||
| 48 | } | ||
| 49 | } | ||
| 50 | |||
| 51 | #[embassy_executor::task] | ||
| 52 | async fn humidity(mut i2c: impl embedded_hal_1::i2c::I2c + 'static) { | ||
| 53 | let mut data = [0u8; 6]; | ||
| 54 | |||
| 55 | loop { | ||
| 56 | // Wakeup | ||
| 57 | match i2c.write(SHTC3_ADDR, &SHTC3_WAKEUP) { | ||
| 58 | Ok(()) => Timer::after(Duration::from_millis(20)).await, | ||
| 59 | Err(_) => error!("I2C Error"), | ||
| 60 | } | ||
| 61 | |||
| 62 | // Measurement | ||
| 63 | match i2c.write(SHTC3_ADDR, &SHTC3_MEASURE_RH_FIRST) { | ||
| 64 | Ok(()) => Timer::after(Duration::from_millis(5)).await, | ||
| 65 | Err(_) => error!("I2C Error"), | ||
| 66 | } | ||
| 67 | |||
| 68 | // Result | ||
| 69 | match i2c.read(SHTC3_ADDR, &mut data) { | ||
| 70 | Ok(()) => Timer::after(Duration::from_millis(5)).await, | ||
| 71 | Err(_) => error!("I2C Error"), | ||
| 72 | } | ||
| 73 | |||
| 74 | // Sleep | ||
| 75 | match i2c.write(SHTC3_ADDR, &SHTC3_SLEEP) { | ||
| 76 | Ok(()) => { | ||
| 77 | let (bytes, _) = data.split_at(core::mem::size_of::<i16>()); | ||
| 78 | let rh = f32::from(u16::from_be_bytes(bytes.try_into().unwrap())) * 100.0 / 65536.0; | ||
| 79 | info!("Humidity: {}", rh); | ||
| 80 | } | ||
| 81 | Err(_) => error!("I2C Error"), | ||
| 82 | } | ||
| 83 | |||
| 84 | Timer::after(Duration::from_millis(1000)).await; | ||
| 85 | } | ||
| 86 | } | ||
| 87 | |||
| 88 | #[embassy_executor::main] | ||
| 89 | async fn main(spawner: Spawner) { | ||
| 90 | let p = embassy_stm32::init(Default::default()); | ||
| 91 | |||
| 92 | let i2c = I2c::new( | ||
| 93 | p.I2C1, | ||
| 94 | p.PB8, | ||
| 95 | p.PB9, | ||
| 96 | Irqs, | ||
| 97 | p.DMA1_CH4, | ||
| 98 | p.DMA1_CH5, | ||
| 99 | Hertz(100_000), | ||
| 100 | Default::default(), | ||
| 101 | ); | ||
| 102 | let i2c_bus = NoopMutex::new(RefCell::new(i2c)); | ||
| 103 | let i2c_bus = I2C_BUS.init(i2c_bus); | ||
| 104 | |||
| 105 | // Device 1, using embedded-hal-async compatible driver for TMP117 | ||
| 106 | let i2c_dev1 = I2cDevice::new(i2c_bus); | ||
| 107 | spawner.spawn(temperature(i2c_dev1)).unwrap(); | ||
| 108 | |||
| 109 | // Device 2, using embedded-hal-async compatible driver for SHTC3 | ||
| 110 | let i2c_dev2 = I2cDevice::new(i2c_bus); | ||
| 111 | spawner.spawn(humidity(i2c_dev2)).unwrap(); | ||
| 112 | } | ||
