Skip to main content
MFabb.1
Associate III
March 1, 2022
Solved

VL53L5CX Linux driver 1.2.0 - Timeout issue in _vl53l5cx_poll_for_answer() function

  • March 1, 2022
  • 3 replies
  • 1342 views
/**
 * @brief Inner function, not available outside this file. This function is used
 * to wait for an answer from VL53L5CX sensor.
 */
 
static uint8_t _vl53l5cx_poll_for_answer(
		VL53L5CX_Configuration	*p_dev,
		uint8_t					size,
		uint8_t					pos,
		uint16_t				address,
		uint8_t					mask,
		uint8_t					expected_value)
{
	uint8_t status = VL53L5CX_STATUS_OK;
	uint8_t timeout = 0;
 
	do {
		status |= RdMulti(&(p_dev->platform), address,
				p_dev->temp_buffer, size);
		status |= WaitMs(&(p_dev->platform), 10);
 
		if(timeout >= (uint8_t)200)	/* 2s timeout */
		{
			status |= p_dev->temp_buffer[2];
		}else if((size >= (uint8_t)4) 
 && (p_dev->temp_buffer[2] >= (uint8_t)0x7f))
		{
			status |= VL53L5CX_MCU_ERROR;
		}
		else
		{
			timeout++;
		}
	}while ((p_dev->temp_buffer[pos] & mask) != expected_value);
 
	return status;
}

The variable timeout is incremented and tested if above 200 (2s timeout) but there is not a condition on timeout value to break the while loop.

This could lead to a infinite loop if the while condition below is never true.

(p_dev->temp_buffer[pos] & mask) != expected_value

Could you double check the code?

Thank you

This topic has been closed for replies.
Best answer by John E KVAM

The suggested fix is:

if(timeout >= (uint8_t)200) /* 2s timeout */
{
 status |= (uint8_t)VL53L5CX_STATUS_TIMEOUT_ERROR;
 break; // To be tested
}else if((size >= (uint8_t)4) 
 && (p_dev->temp_buffer[2] >= (uint8_t)0x7f))
{
 status |= VL53L5CX_MCU_ERROR;
 break; // To be tested
}
else
{
 timeout++;
}

It's up to you if you want to implement this before we finish regression tests.

So try it if you want to, or wait for our update.

And thanks for the note.

  • john

3 replies

John E KVAM
ST Employee
March 3, 2022

I completely agree. Why even bother to increment a timeout, if you are not going to test for it.

I've forwarded your complaint to the software team.

Nice catch.

  • john
John E KVAM
John E KVAMBest answer
ST Employee
March 4, 2022

The suggested fix is:

if(timeout >= (uint8_t)200) /* 2s timeout */
{
 status |= (uint8_t)VL53L5CX_STATUS_TIMEOUT_ERROR;
 break; // To be tested
}else if((size >= (uint8_t)4) 
 && (p_dev->temp_buffer[2] >= (uint8_t)0x7f))
{
 status |= VL53L5CX_MCU_ERROR;
 break; // To be tested
}
else
{
 timeout++;
}

It's up to you if you want to implement this before we finish regression tests.

So try it if you want to, or wait for our update.

And thanks for the note.

  • john
MFabb.1
MFabb.1Author
Associate III
March 8, 2022

Thank you John.

I am looking forward for an API update.