From 8740f92e792e3557b452172fa6b13fe610fb2f94 Mon Sep 17 00:00:00 2001 From: wangdw10 Date: Fri, 21 Jun 2019 11:35:04 +0800 Subject: [PATCH] input: vl53l0: update TOF driver - update vl53l0 driver to ver 1.1.23 - remove platform driver and cci depence Change-Id: Ic71c3e59f2aeb9cb196e998c42824248700dca36 Signed-off-by: wangdw10 Reviewed-on: https://gerrit.mot.com/1374391 SLTApproved: Slta Waiver SME-Granted: SME Approvals Granted Tested-by: Jira Key Reviewed-by: Chuanxin Guo Reviewed-by: Bolei Shang Reviewed-by: Dawei Wang Reviewed-by: Jianqi Yang Submit-Approved: Jira Key --- drivers/input/misc/vl53L0/Kbuild | 14 - drivers/input/misc/vl53L0/inc/vl53l010_api.h | 1423 ------ .../input/misc/vl53L0/inc/vl53l010_device.h | 231 - .../input/misc/vl53L0/inc/vl53l010_strings.h | 134 - .../input/misc/vl53L0/inc/vl53l010_tuning.h | 58 - drivers/input/misc/vl53L0/inc/vl53l0_api.h | 72 +- .../misc/vl53L0/inc/vl53l0_api_calibration.h | 2 +- .../input/misc/vl53L0/inc/vl53l0_api_core.h | 9 +- .../misc/vl53L0/inc/vl53l0_api_histogram.h | 4 +- .../misc/vl53L0/inc/vl53l0_api_ranging.h | 2 +- .../misc/vl53L0/inc/vl53l0_api_strings.h | 8 +- drivers/input/misc/vl53L0/inc/vl53l0_def.h | 190 +- drivers/input/misc/vl53L0/inc/vl53l0_device.h | 20 +- .../inc/vl53l0_interrupt_threshold_settings.h | 194 + .../input/misc/vl53L0/inc/vl53l0_platform.h | 14 +- .../misc/vl53L0/inc/vl53l0_platform_log.h | 19 +- .../input/misc/vl53L0/inc/vl53l0_strings.h | 211 - drivers/input/misc/vl53L0/inc/vl53l0_tuning.h | 3 +- drivers/input/misc/vl53L0/inc/vl53l0_types.h | 9 +- drivers/input/misc/vl53L0/src/vl53l010_api.c | 4127 ----------------- .../input/misc/vl53L0/src/vl53l010_tuning.c | 138 - drivers/input/misc/vl53L0/src/vl53l0_api.c | 545 ++- .../misc/vl53L0/src/vl53l0_api_calibration.c | 66 +- .../input/misc/vl53L0/src/vl53l0_api_core.c | 603 +-- .../misc/vl53L0/src/vl53l0_api_histogram.c | 12 +- .../misc/vl53L0/src/vl53l0_api_ranging.c | 2 +- .../misc/vl53L0/src/vl53l0_api_strings.c | 17 +- .../misc/vl53L0/src/vl53l0_i2c_platform.c | 11 +- .../input/misc/vl53L0/src/vl53l0_platform.c | 4 +- .../input/misc/vl53L0/src/vl53l0_port_i2c.c | 73 +- drivers/input/misc/vl53L0/src/vl53l0_tuning.c | 137 - drivers/input/misc/vl53L0/stmvl53l0-cci.h | 65 - drivers/input/misc/vl53L0/stmvl53l0-i2c.h | 13 +- drivers/input/misc/vl53L0/stmvl53l0.h | 210 +- .../input/misc/vl53L0/stmvl53l0_module-cci.c | 475 -- .../input/misc/vl53L0/stmvl53l0_module-i2c.c | 138 +- drivers/input/misc/vl53L0/stmvl53l0_module.c | 3710 +++++++-------- 37 files changed, 3181 insertions(+), 9782 deletions(-) delete mode 100644 drivers/input/misc/vl53L0/inc/vl53l010_api.h delete mode 100644 drivers/input/misc/vl53L0/inc/vl53l010_device.h delete mode 100644 drivers/input/misc/vl53L0/inc/vl53l010_strings.h delete mode 100644 drivers/input/misc/vl53L0/inc/vl53l010_tuning.h create mode 100644 drivers/input/misc/vl53L0/inc/vl53l0_interrupt_threshold_settings.h delete mode 100644 drivers/input/misc/vl53L0/inc/vl53l0_strings.h delete mode 100644 drivers/input/misc/vl53L0/src/vl53l010_api.c delete mode 100644 drivers/input/misc/vl53L0/src/vl53l010_tuning.c delete mode 100644 drivers/input/misc/vl53L0/src/vl53l0_tuning.c delete mode 100644 drivers/input/misc/vl53L0/stmvl53l0-cci.h delete mode 100644 drivers/input/misc/vl53L0/stmvl53l0_module-cci.c diff --git a/drivers/input/misc/vl53L0/Kbuild b/drivers/input/misc/vl53L0/Kbuild index d53a7c84513e..f2a451a166a2 100644 --- a/drivers/input/misc/vl53L0/Kbuild +++ b/drivers/input/misc/vl53L0/Kbuild @@ -3,28 +3,14 @@ # # Each configuration option enables a list of files. -FEATURE_USE_CCI := false - -ifeq ($(FEATURE_USE_CCI), true) -EXTRA_CFLAGS += -DCAMERA_CCI -else -EXTRA_CFLAGS += -DSTM_TEST -endif EXTRA_CFLAGS += -Wall EXTRA_CFLAGS += -I$(TOP)/motorola/kernel/modules/drivers/input/misc/vl53L0 EXTRA_CFLAGS += -I$(TOP)/motorola/kernel/modules/drivers/input/misc/vl53L0/inc -EXTRA_CFLAGS += -I$(TOP)/kernel/msm-4.14/drivers/media/platform/msm/camera_v2/sensor/io -EXTRA_CFLAGS += -I$(TOP)/kernel/msm-4.14/drivers/media/platform/msm/camera_v2 -EXTRA_CFLAGS += -I$(TOP)/kernel/msm-4.14/drivers/media/platform/msm/camera_v2/sensor/cci -EXTRA_CFLAGS += -I$(TOP)/kernel/msm-4.14/drivers/media/platform/msm/camera_v2/common - obj-m += stmvl53l0.o stmvl53l0-objs := stmvl53l0_module.o stmvl53l0_module-i2c.o stmvl53l0-objs += src/vl53l0_api_calibration.o src/vl53l0_api_core.o stmvl53l0-objs += src/vl53l0_api_histogram.o src/vl53l0_api_ranging.o stmvl53l0-objs += src/vl53l0_api_strings.o src/vl53l0_api.o src/vl53l0_platform.o stmvl53l0-objs += src/vl53l0_i2c_platform.o src/vl53l0_port_i2c.o -stmvl53l0-objs += src/vl53l010_api.o src/vl53l010_tuning.o - diff --git a/drivers/input/misc/vl53L0/inc/vl53l010_api.h b/drivers/input/misc/vl53L0/inc/vl53l010_api.h deleted file mode 100644 index 6054e4eff719..000000000000 --- a/drivers/input/misc/vl53L0/inc/vl53l010_api.h +++ /dev/null @@ -1,1423 +0,0 @@ -/******************************************************************************* -Copyright © 2016, STMicroelectronics International N.V. -All rights reserved. - -Redistribution and use in source and binary forms, with or without -modification, are permitted provided that the following conditions are met: - * Redistributions of source code must retain the above copyright - notice, this list of conditions and the following disclaimer. - * Redistributions in binary form must reproduce the above copyright - notice, this list of conditions and the following disclaimer in the - documentation and/or other materials provided with the distribution. - * Neither the name of STMicroelectronics nor the - names of its contributors may be used to endorse or promote products - derived from this software without specific prior written permission. - -THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND -ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED -WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, AND -NON-INFRINGEMENT OF INTELLECTUAL PROPERTY RIGHTS ARE DISCLAIMED. -IN NO EVENT SHALL STMICROELECTRONICS INTERNATIONAL N.V. BE LIABLE FOR ANY -DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES -(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; -LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND -ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT -(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS -SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. -*******************************************************************************/ -/* - * @file vl53l0_api.h - * $Date: 2014-12-04 16:15:06 +0100 (Thu, 04 Dec 2014) $ - * $Revision: 1906 $ - */ - - - -#ifndef _VL53L010_API_H_ -#define _VL53L010_API_H_ - -#include "vl53l010_device.h" -#include "vl53l010_strings.h" -#include "vl53l0_def.h" -#include "vl53l0_platform.h" - - -#ifdef __cplusplus -extern "C" { -#endif - - -#ifdef _MSC_VER -# ifdef VL53L0_API_EXPORTS -# define VL53L010_API __declspec(dllexport) -# else -# define VL53L010_API -# endif -#else -# define VL53L010_API -#endif - - -/** @defgroup VL53L010_cut10_group VL53L010 cut1.0 Function Definition - * @brief VL53L010 cut1.0 Function Definition - * @{ - */ - -/** @defgroup VL53L010_general_group VL53L010 General Functions - * @brief VL53L010 General functions and definitions - * @{ - */ - -/** - * @brief Return the VL53L0 PAL Implementation Version - * - * @note This function doesn't access to the device - * - * @param pVersion Pointer to current PAL Implementation Version - * @return VL53L0_ERROR_NONE Success - * @return "Other error code" See ::VL53L0_Error - */ -VL53L010_API VL53L0_Error VL53L010_GetVersion(VL53L0_Version_t *pVersion); - -/** - * @brief Return the PAL Specification Version used for the current - * implementation. - * - * @note This function doesn't access to the device - * - * @param pPalSpecVersion Pointer to current PAL Specification Version - * @return VL53L0_ERROR_NONE Success - * @return "Other error code" See ::VL53L0_Error - */ -VL53L010_API VL53L0_Error VL53L010_GetPalSpecVersion( - VL53L0_Version_t *pPalSpecVersion); - - -/** - * @brief Reads the Device information for given Device - * - * @note This function Access to the device.\n - * Use ProductRevisionMajor and ProductRevisionMinor to know the cut - * of the device used. - * - * @param Dev Device Handle - * @param pVL53L0_DeviceInfo Pointer to current device info for a given - * Device - * - * @return VL53L0_ERROR_NONE Success - * @return "Other error code" See ::VL53L0_Error - */ -VL53L010_API VL53L0_Error VL53L010_GetDeviceInfo(VL53L0_DEV Dev, - VL53L0_DeviceInfo_t *pVL53L0_DeviceInfo); - - -/** - * @brief Read current status of the error register for the selected device - * - * @note This function Access to the device - * - * @param Dev Device Handle - * @param pDeviceErrorStatus Pointer to current error code of the device - * @return VL53L0_ERROR_NONE Success - * @return "Other error code" See ::VL53L0_Error - */ -VL53L010_API VL53L0_Error VL53L010_GetDeviceErrorStatus(VL53L0_DEV Dev, - VL53L010_DeviceError *pDeviceErrorStatus); - -/** - * @brief Human readable error string for a given Error Code - * - * @note This function doesn't access to the device - * - * @param ErrorCode The error code as stored on - * ::VL53L0_DeviceError - * @param pDeviceErrorString The error string corresponding to the - * ErrorCode - * @return VL53L0_ERROR_NONE Success - * @return "Other error code" See ::VL53L0_Error - */ -VL53L010_API VL53L0_Error VL53L010_GetDeviceErrorString( - VL53L010_DeviceError ErrorCode, char *pDeviceErrorString); - - -/** - * @brief Human readable error string for current PAL error status - * - * @note This function doesn't access to the device - * - * @param PalErrorCode The error code as stored on @a VL53L0_Error - * @param pPalErrorString The error string corresponding to the - * PalErrorCode - * @return VL53L0_ERROR_NONE Success - * @return "Other error code" See ::VL53L0_Error - */ -VL53L010_API VL53L0_Error VL53L010_GetPalErrorString(VL53L0_Error PalErrorCode, - char *pPalErrorString); - - -/** - * @brief Reads the internal state of the PAL for a given Device - * - * @note This function doesn't access to the device - * - * @param Dev Device Handle - * @param pPalState Pointer to current state of the PAL for a - * given Device - * @return VL53L0_ERROR_NONE Success - * @return "Other error code" See ::VL53L0_Error - */ -VL53L010_API VL53L0_Error VL53L010_GetPalState(VL53L0_DEV Dev, - VL53L0_State *pPalState); - - -/** - * @brief Set the power mode for a given Device - * The power mode can be Standby or Idle. Different level of both Standby and - * Idle can exists. - * This function should not be used when device is in Ranging state. - * - * @note This function Access to the device - * - * @param Dev Device Handle - * @param PowerMode The value of the power mode to set. - * see ::VL53L0_PowerModes Valid values are: - * VL53L0_POWERMODE_STANDBY_LEVEL1, VL53L0_POWERMODE_IDLE_LEVEL1 - * @return VL53L0_ERROR_NONE Success - * @return VL53L0_ERROR_MODE_NOT_SUPPORTED This error occurs when PowerMode - * is not in the supported list - * @return "Other error code" See ::VL53L0_Error - */ -VL53L010_API VL53L0_Error VL53L010_SetPowerMode(VL53L0_DEV Dev, - VL53L0_PowerModes PowerMode); - -/** - * @brief Get the power mode for a given Device - * - * @note This function Access to the device - * - * @param Dev Device Handle - * @param pPowerMode Pointer to the current value of the power - * mode. see ::VL53L0_PowerModes. Valid values are: - * VL53L0_POWERMODE_STANDBY_LEVEL1, VL53L0_POWERMODE_IDLE_LEVEL1 - * @return VL53L0_ERROR_NONE Success - * @return "Other error code" See ::VL53L0_Error - */ -VL53L010_API VL53L0_Error VL53L010_GetPowerMode(VL53L0_DEV Dev, - VL53L0_PowerModes *pPowerMode); - - -/** - * Set or over-hide part to part calibration offset - * \sa VL53L0_DataInit() VL53L0_GetOffsetCalibrationDataMicroMeter() - * - * @note This function Access to the device - * - * @param Dev Device Handle - * @param OffsetCalibrationDataMicroMeter Offset (in micrometer) - * @return VL53L0_ERROR_NONE Success - * @return "Other error code" See ::VL53L0_Error - */ -VL53L010_API VL53L0_Error VL53L010_SetOffsetCalibrationDataMicroMeter( - VL53L0_DEV Dev, - int32_t OffsetCalibrationDataMicroMeter); - -/** - * @brief Get part to part calibration offset - * - * @par Function Description - * Should only be used after a successful call to @a VL53L0_DataInit to backup - * device NVM value - * - * @note This function Access to the device - * - * @param Dev Device Handle - * @param pOffsetCalibrationDataMicroMeter Return part to part calibration - * offset from device (in micro meter) - * @return VL53L0_ERROR_NONE Success - * @return "Other error code" See ::VL53L0_Error - */ -VL53L010_API VL53L0_Error VL53L010_GetOffsetCalibrationDataMicroMeter( - VL53L0_DEV Dev, - int32_t *pOffsetCalibrationDataMicroMeter); - -/** - * Set Group parameter Hold state - * - * @par Function Description - * Set or remove device internal group parameter hold - * - * @note This function is not Implemented - * - * @param Dev Device Handle - * @param GroupParamHold Group parameter Hold state to be set (on/off) - * @return VL53L0_ERROR_NOT_IMPLEMENTED Not implemented - */ -VL53L010_API VL53L0_Error VL53L010_SetGroupParamHold(VL53L0_DEV Dev, - uint8_t GroupParamHold); - -/** - * @brief Get the maximal distance for actual setup - * @par Function Description - * Device must be initialized through @a VL53L0_SetParameters() prior calling - * this function. - * - * Any range value more than the value returned is to be considered as "no - * target detected" - * or "no target in detectable range"\n - * @warning The maximal distance depends on the setup - * - * @note This function is not Implemented - * - * @param Dev Device Handle - * @param pUpperLimitMilliMeter The maximal range limit for actual setup - * (in millimeter) - * @return VL53L0_ERROR_NOT_IMPLEMENTED Not implemented - */ -VL53L010_API VL53L0_Error VL53L010_GetUpperLimitMilliMeter(VL53L0_DEV Dev, - uint16_t *pUpperLimitMilliMeter); - -/** @} VL53L010_general_group */ - - -/** @defgroup VL53L010_init_group VL53L010 Init Functions - * @brief VL53L010 Init Functions - * @{ - */ - -/** - * @brief Set new device address - * - * After completion the device will answer to the new address programmed. This - * function should be called when several devices are used in parallel - * before start programming the sensor. - * When a single device us used, there is no need to call this function. - * - * @note This function Access to the device - * - * @param Dev Device Handle - * @param DeviceAddress The new Device address - * @return VL53L0_ERROR_NONE Success - * @return "Other error code" See ::VL53L0_Error - */ -VL53L010_API VL53L0_Error VL53L010_SetDeviceAddress(VL53L0_DEV Dev, - uint8_t DeviceAddress); - -/** - * - * @brief One time device initialization - * - * To be called once and only once after device is brought out of reset (Chip - * enable) and booted see @a VL53L0_WaitDeviceBooted() - * - * @par Function Description - * When not used after a fresh device "power up" or reset, it may return @a - * #VL53L0_ERROR_CALIBRATION_WARNING - * meaning wrong calibration data may have been fetched from device that can - * result in ranging offset error\n - * If application cannot execute device reset or need to run VL53L0_DataInit - * multiple time - * then it must ensure proper offset calibration saving and restore on its own - * by using @a VL53L0_GetOffsetCalibrationData() on first power up and then @a - * VL53L0_SetOffsetCalibrationData() in all subsequent init - * This function will change the VL53L0_State from VL53L0_STATE_POWERDOWN to - * VL53L0_STATE_WAIT_STATICINIT. - * - * @note This function Access to the device - * - * @param Dev Device Handle - * @return VL53L0_ERROR_NONE Success - * @return "Other error code" See ::VL53L0_Error - */ -VL53L010_API VL53L0_Error VL53L010_DataInit(VL53L0_DEV Dev); - -/** - * @brief Do basic device init (and eventually patch loading) - * This function will change the VL53L0_State from VL53L0_STATE_WAIT_STATICINIT - * to VL53L0_STATE_IDLE. - * In this stage all defalut setting will be applied. - * - * @note This function Access to the device - * - * @param Dev Device Handle - * @return VL53L0_ERROR_NONE Success - * @return "Other error code" See ::VL53L0_Error - */ -VL53L010_API VL53L0_Error VL53L010_StaticInit(VL53L0_DEV Dev); - -/** - * @brief Wait for device booted after chip enable (hardware standby) - * This function can be run only when VL53L0_State is VL53L0_STATE_POWERDOWN. - * - * @note This function is not Implemented - * - * @param Dev Device Handle - * @return VL53L0_ERROR_NOT_IMPLEMENTED Not implemented - * - */ -VL53L010_API VL53L0_Error VL53L010_WaitDeviceBooted(VL53L0_DEV Dev); - -/** - * @brief Do an hard reset or soft reset (depending on implementation) of the - * device \n - * After call of this function, device must be in same state as right after a - * power-up sequence. - * This function will change the VL53L0_State to VL53L0_STATE_POWERDOWN. - * - * @note This function Access to the device - * - * @param Dev Device Handle - * @return VL53L0_ERROR_NONE Success - * @return "Other error code" See ::VL53L0_Error - */ -VL53L010_API VL53L0_Error VL53L010_ResetDevice(VL53L0_DEV Dev); - -/** @} VL53L010_init_group */ - - -/** @defgroup VL53L010_parameters_group VL53L010 Parameters Functions - * @brief VL53L010 Functions used to prepare and setup the device - * @{ - */ - -/** - * @brief Prepare device for operation - * @par Function Description - * Update device with provided parameters - * @li Then start ranging operation. - * - * @note This function Access to the device - * - * @param Dev Device Handle - * @param pDeviceParameters Pointer to store current device parameters. - * @return VL53L0_ERROR_NONE Success - * @return "Other error code" See ::VL53L0_Error - */ -VL53L010_API VL53L0_Error VL53L010_SetDeviceParameters(VL53L0_DEV Dev, - const VL53L0_DeviceParameters_t *pDeviceParameters); - -/** - * @brief Retrieve current device parameters - * @par Function Description - * Get actual parameters of the device - * @li Then start ranging operation. - * - * @note This function Access to the device - * - * @param Dev Device Handle - * @param pDeviceParameters Pointer to store current device parameters. - * @return VL53L0_ERROR_NONE Success - * @return "Other error code" See ::VL53L0_Error - */ -VL53L010_API VL53L0_Error VL53L010_GetDeviceParameters(VL53L0_DEV Dev, - VL53L0_DeviceParameters_t *pDeviceParameters); - -/** - * @brief Set a new device mode - * @par Function Description - * Set device to a new mode (ranging, histogram ...) - * - * @note This function doesn't Access to the device - * - * @param Dev Device Handle - * @param DeviceMode New device mode to apply - * Valid values are: - * VL53L0_DEVICEMODE_SINGLE_RANGING - * VL53L0_DEVICEMODE_CONTINUOUS_RANGING - * VL53L0_DEVICEMODE_CONTINUOUS_TIMED_RANGING - * VL53L0_DEVICEMODE_SINGLE_HISTOGRAM - * (functionality not available) - * - * @return VL53L0_ERROR_NONE Success - * @return VL53L0_ERROR_MODE_NOT_SUPPORTED This error occurs when - * DeviceMode is not in the supported list - */ -VL53L010_API VL53L0_Error VL53L010_SetDeviceMode(VL53L0_DEV Dev, - VL53L0_DeviceModes DeviceMode); - -/** - * @brief Get current new device mode - * @par Function Description - * Get actual mode of the device(ranging, histogram ...) - * - * @note This function doesn't Access to the device - * - * @param Dev Device Handle - * @param pDeviceMode Pointer to current apply mode value - * Valid values are: - * VL53L0_DEVICEMODE_SINGLE_RANGING - * VL53L0_DEVICEMODE_CONTINUOUS_RANGING - * VL53L0_DEVICEMODE_CONTINUOUS_TIMED_RANGING - * VL53L0_DEVICEMODE_SINGLE_HISTOGRAM - * (functionality not available) - * - * @return VL53L0_ERROR_NONE Success - * @return VL53L0_ERROR_MODE_NOT_SUPPORTED This error occurs when - * DeviceMode is not in the supported list - */ -VL53L010_API VL53L0_Error VL53L010_GetDeviceMode(VL53L0_DEV Dev, - VL53L0_DeviceModes *pDeviceMode); - -/** - * @brief Set a new Histogram mode - * @par Function Description - * Set device to a new Histogram mode - * - * @note This function doesn't Access to the device - * - * @param Dev Device Handle - * @param HistogramMode New device mode to apply - * Valid values are: - * VL53L0_HISTOGRAMMODE_DISABLED - * @return VL53L0_ERROR_NONE Success - * @return VL53L0_ERROR_MODE_NOT_SUPPORTED This error occurs when - * HistogramMode is not in the supported list - * @return "Other error code" See ::VL53L0_Error - */ -VL53L010_API VL53L0_Error VL53L010_SetHistogramMode(VL53L0_DEV Dev, - VL53L0_HistogramModes HistogramMode); - -/** - * @brief Get current new device mode - * @par Function Description - * Get current Histogram mode of a Device - * - * @note This function doesn't Access to the device - * - * @param Dev Device Handle - * @param pHistogramMode Pointer to current Histogram Mode value - * Valid values are: - * VL53L0_HISTOGRAMMODE_DISABLED - * @return VL53L0_ERROR_NONE Success - * @return "Other error code" See ::VL53L0_Error - */ -VL53L010_API VL53L0_Error VL53L010_GetHistogramMode(VL53L0_DEV Dev, - VL53L0_HistogramModes *pHistogramMode); - -/** - * @brief Set Ranging Timing Budget in microseconds - * - * @par Function Description - * Defines the maximum time allowed by the user to the device to run a full - * ranging sequence - * for the current mode (ranging, histogram, ASL ...) - * - * @note This function Access to the device - * - * @param Dev Device Handle - * @param MeasurementTimingBudgetMicroSeconds Max measurement time in - * microseconds. - * Valid values are: - * >= 17000 microseconds when - * wraparound is enabled - * >= 12000 microseconds when - * wraparound is disabled - * @return VL53L0_ERROR_NONE Success - * @return VL53L0_ERROR_INVALID_PARAMS This error is returned if - * MeasurementTimingBudgetMicroSeconds is out of range - * @return "Other error code" See ::VL53L0_Error - */ -VL53L010_API VL53L0_Error VL53L010_SetMeasurementTimingBudgetMicroSeconds( - VL53L0_DEV Dev, - uint32_t MeasurementTimingBudgetMicroSeconds); - -/** - * @brief Get Ranging Timing Budget in microseconds - * - * @par Function Description - * Returns the programmed the maximum time allowed by the user to the device to - * run a full ranging sequence - * for the current mode (ranging, histogram, ASL ...) - * - * @note This function Access to the device - * - * @param Dev Device Handle - * @param pMeasurementTimingBudgetMicroSeconds Max measurement time in - * microseconds. - * Valid values are: - * >= 17000 microseconds when - * wraparound is enabled - * >= 12000 microseconds when - * wraparound is disabled - * @return VL53L0_ERROR_NONE Success - * @return "Other error code" See ::VL53L0_Error - */ -VL53L010_API VL53L0_Error VL53L010_GetMeasurementTimingBudgetMicroSeconds( - VL53L0_DEV Dev, - uint32_t *pMeasurementTimingBudgetMicroSeconds); - -/** - * Program continuous mode Inter-Measurement period in milliseconds - * - * @par Function Description - * When trying to set too short time return INVALID_PARAMS minimal value - * - * @note This function Access to the device - * - * @param Dev Device Handle - * @param InterMeasurementPeriodMilliSeconds Requires Inter-Measurement - * Period in milliseconds. - * @return VL53L0_ERROR_NONE Success - * @return "Other error code" See ::VL53L0_Error - */ -VL53L010_API VL53L0_Error VL53L010_SetInterMeasurementPeriodMilliSeconds( - VL53L0_DEV Dev, - uint32_t InterMeasurementPeriodMilliSeconds); - -/** - * Get continuous mode Inter-Measurement period in milliseconds - * - * @par Function Description - * When trying to set too short time return INVALID_PARAMS minimal value - * - * @note This function Access to the device - * - * @param Dev Device Handle - * @param pInterMeasurementPeriodMilliSeconds Pointer to programmed - * Inter-Measurement Period in milliseconds. - * @return VL53L0_ERROR_NONE Success - * @return "Other error code" See ::VL53L0_Error - */ -VL53L010_API VL53L0_Error VL53L010_GetInterMeasurementPeriodMilliSeconds( - VL53L0_DEV Dev, - uint32_t *pInterMeasurementPeriodMilliSeconds); - -/** - * @brief Enable/Disable Cross talk compensation feature - * - * @note This function Access to the device - * - * @param Dev Device Handle - * @param XTalkCompensationEnable Cross talk compensation to be set - * 0=disabled else = enabled - * @return VL53L0_ERROR_NONE Success - * @return "Other error code" See ::VL53L0_Error - */ -VL53L010_API VL53L0_Error VL53L010_SetXTalkCompensationEnable( - VL53L0_DEV Dev, uint8_t XTalkCompensationEnable); - -/** - * @brief Get Cross talk compensation rate - * - * @note This function Access to the device - * - * @param Dev Device Handle - * @param pXTalkCompensationEnable Pointer to the Cross talk compensation - * state 0=disabled or 1 = enabled - * @return VL53L0_ERROR_NONE Success - * @return "Other error code" See ::VL53L0_Error - */ -VL53L010_API VL53L0_Error VL53L010_GetXTalkCompensationEnable( - VL53L0_DEV Dev, uint8_t *pXTalkCompensationEnable); - -/** - * @brief Set Cross talk compensation rate - * - * @par Function Description - * Set Cross talk compensation rate. - * - * @note This function Access to the device - * - * @param Dev Device Handle - * @param XTalkCompensationRateMegaCps Compensation rate in Mega counts per - * second (16.16 fix point) see datasheet for details - * @return VL53L0_ERROR_NONE Success - * @return "Other error code" See ::VL53L0_Error - */ -VL53L010_API VL53L0_Error VL53L010_SetXTalkCompensationRateMegaCps( - VL53L0_DEV Dev, - FixPoint1616_t XTalkCompensationRateMegaCps); - -/** - * @brief Get Cross talk compensation rate - * - * @par Function Description - * Get Cross talk compensation rate. - * - * @note This function Access to the device - * - * @param Dev Device Handle - * @param pXTalkCompensationRateMegaCps Pointer to Compensation rate in Mega - * counts per second (16.16 fix point) see datasheet for details - * @return VL53L0_ERROR_NONE Success - * @return "Other error code" See ::VL53L0_Error - */ -VL53L010_API VL53L0_Error VL53L010_GetXTalkCompensationRateMegaCps( - VL53L0_DEV Dev, - FixPoint1616_t *pXTalkCompensationRateMegaCps); - - -/** - * @brief Get the number of the check limit managed by a given Device - * - * @par Function Description - * This function give the number of the check limit managed by the Device - * - * @note This function doesn't Access to the device - * - * @param pNumberOfLimitCheck Pointer to the number of check limit. - * @return VL53L0_ERROR_NONE Success - * @return "Other error code" See ::VL53L0_Error - */ -VL53L010_API VL53L0_Error VL53L010_GetNumberOfLimitCheck( - uint16_t *pNumberOfLimitCheck); - -/** - * @brief Return a description string for a given limit check number - * - * @par Function Description - * This function returns a description string for a given limit check number. - * The limit check is identified with the LimitCheckId. - * - * @note This function doesn't Access to the device - * - * @param Dev Device Handle - * @param LimitCheckId Limit Check ID (0<= LimitCheckId < - * VL53L0_GetNumberOfLimitCheck() ). - * @param pLimitCheckString Pointer to the description string of - * the given check limit. - * @return VL53L0_ERROR_NONE Success - * @return VL53L0_ERROR_INVALID_PARAMS This error is returned when - * LimitCheckId value is out of range. - * @return "Other error code" See ::VL53L0_Error - */ -VL53L010_API VL53L0_Error VL53L010_GetLimitCheckInfo(VL53L0_DEV Dev, - uint16_t LimitCheckId, char *pLimitCheckString); - - -/** - * @brief Enable/Disable a specific limit check - * - * @par Function Description - * This function Enable/Disable a specific limit check. - * The limit check is identified with the LimitCheckId. - * - * @note This function doesn't Access to the device - * - * @param Dev Device Handle - * @param LimitCheckId Limit Check ID (0<= LimitCheckId < - * VL53L0_GetNumberOfLimitCheck() ). - * @param LimitCheckEnable if 1 the check limit corresponding to - * LimitCheckId is Enabled - * if 0 the check limit corresponding to - * LimitCheckId is disabled - * @return VL53L0_ERROR_NONE Success - * @return VL53L0_ERROR_INVALID_PARAMS This error is returned when - * LimitCheckId value is out of range. - * @return "Other error code" See ::VL53L0_Error - */ -VL53L010_API VL53L0_Error VL53L010_SetLimitCheckEnable(VL53L0_DEV Dev, - uint16_t LimitCheckId, uint8_t LimitCheckEnable); - - -/** - * @brief Get specific limit check enable state - * - * @par Function Description - * This function get the enable state of a specific limit check. - * The limit check is identified with the LimitCheckId. - * - * @note This function Access to the device - * - * @param Dev Device Handle - * @param LimitCheckId Limit Check ID (0<= LimitCheckId < - * VL53L0_GetNumberOfLimitCheck() ). - * @param pLimitCheckEnable Pointer to the check limit enable - * value. - * if 1 the check limit corresponding to - * LimitCheckId is Enabled - * if 0 the check limit corresponding to - * LimitCheckId is disabled - * @return VL53L0_ERROR_NONE Success - * @return VL53L0_ERROR_INVALID_PARAMS This error is returned when - * LimitCheckId value is out of range. - * @return "Other error code" See ::VL53L0_Error - */ -VL53L010_API VL53L0_Error VL53L010_GetLimitCheckEnable(VL53L0_DEV Dev, - uint16_t LimitCheckId, uint8_t *pLimitCheckEnable); - -/** - * @brief Set a specific limit check value - * - * @par Function Description - * This function set a specific limit check value. - * The limit check is identified with the LimitCheckId. - * - * @note This function Access to the device - * - * @param Dev Device Handle - * @param LimitCheckId Limit Check ID (0<= LimitCheckId < - * VL53L0_GetNumberOfLimitCheck() ). - * @param LimitCheckValue Limit check Value for a given - * LimitCheckId - * @return VL53L0_ERROR_NONE Success - * @return VL53L0_ERROR_INVALID_PARAMS This error is returned when either - * LimitCheckId or LimitCheckValue value is out of range. - * @return "Other error code" See ::VL53L0_Error - */ -VL53L010_API VL53L0_Error VL53L010_SetLimitCheckValue(VL53L0_DEV Dev, - uint16_t LimitCheckId, FixPoint1616_t LimitCheckValue); - -/** - * @brief Get a specific limit check value - * - * @par Function Description - * This function get a specific limit check value from device then it updates - * internal values and check enables. - * The limit check is identified with the LimitCheckId. - * - * @note This function Access to the device - * - * @param Dev Device Handle - * @param LimitCheckId Limit Check ID (0<= LimitCheckId < - * VL53L0_GetNumberOfLimitCheck() ). - * @param pLimitCheckValue Pointer to Limit check Value for a - * given LimitCheckId. - * @return VL53L0_ERROR_NONE Success - * @return VL53L0_ERROR_INVALID_PARAMS This error is returned when - * LimitCheckId value is out of range. - * @return "Other error code" See ::VL53L0_Error - */ -VL53L010_API VL53L0_Error VL53L010_GetLimitCheckValue(VL53L0_DEV Dev, - uint16_t LimitCheckId, - FixPoint1616_t *pLimitCheckValue); - - -/** - * @brief Get the current value of the signal used for the limit check - * - * @par Function Description - * This function get a the current value of the signal used for the limit check. - * To obtain the latest value you should run a ranging before. - * The value reported is linked to the limit check identified with the - * LimitCheckId. - * - * @note This function Access to the device - * - * @param Dev Device Handle - * @param LimitCheckId Limit Check ID (0<= LimitCheckId < - * VL53L0_GetNumberOfLimitCheck() ). - * @param pLimitCheckCurrent Pointer to current Value for a - * given LimitCheckId. - * @return VL53L0_ERROR_NONE Success - * @return VL53L0_ERROR_INVALID_PARAMS This error is returned when - * LimitCheckId value is out of range. - * @return "Other error code" See ::VL53L0_Error - */ -VL53L010_API VL53L0_Error VL53L010_GetLimitCheckCurrent(VL53L0_DEV Dev, - uint16_t LimitCheckId, FixPoint1616_t *pLimitCheckCurrent); - -/** - * @brief Enable (or disable) Wrap around Check - * - * @note This function Access to the device - * - * @param Dev Device Handle - * @param WrapAroundCheckEnable Wrap around Check to be set 0=disabled, - * other = enabled - * @return VL53L0_ERROR_NONE Success - * @return "Other error code" See ::VL53L0_Error - */ -VL53L010_API VL53L0_Error VL53L010_SetWrapAroundCheckEnable(VL53L0_DEV Dev, - uint8_t WrapAroundCheckEnable); - -/** - * @brief Get setup of Wrap around Check - * - * @par Function Description - * This function get the wrapAround check enable parameters - * - * @note This function Access to the device - * - * @param Dev Device Handle - * @param pWrapAroundCheckEnable Pointer to the Wrap around Check state - * 0=disabled or 1 = enabled - * @return VL53L0_ERROR_NONE Success - * @return "Other error code" See ::VL53L0_Error - */ -VL53L010_API VL53L0_Error VL53L010_GetWrapAroundCheckEnable(VL53L0_DEV Dev, - uint8_t *pWrapAroundCheckEnable); - -/** @} VL53L010_parameters_group */ - - -/** @defgroup VL53L010_measurement_group VL53L010 Measurement Functions - * @brief VL53L010 Functions used for the measurements - * @{ - */ - -/** - * @brief Single shot measurement. - * - * @par Function Description - * Perform simple measurement sequence (Start measure, Wait measure to end, and - * returns when measurement is done). - * Once function returns, user can get valid data by calling - * VL53L0_GetRangingMeasurement or VL53L0_GetHistogramMeasurement depending on - * defined measurement mode - * User should Clear the interrupt in case this are enabled by using the - * function VL53L0_ClearInterruptMask(). - * - * @warning This function is a blocking function - * - * @note This function Access to the device - * - * @param Dev Device Handle - * @return VL53L0_ERROR_NONE Success - * @return "Other error code" See ::VL53L0_Error - */ -VL53L010_API VL53L0_Error VL53L010_PerformSingleMeasurement(VL53L0_DEV Dev); - -/** - * @brief Perform Reference Calibration - * - * @details Perform a reference calibration of the Device. - * This function should be run from time to time before doing a ranging - * measurement. - * This function will launch a special ranging measurement, so if interrupt are - * enable an interrupt will be done. - * This function will clear the interrupt generated automatically. - * - * @warning This function is a blocking function - * - * @note This function Access to the device - * - * @param Dev Device Handle - * @return VL53L0_ERROR_NONE Success - * @return "Other error code" See ::VL53L0_Error - */ -VL53L010_API VL53L0_Error VL53L010_PerformRefCalibration(VL53L0_DEV Dev); - -/** - * @brief Perform XTalk Calibration - * - * @details Perform a XTalk calibration of the Device. - * This function will launch a ranging measurement, if interrupts are enabled - * an interrupt will be done. - * This function will clear the interrupt generated automatically. - * This function will program a new value for the XTalk compensation and it - * will enable the cross talk before exit. - * - * @warning This function is a blocking function - * - * @note This function Access to the device - * - * @note This function change the device mode to - * VL53L0_DEVICEMODE_SINGLE_RANGING - * - * @param Dev Device Handle - * @param XTalkCalDistance XTalkCalDistance value used for the - * XTalk computation. - * @param pXTalkCompensationRateMegaCps Pointer to new XTalkCompensation - * value. - * @return VL53L0_ERROR_NONE Success - * @return "Other error code" See ::VL53L0_Error - */ -VL53L010_API VL53L0_Error VL53L010_PerformXTalkCalibration(VL53L0_DEV Dev, - FixPoint1616_t XTalkCalDistance, - FixPoint1616_t *pXTalkCompensationRateMegaCps); - -/** - * @brief Perform Offset Calibration - * - * @details Perform a Offset calibration of the Device. - * This function will launch a ranging measurement, if interrupts are - * enabled an interrupt will be done. - * This function will clear the interrupt generated automatically. - * This function will program a new value for the Offset calibration value and - * - * @warning This function is a blocking function - * - * @note This function Access to the device - * - * @note This function does not change the device mode. - * - * @param Dev Device Handle - * @param CalDistanceMilliMeter Calibration distance value used for the - * offset compensation. - * @param pOffsetMicroMeter Pointer to new Offset value computed by the - * function. - * - * @return VL53L0_ERROR_NONE Success - * @return "Other error code" See ::VL53L0_Error - */ -VL53L010_API VL53L0_Error VL53L010_PerformOffsetCalibration(VL53L0_DEV Dev, - FixPoint1616_t CalDistanceMilliMeter, int32_t* pOffsetMicroMeter); - -/** - * @brief Start device measurement - * - * @details Started measurement will depend on device parameters set through @a - * VL53L0_SetParameters() - * This is a non-blocking function - * This function will change the VL53L0_State from VL53L0_STATE_IDLE to - * VL53L0_STATE_RUNNING. - * - * @note This function Access to the device - * - - * @param Dev Device Handle - * @return VL53L0_ERROR_NONE Success - * @return VL53L0_ERROR_MODE_NOT_SUPPORTED This error occurs when - * DeviceMode programmed with @a VL53L0_SetDeviceMode is not in the supported - * list: - * Supported mode are: - * VL53L0_DEVICEMODE_SINGLE_RANGING, VL53L0_DEVICEMODE_CONTINUOUS_RANGING, - * VL53L0_DEVICEMODE_CONTINUOUS_TIMED_RANGING - * @return VL53L0_ERROR_TIME_OUT Time out on start measurement - * @return "Other error code" See ::VL53L0_Error - */ -VL53L010_API VL53L0_Error VL53L010_StartMeasurement(VL53L0_DEV Dev); - -/** - * @brief Stop device measurement - * - * @details Will set the device in standby mode at end of current measurement \n - * Not necessary in single mode as device shall return automatically - * in standby mode at end of measurement. - * This function will change the VL53L0_State from - * VL53L0_STATE_RUNNING to VL53L0_STATE_IDLE. - * - * @note This function Access to the device - * - * @param Dev Device Handle - * @return VL53L0_ERROR_NONE Success - * @return "Other error code" See ::VL53L0_Error - */ -VL53L010_API VL53L0_Error VL53L010_StopMeasurement(VL53L0_DEV Dev); - -/** - * @brief Return Measurement Data Ready - * - * @par Function Description - * This function indicate that a measurement data is ready. - * This function check if interrupt mode is used then check is done accordingly. - * If perform function clear the interrupt, this function will not work, like - * in case of @a VL53L0_PerformSingleRangingMeasurement(). - * The previous function is blocking function, VL53L0_GetMeasurementDataReady - * is used for non-blocking capture. - * - * @note This function Access to the device - * - * @param Dev Device Handle - * @param pMeasurementDataReady Pointer to Measurement Data Ready. 0=data - * not ready, 1 = data ready - * @return VL53L0_ERROR_NONE Success - * @return "Other error code" See ::VL53L0_Error - */ -VL53L010_API VL53L0_Error VL53L010_GetMeasurementDataReady(VL53L0_DEV Dev, - uint8_t *pMeasurementDataReady); - -/** - * @brief Wait for device ready for a new measurement command. Blocking - * function. - * - * @note This function is not Implemented - * - * @param Dev Device Handle - * @param MaxLoop Max Number of polling loop (timeout). - * @return VL53L0_ERROR_NOT_IMPLEMENTED Not implemented - */ -VL53L010_API VL53L0_Error VL53L010_WaitDeviceReadyForNewMeasurement(VL53L0_DEV Dev, - uint32_t MaxLoop); - - -/** - * @brief Retrieve the measurements from device for a given setup - * - * @par Function Description - * Get data from last successful Ranging measurement - * @warning USER should take care about @a VL53L0_GetNumberOfROIZones() before - * get data. - * PAL will fill a NumberOfROIZones times the corresponding data structure used - * in the measurement function. - * - * @note This function Access to the device - * - * @param Dev Device Handle - * @param pRangingMeasurementData Pointer to the data structure to fill up. - * @return VL53L0_ERROR_NONE Success - * @return "Other error code" See ::VL53L0_Error - */ -VL53L010_API VL53L0_Error VL53L010_GetRangingMeasurementData(VL53L0_DEV Dev, - VL53L0_RangingMeasurementData_t *pRangingMeasurementData); - -/** - * @brief Retrieve the measurements from device for a given setup - * - * @par Function Description - * Get data from last successful Histogram measurement - * @warning USER should take care about @a VL53L0_GetNumberOfROIZones() before - * get data. - * PAL will fill a NumberOfROIZones times the corresponding data structure used - * in the measurement function. - * @note This function is not Implemented - * @param Dev Device Handle - * @param pHistogramMeasurementData Pointer to the data structure to fill - * up. - * @return VL53L0_ERROR_NOT_IMPLEMENTED Not implemented - */ -VL53L010_API VL53L0_Error VL53L010_GetHistogramMeasurementData(VL53L0_DEV Dev, - VL53L0_HistogramMeasurementData_t *pHistogramMeasurementData); - -/** - * @brief Performs a single ranging measurement and retrieve the ranging - * measurement data - * - * @par Function Description - * This function will change the device mode to - * VL53L0_DEVICEMODE_SINGLE_RANGING with @a VL53L0_SetDeviceMode(), - * It performs measurement with @a VL53L0_PerformSingleMeasurement() - * It get data from last successful Ranging measurement with @a - * VL53L0_GetRangingMeasurementData. - * Finally it clear the interrupt with @a VL53L0_ClearInterruptMask(). - * @note This function Access to the device - * - * @note This function change the device mode to - * VL53L0_DEVICEMODE_SINGLE_RANGING - * - * @param Dev Device Handle - * @param pRangingMeasurementData Pointer to the data structure to fill up. - * @return VL53L0_ERROR_NONE Success - * @return "Other error code" See ::VL53L0_Error - */ -VL53L010_API VL53L0_Error VL53L010_PerformSingleRangingMeasurement(VL53L0_DEV Dev, - VL53L0_RangingMeasurementData_t *pRangingMeasurementData); - -/** - * @brief Performs a single histogram measurement and retrieve the histogram - * measurement data - * Is equivalent to VL53L0_PerformSingleMeasurement + - * VL53L0_GetHistogramMeasurementData - * - * @par Function Description - * Get data from last successful Ranging measurement. - * This function will clear the interrupt in case of these are enabled. - * - * @note This function is not Implemented - * - * @param Dev Device Handle - * @param pHistogramMeasurementData Pointer to the data structure to fill - * up. - * @return VL53L0_ERROR_NOT_IMPLEMENTED Not implemented - */ -VL53L010_API VL53L0_Error VL53L010_PerformSingleHistogramMeasurement( - VL53L0_DEV Dev, - VL53L0_HistogramMeasurementData_t *pHistogramMeasurementData); - - - -/** - * @brief Set the number of ROI Zones to be used for a specific Device - * - * @par Function Description - * Set the number of ROI Zones to be used for a specific Device. - * The programmed value should be less than the max number of ROI Zones given - * with @a VL53L0_GetMaxNumberOfROIZones(). - * This version of API manage only one zone. - * - * @param Dev Device Handle - * @param NumberOfROIZones Number of ROI Zones to be used for a - * specific Device. - * @return VL53L0_ERROR_NONE Success - * @return VL53L0_ERROR_INVALID_PARAMS This error is returned if - * NumberOfROIZones != 1 - */ -VL53L010_API VL53L0_Error VL53L010_SetNumberOfROIZones(VL53L0_DEV Dev, - uint8_t NumberOfROIZones); - -/** - * @brief Get the number of ROI Zones managed by the Device - * - * @par Function Description - * Get number of ROI Zones managed by the Device - * USER should take care about @a VL53L0_GetNumberOfROIZones() before get data - * after a perform measurement. - * PAL will fill a NumberOfROIZones times the corresponding data structure used - * in the measurement function. - * - * @note This function doesn't Access to the device - * - * @param Dev Device Handle - * @param pNumberOfROIZones Pointer to the Number of ROI Zones - * value. - * @return VL53L0_ERROR_NONE Success - */ -VL53L010_API VL53L0_Error VL53L010_GetNumberOfROIZones(VL53L0_DEV Dev, - uint8_t *pNumberOfROIZones); - -/** - * @brief Get the Maximum number of ROI Zones managed by the Device - * - * @par Function Description - * Get Maximum number of ROI Zones managed by the Device. - * - * @note This function doesn't Access to the device - * - * @param Dev Device Handle - * @param pMaxNumberOfROIZones Pointer to the Maximum Number of ROI - * Zones value. - * @return VL53L0_ERROR_NONE Success - */ -VL53L010_API VL53L0_Error VL53L010_GetMaxNumberOfROIZones(VL53L0_DEV Dev, - uint8_t *pMaxNumberOfROIZones); - - -/** @} VL53L010_measurement_group */ - - -/** @defgroup VL53L010_interrupt_group VL53L010 Interrupt Functions - * @brief VL53L010 Functions used for interrupt managements - * @{ - */ - -/** - * @brief Set the configuration of GPIO pin for a given device - * - * @note This function Access to the device - * - * @param Dev Device Handle - * @param Pin ID of the GPIO Pin - * @param Functionality Select Pin functionality. Refer to - * ::VL53L0_GpioFunctionality - * @param DeviceMode Device Mode associated to the Gpio. - * @param Polarity Set interrupt polarity. Active high or active - * low see ::VL53L0_InterruptPolarity - * @return VL53L0_ERROR_NONE Success - * @return VL53L0_ERROR_GPIO_NOT_EXISTING Only Pin=0 is - * accepted. - * @return VL53L0_ERROR_GPIO_FUNCTIONALITY_NOT_SUPPORTED This error occurs - * when Functionality programmed is not in the supported list: - * Supported value - * are: - * - * VL53L0_GPIOFUNCTIONALITY_OFF, VL53L0_GPIOFUNCTIONALITY_THRESHOLD_CROSSED_LOW, - * VL53L0_GPIOFUNCTIONALITY_THRESHOLD_CROSSED_HIGH, - * VL53L0_GPIOFUNCTIONALITY_THRESHOLD_CROSSED_OUT, - * - * VL53L0_GPIOFUNCTIONALITY_NEW_MEASURE_READY - * @return "Other error code" See ::VL53L0_Error - */ -VL53L010_API VL53L0_Error VL53L010_SetGpioConfig(VL53L0_DEV Dev, uint8_t Pin, - VL53L0_DeviceModes DeviceMode, - VL53L0_GpioFunctionality Functionality, - VL53L0_InterruptPolarity Polarity); - - -/** - * @brief Get current configuration for GPIO pin for a given device - * - * @note This function Access to the device - * - * @param Dev Device Handle - * @param Pin ID of the GPIO Pin - * @param pDeviceMode Pointer to Device Mode associated to the Gpio. - * @param pFunctionality Pointer to Pin functionality. - * Refer to ::VL53L0_GpioFunctionality - * @param pPolarity Pointer to interrupt polarity. Active high or - * active low see ::VL53L0_InterruptPolarity - * @return VL53L0_ERROR_NONE Success - * @return VL53L0_ERROR_GPIO_NOT_EXISTING Only Pin=0 is accepted. - * @return VL53L0_ERROR_GPIO_FUNCTIONALITY_NOT_SUPPORTED This error occurs - * when Functionality programmed is not in the supported list: - * Supported value are: - * VL53L0_GPIOFUNCTIONALITY_OFF, VL53L0_GPIOFUNCTIONALITY_THRESHOLD_CROSSED_LOW, - * VL53L0_GPIOFUNCTIONALITY_THRESHOLD_CROSSED_HIGH, - * VL53L0_GPIOFUNCTIONALITY_THRESHOLD_CROSSED_OUT, - * VL53L0_GPIOFUNCTIONALITY_NEW_MEASURE_READY - * @return "Other error code" See ::VL53L0_Error - */ -VL53L010_API VL53L0_Error VL53L010_GetGpioConfig(VL53L0_DEV Dev, uint8_t Pin, - VL53L0_DeviceModes *pDeviceMode, - VL53L0_GpioFunctionality *pFunctionality, - VL53L0_InterruptPolarity *pPolarity); - -/** - * @brief Set low and high Interrupt thresholds for a given mode (ranging, ALS, - * ...) for a given device - * - * @par Function Description - * Set low and high Interrupt thresholds for a given mode (ranging, ALS, ...) - * for a given device - * - * @note This function Access to the device - * - * @note DeviceMode is ignored for the current device - * - * @param Dev Device Handle - * @param DeviceMode Device Mode for which change thresholds - * @param ThresholdLow Low threshold (mm, lux ..., depending on the - * mode) - * @param ThresholdHigh High threshold (mm, lux ..., depending on the - * mode) - * @return VL53L0_ERROR_NONE Success - * @return "Other error code" See ::VL53L0_Error - */ -VL53L010_API VL53L0_Error VL53L010_SetInterruptThresholds(VL53L0_DEV Dev, - VL53L0_DeviceModes DeviceMode, - FixPoint1616_t ThresholdLow, - FixPoint1616_t ThresholdHigh); - -/** - * @brief Get high and low Interrupt thresholds for a given mode (ranging, - * ALS, ...) for a given device - * - * @par Function Description - * Get high and low Interrupt thresholds for a given mode (ranging, ALS, ...) - * for a given device - * - * @note This function Access to the device - * - * @note DeviceMode is ignored for the current device - * - * @param Dev Device Handle - * @param DeviceMode Device Mode from which read thresholds - * @param pThresholdLow Low threshold (mm, lux ..., depending on the - * mode) - * @param pThresholdHigh High threshold (mm, lux ..., depending on the - * mode) - * @return VL53L0_ERROR_NONE Success - * @return "Other error code" See ::VL53L0_Error - */ -VL53L010_API VL53L0_Error VL53L010_GetInterruptThresholds(VL53L0_DEV Dev, - VL53L0_DeviceModes DeviceMode, - FixPoint1616_t *pThresholdLow, - FixPoint1616_t *pThresholdHigh); - -/** - * @brief Clear given system interrupt condition - * - * @par Function Description - * Clear given interrupt(s). - * - * @note This function Access to the device - * - * @param Dev Device Handle - * @param InterruptMask Mask of interrupts to clear - * @return VL53L0_ERROR_NONE Success - * @return "Other error code" See ::VL53L0_Error - */ -VL53L010_API VL53L0_Error VL53L010_ClearInterruptMask(VL53L0_DEV Dev, - uint32_t InterruptMask); - -/** - * @brief Return device interrupt status - * - * @par Function Description - * Returns currently raised interrupts by the device. - * User shall be able to activate/deactivate interrupts through - * @a VL53L0_SetGpioConfig() - * - * @note This function Access to the device - * - * @param Dev Device Handle - * @param pInterruptMaskStatus Pointer to status variable to update - * @return VL53L0_ERROR_NONE Success - * @return "Other error code" See ::VL53L0_Error - */ -VL53L010_API VL53L0_Error VL53L010_GetInterruptMaskStatus(VL53L0_DEV Dev, - uint32_t *pInterruptMaskStatus); - - -/** - * @brief Configure ranging interrupt reported to system - * - * @note This function is not Implemented - * - * @param Dev Device Handle - * @param InterruptMask Mask of interrupt to Enable/disable - * (0:interrupt disabled or 1: interrupt enabled) - * @return VL53L0_ERROR_NOT_IMPLEMENTED Not implemented - */ -VL53L010_API VL53L0_Error VL53L010_EnableInterruptMask(VL53L0_DEV Dev, - uint32_t InterruptMask); - - -/** @} VL53L010_interrupt_group */ - - -/** @defgroup VL53L010_SPADfunctions_group VL53L010 SPAD Functions - * @brief VL53L010 Functions used for SPAD managements - * @{ - */ - - - -/** - * @brief Set the SPAD Ambient Damper Threshold value - * - * @par Function Description - * This function set the SPAD Ambient Damper Threshold value - * - * @note This function Access to the device - * - * @param Dev Device Handle - * @param SpadAmbientDamperThreshold SPAD Ambient Damper Threshold value - * @return VL53L0_ERROR_NONE Success - * @return "Other error code" See ::VL53L0_Error - */ -VL53L010_API VL53L0_Error VL53L010_SetSpadAmbientDamperThreshold(VL53L0_DEV Dev, - uint16_t SpadAmbientDamperThreshold); - -/** - * @brief Get the current SPAD Ambient Damper Threshold value - * - * @par Function Description - * This function get the SPAD Ambient Damper Threshold value - * - * @note This function Access to the device - * - * @param Dev Device Handle - * @param pSpadAmbientDamperThreshold Pointer to programmed SPAD Ambient - * Damper Threshold value - * @return VL53L0_ERROR_NONE Success - * @return "Other error code" See ::VL53L0_Error - */ -VL53L010_API VL53L0_Error VL53L010_GetSpadAmbientDamperThreshold(VL53L0_DEV Dev, - uint16_t *pSpadAmbientDamperThreshold); - - -/** - * @brief Set the SPAD Ambient Damper Factor value - * - * @par Function Description - * This function set the SPAD Ambient Damper Factor value - * - * @note This function Access to the device - * - * @param Dev Device Handle - * @param SpadAmbientDamperFactor SPAD Ambient Damper Factor value - * @return VL53L0_ERROR_NONE Success - * @return "Other error code" See ::VL53L0_Error - */ -VL53L010_API VL53L0_Error VL53L010_SetSpadAmbientDamperFactor(VL53L0_DEV Dev, - uint16_t SpadAmbientDamperFactor); - -/** - * @brief Get the current SPAD Ambient Damper Factor value - * - * @par Function Description - * This function get the SPAD Ambient Damper Factor value - * - * @note This function Access to the device - * - * @param Dev Device Handle - * @param pSpadAmbientDamperFactor Pointer to programmed SPAD Ambient - * Damper Factor value - * @return VL53L0_ERROR_NONE Success - * @return "Other error code" See ::VL53L0_Error - */ -VL53L010_API VL53L0_Error VL53L010_GetSpadAmbientDamperFactor(VL53L0_DEV Dev, - uint16_t *pSpadAmbientDamperFactor); - - -/** @} VL53L010_SPADfunctions_group */ - -/** @} VL53L010_cut10_group */ - - - -#ifdef __cplusplus -} -#endif - -#endif /* _VL53L010_API_H_ */ diff --git a/drivers/input/misc/vl53L0/inc/vl53l010_device.h b/drivers/input/misc/vl53L0/inc/vl53l010_device.h deleted file mode 100644 index a94b552c1b44..000000000000 --- a/drivers/input/misc/vl53L0/inc/vl53l010_device.h +++ /dev/null @@ -1,231 +0,0 @@ -/******************************************************************************* -Copyright © 2016, STMicroelectronics International N.V. -All rights reserved. - -Redistribution and use in source and binary forms, with or without -modification, are permitted provided that the following conditions are met: - * Redistributions of source code must retain the above copyright - notice, this list of conditions and the following disclaimer. - * Redistributions in binary form must reproduce the above copyright - notice, this list of conditions and the following disclaimer in the - documentation and/or other materials provided with the distribution. - * Neither the name of STMicroelectronics nor the - names of its contributors may be used to endorse or promote products - derived from this software without specific prior written permission. - -THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND -ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED -WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, AND -NON-INFRINGEMENT OF INTELLECTUAL PROPERTY RIGHTS ARE DISCLAIMED. -IN NO EVENT SHALL STMICROELECTRONICS INTERNATIONAL N.V. BE LIABLE FOR ANY -DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES -(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; -LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND -ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT -(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS -SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. -*******************************************************************************/ - -/** - * Device specific defines. To be adapted by implementer for the targeted - * device. - */ - -#ifndef _VL53L010_DEVICE_H_ -#define _VL53L010_DEVICE_H_ - -#include "vl53l0_types.h" - -/** @defgroup VL53L010_SpecDefines_group VL53L010 cut1.0 Device Specific Defines - * @brief VL53L010 cut1.0 Device Specific Defines - * @{ - */ - - -/** @defgroup VL53L010_DeviceError_group Device Error - * @brief Device Error code - * - * This enum is Device specific it should be updated in the implementation - * Use @a VL53L010_GetStatusErrorString() to get the string. - * It is related to Status Register of the Device. - * @{ - */ -typedef uint8_t VL53L010_DeviceError; - -#define VL53L010_DEVICEERROR_NONE ((VL53L010_DeviceError) 0) -#define VL53L010_DEVICEERROR_VCSELCONTINUITYTESTFAILURE ((VL53L010_DeviceError) 1) -#define VL53L010_DEVICEERROR_VCSELWATCHDOGTESTFAILURE ((VL53L010_DeviceError) 2) -#define VL53L010_DEVICEERROR_NOVHVVALUEFOUND ((VL53L010_DeviceError) 3) -#define VL53L010_DEVICEERROR_MSRCNOTARGET ((VL53L010_DeviceError) 4) -#define VL53L010_DEVICEERROR_MSRCMINIMUMSNR ((VL53L010_DeviceError) 5) -#define VL53L010_DEVICEERROR_MSRCWRAPAROUND ((VL53L010_DeviceError) 6) -#define VL53L010_DEVICEERROR_TCC ((VL53L010_DeviceError) 7) -#define VL53L010_DEVICEERROR_RANGEAWRAPAROUND ((VL53L010_DeviceError) 8) -#define VL53L010_DEVICEERROR_RANGEBWRAPAROUND ((VL53L010_DeviceError) 9) -#define VL53L010_DEVICEERROR_MINCLIP ((VL53L010_DeviceError) 10) -#define VL53L010_DEVICEERROR_RANGECOMPLETE ((VL53L010_DeviceError) 11) -#define VL53L010_DEVICEERROR_ALGOUNDERFLOW ((VL53L010_DeviceError) 12) -#define VL53L010_DEVICEERROR_ALGOOVERFLOW ((VL53L010_DeviceError) 13) -#define VL53L010_DEVICEERROR_FINALSNRLIMIT ((VL53L010_DeviceError) 14) -#define VL53L010_DEVICEERROR_NOTARGETIGNORE ((VL53L010_DeviceError) 15) - -/** @} VL53L010_DeviceError_group */ - -/** @defgroup VL53L010_CheckEnable_group Check Enable list - * @brief Check Enable code - * - * Define used to specify the LimitCheckId. - * Use @a VL53L010_GetLimitCheckInfo() to get the string. - * @{ - */ - -#define VL53L010_CHECKENABLE_SIGMA_FINAL_RANGE 0 -#define VL53L010_CHECKENABLE_SIGNAL_RATE_FINAL_RANGE 1 -#define VL53L010_CHECKENABLE_NUMBER_OF_CHECKS 2 - -/** @} VL53L010_CheckEnable_group */ - - -/** @defgroup VL53L010_GpioFunctionality_group Gpio Functionality - * @brief Defines the different functionalities for the device GPIO(s) - * @{ - */ -typedef uint8_t VL53L010_GpioFunctionality; - -#define VL53L010_GPIOFUNCTIONALITY_OFF ((VL53L010_GpioFunctionality) 0) /*!< NO Interrupt */ -#define VL53L010_GPIOFUNCTIONALITY_THRESHOLD_CROSSED_LOW ((VL53L010_GpioFunctionality) 1) /*!< Level Low (value < thresh_low) */ -#define VL53L010_GPIOFUNCTIONALITY_THRESHOLD_CROSSED_HIGH ((VL53L010_GpioFunctionality) 2) /*!< Level High (value > thresh_high) */ -#define VL53L010_GPIOFUNCTIONALITY_THRESHOLD_CROSSED_OUT ((VL53L010_GpioFunctionality) 3) /*!< Out Of Window (value < thresh_low OR value > thresh_high) */ -#define VL53L010_GPIOFUNCTIONALITY_NEW_MEASURE_READY ((VL53L010_GpioFunctionality) 4) /*!< New Sample Ready */ - -/** @} VL53L010_GpioFunctionality_group */ - - - -/* Device register map */ - -/** @defgroup VL53L010_DefineRegisters_group Define Registers - * @brief List of all the defined registers - * @{ - */ -#define VL53L010_REG_SYSRANGE_START 0x000 - /** mask existing bit in #VL53L010_REG_SYSRANGE_START*/ - #define VL53L010_REG_SYSRANGE_MODE_MASK 0x0F - /** bit 0 in #VL53L010_REG_SYSRANGE_START write 1 toggle state in - continuous mode and arm next shot in single shot mode */ - #define VL53L010_REG_SYSRANGE_MODE_START_STOP 0x01 - /** bit 1 write 0 in #VL53L010_REG_SYSRANGE_START set single shot mode */ - #define VL53L010_REG_SYSRANGE_MODE_SINGLESHOT 0x00 - /** bit 1 write 1 in #VL53L010_REG_SYSRANGE_START set back-to-back - operation mode */ - #define VL53L010_REG_SYSRANGE_MODE_BACKTOBACK 0x02 - /** bit 2 write 1 in #VL53L010_REG_SYSRANGE_START set timed operation - mode */ - #define VL53L010_REG_SYSRANGE_MODE_TIMED 0x04 - /** bit 3 write 1 in #VL53L010_REG_SYSRANGE_START set histogram - operation mode */ - #define VL53L010_REG_SYSRANGE_MODE_HISTOGRAM 0x08 - - -#define VL53L010_REG_SYSTEM_THRESH_HIGH 0x000C /* NOSLC 2 bytes */ -#define VL53L010_REG_SYSTEM_THRESH_LOW 0x000E /* NOSLC 2 bytes */ - - -/* FPGA bitstream */ -#define VL53L010_REG_SYSTEM_SEQUENCE_CONFIG 0x0001 -#define VL53L010_REG_SYSTEM_INTERMEASUREMENT_PERIOD 0x0004 - -#define VL53L010_REG_SYSTEM_REPORT_REQUEST 0x0009 - #define VL53L010_REG_SYSTEM_RANGEA_DATA 0x04 - #define VL53L010_REG_SYSTEM_RANGEB_DATA 0x05 - -#define VL53L010_REG_SYSTEM_INTERRUPT_CONFIG_GPIO 0x000A - #define VL53L010_REG_SYSTEM_INTERRUPT_GPIO_DISABLED 0x00 - #define VL53L010_REG_SYSTEM_INTERRUPT_GPIO_LEVEL_LOW 0x01 - #define VL53L010_REG_SYSTEM_INTERRUPT_GPIO_LEVEL_HIGH 0x02 - #define VL53L010_REG_SYSTEM_INTERRUPT_GPIO_OUT_OF_WINDOW 0x03 - #define VL53L010_REG_SYSTEM_INTERRUPT_GPIO_NEW_SAMPLE_READY 0x04 - -#define VL53L010_REG_GPIO_HV_MUX_ACTIVE_HIGH 0x0084 - -#define VL53L010_REG_SYSTEM_INTERRUPT_CLEAR 0x000B - -/* Result registers */ -#define VL53L010_REG_RESULT_INTERRUPT_STATUS 0x0013 -#define VL53L010_REG_RESULT_RANGE_STATUS 0x0014 - -#define VL53L010_REG_RESULT_SIGNAL_COUNT_RATE_RET 0x001A -#define VL53L010_REG_RESULT_AMBIENT_COUNT_RATE_RET 0x001C -#define VL53L010_REG_RESULT_FINAL_RANGE 0x001E - -/* Algo register */ -#define VL53L010_REG_ALGO_CROSSTALK_COMPENSATION_RATE 0x0020 -#define VL53L010_REG_ALGO_RANGE_IGNORE_VALID_HEIGHT 0x0025 -#define VL53L010_REG_ALGO_RANGE_IGNORE_THRESHOLD 0x0026 -#define VL53L010_REG_ALGO_SNR_RATIO 0x0027 -#define VL53L010_REG_ALGO_RANGE_CHECK_ENABLES 0x0028 - -#define VL53L010_REG_ALGO_PART_TO_PART_RANGE_OFFSET 0x0029 - -#define VL53L010_REG_I2C_SLAVE_DEVICE_ADDRESS 0x008a - -/* MSRC registers */ -#define VL53L010_REG_MSRC_CONFIG_COUNT 0x0044 -#define VL53L010_REG_MSRC_CONFIG_TIMEOUT 0x0046 -#define VL53L010_REG_MSRC_CONFIG_MIN_SNR 0x0055 -#define VL53L010_REG_MSRC_CONFIG_VALID_PHASE_LOW 0x0047 -#define VL53L010_REG_MSRC_CONFIG_VALID_PHASE_HIGH 0x0048 - -/* RANGE A registers */ -#define VL53L010_REG_RNGA_CONFIG_VCSEL_PERIOD 0x0050 -#define VL53L010_REG_RNGA_TIMEOUT_MSB 0x0051 -#define VL53L010_REG_RNGA_TIMEOUT_LSB 0x0052 -#define VL53L010_REG_RNGA_CONFIG_VALID_PHASE_LOW 0x0056 -#define VL53L010_REG_RNGA_CONFIG_VALID_PHASE_HIGH 0x0057 - -/* RANGE B1 registers */ -#define VL53L010_REG_RNGB1_CONFIG_VCSEL_PERIOD 0x0060 -#define VL53L010_REG_RNGB1_TIMEOUT_MSB 0x0061 -#define VL53L010_REG_RNGB1_TIMEOUT_LSB 0x0062 -#define VL53L010_REG_RNGB1_CONFIG_VALID_PHASE_LOW 0x0066 -#define VL53L010_REG_RNGB1_CONFIG_VALID_PHASE_HIGH 0x0067 - -/* RANGE B2 registers */ -#define VL53L010_REG_RNGB2_CONFIG_VCSEL_PERIOD 0x0070 -#define VL53L010_REG_RNGB2_TIMEOUT_MSB 0x0071 -#define VL53L010_REG_RNGB2_TIMEOUT_LSB 0x0072 -#define VL53L010_REG_RNGB2_CONFIG_VALID_PHASE_LOW 0x0076 -#define VL53L010_REG_RNGB2_CONFIG_VALID_PHASE_HIGH 0x0077 - - -#define VL53L010_REG_SOFT_RESET_GO2_SOFT_RESET_N 0x00bf -#define VL53L010_REG_IDENTIFICATION_MODEL_ID 0x00c0 -#define VL53L010_REG_IDENTIFICATION_REVISION_ID 0x00c2 -#define VL53L010_REG_IDENTIFICATION_MODULE_ID 0x00c3 - -#define VL53L010_REG_OSC_CALIBRATE_VAL 0x00f8 - -#define VL53L010_REG_FIRMWARE_MODE_STATUS 0x00C5 - -#define VL53L010_REG_DYNAMIC_SPAD_ACTUAL_RTN_SPADS_INT 0x0016 - -#define VL53L010_SIGMA_ESTIMATE_MAX_VALUE 65535 -/*equivalent to a range sigma of 655.35mm */ - -/* - * Speed of light in um per 1E-10 Seconds - */ - -#define VL53L010_SPEED_OF_LIGHT_IN_AIR 2997 - - -/** @} VL53L010_DefineRegisters_group */ - -/** @} VL53L010_SpecDefines_group */ - - -#endif - -/* _VL53L010_DEVICE_H_ */ - - diff --git a/drivers/input/misc/vl53L0/inc/vl53l010_strings.h b/drivers/input/misc/vl53L0/inc/vl53l010_strings.h deleted file mode 100644 index cd632abc5728..000000000000 --- a/drivers/input/misc/vl53L0/inc/vl53l010_strings.h +++ /dev/null @@ -1,134 +0,0 @@ -/******************************************************************************* -Copyright © 2016, STMicroelectronics International N.V. -All rights reserved. - -Redistribution and use in source and binary forms, with or without -modification, are permitted provided that the following conditions are met: - * Redistributions of source code must retain the above copyright - notice, this list of conditions and the following disclaimer. - * Redistributions in binary form must reproduce the above copyright - notice, this list of conditions and the following disclaimer in the - documentation and/or other materials provided with the distribution. - * Neither the name of STMicroelectronics nor the - names of its contributors may be used to endorse or promote products - derived from this software without specific prior written permission. - -THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND -ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED -WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, AND -NON-INFRINGEMENT OF INTELLECTUAL PROPERTY RIGHTS ARE DISCLAIMED. -IN NO EVENT SHALL STMICROELECTRONICS INTERNATIONAL N.V. BE LIABLE FOR ANY -DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES -(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; -LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND -ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT -(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS -SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. -*******************************************************************************/ - -#ifndef VL53L010_STRINGS_H_ -#define VL53L010_STRINGS_H_ - -#ifdef __cplusplus -extern "C" { -#endif - - #define VL53L010_STRING_DEVICE_INFO_NAME \ - "VL53L0 cut1.0" - #define VL53L010_STRING_DEVICE_INFO_NAME_TS0 \ - "VL53L0 TS0" - #define VL53L010_STRING_DEVICE_INFO_NAME_TS1 \ - "VL53L0 TS1" - #define VL53L010_STRING_DEVICE_INFO_NAME_TS2 \ - "VL53L0 TS2" - #define VL53L010_STRING_DEVICE_INFO_NAME_ES1 \ - "VL53L0 ES1 or later" - #define VL53L010_STRING_DEVICE_INFO_TYPE \ - "VL53L0" - - /* PAL ERROR strings */ - #define VL53L010_STRING_ERROR_NONE \ - "No Error" - #define VL53L010_STRING_ERROR_CALIBRATION_WARNING \ - "Calibration Warning Error" - #define VL53L010_STRING_ERROR_MIN_CLIPPED \ - "Min clipped error" - #define VL53L010_STRING_ERROR_UNDEFINED \ - "Undefined error" - #define VL53L010_STRING_ERROR_INVALID_PARAMS \ - "Invalid parameters error" - #define VL53L010_STRING_ERROR_NOT_SUPPORTED \ - "Not supported error" - #define VL53L010_STRING_ERROR_RANGE_ERROR \ - "Range error" - #define VL53L010_STRING_ERROR_TIME_OUT \ - "Time out error" - #define VL53L010_STRING_ERROR_MODE_NOT_SUPPORTED \ - "Mode not supported error" - #define VL53L010_STRING_ERROR_NOT_IMPLEMENTED \ - "Not implemented error" - - #define VL53L010_STRING_UNKNOW_ERROR_CODE \ - "Unknow Error Code" - #define VL53L010_STRING_ERROR_BUFFER_TOO_SMALL \ - "Buffer too small" - - #define VL53L010_STRING_ERROR_GPIO_NOT_EXISTING \ - "GPIO not existing" - #define VL53L010_STRING_ERROR_GPIO_FUNCTIONALITY_NOT_SUPPORTED \ - "GPIO functionality not supported" - #define VL53L010_STRING_ERROR_CONTROL_INTERFACE \ - "Control Interface Error" - - - /* Device Specific */ - #define VL53L010_STRING_DEVICEERROR_NONE \ - "No Update" - #define VL53L010_STRING_DEVICEERROR_VCSELCONTINUITYTESTFAILURE \ - "VCSEL Continuity Test Failure" - #define VL53L010_STRING_DEVICEERROR_VCSELWATCHDOGTESTFAILURE \ - "VCSEL Watchdog Test Failure" - #define VL53L010_STRING_DEVICEERROR_NOVHVVALUEFOUND \ - "No VHV Value found" - #define VL53L010_STRING_DEVICEERROR_MSRCNOTARGET \ - "MSRC No Target Error" - #define VL53L010_STRING_DEVICEERROR_MSRCMINIMUMSNR \ - "MSRC Minimum SNR Error" - #define VL53L010_STRING_DEVICEERROR_MSRCWRAPAROUND \ - "MSRC Wraparound Error" - #define VL53L010_STRING_DEVICEERROR_TCC \ - "TCC Error" - #define VL53L010_STRING_DEVICEERROR_RANGEAWRAPAROUND \ - "Range A Wraparound Error" - #define VL53L010_STRING_DEVICEERROR_RANGEBWRAPAROUND \ - "Range B Wraparound Error" - #define VL53L010_STRING_DEVICEERROR_MINCLIP \ - "Min Clip Error" - #define VL53L010_STRING_DEVICEERROR_RANGECOMPLETE \ - "Range Complete" - #define VL53L010_STRING_DEVICEERROR_ALGOUNDERFLOW \ - "Range Algo Underflow Error" - #define VL53L010_STRING_DEVICEERROR_ALGOOVERFLOW \ - "Range Algo Overlow Error" - #define VL53L010_STRING_DEVICEERROR_FINALSNRLIMIT \ - "Final Minimum SNR Error" - #define VL53L010_STRING_DEVICEERROR_NOTARGETIGNORE \ - "No Target Ignore Error" - #define VL53L010_STRING_DEVICEERROR_UNKNOWN \ - "Unknown error code" - - - /* Check Enable */ - #define VL53L010_STRING_CHECKENABLE_SIGMA \ - "SIGMA" - #define VL53L010_STRING_CHECKENABLE_SIGNAL_RATE \ - "SIGNAL RATE" - - - -#ifdef __cplusplus -} -#endif - -#endif - diff --git a/drivers/input/misc/vl53L0/inc/vl53l010_tuning.h b/drivers/input/misc/vl53L0/inc/vl53l010_tuning.h deleted file mode 100644 index 8128e65d8f6c..000000000000 --- a/drivers/input/misc/vl53L0/inc/vl53l010_tuning.h +++ /dev/null @@ -1,58 +0,0 @@ -/******************************************************************************* -Copyright © 2016, STMicroelectronics International N.V. -All rights reserved. - -Redistribution and use in source and binary forms, with or without -modification, are permitted provided that the following conditions are met: - * Redistributions of source code must retain the above copyright - notice, this list of conditions and the following disclaimer. - * Redistributions in binary form must reproduce the above copyright - notice, this list of conditions and the following disclaimer in the - documentation and/or other materials provided with the distribution. - * Neither the name of STMicroelectronics nor the - names of its contributors may be used to endorse or promote products - derived from this software without specific prior written permission. - -THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND -ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED -WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, AND -NON-INFRINGEMENT OF INTELLECTUAL PROPERTY RIGHTS ARE DISCLAIMED. -IN NO EVENT SHALL STMICROELECTRONICS INTERNATIONAL N.V. BE LIABLE FOR ANY -DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES -(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; -LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND -ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT -(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS -SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. -*******************************************************************************/ - -#ifndef _VL53L010_TUNING_H_ -#define _VL53L010_TUNING_H_ - -#include "vl53l0_def.h" -#include "vl53l0_platform.h" - - -#ifdef __cplusplus -extern "C" { -#endif - - -/** - * @brief Internal function used to Program the default tuning settings - * - * @ingroup VL53L0_general_group - * @note This function access to the device - * - * @param Dev Device Handle - * @return VL53L0_ERROR_NONE Success - * @return "Other error code" See ::VL53L0_Error - */ -VL53L0_Error VL53L010_load_tuning_settings(VL53L0_DEV Dev); - - -#ifdef __cplusplus -} -#endif - -#endif /* _VL53L010_TUNING_H_ */ diff --git a/drivers/input/misc/vl53L0/inc/vl53l0_api.h b/drivers/input/misc/vl53L0/inc/vl53l0_api.h index f56603853b45..5c38f5e3ae5c 100644 --- a/drivers/input/misc/vl53L0/inc/vl53l0_api.h +++ b/drivers/input/misc/vl53L0/inc/vl53l0_api.h @@ -1,5 +1,5 @@ /******************************************************************************* - Copyright © 2016, STMicroelectronics International N.V. + * Copyright © 2016, STMicroelectronics International N.V. All rights reserved. Redistribution and use in source and binary forms, with or without @@ -124,7 +124,7 @@ VL53L0_API VL53L0_Error VL53L0_GetDeviceInfo(VL53L0_DEV Dev, * @return "Other error code" See ::VL53L0_Error */ VL53L0_API VL53L0_Error VL53L0_GetDeviceErrorStatus(VL53L0_DEV Dev, - VL53L0_DeviceError *pDeviceErrorStatus); + VL53L0_DeviceError * pDeviceErrorStatus); /** * @brief Human readable Range Status string for a given RangeStatus @@ -193,7 +193,7 @@ VL53L0_API VL53L0_Error VL53L0_GetPalStateString(VL53L0_State PalStateCode, * @return "Other error code" See ::VL53L0_Error */ VL53L0_API VL53L0_Error VL53L0_GetPalState(VL53L0_DEV Dev, - VL53L0_State *pPalState); + VL53L0_State * pPalState); /** * @brief Set the power mode for a given Device @@ -232,7 +232,7 @@ VL53L0_API VL53L0_Error VL53L0_SetPowerMode(VL53L0_DEV Dev, * @return "Other error code" See ::VL53L0_Error */ VL53L0_API VL53L0_Error VL53L0_GetPowerMode(VL53L0_DEV Dev, - VL53L0_PowerModes *pPowerMode); + VL53L0_PowerModes * pPowerMode); /** * Set or over-hide part to part calibration offset @@ -571,7 +571,7 @@ VL53L0_API VL53L0_Error VL53L0_SetDeviceMode(VL53L0_DEV Dev, * DeviceMode is not in the supported list */ VL53L0_API VL53L0_Error VL53L0_GetDeviceMode(VL53L0_DEV Dev, - VL53L0_DeviceModes *pDeviceMode); + VL53L0_DeviceModes * pDeviceMode); /** * @brief Sets the resolution of range measurements. @@ -601,8 +601,8 @@ VL53L0_API VL53L0_Error VL53L0_SetRangeFractionEnable(VL53L0_DEV Dev, * * @note This function Accesses the device * - * @param Dev Device Handle - * @param pEnable Output Parameter reporting the fraction enable state. + * @param Dev Device Handle + * @param pEnable Output Parameter reporting the fraction enable state. * * @return VL53L0_ERROR_NONE Success * @return "Other error code" See ::VL53L0_Error @@ -653,7 +653,7 @@ VL53L0_API VL53L0_Error VL53L0_SetHistogramMode(VL53L0_DEV Dev, * @return "Other error code" See ::VL53L0_Error */ VL53L0_API VL53L0_Error VL53L0_GetHistogramMode(VL53L0_DEV Dev, - VL53L0_HistogramModes *pHistogramMode); + VL53L0_HistogramModes * pHistogramMode); /** * @brief Set Ranging Timing Budget in microseconds @@ -1214,36 +1214,6 @@ VL53L0_API VL53L0_Error VL53L0_SetWrapAroundCheckEnable(VL53L0_DEV Dev, VL53L0_API VL53L0_Error VL53L0_GetWrapAroundCheckEnable(VL53L0_DEV Dev, uint8_t *pWrapAroundCheckEnable); -/** - * @brief Set Dmax Calibration Parameters for a given device - * When one of the parameter is zero, this function will get parameter - * from NVM. - * @note This function doesn't Access to the device - * - * @param Dev Device Handle - * @param RangeMilliMeter Calibration Distance - * @param SignalRateRtnMegaCps Signal rate return read at CalDistance - * @return VL53L0_ERROR_NONE Success - * @return "Other error code" See ::VL53L0_Error - */ -VL53L0_API VL53L0_Error VL53L0_SetDmaxCalParameters(VL53L0_DEV Dev, - uint16_t RangeMilliMeter, FixPoint1616_t SignalRateRtnMegaCps); - -/** - * @brief Get Dmax Calibration Parameters for a given device - * - * - * @note This function Access to the device - * - * @param Dev Device Handle - * @param pRangeMilliMeter Pointer to Calibration Distance - * @param pSignalRateRtnMegaCps Pointer to Signal rate return - * @return VL53L0_ERROR_NONE Success - * @return "Other error code" See ::VL53L0_Error - */ -VL53L0_API VL53L0_Error VL53L0_GetDmaxCalParameters(VL53L0_DEV Dev, - uint16_t *pRangeMilliMeter, FixPoint1616_t *pSignalRateRtnMegaCps); - /** @} VL53L0_parameters_group */ /** @defgroup VL53L0_measurement_group VL53L0 Measurement Functions @@ -1678,9 +1648,9 @@ VL53L0_API VL53L0_Error VL53L0_SetGpioConfig(VL53L0_DEV Dev, uint8_t Pin, * @return "Other error code" See ::VL53L0_Error */ VL53L0_API VL53L0_Error VL53L0_GetGpioConfig(VL53L0_DEV Dev, uint8_t Pin, - VL53L0_DeviceModes *pDeviceMode, - VL53L0_GpioFunctionality *pFunctionality, - VL53L0_InterruptPolarity *pPolarity); + VL53L0_DeviceModes * pDeviceMode, + VL53L0_GpioFunctionality * pFunctionality, + VL53L0_InterruptPolarity * pPolarity); /** * @brief Set low and high Interrupt thresholds for a given mode @@ -1728,6 +1698,24 @@ VL53L0_API VL53L0_Error VL53L0_GetInterruptThresholds(VL53L0_DEV Dev, VL53L0_DeviceModes DeviceMode, FixPoint1616_t *pThresholdLow, FixPoint1616_t *pThresholdHigh); +/** + * @brief Return device stop completion status + * + * @par Function Description + * Returns stop completiob status. + * User shall call this function after a stop command + * + * @note This function Access to the device + * + * @param Dev Device Handle + * @param pStopStatus Pointer to status variable to update + * @return VL53L0_ERROR_NONE Success + * @return "Other error code" See ::VL53L0_Error + */ +VL53L0_API VL53L0_Error VL53L0_GetStopCompletedStatus(VL53L0_DEV Dev, + uint32_t *pStopStatus); + + /** * @brief Clear given system interrupt condition * @@ -1739,6 +1727,8 @@ VL53L0_API VL53L0_Error VL53L0_GetInterruptThresholds(VL53L0_DEV Dev, * @param Dev Device Handle * @param InterruptMask Mask of interrupts to clear * @return VL53L0_ERROR_NONE Success + * @return VL53L0_ERROR_INTERRUPT_NOT_CLEARED Cannot clear interrupts + * * @return "Other error code" See ::VL53L0_Error */ VL53L0_API VL53L0_Error VL53L0_ClearInterruptMask(VL53L0_DEV Dev, diff --git a/drivers/input/misc/vl53L0/inc/vl53l0_api_calibration.h b/drivers/input/misc/vl53L0/inc/vl53l0_api_calibration.h index ff54e99263d9..df9b43987eb5 100644 --- a/drivers/input/misc/vl53L0/inc/vl53l0_api_calibration.h +++ b/drivers/input/misc/vl53L0/inc/vl53l0_api_calibration.h @@ -1,5 +1,5 @@ /******************************************************************************* -Copyright © 2016, STMicroelectronics International N.V. + * Copyright © 2016, STMicroelectronics International N.V. All rights reserved. Redistribution and use in source and binary forms, with or without diff --git a/drivers/input/misc/vl53L0/inc/vl53l0_api_core.h b/drivers/input/misc/vl53L0/inc/vl53l0_api_core.h index 563017067528..4596bbef20de 100644 --- a/drivers/input/misc/vl53L0/inc/vl53l0_api_core.h +++ b/drivers/input/misc/vl53L0/inc/vl53l0_api_core.h @@ -1,5 +1,5 @@ /******************************************************************************* -Copyright © 2016, STMicroelectronics International N.V. + * Copyright © 2016, STMicroelectronics International N.V. All rights reserved. Redistribution and use in source and binary forms, with or without @@ -79,11 +79,14 @@ VL53L0_Error VL53L0_load_tuning_settings(VL53L0_DEV Dev, VL53L0_Error VL53L0_calc_sigma_estimate(VL53L0_DEV Dev, VL53L0_RangingMeasurementData_t *pRangingMeasurementData, - FixPoint1616_t *pSigmaEstimate, uint32_t *pDmax_mm); + FixPoint1616_t *pSigmaEstimate); + +VL53L0_Error VL53L0_calc_dmax( + VL53L0_DEV Dev, FixPoint1616_t ambRateMeas, uint32_t *pdmax_mm); VL53L0_Error VL53L0_get_total_xtalk_rate(VL53L0_DEV Dev, VL53L0_RangingMeasurementData_t *pRangingMeasurementData, - FixPoint1616_t *ptotal_xtalk_rate_mcps); + FixPoint1616_t *ptotal_xtalk_rate_mcps); VL53L0_Error VL53L0_get_total_signal_rate(VL53L0_DEV Dev, VL53L0_RangingMeasurementData_t *pRangingMeasurementData, diff --git a/drivers/input/misc/vl53L0/inc/vl53l0_api_histogram.h b/drivers/input/misc/vl53L0/inc/vl53l0_api_histogram.h index b3e21442ca53..e453d82eaec8 100644 --- a/drivers/input/misc/vl53L0/inc/vl53l0_api_histogram.h +++ b/drivers/input/misc/vl53L0/inc/vl53l0_api_histogram.h @@ -1,5 +1,5 @@ /******************************************************************************* -Copyright © 2016, STMicroelectronics International N.V. + * Copyright © 2016, STMicroelectronics International N.V. All rights reserved. Redistribution and use in source and binary forms, with or without @@ -44,7 +44,7 @@ VL53L0_Error VL53L0_set_histogram_mode(VL53L0_DEV Dev, VL53L0_HistogramModes HistogramMode); VL53L0_Error VL53L0_get_histogram_mode(VL53L0_DEV Dev, - VL53L0_HistogramModes *pHistogramMode); + VL53L0_HistogramModes * pHistogramMode); VL53L0_Error VL53L0_start_histogram_measurement(VL53L0_DEV Dev, VL53L0_HistogramModes histoMode, diff --git a/drivers/input/misc/vl53L0/inc/vl53l0_api_ranging.h b/drivers/input/misc/vl53L0/inc/vl53l0_api_ranging.h index d2f90d34f3e4..ba0bd03fe71f 100644 --- a/drivers/input/misc/vl53L0/inc/vl53l0_api_ranging.h +++ b/drivers/input/misc/vl53L0/inc/vl53l0_api_ranging.h @@ -1,5 +1,5 @@ /******************************************************************************* -Copyright © 2016, STMicroelectronics International N.V. + * Copyright © 2016, STMicroelectronics International N.V. All rights reserved. Redistribution and use in source and binary forms, with or without diff --git a/drivers/input/misc/vl53L0/inc/vl53l0_api_strings.h b/drivers/input/misc/vl53L0/inc/vl53l0_api_strings.h index 72f290f19c3f..2bb6a1e8ea33 100644 --- a/drivers/input/misc/vl53L0/inc/vl53l0_api_strings.h +++ b/drivers/input/misc/vl53L0/inc/vl53l0_api_strings.h @@ -1,5 +1,5 @@ /******************************************************************************* -Copyright © 2016, STMicroelectronics International N.V. + * Copyright © 2016, STMicroelectronics International N.V. All rights reserved. Redistribution and use in source and binary forms, with or without @@ -174,6 +174,8 @@ VL53L0_Error VL53L0_get_limit_check_info(VL53L0_DEV Dev, uint16_t LimitCheckId, "GPIO not existing" #define VL53L0_STRING_ERROR_GPIO_FUNCTIONALITY_NOT_SUPPORTED \ "GPIO funct not supported" + #define VL53L0_STRING_ERROR_INTERRUPT_NOT_CLEARED \ + "Interrupt not Cleared" #define VL53L0_STRING_ERROR_CONTROL_INTERFACE \ "Control Interface Error" #define VL53L0_STRING_ERROR_INVALID_COMMAND \ @@ -253,6 +255,10 @@ VL53L0_Error VL53L0_get_limit_check_info(VL53L0_DEV Dev, uint16_t LimitCheckId, "SIGNAL REF CLIP" #define VL53L0_STRING_CHECKENABLE_RANGE_IGNORE_THRESHOLD \ "RANGE IGNORE THRESHOLD" + #define VL53L0_STRING_CHECKENABLE_SIGNAL_RATE_MSRC \ + "SIGNAL RATE MSRC" + #define VL53L0_STRING_CHECKENABLE_SIGNAL_RATE_PRE_RANGE \ + "SIGNAL RATE PRE RANGE" /* Sequence Step */ #define VL53L0_STRING_SEQUENCESTEP_TCC "TCC" diff --git a/drivers/input/misc/vl53L0/inc/vl53l0_def.h b/drivers/input/misc/vl53L0/inc/vl53l0_def.h index 2391242949ed..cedfd5008055 100644 --- a/drivers/input/misc/vl53L0/inc/vl53l0_def.h +++ b/drivers/input/misc/vl53L0/inc/vl53l0_def.h @@ -1,5 +1,5 @@ /******************************************************************************* -Copyright © 2016, STMicroelectronics International N.V. + * Copyright © 2016, STMicroelectronics International N.V. All rights reserved. Redistribution and use in source and binary forms, with or without @@ -80,10 +80,10 @@ extern "C" { /** VL53L0 PAL IMPLEMENTATION minor version */ #define VL53L0_IMPLEMENTATION_VER_MINOR 1 /** VL53L0 PAL IMPLEMENTATION sub version */ -#define VL53L0_IMPLEMENTATION_VER_SUB 18 +#define VL53L0_IMPLEMENTATION_VER_SUB 23 /** VL53L0 PAL IMPLEMENTATION sub version */ -#define VL53L0_IMPLEMENTATION_VER_REVISION 4357 -#define VL53L0_DEFAULT_MAX_LOOP 200 +#define VL53L0_IMPLEMENTATION_VER_REVISION 4960 +#define VL53L0_DEFAULT_MAX_LOOP 2000 #define VL53L0_MAX_STRING_LENGTH 32 @@ -130,45 +130,50 @@ typedef struct { typedef int8_t VL53L0_Error; -#define VL53L0_ERROR_NONE ((VL53L0_Error) 0) -#define VL53L0_ERROR_CALIBRATION_WARNING ((VL53L0_Error) - 1) +#define VL53L0_ERROR_NONE ((VL53L0_Error) 0) +#define VL53L0_ERROR_CALIBRATION_WARNING ((VL53L0_Error)-1) /*!< Warning invalid calibration data may be in used - \a VL53L0_InitData() - \a VL53L0_GetOffsetCalibrationData - \a VL53L0_SetOffsetCalibrationData */ -#define VL53L0_ERROR_MIN_CLIPPED ((VL53L0_Error) - 2) + * \a VL53L0_InitData() + * \a VL53L0_GetOffsetCalibrationData + * \a VL53L0_SetOffsetCalibrationData + */ +#define VL53L0_ERROR_MIN_CLIPPED ((VL53L0_Error)-2) /*!< Warning parameter passed was clipped to min before to be applied */ -#define VL53L0_ERROR_UNDEFINED ((VL53L0_Error) - 3) +#define VL53L0_ERROR_UNDEFINED ((VL53L0_Error)-3) /*!< Unqualified error */ -#define VL53L0_ERROR_INVALID_PARAMS ((VL53L0_Error) - 4) +#define VL53L0_ERROR_INVALID_PARAMS ((VL53L0_Error)-4) /*!< Parameter passed is invalid or out of range */ -#define VL53L0_ERROR_NOT_SUPPORTED ((VL53L0_Error) - 5) +#define VL53L0_ERROR_NOT_SUPPORTED ((VL53L0_Error)-5) /*!< Function is not supported in current mode or configuration */ -#define VL53L0_ERROR_RANGE_ERROR ((VL53L0_Error) - 6) +#define VL53L0_ERROR_RANGE_ERROR ((VL53L0_Error)-6) /*!< Device report a ranging error interrupt status */ -#define VL53L0_ERROR_TIME_OUT ((VL53L0_Error) - 7) +#define VL53L0_ERROR_TIME_OUT ((VL53L0_Error)-7) /*!< Aborted due to time out */ -#define VL53L0_ERROR_MODE_NOT_SUPPORTED ((VL53L0_Error) - 8) +#define VL53L0_ERROR_MODE_NOT_SUPPORTED ((VL53L0_Error)-8) /*!< Asked mode is not supported by the device */ -#define VL53L0_ERROR_BUFFER_TOO_SMALL ((VL53L0_Error) - 9) +#define VL53L0_ERROR_BUFFER_TOO_SMALL ((VL53L0_Error)-9) /*!< ... */ -#define VL53L0_ERROR_GPIO_NOT_EXISTING ((VL53L0_Error) - 10) +#define VL53L0_ERROR_GPIO_NOT_EXISTING ((VL53L0_Error)-10) /*!< User tried to setup a non-existing GPIO pin */ -#define VL53L0_ERROR_GPIO_FUNCTIONALITY_NOT_SUPPORTED ((VL53L0_Error) - 11) +#define VL53L0_ERROR_GPIO_FUNCTIONALITY_NOT_SUPPORTED ((VL53L0_Error)-11) /*!< unsupported GPIO functionality */ -#define VL53L0_ERROR_CONTROL_INTERFACE ((VL53L0_Error) - 20) +#define VL53L0_ERROR_INTERRUPT_NOT_CLEARED ((VL53L0_Error)-12) + /*!< Error during interrupt clear */ +#define VL53L0_ERROR_CONTROL_INTERFACE ((VL53L0_Error)-20) /*!< error reported from IO functions */ -#define VL53L0_ERROR_INVALID_COMMAND ((VL53L0_Error) - 30) +#define VL53L0_ERROR_INVALID_COMMAND ((VL53L0_Error)-30) /*!< The command is not allowed in the current device state - * (power down) */ -#define VL53L0_ERROR_DIVISION_BY_ZERO ((VL53L0_Error) - 40) + * (power down) + */ +#define VL53L0_ERROR_DIVISION_BY_ZERO ((VL53L0_Error)-40) /*!< In the function a division by zero occurs */ -#define VL53L0_ERROR_REF_SPAD_INIT ((VL53L0_Error) - 50) +#define VL53L0_ERROR_REF_SPAD_INIT ((VL53L0_Error)-50) /*!< Error during reference SPAD initialization */ -#define VL53L0_ERROR_NOT_IMPLEMENTED ((VL53L0_Error) - 99) +#define VL53L0_ERROR_NOT_IMPLEMENTED ((VL53L0_Error)-99) /*!< Tells requested functionality has not been implemented yet or - * not compatible with the device */ + * not compatible with the device + */ /** @} VL53L0_define_Error_group */ @@ -178,13 +183,13 @@ typedef int8_t VL53L0_Error; */ typedef uint8_t VL53L0_DeviceModes; -#define VL53L0_DEVICEMODE_SINGLE_RANGING ((VL53L0_DeviceModes) 0) +#define VL53L0_DEVICEMODE_SINGLE_RANGING ((VL53L0_DeviceModes) 0) #define VL53L0_DEVICEMODE_CONTINUOUS_RANGING ((VL53L0_DeviceModes) 1) -#define VL53L0_DEVICEMODE_SINGLE_HISTOGRAM ((VL53L0_DeviceModes) 2) +#define VL53L0_DEVICEMODE_SINGLE_HISTOGRAM ((VL53L0_DeviceModes) 2) #define VL53L0_DEVICEMODE_CONTINUOUS_TIMED_RANGING ((VL53L0_DeviceModes) 3) -#define VL53L0_DEVICEMODE_SINGLE_ALS ((VL53L0_DeviceModes) 10) -#define VL53L0_DEVICEMODE_GPIO_DRIVE ((VL53L0_DeviceModes) 20) -#define VL53L0_DEVICEMODE_GPIO_OSC ((VL53L0_DeviceModes) 21) +#define VL53L0_DEVICEMODE_SINGLE_ALS ((VL53L0_DeviceModes) 10) +#define VL53L0_DEVICEMODE_GPIO_DRIVE ((VL53L0_DeviceModes) 20) +#define VL53L0_DEVICEMODE_GPIO_OSC ((VL53L0_DeviceModes) 21) /* ... Modes to be added depending on device */ /** @} VL53L0_define_DeviceModes_group */ @@ -196,13 +201,13 @@ typedef uint8_t VL53L0_DeviceModes; */ typedef uint8_t VL53L0_HistogramModes; -#define VL53L0_HISTOGRAMMODE_DISABLED ((VL53L0_HistogramModes) 0) +#define VL53L0_HISTOGRAMMODE_DISABLED ((VL53L0_HistogramModes) 0) /*!< Histogram Disabled */ -#define VL53L0_HISTOGRAMMODE_REFERENCE_ONLY ((VL53L0_HistogramModes) 1) +#define VL53L0_HISTOGRAMMODE_REFERENCE_ONLY ((VL53L0_HistogramModes) 1) /*!< Histogram Reference array only */ -#define VL53L0_HISTOGRAMMODE_RETURN_ONLY ((VL53L0_HistogramModes) 2) +#define VL53L0_HISTOGRAMMODE_RETURN_ONLY ((VL53L0_HistogramModes) 2) /*!< Histogram Return array only */ -#define VL53L0_HISTOGRAMMODE_BOTH ((VL53L0_HistogramModes) 3) +#define VL53L0_HISTOGRAMMODE_BOTH ((VL53L0_HistogramModes) 3) /*!< Histogram both Reference and Return Arrays */ /* ... Modes to be added depending on device */ /** @} VL53L0_define_HistogramModes_group */ @@ -227,6 +232,18 @@ typedef uint8_t VL53L0_PowerModes; /** @} VL53L0_define_PowerModes_group */ +#define VL53L0_DMAX_LUT_SIZE 7 + /*!< Defines the number of items in the DMAX lookup table */ + +/** @brief Structure defining data pair that makes up the DMAX Lookup table. + */ +typedef struct { + FixPoint1616_t ambRate_mcps[VL53L0_DMAX_LUT_SIZE]; + /*!< Ambient rate (mcps) */ + FixPoint1616_t dmax_mm[VL53L0_DMAX_LUT_SIZE]; + /*!< DMAX Value (mm) */ +} VL53L0_DMaxLUT_t; + /** @brief Defines all parameters for the device */ typedef struct { @@ -234,19 +251,22 @@ typedef struct { /*!< Defines type of measurement to be done for the next measure */ VL53L0_HistogramModes HistogramMode; /*!< Defines type of histogram measurement to be done for the next - * measure */ + * measure + */ uint32_t MeasurementTimingBudgetMicroSeconds; /*!< Defines the allowed total time for a single measurement */ uint32_t InterMeasurementPeriodMilliSeconds; /*!< Defines time between two consecutive measurements (between two - * measurement starts). If set to 0 means back-to-back mode */ + * measurement starts). If set to 0 means back-to-back mode + */ uint8_t XTalkCompensationEnable; /*!< Tells if Crosstalk compensation shall be enable or not */ uint16_t XTalkCompensationRangeMilliMeter; /*!< CrossTalk compensation range in millimeter */ FixPoint1616_t XTalkCompensationRateMegaCps; /*!< CrossTalk compensation rate in Mega counts per seconds. - * Expressed in 16.16 fixed point format. */ + * Expressed in 16.16 fixed point format. + */ int32_t RangeOffsetMicroMeters; /*!< Range offset adjustment (mm). */ @@ -254,10 +274,16 @@ typedef struct { /*!< This Array store all the Limit Check enable for this device. */ uint8_t LimitChecksStatus[VL53L0_CHECKENABLE_NUMBER_OF_CHECKS]; /*!< This Array store all the Status of the check linked to last - * measurement. */ + * measurement. + */ FixPoint1616_t LimitChecksValue[VL53L0_CHECKENABLE_NUMBER_OF_CHECKS]; /*!< This Array store all the Limit Check value for this device */ + VL53L0_DMaxLUT_t dmax_lut; + /*!< Lookup table defining ambient rates and associated + * dmax values. + */ + uint8_t WrapAroundCheckEnable; /*!< Tells if Wrap Around Check shall be enable or not */ } VL53L0_DeviceParameters_t; @@ -288,57 +314,55 @@ typedef uint8_t VL53L0_State; /** @} VL53L0_define_State_group */ -/** @brief Structure containing the Dmax computation parameters and data - */ -typedef struct { - int32_t AmbTuningWindowFactor_K; - /*!< internal algo tuning (*1000) */ - int32_t RetSignalAt0mm; - /*!< intermediate dmax computation value caching */ -} VL53L0_DMaxData_t; - /** * @struct VL53L0_RangeData_t * @brief Range measurement data. */ typedef struct { - uint32_t TimeStamp; /*!< 32-bit time stamp. */ + uint32_t TimeStamp; /*!< 32-bit time stamp. */ uint32_t MeasurementTimeUsec; /*!< Give the Measurement time needed by the device to do the - * measurement.*/ + * measurement. + */ - uint16_t RangeMilliMeter; /*!< range distance in millimeter. */ + uint16_t RangeMilliMeter; /*!< range distance in millimeter. */ uint16_t RangeDMaxMilliMeter; /*!< Tells what is the maximum detection distance of the device * in current setup and environment conditions (Filled when - * applicable) */ + * applicable) + */ FixPoint1616_t SignalRateRtnMegaCps; /*!< Return signal rate (MCPS)\n these is a 16.16 fix point * value, which is effectively a measure of target - * reflectance.*/ + * reflectance. + */ FixPoint1616_t AmbientRateRtnMegaCps; /*!< Return ambient rate (MCPS)\n these is a 16.16 fix point * value, which is effectively a measure of the ambien - * t light.*/ + * t light. + */ uint16_t EffectiveSpadRtnCount; /*!< Return the effective SPAD count for the return signal. - * To obtain Real value it should be divided by 256 */ + * To obtain Real value it should be divided by 256 + */ uint8_t ZoneId; /*!< Denotes which zone and range scheduler stage the range - * data relates to. */ + * data relates to. + */ uint8_t RangeFractionalPart; /*!< Fractional part of range distance. Final value is a - * FixPoint168 value. */ + * FixPoint168 value. + */ uint8_t RangeStatus; /*!< Range Status for the current measurement. This is device * dependent. Value = 0 means value is valid. - * See \ref RangeStatusPage */ - FixPoint1616_t SigmaEstimate; + * See \ref RangeStatusPage + */ } VL53L0_RangingMeasurementData_t; @@ -352,8 +376,10 @@ typedef struct { /* Histogram Measurement data */ uint32_t HistogramData[VL53L0_HISTOGRAM_BUFFER_SIZE]; /*!< Histogram data */ - uint8_t HistogramType; /*!< Indicate the types of histogram data : - Return only, Reference only, both Return and Reference */ + /*!< Indicate the types of histogram data : + *Return only, Reference only, both Return and Reference + */ + uint8_t HistogramType; uint8_t FirstBin; /*!< First Bin value */ uint8_t BufferSize; /*!< Buffer Size - Set by the user.*/ uint8_t NumberOfBins; @@ -361,7 +387,8 @@ typedef struct { VL53L0_DeviceError ErrorStatus; /*!< Error status of the current measurement. \n - see @a ::VL53L0_DeviceError @a VL53L0_GetStatusErrorString() */ + * see @a ::VL53L0_DeviceError @a VL53L0_GetStatusErrorString() + */ } VL53L0_HistogramMeasurementData_t; #define VL53L0_REF_SPAD_BUFFER_SIZE 6 @@ -399,14 +426,16 @@ typedef struct { /*!< Reference array sigma value in 1/100th of [mm] e.g. 100 = 1mm */ uint16_t SigmaEstEffPulseWidth; /*!< Effective Pulse width for sigma estimate in 1/100th - * of ns e.g. 900 = 9.0ns */ + * of ns e.g. 900 = 9.0ns + */ uint16_t SigmaEstEffAmbWidth; /*!< Effective Ambient width for sigma estimate in 1/100th of ns - * e.g. 500 = 5.0ns */ + * e.g. 500 = 5.0ns + */ - uint8_t ReadDataFromDeviceDone; /* Indicate if read from device has - been done (==1) or not (==0) */ + /* Indicate if read from device has been done (==1) or not (==0) */ + uint8_t ReadDataFromDeviceDone; uint8_t ModuleId; /* Module ID */ uint8_t Revision; /* test Revision */ char ProductId[VL53L0_MAX_STRING_LENGTH]; @@ -416,8 +445,8 @@ typedef struct { uint8_t RefSpadsInitialised; /* reports if ref spads are initialised. */ uint32_t PartUIDUpper; /*!< Unique Part ID Upper */ uint32_t PartUIDLower; /*!< Unique Part ID Lower */ - FixPoint1616_t SignalRateMeasFixed400mm; /*!< Peek Signal rate - at 400 mm*/ + /*!< Peek Signal rate at 400 mm*/ + FixPoint1616_t SignalRateMeasFixed400mm; } VL53L0_DeviceSpecificParameters_t; @@ -430,8 +459,6 @@ typedef struct { * These must never access directly but only via macro */ typedef struct { - VL53L0_DMaxData_t DMaxData; - /*!< Dmax Data */ int32_t Part2PartOffsetNVMMicroMeter; /*!< backed up NVM value */ int32_t Part2PartOffsetAdjustmentNVMMicroMeter; @@ -458,15 +485,20 @@ typedef struct { /*!< Reference array sigma value in 1/100th of [mm] e.g. 100 = 1mm */ uint16_t SigmaEstEffPulseWidth; /*!< Effective Pulse width for sigma estimate in 1/100th - * of ns e.g. 900 = 9.0ns */ + * of ns e.g. 900 = 9.0ns + */ uint16_t SigmaEstEffAmbWidth; /*!< Effective Ambient width for sigma estimate in 1/100th of ns - * e.g. 500 = 5.0ns */ + * e.g. 500 = 5.0ns + */ + uint8_t StopVariable; + /*!< StopVariable used during the stop sequence */ uint16_t targetRefRate; /*!< Target Ambient Rate for Ref spad management */ FixPoint1616_t SigmaEstimate; /*!< Sigma Estimate - based on ambient & VCSEL rates and - * signal_total_events */ + * signal_total_events + */ FixPoint1616_t SignalEstimate; /*!< Signal Estimate - based on ambient & VCSEL rates and cross talk */ FixPoint1616_t LastSignalRefMcps; @@ -477,11 +509,6 @@ typedef struct { /*!< Indicate if we use Tuning Settings table */ uint16_t LinearityCorrectiveGain; /*!< Linearity Corrective Gain value in x1000 */ - uint16_t DmaxCalRangeMilliMeter; - /*!< Dmax Calibration Range millimeter */ - FixPoint1616_t DmaxCalSignalRateRtnMegaCps; - /*!< Dmax Calibration Signal Rate Return MegaCps */ - } VL53L0_DevData_t; @@ -520,7 +547,7 @@ typedef uint8_t VL53L0_VcselPeriod; * i.e. enabled/disabled. */ typedef struct { - uint8_t TccOn; /*! -#endif - -#ifdef VL53L0_EXTERNAL_USE -#define VL53L010_EXTERNAL -#else -#define VL53L010_EXTERNAL static -#endif - - -#define LOG_FUNCTION_START(fmt, ...) \ - _LOG_FUNCTION_START(TRACE_MODULE_API, fmt, ##__VA_ARGS__) -#define LOG_FUNCTION_END(status, ...) \ - _LOG_FUNCTION_END(TRACE_MODULE_API, status, ##__VA_ARGS__) -#define LOG_FUNCTION_END_FMT(status, fmt, ...) \ - _LOG_FUNCTION_END_FMT(TRACE_MODULE_API, status, fmt, ##__VA_ARGS__) - -#ifdef VL53L0_LOG_ENABLE -#define trace_print(level, ...) trace_print_module_function(TRACE_MODULE_API, \ - level, TRACE_FUNCTION_NONE, ##__VA_ARGS__) -#endif - - -/* Defines */ -#define VL53L010_SETPARAMETERFIELD(Dev, field, value) \ - do { \ - if (Status == VL53L0_ERROR_NONE) {\ - CurrentParameters = \ - PALDevDataGet(Dev, CurrentParameters); \ - CurrentParameters.field = value; \ - CurrentParameters = \ - PALDevDataSet(Dev, CurrentParameters, \ - CurrentParameters); \ - } \ - } while (0) -#define VL53L010_SETARRAYPARAMETERFIELD(Dev, field, index, value) \ - do { \ - if (Status == VL53L0_ERROR_NONE) {\ - CurrentParameters = \ - PALDevDataGet(Dev, CurrentParameters); \ - CurrentParameters.field[index] = value; \ - CurrentParameters = \ - PALDevDataSet(Dev, CurrentParameters, \ - CurrentParameters); \ - } \ - } while (0) - -#define VL53L010_GETPARAMETERFIELD(Dev, field, variable) \ - do { \ - if (Status == VL53L0_ERROR_NONE) { \ - CurrentParameters = \ - PALDevDataGet(Dev, CurrentParameters); \ - variable = CurrentParameters.field; \ - } \ - } while (0) - -#define VL53L010_GETARRAYPARAMETERFIELD(Dev, field, index, variable) \ - do { \ - if (Status == VL53L0_ERROR_NONE) { \ - CurrentParameters = \ - PALDevDataGet(Dev, CurrentParameters); \ - variable = CurrentParameters.field[index]; \ - } \ - } while (0) - -#define VL53L010_SETDEVICESPECIFICPARAMETER(Dev, field, value) \ - do { \ - if (Status == VL53L0_ERROR_NONE) { \ - DeviceSpecificParameters = \ - PALDevDataGet(Dev, DeviceSpecificParameters); \ - DeviceSpecificParameters.field = value; \ - DeviceSpecificParameters = \ - PALDevDataSet(Dev, DeviceSpecificParameters, \ - DeviceSpecificParameters); \ - } \ - } while (0) - -#define VL53L010_GETDEVICESPECIFICPARAMETER(Dev, field) \ - PALDevDataGet(Dev, DeviceSpecificParameters).field - -#define VL53L010_FIXPOINT1616TOFIXPOINT97(Value) \ - (uint16_t)((Value >> 9) & 0xFFFF) -#define VL53L010_FIXPOINT97TOFIXPOINT1616(Value) \ - (FixPoint1616_t)(Value << 9) -#define VL53L010_FIXPOINT1616TOFIXPOINT412(Value) \ - (uint16_t)((Value >> 4) & 0xFFFF) -#define VL53L010_FIXPOINT412TOFIXPOINT1616(Value) \ - (FixPoint1616_t)(Value << 4) -#define VL53L010_FIXPOINT1616TOFIXPOINT08(Value) \ - (uint8_t)((Value >> 8) & 0x00FF) -#define VL53L010_FIXPOINT08TOFIXPOINT1616(Value) \ - (FixPoint1616_t)(Value << 8) -#define VL53L010_MAKEUINT16(lsb, msb) \ - (uint16_t)((((uint16_t)msb) << 8) + (uint16_t)lsb) - -/* Internal functions declaration */ -VL53L010_EXTERNAL VL53L0_Error VL53L010_get_vcsel_pulse_period(VL53L0_DEV Dev, - uint8_t *pVCSELPulsePeriod, uint8_t RangeIndex); -VL53L010_EXTERNAL uint8_t VL53L010_encode_vcsel_period(uint8_t vcsel_period_pclks); -VL53L010_EXTERNAL uint8_t VL53L010_decode_vcsel_period(uint8_t vcsel_period_reg); -VL53L010_EXTERNAL uint16_t VL53L010_calc_encoded_timeout(VL53L0_DEV Dev, - uint32_t timeout_period_us, uint8_t vcsel_period); -VL53L010_EXTERNAL uint32_t VL53L010_calc_ranging_wait_us(VL53L0_DEV Dev, - uint16_t timeout_overall_periods, uint8_t vcsel_period); -VL53L010_EXTERNAL VL53L0_Error VL53L010_load_additional_settings1(VL53L0_DEV Dev); -VL53L010_EXTERNAL VL53L0_Error VL53L010_load_additional_settings3(VL53L0_DEV Dev); -VL53L010_EXTERNAL VL53L0_Error VL53L010_check_part_used(VL53L0_DEV Dev, - uint8_t *Revision, VL53L0_DeviceInfo_t *pVL53L0_DeviceInfo); -VL53L010_EXTERNAL VL53L0_Error VL53L010_get_info_from_device(VL53L0_DEV Dev); -VL53L010_EXTERNAL VL53L0_Error VL53L010_device_read_strobe(VL53L0_DEV Dev); -VL53L010_EXTERNAL VL53L0_Error VL53L010_get_pal_range_status(VL53L0_DEV Dev, - uint8_t DeviceRangeStatus, - FixPoint1616_t SignalRate, - FixPoint1616_t CrosstalkCompensation, - uint16_t EffectiveSpadRtnCount, - VL53L0_RangingMeasurementData_t - *pRangingMeasurementData, - uint8_t *pPalRangeStatus); - -/* Group PAL General Functions */ -VL53L0_Error VL53L010_GetVersion(VL53L0_Version_t *pVersion) -{ - VL53L0_Error Status = VL53L0_ERROR_NONE; - - LOG_FUNCTION_START(""); - - pVersion->major = VL53L010_IMPLEMENTATION_VER_MAJOR; - pVersion->minor = VL53L010_IMPLEMENTATION_VER_MINOR; - pVersion->build = VL53L010_IMPLEMENTATION_VER_SUB; - - pVersion->revision = VL53L0_IMPLEMENTATION_VER_REVISION; - - LOG_FUNCTION_END(Status); - return Status; -} - -VL53L0_Error VL53L010_GetPalSpecVersion(VL53L0_Version_t *pPalSpecVersion) -{ - VL53L0_Error Status = VL53L0_ERROR_NONE; - - LOG_FUNCTION_START(""); - - pPalSpecVersion->major = VL53L010_SPECIFICATION_VER_MAJOR; - pPalSpecVersion->minor = VL53L010_SPECIFICATION_VER_MINOR; - pPalSpecVersion->build = VL53L010_SPECIFICATION_VER_SUB; - - pPalSpecVersion->revision = VL53L010_SPECIFICATION_VER_REVISION; - - LOG_FUNCTION_END(Status); - return Status; -} - -VL53L0_Error VL53L010_GetDeviceInfo(VL53L0_DEV Dev, - VL53L0_DeviceInfo_t *pVL53L0_DeviceInfo) -{ - VL53L0_Error Status = VL53L0_ERROR_NONE; - uint8_t model_id; - uint8_t Revision; - - LOG_FUNCTION_START(""); - - Status = VL53L010_check_part_used(Dev, &Revision, pVL53L0_DeviceInfo); - - if (Status == VL53L0_ERROR_NONE) { - if (Revision == 0) { - VL53L0_COPYSTRING(pVL53L0_DeviceInfo->Name, - VL53L010_STRING_DEVICE_INFO_NAME_TS0); - } else if ((Revision <= 34) && (Revision != 32)) { - VL53L0_COPYSTRING(pVL53L0_DeviceInfo->Name, - VL53L010_STRING_DEVICE_INFO_NAME_TS1); - } else if (Revision < 39) { - VL53L0_COPYSTRING(pVL53L0_DeviceInfo->Name, - VL53L010_STRING_DEVICE_INFO_NAME_TS2); - } else { - VL53L0_COPYSTRING(pVL53L0_DeviceInfo->Name, - VL53L010_STRING_DEVICE_INFO_NAME_ES1); - } - - VL53L0_COPYSTRING(pVL53L0_DeviceInfo->Type, - VL53L010_STRING_DEVICE_INFO_TYPE); - } - - if (Status == VL53L0_ERROR_NONE) { - Status = VL53L0_RdByte(Dev, VL53L010_REG_IDENTIFICATION_MODEL_ID, - &pVL53L0_DeviceInfo->ProductType); - } - - if (Status == VL53L0_ERROR_NONE) { - Status = VL53L0_RdByte(Dev, VL53L010_REG_IDENTIFICATION_REVISION_ID, - &model_id); - pVL53L0_DeviceInfo->ProductRevisionMajor = 1; - pVL53L0_DeviceInfo->ProductRevisionMinor = (model_id & 0xF0) >> 4; - } - - LOG_FUNCTION_END(Status); - return Status; -} - -VL53L0_Error VL53L010_GetDeviceErrorStatus(VL53L0_DEV Dev, - VL53L010_DeviceError *pDeviceErrorStatus) -{ - VL53L0_Error Status = VL53L0_ERROR_NONE; - uint8_t RangeStatus; - - LOG_FUNCTION_START(""); - - Status = VL53L0_RdByte(Dev, VL53L010_REG_RESULT_RANGE_STATUS, - &RangeStatus); - - *pDeviceErrorStatus = (VL53L0_DeviceError)((RangeStatus & 0x78) >> 3); - - LOG_FUNCTION_END(Status); - return Status; -} - -#define VL53L010_BUILDSTATUSERRORSTRING(BUFFER, ERRORCODE, STRINGVALUE) \ - case ERRORCODE: \ - VL53L0_COPYSTRING(BUFFER, STRINGVALUE);\ - break;\ - -VL53L0_Error VL53L010_GetDeviceErrorString(VL53L0_DeviceError ErrorCode, - char *pDeviceErrorString) -{ - VL53L0_Error Status = VL53L0_ERROR_NONE; - - LOG_FUNCTION_START(""); - - switch (ErrorCode) { - VL53L010_BUILDSTATUSERRORSTRING(pDeviceErrorString, - VL53L010_DEVICEERROR_NONE, - VL53L010_STRING_DEVICEERROR_NONE); - VL53L010_BUILDSTATUSERRORSTRING(pDeviceErrorString, - VL53L010_DEVICEERROR_VCSELCONTINUITYTESTFAILURE, - VL53L010_STRING_DEVICEERROR_VCSELCONTINUITYTESTFAILURE); - VL53L010_BUILDSTATUSERRORSTRING(pDeviceErrorString, - VL53L010_DEVICEERROR_VCSELWATCHDOGTESTFAILURE, - VL53L010_STRING_DEVICEERROR_VCSELWATCHDOGTESTFAILURE); - VL53L010_BUILDSTATUSERRORSTRING(pDeviceErrorString, - VL53L010_DEVICEERROR_NOVHVVALUEFOUND, - VL53L010_STRING_DEVICEERROR_NOVHVVALUEFOUND); - VL53L010_BUILDSTATUSERRORSTRING(pDeviceErrorString, - VL53L010_DEVICEERROR_MSRCNOTARGET, - VL53L010_STRING_DEVICEERROR_MSRCNOTARGET); - VL53L010_BUILDSTATUSERRORSTRING(pDeviceErrorString, - VL53L010_DEVICEERROR_MSRCMINIMUMSNR, - VL53L010_STRING_DEVICEERROR_MSRCMINIMUMSNR); - VL53L010_BUILDSTATUSERRORSTRING(pDeviceErrorString, - VL53L010_DEVICEERROR_MSRCWRAPAROUND, - VL53L010_STRING_DEVICEERROR_MSRCWRAPAROUND); - VL53L010_BUILDSTATUSERRORSTRING(pDeviceErrorString, - VL53L010_DEVICEERROR_TCC, VL53L010_STRING_DEVICEERROR_TCC); - VL53L010_BUILDSTATUSERRORSTRING(pDeviceErrorString, - VL53L010_DEVICEERROR_RANGEAWRAPAROUND, - VL53L010_STRING_DEVICEERROR_RANGEAWRAPAROUND); - VL53L010_BUILDSTATUSERRORSTRING(pDeviceErrorString, - VL53L010_DEVICEERROR_RANGEBWRAPAROUND, - VL53L010_STRING_DEVICEERROR_RANGEBWRAPAROUND); - VL53L010_BUILDSTATUSERRORSTRING(pDeviceErrorString, - VL53L010_DEVICEERROR_MINCLIP, - VL53L010_STRING_DEVICEERROR_MINCLIP); - VL53L010_BUILDSTATUSERRORSTRING(pDeviceErrorString, - VL53L010_DEVICEERROR_RANGECOMPLETE, - VL53L010_STRING_DEVICEERROR_RANGECOMPLETE); - VL53L010_BUILDSTATUSERRORSTRING(pDeviceErrorString, - VL53L010_DEVICEERROR_ALGOUNDERFLOW, - VL53L010_STRING_DEVICEERROR_ALGOUNDERFLOW); - VL53L010_BUILDSTATUSERRORSTRING(pDeviceErrorString, - VL53L010_DEVICEERROR_ALGOOVERFLOW, - VL53L010_STRING_DEVICEERROR_ALGOOVERFLOW); - VL53L010_BUILDSTATUSERRORSTRING(pDeviceErrorString, - VL53L010_DEVICEERROR_FINALSNRLIMIT, - VL53L010_STRING_DEVICEERROR_FINALSNRLIMIT); - VL53L010_BUILDSTATUSERRORSTRING(pDeviceErrorString, - VL53L010_DEVICEERROR_NOTARGETIGNORE, - VL53L010_STRING_DEVICEERROR_NOTARGETIGNORE); - default: - VL53L0_COPYSTRING(pDeviceErrorString, - VL53L010_STRING_UNKNOW_ERROR_CODE); - } - - LOG_FUNCTION_END(Status); - return Status; -} - -VL53L0_Error VL53L010_GetPalErrorString(VL53L0_Error PalErrorCode, - char *pPalErrorString) -{ - VL53L0_Error Status = VL53L0_ERROR_NONE; - - LOG_FUNCTION_START(""); - - switch (PalErrorCode) { - VL53L010_BUILDSTATUSERRORSTRING(pPalErrorString, - VL53L0_ERROR_NONE, VL53L010_STRING_ERROR_NONE); - VL53L010_BUILDSTATUSERRORSTRING(pPalErrorString, - VL53L0_ERROR_CALIBRATION_WARNING, - VL53L010_STRING_ERROR_CALIBRATION_WARNING); - VL53L010_BUILDSTATUSERRORSTRING(pPalErrorString, - VL53L0_ERROR_MIN_CLIPPED, - VL53L010_STRING_ERROR_MIN_CLIPPED); - VL53L010_BUILDSTATUSERRORSTRING(pPalErrorString, - VL53L0_ERROR_UNDEFINED, VL53L010_STRING_ERROR_UNDEFINED); - VL53L010_BUILDSTATUSERRORSTRING(pPalErrorString, - VL53L0_ERROR_INVALID_PARAMS, - VL53L010_STRING_ERROR_INVALID_PARAMS); - VL53L010_BUILDSTATUSERRORSTRING(pPalErrorString, - VL53L0_ERROR_NOT_SUPPORTED, - VL53L010_STRING_ERROR_NOT_SUPPORTED); - VL53L010_BUILDSTATUSERRORSTRING(pPalErrorString, - VL53L0_ERROR_RANGE_ERROR, - VL53L010_STRING_ERROR_RANGE_ERROR); - VL53L010_BUILDSTATUSERRORSTRING(pPalErrorString, - VL53L0_ERROR_TIME_OUT, - VL53L010_STRING_ERROR_TIME_OUT); - VL53L010_BUILDSTATUSERRORSTRING(pPalErrorString, - VL53L0_ERROR_MODE_NOT_SUPPORTED, - VL53L010_STRING_ERROR_MODE_NOT_SUPPORTED); - VL53L010_BUILDSTATUSERRORSTRING(pPalErrorString, - VL53L0_ERROR_NOT_IMPLEMENTED, - VL53L010_STRING_ERROR_NOT_IMPLEMENTED); - VL53L010_BUILDSTATUSERRORSTRING(pPalErrorString, - VL53L0_ERROR_BUFFER_TOO_SMALL, - VL53L010_STRING_ERROR_BUFFER_TOO_SMALL); - VL53L010_BUILDSTATUSERRORSTRING(pPalErrorString, - VL53L0_ERROR_GPIO_NOT_EXISTING, - VL53L010_STRING_ERROR_GPIO_NOT_EXISTING); - VL53L010_BUILDSTATUSERRORSTRING(pPalErrorString, - VL53L0_ERROR_GPIO_FUNCTIONALITY_NOT_SUPPORTED, - VL53L010_STRING_ERROR_GPIO_FUNCTIONALITY_NOT_SUPPORTED); - VL53L010_BUILDSTATUSERRORSTRING(pPalErrorString, - VL53L0_ERROR_CONTROL_INTERFACE, - VL53L010_STRING_ERROR_CONTROL_INTERFACE); - default: - VL53L0_COPYSTRING(pPalErrorString, - VL53L010_STRING_UNKNOW_ERROR_CODE); - break; - } - - LOG_FUNCTION_END(Status); - return Status; -} - - -VL53L0_Error VL53L010_GetPalState(VL53L0_DEV Dev, VL53L0_State *pPalState) -{ - VL53L0_Error Status = VL53L0_ERROR_NONE; - - LOG_FUNCTION_START(""); - - *pPalState = PALDevDataGet(Dev, PalState); - - LOG_FUNCTION_END(Status); - return Status; -} - -VL53L0_Error VL53L010_SetPowerMode(VL53L0_DEV Dev, VL53L0_PowerModes PowerMode) -{ - VL53L0_Error Status = VL53L0_ERROR_NONE; - - LOG_FUNCTION_START(""); - - /* Only level1 of Power mode exists */ - if ((PowerMode != VL53L0_POWERMODE_STANDBY_LEVEL1) && - (PowerMode != VL53L0_POWERMODE_IDLE_LEVEL1)) { - Status = VL53L0_ERROR_MODE_NOT_SUPPORTED; - } else if (PowerMode == VL53L0_POWERMODE_STANDBY_LEVEL1) { - /* set the standby level1 of power mode */ - Status = VL53L0_WrByte(Dev, 0x80, 0x00); - if (Status == VL53L0_ERROR_NONE) { - /* Set PAL State to standby */ - PALDevDataSet(Dev, PalState, VL53L0_STATE_STANDBY); - PALDevDataSet(Dev, PowerMode, - VL53L0_POWERMODE_STANDBY_LEVEL1); - } - - } else { - /* VL53L0_POWERMODE_IDLE_LEVEL1 */ - Status = VL53L0_WrByte(Dev, 0x80, 0x01); - if (Status == VL53L0_ERROR_NONE) - Status = VL53L010_StaticInit(Dev); - if (Status == VL53L0_ERROR_NONE) - PALDevDataSet(Dev, PowerMode, - VL53L0_POWERMODE_IDLE_LEVEL1); - } - - LOG_FUNCTION_END(Status); - return Status; -} - -VL53L0_Error VL53L010_GetPowerMode(VL53L0_DEV Dev, VL53L0_PowerModes *pPowerMode) -{ - VL53L0_Error Status = VL53L0_ERROR_NONE; - uint8_t Byte; - - LOG_FUNCTION_START(""); - - /* Only level1 of Power mode exists */ - Status = VL53L0_RdByte(Dev, 0x80, &Byte); - - if (Status == VL53L0_ERROR_NONE) { - if (Byte == 1) - PALDevDataSet(Dev, PowerMode, - VL53L0_POWERMODE_IDLE_LEVEL1); - else - PALDevDataSet(Dev, PowerMode, - VL53L0_POWERMODE_STANDBY_LEVEL1); - } - - LOG_FUNCTION_END(Status); - return Status; -} - -VL53L0_Error VL53L010_SetOffsetCalibrationDataMicroMeter(VL53L0_DEV Dev, - int32_t OffsetCalibrationDataMicroMeter) -{ - VL53L0_Error Status = VL53L0_ERROR_NONE; - uint8_t OffsetCalibrationData; - - LOG_FUNCTION_START(""); - - OffsetCalibrationData = (uint8_t) (OffsetCalibrationDataMicroMeter - / 1000); - Status = VL53L0_WrByte(Dev, VL53L010_REG_ALGO_PART_TO_PART_RANGE_OFFSET, - *(uint8_t *) &OffsetCalibrationData); - - LOG_FUNCTION_END(Status); - return Status; -} - -VL53L0_Error VL53L010_GetOffsetCalibrationDataMicroMeter(VL53L0_DEV Dev, - int32_t *pOffsetCalibrationDataMicroMeter) -{ - VL53L0_Error Status = VL53L0_ERROR_NONE; - uint8_t RangeOffsetRegister; - - LOG_FUNCTION_START(""); - - Status = VL53L0_RdByte(Dev, VL53L010_REG_ALGO_PART_TO_PART_RANGE_OFFSET, - &RangeOffsetRegister); - if (Status == VL53L0_ERROR_NONE) { - *pOffsetCalibrationDataMicroMeter = - (*((int8_t *) (&RangeOffsetRegister))) * 1000; - } - - LOG_FUNCTION_END(Status); - return Status; -} - -VL53L0_Error VL53L010_SetGroupParamHold(VL53L0_DEV Dev, uint8_t GroupParamHold) -{ - VL53L0_Error Status = VL53L0_ERROR_NOT_IMPLEMENTED; - - LOG_FUNCTION_START(""); - - /* not implemented on VL53L0 */ - - LOG_FUNCTION_END(Status); - return Status; -} - -VL53L0_Error VL53L010_GetUpperLimitMilliMeter(VL53L0_DEV Dev, - uint16_t *pUpperLimitMilliMeter) -{ - VL53L0_Error Status = VL53L0_ERROR_NOT_IMPLEMENTED; - - LOG_FUNCTION_START(""); - - /* not implemented on VL53L0 */ - - LOG_FUNCTION_END(Status); - return Status; -} -/* End Group PAL General Functions */ - -/* Group PAL Init Functions */ -VL53L0_Error VL53L010_SetDeviceAddress(VL53L0_DEV Dev, uint8_t DeviceAddress) -{ - VL53L0_Error Status = VL53L0_ERROR_NONE; - - LOG_FUNCTION_START(""); - - Status = VL53L0_WrByte(Dev, VL53L010_REG_I2C_SLAVE_DEVICE_ADDRESS, - DeviceAddress / 2); - - LOG_FUNCTION_END(Status); - return Status; -} - -VL53L0_Error VL53L010_DataInit(VL53L0_DEV Dev) -{ - VL53L0_Error Status = VL53L0_ERROR_NONE; - VL53L0_DeviceParameters_t CurrentParameters; - VL53L0_DeviceSpecificParameters_t DeviceSpecificParameters; - int32_t OffsetCalibrationData; - - LOG_FUNCTION_START(""); - - if (Status == VL53L0_ERROR_NONE) { - /* read device info */ - VL53L010_SETDEVICESPECIFICPARAMETER(Dev, ReadDataFromDeviceDone, 0); - - Status = VL53L010_get_info_from_device(Dev); - } - - /* Set Default static parameters */ - /* set first temporary values 11.3999MHz * 65536 = 748421 */ - VL53L010_SETDEVICESPECIFICPARAMETER(Dev, OscFrequencyMHz, 748421); - /* 11.3999MHz * 65536 = 748421 */ - - /* Get default parameters */ - Status = VL53L010_GetDeviceParameters(Dev, &CurrentParameters); - if (Status == VL53L0_ERROR_NONE) { - /* initialize PAL values */ - CurrentParameters.DeviceMode = VL53L0_DEVICEMODE_SINGLE_RANGING; - CurrentParameters.HistogramMode = VL53L0_HISTOGRAMMODE_DISABLED; - PALDevDataSet(Dev, CurrentParameters, CurrentParameters); - } - - /* Sigma estimator variable */ - PALDevDataSet(Dev, SigmaEstRefArray, 100); - PALDevDataSet(Dev, SigmaEstEffPulseWidth, 900); - PALDevDataSet(Dev, SigmaEstEffAmbWidth, 500); - - /* Set Signal and Sigma check */ - if (Status == VL53L0_ERROR_NONE) { - Status = VL53L010_SetLimitCheckEnable(Dev, - VL53L010_CHECKENABLE_SIGMA_FINAL_RANGE, 0); - } - if (Status == VL53L0_ERROR_NONE) { - Status = VL53L010_SetLimitCheckEnable(Dev, - VL53L010_CHECKENABLE_SIGNAL_RATE_FINAL_RANGE, 0); - } - if(Status == VL53L0_ERROR_NONE) { - Status = VL53L010_SetLimitCheckValue(Dev, - VL53L010_CHECKENABLE_SIGMA_FINAL_RANGE, - (FixPoint1616_t)(32<<16)); - } - if (Status == VL53L0_ERROR_NONE) { - Status = VL53L010_SetLimitCheckValue(Dev, - VL53L010_CHECKENABLE_SIGNAL_RATE_FINAL_RANGE, - (FixPoint1616_t)(25 * 65536 / 100)); - /* 0.25 * 65538 */ - } - - /* Read back NVM offset */ - if (Status == VL53L0_ERROR_NONE) { - Status = VL53L010_GetOffsetCalibrationDataMicroMeter(Dev, - &OffsetCalibrationData); - } - - if (Status == VL53L0_ERROR_NONE) { - PALDevDataSet(Dev, Part2PartOffsetNVMMicroMeter, - OffsetCalibrationData); - - PALDevDataSet(Dev, SequenceConfig, 0xFF); - - /* Set PAL state to tell that we are waiting for call - to VL53L010_StaticInit */ - PALDevDataSet(Dev, PalState, VL53L0_STATE_WAIT_STATICINIT); - } - - LOG_FUNCTION_END(Status); - return Status; -} - - -VL53L0_Error VL53L010_StaticInit(VL53L0_DEV Dev) -{ - VL53L0_Error Status = VL53L0_ERROR_NONE; - VL53L0_DeviceSpecificParameters_t DeviceSpecificParameters; - VL53L0_DeviceParameters_t CurrentParameters; - uint16_t TempWord; - uint8_t TempByte; - uint8_t localBuffer[32]; - uint8_t i; - uint8_t Revision; - - LOG_FUNCTION_START(""); - - /* Set I2C standard mode */ - if (Status == VL53L0_ERROR_NONE) - Status = VL53L0_WrByte(Dev, 0x88, 0x00); - - /* this function do nothing if it has been called before */ - Status = VL53L010_get_info_from_device(Dev); - - if (Status == VL53L0_ERROR_NONE) { - Revision = VL53L010_GETDEVICESPECIFICPARAMETER(Dev, Revision); - } - - if (Status == VL53L0_ERROR_NONE) { - if (Revision == 0) - Status = VL53L010_load_additional_settings1(Dev); - } - - /* update13_05_15 */ - if (Status == VL53L0_ERROR_NONE) { - if ((Revision <= 34) && (Revision != 32)) { - - for (i = 0; i < 32; i++) - localBuffer[i] = 0xff; - - Status = VL53L0_WriteMulti(Dev, 0x90, localBuffer, 32); - - Status |= VL53L0_WrByte(Dev, 0xb6, 16); - Status |= VL53L0_WrByte(Dev, 0xb0, 0x0); - Status |= VL53L0_WrByte(Dev, 0xb1, 0x0); - Status |= VL53L0_WrByte(Dev, 0xb2, 0xE0); - Status |= VL53L0_WrByte(Dev, 0xb3, 0xE0); - Status |= VL53L0_WrByte(Dev, 0xb4, 0xE0); - Status |= VL53L0_WrByte(Dev, 0xb5, 0xE0); - } - } - - /* update 17_06_15_v10 */ - if (Status == VL53L0_ERROR_NONE) - Status = VL53L010_load_tuning_settings(Dev); - - /* check if GO1 power is ON after load default tuning */ - if (Status == VL53L0_ERROR_NONE) { - Status = VL53L0_RdByte(Dev, 0x80, &TempByte); - if ((TempByte != 0) && (Status == VL53L0_ERROR_NONE)) { - /* update 07_05_15 */ - Status = VL53L010_load_additional_settings3(Dev); - } - } - - /* Set interrupt config to new sample ready */ - if (Status == VL53L0_ERROR_NONE) { - Status = VL53L010_SetGpioConfig(Dev, 0, 0, - VL53L010_REG_SYSTEM_INTERRUPT_GPIO_NEW_SAMPLE_READY, - VL53L0_INTERRUPTPOLARITY_LOW); - } - - if (Status == VL53L0_ERROR_NONE) { - Status = VL53L0_WrByte(Dev, 0xFF, 0x01); - Status |= VL53L0_RdWord(Dev, 0x84, &TempWord); - Status |= VL53L0_WrByte(Dev, 0xFF, 0x00); - } - - if (Status == VL53L0_ERROR_NONE) { - VL53L010_SETDEVICESPECIFICPARAMETER(Dev, OscFrequencyMHz, - VL53L010_FIXPOINT412TOFIXPOINT1616(TempWord)); - } - - /* After static init, some device parameters may be changed, - so update them */ - if (Status == VL53L0_ERROR_NONE) - Status = VL53L010_GetDeviceParameters(Dev, &CurrentParameters); - - if (Status == VL53L0_ERROR_NONE) - PALDevDataSet(Dev, CurrentParameters, CurrentParameters); - - - /* read the sequence config and save it */ - if (Status == VL53L0_ERROR_NONE) { - Status = VL53L0_RdByte(Dev, VL53L010_REG_SYSTEM_SEQUENCE_CONFIG, - &TempByte); - if (Status == VL53L0_ERROR_NONE) - PALDevDataSet(Dev, SequenceConfig, TempByte); - - } - - - if (Status == VL53L0_ERROR_NONE) - Status = VL53L010_PerformRefCalibration(Dev); - - /* Set PAL State to standby */ - if (Status == VL53L0_ERROR_NONE) - PALDevDataSet(Dev, PalState, VL53L0_STATE_IDLE); - - LOG_FUNCTION_END(Status); - return Status; -} - -VL53L0_Error VL53L010_WaitDeviceBooted(VL53L0_DEV Dev) -{ - VL53L0_Error Status = VL53L0_ERROR_NOT_IMPLEMENTED; - - LOG_FUNCTION_START(""); - - /* not implemented on VL53L0 */ - - LOG_FUNCTION_END(Status); - return Status; -} - -VL53L0_Error VL53L010_ResetDevice(VL53L0_DEV Dev) -{ - VL53L0_Error Status = VL53L0_ERROR_NONE; - uint8_t Byte; - - LOG_FUNCTION_START(""); - - /* Set reset bit */ - Status = VL53L0_WrByte(Dev, VL53L010_REG_SOFT_RESET_GO2_SOFT_RESET_N, - 0x00); - - /* Wait for some time */ - if (Status == VL53L0_ERROR_NONE) { - do { - Status = VL53L0_RdByte(Dev, - VL53L010_REG_IDENTIFICATION_MODEL_ID, &Byte); - } while (Byte != 0x00); - } - - /* Release reset */ - Status = VL53L0_WrByte(Dev, VL53L010_REG_SOFT_RESET_GO2_SOFT_RESET_N, - 0x01); - - /* Wait until correct boot-up of the device */ - if (Status == VL53L0_ERROR_NONE) { - do { - Status = VL53L0_RdByte(Dev, - VL53L010_REG_IDENTIFICATION_MODEL_ID, &Byte); - } while (Byte == 0x00); - } - - LOG_FUNCTION_END(Status); - return Status; -} -/* End Group PAL Init Functions */ - -/* Group PAL Parameters Functions */ -VL53L0_Error VL53L010_SetDeviceParameters(VL53L0_DEV Dev, - const VL53L0_DeviceParameters_t* - pDeviceParameters) -{ - VL53L0_Error Status = VL53L0_ERROR_NONE; - int i; - - LOG_FUNCTION_START(""); - - Status = VL53L010_SetDeviceMode(Dev, pDeviceParameters->DeviceMode); - - if (Status == VL53L0_ERROR_NONE) { - Status = VL53L010_SetHistogramMode(Dev, - pDeviceParameters->HistogramMode); - } - - if (Status == VL53L0_ERROR_NONE) { - Status = VL53L010_SetInterMeasurementPeriodMilliSeconds(Dev, - - pDeviceParameters->InterMeasurementPeriodMilliSeconds); - } - - if (Status == VL53L0_ERROR_NONE) { - Status = VL53L010_SetXTalkCompensationEnable(Dev, - - pDeviceParameters->XTalkCompensationEnable); - } - if (Status == VL53L0_ERROR_NONE) { - Status = VL53L010_SetXTalkCompensationRateMegaCps(Dev, - - pDeviceParameters->XTalkCompensationRateMegaCps); - } - - if (Status == VL53L0_ERROR_NONE) { - Status = VL53L010_SetOffsetCalibrationDataMicroMeter(Dev, - pDeviceParameters->RangeOffsetMicroMeters); - } - - for (i = 0; i < VL53L010_CHECKENABLE_NUMBER_OF_CHECKS; i++) { - if (Status == VL53L0_ERROR_NONE) { - Status |= VL53L010_SetLimitCheckEnable(Dev, i, - pDeviceParameters->LimitChecksEnable[i]); - } else { - break; - } - if (Status == VL53L0_ERROR_NONE) { - Status |= VL53L010_SetLimitCheckValue(Dev, i, - pDeviceParameters->LimitChecksValue[i]); - } else { - break; - } - } - - if (Status == VL53L0_ERROR_NONE) { - Status = VL53L010_SetWrapAroundCheckEnable(Dev, - - pDeviceParameters->WrapAroundCheckEnable); - } - - if (Status == VL53L0_ERROR_NONE) { - Status = VL53L010_SetMeasurementTimingBudgetMicroSeconds(Dev, - - pDeviceParameters->MeasurementTimingBudgetMicroSeconds); - } - - - LOG_FUNCTION_END(Status); - return Status; -} - -VL53L0_Error VL53L010_GetDeviceParameters(VL53L0_DEV Dev, - VL53L0_DeviceParameters_t *pDeviceParameters) -{ - VL53L0_Error Status = VL53L0_ERROR_NONE; - int i; - - LOG_FUNCTION_START(""); - - Status = VL53L010_GetDeviceMode(Dev, &(pDeviceParameters->DeviceMode)); - - if (Status == VL53L0_ERROR_NONE) { - Status = VL53L010_GetHistogramMode(Dev, - &(pDeviceParameters->HistogramMode)); - } - if (Status == VL53L0_ERROR_NONE) { - Status = VL53L010_GetInterMeasurementPeriodMilliSeconds(Dev, - &(pDeviceParameters->InterMeasurementPeriodMilliSeconds)); - } - if (Status == VL53L0_ERROR_NONE) { - Status = VL53L010_GetXTalkCompensationEnable(Dev, - &(pDeviceParameters->XTalkCompensationEnable)); - } - if (Status == VL53L0_ERROR_NONE) { - Status = VL53L010_GetXTalkCompensationRateMegaCps(Dev, - - &(pDeviceParameters->XTalkCompensationRateMegaCps)); - } - - if (Status == VL53L0_ERROR_NONE) { - Status = VL53L010_GetOffsetCalibrationDataMicroMeter(Dev, - &(pDeviceParameters->RangeOffsetMicroMeters)); - } - - if (Status == VL53L0_ERROR_NONE) { - for (i = 0; i < VL53L010_CHECKENABLE_NUMBER_OF_CHECKS; i++) { - // get first the values, then the enables. - // VL53L0_GetLimitCheckValue will modify the enable flags - if (Status == VL53L0_ERROR_NONE) { - Status |= VL53L010_GetLimitCheckValue(Dev, i, - &(pDeviceParameters->LimitChecksValue[i])); - } else { - break; - } - if (Status == VL53L0_ERROR_NONE) { - Status |= VL53L010_GetLimitCheckEnable(Dev, i, - &(pDeviceParameters->LimitChecksEnable[i])); - } else { - break; - } - } - } - - if (Status == VL53L0_ERROR_NONE) { - Status = VL53L010_GetWrapAroundCheckEnable(Dev, - &(pDeviceParameters->WrapAroundCheckEnable)); - } - - /* Need to be done at the end as it uses VCSELPulsePeriod */ - if (Status == VL53L0_ERROR_NONE) { - Status = VL53L010_GetMeasurementTimingBudgetMicroSeconds(Dev, - &(pDeviceParameters->MeasurementTimingBudgetMicroSeconds)); - } - - LOG_FUNCTION_END(Status); - return Status; -} - -VL53L0_Error VL53L010_SetDeviceMode(VL53L0_DEV Dev, - VL53L0_DeviceModes DeviceMode) -{ - VL53L0_Error Status = VL53L0_ERROR_NONE; - VL53L0_DeviceParameters_t CurrentParameters; - - LOG_FUNCTION_START("%d", (int)DeviceMode); - - switch (DeviceMode) { - case VL53L0_DEVICEMODE_SINGLE_RANGING: - case VL53L0_DEVICEMODE_CONTINUOUS_RANGING: - case VL53L0_DEVICEMODE_CONTINUOUS_TIMED_RANGING: - case VL53L0_DEVICEMODE_SINGLE_HISTOGRAM: - case VL53L0_DEVICEMODE_GPIO_DRIVE: - case VL53L0_DEVICEMODE_GPIO_OSC: - /* Supported mode */ - VL53L010_SETPARAMETERFIELD(Dev, DeviceMode, DeviceMode); - break; - default: - /* Unsupported mode */ - Status = VL53L0_ERROR_MODE_NOT_SUPPORTED; - } - - LOG_FUNCTION_END(Status); - return Status; -} - -VL53L0_Error VL53L010_GetDeviceMode(VL53L0_DEV Dev, - VL53L0_DeviceModes *pDeviceMode) -{ - VL53L0_Error Status = VL53L0_ERROR_NONE; - VL53L0_DeviceParameters_t CurrentParameters; - - LOG_FUNCTION_START(""); - - VL53L010_GETPARAMETERFIELD(Dev, DeviceMode, *pDeviceMode); - - LOG_FUNCTION_END(Status); - return Status; -} - -VL53L0_Error VL53L010_SetHistogramMode(VL53L0_DEV Dev, - VL53L0_HistogramModes HistogramMode) -{ - VL53L0_Error Status = VL53L0_ERROR_NONE; - VL53L0_DeviceParameters_t CurrentParameters; - - LOG_FUNCTION_START("%d", (int)HistogramMode); - - switch (HistogramMode) { - case VL53L0_HISTOGRAMMODE_DISABLED: - /* Supported mode */ - VL53L010_SETPARAMETERFIELD(Dev, HistogramMode, - HistogramMode); - break; - case VL53L0_HISTOGRAMMODE_REFERENCE_ONLY: - case VL53L0_HISTOGRAMMODE_RETURN_ONLY: - case VL53L0_HISTOGRAMMODE_BOTH: - default: - /* Unsupported mode */ - Status = VL53L0_ERROR_MODE_NOT_SUPPORTED; - } - - LOG_FUNCTION_END(Status); - return Status; -} - -VL53L0_Error VL53L010_GetHistogramMode(VL53L0_DEV Dev, - VL53L0_HistogramModes *pHistogramMode) -{ - VL53L0_Error Status = VL53L0_ERROR_NONE; - VL53L0_DeviceParameters_t CurrentParameters; - - LOG_FUNCTION_START(""); - - VL53L010_GETPARAMETERFIELD(Dev, HistogramMode, *pHistogramMode); - - LOG_FUNCTION_END(Status); - return Status; -} - -VL53L0_Error VL53L010_SetMeasurementTimingBudgetMicroSeconds(VL53L0_DEV Dev, - uint32_t MeasurementTimingBudgetMicroSeconds) -{ - VL53L0_Error Status = VL53L0_ERROR_NONE; - VL53L0_DeviceParameters_t CurrentParameters; - VL53L0_DeviceSpecificParameters_t DeviceSpecificParameters; - uint8_t CurrentVCSELPulsePeriod; - uint8_t CurrentVCSELPulsePeriodPClk; - uint8_t Byte; - uint32_t NewTimingBudgetMicroSeconds; - uint16_t encodedTimeOut; - - LOG_FUNCTION_START(""); - - /* check if rangeB is done: */ - Status = VL53L010_GetWrapAroundCheckEnable(Dev, &Byte); - - if (Status == VL53L0_ERROR_NONE) { - if (((Byte == 1) && (MeasurementTimingBudgetMicroSeconds < -17000)) || - ((Byte == 0) && (MeasurementTimingBudgetMicroSeconds < -12000))) { - Status = VL53L0_ERROR_INVALID_PARAMS; - } - } - - if (Status == VL53L0_ERROR_NONE) { - NewTimingBudgetMicroSeconds = -MeasurementTimingBudgetMicroSeconds - - 7000; - if (Byte == 1) { - NewTimingBudgetMicroSeconds = - (uint32_t)(NewTimingBudgetMicroSeconds >> 1); - } - } - - if (Status == VL53L0_ERROR_NONE) { - Status = VL53L010_get_vcsel_pulse_period(Dev, - &CurrentVCSELPulsePeriodPClk, 0); - } - - if (Status == VL53L0_ERROR_NONE) { - CurrentVCSELPulsePeriod = VL53L010_encode_vcsel_period( - CurrentVCSELPulsePeriodPClk); - encodedTimeOut = VL53L010_calc_encoded_timeout(Dev, - NewTimingBudgetMicroSeconds, - (uint8_t) - CurrentVCSELPulsePeriod); - VL53L010_SETPARAMETERFIELD(Dev, -MeasurementTimingBudgetMicroSeconds, - MeasurementTimingBudgetMicroSeconds); - VL53L010_SETDEVICESPECIFICPARAMETER(Dev, LastEncodedTimeout, - encodedTimeOut); - } - - /* Program in register */ - if (Status == VL53L0_ERROR_NONE) { - Status = VL53L0_WrWord(Dev, VL53L010_REG_RNGA_TIMEOUT_MSB, - encodedTimeOut); - } - - /* Temp: program same value for rangeB1 and rangeB2 */ - /* Range B1 */ - if (Status == VL53L0_ERROR_NONE) { - Status = VL53L010_get_vcsel_pulse_period(Dev, - &CurrentVCSELPulsePeriodPClk, 1); - if (Status == VL53L0_ERROR_NONE) { - CurrentVCSELPulsePeriod = VL53L010_encode_vcsel_period( - CurrentVCSELPulsePeriodPClk); - encodedTimeOut = VL53L010_calc_encoded_timeout(Dev, - NewTimingBudgetMicroSeconds, - (uint8_t) - CurrentVCSELPulsePeriod); - } - } - - if (Status == VL53L0_ERROR_NONE) { - Status = VL53L0_WrWord(Dev, VL53L010_REG_RNGB1_TIMEOUT_MSB, - encodedTimeOut); - } - - /* Range B2 */ - if (Status == VL53L0_ERROR_NONE) { - Status = VL53L010_get_vcsel_pulse_period(Dev, - &CurrentVCSELPulsePeriodPClk, 2); - if (Status == VL53L0_ERROR_NONE) { - CurrentVCSELPulsePeriod = VL53L010_encode_vcsel_period( - CurrentVCSELPulsePeriodPClk); - encodedTimeOut = VL53L010_calc_encoded_timeout(Dev, - NewTimingBudgetMicroSeconds, - (uint8_t) - CurrentVCSELPulsePeriod); - } - } - - if (Status == VL53L0_ERROR_NONE) { - Status = VL53L0_WrWord(Dev, VL53L010_REG_RNGB2_TIMEOUT_MSB, - encodedTimeOut); - } - - LOG_FUNCTION_END(Status); - return Status; -} - -VL53L0_Error VL53L010_GetMeasurementTimingBudgetMicroSeconds(VL53L0_DEV Dev, - uint32_t *pMeasurementTimingBudgetMicroSeconds) -{ - VL53L0_Error Status = VL53L0_ERROR_NONE; - VL53L0_DeviceParameters_t CurrentParameters; - uint8_t CurrentVCSELPulsePeriod; - uint8_t CurrentVCSELPulsePeriodPClk; - uint16_t encodedTimeOut; - uint32_t RangATimingBudgetMicroSeconds = 0; - uint32_t RangBTimingBudgetMicroSeconds = 0; - uint8_t Byte; - - LOG_FUNCTION_START(""); - - /* check if rangeB is done: */ - Status = VL53L010_GetWrapAroundCheckEnable(Dev, &Byte); - - if (Status == VL53L0_ERROR_NONE) { - VL53L010_get_vcsel_pulse_period(Dev, - &CurrentVCSELPulsePeriodPClk, 0); - CurrentVCSELPulsePeriod = VL53L010_encode_vcsel_period( - CurrentVCSELPulsePeriodPClk); - - /* Read from register */ - Status = VL53L0_RdWord(Dev, VL53L010_REG_RNGA_TIMEOUT_MSB, - &encodedTimeOut); - if (Status == VL53L0_ERROR_NONE) { - RangATimingBudgetMicroSeconds = - VL53L010_calc_ranging_wait_us(Dev, - encodedTimeOut, - CurrentVCSELPulsePeriod); - } - } - - if (Status == VL53L0_ERROR_NONE) { - if (Byte == 0) { - *pMeasurementTimingBudgetMicroSeconds = - RangATimingBudgetMicroSeconds + 7000; - VL53L010_SETPARAMETERFIELD(Dev, - MeasurementTimingBudgetMicroSeconds, - *pMeasurementTimingBudgetMicroSeconds); - } else { - VL53L010_get_vcsel_pulse_period(Dev, - &CurrentVCSELPulsePeriodPClk, 1); - CurrentVCSELPulsePeriod = VL53L010_encode_vcsel_period( - CurrentVCSELPulsePeriodPClk); - - /* Read from register */ - Status = VL53L0_RdWord(Dev, - VL53L010_REG_RNGB1_TIMEOUT_MSB, - &encodedTimeOut); - if (Status == VL53L0_ERROR_NONE) { - RangBTimingBudgetMicroSeconds = - VL53L010_calc_ranging_wait_us( - Dev, encodedTimeOut, - CurrentVCSELPulsePeriod); - } - - *pMeasurementTimingBudgetMicroSeconds = - RangATimingBudgetMicroSeconds + - RangBTimingBudgetMicroSeconds + - 7000; - VL53L010_SETPARAMETERFIELD(Dev, - MeasurementTimingBudgetMicroSeconds, - *pMeasurementTimingBudgetMicroSeconds); - } - } - - - - LOG_FUNCTION_END(Status); - return Status; -} - -VL53L0_Error VL53L010_SetInterMeasurementPeriodMilliSeconds(VL53L0_DEV Dev, - uint32_t InterMeasurementPeriodMilliSeconds) -{ - VL53L0_Error Status = VL53L0_ERROR_NONE; - VL53L0_DeviceParameters_t CurrentParameters; - uint16_t osc_calibrate_val; - uint32_t IMPeriodMilliSeconds; - - LOG_FUNCTION_START(""); - - Status = VL53L0_RdWord(Dev, VL53L010_REG_OSC_CALIBRATE_VAL, - &osc_calibrate_val); - - if (Status == VL53L0_ERROR_NONE) { - - if (osc_calibrate_val != 0) { - - IMPeriodMilliSeconds = - InterMeasurementPeriodMilliSeconds * - osc_calibrate_val; - } else { - IMPeriodMilliSeconds = - InterMeasurementPeriodMilliSeconds; - } - Status = VL53L0_WrDWord(Dev, - VL53L010_REG_SYSTEM_INTERMEASUREMENT_PERIOD, - IMPeriodMilliSeconds); - } - - if (Status == VL53L0_ERROR_NONE) { - VL53L010_SETPARAMETERFIELD(Dev, - InterMeasurementPeriodMilliSeconds, - InterMeasurementPeriodMilliSeconds); - } - - LOG_FUNCTION_END(Status); - return Status; -} - -VL53L0_Error VL53L010_GetInterMeasurementPeriodMilliSeconds(VL53L0_DEV Dev, - uint32_t *pInterMeasurementPeriodMilliSeconds) -{ - VL53L0_Error Status = VL53L0_ERROR_NONE; - VL53L0_DeviceParameters_t CurrentParameters; - uint16_t osc_calibrate_val; - uint32_t IMPeriodMilliSeconds; - - LOG_FUNCTION_START(""); - - Status = VL53L0_RdWord(Dev, VL53L010_REG_OSC_CALIBRATE_VAL, - &osc_calibrate_val); - - if (Status == VL53L0_ERROR_NONE) { - Status = VL53L0_RdDWord(Dev, - VL53L010_REG_SYSTEM_INTERMEASUREMENT_PERIOD, - &IMPeriodMilliSeconds); - } - - if (Status == VL53L0_ERROR_NONE) { - if (osc_calibrate_val != 0) - *pInterMeasurementPeriodMilliSeconds = - IMPeriodMilliSeconds / - osc_calibrate_val; - - VL53L010_SETPARAMETERFIELD(Dev, - InterMeasurementPeriodMilliSeconds, - *pInterMeasurementPeriodMilliSeconds); - } - - LOG_FUNCTION_END(Status); - return Status; -} - -VL53L0_Error VL53L010_SetXTalkCompensationEnable(VL53L0_DEV Dev, - uint8_t XTalkCompensationEnable) -{ - VL53L0_Error Status = VL53L0_ERROR_NONE; - VL53L0_DeviceParameters_t CurrentParameters; - uint8_t XTalkCompensationEnableValue; - - LOG_FUNCTION_START(""); - - if (XTalkCompensationEnable == 0) { - /* Disable the crosstalk compensation */ - XTalkCompensationEnableValue = 0x00; - } else { - /* Enable the crosstalk compensation */ - XTalkCompensationEnableValue = 0x01; - } - Status = VL53L0_UpdateByte(Dev, VL53L010_REG_ALGO_RANGE_CHECK_ENABLES, - 0xFE, - XTalkCompensationEnableValue); - if (Status == VL53L0_ERROR_NONE) { - VL53L010_SETPARAMETERFIELD(Dev, XTalkCompensationEnable, - XTalkCompensationEnableValue); - } - - LOG_FUNCTION_END(Status); - return Status; -} - -VL53L0_Error VL53L010_GetXTalkCompensationEnable(VL53L0_DEV Dev, uint8_t* - pXTalkCompensationEnable) -{ - VL53L0_Error Status = VL53L0_ERROR_NONE; - VL53L0_DeviceParameters_t CurrentParameters; - uint8_t data; - uint8_t Temp; - - LOG_FUNCTION_START(""); - - Status = VL53L0_RdByte(Dev, VL53L010_REG_ALGO_RANGE_CHECK_ENABLES, &data); - if (Status == VL53L0_ERROR_NONE) { - if (data & 0x01) - Temp = 0x01; - else - Temp = 0x00; - - *pXTalkCompensationEnable = Temp; - } - if (Status == VL53L0_ERROR_NONE) - VL53L010_SETPARAMETERFIELD(Dev, XTalkCompensationEnable, Temp); - - LOG_FUNCTION_END(Status); - return Status; -} - -VL53L0_Error VL53L010_SetXTalkCompensationRateMegaCps(VL53L0_DEV Dev, - FixPoint1616_t XTalkCompensationRateMegaCps) -{ - VL53L0_Error Status = VL53L0_ERROR_NONE; - VL53L0_DeviceParameters_t CurrentParameters; - - LOG_FUNCTION_START(""); - - Status = VL53L0_WrWord(Dev, VL53L010_REG_ALGO_CROSSTALK_COMPENSATION_RATE, - VL53L010_FIXPOINT1616TOFIXPOINT412(XTalkCompensationRateMegaCps)); - if (Status == VL53L0_ERROR_NONE) { - VL53L010_SETPARAMETERFIELD(Dev, XTalkCompensationRateMegaCps, - XTalkCompensationRateMegaCps); - } - - LOG_FUNCTION_END(Status); - return Status; -} - -VL53L0_Error VL53L010_GetXTalkCompensationRateMegaCps(VL53L0_DEV Dev, - FixPoint1616_t *pXTalkCompensationRateMegaCps) -{ - VL53L0_Error Status = VL53L0_ERROR_NONE; - uint16_t Value; - FixPoint1616_t TempFix1616; - VL53L0_DeviceParameters_t CurrentParameters; - - LOG_FUNCTION_START(""); - - Status = VL53L0_RdWord(Dev, VL53L010_REG_ALGO_CROSSTALK_COMPENSATION_RATE, - (uint16_t *) &Value); - if (Status == VL53L0_ERROR_NONE) { - TempFix1616 = VL53L010_FIXPOINT412TOFIXPOINT1616(Value); - *pXTalkCompensationRateMegaCps = TempFix1616; - VL53L010_SETPARAMETERFIELD(Dev, XTalkCompensationRateMegaCps, - TempFix1616); - } - - LOG_FUNCTION_END(Status); - return Status; -} - - - - -/* - * CHECK LIMIT FUNCTIONS - */ - -VL53L0_Error VL53L010_GetNumberOfLimitCheck(uint16_t *pNumberOfLimitCheck) -{ - VL53L0_Error Status = VL53L0_ERROR_NONE; - - LOG_FUNCTION_START(""); - - *pNumberOfLimitCheck = VL53L010_CHECKENABLE_NUMBER_OF_CHECKS; - - LOG_FUNCTION_END(Status); - return Status; -} - - -#define VL53L010_BUILDCASESTRING(BUFFER, CODE, STRINGVALUE) \ - do { \ - case CODE: \ - VL53L0_COPYSTRING(BUFFER, STRINGVALUE); \ - break; \ - } while (0) - -VL53L0_Error VL53L010_GetLimitCheckInfo(VL53L0_DEV Dev, uint16_t LimitCheckId, - char *pLimitCheckString) -{ - VL53L0_Error Status = VL53L0_ERROR_NONE; - - LOG_FUNCTION_START(""); - - switch (LimitCheckId) { - VL53L010_BUILDCASESTRING(pLimitCheckString, - VL53L010_CHECKENABLE_SIGMA_FINAL_RANGE, - VL53L010_STRING_CHECKENABLE_SIGMA); - VL53L010_BUILDCASESTRING(pLimitCheckString, - VL53L010_CHECKENABLE_SIGNAL_RATE_FINAL_RANGE, - VL53L010_STRING_CHECKENABLE_SIGNAL_RATE); - - default: - VL53L0_COPYSTRING(pLimitCheckString, - VL53L010_STRING_UNKNOW_ERROR_CODE); - } - - LOG_FUNCTION_END(Status); - return Status; -} - -VL53L0_Error VL53L010_SetLimitCheckEnable(VL53L0_DEV Dev, uint16_t LimitCheckId, - uint8_t LimitCheckEnable) -{ - VL53L0_Error Status = VL53L0_ERROR_NONE; - VL53L0_DeviceParameters_t CurrentParameters; - - LOG_FUNCTION_START(""); - - if (LimitCheckId >= VL53L010_CHECKENABLE_NUMBER_OF_CHECKS) { - Status = VL53L0_ERROR_INVALID_PARAMS; - } else { - if (LimitCheckEnable == 0) { - VL53L010_SETARRAYPARAMETERFIELD(Dev, LimitChecksEnable, - LimitCheckId, 0); - } else { - VL53L010_SETARRAYPARAMETERFIELD(Dev, LimitChecksEnable, - LimitCheckId, 1); - } - } - - LOG_FUNCTION_END(Status); - return Status; -} - -VL53L0_Error VL53L010_GetLimitCheckEnable(VL53L0_DEV Dev, uint16_t LimitCheckId, - uint8_t *pLimitCheckEnable) -{ - VL53L0_Error Status = VL53L0_ERROR_NONE; - VL53L0_DeviceParameters_t CurrentParameters; - uint8_t Temp8; - - LOG_FUNCTION_START(""); - - if (LimitCheckId >= VL53L010_CHECKENABLE_NUMBER_OF_CHECKS) { - Status = VL53L0_ERROR_INVALID_PARAMS; - } else { - VL53L010_GETARRAYPARAMETERFIELD(Dev, LimitChecksEnable, LimitCheckId, Temp8); - *pLimitCheckEnable = Temp8; - } - - LOG_FUNCTION_END(Status); - return Status; -} - -VL53L0_Error VL53L010_SetLimitCheckValue(VL53L0_DEV Dev, - uint16_t LimitCheckId, FixPoint1616_t LimitCheckValue) -{ - VL53L0_Error Status = VL53L0_ERROR_NONE; - VL53L0_DeviceParameters_t CurrentParameters; - - LOG_FUNCTION_START(""); - - if (LimitCheckId >= VL53L010_CHECKENABLE_NUMBER_OF_CHECKS) { - Status = VL53L0_ERROR_INVALID_PARAMS; - } else { - VL53L010_SETARRAYPARAMETERFIELD(Dev, LimitChecksValue, LimitCheckId, - LimitCheckValue); - } - - LOG_FUNCTION_END(Status); - return Status; -} - -VL53L0_Error VL53L010_GetLimitCheckValue(VL53L0_DEV Dev, - uint16_t LimitCheckId, FixPoint1616_t *pLimitCheckValue) -{ - VL53L0_Error Status = VL53L0_ERROR_NONE; - VL53L0_DeviceParameters_t CurrentParameters; - - - LOG_FUNCTION_START(""); - - if (LimitCheckId >= VL53L010_CHECKENABLE_NUMBER_OF_CHECKS) { - Status = VL53L0_ERROR_INVALID_PARAMS; - } else { - VL53L010_GETARRAYPARAMETERFIELD(Dev, LimitChecksValue, - LimitCheckId, *pLimitCheckValue); - } - - LOG_FUNCTION_END(Status); - return Status; - -} - -VL53L0_Error VL53L010_GetLimitCheckCurrent(VL53L0_DEV Dev, - uint16_t LimitCheckId, FixPoint1616_t *pLimitCheckCurrent) -{ - VL53L0_Error Status = VL53L0_ERROR_NONE; - - LOG_FUNCTION_START(""); - - if (LimitCheckId >= VL53L010_CHECKENABLE_NUMBER_OF_CHECKS) { - Status = VL53L0_ERROR_INVALID_PARAMS; - } else { - switch (LimitCheckId) { - case VL53L010_CHECKENABLE_SIGMA_FINAL_RANGE: - /* Need to run a ranging to have the latest values */ - *pLimitCheckCurrent = PALDevDataGet(Dev, SigmaEstimate); - - break; - - case VL53L010_CHECKENABLE_SIGNAL_RATE_FINAL_RANGE: - /* Need to run a ranging to have the latest values */ - *pLimitCheckCurrent = PALDevDataGet(Dev, SignalEstimate); - - break; - default: - Status = VL53L0_ERROR_INVALID_PARAMS; - } - } - - LOG_FUNCTION_END(Status); - return Status; - -} - -/* - * WRAPAROUND LIMIT - */ -VL53L0_Error VL53L010_SetWrapAroundCheckEnable(VL53L0_DEV Dev, uint8_t - WrapAroundCheckEnable) -{ - VL53L0_Error Status = VL53L0_ERROR_NONE; - uint8_t Byte; - uint8_t WrapAroundCheckEnableInt; - VL53L0_DeviceParameters_t CurrentParameters; - - LOG_FUNCTION_START(""); - - Status = VL53L0_RdByte(Dev, VL53L010_REG_SYSTEM_SEQUENCE_CONFIG, &Byte); - if (WrapAroundCheckEnable == 0) { - /* Disable wraparound */ - Byte = Byte & 0x7F; - WrapAroundCheckEnableInt = 0; - } else { - /* Enable wraparound */ - Byte = Byte | 0x80; - WrapAroundCheckEnableInt = 1; - } - - Status = VL53L0_WrByte(Dev, VL53L010_REG_SYSTEM_SEQUENCE_CONFIG, Byte); - - if (Status == VL53L0_ERROR_NONE) { - PALDevDataSet(Dev, SequenceConfig, Byte); - VL53L010_SETPARAMETERFIELD(Dev, WrapAroundCheckEnable, - WrapAroundCheckEnableInt); - } - - LOG_FUNCTION_END(Status); - return Status; -} - -VL53L0_Error VL53L010_GetWrapAroundCheckEnable(VL53L0_DEV Dev, uint8_t - *pWrapAroundCheckEnable) -{ - VL53L0_Error Status = VL53L0_ERROR_NONE; - uint8_t data; - VL53L0_DeviceParameters_t CurrentParameters; - - LOG_FUNCTION_START(""); - - Status = VL53L0_RdByte(Dev, VL53L010_REG_SYSTEM_SEQUENCE_CONFIG, &data); - if (Status == VL53L0_ERROR_NONE) { - PALDevDataSet(Dev, SequenceConfig, data); - if (data & (0x01 << 7)) - *pWrapAroundCheckEnable = 0x01; - else - *pWrapAroundCheckEnable = 0x00; - - } - if (Status == VL53L0_ERROR_NONE) { - VL53L010_SETPARAMETERFIELD(Dev, WrapAroundCheckEnable, - *pWrapAroundCheckEnable); - } - - LOG_FUNCTION_END(Status); - return Status; -} - -/* End Group PAL Parameters Functions */ - -/* Group PAL Measurement Functions */ -VL53L0_Error VL53L010_PerformSingleMeasurement(VL53L0_DEV Dev) -{ - VL53L0_Error Status = VL53L0_ERROR_NONE; - VL53L0_DeviceModes DeviceMode; - uint8_t NewDatReady = 0; - uint32_t LoopNb; - - LOG_FUNCTION_START(""); - - /* Get Current DeviceMode */ - Status = VL53L010_GetDeviceMode(Dev, &DeviceMode); - - /* Start immediately to run a single ranging measurement in case of - single ranging or single histogram */ - if ((Status == VL53L0_ERROR_NONE) && - ((DeviceMode == VL53L0_DEVICEMODE_SINGLE_RANGING) || - (DeviceMode == - VL53L0_DEVICEMODE_SINGLE_HISTOGRAM))) { - Status = VL53L010_StartMeasurement(Dev); - } - - /* Wait until it finished - use timeout to avoid deadlock - */ - if (Status == VL53L0_ERROR_NONE) { - LoopNb = 0; - do { - Status = VL53L010_GetMeasurementDataReady(Dev, - &NewDatReady); - if ((NewDatReady == 0x01) || Status != - VL53L0_ERROR_NONE) { - break; - } - LoopNb = LoopNb + 1; - VL53L0_PollingDelay(Dev); - } while (LoopNb < VL53L0_DEFAULT_MAX_LOOP); - - if (LoopNb >= VL53L0_DEFAULT_MAX_LOOP) - Status = VL53L0_ERROR_TIME_OUT; - - } - - /* Change PAL State in case of single ranging or single histogram - */ - if ((Status == VL53L0_ERROR_NONE) && - ((DeviceMode == VL53L0_DEVICEMODE_SINGLE_RANGING) || - (DeviceMode == - VL53L0_DEVICEMODE_SINGLE_HISTOGRAM))) { - PALDevDataSet(Dev, PalState, VL53L0_STATE_IDLE); - } - - LOG_FUNCTION_END(Status); - return Status; -} - -VL53L0_Error VL53L010_PerformRefCalibration(VL53L0_DEV Dev) -{ - VL53L0_Error Status = VL53L0_ERROR_NONE; - uint8_t NewDatReady = 0; - uint8_t Byte = 0; - uint8_t SequenceConfig = 0; - uint32_t LoopNb; - - LOG_FUNCTION_START(""); - - /* store the value of the sequence config, - * this will be reset before the end of the function - */ - - SequenceConfig = PALDevDataGet(Dev, SequenceConfig); - - Status = VL53L0_WrByte(Dev, VL53L010_REG_SYSTEM_SEQUENCE_CONFIG, 0x03); - - if (Status == VL53L0_ERROR_NONE) { - PALDevDataSet(Dev, SequenceConfig, 0x03); - Status = VL53L0_WrByte(Dev, VL53L010_REG_SYSRANGE_START, - VL53L010_REG_SYSRANGE_MODE_START_STOP); - } - - if (Status == VL53L0_ERROR_NONE) { - /* Wait until start bit has been cleared */ - LoopNb = 0; - do { - if (LoopNb > 0) - Status = VL53L0_RdByte(Dev, - VL53L010_REG_SYSRANGE_START, &Byte); - LoopNb = LoopNb + 1; - } while (((Byte & VL53L010_REG_SYSRANGE_MODE_START_STOP) == - VL53L010_REG_SYSRANGE_MODE_START_STOP) && - (Status == VL53L0_ERROR_NONE) && - (LoopNb < VL53L0_DEFAULT_MAX_LOOP)); - } - - /* Wait until it finished - use timeout to avoid deadlock - */ - if (Status == VL53L0_ERROR_NONE) { - LoopNb = 0; - do { - Status = VL53L010_GetMeasurementDataReady(Dev, - &NewDatReady); - if ((NewDatReady == 0x01) || Status != - VL53L0_ERROR_NONE) { - break; - } - LoopNb = LoopNb + 1; - VL53L0_PollingDelay(Dev); - } while (LoopNb < VL53L0_DEFAULT_MAX_LOOP); - - if (LoopNb >= VL53L0_DEFAULT_MAX_LOOP) - Status = VL53L0_ERROR_TIME_OUT; - - } - - if (Status == VL53L0_ERROR_NONE) { - Status = VL53L0_WrByte(Dev, 0xFF, 0x01); - Status |= VL53L0_WrByte(Dev, 0x00, 0x00); - - Status |= VL53L0_WrByte(Dev, 0xFF, 0x04); - Status |= VL53L0_RdByte(Dev, 0x30, &Byte); - - Status |= VL53L0_WrByte(Dev, 0xFF, 0x00); - Status |= VL53L0_WrByte(Dev, 0x31, Byte); - Status |= VL53L0_WrByte(Dev, 0xFF, 0x01); - Status |= VL53L0_WrByte(Dev, 0x00, 0x01); - Status |= VL53L0_WrByte(Dev, 0xFF, 0x00); - } - - if (Status == VL53L0_ERROR_NONE) - Status = VL53L010_ClearInterruptMask(Dev, 0); - - - if (Status == VL53L0_ERROR_NONE) { - /* restore the previous Sequence Config */ - Status = VL53L0_WrByte(Dev, VL53L010_REG_SYSTEM_SEQUENCE_CONFIG, - SequenceConfig); - } - if (Status == VL53L0_ERROR_NONE) - PALDevDataSet(Dev, SequenceConfig, SequenceConfig); - - - LOG_FUNCTION_END(Status); - return Status; -} - -VL53L010_API VL53L0_Error VL53L010_PerformXTalkCalibration(VL53L0_DEV Dev, - FixPoint1616_t XTalkCalDistance, - FixPoint1616_t *pXTalkCompensationRateMegaCps) -{ - VL53L0_Error Status = VL53L0_ERROR_NONE; - uint16_t sum_ranging = 0; - uint16_t sum_spads = 0; - FixPoint1616_t sum_signalRate = 0; - FixPoint1616_t total_count = 0; - uint8_t xtalk_meas = 0; - VL53L0_RangingMeasurementData_t RangingMeasurementData; - FixPoint1616_t xTalkStoredMeanSignalRate; - FixPoint1616_t xTalkStoredMeanRange; - FixPoint1616_t xTalkStoredMeanRtnSpads; - uint32_t signalXTalkTotalPerSpad; - uint32_t xTalkStoredMeanRtnSpadsAsInt; - uint32_t xTalkCalDistanceAsInt; - FixPoint1616_t XTalkCompensationRateMegaCps; - - LOG_FUNCTION_START(""); - - if (XTalkCalDistance <= 0) - Status = VL53L0_ERROR_INVALID_PARAMS; - - - /* Disable the XTalk compensation */ - if (Status == VL53L0_ERROR_NONE) - Status = VL53L010_SetXTalkCompensationEnable(Dev, 0); - - - /* Perform 50 measurements and compute the averages */ - if (Status == VL53L0_ERROR_NONE) { - sum_ranging = 0; - sum_spads = 0; - sum_signalRate = 0; - total_count = 0; - for (xtalk_meas = 0; xtalk_meas < 50; xtalk_meas++) { - Status = VL53L010_PerformSingleRangingMeasurement(Dev, - &RangingMeasurementData); - - if (Status != VL53L0_ERROR_NONE) - break; - - /* The range is valid when RangeStatus = 0 */ - if (RangingMeasurementData.RangeStatus == 0) { - sum_ranging = sum_ranging + - RangingMeasurementData.RangeMilliMeter; - sum_signalRate = sum_signalRate + - RangingMeasurementData.SignalRateRtnMegaCps; - sum_spads = sum_spads + - RangingMeasurementData.EffectiveSpadRtnCount - / 32; - total_count = total_count + 1; - } - } - - if (total_count == 0) { - /* no valid values found */ - Status = VL53L0_ERROR_DIVISION_BY_ZERO; - } - } - - - if (Status == VL53L0_ERROR_NONE) { - /* FixPoint1616_t / uint16_t = FixPoint1616_t */ - xTalkStoredMeanSignalRate = sum_signalRate / total_count; - xTalkStoredMeanRange = - (FixPoint1616_t)((uint32_t)(sum_ranging << 16) / - total_count); - xTalkStoredMeanRtnSpads = - (FixPoint1616_t)((uint32_t)(sum_spads<<16) / - total_count); - - /* Round Mean Spads to Whole Number. - Typically the calculated mean SPAD count is a whole number or - very close to a whole - number, therefore any truncation will not result in a - significant loss in accuracy. - Also, for a grey target at a typical distance of around 400mm, - around 220 SPADs will - be enabled, therefore, any truncation will result in a loss of - accuracy of less than 0.5%. - */ - xTalkStoredMeanRtnSpadsAsInt = (xTalkStoredMeanRtnSpads + - 0x8000) >> 16; - - /* Round Cal Distance to Whole Number. - Note that the cal distance is in mm, therefore no resolution is - lost. - */ - xTalkCalDistanceAsInt = (XTalkCalDistance + 0x8000) >> 16; - - if (xTalkStoredMeanRtnSpadsAsInt == 0 || xTalkCalDistanceAsInt - == 0 || - xTalkStoredMeanRange >= XTalkCalDistance) { - XTalkCompensationRateMegaCps = 0; - } else { - /* Round Cal Distance to Whole Number. - Note that the cal distance is in mm, therefore no - resolution is lost. - */ - xTalkCalDistanceAsInt = (XTalkCalDistance + 0x8000) >> - 16; - - /* Apply division by mean spad count early in the - calculation to keep the numbers small. - This ensures we can maintain a 32bit calculation. - Fixed1616 / int := Fixed1616 - */ - signalXTalkTotalPerSpad = - (xTalkStoredMeanSignalRate) / - xTalkStoredMeanRtnSpadsAsInt; - - /* Complete the calculation for total Signal XTalk per - SPAD Fixed1616 * (Fixed1616 - Fixed1616/int) - := (2^16 * Fixed1616) - */ - signalXTalkTotalPerSpad *= ((1<<16) - - (xTalkStoredMeanRange/xTalkCalDistanceAsInt)); - - /* Round from 2^16 * Fixed1616, to Fixed1616. */ - XTalkCompensationRateMegaCps = (signalXTalkTotalPerSpad - + 0x8000) >> 16; - } - - *pXTalkCompensationRateMegaCps = XTalkCompensationRateMegaCps; - - /* Enable the XTalk compensation */ - if (Status == VL53L0_ERROR_NONE) - Status = VL53L010_SetXTalkCompensationEnable(Dev, 1); - - - /* Enable the XTalk compensation */ - if (Status == VL53L0_ERROR_NONE) { - Status = VL53L010_SetXTalkCompensationRateMegaCps(Dev, - XTalkCompensationRateMegaCps); - } - - } - - LOG_FUNCTION_END(Status); - return Status; -} - -VL53L010_API VL53L0_Error VL53L010_PerformOffsetCalibration(VL53L0_DEV Dev, - FixPoint1616_t CalDistanceMilliMeter, - int32_t* pOffsetMicroMeter) { - VL53L0_Error Status = VL53L0_ERROR_NONE; - uint16_t sum_ranging = 0; - FixPoint1616_t total_count = 0; - VL53L0_RangingMeasurementData_t RangingMeasurementData; - FixPoint1616_t StoredMeanRange; - uint32_t StoredMeanRangeAsInt; - VL53L0_DeviceParameters_t CurrentParameters; - uint32_t CalDistanceAsInt_mm; - int meas = 0; - LOG_FUNCTION_START(""); - - if (CalDistanceMilliMeter<=0) { - Status = VL53L0_ERROR_INVALID_PARAMS; - } - - if (Status == VL53L0_ERROR_NONE) { - VL53L010_SetOffsetCalibrationDataMicroMeter(Dev, 0); - } - - /* Perform 50 measurements and compute the averages */ - if (Status == VL53L0_ERROR_NONE) { - sum_ranging = 0; - total_count = 0; - for(meas=0;meas<50;meas++) - { - Status = VL53L010_PerformSingleRangingMeasurement(Dev, &RangingMeasurementData); - - if (Status != VL53L0_ERROR_NONE) { - break; - } - - /* The range is valid when RangeStatus = 0 */ - if (RangingMeasurementData.RangeStatus == 0) { - sum_ranging = sum_ranging + RangingMeasurementData.RangeMilliMeter; - total_count = total_count + 1; - } - } - - if (total_count == 0) { - /* no valid values found */ - Status = VL53L0_ERROR_RANGE_ERROR; - } - } - - - if (Status == VL53L0_ERROR_NONE) { - /* FixPoint1616_t / uint16_t = FixPoint1616_t */ - StoredMeanRange = (FixPoint1616_t)((uint32_t)(sum_ranging<<16) / total_count); - - StoredMeanRangeAsInt = (StoredMeanRange + 0x8000) >> 16; - - /* Round Cal Distance to Whole Number. - * Note that the cal distance is in mm, therefore no resolution is lost.*/ - CalDistanceAsInt_mm = (CalDistanceMilliMeter + 0x8000) >> 16; - - *pOffsetMicroMeter = (CalDistanceAsInt_mm - StoredMeanRangeAsInt) * 1000; - - /* Apply the calculated offset */ - if (Status == VL53L0_ERROR_NONE) { - VL53L010_SETPARAMETERFIELD(Dev, RangeOffsetMicroMeters, *pOffsetMicroMeter); - Status = VL53L010_SetOffsetCalibrationDataMicroMeter(Dev, *pOffsetMicroMeter); - } - - } - - LOG_FUNCTION_END(Status); - return Status; -} - -VL53L0_Error VL53L010_StartMeasurement(VL53L0_DEV Dev) -{ - VL53L0_Error Status = VL53L0_ERROR_NONE; - VL53L0_DeviceModes DeviceMode; - uint8_t Byte = 0; - uint32_t LoopNb; - - LOG_FUNCTION_START(""); - - /* Get Current DeviceMode */ - VL53L010_GetDeviceMode(Dev, &DeviceMode); - - switch (DeviceMode) { - case VL53L0_DEVICEMODE_SINGLE_RANGING: - Status = VL53L0_WrByte(Dev, VL53L010_REG_SYSRANGE_START, - VL53L010_REG_SYSRANGE_MODE_SINGLESHOT | - VL53L010_REG_SYSRANGE_MODE_START_STOP); - break; - case VL53L0_DEVICEMODE_CONTINUOUS_RANGING: - /* Back-to-back mode */ - Status = VL53L0_WrByte(Dev, VL53L010_REG_SYSRANGE_START, - VL53L010_REG_SYSRANGE_MODE_BACKTOBACK | - VL53L010_REG_SYSRANGE_MODE_START_STOP); - if (Status == VL53L0_ERROR_NONE) { - /* Set PAL State to Running */ - PALDevDataSet(Dev, PalState, VL53L0_STATE_RUNNING); - } - break; - case VL53L0_DEVICEMODE_CONTINUOUS_TIMED_RANGING: - /* Continuous mode */ - Status = VL53L0_WrByte(Dev, VL53L010_REG_SYSRANGE_START, - VL53L010_REG_SYSRANGE_MODE_TIMED | - VL53L010_REG_SYSRANGE_MODE_START_STOP); - if (Status == VL53L0_ERROR_NONE) { - /* Set PAL State to Running */ - PALDevDataSet(Dev, PalState, VL53L0_STATE_RUNNING); - } - break; - default: - /* Selected mode not supported */ - Status = VL53L0_ERROR_MODE_NOT_SUPPORTED; - } - - if (Status == VL53L0_ERROR_NONE) { - /* Wait until start bit has been cleared */ - LoopNb = 0; - do { - if (LoopNb > 0) - Status = VL53L0_RdByte(Dev, - VL53L010_REG_SYSRANGE_START, &Byte); - LoopNb = LoopNb + 1; - } while (((Byte & VL53L010_REG_SYSRANGE_MODE_START_STOP) == - VL53L010_REG_SYSRANGE_MODE_START_STOP) && - (Status == VL53L0_ERROR_NONE) && - (LoopNb < VL53L0_DEFAULT_MAX_LOOP)); - - if (LoopNb >= VL53L0_DEFAULT_MAX_LOOP) - Status = VL53L0_ERROR_TIME_OUT; - - } - - LOG_FUNCTION_END(Status); - return Status; -} - -VL53L0_Error VL53L010_StopMeasurement(VL53L0_DEV Dev) -{ - VL53L0_Error Status = VL53L0_ERROR_NONE; - - LOG_FUNCTION_START(""); - - Status = VL53L0_WrByte(Dev, VL53L010_REG_SYSRANGE_START, - VL53L010_REG_SYSRANGE_MODE_SINGLESHOT); - - if (Status == VL53L0_ERROR_NONE) { - /* Set PAL State to Idle */ - PALDevDataSet(Dev, PalState, VL53L0_STATE_IDLE); - } - - LOG_FUNCTION_END(Status); - return Status; -} - -VL53L0_Error VL53L010_GetMeasurementDataReady(VL53L0_DEV Dev, uint8_t - *pMeasurementDataReady) -{ - VL53L0_Error Status = VL53L0_ERROR_NONE; - uint8_t SysRangeStatusRegister; - uint8_t InterruptConfig; - uint32_t InterruptMask; - - LOG_FUNCTION_START(""); - - InterruptConfig = VL53L010_GETDEVICESPECIFICPARAMETER(Dev, - Pin0GpioFunctionality); - - if (InterruptConfig == - VL53L010_REG_SYSTEM_INTERRUPT_GPIO_NEW_SAMPLE_READY) { - VL53L010_GetInterruptMaskStatus(Dev, &InterruptMask); - if (InterruptMask == - VL53L010_REG_SYSTEM_INTERRUPT_GPIO_NEW_SAMPLE_READY){ - *pMeasurementDataReady = 1; - } else { - *pMeasurementDataReady = 0; - } - } else { - Status = VL53L0_RdByte(Dev, VL53L010_REG_RESULT_RANGE_STATUS, - &SysRangeStatusRegister); - if (Status == VL53L0_ERROR_NONE) { - if (SysRangeStatusRegister & 0x01) - *pMeasurementDataReady = 1; - else - *pMeasurementDataReady = 0; - - } - } - - LOG_FUNCTION_END(Status); - return Status; -} - -VL53L0_Error VL53L010_WaitDeviceReadyForNewMeasurement(VL53L0_DEV Dev, uint32_t - MaxLoop) -{ - VL53L0_Error Status = VL53L0_ERROR_NOT_IMPLEMENTED; - - LOG_FUNCTION_START(""); - - /* not implemented for VL53L0 */ - - LOG_FUNCTION_END(Status); - return Status; -} - -VL53L0_Error VL53L010_GetRangingMeasurementData(VL53L0_DEV Dev, - VL53L0_RangingMeasurementData_t *pRangingMeasurementData) -{ - VL53L0_Error Status = VL53L0_ERROR_NONE; - uint8_t DeviceRangeStatus; - uint8_t PalRangeStatus; - uint16_t AmbientRate; - FixPoint1616_t SignalRate; - FixPoint1616_t CrosstalkCompensation; - uint16_t EffectiveSpadRtnCount; - uint8_t localBuffer[14]; - VL53L0_RangingMeasurementData_t LastRangeDataBuffer; - - LOG_FUNCTION_START(""); - - /* use multi read even if some registers are not useful, result will be - more efficient - start reading at 0x14 dec20 - end reading at 0x21 dec33 total 14 bytes to read - */ - Status = VL53L0_ReadMulti(Dev, 0x14, localBuffer, 14); - - if (Status == VL53L0_ERROR_NONE) { - - pRangingMeasurementData->ZoneId = 0; /* Only one zone */ - pRangingMeasurementData->TimeStamp = 0; /* Not Implemented */ - - pRangingMeasurementData->RangeMilliMeter = - VL53L010_MAKEUINT16(localBuffer[11], localBuffer[10]); - - pRangingMeasurementData->RangeDMaxMilliMeter = 0; - pRangingMeasurementData->RangeFractionalPart = 0; - pRangingMeasurementData->MeasurementTimeUsec = 0; - - SignalRate = - VL53L010_FIXPOINT97TOFIXPOINT1616( - VL53L010_MAKEUINT16(localBuffer[7], localBuffer[6])); - pRangingMeasurementData->SignalRateRtnMegaCps = SignalRate; - - AmbientRate = VL53L010_MAKEUINT16(localBuffer[9], localBuffer[8]); - pRangingMeasurementData->AmbientRateRtnMegaCps = - VL53L010_FIXPOINT97TOFIXPOINT1616(AmbientRate); - - EffectiveSpadRtnCount = VL53L010_MAKEUINT16(localBuffer[3], - localBuffer[2]); - pRangingMeasurementData->EffectiveSpadRtnCount = - EffectiveSpadRtnCount; - - DeviceRangeStatus = localBuffer[0]; - - /* initial format = 4.12, when pass to 16.16 from 9.7 we shift - 5 bit more this will be absorbed in the further computation - */ - CrosstalkCompensation = - VL53L010_FIXPOINT97TOFIXPOINT1616( - VL53L010_MAKEUINT16(localBuffer[13], localBuffer[12])); - - /* - * For a standard definition of RangeStatus, this should return - * 0 in case of good result after a ranging - * The range status depends on the device so call a device - * specific function to obtain the right Status. - */ - Status = VL53L010_get_pal_range_status(Dev, DeviceRangeStatus, - SignalRate, CrosstalkCompensation, - EffectiveSpadRtnCount, - pRangingMeasurementData, &PalRangeStatus); - - if (Status == VL53L0_ERROR_NONE) - pRangingMeasurementData->RangeStatus = PalRangeStatus; - - } - - if (Status == VL53L0_ERROR_NONE) { - /* Copy last read data into Dev buffer */ - LastRangeDataBuffer = PALDevDataGet(Dev, LastRangeMeasure); - - LastRangeDataBuffer.RangeMilliMeter = - pRangingMeasurementData->RangeMilliMeter; - LastRangeDataBuffer.RangeFractionalPart = - pRangingMeasurementData->RangeFractionalPart; - LastRangeDataBuffer.RangeDMaxMilliMeter = - pRangingMeasurementData->RangeDMaxMilliMeter; - LastRangeDataBuffer.MeasurementTimeUsec = - pRangingMeasurementData->MeasurementTimeUsec; - LastRangeDataBuffer.SignalRateRtnMegaCps = - pRangingMeasurementData->SignalRateRtnMegaCps; - LastRangeDataBuffer.AmbientRateRtnMegaCps = - pRangingMeasurementData->AmbientRateRtnMegaCps; - LastRangeDataBuffer.EffectiveSpadRtnCount = - pRangingMeasurementData->EffectiveSpadRtnCount; - LastRangeDataBuffer.RangeStatus = - pRangingMeasurementData->RangeStatus; - - PALDevDataSet(Dev, LastRangeMeasure, LastRangeDataBuffer); - } - - LOG_FUNCTION_END(Status); - return Status; -} - -VL53L0_Error VL53L010_GetHistogramMeasurementData(VL53L0_DEV Dev, - VL53L0_HistogramMeasurementData_t *pHistogramMeasurementData) -{ - VL53L0_Error Status = VL53L0_ERROR_NOT_IMPLEMENTED; - - LOG_FUNCTION_START(""); - - LOG_FUNCTION_END(Status); - return Status; -} - - -VL53L0_Error VL53L010_PerformSingleRangingMeasurement(VL53L0_DEV Dev, - VL53L0_RangingMeasurementData_t *pRangingMeasurementData) -{ - VL53L0_Error Status = VL53L0_ERROR_NONE; - - LOG_FUNCTION_START(""); - - /* This function will do a complete single ranging - * Here we fix the mode! - */ - Status = VL53L010_SetDeviceMode(Dev, VL53L0_DEVICEMODE_SINGLE_RANGING); - - if (Status == VL53L0_ERROR_NONE) - Status = VL53L010_PerformSingleMeasurement(Dev); - - - if (Status == VL53L0_ERROR_NONE) { - Status = VL53L010_GetRangingMeasurementData(Dev, - pRangingMeasurementData); - } - - if (Status == VL53L0_ERROR_NONE) - Status = VL53L010_ClearInterruptMask(Dev, 0); - - - LOG_FUNCTION_END(Status); - return Status; -} - - -VL53L0_Error VL53L010_PerformSingleHistogramMeasurement(VL53L0_DEV Dev, - VL53L0_HistogramMeasurementData_t *pHistogramMeasurementData) -{ - VL53L0_Error Status = VL53L0_ERROR_NOT_IMPLEMENTED; - - LOG_FUNCTION_START(""); - - LOG_FUNCTION_END(Status); - return Status; -} - -VL53L0_Error VL53L010_SetNumberOfROIZones(VL53L0_DEV Dev, uint8_t - NumberOfROIZones) -{ - VL53L0_Error Status = VL53L0_ERROR_NONE; - - LOG_FUNCTION_START(""); - - if (NumberOfROIZones != 1) - Status = VL53L0_ERROR_INVALID_PARAMS; - - - LOG_FUNCTION_END(Status); - return Status; -} - -VL53L0_Error VL53L010_GetNumberOfROIZones(VL53L0_DEV Dev, uint8_t* - pNumberOfROIZones) -{ - VL53L0_Error Status = VL53L0_ERROR_NONE; - - LOG_FUNCTION_START(""); - - *pNumberOfROIZones = 1; - - LOG_FUNCTION_END(Status); - return Status; -} - -VL53L0_Error VL53L010_GetMaxNumberOfROIZones(VL53L0_DEV Dev, uint8_t - *pMaxNumberOfROIZones) -{ - VL53L0_Error Status = VL53L0_ERROR_NONE; - - LOG_FUNCTION_START(""); - - *pMaxNumberOfROIZones = 1; - - LOG_FUNCTION_END(Status); - return Status; -} - -/* End Group PAL Measurement Functions */ - - -VL53L0_Error VL53L010_SetGpioConfig(VL53L0_DEV Dev, uint8_t Pin, - VL53L0_DeviceModes DeviceMode, VL53L0_GpioFunctionality Functionality, - VL53L0_InterruptPolarity Polarity) -{ - VL53L0_Error Status = VL53L0_ERROR_NONE; - VL53L0_DeviceSpecificParameters_t DeviceSpecificParameters; - uint8_t data; - - LOG_FUNCTION_START(""); - - if (Pin != 0) { - Status = VL53L0_ERROR_GPIO_NOT_EXISTING; - } else if (DeviceMode == VL53L0_DEVICEMODE_GPIO_DRIVE) { - if (Polarity == VL53L0_INTERRUPTPOLARITY_LOW) { - data = 0x10; - } else { - data = 1; - } - Status = VL53L0_WrByte(Dev, - VL53L010_REG_GPIO_HV_MUX_ACTIVE_HIGH, data); - - } else if (DeviceMode == VL53L0_DEVICEMODE_GPIO_OSC) { - - Status |= VL53L0_WrByte(Dev, 0xff, 0x01); - Status |= VL53L0_WrByte(Dev, 0x00, 0x00); - - Status |= VL53L0_WrByte(Dev, 0xff, 0x00); - Status |= VL53L0_WrByte(Dev, 0x80, 0x01); - Status |= VL53L0_WrByte(Dev, 0x85, 0x02); - - Status |= VL53L0_WrByte(Dev, 0xff, 0x04); - Status |= VL53L0_WrByte(Dev, 0xcd, 0x00); - Status |= VL53L0_WrByte(Dev, 0xcc, 0x11); - - Status |= VL53L0_WrByte(Dev, 0xff, 0x07); - Status |= VL53L0_WrByte(Dev, 0xbe, 0x00); - - Status |= VL53L0_WrByte(Dev, 0xff, 0x06); - Status |= VL53L0_WrByte(Dev, 0xcc, 0x09); - - Status |= VL53L0_WrByte(Dev, 0xff, 0x00); - Status |= VL53L0_WrByte(Dev, 0xff, 0x01); - Status |= VL53L0_WrByte(Dev, 0x00, 0x00); - - } else { - - if (Status == VL53L0_ERROR_NONE) { - switch (Functionality) { - case VL53L010_GPIOFUNCTIONALITY_OFF: - data = 0x00; - break; - case VL53L010_GPIOFUNCTIONALITY_THRESHOLD_CROSSED_LOW: - data = 0x01; - break; - case VL53L010_GPIOFUNCTIONALITY_THRESHOLD_CROSSED_HIGH: - data = 0x02; - break; - case VL53L010_GPIOFUNCTIONALITY_THRESHOLD_CROSSED_OUT: - data = 0x03; - break; - case VL53L010_GPIOFUNCTIONALITY_NEW_MEASURE_READY: - data = 0x04; - break; - default: - Status = VL53L0_ERROR_GPIO_FUNCTIONALITY_NOT_SUPPORTED; - } - } - - if (Status == VL53L0_ERROR_NONE) { - Status = VL53L0_WrByte(Dev, - VL53L010_REG_SYSTEM_INTERRUPT_CONFIG_GPIO, - data); - } - - if (Status == VL53L0_ERROR_NONE) { - if (Polarity == VL53L0_INTERRUPTPOLARITY_LOW) - data = 0; - else - data = (uint8_t)(1<<4); - - Status = VL53L0_UpdateByte(Dev, - VL53L010_REG_GPIO_HV_MUX_ACTIVE_HIGH, - 0xEF, data); - } - - if (Status == VL53L0_ERROR_NONE) { - VL53L010_SETDEVICESPECIFICPARAMETER(Dev, Pin0GpioFunctionality, - Functionality); - } - - if (Status == VL53L0_ERROR_NONE) - Status = VL53L010_ClearInterruptMask(Dev, 0); - } - - LOG_FUNCTION_END(Status); - return Status; -} - -VL53L0_Error VL53L010_GetGpioConfig(VL53L0_DEV Dev, uint8_t Pin, - VL53L0_DeviceModes *DeviceMode, - VL53L0_GpioFunctionality *pFunctionality, - VL53L0_InterruptPolarity *pPolarity) -{ - VL53L0_Error Status = VL53L0_ERROR_NONE; - VL53L0_DeviceSpecificParameters_t DeviceSpecificParameters; - VL53L0_GpioFunctionality GpioFunctionality; - uint8_t data; - - LOG_FUNCTION_START(""); - - if (Pin != 0) { - Status = VL53L0_ERROR_GPIO_NOT_EXISTING; - } else { - Status = VL53L0_RdByte(Dev, - VL53L010_REG_SYSTEM_INTERRUPT_CONFIG_GPIO, - &data); - } - - if (Status == VL53L0_ERROR_NONE) { - switch (data&0x07) { - case 0x00: - GpioFunctionality = VL53L010_GPIOFUNCTIONALITY_OFF; - break; - case 0x01: - GpioFunctionality = - VL53L010_GPIOFUNCTIONALITY_THRESHOLD_CROSSED_LOW; - break; - case 0x02: - GpioFunctionality = - VL53L010_GPIOFUNCTIONALITY_THRESHOLD_CROSSED_HIGH; - break; - case 0x03: - GpioFunctionality = - VL53L010_GPIOFUNCTIONALITY_THRESHOLD_CROSSED_OUT; - break; - case 0x04: - GpioFunctionality = - VL53L010_GPIOFUNCTIONALITY_NEW_MEASURE_READY; - break; - default: - Status = VL53L0_ERROR_GPIO_FUNCTIONALITY_NOT_SUPPORTED; - } - } - - if (Status == VL53L0_ERROR_NONE) - Status = VL53L0_RdByte(Dev, - VL53L010_REG_GPIO_HV_MUX_ACTIVE_HIGH, &data); - - if (Status == VL53L0_ERROR_NONE) { - if ((data & (uint8_t)(1<<4)) == 0) - *pPolarity = VL53L0_INTERRUPTPOLARITY_LOW; - else - *pPolarity = VL53L0_INTERRUPTPOLARITY_HIGH; - } - - if (Status == VL53L0_ERROR_NONE) { - *pFunctionality = GpioFunctionality; - VL53L010_SETDEVICESPECIFICPARAMETER(Dev, Pin0GpioFunctionality, - GpioFunctionality); - } - - LOG_FUNCTION_END(Status); - return Status; -} - -VL53L0_Error VL53L010_SetInterruptThresholds(VL53L0_DEV Dev, VL53L0_DeviceModes - DeviceMode, FixPoint1616_t ThresholdLow, FixPoint1616_t ThresholdHigh) -{ - VL53L0_Error Status = VL53L0_ERROR_NONE; - uint16_t Threshold16; - - LOG_FUNCTION_START(""); - - /* no dependency on DeviceMode for Ewok */ - /* Need to divide by 2 because the FW will apply a x2 */ - Threshold16 = (uint16_t)((ThresholdLow >> 17) & 0x00fff); - Status = VL53L0_WrWord(Dev, VL53L010_REG_SYSTEM_THRESH_LOW, Threshold16); - - if (Status == VL53L0_ERROR_NONE) { - /* Need to divide by 2 because the FW will apply a x2 */ - Threshold16 = (uint16_t)((ThresholdHigh >> 17) & 0x00fff); - Status = VL53L0_WrWord(Dev, VL53L010_REG_SYSTEM_THRESH_HIGH, - Threshold16); - } - - LOG_FUNCTION_END(Status); - return Status; -} - -VL53L0_Error VL53L010_GetInterruptThresholds(VL53L0_DEV Dev, VL53L0_DeviceModes - DeviceMode, FixPoint1616_t *pThresholdLow, - FixPoint1616_t *pThresholdHigh) -{ - VL53L0_Error Status = VL53L0_ERROR_NONE; - uint16_t Threshold16; - - LOG_FUNCTION_START(""); - - /* no dependency on DeviceMode for Ewok */ - - Status = VL53L0_RdWord(Dev, VL53L010_REG_SYSTEM_THRESH_LOW, &Threshold16); - /* Need to multiply by 2 because the FW will apply a x2 */ - *pThresholdLow = (FixPoint1616_t)((0x00fff & Threshold16)<<17); - - if (Status == VL53L0_ERROR_NONE) { - Status = VL53L0_RdWord(Dev, VL53L010_REG_SYSTEM_THRESH_HIGH, - &Threshold16); - /* Need to multiply by 2 because the FW will apply a x2 */ - *pThresholdHigh = (FixPoint1616_t)((0x00fff & Threshold16)<<17); - } - - LOG_FUNCTION_END(Status); - return Status; -} - -/* Group PAL Interrupt Functions */ -VL53L0_Error VL53L010_ClearInterruptMask(VL53L0_DEV Dev, uint32_t InterruptMask) -{ - VL53L0_Error Status = VL53L0_ERROR_NONE; - uint8_t LoopCount; - uint8_t Byte; - - LOG_FUNCTION_START(""); - - /* clear bit 0 range interrupt, bit 1 error interrupt */ - Status = VL53L0_WrByte(Dev, VL53L010_REG_SYSTEM_INTERRUPT_CLEAR, 0x01); - LoopCount = 0; - do { - VL53L0_RdByte(Dev, VL53L010_REG_RESULT_INTERRUPT_STATUS, &Byte); - LoopCount++; - } while (((Byte & 0x07) != 0x00) && (LoopCount < 8)); - Status = VL53L0_WrByte(Dev, VL53L010_REG_SYSTEM_INTERRUPT_CLEAR, 0x00); - /* clear all */ - - LOG_FUNCTION_END(Status); - return Status; -} - -VL53L0_Error VL53L010_GetInterruptMaskStatus(VL53L0_DEV Dev, uint32_t - *pInterruptMaskStatus) -{ - VL53L0_Error Status = VL53L0_ERROR_NONE; - uint8_t Byte; - - LOG_FUNCTION_START(""); - - Status = VL53L0_RdByte(Dev, VL53L010_REG_RESULT_INTERRUPT_STATUS, &Byte); - *pInterruptMaskStatus = Byte & 0x07; - - if (Byte & 0x18) { - Status = VL53L0_ERROR_RANGE_ERROR; /* check if some error - occurs */ - } - - LOG_FUNCTION_END(Status); - return Status; -} - -VL53L0_Error VL53L010_EnableInterruptMask(VL53L0_DEV Dev, uint32_t InterruptMask) -{ - VL53L0_Error Status = VL53L0_ERROR_NOT_IMPLEMENTED; - - LOG_FUNCTION_START(""); - - /* not implemented for VL53L0 */ - - LOG_FUNCTION_END(Status); - return Status; -} - -/* End Group PAL Interrupt Functions */ - -/* Group SPAD functions */ - -VL53L0_Error VL53L010_SetSpadAmbientDamperThreshold(VL53L0_DEV Dev, uint16_t - SpadAmbientDamperThreshold) -{ - VL53L0_Error Status = VL53L0_ERROR_NONE; - - LOG_FUNCTION_START(""); - - VL53L0_WrByte(Dev, 0xFF, 0x01); - Status = VL53L0_WrWord(Dev, 0x40, SpadAmbientDamperThreshold); - VL53L0_WrByte(Dev, 0xFF, 0x00); - - LOG_FUNCTION_END(Status); - return Status; -} - -VL53L0_Error VL53L010_GetSpadAmbientDamperThreshold(VL53L0_DEV Dev, uint16_t - *pSpadAmbientDamperThreshold) -{ - VL53L0_Error Status = VL53L0_ERROR_NONE; - - LOG_FUNCTION_START(""); - - VL53L0_WrByte(Dev, 0xFF, 0x01); - Status = VL53L0_RdWord(Dev, 0x40, pSpadAmbientDamperThreshold); - VL53L0_WrByte(Dev, 0xFF, 0x00); - - LOG_FUNCTION_END(Status); - return Status; -} - -VL53L0_Error VL53L010_SetSpadAmbientDamperFactor(VL53L0_DEV Dev, uint16_t - SpadAmbientDamperFactor) -{ - VL53L0_Error Status = VL53L0_ERROR_NONE; - uint8_t Byte; - - LOG_FUNCTION_START(""); - - Byte = (uint8_t) (SpadAmbientDamperFactor & 0x00FF); - - VL53L0_WrByte(Dev, 0xFF, 0x01); - Status = VL53L0_WrByte(Dev, 0x42, Byte); - VL53L0_WrByte(Dev, 0xFF, 0x00); - - LOG_FUNCTION_END(Status); - return Status; -} - -VL53L0_Error VL53L010_GetSpadAmbientDamperFactor(VL53L0_DEV Dev, uint16_t - *pSpadAmbientDamperFactor) -{ - VL53L0_Error Status = VL53L0_ERROR_NONE; - uint8_t Byte; - - LOG_FUNCTION_START(""); - - VL53L0_WrByte(Dev, 0xFF, 0x01); - Status = VL53L0_RdByte(Dev, 0x42, &Byte); - VL53L0_WrByte(Dev, 0xFF, 0x00); - *pSpadAmbientDamperFactor = (uint16_t) Byte; - - LOG_FUNCTION_END(Status); - return Status; -} - -/* END Group SPAD functions */ - -/*////////////////////////////////////////////////////////////////////////////// -// -////////////////////////////////// -// Internal functions -//////////////////////////////////////////////////////////////////////////////// -////////////////////////////////// -*/ - - -VL53L010_EXTERNAL uint32_t VL53L010_calc_macro_period_ps(VL53L0_DEV Dev, - uint8_t vcsel_period); -VL53L010_EXTERNAL uint16_t VL53L010_encode_timeout(uint32_t timeout_mclks); -VL53L010_EXTERNAL uint32_t VL53L010_decode_timeout(uint16_t encoded_timeout); - -VL53L010_EXTERNAL VL53L0_Error VL53L010_get_vcsel_pulse_period(VL53L0_DEV Dev, uint8_t - *pVCSELPulsePeriod, uint8_t RangeIndex) -{ - VL53L0_Error Status = VL53L0_ERROR_NONE; - uint8_t vcsel_period_reg; - - LOG_FUNCTION_START(""); - - switch (RangeIndex) { - case 0: - Status = VL53L0_RdByte(Dev, VL53L010_REG_RNGA_CONFIG_VCSEL_PERIOD, - &vcsel_period_reg); - break; - case 1: - Status = VL53L0_RdByte(Dev, - VL53L010_REG_RNGB1_CONFIG_VCSEL_PERIOD, - &vcsel_period_reg); - break; - case 2: - Status = VL53L0_RdByte(Dev, - VL53L010_REG_RNGB2_CONFIG_VCSEL_PERIOD, - &vcsel_period_reg); - break; - default: - Status = VL53L0_RdByte(Dev, VL53L010_REG_RNGA_CONFIG_VCSEL_PERIOD, - &vcsel_period_reg); - } - - if (Status == VL53L0_ERROR_NONE) { - *pVCSELPulsePeriod = - VL53L010_decode_vcsel_period(vcsel_period_reg); - } - - LOG_FUNCTION_END(Status); - return Status; -} - -/* To convert ms into register value */ -VL53L010_EXTERNAL uint16_t VL53L010_calc_encoded_timeout(VL53L0_DEV Dev, uint32_t - timeout_period_us, uint8_t vcsel_period) -{ - uint32_t macro_period_ps; - uint32_t macro_period_ns; - uint32_t timeout_period_mclks = 0; - uint16_t timeout_overall_periods = 0; - - macro_period_ps = VL53L010_calc_macro_period_ps(Dev, vcsel_period); - macro_period_ns = macro_period_ps / 1000; - - timeout_period_mclks = (uint32_t) (((timeout_period_us * 1000) + - (macro_period_ns / 2)) / macro_period_ns); - timeout_overall_periods = VL53L010_encode_timeout(timeout_period_mclks); - - return timeout_overall_periods; -} - -/* To convert register value into us */ -VL53L010_EXTERNAL uint32_t VL53L010_calc_ranging_wait_us(VL53L0_DEV Dev, uint16_t - timeout_overall_periods, uint8_t vcsel_period) -{ - uint32_t macro_period_ps; - uint32_t macro_period_ns; - uint32_t timeout_period_mclks = 0; - uint32_t actual_timeout_period_us = 0; - - macro_period_ps = VL53L010_calc_macro_period_ps(Dev, vcsel_period); - macro_period_ns = macro_period_ps / 1000; - - timeout_period_mclks = VL53L010_decode_timeout(timeout_overall_periods); - actual_timeout_period_us = ((timeout_period_mclks * macro_period_ns) + - (macro_period_ns / 2)) / 1000; - - return actual_timeout_period_us; -} - -VL53L010_EXTERNAL uint32_t VL53L010_calc_macro_period_ps(VL53L0_DEV Dev, - uint8_t vcsel_period) -{ - uint32_t PLL_multiplier; - uint64_t PLL_period_ps; - uint8_t vcsel_period_pclks; - uint32_t macro_period_vclks; - uint32_t macro_period_ps; - FixPoint1616_t OscFrequencyMHz; - VL53L0_Error Status = VL53L0_ERROR_NONE; - VL53L0_DeviceSpecificParameters_t DeviceSpecificParameters; - - LOG_FUNCTION_START(""); - - PLL_multiplier = 65536 / 64; /* PLL multiplier is 64 */ - - OscFrequencyMHz = VL53L010_GETDEVICESPECIFICPARAMETER(Dev, - OscFrequencyMHz); - - if (OscFrequencyMHz == 0) { - /* Use default one */ - VL53L010_SETDEVICESPECIFICPARAMETER(Dev, OscFrequencyMHz, 748421); - OscFrequencyMHz = 748421; - } - PLL_period_ps = (1000 * 1000 * PLL_multiplier) / - OscFrequencyMHz; - - vcsel_period_pclks = VL53L010_decode_vcsel_period(vcsel_period); - - macro_period_vclks = 2304; - macro_period_ps = (uint32_t)(macro_period_vclks * vcsel_period_pclks * - PLL_period_ps); - - LOG_FUNCTION_END(""); - return macro_period_ps; -} - -VL53L010_EXTERNAL uint8_t VL53L010_decode_vcsel_period(uint8_t vcsel_period_reg) -{ - - /*! - * Converts the encoded VCSEL period register value into the real - * period in PLL clocks - */ - - uint8_t vcsel_period_pclks = 0; - - vcsel_period_pclks = (vcsel_period_reg + 1) << 1; - - return vcsel_period_pclks; -} - -VL53L010_EXTERNAL uint8_t VL53L010_encode_vcsel_period(uint8_t vcsel_period_pclks) -{ - - /*! - * Converts the encoded VCSEL period register value into the real - * period in PLL clocks - */ - - uint8_t vcsel_period_reg = 0; - - vcsel_period_reg = (vcsel_period_pclks >> 1) - 1; - - return vcsel_period_reg; -} - -VL53L010_EXTERNAL uint16_t VL53L010_encode_timeout(uint32_t timeout_mclks) -{ - /*! - * Encode timeout in macro periods in (LSByte * 2^MSByte) + 1 format - * - */ - - uint16_t encoded_timeout = 0; - uint32_t ls_byte = 0; - uint16_t ms_byte = 0; - - if (timeout_mclks > 0) { - ls_byte = timeout_mclks - 1; - - while ((ls_byte & 0xFFFFFF00) > 0) { - ls_byte = ls_byte >> 1; - ms_byte++; - } - - encoded_timeout = (ms_byte << 8) + (uint16_t) (ls_byte & - 0x000000FF); - - } - - return encoded_timeout; - -} - -VL53L010_EXTERNAL uint32_t VL53L010_decode_timeout(uint16_t encoded_timeout) -{ - /*! - * Decode 16-bit timeout register value - format (LSByte * 2^MSByte) + 1 - * - */ - - uint32_t timeout_mclks = 0; - - timeout_mclks = ((uint32_t) (encoded_timeout & 0x00FF) << (uint32_t) - ((encoded_timeout & 0xFF00) >> 8)) + 1; - - return timeout_mclks; - -} - - -VL53L010_EXTERNAL VL53L0_Error VL53L010_load_additional_settings1(VL53L0_DEV Dev) -{ - VL53L0_Error Status = VL53L0_ERROR_NONE; - - LOG_FUNCTION_START(""); - - /* update 12_05_15_v6 */ - /* OSCT */ - Status |= VL53L0_WrByte(Dev, 0xFF, 0x01); - Status |= VL53L0_WrByte(Dev, 0x00, 0x00); - Status |= VL53L0_WrByte(Dev, 0xFF, 0x00); - Status |= VL53L0_WrByte(Dev, 0x80, 0x01); - - Status |= VL53L0_WrByte(Dev, 0xFF, 0x01); - Status |= VL53L0_WrByte(Dev, 0x14, 0x01); - - Status |= VL53L0_WrByte(Dev, 0xFF, 0x00); - Status |= VL53L0_WrByte(Dev, 0xCD, 0x6C); - Status |= VL53L0_WrByte(Dev, 0xFF, 0x01); - Status |= VL53L0_WrByte(Dev, 0x86, 0x03); - Status |= VL53L0_WrByte(Dev, 0x87, 0x01); - Status |= VL53L0_WrByte(Dev, 0x00, 0x01); - Status |= VL53L0_WrByte(Dev, 0xFF, 0x00); - - /* update 12_05_15_v6 */ - Status |= VL53L0_WrByte(Dev, 0xFF, 0x01); - Status |= VL53L0_WrByte(Dev, 0x00, 0x00); - Status |= VL53L0_WrByte(Dev, 0xFF, 0x00); - Status |= VL53L0_WrByte(Dev, 0xcd, 0x6c); - Status |= VL53L0_WrByte(Dev, 0xFF, 0x01); - Status |= VL53L0_WrByte(Dev, 0x90, 0x07); - Status |= VL53L0_WrByte(Dev, 0x91, 0x3f); - Status |= VL53L0_WrByte(Dev, 0x92, 0x3f); - Status |= VL53L0_WrByte(Dev, 0x88, 0x2b); - Status |= VL53L0_WrByte(Dev, 0x89, 0x03); - Status |= VL53L0_WrByte(Dev, 0xFF, 0x00); - Status |= VL53L0_WrByte(Dev, 0xcd, 0x00); - Status |= VL53L0_WrByte(Dev, 0xFF, 0x01); - Status |= VL53L0_WrByte(Dev, 0x00, 0x01); - Status |= VL53L0_WrByte(Dev, 0xFF, 0x00); - - /* update 12_05_15 */ - Status |= VL53L0_WrByte(Dev, 0xb0, 0x00); - Status |= VL53L0_WrByte(Dev, 0xb1, 0xfc); - Status |= VL53L0_WrByte(Dev, 0xb2, 0x00); - Status |= VL53L0_WrByte(Dev, 0xb3, 0x00); - Status |= VL53L0_WrByte(Dev, 0xb4, 0x00); - Status |= VL53L0_WrByte(Dev, 0xb5, 0x00); - Status |= VL53L0_WrByte(Dev, 0xb6, 0xb0); - - Status |= VL53L0_WrByte(Dev, 0x32, 0x03); - - Status |= VL53L0_WrByte(Dev, 0x41, 0xff); - Status |= VL53L0_WrByte(Dev, 0x42, 0x07); - Status |= VL53L0_WrByte(Dev, 0x43, 0x01); - - Status |= VL53L0_WrByte(Dev, 0x01, 0x01); - - if (Status != 0) - Status = VL53L0_ERROR_CONTROL_INTERFACE; - - LOG_FUNCTION_END(Status); - return Status; -} - - -VL53L010_EXTERNAL VL53L0_Error VL53L010_load_additional_settings3(VL53L0_DEV Dev) -{ - VL53L0_Error Status = VL53L0_ERROR_NONE; - - LOG_FUNCTION_START(""); - - /* update 150624_b */ - - Status |= VL53L0_WrByte(Dev, 0xff, 0x00); - Status |= VL53L0_WrByte(Dev, 0x80, 0x01); - - Status |= VL53L0_WrByte(Dev, 0xff, 0x01); - Status |= VL53L0_WrByte(Dev, 0x00, 0x00); - - Status |= VL53L0_WrByte(Dev, 0xff, 0x01); - Status |= VL53L0_WrByte(Dev, 0x4f, 0x0B); - - Status |= VL53L0_WrByte(Dev, 0xFF, 0x0E); - - Status |= VL53L0_WrByte(Dev, 0x00, 0x0C); - Status |= VL53L0_WrByte(Dev, 0x01, 0x0C); - Status |= VL53L0_WrByte(Dev, 0x02, 0x0A); - Status |= VL53L0_WrByte(Dev, 0x03, 0x0D); - Status |= VL53L0_WrByte(Dev, 0x04, 0x00); - Status |= VL53L0_WrByte(Dev, 0x05, 0x60); - Status |= VL53L0_WrByte(Dev, 0x06, 0x06); - Status |= VL53L0_WrByte(Dev, 0x07, 0x47); - Status |= VL53L0_WrByte(Dev, 0x08, 0x28); - Status |= VL53L0_WrByte(Dev, 0x09, 0x20); - Status |= VL53L0_WrByte(Dev, 0x0A, 0x28); - Status |= VL53L0_WrByte(Dev, 0x0B, 0x49); - Status |= VL53L0_WrByte(Dev, 0x0C, 0x00); - Status |= VL53L0_WrByte(Dev, 0x0D, 0x0B); - Status |= VL53L0_WrByte(Dev, 0x0E, 0x00); - Status |= VL53L0_WrByte(Dev, 0x0F, 0x0B); - Status |= VL53L0_WrByte(Dev, 0x10, 0x00); - Status |= VL53L0_WrByte(Dev, 0x11, 0xA1); - Status |= VL53L0_WrByte(Dev, 0x12, 0x00); - Status |= VL53L0_WrByte(Dev, 0x13, 0xA0); - Status |= VL53L0_WrByte(Dev, 0x14, 0x00); - Status |= VL53L0_WrByte(Dev, 0x15, 0x04); - Status |= VL53L0_WrByte(Dev, 0x16, 0x01); - Status |= VL53L0_WrByte(Dev, 0x17, 0x00); - Status |= VL53L0_WrByte(Dev, 0x18, 0x01); - Status |= VL53L0_WrByte(Dev, 0x19, 0xA0); - Status |= VL53L0_WrByte(Dev, 0x1A, 0x11); - Status |= VL53L0_WrByte(Dev, 0x1B, 0x00); - Status |= VL53L0_WrByte(Dev, 0x1C, 0x09); - Status |= VL53L0_WrByte(Dev, 0x1D, 0x04); - Status |= VL53L0_WrByte(Dev, 0x1E, 0x11); - Status |= VL53L0_WrByte(Dev, 0x1F, 0x08); - Status |= VL53L0_WrByte(Dev, 0x20, 0x09); - Status |= VL53L0_WrByte(Dev, 0x21, 0x02); - Status |= VL53L0_WrByte(Dev, 0x22, 0x0C); - Status |= VL53L0_WrByte(Dev, 0x23, 0x00); - Status |= VL53L0_WrByte(Dev, 0x24, 0x0A); - Status |= VL53L0_WrByte(Dev, 0x25, 0x0D); - Status |= VL53L0_WrByte(Dev, 0x26, 0x00); - Status |= VL53L0_WrByte(Dev, 0x27, 0x0B); - Status |= VL53L0_WrByte(Dev, 0x28, 0x00); - Status |= VL53L0_WrByte(Dev, 0x29, 0x0B); - Status |= VL53L0_WrByte(Dev, 0x2A, 0x00); - Status |= VL53L0_WrByte(Dev, 0x2B, 0x04); - Status |= VL53L0_WrByte(Dev, 0x2C, 0x01); - Status |= VL53L0_WrByte(Dev, 0x2D, 0x60); - Status |= VL53L0_WrByte(Dev, 0x2E, 0x09); - Status |= VL53L0_WrByte(Dev, 0x2F, 0x92); - Status |= VL53L0_WrByte(Dev, 0x30, 0x01); - Status |= VL53L0_WrByte(Dev, 0x31, 0x64); - Status |= VL53L0_WrByte(Dev, 0x32, 0x09); - Status |= VL53L0_WrByte(Dev, 0x33, 0x8A); - Status |= VL53L0_WrByte(Dev, 0x34, 0x01); - Status |= VL53L0_WrByte(Dev, 0x35, 0xE0); - Status |= VL53L0_WrByte(Dev, 0x36, 0x0F); - Status |= VL53L0_WrByte(Dev, 0x37, 0xAA); - Status |= VL53L0_WrByte(Dev, 0x38, 0x01); - Status |= VL53L0_WrByte(Dev, 0x39, 0xE4); - Status |= VL53L0_WrByte(Dev, 0x3A, 0x0F); - Status |= VL53L0_WrByte(Dev, 0x3B, 0xAE); - Status |= VL53L0_WrByte(Dev, 0x3C, 0x01); - Status |= VL53L0_WrByte(Dev, 0x3D, 0x00); - Status |= VL53L0_WrByte(Dev, 0x3E, 0x00); - Status |= VL53L0_WrByte(Dev, 0x3F, 0x54); - Status |= VL53L0_WrByte(Dev, 0x40, 0x05); - Status |= VL53L0_WrByte(Dev, 0x41, 0x88); - Status |= VL53L0_WrByte(Dev, 0x42, 0x0B); - Status |= VL53L0_WrByte(Dev, 0x43, 0x02); - Status |= VL53L0_WrByte(Dev, 0x44, 0x0C); - Status |= VL53L0_WrByte(Dev, 0x45, 0x04); - Status |= VL53L0_WrByte(Dev, 0x46, 0x06); - Status |= VL53L0_WrByte(Dev, 0x47, 0x87); - Status |= VL53L0_WrByte(Dev, 0x48, 0x28); - Status |= VL53L0_WrByte(Dev, 0x49, 0x38); - Status |= VL53L0_WrByte(Dev, 0x4A, 0x2B); - Status |= VL53L0_WrByte(Dev, 0x4B, 0x89); - Status |= VL53L0_WrByte(Dev, 0x4C, 0x00); - Status |= VL53L0_WrByte(Dev, 0x4D, 0x0B); - Status |= VL53L0_WrByte(Dev, 0x4E, 0x00); - Status |= VL53L0_WrByte(Dev, 0x4F, 0x0B); - Status |= VL53L0_WrByte(Dev, 0x50, 0x00); - Status |= VL53L0_WrByte(Dev, 0x51, 0xA1); - Status |= VL53L0_WrByte(Dev, 0x52, 0x00); - Status |= VL53L0_WrByte(Dev, 0x53, 0xA0); - Status |= VL53L0_WrByte(Dev, 0x54, 0x00); - Status |= VL53L0_WrByte(Dev, 0x55, 0x04); - Status |= VL53L0_WrByte(Dev, 0x56, 0x01); - Status |= VL53L0_WrByte(Dev, 0x57, 0x00); - Status |= VL53L0_WrByte(Dev, 0x58, 0x01); - Status |= VL53L0_WrByte(Dev, 0x59, 0x0D); - Status |= VL53L0_WrByte(Dev, 0x5A, 0x09); - Status |= VL53L0_WrByte(Dev, 0x5B, 0x02); - Status |= VL53L0_WrByte(Dev, 0x5C, 0x00); - Status |= VL53L0_WrByte(Dev, 0x5D, 0x60); - Status |= VL53L0_WrByte(Dev, 0x5E, 0x0D); - Status |= VL53L0_WrByte(Dev, 0x5F, 0x67); - Status |= VL53L0_WrByte(Dev, 0x60, 0x00); - Status |= VL53L0_WrByte(Dev, 0x61, 0x60); - Status |= VL53L0_WrByte(Dev, 0x62, 0x0D); - Status |= VL53L0_WrByte(Dev, 0x63, 0xB0); - Status |= VL53L0_WrByte(Dev, 0x64, 0x28); - Status |= VL53L0_WrByte(Dev, 0x65, 0x20); - Status |= VL53L0_WrByte(Dev, 0x66, 0x29); - Status |= VL53L0_WrByte(Dev, 0x67, 0xC1); - Status |= VL53L0_WrByte(Dev, 0x68, 0x00); - Status |= VL53L0_WrByte(Dev, 0x69, 0x0B); - Status |= VL53L0_WrByte(Dev, 0x6A, 0x00); - Status |= VL53L0_WrByte(Dev, 0x6B, 0x0B); - Status |= VL53L0_WrByte(Dev, 0x6C, 0x00); - Status |= VL53L0_WrByte(Dev, 0x6D, 0x0B); - Status |= VL53L0_WrByte(Dev, 0x6E, 0x00); - Status |= VL53L0_WrByte(Dev, 0x6F, 0x0B); - Status |= VL53L0_WrByte(Dev, 0x70, 0x00); - Status |= VL53L0_WrByte(Dev, 0x71, 0xA1); - Status |= VL53L0_WrByte(Dev, 0x72, 0x00); - Status |= VL53L0_WrByte(Dev, 0x73, 0xA0); - Status |= VL53L0_WrByte(Dev, 0x74, 0x00); - Status |= VL53L0_WrByte(Dev, 0x75, 0x04); - Status |= VL53L0_WrByte(Dev, 0x76, 0x04); - Status |= VL53L0_WrByte(Dev, 0x77, 0x09); - Status |= VL53L0_WrByte(Dev, 0x78, 0x09); - Status |= VL53L0_WrByte(Dev, 0x79, 0x0D); - Status |= VL53L0_WrByte(Dev, 0x7A, 0x04); - Status |= VL53L0_WrByte(Dev, 0x7B, 0x1B); - Status |= VL53L0_WrByte(Dev, 0x7C, 0x09); - Status |= VL53L0_WrByte(Dev, 0x7D, 0x82); - Status |= VL53L0_WrByte(Dev, 0x7E, 0x04); - Status |= VL53L0_WrByte(Dev, 0x7F, 0x24); - Status |= VL53L0_WrByte(Dev, 0x80, 0x09); - Status |= VL53L0_WrByte(Dev, 0x81, 0x09); - Status |= VL53L0_WrByte(Dev, 0x82, 0x28); - Status |= VL53L0_WrByte(Dev, 0x83, 0x00); - Status |= VL53L0_WrByte(Dev, 0x84, 0x28); - Status |= VL53L0_WrByte(Dev, 0x85, 0x01); - Status |= VL53L0_WrByte(Dev, 0x86, 0x03); - Status |= VL53L0_WrByte(Dev, 0x87, 0x21); - Status |= VL53L0_WrByte(Dev, 0x88, 0x03); - Status |= VL53L0_WrByte(Dev, 0x89, 0x58); - Status |= VL53L0_WrByte(Dev, 0x8A, 0x03); - Status |= VL53L0_WrByte(Dev, 0x8B, 0xCC); - Status |= VL53L0_WrByte(Dev, 0x8C, 0x03); - Status |= VL53L0_WrByte(Dev, 0x8D, 0xC3); - Status |= VL53L0_WrByte(Dev, 0x8E, 0x00); - Status |= VL53L0_WrByte(Dev, 0x8F, 0x94); - Status |= VL53L0_WrByte(Dev, 0x90, 0x00); - Status |= VL53L0_WrByte(Dev, 0x91, 0x53); - Status |= VL53L0_WrByte(Dev, 0x92, 0x1E); - Status |= VL53L0_WrByte(Dev, 0x93, 0x03); - Status |= VL53L0_WrByte(Dev, 0x94, 0x01); - Status |= VL53L0_WrByte(Dev, 0x95, 0x00); - Status |= VL53L0_WrByte(Dev, 0x96, 0x00); - Status |= VL53L0_WrByte(Dev, 0x97, 0x28); - Status |= VL53L0_WrByte(Dev, 0x98, 0x20); - Status |= VL53L0_WrByte(Dev, 0x99, 0x20); - Status |= VL53L0_WrByte(Dev, 0x9A, 0x08); - Status |= VL53L0_WrByte(Dev, 0x9B, 0x10); - Status |= VL53L0_WrByte(Dev, 0x9C, 0x09); - Status |= VL53L0_WrByte(Dev, 0x9D, 0x03); - Status |= VL53L0_WrByte(Dev, 0x9E, 0x28); - Status |= VL53L0_WrByte(Dev, 0x9F, 0x50); - Status |= VL53L0_WrByte(Dev, 0xA0, 0x2B); - Status |= VL53L0_WrByte(Dev, 0xA1, 0xB1); - Status |= VL53L0_WrByte(Dev, 0xA2, 0x0B); - Status |= VL53L0_WrByte(Dev, 0xA3, 0x02); - Status |= VL53L0_WrByte(Dev, 0xA4, 0x28); - Status |= VL53L0_WrByte(Dev, 0xA5, 0x50); - Status |= VL53L0_WrByte(Dev, 0xA6, 0x2C); - Status |= VL53L0_WrByte(Dev, 0xA7, 0x11); - Status |= VL53L0_WrByte(Dev, 0xA8, 0x00); - Status |= VL53L0_WrByte(Dev, 0xA9, 0x0B); - Status |= VL53L0_WrByte(Dev, 0xAA, 0x00); - Status |= VL53L0_WrByte(Dev, 0xAB, 0x0B); - Status |= VL53L0_WrByte(Dev, 0xAC, 0x00); - Status |= VL53L0_WrByte(Dev, 0xAD, 0xA1); - Status |= VL53L0_WrByte(Dev, 0xAE, 0x00); - Status |= VL53L0_WrByte(Dev, 0xAF, 0xA0); - Status |= VL53L0_WrByte(Dev, 0xB0, 0x00); - Status |= VL53L0_WrByte(Dev, 0xB1, 0x04); - Status |= VL53L0_WrByte(Dev, 0xB2, 0x28); - Status |= VL53L0_WrByte(Dev, 0xB3, 0x4E); - Status |= VL53L0_WrByte(Dev, 0xB4, 0x2D); - Status |= VL53L0_WrByte(Dev, 0xB5, 0x47); - Status |= VL53L0_WrByte(Dev, 0xB6, 0x00); - Status |= VL53L0_WrByte(Dev, 0xB7, 0x0B); - Status |= VL53L0_WrByte(Dev, 0xB8, 0x00); - Status |= VL53L0_WrByte(Dev, 0xB9, 0x0B); - Status |= VL53L0_WrByte(Dev, 0xBA, 0x00); - Status |= VL53L0_WrByte(Dev, 0xBB, 0xA7); - Status |= VL53L0_WrByte(Dev, 0xBC, 0x00); - Status |= VL53L0_WrByte(Dev, 0xBD, 0xA6); - Status |= VL53L0_WrByte(Dev, 0xBE, 0x01); - Status |= VL53L0_WrByte(Dev, 0xBF, 0x02); - Status |= VL53L0_WrByte(Dev, 0xC0, 0x04); - Status |= VL53L0_WrByte(Dev, 0xC1, 0x30); - Status |= VL53L0_WrByte(Dev, 0xC2, 0x00); - Status |= VL53L0_WrByte(Dev, 0xC3, 0x04); - Status |= VL53L0_WrByte(Dev, 0xC4, 0x28); - Status |= VL53L0_WrByte(Dev, 0xC5, 0x60); - Status |= VL53L0_WrByte(Dev, 0xC6, 0x2D); - Status |= VL53L0_WrByte(Dev, 0xC7, 0x89); - Status |= VL53L0_WrByte(Dev, 0xC8, 0x00); - Status |= VL53L0_WrByte(Dev, 0xC9, 0x0B); - Status |= VL53L0_WrByte(Dev, 0xCA, 0x00); - Status |= VL53L0_WrByte(Dev, 0xCB, 0x0B); - Status |= VL53L0_WrByte(Dev, 0xCC, 0x00); - Status |= VL53L0_WrByte(Dev, 0xCD, 0xA1); - Status |= VL53L0_WrByte(Dev, 0xCE, 0x00); - Status |= VL53L0_WrByte(Dev, 0xCF, 0xA0); - Status |= VL53L0_WrByte(Dev, 0xD0, 0x00); - Status |= VL53L0_WrByte(Dev, 0xD1, 0x04); - Status |= VL53L0_WrByte(Dev, 0xD2, 0x00); - Status |= VL53L0_WrByte(Dev, 0xD3, 0x25); - Status |= VL53L0_WrByte(Dev, 0xD4, 0x00); - Status |= VL53L0_WrByte(Dev, 0xD5, 0x2E); - Status |= VL53L0_WrByte(Dev, 0xD6, 0x00); - Status |= VL53L0_WrByte(Dev, 0xD7, 0x25); - Status |= VL53L0_WrByte(Dev, 0xD8, 0x00); - Status |= VL53L0_WrByte(Dev, 0xD9, 0x2E); - Status |= VL53L0_WrByte(Dev, 0xDA, 0x03); - Status |= VL53L0_WrByte(Dev, 0xDB, 0xF3); - Status |= VL53L0_WrByte(Dev, 0xDC, 0x03); - Status |= VL53L0_WrByte(Dev, 0xDD, 0xEA); - Status |= VL53L0_WrByte(Dev, 0xDE, 0x28); - Status |= VL53L0_WrByte(Dev, 0xDF, 0x58); - Status |= VL53L0_WrByte(Dev, 0xE0, 0x2C); - Status |= VL53L0_WrByte(Dev, 0xE1, 0xD9); - Status |= VL53L0_WrByte(Dev, 0xE2, 0x00); - Status |= VL53L0_WrByte(Dev, 0xE3, 0x0B); - Status |= VL53L0_WrByte(Dev, 0xE4, 0x00); - Status |= VL53L0_WrByte(Dev, 0xE5, 0x0B); - Status |= VL53L0_WrByte(Dev, 0xE6, 0x00); - Status |= VL53L0_WrByte(Dev, 0xE7, 0xA1); - Status |= VL53L0_WrByte(Dev, 0xE8, 0x00); - Status |= VL53L0_WrByte(Dev, 0xE9, 0xA0); - Status |= VL53L0_WrByte(Dev, 0xEA, 0x00); - Status |= VL53L0_WrByte(Dev, 0xEB, 0x04); - Status |= VL53L0_WrByte(Dev, 0xEC, 0x01); - Status |= VL53L0_WrByte(Dev, 0xED, 0x26); - Status |= VL53L0_WrByte(Dev, 0xEE, 0x00); - Status |= VL53L0_WrByte(Dev, 0xEF, 0xDC); - Status |= VL53L0_WrByte(Dev, 0xF0, 0x28); - Status |= VL53L0_WrByte(Dev, 0xF1, 0x58); - Status |= VL53L0_WrByte(Dev, 0xF2, 0x2F); - Status |= VL53L0_WrByte(Dev, 0xF3, 0x21); - Status |= VL53L0_WrByte(Dev, 0xF4, 0x00); - Status |= VL53L0_WrByte(Dev, 0xF5, 0x0B); - Status |= VL53L0_WrByte(Dev, 0xF6, 0x00); - Status |= VL53L0_WrByte(Dev, 0xF7, 0x0B); - Status |= VL53L0_WrByte(Dev, 0xF8, 0x00); - Status |= VL53L0_WrByte(Dev, 0xF9, 0xA1); - Status |= VL53L0_WrByte(Dev, 0xFA, 0x00); - Status |= VL53L0_WrByte(Dev, 0xFB, 0xA0); - Status |= VL53L0_WrByte(Dev, 0xFC, 0x00); - Status |= VL53L0_WrByte(Dev, 0xFD, 0x04); - Status |= VL53L0_WrWord(Dev, 0xFE, 0x01E3); - Status |= VL53L0_WrByte(Dev, 0xFF, 0x0F); - Status |= VL53L0_WrByte(Dev, 0x00, 0x04); - Status |= VL53L0_WrByte(Dev, 0x01, 0x48); - Status |= VL53L0_WrByte(Dev, 0x02, 0x01); - Status |= VL53L0_WrByte(Dev, 0x03, 0x60); - Status |= VL53L0_WrByte(Dev, 0x04, 0x09); - Status |= VL53L0_WrByte(Dev, 0x05, 0xA4); - Status |= VL53L0_WrByte(Dev, 0x06, 0x05); - Status |= VL53L0_WrByte(Dev, 0x07, 0xB8); - Status |= VL53L0_WrByte(Dev, 0x08, 0x0B); - Status |= VL53L0_WrByte(Dev, 0x09, 0x07); - Status |= VL53L0_WrByte(Dev, 0x0A, 0x00); - Status |= VL53L0_WrByte(Dev, 0x0B, 0x60); - Status |= VL53L0_WrByte(Dev, 0x0C, 0x0B); - Status |= VL53L0_WrByte(Dev, 0x0D, 0x6B); - Status |= VL53L0_WrByte(Dev, 0x0E, 0x01); - Status |= VL53L0_WrByte(Dev, 0x0F, 0x64); - Status |= VL53L0_WrByte(Dev, 0x10, 0x04); - Status |= VL53L0_WrByte(Dev, 0x11, 0x3C); - Status |= VL53L0_WrByte(Dev, 0x12, 0x00); - Status |= VL53L0_WrByte(Dev, 0x13, 0x60); - Status |= VL53L0_WrByte(Dev, 0x14, 0x0B); - Status |= VL53L0_WrByte(Dev, 0x15, 0x74); - Status |= VL53L0_WrByte(Dev, 0x16, 0x0B); - Status |= VL53L0_WrByte(Dev, 0x17, 0x02); - Status |= VL53L0_WrByte(Dev, 0x18, 0x28); - Status |= VL53L0_WrByte(Dev, 0x19, 0x02); - Status |= VL53L0_WrByte(Dev, 0x1A, 0x28); - Status |= VL53L0_WrByte(Dev, 0x1B, 0x03); - Status |= VL53L0_WrByte(Dev, 0x1C, 0x01); - Status |= VL53L0_WrByte(Dev, 0x1D, 0xA2); - Status |= VL53L0_WrByte(Dev, 0x1E, 0x07); - Status |= VL53L0_WrByte(Dev, 0x1F, 0x8E); - Status |= VL53L0_WrByte(Dev, 0x20, 0x28); - Status |= VL53L0_WrByte(Dev, 0x21, 0x50); - Status |= VL53L0_WrByte(Dev, 0x22, 0x2E); - Status |= VL53L0_WrByte(Dev, 0x23, 0xC9); - Status |= VL53L0_WrByte(Dev, 0x24, 0x00); - Status |= VL53L0_WrByte(Dev, 0x25, 0x0B); - Status |= VL53L0_WrByte(Dev, 0x26, 0x00); - Status |= VL53L0_WrByte(Dev, 0x27, 0x0B); - Status |= VL53L0_WrByte(Dev, 0x28, 0x00); - Status |= VL53L0_WrByte(Dev, 0x29, 0xA1); - Status |= VL53L0_WrByte(Dev, 0x2A, 0x00); - Status |= VL53L0_WrByte(Dev, 0x2B, 0xA0); - Status |= VL53L0_WrByte(Dev, 0x2C, 0x28); - Status |= VL53L0_WrByte(Dev, 0x2D, 0x00); - Status |= VL53L0_WrByte(Dev, 0x2E, 0x00); - Status |= VL53L0_WrByte(Dev, 0x2F, 0x04); - Status |= VL53L0_WrByte(Dev, 0x30, 0x01); - Status |= VL53L0_WrByte(Dev, 0x31, 0x00); - Status |= VL53L0_WrByte(Dev, 0x32, 0x01); - Status |= VL53L0_WrByte(Dev, 0x33, 0xA0); - Status |= VL53L0_WrByte(Dev, 0x34, 0x11); - Status |= VL53L0_WrByte(Dev, 0x35, 0x00); - Status |= VL53L0_WrByte(Dev, 0x36, 0x09); - Status |= VL53L0_WrByte(Dev, 0x37, 0x05); - Status |= VL53L0_WrByte(Dev, 0x38, 0x11); - Status |= VL53L0_WrByte(Dev, 0x39, 0x08); - Status |= VL53L0_WrByte(Dev, 0x3A, 0x09); - Status |= VL53L0_WrByte(Dev, 0x3B, 0x03); - Status |= VL53L0_WrByte(Dev, 0x3C, 0x11); - Status |= VL53L0_WrByte(Dev, 0x3D, 0x18); - Status |= VL53L0_WrByte(Dev, 0x3E, 0x09); - Status |= VL53L0_WrByte(Dev, 0x3F, 0x01); - Status |= VL53L0_WrByte(Dev, 0x40, 0x0B); - Status |= VL53L0_WrByte(Dev, 0x41, 0x04); - Status |= VL53L0_WrByte(Dev, 0x42, 0x0C); - Status |= VL53L0_WrByte(Dev, 0x43, 0x00); - Status |= VL53L0_WrByte(Dev, 0x44, 0x0A); - Status |= VL53L0_WrByte(Dev, 0x45, 0x01); - Status |= VL53L0_WrByte(Dev, 0x46, 0x0C); - Status |= VL53L0_WrByte(Dev, 0x47, 0x08); - Status |= VL53L0_WrByte(Dev, 0x48, 0x0A); - Status |= VL53L0_WrByte(Dev, 0x49, 0x01); - Status |= VL53L0_WrByte(Dev, 0x4A, 0x28); - Status |= VL53L0_WrByte(Dev, 0x4B, 0x40); - Status |= VL53L0_WrByte(Dev, 0x4C, 0x2F); - Status |= VL53L0_WrByte(Dev, 0x4D, 0xD1); - Status |= VL53L0_WrByte(Dev, 0x4E, 0x00); - Status |= VL53L0_WrByte(Dev, 0x4F, 0x0B); - Status |= VL53L0_WrByte(Dev, 0x50, 0x00); - Status |= VL53L0_WrByte(Dev, 0x51, 0x0B); - Status |= VL53L0_WrByte(Dev, 0x52, 0x00); - Status |= VL53L0_WrByte(Dev, 0x53, 0xA1); - Status |= VL53L0_WrByte(Dev, 0x54, 0x00); - Status |= VL53L0_WrByte(Dev, 0x55, 0xA0); - Status |= VL53L0_WrByte(Dev, 0x56, 0x00); - Status |= VL53L0_WrByte(Dev, 0x57, 0x04); - Status |= VL53L0_WrByte(Dev, 0x58, 0x28); - Status |= VL53L0_WrByte(Dev, 0x59, 0x11); - Status |= VL53L0_WrByte(Dev, 0x5A, 0x05); - Status |= VL53L0_WrByte(Dev, 0x5B, 0x48); - Status |= VL53L0_WrByte(Dev, 0x5C, 0x00); - Status |= VL53L0_WrByte(Dev, 0x5D, 0x60); - Status |= VL53L0_WrByte(Dev, 0x5E, 0x0A); - Status |= VL53L0_WrByte(Dev, 0x5F, 0xA2); - Status |= VL53L0_WrByte(Dev, 0x60, 0x00); - Status |= VL53L0_WrByte(Dev, 0x61, 0x60); - Status |= VL53L0_WrByte(Dev, 0x62, 0x0B); - Status |= VL53L0_WrByte(Dev, 0x63, 0x3E); - Status |= VL53L0_WrByte(Dev, 0x64, 0x01); - Status |= VL53L0_WrByte(Dev, 0x65, 0x00); - Status |= VL53L0_WrByte(Dev, 0x66, 0x00); - Status |= VL53L0_WrByte(Dev, 0x67, 0x54); - Status |= VL53L0_WrByte(Dev, 0x68, 0x05); - Status |= VL53L0_WrByte(Dev, 0x69, 0x80); - Status |= VL53L0_WrByte(Dev, 0x6A, 0x0B); - Status |= VL53L0_WrByte(Dev, 0x6B, 0x03); - Status |= VL53L0_WrByte(Dev, 0x6C, 0x28); - Status |= VL53L0_WrByte(Dev, 0x6D, 0x38); - Status |= VL53L0_WrByte(Dev, 0x6E, 0x28); - Status |= VL53L0_WrByte(Dev, 0x6F, 0xE1); - Status |= VL53L0_WrByte(Dev, 0x70, 0x0B); - Status |= VL53L0_WrByte(Dev, 0x71, 0x02); - Status |= VL53L0_WrByte(Dev, 0x72, 0x28); - Status |= VL53L0_WrByte(Dev, 0x73, 0x38); - Status |= VL53L0_WrByte(Dev, 0x74, 0x29); - Status |= VL53L0_WrByte(Dev, 0x75, 0x21); - Status |= VL53L0_WrByte(Dev, 0x76, 0x00); - Status |= VL53L0_WrByte(Dev, 0x77, 0x0B); - Status |= VL53L0_WrByte(Dev, 0x78, 0x00); - Status |= VL53L0_WrByte(Dev, 0x79, 0x0B); - Status |= VL53L0_WrByte(Dev, 0x7A, 0x00); - Status |= VL53L0_WrByte(Dev, 0x7B, 0xA1); - Status |= VL53L0_WrByte(Dev, 0x7C, 0x00); - Status |= VL53L0_WrByte(Dev, 0x7D, 0xA0); - Status |= VL53L0_WrByte(Dev, 0x7E, 0x00); - Status |= VL53L0_WrByte(Dev, 0x7F, 0x04); - Status |= VL53L0_WrByte(Dev, 0x80, 0x03); - Status |= VL53L0_WrByte(Dev, 0x81, 0x33); - Status |= VL53L0_WrByte(Dev, 0x82, 0x03); - Status |= VL53L0_WrByte(Dev, 0x83, 0x6A); - Status |= VL53L0_WrByte(Dev, 0x84, 0x03); - Status |= VL53L0_WrByte(Dev, 0x85, 0x61); - Status |= VL53L0_WrByte(Dev, 0x86, 0x05); - Status |= VL53L0_WrByte(Dev, 0x87, 0xF9); - Status |= VL53L0_WrByte(Dev, 0x88, 0x0B); - Status |= VL53L0_WrByte(Dev, 0x89, 0x03); - Status |= VL53L0_WrByte(Dev, 0x8A, 0x28); - Status |= VL53L0_WrByte(Dev, 0x8B, 0x00); - Status |= VL53L0_WrByte(Dev, 0x8C, 0x28); - Status |= VL53L0_WrByte(Dev, 0x8D, 0x09); - Status |= VL53L0_WrByte(Dev, 0x8E, 0x0B); - Status |= VL53L0_WrByte(Dev, 0x8F, 0x03); - Status |= VL53L0_WrByte(Dev, 0x90, 0x28); - Status |= VL53L0_WrByte(Dev, 0x91, 0x66); - Status |= VL53L0_WrByte(Dev, 0x92, 0x2A); - Status |= VL53L0_WrByte(Dev, 0x93, 0x67); - Status |= VL53L0_WrByte(Dev, 0x94, 0x0B); - Status |= VL53L0_WrByte(Dev, 0x95, 0x02); - Status |= VL53L0_WrByte(Dev, 0x96, 0x28); - Status |= VL53L0_WrByte(Dev, 0x97, 0x66); - Status |= VL53L0_WrByte(Dev, 0x98, 0x2A); - Status |= VL53L0_WrByte(Dev, 0x99, 0xAF); - Status |= VL53L0_WrByte(Dev, 0x9A, 0x00); - Status |= VL53L0_WrByte(Dev, 0x9B, 0x0B); - Status |= VL53L0_WrByte(Dev, 0x9C, 0x00); - Status |= VL53L0_WrByte(Dev, 0x9D, 0x0B); - Status |= VL53L0_WrByte(Dev, 0x9E, 0x00); - Status |= VL53L0_WrByte(Dev, 0x9F, 0xA7); - Status |= VL53L0_WrByte(Dev, 0xA0, 0x00); - Status |= VL53L0_WrByte(Dev, 0xA1, 0xA6); - Status |= VL53L0_WrByte(Dev, 0xA2, 0x00); - Status |= VL53L0_WrByte(Dev, 0xA3, 0x04); - - Status |= VL53L0_WrByte(Dev, 0xff, 0x04); - - Status |= VL53L0_WrByte(Dev, 0x79, 0x0B); - Status |= VL53L0_WrByte(Dev, 0x7B, 0x16); - Status |= VL53L0_WrByte(Dev, 0x7D, 0x2B); - Status |= VL53L0_WrByte(Dev, 0x7F, 0x3B); - Status |= VL53L0_WrByte(Dev, 0x81, 0x59); - Status |= VL53L0_WrByte(Dev, 0x83, 0x62); - Status |= VL53L0_WrByte(Dev, 0x85, 0x69); - Status |= VL53L0_WrByte(Dev, 0x87, 0x76); - Status |= VL53L0_WrByte(Dev, 0x89, 0x7F); - Status |= VL53L0_WrByte(Dev, 0x8B, 0x98); - Status |= VL53L0_WrByte(Dev, 0x8D, 0xAC); - Status |= VL53L0_WrByte(Dev, 0x8F, 0xC0); - Status |= VL53L0_WrByte(Dev, 0x90, 0x0C); - Status |= VL53L0_WrByte(Dev, 0x91, 0x30); - Status |= VL53L0_WrByte(Dev, 0x92, 0x28); - Status |= VL53L0_WrByte(Dev, 0x93, 0x02); - Status |= VL53L0_WrByte(Dev, 0x94, 0x37); - Status |= VL53L0_WrByte(Dev, 0x95, 0x62); - - Status |= VL53L0_WrByte(Dev, 0x96, 0x04); - Status |= VL53L0_WrByte(Dev, 0x97, 0x08); - Status |= VL53L0_WrByte(Dev, 0x98, 0x07); - Status |= VL53L0_WrByte(Dev, 0x99, 0x18); - Status |= VL53L0_WrByte(Dev, 0x9A, 0x07); - Status |= VL53L0_WrByte(Dev, 0x9B, 0x6F); - Status |= VL53L0_WrByte(Dev, 0x9C, 0x05); - Status |= VL53L0_WrByte(Dev, 0x9D, 0xD4); - Status |= VL53L0_WrByte(Dev, 0x9E, 0x0A); - Status |= VL53L0_WrByte(Dev, 0x9F, 0x6E); - Status |= VL53L0_WrByte(Dev, 0xA0, 0x09); - Status |= VL53L0_WrByte(Dev, 0xA1, 0xA2); - Status |= VL53L0_WrByte(Dev, 0xA2, 0x0C); - Status |= VL53L0_WrByte(Dev, 0xA3, 0xAA); - Status |= VL53L0_WrByte(Dev, 0xA4, 0x0B); - Status |= VL53L0_WrByte(Dev, 0xA5, 0x97); - Status |= VL53L0_WrByte(Dev, 0xA6, 0x0B); - Status |= VL53L0_WrByte(Dev, 0xA7, 0xD8); - Status |= VL53L0_WrByte(Dev, 0xA8, 0x0A); - Status |= VL53L0_WrByte(Dev, 0xA9, 0xD7); - Status |= VL53L0_WrByte(Dev, 0xAA, 0x08); - Status |= VL53L0_WrByte(Dev, 0xAB, 0xF6); - Status |= VL53L0_WrByte(Dev, 0xAC, 0x07); - Status |= VL53L0_WrByte(Dev, 0xAD, 0x1A); - Status |= VL53L0_WrByte(Dev, 0xAE, 0x0C); - Status |= VL53L0_WrByte(Dev, 0xAF, 0x49); - Status |= VL53L0_WrByte(Dev, 0xB0, 0x09); - Status |= VL53L0_WrByte(Dev, 0xB1, 0x17); - Status |= VL53L0_WrByte(Dev, 0xB2, 0x03); - Status |= VL53L0_WrByte(Dev, 0xB3, 0xCD); - Status |= VL53L0_WrByte(Dev, 0xB4, 0x04); - Status |= VL53L0_WrByte(Dev, 0xB5, 0x55); - - Status |= VL53L0_WrByte(Dev, 0x72, 0xFF); - Status |= VL53L0_WrByte(Dev, 0x73, 0xFF); - - Status |= VL53L0_WrByte(Dev, 0x74, 0xE0); - - Status |= VL53L0_WrByte(Dev, 0x70, 0x01); - - Status |= VL53L0_WrByte(Dev, 0xff, 0x01); - Status |= VL53L0_WrByte(Dev, 0x00, 0x01); - Status |= VL53L0_WrByte(Dev, 0xff, 0x00); - - if (Status != 0) - Status = VL53L0_ERROR_CONTROL_INTERFACE; - - LOG_FUNCTION_END(Status); - return Status; -} - -VL53L010_EXTERNAL VL53L0_Error VL53L010_check_part_used(VL53L0_DEV Dev, - uint8_t *Revision, VL53L0_DeviceInfo_t* pVL53L0_DeviceInfo) -{ - VL53L0_Error Status = VL53L0_ERROR_NONE; - uint8_t ModuleIdInt; - char *ProductId_tmp; - - LOG_FUNCTION_START(""); - - Status = VL53L010_get_info_from_device(Dev); - - if (Status == VL53L0_ERROR_NONE) { - ModuleIdInt = VL53L010_GETDEVICESPECIFICPARAMETER(Dev, ModuleId); - - if (ModuleIdInt == 0) { - *Revision = 0; - VL53L0_COPYSTRING(pVL53L0_DeviceInfo->ProductId, ""); - } else { - *Revision = VL53L010_GETDEVICESPECIFICPARAMETER(Dev, Revision); - ProductId_tmp = VL53L010_GETDEVICESPECIFICPARAMETER(Dev, ProductId); - VL53L0_COPYSTRING(pVL53L0_DeviceInfo->ProductId, ProductId_tmp); - } - } - - LOG_FUNCTION_END(Status); - return Status; -} - -VL53L010_EXTERNAL VL53L0_Error VL53L010_get_info_from_device(VL53L0_DEV Dev) -{ - VL53L0_Error Status = VL53L0_ERROR_NONE; - uint8_t byte; - uint32_t TmpDWord; - VL53L0_DeviceSpecificParameters_t DeviceSpecificParameters; - uint8_t ModuleId; - uint8_t Revision; - uint8_t ReferenceSpadCount; - uint8_t ReferenceSpadType; - char ProductId[19]; - char *ProductId_tmp; - uint8_t ReadDataFromDeviceDone; - - LOG_FUNCTION_START(""); - - ReadDataFromDeviceDone = VL53L010_GETDEVICESPECIFICPARAMETER(Dev, - ReadDataFromDeviceDone); - - /* This access is done only once after that a GetDeviceInfo or - * datainit is done*/ - if (ReadDataFromDeviceDone == 0) { - - Status |= VL53L0_WrByte(Dev, 0x80, 0x01); - Status |= VL53L0_WrByte(Dev, 0xFF, 0x01); - Status |= VL53L0_WrByte(Dev, 0x00, 0x00); - - Status |= VL53L0_WrByte(Dev, 0xFF, 0x06); - Status |= VL53L0_RdByte(Dev, 0x83, &byte); - Status |= VL53L0_WrByte(Dev, 0x83, byte|4); - Status |= VL53L0_WrByte(Dev, 0xFF, 0x07); - Status |= VL53L0_WrByte(Dev, 0x81, 0x01); - - Status |= VL53L0_PollingDelay(Dev); - - Status |= VL53L0_WrByte(Dev, 0x80, 0x01); - - Status |= VL53L0_WrByte(Dev, 0x94, 0x6b); - Status |= VL53L010_device_read_strobe(Dev); - Status |= VL53L0_RdDWord(Dev, 0x90, &TmpDWord); - - ReferenceSpadCount = (uint8_t)((TmpDWord >> 8) & 0x07f); - ReferenceSpadType = (uint8_t)((TmpDWord >> 15) & 0x01); - - Status |= VL53L0_WrByte(Dev, 0x94, 0x02); - Status |= VL53L010_device_read_strobe(Dev); - Status |= VL53L0_RdByte(Dev, 0x90, &ModuleId); - - Status |= VL53L0_WrByte(Dev, 0x94, 0x7B); - Status |= VL53L010_device_read_strobe(Dev); - Status |= VL53L0_RdByte(Dev, 0x90, &Revision); - - Status |= VL53L0_WrByte(Dev, 0x94, 0x77); - Status |= VL53L010_device_read_strobe(Dev); - Status |= VL53L0_RdDWord(Dev, 0x90, &TmpDWord); - - ProductId[0] = (char)((TmpDWord >> 25) & 0x07f); - ProductId[1] = (char)((TmpDWord >> 18) & 0x07f); - ProductId[2] = (char)((TmpDWord >> 11) & 0x07f); - ProductId[3] = (char)((TmpDWord >> 4) & 0x07f); - - byte = (uint8_t)((TmpDWord & 0x00f) << 3); - - Status |= VL53L0_WrByte(Dev, 0x94, 0x78); - Status |= VL53L010_device_read_strobe(Dev); - Status |= VL53L0_RdDWord(Dev, 0x90, &TmpDWord); - - ProductId[4] = (char)(byte + - ((TmpDWord >> 29) & 0x07f)); - ProductId[5] = (char)((TmpDWord >> 22) & 0x07f); - ProductId[6] = (char)((TmpDWord >> 15) & 0x07f); - ProductId[7] = (char)((TmpDWord >> 8) & 0x07f); - ProductId[8] = (char)((TmpDWord >> 1) & 0x07f); - - byte = (uint8_t)((TmpDWord & 0x001) << 6); - - Status |= VL53L0_WrByte(Dev, 0x94, 0x79); - - Status |= VL53L010_device_read_strobe(Dev); - - Status |= VL53L0_RdDWord(Dev, 0x90, &TmpDWord); - - ProductId[9] = (char)(byte + - ((TmpDWord >> 26) & 0x07f)); - ProductId[10] = (char)((TmpDWord >> 19) & 0x07f); - ProductId[11] = (char)((TmpDWord >> 12) & 0x07f); - ProductId[12] = (char)((TmpDWord >> 5) & 0x07f); - - byte = (uint8_t)((TmpDWord & 0x01f) << 2); - - Status |= VL53L0_WrByte(Dev, 0x94, 0x80); - - Status |= VL53L010_device_read_strobe(Dev); - - Status |= VL53L0_RdDWord(Dev, 0x90, &TmpDWord); - - ProductId[13] = (char)(byte + - ((TmpDWord >> 30) & 0x07f)); - ProductId[14] = (char)((TmpDWord >> 23) & 0x07f); - ProductId[15] = (char)((TmpDWord >> 16) & 0x07f); - ProductId[16] = (char)((TmpDWord >> 9) & 0x07f); - ProductId[17] = (char)((TmpDWord >> 2) & 0x07f); - ProductId[18] = '\0'; - - Status |= VL53L0_WrByte(Dev, 0x81, 0x00); - Status |= VL53L0_WrByte(Dev, 0xFF, 0x06); - Status |= VL53L0_RdByte(Dev, 0x83, &byte); - Status |= VL53L0_WrByte(Dev, 0x83, byte&0xfb); - Status |= VL53L0_WrByte(Dev, 0xFF, 0x01); - Status |= VL53L0_WrByte(Dev, 0x00, 0x01); - - Status |= VL53L0_WrByte(Dev, 0xFF, 0x00); - Status |= VL53L0_WrByte(Dev, 0x80, 0x00); - - if (Status == VL53L0_ERROR_NONE) { - VL53L010_SETDEVICESPECIFICPARAMETER(Dev, - ModuleId, ModuleId); - - VL53L010_SETDEVICESPECIFICPARAMETER(Dev, - Revision, Revision); - - VL53L010_SETDEVICESPECIFICPARAMETER(Dev, - ReferenceSpadCount, ReferenceSpadCount); - - VL53L010_SETDEVICESPECIFICPARAMETER(Dev, - ReferenceSpadType, ReferenceSpadType); - - ProductId_tmp = VL53L010_GETDEVICESPECIFICPARAMETER(Dev, - ProductId); - VL53L0_COPYSTRING(ProductId_tmp, ProductId); - - VL53L010_SETDEVICESPECIFICPARAMETER(Dev, - ReadDataFromDeviceDone, 1); - } - } - - LOG_FUNCTION_END(Status); - return Status; -} - -uint32_t VL53L010_isqrt(uint32_t num) -{ - - /* - * Implements an integer square root - * - * From: http://en.wikipedia.org/wiki/Methods_of_computing_square_roots - */ - - uint32_t res = 0; - uint32_t bit = 1 << 30; - /* The second-to-top bit is set: 1 << 14 for - 16-bits, 1 << 30 for 32 bits - */ - /* "bit" starts at the highest power of four <= the argument.*/ - while (bit > num) - bit >>= 2; - - - while (bit != 0) { - if (num >= res + bit) { - num -= res + bit; - res = (res >> 1) + bit; - } else - res >>= 1; - - bit >>= 2; - } - - return res; -} - - -VL53L0_Error VL53L010_device_read_strobe(VL53L0_DEV Dev) { - VL53L0_Error Status = VL53L0_ERROR_NONE; - uint8_t strobe; - uint32_t LoopNb; - LOG_FUNCTION_START(""); - - Status |= VL53L0_WrByte(Dev, 0x83, 0x00); - - // polling - // use timeout to avoid deadlock - if (Status == VL53L0_ERROR_NONE) { - LoopNb = 0; - do { - Status =VL53L0_RdByte(Dev, 0x83, &strobe); - if ((strobe != 0x00) || Status != VL53L0_ERROR_NONE) { - break; - } - LoopNb = LoopNb + 1; - } while (LoopNb < VL53L0_DEFAULT_MAX_LOOP); - - if (LoopNb >= VL53L0_DEFAULT_MAX_LOOP) { - Status = VL53L0_ERROR_TIME_OUT; - } - } - - Status |= VL53L0_WrByte(Dev, 0x83, 0x01); - - LOG_FUNCTION_END(Status); - return Status; - -} - -uint32_t VL53L010_quadrature_sum(uint32_t a, - uint32_t b) -{ - /* - * Implements a quadrature sum - * - * rea = sqrt(a^2 + b^2) - * - * Trap overflow case max input value is 65535 (16-bit value) - * as internal calc are 32-bit wide - * - * If overflow then seta output to maximum - */ - uint32_t res = 0; - - if (a > 65535 || b > 65535) - res = 65535; - else - res = VL53L010_isqrt(a*a + b*b); - - - return res; -} - - - -VL53L0_Error VL53L010_get_jmp_vcsel_ambient_rate(VL53L0_DEV Dev, - uint32_t *pAmbient_rate_kcps, - uint32_t *pVcsel_rate_kcps, - uint32_t *pSignalTotalEventsRtn) -{ - VL53L0_Error Status = VL53L0_ERROR_NONE; - uint16_t encodedTimeOut; - - uint32_t total_periods_elapsed_rtn__macrop = 0; - uint32_t result_core__total_periods_elapsed_rtn = 0; - uint32_t rngb1_config__timeout__macrop = 0; - uint32_t rngb2_config__timeout__macrop = 0; - uint32_t result_core__ambient_window_events_rtn = 0; - uint32_t result_core__signal_total_events_rtn = 0; - uint8_t last_woi_period; - uint8_t rnga_config__vcsel_period; - uint8_t rngb1_config__vcsel_period; - uint8_t rngb2_config__vcsel_period; - uint8_t global_config__vcsel_width; - - uint32_t ambient_duration_us = 0; - uint32_t vcsel_duration_us = 0; - - uint32_t pll_period_us = 0; - - LOG_FUNCTION_START(""); - - /* read the following */ - Status = VL53L0_WrByte(Dev, 0xFF, 0x01); - Status |= VL53L0_RdDWord(Dev, 0xC8, - &result_core__total_periods_elapsed_rtn); - Status |= VL53L0_RdDWord(Dev, 0xF0, &pll_period_us); - Status |= VL53L0_RdDWord(Dev, 0xbc, - &result_core__ambient_window_events_rtn); - Status |= VL53L0_RdDWord(Dev, 0xc4, - &result_core__signal_total_events_rtn); - Status |= VL53L0_WrByte(Dev, 0xFF, 0x00); - - - if (Status == VL53L0_ERROR_NONE) { - result_core__total_periods_elapsed_rtn = - (int32_t)(result_core__total_periods_elapsed_rtn & - 0x00ffffff); - pll_period_us = (int32_t)(pll_period_us & 0x3ffff); - } - - if (Status == VL53L0_ERROR_NONE) { - Status = VL53L0_RdWord(Dev, VL53L010_REG_RNGB1_TIMEOUT_MSB, - &encodedTimeOut); - if (Status == VL53L0_ERROR_NONE) - rngb1_config__timeout__macrop = - VL53L010_decode_timeout(encodedTimeOut) - 1; - - } - - if (Status == VL53L0_ERROR_NONE) { - Status = VL53L0_RdByte(Dev, VL53L010_REG_RNGA_CONFIG_VCSEL_PERIOD, - &rnga_config__vcsel_period); - } - if (Status == VL53L0_ERROR_NONE) { - Status = VL53L0_RdByte(Dev, - VL53L010_REG_RNGB1_CONFIG_VCSEL_PERIOD, - &rngb1_config__vcsel_period); - } - if (Status == VL53L0_ERROR_NONE) { - Status = VL53L0_RdByte(Dev, - VL53L010_REG_RNGB2_CONFIG_VCSEL_PERIOD, - &rngb2_config__vcsel_period); - } - if (Status == VL53L0_ERROR_NONE) - Status = VL53L0_RdByte(Dev, 0x32, &global_config__vcsel_width); - - - if (Status == VL53L0_ERROR_NONE) { - Status = VL53L0_RdWord(Dev, VL53L010_REG_RNGB2_TIMEOUT_MSB, - &encodedTimeOut); - if (Status == VL53L0_ERROR_NONE) - rngb2_config__timeout__macrop = - VL53L010_decode_timeout(encodedTimeOut) - 1; - - } - - if (Status == VL53L0_ERROR_NONE) { - total_periods_elapsed_rtn__macrop = - result_core__total_periods_elapsed_rtn + 1; - - if (result_core__total_periods_elapsed_rtn == - rngb1_config__timeout__macrop) { - last_woi_period = rngb1_config__vcsel_period; - } else if (result_core__total_periods_elapsed_rtn == - rngb2_config__timeout__macrop) { - last_woi_period = rngb2_config__vcsel_period; - } else { - last_woi_period = rnga_config__vcsel_period; - - } - /* 512 = 1<<9 ==> 24-9=15 */ - ambient_duration_us = last_woi_period * - total_periods_elapsed_rtn__macrop * pll_period_us; - ambient_duration_us = ambient_duration_us / 1000; - - if (ambient_duration_us != 0) { - *pAmbient_rate_kcps = ((1 << 15) * - result_core__ambient_window_events_rtn) / - ambient_duration_us; - } else { - Status = VL53L0_ERROR_DIVISION_BY_ZERO; - } - - if (Status == VL53L0_ERROR_NONE) { - - /* 2048 = 1<<11 ==> 24-11=13 */ - vcsel_duration_us = - (10 * global_config__vcsel_width + 4) - * total_periods_elapsed_rtn__macrop * - pll_period_us ; - vcsel_duration_us = vcsel_duration_us / 10000 ; - - - if (vcsel_duration_us != 0) { - *pVcsel_rate_kcps = ((1 << 13) * - result_core__signal_total_events_rtn) / - vcsel_duration_us; - *pSignalTotalEventsRtn = - result_core__signal_total_events_rtn; - } else { - Status = VL53L0_ERROR_DIVISION_BY_ZERO; - } - - } - } - - LOG_FUNCTION_END(Status); - return Status; - -} - -VL53L0_Error VL53L010_calc_sigma_estimate(VL53L0_DEV Dev, - VL53L0_RangingMeasurementData_t - *pRangingMeasurementData, - FixPoint1616_t *pSigmaEstimate) -{ - /* Expressed in 100ths of a ns, i.e. centi-ns */ - const uint32_t cPulseEffectiveWidth_centi_ns = 800; - /* Expressed in 100ths of a ns, i.e. centi-ns */ - const uint32_t cAmbientEffectiveWidth_centi_ns = 600; - const FixPoint1616_t cSigmaEstRef = 0x00000042; - /* pico secs */ - const uint32_t cVcselPulseWidth_ps = 4700; - const FixPoint1616_t cSigmaEstMax = 0x028F87AE; - /* Time Of Flight per mm (6.6 pico secs) */ - const FixPoint1616_t cTOF_per_mm_ps = 0x0006999A; - const uint32_t c16BitRoundingParam = 0x00008000; - const FixPoint1616_t cMaxXTalk_kcps = 0x00320000; - - uint32_t signalTotalEventsRtn; - FixPoint1616_t sigmaEstimateP1; - FixPoint1616_t sigmaEstimateP2; - FixPoint1616_t sigmaEstimateP3; - FixPoint1616_t deltaT_ps; - FixPoint1616_t pwMult; - FixPoint1616_t sigmaEstRtn; - FixPoint1616_t sigmaEstimate; - FixPoint1616_t xTalkCorrection; - uint32_t signalTotalEventsRtnRawVal; - FixPoint1616_t ambientRate_kcps; - FixPoint1616_t vcselRate_kcps; - FixPoint1616_t xTalkCompRate_mcps; - uint32_t xTalkCompRate_kcps; - VL53L0_Error Status = VL53L0_ERROR_NONE; - VL53L0_DeviceParameters_t CurrentParameters; - FixPoint1616_t diff1_mcps; - FixPoint1616_t diff2_mcps; - FixPoint1616_t sqr1; - FixPoint1616_t sqr2; - FixPoint1616_t sqrSum; - FixPoint1616_t sqrtResult_centi_ns; - FixPoint1616_t sqrtResult; - - /*! \addtogroup calc_sigma_estimate - * @{ - * - * Estimates the range sigma based on the - * - * - vcsel_rate_kcps - * - ambient_rate_kcps - * - signal_total_events - * - xtalk_rate - * - * and the following parameters - * - * - SigmaEstRefArray - * - SigmaEstEffPulseWidth - * - SigmaEstEffAmbWidth - */ - - LOG_FUNCTION_START(""); - - VL53L010_GETPARAMETERFIELD(Dev, XTalkCompensationRateMegaCps, - xTalkCompRate_mcps); - /* - * We work in kcps rather than mcps as this helps keep within the confines - * of the 32 Fix1616 type. - */ - - xTalkCompRate_kcps = xTalkCompRate_mcps * 1000; - if (xTalkCompRate_kcps > cMaxXTalk_kcps) - xTalkCompRate_kcps = cMaxXTalk_kcps; - - - Status = VL53L010_get_jmp_vcsel_ambient_rate(Dev, - &ambientRate_kcps, - &vcselRate_kcps, - &signalTotalEventsRtnRawVal); - - if (Status == VL53L0_ERROR_NONE) { - if (vcselRate_kcps == 0) { - Status = VL53L0_ERROR_DIVISION_BY_ZERO; - } else { - signalTotalEventsRtn = signalTotalEventsRtnRawVal; - if (signalTotalEventsRtn < 1) - signalTotalEventsRtn = 1; - - /* - * Calculate individual components of the main - * equation - replicating the equation implemented in - * the script OpenAll_Ewok_ranging_data.jsl. - * - * sigmaEstimateP1 represents the effective pulse width, - * which is a tuning parameter, rather than a real - * value. - * - * sigmaEstimateP2 represents the ambient/signal rate - * ratio expressed as a multiple of the effective - * ambient width (tuning parameter). - * - * sigmaEstimateP3 provides the signal event component, - * with the knowledge that - * - Noise of a square pulse is 1/sqrt(12) of the - * pulse width. - * - at 0Lux, sigma is proportional to - * effectiveVcselPulseWidth / - * sqrt(12 * signalTotalEvents) - * - * deltaT_ps represents the time of flight in pico secs - * for the current range measurement, using the - * "TOF per mm" constant (in ps). - */ - - sigmaEstimateP1 = cPulseEffectiveWidth_centi_ns; - - /* - ((FixPoint1616 << 16)* uint32)/FixPoint1616 = - FixPoint1616 - */ - sigmaEstimateP2 = (ambientRate_kcps << 16) / - vcselRate_kcps; - sigmaEstimateP2 *= cAmbientEffectiveWidth_centi_ns; - - sigmaEstimateP3 = 2 * - VL53L010_isqrt(signalTotalEventsRtn * 12); - - /* uint32 * FixPoint1616 = FixPoint1616 */ - deltaT_ps = - pRangingMeasurementData->RangeMilliMeter * - cTOF_per_mm_ps; - - /* - * vcselRate - xtalkCompRate - * (uint32 << 16) - FixPoint1616 = FixPoint1616. - * Divide result by 1000 to convert to mcps. - * 500 is added to ensure rounding when integer - * division truncates. - */ - diff1_mcps = (((vcselRate_kcps << 16) - - xTalkCompRate_kcps) + 500)/1000; - - /* vcselRate + xtalkCompRate */ - diff2_mcps = (((vcselRate_kcps << 16) + - xTalkCompRate_kcps) + 500)/1000; - - /* Shift by 12 bits to increase resolution prior to the - division */ - diff1_mcps <<= 12; - - /* FixPoint0428/FixPoint1616 = FixPoint2012 */ - xTalkCorrection = abs(diff1_mcps/diff2_mcps); - - /* FixPoint2012 << 4 = FixPoint1616 */ - xTalkCorrection <<= 4; - - /* FixPoint1616/uint32 = FixPoint1616 */ - pwMult = deltaT_ps/cVcselPulseWidth_ps; - /* smaller than 1.0f */ - - /* - * FixPoint1616 * FixPoint1616 = FixPoint3232, however - * both values are small enough such that32 bits will - * not be exceeded. - */ - pwMult *= ((1 << 16) - xTalkCorrection); - - /* (FixPoint3232 >> 16) = FixPoint1616 */ - pwMult = (pwMult + c16BitRoundingParam) >> 16; - - /* FixPoint1616 + FixPoint1616 = FixPoint1616 */ - pwMult += (1 << 16); - - /* - * At this point the value will be 1.xx, therefore if we - * square the value this will exceed 32 bits. To address - * this perform a single shift to the right before the - * multiplication. - */ - pwMult >>= 1; - /* FixPoint1715 * FixPoint1715 = FixPoint3430 */ - pwMult = pwMult * pwMult; - - /* (FixPoint3430 >> 14) = Fix1616 */ - pwMult >>= 14; - - /* FixPoint1616 * FixPoint1616 = FixPoint3232 */ - sqr1 = pwMult * sigmaEstimateP1; - - /* (FixPoint1616 >> 12) = FixPoint2804 */ - sqr1 = (sqr1 + 0x800) >> 12; - - /* FixPoint2804 * FixPoint2804 = FixPoint5608 */ - sqr1 *= sqr1; - - sqr2 = sigmaEstimateP2; - - /* (FixPoint1616 >> 12) = FixPoint2804 */ - sqr2 = (sqr2 + 0x800) >> 12; - - /* FixPoint2804 * FixPoint2804 = FixPoint5608 */ - sqr2 *= sqr2; - - /* FixPoint5608 + FixPoint5608 = FixPoint5608 */ - sqrSum = sqr1 + sqr2; - - /* SQRT(FixPoint5608) = FixPoint2804 */ - sqrtResult_centi_ns = VL53L010_isqrt(sqrSum); - - /* (FixPoint2804 << 12) = FixPoint1616 */ - sqrtResult_centi_ns <<= 12; - - /* - * Note that the Speed Of Light is expressed in um - * per 1E-10 seconds (2997). Therefore to get mm/ns - * we have to divide by 10000 - */ - sigmaEstRtn = - ((sqrtResult_centi_ns + 50) / 100 * - VL53L010_SPEED_OF_LIGHT_IN_AIR); - sigmaEstRtn /= (sigmaEstimateP3); - /* Add 5000 before dividing by 10000 to ensure - rounding. */ - sigmaEstRtn += 5000; - sigmaEstRtn /= 10000; - - /* FixPoint1616 * FixPoint1616 = FixPoint3232 */ - sqr1 = sigmaEstRtn * sigmaEstRtn; - /* FixPoint1616 * FixPoint1616 = FixPoint3232 */ - sqr2 = cSigmaEstRef * cSigmaEstRef; - - /* sqrt(FixPoint3232 << 12) = FixPoint1022 */ - sqrtResult = VL53L010_isqrt((sqr1 + sqr2) << 12); - sqrtResult = (sqrtResult + 0x20) >> 6; - /* - * Note that the Shift by 12bits increases resolution - * prior to the sqrt, therefore the result must be - * shifted by 6bits to the right to revert back to the - * FixPoint1616 format. - */ - - sigmaEstimate = 1000 * sqrtResult; - - if ((vcselRate_kcps < 1) || - (signalTotalEventsRtn < 1) || - (sigmaEstimate > cSigmaEstMax)) { - sigmaEstimate = cSigmaEstMax; - } - - *pSigmaEstimate = (uint32_t)(sigmaEstimate); - PALDevDataSet(Dev, SigmaEstimate, *pSigmaEstimate); - } - } - - LOG_FUNCTION_END(Status); - return Status; -} - -VL53L0_Error VL53L010_get_pal_range_status(VL53L0_DEV Dev, - uint8_t DeviceRangeStatus, - FixPoint1616_t SignalRate, - FixPoint1616_t CrosstalkCompensation, - uint16_t EffectiveSpadRtnCount, - VL53L0_RangingMeasurementData_t *pRangingMeasurementData, - uint8_t *pPalRangeStatus) -{ - VL53L0_Error Status = VL53L0_ERROR_NONE; - uint8_t tmpByte; - uint8_t SigmaLimitCheckEnable; - uint8_t SignalLimitCheckEnable; - FixPoint1616_t SigmaEstimate; - FixPoint1616_t SignalEstimate; - FixPoint1616_t SigmaLimitValue; - FixPoint1616_t SignalLimitValue; - uint8_t DeviceRangeStatusInternal = 0; - - LOG_FUNCTION_START(""); - - /* - * VL53L0 has a good ranging when the value of the - * DeviceRangeStatus = 11. This function will replace - * the value 0 with the value 11 in the DeviceRangeStatus. - * In addition, the SigmaEstimator is not included in the - * VL53L0 DeviceRangeStatus, this will be added in the - * PalRangeStatus. - */ - - DeviceRangeStatusInternal = ((DeviceRangeStatus & 0x78) >> 3); - - if (DeviceRangeStatusInternal == 11) - tmpByte = 0; - else if (DeviceRangeStatusInternal == 0) - tmpByte = 11; - else - tmpByte = DeviceRangeStatusInternal; - - - /* - * Check if Sigma limit is enabled, if yes then do comparison with - * limit value and put the result back into pPalRangeStatus. - */ - Status = VL53L010_GetLimitCheckEnable(Dev, - VL53L010_CHECKENABLE_SIGMA_FINAL_RANGE, - &SigmaLimitCheckEnable); - - if ((SigmaLimitCheckEnable != 0) && (Status == VL53L0_ERROR_NONE)) { - /* - * compute the Sigma and check with limit - */ - Status = VL53L010_calc_sigma_estimate(Dev, - pRangingMeasurementData, &SigmaEstimate); - - if (Status == VL53L0_ERROR_NONE) { - Status = VL53L010_GetLimitCheckValue(Dev, - VL53L010_CHECKENABLE_SIGMA_FINAL_RANGE, - &SigmaLimitValue); - - if ((SigmaLimitValue > 0) && - (SigmaEstimate > SigmaLimitValue)) { - /* Limit Fail add 2^4 to range status */ - tmpByte += 16; - } - } - } - - /* - * Check if Signal limit is enabled, if yes then do comparison with - * limit value and put the result back into pPalRangeStatus. - */ - Status = VL53L010_GetLimitCheckEnable(Dev, - VL53L010_CHECKENABLE_SIGNAL_RATE_FINAL_RANGE, - &SignalLimitCheckEnable); - - if ((SignalLimitCheckEnable != 0) && (Status == VL53L0_ERROR_NONE)) { - /* - * compute the Signal and check with limit - */ - - SignalEstimate = (FixPoint1616_t)(SignalRate - - (FixPoint1616_t)((EffectiveSpadRtnCount * - CrosstalkCompensation) >> 1)); - - PALDevDataSet(Dev, SignalEstimate, SignalEstimate); - - Status = VL53L010_GetLimitCheckValue(Dev, - VL53L010_CHECKENABLE_SIGNAL_RATE_FINAL_RANGE, - &SignalLimitValue); - - if ((SignalLimitValue > 0) && (SignalEstimate < - SignalLimitValue)) { - /* Limit Fail add 2^5 to range status */ - tmpByte += 32; - } - } - - if (Status == VL53L0_ERROR_NONE) - *pPalRangeStatus = tmpByte; - - - - LOG_FUNCTION_END(Status); - return Status; - -} diff --git a/drivers/input/misc/vl53L0/src/vl53l010_tuning.c b/drivers/input/misc/vl53L0/src/vl53l010_tuning.c deleted file mode 100644 index 0f5fd85bd878..000000000000 --- a/drivers/input/misc/vl53L0/src/vl53l010_tuning.c +++ /dev/null @@ -1,138 +0,0 @@ -/******************************************************************************* - Copyright © 2016, STMicroelectronics International N.V. - All rights reserved. - - Redistribution and use in source and binary forms, with or without - modification, are permitted provided that the following conditions are met: - * Redistributions of source code must retain the above copyright - notice, this list of conditions and the following disclaimer. - * Redistributions in binary form must reproduce the above copyright - notice, this list of conditions and the following disclaimer in the - documentation and/or other materials provided with the distribution. - * Neither the name of STMicroelectronics nor the - names of its contributors may be used to endorse or promote products - derived from this software without specific prior written permission. - - THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND - ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED - WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, AND - NON-INFRINGEMENT OF INTELLECTUAL PROPERTY RIGHTS ARE DISCLAIMED. - IN NO EVENT SHALL STMICROELECTRONICS INTERNATIONAL N.V. BE LIABLE FOR ANY - DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES - (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; - LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND - ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT - (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS - SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - ******************************************************************************/ - -#include "vl53l010_tuning.h" - -#define LOG_FUNCTION_START(fmt, ...) \ - _LOG_FUNCTION_START(TRACE_MODULE_API, fmt, ##__VA_ARGS__) -#define LOG_FUNCTION_END(status, ...) \ - _LOG_FUNCTION_END(TRACE_MODULE_API, status, ##__VA_ARGS__) -#define LOG_FUNCTION_END_FMT(status, fmt, ...) \ - _LOG_FUNCTION_END_FMT(TRACE_MODULE_API, status, fmt, ##__VA_ARGS__) - -#ifdef VL53L0_LOG_ENABLE -#define trace_print(level, ...) \ - trace_print_module_function(TRACE_MODULE_API,\ - level, TRACE_FUNCTION_NONE, ##__VA_ARGS__) -#endif - -/* -////////////////////////////////////////////////////// -//// DEFAULT TUNING SETTINGS //// -////////////////////////////////////////////////////// -*/ -VL53L0_Error VL53L010_load_tuning_settings(VL53L0_DEV Dev) -{ - VL53L0_Error Status = VL53L0_ERROR_NONE; - LOG_FUNCTION_START(""); - - /* update 14_12_15_v11 */ - Status |= VL53L0_WrByte(Dev, 0xFF, 0x01); - Status |= VL53L0_WrByte(Dev, 0x00, 0x00); - Status |= VL53L0_WrByte(Dev, 0x91, 0x3C); - Status |= VL53L0_WrByte(Dev, 0xFF, 0x00); - Status |= VL53L0_WrByte(Dev, 0x54, 0x01); - Status |= VL53L0_WrByte(Dev, 0x33, 0x05); - Status |= VL53L0_WrByte(Dev, 0x32, 0x03); - Status |= VL53L0_WrByte(Dev, 0x30, 0x05); - Status |= VL53L0_WrByte(Dev, 0x50, 0x05); - Status |= VL53L0_WrByte(Dev, 0x60, 0x04); - Status |= VL53L0_WrByte(Dev, 0x70, 0x06); - - Status |= VL53L0_WrByte(Dev, 0x46, 0x1a); - Status |= VL53L0_WrWord(Dev, 0x51, 0x01a3); - Status |= VL53L0_WrWord(Dev, 0x61, 0x01c4); - Status |= VL53L0_WrWord(Dev, 0x71, 0x018c); - - Status |= VL53L0_WrByte(Dev, 0xFF, 0x01); - Status |= VL53L0_WrByte(Dev, 0x31, 0x0f); - Status |= VL53L0_WrByte(Dev, 0xFF, 0x00); - Status |= VL53L0_WrByte(Dev, 0x66, 0x38); - - Status |= VL53L0_WrByte(Dev, 0x47, 0x00); - Status |= VL53L0_WrByte(Dev, 0x48, 0xff); - Status |= VL53L0_WrByte(Dev, 0x57, 0x4c); - Status |= VL53L0_WrByte(Dev, 0x67, 0x3c); - Status |= VL53L0_WrByte(Dev, 0x77, 0x5c); - - Status |= VL53L0_WrWord(Dev, 0x44, 0x0000); - - Status |= VL53L0_WrByte(Dev, 0x27, 0x00); - Status |= VL53L0_WrByte(Dev, 0x55, 0x00); - - Status |= VL53L0_WrByte(Dev, 0xFF, 0x01); - Status |= VL53L0_WrByte(Dev, 0x30, 0x28); - Status |= VL53L0_WrByte(Dev, 0xFF, 0x00); - - Status |= VL53L0_WrByte(Dev, 0x10, 0x0f); - Status |= VL53L0_WrByte(Dev, 0x11, 0xff); - Status |= VL53L0_WrByte(Dev, 0x40, 0x82); - Status |= VL53L0_WrByte(Dev, 0x41, 0xff); - Status |= VL53L0_WrByte(Dev, 0x42, 0x07); - Status |= VL53L0_WrByte(Dev, 0x43, 0x12); - - Status |= VL53L0_WrByte(Dev, 0x20, 0x00); - Status |= VL53L0_WrByte(Dev, 0x21, 0x00); - - Status |= VL53L0_WrByte(Dev, 0x28, 0x06); - Status |= VL53L0_WrByte(Dev, 0xFF, 0x01); - Status |= VL53L0_WrByte(Dev, 0x48, 0x28); - Status |= VL53L0_WrByte(Dev, 0xFF, 0x00); - - Status |= VL53L0_WrByte(Dev, 0x7a, 0x0a); - Status |= VL53L0_WrByte(Dev, 0x7b, 0x00); - Status |= VL53L0_WrByte(Dev, 0x78, 0x00); - Status |= VL53L0_WrByte(Dev, 0xFF, 0x01); - Status |= VL53L0_WrByte(Dev, 0x44, 0xff); - Status |= VL53L0_WrByte(Dev, 0x45, 0x00); - Status |= VL53L0_WrByte(Dev, 0x46, 0x10); - Status |= VL53L0_WrByte(Dev, 0xFF, 0x00); - - Status |= VL53L0_WrByte(Dev, 0x04, 0x00); - Status |= VL53L0_WrByte(Dev, 0x05, 0x04); - Status |= VL53L0_WrByte(Dev, 0x06, 0x00); - Status |= VL53L0_WrByte(Dev, 0x07, 0x00); - - Status |= VL53L0_WrByte(Dev, 0xFF, 0x01); - Status |= VL53L0_WrByte(Dev, 0x0d, 0x01); - Status |= VL53L0_WrByte(Dev, 0xFF, 0x00); - Status |= VL53L0_WrByte(Dev, 0x80, 0x01); - Status |= VL53L0_WrByte(Dev, 0x01, 0xF8); - - Status |= VL53L0_WrByte(Dev, 0xFF, 0x01); - Status |= VL53L0_WrByte(Dev, 0x8e, 0x01); - Status |= VL53L0_WrByte(Dev, 0x00, 0x01); - Status |= VL53L0_WrByte(Dev, 0xFF, 0x00); - - - if (Status != 0) - Status = VL53L0_ERROR_CONTROL_INTERFACE; - - LOG_FUNCTION_END(Status); - return Status; -} diff --git a/drivers/input/misc/vl53L0/src/vl53l0_api.c b/drivers/input/misc/vl53L0/src/vl53l0_api.c index ccb446a15c4c..7b9167b17f2e 100644 --- a/drivers/input/misc/vl53L0/src/vl53l0_api.c +++ b/drivers/input/misc/vl53L0/src/vl53l0_api.c @@ -1,5 +1,5 @@ /******************************************************************************* - Copyright © 2016, STMicroelectronics International N.V. + * Copyright © 2016, STMicroelectronics International N.V. All rights reserved. Redistribution and use in source and binary forms, with or without @@ -28,6 +28,7 @@ #include "vl53l0_api.h" #include "vl53l0_tuning.h" +#include "vl53l0_interrupt_threshold_settings.h" #include "vl53l0_api_core.h" #include "vl53l0_api_histogram.h" #include "vl53l0_api_calibration.h" @@ -53,6 +54,7 @@ VL53L0_Error VL53L0_GetVersion(VL53L0_Version_t *pVersion) { VL53L0_Error Status = VL53L0_ERROR_NONE; + LOG_FUNCTION_START(""); pVersion->major = VL53L0_IMPLEMENTATION_VER_MAJOR; @@ -103,6 +105,7 @@ VL53L0_Error VL53L0_GetDeviceInfo(VL53L0_DEV Dev, VL53L0_DeviceInfo_t *pVL53L0_DeviceInfo) { VL53L0_Error Status = VL53L0_ERROR_NONE; + LOG_FUNCTION_START(""); Status = VL53L0_get_device_info(Dev, pVL53L0_DeviceInfo); @@ -146,6 +149,7 @@ VL53L0_Error VL53L0_GetRangeStatusString(uint8_t RangeStatus, char *pRangeStatusString) { VL53L0_Error Status = VL53L0_ERROR_NONE; + LOG_FUNCTION_START(""); Status = VL53L0_get_range_status_string(RangeStatus, @@ -159,6 +163,7 @@ VL53L0_Error VL53L0_GetPalErrorString(VL53L0_Error PalErrorCode, char *pPalErrorString) { VL53L0_Error Status = VL53L0_ERROR_NONE; + LOG_FUNCTION_START(""); Status = VL53L0_get_pal_error_string(PalErrorCode, pPalErrorString); @@ -171,6 +176,7 @@ VL53L0_Error VL53L0_GetPalStateString(VL53L0_State PalStateCode, char *pPalStateString) { VL53L0_Error Status = VL53L0_ERROR_NONE; + LOG_FUNCTION_START(""); Status = VL53L0_get_pal_state_string(PalStateCode, pPalStateString); @@ -182,6 +188,7 @@ VL53L0_Error VL53L0_GetPalStateString(VL53L0_State PalStateCode, VL53L0_Error VL53L0_GetPalState(VL53L0_DEV Dev, VL53L0_State *pPalState) { VL53L0_Error Status = VL53L0_ERROR_NONE; + LOG_FUNCTION_START(""); *pPalState = PALDevDataGet(Dev, PalState); @@ -193,6 +200,7 @@ VL53L0_Error VL53L0_GetPalState(VL53L0_DEV Dev, VL53L0_State *pPalState) VL53L0_Error VL53L0_SetPowerMode(VL53L0_DEV Dev, VL53L0_PowerModes PowerMode) { VL53L0_Error Status = VL53L0_ERROR_NONE; + LOG_FUNCTION_START(""); /* Only level1 of Power mode exists */ @@ -229,6 +237,7 @@ VL53L0_Error VL53L0_GetPowerMode(VL53L0_DEV Dev, VL53L0_PowerModes *pPowerMode) { VL53L0_Error Status = VL53L0_ERROR_NONE; uint8_t Byte; + LOG_FUNCTION_START(""); /* Only level1 of Power mode exists */ @@ -252,6 +261,7 @@ VL53L0_Error VL53L0_SetOffsetCalibrationDataMicroMeter(VL53L0_DEV Dev, int32_t OffsetCalibrationDataMicroMeter) { VL53L0_Error Status = VL53L0_ERROR_NONE; + LOG_FUNCTION_START(""); Status = VL53L0_set_offset_calibration_data_micro_meter(Dev, @@ -265,6 +275,7 @@ VL53L0_Error VL53L0_GetOffsetCalibrationDataMicroMeter(VL53L0_DEV Dev, int32_t *pOffsetCalibrationDataMicroMeter) { VL53L0_Error Status = VL53L0_ERROR_NONE; + LOG_FUNCTION_START(""); Status = VL53L0_get_offset_calibration_data_micro_meter(Dev, @@ -278,6 +289,7 @@ VL53L0_Error VL53L0_SetLinearityCorrectiveGain(VL53L0_DEV Dev, int16_t LinearityCorrectiveGain) { VL53L0_Error Status = VL53L0_ERROR_NONE; + LOG_FUNCTION_START(""); if ((LinearityCorrectiveGain < 0) || (LinearityCorrectiveGain > 1000)) @@ -301,6 +313,7 @@ VL53L0_Error VL53L0_GetLinearityCorrectiveGain(VL53L0_DEV Dev, uint16_t *pLinearityCorrectiveGain) { VL53L0_Error Status = VL53L0_ERROR_NONE; + LOG_FUNCTION_START(""); *pLinearityCorrectiveGain = PALDevDataGet(Dev, LinearityCorrectiveGain); @@ -312,6 +325,7 @@ VL53L0_Error VL53L0_GetLinearityCorrectiveGain(VL53L0_DEV Dev, VL53L0_Error VL53L0_SetGroupParamHold(VL53L0_DEV Dev, uint8_t GroupParamHold) { VL53L0_Error Status = VL53L0_ERROR_NOT_IMPLEMENTED; + LOG_FUNCTION_START(""); /* not implemented on VL53L0 */ @@ -324,6 +338,7 @@ VL53L0_Error VL53L0_GetUpperLimitMilliMeter(VL53L0_DEV Dev, uint16_t *pUpperLimitMilliMeter) { VL53L0_Error Status = VL53L0_ERROR_NOT_IMPLEMENTED; + LOG_FUNCTION_START(""); /* not implemented on VL53L0 */ @@ -355,6 +370,7 @@ VL53L0_Error VL53L0_GetTotalSignalRate(VL53L0_DEV Dev, VL53L0_Error VL53L0_SetDeviceAddress(VL53L0_DEV Dev, uint8_t DeviceAddress) { VL53L0_Error Status = VL53L0_ERROR_NONE; + LOG_FUNCTION_START(""); Status = VL53L0_WrByte(Dev, VL53L0_REG_I2C_SLAVE_DEVICE_ADDRESS, @@ -369,11 +385,13 @@ VL53L0_Error VL53L0_DataInit(VL53L0_DEV Dev) VL53L0_Error Status = VL53L0_ERROR_NONE; VL53L0_DeviceParameters_t CurrentParameters; int i; + uint8_t StopVariable; LOG_FUNCTION_START(""); /* by default the I2C is running at 1V8 if you want to change it you - * need to include this define at compilation level. */ + * need to include this define at compilation level. + */ #ifdef USE_I2C_2V8 Status = VL53L0_UpdateByte(Dev, VL53L0_REG_VHV_CONFIG_PAD_SCL_SDA__EXTSUP_HV, @@ -395,13 +413,9 @@ VL53L0_Error VL53L0_DataInit(VL53L0_DEV Dev) /* Default value is 1000 for Linearity Corrective Gain */ PALDevDataSet(Dev, LinearityCorrectiveGain, 1000); - /* Dmax default Parameter */ - PALDevDataSet(Dev, DmaxCalRangeMilliMeter, 400); - PALDevDataSet(Dev, DmaxCalSignalRateRtnMegaCps, - (FixPoint1616_t)((0x00016B85))); /* 1.42 No Cover Glass*/ - /* Set Default static parameters - *set first temporary values 9.44MHz * 65536 = 618660 */ + *set first temporary values 9.44MHz * 65536 = 618660 + */ VL53L0_SETDEVICESPECIFICPARAMETER(Dev, OscFrequencyMHz, 618660); /* Set Default XTalkCompensationRateMegaCps to 0 */ @@ -413,6 +427,37 @@ VL53L0_Error VL53L0_DataInit(VL53L0_DEV Dev) /* initialize PAL values */ CurrentParameters.DeviceMode = VL53L0_DEVICEMODE_SINGLE_RANGING; CurrentParameters.HistogramMode = VL53L0_HISTOGRAMMODE_DISABLED; + + /* Dmax lookup table */ + /* 0.0 */ + CurrentParameters.dmax_lut.ambRate_mcps[0] = (FixPoint1616_t)0x00000000; + /* 1200 */ + CurrentParameters.dmax_lut.dmax_mm[0] = (FixPoint1616_t)0x04B00000; + /* 0.7 */ + CurrentParameters.dmax_lut.ambRate_mcps[1] = (FixPoint1616_t)0x0000B333; + /* 1100 */ + CurrentParameters.dmax_lut.dmax_mm[1] = (FixPoint1616_t)0x044C0000; + /* 2 */ + CurrentParameters.dmax_lut.ambRate_mcps[2] = (FixPoint1616_t)0x00020000; + /* 900 */ + CurrentParameters.dmax_lut.dmax_mm[2] = (FixPoint1616_t)0x03840000; + /* 3.8 */ + CurrentParameters.dmax_lut.ambRate_mcps[3] = (FixPoint1616_t)0x0003CCCC; + /* 750 */ + CurrentParameters.dmax_lut.dmax_mm[3] = (FixPoint1616_t)0x02EE0000; + /* 7.3 */ + CurrentParameters.dmax_lut.ambRate_mcps[4] = (FixPoint1616_t)0x00074CCC; + /* 550 */ + CurrentParameters.dmax_lut.dmax_mm[4] = (FixPoint1616_t)0x02260000; + /* 10 */ + CurrentParameters.dmax_lut.ambRate_mcps[5] = (FixPoint1616_t)0x000A0000; + /* 500 */ + CurrentParameters.dmax_lut.dmax_mm[5] = (FixPoint1616_t)0x01F40000; + /* 15 */ + CurrentParameters.dmax_lut.ambRate_mcps[6] = (FixPoint1616_t)0x000F0000; + /* 400 */ + CurrentParameters.dmax_lut.dmax_mm[6] = (FixPoint1616_t)0x01900000; + PALDevDataSet(Dev, CurrentParameters, CurrentParameters); } @@ -425,6 +470,15 @@ VL53L0_Error VL53L0_DataInit(VL53L0_DEV Dev) /* Use internal default settings */ PALDevDataSet(Dev, UseInternalTuningSettings, 1); + Status |= VL53L0_WrByte(Dev, 0x80, 0x01); + Status |= VL53L0_WrByte(Dev, 0xFF, 0x01); + Status |= VL53L0_WrByte(Dev, 0x00, 0x00); + Status |= VL53L0_RdByte(Dev, 0x91, &StopVariable); + PALDevDataSet(Dev, StopVariable, StopVariable); + Status |= VL53L0_WrByte(Dev, 0x00, 0x01); + Status |= VL53L0_WrByte(Dev, 0xFF, 0x00); + Status |= VL53L0_WrByte(Dev, 0x80, 0x00); + /* Enable all check */ for (i = 0; i < VL53L0_CHECKENABLE_NUMBER_OF_CHECKS; i++) { if (Status == VL53L0_ERROR_NONE) @@ -443,6 +497,14 @@ VL53L0_Error VL53L0_DataInit(VL53L0_DEV Dev) Status = VL53L0_SetLimitCheckEnable(Dev, VL53L0_CHECKENABLE_RANGE_IGNORE_THRESHOLD, 0); + if (Status == VL53L0_ERROR_NONE) + Status = VL53L0_SetLimitCheckEnable(Dev, + VL53L0_CHECKENABLE_SIGNAL_RATE_MSRC, 0); + + if (Status == VL53L0_ERROR_NONE) + Status = VL53L0_SetLimitCheckEnable(Dev, + VL53L0_CHECKENABLE_SIGNAL_RATE_PRE_RANGE, 0); + /* Limit default values */ if (Status == VL53L0_ERROR_NONE) { Status = VL53L0_SetLimitCheckValue(Dev, @@ -475,7 +537,8 @@ VL53L0_Error VL53L0_DataInit(VL53L0_DEV Dev) 0xFF); /* Set PAL state to tell that we are waiting for call to - * VL53L0_StaticInit */ + * VL53L0_StaticInit + */ PALDevDataSet(Dev, PalState, VL53L0_STATE_WAIT_STATICINIT); } @@ -542,7 +605,7 @@ VL53L0_Error VL53L0_StaticInit(VL53L0_DEV Dev) uint32_t refSpadCount = 0; uint8_t ApertureSpads = 0; uint8_t vcselPulsePeriodPCLK; - FixPoint1616_t seqTimeoutMilliSecs; + uint32_t seqTimeoutMicroSecs; LOG_FUNCTION_START(""); @@ -582,9 +645,6 @@ VL53L0_Error VL53L0_StaticInit(VL53L0_DEV Dev) if (Status == VL53L0_ERROR_NONE) Status = VL53L0_load_tuning_settings(Dev, pTuningSettingBuffer); - if (Status == VL53L0_ERROR_NONE) - Status = VL53L0_WrByte(Dev, 0x80, 0x00); - /* Set interrupt config to new sample ready */ if (Status == VL53L0_ERROR_NONE) { @@ -605,7 +665,8 @@ VL53L0_Error VL53L0_StaticInit(VL53L0_DEV Dev) } /* After static init, some device parameters may be changed, - * so update them */ + * so update them + */ if (Status == VL53L0_ERROR_NONE) Status = VL53L0_GetDeviceParameters(Dev, &CurrentParameters); @@ -656,10 +717,10 @@ VL53L0_Error VL53L0_StaticInit(VL53L0_DEV Dev) } if (Status == VL53L0_ERROR_NONE) { - VL53L0_SETDEVICESPECIFICPARAMETER( - Dev, - PreRangeVcselPulsePeriod, - vcselPulsePeriodPCLK); + VL53L0_SETDEVICESPECIFICPARAMETER( + Dev, + PreRangeVcselPulsePeriod, + vcselPulsePeriodPCLK); } /* Store final-range vcsel period */ @@ -671,40 +732,40 @@ VL53L0_Error VL53L0_StaticInit(VL53L0_DEV Dev) } if (Status == VL53L0_ERROR_NONE) { - VL53L0_SETDEVICESPECIFICPARAMETER( - Dev, - FinalRangeVcselPulsePeriod, - vcselPulsePeriodPCLK); + VL53L0_SETDEVICESPECIFICPARAMETER( + Dev, + FinalRangeVcselPulsePeriod, + vcselPulsePeriodPCLK); } /* Store pre-range timeout */ if (Status == VL53L0_ERROR_NONE) { - Status = VL53L0_GetSequenceStepTimeout( + Status = get_sequence_step_timeout( Dev, VL53L0_SEQUENCESTEP_PRE_RANGE, - &seqTimeoutMilliSecs); + &seqTimeoutMicroSecs); } if (Status == VL53L0_ERROR_NONE) { VL53L0_SETDEVICESPECIFICPARAMETER( Dev, PreRangeTimeoutMicroSecs, - seqTimeoutMilliSecs); + seqTimeoutMicroSecs); } /* Store final-range timeout */ if (Status == VL53L0_ERROR_NONE) { - Status = VL53L0_GetSequenceStepTimeout( + Status = get_sequence_step_timeout( Dev, VL53L0_SEQUENCESTEP_FINAL_RANGE, - &seqTimeoutMilliSecs); + &seqTimeoutMicroSecs); } if (Status == VL53L0_ERROR_NONE) { VL53L0_SETDEVICESPECIFICPARAMETER( Dev, FinalRangeTimeoutMicroSecs, - seqTimeoutMilliSecs); + seqTimeoutMicroSecs); } LOG_FUNCTION_END(Status); @@ -714,6 +775,7 @@ VL53L0_Error VL53L0_StaticInit(VL53L0_DEV Dev) VL53L0_Error VL53L0_WaitDeviceBooted(VL53L0_DEV Dev) { VL53L0_Error Status = VL53L0_ERROR_NOT_IMPLEMENTED; + LOG_FUNCTION_START(""); /* not implemented on VL53L0 */ @@ -726,6 +788,7 @@ VL53L0_Error VL53L0_ResetDevice(VL53L0_DEV Dev) { VL53L0_Error Status = VL53L0_ERROR_NONE; uint8_t Byte; + LOG_FUNCTION_START(""); /* Set reset bit */ @@ -740,6 +803,8 @@ VL53L0_Error VL53L0_ResetDevice(VL53L0_DEV Dev) } while (Byte != 0x00); } + VL53L0_PollingDelay(Dev); + /* Release reset */ Status = VL53L0_WrByte(Dev, VL53L0_REG_SOFT_RESET_GO2_SOFT_RESET_N, 0x01); @@ -752,6 +817,8 @@ VL53L0_Error VL53L0_ResetDevice(VL53L0_DEV Dev) } while (Byte == 0x00); } + VL53L0_PollingDelay(Dev); + /* Set PAL State to VL53L0_STATE_POWERDOWN */ if (Status == VL53L0_ERROR_NONE) PALDevDataSet(Dev, PalState, VL53L0_STATE_POWERDOWN); @@ -768,6 +835,7 @@ VL53L0_Error VL53L0_SetDeviceParameters(VL53L0_DEV Dev, { VL53L0_Error Status = VL53L0_ERROR_NONE; int i; + LOG_FUNCTION_START(""); Status = VL53L0_SetDeviceMode(Dev, pDeviceParameters->DeviceMode); @@ -883,6 +951,15 @@ VL53L0_Error VL53L0_GetDeviceParameters(VL53L0_DEV Dev, &(pDeviceParameters->MeasurementTimingBudgetMicroSeconds)); } + if (Status == VL53L0_ERROR_NONE) { + for (i = 0; i < VL53L0_DMAX_LUT_SIZE; i++) { + pDeviceParameters->dmax_lut.ambRate_mcps[i] = + Dev->Data.CurrentParameters.dmax_lut.ambRate_mcps[i]; + pDeviceParameters->dmax_lut.dmax_mm[i] = + Dev->Data.CurrentParameters.dmax_lut.dmax_mm[i]; + } + } + LOG_FUNCTION_END(Status); return Status; } @@ -916,6 +993,7 @@ VL53L0_Error VL53L0_GetDeviceMode(VL53L0_DEV Dev, VL53L0_DeviceModes *pDeviceMode) { VL53L0_Error Status = VL53L0_ERROR_NONE; + LOG_FUNCTION_START(""); VL53L0_GETPARAMETERFIELD(Dev, DeviceMode, *pDeviceMode); @@ -942,6 +1020,7 @@ VL53L0_Error VL53L0_SetRangeFractionEnable(VL53L0_DEV Dev, uint8_t Enable) VL53L0_Error VL53L0_GetFractionEnable(VL53L0_DEV Dev, uint8_t *pEnabled) { VL53L0_Error Status = VL53L0_ERROR_NONE; + LOG_FUNCTION_START(""); Status = VL53L0_RdByte(Dev, VL53L0_REG_SYSTEM_RANGE_CONFIG, pEnabled); @@ -957,6 +1036,7 @@ VL53L0_Error VL53L0_SetHistogramMode(VL53L0_DEV Dev, VL53L0_HistogramModes HistogramMode) { VL53L0_Error Status = VL53L0_ERROR_NONE; + LOG_FUNCTION_START("%d", (int)HistogramMode); Status = VL53L0_set_histogram_mode(Dev, HistogramMode); @@ -969,6 +1049,7 @@ VL53L0_Error VL53L0_GetHistogramMode(VL53L0_DEV Dev, VL53L0_HistogramModes *pHistogramMode) { VL53L0_Error Status = VL53L0_ERROR_NONE; + LOG_FUNCTION_START(""); Status = VL53L0_get_histogram_mode(Dev, pHistogramMode); @@ -981,6 +1062,7 @@ VL53L0_Error VL53L0_SetMeasurementTimingBudgetMicroSeconds(VL53L0_DEV Dev, uint32_t MeasurementTimingBudgetMicroSeconds) { VL53L0_Error Status = VL53L0_ERROR_NONE; + LOG_FUNCTION_START(""); Status = VL53L0_set_measurement_timing_budget_micro_seconds(Dev, @@ -995,6 +1077,7 @@ VL53L0_Error VL53L0_GetMeasurementTimingBudgetMicroSeconds(VL53L0_DEV Dev, uint32_t *pMeasurementTimingBudgetMicroSeconds) { VL53L0_Error Status = VL53L0_ERROR_NONE; + LOG_FUNCTION_START(""); Status = VL53L0_get_measurement_timing_budget_micro_seconds(Dev, @@ -1008,6 +1091,7 @@ VL53L0_Error VL53L0_SetVcselPulsePeriod(VL53L0_DEV Dev, VL53L0_VcselPeriod VcselPeriodType, uint8_t VCSELPulsePeriodPCLK) { VL53L0_Error Status = VL53L0_ERROR_NONE; + LOG_FUNCTION_START(""); Status = VL53L0_set_vcsel_pulse_period(Dev, VcselPeriodType, @@ -1021,6 +1105,7 @@ VL53L0_Error VL53L0_GetVcselPulsePeriod(VL53L0_DEV Dev, VL53L0_VcselPeriod VcselPeriodType, uint8_t *pVCSELPulsePeriodPCLK) { VL53L0_Error Status = VL53L0_ERROR_NONE; + LOG_FUNCTION_START(""); Status = VL53L0_get_vcsel_pulse_period(Dev, VcselPeriodType, @@ -1037,6 +1122,7 @@ VL53L0_Error VL53L0_SetSequenceStepEnable(VL53L0_DEV Dev, uint8_t SequenceConfig = 0; uint8_t SequenceConfigNew = 0; uint32_t MeasurementTimingBudgetMicroSeconds; + LOG_FUNCTION_START(""); Status = VL53L0_RdByte(Dev, VL53L0_REG_SYSTEM_SEQUENCE_CONFIG, @@ -1156,6 +1242,7 @@ VL53L0_Error VL53L0_GetSequenceStepEnable(VL53L0_DEV Dev, { VL53L0_Error Status = VL53L0_ERROR_NONE; uint8_t SequenceConfig = 0; + LOG_FUNCTION_START(""); Status = VL53L0_RdByte(Dev, VL53L0_REG_SYSTEM_SEQUENCE_CONFIG, @@ -1175,6 +1262,7 @@ VL53L0_Error VL53L0_GetSequenceStepEnables(VL53L0_DEV Dev, { VL53L0_Error Status = VL53L0_ERROR_NONE; uint8_t SequenceConfig = 0; + LOG_FUNCTION_START(""); Status = VL53L0_RdByte(Dev, VL53L0_REG_SYSTEM_SEQUENCE_CONFIG, @@ -1214,6 +1302,7 @@ VL53L0_Error VL53L0_GetNumberOfSequenceSteps(VL53L0_DEV Dev, uint8_t *pNumberOfSequenceSteps) { VL53L0_Error Status = VL53L0_ERROR_NONE; + LOG_FUNCTION_START(""); *pNumberOfSequenceSteps = VL53L0_SEQUENCESTEP_NUMBER_OF_CHECKS; @@ -1226,6 +1315,7 @@ VL53L0_Error VL53L0_GetSequenceStepsInfo(VL53L0_SequenceStepId SequenceStepId, char *pSequenceStepsString) { VL53L0_Error Status = VL53L0_ERROR_NONE; + LOG_FUNCTION_START(""); Status = VL53L0_get_sequence_steps_info( @@ -1265,8 +1355,8 @@ VL53L0_Error VL53L0_SetSequenceStepTimeout(VL53L0_DEV Dev, MeasurementTimingBudgetMicroSeconds); /* At this point we don't know if the requested value is valid, - therefore proceed to update the entire timing budget and - if this fails, revert back to the previous value. + * therefore proceed to update the entire timing budget and + * if this fails, revert back to the previous value. */ Status = VL53L0_SetMeasurementTimingBudgetMicroSeconds(Dev, MeasurementTimingBudgetMicroSeconds); @@ -1296,17 +1386,15 @@ VL53L0_Error VL53L0_GetSequenceStepTimeout(VL53L0_DEV Dev, { VL53L0_Error Status = VL53L0_ERROR_NONE; uint32_t TimeoutMicroSeconds; - uint32_t WholeNumber_ms = 0; - uint32_t Fraction_ms = 0; + LOG_FUNCTION_START(""); Status = get_sequence_step_timeout(Dev, SequenceStepId, &TimeoutMicroSeconds); if (Status == VL53L0_ERROR_NONE) { - WholeNumber_ms = TimeoutMicroSeconds / 1000; - Fraction_ms = TimeoutMicroSeconds - (WholeNumber_ms * 1000); - *pTimeOutMilliSecs = (WholeNumber_ms << 16) - + (((Fraction_ms * 0xffff) + 500) / 1000); + TimeoutMicroSeconds <<= 8; + *pTimeOutMilliSecs = (TimeoutMicroSeconds + 500)/1000; + *pTimeOutMilliSecs <<= 8; } LOG_FUNCTION_END(Status); @@ -1424,6 +1512,7 @@ VL53L0_Error VL53L0_GetXTalkCompensationEnable(VL53L0_DEV Dev, { VL53L0_Error Status = VL53L0_ERROR_NONE; uint8_t Temp8; + LOG_FUNCTION_START(""); VL53L0_GETPARAMETERFIELD(Dev, XTalkCompensationEnable, Temp8); @@ -1440,6 +1529,7 @@ VL53L0_Error VL53L0_SetXTalkCompensationRateMegaCps(VL53L0_DEV Dev, uint8_t Temp8; uint16_t LinearityCorrectiveGain; uint16_t data; + LOG_FUNCTION_START(""); VL53L0_GETPARAMETERFIELD(Dev, XTalkCompensationEnable, Temp8); @@ -1508,6 +1598,7 @@ VL53L0_Error VL53L0_SetRefCalibration(VL53L0_DEV Dev, uint8_t VhvSettings, uint8_t PhaseCal) { VL53L0_Error Status = VL53L0_ERROR_NONE; + LOG_FUNCTION_START(""); Status = VL53L0_set_ref_calibration(Dev, VhvSettings, PhaseCal); @@ -1520,6 +1611,7 @@ VL53L0_Error VL53L0_GetRefCalibration(VL53L0_DEV Dev, uint8_t *pVhvSettings, uint8_t *pPhaseCal) { VL53L0_Error Status = VL53L0_ERROR_NONE; + LOG_FUNCTION_START(""); Status = VL53L0_get_ref_calibration(Dev, pVhvSettings, pPhaseCal); @@ -1535,6 +1627,7 @@ VL53L0_Error VL53L0_GetRefCalibration(VL53L0_DEV Dev, uint8_t *pVhvSettings, VL53L0_Error VL53L0_GetNumberOfLimitCheck(uint16_t *pNumberOfLimitCheck) { VL53L0_Error Status = VL53L0_ERROR_NONE; + LOG_FUNCTION_START(""); *pNumberOfLimitCheck = VL53L0_CHECKENABLE_NUMBER_OF_CHECKS; @@ -1585,7 +1678,9 @@ VL53L0_Error VL53L0_SetLimitCheckEnable(VL53L0_DEV Dev, uint16_t LimitCheckId, { VL53L0_Error Status = VL53L0_ERROR_NONE; FixPoint1616_t TempFix1616 = 0; - uint8_t LimitCheckEnableInt; + uint8_t LimitCheckEnableInt = 0; + uint8_t LimitCheckDisable = 0; + uint8_t Temp8; LOG_FUNCTION_START(""); @@ -1595,9 +1690,12 @@ VL53L0_Error VL53L0_SetLimitCheckEnable(VL53L0_DEV Dev, uint16_t LimitCheckId, if (LimitCheckEnable == 0) { TempFix1616 = 0; LimitCheckEnableInt = 0; + LimitCheckDisable = 1; + } else { VL53L0_GETARRAYPARAMETERFIELD(Dev, LimitChecksValue, LimitCheckId, TempFix1616); + LimitCheckDisable = 0; /* this to be sure to have either 0 or 1 */ LimitCheckEnableInt = 1; } @@ -1638,6 +1736,25 @@ VL53L0_Error VL53L0_SetLimitCheckEnable(VL53L0_DEV Dev, uint16_t LimitCheckId, break; + case VL53L0_CHECKENABLE_SIGNAL_RATE_MSRC: + + Temp8 = (uint8_t)(LimitCheckDisable << 1); + Status = VL53L0_UpdateByte(Dev, + VL53L0_REG_MSRC_CONFIG_CONTROL, + 0xFE, Temp8); + + break; + + case VL53L0_CHECKENABLE_SIGNAL_RATE_PRE_RANGE: + + Temp8 = (uint8_t)(LimitCheckDisable << 4); + Status = VL53L0_UpdateByte(Dev, + VL53L0_REG_MSRC_CONFIG_CONTROL, + 0xEF, Temp8); + + break; + + default: Status = VL53L0_ERROR_INVALID_PARAMS; @@ -1732,6 +1849,16 @@ VL53L0_Error VL53L0_SetLimitCheckValue(VL53L0_DEV Dev, uint16_t LimitCheckId, break; + case VL53L0_CHECKENABLE_SIGNAL_RATE_MSRC: + case VL53L0_CHECKENABLE_SIGNAL_RATE_PRE_RANGE: + + Status = VL53L0_WrWord(Dev, + VL53L0_REG_PRE_RANGE_MIN_COUNT_RATE_RTN_LIMIT, + VL53L0_FIXPOINT1616TOFIXPOINT97( + LimitCheckValue)); + + break; + default: Status = VL53L0_ERROR_INVALID_PARAMS; @@ -1791,6 +1918,18 @@ VL53L0_Error VL53L0_GetLimitCheckValue(VL53L0_DEV Dev, uint16_t LimitCheckId, EnableZeroValue = 0; break; + case VL53L0_CHECKENABLE_SIGNAL_RATE_MSRC: + case VL53L0_CHECKENABLE_SIGNAL_RATE_PRE_RANGE: + Status = VL53L0_RdWord(Dev, + VL53L0_REG_PRE_RANGE_MIN_COUNT_RATE_RTN_LIMIT, + &Temp16); + if (Status == VL53L0_ERROR_NONE) + TempFix1616 = VL53L0_FIXPOINT97TOFIXPOINT1616(Temp16); + + + EnableZeroValue = 0; + break; + default: Status = VL53L0_ERROR_INVALID_PARAMS; @@ -1869,6 +2008,24 @@ VL53L0_Error VL53L0_GetLimitCheckCurrent(VL53L0_DEV Dev, uint16_t LimitCheckId, break; + case VL53L0_CHECKENABLE_SIGNAL_RATE_MSRC: + /* Need to run a ranging to have the latest values */ + LastRangeDataBuffer = PALDevDataGet(Dev, + LastRangeMeasure); + *pLimitCheckCurrent = + LastRangeDataBuffer.SignalRateRtnMegaCps; + + break; + + case VL53L0_CHECKENABLE_SIGNAL_RATE_PRE_RANGE: + /* Need to run a ranging to have the latest values */ + LastRangeDataBuffer = PALDevDataGet(Dev, + LastRangeMeasure); + *pLimitCheckCurrent = + LastRangeDataBuffer.SignalRateRtnMegaCps; + + break; + default: Status = VL53L0_ERROR_INVALID_PARAMS; } @@ -1939,55 +2096,6 @@ VL53L0_Error VL53L0_GetWrapAroundCheckEnable(VL53L0_DEV Dev, return Status; } -VL53L0_Error VL53L0_SetDmaxCalParameters(VL53L0_DEV Dev, - uint16_t RangeMilliMeter, FixPoint1616_t SignalRateRtnMegaCps) -{ - VL53L0_Error Status = VL53L0_ERROR_NONE; - FixPoint1616_t SignalRateRtnMegaCpsTemp = 0; - - LOG_FUNCTION_START(""); - - /* Check if one of input parameter is zero, in that case the - * value are get from NVM */ - if ((RangeMilliMeter == 0) || (SignalRateRtnMegaCps == 0)) { - /* NVM parameters */ - /* Run VL53L0_get_info_from_device wit option 4 to get - * signal rate at 400 mm if the value have been already - * get this function will return with no access to device */ - VL53L0_get_info_from_device(Dev, 4); - - SignalRateRtnMegaCpsTemp = VL53L0_GETDEVICESPECIFICPARAMETER( - Dev, SignalRateMeasFixed400mm); - - PALDevDataSet(Dev, DmaxCalRangeMilliMeter, 400); - PALDevDataSet(Dev, DmaxCalSignalRateRtnMegaCps, - SignalRateRtnMegaCpsTemp); - } else { - /* User parameters */ - PALDevDataSet(Dev, DmaxCalRangeMilliMeter, RangeMilliMeter); - PALDevDataSet(Dev, DmaxCalSignalRateRtnMegaCps, - SignalRateRtnMegaCps); - } - - LOG_FUNCTION_END(Status); - return Status; -} - -VL53L0_Error VL53L0_GetDmaxCalParameters(VL53L0_DEV Dev, - uint16_t *pRangeMilliMeter, FixPoint1616_t *pSignalRateRtnMegaCps) -{ - VL53L0_Error Status = VL53L0_ERROR_NONE; - - LOG_FUNCTION_START(""); - - *pRangeMilliMeter = PALDevDataGet(Dev, DmaxCalRangeMilliMeter); - *pSignalRateRtnMegaCps = PALDevDataGet(Dev, - DmaxCalSignalRateRtnMegaCps); - - LOG_FUNCTION_END(Status); - return Status; -} - /* End Group PAL Parameters Functions */ /* Group PAL Measurement Functions */ @@ -2002,7 +2110,8 @@ VL53L0_Error VL53L0_PerformSingleMeasurement(VL53L0_DEV Dev) Status = VL53L0_GetDeviceMode(Dev, &DeviceMode); /* Start immediately to run a single ranging measurement in case of - * single ranging or single histogram */ + * single ranging or single histogram + */ if (Status == VL53L0_ERROR_NONE && DeviceMode == VL53L0_DEVICEMODE_SINGLE_RANGING) Status = VL53L0_StartMeasurement(Dev); @@ -2026,6 +2135,7 @@ VL53L0_Error VL53L0_PerformSingleHistogramMeasurement(VL53L0_DEV Dev, VL53L0_HistogramMeasurementData_t *pHistogramMeasurementData) { VL53L0_Error Status = VL53L0_ERROR_NONE; + LOG_FUNCTION_START(""); Status = VL53L0_perform_single_histogram_measurement(Dev, @@ -2039,6 +2149,7 @@ VL53L0_Error VL53L0_PerformRefCalibration(VL53L0_DEV Dev, uint8_t *pVhvSettings, uint8_t *pPhaseCal) { VL53L0_Error Status = VL53L0_ERROR_NONE; + LOG_FUNCTION_START(""); Status = VL53L0_perform_ref_calibration(Dev, pVhvSettings, @@ -2053,6 +2164,7 @@ VL53L0_Error VL53L0_PerformXTalkMeasurement(VL53L0_DEV Dev, uint8_t *pAmbientTooHigh) { VL53L0_Error Status = VL53L0_ERROR_NONE; + LOG_FUNCTION_START(""); Status = VL53L0_perform_xtalk_measurement(Dev, TimeoutMs, @@ -2067,6 +2179,7 @@ VL53L0_Error VL53L0_PerformXTalkCalibration(VL53L0_DEV Dev, FixPoint1616_t *pXTalkCompensationRateMegaCps) { VL53L0_Error Status = VL53L0_ERROR_NONE; + LOG_FUNCTION_START(""); Status = VL53L0_perform_xtalk_calibration(Dev, XTalkCalDistance, @@ -2080,6 +2193,7 @@ VL53L0_Error VL53L0_PerformOffsetCalibration(VL53L0_DEV Dev, FixPoint1616_t CalDistanceMilliMeter, int32_t *pOffsetMicroMeter) { VL53L0_Error Status = VL53L0_ERROR_NONE; + LOG_FUNCTION_START(""); Status = VL53L0_perform_offset_calibration(Dev, CalDistanceMilliMeter, @@ -2089,6 +2203,82 @@ VL53L0_Error VL53L0_PerformOffsetCalibration(VL53L0_DEV Dev, return Status; } +VL53L0_Error VL53L0_CheckAndLoadInterruptSettings(VL53L0_DEV Dev, + uint8_t StartNotStopFlag) +{ + uint8_t InterruptConfig; + FixPoint1616_t ThresholdLow; + FixPoint1616_t ThresholdHigh; + VL53L0_Error Status = VL53L0_ERROR_NONE; + + LOG_FUNCTION_START(""); + + InterruptConfig = VL53L0_GETDEVICESPECIFICPARAMETER(Dev, + Pin0GpioFunctionality); + + switch (InterruptConfig) { + case VL53L0_GPIOFUNCTIONALITY_THRESHOLD_CROSSED_LOW: + Status = VL53L0_GetInterruptThresholds(Dev, + VL53L0_DEVICEMODE_CONTINUOUS_RANGING, + &ThresholdLow, &ThresholdHigh); + + if ((ThresholdLow > 255*65536) && + (Status == VL53L0_ERROR_NONE)) { + + if (StartNotStopFlag != 0) { + Status = VL53L0_load_tuning_settings(Dev, + InterruptThresholdSettings); + } else { + Status |= VL53L0_WrByte(Dev, 0xFF, 0x04); + Status |= VL53L0_WrByte(Dev, 0x70, 0x00); + Status |= VL53L0_WrByte(Dev, 0xFF, 0x00); + Status |= VL53L0_WrByte(Dev, 0x80, 0x00); + } + } + break; + case VL53L0_GPIOFUNCTIONALITY_THRESHOLD_CROSSED_HIGH: + Status = VL53L0_GetInterruptThresholds(Dev, + VL53L0_DEVICEMODE_CONTINUOUS_RANGING, + &ThresholdLow, &ThresholdHigh); + + if ((ThresholdHigh > 0) && + (Status == VL53L0_ERROR_NONE)) { + + if (StartNotStopFlag != 0) { + Status = VL53L0_load_tuning_settings(Dev, + InterruptThresholdSettings); + } else { + Status |= VL53L0_WrByte(Dev, 0xFF, 0x04); + Status |= VL53L0_WrByte(Dev, 0x70, 0x00); + Status |= VL53L0_WrByte(Dev, 0xFF, 0x00); + Status |= VL53L0_WrByte(Dev, 0x80, 0x00); + } + } + break; + case VL53L0_GPIOFUNCTIONALITY_THRESHOLD_CROSSED_OUT: + Status = VL53L0_GetInterruptThresholds(Dev, + VL53L0_DEVICEMODE_CONTINUOUS_RANGING, + &ThresholdLow, &ThresholdHigh); + + if (Status == VL53L0_ERROR_NONE) { + if (StartNotStopFlag != 0) { + Status = VL53L0_load_tuning_settings(Dev, + InterruptThresholdSettings); + } else { + Status |= VL53L0_WrByte(Dev, 0xFF, 0x04); + Status |= VL53L0_WrByte(Dev, 0x70, 0x00); + Status |= VL53L0_WrByte(Dev, 0xFF, 0x00); + Status |= VL53L0_WrByte(Dev, 0x80, 0x00); + } + } + break; + } + + LOG_FUNCTION_END(Status); + return Status; +} + + VL53L0_Error VL53L0_StartMeasurement(VL53L0_DEV Dev) { VL53L0_Error Status = VL53L0_ERROR_NONE; @@ -2096,22 +2286,53 @@ VL53L0_Error VL53L0_StartMeasurement(VL53L0_DEV Dev) uint8_t Byte; uint8_t StartStopByte = VL53L0_REG_SYSRANGE_MODE_START_STOP; uint32_t LoopNb; + LOG_FUNCTION_START(""); /* Get Current DeviceMode */ VL53L0_GetDeviceMode(Dev, &DeviceMode); + Status = VL53L0_WrByte(Dev, 0x80, 0x01); + Status = VL53L0_WrByte(Dev, 0xFF, 0x01); + Status = VL53L0_WrByte(Dev, 0x00, 0x00); + Status = VL53L0_WrByte(Dev, 0x91, PALDevDataGet(Dev, StopVariable)); + Status = VL53L0_WrByte(Dev, 0x00, 0x01); + Status = VL53L0_WrByte(Dev, 0xFF, 0x00); + Status = VL53L0_WrByte(Dev, 0x80, 0x00); + switch (DeviceMode) { case VL53L0_DEVICEMODE_SINGLE_RANGING: - Status = VL53L0_WrByte(Dev, - VL53L0_REG_SYSRANGE_START, - VL53L0_REG_SYSRANGE_MODE_SINGLESHOT | StartStopByte); + Status = VL53L0_WrByte(Dev, VL53L0_REG_SYSRANGE_START, 0x01); + + Byte = StartStopByte; + if (Status == VL53L0_ERROR_NONE) { + /* Wait until start bit has been cleared */ + LoopNb = 0; + do { + if (LoopNb > 0) + Status = VL53L0_RdByte(Dev, + VL53L0_REG_SYSRANGE_START, &Byte); + LoopNb = LoopNb + 1; + } while (((Byte & StartStopByte) == StartStopByte) + && (Status == VL53L0_ERROR_NONE) + && (LoopNb < VL53L0_DEFAULT_MAX_LOOP)); + + if (LoopNb >= VL53L0_DEFAULT_MAX_LOOP) + Status = VL53L0_ERROR_TIME_OUT; + + } + break; case VL53L0_DEVICEMODE_CONTINUOUS_RANGING: /* Back-to-back mode */ + + /* Check if need to apply interrupt settings */ + if (Status == VL53L0_ERROR_NONE) + Status = VL53L0_CheckAndLoadInterruptSettings(Dev, 1); + Status = VL53L0_WrByte(Dev, VL53L0_REG_SYSRANGE_START, - VL53L0_REG_SYSRANGE_MODE_BACKTOBACK | StartStopByte); + VL53L0_REG_SYSRANGE_MODE_BACKTOBACK); if (Status == VL53L0_ERROR_NONE) { /* Set PAL State to Running */ PALDevDataSet(Dev, PalState, VL53L0_STATE_RUNNING); @@ -2119,9 +2340,14 @@ VL53L0_Error VL53L0_StartMeasurement(VL53L0_DEV Dev) break; case VL53L0_DEVICEMODE_CONTINUOUS_TIMED_RANGING: /* Continuous mode */ + /* Check if need to apply interrupt settings */ + if (Status == VL53L0_ERROR_NONE) + Status = VL53L0_CheckAndLoadInterruptSettings(Dev, 1); + Status = VL53L0_WrByte(Dev, VL53L0_REG_SYSRANGE_START, - VL53L0_REG_SYSRANGE_MODE_TIMED | StartStopByte); + VL53L0_REG_SYSRANGE_MODE_TIMED); + if (Status == VL53L0_ERROR_NONE) { /* Set PAL State to Running */ PALDevDataSet(Dev, PalState, VL53L0_STATE_RUNNING); @@ -2132,23 +2358,6 @@ VL53L0_Error VL53L0_StartMeasurement(VL53L0_DEV Dev) Status = VL53L0_ERROR_MODE_NOT_SUPPORTED; } - Byte = StartStopByte; - if (Status == VL53L0_ERROR_NONE) { - /* Wait until start bit has been cleared */ - LoopNb = 0; - do { - if (LoopNb > 0) - Status = VL53L0_RdByte(Dev, - VL53L0_REG_SYSRANGE_START, &Byte); - LoopNb = LoopNb + 1; - } while (((Byte & StartStopByte) == StartStopByte) - && (Status == VL53L0_ERROR_NONE) - && (LoopNb < VL53L0_DEFAULT_MAX_LOOP)); - - if (LoopNb >= VL53L0_DEFAULT_MAX_LOOP) - Status = VL53L0_ERROR_TIME_OUT; - - } LOG_FUNCTION_END(Status); return Status; @@ -2157,16 +2366,27 @@ VL53L0_Error VL53L0_StartMeasurement(VL53L0_DEV Dev) VL53L0_Error VL53L0_StopMeasurement(VL53L0_DEV Dev) { VL53L0_Error Status = VL53L0_ERROR_NONE; + LOG_FUNCTION_START(""); Status = VL53L0_WrByte(Dev, VL53L0_REG_SYSRANGE_START, VL53L0_REG_SYSRANGE_MODE_SINGLESHOT); + Status = VL53L0_WrByte(Dev, 0xFF, 0x01); + Status = VL53L0_WrByte(Dev, 0x00, 0x00); + Status = VL53L0_WrByte(Dev, 0x91, 0x00); + Status = VL53L0_WrByte(Dev, 0x00, 0x01); + Status = VL53L0_WrByte(Dev, 0xFF, 0x00); + if (Status == VL53L0_ERROR_NONE) { /* Set PAL State to Idle */ PALDevDataSet(Dev, PalState, VL53L0_STATE_IDLE); } + /* Check if need to apply interrupt settings */ + if (Status == VL53L0_ERROR_NONE) + Status = VL53L0_CheckAndLoadInterruptSettings(Dev, 0); + LOG_FUNCTION_END(Status); return Status; } @@ -2178,6 +2398,7 @@ VL53L0_Error VL53L0_GetMeasurementDataReady(VL53L0_DEV Dev, uint8_t SysRangeStatusRegister; uint8_t InterruptConfig; uint32_t InterruptMask; + LOG_FUNCTION_START(""); InterruptConfig = VL53L0_GETDEVICESPECIFICPARAMETER(Dev, @@ -2210,6 +2431,7 @@ VL53L0_Error VL53L0_WaitDeviceReadyForNewMeasurement(VL53L0_DEV Dev, uint32_t MaxLoop) { VL53L0_Error Status = VL53L0_ERROR_NOT_IMPLEMENTED; + LOG_FUNCTION_START(""); /* not implemented for VL53L0 */ @@ -2259,6 +2481,7 @@ VL53L0_Error VL53L0_GetRangingMeasurementData(VL53L0_DEV Dev, pRangingMeasurementData->MeasurementTimeUsec = 0; + SignalRate = VL53L0_FIXPOINT97TOFIXPOINT1616( VL53L0_MAKEUINT16(localBuffer[7], localBuffer[6])); /* peak_signal_count_rate_rtn_mcps */ @@ -2378,19 +2601,28 @@ VL53L0_Error VL53L0_GetMeasurementRefSignal(VL53L0_DEV Dev, FixPoint1616_t *pMeasurementRefSignal) { VL53L0_Error Status = VL53L0_ERROR_NONE; + uint8_t SignalRefClipLimitCheckEnable = 0; + LOG_FUNCTION_START(""); - *pMeasurementRefSignal = PALDevDataGet(Dev, LastSignalRefMcps); + Status = VL53L0_GetLimitCheckEnable(Dev, + VL53L0_CHECKENABLE_SIGNAL_REF_CLIP, + &SignalRefClipLimitCheckEnable); + if (SignalRefClipLimitCheckEnable != 0) + *pMeasurementRefSignal = PALDevDataGet(Dev, LastSignalRefMcps); + else + Status = VL53L0_ERROR_INVALID_COMMAND; LOG_FUNCTION_END(Status); - return Status; + return Status; } VL53L0_Error VL53L0_GetHistogramMeasurementData(VL53L0_DEV Dev, VL53L0_HistogramMeasurementData_t *pHistogramMeasurementData) { VL53L0_Error Status = VL53L0_ERROR_NOT_IMPLEMENTED; + LOG_FUNCTION_START(""); LOG_FUNCTION_END(Status); @@ -2405,7 +2637,8 @@ VL53L0_Error VL53L0_PerformSingleRangingMeasurement(VL53L0_DEV Dev, LOG_FUNCTION_START(""); /* This function will do a complete single ranging - * Here we fix the mode! */ + * Here we fix the mode! + */ Status = VL53L0_SetDeviceMode(Dev, VL53L0_DEVICEMODE_SINGLE_RANGING); if (Status == VL53L0_ERROR_NONE) @@ -2566,7 +2799,7 @@ VL53L0_Error VL53L0_SetGpioConfig(VL53L0_DEV Dev, uint8_t Pin, VL53L0_Error VL53L0_GetGpioConfig(VL53L0_DEV Dev, uint8_t Pin, VL53L0_DeviceModes *pDeviceMode, VL53L0_GpioFunctionality *pFunctionality, - VL53L0_InterruptPolarity *pPolarity) + VL53L0_InterruptPolarity * pPolarity) { VL53L0_Error Status = VL53L0_ERROR_NONE; VL53L0_GpioFunctionality GpioFunctionality; @@ -2640,6 +2873,7 @@ VL53L0_Error VL53L0_SetInterruptThresholds(VL53L0_DEV Dev, { VL53L0_Error Status = VL53L0_ERROR_NONE; uint16_t Threshold16; + LOG_FUNCTION_START(""); /* no dependency on DeviceMode for Ewok */ @@ -2664,6 +2898,7 @@ VL53L0_Error VL53L0_GetInterruptThresholds(VL53L0_DEV Dev, { VL53L0_Error Status = VL53L0_ERROR_NONE; uint16_t Threshold16; + LOG_FUNCTION_START(""); /* no dependency on DeviceMode for Ewok */ @@ -2684,22 +2919,65 @@ VL53L0_Error VL53L0_GetInterruptThresholds(VL53L0_DEV Dev, return Status; } +VL53L0_Error VL53L0_GetStopCompletedStatus(VL53L0_DEV Dev, + uint32_t *pStopStatus) +{ + VL53L0_Error Status = VL53L0_ERROR_NONE; + uint8_t Byte = 0; + + LOG_FUNCTION_START(""); + + Status = VL53L0_WrByte(Dev, 0xFF, 0x01); + + if (Status == VL53L0_ERROR_NONE) + Status = VL53L0_RdByte(Dev, 0x04, &Byte); + + if (Status == VL53L0_ERROR_NONE) + Status = VL53L0_WrByte(Dev, 0xFF, 0x0); + + *pStopStatus = Byte; + + if (Byte == 0) { + Status = VL53L0_WrByte(Dev, 0x80, 0x01); + Status = VL53L0_WrByte(Dev, 0xFF, 0x01); + Status = VL53L0_WrByte(Dev, 0x00, 0x00); + Status = VL53L0_WrByte(Dev, 0x91, + PALDevDataGet(Dev, StopVariable)); + Status = VL53L0_WrByte(Dev, 0x00, 0x01); + Status = VL53L0_WrByte(Dev, 0xFF, 0x00); + Status = VL53L0_WrByte(Dev, 0x80, 0x00); + } + + LOG_FUNCTION_END(Status); + return Status; +} + /* Group PAL Interrupt Functions */ VL53L0_Error VL53L0_ClearInterruptMask(VL53L0_DEV Dev, uint32_t InterruptMask) { VL53L0_Error Status = VL53L0_ERROR_NONE; uint8_t LoopCount; uint8_t Byte; + LOG_FUNCTION_START(""); /* clear bit 0 range interrupt, bit 1 error interrupt */ - Status = VL53L0_WrByte(Dev, VL53L0_REG_SYSTEM_INTERRUPT_CLEAR, 0x01); LoopCount = 0; do { - VL53L0_RdByte(Dev, VL53L0_REG_RESULT_INTERRUPT_STATUS, &Byte); + Status = VL53L0_WrByte(Dev, + VL53L0_REG_SYSTEM_INTERRUPT_CLEAR, 0x01); + Status |= VL53L0_WrByte(Dev, + VL53L0_REG_SYSTEM_INTERRUPT_CLEAR, 0x00); + Status |= VL53L0_RdByte(Dev, + VL53L0_REG_RESULT_INTERRUPT_STATUS, &Byte); LoopCount++; - } while (((Byte & 0x07) != 0x00) && (LoopCount < 8)); - Status = VL53L0_WrByte(Dev, VL53L0_REG_SYSTEM_INTERRUPT_CLEAR, 0x00); + } while (((Byte & 0x07) != 0x00) + && (LoopCount < 3) + && (Status == VL53L0_ERROR_NONE)); + + + if (LoopCount >= 3) + Status = VL53L0_ERROR_INTERRUPT_NOT_CLEARED; LOG_FUNCTION_END(Status); return Status; @@ -2710,6 +2988,7 @@ VL53L0_Error VL53L0_GetInterruptMaskStatus(VL53L0_DEV Dev, { VL53L0_Error Status = VL53L0_ERROR_NONE; uint8_t Byte; + LOG_FUNCTION_START(""); Status = VL53L0_RdByte(Dev, VL53L0_REG_RESULT_INTERRUPT_STATUS, &Byte); @@ -2725,6 +3004,7 @@ VL53L0_Error VL53L0_GetInterruptMaskStatus(VL53L0_DEV Dev, VL53L0_Error VL53L0_EnableInterruptMask(VL53L0_DEV Dev, uint32_t InterruptMask) { VL53L0_Error Status = VL53L0_ERROR_NOT_IMPLEMENTED; + LOG_FUNCTION_START(""); /* not implemented for VL53L0 */ @@ -2741,11 +3021,12 @@ VL53L0_Error VL53L0_SetSpadAmbientDamperThreshold(VL53L0_DEV Dev, uint16_t SpadAmbientDamperThreshold) { VL53L0_Error Status = VL53L0_ERROR_NONE; + LOG_FUNCTION_START(""); - VL53L0_WrByte(Dev, 0xFF, 0x01); - Status = VL53L0_WrWord(Dev, 0x40, SpadAmbientDamperThreshold); - VL53L0_WrByte(Dev, 0xFF, 0x00); + Status = VL53L0_WrByte(Dev, 0xFF, 0x01); + Status |= VL53L0_WrWord(Dev, 0x40, SpadAmbientDamperThreshold); + Status |= VL53L0_WrByte(Dev, 0xFF, 0x00); LOG_FUNCTION_END(Status); return Status; @@ -2755,11 +3036,12 @@ VL53L0_Error VL53L0_GetSpadAmbientDamperThreshold(VL53L0_DEV Dev, uint16_t *pSpadAmbientDamperThreshold) { VL53L0_Error Status = VL53L0_ERROR_NONE; + LOG_FUNCTION_START(""); - VL53L0_WrByte(Dev, 0xFF, 0x01); - Status = VL53L0_RdWord(Dev, 0x40, pSpadAmbientDamperThreshold); - VL53L0_WrByte(Dev, 0xFF, 0x00); + Status = VL53L0_WrByte(Dev, 0xFF, 0x01); + Status |= VL53L0_RdWord(Dev, 0x40, pSpadAmbientDamperThreshold); + Status |= VL53L0_WrByte(Dev, 0xFF, 0x00); LOG_FUNCTION_END(Status); return Status; @@ -2770,13 +3052,14 @@ VL53L0_Error VL53L0_SetSpadAmbientDamperFactor(VL53L0_DEV Dev, { VL53L0_Error Status = VL53L0_ERROR_NONE; uint8_t Byte; + LOG_FUNCTION_START(""); Byte = (uint8_t)(SpadAmbientDamperFactor & 0x00FF); - VL53L0_WrByte(Dev, 0xFF, 0x01); - Status = VL53L0_WrByte(Dev, 0x42, Byte); - VL53L0_WrByte(Dev, 0xFF, 0x00); + Status = VL53L0_WrByte(Dev, 0xFF, 0x01); + Status |= VL53L0_WrByte(Dev, 0x42, Byte); + Status |= VL53L0_WrByte(Dev, 0xFF, 0x00); LOG_FUNCTION_END(Status); return Status; @@ -2787,11 +3070,12 @@ VL53L0_Error VL53L0_GetSpadAmbientDamperFactor(VL53L0_DEV Dev, { VL53L0_Error Status = VL53L0_ERROR_NONE; uint8_t Byte; + LOG_FUNCTION_START(""); - VL53L0_WrByte(Dev, 0xFF, 0x01); - Status = VL53L0_RdByte(Dev, 0x42, &Byte); - VL53L0_WrByte(Dev, 0xFF, 0x00); + Status = VL53L0_WrByte(Dev, 0xFF, 0x01); + Status |= VL53L0_RdByte(Dev, 0x42, &Byte); + Status |= VL53L0_WrByte(Dev, 0xFF, 0x00); *pSpadAmbientDamperFactor = (uint16_t)Byte; LOG_FUNCTION_END(Status); @@ -2808,6 +3092,7 @@ VL53L0_Error VL53L0_SetReferenceSpads(VL53L0_DEV Dev, uint32_t count, uint8_t isApertureSpads) { VL53L0_Error Status = VL53L0_ERROR_NONE; + LOG_FUNCTION_START(""); Status = VL53L0_set_reference_spads(Dev, count, isApertureSpads); @@ -2821,6 +3106,7 @@ VL53L0_Error VL53L0_GetReferenceSpads(VL53L0_DEV Dev, uint32_t *pSpadCount, uint8_t *pIsApertureSpads) { VL53L0_Error Status = VL53L0_ERROR_NONE; + LOG_FUNCTION_START(""); Status = VL53L0_get_reference_spads(Dev, pSpadCount, pIsApertureSpads); @@ -2834,6 +3120,7 @@ VL53L0_Error VL53L0_PerformRefSpadManagement(VL53L0_DEV Dev, uint32_t *refSpadCount, uint8_t *isApertureSpads) { VL53L0_Error Status = VL53L0_ERROR_NONE; + LOG_FUNCTION_START(""); Status = VL53L0_perform_ref_spad_management(Dev, refSpadCount, diff --git a/drivers/input/misc/vl53L0/src/vl53l0_api_calibration.c b/drivers/input/misc/vl53L0/src/vl53l0_api_calibration.c index 596b07017ae0..57bf47999df4 100644 --- a/drivers/input/misc/vl53L0/src/vl53l0_api_calibration.c +++ b/drivers/input/misc/vl53L0/src/vl53l0_api_calibration.c @@ -1,5 +1,5 @@ /******************************************************************************* - Copyright © 2016, STMicroelectronics International N.V. + * Copyright © 2016, STMicroelectronics International N.V. All rights reserved. Redistribution and use in source and binary forms, with or without @@ -137,7 +137,8 @@ VL53L0_Error VL53L0_perform_xtalk_calibration(VL53L0_DEV Dev, /* Round Cal Distance to Whole Number. * Note that the cal distance is in mm, therefore no resolution - * is lost.*/ + * is lost. + */ xTalkCalDistanceAsInt = (XTalkCalDistance + 0x8000) >> 16; if (xTalkStoredMeanRtnSpadsAsInt == 0 || @@ -146,15 +147,17 @@ VL53L0_Error VL53L0_perform_xtalk_calibration(VL53L0_DEV Dev, XTalkCompensationRateMegaCps = 0; } else { /* Round Cal Distance to Whole Number. - Note that the cal distance is in mm, therefore no - resolution is lost.*/ + * Note that the cal distance is in mm, therefore no + * resolution is lost. + */ xTalkCalDistanceAsInt = (XTalkCalDistance + 0x8000) >> 16; /* Apply division by mean spad count early in the * calculation to keep the numbers small. * This ensures we can maintain a 32bit calculation. - * Fixed1616 / int := Fixed1616 */ + * Fixed1616 / int := Fixed1616 + */ signalXTalkTotalPerSpad = (xTalkStoredMeanSignalRate) / xTalkStoredMeanRtnSpadsAsInt; @@ -169,9 +172,6 @@ VL53L0_Error VL53L0_perform_xtalk_calibration(VL53L0_DEV Dev, /* Round from 2^16 * Fixed1616, to Fixed1616. */ XTalkCompensationRateMegaCps = (signalXTalkTotalPerSpad + 0x8000) >> 16; - - if (XTalkCompensationRateMegaCps < 0) - XTalkCompensationRateMegaCps = 0; } *pXTalkCompensationRateMegaCps = XTalkCompensationRateMegaCps; @@ -262,7 +262,8 @@ VL53L0_Error VL53L0_perform_offset_calibration(VL53L0_DEV Dev, /* Round Cal Distance to Whole Number. * Note that the cal distance is in mm, therefore no resolution - * is lost.*/ + * is lost. + */ CalDistanceAsInt_mm = (CalDistanceMilliMeter + 0x8000) >> 16; *pOffsetMicroMeter = (CalDistanceAsInt_mm - @@ -365,7 +366,8 @@ VL53L0_Error VL53L0_apply_offset_adjustment(VL53L0_DEV Dev) int32_t CurrentOffsetMicroMeters; /* if we run on this function we can read all the NVM info - * used by the API */ + * used by the API + */ Status = VL53L0_get_info_from_device(Dev, 7); /* Read back current device offset */ @@ -429,7 +431,8 @@ void get_next_good_spad(uint8_t goodSpadArray[], uint32_t size, if (coarseIndex == startIndex) { /* locate the bit position of the provided current - * spad bit before iterating */ + * spad bit before iterating + */ dataByte >>= fineOffset; fineIndex = fineOffset; } @@ -455,6 +458,7 @@ uint8_t is_aperture(uint32_t spadIndex) */ uint32_t quadrant; uint8_t isAperture = 1; + quadrant = spadIndex >> 6; if (refArrayQuadrants[quadrant] == REF_ARRAY_SPAD_0) isAperture = 0; @@ -518,7 +522,7 @@ VL53L0_Error count_enabled_spads(uint8_t spadArray[], if (!spadTypeIdentified) { *pIsAperture = 1; if ((byteIndex < 2) && (bitIndex < 4)) - *pIsAperture = 0; + *pIsAperture = 0; spadTypeIdentified = 1; } } @@ -734,9 +738,9 @@ VL53L0_Error VL53L0_perform_ref_spad_management(VL53L0_DEV Dev, * Note that there are 6 bytes. Only the first 44 bits will be used to * represent spads. */ - for (index = 0; index < spadArraySize; index++) { + for (index = 0; index < spadArraySize; index++) Dev->Data.SpadData.RefSpadEnables[index] = 0; - } + Status = VL53L0_WrByte(Dev, 0xFF, 0x01); @@ -790,7 +794,8 @@ VL53L0_Error VL53L0_perform_ref_spad_management(VL53L0_DEV Dev, if ((Status == VL53L0_ERROR_NONE) && (peakSignalRateRef > targetRefRate)) { /* Signal rate measurement too high, - * switch to APERTURE SPADs */ + * switch to APERTURE SPADs + */ for (index = 0; index < spadArraySize; index++) Dev->Data.SpadData.RefSpadEnables[index] = 0; @@ -823,10 +828,12 @@ VL53L0_Error VL53L0_perform_ref_spad_management(VL53L0_DEV Dev, (peakSignalRateRef > targetRefRate)) { /* Signal rate still too high after * setting the minimum number of - * APERTURE spads. Can do no more. + * APERTURE spads. Can do no more + * therefore set the min number of + * aperture spads as the result. */ - Status = VL53L0_ERROR_REF_SPAD_INIT; - needAptSpads = 0; + isApertureSpads_int = 1; + refSpadCount_int = minimumSpadCount; } } } else { @@ -861,17 +868,20 @@ VL53L0_Error VL53L0_perform_ref_spad_management(VL53L0_DEV Dev, break; } - (refSpadCount_int)++; - /* Cannot combine Aperture and Non-Aperture spads, so * ensure the current spad is of the correct type. */ if (is_aperture((uint32_t)startSelect + nextGoodSpad) != needAptSpads) { - Status = VL53L0_ERROR_REF_SPAD_INIT; + /* At this point we have enabled the maximum + * number of Aperture spads. + */ + complete = 1; break; } + (refSpadCount_int)++; + currentSpadIndex = nextGoodSpad; Status = enable_spad_bit( Dev->Data.SpadData.RefSpadEnables, @@ -880,7 +890,8 @@ VL53L0_Error VL53L0_perform_ref_spad_management(VL53L0_DEV Dev, if (Status == VL53L0_ERROR_NONE) { currentSpadIndex++; /* Proceed to apply the additional spad and - * perform measurement. */ + * perform measurement. + */ Status = set_ref_spad_map(Dev, Dev->Data.SpadData.RefSpadEnables); } @@ -903,7 +914,8 @@ VL53L0_Error VL53L0_perform_ref_spad_management(VL53L0_DEV Dev, */ if (signalRateDiff > lastSignalRateDiff) { /* Previous spad map produced a closer - * measurement, so choose this. */ + * measurement, so choose this. + */ Status = set_ref_spad_map(Dev, lastSpadArray); memcpy( @@ -974,9 +986,8 @@ VL53L0_Error VL53L0_set_reference_spads(VL53L0_DEV Dev, VL53L0_REG_GLOBAL_CONFIG_REF_EN_START_SELECT, startSelect); - for (index = 0; index < spadArraySize; index++) { + for (index = 0; index < spadArraySize; index++) Dev->Data.SpadData.RefSpadEnables[index] = 0; - } if (isApertureSpads) { /* Increment to the first APERTURE spad */ @@ -1022,7 +1033,7 @@ VL53L0_Error VL53L0_get_reference_spads(VL53L0_DEV Dev, if (refSpadsInitialised == 1) { - *pSpadCount = (uint32_t)VL53L0_GETDEVICESPECIFICPARAMETER(Dev, + *pSpadCount = (uint32_t)VL53L0_GETDEVICESPECIFICPARAMETER(Dev, ReferenceSpadCount); *pIsApertureSpads = VL53L0_GETDEVICESPECIFICPARAMETER(Dev, ReferenceSpadType); @@ -1222,7 +1233,8 @@ VL53L0_Error VL53L0_perform_ref_calibration(VL53L0_DEV Dev, SequenceConfig = PALDevDataGet(Dev, SequenceConfig); /* In the following function we don't save the config to optimize - * writes on device. Config is saved and restored only once. */ + * writes on device. Config is saved and restored only once. + */ Status = VL53L0_perform_vhv_calibration( Dev, pVhvSettings, get_data_enable, 0); diff --git a/drivers/input/misc/vl53L0/src/vl53l0_api_core.c b/drivers/input/misc/vl53L0/src/vl53l0_api_core.c index ca3ec3e33bc3..2ccaf975b30e 100644 --- a/drivers/input/misc/vl53L0/src/vl53l0_api_core.c +++ b/drivers/input/misc/vl53L0/src/vl53l0_api_core.c @@ -1,5 +1,5 @@ /******************************************************************************* - Copyright © 2016, STMicroelectronics International N.V. + * Copyright © 2016, STMicroelectronics International N.V. All rights reserved. Redistribution and use in source and binary forms, with or without @@ -30,10 +30,10 @@ #include "vl53l0_api_core.h" #include "vl53l0_api_calibration.h" + #ifndef __KERNEL__ #include #endif - #define LOG_FUNCTION_START(fmt, ...) \ _LOG_FUNCTION_START(TRACE_MODULE_API, fmt, ##__VA_ARGS__) #define LOG_FUNCTION_END(status, ...) \ @@ -78,7 +78,7 @@ VL53L0_Error VL53L0_measurement_poll_for_completion(VL53L0_DEV Dev) LoopNb++; if (LoopNb >= VL53L0_DEFAULT_MAX_LOOP) { - Status = VL53L0_ERROR_TIME_OUT; + Status = VL53L0_ERROR_TIME_OUT; break; } @@ -131,7 +131,8 @@ uint32_t VL53L0_isqrt(uint32_t num) uint32_t res = 0; uint32_t bit = 1 << 30; /* The second-to-top bit is set: - * 1 << 14 for 16-bits, 1 << 30 for 32 bits */ + * 1 << 14 for 16-bits, 1 << 30 for 32 bits + */ /* "bit" starts at the highest power of four <= the argument. */ while (bit > num) @@ -180,18 +181,20 @@ VL53L0_Error VL53L0_device_read_strobe(VL53L0_DEV Dev) VL53L0_Error Status = VL53L0_ERROR_NONE; uint8_t strobe; uint32_t LoopNb; + LOG_FUNCTION_START(""); Status |= VL53L0_WrByte(Dev, 0x83, 0x00); /* polling - * use timeout to avoid deadlock*/ + * use timeout to avoid deadlock + */ if (Status == VL53L0_ERROR_NONE) { LoopNb = 0; do { Status = VL53L0_RdByte(Dev, 0x83, &strobe); if ((strobe != 0x00) || Status != VL53L0_ERROR_NONE) - break; + break; LoopNb = LoopNb + 1; } while (LoopNb < VL53L0_DEFAULT_MAX_LOOP); @@ -239,7 +242,8 @@ VL53L0_Error VL53L0_get_info_from_device(VL53L0_DEV Dev, uint8_t option) ReadDataFromDeviceDone); /* This access is done only once after that a GetDeviceInfo or - * datainit is done*/ + * datainit is done + */ if (ReadDataFromDeviceDone != 7) { Status |= VL53L0_WrByte(Dev, 0x80, 0x01); @@ -415,7 +419,7 @@ VL53L0_Error VL53L0_get_info_from_device(VL53L0_DEV Dev, uint8_t option) VL53L0_SETDEVICESPECIFICPARAMETER(Dev, ReferenceSpadType, ReferenceSpadType); - for(i = 0; i < VL53L0_REF_SPAD_BUFFER_SIZE; i++){ + for (i = 0; i < VL53L0_REF_SPAD_BUFFER_SIZE; i++) { Dev->Data.SpadData.RefGoodSpadMap[i] = NvmRefGoodSpadMap[i]; } @@ -453,12 +457,12 @@ VL53L0_Error VL53L0_get_info_from_device(VL53L0_DEV Dev, uint8_t option) OffsetMicroMeters = 0; if (DistMeasFixed1104_400_mm != 0) { - OffsetFixed1104_mm = - DistMeasFixed1104_400_mm - - DistMeasTgtFixed1104_mm; - OffsetMicroMeters = (OffsetFixed1104_mm + OffsetFixed1104_mm = + DistMeasFixed1104_400_mm - + DistMeasTgtFixed1104_mm; + OffsetMicroMeters = (OffsetFixed1104_mm * 1000) >> 4; - OffsetMicroMeters *= -1; + OffsetMicroMeters *= -1; } PALDevDataSet(Dev, @@ -484,8 +488,8 @@ uint32_t VL53L0_calc_macro_period_ps(VL53L0_DEV Dev, uint8_t vcsel_period_pclks) LOG_FUNCTION_START(""); /* The above calculation will produce rounding errors, - therefore set fixed value - */ + * therefore set fixed value + */ PLL_period_ps = 1655; macro_period_vclks = 2304; @@ -569,8 +573,7 @@ uint32_t VL53L0_calc_timeout_us(VL53L0_DEV Dev, macro_period_ns = (macro_period_ps + 500) / 1000; actual_timeout_period_us = - ((timeout_period_mclks * macro_period_ns) - + (macro_period_ns / 2)) / 1000; + ((timeout_period_mclks * macro_period_ns) + 500) / 1000; return actual_timeout_period_us; } @@ -647,7 +650,8 @@ VL53L0_Error get_sequence_step_timeout(VL53L0_DEV Dev, &CurrentVCSELPulsePeriodPClk); /* Retrieve PRE-RANGE Timeout in Macro periods - * (MCLKS) */ + * (MCLKS) + */ if (Status == VL53L0_ERROR_NONE) { Status = VL53L0_RdWord(Dev, VL53L0_REG_PRE_RANGE_CONFIG_TIMEOUT_MACROP_HI, @@ -695,7 +699,7 @@ VL53L0_Error set_sequence_step_timeout(VL53L0_DEV Dev, uint16_t PreRangeEncodedTimeOut; uint16_t PreRangeTimeOutMClks; uint16_t MsrcRangeTimeOutMClks; - uint16_t FinalRangeTimeOutMClks; + uint32_t FinalRangeTimeOutMClks; uint16_t FinalRangeEncodedTimeOut; VL53L0_SchedulerSequenceSteps_t SchedulerSequenceSteps; @@ -779,7 +783,8 @@ VL53L0_Error set_sequence_step_timeout(VL53L0_DEV Dev, &CurrentVCSELPulsePeriodPClk); /* Retrieve PRE-RANGE Timeout in Macro periods - * (MCLKS) */ + * (MCLKS) + */ if (Status == VL53L0_ERROR_NONE) { Status = VL53L0_RdWord(Dev, 0x51, &PreRangeEncodedTimeOut); @@ -1070,7 +1075,8 @@ VL53L0_Error VL53L0_set_vcsel_pulse_period(VL53L0_DEV Dev, /* Perform the phase calibration. This is needed after changing on * vcsel period. - * get_data_enable = 0, restore_config = 1 */ + * get_data_enable = 0, restore_config = 1 + */ if (Status == VL53L0_ERROR_NONE) Status = VL53L0_perform_phase_calibration( Dev, &PhaseCalInt, 0, 1); @@ -1115,7 +1121,7 @@ VL53L0_Error VL53L0_set_measurement_timing_budget_micro_seconds(VL53L0_DEV Dev, uint32_t FinalRangeTimingBudgetMicroSeconds; VL53L0_SchedulerSequenceSteps_t SchedulerSequenceSteps; uint32_t MsrcDccTccTimeoutMicroSeconds = 2000; - uint32_t StartOverheadMicroSeconds = 1320; + uint32_t StartOverheadMicroSeconds = 1910; uint32_t EndOverheadMicroSeconds = 960; uint32_t MsrcOverheadMicroSeconds = 660; uint32_t TccOverheadMicroSeconds = 590; @@ -1123,17 +1129,10 @@ VL53L0_Error VL53L0_set_measurement_timing_budget_micro_seconds(VL53L0_DEV Dev, uint32_t PreRangeOverheadMicroSeconds = 660; uint32_t FinalRangeOverheadMicroSeconds = 550; uint32_t PreRangeTimeoutMicroSeconds = 0; - uint32_t cMinTimingBudgetMicroSeconds = 20000; uint32_t SubTimeout = 0; LOG_FUNCTION_START(""); - if (MeasurementTimingBudgetMicroSeconds - < cMinTimingBudgetMicroSeconds) { - Status = VL53L0_ERROR_INVALID_PARAMS; - return Status; - } - FinalRangeTimingBudgetMicroSeconds = MeasurementTimingBudgetMicroSeconds - (StartOverheadMicroSeconds + EndOverheadMicroSeconds); @@ -1151,7 +1150,8 @@ VL53L0_Error VL53L0_set_measurement_timing_budget_micro_seconds(VL53L0_DEV Dev, &MsrcDccTccTimeoutMicroSeconds); /* Subtract the TCC, MSRC and DSS timeouts if they are - * enabled. */ + * enabled. + */ if (Status != VL53L0_ERROR_NONE) return Status; @@ -1238,19 +1238,19 @@ VL53L0_Error VL53L0_set_measurement_timing_budget_micro_seconds(VL53L0_DEV Dev, FinalRangeOverheadMicroSeconds; /* Final Range Timeout - * Note that the final range timeout is determined by the timing - * budget and the sum of all other timeouts within the sequence. - * If there is no room for the final range timeout, then an error - * will be set. Otherwise the remaining time will be applied to - * the final range. - */ + * Note that the final range timeout is determined by the timing + * budget and the sum of all other timeouts within the sequence. + * If there is no room for the final range timeout, then an + * error will be set. Otherwise the remaining time will be + * applied to the final range. + */ Status = set_sequence_step_timeout(Dev, - VL53L0_SEQUENCESTEP_FINAL_RANGE, - FinalRangeTimingBudgetMicroSeconds); + VL53L0_SEQUENCESTEP_FINAL_RANGE, + FinalRangeTimingBudgetMicroSeconds); VL53L0_SETPARAMETERFIELD(Dev, - MeasurementTimingBudgetMicroSeconds, - MeasurementTimingBudgetMicroSeconds); + MeasurementTimingBudgetMicroSeconds, + MeasurementTimingBudgetMicroSeconds); } LOG_FUNCTION_END(Status); @@ -1431,14 +1431,6 @@ VL53L0_Error VL53L0_load_tuning_settings(VL53L0_DEV Dev, } } - /* Set Pulse Width Offset. This duplicates the previous setting - * applied in the tuning list, however is required because the - * register value appears to have not been set. - */ - Status |= VL53L0_WrByte(Dev, 0xff, 0x01); - Status |= VL53L0_WrByte(Dev, 0x50, 0x06); - Status |= VL53L0_WrByte(Dev, 0xff, 0x00); - LOG_FUNCTION_END(Status); return Status; } @@ -1456,9 +1448,9 @@ VL53L0_Error VL53L0_get_total_xtalk_rate(VL53L0_DEV Dev, *ptotal_xtalk_rate_mcps = 0; Status = VL53L0_GetXTalkCompensationEnable(Dev, &xtalkCompEnable); - if(Status == VL53L0_ERROR_NONE) { + if (Status == VL53L0_ERROR_NONE) { - if(xtalkCompEnable) { + if (xtalkCompEnable) { VL53L0_GETPARAMETERFIELD( Dev, @@ -1471,7 +1463,8 @@ VL53L0_Error VL53L0_get_total_xtalk_rate(VL53L0_DEV Dev, xtalkPerSpadMegaCps; /* FixPoint0824 >> 8 = FixPoint1616 */ - *ptotal_xtalk_rate_mcps = (totalXtalkMegaCps + 0x80) >> 8; + *ptotal_xtalk_rate_mcps = + (totalXtalkMegaCps + 0x80) >> 8; } } @@ -1493,199 +1486,112 @@ VL53L0_Error VL53L0_get_total_signal_rate(VL53L0_DEV Dev, Status = VL53L0_get_total_xtalk_rate( Dev, pRangingMeasurementData, &totalXtalkMegaCps); - if(Status == VL53L0_ERROR_NONE) + if (Status == VL53L0_ERROR_NONE) *ptotal_signal_rate_mcps += totalXtalkMegaCps; return Status; } -VL53L0_Error VL53L0_calc_dmax( - VL53L0_DEV Dev, - FixPoint1616_t totalSignalRate_mcps, - FixPoint1616_t totalCorrSignalRate_mcps, - FixPoint1616_t pwMult, - uint32_t sigmaEstimateP1, - FixPoint1616_t sigmaEstimateP2, - uint32_t peakVcselDuration_us, - uint32_t *pdmax_mm) -{ - const uint32_t cSigmaLimit = 18; - const FixPoint1616_t cSignalLimit = 0x4000; /* 0.25 */ - const FixPoint1616_t cSigmaEstRef = 0x00000042; /* 0.001 */ - const uint32_t cAmbEffWidthSigmaEst_ns = 6; - const uint32_t cAmbEffWidthDMax_ns = 7; - uint32_t dmaxCalRange_mm; - FixPoint1616_t dmaxCalSignalRateRtn_mcps; - FixPoint1616_t minSignalNeeded; - FixPoint1616_t minSignalNeeded_p1; - FixPoint1616_t minSignalNeeded_p2; - FixPoint1616_t minSignalNeeded_p3; - FixPoint1616_t minSignalNeeded_p4; - FixPoint1616_t sigmaLimitTmp; - FixPoint1616_t sigmaEstSqTmp; - FixPoint1616_t signalLimitTmp; - FixPoint1616_t SignalAt0mm; - FixPoint1616_t dmaxDark; - FixPoint1616_t dmaxAmbient; - FixPoint1616_t dmaxDarkTmp; - uint32_t signalRateTemp_mcps; - +VL53L0_Error get_dmax_lut_points(VL53L0_DMaxLUT_t data, uint32_t lut_size, + FixPoint1616_t input, int32_t *index0, int32_t *index1){ VL53L0_Error Status = VL53L0_ERROR_NONE; + FixPoint1616_t index0_tmp = 0; + FixPoint1616_t index1_tmp = 0; + int index = 0; + + for (index = 0; index < lut_size; index++) { + if (input <= data.ambRate_mcps[index]) { + index1_tmp = index; + break; + } + } + + if (index == lut_size) { + /* input is higher than last x point */ + index0_tmp = index1_tmp = lut_size - 1; + } else if (index1_tmp == 0) { + /* input is lower than first x point */ + index0_tmp = 0; + } else{ + /* input is in between 2 points */ + index0_tmp = index1_tmp - 1; + } + + *index0 = index0_tmp; + *index1 = index1_tmp; + + return Status; +} + +VL53L0_Error VL53L0_calc_dmax( + VL53L0_DEV Dev, FixPoint1616_t ambRateMeas, uint32_t *pdmax_mm){ + VL53L0_Error Status = VL53L0_ERROR_NONE; + VL53L0_DeviceParameters_t CurrentParameters; + int32_t index0 = 0; + int32_t index1 = 0; + FixPoint1616_t amb0, amb1, dmax0, dmax1; + FixPoint1616_t dmax_mm; + FixPoint1616_t linearSlope; LOG_FUNCTION_START(""); - dmaxCalRange_mm = - PALDevDataGet(Dev, DmaxCalRangeMilliMeter); + Status = VL53L0_GetDeviceParameters(Dev, &CurrentParameters); - dmaxCalSignalRateRtn_mcps = - PALDevDataGet(Dev, DmaxCalSignalRateRtnMegaCps); + if (ambRateMeas <= CurrentParameters.dmax_lut.ambRate_mcps[0]) { + dmax_mm = CurrentParameters.dmax_lut.dmax_mm[0]; + } else if (ambRateMeas >= + CurrentParameters.dmax_lut. + ambRate_mcps[VL53L0_DMAX_LUT_SIZE - 1]) { + dmax_mm = + CurrentParameters.dmax_lut.dmax_mm[VL53L0_DMAX_LUT_SIZE - + 1]; + } else{ + get_dmax_lut_points(CurrentParameters.dmax_lut, + VL53L0_DMAX_LUT_SIZE, ambRateMeas, &index0, &index1); - /* uint32 * FixPoint1616 = FixPoint1616 */ - SignalAt0mm = dmaxCalRange_mm * dmaxCalSignalRateRtn_mcps; + if (index0 == index1) { + dmax_mm = CurrentParameters.dmax_lut.dmax_mm[index0]; + } else { + amb0 = CurrentParameters.dmax_lut.ambRate_mcps[index0]; + amb1 = CurrentParameters.dmax_lut.ambRate_mcps[index1]; + dmax0 = CurrentParameters.dmax_lut.dmax_mm[index0]; + dmax1 = CurrentParameters.dmax_lut.dmax_mm[index1]; + if ((amb1 - amb0) != 0) { + /* Fix16:16/Fix16:8 => Fix16:8 */ + linearSlope = (dmax0-dmax1)/((amb1-amb0) >> 8); - /* FixPoint1616 >> 8 = FixPoint2408 */ - SignalAt0mm = (SignalAt0mm + 0x80) >> 8; - SignalAt0mm *= dmaxCalRange_mm; - - minSignalNeeded_p1 = 0; - if(totalCorrSignalRate_mcps > 0) { - - /* Shift by 10 bits to increase resolution prior to the - * division */ - signalRateTemp_mcps = totalSignalRate_mcps << 10; - - /* Add rounding value prior to division */ - minSignalNeeded_p1 = signalRateTemp_mcps + - (totalCorrSignalRate_mcps/2); - - /* FixPoint0626/FixPoint1616 = FixPoint2210 */ - minSignalNeeded_p1 /= totalCorrSignalRate_mcps; - - /* Apply a factored version of the speed of light. - Correction to be applied at the end */ - minSignalNeeded_p1 *= 3; - - /* FixPoint2210 * FixPoint2210 = FixPoint1220 */ - minSignalNeeded_p1 *= minSignalNeeded_p1; - - /* FixPoint1220 >> 16 = FixPoint2804 */ - minSignalNeeded_p1 = (minSignalNeeded_p1 + 0x8000) >> 16; + /* Fix16:8 * Fix16:8 => Fix16:16 */ + dmax_mm = + (((amb1 - + ambRateMeas) >> 8) * linearSlope) + + dmax1; + } else{ + dmax_mm = dmax0; + } + } } - - minSignalNeeded_p2 = pwMult * sigmaEstimateP1; - - /* FixPoint1616 >> 16 = uint32 */ - minSignalNeeded_p2 = (minSignalNeeded_p2 + 0x8000) >> 16; - - /* uint32 * uint32 = uint32 */ - minSignalNeeded_p2 *= minSignalNeeded_p2; - - /* DMAX uses a different ambient width from sigma, so apply - * correction. - * Perform division before multiplication to prevent overflow. - */ - sigmaEstimateP2 = (sigmaEstimateP2 + cAmbEffWidthSigmaEst_ns/2)/ - cAmbEffWidthSigmaEst_ns; - sigmaEstimateP2 *= cAmbEffWidthDMax_ns; - - /* FixPoint1616 >> 16 = uint32 */ - minSignalNeeded_p3 = (sigmaEstimateP2 + 0x8000) >> 16; - - minSignalNeeded_p3 *= minSignalNeeded_p3; - - /* FixPoint2012 / uint32 = FixPoint2012 */ - sigmaLimitTmp = ((cSigmaLimit << 12) + 500) / 1000; - - /* FixPoint2012 * FixPoint2012 = FixPoint4420 := FixPoint0824 */ - sigmaLimitTmp *= sigmaLimitTmp; - - /* FixPoint1616 * FixPoint1616 = FixPoint3232 */ - sigmaEstSqTmp = cSigmaEstRef * cSigmaEstRef; - - /* FixPoint3232 >> 8 = FixPoint0824 */ - sigmaEstSqTmp = (sigmaEstSqTmp + 0x80) >> 8; - - /* FixPoint0824 - FixPoint0824 = FixPoint0824 */ - sigmaLimitTmp -= sigmaEstSqTmp; - - /* uint32_t * FixPoint0824 = FixPoint0824 */ - minSignalNeeded_p4 = 4 * 12 * sigmaLimitTmp; - - /* FixPoint0824 >> 12 = FixPoint2012 */ - minSignalNeeded_p4 = (minSignalNeeded_p4 + 0x800) >> 12; - - /* uint32 + uint32 = uint32 */ - minSignalNeeded = (minSignalNeeded_p2 + minSignalNeeded_p3); - - /* uint32 / uint32 = uint32 */ - minSignalNeeded += (peakVcselDuration_us/2); - minSignalNeeded /= peakVcselDuration_us; - - /* uint32 << 12 = FixPoint2012 */ - minSignalNeeded <<= 12; - - /* FixPoint2012 / FixPoint2012 = uint32 */ - minSignalNeeded += (minSignalNeeded_p4/2); - minSignalNeeded /= minSignalNeeded_p4; - - /* FixPoint3200 * FixPoint2804 := FixPoint2804*/ - minSignalNeeded *= minSignalNeeded_p1; - - /* Apply correction by dividing by 1000000. - * This assumes 10E16 on the numerator of the equation - * and 10E-22 on the denominator. - * We do this because 32bit fix point calculation can't - * handle the larger and smaller elements of this equation, - * i.e. speed of light and pulse widths. - */ - minSignalNeeded = (minSignalNeeded + 500) / 1000; - minSignalNeeded <<= 4; - - minSignalNeeded = (minSignalNeeded + 500) / 1000; - - /* FixPoint1616 >> 8 = FixPoint2408 */ - signalLimitTmp = (cSignalLimit + 0x80) >> 8; - - /* FixPoint2408/FixPoint2408 = uint32 */ - if (signalLimitTmp != 0) - dmaxDarkTmp = (SignalAt0mm + (signalLimitTmp / 2)) - / signalLimitTmp; - else - dmaxDarkTmp = 0; - - dmaxDark = VL53L0_isqrt(dmaxDarkTmp); - - /* FixPoint2408/FixPoint2408 = uint32 */ - if (minSignalNeeded != 0) - dmaxAmbient = (SignalAt0mm + minSignalNeeded/2) - / minSignalNeeded; - else - dmaxAmbient = 0; - - dmaxAmbient = VL53L0_isqrt(dmaxAmbient); - - *pdmax_mm = dmaxDark; - if(dmaxDark > dmaxAmbient) - *pdmax_mm = dmaxAmbient; + *pdmax_mm = (uint32_t)(dmax_mm >> 16); LOG_FUNCTION_END(Status); return Status; } - VL53L0_Error VL53L0_calc_sigma_estimate(VL53L0_DEV Dev, VL53L0_RangingMeasurementData_t *pRangingMeasurementData, - FixPoint1616_t *pSigmaEstimate, - uint32_t *pDmax_mm) + FixPoint1616_t *pSigmaEstimate) { /* Expressed in 100ths of a ns, i.e. centi-ns */ const uint32_t cPulseEffectiveWidth_centi_ns = 800; /* Expressed in 100ths of a ns, i.e. centi-ns */ const uint32_t cAmbientEffectiveWidth_centi_ns = 600; - const FixPoint1616_t cSigmaEstRef = 0x00000042; /* 0.001 */ - const uint32_t cVcselPulseWidth_ps = 4700; /* pico secs */ - const FixPoint1616_t cSigmaEstMax = 0x028F87AE; + const FixPoint1616_t cDfltFinalRangeIntegrationTimeMilliSecs = + 0x00190000; /* 25ms */ + const uint32_t cVcselPulseWidth_ps = 4700; /* pico secs */ + const FixPoint1616_t cSigmaEstMax = 0x028F87AE; + const FixPoint1616_t cSigmaEstRtnMax = 0xF000; + const FixPoint1616_t cAmbToSignalRatioMax = 0xF0000000/ + cAmbientEffectiveWidth_centi_ns; /* Time Of Flight per mm (6.6 pico secs) */ const FixPoint1616_t cTOF_per_mm_ps = 0x0006999A; const uint32_t c16BitRoundingParam = 0x00008000; @@ -1695,6 +1601,7 @@ VL53L0_Error VL53L0_calc_sigma_estimate(VL53L0_DEV Dev, uint32_t vcselTotalEventsRtn; uint32_t finalRangeTimeoutMicroSecs; uint32_t preRangeTimeoutMicroSecs; + uint32_t finalRangeIntegrationTimeMilliSecs; FixPoint1616_t sigmaEstimateP1; FixPoint1616_t sigmaEstimateP2; FixPoint1616_t sigmaEstimateP3; @@ -1716,7 +1623,7 @@ VL53L0_Error VL53L0_calc_sigma_estimate(VL53L0_DEV Dev, FixPoint1616_t sqrtResult_centi_ns; FixPoint1616_t sqrtResult; FixPoint1616_t totalSignalRate_mcps; - FixPoint1616_t correctedSignalRate_mcps; + FixPoint1616_t sigmaEstRef; uint32_t vcselWidth; uint32_t finalRangeMacroPCLKS; uint32_t preRangeMacroPCLKS; @@ -1726,18 +1633,7 @@ VL53L0_Error VL53L0_calc_sigma_estimate(VL53L0_DEV Dev, /*! \addtogroup calc_sigma_estimate * @{ * - * Estimates the range sigma based on the - * - * - vcsel_rate_kcps - * - ambient_rate_kcps - * - signal_total_events - * - xtalk_rate - * - * and the following parameters - * - * - SigmaEstRefArray - * - SigmaEstEffPulseWidth - * - SigmaEstEffAmbWidth + * Estimates the range sigma */ LOG_FUNCTION_START(""); @@ -1753,10 +1649,6 @@ VL53L0_Error VL53L0_calc_sigma_estimate(VL53L0_DEV Dev, ambientRate_kcps = (pRangingMeasurementData->AmbientRateRtnMegaCps * 1000) >> 16; - correctedSignalRate_mcps = - pRangingMeasurementData->SignalRateRtnMegaCps; - - Status = VL53L0_get_total_signal_rate( Dev, pRangingMeasurementData, &totalSignalRate_mcps); Status = VL53L0_get_total_xtalk_rate( @@ -1767,14 +1659,14 @@ VL53L0_Error VL53L0_calc_sigma_estimate(VL53L0_DEV Dev, * peak signal rate, not average. */ peakSignalRate_kcps = (totalSignalRate_mcps * 1000); - peakSignalRate_kcps = (peakSignalRate_kcps + 0x8000) >> 16; + peakSignalRate_kcps = (peakSignalRate_kcps + 0x8000) >> 16; xTalkCompRate_kcps = xTalkCompRate_mcps * 1000; if (xTalkCompRate_kcps > cMaxXTalk_kcps) xTalkCompRate_kcps = cMaxXTalk_kcps; - if(Status == VL53L0_ERROR_NONE){ + if (Status == VL53L0_ERROR_NONE) { /* Calculate final range macro periods */ finalRangeTimeoutMicroSecs = VL53L0_GETDEVICESPECIFICPARAMETER( @@ -1797,9 +1689,9 @@ VL53L0_Error VL53L0_calc_sigma_estimate(VL53L0_DEV Dev, Dev, preRangeTimeoutMicroSecs, preRangeVcselPCLKS); vcselWidth = 3; - if(finalRangeVcselPCLKS == 8) { + if (finalRangeVcselPCLKS == 8) vcselWidth = 2; - } + peakVcselDuration_us = vcselWidth * 2048 * (preRangeMacroPCLKS + finalRangeMacroPCLKS); @@ -1807,9 +1699,18 @@ VL53L0_Error VL53L0_calc_sigma_estimate(VL53L0_DEV Dev, peakVcselDuration_us *= cPllPeriod_ps; peakVcselDuration_us = (peakVcselDuration_us + 500)/1000; + /* Fix1616 >> 8 = Fix2408 */ + totalSignalRate_mcps = (totalSignalRate_mcps + 0x80) >> 8; + + /* Fix2408 * uint32 = Fix2408 */ vcselTotalEventsRtn = totalSignalRate_mcps * peakVcselDuration_us; - vcselTotalEventsRtn = (vcselTotalEventsRtn + 0x8000) >> 16; + + /* Fix2408 >> 8 = uint32 */ + vcselTotalEventsRtn = (vcselTotalEventsRtn + 0x80) >> 8; + + /* Fix2408 << 8 = Fix1616 = */ + totalSignalRate_mcps <<= 8; } if (Status != VL53L0_ERROR_NONE) { @@ -1818,41 +1719,22 @@ VL53L0_Error VL53L0_calc_sigma_estimate(VL53L0_DEV Dev, } if (peakSignalRate_kcps == 0) { - *pSigmaEstimate = 0; - PALDevDataSet(Dev, SigmaEstimate, 0); - *pDmax_mm = 0; + *pSigmaEstimate = cSigmaEstMax; + PALDevDataSet(Dev, SigmaEstimate, cSigmaEstMax); } else { if (vcselTotalEventsRtn < 1) vcselTotalEventsRtn = 1; - /* - * Calculate individual components of the main equation - - * replicating the equation implemented in the script - * OpenAll_Ewok_ranging_data.jsl. - * - * sigmaEstimateP1 represents the effective pulse width, which - * is a tuning parameter, rather than a real value. - * - * sigmaEstimateP2 represents the ambient/signal rate ratio - * expressed as a multiple of the effective ambient width - * (tuning parameter). - * - * sigmaEstimateP3 provides the signal event component, with the - * knowledge that - * - Noise of a square pulse is 1/sqrt(12) of the pulse - * width. - * - at 0Lux, sigma is proportional to - * effectiveVcselPulseWidth/sqrt(12 * signalTotalEvents) - * - * deltaT_ps represents the time of flight in pico secs for the - * current range measurement, using the "TOF per mm" constant - * (in ps). - */ - sigmaEstimateP1 = cPulseEffectiveWidth_centi_ns; /* ((FixPoint1616 << 16)* uint32)/uint32 = FixPoint1616 */ sigmaEstimateP2 = (ambientRate_kcps << 16)/peakSignalRate_kcps; + if (sigmaEstimateP2 > cAmbToSignalRatioMax) { + /* Clip to prevent overflow. Will ensure safe + * max result. + */ + sigmaEstimateP2 = cAmbToSignalRatioMax; + } sigmaEstimateP2 *= cAmbientEffectiveWidth_centi_ns; sigmaEstimateP3 = 2 * VL53L0_isqrt(vcselTotalEventsRtn * 12); @@ -1869,75 +1751,81 @@ VL53L0_Error VL53L0_calc_sigma_estimate(VL53L0_DEV Dev, * truncates. */ diff1_mcps = (((peakSignalRate_kcps << 16) - - xTalkCompRate_kcps) + 500)/1000; + 2 * xTalkCompRate_kcps) + 500)/1000; /* vcselRate + xtalkCompRate */ - diff2_mcps = (((peakSignalRate_kcps << 16) + - xTalkCompRate_kcps) + 500)/1000; + diff2_mcps = ((peakSignalRate_kcps << 16) + 500)/1000; - /* Shift by 12 bits to increase resolution prior to the - * division */ - diff1_mcps <<= 12; + /* Shift by 8 bits to increase resolution prior to the + * division + */ + diff1_mcps <<= 8; - /* FixPoint0428/FixPoint1616 = FixPoint2012 */ + /* FixPoint0824/FixPoint1616 = FixPoint2408 */ xTalkCorrection = abs(diff1_mcps/diff2_mcps); - /* FixPoint2012 << 4 = FixPoint1616 */ - xTalkCorrection <<= 4; + /* FixPoint2408 << 8 = FixPoint1616 */ + xTalkCorrection <<= 8; - /* FixPoint1616/uint32 = FixPoint1616 */ - pwMult = deltaT_ps/cVcselPulseWidth_ps; /* smaller than 1.0f */ + if (pRangingMeasurementData->RangeStatus != 0) { + pwMult = 1 << 16; + } else { + /* FixPoint1616/uint32 = FixPoint1616 */ + /* smaller than 1.0f */ + pwMult = deltaT_ps/cVcselPulseWidth_ps; - /* - * FixPoint1616 * FixPoint1616 = FixPoint3232, however both - * values are small enough such that32 bits will not be - * exceeded. - */ - pwMult *= ((1 << 16) - xTalkCorrection); + /* + * FixPoint1616 * FixPoint1616 = FixPoint3232, however + * both values are small enough such that32 bits will + * not be exceeded. + */ + pwMult *= ((1 << 16) - xTalkCorrection); - /* (FixPoint3232 >> 16) = FixPoint1616 */ - pwMult = (pwMult + c16BitRoundingParam) >> 16; + /* (FixPoint3232 >> 16) = FixPoint1616 */ + pwMult = (pwMult + c16BitRoundingParam) >> 16; - /* FixPoint1616 + FixPoint1616 = FixPoint1616 */ - pwMult += (1 << 16); + /* FixPoint1616 + FixPoint1616 = FixPoint1616 */ + pwMult += (1 << 16); - /* - * At this point the value will be 1.xx, therefore if we square - * the value this will exceed 32 bits. To address this perform - * a single shift to the right before the multiplication. - */ - pwMult >>= 1; - /* FixPoint1715 * FixPoint1715 = FixPoint3430 */ - pwMult = pwMult * pwMult; + /* + * At this point the value will be 1.xx, therefore if we + * square the value this will exceed 32 bits. To address + * this perform a single shift to the right before the + * multiplication. + */ + pwMult >>= 1; + /* FixPoint1715 * FixPoint1715 = FixPoint3430 */ + pwMult = pwMult * pwMult; - /* (FixPoint3430 >> 14) = Fix1616 */ - pwMult >>= 14; + /* (FixPoint3430 >> 14) = Fix1616 */ + pwMult >>= 14; + } /* FixPoint1616 * uint32 = FixPoint1616 */ sqr1 = pwMult * sigmaEstimateP1; - /* (FixPoint1616 >> 12) = FixPoint2804 */ - sqr1 = (sqr1 + 0x800) >> 12; + /* (FixPoint1616 >> 16) = FixPoint3200 */ + sqr1 = (sqr1 + 0x8000) >> 16; - /* FixPoint2804 * FixPoint2804 = FixPoint5608 */ + /* FixPoint3200 * FixPoint3200 = FixPoint6400 */ sqr1 *= sqr1; sqr2 = sigmaEstimateP2; - /* (FixPoint1616 >> 12) = FixPoint2804 */ - sqr2 = (sqr2 + 0x800) >> 12; + /* (FixPoint1616 >> 16) = FixPoint3200 */ + sqr2 = (sqr2 + 0x8000) >> 16; - /* FixPoint2804 * FixPoint2804 = FixPoint5608 */ + /* FixPoint3200 * FixPoint3200 = FixPoint6400 */ sqr2 *= sqr2; - /* FixPoint5608 + FixPoint5608 = FixPoint5608 */ + /* FixPoint64000 + FixPoint6400 = FixPoint6400 */ sqrSum = sqr1 + sqr2; - /* SQRT(FixPoint5608) = FixPoint2804 */ + /* SQRT(FixPoin6400) = FixPoint3200 */ sqrtResult_centi_ns = VL53L0_isqrt(sqrSum); - /* (FixPoint2804 << 12) = FixPoint1616 */ - sqrtResult_centi_ns <<= 12; + /* (FixPoint3200 << 16) = FixPoint1616 */ + sqrtResult_centi_ns <<= 16; /* * Note that the Speed Of Light is expressed in um per 1E-10 @@ -1952,17 +1840,39 @@ VL53L0_Error VL53L0_calc_sigma_estimate(VL53L0_DEV Dev, sigmaEstRtn += 5000; sigmaEstRtn /= 10000; + if (sigmaEstRtn > cSigmaEstRtnMax) { + /* Clip to prevent overflow. Will ensure safe + * max result. + */ + sigmaEstRtn = cSigmaEstRtnMax; + } + finalRangeIntegrationTimeMilliSecs = + (finalRangeTimeoutMicroSecs + preRangeTimeoutMicroSecs + + 500) / 1000; + + /* sigmaEstRef = 1mm * 25ms/final range integration time + * (inc pre-range) + * sqrt(FixPoint1616/int) = FixPoint2408) + */ + sigmaEstRef = + VL53L0_isqrt((cDfltFinalRangeIntegrationTimeMilliSecs + + finalRangeIntegrationTimeMilliSecs/2)/ + finalRangeIntegrationTimeMilliSecs); + + /* FixPoint2408 << 8 = FixPoint1616 */ + sigmaEstRef <<= 8; + sigmaEstRef = (sigmaEstRef + 500)/1000; + /* FixPoint1616 * FixPoint1616 = FixPoint3232 */ sqr1 = sigmaEstRtn * sigmaEstRtn; /* FixPoint1616 * FixPoint1616 = FixPoint3232 */ - sqr2 = cSigmaEstRef * cSigmaEstRef; + sqr2 = sigmaEstRef * sigmaEstRef; - /* sqrt(FixPoint3232 << 8) = sqrt(FixPoint4024) =FixPoint2012 */ - sqrtResult = VL53L0_isqrt((sqr1 + sqr2) << 8); - sqrtResult = (sqrtResult + 0x08) >> 4; + /* sqrt(FixPoint3232) = FixPoint1616 */ + sqrtResult = VL53L0_isqrt((sqr1 + sqr2)); /* - * Note that the Shift by 12bits increases resolution prior to - * the sqrt, therefore the result must be shifted by 6bits to + * Note that the Shift by 4 bits increases resolution prior to + * the sqrt, therefore the result must be shifted by 2 bits to * the right to revert back to the FixPoint1616 format. */ @@ -1970,21 +1880,11 @@ VL53L0_Error VL53L0_calc_sigma_estimate(VL53L0_DEV Dev, if ((peakSignalRate_kcps < 1) || (vcselTotalEventsRtn < 1) || (sigmaEstimate > cSigmaEstMax)) { - sigmaEstimate = cSigmaEstMax; + sigmaEstimate = cSigmaEstMax; } *pSigmaEstimate = (uint32_t)(sigmaEstimate); PALDevDataSet(Dev, SigmaEstimate, *pSigmaEstimate); - - Status = VL53L0_calc_dmax( - Dev, - totalSignalRate_mcps, - correctedSignalRate_mcps, - pwMult, - sigmaEstimateP1, - sigmaEstimateP2, - peakVcselDuration_us, - pDmax_mm); } LOG_FUNCTION_END(Status); @@ -2044,22 +1944,6 @@ VL53L0_Error VL53L0_get_pal_range_status(VL53L0_DEV Dev, NoneFlag = 0; } - /* LastSignalRefMcps */ - if (Status == VL53L0_ERROR_NONE) - Status = VL53L0_WrByte(Dev, 0xFF, 0x01); - - if (Status == VL53L0_ERROR_NONE) - Status = VL53L0_RdWord(Dev, - VL53L0_REG_RESULT_PEAK_SIGNAL_RATE_REF, - &tmpWord); - - LastSignalRefMcps = VL53L0_FIXPOINT97TOFIXPOINT1616(tmpWord); - - if (Status == VL53L0_ERROR_NONE) - Status = VL53L0_WrByte(Dev, 0xFF, 0x00); - - PALDevDataSet(Dev, LastSignalRefMcps, LastSignalRefMcps); - /* * Check if Sigma limit is enabled, if yes then do comparison with limit * value and put the result back into pPalRangeStatus. @@ -2068,17 +1952,6 @@ VL53L0_Error VL53L0_get_pal_range_status(VL53L0_DEV Dev, Status = VL53L0_GetLimitCheckEnable(Dev, VL53L0_CHECKENABLE_SIGMA_FINAL_RANGE, &SigmaLimitCheckEnable); - /* - * compute the Sigma and check with limit - */ - Status = VL53L0_calc_sigma_estimate( - Dev, - pRangingMeasurementData, - &SigmaEstimate, - &Dmax_mm); - pRangingMeasurementData->SigmaEstimate = SigmaEstimate; - if (Status == VL53L0_ERROR_NONE) - pRangingMeasurementData->RangeDMaxMilliMeter = Dmax_mm; if ((SigmaLimitCheckEnable != 0) && (Status == VL53L0_ERROR_NONE)) { /* @@ -2087,9 +1960,12 @@ VL53L0_Error VL53L0_get_pal_range_status(VL53L0_DEV Dev, Status = VL53L0_calc_sigma_estimate( Dev, pRangingMeasurementData, - &SigmaEstimate, - &Dmax_mm); - + &SigmaEstimate); + if (Status == VL53L0_ERROR_NONE) + Status = VL53L0_calc_dmax( + Dev, + pRangingMeasurementData->AmbientRateRtnMegaCps, + &Dmax_mm); if (Status == VL53L0_ERROR_NONE) pRangingMeasurementData->RangeDMaxMilliMeter = Dmax_mm; @@ -2101,7 +1977,7 @@ VL53L0_Error VL53L0_get_pal_range_status(VL53L0_DEV Dev, if ((SigmaLimitValue > 0) && (SigmaEstimate > SigmaLimitValue)) /* Limit Fail */ - SigmaLimitflag = 1; + SigmaLimitflag = 1; } } @@ -2121,6 +1997,21 @@ VL53L0_Error VL53L0_get_pal_range_status(VL53L0_DEV Dev, VL53L0_CHECKENABLE_SIGNAL_REF_CLIP, &SignalRefClipValue); + /* Read LastSignalRefMcps from device */ + if (Status == VL53L0_ERROR_NONE) + Status = VL53L0_WrByte(Dev, 0xFF, 0x01); + + if (Status == VL53L0_ERROR_NONE) + Status = VL53L0_RdWord(Dev, + VL53L0_REG_RESULT_PEAK_SIGNAL_RATE_REF, + &tmpWord); + + if (Status == VL53L0_ERROR_NONE) + Status = VL53L0_WrByte(Dev, 0xFF, 0x00); + + LastSignalRefMcps = VL53L0_FIXPOINT97TOFIXPOINT1616(tmpWord); + PALDevDataSet(Dev, LastSignalRefMcps, LastSignalRefMcps); + if ((SignalRefClipValue > 0) && (LastSignalRefMcps > SignalRefClipValue)) { /* Limit Fail */ diff --git a/drivers/input/misc/vl53L0/src/vl53l0_api_histogram.c b/drivers/input/misc/vl53L0/src/vl53l0_api_histogram.c index ef543bcf6bde..21b0410dd6ae 100644 --- a/drivers/input/misc/vl53L0/src/vl53l0_api_histogram.c +++ b/drivers/input/misc/vl53L0/src/vl53l0_api_histogram.c @@ -1,5 +1,5 @@ /******************************************************************************* - Copyright © 2016, STMicroelectronics International N.V. + * Copyright © 2016, STMicroelectronics International N.V. All rights reserved. Redistribution and use in source and binary forms, with or without @@ -55,6 +55,7 @@ VL53L0_Error VL53L0_start_histogram_measurement(VL53L0_DEV Dev, { VL53L0_Error Status = VL53L0_ERROR_NONE; uint8_t dataByte; + LOG_FUNCTION_START(""); @@ -104,7 +105,7 @@ VL53L0_Error VL53L0_confirm_measurement_start(VL53L0_DEV Dev) do { Status = VL53L0_GetMeasurementDataReady(Dev, &NewDataReady); if ((NewDataReady == 0x01) || Status != VL53L0_ERROR_NONE) - break; + break; LoopNb = LoopNb + 1; VL53L0_PollingDelay(Dev); @@ -271,6 +272,7 @@ VL53L0_Error VL53L0_get_histogram_measurement_data(VL53L0_DEV Dev, VL53L0_HistogramMeasurementData_t *pHistogramMeasurementData) { VL53L0_Error Status = VL53L0_ERROR_NOT_IMPLEMENTED; + LOG_FUNCTION_START(""); LOG_FUNCTION_END(Status); @@ -402,13 +404,14 @@ VL53L0_Error VL53L0_get_max_spads(VL53L0_DEV Dev, if (Status == VL53L0_ERROR_NONE) { if (max_spads <= 0) { *pambient_too_high = 1; - return VL53L0_ERROR_DIVISION_BY_ZERO; + Status = VL53L0_ERROR_DIVISION_BY_ZERO; } else { ratio = RangingMeasurementData.AmbientRateRtnMegaCps / max_spads; /* ratio is given in mega count per second and - * FixPoint1616_t */ + * FixPoint1616_t + */ if (ratio > 65536/100) *pambient_too_high = 1; else @@ -519,6 +522,7 @@ uint32_t bytes_to_int(uint8_t *data_bytes) /* Convert an array of 4 bytes to an integer. */ uint32_t data = (uint32_t)data_bytes[0] << 24; + data += ((uint32_t)data_bytes[1] << 16); data += ((uint32_t)data_bytes[2] << 8); data += ((uint32_t)data_bytes[3]); diff --git a/drivers/input/misc/vl53L0/src/vl53l0_api_ranging.c b/drivers/input/misc/vl53L0/src/vl53l0_api_ranging.c index d1b065b00fe9..f575aec7c5ed 100644 --- a/drivers/input/misc/vl53L0/src/vl53l0_api_ranging.c +++ b/drivers/input/misc/vl53L0/src/vl53l0_api_ranging.c @@ -1,5 +1,5 @@ /******************************************************************************* - Copyright © 2016, STMicroelectronics International N.V. + * Copyright © 2016, STMicroelectronics International N.V. All rights reserved. Redistribution and use in source and binary forms, with or without diff --git a/drivers/input/misc/vl53L0/src/vl53l0_api_strings.c b/drivers/input/misc/vl53L0/src/vl53l0_api_strings.c index eacdd9ae4959..a5f2bbadd290 100644 --- a/drivers/input/misc/vl53L0/src/vl53l0_api_strings.c +++ b/drivers/input/misc/vl53L0/src/vl53l0_api_strings.c @@ -1,5 +1,5 @@ /******************************************************************************* - Copyright © 2016, STMicroelectronics International N.V. + * Copyright © 2016, STMicroelectronics International N.V. All rights reserved. Redistribution and use in source and binary forms, with or without @@ -273,6 +273,10 @@ VL53L0_Error VL53L0_get_pal_error_string(VL53L0_Error PalErrorCode, VL53L0_COPYSTRING(pPalErrorString, VL53L0_STRING_ERROR_NOT_SUPPORTED); break; + case VL53L0_ERROR_INTERRUPT_NOT_CLEARED: + VL53L0_COPYSTRING(pPalErrorString, + VL53L0_STRING_ERROR_INTERRUPT_NOT_CLEARED); + break; case VL53L0_ERROR_RANGE_ERROR: VL53L0_COPYSTRING(pPalErrorString, VL53L0_STRING_ERROR_RANGE_ERROR); @@ -378,6 +382,7 @@ VL53L0_Error VL53L0_get_sequence_steps_info( char *pSequenceStepsString) { VL53L0_Error Status = VL53L0_ERROR_NONE; + LOG_FUNCTION_START(""); switch (SequenceStepId) { @@ -437,6 +442,16 @@ VL53L0_Error VL53L0_get_limit_check_info(VL53L0_DEV Dev, uint16_t LimitCheckId, VL53L0_STRING_CHECKENABLE_RANGE_IGNORE_THRESHOLD); break; + case VL53L0_CHECKENABLE_SIGNAL_RATE_MSRC: + VL53L0_COPYSTRING(pLimitCheckString, + VL53L0_STRING_CHECKENABLE_SIGNAL_RATE_MSRC); + break; + + case VL53L0_CHECKENABLE_SIGNAL_RATE_PRE_RANGE: + VL53L0_COPYSTRING(pLimitCheckString, + VL53L0_STRING_CHECKENABLE_SIGNAL_RATE_PRE_RANGE); + break; + default: VL53L0_COPYSTRING(pLimitCheckString, VL53L0_STRING_UNKNOW_ERROR_CODE); diff --git a/drivers/input/misc/vl53L0/src/vl53l0_i2c_platform.c b/drivers/input/misc/vl53L0/src/vl53l0_i2c_platform.c index a86287b9a6d2..1b5e4d4ab0ce 100644 --- a/drivers/input/misc/vl53L0/src/vl53l0_i2c_platform.c +++ b/drivers/input/misc/vl53L0/src/vl53l0_i2c_platform.c @@ -22,8 +22,6 @@ #include #include #include "stmvl53l0-i2c.h" -#include "stmvl53l0-cci.h" - #include "vl53l0_platform.h" #include "vl53l0_i2c_platform.h" #include "vl53l0_def.h" @@ -75,15 +73,18 @@ #define VL53L0_GetLocalBuffer(Dev, n_byte) LocBuffer #elif I2C_BUFFER_CONFIG == 2 /* user define buffer type declare DECL_I2C_BUFFER as access via - VL53L0_GetLocalBuffer */ + * VL53L0_GetLocalBuffer + */ #define DECL_I2C_BUFFER #else #error "invalid I2C_BUFFER_CONFIG " #endif -#define VL53L0_I2C_USER_VAR /* none but could be for a flag var to - get/pass to mutex interruptible return flags and try again */ +/* none but could be for a flag var to + * get/pass to mutex interruptible return flags and try again + */ +#define VL53L0_I2C_USER_VAR #define VL53L0_GetI2CAccess(Dev) /* todo mutex acquire */ #define VL53L0_DoneI2CAcces(Dev) /* todo mutex release */ diff --git a/drivers/input/misc/vl53L0/src/vl53l0_platform.c b/drivers/input/misc/vl53L0/src/vl53l0_platform.c index e971b4813e6d..f7292ab6f8f2 100644 --- a/drivers/input/misc/vl53L0/src/vl53l0_platform.c +++ b/drivers/input/misc/vl53L0/src/vl53l0_platform.c @@ -1,5 +1,5 @@ /******************************************************************************* -Copyright © 2016, STMicroelectronics International N.V. + * Copyright © 2016, STMicroelectronics International N.V. All rights reserved. Redistribution and use in source and binary forms, with or without @@ -236,7 +236,7 @@ VL53L0_Error VL53L0_PollingDelay(VL53L0_DEV Dev) VL53L0_Error status = VL53L0_ERROR_NONE; LOG_FUNCTION_START(""); - udelay(1000); + usleep_range(950, 1000); LOG_FUNCTION_END(status); return status; } diff --git a/drivers/input/misc/vl53L0/src/vl53l0_port_i2c.c b/drivers/input/misc/vl53L0/src/vl53l0_port_i2c.c index 1346de0cbd18..b204c776d803 100644 --- a/drivers/input/misc/vl53L0/src/vl53l0_port_i2c.c +++ b/drivers/input/misc/vl53L0/src/vl53l0_port_i2c.c @@ -8,7 +8,6 @@ #include #include #include "stmvl53l0-i2c.h" -#include "stmvl53l0-cci.h" #include "vl53l0_platform.h" #include "vl53l0_i2c_platform.h" #include "stmvl53l0.h" @@ -25,58 +24,16 @@ */ int VL53L0_I2CWrite(VL53L0_DEV dev, uint8_t *buff, uint8_t len) { - - int err = 0; + if (dev->bus_type == CCI_BUS) { -#ifdef CAMERA_CCI - uint16_t index; - struct cci_data *cci_client_obj = - (struct cci_data *)dev->client_object; - struct msm_camera_i2c_client *client = cci_client_obj->client; - index = buff[0]; - /*pr_err("%s: index: %d len: %d\n", __func__, index, len); */ - - if (len == 2) { - uint8_t data; - - data = buff[1]; - /* for byte access */ - err = client->i2c_func_tbl->i2c_write(client, index, - data, MSM_CAMERA_I2C_BYTE_DATA); - if (err < 0) { - pr_err("%s:%d failed status=%d\n", - __func__, __LINE__, err); - return err; - } - } else if (len == 3) { - uint16_t data; - - data = ((uint16_t)buff[1] << 8) | (uint16_t)buff[2]; - err = client->i2c_func_tbl->i2c_write(client, index, - data, MSM_CAMERA_I2C_WORD_DATA); - if (err < 0) { - pr_err("%s:%d failed status=%d\n", - __func__, __LINE__, err); - return err; - } - } else if (len >= 5) { - err = client->i2c_func_tbl->i2c_write_seq(client, - index, &buff[1], (len-1)); - if (err < 0) { - pr_err("%s:%d failed status=%d\n", - __func__, __LINE__, err); - return err; - } - - } -#endif } else { struct i2c_msg msg[1]; struct i2c_data *i2c_client_obj = - (struct i2c_data *)dev->client_object; - struct i2c_client *client = (struct i2c_client*)i2c_client_obj->client; + (struct i2c_data *)dev->client_object; + struct i2c_client *client = + (struct i2c_client *)i2c_client_obj->client; msg[0].addr = client->addr; msg[0].flags = I2C_M_WR; @@ -110,27 +67,12 @@ int VL53L0_I2CRead(VL53L0_DEV dev, uint8_t *buff, uint8_t len) int err = 0; if (dev->bus_type == CCI_BUS) { -#ifdef CAMERA_CCI - uint16_t index; - struct cci_data *cci_client_obj = - (struct cci_data *)dev->client_object; - struct msm_camera_i2c_client *client = cci_client_obj->client; - - index = buff[0]; - /* pr_err("%s: index: %d\n", __func__, index); */ - err = client->i2c_func_tbl->i2c_read_seq(client, - index, buff, len); - if (err < 0) { - pr_err("%s:%d failed status=%d\n", - __func__, __LINE__, err); - return err; - } -#endif } else { struct i2c_msg msg[1]; struct i2c_data *i2c_client_obj = - (struct i2c_data *)dev->client_object; - struct i2c_client *client = (struct i2c_client*) i2c_client_obj->client; + (struct i2c_data *)dev->client_object; + struct i2c_client *client = + (struct i2c_client *) i2c_client_obj->client; msg[0].addr = client->addr; msg[0].flags = I2C_M_RD|client->flags; @@ -138,7 +80,6 @@ int VL53L0_I2CRead(VL53L0_DEV dev, uint8_t *buff, uint8_t len) msg[0].len = len; err = i2c_transfer(client->adapter, &msg[0], 1); - /* return the actual mesage transfer */ if (err != 1) { pr_err("%s: Read i2c_transfer err:%d, addr:0x%x\n", __func__, err, client->addr); diff --git a/drivers/input/misc/vl53L0/src/vl53l0_tuning.c b/drivers/input/misc/vl53L0/src/vl53l0_tuning.c deleted file mode 100644 index 2efca96a7713..000000000000 --- a/drivers/input/misc/vl53L0/src/vl53l0_tuning.c +++ /dev/null @@ -1,137 +0,0 @@ -/******************************************************************************* - Copyright © 2015, STMicroelectronics International N.V. - All rights reserved. - - Redistribution and use in source and binary forms, with or without - modification, are permitted provided that the following conditions are met: - * Redistributions of source code must retain the above copyright - notice, this list of conditions and the following disclaimer. - * Redistributions in binary form must reproduce the above copyright - notice, this list of conditions and the following disclaimer in the - documentation and/or other materials provided with the distribution. - * Neither the name of STMicroelectronics nor the - names of its contributors may be used to endorse or promote products - derived from this software without specific prior written permission. - - THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND - ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED - WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, AND - NON-INFRINGEMENT OF INTELLECTUAL PROPERTY RIGHTS ARE DISCLAIMED. - IN NO EVENT SHALL STMICROELECTRONICS INTERNATIONAL N.V. BE LIABLE FOR ANY - DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES - (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; - LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND - ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT - (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS - SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - ******************************************************************************/ - - -#define LOG_FUNCTION_START(fmt, ...) \ - _LOG_FUNCTION_START(TRACE_MODULE_API, fmt, ##__VA_ARGS__) -#define LOG_FUNCTION_END(status, ...) \ - _LOG_FUNCTION_END(TRACE_MODULE_API, status, ##__VA_ARGS__) -#define LOG_FUNCTION_END_FMT(status, fmt, ...) \ - _LOG_FUNCTION_END_FMT(TRACE_MODULE_API, status, fmt, ##__VA_ARGS__) - -#ifdef VL53L0_LOG_ENABLE -#define trace_print(level, ...) \ - trace_print_module_function(TRACE_MODULE_API,\ - level, TRACE_FUNCTION_NONE, ##__VA_ARGS__) -#endif - -/* -////////////////////////////////////////////////////// -//// DEFAULT TUNING SETTINGS //// -////////////////////////////////////////////////////// -*/ -/*VL53L0_Error VL53L0_load_tuning_settings(VL53L0_DEV Dev) -{ - VL53L0_Error Status = VL53L0_ERROR_NONE; - - LOG_FUNCTION_START(""); - - Status |= VL53L0_WrByte(Dev, 0xFF, 0x01); - Status |= VL53L0_WrByte(Dev, 0x00, 0x00); - Status |= VL53L0_WrByte(Dev, 0x91, 0x3C); - Status |= VL53L0_WrByte(Dev, 0xFF, 0x00); - Status |= VL53L0_WrByte(Dev, 0x54, 0x01); - Status |= VL53L0_WrByte(Dev, 0x33, 0x05); - Status |= VL53L0_WrByte(Dev, 0x32, 0x03); - Status |= VL53L0_WrByte(Dev, 0x30, 0x05); - Status |= VL53L0_WrByte(Dev, 0x50, 0x05); - Status |= VL53L0_WrByte(Dev, 0x60, 0x04); - Status |= VL53L0_WrByte(Dev, 0x70, 0x06); - - Status |= VL53L0_WrByte(Dev, 0x46, 0x1a); - Status |= VL53L0_WrWord(Dev, 0x51, 0x01a3); - Status |= VL53L0_WrWord(Dev, 0x61, 0x01c4); - Status |= VL53L0_WrWord(Dev, 0x71, 0x018c); - - Status |= VL53L0_WrByte(Dev, 0xFF, 0x01); - Status |= VL53L0_WrByte(Dev, 0x31, 0x0f); - Status |= VL53L0_WrByte(Dev, 0xFF, 0x00); - Status |= VL53L0_WrByte(Dev, 0x66, 0x38); - - Status |= VL53L0_WrByte(Dev, 0x47, 0x00); - Status |= VL53L0_WrByte(Dev, 0x48, 0xff); - Status |= VL53L0_WrByte(Dev, 0x57, 0x4c); - Status |= VL53L0_WrByte(Dev, 0x67, 0x3c); - Status |= VL53L0_WrByte(Dev, 0x77, 0x5c); - - Status |= VL53L0_WrWord(Dev, 0x44, 0x0000); - - Status |= VL53L0_WrByte(Dev, 0x27, 0x00); - Status |= VL53L0_WrByte(Dev, 0x55, 0x00); - - Status |= VL53L0_WrByte(Dev, 0xFF, 0x01); - Status |= VL53L0_WrByte(Dev, 0x30, 0x28); - Status |= VL53L0_WrByte(Dev, 0xFF, 0x00); - - Status |= VL53L0_WrByte(Dev, 0x10, 0x0f); - Status |= VL53L0_WrByte(Dev, 0x11, 0xff); - Status |= VL53L0_WrByte(Dev, 0x41, 0xff); - Status |= VL53L0_WrByte(Dev, 0x42, 0x07); - Status |= VL53L0_WrByte(Dev, 0x43, 0x12); - - Status |= VL53L0_WrByte(Dev, 0x20, 0x00); - Status |= VL53L0_WrByte(Dev, 0x21, 0x00); - - Status |= VL53L0_WrByte(Dev, 0x28, 0x06); - Status |= VL53L0_WrByte(Dev, 0xFF, 0x01); - Status |= VL53L0_WrByte(Dev, 0x48, 0x28); - Status |= VL53L0_WrByte(Dev, 0xFF, 0x00); - - Status |= VL53L0_WrByte(Dev, 0x7a, 0x0a); - Status |= VL53L0_WrByte(Dev, 0x7b, 0x00); - Status |= VL53L0_WrByte(Dev, 0x78, 0x00); - Status |= VL53L0_WrByte(Dev, 0xFF, 0x01); - Status |= VL53L0_WrByte(Dev, 0x44, 0xff); - Status |= VL53L0_WrByte(Dev, 0x45, 0x00); - Status |= VL53L0_WrByte(Dev, 0x46, 0x10); - Status |= VL53L0_WrByte(Dev, 0xFF, 0x00); - - Status |= VL53L0_WrByte(Dev, 0x04, 0x00); - Status |= VL53L0_WrByte(Dev, 0x05, 0x04); - Status |= VL53L0_WrByte(Dev, 0x06, 0x00); - Status |= VL53L0_WrByte(Dev, 0x07, 0x00); - - Status |= VL53L0_WrByte(Dev, 0xFF, 0x01); - Status |= VL53L0_WrByte(Dev, 0x0d, 0x01); - Status |= VL53L0_WrByte(Dev, 0xFF, 0x00); - Status |= VL53L0_WrByte(Dev, 0x80, 0x01); - Status |= VL53L0_WrByte(Dev, 0x01, 0xF8); - - Status |= VL53L0_WrByte(Dev, 0xFF, 0x01); - Status |= VL53L0_WrByte(Dev, 0x8e, 0x01); - Status |= VL53L0_WrByte(Dev, 0x00, 0x01); - Status |= VL53L0_WrByte(Dev, 0xFF, 0x00); - - - if (Status != 0) - Status = VL53L0_ERROR_CONTROL_INTERFACE; - - LOG_FUNCTION_END(Status); - return Status; -} -*/ diff --git a/drivers/input/misc/vl53L0/stmvl53l0-cci.h b/drivers/input/misc/vl53L0/stmvl53l0-cci.h deleted file mode 100644 index d8c3b778a04d..000000000000 --- a/drivers/input/misc/vl53L0/stmvl53l0-cci.h +++ /dev/null @@ -1,65 +0,0 @@ -/* - * stmvl53l0-cci.h - Linux kernel modules for STM VL53L0 FlightSense TOF sensor - * - * Copyright (C) 2015 STMicroelectronics Imaging Division - * - * This program is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 2 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program; if not, write to the Free Software - * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. - */ -/* - * Defines - */ -#ifndef STMVL53L0_CCI_H -#define STMVL53L0_CCI_H -#include - -#include -#include "msm_camera_i2c.h" -#include "msm_camera_dt_util.h" -#include "msm_camera_io_util.h" -#include "msm_cci.h" - -#define MSM_TOF_MAX_VREGS (10) - -struct msm_tof_vreg { - struct camera_vreg_t *cam_vreg; - void *data[MSM_TOF_MAX_VREGS]; - int num_vreg; -}; - -struct cci_data { - struct msm_camera_i2c_client g_client; - struct msm_camera_i2c_client *client; - struct platform_device *pdev; - enum msm_camera_device_type_t device_type; - enum cci_i2c_master_t cci_master; - struct msm_tof_vreg vreg_cfg; - struct msm_sd_subdev msm_sd; - struct v4l2_subdev sdev; - struct v4l2_subdev_ops *subdev_ops; - char subdev_initialized; - uint32_t subdev_id; - uint8_t power_up; - uint32_t lowv; - uint32_t highv; - uint32_t xtalk; - struct msm_camera_gpio_conf gconf; - struct msm_pinctrl_info pinctrl_info; -}; -int stmvl53l0_init_cci(void); -void stmvl53l0_exit_cci(void *); -int stmvl53l0_power_down_cci(void *); -int stmvl53l0_power_up_cci(void *, unsigned int *); - -#endif /* STMVL53L0_CCI_H */ diff --git a/drivers/input/misc/vl53L0/stmvl53l0-i2c.h b/drivers/input/misc/vl53L0/stmvl53l0-i2c.h index a75a9e58da97..26968cba2e81 100644 --- a/drivers/input/misc/vl53L0/stmvl53l0-i2c.h +++ b/drivers/input/misc/vl53L0/stmvl53l0-i2c.h @@ -1,7 +1,7 @@ /* * stmvl53l0-i2c.h - Linux kernel modules for STM VL53L0 FlightSense TOF sensor * - * Copyright (C) 2015 STMicroelectronics Imaging Division + * Copyright (C) 2016 STMicroelectronics Imaging Division * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -12,10 +12,6 @@ * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program; if not, write to the Free Software - * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */ /* * Defines @@ -24,17 +20,11 @@ #define STMVL53L0_I2C_H #include #include -#include "msm_camera_i2c.h" -#include "msm_camera_dt_util.h" -#include "msm_camera_io_util.h" -#include "msm_cci.h" struct i2c_data { struct i2c_client *client; struct regulator *vana; uint8_t power_up; - uint32_t lowv; - uint32_t highv; uint32_t xtalk; struct msm_camera_gpio_conf gconf; struct msm_pinctrl_info pinctrl_info; @@ -43,5 +33,4 @@ int stmvl53l0_init_i2c(void); void stmvl53l0_exit_i2c(void *); int stmvl53l0_power_up_i2c(void *, unsigned int *); int stmvl53l0_power_down_i2c(void *); - #endif /* STMVL53L0_I2C_H */ diff --git a/drivers/input/misc/vl53L0/stmvl53l0.h b/drivers/input/misc/vl53L0/stmvl53l0.h index 4ae84c9e8e83..54ccff920a6c 100644 --- a/drivers/input/misc/vl53L0/stmvl53l0.h +++ b/drivers/input/misc/vl53L0/stmvl53l0.h @@ -1,7 +1,7 @@ /* * stmvl53l0.h - Linux kernel modules for STM VL53L0 FlightSense TOF sensor * - * Copyright (C) 2015 STMicroelectronics Imaging Division + * Copyright (C) 2016 STMicroelectronics Imaging Division * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -12,10 +12,6 @@ * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program; if not, write to the Free Software - * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */ /* * Defines @@ -27,70 +23,59 @@ #include #include #include +#include #define STMVL53L0_DRV_NAME "stmvl53l0" #define STMVL53L0_SLAVE_ADDR (0x52>>1) -#define DRIVER_VERSION "1.1.18" -#define DRIVER_VERSION_NUM 18 +#define DRIVER_VERSION "1.1.23" #define I2C_M_WR 0x00 /* #define INT_POLLING_DELAY 20 */ -/* if don't want to have output from vl6180_dbgmsg, comment out #DEBUG macro */ -/*#define DEBUG*/ -/* #define vl6180_dbgmsg(str, args...) pr_debug("%s: " str, __func__, ##args) */ -#define vl53l0_dbgmsg_en(str, args...) \ +/* if don't want to have output from vl53l0_dbgmsg, comment out #DEBUG macro */ +//#define DEBUG +#ifdef DEBUG +#define vl53l0_dbgmsg(str, args...) \ + pr_err("%s: " str, __func__, ##args) +#else +#define vl53l0_dbgmsg(str, args...) \ pr_debug("%s: " str, __func__, ##args) - -#define vl53l0_dbgmsg(str, args...) \ - pr_debug("%s: " str, __func__, ##args) -/* #define vl6180_errmsg(str, args...) pr_err("%s: " str, __func__, ##args) */ +#endif #define vl53l0_errmsg(str, args...) \ - printk(KERN_ERR "%s: " str, __func__, ##args) + pr_err("%s: " str, __func__, ##args) -#define VL53L0_VDD_MIN 2850000 -#define VL53L0_VDD_MAX 2850000 -/*driver working mode*/ -#define OFF_MODE 0 -#define CAM_MODE 1 -#define SAR_MODE 2 -#define SUPER_MODE 3 -#define XTALKCAL_MODE 4 -#define OFFSETCAL_MODE 5 +#define VL53L0_VDD_MIN 2600000 +#define VL53L0_VDD_MAX 3000000 -/*user actions*/ -#define CAM_ON 0 -#define CAM_OFF 1 -#define SAR_ON 2 -#define SAR_OFF 3 -#define XTALKCAL_ON 4 -#define OFFSETCAL_ON 5 -#define CAL_OFF 6 -#define RESET 7 -/*parameter types*/ -#define OFFSET_PAR 0 -#define XTALKRATE_PAR 1 -#define XTALKENABLE_PAR 2 -#define SIGMAVAL_PRA 3 -#define SIGMACTL_PRA 4 -#define WRAPAROUNDCTL_PRA 5 -#define INTERMEASUREMENTPERIOD_PAR 6 -#define MEASUREMENTTIMINGBUDGET_PAR 7 -#define SGLVAL_PRA 8 -#define SGLCTL_PRA 9 -#define CUTV_PRA 10 -#define DRVV_PRA 11 -#define PLLPRE_PRA 12 -#define PLLFINAL_PRA 13 -#define CCI_BUS 0 -#define I2C_BUS 1 +typedef enum { + NORMAL_MODE = 0, + OFFSETCALIB_MODE = 1, + XTALKCALIB_MODE = 2, +} init_mode_e; +typedef enum { + OFFSET_PAR = 0, + XTALKRATE_PAR = 1, + XTALKENABLE_PAR = 2, + GPIOFUNC_PAR = 3, + LOWTHRESH_PAR = 4, + HIGHTHRESH_PAR = 5, + DEVICEMODE_PAR = 6, + INTERMEASUREMENT_PAR = 7, + REFERENCESPADS_PAR = 8, + REFCALIBRATION_PAR = 9, +} parameter_name_e; + +enum { + CCI_BUS = 0, + I2C_BUS = 1, +}; /* * IOCTL register data structs */ struct stmvl53l0_register { - uint32_t is_read; /*1: read 0: #define write*/ + uint32_t is_read; /*1: read 0: write*/ uint32_t reg_index; uint32_t reg_bytes; uint32_t reg_data; @@ -102,34 +87,48 @@ struct stmvl53l0_register { */ struct stmvl53l0_parameter { uint32_t is_read; /*1: Get 0: Set*/ - uint32_t name; + parameter_name_e name; int32_t value; + int32_t value2; int32_t status; }; +/* + * IOCTL Custom Use Case + */ +struct stmvl53l0_custom_use_case { + FixPoint1616_t signalRateLimit; + FixPoint1616_t sigmaLimit; + uint32_t preRangePulsePeriod; + uint32_t finalRangePulsePeriod; + uint32_t timingBudget; +}; + /* * driver data structs */ struct stmvl53l0_data { - /* embed ST VL53L0 Dev data as "dev_data" */ - VL53L0_DevData_t Data; - /* i2c device address user specific field */ - uint8_t I2cDevAddr; - uint8_t comms_type; - uint16_t comms_speed_khz; - struct cci_data cci_client_object; - struct i2c_data i2c_client_object; - void *client_object; + /* ! -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -/* - * power specific includes - */ -#include -#include -#include -#include -#include -/* - * API includes - */ -#include "vl53l0_api.h" -#include "vl53l0_def.h" -#include "vl53l0_platform.h" -#include "stmvl53l0-cci.h" -#include "stmvl53l0-i2c.h" -#include "stmvl53l0.h" -#define LASER_SENSOR_PINCTRL_STATE_SLEEP "laser_suspend" -#define LASER_SENSOR_PINCTRL_STATE_DEFAULT "laser_default" -#ifdef CAMERA_CCI -/* - * Global data - */ -static struct v4l2_file_operations msm_tof_v4l2_subdev_fops; -static struct msm_camera_i2c_fn_t msm_sensor_cci_func_tbl = { - .i2c_read = msm_camera_cci_i2c_read, - .i2c_read_seq = msm_camera_cci_i2c_read_seq, - .i2c_write = msm_camera_cci_i2c_write, - .i2c_write_seq = msm_camera_cci_i2c_write_seq, - .i2c_write_table = msm_camera_cci_i2c_write_table, - .i2c_write_seq_table = msm_camera_cci_i2c_write_seq_table, - .i2c_write_table_w_microdelay = - msm_camera_cci_i2c_write_table_w_microdelay, - .i2c_util = msm_sensor_cci_i2c_util, - .i2c_poll = msm_camera_cci_i2c_poll, -}; - -int vl53l0_get_dt_xtalk_data(struct device_node *of_node, int *xtalk) -{ - int rc = 0; - uint32_t count = 0; - uint32_t v_array[1]; - - count = of_property_count_strings(of_node, "st,xtalkval"); - - if (!count) - return 0; - - rc = of_property_read_u32_array(of_node, "st,xtalkval", - v_array, 1); - - if (rc != -EINVAL) { - if (rc < 0) - pr_err("%s failed %d\n", __func__, __LINE__); - else - *xtalk = v_array[0]; - } else - rc = 0; - - return rc; -} -static int stmvl53l0_get_dt_data(struct device *dev, struct cci_data *data); - -/* - * QCOM specific functions - */ -static int stmvl53l0_get_dt_data(struct device *dev, struct cci_data *data) -{ - int rc = 0; - struct msm_camera_gpio_conf *gconf = NULL; - uint16_t *gpio_array = NULL; - uint16_t gpio_array_size = 0; - int i; - struct msm_pinctrl_info *sensor_pctrl = NULL; - struct msm_tof_vreg *vreg_cfg = NULL; - - vl53l0_dbgmsg("Enter\n"); - - sensor_pctrl = &data->pinctrl_info; - sensor_pctrl->pinctrl = devm_pinctrl_get(dev); - if (IS_ERR_OR_NULL(sensor_pctrl->pinctrl)) { - pr_err("%s:%d Getting pinctrl handle failed\n", - __func__, __LINE__); - return -EINVAL; - } - sensor_pctrl->gpio_state_active = - pinctrl_lookup_state(sensor_pctrl->pinctrl, - LASER_SENSOR_PINCTRL_STATE_DEFAULT); - - sensor_pctrl->gpio_state_suspend - = pinctrl_lookup_state(sensor_pctrl->pinctrl, - LASER_SENSOR_PINCTRL_STATE_SLEEP); - - if (dev->of_node) { - struct device_node *of_node = dev->of_node; - - if (!of_node) { - vl53l0_errmsg("failed %d\n", __LINE__); - return -EINVAL; - } - - rc = of_property_read_u32(of_node, - "cell-index", &data->pdev->id); - if (rc < 0) { - vl53l0_errmsg("failed %d\n", __LINE__); - return rc; - } - vl53l0_dbgmsg("cell-index: %d\n", data->pdev->id); - rc = of_property_read_u32(of_node, - "qcom,cci-master", &data->cci_master); - if (rc < 0) { - vl53l0_errmsg("failed %d\n", __LINE__); - /* Set default master 0 */ - data->cci_master = MASTER_0; - rc = 0; - } - vl53l0_dbgmsg("cci_master: %d\n", data->cci_master); - - if (of_find_property(of_node, - "qcom,cam-vreg-name", NULL)) { - vl53l0_dbgmsg("vreg setting is found"); - vreg_cfg = &data->vreg_cfg; - rc = msm_camera_get_dt_vreg_data(of_node, - &vreg_cfg->cam_vreg, &vreg_cfg->num_vreg); - if (rc < 0) { - vl53l0_errmsg("failed %d\n", __LINE__); - rc = 0; - } - } - vl53l0_dbgmsg("vreg num: %d\n", vreg_cfg->num_vreg); - - gpio_array_size = of_gpio_count(of_node); - gconf = &data->gconf; - - if (gpio_array_size) { - gpio_array = kcalloc(gpio_array_size, sizeof(uint16_t), - GFP_KERNEL); - - for (i = 0; i < gpio_array_size; i++) { - gpio_array[i] = of_get_gpio(of_node, i); - pr_err("%s gpio_array[%d] = %d\n", __func__, i, - gpio_array[i]); - } - - rc = msm_camera_get_dt_gpio_req_tbl(of_node, gconf, - gpio_array, gpio_array_size); - if (rc < 0) { - pr_err("%s failed %d\n", __func__, __LINE__); - return rc; - } - - rc = msm_camera_init_gpio_pin_tbl(of_node, gconf, - gpio_array, gpio_array_size); - if (rc < 0) { - pr_err("%s failed %d\n", __func__, __LINE__); - return rc; - } - } - rc = vl53l0_get_dt_xtalk_data(of_node, - &(data->xtalk)); - } - vl53l0_dbgmsg("End rc =%d\n", rc); - - return rc; -} - -static int32_t stmvl53l0_vreg_control(struct cci_data *data, int config) -{ - int rc = 0, i, cnt; - struct msm_tof_vreg *vreg_cfg; - - vl53l0_dbgmsg("Enter with config %d\n", config); - - vreg_cfg = &data->vreg_cfg; - cnt = vreg_cfg->num_vreg; - if (!cnt) { - vl53l0_errmsg("failed %d\n", __LINE__); - return 0; - } - - if (cnt >= MSM_TOF_MAX_VREGS) { - vl53l0_errmsg("failed %d cnt %d\n", __LINE__, cnt); - return -EINVAL; - } - - for (i = 0; i < cnt; i++) { - rc = msm_camera_config_single_vreg(&(data->pdev->dev), - &vreg_cfg->cam_vreg[i], - (struct regulator **)&vreg_cfg->data[i], - config); - } - - vl53l0_dbgmsg("EXIT rc =%d\n", rc); - return rc; -} - -static int msm_tof_close(struct v4l2_subdev *sd, struct v4l2_subdev_fh *fh) -{ - int rc = 0; - return rc; -} - - -static const struct v4l2_subdev_internal_ops msm_tof_internal_ops = { - .close = msm_tof_close, -}; - -static long msm_tof_subdev_ioctl(struct v4l2_subdev *sd, - unsigned int cmd, void *arg) -{ - vl53l0_dbgmsg("Subdev_ioctl not handled\n"); - return 0; -} - -static int32_t msm_tof_power(struct v4l2_subdev *sd, int on) -{ - vl53l0_dbgmsg("TOF power called\n"); - return 0; -} - -static struct v4l2_subdev_core_ops msm_tof_subdev_core_ops = { - .ioctl = msm_tof_subdev_ioctl, - .s_power = msm_tof_power, -}; - -static struct v4l2_subdev_ops msm_tof_subdev_ops = { - .core = &msm_tof_subdev_core_ops, -}; - -static int stmvl53l0_cci_init(struct cci_data *data) -{ - int rc = 0; - struct msm_camera_cci_client *cci_client = data->client->cci_client; - - if (FALSE == data->subdev_initialized) { - data->client->i2c_func_tbl = &msm_sensor_cci_func_tbl; - data->client->cci_client = - kzalloc(sizeof(struct msm_camera_cci_client), - GFP_KERNEL); - - if (!data->client->cci_client) { - vl53l0_errmsg("%d, failed no memory\n", __LINE__); - return -ENOMEM; - } - cci_client = data->client->cci_client; - cci_client->cci_subdev = msm_cci_get_subdev(); - cci_client->cci_i2c_master = data->cci_master; - v4l2_subdev_init(&data->msm_sd.sd, data->subdev_ops); - v4l2_set_subdevdata(&data->msm_sd.sd, data); - data->msm_sd.sd.internal_ops = &msm_tof_internal_ops; - data->msm_sd.sd.flags |= V4L2_SUBDEV_FL_HAS_DEVNODE; - snprintf(data->msm_sd.sd.name, - ARRAY_SIZE(data->msm_sd.sd.name), "msm_tof"); - media_entity_init(&data->msm_sd.sd.entity, 0, NULL, 0); - data->msm_sd.sd.entity.type = MEDIA_ENT_T_V4L2_SUBDEV; - data->msm_sd.sd.entity.group_id = MSM_CAMERA_SUBDEV_TOF; - data->msm_sd.close_seq = MSM_SD_CLOSE_2ND_CATEGORY | 0x2; - msm_sd_register(&data->msm_sd); - msm_tof_v4l2_subdev_fops = v4l2_subdev_fops; - data->msm_sd.sd.devnode->fops = &msm_tof_v4l2_subdev_fops; - data->subdev_initialized = TRUE; - } - - vl53l0_dbgmsg("inited\n"); - cci_client->sid = 0x29; - cci_client->retries = 3; - cci_client->id_map = 0; - cci_client->cci_i2c_master = data->cci_master; - cci_client->i2c_freq_mode = I2C_FAST_MODE; - rc = data->client->i2c_func_tbl->i2c_util(data->client, MSM_CCI_INIT); - if (rc < 0) { - vl53l0_errmsg("%d: CCI Init failed\n", __LINE__); - return rc; - } - vl53l0_dbgmsg("CCI Init Succeeded\n"); - data->client->addr_type = MSM_CAMERA_I2C_BYTE_ADDR; - - return 0; -} - -static int32_t stmvl53l0_platform_probe(struct platform_device *pdev) -{ - struct stmvl53l0_data *vl53l0_data = NULL; - struct cci_data *data = NULL; - unsigned int present = 0; - - int32_t rc = 0; - - vl53l0_dbgmsg("Enter\n"); - - if (!pdev->dev.of_node) { - vl53l0_errmsg("%d,of_node NULL\n", __LINE__); - return -EINVAL; - } - - vl53l0_data = stmvl53l0_getobject(); - if (NULL == vl53l0_data) { - vl53l0_errmsg("%d,Object not found!\n", __LINE__); - return -EINVAL; - } - data = &(vl53l0_data->cci_client_object); - if (!data) { - vl53l0_errmsg("%d,data NULL\n", __LINE__); - return -EINVAL; - } - - /* Set platform device handle */ - data->subdev_ops = &msm_tof_subdev_ops; - data->pdev = pdev; - stmvl53l0_get_dt_data(&pdev->dev, data); - data->subdev_id = pdev->id; - - /* Set device type as platform device */ - data->device_type = MSM_CAMERA_PLATFORM_DEVICE; - data->subdev_initialized = FALSE; - - data->client = (struct msm_camera_i2c_client *)&data->g_client; - - rc = stmvl53l0_power_up_cci(data, &present); - if (rc) { - vl53l0_errmsg("%d,error rc %d\n", __LINE__, rc); - return rc; - } - rc = stmvl53l0_checkmoduleid(vl53l0_data, data->client, CCI_BUS); - if (rc != 0) { - vl53l0_errmsg("%d,error rc %d\n", __LINE__, rc); - stmvl53l0_power_down_cci(data); - return rc; - } - stmvl53l0_power_down_cci(data); - stmvl53l0_setup(vl53l0_data, CCI_BUS); - - vl53l0_dbgmsg("End\n"); - - return rc; - -} - - -static const struct of_device_id st_stmvl53l0_dt_match[] = { - { .compatible = "st,stmvl53l0", }, - { }, -}; - -static struct platform_driver stmvl53l0_platform_driver = { - .probe = stmvl53l0_platform_probe, - .driver = { - .name = STMVL53L0_DRV_NAME, - .owner = THIS_MODULE, - .of_match_table = st_stmvl53l0_dt_match, - }, -}; - -int stmvl53l0_power_up_cci(void *cci_object, unsigned int *preset_flag) -{ - int ret = 0; - struct cci_data *data = (struct cci_data *)cci_object; - - vl53l0_dbgmsg("Enter"); - pinctrl_select_state(data->pinctrl_info.pinctrl, - data->pinctrl_info.gpio_state_active); - - stmvl53l0_vreg_control(data, 1); - msleep(20); - - /* need to init cci first */ - ret = stmvl53l0_cci_init(data); - if (ret) { - vl53l0_errmsg("stmvl53l0_cci_init failed %d\n", __LINE__); - return ret; - } - - msm_camera_request_gpio_table( - data->gconf.cam_gpio_req_tbl, - data->gconf.cam_gpio_req_tbl_size, 1); - gpio_set_value_cansleep(data->gconf.cam_gpio_req_tbl[0].gpio, 1); - data->power_up = 1; - *preset_flag = 1; - vl53l0_dbgmsg("End\n"); - msleep(20); - return ret; -} - -int stmvl53l0_power_down_cci(void *cci_object) -{ - int ret = 0; - struct cci_data *data = (struct cci_data *)cci_object; - - vl53l0_dbgmsg("Enter\n"); - if (data->power_up) { - /* need to release cci first */ - ret = data->client->i2c_func_tbl->i2c_util(data->client, - MSM_CCI_RELEASE); - if (ret < 0) - vl53l0_errmsg("%d,CCI Release failed rc %d\n", - __LINE__, ret); - - pinctrl_select_state(data->pinctrl_info.pinctrl, - data->pinctrl_info.gpio_state_suspend); - - msm_camera_request_gpio_table( - data->gconf.cam_gpio_req_tbl, - data->gconf.cam_gpio_req_tbl_size, 0); - - gpio_set_value_cansleep( - data->gconf.cam_gpio_req_tbl[0].gpio, 0); - - stmvl53l0_vreg_control(data, 0); - } - data->power_up = 0; - vl53l0_dbgmsg("End\n"); - return ret; -} - -int stmvl53l0_init_cci(void) -{ - int ret = 0; - vl53l0_dbgmsg("Enter\n"); - - /* register as a platform device */ - ret = platform_driver_register(&stmvl53l0_platform_driver); - if (ret) - vl53l0_errmsg("%d, error ret:%d\n", __LINE__, ret); - - vl53l0_dbgmsg("End\n"); - - return ret; -} - -void stmvl53l0_exit_cci(void *cci_object) -{ - struct cci_data *data = (struct cci_data *)cci_object; - vl53l0_dbgmsg("Enter\n"); - - if (data && data->client->cci_client) - kfree(data->client->cci_client); - - vl53l0_dbgmsg("End\n"); -} -#endif /* end of CAMERA_CCI */ diff --git a/drivers/input/misc/vl53L0/stmvl53l0_module-i2c.c b/drivers/input/misc/vl53L0/stmvl53l0_module-i2c.c index f20f68e568c8..3439424d4908 100644 --- a/drivers/input/misc/vl53L0/stmvl53l0_module-i2c.c +++ b/drivers/input/misc/vl53L0/stmvl53l0_module-i2c.c @@ -1,22 +1,19 @@ /* -* stmvl53l0_module-i2c.c - Linux kernel modules for STM VL53L0 FlightSense TOF -* sensor -* -* Copyright (C) 2015 STMicroelectronics Imaging Division. -* -* This program is free software; you can redistribute it and/or modify -* it under the terms of the GNU General Public License as published by -* the Free Software Foundation; either version 2 of the License, or -* (at your option) any later version. -* -* This program is distributed in the hope that it will be useful, -* but WITHOUT ANY WARRANTY; without even the implied warranty of -* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -* GNU General Public License for more details. -* -* You should have received a copy of the GNU General Public License -* along with this program; -*/ + * stmvl53l0_module-i2c.c - Linux kernel modules for STM VL53L0 FlightSense TOF + * sensor + * + * Copyright (C) 2016 STMicroelectronics Imaging Division. + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + */ #include #include #include @@ -48,7 +45,6 @@ #include "vl53l0_def.h" #include "vl53l0_platform.h" #include "stmvl53l0-i2c.h" -#include "stmvl53l0-cci.h" #include "stmvl53l0.h" #define LASER_SENSOR_PINCTRL_STATE_SLEEP "laser_suspend" @@ -65,13 +61,13 @@ static int stmvl53l0_parse_vdd(struct device *dev, struct i2c_data *data); static int stmvl53l0_parse_vdd(struct device *dev, struct i2c_data *data) { int ret = 0; + vl53l0_dbgmsg("Enter\n"); if (dev->of_node) { data->vana = regulator_get_optional(dev, "vdd"); if (IS_ERR(data->vana)) { - vl53l0_dbgmsg("%d,vdd supply is not provided\n", - __LINE__); + vl53l0_errmsg("vdd supply is not provided\n"); ret = -1; } } @@ -103,33 +99,6 @@ int get_dt_xtalk_data(struct device_node *of_node, int *xtalk) return rc; } -int get_dt_threshold_data(struct device_node *of_node, int *lowv, int *highv) -{ - int rc = 0; - uint32_t count = 0; - uint32_t v_array[2]; - - count = of_property_count_strings(of_node, "st,sensorthreshold"); - - if (!count) - return 0; - - rc = of_property_read_u32_array(of_node, "st,sensorthreshold", - v_array, 2); - - if (rc != -EINVAL) { - if (rc < 0) { - pr_err("%s failed %d\n", __func__, __LINE__); - } else { - *lowv = v_array[0]; - *highv = v_array[1]; - } - } else { - rc = 0; - } - - return rc; -} static int stmvl53l0_get_dt_gpio_req_tbl(struct device_node *of_node, struct msm_camera_gpio_conf *gconf, uint16_t *gpio_array, @@ -375,8 +344,6 @@ static int stmvl53l0_get_dt_data(struct device *dev, struct i2c_data *data) } kfree(gpio_array); } - rc = get_dt_threshold_data(of_node, - &(data->lowv), &(data->highv)); rc = get_dt_xtalk_data(of_node, &(data->xtalk)); } @@ -384,32 +351,36 @@ static int stmvl53l0_get_dt_data(struct device *dev, struct i2c_data *data) return rc; } - static int stmvl53l0_probe(struct i2c_client *client, - const struct i2c_device_id *id) + const struct i2c_device_id *id) { int rc = 0, i = 0; struct stmvl53l0_data *vl53l0_data = NULL; struct i2c_data *i2c_object = NULL; int present; - vl53l0_dbgmsg("Enter\n"); + vl53l0_errmsg("Enter\n"); if (!i2c_check_functionality(client->adapter, I2C_FUNC_SMBUS_BYTE)) { rc = -EIO; return rc; } - vl53l0_data = stmvl53l0_getobject(); + vl53l0_data = kzalloc(sizeof(struct stmvl53l0_data), GFP_KERNEL); if (!vl53l0_data) { - vl53l0_errmsg("%d,data NULL\n", __LINE__); - return -EINVAL; + rc = -ENOMEM; + return rc; + } + if (vl53l0_data) { + vl53l0_data->client_object = + kzalloc(sizeof(struct i2c_data), GFP_KERNEL); + i2c_object = (struct i2c_data *)vl53l0_data->client_object; } - - if (vl53l0_data) - i2c_object = &(vl53l0_data->i2c_client_object); i2c_object->client = client; + /* setup bus type */ + vl53l0_data->bus_type = I2C_BUS; + /* setup regulator */ stmvl53l0_parse_vdd(&i2c_object->client->dev, i2c_object); @@ -423,6 +394,10 @@ static int stmvl53l0_probe(struct i2c_client *client, i2c_set_clientdata(client, vl53l0_data); stmvl53l0_get_dt_data(&client->dev, i2c_object); + + /* fill xtalk data */ + vl53l0_data->xtalk= i2c_object->xtalk; + rc = stmvl53l0_power_up_i2c(i2c_object, &present); if (rc) { vl53l0_errmsg("%d,error rc %d\n", __LINE__, rc); @@ -454,29 +429,36 @@ static int stmvl53l0_probe(struct i2c_client *client, stmvl53l0_power_down_i2c(i2c_object); - rc = stmvl53l0_setup(vl53l0_data, I2C_BUS); + /* setup other stuff */ + rc = stmvl53l0_setup(vl53l0_data); - vl53l0_dbgmsg("End\n"); + /* init default value */ + i2c_object->power_up = 0; + + vl53l0_errmsg("End\n"); return rc; } static int stmvl53l0_remove(struct i2c_client *client) { - struct stmvl53l0_data *data = stmvl53l0_getobject(); + struct stmvl53l0_data *data = i2c_get_clientdata(client); vl53l0_dbgmsg("Enter\n"); /* Power down the device */ - stmvl53l0_power_down_i2c((void *)data->client_object); - + stmvl53l0_power_down_i2c(data->client_object); + stmvl53l0_cleanup(data); + kfree(data->client_object); + kfree(data); vl53l0_dbgmsg("End\n"); return 0; } static const struct i2c_device_id stmvl53l0_id[] = { - { STMVL53L0_DRV_NAME, 0 }, - { }, + {STMVL53L0_DRV_NAME, 0}, + {}, }; + MODULE_DEVICE_TABLE(i2c, stmvl53l0_id); static const struct of_device_id st_stmvl53l0_dt_match[] = { @@ -484,27 +466,14 @@ static const struct of_device_id st_stmvl53l0_dt_match[] = { { }, }; -/* -int stmv153l0_resume(struct i2c_client *clt) -{ - struct stmvl53l0_data *vl53l0_data = NULL; - - vl53l0_dbgmsg("Enter\n"); - vl53l0_data = stmvl53l0_getobject(); - stmvl53l0_livechecking(vl53l0_data); - return 0; -} -*/ - static struct i2c_driver stmvl53l0_driver = { .driver = { - .name = STMVL53L0_DRV_NAME, - .owner = THIS_MODULE, + .name = STMVL53L0_DRV_NAME, + .owner = THIS_MODULE, .of_match_table = st_stmvl53l0_dt_match, }, - .probe = stmvl53l0_probe, - .remove = stmvl53l0_remove, - /* .resume = stmv153l0_resume, */ + .probe = stmvl53l0_probe, + .remove = stmvl53l0_remove, .id_table = stmvl53l0_id, }; @@ -608,7 +577,7 @@ int stmvl53l0_init_i2c(void) if (ret) vl53l0_errmsg("%d erro ret:%d\n", __LINE__, ret); - vl53l0_dbgmsg("End with rc:%d\n", ret); + vl53l0_errmsg("End with rc:%d\n", ret); return ret; } @@ -620,4 +589,3 @@ void stmvl53l0_exit_i2c(void *i2c_object) vl53l0_dbgmsg("End\n"); } - diff --git a/drivers/input/misc/vl53L0/stmvl53l0_module.c b/drivers/input/misc/vl53L0/stmvl53l0_module.c index a93dcd1dd243..183012b72b6b 100644 --- a/drivers/input/misc/vl53L0/stmvl53l0_module.c +++ b/drivers/input/misc/vl53L0/stmvl53l0_module.c @@ -1,22 +1,19 @@ /* -* stmvl53l0_module.c - Linux kernel modules for STM VL53L0 FlightSense TOF -* sensor -* -* Copyright (C) 2015 STMicroelectronics Imaging Division. -* -* This program is free software; you can redistribute it and/or modify -* it under the terms of the GNU General Public License as published by -* the Free Software Foundation; either version 2 of the License, or -* (at your option) any later version. -* -* This program is distributed in the hope that it will be useful, -* but WITHOUT ANY WARRANTY; without even the implied warranty of -* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -* GNU General Public License for more details. -* -* You should have received a copy of the GNU General Public License -* along with this program; -*/ + * stmvl53l0_module.c - Linux kernel modules for STM VL53L0 FlightSense TOF + * sensor + * + * Copyright (C) 2016 STMicroelectronics Imaging Division. + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + */ #include #include #include @@ -34,246 +31,231 @@ #include #include #include +#include /* -* API includes -*/ + * API includes + */ #include "vl53l0_api.h" -#include "vl53l010_api.h" -/* -#include "vl53l0_def.h" -#include "vl53l0_platform.h" -#include "stmvl53l0-i2c.h" -#include "stmvl53l0-cci.h" -#include "stmvl53l0.h" -*/ -#include "vl53l0_api_histogram.h" - -#define USE_INT -#define IRQ_NUM 59 -#define DATA_TIMEOUT msecs_to_jiffies(150) -/*#define DEBUG_TIME_LOG*/ +/*#define USE_INT */ +/* #define DEBUG_TIME_LOG */ #ifdef DEBUG_TIME_LOG struct timeval start_tv, stop_tv; #endif -#define SAR_LOW 30 -#define SAR_HIGH 40 +/* + * Global data + */ static char *devname = "laser"; - -static struct stmvl53l0_data *gp_vl53l0_data; -static int s_count; -static int s_prevalue; - -#ifdef CAMERA_CCI -static struct stmvl53l0_module_fn_t stmvl53l0_module_func_tbl = { - .init = stmvl53l0_init_cci, - .deinit = stmvl53l0_exit_cci, - .power_up = stmvl53l0_power_up_cci, - .power_down = stmvl53l0_power_down_cci, -}; -#else static struct stmvl53l0_module_fn_t stmvl53l0_module_func_tbl = { .init = stmvl53l0_init_i2c, .deinit = stmvl53l0_exit_i2c, .power_up = stmvl53l0_power_up_i2c, .power_down = stmvl53l0_power_down_i2c, }; -#endif struct stmvl53l0_module_fn_t *pmodule_func_tbl; -static struct stmvl53l0_parameter s_parameters[10]; -static int s_nump; - struct stmvl53l0_api_fn_t { - VL53L0_Error(*GetVersion)(VL53L0_Version_t *pVersion); - VL53L0_Error(*GetPalSpecVersion)(VL53L0_Version_t *pPalSpecVersion); + int8_t (*GetVersion)(VL53L0_Version_t *pVersion); + int8_t (*GetPalSpecVersion)(VL53L0_Version_t *pPalSpecVersion); - VL53L0_Error(*GetProductRevision)(VL53L0_DEV Dev, + int8_t (*GetProductRevision)(VL53L0_DEV Dev, uint8_t *pProductRevisionMajor, uint8_t *pProductRevisionMinor); - VL53L0_Error(*GetDeviceInfo)(VL53L0_DEV Dev, + int8_t (*GetDeviceInfo)(VL53L0_DEV Dev, VL53L0_DeviceInfo_t *pVL53L0_DeviceInfo); - VL53L0_Error(*GetDeviceErrorStatus)(VL53L0_DEV Dev, - VL53L0_DeviceError * pDeviceErrorStatus); + int8_t (*GetDeviceErrorStatus)(VL53L0_DEV Dev, + VL53L0_DeviceError *pDeviceErrorStatus); int8_t (*GetRangeStatusString)(uint8_t RangeStatus, - char *pRangeStatusString); - VL53L0_Error(*GetDeviceErrorString)(VL53L0_DeviceError ErrorCode, + char *pRangeStatusString); + int8_t (*GetDeviceErrorString)(VL53L0_DeviceError ErrorCode, char *pDeviceErrorString); - VL53L0_Error(*GetPalErrorString)(VL53L0_Error PalErrorCode, + int8_t (*GetPalErrorString)(VL53L0_Error PalErrorCode, char *pPalErrorString); int8_t (*GetPalStateString)(VL53L0_State PalStateCode, - char *pPalStateString); - VL53L0_Error(*GetPalState)(VL53L0_DEV Dev, VL53L0_State * pPalState); - VL53L0_Error(*SetPowerMode)(VL53L0_DEV Dev, + char *pPalStateString); + int8_t (*GetPalState)(VL53L0_DEV Dev, VL53L0_State *pPalState); + int8_t (*SetPowerMode)(VL53L0_DEV Dev, VL53L0_PowerModes PowerMode); - VL53L0_Error(*GetPowerMode)(VL53L0_DEV Dev, - VL53L0_PowerModes * pPowerMode); - VL53L0_Error(*SetOffsetCalibrationDataMicroMeter)(VL53L0_DEV Dev, + int8_t (*GetPowerMode)(VL53L0_DEV Dev, + VL53L0_PowerModes *pPowerMode); + int8_t (*SetOffsetCalibrationDataMicroMeter)(VL53L0_DEV Dev, int32_t OffsetCalibrationDataMicroMeter); - VL53L0_Error(*GetOffsetCalibrationDataMicroMeter)(VL53L0_DEV Dev, + int8_t (*GetOffsetCalibrationDataMicroMeter)(VL53L0_DEV Dev, int32_t *pOffsetCalibrationDataMicroMeter); int8_t (*SetLinearityCorrectiveGain)(VL53L0_DEV Dev, - int16_t LinearityCorrectiveGain); + int16_t LinearityCorrectiveGain); int8_t (*GetLinearityCorrectiveGain)(VL53L0_DEV Dev, - uint16_t *pLinearityCorrectiveGain); - VL53L0_Error(*SetGroupParamHold)(VL53L0_DEV Dev, + uint16_t *pLinearityCorrectiveGain); + int8_t (*SetGroupParamHold)(VL53L0_DEV Dev, uint8_t GroupParamHold); - VL53L0_Error(*GetUpperLimitMilliMeter)(VL53L0_DEV Dev, + int8_t (*GetUpperLimitMilliMeter)(VL53L0_DEV Dev, uint16_t *pUpperLimitMilliMeter); - VL53L0_Error(*SetDeviceAddress)(VL53L0_DEV Dev, + int8_t (*SetDeviceAddress)(VL53L0_DEV Dev, uint8_t DeviceAddress); - VL53L0_Error(*DataInit)(VL53L0_DEV Dev); - VL53L0_Error(*SetTuningSettingBuffer)(VL53L0_DEV Dev, + int8_t (*DataInit)(VL53L0_DEV Dev); + int8_t (*SetTuningSettingBuffer)(VL53L0_DEV Dev, uint8_t *pTuningSettingBuffer, uint8_t UseInternalTuningSettings); - VL53L0_Error(*GetTuningSettingBuffer)(VL53L0_DEV Dev, + int8_t (*GetTuningSettingBuffer)(VL53L0_DEV Dev, uint8_t **pTuningSettingBuffer, uint8_t *pUseInternalTuningSettings); - VL53L0_Error(*StaticInit)(VL53L0_DEV Dev); - VL53L0_Error(*WaitDeviceBooted)(VL53L0_DEV Dev); - VL53L0_Error(*ResetDevice)(VL53L0_DEV Dev); - VL53L0_Error(*SetDeviceParameters)(VL53L0_DEV Dev, + int8_t (*StaticInit)(VL53L0_DEV Dev); + int8_t (*WaitDeviceBooted)(VL53L0_DEV Dev); + int8_t (*ResetDevice)(VL53L0_DEV Dev); + int8_t (*SetDeviceParameters)(VL53L0_DEV Dev, const VL53L0_DeviceParameters_t *pDeviceParameters); - VL53L0_Error(*GetDeviceParameters)(VL53L0_DEV Dev, + int8_t (*GetDeviceParameters)(VL53L0_DEV Dev, VL53L0_DeviceParameters_t *pDeviceParameters); - VL53L0_Error(*SetDeviceMode)(VL53L0_DEV Dev, + int8_t (*SetDeviceMode)(VL53L0_DEV Dev, VL53L0_DeviceModes DeviceMode); - VL53L0_Error(*GetDeviceMode)(VL53L0_DEV Dev, - VL53L0_DeviceModes * pDeviceMode); - VL53L0_Error(*SetHistogramMode)(VL53L0_DEV Dev, + int8_t (*GetDeviceMode)(VL53L0_DEV Dev, + VL53L0_DeviceModes *pDeviceMode); + int8_t (*SetHistogramMode)(VL53L0_DEV Dev, VL53L0_HistogramModes HistogramMode); - VL53L0_Error(*GetHistogramMode)(VL53L0_DEV Dev, - VL53L0_HistogramModes * pHistogramMode); - VL53L0_Error(*SetMeasurementTimingBudgetMicroSeconds)(VL53L0_DEV Dev, + int8_t (*GetHistogramMode)(VL53L0_DEV Dev, + VL53L0_HistogramModes *pHistogramMode); + int8_t (*SetMeasurementTimingBudgetMicroSeconds)(VL53L0_DEV Dev, uint32_t MeasurementTimingBudgetMicroSeconds); - VL53L0_Error(*GetMeasurementTimingBudgetMicroSeconds)( + int8_t (*GetMeasurementTimingBudgetMicroSeconds)( VL53L0_DEV Dev, uint32_t *pMeasurementTimingBudgetMicroSeconds); - VL53L0_Error(*GetVcselPulsePeriod)(VL53L0_DEV Dev, + int8_t (*GetVcselPulsePeriod)(VL53L0_DEV Dev, VL53L0_VcselPeriod VcselPeriodType, uint8_t *pVCSELPulsePeriod); - VL53L0_Error(*SetVcselPulsePeriod)(VL53L0_DEV Dev, + int8_t (*SetVcselPulsePeriod)(VL53L0_DEV Dev, VL53L0_VcselPeriod VcselPeriodType, uint8_t VCSELPulsePeriod); - VL53L0_Error(*SetSequenceStepEnable)(VL53L0_DEV Dev, + int8_t (*SetSequenceStepEnable)(VL53L0_DEV Dev, VL53L0_SequenceStepId SequenceStepId, uint8_t SequenceStepEnabled); - VL53L0_Error(*GetSequenceStepEnable)(VL53L0_DEV Dev, + int8_t (*GetSequenceStepEnable)(VL53L0_DEV Dev, VL53L0_SequenceStepId SequenceStepId, uint8_t *pSequenceStepEnabled); - VL53L0_Error(*GetSequenceStepEnables)(VL53L0_DEV Dev, + int8_t (*GetSequenceStepEnables)(VL53L0_DEV Dev, VL53L0_SchedulerSequenceSteps_t *pSchedulerSequenceSteps); - VL53L0_Error(*SetSequenceStepTimeout)(VL53L0_DEV Dev, + int8_t (*SetSequenceStepTimeout)(VL53L0_DEV Dev, VL53L0_SequenceStepId SequenceStepId, FixPoint1616_t TimeOutMilliSecs); - VL53L0_Error(*GetSequenceStepTimeout)(VL53L0_DEV Dev, + int8_t (*GetSequenceStepTimeout)(VL53L0_DEV Dev, VL53L0_SequenceStepId SequenceStepId, FixPoint1616_t *pTimeOutMilliSecs); - VL53L0_Error(*GetNumberOfSequenceSteps)(VL53L0_DEV Dev, + int8_t (*GetNumberOfSequenceSteps)(VL53L0_DEV Dev, uint8_t *pNumberOfSequenceSteps); - VL53L0_Error(*GetSequenceStepsInfo)(VL53L0_SequenceStepId SequenceStepId, + int8_t (*GetSequenceStepsInfo)( + VL53L0_SequenceStepId SequenceStepId, char *pSequenceStepsString); - VL53L0_Error(*SetInterMeasurementPeriodMilliSeconds)( + int8_t (*SetInterMeasurementPeriodMilliSeconds)( VL53L0_DEV Dev, uint32_t InterMeasurementPeriodMilliSeconds); - VL53L0_Error(*GetInterMeasurementPeriodMilliSeconds)( + int8_t (*GetInterMeasurementPeriodMilliSeconds)( VL53L0_DEV Dev, uint32_t *pInterMeasurementPeriodMilliSeconds); - VL53L0_Error(*SetXTalkCompensationEnable)(VL53L0_DEV Dev, + int8_t (*SetXTalkCompensationEnable)(VL53L0_DEV Dev, uint8_t XTalkCompensationEnable); - VL53L0_Error(*GetXTalkCompensationEnable)(VL53L0_DEV Dev, + int8_t (*GetXTalkCompensationEnable)(VL53L0_DEV Dev, uint8_t *pXTalkCompensationEnable); - VL53L0_Error(*SetXTalkCompensationRateMegaCps)( + int8_t (*SetXTalkCompensationRateMegaCps)( VL53L0_DEV Dev, FixPoint1616_t XTalkCompensationRateMegaCps); - VL53L0_Error(*GetXTalkCompensationRateMegaCps)( + int8_t (*GetXTalkCompensationRateMegaCps)( VL53L0_DEV Dev, FixPoint1616_t *pXTalkCompensationRateMegaCps); - VL53L0_Error(*GetNumberOfLimitCheck)( + int8_t (*GetNumberOfLimitCheck)( uint16_t *pNumberOfLimitCheck); - VL53L0_Error(*GetLimitCheckInfo)(VL53L0_DEV Dev, + int8_t (*GetLimitCheckInfo)(VL53L0_DEV Dev, uint16_t LimitCheckId, char *pLimitCheckString); - VL53L0_Error(*SetLimitCheckEnable)(VL53L0_DEV Dev, + int8_t (*SetLimitCheckEnable)(VL53L0_DEV Dev, uint16_t LimitCheckId, uint8_t LimitCheckEnable); - VL53L0_Error(*GetLimitCheckEnable)(VL53L0_DEV Dev, + int8_t (*GetLimitCheckEnable)(VL53L0_DEV Dev, uint16_t LimitCheckId, uint8_t *pLimitCheckEnable); - VL53L0_Error(*SetLimitCheckValue)(VL53L0_DEV Dev, + int8_t (*SetLimitCheckValue)(VL53L0_DEV Dev, uint16_t LimitCheckId, FixPoint1616_t LimitCheckValue); - VL53L0_Error(*GetLimitCheckValue)(VL53L0_DEV Dev, + int8_t (*GetLimitCheckValue)(VL53L0_DEV Dev, uint16_t LimitCheckId, FixPoint1616_t *pLimitCheckValue); - VL53L0_Error(*GetLimitCheckCurrent)(VL53L0_DEV Dev, + int8_t (*GetLimitCheckCurrent)(VL53L0_DEV Dev, uint16_t LimitCheckId, FixPoint1616_t *pLimitCheckCurrent); - VL53L0_Error(*SetWrapAroundCheckEnable)(VL53L0_DEV Dev, + int8_t (*SetWrapAroundCheckEnable)(VL53L0_DEV Dev, uint8_t WrapAroundCheckEnable); - VL53L0_Error(*GetWrapAroundCheckEnable)(VL53L0_DEV Dev, + int8_t (*GetWrapAroundCheckEnable)(VL53L0_DEV Dev, uint8_t *pWrapAroundCheckEnable); - VL53L0_Error(*PerformSingleMeasurement)(VL53L0_DEV Dev); - VL53L0_Error(*PerformRefCalibration)(VL53L0_DEV Dev, - uint8_t *pVhvSettings, uint8_t *pPhaseCal); - VL53L0_Error(*PerformXTalkCalibration)(VL53L0_DEV Dev, + int8_t (*PerformSingleMeasurement)(VL53L0_DEV Dev); + int8_t (*PerformRefCalibration)(VL53L0_DEV Dev, + uint8_t *pVhvSettings, uint8_t *pPhaseCal); + int8_t (*SetRefCalibration)(VL53L0_DEV Dev, + uint8_t VhvSettings, + uint8_t PhaseCal); + int8_t (*GetRefCalibration)(VL53L0_DEV Dev, + uint8_t *pVhvSettings, + uint8_t *pPhaseCal); + int8_t (*PerformXTalkCalibration)(VL53L0_DEV Dev, FixPoint1616_t XTalkCalDistance, FixPoint1616_t *pXTalkCompensationRateMegaCps); - VL53L0_Error(*PerformOffsetCalibration)(VL53L0_DEV Dev, + int8_t (*PerformOffsetCalibration)(VL53L0_DEV Dev, FixPoint1616_t CalDistanceMilliMeter, int32_t *pOffsetMicroMeter); - VL53L0_Error(*StartMeasurement)(VL53L0_DEV Dev); - VL53L0_Error(*StopMeasurement)(VL53L0_DEV Dev); - VL53L0_Error(*GetMeasurementDataReady)(VL53L0_DEV Dev, + int8_t (*StartMeasurement)(VL53L0_DEV Dev); + int8_t (*StopMeasurement)(VL53L0_DEV Dev); + int8_t (*GetMeasurementDataReady)(VL53L0_DEV Dev, uint8_t *pMeasurementDataReady); - VL53L0_Error(*WaitDeviceReadyForNewMeasurement)(VL53L0_DEV Dev, + int8_t (*WaitDeviceReadyForNewMeasurement)(VL53L0_DEV Dev, uint32_t MaxLoop); - VL53L0_Error(*GetRangingMeasurementData)(VL53L0_DEV Dev, + int8_t (*GetRangingMeasurementData)(VL53L0_DEV Dev, VL53L0_RangingMeasurementData_t *pRangingMeasurementData); - VL53L0_Error(*GetHistogramMeasurementData)(VL53L0_DEV Dev, + int8_t (*GetHistogramMeasurementData)(VL53L0_DEV Dev, VL53L0_HistogramMeasurementData_t *pHistogramMeasurementData); - VL53L0_Error(*PerformSingleRangingMeasurement)(VL53L0_DEV Dev, + int8_t (*PerformSingleRangingMeasurement)(VL53L0_DEV Dev, VL53L0_RangingMeasurementData_t *pRangingMeasurementData); - VL53L0_Error(*PerformSingleHistogramMeasurement)(VL53L0_DEV Dev, + int8_t (*PerformSingleHistogramMeasurement)(VL53L0_DEV Dev, VL53L0_HistogramMeasurementData_t *pHistogramMeasurementData); - VL53L0_Error(*SetNumberOfROIZones)(VL53L0_DEV Dev, + int8_t (*SetNumberOfROIZones)(VL53L0_DEV Dev, uint8_t NumberOfROIZones); - VL53L0_Error(*GetNumberOfROIZones)(VL53L0_DEV Dev, + int8_t (*GetNumberOfROIZones)(VL53L0_DEV Dev, uint8_t *pNumberOfROIZones); - VL53L0_Error(*GetMaxNumberOfROIZones)(VL53L0_DEV Dev, + int8_t (*GetMaxNumberOfROIZones)(VL53L0_DEV Dev, uint8_t *pMaxNumberOfROIZones); - VL53L0_Error(*SetGpioConfig)(VL53L0_DEV Dev, + int8_t (*SetGpioConfig)(VL53L0_DEV Dev, uint8_t Pin, VL53L0_DeviceModes DeviceMode, VL53L0_GpioFunctionality Functionality, VL53L0_InterruptPolarity Polarity); - VL53L0_Error(*GetGpioConfig)(VL53L0_DEV Dev, + int8_t (*GetGpioConfig)(VL53L0_DEV Dev, uint8_t Pin, - VL53L0_DeviceModes * pDeviceMode, - VL53L0_GpioFunctionality * pFunctionality, - VL53L0_InterruptPolarity * pPolarity); - VL53L0_Error(*SetInterruptThresholds)(VL53L0_DEV Dev, + VL53L0_DeviceModes *pDeviceMode, + VL53L0_GpioFunctionality *pFunctionality, + VL53L0_InterruptPolarity *pPolarity); + int8_t (*SetInterruptThresholds)(VL53L0_DEV Dev, VL53L0_DeviceModes DeviceMode, FixPoint1616_t ThresholdLow, FixPoint1616_t ThresholdHigh); - VL53L0_Error(*GetInterruptThresholds)(VL53L0_DEV Dev, + int8_t (*GetInterruptThresholds)(VL53L0_DEV Dev, VL53L0_DeviceModes DeviceMode, FixPoint1616_t *pThresholdLow, FixPoint1616_t *pThresholdHigh); - VL53L0_Error(*ClearInterruptMask)(VL53L0_DEV Dev, + int8_t (*ClearInterruptMask)(VL53L0_DEV Dev, uint32_t InterruptMask); - VL53L0_Error(*GetInterruptMaskStatus)(VL53L0_DEV Dev, + int8_t (*GetInterruptMaskStatus)(VL53L0_DEV Dev, uint32_t *pInterruptMaskStatus); - VL53L0_Error(*EnableInterruptMask)(VL53L0_DEV Dev, - uint32_t InterruptMask); - VL53L0_Error(*SetSpadAmbientDamperThreshold)(VL53L0_DEV Dev, + int8_t (*EnableInterruptMask)(VL53L0_DEV Dev, uint32_t InterruptMask); + int8_t (*SetSpadAmbientDamperThreshold)(VL53L0_DEV Dev, uint16_t SpadAmbientDamperThreshold); - VL53L0_Error(*GetSpadAmbientDamperThreshold)(VL53L0_DEV Dev, + int8_t (*GetSpadAmbientDamperThreshold)(VL53L0_DEV Dev, uint16_t *pSpadAmbientDamperThreshold); - VL53L0_Error(*SetSpadAmbientDamperFactor)(VL53L0_DEV Dev, + int8_t (*SetSpadAmbientDamperFactor)(VL53L0_DEV Dev, uint16_t SpadAmbientDamperFactor); - VL53L0_Error(*GetSpadAmbientDamperFactor)(VL53L0_DEV Dev, + int8_t (*GetSpadAmbientDamperFactor)(VL53L0_DEV Dev, uint16_t *pSpadAmbientDamperFactor); - VL53L0_Error(*PerformRefSpadManagement)(VL53L0_DEV Dev, - uint32_t *refSpadCount, uint8_t *isApertureSpads); + int8_t (*PerformRefSpadManagement)(VL53L0_DEV Dev, + uint32_t *refSpadCount, uint8_t *isApertureSpads); + int8_t (*SetReferenceSpads)(VL53L0_DEV Dev, + uint32_t count, uint8_t isApertureSpads); + int8_t (*GetReferenceSpads)(VL53L0_DEV Dev, + uint32_t *pSpadCount, uint8_t *pIsApertureSpads); + int8_t (*GetStopCompletedStatus)(VL53L0_DEV Dev, + uint32_t *pStopStatus); }; static struct stmvl53l0_api_fn_t stmvl53l0_api_func_tbl = { @@ -289,13 +271,13 @@ static struct stmvl53l0_api_fn_t stmvl53l0_api_func_tbl = { .SetPowerMode = VL53L0_SetPowerMode, .GetPowerMode = VL53L0_GetPowerMode, .SetOffsetCalibrationDataMicroMeter = - VL53L0_SetOffsetCalibrationDataMicroMeter, + VL53L0_SetOffsetCalibrationDataMicroMeter, .SetLinearityCorrectiveGain = VL53L0_SetLinearityCorrectiveGain, .GetLinearityCorrectiveGain = VL53L0_GetLinearityCorrectiveGain, .GetOffsetCalibrationDataMicroMeter = - VL53L0_GetOffsetCalibrationDataMicroMeter, + VL53L0_GetOffsetCalibrationDataMicroMeter, .SetGroupParamHold = VL53L0_SetGroupParamHold, .GetUpperLimitMilliMeter = VL53L0_GetUpperLimitMilliMeter, .SetDeviceAddress = VL53L0_SetDeviceAddress, @@ -311,9 +293,9 @@ static struct stmvl53l0_api_fn_t stmvl53l0_api_func_tbl = { .SetHistogramMode = VL53L0_SetHistogramMode, .GetHistogramMode = VL53L0_GetHistogramMode, .SetMeasurementTimingBudgetMicroSeconds = - VL53L0_SetMeasurementTimingBudgetMicroSeconds, + VL53L0_SetMeasurementTimingBudgetMicroSeconds, .GetMeasurementTimingBudgetMicroSeconds = - VL53L0_GetMeasurementTimingBudgetMicroSeconds, + VL53L0_GetMeasurementTimingBudgetMicroSeconds, .GetVcselPulsePeriod = VL53L0_GetVcselPulsePeriod, .SetVcselPulsePeriod = VL53L0_SetVcselPulsePeriod, .SetSequenceStepEnable = VL53L0_SetSequenceStepEnable, @@ -324,15 +306,15 @@ static struct stmvl53l0_api_fn_t stmvl53l0_api_func_tbl = { .GetNumberOfSequenceSteps = VL53L0_GetNumberOfSequenceSteps, .GetSequenceStepsInfo = VL53L0_GetSequenceStepsInfo, .SetInterMeasurementPeriodMilliSeconds = - VL53L0_SetInterMeasurementPeriodMilliSeconds, + VL53L0_SetInterMeasurementPeriodMilliSeconds, .GetInterMeasurementPeriodMilliSeconds = - VL53L0_GetInterMeasurementPeriodMilliSeconds, + VL53L0_GetInterMeasurementPeriodMilliSeconds, .SetXTalkCompensationEnable = VL53L0_SetXTalkCompensationEnable, .GetXTalkCompensationEnable = VL53L0_GetXTalkCompensationEnable, .SetXTalkCompensationRateMegaCps = - VL53L0_SetXTalkCompensationRateMegaCps, + VL53L0_SetXTalkCompensationRateMegaCps, .GetXTalkCompensationRateMegaCps = - VL53L0_GetXTalkCompensationRateMegaCps, + VL53L0_GetXTalkCompensationRateMegaCps, .GetNumberOfLimitCheck = VL53L0_GetNumberOfLimitCheck, .GetLimitCheckInfo = VL53L0_GetLimitCheckInfo, .SetLimitCheckEnable = VL53L0_SetLimitCheckEnable, @@ -344,19 +326,21 @@ static struct stmvl53l0_api_fn_t stmvl53l0_api_func_tbl = { .GetWrapAroundCheckEnable = VL53L0_GetWrapAroundCheckEnable, .PerformSingleMeasurement = VL53L0_PerformSingleMeasurement, .PerformRefCalibration = VL53L0_PerformRefCalibration, + .SetRefCalibration = VL53L0_SetRefCalibration, + .GetRefCalibration = VL53L0_GetRefCalibration, .PerformXTalkCalibration = VL53L0_PerformXTalkCalibration, .PerformOffsetCalibration = VL53L0_PerformOffsetCalibration, .StartMeasurement = VL53L0_StartMeasurement, .StopMeasurement = VL53L0_StopMeasurement, .GetMeasurementDataReady = VL53L0_GetMeasurementDataReady, .WaitDeviceReadyForNewMeasurement = - VL53L0_WaitDeviceReadyForNewMeasurement, + VL53L0_WaitDeviceReadyForNewMeasurement, .GetRangingMeasurementData = VL53L0_GetRangingMeasurementData, .GetHistogramMeasurementData = VL53L0_GetHistogramMeasurementData, .PerformSingleRangingMeasurement = - VL53L0_PerformSingleRangingMeasurement, + VL53L0_PerformSingleRangingMeasurement, .PerformSingleHistogramMeasurement = - VL53L0_PerformSingleHistogramMeasurement, + VL53L0_PerformSingleHistogramMeasurement, .SetNumberOfROIZones = VL53L0_SetNumberOfROIZones, .GetNumberOfROIZones = VL53L0_GetNumberOfROIZones, .GetMaxNumberOfROIZones = VL53L0_GetMaxNumberOfROIZones, @@ -372,31 +356,102 @@ static struct stmvl53l0_api_fn_t stmvl53l0_api_func_tbl = { .SetSpadAmbientDamperFactor = VL53L0_SetSpadAmbientDamperFactor, .GetSpadAmbientDamperFactor = VL53L0_GetSpadAmbientDamperFactor, .PerformRefSpadManagement = VL53L0_PerformRefSpadManagement, + .SetReferenceSpads = VL53L0_SetReferenceSpads, + .GetReferenceSpads = VL53L0_GetReferenceSpads, + .GetStopCompletedStatus = VL53L0_GetStopCompletedStatus, + }; struct stmvl53l0_api_fn_t *papi_func_tbl; -int stmvl53l0_stopmeasurement(struct stmvl53l0_data *data) -{ - uint32_t us; +/* + * IOCTL definitions + */ +#define VL53L0_IOCTL_INIT _IO('p', 0x01) +#define VL53L0_IOCTL_XTALKCALB _IOWR('p', 0x02, unsigned int) +#define VL53L0_IOCTL_OFFCALB _IOWR('p', 0x03, unsigned int) +#define VL53L0_IOCTL_STOP _IO('p', 0x05) +#define VL53L0_IOCTL_SETXTALK _IOW('p', 0x06, unsigned int) +#define VL53L0_IOCTL_SETOFFSET _IOW('p', 0x07, int8_t) +#define VL53L0_IOCTL_ACTIVATE_USE_CASE _IOW('p', 0x08, uint8_t) +#define VL53L0_IOCTL_ACTIVATE_CUSTOM_USE_CASE \ + _IOW('p', 0x09, struct stmvl53l0_custom_use_case) - VL53L0_StopMeasurement(data); - VL53L0_GetMeasurementTimingBudgetMicroSeconds( - data, (uint32_t *)&us); - udelay(us); - return 0; +#define VL53L0_IOCTL_GETDATAS \ + _IOWR('p', 0x0b, VL53L0_RangingMeasurementData_t) +#define VL53L0_IOCTL_REGISTER \ + _IOWR('p', 0x0c, struct stmvl53l0_register) +#define VL53L0_IOCTL_PARAMETER \ + _IOWR('p', 0x0d, struct stmvl53l0_parameter) + +/* Mask fields to indicate Offset and Xtalk Comp + * values have been set by application + */ +#define SET_OFFSET_CALIB_DATA_MICROMETER_MASK 0x1 +#define SET_XTALK_COMP_RATE_MCPS_MASK 0x2 + +/* Macros used across different functions */ +#define USE_CASE_LONG_DISTANCE 1 +#define USE_CASE_HIGH_ACCURACY 2 +#define USE_CASE_HIGH_SPEED 3 +#define USE_CASE_CUSTOM 4 + +#define LONG_DISTANCE_TIMING_BUDGET 26000 +#define LONG_DISTANCE_SIGNAL_RATE_LIMIT (65536 / 10) /* 0.1 * 65536 */ +#define LONG_DISTANCE_SIGMA_LIMIT (60*65536) +#define LONG_DISTANCE_PRE_RANGE_PULSE_PERIOD 18 +#define LONG_DISTANCE_FINAL_RANGE_PULSE_PERIOD 14 + +#define HIGH_ACCURACY_TIMING_BUDGET 200000 +#define HIGH_ACCURACY_SIGNAL_RATE_LIMIT (25 * 65536 / 100) /* 0.25 * 65536 */ +#define HIGH_ACCURACY_SIGMA_LIMIT (18*65536) +#define HIGH_ACCURACY_PRE_RANGE_PULSE_PERIOD 14 +#define HIGH_ACCURACY_FINAL_RANGE_PULSE_PERIOD 10 + +#define HIGH_SPEED_TIMING_BUDGET 20000 +#define HIGH_SPEED_SIGNAL_RATE_LIMIT (25 * 65536 / 100) /* 0.25 * 65536 */ +#define HIGH_SPEED_SIGMA_LIMIT (32*65536) +#define HIGH_SPEED_PRE_RANGE_PULSE_PERIOD 14 +#define HIGH_SPEED_FINAL_RANGE_PULSE_PERIOD 10 + +static long stmvl53l0_ioctl(struct file *file, + unsigned int cmd, unsigned long arg); +/*static int stmvl53l0_flush(struct file *file, fl_owner_t id);*/ +static int stmvl53l0_open(struct inode *inode, struct file *file); +static int stmvl53l0_init_client(struct stmvl53l0_data *data); +static int stmvl53l0_start(struct stmvl53l0_data *data, uint8_t scaling, + init_mode_e mode); +static int stmvl53l0_stop(struct stmvl53l0_data *data); +static int stmvl53l0_config_use_case(struct stmvl53l0_data *data); + +#ifdef DEBUG_TIME_LOG +static void stmvl53l0_DebugTimeGet(struct timeval *ptv) +{ + do_gettimeofday(ptv); } -static void stmvl53l0_setupAPIFunctions(struct stmvl53l0_data *data) +static void stmvl53l0_DebugTimeDuration(struct timeval *pstart_tv, + struct timeval *pstop_tv) +{ + long total_sec, total_msec; + + total_sec = pstop_tv->tv_sec - pstart_tv->tv_sec; + total_msec = (pstop_tv->tv_usec - pstart_tv->tv_usec)/1000; + total_msec += total_sec * 1000; + pr_err("elapsedTime:%ld\n", total_msec); +} +#endif + +static uint8_t stmvl53l0_setupAPIFunctions(struct stmvl53l0_data *data) { uint8_t revision = 0; VL53L0_DEV vl53l0_dev = data; /* Read Revision ID */ VL53L0_RdByte(vl53l0_dev, - VL53L0_REG_IDENTIFICATION_REVISION_ID, &revision); + VL53L0_REG_IDENTIFICATION_REVISION_ID, + &revision); vl53l0_errmsg("read REVISION_ID: 0x%x\n", revision); revision = (revision & 0xF0) >> 4; - data->cut_v = revision; if (revision == 1) { /*cut 1.1*/ vl53l0_errmsg("to setup API cut 1.1\n"); @@ -406,12 +461,18 @@ static void stmvl53l0_setupAPIFunctions(struct stmvl53l0_data *data) papi_func_tbl->GetDeviceInfo = VL53L0_GetDeviceInfo; papi_func_tbl->GetDeviceErrorStatus = VL53L0_GetDeviceErrorStatus; + papi_func_tbl->GetRangeStatusString = + VL53L0_GetRangeStatusString; papi_func_tbl->GetDeviceErrorString = VL53L0_GetDeviceErrorString; - papi_func_tbl->GetPalErrorString = VL53L0_GetPalErrorString; - papi_func_tbl->GetPalState = VL53L0_GetPalState; - papi_func_tbl->SetPowerMode = VL53L0_SetPowerMode; - papi_func_tbl->GetPowerMode = VL53L0_GetPowerMode; + papi_func_tbl->GetPalErrorString = + VL53L0_GetPalErrorString; + papi_func_tbl->GetPalState = + VL53L0_GetPalState; + papi_func_tbl->SetPowerMode = + VL53L0_SetPowerMode; + papi_func_tbl->GetPowerMode = + VL53L0_GetPowerMode; papi_func_tbl->SetOffsetCalibrationDataMicroMeter = VL53L0_SetOffsetCalibrationDataMicroMeter; papi_func_tbl->GetOffsetCalibrationDataMicroMeter = @@ -423,16 +484,22 @@ static void stmvl53l0_setupAPIFunctions(struct stmvl53l0_data *data) papi_func_tbl->SetGroupParamHold = VL53L0_SetGroupParamHold; papi_func_tbl->GetUpperLimitMilliMeter = VL53L0_GetUpperLimitMilliMeter; - papi_func_tbl->SetDeviceAddress = VL53L0_SetDeviceAddress; - papi_func_tbl->DataInit = VL53L0_DataInit; + papi_func_tbl->SetDeviceAddress = + VL53L0_SetDeviceAddress; + papi_func_tbl->DataInit = + VL53L0_DataInit; papi_func_tbl->SetTuningSettingBuffer = VL53L0_SetTuningSettingBuffer; papi_func_tbl->GetTuningSettingBuffer = VL53L0_GetTuningSettingBuffer; - papi_func_tbl->StaticInit = VL53L0_StaticInit; - papi_func_tbl->WaitDeviceBooted = VL53L0_WaitDeviceBooted; - papi_func_tbl->ResetDevice = VL53L0_ResetDevice; - papi_func_tbl->SetDeviceParameters = VL53L0_SetDeviceParameters; + papi_func_tbl->StaticInit = + VL53L0_StaticInit; + papi_func_tbl->WaitDeviceBooted = + VL53L0_WaitDeviceBooted; + papi_func_tbl->ResetDevice = + VL53L0_ResetDevice; + papi_func_tbl->SetDeviceParameters = + VL53L0_SetDeviceParameters; papi_func_tbl->SetDeviceMode = VL53L0_SetDeviceMode; papi_func_tbl->GetDeviceMode = VL53L0_GetDeviceMode; papi_func_tbl->SetHistogramMode = VL53L0_SetHistogramMode; @@ -471,11 +538,16 @@ static void stmvl53l0_setupAPIFunctions(struct stmvl53l0_data *data) VL53L0_GetXTalkCompensationRateMegaCps; papi_func_tbl->GetNumberOfLimitCheck = VL53L0_GetNumberOfLimitCheck; - papi_func_tbl->GetLimitCheckInfo = VL53L0_GetLimitCheckInfo; - papi_func_tbl->SetLimitCheckEnable = VL53L0_SetLimitCheckEnable; - papi_func_tbl->GetLimitCheckEnable = VL53L0_GetLimitCheckEnable; - papi_func_tbl->SetLimitCheckValue = VL53L0_SetLimitCheckValue; - papi_func_tbl->GetLimitCheckValue = VL53L0_GetLimitCheckValue; + papi_func_tbl->GetLimitCheckInfo = + VL53L0_GetLimitCheckInfo; + papi_func_tbl->SetLimitCheckEnable = + VL53L0_SetLimitCheckEnable; + papi_func_tbl->GetLimitCheckEnable = + VL53L0_GetLimitCheckEnable; + papi_func_tbl->SetLimitCheckValue = + VL53L0_SetLimitCheckValue; + papi_func_tbl->GetLimitCheckValue = + VL53L0_GetLimitCheckValue; papi_func_tbl->GetLimitCheckCurrent = VL53L0_GetLimitCheckCurrent; papi_func_tbl->SetWrapAroundCheckEnable = @@ -486,12 +558,18 @@ static void stmvl53l0_setupAPIFunctions(struct stmvl53l0_data *data) VL53L0_PerformSingleMeasurement; papi_func_tbl->PerformRefCalibration = VL53L0_PerformRefCalibration; + papi_func_tbl->SetRefCalibration = + VL53L0_SetRefCalibration; + papi_func_tbl->GetRefCalibration = + VL53L0_GetRefCalibration; papi_func_tbl->PerformXTalkCalibration = VL53L0_PerformXTalkCalibration; papi_func_tbl->PerformOffsetCalibration = VL53L0_PerformOffsetCalibration; - papi_func_tbl->StartMeasurement = VL53L0_StartMeasurement; - papi_func_tbl->StopMeasurement = VL53L0_StopMeasurement; + papi_func_tbl->StartMeasurement = + VL53L0_StartMeasurement; + papi_func_tbl->StopMeasurement = + VL53L0_StopMeasurement; papi_func_tbl->GetMeasurementDataReady = VL53L0_GetMeasurementDataReady; papi_func_tbl->WaitDeviceReadyForNewMeasurement = @@ -504,21 +582,25 @@ static void stmvl53l0_setupAPIFunctions(struct stmvl53l0_data *data) VL53L0_PerformSingleRangingMeasurement; papi_func_tbl->PerformSingleHistogramMeasurement = VL53L0_PerformSingleHistogramMeasurement; - papi_func_tbl->SetNumberOfROIZones = VL53L0_SetNumberOfROIZones; - papi_func_tbl->GetNumberOfROIZones = VL53L0_GetNumberOfROIZones; + papi_func_tbl->SetNumberOfROIZones = + VL53L0_SetNumberOfROIZones; + papi_func_tbl->GetNumberOfROIZones = + VL53L0_GetNumberOfROIZones; papi_func_tbl->GetMaxNumberOfROIZones = VL53L0_GetMaxNumberOfROIZones; - papi_func_tbl->SetGpioConfig = VL53L0_SetGpioConfig; - papi_func_tbl->GetGpioConfig = VL53L0_GetGpioConfig; + papi_func_tbl->SetGpioConfig = + VL53L0_SetGpioConfig; + papi_func_tbl->GetGpioConfig = + VL53L0_GetGpioConfig; papi_func_tbl->SetInterruptThresholds = VL53L0_SetInterruptThresholds; papi_func_tbl->GetInterruptThresholds = VL53L0_GetInterruptThresholds; - papi_func_tbl->ClearInterruptMask = VL53L0_ClearInterruptMask; + papi_func_tbl->ClearInterruptMask = + VL53L0_ClearInterruptMask; papi_func_tbl->GetInterruptMaskStatus = VL53L0_GetInterruptMaskStatus; - papi_func_tbl->EnableInterruptMask = - VL53L0_EnableInterruptMask; + papi_func_tbl->EnableInterruptMask = VL53L0_EnableInterruptMask; papi_func_tbl->SetSpadAmbientDamperThreshold = VL53L0_SetSpadAmbientDamperThreshold; papi_func_tbl->GetSpadAmbientDamperThreshold = @@ -529,335 +611,26 @@ static void stmvl53l0_setupAPIFunctions(struct stmvl53l0_data *data) VL53L0_GetSpadAmbientDamperFactor; papi_func_tbl->PerformRefSpadManagement = VL53L0_PerformRefSpadManagement; - + papi_func_tbl->SetReferenceSpads = VL53L0_SetReferenceSpads; + papi_func_tbl->GetReferenceSpads = VL53L0_GetReferenceSpads; } else if (revision == 0) { /*cut 1.0*/ - vl53l0_errmsg("to setup API cut 1.0\n"); - papi_func_tbl->GetVersion = VL53L010_GetVersion; - papi_func_tbl->GetPalSpecVersion = VL53L010_GetPalSpecVersion; - papi_func_tbl->GetDeviceInfo = VL53L010_GetDeviceInfo; - papi_func_tbl->GetDeviceErrorStatus = - VL53L010_GetDeviceErrorStatus; - papi_func_tbl->GetDeviceErrorString = - VL53L010_GetDeviceErrorString; - papi_func_tbl->GetPalErrorString = VL53L010_GetPalErrorString; - papi_func_tbl->GetPalState = VL53L010_GetPalState; - papi_func_tbl->SetPowerMode = VL53L010_SetPowerMode; - papi_func_tbl->GetPowerMode = VL53L010_GetPowerMode; - papi_func_tbl->SetOffsetCalibrationDataMicroMeter = - VL53L010_SetOffsetCalibrationDataMicroMeter; - papi_func_tbl->GetOffsetCalibrationDataMicroMeter = - VL53L010_GetOffsetCalibrationDataMicroMeter; - papi_func_tbl->SetGroupParamHold = VL53L010_SetGroupParamHold; - papi_func_tbl->GetUpperLimitMilliMeter = - VL53L010_GetUpperLimitMilliMeter; - papi_func_tbl->SetDeviceAddress = VL53L010_SetDeviceAddress; - papi_func_tbl->DataInit = VL53L010_DataInit; - /* - papi_func_tbl->SetTuningSettingBuffer = NULL; - papi_func_tbl->GetTuningSettingBuffer = NULL; - */ - papi_func_tbl->StaticInit = VL53L010_StaticInit; - papi_func_tbl->WaitDeviceBooted = VL53L010_WaitDeviceBooted; - papi_func_tbl->ResetDevice = VL53L010_ResetDevice; - papi_func_tbl->SetDeviceParameters = - VL53L010_SetDeviceParameters; - papi_func_tbl->SetDeviceMode = VL53L010_SetDeviceMode; - papi_func_tbl->GetDeviceMode = VL53L010_GetDeviceMode; - papi_func_tbl->SetHistogramMode = VL53L010_SetHistogramMode; - papi_func_tbl->GetHistogramMode = VL53L010_GetHistogramMode; - papi_func_tbl->SetMeasurementTimingBudgetMicroSeconds = - VL53L010_SetMeasurementTimingBudgetMicroSeconds; - papi_func_tbl->GetMeasurementTimingBudgetMicroSeconds = - VL53L010_GetMeasurementTimingBudgetMicroSeconds; - /* - papi_func_tbl->GetVcselPulsePeriod = NULL; - papi_func_tbl->SetVcselPulsePeriod = NULL; - papi_func_tbl->SetSequenceStepEnable = NULL; - papi_func_tbl->GetSequenceStepEnable = NULL; - papi_func_tbl->GetSequenceStepEnables = NULL; - papi_func_tbl->SetSequenceStepTimeout = NULL; - papi_func_tbl->GetSequenceStepTimeout = NULL; - papi_func_tbl->GetNumberOfSequenceSteps =NULL; - papi_func_tbl->GetSequenceStepsInfo = NULL; - */ - papi_func_tbl->SetInterMeasurementPeriodMilliSeconds = - VL53L010_SetInterMeasurementPeriodMilliSeconds; - papi_func_tbl->GetInterMeasurementPeriodMilliSeconds = - VL53L010_GetInterMeasurementPeriodMilliSeconds; - papi_func_tbl->SetXTalkCompensationEnable = - VL53L010_SetXTalkCompensationEnable; - papi_func_tbl->GetXTalkCompensationEnable = - VL53L010_GetXTalkCompensationEnable; - papi_func_tbl->SetXTalkCompensationRateMegaCps = - VL53L010_SetXTalkCompensationRateMegaCps; - papi_func_tbl->GetXTalkCompensationRateMegaCps = - VL53L010_GetXTalkCompensationRateMegaCps; - papi_func_tbl->GetNumberOfLimitCheck = - VL53L010_GetNumberOfLimitCheck; - papi_func_tbl->GetLimitCheckInfo = VL53L010_GetLimitCheckInfo; - papi_func_tbl->SetLimitCheckEnable = - VL53L010_SetLimitCheckEnable; - papi_func_tbl->GetLimitCheckEnable = - VL53L010_GetLimitCheckEnable; - papi_func_tbl->SetLimitCheckValue = VL53L010_SetLimitCheckValue; - papi_func_tbl->GetLimitCheckValue = VL53L010_GetLimitCheckValue; - papi_func_tbl->GetLimitCheckCurrent = - VL53L010_GetLimitCheckCurrent; - papi_func_tbl->SetWrapAroundCheckEnable = - VL53L010_SetWrapAroundCheckEnable; - papi_func_tbl->GetWrapAroundCheckEnable = - VL53L010_GetWrapAroundCheckEnable; - papi_func_tbl->PerformSingleMeasurement = - VL53L010_PerformSingleMeasurement; - papi_func_tbl->PerformXTalkCalibration = - VL53L010_PerformXTalkCalibration; - papi_func_tbl->PerformOffsetCalibration = - VL53L010_PerformOffsetCalibration; - papi_func_tbl->StartMeasurement = VL53L010_StartMeasurement; - papi_func_tbl->StopMeasurement = VL53L010_StopMeasurement; - papi_func_tbl->GetMeasurementDataReady = - VL53L010_GetMeasurementDataReady; - papi_func_tbl->WaitDeviceReadyForNewMeasurement = - VL53L010_WaitDeviceReadyForNewMeasurement; - papi_func_tbl->GetRangingMeasurementData = - VL53L010_GetRangingMeasurementData; - papi_func_tbl->GetHistogramMeasurementData = - VL53L010_GetHistogramMeasurementData; - papi_func_tbl->PerformSingleRangingMeasurement = - VL53L010_PerformSingleRangingMeasurement; - papi_func_tbl->PerformSingleHistogramMeasurement = - VL53L010_PerformSingleHistogramMeasurement; - papi_func_tbl->SetNumberOfROIZones = - VL53L010_SetNumberOfROIZones; - papi_func_tbl->GetNumberOfROIZones = - VL53L010_GetNumberOfROIZones; - papi_func_tbl->GetMaxNumberOfROIZones = - VL53L010_GetMaxNumberOfROIZones; - papi_func_tbl->SetGpioConfig = VL53L010_SetGpioConfig; - papi_func_tbl->GetGpioConfig = VL53L010_GetGpioConfig; - papi_func_tbl->SetInterruptThresholds = - VL53L010_SetInterruptThresholds; - papi_func_tbl->GetInterruptThresholds = - VL53L010_GetInterruptThresholds; - papi_func_tbl->ClearInterruptMask = - VL53L010_ClearInterruptMask; - papi_func_tbl->GetInterruptMaskStatus = - VL53L010_GetInterruptMaskStatus; - papi_func_tbl->EnableInterruptMask = - VL53L010_EnableInterruptMask; - papi_func_tbl->SetSpadAmbientDamperThreshold = - VL53L010_SetSpadAmbientDamperThreshold; - papi_func_tbl->GetSpadAmbientDamperThreshold = - VL53L010_GetSpadAmbientDamperThreshold; - papi_func_tbl->SetSpadAmbientDamperFactor = - VL53L010_SetSpadAmbientDamperFactor; - papi_func_tbl->GetSpadAmbientDamperFactor = - VL53L010_GetSpadAmbientDamperFactor; + vl53l0_errmsg("API cut 1.0 NOT SUPPORTED\n"); } -} -/* - * IOCTL definitions - */ -#define VL53L0_IOCTL_INIT _IO('p', 0x01) -#define VL53L0_IOCTL_INIT_NS _IO('p', 0x11) -#define VL53L0_IOCTL_INIT_NB _IO('p', 0x90) -#define VL53L0_IOCTL_SUSPEND _IO('p', 0x93) -#define VL53L0_IOCTL_RESUME _IO('p', 0x94) -#define VL53L0_IOCTL_RESET _IO('p', 0x95) -#define VL53L0_IOCTL_RESET_NB _IO('p', 0x96) -#define VL53L0_IOCTL_XTALKCALB _IOWR('p', 0x02, unsigned int) -#define VL53L0_IOCTL_OFFCALB _IOW('p', 0x03, unsigned int) -#define VL53L0_IOCTL_STOPCALB _IO('p', 0x04) -#define VL53L0_IOCTL_STOP _IO('p', 0x05) -#define VL53L0_IOCTL_SETXTALK _IOW('p', 0x06, unsigned int) -#define VL53L0_IOCTL_SETOFFSET _IOW('p', 0x07, int8_t) -#define VL53L0_IOCTL_GETDATAS \ - _IOR('p', 0x0b, VL53L0_RangingMeasurementData_t) -#define VL53L0_IOCTL_REGISTER \ - _IOWR('p', 0x0c, struct stmvl53l0_register) -#define VL53L0_IOCTL_PARAMETER \ - _IOWR('p', 0x0d, struct stmvl53l0_parameter) -#define VL53L0_IOCTL_GETDATAS_BLOCK \ - _IOR('p', 0x0e, VL53L0_RangingMeasurementData_t) - -#define VL53L0_FAST_HISTOGRAMMODE_REFERENCE ((VL53L0_HistogramTest) 1) -#define VL53L0_FAST_HISTOGRAMMODE_RETURN ((VL53L0_HistogramTest) 2) -#define VL53L0_FAST_HISTOGRAMMODE_AMBIENT ((VL53L0_HistogramTest) 3) - - -static long stmvl53l0_ioctl(struct file *file, - unsigned int cmd, - unsigned long arg); -static int stmvl53l0_flush(struct file *file, fl_owner_t id); -static int stmvl53l0_open(struct inode *inode, struct file *file); -static int stmvl53l0_close(struct inode *inode, struct file *file); -static int stmvl53l0_init_client(struct stmvl53l0_data *data); -static int stmvl53l0_start(struct stmvl53l0_data *data); -static int stmvl53l0_stop(struct stmvl53l0_data *data); - -void stmvl53l0_dumpreg(struct stmvl53l0_data *data) -{ - int i = 0; - uint8_t reg; - - if (data->enableDebug) { - for (i = 1; i <= 0xff; i++) { - VL53L0_RdByte(data, i, ®); - pr_err("STM VL53L0 reg= %x. value:%x.\n", i, reg); - } - } - -} - -void stmvl53l0_apply_parameters(struct stmvl53l0_data *vl53l0_dev) -{ - int i; - struct stmvl53l0_parameter parameter; - - for (i = 0; i < s_nump; i++) { - parameter = s_parameters[i]; - switch (parameter.name) { - case (OFFSET_PAR): - if (parameter.is_read) - parameter.status = - papi_func_tbl->GetOffsetCalibrationDataMicroMeter( - vl53l0_dev, ¶meter.value); - else - parameter.status = - papi_func_tbl->SetOffsetCalibrationDataMicroMeter( - vl53l0_dev, parameter.value); - break; - case (XTALKRATE_PAR): - if (parameter.is_read) - parameter.status = - VL53L0_GetXTalkCompensationRateMegaCps( - vl53l0_dev, (FixPoint1616_t *)¶meter.value); - else - parameter.status = - papi_func_tbl->SetXTalkCompensationRateMegaCps( - vl53l0_dev, (FixPoint1616_t)parameter.value); - break; - case (XTALKENABLE_PAR): - if (parameter.is_read) - parameter.status = - papi_func_tbl->GetXTalkCompensationEnable( - vl53l0_dev, (uint8_t *)¶meter.value); - else - parameter.status = - papi_func_tbl->SetXTalkCompensationEnable( - vl53l0_dev, (uint8_t)parameter.value); - break; - case (SIGMAVAL_PRA): - if (!parameter.is_read) - parameter.status = papi_func_tbl-> - SetLimitCheckValue(vl53l0_dev, - VL53L0_CHECKENABLE_SIGMA_FINAL_RANGE, - (FixPoint1616_t)parameter.value); - else - parameter.status = papi_func_tbl-> - GetLimitCheckValue(vl53l0_dev, - VL53L0_CHECKENABLE_SIGMA_FINAL_RANGE, - (FixPoint1616_t *)¶meter.value); - break; - - case (SIGMACTL_PRA): - if (!parameter.is_read) - parameter.status = - papi_func_tbl->SetLimitCheckEnable(vl53l0_dev, - VL53L0_CHECKENABLE_SIGMA_FINAL_RANGE, - (uint8_t)parameter.value); - else - parameter.status = - papi_func_tbl->GetLimitCheckEnable(vl53l0_dev, - VL53L0_CHECKENABLE_SIGMA_FINAL_RANGE, - (uint8_t *)¶meter.value); - break; - case (SGLVAL_PRA): - if (!parameter.is_read) - parameter.status = papi_func_tbl-> - SetLimitCheckValue(vl53l0_dev, - VL53L0_CHECKENABLE_SIGNAL_RATE_FINAL_RANGE, - (FixPoint1616_t)parameter.value); - else - parameter.status = papi_func_tbl-> - GetLimitCheckValue(vl53l0_dev, - VL53L0_CHECKENABLE_SIGNAL_RATE_FINAL_RANGE, - (FixPoint1616_t *)¶meter.value); - break; - - case (SGLCTL_PRA): - if (!parameter.is_read) - parameter.status = - papi_func_tbl->SetLimitCheckEnable(vl53l0_dev, - VL53L0_CHECKENABLE_SIGNAL_RATE_FINAL_RANGE, - (uint8_t)parameter.value); - else - parameter.status = - papi_func_tbl->GetLimitCheckEnable(vl53l0_dev, - VL53L0_CHECKENABLE_SIGNAL_RATE_FINAL_RANGE, - (uint8_t *)¶meter.value); - break; - - case (WRAPAROUNDCTL_PRA): - if (parameter.is_read) - parameter.status = - VL53L0_GetWrapAroundCheckEnable( - vl53l0_dev, (uint8_t *)¶meter.value); - else - parameter.status = - VL53L0_SetWrapAroundCheckEnable( - vl53l0_dev, (uint8_t)parameter.value); - break; - - case (MEASUREMENTTIMINGBUDGET_PAR): - if (parameter.is_read) - parameter.status = - VL53L0_GetMeasurementTimingBudgetMicroSeconds( - vl53l0_dev, (uint32_t *)¶meter.value); - else - parameter.status = - VL53L0_SetMeasurementTimingBudgetMicroSeconds( - vl53l0_dev, (uint32_t)parameter.value); - break; - case (PLLPRE_PRA): - if (parameter.is_read) - parameter.status = - papi_func_tbl->GetVcselPulsePeriod(vl53l0_dev, - VL53L0_VCSEL_PERIOD_PRE_RANGE, (uint8_t *)¶meter.value); - else - parameter.status = - papi_func_tbl->SetVcselPulsePeriod(vl53l0_dev, - VL53L0_VCSEL_PERIOD_PRE_RANGE, (uint8_t)parameter.value); - break; - case (PLLFINAL_PRA): - if (parameter.is_read) - parameter.status = - papi_func_tbl->GetVcselPulsePeriod(vl53l0_dev, - VL53L0_VCSEL_PERIOD_FINAL_RANGE, (uint8_t *)¶meter.value); - else - parameter.status = - papi_func_tbl->SetVcselPulsePeriod(vl53l0_dev, - VL53L0_VCSEL_PERIOD_FINAL_RANGE, (uint8_t)parameter.value); - break; - } - - } - s_nump = 0; + return revision; } static void stmvl53l0_ps_read_measurement(struct stmvl53l0_data *data) { struct timeval tv; + VL53L0_DEV vl53l0_dev = data; + VL53L0_Error Status = VL53L0_ERROR_NONE; + FixPoint1616_t LimitCheckCurrent; do_gettimeofday(&tv); - data->ps_data = 100; - if (data->rangeData.RangeMilliMeter < data->highv) - data->ps_data = 10; - if (data->gpio_function == VL53L0_GPIOFUNCTIONALITY_THRESHOLD_CROSSED_LOW) - data->ps_data = 10; + data->ps_data = data->rangeData.RangeMilliMeter; input_report_abs(data->input_dev_ps, ABS_DISTANCE, (int)(data->ps_data + 5) / 10); input_report_abs(data->input_dev_ps, ABS_HAT0X, tv.tv_sec); @@ -874,318 +647,28 @@ static void stmvl53l0_ps_read_measurement(struct stmvl53l0_data *data) data->rangeData.MeasurementTimeUsec); input_report_abs(data->input_dev_ps, ABS_HAT3Y, data->rangeData.RangeDMaxMilliMeter); + Status = papi_func_tbl->GetLimitCheckCurrent(vl53l0_dev, + VL53L0_CHECKENABLE_SIGMA_FINAL_RANGE, + &LimitCheckCurrent); + if (Status == VL53L0_ERROR_NONE) { + input_report_abs(data->input_dev_ps, ABS_WHEEL, + LimitCheckCurrent); + } + input_report_abs(data->input_dev_ps, ABS_MISC, + data->rangeData.EffectiveSpadRtnCount); input_sync(data->input_dev_ps); if (data->enableDebug) - vl53l0_errmsg("range:%d, signalRateRtnMegaCps:%d, \ - error:0x%x,rtnambrate:%u,DMax:%d,measuretime:%u\n", - data->rangeData.RangeMilliMeter, - data->rangeData.SignalRateRtnMegaCps, - data->rangeData.RangeStatus, - data->rangeData.AmbientRateRtnMegaCps, - data->rangeData.RangeDMaxMilliMeter, - data->rangeData.MeasurementTimeUsec); - -} - -static void stmvl53l0_enter_off(struct stmvl53l0_data *data, uint8_t from) -{ - vl53l0_dbgmsg_en("Enter, stmvl53l0_enter_off from:%d\n", from); - if (data->enable_ps_sensor == 0) - stmvl53l0_start(data); - papi_func_tbl->StopMeasurement(data); - papi_func_tbl->ClearInterruptMask(data, 0); - papi_func_tbl->SetDeviceMode(data, - VL53L0_DEVICEMODE_SINGLE_RANGING); - papi_func_tbl->SetGpioConfig(data, 0, 0, - VL53L0_GPIOFUNCTIONALITY_NEW_MEASURE_READY, - VL53L0_INTERRUPTPOLARITY_LOW); - data->gpio_function = VL53L0_GPIOFUNCTIONALITY_NEW_MEASURE_READY; - - vl53l0_dbgmsg_en("End\n"); -} - -static void stmvl53l0_enter_cam(struct stmvl53l0_data *data, uint8_t from) -{ - - vl53l0_errmsg("Enter, stmvl53l0_enter_cam from:%d\n", from); - /* turn on tof sensor */ - if (data->enable_ps_sensor == 0) - stmvl53l0_start(data); - papi_func_tbl->SetVcselPulsePeriod(data, - VL53L0_VCSEL_PERIOD_PRE_RANGE, data->pll_p); - papi_func_tbl->SetVcselPulsePeriod(data, - VL53L0_VCSEL_PERIOD_FINAL_RANGE, data->pll_f); - - vl53l0_errmsg("Call of setlimt \n"); - papi_func_tbl->SetLimitCheckEnable(data, - VL53L0_CHECKENABLE_SIGNAL_RATE_FINAL_RANGE, - (uint8_t)0); - - papi_func_tbl->SetLimitCheckEnable(data, - VL53L0_CHECKENABLE_SIGMA_FINAL_RANGE, - (uint8_t)0); - vl53l0_errmsg("Call ofSetMeasurementTimingBudgetMicroSeconds \n"); - papi_func_tbl->SetMeasurementTimingBudgetMicroSeconds( - data, 26000); - - papi_func_tbl->SetDeviceMode(data, - VL53L0_DEVICEMODE_SINGLE_RANGING); - - vl53l0_errmsg("Call of VL53L0_DEVICEMODE_SINGLE_RANGING\n"); - papi_func_tbl->SetGpioConfig(data, 0, 0, - VL53L0_GPIOFUNCTIONALITY_NEW_MEASURE_READY, - VL53L0_INTERRUPTPOLARITY_LOW); - data->gpio_function = VL53L0_GPIOFUNCTIONALITY_NEW_MEASURE_READY; - - papi_func_tbl->StartMeasurement(data); - - vl53l0_errmsg("End\n"); -} - -static void stmvl53l0_enter_sar(struct stmvl53l0_data *data, uint8_t from) -{ - VL53L0_RangingMeasurementData_t RMData; - vl53l0_errmsg("Enter, from:%d\n", from); - /* turn on tof sensor */ - if (data->enable_ps_sensor == 0) - stmvl53l0_start(data); - - memset(&RMData, 0, sizeof(RMData)); - - papi_func_tbl->SetDeviceMode(data, - VL53L0_DEVICEMODE_SINGLE_RANGING); - - papi_func_tbl->SetVcselPulsePeriod(data, - VL53L0_VCSEL_PERIOD_PRE_RANGE, 14); - - papi_func_tbl->SetVcselPulsePeriod(data, - VL53L0_VCSEL_PERIOD_FINAL_RANGE, 10); - - papi_func_tbl->SetLimitCheckEnable(data, - VL53L0_CHECKENABLE_SIGNAL_RATE_FINAL_RANGE, - (uint8_t)1); - - papi_func_tbl->SetLimitCheckEnable(data, - VL53L0_CHECKENABLE_SIGMA_FINAL_RANGE, - (uint8_t)0); - - papi_func_tbl->SetMeasurementTimingBudgetMicroSeconds( - data, 33000); - - papi_func_tbl->PerformSingleRangingMeasurement(data, - &RMData); - memcpy(&(data->rangeData), &RMData, - sizeof(VL53L0_RangingMeasurementData_t)); - stmvl53l0_ps_read_measurement(data); - - papi_func_tbl->SetGpioConfig(data, 0, 0, - VL53L0_GPIOFUNCTIONALITY_THRESHOLD_CROSSED_LOW, - VL53L0_INTERRUPTPOLARITY_LOW); - data->gpio_function = VL53L0_GPIOFUNCTIONALITY_THRESHOLD_CROSSED_LOW; - papi_func_tbl->SetInterMeasurementPeriodMilliSeconds( - data, 5000); - papi_func_tbl->SetMeasurementTimingBudgetMicroSeconds( - data, 33000); - papi_func_tbl->SetInterruptThresholds( - data, 0, data->lowv << 16, data->highv << 16); - papi_func_tbl->SetDeviceMode(data, - VL53L0_DEVICEMODE_CONTINUOUS_TIMED_RANGING); - data->lowint = 1; - papi_func_tbl->ClearInterruptMask(data, 0); - papi_func_tbl->StartMeasurement(data); - vl53l0_errmsg("End\n"); -} -static void stmvl53l0_enter_super(struct stmvl53l0_data *data, uint8_t from) -{ - vl53l0_errmsg("Enter, stmvl53l0_enter_super flag:%d\n", from); - if (from == SAR_MODE || from == SUPER_MODE) { - vl53l0_dbgmsg_en("Call of VL53L0_DEVICEMODE_CONTINUOUS_RANGING\n"); - papi_func_tbl->ClearInterruptMask(data, 0); - - papi_func_tbl->SetVcselPulsePeriod(data, - VL53L0_VCSEL_PERIOD_PRE_RANGE, 18); - - papi_func_tbl->SetVcselPulsePeriod(data, - VL53L0_VCSEL_PERIOD_FINAL_RANGE, 14); - - papi_func_tbl->SetLimitCheckEnable(data, - VL53L0_CHECKENABLE_SIGNAL_RATE_FINAL_RANGE, - (uint8_t)0); - - papi_func_tbl->SetLimitCheckEnable(data, - VL53L0_CHECKENABLE_SIGMA_FINAL_RANGE, - (uint8_t)0); - - papi_func_tbl->SetMeasurementTimingBudgetMicroSeconds( - data, 26000); - - papi_func_tbl->SetDeviceMode(data, - VL53L0_DEVICEMODE_SINGLE_RANGING); - - vl53l0_errmsg("Call of VL53L0_DEVICEMODE_SINGLE_RANGING\n"); - papi_func_tbl->SetGpioConfig(data, 0, 0, - VL53L0_GPIOFUNCTIONALITY_NEW_MEASURE_READY, - VL53L0_INTERRUPTPOLARITY_LOW); - data->gpio_function = VL53L0_GPIOFUNCTIONALITY_NEW_MEASURE_READY; - - papi_func_tbl->StartMeasurement(data); - - vl53l0_errmsg("End\n"); - } - - vl53l0_dbgmsg_en("End\n"); -} -static void stmvl53l0_enter_xtalkcal(struct stmvl53l0_data *data, - uint8_t from) -{ - FixPoint1616_t XTalkCompensationRateMegaCps; - - vl53l0_dbgmsg_en("Enter, stmvl53l0_enter_cal flag:%d\n", from); - if (data->enable_ps_sensor == 0) - stmvl53l0_start(data); - - papi_func_tbl->SetXTalkCompensationEnable(data, 0); - - papi_func_tbl->PerformXTalkCalibration(data, - (data->xtalkCalDistance << 16), &XTalkCompensationRateMegaCps); - data->xtalkcalval = XTalkCompensationRateMegaCps; - vl53l0_dbgmsg_en("End\n"); -} -static void stmvl53l0_enter_offsetcal(struct stmvl53l0_data *data, - uint8_t from) -{ - FixPoint1616_t OffsetMicroMeter; - - vl53l0_dbgmsg_en("Enter, stmvl53l0_enter_cal flag:%d\n", from); - papi_func_tbl->PerformOffsetCalibration(data, - (data->offsetCalDistance<<16), - &OffsetMicroMeter); - pr_err("Offset calibration:%u\n", OffsetMicroMeter); - vl53l0_dbgmsg_en("End\n"); -} -static void stmvl53l0_work_state( -struct stmvl53l0_data *data, uint8_t input) -{ - int nochange = 0; - uint8_t from = data->w_mode; - - vl53l0_dbgmsg_en("Enter, stmvl53l0_work_state:%d\n", input); - mutex_lock(&data->work_mutex); - switch (data->w_mode) { - case OFF_MODE: - if (input == CAM_ON) - data->w_mode = CAM_MODE; - else if (input == SAR_ON) - data->w_mode = SAR_MODE; - else if (input == XTALKCAL_ON) - data->w_mode = XTALKCAL_MODE; - else if (input == OFFSETCAL_ON) - data->w_mode = OFFSETCAL_MODE; - else { - vl53l0_dbgmsg_en("unsopport status = %d,cs = OFF_MODE", - input); - nochange = 1; - } - break; - case CAM_MODE: - if (input == CAM_OFF) - data->w_mode = OFF_MODE; - else if (input == SAR_ON) - data->w_mode = SUPER_MODE; - else { - vl53l0_dbgmsg_en("unsopport status = %d,cs = CAM_MODE", input); - nochange = 1; - } - break; - case SAR_MODE: - if (input == CAM_ON) - data->w_mode = SUPER_MODE; - else if (input == SAR_OFF) - data->w_mode = OFF_MODE; - else { - vl53l0_dbgmsg_en("unsopport status = %d,cs = SAR_MODE", input); - nochange = 1; - } - break; - case SUPER_MODE: - if (input == CAM_OFF) { - stmvl53l0_stopmeasurement(data); - data->w_mode = SAR_MODE; - } else if (input == SAR_OFF) { - stmvl53l0_stopmeasurement(data); - data->w_mode = CAM_MODE; - } else { - vl53l0_dbgmsg_en("unsopport status= %d,cs = SUPER_MODE", input); - nochange = 1; - } - break; - case XTALKCAL_MODE: - if (input == CAL_OFF) - data->w_mode = OFF_MODE; - else { - vl53l0_dbgmsg_en("unsopport status = %d,cs= SUPER_MODE", input); - data->w_mode = OFF_MODE; - } - break; - case OFFSETCAL_MODE: - if (input == CAL_OFF) - data->w_mode = OFF_MODE; - else { - vl53l0_dbgmsg_en("unsopport status= %d,cs = SUPER_MODE", input); - data->w_mode = OFF_MODE; - } - break; - default: - vl53l0_dbgmsg_en("unsopport status= %d,cs = unknown", input); - nochange = 1; - break; - } - if (input == CAM_OFF && data->c_suspend == 1) - data->c_suspend = 0; - - if (nochange && input != RESET) { - vl53l0_dbgmsg_en("unsopport status= %d,cs = %d", - input, data->w_mode); - mutex_unlock(&data->work_mutex); - return; - } - data->c_stopped = 1; - if (input == RESET) { - stmvl53l0_stop(data); - data->reset = 0; - if (data->enable_ps_sensor == 0) - stmvl53l0_start(data); - } - - cancel_delayed_work(&data->dwork); - switch (data->w_mode) { - case OFF_MODE: - stmvl53l0_stopmeasurement(data); - stmvl53l0_enter_off(data, from); - break; - case CAM_MODE: - stmvl53l0_enter_cam(data, from); - break; - case SAR_MODE: - stmvl53l0_enter_sar(data, from); - break; - case SUPER_MODE: - stmvl53l0_stopmeasurement(data); - stmvl53l0_enter_super(data, from); - break; - case XTALKCAL_MODE: - stmvl53l0_enter_xtalkcal(data, from); - break; - case OFFSETCAL_MODE: - stmvl53l0_enter_offsetcal(data, from); - break; - default: - pr_err("status unknown, input = %d", input); - break; - } - data->c_stopped = 0; - mutex_unlock(&data->work_mutex); + vl53l0_errmsg("range:%d, RtnRateMcps:%d, \ + err:0x%x,Dmax:%d,rtnambr:%d,time:%d,Spad:%d,SigmaLimit:%d\n", + data->rangeData.RangeMilliMeter, + data->rangeData.SignalRateRtnMegaCps, + data->rangeData.RangeStatus, + data->rangeData.RangeDMaxMilliMeter, + data->rangeData.AmbientRateRtnMegaCps, + data->rangeData.MeasurementTimeUsec, + data->rangeData.EffectiveSpadRtnCount, + LimitCheckCurrent); } static void stmvl53l0_cancel_handler(struct stmvl53l0_data *data) @@ -1200,219 +683,235 @@ static void stmvl53l0_cancel_handler(struct stmvl53l0_data *data) */ ret = cancel_delayed_work(&data->dwork); if (ret == 0) - vl53l0_errmsg("%d,cancel_delayed_work return FALSE\n", - __LINE__); - ret = cancel_delayed_work(&data->initwork); - if (ret == 0) - vl53l0_errmsg("%d,cancel_delayed_work return FALSE\n", - __LINE__); - ret = cancel_delayed_work(&data->resetwork); - if (ret == 0) - vl53l0_errmsg("%d,cancel_delayed_work return FALSE\n", - __LINE__); - ret = cancel_delayed_work(&data->checkwork); - if (ret == 0) - vl53l0_errmsg("%d,cancel_delayEd_work return FALSE\n", - __LINE__); + vl53l0_errmsg("cancel_delayed_work return FALSE\n"); + spin_unlock_irqrestore(&data->update_lock.wait_lock, flags); - return; } -static void stmvl53l0_state_process(void) +void stmvl53l0_schedule_handler(struct stmvl53l0_data *data) { -VL53L0_RangingMeasurementData_t RMData; -struct stmvl53l0_data *data = gp_vl53l0_data; -VL53L0_DEV vl53l0_dev = data; -struct timeval tv; -int newv; + unsigned long flags; -do_gettimeofday(&tv); -memset(&RMData, 0, sizeof(RMData)); -papi_func_tbl->GetRangingMeasurementData(vl53l0_dev, &RMData); + spin_lock_irqsave(&data->update_lock.wait_lock, flags); + /* + * If work is already scheduled then subsequent schedules will not + * change the scheduled time that's why we have to cancel it first. + */ + cancel_delayed_work(&data->dwork); + schedule_delayed_work(&data->dwork, 0); + spin_unlock_irqrestore(&data->update_lock.wait_lock, flags); -vl53l0_dbgmsg_en("which MODE =%d\n", data->w_mode); -papi_func_tbl->ClearInterruptMask(vl53l0_dev, 0); -memcpy(&(data->rangeData), &RMData, - sizeof(VL53L0_RangingMeasurementData_t)); -data->d_ready = 1; +} -data->rangeData.TimeStamp = tv.tv_sec; -data->rangeData.MeasurementTimeUsec = tv.tv_usec; -wake_up(&data->range_data_wait); - if (CAM_MODE == data->w_mode) { - vl53l0_dbgmsg_en("CAM_MODE\n"); - stmvl53l0_apply_parameters(data); - papi_func_tbl->StartMeasurement(data); - if (data->enableDebug) - vl53l0_errmsg("range:%d, signalRateRtnMegaCps:%d, \ - error:0x%x,rtnambrate:%u,Dmax:%d,measuretime:%u\n", - data->rangeData.RangeMilliMeter, - data->rangeData.SignalRateRtnMegaCps, - data->rangeData.RangeStatus, - data->rangeData.AmbientRateRtnMegaCps, - data->rangeData.RangeDMaxMilliMeter, - data->rangeData.MeasurementTimeUsec); - } else if (SAR_MODE == data->w_mode) { - vl53l0_dbgmsg("SAR_MODE\n"); - if (data->gpio_function == VL53L0_GPIOFUNCTIONALITY_THRESHOLD_CROSSED_LOW) { - vl53l0_dbgmsg("SAR enter LOW\n"); - if (data->lowint < 2) { - data->lowint++; - papi_func_tbl->ClearInterruptMask(vl53l0_dev, 0); - return; - } +#ifdef USE_INT +static irqreturn_t stmvl53l0_interrupt_handler(int vec, void *info) +{ - papi_func_tbl->ClearInterruptMask(vl53l0_dev, 0); - stmvl53l0_ps_read_measurement(vl53l0_dev); - papi_func_tbl->SetGpioConfig(data, 0, 0, - VL53L0_GPIOFUNCTIONALITY_THRESHOLD_CROSSED_HIGH, - VL53L0_INTERRUPTPOLARITY_LOW); - data->gpio_function = VL53L0_GPIOFUNCTIONALITY_THRESHOLD_CROSSED_HIGH; - papi_func_tbl->SetInterMeasurementPeriodMilliSeconds( - data, 5000); - papi_func_tbl->SetMeasurementTimingBudgetMicroSeconds( - data, 33000); - papi_func_tbl->SetInterruptThresholds( - data, 0, data->lowv << 16, data->highv << 16); - papi_func_tbl->SetDeviceMode(data, - VL53L0_DEVICEMODE_CONTINUOUS_TIMED_RANGING); - data->lowint = 5; - papi_func_tbl->StartMeasurement(data); - vl53l0_dbgmsg("SAR enter LOW sent uevent\n"); - } else if (data->gpio_function == VL53L0_GPIOFUNCTIONALITY_THRESHOLD_CROSSED_HIGH) { - vl53l0_dbgmsg("SAR enter HIGH\n"); - if (data->lowint > 4) { - data->lowint--; - papi_func_tbl->ClearInterruptMask(vl53l0_dev, 0); - return; - } + struct stmvl53l0_data *data = (struct stmvl53l0_data *)info; - papi_func_tbl->ClearInterruptMask(vl53l0_dev, 0); - stmvl53l0_ps_read_measurement(vl53l0_dev); - papi_func_tbl->SetGpioConfig(data, 0, 0, - VL53L0_GPIOFUNCTIONALITY_THRESHOLD_CROSSED_LOW, - VL53L0_INTERRUPTPOLARITY_LOW); - data->gpio_function = VL53L0_GPIOFUNCTIONALITY_THRESHOLD_CROSSED_LOW; - papi_func_tbl->SetInterMeasurementPeriodMilliSeconds( - data, 5000); - papi_func_tbl->SetMeasurementTimingBudgetMicroSeconds( - data, 33000); - papi_func_tbl->SetInterruptThresholds( - data, 0, data->lowv << 16, data->highv << 16); - papi_func_tbl->SetDeviceMode(data, - VL53L0_DEVICEMODE_CONTINUOUS_TIMED_RANGING); - data->lowint = 1; - papi_func_tbl->StartMeasurement(data); - vl53l0_dbgmsg("SAR enter high sent uevent\n"); + if (data->irq == vec) { + data->interrupt_received = 1; + schedule_delayed_work(&data->dwork, 0); + } + return IRQ_HANDLED; +} +#else +/* Flag used to exit the thread when kthread_stop() is invoked */ +static int poll_thread_exit; +int stmvl53l0_poll_thread(void *data) +{ + VL53L0_DEV vl53l0_dev = data; + VL53L0_Error Status = VL53L0_ERROR_NONE; + uint32_t sleep_time = 0; + uint32_t interruptStatus = 0; + + pr_err("%s(%d) : Starting Polling thread\n", __func__, __LINE__); + + while (!kthread_should_stop()) { + /* Check if enable_ps_sensor is true or exit request is made. + * If not block + */ + wait_event(vl53l0_dev->poll_thread_wq, + (vl53l0_dev->enable_ps_sensor || poll_thread_exit)); + if (poll_thread_exit) { + pr_err("%s(%d) : Exiting the poll thread\n", __func__, __LINE__); + break; } - vl53l0_dbgmsg_en("SAR measurement ready\n"); - } else if (SUPER_MODE == data->w_mode) { - vl53l0_dbgmsg_en("SUPER_MODE\n"); - stmvl53l0_apply_parameters(data); - newv = data->rangeData.RangeMilliMeter > data->highv ? 100:10; - if (s_prevalue != newv || s_count > 100) - stmvl53l0_ps_read_measurement(vl53l0_dev); - if (s_count > 100) - s_count = 0; - s_count++; - s_prevalue = newv; - papi_func_tbl->StartMeasurement(data); + mutex_lock(&vl53l0_dev->work_mutex); + + sleep_time = vl53l0_dev->delay_ms; + Status = VL53L0_GetInterruptMaskStatus(vl53l0_dev, + &interruptStatus); + if (Status == VL53L0_ERROR_NONE && + interruptStatus && + interruptStatus != vl53l0_dev->interruptStatus) { + vl53l0_dev->interruptStatus = interruptStatus; + vl53l0_dev->noInterruptCount = 0; + stmvl53l0_schedule_handler(vl53l0_dev); + } else { + vl53l0_dev->noInterruptCount++; + } + + /*Force Clear interrupt mask and restart if + *no interrupt after twice the timingBudget + */ + if ((vl53l0_dev->noInterruptCount * vl53l0_dev->delay_ms) > + (vl53l0_dev->timingBudget * 2)) { + pr_err("No interrupt after (%u) msec(TimingBudget = %u). Clear Interrupt Mask and restart\n", + (vl53l0_dev->noInterruptCount * vl53l0_dev->delay_ms), + vl53l0_dev->timingBudget); + Status = papi_func_tbl->ClearInterruptMask(vl53l0_dev,0); + if (vl53l0_dev->deviceMode == + VL53L0_DEVICEMODE_SINGLE_RANGING) { + Status = papi_func_tbl->StartMeasurement( + vl53l0_dev); + if (Status != VL53L0_ERROR_NONE) { + pr_err("%s(%d) : Status = %d\n", + __func__, __LINE__, Status); + } + } + } + mutex_unlock(&vl53l0_dev->work_mutex); + /* Sleep for delay_ms milliseconds */ + msleep(sleep_time); } + return 0; } -static void stmvl53l0_init_handler(struct work_struct *work) -{ - struct stmvl53l0_data *data = gp_vl53l0_data; +#endif - vl53l0_dbgmsg_en("get in the handler\n"); - stmvl53l0_work_state(data, CAM_ON); - return; - -} -static void stmvl53l0_reset_handler(struct work_struct *work) -{ - struct stmvl53l0_data *data = gp_vl53l0_data; - - vl53l0_dbgmsg_en("get in the handler\n"); - stmvl53l0_work_state(data, RESET); - return; - -} -static void stmvl53l0_check_handler(struct work_struct *work) -{ - struct stmvl53l0_data *data = gp_vl53l0_data; - - vl53l0_dbgmsg_en("get in the handler\n"); - stmvl53l0_livechecking(data); - schedule_delayed_work(&data->checkwork, 1800000); -} -/* interrupt work handler */ +/* work handler */ static void stmvl53l0_work_handler(struct work_struct *work) { - struct stmvl53l0_data *data = gp_vl53l0_data; + struct stmvl53l0_data *data = container_of(work, struct stmvl53l0_data, + dwork.work); VL53L0_DEV vl53l0_dev = data; - uint32_t InterruptMask; - vl53l0_dbgmsg_en("get in the handler\n"); + VL53L0_Error Status = VL53L0_ERROR_NONE; mutex_lock(&data->work_mutex); + /* vl53l0_dbgmsg("Enter\n"); */ - if (data->enable_ps_sensor == 1 && data->c_suspend == 0 && 7 != data->d_mode && data->c_stopped == 0) { - papi_func_tbl->GetInterruptMaskStatus( - vl53l0_dev, &InterruptMask); - vl53l0_dbgmsg_en(" InterruptMasksssss :%d\n", InterruptMask); - if (InterruptMask > 0 && data->interrupt_received == 1) { - data->interrupt_received = 0; - stmvl53l0_state_process(); + if (vl53l0_dev->enable_ps_sensor == 1) { +#ifdef DEBUG_TIME_LOG + stmvl53l0_DebugTimeGet(&stop_tv); + stmvl53l0_DebugTimeDuration(&start_tv, &stop_tv); +#endif + /* Check if ISR has scheduled this function */ + if (vl53l0_dev->interrupt_received == 1) { + Status = papi_func_tbl->GetInterruptMaskStatus( + vl53l0_dev, + &vl53l0_dev->interruptStatus); + if (Status != VL53L0_ERROR_NONE) + pr_err("%s(%d) : Status = %d\n", + __func__, __LINE__, Status); + vl53l0_dev->interrupt_received = 0; } - } + if (data->enableDebug) + pr_err("interruptStatus:0x%x, interrupt_received:%d\n", + vl53l0_dev->interruptStatus, + vl53l0_dev->interrupt_received); + + if (vl53l0_dev->interruptStatus == vl53l0_dev->gpio_function) { + Status = + papi_func_tbl->GetRangingMeasurementData( + vl53l0_dev, + &(data->rangeData)); + /* to push the measurement */ + if (Status == VL53L0_ERROR_NONE) + stmvl53l0_ps_read_measurement(data); + else + pr_err("%s(%d) : Status = %d\n", + __func__, __LINE__, Status); + + if (data->enableDebug) + pr_err("Measured range:%d\n", + data->rangeData.RangeMilliMeter); + + Status = papi_func_tbl->ClearInterruptMask( + vl53l0_dev, 0); + if (Status != VL53L0_ERROR_NONE) { + pr_err("%s(%d) : Status = %d\n", + __func__, __LINE__, Status); + } + + if (data->deviceMode == + VL53L0_DEVICEMODE_SINGLE_RANGING) { + /* Before restarting measurement + * check if use case needs to be changed + */ + + if (data->updateUseCase) { + Status = + stmvl53l0_config_use_case(data); + if (Status != VL53L0_ERROR_NONE) { + vl53l0_errmsg( + "Failed to configure Use case = %u\n", + vl53l0_dev->useCase); + } else { + data->updateUseCase = 0; + } + } + Status = + papi_func_tbl->StartMeasurement( + vl53l0_dev); + } + } +#ifdef DEBUG_TIME_LOG + stmvl53l0_DebugTimeGet(&start_tv); +#endif + } + + vl53l0_dev->interruptStatus = 0; mutex_unlock(&data->work_mutex); - return; } + /* * SysFS support */ static ssize_t stmvl53l0_show_enable_ps_sensor(struct device *dev, struct device_attribute *attr, char *buf) { - struct stmvl53l0_data *data = gp_vl53l0_data; + struct stmvl53l0_data *data = dev_get_drvdata(dev); - return snprintf(buf, 2, "%d\n", data->enable_ps_sensor); + return snprintf(buf, 5, "%d\n", data->enable_ps_sensor); } static ssize_t stmvl53l0_store_enable_ps_sensor(struct device *dev, - struct device_attribute *attr, const char *buf, size_t count) + struct device_attribute *attr, const char *buf, + size_t count) { - struct stmvl53l0_data *data = gp_vl53l0_data; + struct stmvl53l0_data *data = dev_get_drvdata(dev); + unsigned long val = 0; - unsigned long val; + int ret = kstrtoul(buf, 10, &val); - if (kstrtoul(buf, 10, &val)) - return count; + if (ret != 0) + return ret; if ((val != 0) && (val != 1)) { - vl53l0_errmsg("%d,store unvalid value=%ld\n", __LINE__, val); + vl53l0_errmsg("store unvalid value = %lu\n", val); return count; } mutex_lock(&data->work_mutex); - vl53l0_dbgmsg_en("Enter, enable_ps_sensor flag:%d\n", + vl53l0_errmsg("Enter, enable_ps_sensor flag:%d\n", data->enable_ps_sensor); - vl53l0_dbgmsg_en("enable ps senosr ( %ld)\n", val); + vl53l0_errmsg("enable ps senosr ( %ld)\n", val); if (val == 1) { /* turn on tof sensor */ if (data->enable_ps_sensor == 0) { /* to start */ - stmvl53l0_start(data); + stmvl53l0_start(data, 3, NORMAL_MODE); } else { - vl53l0_errmsg("%d,Already enabled. Skip !", __LINE__); + vl53l0_errmsg("Already enabled. Skip !"); } } else { /* turn off tof sensor */ @@ -1422,212 +921,38 @@ static ssize_t stmvl53l0_store_enable_ps_sensor(struct device *dev, stmvl53l0_stop(data); } } - vl53l0_dbgmsg_en("End\n"); + vl53l0_dbgmsg("End\n"); mutex_unlock(&data->work_mutex); return count; } -static DEVICE_ATTR(enable_ps_sensor, S_IWUSR | S_IWGRP | S_IRUGO, +static DEVICE_ATTR(enable_ps_sensor, 0664/*S_IWUGO | S_IRUGO*/, stmvl53l0_show_enable_ps_sensor, stmvl53l0_store_enable_ps_sensor); -static ssize_t stmvl53l0_show_set_mode(struct device *dev, -struct device_attribute *attr, char *buf) -{ - struct stmvl53l0_data *data = gp_vl53l0_data; - - return snprintf(buf, 3, "%d\n", data->d_mode); -} - -/* for debug */ -static ssize_t stmvl53l0_store_set_mode(struct device *dev, -struct device_attribute *attr, const char *buf, size_t count) -{ - struct stmvl53l0_data *data = gp_vl53l0_data; - VL53L0_RangingMeasurementData_t RMData; - long on; - VL53L0_Error Status = VL53L0_ERROR_NONE; - - if (kstrtoul(buf, 10, &on)) - return count; - if (on > 7) { - vl53l0_errmsg("%d,set d_mode=%ld\n", __LINE__, on); - return count; - } - data->d_mode = on; - /* turn on tof sensor */ - if (data->enable_ps_sensor == 0) { - /* to start */ - stmvl53l0_start(data); - } - memset(&RMData, 0, sizeof(RMData)); - if (VL53L0_DEVICEMODE_SINGLE_RANGING == data->d_mode) { - vl53l0_errmsg("Call of VL53L0_DEVICEMODE_SINGLE_RANGING\n"); - Status = papi_func_tbl->SetGpioConfig(data, 0, 0, - VL53L0_GPIOFUNCTIONALITY_NEW_MEASURE_READY, - VL53L0_INTERRUPTPOLARITY_LOW); - data->gpio_function = VL53L0_GPIOFUNCTIONALITY_NEW_MEASURE_READY; - Status = papi_func_tbl->SetDeviceMode(data, - VL53L0_DEVICEMODE_SINGLE_RANGING); - papi_func_tbl->SetMeasurementTimingBudgetMicroSeconds( - data, 50000); - Status = papi_func_tbl->PerformSingleRangingMeasurement(data, - &RMData); - stmvl53l0_ps_read_measurement(data); - } else if (1 == data->d_mode) { - vl53l0_errmsg("Call of mode 1\n"); - Status = papi_func_tbl->SetGpioConfig(data, 0, 0, - VL53L0_GPIOFUNCTIONALITY_NEW_MEASURE_READY, - VL53L0_INTERRUPTPOLARITY_LOW); - data->gpio_function = VL53L0_GPIOFUNCTIONALITY_NEW_MEASURE_READY; - Status = papi_func_tbl->SetDeviceMode(data, - VL53L0_DEVICEMODE_CONTINUOUS_RANGING); - papi_func_tbl->StartMeasurement(data); - } else if (2 == data->d_mode) { - vl53l0_errmsg("Call of mode 2\n"); - Status = papi_func_tbl->SetGpioConfig(data, 0, 0, - VL53L0_GPIOFUNCTIONALITY_THRESHOLD_CROSSED_LOW, - VL53L0_INTERRUPTPOLARITY_LOW); - data->gpio_function = VL53L0_GPIOFUNCTIONALITY_THRESHOLD_CROSSED_LOW; - Status = papi_func_tbl->SetDeviceMode(data, - VL53L0_DEVICEMODE_CONTINUOUS_RANGING); - papi_func_tbl->SetInterruptThresholds - (data, 0, 60 << 16, 80 << 16); - papi_func_tbl->StartMeasurement(data); - - } else if (3 == data->d_mode) { - vl53l0_errmsg("Call mode 3\n"); - papi_func_tbl->SetInterMeasurementPeriodMilliSeconds - (data, 0x00001388); - papi_func_tbl->SetGpioConfig(data, 0, 0, - VL53L0_GPIOFUNCTIONALITY_THRESHOLD_CROSSED_HIGH, - VL53L0_INTERRUPTPOLARITY_LOW); - data->gpio_function = VL53L0_GPIOFUNCTIONALITY_THRESHOLD_CROSSED_HIGH; - papi_func_tbl->SetInterruptThresholds - (data, 0, 30 << 16, 80 << 16); - Status = papi_func_tbl->SetDeviceMode(data, - VL53L0_DEVICEMODE_CONTINUOUS_TIMED_RANGING); - papi_func_tbl->StartMeasurement(data); - } else if (4 == data->d_mode) { - vl53l0_errmsg("Call of mode 4\n"); - Status = papi_func_tbl->SetGpioConfig(data, 0, 0, - VL53L0_GPIOFUNCTIONALITY_THRESHOLD_CROSSED_HIGH, - VL53L0_INTERRUPTPOLARITY_LOW); - data->gpio_function = VL53L0_GPIOFUNCTIONALITY_THRESHOLD_CROSSED_HIGH; - Status = papi_func_tbl->SetDeviceMode(data, - VL53L0_DEVICEMODE_CONTINUOUS_RANGING); - papi_func_tbl->SetInterruptThresholds - (data, 0, 30 << 16, 40 << 16); - papi_func_tbl->StartMeasurement(data); - } else if (5 == data->d_mode) - stmvl53l0_work_state(data, CAM_ON); - else if (6 == data->d_mode) - stmvl53l0_work_state(data, CAM_OFF); - else if (7 == data->d_mode) { - vl53l0_errmsg("call VL53L0_perform_single_histogram3\n"); - /*changes for feture*/ - /*papi_func_tbl->SetMeasurementTimingBudgetMicroSeconds( - data, 500000); - for (hdata.FirstBin = 0; hdata.FirstBin < 12; hdata.FirstBin++) - hdata.HistogramData[hdata.FirstBin] = 0; - VL53L0_perform_single_histogram(data, 1, 1, 0, &hdata); - vl53l0_errmsg("VL53L0_perform_single_histogram start_bin %d, NumberOfBins %d, ErrorStatus %d\n", hdata.FirstBin, hdata.NumberOfBins, hdata.ErrorStatus); - Start_bin = hdata.FirstBin; - vl53l0_errmsg("call VL53L0_perform_single_histogram3\n"); - VL53L0_perform_single_histogram(data, 2, 1, Start_bin, &hdata); - vl53l0_errmsg("VL53L0_perform_single_histogram start_bin %d, NumberOfBins %d, ErrorStatus %d\n", hdata.FirstBin, hdata.NumberOfBins, hdata.ErrorStatus); - vl53l0_errmsg("VL53L0_perform_single_histogram 1 %d, 2 %d, 3 %d, 4 %d\n", hdata.HistogramData[hdata.FirstBin], hdata.HistogramData[hdata.FirstBin+1], - hdata.HistogramData[hdata.FirstBin+2], hdata.HistogramData[hdata.FirstBin+3]); - vl53l0_errmsg("VL53L0_perform_single_histogram 5 %d, 6 %d, 7 %d, 8 %d\n", hdata.HistogramData[hdata.FirstBin+4], hdata.HistogramData[hdata.FirstBin+5], - hdata.HistogramData[hdata.FirstBin+6], hdata.HistogramData[hdata.FirstBin+7]); - vl53l0_errmsg("VL53L0_perform_single_histogram 9 %d, 10 %d, 11 %d, 12 %d\n", hdata.HistogramData[hdata.FirstBin+8], hdata.HistogramData[hdata.FirstBin+9], - hdata.HistogramData[hdata.FirstBin+10], hdata.HistogramData[hdata.FirstBin+11]); - - VL53L0_perform_single_histogram(data, 2, 1, 3, &hdata); - vl53l0_errmsg("VL53L0_perform_single_histogram start_bin %d, NumberOfBins %d, ErrorStatus %d\n", hdata.FirstBin, hdata.NumberOfBins, hdata.ErrorStatus); - vl53l0_errmsg("VL53L0_perform_single_histogram 1 %d, 2 %d, 3 %d, 4 %d\n", hdata.HistogramData[hdata.FirstBin], hdata.HistogramData[hdata.FirstBin+1], - hdata.HistogramData[hdata.FirstBin+2], hdata.HistogramData[hdata.FirstBin+3]); - VL53L0_perform_single_histogram(data, 2, 1, 7, &hdata); - vl53l0_errmsg("VL53L0_perform_single_histogram start_bin %d, NumberOfBins %d, ErrorStatus %d\n", hdata.FirstBin, hdata.NumberOfBins, hdata.ErrorStatus); - vl53l0_errmsg("VL53L0_perform_single_histogram 1 %d, 2 %d, 3 %d, 4 %d\n", hdata.HistogramData[hdata.FirstBin], hdata.HistogramData[hdata.FirstBin+1], - hdata.HistogramData[hdata.FirstBin+2], hdata.HistogramData[hdata.FirstBin+3]);*/ - } - - return count; -} -/* DEVICE_ATTR(name,mode,show,store) */ -static DEVICE_ATTR(set_mode, S_IWUSR | S_IRUGO, - stmvl53l0_show_set_mode, stmvl53l0_store_set_mode); - -static ssize_t stmvl53l0_show_enable_sar(struct device *dev, -struct device_attribute *attr, char *buf) -{ - struct stmvl53l0_data *data = gp_vl53l0_data; - - return snprintf(buf, 3, "%d\n", data->w_mode); -} - -/* - * to set sar working mode for rear proximity sensor - * parameter value 0: to turn sar mode off - * parameter value 1: to turn sar mode on - * parameter value 2: to reply flush call from user space by - * using current EV_ABS api - */ -static ssize_t stmvl53l0_store_enable_sar(struct device *dev, -struct device_attribute *attr, const char *buf, size_t count) -{ - struct stmvl53l0_data *data = gp_vl53l0_data; - long on; - - if (kstrtoul(buf, 10, &on)) - return count; - - if (on == 2) { - /* send flush reply */ - int dt_flush = 1; - - input_report_abs(data->input_dev_ps, ABS_MISC, dt_flush); - input_report_abs(data->input_dev_ps, ABS_MISC, ++dt_flush); - input_sync(data->input_dev_ps); - return count; - } - - if ((on != 0) && (on != 1)) { - vl53l0_errmsg("%d,set debug=%ld\n", __LINE__, on); - return count; - } - - if (on) - stmvl53l0_work_state(data, SAR_ON); - else - stmvl53l0_work_state(data, SAR_OFF); - - return count; -} - -/* DEVICE_ATTR(name,mode,show,store) */ -static DEVICE_ATTR(enable_sar, S_IWUSR | S_IRUGO, - stmvl53l0_show_enable_sar, stmvl53l0_store_enable_sar); static ssize_t stmvl53l0_show_enable_debug(struct device *dev, - struct device_attribute *attr, char *buf) + struct device_attribute *attr, char *buf) { - struct stmvl53l0_data *data = gp_vl53l0_data; + struct stmvl53l0_data *data = dev_get_drvdata(dev); - return snprintf(buf, 3, "%d\n", data->enableDebug); + return snprintf(buf, 5, "%d\n", data->enableDebug); } /* for debug */ static ssize_t stmvl53l0_store_enable_debug(struct device *dev, -struct device_attribute *attr, const char *buf, size_t count) + struct device_attribute *attr, const + char *buf, size_t count) { - struct stmvl53l0_data *data = gp_vl53l0_data; - long on; + struct stmvl53l0_data *data = dev_get_drvdata(dev); + unsigned long on = 0; - if (kstrtoul(buf, 10, &on)) - return count; - if ((on != 0) && (on != 1)) { - vl53l0_errmsg("%d,set debug=%ld\n", __LINE__, on); + int ret = kstrtoul(buf, 10, &on); + + if (ret != 0) + return ret; + + if ((on != 0) && (on != 1)) { + vl53l0_errmsg("set debug=%lu\n", on); return count; } data->enableDebug = on; @@ -1636,13 +961,388 @@ struct device_attribute *attr, const char *buf, size_t count) } /* DEVICE_ATTR(name,mode,show,store) */ -static DEVICE_ATTR(enable_debug, S_IWUSR | S_IRUGO, +static DEVICE_ATTR(enable_debug, 0660/*S_IWUSR | S_IRUGO*/, stmvl53l0_show_enable_debug, stmvl53l0_store_enable_debug); +static ssize_t stmvl53l0_show_set_delay_ms(struct device *dev, + struct device_attribute *attr, char *buf) +{ + struct stmvl53l0_data *data = dev_get_drvdata(dev); + + return snprintf(buf, 5, "%d\n", data->delay_ms); +} + +/* for work handler scheduler time */ +static ssize_t stmvl53l0_store_set_delay_ms(struct device *dev, + struct device_attribute *attr, + const char *buf, size_t count) +{ + struct stmvl53l0_data *data = dev_get_drvdata(dev); + unsigned long delay_ms = 0; + + int ret = kstrtoul(buf, 10, &delay_ms); + + if (ret != 0) + return ret; + if (delay_ms == 0) { + vl53l0_errmsg("set delay_ms=%lu\n", delay_ms); + return count; + } + mutex_lock(&data->work_mutex); + data->delay_ms = delay_ms; + mutex_unlock(&data->work_mutex); + + return count; +} + +/* DEVICE_ATTR(name,mode,show,store) */ +static DEVICE_ATTR(set_delay_ms, 0660/*S_IWUGO | S_IRUGO*/, + stmvl53l0_show_set_delay_ms, stmvl53l0_store_set_delay_ms); + +/* Timing Budget */ +static ssize_t stmvl53l0_show_timing_budget(struct device *dev, + struct device_attribute *attr, char *buf) +{ + struct stmvl53l0_data *data = dev_get_drvdata(dev); + + return snprintf(buf, 10, "%d\n", data->timingBudget); +} + +static ssize_t stmvl53l0_store_set_timing_budget(struct device *dev, + struct device_attribute *attr, + const char *buf, size_t count) +{ + struct stmvl53l0_data *data = dev_get_drvdata(dev); + unsigned long timingBudget = 0; + int ret = kstrtoul(buf, 10, &timingBudget); + + if (ret != 0) + return ret; + if (timingBudget == 0) { + vl53l0_errmsg("set timingBudget=%lu\n", timingBudget); + return count; + } + mutex_lock(&data->work_mutex); + data->timingBudget = timingBudget; + mutex_unlock(&data->work_mutex); + + return count; +} + +/* DEVICE_ATTR(name,mode,show,store) */ +static DEVICE_ATTR(set_timing_budget, 0660/*S_IWUGO | S_IRUGO*/, + stmvl53l0_show_timing_budget, stmvl53l0_store_set_timing_budget); + +/* Use case */ +static ssize_t stmvl53l0_show_use_case(struct device *dev, + struct device_attribute *attr, char *buf) +{ + struct stmvl53l0_data *data = dev_get_drvdata(dev); + + switch (data->useCase) { + case USE_CASE_LONG_DISTANCE: + return snprintf(buf, 20, "Long Distance\n"); + case USE_CASE_HIGH_ACCURACY: + return snprintf(buf, 20, "High Accuracy\n"); + case USE_CASE_HIGH_SPEED: + return snprintf(buf, 20, "High Speed\n"); + default: + break; + } + + return snprintf(buf, 25, "Unknown use case\n"); +} + +static ssize_t stmvl53l0_store_set_use_case(struct device *dev, + struct device_attribute *attr, + const char *buf, size_t count) +{ + struct stmvl53l0_data *data = dev_get_drvdata(dev); + unsigned long useCase = 0; + int ret = kstrtoul(buf, 10, &useCase); + + if (ret != 0) + return ret; + + mutex_lock(&data->work_mutex); + + if (useCase == USE_CASE_LONG_DISTANCE) { + data->timingBudget = LONG_DISTANCE_TIMING_BUDGET; + } else if (useCase == USE_CASE_HIGH_SPEED) { + data->timingBudget = HIGH_SPEED_TIMING_BUDGET; + } else if (useCase == USE_CASE_HIGH_ACCURACY) { + data->timingBudget = HIGH_ACCURACY_TIMING_BUDGET; + } else { + count = -EINVAL; + mutex_unlock(&data->work_mutex); + return count; + } + + data->useCase = useCase; + mutex_unlock(&data->work_mutex); + + return count; +} + +/* DEVICE_ATTR(name,mode,show,store) */ +static DEVICE_ATTR(set_use_case, 0660/*S_IWUGO | S_IRUGO*/, + stmvl53l0_show_use_case, stmvl53l0_store_set_use_case); + + +/* Get Current configuration info */ +static ssize_t stmvl53l0_show_current_configuration(struct device *dev, + struct device_attribute *attr, char *buf) +{ + struct stmvl53l0_data *vl53l0_dev = dev_get_drvdata(dev); + VL53L0_Error Status = VL53L0_ERROR_NONE; + int ret = -1; + FixPoint1616_t LimitValue = 0; + uint8_t LimitEnable = 0; + uint32_t refSpadCount = 0; + uint8_t isApertureSpads = 0; + uint8_t VhvSettings = 0; + uint8_t PhaseCal = 0; + uint8_t pulsePeriod = 0; + uint32_t timingBudget = 0; + int32_t offsetCalibrationDataMicroMeter = -1; + uint8_t XTalkCompensationEnable = 0; + FixPoint1616_t xTalkCompensationRateMegaCps = 0; + + ret = scnprintf(buf, PAGE_SIZE, "VL53L0 current configuration:\n"); + + mutex_lock(&vl53l0_dev->work_mutex); + pr_err("Driver Config:UseCase:%d, offsetCalDistance:%u,xtalkCalDistance:%u,setCalibratedValue:0x%X\n", + vl53l0_dev->useCase, vl53l0_dev->offsetCalDistance, + vl53l0_dev->xtalkCalDistance, + vl53l0_dev->setCalibratedValue); + + ret += scnprintf(buf + ret, PAGE_SIZE - ret, + "Driver Config:UseCase:%d, offsetCalDistance:%u,xtalkCalDistance:%u,setCalibratedValue:0x%X\n", + vl53l0_dev->useCase, + vl53l0_dev->offsetCalDistance, + vl53l0_dev->xtalkCalDistance, + vl53l0_dev->setCalibratedValue); + + if (vl53l0_dev->useCase == USE_CASE_CUSTOM) { + pr_err("CustomUseCase : Sigma=%u :Signal=%u: Pre=%u :Final=%u\n", + vl53l0_dev->sigmaLimit, + vl53l0_dev->signalRateLimit, + vl53l0_dev->preRangePulsePeriod, + vl53l0_dev->finalRangePulsePeriod); + + ret += scnprintf(buf + ret, PAGE_SIZE - ret, + "CustomUseCase : Sigma=%u :Signal=%u: Pre=%u :Final=%u\n", + vl53l0_dev->sigmaLimit, + vl53l0_dev->signalRateLimit, + vl53l0_dev->preRangePulsePeriod, + vl53l0_dev->finalRangePulsePeriod); + } + + Status = papi_func_tbl->GetLimitCheckValue(vl53l0_dev, + VL53L0_CHECKENABLE_SIGMA_FINAL_RANGE, + &LimitValue); + + if (Status == VL53L0_ERROR_NONE) { + Status = papi_func_tbl->GetLimitCheckEnable(vl53l0_dev, + VL53L0_CHECKENABLE_SIGMA_FINAL_RANGE, + &LimitEnable); + } + + if (Status == VL53L0_ERROR_NONE) { + pr_err("Get LimitCheckValue SIGMA_FINAL_RANGE as:%d,Enable:%d\n", + (LimitValue>>16), LimitEnable); + ret += scnprintf(buf + ret, PAGE_SIZE - ret, + "Sigma Limit:%u, Enable:%u\n", + (LimitValue>>16), + LimitEnable); + Status = papi_func_tbl->GetLimitCheckValue(vl53l0_dev, + VL53L0_CHECKENABLE_SIGNAL_RATE_FINAL_RANGE, + &LimitValue); + Status = papi_func_tbl->GetLimitCheckEnable(vl53l0_dev, + VL53L0_CHECKENABLE_SIGNAL_RATE_FINAL_RANGE, + &LimitEnable); + } + + if (Status == VL53L0_ERROR_NONE) { + pr_err("Get LimitCheckValue SIGNAL_FINAL_RANGE as:%d(Fix1616),Enable:%d\n", + (LimitValue), LimitEnable); + ret += scnprintf(buf + ret, PAGE_SIZE - ret, + "SIGNAL Limit:%u, Enable:%u\n", + LimitValue, + LimitEnable); + + Status = papi_func_tbl->GetLimitCheckValue(vl53l0_dev, + VL53L0_CHECKENABLE_SIGNAL_REF_CLIP, + &LimitValue); + Status = papi_func_tbl->GetLimitCheckEnable(vl53l0_dev, + VL53L0_CHECKENABLE_SIGNAL_REF_CLIP, + &LimitEnable); + + } + + if (Status == VL53L0_ERROR_NONE) { + pr_err("Get LimitCheckValue SIGNAL_REF_CLIP as:%d(fix1616),Enable:%d\n", + (LimitValue), LimitEnable); + ret += scnprintf(buf + ret, PAGE_SIZE - ret, + "RefClipLimit:%u, Enable:%u\n", + LimitValue, + LimitEnable); + + Status = papi_func_tbl->GetLimitCheckValue(vl53l0_dev, + VL53L0_CHECKENABLE_RANGE_IGNORE_THRESHOLD, + &LimitValue); + Status = papi_func_tbl->GetLimitCheckEnable(vl53l0_dev, + VL53L0_CHECKENABLE_RANGE_IGNORE_THRESHOLD, + &LimitEnable); + } + + if (Status == VL53L0_ERROR_NONE) { + pr_err( + "Get LimitCheckValue RANGE_IGNORE_THRESHOLDas:%d(fix1616),Enable:%d\n", + (LimitValue), + LimitEnable); + ret += scnprintf(buf + ret, PAGE_SIZE - ret, + "RngIgnoreThresh:%u, Enable:%u\n", + LimitValue, + LimitEnable); + + Status = papi_func_tbl->GetRefCalibration(vl53l0_dev, + &VhvSettings, &PhaseCal); /* Ref calibration */ + + + } + + if (Status == VL53L0_ERROR_NONE) { + pr_err("GetRefCalibration - Vhv = %u, PhaseCal = %u\n", + VhvSettings, PhaseCal); + ret += scnprintf(buf + ret, PAGE_SIZE - ret, + "Vhv:%u, PhCal:%u\n", + VhvSettings, + PhaseCal); + + /* Ref Spad Management */ + Status = papi_func_tbl->GetReferenceSpads(vl53l0_dev, + &refSpadCount, &isApertureSpads); + } + + if (Status == VL53L0_ERROR_NONE) { + pr_err("GetSpads - Count = %u, IsAperture = %u\n", + refSpadCount, isApertureSpads); + ret += scnprintf(buf + ret, PAGE_SIZE - ret, + "SpadCount:%u, IsAperture:%u\n", + refSpadCount, + isApertureSpads); + + Status = papi_func_tbl->GetMeasurementTimingBudgetMicroSeconds( + vl53l0_dev, + &timingBudget); + } + + if (Status == VL53L0_ERROR_NONE) { + pr_err("TimingBudget = %u\n", timingBudget); + ret += scnprintf(buf + ret, PAGE_SIZE - ret, "TimBudget:%u\n", + timingBudget); + Status = papi_func_tbl->GetVcselPulsePeriod(vl53l0_dev, + VL53L0_VCSEL_PERIOD_PRE_RANGE, + &pulsePeriod); + } + + if (Status == VL53L0_ERROR_NONE) { + pr_err("GetVcselPulsePeriod - PRE_RANGE = %u\n", + pulsePeriod); + ret += scnprintf(buf + ret, PAGE_SIZE - ret, + "PulsePreRange:%u\n", + pulsePeriod); + + Status = papi_func_tbl->GetVcselPulsePeriod(vl53l0_dev, + VL53L0_VCSEL_PERIOD_FINAL_RANGE, + &pulsePeriod); + } + + if (Status == VL53L0_ERROR_NONE) { + pr_err("GetVcselPulsePeriod - FINAL_RANGE = %u\n", + pulsePeriod); + ret += scnprintf(buf + ret, PAGE_SIZE - ret, + "PulseFinalRange:%u\n", + pulsePeriod); + + Status = papi_func_tbl->GetOffsetCalibrationDataMicroMeter( + vl53l0_dev, + &offsetCalibrationDataMicroMeter); + } + + if (Status == VL53L0_ERROR_NONE) { + pr_err("OffsetCalibrationDataMicroMeter = %d\n", + offsetCalibrationDataMicroMeter); + ret += scnprintf(buf + ret, PAGE_SIZE - ret, "Offset:%d\n", + offsetCalibrationDataMicroMeter); + + Status = papi_func_tbl->GetXTalkCompensationEnable(vl53l0_dev, + &XTalkCompensationEnable); + } + + if (Status == VL53L0_ERROR_NONE) { + pr_err("Xtalk Enable = %u\n", XTalkCompensationEnable); + ret += scnprintf(buf + ret, PAGE_SIZE - ret, "XtalkEnable:%u\n", + XTalkCompensationEnable); + + Status = papi_func_tbl->GetXTalkCompensationRateMegaCps( + vl53l0_dev, + &xTalkCompensationRateMegaCps); + } + + + if (Status == VL53L0_ERROR_NONE) { + pr_err("XtalkComp MCPS = %u\n", xTalkCompensationRateMegaCps); + ret += scnprintf(buf + ret, PAGE_SIZE - ret, "XtalkMcps:%u\n", + xTalkCompensationRateMegaCps); + + } else { + pr_err("Error = %d\n", Status); + ret += scnprintf(buf + ret, PAGE_SIZE - ret, "Error:%d\n", + Status); + + } + + pr_err("Total Bytes returned = %d\n", ret); + + mutex_unlock(&vl53l0_dev->work_mutex); + + + return ret; +} + +/* DEVICE_ATTR(name,mode,show,store) */ +static DEVICE_ATTR(show_current_configuration, 0660/*S_IWUGO | S_IRUGO*/, + stmvl53l0_show_current_configuration, NULL); +/* for work handler scheduler time */ +static ssize_t stmvl53l0_do_flush(struct device *dev, + struct device_attribute *attr, + const char *buf, size_t count) +{ + struct stmvl53l0_data *data = dev_get_drvdata(dev); + + int ret = 0; + /* Revisit : Avoid lock if it takes too long time */ + mutex_lock(&data->work_mutex); + + vl53l0_dbgmsg("Starting timer to fire in 1ms (%ld)\n", jiffies); + ret = mod_timer(&data->timer, jiffies + msecs_to_jiffies(1)); + if (ret) + pr_err("Error from mod_timer = %d\n", ret); + + mutex_unlock(&data->work_mutex); + return count; +} + +/* DEVICE_ATTR(name,mode,show,store) */ +static DEVICE_ATTR(do_flush, 0660/*S_IWUGO | S_IRUGO*/, + NULL, stmvl53l0_do_flush); + static ssize_t stmvl53l0_show_offset(struct device *dev, struct device_attribute *attr, char *buf) { - struct stmvl53l0_data *data = gp_vl53l0_data; + struct stmvl53l0_data *data = dev_get_drvdata(dev); return snprintf(buf, 5, "%d\n", data->offset); } @@ -1650,7 +1350,7 @@ static ssize_t stmvl53l0_show_offset(struct device *dev, static ssize_t stmvl53l0_store_offset(struct device *dev, struct device_attribute *attr, const char *buf, size_t count) { - struct stmvl53l0_data *data = gp_vl53l0_data; + struct stmvl53l0_data *data = dev_get_drvdata(dev); long on; if (buf[0] == '-') { @@ -1663,24 +1363,7 @@ static ssize_t stmvl53l0_store_offset(struct device *dev, } vl53l0_errmsg("%d,set offset=%ld\n", __LINE__, on); data->offset = on * 1000; - mutex_lock(&data->work_mutex); - if (data->enable_ps_sensor == 0) - stmvl53l0_start(data); - if (data->w_mode != OFF_MODE) { - vl53l0_errmsg("%d, set offsetcal in other mode, it will be postpone \n", __LINE__); - if (s_nump < 10) { - s_parameters[s_nump].name = OFFSET_PAR; - s_parameters[s_nump].is_read = 0; - s_parameters[s_nump].value = data->offset; - s_nump++; - } - } - if (data->enable_ps_sensor == 1) { - data->enable_ps_sensor = 0; - /* to stop */ - stmvl53l0_stop(data); - } - mutex_unlock(&data->work_mutex); + return count; } @@ -1691,7 +1374,7 @@ static DEVICE_ATTR(offset, S_IWUSR | S_IRUGO, static ssize_t stmvl53l0_show_xtalk(struct device *dev, struct device_attribute *attr, char *buf) { - struct stmvl53l0_data *data = gp_vl53l0_data; + struct stmvl53l0_data *data = dev_get_drvdata(dev); return snprintf(buf, 5, "%d\n", data->xtalk); } @@ -1699,7 +1382,7 @@ static ssize_t stmvl53l0_show_xtalk(struct device *dev, static ssize_t stmvl53l0_store_xtalk(struct device *dev, struct device_attribute *attr, const char *buf, size_t count) { - struct stmvl53l0_data *data = gp_vl53l0_data; + struct stmvl53l0_data *data = dev_get_drvdata(dev); long on; if (kstrtoul(buf, 10, &on)) @@ -1713,164 +1396,100 @@ static ssize_t stmvl53l0_store_xtalk(struct device *dev, static DEVICE_ATTR(xtalk, S_IWUSR | S_IRUGO, stmvl53l0_show_xtalk, stmvl53l0_store_xtalk); -static ssize_t stmvl53l0_show_set_delay_ms(struct device *dev, - struct device_attribute *attr, char *buf) -{ - struct stmvl53l0_data *data = gp_vl53l0_data; - - return snprintf(buf, 3, "%d\n", data->delay_ms); -} - -/* for work handler scheduler time */ -static ssize_t stmvl53l0_store_set_delay_ms(struct device *dev, - struct device_attribute *attr, const char *buf, size_t count) -{ - struct stmvl53l0_data *data = gp_vl53l0_data; - long delay_ms; - - if (kstrtoul(buf, 10, &delay_ms)) - return count; - if (delay_ms == 0) { - vl53l0_errmsg("%d,set delay_ms=%ld\n", __LINE__, delay_ms); - return count; - } - mutex_lock(&data->work_mutex); - data->delay_ms = delay_ms; - mutex_unlock(&data->work_mutex); - - return count; -} - -/* DEVICE_ATTR(name,mode,show,store) */ -static DEVICE_ATTR(set_delay_ms, S_IWUSR | S_IWGRP | S_IRUGO, - stmvl53l0_show_set_delay_ms, stmvl53l0_store_set_delay_ms); - -static ssize_t stmvl53l0_show_near(struct device *dev, -struct device_attribute *attr, char *buf) -{ - struct stmvl53l0_data *data = gp_vl53l0_data; - - return snprintf(buf, 5, "%d\n", - data->rangeData.RangeMilliMeter); -} - -/* for work handler scheduler time */ -static ssize_t stmvl53l0_store_near(struct device *dev, -struct device_attribute *attr, const char *buf, size_t count) -{ - return count; -} - -/* DEVICE_ATTR(name,mode,show,store) */ -static DEVICE_ATTR(near, S_IRUGO, - stmvl53l0_show_near, stmvl53l0_store_near); - - static struct attribute *stmvl53l0_attributes[] = { &dev_attr_enable_ps_sensor.attr, &dev_attr_enable_debug.attr, &dev_attr_set_delay_ms.attr, - &dev_attr_set_mode.attr, - &dev_attr_enable_sar.attr, - &dev_attr_near.attr, - &dev_attr_xtalk.attr, + &dev_attr_set_timing_budget.attr, + &dev_attr_set_use_case.attr, + &dev_attr_do_flush.attr, &dev_attr_offset.attr, + &dev_attr_xtalk.attr, + &dev_attr_show_current_configuration.attr, NULL }; - static const struct attribute_group stmvl53l0_attr_group = { .attrs = stmvl53l0_attributes, }; - /* -* misc device file operation functions -*/ + * misc device file operation functions + */ static int stmvl53l0_ioctl_handler(struct file *file, - unsigned int cmd, unsigned long arg, void __user *p) + unsigned int cmd, unsigned long arg, + void __user *p) { int rc = 0; unsigned int xtalkint = 0; unsigned int targetDistance = 0; int8_t offsetint = 0; - struct stmvl53l0_data *data = gp_vl53l0_data; - + uint8_t useCase = 0; + struct stmvl53l0_custom_use_case customUseCase; + struct stmvl53l0_data *data = + container_of(file->private_data, + struct stmvl53l0_data, miscdev); struct stmvl53l0_register reg; struct stmvl53l0_parameter parameter; VL53L0_DEV vl53l0_dev = data; + VL53L0_DeviceModes deviceMode; uint8_t page_num = 0; + VL53L0_Error Status = VL53L0_ERROR_NONE; + if (!data) return -EINVAL; - vl53l0_dbgmsg_en("Enter enable_ps_sensor:%d\n", data->enable_ps_sensor); + vl53l0_dbgmsg("Enter enable_ps_sensor:%d\n", data->enable_ps_sensor); switch (cmd) { - /* enable */ + /* enable */ case VL53L0_IOCTL_INIT: - vl53l0_dbgmsg_en("VL53L0_IOCTL_INIT\n"); - stmvl53l0_work_state(data, CAM_ON); + vl53l0_dbgmsg("VL53L0_IOCTL_INIT\n"); + /* turn on tof sensor only if it's not enabled by other + * client + */ + if (data->enable_ps_sensor == 0) { + /* to start */ + stmvl53l0_start(data, 3, NORMAL_MODE); + } else + rc = -EINVAL; break; - case VL53L0_IOCTL_INIT_NS: - if (data->enable_ps_sensor == 0) - stmvl53l0_start(data); - break; - case VL53L0_IOCTL_INIT_NB: - vl53l0_dbgmsg_en("init nb called\n"); - schedule_delayed_work(&data->initwork, 0); - break; - case VL53L0_IOCTL_SUSPEND: - mutex_lock(&data->work_mutex); - vl53l0_dbgmsg_en("VL53L0_IOCTL_SUSPEND\n"); - data->c_suspend = 1; - mutex_unlock(&data->work_mutex); - break; - case VL53L0_IOCTL_RESUME: - vl53l0_dbgmsg_en("VL53L0_IOCTL_RESUME\n"); - mutex_lock(&data->work_mutex); - if (data->c_suspend == 1) { - data->c_suspend = 0; - VL53L0_ClearInterruptMask(data, 0); - papi_func_tbl->StartMeasurement(data); - } - mutex_unlock(&data->work_mutex); - break; - case VL53L0_IOCTL_RESET: - stmvl53l0_work_state(data, RESET); - break; - case VL53L0_IOCTL_RESET_NB: - schedule_delayed_work(&data->resetwork, 0); - break; - /* crosstalk calibration */ + /* crosstalk calibration */ case VL53L0_IOCTL_XTALKCALB: - vl53l0_dbgmsg_en("VL53L0_IOCTL_XTALKCALB\n"); - data->xtalkCalDistance = 100; + vl53l0_dbgmsg("VL53L0_IOCTL_XTALKCALB\n"); + data->xtalkCalDistance = 100; if (copy_from_user(&targetDistance, (unsigned int *)p, sizeof(unsigned int))) { vl53l0_errmsg("%d, fail\n", __LINE__); return -EFAULT; } data->xtalkCalDistance = targetDistance; - stmvl53l0_work_state(data, XTALKCAL_ON); - if (copy_to_user((unsigned int *)p, - &(data->xtalkcalval), - sizeof(unsigned int))) { - vl53l0_errmsg("%d, fail\n", __LINE__); - return -EFAULT; - } + + /* turn on tof sensor only if it's not enabled by other + * client + */ + if (data->enable_ps_sensor == 0) { + /* to start */ + stmvl53l0_start(data, 3, XTALKCALIB_MODE); + } else + rc = -EINVAL; break; - /* set up Xtalk value */ + /* set up Xtalk value */ case VL53L0_IOCTL_SETXTALK: - vl53l0_dbgmsg_en("VL53L0_IOCTL_SETXTALK\n"); + vl53l0_dbgmsg("VL53L0_IOCTL_SETXTALK\n"); if (copy_from_user(&xtalkint, (unsigned int *)p, sizeof(unsigned int))) { vl53l0_errmsg("%d, fail\n", __LINE__); return -EFAULT; } - vl53l0_dbgmsg_en("SETXTALK as 0x%x\n", xtalkint); + vl53l0_dbgmsg("SETXTALK as 0x%x\n", xtalkint); + + /* later + * SetXTalkCompensationRate(vl53l0_dev, xtalkint); + */ break; - /* offset calibration */ + /* offset calibration */ case VL53L0_IOCTL_OFFCALB: - vl53l0_dbgmsg_en("VL53L0_IOCTL_OFFCALB\n"); + vl53l0_dbgmsg("VL53L0_IOCTL_OFFCALB\n"); data->offsetCalDistance = 50; if (copy_from_user(&targetDistance, (unsigned int *)p, sizeof(unsigned int))) { @@ -1878,45 +1497,116 @@ static int stmvl53l0_ioctl_handler(struct file *file, return -EFAULT; } data->offsetCalDistance = targetDistance; - stmvl53l0_work_state(data, OFFSETCAL_ON); + if (data->enable_ps_sensor == 0) { + /* to start */ + stmvl53l0_start(data, 3, OFFSETCALIB_MODE); + } else + rc = -EINVAL; break; - case VL53L0_IOCTL_STOPCALB: - vl53l0_dbgmsg_en("VL53L0_IOCTL_STOPCALB\n"); - stmvl53l0_work_state(data, CAL_OFF); - break; - /* set up offset value */ + /* set up offset value */ case VL53L0_IOCTL_SETOFFSET: - vl53l0_dbgmsg_en("VL53L0_IOCTL_SETOFFSET\n"); + vl53l0_dbgmsg("VL53L0_IOCTL_SETOFFSET\n"); if (copy_from_user(&offsetint, (int8_t *)p, sizeof(int8_t))) { vl53l0_errmsg("%d, fail\n", __LINE__); return -EFAULT; } - vl53l0_dbgmsg_en("SETOFFSET as %d\n", offsetint); + vl53l0_dbgmsg("SETOFFSET as %d\n", offsetint); + + /* later + * SetOffsetCalibrationData(vl53l0_dev, offsetint); + */ + break; + + /* Config use case */ + case VL53L0_IOCTL_ACTIVATE_USE_CASE: + vl53l0_dbgmsg("VL53L0_IOCTL_ACTIVATE_USE_CASE\n"); + if (copy_from_user(&useCase, (uint8_t *)p, sizeof(uint8_t))) { + vl53l0_errmsg("%d, fail\n", __LINE__); + return -EFAULT; + } + + /* Validate the user passed use case. + * Update the timingBudget value. The other + * parameters are updated approrpiately in config_use_case() + * Currently the timing budget can be updated through + * sysfs entry, and this needs additional steps to manage. + */ + switch (useCase) { + case USE_CASE_LONG_DISTANCE: + data->timingBudget = LONG_DISTANCE_TIMING_BUDGET; + break; + + case USE_CASE_HIGH_ACCURACY: + data->timingBudget = HIGH_ACCURACY_TIMING_BUDGET; + break; + + case USE_CASE_HIGH_SPEED: + data->timingBudget = HIGH_SPEED_TIMING_BUDGET; + break; + + default: + vl53l0_errmsg("%d, Unknown Use case = %u\n", __LINE__, + useCase); + return -EFAULT; + } + vl53l0_dbgmsg("useCase as %d\n", useCase); + /* record the use case */ + data->useCase = useCase; + + /* If ranging is in progress, let the work handler + * update the use case + */ + if (data->enable_ps_sensor) + data->updateUseCase = 1; + break; + + /* Config Custom use case */ + case VL53L0_IOCTL_ACTIVATE_CUSTOM_USE_CASE: + vl53l0_dbgmsg("VL53L0_IOCTL_ACTIVATE_CUSTOM_USE_CASE\n"); + if (copy_from_user(&customUseCase, + (struct stmvl53l0_custom_use_case *)p, + sizeof(struct stmvl53l0_custom_use_case))) { + vl53l0_errmsg("%d, fail\n", __LINE__); + return -EFAULT; + } + + data->sigmaLimit = customUseCase.sigmaLimit; + data->signalRateLimit = customUseCase.signalRateLimit; + data->preRangePulsePeriod = customUseCase.preRangePulsePeriod; + data->finalRangePulsePeriod = + customUseCase.finalRangePulsePeriod; + data->timingBudget = customUseCase.timingBudget; + vl53l0_dbgmsg( + "Sigma=%u,Signal=%u,Pre=%u,Final=%u,timingBudget=%u\n", + data->sigmaLimit, + data->signalRateLimit, + data->preRangePulsePeriod, + data->finalRangePulsePeriod, + data->timingBudget); + + /* record the use case */ + data->useCase = USE_CASE_CUSTOM; + /* If ranging is in progress, + * let the work handler update the use case + */ + if (data->enable_ps_sensor) + data->updateUseCase = 1; break; - /* disable */ + + /* disable */ case VL53L0_IOCTL_STOP: - vl53l0_dbgmsg_en("VL53L0_IOCTL_STOP\n"); - stmvl53l0_work_state(data, CAM_OFF); - break; - case VL53L0_IOCTL_GETDATAS_BLOCK: - vl53l0_dbgmsg_en("VL53L0_IOCTL_GETDATAS_BLOCK\n"); - data->d_ready = 0; - wait_event_timeout(data->range_data_wait, - data->d_ready > 0, - DATA_TIMEOUT); - if (copy_to_user((VL53L0_RangingMeasurementData_t *)p, - &(data->rangeData), - sizeof(VL53L0_RangingMeasurementData_t))) { - vl53l0_errmsg("%d, fail\n", __LINE__); - return -EFAULT; + vl53l0_dbgmsg("VL53L0_IOCTL_STOP\n"); + /* turn off tof sensor only if it's enabled by other client */ + if (data->enable_ps_sensor == 1) { + data->enable_ps_sensor = 0; + /* to stop */ + stmvl53l0_stop(data); } break; - /* Get all range data */ + /* Get all range data */ case VL53L0_IOCTL_GETDATAS: - vl53l0_dbgmsg_en("VL53L0_IOCTL_GETDATAS\n"); - if (data->enable_ps_sensor == 0) - return -EFAULT; + vl53l0_dbgmsg("VL53L0_IOCTL_GETDATAS\n"); if (copy_to_user((VL53L0_RangingMeasurementData_t *)p, &(data->rangeData), sizeof(VL53L0_RangingMeasurementData_t))) { @@ -1924,14 +1614,9 @@ static int stmvl53l0_ioctl_handler(struct file *file, return -EFAULT; } break; - /* Register tool */ + /* Register tool */ case VL53L0_IOCTL_REGISTER: - vl53l0_dbgmsg_en("VL53L0_IOCTL_REGISTER\n"); - /* turn on tof sensor */ - if (data->enable_ps_sensor == 0) { - /* to start */ - stmvl53l0_start(data); - } + vl53l0_dbgmsg("VL53L0_IOCTL_REGISTER\n"); if (copy_from_user(®, (struct stmvl53l0_register *)p, sizeof(struct stmvl53l0_register))) { vl53l0_errmsg("%d, fail\n", __LINE__); @@ -1939,7 +1624,7 @@ static int stmvl53l0_ioctl_handler(struct file *file, } reg.status = 0; page_num = (uint8_t)((reg.reg_index & 0x0000ff00) >> 8); - vl53l0_dbgmsg_en("IOCTL_REGISTER, page num:%d\n", page_num); + vl53l0_dbgmsg("VL53L0_IOCTL_REGISTER, page number:%d\n", page_num); if (page_num != 0) reg.status = VL53L0_WrByte(vl53l0_dev, 0xFF, page_num); @@ -1947,30 +1632,32 @@ static int stmvl53l0_ioctl_handler(struct file *file, case(4): if (reg.is_read) reg.status = VL53L0_RdDWord(vl53l0_dev, - (uint8_t)reg.reg_index, ®.reg_data); + (uint8_t)reg.reg_index, + ®.reg_data); else reg.status = VL53L0_WrDWord(vl53l0_dev, - (uint8_t)reg.reg_index, reg.reg_data); + (uint8_t)reg.reg_index, + reg.reg_data); break; case(2): if (reg.is_read) reg.status = VL53L0_RdWord(vl53l0_dev, - (uint8_t)reg.reg_index, - (uint16_t *)®.reg_data); + (uint8_t)reg.reg_index, + (uint16_t *)®.reg_data); else reg.status = VL53L0_WrWord(vl53l0_dev, - (uint8_t)reg.reg_index, - (uint16_t)reg.reg_data); + (uint8_t)reg.reg_index, + (uint16_t)reg.reg_data); break; case(1): if (reg.is_read) reg.status = VL53L0_RdByte(vl53l0_dev, - (uint8_t)reg.reg_index, - (uint8_t *)®.reg_data); + (uint8_t)reg.reg_index, + (uint8_t *)®.reg_data); else reg.status = VL53L0_WrByte(vl53l0_dev, - (uint8_t)reg.reg_index, - (uint8_t)reg.reg_data); + (uint8_t)reg.reg_index, + (uint8_t)reg.reg_data); break; default: reg.status = -1; @@ -1981,184 +1668,268 @@ static int stmvl53l0_ioctl_handler(struct file *file, if (copy_to_user((struct stmvl53l0_register *)p, ®, - sizeof(struct stmvl53l0_register))) { + sizeof(struct stmvl53l0_register))) { vl53l0_errmsg("%d, fail\n", __LINE__); return -EFAULT; } break; - /* parameter access */ + /* parameter access */ case VL53L0_IOCTL_PARAMETER: - vl53l0_dbgmsg_en("VL53L0_IOCTL_PARAMETER\n"); - /* turn on tof sensor */ - if (data->enable_ps_sensor == 0) { - /* to start */ - stmvl53l0_start(data); - } + vl53l0_dbgmsg("VL53L0_IOCTL_PARAMETER\n"); if (copy_from_user(¶meter, (struct stmvl53l0_parameter *)p, - sizeof(struct stmvl53l0_parameter))) { + sizeof(struct stmvl53l0_parameter))) { vl53l0_errmsg("%d, fail\n", __LINE__); return -EFAULT; } parameter.status = 0; - mutex_lock(&data->work_mutex); - if (!parameter.is_read && data->w_mode != OFF_MODE) { - vl53l0_errmsg("%d, write parameter in other mode, it will be postpone \n", __LINE__); - if (s_nump < 10) { - if (!copy_from_user(&s_parameters[s_nump], - (struct stmvl53l0_parameter *)p, sizeof(struct stmvl53l0_parameter))) - s_nump++; - } - mutex_unlock(&data->work_mutex); - return rc; - } + if (data->enableDebug) + vl53l0_dbgmsg( + "VL53L0_IOCTL_PARAMETER Name = %d\n", parameter.name); switch (parameter.name) { case (OFFSET_PAR): if (parameter.is_read) parameter.status = - papi_func_tbl->GetOffsetCalibrationDataMicroMeter( - vl53l0_dev, ¶meter.value); - else + papi_func_tbl->GetOffsetCalibrationDataMicroMeter( + vl53l0_dev, ¶meter.value); + else { parameter.status = - papi_func_tbl->SetOffsetCalibrationDataMicroMeter( - vl53l0_dev, parameter.value); + papi_func_tbl->SetOffsetCalibrationDataMicroMeter( + vl53l0_dev, parameter.value); + data->OffsetMicroMeter = parameter.value; + data->setCalibratedValue + |= SET_OFFSET_CALIB_DATA_MICROMETER_MASK; + + } + vl53l0_dbgmsg("get parameter value as %d\n", + parameter.value); + break; + + case (REFERENCESPADS_PAR): + if (parameter.is_read) { + parameter.status = + papi_func_tbl->GetReferenceSpads(vl53l0_dev, + (uint32_t *)&(parameter.value), + (uint8_t *)&(parameter.value2)); + if (data->enableDebug) + vl53l0_dbgmsg( + "Get RefSpad : Count:%u, Type:%u\n", + parameter.value, + (uint8_t)parameter.value2); + } else { + if (data->enableDebug) + vl53l0_dbgmsg( + "Set RefSpad : Count:%u, Type:%u\n", + parameter.value, + (uint8_t)parameter.value2); + + parameter.status = + papi_func_tbl->SetReferenceSpads(vl53l0_dev, + (uint32_t)(parameter.value), + (uint8_t)(parameter.value2)); + + data->refSpadCount = parameter.value; + data->isApertureSpads = + (uint8_t)(parameter.value2); + } + break; + + case (REFCALIBRATION_PAR): + if (parameter.is_read) { + parameter.status = + papi_func_tbl->GetRefCalibration(vl53l0_dev, + (uint8_t *)&(parameter.value), + (uint8_t *)&(parameter.value2)); + if (data->enableDebug) + vl53l0_dbgmsg( + "Get Ref : Vhv:%u, PhaseCal:%u\n", + (uint8_t)parameter.value, + (uint8_t)parameter.value2); + } else { + if (data->enableDebug) + vl53l0_dbgmsg( + "Set Ref : Vhv:%u, PhaseCal:%u\n", + (uint8_t)parameter.value, + (uint8_t)parameter.value2); + parameter.status = + papi_func_tbl->SetRefCalibration( + vl53l0_dev, + (uint8_t)(parameter.value), + (uint8_t)(parameter.value2)); + data->VhvSettings = (uint8_t)parameter.value; + data->PhaseCal = (uint8_t)(parameter.value2); + } break; case (XTALKRATE_PAR): if (parameter.is_read) parameter.status = - VL53L0_GetXTalkCompensationRateMegaCps( - vl53l0_dev, (FixPoint1616_t *)¶meter.value); - else - parameter.status = - papi_func_tbl->SetXTalkCompensationRateMegaCps( - vl53l0_dev, (FixPoint1616_t)parameter.value); + papi_func_tbl->GetXTalkCompensationRateMegaCps( + vl53l0_dev, + (FixPoint1616_t *) + ¶meter.value); + else { + /* Range Ignore Threshold value */ + FixPoint1616_t ritValue = 0; + parameter.status = + papi_func_tbl->SetXTalkCompensationRateMegaCps( + vl53l0_dev, + (FixPoint1616_t) + parameter.value); + data->XTalkCompensationRateMegaCps = + parameter.value; + data->setCalibratedValue |= + SET_XTALK_COMP_RATE_MCPS_MASK; + + + /*0.7 KCps converted to MCps */ + if (data->XTalkCompensationRateMegaCps < + 7*65536/10000) { + ritValue = 15 * 7 * 65536/100000; + } else { + ritValue = 15 * + vl53l0_dev->XTalkCompensationRateMegaCps + /10; + } + + if (papi_func_tbl->SetLimitCheckEnable + != NULL) { + Status = + papi_func_tbl->SetLimitCheckEnable( + vl53l0_dev, + VL53L0_CHECKENABLE_RANGE_IGNORE_THRESHOLD, + 1); + } + + if ((Status == VL53L0_ERROR_NONE) && + (papi_func_tbl->SetLimitCheckValue + != NULL)) { + vl53l0_dbgmsg( + "Set RIT - %u\n", ritValue); + Status = + papi_func_tbl->SetLimitCheckValue( + vl53l0_dev, + VL53L0_CHECKENABLE_RANGE_IGNORE_THRESHOLD, + ritValue); + } + + } break; case (XTALKENABLE_PAR): if (parameter.is_read) parameter.status = papi_func_tbl->GetXTalkCompensationEnable( - vl53l0_dev, (uint8_t *)¶meter.value); + vl53l0_dev, + (uint8_t *) ¶meter.value); else parameter.status = papi_func_tbl->SetXTalkCompensationEnable( - vl53l0_dev, (uint8_t)parameter.value); + vl53l0_dev, + (uint8_t) parameter.value); break; - case (SIGMAVAL_PRA): - if (!parameter.is_read) - parameter.status = papi_func_tbl-> - SetLimitCheckValue(vl53l0_dev, - VL53L0_CHECKENABLE_SIGMA_FINAL_RANGE, - (FixPoint1616_t)parameter.value); - else - parameter.status = papi_func_tbl-> - GetLimitCheckValue(vl53l0_dev, - VL53L0_CHECKENABLE_SIGMA_FINAL_RANGE, - (FixPoint1616_t *)¶meter.value); - break; - - case (SIGMACTL_PRA): - if (!parameter.is_read) + case (GPIOFUNC_PAR): + if (parameter.is_read) { parameter.status = - papi_func_tbl->SetLimitCheckEnable(vl53l0_dev, - VL53L0_CHECKENABLE_SIGMA_FINAL_RANGE, - (uint8_t)parameter.value); - else + papi_func_tbl->GetGpioConfig( + vl53l0_dev, + 0, + &deviceMode, + &data->gpio_function, + &data->gpio_polarity); + parameter.value = + data->gpio_function; + } else { + data->gpio_function = parameter.value; parameter.status = - papi_func_tbl->GetLimitCheckEnable(vl53l0_dev, - VL53L0_CHECKENABLE_SIGMA_FINAL_RANGE, - (uint8_t *)¶meter.value); - break; - case (SGLVAL_PRA): - if (!parameter.is_read) - parameter.status = papi_func_tbl-> - SetLimitCheckValue(vl53l0_dev, - VL53L0_CHECKENABLE_SIGNAL_RATE_FINAL_RANGE, - (FixPoint1616_t)parameter.value); - else - parameter.status = papi_func_tbl-> - GetLimitCheckValue(vl53l0_dev, - VL53L0_CHECKENABLE_SIGNAL_RATE_FINAL_RANGE, - (FixPoint1616_t *)¶meter.value); - break; - - case (SGLCTL_PRA): - if (!parameter.is_read) - parameter.status = - papi_func_tbl->SetLimitCheckEnable(vl53l0_dev, - VL53L0_CHECKENABLE_SIGNAL_RATE_FINAL_RANGE, - (uint8_t)parameter.value); - else - parameter.status = - papi_func_tbl->GetLimitCheckEnable(vl53l0_dev, - VL53L0_CHECKENABLE_SIGNAL_RATE_FINAL_RANGE, - (uint8_t *)¶meter.value); - break; - - case (WRAPAROUNDCTL_PRA): - if (parameter.is_read) - parameter.status = - VL53L0_GetWrapAroundCheckEnable( - vl53l0_dev, (uint8_t *)¶meter.value); - else - parameter.status = - VL53L0_SetWrapAroundCheckEnable( - vl53l0_dev, (uint8_t)parameter.value); - break; - - case (MEASUREMENTTIMINGBUDGET_PAR): - if (parameter.is_read) - parameter.status = - VL53L0_GetMeasurementTimingBudgetMicroSeconds( - vl53l0_dev, (uint32_t *)¶meter.value); - else - parameter.status = - VL53L0_SetMeasurementTimingBudgetMicroSeconds( - vl53l0_dev, (uint32_t)parameter.value); - break; - case (CUTV_PRA): - if (parameter.is_read) - parameter.value = vl53l0_dev->cut_v; - break; - case (DRVV_PRA): - if (parameter.is_read) - parameter.value = DRIVER_VERSION_NUM; - break; - case (PLLPRE_PRA): - if (parameter.is_read) - parameter.status = - papi_func_tbl->GetVcselPulsePeriod(vl53l0_dev, - VL53L0_VCSEL_PERIOD_PRE_RANGE, (uint8_t *)¶meter.value); - else { - parameter.status = - papi_func_tbl->SetVcselPulsePeriod(vl53l0_dev, - VL53L0_VCSEL_PERIOD_PRE_RANGE, (uint8_t)parameter.value); - vl53l0_dev->pll_p = (uint8_t)parameter.value; + papi_func_tbl->SetGpioConfig( + vl53l0_dev, + 0, + 0, + data->gpio_function, + data->gpio_polarity); } break; - case (PLLFINAL_PRA): - if (parameter.is_read) + case (LOWTHRESH_PAR): + if (parameter.is_read) { parameter.status = - papi_func_tbl->GetVcselPulsePeriod(vl53l0_dev, - VL53L0_VCSEL_PERIOD_FINAL_RANGE, (uint8_t *)¶meter.value); - else { + papi_func_tbl->GetInterruptThresholds( + vl53l0_dev, + 0, + &(data->low_threshold), + &(data->high_threshold)); + parameter.value = + data->low_threshold >> 16; + } else { + data->low_threshold = parameter.value << 16; parameter.status = - papi_func_tbl->SetVcselPulsePeriod(vl53l0_dev, - VL53L0_VCSEL_PERIOD_FINAL_RANGE, (uint8_t)parameter.value); - vl53l0_dev->pll_f = (uint8_t)parameter.value; + papi_func_tbl->SetInterruptThresholds( + vl53l0_dev, + 0, + data->low_threshold, + data->high_threshold); } break; + case (HIGHTHRESH_PAR): + if (parameter.is_read) { + parameter.status = + papi_func_tbl->GetInterruptThresholds( + vl53l0_dev, + 0, + &(data->low_threshold), + &(data->high_threshold)); + parameter.value = + data->high_threshold >> 16; + } else { + data->high_threshold = + parameter.value << 16; + parameter.status = + papi_func_tbl->SetInterruptThresholds( + vl53l0_dev, + 0, + data->low_threshold, + data->high_threshold); + } + break; + case (DEVICEMODE_PAR): + if (parameter.is_read) { + parameter.status = + papi_func_tbl->GetDeviceMode( + vl53l0_dev, + (VL53L0_DeviceModes *)&(parameter.value)); + } else { + parameter.status = + papi_func_tbl->SetDeviceMode( + vl53l0_dev, + (VL53L0_DeviceModes)(parameter.value)); + data->deviceMode = + (VL53L0_DeviceModes)(parameter.value); + } + break; + + + + case (INTERMEASUREMENT_PAR): + if (parameter.is_read) { + parameter.status = + papi_func_tbl->GetInterMeasurementPeriodMilliSeconds( + vl53l0_dev, + (uint32_t *)&(parameter.value)); + } else { + parameter.status = + papi_func_tbl->SetInterMeasurementPeriodMilliSeconds( + vl53l0_dev, + (uint32_t)(parameter.value)); + data->interMeasurems = parameter.value; + } + break; + } - - - mutex_unlock(&data->work_mutex); if (copy_to_user((struct stmvl53l0_parameter *)p, ¶meter, - sizeof(struct stmvl53l0_parameter))) { + sizeof(struct stmvl53l0_parameter))) { vl53l0_errmsg("%d, fail\n", __LINE__); return -EFAULT; } break; - default: rc = -EINVAL; break; @@ -2172,28 +1943,41 @@ static int stmvl53l0_open(struct inode *inode, struct file *file) return 0; } -static int stmvl53l0_close(struct inode *inode, struct file *file) -{ - struct stmvl53l0_data *data = gp_vl53l0_data; - - if (data->w_mode == CAM_MODE || data->w_mode == SUPER_MODE) { - stmvl53l0_work_state(data, CAM_OFF); - } - if (data->w_mode == XTALKCAL_MODE || data->w_mode == OFFSETCAL_MODE) - stmvl53l0_work_state(data, CAL_OFF); - return 0; -} - +/* Flush will be invoked on close(device_fd) */ static int stmvl53l0_flush(struct file *file, fl_owner_t id) { + struct stmvl53l0_data *data = container_of(file->private_data, + struct stmvl53l0_data, miscdev); + (void) file; + (void) id; + /* Revisit : Check if the + *instance are opened multiple times on some platforms + */ + mutex_lock(&data->work_mutex); + if (data) { + if (data->enable_ps_sensor == 1) { + /* turn off tof sensor if it's enabled */ + data->enable_ps_sensor = 0; + /* to stop */ + stmvl53l0_stop(data); + } + } + mutex_unlock(&data->work_mutex); + return 0; } static long stmvl53l0_ioctl(struct file *file, unsigned int cmd, unsigned long arg) { - int ret; + long ret; + struct stmvl53l0_data *data = + container_of(file->private_data, + struct stmvl53l0_data, miscdev); + mutex_lock(&data->work_mutex); ret = stmvl53l0_ioctl_handler(file, cmd, arg, (void __user *)arg); + mutex_unlock(&data->work_mutex); + return ret; } @@ -2201,54 +1985,18 @@ static long stmvl53l0_ioctl(struct file *file, static long stmvl53l0_compat_ioctl(struct file *file, unsigned int cmd, unsigned long arg) { - int ret; - + long ret; + struct stmvl53l0_data *data = + container_of(file->private_data, + struct stmvl53l0_data, miscdev); + mutex_lock(&data->work_mutex); ret = stmvl53l0_ioctl_handler(file, cmd, arg, compat_ptr(arg)); + mutex_unlock(&data->work_mutex); + return ret; } #endif -int stmvl53l0_livechecking(struct stmvl53l0_data *data) -{ - uint8_t id = 0; - VL53L0_DEV vl53l0_dev = data; - struct timeval tv; - - vl53l0_dbgmsg_en("Enter\n"); - /* Read Model ID */ - mutex_lock(&data->work_mutex); - if (data->enable_ps_sensor == 1 && SAR_MODE == data->w_mode) { - VL53L0_RdByte(vl53l0_dev, - VL53L0_REG_IDENTIFICATION_MODEL_ID, &id); - vl53l0_dbgmsg("read MODLE_ID: 0x%x\n", id); - if (id == 0xee) - vl53l0_dbgmsg("STM VL53L0 still alive\n"); - else { - vl53l0_errmsg("STM VL53L0 looks dead\n"); - do_gettimeofday(&tv); - - input_report_abs(data->input_dev_ps, ABS_DISTANCE, 1); - input_report_abs(data->input_dev_ps, ABS_HAT0X, tv.tv_sec); - input_report_abs(data->input_dev_ps, ABS_HAT0Y, tv.tv_usec); - input_report_abs(data->input_dev_ps, ABS_HAT1X, - data->rangeData.RangeMilliMeter); - input_report_abs(data->input_dev_ps, ABS_HAT1Y, - data->rangeData.RangeStatus); - input_report_abs(data->input_dev_ps, ABS_HAT2X, - data->rangeData.SignalRateRtnMegaCps); - input_report_abs(data->input_dev_ps, ABS_HAT2Y, - data->rangeData.AmbientRateRtnMegaCps); - input_report_abs(data->input_dev_ps, ABS_HAT3X, - data->rangeData.MeasurementTimeUsec); - input_report_abs(data->input_dev_ps, ABS_HAT3Y, - data->rangeData.RangeDMaxMilliMeter); - input_sync(data->input_dev_ps); - } - } - mutex_unlock(&data->work_mutex); - -return 0; -} int stmvl53l0_checkmoduleid(struct stmvl53l0_data *data, void *client, uint8_t type) { @@ -2257,11 +2005,6 @@ int stmvl53l0_checkmoduleid(struct stmvl53l0_data *data, vl53l0_dbgmsg("Enter\n"); if (type == CCI_BUS) { - struct msm_camera_i2c_client *cci_pclient = - (struct msm_camera_i2c_client *)client; - - cci_pclient->i2c_func_tbl->i2c_read_seq(cci_pclient, - 0xc0, &id, 1); } else { struct i2c_client *i2c_pclient = (struct i2c_client *)client; struct i2c_msg msg[1]; @@ -2307,161 +2050,207 @@ int stmvl53l0_checkmoduleid(struct stmvl53l0_data *data, } /* -* Initialization function -*/ + * Initialization function + */ static int stmvl53l0_init_client(struct stmvl53l0_data *data) { - uint8_t id = 0, type = 0, i = 0; - uint8_t revision = 0, module_id = 0; + VL53L0_Error Status = VL53L0_ERROR_NONE; VL53L0_DeviceInfo_t DeviceInfo; VL53L0_DEV vl53l0_dev = data; - FixPoint1616_t LimitValue; - uint8_t LimitEnable; + uint32_t refSpadCount; + uint8_t isApertureSpads; + uint8_t VhvSettings; + uint8_t PhaseCal; + uint8_t revision = 1; + uint8_t retry = 2; - vl53l0_dbgmsg_en("Enter\n"); + vl53l0_dbgmsg("Enter\n"); - /* Read Model ID */ - for (i = 0; i < 5; i++) { - VL53L0_RdByte(vl53l0_dev, - VL53L0_REG_IDENTIFICATION_MODEL_ID, &id); - vl53l0_errmsg("read MODLE_ID: 0x%x\n", id); - if (id == 0xee) { - vl53l0_errmsg("STM VL53L0 Found\n"); + data->I2cDevAddr = 0x52; + data->comms_type = 1; + data->comms_speed_khz = 400; + + /* Setup API functions based on revision */ + do { + retry--; + revision = stmvl53l0_setupAPIFunctions(data); + if ((revision == 1) || (retry == 0)) { break; + } else { + vl53l0_dbgmsg("retry to stmvl53l0_setupAPIFunctions\n"); + msleep(20); } + } while (retry > 0); + + if (revision == 0) + return VL53L0_ERROR_CONTROL_INTERFACE; + + /* Perform Ref and RefSpad calibrations and save the values */ - udelay(1000); - } - if (id != 0xee) { - vl53l0_errmsg("Not found STM VL53L0\n"); - return -EIO; - } if (data->reset) { - VL53L0_RdByte(vl53l0_dev, 0xc1, &id); - vl53l0_errmsg("read 0xc1: 0x%x\n", id); - VL53L0_RdByte(vl53l0_dev, 0xc2, &id); - vl53l0_errmsg("read 0xc2: 0x%x\n", id); - VL53L0_RdByte(vl53l0_dev, 0xc3, &id); - vl53l0_errmsg("read 0xc3: 0x%x\n", id); - - /* Read Model Version */ - VL53L0_RdByte(vl53l0_dev, 0xC0, &type); - VL53L0_RdByte(vl53l0_dev, VL53L0_REG_IDENTIFICATION_REVISION_ID, - &revision); - VL53L0_RdByte(vl53l0_dev, 0xc3, - &module_id); - vl53l0_errmsg("STM VL53L0 Model type : %d. rev:%d. module:%d\n", type, - revision, module_id); - - vl53l0_dev->I2cDevAddr = 0x52; - vl53l0_dev->comms_type = 1; - vl53l0_dev->comms_speed_khz = 400; - - /* Setup API functions based on revision */ - stmvl53l0_setupAPIFunctions(data); - } - - if (Status == VL53L0_ERROR_NONE) { pr_err("Call of VL53L0_DataInit\n"); + /* Data initialization */ Status = papi_func_tbl->DataInit(vl53l0_dev); - } - - if (Status == VL53L0_ERROR_NONE && data->reset) { - pr_err("VL53L0_GetDeviceInfo:\n"); - Status = papi_func_tbl->GetDeviceInfo(vl53l0_dev, &DeviceInfo); - if (Status == VL53L0_ERROR_NONE) { - pr_err("Device Name : %s\n", DeviceInfo.Name); - pr_err("Device Type : %s\n", DeviceInfo.Type); - pr_err("Device ID : %s\n", DeviceInfo.ProductId); - pr_err("Product type: %d\n", DeviceInfo.ProductType); - pr_err("ProductRevisionMajor : %d\n", - DeviceInfo.ProductRevisionMajor); - pr_err("ProductRevisionMinor : %d\n", - DeviceInfo.ProductRevisionMinor); + /* data->reset = 0; */ + if (Status != VL53L0_ERROR_NONE) { + vl53l0_errmsg( + "%d- error status %d\n", __LINE__, Status); + return Status; } } + vl53l0_dbgmsg("VL53L0_GetDeviceInfo:\n"); + Status = papi_func_tbl->GetDeviceInfo(vl53l0_dev, &DeviceInfo); if (Status == VL53L0_ERROR_NONE) { - pr_err("Call of VL53L0_StaticInit\n"); - Status = papi_func_tbl->StaticInit(vl53l0_dev); + pr_err("Device Name : %s\n", DeviceInfo.Name); + pr_err("Device Type : %s\n", DeviceInfo.Type); + pr_err("Device ID : %s\n", DeviceInfo.ProductId); + pr_err("Product type: %d\n", DeviceInfo.ProductType); + pr_err("ProductRevisionMajor : %d\n", + DeviceInfo.ProductRevisionMajor); + pr_err("ProductRevisionMinor : %d\n", + DeviceInfo.ProductRevisionMinor); + } /* Device Initialization */ + vl53l0_dbgmsg("Call of VL53L0_StaticInit\n"); + Status = papi_func_tbl->StaticInit(vl53l0_dev); + if (Status != VL53L0_ERROR_NONE) { + vl53l0_errmsg("%d- error status %d\n", __LINE__, Status); + return Status; } - if (Status == VL53L0_ERROR_NONE && data->reset) { - pr_err("Call of VL53L0_PerformRefCalibration\n"); + if (papi_func_tbl->PerformRefCalibration != NULL && data->reset) { + vl53l0_dbgmsg("Call of VL53L0_PerformRefCalibration\n"); Status = papi_func_tbl->PerformRefCalibration(vl53l0_dev, - &(data->VhvSettings), &(data->PhaseCal)); /* Ref calibration */ - - pr_err("VL53L0_PerformRefCalibration %d, %d\n", data->VhvSettings, data->PhaseCal); - } else if (Status == VL53L0_ERROR_NONE && data->reset == 0) { - pr_err("Call of VL53L0_SetRefCalibration\n"); - VL53L0_SetRefCalibration(vl53l0_dev, data->VhvSettings, data->PhaseCal); + &VhvSettings, &PhaseCal); /* Ref calibration */ + if (Status != + VL53L0_ERROR_NONE) { + vl53l0_errmsg( + "%d- error status %d\n", __LINE__, Status); + return Status; + } } + vl53l0_dbgmsg("VHV = %u, PhaseCal = %u\n", VhvSettings, PhaseCal); + vl53l0_dev->VhvSettings = VhvSettings; + vl53l0_dev->PhaseCal = PhaseCal; - if (Status == VL53L0_ERROR_NONE && data->reset) { - pr_err("Call of VL53L0_PerformRefSpadManagement\n"); + if (papi_func_tbl->PerformRefSpadManagement != NULL && data->reset) { + vl53l0_dbgmsg( + "Call of VL53L0_PerformRefSpadManagement\n"); Status = papi_func_tbl->PerformRefSpadManagement(vl53l0_dev, - &(data->refSpadCount), &(data->isApertureSpads)); + &refSpadCount, + &isApertureSpads); /* Ref Spad Management */ + if (Status != VL53L0_ERROR_NONE) { + vl53l0_errmsg( + "%d- error status %d\n", __LINE__, Status); + return Status; + } + } + + vl53l0_dbgmsg( + "SpadCount = %u, isAperature = %u\n", + refSpadCount, isApertureSpads); + vl53l0_dev->refSpadCount = refSpadCount; + vl53l0_dev->isApertureSpads = isApertureSpads; + + if (Status == VL53L0_ERROR_NONE && data->reset) { + if ((papi_func_tbl->SetOffsetCalibrationDataMicroMeter != NULL) + && (vl53l0_dev->setCalibratedValue & + SET_OFFSET_CALIB_DATA_MICROMETER_MASK)) { + vl53l0_dbgmsg( + "Call of SetOffsetCalibrationDataMicroMeter\n"); + Status = + papi_func_tbl->SetOffsetCalibrationDataMicroMeter( + vl53l0_dev, + vl53l0_dev->OffsetMicroMeter); + if (Status != VL53L0_ERROR_NONE) { + vl53l0_errmsg( + "%d- error status %d\n", __LINE__, Status); + return Status; + } + } + } + + if (data->reset) { + if ((papi_func_tbl->SetXTalkCompensationRateMegaCps != NULL) && + (vl53l0_dev->setCalibratedValue & + SET_XTALK_COMP_RATE_MCPS_MASK)) { + vl53l0_dbgmsg( + "Call of SetXTalkCompensationRateMegaCps\n"); + Status = papi_func_tbl->SetXTalkCompensationRateMegaCps( + vl53l0_dev, + vl53l0_dev->XTalkCompensationRateMegaCps); + if (Status != VL53L0_ERROR_NONE) { + vl53l0_errmsg( + "%d- error status %d\n", + __LINE__, Status); + return Status; + } + } + } + + if (data->reset) { + if (vl53l0_dev->setCalibratedValue & + /* Xtalk calibration done*/ + SET_XTALK_COMP_RATE_MCPS_MASK) { + /* Range Ignore Threshold */ + FixPoint1616_t ritValue = 0; + + if (vl53l0_dev->XTalkCompensationRateMegaCps < + 7*65536/10000) { + /*0.7 KCps converted to MCps */ + ritValue = 15 * 7 * 65536/100000; + } else { + ritValue = 15 * + vl53l0_dev->XTalkCompensationRateMegaCps/10; + } + + if (papi_func_tbl->SetLimitCheckEnable != NULL) { + Status = papi_func_tbl->SetLimitCheckEnable( + vl53l0_dev, + VL53L0_CHECKENABLE_RANGE_IGNORE_THRESHOLD, + 1); + if (Status != VL53L0_ERROR_NONE) { + vl53l0_errmsg( + "%d- error status %d\n", __LINE__, + Status); + return Status; + } + } + + if (papi_func_tbl->SetLimitCheckValue != NULL) { + vl53l0_dbgmsg("Set RIT - %u\n", ritValue); + Status = papi_func_tbl->SetLimitCheckValue( + vl53l0_dev, + VL53L0_CHECKENABLE_RANGE_IGNORE_THRESHOLD, + ritValue); + if (Status != VL53L0_ERROR_NONE) { + vl53l0_errmsg( + "%d- error status %d\n", + __LINE__, Status); + return Status; + } + } + } data->reset = 0; - pr_err("VL53L0_PerformRefSpadManagement, %d, %d\n", data->refSpadCount, data->isApertureSpads); - } else if (Status == VL53L0_ERROR_NONE && data->reset == 0) { - pr_err("Call of VL53L0_SetReferenceSpads\n"); - VL53L0_SetReferenceSpads(vl53l0_dev, data->refSpadCount, data->isApertureSpads); - } - if (Status == VL53L0_ERROR_NONE && data->reset) { - - pr_err("Call of papi_func_tbl->SetDeviceMode\n"); - Status = papi_func_tbl->SetDeviceMode(vl53l0_dev, - VL53L0_DEVICEMODE_SINGLE_RANGING); - /* Setup in single ranging mode */ - } - if (Status == VL53L0_ERROR_NONE && data->reset) { - Status = papi_func_tbl->GetLimitCheckValue(vl53l0_dev, - VL53L0_CHECKENABLE_SIGMA_FINAL_RANGE, - &LimitValue); - Status = papi_func_tbl->GetLimitCheckEnable(vl53l0_dev, - VL53L0_CHECKENABLE_SIGMA_FINAL_RANGE, - &LimitEnable); - pr_err("Get LimitCheckValue SIGMA_FINAL_RANGE as:%d,Enable:%d\n", - (LimitValue>>16), LimitEnable); } - if (Status == VL53L0_ERROR_NONE && data->reset) { - Status = papi_func_tbl->GetLimitCheckValue(vl53l0_dev, - VL53L0_CHECKENABLE_SIGNAL_RATE_FINAL_RANGE, - &LimitValue); - Status = papi_func_tbl->GetLimitCheckEnable(vl53l0_dev, - VL53L0_CHECKENABLE_SIGNAL_RATE_FINAL_RANGE, - &LimitEnable); - pr_err("Get LimitCheckValue SIGNAL_FINAL_RANGE as:%d(Fix1616),Eanble:%d\n", - (LimitValue), LimitEnable); + /* Setup in single ranging mode */ + pr_err("Call of VL53L0_SetDeviceMode\n"); + Status = papi_func_tbl->SetDeviceMode(vl53l0_dev, + VL53L0_DEVICEMODE_SINGLE_RANGING); + if (Status != VL53L0_ERROR_NONE) { + vl53l0_errmsg("%d- error status %d\n", __LINE__, Status); + return Status; } - if (Status == VL53L0_ERROR_NONE && data->reset) { - Status = papi_func_tbl->GetLimitCheckValue(vl53l0_dev, - VL53L0_CHECKENABLE_SIGNAL_REF_CLIP, - &LimitValue); - Status = papi_func_tbl->GetLimitCheckEnable(vl53l0_dev, - VL53L0_CHECKENABLE_SIGNAL_REF_CLIP, - &LimitEnable); - pr_err("Get LimitCheckValue SIGNAL_REF_CLIP as:%d(fix1616),Enable:%d\n", - (LimitValue), LimitEnable); + Status = papi_func_tbl->SetWrapAroundCheckEnable(vl53l0_dev, 1); + if (Status != VL53L0_ERROR_NONE) { + vl53l0_errmsg("%d- error status %d\n", __LINE__, Status); + return Status; } - if (Status == VL53L0_ERROR_NONE && data->reset) { - Status = papi_func_tbl->GetLimitCheckValue(vl53l0_dev, - VL53L0_CHECKENABLE_RANGE_IGNORE_THRESHOLD, - &LimitValue); - Status = papi_func_tbl->GetLimitCheckEnable(vl53l0_dev, - VL53L0_CHECKENABLE_RANGE_IGNORE_THRESHOLD, - &LimitEnable); - pr_err("Get LimitCheckValue RANGE_IGNORE_THRESHOLD as:%d(fix1616),Enable:%d\n", - (LimitValue), LimitEnable); - } - - if (Status == VL53L0_ERROR_NONE) - Status = papi_func_tbl->SetWrapAroundCheckEnable(vl53l0_dev, 1); if (data->offset != 0) papi_func_tbl->SetOffsetCalibrationDataMicroMeter( vl53l0_dev, data->offset); @@ -2470,41 +2259,162 @@ static int stmvl53l0_init_client(struct stmvl53l0_data *data) vl53l0_dev, (FixPoint1616_t)data->xtalk); papi_func_tbl->SetXTalkCompensationEnable(vl53l0_dev, 1); -#ifdef CALIBRATION_FILE - /*stmvl53l0_read_calibration_file(data);*/ -#endif - - vl53l0_dbgmsg_en("End\n"); + vl53l0_dbgmsg("End\n"); return 0; } -irqreturn_t laser_isr(int irq, void *dev) +static int stmvl53l0_config_use_case(struct stmvl53l0_data *data) { - struct stmvl53l0_data *data = gp_vl53l0_data; + VL53L0_DEV vl53l0_dev = data; + VL53L0_Error Status = VL53L0_ERROR_NONE; + FixPoint1616_t signalRateLimit; + FixPoint1616_t sigmaLimit; + uint32_t preRangePulsePeriod; + uint32_t finalRangePulsePeriod; - vl53l0_dbgmsg_en("interrupt called"); + vl53l0_dbgmsg("Enter\n"); - if (data->w_mode != OFF_MODE || data->d_mode > 0) { - vl53l0_dbgmsg_en("have mode"); - data->interrupt_received = 1; - cancel_delayed_work(&data->dwork); - schedule_delayed_work(&data->dwork, 0); + switch (vl53l0_dev->useCase) { + + case USE_CASE_LONG_DISTANCE: + sigmaLimit = LONG_DISTANCE_SIGMA_LIMIT; + signalRateLimit = LONG_DISTANCE_SIGNAL_RATE_LIMIT; + preRangePulsePeriod = LONG_DISTANCE_PRE_RANGE_PULSE_PERIOD; + finalRangePulsePeriod = LONG_DISTANCE_FINAL_RANGE_PULSE_PERIOD; + break; + + case USE_CASE_HIGH_ACCURACY: + sigmaLimit = HIGH_ACCURACY_SIGMA_LIMIT; + signalRateLimit = HIGH_ACCURACY_SIGNAL_RATE_LIMIT; + preRangePulsePeriod = HIGH_ACCURACY_PRE_RANGE_PULSE_PERIOD; + finalRangePulsePeriod = HIGH_ACCURACY_FINAL_RANGE_PULSE_PERIOD; + break; + + case USE_CASE_HIGH_SPEED: + sigmaLimit = HIGH_SPEED_SIGMA_LIMIT; + signalRateLimit = HIGH_SPEED_SIGNAL_RATE_LIMIT; + preRangePulsePeriod = HIGH_SPEED_PRE_RANGE_PULSE_PERIOD; + finalRangePulsePeriod = HIGH_SPEED_FINAL_RANGE_PULSE_PERIOD; + break; + + case USE_CASE_CUSTOM: + /* Set by application through IOCTL interface */ + sigmaLimit = vl53l0_dev->sigmaLimit; + signalRateLimit = vl53l0_dev->signalRateLimit; + preRangePulsePeriod = vl53l0_dev->preRangePulsePeriod; + finalRangePulsePeriod = vl53l0_dev->finalRangePulsePeriod; + break; + + default: + vl53l0_errmsg( + "Invalid use case = %d\n", vl53l0_dev->useCase); + /* Invalid parameter, should not reach here */ + return -EINVAL; } - return IRQ_HANDLED; + vl53l0_dbgmsg( + "Configure UseCase(%d) : Sigma=%u,Signal=%u,Pre=%u,Final=%u,timingBudget=%u\n", + vl53l0_dev->useCase, + sigmaLimit, + signalRateLimit, + preRangePulsePeriod, + finalRangePulsePeriod, + vl53l0_dev->timingBudget); + + if (papi_func_tbl->SetLimitCheckEnable != NULL) { + Status = papi_func_tbl->SetLimitCheckEnable( + vl53l0_dev, + VL53L0_CHECKENABLE_SIGMA_FINAL_RANGE, + 1); + } + + if (Status == VL53L0_ERROR_NONE) { + Status = papi_func_tbl->SetLimitCheckEnable( + vl53l0_dev, + VL53L0_CHECKENABLE_SIGNAL_RATE_FINAL_RANGE, + 1); + } else { + vl53l0_errmsg( + "SetLimitCheckEnable(SIGMA_FINAL_RANGE) failed with errcode = %d\n", + Status); + } + + if (Status == VL53L0_ERROR_NONE) { + Status = papi_func_tbl->SetLimitCheckValue( + vl53l0_dev, + VL53L0_CHECKENABLE_SIGNAL_RATE_FINAL_RANGE, + signalRateLimit); + } else { + vl53l0_errmsg( + "SetLimitCheckEnable(SIGNAL_RATE_FINAL_RANGE) failed with errcode = %d\n", + Status); + } + + if (Status == VL53L0_ERROR_NONE) { + Status = papi_func_tbl->SetLimitCheckValue(vl53l0_dev, + VL53L0_CHECKENABLE_SIGMA_FINAL_RANGE, + sigmaLimit); + } else { + vl53l0_dbgmsg( + "SIGNAL_RATE_FINAL_RANGE failed with errcode = %d\n", + Status); + } + + if (Status == VL53L0_ERROR_NONE) { + Status = + papi_func_tbl->SetMeasurementTimingBudgetMicroSeconds( + vl53l0_dev, + vl53l0_dev->timingBudget); + } else { + vl53l0_dbgmsg( + "SIGMA_FINAL_RANGE failed with errcode = %d\n", + Status); + } + + + if (Status == VL53L0_ERROR_NONE) { + Status = papi_func_tbl->SetVcselPulsePeriod(vl53l0_dev, + VL53L0_VCSEL_PERIOD_PRE_RANGE, + preRangePulsePeriod); + } else { + vl53l0_dbgmsg( + "SetMeasurementTimingBudget failed with errcode = %d\n", + Status); + } + + if (Status == VL53L0_ERROR_NONE) { + Status = papi_func_tbl->SetVcselPulsePeriod(vl53l0_dev, + VL53L0_VCSEL_PERIOD_FINAL_RANGE, + finalRangePulsePeriod); + } else + vl53l0_dbgmsg( + "SetVcselPulsePeriod(PRE) failed with errcode = %d\n", Status); + + if (Status != VL53L0_ERROR_NONE) + vl53l0_dbgmsg( + "SetVcselPulsePeriod(FINAL)failed with errcode = %d\n", Status); + + vl53l0_dbgmsg("End\n"); + return Status; } -static int stmvl53l0_start(struct stmvl53l0_data *data) + +static int stmvl53l0_start(struct stmvl53l0_data *data, uint8_t scaling, + init_mode_e mode) { int rc = 0; - int reset = 0; + VL53L0_DEV vl53l0_dev = data; + VL53L0_Error Status = VL53L0_ERROR_NONE; + + vl53l0_errmsg("Enter\n"); /* Power up */ - rc = pmodule_func_tbl->power_up(data->client_object, &reset); + rc = pmodule_func_tbl->power_up(data->client_object, &data->reset); if (rc) { vl53l0_errmsg("%d,error rc %d\n", __LINE__, rc); return rc; } + /* init */ rc = stmvl53l0_init_client(data); if (rc) { @@ -2513,163 +2423,319 @@ static int stmvl53l0_start(struct stmvl53l0_data *data) return -EINVAL; } + /* check mode */ + if (mode != NORMAL_MODE) + papi_func_tbl->SetXTalkCompensationEnable(vl53l0_dev, 1); + + if (mode == OFFSETCALIB_MODE) { + /*VL53L0_SetOffsetCalibrationDataMicroMeter(vl53l0_dev, 0);*/ + FixPoint1616_t OffsetMicroMeter; + + papi_func_tbl->PerformOffsetCalibration(vl53l0_dev, + (data->offsetCalDistance<<16), + &OffsetMicroMeter); + pr_err("Offset calibration:%u\n", OffsetMicroMeter); + vl53l0_dev->OffsetMicroMeter = OffsetMicroMeter; + vl53l0_dev->setCalibratedValue |= + SET_OFFSET_CALIB_DATA_MICROMETER_MASK; + + return rc; + } else if (mode == XTALKCALIB_MODE) { + FixPoint1616_t XTalkCompensationRateMegaCps; + /*caltarget distance : 100mm and convert to + * fixed point 16 16 format + */ + papi_func_tbl->PerformXTalkCalibration(vl53l0_dev, + (data->xtalkCalDistance<<16), + &XTalkCompensationRateMegaCps); + pr_err("Xtalk calibration:%u\n", + XTalkCompensationRateMegaCps); + vl53l0_dev->XTalkCompensationRateMegaCps = + XTalkCompensationRateMegaCps; + vl53l0_dev->setCalibratedValue |= + SET_XTALK_COMP_RATE_MCPS_MASK; + + return rc; + } + + /* set up device parameters */ + data->gpio_polarity = VL53L0_INTERRUPTPOLARITY_LOW; + + /* Following two calls are made from IOCTL as well */ + Status = papi_func_tbl->SetGpioConfig(vl53l0_dev, 0, 0, + data->gpio_function, + VL53L0_INTERRUPTPOLARITY_LOW); + if (Status != VL53L0_ERROR_NONE) { + vl53l0_errmsg("Failed to SetGpioConfig. Error = %d\n", Status); + return -EPERM; + } + + Status = papi_func_tbl->SetInterruptThresholds(vl53l0_dev, 0, + data->low_threshold, + data->high_threshold); + if (Status != VL53L0_ERROR_NONE) { + vl53l0_errmsg( + "Failed to SetInterruptThresholds. Error = %d\n", Status); + return -EPERM; + } + + if (data->deviceMode == VL53L0_DEVICEMODE_CONTINUOUS_TIMED_RANGING) { + Status = papi_func_tbl->SetInterMeasurementPeriodMilliSeconds( + vl53l0_dev, + data->interMeasurems); + if (Status != VL53L0_ERROR_NONE) { + vl53l0_errmsg( + "Failed to SetInterMeasurementPeriodMilliSeconds. Error = %d\n", + Status); + return -EPERM; + } + pr_err( + "DeviceMode:0x%x, interMeasurems:%d==\n", + data->deviceMode, + data->interMeasurems); + } + + Status = papi_func_tbl->SetDeviceMode( + vl53l0_dev, + data->deviceMode); + if (Status != VL53L0_ERROR_NONE) { + vl53l0_errmsg("Failed to SetDeviceMode. Error = %d\n", Status); + return -EPERM; + } + + Status = papi_func_tbl->ClearInterruptMask(vl53l0_dev, + 0); + if (Status != VL53L0_ERROR_NONE) { + vl53l0_errmsg( + "Failed to ClearInterruptMask. Error = %d\n", Status); + return -EPERM; + } + + Status = stmvl53l0_config_use_case(vl53l0_dev); + if (Status != VL53L0_ERROR_NONE) { + vl53l0_errmsg( + "Failed to configure Use case = %u\n", + vl53l0_dev->useCase); + return -EPERM; + } + + /* start the ranging */ + Status = papi_func_tbl->StartMeasurement(vl53l0_dev); + if (Status != VL53L0_ERROR_NONE) { + vl53l0_errmsg( + "Failed to StartMeasurement. Error = %d\n", Status); + return -EPERM; + } + data->enable_ps_sensor = 1; - schedule_delayed_work(&data->checkwork, 1800000); - vl53l0_dbgmsg_en("End\n"); + +#ifndef USE_INT + /* Unblock the thread execution */ + wake_up(&vl53l0_dev->poll_thread_wq); +#endif + + vl53l0_dbgmsg("End\n"); return rc; } +VL53L0_Error WaitStopCompleted(VL53L0_DEV Dev) +{ + VL53L0_Error Status = VL53L0_ERROR_NONE; + uint32_t StopCompleted = 0; + uint32_t LoopNb; + + vl53l0_dbgmsg("Enter\n"); + + /* Wait until it finished + * use timeout to avoid deadlock + */ + if (Status == VL53L0_ERROR_NONE) { + LoopNb = 0; + do { + Status = papi_func_tbl->GetStopCompletedStatus(Dev, + &StopCompleted); + if ((StopCompleted == 0x00) || + (Status != VL53L0_ERROR_NONE)) + break; + + LoopNb = LoopNb + 1; + VL53L0_PollingDelay(Dev); + } while (LoopNb < VL53L0_DEFAULT_MAX_LOOP); + + if (LoopNb >= VL53L0_DEFAULT_MAX_LOOP) + Status = VL53L0_ERROR_TIME_OUT; + + } + + vl53l0_dbgmsg("End WaitStopCompleted - %d\n", Status); + return Status; +} + static int stmvl53l0_stop(struct stmvl53l0_data *data) { int rc = 0; + VL53L0_DEV vl53l0_dev = data; - vl53l0_dbgmsg_en("Enter\n"); - VL53L0_StopMeasurement(data); + vl53l0_errmsg("Enter\n"); + + /* stop - if continuous mode */ + if (data->deviceMode == VL53L0_DEVICEMODE_CONTINUOUS_RANGING || + data->deviceMode == VL53L0_DEVICEMODE_CONTINUOUS_TIMED_RANGING) + papi_func_tbl->StopMeasurement(vl53l0_dev); /* clean interrupt */ - VL53L0_ClearInterruptMask(data, 0); + papi_func_tbl->ClearInterruptMask(vl53l0_dev, 0); + + + WaitStopCompleted(vl53l0_dev); + /* cancel work handler */ stmvl53l0_cancel_handler(data); - data->enable_ps_sensor = 0; + + /* Clear updateUseCase pending operation */ + data->updateUseCase = 0; /* power down */ rc = pmodule_func_tbl->power_down(data->client_object); if (rc) { vl53l0_errmsg("%d, error rc %d\n", __LINE__, rc); return rc; } - vl53l0_dbgmsg_en("End\n"); - + vl53l0_dbgmsg("End\n"); return rc; } +static void stmvl53l0_timer_fn(unsigned long data) +{ + VL53L0_DEV vl53l0_dev = (VL53L0_DEV)data; + + vl53l0_dev->flushCount++; + + input_report_abs(vl53l0_dev->input_dev_ps, ABS_GAS, + vl53l0_dev->flushCount); + + + input_sync(vl53l0_dev->input_dev_ps); + + vl53l0_dbgmsg("Sensor HAL Flush Count = %u\n", vl53l0_dev->flushCount); +} + /* * I2C init/probing/exit functions */ static const struct file_operations stmvl53l0_ranging_fops = { .owner = THIS_MODULE, - .unlocked_ioctl = stmvl53l0_ioctl, + .unlocked_ioctl = stmvl53l0_ioctl, #ifdef CONFIG_COMPAT - .compat_ioctl = stmvl53l0_compat_ioctl, + .compat_ioctl = stmvl53l0_compat_ioctl, #endif .open = stmvl53l0_open, .flush = stmvl53l0_flush, - .release = stmvl53l0_close, }; -/* -static struct miscdevice stmvl53l0_ranging_dev = { - .minor = MISC_DYNAMIC_MINOR, - .name = "stmvl53l0_ranging", - .fops = &stmvl53l0_ranging_fops -}; -*/ - -int stmvl53l0_setup(struct stmvl53l0_data *data, uint8_t type) +int stmvl53l0_setup(struct stmvl53l0_data *data) { int rc = 0; - int irq; + +#ifdef USE_INT + int irq = 0; int gpio; +#endif vl53l0_dbgmsg("Enter\n"); - data->lowv = 40; - data->highv = 45; - if (type == CCI_BUS) { - data->bus_type = CCI_BUS; - data->client_object = &(data->cci_client_object); - /* assign function table */ - pmodule_func_tbl = &stmvl53l0_module_func_tbl; - gpio = data->cci_client_object.gconf.cam_gpio_req_tbl[2].gpio; - data->lowv = data->cci_client_object.lowv; - data->highv = data->cci_client_object.highv; - data->xtalk = data->cci_client_object.xtalk; - } else { - data->bus_type = I2C_BUS; - data->client_object = &(data->i2c_client_object); - pmodule_func_tbl = &stmvl53l0_module_func_tbl; - gpio = data->i2c_client_object.gconf.cam_gpio_req_tbl[2].gpio; - data->lowv = data->i2c_client_object.lowv; - data->highv = data->i2c_client_object.highv; - data->xtalk = data->i2c_client_object.xtalk; - } + + /* init mutex */ + mutex_init(&data->update_lock); + mutex_init(&data->work_mutex); + +#ifdef USE_INT /* init interrupt */ + gpio = data->client_object.gconf.cam_gpio_req_tbl[2].gpio; gpio_request(gpio, "vl53l0_gpio_int"); vl53l0_dbgmsg("gpio_request %d to register_irq\n", gpio); gpio_direction_input(gpio); irq = gpio_to_irq(gpio); - - if (irq < 0) { vl53l0_errmsg("%d,filed to map GPIO: %d to interrupt:%d\n", __LINE__, gpio, irq); } else { - vl53l0_dbgmsg("register_irq:%d\n", irq); -/* IRQF_TRIGGER_FALLING- poliarity:0 IRQF_TRIGGER_RISNG - poliarty:1 */ - rc = request_irq(irq, laser_isr, - IRQF_TRIGGER_FALLING , - "vl6180_lb_gpio_int", (void *)data); + /* IRQF_TRIGGER_FALLING- poliarity:0 IRQF_TRIGGER_RISNG - + * poliarty:1 + */ + rc = request_threaded_irq(irq, NULL, + stmvl53l0_interrupt_handler, + IRQF_TRIGGER_FALLING|IRQF_ONESHOT, + "vl53l0_interrupt", + (void *)data); if (rc) { - vl53l0_errmsg("%d, Could not allocate INT %d\n", - __LINE__, rc); - goto exit_free_irq; + vl53l0_errmsg("%d, Could not allocate STMVL53L0_INT ! result:%d\n", __LINE__, rc); + free_irq(irq, data); + goto exit_free_irq; } } data->irq = irq; - enable_irq_wake(irq); - vl53l0_dbgmsg("interrupt is hooked\n"); - /* init mutex */ - mutex_init(&data->update_lock); - mutex_init(&data->work_mutex); - data->w_mode = OFF_MODE; - init_waitqueue_head(&data->range_data_wait); - s_nump = 0; + vl53l0_errmsg("interrupt is hooked\n"); +#else + + init_waitqueue_head(&data->poll_thread_wq); + + data->poll_thread = kthread_run(&stmvl53l0_poll_thread, + (void *)data, + "STM-VL53L0"); + if (data->poll_thread == NULL) { + pr_err( + "%s(%d) - Failed to create Polling thread\n", __func__, __LINE__); + goto exit_free_irq; + } +#endif + /* init work handler */ INIT_DELAYED_WORK(&data->dwork, stmvl53l0_work_handler); - INIT_DELAYED_WORK(&data->initwork, stmvl53l0_init_handler); - INIT_DELAYED_WORK(&data->resetwork, stmvl53l0_reset_handler); - INIT_DELAYED_WORK(&data->checkwork, stmvl53l0_check_handler); + /* Register to Input Device */ data->input_dev_ps = input_allocate_device(); if (!data->input_dev_ps) { rc = -ENOMEM; vl53l0_errmsg("%d error:%d\n", __LINE__, rc); - goto exit_free_irq; } set_bit(EV_ABS, data->input_dev_ps->evbit); /* range in cm*/ - input_set_abs_params( - data->input_dev_ps, ABS_DISTANCE, 0, 76, 0, 0); + input_set_abs_params(data->input_dev_ps, ABS_DISTANCE, 0, 76, 0, 0); /* tv_sec */ - input_set_abs_params( - data->input_dev_ps, ABS_HAT0X, 0, 0xffffffff, 0, 0); + input_set_abs_params(data->input_dev_ps, ABS_HAT0X, 0, 0xffffffff, + 0, 0); /* tv_usec */ - input_set_abs_params( - data->input_dev_ps, ABS_HAT0Y, 0, 0xffffffff, 0, 0); + input_set_abs_params(data->input_dev_ps, ABS_HAT0Y, 0, 0xffffffff, + 0, 0); /* range in_mm */ - input_set_abs_params( - data->input_dev_ps, ABS_HAT1X, 0, 765, 0, 0); + input_set_abs_params(data->input_dev_ps, ABS_HAT1X, 0, 765, 0, 0); /* error code change maximum to 0xff for more flexibility */ - input_set_abs_params( - data->input_dev_ps, ABS_HAT1Y, 0, 0xff, 0, 0); + input_set_abs_params(data->input_dev_ps, ABS_HAT1Y, 0, 0xff, 0, 0); /* rtnRate */ - input_set_abs_params( - data->input_dev_ps, ABS_HAT2X, 0, 0xffffffff, 0, 0); + input_set_abs_params(data->input_dev_ps, ABS_HAT2X, 0, 0xffffffff, + 0, 0); /* rtn_amb_rate */ - input_set_abs_params( - data->input_dev_ps, ABS_HAT2Y, 0, 0xffffffff, 0, 0); + input_set_abs_params(data->input_dev_ps, ABS_HAT2Y, 0, 0xffffffff, + 0, 0); /* rtn_conv_time */ - input_set_abs_params( - data->input_dev_ps, ABS_HAT3X, 0, 0xffffffff, 0, 0); + input_set_abs_params(data->input_dev_ps, ABS_HAT3X, 0, 0xffffffff, + 0, 0); /* dmax */ - input_set_abs_params( - data->input_dev_ps, ABS_HAT3Y, 0, 0xffffffff, 0, 0); - /* misc */ - input_set_abs_params( - data->input_dev_ps, ABS_MISC, 0, 0xff, 0, 0); - data->input_dev_ps->name = "Rear proximity sensor"; + input_set_abs_params(data->input_dev_ps, ABS_HAT3Y, 0, 0xffffffff, + 0, 0); + + input_set_abs_params(data->input_dev_ps, ABS_MISC, 0, 0xffffffff, + 0, 0); + + input_set_abs_params(data->input_dev_ps, ABS_WHEEL, 0, 0xffffffff, + 0, 0); + + input_set_abs_params(data->input_dev_ps, ABS_GAS, 0, 0xffffffff, + 0, 0); + data->input_dev_ps->name = "STM VL53L0 proximity sensor"; rc = input_register_device(data->input_dev_ps); if (rc) { @@ -2677,44 +2743,56 @@ int stmvl53l0_setup(struct stmvl53l0_data *data, uint8_t type) vl53l0_errmsg("%d error:%d\n", __LINE__, rc); goto exit_free_dev_ps; } + /* setup drv data */ + input_set_drvdata(data->input_dev_ps, data); /* Register sysfs hooks */ data->range_kobj = kobject_create_and_add("range", kernel_kobj); if (!data->range_kobj) { rc = -ENOMEM; vl53l0_errmsg("%d error:%d\n", __LINE__, rc); - goto exit_unregister_dev_ps; } - rc = sysfs_create_group(data->range_kobj, &stmvl53l0_attr_group); + rc = sysfs_create_group(&data->input_dev_ps->dev.kobj, + &stmvl53l0_attr_group); if (rc) { rc = -ENOMEM; vl53l0_errmsg("%d error:%d\n", __LINE__, rc); - goto exit_unregister_dev_ps_1; } + setup_timer(&data->timer, stmvl53l0_timer_fn, (unsigned long)data); + /* to register as a misc device */ data->miscdev.minor = MISC_DYNAMIC_MINOR; data->dev_name = devname; data->miscdev.name = data->dev_name; data->miscdev.fops = &stmvl53l0_ranging_fops; - + vl53l0_errmsg("Misc device registration name:%s\n", data->dev_name); if (misc_register(&data->miscdev) != 0) - vl53l0_errmsg("%d,Couldn't register misc dev\n", __LINE__); + vl53l0_errmsg("Could not register misc. dev for stmvl53l0 ranging\n"); - /* init default value */ + /* init default device parameter value */ data->enable_ps_sensor = 0; data->reset = 1; - data->c_suspend = 0; - data->c_stopped = 0; - data->delay_ms = 2000; /* delay time to 2s */ + data->delay_ms = 30; /* delay time to 30ms */ data->enableDebug = 0; - data->pll_p = 18; - data->pll_f = 14; -/* data->client_object.power_up = 0; */ + data->gpio_polarity = VL53L0_INTERRUPTPOLARITY_LOW; + data->gpio_function = VL53L0_GPIOFUNCTIONALITY_NEW_MEASURE_READY; + data->low_threshold = 60; + data->high_threshold = 200; + data->deviceMode = VL53L0_DEVICEMODE_SINGLE_RANGING; + data->interMeasurems = 30; + data->useCase = USE_CASE_LONG_DISTANCE; + data->timingBudget = LONG_DISTANCE_TIMING_BUDGET; - vl53l0_dbgmsg("support ver. %s enabled\n", DRIVER_VERSION); + /* Set default values used in Custom Mode Use Case */ + data->signalRateLimit = LONG_DISTANCE_SIGNAL_RATE_LIMIT; + data->sigmaLimit = LONG_DISTANCE_SIGMA_LIMIT; + data->preRangePulsePeriod = LONG_DISTANCE_PRE_RANGE_PULSE_PERIOD; + data->finalRangePulsePeriod = LONG_DISTANCE_FINAL_RANGE_PULSE_PERIOD; + + vl53l0_errmsg("support ver. %s enabled\n", DRIVER_VERSION); vl53l0_dbgmsg("End"); return 0; @@ -2722,46 +2800,41 @@ exit_unregister_dev_ps_1: kobject_put(data->range_kobj); exit_unregister_dev_ps: input_unregister_device(data->input_dev_ps); - exit_free_dev_ps: input_free_device(data->input_dev_ps); - exit_free_irq: - +#ifdef USE_INT free_irq(irq, data); +#endif kfree(data); return rc; } +void stmvl53l0_cleanup(struct stmvl53l0_data *data) +{ +#ifndef USE_INT + pr_err("%s(%d) : Stop poll_thread\n", __func__, __LINE__); + poll_thread_exit = 1; + kthread_stop(data->poll_thread); +#endif +} + static int __init stmvl53l0_init(void) { - struct stmvl53l0_data *vl53l0_data = NULL; - int ret = 0; + int ret = -1; vl53l0_dbgmsg("Enter\n"); - if (gp_vl53l0_data == NULL) { - vl53l0_data = - kzalloc(sizeof(struct stmvl53l0_data), GFP_KERNEL); - if (!vl53l0_data) { - vl53l0_errmsg("%d failed no memory\n", __LINE__); - return -ENOMEM; - } - /* assign to global variable */ - gp_vl53l0_data = vl53l0_data; - } else { - vl53l0_data = gp_vl53l0_data; - } /* assign function table */ + pmodule_func_tbl = &stmvl53l0_module_func_tbl; papi_func_tbl = &stmvl53l0_api_func_tbl; - /* client specific init function */ - stmvl53l0_module_func_tbl.init(); - if (ret) { - kfree(vl53l0_data); - gp_vl53l0_data = NULL; + /* client specific init function */ + ret = pmodule_func_tbl->init(); + + if (ret) vl53l0_errmsg("%d failed with %d\n", __LINE__, ret); - } + vl53l0_dbgmsg("End\n"); return ret; @@ -2769,36 +2842,15 @@ static int __init stmvl53l0_init(void) static void __exit stmvl53l0_exit(void) { - vl53l0_dbgmsg_en("Enter\n"); -#if 0 - if (gp_vl53l0_data) { - input_unregister_device(gp_vl53l0_data->input_dev_ps); - input_free_device(gp_vl53l0_data->input_dev_ps); -#ifdef USE_INT - free_irq(data->irq, gp_vl53l0_data); -#endif - sysfs_remove_group( - gp_vl53l0_data->range_kobj, &stmvl53l0_attr_group); - gp_vl53l0_data->pmodule_func_tbl->deinit( - gp_vl53l0_data->client_object); - kfree(gp_vl53l0_data); - gp_vl53l0_data = NULL; - } -#endif - vl53l0_dbgmsg_en("End\n"); + vl53l0_dbgmsg("Enter\n"); + + vl53l0_dbgmsg("End\n"); } - -struct stmvl53l0_data *stmvl53l0_getobject(void) -{ - return gp_vl53l0_data; -} - - MODULE_AUTHOR("STMicroelectronics Imaging Division"); MODULE_DESCRIPTION("ST FlightSense Time-of-Flight sensor driver"); MODULE_LICENSE("GPL"); MODULE_VERSION(DRIVER_VERSION); -late_initcall(stmvl53l0_init); +module_init(stmvl53l0_init); module_exit(stmvl53l0_exit);