-
Notifications
You must be signed in to change notification settings - Fork 23
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
126 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |