Skip to main content
Visitor II
September 28, 2020
Solved

Hello everyone I downloaded the API for VL53L3 range sensor. I try to compile the API with Eclipse but the file ranging_sensor_comms.h is missing (included in vl53lx_platform.c). What is this file? Where is this file documented? Thank you for your h

  • September 28, 2020
  • 8 replies
  • 4233 views

Ultimately, I would like to know where I2C commands are described in order to re-write my own I2C R/W function according to my hardware. First I woud like to play with the entire API to get familiar with it. The file ranging_sensor_comms.h is not included in the STM32 53L3A2 expansion board software. The vl53lx_platform.c is completely re-written,and the project only uses this file frome the API and none of the other files. Thank you for your help

    This topic has been closed for replies.
    Best answer by Ben1

    UPDATE : the answer was dead simple : I forgot to shift the I2C adress like this :

    status = (int8_t)bus_i2c_ReadData(DevAddr>>1, pdata, pdev->i2c_slave_address + index, count);

    here is my final code for those who have the issue aswell :

    VL53LX_Error VL53LX_ReadMulti(VL53LX_Dev_t *pdev, uint16_t index, uint8_t *pdata, uint32_t count)
    {
    	VL53LX_Error status = VL53LX_ERROR_NONE;
    	uint8_t DevAddr = pdev->I2CDevAddr;
     int32_t status_int;
     uint8_t I2C_buff[2] = {0};
     
     if (count>=VL53LX_COMMS_BUFFER_SIZE)
     {
     status = VL53LX_ERROR_INVALID_PARAMS;
     }
     
     if (count > (VL53LX_MAX_STRING_LENGTH_PLT))
     {
     return NRF_ERROR_INVALID_LENGTH;
     }
     
     status_int = bus_i2c_ReadData(DevAddr>>1, pdata, index, count);
     
    	if (status_int != 0)
    		status = VL53LX_ERROR_CONTROL_INTERFACE;
     
    	return status;
    }

    8 replies

    ST Employee
    September 28, 2020

    As delivered the API does everything you need right down to where the code sends the I2C commands. We generally don't assume we know which processor you are running on, so the platform.c and platform.h are left to you.

    But clearly someone left the PC test code in there.

    Platform.c and platform.h should have been empty prototypes.

    I'm going to guess - and it's a big guess that you have an STM32.

    If so the following platform.c and should work.

    The post gets long, so I'm going to put the .h file in the next post

    //?????#include "hal.h"
    #include "vl53l0x_platform.h"
    #include "vl53l0x_api.h"
     
    #include "stm32xxx_hal.h"
    #include <string.h>
     
    #define I2C_TIME_OUT_BASE 10
    #define I2C_TIME_OUT_BYTE 1
    #define VL53L0X_OsDelay(...) HAL_Delay(2)
     
     
    #ifndef HAL_I2C_MODULE_ENABLED
    #warning "HAL I2C module must be enable "
    #endif
    //extern I2C_HandleTypeDef hi2c1;
    //#define VL53L0X_pI2cHandle (&hi2c1)
     
    /* when not customized by application define dummy one */
    #ifndef VL53L0X_GetI2cBus
    /** This macro can be overloaded by user to enforce i2c sharing in RTOS context
     */
    # define VL53L0X_GetI2cBus(...) (void)0
    #endif
     
    #ifndef VL53L0X_PutI2cBus
    /** This macro can be overloaded by user to enforce i2c sharing in RTOS context
     */
    # define VL53L0X_PutI2cBus(...) (void)0
    #endif
     
    #ifndef VL53L0X_OsDelay
    # define VL53L0X_OsDelay(...) (void)0
    #endif
     
     
    uint8_t _I2CBuffer[64];
     
    int _I2CWrite(VL53L0X_DEV Dev, uint8_t *pdata, uint32_t count) {
     int status;
     int i2c_time_out = I2C_TIME_OUT_BASE+ count* I2C_TIME_OUT_BYTE;
     
     status = HAL_I2C_Master_Transmit(Dev->I2cHandle, Dev->I2cDevAddr, pdata, count, i2c_time_out);
     if (status) {
     //VL6180x_ErrLog("I2C error 0x%x %d len", dev->I2cAddr, len);
     //XNUCLEO6180XA1_I2C1_Init(&hi2c1);
     }
     return status;
    }
     
    int _I2CRead(VL53L0X_DEV Dev, uint8_t *pdata, uint32_t count) {
     int status;
     int i2c_time_out = I2C_TIME_OUT_BASE+ count* I2C_TIME_OUT_BYTE;
     
     status = HAL_I2C_Master_Receive(Dev->I2cHandle, Dev->I2cDevAddr|1, pdata, count, i2c_time_out);
     if (status) {
     //VL6180x_ErrLog("I2C error 0x%x %d len", dev->I2cAddr, len);
     //XNUCLEO6180XA1_I2C1_Init(&hi2c1);
     }
     return status;
    }
     
    // the ranging_sensor_comms.dll will take care of the page selection
    VL53L0X_Error VL53L0X_WriteMulti(VL53L0X_DEV Dev, uint8_t index, uint8_t *pdata, uint32_t count) {
     int status_int;
     VL53L0X_Error Status = VL53L0X_ERROR_NONE;
     if (count > sizeof(_I2CBuffer) - 1) {
     return VL53L0X_ERROR_INVALID_PARAMS;
     }
     _I2CBuffer[0] = index;
     memcpy(&_I2CBuffer[1], pdata, count);
     VL53L0X_GetI2cBus();
     status_int = _I2CWrite(Dev, _I2CBuffer, count + 1);
     if (status_int != 0) {
     Status = VL53L0X_ERROR_CONTROL_INTERFACE;
     }
     VL53L0X_PutI2cBus();
     return Status;
    }
     
    // the ranging_sensor_comms.dll will take care of the page selection
    VL53L0X_Error VL53L0X_ReadMulti(VL53L0X_DEV Dev, uint8_t index, uint8_t *pdata, uint32_t count) {
     VL53L0X_Error Status = VL53L0X_ERROR_NONE;
     int32_t status_int;
     VL53L0X_GetI2cBus();
     status_int = _I2CWrite(Dev, &index, 1);
     if (status_int != 0) {
     Status = VL53L0X_ERROR_CONTROL_INTERFACE;
     goto done;
     }
     status_int = _I2CRead(Dev, pdata, count);
     if (status_int != 0) {
     Status = VL53L0X_ERROR_CONTROL_INTERFACE;
     }
    done:
     VL53L0X_PutI2cBus();
     return Status;
    }
     
    VL53L0X_Error VL53L0X_WrByte(VL53L0X_DEV Dev, uint8_t index, uint8_t data) {
     VL53L0X_Error Status = VL53L0X_ERROR_NONE;
     int32_t status_int;
     
     _I2CBuffer[0] = index;
     _I2CBuffer[1] = data;
     
     VL53L0X_GetI2cBus();
     status_int = _I2CWrite(Dev, _I2CBuffer, 2);
     if (status_int != 0) {
     Status = VL53L0X_ERROR_CONTROL_INTERFACE;
     }
     VL53L0X_PutI2cBus();
     return Status;
    }
     
    VL53L0X_Error VL53L0X_WrWord(VL53L0X_DEV Dev, uint8_t index, uint16_t data) {
     VL53L0X_Error Status = VL53L0X_ERROR_NONE;
     int32_t status_int;
     
     _I2CBuffer[0] = index;
     _I2CBuffer[1] = data >> 8;
     _I2CBuffer[2] = data & 0x00FF;
     
     VL53L0X_GetI2cBus();
     status_int = _I2CWrite(Dev, _I2CBuffer, 3);
     if (status_int != 0) {
     Status = VL53L0X_ERROR_CONTROL_INTERFACE;
     }
     VL53L0X_PutI2cBus();
     return Status;
    }
     
    VL53L0X_Error VL53L0X_WrDWord(VL53L0X_DEV Dev, uint8_t index, uint32_t data) {
     VL53L0X_Error Status = VL53L0X_ERROR_NONE;
     int32_t status_int;
     _I2CBuffer[0] = index;
     _I2CBuffer[1] = (data >> 24) & 0xFF;
     _I2CBuffer[2] = (data >> 16) & 0xFF;
     _I2CBuffer[3] = (data >> 8) & 0xFF;
     _I2CBuffer[4] = (data >> 0 ) & 0xFF;
     VL53L0X_GetI2cBus();
     status_int = _I2CWrite(Dev, _I2CBuffer, 5);
     if (status_int != 0) {
     Status = VL53L0X_ERROR_CONTROL_INTERFACE;
     }
     VL53L0X_PutI2cBus();
     return Status;
    }
     
    VL53L0X_Error VL53L0X_UpdateByte(VL53L0X_DEV Dev, uint8_t index, uint8_t AndData, uint8_t OrData) {
     VL53L0X_Error Status = VL53L0X_ERROR_NONE;
     uint8_t data;
     
     Status = VL53L0X_RdByte(Dev, index, &data);
     if (Status) {
     goto done;
     }
     data = (data & AndData) | OrData;
     Status = VL53L0X_WrByte(Dev, index, data);
    done:
     return Status;
    }
     
    VL53L0X_Error VL53L0X_RdByte(VL53L0X_DEV Dev, uint8_t index, uint8_t *data) {
     VL53L0X_Error Status = VL53L0X_ERROR_NONE;
     int32_t status_int;
     
     VL53L0X_GetI2cBus();
     status_int = _I2CWrite(Dev, &index, 1);
     if( status_int ){
     Status = VL53L0X_ERROR_CONTROL_INTERFACE;
     goto done;
     }
     status_int = _I2CRead(Dev, data, 1);
     if (status_int != 0) {
     Status = VL53L0X_ERROR_CONTROL_INTERFACE;
     }
    done:
     VL53L0X_PutI2cBus();
     return Status;
    }
     
    VL53L0X_Error VL53L0X_RdWord(VL53L0X_DEV Dev, uint8_t index, uint16_t *data) {
     VL53L0X_Error Status = VL53L0X_ERROR_NONE;
     int32_t status_int;
     
     VL53L0X_GetI2cBus();
     status_int = _I2CWrite(Dev, &index, 1);
     
     if( status_int ){
     Status = VL53L0X_ERROR_CONTROL_INTERFACE;
     goto done;
     }
     status_int = _I2CRead(Dev, _I2CBuffer, 2);
     if (status_int != 0) {
     Status = VL53L0X_ERROR_CONTROL_INTERFACE;
     goto done;
     }
     
     *data = ((uint16_t)_I2CBuffer[0]<<8) + (uint16_t)_I2CBuffer[1];
    done:
     VL53L0X_PutI2cBus();
     return Status;
    }
     
    VL53L0X_Error VL53L0X_RdDWord(VL53L0X_DEV Dev, uint8_t index, uint32_t *data) {
     VL53L0X_Error Status = VL53L0X_ERROR_NONE;
     int32_t status_int;
     
     VL53L0X_GetI2cBus();
     status_int = _I2CWrite(Dev, &index, 1);
     if (status_int != 0) {
     Status = VL53L0X_ERROR_CONTROL_INTERFACE;
     goto done;
     }
     status_int = _I2CRead(Dev, _I2CBuffer, 4);
     if (status_int != 0) {
     Status = VL53L0X_ERROR_CONTROL_INTERFACE;
     goto done;
     }
     
     *data = ((uint32_t)_I2CBuffer[0]<<24) + ((uint32_t)_I2CBuffer[1]<<16) + ((uint32_t)_I2CBuffer[2]<<8) + (uint32_t)_I2CBuffer[3];
     
    done:
     VL53L0X_PutI2cBus();
     return Status;
    }
     
    VL53L0X_Error VL53L0X_PollingDelay(VL53L0X_DEV Dev) {
     VL53L0X_Error status = VL53L0X_ERROR_NONE;
     
     // do nothing
     VL53L0X_OsDelay();
     return status;
    }
     
    //end of file

    ST Employee
    September 28, 2020

    And here is the .h file:

    Note that the L0X, or L1X can be changed depending on which sensor you have.

    /*******************************************************************************
    Copyright � 2015, STMicroelectronics International N.V.
    All rights reserved.
     
    Redistribution and use in source and binary forms, with or without
    modification, are permitted provided that the following conditions are met:
     * Redistributions of source code must retain the above copyright
     notice, this list of conditions and the following disclaimer.
     * Redistributions in binary form must reproduce the above copyright
     notice, this list of conditions and the following disclaimer in the
     documentation and/or other materials provided with the distribution.
     * Neither the name of STMicroelectronics nor the
     names of its contributors may be used to endorse or promote products
     derived from this software without specific prior written permission.
     
    THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
    ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
    WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, AND
    NON-INFRINGEMENT OF INTELLECTUAL PROPERTY RIGHTS ARE DISCLAIMED.
    IN NO EVENT SHALL STMICROELECTRONICS INTERNATIONAL N.V. BE LIABLE FOR ANY
    DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
    (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
    LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
    ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
    (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
    SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
    ********************************************************************************/
     
     
    #ifndef _VL53L0X_PLATFORM_H_
    #define _VL53L0X_PLATFORM_H_
     
    #include "vl53l0x_def.h"
    #include "vl53l0x_platform_log.h"
     
    #ifdef __cplusplus
    extern "C" {
    #endif
     
    #include "stm32xxx_hal.h"
     
     
    #if TRACE_UART
    #define trace_printf uart_printf
    #endif
     
    /**
     * @file vl53l0x_platform.h
     *
     * @brief All end user OS/platform/application porting
     */
     
    /**
     * @defgroup VL53L0X_platform_group VL53L0X Platform Functions
     * @brief VL53L0X Platform Functions
     * @{
     */
     
    /**
     * @struct VL53L0X_Dev_t
     * @brief Generic PAL device type that does link between API and platform abstraction layer
     *
     */
    typedef struct {
     VL53L0X_DevData_t Data; /*!< embed ST Ewok Dev data as "Data"*/
     
     /*!< user specific field */
     
     I2C_HandleTypeDef *I2cHandle;
     uint8_t I2cDevAddr;
     
     char DevLetter;
     
     int Id;
     int Present;
     int 	Enabled;
     int		Ready;
     
     uint8_t comms_type;
     uint16_t comms_speed_khz;
     
     int LeakyRange;
     int LeakyFirst;
     uint8_t RangeStatus;
     uint8_t PreviousRangeStatus;
     FixPoint1616_t SignalRateRtnMegaCps;
     uint16_t EffectiveSpadRtnCount;
     uint32_t StartTime;
     
    } VL53L0X_Dev_t;
     
     
    /**
     * @brief Declare the device Handle as a pointer of the structure @a VL53L0X_Dev_t.
     *
     */
    typedef VL53L0X_Dev_t* VL53L0X_DEV;
     
    /**
     * @def PALDevDataGet
     * @brief Get ST private structure @a VL53L0X_DevData_t data access
     *
     * @param Dev Device Handle
     * @param field ST structure field name
     * It maybe used and as real data "ref" not just as "get" for sub-structure item
     * like PALDevDataGet(FilterData.field)[i] or PALDevDataGet(FilterData.MeasurementIndex)++
     */
    #define PALDevDataGet(Dev, field) (Dev->Data.field)
     
    /**
     * @def PALDevDataSet(Dev, field, data)
     * @brief Set ST private structure @a VL53L0X_DevData_t data field
     * @param Dev Device Handle
     * @param field ST structure field name
     * @param data Data to be set
     */
    #define PALDevDataSet(Dev, field, data) (Dev->Data.field)=(data)
     
     
    /**
     * @defgroup VL53L0X_registerAccess_group PAL Register Access Functions
     * @brief PAL Register Access Functions
     * @{
     */
     
    /**
     * Lock comms interface to serialize all commands to a shared I2C interface for a specific device
     * @param Dev Device Handle
     * @return VL53L0X_ERROR_NONE Success
     * @return "Other error code" See ::VL53L0X_Error
     */
    VL53L0X_Error VL53L0X_LockSequenceAccess(VL53L0X_DEV Dev);
     
    /**
     * Unlock comms interface to serialize all commands to a shared I2C interface for a specific device
     * @param Dev Device Handle
     * @return VL53L0X_ERROR_NONE Success
     * @return "Other error code" See ::VL53L0X_Error
     */
    VL53L0X_Error VL53L0X_UnlockSequenceAccess(VL53L0X_DEV Dev);
     
     
    /**
     * Writes the supplied byte buffer to the device
     * @param Dev Device Handle
     * @param index The register index
     * @param pdata Pointer to uint8_t buffer containing the data to be written
     * @param count Number of bytes in the supplied byte buffer
     * @return VL53L0X_ERROR_NONE Success
     * @return "Other error code" See ::VL53L0X_Error
     */
    VL53L0X_Error VL53L0X_WriteMulti(VL53L0X_DEV Dev, uint8_t index, uint8_t *pdata, uint32_t count);
     
    /**
     * Reads the requested number of bytes from the device
     * @param Dev Device Handle
     * @param index The register index
     * @param pdata Pointer to the uint8_t buffer to store read data
     * @param count Number of uint8_t's to read
     * @return VL53L0X_ERROR_NONE Success
     * @return "Other error code" See ::VL53L0X_Error
     */
    VL53L0X_Error VL53L0X_ReadMulti(VL53L0X_DEV Dev, uint8_t index, uint8_t *pdata, uint32_t count);
     
    /**
     * Write single byte register
     * @param Dev Device Handle
     * @param index The register index
     * @param data 8 bit register data
     * @return VL53L0X_ERROR_NONE Success
     * @return "Other error code" See ::VL53L0X_Error
     */
    VL53L0X_Error VL53L0X_WrByte(VL53L0X_DEV Dev, uint8_t index, uint8_t data);
     
    /**
     * Write word register
     * @param Dev Device Handle
     * @param index The register index
     * @param data 16 bit register data
     * @return VL53L0X_ERROR_NONE Success
     * @return "Other error code" See ::VL53L0X_Error
     */
    VL53L0X_Error VL53L0X_WrWord(VL53L0X_DEV Dev, uint8_t index, uint16_t data);
     
    /**
     * Write double word (4 byte) register
     * @param Dev Device Handle
     * @param index The register index
     * @param data 32 bit register data
     * @return VL53L0X_ERROR_NONE Success
     * @return "Other error code" See ::VL53L0X_Error
     */
    VL53L0X_Error VL53L0X_WrDWord(VL53L0X_DEV Dev, uint8_t index, uint32_t data);
     
    /**
     * Read single byte register
     * @param Dev Device Handle
     * @param index The register index
     * @param data pointer to 8 bit data
     * @return VL53L0X_ERROR_NONE Success
     * @return "Other error code" See ::VL53L0X_Error
     */
    VL53L0X_Error VL53L0X_RdByte(VL53L0X_DEV Dev, uint8_t index, uint8_t *data);
     
    /**
     * Read word (2byte) register
     * @param Dev Device Handle
     * @param index The register index
     * @param data pointer to 16 bit data
     * @return VL53L0X_ERROR_NONE Success
     * @return "Other error code" See ::VL53L0X_Error
     */
    VL53L0X_Error VL53L0X_RdWord(VL53L0X_DEV Dev, uint8_t index, uint16_t *data);
     
    /**
     * Read dword (4byte) register
     * @param Dev Device Handle
     * @param index The register index
     * @param data pointer to 32 bit data
     * @return VL53L0X_ERROR_NONE Success
     * @return "Other error code" See ::VL53L0X_Error
     */
    VL53L0X_Error VL53L0X_RdDWord(VL53L0X_DEV Dev, uint8_t index, uint32_t *data);
     
    /**
     * Threat safe Update (read/modify/write) single byte register
     *
     * Final_reg = (Initial_reg & and_data) |or_data
     *
     * @param Dev Device Handle
     * @param index The register index
     * @param AndData 8 bit and data
     * @param OrData 8 bit or data
     * @return VL53L0X_ERROR_NONE Success
     * @return "Other error code" See ::VL53L0X_Error
     */
    VL53L0X_Error VL53L0X_UpdateByte(VL53L0X_DEV Dev, uint8_t index, uint8_t AndData, uint8_t OrData);
     
    /** @} end of VL53L0X_registerAccess_group */
     
     
    /**
     * @brief execute delay in all polling API call
     *
     * A typical multi-thread or RTOs implementation is to sleep the task for some 5ms (with 100Hz max rate faster polling is not needed)
     * if nothing specific is need you can define it as an empty/void macro
     * @code
     * #define VL53L0X_PollingDelay(...) (void)0
     * @endcode
     * @param Dev Device Handle
     * @return VL53L0X_ERROR_NONE Success
     * @return "Other error code" See ::VL53L0X_Error
     */
    VL53L0X_Error VL53L0X_PollingDelay(VL53L0X_DEV Dev); /* usually best implemented as a real function */
     
    /** @} end of VL53L0X_platform_group */
     
    #define VL53L0X_COPYSTRING(str, ...) strcpy(str, ##__VA_ARGS__)
     
    #ifdef __cplusplus
    }
    #endif
     
    #endif /* _VL53L0X_PLATFORM_H_ */
     
     
     

    Ben1Author
    Visitor II
    September 28, 2020

    Hello, thank you for your answer! Unfortunately, it is not an STM32 for the moment ;) thank you anyway for the code. There is STM32F401RE code for 53L3A2 expansion boards I may use.

    The API I have is taken from here

    If I understand well, I have to completely custom my platform.c and platform.h, by putting my chip related I2C functions within each functions in plateform.c, right? Thank you for your help!

    ST Employee
    September 29, 2020

    Exactly.

    and to help verify you got it right, consider including the following code.

    (Remove it once you get the test to pass.)

    1. #define I2C_TEST
    2. #ifdef I2C_TEST
    3. int rd_write_verification( VL53L1_Dev_t *dev, uint16_t addr, uint32_t expected_value)
    4. {
    5. VL53L1_Error Status  = VL53L1_ERROR_NONE;
    6. uint8_t bytes[4],mbytes[4];
    7. uint16_t words[2];
    8. uint32_t dword;
    9. int i;
    10. VL53L1_ReadMulti(dev, addr, mbytes, 4);
    11. for (i=0; i<4; i++){ VL53L1_RdByte(dev, addr+i, &bytes[i]); }
    12. for (i=0; i<2; i++){ VL53L1_RdWord(dev, addr+i*2, &words[i]); }
    13. Status = VL53L1_RdDWord(dev, addr, &dword);
    14. printf("expected   = %8x,\n",expected_value);
    15. printf("read_multi = %2x, %2x, %2x, %2x\n", mbytes[0],mbytes[1],mbytes[2],mbytes[3]);
    16. printf("read_bytes = %2x, %2x, %2x, %2x\n", bytes[0],bytes[1],bytes[2],bytes[3]);
    17. printf("read words = %4x, %4x\n",words[0],words[1]);
    18. printf("read dword = %8x\n",dword);
    19. if((mbytes[0]<<24 | mbytes[1]<<16 | mbytes[2]<<8 | mbytes[3]) != expected_value) return (-1);
    20. if((bytes[0]<<24 | bytes[1]<<16 | bytes[2]<<8 | bytes[3]) != expected_value) return (-1);
    21. if((words[0]<<16 | words[1]) != expected_value) return (-1);
    22. if(dword != expected_value) return(-1);
    23. return Status;
    24. }
    25. #define REG 0x3A
    26. void i2c_test(VL53L1_Dev_t *dev)
    27. {
    28. VL53L1_Error Status  = VL53L1_ERROR_NONE;
    29. int err_count = 0;
    30. uint8_t buff[4] = {0x11,0x22,0x33,0x44};
    31. uint8_t long_out[135] ={0x29, 0x02, 0x10, 0x00, 0x22, 0xBC, 0xCC, 0x81, 0x80, 0x07, 0x16, 0x00, 0xFF, 0xFD,
    32. 0xF7, 0xDE, 0xFF, 0x0F, 0x00, 0x15, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    33. 0x44, 0x00, 0x2C, 0x00, 0x11, 0x0A, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    34. 0x00, 0x11, 0x02, 0x00, 0x02, 0x08, 0x00, 0x08, 0x10, 0x01, 0x01, 0x00, 0x00, 0x00, 0x00, 0xFF,
    35. 0x00, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x20, 0x0B, 0x00, 0x00, 0x02, 0x14, 0x21, 0x00, 0x00,
    36. 0x02, 0x00, 0x00, 0x00, 0x00, 0xC8, 0x00, 0x00, 0x38, 0xFF, 0x01, 0x00, 0x01, 0x00, 0x02, 0x00,
    37. 0x9D, 0x07, 0x00, 0xD2, 0x05, 0x01, 0x68, 0x00, 0xC0, 0x08, 0x38, 0x00, 0x00, 0x00, 0x00, 0x03,
    38. 0xB6, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x07, 0x05, 0x06, 0x06, 0x01, 0x00, 0x02,
    39. 0xC7, 0xFF, 0x8B, 0x00, 0x00, 0x00, 0x01, 0x01, 0x40};
    40. uint8_t long_in[135]= {0xff};
    41.   int i=0;
    42.  
    43. Status = rd_write_verification(dev, 0x10f, 0xeacc10ff); // verify the Chip ID works
    44.  
    45. Status += VL53L1_WriteMulti(dev, 0x01,  long_out, 135); // check if WriteMulti can write 135 bytes
    46. Status += VL53L1_ReadMulti(dev, 0x01,  long_in, 135); // check if WriteMulti can read 135 bytes
    47.  
    48. for (i=0; i<135; i++) if(long_in[i] != long_out[i])err_count++;
    49. if (err_count > 10) Status++;
    50.  
    51. Status += VL53L1_WriteMulti(dev, REG,  buff, 4); // check WriteMulti
    52. if (rd_write_verification(dev, REG, 0x11223344) <0) err_count++;
    53.  
    54. Status += VL53L1_WrDWord(dev, REG, 0xffeeddcc); // check WrDWord
    55. if (rd_write_verification(dev, REG, 0xffeeddcc) <0) err_count++;
    56.  
    57. Status += VL53L1_WrWord(dev, REG, 0x5566); // check WrWord
    58. Status += VL53L1_WrWord(dev, REG+2, 0x7788);
    59. if (rd_write_verification(dev, REG, 0x55667788) <0) err_count++;
    60.  
    61. for (i=0; i<4; i++){ VL53L1_WrByte (dev, REG+i, buff[i]); }
    62. if (rd_write_verification(dev, REG,0x11223344) <0) Status++;
    63. if (Status > 0)
    64. {
    65. printf("i2c test failed - please check it. Status = %d\n", Status);
    66. }
    67. }
    68.  
    69. #endif

    It verifies the long I2C long I/O operations and the 'byte endianness' of the operations.

    Ben1Author
    Visitor II
    September 29, 2020

    Thank you for the snippet, it is great! I'll try it when my functions will be done

    ST Employee
    September 29, 2020

    ​Hi,

    Actually the stsw-img015  you downloaded is the VL53L3CX driver package including the driver itself and the example. To run the example you need IAR, KEIL or STM32CudeIDE (freeware downloadable from st.com ). Your understanding is correct, you have to write your platform.c where you will implement the I2C functions according to your hardware. The platform.c in the example code was written for the X-NUCLEO-53L3A2 and the NucleoF401RE.

    Ben1Author
    Visitor II
    October 21, 2020

    Hi everyone, I am back to this project after few times.

    I want to test with nRF microcontrollers and I am struggling to adapt platform.c with my I2C functions. Here is my main.c : (very simple)

    #include "hardware.h"
    #include "vl53lx_api.h"
    #include "bus_i2c_drv.h"
    #include "debug_uart_nrf52.h"
    #include "nrf_delay.h"
     
    VL53LX_Dev_t dev;
    VL53LX_DEV Dev = &dev;
    Dev->I2cDevAddr = 0x52;
    VL53LX_Error status=0;
    volatile int IntCount;
     
    int main(void)
    {
    	uint8_t byteData;
    	uint16_t wordData;
    	VL53LX_MultiRangingData_t MultiRangingData;
    	VL53LX_MultiRangingData_t *pMultiRangingData = &MultiRangingData;
    	uint8_t NewDataReady=0;
    	int no_of_object_found=0,j;
    	// Initialize.
    	hardware_IO_init();
    	debug_uart_init();
     	hardware_set_pin(PIN_PWR_SENS2); // VDD VL53L3
     	hardware_set_pin(PIN_PWR_SENS); // Pull-up i2c
    	bus_i2c_Init();
     
     // Enter main loop.
     for (;;)
     {
     	status = VL53LX_RdByte(Dev, 0x010F, &byteData);
     	nrf_delay_ms(1000);
     }
    }

    The i2c init returns 0 so it is fine, I have both SDA and SCL to VDD. Then I try to have the same data I have with the STM32F401RE with 53L3A2 board example (i.e 0xEA). VL53LX_RdByte() calls VL53LX_RdByte() which calls VL53LX_ReadMulti(). I edited this function as follow :

    /**
     * @brief Reads the requested number of bytes from the device
     *
     * @param[in] pdev : pointer to device structure (device handle)
     * @param[in] index : uint16_t register index value
     * @param[out] pdata : pointer to the uint8_t (byte) buffer to store read data
     * @param[in] count : number of bytes to read
     *
     * @return VL53LX_ERROR_NONE Success
     * @return "Other error code" See ::VL53LX_Error
     */
    VL53LX_Error VL53LX_ReadMulti(
    	VL53LX_Dev_t *pdev,
    	uint16_t index,
    	uint8_t *pdata,
    	uint32_t count)
    {
    	VL53LX_Error status = VL53LX_ERROR_NONE;
    	uint8_t *pBuffer = pdata;
    	uint8_t DevAddr = pdev->I2cDevAddr;
     
    	status = (int8_t)bus_i2c_ReadData(DevAddr, pdata, pdev->i2c_slave_address + index, count);
     
    	return status;
    }

    bus_i2c_ReadData() is the function I have to use :

    /**
     * @brief Reads Data from I2C communication bus.
     * @param[in] DevAddr: Device address.
     * @param[out] pBuffer: pointer to the buffer that receives the data read from the Device.
     * @param[in] ReadAddr: Device's internal address to start reading from.
     * @param[in] DataSize: number of bytes to read.
     * @retval ret_code_t: NRF_SUCCESS (0) if operation is correctly performed, else return value different from NRF_SUCCESS (0).
     */
    ret_code_t bus_i2c_ReadData(uint8_t DevAddr, uint8_t * pBuffer, uint8_t ReadAddr, size_t DataSize)
    {
    	ret_code_t ret;
    	uint8_t addr8 = (uint8_t)ReadAddr;
     
    	do
    	{
    		ret = nrf_drv_twi_tx(&I2Cinstance_drv, DevAddr, &addr8, 1, true);
    		if (NRF_SUCCESS != ret)
    			break;
    		ret = nrf_drv_twi_rx(&&I2Cinstance_drv, DevAddr, pBuffer, DataSize);
    		debug_uart_printf("ret = 0d%d 0x%x \n\r", ret, ret);
    	} while(0);
     
    	debug_uart_printf("ret = 0d%d 0x%x \n\r", ret, ret);
    	return ret;
    }

    nrf_twi_drv_tx() returns a NACK ( error code 0x8201). I saw in the documentation that it may occurs after the nRF sends the address and the slave doesn't respond, so I assume I have implementation issue concerning slave address. What exactly is the index parameter in VL53LX_ReadMulti()? How can I pass this parameter to bus_i2c_ReadData()? index is uint16_t while ReadAddr is uint8_t. Thank you very much for your help!

    Ben1AuthorAnswer
    Visitor II
    November 20, 2020

    UPDATE : the answer was dead simple : I forgot to shift the I2C adress like this :

    status = (int8_t)bus_i2c_ReadData(DevAddr>>1, pdata, pdev->i2c_slave_address + index, count);

    here is my final code for those who have the issue aswell :

    VL53LX_Error VL53LX_ReadMulti(VL53LX_Dev_t *pdev, uint16_t index, uint8_t *pdata, uint32_t count)
    {
    	VL53LX_Error status = VL53LX_ERROR_NONE;
    	uint8_t DevAddr = pdev->I2CDevAddr;
     int32_t status_int;
     uint8_t I2C_buff[2] = {0};
     
     if (count>=VL53LX_COMMS_BUFFER_SIZE)
     {
     status = VL53LX_ERROR_INVALID_PARAMS;
     }
     
     if (count > (VL53LX_MAX_STRING_LENGTH_PLT))
     {
     return NRF_ERROR_INVALID_LENGTH;
     }
     
     status_int = bus_i2c_ReadData(DevAddr>>1, pdata, index, count);
     
    	if (status_int != 0)
    		status = VL53LX_ERROR_CONTROL_INTERFACE;
     
    	return status;
    }

    ST Employee
    November 20, 2020

    well done.

    I2C addressing has always been an issue. Does one specify the write address 0x52 or the 7-bit address (0x29)?

    I always thought specifiing it as 0x52/0x53 would be best as it's clearly the Write/Read.

    But I'm guessing even with that some software wants the 7-bit address - as yours apparently does.

    This bus has been around forever and putting the Write/Read bit in the LSB has caused issues from the beginning.

    • john

    Ben1Author
    Visitor II
    November 21, 2020

    Hello John, you are right : the address is 7 bits but read from LSB which gives 0x29 in both reading and writing. Then my MCU low level functions add the R/W bit value according to the write or read operation, thank you !