Skip to content

Commit d5483b1

Browse files
committed
drivers: sensors: add ST VL53L1X TOF sensor
Adds Zephyr platform driver for VL51L1X TOF sensor, based on ST's driver API (see UM2356) Signed-off-by: Chris Schramm <[email protected]>
1 parent eddf058 commit d5483b1

File tree

15 files changed

+1477
-1
lines changed

15 files changed

+1477
-1
lines changed

drivers/sensor/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,5 +55,6 @@ add_subdirectory_ifdef(CONFIG_TH02 th02)
5555
add_subdirectory_ifdef(CONFIG_TMP007 tmp007)
5656
add_subdirectory_ifdef(CONFIG_TMP112 tmp112)
5757
add_subdirectory_ifdef(CONFIG_VL53L0X vl53l0x)
58+
add_subdirectory_ifdef(CONFIG_VL53L1X vl53l1x)
5859

5960
zephyr_sources_ifdef(CONFIG_USERSPACE sensor_handlers.c)

drivers/sensor/Kconfig

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,4 +135,6 @@ source "drivers/sensor/tmp112/Kconfig"
135135

136136
source "drivers/sensor/vl53l0x/Kconfig"
137137

138+
source "drivers/sensor/vl53l1x/Kconfig"
139+
138140
endif # SENSOR
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
# SPDX-License-Identifier: Apache-2.0
2+
3+
zephyr_library()
4+
5+
zephyr_library_sources(
6+
vl53l1.c
7+
vl53l1_platform.c
8+
)

drivers/sensor/vl53l1x/Kconfig

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
# Kconfig - VL53L1X time of flight sensor configuration options
2+
3+
#
4+
# Copyright (c) 2019 Makaio GmbH
5+
#
6+
# SPDX-License-Identifier: Apache-2.0
7+
#
8+
9+
menuconfig VL53L1X
10+
bool "VL53L1X time of flight sensor"
11+
depends on I2C && HAS_DTS_I2C
12+
select HAS_STLIB
13+
help
14+
Enable driver for VL53L1X I2C-based time of flight sensor.
15+
16+
if VL53L1X
17+
18+
config VL53L1X_XSHUT_CONTROL_ENABLE
19+
bool "Enable XSHUT pin control"
20+
help
21+
Enable it if XSHUT pin is controlled by host.
22+
23+
config VL53L1X_XSHUT_GPIO_DEV_NAME
24+
string "GPIO device"
25+
depends on VL53L1X_XSHUT_CONTROL_ENABLE
26+
help
27+
The device name of the GPIO device to which the VL53L1X xshut pin
28+
is connected.
29+
30+
config VL53L1X_XSHUT_GPIO_PIN_NUM
31+
int "Interrupt GPIO pin number"
32+
depends on VL53L1X_XSHUT_CONTROL_ENABLE
33+
help
34+
The number of the GPIO on which the xshut signal from the VL53L1X
35+
is connected.
36+
37+
config VL53L1X_PROXIMITY_THRESHOLD
38+
int "Proximity threshold in millimeters"
39+
default 100
40+
help
41+
Threshold used for proximity detection when sensor is used with SENSOR_CHAN_PROX.
42+
43+
config VL53L1X_MEAS_TIMING_BUDGET
44+
int "Default measurement timing budget"
45+
default 50
46+
help
47+
Timing budget for measurements in ms.
48+
Can be overridden in dts with measurement-timing-budget.
49+
50+
config VL53L1X_INTERMEASUREMENT_PERIOD
51+
int "Inter measurement period in ms"
52+
default 500
53+
help
54+
Time between measurements in ms. Must be > timing budget + 4ms.
55+
Can be overridden in dts with inter-measurement-period.
56+
57+
endif # VL53L1X

drivers/sensor/vl53l1x/vl53l1.c

