Binding device driver (i2c device with interrupts)

Hello, I hope you are well.

I created a new driver for an i2c accelerometer with 2 gpio interrupts, but it does not build properly. When calling `static const struct device * accel = DEVICE_DT_GET(DT_NODELABEL(accel_0));` I have the following error:

/opt/nordic/ncs/toolchains/b8efef2ad5/opt/zephyr-sdk/arm-zephyr-eabi/bin/../lib/gcc/arm-zephyr-eabi/12.2.0/../../../../arm-zephyr-eabi/bin/ld.bfd: app/libapp.a(main.c.obj): in function `z_impl_gpio_pin_configure':
/opt/nordic/ncs/sdk/zephyr/include/zephyr/drivers/gpio.h:969: undefined reference to `__device_dts_ord_110'
collect2: error: ld returned 1 exit status

Here is my `.overlay` file:

&gpio1 {
    status = "okay";
};

&gpio0 {
    status = "okay";
};

&i2c22 {
    compatible = "nordic,nrf-twim";
    status = "okay";
    pinctrl-0 = <&i2c22_default>;
    pinctrl-1 = <&i2c22_sleep>;
    pinctrl-names = "default", "sleep";
    clock-frequency = <100000>;

    accel_0: accel@6a {
        status = "okay";
        compatible = "device-custom";
        reg = <0x6a>;
        int1-gpios = <&gpio0 1 GPIO_ACTIVE_HIGH>;
        int2-gpios = <&gpio1 8 GPIO_ACTIVE_HIGH>;
    };
};

Here is the dts binding `.yaml` file:

description: |
    3-axis IMU (Inertial Measurement Unit) sensor
    accessed through I2C bus

compatible: "device-custom"

include: ["i2c-device.yaml"]

properties:
  reg:
    required: true

  int1-gpios:
    type: phandle-array
    required: true
    description: |
      INT1 pin

  int2-gpios:
    type: phandle-array
    required: true
    description: |
      INT2 pin

And here is the driver definition in C:

struct drv_imu_config_t {
    const struct i2c_dt_spec  i2c;
    const struct gpio_dt_spec int1_gpio;
    const struct gpio_dt_spec int2_gpio;
};

static int driver_init(const struct device *p_dev) {
    // ...
    // gpio_pin_configure_dt(), gpio_pin_interrupt_configure_dt(), gpio_init_callback(), and gpio_add_callback().
}

#define DRV_IMU_DEFINE(inst)                                                                                           \
    static const struct drv_imu_config_t imu_config_##inst = {                                                         \
        .i2c       = I2C_DT_SPEC_INST_GET(inst),                                                                       \
        .int1_gpio = GPIO_DT_SPEC_INST_GET_OR(inst, int1_gpios, {0}),                                                        \
        .int2_gpio = GPIO_DT_SPEC_INST_GET_OR(inst, int2_gpios, {0}),                                                        \
    };                                                                                                                 \
    DEVICE_DT_INST_DEFINE(inst, driver_init, NULL, NULL, &imu_config_##inst, POST_KERNEL, DRV_IMU_INIT_PRIORITY, NULL);

DT_INST_FOREACH_STATUS_OKAY(DRV_IMU_DEFINE)

I define `DRV_IMU_DEFINE=y` in the `Kconfig` file, and `CONFIG_DRV_IMU_DEFINE`, `CONFIG_GPIO` and `CONFIG_I2C` are enabled. But it does not build, with the warning `

warning: 'driver_init' defined but not used [-Wunused-function]` and the error `undefined reference to `__device_dts_ord_110'`.

Also, when I grep 110 in the `build/zephyr/devicetree_generated.h`, it does not refer to a gpio device but i2c:

 *   110 /soc/peripheral@50000000/i2c@c8000/accel@6a
	110, /* /soc/peripheral@50000000/i2c@c8000/accel@6a */
#define DT_N_S_soc_S_peripheral_50000000_S_clock_10e000_P_reg {1105920 /* 0x10e000 */, 4096 /* 0x1000 */}
#define DT_N_S_soc_S_peripheral_50000000_S_clock_10e000_P_reg_IDX_0 1105920
#define DT_N_S_soc_S_peripheral_50000000_S_i2c_c6000_P_reg {811008 /* 0xc6000 */, 4096 /* 0x1000 */}
#define DT_N_S_soc_S_peripheral_50000000_S_i2c_c6000_P_reg_IDX_0 811008
#define DT_N_S_soc_S_peripheral_50000000_S_spi_c6000_P_reg {811008 /* 0xc6000 */, 4096 /* 0x1000 */}
#define DT_N_S_soc_S_peripheral_50000000_S_spi_c6000_P_reg_IDX_0 811008
#define DT_N_S_soc_S_peripheral_50000000_S_uart_c6000_P_reg {811008 /* 0xc6000 */, 4096 /* 0x1000 */}
#define DT_N_S_soc_S_peripheral_50000000_S_uart_c6000_P_reg_IDX_0 811008
	110, /* /soc/peripheral@50000000/i2c@c8000/accel@6a */ \
#define DT_N_S_soc_S_peripheral_50000000_S_i2c_c8000_S_accel_6a_ORD 110
#define DT_N_S_soc_S_peripheral_50000000_S_i2c_c8000_S_accel_6a_ORD_STR_SORTABLE 00110
#define DT_N_S_soc_S_peripheral_50000000_S_power_10e000_RANGES_IDX_0_VAL_PARENT_BUS_ADDRESS 1105920 /* 0x10e000 */
#define DT_N_S_soc_S_peripheral_50000000_S_power_10e000_P_reg {1105920 /* 0x10e000 */, 4096 /* 0x1000 */}
#define DT_N_S_soc_S_peripheral_50000000_S_power_10e000_P_reg_IDX_0 1105920

Best regards,

Related