1 /*
2 * Copyright (c) 2017, STMicroelectronics - All Rights Reserved
3 *
4 * This file is part of VL53L1 Core and is dual licensed,
5 * either 'STMicroelectronics
6 * Proprietary license'
7 * or 'BSD 3-clause "New" or "Revised" License' , at your option.
8 *
9 ********************************************************************************
10 *
11 * 'STMicroelectronics Proprietary license'
12 *
13 ********************************************************************************
14 *
15 * License terms: STMicroelectronics Proprietary in accordance with licensing
16 * terms at www.st.com/sla0081
17 *
18 * STMicroelectronics confidential
19 * Reproduction and Communication of this document is strictly prohibited unless
20 * specifically authorized in writing by STMicroelectronics.
21 *
22 *
23 ********************************************************************************
24 *
25 * Alternatively, VL53L1 Core may be distributed under the terms of
26 * 'BSD 3-clause "New" or "Revised" License', in which case the following
27 * provisions apply instead of the ones mentioned above :
28 *
29 ********************************************************************************
30 *
31 * License terms: BSD 3-clause "New" or "Revised" License.
32 *
33 * Redistribution and use in source and binary forms, with or without
34 * modification, are permitted provided that the following conditions are met:
35 *
36 * 1. Redistributions of source code must retain the above copyright notice, this
37 * list of conditions and the following disclaimer.
38 *
39 * 2. Redistributions in binary form must reproduce the above copyright notice,
40 * this list of conditions and the following disclaimer in the documentation
41 * and/or other materials provided with the distribution.
42 *
43 * 3. Neither the name of the copyright holder nor the names of its contributors
44 * may be used to endorse or promote products derived from this software
45 * without specific prior written permission.
46 *
47 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
48 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
49 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
50 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
51 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
52 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
53 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
54 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
55 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
56 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
57 *
58 *
59 ********************************************************************************
60 *
61 */
62 
63 /**
64  * @file  vl53l1_api_core.c
65  *
66  * @brief EwokPlus25 low level API function definition
67  */
68 
69 
70 #include "vl53l1_ll_def.h"
71 #include "vl53l1_ll_device.h"
72 #include "vl53l1_platform.h"
73 #include "vl53l1_register_map.h"
74 #include "vl53l1_register_settings.h"
75 #include "vl53l1_register_funcs.h"
76 #include "vl53l1_nvm_map.h"
77 #include "vl53l1_core.h"
78 #include "vl53l1_wait.h"
79 #include "vl53l1_api_preset_modes.h"
80 #include "vl53l1_silicon_core.h"
81 #include "vl53l1_api_core.h"
82 #include "vl53l1_tuning_parm_defaults.h"
83 
84 #ifdef VL53L1_LOG_ENABLE
85 #include "vl53l1_api_debug.h"
86 #endif
87 
88 #define LOG_FUNCTION_START(fmt, ...) \
89 	_LOG_FUNCTION_START(VL53L1_TRACE_MODULE_CORE, fmt, ##__VA_ARGS__)
90 #define LOG_FUNCTION_END(status, ...) \
91 	_LOG_FUNCTION_END(VL53L1_TRACE_MODULE_CORE, status, ##__VA_ARGS__)
92 #define LOG_FUNCTION_END_FMT(status, fmt, ...) \
93 	_LOG_FUNCTION_END_FMT(VL53L1_TRACE_MODULE_CORE, status, \
94 	fmt, ##__VA_ARGS__)
95 
96 #define trace_print(level, ...) \
97 	_LOG_TRACE_PRINT(VL53L1_TRACE_MODULE_CORE, \
98 	level, VL53L1_TRACE_FUNCTION_NONE, ##__VA_ARGS__)
99 
100 #define VL53L1_MAX_I2C_XFER_SIZE 256
101 
102 #ifdef VL53L1_DEBUG
VL53L1_get_version(VL53L1_DEV Dev,VL53L1_ll_version_t * pdata)103 VL53L1_Error VL53L1_get_version(
104 	VL53L1_DEV           Dev,
105 	VL53L1_ll_version_t *pdata)
106 {
107 	/*
108 	 * respond with the #define values from version.h
109 	 * using memcpy(dst, src, size in bytes)
110 	 */
111 
112 	VL53L1_LLDriverData_t *pdev = VL53L1DevStructGetLLDriverHandle(Dev);
113 
114 	VL53L1_init_version(Dev);
115 
116 	memcpy(pdata, &(pdev->version), sizeof(VL53L1_ll_version_t));
117 
118 	return VL53L1_ERROR_NONE;
119 }
120 
VL53L1_get_device_firmware_version(VL53L1_DEV Dev,uint16_t * pfw_version)121 VL53L1_Error VL53L1_get_device_firmware_version(
122 	VL53L1_DEV        Dev,
123 	uint16_t         *pfw_version)
124 {
125 	/*
126 	 * Read Firmware version from device
127 	 */
128 
129 	VL53L1_Error  status = VL53L1_ERROR_NONE;
130 
131 	LOG_FUNCTION_START("");
132 
133 	if (status == VL53L1_ERROR_NONE) /*lint !e774 always true*/
134 		status = VL53L1_disable_firmware(Dev);
135 
136 	if (status == VL53L1_ERROR_NONE)
137 		status = VL53L1_RdWord(
138 				Dev,
139 				VL53L1_MCU_GENERAL_PURPOSE__GP_0,
140 				pfw_version);
141 
142 	if (status == VL53L1_ERROR_NONE)
143 		status = VL53L1_enable_firmware(Dev);
144 
145 	LOG_FUNCTION_END(status);
146 
147 	return status;
148 }
149 #endif
150 
151 
VL53L1_data_init(VL53L1_DEV Dev,uint8_t read_p2p_data)152 VL53L1_Error VL53L1_data_init(
153 	VL53L1_DEV        Dev,
154 	uint8_t           read_p2p_data)
155 {
156 	/*
157 	 * Initialise pdev data structure
158 	 */
159 
160 	VL53L1_Error status       = VL53L1_ERROR_NONE;
161 	VL53L1_LLDriverData_t    *pdev =
162 			VL53L1DevStructGetLLDriverHandle(Dev);
163 
164 	VL53L1_init_ll_driver_state(
165 			Dev,
166 			VL53L1_DEVICESTATE_UNKNOWN);
167 
168 	pdev->wait_method             = VL53L1_WAIT_METHOD_BLOCKING;
169 	pdev->preset_mode             = VL53L1_DEVICEPRESETMODE_STANDARD_RANGING;
170 	pdev->measurement_mode        = VL53L1_DEVICEMEASUREMENTMODE_STOP;
171 
172 	pdev->offset_calibration_mode =
173 		VL53L1_OFFSETCALIBRATIONMODE__MM1_MM2__STANDARD;
174 	pdev->offset_correction_mode  =
175 		VL53L1_OFFSETCORRECTIONMODE__MM1_MM2_OFFSETS;
176 
177 	pdev->phasecal_config_timeout_us  =  1000;
178 	pdev->mm_config_timeout_us        =  2000;
179 	pdev->range_config_timeout_us     = 13000;
180 	pdev->inter_measurement_period_ms =   100;
181 	pdev->dss_config__target_total_rate_mcps = 0x0A00;
182 	pdev->debug_mode                  =  0x00;
183 
184 	/* initialise gain calibration values to tuning parameter values */
185 
186 	pdev->gain_cal.standard_ranging_gain_factor =
187 			VL53L1_TUNINGPARM_LITE_RANGING_GAIN_FACTOR_DEFAULT;
188 
189 	/*
190 	 * Initialise version structure
191 	 */
192 	VL53L1_init_version(Dev);
193 
194 	/*
195 	 *  For C-API one time initialization only read device G02 registers
196 	 *  containing data copied from NVM
197 	 *
198 	 *  Contains the key NVM data e.g identification info fast oscillator
199 	 *  freq, max trim and laser safety info
200 	 */
201 
202 	if (read_p2p_data > 0 && status == VL53L1_ERROR_NONE) /*lint !e774 always true*/
203 			status = VL53L1_read_p2p_data(Dev);
204 
205 	/* Initialise Ref SPAD Char configuration structure */
206 #ifndef VL53L1_NOCALIB
207 	status =
208 		VL53L1_init_refspadchar_config_struct(
209 			&(pdev->refspadchar));
210 #endif
211 
212 	/* Initialise SPAD Self Check (SSC) configuration structure */
213 #ifndef VL53L1_NOCALIB
214 	status =
215 		VL53L1_init_ssc_config_struct(
216 			&(pdev->ssc_cfg));
217 #endif
218 
219 	/* Initialise Private Xtalk configuration structure
220 	 * - Fill with customer NVM data to begin
221 	 */
222 	status =
223 		VL53L1_init_xtalk_config_struct(
224 			&(pdev->customer),
225 			&(pdev->xtalk_cfg));
226 
227 	/* Initialise Offset Calibration configuration structure
228 	 */
229 #ifndef VL53L1_NOCALIB
230 	status =
231 		VL53L1_init_offset_cal_config_struct(
232 		    &(pdev->offsetcal_cfg));
233 #endif
234 
235 	/* Initialise Tuning Parameter structure
236 	 * - Added as part of Patch_AddingTuningParmStorage_11821
237 	 */
238 	status =
239 		VL53L1_init_tuning_parm_storage_struct(
240 			&(pdev->tuning_parms));
241 
242 	status = VL53L1_set_vhv_loopbound(Dev,
243 		VL53L1_TUNINGPARM_VHV_LOOPBOUND_DEFAULT);
244 
245 	/*
246 	 * Initialise default settings - much happen *after*
247 	 * reading /setting  of static_nvm_managed
248 	 */
249 
250 	if (status == VL53L1_ERROR_NONE)
251 		status = VL53L1_set_preset_mode(
252 						Dev,
253 						pdev->preset_mode,
254 						pdev->dss_config__target_total_rate_mcps,  /* 9.7 format 20Mcps */
255 						pdev->phasecal_config_timeout_us,
256 						pdev->mm_config_timeout_us,
257 						pdev->range_config_timeout_us,
258 						pdev->inter_measurement_period_ms);
259 
260 	/* Initial Low Power Auto Mode data structures */
261 	/* Added for Patch_LowPowerAutoMode */
262 	VL53L1_low_power_auto_data_init(
263 			Dev
264 			);
265 
266 #ifdef VL53L1_LOG_ENABLE
267 
268 	/* Prints out the initial calibration data for debug */
269 
270 	VL53L1_print_static_nvm_managed(
271 		&(pdev->stat_nvm),
272 		"data_init():pdev->lldata.stat_nvm.",
273 		VL53L1_TRACE_MODULE_DATA_INIT);
274 
275 	VL53L1_print_customer_nvm_managed(
276 		&(pdev->customer),
277 		"data_init():pdev->lldata.customer.",
278 		VL53L1_TRACE_MODULE_DATA_INIT);
279 
280 	VL53L1_print_nvm_copy_data(
281 		&(pdev->nvm_copy_data),
282 		"data_init():pdev->lldata.nvm_copy_data.",
283 		VL53L1_TRACE_MODULE_DATA_INIT);
284 
285 	VL53L1_print_additional_offset_cal_data(
286 		&(pdev->add_off_cal_data),
287 		"data_init():pdev->lldata.add_off_cal_data.",
288 		VL53L1_TRACE_MODULE_DATA_INIT);
289 
290 	VL53L1_print_user_zone(
291 		&(pdev->mm_roi),
292 		"data_init():pdev->lldata.mm_roi.",
293 		VL53L1_TRACE_MODULE_DATA_INIT);
294 
295 	VL53L1_print_optical_centre(
296 		&(pdev->optical_centre),
297 		"data_init():pdev->lldata.optical_centre.",
298 		VL53L1_TRACE_MODULE_DATA_INIT);
299 
300 	VL53L1_print_cal_peak_rate_map(
301 		&(pdev->cal_peak_rate_map),
302 		"data_init():pdev->lldata.cal_peak_rate_map.",
303 		VL53L1_TRACE_MODULE_DATA_INIT);
304 
305 #endif
306 
307 	LOG_FUNCTION_END(status);
308 
309 	return status;
310 }
311 
312 
VL53L1_read_p2p_data(VL53L1_DEV Dev)313 VL53L1_Error VL53L1_read_p2p_data(
314 	VL53L1_DEV        Dev)
315 {
316 
317 	/*
318 	 *  For C-API one time initialization only reads device
319 	 *  G02 registers containing data copied from NVM
320 	 *
321 	 *  Contains the key NVM data e.g identification info
322 	 *  fast oscillator freq, max trim and laser safety info
323 	 */
324 
325 	VL53L1_Error status       = VL53L1_ERROR_NONE;
326 	VL53L1_LLDriverData_t *pdev = VL53L1DevStructGetLLDriverHandle(Dev);
327 
328 	LOG_FUNCTION_START("");
329 
330 	if (status == VL53L1_ERROR_NONE)
331 		status = VL53L1_get_static_nvm_managed(
332 						Dev,
333 						&(pdev->stat_nvm));
334 
335 	if (status == VL53L1_ERROR_NONE)
336 		status = VL53L1_get_customer_nvm_managed(
337 						Dev,
338 						&(pdev->customer));
339 
340 	if (status == VL53L1_ERROR_NONE) {
341 
342 		status = VL53L1_get_nvm_copy_data(
343 						Dev,
344 						&(pdev->nvm_copy_data));
345 
346 		/* copy Return Good SPADs to buffer */
347 		if (status == VL53L1_ERROR_NONE)
348 			VL53L1_copy_rtn_good_spads_to_buffer(
349 					&(pdev->nvm_copy_data),
350 					&(pdev->rtn_good_spads[0]));
351 	}
352 
353 	/*
354 	 * read slow osc calibration value
355 	 * counts per ms
356 	 */
357 	if (status == VL53L1_ERROR_NONE)
358 		status =
359 			VL53L1_RdWord(
360 				Dev,
361 				VL53L1_RESULT__OSC_CALIBRATE_VAL,
362 				&(pdev->dbg_results.result__osc_calibrate_val));
363 
364 	/*
365 	 * Check if there a sensible value for osc_measured__fast_osc__frequency
366 	 */
367 
368 	if (pdev->stat_nvm.osc_measured__fast_osc__frequency < 0x1000) {
369 		trace_print(
370 			VL53L1_TRACE_LEVEL_WARNING,
371 			"\nInvalid %s value (0x%04X) - forcing to 0x%04X\n\n",
372 			"pdev->stat_nvm.osc_measured__fast_osc__frequency",
373 			pdev->stat_nvm.osc_measured__fast_osc__frequency,
374 			0xBCCC);
375 		pdev->stat_nvm.osc_measured__fast_osc__frequency = 0xBCCC;
376 	}
377 
378 	/*
379 	 * Get MM ROI - contains optical centre as SPAD number
380 	 */
381 
382 	if (status == VL53L1_ERROR_NONE)
383 		status =
384 			VL53L1_get_mode_mitigation_roi(
385 				Dev,
386 				&(pdev->mm_roi));
387 
388 	/* catch parts where the optical centre is
389 	 * no programmed in to the NVM
390 	 */
391 
392 	if (pdev->optical_centre.x_centre == 0 &&
393 		pdev->optical_centre.y_centre == 0) {
394 		pdev->optical_centre.x_centre =
395 				pdev->mm_roi.x_centre << 4;
396 		pdev->optical_centre.y_centre =
397 				pdev->mm_roi.y_centre << 4;
398 	}
399 
400 	LOG_FUNCTION_END(status);
401 
402 	return status;
403 }
404 
405 
VL53L1_software_reset(VL53L1_DEV Dev)406 VL53L1_Error VL53L1_software_reset(
407 	VL53L1_DEV    Dev)
408 {
409 	/**
410 	 * Sets and clears the software reset register VL53L1_SOFT_RESET.
411 	 * and waits for the firmware to boot
412 	 */
413 
414 	VL53L1_Error status       = VL53L1_ERROR_NONE;
415 
416 	LOG_FUNCTION_START("");
417 
418 	/* apply reset - note despite the name soft reset is active low! */
419 	if (status == VL53L1_ERROR_NONE) /*lint !e774 always true*/
420 		status = VL53L1_WrByte(
421 						Dev,
422 						VL53L1_SOFT_RESET,
423 						0x00);
424 
425 	/* wait for a while before releasing the reset */
426 	if (status == VL53L1_ERROR_NONE)
427 		status =
428 			VL53L1_WaitUs(
429 				Dev,
430 				VL53L1_SOFTWARE_RESET_DURATION_US);
431 
432 	/* release reset */
433 	if (status == VL53L1_ERROR_NONE)
434 		status = VL53L1_WrByte(
435 						Dev,
436 						VL53L1_SOFT_RESET,
437 						0x01);
438 
439 	/* wait for firmware boot to complete */
440 	if (status == VL53L1_ERROR_NONE)
441 		status = VL53L1_wait_for_boot_completion(Dev);
442 
443 	LOG_FUNCTION_END(status);
444 
445 	return status;
446 }
447 
448 
VL53L1_set_part_to_part_data(VL53L1_DEV Dev,VL53L1_calibration_data_t * pcal_data)449 VL53L1_Error VL53L1_set_part_to_part_data(
450 	VL53L1_DEV                            Dev,
451 	VL53L1_calibration_data_t            *pcal_data)
452 {
453 	/**
454 	 * Uses memcpy to copy input data to pdev->customer
455 	 */
456 
457 	VL53L1_Error  status = VL53L1_ERROR_NONE;
458 	VL53L1_LLDriverData_t *pdev = VL53L1DevStructGetLLDriverHandle(Dev);
459 
460 	uint32_t tempu32;
461 
462 	LOG_FUNCTION_START("");
463 
464 	if (pcal_data->struct_version !=
465 		VL53L1_LL_CALIBRATION_DATA_STRUCT_VERSION) {
466 		status = VL53L1_ERROR_INVALID_PARAMS;
467 	}
468 
469 	if (status == VL53L1_ERROR_NONE) {
470 
471 		/* memcpy(DEST, SRC, N)  */
472 		memcpy(
473 			&(pdev->customer),
474 			&(pcal_data->customer),
475 			sizeof(VL53L1_customer_nvm_managed_t));
476 
477 		/* memcpy(DEST, SRC, N)  */
478 		memcpy(
479 			&(pdev->add_off_cal_data),
480 			&(pcal_data->add_off_cal_data),
481 			sizeof(VL53L1_additional_offset_cal_data_t));
482 
483 		/* memcpy(DEST, SRC, N)  */
484 		memcpy(
485 			&(pdev->gain_cal),
486 			&(pcal_data->gain_cal),
487 			sizeof(VL53L1_gain_calibration_data_t));
488 
489 		/* memcpy(DEST, SRC, N)  */
490 		memcpy(
491 			&(pdev->cal_peak_rate_map),
492 			&(pcal_data->cal_peak_rate_map),
493 			sizeof(VL53L1_cal_peak_rate_map_t));
494 
495 		/*
496 		 *  Update internal xtalk data structures
497 		 */
498 
499 		pdev->xtalk_cfg.algo__crosstalk_compensation_plane_offset_kcps =
500 			pdev->customer.algo__crosstalk_compensation_plane_offset_kcps;
501 		pdev->xtalk_cfg.algo__crosstalk_compensation_x_plane_gradient_kcps =
502 			pdev->customer.algo__crosstalk_compensation_x_plane_gradient_kcps;
503 		pdev->xtalk_cfg.algo__crosstalk_compensation_y_plane_gradient_kcps =
504 			pdev->customer.algo__crosstalk_compensation_y_plane_gradient_kcps;
505 
506 		/* Assess and update customer packet xtalk parameters */
507 
508 		if (pdev->xtalk_cfg.global_crosstalk_compensation_enable == 0x00) {
509 			pdev->customer.algo__crosstalk_compensation_plane_offset_kcps =
510 				0x00;
511 			pdev->customer.algo__crosstalk_compensation_x_plane_gradient_kcps =
512 				0x00;
513 			pdev->customer.algo__crosstalk_compensation_y_plane_gradient_kcps =
514 				0x00;
515 		} else {
516 			tempu32 = VL53L1_calc_crosstalk_plane_offset_with_margin(
517 				pdev->xtalk_cfg.algo__crosstalk_compensation_plane_offset_kcps,
518 				pdev->xtalk_cfg.lite_mode_crosstalk_margin_kcps);
519 			if (tempu32 > 0xFFFF) {	/* clip to 16 bits */
520 				tempu32 = 0xFFFF;
521 			}
522 			pdev->customer.algo__crosstalk_compensation_plane_offset_kcps =
523 				(uint16_t)tempu32;
524 		}
525 	}
526 
527 	LOG_FUNCTION_END(status);
528 
529 	return status;
530 }
531 
532 
VL53L1_get_part_to_part_data(VL53L1_DEV Dev,VL53L1_calibration_data_t * pcal_data)533 VL53L1_Error VL53L1_get_part_to_part_data(
534 	VL53L1_DEV                      Dev,
535 	VL53L1_calibration_data_t      *pcal_data)
536 {
537 	/**
538 	 * Uses memcpy to copy pdev->customer to output data
539 	 */
540 
541 	VL53L1_Error  status = VL53L1_ERROR_NONE;
542 	VL53L1_LLDriverData_t *pdev = VL53L1DevStructGetLLDriverHandle(Dev);
543 
544 	LOG_FUNCTION_START("");
545 
546 	pcal_data->struct_version =
547 			VL53L1_LL_CALIBRATION_DATA_STRUCT_VERSION;
548 
549 	/* memcpy(DEST, SRC, N)  */
550 	memcpy(
551 		&(pcal_data->customer),
552 		&(pdev->customer),
553 		sizeof(VL53L1_customer_nvm_managed_t));
554 
555 	/* Overwrite Struct with xtalk config parameters */
556 	/* - customer struct versions are not golden copy */
557 
558 	if (pdev->xtalk_cfg.algo__crosstalk_compensation_plane_offset_kcps > 0xFFFF) {
559 		pcal_data->customer.algo__crosstalk_compensation_plane_offset_kcps =
560 			0xFFFF;
561 	} else {
562 		pcal_data->customer.algo__crosstalk_compensation_plane_offset_kcps =
563 			(uint16_t)pdev->xtalk_cfg.algo__crosstalk_compensation_plane_offset_kcps;
564 	}
565 	pcal_data->customer.algo__crosstalk_compensation_x_plane_gradient_kcps =
566 		pdev->xtalk_cfg.algo__crosstalk_compensation_x_plane_gradient_kcps;
567 	pcal_data->customer.algo__crosstalk_compensation_y_plane_gradient_kcps =
568 		pdev->xtalk_cfg.algo__crosstalk_compensation_y_plane_gradient_kcps;
569 
570 	/* memcpy(DEST, SRC, N)  */
571 	memcpy(
572 		&(pcal_data->add_off_cal_data),
573 		&(pdev->add_off_cal_data),
574 		sizeof(VL53L1_additional_offset_cal_data_t));
575 
576 	/* memcpy(DEST, SRC, N)  */
577 	memcpy(
578 		&(pcal_data->optical_centre),
579 		&(pdev->optical_centre),
580 		sizeof(VL53L1_optical_centre_t));
581 
582 	/* memcpy(DEST, SRC, N)  */
583 	memcpy(
584 		&(pcal_data->gain_cal),
585 		&(pdev->gain_cal),
586 		sizeof(VL53L1_gain_calibration_data_t));
587 
588 	/* memcpy(DEST, SRC, N)  */
589 	memcpy(
590 		&(pcal_data->cal_peak_rate_map),
591 		&(pdev->cal_peak_rate_map),
592 		sizeof(VL53L1_cal_peak_rate_map_t));
593 
594 	LOG_FUNCTION_END(status);
595 
596 	return status;
597 }
598 
599 
VL53L1_set_inter_measurement_period_ms(VL53L1_DEV Dev,uint32_t inter_measurement_period_ms)600 VL53L1_Error VL53L1_set_inter_measurement_period_ms(
601 	VL53L1_DEV              Dev,
602 	uint32_t                inter_measurement_period_ms)
603 {
604 	/**
605 	 * Convenience function for setting the inter measurement period
606 	 */
607 
608 	VL53L1_Error  status = VL53L1_ERROR_NONE;
609 	VL53L1_LLDriverData_t *pdev = VL53L1DevStructGetLLDriverHandle(Dev);
610 
611 	LOG_FUNCTION_START("");
612 
613 	if (pdev->dbg_results.result__osc_calibrate_val == 0)
614 		status = VL53L1_ERROR_DIVISION_BY_ZERO;
615 
616 	if (status == VL53L1_ERROR_NONE) {
617 		pdev->inter_measurement_period_ms = inter_measurement_period_ms;
618 		pdev->tim_cfg.system__intermeasurement_period = \
619 			inter_measurement_period_ms *
620 			(uint32_t)pdev->dbg_results.result__osc_calibrate_val;
621 	}
622 
623 	LOG_FUNCTION_END(status);
624 
625 	return status;
626 }
627 
628 
VL53L1_get_inter_measurement_period_ms(VL53L1_DEV Dev,uint32_t * pinter_measurement_period_ms)629 VL53L1_Error VL53L1_get_inter_measurement_period_ms(
630 	VL53L1_DEV              Dev,
631 	uint32_t               *pinter_measurement_period_ms)
632 {
633 	/**
634 	 * Convenience function for getting the inter measurement period
635 	 */
636 
637 	VL53L1_Error  status = VL53L1_ERROR_NONE;
638 	VL53L1_LLDriverData_t *pdev = VL53L1DevStructGetLLDriverHandle(Dev);
639 
640 	LOG_FUNCTION_START("");
641 
642 	if (pdev->dbg_results.result__osc_calibrate_val == 0)
643 		status = VL53L1_ERROR_DIVISION_BY_ZERO;
644 
645 	if (status == VL53L1_ERROR_NONE)
646 		*pinter_measurement_period_ms = \
647 			pdev->tim_cfg.system__intermeasurement_period /
648 			(uint32_t)pdev->dbg_results.result__osc_calibrate_val;
649 
650 
651 	LOG_FUNCTION_END(status);
652 
653 	return status;
654 }
655 
656 
VL53L1_set_timeouts_us(VL53L1_DEV Dev,uint32_t phasecal_config_timeout_us,uint32_t mm_config_timeout_us,uint32_t range_config_timeout_us)657 VL53L1_Error VL53L1_set_timeouts_us(
658 	VL53L1_DEV          Dev,
659 	uint32_t            phasecal_config_timeout_us,
660 	uint32_t            mm_config_timeout_us,
661 	uint32_t            range_config_timeout_us)
662 {
663 	/**
664 	 * Convenience function for setting the MM and range
665 	 * timeouts
666 	 */
667 
668 	VL53L1_Error  status = VL53L1_ERROR_NONE;
669 	VL53L1_LLDriverData_t *pdev =
670 			VL53L1DevStructGetLLDriverHandle(Dev);
671 
672 	LOG_FUNCTION_START("");
673 
674 	if (pdev->stat_nvm.osc_measured__fast_osc__frequency == 0)
675 		status = VL53L1_ERROR_DIVISION_BY_ZERO;
676 
677 	if (status == VL53L1_ERROR_NONE) {
678 
679 		pdev->phasecal_config_timeout_us = phasecal_config_timeout_us;
680 		pdev->mm_config_timeout_us       = mm_config_timeout_us;
681 		pdev->range_config_timeout_us    = range_config_timeout_us;
682 
683 		status =
684 			VL53L1_calc_timeout_register_values(
685 				phasecal_config_timeout_us,
686 				mm_config_timeout_us,
687 				range_config_timeout_us,
688 				pdev->stat_nvm.osc_measured__fast_osc__frequency,
689 				&(pdev->gen_cfg),
690 				&(pdev->tim_cfg));
691 	}
692 
693 	LOG_FUNCTION_END(status);
694 
695 	return status;
696 }
697 
698 
VL53L1_get_timeouts_us(VL53L1_DEV Dev,uint32_t * pphasecal_config_timeout_us,uint32_t * pmm_config_timeout_us,uint32_t * prange_config_timeout_us)699 VL53L1_Error VL53L1_get_timeouts_us(
700 	VL53L1_DEV           Dev,
701 	uint32_t            *pphasecal_config_timeout_us,
702 	uint32_t            *pmm_config_timeout_us,
703 	uint32_t			*prange_config_timeout_us)
704 {
705 	/**
706 	 * Convenience function for getting the MM and range
707 	 * timeouts
708 	 */
709 
710 	VL53L1_Error  status = VL53L1_ERROR_NONE;
711 	VL53L1_LLDriverData_t *pdev =
712 			VL53L1DevStructGetLLDriverHandle(Dev);
713 
714 	uint32_t  macro_period_us = 0;
715 	uint16_t  timeout_encoded = 0;
716 
717 	LOG_FUNCTION_START("");
718 
719 	if (pdev->stat_nvm.osc_measured__fast_osc__frequency == 0)
720 		status = VL53L1_ERROR_DIVISION_BY_ZERO;
721 
722 	if (status == VL53L1_ERROR_NONE) {
723 
724 		/* Update Macro Period for Range A VCSEL Period */
725 		macro_period_us =
726 			VL53L1_calc_macro_period_us(
727 				pdev->stat_nvm.osc_measured__fast_osc__frequency,
728 				pdev->tim_cfg.range_config__vcsel_period_a);
729 
730 		/*  Get Phase Cal Timing A timeout */
731 
732 		*pphasecal_config_timeout_us =
733 			VL53L1_calc_timeout_us(
734 				(uint32_t)pdev->gen_cfg.phasecal_config__timeout_macrop,
735 				macro_period_us);
736 
737 		/*  Get MM Timing A timeout */
738 
739 		timeout_encoded =
740 			(uint16_t)pdev->tim_cfg.mm_config__timeout_macrop_a_hi;
741 		timeout_encoded = (timeout_encoded << 8) +
742 			(uint16_t)pdev->tim_cfg.mm_config__timeout_macrop_a_lo;
743 
744 		*pmm_config_timeout_us =
745 			VL53L1_calc_decoded_timeout_us(
746 				timeout_encoded,
747 				macro_period_us);
748 
749 		/* Get Range Timing A timeout */
750 
751 		timeout_encoded =
752 			(uint16_t)pdev->tim_cfg.range_config__timeout_macrop_a_hi;
753 		timeout_encoded = (timeout_encoded << 8) +
754 			(uint16_t)pdev->tim_cfg.range_config__timeout_macrop_a_lo;
755 
756 		*prange_config_timeout_us =
757 			VL53L1_calc_decoded_timeout_us(
758 				timeout_encoded,
759 				macro_period_us);
760 
761 		pdev->phasecal_config_timeout_us = *pphasecal_config_timeout_us;
762 		pdev->mm_config_timeout_us       = *pmm_config_timeout_us;
763 		pdev->range_config_timeout_us    = *prange_config_timeout_us;
764 
765 	}
766 
767 	LOG_FUNCTION_END(status);
768 
769 	return status;
770 }
771 
772 
VL53L1_set_calibration_repeat_period(VL53L1_DEV Dev,uint16_t cal_config__repeat_period)773 VL53L1_Error VL53L1_set_calibration_repeat_period(
774 	VL53L1_DEV          Dev,
775 	uint16_t            cal_config__repeat_period)
776 {
777 	/**
778 	 * Convenience function for setting calibration repeat period
779 	 */
780 
781 	VL53L1_Error  status = VL53L1_ERROR_NONE;
782 	VL53L1_LLDriverData_t *pdev =
783 		VL53L1DevStructGetLLDriverHandle(Dev);
784 
785 	pdev->gen_cfg.cal_config__repeat_rate = cal_config__repeat_period;
786 
787 	return status;
788 
789 }
790 
791 
VL53L1_get_calibration_repeat_period(VL53L1_DEV Dev,uint16_t * pcal_config__repeat_period)792 VL53L1_Error VL53L1_get_calibration_repeat_period(
793 	VL53L1_DEV          Dev,
794 	uint16_t           *pcal_config__repeat_period)
795 {
796 	/**
797 	 * Convenience function for getting calibration repeat period
798 	 */
799 
800 	VL53L1_Error  status = VL53L1_ERROR_NONE;
801 	VL53L1_LLDriverData_t *pdev =
802 		VL53L1DevStructGetLLDriverHandle(Dev);
803 
804 	*pcal_config__repeat_period = pdev->gen_cfg.cal_config__repeat_rate;
805 
806 	return status;
807 
808 }
809 
810 
VL53L1_set_sequence_config_bit(VL53L1_DEV Dev,VL53L1_DeviceSequenceConfig bit_id,uint8_t value)811 VL53L1_Error VL53L1_set_sequence_config_bit(
812 	VL53L1_DEV                    Dev,
813 	VL53L1_DeviceSequenceConfig   bit_id,
814 	uint8_t                       value)
815 {
816 	/**
817 	 * Convenience function for setting sequence
818 	 * config enable bits
819 	 */
820 
821 	VL53L1_Error  status = VL53L1_ERROR_NONE;
822 	VL53L1_LLDriverData_t *pdev =
823 		VL53L1DevStructGetLLDriverHandle(Dev);
824 
825 	uint8_t  bit_mask        = 0x01;
826 	uint8_t  clr_mask        = 0xFF  - bit_mask;
827 	uint8_t  bit_value       = value & bit_mask;
828 
829 	if (bit_id <= VL53L1_DEVICESEQUENCECONFIG_RANGE) {
830 
831 		if (bit_id > 0) {
832 			bit_mask  = 0x01 << bit_id;
833 			bit_value = bit_value << bit_id;
834 			clr_mask  = 0xFF  - bit_mask;
835 		}
836 
837 		pdev->dyn_cfg.system__sequence_config = \
838 			(pdev->dyn_cfg.system__sequence_config & clr_mask) | \
839 			bit_value;
840 
841 	} else {
842 		status = VL53L1_ERROR_INVALID_PARAMS;
843 	}
844 
845 	return status;
846 
847 }
848 
849 
VL53L1_get_sequence_config_bit(VL53L1_DEV Dev,VL53L1_DeviceSequenceConfig bit_id,uint8_t * pvalue)850 VL53L1_Error VL53L1_get_sequence_config_bit(
851 	VL53L1_DEV                    Dev,
852 	VL53L1_DeviceSequenceConfig   bit_id,
853 	uint8_t                      *pvalue)
854 {
855 	/**
856 	 * Convenience function for getting sequence
857 	 * config enable bits
858 	 */
859 
860 	VL53L1_Error  status = VL53L1_ERROR_NONE;
861 	VL53L1_LLDriverData_t *pdev =
862 		VL53L1DevStructGetLLDriverHandle(Dev);
863 
864 	uint8_t  bit_mask        = 0x01;
865 
866 	if (bit_id <= VL53L1_DEVICESEQUENCECONFIG_RANGE) {
867 
868 		if (bit_id > 0) {
869 			bit_mask  = 0x01 << bit_id;
870 		}
871 
872 		*pvalue =
873 			pdev->dyn_cfg.system__sequence_config & bit_mask;
874 
875 		if (bit_id > 0) {
876 			*pvalue  = *pvalue >> bit_id;
877 		}
878 
879 	} else {
880 		status = VL53L1_ERROR_INVALID_PARAMS;
881 	}
882 
883 	return status;
884 }
885 
886 
VL53L1_set_interrupt_polarity(VL53L1_DEV Dev,VL53L1_DeviceInterruptPolarity interrupt_polarity)887 VL53L1_Error VL53L1_set_interrupt_polarity(
888 	VL53L1_DEV                      Dev,
889 	VL53L1_DeviceInterruptPolarity  interrupt_polarity)
890 {
891 	/**
892 	 * Convenience function for setting interrupt polarity
893 	 */
894 
895 	VL53L1_Error  status = VL53L1_ERROR_NONE;
896 	VL53L1_LLDriverData_t *pdev =
897 		VL53L1DevStructGetLLDriverHandle(Dev);
898 
899 	pdev->stat_cfg.gpio_hv_mux__ctrl = \
900 			(pdev->stat_cfg.gpio_hv_mux__ctrl & \
901 			 VL53L1_DEVICEINTERRUPTPOLARITY_CLEAR_MASK) | \
902 			(interrupt_polarity & \
903 			 VL53L1_DEVICEINTERRUPTPOLARITY_BIT_MASK);
904 
905 	return status;
906 
907 }
908 
909 
910 #ifndef VL53L1_NOCALIB
VL53L1_set_refspadchar_config_struct(VL53L1_DEV Dev,VL53L1_refspadchar_config_t * pdata)911 VL53L1_Error VL53L1_set_refspadchar_config_struct(
912 	VL53L1_DEV                     Dev,
913 	VL53L1_refspadchar_config_t   *pdata)
914 {
915 	/*
916 	 * Allows user config of Ref SPAD Char data structure
917 	 */
918 
919 	VL53L1_Error  status = VL53L1_ERROR_NONE;
920 	VL53L1_LLDriverData_t *pdev =
921 		VL53L1DevStructGetLLDriverHandle(Dev);
922 
923 	LOG_FUNCTION_START("");
924 
925 	pdev->refspadchar.device_test_mode = pdata->device_test_mode;
926 	pdev->refspadchar.vcsel_period     = pdata->vcsel_period;
927 	pdev->refspadchar.timeout_us       = pdata->timeout_us;
928 	pdev->refspadchar.target_count_rate_mcps    =
929 			pdata->target_count_rate_mcps;
930 	pdev->refspadchar.min_count_rate_limit_mcps =
931 			pdata->min_count_rate_limit_mcps;
932 	pdev->refspadchar.max_count_rate_limit_mcps =
933 			pdata->max_count_rate_limit_mcps;
934 
935 	LOG_FUNCTION_END(status);
936 
937 	return status;
938 }
939 #endif
940 
941 #ifndef VL53L1_NOCALIB
VL53L1_get_refspadchar_config_struct(VL53L1_DEV Dev,VL53L1_refspadchar_config_t * pdata)942 VL53L1_Error VL53L1_get_refspadchar_config_struct(
943 	VL53L1_DEV                     Dev,
944 	VL53L1_refspadchar_config_t   *pdata)
945 {
946 	/*
947 	 * Allows user config of Ref SPAD Char data structure
948 	 */
949 
950 	VL53L1_Error  status = VL53L1_ERROR_NONE;
951 	VL53L1_LLDriverData_t *pdev =
952 		VL53L1DevStructGetLLDriverHandle(Dev);
953 
954 	LOG_FUNCTION_START("");
955 
956 	pdata->device_test_mode       = pdev->refspadchar.device_test_mode;
957 	pdata->vcsel_period           = pdev->refspadchar.vcsel_period;
958 	pdata->timeout_us             = pdev->refspadchar.timeout_us;
959 	pdata->target_count_rate_mcps = pdev->refspadchar.target_count_rate_mcps;
960 	pdata->min_count_rate_limit_mcps =
961 			pdev->refspadchar.min_count_rate_limit_mcps;
962 	pdata->max_count_rate_limit_mcps =
963 			pdev->refspadchar.max_count_rate_limit_mcps;
964 
965 	LOG_FUNCTION_END(status);
966 
967 	return status;
968 }
969 #endif
970 
971 
VL53L1_set_range_ignore_threshold(VL53L1_DEV Dev,uint8_t range_ignore_thresh_mult,uint16_t range_ignore_threshold_mcps)972 VL53L1_Error VL53L1_set_range_ignore_threshold(
973 	VL53L1_DEV              Dev,
974 	uint8_t                 range_ignore_thresh_mult,
975 	uint16_t                range_ignore_threshold_mcps)
976 {
977 	/**
978 	 * Convenience function for setting Range Ignore Threshold
979 	 */
980 
981 	VL53L1_Error  status = VL53L1_ERROR_NONE;
982 	VL53L1_LLDriverData_t *pdev =
983 		VL53L1DevStructGetLLDriverHandle(Dev);
984 
985 	pdev->xtalk_cfg.crosstalk_range_ignore_threshold_rate_mcps =
986 		range_ignore_threshold_mcps;
987 
988 	pdev->xtalk_cfg.crosstalk_range_ignore_threshold_mult =
989 		range_ignore_thresh_mult;
990 
991 	return status;
992 
993 }
994 
VL53L1_get_range_ignore_threshold(VL53L1_DEV Dev,uint8_t * prange_ignore_thresh_mult,uint16_t * prange_ignore_threshold_mcps_internal,uint16_t * prange_ignore_threshold_mcps_current)995 VL53L1_Error VL53L1_get_range_ignore_threshold(
996 	VL53L1_DEV              Dev,
997 	uint8_t                *prange_ignore_thresh_mult,
998 	uint16_t               *prange_ignore_threshold_mcps_internal,
999 	uint16_t               *prange_ignore_threshold_mcps_current)
1000 {
1001 	/**
1002 	 * Convenience function for retrieving Range Ignore Threshold
1003 	 * - Returns both the calculated internal value
1004 	 * - and the value currently applied to device reg settings
1005 	 *
1006 	 * Values both in fixed point 3.13 Mcps per spad
1007 	 *
1008 	 */
1009 
1010 	VL53L1_Error  status = VL53L1_ERROR_NONE;
1011 	VL53L1_LLDriverData_t *pdev =
1012 		VL53L1DevStructGetLLDriverHandle(Dev);
1013 
1014 	*prange_ignore_thresh_mult =
1015 	    pdev->xtalk_cfg.crosstalk_range_ignore_threshold_mult;
1016 
1017 	*prange_ignore_threshold_mcps_current =
1018 		pdev->stat_cfg.algo__range_ignore_threshold_mcps;
1019 
1020 	*prange_ignore_threshold_mcps_internal =
1021 		pdev->xtalk_cfg.crosstalk_range_ignore_threshold_rate_mcps;
1022 
1023 	return status;
1024 
1025 }
1026 
1027 
1028 
VL53L1_get_interrupt_polarity(VL53L1_DEV Dev,VL53L1_DeviceInterruptPolarity * pinterrupt_polarity)1029 VL53L1_Error VL53L1_get_interrupt_polarity(
1030 	VL53L1_DEV                       Dev,
1031 	VL53L1_DeviceInterruptPolarity  *pinterrupt_polarity)
1032 {
1033 	/**
1034 	 * Convenience function for getting interrupt polarity
1035 	 */
1036 
1037 	VL53L1_Error  status = VL53L1_ERROR_NONE;
1038 	VL53L1_LLDriverData_t *pdev =
1039 		VL53L1DevStructGetLLDriverHandle(Dev);
1040 
1041 	*pinterrupt_polarity = \
1042 		pdev->stat_cfg.gpio_hv_mux__ctrl & \
1043 		VL53L1_DEVICEINTERRUPTPOLARITY_BIT_MASK ;
1044 
1045 	return status;
1046 
1047 }
1048 
1049 
VL53L1_set_user_zone(VL53L1_DEV Dev,VL53L1_user_zone_t * puser_zone)1050 VL53L1_Error VL53L1_set_user_zone(
1051 	VL53L1_DEV              Dev,
1052 	VL53L1_user_zone_t     *puser_zone)
1053 {
1054 	/**
1055 	 * Convenience function for setting the user ROI
1056 	 */
1057 
1058 	VL53L1_Error  status = VL53L1_ERROR_NONE;
1059 	VL53L1_LLDriverData_t *pdev = VL53L1DevStructGetLLDriverHandle(Dev);
1060 
1061 	LOG_FUNCTION_START("");
1062 
1063 	/* convert (row,col) location into a SPAD number */
1064 	VL53L1_encode_row_col(
1065 		puser_zone->y_centre,
1066 		puser_zone->x_centre,
1067 		&(pdev->dyn_cfg.roi_config__user_roi_centre_spad));
1068 
1069 	/* merge x and y sizes */
1070 	VL53L1_encode_zone_size(
1071 		puser_zone->width,
1072 		puser_zone->height,
1073 		&(pdev->dyn_cfg.roi_config__user_roi_requested_global_xy_size));
1074 
1075 	/* need to add checks to ensure ROI is within array */
1076 
1077 	LOG_FUNCTION_END(status);
1078 
1079 	return status;
1080 }
1081 
1082 
VL53L1_get_user_zone(VL53L1_DEV Dev,VL53L1_user_zone_t * puser_zone)1083 VL53L1_Error VL53L1_get_user_zone(
1084 	VL53L1_DEV              Dev,
1085 	VL53L1_user_zone_t     *puser_zone)
1086 {
1087 	/**
1088 	 * Convenience function for getting the user ROI
1089 	 */
1090 
1091 	VL53L1_Error  status = VL53L1_ERROR_NONE;
1092 	VL53L1_LLDriverData_t *pdev = VL53L1DevStructGetLLDriverHandle(Dev);
1093 
1094 	LOG_FUNCTION_START("");
1095 
1096 	/* convert SPAD number into (row,col) location*/
1097 	VL53L1_decode_row_col(
1098 			pdev->dyn_cfg.roi_config__user_roi_centre_spad,
1099 			&(puser_zone->y_centre),
1100 			&(puser_zone->x_centre));
1101 
1102 	/* extract x and y sizes */
1103 	VL53L1_decode_zone_size(
1104 		pdev->dyn_cfg.roi_config__user_roi_requested_global_xy_size,
1105 		&(puser_zone->width),
1106 		&(puser_zone->height));
1107 
1108 	LOG_FUNCTION_END(status);
1109 
1110 	return status;
1111 }
1112 
1113 
1114 
VL53L1_get_mode_mitigation_roi(VL53L1_DEV Dev,VL53L1_user_zone_t * pmm_roi)1115 VL53L1_Error VL53L1_get_mode_mitigation_roi(
1116 	VL53L1_DEV              Dev,
1117 	VL53L1_user_zone_t     *pmm_roi)
1118 {
1119 	/**
1120 	 * Convenience function for getting the mode mitigation ROI
1121 	 */
1122 
1123 	VL53L1_Error  status = VL53L1_ERROR_NONE;
1124 	VL53L1_LLDriverData_t *pdev = VL53L1DevStructGetLLDriverHandle(Dev);
1125 
1126 	uint8_t  x       = 0;
1127 	uint8_t  y       = 0;
1128 	uint8_t  xy_size = 0;
1129 
1130 	LOG_FUNCTION_START("");
1131 
1132 	/* convert SPAD number into (row,col) location */
1133 	VL53L1_decode_row_col(
1134 			pdev->nvm_copy_data.roi_config__mode_roi_centre_spad,
1135 			&y,
1136 			&x);
1137 
1138 	pmm_roi->x_centre = x;
1139 	pmm_roi->y_centre = y;
1140 
1141 	/* extract x and y sizes
1142 	 *
1143 	 * Important: the sense of the device width and height is swapped
1144 	 * versus the API sense
1145 	 *
1146 	 * MS Nibble = height
1147 	 * LS Nibble = width
1148 	 */
1149 	xy_size = pdev->nvm_copy_data.roi_config__mode_roi_xy_size;
1150 
1151 	pmm_roi->height = xy_size >> 4;
1152 	pmm_roi->width  = xy_size & 0x0F;
1153 
1154 	LOG_FUNCTION_END(status);
1155 
1156 	return status;
1157 }
1158 
VL53L1_get_preset_mode_timing_cfg(VL53L1_DEV Dev,VL53L1_DevicePresetModes device_preset_mode,uint16_t * pdss_config__target_total_rate_mcps,uint32_t * pphasecal_config_timeout_us,uint32_t * pmm_config_timeout_us,uint32_t * prange_config_timeout_us)1159 VL53L1_Error VL53L1_get_preset_mode_timing_cfg(
1160 	VL53L1_DEV                   Dev,
1161 	VL53L1_DevicePresetModes     device_preset_mode,
1162 	uint16_t                    *pdss_config__target_total_rate_mcps,
1163 	uint32_t                    *pphasecal_config_timeout_us,
1164 	uint32_t                    *pmm_config_timeout_us,
1165 	uint32_t                    *prange_config_timeout_us)
1166 {
1167 	VL53L1_Error  status = VL53L1_ERROR_NONE;
1168 	VL53L1_LLDriverData_t *pdev = VL53L1DevStructGetLLDriverHandle(Dev);
1169 
1170 	LOG_FUNCTION_START("");
1171 
1172 
1173 	switch (device_preset_mode) {
1174 
1175 	case VL53L1_DEVICEPRESETMODE_STANDARD_RANGING:
1176 	case VL53L1_DEVICEPRESETMODE_STANDARD_RANGING_SHORT_RANGE:
1177 	case VL53L1_DEVICEPRESETMODE_STANDARD_RANGING_LONG_RANGE:
1178 	case VL53L1_DEVICEPRESETMODE_STANDARD_RANGING_MM1_CAL:
1179 	case VL53L1_DEVICEPRESETMODE_STANDARD_RANGING_MM2_CAL:
1180 	case VL53L1_DEVICEPRESETMODE_OLT:
1181 		*pdss_config__target_total_rate_mcps =
1182 				pdev->tuning_parms.tp_dss_target_lite_mcps;
1183 		*pphasecal_config_timeout_us =
1184 				pdev->tuning_parms.tp_phasecal_timeout_lite_us;
1185 		*pmm_config_timeout_us =
1186 				pdev->tuning_parms.tp_mm_timeout_lite_us;
1187 		*prange_config_timeout_us =
1188 				pdev->tuning_parms.tp_range_timeout_lite_us;
1189 	break;
1190 
1191 	case VL53L1_DEVICEPRESETMODE_TIMED_RANGING:
1192 	case VL53L1_DEVICEPRESETMODE_TIMED_RANGING_SHORT_RANGE:
1193 	case VL53L1_DEVICEPRESETMODE_TIMED_RANGING_LONG_RANGE:
1194 	case VL53L1_DEVICEPRESETMODE_SINGLESHOT_RANGING:
1195 		*pdss_config__target_total_rate_mcps =
1196 				pdev->tuning_parms.tp_dss_target_timed_mcps;
1197 		*pphasecal_config_timeout_us =
1198 				pdev->tuning_parms.tp_phasecal_timeout_timed_us;
1199 		*pmm_config_timeout_us =
1200 				pdev->tuning_parms.tp_mm_timeout_timed_us;
1201 		*prange_config_timeout_us =
1202 				pdev->tuning_parms.tp_range_timeout_timed_us;
1203 	break;
1204 
1205 	case VL53L1_DEVICEPRESETMODE_LOWPOWERAUTO_SHORT_RANGE:
1206 	case VL53L1_DEVICEPRESETMODE_LOWPOWERAUTO_MEDIUM_RANGE:
1207 	case VL53L1_DEVICEPRESETMODE_LOWPOWERAUTO_LONG_RANGE:
1208 		*pdss_config__target_total_rate_mcps =
1209 				pdev->tuning_parms.tp_dss_target_timed_mcps;
1210 		*pphasecal_config_timeout_us =
1211 				pdev->tuning_parms.tp_phasecal_timeout_timed_us;
1212 		*pmm_config_timeout_us =
1213 				pdev->tuning_parms.tp_mm_timeout_lpa_us;
1214 		*prange_config_timeout_us =
1215 				pdev->tuning_parms.tp_range_timeout_lpa_us;
1216 	break;
1217 
1218 	default:
1219 		status = VL53L1_ERROR_INVALID_PARAMS;
1220 		break;
1221 
1222 	}
1223 
1224 	LOG_FUNCTION_END(status);
1225 
1226 	return status;
1227 }
1228 
1229 
VL53L1_set_preset_mode(VL53L1_DEV Dev,VL53L1_DevicePresetModes device_preset_mode,uint16_t dss_config__target_total_rate_mcps,uint32_t phasecal_config_timeout_us,uint32_t mm_config_timeout_us,uint32_t range_config_timeout_us,uint32_t inter_measurement_period_ms)1230 VL53L1_Error VL53L1_set_preset_mode(
1231 	VL53L1_DEV                   Dev,
1232 	VL53L1_DevicePresetModes     device_preset_mode,
1233 	uint16_t                     dss_config__target_total_rate_mcps,
1234 	uint32_t                     phasecal_config_timeout_us,
1235 	uint32_t                     mm_config_timeout_us,
1236 	uint32_t                     range_config_timeout_us,
1237 	uint32_t                     inter_measurement_period_ms)
1238 {
1239 	/**
1240 	 * Initializes static and dynamic data structures for
1241 	 * the provided preset mode
1242 	 */
1243 
1244 	VL53L1_Error  status = VL53L1_ERROR_NONE;
1245 	VL53L1_LLDriverData_t *pdev =
1246 			VL53L1DevStructGetLLDriverHandle(Dev);
1247 
1248 	VL53L1_static_config_t        *pstatic       = &(pdev->stat_cfg);
1249 	VL53L1_general_config_t       *pgeneral      = &(pdev->gen_cfg);
1250 	VL53L1_timing_config_t        *ptiming       = &(pdev->tim_cfg);
1251 	VL53L1_dynamic_config_t       *pdynamic      = &(pdev->dyn_cfg);
1252 	VL53L1_system_control_t       *psystem       = &(pdev->sys_ctrl);
1253 	VL53L1_tuning_parm_storage_t  *ptuning_parms = &(pdev->tuning_parms);
1254 	VL53L1_low_power_auto_data_t  *plpadata      =
1255 					&(pdev->low_power_auto_data);
1256 
1257 	LOG_FUNCTION_START("");
1258 
1259 	/* save input settings */
1260 	pdev->preset_mode                 = device_preset_mode;
1261 	pdev->mm_config_timeout_us        = mm_config_timeout_us;
1262 	pdev->range_config_timeout_us     = range_config_timeout_us;
1263 	pdev->inter_measurement_period_ms = inter_measurement_period_ms;
1264 
1265 	/* Reset LL Driver state variables */
1266 
1267 	VL53L1_init_ll_driver_state(
1268 			Dev,
1269 			VL53L1_DEVICESTATE_SW_STANDBY);
1270 
1271 	/* apply selected preset */
1272 
1273 	switch (device_preset_mode) {
1274 
1275 	case VL53L1_DEVICEPRESETMODE_STANDARD_RANGING:
1276 		status = VL53L1_preset_mode_standard_ranging(
1277 					pstatic,
1278 					pgeneral,
1279 					ptiming,
1280 					pdynamic,
1281 					psystem,
1282 					ptuning_parms);
1283 		break;
1284 
1285 	case VL53L1_DEVICEPRESETMODE_STANDARD_RANGING_SHORT_RANGE:
1286 		status = VL53L1_preset_mode_standard_ranging_short_range(
1287 					pstatic,
1288 					pgeneral,
1289 					ptiming,
1290 					pdynamic,
1291 					psystem,
1292 					ptuning_parms);
1293 		break;
1294 
1295 	case VL53L1_DEVICEPRESETMODE_STANDARD_RANGING_LONG_RANGE:
1296 		status = VL53L1_preset_mode_standard_ranging_long_range(
1297 					pstatic,
1298 					pgeneral,
1299 					ptiming,
1300 					pdynamic,
1301 					psystem,
1302 					ptuning_parms);
1303 		break;
1304 
1305 #ifndef VL53L1_NOCALIB
1306 	case VL53L1_DEVICEPRESETMODE_STANDARD_RANGING_MM1_CAL:
1307 		status = VL53L1_preset_mode_standard_ranging_mm1_cal(
1308 					pstatic,
1309 					pgeneral,
1310 					ptiming,
1311 					pdynamic,
1312 					psystem,
1313 					ptuning_parms);
1314 		break;
1315 
1316 	case VL53L1_DEVICEPRESETMODE_STANDARD_RANGING_MM2_CAL:
1317 		status = VL53L1_preset_mode_standard_ranging_mm2_cal(
1318 					pstatic,
1319 					pgeneral,
1320 					ptiming,
1321 					pdynamic,
1322 					psystem,
1323 					ptuning_parms);
1324 		break;
1325 #endif
1326 
1327 	case VL53L1_DEVICEPRESETMODE_TIMED_RANGING:
1328 		status = VL53L1_preset_mode_timed_ranging(
1329 					pstatic,
1330 					pgeneral,
1331 					ptiming,
1332 					pdynamic,
1333 					psystem,
1334 					ptuning_parms);
1335 		break;
1336 
1337 	case VL53L1_DEVICEPRESETMODE_TIMED_RANGING_SHORT_RANGE:
1338 		status = VL53L1_preset_mode_timed_ranging_short_range(
1339 					pstatic,
1340 					pgeneral,
1341 					ptiming,
1342 					pdynamic,
1343 					psystem,
1344 					ptuning_parms);
1345 		break;
1346 
1347 	case VL53L1_DEVICEPRESETMODE_TIMED_RANGING_LONG_RANGE:
1348 		status = VL53L1_preset_mode_timed_ranging_long_range(
1349 					pstatic,
1350 					pgeneral,
1351 					ptiming,
1352 					pdynamic,
1353 					psystem,
1354 					ptuning_parms);
1355 		break;
1356 
1357 	case VL53L1_DEVICEPRESETMODE_OLT:
1358 		status = VL53L1_preset_mode_olt(
1359 					pstatic,
1360 					pgeneral,
1361 					ptiming,
1362 					pdynamic,
1363 					psystem,
1364 					ptuning_parms);
1365 		break;
1366 
1367 	case VL53L1_DEVICEPRESETMODE_SINGLESHOT_RANGING:
1368 		status = VL53L1_preset_mode_singleshot_ranging(
1369 					pstatic,
1370 					pgeneral,
1371 					ptiming,
1372 					pdynamic,
1373 					psystem,
1374 					ptuning_parms);
1375 		break;
1376 
1377 	case VL53L1_DEVICEPRESETMODE_LOWPOWERAUTO_SHORT_RANGE:
1378 		status = VL53L1_preset_mode_low_power_auto_short_ranging(
1379 					pstatic,
1380 					pgeneral,
1381 					ptiming,
1382 					pdynamic,
1383 					psystem,
1384 					ptuning_parms,
1385 					plpadata);
1386 		break;
1387 
1388 	case VL53L1_DEVICEPRESETMODE_LOWPOWERAUTO_MEDIUM_RANGE:
1389 		status = VL53L1_preset_mode_low_power_auto_ranging(
1390 					pstatic,
1391 					pgeneral,
1392 					ptiming,
1393 					pdynamic,
1394 					psystem,
1395 					ptuning_parms,
1396 					plpadata);
1397 		break;
1398 
1399 	case VL53L1_DEVICEPRESETMODE_LOWPOWERAUTO_LONG_RANGE:
1400 		status = VL53L1_preset_mode_low_power_auto_long_ranging(
1401 					pstatic,
1402 					pgeneral,
1403 					ptiming,
1404 					pdynamic,
1405 					psystem,
1406 					ptuning_parms,
1407 					plpadata);
1408 		break;
1409 
1410 	default:
1411 		status = VL53L1_ERROR_INVALID_PARAMS;
1412 		break;
1413 
1414 	}
1415 
1416 	/* update DSS target */
1417 
1418 	if (status == VL53L1_ERROR_NONE) {
1419 
1420 		pstatic->dss_config__target_total_rate_mcps =
1421 				dss_config__target_total_rate_mcps;
1422 		pdev->dss_config__target_total_rate_mcps    =
1423 				dss_config__target_total_rate_mcps;
1424 
1425 	}
1426 
1427 	/*
1428 	 * Update the register timeout values based on input
1429 	 * real time values and preset mode VCSEL periods
1430 	 */
1431 
1432 	if (status == VL53L1_ERROR_NONE)
1433 		status =
1434 			VL53L1_set_timeouts_us(
1435 				Dev,
1436 				phasecal_config_timeout_us,
1437 				mm_config_timeout_us,
1438 				range_config_timeout_us);
1439 
1440 	if (status == VL53L1_ERROR_NONE)
1441 		status =
1442 			VL53L1_set_inter_measurement_period_ms(
1443 				Dev,
1444 				inter_measurement_period_ms);
1445 
1446 	LOG_FUNCTION_END(status);
1447 
1448 	return status;
1449 }
1450 
1451 
VL53L1_enable_xtalk_compensation(VL53L1_DEV Dev)1452 VL53L1_Error  VL53L1_enable_xtalk_compensation(
1453 	VL53L1_DEV                 Dev)
1454 {
1455 	/**
1456 	 * Currently a very simple function to copy
1457 	 * private xtalk parms into customer section and apply to device
1458 	 *
1459 	 */
1460 
1461 	VL53L1_Error status = VL53L1_ERROR_NONE;
1462 	uint32_t tempu32;
1463 
1464 	VL53L1_LLDriverData_t *pdev = VL53L1DevStructGetLLDriverHandle(Dev);
1465 
1466 	LOG_FUNCTION_START("");
1467 
1468 	/* Fill Public customer NVM data with Xtalk parms */
1469 	tempu32 = VL53L1_calc_crosstalk_plane_offset_with_margin(
1470 		pdev->xtalk_cfg.algo__crosstalk_compensation_plane_offset_kcps,
1471 		pdev->xtalk_cfg.lite_mode_crosstalk_margin_kcps);
1472 	if (tempu32 > 0xFFFF) {
1473 		tempu32 = 0xFFFF;
1474 	}
1475 	pdev->customer.algo__crosstalk_compensation_plane_offset_kcps =
1476 		(uint16_t)tempu32;
1477 
1478 	pdev->customer.algo__crosstalk_compensation_x_plane_gradient_kcps =
1479 		pdev->xtalk_cfg.algo__crosstalk_compensation_x_plane_gradient_kcps;
1480 
1481 	pdev->customer.algo__crosstalk_compensation_y_plane_gradient_kcps =
1482 		pdev->xtalk_cfg.algo__crosstalk_compensation_y_plane_gradient_kcps;
1483 
1484 	/* Enable Xtalk compensation */
1485 	pdev->xtalk_cfg.global_crosstalk_compensation_enable = 0x01;
1486 
1487 	/* Update Range Ignore Threshold Xtalk Parameter */
1488 
1489 	if (status == VL53L1_ERROR_NONE) {
1490 		pdev->xtalk_cfg.crosstalk_range_ignore_threshold_rate_mcps =
1491 			VL53L1_calc_range_ignore_threshold(
1492 				pdev->xtalk_cfg.algo__crosstalk_compensation_plane_offset_kcps,
1493 				pdev->xtalk_cfg.algo__crosstalk_compensation_x_plane_gradient_kcps,
1494 				pdev->xtalk_cfg.algo__crosstalk_compensation_y_plane_gradient_kcps,
1495 				pdev->xtalk_cfg.crosstalk_range_ignore_threshold_mult);
1496 	}
1497 
1498 	/* Apply to device */
1499 
1500 	if (status == VL53L1_ERROR_NONE) /*lint !e774 always true*/
1501 		status =
1502 			VL53L1_set_customer_nvm_managed(
1503 				Dev,
1504 				&(pdev->customer));
1505 
1506 	LOG_FUNCTION_END(status);
1507 
1508 	return status;
1509 
1510 }
1511 
VL53L1_get_xtalk_compensation_enable(VL53L1_DEV Dev,uint8_t * pcrosstalk_compensation_enable)1512 void VL53L1_get_xtalk_compensation_enable(
1513 	VL53L1_DEV    Dev,
1514 	uint8_t       *pcrosstalk_compensation_enable)
1515 {
1516 	/**
1517 	 * Currently a very simple function to return
1518 	 *
1519 	 * - this flags whether xtalk compensation is enabled for all modes
1520 	 * or not.
1521 	 *
1522 	 * #1 - Enabled
1523 	 * #0 - Disabled
1524 	 *
1525 	 */
1526 
1527 	VL53L1_LLDriverData_t *pdev = VL53L1DevStructGetLLDriverHandle(Dev);
1528 
1529 	LOG_FUNCTION_START("");
1530 
1531 	/* Extract Xtalk Compensation Enable Status*/
1532 
1533 	*pcrosstalk_compensation_enable =
1534 		pdev->xtalk_cfg.global_crosstalk_compensation_enable;
1535 
1536 }
1537 
1538 
VL53L1_get_lite_xtalk_margin_kcps(VL53L1_DEV Dev,int16_t * pxtalk_margin)1539 VL53L1_Error VL53L1_get_lite_xtalk_margin_kcps(
1540 	VL53L1_DEV                          Dev,
1541 	int16_t                           *pxtalk_margin)
1542 {
1543 
1544 	/*
1545 	 * Gets the Xtalk Margin Factor  in Kcps (fixed point 9.7)
1546 	 */
1547 
1548 	VL53L1_Error  status = VL53L1_ERROR_NONE;
1549 
1550 	VL53L1_LLDriverData_t *pdev = VL53L1DevStructGetLLDriverHandle(Dev);
1551 
1552 	LOG_FUNCTION_START("");
1553 
1554 	*pxtalk_margin = pdev->xtalk_cfg.lite_mode_crosstalk_margin_kcps;
1555 
1556 	LOG_FUNCTION_END(status);
1557 
1558 	return status;
1559 
1560 }
1561 
VL53L1_set_lite_xtalk_margin_kcps(VL53L1_DEV Dev,int16_t xtalk_margin)1562 VL53L1_Error VL53L1_set_lite_xtalk_margin_kcps(
1563 	VL53L1_DEV                     Dev,
1564 	int16_t                        xtalk_margin)
1565 {
1566 
1567 	/*
1568 	 * Sets the offset calibration mode
1569 	 */
1570 
1571 	VL53L1_Error  status = VL53L1_ERROR_NONE;
1572 
1573 	VL53L1_LLDriverData_t *pdev = VL53L1DevStructGetLLDriverHandle(Dev);
1574 
1575 	LOG_FUNCTION_START("");
1576 
1577 	pdev->xtalk_cfg.lite_mode_crosstalk_margin_kcps = xtalk_margin;
1578 
1579 	LOG_FUNCTION_END(status);
1580 
1581 	return status;
1582 }
1583 
1584 
VL53L1_restore_xtalk_nvm_default(VL53L1_DEV Dev)1585 VL53L1_Error VL53L1_restore_xtalk_nvm_default(
1586 	VL53L1_DEV                     Dev)
1587 {
1588 
1589 	/*
1590 	 * Returns xtalk rate values to defaults as extracted from device NVM
1591 	 */
1592 
1593 	VL53L1_Error  status = VL53L1_ERROR_NONE;
1594 
1595 	VL53L1_LLDriverData_t *pdev = VL53L1DevStructGetLLDriverHandle(Dev);
1596 
1597 	LOG_FUNCTION_START("");
1598 
1599 	pdev->xtalk_cfg.algo__crosstalk_compensation_plane_offset_kcps =
1600 		pdev->xtalk_cfg.nvm_default__crosstalk_compensation_plane_offset_kcps;
1601 	pdev->xtalk_cfg.algo__crosstalk_compensation_x_plane_gradient_kcps =
1602 		pdev->xtalk_cfg.nvm_default__crosstalk_compensation_x_plane_gradient_kcps;
1603 	pdev->xtalk_cfg.algo__crosstalk_compensation_y_plane_gradient_kcps =
1604 		pdev->xtalk_cfg.nvm_default__crosstalk_compensation_y_plane_gradient_kcps;
1605 
1606 	LOG_FUNCTION_END(status);
1607 
1608 	return status;
1609 }
1610 
VL53L1_disable_xtalk_compensation(VL53L1_DEV Dev)1611 VL53L1_Error  VL53L1_disable_xtalk_compensation(
1612 	VL53L1_DEV                 Dev)
1613 {
1614 	/**
1615 	 * Currently a very simple function to clear
1616 	 * customer xtalk parms and apply to device
1617 	 *
1618 	 */
1619 
1620 	VL53L1_Error status = VL53L1_ERROR_NONE;
1621 
1622 	VL53L1_LLDriverData_t *pdev = VL53L1DevStructGetLLDriverHandle(Dev);
1623 
1624 	LOG_FUNCTION_START("");
1625 
1626 	/* Fill Public customer NVM data with Xtalk parms */
1627 	pdev->customer.algo__crosstalk_compensation_plane_offset_kcps =
1628 		0x00;
1629 
1630 	pdev->customer.algo__crosstalk_compensation_x_plane_gradient_kcps =
1631 		0x00;
1632 
1633 	pdev->customer.algo__crosstalk_compensation_y_plane_gradient_kcps =
1634 		0x00;
1635 
1636 
1637 	/* Disable Global Xtalk comnpensation */
1638 	pdev->xtalk_cfg.global_crosstalk_compensation_enable = 0x00;
1639 
1640 	/* Update Range Ignore Threshold Xtalk Parameter */
1641 
1642 	if (status == VL53L1_ERROR_NONE) {
1643 		pdev->xtalk_cfg.crosstalk_range_ignore_threshold_rate_mcps =
1644 			0x0000;
1645 	}
1646 
1647 	/* Apply to device */
1648 
1649 	if (status == VL53L1_ERROR_NONE) { /*lint !e774 always true*/
1650 		status =
1651 			VL53L1_set_customer_nvm_managed(
1652 				Dev,
1653 				&(pdev->customer));
1654 	}
1655 	LOG_FUNCTION_END(status);
1656 
1657 	return status;
1658 
1659 }
1660 
VL53L1_get_lite_sigma_threshold(VL53L1_DEV Dev,uint16_t * plite_sigma)1661 VL53L1_Error VL53L1_get_lite_sigma_threshold(
1662 	VL53L1_DEV                          Dev,
1663 	uint16_t                           *plite_sigma)
1664 {
1665 
1666 	/*
1667 	 * Gets the Sigma Threshold value for Lite Mode
1668 	 *
1669 	 * (fixed point 14.2)
1670 	 */
1671 
1672 	VL53L1_Error  status = VL53L1_ERROR_NONE;
1673 
1674 	VL53L1_LLDriverData_t *pdev = VL53L1DevStructGetLLDriverHandle(Dev);
1675 
1676 	LOG_FUNCTION_START("");
1677 
1678 	*plite_sigma =
1679 			pdev->tim_cfg.range_config__sigma_thresh;
1680 
1681 	LOG_FUNCTION_END(status);
1682 
1683 	return status;
1684 
1685 }
1686 
VL53L1_set_lite_sigma_threshold(VL53L1_DEV Dev,uint16_t lite_sigma)1687 VL53L1_Error VL53L1_set_lite_sigma_threshold(
1688 	VL53L1_DEV                          Dev,
1689 	uint16_t                           lite_sigma)
1690 {
1691 
1692 	/*
1693 	 * Sets the Sigma threshold value for Lite mode
1694 	 *
1695 	 * (fixed point 14.2)
1696 	 */
1697 
1698 	VL53L1_Error  status = VL53L1_ERROR_NONE;
1699 
1700 	VL53L1_LLDriverData_t *pdev = VL53L1DevStructGetLLDriverHandle(Dev);
1701 
1702 	LOG_FUNCTION_START("");
1703 
1704 	pdev->tim_cfg.range_config__sigma_thresh = lite_sigma;
1705 
1706 	LOG_FUNCTION_END(status);
1707 
1708 	return status;
1709 
1710 }
1711 
VL53L1_get_lite_min_count_rate(VL53L1_DEV Dev,uint16_t * plite_mincountrate)1712 VL53L1_Error VL53L1_get_lite_min_count_rate(
1713 	VL53L1_DEV                          Dev,
1714 	uint16_t                           *plite_mincountrate)
1715 {
1716 
1717 	/*
1718 	 * Gets the Min Count Rate value for Lite Mode
1719 	 *
1720 	 * (fixed point 9.7 Mcps)
1721 	 */
1722 
1723 	VL53L1_Error  status = VL53L1_ERROR_NONE;
1724 
1725 	VL53L1_LLDriverData_t *pdev = VL53L1DevStructGetLLDriverHandle(Dev);
1726 
1727 	LOG_FUNCTION_START("");
1728 
1729 	*plite_mincountrate =
1730 			pdev->tim_cfg.range_config__min_count_rate_rtn_limit_mcps;
1731 
1732 	LOG_FUNCTION_END(status);
1733 
1734 	return status;
1735 
1736 }
1737 
VL53L1_set_lite_min_count_rate(VL53L1_DEV Dev,uint16_t lite_mincountrate)1738 VL53L1_Error VL53L1_set_lite_min_count_rate(
1739 	VL53L1_DEV                          Dev,
1740 	uint16_t                            lite_mincountrate)
1741 {
1742 
1743 	/*
1744 	 * Sets the Min COunt Rate value for Lite mode
1745 	 *
1746 	 * (fixed point 19.7Mcps)
1747 	 */
1748 
1749 	VL53L1_Error  status = VL53L1_ERROR_NONE;
1750 
1751 	VL53L1_LLDriverData_t *pdev = VL53L1DevStructGetLLDriverHandle(Dev);
1752 
1753 	LOG_FUNCTION_START("");
1754 
1755 	pdev->tim_cfg.range_config__min_count_rate_rtn_limit_mcps =
1756 		lite_mincountrate;
1757 
1758 	LOG_FUNCTION_END(status);
1759 
1760 	return status;
1761 
1762 }
1763 
VL53L1_get_vhv_loopbound(VL53L1_DEV Dev,uint8_t * pvhv_loopbound)1764 VL53L1_Error VL53L1_get_vhv_loopbound(
1765 	VL53L1_DEV                   Dev,
1766 	uint8_t                     *pvhv_loopbound)
1767 {
1768 
1769 	/*
1770 	 * Gets the VHV Loop bound parm
1771 	 * - extracts only bits 7:2 from internal stat nvm parm
1772 	 */
1773 
1774 	VL53L1_Error  status = VL53L1_ERROR_NONE;
1775 
1776 	VL53L1_LLDriverData_t *pdev = VL53L1DevStructGetLLDriverHandle(Dev);
1777 
1778 	LOG_FUNCTION_START("");
1779 
1780 	*pvhv_loopbound = pdev->stat_nvm.vhv_config__timeout_macrop_loop_bound / 4 ;
1781 
1782 	LOG_FUNCTION_END(status);
1783 
1784 	return status;
1785 
1786 }
1787 
1788 
1789 
VL53L1_set_vhv_loopbound(VL53L1_DEV Dev,uint8_t vhv_loopbound)1790 VL53L1_Error VL53L1_set_vhv_loopbound(
1791 	VL53L1_DEV                   Dev,
1792 	uint8_t                      vhv_loopbound)
1793 {
1794 
1795 	/*
1796 	 * Sets the VHV Loop bound parm
1797 	 * - sets only bits 7:2
1798 	 * - bits 1:0 remain unchanged
1799 	 * - ensure that any change here is followed by a
1800 	 * init_and_start_range with full i2c packet
1801 	 * configuration.
1802 	 */
1803 
1804 	VL53L1_Error  status = VL53L1_ERROR_NONE;
1805 
1806 	VL53L1_LLDriverData_t *pdev = VL53L1DevStructGetLLDriverHandle(Dev);
1807 
1808 	LOG_FUNCTION_START("");
1809 
1810 	pdev->stat_nvm.vhv_config__timeout_macrop_loop_bound =
1811 			(pdev->stat_nvm.vhv_config__timeout_macrop_loop_bound & 0x03) +
1812 			(vhv_loopbound * 4);
1813 
1814 	LOG_FUNCTION_END(status);
1815 
1816 	return status;
1817 
1818 }
1819 
1820 
1821 
VL53L1_get_vhv_config(VL53L1_DEV Dev,uint8_t * pvhv_init_en,uint8_t * pvhv_init_value)1822 VL53L1_Error VL53L1_get_vhv_config(
1823 	VL53L1_DEV                   Dev,
1824 	uint8_t                     *pvhv_init_en,
1825 	uint8_t                     *pvhv_init_value)
1826 {
1827 
1828 	/*
1829 	 * Gets the VHV config init data
1830 	 */
1831 
1832 	/*!<
1833 		info: \n
1834 			- msb =  7
1835 			- lsb =  0
1836 			- i2c_size =  1
1837 
1838 		fields: \n
1839 			-   [7] = vhv0_init_enable
1840 			- [5:0] = vhv0_init_value
1841 	*/
1842 
1843 	VL53L1_Error  status = VL53L1_ERROR_NONE;
1844 
1845 	VL53L1_LLDriverData_t *pdev = VL53L1DevStructGetLLDriverHandle(Dev);
1846 
1847 	LOG_FUNCTION_START("");
1848 
1849 	*pvhv_init_en    = (pdev->stat_nvm.vhv_config__init & 0x80) >> 7;
1850 	*pvhv_init_value =
1851 			(pdev->stat_nvm.vhv_config__init & 0x7F);
1852 
1853 	LOG_FUNCTION_END(status);
1854 
1855 	return status;
1856 
1857 }
1858 
1859 
1860 
VL53L1_set_vhv_config(VL53L1_DEV Dev,uint8_t vhv_init_en,uint8_t vhv_init_value)1861 VL53L1_Error VL53L1_set_vhv_config(
1862 	VL53L1_DEV                   Dev,
1863 	uint8_t                      vhv_init_en,
1864 	uint8_t                      vhv_init_value)
1865 {
1866 
1867 	/*
1868 	 * Sets the VHV Config init
1869 	 */
1870 
1871 	VL53L1_Error  status = VL53L1_ERROR_NONE;
1872 
1873 	VL53L1_LLDriverData_t *pdev = VL53L1DevStructGetLLDriverHandle(Dev);
1874 
1875 	LOG_FUNCTION_START("");
1876 
1877 	pdev->stat_nvm.vhv_config__init =
1878 		((vhv_init_en   & 0x01) << 7) +
1879 		(vhv_init_value & 0x7F);
1880 
1881 	LOG_FUNCTION_END(status);
1882 
1883 	return status;
1884 
1885 }
1886 
1887 
1888 
VL53L1_init_and_start_range(VL53L1_DEV Dev,uint8_t measurement_mode,VL53L1_DeviceConfigLevel device_config_level)1889 VL53L1_Error VL53L1_init_and_start_range(
1890 	VL53L1_DEV                     Dev,
1891 	uint8_t                        measurement_mode,
1892 	VL53L1_DeviceConfigLevel       device_config_level)
1893 {
1894 	/*
1895 	 * Builds and sends a single I2C multiple byte transaction to
1896 	 * initialize the device and start a range measurement.
1897 	 *
1898 	 * The level of initialization is controlled by the
1899 	 * device_config_level input parameter
1900 	 *
1901 	 * system_control is always sent as the last byte of this
1902 	 * register group (mode_start) either triggers the range
1903 	 * or enables the next range
1904 	 */
1905 
1906 	VL53L1_Error status = VL53L1_ERROR_NONE;
1907 	VL53L1_LLDriverData_t *pdev = VL53L1DevStructGetLLDriverHandle(Dev);
1908 
1909 	uint8_t buffer[VL53L1_MAX_I2C_XFER_SIZE];
1910 
1911 	VL53L1_static_nvm_managed_t   *pstatic_nvm   = &(pdev->stat_nvm);
1912 	VL53L1_customer_nvm_managed_t *pcustomer_nvm = &(pdev->customer);
1913 	VL53L1_static_config_t        *pstatic       = &(pdev->stat_cfg);
1914 	VL53L1_general_config_t       *pgeneral      = &(pdev->gen_cfg);
1915 	VL53L1_timing_config_t        *ptiming       = &(pdev->tim_cfg);
1916 	VL53L1_dynamic_config_t       *pdynamic      = &(pdev->dyn_cfg);
1917 	VL53L1_system_control_t       *psystem       = &(pdev->sys_ctrl);
1918 
1919 	VL53L1_ll_driver_state_t  *pstate   = &(pdev->ll_state);
1920 
1921 	uint8_t  *pbuffer                   = &buffer[0];
1922 	uint16_t i                          = 0;
1923 	uint16_t i2c_index                  = 0;
1924 	uint16_t i2c_buffer_offset_bytes    = 0;
1925 	uint16_t i2c_buffer_size_bytes      = 0;
1926 
1927 	LOG_FUNCTION_START("");
1928 
1929 	/* save measurement mode */
1930 	pdev->measurement_mode = measurement_mode;
1931 
1932 	/* Merge measurement mode with mode_start */
1933 
1934 	psystem->system__mode_start =
1935 		(psystem->system__mode_start &
1936 		VL53L1_DEVICEMEASUREMENTMODE_STOP_MASK) |
1937 		measurement_mode;
1938 
1939 	/* copy in rit from xtalk config */
1940 
1941 	pdev->stat_cfg.algo__range_ignore_threshold_mcps =
1942 		pdev->xtalk_cfg.crosstalk_range_ignore_threshold_rate_mcps;
1943 
1944 	/* Start Patch_LowPowerAutoMode */
1945 
1946 	/* doing this ensures stop_range followed by a get_device_results does
1947 	 * not mess up the counters */
1948 
1949 	if (pdev->low_power_auto_data.low_power_auto_range_count == 0xFF) {
1950 		pdev->low_power_auto_data.low_power_auto_range_count = 0x0;
1951 	}
1952 
1953 	/* For Presence. Override threshold config */
1954 	if ((pdev->low_power_auto_data.is_low_power_auto_mode == 1) &&
1955 		(pdev->low_power_auto_data.low_power_auto_range_count == 0)) {
1956 		/* save interrupt config */
1957 		pdev->low_power_auto_data.saved_interrupt_config =
1958 			pdev->gen_cfg.system__interrupt_config_gpio;
1959 		/* set intr_new_measure_ready */
1960 		pdev->gen_cfg.system__interrupt_config_gpio = 1 << 5;
1961 		/* check MM1/MM2 disabled? */
1962 		if ((pdev->dyn_cfg.system__sequence_config & (
1963 			VL53L1_SEQUENCE_MM1_EN | VL53L1_SEQUENCE_MM2_EN)) ==
1964 				0x0) {
1965 			pdev->customer.algo__part_to_part_range_offset_mm =
1966 				pdev->customer.mm_config__outer_offset_mm * 4;
1967 		} else {
1968 			pdev->customer.algo__part_to_part_range_offset_mm = 0x0;
1969 		}
1970 
1971 		/* make sure config gets written out */
1972 		if (device_config_level <
1973 				VL53L1_DEVICECONFIGLEVEL_CUSTOMER_ONWARDS) {
1974 			device_config_level =
1975 				VL53L1_DEVICECONFIGLEVEL_CUSTOMER_ONWARDS;
1976 		}
1977 	}
1978 
1979 	if ((pdev->low_power_auto_data.is_low_power_auto_mode == 1) &&
1980 		(pdev->low_power_auto_data.low_power_auto_range_count == 1)) {
1981 		/* restore interrupt config */
1982 		pdev->gen_cfg.system__interrupt_config_gpio =
1983 			pdev->low_power_auto_data.saved_interrupt_config;
1984 
1985 		/* make sure config gets written out including VHV config */
1986 		device_config_level = VL53L1_DEVICECONFIGLEVEL_FULL;
1987 	}
1988 
1989 	/* End Patch_LowPowerAutoMode */
1990 
1991 	/*
1992 	 * Determine Initial I2C index
1993 	 */
1994 
1995 	switch (device_config_level) {
1996 	case VL53L1_DEVICECONFIGLEVEL_FULL:
1997 		i2c_index = VL53L1_STATIC_NVM_MANAGED_I2C_INDEX;
1998 		break;
1999 	case VL53L1_DEVICECONFIGLEVEL_CUSTOMER_ONWARDS:
2000 		i2c_index = VL53L1_CUSTOMER_NVM_MANAGED_I2C_INDEX;
2001 		break;
2002 	case VL53L1_DEVICECONFIGLEVEL_STATIC_ONWARDS:
2003 		i2c_index = VL53L1_STATIC_CONFIG_I2C_INDEX;
2004 		break;
2005 	case VL53L1_DEVICECONFIGLEVEL_GENERAL_ONWARDS:
2006 		i2c_index = VL53L1_GENERAL_CONFIG_I2C_INDEX;
2007 		break;
2008 	case VL53L1_DEVICECONFIGLEVEL_TIMING_ONWARDS:
2009 		i2c_index = VL53L1_TIMING_CONFIG_I2C_INDEX;
2010 		break;
2011 	case VL53L1_DEVICECONFIGLEVEL_DYNAMIC_ONWARDS:
2012 		i2c_index = VL53L1_DYNAMIC_CONFIG_I2C_INDEX;
2013 		break;
2014 	default:
2015 		i2c_index = VL53L1_SYSTEM_CONTROL_I2C_INDEX;
2016 		break;
2017 	}
2018 
2019 	/* I2C Buffer size */
2020 
2021 	i2c_buffer_size_bytes = \
2022 			(VL53L1_SYSTEM_CONTROL_I2C_INDEX +
2023 			 VL53L1_SYSTEM_CONTROL_I2C_SIZE_BYTES) -
2024 			 i2c_index;
2025 
2026 	/* Initialize buffer */
2027 
2028 	pbuffer = &buffer[0];
2029 	for (i = 0 ; i < i2c_buffer_size_bytes ; i++) {
2030 		*pbuffer++ = 0;
2031 	}
2032 
2033 	/* Build I2C buffer */
2034 
2035 	if (device_config_level >= VL53L1_DEVICECONFIGLEVEL_FULL &&
2036 		status == VL53L1_ERROR_NONE) {
2037 
2038 		i2c_buffer_offset_bytes = \
2039 			VL53L1_STATIC_NVM_MANAGED_I2C_INDEX - i2c_index;
2040 
2041 		status =
2042 			VL53L1_i2c_encode_static_nvm_managed(
2043 				pstatic_nvm,
2044 				VL53L1_STATIC_NVM_MANAGED_I2C_SIZE_BYTES,
2045 				&buffer[i2c_buffer_offset_bytes]);
2046 	}
2047 
2048 	if (device_config_level >= VL53L1_DEVICECONFIGLEVEL_CUSTOMER_ONWARDS &&
2049 		status == VL53L1_ERROR_NONE) {
2050 
2051 		i2c_buffer_offset_bytes = \
2052 			VL53L1_CUSTOMER_NVM_MANAGED_I2C_INDEX - i2c_index;
2053 
2054 		status =
2055 			VL53L1_i2c_encode_customer_nvm_managed(
2056 				pcustomer_nvm,
2057 				VL53L1_CUSTOMER_NVM_MANAGED_I2C_SIZE_BYTES,
2058 				&buffer[i2c_buffer_offset_bytes]);
2059 	}
2060 
2061 	if (device_config_level >= VL53L1_DEVICECONFIGLEVEL_STATIC_ONWARDS &&
2062 		status == VL53L1_ERROR_NONE) {
2063 
2064 		i2c_buffer_offset_bytes = \
2065 			VL53L1_STATIC_CONFIG_I2C_INDEX - i2c_index;
2066 
2067 		status =
2068 			VL53L1_i2c_encode_static_config(
2069 				pstatic,
2070 				VL53L1_STATIC_CONFIG_I2C_SIZE_BYTES,
2071 				&buffer[i2c_buffer_offset_bytes]);
2072 	}
2073 
2074 	if (device_config_level >= VL53L1_DEVICECONFIGLEVEL_GENERAL_ONWARDS &&
2075 		status == VL53L1_ERROR_NONE) {
2076 
2077 		i2c_buffer_offset_bytes =
2078 				VL53L1_GENERAL_CONFIG_I2C_INDEX - i2c_index;
2079 
2080 		status =
2081 			VL53L1_i2c_encode_general_config(
2082 				pgeneral,
2083 				VL53L1_GENERAL_CONFIG_I2C_SIZE_BYTES,
2084 				&buffer[i2c_buffer_offset_bytes]);
2085 	}
2086 
2087 	if (device_config_level >= VL53L1_DEVICECONFIGLEVEL_TIMING_ONWARDS &&
2088 		status == VL53L1_ERROR_NONE) {
2089 
2090 		i2c_buffer_offset_bytes = \
2091 				VL53L1_TIMING_CONFIG_I2C_INDEX - i2c_index;
2092 
2093 		status =
2094 			VL53L1_i2c_encode_timing_config(
2095 				ptiming,
2096 				VL53L1_TIMING_CONFIG_I2C_SIZE_BYTES,
2097 				&buffer[i2c_buffer_offset_bytes]);
2098 	}
2099 
2100 	if (device_config_level >= VL53L1_DEVICECONFIGLEVEL_DYNAMIC_ONWARDS &&
2101 		status == VL53L1_ERROR_NONE) {
2102 
2103 		i2c_buffer_offset_bytes = \
2104 			VL53L1_DYNAMIC_CONFIG_I2C_INDEX - i2c_index;
2105 
2106 		/* If in back to back mode, use GPH ID from cfg_state */
2107 		if ((psystem->system__mode_start &
2108 			VL53L1_DEVICEMEASUREMENTMODE_BACKTOBACK) ==
2109 			VL53L1_DEVICEMEASUREMENTMODE_BACKTOBACK) {
2110 			pdynamic->system__grouped_parameter_hold_0 = pstate->cfg_gph_id | 0x01;
2111 			pdynamic->system__grouped_parameter_hold_1 = pstate->cfg_gph_id | 0x01;
2112 			pdynamic->system__grouped_parameter_hold   = pstate->cfg_gph_id;
2113 		}
2114 		status =
2115 			VL53L1_i2c_encode_dynamic_config(
2116 				pdynamic,
2117 				VL53L1_DYNAMIC_CONFIG_I2C_SIZE_BYTES,
2118 				&buffer[i2c_buffer_offset_bytes]);
2119 	}
2120 
2121 	if (status == VL53L1_ERROR_NONE) {
2122 
2123 		i2c_buffer_offset_bytes = \
2124 				VL53L1_SYSTEM_CONTROL_I2C_INDEX - i2c_index;
2125 
2126 		status =
2127 			VL53L1_i2c_encode_system_control(
2128 				psystem,
2129 				VL53L1_SYSTEM_CONTROL_I2C_SIZE_BYTES,
2130 				&buffer[i2c_buffer_offset_bytes]);
2131 	}
2132 
2133 	/* Send I2C Buffer */
2134 
2135 	if (status == VL53L1_ERROR_NONE) {
2136 		status =
2137 			VL53L1_WriteMulti(
2138 				Dev,
2139 				i2c_index,
2140 				buffer,
2141 				(uint32_t)i2c_buffer_size_bytes);
2142 	}
2143 
2144 	/*
2145 	 * Update LL Driver State
2146 	 */
2147 	if (status == VL53L1_ERROR_NONE)
2148 		status = VL53L1_update_ll_driver_rd_state(Dev);
2149 
2150 	if (status == VL53L1_ERROR_NONE)
2151 		status = VL53L1_update_ll_driver_cfg_state(Dev);
2152 
2153 	LOG_FUNCTION_END(status);
2154 
2155 	return status;
2156 }
2157 
2158 
VL53L1_stop_range(VL53L1_DEV Dev)2159 VL53L1_Error VL53L1_stop_range(
2160 	VL53L1_DEV     Dev)
2161 {
2162 	/*
2163 	 * Stops any in process range using the ABORT command
2164 	 * Also clears all of the measurement mode bits
2165 	 */
2166 
2167 	VL53L1_Error status = VL53L1_ERROR_NONE;
2168 
2169 	VL53L1_LLDriverData_t *pdev =
2170 			VL53L1DevStructGetLLDriverHandle(Dev);
2171 
2172 	/* Merge ABORT mode with mode_start */
2173 
2174 	pdev->sys_ctrl.system__mode_start =
2175 			(pdev->sys_ctrl.system__mode_start & VL53L1_DEVICEMEASUREMENTMODE_STOP_MASK) |
2176 			 VL53L1_DEVICEMEASUREMENTMODE_ABORT;
2177 
2178 	status = VL53L1_set_system_control(
2179 				Dev,
2180 				&pdev->sys_ctrl);
2181 
2182 	/* Abort bit is auto clear so clear register group structure to match */
2183 	pdev->sys_ctrl.system__mode_start =
2184 			(pdev->sys_ctrl.system__mode_start & VL53L1_DEVICEMEASUREMENTMODE_STOP_MASK);
2185 
2186 	/* reset zone dynamic info */
2187 	VL53L1_init_ll_driver_state(
2188 			Dev,
2189 			VL53L1_DEVICESTATE_SW_STANDBY);
2190 
2191 	/* reset low power auto */
2192 	if (pdev->low_power_auto_data.is_low_power_auto_mode == 1)
2193 		VL53L1_low_power_auto_data_stop_range(Dev);
2194 
2195 	return status;
2196 }
2197 
2198 
VL53L1_get_measurement_results(VL53L1_DEV Dev,VL53L1_DeviceResultsLevel device_results_level)2199 VL53L1_Error VL53L1_get_measurement_results(
2200 	VL53L1_DEV                     Dev,
2201 	VL53L1_DeviceResultsLevel      device_results_level)
2202 {
2203 	/*
2204 	 * Read via a single I2C multiple byte transaction all
2205 	 * of the requested device measurement data results
2206 	 */
2207 
2208 	VL53L1_Error status = VL53L1_ERROR_NONE;
2209 	VL53L1_LLDriverData_t *pdev = VL53L1DevStructGetLLDriverHandle(Dev);
2210 
2211 	uint8_t buffer[VL53L1_MAX_I2C_XFER_SIZE];
2212 
2213 	VL53L1_system_results_t   *psystem_results = &(pdev->sys_results);
2214 	VL53L1_core_results_t     *pcore_results   = &(pdev->core_results);
2215 	VL53L1_debug_results_t    *pdebug_results  = &(pdev->dbg_results);
2216 
2217 	uint16_t i2c_index               = VL53L1_SYSTEM_RESULTS_I2C_INDEX;
2218 	uint16_t i2c_buffer_offset_bytes = 0;
2219 	uint16_t i2c_buffer_size_bytes   = 0;
2220 
2221 	LOG_FUNCTION_START("");
2222 
2223 	/* Determine multi byte read transaction size */
2224 
2225 	switch (device_results_level) {
2226 	case VL53L1_DEVICERESULTSLEVEL_FULL:
2227 		i2c_buffer_size_bytes =
2228 				(VL53L1_DEBUG_RESULTS_I2C_INDEX +
2229 				VL53L1_DEBUG_RESULTS_I2C_SIZE_BYTES) -
2230 				i2c_index;
2231 		break;
2232 	case VL53L1_DEVICERESULTSLEVEL_UPTO_CORE:
2233 		i2c_buffer_size_bytes =
2234 				(VL53L1_CORE_RESULTS_I2C_INDEX +
2235 				VL53L1_CORE_RESULTS_I2C_SIZE_BYTES) -
2236 				i2c_index;
2237 		break;
2238 	default:
2239 		i2c_buffer_size_bytes =
2240 				VL53L1_SYSTEM_RESULTS_I2C_SIZE_BYTES;
2241 		break;
2242 	}
2243 
2244 	/* Read  Result Data */
2245 
2246 	if (status == VL53L1_ERROR_NONE) /*lint !e774 always true*/
2247 		status =
2248 			VL53L1_ReadMulti(
2249 				Dev,
2250 				i2c_index,
2251 				buffer,
2252 				(uint32_t)i2c_buffer_size_bytes);
2253 
2254 	/* Decode  I2C buffer */
2255 
2256 	if (device_results_level >= VL53L1_DEVICERESULTSLEVEL_FULL &&
2257 		status == VL53L1_ERROR_NONE) {
2258 
2259 		i2c_buffer_offset_bytes =
2260 				VL53L1_DEBUG_RESULTS_I2C_INDEX - i2c_index;
2261 
2262 		status =
2263 			VL53L1_i2c_decode_debug_results(
2264 				VL53L1_DEBUG_RESULTS_I2C_SIZE_BYTES,
2265 				&buffer[i2c_buffer_offset_bytes],
2266 				pdebug_results);
2267 	}
2268 
2269 	if (device_results_level >= VL53L1_DEVICERESULTSLEVEL_UPTO_CORE &&
2270 		status == VL53L1_ERROR_NONE) {
2271 
2272 		i2c_buffer_offset_bytes =
2273 				VL53L1_CORE_RESULTS_I2C_INDEX - i2c_index;
2274 
2275 		status =
2276 			VL53L1_i2c_decode_core_results(
2277 				VL53L1_CORE_RESULTS_I2C_SIZE_BYTES,
2278 				&buffer[i2c_buffer_offset_bytes],
2279 				pcore_results);
2280 	}
2281 
2282 	if (status == VL53L1_ERROR_NONE) {
2283 
2284 		i2c_buffer_offset_bytes = 0;
2285 		status =
2286 			VL53L1_i2c_decode_system_results(
2287 				VL53L1_SYSTEM_RESULTS_I2C_SIZE_BYTES,
2288 				&buffer[i2c_buffer_offset_bytes],
2289 				psystem_results);
2290 	}
2291 
2292 	LOG_FUNCTION_END(status);
2293 
2294 	return status;
2295 }
2296 
2297 
VL53L1_get_device_results(VL53L1_DEV Dev,VL53L1_DeviceResultsLevel device_results_level,VL53L1_range_results_t * prange_results)2298 VL53L1_Error VL53L1_get_device_results(
2299 	VL53L1_DEV                    Dev,
2300 	VL53L1_DeviceResultsLevel     device_results_level,
2301 	VL53L1_range_results_t       *prange_results)
2302 {
2303 	/*
2304 	 * Wrapper function using the functions below
2305 	 *
2306 	 *  VL53L1_get_measurement_results()
2307 	 *  VL53L1_init_and_start_range()
2308 	 *  VL53L1_copy_sys_and_core_results_to_range_results()
2309 	 *
2310 	 *  The input measurement mode controls what happens next ...
2311 	 */
2312 
2313 	VL53L1_Error status = VL53L1_ERROR_NONE;
2314 
2315 	VL53L1_LLDriverData_t *pdev =
2316 			VL53L1DevStructGetLLDriverHandle(Dev);
2317 	VL53L1_LLDriverResults_t *pres =
2318 			VL53L1DevStructGetLLResultsHandle(Dev);
2319 
2320 	VL53L1_range_results_t   *presults = &(pres->range_results);
2321 
2322 	LOG_FUNCTION_START("");
2323 
2324 	/* Get device results */
2325 
2326 	if (status == VL53L1_ERROR_NONE) /*lint !e774 always true*/
2327 		status = VL53L1_get_measurement_results(
2328 						Dev,
2329 						device_results_level);
2330 
2331 	if (status == VL53L1_ERROR_NONE)
2332 		VL53L1_copy_sys_and_core_results_to_range_results(
2333 				(int32_t)pdev->gain_cal.standard_ranging_gain_factor,
2334 				&(pdev->sys_results),
2335 				&(pdev->core_results),
2336 				presults);
2337 
2338 	/* Start Patch_LowPowerAutoMode */
2339 	/* process results from first range of low power auto */
2340 	if (pdev->low_power_auto_data.is_low_power_auto_mode == 1) {
2341 		/* change to manual calibrations. Only needed on the
2342 		 * first range out  */
2343 		if ((status == VL53L1_ERROR_NONE) &&
2344 			(pdev->low_power_auto_data.low_power_auto_range_count == 0)) {
2345 			status = VL53L1_low_power_auto_setup_manual_calibration(
2346 					Dev);
2347 			pdev->low_power_auto_data.low_power_auto_range_count = 1;
2348 		} else if ((status == VL53L1_ERROR_NONE) &&
2349 			(pdev->low_power_auto_data.low_power_auto_range_count == 1)) {
2350 			pdev->low_power_auto_data.low_power_auto_range_count = 2;
2351 		}
2352 
2353 		/* perform DSS calculation. This can be performed every range */
2354 		if ((pdev->low_power_auto_data.low_power_auto_range_count != 0xFF) &&
2355 			(status == VL53L1_ERROR_NONE)) {
2356 			status = VL53L1_low_power_auto_update_DSS(
2357 					Dev);
2358 		}
2359 
2360 	}
2361 	/* End Patch_LowPowerAutoMode */
2362 
2363 	/* copy current state into results */
2364 
2365 	presults->cfg_device_state = pdev->ll_state.cfg_device_state;
2366 	presults->rd_device_state  = pdev->ll_state.rd_device_state;
2367 
2368 	/* copy internal structure to supplied output pointer */
2369 
2370 	memcpy(
2371 		prange_results,
2372 		presults,
2373 		sizeof(VL53L1_range_results_t));
2374 
2375 	/*
2376 	 * Check LL driver and Device are in Sync
2377 	 * If not an error is raised
2378 	 */
2379 
2380 	if (status == VL53L1_ERROR_NONE)
2381 		status = VL53L1_check_ll_driver_rd_state(Dev);
2382 
2383 #ifdef VL53L1_LOG_ENABLE
2384 	if (status == VL53L1_ERROR_NONE)
2385 		VL53L1_print_range_results(
2386 			presults,
2387 			"get_device_results():pdev->llresults.range_results.",
2388 			VL53L1_TRACE_MODULE_RANGE_RESULTS_DATA);
2389 #endif
2390 
2391 	LOG_FUNCTION_END(status);
2392 
2393 	return status;
2394 }
2395 
2396 
VL53L1_clear_interrupt_and_enable_next_range(VL53L1_DEV Dev,uint8_t measurement_mode)2397 VL53L1_Error VL53L1_clear_interrupt_and_enable_next_range(
2398 	VL53L1_DEV        Dev,
2399 	uint8_t           measurement_mode)
2400 {
2401 
2402 	/*
2403 	 * Enable next range by sending handshake which
2404 	 * clears the interrupt
2405 	 */
2406 
2407 	VL53L1_Error status = VL53L1_ERROR_NONE;
2408 
2409 	LOG_FUNCTION_START("");
2410 
2411 	/* Dynamic Management */
2412 	/* Current results analysis and generate next settings */
2413 
2414 
2415 	/* Dynamic GPH Management     */
2416 	/* Setup GPH absorption point and config values for next measurement */
2417 
2418 	/* Update GPH registers, clear interrupt and set measurement mode */
2419 
2420 	if (status == VL53L1_ERROR_NONE) /*lint !e774 always true*/
2421 		status = VL53L1_init_and_start_range(
2422 					Dev,
2423 					measurement_mode,
2424 					VL53L1_DEVICECONFIGLEVEL_GENERAL_ONWARDS);
2425 
2426 	LOG_FUNCTION_END(status);
2427 
2428 	return status;
2429 }
2430 
2431 
VL53L1_copy_sys_and_core_results_to_range_results(int32_t gain_factor,VL53L1_system_results_t * psys,VL53L1_core_results_t * pcore,VL53L1_range_results_t * presults)2432 void VL53L1_copy_sys_and_core_results_to_range_results(
2433 	int32_t                           gain_factor,
2434 	VL53L1_system_results_t          *psys,
2435 	VL53L1_core_results_t            *pcore,
2436 	VL53L1_range_results_t           *presults)
2437 {
2438 	uint8_t  i = 0;
2439 
2440 	VL53L1_range_data_t *pdata;
2441 	int32_t range_mm = 0;
2442 	uint32_t tmpu32 = 0;
2443 
2444 	LOG_FUNCTION_START("");
2445 
2446 	/* copy results */
2447 
2448 	presults->stream_count    = psys->result__stream_count;
2449 
2450 	pdata = &(presults->data[0]);
2451 
2452 	for (i = 0 ; i < 2 ; i++) {
2453 
2454 		pdata->range_id     = i;
2455 		pdata->time_stamp   = 0;
2456 
2457 		if ((psys->result__stream_count == 0) &&
2458 			((psys->result__range_status & VL53L1_RANGE_STATUS__RANGE_STATUS_MASK) ==
2459 			VL53L1_DEVICEERROR_RANGECOMPLETE)) {
2460 			pdata->range_status = VL53L1_DEVICEERROR_RANGECOMPLETE_NO_WRAP_CHECK;
2461 		} else {
2462 			pdata->range_status =
2463 					psys->result__range_status & VL53L1_RANGE_STATUS__RANGE_STATUS_MASK;
2464 		}
2465 
2466 		switch (i) {
2467 
2468 		case 0:
2469 
2470 			if (psys->result__report_status == VL53L1_DEVICEREPORTSTATUS_MM1)
2471 				pdata->actual_effective_spads =
2472 					psys->result__mm_inner_actual_effective_spads_sd0;
2473 			else if (psys->result__report_status == VL53L1_DEVICEREPORTSTATUS_MM2)
2474 				pdata->actual_effective_spads =
2475 						psys->result__mm_outer_actual_effective_spads_sd0;
2476 			else
2477 				pdata->actual_effective_spads =
2478 					psys->result__dss_actual_effective_spads_sd0;
2479 
2480 			pdata->peak_signal_count_rate_mcps =
2481 				psys->result__peak_signal_count_rate_crosstalk_corrected_mcps_sd0;
2482 			pdata->avg_signal_count_rate_mcps =
2483 				psys->result__avg_signal_count_rate_mcps_sd0;
2484 			pdata->ambient_count_rate_mcps =
2485 				psys->result__ambient_count_rate_mcps_sd0;
2486 
2487 			/* Start Patch_SigmaEstimateAccuracyImprovement */
2488 
2489 			/* shift up sigma estimate to 7 bit fractional and clip to 9 bit int */
2490 			tmpu32 = ((uint32_t)psys->result__sigma_sd0 << 5);
2491 			if (tmpu32 > 0xFFFF) {
2492 				tmpu32 = 0xFFFF;
2493 			}
2494 			pdata->sigma_mm = (uint16_t)tmpu32;
2495 
2496 			/* End Patch_SigmaEstimateAccuracyImprovement */
2497 
2498 			pdata->median_phase =
2499 				psys->result__phase_sd0;
2500 
2501 			range_mm =
2502 				(int32_t)psys->result__final_crosstalk_corrected_range_mm_sd0;
2503 
2504 			/* apply correction gain */
2505 			range_mm *= gain_factor;
2506 			range_mm += 0x0400;
2507 			range_mm /= 0x0800;
2508 
2509 			pdata->median_range_mm = (int16_t)range_mm;
2510 
2511 			pdata->ranging_total_events =
2512 				pcore->result_core__ranging_total_events_sd0;
2513 			pdata->signal_total_events =
2514 				pcore->result_core__signal_total_events_sd0;
2515 			pdata->total_periods_elapsed =
2516 				pcore->result_core__total_periods_elapsed_sd0;
2517 			pdata->ambient_window_events =
2518 				pcore->result_core__ambient_window_events_sd0;
2519 
2520 			break;
2521 		case 1:
2522 
2523 			pdata->actual_effective_spads =
2524 				psys->result__dss_actual_effective_spads_sd1;
2525 			pdata->peak_signal_count_rate_mcps =
2526 				psys->result__peak_signal_count_rate_mcps_sd1;
2527 			pdata->avg_signal_count_rate_mcps =
2528 				0xFFFF;
2529 			pdata->ambient_count_rate_mcps =
2530 				psys->result__ambient_count_rate_mcps_sd1;
2531 
2532 			/* Start Patch_SigmaEstimateAccuracyImprovement */
2533 
2534 			/* shift up sigma estimate to 7 bit fractional and clip to 9 bit int */
2535 			tmpu32 = ((uint32_t)psys->result__sigma_sd1 << 5);
2536 			if (tmpu32 > 0xFFFF) {
2537 				tmpu32 = 0xFFFF;
2538 			}
2539 			pdata->sigma_mm = (uint16_t)tmpu32;
2540 
2541 			/* End Patch_SigmaEstimateAccuracyImprovement */
2542 
2543 			pdata->median_phase =
2544 				psys->result__phase_sd1;
2545 
2546 			range_mm =
2547 				(int32_t)psys->result__final_crosstalk_corrected_range_mm_sd1;
2548 
2549 			/* apply correction gain */
2550 			range_mm *= gain_factor;
2551 			range_mm += 0x0400;
2552 			range_mm /= 0x0800;
2553 
2554 			pdata->median_range_mm = (int16_t)range_mm;
2555 
2556 			pdata->ranging_total_events =
2557 				pcore->result_core__ranging_total_events_sd1;
2558 			pdata->signal_total_events =
2559 				pcore->result_core__signal_total_events_sd1;
2560 			pdata->total_periods_elapsed  =
2561 				pcore->result_core__total_periods_elapsed_sd1;
2562 			pdata->ambient_window_events =
2563 				pcore->result_core__ambient_window_events_sd1;
2564 
2565 			break;
2566 		}
2567 
2568 		pdata++;
2569 	}
2570 
2571 	/* Update Global Device Status for results
2572 	 * - Default to no update
2573 	 */
2574 
2575 	presults->device_status = VL53L1_DEVICEERROR_NOUPDATE;
2576 
2577 	/* Check range status
2578 	 * - If device error condition, update device status
2579 	 * - Remove device status from range status output this should
2580 	 * only contain information relating to range data
2581 	 */
2582 
2583 	switch (psys->result__range_status &
2584 			VL53L1_RANGE_STATUS__RANGE_STATUS_MASK) {
2585 
2586 	case VL53L1_DEVICEERROR_VCSELCONTINUITYTESTFAILURE:
2587 	case VL53L1_DEVICEERROR_VCSELWATCHDOGTESTFAILURE:
2588 	case VL53L1_DEVICEERROR_NOVHVVALUEFOUND:
2589 	case VL53L1_DEVICEERROR_USERROICLIP:
2590 	case VL53L1_DEVICEERROR_MULTCLIPFAIL:
2591 
2592 		presults->device_status = (psys->result__range_status &
2593 				VL53L1_RANGE_STATUS__RANGE_STATUS_MASK);
2594 
2595 		presults->data[0].range_status = VL53L1_DEVICEERROR_NOUPDATE;
2596 	break;
2597 
2598 	}
2599 
2600 	LOG_FUNCTION_END(0);
2601 }
2602 
2603 /*
2604  * Configure the GPIO interrupt config, from the given input
2605  */
2606 
VL53L1_set_GPIO_interrupt_config(VL53L1_DEV Dev,VL53L1_GPIO_Interrupt_Mode intr_mode_distance,VL53L1_GPIO_Interrupt_Mode intr_mode_rate,uint8_t intr_new_measure_ready,uint8_t intr_no_target,uint8_t intr_combined_mode,uint16_t thresh_distance_high,uint16_t thresh_distance_low,uint16_t thresh_rate_high,uint16_t thresh_rate_low)2607 VL53L1_Error VL53L1_set_GPIO_interrupt_config(
2608 	VL53L1_DEV                      Dev,
2609 	VL53L1_GPIO_Interrupt_Mode	intr_mode_distance,
2610 	VL53L1_GPIO_Interrupt_Mode	intr_mode_rate,
2611 	uint8_t				intr_new_measure_ready,
2612 	uint8_t				intr_no_target,
2613 	uint8_t				intr_combined_mode,
2614 	uint16_t			thresh_distance_high,
2615 	uint16_t			thresh_distance_low,
2616 	uint16_t			thresh_rate_high,
2617 	uint16_t			thresh_rate_low
2618 	)
2619 {
2620 	VL53L1_Error  status = VL53L1_ERROR_NONE;
2621 
2622 	VL53L1_LLDriverData_t *pdev = VL53L1DevStructGetLLDriverHandle(Dev);
2623 	VL53L1_GPIO_interrupt_config_t *pintconf = &(pdev->gpio_interrupt_config);
2624 
2625 	LOG_FUNCTION_START("");
2626 
2627 	/* update local data structure */
2628 	pintconf->intr_mode_distance = intr_mode_distance;
2629 	pintconf->intr_mode_rate = intr_mode_rate;
2630 	pintconf->intr_new_measure_ready = intr_new_measure_ready;
2631 	pintconf->intr_no_target = intr_no_target;
2632 	pintconf->intr_combined_mode = intr_combined_mode;
2633 	pintconf->threshold_distance_high = thresh_distance_high;
2634 	pintconf->threshold_distance_low = thresh_distance_low;
2635 	pintconf->threshold_rate_high = thresh_rate_high;
2636 	pintconf->threshold_rate_low = thresh_rate_low;
2637 
2638 	/* encoded interrupt config */
2639 	pdev->gen_cfg.system__interrupt_config_gpio =
2640 		VL53L1_encode_GPIO_interrupt_config(pintconf);
2641 
2642 
2643 	/* set thresholds */
2644 	status = VL53L1_set_GPIO_thresholds_from_struct(
2645 			Dev,
2646 			pintconf);
2647 
2648 	LOG_FUNCTION_END(status);
2649 	return status;
2650 }
2651 
2652 /*
2653  * Configure the GPIO interrupt config, from the given structure
2654  */
2655 
VL53L1_set_GPIO_interrupt_config_struct(VL53L1_DEV Dev,VL53L1_GPIO_interrupt_config_t intconf)2656 VL53L1_Error VL53L1_set_GPIO_interrupt_config_struct(
2657 	VL53L1_DEV                      Dev,
2658 	VL53L1_GPIO_interrupt_config_t	intconf)
2659 {
2660 	VL53L1_Error  status = VL53L1_ERROR_NONE;
2661 
2662 	VL53L1_LLDriverData_t *pdev = VL53L1DevStructGetLLDriverHandle(Dev);
2663 	VL53L1_GPIO_interrupt_config_t *pintconf = &(pdev->gpio_interrupt_config);
2664 
2665 	LOG_FUNCTION_START("");
2666 
2667 	 /* using memcpy(dst, src, size in bytes) */
2668 	memcpy(pintconf, &(intconf), sizeof(VL53L1_GPIO_interrupt_config_t));
2669 
2670 	/* encoded interrupt config */
2671 	pdev->gen_cfg.system__interrupt_config_gpio =
2672 		VL53L1_encode_GPIO_interrupt_config(pintconf);
2673 
2674 	/* set thresholds */
2675 	status = VL53L1_set_GPIO_thresholds_from_struct(
2676 			Dev,
2677 			pintconf);
2678 
2679 	LOG_FUNCTION_END(status);
2680 	return status;
2681 }
2682 
2683 /*
2684  * Retrieve GPIO interrupt config structure
2685  */
2686 
VL53L1_get_GPIO_interrupt_config(VL53L1_DEV Dev,VL53L1_GPIO_interrupt_config_t * pintconf)2687 VL53L1_Error VL53L1_get_GPIO_interrupt_config(
2688 	VL53L1_DEV                      Dev,
2689 	VL53L1_GPIO_interrupt_config_t	*pintconf)
2690 {
2691 	VL53L1_Error  status = VL53L1_ERROR_NONE;
2692 
2693 	VL53L1_LLDriverData_t *pdev = VL53L1DevStructGetLLDriverHandle(Dev);
2694 
2695 	LOG_FUNCTION_START("");
2696 
2697 	/*
2698 	 * Decode the system__interrupt_config_gpio register
2699 	 * This makes sure the structure is in line with the register
2700 	 */
2701 	pdev->gpio_interrupt_config = VL53L1_decode_GPIO_interrupt_config(
2702 			pdev->gen_cfg.system__interrupt_config_gpio);
2703 
2704 	/*
2705 	 * Readout the system thresholds
2706 	 */
2707 	pdev->gpio_interrupt_config.threshold_distance_high =
2708 		pdev->dyn_cfg.system__thresh_high;
2709 	pdev->gpio_interrupt_config.threshold_distance_low =
2710 		pdev->dyn_cfg.system__thresh_low;
2711 
2712 	pdev->gpio_interrupt_config.threshold_rate_high =
2713 		pdev->gen_cfg.system__thresh_rate_high;
2714 	pdev->gpio_interrupt_config.threshold_rate_low =
2715 		pdev->gen_cfg.system__thresh_rate_low;
2716 
2717 	if (pintconf == &(pdev->gpio_interrupt_config))	{
2718 		/* Cowardly refusing to copy the same memory locations */
2719 	} else {
2720 
2721 		/* using memcpy(dst, src, size in bytes) */
2722 		memcpy(pintconf, &(pdev->gpio_interrupt_config),
2723 				sizeof(VL53L1_GPIO_interrupt_config_t));
2724 	}
2725 
2726 	LOG_FUNCTION_END(status);
2727 	return status;
2728 }
2729 
VL53L1_set_offset_calibration_mode(VL53L1_DEV Dev,VL53L1_OffsetCalibrationMode offset_cal_mode)2730 VL53L1_Error VL53L1_set_offset_calibration_mode(
2731 	VL53L1_DEV                     Dev,
2732 	VL53L1_OffsetCalibrationMode   offset_cal_mode)
2733 {
2734 
2735 	/*
2736 	 * Sets the offset calibration mode
2737 	 */
2738 
2739 	VL53L1_Error  status = VL53L1_ERROR_NONE;
2740 
2741 	VL53L1_LLDriverData_t *pdev = VL53L1DevStructGetLLDriverHandle(Dev);
2742 
2743 	LOG_FUNCTION_START("");
2744 
2745 	pdev->offset_calibration_mode = offset_cal_mode;
2746 
2747 	LOG_FUNCTION_END(status);
2748 
2749 	return status;
2750 }
2751 
2752 
VL53L1_get_offset_calibration_mode(VL53L1_DEV Dev,VL53L1_OffsetCalibrationMode * poffset_cal_mode)2753 VL53L1_Error VL53L1_get_offset_calibration_mode(
2754 	VL53L1_DEV                     Dev,
2755 	VL53L1_OffsetCalibrationMode  *poffset_cal_mode)
2756 {
2757 
2758 	/*
2759 	 * Gets the offset calibration mode
2760 	 */
2761 
2762 	VL53L1_Error  status = VL53L1_ERROR_NONE;
2763 
2764 	VL53L1_LLDriverData_t *pdev = VL53L1DevStructGetLLDriverHandle(Dev);
2765 
2766 	LOG_FUNCTION_START("");
2767 
2768 	*poffset_cal_mode = pdev->offset_calibration_mode;
2769 
2770 	LOG_FUNCTION_END(status);
2771 
2772 	return status;
2773 }
2774 
2775 
VL53L1_set_offset_correction_mode(VL53L1_DEV Dev,VL53L1_OffsetCorrectionMode offset_cor_mode)2776 VL53L1_Error VL53L1_set_offset_correction_mode(
2777 	VL53L1_DEV                     Dev,
2778 	VL53L1_OffsetCorrectionMode    offset_cor_mode)
2779 {
2780 
2781 	/*
2782 	 * Sets the offset correction mode
2783 	 */
2784 
2785 	VL53L1_Error  status = VL53L1_ERROR_NONE;
2786 
2787 	VL53L1_LLDriverData_t *pdev = VL53L1DevStructGetLLDriverHandle(Dev);
2788 
2789 	LOG_FUNCTION_START("");
2790 
2791 	pdev->offset_correction_mode = offset_cor_mode;
2792 
2793 	LOG_FUNCTION_END(status);
2794 
2795 	return status;
2796 }
2797 
2798 
VL53L1_get_offset_correction_mode(VL53L1_DEV Dev,VL53L1_OffsetCorrectionMode * poffset_cor_mode)2799 VL53L1_Error VL53L1_get_offset_correction_mode(
2800 	VL53L1_DEV                     Dev,
2801 	VL53L1_OffsetCorrectionMode   *poffset_cor_mode)
2802 {
2803 
2804 	/*
2805 	 * Gets the offset correction mode
2806 	 */
2807 
2808 	VL53L1_Error  status = VL53L1_ERROR_NONE;
2809 
2810 	VL53L1_LLDriverData_t *pdev = VL53L1DevStructGetLLDriverHandle(Dev);
2811 
2812 	LOG_FUNCTION_START("");
2813 
2814 	*poffset_cor_mode = pdev->offset_correction_mode;
2815 
2816 	LOG_FUNCTION_END(status);
2817 
2818 	return status;
2819 }
2820 
2821 
2822 /* Start Patch_AddedTuningParms_11761 */
2823 #ifdef VL53L1_DEBUG
VL53L1_get_tuning_debug_data(VL53L1_DEV Dev,VL53L1_tuning_parameters_t * ptun_data)2824 VL53L1_Error VL53L1_get_tuning_debug_data(
2825 	VL53L1_DEV                            Dev,
2826 	VL53L1_tuning_parameters_t           *ptun_data)
2827 {
2828 	/*
2829 	 * Helper function to extract all tuning parm values
2830 	 */
2831 
2832 	VL53L1_Error  status = VL53L1_ERROR_NONE;
2833 
2834 	VL53L1_LLDriverData_t *pdev = VL53L1DevStructGetLLDriverHandle(Dev);
2835 
2836 	LOG_FUNCTION_START("");
2837 
2838 	ptun_data->vl53l1_tuningparm_version =
2839 		pdev->tuning_parms.tp_tuning_parm_version;
2840 
2841 	ptun_data->vl53l1_tuningparm_key_table_version =
2842 		pdev->tuning_parms.tp_tuning_parm_key_table_version;
2843 
2844 
2845 	ptun_data->vl53l1_tuningparm_lld_version =
2846 		pdev->tuning_parms.tp_tuning_parm_lld_version;
2847 
2848 	ptun_data->vl53l1_tuningparm_lite_min_clip_mm =
2849 		pdev->tuning_parms.tp_lite_min_clip;
2850 
2851 	ptun_data->vl53l1_tuningparm_lite_long_sigma_thresh_mm =
2852 		pdev->tuning_parms.tp_lite_long_sigma_thresh_mm;
2853 
2854 	ptun_data->vl53l1_tuningparm_lite_med_sigma_thresh_mm =
2855 		pdev->tuning_parms.tp_lite_med_sigma_thresh_mm;
2856 
2857 	ptun_data->vl53l1_tuningparm_lite_short_sigma_thresh_mm =
2858 		pdev->tuning_parms.tp_lite_short_sigma_thresh_mm;
2859 
2860 	ptun_data->vl53l1_tuningparm_lite_long_min_count_rate_rtn_mcps =
2861 		pdev->tuning_parms.tp_lite_long_min_count_rate_rtn_mcps;
2862 
2863 	ptun_data->vl53l1_tuningparm_lite_med_min_count_rate_rtn_mcps =
2864 		pdev->tuning_parms.tp_lite_med_min_count_rate_rtn_mcps;
2865 
2866 	ptun_data->vl53l1_tuningparm_lite_short_min_count_rate_rtn_mcps =
2867 		pdev->tuning_parms.tp_lite_short_min_count_rate_rtn_mcps;
2868 
2869 	ptun_data->vl53l1_tuningparm_lite_sigma_est_pulse_width =
2870 		pdev->tuning_parms.tp_lite_sigma_est_pulse_width_ns;
2871 
2872 	ptun_data->vl53l1_tuningparm_lite_sigma_est_amb_width_ns =
2873 		pdev->tuning_parms.tp_lite_sigma_est_amb_width_ns;
2874 
2875 	ptun_data->vl53l1_tuningparm_lite_sigma_ref_mm =
2876 		pdev->tuning_parms.tp_lite_sigma_ref_mm;
2877 
2878 	ptun_data->vl53l1_tuningparm_lite_rit_mult =
2879 		pdev->xtalk_cfg.crosstalk_range_ignore_threshold_mult;
2880 
2881 	ptun_data->vl53l1_tuningparm_lite_seed_config =
2882 		pdev->tuning_parms.tp_lite_seed_cfg ;
2883 
2884 	ptun_data->vl53l1_tuningparm_lite_quantifier =
2885 		pdev->tuning_parms.tp_lite_quantifier;
2886 
2887 	ptun_data->vl53l1_tuningparm_lite_first_order_select =
2888 		pdev->tuning_parms.tp_lite_first_order_select;
2889 
2890 	ptun_data->vl53l1_tuningparm_lite_xtalk_margin_kcps =
2891 		pdev->xtalk_cfg.lite_mode_crosstalk_margin_kcps;
2892 
2893 	ptun_data->vl53l1_tuningparm_initial_phase_rtn_lite_long_range =
2894 		pdev->tuning_parms.tp_init_phase_rtn_lite_long;
2895 
2896 	ptun_data->vl53l1_tuningparm_initial_phase_rtn_lite_med_range =
2897 		pdev->tuning_parms.tp_init_phase_rtn_lite_med;
2898 
2899 	ptun_data->vl53l1_tuningparm_initial_phase_rtn_lite_short_range =
2900 		pdev->tuning_parms.tp_init_phase_rtn_lite_short;
2901 
2902 	ptun_data->vl53l1_tuningparm_initial_phase_ref_lite_long_range =
2903 		pdev->tuning_parms.tp_init_phase_ref_lite_long;
2904 
2905 	ptun_data->vl53l1_tuningparm_initial_phase_ref_lite_med_range =
2906 		pdev->tuning_parms.tp_init_phase_ref_lite_med;
2907 
2908 	ptun_data->vl53l1_tuningparm_initial_phase_ref_lite_short_range =
2909 		pdev->tuning_parms.tp_init_phase_ref_lite_short;
2910 
2911 	ptun_data->vl53l1_tuningparm_timed_seed_config =
2912 		pdev->tuning_parms.tp_timed_seed_cfg;
2913 
2914 	ptun_data->vl53l1_tuningparm_vhv_loopbound =
2915 		pdev->stat_nvm.vhv_config__timeout_macrop_loop_bound;
2916 
2917 	ptun_data->vl53l1_tuningparm_refspadchar_device_test_mode =
2918 		pdev->refspadchar.device_test_mode;
2919 
2920 	ptun_data->vl53l1_tuningparm_refspadchar_vcsel_period =
2921 		pdev->refspadchar.vcsel_period;
2922 
2923 	ptun_data->vl53l1_tuningparm_refspadchar_phasecal_timeout_us =
2924 		pdev->refspadchar.timeout_us;
2925 
2926 	ptun_data->vl53l1_tuningparm_refspadchar_target_count_rate_mcps =
2927 		pdev->refspadchar.target_count_rate_mcps;
2928 
2929 	ptun_data->vl53l1_tuningparm_refspadchar_min_countrate_limit_mcps =
2930 		pdev->refspadchar.min_count_rate_limit_mcps;
2931 
2932 	ptun_data->vl53l1_tuningparm_refspadchar_max_countrate_limit_mcps =
2933 		pdev->refspadchar.max_count_rate_limit_mcps;
2934 
2935 	ptun_data->vl53l1_tuningparm_offset_cal_dss_rate_mcps =
2936 		pdev->offsetcal_cfg.dss_config__target_total_rate_mcps;
2937 
2938 	ptun_data->vl53l1_tuningparm_offset_cal_phasecal_timeout_us =
2939 		pdev->offsetcal_cfg.phasecal_config_timeout_us;
2940 
2941 	ptun_data->vl53l1_tuningparm_offset_cal_mm_timeout_us =
2942 		pdev->offsetcal_cfg.mm_config_timeout_us;
2943 
2944 	ptun_data->vl53l1_tuningparm_offset_cal_range_timeout_us =
2945 		pdev->offsetcal_cfg.range_config_timeout_us;
2946 
2947 	ptun_data->vl53l1_tuningparm_offset_cal_pre_samples =
2948 		pdev->offsetcal_cfg.pre_num_of_samples;
2949 
2950 	ptun_data->vl53l1_tuningparm_offset_cal_mm1_samples =
2951 		pdev->offsetcal_cfg.mm1_num_of_samples;
2952 
2953 	ptun_data->vl53l1_tuningparm_offset_cal_mm2_samples =
2954 		pdev->offsetcal_cfg.mm2_num_of_samples;
2955 
2956 	ptun_data->vl53l1_tuningparm_spadmap_vcsel_period =
2957 		pdev->ssc_cfg.vcsel_period;
2958 
2959 	ptun_data->vl53l1_tuningparm_spadmap_vcsel_start =
2960 		pdev->ssc_cfg.vcsel_start;
2961 
2962 	ptun_data->vl53l1_tuningparm_spadmap_rate_limit_mcps =
2963 		pdev->ssc_cfg.rate_limit_mcps;
2964 
2965 	ptun_data->vl53l1_tuningparm_lite_dss_config_target_total_rate_mcps =
2966 		pdev->tuning_parms.tp_dss_target_lite_mcps;
2967 
2968 	ptun_data->vl53l1_tuningparm_timed_dss_config_target_total_rate_mcps =
2969 		pdev->tuning_parms.tp_dss_target_timed_mcps;
2970 
2971 	ptun_data->vl53l1_tuningparm_lite_phasecal_config_timeout_us =
2972 		pdev->tuning_parms.tp_phasecal_timeout_lite_us;
2973 
2974 	ptun_data->vl53l1_tuningparm_timed_phasecal_config_timeout_us =
2975 		pdev->tuning_parms.tp_phasecal_timeout_timed_us;
2976 
2977 	ptun_data->vl53l1_tuningparm_lite_mm_config_timeout_us =
2978 		pdev->tuning_parms.tp_mm_timeout_lite_us;
2979 
2980 	ptun_data->vl53l1_tuningparm_timed_mm_config_timeout_us =
2981 		pdev->tuning_parms.tp_mm_timeout_timed_us;
2982 
2983 	ptun_data->vl53l1_tuningparm_lite_range_config_timeout_us =
2984 		pdev->tuning_parms.tp_range_timeout_lite_us;
2985 
2986 	ptun_data->vl53l1_tuningparm_timed_range_config_timeout_us =
2987 		pdev->tuning_parms.tp_range_timeout_timed_us;
2988 
2989 	ptun_data->vl53l1_tuningparm_lowpowerauto_vhv_loop_bound =
2990 		pdev->low_power_auto_data.vhv_loop_bound;
2991 
2992 	ptun_data->vl53l1_tuningparm_lowpowerauto_mm_config_timeout_us =
2993 		pdev->tuning_parms.tp_mm_timeout_lpa_us;
2994 
2995 	ptun_data->vl53l1_tuningparm_lowpowerauto_range_config_timeout_us =
2996 		pdev->tuning_parms.tp_range_timeout_lpa_us;
2997 
2998 	LOG_FUNCTION_END(status);
2999 
3000 	return status;
3001 }
3002 #endif
3003 
VL53L1_get_tuning_parm(VL53L1_DEV Dev,VL53L1_TuningParms tuning_parm_key,int32_t * ptuning_parm_value)3004 VL53L1_Error VL53L1_get_tuning_parm(
3005 	VL53L1_DEV                     Dev,
3006 	VL53L1_TuningParms             tuning_parm_key,
3007 	int32_t                       *ptuning_parm_value)
3008 {
3009 
3010 	/*
3011 	 * Gets the requested tuning parm value
3012 	 * - Large case statement for returns
3013 	 * - if key does not match, INVALID parm error returned
3014 	 */
3015 
3016 	VL53L1_Error  status = VL53L1_ERROR_NONE;
3017 
3018 	VL53L1_LLDriverData_t *pdev = VL53L1DevStructGetLLDriverHandle(Dev);
3019 
3020 	LOG_FUNCTION_START("");
3021 
3022 	switch (tuning_parm_key) {
3023 
3024 	case VL53L1_TUNINGPARM_VERSION:
3025 		*ptuning_parm_value =
3026 				(int32_t)pdev->tuning_parms.tp_tuning_parm_version;
3027 	break;
3028 	case VL53L1_TUNINGPARM_KEY_TABLE_VERSION:
3029 		*ptuning_parm_value =
3030 				(int32_t)pdev->tuning_parms.tp_tuning_parm_key_table_version;
3031 	break;
3032 	case VL53L1_TUNINGPARM_LLD_VERSION:
3033 		*ptuning_parm_value =
3034 				(int32_t)pdev->tuning_parms.tp_tuning_parm_lld_version;
3035 	break;
3036 	case VL53L1_TUNINGPARM_CONSISTENCY_LITE_PHASE_TOLERANCE:
3037 		*ptuning_parm_value =
3038 				(int32_t)pdev->tuning_parms.tp_consistency_lite_phase_tolerance;
3039 	break;
3040 	case VL53L1_TUNINGPARM_PHASECAL_TARGET:
3041 		*ptuning_parm_value =
3042 				(int32_t)pdev->tuning_parms.tp_phasecal_target;
3043 	break;
3044 	case VL53L1_TUNINGPARM_LITE_CAL_REPEAT_RATE:
3045 		*ptuning_parm_value =
3046 				(int32_t)pdev->tuning_parms.tp_cal_repeat_rate;
3047 	break;
3048 	case VL53L1_TUNINGPARM_LITE_RANGING_GAIN_FACTOR:
3049 		*ptuning_parm_value =
3050 				(int32_t)pdev->gain_cal.standard_ranging_gain_factor;
3051 	break;
3052 	case VL53L1_TUNINGPARM_LITE_MIN_CLIP_MM:
3053 		*ptuning_parm_value =
3054 				(int32_t)pdev->tuning_parms.tp_lite_min_clip;
3055 	break;
3056 	case VL53L1_TUNINGPARM_LITE_LONG_SIGMA_THRESH_MM:
3057 		*ptuning_parm_value =
3058 				(int32_t)pdev->tuning_parms.tp_lite_long_sigma_thresh_mm;
3059 	break;
3060 	case VL53L1_TUNINGPARM_LITE_MED_SIGMA_THRESH_MM:
3061 		*ptuning_parm_value =
3062 				(int32_t)pdev->tuning_parms.tp_lite_med_sigma_thresh_mm;
3063 	break;
3064 	case VL53L1_TUNINGPARM_LITE_SHORT_SIGMA_THRESH_MM:
3065 		*ptuning_parm_value =
3066 				(int32_t)pdev->tuning_parms.tp_lite_short_sigma_thresh_mm;
3067 	break;
3068 	case VL53L1_TUNINGPARM_LITE_LONG_MIN_COUNT_RATE_RTN_MCPS:
3069 		*ptuning_parm_value =
3070 				(int32_t)pdev->tuning_parms.tp_lite_long_min_count_rate_rtn_mcps;
3071 	break;
3072 	case VL53L1_TUNINGPARM_LITE_MED_MIN_COUNT_RATE_RTN_MCPS:
3073 		*ptuning_parm_value =
3074 				(int32_t)pdev->tuning_parms.tp_lite_med_min_count_rate_rtn_mcps;
3075 	break;
3076 	case VL53L1_TUNINGPARM_LITE_SHORT_MIN_COUNT_RATE_RTN_MCPS:
3077 		*ptuning_parm_value =
3078 				(int32_t)pdev->tuning_parms.tp_lite_short_min_count_rate_rtn_mcps;
3079 	break;
3080 	case VL53L1_TUNINGPARM_LITE_SIGMA_EST_PULSE_WIDTH:
3081 		*ptuning_parm_value =
3082 				(int32_t)pdev->tuning_parms.tp_lite_sigma_est_pulse_width_ns;
3083 	break;
3084 	case VL53L1_TUNINGPARM_LITE_SIGMA_EST_AMB_WIDTH_NS:
3085 		*ptuning_parm_value =
3086 				(int32_t)pdev->tuning_parms.tp_lite_sigma_est_amb_width_ns;
3087 	break;
3088 	case VL53L1_TUNINGPARM_LITE_SIGMA_REF_MM:
3089 		*ptuning_parm_value =
3090 				(int32_t)pdev->tuning_parms.tp_lite_sigma_ref_mm;
3091 	break;
3092 	case VL53L1_TUNINGPARM_LITE_RIT_MULT:
3093 		*ptuning_parm_value =
3094 				(int32_t)pdev->xtalk_cfg.crosstalk_range_ignore_threshold_mult;
3095 	break;
3096 	case VL53L1_TUNINGPARM_LITE_SEED_CONFIG:
3097 		*ptuning_parm_value =
3098 				(int32_t)pdev->tuning_parms.tp_lite_seed_cfg ;
3099 	break;
3100 	case VL53L1_TUNINGPARM_LITE_QUANTIFIER:
3101 		*ptuning_parm_value =
3102 				(int32_t)pdev->tuning_parms.tp_lite_quantifier;
3103 	break;
3104 	case VL53L1_TUNINGPARM_LITE_FIRST_ORDER_SELECT:
3105 		*ptuning_parm_value =
3106 				(int32_t)pdev->tuning_parms.tp_lite_first_order_select;
3107 	break;
3108 	case VL53L1_TUNINGPARM_LITE_XTALK_MARGIN_KCPS:
3109 		*ptuning_parm_value =
3110 				(int32_t)pdev->xtalk_cfg.lite_mode_crosstalk_margin_kcps;
3111 	break;
3112 	case VL53L1_TUNINGPARM_INITIAL_PHASE_RTN_LITE_LONG_RANGE:
3113 		*ptuning_parm_value =
3114 				(int32_t)pdev->tuning_parms.tp_init_phase_rtn_lite_long;
3115 	break;
3116 	case VL53L1_TUNINGPARM_INITIAL_PHASE_RTN_LITE_MED_RANGE:
3117 		*ptuning_parm_value =
3118 				(int32_t)pdev->tuning_parms.tp_init_phase_rtn_lite_med;
3119 	break;
3120 	case VL53L1_TUNINGPARM_INITIAL_PHASE_RTN_LITE_SHORT_RANGE:
3121 		*ptuning_parm_value =
3122 				(int32_t)pdev->tuning_parms.tp_init_phase_rtn_lite_short;
3123 	break;
3124 	case VL53L1_TUNINGPARM_INITIAL_PHASE_REF_LITE_LONG_RANGE:
3125 		*ptuning_parm_value =
3126 				(int32_t)pdev->tuning_parms.tp_init_phase_ref_lite_long;
3127 	break;
3128 	case VL53L1_TUNINGPARM_INITIAL_PHASE_REF_LITE_MED_RANGE:
3129 		*ptuning_parm_value =
3130 				(int32_t)pdev->tuning_parms.tp_init_phase_ref_lite_med;
3131 	break;
3132 	case VL53L1_TUNINGPARM_INITIAL_PHASE_REF_LITE_SHORT_RANGE:
3133 		*ptuning_parm_value =
3134 				(int32_t)pdev->tuning_parms.tp_init_phase_ref_lite_short;
3135 	break;
3136 	case VL53L1_TUNINGPARM_TIMED_SEED_CONFIG:
3137 		*ptuning_parm_value =
3138 				(int32_t)pdev->tuning_parms.tp_timed_seed_cfg;
3139 	break;
3140 	case VL53L1_TUNINGPARM_VHV_LOOPBOUND:
3141 		*ptuning_parm_value =
3142 				(int32_t)pdev->stat_nvm.vhv_config__timeout_macrop_loop_bound;
3143 	break;
3144 	case VL53L1_TUNINGPARM_REFSPADCHAR_DEVICE_TEST_MODE:
3145 		*ptuning_parm_value =
3146 				(int32_t)pdev->refspadchar.device_test_mode;
3147 	break;
3148 	case VL53L1_TUNINGPARM_REFSPADCHAR_VCSEL_PERIOD:
3149 		*ptuning_parm_value =
3150 				(int32_t)pdev->refspadchar.vcsel_period;
3151 	break;
3152 	case VL53L1_TUNINGPARM_REFSPADCHAR_PHASECAL_TIMEOUT_US:
3153 		*ptuning_parm_value =
3154 				(int32_t)pdev->refspadchar.timeout_us;
3155 	break;
3156 	case VL53L1_TUNINGPARM_REFSPADCHAR_TARGET_COUNT_RATE_MCPS:
3157 		*ptuning_parm_value =
3158 				(int32_t)pdev->refspadchar.target_count_rate_mcps;
3159 	break;
3160 	case VL53L1_TUNINGPARM_REFSPADCHAR_MIN_COUNTRATE_LIMIT_MCPS:
3161 		*ptuning_parm_value =
3162 				(int32_t)pdev->refspadchar.min_count_rate_limit_mcps;
3163 	break;
3164 	case VL53L1_TUNINGPARM_REFSPADCHAR_MAX_COUNTRATE_LIMIT_MCPS:
3165 		*ptuning_parm_value =
3166 				(int32_t)pdev->refspadchar.max_count_rate_limit_mcps;
3167 	break;
3168 	case VL53L1_TUNINGPARM_OFFSET_CAL_DSS_RATE_MCPS:
3169 		*ptuning_parm_value =
3170 				(int32_t)pdev->offsetcal_cfg.dss_config__target_total_rate_mcps;;
3171 	break;
3172 	case VL53L1_TUNINGPARM_OFFSET_CAL_PHASECAL_TIMEOUT_US:
3173 		*ptuning_parm_value =
3174 				(int32_t)pdev->offsetcal_cfg.phasecal_config_timeout_us;
3175 	break;
3176 	case VL53L1_TUNINGPARM_OFFSET_CAL_MM_TIMEOUT_US:
3177 		*ptuning_parm_value =
3178 				(int32_t)pdev->offsetcal_cfg.mm_config_timeout_us;
3179 	break;
3180 	case VL53L1_TUNINGPARM_OFFSET_CAL_RANGE_TIMEOUT_US:
3181 		*ptuning_parm_value =
3182 				(int32_t)pdev->offsetcal_cfg.range_config_timeout_us;
3183 	break;
3184 	case VL53L1_TUNINGPARM_OFFSET_CAL_PRE_SAMPLES:
3185 		*ptuning_parm_value =
3186 				(int32_t)pdev->offsetcal_cfg.pre_num_of_samples;
3187 	break;
3188 	case VL53L1_TUNINGPARM_OFFSET_CAL_MM1_SAMPLES:
3189 		*ptuning_parm_value =
3190 			(int32_t)pdev->offsetcal_cfg.mm1_num_of_samples;
3191 	break;
3192 	case VL53L1_TUNINGPARM_OFFSET_CAL_MM2_SAMPLES:
3193 		*ptuning_parm_value =
3194 				(int32_t)pdev->offsetcal_cfg.mm2_num_of_samples;
3195 	break;
3196 	case VL53L1_TUNINGPARM_SPADMAP_VCSEL_PERIOD:
3197 		*ptuning_parm_value =
3198 				(int32_t)pdev->ssc_cfg.vcsel_period;
3199 	break;
3200 	case VL53L1_TUNINGPARM_SPADMAP_VCSEL_START:
3201 		*ptuning_parm_value =
3202 				(int32_t)pdev->ssc_cfg.vcsel_start;
3203 	break;
3204 	case VL53L1_TUNINGPARM_SPADMAP_RATE_LIMIT_MCPS:
3205 		*ptuning_parm_value =
3206 				(int32_t)pdev->ssc_cfg.rate_limit_mcps;
3207 	break;
3208 	case VL53L1_TUNINGPARM_LITE_DSS_CONFIG_TARGET_TOTAL_RATE_MCPS:
3209 		*ptuning_parm_value =
3210 				(int32_t)pdev->tuning_parms.tp_dss_target_lite_mcps;
3211 	break;
3212 	case VL53L1_TUNINGPARM_TIMED_DSS_CONFIG_TARGET_TOTAL_RATE_MCPS:
3213 		*ptuning_parm_value =
3214 				(int32_t)pdev->tuning_parms.tp_dss_target_timed_mcps;
3215 	break;
3216 	case VL53L1_TUNINGPARM_LITE_PHASECAL_CONFIG_TIMEOUT_US:
3217 		*ptuning_parm_value =
3218 				(int32_t)pdev->tuning_parms.tp_phasecal_timeout_lite_us;
3219 	break;
3220 	case VL53L1_TUNINGPARM_TIMED_PHASECAL_CONFIG_TIMEOUT_US:
3221 		*ptuning_parm_value =
3222 				(int32_t)pdev->tuning_parms.tp_phasecal_timeout_timed_us;
3223 	break;
3224 	case VL53L1_TUNINGPARM_LITE_MM_CONFIG_TIMEOUT_US:
3225 		*ptuning_parm_value =
3226 				(int32_t)pdev->tuning_parms.tp_mm_timeout_lite_us;
3227 	break;
3228 	case VL53L1_TUNINGPARM_TIMED_MM_CONFIG_TIMEOUT_US:
3229 		*ptuning_parm_value =
3230 				(int32_t)pdev->tuning_parms.tp_mm_timeout_timed_us;
3231 	break;
3232 	case VL53L1_TUNINGPARM_LITE_RANGE_CONFIG_TIMEOUT_US:
3233 		*ptuning_parm_value =
3234 				(int32_t)pdev->tuning_parms.tp_range_timeout_lite_us;
3235 	break;
3236 	case VL53L1_TUNINGPARM_TIMED_RANGE_CONFIG_TIMEOUT_US:
3237 		*ptuning_parm_value =
3238 				(int32_t)pdev->tuning_parms.tp_range_timeout_timed_us;
3239 	break;
3240 	case VL53L1_TUNINGPARM_LOWPOWERAUTO_VHV_LOOP_BOUND:
3241 		*ptuning_parm_value =
3242 				(int32_t)pdev->low_power_auto_data.vhv_loop_bound;
3243 	break;
3244 	case VL53L1_TUNINGPARM_LOWPOWERAUTO_MM_CONFIG_TIMEOUT_US:
3245 		*ptuning_parm_value =
3246 				(int32_t)pdev->tuning_parms.tp_mm_timeout_lpa_us;
3247 	break;
3248 	case VL53L1_TUNINGPARM_LOWPOWERAUTO_RANGE_CONFIG_TIMEOUT_US:
3249 		*ptuning_parm_value =
3250 				(int32_t)pdev->tuning_parms.tp_range_timeout_lpa_us;
3251 	break;
3252 
3253 
3254 	default:
3255 		*ptuning_parm_value = 0x7FFFFFFF;
3256 		status = VL53L1_ERROR_INVALID_PARAMS;
3257 	break;
3258 
3259 	}
3260 
3261 	LOG_FUNCTION_END(status);
3262 
3263 	return status;
3264 }
3265 
VL53L1_set_tuning_parm(VL53L1_DEV Dev,VL53L1_TuningParms tuning_parm_key,int32_t tuning_parm_value)3266 VL53L1_Error VL53L1_set_tuning_parm(
3267 	VL53L1_DEV            Dev,
3268 	VL53L1_TuningParms    tuning_parm_key,
3269 	int32_t               tuning_parm_value)
3270 {
3271 
3272 	/*
3273 	 * Sets the requested tuning parm value
3274 	 * - Large case statement for set value
3275 	 * - if key does not match, INVALID parm error returned
3276 	 */
3277 
3278 	VL53L1_Error  status = VL53L1_ERROR_NONE;
3279 
3280 	VL53L1_LLDriverData_t *pdev = VL53L1DevStructGetLLDriverHandle(Dev);
3281 
3282 	LOG_FUNCTION_START("");
3283 
3284 	switch (tuning_parm_key) {
3285 
3286 	case VL53L1_TUNINGPARM_VERSION:
3287 		pdev->tuning_parms.tp_tuning_parm_version =
3288 				(uint16_t)tuning_parm_value;
3289 	break;
3290 	case VL53L1_TUNINGPARM_KEY_TABLE_VERSION:
3291 		pdev->tuning_parms.tp_tuning_parm_key_table_version =
3292 					(uint16_t)tuning_parm_value;
3293 
3294 		/* Perform Key Table Check
3295 		 *
3296 		 * - If does not match default, key table
3297 		 * format does not match tuning file,
3298 		 * error should be thrown
3299 		 *
3300 		 */
3301 
3302 		if ((uint16_t)tuning_parm_value
3303 				!= VL53L1_TUNINGPARM_KEY_TABLE_VERSION_DEFAULT) {
3304 			status = VL53L1_ERROR_TUNING_PARM_KEY_MISMATCH;
3305 		}
3306 	break;
3307 	case VL53L1_TUNINGPARM_LLD_VERSION:
3308 		pdev->tuning_parms.tp_tuning_parm_lld_version =
3309 				(uint16_t)tuning_parm_value;
3310 	break;
3311 	case VL53L1_TUNINGPARM_CONSISTENCY_LITE_PHASE_TOLERANCE:
3312 		pdev->tuning_parms.tp_consistency_lite_phase_tolerance =
3313 				(uint8_t)tuning_parm_value;
3314 	break;
3315 	case VL53L1_TUNINGPARM_PHASECAL_TARGET:
3316 		pdev->tuning_parms.tp_phasecal_target =
3317 				(uint8_t)tuning_parm_value;
3318 	break;
3319 	case VL53L1_TUNINGPARM_LITE_CAL_REPEAT_RATE:
3320 		pdev->tuning_parms.tp_cal_repeat_rate =
3321 				(uint16_t)tuning_parm_value;
3322 	break;
3323 	case VL53L1_TUNINGPARM_LITE_RANGING_GAIN_FACTOR:
3324 		pdev->gain_cal.standard_ranging_gain_factor =
3325 				(uint16_t)tuning_parm_value;
3326 	break;
3327 	case VL53L1_TUNINGPARM_LITE_MIN_CLIP_MM:
3328 		pdev->tuning_parms.tp_lite_min_clip =
3329 				(uint8_t)tuning_parm_value;
3330 	break;
3331 	case VL53L1_TUNINGPARM_LITE_LONG_SIGMA_THRESH_MM:
3332 		pdev->tuning_parms.tp_lite_long_sigma_thresh_mm =
3333 				(uint16_t)tuning_parm_value;
3334 	break;
3335 	case VL53L1_TUNINGPARM_LITE_MED_SIGMA_THRESH_MM:
3336 		pdev->tuning_parms.tp_lite_med_sigma_thresh_mm =
3337 				(uint16_t)tuning_parm_value;
3338 	break;
3339 	case VL53L1_TUNINGPARM_LITE_SHORT_SIGMA_THRESH_MM:
3340 		pdev->tuning_parms.tp_lite_short_sigma_thresh_mm =
3341 				(uint16_t)tuning_parm_value;
3342 	break;
3343 	case VL53L1_TUNINGPARM_LITE_LONG_MIN_COUNT_RATE_RTN_MCPS:
3344 		pdev->tuning_parms.tp_lite_long_min_count_rate_rtn_mcps =
3345 				(uint16_t)tuning_parm_value;
3346 	break;
3347 	case VL53L1_TUNINGPARM_LITE_MED_MIN_COUNT_RATE_RTN_MCPS:
3348 		pdev->tuning_parms.tp_lite_med_min_count_rate_rtn_mcps =
3349 				(uint16_t)tuning_parm_value;
3350 	break;
3351 	case VL53L1_TUNINGPARM_LITE_SHORT_MIN_COUNT_RATE_RTN_MCPS:
3352 		pdev->tuning_parms.tp_lite_short_min_count_rate_rtn_mcps =
3353 				(uint16_t)tuning_parm_value;
3354 	break;
3355 	case VL53L1_TUNINGPARM_LITE_SIGMA_EST_PULSE_WIDTH:
3356 		pdev->tuning_parms.tp_lite_sigma_est_pulse_width_ns =
3357 				(uint8_t)tuning_parm_value;
3358 	break;
3359 	case VL53L1_TUNINGPARM_LITE_SIGMA_EST_AMB_WIDTH_NS:
3360 		pdev->tuning_parms.tp_lite_sigma_est_amb_width_ns =
3361 				(uint8_t)tuning_parm_value;
3362 	break;
3363 	case VL53L1_TUNINGPARM_LITE_SIGMA_REF_MM:
3364 		pdev->tuning_parms.tp_lite_sigma_ref_mm =
3365 				(uint8_t)tuning_parm_value;
3366 	break;
3367 	case VL53L1_TUNINGPARM_LITE_RIT_MULT:
3368 		pdev->xtalk_cfg.crosstalk_range_ignore_threshold_mult =
3369 				(uint8_t)tuning_parm_value;
3370 	break;
3371 	case VL53L1_TUNINGPARM_LITE_SEED_CONFIG:
3372 		pdev->tuning_parms.tp_lite_seed_cfg =
3373 				(uint8_t)tuning_parm_value;
3374 	break;
3375 	case VL53L1_TUNINGPARM_LITE_QUANTIFIER:
3376 		pdev->tuning_parms.tp_lite_quantifier =
3377 				(uint8_t)tuning_parm_value;
3378 	break;
3379 	case VL53L1_TUNINGPARM_LITE_FIRST_ORDER_SELECT:
3380 		pdev->tuning_parms.tp_lite_first_order_select =
3381 				(uint8_t)tuning_parm_value;
3382 	break;
3383 	case VL53L1_TUNINGPARM_LITE_XTALK_MARGIN_KCPS:
3384 		pdev->xtalk_cfg.lite_mode_crosstalk_margin_kcps =
3385 				(int16_t)tuning_parm_value;
3386 	break;
3387 	case VL53L1_TUNINGPARM_INITIAL_PHASE_RTN_LITE_LONG_RANGE:
3388 		pdev->tuning_parms.tp_init_phase_rtn_lite_long =
3389 				(uint8_t)tuning_parm_value;
3390 	break;
3391 	case VL53L1_TUNINGPARM_INITIAL_PHASE_RTN_LITE_MED_RANGE:
3392 		pdev->tuning_parms.tp_init_phase_rtn_lite_med =
3393 				(uint8_t)tuning_parm_value;
3394 	break;
3395 	case VL53L1_TUNINGPARM_INITIAL_PHASE_RTN_LITE_SHORT_RANGE:
3396 		pdev->tuning_parms.tp_init_phase_rtn_lite_short =
3397 				(uint8_t)tuning_parm_value;
3398 	break;
3399 	case VL53L1_TUNINGPARM_INITIAL_PHASE_REF_LITE_LONG_RANGE:
3400 		pdev->tuning_parms.tp_init_phase_ref_lite_long =
3401 				(uint8_t)tuning_parm_value;
3402 	break;
3403 	case VL53L1_TUNINGPARM_INITIAL_PHASE_REF_LITE_MED_RANGE:
3404 		pdev->tuning_parms.tp_init_phase_ref_lite_med =
3405 				(uint8_t)tuning_parm_value;
3406 	break;
3407 	case VL53L1_TUNINGPARM_INITIAL_PHASE_REF_LITE_SHORT_RANGE:
3408 		pdev->tuning_parms.tp_init_phase_ref_lite_short =
3409 				(uint8_t)tuning_parm_value;
3410 	break;
3411 	case VL53L1_TUNINGPARM_TIMED_SEED_CONFIG:
3412 		pdev->tuning_parms.tp_timed_seed_cfg =
3413 				(uint8_t)tuning_parm_value;
3414 	break;
3415 	case VL53L1_TUNINGPARM_VHV_LOOPBOUND:
3416 		pdev->stat_nvm.vhv_config__timeout_macrop_loop_bound =
3417 				(uint8_t)tuning_parm_value;
3418 	break;
3419 	case VL53L1_TUNINGPARM_REFSPADCHAR_DEVICE_TEST_MODE:
3420 		pdev->refspadchar.device_test_mode =
3421 				(uint8_t)tuning_parm_value;
3422 	break;
3423 	case VL53L1_TUNINGPARM_REFSPADCHAR_VCSEL_PERIOD:
3424 		pdev->refspadchar.vcsel_period =
3425 				(uint8_t)tuning_parm_value;
3426 	break;
3427 	case VL53L1_TUNINGPARM_REFSPADCHAR_PHASECAL_TIMEOUT_US:
3428 		pdev->refspadchar.timeout_us =
3429 				(uint32_t)tuning_parm_value;
3430 	break;
3431 	case VL53L1_TUNINGPARM_REFSPADCHAR_TARGET_COUNT_RATE_MCPS:
3432 		pdev->refspadchar.target_count_rate_mcps =
3433 				(uint16_t)tuning_parm_value;
3434 	break;
3435 	case VL53L1_TUNINGPARM_REFSPADCHAR_MIN_COUNTRATE_LIMIT_MCPS:
3436 		pdev->refspadchar.min_count_rate_limit_mcps =
3437 				(uint16_t)tuning_parm_value;
3438 	break;
3439 	case VL53L1_TUNINGPARM_REFSPADCHAR_MAX_COUNTRATE_LIMIT_MCPS:
3440 		pdev->refspadchar.max_count_rate_limit_mcps =
3441 				(uint16_t)tuning_parm_value;
3442 	break;
3443 	case VL53L1_TUNINGPARM_OFFSET_CAL_DSS_RATE_MCPS:
3444 		pdev->offsetcal_cfg.dss_config__target_total_rate_mcps =
3445 				(uint16_t)tuning_parm_value;
3446 	break;
3447 	case VL53L1_TUNINGPARM_OFFSET_CAL_PHASECAL_TIMEOUT_US:
3448 		pdev->offsetcal_cfg.phasecal_config_timeout_us =
3449 				(uint32_t)tuning_parm_value;
3450 	break;
3451 	case VL53L1_TUNINGPARM_OFFSET_CAL_MM_TIMEOUT_US:
3452 		pdev->offsetcal_cfg.mm_config_timeout_us =
3453 				(uint32_t)tuning_parm_value;
3454 	break;
3455 	case VL53L1_TUNINGPARM_OFFSET_CAL_RANGE_TIMEOUT_US:
3456 		pdev->offsetcal_cfg.range_config_timeout_us =
3457 				(uint32_t)tuning_parm_value;
3458 	break;
3459 	case VL53L1_TUNINGPARM_OFFSET_CAL_PRE_SAMPLES:
3460 		pdev->offsetcal_cfg.pre_num_of_samples =
3461 				(uint8_t)tuning_parm_value;
3462 	break;
3463 	case VL53L1_TUNINGPARM_OFFSET_CAL_MM1_SAMPLES:
3464 		pdev->offsetcal_cfg.mm1_num_of_samples =
3465 				(uint8_t)tuning_parm_value;
3466 	break;
3467 	case VL53L1_TUNINGPARM_OFFSET_CAL_MM2_SAMPLES:
3468 		pdev->offsetcal_cfg.mm2_num_of_samples =
3469 				(uint8_t)tuning_parm_value;
3470 	break;
3471 	case VL53L1_TUNINGPARM_SPADMAP_VCSEL_PERIOD:
3472 		pdev->ssc_cfg.vcsel_period =
3473 				(uint8_t)tuning_parm_value;
3474 	break;
3475 	case VL53L1_TUNINGPARM_SPADMAP_VCSEL_START:
3476 		pdev->ssc_cfg.vcsel_start =
3477 				(uint8_t)tuning_parm_value;
3478 	break;
3479 	case VL53L1_TUNINGPARM_SPADMAP_RATE_LIMIT_MCPS:
3480 		pdev->ssc_cfg.rate_limit_mcps =
3481 				(uint16_t)tuning_parm_value;
3482 	break;
3483 	case VL53L1_TUNINGPARM_LITE_DSS_CONFIG_TARGET_TOTAL_RATE_MCPS:
3484 		pdev->tuning_parms.tp_dss_target_lite_mcps =
3485 			(uint16_t)tuning_parm_value;
3486 	break;
3487 	case VL53L1_TUNINGPARM_TIMED_DSS_CONFIG_TARGET_TOTAL_RATE_MCPS:
3488 		pdev->tuning_parms.tp_dss_target_timed_mcps =
3489 			(uint16_t)tuning_parm_value;
3490 	break;
3491 	case VL53L1_TUNINGPARM_LITE_PHASECAL_CONFIG_TIMEOUT_US:
3492 		pdev->tuning_parms.tp_phasecal_timeout_lite_us =
3493 			(uint32_t)tuning_parm_value;
3494 	break;
3495 	case VL53L1_TUNINGPARM_TIMED_PHASECAL_CONFIG_TIMEOUT_US:
3496 		pdev->tuning_parms.tp_phasecal_timeout_timed_us =
3497 			(uint32_t)tuning_parm_value;
3498 	break;
3499 	case VL53L1_TUNINGPARM_LITE_MM_CONFIG_TIMEOUT_US:
3500 		pdev->tuning_parms.tp_mm_timeout_lite_us =
3501 			(uint32_t)tuning_parm_value;
3502 	break;
3503 	case VL53L1_TUNINGPARM_TIMED_MM_CONFIG_TIMEOUT_US:
3504 		pdev->tuning_parms.tp_mm_timeout_timed_us =
3505 			(uint32_t)tuning_parm_value;
3506 	break;
3507 	case VL53L1_TUNINGPARM_LITE_RANGE_CONFIG_TIMEOUT_US:
3508 		pdev->tuning_parms.tp_range_timeout_lite_us =
3509 			(uint32_t)tuning_parm_value;
3510 	break;
3511 	case VL53L1_TUNINGPARM_TIMED_RANGE_CONFIG_TIMEOUT_US:
3512 		pdev->tuning_parms.tp_range_timeout_timed_us =
3513 			(uint32_t)tuning_parm_value;
3514 	break;
3515 	case VL53L1_TUNINGPARM_LOWPOWERAUTO_VHV_LOOP_BOUND:
3516 		pdev->low_power_auto_data.vhv_loop_bound =
3517 			(uint8_t)tuning_parm_value;
3518 	break;
3519 	case VL53L1_TUNINGPARM_LOWPOWERAUTO_MM_CONFIG_TIMEOUT_US:
3520 		pdev->tuning_parms.tp_mm_timeout_lpa_us =
3521 			(uint32_t)tuning_parm_value;
3522 	break;
3523 	case VL53L1_TUNINGPARM_LOWPOWERAUTO_RANGE_CONFIG_TIMEOUT_US:
3524 		pdev->tuning_parms.tp_range_timeout_lpa_us =
3525 			(uint32_t)tuning_parm_value;
3526 	break;
3527 
3528 
3529 	default:
3530 		status = VL53L1_ERROR_INVALID_PARAMS;
3531 	break;
3532 
3533 	}
3534 
3535 	LOG_FUNCTION_END(status);
3536 
3537 	return status;
3538 }
3539 
3540 /* End Patch_AddedTuningParms_11761 */
3541