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,
®, 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