Skip to content

Commit 0cb9b44

Browse files
committed
drivers: sensor: Add vl53l1x time of flight sensor driver
Contains vl53l1x driver itself and the adaptation layer, which is implemented in the driver as it is Zephyr specific. Support its own thread (minimum latency) or share a system-wide thread (less memory) to fetch data. Also, need to clear interrupt flag after every measurement on vl53l1x. Add dts yaml file as dts/bindings/sensor/st,vl53l1x.yaml Signed-off-by: Aaron Tsui <[email protected]>
1 parent 30621bc commit 0cb9b44

16 files changed

+1385
-0
lines changed

drivers/sensor/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,5 +50,6 @@ add_subdirectory_ifdef(CONFIG_TH02 th02)
5050
add_subdirectory_ifdef(CONFIG_TMP007 tmp007)
5151
add_subdirectory_ifdef(CONFIG_TMP112 tmp112)
5252
add_subdirectory_ifdef(CONFIG_VL53L0X vl53l0x)
53+
add_subdirectory_ifdef(CONFIG_VL53L1X vl53l1x)
5354

5455
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
@@ -125,4 +125,6 @@ source "drivers/sensor/tmp112/Kconfig"
125125

126126
source "drivers/sensor/vl53l0x/Kconfig"
127127

128+
source "drivers/sensor/vl53l1x/Kconfig"
129+
128130
endif # SENSOR
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
# SPDX-License-Identifier: Apache-2.0
2+
3+
zephyr_library()
4+
5+
zephyr_library_sources(
6+
vl53l1x.c
7+
vl53l1_platform.c
8+
)
9+
10+
zephyr_library_sources_ifdef(CONFIG_VL53L1X_TRIGGER vl53l1x_trigger.c)

