-
Notifications
You must be signed in to change notification settings - Fork 8.2k
Add vl53l1x support #15566
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
Add vl53l1x support #15566
Changes from all commits
30621bc
0cb9b44
f91f0ca
3c79aa1
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,10 @@ | ||
| # SPDX-License-Identifier: Apache-2.0 | ||
|
|
||
| zephyr_library() | ||
|
|
||
| zephyr_library_sources( | ||
| vl53l1x.c | ||
| vl53l1_platform.c | ||
| ) | ||
|
|
||
| zephyr_library_sources_ifdef(CONFIG_VL53L1X_TRIGGER vl53l1x_trigger.c) |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,81 @@ | ||
| # | ||
| # Copyright (c) 2019 Aaron Tsui <[email protected]> | ||
| # | ||
| # SPDX-License-Identifier: Apache-2.0 | ||
| # | ||
|
|
||
| menuconfig VL53L1X | ||
| bool "VL53L1X time of flight sensor" | ||
| depends on I2C && HAS_DTS_I2C | ||
| select HAS_STLIB | ||
| help | ||
| Enable driver for VL53L1X I2C-based time of flight sensor. | ||
|
|
||
| if VL53L1X | ||
|
|
||
| choice VL53L1X_TRIGGER_MODE | ||
| prompt "Trigger mode" | ||
| default VL53L1X_TRIGGER_NONE | ||
| help | ||
| Specify the type of triggering to be used by the driver. | ||
|
|
||
| config VL53L1X_TRIGGER_NONE | ||
| bool "No trigger" | ||
|
|
||
| config VL53L1X_TRIGGER_GLOBAL_THREAD | ||
| bool "Use global thread" | ||
| depends on GPIO | ||
| select VL53L1X_TRIGGER | ||
|
|
||
| config VL53L1X_TRIGGER_OWN_THREAD | ||
| bool "Use own thread" | ||
| depends on GPIO | ||
| select VL53L1X_TRIGGER | ||
|
|
||
| endchoice # VL53L1X_TRIGGER_MODE | ||
|
|
||
| config VL53L1X_TRIGGER | ||
| bool | ||
|
|
||
| config VL53L1X_THREAD_PRIORITY | ||
| int "Thread priority" | ||
| depends on VL53L1X_TRIGGER_OWN_THREAD | ||
| default 10 | ||
| help | ||
| Priority of thread used by the driver to handle interrupts. | ||
|
|
||
| config VL53L1X_THREAD_STACK_SIZE | ||
| int "Thread stack size" | ||
| depends on VL53L1X_TRIGGER_OWN_THREAD | ||
| default 1024 | ||
| help | ||
| Stack size of thread used by the driver to handle interrupts. | ||
|
|
||
| config VL53L1X_XSHUT_CONTROL_ENABLE | ||
| bool "Enable XSHUT pin control" | ||
| help | ||
| Enable it if XSHUT pin is controlled by host. | ||
|
|
||
| config VL53L1X_XSHUT_GPIO_DEV_NAME | ||
| string "GPIO device" | ||
| default "GPIOC" | ||
| depends on VL53L1X_XSHUT_CONTROL_ENABLE | ||
| help | ||
| The device name of the GPIO device to which the VL53L1X xshut pin | ||
| is connected. | ||
|
|
||
| config VL53L1X_XSHUT_GPIO_PIN_NUM | ||
| int "Interrupt GPIO pin number" | ||
| default 6 | ||
| depends on VL53L1X_XSHUT_CONTROL_ENABLE | ||
| help | ||
| The number of the GPIO on which the xshut signal from the VL53L1X | ||
| is connected. | ||
|
|
||
| config VL53L1X_PROXIMITY_THRESHOLD | ||
| int "Proximity threshold in millimeters" | ||
| default 100 | ||
| help | ||
| Threshold used for proximity detection when sensor is used with SENSOR_CHAN_PROX. | ||
|
|
||
| endif # VL53L1X | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,242 @@ | ||
| /* | ||
| * Copyright (c) 2019 Aaron Tsui <[email protected]> | ||
| * | ||
| * SPDX-License-Identifier: Apache-2.0 | ||
| */ | ||
|
|
||
|
|
||
| #include "vl53l1_platform.h" | ||
|
|
||
| #include <sensor.h> | ||
| #include <kernel.h> | ||
| #include <device.h> | ||
| #include <init.h> | ||
| #include <i2c.h> | ||
| #include <logging/log.h> | ||
|
|
||
| #define LOG_LEVEL CONFIG_SENSOR_LOG_LEVEL | ||
| LOG_MODULE_DECLARE(VL53L1X); | ||
|
|
||
| VL53L1_Error VL53L1_WriteMulti(VL53L1_DEV Dev, uint16_t index, uint8_t *pdata, | ||
| uint32_t count) | ||
| { | ||
|
||
| VL53L1_Error Status = VL53L1_ERROR_NONE; | ||
| int32_t status_int = 0; | ||
| uint8_t buf[count + 2]; | ||
|
|
||
| buf[0] = index >> 8; | ||
| buf[1] = index & 0xFF; | ||
| memcpy(&buf[2], pdata, count); | ||
|
|
||
| status_int = i2c_write(Dev->i2c, buf, count + 2, Dev->I2cDevAddr); | ||
|
|
||
| if (status_int < 0) { | ||
| Status = VL53L1_ERROR_CONTROL_INTERFACE; | ||
| LOG_ERR("Failed to write"); | ||
| } | ||
|
|
||
| return Status; | ||
| } | ||
|
|
||
| VL53L1_Error VL53L1_ReadMulti(VL53L1_DEV Dev, uint16_t index, uint8_t *pdata, | ||
| uint32_t count) | ||
|
||
| { | ||
| VL53L1_Error Status = VL53L1_ERROR_NONE; | ||
| int32_t status_int; | ||
| u8_t buf[2]; | ||
|
|
||
| buf[0] = index >> 8; | ||
| buf[1] = index & 0xFF; | ||
|
|
||
| status_int = | ||
| i2c_write_read(Dev->i2c, Dev->I2cDevAddr, buf, 2, pdata, count); | ||
| if (status_int < 0) { | ||
| LOG_ERR("Failed to read"); | ||
| return -EIO; | ||
| } | ||
|
|
||
| return Status; | ||
| } | ||
|
|
||
| VL53L1_Error VL53L1_WrByte(VL53L1_DEV Dev, uint16_t index, uint8_t data) | ||
| { | ||
| VL53L1_Error Status = VL53L1_ERROR_NONE; | ||
| int32_t status_int; | ||
| u8_t buf[3]; | ||
|
|
||
| buf[0] = index >> 8; | ||
| buf[1] = index & 0xFF; | ||
| buf[2] = data; | ||
|
|
||
| status_int = i2c_write(Dev->i2c, buf, 3, Dev->I2cDevAddr); | ||
|
|
||
| if (status_int < 0) { | ||
| Status = VL53L1_ERROR_CONTROL_INTERFACE; | ||
| LOG_ERR("i2c_reg_write_byte failed (%d)", Status); | ||
| } | ||
|
|
||
| return Status; | ||
| } | ||
|
|
||
| VL53L1_Error VL53L1_WrWord(VL53L1_DEV Dev, uint16_t index, uint16_t data) | ||
| { | ||
| VL53L1_Error Status = VL53L1_ERROR_NONE; | ||
| int32_t status_int; | ||
| uint8_t buf[4]; | ||
|
|
||
| buf[0] = index >> 8; | ||
| buf[1] = index & 0x00FF; | ||
| buf[2] = data >> 8; | ||
| buf[3] = data & 0x00FF; | ||
|
|
||
| status_int = i2c_write(Dev->i2c, buf, 4, Dev->I2cDevAddr); | ||
| if (status_int < 0) { | ||
| Status = VL53L1_ERROR_CONTROL_INTERFACE; | ||
| LOG_ERR("i2c_write failed (%d)", Status); | ||
| } | ||
|
|
||
| return Status; | ||
| } | ||
|
|
||
| VL53L1_Error VL53L1_WrDWord(VL53L1_DEV Dev, uint16_t index, uint32_t data) | ||
| { | ||
| VL53L1_Error Status = VL53L1_ERROR_NONE; | ||
| int32_t status_int; | ||
| uint8_t buf[6]; | ||
|
|
||
| buf[0] = index >> 8; | ||
| buf[1] = index & 0x00FF; | ||
| buf[2] = (data >> 24) & 0xFF; | ||
| buf[3] = (data >> 16) & 0xFF; | ||
| buf[4] = (data >> 8) & 0xFF; | ||
| buf[5] = (data >> 0) & 0xFF; | ||
|
|
||
| status_int = i2c_write(Dev->i2c, buf, 6, Dev->I2cDevAddr); | ||
| if (status_int < 0) { | ||
| Status = VL53L1_ERROR_CONTROL_INTERFACE; | ||
| LOG_ERR("i2c_write failed (%d)", Status); | ||
| } | ||
|
|
||
| return Status; | ||
| } | ||
|
|
||
| VL53L1_Error VL53L1_UpdateByte(VL53L1_DEV Dev, uint16_t index, uint8_t AndData, | ||
| uint8_t OrData) | ||
| { | ||
| VL53L1_Error Status = VL53L1_ERROR_NONE; | ||
| int32_t status_int; | ||
| uint8_t deviceAddress; | ||
| uint8_t data; | ||
|
|
||
| deviceAddress = Dev->I2cDevAddr; | ||
|
|
||
| status_int = VL53L1_RdByte(Dev, index, &data); | ||
| if (status_int < 0) { | ||
| Status = VL53L1_ERROR_CONTROL_INTERFACE; | ||
| LOG_ERR("VL53L1_RdByte failed (%d)", Status); | ||
| } | ||
|
|
||
| if (Status == VL53L1_ERROR_NONE) { | ||
| data = (data & AndData) | OrData; | ||
| status_int = VL53L1_WrByte(Dev, index, data); | ||
| if (status_int != 0) { | ||
| Status = VL53L1_ERROR_CONTROL_INTERFACE; | ||
| LOG_DBG("VL53L1_WrByte failed.(%d)", Status); | ||
| } | ||
| } | ||
|
|
||
| return Status; | ||
| } | ||
|
|
||
| VL53L1_Error VL53L1_RdByte(VL53L1_DEV Dev, uint16_t index, uint8_t *data) | ||
| { | ||
| VL53L1_Error Status = VL53L1_ERROR_NONE; | ||
| int32_t status_int; | ||
| uint8_t buf[2]; | ||
|
|
||
| buf[0] = index >> 8; | ||
| buf[1] = index & 0xFF; | ||
|
|
||
| status_int = i2c_write_read(Dev->i2c, Dev->I2cDevAddr, buf, 2, buf, 1); | ||
| if (status_int < 0) { | ||
| Status = VL53L1_ERROR_CONTROL_INTERFACE; | ||
| LOG_ERR("i2c_reg_read_byte failed (%d)", Status); | ||
| } | ||
| *data = buf[0]; | ||
|
|
||
| return Status; | ||
| } | ||
|
|
||
| VL53L1_Error VL53L1_RdWord(VL53L1_DEV Dev, uint16_t index, uint16_t *data) | ||
| { | ||
| VL53L1_Error Status = VL53L1_ERROR_NONE; | ||
| int32_t status_int; | ||
| uint8_t buf[2]; | ||
|
|
||
| buf[0] = index >> 8; | ||
| buf[1] = index & 0xFF; | ||
|
|
||
| status_int = i2c_write_read(Dev->i2c, Dev->I2cDevAddr, buf, 2, buf, 2); | ||
| if (status_int < 0) { | ||
| LOG_ERR("i2c_write_read failed"); | ||
| return -EIO; | ||
| } | ||
| *data = ((uint16_t)buf[0] << 8) + (uint16_t)buf[1]; | ||
|
|
||
| return Status; | ||
| } | ||
|
|
||
| VL53L1_Error VL53L1_RdDWord(VL53L1_DEV Dev, uint16_t index, uint32_t *data) | ||
| { | ||
| VL53L1_Error Status = VL53L1_ERROR_NONE; | ||
| int32_t status_int; | ||
| u8_t buf[4]; | ||
|
|
||
| buf[0] = index >> 8; | ||
| buf[1] = index & 0xFF; | ||
|
|
||
| status_int = i2c_write_read(Dev->i2c, Dev->I2cDevAddr, buf, 2, buf, 4); | ||
| if (status_int < 0) { | ||
| LOG_ERR("i2c_write_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; | ||
| } | ||
|
|
||
| VL53L1_Error VL53L1_GetTickCount(uint32_t *ptick_count_ms) | ||
| { | ||
| VL53L1_Error status = VL53L1_ERROR_NONE; | ||
| *ptick_count_ms = k_cycle_get_32(); | ||
| return status; | ||
| } | ||
|
|
||
| VL53L1_Error VL53L1_GetTimerFrequency(int32_t *ptimer_freq_hz) | ||
| { | ||
| VL53L1_Error status = VL53L1_ERROR_NONE; | ||
| return status; | ||
| } | ||
|
|
||
| VL53L1_Error VL53L1_WaitMs(VL53L1_Dev_t *pdev, int32_t wait_ms) | ||
| { | ||
| VL53L1_Error status = VL53L1_ERROR_NONE; | ||
| k_sleep(wait_ms); | ||
| return status; | ||
| } | ||
|
|
||
| VL53L1_Error VL53L1_WaitUs(VL53L1_Dev_t *pdev, int32_t wait_us) | ||
| { | ||
| VL53L1_Error status = VL53L1_ERROR_NONE; | ||
| k_busy_wait(wait_us); | ||
| return status; | ||
| } | ||
|
|
||
| VL53L1_Error VL53L1_WaitValueMaskEx(VL53L1_Dev_t *pdev, uint32_t timeout_ms, | ||
| uint16_t index, uint8_t value, uint8_t mask, | ||
| uint32_t poll_delay_ms) | ||
| { | ||
| VL53L1_Error status = VL53L1_ERROR_NONE; | ||
| return status; | ||
| } | ||
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.
I guess here you may want to add a new copyright (2019).
Anyway, I was thinking whether we ca re-use same driver structure for both VL53L0X and VL53L1X. They seem
similar.