-
Notifications
You must be signed in to change notification settings - Fork 7.6k
drivers: sensor: add Garmin LIDAR-Lite v4 LED distance sensor driver #93063
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
alexayl
wants to merge
3
commits into
zephyrproject-rtos:main
Choose a base branch
from
alexayl:lidar_lite_v4
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+165
−0
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
# Copyright (c) 2025 Alex Aylward | ||
# SPDX-License-Identifier: Apache-2.0 | ||
|
||
# zephyr-keep-sorted-start | ||
add_subdirectory_ifdef(CONFIG_LIDAR_LITE_V4 lidar_lite_v4) | ||
# zephyr-keep-sorted-stop |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
# Copyright (c) 2025 Alex Aylward | ||
# SPDX-License-Identifier: Apache-2.0 | ||
|
||
# zephyr-keep-sorted-start | ||
source "drivers/sensor/grmn/lidar_lite_v4/Kconfig" | ||
# zephyr-keep-sorted-stop |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
# Copyright (c) 2025 Alex Aylward | ||
# SPDX-License-Identifier: Apache-2.0 | ||
|
||
zephyr_library() | ||
zephyr_library_sources(lidar_lite_v4.c) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
# Copyright (c) 2025 Alex Aylward | ||
# SPDX-License-Identifier: Apache-2.0 | ||
|
||
config LIDAR_LITE_V4 | ||
bool "Lidar Lite V4 support" | ||
depends on DT_HAS_GRMN_LIDAR_LITE_V4_ENABLED | ||
default y | ||
select I2C | ||
help | ||
Driver for Lidar Lite V4 I2C Operation. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,123 @@ | ||
/* | ||
* Copyright (c) 2025 Alex Aylward | ||
* | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
#define DT_DRV_COMPAT grmn_lidar_lite_v4 | ||
|
||
#include <zephyr/device.h> | ||
#include <zephyr/drivers/i2c.h> | ||
#include <zephyr/drivers/sensor.h> | ||
#include <zephyr/kernel.h> | ||
#include <zephyr/sys/byteorder.h> | ||
#include <zephyr/sys/clock.h> | ||
#include <zephyr/sys/time_units.h> | ||
|
||
#define MAX_TIMEOUT_MS 100 | ||
|
||
#define LIDAR_LITE_V4_REG_MEASURE 0x00 | ||
#define LIDAR_LITE_V4_REG_STATUS 0x01 | ||
#define LIDAR_LITE_V4_REG_DISTANCE_LOW 0x10 | ||
|
||
#define LIDAR_LITE_V4_CMD_MEASURE 0x04 | ||
#define LIDAR_LITE_V4_STATUS_BUSY BIT(0) | ||
|
||
struct lidar_lite_v4_config { | ||
struct i2c_dt_spec i2c; | ||
}; | ||
|
||
struct lidar_lite_v4_data { | ||
uint16_t distance; | ||
}; | ||
|
||
int lidar_lite_v4_init(const struct device *dev) | ||
{ | ||
const struct lidar_lite_v4_config *config = dev->config; | ||
|
||
if (!i2c_is_ready_dt(&config->i2c)) { | ||
return -ENODEV; | ||
} | ||
|
||
return 0; | ||
} | ||
|
||
static int lidar_lite_v4_sample_fetch(const struct device *dev, enum sensor_channel chan) | ||
{ | ||
struct lidar_lite_v4_data *data = dev->data; | ||
const struct lidar_lite_v4_config *config = dev->config; | ||
uint8_t status; | ||
uint8_t distance_bytes[2]; | ||
int ret; | ||
|
||
if (chan != SENSOR_CHAN_DISTANCE && chan != SENSOR_CHAN_ALL) { | ||
return -ENOTSUP; | ||
} | ||
|
||
alexayl marked this conversation as resolved.
Show resolved
Hide resolved
|
||
/* Write command to trigger measurement */ | ||
ret = i2c_reg_write_byte_dt(&config->i2c, LIDAR_LITE_V4_REG_MEASURE, | ||
LIDAR_LITE_V4_CMD_MEASURE); | ||
if (ret < 0) { | ||
return ret; | ||
} | ||
|
||
/* Read status register and wait until measurement completes (timeout based on elapsed time) | ||
*/ | ||
k_timepoint_t timeout = sys_timepoint_calc(K_MSEC(MAX_TIMEOUT_MS)); | ||
while (1) { | ||
ret = i2c_reg_read_byte_dt(&config->i2c, LIDAR_LITE_V4_REG_STATUS, &status); | ||
if (ret < 0) { | ||
return ret; | ||
} | ||
|
||
if ((status & LIDAR_LITE_V4_STATUS_BUSY) == 0) { | ||
break; | ||
} | ||
|
||
if (sys_timepoint_expired(timeout)) { | ||
return -ETIMEDOUT; | ||
} | ||
|
||
k_msleep(1); | ||
} | ||
|
||
/* Read distance data (low byte at 0x10, high byte at 0x11) */ | ||
ret = i2c_burst_read_dt(&config->i2c, LIDAR_LITE_V4_REG_DISTANCE_LOW, distance_bytes, 2); | ||
if (ret < 0) { | ||
return ret; | ||
} | ||
|
||
/* Combine low and high bytes (little-endian) to get 16-bit distance in centimeters */ | ||
data->distance = sys_get_le16(distance_bytes); | ||
|
||
return 0; | ||
} | ||
|
||
static int lidar_lite_v4_channel_get(const struct device *dev, enum sensor_channel chan, | ||
struct sensor_value *val) | ||
{ | ||
struct lidar_lite_v4_data *data = dev->data; | ||
|
||
switch (chan) { | ||
case SENSOR_CHAN_DISTANCE: | ||
return sensor_value_from_milli(val, (uint32_t)data->distance * 10); | ||
default: | ||
return -ENOTSUP; | ||
} | ||
} | ||
|
||
static const struct sensor_driver_api lidar_lite_v4_driver_api = { | ||
.sample_fetch = lidar_lite_v4_sample_fetch, | ||
.channel_get = lidar_lite_v4_channel_get, | ||
}; | ||
|
||
#define LIDAR_LITE_V4_DEFINE(inst) \ | ||
static struct lidar_lite_v4_data lidar_lite_v4_data_##inst; \ | ||
static const struct lidar_lite_v4_config lidar_lite_v4_config_##inst = { \ | ||
.i2c = I2C_DT_SPEC_INST_GET(inst), \ | ||
}; \ | ||
SENSOR_DEVICE_DT_INST_DEFINE(inst, lidar_lite_v4_init, NULL, &lidar_lite_v4_data_##inst, \ | ||
&lidar_lite_v4_config_##inst, POST_KERNEL, \ | ||
CONFIG_SENSOR_INIT_PRIORITY, &lidar_lite_v4_driver_api); | ||
|
||
DT_INST_FOREACH_STATUS_OKAY(LIDAR_LITE_V4_DEFINE) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
# Copyright (c) 2025 Alex Aylward | ||
# SPDX-License-Identifier: Apache-2.0 | ||
|
||
description: Garmin LIDAR-Lite v4 laser rangefinder | ||
|
||
compatible: "grmn,lidar-lite-v4" | ||
|
||
include: [sensor-device.yaml, i2c-device.yaml] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.