Skip to content

Conversation

gschorcht
Copy link
Contributor

@gschorcht gschorcht commented Nov 10, 2018

Contribution description

This PR was originally intended only as an example for the discussion on how to add a device driver as a package. In the meantime I finished the driver. It is fully functional and therefore ready for review.

Overview

This PR adds a device driver for ST VL53L1X Time-of-Flight laser-ranging sensor. The VL53L1X allows accurate and fast ranging up to 4 m and with a frequency of up to 50 Hz.

There are three variants of the driver which differ in functionality and size:

Module Driver Short Description
vl53l1x Standard driver Common functionality and medium size
vl53l1x_st_api ST VL53L1X API driver Complete functionality and large size
vl53l1x_basic Basic driver Minimum functionality and small size

This vl53l1x_st_api variant of the driver uses the ST STSW-IMG007 VL53L1X API as a package (pkg/driver_vl53l1x_st_api). In this case, the driver is simply a wrapper for the API which provides a driver interface that is compatible with the other driver variants. Since the ST STSW-IMG007 VL53L1X API package is quite complex and very large, this driver variant can only be used when memory requirements are not an issue. It has to be used for best accuracy, for calibration and if threshold interrupts shall be used.

The vl53l1x_std driver variant (default) is a compromise of size and functionality. It provides the application with most functionality of the sensor. The driver can be used when memory requirements are important and most of the functionality should be used.

The vl53l1x_basic driver variant is the smallest driver variant which only provides some basic functions such as the distance measurement and the data-ready interrupt.

All driver variants provide SAUL capabilities and data-ready interrupt functionality. However, only the vl53l1x_st_api driver variant allows to use threshold interrupts.

Function / Property vl53l1x_basic vl53l1x_std (default) vl53l1x_st_api
Distance results in mm X X X
Signal rate results in MCPS X X
Measurement status information X X
SAUL capability X X X
Distance mode configuration X X
Timing budget configuration X X
Inter-measurement period configuration X X
Region of Interest (ROI) configuration X X
Data-ready interrupts X X X
Threshold interrupts X
Calibration functions X [1]
Limit check configuration X [1]
Accuracy medium high high
Size on reference platform in kByte [2] 1.0 2.9 19.8

[1] These functions are available by using the ST VL53L1X API directly.
[2] Reference platform: STM32F411RE

Testing procedure

The driver variants can be tested by using the test application with different modules where the standard variant vl53l1x_std is used by default:

make flash -C tests/driver_vl53l1x BOARD=...

To use the other driver variants, module vl53l1x_st_api or module vl53l1x_basic
have to be specified at make command line

USEMODULE=vl53l1x_st_api make flash -C tests/driver_vl53l1x BOARD=...

or

USEMODULE=vl53l1x_basic make flash -C tests/driver_vl53l1x BOARD=... 

If the configuration parameter VL53L1X_PARAM_PIN_INT is defined, e.g.,

CFLAGS='-DVL53L1X_PARAM_PIN_INT=\(GPIO\(0,\1\)' make flash -C tests/driver_vl53l1x BOARD=...

interrupts are used to get data instead of polling for new data. In the case of driver variant vl53l1x_st_api, threshold interrupts are configured.

In all cases, the sensor is configured with following default parameters:

  • timing budget of 50 ms
  • intermeasurement period of 100 ms

Issues/PRs references

Some weeks ago @dylad was providing a sensor driver for the Bosch BME680 gas sensor, see PR #9909. Since the calculations are quite complex, Bosch provides a ready-to-use API. Therefore, he and @aabadie decided that it would be better to add Bosch's BME680 API driver as package.

Since the ST VL53L1X is even more complex, I decided to try it in the same way. This PR is a try to structure a driver as a package.

See also PR #9909

@gschorcht gschorcht added the Discussion: RFC The issue/PR is used as a discussion starting point about the item of the issue/PR label Nov 10, 2018
@gschorcht gschorcht force-pushed the drivers_vl53l1x_st branch 2 times, most recently from 4afebb6 to f54431c Compare November 12, 2018 12:12
@gschorcht gschorcht added State: WIP State: The PR is still work-in-progress and its code is not in its final presentable form yet Area: pkg Area: External package ports Area: drivers Area: Device drivers labels Nov 12, 2018
@gschorcht gschorcht added Type: new feature The issue requests / The PR implemements a new feature for RIOT and removed Discussion: RFC The issue/PR is used as a discussion starting point about the item of the issue/PR State: WIP State: The PR is still work-in-progress and its code is not in its final presentable form yet labels Nov 15, 2018
@gschorcht gschorcht changed the title drivers/vl53l1x: VL53L1X ToF rangins sensor added as package drivers/vl53l1x: VL53L1X ToF ranging sensor added as package Nov 17, 2018
@gschorcht gschorcht added the Area: SAUL Area: Sensor/Actuator Uber Layer label Nov 18, 2018
@stale
Copy link

stale bot commented Aug 10, 2019

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. If you want me to ignore this issue, please mark it with the "State: don't stale" label. Thank you for your contributions.

