Re: [PATCH v5 3/3] samples: rust: add Rust I2C sample driver
From: Danilo Krummrich
Date: Thu Sep 11 2025 - 16:46:33 EST
On Thu Sep 11, 2025 at 5:50 PM CEST, Igor Korotin wrote:
> +// NOTE: The code below is expanded macro module_i2c_driver. It is not used here
> +// because we need to manually create an I2C client in `init()`. The macro
> +// hides `init()`, so to demo client creation on adapter SAMPLE_I2C_ADAPTER_INDEX
> +// we expand it by hand.
> +type Ops<T> = kernel::i2c::Adapter<T>;
Not a huge fan of this type alias, but up to you. :)
> +#[pin_data]
> +struct DriverModule {
> + #[pin]
> + _driver: kernel::driver::Registration<Ops<SampleDriver>>,
> + _reg: i2c::Registration,
> +}
> +
> +impl kernel::InPlaceModule for DriverModule {
> + fn init(
> + module: &'static kernel::ThisModule,
> + ) -> impl ::pin_init::PinInit<Self, kernel::error::Error> {
> + kernel::try_pin_init!(Self {
> + _reg <- {
> + let adapter = i2c::I2cAdapter::<Normal>::get(SAMPLE_I2C_ADAPTER_INDEX)?;
Just i2c::I2cAdapter::get() is fine.
> +
> + i2c::Registration::new(adapter.as_ref(), &BOARD_INFO)
Does i2c_new_client_device() grab a reference count of the adapter? If not, you
have to store an ARef<I2cAdapter> within your i2c::Registration as well.
> + },
> + _driver <- kernel::driver::Registration::new(<Self as kernel::ModuleMetadata>::NAME,module,),
> + })
> + }
> +}