Skip to content

Commit

Permalink
drivers: sensor: vl53l0x: Add private channels
Browse files Browse the repository at this point in the history
Added private channels for metadata associated with
distance measurement according to datasheet and API manual.

Signed-off-by: Michal Piekos <[email protected]>
  • Loading branch information
michalpiekos committed Dec 26, 2024
1 parent a4006e4 commit 4c766d8
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 0 deletions.
24 changes: 24 additions & 0 deletions drivers/sensor/st/vl53l0x/vl53l0x.c
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,16 @@
#include <zephyr/types.h>
#include <zephyr/device.h>
#include <zephyr/logging/log.h>
#include <zephyr/drivers/sensor/vl53l0x.h>

#include "vl53l0x_api.h"
#include "vl53l0x_platform.h"

LOG_MODULE_REGISTER(VL53L0X, CONFIG_SENSOR_LOG_LEVEL);

#define VL53L0X_FIXPOINT1616_SCALE_FACTOR (65536)
#define VL53L0X_SENSOR_CHANNEL_VAL2_FACTOR (1000000)

/* All the values used in this driver are coming from ST datasheet and examples.
* It can be found here:
* https://www.st.com/en/embedded-software/stsw-img005.html
Expand Down Expand Up @@ -288,6 +292,26 @@ static int vl53l0x_channel_get(const struct device *dev,
} else if (chan == SENSOR_CHAN_DISTANCE) {
val->val1 = drv_data->RangingMeasurementData.RangeMilliMeter / 1000;
val->val2 = (drv_data->RangingMeasurementData.RangeMilliMeter % 1000) * 1000;
} else if ((enum sensor_channel_vl53l0x)chan ==
SENSOR_CHAN_VL53L0X_EFFECTIVE_SPAD_RTN_COUNT) {
val->val1 = drv_data->RangingMeasurementData.EffectiveSpadRtnCount / 256;
val->val2 = 0;
} else if ((enum sensor_channel_vl53l0x)chan == SENSOR_CHAN_VL53L0X_AMBIENT_RATE_RTN_CPS) {
val->val1 = (drv_data->RangingMeasurementData.AmbientRateRtnMegaCps >> 16) +
(((drv_data->RangingMeasurementData.AmbientRateRtnMegaCps & 0xFFFF) *
VL53L0X_SENSOR_CHANNEL_VAL2_FACTOR) / VL53L0X_FIXPOINT1616_SCALE_FACTOR);
val->val2 = 0;
} else if ((enum sensor_channel_vl53l0x)chan == SENSOR_CHAN_VL53L0X_SIGNAL_RATE_RTN_CPS) {
val->val1 = (drv_data->RangingMeasurementData.SignalRateRtnMegaCps >> 16) +
(((drv_data->RangingMeasurementData.SignalRateRtnMegaCps & 0xFFFF) *
VL53L0X_SENSOR_CHANNEL_VAL2_FACTOR) / VL53L0X_FIXPOINT1616_SCALE_FACTOR);
val->val2 = 0;
} else if ((enum sensor_channel_vl53l0x)chan == SENSOR_CHAN_VL53L0X_RANGE_DMAX) {
val->val1 = drv_data->RangingMeasurementData.RangeDMaxMilliMeter / 1000;
val->val2 = (drv_data->RangingMeasurementData.RangeDMaxMilliMeter % 1000) * 1000;
} else if ((enum sensor_channel_vl53l0x)chan == SENSOR_CHAN_VL53L0X_RANGE_STATUS) {
val->val1 = drv_data->RangingMeasurementData.RangeStatus;
val->val2 = 0;
} else {
return -ENOTSUP;
}
Expand Down
47 changes: 47 additions & 0 deletions include/zephyr/drivers/sensor/vl53l0x.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/*
* Copyright (c) 2024 Michal Piekos
* SPDX-License-Identifier: Apache-2.0
*/

/**
* @file
* @brief Custom channels and values for VL53L0X ToF Sensor
*
* These channels provide additional sensor data not covered by the standard
* Zephyr sensor channels. Application must include vl53l0x.h file to gain
* access to these channels.
*
* Example usage:
* @code{c}
* #include <zephyr/drivers/sensor/vl53l0x.h>
*
* if (sensor_channel_get(dev, SENSOR_CHAN_VL53L0X_RANGE_STATUS, &value)) {
* printk("Status: %d\n", value.val1);
* }
* @endcode
*/

#ifndef ZEPHYR_INCLUDE_DRIVERS_SENSOR_VL53L0X_H_
#define ZEPHYR_INCLUDE_DRIVERS_SENSOR_VL53L0X_H_

#include <zephyr/drivers/sensor.h>

/* VL53L0x specific channels */
enum sensor_channel_vl53l0x {
SENSOR_CHAN_VL53L0X_RANGE_DMAX = SENSOR_CHAN_PRIV_START,
SENSOR_CHAN_VL53L0X_SIGNAL_RATE_RTN_CPS,
SENSOR_CHAN_VL53L0X_AMBIENT_RATE_RTN_CPS,
SENSOR_CHAN_VL53L0X_EFFECTIVE_SPAD_RTN_COUNT,
SENSOR_CHAN_VL53L0X_RANGE_STATUS,
};

/* VL53L0x meas status values */
#define VL53L0X_RANGE_STATUS_RANGE_VALID (0)
#define VL53L0X_RANGE_STATUS_SIGMA_FAIL (1)
#define VL53L0X_RANGE_STATUS_SIGNAL_FAIL (2)
#define VL53L0X_RANGE_STATUS_MIN_RANGE_FAIL (3)
#define VL53L0X_RANGE_STATUS_PHASE_FAIL (4)
#define VL53L0X_RANGE_STATUS_HARDWARE_FAIL (5)
#define VL53L0X_RANGE_STATUS_NO_UPDATE (255)

#endif /* ZEPHYR_INCLUDE_DRIVERS_SENSOR_VL53L0X_H_ */

0 comments on commit 4c766d8

Please sign in to comment.