@stale stale bot added the State: stale State: The issue / PR has no activity for >185 days label Aug 10, 2019
@gschorcht
Copy link
Contributor Author

It's waiting for review.

@stale stale bot removed the State: stale State: The issue / PR has no activity for >185 days label Aug 10, 2019
@stale
Copy link

stale bot commented Feb 11, 2020

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. If you want me to ignore this issue, please mark it with the "State: don't stale" label. Thank you for your contributions.

@stale stale bot added the State: stale State: The issue / PR has no activity for >185 days label Feb 11, 2020
@gschorcht gschorcht removed the State: stale State: The issue / PR has no activity for >185 days label Feb 11, 2020
@miri64 miri64 requested a review from benpicco June 24, 2020 13:57
@javierfileiv
Copy link
Contributor

Hi! This is a great PR! Thanks @gschorcht ! I was working on a native VL53L0X, but it was turning into a burden with all this i2c settings that come out of nowhere. I'ḿ not sure if your porting is compatible with my breakbord. Otherwise I can port it in a similar way as you did. Thanks!

@maribu
Copy link
Member

maribu commented Jan 15, 2021

I do have some VL53L0X sensors lying around. I assume those are incompatible, is this right?

Update: I ordered two VL53L1X now. So I should be able to review and test this.

Copy link
Member

@maribu maribu left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I haven't seen all files yet, but some comments inline. I can resume review when I have the hardware for testing.

@@ -0,0 +1,11881 @@
/*
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would it be easily possible to provide this header via a pkg, similar to the way it is done with the vendor code for STM? But if that is non-trivial to do, I'm very much fine with having a copy in our repo.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This file is part of the ST VL53L1X driver package. Maybe we could pull the package even if it is not used.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I support this idea, that would instantly reduce the Lines of Code from 14k to 3k.

Comment on lines 181 to 189
#if MODULE_VL53L1X_ST_API
VL53L1X_DIST_SHORT = VL53L1_DISTANCEMODE_SHORT, /**< up to 1.3 m */
VL53L1X_DIST_MEDIUM = VL53L1_DISTANCEMODE_MEDIUM, /**< up to 2 m */
VL53L1X_DIST_LONG = VL53L1_DISTANCEMODE_LONG, /**< up to 4 m */
#else
VL53L1X_DIST_SHORT, /**< up to 1.3 m */
VL53L1X_DIST_MEDIUM, /**< up to 2 m */
VL53L1X_DIST_LONG, /**< up to 4 m */
#endif
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would the be a downside to just unconditionally using the constants which are compatible with the ST API in any case?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These symbols are defined in pkg/driver_vl53l1x_st_api/vl53l1x-st-api/vl53l1_def.h. They are available only if the ST VL53L1X driver is used by enabling the vl53l1x_st_api module.

*/
int vl53l1x_read_mm(vl53l1x_t *dev, int16_t *mm);

#if !MODULE_VL53L1X_BASIC
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
#if !MODULE_VL53L1X_BASIC
#if !MODULE_VL53L1X_BASIC || DOXYGEN

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It shouldn't be necessary because MODULE_VL53L1X_BASIC isn't defined when doxygen is executed so that the condition is true.

@gschorcht
Copy link
Contributor Author

@maribu Ups, I have to appologize, my plan was to improve the code a bit before because the code is quite old and contains a lot of code style violations 😟 Anyway, thanks for starting the review.

@gschorcht
Copy link
Contributor Author

@maribu I would have to touch the code anyway because of the conflicts and the changes in autoinitialization.

@gschorcht gschorcht force-pushed the drivers_vl53l1x_st branch from d52ab8f to f96a505 Compare August 3, 2025 12:39
Copy link
Contributor

@crasbe crasbe left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess this is still a bit WIP at the moment, but here are some comments. There are still some old Copyright notices, I saw that you are/were in the progress of migrating them to the new format.

* @warning The ST STSW-IMG007 VL53L1X API package functions use a significant
* amount of memory in the stack. If you have crashes due to memory
* access errors, try increasing the default thread stack size
* \ref THREAD_STACKSIZE_DEFAULT.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe you can give an estimation on how big the stack size should be.

Comment on lines +139 to +157
#define VL53L1X_RANGESTATUS_RANGE_VALID 0 /**< range is valid */
#define VL53L1X_RANGESTATUS_SIGMA_FAIL 1 /**< sigma failure */
#define VL53L1X_RANGESTATUS_SIGNAL_FAIL 2 /**< signal failure */
#define VL53L1X_RANGESTATUS_RANGE_VALID_MIN_RANGE_CLIPPED 3 /**< target is below minimum
detection threshold */
#define VL53L1X_RANGESTATUS_OUTOFBOUNDS_FAIL 4 /**< phase out of valid limits,
different to a wrap exit. */
#define VL53L1X_RANGESTATUS_HARDWARE_FAIL 5 /**< hardware failure */
#define VL53L1X_RANGESTATUS_RANGE_VALID_NO_WRAP_CHECK_FAIL 6 /**< range is valid but the wrap
around check has not been
done */
#define VL53L1X_RANGESTATUS_WRAP_TARGET_FAIL 7 /**< wrapped target, no matching
phase in other VCSEL period
timing */
#define VL53L1X_RANGESTATUS_XTALK_SIGNAL_FAIL 9 /**< specific to lite ranging */
#define VL53L1X_RANGESTATUS_SYNCRONISATION_INT 10 /**< first interrupt when starting
ranging in back to back mode */
#define VL53L1X_RANGESTATUS_MIN_RANGE_FAIL 13 /**< user ROI input is not valid */
#define VL53L1X_RANGESTATUS_NONE 255 /**< No Update. */
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can also put the documentation in the previous line, that would save some line breaks I guess.

