I2cTargetData

Struct I2cTargetData 

Source
pub struct I2cTargetData<T: I2cTargetCallbacks> { /* private fields */ }
Expand description

Static storage for an I2C target device.

Wraps a user callback implementation T together with the Zephyr i2c_target_config and i2c_target_callbacks needed for registration. Place this in a static and call register to activate the target on an I2C bus.

The data method provides a shared reference to the user state, usable from any thread.

§Example

use zephyr::device::i2c::{I2cTargetData, I2cTargetCallbacks};
use zephyr::sync::SpinMutex;

struct MyTarget {
    inner: SpinMutex<MyState>,
}

impl I2cTargetCallbacks for MyTarget {
    fn write_received(&self, val: u8) -> zephyr::Result<()> {
        let mut s = self.inner.lock().unwrap();
        // handle byte ...
        Ok(())
    }
    // ... other callbacks
}

static TARGET: I2cTargetData<MyTarget> = I2cTargetData::new(0x42, MyTarget {
    inner: SpinMutex::new(MyState::new()),
});

fn main() {
    let mut i2c = /* get I2C device */;
    let _handle = TARGET.register(&mut i2c).unwrap();
    // Access shared state from any thread:
    let state = TARGET.data();
}

Implementations§

Source§

impl<T: I2cTargetCallbacks> I2cTargetData<T>

Source

pub const fn new(address: u16, data: T) -> Self

Create a new I2C target data instance.

address is the 7-bit I2C target address. data is the user state that implements I2cTargetCallbacks.

Internal pointers (callback table, config→callbacks link) are set up lazily by register.

Source

pub fn data(&self) -> &T

Shared reference to the user data.

This is the same T that the ISR callbacks see via &self, so both sides share the same synchronisation primitives inside T.

Source

pub fn register(&'static self, i2c: &mut I2c) -> Result<I2cTarget>

Register this target on the given I2C bus.

Populates the C callback trampolines, links the internal config to the callback table, and calls i2c_target_register.

Returns an I2cTarget handle whose unregister method reverses the registration.

The &'static self requirement ensures the backing storage outlives the registration.

Trait Implementations§

Auto Trait Implementations§

§

impl<T> !Freeze for I2cTargetData<T>

§

impl<T> !RefUnwindSafe for I2cTargetData<T>

§

impl<T> Unpin for I2cTargetData<T>
where T: Unpin,

§

impl<T> UnwindSafe for I2cTargetData<T>
where T: UnwindSafe,

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.