cancel
Showing results for 
Search instead for 
Did you mean: 

Making a demo for VL53L3CX to manage different ToFs together using interrupt? I found MultiRangingSensor demo for VL53L1X and trying to modify it to work for VL53L3CX ,I have noticed that the code works for sensors sequentially!

HElni.1
Associate II
void HAL_GPIO_EXTI_Callback(uint16_t GPIO_Pin)
{
  if (GPIO_Pin==VL53L1X_INT_Pin)
  {
    IntCount++;
  }
 
}
 
/* USER CODE END 0 */
 
int main(void)
{
  /* USER CODE BEGIN 1 */
 
  uint8_t byteData;
  uint16_t wordData;
  uint8_t ToFSensor =1; // 0=Left, 1=Center(default), 2=Right
 
  uint8_t newI2C = 0x52;
 
  /* MCU Configuration----------------------------------------------------------*/
 
  /* Reset of all peripherals, Initializes the Flash interface and the Systick. */
  HAL_Init();
 
  /* Configure the system clock */
  SystemClock_Config();
 
  /* Initialize all configured peripherals */
  MX_GPIO_Init();
 
  MX_USART2_UART_Init();
 
  MX_I2C1_Init();
 
  XNUCLEO53L3A2_Init();
 
  /* USER CODE END 1 */
 
  /* USER CODE BEGIN 2 */
 
 
  /* Allow to select the sensor to be used, multi-sensor is not managed in this example.
  Only when use use the Left ToF in interrupt mode,  solder the U7 on the X-Nucleo-53L3A2 board
  Only when use the Right ToF in interrupt mode, solder the U7 on the X-Nucleo-53L3A2 board
  See "Solder drop configurations" in the X-Nucleo-53L3A2 User Manual for more details */
  //ToFSensor = 1; // Select ToFSensor: 0=Left, 1=Center, 2=Right
  for (ToFSensor=0;ToFSensor<3;ToFSensor++){
	 XNUCLEO53L3A2_ResetId(ToFSensor, 0); // Reset ToF sensor
  }
 
 
  for (ToFSensor=0;ToFSensor<3;ToFSensor++)
 
   {
	  printf("VL53L1X Examples...\n");
   switch(ToFSensor)
 
   {
 
   case 0:
 
	   Dev=&devLeft;
 
	   break;
 
   case 1:
 
	   Dev=&devCenter;
 
	   break;
 
   case 2:
 
	   Dev=&devRight;
 
	   break;
 
 
 
   }
 
   status=XNUCLEO53L3A2_ResetId(ToFSensor, 1);
   Dev->comms_speed_khz = 400;
 
    Dev->I2cHandle = &hi2c1;
 
    Dev->comms_type = 1;
 
    Dev->I2cDevAddr=0x52;
 
    VL53LX_RdByte(Dev, 0x010F, &byteData);
 
    printf("\n\rVL53LX Model_ID: %02X\n", byteData);
 
    VL53LX_RdByte(Dev, 0x0110, &byteData);
 
    printf("VL53LX Module_Type: %02X\n", byteData);
 
    VL53LX_RdWord(Dev, 0x010F, &wordData);
 
    printf("VL53LX: %02X\n\n", wordData);
 
 
    //HAL_Delay(4);
 
 
 
    newI2C = Dev->I2cDevAddr + (ToFSensor+1)*2;
 
    status = VL53LX_SetDeviceAddress(Dev, newI2C);
 
    Dev->I2cDevAddr=newI2C;
 
 //   VL53LX_RdWord(Dev, 0x010F, &wordData);
 
   // printf("VL53LX: %02X\n\n", wordData);
 
 
 
    /* Device Initialization and setting */
 
  status = VL53LX_WaitDeviceBooted(Dev);
  //printf("WaitDeviceBoot: %d \n", status);
  status = VL53LX_DataInit(Dev);
  //printf("DataInit: %d \n", status);
 
  //status = VL53LX_SetDistanceMode(Dev, VL53LX_DISTANCEMODE_SHORT);
  //printf("SetDistanceMode: %d \n", status);
  status = VL53LX_SetMeasurementTimingBudgetMicroSeconds(Dev, 500000);
  printf("SetMeasurementTimingBudge: %d \n", status);
  status = VL53LX_StartMeasurement(Dev);
  //printf("start measurement value: %d \n", status);
 
    }
  /* Sequential ranging loop */
  	while(1){
  		for (ToFSensor=0;ToFSensor<3;ToFSensor++){
  			switch(ToFSensor){
  				case 0:
  					Dev=&devLeft;
  					break;
  				case 1:
  					Dev=&devCenter;
  					break;
  				case 2:
  					Dev=&devRight;
  					break;
  			}
  			status = VL53LX_StartMeasurement(Dev);
  		  status = VL53LX_WaitMeasurementDataReady(Dev);
  			if(!status)
  			{
 
  				status = VL53LX_GetMultiRangingData(Dev, pMultiRangingData);
  				if(status==0){
								no_of_object_found=pMultiRangingData->NumberOfObjectsFound;
								//printf("Count=%5d, ", pMultiRangingData->StreamCount);
								printf("#Objs=%1d ", no_of_object_found);
								for(j=0;j<no_of_object_found;j++){
								if(j!=0)printf("\n                     ");
								printf("\n\r Dev_no%i, status=%d, D=%5dmm, Signal=%2.2f Mcps, Ambient=%2.2f Mcps\n",
								ToFSensor,
								pMultiRangingData->RangeData[j].RangeStatus,
								pMultiRangingData->RangeData[j].RangeMilliMeter,
								pMultiRangingData->RangeData[j].SignalRateRtnMegaCps/65536.0,
								pMultiRangingData->RangeData[j].AmbientRateRtnMegaCps/65536.0);
								HAL_Delay(2);
								}
 
  					//printf("\nDev_no%i,distance = %5d \n",ToFSensor, pMultiRangingData->RangeData[0].RangeMilliMeter);
 
  				}
  				status = VL53LX_ClearInterruptAndStartMeasurement(Dev);
  			}
  		}
  	}
 
 //RangingLoop();
 
  /* USER CODE END 2 */
 
  /* Infinite loop */
  /* USER CODE BEGIN WHILE */
  //while (1)
 // {
    /* USER CODE END WHILE */
 
    /* USER CODE BEGIN 3 */
 
  //}
  /* USER CODE END 3 */
 
}
 
 
 
