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:
charlesmn
Date:
Fri Nov 06 10:06:37 2020 +0000
Revision:
0:3ac96e360672
Child:
7:1add29d51e72
Library for ST Vl53L1A1 time of flight sensor.

Who changed what in which revision?

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