Module rtic_sync::arbiter::i2c

source ·
Expand description

I2C bus sharing using Arbiter

An Example how to use it in RTIC application:

#[app(device = some_hal, peripherals = true, dispatchers = [TIM16])]
mod app {
    use core::mem::MaybeUninit;
    use rtic_sync::{arbiter::{i2c::ArbiterDevice, Arbiter},

    #[shared]
    struct Shared {}

    #[local]
    struct Local {
        ens160: Ens160<ArbiterDevice<'static, I2c<'static, I2C1>>>,
    }

    #[init(local = [
        i2c_arbiter: MaybeUninit<Arbiter<I2c<'static, I2C1>>> = MaybeUninit::uninit(),
    ])]
    fn init(cx: init::Context) -> (Shared, Local) {
        let i2c = I2c::new(cx.device.I2C1);
        let i2c_arbiter = cx.local.i2c_arbiter.write(Arbiter::new(i2c));
        let ens160 = Ens160::new(ArbiterDevice::new(i2c_arbiter), 0x52);

        i2c_sensors::spawn(i2c_arbiter).ok();

        (Shared {}, Local { ens160 })
    }

    #[task(local = [ens160])]
    async fn i2c_sensors(cx: i2c_sensors::Context, i2c: &'static Arbiter<I2c<'static, I2C1>>) {
        use sensor::Asensor;

        loop {
            // Use scope to make sure I2C access is dropped.
            {
                // Read from sensor driver that wants to use I2C directly.
                let mut i2c = i2c.access().await;
                let status = Asensor::status(&mut i2c).await;
            }

            // Read ENS160 sensor.
            let eco2 = cx.local.ens160.eco2().await;
        }
    }
}

Structs§