-------------
/* ranging and display loop */
 
void RangingLoop(void)
{
 
 // uint8_t NewDataReady=0;
  int no_of_object_found=0,j;
  printf("Ranging loop starts\n");
 
  status = VL53LX_WaitDeviceBooted(Dev);
  //printf("WaitDeviceBoot: %d \n", status);
  status = VL53LX_DataInit(Dev);
  //printf("DataInit: %d \n", status);
 
  status = VL53LX_SetDistanceMode(Dev, VL53LX_DISTANCEMODE_SHORT);
  //printf("SetDistanceMode: %d \n", status);
  status = VL53LX_SetMeasurementTimingBudgetMicroSeconds(Dev, 500000);
 // printf("SetMeasurementTimingBudge: %d \n", status);
  status = VL53LX_StartMeasurement(Dev);
  //printf("start measurement value: %d \n", status);
 
 
 
  if(status){
    printf("VL53LX_StartMeasurement failed: error = %d \n", status);
    while(1);
  }
 
  if (isInterrupt){
    do // HW interrupt mode
    {
      __WFI();
      if(IntCount !=0 ){
        IntCount=0;
        status = VL53LX_GetMultiRangingData(Dev, pMultiRangingData);
        if(status==0){
          no_of_object_found=pMultiRangingData->NumberOfObjectsFound;
          printf("Count=%5d, ", pMultiRangingData->StreamCount);
          printf("#Objs=%1d ", no_of_object_found);
          for(j=0;j<no_of_object_found;j++){
            if(j!=0)printf("\n                     ");
            printf("status=%d, D=%5dmm, Signal=%2.2f Mcps, Ambient=%2.2f Mcps",
                   pMultiRangingData->RangeData[j].RangeStatus,
                   pMultiRangingData->RangeData[j].RangeMilliMeter,
                   pMultiRangingData->RangeData[j].SignalRateRtnMegaCps/65536.0,
                   pMultiRangingData->RangeData[j].AmbientRateRtnMegaCps/65536.0);
          }
          printf ("\n");
          status = VL53LX_ClearInterruptAndStartMeasurement(Dev);
        }
        else{
          printf("VL53LX_GetMultiRangingData failed: error = %d \n", status);
          while(1);
        }
      }
    }
    while(1);
  }
 
}

By executing this code and using TeraTerm I can see the ranges and all but still It doesn't not even entering the RangingLoop not through Interrupt or Polling so I would like to know what could be the problem...Thanks in advance

4 REPLIES 4
John E KVAM
ST Employee

the status = VL53LX_WaitMeasurementDataReady(Dev); command will wait until the measurement is done.

you need to put a for-loop around the start measurement - so all the sensors will get going.

Then put your status = VL53LX_WaitMeasurementDataReady(Dev); in a second for-loop.

As all 3 sensor are now running simulatously, they will finish at about the same time.

So

For each sensor {

initialize, change address, configure

}

For each sensor{

start ranging

}

while 1 {

For each sensor {

status = VL53LX_WaitMeasurementDataReady(Dev);

print results

status = VL53LX_ClearInterruptAndStartMeasurement(Dev);

}

}


If this or any post solves your issue, please mark them as 'Accept as Solution' It really helps. And if you notice anything wrong do not hesitate to 'Report Inappropriate Content'. Someone will review it.

Thank you for your reply,I was wondering about the GPIO1 for interrupt,it's written:"the VL53L3CX interrupt of the left and right breakout boards, GPIO1_L and GPIO1_R, can be shared

with the VL53L3CX interrupt on the main board, GPIO1, by fitting U7 and U8 solder drops. By default

U7 and U8 are not fitted." So in order to activate the interrupt,and run the code successfully for the 3 sensors together,I need to solder U7 and U8 togther(to become fitted)?...Thank you in advance

John E KVAM
ST Employee

I don't think I know the answer to that.

But I can help you.

I'm including the PDF of the board. It will show you exactly what is being done.

this board is for the VL53L1, but because all the VL53 sensors have the same footprint, all these expansion boards are same. - We just changed the sensor itself.

  • john

If this or any post solves your issue, please mark them as 'Accept as Solution' It really helps. And if you notice anything wrong do not hesitate to 'Report Inappropriate Content'. Someone will review it.

May sharing this materials from VL53L3A2 USER MANUAL can help?0693W000005CMSVQA4.jpg0693W000005CMSGQA4.jpg0693W000005CMS6QAO.jpg