Zephyr Initialization of I2C and Sensor

I have a custom board with an I2C connected sensor ADXL345. I have to do GPI/O initialization to select the I2C. This works because I can read the ADXL with custom I2C commands. But the sensor driver doesn't work. Here is what I get on the console (very first message before the kernel boots):

[00:00:00.875,915] <err> ADXL345: Read PART ID failed: 0xfffffffb (0)

I know that it is a software error because I get the following messages from a direct I2C read:

[00:00:00.876,708] <inf> adxl344: Call to 'i2c_reg_read_byte_dt': address = 0x00, chip_id = 0xE6

which is the correct I2C.


Here is my board definition file:

&i2c0 {
    compatible = "nordic,nrf-twi";
    status = "okay";
    pinctrl-0 = <&i2c0_default>;
    pinctrl-1 = <&i2c0_sleep>;
    pinctrl-names = "default", "sleep";
    adxl344: adxl344@53 {
        compatible = "adi,adxl345";
        status = "okay";
        reg = < 0x53 >;
    };
};

This is my board pre-initialization:

static int board_nrf52840dongle_nrf52840_init(void)
{

    nrf_gpio_cfg_output(NRF_GPIO_PIN_MAP(0, 5));
    nrf_gpio_cfg_output(NRF_GPIO_PIN_MAP(0, 27));
    nrf_gpio_pin_set(NRF_GPIO_PIN_MAP(0, 5));
    nrf_gpio_pin_clear(NRF_GPIO_PIN_MAP(0, 27));

    return 0;
}

SYS_INIT(board_nrf52840dongle_nrf52840_init, PRE_KERNEL_1, CONFIG_KERNEL_INIT_PRIORITY_DEFAULT);

What am I doing wrong?

  • Hello,

    [00:00:00.875,915] <err> ADXL345: Read PART ID failed: 0xfffffffb (0)

    Looking at the sensor driver implementation, I see that it prints the returned error code as a hexadecimal value rather than a decimal value. I believe the error code should be -5, which corresponds to -EIO. The i2c driver will return this error if the transaction fails due to a data or address NACK. Do you have external pull-ups on the bus lines?

    I know that it is a software error because I get the following messages from a direct I2C read:

    [00:00:00.876,708] <inf> adxl344: Call to 'i2c_reg_read_byte_dt': address = 0x00, chip_id = 0xE6

    It is interesting that you are able to read the ID from the device later in the boot process. Please ensure that the i2c driver is set to have a higher init priority than the adx driver by looking at the generated .config file for your application project.

    This is my board pre-initialization:

    Are these the SDA and SCL lines? In that case, it should be redundant to configure them here as the pin states defined in 'i2c0_default' will be applied during initialization of the driver.

    Best regards,

    Vidar

    EDIT: Could you also try changing the binding to nordic,nrf-twiinstead of nordic,nrf-twi? This will include the driver for the TWIM (easyDMA) peripheral instead of the legacy TWI peripheral. It's not related to the problem, but I think the TWIM driver will give you are more detailed error message in case of an address or data NACK.

Related