The VL53L1CB proximity sensor, based on ST’s FlightSense™, Time-of-Flight technology.

Dependencies:   X_NUCLEO_COMMON ST_INTERFACES

Dependents:   VL53L1CB_noshield_1sensor_polls_auton VL53L1CB_noshield_1sensor_interrupt_auton X_NUCLEO_53L1A2

Based on VL53L1 library, this is a library for the VL53L1CB ToF chip.

Committer:
Charles MacNeill
Date:
Tue Jun 08 10:34:47 2021 +0100
Revision:
7:1add29d51e72
Parent:
1:76429facbf6f
Child:
18:0696efe39d08
Update to v6.6.5 of bare_driver

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Charles MacNeill 7:1add29d51e72 1
Charles MacNeill 7:1add29d51e72 2 // SPDX-License-Identifier: GPL-2.0+ OR BSD-3-Clause
Charles MacNeill 7:1add29d51e72 3 /******************************************************************************
Charles MacNeill 7:1add29d51e72 4 * Copyright (c) 2020, STMicroelectronics - All Rights Reserved
Charles MacNeill 7:1add29d51e72 5
Charles MacNeill 7:1add29d51e72 6 This file is part of VL53L1 and is dual licensed,
Charles MacNeill 7:1add29d51e72 7 either GPL-2.0+
Charles MacNeill 7:1add29d51e72 8 or 'BSD 3-clause "New" or "Revised" License' , at your option.
Charles MacNeill 7:1add29d51e72 9 ******************************************************************************
Charles MacNeill 7:1add29d51e72 10 */
Charles MacNeill 7:1add29d51e72 11
Charles MacNeill 7:1add29d51e72 12
Charles MacNeill 7:1add29d51e72 13
Charles MacNeill 7:1add29d51e72 14
Charles MacNeill 7:1add29d51e72 15 #include "vl53l1_api.h"
Charles MacNeill 7:1add29d51e72 16 #include "vl53l1_api_strings.h"
Charles MacNeill 7:1add29d51e72 17 #include "vl53l1_register_settings.h"
Charles MacNeill 7:1add29d51e72 18 #include "vl53l1_register_funcs.h"
Charles MacNeill 7:1add29d51e72 19 #include "vl53l1_core.h"
Charles MacNeill 7:1add29d51e72 20 #include "vl53l1_api_calibration.h"
Charles MacNeill 7:1add29d51e72 21 #include "vl53l1_wait.h"
Charles MacNeill 7:1add29d51e72 22 #include "vl53l1_preset_setup.h"
Charles MacNeill 7:1add29d51e72 23 #include "vl53l1_api_debug.h"
Charles MacNeill 7:1add29d51e72 24 #include "vl53l1_api_core.h"
Charles MacNeill 7:1add29d51e72 25 #include "vl53l1_nvm.h"
Charles MacNeill 7:1add29d51e72 26
Charles MacNeill 7:1add29d51e72 27
Charles MacNeill 7:1add29d51e72 28 #define ZONE_CHECK VL53L1_MAX_USER_ZONES
Charles MacNeill 7:1add29d51e72 29
Charles MacNeill 7:1add29d51e72 30 #if ZONE_CHECK < 5
Charles MacNeill 7:1add29d51e72 31 #error Must define at least 5 zones in MAX_USER_ZONES constant
Charles MacNeill 7:1add29d51e72 32 #endif
Charles MacNeill 7:1add29d51e72 33
Charles MacNeill 7:1add29d51e72 34 #define LOG_FUNCTION_START(fmt, ...) \
Charles MacNeill 7:1add29d51e72 35 _LOG_FUNCTION_START(VL53L1_TRACE_MODULE_API, fmt, ##__VA_ARGS__)
Charles MacNeill 7:1add29d51e72 36 #define LOG_FUNCTION_END(status, ...) \
Charles MacNeill 7:1add29d51e72 37 _LOG_FUNCTION_END(VL53L1_TRACE_MODULE_API, status, ##__VA_ARGS__)
Charles MacNeill 7:1add29d51e72 38 #define LOG_FUNCTION_END_FMT(status, fmt, ...) \
Charles MacNeill 7:1add29d51e72 39 _LOG_FUNCTION_END_FMT(VL53L1_TRACE_MODULE_API, status, \
Charles MacNeill 7:1add29d51e72 40 fmt, ##__VA_ARGS__)
Charles MacNeill 7:1add29d51e72 41
Charles MacNeill 7:1add29d51e72 42 #ifdef VL53L1_LOG_ENABLE
Charles MacNeill 7:1add29d51e72 43 #define trace_print(level, ...) trace_print_module_function(\
Charles MacNeill 7:1add29d51e72 44 VL53L1_TRACE_MODULE_API, level, VL53L1_TRACE_FUNCTION_NONE, \
Charles MacNeill 7:1add29d51e72 45 ##__VA_ARGS__)
Charles MacNeill 7:1add29d51e72 46 #endif
Charles MacNeill 7:1add29d51e72 47
Charles MacNeill 7:1add29d51e72 48 #ifndef MIN
Charles MacNeill 7:1add29d51e72 49 #define MIN(v1, v2) ((v1) < (v2) ? (v1) : (v2))
Charles MacNeill 7:1add29d51e72 50 #endif
Charles MacNeill 7:1add29d51e72 51 #ifndef MAX
Charles MacNeill 7:1add29d51e72 52 #define MAX(v1, v2) ((v1) < (v2) ? (v2) : (v1))
Charles MacNeill 7:1add29d51e72 53 #endif
Charles MacNeill 7:1add29d51e72 54
Charles MacNeill 7:1add29d51e72 55 #define DMAX_REFLECTANCE_IDX 2
Charles MacNeill 7:1add29d51e72 56
Charles MacNeill 7:1add29d51e72 57
Charles MacNeill 7:1add29d51e72 58
Charles MacNeill 7:1add29d51e72 59 #define LOWPOWER_AUTO_VHV_LOOP_DURATION_US 245
Charles MacNeill 7:1add29d51e72 60 #define LOWPOWER_AUTO_OVERHEAD_BEFORE_A_RANGING 1448
Charles MacNeill 7:1add29d51e72 61 #define LOWPOWER_AUTO_OVERHEAD_BETWEEN_A_B_RANGING 2100
Charles MacNeill 7:1add29d51e72 62
Charles MacNeill 7:1add29d51e72 63 #define FDA_MAX_TIMING_BUDGET_US 550000
Charles MacNeill 7:1add29d51e72 64
Charles MacNeill 7:1add29d51e72 65
Charles MacNeill 7:1add29d51e72 66
Charles MacNeill 7:1add29d51e72 67
Charles MacNeill 7:1add29d51e72 68
Charles MacNeill 7:1add29d51e72 69
Charles MacNeill 7:1add29d51e72 70 static int32_t BDTable[VL53L1_TUNING_MAX_TUNABLE_KEY] = {
Charles MacNeill 7:1add29d51e72 71 TUNING_VERSION,
Charles MacNeill 7:1add29d51e72 72 TUNING_PROXY_MIN,
Charles MacNeill 7:1add29d51e72 73 TUNING_SINGLE_TARGET_XTALK_TARGET_DISTANCE_MM,
Charles MacNeill 7:1add29d51e72 74 TUNING_SINGLE_TARGET_XTALK_SAMPLE_NUMBER,
Charles MacNeill 7:1add29d51e72 75 TUNING_MIN_AMBIENT_DMAX_VALID,
Charles MacNeill 7:1add29d51e72 76 TUNING_MAX_SIMPLE_OFFSET_CALIBRATION_SAMPLE_NUMBER,
Charles MacNeill 7:1add29d51e72 77 TUNING_XTALK_FULL_ROI_TARGET_DISTANCE_MM,
Charles MacNeill 7:1add29d51e72 78 TUNING_SIMPLE_OFFSET_CALIBRATION_REPEAT,
Charles MacNeill 7:1add29d51e72 79 TUNING_XTALK_FULL_ROI_BIN_SUM_MARGIN,
Charles MacNeill 7:1add29d51e72 80 TUNING_XTALK_FULL_ROI_DEFAULT_OFFSET,
Charles MacNeill 7:1add29d51e72 81 TUNING_ZERO_DISTANCE_OFFSET_NON_LINEAR_FACTOR_DEFAULT,
Charles MacNeill 7:1add29d51e72 82 };
Charles MacNeill 7:1add29d51e72 83
Charles MacNeill 7:1add29d51e72 84
Charles MacNeill 7:1add29d51e72 85 static VL53L1_Error SingleTargetXTalkCalibration(VL53L1_DEV Dev)
Charles MacNeill 7:1add29d51e72 86 {
Charles MacNeill 7:1add29d51e72 87 VL53L1_Error Status = VL53L1_ERROR_NONE;
Charles MacNeill 7:1add29d51e72 88
Charles MacNeill 7:1add29d51e72 89 uint32_t sum_ranging = 0;
Charles MacNeill 7:1add29d51e72 90 uint32_t sum_spads = 0;
Charles MacNeill 7:1add29d51e72 91 FixPoint1616_t sum_signalRate = 0;
Charles MacNeill 7:1add29d51e72 92 FixPoint1616_t total_count = 0;
Charles MacNeill 7:1add29d51e72 93 uint8_t xtalk_meas = 0;
Charles MacNeill 7:1add29d51e72 94 uint8_t xtalk_measmax =
Charles MacNeill 7:1add29d51e72 95 BDTable[VL53L1_TUNING_SINGLE_TARGET_XTALK_SAMPLE_NUMBER];
Charles MacNeill 7:1add29d51e72 96 VL53L1_RangingMeasurementData_t RMData;
Charles MacNeill 7:1add29d51e72 97 FixPoint1616_t xTalkStoredMeanSignalRate;
Charles MacNeill 7:1add29d51e72 98 FixPoint1616_t xTalkStoredMeanRange;
Charles MacNeill 7:1add29d51e72 99 FixPoint1616_t xTalkStoredMeanRtnSpads;
Charles MacNeill 7:1add29d51e72 100 uint32_t xTalkStoredMeanRtnSpadsAsInt;
Charles MacNeill 7:1add29d51e72 101 uint32_t xTalkCalDistanceAsInt;
Charles MacNeill 7:1add29d51e72 102 FixPoint1616_t XTalkCompensationRateMegaCps;
Charles MacNeill 7:1add29d51e72 103 uint32_t signalXTalkTotalPerSpad;
Charles MacNeill 7:1add29d51e72 104 VL53L1_PresetModes PresetMode;
Charles MacNeill 7:1add29d51e72 105 VL53L1_CalibrationData_t CalibrationData;
Charles MacNeill 7:1add29d51e72 106 VL53L1_CustomerNvmManaged_t *pC;
Charles MacNeill 7:1add29d51e72 107
Charles MacNeill 7:1add29d51e72 108
Charles MacNeill 7:1add29d51e72 109 LOG_FUNCTION_START("");
Charles MacNeill 7:1add29d51e72 110
Charles MacNeill 7:1add29d51e72 111
Charles MacNeill 7:1add29d51e72 112 PresetMode = VL53L1DevDataGet(Dev, CurrentParameters.PresetMode);
Charles MacNeill 7:1add29d51e72 113
Charles MacNeill 7:1add29d51e72 114 if ((PresetMode != VL53L1_PRESETMODE_AUTONOMOUS) &&
Charles MacNeill 7:1add29d51e72 115 (PresetMode != VL53L1_PRESETMODE_LOWPOWER_AUTONOMOUS) &&
Charles MacNeill 7:1add29d51e72 116 (PresetMode != VL53L1_PRESETMODE_LITE_RANGING)) {
Charles MacNeill 7:1add29d51e72 117 Status = VL53L1_ERROR_MODE_NOT_SUPPORTED;
Charles MacNeill 7:1add29d51e72 118 goto ENDFUNC;
Charles MacNeill 7:1add29d51e72 119 }
Charles MacNeill 7:1add29d51e72 120
Charles MacNeill 7:1add29d51e72 121
Charles MacNeill 7:1add29d51e72 122 Status = VL53L1_disable_xtalk_compensation(Dev);
Charles MacNeill 7:1add29d51e72 123
Charles MacNeill 7:1add29d51e72 124 if (Status != VL53L1_ERROR_NONE)
Charles MacNeill 7:1add29d51e72 125 goto ENDFUNC;
Charles MacNeill 7:1add29d51e72 126
Charles MacNeill 7:1add29d51e72 127 Status = VL53L1_StartMeasurement(Dev);
Charles MacNeill 7:1add29d51e72 128
Charles MacNeill 7:1add29d51e72 129 if (Status != VL53L1_ERROR_NONE)
Charles MacNeill 7:1add29d51e72 130 goto ENDFUNC;
Charles MacNeill 7:1add29d51e72 131
Charles MacNeill 7:1add29d51e72 132
Charles MacNeill 7:1add29d51e72 133 VL53L1_WaitMeasurementDataReady(Dev);
Charles MacNeill 7:1add29d51e72 134 VL53L1_GetRangingMeasurementData(Dev, &RMData);
Charles MacNeill 7:1add29d51e72 135 VL53L1_ClearInterruptAndStartMeasurement(Dev);
Charles MacNeill 7:1add29d51e72 136
Charles MacNeill 7:1add29d51e72 137 sum_ranging = 0;
Charles MacNeill 7:1add29d51e72 138 sum_spads = 0;
Charles MacNeill 7:1add29d51e72 139 sum_signalRate = 0;
Charles MacNeill 7:1add29d51e72 140 total_count = 0;
Charles MacNeill 7:1add29d51e72 141 for (xtalk_meas = 0; xtalk_meas < xtalk_measmax; xtalk_meas++) {
Charles MacNeill 7:1add29d51e72 142 VL53L1_WaitMeasurementDataReady(Dev);
Charles MacNeill 7:1add29d51e72 143 VL53L1_GetRangingMeasurementData(Dev, &RMData);
Charles MacNeill 7:1add29d51e72 144 VL53L1_ClearInterruptAndStartMeasurement(Dev);
Charles MacNeill 7:1add29d51e72 145 if (RMData.RangeStatus == VL53L1_RANGESTATUS_RANGE_VALID) {
Charles MacNeill 7:1add29d51e72 146 sum_ranging += RMData.RangeMilliMeter;
Charles MacNeill 7:1add29d51e72 147 sum_signalRate += RMData.SignalRateRtnMegaCps;
Charles MacNeill 7:1add29d51e72 148 sum_spads += RMData.EffectiveSpadRtnCount / 256;
Charles MacNeill 7:1add29d51e72 149 total_count++;
Charles MacNeill 7:1add29d51e72 150 }
Charles MacNeill 7:1add29d51e72 151 }
Charles MacNeill 7:1add29d51e72 152 Status = VL53L1_StopMeasurement(Dev);
Charles MacNeill 7:1add29d51e72 153
Charles MacNeill 7:1add29d51e72 154 if (total_count > 0) {
Charles MacNeill 7:1add29d51e72 155
Charles MacNeill 7:1add29d51e72 156 xTalkStoredMeanSignalRate = sum_signalRate / total_count;
Charles MacNeill 7:1add29d51e72 157 xTalkStoredMeanRange = (FixPoint1616_t)(sum_ranging << 16);
Charles MacNeill 7:1add29d51e72 158 xTalkStoredMeanRange /= total_count;
Charles MacNeill 7:1add29d51e72 159 xTalkStoredMeanRtnSpads = (FixPoint1616_t)(sum_spads << 16);
Charles MacNeill 7:1add29d51e72 160 xTalkStoredMeanRtnSpads /= total_count;
Charles MacNeill 7:1add29d51e72 161
Charles MacNeill 7:1add29d51e72 162
Charles MacNeill 7:1add29d51e72 163 xTalkStoredMeanRtnSpadsAsInt = (xTalkStoredMeanRtnSpads +
Charles MacNeill 7:1add29d51e72 164 0x8000) >> 16;
Charles MacNeill 7:1add29d51e72 165
Charles MacNeill 7:1add29d51e72 166
Charles MacNeill 7:1add29d51e72 167 xTalkCalDistanceAsInt = ((uint32_t)BDTable[
Charles MacNeill 7:1add29d51e72 168 VL53L1_TUNING_SINGLE_TARGET_XTALK_TARGET_DISTANCE_MM]);
Charles MacNeill 7:1add29d51e72 169 if (xTalkStoredMeanRtnSpadsAsInt == 0 ||
Charles MacNeill 7:1add29d51e72 170 xTalkCalDistanceAsInt == 0 ||
Charles MacNeill 7:1add29d51e72 171 xTalkStoredMeanRange >= (xTalkCalDistanceAsInt << 16)) {
Charles MacNeill 7:1add29d51e72 172 XTalkCompensationRateMegaCps = 0;
Charles MacNeill 7:1add29d51e72 173 } else {
Charles MacNeill 7:1add29d51e72 174
Charles MacNeill 7:1add29d51e72 175 signalXTalkTotalPerSpad = (xTalkStoredMeanSignalRate) /
Charles MacNeill 7:1add29d51e72 176 xTalkStoredMeanRtnSpadsAsInt;
Charles MacNeill 7:1add29d51e72 177
Charles MacNeill 7:1add29d51e72 178
Charles MacNeill 7:1add29d51e72 179 signalXTalkTotalPerSpad *= (((uint32_t)1 << 16) -
Charles MacNeill 7:1add29d51e72 180 (xTalkStoredMeanRange / xTalkCalDistanceAsInt));
Charles MacNeill 7:1add29d51e72 181
Charles MacNeill 7:1add29d51e72 182
Charles MacNeill 7:1add29d51e72 183 XTalkCompensationRateMegaCps = (signalXTalkTotalPerSpad
Charles MacNeill 7:1add29d51e72 184 + 0x8000) >> 16;
Charles MacNeill 7:1add29d51e72 185 }
Charles MacNeill 7:1add29d51e72 186
Charles MacNeill 7:1add29d51e72 187
Charles MacNeill 7:1add29d51e72 188 Status = VL53L1_GetCalibrationData(Dev, &CalibrationData);
Charles MacNeill 7:1add29d51e72 189
Charles MacNeill 7:1add29d51e72 190 if (Status != VL53L1_ERROR_NONE)
Charles MacNeill 7:1add29d51e72 191 goto ENDFUNC;
Charles MacNeill 7:1add29d51e72 192
Charles MacNeill 7:1add29d51e72 193 pC = &CalibrationData.customer;
Charles MacNeill 7:1add29d51e72 194
Charles MacNeill 7:1add29d51e72 195 pC->algo__crosstalk_compensation_plane_offset_kcps =
Charles MacNeill 7:1add29d51e72 196 (uint32_t)(1000 * ((XTalkCompensationRateMegaCps +
Charles MacNeill 7:1add29d51e72 197 ((uint32_t)1<<6)) >> (16-9)));
Charles MacNeill 7:1add29d51e72 198
Charles MacNeill 7:1add29d51e72 199 Status = VL53L1_SetCalibrationData(Dev, &CalibrationData);
Charles MacNeill 7:1add29d51e72 200
Charles MacNeill 7:1add29d51e72 201 if (Status != VL53L1_ERROR_NONE)
Charles MacNeill 7:1add29d51e72 202 goto ENDFUNC;
Charles MacNeill 7:1add29d51e72 203
Charles MacNeill 7:1add29d51e72 204 Status = VL53L1_enable_xtalk_compensation(Dev);
Charles MacNeill 7:1add29d51e72 205
Charles MacNeill 7:1add29d51e72 206 } else
Charles MacNeill 7:1add29d51e72 207
Charles MacNeill 7:1add29d51e72 208 Status = VL53L1_ERROR_XTALK_EXTRACTION_NO_SAMPLE_FAIL;
Charles MacNeill 7:1add29d51e72 209
Charles MacNeill 7:1add29d51e72 210 ENDFUNC:
Charles MacNeill 7:1add29d51e72 211 LOG_FUNCTION_END(Status);
Charles MacNeill 7:1add29d51e72 212 return Status;
Charles MacNeill 7:1add29d51e72 213
Charles MacNeill 7:1add29d51e72 214 }
Charles MacNeill 7:1add29d51e72 215
Charles MacNeill 7:1add29d51e72 216
Charles MacNeill 7:1add29d51e72 217 static VL53L1_Error CheckValidRectRoi(VL53L1_UserRoi_t ROI)
Charles MacNeill 7:1add29d51e72 218 {
Charles MacNeill 7:1add29d51e72 219 VL53L1_Error Status = VL53L1_ERROR_NONE;
Charles MacNeill 7:1add29d51e72 220
Charles MacNeill 7:1add29d51e72 221 LOG_FUNCTION_START("");
Charles MacNeill 7:1add29d51e72 222
Charles MacNeill 7:1add29d51e72 223
Charles MacNeill 7:1add29d51e72 224 if ((ROI.TopLeftX > 15) || (ROI.TopLeftY > 15) ||
Charles MacNeill 7:1add29d51e72 225 (ROI.BotRightX > 15) || (ROI.BotRightY > 15))
Charles MacNeill 7:1add29d51e72 226 Status = VL53L1_ERROR_INVALID_PARAMS;
Charles MacNeill 7:1add29d51e72 227
Charles MacNeill 7:1add29d51e72 228 if ((ROI.TopLeftX > ROI.BotRightX) || (ROI.TopLeftY < ROI.BotRightY))
Charles MacNeill 7:1add29d51e72 229 Status = VL53L1_ERROR_INVALID_PARAMS;
Charles MacNeill 7:1add29d51e72 230
Charles MacNeill 7:1add29d51e72 231 LOG_FUNCTION_END(Status);
Charles MacNeill 7:1add29d51e72 232 return Status;
Charles MacNeill 7:1add29d51e72 233 }
Charles MacNeill 7:1add29d51e72 234
Charles MacNeill 7:1add29d51e72 235 static VL53L1_GPIO_Interrupt_Mode ConvertModeToLLD(VL53L1_Error *pStatus,
Charles MacNeill 7:1add29d51e72 236 VL53L1_ThresholdMode CrossMode)
Charles MacNeill 7:1add29d51e72 237 {
Charles MacNeill 7:1add29d51e72 238 VL53L1_GPIO_Interrupt_Mode Mode;
Charles MacNeill 7:1add29d51e72 239
Charles MacNeill 7:1add29d51e72 240 switch (CrossMode) {
Charles MacNeill 7:1add29d51e72 241 case VL53L1_THRESHOLD_CROSSED_LOW:
Charles MacNeill 7:1add29d51e72 242 Mode = VL53L1_GPIOINTMODE_LEVEL_LOW;
Charles MacNeill 7:1add29d51e72 243 break;
Charles MacNeill 7:1add29d51e72 244 case VL53L1_THRESHOLD_CROSSED_HIGH:
Charles MacNeill 7:1add29d51e72 245 Mode = VL53L1_GPIOINTMODE_LEVEL_HIGH;
Charles MacNeill 7:1add29d51e72 246 break;
Charles MacNeill 7:1add29d51e72 247 case VL53L1_THRESHOLD_OUT_OF_WINDOW:
Charles MacNeill 7:1add29d51e72 248 Mode = VL53L1_GPIOINTMODE_OUT_OF_WINDOW;
Charles MacNeill 7:1add29d51e72 249 break;
Charles MacNeill 7:1add29d51e72 250 case VL53L1_THRESHOLD_IN_WINDOW:
Charles MacNeill 7:1add29d51e72 251 Mode = VL53L1_GPIOINTMODE_IN_WINDOW;
Charles MacNeill 7:1add29d51e72 252 break;
Charles MacNeill 7:1add29d51e72 253 default:
Charles MacNeill 7:1add29d51e72 254
Charles MacNeill 7:1add29d51e72 255 Mode = VL53L1_GPIOINTMODE_LEVEL_HIGH;
Charles MacNeill 7:1add29d51e72 256 *pStatus = VL53L1_ERROR_INVALID_PARAMS;
Charles MacNeill 7:1add29d51e72 257 }
Charles MacNeill 7:1add29d51e72 258 return Mode;
Charles MacNeill 7:1add29d51e72 259 }
Charles MacNeill 7:1add29d51e72 260
Charles MacNeill 7:1add29d51e72 261 static VL53L1_ThresholdMode ConvertModeFromLLD(VL53L1_Error *pStatus,
Charles MacNeill 7:1add29d51e72 262 VL53L1_GPIO_Interrupt_Mode CrossMode)
Charles MacNeill 7:1add29d51e72 263 {
Charles MacNeill 7:1add29d51e72 264 VL53L1_ThresholdMode Mode;
Charles MacNeill 7:1add29d51e72 265
Charles MacNeill 7:1add29d51e72 266 switch (CrossMode) {
Charles MacNeill 7:1add29d51e72 267 case VL53L1_GPIOINTMODE_LEVEL_LOW:
Charles MacNeill 7:1add29d51e72 268 Mode = VL53L1_THRESHOLD_CROSSED_LOW;
Charles MacNeill 7:1add29d51e72 269 break;
Charles MacNeill 7:1add29d51e72 270 case VL53L1_GPIOINTMODE_LEVEL_HIGH:
Charles MacNeill 7:1add29d51e72 271 Mode = VL53L1_THRESHOLD_CROSSED_HIGH;
Charles MacNeill 7:1add29d51e72 272 break;
Charles MacNeill 7:1add29d51e72 273 case VL53L1_GPIOINTMODE_OUT_OF_WINDOW:
Charles MacNeill 7:1add29d51e72 274 Mode = VL53L1_THRESHOLD_OUT_OF_WINDOW;
Charles MacNeill 7:1add29d51e72 275 break;
Charles MacNeill 7:1add29d51e72 276 case VL53L1_GPIOINTMODE_IN_WINDOW:
Charles MacNeill 7:1add29d51e72 277 Mode = VL53L1_THRESHOLD_IN_WINDOW;
Charles MacNeill 7:1add29d51e72 278 break;
Charles MacNeill 7:1add29d51e72 279 default:
Charles MacNeill 7:1add29d51e72 280
Charles MacNeill 7:1add29d51e72 281 Mode = VL53L1_THRESHOLD_CROSSED_HIGH;
Charles MacNeill 7:1add29d51e72 282 *pStatus = VL53L1_ERROR_UNDEFINED;
Charles MacNeill 7:1add29d51e72 283 }
Charles MacNeill 7:1add29d51e72 284 return Mode;
Charles MacNeill 7:1add29d51e72 285 }
Charles MacNeill 7:1add29d51e72 286
Charles MacNeill 7:1add29d51e72 287
Charles MacNeill 7:1add29d51e72 288
Charles MacNeill 7:1add29d51e72 289 VL53L1_Error VL53L1_GetVersion(VL53L1_Version_t *pVersion)
Charles MacNeill 7:1add29d51e72 290 {
Charles MacNeill 7:1add29d51e72 291 VL53L1_Error Status = VL53L1_ERROR_NONE;
Charles MacNeill 7:1add29d51e72 292
Charles MacNeill 7:1add29d51e72 293 LOG_FUNCTION_START("");
Charles MacNeill 7:1add29d51e72 294
Charles MacNeill 7:1add29d51e72 295 pVersion->major = VL53L1_IMPLEMENTATION_VER_MAJOR;
Charles MacNeill 7:1add29d51e72 296 pVersion->minor = VL53L1_IMPLEMENTATION_VER_MINOR;
Charles MacNeill 7:1add29d51e72 297 pVersion->build = VL53L1_IMPLEMENTATION_VER_SUB;
Charles MacNeill 7:1add29d51e72 298
Charles MacNeill 7:1add29d51e72 299 pVersion->revision = VL53L1_IMPLEMENTATION_VER_REVISION;
Charles MacNeill 7:1add29d51e72 300
Charles MacNeill 7:1add29d51e72 301 LOG_FUNCTION_END(Status);
Charles MacNeill 7:1add29d51e72 302 return Status;
Charles MacNeill 7:1add29d51e72 303 }
Charles MacNeill 7:1add29d51e72 304
Charles MacNeill 7:1add29d51e72 305 VL53L1_Error VL53L1_GetProductRevision(VL53L1_DEV Dev,
Charles MacNeill 7:1add29d51e72 306 uint8_t *pProductRevisionMajor, uint8_t *pProductRevisionMinor)
Charles MacNeill 7:1add29d51e72 307 {
Charles MacNeill 7:1add29d51e72 308 VL53L1_Error Status = VL53L1_ERROR_NONE;
Charles MacNeill 7:1add29d51e72 309 uint8_t revision_id;
Charles MacNeill 7:1add29d51e72 310 VL53L1_LLDriverData_t *pLLData;
Charles MacNeill 7:1add29d51e72 311
Charles MacNeill 7:1add29d51e72 312 LOG_FUNCTION_START("");
Charles MacNeill 7:1add29d51e72 313
Charles MacNeill 7:1add29d51e72 314 pLLData = VL53L1DevStructGetLLDriverHandle(Dev);
Charles MacNeill 7:1add29d51e72 315 revision_id = pLLData->nvm_copy_data.identification__revision_id;
Charles MacNeill 7:1add29d51e72 316 *pProductRevisionMajor = 1;
Charles MacNeill 7:1add29d51e72 317 *pProductRevisionMinor = (revision_id & 0xF0) >> 4;
Charles MacNeill 7:1add29d51e72 318
Charles MacNeill 7:1add29d51e72 319 LOG_FUNCTION_END(Status);
Charles MacNeill 7:1add29d51e72 320 return Status;
Charles MacNeill 7:1add29d51e72 321
Charles MacNeill 7:1add29d51e72 322 }
Charles MacNeill 7:1add29d51e72 323
Charles MacNeill 7:1add29d51e72 324 VL53L1_Error VL53L1_GetDeviceInfo(VL53L1_DEV Dev,
Charles MacNeill 7:1add29d51e72 325 VL53L1_DeviceInfo_t *pVL53L1_DeviceInfo)
Charles MacNeill 7:1add29d51e72 326 {
Charles MacNeill 7:1add29d51e72 327 VL53L1_Error Status = VL53L1_ERROR_NONE;
Charles MacNeill 7:1add29d51e72 328 uint8_t revision_id;
Charles MacNeill 7:1add29d51e72 329 VL53L1_LLDriverData_t *pLLData;
Charles MacNeill 7:1add29d51e72 330
Charles MacNeill 7:1add29d51e72 331 LOG_FUNCTION_START("");
Charles MacNeill 7:1add29d51e72 332
Charles MacNeill 7:1add29d51e72 333 pLLData = VL53L1DevStructGetLLDriverHandle(Dev);
Charles MacNeill 7:1add29d51e72 334
Charles MacNeill 7:1add29d51e72 335 strncpy(pVL53L1_DeviceInfo->ProductId, "",
Charles MacNeill 7:1add29d51e72 336 VL53L1_DEVINFO_STRLEN-1);
Charles MacNeill 7:1add29d51e72 337 pVL53L1_DeviceInfo->ProductType =
Charles MacNeill 7:1add29d51e72 338 pLLData->nvm_copy_data.identification__module_type;
Charles MacNeill 7:1add29d51e72 339
Charles MacNeill 7:1add29d51e72 340 revision_id = pLLData->nvm_copy_data.identification__revision_id;
Charles MacNeill 7:1add29d51e72 341 pVL53L1_DeviceInfo->ProductRevisionMajor = 1;
Charles MacNeill 7:1add29d51e72 342 pVL53L1_DeviceInfo->ProductRevisionMinor = (revision_id & 0xF0) >> 4;
Charles MacNeill 7:1add29d51e72 343
Charles MacNeill 7:1add29d51e72 344 #ifndef VL53L1_USE_EMPTY_STRING
Charles MacNeill 7:1add29d51e72 345 if (pVL53L1_DeviceInfo->ProductRevisionMinor == 0)
Charles MacNeill 7:1add29d51e72 346 strncpy(pVL53L1_DeviceInfo->Name,
Charles MacNeill 7:1add29d51e72 347 VL53L1_STRING_DEVICE_INFO_NAME0,
Charles MacNeill 7:1add29d51e72 348 VL53L1_DEVINFO_STRLEN-1);
Charles MacNeill 7:1add29d51e72 349 else
Charles MacNeill 7:1add29d51e72 350 strncpy(pVL53L1_DeviceInfo->Name,
Charles MacNeill 7:1add29d51e72 351 VL53L1_STRING_DEVICE_INFO_NAME1,
Charles MacNeill 7:1add29d51e72 352 VL53L1_DEVINFO_STRLEN-1);
Charles MacNeill 7:1add29d51e72 353 strncpy(pVL53L1_DeviceInfo->Type,
Charles MacNeill 7:1add29d51e72 354 VL53L1_STRING_DEVICE_INFO_TYPE,
Charles MacNeill 7:1add29d51e72 355 VL53L1_DEVINFO_STRLEN-1);
Charles MacNeill 7:1add29d51e72 356
Charles MacNeill 7:1add29d51e72 357 if (pVL53L1_DeviceInfo->ProductType == 0xAA) {
Charles MacNeill 7:1add29d51e72 358 pVL53L1_DeviceInfo->Name[5] = '3';
Charles MacNeill 7:1add29d51e72 359 pVL53L1_DeviceInfo->Type[5] = '3';
Charles MacNeill 7:1add29d51e72 360 }
Charles MacNeill 7:1add29d51e72 361 #else
Charles MacNeill 7:1add29d51e72 362 pVL53L1_DeviceInfo->Name[0] = 0;
Charles MacNeill 7:1add29d51e72 363 pVL53L1_DeviceInfo->Type[0] = 0;
Charles MacNeill 7:1add29d51e72 364 #endif
Charles MacNeill 7:1add29d51e72 365
Charles MacNeill 7:1add29d51e72 366 LOG_FUNCTION_END(Status);
Charles MacNeill 7:1add29d51e72 367 return Status;
Charles MacNeill 7:1add29d51e72 368 }
Charles MacNeill 7:1add29d51e72 369
Charles MacNeill 7:1add29d51e72 370 VL53L1_Error VL53L1_GetUID(VL53L1_DEV Dev, uint64_t *pUid)
Charles MacNeill 7:1add29d51e72 371 {
Charles MacNeill 7:1add29d51e72 372 VL53L1_Error Status = VL53L1_ERROR_NONE;
Charles MacNeill 7:1add29d51e72 373 uint8_t fmtdata[8];
Charles MacNeill 7:1add29d51e72 374
Charles MacNeill 7:1add29d51e72 375 LOG_FUNCTION_START("");
Charles MacNeill 7:1add29d51e72 376
Charles MacNeill 7:1add29d51e72 377 Status = VL53L1_read_nvm_raw_data(Dev,
Charles MacNeill 7:1add29d51e72 378 (uint8_t)(0x1F8 >> 2),
Charles MacNeill 7:1add29d51e72 379 (uint8_t)(8 >> 2),
Charles MacNeill 7:1add29d51e72 380 fmtdata);
Charles MacNeill 7:1add29d51e72 381 memcpy(pUid, fmtdata, sizeof(uint64_t));
Charles MacNeill 7:1add29d51e72 382
Charles MacNeill 7:1add29d51e72 383 LOG_FUNCTION_END(Status);
Charles MacNeill 7:1add29d51e72 384 return Status;
Charles MacNeill 7:1add29d51e72 385 }
Charles MacNeill 7:1add29d51e72 386
Charles MacNeill 7:1add29d51e72 387 VL53L1_Error VL53L1_GetRangeStatusString(uint8_t RangeStatus,
Charles MacNeill 7:1add29d51e72 388 char *pRangeStatusString)
Charles MacNeill 7:1add29d51e72 389 {
Charles MacNeill 7:1add29d51e72 390 VL53L1_Error Status = VL53L1_ERROR_NONE;
Charles MacNeill 7:1add29d51e72 391
Charles MacNeill 7:1add29d51e72 392 LOG_FUNCTION_START("");
Charles MacNeill 7:1add29d51e72 393
Charles MacNeill 7:1add29d51e72 394 Status = VL53L1_get_range_status_string(RangeStatus,
Charles MacNeill 7:1add29d51e72 395 pRangeStatusString);
Charles MacNeill 7:1add29d51e72 396
Charles MacNeill 7:1add29d51e72 397 LOG_FUNCTION_END(Status);
Charles MacNeill 7:1add29d51e72 398 return Status;
Charles MacNeill 7:1add29d51e72 399 }
Charles MacNeill 7:1add29d51e72 400
Charles MacNeill 7:1add29d51e72 401 VL53L1_Error VL53L1_GetPalErrorString(VL53L1_Error PalErrorCode,
Charles MacNeill 7:1add29d51e72 402 char *pPalErrorString)
Charles MacNeill 7:1add29d51e72 403 {
Charles MacNeill 7:1add29d51e72 404 VL53L1_Error Status = VL53L1_ERROR_NONE;
Charles MacNeill 7:1add29d51e72 405
Charles MacNeill 7:1add29d51e72 406 LOG_FUNCTION_START("");
Charles MacNeill 7:1add29d51e72 407
Charles MacNeill 7:1add29d51e72 408 Status = VL53L1_get_pal_error_string(PalErrorCode, pPalErrorString);
Charles MacNeill 7:1add29d51e72 409
Charles MacNeill 7:1add29d51e72 410 LOG_FUNCTION_END(Status);
Charles MacNeill 7:1add29d51e72 411 return Status;
Charles MacNeill 7:1add29d51e72 412 }
Charles MacNeill 7:1add29d51e72 413
Charles MacNeill 7:1add29d51e72 414 VL53L1_Error VL53L1_GetPalStateString(VL53L1_State PalStateCode,
Charles MacNeill 7:1add29d51e72 415 char *pPalStateString)
Charles MacNeill 7:1add29d51e72 416 {
Charles MacNeill 7:1add29d51e72 417 VL53L1_Error Status = VL53L1_ERROR_NONE;
Charles MacNeill 7:1add29d51e72 418
Charles MacNeill 7:1add29d51e72 419 LOG_FUNCTION_START("");
Charles MacNeill 7:1add29d51e72 420
Charles MacNeill 7:1add29d51e72 421 Status = VL53L1_get_pal_state_string(PalStateCode, pPalStateString);
Charles MacNeill 7:1add29d51e72 422
Charles MacNeill 7:1add29d51e72 423 LOG_FUNCTION_END(Status);
Charles MacNeill 7:1add29d51e72 424 return Status;
Charles MacNeill 7:1add29d51e72 425 }
Charles MacNeill 7:1add29d51e72 426
Charles MacNeill 7:1add29d51e72 427 VL53L1_Error VL53L1_GetPalState(VL53L1_DEV Dev, VL53L1_State *pPalState)
Charles MacNeill 7:1add29d51e72 428 {
Charles MacNeill 7:1add29d51e72 429 VL53L1_Error Status = VL53L1_ERROR_NONE;
Charles MacNeill 7:1add29d51e72 430
Charles MacNeill 7:1add29d51e72 431 LOG_FUNCTION_START("");
Charles MacNeill 7:1add29d51e72 432
Charles MacNeill 7:1add29d51e72 433 *pPalState = VL53L1DevDataGet(Dev, PalState);
Charles MacNeill 7:1add29d51e72 434
Charles MacNeill 7:1add29d51e72 435 LOG_FUNCTION_END(Status);
Charles MacNeill 7:1add29d51e72 436 return Status;
Charles MacNeill 7:1add29d51e72 437 }
Charles MacNeill 7:1add29d51e72 438
Charles MacNeill 7:1add29d51e72 439
Charles MacNeill 7:1add29d51e72 440
Charles MacNeill 7:1add29d51e72 441
Charles MacNeill 7:1add29d51e72 442 VL53L1_Error VL53L1_SetDeviceAddress(VL53L1_DEV Dev, uint8_t DeviceAddress)
Charles MacNeill 7:1add29d51e72 443 {
Charles MacNeill 7:1add29d51e72 444 VL53L1_Error Status = VL53L1_ERROR_NONE;
Charles MacNeill 7:1add29d51e72 445 VL53L1_LLDriverData_t *pdev = VL53L1DevStructGetLLDriverHandle(Dev);
Charles MacNeill 7:1add29d51e72 446 VL53L1_static_nvm_managed_t *pdata = &(pdev->stat_nvm);
Charles MacNeill 7:1add29d51e72 447
Charles MacNeill 7:1add29d51e72 448 LOG_FUNCTION_START("");
Charles MacNeill 7:1add29d51e72 449
Charles MacNeill 7:1add29d51e72 450 Status = VL53L1_WrByte(Dev, VL53L1_I2C_SLAVE__DEVICE_ADDRESS,
Charles MacNeill 7:1add29d51e72 451 DeviceAddress / 2);
Charles MacNeill 7:1add29d51e72 452
Charles MacNeill 7:1add29d51e72 453 pdata->i2c_slave__device_address = (DeviceAddress / 2) & 0x7F;
Charles MacNeill 7:1add29d51e72 454
Charles MacNeill 7:1add29d51e72 455 LOG_FUNCTION_END(Status);
Charles MacNeill 7:1add29d51e72 456 return Status;
Charles MacNeill 7:1add29d51e72 457 }
Charles MacNeill 7:1add29d51e72 458
Charles MacNeill 7:1add29d51e72 459 VL53L1_Error VL53L1_DataInit(VL53L1_DEV Dev)
Charles MacNeill 7:1add29d51e72 460 {
Charles MacNeill 7:1add29d51e72 461 VL53L1_Error Status = VL53L1_ERROR_NONE;
Charles MacNeill 7:1add29d51e72 462 uint8_t i;
Charles MacNeill 7:1add29d51e72 463 VL53L1_LLDriverData_t *pdev;
Charles MacNeill 7:1add29d51e72 464
Charles MacNeill 7:1add29d51e72 465 LOG_FUNCTION_START("");
Charles MacNeill 7:1add29d51e72 466
Charles MacNeill 7:1add29d51e72 467
Charles MacNeill 7:1add29d51e72 468 #ifdef USE_I2C_2V8
Charles MacNeill 7:1add29d51e72 469 Status = VL53L1_RdByte(Dev, VL53L1_PAD_I2C_HV__EXTSUP_CONFIG, &i);
Charles MacNeill 7:1add29d51e72 470 if (Status == VL53L1_ERROR_NONE) {
Charles MacNeill 7:1add29d51e72 471 i = (i & 0xfe) | 0x01;
Charles MacNeill 7:1add29d51e72 472 Status = VL53L1_WrByte(Dev, VL53L1_PAD_I2C_HV__EXTSUP_CONFIG,
Charles MacNeill 7:1add29d51e72 473 i);
Charles MacNeill 7:1add29d51e72 474 }
Charles MacNeill 7:1add29d51e72 475 #endif
Charles MacNeill 7:1add29d51e72 476
Charles MacNeill 7:1add29d51e72 477 if (Status == VL53L1_ERROR_NONE)
Charles MacNeill 7:1add29d51e72 478 Status = VL53L1_data_init(Dev, 1);
Charles MacNeill 7:1add29d51e72 479
Charles MacNeill 7:1add29d51e72 480 if (Status == VL53L1_ERROR_NONE) {
Charles MacNeill 7:1add29d51e72 481 pdev = VL53L1DevStructGetLLDriverHandle(Dev);
Charles MacNeill 7:1add29d51e72 482 memset(&pdev->per_vcsel_cal_data, 0,
Charles MacNeill 7:1add29d51e72 483 sizeof(pdev->per_vcsel_cal_data));
Charles MacNeill 7:1add29d51e72 484 }
Charles MacNeill 7:1add29d51e72 485
Charles MacNeill 7:1add29d51e72 486 if (Status == VL53L1_ERROR_NONE)
Charles MacNeill 7:1add29d51e72 487 VL53L1DevDataSet(Dev, PalState, VL53L1_STATE_WAIT_STATICINIT);
Charles MacNeill 7:1add29d51e72 488
Charles MacNeill 7:1add29d51e72 489
Charles MacNeill 7:1add29d51e72 490 for (i = 0; i < VL53L1_CHECKENABLE_NUMBER_OF_CHECKS; i++) {
Charles MacNeill 7:1add29d51e72 491 if (Status == VL53L1_ERROR_NONE)
Charles MacNeill 7:1add29d51e72 492 Status |= VL53L1_SetLimitCheckEnable(Dev, i, 1);
Charles MacNeill 7:1add29d51e72 493 else
Charles MacNeill 7:1add29d51e72 494 break;
Charles MacNeill 7:1add29d51e72 495
Charles MacNeill 7:1add29d51e72 496 }
Charles MacNeill 7:1add29d51e72 497
Charles MacNeill 7:1add29d51e72 498
Charles MacNeill 7:1add29d51e72 499 if (Status == VL53L1_ERROR_NONE) {
Charles MacNeill 7:1add29d51e72 500 Status = VL53L1_set_dmax_mode(Dev,
Charles MacNeill 7:1add29d51e72 501 VL53L1_DEVICEDMAXMODE__CUST_CAL_DATA);
Charles MacNeill 7:1add29d51e72 502 }
Charles MacNeill 7:1add29d51e72 503
Charles MacNeill 7:1add29d51e72 504
Charles MacNeill 7:1add29d51e72 505 LOG_FUNCTION_END(Status);
Charles MacNeill 7:1add29d51e72 506 return Status;
Charles MacNeill 7:1add29d51e72 507 }
Charles MacNeill 7:1add29d51e72 508
Charles MacNeill 7:1add29d51e72 509
Charles MacNeill 7:1add29d51e72 510 VL53L1_Error VL53L1_StaticInit(VL53L1_DEV Dev)
Charles MacNeill 7:1add29d51e72 511 {
Charles MacNeill 7:1add29d51e72 512 VL53L1_Error Status = VL53L1_ERROR_NONE;
Charles MacNeill 7:1add29d51e72 513 uint8_t measurement_mode;
Charles MacNeill 7:1add29d51e72 514
Charles MacNeill 7:1add29d51e72 515 LOG_FUNCTION_START("");
Charles MacNeill 7:1add29d51e72 516
Charles MacNeill 7:1add29d51e72 517 VL53L1DevDataSet(Dev, PalState, VL53L1_STATE_IDLE);
Charles MacNeill 7:1add29d51e72 518
Charles MacNeill 7:1add29d51e72 519 measurement_mode = VL53L1_DEVICEMEASUREMENTMODE_BACKTOBACK;
Charles MacNeill 7:1add29d51e72 520 VL53L1DevDataSet(Dev, LLData.measurement_mode, measurement_mode);
Charles MacNeill 7:1add29d51e72 521
Charles MacNeill 7:1add29d51e72 522 Status = VL53L1_SetPresetMode(Dev,
Charles MacNeill 7:1add29d51e72 523 VL53L1_PRESETMODE_RANGING);
Charles MacNeill 7:1add29d51e72 524 VL53L1_SetDistanceMode(Dev,
Charles MacNeill 7:1add29d51e72 525 VL53L1_DISTANCEMODE_MEDIUM);
Charles MacNeill 7:1add29d51e72 526 VL53L1DevDataSet(Dev, CurrentParameters.OutputMode,
Charles MacNeill 7:1add29d51e72 527 VL53L1_OUTPUTMODE_NEAREST);
Charles MacNeill 7:1add29d51e72 528 VL53L1_SmudgeCorrectionEnable(Dev,
Charles MacNeill 7:1add29d51e72 529 VL53L1_SMUDGE_CORRECTION_NONE);
Charles MacNeill 7:1add29d51e72 530
Charles MacNeill 7:1add29d51e72 531 LOG_FUNCTION_END(Status);
Charles MacNeill 7:1add29d51e72 532 return Status;
Charles MacNeill 7:1add29d51e72 533 }
Charles MacNeill 7:1add29d51e72 534
Charles MacNeill 7:1add29d51e72 535 VL53L1_Error VL53L1_WaitDeviceBooted(VL53L1_DEV Dev)
Charles MacNeill 7:1add29d51e72 536 {
Charles MacNeill 7:1add29d51e72 537 VL53L1_Error Status = VL53L1_ERROR_NONE;
Charles MacNeill 7:1add29d51e72 538
Charles MacNeill 7:1add29d51e72 539 LOG_FUNCTION_START("");
Charles MacNeill 7:1add29d51e72 540
Charles MacNeill 7:1add29d51e72 541 Status = VL53L1_poll_for_boot_completion(Dev,
Charles MacNeill 7:1add29d51e72 542 VL53L1_BOOT_COMPLETION_POLLING_TIMEOUT_MS);
Charles MacNeill 7:1add29d51e72 543
Charles MacNeill 7:1add29d51e72 544 LOG_FUNCTION_END(Status);
Charles MacNeill 7:1add29d51e72 545 return Status;
Charles MacNeill 7:1add29d51e72 546 }
Charles MacNeill 7:1add29d51e72 547
Charles MacNeill 7:1add29d51e72 548
Charles MacNeill 7:1add29d51e72 549
Charles MacNeill 7:1add29d51e72 550
Charles MacNeill 7:1add29d51e72 551 static VL53L1_Error ComputeDevicePresetMode(
Charles MacNeill 7:1add29d51e72 552 VL53L1_PresetModes PresetMode,
Charles MacNeill 7:1add29d51e72 553 VL53L1_DistanceModes DistanceMode,
Charles MacNeill 7:1add29d51e72 554 VL53L1_DevicePresetModes *pDevicePresetMode)
Charles MacNeill 7:1add29d51e72 555 {
Charles MacNeill 7:1add29d51e72 556 VL53L1_Error Status = VL53L1_ERROR_NONE;
Charles MacNeill 7:1add29d51e72 557
Charles MacNeill 7:1add29d51e72 558 uint8_t DistIdx;
Charles MacNeill 7:1add29d51e72 559 VL53L1_DevicePresetModes LightModes[3] = {
Charles MacNeill 7:1add29d51e72 560 VL53L1_DEVICEPRESETMODE_STANDARD_RANGING_SHORT_RANGE,
Charles MacNeill 7:1add29d51e72 561 VL53L1_DEVICEPRESETMODE_STANDARD_RANGING,
Charles MacNeill 7:1add29d51e72 562 VL53L1_DEVICEPRESETMODE_STANDARD_RANGING_LONG_RANGE};
Charles MacNeill 7:1add29d51e72 563
Charles MacNeill 7:1add29d51e72 564 VL53L1_DevicePresetModes RangingModes[3] = {
Charles MacNeill 7:1add29d51e72 565 VL53L1_DEVICEPRESETMODE_HISTOGRAM_SHORT_RANGE,
Charles MacNeill 7:1add29d51e72 566 VL53L1_DEVICEPRESETMODE_HISTOGRAM_MEDIUM_RANGE,
Charles MacNeill 7:1add29d51e72 567 VL53L1_DEVICEPRESETMODE_HISTOGRAM_LONG_RANGE};
Charles MacNeill 7:1add29d51e72 568
Charles MacNeill 7:1add29d51e72 569 VL53L1_DevicePresetModes ScanningModes[3] = {
Charles MacNeill 7:1add29d51e72 570 VL53L1_DEVICEPRESETMODE_HISTOGRAM_MULTIZONE_SHORT_RANGE,
Charles MacNeill 7:1add29d51e72 571 VL53L1_DEVICEPRESETMODE_HISTOGRAM_MULTIZONE,
Charles MacNeill 7:1add29d51e72 572 VL53L1_DEVICEPRESETMODE_HISTOGRAM_MULTIZONE_LONG_RANGE};
Charles MacNeill 7:1add29d51e72 573
Charles MacNeill 7:1add29d51e72 574 VL53L1_DevicePresetModes TimedModes[3] = {
Charles MacNeill 7:1add29d51e72 575 VL53L1_DEVICEPRESETMODE_TIMED_RANGING_SHORT_RANGE,
Charles MacNeill 7:1add29d51e72 576 VL53L1_DEVICEPRESETMODE_TIMED_RANGING,
Charles MacNeill 7:1add29d51e72 577 VL53L1_DEVICEPRESETMODE_TIMED_RANGING_LONG_RANGE};
Charles MacNeill 7:1add29d51e72 578
Charles MacNeill 7:1add29d51e72 579 VL53L1_DevicePresetModes LowPowerTimedModes[3] = {
Charles MacNeill 7:1add29d51e72 580 VL53L1_DEVICEPRESETMODE_LOWPOWERAUTO_SHORT_RANGE,
Charles MacNeill 7:1add29d51e72 581 VL53L1_DEVICEPRESETMODE_LOWPOWERAUTO_MEDIUM_RANGE,
Charles MacNeill 7:1add29d51e72 582 VL53L1_DEVICEPRESETMODE_LOWPOWERAUTO_LONG_RANGE};
Charles MacNeill 7:1add29d51e72 583
Charles MacNeill 7:1add29d51e72 584 *pDevicePresetMode = VL53L1_DEVICEPRESETMODE_STANDARD_RANGING;
Charles MacNeill 7:1add29d51e72 585
Charles MacNeill 7:1add29d51e72 586 switch (DistanceMode) {
Charles MacNeill 7:1add29d51e72 587 case VL53L1_DISTANCEMODE_SHORT:
Charles MacNeill 7:1add29d51e72 588 DistIdx = 0;
Charles MacNeill 7:1add29d51e72 589 break;
Charles MacNeill 7:1add29d51e72 590 case VL53L1_DISTANCEMODE_MEDIUM:
Charles MacNeill 7:1add29d51e72 591 DistIdx = 1;
Charles MacNeill 7:1add29d51e72 592 break;
Charles MacNeill 7:1add29d51e72 593 default:
Charles MacNeill 7:1add29d51e72 594 DistIdx = 2;
Charles MacNeill 7:1add29d51e72 595 }
Charles MacNeill 7:1add29d51e72 596
Charles MacNeill 7:1add29d51e72 597 switch (PresetMode) {
Charles MacNeill 7:1add29d51e72 598 case VL53L1_PRESETMODE_LITE_RANGING:
Charles MacNeill 7:1add29d51e72 599 *pDevicePresetMode = LightModes[DistIdx];
Charles MacNeill 7:1add29d51e72 600 break;
Charles MacNeill 7:1add29d51e72 601
Charles MacNeill 7:1add29d51e72 602 case VL53L1_PRESETMODE_RANGING:
Charles MacNeill 7:1add29d51e72 603 *pDevicePresetMode = RangingModes[DistIdx];
Charles MacNeill 7:1add29d51e72 604 break;
Charles MacNeill 7:1add29d51e72 605
Charles MacNeill 7:1add29d51e72 606 case VL53L1_PRESETMODE_MULTIZONES_SCANNING:
Charles MacNeill 7:1add29d51e72 607 *pDevicePresetMode = ScanningModes[DistIdx];
Charles MacNeill 7:1add29d51e72 608 break;
Charles MacNeill 7:1add29d51e72 609
Charles MacNeill 7:1add29d51e72 610 case VL53L1_PRESETMODE_AUTONOMOUS:
Charles MacNeill 7:1add29d51e72 611 *pDevicePresetMode = TimedModes[DistIdx];
Charles MacNeill 7:1add29d51e72 612 break;
Charles MacNeill 7:1add29d51e72 613
Charles MacNeill 7:1add29d51e72 614 case VL53L1_PRESETMODE_LOWPOWER_AUTONOMOUS:
Charles MacNeill 7:1add29d51e72 615 *pDevicePresetMode = LowPowerTimedModes[DistIdx];
Charles MacNeill 7:1add29d51e72 616 break;
Charles MacNeill 7:1add29d51e72 617 case VL53L1_PRESETMODE_OLT:
Charles MacNeill 7:1add29d51e72 618 *pDevicePresetMode = VL53L1_DEVICEPRESETMODE_OLT;
Charles MacNeill 7:1add29d51e72 619 break;
Charles MacNeill 7:1add29d51e72 620 case VL53L1_PRESETMODE_PROXY_RANGING_MODE:
Charles MacNeill 7:1add29d51e72 621 *pDevicePresetMode =
Charles MacNeill 7:1add29d51e72 622 VL53L1_DEVICEPRESETMODE_SPECIAL_HISTOGRAM_SHORT_RANGE;
Charles MacNeill 7:1add29d51e72 623 break;
Charles MacNeill 7:1add29d51e72 624
Charles MacNeill 7:1add29d51e72 625 default:
Charles MacNeill 7:1add29d51e72 626
Charles MacNeill 7:1add29d51e72 627 Status = VL53L1_ERROR_MODE_NOT_SUPPORTED;
Charles MacNeill 7:1add29d51e72 628 }
Charles MacNeill 7:1add29d51e72 629
Charles MacNeill 7:1add29d51e72 630 return Status;
Charles MacNeill 7:1add29d51e72 631 }
Charles MacNeill 7:1add29d51e72 632
Charles MacNeill 7:1add29d51e72 633 static VL53L1_Error SetPresetMode(VL53L1_DEV Dev,
Charles MacNeill 7:1add29d51e72 634 VL53L1_PresetModes PresetMode,
Charles MacNeill 7:1add29d51e72 635 VL53L1_DistanceModes DistanceMode,
Charles MacNeill 7:1add29d51e72 636 uint32_t inter_measurement_period_ms)
Charles MacNeill 7:1add29d51e72 637 {
Charles MacNeill 7:1add29d51e72 638 VL53L1_Error Status = VL53L1_ERROR_NONE;
Charles MacNeill 7:1add29d51e72 639 VL53L1_DevicePresetModes device_preset_mode;
Charles MacNeill 7:1add29d51e72 640 uint8_t measurement_mode;
Charles MacNeill 7:1add29d51e72 641 uint16_t dss_config__target_total_rate_mcps;
Charles MacNeill 7:1add29d51e72 642 uint32_t phasecal_config_timeout_us;
Charles MacNeill 7:1add29d51e72 643 uint32_t mm_config_timeout_us;
Charles MacNeill 7:1add29d51e72 644 uint32_t lld_range_config_timeout_us;
Charles MacNeill 7:1add29d51e72 645
Charles MacNeill 7:1add29d51e72 646 LOG_FUNCTION_START("%d", (int)PresetMode);
Charles MacNeill 7:1add29d51e72 647
Charles MacNeill 7:1add29d51e72 648 if ((PresetMode == VL53L1_PRESETMODE_AUTONOMOUS) ||
Charles MacNeill 7:1add29d51e72 649 (PresetMode == VL53L1_PRESETMODE_LOWPOWER_AUTONOMOUS))
Charles MacNeill 7:1add29d51e72 650 measurement_mode = VL53L1_DEVICEMEASUREMENTMODE_TIMED;
Charles MacNeill 7:1add29d51e72 651 else
Charles MacNeill 7:1add29d51e72 652 measurement_mode = VL53L1_DEVICEMEASUREMENTMODE_BACKTOBACK;
Charles MacNeill 7:1add29d51e72 653
Charles MacNeill 7:1add29d51e72 654
Charles MacNeill 7:1add29d51e72 655 Status = ComputeDevicePresetMode(PresetMode, DistanceMode,
Charles MacNeill 7:1add29d51e72 656 &device_preset_mode);
Charles MacNeill 7:1add29d51e72 657
Charles MacNeill 7:1add29d51e72 658 if (Status == VL53L1_ERROR_NONE)
Charles MacNeill 7:1add29d51e72 659 Status = VL53L1_get_preset_mode_timing_cfg(Dev,
Charles MacNeill 7:1add29d51e72 660 device_preset_mode,
Charles MacNeill 7:1add29d51e72 661 &dss_config__target_total_rate_mcps,
Charles MacNeill 7:1add29d51e72 662 &phasecal_config_timeout_us,
Charles MacNeill 7:1add29d51e72 663 &mm_config_timeout_us,
Charles MacNeill 7:1add29d51e72 664 &lld_range_config_timeout_us);
Charles MacNeill 7:1add29d51e72 665
Charles MacNeill 7:1add29d51e72 666 if (Status == VL53L1_ERROR_NONE)
Charles MacNeill 7:1add29d51e72 667 Status = VL53L1_set_preset_mode(
Charles MacNeill 7:1add29d51e72 668 Dev,
Charles MacNeill 7:1add29d51e72 669 device_preset_mode,
Charles MacNeill 7:1add29d51e72 670 dss_config__target_total_rate_mcps,
Charles MacNeill 7:1add29d51e72 671 phasecal_config_timeout_us,
Charles MacNeill 7:1add29d51e72 672 mm_config_timeout_us,
Charles MacNeill 7:1add29d51e72 673 lld_range_config_timeout_us,
Charles MacNeill 7:1add29d51e72 674 inter_measurement_period_ms);
Charles MacNeill 7:1add29d51e72 675
Charles MacNeill 7:1add29d51e72 676 if (Status == VL53L1_ERROR_NONE)
Charles MacNeill 7:1add29d51e72 677 VL53L1DevDataSet(Dev, LLData.measurement_mode,
Charles MacNeill 7:1add29d51e72 678 measurement_mode);
Charles MacNeill 7:1add29d51e72 679
Charles MacNeill 7:1add29d51e72 680 if (Status == VL53L1_ERROR_NONE)
Charles MacNeill 7:1add29d51e72 681 VL53L1DevDataSet(Dev, CurrentParameters.PresetMode, PresetMode);
Charles MacNeill 7:1add29d51e72 682
Charles MacNeill 7:1add29d51e72 683 VL53L1DevDataSet(Dev, CurrentParameters.OutputMode,
Charles MacNeill 7:1add29d51e72 684 VL53L1_OUTPUTMODE_NEAREST);
Charles MacNeill 7:1add29d51e72 685 LOG_FUNCTION_END(Status);
Charles MacNeill 7:1add29d51e72 686 return Status;
Charles MacNeill 7:1add29d51e72 687 }
Charles MacNeill 7:1add29d51e72 688
Charles MacNeill 7:1add29d51e72 689
Charles MacNeill 7:1add29d51e72 690 VL53L1_Error VL53L1_SetPresetMode(VL53L1_DEV Dev, VL53L1_PresetModes PresetMode)
Charles MacNeill 7:1add29d51e72 691 {
Charles MacNeill 7:1add29d51e72 692 VL53L1_Error Status = VL53L1_ERROR_NONE;
Charles MacNeill 7:1add29d51e72 693 VL53L1_DistanceModes DistanceMode = VL53L1_DISTANCEMODE_LONG;
Charles MacNeill 7:1add29d51e72 694
Charles MacNeill 7:1add29d51e72 695 LOG_FUNCTION_START("%d", (int)PresetMode);
Charles MacNeill 7:1add29d51e72 696
Charles MacNeill 7:1add29d51e72 697
Charles MacNeill 7:1add29d51e72 698 Status = VL53L1_low_power_auto_data_init(Dev);
Charles MacNeill 7:1add29d51e72 699
Charles MacNeill 7:1add29d51e72 700 if (PresetMode == VL53L1_PRESETMODE_PROXY_RANGING_MODE)
Charles MacNeill 7:1add29d51e72 701 DistanceMode = VL53L1_DISTANCEMODE_SHORT;
Charles MacNeill 7:1add29d51e72 702 Status = SetPresetMode(Dev,
Charles MacNeill 7:1add29d51e72 703 PresetMode,
Charles MacNeill 7:1add29d51e72 704 DistanceMode,
Charles MacNeill 7:1add29d51e72 705 1000);
Charles MacNeill 7:1add29d51e72 706
Charles MacNeill 7:1add29d51e72 707 if (Status == VL53L1_ERROR_NONE) {
Charles MacNeill 7:1add29d51e72 708 if ((PresetMode == VL53L1_PRESETMODE_LITE_RANGING) ||
Charles MacNeill 7:1add29d51e72 709 (PresetMode == VL53L1_PRESETMODE_AUTONOMOUS) ||
Charles MacNeill 7:1add29d51e72 710 (PresetMode == VL53L1_PRESETMODE_LOWPOWER_AUTONOMOUS))
Charles MacNeill 7:1add29d51e72 711 Status = VL53L1_SetMeasurementTimingBudgetMicroSeconds(
Charles MacNeill 7:1add29d51e72 712 Dev, 41000);
Charles MacNeill 7:1add29d51e72 713 else
Charles MacNeill 7:1add29d51e72 714
Charles MacNeill 7:1add29d51e72 715 Status = VL53L1_SetMeasurementTimingBudgetMicroSeconds(
Charles MacNeill 7:1add29d51e72 716 Dev, 33333);
Charles MacNeill 7:1add29d51e72 717 }
Charles MacNeill 7:1add29d51e72 718
Charles MacNeill 7:1add29d51e72 719 if (Status == VL53L1_ERROR_NONE) {
Charles MacNeill 7:1add29d51e72 720
Charles MacNeill 7:1add29d51e72 721 Status = VL53L1_SetInterMeasurementPeriodMilliSeconds(Dev,
Charles MacNeill 7:1add29d51e72 722 1000);
Charles MacNeill 7:1add29d51e72 723 }
Charles MacNeill 7:1add29d51e72 724
Charles MacNeill 7:1add29d51e72 725 LOG_FUNCTION_END(Status);
Charles MacNeill 7:1add29d51e72 726 return Status;
Charles MacNeill 7:1add29d51e72 727 }
Charles MacNeill 7:1add29d51e72 728
Charles MacNeill 7:1add29d51e72 729
Charles MacNeill 7:1add29d51e72 730 VL53L1_Error VL53L1_GetPresetMode(VL53L1_DEV Dev,
Charles MacNeill 7:1add29d51e72 731 VL53L1_PresetModes *pPresetMode)
Charles MacNeill 7:1add29d51e72 732 {
Charles MacNeill 7:1add29d51e72 733 VL53L1_Error Status = VL53L1_ERROR_NONE;
Charles MacNeill 7:1add29d51e72 734
Charles MacNeill 7:1add29d51e72 735 LOG_FUNCTION_START("");
Charles MacNeill 7:1add29d51e72 736
Charles MacNeill 7:1add29d51e72 737 *pPresetMode = VL53L1DevDataGet(Dev, CurrentParameters.PresetMode);
Charles MacNeill 7:1add29d51e72 738
Charles MacNeill 7:1add29d51e72 739 LOG_FUNCTION_END(Status);
Charles MacNeill 7:1add29d51e72 740 return Status;
Charles MacNeill 7:1add29d51e72 741 }
Charles MacNeill 7:1add29d51e72 742
Charles MacNeill 7:1add29d51e72 743 VL53L1_Error VL53L1_SetDistanceMode(VL53L1_DEV Dev,
Charles MacNeill 7:1add29d51e72 744 VL53L1_DistanceModes DistanceMode)
Charles MacNeill 7:1add29d51e72 745 {
Charles MacNeill 7:1add29d51e72 746 VL53L1_Error Status = VL53L1_ERROR_NONE;
Charles MacNeill 7:1add29d51e72 747 VL53L1_PresetModes PresetMode;
Charles MacNeill 7:1add29d51e72 748 uint32_t inter_measurement_period_ms;
Charles MacNeill 7:1add29d51e72 749 uint32_t TimingBudget;
Charles MacNeill 7:1add29d51e72 750 uint32_t MmTimeoutUs;
Charles MacNeill 7:1add29d51e72 751 uint32_t PhaseCalTimeoutUs;
Charles MacNeill 7:1add29d51e72 752 VL53L1_zone_config_t zone_config;
Charles MacNeill 7:1add29d51e72 753
Charles MacNeill 7:1add29d51e72 754 LOG_FUNCTION_START("%d", (int)DistanceMode);
Charles MacNeill 7:1add29d51e72 755
Charles MacNeill 7:1add29d51e72 756 PresetMode = VL53L1DevDataGet(Dev, CurrentParameters.PresetMode);
Charles MacNeill 7:1add29d51e72 757
Charles MacNeill 7:1add29d51e72 758
Charles MacNeill 7:1add29d51e72 759
Charles MacNeill 7:1add29d51e72 760 if ((PresetMode == VL53L1_PRESETMODE_PROXY_RANGING_MODE) &&
Charles MacNeill 7:1add29d51e72 761 (DistanceMode != VL53L1_DISTANCEMODE_SHORT))
Charles MacNeill 7:1add29d51e72 762 return VL53L1_ERROR_INVALID_PARAMS;
Charles MacNeill 7:1add29d51e72 763 if ((DistanceMode != VL53L1_DISTANCEMODE_SHORT) &&
Charles MacNeill 7:1add29d51e72 764 (DistanceMode != VL53L1_DISTANCEMODE_MEDIUM) &&
Charles MacNeill 7:1add29d51e72 765 (DistanceMode != VL53L1_DISTANCEMODE_LONG))
Charles MacNeill 7:1add29d51e72 766 return VL53L1_ERROR_INVALID_PARAMS;
Charles MacNeill 7:1add29d51e72 767
Charles MacNeill 7:1add29d51e72 768 if (Status == VL53L1_ERROR_NONE)
Charles MacNeill 7:1add29d51e72 769 Status = VL53L1_get_zone_config(Dev, &zone_config);
Charles MacNeill 7:1add29d51e72 770
Charles MacNeill 7:1add29d51e72 771 inter_measurement_period_ms = VL53L1DevDataGet(Dev,
Charles MacNeill 7:1add29d51e72 772 LLData.inter_measurement_period_ms);
Charles MacNeill 7:1add29d51e72 773
Charles MacNeill 7:1add29d51e72 774 if (Status == VL53L1_ERROR_NONE)
Charles MacNeill 7:1add29d51e72 775 Status = VL53L1_get_timeouts_us(Dev, &PhaseCalTimeoutUs,
Charles MacNeill 7:1add29d51e72 776 &MmTimeoutUs, &TimingBudget);
Charles MacNeill 7:1add29d51e72 777
Charles MacNeill 7:1add29d51e72 778 if (Status == VL53L1_ERROR_NONE)
Charles MacNeill 7:1add29d51e72 779 Status = SetPresetMode(Dev,
Charles MacNeill 7:1add29d51e72 780 PresetMode,
Charles MacNeill 7:1add29d51e72 781 DistanceMode,
Charles MacNeill 7:1add29d51e72 782 inter_measurement_period_ms);
Charles MacNeill 7:1add29d51e72 783
Charles MacNeill 7:1add29d51e72 784 if (Status == VL53L1_ERROR_NONE) {
Charles MacNeill 7:1add29d51e72 785 VL53L1DevDataSet(Dev, CurrentParameters.DistanceMode,
Charles MacNeill 7:1add29d51e72 786 DistanceMode);
Charles MacNeill 7:1add29d51e72 787 }
Charles MacNeill 7:1add29d51e72 788
Charles MacNeill 7:1add29d51e72 789 if (Status == VL53L1_ERROR_NONE) {
Charles MacNeill 7:1add29d51e72 790 Status = VL53L1_set_timeouts_us(Dev, PhaseCalTimeoutUs,
Charles MacNeill 7:1add29d51e72 791 MmTimeoutUs, TimingBudget);
Charles MacNeill 7:1add29d51e72 792
Charles MacNeill 7:1add29d51e72 793 if (Status == VL53L1_ERROR_NONE)
Charles MacNeill 7:1add29d51e72 794 VL53L1DevDataSet(Dev, LLData.range_config_timeout_us,
Charles MacNeill 7:1add29d51e72 795 TimingBudget);
Charles MacNeill 7:1add29d51e72 796 }
Charles MacNeill 7:1add29d51e72 797
Charles MacNeill 7:1add29d51e72 798 if (Status == VL53L1_ERROR_NONE)
Charles MacNeill 7:1add29d51e72 799 Status = VL53L1_set_zone_config(Dev, &zone_config);
Charles MacNeill 7:1add29d51e72 800
Charles MacNeill 7:1add29d51e72 801 LOG_FUNCTION_END(Status);
Charles MacNeill 7:1add29d51e72 802 return Status;
Charles MacNeill 7:1add29d51e72 803 }
Charles MacNeill 7:1add29d51e72 804
Charles MacNeill 7:1add29d51e72 805 VL53L1_Error VL53L1_GetDistanceMode(VL53L1_DEV Dev,
Charles MacNeill 7:1add29d51e72 806 VL53L1_DistanceModes *pDistanceMode)
Charles MacNeill 7:1add29d51e72 807 {
Charles MacNeill 7:1add29d51e72 808 VL53L1_Error Status = VL53L1_ERROR_NONE;
Charles MacNeill 7:1add29d51e72 809
Charles MacNeill 7:1add29d51e72 810 LOG_FUNCTION_START("");
Charles MacNeill 7:1add29d51e72 811
Charles MacNeill 7:1add29d51e72 812 *pDistanceMode = VL53L1DevDataGet(Dev, CurrentParameters.DistanceMode);
Charles MacNeill 7:1add29d51e72 813
Charles MacNeill 7:1add29d51e72 814 LOG_FUNCTION_END(Status);
Charles MacNeill 7:1add29d51e72 815 return Status;
Charles MacNeill 7:1add29d51e72 816 }
Charles MacNeill 7:1add29d51e72 817
Charles MacNeill 7:1add29d51e72 818 VL53L1_Error VL53L1_SetOutputMode(VL53L1_DEV Dev,
Charles MacNeill 7:1add29d51e72 819 VL53L1_OutputModes OutputMode)
Charles MacNeill 7:1add29d51e72 820 {
Charles MacNeill 7:1add29d51e72 821 VL53L1_Error Status = VL53L1_ERROR_NONE;
Charles MacNeill 7:1add29d51e72 822
Charles MacNeill 7:1add29d51e72 823 LOG_FUNCTION_START("");
Charles MacNeill 7:1add29d51e72 824
Charles MacNeill 7:1add29d51e72 825 if ((OutputMode != VL53L1_OUTPUTMODE_NEAREST) &&
Charles MacNeill 7:1add29d51e72 826 (OutputMode != VL53L1_OUTPUTMODE_STRONGEST))
Charles MacNeill 7:1add29d51e72 827 Status = VL53L1_ERROR_MODE_NOT_SUPPORTED;
Charles MacNeill 7:1add29d51e72 828 else
Charles MacNeill 7:1add29d51e72 829 VL53L1DevDataSet(Dev, CurrentParameters.OutputMode, OutputMode);
Charles MacNeill 7:1add29d51e72 830
Charles MacNeill 7:1add29d51e72 831 LOG_FUNCTION_END(Status);
Charles MacNeill 7:1add29d51e72 832 return Status;
Charles MacNeill 7:1add29d51e72 833 }
Charles MacNeill 7:1add29d51e72 834
Charles MacNeill 7:1add29d51e72 835 VL53L1_Error VL53L1_GetOutputMode(VL53L1_DEV Dev,
Charles MacNeill 7:1add29d51e72 836 VL53L1_OutputModes *pOutputMode)
Charles MacNeill 7:1add29d51e72 837 {
Charles MacNeill 7:1add29d51e72 838 VL53L1_Error Status = VL53L1_ERROR_NONE;
Charles MacNeill 7:1add29d51e72 839
Charles MacNeill 7:1add29d51e72 840 LOG_FUNCTION_START("");
Charles MacNeill 7:1add29d51e72 841
Charles MacNeill 7:1add29d51e72 842 *pOutputMode = VL53L1DevDataGet(Dev, CurrentParameters.OutputMode);
Charles MacNeill 7:1add29d51e72 843
Charles MacNeill 7:1add29d51e72 844 LOG_FUNCTION_END(Status);
Charles MacNeill 7:1add29d51e72 845 return Status;
Charles MacNeill 7:1add29d51e72 846 }
Charles MacNeill 7:1add29d51e72 847
Charles MacNeill 7:1add29d51e72 848
Charles MacNeill 7:1add29d51e72 849
Charles MacNeill 7:1add29d51e72 850 VL53L1_Error VL53L1_SetMeasurementTimingBudgetMicroSeconds(VL53L1_DEV Dev,
Charles MacNeill 7:1add29d51e72 851 uint32_t MeasurementTimingBudgetMicroSeconds)
Charles MacNeill 7:1add29d51e72 852 {
Charles MacNeill 7:1add29d51e72 853 VL53L1_Error Status = VL53L1_ERROR_NONE;
Charles MacNeill 7:1add29d51e72 854 uint8_t Mm1Enabled;
Charles MacNeill 7:1add29d51e72 855 uint8_t Mm2Enabled;
Charles MacNeill 7:1add29d51e72 856 uint32_t TimingGuard;
Charles MacNeill 7:1add29d51e72 857 uint32_t divisor;
Charles MacNeill 7:1add29d51e72 858 uint32_t TimingBudget;
Charles MacNeill 7:1add29d51e72 859 uint32_t MmTimeoutUs;
Charles MacNeill 7:1add29d51e72 860 VL53L1_PresetModes PresetMode;
Charles MacNeill 7:1add29d51e72 861 uint32_t PhaseCalTimeoutUs;
Charles MacNeill 7:1add29d51e72 862 uint32_t vhv;
Charles MacNeill 7:1add29d51e72 863 int32_t vhv_loops;
Charles MacNeill 7:1add29d51e72 864 uint32_t FDAMaxTimingBudgetUs = FDA_MAX_TIMING_BUDGET_US;
Charles MacNeill 7:1add29d51e72 865
Charles MacNeill 7:1add29d51e72 866 LOG_FUNCTION_START("");
Charles MacNeill 7:1add29d51e72 867
Charles MacNeill 7:1add29d51e72 868
Charles MacNeill 7:1add29d51e72 869 if (MeasurementTimingBudgetMicroSeconds > 10000000)
Charles MacNeill 7:1add29d51e72 870 Status = VL53L1_ERROR_INVALID_PARAMS;
Charles MacNeill 7:1add29d51e72 871
Charles MacNeill 7:1add29d51e72 872 if (Status == VL53L1_ERROR_NONE) {
Charles MacNeill 7:1add29d51e72 873 Status = VL53L1_GetSequenceStepEnable(Dev,
Charles MacNeill 7:1add29d51e72 874 VL53L1_SEQUENCESTEP_MM1, &Mm1Enabled);
Charles MacNeill 7:1add29d51e72 875 }
Charles MacNeill 7:1add29d51e72 876
Charles MacNeill 7:1add29d51e72 877 if (Status == VL53L1_ERROR_NONE) {
Charles MacNeill 7:1add29d51e72 878 Status = VL53L1_GetSequenceStepEnable(Dev,
Charles MacNeill 7:1add29d51e72 879 VL53L1_SEQUENCESTEP_MM2, &Mm2Enabled);
Charles MacNeill 7:1add29d51e72 880 }
Charles MacNeill 7:1add29d51e72 881
Charles MacNeill 7:1add29d51e72 882 if (Status == VL53L1_ERROR_NONE)
Charles MacNeill 7:1add29d51e72 883 Status = VL53L1_get_timeouts_us(Dev,
Charles MacNeill 7:1add29d51e72 884 &PhaseCalTimeoutUs,
Charles MacNeill 7:1add29d51e72 885 &MmTimeoutUs,
Charles MacNeill 7:1add29d51e72 886 &TimingBudget);
Charles MacNeill 7:1add29d51e72 887
Charles MacNeill 7:1add29d51e72 888 if (Status == VL53L1_ERROR_NONE) {
Charles MacNeill 7:1add29d51e72 889 PresetMode = VL53L1DevDataGet(Dev,
Charles MacNeill 7:1add29d51e72 890 CurrentParameters.PresetMode);
Charles MacNeill 7:1add29d51e72 891
Charles MacNeill 7:1add29d51e72 892 TimingGuard = 0;
Charles MacNeill 7:1add29d51e72 893 divisor = 1;
Charles MacNeill 7:1add29d51e72 894 switch (PresetMode) {
Charles MacNeill 7:1add29d51e72 895 case VL53L1_PRESETMODE_LITE_RANGING:
Charles MacNeill 7:1add29d51e72 896 if ((Mm1Enabled == 1) || (Mm2Enabled == 1))
Charles MacNeill 7:1add29d51e72 897 TimingGuard = 5000;
Charles MacNeill 7:1add29d51e72 898 else
Charles MacNeill 7:1add29d51e72 899 TimingGuard = 1000;
Charles MacNeill 7:1add29d51e72 900 break;
Charles MacNeill 7:1add29d51e72 901
Charles MacNeill 7:1add29d51e72 902 case VL53L1_PRESETMODE_AUTONOMOUS:
Charles MacNeill 7:1add29d51e72 903 FDAMaxTimingBudgetUs *= 2;
Charles MacNeill 7:1add29d51e72 904 if ((Mm1Enabled == 1) || (Mm2Enabled == 1))
Charles MacNeill 7:1add29d51e72 905 TimingGuard = 26600;
Charles MacNeill 7:1add29d51e72 906 else
Charles MacNeill 7:1add29d51e72 907 TimingGuard = 21600;
Charles MacNeill 7:1add29d51e72 908 divisor = 2;
Charles MacNeill 7:1add29d51e72 909 break;
Charles MacNeill 7:1add29d51e72 910
Charles MacNeill 7:1add29d51e72 911 case VL53L1_PRESETMODE_LOWPOWER_AUTONOMOUS:
Charles MacNeill 7:1add29d51e72 912 FDAMaxTimingBudgetUs *= 2;
Charles MacNeill 7:1add29d51e72 913 vhv = LOWPOWER_AUTO_VHV_LOOP_DURATION_US;
Charles MacNeill 7:1add29d51e72 914 VL53L1_get_tuning_parm(Dev,
Charles MacNeill 7:1add29d51e72 915 VL53L1_TUNINGPARM_LOWPOWERAUTO_VHV_LOOP_BOUND,
Charles MacNeill 7:1add29d51e72 916 &vhv_loops);
Charles MacNeill 7:1add29d51e72 917 if (vhv_loops > 0) {
Charles MacNeill 7:1add29d51e72 918 vhv += vhv_loops *
Charles MacNeill 7:1add29d51e72 919 LOWPOWER_AUTO_VHV_LOOP_DURATION_US;
Charles MacNeill 7:1add29d51e72 920 }
Charles MacNeill 7:1add29d51e72 921 TimingGuard = LOWPOWER_AUTO_OVERHEAD_BEFORE_A_RANGING +
Charles MacNeill 7:1add29d51e72 922 LOWPOWER_AUTO_OVERHEAD_BETWEEN_A_B_RANGING +
Charles MacNeill 7:1add29d51e72 923 vhv;
Charles MacNeill 7:1add29d51e72 924 divisor = 2;
Charles MacNeill 7:1add29d51e72 925 break;
Charles MacNeill 7:1add29d51e72 926
Charles MacNeill 7:1add29d51e72 927 case VL53L1_PRESETMODE_RANGING:
Charles MacNeill 7:1add29d51e72 928 case VL53L1_PRESETMODE_MULTIZONES_SCANNING:
Charles MacNeill 7:1add29d51e72 929 case VL53L1_PRESETMODE_PROXY_RANGING_MODE:
Charles MacNeill 7:1add29d51e72 930 TimingGuard = 1700;
Charles MacNeill 7:1add29d51e72 931 divisor = 6;
Charles MacNeill 7:1add29d51e72 932 break;
Charles MacNeill 7:1add29d51e72 933
Charles MacNeill 7:1add29d51e72 934 case VL53L1_PRESETMODE_OLT:
Charles MacNeill 7:1add29d51e72 935 TimingGuard = MmTimeoutUs + 5000;
Charles MacNeill 7:1add29d51e72 936 break;
Charles MacNeill 7:1add29d51e72 937 default:
Charles MacNeill 7:1add29d51e72 938
Charles MacNeill 7:1add29d51e72 939 Status = VL53L1_ERROR_MODE_NOT_SUPPORTED;
Charles MacNeill 7:1add29d51e72 940 }
Charles MacNeill 7:1add29d51e72 941
Charles MacNeill 7:1add29d51e72 942 if (MeasurementTimingBudgetMicroSeconds <= TimingGuard)
Charles MacNeill 7:1add29d51e72 943 Status = VL53L1_ERROR_INVALID_PARAMS;
Charles MacNeill 7:1add29d51e72 944 else {
Charles MacNeill 7:1add29d51e72 945 TimingBudget = (MeasurementTimingBudgetMicroSeconds
Charles MacNeill 7:1add29d51e72 946 - TimingGuard);
Charles MacNeill 7:1add29d51e72 947 }
Charles MacNeill 7:1add29d51e72 948
Charles MacNeill 7:1add29d51e72 949 if (Status == VL53L1_ERROR_NONE) {
Charles MacNeill 7:1add29d51e72 950 if (TimingBudget > FDAMaxTimingBudgetUs)
Charles MacNeill 7:1add29d51e72 951 Status = VL53L1_ERROR_INVALID_PARAMS;
Charles MacNeill 7:1add29d51e72 952 else {
Charles MacNeill 7:1add29d51e72 953 TimingBudget /= divisor;
Charles MacNeill 7:1add29d51e72 954 Status = VL53L1_set_timeouts_us(
Charles MacNeill 7:1add29d51e72 955 Dev,
Charles MacNeill 7:1add29d51e72 956 PhaseCalTimeoutUs,
Charles MacNeill 7:1add29d51e72 957 MmTimeoutUs,
Charles MacNeill 7:1add29d51e72 958 TimingBudget);
Charles MacNeill 7:1add29d51e72 959 }
Charles MacNeill 7:1add29d51e72 960
Charles MacNeill 7:1add29d51e72 961 if (Status == VL53L1_ERROR_NONE)
Charles MacNeill 7:1add29d51e72 962 VL53L1DevDataSet(Dev,
Charles MacNeill 7:1add29d51e72 963 LLData.range_config_timeout_us,
Charles MacNeill 7:1add29d51e72 964 TimingBudget);
Charles MacNeill 7:1add29d51e72 965 }
Charles MacNeill 7:1add29d51e72 966 }
Charles MacNeill 7:1add29d51e72 967 if (Status == VL53L1_ERROR_NONE) {
Charles MacNeill 7:1add29d51e72 968 VL53L1DevDataSet(Dev,
Charles MacNeill 7:1add29d51e72 969 CurrentParameters.MeasurementTimingBudgetMicroSeconds,
Charles MacNeill 7:1add29d51e72 970 MeasurementTimingBudgetMicroSeconds);
Charles MacNeill 7:1add29d51e72 971 }
Charles MacNeill 7:1add29d51e72 972
Charles MacNeill 7:1add29d51e72 973 LOG_FUNCTION_END(Status);
Charles MacNeill 7:1add29d51e72 974 return Status;
Charles MacNeill 7:1add29d51e72 975 }
Charles MacNeill 7:1add29d51e72 976
Charles MacNeill 7:1add29d51e72 977
Charles MacNeill 7:1add29d51e72 978 VL53L1_Error VL53L1_GetMeasurementTimingBudgetMicroSeconds(VL53L1_DEV Dev,
Charles MacNeill 7:1add29d51e72 979 uint32_t *pMeasurementTimingBudgetMicroSeconds)
Charles MacNeill 7:1add29d51e72 980 {
Charles MacNeill 7:1add29d51e72 981 VL53L1_Error Status = VL53L1_ERROR_NONE;
Charles MacNeill 7:1add29d51e72 982 uint8_t Mm1Enabled = 0;
Charles MacNeill 7:1add29d51e72 983 uint8_t Mm2Enabled = 0;
Charles MacNeill 7:1add29d51e72 984 uint32_t MmTimeoutUs = 0;
Charles MacNeill 7:1add29d51e72 985 uint32_t RangeTimeoutUs = 0;
Charles MacNeill 7:1add29d51e72 986 uint32_t MeasTimingBdg = 0;
Charles MacNeill 7:1add29d51e72 987 uint32_t PhaseCalTimeoutUs = 0;
Charles MacNeill 7:1add29d51e72 988 VL53L1_PresetModes PresetMode;
Charles MacNeill 7:1add29d51e72 989 uint32_t TimingGuard;
Charles MacNeill 7:1add29d51e72 990 uint32_t vhv;
Charles MacNeill 7:1add29d51e72 991 int32_t vhv_loops;
Charles MacNeill 7:1add29d51e72 992
Charles MacNeill 7:1add29d51e72 993 LOG_FUNCTION_START("");
Charles MacNeill 7:1add29d51e72 994
Charles MacNeill 7:1add29d51e72 995 *pMeasurementTimingBudgetMicroSeconds = 0;
Charles MacNeill 7:1add29d51e72 996
Charles MacNeill 7:1add29d51e72 997 if (Status == VL53L1_ERROR_NONE)
Charles MacNeill 7:1add29d51e72 998 Status = VL53L1_GetSequenceStepEnable(Dev,
Charles MacNeill 7:1add29d51e72 999 VL53L1_SEQUENCESTEP_MM1, &Mm1Enabled);
Charles MacNeill 7:1add29d51e72 1000
Charles MacNeill 7:1add29d51e72 1001 if (Status == VL53L1_ERROR_NONE)
Charles MacNeill 7:1add29d51e72 1002 Status = VL53L1_GetSequenceStepEnable(Dev,
Charles MacNeill 7:1add29d51e72 1003 VL53L1_SEQUENCESTEP_MM2, &Mm2Enabled);
Charles MacNeill 7:1add29d51e72 1004
Charles MacNeill 7:1add29d51e72 1005 if (Status == VL53L1_ERROR_NONE)
Charles MacNeill 7:1add29d51e72 1006 Status = VL53L1_get_timeouts_us(Dev,
Charles MacNeill 7:1add29d51e72 1007 &PhaseCalTimeoutUs,
Charles MacNeill 7:1add29d51e72 1008 &MmTimeoutUs,
Charles MacNeill 7:1add29d51e72 1009 &RangeTimeoutUs);
Charles MacNeill 7:1add29d51e72 1010
Charles MacNeill 7:1add29d51e72 1011 if (Status == VL53L1_ERROR_NONE) {
Charles MacNeill 7:1add29d51e72 1012 PresetMode = VL53L1DevDataGet(Dev,
Charles MacNeill 7:1add29d51e72 1013 CurrentParameters.PresetMode);
Charles MacNeill 7:1add29d51e72 1014
Charles MacNeill 7:1add29d51e72 1015 switch (PresetMode) {
Charles MacNeill 7:1add29d51e72 1016 case VL53L1_PRESETMODE_LITE_RANGING:
Charles MacNeill 7:1add29d51e72 1017 if ((Mm1Enabled == 1) || (Mm2Enabled == 1))
Charles MacNeill 7:1add29d51e72 1018 MeasTimingBdg = RangeTimeoutUs + 5000;
Charles MacNeill 7:1add29d51e72 1019 else
Charles MacNeill 7:1add29d51e72 1020 MeasTimingBdg = RangeTimeoutUs + 1000;
Charles MacNeill 7:1add29d51e72 1021
Charles MacNeill 7:1add29d51e72 1022 break;
Charles MacNeill 7:1add29d51e72 1023
Charles MacNeill 7:1add29d51e72 1024 case VL53L1_PRESETMODE_AUTONOMOUS:
Charles MacNeill 7:1add29d51e72 1025 if ((Mm1Enabled == 1) || (Mm2Enabled == 1))
Charles MacNeill 7:1add29d51e72 1026 MeasTimingBdg = 2 * RangeTimeoutUs + 26600;
Charles MacNeill 7:1add29d51e72 1027 else
Charles MacNeill 7:1add29d51e72 1028 MeasTimingBdg = 2 * RangeTimeoutUs + 21600;
Charles MacNeill 7:1add29d51e72 1029
Charles MacNeill 7:1add29d51e72 1030 break;
Charles MacNeill 7:1add29d51e72 1031
Charles MacNeill 7:1add29d51e72 1032 case VL53L1_PRESETMODE_LOWPOWER_AUTONOMOUS:
Charles MacNeill 7:1add29d51e72 1033 vhv = LOWPOWER_AUTO_VHV_LOOP_DURATION_US;
Charles MacNeill 7:1add29d51e72 1034 VL53L1_get_tuning_parm(Dev,
Charles MacNeill 7:1add29d51e72 1035 VL53L1_TUNINGPARM_LOWPOWERAUTO_VHV_LOOP_BOUND,
Charles MacNeill 7:1add29d51e72 1036 &vhv_loops);
Charles MacNeill 7:1add29d51e72 1037 if (vhv_loops > 0) {
Charles MacNeill 7:1add29d51e72 1038 vhv += vhv_loops *
Charles MacNeill 7:1add29d51e72 1039 LOWPOWER_AUTO_VHV_LOOP_DURATION_US;
Charles MacNeill 7:1add29d51e72 1040 }
Charles MacNeill 7:1add29d51e72 1041 TimingGuard = LOWPOWER_AUTO_OVERHEAD_BEFORE_A_RANGING +
Charles MacNeill 7:1add29d51e72 1042 LOWPOWER_AUTO_OVERHEAD_BETWEEN_A_B_RANGING +
Charles MacNeill 7:1add29d51e72 1043 vhv;
Charles MacNeill 7:1add29d51e72 1044 MeasTimingBdg = 2 * RangeTimeoutUs + TimingGuard;
Charles MacNeill 7:1add29d51e72 1045 break;
Charles MacNeill 7:1add29d51e72 1046
Charles MacNeill 7:1add29d51e72 1047 case VL53L1_PRESETMODE_RANGING:
Charles MacNeill 7:1add29d51e72 1048 case VL53L1_PRESETMODE_MULTIZONES_SCANNING:
Charles MacNeill 7:1add29d51e72 1049 case VL53L1_PRESETMODE_PROXY_RANGING_MODE:
Charles MacNeill 7:1add29d51e72 1050 MeasTimingBdg = (6 * RangeTimeoutUs) + 1700;
Charles MacNeill 7:1add29d51e72 1051 break;
Charles MacNeill 7:1add29d51e72 1052
Charles MacNeill 7:1add29d51e72 1053 case VL53L1_PRESETMODE_OLT:
Charles MacNeill 7:1add29d51e72 1054 MeasTimingBdg = RangeTimeoutUs + MmTimeoutUs + 5000;
Charles MacNeill 7:1add29d51e72 1055 break;
Charles MacNeill 7:1add29d51e72 1056 default:
Charles MacNeill 7:1add29d51e72 1057
Charles MacNeill 7:1add29d51e72 1058 Status = VL53L1_ERROR_MODE_NOT_SUPPORTED;
Charles MacNeill 7:1add29d51e72 1059 }
Charles MacNeill 7:1add29d51e72 1060 }
Charles MacNeill 7:1add29d51e72 1061 if (Status == VL53L1_ERROR_NONE)
Charles MacNeill 7:1add29d51e72 1062 *pMeasurementTimingBudgetMicroSeconds = MeasTimingBdg;
Charles MacNeill 7:1add29d51e72 1063
Charles MacNeill 7:1add29d51e72 1064 LOG_FUNCTION_END(Status);
Charles MacNeill 7:1add29d51e72 1065 return Status;
Charles MacNeill 7:1add29d51e72 1066 }
Charles MacNeill 7:1add29d51e72 1067
Charles MacNeill 7:1add29d51e72 1068
Charles MacNeill 7:1add29d51e72 1069
Charles MacNeill 7:1add29d51e72 1070 VL53L1_Error VL53L1_SetInterMeasurementPeriodMilliSeconds(VL53L1_DEV Dev,
Charles MacNeill 7:1add29d51e72 1071 uint32_t InterMeasurementPeriodMilliSeconds)
Charles MacNeill 7:1add29d51e72 1072 {
Charles MacNeill 7:1add29d51e72 1073 VL53L1_Error Status = VL53L1_ERROR_NONE;
Charles MacNeill 7:1add29d51e72 1074 uint32_t adjustedIMP;
Charles MacNeill 7:1add29d51e72 1075
Charles MacNeill 7:1add29d51e72 1076 LOG_FUNCTION_START("");
Charles MacNeill 7:1add29d51e72 1077
Charles MacNeill 7:1add29d51e72 1078
Charles MacNeill 7:1add29d51e72 1079 adjustedIMP = InterMeasurementPeriodMilliSeconds;
Charles MacNeill 7:1add29d51e72 1080 adjustedIMP += (adjustedIMP * 64) / 1000;
Charles MacNeill 7:1add29d51e72 1081
Charles MacNeill 7:1add29d51e72 1082 Status = VL53L1_set_inter_measurement_period_ms(Dev,
Charles MacNeill 7:1add29d51e72 1083 adjustedIMP);
Charles MacNeill 7:1add29d51e72 1084
Charles MacNeill 7:1add29d51e72 1085 LOG_FUNCTION_END(Status);
Charles MacNeill 7:1add29d51e72 1086 return Status;
Charles MacNeill 7:1add29d51e72 1087 }
Charles MacNeill 7:1add29d51e72 1088
Charles MacNeill 7:1add29d51e72 1089 VL53L1_Error VL53L1_GetInterMeasurementPeriodMilliSeconds(VL53L1_DEV Dev,
Charles MacNeill 7:1add29d51e72 1090 uint32_t *pInterMeasurementPeriodMilliSeconds)
Charles MacNeill 7:1add29d51e72 1091 {
Charles MacNeill 7:1add29d51e72 1092 VL53L1_Error Status = VL53L1_ERROR_NONE;
Charles MacNeill 7:1add29d51e72 1093 uint32_t adjustedIMP;
Charles MacNeill 7:1add29d51e72 1094
Charles MacNeill 7:1add29d51e72 1095 LOG_FUNCTION_START("");
Charles MacNeill 7:1add29d51e72 1096
Charles MacNeill 7:1add29d51e72 1097 Status = VL53L1_get_inter_measurement_period_ms(Dev, &adjustedIMP);
Charles MacNeill 7:1add29d51e72 1098
Charles MacNeill 7:1add29d51e72 1099 adjustedIMP -= (adjustedIMP * 64) / 1000;
Charles MacNeill 7:1add29d51e72 1100 *pInterMeasurementPeriodMilliSeconds = adjustedIMP;
Charles MacNeill 7:1add29d51e72 1101
Charles MacNeill 7:1add29d51e72 1102
Charles MacNeill 7:1add29d51e72 1103 LOG_FUNCTION_END(Status);
Charles MacNeill 7:1add29d51e72 1104 return Status;
Charles MacNeill 7:1add29d51e72 1105 }
Charles MacNeill 7:1add29d51e72 1106
Charles MacNeill 7:1add29d51e72 1107 VL53L1_Error VL53L1_SetDmaxReflectance(VL53L1_DEV Dev,
Charles MacNeill 7:1add29d51e72 1108 FixPoint1616_t DmaxReflectance)
Charles MacNeill 7:1add29d51e72 1109 {
Charles MacNeill 7:1add29d51e72 1110 VL53L1_Error Status = VL53L1_ERROR_NONE;
Charles MacNeill 7:1add29d51e72 1111 VL53L1_dmax_reflectance_array_t dmax_reflectances;
Charles MacNeill 7:1add29d51e72 1112
Charles MacNeill 7:1add29d51e72 1113 LOG_FUNCTION_START("");
Charles MacNeill 7:1add29d51e72 1114
Charles MacNeill 7:1add29d51e72 1115 if (DmaxReflectance > 100*65536)
Charles MacNeill 7:1add29d51e72 1116 Status = VL53L1_ERROR_INVALID_PARAMS;
Charles MacNeill 7:1add29d51e72 1117
Charles MacNeill 7:1add29d51e72 1118 if (Status == VL53L1_ERROR_NONE)
Charles MacNeill 7:1add29d51e72 1119 Status = VL53L1_get_dmax_reflectance_values(Dev,
Charles MacNeill 7:1add29d51e72 1120 &dmax_reflectances);
Charles MacNeill 7:1add29d51e72 1121
Charles MacNeill 7:1add29d51e72 1122 if (Status == VL53L1_ERROR_NONE) {
Charles MacNeill 7:1add29d51e72 1123 dmax_reflectances.target_reflectance_for_dmax[
Charles MacNeill 7:1add29d51e72 1124 DMAX_REFLECTANCE_IDX] =
Charles MacNeill 7:1add29d51e72 1125 VL53L1_FIXPOINT1616TOFIXPOINT72(DmaxReflectance);
Charles MacNeill 7:1add29d51e72 1126 Status = VL53L1_set_dmax_reflectance_values(Dev,
Charles MacNeill 7:1add29d51e72 1127 &dmax_reflectances);
Charles MacNeill 7:1add29d51e72 1128 }
Charles MacNeill 7:1add29d51e72 1129
Charles MacNeill 7:1add29d51e72 1130 LOG_FUNCTION_END(Status);
Charles MacNeill 7:1add29d51e72 1131 return Status;
Charles MacNeill 7:1add29d51e72 1132 }
Charles MacNeill 7:1add29d51e72 1133
Charles MacNeill 7:1add29d51e72 1134 VL53L1_Error VL53L1_GetDmaxReflectance(VL53L1_DEV Dev,
Charles MacNeill 7:1add29d51e72 1135 FixPoint1616_t *pDmaxReflectance)
Charles MacNeill 7:1add29d51e72 1136 {
Charles MacNeill 7:1add29d51e72 1137 VL53L1_Error Status = VL53L1_ERROR_NONE;
Charles MacNeill 7:1add29d51e72 1138 VL53L1_dmax_reflectance_array_t dmax_reflectances;
Charles MacNeill 7:1add29d51e72 1139 uint16_t r;
Charles MacNeill 7:1add29d51e72 1140
Charles MacNeill 7:1add29d51e72 1141 LOG_FUNCTION_START("");
Charles MacNeill 7:1add29d51e72 1142 Status = VL53L1_get_dmax_reflectance_values(Dev, &dmax_reflectances);
Charles MacNeill 7:1add29d51e72 1143 if (Status == VL53L1_ERROR_NONE) {
Charles MacNeill 7:1add29d51e72 1144 r = dmax_reflectances.target_reflectance_for_dmax[
Charles MacNeill 7:1add29d51e72 1145 DMAX_REFLECTANCE_IDX];
Charles MacNeill 7:1add29d51e72 1146 *pDmaxReflectance = VL53L1_FIXPOINT72TOFIXPOINT1616(r);
Charles MacNeill 7:1add29d51e72 1147 }
Charles MacNeill 7:1add29d51e72 1148
Charles MacNeill 7:1add29d51e72 1149 LOG_FUNCTION_END(Status);
Charles MacNeill 7:1add29d51e72 1150 return Status;
Charles MacNeill 7:1add29d51e72 1151 }
Charles MacNeill 7:1add29d51e72 1152
Charles MacNeill 7:1add29d51e72 1153
Charles MacNeill 7:1add29d51e72 1154 VL53L1_Error VL53L1_SetDmaxMode(VL53L1_DEV Dev,
Charles MacNeill 7:1add29d51e72 1155 VL53L1_DeviceDmaxModes DmaxMode)
Charles MacNeill 7:1add29d51e72 1156 {
Charles MacNeill 7:1add29d51e72 1157
Charles MacNeill 7:1add29d51e72 1158 VL53L1_Error Status = VL53L1_ERROR_NONE;
Charles MacNeill 7:1add29d51e72 1159 VL53L1_DeviceDmaxMode dmax_mode;
Charles MacNeill 7:1add29d51e72 1160
Charles MacNeill 7:1add29d51e72 1161 LOG_FUNCTION_START("");
Charles MacNeill 7:1add29d51e72 1162
Charles MacNeill 7:1add29d51e72 1163 switch (DmaxMode) {
Charles MacNeill 7:1add29d51e72 1164 case VL53L1_DMAXMODE_FMT_CAL_DATA:
Charles MacNeill 7:1add29d51e72 1165 dmax_mode = VL53L1_DEVICEDMAXMODE__FMT_CAL_DATA;
Charles MacNeill 7:1add29d51e72 1166 break;
Charles MacNeill 7:1add29d51e72 1167 case VL53L1_DMAXMODE_CUSTCAL_DATA:
Charles MacNeill 7:1add29d51e72 1168 dmax_mode = VL53L1_DEVICEDMAXMODE__CUST_CAL_DATA;
Charles MacNeill 7:1add29d51e72 1169 break;
Charles MacNeill 7:1add29d51e72 1170 case VL53L1_DMAXMODE_PER_ZONE_CAL_DATA:
Charles MacNeill 7:1add29d51e72 1171 dmax_mode = VL53L1_DEVICEDMAXMODE__PER_ZONE_CAL_DATA;
Charles MacNeill 7:1add29d51e72 1172 break;
Charles MacNeill 7:1add29d51e72 1173 default:
Charles MacNeill 7:1add29d51e72 1174 Status = VL53L1_ERROR_INVALID_PARAMS;
Charles MacNeill 7:1add29d51e72 1175 break;
Charles MacNeill 7:1add29d51e72 1176 }
Charles MacNeill 7:1add29d51e72 1177 if (Status == VL53L1_ERROR_NONE)
Charles MacNeill 7:1add29d51e72 1178 Status = VL53L1_set_dmax_mode(Dev, dmax_mode);
Charles MacNeill 7:1add29d51e72 1179
Charles MacNeill 7:1add29d51e72 1180 LOG_FUNCTION_END(Status);
Charles MacNeill 7:1add29d51e72 1181 return Status;
Charles MacNeill 7:1add29d51e72 1182 }
Charles MacNeill 7:1add29d51e72 1183
Charles MacNeill 7:1add29d51e72 1184
Charles MacNeill 7:1add29d51e72 1185 VL53L1_Error VL53L1_GetDmaxMode(VL53L1_DEV Dev,
Charles MacNeill 7:1add29d51e72 1186 VL53L1_DeviceDmaxModes *pDmaxMode)
Charles MacNeill 7:1add29d51e72 1187 {
Charles MacNeill 7:1add29d51e72 1188 VL53L1_Error Status = VL53L1_ERROR_NONE;
Charles MacNeill 7:1add29d51e72 1189 VL53L1_DeviceDmaxMode dmax_mode;
Charles MacNeill 7:1add29d51e72 1190
Charles MacNeill 7:1add29d51e72 1191 LOG_FUNCTION_START("");
Charles MacNeill 7:1add29d51e72 1192
Charles MacNeill 7:1add29d51e72 1193 Status = VL53L1_get_dmax_mode(Dev, &dmax_mode);
Charles MacNeill 7:1add29d51e72 1194 if (Status == VL53L1_ERROR_NONE) {
Charles MacNeill 7:1add29d51e72 1195 switch (dmax_mode) {
Charles MacNeill 7:1add29d51e72 1196 case VL53L1_DEVICEDMAXMODE__FMT_CAL_DATA:
Charles MacNeill 7:1add29d51e72 1197 *pDmaxMode = VL53L1_DMAXMODE_FMT_CAL_DATA;
Charles MacNeill 7:1add29d51e72 1198 break;
Charles MacNeill 7:1add29d51e72 1199 case VL53L1_DEVICEDMAXMODE__CUST_CAL_DATA:
Charles MacNeill 7:1add29d51e72 1200 *pDmaxMode = VL53L1_DMAXMODE_CUSTCAL_DATA;
Charles MacNeill 7:1add29d51e72 1201 break;
Charles MacNeill 7:1add29d51e72 1202 case VL53L1_DEVICEDMAXMODE__PER_ZONE_CAL_DATA:
Charles MacNeill 7:1add29d51e72 1203 *pDmaxMode = VL53L1_DMAXMODE_PER_ZONE_CAL_DATA;
Charles MacNeill 7:1add29d51e72 1204 break;
Charles MacNeill 7:1add29d51e72 1205 default:
Charles MacNeill 7:1add29d51e72 1206 *pDmaxMode = VL53L1_DEVICEDMAXMODE__CUST_CAL_DATA;
Charles MacNeill 7:1add29d51e72 1207 Status = VL53L1_ERROR_NOT_IMPLEMENTED;
Charles MacNeill 7:1add29d51e72 1208 break;
Charles MacNeill 7:1add29d51e72 1209 }
Charles MacNeill 7:1add29d51e72 1210 }
Charles MacNeill 7:1add29d51e72 1211
Charles MacNeill 7:1add29d51e72 1212 LOG_FUNCTION_END(Status);
Charles MacNeill 7:1add29d51e72 1213 return Status;
Charles MacNeill 7:1add29d51e72 1214 }
Charles MacNeill 7:1add29d51e72 1215
Charles MacNeill 7:1add29d51e72 1216
Charles MacNeill 7:1add29d51e72 1217
Charles MacNeill 7:1add29d51e72 1218
Charles MacNeill 7:1add29d51e72 1219
Charles MacNeill 7:1add29d51e72 1220
Charles MacNeill 7:1add29d51e72 1221 VL53L1_Error VL53L1_GetNumberOfLimitCheck(uint16_t *pNumberOfLimitCheck)
Charles MacNeill 7:1add29d51e72 1222 {
Charles MacNeill 7:1add29d51e72 1223 VL53L1_Error Status = VL53L1_ERROR_NONE;
Charles MacNeill 7:1add29d51e72 1224
Charles MacNeill 7:1add29d51e72 1225 LOG_FUNCTION_START("");
Charles MacNeill 7:1add29d51e72 1226
Charles MacNeill 7:1add29d51e72 1227 *pNumberOfLimitCheck = VL53L1_CHECKENABLE_NUMBER_OF_CHECKS;
Charles MacNeill 7:1add29d51e72 1228
Charles MacNeill 7:1add29d51e72 1229 LOG_FUNCTION_END(Status);
Charles MacNeill 7:1add29d51e72 1230 return Status;
Charles MacNeill 7:1add29d51e72 1231 }
Charles MacNeill 7:1add29d51e72 1232
Charles MacNeill 7:1add29d51e72 1233 VL53L1_Error VL53L1_GetLimitCheckInfo(uint16_t LimitCheckId,
Charles MacNeill 7:1add29d51e72 1234 char *pLimitCheckString)
Charles MacNeill 7:1add29d51e72 1235 {
Charles MacNeill 7:1add29d51e72 1236 VL53L1_Error Status = VL53L1_ERROR_NONE;
Charles MacNeill 7:1add29d51e72 1237
Charles MacNeill 7:1add29d51e72 1238 LOG_FUNCTION_START("");
Charles MacNeill 7:1add29d51e72 1239
Charles MacNeill 7:1add29d51e72 1240 Status = VL53L1_get_limit_check_info(LimitCheckId,
Charles MacNeill 7:1add29d51e72 1241 pLimitCheckString);
Charles MacNeill 7:1add29d51e72 1242
Charles MacNeill 7:1add29d51e72 1243 LOG_FUNCTION_END(Status);
Charles MacNeill 7:1add29d51e72 1244 return Status;
Charles MacNeill 7:1add29d51e72 1245 }
Charles MacNeill 7:1add29d51e72 1246
Charles MacNeill 7:1add29d51e72 1247 VL53L1_Error VL53L1_GetLimitCheckStatus(VL53L1_DEV Dev, uint16_t LimitCheckId,
Charles MacNeill 7:1add29d51e72 1248 uint8_t *pLimitCheckStatus)
Charles MacNeill 7:1add29d51e72 1249 {
Charles MacNeill 7:1add29d51e72 1250 VL53L1_Error Status = VL53L1_ERROR_NONE;
Charles MacNeill 7:1add29d51e72 1251 uint8_t Temp8;
Charles MacNeill 7:1add29d51e72 1252
Charles MacNeill 7:1add29d51e72 1253 LOG_FUNCTION_START("");
Charles MacNeill 7:1add29d51e72 1254
Charles MacNeill 7:1add29d51e72 1255 if (LimitCheckId >= VL53L1_CHECKENABLE_NUMBER_OF_CHECKS) {
Charles MacNeill 7:1add29d51e72 1256 Status = VL53L1_ERROR_INVALID_PARAMS;
Charles MacNeill 7:1add29d51e72 1257 } else {
Charles MacNeill 7:1add29d51e72 1258 VL53L1_GETARRAYPARAMETERFIELD(Dev, LimitChecksStatus,
Charles MacNeill 7:1add29d51e72 1259 LimitCheckId, Temp8);
Charles MacNeill 7:1add29d51e72 1260 *pLimitCheckStatus = Temp8;
Charles MacNeill 7:1add29d51e72 1261 }
Charles MacNeill 7:1add29d51e72 1262
Charles MacNeill 7:1add29d51e72 1263 LOG_FUNCTION_END(Status);
Charles MacNeill 7:1add29d51e72 1264 return Status;
Charles MacNeill 7:1add29d51e72 1265 }
Charles MacNeill 7:1add29d51e72 1266
Charles MacNeill 7:1add29d51e72 1267 static VL53L1_Error SetLimitValue(VL53L1_DEV Dev, uint16_t LimitCheckId,
Charles MacNeill 7:1add29d51e72 1268 FixPoint1616_t value)
Charles MacNeill 7:1add29d51e72 1269 {
Charles MacNeill 7:1add29d51e72 1270 VL53L1_Error Status = VL53L1_ERROR_NONE;
Charles MacNeill 7:1add29d51e72 1271 uint16_t tmpuint16;
Charles MacNeill 7:1add29d51e72 1272
Charles MacNeill 7:1add29d51e72 1273 LOG_FUNCTION_START("");
Charles MacNeill 7:1add29d51e72 1274
Charles MacNeill 7:1add29d51e72 1275 switch (LimitCheckId) {
Charles MacNeill 7:1add29d51e72 1276 case VL53L1_CHECKENABLE_SIGMA_FINAL_RANGE:
Charles MacNeill 7:1add29d51e72 1277 tmpuint16 = VL53L1_FIXPOINT1616TOFIXPOINT142(value);
Charles MacNeill 7:1add29d51e72 1278 VL53L1_set_lite_sigma_threshold(Dev, tmpuint16);
Charles MacNeill 7:1add29d51e72 1279 break;
Charles MacNeill 7:1add29d51e72 1280 case VL53L1_CHECKENABLE_SIGNAL_RATE_FINAL_RANGE:
Charles MacNeill 7:1add29d51e72 1281 tmpuint16 = VL53L1_FIXPOINT1616TOFIXPOINT97(value);
Charles MacNeill 7:1add29d51e72 1282 VL53L1_set_lite_min_count_rate(Dev, tmpuint16);
Charles MacNeill 7:1add29d51e72 1283 break;
Charles MacNeill 7:1add29d51e72 1284 default:
Charles MacNeill 7:1add29d51e72 1285 Status = VL53L1_ERROR_INVALID_PARAMS;
Charles MacNeill 7:1add29d51e72 1286 }
Charles MacNeill 7:1add29d51e72 1287
Charles MacNeill 7:1add29d51e72 1288 LOG_FUNCTION_END(Status);
Charles MacNeill 7:1add29d51e72 1289 return Status;
Charles MacNeill 7:1add29d51e72 1290 }
Charles MacNeill 7:1add29d51e72 1291
Charles MacNeill 7:1add29d51e72 1292
Charles MacNeill 7:1add29d51e72 1293 VL53L1_Error VL53L1_SetLimitCheckEnable(VL53L1_DEV Dev, uint16_t LimitCheckId,
Charles MacNeill 7:1add29d51e72 1294 uint8_t LimitCheckEnable)
Charles MacNeill 7:1add29d51e72 1295 {
Charles MacNeill 7:1add29d51e72 1296 VL53L1_Error Status = VL53L1_ERROR_NONE;
Charles MacNeill 7:1add29d51e72 1297 FixPoint1616_t TempFix1616 = 0;
Charles MacNeill 7:1add29d51e72 1298
Charles MacNeill 7:1add29d51e72 1299 LOG_FUNCTION_START("");
Charles MacNeill 7:1add29d51e72 1300
Charles MacNeill 7:1add29d51e72 1301
Charles MacNeill 7:1add29d51e72 1302 if (LimitCheckId >= VL53L1_CHECKENABLE_NUMBER_OF_CHECKS) {
Charles MacNeill 7:1add29d51e72 1303 Status = VL53L1_ERROR_INVALID_PARAMS;
Charles MacNeill 7:1add29d51e72 1304 } else {
Charles MacNeill 7:1add29d51e72 1305
Charles MacNeill 7:1add29d51e72 1306 if (LimitCheckEnable == 0)
Charles MacNeill 7:1add29d51e72 1307 TempFix1616 = 0;
Charles MacNeill 7:1add29d51e72 1308 else
Charles MacNeill 7:1add29d51e72 1309 VL53L1_GETARRAYPARAMETERFIELD(Dev, LimitChecksValue,
Charles MacNeill 7:1add29d51e72 1310 LimitCheckId, TempFix1616);
Charles MacNeill 7:1add29d51e72 1311
Charles MacNeill 7:1add29d51e72 1312 Status = SetLimitValue(Dev, LimitCheckId, TempFix1616);
Charles MacNeill 7:1add29d51e72 1313 }
Charles MacNeill 7:1add29d51e72 1314
Charles MacNeill 7:1add29d51e72 1315 if (Status == VL53L1_ERROR_NONE)
Charles MacNeill 7:1add29d51e72 1316 VL53L1_SETARRAYPARAMETERFIELD(Dev,
Charles MacNeill 7:1add29d51e72 1317 LimitChecksEnable,
Charles MacNeill 7:1add29d51e72 1318 LimitCheckId,
Charles MacNeill 7:1add29d51e72 1319 ((LimitCheckEnable == 0) ? 0 : 1));
Charles MacNeill 7:1add29d51e72 1320
Charles MacNeill 7:1add29d51e72 1321
Charles MacNeill 7:1add29d51e72 1322
Charles MacNeill 7:1add29d51e72 1323 LOG_FUNCTION_END(Status);
Charles MacNeill 7:1add29d51e72 1324 return Status;
Charles MacNeill 7:1add29d51e72 1325 }
Charles MacNeill 7:1add29d51e72 1326
Charles MacNeill 7:1add29d51e72 1327 VL53L1_Error VL53L1_GetLimitCheckEnable(VL53L1_DEV Dev, uint16_t LimitCheckId,
Charles MacNeill 7:1add29d51e72 1328 uint8_t *pLimitCheckEnable)
Charles MacNeill 7:1add29d51e72 1329 {
Charles MacNeill 7:1add29d51e72 1330 VL53L1_Error Status = VL53L1_ERROR_NONE;
Charles MacNeill 7:1add29d51e72 1331 uint8_t Temp8;
Charles MacNeill 7:1add29d51e72 1332
Charles MacNeill 7:1add29d51e72 1333 LOG_FUNCTION_START("");
Charles MacNeill 7:1add29d51e72 1334
Charles MacNeill 7:1add29d51e72 1335 if (LimitCheckId >= VL53L1_CHECKENABLE_NUMBER_OF_CHECKS) {
Charles MacNeill 7:1add29d51e72 1336 Status = VL53L1_ERROR_INVALID_PARAMS;
Charles MacNeill 7:1add29d51e72 1337 *pLimitCheckEnable = 0;
Charles MacNeill 7:1add29d51e72 1338 } else {
Charles MacNeill 7:1add29d51e72 1339 VL53L1_GETARRAYPARAMETERFIELD(Dev, LimitChecksEnable,
Charles MacNeill 7:1add29d51e72 1340 LimitCheckId, Temp8);
Charles MacNeill 7:1add29d51e72 1341 *pLimitCheckEnable = Temp8;
Charles MacNeill 7:1add29d51e72 1342 }
Charles MacNeill 7:1add29d51e72 1343
Charles MacNeill 7:1add29d51e72 1344
Charles MacNeill 7:1add29d51e72 1345 LOG_FUNCTION_END(Status);
Charles MacNeill 7:1add29d51e72 1346 return Status;
Charles MacNeill 7:1add29d51e72 1347 }
Charles MacNeill 7:1add29d51e72 1348
Charles MacNeill 7:1add29d51e72 1349 VL53L1_Error VL53L1_SetLimitCheckValue(VL53L1_DEV Dev, uint16_t LimitCheckId,
Charles MacNeill 7:1add29d51e72 1350 FixPoint1616_t LimitCheckValue)
Charles MacNeill 7:1add29d51e72 1351 {
Charles MacNeill 7:1add29d51e72 1352 VL53L1_Error Status = VL53L1_ERROR_NONE;
Charles MacNeill 7:1add29d51e72 1353 uint8_t LimitChecksEnable;
Charles MacNeill 7:1add29d51e72 1354
Charles MacNeill 7:1add29d51e72 1355 LOG_FUNCTION_START("");
Charles MacNeill 7:1add29d51e72 1356
Charles MacNeill 7:1add29d51e72 1357 if (LimitCheckId >= VL53L1_CHECKENABLE_NUMBER_OF_CHECKS) {
Charles MacNeill 7:1add29d51e72 1358 Status = VL53L1_ERROR_INVALID_PARAMS;
Charles MacNeill 7:1add29d51e72 1359 } else {
Charles MacNeill 7:1add29d51e72 1360
Charles MacNeill 7:1add29d51e72 1361 VL53L1_GETARRAYPARAMETERFIELD(Dev, LimitChecksEnable,
Charles MacNeill 7:1add29d51e72 1362 LimitCheckId,
Charles MacNeill 7:1add29d51e72 1363 LimitChecksEnable);
Charles MacNeill 7:1add29d51e72 1364
Charles MacNeill 7:1add29d51e72 1365 if (LimitChecksEnable == 0) {
Charles MacNeill 7:1add29d51e72 1366
Charles MacNeill 7:1add29d51e72 1367 VL53L1_SETARRAYPARAMETERFIELD(Dev, LimitChecksValue,
Charles MacNeill 7:1add29d51e72 1368 LimitCheckId, LimitCheckValue);
Charles MacNeill 7:1add29d51e72 1369 } else {
Charles MacNeill 7:1add29d51e72 1370
Charles MacNeill 7:1add29d51e72 1371 Status = SetLimitValue(Dev, LimitCheckId,
Charles MacNeill 7:1add29d51e72 1372 LimitCheckValue);
Charles MacNeill 7:1add29d51e72 1373
Charles MacNeill 7:1add29d51e72 1374 if (Status == VL53L1_ERROR_NONE) {
Charles MacNeill 7:1add29d51e72 1375 VL53L1_SETARRAYPARAMETERFIELD(Dev,
Charles MacNeill 7:1add29d51e72 1376 LimitChecksValue,
Charles MacNeill 7:1add29d51e72 1377 LimitCheckId, LimitCheckValue);
Charles MacNeill 7:1add29d51e72 1378 }
Charles MacNeill 7:1add29d51e72 1379 }
Charles MacNeill 7:1add29d51e72 1380 }
Charles MacNeill 7:1add29d51e72 1381
Charles MacNeill 7:1add29d51e72 1382 LOG_FUNCTION_END(Status);
Charles MacNeill 7:1add29d51e72 1383 return Status;
Charles MacNeill 7:1add29d51e72 1384 }
Charles MacNeill 7:1add29d51e72 1385
Charles MacNeill 7:1add29d51e72 1386 VL53L1_Error VL53L1_GetLimitCheckValue(VL53L1_DEV Dev, uint16_t LimitCheckId,
Charles MacNeill 7:1add29d51e72 1387 FixPoint1616_t *pLimitCheckValue)
Charles MacNeill 7:1add29d51e72 1388 {
Charles MacNeill 7:1add29d51e72 1389 VL53L1_Error Status = VL53L1_ERROR_NONE;
Charles MacNeill 7:1add29d51e72 1390 uint16_t MinCountRate;
Charles MacNeill 7:1add29d51e72 1391 FixPoint1616_t TempFix1616;
Charles MacNeill 7:1add29d51e72 1392 uint16_t SigmaThresh;
Charles MacNeill 7:1add29d51e72 1393
Charles MacNeill 7:1add29d51e72 1394 LOG_FUNCTION_START("");
Charles MacNeill 7:1add29d51e72 1395
Charles MacNeill 7:1add29d51e72 1396 switch (LimitCheckId) {
Charles MacNeill 7:1add29d51e72 1397 case VL53L1_CHECKENABLE_SIGMA_FINAL_RANGE:
Charles MacNeill 7:1add29d51e72 1398 Status = VL53L1_get_lite_sigma_threshold(Dev, &SigmaThresh);
Charles MacNeill 7:1add29d51e72 1399 TempFix1616 = VL53L1_FIXPOINT142TOFIXPOINT1616(SigmaThresh);
Charles MacNeill 7:1add29d51e72 1400 break;
Charles MacNeill 7:1add29d51e72 1401 case VL53L1_CHECKENABLE_SIGNAL_RATE_FINAL_RANGE:
Charles MacNeill 7:1add29d51e72 1402 Status = VL53L1_get_lite_min_count_rate(Dev, &MinCountRate);
Charles MacNeill 7:1add29d51e72 1403 TempFix1616 = VL53L1_FIXPOINT97TOFIXPOINT1616(MinCountRate);
Charles MacNeill 7:1add29d51e72 1404 break;
Charles MacNeill 7:1add29d51e72 1405 default:
Charles MacNeill 7:1add29d51e72 1406 Status = VL53L1_ERROR_INVALID_PARAMS;
Charles MacNeill 7:1add29d51e72 1407 }
Charles MacNeill 7:1add29d51e72 1408
Charles MacNeill 7:1add29d51e72 1409 if (Status == VL53L1_ERROR_NONE) {
Charles MacNeill 7:1add29d51e72 1410
Charles MacNeill 7:1add29d51e72 1411 if (TempFix1616 == 0) {
Charles MacNeill 7:1add29d51e72 1412
Charles MacNeill 7:1add29d51e72 1413 VL53L1_GETARRAYPARAMETERFIELD(Dev,
Charles MacNeill 7:1add29d51e72 1414 LimitChecksValue, LimitCheckId,
Charles MacNeill 7:1add29d51e72 1415 TempFix1616);
Charles MacNeill 7:1add29d51e72 1416 *pLimitCheckValue = TempFix1616;
Charles MacNeill 7:1add29d51e72 1417 VL53L1_SETARRAYPARAMETERFIELD(Dev,
Charles MacNeill 7:1add29d51e72 1418 LimitChecksEnable, LimitCheckId, 0);
Charles MacNeill 7:1add29d51e72 1419 } else {
Charles MacNeill 7:1add29d51e72 1420 *pLimitCheckValue = TempFix1616;
Charles MacNeill 7:1add29d51e72 1421 VL53L1_SETARRAYPARAMETERFIELD(Dev,
Charles MacNeill 7:1add29d51e72 1422 LimitChecksValue, LimitCheckId,
Charles MacNeill 7:1add29d51e72 1423 TempFix1616);
Charles MacNeill 7:1add29d51e72 1424 VL53L1_SETARRAYPARAMETERFIELD(Dev,
Charles MacNeill 7:1add29d51e72 1425 LimitChecksEnable, LimitCheckId, 1);
Charles MacNeill 7:1add29d51e72 1426 }
Charles MacNeill 7:1add29d51e72 1427 }
Charles MacNeill 7:1add29d51e72 1428 LOG_FUNCTION_END(Status);
Charles MacNeill 7:1add29d51e72 1429 return Status;
Charles MacNeill 7:1add29d51e72 1430
Charles MacNeill 7:1add29d51e72 1431 }
Charles MacNeill 7:1add29d51e72 1432
Charles MacNeill 7:1add29d51e72 1433 VL53L1_Error VL53L1_GetLimitCheckCurrent(VL53L1_DEV Dev, uint16_t LimitCheckId,
Charles MacNeill 7:1add29d51e72 1434 FixPoint1616_t *pLimitCheckCurrent)
Charles MacNeill 7:1add29d51e72 1435 {
Charles MacNeill 7:1add29d51e72 1436 VL53L1_Error Status = VL53L1_ERROR_NONE;
Charles MacNeill 7:1add29d51e72 1437 FixPoint1616_t TempFix1616 = 0;
Charles MacNeill 7:1add29d51e72 1438
Charles MacNeill 7:1add29d51e72 1439 LOG_FUNCTION_START("");
Charles MacNeill 7:1add29d51e72 1440
Charles MacNeill 7:1add29d51e72 1441 if (LimitCheckId >= VL53L1_CHECKENABLE_NUMBER_OF_CHECKS) {
Charles MacNeill 7:1add29d51e72 1442 Status = VL53L1_ERROR_INVALID_PARAMS;
Charles MacNeill 7:1add29d51e72 1443 } else {
Charles MacNeill 7:1add29d51e72 1444 VL53L1_GETARRAYPARAMETERFIELD(Dev, LimitChecksCurrent,
Charles MacNeill 7:1add29d51e72 1445 LimitCheckId, TempFix1616);
Charles MacNeill 7:1add29d51e72 1446 *pLimitCheckCurrent = TempFix1616;
Charles MacNeill 7:1add29d51e72 1447 }
Charles MacNeill 7:1add29d51e72 1448
Charles MacNeill 7:1add29d51e72 1449 LOG_FUNCTION_END(Status);
Charles MacNeill 7:1add29d51e72 1450 return Status;
Charles MacNeill 7:1add29d51e72 1451
Charles MacNeill 7:1add29d51e72 1452 }
Charles MacNeill 7:1add29d51e72 1453
Charles MacNeill 7:1add29d51e72 1454
Charles MacNeill 7:1add29d51e72 1455
Charles MacNeill 7:1add29d51e72 1456
Charles MacNeill 7:1add29d51e72 1457
Charles MacNeill 7:1add29d51e72 1458
Charles MacNeill 7:1add29d51e72 1459
Charles MacNeill 7:1add29d51e72 1460
Charles MacNeill 7:1add29d51e72 1461 VL53L1_Error VL53L1_GetMaxNumberOfROI(VL53L1_DEV Dev,
Charles MacNeill 7:1add29d51e72 1462 uint8_t *pMaxNumberOfROI)
Charles MacNeill 7:1add29d51e72 1463 {
Charles MacNeill 7:1add29d51e72 1464 VL53L1_Error Status = VL53L1_ERROR_NONE;
Charles MacNeill 7:1add29d51e72 1465 VL53L1_PresetModes PresetMode;
Charles MacNeill 7:1add29d51e72 1466
Charles MacNeill 7:1add29d51e72 1467 LOG_FUNCTION_START("");
Charles MacNeill 7:1add29d51e72 1468
Charles MacNeill 7:1add29d51e72 1469 PresetMode = VL53L1DevDataGet(Dev, CurrentParameters.PresetMode);
Charles MacNeill 7:1add29d51e72 1470
Charles MacNeill 7:1add29d51e72 1471
Charles MacNeill 7:1add29d51e72 1472 if (PresetMode == VL53L1_PRESETMODE_MULTIZONES_SCANNING)
Charles MacNeill 7:1add29d51e72 1473 *pMaxNumberOfROI = VL53L1_MAX_USER_ZONES;
Charles MacNeill 7:1add29d51e72 1474 else
Charles MacNeill 7:1add29d51e72 1475 *pMaxNumberOfROI = 1;
Charles MacNeill 7:1add29d51e72 1476
Charles MacNeill 7:1add29d51e72 1477 LOG_FUNCTION_END(Status);
Charles MacNeill 7:1add29d51e72 1478 return Status;
Charles MacNeill 7:1add29d51e72 1479 }
Charles MacNeill 7:1add29d51e72 1480
Charles MacNeill 7:1add29d51e72 1481 VL53L1_Error VL53L1_SetROI(VL53L1_DEV Dev,
Charles MacNeill 7:1add29d51e72 1482 VL53L1_RoiConfig_t *pRoiConfig)
Charles MacNeill 7:1add29d51e72 1483 {
Charles MacNeill 7:1add29d51e72 1484 VL53L1_Error Status = VL53L1_ERROR_NONE;
Charles MacNeill 7:1add29d51e72 1485 VL53L1_PresetModes PresetMode;
Charles MacNeill 7:1add29d51e72 1486 uint8_t MaxNumberOfROI = 1;
Charles MacNeill 7:1add29d51e72 1487 VL53L1_zone_config_t zone_cfg;
Charles MacNeill 7:1add29d51e72 1488 VL53L1_UserRoi_t CurrROI;
Charles MacNeill 7:1add29d51e72 1489 uint8_t i;
Charles MacNeill 7:1add29d51e72 1490 uint8_t x_centre;
Charles MacNeill 7:1add29d51e72 1491 uint8_t y_centre;
Charles MacNeill 7:1add29d51e72 1492 uint8_t width, height;
Charles MacNeill 7:1add29d51e72 1493
Charles MacNeill 7:1add29d51e72 1494 LOG_FUNCTION_START("");
Charles MacNeill 7:1add29d51e72 1495
Charles MacNeill 7:1add29d51e72 1496
Charles MacNeill 7:1add29d51e72 1497 PresetMode = VL53L1DevDataGet(Dev, CurrentParameters.PresetMode);
Charles MacNeill 7:1add29d51e72 1498
Charles MacNeill 7:1add29d51e72 1499
Charles MacNeill 7:1add29d51e72 1500 if (PresetMode == VL53L1_PRESETMODE_MULTIZONES_SCANNING)
Charles MacNeill 7:1add29d51e72 1501 MaxNumberOfROI = VL53L1_MAX_USER_ZONES;
Charles MacNeill 7:1add29d51e72 1502
Charles MacNeill 7:1add29d51e72 1503 if ((pRoiConfig->NumberOfRoi > MaxNumberOfROI) ||
Charles MacNeill 7:1add29d51e72 1504 (pRoiConfig->NumberOfRoi < 1))
Charles MacNeill 7:1add29d51e72 1505 Status = VL53L1_ERROR_INVALID_PARAMS;
Charles MacNeill 7:1add29d51e72 1506
Charles MacNeill 7:1add29d51e72 1507 if (Status == VL53L1_ERROR_NONE) {
Charles MacNeill 7:1add29d51e72 1508
Charles MacNeill 7:1add29d51e72 1509
Charles MacNeill 7:1add29d51e72 1510 zone_cfg.max_zones = MaxNumberOfROI;
Charles MacNeill 7:1add29d51e72 1511 zone_cfg.active_zones = pRoiConfig->NumberOfRoi - 1;
Charles MacNeill 7:1add29d51e72 1512
Charles MacNeill 7:1add29d51e72 1513 for (i = 0; i < pRoiConfig->NumberOfRoi; i++) {
Charles MacNeill 7:1add29d51e72 1514 CurrROI = pRoiConfig->UserRois[i];
Charles MacNeill 7:1add29d51e72 1515
Charles MacNeill 7:1add29d51e72 1516 Status = CheckValidRectRoi(CurrROI);
Charles MacNeill 7:1add29d51e72 1517 if (Status != VL53L1_ERROR_NONE)
Charles MacNeill 7:1add29d51e72 1518 break;
Charles MacNeill 7:1add29d51e72 1519
Charles MacNeill 7:1add29d51e72 1520 x_centre = (CurrROI.BotRightX + CurrROI.TopLeftX + 1)
Charles MacNeill 7:1add29d51e72 1521 / 2;
Charles MacNeill 7:1add29d51e72 1522 y_centre = (CurrROI.TopLeftY + CurrROI.BotRightY + 1)
Charles MacNeill 7:1add29d51e72 1523 / 2;
Charles MacNeill 7:1add29d51e72 1524 width = (CurrROI.BotRightX - CurrROI.TopLeftX);
Charles MacNeill 7:1add29d51e72 1525 height = (CurrROI.TopLeftY - CurrROI.BotRightY);
Charles MacNeill 7:1add29d51e72 1526 if ((width < 3) || (height < 3)) {
Charles MacNeill 7:1add29d51e72 1527 Status = VL53L1_ERROR_INVALID_PARAMS;
Charles MacNeill 7:1add29d51e72 1528 break;
Charles MacNeill 7:1add29d51e72 1529 }
Charles MacNeill 7:1add29d51e72 1530 zone_cfg.user_zones[i].x_centre = x_centre;
Charles MacNeill 7:1add29d51e72 1531 zone_cfg.user_zones[i].y_centre = y_centre;
Charles MacNeill 7:1add29d51e72 1532 zone_cfg.user_zones[i].width = width;
Charles MacNeill 7:1add29d51e72 1533 zone_cfg.user_zones[i].height = height;
Charles MacNeill 7:1add29d51e72 1534 }
Charles MacNeill 7:1add29d51e72 1535 }
Charles MacNeill 7:1add29d51e72 1536
Charles MacNeill 7:1add29d51e72 1537 if (Status == VL53L1_ERROR_NONE)
Charles MacNeill 7:1add29d51e72 1538 Status = VL53L1_set_zone_config(Dev, &zone_cfg);
Charles MacNeill 7:1add29d51e72 1539
Charles MacNeill 7:1add29d51e72 1540 LOG_FUNCTION_END(Status);
Charles MacNeill 7:1add29d51e72 1541 return Status;
Charles MacNeill 7:1add29d51e72 1542 }
Charles MacNeill 7:1add29d51e72 1543
Charles MacNeill 7:1add29d51e72 1544 VL53L1_Error VL53L1_GetROI(VL53L1_DEV Dev,
Charles MacNeill 7:1add29d51e72 1545 VL53L1_RoiConfig_t *pRoiConfig)
Charles MacNeill 7:1add29d51e72 1546 {
Charles MacNeill 7:1add29d51e72 1547 VL53L1_Error Status = VL53L1_ERROR_NONE;
Charles MacNeill 7:1add29d51e72 1548 VL53L1_zone_config_t zone_cfg;
Charles MacNeill 7:1add29d51e72 1549 uint8_t i;
Charles MacNeill 7:1add29d51e72 1550 uint8_t TopLeftX;
Charles MacNeill 7:1add29d51e72 1551 uint8_t TopLeftY;
Charles MacNeill 7:1add29d51e72 1552 uint8_t BotRightX;
Charles MacNeill 7:1add29d51e72 1553 uint8_t BotRightY;
Charles MacNeill 7:1add29d51e72 1554
Charles MacNeill 7:1add29d51e72 1555 LOG_FUNCTION_START("");
Charles MacNeill 7:1add29d51e72 1556
Charles MacNeill 7:1add29d51e72 1557 VL53L1_get_zone_config(Dev, &zone_cfg);
Charles MacNeill 7:1add29d51e72 1558
Charles MacNeill 7:1add29d51e72 1559 pRoiConfig->NumberOfRoi = zone_cfg.active_zones + 1;
Charles MacNeill 7:1add29d51e72 1560
Charles MacNeill 7:1add29d51e72 1561 for (i = 0; i < pRoiConfig->NumberOfRoi; i++) {
Charles MacNeill 7:1add29d51e72 1562 TopLeftX = (2 * zone_cfg.user_zones[i].x_centre -
Charles MacNeill 7:1add29d51e72 1563 zone_cfg.user_zones[i].width) >> 1;
Charles MacNeill 7:1add29d51e72 1564 TopLeftY = (2 * zone_cfg.user_zones[i].y_centre +
Charles MacNeill 7:1add29d51e72 1565 zone_cfg.user_zones[i].height) >> 1;
Charles MacNeill 7:1add29d51e72 1566 BotRightX = (2 * zone_cfg.user_zones[i].x_centre +
Charles MacNeill 7:1add29d51e72 1567 zone_cfg.user_zones[i].width) >> 1;
Charles MacNeill 7:1add29d51e72 1568 BotRightY = (2 * zone_cfg.user_zones[i].y_centre -
Charles MacNeill 7:1add29d51e72 1569 zone_cfg.user_zones[i].height) >> 1;
Charles MacNeill 7:1add29d51e72 1570 pRoiConfig->UserRois[i].TopLeftX = TopLeftX;
Charles MacNeill 7:1add29d51e72 1571 pRoiConfig->UserRois[i].TopLeftY = TopLeftY;
Charles MacNeill 7:1add29d51e72 1572 pRoiConfig->UserRois[i].BotRightX = BotRightX;
Charles MacNeill 7:1add29d51e72 1573 pRoiConfig->UserRois[i].BotRightY = BotRightY;
Charles MacNeill 7:1add29d51e72 1574 }
Charles MacNeill 7:1add29d51e72 1575
Charles MacNeill 7:1add29d51e72 1576 LOG_FUNCTION_END(Status);
Charles MacNeill 7:1add29d51e72 1577 return Status;
Charles MacNeill 7:1add29d51e72 1578 }
Charles MacNeill 7:1add29d51e72 1579
Charles MacNeill 7:1add29d51e72 1580
Charles MacNeill 7:1add29d51e72 1581
Charles MacNeill 7:1add29d51e72 1582
Charles MacNeill 7:1add29d51e72 1583
Charles MacNeill 7:1add29d51e72 1584
Charles MacNeill 7:1add29d51e72 1585
Charles MacNeill 7:1add29d51e72 1586 VL53L1_Error VL53L1_GetNumberOfSequenceSteps(VL53L1_DEV Dev,
Charles MacNeill 7:1add29d51e72 1587 uint8_t *pNumberOfSequenceSteps)
Charles MacNeill 7:1add29d51e72 1588 {
Charles MacNeill 7:1add29d51e72 1589 VL53L1_Error Status = VL53L1_ERROR_NONE;
Charles MacNeill 7:1add29d51e72 1590
Charles MacNeill 7:1add29d51e72 1591 SUPPRESS_UNUSED_WARNING(Dev);
Charles MacNeill 7:1add29d51e72 1592
Charles MacNeill 7:1add29d51e72 1593 LOG_FUNCTION_START("");
Charles MacNeill 7:1add29d51e72 1594
Charles MacNeill 7:1add29d51e72 1595 *pNumberOfSequenceSteps = VL53L1_SEQUENCESTEP_NUMBER_OF_ITEMS;
Charles MacNeill 7:1add29d51e72 1596
Charles MacNeill 7:1add29d51e72 1597 LOG_FUNCTION_END(Status);
Charles MacNeill 7:1add29d51e72 1598 return Status;
Charles MacNeill 7:1add29d51e72 1599 }
Charles MacNeill 7:1add29d51e72 1600
Charles MacNeill 7:1add29d51e72 1601 VL53L1_Error VL53L1_GetSequenceStepsInfo(VL53L1_SequenceStepId SequenceStepId,
Charles MacNeill 7:1add29d51e72 1602 char *pSequenceStepsString)
Charles MacNeill 7:1add29d51e72 1603 {
Charles MacNeill 7:1add29d51e72 1604 VL53L1_Error Status = VL53L1_ERROR_NONE;
Charles MacNeill 7:1add29d51e72 1605
Charles MacNeill 7:1add29d51e72 1606 LOG_FUNCTION_START("");
Charles MacNeill 7:1add29d51e72 1607
Charles MacNeill 7:1add29d51e72 1608 Status = VL53L1_get_sequence_steps_info(
Charles MacNeill 7:1add29d51e72 1609 SequenceStepId,
Charles MacNeill 7:1add29d51e72 1610 pSequenceStepsString);
Charles MacNeill 7:1add29d51e72 1611
Charles MacNeill 7:1add29d51e72 1612 LOG_FUNCTION_END(Status);
Charles MacNeill 7:1add29d51e72 1613
Charles MacNeill 7:1add29d51e72 1614 return Status;
Charles MacNeill 7:1add29d51e72 1615 }
Charles MacNeill 7:1add29d51e72 1616
Charles MacNeill 7:1add29d51e72 1617 VL53L1_Error VL53L1_SetSequenceStepEnable(VL53L1_DEV Dev,
Charles MacNeill 7:1add29d51e72 1618 VL53L1_SequenceStepId SequenceStepId, uint8_t SequenceStepEnabled)
Charles MacNeill 7:1add29d51e72 1619 {
Charles MacNeill 7:1add29d51e72 1620 VL53L1_Error Status = VL53L1_ERROR_NONE;
Charles MacNeill 7:1add29d51e72 1621 uint32_t MeasurementTimingBudgetMicroSeconds;
Charles MacNeill 7:1add29d51e72 1622
Charles MacNeill 7:1add29d51e72 1623 LOG_FUNCTION_START("");
Charles MacNeill 7:1add29d51e72 1624
Charles MacNeill 7:1add29d51e72 1625
Charles MacNeill 7:1add29d51e72 1626
Charles MacNeill 7:1add29d51e72 1627 Status = VL53L1_set_sequence_config_bit(Dev,
Charles MacNeill 7:1add29d51e72 1628 (VL53L1_DeviceSequenceConfig)SequenceStepId,
Charles MacNeill 7:1add29d51e72 1629 SequenceStepEnabled);
Charles MacNeill 7:1add29d51e72 1630
Charles MacNeill 7:1add29d51e72 1631
Charles MacNeill 7:1add29d51e72 1632 if (Status == VL53L1_ERROR_NONE) {
Charles MacNeill 7:1add29d51e72 1633
Charles MacNeill 7:1add29d51e72 1634
Charles MacNeill 7:1add29d51e72 1635 MeasurementTimingBudgetMicroSeconds = VL53L1DevDataGet(Dev,
Charles MacNeill 7:1add29d51e72 1636 CurrentParameters.MeasurementTimingBudgetMicroSeconds);
Charles MacNeill 7:1add29d51e72 1637
Charles MacNeill 7:1add29d51e72 1638 VL53L1_SetMeasurementTimingBudgetMicroSeconds(Dev,
Charles MacNeill 7:1add29d51e72 1639 MeasurementTimingBudgetMicroSeconds);
Charles MacNeill 7:1add29d51e72 1640 }
Charles MacNeill 7:1add29d51e72 1641
Charles MacNeill 7:1add29d51e72 1642 LOG_FUNCTION_END(Status);
Charles MacNeill 7:1add29d51e72 1643
Charles MacNeill 7:1add29d51e72 1644 return Status;
Charles MacNeill 7:1add29d51e72 1645 }
Charles MacNeill 7:1add29d51e72 1646
Charles MacNeill 7:1add29d51e72 1647
Charles MacNeill 7:1add29d51e72 1648 VL53L1_Error VL53L1_GetSequenceStepEnable(VL53L1_DEV Dev,
Charles MacNeill 7:1add29d51e72 1649 VL53L1_SequenceStepId SequenceStepId, uint8_t *pSequenceStepEnabled)
Charles MacNeill 7:1add29d51e72 1650 {
Charles MacNeill 7:1add29d51e72 1651 VL53L1_Error Status = VL53L1_ERROR_NONE;
Charles MacNeill 7:1add29d51e72 1652
Charles MacNeill 7:1add29d51e72 1653 LOG_FUNCTION_START("");
Charles MacNeill 7:1add29d51e72 1654
Charles MacNeill 7:1add29d51e72 1655 Status = VL53L1_get_sequence_config_bit(Dev,
Charles MacNeill 7:1add29d51e72 1656 (VL53L1_DeviceSequenceConfig)SequenceStepId,
Charles MacNeill 7:1add29d51e72 1657 pSequenceStepEnabled);
Charles MacNeill 7:1add29d51e72 1658
Charles MacNeill 7:1add29d51e72 1659 LOG_FUNCTION_END(Status);
Charles MacNeill 7:1add29d51e72 1660 return Status;
Charles MacNeill 7:1add29d51e72 1661 }
Charles MacNeill 7:1add29d51e72 1662
Charles MacNeill 7:1add29d51e72 1663
Charles MacNeill 7:1add29d51e72 1664
Charles MacNeill 7:1add29d51e72 1665
Charles MacNeill 7:1add29d51e72 1666
Charles MacNeill 7:1add29d51e72 1667
Charles MacNeill 7:1add29d51e72 1668
Charles MacNeill 7:1add29d51e72 1669
Charles MacNeill 7:1add29d51e72 1670
Charles MacNeill 7:1add29d51e72 1671
Charles MacNeill 7:1add29d51e72 1672 VL53L1_Error VL53L1_StartMeasurement(VL53L1_DEV Dev)
Charles MacNeill 7:1add29d51e72 1673 {
Charles MacNeill 7:1add29d51e72 1674 #define TIMED_MODE_TIMING_GUARD_MILLISECONDS 4
Charles MacNeill 7:1add29d51e72 1675 VL53L1_Error Status = VL53L1_ERROR_NONE;
Charles MacNeill 7:1add29d51e72 1676 uint8_t DeviceMeasurementMode;
Charles MacNeill 7:1add29d51e72 1677 VL53L1_State CurrPalState;
Charles MacNeill 7:1add29d51e72 1678 VL53L1_Error lStatus;
Charles MacNeill 7:1add29d51e72 1679 uint32_t MTBus, IMPms;
Charles MacNeill 7:1add29d51e72 1680 uint8_t i;
Charles MacNeill 7:1add29d51e72 1681 VL53L1_LLDriverData_t *pdev = VL53L1DevStructGetLLDriverHandle(Dev);
Charles MacNeill 7:1add29d51e72 1682
Charles MacNeill 7:1add29d51e72 1683
Charles MacNeill 7:1add29d51e72 1684 LOG_FUNCTION_START("");
Charles MacNeill 7:1add29d51e72 1685 VL53L1_load_patch(Dev);
Charles MacNeill 7:1add29d51e72 1686 for (i = 0; i < VL53L1_MAX_RANGE_RESULTS; i++) {
Charles MacNeill 7:1add29d51e72 1687 pdev->PreviousRangeMilliMeter[i] = 0;
Charles MacNeill 7:1add29d51e72 1688 pdev->PreviousRangeStatus[i] = 255;
Charles MacNeill 7:1add29d51e72 1689 pdev->PreviousExtendedRange[i] = 0;
Charles MacNeill 7:1add29d51e72 1690 }
Charles MacNeill 7:1add29d51e72 1691 pdev->PreviousStreamCount = 0;
Charles MacNeill 7:1add29d51e72 1692 CurrPalState = VL53L1DevDataGet(Dev, PalState);
Charles MacNeill 7:1add29d51e72 1693 switch (CurrPalState) {
Charles MacNeill 7:1add29d51e72 1694 case VL53L1_STATE_IDLE:
Charles MacNeill 7:1add29d51e72 1695 Status = VL53L1_ERROR_NONE;
Charles MacNeill 7:1add29d51e72 1696 break;
Charles MacNeill 7:1add29d51e72 1697 case VL53L1_STATE_POWERDOWN:
Charles MacNeill 7:1add29d51e72 1698 case VL53L1_STATE_WAIT_STATICINIT:
Charles MacNeill 7:1add29d51e72 1699 case VL53L1_STATE_STANDBY:
Charles MacNeill 7:1add29d51e72 1700 case VL53L1_STATE_RUNNING:
Charles MacNeill 7:1add29d51e72 1701 case VL53L1_STATE_RESET:
Charles MacNeill 7:1add29d51e72 1702 case VL53L1_STATE_UNKNOWN:
Charles MacNeill 7:1add29d51e72 1703 case VL53L1_STATE_ERROR:
Charles MacNeill 7:1add29d51e72 1704 Status = VL53L1_ERROR_INVALID_COMMAND;
Charles MacNeill 7:1add29d51e72 1705 break;
Charles MacNeill 7:1add29d51e72 1706 default:
Charles MacNeill 7:1add29d51e72 1707 Status = VL53L1_ERROR_UNDEFINED;
Charles MacNeill 7:1add29d51e72 1708 }
Charles MacNeill 7:1add29d51e72 1709
Charles MacNeill 7:1add29d51e72 1710 DeviceMeasurementMode = VL53L1DevDataGet(Dev, LLData.measurement_mode);
Charles MacNeill 7:1add29d51e72 1711
Charles MacNeill 7:1add29d51e72 1712
Charles MacNeill 7:1add29d51e72 1713 if ((Status == VL53L1_ERROR_NONE) &&
Charles MacNeill 7:1add29d51e72 1714 (DeviceMeasurementMode == VL53L1_DEVICEMEASUREMENTMODE_TIMED)) {
Charles MacNeill 7:1add29d51e72 1715 lStatus = VL53L1_GetMeasurementTimingBudgetMicroSeconds(Dev,
Charles MacNeill 7:1add29d51e72 1716 &MTBus);
Charles MacNeill 7:1add29d51e72 1717
Charles MacNeill 7:1add29d51e72 1718 MTBus /= 1000;
Charles MacNeill 7:1add29d51e72 1719 lStatus = VL53L1_GetInterMeasurementPeriodMilliSeconds(Dev,
Charles MacNeill 7:1add29d51e72 1720 &IMPms);
Charles MacNeill 7:1add29d51e72 1721
Charles MacNeill 7:1add29d51e72 1722 SUPPRESS_UNUSED_WARNING(lStatus);
Charles MacNeill 7:1add29d51e72 1723 if (IMPms < MTBus + TIMED_MODE_TIMING_GUARD_MILLISECONDS)
Charles MacNeill 7:1add29d51e72 1724 Status = VL53L1_ERROR_INVALID_PARAMS;
Charles MacNeill 7:1add29d51e72 1725 }
Charles MacNeill 7:1add29d51e72 1726
Charles MacNeill 7:1add29d51e72 1727 if (Status == VL53L1_ERROR_NONE)
Charles MacNeill 7:1add29d51e72 1728 Status = VL53L1_init_and_start_range(
Charles MacNeill 7:1add29d51e72 1729 Dev,
Charles MacNeill 7:1add29d51e72 1730 DeviceMeasurementMode,
Charles MacNeill 7:1add29d51e72 1731 VL53L1_DEVICECONFIGLEVEL_FULL);
Charles MacNeill 7:1add29d51e72 1732
Charles MacNeill 7:1add29d51e72 1733
Charles MacNeill 7:1add29d51e72 1734 if (Status == VL53L1_ERROR_NONE)
Charles MacNeill 7:1add29d51e72 1735 VL53L1DevDataSet(Dev, PalState, VL53L1_STATE_RUNNING);
Charles MacNeill 7:1add29d51e72 1736
Charles MacNeill 7:1add29d51e72 1737
Charles MacNeill 7:1add29d51e72 1738 LOG_FUNCTION_END(Status);
Charles MacNeill 7:1add29d51e72 1739 return Status;
Charles MacNeill 7:1add29d51e72 1740 }
Charles MacNeill 7:1add29d51e72 1741
Charles MacNeill 7:1add29d51e72 1742 VL53L1_Error VL53L1_StopMeasurement(VL53L1_DEV Dev)
Charles MacNeill 7:1add29d51e72 1743 {
Charles MacNeill 7:1add29d51e72 1744 VL53L1_Error Status = VL53L1_ERROR_NONE;
Charles MacNeill 7:1add29d51e72 1745
Charles MacNeill 7:1add29d51e72 1746 LOG_FUNCTION_START("");
Charles MacNeill 7:1add29d51e72 1747
Charles MacNeill 7:1add29d51e72 1748 Status = VL53L1_stop_range(Dev);
Charles MacNeill 7:1add29d51e72 1749 VL53L1_unload_patch(Dev);
Charles MacNeill 7:1add29d51e72 1750
Charles MacNeill 7:1add29d51e72 1751 if (Status == VL53L1_ERROR_NONE)
Charles MacNeill 7:1add29d51e72 1752 VL53L1DevDataSet(Dev, PalState, VL53L1_STATE_IDLE);
Charles MacNeill 7:1add29d51e72 1753
Charles MacNeill 7:1add29d51e72 1754 LOG_FUNCTION_END(Status);
Charles MacNeill 7:1add29d51e72 1755 return Status;
Charles MacNeill 7:1add29d51e72 1756 }
Charles MacNeill 7:1add29d51e72 1757
Charles MacNeill 7:1add29d51e72 1758
Charles MacNeill 7:1add29d51e72 1759 VL53L1_Error VL53L1_ClearInterruptAndStartMeasurement(VL53L1_DEV Dev)
Charles MacNeill 7:1add29d51e72 1760 {
Charles MacNeill 7:1add29d51e72 1761 VL53L1_Error Status = VL53L1_ERROR_NONE;
Charles MacNeill 7:1add29d51e72 1762 uint8_t DeviceMeasurementMode;
Charles MacNeill 7:1add29d51e72 1763
Charles MacNeill 7:1add29d51e72 1764 LOG_FUNCTION_START("");
Charles MacNeill 7:1add29d51e72 1765
Charles MacNeill 7:1add29d51e72 1766 DeviceMeasurementMode = VL53L1DevDataGet(Dev, LLData.measurement_mode);
Charles MacNeill 7:1add29d51e72 1767
Charles MacNeill 7:1add29d51e72 1768 Status = VL53L1_clear_interrupt_and_enable_next_range(Dev,
Charles MacNeill 7:1add29d51e72 1769 DeviceMeasurementMode);
Charles MacNeill 7:1add29d51e72 1770
Charles MacNeill 7:1add29d51e72 1771 LOG_FUNCTION_END(Status);
Charles MacNeill 7:1add29d51e72 1772 return Status;
Charles MacNeill 7:1add29d51e72 1773 }
Charles MacNeill 7:1add29d51e72 1774
Charles MacNeill 7:1add29d51e72 1775
Charles MacNeill 7:1add29d51e72 1776 VL53L1_Error VL53L1_GetMeasurementDataReady(VL53L1_DEV Dev,
Charles MacNeill 7:1add29d51e72 1777 uint8_t *pMeasurementDataReady)
Charles MacNeill 7:1add29d51e72 1778 {
Charles MacNeill 7:1add29d51e72 1779 VL53L1_Error Status = VL53L1_ERROR_NONE;
Charles MacNeill 7:1add29d51e72 1780
Charles MacNeill 7:1add29d51e72 1781 LOG_FUNCTION_START("");
Charles MacNeill 7:1add29d51e72 1782
Charles MacNeill 7:1add29d51e72 1783 Status = VL53L1_is_new_data_ready(Dev, pMeasurementDataReady);
Charles MacNeill 7:1add29d51e72 1784
Charles MacNeill 7:1add29d51e72 1785 LOG_FUNCTION_END(Status);
Charles MacNeill 7:1add29d51e72 1786 return Status;
Charles MacNeill 7:1add29d51e72 1787 }
Charles MacNeill 7:1add29d51e72 1788
Charles MacNeill 7:1add29d51e72 1789 VL53L1_Error VL53L1_WaitMeasurementDataReady(VL53L1_DEV Dev)
Charles MacNeill 7:1add29d51e72 1790 {
Charles MacNeill 7:1add29d51e72 1791 VL53L1_Error Status = VL53L1_ERROR_NONE;
Charles MacNeill 7:1add29d51e72 1792
Charles MacNeill 7:1add29d51e72 1793 LOG_FUNCTION_START("");
Charles MacNeill 7:1add29d51e72 1794
Charles MacNeill 7:1add29d51e72 1795
Charles MacNeill 7:1add29d51e72 1796
Charles MacNeill 7:1add29d51e72 1797 Status = VL53L1_poll_for_range_completion(Dev,
Charles MacNeill 7:1add29d51e72 1798 VL53L1_RANGE_COMPLETION_POLLING_TIMEOUT_MS);
Charles MacNeill 7:1add29d51e72 1799
Charles MacNeill 7:1add29d51e72 1800 LOG_FUNCTION_END(Status);
Charles MacNeill 7:1add29d51e72 1801 return Status;
Charles MacNeill 7:1add29d51e72 1802 }
Charles MacNeill 7:1add29d51e72 1803
Charles MacNeill 7:1add29d51e72 1804 static void GenNewPresetMode(int16_t RefRange,
Charles MacNeill 7:1add29d51e72 1805 VL53L1_DistanceModes InternalDistanceMode,
Charles MacNeill 7:1add29d51e72 1806 VL53L1_DistanceModes *pNewDistanceMode)
Charles MacNeill 7:1add29d51e72 1807 {
Charles MacNeill 7:1add29d51e72 1808 uint16_t HRLI = 600;
Charles MacNeill 7:1add29d51e72 1809 uint16_t HRLH = 700;
Charles MacNeill 7:1add29d51e72 1810 uint16_t MRLI = 1400;
Charles MacNeill 7:1add29d51e72 1811 uint16_t MRLH = 1500;
Charles MacNeill 7:1add29d51e72 1812
Charles MacNeill 7:1add29d51e72 1813 switch (InternalDistanceMode) {
Charles MacNeill 7:1add29d51e72 1814 case VL53L1_DISTANCEMODE_SHORT:
Charles MacNeill 7:1add29d51e72 1815
Charles MacNeill 7:1add29d51e72 1816 if (RefRange > MRLH)
Charles MacNeill 7:1add29d51e72 1817 *pNewDistanceMode = VL53L1_DISTANCEMODE_LONG;
Charles MacNeill 7:1add29d51e72 1818 else if (RefRange > HRLH)
Charles MacNeill 7:1add29d51e72 1819 *pNewDistanceMode = VL53L1_DISTANCEMODE_MEDIUM;
Charles MacNeill 7:1add29d51e72 1820 break;
Charles MacNeill 7:1add29d51e72 1821 case VL53L1_DISTANCEMODE_MEDIUM:
Charles MacNeill 7:1add29d51e72 1822
Charles MacNeill 7:1add29d51e72 1823 if (RefRange > MRLH)
Charles MacNeill 7:1add29d51e72 1824 *pNewDistanceMode = VL53L1_DISTANCEMODE_LONG;
Charles MacNeill 7:1add29d51e72 1825 else if (RefRange < HRLI)
Charles MacNeill 7:1add29d51e72 1826 *pNewDistanceMode = VL53L1_DISTANCEMODE_SHORT;
Charles MacNeill 7:1add29d51e72 1827 break;
Charles MacNeill 7:1add29d51e72 1828 default:
Charles MacNeill 7:1add29d51e72 1829
Charles MacNeill 7:1add29d51e72 1830 if (RefRange < HRLI)
Charles MacNeill 7:1add29d51e72 1831 *pNewDistanceMode = VL53L1_DISTANCEMODE_SHORT;
Charles MacNeill 7:1add29d51e72 1832 else if (RefRange < MRLI)
Charles MacNeill 7:1add29d51e72 1833 *pNewDistanceMode = VL53L1_DISTANCEMODE_MEDIUM;
Charles MacNeill 7:1add29d51e72 1834 break;
Charles MacNeill 7:1add29d51e72 1835 }
Charles MacNeill 7:1add29d51e72 1836 }
Charles MacNeill 7:1add29d51e72 1837
Charles MacNeill 7:1add29d51e72 1838 static void CheckAndChangeDistanceMode(VL53L1_DEV Dev,
Charles MacNeill 7:1add29d51e72 1839 VL53L1_TargetRangeData_t *pRangeData,
Charles MacNeill 7:1add29d51e72 1840 int16_t Ambient100DmaxMm,
Charles MacNeill 7:1add29d51e72 1841 VL53L1_DistanceModes *pNewDistanceMode
Charles MacNeill 7:1add29d51e72 1842 )
Charles MacNeill 7:1add29d51e72 1843 {
Charles MacNeill 7:1add29d51e72 1844 VL53L1_DistanceModes DistanceMode;
Charles MacNeill 7:1add29d51e72 1845 uint8_t RangeStatus = pRangeData->RangeStatus;
Charles MacNeill 7:1add29d51e72 1846 uint8_t DmaxValid;
Charles MacNeill 7:1add29d51e72 1847 int32_t MinAmbient = BDTable[VL53L1_TUNING_MIN_AMBIENT_DMAX_VALID];
Charles MacNeill 7:1add29d51e72 1848 VL53L1_LLDriverData_t *pdev = VL53L1DevStructGetLLDriverHandle(Dev);
Charles MacNeill 7:1add29d51e72 1849 int32_t tmpint32;
Charles MacNeill 7:1add29d51e72 1850
Charles MacNeill 7:1add29d51e72 1851
Charles MacNeill 7:1add29d51e72 1852 switch (RangeStatus) {
Charles MacNeill 7:1add29d51e72 1853 case VL53L1_RANGESTATUS_RANGE_VALID_NO_WRAP_CHECK_FAIL:
Charles MacNeill 7:1add29d51e72 1854 case VL53L1_RANGESTATUS_WRAP_TARGET_FAIL:
Charles MacNeill 7:1add29d51e72 1855 case VL53L1_RANGESTATUS_RANGE_VALID_MERGED_PULSE:
Charles MacNeill 7:1add29d51e72 1856 case VL53L1_RANGESTATUS_TARGET_PRESENT_LACK_OF_SIGNAL:
Charles MacNeill 7:1add29d51e72 1857 case VL53L1_RANGESTATUS_SYNCRONISATION_INT:
Charles MacNeill 7:1add29d51e72 1858 case VL53L1_RANGESTATUS_NONE:
Charles MacNeill 7:1add29d51e72 1859 return;
Charles MacNeill 7:1add29d51e72 1860 default:
Charles MacNeill 7:1add29d51e72 1861
Charles MacNeill 7:1add29d51e72 1862 break;
Charles MacNeill 7:1add29d51e72 1863 }
Charles MacNeill 7:1add29d51e72 1864
Charles MacNeill 7:1add29d51e72 1865 DmaxValid = 1;
Charles MacNeill 7:1add29d51e72 1866 tmpint32 = pdev->hist_data.VL53L1_p_004;
Charles MacNeill 7:1add29d51e72 1867 if ((tmpint32 < MinAmbient) || (Ambient100DmaxMm == 0))
Charles MacNeill 7:1add29d51e72 1868 DmaxValid = 0;
Charles MacNeill 7:1add29d51e72 1869
Charles MacNeill 7:1add29d51e72 1870 DistanceMode = VL53L1DevDataGet(Dev,
Charles MacNeill 7:1add29d51e72 1871 CurrentParameters.DistanceMode);
Charles MacNeill 7:1add29d51e72 1872
Charles MacNeill 7:1add29d51e72 1873 *pNewDistanceMode = DistanceMode;
Charles MacNeill 7:1add29d51e72 1874
Charles MacNeill 7:1add29d51e72 1875 if (RangeStatus == VL53L1_RANGESTATUS_RANGE_VALID)
Charles MacNeill 7:1add29d51e72 1876 GenNewPresetMode(pRangeData->RangeMilliMeter,
Charles MacNeill 7:1add29d51e72 1877 DistanceMode, pNewDistanceMode);
Charles MacNeill 7:1add29d51e72 1878 else {
Charles MacNeill 7:1add29d51e72 1879 if (DmaxValid)
Charles MacNeill 7:1add29d51e72 1880 GenNewPresetMode(Ambient100DmaxMm,
Charles MacNeill 7:1add29d51e72 1881 DistanceMode, pNewDistanceMode);
Charles MacNeill 7:1add29d51e72 1882 else
Charles MacNeill 7:1add29d51e72 1883 *pNewDistanceMode = VL53L1_DISTANCEMODE_LONG;
Charles MacNeill 7:1add29d51e72 1884 }
Charles MacNeill 7:1add29d51e72 1885 }
Charles MacNeill 7:1add29d51e72 1886
Charles MacNeill 7:1add29d51e72 1887 static uint8_t ComputeRQL(uint8_t active_results,
Charles MacNeill 7:1add29d51e72 1888 uint8_t FilteredRangeStatus,
Charles MacNeill 7:1add29d51e72 1889 VL53L1_range_data_t *presults_data)
Charles MacNeill 7:1add29d51e72 1890 {
Charles MacNeill 7:1add29d51e72 1891 int16_t T_Wide = 150;
Charles MacNeill 7:1add29d51e72 1892 int16_t SRL = 300;
Charles MacNeill 7:1add29d51e72 1893 uint16_t SRAS = 30;
Charles MacNeill 7:1add29d51e72 1894 FixPoint1616_t RAS;
Charles MacNeill 7:1add29d51e72 1895 FixPoint1616_t SRQL;
Charles MacNeill 7:1add29d51e72 1896 FixPoint1616_t GI = 7713587;
Charles MacNeill 7:1add29d51e72 1897 FixPoint1616_t GGm = 3198157;
Charles MacNeill 7:1add29d51e72 1898 FixPoint1616_t LRAP = 6554;
Charles MacNeill 7:1add29d51e72 1899 FixPoint1616_t partial;
Charles MacNeill 7:1add29d51e72 1900 uint8_t finalvalue;
Charles MacNeill 7:1add29d51e72 1901 uint8_t returnvalue;
Charles MacNeill 7:1add29d51e72 1902
Charles MacNeill 7:1add29d51e72 1903 if (active_results == 0)
Charles MacNeill 7:1add29d51e72 1904 returnvalue = 0;
Charles MacNeill 7:1add29d51e72 1905 else if (((presults_data->max_range_mm -
Charles MacNeill 7:1add29d51e72 1906 presults_data->min_range_mm) >= T_Wide) ||
Charles MacNeill 7:1add29d51e72 1907 (FilteredRangeStatus == VL53L1_DEVICEERROR_PHASECONSISTENCY))
Charles MacNeill 7:1add29d51e72 1908 returnvalue = 50;
Charles MacNeill 7:1add29d51e72 1909 else {
Charles MacNeill 7:1add29d51e72 1910 if (presults_data->median_range_mm < SRL)
Charles MacNeill 7:1add29d51e72 1911 RAS = SRAS * 65536;
Charles MacNeill 7:1add29d51e72 1912 else
Charles MacNeill 7:1add29d51e72 1913 RAS = LRAP * presults_data->median_range_mm;
Charles MacNeill 7:1add29d51e72 1914
Charles MacNeill 7:1add29d51e72 1915
Charles MacNeill 7:1add29d51e72 1916 if (RAS != 0) {
Charles MacNeill 7:1add29d51e72 1917 partial = (GGm * presults_data->VL53L1_p_005);
Charles MacNeill 7:1add29d51e72 1918 partial = partial + (RAS >> 1);
Charles MacNeill 7:1add29d51e72 1919 partial = partial / RAS;
Charles MacNeill 7:1add29d51e72 1920 partial = partial * 65536;
Charles MacNeill 7:1add29d51e72 1921 if (partial <= GI)
Charles MacNeill 7:1add29d51e72 1922 SRQL = GI - partial;
Charles MacNeill 7:1add29d51e72 1923 else
Charles MacNeill 7:1add29d51e72 1924 SRQL = 50 * 65536;
Charles MacNeill 7:1add29d51e72 1925 } else
Charles MacNeill 7:1add29d51e72 1926 SRQL = 100 * 65536;
Charles MacNeill 7:1add29d51e72 1927
Charles MacNeill 7:1add29d51e72 1928 finalvalue = (uint8_t)(SRQL >> 16);
Charles MacNeill 7:1add29d51e72 1929 returnvalue = MAX(50, MIN(100, finalvalue));
Charles MacNeill 7:1add29d51e72 1930 }
Charles MacNeill 7:1add29d51e72 1931
Charles MacNeill 7:1add29d51e72 1932 return returnvalue;
Charles MacNeill 7:1add29d51e72 1933 }
Charles MacNeill 7:1add29d51e72 1934
Charles MacNeill 7:1add29d51e72 1935
Charles MacNeill 7:1add29d51e72 1936 static uint8_t ConvertStatusLite(uint8_t FilteredRangeStatus)
Charles MacNeill 7:1add29d51e72 1937 {
Charles MacNeill 7:1add29d51e72 1938 uint8_t RangeStatus;
Charles MacNeill 7:1add29d51e72 1939
Charles MacNeill 7:1add29d51e72 1940 switch (FilteredRangeStatus) {
Charles MacNeill 7:1add29d51e72 1941 case VL53L1_DEVICEERROR_GPHSTREAMCOUNT0READY:
Charles MacNeill 7:1add29d51e72 1942 RangeStatus = VL53L1_RANGESTATUS_SYNCRONISATION_INT;
Charles MacNeill 7:1add29d51e72 1943 break;
Charles MacNeill 7:1add29d51e72 1944 case VL53L1_DEVICEERROR_RANGECOMPLETE_NO_WRAP_CHECK:
Charles MacNeill 7:1add29d51e72 1945 RangeStatus = VL53L1_RANGESTATUS_RANGE_VALID_NO_WRAP_CHECK_FAIL;
Charles MacNeill 7:1add29d51e72 1946 break;
Charles MacNeill 7:1add29d51e72 1947 case VL53L1_DEVICEERROR_RANGEPHASECHECK:
Charles MacNeill 7:1add29d51e72 1948 RangeStatus = VL53L1_RANGESTATUS_OUTOFBOUNDS_FAIL;
Charles MacNeill 7:1add29d51e72 1949 break;
Charles MacNeill 7:1add29d51e72 1950 case VL53L1_DEVICEERROR_MSRCNOTARGET:
Charles MacNeill 7:1add29d51e72 1951 RangeStatus = VL53L1_RANGESTATUS_SIGNAL_FAIL;
Charles MacNeill 7:1add29d51e72 1952 break;
Charles MacNeill 7:1add29d51e72 1953 case VL53L1_DEVICEERROR_SIGMATHRESHOLDCHECK:
Charles MacNeill 7:1add29d51e72 1954 RangeStatus = VL53L1_RANGESTATUS_SIGMA_FAIL;
Charles MacNeill 7:1add29d51e72 1955 break;
Charles MacNeill 7:1add29d51e72 1956 case VL53L1_DEVICEERROR_PHASECONSISTENCY:
Charles MacNeill 7:1add29d51e72 1957 RangeStatus = VL53L1_RANGESTATUS_WRAP_TARGET_FAIL;
Charles MacNeill 7:1add29d51e72 1958 break;
Charles MacNeill 7:1add29d51e72 1959 case VL53L1_DEVICEERROR_RANGEIGNORETHRESHOLD:
Charles MacNeill 7:1add29d51e72 1960 RangeStatus = VL53L1_RANGESTATUS_XTALK_SIGNAL_FAIL;
Charles MacNeill 7:1add29d51e72 1961 break;
Charles MacNeill 7:1add29d51e72 1962 case VL53L1_DEVICEERROR_MINCLIP:
Charles MacNeill 7:1add29d51e72 1963 RangeStatus = VL53L1_RANGESTATUS_RANGE_VALID_MIN_RANGE_CLIPPED;
Charles MacNeill 7:1add29d51e72 1964 break;
Charles MacNeill 7:1add29d51e72 1965 case VL53L1_DEVICEERROR_RANGECOMPLETE:
Charles MacNeill 7:1add29d51e72 1966 RangeStatus = VL53L1_RANGESTATUS_RANGE_VALID;
Charles MacNeill 7:1add29d51e72 1967 break;
Charles MacNeill 7:1add29d51e72 1968 default:
Charles MacNeill 7:1add29d51e72 1969 RangeStatus = VL53L1_RANGESTATUS_NONE;
Charles MacNeill 7:1add29d51e72 1970 }
Charles MacNeill 7:1add29d51e72 1971
Charles MacNeill 7:1add29d51e72 1972 return RangeStatus;
Charles MacNeill 7:1add29d51e72 1973 }
Charles MacNeill 7:1add29d51e72 1974
Charles MacNeill 7:1add29d51e72 1975
Charles MacNeill 7:1add29d51e72 1976 static uint8_t ConvertStatusHisto(uint8_t FilteredRangeStatus)
Charles MacNeill 7:1add29d51e72 1977 {
Charles MacNeill 7:1add29d51e72 1978 uint8_t RangeStatus;
Charles MacNeill 7:1add29d51e72 1979
Charles MacNeill 7:1add29d51e72 1980 switch (FilteredRangeStatus) {
Charles MacNeill 7:1add29d51e72 1981 case VL53L1_DEVICEERROR_RANGEPHASECHECK:
Charles MacNeill 7:1add29d51e72 1982 RangeStatus = VL53L1_RANGESTATUS_OUTOFBOUNDS_FAIL;
Charles MacNeill 7:1add29d51e72 1983 break;
Charles MacNeill 7:1add29d51e72 1984 case VL53L1_DEVICEERROR_SIGMATHRESHOLDCHECK:
Charles MacNeill 7:1add29d51e72 1985 RangeStatus = VL53L1_RANGESTATUS_SIGMA_FAIL;
Charles MacNeill 7:1add29d51e72 1986 break;
Charles MacNeill 7:1add29d51e72 1987 case VL53L1_DEVICEERROR_RANGECOMPLETE_NO_WRAP_CHECK:
Charles MacNeill 7:1add29d51e72 1988 RangeStatus = VL53L1_RANGESTATUS_RANGE_VALID_NO_WRAP_CHECK_FAIL;
Charles MacNeill 7:1add29d51e72 1989 break;
Charles MacNeill 7:1add29d51e72 1990 case VL53L1_DEVICEERROR_PHASECONSISTENCY:
Charles MacNeill 7:1add29d51e72 1991 RangeStatus = VL53L1_RANGESTATUS_WRAP_TARGET_FAIL;
Charles MacNeill 7:1add29d51e72 1992 break;
Charles MacNeill 7:1add29d51e72 1993 case VL53L1_DEVICEERROR_PREV_RANGE_NO_TARGETS:
Charles MacNeill 7:1add29d51e72 1994 RangeStatus = VL53L1_RANGESTATUS_TARGET_PRESENT_LACK_OF_SIGNAL;
Charles MacNeill 7:1add29d51e72 1995 break;
Charles MacNeill 7:1add29d51e72 1996 case VL53L1_DEVICEERROR_EVENTCONSISTENCY:
Charles MacNeill 7:1add29d51e72 1997 RangeStatus = VL53L1_RANGESTATUS_WRAP_TARGET_FAIL;
Charles MacNeill 7:1add29d51e72 1998 break;
Charles MacNeill 7:1add29d51e72 1999 case VL53L1_DEVICEERROR_RANGECOMPLETE_MERGED_PULSE:
Charles MacNeill 7:1add29d51e72 2000 RangeStatus = VL53L1_RANGESTATUS_RANGE_VALID_MERGED_PULSE;
Charles MacNeill 7:1add29d51e72 2001 break;
Charles MacNeill 7:1add29d51e72 2002 case VL53L1_DEVICEERROR_RANGECOMPLETE:
Charles MacNeill 7:1add29d51e72 2003 RangeStatus = VL53L1_RANGESTATUS_RANGE_VALID;
Charles MacNeill 7:1add29d51e72 2004 break;
Charles MacNeill 7:1add29d51e72 2005 default:
Charles MacNeill 7:1add29d51e72 2006 RangeStatus = VL53L1_RANGESTATUS_NONE;
Charles MacNeill 7:1add29d51e72 2007 }
Charles MacNeill 7:1add29d51e72 2008
Charles MacNeill 7:1add29d51e72 2009 return RangeStatus;
Charles MacNeill 7:1add29d51e72 2010 }
Charles MacNeill 7:1add29d51e72 2011
Charles MacNeill 7:1add29d51e72 2012 static VL53L1_Error SetSimpleData(VL53L1_DEV Dev,
Charles MacNeill 7:1add29d51e72 2013 uint8_t active_results, uint8_t device_status,
Charles MacNeill 7:1add29d51e72 2014 VL53L1_range_data_t *presults_data,
Charles MacNeill 7:1add29d51e72 2015 VL53L1_RangingMeasurementData_t *pRangeData)
Charles MacNeill 7:1add29d51e72 2016 {
Charles MacNeill 7:1add29d51e72 2017 VL53L1_Error Status = VL53L1_ERROR_NONE;
Charles MacNeill 7:1add29d51e72 2018 uint8_t FilteredRangeStatus;
Charles MacNeill 7:1add29d51e72 2019 uint8_t SigmaLimitflag;
Charles MacNeill 7:1add29d51e72 2020 uint8_t SignalLimitflag;
Charles MacNeill 7:1add29d51e72 2021 uint8_t Temp8Enable;
Charles MacNeill 7:1add29d51e72 2022 uint8_t Temp8;
Charles MacNeill 7:1add29d51e72 2023 FixPoint1616_t AmbientRate;
Charles MacNeill 7:1add29d51e72 2024 FixPoint1616_t SignalRate;
Charles MacNeill 7:1add29d51e72 2025 FixPoint1616_t TempFix1616;
Charles MacNeill 7:1add29d51e72 2026 FixPoint1616_t LimitCheckValue;
Charles MacNeill 7:1add29d51e72 2027 VL53L1_PresetModes PresetMode;
Charles MacNeill 7:1add29d51e72 2028 int16_t Range;
Charles MacNeill 7:1add29d51e72 2029
Charles MacNeill 7:1add29d51e72 2030 pRangeData->TimeStamp = presults_data->time_stamp;
Charles MacNeill 7:1add29d51e72 2031
Charles MacNeill 7:1add29d51e72 2032 FilteredRangeStatus = presults_data->range_status & 0x1F;
Charles MacNeill 7:1add29d51e72 2033
Charles MacNeill 7:1add29d51e72 2034 pRangeData->RangeQualityLevel = ComputeRQL(active_results,
Charles MacNeill 7:1add29d51e72 2035 FilteredRangeStatus,
Charles MacNeill 7:1add29d51e72 2036 presults_data);
Charles MacNeill 7:1add29d51e72 2037
Charles MacNeill 7:1add29d51e72 2038 SignalRate = VL53L1_FIXPOINT97TOFIXPOINT1616(
Charles MacNeill 7:1add29d51e72 2039 presults_data->peak_signal_count_rate_mcps);
Charles MacNeill 7:1add29d51e72 2040 pRangeData->SignalRateRtnMegaCps
Charles MacNeill 7:1add29d51e72 2041 = SignalRate;
Charles MacNeill 7:1add29d51e72 2042
Charles MacNeill 7:1add29d51e72 2043 AmbientRate = VL53L1_FIXPOINT97TOFIXPOINT1616(
Charles MacNeill 7:1add29d51e72 2044 presults_data->ambient_count_rate_mcps);
Charles MacNeill 7:1add29d51e72 2045 pRangeData->AmbientRateRtnMegaCps = AmbientRate;
Charles MacNeill 7:1add29d51e72 2046
Charles MacNeill 7:1add29d51e72 2047 pRangeData->EffectiveSpadRtnCount =
Charles MacNeill 7:1add29d51e72 2048 presults_data->VL53L1_p_006;
Charles MacNeill 7:1add29d51e72 2049
Charles MacNeill 7:1add29d51e72 2050 TempFix1616 = VL53L1_FIXPOINT97TOFIXPOINT1616(
Charles MacNeill 7:1add29d51e72 2051 presults_data->VL53L1_p_005);
Charles MacNeill 7:1add29d51e72 2052
Charles MacNeill 7:1add29d51e72 2053 pRangeData->SigmaMilliMeter = TempFix1616;
Charles MacNeill 7:1add29d51e72 2054
Charles MacNeill 7:1add29d51e72 2055 pRangeData->RangeMilliMeter = presults_data->median_range_mm;
Charles MacNeill 7:1add29d51e72 2056
Charles MacNeill 7:1add29d51e72 2057 pRangeData->RangeFractionalPart = 0;
Charles MacNeill 7:1add29d51e72 2058
Charles MacNeill 7:1add29d51e72 2059
Charles MacNeill 7:1add29d51e72 2060 switch (device_status) {
Charles MacNeill 7:1add29d51e72 2061 case VL53L1_DEVICEERROR_MULTCLIPFAIL:
Charles MacNeill 7:1add29d51e72 2062 case VL53L1_DEVICEERROR_VCSELWATCHDOGTESTFAILURE:
Charles MacNeill 7:1add29d51e72 2063 case VL53L1_DEVICEERROR_VCSELCONTINUITYTESTFAILURE:
Charles MacNeill 7:1add29d51e72 2064 case VL53L1_DEVICEERROR_NOVHVVALUEFOUND:
Charles MacNeill 7:1add29d51e72 2065 pRangeData->RangeStatus = VL53L1_RANGESTATUS_HARDWARE_FAIL;
Charles MacNeill 7:1add29d51e72 2066 break;
Charles MacNeill 7:1add29d51e72 2067 case VL53L1_DEVICEERROR_USERROICLIP:
Charles MacNeill 7:1add29d51e72 2068 pRangeData->RangeStatus = VL53L1_RANGESTATUS_MIN_RANGE_FAIL;
Charles MacNeill 7:1add29d51e72 2069 break;
Charles MacNeill 7:1add29d51e72 2070 default:
Charles MacNeill 7:1add29d51e72 2071 pRangeData->RangeStatus = VL53L1_RANGESTATUS_RANGE_VALID;
Charles MacNeill 7:1add29d51e72 2072 }
Charles MacNeill 7:1add29d51e72 2073
Charles MacNeill 7:1add29d51e72 2074
Charles MacNeill 7:1add29d51e72 2075 if (pRangeData->RangeStatus == VL53L1_RANGESTATUS_RANGE_VALID) {
Charles MacNeill 7:1add29d51e72 2076 PresetMode = VL53L1DevDataGet(Dev,
Charles MacNeill 7:1add29d51e72 2077 CurrentParameters.PresetMode);
Charles MacNeill 7:1add29d51e72 2078 if ((PresetMode == VL53L1_PRESETMODE_MULTIZONES_SCANNING) ||
Charles MacNeill 7:1add29d51e72 2079 (PresetMode == VL53L1_PRESETMODE_RANGING) ||
Charles MacNeill 7:1add29d51e72 2080 (PresetMode == VL53L1_PRESETMODE_PROXY_RANGING_MODE))
Charles MacNeill 7:1add29d51e72 2081 pRangeData->RangeStatus =
Charles MacNeill 7:1add29d51e72 2082 ConvertStatusHisto(FilteredRangeStatus);
Charles MacNeill 7:1add29d51e72 2083 else
Charles MacNeill 7:1add29d51e72 2084 pRangeData->RangeStatus =
Charles MacNeill 7:1add29d51e72 2085 ConvertStatusLite(FilteredRangeStatus);
Charles MacNeill 7:1add29d51e72 2086 }
Charles MacNeill 7:1add29d51e72 2087
Charles MacNeill 7:1add29d51e72 2088
Charles MacNeill 7:1add29d51e72 2089 TempFix1616 = VL53L1_FIXPOINT97TOFIXPOINT1616(
Charles MacNeill 7:1add29d51e72 2090 presults_data->VL53L1_p_005);
Charles MacNeill 7:1add29d51e72 2091 VL53L1_SETARRAYPARAMETERFIELD(Dev,
Charles MacNeill 7:1add29d51e72 2092 LimitChecksCurrent, VL53L1_CHECKENABLE_SIGMA_FINAL_RANGE,
Charles MacNeill 7:1add29d51e72 2093 TempFix1616);
Charles MacNeill 7:1add29d51e72 2094
Charles MacNeill 7:1add29d51e72 2095 TempFix1616 = VL53L1_FIXPOINT97TOFIXPOINT1616(
Charles MacNeill 7:1add29d51e72 2096 presults_data->peak_signal_count_rate_mcps);
Charles MacNeill 7:1add29d51e72 2097 VL53L1_SETARRAYPARAMETERFIELD(Dev,
Charles MacNeill 7:1add29d51e72 2098 LimitChecksCurrent, VL53L1_CHECKENABLE_SIGNAL_RATE_FINAL_RANGE,
Charles MacNeill 7:1add29d51e72 2099 TempFix1616);
Charles MacNeill 7:1add29d51e72 2100
Charles MacNeill 7:1add29d51e72 2101
Charles MacNeill 7:1add29d51e72 2102
Charles MacNeill 7:1add29d51e72 2103 VL53L1_GetLimitCheckValue(Dev,
Charles MacNeill 7:1add29d51e72 2104 VL53L1_CHECKENABLE_SIGMA_FINAL_RANGE,
Charles MacNeill 7:1add29d51e72 2105 &LimitCheckValue);
Charles MacNeill 7:1add29d51e72 2106
Charles MacNeill 7:1add29d51e72 2107 SigmaLimitflag = (FilteredRangeStatus ==
Charles MacNeill 7:1add29d51e72 2108 VL53L1_DEVICEERROR_SIGMATHRESHOLDCHECK)
Charles MacNeill 7:1add29d51e72 2109 ? 1 : 0;
Charles MacNeill 7:1add29d51e72 2110
Charles MacNeill 7:1add29d51e72 2111 VL53L1_GetLimitCheckEnable(Dev,
Charles MacNeill 7:1add29d51e72 2112 VL53L1_CHECKENABLE_SIGMA_FINAL_RANGE,
Charles MacNeill 7:1add29d51e72 2113 &Temp8Enable);
Charles MacNeill 7:1add29d51e72 2114
Charles MacNeill 7:1add29d51e72 2115 Temp8 = ((Temp8Enable == 1) && (SigmaLimitflag == 1)) ? 1 : 0;
Charles MacNeill 7:1add29d51e72 2116 VL53L1_SETARRAYPARAMETERFIELD(Dev, LimitChecksStatus,
Charles MacNeill 7:1add29d51e72 2117 VL53L1_CHECKENABLE_SIGMA_FINAL_RANGE, Temp8);
Charles MacNeill 7:1add29d51e72 2118
Charles MacNeill 7:1add29d51e72 2119
Charles MacNeill 7:1add29d51e72 2120 VL53L1_GetLimitCheckValue(Dev,
Charles MacNeill 7:1add29d51e72 2121 VL53L1_CHECKENABLE_SIGNAL_RATE_FINAL_RANGE,
Charles MacNeill 7:1add29d51e72 2122 &LimitCheckValue);
Charles MacNeill 7:1add29d51e72 2123
Charles MacNeill 7:1add29d51e72 2124 SignalLimitflag = (FilteredRangeStatus ==
Charles MacNeill 7:1add29d51e72 2125 VL53L1_DEVICEERROR_MSRCNOTARGET)
Charles MacNeill 7:1add29d51e72 2126 ? 1 : 0;
Charles MacNeill 7:1add29d51e72 2127
Charles MacNeill 7:1add29d51e72 2128 VL53L1_GetLimitCheckEnable(Dev,
Charles MacNeill 7:1add29d51e72 2129 VL53L1_CHECKENABLE_SIGNAL_RATE_FINAL_RANGE,
Charles MacNeill 7:1add29d51e72 2130 &Temp8Enable);
Charles MacNeill 7:1add29d51e72 2131
Charles MacNeill 7:1add29d51e72 2132 Temp8 = ((Temp8Enable == 1) && (SignalLimitflag == 1)) ? 1 : 0;
Charles MacNeill 7:1add29d51e72 2133 VL53L1_SETARRAYPARAMETERFIELD(Dev, LimitChecksStatus,
Charles MacNeill 7:1add29d51e72 2134 VL53L1_CHECKENABLE_SIGNAL_RATE_FINAL_RANGE, Temp8);
Charles MacNeill 7:1add29d51e72 2135
Charles MacNeill 7:1add29d51e72 2136 Range = pRangeData->RangeMilliMeter;
Charles MacNeill 7:1add29d51e72 2137 if ((pRangeData->RangeStatus == VL53L1_RANGESTATUS_RANGE_VALID) &&
Charles MacNeill 7:1add29d51e72 2138 (Range < 0)) {
Charles MacNeill 7:1add29d51e72 2139 if (Range < BDTable[VL53L1_TUNING_PROXY_MIN])
Charles MacNeill 7:1add29d51e72 2140 pRangeData->RangeStatus =
Charles MacNeill 7:1add29d51e72 2141 VL53L1_RANGESTATUS_RANGE_INVALID;
Charles MacNeill 7:1add29d51e72 2142 else
Charles MacNeill 7:1add29d51e72 2143 pRangeData->RangeMilliMeter = 0;
Charles MacNeill 7:1add29d51e72 2144 }
Charles MacNeill 7:1add29d51e72 2145
Charles MacNeill 7:1add29d51e72 2146 return Status;
Charles MacNeill 7:1add29d51e72 2147 }
Charles MacNeill 7:1add29d51e72 2148
Charles MacNeill 7:1add29d51e72 2149 static VL53L1_Error SetTargetData(VL53L1_DEV Dev,
Charles MacNeill 7:1add29d51e72 2150 uint8_t active_results, uint8_t streamcount, uint8_t iteration,
Charles MacNeill 7:1add29d51e72 2151 uint8_t device_status, VL53L1_range_data_t *presults_data,
Charles MacNeill 7:1add29d51e72 2152 VL53L1_TargetRangeData_t *pRangeData)
Charles MacNeill 7:1add29d51e72 2153 {
Charles MacNeill 7:1add29d51e72 2154 VL53L1_Error Status = VL53L1_ERROR_NONE;
Charles MacNeill 7:1add29d51e72 2155 VL53L1_LLDriverData_t *pdev =
Charles MacNeill 7:1add29d51e72 2156 VL53L1DevStructGetLLDriverHandle(Dev);
Charles MacNeill 7:1add29d51e72 2157 VL53L1_tuning_parm_storage_t *tp =
Charles MacNeill 7:1add29d51e72 2158 &(pdev->tuning_parms);
Charles MacNeill 7:1add29d51e72 2159 uint8_t sequency;
Charles MacNeill 7:1add29d51e72 2160 uint8_t FilteredRangeStatus;
Charles MacNeill 7:1add29d51e72 2161 uint8_t SigmaLimitflag;
Charles MacNeill 7:1add29d51e72 2162 uint8_t SignalLimitflag;
Charles MacNeill 7:1add29d51e72 2163 uint8_t Temp8Enable;
Charles MacNeill 7:1add29d51e72 2164 uint8_t Temp8;
Charles MacNeill 7:1add29d51e72 2165 FixPoint1616_t AmbientRate;
Charles MacNeill 7:1add29d51e72 2166 FixPoint1616_t SignalRate;
Charles MacNeill 7:1add29d51e72 2167 FixPoint1616_t TempFix1616;
Charles MacNeill 7:1add29d51e72 2168 FixPoint1616_t LimitCheckValue;
Charles MacNeill 7:1add29d51e72 2169 VL53L1_PresetModes PresetMode;
Charles MacNeill 7:1add29d51e72 2170 int16_t Range, RangeDiff, RangeMillimeterInit;
Charles MacNeill 7:1add29d51e72 2171 int32_t ExtendedRangeEnabled = 0;
Charles MacNeill 7:1add29d51e72 2172 uint8_t uwr_status;
Charles MacNeill 7:1add29d51e72 2173 int16_t AddOffset;
Charles MacNeill 7:1add29d51e72 2174
Charles MacNeill 7:1add29d51e72 2175 FilteredRangeStatus = presults_data->range_status & 0x1F;
Charles MacNeill 7:1add29d51e72 2176
Charles MacNeill 7:1add29d51e72 2177 pRangeData->RangeQualityLevel = ComputeRQL(active_results,
Charles MacNeill 7:1add29d51e72 2178 FilteredRangeStatus,
Charles MacNeill 7:1add29d51e72 2179 presults_data);
Charles MacNeill 7:1add29d51e72 2180
Charles MacNeill 7:1add29d51e72 2181 SignalRate = VL53L1_FIXPOINT97TOFIXPOINT1616(
Charles MacNeill 7:1add29d51e72 2182 presults_data->peak_signal_count_rate_mcps);
Charles MacNeill 7:1add29d51e72 2183 pRangeData->SignalRateRtnMegaCps
Charles MacNeill 7:1add29d51e72 2184 = SignalRate;
Charles MacNeill 7:1add29d51e72 2185
Charles MacNeill 7:1add29d51e72 2186 AmbientRate = VL53L1_FIXPOINT97TOFIXPOINT1616(
Charles MacNeill 7:1add29d51e72 2187 presults_data->ambient_count_rate_mcps);
Charles MacNeill 7:1add29d51e72 2188 pRangeData->AmbientRateRtnMegaCps = AmbientRate;
Charles MacNeill 7:1add29d51e72 2189
Charles MacNeill 7:1add29d51e72 2190 TempFix1616 = VL53L1_FIXPOINT97TOFIXPOINT1616(
Charles MacNeill 7:1add29d51e72 2191 presults_data->VL53L1_p_005);
Charles MacNeill 7:1add29d51e72 2192
Charles MacNeill 7:1add29d51e72 2193 pRangeData->SigmaMilliMeter = TempFix1616;
Charles MacNeill 7:1add29d51e72 2194
Charles MacNeill 7:1add29d51e72 2195 pRangeData->RangeMilliMeter = presults_data->median_range_mm;
Charles MacNeill 7:1add29d51e72 2196 pRangeData->RangeMaxMilliMeter = presults_data->max_range_mm;
Charles MacNeill 7:1add29d51e72 2197 pRangeData->RangeMinMilliMeter = presults_data->min_range_mm;
Charles MacNeill 7:1add29d51e72 2198
Charles MacNeill 7:1add29d51e72 2199 pRangeData->RangeFractionalPart = 0;
Charles MacNeill 7:1add29d51e72 2200
Charles MacNeill 7:1add29d51e72 2201
Charles MacNeill 7:1add29d51e72 2202 switch (device_status) {
Charles MacNeill 7:1add29d51e72 2203 case VL53L1_DEVICEERROR_MULTCLIPFAIL:
Charles MacNeill 7:1add29d51e72 2204 case VL53L1_DEVICEERROR_VCSELWATCHDOGTESTFAILURE:
Charles MacNeill 7:1add29d51e72 2205 case VL53L1_DEVICEERROR_VCSELCONTINUITYTESTFAILURE:
Charles MacNeill 7:1add29d51e72 2206 case VL53L1_DEVICEERROR_NOVHVVALUEFOUND:
Charles MacNeill 7:1add29d51e72 2207 pRangeData->RangeStatus = VL53L1_RANGESTATUS_HARDWARE_FAIL;
Charles MacNeill 7:1add29d51e72 2208 break;
Charles MacNeill 7:1add29d51e72 2209 case VL53L1_DEVICEERROR_USERROICLIP:
Charles MacNeill 7:1add29d51e72 2210 pRangeData->RangeStatus = VL53L1_RANGESTATUS_MIN_RANGE_FAIL;
Charles MacNeill 7:1add29d51e72 2211 break;
Charles MacNeill 7:1add29d51e72 2212 default:
Charles MacNeill 7:1add29d51e72 2213 pRangeData->RangeStatus = VL53L1_RANGESTATUS_RANGE_VALID;
Charles MacNeill 7:1add29d51e72 2214 }
Charles MacNeill 7:1add29d51e72 2215
Charles MacNeill 7:1add29d51e72 2216
Charles MacNeill 7:1add29d51e72 2217 if ((pRangeData->RangeStatus == VL53L1_RANGESTATUS_RANGE_VALID) &&
Charles MacNeill 7:1add29d51e72 2218 (active_results == 0)) {
Charles MacNeill 7:1add29d51e72 2219 pRangeData->RangeStatus = VL53L1_RANGESTATUS_NONE;
Charles MacNeill 7:1add29d51e72 2220 pRangeData->SignalRateRtnMegaCps = 0;
Charles MacNeill 7:1add29d51e72 2221 pRangeData->SigmaMilliMeter = 0;
Charles MacNeill 7:1add29d51e72 2222 pRangeData->RangeMilliMeter = 8191;
Charles MacNeill 7:1add29d51e72 2223 pRangeData->RangeMaxMilliMeter = 8191;
Charles MacNeill 7:1add29d51e72 2224 pRangeData->RangeMinMilliMeter = 8191;
Charles MacNeill 7:1add29d51e72 2225 }
Charles MacNeill 7:1add29d51e72 2226
Charles MacNeill 7:1add29d51e72 2227
Charles MacNeill 7:1add29d51e72 2228 if (pRangeData->RangeStatus == VL53L1_RANGESTATUS_RANGE_VALID) {
Charles MacNeill 7:1add29d51e72 2229 PresetMode = VL53L1DevDataGet(Dev,
Charles MacNeill 7:1add29d51e72 2230 CurrentParameters.PresetMode);
Charles MacNeill 7:1add29d51e72 2231 if ((PresetMode == VL53L1_PRESETMODE_MULTIZONES_SCANNING) ||
Charles MacNeill 7:1add29d51e72 2232 (PresetMode == VL53L1_PRESETMODE_RANGING) ||
Charles MacNeill 7:1add29d51e72 2233 (PresetMode == VL53L1_PRESETMODE_PROXY_RANGING_MODE))
Charles MacNeill 7:1add29d51e72 2234 pRangeData->RangeStatus =
Charles MacNeill 7:1add29d51e72 2235 ConvertStatusHisto(FilteredRangeStatus);
Charles MacNeill 7:1add29d51e72 2236 else
Charles MacNeill 7:1add29d51e72 2237 pRangeData->RangeStatus =
Charles MacNeill 7:1add29d51e72 2238 ConvertStatusLite(FilteredRangeStatus);
Charles MacNeill 7:1add29d51e72 2239 }
Charles MacNeill 7:1add29d51e72 2240
Charles MacNeill 7:1add29d51e72 2241
Charles MacNeill 7:1add29d51e72 2242 TempFix1616 = VL53L1_FIXPOINT97TOFIXPOINT1616(
Charles MacNeill 7:1add29d51e72 2243 presults_data->VL53L1_p_005);
Charles MacNeill 7:1add29d51e72 2244 VL53L1_SETARRAYPARAMETERFIELD(Dev,
Charles MacNeill 7:1add29d51e72 2245 LimitChecksCurrent, VL53L1_CHECKENABLE_SIGMA_FINAL_RANGE,
Charles MacNeill 7:1add29d51e72 2246 TempFix1616);
Charles MacNeill 7:1add29d51e72 2247
Charles MacNeill 7:1add29d51e72 2248 TempFix1616 = VL53L1_FIXPOINT97TOFIXPOINT1616(
Charles MacNeill 7:1add29d51e72 2249 presults_data->peak_signal_count_rate_mcps);
Charles MacNeill 7:1add29d51e72 2250 VL53L1_SETARRAYPARAMETERFIELD(Dev,
Charles MacNeill 7:1add29d51e72 2251 LimitChecksCurrent, VL53L1_CHECKENABLE_SIGNAL_RATE_FINAL_RANGE,
Charles MacNeill 7:1add29d51e72 2252 TempFix1616);
Charles MacNeill 7:1add29d51e72 2253
Charles MacNeill 7:1add29d51e72 2254
Charles MacNeill 7:1add29d51e72 2255
Charles MacNeill 7:1add29d51e72 2256 VL53L1_GetLimitCheckValue(Dev,
Charles MacNeill 7:1add29d51e72 2257 VL53L1_CHECKENABLE_SIGMA_FINAL_RANGE,
Charles MacNeill 7:1add29d51e72 2258 &LimitCheckValue);
Charles MacNeill 7:1add29d51e72 2259
Charles MacNeill 7:1add29d51e72 2260 SigmaLimitflag = (FilteredRangeStatus ==
Charles MacNeill 7:1add29d51e72 2261 VL53L1_DEVICEERROR_SIGMATHRESHOLDCHECK)
Charles MacNeill 7:1add29d51e72 2262 ? 1 : 0;
Charles MacNeill 7:1add29d51e72 2263
Charles MacNeill 7:1add29d51e72 2264 VL53L1_GetLimitCheckEnable(Dev,
Charles MacNeill 7:1add29d51e72 2265 VL53L1_CHECKENABLE_SIGMA_FINAL_RANGE,
Charles MacNeill 7:1add29d51e72 2266 &Temp8Enable);
Charles MacNeill 7:1add29d51e72 2267
Charles MacNeill 7:1add29d51e72 2268 Temp8 = ((Temp8Enable == 1) && (SigmaLimitflag == 1)) ? 1 : 0;
Charles MacNeill 7:1add29d51e72 2269 VL53L1_SETARRAYPARAMETERFIELD(Dev, LimitChecksStatus,
Charles MacNeill 7:1add29d51e72 2270 VL53L1_CHECKENABLE_SIGMA_FINAL_RANGE, Temp8);
Charles MacNeill 7:1add29d51e72 2271
Charles MacNeill 7:1add29d51e72 2272
Charles MacNeill 7:1add29d51e72 2273 VL53L1_GetLimitCheckValue(Dev,
Charles MacNeill 7:1add29d51e72 2274 VL53L1_CHECKENABLE_SIGNAL_RATE_FINAL_RANGE,
Charles MacNeill 7:1add29d51e72 2275 &LimitCheckValue);
Charles MacNeill 7:1add29d51e72 2276
Charles MacNeill 7:1add29d51e72 2277 SignalLimitflag = (FilteredRangeStatus ==
Charles MacNeill 7:1add29d51e72 2278 VL53L1_DEVICEERROR_MSRCNOTARGET)
Charles MacNeill 7:1add29d51e72 2279 ? 1 : 0;
Charles MacNeill 7:1add29d51e72 2280
Charles MacNeill 7:1add29d51e72 2281 VL53L1_GetLimitCheckEnable(Dev,
Charles MacNeill 7:1add29d51e72 2282 VL53L1_CHECKENABLE_SIGNAL_RATE_FINAL_RANGE,
Charles MacNeill 7:1add29d51e72 2283 &Temp8Enable);
Charles MacNeill 7:1add29d51e72 2284
Charles MacNeill 7:1add29d51e72 2285 Temp8 = ((Temp8Enable == 1) && (SignalLimitflag == 1)) ? 1 : 0;
Charles MacNeill 7:1add29d51e72 2286 VL53L1_SETARRAYPARAMETERFIELD(Dev, LimitChecksStatus,
Charles MacNeill 7:1add29d51e72 2287 VL53L1_CHECKENABLE_SIGNAL_RATE_FINAL_RANGE, Temp8);
Charles MacNeill 7:1add29d51e72 2288
Charles MacNeill 7:1add29d51e72 2289 Range = pRangeData->RangeMilliMeter;
Charles MacNeill 7:1add29d51e72 2290 if ((pRangeData->RangeStatus == VL53L1_RANGESTATUS_RANGE_VALID) &&
Charles MacNeill 7:1add29d51e72 2291 (Range < 0)) {
Charles MacNeill 7:1add29d51e72 2292 if (Range < BDTable[VL53L1_TUNING_PROXY_MIN])
Charles MacNeill 7:1add29d51e72 2293 pRangeData->RangeStatus =
Charles MacNeill 7:1add29d51e72 2294 VL53L1_RANGESTATUS_RANGE_INVALID;
Charles MacNeill 7:1add29d51e72 2295 else
Charles MacNeill 7:1add29d51e72 2296 pRangeData->RangeMilliMeter = 0;
Charles MacNeill 7:1add29d51e72 2297 }
Charles MacNeill 7:1add29d51e72 2298
Charles MacNeill 7:1add29d51e72 2299
Charles MacNeill 7:1add29d51e72 2300
Charles MacNeill 7:1add29d51e72 2301 VL53L1_get_tuning_parm(Dev, VL53L1_TUNINGPARM_UWR_ENABLE,
Charles MacNeill 7:1add29d51e72 2302 &ExtendedRangeEnabled);
Charles MacNeill 7:1add29d51e72 2303
Charles MacNeill 7:1add29d51e72 2304 sequency = streamcount % 2;
Charles MacNeill 7:1add29d51e72 2305 uwr_status = 0;
Charles MacNeill 7:1add29d51e72 2306 RangeMillimeterInit = pRangeData->RangeMilliMeter;
Charles MacNeill 7:1add29d51e72 2307 AddOffset = 0;
Charles MacNeill 7:1add29d51e72 2308
Charles MacNeill 7:1add29d51e72 2309 pRangeData->ExtendedRange = 0;
Charles MacNeill 7:1add29d51e72 2310
Charles MacNeill 7:1add29d51e72 2311 if (ExtendedRangeEnabled &&
Charles MacNeill 7:1add29d51e72 2312 (pRangeData->RangeStatus ==
Charles MacNeill 7:1add29d51e72 2313 VL53L1_RANGESTATUS_WRAP_TARGET_FAIL ||
Charles MacNeill 7:1add29d51e72 2314 pRangeData->RangeStatus ==
Charles MacNeill 7:1add29d51e72 2315 VL53L1_RANGESTATUS_OUTOFBOUNDS_FAIL)
Charles MacNeill 7:1add29d51e72 2316 && (pdev->PreviousRangeStatus[iteration] ==
Charles MacNeill 7:1add29d51e72 2317 VL53L1_RANGESTATUS_WRAP_TARGET_FAIL ||
Charles MacNeill 7:1add29d51e72 2318 pdev->PreviousRangeStatus[iteration] ==
Charles MacNeill 7:1add29d51e72 2319 VL53L1_RANGESTATUS_OUTOFBOUNDS_FAIL ||
Charles MacNeill 7:1add29d51e72 2320 (pdev->PreviousRangeStatus[iteration] ==
Charles MacNeill 7:1add29d51e72 2321 VL53L1_RANGESTATUS_RANGE_VALID &&
Charles MacNeill 7:1add29d51e72 2322 pdev->PreviousExtendedRange[iteration] == 1)))
Charles MacNeill 7:1add29d51e72 2323 {
Charles MacNeill 7:1add29d51e72 2324 if (((pdev->PreviousStreamCount) ==
Charles MacNeill 7:1add29d51e72 2325 (pdev->hist_data.result__stream_count - 1 ))
Charles MacNeill 7:1add29d51e72 2326 || ((pdev->PreviousStreamCount) ==
Charles MacNeill 7:1add29d51e72 2327 (pdev->hist_data.result__stream_count + 127)))
Charles MacNeill 7:1add29d51e72 2328 {
Charles MacNeill 7:1add29d51e72 2329 RangeDiff = pRangeData->RangeMilliMeter -
Charles MacNeill 7:1add29d51e72 2330 pdev->PreviousRangeMilliMeter[iteration];
Charles MacNeill 7:1add29d51e72 2331
Charles MacNeill 7:1add29d51e72 2332 uwr_status = 1;
Charles MacNeill 7:1add29d51e72 2333 switch (pdev->preset_mode) {
Charles MacNeill 7:1add29d51e72 2334 case VL53L1_DEVICEPRESETMODE_HISTOGRAM_SHORT_RANGE:
Charles MacNeill 7:1add29d51e72 2335
Charles MacNeill 7:1add29d51e72 2336 uwr_status = 0;
Charles MacNeill 7:1add29d51e72 2337 break;
Charles MacNeill 7:1add29d51e72 2338
Charles MacNeill 7:1add29d51e72 2339 case VL53L1_DEVICEPRESETMODE_HISTOGRAM_MEDIUM_RANGE:
Charles MacNeill 7:1add29d51e72 2340 if (RangeDiff > tp->tp_uwr_med_z_1_min &&
Charles MacNeill 7:1add29d51e72 2341 RangeDiff < tp->tp_uwr_med_z_1_max &&
Charles MacNeill 7:1add29d51e72 2342 sequency == 1) {
Charles MacNeill 7:1add29d51e72 2343 AddOffset =
Charles MacNeill 7:1add29d51e72 2344 tp->tp_uwr_med_corr_z_1_rangeb;
Charles MacNeill 7:1add29d51e72 2345 }
Charles MacNeill 7:1add29d51e72 2346 else
Charles MacNeill 7:1add29d51e72 2347 if (RangeDiff < -tp->tp_uwr_med_z_1_min &&
Charles MacNeill 7:1add29d51e72 2348 RangeDiff > -tp->tp_uwr_med_z_1_max &&
Charles MacNeill 7:1add29d51e72 2349 sequency == 0) {
Charles MacNeill 7:1add29d51e72 2350 AddOffset =
Charles MacNeill 7:1add29d51e72 2351 tp->tp_uwr_med_corr_z_1_rangea;
Charles MacNeill 7:1add29d51e72 2352 }
Charles MacNeill 7:1add29d51e72 2353 else
Charles MacNeill 7:1add29d51e72 2354 if (RangeDiff > tp->tp_uwr_med_z_2_min &&
Charles MacNeill 7:1add29d51e72 2355 RangeDiff < tp->tp_uwr_med_z_2_max &&
Charles MacNeill 7:1add29d51e72 2356 sequency == 0) {
Charles MacNeill 7:1add29d51e72 2357 AddOffset =
Charles MacNeill 7:1add29d51e72 2358 tp->tp_uwr_med_corr_z_2_rangea;
Charles MacNeill 7:1add29d51e72 2359 }
Charles MacNeill 7:1add29d51e72 2360 else
Charles MacNeill 7:1add29d51e72 2361 if (RangeDiff < -tp->tp_uwr_med_z_2_min &&
Charles MacNeill 7:1add29d51e72 2362 RangeDiff > -tp->tp_uwr_med_z_2_max &&
Charles MacNeill 7:1add29d51e72 2363 sequency == 1) {
Charles MacNeill 7:1add29d51e72 2364 AddOffset =
Charles MacNeill 7:1add29d51e72 2365 tp->tp_uwr_med_corr_z_2_rangeb;
Charles MacNeill 7:1add29d51e72 2366 }
Charles MacNeill 7:1add29d51e72 2367 else
Charles MacNeill 7:1add29d51e72 2368 if (RangeDiff > tp->tp_uwr_med_z_3_min &&
Charles MacNeill 7:1add29d51e72 2369 RangeDiff < tp->tp_uwr_med_z_3_max &&
Charles MacNeill 7:1add29d51e72 2370 sequency == 1) {
Charles MacNeill 7:1add29d51e72 2371 AddOffset =
Charles MacNeill 7:1add29d51e72 2372 tp->tp_uwr_med_corr_z_3_rangeb;
Charles MacNeill 7:1add29d51e72 2373 }
Charles MacNeill 7:1add29d51e72 2374 else
Charles MacNeill 7:1add29d51e72 2375 if (RangeDiff < -tp->tp_uwr_med_z_3_min &&
Charles MacNeill 7:1add29d51e72 2376 RangeDiff > -tp->tp_uwr_med_z_3_max &&
Charles MacNeill 7:1add29d51e72 2377 sequency == 0) {
Charles MacNeill 7:1add29d51e72 2378 AddOffset =
Charles MacNeill 7:1add29d51e72 2379 tp->tp_uwr_med_corr_z_3_rangea;
Charles MacNeill 7:1add29d51e72 2380 }
Charles MacNeill 7:1add29d51e72 2381 else
Charles MacNeill 7:1add29d51e72 2382 if (RangeDiff > tp->tp_uwr_med_z_4_min &&
Charles MacNeill 7:1add29d51e72 2383 RangeDiff < tp->tp_uwr_med_z_4_max &&
Charles MacNeill 7:1add29d51e72 2384 sequency == 0) {
Charles MacNeill 7:1add29d51e72 2385 AddOffset =
Charles MacNeill 7:1add29d51e72 2386 tp->tp_uwr_med_corr_z_4_rangea;
Charles MacNeill 7:1add29d51e72 2387 }
Charles MacNeill 7:1add29d51e72 2388 else
Charles MacNeill 7:1add29d51e72 2389 if (RangeDiff < -tp->tp_uwr_med_z_4_min &&
Charles MacNeill 7:1add29d51e72 2390 RangeDiff > -tp->tp_uwr_med_z_4_max &&
Charles MacNeill 7:1add29d51e72 2391 sequency == 1) {
Charles MacNeill 7:1add29d51e72 2392 AddOffset =
Charles MacNeill 7:1add29d51e72 2393 tp->tp_uwr_med_corr_z_4_rangeb;
Charles MacNeill 7:1add29d51e72 2394 }
Charles MacNeill 7:1add29d51e72 2395 else
Charles MacNeill 7:1add29d51e72 2396 if (RangeDiff < tp->tp_uwr_med_z_5_max &&
Charles MacNeill 7:1add29d51e72 2397 RangeDiff > tp->tp_uwr_med_z_5_min) {
Charles MacNeill 7:1add29d51e72 2398 AddOffset =
Charles MacNeill 7:1add29d51e72 2399 tp->tp_uwr_med_corr_z_5_rangea;
Charles MacNeill 7:1add29d51e72 2400 }
Charles MacNeill 7:1add29d51e72 2401 else
Charles MacNeill 7:1add29d51e72 2402 if (RangeDiff > tp->tp_uwr_med_z_6_min &&
Charles MacNeill 7:1add29d51e72 2403 RangeDiff < tp->tp_uwr_med_z_6_max &&
Charles MacNeill 7:1add29d51e72 2404 sequency == 1) {
Charles MacNeill 7:1add29d51e72 2405 AddOffset =
Charles MacNeill 7:1add29d51e72 2406 tp->tp_uwr_med_corr_z_6_rangeb;
Charles MacNeill 7:1add29d51e72 2407 }
Charles MacNeill 7:1add29d51e72 2408 else
Charles MacNeill 7:1add29d51e72 2409 if (RangeDiff < -tp->tp_uwr_med_z_6_min &&
Charles MacNeill 7:1add29d51e72 2410 RangeDiff > -tp->tp_uwr_med_z_6_max &&
Charles MacNeill 7:1add29d51e72 2411 sequency == 0) {
Charles MacNeill 7:1add29d51e72 2412 AddOffset =
Charles MacNeill 7:1add29d51e72 2413 tp->tp_uwr_med_corr_z_6_rangea;
Charles MacNeill 7:1add29d51e72 2414 }
Charles MacNeill 7:1add29d51e72 2415 else
Charles MacNeill 7:1add29d51e72 2416
Charles MacNeill 7:1add29d51e72 2417
Charles MacNeill 7:1add29d51e72 2418
Charles MacNeill 7:1add29d51e72 2419
Charles MacNeill 7:1add29d51e72 2420
Charles MacNeill 7:1add29d51e72 2421
Charles MacNeill 7:1add29d51e72 2422
Charles MacNeill 7:1add29d51e72 2423
Charles MacNeill 7:1add29d51e72 2424
Charles MacNeill 7:1add29d51e72 2425
Charles MacNeill 7:1add29d51e72 2426
Charles MacNeill 7:1add29d51e72 2427 uwr_status = 0;
Charles MacNeill 7:1add29d51e72 2428 break;
Charles MacNeill 7:1add29d51e72 2429
Charles MacNeill 7:1add29d51e72 2430 case VL53L1_DEVICEPRESETMODE_HISTOGRAM_LONG_RANGE:
Charles MacNeill 7:1add29d51e72 2431 if (RangeDiff > tp->tp_uwr_lng_z_1_min &&
Charles MacNeill 7:1add29d51e72 2432 RangeDiff < tp->tp_uwr_lng_z_1_max &&
Charles MacNeill 7:1add29d51e72 2433 sequency == 0) {
Charles MacNeill 7:1add29d51e72 2434 AddOffset =
Charles MacNeill 7:1add29d51e72 2435 tp->tp_uwr_lng_corr_z_1_rangea;
Charles MacNeill 7:1add29d51e72 2436 }
Charles MacNeill 7:1add29d51e72 2437 else
Charles MacNeill 7:1add29d51e72 2438 if (RangeDiff < -tp->tp_uwr_lng_z_1_min &&
Charles MacNeill 7:1add29d51e72 2439 RangeDiff > -tp->tp_uwr_lng_z_1_max &&
Charles MacNeill 7:1add29d51e72 2440 sequency == 1) {
Charles MacNeill 7:1add29d51e72 2441 AddOffset =
Charles MacNeill 7:1add29d51e72 2442 tp->tp_uwr_lng_corr_z_1_rangeb;
Charles MacNeill 7:1add29d51e72 2443 }
Charles MacNeill 7:1add29d51e72 2444 else
Charles MacNeill 7:1add29d51e72 2445 if (RangeDiff > tp->tp_uwr_lng_z_2_min &&
Charles MacNeill 7:1add29d51e72 2446 RangeDiff < tp->tp_uwr_lng_z_2_max &&
Charles MacNeill 7:1add29d51e72 2447 sequency == 1) {
Charles MacNeill 7:1add29d51e72 2448 AddOffset =
Charles MacNeill 7:1add29d51e72 2449 tp->tp_uwr_lng_corr_z_2_rangeb;
Charles MacNeill 7:1add29d51e72 2450 }
Charles MacNeill 7:1add29d51e72 2451 else
Charles MacNeill 7:1add29d51e72 2452 if (RangeDiff < -tp->tp_uwr_lng_z_2_min &&
Charles MacNeill 7:1add29d51e72 2453 RangeDiff > -tp->tp_uwr_lng_z_2_max &&
Charles MacNeill 7:1add29d51e72 2454 sequency == 0) {
Charles MacNeill 7:1add29d51e72 2455 AddOffset =
Charles MacNeill 7:1add29d51e72 2456 tp->tp_uwr_lng_corr_z_2_rangea;
Charles MacNeill 7:1add29d51e72 2457 }
Charles MacNeill 7:1add29d51e72 2458 else
Charles MacNeill 7:1add29d51e72 2459 if (RangeDiff < tp->tp_uwr_lng_z_3_max &&
Charles MacNeill 7:1add29d51e72 2460 RangeDiff > tp->tp_uwr_lng_z_3_min) {
Charles MacNeill 7:1add29d51e72 2461 AddOffset =
Charles MacNeill 7:1add29d51e72 2462 tp->tp_uwr_lng_corr_z_3_rangea;
Charles MacNeill 7:1add29d51e72 2463 }
Charles MacNeill 7:1add29d51e72 2464 else
Charles MacNeill 7:1add29d51e72 2465 if (RangeDiff > tp->tp_uwr_lng_z_4_min &&
Charles MacNeill 7:1add29d51e72 2466 RangeDiff < tp->tp_uwr_lng_z_4_max &&
Charles MacNeill 7:1add29d51e72 2467 sequency == 1) {
Charles MacNeill 7:1add29d51e72 2468 AddOffset =
Charles MacNeill 7:1add29d51e72 2469 tp->tp_uwr_lng_corr_z_4_rangeb;
Charles MacNeill 7:1add29d51e72 2470 }
Charles MacNeill 7:1add29d51e72 2471 else
Charles MacNeill 7:1add29d51e72 2472 if (RangeDiff < -tp->tp_uwr_lng_z_4_min &&
Charles MacNeill 7:1add29d51e72 2473 RangeDiff > -tp->tp_uwr_lng_z_4_max &&
Charles MacNeill 7:1add29d51e72 2474 sequency == 0) {
Charles MacNeill 7:1add29d51e72 2475 AddOffset =
Charles MacNeill 7:1add29d51e72 2476 tp->tp_uwr_lng_corr_z_4_rangea;
Charles MacNeill 7:1add29d51e72 2477 }
Charles MacNeill 7:1add29d51e72 2478 else
Charles MacNeill 7:1add29d51e72 2479 uwr_status = 0;
Charles MacNeill 7:1add29d51e72 2480 break;
Charles MacNeill 7:1add29d51e72 2481
Charles MacNeill 7:1add29d51e72 2482 default:
Charles MacNeill 7:1add29d51e72 2483 uwr_status = 0;
Charles MacNeill 7:1add29d51e72 2484 break;
Charles MacNeill 7:1add29d51e72 2485 }
Charles MacNeill 7:1add29d51e72 2486 }
Charles MacNeill 7:1add29d51e72 2487
Charles MacNeill 7:1add29d51e72 2488 if (uwr_status) {
Charles MacNeill 7:1add29d51e72 2489 pRangeData->RangeMilliMeter += AddOffset;
Charles MacNeill 7:1add29d51e72 2490 pRangeData->RangeMinMilliMeter += AddOffset;
Charles MacNeill 7:1add29d51e72 2491 pRangeData->RangeMaxMilliMeter += AddOffset;
Charles MacNeill 7:1add29d51e72 2492 pRangeData->ExtendedRange = 1;
Charles MacNeill 7:1add29d51e72 2493 pRangeData->RangeStatus = 0;
Charles MacNeill 7:1add29d51e72 2494 }
Charles MacNeill 7:1add29d51e72 2495
Charles MacNeill 7:1add29d51e72 2496 }
Charles MacNeill 7:1add29d51e72 2497
Charles MacNeill 7:1add29d51e72 2498 pdev->PreviousRangeMilliMeter[iteration] = RangeMillimeterInit;
Charles MacNeill 7:1add29d51e72 2499 pdev->PreviousRangeStatus[iteration] = pRangeData->RangeStatus;
Charles MacNeill 7:1add29d51e72 2500 pdev->PreviousExtendedRange[iteration] = pRangeData->ExtendedRange;
Charles MacNeill 7:1add29d51e72 2501
Charles MacNeill 7:1add29d51e72 2502 return Status;
Charles MacNeill 7:1add29d51e72 2503 }
Charles MacNeill 7:1add29d51e72 2504
Charles MacNeill 7:1add29d51e72 2505 static uint8_t GetOutputDataIndex(VL53L1_DEV Dev,
Charles MacNeill 7:1add29d51e72 2506 VL53L1_range_results_t *presults)
Charles MacNeill 7:1add29d51e72 2507 {
Charles MacNeill 7:1add29d51e72 2508 uint8_t i;
Charles MacNeill 7:1add29d51e72 2509 uint8_t index = 0;
Charles MacNeill 7:1add29d51e72 2510 VL53L1_OutputModes OutputMode;
Charles MacNeill 7:1add29d51e72 2511
Charles MacNeill 7:1add29d51e72 2512 OutputMode = VL53L1DevDataGet(Dev, CurrentParameters.OutputMode);
Charles MacNeill 7:1add29d51e72 2513
Charles MacNeill 7:1add29d51e72 2514
Charles MacNeill 7:1add29d51e72 2515 if (OutputMode == VL53L1_OUTPUTMODE_NEAREST)
Charles MacNeill 7:1add29d51e72 2516 return 0;
Charles MacNeill 7:1add29d51e72 2517
Charles MacNeill 7:1add29d51e72 2518
Charles MacNeill 7:1add29d51e72 2519 for (i = 1; i < presults->active_results; i++) {
Charles MacNeill 7:1add29d51e72 2520 if (presults->VL53L1_p_002[i].peak_signal_count_rate_mcps >
Charles MacNeill 7:1add29d51e72 2521 presults->VL53L1_p_002[index].peak_signal_count_rate_mcps)
Charles MacNeill 7:1add29d51e72 2522 index = i;
Charles MacNeill 7:1add29d51e72 2523 }
Charles MacNeill 7:1add29d51e72 2524
Charles MacNeill 7:1add29d51e72 2525 return index;
Charles MacNeill 7:1add29d51e72 2526 }
Charles MacNeill 7:1add29d51e72 2527
Charles MacNeill 7:1add29d51e72 2528 VL53L1_Error VL53L1_GetRangingMeasurementData(VL53L1_DEV Dev,
Charles MacNeill 7:1add29d51e72 2529 VL53L1_RangingMeasurementData_t *pRangingMeasurementData)
Charles MacNeill 7:1add29d51e72 2530 {
Charles MacNeill 7:1add29d51e72 2531 VL53L1_Error Status = VL53L1_ERROR_NONE;
Charles MacNeill 7:1add29d51e72 2532 VL53L1_LLDriverData_t *pdev =
Charles MacNeill 7:1add29d51e72 2533 VL53L1DevStructGetLLDriverHandle(Dev);
Charles MacNeill 7:1add29d51e72 2534 VL53L1_range_results_t *presults =
Charles MacNeill 7:1add29d51e72 2535 (VL53L1_range_results_t *) pdev->wArea1;
Charles MacNeill 7:1add29d51e72 2536 VL53L1_range_data_t *presults_data;
Charles MacNeill 7:1add29d51e72 2537 VL53L1_PresetModes PresetMode;
Charles MacNeill 7:1add29d51e72 2538 uint8_t index = 0;
Charles MacNeill 7:1add29d51e72 2539
Charles MacNeill 7:1add29d51e72 2540 LOG_FUNCTION_START("");
Charles MacNeill 7:1add29d51e72 2541
Charles MacNeill 7:1add29d51e72 2542
Charles MacNeill 7:1add29d51e72 2543 PresetMode = VL53L1DevDataGet(Dev, CurrentParameters.PresetMode);
Charles MacNeill 7:1add29d51e72 2544
Charles MacNeill 7:1add29d51e72 2545 if (PresetMode == VL53L1_PRESETMODE_MULTIZONES_SCANNING) {
Charles MacNeill 7:1add29d51e72 2546 Status = VL53L1_ERROR_MODE_NOT_SUPPORTED;
Charles MacNeill 7:1add29d51e72 2547 LOG_FUNCTION_END(Status);
Charles MacNeill 7:1add29d51e72 2548 return Status;
Charles MacNeill 7:1add29d51e72 2549 }
Charles MacNeill 7:1add29d51e72 2550
Charles MacNeill 7:1add29d51e72 2551
Charles MacNeill 7:1add29d51e72 2552 memset(pRangingMeasurementData, 0xFF,
Charles MacNeill 7:1add29d51e72 2553 sizeof(VL53L1_RangingMeasurementData_t));
Charles MacNeill 7:1add29d51e72 2554
Charles MacNeill 7:1add29d51e72 2555
Charles MacNeill 7:1add29d51e72 2556 Status = VL53L1_get_device_results(
Charles MacNeill 7:1add29d51e72 2557 Dev,
Charles MacNeill 7:1add29d51e72 2558 VL53L1_DEVICERESULTSLEVEL_FULL,
Charles MacNeill 7:1add29d51e72 2559 presults);
Charles MacNeill 7:1add29d51e72 2560
Charles MacNeill 7:1add29d51e72 2561 if (Status == VL53L1_ERROR_NONE) {
Charles MacNeill 7:1add29d51e72 2562 pRangingMeasurementData->StreamCount = presults->stream_count;
Charles MacNeill 7:1add29d51e72 2563
Charles MacNeill 7:1add29d51e72 2564
Charles MacNeill 7:1add29d51e72 2565 index = GetOutputDataIndex(Dev, presults);
Charles MacNeill 7:1add29d51e72 2566 presults_data = &(presults->VL53L1_p_002[index]);
Charles MacNeill 7:1add29d51e72 2567 Status = SetSimpleData(Dev, presults->active_results,
Charles MacNeill 7:1add29d51e72 2568 presults->device_status,
Charles MacNeill 7:1add29d51e72 2569 presults_data,
Charles MacNeill 7:1add29d51e72 2570 pRangingMeasurementData);
Charles MacNeill 7:1add29d51e72 2571 }
Charles MacNeill 7:1add29d51e72 2572
Charles MacNeill 7:1add29d51e72 2573 LOG_FUNCTION_END(Status);
Charles MacNeill 7:1add29d51e72 2574 return Status;
Charles MacNeill 7:1add29d51e72 2575 }
Charles MacNeill 7:1add29d51e72 2576
Charles MacNeill 7:1add29d51e72 2577 static VL53L1_Error SetMeasurementData(VL53L1_DEV Dev,
Charles MacNeill 7:1add29d51e72 2578 VL53L1_range_results_t *presults,
Charles MacNeill 7:1add29d51e72 2579 VL53L1_MultiRangingData_t *pMultiRangingData)
Charles MacNeill 7:1add29d51e72 2580 {
Charles MacNeill 7:1add29d51e72 2581 VL53L1_LLDriverData_t *pdev = VL53L1DevStructGetLLDriverHandle(Dev);
Charles MacNeill 7:1add29d51e72 2582 uint8_t i;
Charles MacNeill 7:1add29d51e72 2583 uint8_t iteration;
Charles MacNeill 7:1add29d51e72 2584 VL53L1_TargetRangeData_t *pRangeData;
Charles MacNeill 7:1add29d51e72 2585 VL53L1_range_data_t *presults_data;
Charles MacNeill 7:1add29d51e72 2586 int16_t dmax_min;
Charles MacNeill 7:1add29d51e72 2587 VL53L1_Error Status = VL53L1_ERROR_NONE;
Charles MacNeill 7:1add29d51e72 2588 uint8_t Furthest_idx = 0;
Charles MacNeill 7:1add29d51e72 2589 int16_t Furthest_range = 0;
Charles MacNeill 7:1add29d51e72 2590 uint8_t ActiveResults, amb_idx;
Charles MacNeill 7:1add29d51e72 2591
Charles MacNeill 7:1add29d51e72 2592 pMultiRangingData->NumberOfObjectsFound = presults->active_results;
Charles MacNeill 7:1add29d51e72 2593 pMultiRangingData->RoiNumber = presults->zone_id;
Charles MacNeill 7:1add29d51e72 2594 pMultiRangingData->HasXtalkValueChanged =
Charles MacNeill 7:1add29d51e72 2595 presults->smudge_corrector_data.new_xtalk_applied_flag;
Charles MacNeill 7:1add29d51e72 2596 dmax_min = MIN(presults->wrap_dmax_mm,
Charles MacNeill 7:1add29d51e72 2597 presults->VL53L1_p_007[DMAX_REFLECTANCE_IDX]);
Charles MacNeill 7:1add29d51e72 2598 pMultiRangingData->DmaxMilliMeter = dmax_min;
Charles MacNeill 7:1add29d51e72 2599
Charles MacNeill 7:1add29d51e72 2600
Charles MacNeill 7:1add29d51e72 2601 pMultiRangingData->TimeStamp = 0;
Charles MacNeill 7:1add29d51e72 2602
Charles MacNeill 7:1add29d51e72 2603 pMultiRangingData->StreamCount = presults->stream_count;
Charles MacNeill 7:1add29d51e72 2604
Charles MacNeill 7:1add29d51e72 2605 pMultiRangingData->RecommendedDistanceMode =
Charles MacNeill 7:1add29d51e72 2606 VL53L1DevDataGet(Dev, CurrentParameters.DistanceMode);
Charles MacNeill 7:1add29d51e72 2607 ActiveResults = presults->active_results;
Charles MacNeill 7:1add29d51e72 2608 if (ActiveResults < 1)
Charles MacNeill 7:1add29d51e72 2609
Charles MacNeill 7:1add29d51e72 2610 iteration = 1;
Charles MacNeill 7:1add29d51e72 2611 else
Charles MacNeill 7:1add29d51e72 2612 iteration = ActiveResults;
Charles MacNeill 7:1add29d51e72 2613 for (i = 0; i < iteration; i++) {
Charles MacNeill 7:1add29d51e72 2614 pRangeData = &(pMultiRangingData->RangeData[i]);
Charles MacNeill 7:1add29d51e72 2615
Charles MacNeill 7:1add29d51e72 2616 presults_data = &(presults->VL53L1_p_002[i]);
Charles MacNeill 7:1add29d51e72 2617 if (Status == VL53L1_ERROR_NONE)
Charles MacNeill 7:1add29d51e72 2618 Status = SetTargetData(Dev, ActiveResults,
Charles MacNeill 7:1add29d51e72 2619 pMultiRangingData->StreamCount,
Charles MacNeill 7:1add29d51e72 2620 i,
Charles MacNeill 7:1add29d51e72 2621 presults->device_status,
Charles MacNeill 7:1add29d51e72 2622 presults_data,
Charles MacNeill 7:1add29d51e72 2623 pRangeData);
Charles MacNeill 7:1add29d51e72 2624
Charles MacNeill 7:1add29d51e72 2625 pMultiRangingData->EffectiveSpadRtnCount =
Charles MacNeill 7:1add29d51e72 2626 presults_data->VL53L1_p_006;
Charles MacNeill 7:1add29d51e72 2627
Charles MacNeill 7:1add29d51e72 2628 if ((pRangeData->RangeStatus == VL53L1_RANGESTATUS_RANGE_VALID)
Charles MacNeill 7:1add29d51e72 2629 && (pRangeData->RangeMilliMeter > Furthest_range)) {
Charles MacNeill 7:1add29d51e72 2630 Furthest_range = pRangeData->RangeMilliMeter;
Charles MacNeill 7:1add29d51e72 2631 Furthest_idx = i;
Charles MacNeill 7:1add29d51e72 2632 }
Charles MacNeill 7:1add29d51e72 2633 }
Charles MacNeill 7:1add29d51e72 2634 pdev->PreviousStreamCount = pdev->hist_data.result__stream_count;
Charles MacNeill 7:1add29d51e72 2635 for (i = iteration; i < VL53L1_MAX_RANGE_RESULTS; i++) {
Charles MacNeill 7:1add29d51e72 2636 pdev->PreviousRangeMilliMeter[i] = 0;
Charles MacNeill 7:1add29d51e72 2637 pdev->PreviousRangeStatus[i] = 255;
Charles MacNeill 7:1add29d51e72 2638 pdev->PreviousExtendedRange[i] = 0;
Charles MacNeill 7:1add29d51e72 2639 }
Charles MacNeill 7:1add29d51e72 2640
Charles MacNeill 7:1add29d51e72 2641 if ((Status == VL53L1_ERROR_NONE) && (ActiveResults > 0)) {
Charles MacNeill 7:1add29d51e72 2642 pRangeData = &(pMultiRangingData->RangeData[Furthest_idx]);
Charles MacNeill 7:1add29d51e72 2643 amb_idx = VL53L1_MAX_AMBIENT_DMAX_VALUES-1;
Charles MacNeill 7:1add29d51e72 2644 CheckAndChangeDistanceMode(Dev, pRangeData,
Charles MacNeill 7:1add29d51e72 2645 presults->VL53L1_p_007[amb_idx],
Charles MacNeill 7:1add29d51e72 2646 &pMultiRangingData->RecommendedDistanceMode);
Charles MacNeill 7:1add29d51e72 2647 }
Charles MacNeill 7:1add29d51e72 2648
Charles MacNeill 7:1add29d51e72 2649 return Status;
Charles MacNeill 7:1add29d51e72 2650 }
Charles MacNeill 7:1add29d51e72 2651
Charles MacNeill 7:1add29d51e72 2652 VL53L1_Error VL53L1_GetMultiRangingData(VL53L1_DEV Dev,
Charles MacNeill 7:1add29d51e72 2653 VL53L1_MultiRangingData_t *pMultiRangingData)
Charles MacNeill 7:1add29d51e72 2654 {
Charles MacNeill 7:1add29d51e72 2655 VL53L1_Error Status = VL53L1_ERROR_NONE;
Charles MacNeill 7:1add29d51e72 2656 VL53L1_LLDriverData_t *pdev =
Charles MacNeill 7:1add29d51e72 2657 VL53L1DevStructGetLLDriverHandle(Dev);
Charles MacNeill 7:1add29d51e72 2658 VL53L1_range_results_t *presults =
Charles MacNeill 7:1add29d51e72 2659 (VL53L1_range_results_t *) pdev->wArea1;
Charles MacNeill 7:1add29d51e72 2660
Charles MacNeill 7:1add29d51e72 2661 LOG_FUNCTION_START("");
Charles MacNeill 7:1add29d51e72 2662
Charles MacNeill 7:1add29d51e72 2663
Charles MacNeill 7:1add29d51e72 2664 memset(pMultiRangingData, 0xFF,
Charles MacNeill 7:1add29d51e72 2665 sizeof(VL53L1_MultiRangingData_t));
Charles MacNeill 7:1add29d51e72 2666
Charles MacNeill 7:1add29d51e72 2667
Charles MacNeill 7:1add29d51e72 2668 Status = VL53L1_get_device_results(
Charles MacNeill 7:1add29d51e72 2669 Dev,
Charles MacNeill 7:1add29d51e72 2670 VL53L1_DEVICERESULTSLEVEL_FULL,
Charles MacNeill 7:1add29d51e72 2671 presults);
Charles MacNeill 7:1add29d51e72 2672
Charles MacNeill 7:1add29d51e72 2673
Charles MacNeill 7:1add29d51e72 2674 if (Status == VL53L1_ERROR_NONE) {
Charles MacNeill 7:1add29d51e72 2675 switch (presults->rd_device_state) {
Charles MacNeill 7:1add29d51e72 2676 case VL53L1_DEVICESTATE_RANGING_GATHER_DATA:
Charles MacNeill 7:1add29d51e72 2677 pMultiRangingData->RoiStatus =
Charles MacNeill 7:1add29d51e72 2678 VL53L1_ROISTATUS_VALID_NOT_LAST;
Charles MacNeill 7:1add29d51e72 2679 break;
Charles MacNeill 7:1add29d51e72 2680 case VL53L1_DEVICESTATE_RANGING_OUTPUT_DATA:
Charles MacNeill 7:1add29d51e72 2681 pMultiRangingData->RoiStatus =
Charles MacNeill 7:1add29d51e72 2682 VL53L1_ROISTATUS_VALID_LAST;
Charles MacNeill 7:1add29d51e72 2683 break;
Charles MacNeill 7:1add29d51e72 2684 default:
Charles MacNeill 7:1add29d51e72 2685 pMultiRangingData->RoiStatus =
Charles MacNeill 7:1add29d51e72 2686 VL53L1_ROISTATUS_NOT_VALID;
Charles MacNeill 7:1add29d51e72 2687 }
Charles MacNeill 7:1add29d51e72 2688
Charles MacNeill 7:1add29d51e72 2689 Status = SetMeasurementData(Dev,
Charles MacNeill 7:1add29d51e72 2690 presults,
Charles MacNeill 7:1add29d51e72 2691 pMultiRangingData);
Charles MacNeill 7:1add29d51e72 2692
Charles MacNeill 7:1add29d51e72 2693 }
Charles MacNeill 7:1add29d51e72 2694
Charles MacNeill 7:1add29d51e72 2695 LOG_FUNCTION_END(Status);
Charles MacNeill 7:1add29d51e72 2696 return Status;
Charles MacNeill 7:1add29d51e72 2697 }
Charles MacNeill 7:1add29d51e72 2698
Charles MacNeill 7:1add29d51e72 2699 VL53L1_Error VL53L1_GetAdditionalData(VL53L1_DEV Dev,
Charles MacNeill 7:1add29d51e72 2700 VL53L1_AdditionalData_t *pAdditionalData)
Charles MacNeill 7:1add29d51e72 2701 {
Charles MacNeill 7:1add29d51e72 2702 VL53L1_Error Status = VL53L1_ERROR_NONE;
Charles MacNeill 7:1add29d51e72 2703
Charles MacNeill 7:1add29d51e72 2704 LOG_FUNCTION_START("");
Charles MacNeill 7:1add29d51e72 2705
Charles MacNeill 7:1add29d51e72 2706 Status = VL53L1_get_additional_data(Dev, pAdditionalData);
Charles MacNeill 7:1add29d51e72 2707
Charles MacNeill 7:1add29d51e72 2708 LOG_FUNCTION_END(Status);
Charles MacNeill 7:1add29d51e72 2709 return Status;
Charles MacNeill 7:1add29d51e72 2710 }
Charles MacNeill 7:1add29d51e72 2711
Charles MacNeill 7:1add29d51e72 2712
Charles MacNeill 7:1add29d51e72 2713
Charles MacNeill 7:1add29d51e72 2714
Charles MacNeill 7:1add29d51e72 2715
Charles MacNeill 7:1add29d51e72 2716
Charles MacNeill 7:1add29d51e72 2717 VL53L1_Error VL53L1_SetTuningParameter(VL53L1_DEV Dev,
Charles MacNeill 7:1add29d51e72 2718 uint16_t TuningParameterId, int32_t TuningParameterValue)
Charles MacNeill 7:1add29d51e72 2719 {
Charles MacNeill 7:1add29d51e72 2720 VL53L1_Error Status = VL53L1_ERROR_NONE;
Charles MacNeill 7:1add29d51e72 2721
Charles MacNeill 7:1add29d51e72 2722 LOG_FUNCTION_START("");
Charles MacNeill 7:1add29d51e72 2723
Charles MacNeill 7:1add29d51e72 2724 if (TuningParameterId ==
Charles MacNeill 7:1add29d51e72 2725 VL53L1_TUNINGPARM_DYNXTALK_NODETECT_XTALK_OFFSET_KCPS)
Charles MacNeill 7:1add29d51e72 2726 return VL53L1_ERROR_INVALID_PARAMS;
Charles MacNeill 7:1add29d51e72 2727
Charles MacNeill 7:1add29d51e72 2728 if (TuningParameterId >= 32768)
Charles MacNeill 7:1add29d51e72 2729 Status = VL53L1_set_tuning_parm(Dev,
Charles MacNeill 7:1add29d51e72 2730 TuningParameterId,
Charles MacNeill 7:1add29d51e72 2731 TuningParameterValue);
Charles MacNeill 7:1add29d51e72 2732 else {
Charles MacNeill 7:1add29d51e72 2733 if (TuningParameterId < VL53L1_TUNING_MAX_TUNABLE_KEY)
Charles MacNeill 7:1add29d51e72 2734 BDTable[TuningParameterId] = TuningParameterValue;
Charles MacNeill 7:1add29d51e72 2735 else
Charles MacNeill 7:1add29d51e72 2736 Status = VL53L1_ERROR_INVALID_PARAMS;
Charles MacNeill 7:1add29d51e72 2737 }
Charles MacNeill 7:1add29d51e72 2738
Charles MacNeill 7:1add29d51e72 2739 LOG_FUNCTION_END(Status);
Charles MacNeill 7:1add29d51e72 2740 return Status;
Charles MacNeill 7:1add29d51e72 2741 }
Charles MacNeill 7:1add29d51e72 2742
Charles MacNeill 7:1add29d51e72 2743 VL53L1_Error VL53L1_GetTuningParameter(VL53L1_DEV Dev,
Charles MacNeill 7:1add29d51e72 2744 uint16_t TuningParameterId, int32_t *pTuningParameterValue)
Charles MacNeill 7:1add29d51e72 2745 {
Charles MacNeill 7:1add29d51e72 2746 VL53L1_Error Status = VL53L1_ERROR_NONE;
Charles MacNeill 7:1add29d51e72 2747
Charles MacNeill 7:1add29d51e72 2748 LOG_FUNCTION_START("");
Charles MacNeill 7:1add29d51e72 2749
Charles MacNeill 7:1add29d51e72 2750 if (TuningParameterId >= 32768)
Charles MacNeill 7:1add29d51e72 2751 Status = VL53L1_get_tuning_parm(Dev,
Charles MacNeill 7:1add29d51e72 2752 TuningParameterId,
Charles MacNeill 7:1add29d51e72 2753 pTuningParameterValue);
Charles MacNeill 7:1add29d51e72 2754 else {
Charles MacNeill 7:1add29d51e72 2755 if (TuningParameterId < VL53L1_TUNING_MAX_TUNABLE_KEY)
Charles MacNeill 7:1add29d51e72 2756 *pTuningParameterValue = BDTable[TuningParameterId];
Charles MacNeill 7:1add29d51e72 2757 else
Charles MacNeill 7:1add29d51e72 2758 Status = VL53L1_ERROR_INVALID_PARAMS;
Charles MacNeill 7:1add29d51e72 2759 }
Charles MacNeill 7:1add29d51e72 2760
Charles MacNeill 7:1add29d51e72 2761 LOG_FUNCTION_END(Status);
Charles MacNeill 7:1add29d51e72 2762 return Status;
Charles MacNeill 7:1add29d51e72 2763 }
Charles MacNeill 7:1add29d51e72 2764
Charles MacNeill 7:1add29d51e72 2765
Charles MacNeill 7:1add29d51e72 2766 VL53L1_Error VL53L1_PerformRefSpadManagement(VL53L1_DEV Dev)
Charles MacNeill 7:1add29d51e72 2767 {
Charles MacNeill 7:1add29d51e72 2768 #ifdef VL53L1_NOCALIB
Charles MacNeill 7:1add29d51e72 2769 VL53L1_Error Status = VL53L1_ERROR_NOT_SUPPORTED;
Charles MacNeill 7:1add29d51e72 2770
Charles MacNeill 7:1add29d51e72 2771 SUPPRESS_UNUSED_WARNING(Dev);
Charles MacNeill 7:1add29d51e72 2772
Charles MacNeill 7:1add29d51e72 2773 LOG_FUNCTION_START("");
Charles MacNeill 7:1add29d51e72 2774 #else
Charles MacNeill 7:1add29d51e72 2775 VL53L1_Error Status = VL53L1_ERROR_NONE;
Charles MacNeill 7:1add29d51e72 2776 VL53L1_Error RawStatus;
Charles MacNeill 7:1add29d51e72 2777 uint8_t dcrbuffer[24];
Charles MacNeill 7:1add29d51e72 2778 uint8_t *commbuf;
Charles MacNeill 7:1add29d51e72 2779 uint8_t numloc[2] = {5, 3};
Charles MacNeill 7:1add29d51e72 2780 VL53L1_LLDriverData_t *pdev;
Charles MacNeill 7:1add29d51e72 2781 VL53L1_customer_nvm_managed_t *pc;
Charles MacNeill 7:1add29d51e72 2782 VL53L1_PresetModes PresetMode;
Charles MacNeill 7:1add29d51e72 2783
Charles MacNeill 7:1add29d51e72 2784 LOG_FUNCTION_START("");
Charles MacNeill 7:1add29d51e72 2785
Charles MacNeill 7:1add29d51e72 2786 pdev = VL53L1DevStructGetLLDriverHandle(Dev);
Charles MacNeill 7:1add29d51e72 2787 pc = &pdev->customer;
Charles MacNeill 7:1add29d51e72 2788
Charles MacNeill 7:1add29d51e72 2789 if (Status == VL53L1_ERROR_NONE) {
Charles MacNeill 7:1add29d51e72 2790 PresetMode = VL53L1DevDataGet(Dev,
Charles MacNeill 7:1add29d51e72 2791 CurrentParameters.PresetMode);
Charles MacNeill 7:1add29d51e72 2792 Status = VL53L1_run_ref_spad_char(Dev, &RawStatus);
Charles MacNeill 7:1add29d51e72 2793
Charles MacNeill 7:1add29d51e72 2794 if (Status == VL53L1_ERROR_NONE)
Charles MacNeill 7:1add29d51e72 2795 Status = VL53L1_SetPresetMode(Dev, PresetMode);
Charles MacNeill 7:1add29d51e72 2796 }
Charles MacNeill 7:1add29d51e72 2797
Charles MacNeill 7:1add29d51e72 2798 if (Status == VL53L1_WARNING_REF_SPAD_CHAR_RATE_TOO_HIGH) {
Charles MacNeill 7:1add29d51e72 2799
Charles MacNeill 7:1add29d51e72 2800 Status = VL53L1_read_nvm_raw_data(Dev,
Charles MacNeill 7:1add29d51e72 2801 (uint8_t)(0xA0 >> 2),
Charles MacNeill 7:1add29d51e72 2802 (uint8_t)(24 >> 2),
Charles MacNeill 7:1add29d51e72 2803 dcrbuffer);
Charles MacNeill 7:1add29d51e72 2804
Charles MacNeill 7:1add29d51e72 2805 if (Status == VL53L1_ERROR_NONE)
Charles MacNeill 7:1add29d51e72 2806 Status = VL53L1_WriteMulti(Dev,
Charles MacNeill 7:1add29d51e72 2807 VL53L1_REF_SPAD_MAN__NUM_REQUESTED_REF_SPADS,
Charles MacNeill 7:1add29d51e72 2808 numloc, 2);
Charles MacNeill 7:1add29d51e72 2809
Charles MacNeill 7:1add29d51e72 2810 if (Status == VL53L1_ERROR_NONE) {
Charles MacNeill 7:1add29d51e72 2811 pc->ref_spad_man__num_requested_ref_spads = numloc[0];
Charles MacNeill 7:1add29d51e72 2812 pc->ref_spad_man__ref_location = numloc[1];
Charles MacNeill 7:1add29d51e72 2813 }
Charles MacNeill 7:1add29d51e72 2814
Charles MacNeill 7:1add29d51e72 2815 if (Status == VL53L1_ERROR_NONE)
Charles MacNeill 7:1add29d51e72 2816 commbuf = &dcrbuffer[16];
Charles MacNeill 7:1add29d51e72 2817
Charles MacNeill 7:1add29d51e72 2818
Charles MacNeill 7:1add29d51e72 2819
Charles MacNeill 7:1add29d51e72 2820 if (Status == VL53L1_ERROR_NONE)
Charles MacNeill 7:1add29d51e72 2821 Status = VL53L1_WriteMulti(Dev,
Charles MacNeill 7:1add29d51e72 2822 VL53L1_GLOBAL_CONFIG__SPAD_ENABLES_REF_0,
Charles MacNeill 7:1add29d51e72 2823 commbuf, 6);
Charles MacNeill 7:1add29d51e72 2824
Charles MacNeill 7:1add29d51e72 2825 if (Status == VL53L1_ERROR_NONE) {
Charles MacNeill 7:1add29d51e72 2826 pc->global_config__spad_enables_ref_0 = commbuf[0];
Charles MacNeill 7:1add29d51e72 2827 pc->global_config__spad_enables_ref_1 = commbuf[1];
Charles MacNeill 7:1add29d51e72 2828 pc->global_config__spad_enables_ref_2 = commbuf[2];
Charles MacNeill 7:1add29d51e72 2829 pc->global_config__spad_enables_ref_3 = commbuf[3];
Charles MacNeill 7:1add29d51e72 2830 pc->global_config__spad_enables_ref_4 = commbuf[4];
Charles MacNeill 7:1add29d51e72 2831 pc->global_config__spad_enables_ref_5 = commbuf[5];
Charles MacNeill 7:1add29d51e72 2832 }
Charles MacNeill 7:1add29d51e72 2833
Charles MacNeill 7:1add29d51e72 2834 }
Charles MacNeill 7:1add29d51e72 2835
Charles MacNeill 7:1add29d51e72 2836 #endif
Charles MacNeill 7:1add29d51e72 2837
Charles MacNeill 7:1add29d51e72 2838 LOG_FUNCTION_END(Status);
Charles MacNeill 7:1add29d51e72 2839 return Status;
Charles MacNeill 7:1add29d51e72 2840 }
Charles MacNeill 7:1add29d51e72 2841
Charles MacNeill 7:1add29d51e72 2842 VL53L1_Error VL53L1_SmudgeCorrectionEnable(VL53L1_DEV Dev,
Charles MacNeill 7:1add29d51e72 2843 VL53L1_SmudgeCorrectionModes Mode)
Charles MacNeill 7:1add29d51e72 2844 {
Charles MacNeill 7:1add29d51e72 2845 VL53L1_Error Status = VL53L1_ERROR_NONE;
Charles MacNeill 7:1add29d51e72 2846 VL53L1_Error s1 = VL53L1_ERROR_NONE;
Charles MacNeill 7:1add29d51e72 2847 VL53L1_Error s2 = VL53L1_ERROR_NONE;
Charles MacNeill 7:1add29d51e72 2848 VL53L1_Error s3 = VL53L1_ERROR_NONE;
Charles MacNeill 7:1add29d51e72 2849
Charles MacNeill 7:1add29d51e72 2850 LOG_FUNCTION_START("");
Charles MacNeill 7:1add29d51e72 2851
Charles MacNeill 7:1add29d51e72 2852 switch (Mode) {
Charles MacNeill 7:1add29d51e72 2853 case VL53L1_SMUDGE_CORRECTION_NONE:
Charles MacNeill 7:1add29d51e72 2854 s1 = VL53L1_dynamic_xtalk_correction_disable(Dev);
Charles MacNeill 7:1add29d51e72 2855 s2 = VL53L1_dynamic_xtalk_correction_apply_disable(Dev);
Charles MacNeill 7:1add29d51e72 2856 s3 = VL53L1_dynamic_xtalk_correction_single_apply_disable(Dev);
Charles MacNeill 7:1add29d51e72 2857 break;
Charles MacNeill 7:1add29d51e72 2858 case VL53L1_SMUDGE_CORRECTION_CONTINUOUS:
Charles MacNeill 7:1add29d51e72 2859 s1 = VL53L1_dynamic_xtalk_correction_enable(Dev);
Charles MacNeill 7:1add29d51e72 2860 s2 = VL53L1_dynamic_xtalk_correction_apply_enable(Dev);
Charles MacNeill 7:1add29d51e72 2861 s3 = VL53L1_dynamic_xtalk_correction_single_apply_disable(Dev);
Charles MacNeill 7:1add29d51e72 2862 break;
Charles MacNeill 7:1add29d51e72 2863 case VL53L1_SMUDGE_CORRECTION_SINGLE:
Charles MacNeill 7:1add29d51e72 2864 s1 = VL53L1_dynamic_xtalk_correction_enable(Dev);
Charles MacNeill 7:1add29d51e72 2865 s2 = VL53L1_dynamic_xtalk_correction_apply_enable(Dev);
Charles MacNeill 7:1add29d51e72 2866 s3 = VL53L1_dynamic_xtalk_correction_single_apply_enable(Dev);
Charles MacNeill 7:1add29d51e72 2867 break;
Charles MacNeill 7:1add29d51e72 2868 case VL53L1_SMUDGE_CORRECTION_DEBUG:
Charles MacNeill 7:1add29d51e72 2869 s1 = VL53L1_dynamic_xtalk_correction_enable(Dev);
Charles MacNeill 7:1add29d51e72 2870 s2 = VL53L1_dynamic_xtalk_correction_apply_disable(Dev);
Charles MacNeill 7:1add29d51e72 2871 s3 = VL53L1_dynamic_xtalk_correction_single_apply_disable(Dev);
Charles MacNeill 7:1add29d51e72 2872 break;
Charles MacNeill 7:1add29d51e72 2873 default:
Charles MacNeill 7:1add29d51e72 2874 Status = VL53L1_ERROR_INVALID_PARAMS;
Charles MacNeill 7:1add29d51e72 2875 break;
Charles MacNeill 7:1add29d51e72 2876 }
Charles MacNeill 7:1add29d51e72 2877
Charles MacNeill 7:1add29d51e72 2878 if (Status == VL53L1_ERROR_NONE) {
Charles MacNeill 7:1add29d51e72 2879 Status = s1;
Charles MacNeill 7:1add29d51e72 2880 if (Status == VL53L1_ERROR_NONE)
Charles MacNeill 7:1add29d51e72 2881 Status = s2;
Charles MacNeill 7:1add29d51e72 2882 if (Status == VL53L1_ERROR_NONE)
Charles MacNeill 7:1add29d51e72 2883 Status = s3;
Charles MacNeill 7:1add29d51e72 2884 }
Charles MacNeill 7:1add29d51e72 2885
Charles MacNeill 7:1add29d51e72 2886 LOG_FUNCTION_END(Status);
Charles MacNeill 7:1add29d51e72 2887 return Status;
Charles MacNeill 7:1add29d51e72 2888 }
Charles MacNeill 7:1add29d51e72 2889
Charles MacNeill 7:1add29d51e72 2890 VL53L1_Error VL53L1_SetXTalkCompensationEnable(VL53L1_DEV Dev,
Charles MacNeill 7:1add29d51e72 2891 uint8_t XTalkCompensationEnable)
Charles MacNeill 7:1add29d51e72 2892 {
Charles MacNeill 7:1add29d51e72 2893 VL53L1_Error Status = VL53L1_ERROR_NONE;
Charles MacNeill 7:1add29d51e72 2894
Charles MacNeill 7:1add29d51e72 2895 LOG_FUNCTION_START("");
Charles MacNeill 7:1add29d51e72 2896
Charles MacNeill 7:1add29d51e72 2897 if (XTalkCompensationEnable == 0)
Charles MacNeill 7:1add29d51e72 2898 Status = VL53L1_disable_xtalk_compensation(Dev);
Charles MacNeill 7:1add29d51e72 2899 else
Charles MacNeill 7:1add29d51e72 2900 Status = VL53L1_enable_xtalk_compensation(Dev);
Charles MacNeill 7:1add29d51e72 2901
Charles MacNeill 7:1add29d51e72 2902 LOG_FUNCTION_END(Status);
Charles MacNeill 7:1add29d51e72 2903 return Status;
Charles MacNeill 7:1add29d51e72 2904 }
Charles MacNeill 7:1add29d51e72 2905
Charles MacNeill 7:1add29d51e72 2906
Charles MacNeill 7:1add29d51e72 2907 VL53L1_Error VL53L1_GetXTalkCompensationEnable(VL53L1_DEV Dev,
Charles MacNeill 7:1add29d51e72 2908 uint8_t *pXTalkCompensationEnable)
Charles MacNeill 7:1add29d51e72 2909 {
Charles MacNeill 7:1add29d51e72 2910 VL53L1_Error Status = VL53L1_ERROR_NONE;
Charles MacNeill 7:1add29d51e72 2911
Charles MacNeill 7:1add29d51e72 2912 LOG_FUNCTION_START("");
Charles MacNeill 7:1add29d51e72 2913
Charles MacNeill 7:1add29d51e72 2914 VL53L1_get_xtalk_compensation_enable(
Charles MacNeill 7:1add29d51e72 2915 Dev,
Charles MacNeill 7:1add29d51e72 2916 pXTalkCompensationEnable);
Charles MacNeill 7:1add29d51e72 2917
Charles MacNeill 7:1add29d51e72 2918 LOG_FUNCTION_END(Status);
Charles MacNeill 7:1add29d51e72 2919 return Status;
Charles MacNeill 7:1add29d51e72 2920 }
Charles MacNeill 7:1add29d51e72 2921
Charles MacNeill 7:1add29d51e72 2922
Charles MacNeill 7:1add29d51e72 2923 VL53L1_Error VL53L1_PerformXTalkCalibration(VL53L1_DEV Dev,
Charles MacNeill 7:1add29d51e72 2924 uint8_t CalibrationOption)
Charles MacNeill 7:1add29d51e72 2925 {
Charles MacNeill 7:1add29d51e72 2926 VL53L1_Error Status = VL53L1_ERROR_NONE;
Charles MacNeill 7:1add29d51e72 2927 VL53L1_Error UStatus;
Charles MacNeill 7:1add29d51e72 2928 int16_t CalDistanceMm;
Charles MacNeill 7:1add29d51e72 2929 VL53L1_xtalk_calibration_results_t xtalk;
Charles MacNeill 7:1add29d51e72 2930
Charles MacNeill 7:1add29d51e72 2931 VL53L1_CalibrationData_t caldata;
Charles MacNeill 7:1add29d51e72 2932 VL53L1_LLDriverData_t *pLLData;
Charles MacNeill 7:1add29d51e72 2933 int i;
Charles MacNeill 7:1add29d51e72 2934 uint32_t *pPlaneOffsetKcps;
Charles MacNeill 7:1add29d51e72 2935 uint32_t Margin =
Charles MacNeill 7:1add29d51e72 2936 BDTable[VL53L1_TUNING_XTALK_FULL_ROI_BIN_SUM_MARGIN];
Charles MacNeill 7:1add29d51e72 2937 uint32_t DefaultOffset =
Charles MacNeill 7:1add29d51e72 2938 BDTable[VL53L1_TUNING_XTALK_FULL_ROI_DEFAULT_OFFSET];
Charles MacNeill 7:1add29d51e72 2939 uint32_t *pLLDataPlaneOffsetKcps;
Charles MacNeill 7:1add29d51e72 2940 uint32_t sum = 0;
Charles MacNeill 7:1add29d51e72 2941 uint8_t binok = 0;
Charles MacNeill 7:1add29d51e72 2942 int32_t merge;
Charles MacNeill 7:1add29d51e72 2943
Charles MacNeill 7:1add29d51e72 2944 LOG_FUNCTION_START("");
Charles MacNeill 7:1add29d51e72 2945
Charles MacNeill 7:1add29d51e72 2946 pPlaneOffsetKcps =
Charles MacNeill 7:1add29d51e72 2947 &caldata.customer.algo__crosstalk_compensation_plane_offset_kcps;
Charles MacNeill 7:1add29d51e72 2948 pLLData = VL53L1DevStructGetLLDriverHandle(Dev);
Charles MacNeill 7:1add29d51e72 2949 pLLDataPlaneOffsetKcps =
Charles MacNeill 7:1add29d51e72 2950 &pLLData->xtalk_cal.algo__crosstalk_compensation_plane_offset_kcps;
Charles MacNeill 7:1add29d51e72 2951 VL53L1_get_tuning_parm(Dev, VL53L1_TUNINGPARM_HIST_MERGE, &merge);
Charles MacNeill 7:1add29d51e72 2952
Charles MacNeill 7:1add29d51e72 2953 VL53L1_set_tuning_parm(Dev, VL53L1_TUNINGPARM_HIST_MERGE, 0);
Charles MacNeill 7:1add29d51e72 2954 switch (CalibrationOption) {
Charles MacNeill 7:1add29d51e72 2955 case VL53L1_XTALKCALIBRATIONMODE_NO_TARGET:
Charles MacNeill 7:1add29d51e72 2956 Status = VL53L1_run_xtalk_extraction(Dev, &UStatus);
Charles MacNeill 7:1add29d51e72 2957
Charles MacNeill 7:1add29d51e72 2958 if (Status == VL53L1_ERROR_XTALK_EXTRACTION_NO_SAMPLE_FAIL)
Charles MacNeill 7:1add29d51e72 2959 VL53L1_xtalk_cal_data_init(Dev);
Charles MacNeill 7:1add29d51e72 2960 break;
Charles MacNeill 7:1add29d51e72 2961 case VL53L1_XTALKCALIBRATIONMODE_SINGLE_TARGET:
Charles MacNeill 7:1add29d51e72 2962 Status = SingleTargetXTalkCalibration(Dev);
Charles MacNeill 7:1add29d51e72 2963 break;
Charles MacNeill 7:1add29d51e72 2964 case VL53L1_XTALKCALIBRATIONMODE_FULL_ROI:
Charles MacNeill 7:1add29d51e72 2965 CalDistanceMm = (int16_t)
Charles MacNeill 7:1add29d51e72 2966 BDTable[VL53L1_TUNING_XTALK_FULL_ROI_TARGET_DISTANCE_MM];
Charles MacNeill 7:1add29d51e72 2967
Charles MacNeill 7:1add29d51e72 2968 VL53L1_set_tuning_parm(Dev, VL53L1_TUNINGPARM_HIST_MERGE,
Charles MacNeill 7:1add29d51e72 2969 merge);
Charles MacNeill 7:1add29d51e72 2970 Status = VL53L1_run_hist_xtalk_extraction(Dev, CalDistanceMm,
Charles MacNeill 7:1add29d51e72 2971 &UStatus);
Charles MacNeill 7:1add29d51e72 2972
Charles MacNeill 7:1add29d51e72 2973 VL53L1_GetCalibrationData(Dev, &caldata);
Charles MacNeill 7:1add29d51e72 2974 for (i = 0; i < VL53L1_XTALK_HISTO_BINS; i++) {
Charles MacNeill 7:1add29d51e72 2975 sum += caldata.xtalkhisto.xtalk_shape.bin_data[i];
Charles MacNeill 7:1add29d51e72 2976 if (caldata.xtalkhisto.xtalk_shape.bin_data[i] > 0)
Charles MacNeill 7:1add29d51e72 2977 binok++;
Charles MacNeill 7:1add29d51e72 2978 }
Charles MacNeill 7:1add29d51e72 2979 if ((UStatus ==
Charles MacNeill 7:1add29d51e72 2980 VL53L1_ERROR_XTALK_EXTRACTION_SIGMA_LIMIT_FAIL) ||
Charles MacNeill 7:1add29d51e72 2981 (sum > (1024 + Margin)) || (sum < (1024 - Margin)) ||
Charles MacNeill 7:1add29d51e72 2982 (binok < 3)) {
Charles MacNeill 7:1add29d51e72 2983 *pPlaneOffsetKcps = DefaultOffset;
Charles MacNeill 7:1add29d51e72 2984 *pLLDataPlaneOffsetKcps = DefaultOffset;
Charles MacNeill 7:1add29d51e72 2985 caldata.xtalkhisto.xtalk_shape.bin_data[0] = 307;
Charles MacNeill 7:1add29d51e72 2986 caldata.xtalkhisto.xtalk_shape.bin_data[1] = 410;
Charles MacNeill 7:1add29d51e72 2987 caldata.xtalkhisto.xtalk_shape.bin_data[2] = 410;
Charles MacNeill 7:1add29d51e72 2988 caldata.xtalkhisto.xtalk_shape.bin_data[3] = 307;
Charles MacNeill 7:1add29d51e72 2989 for (i = 4; i < VL53L1_XTALK_HISTO_BINS; i++)
Charles MacNeill 7:1add29d51e72 2990 caldata.xtalkhisto.xtalk_shape.bin_data[i] = 0;
Charles MacNeill 7:1add29d51e72 2991 for (i = 0; i < VL53L1_BIN_REC_SIZE; i++)
Charles MacNeill 7:1add29d51e72 2992 caldata.algo__xtalk_cpo_HistoMerge_kcps[i] =
Charles MacNeill 7:1add29d51e72 2993 DefaultOffset + DefaultOffset * i;
Charles MacNeill 7:1add29d51e72 2994 VL53L1_SetCalibrationData(Dev, &caldata);
Charles MacNeill 7:1add29d51e72 2995 }
Charles MacNeill 7:1add29d51e72 2996
Charles MacNeill 7:1add29d51e72 2997 break;
Charles MacNeill 7:1add29d51e72 2998 default:
Charles MacNeill 7:1add29d51e72 2999 Status = VL53L1_ERROR_INVALID_PARAMS;
Charles MacNeill 7:1add29d51e72 3000 }
Charles MacNeill 7:1add29d51e72 3001 VL53L1_set_tuning_parm(Dev, VL53L1_TUNINGPARM_HIST_MERGE, merge);
Charles MacNeill 7:1add29d51e72 3002
Charles MacNeill 7:1add29d51e72 3003 if (Status == VL53L1_ERROR_NONE) {
Charles MacNeill 7:1add29d51e72 3004 Status = VL53L1_get_current_xtalk_settings(Dev, &xtalk);
Charles MacNeill 7:1add29d51e72 3005 Status = VL53L1_set_tuning_parm(Dev,
Charles MacNeill 7:1add29d51e72 3006 VL53L1_TUNINGPARM_DYNXTALK_NODETECT_XTALK_OFFSET_KCPS,
Charles MacNeill 7:1add29d51e72 3007 xtalk.algo__crosstalk_compensation_plane_offset_kcps);
Charles MacNeill 7:1add29d51e72 3008 }
Charles MacNeill 7:1add29d51e72 3009
Charles MacNeill 7:1add29d51e72 3010 LOG_FUNCTION_END(Status);
Charles MacNeill 7:1add29d51e72 3011 return Status;
Charles MacNeill 7:1add29d51e72 3012 }
Charles MacNeill 7:1add29d51e72 3013
Charles MacNeill 7:1add29d51e72 3014 VL53L1_Error VL53L1_SetOffsetCalibrationMode(VL53L1_DEV Dev,
Charles MacNeill 7:1add29d51e72 3015 VL53L1_OffsetCalibrationModes OffsetCalibrationMode)
Charles MacNeill 7:1add29d51e72 3016 {
Charles MacNeill 7:1add29d51e72 3017 VL53L1_Error Status = VL53L1_ERROR_NONE;
Charles MacNeill 7:1add29d51e72 3018 VL53L1_OffsetCalibrationMode offset_cal_mode;
Charles MacNeill 7:1add29d51e72 3019
Charles MacNeill 7:1add29d51e72 3020 LOG_FUNCTION_START("");
Charles MacNeill 7:1add29d51e72 3021
Charles MacNeill 7:1add29d51e72 3022 if (OffsetCalibrationMode == VL53L1_OFFSETCALIBRATIONMODE_STANDARD) {
Charles MacNeill 7:1add29d51e72 3023 offset_cal_mode =
Charles MacNeill 7:1add29d51e72 3024 VL53L1_OFFSETCALIBRATIONMODE__MM1_MM2__STANDARD;
Charles MacNeill 7:1add29d51e72 3025 } else if (OffsetCalibrationMode ==
Charles MacNeill 7:1add29d51e72 3026 VL53L1_OFFSETCALIBRATIONMODE_PRERANGE_ONLY) {
Charles MacNeill 7:1add29d51e72 3027 offset_cal_mode =
Charles MacNeill 7:1add29d51e72 3028 VL53L1_OFFSETCALIBRATIONMODE__MM1_MM2__STANDARD_PRE_RANGE_ONLY;
Charles MacNeill 7:1add29d51e72 3029 } else if (OffsetCalibrationMode ==
Charles MacNeill 7:1add29d51e72 3030 VL53L1_OFFSETCALIBRATIONMODE_MULTI_ZONE) {
Charles MacNeill 7:1add29d51e72 3031 offset_cal_mode =
Charles MacNeill 7:1add29d51e72 3032 VL53L1_OFFSETCALIBRATIONMODE__PER_ZONE;
Charles MacNeill 7:1add29d51e72 3033 } else {
Charles MacNeill 7:1add29d51e72 3034 Status = VL53L1_ERROR_INVALID_PARAMS;
Charles MacNeill 7:1add29d51e72 3035 }
Charles MacNeill 7:1add29d51e72 3036
Charles MacNeill 7:1add29d51e72 3037 if (Status == VL53L1_ERROR_NONE)
Charles MacNeill 7:1add29d51e72 3038 Status = VL53L1_set_offset_calibration_mode(Dev,
Charles MacNeill 7:1add29d51e72 3039 offset_cal_mode);
Charles MacNeill 7:1add29d51e72 3040
Charles MacNeill 7:1add29d51e72 3041 LOG_FUNCTION_END(Status);
Charles MacNeill 7:1add29d51e72 3042 return Status;
Charles MacNeill 7:1add29d51e72 3043 }
Charles MacNeill 7:1add29d51e72 3044
Charles MacNeill 7:1add29d51e72 3045 VL53L1_Error VL53L1_SetOffsetCorrectionMode(VL53L1_DEV Dev,
Charles MacNeill 7:1add29d51e72 3046 VL53L1_OffsetCorrectionModes OffsetCorrectionMode)
Charles MacNeill 7:1add29d51e72 3047 {
Charles MacNeill 7:1add29d51e72 3048 VL53L1_Error Status = VL53L1_ERROR_NONE;
Charles MacNeill 7:1add29d51e72 3049 VL53L1_OffsetCorrectionMode offset_cor_mode;
Charles MacNeill 7:1add29d51e72 3050
Charles MacNeill 7:1add29d51e72 3051 LOG_FUNCTION_START("");
Charles MacNeill 7:1add29d51e72 3052
Charles MacNeill 7:1add29d51e72 3053 if (OffsetCorrectionMode == VL53L1_OFFSETCORRECTIONMODE_STANDARD) {
Charles MacNeill 7:1add29d51e72 3054 offset_cor_mode =
Charles MacNeill 7:1add29d51e72 3055 VL53L1_OFFSETCORRECTIONMODE__MM1_MM2_OFFSETS;
Charles MacNeill 7:1add29d51e72 3056 } else if (OffsetCorrectionMode ==
Charles MacNeill 7:1add29d51e72 3057 VL53L1_OFFSETCORRECTIONMODE_PERZONE) {
Charles MacNeill 7:1add29d51e72 3058 offset_cor_mode =
Charles MacNeill 7:1add29d51e72 3059 VL53L1_OFFSETCORRECTIONMODE__PER_ZONE_OFFSETS;
Charles MacNeill 7:1add29d51e72 3060 } else if (OffsetCorrectionMode ==
Charles MacNeill 7:1add29d51e72 3061 VL53L1_OFFSETCORRECTIONMODE_PERVCSEL) {
Charles MacNeill 7:1add29d51e72 3062 offset_cor_mode =
Charles MacNeill 7:1add29d51e72 3063 VL53L1_OFFSETCORRECTIONMODE__PER_VCSEL_OFFSETS;
Charles MacNeill 7:1add29d51e72 3064 } else {
Charles MacNeill 7:1add29d51e72 3065 Status = VL53L1_ERROR_INVALID_PARAMS;
Charles MacNeill 7:1add29d51e72 3066 }
Charles MacNeill 7:1add29d51e72 3067
Charles MacNeill 7:1add29d51e72 3068 if (Status == VL53L1_ERROR_NONE)
Charles MacNeill 7:1add29d51e72 3069 Status = VL53L1_set_offset_correction_mode(Dev,
Charles MacNeill 7:1add29d51e72 3070 offset_cor_mode);
Charles MacNeill 7:1add29d51e72 3071
Charles MacNeill 7:1add29d51e72 3072 LOG_FUNCTION_END(Status);
Charles MacNeill 7:1add29d51e72 3073 return Status;
Charles MacNeill 7:1add29d51e72 3074 }
Charles MacNeill 7:1add29d51e72 3075
Charles MacNeill 7:1add29d51e72 3076 VL53L1_Error VL53L1_PerformOffsetCalibration(VL53L1_DEV Dev,
Charles MacNeill 7:1add29d51e72 3077 int32_t CalDistanceMilliMeter, FixPoint1616_t CalReflectancePercent)
Charles MacNeill 7:1add29d51e72 3078 {
Charles MacNeill 7:1add29d51e72 3079 VL53L1_Error Status = VL53L1_ERROR_NONE;
Charles MacNeill 7:1add29d51e72 3080 VL53L1_Error UnfilteredStatus;
Charles MacNeill 7:1add29d51e72 3081 VL53L1_OffsetCalibrationMode offset_cal_mode;
Charles MacNeill 7:1add29d51e72 3082 uint16_t CalReflectancePercent_int;
Charles MacNeill 7:1add29d51e72 3083
Charles MacNeill 7:1add29d51e72 3084 VL53L1_DevicePresetModes device_preset_mode;
Charles MacNeill 7:1add29d51e72 3085 VL53L1_DeviceZonePreset zone_preset;
Charles MacNeill 7:1add29d51e72 3086 VL53L1_zone_config_t zone_cfg;
Charles MacNeill 7:1add29d51e72 3087 int32_t MergeEnabled;
Charles MacNeill 7:1add29d51e72 3088
Charles MacNeill 7:1add29d51e72 3089 LOG_FUNCTION_START("");
Charles MacNeill 7:1add29d51e72 3090
Charles MacNeill 7:1add29d51e72 3091 CalReflectancePercent_int =
Charles MacNeill 7:1add29d51e72 3092 VL53L1_FIXPOINT1616TOFIXPOINT72(CalReflectancePercent);
Charles MacNeill 7:1add29d51e72 3093
Charles MacNeill 7:1add29d51e72 3094 if (Status == VL53L1_ERROR_NONE)
Charles MacNeill 7:1add29d51e72 3095 Status = VL53L1_get_offset_calibration_mode(Dev,
Charles MacNeill 7:1add29d51e72 3096 &offset_cal_mode);
Charles MacNeill 7:1add29d51e72 3097
Charles MacNeill 7:1add29d51e72 3098 if (Status != VL53L1_ERROR_NONE) {
Charles MacNeill 7:1add29d51e72 3099 LOG_FUNCTION_END(Status);
Charles MacNeill 7:1add29d51e72 3100 return Status;
Charles MacNeill 7:1add29d51e72 3101 }
Charles MacNeill 7:1add29d51e72 3102
Charles MacNeill 7:1add29d51e72 3103
Charles MacNeill 7:1add29d51e72 3104 if ((offset_cal_mode ==
Charles MacNeill 7:1add29d51e72 3105 VL53L1_OFFSETCALIBRATIONMODE__MM1_MM2__STANDARD) ||
Charles MacNeill 7:1add29d51e72 3106 (offset_cal_mode ==
Charles MacNeill 7:1add29d51e72 3107 VL53L1_OFFSETCALIBRATIONMODE__MM1_MM2__STANDARD_PRE_RANGE_ONLY
Charles MacNeill 7:1add29d51e72 3108 )) {
Charles MacNeill 7:1add29d51e72 3109 if (Status == VL53L1_ERROR_NONE)
Charles MacNeill 7:1add29d51e72 3110 Status = VL53L1_run_offset_calibration(
Charles MacNeill 7:1add29d51e72 3111 Dev,
Charles MacNeill 7:1add29d51e72 3112 (int16_t)CalDistanceMilliMeter,
Charles MacNeill 7:1add29d51e72 3113 CalReflectancePercent_int,
Charles MacNeill 7:1add29d51e72 3114 &UnfilteredStatus);
Charles MacNeill 7:1add29d51e72 3115
Charles MacNeill 7:1add29d51e72 3116 } else if (offset_cal_mode ==
Charles MacNeill 7:1add29d51e72 3117 VL53L1_OFFSETCALIBRATIONMODE__PER_ZONE) {
Charles MacNeill 7:1add29d51e72 3118 VL53L1_get_tuning_parm(Dev, VL53L1_TUNINGPARM_HIST_MERGE,
Charles MacNeill 7:1add29d51e72 3119 &MergeEnabled);
Charles MacNeill 7:1add29d51e72 3120 VL53L1_set_tuning_parm(Dev, VL53L1_TUNINGPARM_HIST_MERGE, 0);
Charles MacNeill 7:1add29d51e72 3121 device_preset_mode =
Charles MacNeill 7:1add29d51e72 3122 VL53L1_DEVICEPRESETMODE_HISTOGRAM_MULTIZONE_LONG_RANGE;
Charles MacNeill 7:1add29d51e72 3123 zone_preset = VL53L1_DEVICEZONEPRESET_CUSTOM;
Charles MacNeill 7:1add29d51e72 3124
Charles MacNeill 7:1add29d51e72 3125 Status = VL53L1_get_zone_config(Dev, &zone_cfg);
Charles MacNeill 7:1add29d51e72 3126 if (Status == VL53L1_ERROR_NONE)
Charles MacNeill 7:1add29d51e72 3127 Status = VL53L1_run_zone_calibration(
Charles MacNeill 7:1add29d51e72 3128 Dev,
Charles MacNeill 7:1add29d51e72 3129 device_preset_mode,
Charles MacNeill 7:1add29d51e72 3130 zone_preset,
Charles MacNeill 7:1add29d51e72 3131 &zone_cfg,
Charles MacNeill 7:1add29d51e72 3132 (int16_t)CalDistanceMilliMeter,
Charles MacNeill 7:1add29d51e72 3133 CalReflectancePercent_int,
Charles MacNeill 7:1add29d51e72 3134 &UnfilteredStatus);
Charles MacNeill 7:1add29d51e72 3135 VL53L1_set_tuning_parm(Dev, VL53L1_TUNINGPARM_HIST_MERGE,
Charles MacNeill 7:1add29d51e72 3136 MergeEnabled);
Charles MacNeill 7:1add29d51e72 3137
Charles MacNeill 7:1add29d51e72 3138 } else {
Charles MacNeill 7:1add29d51e72 3139 Status = VL53L1_ERROR_INVALID_PARAMS;
Charles MacNeill 7:1add29d51e72 3140 }
Charles MacNeill 7:1add29d51e72 3141 LOG_FUNCTION_END(Status);
Charles MacNeill 7:1add29d51e72 3142 return Status;
Charles MacNeill 7:1add29d51e72 3143 }
Charles MacNeill 7:1add29d51e72 3144
Charles MacNeill 7:1add29d51e72 3145 VL53L1_Error VL53L1_PerformOffsetSimpleCalibration(VL53L1_DEV Dev,
Charles MacNeill 7:1add29d51e72 3146 int32_t CalDistanceMilliMeter)
Charles MacNeill 7:1add29d51e72 3147 {
Charles MacNeill 7:1add29d51e72 3148 VL53L1_Error Status = VL53L1_ERROR_NONE;
Charles MacNeill 7:1add29d51e72 3149 int32_t sum_ranging;
Charles MacNeill 7:1add29d51e72 3150 uint8_t offset_meas;
Charles MacNeill 7:1add29d51e72 3151 int16_t Max, UnderMax, OverMax, Repeat;
Charles MacNeill 7:1add29d51e72 3152 int32_t total_count, inloopcount;
Charles MacNeill 7:1add29d51e72 3153 int32_t IncRounding;
Charles MacNeill 7:1add29d51e72 3154 int16_t meanDistance_mm;
Charles MacNeill 7:1add29d51e72 3155 int16_t offset;
Charles MacNeill 7:1add29d51e72 3156 VL53L1_RangingMeasurementData_t RangingMeasurementData;
Charles MacNeill 7:1add29d51e72 3157 VL53L1_LLDriverData_t *pdev;
Charles MacNeill 7:1add29d51e72 3158 uint8_t goodmeas;
Charles MacNeill 7:1add29d51e72 3159 VL53L1_Error SmudgeStatus = VL53L1_ERROR_NONE;
Charles MacNeill 7:1add29d51e72 3160 uint8_t smudge_corr_en;
Charles MacNeill 7:1add29d51e72 3161
Charles MacNeill 7:1add29d51e72 3162 LOG_FUNCTION_START("");
Charles MacNeill 7:1add29d51e72 3163
Charles MacNeill 7:1add29d51e72 3164 pdev = VL53L1DevStructGetLLDriverHandle(Dev);
Charles MacNeill 7:1add29d51e72 3165
Charles MacNeill 7:1add29d51e72 3166 smudge_corr_en = pdev->smudge_correct_config.smudge_corr_enabled;
Charles MacNeill 7:1add29d51e72 3167 SmudgeStatus = VL53L1_dynamic_xtalk_correction_disable(Dev);
Charles MacNeill 7:1add29d51e72 3168
Charles MacNeill 7:1add29d51e72 3169 pdev->customer.algo__part_to_part_range_offset_mm = 0;
Charles MacNeill 7:1add29d51e72 3170 pdev->customer.mm_config__inner_offset_mm = 0;
Charles MacNeill 7:1add29d51e72 3171 pdev->customer.mm_config__outer_offset_mm = 0;
Charles MacNeill 7:1add29d51e72 3172 memset(&pdev->per_vcsel_cal_data, 0, sizeof(pdev->per_vcsel_cal_data));
Charles MacNeill 7:1add29d51e72 3173 Repeat = BDTable[VL53L1_TUNING_SIMPLE_OFFSET_CALIBRATION_REPEAT];
Charles MacNeill 7:1add29d51e72 3174 Max = BDTable[
Charles MacNeill 7:1add29d51e72 3175 VL53L1_TUNING_MAX_SIMPLE_OFFSET_CALIBRATION_SAMPLE_NUMBER];
Charles MacNeill 7:1add29d51e72 3176 UnderMax = 1 + (Max / 2);
Charles MacNeill 7:1add29d51e72 3177 OverMax = Max + (Max / 2);
Charles MacNeill 7:1add29d51e72 3178 sum_ranging = 0;
Charles MacNeill 7:1add29d51e72 3179 total_count = 0;
Charles MacNeill 7:1add29d51e72 3180
Charles MacNeill 7:1add29d51e72 3181 while ((Repeat > 0) && (Status == VL53L1_ERROR_NONE)) {
Charles MacNeill 7:1add29d51e72 3182 Status = VL53L1_StartMeasurement(Dev);
Charles MacNeill 7:1add29d51e72 3183
Charles MacNeill 7:1add29d51e72 3184 if (Status == VL53L1_ERROR_NONE) {
Charles MacNeill 7:1add29d51e72 3185 VL53L1_WaitMeasurementDataReady(Dev);
Charles MacNeill 7:1add29d51e72 3186 VL53L1_GetRangingMeasurementData(Dev,
Charles MacNeill 7:1add29d51e72 3187 &RangingMeasurementData);
Charles MacNeill 7:1add29d51e72 3188 VL53L1_ClearInterruptAndStartMeasurement(Dev);
Charles MacNeill 7:1add29d51e72 3189 }
Charles MacNeill 7:1add29d51e72 3190
Charles MacNeill 7:1add29d51e72 3191 inloopcount = 0;
Charles MacNeill 7:1add29d51e72 3192 offset_meas = 0;
Charles MacNeill 7:1add29d51e72 3193 while ((Status == VL53L1_ERROR_NONE) && (inloopcount < Max) &&
Charles MacNeill 7:1add29d51e72 3194 (offset_meas < OverMax)) {
Charles MacNeill 7:1add29d51e72 3195 Status = VL53L1_WaitMeasurementDataReady(Dev);
Charles MacNeill 7:1add29d51e72 3196 if (Status == VL53L1_ERROR_NONE)
Charles MacNeill 7:1add29d51e72 3197 Status = VL53L1_GetRangingMeasurementData(Dev,
Charles MacNeill 7:1add29d51e72 3198 &RangingMeasurementData);
Charles MacNeill 7:1add29d51e72 3199 goodmeas = (RangingMeasurementData.RangeStatus ==
Charles MacNeill 7:1add29d51e72 3200 VL53L1_RANGESTATUS_RANGE_VALID);
Charles MacNeill 7:1add29d51e72 3201 if ((Status == VL53L1_ERROR_NONE) && goodmeas) {
Charles MacNeill 7:1add29d51e72 3202 sum_ranging = sum_ranging +
Charles MacNeill 7:1add29d51e72 3203 RangingMeasurementData.RangeMilliMeter;
Charles MacNeill 7:1add29d51e72 3204 inloopcount++;
Charles MacNeill 7:1add29d51e72 3205 }
Charles MacNeill 7:1add29d51e72 3206 Status = VL53L1_ClearInterruptAndStartMeasurement(Dev);
Charles MacNeill 7:1add29d51e72 3207 offset_meas++;
Charles MacNeill 7:1add29d51e72 3208 }
Charles MacNeill 7:1add29d51e72 3209 total_count += inloopcount;
Charles MacNeill 7:1add29d51e72 3210
Charles MacNeill 7:1add29d51e72 3211
Charles MacNeill 7:1add29d51e72 3212 if (inloopcount < UnderMax)
Charles MacNeill 7:1add29d51e72 3213 Status = VL53L1_ERROR_OFFSET_CAL_NO_SAMPLE_FAIL;
Charles MacNeill 7:1add29d51e72 3214
Charles MacNeill 7:1add29d51e72 3215 VL53L1_StopMeasurement(Dev);
Charles MacNeill 7:1add29d51e72 3216
Charles MacNeill 7:1add29d51e72 3217 Repeat--;
Charles MacNeill 7:1add29d51e72 3218
Charles MacNeill 7:1add29d51e72 3219 }
Charles MacNeill 7:1add29d51e72 3220
Charles MacNeill 7:1add29d51e72 3221 if ((SmudgeStatus == VL53L1_ERROR_NONE) && (smudge_corr_en == 1))
Charles MacNeill 7:1add29d51e72 3222 SmudgeStatus = VL53L1_dynamic_xtalk_correction_enable(Dev);
Charles MacNeill 7:1add29d51e72 3223
Charles MacNeill 7:1add29d51e72 3224 if ((sum_ranging < 0) ||
Charles MacNeill 7:1add29d51e72 3225 (sum_ranging > ((int32_t) total_count * 0xffff)))
Charles MacNeill 7:1add29d51e72 3226 Status = VL53L1_WARNING_OFFSET_CAL_SIGMA_TOO_HIGH;
Charles MacNeill 7:1add29d51e72 3227
Charles MacNeill 7:1add29d51e72 3228 if ((Status == VL53L1_ERROR_NONE) && (total_count > 0)) {
Charles MacNeill 7:1add29d51e72 3229 IncRounding = total_count / 2;
Charles MacNeill 7:1add29d51e72 3230 meanDistance_mm = (int16_t)((sum_ranging + IncRounding)
Charles MacNeill 7:1add29d51e72 3231 / total_count);
Charles MacNeill 7:1add29d51e72 3232 offset = (int16_t)CalDistanceMilliMeter - meanDistance_mm;
Charles MacNeill 7:1add29d51e72 3233 pdev->customer.algo__part_to_part_range_offset_mm = 0;
Charles MacNeill 7:1add29d51e72 3234 pdev->customer.mm_config__inner_offset_mm = offset;
Charles MacNeill 7:1add29d51e72 3235 pdev->customer.mm_config__outer_offset_mm = offset;
Charles MacNeill 7:1add29d51e72 3236
Charles MacNeill 7:1add29d51e72 3237 Status = VL53L1_set_customer_nvm_managed(Dev,
Charles MacNeill 7:1add29d51e72 3238 &(pdev->customer));
Charles MacNeill 7:1add29d51e72 3239 }
Charles MacNeill 7:1add29d51e72 3240
Charles MacNeill 7:1add29d51e72 3241 LOG_FUNCTION_END(Status);
Charles MacNeill 7:1add29d51e72 3242 return Status;
Charles MacNeill 7:1add29d51e72 3243 }
Charles MacNeill 7:1add29d51e72 3244
Charles MacNeill 7:1add29d51e72 3245 VL53L1_Error VL53L1_PerformOffsetZeroDistanceCalibration(VL53L1_DEV Dev)
Charles MacNeill 7:1add29d51e72 3246 {
Charles MacNeill 7:1add29d51e72 3247 #define START_OFFSET 50
Charles MacNeill 7:1add29d51e72 3248 VL53L1_Error Status = VL53L1_ERROR_NONE;
Charles MacNeill 7:1add29d51e72 3249 int32_t sum_ranging;
Charles MacNeill 7:1add29d51e72 3250 uint8_t offset_meas;
Charles MacNeill 7:1add29d51e72 3251 int16_t Max, UnderMax, OverMax, Repeat;
Charles MacNeill 7:1add29d51e72 3252 int32_t total_count, inloopcount;
Charles MacNeill 7:1add29d51e72 3253 int32_t IncRounding;
Charles MacNeill 7:1add29d51e72 3254 int16_t meanDistance_mm;
Charles MacNeill 7:1add29d51e72 3255 int16_t offset, ZeroDistanceOffset;
Charles MacNeill 7:1add29d51e72 3256 VL53L1_RangingMeasurementData_t RangingMeasurementData;
Charles MacNeill 7:1add29d51e72 3257 VL53L1_LLDriverData_t *pdev;
Charles MacNeill 7:1add29d51e72 3258 uint8_t goodmeas;
Charles MacNeill 7:1add29d51e72 3259 VL53L1_Error SmudgeStatus = VL53L1_ERROR_NONE;
Charles MacNeill 7:1add29d51e72 3260 uint8_t smudge_corr_en;
Charles MacNeill 7:1add29d51e72 3261
Charles MacNeill 7:1add29d51e72 3262 LOG_FUNCTION_START("");
Charles MacNeill 7:1add29d51e72 3263
Charles MacNeill 7:1add29d51e72 3264 pdev = VL53L1DevStructGetLLDriverHandle(Dev);
Charles MacNeill 7:1add29d51e72 3265 smudge_corr_en = pdev->smudge_correct_config.smudge_corr_enabled;
Charles MacNeill 7:1add29d51e72 3266 SmudgeStatus = VL53L1_dynamic_xtalk_correction_disable(Dev);
Charles MacNeill 7:1add29d51e72 3267 pdev->customer.algo__part_to_part_range_offset_mm = 0;
Charles MacNeill 7:1add29d51e72 3268 pdev->customer.mm_config__inner_offset_mm = START_OFFSET;
Charles MacNeill 7:1add29d51e72 3269 pdev->customer.mm_config__outer_offset_mm = START_OFFSET;
Charles MacNeill 7:1add29d51e72 3270 memset(&pdev->per_vcsel_cal_data, 0, sizeof(pdev->per_vcsel_cal_data));
Charles MacNeill 7:1add29d51e72 3271 ZeroDistanceOffset = BDTable[
Charles MacNeill 7:1add29d51e72 3272 VL53L1_TUNING_ZERO_DISTANCE_OFFSET_NON_LINEAR_FACTOR];
Charles MacNeill 7:1add29d51e72 3273 Repeat = BDTable[VL53L1_TUNING_SIMPLE_OFFSET_CALIBRATION_REPEAT];
Charles MacNeill 7:1add29d51e72 3274 Max = BDTable[
Charles MacNeill 7:1add29d51e72 3275 VL53L1_TUNING_MAX_SIMPLE_OFFSET_CALIBRATION_SAMPLE_NUMBER];
Charles MacNeill 7:1add29d51e72 3276 UnderMax = 1 + (Max / 2);
Charles MacNeill 7:1add29d51e72 3277 OverMax = Max + (Max / 2);
Charles MacNeill 7:1add29d51e72 3278 sum_ranging = 0;
Charles MacNeill 7:1add29d51e72 3279 total_count = 0;
Charles MacNeill 7:1add29d51e72 3280
Charles MacNeill 7:1add29d51e72 3281 while ((Repeat > 0) && (Status == VL53L1_ERROR_NONE)) {
Charles MacNeill 7:1add29d51e72 3282 Status = VL53L1_StartMeasurement(Dev);
Charles MacNeill 7:1add29d51e72 3283 if (Status == VL53L1_ERROR_NONE) {
Charles MacNeill 7:1add29d51e72 3284 VL53L1_WaitMeasurementDataReady(Dev);
Charles MacNeill 7:1add29d51e72 3285 VL53L1_GetRangingMeasurementData(Dev,
Charles MacNeill 7:1add29d51e72 3286 &RangingMeasurementData);
Charles MacNeill 7:1add29d51e72 3287 VL53L1_ClearInterruptAndStartMeasurement(Dev);
Charles MacNeill 7:1add29d51e72 3288 }
Charles MacNeill 7:1add29d51e72 3289 inloopcount = 0;
Charles MacNeill 7:1add29d51e72 3290 offset_meas = 0;
Charles MacNeill 7:1add29d51e72 3291 while ((Status == VL53L1_ERROR_NONE) && (inloopcount < Max) &&
Charles MacNeill 7:1add29d51e72 3292 (offset_meas < OverMax)) {
Charles MacNeill 7:1add29d51e72 3293 Status = VL53L1_WaitMeasurementDataReady(Dev);
Charles MacNeill 7:1add29d51e72 3294 if (Status == VL53L1_ERROR_NONE)
Charles MacNeill 7:1add29d51e72 3295 Status = VL53L1_GetRangingMeasurementData(Dev,
Charles MacNeill 7:1add29d51e72 3296 &RangingMeasurementData);
Charles MacNeill 7:1add29d51e72 3297 goodmeas = (RangingMeasurementData.RangeStatus ==
Charles MacNeill 7:1add29d51e72 3298 VL53L1_RANGESTATUS_RANGE_VALID);
Charles MacNeill 7:1add29d51e72 3299 if ((Status == VL53L1_ERROR_NONE) && goodmeas) {
Charles MacNeill 7:1add29d51e72 3300 sum_ranging = sum_ranging +
Charles MacNeill 7:1add29d51e72 3301 RangingMeasurementData.RangeMilliMeter;
Charles MacNeill 7:1add29d51e72 3302 inloopcount++;
Charles MacNeill 7:1add29d51e72 3303 }
Charles MacNeill 7:1add29d51e72 3304 Status = VL53L1_ClearInterruptAndStartMeasurement(Dev);
Charles MacNeill 7:1add29d51e72 3305 offset_meas++;
Charles MacNeill 7:1add29d51e72 3306 }
Charles MacNeill 7:1add29d51e72 3307 total_count += inloopcount;
Charles MacNeill 7:1add29d51e72 3308 if (inloopcount < UnderMax)
Charles MacNeill 7:1add29d51e72 3309 Status = VL53L1_ERROR_OFFSET_CAL_NO_SAMPLE_FAIL;
Charles MacNeill 7:1add29d51e72 3310 VL53L1_StopMeasurement(Dev);
Charles MacNeill 7:1add29d51e72 3311 Repeat--;
Charles MacNeill 7:1add29d51e72 3312 }
Charles MacNeill 7:1add29d51e72 3313 if ((SmudgeStatus == VL53L1_ERROR_NONE) && (smudge_corr_en == 1))
Charles MacNeill 7:1add29d51e72 3314 SmudgeStatus = VL53L1_dynamic_xtalk_correction_enable(Dev);
Charles MacNeill 7:1add29d51e72 3315 if ((sum_ranging < 0) ||
Charles MacNeill 7:1add29d51e72 3316 (sum_ranging > ((int32_t) total_count * 0xffff)))
Charles MacNeill 7:1add29d51e72 3317 Status = VL53L1_WARNING_OFFSET_CAL_SIGMA_TOO_HIGH;
Charles MacNeill 7:1add29d51e72 3318
Charles MacNeill 7:1add29d51e72 3319 if ((Status == VL53L1_ERROR_NONE) && (total_count > 0)) {
Charles MacNeill 7:1add29d51e72 3320 IncRounding = total_count / 2;
Charles MacNeill 7:1add29d51e72 3321 meanDistance_mm = (int16_t)
Charles MacNeill 7:1add29d51e72 3322 ((sum_ranging + IncRounding) / total_count);
Charles MacNeill 7:1add29d51e72 3323 offset = START_OFFSET - meanDistance_mm + ZeroDistanceOffset;
Charles MacNeill 7:1add29d51e72 3324 pdev->customer.algo__part_to_part_range_offset_mm = 0;
Charles MacNeill 7:1add29d51e72 3325 pdev->customer.mm_config__inner_offset_mm = offset;
Charles MacNeill 7:1add29d51e72 3326 pdev->customer.mm_config__outer_offset_mm = offset;
Charles MacNeill 7:1add29d51e72 3327 Status = VL53L1_set_customer_nvm_managed(Dev,
Charles MacNeill 7:1add29d51e72 3328 &(pdev->customer));
Charles MacNeill 7:1add29d51e72 3329 }
Charles MacNeill 7:1add29d51e72 3330
Charles MacNeill 7:1add29d51e72 3331 LOG_FUNCTION_END(Status);
Charles MacNeill 7:1add29d51e72 3332 return Status;
Charles MacNeill 7:1add29d51e72 3333 }
Charles MacNeill 7:1add29d51e72 3334
Charles MacNeill 7:1add29d51e72 3335 VL53L1_Error VL53L1_SetCalibrationData(VL53L1_DEV Dev,
Charles MacNeill 7:1add29d51e72 3336 VL53L1_CalibrationData_t *pCalibrationData)
Charles MacNeill 7:1add29d51e72 3337 {
Charles MacNeill 7:1add29d51e72 3338 VL53L1_Error Status = VL53L1_ERROR_NONE;
Charles MacNeill 7:1add29d51e72 3339 VL53L1_CustomerNvmManaged_t *pC;
Charles MacNeill 7:1add29d51e72 3340 VL53L1_calibration_data_t cal_data;
Charles MacNeill 7:1add29d51e72 3341 uint32_t x, IncomeVersion, CurrentVersion;
Charles MacNeill 7:1add29d51e72 3342 uint8_t CalStopsOn_cal_peak_rate_map = 0;
Charles MacNeill 7:1add29d51e72 3343 VL53L1_xtalk_calibration_results_t xtalk;
Charles MacNeill 7:1add29d51e72 3344 VL53L1_LLDriverData_t *pdev = VL53L1DevStructGetLLDriverHandle(Dev);
Charles MacNeill 7:1add29d51e72 3345
Charles MacNeill 7:1add29d51e72 3346 LOG_FUNCTION_START("");
Charles MacNeill 7:1add29d51e72 3347
Charles MacNeill 7:1add29d51e72 3348 cal_data.struct_version = pCalibrationData->struct_version -
Charles MacNeill 7:1add29d51e72 3349 VL53L1_ADDITIONAL_CALIBRATION_DATA_STRUCT_VERSION;
Charles MacNeill 7:1add29d51e72 3350
Charles MacNeill 7:1add29d51e72 3351 IncomeVersion = pCalibrationData->struct_version;
Charles MacNeill 7:1add29d51e72 3352 CurrentVersion = VL53L1_LL_CALIBRATION_DATA_STRUCT_VERSION +
Charles MacNeill 7:1add29d51e72 3353 VL53L1_ADDITIONAL_CALIBRATION_DATA_STRUCT_VERSION;
Charles MacNeill 7:1add29d51e72 3354
Charles MacNeill 7:1add29d51e72 3355 if ((IncomeVersion < CurrentVersion) &&
Charles MacNeill 7:1add29d51e72 3356 ((IncomeVersion & 0xFFFFFF0F) ==
Charles MacNeill 7:1add29d51e72 3357 (CurrentVersion & 0xFFFFFF0F))) {
Charles MacNeill 7:1add29d51e72 3358 cal_data.struct_version =
Charles MacNeill 7:1add29d51e72 3359 VL53L1_LL_CALIBRATION_DATA_STRUCT_VERSION;
Charles MacNeill 7:1add29d51e72 3360 CalStopsOn_cal_peak_rate_map = 1;
Charles MacNeill 7:1add29d51e72 3361
Charles MacNeill 7:1add29d51e72 3362 pdev->tuning_parms.tp_hist_merge = 0;
Charles MacNeill 7:1add29d51e72 3363 }
Charles MacNeill 7:1add29d51e72 3364
Charles MacNeill 7:1add29d51e72 3365
Charles MacNeill 7:1add29d51e72 3366
Charles MacNeill 7:1add29d51e72 3367 memcpy(
Charles MacNeill 7:1add29d51e72 3368 &(cal_data.fmt_dmax_cal),
Charles MacNeill 7:1add29d51e72 3369 &(pCalibrationData->fmt_dmax_cal),
Charles MacNeill 7:1add29d51e72 3370 sizeof(VL53L1_dmax_calibration_data_t));
Charles MacNeill 7:1add29d51e72 3371
Charles MacNeill 7:1add29d51e72 3372
Charles MacNeill 7:1add29d51e72 3373 memcpy(
Charles MacNeill 7:1add29d51e72 3374 &(cal_data.cust_dmax_cal),
Charles MacNeill 7:1add29d51e72 3375 &(pCalibrationData->cust_dmax_cal),
Charles MacNeill 7:1add29d51e72 3376 sizeof(VL53L1_dmax_calibration_data_t));
Charles MacNeill 7:1add29d51e72 3377
Charles MacNeill 7:1add29d51e72 3378
Charles MacNeill 7:1add29d51e72 3379 memcpy(
Charles MacNeill 7:1add29d51e72 3380 &(cal_data.add_off_cal_data),
Charles MacNeill 7:1add29d51e72 3381 &(pCalibrationData->add_off_cal_data),
Charles MacNeill 7:1add29d51e72 3382 sizeof(VL53L1_additional_offset_cal_data_t));
Charles MacNeill 7:1add29d51e72 3383
Charles MacNeill 7:1add29d51e72 3384
Charles MacNeill 7:1add29d51e72 3385 memcpy(
Charles MacNeill 7:1add29d51e72 3386 &(cal_data.optical_centre),
Charles MacNeill 7:1add29d51e72 3387 &(pCalibrationData->optical_centre),
Charles MacNeill 7:1add29d51e72 3388 sizeof(VL53L1_optical_centre_t));
Charles MacNeill 7:1add29d51e72 3389
Charles MacNeill 7:1add29d51e72 3390
Charles MacNeill 7:1add29d51e72 3391 memcpy(
Charles MacNeill 7:1add29d51e72 3392 &(cal_data.xtalkhisto),
Charles MacNeill 7:1add29d51e72 3393 &(pCalibrationData->xtalkhisto),
Charles MacNeill 7:1add29d51e72 3394 sizeof(VL53L1_xtalk_histogram_data_t));
Charles MacNeill 7:1add29d51e72 3395
Charles MacNeill 7:1add29d51e72 3396
Charles MacNeill 7:1add29d51e72 3397 memcpy(
Charles MacNeill 7:1add29d51e72 3398 &(cal_data.gain_cal),
Charles MacNeill 7:1add29d51e72 3399 &(pCalibrationData->gain_cal),
Charles MacNeill 7:1add29d51e72 3400 sizeof(VL53L1_gain_calibration_data_t));
Charles MacNeill 7:1add29d51e72 3401
Charles MacNeill 7:1add29d51e72 3402
Charles MacNeill 7:1add29d51e72 3403 memcpy(
Charles MacNeill 7:1add29d51e72 3404 &(cal_data.cal_peak_rate_map),
Charles MacNeill 7:1add29d51e72 3405 &(pCalibrationData->cal_peak_rate_map),
Charles MacNeill 7:1add29d51e72 3406 sizeof(VL53L1_cal_peak_rate_map_t));
Charles MacNeill 7:1add29d51e72 3407
Charles MacNeill 7:1add29d51e72 3408
Charles MacNeill 7:1add29d51e72 3409 if (!CalStopsOn_cal_peak_rate_map)
Charles MacNeill 7:1add29d51e72 3410 memcpy(
Charles MacNeill 7:1add29d51e72 3411 &(cal_data.per_vcsel_cal_data),
Charles MacNeill 7:1add29d51e72 3412 &(pCalibrationData->per_vcsel_cal_data),
Charles MacNeill 7:1add29d51e72 3413 sizeof(VL53L1_per_vcsel_period_offset_cal_data_t));
Charles MacNeill 7:1add29d51e72 3414 else {
Charles MacNeill 7:1add29d51e72 3415 cal_data.per_vcsel_cal_data.short_a_offset_mm =
Charles MacNeill 7:1add29d51e72 3416 cal_data.per_vcsel_cal_data.short_b_offset_mm =
Charles MacNeill 7:1add29d51e72 3417 cal_data.per_vcsel_cal_data.medium_a_offset_mm =
Charles MacNeill 7:1add29d51e72 3418 cal_data.per_vcsel_cal_data.medium_b_offset_mm =
Charles MacNeill 7:1add29d51e72 3419 cal_data.per_vcsel_cal_data.long_a_offset_mm =
Charles MacNeill 7:1add29d51e72 3420 cal_data.per_vcsel_cal_data.long_b_offset_mm = 0;
Charles MacNeill 7:1add29d51e72 3421 }
Charles MacNeill 7:1add29d51e72 3422
Charles MacNeill 7:1add29d51e72 3423 pC = &pCalibrationData->customer;
Charles MacNeill 7:1add29d51e72 3424 x = pC->algo__crosstalk_compensation_plane_offset_kcps;
Charles MacNeill 7:1add29d51e72 3425 cal_data.customer.algo__crosstalk_compensation_plane_offset_kcps =
Charles MacNeill 7:1add29d51e72 3426 (uint16_t)(x&0x0000FFFF);
Charles MacNeill 7:1add29d51e72 3427
Charles MacNeill 7:1add29d51e72 3428 cal_data.customer.global_config__spad_enables_ref_0 =
Charles MacNeill 7:1add29d51e72 3429 pC->global_config__spad_enables_ref_0;
Charles MacNeill 7:1add29d51e72 3430 cal_data.customer.global_config__spad_enables_ref_1 =
Charles MacNeill 7:1add29d51e72 3431 pC->global_config__spad_enables_ref_1;
Charles MacNeill 7:1add29d51e72 3432 cal_data.customer.global_config__spad_enables_ref_2 =
Charles MacNeill 7:1add29d51e72 3433 pC->global_config__spad_enables_ref_2;
Charles MacNeill 7:1add29d51e72 3434 cal_data.customer.global_config__spad_enables_ref_3 =
Charles MacNeill 7:1add29d51e72 3435 pC->global_config__spad_enables_ref_3;
Charles MacNeill 7:1add29d51e72 3436 cal_data.customer.global_config__spad_enables_ref_4 =
Charles MacNeill 7:1add29d51e72 3437 pC->global_config__spad_enables_ref_4;
Charles MacNeill 7:1add29d51e72 3438 cal_data.customer.global_config__spad_enables_ref_5 =
Charles MacNeill 7:1add29d51e72 3439 pC->global_config__spad_enables_ref_5;
Charles MacNeill 7:1add29d51e72 3440 cal_data.customer.global_config__ref_en_start_select =
Charles MacNeill 7:1add29d51e72 3441 pC->global_config__ref_en_start_select;
Charles MacNeill 7:1add29d51e72 3442 cal_data.customer.ref_spad_man__num_requested_ref_spads =
Charles MacNeill 7:1add29d51e72 3443 pC->ref_spad_man__num_requested_ref_spads;
Charles MacNeill 7:1add29d51e72 3444 cal_data.customer.ref_spad_man__ref_location =
Charles MacNeill 7:1add29d51e72 3445 pC->ref_spad_man__ref_location;
Charles MacNeill 7:1add29d51e72 3446 cal_data.customer.algo__crosstalk_compensation_x_plane_gradient_kcps =
Charles MacNeill 7:1add29d51e72 3447 pC->algo__crosstalk_compensation_x_plane_gradient_kcps;
Charles MacNeill 7:1add29d51e72 3448 cal_data.customer.algo__crosstalk_compensation_y_plane_gradient_kcps =
Charles MacNeill 7:1add29d51e72 3449 pC->algo__crosstalk_compensation_y_plane_gradient_kcps;
Charles MacNeill 7:1add29d51e72 3450 cal_data.customer.ref_spad_char__total_rate_target_mcps =
Charles MacNeill 7:1add29d51e72 3451 pC->ref_spad_char__total_rate_target_mcps;
Charles MacNeill 7:1add29d51e72 3452 cal_data.customer.algo__part_to_part_range_offset_mm =
Charles MacNeill 7:1add29d51e72 3453 pC->algo__part_to_part_range_offset_mm;
Charles MacNeill 7:1add29d51e72 3454 cal_data.customer.mm_config__inner_offset_mm =
Charles MacNeill 7:1add29d51e72 3455 pC->mm_config__inner_offset_mm;
Charles MacNeill 7:1add29d51e72 3456 cal_data.customer.mm_config__outer_offset_mm =
Charles MacNeill 7:1add29d51e72 3457 pC->mm_config__outer_offset_mm;
Charles MacNeill 7:1add29d51e72 3458
Charles MacNeill 7:1add29d51e72 3459 Status = VL53L1_set_part_to_part_data(Dev, &cal_data);
Charles MacNeill 7:1add29d51e72 3460 if (Status != VL53L1_ERROR_NONE)
Charles MacNeill 7:1add29d51e72 3461 goto ENDFUNC;
Charles MacNeill 7:1add29d51e72 3462
Charles MacNeill 7:1add29d51e72 3463 Status = VL53L1_get_current_xtalk_settings(Dev, &xtalk);
Charles MacNeill 7:1add29d51e72 3464
Charles MacNeill 7:1add29d51e72 3465 if (Status != VL53L1_ERROR_NONE)
Charles MacNeill 7:1add29d51e72 3466 goto ENDFUNC;
Charles MacNeill 7:1add29d51e72 3467
Charles MacNeill 7:1add29d51e72 3468 xtalk.algo__crosstalk_compensation_plane_offset_kcps = x;
Charles MacNeill 7:1add29d51e72 3469
Charles MacNeill 7:1add29d51e72 3470 Status = VL53L1_set_tuning_parm(Dev,
Charles MacNeill 7:1add29d51e72 3471 VL53L1_TUNINGPARM_DYNXTALK_NODETECT_XTALK_OFFSET_KCPS,
Charles MacNeill 7:1add29d51e72 3472 x);
Charles MacNeill 7:1add29d51e72 3473
Charles MacNeill 7:1add29d51e72 3474
Charles MacNeill 7:1add29d51e72 3475 if (!CalStopsOn_cal_peak_rate_map)
Charles MacNeill 7:1add29d51e72 3476 memcpy(
Charles MacNeill 7:1add29d51e72 3477 &(xtalk.algo__xtalk_cpo_HistoMerge_kcps[0]),
Charles MacNeill 7:1add29d51e72 3478 &(pCalibrationData->algo__xtalk_cpo_HistoMerge_kcps[0]),
Charles MacNeill 7:1add29d51e72 3479 sizeof(pCalibrationData->algo__xtalk_cpo_HistoMerge_kcps));
Charles MacNeill 7:1add29d51e72 3480 else
Charles MacNeill 7:1add29d51e72 3481 memset(
Charles MacNeill 7:1add29d51e72 3482 &(xtalk.algo__xtalk_cpo_HistoMerge_kcps[0]), 0,
Charles MacNeill 7:1add29d51e72 3483 sizeof(pCalibrationData->algo__xtalk_cpo_HistoMerge_kcps));
Charles MacNeill 7:1add29d51e72 3484
Charles MacNeill 7:1add29d51e72 3485 Status = VL53L1_set_current_xtalk_settings(Dev, &xtalk);
Charles MacNeill 7:1add29d51e72 3486
Charles MacNeill 7:1add29d51e72 3487 ENDFUNC:
Charles MacNeill 7:1add29d51e72 3488 LOG_FUNCTION_END(Status);
Charles MacNeill 7:1add29d51e72 3489 return Status;
Charles MacNeill 7:1add29d51e72 3490
Charles MacNeill 7:1add29d51e72 3491 }
Charles MacNeill 7:1add29d51e72 3492
Charles MacNeill 7:1add29d51e72 3493 VL53L1_Error VL53L1_GetCalibrationData(VL53L1_DEV Dev,
Charles MacNeill 7:1add29d51e72 3494 VL53L1_CalibrationData_t *pCalibrationData)
Charles MacNeill 7:1add29d51e72 3495 {
Charles MacNeill 7:1add29d51e72 3496 VL53L1_Error Status = VL53L1_ERROR_NONE;
Charles MacNeill 7:1add29d51e72 3497 VL53L1_calibration_data_t cal_data;
Charles MacNeill 7:1add29d51e72 3498 VL53L1_CustomerNvmManaged_t *pC;
Charles MacNeill 7:1add29d51e72 3499 VL53L1_customer_nvm_managed_t *pC2;
Charles MacNeill 7:1add29d51e72 3500 VL53L1_xtalk_calibration_results_t xtalk;
Charles MacNeill 7:1add29d51e72 3501 uint32_t tmp;
Charles MacNeill 7:1add29d51e72 3502 VL53L1_PresetModes PresetMode;
Charles MacNeill 7:1add29d51e72 3503
Charles MacNeill 7:1add29d51e72 3504 LOG_FUNCTION_START("");
Charles MacNeill 7:1add29d51e72 3505
Charles MacNeill 7:1add29d51e72 3506
Charles MacNeill 7:1add29d51e72 3507 Status = VL53L1_get_part_to_part_data(Dev, &cal_data);
Charles MacNeill 7:1add29d51e72 3508
Charles MacNeill 7:1add29d51e72 3509 pCalibrationData->struct_version = cal_data.struct_version +
Charles MacNeill 7:1add29d51e72 3510 VL53L1_ADDITIONAL_CALIBRATION_DATA_STRUCT_VERSION;
Charles MacNeill 7:1add29d51e72 3511
Charles MacNeill 7:1add29d51e72 3512
Charles MacNeill 7:1add29d51e72 3513 memcpy(
Charles MacNeill 7:1add29d51e72 3514 &(pCalibrationData->fmt_dmax_cal),
Charles MacNeill 7:1add29d51e72 3515 &(cal_data.fmt_dmax_cal),
Charles MacNeill 7:1add29d51e72 3516 sizeof(VL53L1_dmax_calibration_data_t));
Charles MacNeill 7:1add29d51e72 3517
Charles MacNeill 7:1add29d51e72 3518
Charles MacNeill 7:1add29d51e72 3519 memcpy(
Charles MacNeill 7:1add29d51e72 3520 &(pCalibrationData->cust_dmax_cal),
Charles MacNeill 7:1add29d51e72 3521 &(cal_data.cust_dmax_cal),
Charles MacNeill 7:1add29d51e72 3522 sizeof(VL53L1_dmax_calibration_data_t));
Charles MacNeill 7:1add29d51e72 3523
Charles MacNeill 7:1add29d51e72 3524
Charles MacNeill 7:1add29d51e72 3525 memcpy(
Charles MacNeill 7:1add29d51e72 3526 &(pCalibrationData->add_off_cal_data),
Charles MacNeill 7:1add29d51e72 3527 &(cal_data.add_off_cal_data),
Charles MacNeill 7:1add29d51e72 3528 sizeof(VL53L1_additional_offset_cal_data_t));
Charles MacNeill 7:1add29d51e72 3529
Charles MacNeill 7:1add29d51e72 3530
Charles MacNeill 7:1add29d51e72 3531 memcpy(
Charles MacNeill 7:1add29d51e72 3532 &(pCalibrationData->optical_centre),
Charles MacNeill 7:1add29d51e72 3533 &(cal_data.optical_centre),
Charles MacNeill 7:1add29d51e72 3534 sizeof(VL53L1_optical_centre_t));
Charles MacNeill 7:1add29d51e72 3535
Charles MacNeill 7:1add29d51e72 3536
Charles MacNeill 7:1add29d51e72 3537 memcpy(
Charles MacNeill 7:1add29d51e72 3538 &(pCalibrationData->xtalkhisto),
Charles MacNeill 7:1add29d51e72 3539 &(cal_data.xtalkhisto),
Charles MacNeill 7:1add29d51e72 3540 sizeof(VL53L1_xtalk_histogram_data_t));
Charles MacNeill 7:1add29d51e72 3541
Charles MacNeill 7:1add29d51e72 3542 memcpy(
Charles MacNeill 7:1add29d51e72 3543 &(pCalibrationData->gain_cal),
Charles MacNeill 7:1add29d51e72 3544 &(cal_data.gain_cal),
Charles MacNeill 7:1add29d51e72 3545 sizeof(VL53L1_gain_calibration_data_t));
Charles MacNeill 7:1add29d51e72 3546
Charles MacNeill 7:1add29d51e72 3547
Charles MacNeill 7:1add29d51e72 3548 memcpy(
Charles MacNeill 7:1add29d51e72 3549 &(pCalibrationData->cal_peak_rate_map),
Charles MacNeill 7:1add29d51e72 3550 &(cal_data.cal_peak_rate_map),
Charles MacNeill 7:1add29d51e72 3551 sizeof(VL53L1_cal_peak_rate_map_t));
Charles MacNeill 7:1add29d51e72 3552
Charles MacNeill 7:1add29d51e72 3553
Charles MacNeill 7:1add29d51e72 3554 memcpy(
Charles MacNeill 7:1add29d51e72 3555 &(pCalibrationData->per_vcsel_cal_data),
Charles MacNeill 7:1add29d51e72 3556 &(cal_data.per_vcsel_cal_data),
Charles MacNeill 7:1add29d51e72 3557 sizeof(VL53L1_per_vcsel_period_offset_cal_data_t));
Charles MacNeill 7:1add29d51e72 3558
Charles MacNeill 7:1add29d51e72 3559 pC = &pCalibrationData->customer;
Charles MacNeill 7:1add29d51e72 3560 pC2 = &cal_data.customer;
Charles MacNeill 7:1add29d51e72 3561 pC->global_config__spad_enables_ref_0 =
Charles MacNeill 7:1add29d51e72 3562 pC2->global_config__spad_enables_ref_0;
Charles MacNeill 7:1add29d51e72 3563 pC->global_config__spad_enables_ref_1 =
Charles MacNeill 7:1add29d51e72 3564 pC2->global_config__spad_enables_ref_1;
Charles MacNeill 7:1add29d51e72 3565 pC->global_config__spad_enables_ref_2 =
Charles MacNeill 7:1add29d51e72 3566 pC2->global_config__spad_enables_ref_2;
Charles MacNeill 7:1add29d51e72 3567 pC->global_config__spad_enables_ref_3 =
Charles MacNeill 7:1add29d51e72 3568 pC2->global_config__spad_enables_ref_3;
Charles MacNeill 7:1add29d51e72 3569 pC->global_config__spad_enables_ref_4 =
Charles MacNeill 7:1add29d51e72 3570 pC2->global_config__spad_enables_ref_4;
Charles MacNeill 7:1add29d51e72 3571 pC->global_config__spad_enables_ref_5 =
Charles MacNeill 7:1add29d51e72 3572 pC2->global_config__spad_enables_ref_5;
Charles MacNeill 7:1add29d51e72 3573 pC->global_config__ref_en_start_select =
Charles MacNeill 7:1add29d51e72 3574 pC2->global_config__ref_en_start_select;
Charles MacNeill 7:1add29d51e72 3575 pC->ref_spad_man__num_requested_ref_spads =
Charles MacNeill 7:1add29d51e72 3576 pC2->ref_spad_man__num_requested_ref_spads;
Charles MacNeill 7:1add29d51e72 3577 pC->ref_spad_man__ref_location =
Charles MacNeill 7:1add29d51e72 3578 pC2->ref_spad_man__ref_location;
Charles MacNeill 7:1add29d51e72 3579 pC->algo__crosstalk_compensation_x_plane_gradient_kcps =
Charles MacNeill 7:1add29d51e72 3580 pC2->algo__crosstalk_compensation_x_plane_gradient_kcps;
Charles MacNeill 7:1add29d51e72 3581 pC->algo__crosstalk_compensation_y_plane_gradient_kcps =
Charles MacNeill 7:1add29d51e72 3582 pC2->algo__crosstalk_compensation_y_plane_gradient_kcps;
Charles MacNeill 7:1add29d51e72 3583 pC->ref_spad_char__total_rate_target_mcps =
Charles MacNeill 7:1add29d51e72 3584 pC2->ref_spad_char__total_rate_target_mcps;
Charles MacNeill 7:1add29d51e72 3585 pC->algo__part_to_part_range_offset_mm =
Charles MacNeill 7:1add29d51e72 3586 pC2->algo__part_to_part_range_offset_mm;
Charles MacNeill 7:1add29d51e72 3587 pC->mm_config__inner_offset_mm =
Charles MacNeill 7:1add29d51e72 3588 pC2->mm_config__inner_offset_mm;
Charles MacNeill 7:1add29d51e72 3589 pC->mm_config__outer_offset_mm =
Charles MacNeill 7:1add29d51e72 3590 pC2->mm_config__outer_offset_mm;
Charles MacNeill 7:1add29d51e72 3591
Charles MacNeill 7:1add29d51e72 3592 pC->algo__crosstalk_compensation_plane_offset_kcps =
Charles MacNeill 7:1add29d51e72 3593 (uint32_t)(
Charles MacNeill 7:1add29d51e72 3594 pC2->algo__crosstalk_compensation_plane_offset_kcps);
Charles MacNeill 7:1add29d51e72 3595
Charles MacNeill 7:1add29d51e72 3596 PresetMode = VL53L1DevDataGet(Dev, CurrentParameters.PresetMode);
Charles MacNeill 7:1add29d51e72 3597
Charles MacNeill 7:1add29d51e72 3598 if ((PresetMode == VL53L1_PRESETMODE_RANGING) ||
Charles MacNeill 7:1add29d51e72 3599 (PresetMode == VL53L1_PRESETMODE_MULTIZONES_SCANNING) ||
Charles MacNeill 7:1add29d51e72 3600 (PresetMode == VL53L1_PRESETMODE_PROXY_RANGING_MODE)
Charles MacNeill 7:1add29d51e72 3601 ) {
Charles MacNeill 7:1add29d51e72 3602
Charles MacNeill 7:1add29d51e72 3603 Status = VL53L1_get_current_xtalk_settings(Dev, &xtalk);
Charles MacNeill 7:1add29d51e72 3604
Charles MacNeill 7:1add29d51e72 3605 if (Status != VL53L1_ERROR_NONE)
Charles MacNeill 7:1add29d51e72 3606 goto ENDFUNC;
Charles MacNeill 7:1add29d51e72 3607
Charles MacNeill 7:1add29d51e72 3608 tmp = xtalk.algo__crosstalk_compensation_plane_offset_kcps;
Charles MacNeill 7:1add29d51e72 3609 pC->algo__crosstalk_compensation_plane_offset_kcps = tmp;
Charles MacNeill 7:1add29d51e72 3610 tmp = xtalk.algo__crosstalk_compensation_x_plane_gradient_kcps;
Charles MacNeill 7:1add29d51e72 3611 pC->algo__crosstalk_compensation_x_plane_gradient_kcps = tmp;
Charles MacNeill 7:1add29d51e72 3612 tmp = xtalk.algo__crosstalk_compensation_y_plane_gradient_kcps;
Charles MacNeill 7:1add29d51e72 3613 pC->algo__crosstalk_compensation_y_plane_gradient_kcps = tmp;
Charles MacNeill 7:1add29d51e72 3614
Charles MacNeill 7:1add29d51e72 3615 memcpy(&(pCalibrationData->algo__xtalk_cpo_HistoMerge_kcps[0]),
Charles MacNeill 7:1add29d51e72 3616 &(xtalk.algo__xtalk_cpo_HistoMerge_kcps[0]),
Charles MacNeill 7:1add29d51e72 3617 sizeof(pCalibrationData->algo__xtalk_cpo_HistoMerge_kcps));
Charles MacNeill 7:1add29d51e72 3618 }
Charles MacNeill 7:1add29d51e72 3619 ENDFUNC:
Charles MacNeill 7:1add29d51e72 3620 LOG_FUNCTION_END(Status);
Charles MacNeill 7:1add29d51e72 3621 return Status;
Charles MacNeill 7:1add29d51e72 3622 }
Charles MacNeill 7:1add29d51e72 3623
Charles MacNeill 7:1add29d51e72 3624
Charles MacNeill 7:1add29d51e72 3625 VL53L1_Error VL53L1_SetZoneCalibrationData(VL53L1_DEV Dev,
Charles MacNeill 7:1add29d51e72 3626 VL53L1_ZoneCalibrationData_t *pZoneCalibrationData)
Charles MacNeill 7:1add29d51e72 3627 {
Charles MacNeill 7:1add29d51e72 3628 VL53L1_Error Status = VL53L1_ERROR_NONE;
Charles MacNeill 7:1add29d51e72 3629
Charles MacNeill 7:1add29d51e72 3630 LOG_FUNCTION_START("");
Charles MacNeill 7:1add29d51e72 3631
Charles MacNeill 7:1add29d51e72 3632 Status = VL53L1_set_zone_calibration_data(Dev, pZoneCalibrationData);
Charles MacNeill 7:1add29d51e72 3633
Charles MacNeill 7:1add29d51e72 3634 LOG_FUNCTION_END(Status);
Charles MacNeill 7:1add29d51e72 3635 return Status;
Charles MacNeill 7:1add29d51e72 3636
Charles MacNeill 7:1add29d51e72 3637 }
Charles MacNeill 7:1add29d51e72 3638
Charles MacNeill 7:1add29d51e72 3639 VL53L1_Error VL53L1_GetZoneCalibrationData(VL53L1_DEV Dev,
Charles MacNeill 7:1add29d51e72 3640 VL53L1_ZoneCalibrationData_t *pZoneCalibrationData)
Charles MacNeill 7:1add29d51e72 3641 {
Charles MacNeill 7:1add29d51e72 3642 VL53L1_Error Status = VL53L1_ERROR_NONE;
Charles MacNeill 7:1add29d51e72 3643
Charles MacNeill 7:1add29d51e72 3644 LOG_FUNCTION_START("");
Charles MacNeill 7:1add29d51e72 3645
Charles MacNeill 7:1add29d51e72 3646 Status = VL53L1_get_zone_calibration_data(Dev, pZoneCalibrationData);
Charles MacNeill 7:1add29d51e72 3647
Charles MacNeill 7:1add29d51e72 3648 LOG_FUNCTION_END(Status);
Charles MacNeill 7:1add29d51e72 3649 return Status;
Charles MacNeill 7:1add29d51e72 3650 }
Charles MacNeill 7:1add29d51e72 3651
Charles MacNeill 7:1add29d51e72 3652 VL53L1_Error VL53L1_GetOpticalCenter(VL53L1_DEV Dev,
Charles MacNeill 7:1add29d51e72 3653 FixPoint1616_t *pOpticalCenterX,
Charles MacNeill 7:1add29d51e72 3654 FixPoint1616_t *pOpticalCenterY)
Charles MacNeill 7:1add29d51e72 3655 {
Charles MacNeill 7:1add29d51e72 3656 VL53L1_Error Status = VL53L1_ERROR_NONE;
Charles MacNeill 7:1add29d51e72 3657 VL53L1_calibration_data_t CalibrationData;
Charles MacNeill 7:1add29d51e72 3658
Charles MacNeill 7:1add29d51e72 3659 LOG_FUNCTION_START("");
Charles MacNeill 7:1add29d51e72 3660
Charles MacNeill 7:1add29d51e72 3661 *pOpticalCenterX = 0;
Charles MacNeill 7:1add29d51e72 3662 *pOpticalCenterY = 0;
Charles MacNeill 7:1add29d51e72 3663 Status = VL53L1_get_part_to_part_data(Dev, &CalibrationData);
Charles MacNeill 7:1add29d51e72 3664 if (Status == VL53L1_ERROR_NONE) {
Charles MacNeill 7:1add29d51e72 3665 *pOpticalCenterX = VL53L1_FIXPOINT44TOFIXPOINT1616(
Charles MacNeill 7:1add29d51e72 3666 CalibrationData.optical_centre.x_centre);
Charles MacNeill 7:1add29d51e72 3667 *pOpticalCenterY = VL53L1_FIXPOINT44TOFIXPOINT1616(
Charles MacNeill 7:1add29d51e72 3668 CalibrationData.optical_centre.y_centre);
Charles MacNeill 7:1add29d51e72 3669 }
Charles MacNeill 7:1add29d51e72 3670
Charles MacNeill 7:1add29d51e72 3671 LOG_FUNCTION_END(Status);
Charles MacNeill 7:1add29d51e72 3672 return Status;
Charles MacNeill 7:1add29d51e72 3673 }
Charles MacNeill 7:1add29d51e72 3674
Charles MacNeill 7:1add29d51e72 3675
Charles MacNeill 7:1add29d51e72 3676
Charles MacNeill 7:1add29d51e72 3677
Charles MacNeill 7:1add29d51e72 3678
Charles MacNeill 7:1add29d51e72 3679
Charles MacNeill 7:1add29d51e72 3680 VL53L1_Error VL53L1_SetThresholdConfig(VL53L1_DEV Dev,
Charles MacNeill 7:1add29d51e72 3681 VL53L1_DetectionConfig_t *pConfig)
Charles MacNeill 7:1add29d51e72 3682 {
Charles MacNeill 7:1add29d51e72 3683 #define BADTHRESBOUNDS(T) \
Charles MacNeill 7:1add29d51e72 3684 (((T.CrossMode == VL53L1_THRESHOLD_OUT_OF_WINDOW) || \
Charles MacNeill 7:1add29d51e72 3685 (T.CrossMode == VL53L1_THRESHOLD_IN_WINDOW)) && (T.Low > T.High))
Charles MacNeill 7:1add29d51e72 3686
Charles MacNeill 7:1add29d51e72 3687 VL53L1_Error Status = VL53L1_ERROR_NONE;
Charles MacNeill 7:1add29d51e72 3688 VL53L1_GPIO_interrupt_config_t Cfg;
Charles MacNeill 7:1add29d51e72 3689 uint16_t g;
Charles MacNeill 7:1add29d51e72 3690 FixPoint1616_t gain, high1616, low1616;
Charles MacNeill 7:1add29d51e72 3691 VL53L1_LLDriverData_t *pdev;
Charles MacNeill 7:1add29d51e72 3692
Charles MacNeill 7:1add29d51e72 3693 LOG_FUNCTION_START("");
Charles MacNeill 7:1add29d51e72 3694
Charles MacNeill 7:1add29d51e72 3695 pdev = VL53L1DevStructGetLLDriverHandle(Dev);
Charles MacNeill 7:1add29d51e72 3696
Charles MacNeill 7:1add29d51e72 3697 Status = VL53L1_get_GPIO_interrupt_config(Dev, &Cfg);
Charles MacNeill 7:1add29d51e72 3698 if (Status != VL53L1_ERROR_NONE)
Charles MacNeill 7:1add29d51e72 3699 return Status;
Charles MacNeill 7:1add29d51e72 3700
Charles MacNeill 7:1add29d51e72 3701 if (pConfig->DetectionMode == VL53L1_DETECTION_NORMAL_RUN) {
Charles MacNeill 7:1add29d51e72 3702 Cfg.intr_new_measure_ready = 1;
Charles MacNeill 7:1add29d51e72 3703 Status = VL53L1_set_GPIO_interrupt_config_struct(Dev,
Charles MacNeill 7:1add29d51e72 3704 Cfg);
Charles MacNeill 7:1add29d51e72 3705 } else {
Charles MacNeill 7:1add29d51e72 3706 if (BADTHRESBOUNDS(pConfig->Distance))
Charles MacNeill 7:1add29d51e72 3707 Status = VL53L1_ERROR_INVALID_PARAMS;
Charles MacNeill 7:1add29d51e72 3708 if ((Status == VL53L1_ERROR_NONE) &&
Charles MacNeill 7:1add29d51e72 3709 (BADTHRESBOUNDS(pConfig->Rate)))
Charles MacNeill 7:1add29d51e72 3710 Status = VL53L1_ERROR_INVALID_PARAMS;
Charles MacNeill 7:1add29d51e72 3711 if (Status == VL53L1_ERROR_NONE) {
Charles MacNeill 7:1add29d51e72 3712 Cfg.intr_new_measure_ready = 0;
Charles MacNeill 7:1add29d51e72 3713 Cfg.intr_no_target = pConfig->IntrNoTarget;
Charles MacNeill 7:1add29d51e72 3714
Charles MacNeill 7:1add29d51e72 3715 g = pdev->gain_cal.standard_ranging_gain_factor;
Charles MacNeill 7:1add29d51e72 3716 if (g != 0) {
Charles MacNeill 7:1add29d51e72 3717
Charles MacNeill 7:1add29d51e72 3718 gain = (FixPoint1616_t) ((uint32_t)g << 5);
Charles MacNeill 7:1add29d51e72 3719 high1616 = (FixPoint1616_t) ((uint32_t)
Charles MacNeill 7:1add29d51e72 3720 pConfig->Distance.High << 16);
Charles MacNeill 7:1add29d51e72 3721 low1616 = (FixPoint1616_t) ((uint32_t)
Charles MacNeill 7:1add29d51e72 3722 pConfig->Distance.Low << 16);
Charles MacNeill 7:1add29d51e72 3723
Charles MacNeill 7:1add29d51e72 3724 high1616 = (high1616 + 32768) / gain;
Charles MacNeill 7:1add29d51e72 3725 low1616 = (low1616 + 32768) / gain;
Charles MacNeill 7:1add29d51e72 3726 Cfg.threshold_distance_high = (uint16_t)
Charles MacNeill 7:1add29d51e72 3727 (high1616 & 0xFFFF);
Charles MacNeill 7:1add29d51e72 3728 Cfg.threshold_distance_low = (uint16_t)
Charles MacNeill 7:1add29d51e72 3729 (low1616 & 0xFFFF);
Charles MacNeill 7:1add29d51e72 3730 }
Charles MacNeill 7:1add29d51e72 3731 Cfg.threshold_rate_high =
Charles MacNeill 7:1add29d51e72 3732 VL53L1_FIXPOINT1616TOFIXPOINT97(
Charles MacNeill 7:1add29d51e72 3733 pConfig->Rate.High);
Charles MacNeill 7:1add29d51e72 3734 Cfg.threshold_rate_low =
Charles MacNeill 7:1add29d51e72 3735 VL53L1_FIXPOINT1616TOFIXPOINT97(
Charles MacNeill 7:1add29d51e72 3736 pConfig->Rate.Low);
Charles MacNeill 7:1add29d51e72 3737
Charles MacNeill 7:1add29d51e72 3738 Cfg.intr_mode_distance = ConvertModeToLLD(
Charles MacNeill 7:1add29d51e72 3739 &Status,
Charles MacNeill 7:1add29d51e72 3740 pConfig->Distance.CrossMode);
Charles MacNeill 7:1add29d51e72 3741 if (Status == VL53L1_ERROR_NONE)
Charles MacNeill 7:1add29d51e72 3742 Cfg.intr_mode_rate = ConvertModeToLLD(
Charles MacNeill 7:1add29d51e72 3743 &Status,
Charles MacNeill 7:1add29d51e72 3744 pConfig->Rate.CrossMode);
Charles MacNeill 7:1add29d51e72 3745 }
Charles MacNeill 7:1add29d51e72 3746
Charles MacNeill 7:1add29d51e72 3747
Charles MacNeill 7:1add29d51e72 3748 if (Status == VL53L1_ERROR_NONE) {
Charles MacNeill 7:1add29d51e72 3749 Cfg.intr_combined_mode = 1;
Charles MacNeill 7:1add29d51e72 3750 switch (pConfig->DetectionMode) {
Charles MacNeill 7:1add29d51e72 3751 case VL53L1_DETECTION_DISTANCE_ONLY:
Charles MacNeill 7:1add29d51e72 3752 Cfg.threshold_rate_high = 0;
Charles MacNeill 7:1add29d51e72 3753 Cfg.threshold_rate_low = 0;
Charles MacNeill 7:1add29d51e72 3754 break;
Charles MacNeill 7:1add29d51e72 3755 case VL53L1_DETECTION_RATE_ONLY:
Charles MacNeill 7:1add29d51e72 3756 Cfg.threshold_distance_high = 0;
Charles MacNeill 7:1add29d51e72 3757 Cfg.threshold_distance_low = 0;
Charles MacNeill 7:1add29d51e72 3758 break;
Charles MacNeill 7:1add29d51e72 3759 case VL53L1_DETECTION_DISTANCE_OR_RATE:
Charles MacNeill 7:1add29d51e72 3760
Charles MacNeill 7:1add29d51e72 3761 break;
Charles MacNeill 7:1add29d51e72 3762 case VL53L1_DETECTION_DISTANCE_AND_RATE:
Charles MacNeill 7:1add29d51e72 3763 Cfg.intr_combined_mode = 0;
Charles MacNeill 7:1add29d51e72 3764 break;
Charles MacNeill 7:1add29d51e72 3765 default:
Charles MacNeill 7:1add29d51e72 3766 Status = VL53L1_ERROR_INVALID_PARAMS;
Charles MacNeill 7:1add29d51e72 3767 }
Charles MacNeill 7:1add29d51e72 3768 }
Charles MacNeill 7:1add29d51e72 3769
Charles MacNeill 7:1add29d51e72 3770 if (Status == VL53L1_ERROR_NONE)
Charles MacNeill 7:1add29d51e72 3771 Status =
Charles MacNeill 7:1add29d51e72 3772 VL53L1_set_GPIO_interrupt_config_struct(Dev, Cfg);
Charles MacNeill 7:1add29d51e72 3773
Charles MacNeill 7:1add29d51e72 3774 }
Charles MacNeill 7:1add29d51e72 3775
Charles MacNeill 7:1add29d51e72 3776 LOG_FUNCTION_END(Status);
Charles MacNeill 7:1add29d51e72 3777 return Status;
Charles MacNeill 7:1add29d51e72 3778 }
Charles MacNeill 7:1add29d51e72 3779
Charles MacNeill 7:1add29d51e72 3780
Charles MacNeill 7:1add29d51e72 3781 VL53L1_Error VL53L1_GetThresholdConfig(VL53L1_DEV Dev,
Charles MacNeill 7:1add29d51e72 3782 VL53L1_DetectionConfig_t *pConfig)
Charles MacNeill 7:1add29d51e72 3783 {
Charles MacNeill 7:1add29d51e72 3784 VL53L1_Error Status = VL53L1_ERROR_NONE;
Charles MacNeill 7:1add29d51e72 3785 VL53L1_GPIO_interrupt_config_t Cfg;
Charles MacNeill 7:1add29d51e72 3786
Charles MacNeill 7:1add29d51e72 3787 LOG_FUNCTION_START("");
Charles MacNeill 7:1add29d51e72 3788
Charles MacNeill 7:1add29d51e72 3789 Status = VL53L1_get_GPIO_interrupt_config(Dev, &Cfg);
Charles MacNeill 7:1add29d51e72 3790
Charles MacNeill 7:1add29d51e72 3791 if (Status != VL53L1_ERROR_NONE) {
Charles MacNeill 7:1add29d51e72 3792 LOG_FUNCTION_END(Status);
Charles MacNeill 7:1add29d51e72 3793 return Status;
Charles MacNeill 7:1add29d51e72 3794 }
Charles MacNeill 7:1add29d51e72 3795
Charles MacNeill 7:1add29d51e72 3796 pConfig->IntrNoTarget = Cfg.intr_no_target;
Charles MacNeill 7:1add29d51e72 3797 pConfig->Distance.High = Cfg.threshold_distance_high;
Charles MacNeill 7:1add29d51e72 3798 pConfig->Distance.Low = Cfg.threshold_distance_low;
Charles MacNeill 7:1add29d51e72 3799 pConfig->Rate.High =
Charles MacNeill 7:1add29d51e72 3800 VL53L1_FIXPOINT97TOFIXPOINT1616(
Charles MacNeill 7:1add29d51e72 3801 Cfg.threshold_rate_high);
Charles MacNeill 7:1add29d51e72 3802 pConfig->Rate.Low =
Charles MacNeill 7:1add29d51e72 3803 VL53L1_FIXPOINT97TOFIXPOINT1616(Cfg.threshold_rate_low);
Charles MacNeill 7:1add29d51e72 3804 pConfig->Distance.CrossMode =
Charles MacNeill 7:1add29d51e72 3805 ConvertModeFromLLD(&Status, Cfg.intr_mode_distance);
Charles MacNeill 7:1add29d51e72 3806 if (Status == VL53L1_ERROR_NONE)
Charles MacNeill 7:1add29d51e72 3807 pConfig->Rate.CrossMode =
Charles MacNeill 7:1add29d51e72 3808 ConvertModeFromLLD(&Status, Cfg.intr_mode_rate);
Charles MacNeill 7:1add29d51e72 3809
Charles MacNeill 7:1add29d51e72 3810 if (Cfg.intr_new_measure_ready == 1) {
Charles MacNeill 7:1add29d51e72 3811 pConfig->DetectionMode = VL53L1_DETECTION_NORMAL_RUN;
Charles MacNeill 7:1add29d51e72 3812 } else {
Charles MacNeill 7:1add29d51e72 3813
Charles MacNeill 7:1add29d51e72 3814 if (Status == VL53L1_ERROR_NONE) {
Charles MacNeill 7:1add29d51e72 3815 if (Cfg.intr_combined_mode == 0)
Charles MacNeill 7:1add29d51e72 3816 pConfig->DetectionMode =
Charles MacNeill 7:1add29d51e72 3817 VL53L1_DETECTION_DISTANCE_AND_RATE;
Charles MacNeill 7:1add29d51e72 3818 else {
Charles MacNeill 7:1add29d51e72 3819 if ((Cfg.threshold_distance_high == 0) &&
Charles MacNeill 7:1add29d51e72 3820 (Cfg.threshold_distance_low == 0))
Charles MacNeill 7:1add29d51e72 3821 pConfig->DetectionMode =
Charles MacNeill 7:1add29d51e72 3822 VL53L1_DETECTION_RATE_ONLY;
Charles MacNeill 7:1add29d51e72 3823 else if ((Cfg.threshold_rate_high == 0) &&
Charles MacNeill 7:1add29d51e72 3824 (Cfg.threshold_rate_low == 0))
Charles MacNeill 7:1add29d51e72 3825 pConfig->DetectionMode =
Charles MacNeill 7:1add29d51e72 3826 VL53L1_DETECTION_DISTANCE_ONLY;
Charles MacNeill 7:1add29d51e72 3827 else
Charles MacNeill 7:1add29d51e72 3828 pConfig->DetectionMode =
Charles MacNeill 7:1add29d51e72 3829 VL53L1_DETECTION_DISTANCE_OR_RATE;
Charles MacNeill 7:1add29d51e72 3830 }
Charles MacNeill 7:1add29d51e72 3831 }
Charles MacNeill 7:1add29d51e72 3832 }
Charles MacNeill 7:1add29d51e72 3833
Charles MacNeill 7:1add29d51e72 3834 LOG_FUNCTION_END(Status);
Charles MacNeill 7:1add29d51e72 3835 return Status;
Charles MacNeill 7:1add29d51e72 3836 }
Charles MacNeill 7:1add29d51e72 3837
Charles MacNeill 7:1add29d51e72 3838
Charles MacNeill 7:1add29d51e72 3839
Charles MacNeill 7:1add29d51e72 3840
Charles MacNeill 7:1add29d51e72 3841 VL53L1_Error VL53L1_PerformOffsetPerVcselCalibration(VL53L1_DEV Dev,
Charles MacNeill 7:1add29d51e72 3842 int32_t CalDistanceMilliMeter)
Charles MacNeill 7:1add29d51e72 3843 {
Charles MacNeill 7:1add29d51e72 3844 VL53L1_Error Status = VL53L1_ERROR_NONE;
Charles MacNeill 7:1add29d51e72 3845 int32_t sum_ranging_range_A, sum_ranging_range_B;
Charles MacNeill 7:1add29d51e72 3846 uint8_t offset_meas_range_A, offset_meas_range_B;
Charles MacNeill 7:1add29d51e72 3847 int16_t Max, UnderMax, OverMax, Repeat;
Charles MacNeill 7:1add29d51e72 3848 int32_t inloopcount;
Charles MacNeill 7:1add29d51e72 3849 int32_t IncRounding;
Charles MacNeill 7:1add29d51e72 3850 int16_t meanDistance_mm;
Charles MacNeill 7:1add29d51e72 3851 VL53L1_RangingMeasurementData_t RangingMeasurementData;
Charles MacNeill 7:1add29d51e72 3852 VL53L1_LLDriverData_t *pdev;
Charles MacNeill 7:1add29d51e72 3853 uint8_t goodmeas;
Charles MacNeill 7:1add29d51e72 3854 VL53L1_PresetModes currentMode;
Charles MacNeill 7:1add29d51e72 3855 VL53L1_DistanceModes currentDist;
Charles MacNeill 7:1add29d51e72 3856 VL53L1_DistanceModes DistMode[3] = {VL53L1_DISTANCEMODE_SHORT,
Charles MacNeill 7:1add29d51e72 3857 VL53L1_DISTANCEMODE_MEDIUM, VL53L1_DISTANCEMODE_LONG};
Charles MacNeill 7:1add29d51e72 3858 int16_t offsetA[3];
Charles MacNeill 7:1add29d51e72 3859 int16_t offsetB[3];
Charles MacNeill 7:1add29d51e72 3860
Charles MacNeill 7:1add29d51e72 3861 VL53L1_Error SmudgeStatus = VL53L1_ERROR_NONE;
Charles MacNeill 7:1add29d51e72 3862 uint8_t smudge_corr_en, isc;
Charles MacNeill 7:1add29d51e72 3863
Charles MacNeill 7:1add29d51e72 3864 LOG_FUNCTION_START("");
Charles MacNeill 7:1add29d51e72 3865
Charles MacNeill 7:1add29d51e72 3866 pdev = VL53L1DevStructGetLLDriverHandle(Dev);
Charles MacNeill 7:1add29d51e72 3867
Charles MacNeill 7:1add29d51e72 3868 smudge_corr_en = pdev->smudge_correct_config.smudge_corr_enabled;
Charles MacNeill 7:1add29d51e72 3869 SmudgeStatus = VL53L1_dynamic_xtalk_correction_disable(Dev);
Charles MacNeill 7:1add29d51e72 3870
Charles MacNeill 7:1add29d51e72 3871 pdev->customer.algo__part_to_part_range_offset_mm = 0;
Charles MacNeill 7:1add29d51e72 3872 pdev->customer.mm_config__inner_offset_mm = 0;
Charles MacNeill 7:1add29d51e72 3873 pdev->customer.mm_config__outer_offset_mm = 0;
Charles MacNeill 7:1add29d51e72 3874 pdev->customer.mm_config__outer_offset_mm = 0;
Charles MacNeill 7:1add29d51e72 3875 memset(&pdev->per_vcsel_cal_data, 0, sizeof(pdev->per_vcsel_cal_data));
Charles MacNeill 7:1add29d51e72 3876
Charles MacNeill 7:1add29d51e72 3877 Repeat = 0;
Charles MacNeill 7:1add29d51e72 3878 Max = 2 * BDTable[
Charles MacNeill 7:1add29d51e72 3879 VL53L1_TUNING_MAX_SIMPLE_OFFSET_CALIBRATION_SAMPLE_NUMBER];
Charles MacNeill 7:1add29d51e72 3880 UnderMax = 1 + (Max / 2);
Charles MacNeill 7:1add29d51e72 3881 OverMax = Max + (Max / 2);
Charles MacNeill 7:1add29d51e72 3882
Charles MacNeill 7:1add29d51e72 3883 Status = VL53L1_GetPresetMode(Dev, &currentMode);
Charles MacNeill 7:1add29d51e72 3884 Status = VL53L1_GetDistanceMode(Dev, &currentDist);
Charles MacNeill 7:1add29d51e72 3885
Charles MacNeill 7:1add29d51e72 3886 while ((Repeat < 3) && (Status == VL53L1_ERROR_NONE)) {
Charles MacNeill 7:1add29d51e72 3887 Status = VL53L1_SetDistanceMode(Dev, DistMode[Repeat]);
Charles MacNeill 7:1add29d51e72 3888 Status = VL53L1_StartMeasurement(Dev);
Charles MacNeill 7:1add29d51e72 3889
Charles MacNeill 7:1add29d51e72 3890 if (Status == VL53L1_ERROR_NONE) {
Charles MacNeill 7:1add29d51e72 3891 VL53L1_WaitMeasurementDataReady(Dev);
Charles MacNeill 7:1add29d51e72 3892 VL53L1_GetRangingMeasurementData(Dev,
Charles MacNeill 7:1add29d51e72 3893 &RangingMeasurementData);
Charles MacNeill 7:1add29d51e72 3894 VL53L1_ClearInterruptAndStartMeasurement(Dev);
Charles MacNeill 7:1add29d51e72 3895 }
Charles MacNeill 7:1add29d51e72 3896
Charles MacNeill 7:1add29d51e72 3897 inloopcount = 0;
Charles MacNeill 7:1add29d51e72 3898 offset_meas_range_A = 0;
Charles MacNeill 7:1add29d51e72 3899 sum_ranging_range_A = 0;
Charles MacNeill 7:1add29d51e72 3900 offset_meas_range_B = 0;
Charles MacNeill 7:1add29d51e72 3901 sum_ranging_range_B = 0;
Charles MacNeill 7:1add29d51e72 3902 while ((Status == VL53L1_ERROR_NONE) && (inloopcount < Max) &&
Charles MacNeill 7:1add29d51e72 3903 (inloopcount < OverMax)) {
Charles MacNeill 7:1add29d51e72 3904 Status = VL53L1_WaitMeasurementDataReady(Dev);
Charles MacNeill 7:1add29d51e72 3905 if (Status == VL53L1_ERROR_NONE)
Charles MacNeill 7:1add29d51e72 3906 Status = VL53L1_GetRangingMeasurementData(Dev,
Charles MacNeill 7:1add29d51e72 3907 &RangingMeasurementData);
Charles MacNeill 7:1add29d51e72 3908 goodmeas = (RangingMeasurementData.RangeStatus ==
Charles MacNeill 7:1add29d51e72 3909 VL53L1_RANGESTATUS_RANGE_VALID);
Charles MacNeill 7:1add29d51e72 3910 isc = pdev->ll_state.cfg_internal_stream_count;
Charles MacNeill 7:1add29d51e72 3911 if ((Status == VL53L1_ERROR_NONE) && goodmeas) {
Charles MacNeill 7:1add29d51e72 3912 if (isc & 0x01) {
Charles MacNeill 7:1add29d51e72 3913 sum_ranging_range_A +=
Charles MacNeill 7:1add29d51e72 3914 RangingMeasurementData.RangeMilliMeter;
Charles MacNeill 7:1add29d51e72 3915 offset_meas_range_A++;
Charles MacNeill 7:1add29d51e72 3916 } else {
Charles MacNeill 7:1add29d51e72 3917 sum_ranging_range_B +=
Charles MacNeill 7:1add29d51e72 3918 RangingMeasurementData.RangeMilliMeter;
Charles MacNeill 7:1add29d51e72 3919 offset_meas_range_B++;
Charles MacNeill 7:1add29d51e72 3920 }
Charles MacNeill 7:1add29d51e72 3921 inloopcount = offset_meas_range_A +
Charles MacNeill 7:1add29d51e72 3922 offset_meas_range_B;
Charles MacNeill 7:1add29d51e72 3923 }
Charles MacNeill 7:1add29d51e72 3924 Status = VL53L1_ClearInterruptAndStartMeasurement(Dev);
Charles MacNeill 7:1add29d51e72 3925 }
Charles MacNeill 7:1add29d51e72 3926
Charles MacNeill 7:1add29d51e72 3927
Charles MacNeill 7:1add29d51e72 3928 if (inloopcount < UnderMax)
Charles MacNeill 7:1add29d51e72 3929 Status = VL53L1_ERROR_OFFSET_CAL_NO_SAMPLE_FAIL;
Charles MacNeill 7:1add29d51e72 3930
Charles MacNeill 7:1add29d51e72 3931 VL53L1_StopMeasurement(Dev);
Charles MacNeill 7:1add29d51e72 3932
Charles MacNeill 7:1add29d51e72 3933
Charles MacNeill 7:1add29d51e72 3934 if ((sum_ranging_range_A < 0) ||
Charles MacNeill 7:1add29d51e72 3935 (sum_ranging_range_B < 0) ||
Charles MacNeill 7:1add29d51e72 3936 (sum_ranging_range_A >
Charles MacNeill 7:1add29d51e72 3937 ((int32_t) offset_meas_range_A * 0xffff)) ||
Charles MacNeill 7:1add29d51e72 3938 (sum_ranging_range_B >
Charles MacNeill 7:1add29d51e72 3939 ((int32_t) offset_meas_range_B * 0xffff))) {
Charles MacNeill 7:1add29d51e72 3940 Status = VL53L1_WARNING_OFFSET_CAL_SIGMA_TOO_HIGH;
Charles MacNeill 7:1add29d51e72 3941 }
Charles MacNeill 7:1add29d51e72 3942
Charles MacNeill 7:1add29d51e72 3943 if ((Status == VL53L1_ERROR_NONE) &&
Charles MacNeill 7:1add29d51e72 3944 (offset_meas_range_A > 0)) {
Charles MacNeill 7:1add29d51e72 3945 IncRounding = offset_meas_range_A / 2;
Charles MacNeill 7:1add29d51e72 3946 meanDistance_mm = (int16_t)
Charles MacNeill 7:1add29d51e72 3947 ((sum_ranging_range_A + IncRounding)
Charles MacNeill 7:1add29d51e72 3948 / offset_meas_range_A);
Charles MacNeill 7:1add29d51e72 3949 offsetA[Repeat] = (int16_t)
Charles MacNeill 7:1add29d51e72 3950 CalDistanceMilliMeter - meanDistance_mm;
Charles MacNeill 7:1add29d51e72 3951 }
Charles MacNeill 7:1add29d51e72 3952
Charles MacNeill 7:1add29d51e72 3953 if ((Status == VL53L1_ERROR_NONE) &&
Charles MacNeill 7:1add29d51e72 3954 (offset_meas_range_B > 0)) {
Charles MacNeill 7:1add29d51e72 3955 IncRounding = offset_meas_range_B / 2;
Charles MacNeill 7:1add29d51e72 3956 meanDistance_mm = (int16_t)
Charles MacNeill 7:1add29d51e72 3957 ((sum_ranging_range_B + IncRounding)
Charles MacNeill 7:1add29d51e72 3958 / offset_meas_range_B);
Charles MacNeill 7:1add29d51e72 3959 offsetB[Repeat] = (int16_t)
Charles MacNeill 7:1add29d51e72 3960 CalDistanceMilliMeter - meanDistance_mm;
Charles MacNeill 7:1add29d51e72 3961 }
Charles MacNeill 7:1add29d51e72 3962 Repeat++;
Charles MacNeill 7:1add29d51e72 3963 }
Charles MacNeill 7:1add29d51e72 3964
Charles MacNeill 7:1add29d51e72 3965 if ((SmudgeStatus == VL53L1_ERROR_NONE) && (smudge_corr_en == 1))
Charles MacNeill 7:1add29d51e72 3966 SmudgeStatus = VL53L1_dynamic_xtalk_correction_enable(Dev);
Charles MacNeill 7:1add29d51e72 3967
Charles MacNeill 7:1add29d51e72 3968 if (Status == VL53L1_ERROR_NONE) {
Charles MacNeill 7:1add29d51e72 3969 pdev->per_vcsel_cal_data.short_a_offset_mm = offsetA[0];
Charles MacNeill 7:1add29d51e72 3970 pdev->per_vcsel_cal_data.short_b_offset_mm = offsetB[0];
Charles MacNeill 7:1add29d51e72 3971 pdev->per_vcsel_cal_data.medium_a_offset_mm = offsetA[1];
Charles MacNeill 7:1add29d51e72 3972 pdev->per_vcsel_cal_data.medium_b_offset_mm = offsetB[1];
Charles MacNeill 7:1add29d51e72 3973 pdev->per_vcsel_cal_data.long_a_offset_mm = offsetA[2];
Charles MacNeill 7:1add29d51e72 3974 pdev->per_vcsel_cal_data.long_b_offset_mm = offsetB[2];
Charles MacNeill 7:1add29d51e72 3975 }
Charles MacNeill 7:1add29d51e72 3976
Charles MacNeill 7:1add29d51e72 3977 VL53L1_SetPresetMode(Dev, currentMode);
Charles MacNeill 7:1add29d51e72 3978 VL53L1_SetDistanceMode(Dev, currentDist);
Charles MacNeill 7:1add29d51e72 3979
Charles MacNeill 7:1add29d51e72 3980 LOG_FUNCTION_END(Status);
Charles MacNeill 7:1add29d51e72 3981 return Status;
Charles MacNeill 7:1add29d51e72 3982 }
Charles MacNeill 7:1add29d51e72 3983
Charles MacNeill 7:1add29d51e72 3984