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>
impl<T: I2cTargetCallbacks> I2cTargetData<T>
Sourcepub const fn new(address: u16, data: T) -> Self
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.
Sourcepub fn data(&self) -> &T
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.
Sourcepub fn register(&'static self, i2c: &mut I2c) -> Result<I2cTarget>
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.