This post is older than 2 years and might not be relevant anymore
More Info: Consider searching for newer posts

-20 Error code when initializing VL53L0X ToF Sensor. Trouble implementing TWI driver.

Hello,

I am using the nRF52832 with the s132 SoftDevice. I've been trying to interface the VL53L0X sensor to get ToF readings. I'm using the code of this driver. When calling the DataInit() and StaticInit(), they are returning a -20 (control interface error). I have checked the implementations of the functions in this driver in the i2c_platform.c (read_byte, write_multi, etc.), and they all seem to make sense, and I am not seeing where they could be wrong.

I have double checked the wiring and pin assignments between the devices, and tried it on two different devices sensors and dev kits, both of which are acting the same way.

I am using this test code to see what these control interface functions are doing:

void i2c_test(void)
{
	int err_count = 0;
	int expected_value = 0;

	uint8_t buff[4] = {0x11,0x22,0x33,0x44};
	uint8_t ChipID[4];
	int i=0;

	for (i=0; i<4; i++){ VL53L0X_read_byte(ToFDevAddr, 0xC0+i, &ChipID[i]); }
	expected_value = ChipID[0]<<24 | ChipID[1]<<16 | ChipID[2]<<8 | ChipID[3];
	if(rd_write_verification(0xc0, expected_value) <0) err_count++;	// check the chip ID

	VL53L0X_write_multi(ToFDevAddr, 0x4,  buff, 4);				// check WriteMulti
	if(rd_write_verification(0x4, 0x11223344) <0) err_count++;

	VL53L0X_write_dword(ToFDevAddr, 0x4, 0xffeeddcc);				// check WrDWord
	if(rd_write_verification(0x4, 0xffeeddcc) <0) err_count++;

	VL53L0X_write_word(ToFDevAddr, 0x4, 0x5566);					// check WrWord
	VL53L0X_write_word(ToFDevAddr, 0x6, 0x7788);
	if(rd_write_verification(0x4, 0x55667788) <0) err_count++;

        for (i=0; i<4; i++){ VL53L0X_write_byte(ToFDevAddr, 0x04+i, &buff[i]); }
	if(rd_write_verification(0x4,0x11223344) <0) err_count++;
	if(err_count>0)
	{
		printf("i2c test failed - please check it\n");
	}
}
int rd_write_verification( uint8_t idx, uint32_t expected_value)
{
	uint8_t bytes[4],mbytes[4];
	uint16_t words[2];
	uint32_t dword;
	VL53L0X_read_multi(ToFDevAddr, idx, mbytes, 4);
	for (int i=0; i<4; i++){ VL53L0X_read_byte(ToFDevAddr, idx+i, &bytes[i]); }
	for (int i=0; i<2; i++){ VL53L0X_read_word(ToFDevAddr, idx+i*2, &words[i]); }
	VL53L0X_read_dword(ToFDevAddr, idx, &dword);
	
	printf("expected   = %08x,\n",expected_value);
	printf("read_multi = %02x, %02x, %02x, %02x\n", mbytes[0],mbytes[1],mbytes[2],mbytes[3]);
	printf("read_bytes = %02x, %02x, %02x, %02x\n", bytes[0],bytes[1],bytes[2],bytes[3]);
	printf("read words = %04x, %04x\n",words[0],words[1]);
	printf("read dword = %08x\n",dword);
	
	if((mbytes[0]<<24 | mbytes[1]<<16 | mbytes[2]<<8 | mbytes[3]) != expected_value) return (-1);
	if((bytes[0]<<24 | bytes[1]<<16 | bytes[2]<<8 | bytes[3]) != expected_value) return (-1);
	if((words[0]<<16 | words[1]) != expected_value) return (-1);
	if(dword != expected_value) return(-1);
	return(0);
	
}

Here is what is printed out when calling this test, before the init functions mentioned above:

expected   = 00000000,
read_multi = 03, 00, 06, 00
read_bytes = 01, 00, 00, 00
read words = 0029, 0000
read dword = c3000000
expected   = 11223344,
read_multi = 03, 00, 06, 00
read_bytes = 01, 00, 00, 00
read words = 0029, 0000
read dword = c3000000
expected   = ffeeddcc,
read_multi = a5, da, 02, 00
read_bytes = 02, 00, 00, 00
read words = 0000, 0000
read dword = 00000004
expected   = 55667788,
read_multi = 6d, da, 02, 00
read_bytes = 03, 00, 00, 00
read words = 0000, 0000
read dword = 00000006
<info> app: TWI init successful

I can't figure out what could be causing these driver functions to be reading the wrong values. Any help would be appreciated, this has been a tricky problem.

Thank you!

  • Hi,

    I would recommend you to debug the driver code to see where the error is returned inside the DataInit() and StaticInit() functions, that may provide more details of why this happens. If you could also post a logic trace of the TWI bus when you run these functions, it may give some indication of what goes wrong.

    Are you first calling VL53L0X_comms_initialise(), and is this successful?

    It does not look like the log is giving any meaningful/static data, so most likely the values you are seeing is just garbage from RAM.

    Best regards,
    Jørgen

Related