Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ch32v003 i2c shared example #81

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions examples/ch32v003/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,15 @@ ch32-hal = { path = "../../", features = [
"time-driver-tim2",
"rt",
] }

embassy-embedded-hal = "0.2.0"
embassy-executor = { version = "0.6.0", features = [
"integrated-timers",
"arch-spin",
"executor-thread",
"task-arena-size-128", # or better use nightly, but fails on recent Rust versions
] }
embassy-sync = "0.6.0"
embassy-time = { version = "0.3.0" }

# This is okay because we should automatically use whatever ch32-hal uses
Expand All @@ -31,6 +34,7 @@ embedded-hal = "1.0.0"
display-interface-spi = "0.5.0"
embedded-graphics = "0.8.1"
micromath = { version = "2.1.0", features = ["num-traits"] }
embedded-hal-async = "1.0.0"

[profile.release]
strip = false # symbols are not flashed to the microcontroller, so don't strip them.
Expand Down
73 changes: 73 additions & 0 deletions examples/ch32v003/src/bin/embassy_i2c_shared_bus.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
//! This example shows how to share an async I2C bus between multiple devices with embassy.
//! This allows you to have multiple devices on the same bus.
#![no_std]
#![no_main]
#![feature(type_alias_impl_trait)]

use ch32_hal::gpio::{Level, Output};
use ch32_hal::i2c::I2c;
use ch32_hal::time::Hertz;
use ch32_hal::{bind_interrupts, println};
use embassy_embedded_hal::shared_bus::asynch::i2c::I2cDevice;
use embassy_executor::Spawner;
use embassy_sync::blocking_mutex::raw::NoopRawMutex;
use embassy_sync::mutex::Mutex;
use embassy_time::Timer;
use panic_halt as _;

bind_interrupts!(struct Irqs {
I2C1_EV => ch32_hal::i2c::EventInterruptHandler<ch32_hal::peripherals::I2C1>;
I2C1_ER => ch32_hal::i2c::ErrorInterruptHandler<ch32_hal::peripherals::I2C1>;
});

type I2cBus = Mutex<NoopRawMutex, I2c<'static, ch32_hal::peripherals::I2C1, ch32_hal::mode::Async>>;

#[embassy_executor::main(entry = "qingke_rt::entry")]
async fn main(_spawner: Spawner) -> ! {
ch32_hal::debug::SDIPrint::enable();
let p = ch32_hal::init(ch32_hal::Config::default());
//Check your board for the correct pin. This is for the nanoCH32V003 Development Board
let mut led = Output::new(p.PD6, Level::Low, Default::default());

//Sets up an async i2c
let scl = p.PC2;
let sda = p.PC1;
let i2c = I2c::new(
p.I2C1,
scl,
sda,
Irqs,
p.DMA1_CH6,
p.DMA1_CH7,
Hertz::hz(400_000),
Default::default(),
);

//sets up a i2c bus using the custom type I2cBus at the top
let i2c_bus: I2cBus = Mutex::new(i2c);

//Sets up 2 difference embedded_hal devices from the i2c bus
let i2c_device_1 = I2cDevice::new(&i2c_bus);
let i2c_device_2 = I2cDevice::new(&i2c_bus);

//Sets up 2 dummy i2c drivers one for each device
let mut _dummy_i2c_driver_1 = DummyI2cDeviceDriver::new(i2c_device_1, 0x01);
let mut _dummy_i2c_driver_2 = DummyI2cDeviceDriver::new(i2c_device_2, 0x02);

loop {
led.toggle();
println!("toggle!");
Timer::after_millis(1000).await;
}
}

// Dummy I2C device driver, using `embedded-hal-async`
struct DummyI2cDeviceDriver<I2C: embedded_hal_async::i2c::I2c> {
_i2c: I2C,
}

impl<I2C: embedded_hal_async::i2c::I2c> DummyI2cDeviceDriver<I2C> {
fn new(i2c_dev: I2C, _address: u8) -> Self {
Self { _i2c: i2c_dev }
}
}