esp-hal/qa-test/src/bin/embassy_i2c.rs
Dániel Buga 7f8af8a651
Remove prelude (#2845)
* Remove prelude

* Changelog
2024-12-20 10:24:57 +00:00

56 lines
1.5 KiB
Rust

//! Embassy I2C
//!
//! Depending on your target and the board you are using you have to change the
//! pins.
//!
//! This is an example of running the embassy executor with IC2. It uses an
//! LIS3DH to get accelerometer data.
//!
//! Following pins are used:
//! - SDA => GPIO4
//! - SCL => GPIO5
//% CHIPS: esp32 esp32c2 esp32c3 esp32c6 esp32h2 esp32s2 esp32s3
//% FEATURES: embassy-generic-timers
#![no_std]
#![no_main]
use embassy_executor::Spawner;
use embassy_time::{Duration, Timer};
use esp_backtrace as _;
use esp_hal::{
i2c::master::{Config, I2c},
time::RateExtU32,
timer::timg::TimerGroup,
};
use lis3dh_async::{Lis3dh, Range, SlaveAddr};
#[esp_hal_embassy::main]
async fn main(_spawner: Spawner) {
let peripherals = esp_hal::init(esp_hal::Config::default());
let timg0 = TimerGroup::new(peripherals.TIMG0);
esp_hal_embassy::init(timg0.timer0);
let i2c0 = I2c::new(peripherals.I2C0, {
let mut config = Config::default();
config.frequency = 400.kHz();
config
})
.unwrap()
.with_sda(peripherals.GPIO4)
.with_scl(peripherals.GPIO5)
.into_async();
let mut lis3dh = Lis3dh::new_i2c(i2c0, SlaveAddr::Alternate).await.unwrap();
lis3dh.set_range(Range::G8).await.unwrap();
loop {
let norm = lis3dh.accel_norm().await.unwrap();
esp_println::println!("X: {:+.5} Y: {:+.5} Z: {:+.5}", norm.x, norm.y, norm.z);
Timer::after(Duration::from_millis(100)).await;
}
}