Statistics
| Branch: | Tag: | Revision:

amiro-lld / drivers / VL53L1X / v1 / api / core / vl53l1_api_preset_modes.c @ 4dba9195

History | View | Annotate | Download (42.294 KB)

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_preset_modes.c
65
 *
66
 * @brief EwokPlus25 API Preset Modes definitions
67
 */
68

    
69
#include "vl53l1_ll_def.h"
70
#include "vl53l1_platform_log.h"
71
#include "vl53l1_register_structs.h"
72
#include "vl53l1_register_settings.h"
73
#include "vl53l1_core.h"
74
#include "vl53l1_api_preset_modes.h"
75
#include "vl53l1_tuning_parm_defaults.h"
76

    
77

    
78
#define LOG_FUNCTION_START(fmt, ...) \
79
        _LOG_FUNCTION_START(VL53L1_TRACE_MODULE_API, fmt, ##__VA_ARGS__)
80
#define LOG_FUNCTION_END(status, ...) \
81
        _LOG_FUNCTION_END(VL53L1_TRACE_MODULE_API, status, ##__VA_ARGS__)
82
#define LOG_FUNCTION_END_FMT(status, fmt, ...) \
83
        _LOG_FUNCTION_END_FMT(VL53L1_TRACE_MODULE_API, status, fmt, ##__VA_ARGS__)
84

    
85

    
86
#ifndef VL53L1_NOCALIB
87
VL53L1_Error VL53L1_init_refspadchar_config_struct(
88
        VL53L1_refspadchar_config_t   *pdata)
89
{
90
        /*
91
         * Initializes Ref SPAD Char data structures preset mode
92
         */
93

    
94
        VL53L1_Error  status = VL53L1_ERROR_NONE;
95

    
96
        LOG_FUNCTION_START("");
97

    
98
        /* Reference SPAD Char Configuration
99
         *
100
         * vcsel_period              = 0x0B   - 24 clock VCSEL period
101
         * timeout_us                = 1000   - Set 1000us phase cal timeout
102
         * target_count_rate_mcps    = 0x0A00 - 9.7 -> 20.0 Mcps
103
         * min_count_rate_limit_mcps = 0x0500 - 9.7 -> 10.0 Mcps
104
         * max_count_rate_limit_mcps = 0x1400 - 9.7 -> 40.0 Mcps
105
         */
106

    
107
        pdata->device_test_mode =
108
                        VL53L1_TUNINGPARM_REFSPADCHAR_DEVICE_TEST_MODE_DEFAULT;
109
        pdata->vcsel_period              =
110
                        VL53L1_TUNINGPARM_REFSPADCHAR_VCSEL_PERIOD_DEFAULT;
111
        pdata->timeout_us                =
112
                        VL53L1_TUNINGPARM_REFSPADCHAR_PHASECAL_TIMEOUT_US_DEFAULT;
113
        pdata->target_count_rate_mcps    =
114
                        VL53L1_TUNINGPARM_REFSPADCHAR_TARGET_COUNT_RATE_MCPS_DEFAULT;
115
        pdata->min_count_rate_limit_mcps =
116
                        VL53L1_TUNINGPARM_REFSPADCHAR_MIN_COUNTRATE_LIMIT_MCPS_DEFAULT;
117
        pdata->max_count_rate_limit_mcps =
118
                        VL53L1_TUNINGPARM_REFSPADCHAR_MAX_COUNTRATE_LIMIT_MCPS_DEFAULT;
119

    
120
        LOG_FUNCTION_END(status);
121

    
122
        return status;
123
}
124
#endif
125

    
126

    
127
#ifndef VL53L1_NOCALIB
128
VL53L1_Error VL53L1_init_ssc_config_struct(
129
        VL53L1_ssc_config_t   *pdata)
130
{
131
        /*
132
         * Initializes SPAD Self Check (SSC) data structure
133
         */
134

    
135
        VL53L1_Error  status = VL53L1_ERROR_NONE;
136

    
137
        LOG_FUNCTION_START("");
138

    
139
        /* SPAD Select Check Configuration */
140

    
141
        /* 0 - store RTN count rates
142
         * 1 - store REF count rates
143
         */
144
        pdata->array_select = VL53L1_DEVICESSCARRAY_RTN;
145

    
146
        /* VCSEL period register value  0x12 (18) -> 38 VCSEL clocks */
147
        pdata->vcsel_period =
148
                        VL53L1_TUNINGPARM_SPADMAP_VCSEL_PERIOD_DEFAULT;
149

    
150
        /* VCSEL pulse start */
151
        pdata->vcsel_start  =
152
                        VL53L1_TUNINGPARM_SPADMAP_VCSEL_START_DEFAULT;
153

    
154
        /* VCSEL pulse width */
155
        pdata->vcsel_width  = 0x02;
156

    
157
        /* SSC timeout [us] */
158
        pdata->timeout_us   = 36000;
159

    
160
        /* SSC rate limit [Mcps]
161
         * - 9.7 for VCSEL ON
162
         * - 1.15 for VCSEL OFF
163
         */
164
        pdata->rate_limit_mcps =
165
                        VL53L1_TUNINGPARM_SPADMAP_RATE_LIMIT_MCPS_DEFAULT;
166

    
167
        LOG_FUNCTION_END(status);
168

    
169
        return status;
170
}
171
#endif
172

    
173

    
174
VL53L1_Error VL53L1_init_xtalk_config_struct(
175
        VL53L1_customer_nvm_managed_t *pnvm,
176
        VL53L1_xtalk_config_t   *pdata)
177
{
178
        /*
179
         * Initializes Xtalk Config structure
180
         */
181

    
182
        VL53L1_Error  status = VL53L1_ERROR_NONE;
183

    
184
        LOG_FUNCTION_START("");
185

    
186
        /* Xtalk default configuration
187
         *
188
         * algo__crosstalk_compensation_plane_offset_kcps
189
         *  = pdev->customer.algo__crosstalk_compensation_plane_offset_kcps
190
         * algo__crosstalk_compensation_x_plane_gradient_kcps
191
         *  = pdev->customer.algo__crosstalk_compensation_x_plane_gradient_kcps
192
         * algo__crosstalk_compensation_y_plane_gradient_kcps
193
         *  = pdev->customer.algo__crosstalk_compensation_y_plane_gradient_kcps
194
         *
195
         */
196

    
197
        /* Store xtalk data into golden copy */
198

    
199
        pdata->algo__crosstalk_compensation_plane_offset_kcps      =
200
                pnvm->algo__crosstalk_compensation_plane_offset_kcps;
201
        pdata->algo__crosstalk_compensation_x_plane_gradient_kcps  =
202
                pnvm->algo__crosstalk_compensation_x_plane_gradient_kcps;
203
        pdata->algo__crosstalk_compensation_y_plane_gradient_kcps  =
204
                pnvm->algo__crosstalk_compensation_y_plane_gradient_kcps;
205

    
206
        /* Store NVM defaults for later use */
207

    
208
        pdata->nvm_default__crosstalk_compensation_plane_offset_kcps      =
209
                (uint32_t)pnvm->algo__crosstalk_compensation_plane_offset_kcps;
210
        pdata->nvm_default__crosstalk_compensation_x_plane_gradient_kcps  =
211
                pnvm->algo__crosstalk_compensation_x_plane_gradient_kcps;
212
        pdata->nvm_default__crosstalk_compensation_y_plane_gradient_kcps  =
213
                pnvm->algo__crosstalk_compensation_y_plane_gradient_kcps;
214

    
215
        pdata->lite_mode_crosstalk_margin_kcps                     =
216
                        VL53L1_TUNINGPARM_LITE_XTALK_MARGIN_KCPS_DEFAULT;
217

    
218
        /* Default for Range Ignore Threshold Mult = 2.0 */
219

    
220
        pdata->crosstalk_range_ignore_threshold_mult =
221
                        VL53L1_TUNINGPARM_LITE_RIT_MULT_DEFAULT;
222

    
223
        if ((pdata->algo__crosstalk_compensation_plane_offset_kcps == 0x00)
224
                && (pdata->algo__crosstalk_compensation_x_plane_gradient_kcps == 0x00)
225
                && (pdata->algo__crosstalk_compensation_y_plane_gradient_kcps == 0x00))
226
                pdata->global_crosstalk_compensation_enable = 0x00;
227
        else
228
                pdata->global_crosstalk_compensation_enable = 0x01;
229

    
230

    
231
        if ((status == VL53L1_ERROR_NONE) &&
232
                (pdata->global_crosstalk_compensation_enable == 0x01)) {
233
                pdata->crosstalk_range_ignore_threshold_rate_mcps =
234
                        VL53L1_calc_range_ignore_threshold(
235
                                pdata->algo__crosstalk_compensation_plane_offset_kcps,
236
                                pdata->algo__crosstalk_compensation_x_plane_gradient_kcps,
237
                                pdata->algo__crosstalk_compensation_y_plane_gradient_kcps,
238
                                pdata->crosstalk_range_ignore_threshold_mult);
239
        } else {
240
                pdata->crosstalk_range_ignore_threshold_rate_mcps = 0;
241
        }
242

    
243
        LOG_FUNCTION_END(status);
244

    
245
        return status;
246
}
247

    
248
#ifndef VL53L1_NOCALIB
249
VL53L1_Error VL53L1_init_offset_cal_config_struct(
250
        VL53L1_offsetcal_config_t   *pdata)
251
{
252
        /*
253
         * Initializes Offset Calibration Config structure
254
         * - for use with VL53L1_run_offset_calibration()
255
         */
256

    
257
        VL53L1_Error  status = VL53L1_ERROR_NONE;
258

    
259
        LOG_FUNCTION_START("");
260

    
261
        /* Preset Timeout and DSS defaults */
262

    
263
        pdata->dss_config__target_total_rate_mcps          =
264
                        VL53L1_TUNINGPARM_OFFSET_CAL_DSS_RATE_MCPS_DEFAULT;
265
        /* 20.0 Mcps */
266
        pdata->phasecal_config_timeout_us                  =
267
                        VL53L1_TUNINGPARM_OFFSET_CAL_PHASECAL_TIMEOUT_US_DEFAULT;
268
        /* 1000 us */
269
        pdata->range_config_timeout_us                     =
270
                        VL53L1_TUNINGPARM_OFFSET_CAL_RANGE_TIMEOUT_US_DEFAULT;
271
        /* 13000 us */
272
        pdata->mm_config_timeout_us                        =
273
                        VL53L1_TUNINGPARM_OFFSET_CAL_MM_TIMEOUT_US_DEFAULT;
274
        /* 13000 us - Added as part of Patch_AddedOffsetCalMMTuningParm_11791 */
275

    
276
        /* Init number of averaged samples */
277

    
278
        pdata->pre_num_of_samples                          =
279
                        VL53L1_TUNINGPARM_OFFSET_CAL_PRE_SAMPLES_DEFAULT;
280
        pdata->mm1_num_of_samples                          =
281
                        VL53L1_TUNINGPARM_OFFSET_CAL_MM1_SAMPLES_DEFAULT;
282
        pdata->mm2_num_of_samples                          =
283
                        VL53L1_TUNINGPARM_OFFSET_CAL_MM2_SAMPLES_DEFAULT;
284

    
285
        LOG_FUNCTION_END(status);
286

    
287
        return status;
288
}
289
#endif
290

    
291
VL53L1_Error VL53L1_init_tuning_parm_storage_struct(
292
        VL53L1_tuning_parm_storage_t   *pdata)
293
{
294
        /*
295
         * Initializes  Tuning Param storage structure
296
         */
297

    
298
        VL53L1_Error  status = VL53L1_ERROR_NONE;
299

    
300
        LOG_FUNCTION_START("");
301

    
302
        /* Default configuration
303
         *
304
         * - Custom overwrite possible from vl53l1_set_tuning_parms()
305
         * - via tuning file input
306
         */
307

    
308
        pdata->tp_tuning_parm_version              =
309
                        VL53L1_TUNINGPARM_VERSION_DEFAULT;
310
        pdata->tp_tuning_parm_key_table_version    =
311
                        VL53L1_TUNINGPARM_KEY_TABLE_VERSION_DEFAULT;
312
        pdata->tp_tuning_parm_lld_version          =
313
                        VL53L1_TUNINGPARM_LLD_VERSION_DEFAULT;
314
        pdata->tp_init_phase_rtn_lite_long         =
315
                        VL53L1_TUNINGPARM_INITIAL_PHASE_RTN_LITE_LONG_RANGE_DEFAULT;
316
        pdata->tp_init_phase_rtn_lite_med          =
317
                        VL53L1_TUNINGPARM_INITIAL_PHASE_RTN_LITE_MED_RANGE_DEFAULT;
318
        pdata->tp_init_phase_rtn_lite_short        =
319
                        VL53L1_TUNINGPARM_INITIAL_PHASE_RTN_LITE_SHORT_RANGE_DEFAULT;
320
        pdata->tp_init_phase_ref_lite_long         =
321
                        VL53L1_TUNINGPARM_INITIAL_PHASE_REF_LITE_LONG_RANGE_DEFAULT;
322
        pdata->tp_init_phase_ref_lite_med          =
323
                        VL53L1_TUNINGPARM_INITIAL_PHASE_REF_LITE_MED_RANGE_DEFAULT;
324
        pdata->tp_init_phase_ref_lite_short        =
325
                        VL53L1_TUNINGPARM_INITIAL_PHASE_REF_LITE_SHORT_RANGE_DEFAULT;
326
        pdata->tp_consistency_lite_phase_tolerance =
327
                        VL53L1_TUNINGPARM_CONSISTENCY_LITE_PHASE_TOLERANCE_DEFAULT;
328
        pdata->tp_phasecal_target                  =
329
                        VL53L1_TUNINGPARM_PHASECAL_TARGET_DEFAULT;
330
        pdata->tp_cal_repeat_rate                  =
331
                        VL53L1_TUNINGPARM_LITE_CAL_REPEAT_RATE_DEFAULT;
332
        pdata->tp_lite_min_clip                    =
333
                        VL53L1_TUNINGPARM_LITE_MIN_CLIP_MM_DEFAULT;
334
        pdata->tp_lite_long_sigma_thresh_mm        =
335
                        VL53L1_TUNINGPARM_LITE_LONG_SIGMA_THRESH_MM_DEFAULT;
336
        pdata->tp_lite_med_sigma_thresh_mm         =
337
                        VL53L1_TUNINGPARM_LITE_MED_SIGMA_THRESH_MM_DEFAULT;
338
        pdata->tp_lite_short_sigma_thresh_mm       =
339
                        VL53L1_TUNINGPARM_LITE_SHORT_SIGMA_THRESH_MM_DEFAULT;
340
        pdata->tp_lite_long_min_count_rate_rtn_mcps  =
341
                        VL53L1_TUNINGPARM_LITE_LONG_MIN_COUNT_RATE_RTN_MCPS_DEFAULT;
342
        pdata->tp_lite_med_min_count_rate_rtn_mcps   =
343
                        VL53L1_TUNINGPARM_LITE_MED_MIN_COUNT_RATE_RTN_MCPS_DEFAULT;
344
        pdata->tp_lite_short_min_count_rate_rtn_mcps =
345
                        VL53L1_TUNINGPARM_LITE_SHORT_MIN_COUNT_RATE_RTN_MCPS_DEFAULT;
346
        pdata->tp_lite_sigma_est_pulse_width_ns      =
347
                        VL53L1_TUNINGPARM_LITE_SIGMA_EST_PULSE_WIDTH_DEFAULT;
348
        pdata->tp_lite_sigma_est_amb_width_ns        =
349
                        VL53L1_TUNINGPARM_LITE_SIGMA_EST_AMB_WIDTH_NS_DEFAULT;
350
        pdata->tp_lite_sigma_ref_mm                  =
351
                        VL53L1_TUNINGPARM_LITE_SIGMA_REF_MM_DEFAULT;
352
        pdata->tp_lite_seed_cfg                      =
353
                        VL53L1_TUNINGPARM_LITE_SEED_CONFIG_DEFAULT;
354
        pdata->tp_timed_seed_cfg                     =
355
                        VL53L1_TUNINGPARM_TIMED_SEED_CONFIG_DEFAULT;
356
        pdata->tp_lite_quantifier                    =
357
                        VL53L1_TUNINGPARM_LITE_QUANTIFIER_DEFAULT;
358
        pdata->tp_lite_first_order_select            =
359
                        VL53L1_TUNINGPARM_LITE_FIRST_ORDER_SELECT_DEFAULT;
360

    
361
        /* Preset Mode Configurations */
362
        /* - New parms added as part of Patch_TuningParmPresetModeAddition_11839 */
363

    
364
        pdata->tp_dss_target_lite_mcps               =
365
                        VL53L1_TUNINGPARM_LITE_DSS_CONFIG_TARGET_TOTAL_RATE_MCPS_DEFAULT;
366
        pdata->tp_dss_target_timed_mcps              =
367
                        VL53L1_TUNINGPARM_TIMED_DSS_CONFIG_TARGET_TOTAL_RATE_MCPS_DEFAULT;
368
        pdata->tp_phasecal_timeout_lite_us           =
369
                        VL53L1_TUNINGPARM_LITE_PHASECAL_CONFIG_TIMEOUT_US;
370
        pdata->tp_phasecal_timeout_timed_us          =
371
                        VL53L1_TUNINGPARM_TIMED_PHASECAL_CONFIG_TIMEOUT_US_DEFAULT;
372
        pdata->tp_mm_timeout_lite_us                 =
373
                        VL53L1_TUNINGPARM_LITE_MM_CONFIG_TIMEOUT_US_DEFAULT;
374
        pdata->tp_mm_timeout_timed_us                =
375
                        VL53L1_TUNINGPARM_TIMED_MM_CONFIG_TIMEOUT_US_DEFAULT;
376
        pdata->tp_range_timeout_lite_us              =
377
                        VL53L1_TUNINGPARM_LITE_RANGE_CONFIG_TIMEOUT_US_DEFAULT;
378
        pdata->tp_range_timeout_timed_us             =
379
                        VL53L1_TUNINGPARM_TIMED_RANGE_CONFIG_TIMEOUT_US_DEFAULT;
380

    
381
        /* Added for Patch_LowPowerAutoMode */
382

    
383
        pdata->tp_mm_timeout_lpa_us =
384
                        VL53L1_TUNINGPARM_LOWPOWERAUTO_MM_CONFIG_TIMEOUT_US_DEFAULT;
385
        pdata->tp_range_timeout_lpa_us =
386
                        VL53L1_TUNINGPARM_LOWPOWERAUTO_RANGE_CONFIG_TIMEOUT_US_DEFAULT;
387

    
388

    
389
        LOG_FUNCTION_END(status);
390

    
391
        return status;
392
}
393

    
394

    
395
VL53L1_Error VL53L1_preset_mode_standard_ranging(
396
        VL53L1_static_config_t    *pstatic,
397
        VL53L1_general_config_t   *pgeneral,
398
        VL53L1_timing_config_t    *ptiming,
399
        VL53L1_dynamic_config_t   *pdynamic,
400
        VL53L1_system_control_t   *psystem,
401
        VL53L1_tuning_parm_storage_t *ptuning_parms)
402
{
403
        /*
404
         * Initializes static and dynamic data structures fordevice preset mode
405
         * VL53L1_DEVICEPRESETMODE_STANDARD_RANGING
406
         *
407
         *  - streaming
408
         *  - single sigma delta
409
         *  - back to back
410
         *
411
         *  PLEASE NOTE THE SETTINGS BELOW AT PROVISIONAL AND WILL CHANGE!
412
         */
413

    
414
        VL53L1_Error  status = VL53L1_ERROR_NONE;
415

    
416
        LOG_FUNCTION_START("");
417

    
418
        /* Static Configuration */
419

    
420
        /* dss_config__target_total_rate_mcps = 20.0 Mcps 9.7 fp */
421
        pstatic->dss_config__target_total_rate_mcps               = 0x0A00;
422
        pstatic->debug__ctrl                                      = 0x00;
423
        pstatic->test_mode__ctrl                                  = 0x00;
424
        pstatic->clk_gating__ctrl                                 = 0x00;
425
        pstatic->nvm_bist__ctrl                                   = 0x00;
426
        pstatic->nvm_bist__num_nvm_words                          = 0x00;
427
        pstatic->nvm_bist__start_address                          = 0x00;
428
        pstatic->host_if__status                                  = 0x00;
429
        pstatic->pad_i2c_hv__config                               = 0x00;
430
        pstatic->pad_i2c_hv__extsup_config                        = 0x00;
431

    
432
        /*
433
         *  0 - gpio__extsup_hv
434
         *  1 - gpio__vmodeint_hv
435
         */
436
        pstatic->gpio_hv_pad__ctrl                                = 0x00;
437

    
438
        /*
439
         * Set interrupt active low
440
         *
441
         *  3:0 - gpio__mux_select_hv
442
         *    4 - gpio__mux_active_high_hv
443
         */
444
        pstatic->gpio_hv_mux__ctrl  = \
445
                        VL53L1_DEVICEINTERRUPTPOLARITY_ACTIVE_LOW | \
446
                        VL53L1_DEVICEGPIOMODE_OUTPUT_RANGE_AND_ERROR_INTERRUPTS;
447

    
448
        pstatic->gpio__tio_hv_status                              = 0x02;
449
        pstatic->gpio__fio_hv_status                              = 0x00;
450
        pstatic->ana_config__spad_sel_pswidth                     = 0x02;
451
        pstatic->ana_config__vcsel_pulse_width_offset             = 0x08;
452
        pstatic->ana_config__fast_osc__config_ctrl                = 0x00;
453

    
454
        pstatic->sigma_estimator__effective_pulse_width_ns        =
455
                        ptuning_parms->tp_lite_sigma_est_pulse_width_ns;
456
        pstatic->sigma_estimator__effective_ambient_width_ns      =
457
                        ptuning_parms->tp_lite_sigma_est_amb_width_ns;
458
        pstatic->sigma_estimator__sigma_ref_mm                    =
459
                        ptuning_parms->tp_lite_sigma_ref_mm;
460
        /* Minimum allowable value of 1 - 0 disables the feature */
461
        pstatic->algo__crosstalk_compensation_valid_height_mm     = 0x01;
462
        pstatic->spare_host_config__static_config_spare_0         = 0x00;
463
        pstatic->spare_host_config__static_config_spare_1         = 0x00;
464

    
465
        pstatic->algo__range_ignore_threshold_mcps                = 0x0000;
466

    
467
        /* set RIT distance to 20 mm */
468
        pstatic->algo__range_ignore_valid_height_mm               = 0xff;
469
        pstatic->algo__range_min_clip                             =
470
                        ptuning_parms->tp_lite_min_clip;
471
        /*
472
         * Phase consistency check limit - format 1.3 fp
473
         * 0x02 -> 0.25
474
         * 0x08 -> 1.00
475
         */
476
        pstatic->algo__consistency_check__tolerance               =
477
                        ptuning_parms->tp_consistency_lite_phase_tolerance;
478
        pstatic->spare_host_config__static_config_spare_2         = 0x00;
479
        pstatic->sd_config__reset_stages_msb                      = 0x00;
480
        pstatic->sd_config__reset_stages_lsb                      = 0x00;
481

    
482
        pgeneral->gph_config__stream_count_update_value           = 0x00;
483
        pgeneral->global_config__stream_divider                   = 0x00;
484
        pgeneral->system__interrupt_config_gpio =
485
                        VL53L1_INTERRUPT_CONFIG_NEW_SAMPLE_READY;
486
        pgeneral->cal_config__vcsel_start                         = 0x0B;
487

    
488
        /*
489
         * Set VHV / Phase Cal repeat rate to 1 every
490
         * 60 * 60 ranges (once every minute @ 60Hz)
491
         * 0 - disables
492
         * 12-bit value -> 4095 max
493
         */
494
        pgeneral->cal_config__repeat_rate                         =
495
                        ptuning_parms->tp_cal_repeat_rate;
496
        pgeneral->global_config__vcsel_width                      = 0x02;
497
        /* 13 macro periods gives a timeout of 1ms */
498
        pgeneral->phasecal_config__timeout_macrop                 = 0x0D;
499
        /* Phase cal target phase 2.0625 - 4.4 fp -> 0x21*/
500
        pgeneral->phasecal_config__target                         =
501
                        ptuning_parms->tp_phasecal_target;
502
        pgeneral->phasecal_config__override                       = 0x00;
503
        pgeneral->dss_config__roi_mode_control =
504
                        VL53L1_DEVICEDSSMODE__TARGET_RATE;
505
        /* format for threshold high and low is 9.7 fp */
506
        pgeneral->system__thresh_rate_high                        = 0x0000;
507
        pgeneral->system__thresh_rate_low                         = 0x0000;
508
        /* The format for manual effective spads is 8.8 -> 0x8C00 = 140.00 */
509
        pgeneral->dss_config__manual_effective_spads_select       = 0x8C00;
510
        pgeneral->dss_config__manual_block_select                 = 0x00;
511

    
512
        /*
513
         * Aperture attenuation value - format 0.8
514
         *
515
         * Nominal:  5x   -> 0.200000 * 256 = 51 = 0x33
516
         * Measured: 4.6x -> 0.217391 * 256 = 56 = 0x38
517
         */
518
        pgeneral->dss_config__aperture_attenuation                = 0x38;
519
        pgeneral->dss_config__max_spads_limit                     = 0xFF;
520
        pgeneral->dss_config__min_spads_limit                     = 0x01;
521

    
522
        /* Timing Configuration */
523

    
524
        /* Default timing of 2ms */
525
        ptiming->mm_config__timeout_macrop_a_hi                   = 0x00;
526
        ptiming->mm_config__timeout_macrop_a_lo                   = 0x1a;
527
        ptiming->mm_config__timeout_macrop_b_hi                   = 0x00;
528
        ptiming->mm_config__timeout_macrop_b_lo                   = 0x20;
529
        /* Setup for 30ms default */
530
        ptiming->range_config__timeout_macrop_a_hi                = 0x01;
531
        ptiming->range_config__timeout_macrop_a_lo                = 0xCC;
532
        /* register value 11 gives a 24 VCSEL period */
533
        ptiming->range_config__vcsel_period_a                     = 0x0B;
534
        /* Setup for 30ms default */
535
        ptiming->range_config__timeout_macrop_b_hi                = 0x01;
536
        ptiming->range_config__timeout_macrop_b_lo                = 0xF5;
537
        /* register value  09 gives a 20 VCSEL period */
538
        ptiming->range_config__vcsel_period_b                     = 0x09;
539
        /*
540
         * Sigma thresh register - format 14.2
541
         *
542
         * 0x003C -> 15.0 mm
543
         * 0x0050 -> 20.0 mm
544
         */
545
        ptiming->range_config__sigma_thresh                       =
546
                        ptuning_parms->tp_lite_med_sigma_thresh_mm;
547
        /*
548
         *  Rate Limit - format 9.7fp
549
         *  0x0020 -> 0.250 Mcps
550
         *  0x0080 -> 1.000 Mcps
551
         */
552
        ptiming->range_config__min_count_rate_rtn_limit_mcps      =
553
                        ptuning_parms->tp_lite_med_min_count_rate_rtn_mcps;
554

    
555
        /* Phase limit register formats = 5.3
556
         * low   = 0x08 ->  1.0
557
         * high  = 0x78 -> 15.0 -> 3.0m
558
         */
559
        ptiming->range_config__valid_phase_low                    = 0x08;
560
        ptiming->range_config__valid_phase_high                   = 0x78;
561
        ptiming->system__intermeasurement_period                  = 0x00000000;
562
        ptiming->system__fractional_enable                        = 0x00;
563

    
564
        /* Dynamic Configuration */
565

    
566
        pdynamic->system__grouped_parameter_hold_0                 = 0x01;
567

    
568
        pdynamic->system__thresh_high                              = 0x0000;
569
        pdynamic->system__thresh_low                               = 0x0000;
570
        pdynamic->system__enable_xtalk_per_quadrant                = 0x00;
571
        pdynamic->system__seed_config =
572
                        ptuning_parms->tp_lite_seed_cfg;
573

    
574
        /* Timing A */
575
        pdynamic->sd_config__woi_sd0                               = 0x0B;
576
        /* Timing B */
577
        pdynamic->sd_config__woi_sd1                               = 0x09;
578

    
579
        pdynamic->sd_config__initial_phase_sd0                     =
580
                        ptuning_parms->tp_init_phase_rtn_lite_med;
581
        pdynamic->sd_config__initial_phase_sd1                     =
582
                        ptuning_parms->tp_init_phase_ref_lite_med;;
583

    
584
        pdynamic->system__grouped_parameter_hold_1                 = 0x01;
585

    
586
        /*
587
         *  Quantifier settings
588
         *
589
         *  sd_config__first_order_select
590
         *     bit 0 - return sigma delta
591
         *     bit 1 - reference sigma delta
592
         *
593
         *  sd_config__first_order_select = 0x03 (1st order)
594
         *
595
         *      sd_config__quantifier options
596
         *        0
597
         *        1 ->   64
598
         *        2 ->  128
599
         *        3 ->  256
600
         *
601
         *  sd_config__first_order_select = 0x00 (2nd order)
602
         *
603
         *      sd_config__quantifier options
604
         *        0
605
         *        1  ->  256
606
         *        2  -> 1024
607
         *        3  -> 4095
608
         *
609
         *  Setting below 2nd order, Quantifier = 1024
610
         */
611

    
612
        pdynamic->sd_config__first_order_select =
613
                        ptuning_parms->tp_lite_first_order_select;
614
        pdynamic->sd_config__quantifier         =
615
                        ptuning_parms->tp_lite_quantifier;
616

    
617
        /* Below defaults will be overwritten by zone_cfg
618
         * Spad no = 199 (0xC7)
619
         * Spad no =  63 (0x3F)
620
         */
621
        pdynamic->roi_config__user_roi_centre_spad              = 0xC7;
622
        /* 16x16 ROI */
623
        pdynamic->roi_config__user_roi_requested_global_xy_size = 0xFF;
624

    
625

    
626
        pdynamic->system__sequence_config                          = \
627
                        VL53L1_SEQUENCE_VHV_EN | \
628
                        VL53L1_SEQUENCE_PHASECAL_EN | \
629
                        VL53L1_SEQUENCE_DSS1_EN | \
630
                        VL53L1_SEQUENCE_DSS2_EN | \
631
                        VL53L1_SEQUENCE_MM2_EN | \
632
                        VL53L1_SEQUENCE_RANGE_EN;
633

    
634
        pdynamic->system__grouped_parameter_hold                   = 0x02;
635

    
636
        /* System control */
637

    
638

    
639
        psystem->system__stream_count_ctrl                         = 0x00;
640
        psystem->firmware__enable                                  = 0x01;
641
        psystem->system__interrupt_clear                           = \
642
                        VL53L1_CLEAR_RANGE_INT;
643

    
644
        psystem->system__mode_start                                = \
645
                        VL53L1_DEVICESCHEDULERMODE_STREAMING | \
646
                        VL53L1_DEVICEREADOUTMODE_SINGLE_SD | \
647
                        VL53L1_DEVICEMEASUREMENTMODE_BACKTOBACK;
648

    
649
        LOG_FUNCTION_END(status);
650

    
651
        return status;
652
}
653

    
654

    
655
VL53L1_Error VL53L1_preset_mode_standard_ranging_short_range(
656
        VL53L1_static_config_t    *pstatic,
657
        VL53L1_general_config_t   *pgeneral,
658
        VL53L1_timing_config_t    *ptiming,
659
        VL53L1_dynamic_config_t   *pdynamic,
660
        VL53L1_system_control_t   *psystem,
661
        VL53L1_tuning_parm_storage_t *ptuning_parms)
662
{
663
        /*
664
         * Initializes static and dynamic data structures for
665
         * device preset mode
666
         *
667
         * VL53L1_DEVICEPRESETMODE_STANDARD_RANGING_SHORT_RANGE
668
         * (up to 1.4 metres)
669
         *
670
         * PLEASE NOTE THE SETTINGS BELOW AT PROVISIONAL AND WILL CHANGE!
671
         */
672

    
673
        VL53L1_Error  status = VL53L1_ERROR_NONE;
674

    
675
        LOG_FUNCTION_START("");
676

    
677
        /* Call standard ranging configuration followed by
678
         * overrides for the  short range configuration
679
         */
680

    
681
        status = VL53L1_preset_mode_standard_ranging(
682
                pstatic,
683
                pgeneral,
684
                ptiming,
685
                pdynamic,
686
                psystem,
687
                ptuning_parms);
688

    
689
        /* now override standard ranging specific registers */
690

    
691
        if (status == VL53L1_ERROR_NONE) {
692

    
693
                /* Timing Configuration
694
                 *
695
                 * vcsel_period_a    = 7 -> 16 period
696
                 * vcsel_period_b    = 5 -> 12 period
697
                 * sigma_thresh                  = 0x003C -> 14.2fp -> 15.0 mm
698
                 * min_count_rate_rtn_limit_mcps = 0x0080 ->  9.7fp ->  1.0 Mcps
699
                 * valid_phase_low               = 0x08 -> 5.3fp -> 1.0
700
                 * valid_phase_high              = 0x38 -> 5.3fp -> 7.0 -> 1.4m
701
                 */
702

    
703
                ptiming->range_config__vcsel_period_a                = 0x07;
704
                ptiming->range_config__vcsel_period_b                = 0x05;
705
                ptiming->range_config__sigma_thresh                  =
706
                                ptuning_parms->tp_lite_short_sigma_thresh_mm;
707
                ptiming->range_config__min_count_rate_rtn_limit_mcps =
708
                                ptuning_parms->tp_lite_short_min_count_rate_rtn_mcps;
709
                ptiming->range_config__valid_phase_low               = 0x08;
710
                ptiming->range_config__valid_phase_high              = 0x38;
711

    
712
                /* Dynamic Configuration
713
                 * SD0 -> Timing A
714
                 * SD1 -> Timing B
715
                 */
716

    
717
                pdynamic->sd_config__woi_sd0                         = 0x07;
718
                pdynamic->sd_config__woi_sd1                         = 0x05;
719
                pdynamic->sd_config__initial_phase_sd0               =
720
                                ptuning_parms->tp_init_phase_rtn_lite_short;
721
                pdynamic->sd_config__initial_phase_sd1               =
722
                                ptuning_parms->tp_init_phase_ref_lite_short;
723
        }
724

    
725
        LOG_FUNCTION_END(status);
726

    
727
        return status;
728
}
729

    
730

    
731
VL53L1_Error VL53L1_preset_mode_standard_ranging_long_range(
732
        VL53L1_static_config_t    *pstatic,
733
        VL53L1_general_config_t   *pgeneral,
734
        VL53L1_timing_config_t    *ptiming,
735
        VL53L1_dynamic_config_t   *pdynamic,
736
        VL53L1_system_control_t   *psystem,
737
        VL53L1_tuning_parm_storage_t *ptuning_parms)
738
{
739
        /*
740
         * Initializes static and dynamic data structures for
741
         * device preset mode
742
         *
743
         * VL53L1_DEVICEPRESETMODE_STANDARD_RANGING_LONG_RANGE
744
         * (up to 4.8 metres)
745
         *
746
         *  PLEASE NOTE THE SETTINGS BELOW AT PROVISIONAL AND WILL CHANGE!
747
         */
748

    
749
        VL53L1_Error  status = VL53L1_ERROR_NONE;
750

    
751
        LOG_FUNCTION_START("");
752

    
753
        /* Call standard ranging configuration with
754
         * overrides for long range configuration
755
         */
756

    
757
        status = VL53L1_preset_mode_standard_ranging(
758
                pstatic,
759
                pgeneral,
760
                ptiming,
761
                pdynamic,
762
                psystem,
763
                ptuning_parms);
764

    
765
        /* now override standard ranging specific registers */
766

    
767
        if (status == VL53L1_ERROR_NONE) {
768

    
769
                /* Timing Configuration
770
                 *
771
                 * vcsel_period_a    = 15 -> 32 period
772
                 * vcsel_period_b    = 13 -> 28 period
773
                 * sigma_thresh                  = 0x003C -> 14.2fp -> 15.0 mm
774
                 * min_count_rate_rtn_limit_mcps = 0x0080 ->  9.7fp ->  1.0 Mcps
775
                 * valid_phase_low               = 0x08 -> 5.3fp ->  1.0
776
                 * valid_phase_high              = 0xB8 -> 5.3fp -> 23.0 -> 4.6m
777
                 */
778

    
779
                ptiming->range_config__vcsel_period_a                = 0x0F;
780
                ptiming->range_config__vcsel_period_b                = 0x0D;
781
                ptiming->range_config__sigma_thresh                  =
782
                                ptuning_parms->tp_lite_long_sigma_thresh_mm;
783
                ptiming->range_config__min_count_rate_rtn_limit_mcps =
784
                                ptuning_parms->tp_lite_long_min_count_rate_rtn_mcps;
785
                ptiming->range_config__valid_phase_low               = 0x08;
786
                ptiming->range_config__valid_phase_high              = 0xB8;
787

    
788
                /* Dynamic Configuration
789
                 * SD0 -> Timing A
790
                 * SD1 -> Timing B
791
                 */
792

    
793
                pdynamic->sd_config__woi_sd0                         = 0x0F;
794
                pdynamic->sd_config__woi_sd1                         = 0x0D;
795
                pdynamic->sd_config__initial_phase_sd0               =
796
                                ptuning_parms->tp_init_phase_rtn_lite_long;
797
                pdynamic->sd_config__initial_phase_sd1               =
798
                                ptuning_parms->tp_init_phase_ref_lite_long;
799
        }
800

    
801
        LOG_FUNCTION_END(status);
802

    
803
        return status;
804
}
805

    
806

    
807
#ifndef VL53L1_NOCALIB
808
VL53L1_Error VL53L1_preset_mode_standard_ranging_mm1_cal(
809
        VL53L1_static_config_t    *pstatic,
810
        VL53L1_general_config_t   *pgeneral,
811
        VL53L1_timing_config_t    *ptiming,
812
        VL53L1_dynamic_config_t   *pdynamic,
813
        VL53L1_system_control_t   *psystem,
814
        VL53L1_tuning_parm_storage_t *ptuning_parms)
815
{
816
        /*
817
         * Initializes static and dynamic data structures for
818
         * device preset mode
819
         *
820
         * VL53L1_DEVICEPRESETMODE_STANDARD_RANGING_MM1_CAL
821
         *
822
         * PLEASE NOTE THE SETTINGS BELOW AT PROVISIONAL AND WILL CHANGE!
823
         */
824

    
825
        VL53L1_Error  status = VL53L1_ERROR_NONE;
826

    
827
        LOG_FUNCTION_START("");
828

    
829
        /* Call standard ranging configuration with
830
         * overrides for long range configuration
831
         */
832

    
833
        status = VL53L1_preset_mode_standard_ranging(
834
                pstatic,
835
                pgeneral,
836
                ptiming,
837
                pdynamic,
838
                psystem,
839
                ptuning_parms);
840

    
841
        /* now override standard ranging specific registers */
842

    
843
        if (status == VL53L1_ERROR_NONE) {
844

    
845
                pgeneral->dss_config__roi_mode_control =
846
                                VL53L1_DEVICEDSSMODE__REQUESTED_EFFFECTIVE_SPADS;
847

    
848
                pdynamic->system__sequence_config  = \
849
                                VL53L1_SEQUENCE_VHV_EN | \
850
                                VL53L1_SEQUENCE_PHASECAL_EN | \
851
                                VL53L1_SEQUENCE_DSS1_EN | \
852
                                VL53L1_SEQUENCE_DSS2_EN | \
853
                                VL53L1_SEQUENCE_MM1_EN;
854
        }
855

    
856
        LOG_FUNCTION_END(status);
857

    
858
        return status;
859
}
860

    
861

    
862
VL53L1_Error VL53L1_preset_mode_standard_ranging_mm2_cal(
863
        VL53L1_static_config_t    *pstatic,
864
        VL53L1_general_config_t   *pgeneral,
865
        VL53L1_timing_config_t    *ptiming,
866
        VL53L1_dynamic_config_t   *pdynamic,
867
        VL53L1_system_control_t   *psystem,
868
        VL53L1_tuning_parm_storage_t *ptuning_parms)
869
{
870
        /*
871
         * Initializes static and dynamic data structures for
872
         * device preset mode
873
         *
874
         * VL53L1_DEVICEPRESETMODE_STANDARD_RANGING_MM2_CAL
875
         *
876
         * PLEASE NOTE THE SETTINGS BELOW AT PROVISIONAL AND WILL CHANGE!
877
         */
878

    
879
        VL53L1_Error  status = VL53L1_ERROR_NONE;
880

    
881
        LOG_FUNCTION_START("");
882

    
883
        /* Call standard ranging configuration with
884
         * overrides for long range configuration
885
         */
886

    
887
        status = VL53L1_preset_mode_standard_ranging(
888
                pstatic,
889
                pgeneral,
890
                ptiming,
891
                pdynamic,
892
                psystem,
893
                ptuning_parms);
894

    
895
        /* now override standard ranging specific registers */
896

    
897
        if (status == VL53L1_ERROR_NONE) {
898

    
899
                pgeneral->dss_config__roi_mode_control =
900
                                VL53L1_DEVICEDSSMODE__REQUESTED_EFFFECTIVE_SPADS;
901

    
902
                pdynamic->system__sequence_config  = \
903
                                VL53L1_SEQUENCE_VHV_EN | \
904
                                VL53L1_SEQUENCE_PHASECAL_EN | \
905
                                VL53L1_SEQUENCE_DSS1_EN | \
906
                                VL53L1_SEQUENCE_DSS2_EN | \
907
                                VL53L1_SEQUENCE_MM2_EN;
908
        }
909

    
910
        LOG_FUNCTION_END(status);
911

    
912
        return status;
913
}
914
#endif
915

    
916

    
917
VL53L1_Error VL53L1_preset_mode_timed_ranging(
918

    
919
        VL53L1_static_config_t    *pstatic,
920
        VL53L1_general_config_t   *pgeneral,
921
        VL53L1_timing_config_t    *ptiming,
922
        VL53L1_dynamic_config_t   *pdynamic,
923
        VL53L1_system_control_t   *psystem,
924
        VL53L1_tuning_parm_storage_t *ptuning_parms)
925
{
926
        /*
927
        * Initializes static and dynamic data structures for
928
        * device preset mode
929
        *
930
        * VL53L1_DEVICEPRESETMODE_TIMED_RANGING
931
        *
932
        *  - pseudo-solo
933
        *  - single sigma delta
934
        *  - timed
935
        *
936
        *  PLEASE NOTE THE SETTINGS BELOW AT PROVISIONAL AND WILL CHANGE!
937
        */
938

    
939
        VL53L1_Error  status = VL53L1_ERROR_NONE;
940

    
941
        LOG_FUNCTION_START("");
942

    
943
        /* Call standard ranging configuration */
944

    
945
        status = VL53L1_preset_mode_standard_ranging(
946
                                        pstatic,
947
                                        pgeneral,
948
                                        ptiming,
949
                                        pdynamic,
950
                                        psystem,
951
                                        ptuning_parms);
952

    
953
        /* now override standard ranging specific registers */
954

    
955
        if (status == VL53L1_ERROR_NONE) {
956

    
957
                /* Dynamic Configuration */
958

    
959
                /* Disable GPH  */
960
                pdynamic->system__grouped_parameter_hold = 0x00;
961

    
962
                /* Re-Configure timing budget default for 13ms */
963
                ptiming->range_config__timeout_macrop_a_hi                = 0x00;
964
                ptiming->range_config__timeout_macrop_a_lo                = 0xB1;
965
                /* Setup for 13ms default */
966
                ptiming->range_config__timeout_macrop_b_hi                = 0x00;
967
                ptiming->range_config__timeout_macrop_b_lo                = 0xD4;
968

    
969
                /* Timing Configuration */
970

    
971
                ptiming->system__intermeasurement_period = 0x00000600;
972
                pdynamic->system__seed_config =
973
                                ptuning_parms->tp_timed_seed_cfg;
974

    
975
                /* System control */
976

    
977
                /* Configure Timed/Psuedo-solo mode */
978
                psystem->system__mode_start =
979
                                VL53L1_DEVICESCHEDULERMODE_PSEUDO_SOLO | \
980
                                VL53L1_DEVICEREADOUTMODE_SINGLE_SD     | \
981
                                VL53L1_DEVICEMEASUREMENTMODE_TIMED;
982
        }
983

    
984
        LOG_FUNCTION_END(status);
985

    
986
        return status;
987
}
988

    
989
VL53L1_Error VL53L1_preset_mode_timed_ranging_short_range(
990

    
991
        VL53L1_static_config_t    *pstatic,
992
        VL53L1_general_config_t   *pgeneral,
993
        VL53L1_timing_config_t    *ptiming,
994
        VL53L1_dynamic_config_t   *pdynamic,
995
        VL53L1_system_control_t   *psystem,
996
        VL53L1_tuning_parm_storage_t *ptuning_parms)
997
{
998
        /*
999
        * Initializes static and dynamic data structures for
1000
        * device preset mode
1001
        *
1002
        * VL53L1_DEVICEPRESETMODE_TIMED_RANGING_SHORT_RANGE
1003
        *
1004
        *  - pseudo-solo
1005
        *  - single sigma delta
1006
        *  - timed
1007
        *
1008
        *  PLEASE NOTE THE SETTINGS BELOW AT PROVISIONAL AND WILL CHANGE!
1009
        */
1010

    
1011
        VL53L1_Error  status = VL53L1_ERROR_NONE;
1012

    
1013
        LOG_FUNCTION_START("");
1014

    
1015
        /* Call standard ranging configuration */
1016

    
1017
        status = VL53L1_preset_mode_standard_ranging_short_range(
1018
                                        pstatic,
1019
                                        pgeneral,
1020
                                        ptiming,
1021
                                        pdynamic,
1022
                                        psystem,
1023
                                        ptuning_parms);
1024

    
1025
        /* now override standard ranging specific registers */
1026

    
1027
        if (status == VL53L1_ERROR_NONE) {
1028

    
1029
                /* Dynamic Configuration */
1030

    
1031
                /* Disable GPH  */
1032
                pdynamic->system__grouped_parameter_hold = 0x00;
1033

    
1034

    
1035
                /* Timing Configuration */
1036

    
1037
                /* Re-Configure timing budget default for 13ms */
1038
                ptiming->range_config__timeout_macrop_a_hi                = 0x01;
1039
                ptiming->range_config__timeout_macrop_a_lo                = 0x84;
1040
                /* Setup for 13ms default */
1041
                ptiming->range_config__timeout_macrop_b_hi                = 0x01;
1042
                ptiming->range_config__timeout_macrop_b_lo                = 0xB1;
1043

    
1044
                ptiming->system__intermeasurement_period = 0x00000600;
1045
                pdynamic->system__seed_config =
1046
                                ptuning_parms->tp_timed_seed_cfg;
1047

    
1048
                /* System control */
1049

    
1050
                /* Configure Timed/Psuedo-solo mode */
1051
                psystem->system__mode_start =
1052
                                VL53L1_DEVICESCHEDULERMODE_PSEUDO_SOLO | \
1053
                                VL53L1_DEVICEREADOUTMODE_SINGLE_SD     | \
1054
                                VL53L1_DEVICEMEASUREMENTMODE_TIMED;
1055
        }
1056

    
1057
        LOG_FUNCTION_END(status);
1058

    
1059
        return status;
1060
}
1061

    
1062
VL53L1_Error VL53L1_preset_mode_timed_ranging_long_range(
1063

    
1064
        VL53L1_static_config_t    *pstatic,
1065
        VL53L1_general_config_t   *pgeneral,
1066
        VL53L1_timing_config_t    *ptiming,
1067
        VL53L1_dynamic_config_t   *pdynamic,
1068
        VL53L1_system_control_t   *psystem,
1069
        VL53L1_tuning_parm_storage_t *ptuning_parms)
1070
{
1071
        /*
1072
        * Initializes static and dynamic data structures for
1073
        * device preset mode
1074
        *
1075
        * VL53L1_DEVICEPRESETMODE_TIMED_RANGING_LONG_RANGE
1076
        *
1077
        *  - pseudo-solo
1078
        *  - single sigma delta
1079
        *  - timed
1080
        *
1081
        *  PLEASE NOTE THE SETTINGS BELOW AT PROVISIONAL AND WILL CHANGE!
1082
        */
1083

    
1084
        VL53L1_Error  status = VL53L1_ERROR_NONE;
1085

    
1086
        LOG_FUNCTION_START("");
1087

    
1088
        /* Call standard ranging configuration */
1089

    
1090
        status = VL53L1_preset_mode_standard_ranging_long_range(
1091
                                        pstatic,
1092
                                        pgeneral,
1093
                                        ptiming,
1094
                                        pdynamic,
1095
                                        psystem,
1096
                                        ptuning_parms);
1097

    
1098
        /* now override standard ranging specific registers */
1099

    
1100
        if (status == VL53L1_ERROR_NONE) {
1101

    
1102
                /* Dynamic Configuration */
1103

    
1104
                /* Disable GPH  */
1105
                pdynamic->system__grouped_parameter_hold = 0x00;
1106

    
1107

    
1108
                /* Timing Configuration */
1109

    
1110
                /* Re-Configure timing budget default for 13ms */
1111
                ptiming->range_config__timeout_macrop_a_hi                = 0x00;
1112
                ptiming->range_config__timeout_macrop_a_lo                = 0x97;
1113
                /* Setup for 13ms default */
1114
                ptiming->range_config__timeout_macrop_b_hi                = 0x00;
1115
                ptiming->range_config__timeout_macrop_b_lo                = 0xB1;
1116

    
1117
                ptiming->system__intermeasurement_period = 0x00000600;
1118
                pdynamic->system__seed_config =
1119
                                ptuning_parms->tp_timed_seed_cfg;
1120

    
1121
                /* System control */
1122

    
1123
                /* Configure Timed/Psuedo-solo mode */
1124
                psystem->system__mode_start =
1125
                                VL53L1_DEVICESCHEDULERMODE_PSEUDO_SOLO | \
1126
                                VL53L1_DEVICEREADOUTMODE_SINGLE_SD     | \
1127
                                VL53L1_DEVICEMEASUREMENTMODE_TIMED;
1128
        }
1129

    
1130
        LOG_FUNCTION_END(status);
1131

    
1132
        return status;
1133
}
1134

    
1135
/* Start Patch_LowPowerAutoMode */
1136
VL53L1_Error VL53L1_preset_mode_low_power_auto_ranging(
1137

    
1138
        VL53L1_static_config_t    *pstatic,
1139
        VL53L1_general_config_t   *pgeneral,
1140
        VL53L1_timing_config_t    *ptiming,
1141
        VL53L1_dynamic_config_t   *pdynamic,
1142
        VL53L1_system_control_t   *psystem,
1143
        VL53L1_tuning_parm_storage_t *ptuning_parms,
1144
        VL53L1_low_power_auto_data_t *plpadata)
1145
{
1146
        /*
1147
        * Initializes static and dynamic data structures for
1148
        * device preset mode
1149
        *
1150
        * VL53L1_DEVICEPRESETMODE_LOWPOWERAUTO_MEDIUM_RANGE
1151
        *
1152
        *  - pseudo-solo
1153
        *  - single sigma delta
1154
        *  - timed
1155
        *  - special low power auto mode for Presence application
1156
        *
1157
        *  PLEASE NOTE THE SETTINGS BELOW ARE PROVISIONAL AND WILL CHANGE!
1158
        */
1159

    
1160
        VL53L1_Error  status = VL53L1_ERROR_NONE;
1161

    
1162
        LOG_FUNCTION_START("");
1163

    
1164
        /* Call standard ranging configuration */
1165

    
1166
        status = VL53L1_preset_mode_timed_ranging(
1167
                                        pstatic,
1168
                                        pgeneral,
1169
                                        ptiming,
1170
                                        pdynamic,
1171
                                        psystem,
1172
                                        ptuning_parms);
1173

    
1174
        /* now setup the low power auto mode */
1175

    
1176
        if (status == VL53L1_ERROR_NONE) {
1177
                status = VL53L1_config_low_power_auto_mode(
1178
                                pgeneral,
1179
                                pdynamic,
1180
                                plpadata
1181
                                );
1182
        }
1183

    
1184
        LOG_FUNCTION_END(status);
1185

    
1186
        return status;
1187
}
1188

    
1189
VL53L1_Error VL53L1_preset_mode_low_power_auto_short_ranging(
1190

    
1191
        VL53L1_static_config_t    *pstatic,
1192
        VL53L1_general_config_t   *pgeneral,
1193
        VL53L1_timing_config_t    *ptiming,
1194
        VL53L1_dynamic_config_t   *pdynamic,
1195
        VL53L1_system_control_t   *psystem,
1196
        VL53L1_tuning_parm_storage_t *ptuning_parms,
1197
        VL53L1_low_power_auto_data_t *plpadata)
1198
{
1199
        /*
1200
        * Initializes static and dynamic data structures for
1201
        * device preset mode
1202
        *
1203
        * VL53L1_DEVICEPRESETMODE_LOWPOWERAUTO_SHORT_RANGE
1204
        *
1205
        *  - pseudo-solo
1206
        *  - single sigma delta
1207
        *  - timed
1208
        *  - special low power auto mode for Presence application
1209
        *
1210
        *  PLEASE NOTE THE SETTINGS BELOW ARE PROVISIONAL AND WILL CHANGE!
1211
        */
1212

    
1213
        VL53L1_Error  status = VL53L1_ERROR_NONE;
1214

    
1215
        LOG_FUNCTION_START("");
1216

    
1217
        /* Call standard ranging configuration */
1218

    
1219
        status = VL53L1_preset_mode_timed_ranging_short_range(
1220
                                        pstatic,
1221
                                        pgeneral,
1222
                                        ptiming,
1223
                                        pdynamic,
1224
                                        psystem,
1225
                                        ptuning_parms);
1226

    
1227
        /* now setup the low power auto mode */
1228

    
1229
        if (status == VL53L1_ERROR_NONE) {
1230
                status = VL53L1_config_low_power_auto_mode(
1231
                                pgeneral,
1232
                                pdynamic,
1233
                                plpadata
1234
                                );
1235
        }
1236

    
1237
        LOG_FUNCTION_END(status);
1238

    
1239
        return status;
1240
}
1241

    
1242
VL53L1_Error VL53L1_preset_mode_low_power_auto_long_ranging(
1243

    
1244
        VL53L1_static_config_t    *pstatic,
1245
        VL53L1_general_config_t   *pgeneral,
1246
        VL53L1_timing_config_t    *ptiming,
1247
        VL53L1_dynamic_config_t   *pdynamic,
1248
        VL53L1_system_control_t   *psystem,
1249
        VL53L1_tuning_parm_storage_t *ptuning_parms,
1250
        VL53L1_low_power_auto_data_t *plpadata)
1251
{
1252
        /*
1253
        * Initializes static and dynamic data structures for
1254
        * device preset mode
1255
        *
1256
        * VL53L1_DEVICEPRESETMODE_LOWPOWERAUTO_LONG_RANGE
1257
        *
1258
        *  - pseudo-solo
1259
        *  - single sigma delta
1260
        *  - timed
1261
        *  - special low power auto mode for Presence application
1262
        *
1263
        *  PLEASE NOTE THE SETTINGS BELOW ARE PROVISIONAL AND WILL CHANGE!
1264
        */
1265

    
1266
        VL53L1_Error  status = VL53L1_ERROR_NONE;
1267

    
1268
        LOG_FUNCTION_START("");
1269

    
1270
        /* Call standard ranging configuration */
1271

    
1272
        status = VL53L1_preset_mode_timed_ranging_long_range(
1273
                                        pstatic,
1274
                                        pgeneral,
1275
                                        ptiming,
1276
                                        pdynamic,
1277
                                        psystem,
1278
                                        ptuning_parms);
1279

    
1280
        /* now setup the low power auto mode */
1281

    
1282
        if (status == VL53L1_ERROR_NONE) {
1283
                status = VL53L1_config_low_power_auto_mode(
1284
                                pgeneral,
1285
                                pdynamic,
1286
                                plpadata
1287
                                );
1288
        }
1289

    
1290
        LOG_FUNCTION_END(status);
1291

    
1292
        return status;
1293
}
1294

    
1295
/* End Patch_LowPowerAutoMode */
1296

    
1297
VL53L1_Error VL53L1_preset_mode_singleshot_ranging(
1298

    
1299
        VL53L1_static_config_t    *pstatic,
1300
        VL53L1_general_config_t   *pgeneral,
1301
        VL53L1_timing_config_t    *ptiming,
1302
        VL53L1_dynamic_config_t   *pdynamic,
1303
        VL53L1_system_control_t   *psystem,
1304
        VL53L1_tuning_parm_storage_t *ptuning_parms)
1305
{
1306
        /*
1307
        * Initializes static and dynamic data structures for device preset mode
1308
        * VL53L1_DEVICEPRESETMODE_TIMED_RANGING
1309
        *
1310
        *  - pseudo-solo
1311
        *  - single sigma delta
1312
        *  - timed
1313
        *
1314
        *  PLEASE NOTE THE SETTINGS BELOW AT PROVISIONAL AND WILL CHANGE!
1315
        */
1316

    
1317
        VL53L1_Error  status = VL53L1_ERROR_NONE;
1318

    
1319
        LOG_FUNCTION_START("");
1320

    
1321
        /* Call standard ranging configuration */
1322

    
1323
        status = VL53L1_preset_mode_standard_ranging(
1324
                pstatic,
1325
                pgeneral,
1326
                ptiming,
1327
                pdynamic,
1328
                psystem,
1329
                ptuning_parms);
1330

    
1331
        /* now override standard ranging specific registers */
1332

    
1333
        if (status == VL53L1_ERROR_NONE) {
1334

    
1335
                /* Dynamic Configuration */
1336

    
1337
                /* Disable GPH  */
1338
                pdynamic->system__grouped_parameter_hold = 0x00;
1339

    
1340
                /* Timing Configuration */
1341

    
1342
                /* Re-Configure timing budget default for 13ms */
1343
                ptiming->range_config__timeout_macrop_a_hi                = 0x00;
1344
                ptiming->range_config__timeout_macrop_a_lo                = 0xB1;
1345
                /* Setup for 13ms default */
1346
                ptiming->range_config__timeout_macrop_b_hi                = 0x00;
1347
                ptiming->range_config__timeout_macrop_b_lo                = 0xD4;
1348

    
1349
                pdynamic->system__seed_config =
1350
                                ptuning_parms->tp_timed_seed_cfg;
1351

    
1352
                /* System control */
1353

    
1354
                /* Configure Timed/Psuedo-solo mode */
1355
                psystem->system__mode_start = \
1356
                                VL53L1_DEVICESCHEDULERMODE_PSEUDO_SOLO | \
1357
                                VL53L1_DEVICEREADOUTMODE_SINGLE_SD     | \
1358
                                VL53L1_DEVICEMEASUREMENTMODE_SINGLESHOT;
1359
        }
1360

    
1361
        LOG_FUNCTION_END(status);
1362

    
1363
        return status;
1364
}
1365

    
1366

    
1367
VL53L1_Error VL53L1_preset_mode_olt(
1368
        VL53L1_static_config_t    *pstatic,
1369
        VL53L1_general_config_t   *pgeneral,
1370
        VL53L1_timing_config_t    *ptiming,
1371
        VL53L1_dynamic_config_t   *pdynamic,
1372
        VL53L1_system_control_t   *psystem,
1373
        VL53L1_tuning_parm_storage_t *ptuning_parms)
1374
{
1375
        /**
1376
         * Initializes static and dynamic data structures for device preset mode
1377
         * VL53L1_DEVICEPRESETMODE_OLT
1378
         *
1379
         *  PLEASE NOTE THE SETTINGS BELOW AT PROVISIONAL AND WILL CHANGE!
1380
         */
1381

    
1382
        VL53L1_Error  status = VL53L1_ERROR_NONE;
1383

    
1384
        LOG_FUNCTION_START("");
1385

    
1386
        /* Call standard ranging configuration */
1387

    
1388
        status = VL53L1_preset_mode_standard_ranging(
1389
                                        pstatic,
1390
                                        pgeneral,
1391
                                        ptiming,
1392
                                        pdynamic,
1393
                                        psystem,
1394
                                        ptuning_parms);
1395

    
1396
        /* now override OLT specific registers */
1397

    
1398
        if (status == VL53L1_ERROR_NONE) {
1399

    
1400
                /* Disables requirement for host handshake */
1401
                psystem->system__stream_count_ctrl  = 0x01;
1402
        }
1403

    
1404
        LOG_FUNCTION_END(status);
1405

    
1406
        return status;
1407
}