mirror of
https://github.com/esp-rs/esp-hal.git
synced 2025-10-02 14:44:42 +00:00

* Unify the system peripheral Whilst the PCR, SYSTEM and DPORT peripherals are different, we currently use them all in the same way. This PR unifies the peripheral name in the hal to `SYSTEM`. The idea is that they all do the same sort of thing, so we can collect them under the same name, and later down the line we can being to expose differences under an extended API. The benifits to this are imo quite big, the examples now are all identical, which makes things easier for esp-wifi, and paves a path towards the multichip hal. Why not do this in the PAC? Imo the pac should be as close to the hardware as possible, and the HAL is where we should abstractions such as this. * changelog
80 lines
2.0 KiB
Rust
80 lines
2.0 KiB
Rust
//! Embassy I2C
|
|
//!
|
|
//! Folowing pins are used:
|
|
//! SDA GPIO1
|
|
//! SCL GPIO2
|
|
//!
|
|
//! 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.
|
|
|
|
#![no_std]
|
|
#![no_main]
|
|
#![feature(type_alias_impl_trait)]
|
|
|
|
use embassy_executor::Executor;
|
|
use embassy_time::{Duration, Timer};
|
|
use esp32h2_hal::{
|
|
clock::ClockControl,
|
|
embassy,
|
|
i2c::I2C,
|
|
interrupt,
|
|
peripherals::{Interrupt, Peripherals, I2C0},
|
|
prelude::*,
|
|
IO,
|
|
};
|
|
use esp_backtrace as _;
|
|
use lis3dh_async::{Lis3dh, Range, SlaveAddr};
|
|
use static_cell::make_static;
|
|
|
|
#[embassy_executor::task]
|
|
async fn run(i2c: I2C<'static, I2C0>) {
|
|
let mut lis3dh = Lis3dh::new_i2c(i2c, 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;
|
|
}
|
|
}
|
|
|
|
#[entry]
|
|
fn main() -> ! {
|
|
let peripherals = Peripherals::take();
|
|
let system = peripherals.SYSTEM.split();
|
|
let clocks = ClockControl::boot_defaults(system.clock_control).freeze();
|
|
|
|
#[cfg(feature = "embassy-time-systick")]
|
|
embassy::init(
|
|
&clocks,
|
|
esp32h2_hal::systimer::SystemTimer::new(peripherals.SYSTIMER),
|
|
);
|
|
|
|
#[cfg(feature = "embassy-time-timg0")]
|
|
{
|
|
let timer_group0 = esp32h2_hal::timer::TimerGroup::new(peripherals.TIMG0, &clocks);
|
|
embassy::init(&clocks, timer_group0.timer0);
|
|
}
|
|
|
|
let io = IO::new(peripherals.GPIO, peripherals.IO_MUX);
|
|
|
|
let i2c0 = I2C::new(
|
|
peripherals.I2C0,
|
|
io.pins.gpio1,
|
|
io.pins.gpio2,
|
|
400u32.kHz(),
|
|
&clocks,
|
|
);
|
|
|
|
interrupt::enable(Interrupt::I2C_EXT0, interrupt::Priority::Priority1).unwrap();
|
|
|
|
let executor = make_static!(Executor::new());
|
|
executor.run(|spawner| {
|
|
spawner.spawn(run(i2c0)).ok();
|
|
});
|
|
}
|