Skip to content

Commit

Permalink
enhance: add async i2c demo
Browse files Browse the repository at this point in the history
  • Loading branch information
andelf committed Oct 10, 2024
1 parent 9d008da commit 80c59c1
Show file tree
Hide file tree
Showing 2 changed files with 126 additions and 0 deletions.
71 changes: 71 additions & 0 deletions examples/ch32v307/src/bin/i2c-bmp180-async.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
//! I2C test with SSD1306
//!
//! Folowing pins are used:
//! SDA PB11
//! SCL PB10
//!
//! Depending on your target and the board you are using you have to change the pins.
//!
//! For this example you need to hook up an SSD1306 I2C display.
//! The display will flash black and white.
#![no_std]
#![no_main]
#![feature(type_alias_impl_trait)]
#![feature(impl_trait_in_assoc_type)]
#![feature(naked_functions)]

use ch32_hal as hal;
use embassy_executor::Spawner;
use embassy_time::{Delay, Timer};
use hal::i2c::I2c;
use hal::time::Hertz;
use hal::{bind_interrupts, println};
use hal::peripherals;

bind_interrupts!(struct Irqs {
I2C2_EV => hal::i2c::EventInterruptHandler<peripherals::I2C2>;
I2C2_ER => hal::i2c::ErrorInterruptHandler<peripherals::I2C2>;
});

#[panic_handler]
fn panic(info: &core::panic::PanicInfo) -> ! {
let _ = println!("\n\n\n{}", info);
loop {}
}

#[embassy_executor::main(entry = "ch32_hal::entry")]
async fn main(_spawner: Spawner) -> ! {
hal::debug::SDIPrint::enable();
let mut config = hal::Config::default();
config.rcc = hal::rcc::Config::SYSCLK_FREQ_144MHZ_HSE;
let p = hal::init(Default::default());
hal::embassy::init();

let i2c_sda = p.PB11;
let i2c_scl = p.PB10;

println!("init ok");
let i2c = I2c::new(
p.I2C2,
i2c_scl,
i2c_sda,
Irqs,
p.DMA1_CH4,
p.DMA1_CH5,
Hertz::khz(400),
Default::default(),
);

let mut sensor = edrv_bmp180::BMP180::new_primary(i2c);

sensor.init(Default::default()).await.unwrap();

loop {
let m = sensor.read_measurement(&mut Delay).await.unwrap();

println!("Measurement: {:?}", m);

Timer::after_secs(1).await;
}
}
55 changes: 55 additions & 0 deletions examples/ch32v307/src/bin/i2c-bmp180.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
//! I2C test with SSD1306
//!
//! Folowing pins are used:
//! SDA PB11
//! SCL PB10
//!
//! Depending on your target and the board you are using you have to change the pins.
//!
//! For this example you need to hook up an SSD1306 I2C display.
//! The display will flash black and white.
#![no_std]
#![no_main]
#![feature(type_alias_impl_trait)]
#![feature(impl_trait_in_assoc_type)]

use ch32_hal as hal;
use embassy_executor::Spawner;
use embassy_time::{Delay, Timer};
use hal::i2c::I2c;
use hal::println;
use hal::time::Hertz;

#[panic_handler]
fn panic(info: &core::panic::PanicInfo) -> ! {
let _ = println!("\n\n\n{}", info);
loop {}
}

#[embassy_executor::main(entry = "ch32_hal::entry")]
async fn main(_spawner: Spawner) -> ! {
hal::debug::SDIPrint::enable();
let mut config = hal::Config::default();
config.rcc = hal::rcc::Config::SYSCLK_FREQ_144MHZ_HSE;
let p = hal::init(Default::default());
hal::embassy::init();

let i2c_sda = p.PB11;
let i2c_scl = p.PB10;

println!("init ok");
let i2c = I2c::new_blocking(p.I2C2, i2c_scl, i2c_sda, Hertz::khz(400), Default::default());

let mut sensor = edrv_bmp180::blocking::BMP180::new_primary(i2c);

sensor.init(Default::default()).unwrap();

loop {
let m = sensor.read_measurement(&mut Delay).unwrap();

println!("Measurement: {:?}", m);

Timer::after_secs(1).await;
}
}

0 comments on commit 80c59c1

Please sign in to comment.