Skip to main content
Associate III
March 28, 2025
Solved

Getting data from VL53L8CX using vl53l8cx_api

  • March 28, 2025
  • 1 reply
  • 533 views

Hello  everyone, 

I started learning programming on the STM32 ecosystem a few weeks ago, and I'm currently trying to get data from the VL53L8CX sensor using the NUCLEO-F401RE expansion boards and the vl53l8cx_api. I wrote a small program based on what I have seen in other projects, but my code doesn't work at all. While debugging, I discovered that the program jumps to the HardFault_Handler() function at this line in vl53l8cx_init().

 
	status |= VL53L8CX_WrByte(&(p_dev->platform), 0x7fff, 0x00);

Here is the essential part of my code maybe someone have any solution:

#include "vl53l8cx_api.h"
#include "main.h"
#include "stm32f4xx_hal.h"
#include <stdio.h>

// Private variables ---------------------------------------------------------
static VL53L8CX_Configuration tof_dev; // Sensor-Konfiguration
static VL53L8CX_ResultsData results; // Ergebnis der Messung
static int32_t status ; // Status für Fehlerbehandlung

static void MX_VL53L8CX_SimpleRanging_Process(void);
static void print_result(VL53L8CX_ResultsData *results) ;
static void MX_VL53L8CX_SimpleRanging_Init(void);
void ToF_EventDetected(void);


#define TIMING_BUDGET (30U) // Timing-Budget (ms)
#define RANGING_FREQUENCY (10U) // Messfrequenz (Hz)
#define POLLING_PERIOD (1) // Polling-Periode (ms)

// Initialisierungs-Funktion für VL53L8CX
 void MX_TOF_Init(void)
{
 // Initialisiere die Peripherie und den Sensor
 MX_VL53L8CX_SimpleRanging_Init();
}

// Polling-Task für die Messungen
void MX_TOF_Process(void)
{
 MX_VL53L8CX_SimpleRanging_Process();
}

// Funktion zur Initialisierung des Sensors
void MX_VL53L8CX_SimpleRanging_Init(void)
{
 // Sensor-Reset (falls erforderlich)
 HAL_GPIO_WritePin(GPIOB, GPIO_PIN_0, GPIO_PIN_RESET); // Beispiel für Reset-Pin
 HAL_Delay(2);
 HAL_GPIO_WritePin(GPIOB, GPIO_PIN_0, GPIO_PIN_SET); // Reset-Pin auf High setzen
 HAL_Delay(2);


 // Initialisierung des Sensors
 printf("VL53L8CX Simple Ranging demo application\n");
 printf("Sensor initialization...\n");

 tof_dev.platform.address = VL53L8CX_DEFAULT_I2C_ADDRESS ;
 status += vl53l8cx_init(&tof_dev);
 status += vl53l8cx_set_ranging_mode(&tof_dev , VL53L8CX_RANGING_MODE_AUTONOMOUS );
 status += vl53l8cx_set_resolution(&tof_dev ,VL53L8CX_RESOLUTION_8X8 );
 // Sensor-Konfiguration
 vl53l8cx_set_integration_time_ms(&tof_dev, TIMING_BUDGET);
 vl53l8cx_set_ranging_frequency_hz(&tof_dev, RANGING_FREQUENCY);
 vl53l8cx_start_ranging(&tof_dev); // Startet den kontinuierlichen Messmodus
}

// Funktion für den Messprozess
static void MX_VL53L8CX_SimpleRanging_Process(void)
{
	status = vl53l8cx_get_ranging_data(&tof_dev, &results);
 if (status != VL53L8CX_STATUS_ERROR)
 {
 print_result(&results);
 }
 HAL_Delay(POLLING_PERIOD);
}

// Ergebnis ausdrucken
static void print_result(VL53L8CX_ResultsData *results)
{
 char buffer[128]; // Buffer für die serielle Ausgabe

 for (int i = 0; i < 64; i++) // Sensor liefert 64 Werte (8x8 Matrix)
 {
 uint16_t distance = results->distance_mm[i]; // Korrekte Strukturzugriffsnotation

 // Formatierte Ausgabe für jeden Wert
 snprintf(buffer, sizeof(buffer), "Pixel %d: Distance = %d mm\n", i, distance);
 HAL_UART_Transmit(&huart2, (uint8_t*)buffer, strlen(buffer), HAL_MAX_DELAY);
 }

 // Falls nur der erste Wert gesendet werden soll:
 snprintf(buffer, sizeof(buffer), "First Distance: %d mm\n", results->distance_mm[0]);
 HAL_UART_Transmit(&huart2, (uint8_t*)buffer, strlen(buffer), HAL_MAX_DELAY);
}

void ToF_EventDetected(void)
{
 // optional: printf("Event detected\n");
}



#ifdef __cplusplus
}
#endif

 

Best answer by John E KVAM

You have a coding error. But you were oh so close.

In the examples referenced by Anne above there is the line

#define VL53L8CX_DEFAULT_I2C_ADDRESS	 ((uint16_t)0x52)
...
Dev.platform.address = VL53L8CX_DEFAULT_I2C_ADDRESS;

 

So it's my guess is that you never entered the I2C address into the Dev Structure before you called the init funtion. And that is why the whole thing faulted.

- john

 

1 reply

Anne BIGOT
Technical Moderator
April 18, 2025

Hello,

Did you have a look to the example codes published within the STSW-IM040 software package ?

STSW-IMG040\VL53L8CX_ULD_driver_2.0.1\Examples

It may help

 

Our community relies on fruitful exchanges and good quality content. You can thank and reward helpful and positive contributions by marking them as 'Accept as Solution'. When marking a solution, make sure it answers your original question or issue that you raised. ST Employees that act as moderators have the right to accept the solution, judging by their expertise. This helps other community members identify useful discussions and refrain from raising the same question. If you notice any false behavior or abuse of the action, do not hesitate to 'Report Inappropriate Content'
John E KVAM
John E KVAMBest answer
ST Employee
April 23, 2025

You have a coding error. But you were oh so close.

In the examples referenced by Anne above there is the line

#define VL53L8CX_DEFAULT_I2C_ADDRESS	 ((uint16_t)0x52)
...
Dev.platform.address = VL53L8CX_DEFAULT_I2C_ADDRESS;

 

So it's my guess is that you never entered the I2C address into the Dev Structure before you called the init funtion. And that is why the whole thing faulted.

- john