esp-hal/examples/src/bin/embassy_i2c.rs
Scott Mabin f32565b4af
Embassy enable thread and interrupt by default, enable embassy when building docs (#1485)
* Remove interrupt and thread executor embassy features

* Reserve sw interrupt 3 (4) instead of 0 for multicore systems with the embassy feature enabled

* Remove uneeded #[feature()] from examples

* Fix HIL tests

* Add thread mode context id and fix up examples

* improve embassy module docs

* changelog

* fixup hil tests

* Fixup usb examples
2024-05-02 15:58:04 +00:00

63 lines
1.6 KiB
Rust

//! Embassy I2C
//!
//! Folowing pins are used:
//! SDA GPIO4
//! SCL GPIO5
//!
//! 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.
//% CHIPS: esp32 esp32c2 esp32c3 esp32c6 esp32h2 esp32s2 esp32s3
//% FEATURES: async embassy embassy-time-timg0 embassy-generic-timers
#![no_std]
#![no_main]
use embassy_executor::Spawner;
use embassy_time::{Duration, Timer};
use esp_backtrace as _;
use esp_hal::{
clock::ClockControl,
embassy::{self},
gpio::Io,
i2c::I2C,
peripherals::Peripherals,
prelude::*,
system::SystemControl,
timer::timg::TimerGroup,
};
use lis3dh_async::{Lis3dh, Range, SlaveAddr};
#[main]
async fn main(_spawner: Spawner) {
let peripherals = Peripherals::take();
let system = SystemControl::new(peripherals.SYSTEM);
let clocks = ClockControl::boot_defaults(system.clock_control).freeze();
let timg0 = TimerGroup::new_async(peripherals.TIMG0, &clocks);
embassy::init(&clocks, timg0);
let io = Io::new(peripherals.GPIO, peripherals.IO_MUX);
let i2c0 = I2C::new_async(
peripherals.I2C0,
io.pins.gpio4,
io.pins.gpio5,
400.kHz(),
&clocks,
);
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;
}
}