* @brief Interrupt mode
*
* It defines when an interrupt is triggered. Interrupts can be triggered
* either on new data in each measurement cycly or when the threshold condition
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
* either on new data in each measurement cycly or when the threshold condition
* either on new data in each measurement cycle or when the threshold condition

Comment on lines +281 to +283
uint32_t period; /**< Inter-measurement period in ms, has to be
vl53l1x_params_t::t_budget + 4ms,
default: 100 ms */
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But the budget is 50ms, and 50+4ms != 100ms 🤔

* - Since reading any data resets the interrupt as well as the data-ready
* flag, either function #vl53l1x_read_mm, function #vl53l1x_read_data, or
* function #vl53l1x_read_details can be used in one measurement cycle.
* - This function is ONLY available when module \ref vl53l1x_basic is used.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
* - This function is ONLY available when module \ref vl53l1x_basic is used.
* - This function is ONLY available when module \ref vl53l1x_st_api is used.

However this part of the note is redundant, because in the normal description block, the same information is already given. So you can also remove the sentence I guess.

#include "vl53l1x.h"
#include "vl53l1x_params.h"

static void isr (void *arg)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
static void isr (void *arg)
static void isr(void *arg)

#include "vl53l1x.h"
#include "vl53l1x_params.h"

static void isr (void *arg)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
static void isr (void *arg)
static void isr(void *arg)

Comment on lines +12 to +42
* The test application demonstrates the usage of different functions
* dependent on the used driver variant:
*
* - vl53l1x Standard driver with most functionality
* - vl53l1x_basic Basic driver with only very basic functionality
* - vl53l1x_st_api ST VL53L1X API driver with complete functionality
*
* The driver variant used is defined by my variable DRIVER, which is set to
* vl53l1x by default. In this case, it is not necessary to specify the DRIVER
* variable in the make command:
*
* make flash -C tests/driver_vl53l1x BOARD=...
*
* To use other driver variants, module vl53l1x_st_api or module vl53l1x_basic
* have to be specified at make command line
*
* DRIVER=vl53l1x_st_api make flash -C tests/driver_vl53l1x BOARD=...
*
* or
*
* DRIVER=vl53l1x_basic make flash -C tests/driver_vl53l1x BOARD=...
*
* If the configuration parameter VL53L1X_PARAM_PIN_INT is defined, interrupts
* are used to get data instead of polling for new data. In the case of driver
* variant vl53l1x_st_api, threshold interrupts are configured. Otherwise
* only data interrupts are used.
*
* In all cases, the sensor is configured with following default parameters:
*
* - timing budget of 50 ms
* - intermeasurement period of 100 ms
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure if it makes sense to replicate the content of the README.md here?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this is also the case for other .c files.


## Usage

The driver variant used is defined by my variable `DRIVER`, which is set to
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

IMO the "defined by my variable" sounds a bit weird.

@@ -0,0 +1,33 @@
/*
* SPDX-FileCopyrightText: 20215 Gunar Schorcht
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
* SPDX-FileCopyrightText: 20215 Gunar Schorcht
* SPDX-FileCopyrightText: 2025 Gunar Schorcht

@gschorcht gschorcht added the State: WIP State: The PR is still work-in-progress and its code is not in its final presentable form yet label Aug 3, 2025
@gschorcht
Copy link
Contributor Author

I guess this is still a bit WIP at the moment, but here are some comments. There are still some old Copyright notices, I saw that you are/were in the progress of migrating them to the new format.

Oh sorry, I forgot to set the WIP label. I picked up this PR after it was not touched for 4 years and just started with rebasing it. Now, I'm going through step by step to see what have to be done do update this PR. It#s definitly too early to review it for the moment.

@gschorcht
Copy link
Contributor Author

Closed because of a complete rework in PR #21648.

@gschorcht gschorcht closed this Aug 7, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Area: build system Area: Build system Area: doc Area: Documentation Area: drivers Area: Device drivers Area: Kconfig Area: Kconfig integration Area: pkg Area: External package ports Area: SAUL Area: Sensor/Actuator Uber Layer Area: tests Area: tests and testing framework CI: ready for build If set, CI server will compile all applications for all available boards for the labeled PR State: WIP State: The PR is still work-in-progress and its code is not in its final presentable form yet Type: new feature The issue requests / The PR implemements a new feature for RIOT
Projects
None yet
Development

Successfully merging this pull request may close these issues.

7 participants