AnsweredAssumed Answered

Ported VL53L0X Pololu code to STM32 - Always times out, can't find error!

Question asked by Adam Halliwell on May 29, 2018
Latest reply on Jun 4, 2018 by Adam Halliwell

Hi all


In lieu of the VL53L0X API being so awkward to use, I've made an effort to port the Pololu version of the VL53L0X library to STM32-compatible C code, using HAL functions to manage I2C communication.


Using an STM32F072RB board, with fast mode I2C (400KHz) and the "D3" pin as XSHUT1, the setup and operation of the sensor in main.c goes as follows (removed surrounding code for readability)


VL53L0X sensor1;
char msg[64];
HAL_GPIO_WritePin(XSHUT1_GPIO_Port, XSHUT1_Pin,false);
HAL_GPIO_WritePin(XSHUT1_GPIO_Port, XSHUT1_Pin,true);
   snprintf(msg,sizeof(msg),"Failed to initialize\r\n");
   HAL_UART_Transmit(UART, (uint8_t*)msg, strlen(msg), 0xFFFF);
   snprintf(msg,sizeof(msg),"Successfully initialized\r\n");
   HAL_UART_Transmit(UART, (uint8_t*)msg, strlen(msg), 0xFFFF);


      snprintf(msg,sizeof(msg),"Read value: %u\r\n");//,readRangeContinuousMillimeters(&sensor1));
      HAL_UART_Transmit(UART, (uint8_t*)msg, strlen(msg), 0xFFFF);


Unfortunately the function readRangeContinuousMillimeters(...) always returns a timeout (65536), as if the sensor is never initialised to read continuously (but, as shown in the code, it definitely is). Furthermore, reading WHO_AM_I always returns 0xEE which is correct and proves that the I2C bus is working.


Every function in the VL53L0X library has been ported over appropriately, and I've scanned every line of code to try and find the source of the problem. However, zero luck as of yet.


I have attached both the Pololu and my own version of the VL53L0X library, plus a custom I2C library which makes use of HAL functions, and the full main.c of this project.

If anyone is able to play spot-the-difference in order to get this working I'd be eternally grateful, as I'm completely stumped. Not that it's much in return, but this code is free for anyone to use if they can get it working.