-
Notifications
You must be signed in to change notification settings - Fork 8k
st: vl6180x: Driver for vl6180x #50517
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
Closed
Closed
Changes from all commits
Commits
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,8 @@ | ||
# SPDX-License-Identifier: Apache-2.0 | ||
|
||
zephyr_library() | ||
|
||
zephyr_library_sources( | ||
vl6180x.c | ||
vl6180_platform.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,33 @@ | ||
# VL6180X time of flight sensor configuration options | ||
|
||
# Copyright (c) 2017 STMicroelectronics | ||
# SPDX-License-Identifier: Apache-2.0 | ||
|
||
menuconfig VL6180X | ||
bool "VL6180X time of flight sensor" | ||
default y | ||
depends on DT_HAS_ST_VL6180X_ENABLED | ||
select I2C | ||
select HAS_STLIB | ||
help | ||
Enable driver for VL6180X I2C-based time of flight sensor. | ||
|
||
config VL6180X_PROXIMITY_THRESHOLD | ||
int "Proximity threshold in millimeters" | ||
default 100 | ||
depends on VL6180X | ||
help | ||
Threshold used for proximity detection when sensor is used with SENSOR_CHAN_PROX. | ||
|
||
config VL6180X_RECONFIGURE_ADDRESS | ||
bool "Support reconfigurable sensor address" | ||
depends on VL6180X | ||
help | ||
Enable support for reconfiguring the sensor address at runtime. | ||
When this option is enabled, all sensors declared in the device tree | ||
must have an xshut-gpio property. | ||
|
||
All sensors are disabled during initialization. When reading the first | ||
value from a sensor, it is powered up and its I2C address is reconfigured | ||
from the manufacturer default (0x29) to the address specified in the | ||
device tree. |
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,192 @@ | ||
/* vl6180x_platform.c - Zephyr customization of ST vl6180x library. | ||
* (library is located in ext/hal/st/lib/sensor/vl6180x/) | ||
*/ | ||
|
||
/* | ||
* Copyright (c) 2017 STMicroelectronics | ||
* | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
#include "vl6180_platform.h" | ||
#include "vl6180_api.h" | ||
|
||
#include <zephyr/drivers/sensor.h> | ||
#include <zephyr/kernel.h> | ||
#include <zephyr/device.h> | ||
#include <zephyr/init.h> | ||
#include <zephyr/drivers/i2c.h> | ||
#include <zephyr/logging/log.h> | ||
|
||
LOG_MODULE_DECLARE(VL6180X, CONFIG_SENSOR_LOG_LEVEL); | ||
|
||
#define VL6180X_ERROR_CONTROL_INTERFACE -5 | ||
|
||
|
||
int VL6180_RdMulti(VL6180Dev_t dev, uint16_t index, uint8_t *data, int nData) | ||
{ | ||
int Status = NoError; | ||
int32_t status_int; | ||
uint8_t reg[2]; | ||
|
||
reg[0] = (index >> 8) & 0xFF; | ||
reg[1] = (index >> 0) & 0xFF; | ||
status_int = i2c_write_read(dev->i2c, dev->I2cDevAddr, reg, sizeof(reg), data, nData); | ||
if (status_int < 0) { | ||
LOG_ERR("Failed to read"); | ||
return -EIO; | ||
} | ||
|
||
return Status; | ||
} | ||
|
||
int VL6180_WrByte(VL6180Dev_t Dev, uint16_t index, uint8_t data) | ||
{ | ||
int Status = NoError; | ||
int32_t status_int; | ||
uint8_t I2CBuffer[3]; | ||
|
||
I2CBuffer[0] = (index >> 8) & 0xFF; | ||
I2CBuffer[1] = (index >> 0) & 0xFF; | ||
I2CBuffer[2] = data; | ||
|
||
status_int = i2c_write(Dev->i2c, I2CBuffer, sizeof(I2CBuffer), Dev->I2cDevAddr); | ||
if (status_int < 0) { | ||
Status = VL6180X_ERROR_CONTROL_INTERFACE; | ||
LOG_ERR("i2c_reg_write_byte failed (%d)", Status); | ||
} | ||
|
||
return Status; | ||
} | ||
|
||
int VL6180_WrWord(VL6180Dev_t Dev, uint16_t index, uint16_t data) | ||
{ | ||
int Status = NoError; | ||
int32_t status_int; | ||
uint8_t I2CBuffer[4]; | ||
|
||
I2CBuffer[0] = (index >> 8) & 0xFF; | ||
I2CBuffer[1] = (index >> 0) & 0xFF; | ||
I2CBuffer[2] = data >> 8; | ||
I2CBuffer[3] = data & 0x00FF; | ||
|
||
status_int = i2c_write(Dev->i2c, I2CBuffer, sizeof(I2CBuffer), Dev->I2cDevAddr); | ||
if (status_int < 0) { | ||
Status = VL6180X_ERROR_CONTROL_INTERFACE; | ||
LOG_ERR("i2c_write failed (%d)", Status); | ||
} | ||
|
||
return Status; | ||
} | ||
|
||
int VL6180_WrDWord(VL6180Dev_t Dev, uint16_t index, uint32_t data) | ||
{ | ||
int Status = NoError; | ||
int32_t status_int; | ||
uint8_t I2CBuffer[6]; | ||
|
||
I2CBuffer[0] = (index >> 8) & 0xFF; | ||
I2CBuffer[1] = (index >> 0) & 0xFF; | ||
I2CBuffer[2] = (data >> 24) & 0xFF; | ||
I2CBuffer[3] = (data >> 16) & 0xFF; | ||
I2CBuffer[4] = (data >> 8) & 0xFF; | ||
I2CBuffer[5] = (data >> 0) & 0xFF; | ||
|
||
status_int = i2c_write(Dev->i2c, I2CBuffer, sizeof(I2CBuffer), Dev->I2cDevAddr); | ||
if (status_int < 0) { | ||
Status = VL6180X_ERROR_CONTROL_INTERFACE; | ||
LOG_ERR("i2c_write failed (%d)", Status); | ||
} | ||
|
||
return Status; | ||
} | ||
|
||
int VL6180_UpdateByte(VL6180Dev_t Dev, uint16_t index, | ||
uint8_t AndData, uint8_t OrData) | ||
{ | ||
int Status = NoError; | ||
int32_t status_int; | ||
uint8_t deviceAddress; | ||
uint8_t data; | ||
|
||
deviceAddress = Dev->I2cDevAddr; | ||
|
||
status_int = VL6180_RdByte(Dev, index, &data); | ||
if (status_int < 0) { | ||
Status = VL6180X_ERROR_CONTROL_INTERFACE; | ||
LOG_ERR("VL6180_RdByte failed (%d)", Status); | ||
} | ||
|
||
if (Status == NoError) { | ||
data = (data & AndData) | OrData; | ||
status_int = VL6180_WrByte(Dev, index, data); | ||
if (status_int != 0) { | ||
Status = VL6180X_ERROR_CONTROL_INTERFACE; | ||
LOG_DBG("VL6180_WrByte failed.(%d)", Status); | ||
} | ||
} | ||
|
||
return Status; | ||
} | ||
|
||
int VL6180_RdByte(VL6180Dev_t dev, uint16_t index, uint8_t *data) | ||
{ | ||
int Status = NoError; | ||
int32_t status_int; | ||
uint8_t reg[2]; | ||
|
||
reg[0] = (index >> 8) & 0xFF; | ||
reg[1] = (index >> 0) & 0xFF; | ||
status_int = i2c_write_read(dev->i2c, dev->I2cDevAddr, reg, sizeof(reg), data, 1); | ||
if (status_int < 0) { | ||
Status = VL6180X_ERROR_CONTROL_INTERFACE; | ||
LOG_ERR("i2c_reg_read_byte failed (%d)", Status); | ||
} | ||
|
||
return Status; | ||
} | ||
|
||
int VL6180_RdWord(VL6180Dev_t dev, uint16_t index, uint16_t *data) | ||
{ | ||
int Status = NoError; | ||
int32_t status_int; | ||
uint8_t reg[2]; | ||
uint8_t buf[2]; | ||
|
||
reg[0] = (index >> 8) & 0xFF; | ||
reg[1] = (index >> 0) & 0xFF; | ||
status_int = i2c_write_read(dev->i2c, dev->I2cDevAddr, reg, sizeof(reg), buf, sizeof(buf)); | ||
if (status_int < 0) { | ||
LOG_ERR("i2c_burst_read failed"); | ||
return -EIO; | ||
} | ||
*data = ((uint16_t)buf[0]<<8) + (uint16_t)buf[1]; | ||
|
||
return Status; | ||
} | ||
|
||
int VL6180_RdDWord(VL6180Dev_t dev, uint16_t index, uint32_t *data) | ||
{ | ||
int Status = NoError; | ||
int32_t status_int; | ||
uint8_t reg[2]; | ||
uint8_t buf[4]; | ||
|
||
reg[0] = (index >> 8) & 0xFF; | ||
reg[1] = (index >> 0) & 0xFF; | ||
status_int = i2c_write_read(dev->i2c, dev->I2cDevAddr, reg, sizeof(reg), buf, sizeof(buf)); | ||
if (status_int < 0) { | ||
LOG_ERR("i2c_burst_read failed"); | ||
return -EIO; | ||
} | ||
*data = ((uint32_t)buf[0]<<24) + ((uint32_t)buf[1]<<16) + | ||
((uint32_t)buf[2]<<8) + (uint32_t)buf[3]; | ||
|
||
return Status; | ||
} | ||
|
||
void VL6180_PollDelay(VL6180Dev_t Dev) | ||
{ | ||
k_sleep(K_MSEC(2)); | ||
} | ||
|
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Convert this to a devicetree property so it can be configured on a per-instance basis