I2C read failed reg 0x00 err -5

I am faced "I2C read failed reg 0x00 err -5" issue 

&pinctrl {
	i2c22_default: i2c22_default {
		group1 {
			psels = <NRF_PSEL(TWIM_SCL, 2, 7)>,
					<NRF_PSEL(TWIM_SDA, 2, 6)>;
					bias-pull-up;
		};
	};

	i2c22_sleep: i2c22_sleep {
		group1 {
			psels = <NRF_PSEL(TWIM_SCL, 2, 7)>,
					<NRF_PSEL(TWIM_SDA, 2, 6)>;
					low-power-enable;
		};
	};

	i2s20_default: i2s20_default {
		group1 {
			psels = <NRF_PSEL(I2S_SDOUT, 0, 0)>,
			        <NRF_PSEL(I2S_SDIN, 0, 1)>,
			        <NRF_PSEL(I2S_SCK_S, 0, 2)>,
			        <NRF_PSEL(I2S_MCK, 0, 3)>,
			        <NRF_PSEL(I2S_LRCK_M, 0, 4)>;
		};
	};

	i2s20_sleep: i2s20_sleep {
		group1 {
			psels = <NRF_PSEL(I2S_SDOUT, 0, 0)>,
			        <NRF_PSEL(I2S_SDIN, 0, 1)>,
			        <NRF_PSEL(I2S_SCK_S, 0, 2)>,
			        <NRF_PSEL(I2S_MCK, 0, 3)>,
			        <NRF_PSEL(I2S_LRCK_M, 0, 4)>;
		};
	};
};

&i2c22 {
	status = "okay";
	pinctrl-0 = <&i2c22_default>;
	pinctrl-1 = <&i2c22_sleep>;
	pinctrl-names = "default", "sleep";

	audiocodec: audiocodec@18 {
		compatible = "i2c-device";
		status = "okay";
		reg = < 0x18 >;
	};
};

&i2s20 {
	status = "okay";
	pinctrl-0 = <&i2s20_default>;
	pinctrl-1 = <&i2s20_sleep>;
	pinctrl-names = "default", "sleep";
}; this is my overlay

// read command
int read_eg(void)
{
    int ret;
    uint8_t reg = 0x00;
    uint8_t val = 0;

    ret = i2c_write_read_dt(&dev_i2c,
                            &reg, 1,
                            &val, 1);
    if (ret)
    {
        printk("I2C read failed reg 0x%02x err %d\n", reg, ret);
        return ret;
    }

   printk("Read reg 0x%02x = 0x%02x\n", reg, val);

    return 0;
}

int codec_hw_reset(void)
 {
    int ret;

    ret=gpio_pin_set_dt(&codec_reset_pin, 1);
    k_msleep(20);

    /* HIGH → reset released */
    ret=gpio_pin_set_dt(&codec_reset_pin, 0);
    k_msleep(50);

    // return 0;

    // if(ret<3)
    // {
    //   printf("fail hw reset");
    // }

    return 0;
}

static int codec_i2c_config(void)
{
    if (!device_is_ready(dev_i2c.bus))
    {
        printk("Error: I2C bus %s is not ready!\n", dev_i2c.bus->name);
        return -1;
    }

    printk("I2C bus ready, addr=0x%02x\n", dev_i2c.addr); //dev_i2c.addr
 

    return 0;
}

static int codec_gpios_config(void)
{
    int ret;

    if (!gpio_is_ready_dt(&codec_reset_pin))
    {
        printk("Error: Codec reset pin not ready\n");
        return -1;
    }
    ret=gpio_pin_configure_dt(&codec_reset_pin,
                                GPIO_OUTPUT_ACTIVE |
                                GPIO_ACTIVE_LOW);

   
    ret=gpio_pin_configure_dt(&codec_reset_pin,
                                GPIO_OUTPUT_ACTIVE |
                                GPIO_ACTIVE_LOW);
   
    if (ret < 0)
    {
        printk("Error %d: Failed to configure codec reset pin\n", ret);
        return -1;
    }

    return 0;
}