drivers/sensor/vl53l1x/Kconfig

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
#
2+
# Copyright (c) 2019 Aaron Tsui <[email protected]>
3+
#
4+
# SPDX-License-Identifier: Apache-2.0
5+
#
6+
7+
menuconfig VL53L1X
8+
bool "VL53L1X time of flight sensor"
9+
depends on I2C && HAS_DTS_I2C
10+
select HAS_STLIB
11+
help
12+
Enable driver for VL53L1X I2C-based time of flight sensor.
13+
14+
if VL53L1X
15+
16+
choice VL53L1X_TRIGGER_MODE
17+
prompt "Trigger mode"
18+
default VL53L1X_TRIGGER_NONE
19+
help
20+
Specify the type of triggering to be used by the driver.
21+
22+
config VL53L1X_TRIGGER_NONE
23+
bool "No trigger"
24+
25+
config VL53L1X_TRIGGER_GLOBAL_THREAD
26+
bool "Use global thread"
27+
depends on GPIO
28+
select VL53L1X_TRIGGER
29+
30+
config VL53L1X_TRIGGER_OWN_THREAD
31+
bool "Use own thread"
32+
depends on GPIO
33+
select VL53L1X_TRIGGER
34+
35+
endchoice # VL53L1X_TRIGGER_MODE
36+
37+
config VL53L1X_TRIGGER
38+
bool
39+
40+
config VL53L1X_THREAD_PRIORITY
41+
int "Thread priority"
42+
depends on VL53L1X_TRIGGER_OWN_THREAD
43+
default 10
44+
help
45+
Priority of thread used by the driver to handle interrupts.
46+
47+
config VL53L1X_THREAD_STACK_SIZE
48+
int "Thread stack size"
49+
depends on VL53L1X_TRIGGER_OWN_THREAD
50+
default 1024
51+
help
52+
Stack size of thread used by the driver to handle interrupts.
53+
54+
config VL53L1X_XSHUT_CONTROL_ENABLE
55+
bool "Enable XSHUT pin control"
56+
help
57+
Enable it if XSHUT pin is controlled by host.
58+
59+
config VL53L1X_XSHUT_GPIO_DEV_NAME
60+
string "GPIO device"
61+
default "GPIOC"
62+
depends on VL53L1X_XSHUT_CONTROL_ENABLE
63+
help
64+
The device name of the GPIO device to which the VL53L1X xshut pin
65+
is connected.
66+
67+
config VL53L1X_XSHUT_GPIO_PIN_NUM
68+
int "Interrupt GPIO pin number"
69+
default 6
70+
depends on VL53L1X_XSHUT_CONTROL_ENABLE
71+
help
72+
The number of the GPIO on which the xshut signal from the VL53L1X
73+
is connected.
74+
75+
config VL53L1X_PROXIMITY_THRESHOLD
76+
int "Proximity threshold in millimeters"
77+
default 100
78+
help
79+
Threshold used for proximity detection when sensor is used with SENSOR_CHAN_PROX.
80+
81+
endif # VL53L1X
Lines changed: 242 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,242 @@
1+
/*
2+
* Copyright (c) 2019 Aaron Tsui <[email protected]>
3+
*
4+
* SPDX-License-Identifier: Apache-2.0
5+
*/
6+
7+
8+
#include "vl53l1_platform.h"
9+
10+
#include <sensor.h>
11+
#include <kernel.h>
12+
#include <device.h>
13+
#include <init.h>
14+
#include <i2c.h>
15+
#include <logging/log.h>
16+
17+
#define LOG_LEVEL CONFIG_SENSOR_LOG_LEVEL
18+
LOG_MODULE_DECLARE(VL53L1X);
19+
20+
VL53L1_Error VL53L1_WriteMulti(VL53L1_DEV Dev, uint16_t index, uint8_t *pdata,
21+
uint32_t count)
22+
{
23+
VL53L1_Error Status = VL53L1_ERROR_NONE;
24+
int32_t status_int = 0;
25+
uint8_t buf[count + 2];
26+
27+
buf[0] = index >> 8;
28+
buf[1] = index & 0xFF;
29+
memcpy(&buf[2], pdata, count);
30+
31+
status_int = i2c_write(Dev->i2c, buf, count + 2, Dev->I2cDevAddr);
32+
33+
if (status_int < 0) {
34+
Status = VL53L1_ERROR_CONTROL_INTERFACE;
35+
LOG_ERR("Failed to write");
36+
}
37+
38+
return Status;
39+
}
40+
41+
VL53L1_Error VL53L1_ReadMulti(VL53L1_DEV Dev, uint16_t index, uint8_t *pdata,
42+
uint32_t count)
43+
{
44+
VL53L1_Error Status = VL53L1_ERROR_NONE;
45+
int32_t status_int;
46+
u8_t buf[2];
47+
48+
buf[0] = index >> 8;
49+
buf[1] = index & 0xFF;
50+
51+
status_int =
52+
i2c_write_read(Dev->i2c, Dev->I2cDevAddr, buf, 2, pdata, count);
53+
if (status_int < 0) {
54+
LOG_ERR("Failed to read");
55+
return -EIO;
56+
}
57+
58+
return Status;
59+
}
60+
61+
VL53L1_Error VL53L1_WrByte(VL53L1_DEV Dev, uint16_t index, uint8_t data)
62+
{
63+
VL53L1_Error Status = VL53L1_ERROR_NONE;
64+
int32_t status_int;
65+
u8_t buf[3];
66+
67+
buf[0] = index >> 8;
68+
buf[1] = index & 0xFF;
69+
buf[2] = data;
70+
71+
status_int = i2c_write(Dev->i2c, buf, 3, Dev->I2cDevAddr);
72+
73+
if (status_int < 0) {
74+
Status = VL53L1_ERROR_CONTROL_INTERFACE;
75+
LOG_ERR("i2c_reg_write_byte failed (%d)", Status);
76+
}
77+
78+
return Status;
79+
}
80+
81+
VL53L1_Error VL53L1_WrWord(VL53L1_DEV Dev, uint16_t index, uint16_t data)
82+
{
83+
VL53L1_Error Status = VL53L1_ERROR_NONE;
84+
int32_t status_int;
85+
uint8_t buf[4];
86+
87+
buf[0] = index >> 8;
88+
buf[1] = index & 0x00FF;
89+
buf[2] = data >> 8;
90+
buf[3] = data & 0x00FF;
91+
92+
status_int = i2c_write(Dev->i2c, buf, 4, Dev->I2cDevAddr);
93+
if (status_int < 0) {
94+
Status = VL53L1_ERROR_CONTROL_INTERFACE;
95+
LOG_ERR("i2c_write failed (%d)", Status);
96+
}
97+
98+
return Status;
99+
}
100+
101+
VL53L1_Error VL53L1_WrDWord(VL53L1_DEV Dev, uint16_t index, uint32_t data)
102+
{
103+
VL53L1_Error Status = VL53L1_ERROR_NONE;
104+
int32_t status_int;
105+
uint8_t buf[6];
106+
107+
buf[0] = index >> 8;
108+
buf[1] = index & 0x00FF;
109+
buf[2] = (data >> 24) & 0xFF;
110+
buf[3] = (data >> 16) & 0xFF;
111+
buf[4] = (data >> 8) & 0xFF;
112+
buf[5] = (data >> 0) & 0xFF;
113+
114+
status_int = i2c_write(Dev->i2c, buf, 6, Dev->I2cDevAddr);
115+
if (status_int < 0) {
116+
Status = VL53L1_ERROR_CONTROL_INTERFACE;
117+
LOG_ERR("i2c_write failed (%d)", Status);
118+
}
119+
120+
return Status;
121+
}
122+
123+
VL53L1_Error VL53L1_UpdateByte(VL53L1_DEV Dev, uint16_t index, uint8_t AndData,
124+
uint8_t OrData)
125+
{
126+
VL53L1_Error Status = VL53L1_ERROR_NONE;
127+
int32_t status_int;
128+
uint8_t deviceAddress;
129+
uint8_t data;
130+
131+
deviceAddress = Dev->I2cDevAddr;
132+
133+
status_int = VL53L1_RdByte(Dev, index, &data);
134+
if (status_int < 0) {
135+
Status = VL53L1_ERROR_CONTROL_INTERFACE;
136+
LOG_ERR("VL53L1_RdByte failed (%d)", Status);
137+
}
138+
139+
if (Status == VL53L1_ERROR_NONE) {
140+
data = (data & AndData) | OrData;
141+
status_int = VL53L1_WrByte(Dev, index, data);
142+
if (status_int != 0) {
143+
Status = VL53L1_ERROR_CONTROL_INTERFACE;
144+
LOG_DBG("VL53L1_WrByte failed.(%d)", Status);
145+
}
146+
}
147+
148+
return Status;
149+
}
150+
151+
VL53L1_Error VL53L1_RdByte(VL53L1_DEV Dev, uint16_t index, uint8_t *data)
152+
{
153+
VL53L1_Error Status = VL53L1_ERROR_NONE;
154+
int32_t status_int;
155+
uint8_t buf[2];
156+
157+
buf[0] = index >> 8;
158+
buf[1] = index & 0xFF;
159+
160+
status_int = i2c_write_read(Dev->i2c, Dev->I2cDevAddr, buf, 2, buf, 1);
161+
if (status_int < 0) {
162+
Status = VL53L1_ERROR_CONTROL_INTERFACE;
163+
LOG_ERR("i2c_reg_read_byte failed (%d)", Status);
164+
}
165+
*data = buf[0];
166+
167+
return Status;
168+
}
169+
170+
VL53L1_Error VL53L1_RdWord(VL53L1_DEV Dev, uint16_t index, uint16_t *data)
171+
{
172+
VL53L1_Error Status = VL53L1_ERROR_NONE;
173+
int32_t status_int;
174+
uint8_t buf[2];
175+
176+
buf[0] = index >> 8;
177+
buf[1] = index & 0xFF;
178+
179+
status_int = i2c_write_read(Dev->i2c, Dev->I2cDevAddr, buf, 2, buf, 2);
180+
if (status_int < 0) {
181+
LOG_ERR("i2c_write_read failed");
182+
return -EIO;
183+
}
184+
*data = ((uint16_t)buf[0] << 8) + (uint16_t)buf[1];
185+
186+
return Status;
187+
}
188+
189+
VL53L1_Error VL53L1_RdDWord(VL53L1_DEV Dev, uint16_t index, uint32_t *data)
190+
{
191+
VL53L1_Error Status = VL53L1_ERROR_NONE;
192+
int32_t status_int;
193+
u8_t buf[4];
194+
195+
buf[0] = index >> 8;
196+
buf[1] = index & 0xFF;
197+
198+
status_int = i2c_write_read(Dev->i2c, Dev->I2cDevAddr, buf, 2, buf, 4);
199+
if (status_int < 0) {
200+
LOG_ERR("i2c_write_read failed");
201+
return -EIO;
202+
}
203+
*data = ((uint32_t)buf[0] << 24) + ((uint32_t)buf[1] << 16) +
204+
((uint32_t)buf[2] << 8) + (uint32_t)buf[3];
205+
206+
return Status;
207+
}
208+
209+
VL53L1_Error VL53L1_GetTickCount(uint32_t *ptick_count_ms)
210+
{
211+
VL53L1_Error status = VL53L1_ERROR_NONE;
212+
*ptick_count_ms = k_cycle_get_32();
213+
return status;
214+
}
215+
216+
VL53L1_Error VL53L1_GetTimerFrequency(int32_t *ptimer_freq_hz)
217+
{
218+
VL53L1_Error status = VL53L1_ERROR_NONE;
219+
return status;
220+
}
221+
222+
VL53L1_Error VL53L1_WaitMs(VL53L1_Dev_t *pdev, int32_t wait_ms)
223+
{
224+
VL53L1_Error status = VL53L1_ERROR_NONE;
225+
k_sleep(wait_ms);
226+
return status;
227+
}
228+
229+
VL53L1_Error VL53L1_WaitUs(VL53L1_Dev_t *pdev, int32_t wait_us)
230+
{
231+
VL53L1_Error status = VL53L1_ERROR_NONE;
232+
k_busy_wait(wait_us);
233+
return status;
234+
}
235+
236+
VL53L1_Error VL53L1_WaitValueMaskEx(VL53L1_Dev_t *pdev, uint32_t timeout_ms,
237+
uint16_t index, uint8_t value, uint8_t mask,
238+
uint32_t poll_delay_ms)
239+
{
240+
VL53L1_Error status = VL53L1_ERROR_NONE;
241+
return status;
242+
}

0 commit comments

Comments
 (0)