aboutsummaryrefslogtreecommitdiff
path: root/examples/mcxa/src/bin/i2c-async.rs
blob: edcfd5f2292fe6f33d87c4361c2d04533ac0dafc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
#![no_std]
#![no_main]

use embassy_executor::Spawner;
use embassy_time::Timer;
use hal::bind_interrupts;
use hal::clocks::config::Div8;
use hal::config::Config;
use hal::i2c::InterruptHandler;
use hal::i2c::controller::{self, I2c, Speed};
use hal::peripherals::LPI2C3;
use {defmt_rtt as _, embassy_mcxa as hal, panic_probe as _};

bind_interrupts!(
    struct Irqs {
        LPI2C3 => InterruptHandler<LPI2C3>;
    }
);

#[embassy_executor::main]
async fn main(_spawner: Spawner) {
    let mut config = Config::default();
    config.clock_cfg.sirc.fro_lf_div = Div8::from_divisor(1);

    let p = hal::init(config);

    defmt::info!("I2C example");

    let mut config = controller::Config::default();
    config.speed = Speed::Standard;
    let mut i2c = I2c::new_async(p.LPI2C3, p.P3_27, p.P3_28, Irqs, config).unwrap();
    let mut buf = [0u8; 2];

    loop {
        i2c.async_write_read(0x48, &[0x00], &mut buf).await.unwrap();
        defmt::info!("Buffer: {:02x}", buf);
        Timer::after_secs(1).await;
    }
}