2025-01-23 06:00 AM - edited 2025-01-23 06:50 AM
Hi there,
I'm currently trying to write the platform code for the VL53L5CX using a Raspberry Pi Pico.
I'm trying to publish the sensor data via micro-ROS in a ROS2 topic. When I use my current code I only get values close to zero and they are the same every time so I think the problem is in the I2C connection. I used an I2C-Scan so I know that the address is 0x29.
The only thing I could find online that was close to a platform code using a Pico was the platform code for a VL53L1X. Could anybody tell what I did wrong or how I have to configure the platform code for the VL53L1X so I can use it with the VL53L5CX.
Thanks a lot :)
Current platform.c:
/**
*
* Copyright (c) 2021 STMicroelectronics.
* All rights reserved.
*
* This software is licensed under terms that can be found in the LICENSE file
* in the root directory of this software component.
* If no LICENSE file comes with this software, it is provided AS-IS.
*
******************************************************************************
*/
#include "hardware/i2c.h"
#include <stdbool.h>
#include "platform.h"
uint8_t VL53L5CX_WrByte(
VL53L5CX_Platform *p_platform,
uint16_t RegisterAdress,
uint8_t value)
{
uint8_t buffer[3];
buffer[0] = (RegisterAdress >> 8) & 0xFF;
buffer[1] = RegisterAdress & 0xFF;
buffer[2] = value;
p_platform->address = 0x29;
int result = i2c_write_blocking(i2c0, p_platform->address, buffer, sizeof(buffer), false);
return (result < 0) ? 1 : 0;
}
uint8_t VL53L5CX_RdByte(
VL53L5CX_Platform *p_platform,
uint16_t RegisterAdress,
uint8_t *p_value)
{
uint8_t buffer[2];
buffer[0] = (RegisterAdress >> 8) & 0xFF; // High-Byte der Registeradresse
buffer[1] = RegisterAdress & 0xFF; // Low-Byte der Registeradresse
p_platform->address = 0x29;
// Registeradresse senden
int result = i2c_write_blocking(i2c0, p_platform->address, buffer, sizeof(buffer), true);
if (result < 0)
{
return 1; // Fehler
}
// Byte lesen
result = i2c_read_blocking(i2c0, p_platform->address, p_value, 1, false);
return (result < 0) ? 1 : 0; // 0 = Erfolg, 1 = Fehler
}
uint8_t VL53L5CX_WrMulti(
VL53L5CX_Platform *p_platform,
uint16_t RegisterAdress,
uint8_t *p_values,
uint32_t size)
{
p_platform->address = 0x29;
uint8_t buffer[2 + size];
buffer[0] = (RegisterAdress >> 8) & 0xFF; // High-Byte der Registeradresse
buffer[1] = RegisterAdress & 0xFF; // Low-Byte der Registeradresse
memcpy(&buffer[2], p_values, size); // Daten kopieren
int result = i2c_write_blocking(i2c0, p_platform->address, buffer, sizeof(buffer), false);
return (result < 0) ? 1 : 0; // 0 = Erfolg, 1 = Fehler
}
uint8_t VL53L5CX_RdMulti(
VL53L5CX_Platform *p_platform,
uint16_t RegisterAdress,
uint8_t *p_values,
uint32_t size)
{
p_platform->address = 0x29;
uint8_t buffer[2];
buffer[0] = (RegisterAdress >> 8) & 0xFF; // High-Byte der Registeradresse
buffer[1] = RegisterAdress & 0xFF; // Low-Byte der Registeradresse
// Registeradresse senden
int result = i2c_write_blocking(i2c0, p_platform->address, buffer, sizeof(buffer), true);
if (result < 0)
{
return 1; // Fehler
}
// Daten lesen
result = i2c_read_blocking(i2c0, p_platform->address, p_values, size, false);
return (result < 0) ? 1 : 0; // 0 = Erfolg, 1 = Fehler
}
uint8_t VL53L5CX_Reset_Sensor(
VL53L5CX_Platform *p_platform)
{
uint8_t status = 0;
/* (Optional) Need to be implemented by customer. This function returns 0 if OK */
/* Set pin LPN to LOW */
/* Set pin AVDD to LOW */
/* Set pin VDDIO to LOW */
VL53L5CX_WaitMs(p_platform, 100);
/* Set pin LPN of to HIGH */
/* Set pin AVDD of to HIGH */
/* Set pin VDDIO of to HIGH */
VL53L5CX_WaitMs(p_platform, 100);
return status;
}
void VL53L5CX_SwapBuffer(
uint8_t *buffer,
uint16_t size)
{
uint32_t i, tmp;
/* Example of possible implementation using <string.h> */
for(i = 0; i < size; i = i + 4)
{
tmp = (
buffer[i]<<24)
|(buffer[i+1]<<16)
|(buffer[i+2]<<8)
|(buffer[i+3]);
memcpy(&(buffer[i]), &tmp, 4);
}
}
uint8_t VL53L5CX_WaitMs(
VL53L5CX_Platform *p_platform,
uint32_t TimeMs)
{
uint8_t status = 255;
/* Need to be implemented by customer. This function returns 0 if OK */
return status;
}
2025-01-23 07:13 AM
A couple of comments:
Check Gethub. The linux community is good at sharing code. I found GitHub - akionu/pico-vl53l5cx: A Tiny Library for ST's VL53L5CX using Raspberry Pi Pico C/C++ SDK
which looks very promising.
In my experience the i2c issues generally boil down to byte-swap (and word-swap) issues.
Sometimes I see a set-up with long wires, and that can cause electrical issues. But if you aren't getting anywhere at the start, it's a byte-swap issue.
Checkout the github, and see what they did. That should get you going.
- john