title: VL53L0x sensor Ranging error. How to solve the error?
body: <p>I'm trying to interface vl53l0x sensor with my atSamE51A19 controller. I can send the data to the sensor. As it has established connection I send identification code and it responds. But further initialization and ranging is not working.</p>
  <p>I'm using stm library implementation on my sam board. [stmArduinoLib][1]
  The only changes I made is in the read write functions. It was using stm base functions but I'm using different functions.</p>
  <pre><code>    /****************** Write and read functions from I2C *************************/
  
  VL53L0X_Error VL53L0X::VL53L0X_WriteMulti(VL53L0X_DEV Dev, uint8_t index, uint8_t *pdata, uint32_t count)
  {
      int status;
  
      status = VL53L0X_I2CWrite(index, pdata, (uint16_t) count);
      if (status == 0)
      {
          status = VL53L0X_ERROR_CONTROL_INTERFACE;
      }
      else
      {
          status = VL53L0X_ERROR_NONE;
      }
      return status;      
  }
  
  VL53L0X_Error VL53L0X::VL53L0X_ReadMulti(VL53L0X_DEV Dev, uint8_t index, uint8_t *pdata, uint32_t count)
  {
      int status;
  
      if (count &gt;= VL53L0X_MAX_I2C_XFER_SIZE)
      {
          status = VL53L0X_ERROR_INVALID_PARAMS;
      }
  
      status = VL53L0X_I2CRead(index, pdata, (uint16_t) count);
  
      return status;  
  }
  
  VL53L0X_Error VL53L0X::VL53L0X_WrByte(VL53L0X_DEV Dev, uint8_t index, uint8_t data)
  {
      int status;
  
      status = WriteRegister(index, data);    //VL53L0X_I2CWrite(index, &amp;data, 1);
  
      if (status == 0)
      {
          status = VL53L0X_ERROR_CONTROL_INTERFACE;
      }
      else
      {
          status = VL53L0X_ERROR_NONE;
      }
  
      return status;  
  }
  
  VL53L0X_Error VL53L0X::VL53L0X_WrWord(VL53L0X_DEV Dev, uint8_t index, uint16_t data)
  {
      int status;
      int32_t status_int;
      alignas(2) uint8_t buffer[2];
  
      buffer[0] = data &gt;&gt; 8;
      buffer[1] = data &amp; 0x00FF;
  
      status_int = VL53L0X_I2CWrite(index, (uint8_t*) buffer, 2);
  
      // buffer[0] = MSB
      // buffer[1]
      // buffer[2]
      // buffer[3] = LSB
      if (status_int == 0)
      {
          status = VL53L0X_ERROR_CONTROL_INTERFACE;
      }
      else
      {
          status = VL53L0X_ERROR_NONE;
      }
  
      return status;  
  }
  
  VL53L0X_Error VL53L0X::VL53L0X_WrDWord(VL53L0X_DEV Dev, uint8_t index, uint32_t data)
  {
      int status;
      int32_t status_int;
      alignas(2) uint8_t buffer[4];
  
      buffer[0] = (data &gt;&gt; 24) &amp; 0xFF;                // MSB
      buffer[1] = (data &gt;&gt; 16) &amp; 0xFF;
      buffer[2] = (data &gt;&gt; 8) &amp; 0xFF;
      buffer[3] = (data &gt;&gt; 0) &amp; 0xFF;                 // LSB
      status_int = VL53L0X_I2CWrite(index, buffer, 4);
  
      if (status_int == 0)
      {
          status = VL53L0X_ERROR_CONTROL_INTERFACE;
      }
      else
      {
          status = VL53L0X_ERROR_NONE;
      }
  
      return status;  
  }
  
  VL53L0X_Error VL53L0X::VL53L0X_RdByte(VL53L0X_DEV Dev, uint8_t index, uint8_t *data)
  {
      int status = VL53L0X_ERROR_NONE;
  
      status = ReadRegister(index, *data);    //
      if (status)
      {
          status = VL53L0X_ERROR_NONE;
          return status;
      }
      else
      {
          status = VL53L0X_ERROR_CONTROL_INTERFACE;
      }
      return status;
  }
  
  VL53L0X_Error VL53L0X::VL53L0X_RdWord(VL53L0X_DEV Dev, uint8_t index, uint16_t *data)
  {
      int status = 0;
      uint8_t buffer[2] =
      { 0, 0 };
  
      status = VL53L0X_I2CRead(index, buffer, 2);
      if (status == 0)
      {
          *data = (uint16_t) (buffer[0] &lt;&lt; 8) | (buffer[1] &amp; 0xFF);       // | ? +
      }
  
      return status;          
  }
  
  VL53L0X_Error VL53L0X::VL53L0X_RdDWord(VL53L0X_DEV Dev, uint8_t index, uint32_t *data)
  {
      int status;
      uint8_t buffer[4] =
      { 0, 0, 0, 0 };
  
      status = VL53L0X_I2CRead(index, buffer, 4);
      if (!status)                                    // use OR
      {
          *data = ((uint32_t) buffer[0] &lt;&lt; 24) | ((uint32_t) buffer[1] &lt;&lt; 16) | ((uint32_t) buffer[2] &lt;&lt; 8) | (uint32_t) buffer[3];
      }
      return status;
  
  }
  
  VL53L0X_Error VL53L0X::VL53L0X_UpdateByte(VL53L0X_DEV Dev, uint8_t index, uint8_t AndData, uint8_t OrData)
  {
      int status;
      uint8_t buffer = 0;
  
      /* read data direct onto buffer */
      status = VL53L0X_I2CRead(index, &amp;buffer, 1);
      if (!status)
      {
          buffer = (buffer &amp; AndData) | OrData;
          status = VL53L0X_I2CWrite(index, &amp;buffer, (uint8_t) 1);
      }
  
      if (status == 0)
      {
          status = VL53L0X_ERROR_CONTROL_INTERFACE;
      }
      else
      {
  
          status = VL53L0X_ERROR_NONE;
      }
      return status;
  }
  
  VL53L0X_Error VL53L0X::VL53L0X_I2CWrite(uint8_t RegisterAddr, uint8_t *pBuffer, uint16_t NumByteToWrite)
  {
      return WriteRegisters(RegisterAddr, pBuffer, NumByteToWrite);
  }
  
  VL53L0X_Error VL53L0X::VL53L0X_I2CRead(uint8_t RegisterAddr, uint8_t *pBuffer, uint16_t NumByteToRead)
  {
      alignas(2) uint8_t vpBuffer[5];
      /*  dev_i2c-&gt;beginTransmission(((uint8_t) (((DeviceAddr) &gt;&gt; 1) &amp; 0x7F)));
       */
  
      if (ReadRegisters(RegisterAddr, vpBuffer, NumByteToRead))
      {
          pBuffer = vpBuffer;
          return 0;
      }
  
      return 1;
  }
  </code></pre>
  <p>In this functions I'm not sending device address everytime just sending the index and data. In constructor only I've set the address once. The only error I'm getting when I use Status = <code>VL53L0X_PerformRefSpadManagement(Device, &amp;refSpadCount, &amp;isApertureSpads);</code> This function. In initialization. Don't know how to solve this issue. Tried to change the mode to continous that didn't help. Note: the sensor is working fine with arduino.
  [1]: <a href="https://github.com/stm32duino/VL53L0X" rel="nofollow noreferrer">https://github.com/stm32duino/VL53L0X</a></p>