2025-07-10 2:54 AM
Hi everyone,
I have been struggling to work with VL53L5CX on my NRF board using Zephyr. I used the VL53L5CX_ULD_driver_2.0.1 API and changed the platform.cpp. The WrByte. RdByte, and MultiWrite are working, but the ReadMulti always has the wrong values. So far, what I got is as follows:
status |= _vl53l5cx_poll_for_answer(p_dev, 4, 0,VL53L5CX_UI_CMD_STATUS, 0xff, 2);
My code is as follows:
uint8_t VL53L5CX_WrMulti(VL53L5CX_Platform *sensor, uint16_t reg, uint8_t *p_values, uint32_t size)
{
uint8_t status = VL53L5CX_STATUS_OK;
uint16_t current_address = reg; // Initialize current address
uint32_t remaining_size = size; // Calculate remaining size to write
// Loop until all data is written
while (remaining_size > 0)
{
// Calculate the current chunk size to write
uint32_t current_chunk_size = (remaining_size > chunk_size) ? chunk_size : remaining_size;
struct i2c_msg msg[2];
uint8_t reg_buff[2] = {current_address >> 8, current_address & 0xFF};
msg[0].buf = ®_buff;
msg[0].len = 2;
msg[0].flags = I2C_MSG_WRITE | I2C_MSG_STOP;
msg[1].buf = &p_values;
msg[1].len = current_chunk_size;
msg[1].flags = I2C_MSG_WRITE | I2C_MSG_STOP;
status = i2c_transfer(sensor->i2c_dev, msg, 2, sensor->address);
if (status != VL53L5CX_STATUS_OK)
{
status = VL53L5CX_STATUS_ERROR;
LOG_ERR("Failed to write status %d", status);
}
// Update remaining size and pointer to move to the next chunk
remaining_size -= current_chunk_size;
current_address += current_chunk_size; // Increment register address for next chunk
p_values += current_chunk_size;
VL53L5CX_WaitMs(&sensor, 10);
}
return status;
}
uint8_t VL53L5CX_RdMulti(VL53L5CX_Platform *sensor, uint16_t reg, uint8_t *p_values, uint32_t size)
{
uint8_t status = VL53L5CX_STATUS_OK;
uint8_t buf[2] = {reg >> 8, reg & 0xFF};
status = i2c_write(sensor->i2c_dev, buf, 2, sensor->address);
if (status != VL53L5CX_STATUS_OK)
{
status = VL53L5CX_STATUS_ERROR;
LOG_ERR("Failed to write, status %d", status);
}
uint16_t bytesToReadRemaining = size;
uint16_t offset = 0;
while (bytesToReadRemaining > 0)
{
uint16_t bytesToRead = (bytesToReadRemaining > chunk_size) ? chunk_size : bytesToReadRemaining;
// for (uint16_t x = 0; x < bytesToRead; x++)
// {
// status = i2c_read(sensor->i2c_dev, &p_values[offset + x], (uint8_t)bytesToRead, sensor->address);
// if (status != VL53L5CX_STATUS_OK)
// {
// status = VL53L5CX_STATUS_ERROR;
// LOG_ERR("Failed to read status %d", status);
// }
// }
status = i2c_read(sensor->i2c_dev, &p_values[offset], bytesToRead, sensor->address);
if (status != VL53L5CX_STATUS_OK)
{
status = VL53L5CX_STATUS_ERROR;
LOG_ERR("Failed to read status %d", status);
}
offset += bytesToRead;
bytesToReadRemaining -= bytesToRead;
}
return status;
}
Kindly help me if anyone has successfully used VL53L5CX on the Zephyr platform.
Thank you!
Best Regards,