This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

I2C reading from a register not working

Hello,

I am using zephyr on a nRF52840 to communicate with a vl53l5cx TOF sensor using I2C. When I use i2c_reg_read_byte() or i2c_burst_read(), the device address is sent, the internal register is sent, and then when read command is sent I receive no ACK and no error is returned from the function. Writing using the same functions work as expected. I am using sdk 1.9.1. Any help would be appreciated.

#include <zephyr.h>
#include <device.h>
#include <drivers/sensor.h>
#include <stdio.h>
#include <sys/printk.h>
#include <drivers/i2c.h>

#define TOF_I2C_ADDR (0x52 >> 1)

void main(void)
{
	const struct device *i2c_dev = device_get_binding("I2C_0");

	int ret;
	uint8_t buf[1];
	buf[0] = 0x1;
	//buf[1] = 0x2;
	//buf[2] = 0x3;
	//buf[3] = 0x4;

	if (i2c_dev == NULL)
	{
		printk("Could not get device\n");
		return;
	}

	i2c_configure(i2c_dev, I2C_SPEED_SET(I2C_SPEED_STANDARD));
	k_msleep(10);

	while (1)
	{
		ret = i2c_reg_read_byte(i2c_dev, TOF_I2C_ADDR, 0x00, buf);
		if (ret)
		{
			printk("Failed to read/write byte: %d\n", ret);
		}

		//printk("Buf: [%d, %d, %d, %d]\n\n", buf[0], buf[1], buf[2], buf[3]);
		printk("Buf: [%d]\n\n", buf[0]);
		k_msleep(1000);
	}
}

Thanks,

Jacob

Parents
  • Hello

    What sort of ACK are you expecting to see other than the functions returning 0?

    What sort  of register are you trying to read from the I2C device, and are you sure it's not read correctly?

    Can you explain what you have done to test writing, since you are unable to read?

    Best regards,

    Einar

Reply
  • Hello

    What sort of ACK are you expecting to see other than the functions returning 0?

    What sort  of register are you trying to read from the I2C device, and are you sure it's not read correctly?

    Can you explain what you have done to test writing, since you are unable to read?

    Best regards,

    Einar

Children
No Data
Related