Skip to main content
Associate
April 2, 2025
Question

VL53L0X API Library - division by Zero fault in VL53L0X_calc_timeout_mclks

  • April 2, 2025
  • 1 reply
  • 615 views

Hello ST Team!

In one of my new projects i use the VL53L0X TOF Sensor to measure simple distances.

The mcu i use is the STM32F103.

To get values from the sensor i use the ST API Library. It's kind of s special version since i use the modified one which is working with the Cube MX Hal.

This is the repository i cloned and pointing to the master branch:

STM32CubeL4/Projects/B-L475E-IOT01A/Applications/Proximity at master · STMicroelectronics/STM32CubeL4 · GitHub

As this project is for the L4 i just replaced the hal include from 

#include "stm32l4xx_hal.h" to  #include "stm32f1xx_hal.h"

in the vl53l0x_platform.h file. 

1. The INIT VL53L0X_PROXIMITY_Init() finishes without errors

2. The problem occurs when i do  VL53L0X_PROXIMITY_GetDistance() 

Within the function VL53L0X_calc_timeout_mclks() the division by zero fault occurs and i don't know exactly why since the range measurement is already done correctly.

Following depicts the callstack before the divsion happens.

LukasA93_0-1743615159535.png

Additionally the "DeviceSpecificParameters"

LukasA93_1-1743615251795.png

If I work around this the next division by zero is following immediatly.

LukasA93_2-1743615848780.png

 

Can you give me some hints why the zero division is happening? 
Do i have to set some parameters as mentioned here? : VL53L0X Divide by Zero error in API - STMicroelectronics Community

Problem is that i do not have the VL53L0_i2c_platfform.c platform files.

 

Thank you in advance!
Regards, Lukas

1 reply

John E KVAM
ST Employee
April 2, 2025

I doubt yours is the same error. His was a porting error and you are using an STM32, so the timing functions should work. 

I will grant you that one really should follow the advice in that issue though. 

"...by checking for a zero value (and setting to 1) before doing the (divide)."

But yours is the first mention I've heard of in 4 years, so I'm going to guess it's your configuration.

Although I really can't point to where you might have gone wrong. 

Could you post all your sensor initialization calls?

I'm betting there is something amiss in those.

- john 

LukasA93Author
Associate
April 2, 2025

Thank you for your fast reply!

 

1. VL53L0X_PROXIMITY_Init();

2. VL53L0X_PROXIMITY_GetDistance();

This is the Init routine:

/**
 * @brief VL53L0X proximity sensor Initialization.
 */
 static void VL53L0X_PROXIMITY_Init(void)
 {
 uint16_t vl53l0x_id = 0; 
 VL53L0X_DeviceInfo_t VL53L0X_DeviceInfo;
 
 /* Initialize IO interface */
 //SENSOR_IO_Init(); --> I2C is already initialized in main.c
 VL53L0X_PROXIMITY_MspInit();
 
 memset(&VL53L0X_DeviceInfo, 0, sizeof(VL53L0X_DeviceInfo_t));
 
 if (VL53L0X_ERROR_NONE == VL53L0X_GetDeviceInfo(&Vl53Instance, &VL53L0X_DeviceInfo))
 { 
	 if (VL53L0X_ERROR_NONE == VL53L0X_RdWord(&Vl53Instance, VL53L0X_REG_IDENTIFICATION_MODEL_ID, (uint16_t *) &vl53l0x_id))
	 {
	 if (vl53l0x_id == VL53L0X_ID)
	 {
		 if (VL53L0X_ERROR_NONE == VL53L0X_DataInit(&Vl53Instance))
		 {
			Vl53Instance.Present = 1;
		 SetupSingleShot(Vl53Instance);
		 }
		 else
		 { 
		 printf("VL53L0X Time of Flight Failed to send its ID!\n");
		 }
	 }
	 }
	 else
	 {
	 printf("VL53L0X Time of Flight Failed to Initialize!\n");
	 }
 }
 else
 {
	 printf("VL53L0X Time of Flight Failed to get infos!\n");
 } 
 }

Additional Msp Init function and the GetDistance function

/**
 * @brief VL53L0X proximity sensor Msp Initialization.
 */
 static void VL53L0X_PROXIMITY_MspInit(void)
 {
 // done at hal init
 //GPIO_InitTypeDef GPIO_InitStruct;
 //
 ///*Configure GPIO pin : VL53L0X_XSHUT_Pin */
 //GPIO_InitStruct.Pin = VL53L0X_XSHUT_Pin;
 //GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
 //GPIO_InitStruct.Pull = GPIO_PULLUP;
 //GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;
 //HAL_GPIO_Init(VL53L0X_XSHUT_GPIO_Port, &GPIO_InitStruct);
 
 HAL_GPIO_WritePin(VL53L0X_XSHUT_GPIO_Port, VL53L0X_XSHUT_Pin, GPIO_PIN_SET);
 
 HAL_Delay(1000); 
 }

 /**
 * @brief Get distance from VL53L0X proximity sensor.
 * @retval Distance in mm
 */
static uint16_t VL53L0X_PROXIMITY_GetDistance(void)
{
 VL53L0X_RangingMeasurementData_t RangingMeasurementData;
 // TODO - Error handling
 VL53L0X_PerformSingleRangingMeasurement(&Vl53Instance, &RangingMeasurementData);
 
 return RangingMeasurementData.RangeMilliMeter; 
}

 Additionally attached the vl53l0x_tof.c file where the SetupSingleShot function is defined.

Basically it is all the same as in the nucleo example mentioned in the first post.

 

Regards, Lukas