Lines changed: 198 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,198 @@
1+
/* vl53l1.c - Driver for ST VL53L1X time of flight sensor */
2+
/*
3+
* Copyright (c) 2017 STMicroelectronics
4+
* Copyright (c) 2019 Makaio GmbH
5+
*
6+
* SPDX-License-Identifier: Apache-2.0
7+
*/
8+
9+
#include <errno.h>
10+
11+
#include <kernel.h>
12+
#include <drivers/i2c.h>
13+
#include <drivers/sensor.h>
14+
#include <init.h>
15+
#include <drivers/gpio.h>
16+
#include <sys/__assert.h>
17+
#include <zephyr/types.h>
18+
#include <device.h>
19+
#include <logging/log.h>
20+
21+
#include "vl53l1_api.h"
22+
#include "vl53l1_platform.h"
23+
24+
25+
26+
#define LOG_LEVEL CONFIG_SENSOR_LOG_LEVEL
27+
LOG_MODULE_REGISTER(VL53L1X);
28+
29+
struct vl53l1x_pin_config {
30+
bool use;
31+
u32_t gpios_pin;
32+
char *gpios_ctrl;
33+
};
34+
35+
struct vl53l1x_data {
36+
char *i2c_dev_name;
37+
u16_t i2c_addr;
38+
VL53L1_Dev_t vl53l1x;
39+
VL53L1_RangingMeasurementData_t RangingMeasurementData;
40+
int inter_measurement_period;
41+
int timing_budget;
42+
struct vl53l1x_pin_config irq_config;
43+
struct vl53l1x_pin_config xshut_config;
44+
u8_t instance_id;
45+
};
46+
47+
static void vl53l1x_print_device_settings(struct device *dev)
48+
{
49+
struct vl53l1x_data *drv_data = dev->driver_data;
50+
51+
struct vl53l1x_pin_config *irq_cfg = &drv_data->irq_config;
52+
53+
LOG_DBG("Name:\t\t%s", log_strdup(dev->config->name));
54+
LOG_DBG("Instance ID:\t%u", drv_data->instance_id);
55+
LOG_DBG("I2C Address:\t0x%02x", drv_data->i2c_addr);
56+
57+
if (irq_cfg && irq_cfg->use) {
58+
LOG_DBG("IRQ:\t\tenabled, ctrl %s, pin %u",
59+
irq_cfg->gpios_ctrl, irq_cfg->gpios_pin);
60+
} else {
61+
LOG_DBG("IRQ:\t\tdisabled");
62+
}
63+
64+
struct vl53l1x_pin_config *xshut_cfg = &drv_data->xshut_config;
65+
66+
if (xshut_cfg && xshut_cfg->use) {
67+
LOG_DBG("XSHUT:\t\tenabled, ctrl %s, pin %u",
68+
xshut_cfg->gpios_ctrl, xshut_cfg->gpios_pin);
69+
} else {
70+
LOG_DBG("XSHUT:\t\tdisabled");
71+
}
72+
73+
74+
}
75+
76+
static int vl53l1x_init(struct device *dev)
77+
{
78+
struct vl53l1x_data *drv_data = dev->driver_data;
79+
VL53L1_Error ret;
80+
u16_t vl53l1x_id = 0U;
81+
VL53L1_DeviceInfo_t vl53l1x_dev_info;
82+
83+
LOG_DBG("enter in %s", __func__);
84+
85+
vl53l1x_print_device_settings(dev);
86+
87+
VL53L1_Dev_t vl53l1dev = {
88+
.I2cDevAddr = drv_data->i2c_addr,
89+
.I2cHandle = device_get_binding(drv_data->i2c_dev_name)
90+
};
91+
drv_data->vl53l1x = vl53l1dev;
92+
93+
if (vl53l1dev.I2cHandle == NULL) {
94+
LOG_ERR("Could not get pointer to %s device.",
95+
drv_data->i2c_dev_name);
96+
return -EINVAL;
97+
}
98+
99+
ret = VL53L1_WaitDeviceBooted(&drv_data->vl53l1x);
100+
101+
if (ret) {
102+
return -ETIMEDOUT;
103+
}
104+
105+
LOG_DBG("VL53L1_WaitDeviceBooted succeeded");
106+
107+
/* sensor init */
108+
ret = VL53L1_DataInit(&drv_data->vl53l1x);
109+
if (ret < 0) {
110+
LOG_ERR("VL53L1X_DataInit return error (%d)", ret);
111+
return -ENOTSUP;
112+
}
113+
114+
LOG_DBG("VL53L1_DataInit succeeded");
115+
116+
/* static init */
117+
ret = VL53L1_StaticInit(&drv_data->vl53l1x);
118+
if (ret < 0) {
119+
LOG_ERR("VL53L1_StaticInit return error (%d)", ret);
120+
return -ENOTSUP;
121+
}
122+
123+
LOG_DBG("VL53L1_StaticInit succeeded");
124+
125+
ret = VL53L1_SetDistanceMode(&drv_data->vl53l1x,
126+
VL53L1_DISTANCEMODE_LONG);
127+
if (ret < 0) {
128+
LOG_ERR("VL53L1_SetDistanceMode return error (%d)", ret);
129+
return -ENOTSUP;
130+
}
131+
132+
LOG_DBG("VL53L1_SetDistanceMode succeeded");
133+
134+
ret = VL53L1_StartMeasurement(&drv_data->vl53l1x);
135+
136+
return 0;
137+
}
138+
139+
#define VL53L1X_PIN_CFG(id, pintype) \
140+
static const struct vl53l1x_pin_config\
141+
vl53l1x_##pintype##_##id##_cfg = { \
142+
.use = true, \
143+
.gpios_pin = \
144+
DT_INST_##id##_ST_VL53L1X_##pintype##_GPIOS_PIN, \
145+
.gpios_ctrl = \
146+
DT_INST_##id##_ST_VL53L1X_##pintype##_GPIOS_CONTROLLER \
147+
}
148+
149+
#define VL53L1X_NOPIN_CFG(id, pintype) \
150+
static const struct vl53l1x_pin_config \
151+
vl53l1x_##pintype##_##id##_cfg = { \
152+
/* this is unnecessary, but let's keep it for clarity */ \
153+
.use = false, \
154+
}
155+
156+
157+
158+
159+
#define VL53L1X_INST_INIT(id) \
160+
static struct vl53l1x_data vl53l1x_driver_##id##_data = { \
161+
.instance_id = id, \
162+
.i2c_dev_name = DT_INST_##id##_ST_VL53L1X_BUS_NAME, \
163+
.i2c_addr = DT_INST_##id##_ST_VL53L1X_BASE_ADDRESS, \
164+
.irq_config = vl53l1x_IRQ_##id##_cfg, \
165+
.xshut_config = vl53l1x_XSHUT_##id##_cfg, \
166+
.inter_measurement_period = \
167+
DT_INST_##id##_ST_VL53L1X_INTER_MEASUREMENT_PERIOD, \
168+
.timing_budget = \
169+
DT_INST_##id##_ST_VL53L1X_MEASUREMENT_TIMING_BUDGET \
170+
}; \
171+
DEVICE_AND_API_INIT(vl53l1x, \
172+
DT_INST_##id##_ST_VL53L1X_LABEL, \
173+
vl53l1x_init, \
174+
&vl53l1x_driver_##id##_data, \
175+
NULL, POST_KERNEL, CONFIG_SENSOR_INIT_PRIORITY, NULL)
176+
177+
#ifdef DT_INST_0_ST_VL53L1X_BASE_ADDRESS
178+
#ifndef DT_INST_0_ST_VL53L1X_INTER_MEASUREMENT_PERIOD
179+
#define DT_INST_0_ST_VL53L1X_INTER_MEASUREMENT_PERIOD
180+
CONFIG_VL53L1X_INTERMEASUREMENT_PERIOD
181+
#endif
182+
#ifndef DT_INST_0_ST_VL53L1X_MEASUREMENT_TIMING_BUDGET
183+
#define DT_INST_0_ST_VL53L1X_MEASUREMENT_TIMING_BUDGET
184+
CONFIG_VL53L1X_MEAS_TIMING_BUDGET
185+
#endif
186+
#ifdef DT_INST_0_ST_VL53L1X_IRQ_GPIOS_PIN
187+
VL53L1X_PIN_CFG(0, IRQ);
188+
#else
189+
VL53L1X_NOPIN_CFG(0, IRQ);
190+
#endif
191+
#ifdef DT_INST_0_ST_VL53L1X_XSHUT_GPIOS_PIN
192+
VL53L1X_PIN_CFG(0, XSHUT);
193+
#else
194+
VL53L1X_NOPIN_CFG(0, XSHUT);
195+
#endif
196+
VL53L1X_INST_INIT(0);
197+
#endif
198+

0 commit comments

Comments
 (0)