int codec_init(void)
{
   
    //
    int ret;

    ret = codec_gpios_config();
    if (ret != 0)
    {
        return ret;
    }

    ret = codec_i2c_config();
    if (ret != 0)
    {
        return ret;
    }

    ret = codec_hw_reset();
    if (ret != 0)
    {
        return ret;
    }
    k_msleep(50);
    read_eg();

    return 0;
} this is my code i get -5 error 

  

Parents Reply Children
  • hi ,

    when i am connect i2s ans i2c together is gettimg -5 i2c error . without i2s , i2c register read is proper ??


  • &pinctrl {
        i2c22_default: i2c22_default {
            group1 {
                psels =
                        <NRF_PSEL(TWIM_SCL, 1, 11)>,
                        <NRF_PSEL(TWIM_SDA, 1, 12)>;
                       
            };
        };

        i2c22_sleep: i2c22_sleep {
            group1 {
                psels = <NRF_PSEL(TWIM_SCL, 1,11)>,
                        <NRF_PSEL(TWIM_SDA, 1, 12)>;
                        low-power-enable;
            };
        };

        i2s20_default: i2s20_default {
            group1 {
                psels = <NRF_PSEL(I2S_SDIN, 1, 4)>,
                        <NRF_PSEL(I2S_SDOUT, 1, 5)>,
                        <NRF_PSEL(I2S_SCK_S, 1, 6)>,
                        <NRF_PSEL(I2S_MCK, 1, 13)>,
                        <NRF_PSEL(I2S_LRCK_M, 1, 14)>;
            };
        };

        i2s20_sleep: i2s20_sleep {
            group1 {
                psels = <NRF_PSEL(I2S_SDIN, 1, 4)>,
                        <NRF_PSEL(I2S_SDOUT, 1, 5)>,
                        <NRF_PSEL(I2S_SCK_S, 1, 6)>,
                        <NRF_PSEL(I2S_MCK, 1, 13)>,
                        <NRF_PSEL(I2S_LRCK_M, 1, 14)>;
            };
        };
    };

    &i2c22 {
        status = "okay";
        pinctrl-0 = <&i2c22_default>;
        pinctrl-1 = <&i2c22_sleep>;
        pinctrl-names = "default", "sleep";

        audiocodec: audiocodec@18 {
            compatible = "i2c-device";
            status = "okay";
            reg = < 0x18 >;
        };
    };

    &i2s20 {
        status = "okay";
        pinctrl-0 = <&i2s20_default>;
        pinctrl-1 = <&i2s20_sleep>;
        pinctrl-names = "default", "sleep";
    };

    &led0 {status = "disabled";};

    / {
         custom_pins{
            compatible = "gpio-keys";

            codec_reset: codec-reset-node {
                gpios = <&gpio1 9 GPIO_ACTIVE_HIGH>;
                label = "Codec Reset Pin";
            };
           
        };
    };






    &i2c30 {
        status = "disabled";
    };
    /delete-node/ &{/pin-controller/i2c22_default/group2/};
    /delete-node/ &{/pin-controller/i2s20_default/group5/};
    /delete-node/ &{/pin-controller/i2s20_default/group2/};
    /delete-node/ &{/pin-controller/i2s20_default/group3/};
    /delete-node/ &{/pin-controller/i2s20_default/group4/};

    // &button0 {
    //  /delete-property/ gpios;
    // };
  • Hi,

    Since the issue only appears when I2S is active, it may be related to marginal I2C signal integrity. Could you try enabling internal pull-ups on the I2C pins by adding bias-pull-up to the i2c22_default pinctrl node, and check if the behavior changes. You could trying adding this to the overlay:

    i2c22_default: i2c22_default {
        group1 {
            psels =
                <NRF_PSEL(TWIM_SCL, 1, 11)>,
                <NRF_PSEL(TWIM_SDA, 1, 12)>;
            bias-pull-up;
        };
    };

    Best Regards,
    Syed Maysum

Related