Skip to content

Commit

Permalink
Use embassy for the shared i2c bus
Browse files Browse the repository at this point in the history
Move from embedded-hal-bus to embassy-embedded-hal for the purposes of
implementing an async shared i2c bus. This also drops the need for a
patched embedded-hal-bus, which is unlikely to go upstream[^0]

[^0]: rust-embedded/embedded-hal#600
Signed-off-by: Sjoerd Simons <sjoerd@collabora.com>
  • Loading branch information
sjoerdsimons committed May 9, 2024
1 parent a4312e6 commit ea7dcab
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 28 deletions.
19 changes: 1 addition & 18 deletions stm32g071b-disco-firmware/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 1 addition & 2 deletions stm32g071b-disco-firmware/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,13 @@ cortex-m = { version = "0.7.7", features = ["critical-section-single-core"] }
cortex-m-rt = "0.7.4"
defmt = "0.3.6"
defmt-rtt = "0.4.0"
embassy-embedded-hal = { git = "https://github.com/embassy-rs/embassy", version = "0.1.0" }
embassy-executor = { version = "0.5.0", features = ["arch-cortex-m", "executor-thread", "defmt", "integrated-timers"], git = "https://github.com/embassy-rs/embassy" }
embassy-futures = "0.1.1"
embassy-stm32 = { version = "0.1.0", features = ["stm32g071rb", "defmt", "memory-x", "time-driver-any", "exti"] , git = "https://github.com/embassy-rs/embassy" }
embassy-sync = { git = "https://github.com/embassy-rs/embassy", version = "0.5.0" }
embassy-time = { version = "0.3.0", git = "https://github.com/embassy-rs/embassy" }
embedded-hal = "1.0.0"
embedded-hal-bus = { version = "0.2.0", features = ["async"], git = "https://github.com/sjoerdsimons/embedded-hal", branch = "i2c-ref-async+fix-armv6-build" }
#embedded-hal-bus = { version = "0.2.0", features = ["async"], path = "../../embedded-hal/embedded-hal-bus" }
embedded-io-async = "0.6.1"
ina226 = { version = "0.2.0", features = ["async"], git = "https://github.com/sjoerdsimons/ina226", branch = "asyncify" }
minicbor = "0.24.0"
Expand Down
14 changes: 6 additions & 8 deletions stm32g071b-disco-firmware/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
#![no_main]
#![no_std]
use core::cell::RefCell;

use analyzer_cbor::AnalyserData;
use byteorder::{ByteOrder, LittleEndian};
use defmt::info;
use defmt_rtt as _;
use embassy_embedded_hal::shared_bus::asynch::i2c::I2cDevice;
use embassy_executor::Spawner;
use embassy_futures::select::select_array;
use embassy_stm32::{
Expand All @@ -20,7 +19,7 @@ use embassy_stm32::{
ucpd::{self, CcSel},
usart::{self, BufferedUart},
};
use embassy_sync::{blocking_mutex::raw::ThreadModeRawMutex, channel::Channel};
use embassy_sync::{blocking_mutex::raw::{NoopRawMutex, ThreadModeRawMutex}, channel::Channel, mutex::Mutex};
use embassy_time::Timer;
use embedded_io_async::Write;
use minicbor::encode::write::Cursor;
Expand Down Expand Up @@ -101,17 +100,16 @@ async fn serial_output(uart: USART3, tx: PC11, rx: PC10) {

#[embassy_executor::task]
async fn sensor_monitor(i2c: I2c<'static, peripherals::I2C1, Async>) {
let i2c = RefCell::new(i2c);
let mut vbus = ina226::INA226::new(embedded_hal_bus::i2c::RefCellDevice::new(&i2c), 0x40);

let i2c: Mutex<NoopRawMutex,_> = Mutex::new(i2c);
let mut vbus = ina226::INA226::new(I2cDevice::new(&i2c), 0x40);
vbus.callibrate(0.015, 5.0)
.await
.expect("Failed to calibrate vbus");
let mut cc1 = ina226::INA226::new(embedded_hal_bus::i2c::RefCellDevice::new(&i2c), 0x41);
let mut cc1 = ina226::INA226::new(I2cDevice::new(&i2c), 0x41);
cc1.callibrate(0.015, 5.0)
.await
.expect("Failed to calibrate cc1");
let mut cc2 = ina226::INA226::new(embedded_hal_bus::i2c::RefCellDevice::new(&i2c), 0x42);
let mut cc2 = ina226::INA226::new(I2cDevice::new(&i2c), 0x42);
cc2.callibrate(0.015, 5.0)
.await
.expect("Failed to calibrate cc2");
Expand Down

0 comments on commit ea7dcab

Please sign in to comment.