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 	}
2354 	/* End Patch_LowPowerAutoMode */
2355 
2356 	/* copy current state into results */
2357 
2358 	presults->cfg_device_state = pdev->ll_state.cfg_device_state;
2359 	presults->rd_device_state  = pdev->ll_state.rd_device_state;
2360 
2361 	/* copy internal structure to supplied output pointer */
2362 
2363 	memcpy(
2364 		prange_results,
2365 		presults,
2366 		sizeof(VL53L1_range_results_t));
2367 
2368 	/*
2369 	 * Check LL driver and Device are in Sync
2370 	 * If not an error is raised
2371 	 */
2372 
2373 	if (status == VL53L1_ERROR_NONE)
2374 		status = VL53L1_check_ll_driver_rd_state(Dev);
2375 
2376 #ifdef VL53L1_LOG_ENABLE
2377 	if (status == VL53L1_ERROR_NONE)
2378 		VL53L1_print_range_results(
2379 			presults,
2380 			"get_device_results():pdev->llresults.range_results.",
2381 			VL53L1_TRACE_MODULE_RANGE_RESULTS_DATA);
2382 #endif
2383 
2384 	LOG_FUNCTION_END(status);
2385 
2386 	return status;
2387 }
2388 
2389 
VL53L1_clear_interrupt_and_enable_next_range(VL53L1_DEV Dev,uint8_t measurement_mode)2390 VL53L1_Error VL53L1_clear_interrupt_and_enable_next_range(
2391 	VL53L1_DEV        Dev,
2392 	uint8_t           measurement_mode)
2393 {
2394 
2395 	/*
2396 	 * Enable next range by sending handshake which
2397 	 * clears the interrupt
2398 	 */
2399 
2400 	VL53L1_Error status = VL53L1_ERROR_NONE;
2401 
2402 	LOG_FUNCTION_START("");
2403 
2404 	/* Dynamic Management */
2405 	/* Current results analysis and generate next settings */
2406 
2407 
2408 	/* Dynamic GPH Management     */
2409 	/* Setup GPH absorption point and config values for next measurement */
2410 
2411 	/* Update GPH registers, clear interrupt and set measurement mode */
2412 
2413 	if (status == VL53L1_ERROR_NONE) /*lint !e774 always true*/
2414 		status = VL53L1_init_and_start_range(
2415 					Dev,
2416 					measurement_mode,
2417 					VL53L1_DEVICECONFIGLEVEL_GENERAL_ONWARDS);
2418 
2419 	LOG_FUNCTION_END(status);
2420 
2421 	return status;
2422 }
2423 
2424 
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)2425 void VL53L1_copy_sys_and_core_results_to_range_results(
2426 	int32_t                           gain_factor,
2427 	VL53L1_system_results_t          *psys,
2428 	VL53L1_core_results_t            *pcore,
2429 	VL53L1_range_results_t           *presults)
2430 {
2431 	uint8_t  i = 0;
2432 
2433 	VL53L1_range_data_t *pdata;
2434 	int32_t range_mm = 0;
2435 	uint32_t tmpu32 = 0;
2436 
2437 	LOG_FUNCTION_START("");
2438 
2439 	/* copy results */
2440 
2441 	presults->stream_count    = psys->result__stream_count;
2442 
2443 	pdata = &(presults->data[0]);
2444 
2445 	for (i = 0 ; i < 2 ; i++) {
2446 
2447 		pdata->range_id     = i;
2448 		pdata->time_stamp   = 0;
2449 
2450 		if ((psys->result__stream_count == 0) &&
2451 			((psys->result__range_status & VL53L1_RANGE_STATUS__RANGE_STATUS_MASK) ==
2452 			VL53L1_DEVICEERROR_RANGECOMPLETE)) {
2453 			pdata->range_status = VL53L1_DEVICEERROR_RANGECOMPLETE_NO_WRAP_CHECK;
2454 		} else {
2455 			pdata->range_status =
2456 					psys->result__range_status & VL53L1_RANGE_STATUS__RANGE_STATUS_MASK;
2457 		}
2458 
2459 		switch (i) {
2460 
2461 		case 0:
2462 
2463 			if (psys->result__report_status == VL53L1_DEVICEREPORTSTATUS_MM1)
2464 				pdata->actual_effective_spads =
2465 					psys->result__mm_inner_actual_effective_spads_sd0;
2466 			else if (psys->result__report_status == VL53L1_DEVICEREPORTSTATUS_MM2)
2467 				pdata->actual_effective_spads =
2468 						psys->result__mm_outer_actual_effective_spads_sd0;
2469 			else
2470 				pdata->actual_effective_spads =
2471 					psys->result__dss_actual_effective_spads_sd0;
2472 
2473 			pdata->peak_signal_count_rate_mcps =
2474 				psys->result__peak_signal_count_rate_crosstalk_corrected_mcps_sd0;
2475 			pdata->avg_signal_count_rate_mcps =
2476 				psys->result__avg_signal_count_rate_mcps_sd0;
2477 			pdata->ambient_count_rate_mcps =
2478 				psys->result__ambient_count_rate_mcps_sd0;
2479 
2480 			/* Start Patch_SigmaEstimateAccuracyImprovement */
2481 
2482 			/* shift up sigma estimate to 7 bit fractional and clip to 9 bit int */
2483 			tmpu32 = ((uint32_t)psys->result__sigma_sd0 << 5);
2484 			if (tmpu32 > 0xFFFF) {
2485 				tmpu32 = 0xFFFF;
2486 			}
2487 			pdata->sigma_mm = (uint16_t)tmpu32;
2488 
2489 			/* End Patch_SigmaEstimateAccuracyImprovement */
2490 
2491 			pdata->median_phase =
2492 				psys->result__phase_sd0;
2493 
2494 			range_mm =
2495 				(int32_t)psys->result__final_crosstalk_corrected_range_mm_sd0;
2496 
2497 			/* apply correction gain */
2498 			range_mm *= gain_factor;
2499 			range_mm += 0x0400;
2500 			range_mm /= 0x0800;
2501 
2502 			pdata->median_range_mm = (int16_t)range_mm;
2503 
2504 			pdata->ranging_total_events =
2505 				pcore->result_core__ranging_total_events_sd0;
2506 			pdata->signal_total_events =
2507 				pcore->result_core__signal_total_events_sd0;
2508 			pdata->total_periods_elapsed =
2509 				pcore->result_core__total_periods_elapsed_sd0;
2510 			pdata->ambient_window_events =
2511 				pcore->result_core__ambient_window_events_sd0;
2512 
2513 			break;
2514 		case 1:
2515 
2516 			pdata->actual_effective_spads =
2517 				psys->result__dss_actual_effective_spads_sd1;
2518 			pdata->peak_signal_count_rate_mcps =
2519 				psys->result__peak_signal_count_rate_mcps_sd1;
2520 			pdata->avg_signal_count_rate_mcps =
2521 				0xFFFF;
2522 			pdata->ambient_count_rate_mcps =
2523 				psys->result__ambient_count_rate_mcps_sd1;
2524 
2525 			/* Start Patch_SigmaEstimateAccuracyImprovement */
2526 
2527 			/* shift up sigma estimate to 7 bit fractional and clip to 9 bit int */
2528 			tmpu32 = ((uint32_t)psys->result__sigma_sd1 << 5);
2529 			if (tmpu32 > 0xFFFF) {
2530 				tmpu32 = 0xFFFF;
2531 			}
2532 			pdata->sigma_mm = (uint16_t)tmpu32;
2533 
2534 			/* End Patch_SigmaEstimateAccuracyImprovement */
2535 
2536 			pdata->median_phase =
2537 				psys->result__phase_sd1;
2538 
2539 			range_mm =
2540 				(int32_t)psys->result__final_crosstalk_corrected_range_mm_sd1;
2541 
2542 			/* apply correction gain */
2543 			range_mm *= gain_factor;
2544 			range_mm += 0x0400;
2545 			range_mm /= 0x0800;
2546 
2547 			pdata->median_range_mm = (int16_t)range_mm;
2548 
2549 			pdata->ranging_total_events =
2550 				pcore->result_core__ranging_total_events_sd1;
2551 			pdata->signal_total_events =
2552 				pcore->result_core__signal_total_events_sd1;
2553 			pdata->total_periods_elapsed  =
2554 				pcore->result_core__total_periods_elapsed_sd1;
2555 			pdata->ambient_window_events =
2556 				pcore->result_core__ambient_window_events_sd1;
2557 
2558 			break;
2559 		}
2560 
2561 		pdata++;
2562 	}
2563 
2564 	/* Update Global Device Status for results
2565 	 * - Default to no update
2566 	 */
2567 
2568 	presults->device_status = VL53L1_DEVICEERROR_NOUPDATE;
2569 
2570 	/* Check range status
2571 	 * - If device error condition, update device status
2572 	 * - Remove device status from range status output this should
2573 	 * only contain information relating to range data
2574 	 */
2575 
2576 	switch (psys->result__range_status &
2577 			VL53L1_RANGE_STATUS__RANGE_STATUS_MASK) {
2578 
2579 	case VL53L1_DEVICEERROR_VCSELCONTINUITYTESTFAILURE:
2580 	case VL53L1_DEVICEERROR_VCSELWATCHDOGTESTFAILURE:
2581 	case VL53L1_DEVICEERROR_NOVHVVALUEFOUND:
2582 	case VL53L1_DEVICEERROR_USERROICLIP:
2583 	case VL53L1_DEVICEERROR_MULTCLIPFAIL:
2584 
2585 		presults->device_status = (psys->result__range_status &
2586 				VL53L1_RANGE_STATUS__RANGE_STATUS_MASK);
2587 
2588 		presults->data[0].range_status = VL53L1_DEVICEERROR_NOUPDATE;
2589 	break;
2590 
2591 	}
2592 
2593 	LOG_FUNCTION_END(0);
2594 }
2595 
2596 /*
2597  * Configure the GPIO interrupt config, from the given input
2598  */
2599 
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)2600 VL53L1_Error VL53L1_set_GPIO_interrupt_config(
2601 	VL53L1_DEV                      Dev,
2602 	VL53L1_GPIO_Interrupt_Mode	intr_mode_distance,
2603 	VL53L1_GPIO_Interrupt_Mode	intr_mode_rate,
2604 	uint8_t				intr_new_measure_ready,
2605 	uint8_t				intr_no_target,
2606 	uint8_t				intr_combined_mode,
2607 	uint16_t			thresh_distance_high,
2608 	uint16_t			thresh_distance_low,
2609 	uint16_t			thresh_rate_high,
2610 	uint16_t			thresh_rate_low
2611 	)
2612 {
2613 	VL53L1_Error  status = VL53L1_ERROR_NONE;
2614 
2615 	VL53L1_LLDriverData_t *pdev = VL53L1DevStructGetLLDriverHandle(Dev);
2616 	VL53L1_GPIO_interrupt_config_t *pintconf = &(pdev->gpio_interrupt_config);
2617 
2618 	LOG_FUNCTION_START("");
2619 
2620 	/* update local data structure */
2621 	pintconf->intr_mode_distance = intr_mode_distance;
2622 	pintconf->intr_mode_rate = intr_mode_rate;
2623 	pintconf->intr_new_measure_ready = intr_new_measure_ready;
2624 	pintconf->intr_no_target = intr_no_target;
2625 	pintconf->intr_combined_mode = intr_combined_mode;
2626 	pintconf->threshold_distance_high = thresh_distance_high;
2627 	pintconf->threshold_distance_low = thresh_distance_low;
2628 	pintconf->threshold_rate_high = thresh_rate_high;
2629 	pintconf->threshold_rate_low = thresh_rate_low;
2630 
2631 	/* encoded interrupt config */
2632 	pdev->gen_cfg.system__interrupt_config_gpio =
2633 		VL53L1_encode_GPIO_interrupt_config(pintconf);
2634 
2635 
2636 	/* set thresholds */
2637 	status = VL53L1_set_GPIO_thresholds_from_struct(
2638 			Dev,
2639 			pintconf);
2640 
2641 	LOG_FUNCTION_END(status);
2642 	return status;
2643 }
2644 
2645 /*
2646  * Configure the GPIO interrupt config, from the given structure
2647  */
2648 
VL53L1_set_GPIO_interrupt_config_struct(VL53L1_DEV Dev,VL53L1_GPIO_interrupt_config_t intconf)2649 VL53L1_Error VL53L1_set_GPIO_interrupt_config_struct(
2650 	VL53L1_DEV                      Dev,
2651 	VL53L1_GPIO_interrupt_config_t	intconf)
2652 {
2653 	VL53L1_Error  status = VL53L1_ERROR_NONE;
2654 
2655 	VL53L1_LLDriverData_t *pdev = VL53L1DevStructGetLLDriverHandle(Dev);
2656 	VL53L1_GPIO_interrupt_config_t *pintconf = &(pdev->gpio_interrupt_config);
2657 
2658 	LOG_FUNCTION_START("");
2659 
2660 	 /* using memcpy(dst, src, size in bytes) */
2661 	memcpy(pintconf, &(intconf), sizeof(VL53L1_GPIO_interrupt_config_t));
2662 
2663 	/* encoded interrupt config */
2664 	pdev->gen_cfg.system__interrupt_config_gpio =
2665 		VL53L1_encode_GPIO_interrupt_config(pintconf);
2666 
2667 	/* set thresholds */
2668 	status = VL53L1_set_GPIO_thresholds_from_struct(
2669 			Dev,
2670 			pintconf);
2671 
2672 	LOG_FUNCTION_END(status);
2673 	return status;
2674 }
2675 
2676 /*
2677  * Retrieve GPIO interrupt config structure
2678  */
2679 
VL53L1_get_GPIO_interrupt_config(VL53L1_DEV Dev,VL53L1_GPIO_interrupt_config_t * pintconf)2680 VL53L1_Error VL53L1_get_GPIO_interrupt_config(
2681 	VL53L1_DEV                      Dev,
2682 	VL53L1_GPIO_interrupt_config_t	*pintconf)
2683 {
2684 	VL53L1_Error  status = VL53L1_ERROR_NONE;
2685 
2686 	VL53L1_LLDriverData_t *pdev = VL53L1DevStructGetLLDriverHandle(Dev);
2687 
2688 	LOG_FUNCTION_START("");
2689 
2690 	/*
2691 	 * Decode the system__interrupt_config_gpio register
2692 	 * This makes sure the structure is in line with the register
2693 	 */
2694 	pdev->gpio_interrupt_config = VL53L1_decode_GPIO_interrupt_config(
2695 			pdev->gen_cfg.system__interrupt_config_gpio);
2696 
2697 	/*
2698 	 * Readout the system thresholds
2699 	 */
2700 	pdev->gpio_interrupt_config.threshold_distance_high =
2701 		pdev->dyn_cfg.system__thresh_high;
2702 	pdev->gpio_interrupt_config.threshold_distance_low =
2703 		pdev->dyn_cfg.system__thresh_low;
2704 
2705 	pdev->gpio_interrupt_config.threshold_rate_high =
2706 		pdev->gen_cfg.system__thresh_rate_high;
2707 	pdev->gpio_interrupt_config.threshold_rate_low =
2708 		pdev->gen_cfg.system__thresh_rate_low;
2709 
2710 	if (pintconf == &(pdev->gpio_interrupt_config))	{
2711 		/* Cowardly refusing to copy the same memory locations */
2712 	} else {
2713 
2714 		/* using memcpy(dst, src, size in bytes) */
2715 		memcpy(pintconf, &(pdev->gpio_interrupt_config),
2716 				sizeof(VL53L1_GPIO_interrupt_config_t));
2717 	}
2718 
2719 	LOG_FUNCTION_END(status);
2720 	return status;
2721 }
2722 
VL53L1_set_offset_calibration_mode(VL53L1_DEV Dev,VL53L1_OffsetCalibrationMode offset_cal_mode)2723 VL53L1_Error VL53L1_set_offset_calibration_mode(
2724 	VL53L1_DEV                     Dev,
2725 	VL53L1_OffsetCalibrationMode   offset_cal_mode)
2726 {
2727 
2728 	/*
2729 	 * Sets the offset calibration mode
2730 	 */
2731 
2732 	VL53L1_Error  status = VL53L1_ERROR_NONE;
2733 
2734 	VL53L1_LLDriverData_t *pdev = VL53L1DevStructGetLLDriverHandle(Dev);
2735 
2736 	LOG_FUNCTION_START("");
2737 
2738 	pdev->offset_calibration_mode = offset_cal_mode;
2739 
2740 	LOG_FUNCTION_END(status);
2741 
2742 	return status;
2743 }
2744 
2745 
VL53L1_get_offset_calibration_mode(VL53L1_DEV Dev,VL53L1_OffsetCalibrationMode * poffset_cal_mode)2746 VL53L1_Error VL53L1_get_offset_calibration_mode(
2747 	VL53L1_DEV                     Dev,
2748 	VL53L1_OffsetCalibrationMode  *poffset_cal_mode)
2749 {
2750 
2751 	/*
2752 	 * Gets the offset calibration mode
2753 	 */
2754 
2755 	VL53L1_Error  status = VL53L1_ERROR_NONE;
2756 
2757 	VL53L1_LLDriverData_t *pdev = VL53L1DevStructGetLLDriverHandle(Dev);
2758 
2759 	LOG_FUNCTION_START("");
2760 
2761 	*poffset_cal_mode = pdev->offset_calibration_mode;
2762 
2763 	LOG_FUNCTION_END(status);
2764 
2765 	return status;
2766 }
2767 
2768 
VL53L1_set_offset_correction_mode(VL53L1_DEV Dev,VL53L1_OffsetCorrectionMode offset_cor_mode)2769 VL53L1_Error VL53L1_set_offset_correction_mode(
2770 	VL53L1_DEV                     Dev,
2771 	VL53L1_OffsetCorrectionMode    offset_cor_mode)
2772 {
2773 
2774 	/*
2775 	 * Sets the offset correction mode
2776 	 */
2777 
2778 	VL53L1_Error  status = VL53L1_ERROR_NONE;
2779 
2780 	VL53L1_LLDriverData_t *pdev = VL53L1DevStructGetLLDriverHandle(Dev);
2781 
2782 	LOG_FUNCTION_START("");
2783 
2784 	pdev->offset_correction_mode = offset_cor_mode;
2785 
2786 	LOG_FUNCTION_END(status);
2787 
2788 	return status;
2789 }
2790 
2791 
VL53L1_get_offset_correction_mode(VL53L1_DEV Dev,VL53L1_OffsetCorrectionMode * poffset_cor_mode)2792 VL53L1_Error VL53L1_get_offset_correction_mode(
2793 	VL53L1_DEV                     Dev,
2794 	VL53L1_OffsetCorrectionMode   *poffset_cor_mode)
2795 {
2796 
2797 	/*
2798 	 * Gets the offset correction mode
2799 	 */
2800 
2801 	VL53L1_Error  status = VL53L1_ERROR_NONE;
2802 
2803 	VL53L1_LLDriverData_t *pdev = VL53L1DevStructGetLLDriverHandle(Dev);
2804 
2805 	LOG_FUNCTION_START("");
2806 
2807 	*poffset_cor_mode = pdev->offset_correction_mode;
2808 
2809 	LOG_FUNCTION_END(status);
2810 
2811 	return status;
2812 }
2813 
2814 
2815 /* Start Patch_AddedTuningParms_11761 */
2816 #ifdef VL53L1_DEBUG
VL53L1_get_tuning_debug_data(VL53L1_DEV Dev,VL53L1_tuning_parameters_t * ptun_data)2817 VL53L1_Error VL53L1_get_tuning_debug_data(
2818 	VL53L1_DEV                            Dev,
2819 	VL53L1_tuning_parameters_t           *ptun_data)
2820 {
2821 	/*
2822 	 * Helper function to extract all tuning parm values
2823 	 */
2824 
2825 	VL53L1_Error  status = VL53L1_ERROR_NONE;
2826 
2827 	VL53L1_LLDriverData_t *pdev = VL53L1DevStructGetLLDriverHandle(Dev);
2828 
2829 	LOG_FUNCTION_START("");
2830 
2831 	ptun_data->vl53l1_tuningparm_version =
2832 		pdev->tuning_parms.tp_tuning_parm_version;
2833 
2834 	ptun_data->vl53l1_tuningparm_key_table_version =
2835 		pdev->tuning_parms.tp_tuning_parm_key_table_version;
2836 
2837 
2838 	ptun_data->vl53l1_tuningparm_lld_version =
2839 		pdev->tuning_parms.tp_tuning_parm_lld_version;
2840 
2841 	ptun_data->vl53l1_tuningparm_lite_min_clip_mm =
2842 		pdev->tuning_parms.tp_lite_min_clip;
2843 
2844 	ptun_data->vl53l1_tuningparm_lite_long_sigma_thresh_mm =
2845 		pdev->tuning_parms.tp_lite_long_sigma_thresh_mm;
2846 
2847 	ptun_data->vl53l1_tuningparm_lite_med_sigma_thresh_mm =
2848 		pdev->tuning_parms.tp_lite_med_sigma_thresh_mm;
2849 
2850 	ptun_data->vl53l1_tuningparm_lite_short_sigma_thresh_mm =
2851 		pdev->tuning_parms.tp_lite_short_sigma_thresh_mm;
2852 
2853 	ptun_data->vl53l1_tuningparm_lite_long_min_count_rate_rtn_mcps =
2854 		pdev->tuning_parms.tp_lite_long_min_count_rate_rtn_mcps;
2855 
2856 	ptun_data->vl53l1_tuningparm_lite_med_min_count_rate_rtn_mcps =
2857 		pdev->tuning_parms.tp_lite_med_min_count_rate_rtn_mcps;
2858 
2859 	ptun_data->vl53l1_tuningparm_lite_short_min_count_rate_rtn_mcps =
2860 		pdev->tuning_parms.tp_lite_short_min_count_rate_rtn_mcps;
2861 
2862 	ptun_data->vl53l1_tuningparm_lite_sigma_est_pulse_width =
2863 		pdev->tuning_parms.tp_lite_sigma_est_pulse_width_ns;
2864 
2865 	ptun_data->vl53l1_tuningparm_lite_sigma_est_amb_width_ns =
2866 		pdev->tuning_parms.tp_lite_sigma_est_amb_width_ns;
2867 
2868 	ptun_data->vl53l1_tuningparm_lite_sigma_ref_mm =
2869 		pdev->tuning_parms.tp_lite_sigma_ref_mm;
2870 
2871 	ptun_data->vl53l1_tuningparm_lite_rit_mult =
2872 		pdev->xtalk_cfg.crosstalk_range_ignore_threshold_mult;
2873 
2874 	ptun_data->vl53l1_tuningparm_lite_seed_config =
2875 		pdev->tuning_parms.tp_lite_seed_cfg ;
2876 
2877 	ptun_data->vl53l1_tuningparm_lite_quantifier =
2878 		pdev->tuning_parms.tp_lite_quantifier;
2879 
2880 	ptun_data->vl53l1_tuningparm_lite_first_order_select =
2881 		pdev->tuning_parms.tp_lite_first_order_select;
2882 
2883 	ptun_data->vl53l1_tuningparm_lite_xtalk_margin_kcps =
2884 		pdev->xtalk_cfg.lite_mode_crosstalk_margin_kcps;
2885 
2886 	ptun_data->vl53l1_tuningparm_initial_phase_rtn_lite_long_range =
2887 		pdev->tuning_parms.tp_init_phase_rtn_lite_long;
2888 
2889 	ptun_data->vl53l1_tuningparm_initial_phase_rtn_lite_med_range =
2890 		pdev->tuning_parms.tp_init_phase_rtn_lite_med;
2891 
2892 	ptun_data->vl53l1_tuningparm_initial_phase_rtn_lite_short_range =
2893 		pdev->tuning_parms.tp_init_phase_rtn_lite_short;
2894 
2895 	ptun_data->vl53l1_tuningparm_initial_phase_ref_lite_long_range =
2896 		pdev->tuning_parms.tp_init_phase_ref_lite_long;
2897 
2898 	ptun_data->vl53l1_tuningparm_initial_phase_ref_lite_med_range =
2899 		pdev->tuning_parms.tp_init_phase_ref_lite_med;
2900 
2901 	ptun_data->vl53l1_tuningparm_initial_phase_ref_lite_short_range =
2902 		pdev->tuning_parms.tp_init_phase_ref_lite_short;
2903 
2904 	ptun_data->vl53l1_tuningparm_timed_seed_config =
2905 		pdev->tuning_parms.tp_timed_seed_cfg;
2906 
2907 	ptun_data->vl53l1_tuningparm_vhv_loopbound =
2908 		pdev->stat_nvm.vhv_config__timeout_macrop_loop_bound;
2909 
2910 	ptun_data->vl53l1_tuningparm_refspadchar_device_test_mode =
2911 		pdev->refspadchar.device_test_mode;
2912 
2913 	ptun_data->vl53l1_tuningparm_refspadchar_vcsel_period =
2914 		pdev->refspadchar.vcsel_period;
2915 
2916 	ptun_data->vl53l1_tuningparm_refspadchar_phasecal_timeout_us =
2917 		pdev->refspadchar.timeout_us;
2918 
2919 	ptun_data->vl53l1_tuningparm_refspadchar_target_count_rate_mcps =
2920 		pdev->refspadchar.target_count_rate_mcps;
2921 
2922 	ptun_data->vl53l1_tuningparm_refspadchar_min_countrate_limit_mcps =
2923 		pdev->refspadchar.min_count_rate_limit_mcps;
2924 
2925 	ptun_data->vl53l1_tuningparm_refspadchar_max_countrate_limit_mcps =
2926 		pdev->refspadchar.max_count_rate_limit_mcps;
2927 
2928 	ptun_data->vl53l1_tuningparm_offset_cal_dss_rate_mcps =
2929 		pdev->offsetcal_cfg.dss_config__target_total_rate_mcps;
2930 
2931 	ptun_data->vl53l1_tuningparm_offset_cal_phasecal_timeout_us =
2932 		pdev->offsetcal_cfg.phasecal_config_timeout_us;
2933 
2934 	ptun_data->vl53l1_tuningparm_offset_cal_mm_timeout_us =
2935 		pdev->offsetcal_cfg.mm_config_timeout_us;
2936 
2937 	ptun_data->vl53l1_tuningparm_offset_cal_range_timeout_us =
2938 		pdev->offsetcal_cfg.range_config_timeout_us;
2939 
2940 	ptun_data->vl53l1_tuningparm_offset_cal_pre_samples =
2941 		pdev->offsetcal_cfg.pre_num_of_samples;
2942 
2943 	ptun_data->vl53l1_tuningparm_offset_cal_mm1_samples =
2944 		pdev->offsetcal_cfg.mm1_num_of_samples;
2945 
2946 	ptun_data->vl53l1_tuningparm_offset_cal_mm2_samples =
2947 		pdev->offsetcal_cfg.mm2_num_of_samples;
2948 
2949 	ptun_data->vl53l1_tuningparm_spadmap_vcsel_period =
2950 		pdev->ssc_cfg.vcsel_period;
2951 
2952 	ptun_data->vl53l1_tuningparm_spadmap_vcsel_start =
2953 		pdev->ssc_cfg.vcsel_start;
2954 
2955 	ptun_data->vl53l1_tuningparm_spadmap_rate_limit_mcps =
2956 		pdev->ssc_cfg.rate_limit_mcps;
2957 
2958 	ptun_data->vl53l1_tuningparm_lite_dss_config_target_total_rate_mcps =
2959 		pdev->tuning_parms.tp_dss_target_lite_mcps;
2960 
2961 	ptun_data->vl53l1_tuningparm_timed_dss_config_target_total_rate_mcps =
2962 		pdev->tuning_parms.tp_dss_target_timed_mcps;
2963 
2964 	ptun_data->vl53l1_tuningparm_lite_phasecal_config_timeout_us =
2965 		pdev->tuning_parms.tp_phasecal_timeout_lite_us;
2966 
2967 	ptun_data->vl53l1_tuningparm_timed_phasecal_config_timeout_us =
2968 		pdev->tuning_parms.tp_phasecal_timeout_timed_us;
2969 
2970 	ptun_data->vl53l1_tuningparm_lite_mm_config_timeout_us =
2971 		pdev->tuning_parms.tp_mm_timeout_lite_us;
2972 
2973 	ptun_data->vl53l1_tuningparm_timed_mm_config_timeout_us =
2974 		pdev->tuning_parms.tp_mm_timeout_timed_us;
2975 
2976 	ptun_data->vl53l1_tuningparm_lite_range_config_timeout_us =
2977 		pdev->tuning_parms.tp_range_timeout_lite_us;
2978 
2979 	ptun_data->vl53l1_tuningparm_timed_range_config_timeout_us =
2980 		pdev->tuning_parms.tp_range_timeout_timed_us;
2981 
2982 	ptun_data->vl53l1_tuningparm_lowpowerauto_vhv_loop_bound =
2983 		pdev->low_power_auto_data.vhv_loop_bound;
2984 
2985 	ptun_data->vl53l1_tuningparm_lowpowerauto_mm_config_timeout_us =
2986 		pdev->tuning_parms.tp_mm_timeout_lpa_us;
2987 
2988 	ptun_data->vl53l1_tuningparm_lowpowerauto_range_config_timeout_us =
2989 		pdev->tuning_parms.tp_range_timeout_lpa_us;
2990 
2991 	LOG_FUNCTION_END(status);
2992 
2993 	return status;
2994 }
2995 #endif
2996 
VL53L1_get_tuning_parm(VL53L1_DEV Dev,VL53L1_TuningParms tuning_parm_key,int32_t * ptuning_parm_value)2997 VL53L1_Error VL53L1_get_tuning_parm(
2998 	VL53L1_DEV                     Dev,
2999 	VL53L1_TuningParms             tuning_parm_key,
3000 	int32_t                       *ptuning_parm_value)
3001 {
3002 
3003 	/*
3004 	 * Gets the requested tuning parm value
3005 	 * - Large case statement for returns
3006 	 * - if key does not match, INVALID parm error returned
3007 	 */
3008 
3009 	VL53L1_Error  status = VL53L1_ERROR_NONE;
3010 
3011 	VL53L1_LLDriverData_t *pdev = VL53L1DevStructGetLLDriverHandle(Dev);
3012 
3013 	LOG_FUNCTION_START("");
3014 
3015 	switch (tuning_parm_key) {
3016 
3017 	case VL53L1_TUNINGPARM_VERSION:
3018 		*ptuning_parm_value =
3019 				(int32_t)pdev->tuning_parms.tp_tuning_parm_version;
3020 	break;
3021 	case VL53L1_TUNINGPARM_KEY_TABLE_VERSION:
3022 		*ptuning_parm_value =
3023 				(int32_t)pdev->tuning_parms.tp_tuning_parm_key_table_version;
3024 	break;
3025 	case VL53L1_TUNINGPARM_LLD_VERSION:
3026 		*ptuning_parm_value =
3027 				(int32_t)pdev->tuning_parms.tp_tuning_parm_lld_version;
3028 	break;
3029 	case VL53L1_TUNINGPARM_CONSISTENCY_LITE_PHASE_TOLERANCE:
3030 		*ptuning_parm_value =
3031 				(int32_t)pdev->tuning_parms.tp_consistency_lite_phase_tolerance;
3032 	break;
3033 	case VL53L1_TUNINGPARM_PHASECAL_TARGET:
3034 		*ptuning_parm_value =
3035 				(int32_t)pdev->tuning_parms.tp_phasecal_target;
3036 	break;
3037 	case VL53L1_TUNINGPARM_LITE_CAL_REPEAT_RATE:
3038 		*ptuning_parm_value =
3039 				(int32_t)pdev->tuning_parms.tp_cal_repeat_rate;
3040 	break;
3041 	case VL53L1_TUNINGPARM_LITE_RANGING_GAIN_FACTOR:
3042 		*ptuning_parm_value =
3043 				(int32_t)pdev->gain_cal.standard_ranging_gain_factor;
3044 	break;
3045 	case VL53L1_TUNINGPARM_LITE_MIN_CLIP_MM:
3046 		*ptuning_parm_value =
3047 				(int32_t)pdev->tuning_parms.tp_lite_min_clip;
3048 	break;
3049 	case VL53L1_TUNINGPARM_LITE_LONG_SIGMA_THRESH_MM:
3050 		*ptuning_parm_value =
3051 				(int32_t)pdev->tuning_parms.tp_lite_long_sigma_thresh_mm;
3052 	break;
3053 	case VL53L1_TUNINGPARM_LITE_MED_SIGMA_THRESH_MM:
3054 		*ptuning_parm_value =
3055 				(int32_t)pdev->tuning_parms.tp_lite_med_sigma_thresh_mm;
3056 	break;
3057 	case VL53L1_TUNINGPARM_LITE_SHORT_SIGMA_THRESH_MM:
3058 		*ptuning_parm_value =
3059 				(int32_t)pdev->tuning_parms.tp_lite_short_sigma_thresh_mm;
3060 	break;
3061 	case VL53L1_TUNINGPARM_LITE_LONG_MIN_COUNT_RATE_RTN_MCPS:
3062 		*ptuning_parm_value =
3063 				(int32_t)pdev->tuning_parms.tp_lite_long_min_count_rate_rtn_mcps;
3064 	break;
3065 	case VL53L1_TUNINGPARM_LITE_MED_MIN_COUNT_RATE_RTN_MCPS:
3066 		*ptuning_parm_value =
3067 				(int32_t)pdev->tuning_parms.tp_lite_med_min_count_rate_rtn_mcps;
3068 	break;
3069 	case VL53L1_TUNINGPARM_LITE_SHORT_MIN_COUNT_RATE_RTN_MCPS:
3070 		*ptuning_parm_value =
3071 				(int32_t)pdev->tuning_parms.tp_lite_short_min_count_rate_rtn_mcps;
3072 	break;
3073 	case VL53L1_TUNINGPARM_LITE_SIGMA_EST_PULSE_WIDTH:
3074 		*ptuning_parm_value =
3075 				(int32_t)pdev->tuning_parms.tp_lite_sigma_est_pulse_width_ns;
3076 	break;
3077 	case VL53L1_TUNINGPARM_LITE_SIGMA_EST_AMB_WIDTH_NS:
3078 		*ptuning_parm_value =
3079 				(int32_t)pdev->tuning_parms.tp_lite_sigma_est_amb_width_ns;
3080 	break;
3081 	case VL53L1_TUNINGPARM_LITE_SIGMA_REF_MM:
3082 		*ptuning_parm_value =
3083 				(int32_t)pdev->tuning_parms.tp_lite_sigma_ref_mm;
3084 	break;
3085 	case VL53L1_TUNINGPARM_LITE_RIT_MULT:
3086 		*ptuning_parm_value =
3087 				(int32_t)pdev->xtalk_cfg.crosstalk_range_ignore_threshold_mult;
3088 	break;
3089 	case VL53L1_TUNINGPARM_LITE_SEED_CONFIG:
3090 		*ptuning_parm_value =
3091 				(int32_t)pdev->tuning_parms.tp_lite_seed_cfg ;
3092 	break;
3093 	case VL53L1_TUNINGPARM_LITE_QUANTIFIER:
3094 		*ptuning_parm_value =
3095 				(int32_t)pdev->tuning_parms.tp_lite_quantifier;
3096 	break;
3097 	case VL53L1_TUNINGPARM_LITE_FIRST_ORDER_SELECT:
3098 		*ptuning_parm_value =
3099 				(int32_t)pdev->tuning_parms.tp_lite_first_order_select;
3100 	break;
3101 	case VL53L1_TUNINGPARM_LITE_XTALK_MARGIN_KCPS:
3102 		*ptuning_parm_value =
3103 				(int32_t)pdev->xtalk_cfg.lite_mode_crosstalk_margin_kcps;
3104 	break;
3105 	case VL53L1_TUNINGPARM_INITIAL_PHASE_RTN_LITE_LONG_RANGE:
3106 		*ptuning_parm_value =
3107 				(int32_t)pdev->tuning_parms.tp_init_phase_rtn_lite_long;
3108 	break;
3109 	case VL53L1_TUNINGPARM_INITIAL_PHASE_RTN_LITE_MED_RANGE:
3110 		*ptuning_parm_value =
3111 				(int32_t)pdev->tuning_parms.tp_init_phase_rtn_lite_med;
3112 	break;
3113 	case VL53L1_TUNINGPARM_INITIAL_PHASE_RTN_LITE_SHORT_RANGE:
3114 		*ptuning_parm_value =
3115 				(int32_t)pdev->tuning_parms.tp_init_phase_rtn_lite_short;
3116 	break;
3117 	case VL53L1_TUNINGPARM_INITIAL_PHASE_REF_LITE_LONG_RANGE:
3118 		*ptuning_parm_value =
3119 				(int32_t)pdev->tuning_parms.tp_init_phase_ref_lite_long;
3120 	break;
3121 	case VL53L1_TUNINGPARM_INITIAL_PHASE_REF_LITE_MED_RANGE:
3122 		*ptuning_parm_value =
3123 				(int32_t)pdev->tuning_parms.tp_init_phase_ref_lite_med;
3124 	break;
3125 	case VL53L1_TUNINGPARM_INITIAL_PHASE_REF_LITE_SHORT_RANGE:
3126 		*ptuning_parm_value =
3127 				(int32_t)pdev->tuning_parms.tp_init_phase_ref_lite_short;
3128 	break;
3129 	case VL53L1_TUNINGPARM_TIMED_SEED_CONFIG:
3130 		*ptuning_parm_value =
3131 				(int32_t)pdev->tuning_parms.tp_timed_seed_cfg;
3132 	break;
3133 	case VL53L1_TUNINGPARM_VHV_LOOPBOUND:
3134 		*ptuning_parm_value =
3135 				(int32_t)pdev->stat_nvm.vhv_config__timeout_macrop_loop_bound;
3136 	break;
3137 	case VL53L1_TUNINGPARM_REFSPADCHAR_DEVICE_TEST_MODE:
3138 		*ptuning_parm_value =
3139 				(int32_t)pdev->refspadchar.device_test_mode;
3140 	break;
3141 	case VL53L1_TUNINGPARM_REFSPADCHAR_VCSEL_PERIOD:
3142 		*ptuning_parm_value =
3143 				(int32_t)pdev->refspadchar.vcsel_period;
3144 	break;
3145 	case VL53L1_TUNINGPARM_REFSPADCHAR_PHASECAL_TIMEOUT_US:
3146 		*ptuning_parm_value =
3147 				(int32_t)pdev->refspadchar.timeout_us;
3148 	break;
3149 	case VL53L1_TUNINGPARM_REFSPADCHAR_TARGET_COUNT_RATE_MCPS:
3150 		*ptuning_parm_value =
3151 				(int32_t)pdev->refspadchar.target_count_rate_mcps;
3152 	break;
3153 	case VL53L1_TUNINGPARM_REFSPADCHAR_MIN_COUNTRATE_LIMIT_MCPS:
3154 		*ptuning_parm_value =
3155 				(int32_t)pdev->refspadchar.min_count_rate_limit_mcps;
3156 	break;
3157 	case VL53L1_TUNINGPARM_REFSPADCHAR_MAX_COUNTRATE_LIMIT_MCPS:
3158 		*ptuning_parm_value =
3159 				(int32_t)pdev->refspadchar.max_count_rate_limit_mcps;
3160 	break;
3161 	case VL53L1_TUNINGPARM_OFFSET_CAL_DSS_RATE_MCPS:
3162 		*ptuning_parm_value =
3163 				(int32_t)pdev->offsetcal_cfg.dss_config__target_total_rate_mcps;;
3164 	break;
3165 	case VL53L1_TUNINGPARM_OFFSET_CAL_PHASECAL_TIMEOUT_US:
3166 		*ptuning_parm_value =
3167 				(int32_t)pdev->offsetcal_cfg.phasecal_config_timeout_us;
3168 	break;
3169 	case VL53L1_TUNINGPARM_OFFSET_CAL_MM_TIMEOUT_US:
3170 		*ptuning_parm_value =
3171 				(int32_t)pdev->offsetcal_cfg.mm_config_timeout_us;
3172 	break;
3173 	case VL53L1_TUNINGPARM_OFFSET_CAL_RANGE_TIMEOUT_US:
3174 		*ptuning_parm_value =
3175 				(int32_t)pdev->offsetcal_cfg.range_config_timeout_us;
3176 	break;
3177 	case VL53L1_TUNINGPARM_OFFSET_CAL_PRE_SAMPLES:
3178 		*ptuning_parm_value =
3179 				(int32_t)pdev->offsetcal_cfg.pre_num_of_samples;
3180 	break;
3181 	case VL53L1_TUNINGPARM_OFFSET_CAL_MM1_SAMPLES:
3182 		*ptuning_parm_value =
3183 			(int32_t)pdev->offsetcal_cfg.mm1_num_of_samples;
3184 	break;
3185 	case VL53L1_TUNINGPARM_OFFSET_CAL_MM2_SAMPLES:
3186 		*ptuning_parm_value =
3187 				(int32_t)pdev->offsetcal_cfg.mm2_num_of_samples;
3188 	break;
3189 	case VL53L1_TUNINGPARM_SPADMAP_VCSEL_PERIOD:
3190 		*ptuning_parm_value =
3191 				(int32_t)pdev->ssc_cfg.vcsel_period;
3192 	break;
3193 	case VL53L1_TUNINGPARM_SPADMAP_VCSEL_START:
3194 		*ptuning_parm_value =
3195 				(int32_t)pdev->ssc_cfg.vcsel_start;
3196 	break;
3197 	case VL53L1_TUNINGPARM_SPADMAP_RATE_LIMIT_MCPS:
3198 		*ptuning_parm_value =
3199 				(int32_t)pdev->ssc_cfg.rate_limit_mcps;
3200 	break;
3201 	case VL53L1_TUNINGPARM_LITE_DSS_CONFIG_TARGET_TOTAL_RATE_MCPS:
3202 		*ptuning_parm_value =
3203 				(int32_t)pdev->tuning_parms.tp_dss_target_lite_mcps;
3204 	break;
3205 	case VL53L1_TUNINGPARM_TIMED_DSS_CONFIG_TARGET_TOTAL_RATE_MCPS:
3206 		*ptuning_parm_value =
3207 				(int32_t)pdev->tuning_parms.tp_dss_target_timed_mcps;
3208 	break;
3209 	case VL53L1_TUNINGPARM_LITE_PHASECAL_CONFIG_TIMEOUT_US:
3210 		*ptuning_parm_value =
3211 				(int32_t)pdev->tuning_parms.tp_phasecal_timeout_lite_us;
3212 	break;
3213 	case VL53L1_TUNINGPARM_TIMED_PHASECAL_CONFIG_TIMEOUT_US:
3214 		*ptuning_parm_value =
3215 				(int32_t)pdev->tuning_parms.tp_phasecal_timeout_timed_us;
3216 	break;
3217 	case VL53L1_TUNINGPARM_LITE_MM_CONFIG_TIMEOUT_US:
3218 		*ptuning_parm_value =
3219 				(int32_t)pdev->tuning_parms.tp_mm_timeout_lite_us;
3220 	break;
3221 	case VL53L1_TUNINGPARM_TIMED_MM_CONFIG_TIMEOUT_US:
3222 		*ptuning_parm_value =
3223 				(int32_t)pdev->tuning_parms.tp_mm_timeout_timed_us;
3224 	break;
3225 	case VL53L1_TUNINGPARM_LITE_RANGE_CONFIG_TIMEOUT_US:
3226 		*ptuning_parm_value =
3227 				(int32_t)pdev->tuning_parms.tp_range_timeout_lite_us;
3228 	break;
3229 	case VL53L1_TUNINGPARM_TIMED_RANGE_CONFIG_TIMEOUT_US:
3230 		*ptuning_parm_value =
3231 				(int32_t)pdev->tuning_parms.tp_range_timeout_timed_us;
3232 	break;
3233 	case VL53L1_TUNINGPARM_LOWPOWERAUTO_VHV_LOOP_BOUND:
3234 		*ptuning_parm_value =
3235 				(int32_t)pdev->low_power_auto_data.vhv_loop_bound;
3236 	break;
3237 	case VL53L1_TUNINGPARM_LOWPOWERAUTO_MM_CONFIG_TIMEOUT_US:
3238 		*ptuning_parm_value =
3239 				(int32_t)pdev->tuning_parms.tp_mm_timeout_lpa_us;
3240 	break;
3241 	case VL53L1_TUNINGPARM_LOWPOWERAUTO_RANGE_CONFIG_TIMEOUT_US:
3242 		*ptuning_parm_value =
3243 				(int32_t)pdev->tuning_parms.tp_range_timeout_lpa_us;
3244 	break;
3245 
3246 
3247 	default:
3248 		*ptuning_parm_value = 0x7FFFFFFF;
3249 		status = VL53L1_ERROR_INVALID_PARAMS;
3250 	break;
3251 
3252 	}
3253 
3254 	LOG_FUNCTION_END(status);
3255 
3256 	return status;
3257 }
3258 
VL53L1_set_tuning_parm(VL53L1_DEV Dev,VL53L1_TuningParms tuning_parm_key,int32_t tuning_parm_value)3259 VL53L1_Error VL53L1_set_tuning_parm(
3260 	VL53L1_DEV            Dev,
3261 	VL53L1_TuningParms    tuning_parm_key,
3262 	int32_t               tuning_parm_value)
3263 {
3264 
3265 	/*
3266 	 * Sets the requested tuning parm value
3267 	 * - Large case statement for set value
3268 	 * - if key does not match, INVALID parm error returned
3269 	 */
3270 
3271 	VL53L1_Error  status = VL53L1_ERROR_NONE;
3272 
3273 	VL53L1_LLDriverData_t *pdev = VL53L1DevStructGetLLDriverHandle(Dev);
3274 
3275 	LOG_FUNCTION_START("");
3276 
3277 	switch (tuning_parm_key) {
3278 
3279 	case VL53L1_TUNINGPARM_VERSION:
3280 		pdev->tuning_parms.tp_tuning_parm_version =
3281 				(uint16_t)tuning_parm_value;
3282 	break;
3283 	case VL53L1_TUNINGPARM_KEY_TABLE_VERSION:
3284 		pdev->tuning_parms.tp_tuning_parm_key_table_version =
3285 					(uint16_t)tuning_parm_value;
3286 
3287 		/* Perform Key Table Check
3288 		 *
3289 		 * - If does not match default, key table
3290 		 * format does not match tuning file,
3291 		 * error should be thrown
3292 		 *
3293 		 */
3294 
3295 		if ((uint16_t)tuning_parm_value
3296 				!= VL53L1_TUNINGPARM_KEY_TABLE_VERSION_DEFAULT) {
3297 			status = VL53L1_ERROR_TUNING_PARM_KEY_MISMATCH;
3298 		}
3299 	break;
3300 	case VL53L1_TUNINGPARM_LLD_VERSION:
3301 		pdev->tuning_parms.tp_tuning_parm_lld_version =
3302 				(uint16_t)tuning_parm_value;
3303 	break;
3304 	case VL53L1_TUNINGPARM_CONSISTENCY_LITE_PHASE_TOLERANCE:
3305 		pdev->tuning_parms.tp_consistency_lite_phase_tolerance =
3306 				(uint8_t)tuning_parm_value;
3307 	break;
3308 	case VL53L1_TUNINGPARM_PHASECAL_TARGET:
3309 		pdev->tuning_parms.tp_phasecal_target =
3310 				(uint8_t)tuning_parm_value;
3311 	break;
3312 	case VL53L1_TUNINGPARM_LITE_CAL_REPEAT_RATE:
3313 		pdev->tuning_parms.tp_cal_repeat_rate =
3314 				(uint16_t)tuning_parm_value;
3315 	break;
3316 	case VL53L1_TUNINGPARM_LITE_RANGING_GAIN_FACTOR:
3317 		pdev->gain_cal.standard_ranging_gain_factor =
3318 				(uint16_t)tuning_parm_value;
3319 	break;
3320 	case VL53L1_TUNINGPARM_LITE_MIN_CLIP_MM:
3321 		pdev->tuning_parms.tp_lite_min_clip =
3322 				(uint8_t)tuning_parm_value;
3323 	break;
3324 	case VL53L1_TUNINGPARM_LITE_LONG_SIGMA_THRESH_MM:
3325 		pdev->tuning_parms.tp_lite_long_sigma_thresh_mm =
3326 				(uint16_t)tuning_parm_value;
3327 	break;
3328 	case VL53L1_TUNINGPARM_LITE_MED_SIGMA_THRESH_MM:
3329 		pdev->tuning_parms.tp_lite_med_sigma_thresh_mm =
3330 				(uint16_t)tuning_parm_value;
3331 	break;
3332 	case VL53L1_TUNINGPARM_LITE_SHORT_SIGMA_THRESH_MM:
3333 		pdev->tuning_parms.tp_lite_short_sigma_thresh_mm =
3334 				(uint16_t)tuning_parm_value;
3335 	break;
3336 	case VL53L1_TUNINGPARM_LITE_LONG_MIN_COUNT_RATE_RTN_MCPS:
3337 		pdev->tuning_parms.tp_lite_long_min_count_rate_rtn_mcps =
3338 				(uint16_t)tuning_parm_value;
3339 	break;
3340 	case VL53L1_TUNINGPARM_LITE_MED_MIN_COUNT_RATE_RTN_MCPS:
3341 		pdev->tuning_parms.tp_lite_med_min_count_rate_rtn_mcps =
3342 				(uint16_t)tuning_parm_value;
3343 	break;
3344 	case VL53L1_TUNINGPARM_LITE_SHORT_MIN_COUNT_RATE_RTN_MCPS:
3345 		pdev->tuning_parms.tp_lite_short_min_count_rate_rtn_mcps =
3346 				(uint16_t)tuning_parm_value;
3347 	break;
3348 	case VL53L1_TUNINGPARM_LITE_SIGMA_EST_PULSE_WIDTH:
3349 		pdev->tuning_parms.tp_lite_sigma_est_pulse_width_ns =
3350 				(uint8_t)tuning_parm_value;
3351 	break;
3352 	case VL53L1_TUNINGPARM_LITE_SIGMA_EST_AMB_WIDTH_NS:
3353 		pdev->tuning_parms.tp_lite_sigma_est_amb_width_ns =
3354 				(uint8_t)tuning_parm_value;
3355 	break;
3356 	case VL53L1_TUNINGPARM_LITE_SIGMA_REF_MM:
3357 		pdev->tuning_parms.tp_lite_sigma_ref_mm =
3358 				(uint8_t)tuning_parm_value;
3359 	break;
3360 	case VL53L1_TUNINGPARM_LITE_RIT_MULT:
3361 		pdev->xtalk_cfg.crosstalk_range_ignore_threshold_mult =
3362 				(uint8_t)tuning_parm_value;
3363 	break;
3364 	case VL53L1_TUNINGPARM_LITE_SEED_CONFIG:
3365 		pdev->tuning_parms.tp_lite_seed_cfg =
3366 				(uint8_t)tuning_parm_value;
3367 	break;
3368 	case VL53L1_TUNINGPARM_LITE_QUANTIFIER:
3369 		pdev->tuning_parms.tp_lite_quantifier =
3370 				(uint8_t)tuning_parm_value;
3371 	break;
3372 	case VL53L1_TUNINGPARM_LITE_FIRST_ORDER_SELECT:
3373 		pdev->tuning_parms.tp_lite_first_order_select =
3374 				(uint8_t)tuning_parm_value;
3375 	break;
3376 	case VL53L1_TUNINGPARM_LITE_XTALK_MARGIN_KCPS:
3377 		pdev->xtalk_cfg.lite_mode_crosstalk_margin_kcps =
3378 				(int16_t)tuning_parm_value;
3379 	break;
3380 	case VL53L1_TUNINGPARM_INITIAL_PHASE_RTN_LITE_LONG_RANGE:
3381 		pdev->tuning_parms.tp_init_phase_rtn_lite_long =
3382 				(uint8_t)tuning_parm_value;
3383 	break;
3384 	case VL53L1_TUNINGPARM_INITIAL_PHASE_RTN_LITE_MED_RANGE:
3385 		pdev->tuning_parms.tp_init_phase_rtn_lite_med =
3386 				(uint8_t)tuning_parm_value;
3387 	break;
3388 	case VL53L1_TUNINGPARM_INITIAL_PHASE_RTN_LITE_SHORT_RANGE:
3389 		pdev->tuning_parms.tp_init_phase_rtn_lite_short =
3390 				(uint8_t)tuning_parm_value;
3391 	break;
3392 	case VL53L1_TUNINGPARM_INITIAL_PHASE_REF_LITE_LONG_RANGE:
3393 		pdev->tuning_parms.tp_init_phase_ref_lite_long =
3394 				(uint8_t)tuning_parm_value;
3395 	break;
3396 	case VL53L1_TUNINGPARM_INITIAL_PHASE_REF_LITE_MED_RANGE:
3397 		pdev->tuning_parms.tp_init_phase_ref_lite_med =
3398 				(uint8_t)tuning_parm_value;
3399 	break;
3400 	case VL53L1_TUNINGPARM_INITIAL_PHASE_REF_LITE_SHORT_RANGE:
3401 		pdev->tuning_parms.tp_init_phase_ref_lite_short =
3402 				(uint8_t)tuning_parm_value;
3403 	break;
3404 	case VL53L1_TUNINGPARM_TIMED_SEED_CONFIG:
3405 		pdev->tuning_parms.tp_timed_seed_cfg =
3406 				(uint8_t)tuning_parm_value;
3407 	break;
3408 	case VL53L1_TUNINGPARM_VHV_LOOPBOUND:
3409 		pdev->stat_nvm.vhv_config__timeout_macrop_loop_bound =
3410 				(uint8_t)tuning_parm_value;
3411 	break;
3412 	case VL53L1_TUNINGPARM_REFSPADCHAR_DEVICE_TEST_MODE:
3413 		pdev->refspadchar.device_test_mode =
3414 				(uint8_t)tuning_parm_value;
3415 	break;
3416 	case VL53L1_TUNINGPARM_REFSPADCHAR_VCSEL_PERIOD:
3417 		pdev->refspadchar.vcsel_period =
3418 				(uint8_t)tuning_parm_value;
3419 	break;
3420 	case VL53L1_TUNINGPARM_REFSPADCHAR_PHASECAL_TIMEOUT_US:
3421 		pdev->refspadchar.timeout_us =
3422 				(uint32_t)tuning_parm_value;
3423 	break;
3424 	case VL53L1_TUNINGPARM_REFSPADCHAR_TARGET_COUNT_RATE_MCPS:
3425 		pdev->refspadchar.target_count_rate_mcps =
3426 				(uint16_t)tuning_parm_value;
3427 	break;
3428 	case VL53L1_TUNINGPARM_REFSPADCHAR_MIN_COUNTRATE_LIMIT_MCPS:
3429 		pdev->refspadchar.min_count_rate_limit_mcps =
3430 				(uint16_t)tuning_parm_value;
3431 	break;
3432 	case VL53L1_TUNINGPARM_REFSPADCHAR_MAX_COUNTRATE_LIMIT_MCPS:
3433 		pdev->refspadchar.max_count_rate_limit_mcps =
3434 				(uint16_t)tuning_parm_value;
3435 	break;
3436 	case VL53L1_TUNINGPARM_OFFSET_CAL_DSS_RATE_MCPS:
3437 		pdev->offsetcal_cfg.dss_config__target_total_rate_mcps =
3438 				(uint16_t)tuning_parm_value;
3439 	break;
3440 	case VL53L1_TUNINGPARM_OFFSET_CAL_PHASECAL_TIMEOUT_US:
3441 		pdev->offsetcal_cfg.phasecal_config_timeout_us =
3442 				(uint32_t)tuning_parm_value;
3443 	break;
3444 	case VL53L1_TUNINGPARM_OFFSET_CAL_MM_TIMEOUT_US:
3445 		pdev->offsetcal_cfg.mm_config_timeout_us =
3446 				(uint32_t)tuning_parm_value;
3447 	break;
3448 	case VL53L1_TUNINGPARM_OFFSET_CAL_RANGE_TIMEOUT_US:
3449 		pdev->offsetcal_cfg.range_config_timeout_us =
3450 				(uint32_t)tuning_parm_value;
3451 	break;
3452 	case VL53L1_TUNINGPARM_OFFSET_CAL_PRE_SAMPLES:
3453 		pdev->offsetcal_cfg.pre_num_of_samples =
3454 				(uint8_t)tuning_parm_value;
3455 	break;
3456 	case VL53L1_TUNINGPARM_OFFSET_CAL_MM1_SAMPLES:
3457 		pdev->offsetcal_cfg.mm1_num_of_samples =
3458 				(uint8_t)tuning_parm_value;
3459 	break;
3460 	case VL53L1_TUNINGPARM_OFFSET_CAL_MM2_SAMPLES:
3461 		pdev->offsetcal_cfg.mm2_num_of_samples =
3462 				(uint8_t)tuning_parm_value;
3463 	break;
3464 	case VL53L1_TUNINGPARM_SPADMAP_VCSEL_PERIOD:
3465 		pdev->ssc_cfg.vcsel_period =
3466 				(uint8_t)tuning_parm_value;
3467 	break;
3468 	case VL53L1_TUNINGPARM_SPADMAP_VCSEL_START:
3469 		pdev->ssc_cfg.vcsel_start =
3470 				(uint8_t)tuning_parm_value;
3471 	break;
3472 	case VL53L1_TUNINGPARM_SPADMAP_RATE_LIMIT_MCPS:
3473 		pdev->ssc_cfg.rate_limit_mcps =
3474 				(uint16_t)tuning_parm_value;
3475 	break;
3476 	case VL53L1_TUNINGPARM_LITE_DSS_CONFIG_TARGET_TOTAL_RATE_MCPS:
3477 		pdev->tuning_parms.tp_dss_target_lite_mcps =
3478 			(uint16_t)tuning_parm_value;
3479 	break;
3480 	case VL53L1_TUNINGPARM_TIMED_DSS_CONFIG_TARGET_TOTAL_RATE_MCPS:
3481 		pdev->tuning_parms.tp_dss_target_timed_mcps =
3482 			(uint16_t)tuning_parm_value;
3483 	break;
3484 	case VL53L1_TUNINGPARM_LITE_PHASECAL_CONFIG_TIMEOUT_US:
3485 		pdev->tuning_parms.tp_phasecal_timeout_lite_us =
3486 			(uint32_t)tuning_parm_value;
3487 	break;
3488 	case VL53L1_TUNINGPARM_TIMED_PHASECAL_CONFIG_TIMEOUT_US:
3489 		pdev->tuning_parms.tp_phasecal_timeout_timed_us =
3490 			(uint32_t)tuning_parm_value;
3491 	break;
3492 	case VL53L1_TUNINGPARM_LITE_MM_CONFIG_TIMEOUT_US:
3493 		pdev->tuning_parms.tp_mm_timeout_lite_us =
3494 			(uint32_t)tuning_parm_value;
3495 	break;
3496 	case VL53L1_TUNINGPARM_TIMED_MM_CONFIG_TIMEOUT_US:
3497 		pdev->tuning_parms.tp_mm_timeout_timed_us =
3498 			(uint32_t)tuning_parm_value;
3499 	break;
3500 	case VL53L1_TUNINGPARM_LITE_RANGE_CONFIG_TIMEOUT_US:
3501 		pdev->tuning_parms.tp_range_timeout_lite_us =
3502 			(uint32_t)tuning_parm_value;
3503 	break;
3504 	case VL53L1_TUNINGPARM_TIMED_RANGE_CONFIG_TIMEOUT_US:
3505 		pdev->tuning_parms.tp_range_timeout_timed_us =
3506 			(uint32_t)tuning_parm_value;
3507 	break;
3508 	case VL53L1_TUNINGPARM_LOWPOWERAUTO_VHV_LOOP_BOUND:
3509 		pdev->low_power_auto_data.vhv_loop_bound =
3510 			(uint8_t)tuning_parm_value;
3511 	break;
3512 	case VL53L1_TUNINGPARM_LOWPOWERAUTO_MM_CONFIG_TIMEOUT_US:
3513 		pdev->tuning_parms.tp_mm_timeout_lpa_us =
3514 			(uint32_t)tuning_parm_value;
3515 	break;
3516 	case VL53L1_TUNINGPARM_LOWPOWERAUTO_RANGE_CONFIG_TIMEOUT_US:
3517 		pdev->tuning_parms.tp_range_timeout_lpa_us =
3518 			(uint32_t)tuning_parm_value;
3519 	break;
3520 
3521 
3522 	default:
3523 		status = VL53L1_ERROR_INVALID_PARAMS;
3524 	break;
3525 
3526 	}
3527 
3528 	LOG_FUNCTION_END(status);
3529 
3530 	return status;
3531 }
3532 
3533 /* End Patch_AddedTuningParms_11761 */
3534