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:
lugandc
Date:
Wed Jul 21 17:06:38 2021 +0200
Revision:
18:0696efe39d08
Parent:
7:1add29d51e72
Cleanup i2c functions, removed all bad references to L1X
Cleanup VL53L1CB class:
- i2c device object is passed in a consistent way in MyDevice structure
- removed useless functions
Updated VL53L1CB component driver with bare driver release 6.6.7 content

Who changed what in which revision?

UserRevisionLine numberNew contents of line
charlesmn 0:3ac96e360672 1
Charles MacNeill 7:1add29d51e72 2 // SPDX-License-Identifier: GPL-2.0+ OR BSD-3-Clause
Charles MacNeill 7:1add29d51e72 3 /******************************************************************************
charlesmn 0:3ac96e360672 4 * Copyright (c) 2020, STMicroelectronics - All Rights Reserved
charlesmn 0:3ac96e360672 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+
charlesmn 0:3ac96e360672 8 or 'BSD 3-clause "New" or "Revised" License' , at your option.
Charles MacNeill 7:1add29d51e72 9 ******************************************************************************
Charles MacNeill 7:1add29d51e72 10 */
charlesmn 0:3ac96e360672 11
charlesmn 0:3ac96e360672 12
charlesmn 0:3ac96e360672 13
charlesmn 0:3ac96e360672 14
charlesmn 0:3ac96e360672 15
charlesmn 0:3ac96e360672 16 #include "vl53l1_ll_def.h"
charlesmn 0:3ac96e360672 17 #include "vl53l1_ll_device.h"
charlesmn 0:3ac96e360672 18 #include "vl53l1_platform.h"
charlesmn 0:3ac96e360672 19 #include "vl53l1_platform_ipp.h"
charlesmn 0:3ac96e360672 20 #include "vl53l1_register_map.h"
charlesmn 0:3ac96e360672 21 #include "vl53l1_register_funcs.h"
charlesmn 0:3ac96e360672 22 #include "vl53l1_register_settings.h"
charlesmn 0:3ac96e360672 23 #include "vl53l1_hist_map.h"
charlesmn 0:3ac96e360672 24 #include "vl53l1_hist_structs.h"
charlesmn 0:3ac96e360672 25 #include "vl53l1_core.h"
charlesmn 0:3ac96e360672 26 #include "vl53l1_wait.h"
charlesmn 0:3ac96e360672 27 #include "vl53l1_api_preset_modes.h"
charlesmn 0:3ac96e360672 28 #include "vl53l1_silicon_core.h"
charlesmn 0:3ac96e360672 29 #include "vl53l1_api_core.h"
charlesmn 0:3ac96e360672 30 #include "vl53l1_api_calibration.h"
charlesmn 0:3ac96e360672 31
charlesmn 0:3ac96e360672 32 #ifdef VL53L1_LOG_ENABLE
charlesmn 0:3ac96e360672 33 #include "vl53l1_api_debug.h"
charlesmn 0:3ac96e360672 34 #endif
charlesmn 0:3ac96e360672 35
charlesmn 0:3ac96e360672 36
charlesmn 0:3ac96e360672 37 #define LOG_FUNCTION_START(fmt, ...) \
charlesmn 0:3ac96e360672 38 _LOG_FUNCTION_START(VL53L1_TRACE_MODULE_CORE, fmt, ##__VA_ARGS__)
charlesmn 0:3ac96e360672 39 #define LOG_FUNCTION_END(status, ...) \
charlesmn 0:3ac96e360672 40 _LOG_FUNCTION_END(VL53L1_TRACE_MODULE_CORE, status, ##__VA_ARGS__)
charlesmn 0:3ac96e360672 41 #define LOG_FUNCTION_END_FMT(status, fmt, ...) \
charlesmn 0:3ac96e360672 42 _LOG_FUNCTION_END_FMT(VL53L1_TRACE_MODULE_CORE, status, \
charlesmn 0:3ac96e360672 43 fmt, ##__VA_ARGS__)
charlesmn 0:3ac96e360672 44
charlesmn 0:3ac96e360672 45 #define trace_print(level, ...) \
charlesmn 0:3ac96e360672 46 _LOG_TRACE_PRINT(VL53L1_TRACE_MODULE_CORE, \
charlesmn 0:3ac96e360672 47 level, VL53L1_TRACE_FUNCTION_NONE, ##__VA_ARGS__)
charlesmn 0:3ac96e360672 48
charlesmn 0:3ac96e360672 49
charlesmn 0:3ac96e360672 50 VL53L1_Error VL53L1_run_ref_spad_char(
charlesmn 0:3ac96e360672 51 VL53L1_DEV Dev,
charlesmn 0:3ac96e360672 52 VL53L1_Error *pcal_status)
charlesmn 0:3ac96e360672 53 {
charlesmn 0:3ac96e360672 54
charlesmn 0:3ac96e360672 55
charlesmn 0:3ac96e360672 56 VL53L1_Error status = VL53L1_ERROR_NONE;
charlesmn 0:3ac96e360672 57 VL53L1_LLDriverData_t *pdev = VL53L1DevStructGetLLDriverHandle(Dev);
charlesmn 0:3ac96e360672 58
charlesmn 0:3ac96e360672 59 uint8_t comms_buffer[6];
charlesmn 0:3ac96e360672 60
charlesmn 0:3ac96e360672 61 VL53L1_refspadchar_config_t *prefspadchar = &(pdev->refspadchar);
charlesmn 0:3ac96e360672 62
charlesmn 0:3ac96e360672 63 LOG_FUNCTION_START("");
charlesmn 0:3ac96e360672 64
charlesmn 0:3ac96e360672 65
charlesmn 0:3ac96e360672 66
charlesmn 0:3ac96e360672 67 if (status == VL53L1_ERROR_NONE)
charlesmn 0:3ac96e360672 68 status = VL53L1_enable_powerforce(Dev);
charlesmn 0:3ac96e360672 69
charlesmn 0:3ac96e360672 70
charlesmn 0:3ac96e360672 71
charlesmn 0:3ac96e360672 72 if (status == VL53L1_ERROR_NONE)
charlesmn 0:3ac96e360672 73 status =
charlesmn 0:3ac96e360672 74 VL53L1_set_ref_spad_char_config(
charlesmn 0:3ac96e360672 75 Dev,
charlesmn 0:3ac96e360672 76 prefspadchar->VL53L1_p_009,
charlesmn 0:3ac96e360672 77 prefspadchar->timeout_us,
charlesmn 0:3ac96e360672 78 prefspadchar->target_count_rate_mcps,
charlesmn 0:3ac96e360672 79 prefspadchar->max_count_rate_limit_mcps,
charlesmn 0:3ac96e360672 80 prefspadchar->min_count_rate_limit_mcps,
charlesmn 0:3ac96e360672 81 pdev->stat_nvm.osc_measured__fast_osc__frequency);
charlesmn 0:3ac96e360672 82
charlesmn 0:3ac96e360672 83
charlesmn 0:3ac96e360672 84
charlesmn 0:3ac96e360672 85 if (status == VL53L1_ERROR_NONE)
charlesmn 0:3ac96e360672 86 status = VL53L1_run_device_test(
charlesmn 0:3ac96e360672 87 Dev,
charlesmn 0:3ac96e360672 88 prefspadchar->device_test_mode);
charlesmn 0:3ac96e360672 89
charlesmn 0:3ac96e360672 90
charlesmn 0:3ac96e360672 91
charlesmn 0:3ac96e360672 92 if (status == VL53L1_ERROR_NONE)
charlesmn 0:3ac96e360672 93 status =
charlesmn 0:3ac96e360672 94 VL53L1_ReadMulti(
charlesmn 0:3ac96e360672 95 Dev,
charlesmn 0:3ac96e360672 96 VL53L1_REF_SPAD_CHAR_RESULT__NUM_ACTUAL_REF_SPADS,
charlesmn 0:3ac96e360672 97 comms_buffer,
charlesmn 0:3ac96e360672 98 2);
charlesmn 0:3ac96e360672 99
charlesmn 0:3ac96e360672 100 if (status == VL53L1_ERROR_NONE) {
charlesmn 0:3ac96e360672 101 pdev->dbg_results.ref_spad_char_result__num_actual_ref_spads =
charlesmn 0:3ac96e360672 102 comms_buffer[0];
charlesmn 0:3ac96e360672 103 pdev->dbg_results.ref_spad_char_result__ref_location =
charlesmn 0:3ac96e360672 104 comms_buffer[1];
charlesmn 0:3ac96e360672 105 }
charlesmn 0:3ac96e360672 106
charlesmn 0:3ac96e360672 107
charlesmn 0:3ac96e360672 108
charlesmn 0:3ac96e360672 109 if (status == VL53L1_ERROR_NONE)
charlesmn 0:3ac96e360672 110 status =
charlesmn 0:3ac96e360672 111 VL53L1_WriteMulti(
charlesmn 0:3ac96e360672 112 Dev,
charlesmn 0:3ac96e360672 113 VL53L1_REF_SPAD_MAN__NUM_REQUESTED_REF_SPADS,
charlesmn 0:3ac96e360672 114 comms_buffer,
charlesmn 0:3ac96e360672 115 2);
charlesmn 0:3ac96e360672 116
charlesmn 0:3ac96e360672 117 if (status == VL53L1_ERROR_NONE) {
charlesmn 0:3ac96e360672 118 pdev->customer.ref_spad_man__num_requested_ref_spads =
charlesmn 0:3ac96e360672 119 comms_buffer[0];
charlesmn 0:3ac96e360672 120 pdev->customer.ref_spad_man__ref_location =
charlesmn 0:3ac96e360672 121 comms_buffer[1];
charlesmn 0:3ac96e360672 122 }
charlesmn 0:3ac96e360672 123
charlesmn 0:3ac96e360672 124
charlesmn 0:3ac96e360672 125
charlesmn 0:3ac96e360672 126 if (status == VL53L1_ERROR_NONE)
charlesmn 0:3ac96e360672 127 status =
charlesmn 0:3ac96e360672 128 VL53L1_ReadMulti(
charlesmn 0:3ac96e360672 129 Dev,
charlesmn 0:3ac96e360672 130 VL53L1_RESULT__SPARE_0_SD1,
charlesmn 0:3ac96e360672 131 comms_buffer,
charlesmn 0:3ac96e360672 132 6);
charlesmn 0:3ac96e360672 133
charlesmn 0:3ac96e360672 134
charlesmn 0:3ac96e360672 135
charlesmn 0:3ac96e360672 136 if (status == VL53L1_ERROR_NONE)
charlesmn 0:3ac96e360672 137 status =
charlesmn 0:3ac96e360672 138 VL53L1_WriteMulti(
charlesmn 0:3ac96e360672 139 Dev,
charlesmn 0:3ac96e360672 140 VL53L1_GLOBAL_CONFIG__SPAD_ENABLES_REF_0,
charlesmn 0:3ac96e360672 141 comms_buffer,
charlesmn 0:3ac96e360672 142 6);
charlesmn 0:3ac96e360672 143
charlesmn 0:3ac96e360672 144 if (status == VL53L1_ERROR_NONE) {
charlesmn 0:3ac96e360672 145 pdev->customer.global_config__spad_enables_ref_0 =
charlesmn 0:3ac96e360672 146 comms_buffer[0];
charlesmn 0:3ac96e360672 147 pdev->customer.global_config__spad_enables_ref_1 =
charlesmn 0:3ac96e360672 148 comms_buffer[1];
charlesmn 0:3ac96e360672 149 pdev->customer.global_config__spad_enables_ref_2 =
charlesmn 0:3ac96e360672 150 comms_buffer[2];
charlesmn 0:3ac96e360672 151 pdev->customer.global_config__spad_enables_ref_3 =
charlesmn 0:3ac96e360672 152 comms_buffer[3];
charlesmn 0:3ac96e360672 153 pdev->customer.global_config__spad_enables_ref_4 =
charlesmn 0:3ac96e360672 154 comms_buffer[4];
charlesmn 0:3ac96e360672 155 pdev->customer.global_config__spad_enables_ref_5 =
charlesmn 0:3ac96e360672 156 comms_buffer[5];
charlesmn 0:3ac96e360672 157 }
charlesmn 0:3ac96e360672 158
charlesmn 0:3ac96e360672 159 #ifdef VL53L1_LOG_ENABLE
charlesmn 0:3ac96e360672 160
charlesmn 0:3ac96e360672 161 if (status == VL53L1_ERROR_NONE)
charlesmn 0:3ac96e360672 162 VL53L1_print_customer_nvm_managed(
charlesmn 0:3ac96e360672 163 &(pdev->customer),
charlesmn 0:3ac96e360672 164 "run_ref_spad_char():pdev->lldata.customer.",
charlesmn 0:3ac96e360672 165 VL53L1_TRACE_MODULE_REF_SPAD_CHAR);
charlesmn 0:3ac96e360672 166 #endif
charlesmn 0:3ac96e360672 167
charlesmn 0:3ac96e360672 168 if (status == VL53L1_ERROR_NONE) {
charlesmn 0:3ac96e360672 169
charlesmn 0:3ac96e360672 170 switch (pdev->sys_results.result__range_status) {
charlesmn 0:3ac96e360672 171
charlesmn 0:3ac96e360672 172 case VL53L1_DEVICEERROR_REFSPADCHARNOTENOUGHDPADS:
charlesmn 0:3ac96e360672 173 status = VL53L1_WARNING_REF_SPAD_CHAR_NOT_ENOUGH_SPADS;
charlesmn 0:3ac96e360672 174 break;
charlesmn 0:3ac96e360672 175
charlesmn 0:3ac96e360672 176 case VL53L1_DEVICEERROR_REFSPADCHARMORETHANTARGET:
charlesmn 0:3ac96e360672 177 status = VL53L1_WARNING_REF_SPAD_CHAR_RATE_TOO_HIGH;
charlesmn 0:3ac96e360672 178 break;
charlesmn 0:3ac96e360672 179
charlesmn 0:3ac96e360672 180 case VL53L1_DEVICEERROR_REFSPADCHARLESSTHANTARGET:
charlesmn 0:3ac96e360672 181 status = VL53L1_WARNING_REF_SPAD_CHAR_RATE_TOO_LOW;
charlesmn 0:3ac96e360672 182 break;
charlesmn 0:3ac96e360672 183 }
charlesmn 0:3ac96e360672 184 }
charlesmn 0:3ac96e360672 185
charlesmn 0:3ac96e360672 186
charlesmn 0:3ac96e360672 187
charlesmn 0:3ac96e360672 188 *pcal_status = status;
charlesmn 0:3ac96e360672 189
charlesmn 0:3ac96e360672 190
charlesmn 0:3ac96e360672 191
charlesmn 0:3ac96e360672 192 IGNORE_STATUS(
charlesmn 0:3ac96e360672 193 IGNORE_REF_SPAD_CHAR_NOT_ENOUGH_SPADS,
charlesmn 0:3ac96e360672 194 VL53L1_WARNING_REF_SPAD_CHAR_NOT_ENOUGH_SPADS,
charlesmn 0:3ac96e360672 195 status);
charlesmn 0:3ac96e360672 196
charlesmn 0:3ac96e360672 197 IGNORE_STATUS(
charlesmn 0:3ac96e360672 198 IGNORE_REF_SPAD_CHAR_RATE_TOO_HIGH,
charlesmn 0:3ac96e360672 199 VL53L1_WARNING_REF_SPAD_CHAR_RATE_TOO_HIGH,
charlesmn 0:3ac96e360672 200 status);
charlesmn 0:3ac96e360672 201
charlesmn 0:3ac96e360672 202 IGNORE_STATUS(
charlesmn 0:3ac96e360672 203 IGNORE_REF_SPAD_CHAR_RATE_TOO_LOW,
charlesmn 0:3ac96e360672 204 VL53L1_WARNING_REF_SPAD_CHAR_RATE_TOO_LOW,
charlesmn 0:3ac96e360672 205 status);
charlesmn 0:3ac96e360672 206
charlesmn 0:3ac96e360672 207
charlesmn 0:3ac96e360672 208 LOG_FUNCTION_END(status);
charlesmn 0:3ac96e360672 209
charlesmn 0:3ac96e360672 210 return status;
charlesmn 0:3ac96e360672 211 }
charlesmn 0:3ac96e360672 212
charlesmn 0:3ac96e360672 213
charlesmn 0:3ac96e360672 214 VL53L1_Error VL53L1_run_xtalk_extraction(
charlesmn 0:3ac96e360672 215 VL53L1_DEV Dev,
charlesmn 0:3ac96e360672 216 VL53L1_Error *pcal_status)
charlesmn 0:3ac96e360672 217 {
charlesmn 0:3ac96e360672 218
charlesmn 0:3ac96e360672 219
charlesmn 0:3ac96e360672 220 VL53L1_Error status = VL53L1_ERROR_NONE;
charlesmn 0:3ac96e360672 221 VL53L1_LLDriverData_t *pdev =
charlesmn 0:3ac96e360672 222 VL53L1DevStructGetLLDriverHandle(Dev);
charlesmn 0:3ac96e360672 223
charlesmn 0:3ac96e360672 224
charlesmn 0:3ac96e360672 225
charlesmn 0:3ac96e360672 226 VL53L1_xtalkextract_config_t *pX = &(pdev->xtalk_extract_cfg);
charlesmn 0:3ac96e360672 227 VL53L1_xtalk_config_t *pC = &(pdev->xtalk_cfg);
charlesmn 0:3ac96e360672 228 VL53L1_xtalk_calibration_results_t *pXC = &(pdev->xtalk_cal);
charlesmn 0:3ac96e360672 229
charlesmn 0:3ac96e360672 230 uint8_t results_invalid = 0;
charlesmn 0:3ac96e360672 231
charlesmn 0:3ac96e360672 232 uint8_t i = 0;
charlesmn 0:3ac96e360672 233 uint16_t tmp16 = 0;
charlesmn 0:3ac96e360672 234
charlesmn 0:3ac96e360672 235 uint8_t measurement_mode = VL53L1_DEVICEMEASUREMENTMODE_BACKTOBACK;
charlesmn 0:3ac96e360672 236
charlesmn 0:3ac96e360672 237 LOG_FUNCTION_START("");
charlesmn 0:3ac96e360672 238
charlesmn 0:3ac96e360672 239
charlesmn 0:3ac96e360672 240
charlesmn 0:3ac96e360672 241
charlesmn 0:3ac96e360672 242
charlesmn 0:3ac96e360672 243 VL53L1_init_histogram_bin_data_struct(
charlesmn 0:3ac96e360672 244 0,
charlesmn 0:3ac96e360672 245 (uint16_t)VL53L1_HISTOGRAM_BUFFER_SIZE,
charlesmn 0:3ac96e360672 246 &(pdev->xtalk_results.central_histogram_avg));
charlesmn 0:3ac96e360672 247
charlesmn 0:3ac96e360672 248 VL53L1_init_histogram_bin_data_struct(
charlesmn 0:3ac96e360672 249 0,
charlesmn 0:3ac96e360672 250 (uint16_t)VL53L1_HISTOGRAM_BUFFER_SIZE,
charlesmn 0:3ac96e360672 251 &(pdev->xtalk_results.central_histogram_sum));
charlesmn 0:3ac96e360672 252
charlesmn 0:3ac96e360672 253
charlesmn 0:3ac96e360672 254
charlesmn 0:3ac96e360672 255 if (status == VL53L1_ERROR_NONE)
charlesmn 0:3ac96e360672 256 status =
charlesmn 0:3ac96e360672 257 VL53L1_set_preset_mode(
charlesmn 0:3ac96e360672 258 Dev,
charlesmn 0:3ac96e360672 259 VL53L1_DEVICEPRESETMODE_HISTOGRAM_XTALK_PLANAR,
charlesmn 0:3ac96e360672 260
charlesmn 0:3ac96e360672 261 pX->dss_config__target_total_rate_mcps,
charlesmn 0:3ac96e360672 262 pX->phasecal_config_timeout_us,
charlesmn 0:3ac96e360672 263 pX->mm_config_timeout_us,
charlesmn 0:3ac96e360672 264 pX->range_config_timeout_us,
charlesmn 0:3ac96e360672 265
charlesmn 0:3ac96e360672 266 100);
charlesmn 0:3ac96e360672 267
charlesmn 0:3ac96e360672 268
charlesmn 0:3ac96e360672 269
charlesmn 0:3ac96e360672 270 if (status == VL53L1_ERROR_NONE)
charlesmn 0:3ac96e360672 271 status = VL53L1_disable_xtalk_compensation(Dev);
charlesmn 0:3ac96e360672 272
charlesmn 0:3ac96e360672 273
charlesmn 0:3ac96e360672 274
charlesmn 0:3ac96e360672 275 pdev->xtalk_results.max_results = VL53L1_MAX_XTALK_RANGE_RESULTS;
charlesmn 0:3ac96e360672 276 pdev->xtalk_results.active_results = pdev->zone_cfg.active_zones+1;
charlesmn 0:3ac96e360672 277
charlesmn 0:3ac96e360672 278
charlesmn 0:3ac96e360672 279
charlesmn 0:3ac96e360672 280 pdev->xtalk_results.central_histogram__window_start = 0xFF;
charlesmn 0:3ac96e360672 281 pdev->xtalk_results.central_histogram__window_end = 0x00;
charlesmn 0:3ac96e360672 282
charlesmn 0:3ac96e360672 283 pdev->xtalk_results.num_of_samples_status = 0x00;
charlesmn 0:3ac96e360672 284 pdev->xtalk_results.zero_samples_status = 0x00;
charlesmn 0:3ac96e360672 285 pdev->xtalk_results.max_sigma_status = 0x00;
charlesmn 0:3ac96e360672 286
charlesmn 0:3ac96e360672 287 for (i = 0; i < pdev->xtalk_results.max_results; i++) {
charlesmn 0:3ac96e360672 288 pdev->xtalk_results.VL53L1_p_002[i].no_of_samples = 0;
charlesmn 0:3ac96e360672 289 pdev->xtalk_results.VL53L1_p_002[i].signal_total_events_avg = 0;
charlesmn 0:3ac96e360672 290 pdev->xtalk_results.VL53L1_p_002[i].signal_total_events_sum = 0;
charlesmn 0:3ac96e360672 291 pdev->xtalk_results.VL53L1_p_002[i].rate_per_spad_kcps_sum = 0;
charlesmn 0:3ac96e360672 292 pdev->xtalk_results.VL53L1_p_002[i].rate_per_spad_kcps_avg = 0;
charlesmn 0:3ac96e360672 293 pdev->xtalk_results.VL53L1_p_002[i].sigma_mm_sum = 0;
charlesmn 0:3ac96e360672 294 pdev->xtalk_results.VL53L1_p_002[i].sigma_mm_avg = 0;
charlesmn 0:3ac96e360672 295
charlesmn 0:3ac96e360672 296 pdev->xtalk_results.VL53L1_p_002[i].median_phase_sum = 0;
charlesmn 0:3ac96e360672 297 pdev->xtalk_results.VL53L1_p_002[i].median_phase_avg = 0;
charlesmn 0:3ac96e360672 298
charlesmn 0:3ac96e360672 299 }
charlesmn 0:3ac96e360672 300
charlesmn 0:3ac96e360672 301
charlesmn 0:3ac96e360672 302 if (status == VL53L1_ERROR_NONE) {
charlesmn 0:3ac96e360672 303
charlesmn 0:3ac96e360672 304 status =
charlesmn 0:3ac96e360672 305 VL53L1_get_and_avg_xtalk_samples(
charlesmn 0:3ac96e360672 306 Dev,
charlesmn 0:3ac96e360672 307
charlesmn 0:3ac96e360672 308 pX->num_of_samples,
charlesmn 0:3ac96e360672 309
charlesmn 0:3ac96e360672 310 measurement_mode,
charlesmn 0:3ac96e360672 311
charlesmn 0:3ac96e360672 312 pX->algo__crosstalk_extract_max_valid_range_mm,
charlesmn 0:3ac96e360672 313 pX->algo__crosstalk_extract_min_valid_range_mm,
charlesmn 0:3ac96e360672 314 pX->algo__crosstalk_extract_max_valid_rate_kcps,
charlesmn 0:3ac96e360672 315
charlesmn 0:3ac96e360672 316 0x0,
charlesmn 0:3ac96e360672 317 0x4,
charlesmn 0:3ac96e360672 318 &(pdev->xtalk_results),
charlesmn 0:3ac96e360672 319 &(pdev->xtalk_results.central_histogram_sum),
charlesmn 0:3ac96e360672 320 &(pdev->xtalk_results.central_histogram_avg));
charlesmn 0:3ac96e360672 321 }
charlesmn 0:3ac96e360672 322
charlesmn 0:3ac96e360672 323
charlesmn 0:3ac96e360672 324
charlesmn 0:3ac96e360672 325
charlesmn 0:3ac96e360672 326
charlesmn 0:3ac96e360672 327
charlesmn 0:3ac96e360672 328
charlesmn 0:3ac96e360672 329 if (status == VL53L1_ERROR_NONE)
charlesmn 0:3ac96e360672 330 if ((pdev->xtalk_results.VL53L1_p_002[4].no_of_samples == 0) ||
charlesmn 0:3ac96e360672 331 (pdev->xtalk_results.VL53L1_p_002[4].sigma_mm_avg >
charlesmn 0:3ac96e360672 332 ((uint32_t)pX->algo__crosstalk_extract_max_sigma_mm
charlesmn 0:3ac96e360672 333 << 5)))
charlesmn 0:3ac96e360672 334 results_invalid = 0x01;
charlesmn 0:3ac96e360672 335
charlesmn 0:3ac96e360672 336
charlesmn 0:3ac96e360672 337
charlesmn 0:3ac96e360672 338 #ifdef VL53L1_LOG_ENABLE
charlesmn 0:3ac96e360672 339 if (status == VL53L1_ERROR_NONE)
charlesmn 0:3ac96e360672 340 VL53L1_print_xtalk_range_results(
charlesmn 0:3ac96e360672 341 &(pdev->xtalk_results),
charlesmn 0:3ac96e360672 342 "pdev->xtalk_results",
charlesmn 0:3ac96e360672 343 VL53L1_TRACE_MODULE_CORE);
charlesmn 0:3ac96e360672 344 #endif
charlesmn 0:3ac96e360672 345
charlesmn 0:3ac96e360672 346 if ((status == VL53L1_ERROR_NONE) && (results_invalid == 0)) {
charlesmn 0:3ac96e360672 347
charlesmn 0:3ac96e360672 348 status =
charlesmn 0:3ac96e360672 349 VL53L1_ipp_xtalk_calibration_process_data(
charlesmn 0:3ac96e360672 350 Dev,
charlesmn 0:3ac96e360672 351 &(pdev->xtalk_results),
charlesmn 0:3ac96e360672 352 &(pdev->xtalk_shapes),
charlesmn 0:3ac96e360672 353 &(pdev->xtalk_cal));
charlesmn 0:3ac96e360672 354
charlesmn 0:3ac96e360672 355 }
charlesmn 0:3ac96e360672 356 if ((status == VL53L1_ERROR_NONE) && (results_invalid == 0)) {
charlesmn 0:3ac96e360672 357 for (i = 0; i < VL53L1_BIN_REC_SIZE; i++)
charlesmn 0:3ac96e360672 358 pXC->algo__xtalk_cpo_HistoMerge_kcps[i] =
charlesmn 0:3ac96e360672 359 pXC->algo__crosstalk_compensation_plane_offset_kcps;
charlesmn 0:3ac96e360672 360 pC->algo__crosstalk_compensation_x_plane_gradient_kcps =
charlesmn 0:3ac96e360672 361 pXC->algo__crosstalk_compensation_x_plane_gradient_kcps;
charlesmn 0:3ac96e360672 362 pC->algo__crosstalk_compensation_y_plane_gradient_kcps =
charlesmn 0:3ac96e360672 363 pXC->algo__crosstalk_compensation_y_plane_gradient_kcps;
charlesmn 0:3ac96e360672 364 pC->algo__crosstalk_compensation_plane_offset_kcps =
charlesmn 0:3ac96e360672 365 pXC->algo__crosstalk_compensation_plane_offset_kcps;
charlesmn 0:3ac96e360672 366 }
charlesmn 0:3ac96e360672 367
charlesmn 0:3ac96e360672 368
charlesmn 0:3ac96e360672 369
charlesmn 0:3ac96e360672 370 if (status == VL53L1_ERROR_NONE)
charlesmn 0:3ac96e360672 371 status = VL53L1_enable_xtalk_compensation(Dev);
charlesmn 0:3ac96e360672 372
charlesmn 0:3ac96e360672 373
charlesmn 0:3ac96e360672 374
charlesmn 0:3ac96e360672 375
charlesmn 0:3ac96e360672 376 if (status == VL53L1_ERROR_NONE) {
charlesmn 0:3ac96e360672 377
charlesmn 0:3ac96e360672 378 for (i = 0; i < pdev->xtalk_results.max_results; i++) {
charlesmn 0:3ac96e360672 379
charlesmn 0:3ac96e360672 380 if (pdev->xtalk_results.VL53L1_p_002[i].no_of_samples !=
charlesmn 0:3ac96e360672 381
charlesmn 0:3ac96e360672 382 pX->num_of_samples) {
charlesmn 0:3ac96e360672 383
charlesmn 0:3ac96e360672 384 pdev->xtalk_results.num_of_samples_status =
charlesmn 0:3ac96e360672 385 pdev->xtalk_results.num_of_samples_status |
charlesmn 0:3ac96e360672 386 (1 << i);
charlesmn 0:3ac96e360672 387 }
charlesmn 0:3ac96e360672 388
charlesmn 0:3ac96e360672 389 if (pdev->xtalk_results.VL53L1_p_002[i].no_of_samples ==
charlesmn 0:3ac96e360672 390 0x00) {
charlesmn 0:3ac96e360672 391 pdev->xtalk_results.zero_samples_status =
charlesmn 0:3ac96e360672 392 pdev->xtalk_results.zero_samples_status |
charlesmn 0:3ac96e360672 393 (1 << i);
charlesmn 0:3ac96e360672 394 }
charlesmn 0:3ac96e360672 395
charlesmn 0:3ac96e360672 396
charlesmn 0:3ac96e360672 397
charlesmn 0:3ac96e360672 398
charlesmn 0:3ac96e360672 399 tmp16 = pX->algo__crosstalk_extract_max_sigma_mm;
charlesmn 0:3ac96e360672 400 if (pdev->xtalk_results.VL53L1_p_002[i].sigma_mm_avg >
charlesmn 0:3ac96e360672 401 ((uint32_t)tmp16 << 5)) {
charlesmn 0:3ac96e360672 402 pdev->xtalk_results.max_sigma_status =
charlesmn 0:3ac96e360672 403 pdev->xtalk_results.max_sigma_status |
charlesmn 0:3ac96e360672 404 (1 << i);
charlesmn 0:3ac96e360672 405 }
charlesmn 0:3ac96e360672 406
charlesmn 0:3ac96e360672 407 }
charlesmn 0:3ac96e360672 408 }
charlesmn 0:3ac96e360672 409
charlesmn 0:3ac96e360672 410
charlesmn 0:3ac96e360672 411 if (results_invalid > 0) {
charlesmn 0:3ac96e360672 412
charlesmn 0:3ac96e360672 413 if (pdev->xtalk_results.VL53L1_p_002[4].no_of_samples == 0) {
charlesmn 0:3ac96e360672 414 status = VL53L1_ERROR_XTALK_EXTRACTION_NO_SAMPLE_FAIL;
charlesmn 0:3ac96e360672 415 } else {
charlesmn 0:3ac96e360672 416
charlesmn 0:3ac96e360672 417
charlesmn 0:3ac96e360672 418 if (pdev->xtalk_results.VL53L1_p_002[4].sigma_mm_avg >
charlesmn 0:3ac96e360672 419 (((uint32_t)pX->algo__crosstalk_extract_max_sigma_mm)
charlesmn 0:3ac96e360672 420 << 5)) {
charlesmn 0:3ac96e360672 421 status =
charlesmn 0:3ac96e360672 422 VL53L1_ERROR_XTALK_EXTRACTION_SIGMA_LIMIT_FAIL;
charlesmn 0:3ac96e360672 423 }
charlesmn 0:3ac96e360672 424
charlesmn 0:3ac96e360672 425 }
charlesmn 0:3ac96e360672 426 } else {
charlesmn 0:3ac96e360672 427
charlesmn 0:3ac96e360672 428 if (pdev->xtalk_results.zero_samples_status != 0x00) {
charlesmn 0:3ac96e360672 429 status = VL53L1_WARNING_XTALK_NO_SAMPLES_FOR_GRADIENT;
charlesmn 0:3ac96e360672 430 } else {
charlesmn 0:3ac96e360672 431 if (pdev->xtalk_results.max_sigma_status != 0x00) {
charlesmn 0:3ac96e360672 432 status =
charlesmn 0:3ac96e360672 433 VL53L1_WARNING_XTALK_SIGMA_LIMIT_FOR_GRADIENT;
charlesmn 0:3ac96e360672 434 } else {
charlesmn 0:3ac96e360672 435 if (pdev->xtalk_results.num_of_samples_status !=
charlesmn 0:3ac96e360672 436 0x00)
charlesmn 0:3ac96e360672 437 status =
charlesmn 0:3ac96e360672 438 VL53L1_WARNING_XTALK_MISSING_SAMPLES;
charlesmn 0:3ac96e360672 439 }
charlesmn 0:3ac96e360672 440 }
charlesmn 0:3ac96e360672 441 }
charlesmn 0:3ac96e360672 442
charlesmn 0:3ac96e360672 443
charlesmn 0:3ac96e360672 444
charlesmn 0:3ac96e360672 445 pdev->xtalk_results.cal_status = status;
charlesmn 0:3ac96e360672 446 *pcal_status = pdev->xtalk_results.cal_status;
charlesmn 0:3ac96e360672 447
charlesmn 0:3ac96e360672 448
charlesmn 0:3ac96e360672 449
charlesmn 0:3ac96e360672 450 IGNORE_STATUS(
charlesmn 0:3ac96e360672 451 IGNORE_XTALK_EXTRACTION_NO_SAMPLE_FAIL,
charlesmn 0:3ac96e360672 452 VL53L1_ERROR_XTALK_EXTRACTION_NO_SAMPLE_FAIL,
charlesmn 0:3ac96e360672 453 status);
charlesmn 0:3ac96e360672 454
charlesmn 0:3ac96e360672 455 IGNORE_STATUS(
charlesmn 0:3ac96e360672 456 IGNORE_XTALK_EXTRACTION_SIGMA_LIMIT_FAIL,
charlesmn 0:3ac96e360672 457 VL53L1_ERROR_XTALK_EXTRACTION_SIGMA_LIMIT_FAIL,
charlesmn 0:3ac96e360672 458 status);
charlesmn 0:3ac96e360672 459
charlesmn 0:3ac96e360672 460 IGNORE_STATUS(
charlesmn 0:3ac96e360672 461 IGNORE_XTALK_EXTRACTION_NO_SAMPLE_FOR_GRADIENT_WARN,
charlesmn 0:3ac96e360672 462 VL53L1_WARNING_XTALK_NO_SAMPLES_FOR_GRADIENT,
charlesmn 0:3ac96e360672 463 status);
charlesmn 0:3ac96e360672 464
charlesmn 0:3ac96e360672 465 IGNORE_STATUS(
charlesmn 0:3ac96e360672 466 IGNORE_XTALK_EXTRACTION_SIGMA_LIMIT_FOR_GRADIENT_WARN,
charlesmn 0:3ac96e360672 467 VL53L1_WARNING_XTALK_SIGMA_LIMIT_FOR_GRADIENT,
charlesmn 0:3ac96e360672 468 status);
charlesmn 0:3ac96e360672 469
charlesmn 0:3ac96e360672 470 IGNORE_STATUS(
charlesmn 0:3ac96e360672 471 IGNORE_XTALK_EXTRACTION_MISSING_SAMPLES_WARN,
charlesmn 0:3ac96e360672 472 VL53L1_WARNING_XTALK_MISSING_SAMPLES,
charlesmn 0:3ac96e360672 473 status);
charlesmn 0:3ac96e360672 474
charlesmn 0:3ac96e360672 475 #ifdef VL53L1_LOG_ENABLE
charlesmn 0:3ac96e360672 476
charlesmn 0:3ac96e360672 477
charlesmn 0:3ac96e360672 478
charlesmn 0:3ac96e360672 479 VL53L1_print_customer_nvm_managed(
charlesmn 0:3ac96e360672 480 &(pdev->customer),
charlesmn 0:3ac96e360672 481 "run_xtalk_extraction():pdev->lldata.customer.",
charlesmn 0:3ac96e360672 482 VL53L1_TRACE_MODULE_XTALK_DATA);
charlesmn 0:3ac96e360672 483
charlesmn 0:3ac96e360672 484 VL53L1_print_xtalk_config(
charlesmn 0:3ac96e360672 485 &(pdev->xtalk_cfg),
charlesmn 0:3ac96e360672 486 "run_xtalk_extraction():pdev->lldata.xtalk_cfg.",
charlesmn 0:3ac96e360672 487 VL53L1_TRACE_MODULE_XTALK_DATA);
charlesmn 0:3ac96e360672 488
charlesmn 0:3ac96e360672 489 VL53L1_print_xtalk_extract_config(
charlesmn 0:3ac96e360672 490 &(pdev->xtalk_extract_cfg),
charlesmn 0:3ac96e360672 491 "run_xtalk_extraction():pdev->lldata.xtalk_extract_cfg.",
charlesmn 0:3ac96e360672 492 VL53L1_TRACE_MODULE_XTALK_DATA);
charlesmn 0:3ac96e360672 493
charlesmn 0:3ac96e360672 494 VL53L1_print_histogram_bin_data(
charlesmn 0:3ac96e360672 495 &(pdev->hist_data),
charlesmn 0:3ac96e360672 496 "run_xtalk_extraction():pdev->lldata.hist_data.",
charlesmn 0:3ac96e360672 497 VL53L1_TRACE_MODULE_XTALK_DATA);
charlesmn 0:3ac96e360672 498
charlesmn 0:3ac96e360672 499 VL53L1_print_xtalk_histogram_data(
charlesmn 0:3ac96e360672 500 &(pdev->xtalk_shapes),
charlesmn 0:3ac96e360672 501 "pdev->lldata.xtalk_shapes.",
charlesmn 0:3ac96e360672 502 VL53L1_TRACE_MODULE_XTALK_DATA);
charlesmn 0:3ac96e360672 503
charlesmn 0:3ac96e360672 504 VL53L1_print_xtalk_range_results(
charlesmn 0:3ac96e360672 505 &(pdev->xtalk_results),
charlesmn 0:3ac96e360672 506 "run_xtalk_extraction():pdev->lldata.xtalk_results.",
charlesmn 0:3ac96e360672 507 VL53L1_TRACE_MODULE_XTALK_DATA);
charlesmn 0:3ac96e360672 508
charlesmn 0:3ac96e360672 509 #endif
charlesmn 0:3ac96e360672 510
charlesmn 0:3ac96e360672 511 LOG_FUNCTION_END(status);
charlesmn 0:3ac96e360672 512
charlesmn 0:3ac96e360672 513 return status;
charlesmn 0:3ac96e360672 514
charlesmn 0:3ac96e360672 515 }
charlesmn 0:3ac96e360672 516
charlesmn 0:3ac96e360672 517
charlesmn 0:3ac96e360672 518
charlesmn 0:3ac96e360672 519 VL53L1_Error VL53L1_get_and_avg_xtalk_samples(
charlesmn 0:3ac96e360672 520 VL53L1_DEV Dev,
charlesmn 0:3ac96e360672 521 uint8_t num_of_samples,
charlesmn 0:3ac96e360672 522 uint8_t measurement_mode,
charlesmn 0:3ac96e360672 523 int16_t xtalk_filter_thresh_max_mm,
charlesmn 0:3ac96e360672 524 int16_t xtalk_filter_thresh_min_mm,
charlesmn 0:3ac96e360672 525 uint16_t xtalk_max_valid_rate_kcps,
charlesmn 0:3ac96e360672 526 uint8_t xtalk_result_id,
charlesmn 0:3ac96e360672 527 uint8_t xtalk_histo_id,
charlesmn 0:3ac96e360672 528 VL53L1_xtalk_range_results_t *pXR,
charlesmn 0:3ac96e360672 529 VL53L1_histogram_bin_data_t *psum_histo,
charlesmn 0:3ac96e360672 530 VL53L1_histogram_bin_data_t *pavg_histo)
charlesmn 0:3ac96e360672 531 {
charlesmn 0:3ac96e360672 532
charlesmn 0:3ac96e360672 533
charlesmn 0:3ac96e360672 534
charlesmn 0:3ac96e360672 535 VL53L1_Error status = VL53L1_ERROR_NONE;
charlesmn 0:3ac96e360672 536 VL53L1_LLDriverData_t *pdev =
charlesmn 0:3ac96e360672 537 VL53L1DevStructGetLLDriverHandle(Dev);
charlesmn 0:3ac96e360672 538
charlesmn 0:3ac96e360672 539 #ifdef VL53L1_LOG_ENABLE
charlesmn 0:3ac96e360672 540 VL53L1_LLDriverResults_t *pres =
charlesmn 0:3ac96e360672 541 VL53L1DevStructGetLLResultsHandle(Dev);
charlesmn 0:3ac96e360672 542 #endif
charlesmn 0:3ac96e360672 543
charlesmn 0:3ac96e360672 544 VL53L1_range_results_t *prs =
charlesmn 0:3ac96e360672 545 (VL53L1_range_results_t *) pdev->wArea1;
charlesmn 0:3ac96e360672 546
charlesmn 0:3ac96e360672 547 VL53L1_range_data_t *prange_data;
charlesmn 0:3ac96e360672 548 VL53L1_xtalk_range_data_t *pxtalk_range_data;
charlesmn 0:3ac96e360672 549
charlesmn 0:3ac96e360672 550 uint8_t i = 0;
charlesmn 0:3ac96e360672 551 uint8_t j = 0;
charlesmn 0:3ac96e360672 552 uint8_t zone_id = 0;
charlesmn 0:3ac96e360672 553 uint8_t final_zone = pdev->zone_cfg.active_zones+1;
charlesmn 0:3ac96e360672 554 uint8_t valid_result;
charlesmn 0:3ac96e360672 555
charlesmn 0:3ac96e360672 556 uint8_t smudge_corr_en = 0;
charlesmn 0:3ac96e360672 557
charlesmn 0:3ac96e360672 558
charlesmn 0:3ac96e360672 559
charlesmn 0:3ac96e360672 560
charlesmn 0:3ac96e360672 561 smudge_corr_en = pdev->smudge_correct_config.smudge_corr_enabled;
charlesmn 0:3ac96e360672 562
charlesmn 0:3ac96e360672 563 status = VL53L1_dynamic_xtalk_correction_disable(Dev);
charlesmn 0:3ac96e360672 564
charlesmn 0:3ac96e360672 565
charlesmn 0:3ac96e360672 566 VL53L1_load_patch(Dev);
charlesmn 0:3ac96e360672 567
charlesmn 0:3ac96e360672 568
charlesmn 0:3ac96e360672 569
charlesmn 0:3ac96e360672 570 if (status == VL53L1_ERROR_NONE)
charlesmn 0:3ac96e360672 571 status =
charlesmn 0:3ac96e360672 572 VL53L1_init_and_start_range(
charlesmn 0:3ac96e360672 573 Dev,
charlesmn 0:3ac96e360672 574 measurement_mode,
charlesmn 0:3ac96e360672 575 VL53L1_DEVICECONFIGLEVEL_CUSTOMER_ONWARDS);
charlesmn 0:3ac96e360672 576
charlesmn 0:3ac96e360672 577
charlesmn 0:3ac96e360672 578 for (i = 0; i <= (final_zone*num_of_samples); i++) {
charlesmn 0:3ac96e360672 579
charlesmn 0:3ac96e360672 580
charlesmn 0:3ac96e360672 581
charlesmn 0:3ac96e360672 582 if (status == VL53L1_ERROR_NONE)
charlesmn 0:3ac96e360672 583 status = VL53L1_wait_for_range_completion(Dev);
charlesmn 0:3ac96e360672 584
charlesmn 0:3ac96e360672 585
charlesmn 0:3ac96e360672 586
charlesmn 0:3ac96e360672 587 if (status == VL53L1_ERROR_NONE)
charlesmn 0:3ac96e360672 588 status =
charlesmn 0:3ac96e360672 589 VL53L1_get_device_results(
charlesmn 0:3ac96e360672 590 Dev,
charlesmn 0:3ac96e360672 591 VL53L1_DEVICERESULTSLEVEL_FULL,
charlesmn 0:3ac96e360672 592 prs);
charlesmn 0:3ac96e360672 593
charlesmn 0:3ac96e360672 594
charlesmn 0:3ac96e360672 595
charlesmn 0:3ac96e360672 596 if (status == VL53L1_ERROR_NONE &&
charlesmn 0:3ac96e360672 597 pdev->ll_state.rd_device_state !=
charlesmn 0:3ac96e360672 598 VL53L1_DEVICESTATE_RANGING_WAIT_GPH_SYNC) {
charlesmn 0:3ac96e360672 599
charlesmn 0:3ac96e360672 600 zone_id = pdev->ll_state.rd_zone_id + xtalk_result_id;
charlesmn 0:3ac96e360672 601 prange_data = &(prs->VL53L1_p_002[0]);
charlesmn 0:3ac96e360672 602
charlesmn 0:3ac96e360672 603
charlesmn 0:3ac96e360672 604 if (prs->active_results > 1) {
charlesmn 0:3ac96e360672 605 for (j = 1;
charlesmn 0:3ac96e360672 606 j < prs->active_results; j++) {
charlesmn 0:3ac96e360672 607 if (prs->VL53L1_p_002[j].median_range_mm
charlesmn 0:3ac96e360672 608 <
charlesmn 0:3ac96e360672 609 prange_data->median_range_mm)
charlesmn 0:3ac96e360672 610 prange_data =
charlesmn 0:3ac96e360672 611 &(prs->VL53L1_p_002[j]);
charlesmn 0:3ac96e360672 612
charlesmn 0:3ac96e360672 613 }
charlesmn 0:3ac96e360672 614 }
charlesmn 0:3ac96e360672 615
charlesmn 0:3ac96e360672 616 pxtalk_range_data = &(pXR->VL53L1_p_002[zone_id]);
charlesmn 0:3ac96e360672 617
charlesmn 0:3ac96e360672 618
charlesmn 0:3ac96e360672 619
charlesmn 0:3ac96e360672 620 if ((prs->active_results > 0) &&
charlesmn 0:3ac96e360672 621 (prange_data->median_range_mm <
charlesmn 0:3ac96e360672 622 xtalk_filter_thresh_max_mm) &&
charlesmn 0:3ac96e360672 623 (prange_data->median_range_mm >
charlesmn 0:3ac96e360672 624 xtalk_filter_thresh_min_mm) &&
charlesmn 0:3ac96e360672 625 (prange_data->VL53L1_p_012 <
charlesmn 0:3ac96e360672 626 (uint32_t)(xtalk_max_valid_rate_kcps * 16)))
charlesmn 0:3ac96e360672 627 valid_result = 1;
charlesmn 0:3ac96e360672 628 else
charlesmn 0:3ac96e360672 629 valid_result = 0;
charlesmn 0:3ac96e360672 630
charlesmn 0:3ac96e360672 631 if (valid_result == 1) {
charlesmn 0:3ac96e360672 632
charlesmn 0:3ac96e360672 633 pxtalk_range_data->no_of_samples++;
charlesmn 0:3ac96e360672 634
charlesmn 0:3ac96e360672 635 pxtalk_range_data->rate_per_spad_kcps_sum +=
charlesmn 0:3ac96e360672 636 prange_data->VL53L1_p_012;
charlesmn 0:3ac96e360672 637
charlesmn 0:3ac96e360672 638 pxtalk_range_data->signal_total_events_sum +=
charlesmn 0:3ac96e360672 639 prange_data->VL53L1_p_013;
charlesmn 0:3ac96e360672 640
charlesmn 0:3ac96e360672 641 pxtalk_range_data->sigma_mm_sum +=
charlesmn 0:3ac96e360672 642 (uint32_t)prange_data->VL53L1_p_005;
charlesmn 0:3ac96e360672 643
charlesmn 0:3ac96e360672 644
charlesmn 0:3ac96e360672 645
charlesmn 0:3ac96e360672 646 pxtalk_range_data->median_phase_sum +=
charlesmn 0:3ac96e360672 647 (uint32_t)prange_data->VL53L1_p_014;
charlesmn 0:3ac96e360672 648
charlesmn 0:3ac96e360672 649
charlesmn 0:3ac96e360672 650
charlesmn 0:3ac96e360672 651
charlesmn 0:3ac96e360672 652 }
charlesmn 0:3ac96e360672 653
charlesmn 0:3ac96e360672 654 if ((valid_result == 1) && (zone_id >= 4)) {
charlesmn 0:3ac96e360672 655 status = VL53L1_sum_histogram_data(
charlesmn 0:3ac96e360672 656 &(pdev->hist_data),
charlesmn 0:3ac96e360672 657 psum_histo);
charlesmn 0:3ac96e360672 658
charlesmn 0:3ac96e360672 659
charlesmn 0:3ac96e360672 660
charlesmn 0:3ac96e360672 661 if (prange_data->VL53L1_p_015 <
charlesmn 0:3ac96e360672 662 pXR->central_histogram__window_start)
charlesmn 0:3ac96e360672 663 pXR->central_histogram__window_start =
charlesmn 0:3ac96e360672 664 prange_data->VL53L1_p_015;
charlesmn 0:3ac96e360672 665
charlesmn 0:3ac96e360672 666
charlesmn 0:3ac96e360672 667 if (prange_data->VL53L1_p_016 >
charlesmn 0:3ac96e360672 668 pXR->central_histogram__window_end)
charlesmn 0:3ac96e360672 669 pXR->central_histogram__window_end =
charlesmn 0:3ac96e360672 670 prange_data->VL53L1_p_016;
charlesmn 0:3ac96e360672 671
charlesmn 0:3ac96e360672 672 }
charlesmn 0:3ac96e360672 673
charlesmn 0:3ac96e360672 674 }
charlesmn 0:3ac96e360672 675
charlesmn 0:3ac96e360672 676
charlesmn 0:3ac96e360672 677
charlesmn 0:3ac96e360672 678 #ifdef VL53L1_LOG_ENABLE
charlesmn 0:3ac96e360672 679 if (status == VL53L1_ERROR_NONE) {
charlesmn 0:3ac96e360672 680 VL53L1_print_range_results(
charlesmn 0:3ac96e360672 681 &(pres->range_results),
charlesmn 0:3ac96e360672 682 "pres->range_results.",
charlesmn 0:3ac96e360672 683 VL53L1_TRACE_MODULE_CORE);
charlesmn 0:3ac96e360672 684 }
charlesmn 0:3ac96e360672 685 #endif
charlesmn 0:3ac96e360672 686
charlesmn 0:3ac96e360672 687
charlesmn 0:3ac96e360672 688
charlesmn 0:3ac96e360672 689 if (status == VL53L1_ERROR_NONE)
charlesmn 0:3ac96e360672 690 status = VL53L1_wait_for_firmware_ready(Dev);
charlesmn 0:3ac96e360672 691
charlesmn 0:3ac96e360672 692
charlesmn 0:3ac96e360672 693
charlesmn 0:3ac96e360672 694 if (status == VL53L1_ERROR_NONE)
charlesmn 0:3ac96e360672 695 status =
charlesmn 0:3ac96e360672 696 VL53L1_clear_interrupt_and_enable_next_range(
charlesmn 0:3ac96e360672 697 Dev,
charlesmn 0:3ac96e360672 698 measurement_mode);
charlesmn 0:3ac96e360672 699
charlesmn 0:3ac96e360672 700
charlesmn 0:3ac96e360672 701 }
charlesmn 0:3ac96e360672 702
charlesmn 0:3ac96e360672 703
charlesmn 0:3ac96e360672 704
charlesmn 0:3ac96e360672 705
charlesmn 0:3ac96e360672 706 if (status == VL53L1_ERROR_NONE)
charlesmn 0:3ac96e360672 707 status = VL53L1_stop_range(Dev);
charlesmn 0:3ac96e360672 708
charlesmn 0:3ac96e360672 709 VL53L1_unload_patch(Dev);
charlesmn 0:3ac96e360672 710
charlesmn 0:3ac96e360672 711
charlesmn 0:3ac96e360672 712
charlesmn 0:3ac96e360672 713 for (i = 0; i < (pdev->zone_cfg.active_zones+1); i++) {
charlesmn 0:3ac96e360672 714
charlesmn 0:3ac96e360672 715 pxtalk_range_data = &(pXR->VL53L1_p_002[i+xtalk_result_id]);
charlesmn 0:3ac96e360672 716
charlesmn 0:3ac96e360672 717 if (pxtalk_range_data->no_of_samples > 0) {
charlesmn 0:3ac96e360672 718 pxtalk_range_data->rate_per_spad_kcps_avg =
charlesmn 0:3ac96e360672 719 pxtalk_range_data->rate_per_spad_kcps_sum /
charlesmn 0:3ac96e360672 720 (uint32_t)pxtalk_range_data->no_of_samples;
charlesmn 0:3ac96e360672 721
charlesmn 0:3ac96e360672 722 pxtalk_range_data->signal_total_events_avg =
charlesmn 0:3ac96e360672 723 pxtalk_range_data->signal_total_events_sum /
charlesmn 0:3ac96e360672 724 (int32_t)pxtalk_range_data->no_of_samples;
charlesmn 0:3ac96e360672 725
charlesmn 0:3ac96e360672 726 pxtalk_range_data->sigma_mm_avg =
charlesmn 0:3ac96e360672 727 pxtalk_range_data->sigma_mm_sum /
charlesmn 0:3ac96e360672 728 (uint32_t)pxtalk_range_data->no_of_samples;
charlesmn 0:3ac96e360672 729
charlesmn 0:3ac96e360672 730
charlesmn 0:3ac96e360672 731
charlesmn 0:3ac96e360672 732 pxtalk_range_data->median_phase_avg =
charlesmn 0:3ac96e360672 733 pxtalk_range_data->median_phase_sum /
charlesmn 0:3ac96e360672 734 (uint32_t)pxtalk_range_data->no_of_samples;
charlesmn 0:3ac96e360672 735
charlesmn 0:3ac96e360672 736
charlesmn 0:3ac96e360672 737
charlesmn 0:3ac96e360672 738 } else {
charlesmn 0:3ac96e360672 739 pxtalk_range_data->rate_per_spad_kcps_avg =
charlesmn 0:3ac96e360672 740 pxtalk_range_data->rate_per_spad_kcps_sum;
charlesmn 0:3ac96e360672 741 pxtalk_range_data->signal_total_events_avg =
charlesmn 0:3ac96e360672 742 pxtalk_range_data->signal_total_events_sum;
charlesmn 0:3ac96e360672 743 pxtalk_range_data->sigma_mm_avg =
charlesmn 0:3ac96e360672 744 pxtalk_range_data->sigma_mm_sum;
charlesmn 0:3ac96e360672 745
charlesmn 0:3ac96e360672 746
charlesmn 0:3ac96e360672 747
charlesmn 0:3ac96e360672 748 pxtalk_range_data->median_phase_avg =
charlesmn 0:3ac96e360672 749 pxtalk_range_data->median_phase_sum;
charlesmn 0:3ac96e360672 750
charlesmn 0:3ac96e360672 751
charlesmn 0:3ac96e360672 752 }
charlesmn 0:3ac96e360672 753 }
charlesmn 0:3ac96e360672 754
charlesmn 0:3ac96e360672 755
charlesmn 0:3ac96e360672 756
charlesmn 0:3ac96e360672 757 memcpy(pavg_histo, &(pdev->hist_data),
charlesmn 0:3ac96e360672 758 sizeof(VL53L1_histogram_bin_data_t));
charlesmn 0:3ac96e360672 759
charlesmn 0:3ac96e360672 760
charlesmn 0:3ac96e360672 761
charlesmn 0:3ac96e360672 762 if (status == VL53L1_ERROR_NONE) {
charlesmn 0:3ac96e360672 763
charlesmn 0:3ac96e360672 764 pxtalk_range_data = &(pXR->VL53L1_p_002[xtalk_histo_id]);
charlesmn 0:3ac96e360672 765
charlesmn 0:3ac96e360672 766 status = VL53L1_avg_histogram_data(
charlesmn 0:3ac96e360672 767 pxtalk_range_data->no_of_samples,
charlesmn 0:3ac96e360672 768 psum_histo,
charlesmn 0:3ac96e360672 769 pavg_histo);
charlesmn 0:3ac96e360672 770 }
charlesmn 0:3ac96e360672 771
charlesmn 0:3ac96e360672 772
charlesmn 0:3ac96e360672 773
charlesmn 0:3ac96e360672 774
charlesmn 0:3ac96e360672 775 if (status == VL53L1_ERROR_NONE) {
charlesmn 0:3ac96e360672 776 if (smudge_corr_en == 1)
charlesmn 0:3ac96e360672 777 status = VL53L1_dynamic_xtalk_correction_enable(Dev);
charlesmn 0:3ac96e360672 778 }
charlesmn 0:3ac96e360672 779
charlesmn 0:3ac96e360672 780
charlesmn 0:3ac96e360672 781 LOG_FUNCTION_END(status);
charlesmn 0:3ac96e360672 782
charlesmn 0:3ac96e360672 783 return status;
charlesmn 0:3ac96e360672 784
charlesmn 0:3ac96e360672 785 }
charlesmn 0:3ac96e360672 786
charlesmn 0:3ac96e360672 787
charlesmn 0:3ac96e360672 788
charlesmn 0:3ac96e360672 789 VL53L1_Error VL53L1_run_offset_calibration(
charlesmn 0:3ac96e360672 790 VL53L1_DEV Dev,
charlesmn 0:3ac96e360672 791 int16_t cal_distance_mm,
charlesmn 0:3ac96e360672 792 uint16_t cal_reflectance_pc,
charlesmn 0:3ac96e360672 793 VL53L1_Error *pcal_status)
charlesmn 0:3ac96e360672 794 {
charlesmn 0:3ac96e360672 795
charlesmn 0:3ac96e360672 796
charlesmn 0:3ac96e360672 797 VL53L1_Error status = VL53L1_ERROR_NONE;
charlesmn 0:3ac96e360672 798 VL53L1_LLDriverData_t *pdev =
charlesmn 0:3ac96e360672 799 VL53L1DevStructGetLLDriverHandle(Dev);
charlesmn 0:3ac96e360672 800
charlesmn 0:3ac96e360672 801 VL53L1_DevicePresetModes device_preset_modes[
charlesmn 0:3ac96e360672 802 VL53L1_MAX_OFFSET_RANGE_RESULTS];
charlesmn 0:3ac96e360672 803
charlesmn 0:3ac96e360672 804 VL53L1_range_results_t *prange_results =
charlesmn 0:3ac96e360672 805 (VL53L1_range_results_t *) pdev->wArea1;
charlesmn 0:3ac96e360672 806
charlesmn 0:3ac96e360672 807 VL53L1_range_data_t *pRData = NULL;
charlesmn 0:3ac96e360672 808 VL53L1_offset_range_data_t *pfs = NULL;
charlesmn 0:3ac96e360672 809 VL53L1_general_config_t *pG = &(pdev->gen_cfg);
charlesmn 0:3ac96e360672 810 VL53L1_additional_offset_cal_data_t *pAO = &(pdev->add_off_cal_data);
charlesmn 0:3ac96e360672 811
charlesmn 0:3ac96e360672 812 uint8_t i = 0;
charlesmn 0:3ac96e360672 813 uint8_t m = 0;
charlesmn 0:3ac96e360672 814 uint8_t measurement_mode =
charlesmn 0:3ac96e360672 815 VL53L1_DEVICEMEASUREMENTMODE_BACKTOBACK;
charlesmn 0:3ac96e360672 816 uint16_t manual_effective_spads =
charlesmn 0:3ac96e360672 817 pG->dss_config__manual_effective_spads_select;
charlesmn 0:3ac96e360672 818
charlesmn 0:3ac96e360672 819 uint8_t num_of_samples[VL53L1_MAX_OFFSET_RANGE_RESULTS];
charlesmn 0:3ac96e360672 820
charlesmn 0:3ac96e360672 821 uint8_t smudge_corr_en = 0;
charlesmn 0:3ac96e360672 822
charlesmn 0:3ac96e360672 823 LOG_FUNCTION_START("");
charlesmn 0:3ac96e360672 824
charlesmn 0:3ac96e360672 825
charlesmn 0:3ac96e360672 826
charlesmn 0:3ac96e360672 827 switch (pdev->offset_calibration_mode) {
charlesmn 0:3ac96e360672 828
charlesmn 0:3ac96e360672 829 case VL53L1_OFFSETCALIBRATIONMODE__MM1_MM2__HISTOGRAM:
charlesmn 0:3ac96e360672 830 case VL53L1_OFFSETCALIBRATIONMODE__MM1_MM2__HISTOGRAM_PRE_RANGE_ONLY:
charlesmn 0:3ac96e360672 831 device_preset_modes[0] =
charlesmn 0:3ac96e360672 832 VL53L1_DEVICEPRESETMODE_HISTOGRAM_RANGING;
charlesmn 0:3ac96e360672 833 device_preset_modes[1] =
charlesmn 0:3ac96e360672 834 VL53L1_DEVICEPRESETMODE_HISTOGRAM_RANGING_MM1_CAL;
charlesmn 0:3ac96e360672 835 device_preset_modes[2] =
charlesmn 0:3ac96e360672 836 VL53L1_DEVICEPRESETMODE_HISTOGRAM_RANGING_MM2_CAL;
charlesmn 0:3ac96e360672 837 break;
charlesmn 0:3ac96e360672 838
charlesmn 0:3ac96e360672 839 default:
charlesmn 0:3ac96e360672 840 device_preset_modes[0] =
charlesmn 0:3ac96e360672 841 VL53L1_DEVICEPRESETMODE_STANDARD_RANGING;
charlesmn 0:3ac96e360672 842 device_preset_modes[1] =
charlesmn 0:3ac96e360672 843 VL53L1_DEVICEPRESETMODE_STANDARD_RANGING_MM1_CAL;
charlesmn 0:3ac96e360672 844 device_preset_modes[2] =
charlesmn 0:3ac96e360672 845 VL53L1_DEVICEPRESETMODE_STANDARD_RANGING_MM2_CAL;
charlesmn 0:3ac96e360672 846 break;
charlesmn 0:3ac96e360672 847 }
charlesmn 0:3ac96e360672 848
charlesmn 0:3ac96e360672 849
charlesmn 0:3ac96e360672 850
charlesmn 0:3ac96e360672 851 num_of_samples[0] = pdev->offsetcal_cfg.pre_num_of_samples;
charlesmn 0:3ac96e360672 852 num_of_samples[1] = pdev->offsetcal_cfg.mm1_num_of_samples;
charlesmn 0:3ac96e360672 853 num_of_samples[2] = pdev->offsetcal_cfg.mm2_num_of_samples;
charlesmn 0:3ac96e360672 854
charlesmn 0:3ac96e360672 855
charlesmn 0:3ac96e360672 856
charlesmn 0:3ac96e360672 857
charlesmn 0:3ac96e360672 858 switch (pdev->offset_calibration_mode) {
charlesmn 0:3ac96e360672 859
charlesmn 0:3ac96e360672 860 case VL53L1_OFFSETCALIBRATIONMODE__MM1_MM2__STANDARD_PRE_RANGE_ONLY:
charlesmn 0:3ac96e360672 861 case VL53L1_OFFSETCALIBRATIONMODE__MM1_MM2__HISTOGRAM_PRE_RANGE_ONLY:
charlesmn 0:3ac96e360672 862
charlesmn 0:3ac96e360672 863 pdev->offset_results.active_results = 1;
charlesmn 0:3ac96e360672 864
charlesmn 0:3ac96e360672 865 break;
charlesmn 0:3ac96e360672 866
charlesmn 0:3ac96e360672 867 default:
charlesmn 0:3ac96e360672 868
charlesmn 0:3ac96e360672 869 pdev->customer.mm_config__inner_offset_mm = 0;
charlesmn 0:3ac96e360672 870 pdev->customer.mm_config__outer_offset_mm = 0;
charlesmn 0:3ac96e360672 871 pdev->offset_results.active_results =
charlesmn 0:3ac96e360672 872 VL53L1_MAX_OFFSET_RANGE_RESULTS;
charlesmn 0:3ac96e360672 873
charlesmn 0:3ac96e360672 874 break;
charlesmn 0:3ac96e360672 875 }
charlesmn 0:3ac96e360672 876
charlesmn 0:3ac96e360672 877 pdev->customer.algo__part_to_part_range_offset_mm = 0;
charlesmn 0:3ac96e360672 878
charlesmn 0:3ac96e360672 879
charlesmn 0:3ac96e360672 880
charlesmn 0:3ac96e360672 881 pdev->offset_results.max_results = VL53L1_MAX_OFFSET_RANGE_RESULTS;
charlesmn 0:3ac96e360672 882 pdev->offset_results.cal_distance_mm = cal_distance_mm;
charlesmn 0:3ac96e360672 883 pdev->offset_results.cal_reflectance_pc = cal_reflectance_pc;
charlesmn 0:3ac96e360672 884
charlesmn 0:3ac96e360672 885 for (m = 0; m < VL53L1_MAX_OFFSET_RANGE_RESULTS; m++) {
charlesmn 0:3ac96e360672 886
charlesmn 0:3ac96e360672 887 pfs = &(pdev->offset_results.VL53L1_p_002[m]);
charlesmn 0:3ac96e360672 888 pfs->preset_mode = 0;
charlesmn 0:3ac96e360672 889 pfs->no_of_samples = 0;
charlesmn 0:3ac96e360672 890 pfs->effective_spads = 0;
charlesmn 0:3ac96e360672 891 pfs->peak_rate_mcps = 0;
charlesmn 0:3ac96e360672 892 pfs->VL53L1_p_005 = 0;
charlesmn 0:3ac96e360672 893 pfs->median_range_mm = 0;
charlesmn 0:3ac96e360672 894 }
charlesmn 0:3ac96e360672 895
charlesmn 0:3ac96e360672 896
charlesmn 0:3ac96e360672 897
charlesmn 0:3ac96e360672 898
charlesmn 0:3ac96e360672 899 smudge_corr_en = pdev->smudge_correct_config.smudge_corr_enabled;
charlesmn 0:3ac96e360672 900
charlesmn 0:3ac96e360672 901 status = VL53L1_dynamic_xtalk_correction_disable(Dev);
charlesmn 0:3ac96e360672 902
charlesmn 0:3ac96e360672 903
charlesmn 0:3ac96e360672 904
charlesmn 0:3ac96e360672 905 for (m = 0; m < pdev->offset_results.active_results; m++) {
charlesmn 0:3ac96e360672 906
charlesmn 0:3ac96e360672 907 pfs = &(pdev->offset_results.VL53L1_p_002[m]);
charlesmn 0:3ac96e360672 908
charlesmn 0:3ac96e360672 909 pfs->preset_mode = device_preset_modes[m];
charlesmn 0:3ac96e360672 910
charlesmn 0:3ac96e360672 911
charlesmn 0:3ac96e360672 912
charlesmn 0:3ac96e360672 913 if (status == VL53L1_ERROR_NONE)
charlesmn 0:3ac96e360672 914 status =
charlesmn 0:3ac96e360672 915 VL53L1_set_preset_mode(
charlesmn 0:3ac96e360672 916 Dev,
charlesmn 0:3ac96e360672 917 device_preset_modes[m],
charlesmn 0:3ac96e360672 918
charlesmn 0:3ac96e360672 919 pdev->offsetcal_cfg.dss_config__target_total_rate_mcps,
charlesmn 0:3ac96e360672 920 pdev->offsetcal_cfg.phasecal_config_timeout_us,
charlesmn 0:3ac96e360672 921 pdev->offsetcal_cfg.mm_config_timeout_us,
charlesmn 0:3ac96e360672 922 pdev->offsetcal_cfg.range_config_timeout_us,
charlesmn 0:3ac96e360672 923
charlesmn 0:3ac96e360672 924 100);
charlesmn 0:3ac96e360672 925
charlesmn 0:3ac96e360672 926 pG->dss_config__manual_effective_spads_select =
charlesmn 0:3ac96e360672 927 manual_effective_spads;
charlesmn 0:3ac96e360672 928
charlesmn 0:3ac96e360672 929
charlesmn 0:3ac96e360672 930 VL53L1_load_patch(Dev);
charlesmn 0:3ac96e360672 931
charlesmn 0:3ac96e360672 932 if (status == VL53L1_ERROR_NONE)
charlesmn 0:3ac96e360672 933 status =
charlesmn 0:3ac96e360672 934 VL53L1_init_and_start_range(
charlesmn 0:3ac96e360672 935 Dev,
charlesmn 0:3ac96e360672 936 measurement_mode,
charlesmn 0:3ac96e360672 937 VL53L1_DEVICECONFIGLEVEL_CUSTOMER_ONWARDS);
charlesmn 0:3ac96e360672 938
charlesmn 0:3ac96e360672 939 for (i = 0; i <= (num_of_samples[m]+2); i++) {
charlesmn 0:3ac96e360672 940
charlesmn 0:3ac96e360672 941
charlesmn 0:3ac96e360672 942
charlesmn 0:3ac96e360672 943 if (status == VL53L1_ERROR_NONE)
charlesmn 0:3ac96e360672 944 status =
charlesmn 0:3ac96e360672 945 VL53L1_wait_for_range_completion(Dev);
charlesmn 0:3ac96e360672 946
charlesmn 0:3ac96e360672 947
charlesmn 0:3ac96e360672 948
charlesmn 0:3ac96e360672 949 if (status == VL53L1_ERROR_NONE)
charlesmn 0:3ac96e360672 950 status =
charlesmn 0:3ac96e360672 951 VL53L1_get_device_results(
charlesmn 0:3ac96e360672 952 Dev,
charlesmn 0:3ac96e360672 953 VL53L1_DEVICERESULTSLEVEL_FULL,
charlesmn 0:3ac96e360672 954 prange_results);
charlesmn 0:3ac96e360672 955
charlesmn 0:3ac96e360672 956
charlesmn 0:3ac96e360672 957
charlesmn 0:3ac96e360672 958 pRData = &(prange_results->VL53L1_p_002[0]);
charlesmn 0:3ac96e360672 959
charlesmn 0:3ac96e360672 960 if ((prange_results->active_results > 0 &&
charlesmn 0:3ac96e360672 961 prange_results->stream_count > 1) &&
charlesmn 0:3ac96e360672 962 (pRData->range_status ==
charlesmn 0:3ac96e360672 963 VL53L1_DEVICEERROR_RANGECOMPLETE)) {
charlesmn 0:3ac96e360672 964
charlesmn 0:3ac96e360672 965 pfs->no_of_samples++;
charlesmn 0:3ac96e360672 966 pfs->effective_spads +=
charlesmn 0:3ac96e360672 967 (uint32_t)pRData->VL53L1_p_006;
charlesmn 0:3ac96e360672 968 pfs->peak_rate_mcps +=
charlesmn 0:3ac96e360672 969 (uint32_t)pRData->peak_signal_count_rate_mcps;
charlesmn 0:3ac96e360672 970 pfs->VL53L1_p_005 +=
charlesmn 0:3ac96e360672 971 (uint32_t)pRData->VL53L1_p_005;
charlesmn 0:3ac96e360672 972 pfs->median_range_mm +=
charlesmn 0:3ac96e360672 973 (int32_t)pRData->median_range_mm;
charlesmn 0:3ac96e360672 974
charlesmn 0:3ac96e360672 975 pfs->dss_config__roi_mode_control =
charlesmn 0:3ac96e360672 976 pG->dss_config__roi_mode_control;
charlesmn 0:3ac96e360672 977 pfs->dss_config__manual_effective_spads_select =
charlesmn 0:3ac96e360672 978 pG->dss_config__manual_effective_spads_select;
charlesmn 0:3ac96e360672 979
charlesmn 0:3ac96e360672 980 }
charlesmn 0:3ac96e360672 981
charlesmn 0:3ac96e360672 982
charlesmn 0:3ac96e360672 983
charlesmn 0:3ac96e360672 984 if (status == VL53L1_ERROR_NONE)
charlesmn 0:3ac96e360672 985 status =
charlesmn 0:3ac96e360672 986 VL53L1_wait_for_firmware_ready(Dev);
charlesmn 0:3ac96e360672 987
charlesmn 0:3ac96e360672 988
charlesmn 0:3ac96e360672 989
charlesmn 0:3ac96e360672 990 if (status == VL53L1_ERROR_NONE)
charlesmn 0:3ac96e360672 991 status =
charlesmn 0:3ac96e360672 992 VL53L1_clear_interrupt_and_enable_next_range(
charlesmn 0:3ac96e360672 993 Dev,
charlesmn 0:3ac96e360672 994 measurement_mode);
charlesmn 0:3ac96e360672 995 }
charlesmn 0:3ac96e360672 996
charlesmn 0:3ac96e360672 997
charlesmn 0:3ac96e360672 998
charlesmn 0:3ac96e360672 999 if (status == VL53L1_ERROR_NONE)
charlesmn 0:3ac96e360672 1000 status = VL53L1_stop_range(Dev);
charlesmn 0:3ac96e360672 1001
charlesmn 0:3ac96e360672 1002
charlesmn 0:3ac96e360672 1003
charlesmn 0:3ac96e360672 1004 if (status == VL53L1_ERROR_NONE)
charlesmn 0:3ac96e360672 1005 status = VL53L1_WaitUs(Dev, 1000);
charlesmn 0:3ac96e360672 1006 VL53L1_unload_patch(Dev);
charlesmn 0:3ac96e360672 1007
charlesmn 0:3ac96e360672 1008
charlesmn 0:3ac96e360672 1009 if (pfs->no_of_samples > 0) {
charlesmn 0:3ac96e360672 1010
charlesmn 0:3ac96e360672 1011 pfs->effective_spads += (pfs->no_of_samples/2);
charlesmn 0:3ac96e360672 1012 pfs->effective_spads /= pfs->no_of_samples;
charlesmn 0:3ac96e360672 1013
charlesmn 0:3ac96e360672 1014 pfs->peak_rate_mcps += (pfs->no_of_samples/2);
charlesmn 0:3ac96e360672 1015 pfs->peak_rate_mcps /= pfs->no_of_samples;
charlesmn 0:3ac96e360672 1016
charlesmn 0:3ac96e360672 1017 pfs->VL53L1_p_005 += (pfs->no_of_samples/2);
charlesmn 0:3ac96e360672 1018 pfs->VL53L1_p_005 /= pfs->no_of_samples;
charlesmn 0:3ac96e360672 1019
charlesmn 0:3ac96e360672 1020 pfs->median_range_mm += (pfs->no_of_samples/2);
charlesmn 0:3ac96e360672 1021 pfs->median_range_mm /= pfs->no_of_samples;
charlesmn 0:3ac96e360672 1022
charlesmn 0:3ac96e360672 1023 pfs->range_mm_offset = (int32_t)cal_distance_mm;
charlesmn 0:3ac96e360672 1024 pfs->range_mm_offset -= pfs->median_range_mm;
charlesmn 0:3ac96e360672 1025
charlesmn 0:3ac96e360672 1026
charlesmn 0:3ac96e360672 1027 if (pfs->preset_mode ==
charlesmn 0:3ac96e360672 1028 VL53L1_DEVICEPRESETMODE_STANDARD_RANGING)
charlesmn 0:3ac96e360672 1029 manual_effective_spads =
charlesmn 0:3ac96e360672 1030 (uint16_t)pfs->effective_spads;
charlesmn 0:3ac96e360672 1031 }
charlesmn 0:3ac96e360672 1032 }
charlesmn 0:3ac96e360672 1033
charlesmn 0:3ac96e360672 1034
charlesmn 0:3ac96e360672 1035
charlesmn 0:3ac96e360672 1036 switch (pdev->offset_calibration_mode) {
charlesmn 0:3ac96e360672 1037
charlesmn 0:3ac96e360672 1038 case VL53L1_OFFSETCALIBRATIONMODE__MM1_MM2__STANDARD_PRE_RANGE_ONLY:
charlesmn 0:3ac96e360672 1039 case VL53L1_OFFSETCALIBRATIONMODE__MM1_MM2__HISTOGRAM_PRE_RANGE_ONLY:
charlesmn 0:3ac96e360672 1040
charlesmn 0:3ac96e360672 1041
charlesmn 0:3ac96e360672 1042 pdev->customer.mm_config__inner_offset_mm +=
charlesmn 0:3ac96e360672 1043 (int16_t)pdev->offset_results.VL53L1_p_002[0].range_mm_offset;
charlesmn 0:3ac96e360672 1044 pdev->customer.mm_config__outer_offset_mm +=
charlesmn 0:3ac96e360672 1045 (int16_t)pdev->offset_results.VL53L1_p_002[0].range_mm_offset;
charlesmn 0:3ac96e360672 1046 break;
charlesmn 0:3ac96e360672 1047
charlesmn 0:3ac96e360672 1048 default:
charlesmn 0:3ac96e360672 1049
charlesmn 0:3ac96e360672 1050 pdev->customer.mm_config__inner_offset_mm =
charlesmn 0:3ac96e360672 1051 (int16_t)pdev->offset_results.VL53L1_p_002[1].range_mm_offset;
charlesmn 0:3ac96e360672 1052 pdev->customer.mm_config__outer_offset_mm =
charlesmn 0:3ac96e360672 1053 (int16_t)pdev->offset_results.VL53L1_p_002[2].range_mm_offset;
charlesmn 0:3ac96e360672 1054 pdev->customer.algo__part_to_part_range_offset_mm = 0;
charlesmn 0:3ac96e360672 1055
charlesmn 0:3ac96e360672 1056
charlesmn 0:3ac96e360672 1057
charlesmn 0:3ac96e360672 1058 pAO->result__mm_inner_actual_effective_spads =
charlesmn 0:3ac96e360672 1059 (uint16_t)pdev->offset_results.VL53L1_p_002[1].effective_spads;
charlesmn 0:3ac96e360672 1060 pAO->result__mm_outer_actual_effective_spads =
charlesmn 0:3ac96e360672 1061 (uint16_t)pdev->offset_results.VL53L1_p_002[2].effective_spads;
charlesmn 0:3ac96e360672 1062
charlesmn 0:3ac96e360672 1063 pAO->result__mm_inner_peak_signal_count_rtn_mcps =
charlesmn 0:3ac96e360672 1064 (uint16_t)pdev->offset_results.VL53L1_p_002[1].peak_rate_mcps;
charlesmn 0:3ac96e360672 1065 pAO->result__mm_outer_peak_signal_count_rtn_mcps =
charlesmn 0:3ac96e360672 1066 (uint16_t)pdev->offset_results.VL53L1_p_002[2].peak_rate_mcps;
charlesmn 0:3ac96e360672 1067
charlesmn 0:3ac96e360672 1068 break;
charlesmn 0:3ac96e360672 1069 }
charlesmn 0:3ac96e360672 1070
charlesmn 0:3ac96e360672 1071
charlesmn 0:3ac96e360672 1072
charlesmn 0:3ac96e360672 1073 pdev->cust_dmax_cal.ref__actual_effective_spads =
charlesmn 0:3ac96e360672 1074 (uint16_t)pdev->offset_results.VL53L1_p_002[0].effective_spads;
charlesmn 0:3ac96e360672 1075 pdev->cust_dmax_cal.ref__peak_signal_count_rate_mcps =
charlesmn 0:3ac96e360672 1076 (uint16_t)pdev->offset_results.VL53L1_p_002[0].peak_rate_mcps;
charlesmn 0:3ac96e360672 1077
charlesmn 0:3ac96e360672 1078
charlesmn 0:3ac96e360672 1079 pdev->cust_dmax_cal.ref__distance_mm = cal_distance_mm * 16;
charlesmn 0:3ac96e360672 1080
charlesmn 0:3ac96e360672 1081 pdev->cust_dmax_cal.ref_reflectance_pc = cal_reflectance_pc;
charlesmn 0:3ac96e360672 1082 pdev->cust_dmax_cal.coverglass_transmission = 0x0100;
charlesmn 0:3ac96e360672 1083
charlesmn 0:3ac96e360672 1084
charlesmn 0:3ac96e360672 1085
charlesmn 0:3ac96e360672 1086 if (status == VL53L1_ERROR_NONE)
charlesmn 0:3ac96e360672 1087 status =
charlesmn 0:3ac96e360672 1088 VL53L1_set_customer_nvm_managed(
charlesmn 0:3ac96e360672 1089 Dev,
charlesmn 0:3ac96e360672 1090 &(pdev->customer));
charlesmn 0:3ac96e360672 1091
charlesmn 0:3ac96e360672 1092
charlesmn 0:3ac96e360672 1093
charlesmn 0:3ac96e360672 1094
charlesmn 0:3ac96e360672 1095 if (status == VL53L1_ERROR_NONE) {
charlesmn 0:3ac96e360672 1096 if (smudge_corr_en == 1)
charlesmn 0:3ac96e360672 1097 status = VL53L1_dynamic_xtalk_correction_enable(Dev);
charlesmn 0:3ac96e360672 1098 }
charlesmn 0:3ac96e360672 1099
charlesmn 0:3ac96e360672 1100
charlesmn 0:3ac96e360672 1101
charlesmn 0:3ac96e360672 1102
charlesmn 0:3ac96e360672 1103 for (m = 0; m < pdev->offset_results.active_results; m++) {
charlesmn 0:3ac96e360672 1104
charlesmn 0:3ac96e360672 1105 pfs = &(pdev->offset_results.VL53L1_p_002[m]);
charlesmn 0:3ac96e360672 1106
charlesmn 0:3ac96e360672 1107 if (status == VL53L1_ERROR_NONE) {
charlesmn 0:3ac96e360672 1108
charlesmn 0:3ac96e360672 1109 pdev->offset_results.cal_report = m;
charlesmn 0:3ac96e360672 1110
charlesmn 0:3ac96e360672 1111 if (pfs->no_of_samples < num_of_samples[m])
charlesmn 0:3ac96e360672 1112 status =
charlesmn 0:3ac96e360672 1113 VL53L1_WARNING_OFFSET_CAL_MISSING_SAMPLES;
charlesmn 0:3ac96e360672 1114
charlesmn 0:3ac96e360672 1115
charlesmn 0:3ac96e360672 1116 if (m == 0 && pfs->VL53L1_p_005 >
charlesmn 0:3ac96e360672 1117 ((uint32_t)VL53L1_OFFSET_CAL_MAX_SIGMA_MM << 5))
charlesmn 0:3ac96e360672 1118 status =
charlesmn 0:3ac96e360672 1119 VL53L1_WARNING_OFFSET_CAL_SIGMA_TOO_HIGH;
charlesmn 0:3ac96e360672 1120
charlesmn 0:3ac96e360672 1121 if (pfs->peak_rate_mcps >
charlesmn 0:3ac96e360672 1122 VL53L1_OFFSET_CAL_MAX_PRE_PEAK_RATE_MCPS)
charlesmn 0:3ac96e360672 1123 status =
charlesmn 0:3ac96e360672 1124 VL53L1_WARNING_OFFSET_CAL_RATE_TOO_HIGH;
charlesmn 0:3ac96e360672 1125
charlesmn 0:3ac96e360672 1126 if (pfs->dss_config__manual_effective_spads_select <
charlesmn 0:3ac96e360672 1127 VL53L1_OFFSET_CAL_MIN_EFFECTIVE_SPADS)
charlesmn 0:3ac96e360672 1128 status =
charlesmn 0:3ac96e360672 1129 VL53L1_WARNING_OFFSET_CAL_SPAD_COUNT_TOO_LOW;
charlesmn 0:3ac96e360672 1130
charlesmn 0:3ac96e360672 1131 if (pfs->dss_config__manual_effective_spads_select == 0)
charlesmn 0:3ac96e360672 1132 status =
charlesmn 0:3ac96e360672 1133 VL53L1_ERROR_OFFSET_CAL_NO_SPADS_ENABLED_FAIL;
charlesmn 0:3ac96e360672 1134
charlesmn 0:3ac96e360672 1135 if (pfs->no_of_samples == 0)
charlesmn 0:3ac96e360672 1136 status = VL53L1_ERROR_OFFSET_CAL_NO_SAMPLE_FAIL;
charlesmn 0:3ac96e360672 1137 }
charlesmn 0:3ac96e360672 1138 }
charlesmn 0:3ac96e360672 1139
charlesmn 0:3ac96e360672 1140
charlesmn 0:3ac96e360672 1141
charlesmn 0:3ac96e360672 1142 pdev->offset_results.cal_status = status;
charlesmn 0:3ac96e360672 1143 *pcal_status = pdev->offset_results.cal_status;
charlesmn 0:3ac96e360672 1144
charlesmn 0:3ac96e360672 1145
charlesmn 0:3ac96e360672 1146
charlesmn 0:3ac96e360672 1147 IGNORE_STATUS(
charlesmn 0:3ac96e360672 1148 IGNORE_OFFSET_CAL_MISSING_SAMPLES,
charlesmn 0:3ac96e360672 1149 VL53L1_WARNING_OFFSET_CAL_MISSING_SAMPLES,
charlesmn 0:3ac96e360672 1150 status);
charlesmn 0:3ac96e360672 1151
charlesmn 0:3ac96e360672 1152 IGNORE_STATUS(
charlesmn 0:3ac96e360672 1153 IGNORE_OFFSET_CAL_SIGMA_TOO_HIGH,
charlesmn 0:3ac96e360672 1154 VL53L1_WARNING_OFFSET_CAL_SIGMA_TOO_HIGH,
charlesmn 0:3ac96e360672 1155 status);
charlesmn 0:3ac96e360672 1156
charlesmn 0:3ac96e360672 1157 IGNORE_STATUS(
charlesmn 0:3ac96e360672 1158 IGNORE_OFFSET_CAL_RATE_TOO_HIGH,
charlesmn 0:3ac96e360672 1159 VL53L1_WARNING_OFFSET_CAL_RATE_TOO_HIGH,
charlesmn 0:3ac96e360672 1160 status);
charlesmn 0:3ac96e360672 1161
charlesmn 0:3ac96e360672 1162 IGNORE_STATUS(
charlesmn 0:3ac96e360672 1163 IGNORE_OFFSET_CAL_SPAD_COUNT_TOO_LOW,
charlesmn 0:3ac96e360672 1164 VL53L1_WARNING_OFFSET_CAL_SPAD_COUNT_TOO_LOW,
charlesmn 0:3ac96e360672 1165 status);
charlesmn 0:3ac96e360672 1166
charlesmn 0:3ac96e360672 1167 #ifdef VL53L1_LOG_ENABLE
charlesmn 0:3ac96e360672 1168
charlesmn 0:3ac96e360672 1169
charlesmn 0:3ac96e360672 1170
charlesmn 0:3ac96e360672 1171 VL53L1_print_customer_nvm_managed(
charlesmn 0:3ac96e360672 1172 &(pdev->customer),
charlesmn 0:3ac96e360672 1173 "run_offset_calibration():pdev->lldata.customer.",
charlesmn 0:3ac96e360672 1174 VL53L1_TRACE_MODULE_OFFSET_DATA);
charlesmn 0:3ac96e360672 1175
charlesmn 0:3ac96e360672 1176 VL53L1_print_dmax_calibration_data(
charlesmn 0:3ac96e360672 1177 &(pdev->fmt_dmax_cal),
charlesmn 0:3ac96e360672 1178 "run_offset_calibration():pdev->lldata.fmt_dmax_cal.",
charlesmn 0:3ac96e360672 1179 VL53L1_TRACE_MODULE_OFFSET_DATA);
charlesmn 0:3ac96e360672 1180
charlesmn 0:3ac96e360672 1181 VL53L1_print_dmax_calibration_data(
charlesmn 0:3ac96e360672 1182 &(pdev->cust_dmax_cal),
charlesmn 0:3ac96e360672 1183 "run_offset_calibration():pdev->lldata.cust_dmax_cal.",
charlesmn 0:3ac96e360672 1184 VL53L1_TRACE_MODULE_OFFSET_DATA);
charlesmn 0:3ac96e360672 1185
charlesmn 0:3ac96e360672 1186 VL53L1_print_additional_offset_cal_data(
charlesmn 0:3ac96e360672 1187 &(pdev->add_off_cal_data),
charlesmn 0:3ac96e360672 1188 "run_offset_calibration():pdev->lldata.add_off_cal_data.",
charlesmn 0:3ac96e360672 1189 VL53L1_TRACE_MODULE_OFFSET_DATA);
charlesmn 0:3ac96e360672 1190
charlesmn 0:3ac96e360672 1191 VL53L1_print_offset_range_results(
charlesmn 0:3ac96e360672 1192 &(pdev->offset_results),
charlesmn 0:3ac96e360672 1193 "run_offset_calibration():pdev->lldata.offset_results.",
charlesmn 0:3ac96e360672 1194 VL53L1_TRACE_MODULE_OFFSET_DATA);
charlesmn 0:3ac96e360672 1195 #endif
charlesmn 0:3ac96e360672 1196
charlesmn 0:3ac96e360672 1197 LOG_FUNCTION_END(status);
charlesmn 0:3ac96e360672 1198
charlesmn 0:3ac96e360672 1199 return status;
charlesmn 0:3ac96e360672 1200 }
charlesmn 0:3ac96e360672 1201
charlesmn 0:3ac96e360672 1202
charlesmn 0:3ac96e360672 1203 VL53L1_Error VL53L1_run_phasecal_average(
charlesmn 0:3ac96e360672 1204 VL53L1_DEV Dev,
charlesmn 0:3ac96e360672 1205 uint8_t measurement_mode,
charlesmn 0:3ac96e360672 1206 uint8_t phasecal_result__vcsel_start,
charlesmn 0:3ac96e360672 1207 uint16_t phasecal_num_of_samples,
charlesmn 0:3ac96e360672 1208 VL53L1_range_results_t *prange_results,
charlesmn 0:3ac96e360672 1209 uint16_t *pphasecal_result__reference_phase,
charlesmn 0:3ac96e360672 1210 uint16_t *pzero_distance_phase)
charlesmn 0:3ac96e360672 1211 {
charlesmn 0:3ac96e360672 1212
charlesmn 0:3ac96e360672 1213
charlesmn 0:3ac96e360672 1214 VL53L1_Error status = VL53L1_ERROR_NONE;
charlesmn 0:3ac96e360672 1215 VL53L1_LLDriverData_t *pdev =
charlesmn 0:3ac96e360672 1216 VL53L1DevStructGetLLDriverHandle(Dev);
charlesmn 0:3ac96e360672 1217
charlesmn 0:3ac96e360672 1218 uint16_t i = 0;
charlesmn 0:3ac96e360672 1219 uint16_t m = 0;
charlesmn 0:3ac96e360672 1220 uint32_t samples = 0;
charlesmn 0:3ac96e360672 1221
charlesmn 0:3ac96e360672 1222 uint32_t period = 0;
charlesmn 0:3ac96e360672 1223 uint32_t VL53L1_p_017 = 0;
charlesmn 0:3ac96e360672 1224 uint32_t phasecal_result__reference_phase = 0;
charlesmn 0:3ac96e360672 1225 uint32_t zero_distance_phase = 0;
charlesmn 0:3ac96e360672 1226
charlesmn 0:3ac96e360672 1227
charlesmn 0:3ac96e360672 1228 VL53L1_load_patch(Dev);
charlesmn 0:3ac96e360672 1229
charlesmn 0:3ac96e360672 1230 for (m = 0; m < phasecal_num_of_samples; m++) {
charlesmn 0:3ac96e360672 1231
charlesmn 0:3ac96e360672 1232
charlesmn 0:3ac96e360672 1233
charlesmn 0:3ac96e360672 1234 if (status == VL53L1_ERROR_NONE)
charlesmn 0:3ac96e360672 1235 status =
charlesmn 0:3ac96e360672 1236 VL53L1_init_and_start_range(
charlesmn 0:3ac96e360672 1237 Dev,
charlesmn 0:3ac96e360672 1238 measurement_mode,
charlesmn 0:3ac96e360672 1239 VL53L1_DEVICECONFIGLEVEL_CUSTOMER_ONWARDS);
charlesmn 0:3ac96e360672 1240
charlesmn 0:3ac96e360672 1241 for (i = 0; i <= 1; i++) {
charlesmn 0:3ac96e360672 1242
charlesmn 0:3ac96e360672 1243
charlesmn 0:3ac96e360672 1244
charlesmn 0:3ac96e360672 1245 if (status == VL53L1_ERROR_NONE)
charlesmn 0:3ac96e360672 1246 status =
charlesmn 0:3ac96e360672 1247 VL53L1_wait_for_range_completion(Dev);
charlesmn 0:3ac96e360672 1248
charlesmn 0:3ac96e360672 1249
charlesmn 0:3ac96e360672 1250
charlesmn 0:3ac96e360672 1251 if (status == VL53L1_ERROR_NONE)
charlesmn 0:3ac96e360672 1252 status =
charlesmn 0:3ac96e360672 1253 VL53L1_get_device_results(
charlesmn 0:3ac96e360672 1254 Dev,
charlesmn 0:3ac96e360672 1255 VL53L1_DEVICERESULTSLEVEL_FULL,
charlesmn 0:3ac96e360672 1256 prange_results);
charlesmn 0:3ac96e360672 1257
charlesmn 0:3ac96e360672 1258
charlesmn 0:3ac96e360672 1259
charlesmn 0:3ac96e360672 1260 if (status == VL53L1_ERROR_NONE)
charlesmn 0:3ac96e360672 1261 status =
charlesmn 0:3ac96e360672 1262 VL53L1_wait_for_firmware_ready(Dev);
charlesmn 0:3ac96e360672 1263
charlesmn 0:3ac96e360672 1264
charlesmn 0:3ac96e360672 1265
charlesmn 0:3ac96e360672 1266 if (status == VL53L1_ERROR_NONE)
charlesmn 0:3ac96e360672 1267 status =
charlesmn 0:3ac96e360672 1268 VL53L1_clear_interrupt_and_enable_next_range(
charlesmn 0:3ac96e360672 1269 Dev,
charlesmn 0:3ac96e360672 1270 measurement_mode);
charlesmn 0:3ac96e360672 1271 }
charlesmn 0:3ac96e360672 1272
charlesmn 0:3ac96e360672 1273
charlesmn 0:3ac96e360672 1274
charlesmn 0:3ac96e360672 1275 if (status == VL53L1_ERROR_NONE)
charlesmn 0:3ac96e360672 1276 status = VL53L1_stop_range(Dev);
charlesmn 0:3ac96e360672 1277
charlesmn 0:3ac96e360672 1278
charlesmn 0:3ac96e360672 1279
charlesmn 0:3ac96e360672 1280 if (status == VL53L1_ERROR_NONE)
charlesmn 0:3ac96e360672 1281 status = VL53L1_WaitUs(Dev, 1000);
charlesmn 0:3ac96e360672 1282
charlesmn 0:3ac96e360672 1283
charlesmn 0:3ac96e360672 1284
charlesmn 0:3ac96e360672 1285 if (status == VL53L1_ERROR_NONE) {
charlesmn 0:3ac96e360672 1286
charlesmn 0:3ac96e360672 1287 samples++;
charlesmn 0:3ac96e360672 1288
charlesmn 0:3ac96e360672 1289
charlesmn 0:3ac96e360672 1290 period = 2048 *
charlesmn 0:3ac96e360672 1291 (uint32_t)VL53L1_decode_vcsel_period(
charlesmn 0:3ac96e360672 1292 pdev->hist_data.VL53L1_p_009);
charlesmn 0:3ac96e360672 1293
charlesmn 0:3ac96e360672 1294 VL53L1_p_017 = period;
charlesmn 0:3ac96e360672 1295 VL53L1_p_017 += (uint32_t)(
charlesmn 0:3ac96e360672 1296 pdev->hist_data.phasecal_result__reference_phase);
charlesmn 0:3ac96e360672 1297 VL53L1_p_017 +=
charlesmn 0:3ac96e360672 1298 (2048 *
charlesmn 0:3ac96e360672 1299 (uint32_t)phasecal_result__vcsel_start);
charlesmn 0:3ac96e360672 1300 VL53L1_p_017 -= (2048 *
charlesmn 0:3ac96e360672 1301 (uint32_t)pdev->hist_data.cal_config__vcsel_start);
charlesmn 0:3ac96e360672 1302
lugandc 18:0696efe39d08 1303 if (period != 0) {
lugandc 18:0696efe39d08 1304 VL53L1_p_017 = VL53L1_p_017 % period;
lugandc 18:0696efe39d08 1305 }
lugandc 18:0696efe39d08 1306 else {
lugandc 18:0696efe39d08 1307 status =
lugandc 18:0696efe39d08 1308 VL53L1_ERROR_DIVISION_BY_ZERO;
lugandc 18:0696efe39d08 1309 VL53L1_p_017 = 0;
lugandc 18:0696efe39d08 1310 }
charlesmn 0:3ac96e360672 1311
charlesmn 0:3ac96e360672 1312 phasecal_result__reference_phase += (uint32_t)(
charlesmn 0:3ac96e360672 1313 pdev->hist_data.phasecal_result__reference_phase);
charlesmn 0:3ac96e360672 1314
charlesmn 0:3ac96e360672 1315 zero_distance_phase += (uint32_t)VL53L1_p_017;
charlesmn 0:3ac96e360672 1316 }
charlesmn 0:3ac96e360672 1317 }
charlesmn 0:3ac96e360672 1318 VL53L1_unload_patch(Dev);
charlesmn 0:3ac96e360672 1319
charlesmn 0:3ac96e360672 1320
charlesmn 0:3ac96e360672 1321
charlesmn 0:3ac96e360672 1322 if (status == VL53L1_ERROR_NONE && samples > 0) {
charlesmn 0:3ac96e360672 1323
charlesmn 0:3ac96e360672 1324 phasecal_result__reference_phase += (samples >> 1);
charlesmn 0:3ac96e360672 1325 phasecal_result__reference_phase /= samples;
charlesmn 0:3ac96e360672 1326
charlesmn 0:3ac96e360672 1327 zero_distance_phase += (samples >> 1);
charlesmn 0:3ac96e360672 1328 zero_distance_phase /= samples;
charlesmn 0:3ac96e360672 1329
charlesmn 0:3ac96e360672 1330 *pphasecal_result__reference_phase =
charlesmn 0:3ac96e360672 1331 (uint16_t)phasecal_result__reference_phase;
charlesmn 0:3ac96e360672 1332 *pzero_distance_phase =
charlesmn 0:3ac96e360672 1333 (uint16_t)zero_distance_phase;
charlesmn 0:3ac96e360672 1334 }
charlesmn 0:3ac96e360672 1335
charlesmn 0:3ac96e360672 1336 return status;
charlesmn 0:3ac96e360672 1337 }
charlesmn 0:3ac96e360672 1338
charlesmn 0:3ac96e360672 1339
charlesmn 0:3ac96e360672 1340 VL53L1_Error VL53L1_run_zone_calibration(
charlesmn 0:3ac96e360672 1341 VL53L1_DEV Dev,
charlesmn 0:3ac96e360672 1342 VL53L1_DevicePresetModes device_preset_mode,
charlesmn 0:3ac96e360672 1343 VL53L1_DeviceZonePreset zone_preset,
charlesmn 0:3ac96e360672 1344 VL53L1_zone_config_t *pzone_cfg,
charlesmn 0:3ac96e360672 1345 int16_t cal_distance_mm,
charlesmn 0:3ac96e360672 1346 uint16_t cal_reflectance_pc,
charlesmn 0:3ac96e360672 1347 VL53L1_Error *pcal_status)
charlesmn 0:3ac96e360672 1348 {
charlesmn 0:3ac96e360672 1349
charlesmn 0:3ac96e360672 1350
charlesmn 0:3ac96e360672 1351 VL53L1_Error status = VL53L1_ERROR_NONE;
charlesmn 0:3ac96e360672 1352 VL53L1_LLDriverData_t *pdev =
charlesmn 0:3ac96e360672 1353 VL53L1DevStructGetLLDriverHandle(Dev);
charlesmn 0:3ac96e360672 1354
charlesmn 0:3ac96e360672 1355 VL53L1_LLDriverResults_t *pres =
charlesmn 0:3ac96e360672 1356 VL53L1DevStructGetLLResultsHandle(Dev);
charlesmn 0:3ac96e360672 1357
charlesmn 0:3ac96e360672 1358 VL53L1_range_results_t *pRR =
charlesmn 0:3ac96e360672 1359 (VL53L1_range_results_t *) pdev->wArea1;
charlesmn 0:3ac96e360672 1360 VL53L1_range_data_t *prange_data = NULL;
charlesmn 0:3ac96e360672 1361 VL53L1_zone_calibration_data_t *pzone_data = NULL;
charlesmn 0:3ac96e360672 1362
charlesmn 0:3ac96e360672 1363 uint16_t i = 0;
charlesmn 0:3ac96e360672 1364 uint16_t m = 0;
charlesmn 0:3ac96e360672 1365
charlesmn 0:3ac96e360672 1366 uint8_t z = 0;
charlesmn 0:3ac96e360672 1367 uint8_t measurement_mode =
charlesmn 0:3ac96e360672 1368 VL53L1_DEVICEMEASUREMENTMODE_BACKTOBACK;
charlesmn 0:3ac96e360672 1369
charlesmn 0:3ac96e360672 1370 VL53L1_OffsetCorrectionMode offset_cor_mode =
charlesmn 0:3ac96e360672 1371 VL53L1_OFFSETCORRECTIONMODE__NONE;
charlesmn 0:3ac96e360672 1372
charlesmn 0:3ac96e360672 1373 LOG_FUNCTION_START("");
charlesmn 0:3ac96e360672 1374
charlesmn 0:3ac96e360672 1375
charlesmn 0:3ac96e360672 1376
charlesmn 0:3ac96e360672 1377 if (status == VL53L1_ERROR_NONE)
charlesmn 0:3ac96e360672 1378 status =
charlesmn 0:3ac96e360672 1379 VL53L1_set_preset_mode(
charlesmn 0:3ac96e360672 1380 Dev,
charlesmn 0:3ac96e360672 1381 device_preset_mode,
charlesmn 0:3ac96e360672 1382
charlesmn 0:3ac96e360672 1383 pdev->zonecal_cfg.dss_config__target_total_rate_mcps,
charlesmn 0:3ac96e360672 1384 pdev->zonecal_cfg.phasecal_config_timeout_us,
charlesmn 0:3ac96e360672 1385 pdev->zonecal_cfg.mm_config_timeout_us,
charlesmn 0:3ac96e360672 1386 pdev->zonecal_cfg.range_config_timeout_us,
charlesmn 0:3ac96e360672 1387
charlesmn 0:3ac96e360672 1388 100);
charlesmn 0:3ac96e360672 1389
charlesmn 0:3ac96e360672 1390
charlesmn 0:3ac96e360672 1391
charlesmn 0:3ac96e360672 1392 if (zone_preset == VL53L1_DEVICEZONEPRESET_CUSTOM) {
charlesmn 0:3ac96e360672 1393
charlesmn 0:3ac96e360672 1394 if (status == VL53L1_ERROR_NONE)
charlesmn 0:3ac96e360672 1395 status =
charlesmn 0:3ac96e360672 1396 VL53L1_set_zone_config(
charlesmn 0:3ac96e360672 1397 Dev,
charlesmn 0:3ac96e360672 1398 pzone_cfg);
charlesmn 0:3ac96e360672 1399
charlesmn 0:3ac96e360672 1400 } else if (zone_preset != VL53L1_DEVICEZONEPRESET_NONE) {
charlesmn 0:3ac96e360672 1401
charlesmn 0:3ac96e360672 1402 if (status == VL53L1_ERROR_NONE)
charlesmn 0:3ac96e360672 1403 status =
charlesmn 0:3ac96e360672 1404 VL53L1_set_zone_preset(
charlesmn 0:3ac96e360672 1405 Dev,
charlesmn 0:3ac96e360672 1406 zone_preset);
charlesmn 0:3ac96e360672 1407 }
charlesmn 0:3ac96e360672 1408
charlesmn 0:3ac96e360672 1409
charlesmn 0:3ac96e360672 1410
charlesmn 0:3ac96e360672 1411 pres->zone_cal.preset_mode = device_preset_mode;
charlesmn 0:3ac96e360672 1412 pres->zone_cal.zone_preset = zone_preset;
charlesmn 0:3ac96e360672 1413
charlesmn 0:3ac96e360672 1414 pres->zone_cal.cal_distance_mm = cal_distance_mm * 16;
charlesmn 0:3ac96e360672 1415 pres->zone_cal.cal_reflectance_pc = cal_reflectance_pc;
charlesmn 0:3ac96e360672 1416 pres->zone_cal.max_zones = VL53L1_MAX_USER_ZONES;
charlesmn 0:3ac96e360672 1417 pres->zone_cal.active_zones = pdev->zone_cfg.active_zones + 1;
charlesmn 0:3ac96e360672 1418
charlesmn 0:3ac96e360672 1419 for (i = 0; i < VL53L1_MAX_USER_ZONES; i++) {
charlesmn 0:3ac96e360672 1420 pres->zone_cal.VL53L1_p_002[i].no_of_samples = 0;
charlesmn 0:3ac96e360672 1421 pres->zone_cal.VL53L1_p_002[i].effective_spads = 0;
charlesmn 0:3ac96e360672 1422 pres->zone_cal.VL53L1_p_002[i].peak_rate_mcps = 0;
charlesmn 0:3ac96e360672 1423 pres->zone_cal.VL53L1_p_002[i].VL53L1_p_014 = 0;
charlesmn 0:3ac96e360672 1424 pres->zone_cal.VL53L1_p_002[i].VL53L1_p_005 = 0;
charlesmn 0:3ac96e360672 1425 pres->zone_cal.VL53L1_p_002[i].median_range_mm = 0;
charlesmn 0:3ac96e360672 1426 pres->zone_cal.VL53L1_p_002[i].range_mm_offset = 0;
charlesmn 0:3ac96e360672 1427 }
charlesmn 0:3ac96e360672 1428
charlesmn 0:3ac96e360672 1429 pres->zone_cal.phasecal_result__reference_phase = 0;
charlesmn 0:3ac96e360672 1430 pres->zone_cal.zero_distance_phase = 0;
charlesmn 0:3ac96e360672 1431
charlesmn 0:3ac96e360672 1432
charlesmn 0:3ac96e360672 1433
charlesmn 0:3ac96e360672 1434 status =
charlesmn 0:3ac96e360672 1435 VL53L1_get_offset_correction_mode(
charlesmn 0:3ac96e360672 1436 Dev,
charlesmn 0:3ac96e360672 1437 &offset_cor_mode);
charlesmn 0:3ac96e360672 1438
charlesmn 0:3ac96e360672 1439 if (status == VL53L1_ERROR_NONE)
charlesmn 0:3ac96e360672 1440 status =
charlesmn 0:3ac96e360672 1441 VL53L1_set_offset_correction_mode(
charlesmn 0:3ac96e360672 1442 Dev,
charlesmn 0:3ac96e360672 1443 VL53L1_OFFSETCORRECTIONMODE__NONE);
charlesmn 0:3ac96e360672 1444
charlesmn 0:3ac96e360672 1445
charlesmn 0:3ac96e360672 1446 VL53L1_load_patch(Dev);
charlesmn 0:3ac96e360672 1447
charlesmn 0:3ac96e360672 1448 if (status == VL53L1_ERROR_NONE)
charlesmn 0:3ac96e360672 1449 status =
charlesmn 0:3ac96e360672 1450 VL53L1_init_and_start_range(
charlesmn 0:3ac96e360672 1451 Dev,
charlesmn 0:3ac96e360672 1452 measurement_mode,
charlesmn 0:3ac96e360672 1453 VL53L1_DEVICECONFIGLEVEL_CUSTOMER_ONWARDS);
charlesmn 0:3ac96e360672 1454
charlesmn 0:3ac96e360672 1455
charlesmn 0:3ac96e360672 1456
charlesmn 0:3ac96e360672 1457
charlesmn 0:3ac96e360672 1458 m = (pdev->zonecal_cfg.zone_num_of_samples + 2) *
charlesmn 0:3ac96e360672 1459 (uint16_t)pres->zone_cal.active_zones;
charlesmn 0:3ac96e360672 1460
charlesmn 0:3ac96e360672 1461
charlesmn 0:3ac96e360672 1462 for (i = 0; i <= m; i++) {
charlesmn 0:3ac96e360672 1463
charlesmn 0:3ac96e360672 1464
charlesmn 0:3ac96e360672 1465
charlesmn 0:3ac96e360672 1466 if (status == VL53L1_ERROR_NONE)
charlesmn 0:3ac96e360672 1467 status =
charlesmn 0:3ac96e360672 1468 VL53L1_wait_for_range_completion(Dev);
charlesmn 0:3ac96e360672 1469
charlesmn 0:3ac96e360672 1470
charlesmn 0:3ac96e360672 1471
charlesmn 0:3ac96e360672 1472 if (status == VL53L1_ERROR_NONE)
charlesmn 0:3ac96e360672 1473 status =
charlesmn 0:3ac96e360672 1474 VL53L1_get_device_results(
charlesmn 0:3ac96e360672 1475 Dev,
charlesmn 0:3ac96e360672 1476 VL53L1_DEVICERESULTSLEVEL_FULL,
charlesmn 0:3ac96e360672 1477 pRR);
charlesmn 0:3ac96e360672 1478
charlesmn 0:3ac96e360672 1479
charlesmn 0:3ac96e360672 1480
charlesmn 0:3ac96e360672 1481 prange_data = &(pRR->VL53L1_p_002[0]);
charlesmn 0:3ac96e360672 1482
charlesmn 0:3ac96e360672 1483 if (pRR->active_results > 0 &&
charlesmn 0:3ac96e360672 1484 i > (uint16_t)pres->zone_cal.active_zones) {
charlesmn 0:3ac96e360672 1485
charlesmn 0:3ac96e360672 1486 if (prange_data->range_status ==
charlesmn 0:3ac96e360672 1487 VL53L1_DEVICEERROR_RANGECOMPLETE) {
charlesmn 0:3ac96e360672 1488
charlesmn 0:3ac96e360672 1489 pres->zone_cal.phasecal_result__reference_phase
charlesmn 0:3ac96e360672 1490 =
charlesmn 0:3ac96e360672 1491 pdev->hist_data.phasecal_result__reference_phase
charlesmn 0:3ac96e360672 1492 ;
charlesmn 0:3ac96e360672 1493 pres->zone_cal.zero_distance_phase =
charlesmn 0:3ac96e360672 1494 pdev->hist_data.zero_distance_phase;
charlesmn 0:3ac96e360672 1495
charlesmn 0:3ac96e360672 1496 pzone_data =
charlesmn 0:3ac96e360672 1497 &(pres->zone_cal.VL53L1_p_002[pRR->zone_id]);
charlesmn 0:3ac96e360672 1498 pzone_data->no_of_samples++;
charlesmn 0:3ac96e360672 1499 pzone_data->effective_spads +=
charlesmn 0:3ac96e360672 1500 (uint32_t)prange_data->VL53L1_p_006;
charlesmn 0:3ac96e360672 1501 pzone_data->peak_rate_mcps += (uint32_t)(
charlesmn 0:3ac96e360672 1502 prange_data->peak_signal_count_rate_mcps);
charlesmn 0:3ac96e360672 1503 pzone_data->VL53L1_p_014 +=
charlesmn 0:3ac96e360672 1504 (uint32_t)prange_data->VL53L1_p_014;
charlesmn 0:3ac96e360672 1505 pzone_data->VL53L1_p_005 +=
charlesmn 0:3ac96e360672 1506 (uint32_t)prange_data->VL53L1_p_005;
charlesmn 0:3ac96e360672 1507 pzone_data->median_range_mm +=
charlesmn 0:3ac96e360672 1508 (int32_t)prange_data->median_range_mm;
charlesmn 0:3ac96e360672 1509
charlesmn 0:3ac96e360672 1510 }
charlesmn 0:3ac96e360672 1511 }
charlesmn 0:3ac96e360672 1512
charlesmn 0:3ac96e360672 1513
charlesmn 0:3ac96e360672 1514
charlesmn 0:3ac96e360672 1515 if (status == VL53L1_ERROR_NONE)
charlesmn 0:3ac96e360672 1516 status =
charlesmn 0:3ac96e360672 1517 VL53L1_wait_for_firmware_ready(Dev);
charlesmn 0:3ac96e360672 1518
charlesmn 0:3ac96e360672 1519
charlesmn 0:3ac96e360672 1520
charlesmn 0:3ac96e360672 1521 if (status == VL53L1_ERROR_NONE)
charlesmn 0:3ac96e360672 1522 status =
charlesmn 0:3ac96e360672 1523 VL53L1_clear_interrupt_and_enable_next_range(
charlesmn 0:3ac96e360672 1524 Dev,
charlesmn 0:3ac96e360672 1525 measurement_mode);
charlesmn 0:3ac96e360672 1526 }
charlesmn 0:3ac96e360672 1527
charlesmn 0:3ac96e360672 1528
charlesmn 0:3ac96e360672 1529
charlesmn 0:3ac96e360672 1530 if (status == VL53L1_ERROR_NONE)
charlesmn 0:3ac96e360672 1531 status = VL53L1_stop_range(Dev);
charlesmn 0:3ac96e360672 1532
charlesmn 0:3ac96e360672 1533
charlesmn 0:3ac96e360672 1534 if (status == VL53L1_ERROR_NONE)
charlesmn 0:3ac96e360672 1535 status = VL53L1_WaitUs(Dev, 1000);
charlesmn 0:3ac96e360672 1536 VL53L1_unload_patch(Dev);
charlesmn 0:3ac96e360672 1537
charlesmn 0:3ac96e360672 1538
charlesmn 0:3ac96e360672 1539 if (status == VL53L1_ERROR_NONE)
charlesmn 0:3ac96e360672 1540 status =
charlesmn 0:3ac96e360672 1541 VL53L1_run_phasecal_average(
charlesmn 0:3ac96e360672 1542 Dev,
charlesmn 0:3ac96e360672 1543 measurement_mode,
charlesmn 0:3ac96e360672 1544 pdev->hist_data.phasecal_result__vcsel_start,
charlesmn 0:3ac96e360672 1545
charlesmn 0:3ac96e360672 1546 pdev->zonecal_cfg.phasecal_num_of_samples,
charlesmn 0:3ac96e360672 1547
charlesmn 0:3ac96e360672 1548 pRR,
charlesmn 0:3ac96e360672 1549 &(pres->zone_cal.phasecal_result__reference_phase),
charlesmn 0:3ac96e360672 1550 &(pres->zone_cal.zero_distance_phase));
charlesmn 0:3ac96e360672 1551
charlesmn 0:3ac96e360672 1552
charlesmn 0:3ac96e360672 1553
charlesmn 0:3ac96e360672 1554 if (status == VL53L1_ERROR_NONE)
charlesmn 0:3ac96e360672 1555 status =
charlesmn 0:3ac96e360672 1556 VL53L1_set_offset_correction_mode(
charlesmn 0:3ac96e360672 1557 Dev,
charlesmn 0:3ac96e360672 1558 offset_cor_mode);
charlesmn 0:3ac96e360672 1559
charlesmn 0:3ac96e360672 1560
charlesmn 0:3ac96e360672 1561
charlesmn 0:3ac96e360672 1562 if (status == VL53L1_ERROR_NONE) {
charlesmn 0:3ac96e360672 1563
charlesmn 0:3ac96e360672 1564 for (z = 0; z < pres->zone_cal.active_zones; z++) {
charlesmn 0:3ac96e360672 1565
charlesmn 0:3ac96e360672 1566 pzone_data = &(pres->zone_cal.VL53L1_p_002[z]);
charlesmn 0:3ac96e360672 1567
charlesmn 0:3ac96e360672 1568
charlesmn 0:3ac96e360672 1569 if (pzone_data->no_of_samples > 0) {
charlesmn 0:3ac96e360672 1570
charlesmn 0:3ac96e360672 1571 pzone_data->effective_spads +=
charlesmn 0:3ac96e360672 1572 (pzone_data->no_of_samples/2);
charlesmn 0:3ac96e360672 1573 pzone_data->effective_spads /=
charlesmn 0:3ac96e360672 1574 pzone_data->no_of_samples;
charlesmn 0:3ac96e360672 1575
charlesmn 0:3ac96e360672 1576 pzone_data->peak_rate_mcps +=
charlesmn 0:3ac96e360672 1577 (pzone_data->no_of_samples/2);
charlesmn 0:3ac96e360672 1578 pzone_data->peak_rate_mcps /=
charlesmn 0:3ac96e360672 1579 pzone_data->no_of_samples;
charlesmn 0:3ac96e360672 1580
charlesmn 0:3ac96e360672 1581 pzone_data->VL53L1_p_014 +=
charlesmn 0:3ac96e360672 1582 (pzone_data->no_of_samples/2);
charlesmn 0:3ac96e360672 1583 pzone_data->VL53L1_p_014 /=
charlesmn 0:3ac96e360672 1584 pzone_data->no_of_samples;
charlesmn 0:3ac96e360672 1585
charlesmn 0:3ac96e360672 1586 pzone_data->VL53L1_p_005 +=
charlesmn 0:3ac96e360672 1587 (pzone_data->no_of_samples/2);
charlesmn 0:3ac96e360672 1588 pzone_data->VL53L1_p_005 /=
charlesmn 0:3ac96e360672 1589 pzone_data->no_of_samples;
charlesmn 0:3ac96e360672 1590
charlesmn 0:3ac96e360672 1591
charlesmn 0:3ac96e360672 1592
charlesmn 0:3ac96e360672 1593 pzone_data->median_range_mm =
charlesmn 0:3ac96e360672 1594 VL53L1_range_maths(
charlesmn 0:3ac96e360672 1595 pdev->stat_nvm.osc_measured__fast_osc__frequency
charlesmn 0:3ac96e360672 1596 , (uint16_t)pzone_data->VL53L1_p_014,
charlesmn 0:3ac96e360672 1597 pres->zone_cal.zero_distance_phase,
charlesmn 0:3ac96e360672 1598 2,
charlesmn 0:3ac96e360672 1599 0x0800,
charlesmn 0:3ac96e360672 1600 0);
charlesmn 0:3ac96e360672 1601
charlesmn 0:3ac96e360672 1602 pzone_data->range_mm_offset =
charlesmn 0:3ac96e360672 1603 ((int32_t)cal_distance_mm) * 4;
charlesmn 0:3ac96e360672 1604 pzone_data->range_mm_offset -=
charlesmn 0:3ac96e360672 1605 pzone_data->median_range_mm;
charlesmn 0:3ac96e360672 1606
charlesmn 0:3ac96e360672 1607
charlesmn 0:3ac96e360672 1608 if (pzone_data->no_of_samples <
charlesmn 0:3ac96e360672 1609 pdev->zonecal_cfg.zone_num_of_samples)
charlesmn 0:3ac96e360672 1610 status =
charlesmn 0:3ac96e360672 1611 VL53L1_WARNING_ZONE_CAL_MISSING_SAMPLES;
charlesmn 0:3ac96e360672 1612
charlesmn 0:3ac96e360672 1613
charlesmn 0:3ac96e360672 1614 if (pzone_data->VL53L1_p_005 >
charlesmn 0:3ac96e360672 1615 ((uint32_t)VL53L1_ZONE_CAL_MAX_SIGMA_MM
charlesmn 0:3ac96e360672 1616 << 5))
charlesmn 0:3ac96e360672 1617 status =
charlesmn 0:3ac96e360672 1618 VL53L1_WARNING_ZONE_CAL_SIGMA_TOO_HIGH;
charlesmn 0:3ac96e360672 1619
charlesmn 0:3ac96e360672 1620 if (pzone_data->peak_rate_mcps >
charlesmn 0:3ac96e360672 1621 VL53L1_ZONE_CAL_MAX_PRE_PEAK_RATE_MCPS)
charlesmn 0:3ac96e360672 1622 status =
charlesmn 0:3ac96e360672 1623 VL53L1_WARNING_ZONE_CAL_RATE_TOO_HIGH;
charlesmn 0:3ac96e360672 1624
charlesmn 0:3ac96e360672 1625 } else {
charlesmn 0:3ac96e360672 1626 status = VL53L1_ERROR_ZONE_CAL_NO_SAMPLE_FAIL;
charlesmn 0:3ac96e360672 1627 }
charlesmn 0:3ac96e360672 1628 }
charlesmn 0:3ac96e360672 1629 }
charlesmn 0:3ac96e360672 1630
charlesmn 0:3ac96e360672 1631
charlesmn 0:3ac96e360672 1632
charlesmn 0:3ac96e360672 1633 pres->zone_cal.cal_status = status;
charlesmn 0:3ac96e360672 1634 *pcal_status = pres->zone_cal.cal_status;
charlesmn 0:3ac96e360672 1635
charlesmn 0:3ac96e360672 1636
charlesmn 0:3ac96e360672 1637
charlesmn 0:3ac96e360672 1638 IGNORE_STATUS(
charlesmn 0:3ac96e360672 1639 IGNORE_ZONE_CAL_MISSING_SAMPLES,
charlesmn 0:3ac96e360672 1640 VL53L1_WARNING_ZONE_CAL_MISSING_SAMPLES,
charlesmn 0:3ac96e360672 1641 status);
charlesmn 0:3ac96e360672 1642
charlesmn 0:3ac96e360672 1643 IGNORE_STATUS(
charlesmn 0:3ac96e360672 1644 IGNORE_ZONE_CAL_SIGMA_TOO_HIGH,
charlesmn 0:3ac96e360672 1645 VL53L1_WARNING_ZONE_CAL_SIGMA_TOO_HIGH,
charlesmn 0:3ac96e360672 1646 status);
charlesmn 0:3ac96e360672 1647
charlesmn 0:3ac96e360672 1648 IGNORE_STATUS(
charlesmn 0:3ac96e360672 1649 IGNORE_ZONE_CAL_RATE_TOO_HIGH,
charlesmn 0:3ac96e360672 1650 VL53L1_WARNING_ZONE_CAL_RATE_TOO_HIGH,
charlesmn 0:3ac96e360672 1651 status);
charlesmn 0:3ac96e360672 1652
charlesmn 0:3ac96e360672 1653 #ifdef VL53L1_LOG_ENABLE
charlesmn 0:3ac96e360672 1654
charlesmn 0:3ac96e360672 1655
charlesmn 0:3ac96e360672 1656
charlesmn 0:3ac96e360672 1657 VL53L1_print_zone_calibration_results(
charlesmn 0:3ac96e360672 1658 &(pres->zone_cal),
charlesmn 0:3ac96e360672 1659 "run_zone_calibration():pdev->llresults.zone_cal.",
charlesmn 0:3ac96e360672 1660 VL53L1_TRACE_MODULE_OFFSET_DATA);
charlesmn 0:3ac96e360672 1661
charlesmn 0:3ac96e360672 1662 #endif
charlesmn 0:3ac96e360672 1663
charlesmn 0:3ac96e360672 1664 LOG_FUNCTION_END(status);
charlesmn 0:3ac96e360672 1665
charlesmn 0:3ac96e360672 1666 return status;
charlesmn 0:3ac96e360672 1667 }
charlesmn 0:3ac96e360672 1668
charlesmn 0:3ac96e360672 1669
charlesmn 0:3ac96e360672 1670 VL53L1_Error VL53L1_run_spad_rate_map(
charlesmn 0:3ac96e360672 1671 VL53L1_DEV Dev,
charlesmn 0:3ac96e360672 1672 VL53L1_DeviceTestMode device_test_mode,
charlesmn 0:3ac96e360672 1673 VL53L1_DeviceSscArray array_select,
charlesmn 0:3ac96e360672 1674 uint32_t ssc_config_timeout_us,
charlesmn 0:3ac96e360672 1675 VL53L1_spad_rate_data_t *pspad_rate_data)
charlesmn 0:3ac96e360672 1676 {
charlesmn 0:3ac96e360672 1677
charlesmn 0:3ac96e360672 1678
charlesmn 0:3ac96e360672 1679
charlesmn 0:3ac96e360672 1680 VL53L1_Error status = VL53L1_ERROR_NONE;
charlesmn 0:3ac96e360672 1681
charlesmn 0:3ac96e360672 1682 VL53L1_LLDriverData_t *pdev =
charlesmn 0:3ac96e360672 1683 VL53L1DevStructGetLLDriverHandle(Dev);
charlesmn 0:3ac96e360672 1684
charlesmn 0:3ac96e360672 1685 LOG_FUNCTION_START("");
charlesmn 0:3ac96e360672 1686
charlesmn 0:3ac96e360672 1687
charlesmn 0:3ac96e360672 1688 if (status == VL53L1_ERROR_NONE)
charlesmn 0:3ac96e360672 1689 status = VL53L1_enable_powerforce(Dev);
charlesmn 0:3ac96e360672 1690
charlesmn 0:3ac96e360672 1691
charlesmn 0:3ac96e360672 1692
charlesmn 0:3ac96e360672 1693 if (status == VL53L1_ERROR_NONE) {
charlesmn 0:3ac96e360672 1694 pdev->ssc_cfg.array_select = array_select;
charlesmn 0:3ac96e360672 1695 pdev->ssc_cfg.timeout_us = ssc_config_timeout_us;
charlesmn 0:3ac96e360672 1696 status =
charlesmn 0:3ac96e360672 1697 VL53L1_set_ssc_config(
charlesmn 0:3ac96e360672 1698 Dev,
charlesmn 0:3ac96e360672 1699 &(pdev->ssc_cfg),
charlesmn 0:3ac96e360672 1700 pdev->stat_nvm.osc_measured__fast_osc__frequency);
charlesmn 0:3ac96e360672 1701 }
charlesmn 0:3ac96e360672 1702
charlesmn 0:3ac96e360672 1703
charlesmn 0:3ac96e360672 1704
charlesmn 0:3ac96e360672 1705 if (status == VL53L1_ERROR_NONE)
charlesmn 0:3ac96e360672 1706 status =
charlesmn 0:3ac96e360672 1707 VL53L1_run_device_test(
charlesmn 0:3ac96e360672 1708 Dev,
charlesmn 0:3ac96e360672 1709 device_test_mode);
charlesmn 0:3ac96e360672 1710
charlesmn 0:3ac96e360672 1711
charlesmn 0:3ac96e360672 1712
charlesmn 0:3ac96e360672 1713 if (status == VL53L1_ERROR_NONE)
charlesmn 0:3ac96e360672 1714 status =
charlesmn 0:3ac96e360672 1715 VL53L1_get_spad_rate_data(
charlesmn 0:3ac96e360672 1716 Dev,
charlesmn 0:3ac96e360672 1717 pspad_rate_data);
charlesmn 0:3ac96e360672 1718
charlesmn 0:3ac96e360672 1719 if (device_test_mode == VL53L1_DEVICETESTMODE_LCR_VCSEL_ON)
charlesmn 0:3ac96e360672 1720 pspad_rate_data->fractional_bits = 7;
charlesmn 0:3ac96e360672 1721 else
charlesmn 0:3ac96e360672 1722 pspad_rate_data->fractional_bits = 15;
charlesmn 0:3ac96e360672 1723
charlesmn 0:3ac96e360672 1724
charlesmn 0:3ac96e360672 1725
charlesmn 0:3ac96e360672 1726 if (status == VL53L1_ERROR_NONE)
charlesmn 0:3ac96e360672 1727 status = VL53L1_disable_powerforce(Dev);
charlesmn 0:3ac96e360672 1728
charlesmn 0:3ac96e360672 1729 #ifdef VL53L1_LOG_ENABLE
charlesmn 0:3ac96e360672 1730
charlesmn 0:3ac96e360672 1731
charlesmn 0:3ac96e360672 1732 if (status == VL53L1_ERROR_NONE) {
charlesmn 0:3ac96e360672 1733 VL53L1_print_spad_rate_data(
charlesmn 0:3ac96e360672 1734 pspad_rate_data,
charlesmn 0:3ac96e360672 1735 "run_spad_rate_map():",
charlesmn 0:3ac96e360672 1736 VL53L1_TRACE_MODULE_SPAD_RATE_MAP);
charlesmn 0:3ac96e360672 1737 VL53L1_print_spad_rate_map(
charlesmn 0:3ac96e360672 1738 pspad_rate_data,
charlesmn 0:3ac96e360672 1739 "run_spad_rate_map():",
charlesmn 0:3ac96e360672 1740 VL53L1_TRACE_MODULE_SPAD_RATE_MAP);
charlesmn 0:3ac96e360672 1741 }
charlesmn 0:3ac96e360672 1742 #endif
charlesmn 0:3ac96e360672 1743
charlesmn 0:3ac96e360672 1744 LOG_FUNCTION_END(status);
charlesmn 0:3ac96e360672 1745
charlesmn 0:3ac96e360672 1746 return status;
charlesmn 0:3ac96e360672 1747 }
charlesmn 0:3ac96e360672 1748
charlesmn 0:3ac96e360672 1749
charlesmn 0:3ac96e360672 1750 VL53L1_Error VL53L1_run_device_test(
charlesmn 0:3ac96e360672 1751 VL53L1_DEV Dev,
charlesmn 0:3ac96e360672 1752 VL53L1_DeviceTestMode device_test_mode)
charlesmn 0:3ac96e360672 1753 {
charlesmn 0:3ac96e360672 1754
charlesmn 0:3ac96e360672 1755
charlesmn 0:3ac96e360672 1756 VL53L1_Error status = VL53L1_ERROR_NONE;
charlesmn 0:3ac96e360672 1757 VL53L1_LLDriverData_t *pdev = VL53L1DevStructGetLLDriverHandle(Dev);
charlesmn 0:3ac96e360672 1758
charlesmn 0:3ac96e360672 1759 uint8_t comms_buffer[2];
charlesmn 0:3ac96e360672 1760 uint8_t gpio_hv_mux__ctrl = 0;
charlesmn 0:3ac96e360672 1761
charlesmn 0:3ac96e360672 1762 LOG_FUNCTION_START("");
charlesmn 0:3ac96e360672 1763
charlesmn 0:3ac96e360672 1764
charlesmn 0:3ac96e360672 1765
charlesmn 0:3ac96e360672 1766 if (status == VL53L1_ERROR_NONE)
charlesmn 0:3ac96e360672 1767 status =
charlesmn 0:3ac96e360672 1768 VL53L1_RdByte(
charlesmn 0:3ac96e360672 1769 Dev,
charlesmn 0:3ac96e360672 1770 VL53L1_GPIO_HV_MUX__CTRL,
charlesmn 0:3ac96e360672 1771 &gpio_hv_mux__ctrl);
charlesmn 0:3ac96e360672 1772
charlesmn 0:3ac96e360672 1773 if (status == VL53L1_ERROR_NONE)
charlesmn 0:3ac96e360672 1774 pdev->stat_cfg.gpio_hv_mux__ctrl = gpio_hv_mux__ctrl;
charlesmn 0:3ac96e360672 1775
charlesmn 0:3ac96e360672 1776
charlesmn 0:3ac96e360672 1777 if (status == VL53L1_ERROR_NONE)
charlesmn 0:3ac96e360672 1778 status = VL53L1_start_test(
charlesmn 0:3ac96e360672 1779 Dev,
charlesmn 0:3ac96e360672 1780 device_test_mode);
charlesmn 0:3ac96e360672 1781
charlesmn 0:3ac96e360672 1782
charlesmn 0:3ac96e360672 1783 if (status == VL53L1_ERROR_NONE)
charlesmn 0:3ac96e360672 1784 status = VL53L1_wait_for_test_completion(Dev);
charlesmn 0:3ac96e360672 1785
charlesmn 0:3ac96e360672 1786
charlesmn 0:3ac96e360672 1787 if (status == VL53L1_ERROR_NONE)
charlesmn 0:3ac96e360672 1788 status =
charlesmn 0:3ac96e360672 1789 VL53L1_ReadMulti(
charlesmn 0:3ac96e360672 1790 Dev,
charlesmn 0:3ac96e360672 1791 VL53L1_RESULT__RANGE_STATUS,
charlesmn 0:3ac96e360672 1792 comms_buffer,
charlesmn 0:3ac96e360672 1793 2);
charlesmn 0:3ac96e360672 1794
charlesmn 0:3ac96e360672 1795 if (status == VL53L1_ERROR_NONE) {
charlesmn 0:3ac96e360672 1796 pdev->sys_results.result__range_status = comms_buffer[0];
charlesmn 0:3ac96e360672 1797 pdev->sys_results.result__report_status = comms_buffer[1];
charlesmn 0:3ac96e360672 1798 }
charlesmn 0:3ac96e360672 1799
charlesmn 0:3ac96e360672 1800
charlesmn 0:3ac96e360672 1801
charlesmn 0:3ac96e360672 1802 pdev->sys_results.result__range_status &=
charlesmn 0:3ac96e360672 1803 VL53L1_RANGE_STATUS__RANGE_STATUS_MASK;
charlesmn 0:3ac96e360672 1804
charlesmn 0:3ac96e360672 1805 if (status == VL53L1_ERROR_NONE) {
charlesmn 0:3ac96e360672 1806 trace_print(
charlesmn 0:3ac96e360672 1807 VL53L1_TRACE_LEVEL_INFO,
charlesmn 0:3ac96e360672 1808 " Device Test Complete:\n\t%-32s = %3u\n\t%-32s = %3u\n",
charlesmn 0:3ac96e360672 1809 "result__range_status",
charlesmn 0:3ac96e360672 1810 pdev->sys_results.result__range_status,
charlesmn 0:3ac96e360672 1811 "result__report_status",
charlesmn 0:3ac96e360672 1812 pdev->sys_results.result__report_status);
charlesmn 0:3ac96e360672 1813
charlesmn 0:3ac96e360672 1814
charlesmn 0:3ac96e360672 1815 if (status == VL53L1_ERROR_NONE)
charlesmn 0:3ac96e360672 1816 status = VL53L1_clear_interrupt(Dev);
charlesmn 0:3ac96e360672 1817 }
charlesmn 0:3ac96e360672 1818
charlesmn 0:3ac96e360672 1819
charlesmn 0:3ac96e360672 1820
charlesmn 0:3ac96e360672 1821 if (status == VL53L1_ERROR_NONE)
charlesmn 0:3ac96e360672 1822 status =
charlesmn 0:3ac96e360672 1823 VL53L1_start_test(
charlesmn 0:3ac96e360672 1824 Dev,
charlesmn 0:3ac96e360672 1825 0x00);
charlesmn 0:3ac96e360672 1826
charlesmn 0:3ac96e360672 1827 LOG_FUNCTION_END(status);
charlesmn 0:3ac96e360672 1828
charlesmn 0:3ac96e360672 1829 return status;
charlesmn 0:3ac96e360672 1830 }
charlesmn 0:3ac96e360672 1831
charlesmn 0:3ac96e360672 1832
charlesmn 0:3ac96e360672 1833 void VL53L1_hist_xtalk_extract_data_init(
charlesmn 0:3ac96e360672 1834 VL53L1_hist_xtalk_extract_data_t *pxtalk_data)
charlesmn 0:3ac96e360672 1835 {
charlesmn 0:3ac96e360672 1836
charlesmn 0:3ac96e360672 1837
charlesmn 0:3ac96e360672 1838 int32_t lb = 0;
charlesmn 0:3ac96e360672 1839
charlesmn 0:3ac96e360672 1840 pxtalk_data->sample_count = 0U;
charlesmn 0:3ac96e360672 1841 pxtalk_data->pll_period_mm = 0U;
charlesmn 0:3ac96e360672 1842 pxtalk_data->peak_duration_us_sum = 0U;
charlesmn 0:3ac96e360672 1843 pxtalk_data->effective_spad_count_sum = 0U;
charlesmn 0:3ac96e360672 1844 pxtalk_data->zero_distance_phase_sum = 0U;
charlesmn 0:3ac96e360672 1845 pxtalk_data->zero_distance_phase_avg = 0U;
charlesmn 0:3ac96e360672 1846 pxtalk_data->event_scaler_sum = 0U;
charlesmn 0:3ac96e360672 1847 pxtalk_data->event_scaler_avg = 4096U;
charlesmn 0:3ac96e360672 1848 pxtalk_data->signal_events_sum = 0;
charlesmn 0:3ac96e360672 1849 pxtalk_data->xtalk_rate_kcps_per_spad = 0U;
charlesmn 0:3ac96e360672 1850 pxtalk_data->VL53L1_p_015 = 0U;
charlesmn 0:3ac96e360672 1851 pxtalk_data->VL53L1_p_016 = 0U;
charlesmn 0:3ac96e360672 1852 pxtalk_data->target_start = 0U;
charlesmn 0:3ac96e360672 1853
charlesmn 0:3ac96e360672 1854 for (lb = 0; lb < VL53L1_XTALK_HISTO_BINS; lb++)
charlesmn 0:3ac96e360672 1855 pxtalk_data->bin_data_sums[lb] = 0;
charlesmn 0:3ac96e360672 1856
charlesmn 0:3ac96e360672 1857 }
charlesmn 0:3ac96e360672 1858
charlesmn 0:3ac96e360672 1859
charlesmn 0:3ac96e360672 1860 VL53L1_Error VL53L1_hist_xtalk_extract_update(
charlesmn 0:3ac96e360672 1861 int16_t target_distance_mm,
charlesmn 0:3ac96e360672 1862 uint16_t target_width_oversize,
charlesmn 0:3ac96e360672 1863 VL53L1_histogram_bin_data_t *phist_bins,
charlesmn 0:3ac96e360672 1864 VL53L1_hist_xtalk_extract_data_t *pxtalk_data)
charlesmn 0:3ac96e360672 1865 {
charlesmn 0:3ac96e360672 1866
charlesmn 0:3ac96e360672 1867
charlesmn 0:3ac96e360672 1868 VL53L1_Error status = VL53L1_ERROR_NONE;
charlesmn 0:3ac96e360672 1869
charlesmn 0:3ac96e360672 1870 LOG_FUNCTION_START("");
charlesmn 0:3ac96e360672 1871
charlesmn 0:3ac96e360672 1872 status =
charlesmn 0:3ac96e360672 1873 VL53L1_hist_xtalk_extract_calc_window(
charlesmn 0:3ac96e360672 1874 target_distance_mm,
charlesmn 0:3ac96e360672 1875 target_width_oversize,
charlesmn 0:3ac96e360672 1876 phist_bins,
charlesmn 0:3ac96e360672 1877 pxtalk_data);
charlesmn 0:3ac96e360672 1878
charlesmn 0:3ac96e360672 1879 if (status == VL53L1_ERROR_NONE) {
charlesmn 0:3ac96e360672 1880 status =
charlesmn 0:3ac96e360672 1881 VL53L1_hist_xtalk_extract_calc_event_sums(
charlesmn 0:3ac96e360672 1882 phist_bins,
charlesmn 0:3ac96e360672 1883 pxtalk_data);
charlesmn 0:3ac96e360672 1884 }
charlesmn 0:3ac96e360672 1885
charlesmn 0:3ac96e360672 1886 LOG_FUNCTION_END(status);
charlesmn 0:3ac96e360672 1887
charlesmn 0:3ac96e360672 1888 return status;
charlesmn 0:3ac96e360672 1889 }
charlesmn 0:3ac96e360672 1890
charlesmn 0:3ac96e360672 1891
charlesmn 0:3ac96e360672 1892 VL53L1_Error VL53L1_hist_xtalk_extract_fini(
charlesmn 0:3ac96e360672 1893 VL53L1_histogram_bin_data_t *phist_bins,
charlesmn 0:3ac96e360672 1894 VL53L1_hist_xtalk_extract_data_t *pxtalk_data,
charlesmn 0:3ac96e360672 1895 VL53L1_xtalk_calibration_results_t *pxtalk_cal,
charlesmn 0:3ac96e360672 1896 VL53L1_xtalk_histogram_shape_t *pxtalk_shape)
charlesmn 0:3ac96e360672 1897 {
charlesmn 0:3ac96e360672 1898
charlesmn 0:3ac96e360672 1899
charlesmn 0:3ac96e360672 1900 VL53L1_Error status = VL53L1_ERROR_NONE;
charlesmn 0:3ac96e360672 1901 VL53L1_xtalk_calibration_results_t *pX = pxtalk_cal;
charlesmn 0:3ac96e360672 1902
charlesmn 0:3ac96e360672 1903 LOG_FUNCTION_START("");
charlesmn 0:3ac96e360672 1904
charlesmn 0:3ac96e360672 1905 if (pxtalk_data->sample_count > 0) {
charlesmn 0:3ac96e360672 1906
charlesmn 0:3ac96e360672 1907
charlesmn 0:3ac96e360672 1908 pxtalk_data->event_scaler_avg = pxtalk_data->event_scaler_sum;
charlesmn 0:3ac96e360672 1909 pxtalk_data->event_scaler_avg +=
charlesmn 0:3ac96e360672 1910 (pxtalk_data->sample_count >> 1);
charlesmn 0:3ac96e360672 1911 pxtalk_data->event_scaler_avg /= pxtalk_data->sample_count;
charlesmn 0:3ac96e360672 1912
charlesmn 0:3ac96e360672 1913
charlesmn 0:3ac96e360672 1914
charlesmn 0:3ac96e360672 1915 status =
charlesmn 0:3ac96e360672 1916 VL53L1_hist_xtalk_extract_calc_rate_per_spad(
charlesmn 0:3ac96e360672 1917 pxtalk_data);
charlesmn 0:3ac96e360672 1918
charlesmn 0:3ac96e360672 1919
charlesmn 0:3ac96e360672 1920
charlesmn 0:3ac96e360672 1921 if (status == VL53L1_ERROR_NONE) {
charlesmn 0:3ac96e360672 1922
charlesmn 0:3ac96e360672 1923
charlesmn 0:3ac96e360672 1924 pxtalk_data->zero_distance_phase_avg =
charlesmn 0:3ac96e360672 1925 pxtalk_data->zero_distance_phase_sum;
charlesmn 0:3ac96e360672 1926 pxtalk_data->zero_distance_phase_avg +=
charlesmn 0:3ac96e360672 1927 (pxtalk_data->sample_count >> 1);
charlesmn 0:3ac96e360672 1928 pxtalk_data->zero_distance_phase_avg /=
charlesmn 0:3ac96e360672 1929 pxtalk_data->sample_count;
charlesmn 0:3ac96e360672 1930
charlesmn 0:3ac96e360672 1931
charlesmn 0:3ac96e360672 1932 status =
charlesmn 0:3ac96e360672 1933 VL53L1_hist_xtalk_extract_calc_shape(
charlesmn 0:3ac96e360672 1934 pxtalk_data,
charlesmn 0:3ac96e360672 1935 pxtalk_shape);
charlesmn 0:3ac96e360672 1936
charlesmn 0:3ac96e360672 1937
charlesmn 0:3ac96e360672 1938
charlesmn 0:3ac96e360672 1939
charlesmn 0:3ac96e360672 1940 pxtalk_shape->phasecal_result__vcsel_start =
charlesmn 0:3ac96e360672 1941 phist_bins->phasecal_result__vcsel_start;
charlesmn 0:3ac96e360672 1942 pxtalk_shape->cal_config__vcsel_start =
charlesmn 0:3ac96e360672 1943 phist_bins->cal_config__vcsel_start;
charlesmn 0:3ac96e360672 1944 pxtalk_shape->vcsel_width =
charlesmn 0:3ac96e360672 1945 phist_bins->vcsel_width;
charlesmn 0:3ac96e360672 1946 pxtalk_shape->VL53L1_p_019 =
charlesmn 0:3ac96e360672 1947 phist_bins->VL53L1_p_019;
charlesmn 0:3ac96e360672 1948 }
charlesmn 0:3ac96e360672 1949
charlesmn 0:3ac96e360672 1950
charlesmn 0:3ac96e360672 1951 if (status == VL53L1_ERROR_NONE) {
charlesmn 0:3ac96e360672 1952
charlesmn 0:3ac96e360672 1953
charlesmn 0:3ac96e360672 1954 pX->algo__crosstalk_compensation_plane_offset_kcps =
charlesmn 0:3ac96e360672 1955 pxtalk_data->xtalk_rate_kcps_per_spad;
charlesmn 0:3ac96e360672 1956 pX->algo__crosstalk_compensation_x_plane_gradient_kcps
charlesmn 0:3ac96e360672 1957 = 0U;
charlesmn 0:3ac96e360672 1958 pX->algo__crosstalk_compensation_y_plane_gradient_kcps
charlesmn 0:3ac96e360672 1959 = 0U;
charlesmn 0:3ac96e360672 1960
charlesmn 0:3ac96e360672 1961 }
charlesmn 0:3ac96e360672 1962 }
charlesmn 0:3ac96e360672 1963
charlesmn 0:3ac96e360672 1964 LOG_FUNCTION_END(status);
charlesmn 0:3ac96e360672 1965
charlesmn 0:3ac96e360672 1966 return status;
charlesmn 0:3ac96e360672 1967 }
charlesmn 0:3ac96e360672 1968
charlesmn 0:3ac96e360672 1969
charlesmn 0:3ac96e360672 1970 VL53L1_Error VL53L1_run_hist_xtalk_extraction(
charlesmn 0:3ac96e360672 1971 VL53L1_DEV Dev,
charlesmn 0:3ac96e360672 1972 int16_t cal_distance_mm,
charlesmn 0:3ac96e360672 1973 VL53L1_Error *pcal_status)
charlesmn 0:3ac96e360672 1974 {
charlesmn 0:3ac96e360672 1975
charlesmn 0:3ac96e360672 1976
charlesmn 0:3ac96e360672 1977 #define OVERSIZE 4
charlesmn 0:3ac96e360672 1978 VL53L1_Error status = VL53L1_ERROR_NONE;
charlesmn 0:3ac96e360672 1979 VL53L1_LLDriverData_t *pdev = VL53L1DevStructGetLLDriverHandle(Dev);
charlesmn 0:3ac96e360672 1980 VL53L1_xtalkextract_config_t *pX = &(pdev->xtalk_extract_cfg);
charlesmn 0:3ac96e360672 1981 VL53L1_xtalk_config_t *pC = &(pdev->xtalk_cfg);
charlesmn 0:3ac96e360672 1982 VL53L1_xtalk_calibration_results_t *pXC = &(pdev->xtalk_cal);
charlesmn 0:3ac96e360672 1983
charlesmn 0:3ac96e360672 1984
charlesmn 0:3ac96e360672 1985
charlesmn 0:3ac96e360672 1986 uint8_t smudge_corr_en = 0;
charlesmn 0:3ac96e360672 1987 uint8_t i = 0;
charlesmn 0:3ac96e360672 1988 int8_t k = 0;
charlesmn 0:3ac96e360672 1989 uint8_t nbloops;
charlesmn 0:3ac96e360672 1990 int32_t initMergeSize = 0;
charlesmn 0:3ac96e360672 1991 int32_t MergeEnabled = 0;
charlesmn 0:3ac96e360672 1992 uint32_t deltaXtalk;
charlesmn 0:3ac96e360672 1993 uint32_t stepXtalk;
charlesmn 0:3ac96e360672 1994 uint32_t XtalkMin;
charlesmn 0:3ac96e360672 1995 uint32_t XtalkMax;
charlesmn 0:3ac96e360672 1996 uint8_t measurement_mode = VL53L1_DEVICEMEASUREMENTMODE_BACKTOBACK;
charlesmn 0:3ac96e360672 1997 int8_t MaxId;
charlesmn 0:3ac96e360672 1998 uint8_t histo_merge_nb;
charlesmn 0:3ac96e360672 1999 uint8_t wait_for_accumulation;
charlesmn 0:3ac96e360672 2000 VL53L1_range_results_t *prange_results =
charlesmn 0:3ac96e360672 2001 (VL53L1_range_results_t *) pdev->wArea1;
charlesmn 0:3ac96e360672 2002 uint8_t Very1stRange = 0;
charlesmn 0:3ac96e360672 2003
charlesmn 0:3ac96e360672 2004 LOG_FUNCTION_START("");
charlesmn 0:3ac96e360672 2005
charlesmn 0:3ac96e360672 2006
charlesmn 0:3ac96e360672 2007
charlesmn 0:3ac96e360672 2008 if (status == VL53L1_ERROR_NONE)
charlesmn 0:3ac96e360672 2009 status =
charlesmn 0:3ac96e360672 2010 VL53L1_set_preset_mode(
charlesmn 0:3ac96e360672 2011 Dev,
charlesmn 0:3ac96e360672 2012 VL53L1_DEVICEPRESETMODE_HISTOGRAM_LONG_RANGE,
charlesmn 0:3ac96e360672 2013 pX->dss_config__target_total_rate_mcps,
charlesmn 0:3ac96e360672 2014 pX->phasecal_config_timeout_us,
charlesmn 0:3ac96e360672 2015 pX->mm_config_timeout_us,
charlesmn 0:3ac96e360672 2016 pX->range_config_timeout_us,
charlesmn 0:3ac96e360672 2017 100);
charlesmn 0:3ac96e360672 2018
charlesmn 0:3ac96e360672 2019
charlesmn 0:3ac96e360672 2020
charlesmn 0:3ac96e360672 2021 if (status == VL53L1_ERROR_NONE)
charlesmn 0:3ac96e360672 2022 status = VL53L1_disable_xtalk_compensation(Dev);
charlesmn 0:3ac96e360672 2023
charlesmn 0:3ac96e360672 2024
charlesmn 0:3ac96e360672 2025
charlesmn 0:3ac96e360672 2026 smudge_corr_en = pdev->smudge_correct_config.smudge_corr_enabled;
charlesmn 0:3ac96e360672 2027
charlesmn 0:3ac96e360672 2028 if (status == VL53L1_ERROR_NONE)
charlesmn 0:3ac96e360672 2029 status = VL53L1_dynamic_xtalk_correction_disable(Dev);
charlesmn 0:3ac96e360672 2030
charlesmn 0:3ac96e360672 2031
charlesmn 0:3ac96e360672 2032 VL53L1_load_patch(Dev);
charlesmn 0:3ac96e360672 2033
charlesmn 0:3ac96e360672 2034 VL53L1_get_tuning_parm(Dev, VL53L1_TUNINGPARM_HIST_MERGE_MAX_SIZE,
charlesmn 0:3ac96e360672 2035 &initMergeSize);
charlesmn 0:3ac96e360672 2036 VL53L1_get_tuning_parm(Dev, VL53L1_TUNINGPARM_HIST_MERGE,
charlesmn 0:3ac96e360672 2037 &MergeEnabled);
charlesmn 0:3ac96e360672 2038 memset(&pdev->xtalk_cal, 0, sizeof(pdev->xtalk_cal));
charlesmn 0:3ac96e360672 2039
charlesmn 0:3ac96e360672 2040 if (status == VL53L1_ERROR_NONE)
charlesmn 0:3ac96e360672 2041 status = VL53L1_init_and_start_range(
charlesmn 0:3ac96e360672 2042 Dev, measurement_mode,
charlesmn 0:3ac96e360672 2043 VL53L1_DEVICECONFIGLEVEL_CUSTOMER_ONWARDS);
charlesmn 0:3ac96e360672 2044
charlesmn 0:3ac96e360672 2045 MaxId = pdev->tuning_parms.tp_hist_merge_max_size - 1;
charlesmn 0:3ac96e360672 2046 nbloops = (MergeEnabled == 0 ? 1 : 2);
charlesmn 0:3ac96e360672 2047 for (k = 0; k < nbloops; k++) {
charlesmn 0:3ac96e360672 2048
charlesmn 0:3ac96e360672 2049 VL53L1_hist_xtalk_extract_data_init(
charlesmn 0:3ac96e360672 2050 &(pdev->xtalk_extract));
charlesmn 0:3ac96e360672 2051 VL53L1_set_tuning_parm(Dev,
charlesmn 0:3ac96e360672 2052 VL53L1_TUNINGPARM_HIST_MERGE_MAX_SIZE,
charlesmn 0:3ac96e360672 2053 k * MaxId + 1);
charlesmn 0:3ac96e360672 2054
charlesmn 0:3ac96e360672 2055 for (i = 0; i <= pX->num_of_samples; i++) {
charlesmn 0:3ac96e360672 2056 if (status == VL53L1_ERROR_NONE)
charlesmn 0:3ac96e360672 2057 status = VL53L1_wait_for_range_completion(Dev);
charlesmn 0:3ac96e360672 2058 if (status == VL53L1_ERROR_NONE)
charlesmn 0:3ac96e360672 2059 status = VL53L1_get_device_results(Dev,
charlesmn 0:3ac96e360672 2060 VL53L1_DEVICERESULTSLEVEL_FULL,
charlesmn 0:3ac96e360672 2061 prange_results);
charlesmn 0:3ac96e360672 2062 Very1stRange =
charlesmn 0:3ac96e360672 2063 (pdev->ll_state.rd_device_state ==
charlesmn 0:3ac96e360672 2064 VL53L1_DEVICESTATE_RANGING_WAIT_GPH_SYNC);
charlesmn 0:3ac96e360672 2065
charlesmn 0:3ac96e360672 2066 VL53L1_compute_histo_merge_nb(Dev, &histo_merge_nb);
charlesmn 0:3ac96e360672 2067 wait_for_accumulation = ((k != 0) &&
charlesmn 0:3ac96e360672 2068 (MergeEnabled) &&
charlesmn 0:3ac96e360672 2069 (status == VL53L1_ERROR_NONE) &&
charlesmn 0:3ac96e360672 2070 (histo_merge_nb <
charlesmn 0:3ac96e360672 2071 pdev->tuning_parms.tp_hist_merge_max_size));
charlesmn 0:3ac96e360672 2072 if (wait_for_accumulation)
charlesmn 0:3ac96e360672 2073 i = 0;
charlesmn 0:3ac96e360672 2074 else {
charlesmn 0:3ac96e360672 2075 if ((status == VL53L1_ERROR_NONE) &&
charlesmn 0:3ac96e360672 2076 (!Very1stRange)) {
charlesmn 0:3ac96e360672 2077 status =
charlesmn 0:3ac96e360672 2078 VL53L1_hist_xtalk_extract_update(
charlesmn 0:3ac96e360672 2079 cal_distance_mm,
charlesmn 0:3ac96e360672 2080 OVERSIZE,
charlesmn 0:3ac96e360672 2081 &(pdev->hist_data),
charlesmn 0:3ac96e360672 2082 &(pdev->xtalk_extract));
charlesmn 0:3ac96e360672 2083 }
charlesmn 0:3ac96e360672 2084 }
charlesmn 0:3ac96e360672 2085
charlesmn 0:3ac96e360672 2086 if (status == VL53L1_ERROR_NONE)
charlesmn 0:3ac96e360672 2087 status = VL53L1_wait_for_firmware_ready(Dev);
charlesmn 0:3ac96e360672 2088 if (status == VL53L1_ERROR_NONE)
charlesmn 0:3ac96e360672 2089 status =
charlesmn 0:3ac96e360672 2090 VL53L1_clear_interrupt_and_enable_next_range(
charlesmn 0:3ac96e360672 2091 Dev, measurement_mode);
lugandc 18:0696efe39d08 2092 }
lugandc 18:0696efe39d08 2093
lugandc 18:0696efe39d08 2094
lugandc 18:0696efe39d08 2095 if (status == VL53L1_ERROR_NONE)
lugandc 18:0696efe39d08 2096 status =
lugandc 18:0696efe39d08 2097 VL53L1_hist_xtalk_extract_fini(
lugandc 18:0696efe39d08 2098 &(pdev->hist_data),
lugandc 18:0696efe39d08 2099 &(pdev->xtalk_extract),
lugandc 18:0696efe39d08 2100 &(pdev->xtalk_cal),
lugandc 18:0696efe39d08 2101 &(pdev->xtalk_shapes.xtalk_shape));
lugandc 18:0696efe39d08 2102 if (status == VL53L1_ERROR_NONE) {
charlesmn 0:3ac96e360672 2103 pXC->algo__xtalk_cpo_HistoMerge_kcps[k * MaxId] =
charlesmn 0:3ac96e360672 2104 pXC->algo__crosstalk_compensation_plane_offset_kcps;
charlesmn 0:3ac96e360672 2105 }
charlesmn 0:3ac96e360672 2106 }
charlesmn 0:3ac96e360672 2107
charlesmn 0:3ac96e360672 2108
charlesmn 0:3ac96e360672 2109 VL53L1_stop_range(Dev);
charlesmn 0:3ac96e360672 2110
charlesmn 0:3ac96e360672 2111 VL53L1_set_tuning_parm(Dev, VL53L1_TUNINGPARM_HIST_MERGE_MAX_SIZE,
charlesmn 0:3ac96e360672 2112 initMergeSize);
charlesmn 0:3ac96e360672 2113 VL53L1_unload_patch(Dev);
charlesmn 0:3ac96e360672 2114
charlesmn 0:3ac96e360672 2115 if (status != VL53L1_ERROR_NONE)
charlesmn 0:3ac96e360672 2116 status = VL53L1_ERROR_XTALK_EXTRACTION_SIGMA_LIMIT_FAIL;
charlesmn 0:3ac96e360672 2117 else if ((MergeEnabled == 1) && (MaxId > 0)) {
charlesmn 0:3ac96e360672 2118 XtalkMin = pXC->algo__xtalk_cpo_HistoMerge_kcps[0];
charlesmn 0:3ac96e360672 2119 XtalkMax = pXC->algo__xtalk_cpo_HistoMerge_kcps[MaxId];
charlesmn 0:3ac96e360672 2120 pXC->algo__crosstalk_compensation_plane_offset_kcps =
charlesmn 0:3ac96e360672 2121 XtalkMin;
charlesmn 0:3ac96e360672 2122 if (XtalkMax >= XtalkMin) {
charlesmn 0:3ac96e360672 2123 deltaXtalk = XtalkMax - XtalkMin;
charlesmn 0:3ac96e360672 2124 stepXtalk = deltaXtalk / MaxId;
charlesmn 0:3ac96e360672 2125 for (k = 1; k < MaxId; k++)
charlesmn 0:3ac96e360672 2126 pXC->algo__xtalk_cpo_HistoMerge_kcps[k] =
charlesmn 0:3ac96e360672 2127 XtalkMin + stepXtalk * k;
charlesmn 0:3ac96e360672 2128 } else
charlesmn 0:3ac96e360672 2129 status =
charlesmn 0:3ac96e360672 2130 VL53L1_ERROR_XTALK_EXTRACTION_SIGMA_LIMIT_FAIL;
charlesmn 0:3ac96e360672 2131 }
charlesmn 0:3ac96e360672 2132
charlesmn 0:3ac96e360672 2133 if (status == VL53L1_ERROR_NONE) {
charlesmn 0:3ac96e360672 2134 pC->algo__crosstalk_compensation_x_plane_gradient_kcps =
charlesmn 0:3ac96e360672 2135 pXC->algo__crosstalk_compensation_x_plane_gradient_kcps;
charlesmn 0:3ac96e360672 2136 pC->algo__crosstalk_compensation_y_plane_gradient_kcps =
charlesmn 0:3ac96e360672 2137 pXC->algo__crosstalk_compensation_y_plane_gradient_kcps;
charlesmn 0:3ac96e360672 2138 pC->algo__crosstalk_compensation_plane_offset_kcps =
charlesmn 0:3ac96e360672 2139 pXC->algo__crosstalk_compensation_plane_offset_kcps;
charlesmn 0:3ac96e360672 2140 }
charlesmn 0:3ac96e360672 2141
charlesmn 0:3ac96e360672 2142
charlesmn 0:3ac96e360672 2143 pdev->xtalk_results.cal_status = status;
charlesmn 0:3ac96e360672 2144 *pcal_status = pdev->xtalk_results.cal_status;
charlesmn 0:3ac96e360672 2145
charlesmn 0:3ac96e360672 2146
charlesmn 0:3ac96e360672 2147 status = VL53L1_enable_xtalk_compensation(Dev);
charlesmn 0:3ac96e360672 2148 if (smudge_corr_en == 1)
charlesmn 0:3ac96e360672 2149 status = VL53L1_dynamic_xtalk_correction_enable(Dev);
charlesmn 0:3ac96e360672 2150
charlesmn 0:3ac96e360672 2151 #ifdef VL53L1_LOG_ENABLE
charlesmn 0:3ac96e360672 2152
charlesmn 0:3ac96e360672 2153
charlesmn 0:3ac96e360672 2154
charlesmn 0:3ac96e360672 2155 VL53L1_print_customer_nvm_managed(
charlesmn 0:3ac96e360672 2156 &(pdev->customer),
charlesmn 0:3ac96e360672 2157 "run_xtalk_extraction():pdev->lldata.customer.",
charlesmn 0:3ac96e360672 2158 VL53L1_TRACE_MODULE_XTALK_DATA);
charlesmn 0:3ac96e360672 2159
charlesmn 0:3ac96e360672 2160 VL53L1_print_xtalk_config(
charlesmn 0:3ac96e360672 2161 &(pdev->xtalk_cfg),
charlesmn 0:3ac96e360672 2162 "run_xtalk_extraction():pdev->lldata.xtalk_cfg.",
charlesmn 0:3ac96e360672 2163 VL53L1_TRACE_MODULE_XTALK_DATA);
charlesmn 0:3ac96e360672 2164
charlesmn 0:3ac96e360672 2165 VL53L1_print_xtalk_histogram_data(
charlesmn 0:3ac96e360672 2166 &(pdev->xtalk_shapes),
charlesmn 0:3ac96e360672 2167 "pdev->lldata.xtalk_shapes.",
charlesmn 0:3ac96e360672 2168 VL53L1_TRACE_MODULE_XTALK_DATA);
charlesmn 0:3ac96e360672 2169
charlesmn 0:3ac96e360672 2170 #endif
charlesmn 0:3ac96e360672 2171
charlesmn 0:3ac96e360672 2172 LOG_FUNCTION_END(status);
charlesmn 0:3ac96e360672 2173
charlesmn 0:3ac96e360672 2174 return status;
charlesmn 0:3ac96e360672 2175 }
charlesmn 0:3ac96e360672 2176