The VL53L1CB proximity sensor, based on ST’s FlightSense™, Time-of-Flight technology.

Dependencies:   X_NUCLEO_COMMON ST_INTERFACES

Dependents:   VL53L1CB_noshield_1sensor_polls_auton VL53L1CB_noshield_1sensor_interrupt_auton X_NUCLEO_53L1A2

Based on VL53L1 library, this is a library for the VL53L1CB ToF chip.

Committer:
charlesmn
Date:
Fri Nov 06 10:06:37 2020 +0000
Revision:
0:3ac96e360672
Child:
7:1add29d51e72
Library for ST Vl53L1A1 time of flight sensor.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
charlesmn 0:3ac96e360672 1
charlesmn 0:3ac96e360672 2 /*******************************************************************************
charlesmn 0:3ac96e360672 3 This file is part of VL53L1 Protected
charlesmn 0:3ac96e360672 4
charlesmn 0:3ac96e360672 5 Copyright (c) 2020, STMicroelectronics - All Rights Reserved
charlesmn 0:3ac96e360672 6
charlesmn 0:3ac96e360672 7 License terms: STMicroelectronics Proprietary in accordance with licensing
charlesmn 0:3ac96e360672 8 terms at www.st.com/sla0081
charlesmn 0:3ac96e360672 9
charlesmn 0:3ac96e360672 10 STMicroelectronics confidential
charlesmn 0:3ac96e360672 11 Reproduction and Communication of this document is strictly prohibited unless
charlesmn 0:3ac96e360672 12 specifically authorized in writing by STMicroelectronics.
charlesmn 0:3ac96e360672 13
charlesmn 0:3ac96e360672 14 */
charlesmn 0:3ac96e360672 15
charlesmn 0:3ac96e360672 16
charlesmn 0:3ac96e360672 17
charlesmn 0:3ac96e360672 18
charlesmn 0:3ac96e360672 19
charlesmn 0:3ac96e360672 20
charlesmn 0:3ac96e360672 21
charlesmn 0:3ac96e360672 22 #include "vl53l1_types.h"
charlesmn 0:3ac96e360672 23 #include "vl53l1_platform_log.h"
charlesmn 0:3ac96e360672 24
charlesmn 0:3ac96e360672 25 #include "vl53l1_core_support.h"
charlesmn 0:3ac96e360672 26 #include "vl53l1_error_codes.h"
charlesmn 0:3ac96e360672 27 #include "vl53l1_ll_def.h"
charlesmn 0:3ac96e360672 28
charlesmn 0:3ac96e360672 29 #include "vl53l1_hist_funcs.h"
charlesmn 0:3ac96e360672 30 #include "vl53l1_hist_core.h"
charlesmn 0:3ac96e360672 31 #include "vl53l1_hist_private_structs.h"
charlesmn 0:3ac96e360672 32 #include "vl53l1_dmax_private_structs.h"
charlesmn 0:3ac96e360672 33 #include "vl53l1_xtalk.h"
charlesmn 0:3ac96e360672 34 #include "vl53l1_hist_algos_gen3.h"
charlesmn 0:3ac96e360672 35 #include "vl53l1_hist_algos_gen4.h"
charlesmn 0:3ac96e360672 36 #include "vl53l1_dmax.h"
charlesmn 0:3ac96e360672 37
charlesmn 0:3ac96e360672 38
charlesmn 0:3ac96e360672 39
charlesmn 0:3ac96e360672 40 #define LOG_FUNCTION_START(fmt, ...) \
charlesmn 0:3ac96e360672 41 _LOG_FUNCTION_START(VL53L1_TRACE_MODULE_HISTOGRAM, fmt, ##__VA_ARGS__)
charlesmn 0:3ac96e360672 42 #define LOG_FUNCTION_END(status, ...) \
charlesmn 0:3ac96e360672 43 _LOG_FUNCTION_END(VL53L1_TRACE_MODULE_HISTOGRAM, status, ##__VA_ARGS__)
charlesmn 0:3ac96e360672 44 #define LOG_FUNCTION_END_FMT(status, fmt, ...) \
charlesmn 0:3ac96e360672 45 _LOG_FUNCTION_END_FMT(VL53L1_TRACE_MODULE_HISTOGRAM, \
charlesmn 0:3ac96e360672 46 status, fmt, ##__VA_ARGS__)
charlesmn 0:3ac96e360672 47
charlesmn 0:3ac96e360672 48 #define trace_print(level, ...) \
charlesmn 0:3ac96e360672 49 _LOG_TRACE_PRINT(VL53L1_TRACE_MODULE_HISTOGRAM, \
charlesmn 0:3ac96e360672 50 level, VL53L1_TRACE_FUNCTION_NONE, ##__VA_ARGS__)
charlesmn 0:3ac96e360672 51
charlesmn 0:3ac96e360672 52
charlesmn 0:3ac96e360672 53 VL53L1_Error VL53L1_hist_process_data(
charlesmn 0:3ac96e360672 54 VL53L1_dmax_calibration_data_t *pdmax_cal,
charlesmn 0:3ac96e360672 55 VL53L1_hist_gen3_dmax_config_t *pdmax_cfg,
charlesmn 0:3ac96e360672 56 VL53L1_hist_post_process_config_t *ppost_cfg,
charlesmn 0:3ac96e360672 57 VL53L1_histogram_bin_data_t *pbins_input,
charlesmn 0:3ac96e360672 58 VL53L1_xtalk_histogram_data_t *pxtalk_shape,
charlesmn 0:3ac96e360672 59 uint8_t *pArea1,
charlesmn 0:3ac96e360672 60 uint8_t *pArea2,
charlesmn 0:3ac96e360672 61 VL53L1_range_results_t *presults,
charlesmn 0:3ac96e360672 62 uint8_t *HistMergeNumber)
charlesmn 0:3ac96e360672 63 {
charlesmn 0:3ac96e360672 64
charlesmn 0:3ac96e360672 65
charlesmn 0:3ac96e360672 66
charlesmn 0:3ac96e360672 67 VL53L1_Error status = VL53L1_ERROR_NONE;
charlesmn 0:3ac96e360672 68
charlesmn 0:3ac96e360672 69 VL53L1_hist_gen3_algo_private_data_t *palgo_gen3 =
charlesmn 0:3ac96e360672 70 (VL53L1_hist_gen3_algo_private_data_t *) pArea1;
charlesmn 0:3ac96e360672 71 VL53L1_hist_gen4_algo_filtered_data_t *pfiltered4 =
charlesmn 0:3ac96e360672 72 (VL53L1_hist_gen4_algo_filtered_data_t *) pArea2;
charlesmn 0:3ac96e360672 73
charlesmn 0:3ac96e360672 74 VL53L1_hist_gen3_dmax_private_data_t dmax_algo_gen3;
charlesmn 0:3ac96e360672 75 VL53L1_hist_gen3_dmax_private_data_t *pdmax_algo_gen3 =
charlesmn 0:3ac96e360672 76 &dmax_algo_gen3;
charlesmn 0:3ac96e360672 77
charlesmn 0:3ac96e360672 78 VL53L1_histogram_bin_data_t bins_averaged;
charlesmn 0:3ac96e360672 79 VL53L1_histogram_bin_data_t *pbins_averaged = &bins_averaged;
charlesmn 0:3ac96e360672 80
charlesmn 0:3ac96e360672 81 VL53L1_range_data_t *pdata;
charlesmn 0:3ac96e360672 82
charlesmn 0:3ac96e360672 83 uint32_t xtalk_rate_kcps = 0;
charlesmn 0:3ac96e360672 84 uint32_t max_xtalk_rate_per_spad_kcps = 0;
charlesmn 0:3ac96e360672 85 uint8_t xtalk_enable = 0;
charlesmn 0:3ac96e360672 86 uint8_t r = 0;
charlesmn 0:3ac96e360672 87 uint8_t t = 0;
charlesmn 0:3ac96e360672 88 uint32_t XtalkDetectMaxSigma = 0;
charlesmn 0:3ac96e360672 89
charlesmn 0:3ac96e360672 90
charlesmn 0:3ac96e360672 91 int16_t delta_mm = 0;
charlesmn 0:3ac96e360672 92
charlesmn 0:3ac96e360672 93
charlesmn 0:3ac96e360672 94 LOG_FUNCTION_START("");
charlesmn 0:3ac96e360672 95
charlesmn 0:3ac96e360672 96
charlesmn 0:3ac96e360672 97
charlesmn 0:3ac96e360672 98 VL53L1_f_039(
charlesmn 0:3ac96e360672 99 pbins_input,
charlesmn 0:3ac96e360672 100 pbins_averaged);
charlesmn 0:3ac96e360672 101
charlesmn 0:3ac96e360672 102
charlesmn 0:3ac96e360672 103
charlesmn 0:3ac96e360672 104 VL53L1_init_histogram_bin_data_struct(
charlesmn 0:3ac96e360672 105 0,
charlesmn 0:3ac96e360672 106 pxtalk_shape->xtalk_shape.VL53L1_p_024,
charlesmn 0:3ac96e360672 107 &(pxtalk_shape->xtalk_hist_removed));
charlesmn 0:3ac96e360672 108
charlesmn 0:3ac96e360672 109
charlesmn 0:3ac96e360672 110
charlesmn 0:3ac96e360672 111 VL53L1_copy_xtalk_bin_data_to_histogram_data_struct(
charlesmn 0:3ac96e360672 112 &(pxtalk_shape->xtalk_shape),
charlesmn 0:3ac96e360672 113 &(pxtalk_shape->xtalk_hist_removed));
charlesmn 0:3ac96e360672 114
charlesmn 0:3ac96e360672 115
charlesmn 0:3ac96e360672 116
charlesmn 0:3ac96e360672 117 if ((status == VL53L1_ERROR_NONE) &&
charlesmn 0:3ac96e360672 118 (ppost_cfg->algo__crosstalk_compensation_enable > 0))
charlesmn 0:3ac96e360672 119 status =
charlesmn 0:3ac96e360672 120 VL53L1_f_040(
charlesmn 0:3ac96e360672 121 ppost_cfg->algo__crosstalk_compensation_plane_offset_kcps,
charlesmn 0:3ac96e360672 122 ppost_cfg->algo__crosstalk_compensation_x_plane_gradient_kcps,
charlesmn 0:3ac96e360672 123 ppost_cfg->algo__crosstalk_compensation_y_plane_gradient_kcps,
charlesmn 0:3ac96e360672 124 0,
charlesmn 0:3ac96e360672 125 0,
charlesmn 0:3ac96e360672 126 pbins_input->result__dss_actual_effective_spads,
charlesmn 0:3ac96e360672 127 pbins_input->roi_config__user_roi_centre_spad,
charlesmn 0:3ac96e360672 128 pbins_input->roi_config__user_roi_requested_global_xy_size,
charlesmn 0:3ac96e360672 129 &(xtalk_rate_kcps));
charlesmn 0:3ac96e360672 130
charlesmn 0:3ac96e360672 131
charlesmn 0:3ac96e360672 132
charlesmn 0:3ac96e360672 133 if ((status == VL53L1_ERROR_NONE) &&
charlesmn 0:3ac96e360672 134 (ppost_cfg->algo__crosstalk_compensation_enable > 0))
charlesmn 0:3ac96e360672 135 status =
charlesmn 0:3ac96e360672 136 VL53L1_f_041(
charlesmn 0:3ac96e360672 137 pbins_averaged,
charlesmn 0:3ac96e360672 138 &(pxtalk_shape->xtalk_shape),
charlesmn 0:3ac96e360672 139 xtalk_rate_kcps,
charlesmn 0:3ac96e360672 140 &(pxtalk_shape->xtalk_hist_removed));
charlesmn 0:3ac96e360672 141
charlesmn 0:3ac96e360672 142
charlesmn 0:3ac96e360672 143
charlesmn 0:3ac96e360672 144
charlesmn 0:3ac96e360672 145
charlesmn 0:3ac96e360672 146 presults->xmonitor.total_periods_elapsed =
charlesmn 0:3ac96e360672 147 pbins_averaged->total_periods_elapsed;
charlesmn 0:3ac96e360672 148 presults->xmonitor.VL53L1_p_006 =
charlesmn 0:3ac96e360672 149 pbins_averaged->result__dss_actual_effective_spads;
charlesmn 0:3ac96e360672 150
charlesmn 0:3ac96e360672 151 presults->xmonitor.peak_signal_count_rate_mcps = 0;
charlesmn 0:3ac96e360672 152 presults->xmonitor.VL53L1_p_012 = 0;
charlesmn 0:3ac96e360672 153
charlesmn 0:3ac96e360672 154 presults->xmonitor.range_id = 0;
charlesmn 0:3ac96e360672 155 presults->xmonitor.range_status = VL53L1_DEVICEERROR_NOUPDATE;
charlesmn 0:3ac96e360672 156
charlesmn 0:3ac96e360672 157
charlesmn 0:3ac96e360672 158
charlesmn 0:3ac96e360672 159 xtalk_enable = 0;
charlesmn 0:3ac96e360672 160 if (ppost_cfg->algo__crosstalk_compensation_enable > 0)
charlesmn 0:3ac96e360672 161 xtalk_enable = 1;
charlesmn 0:3ac96e360672 162
charlesmn 0:3ac96e360672 163
charlesmn 0:3ac96e360672 164
charlesmn 0:3ac96e360672 165 for (r = 0 ; r <= xtalk_enable ; r++) {
charlesmn 0:3ac96e360672 166
charlesmn 0:3ac96e360672 167
charlesmn 0:3ac96e360672 168 ppost_cfg->algo__crosstalk_compensation_enable = r;
charlesmn 0:3ac96e360672 169
charlesmn 0:3ac96e360672 170
charlesmn 0:3ac96e360672 171
charlesmn 0:3ac96e360672 172 status =
charlesmn 0:3ac96e360672 173 VL53L1_f_033(
charlesmn 0:3ac96e360672 174 pdmax_cal,
charlesmn 0:3ac96e360672 175 pdmax_cfg,
charlesmn 0:3ac96e360672 176 ppost_cfg,
charlesmn 0:3ac96e360672 177 pbins_averaged,
charlesmn 0:3ac96e360672 178 &(pxtalk_shape->xtalk_hist_removed),
charlesmn 0:3ac96e360672 179 palgo_gen3,
charlesmn 0:3ac96e360672 180 pfiltered4,
charlesmn 0:3ac96e360672 181 pdmax_algo_gen3,
charlesmn 0:3ac96e360672 182 presults,
charlesmn 0:3ac96e360672 183 *HistMergeNumber);
charlesmn 0:3ac96e360672 184
charlesmn 0:3ac96e360672 185
charlesmn 0:3ac96e360672 186 if (!(status == VL53L1_ERROR_NONE && r == 0))
charlesmn 0:3ac96e360672 187 continue;
charlesmn 0:3ac96e360672 188
charlesmn 0:3ac96e360672 189
charlesmn 0:3ac96e360672 190
charlesmn 0:3ac96e360672 191 if (presults->active_results == 0) {
charlesmn 0:3ac96e360672 192 pdata = &(presults->VL53L1_p_002[0]);
charlesmn 0:3ac96e360672 193 pdata->ambient_count_rate_mcps =
charlesmn 0:3ac96e360672 194 pdmax_algo_gen3->VL53L1_p_001;
charlesmn 0:3ac96e360672 195 pdata->VL53L1_p_006 =
charlesmn 0:3ac96e360672 196 pdmax_algo_gen3->VL53L1_p_006;
charlesmn 0:3ac96e360672 197 }
charlesmn 0:3ac96e360672 198
charlesmn 0:3ac96e360672 199
charlesmn 0:3ac96e360672 200
charlesmn 0:3ac96e360672 201 max_xtalk_rate_per_spad_kcps = (uint32_t)(
charlesmn 0:3ac96e360672 202 ppost_cfg->algo__crosstalk_detect_max_valid_rate_kcps);
charlesmn 0:3ac96e360672 203 max_xtalk_rate_per_spad_kcps *= (uint32_t)(*HistMergeNumber);
charlesmn 0:3ac96e360672 204 max_xtalk_rate_per_spad_kcps <<= 4;
charlesmn 0:3ac96e360672 205
charlesmn 0:3ac96e360672 206 for (t = 0 ; t < presults->active_results ; t++) {
charlesmn 0:3ac96e360672 207
charlesmn 0:3ac96e360672 208 pdata = &(presults->VL53L1_p_002[t]);
charlesmn 0:3ac96e360672 209
charlesmn 0:3ac96e360672 210
charlesmn 0:3ac96e360672 211
charlesmn 0:3ac96e360672 212 if (pdata->max_range_mm > pdata->min_range_mm)
charlesmn 0:3ac96e360672 213 delta_mm =
charlesmn 0:3ac96e360672 214 pdata->max_range_mm -
charlesmn 0:3ac96e360672 215 pdata->min_range_mm;
charlesmn 0:3ac96e360672 216 else
charlesmn 0:3ac96e360672 217 delta_mm =
charlesmn 0:3ac96e360672 218 pdata->min_range_mm -
charlesmn 0:3ac96e360672 219 pdata->max_range_mm;
charlesmn 0:3ac96e360672 220
charlesmn 0:3ac96e360672 221 XtalkDetectMaxSigma =
charlesmn 0:3ac96e360672 222 ppost_cfg->algo__crosstalk_detect_max_sigma_mm;
charlesmn 0:3ac96e360672 223 XtalkDetectMaxSigma *= (uint32_t)(*HistMergeNumber);
charlesmn 0:3ac96e360672 224 XtalkDetectMaxSigma <<= 5;
charlesmn 0:3ac96e360672 225 if (pdata->median_range_mm >
charlesmn 0:3ac96e360672 226 ppost_cfg->algo__crosstalk_detect_min_valid_range_mm &&
charlesmn 0:3ac96e360672 227 pdata->median_range_mm <
charlesmn 0:3ac96e360672 228 ppost_cfg->algo__crosstalk_detect_max_valid_range_mm &&
charlesmn 0:3ac96e360672 229 pdata->VL53L1_p_012 <
charlesmn 0:3ac96e360672 230 max_xtalk_rate_per_spad_kcps &&
charlesmn 0:3ac96e360672 231 pdata->VL53L1_p_005 < XtalkDetectMaxSigma &&
charlesmn 0:3ac96e360672 232 delta_mm <
charlesmn 0:3ac96e360672 233 ppost_cfg->algo__crosstalk_detect_min_max_tolerance) {
charlesmn 0:3ac96e360672 234
charlesmn 0:3ac96e360672 235
charlesmn 0:3ac96e360672 236
charlesmn 0:3ac96e360672 237 memcpy(
charlesmn 0:3ac96e360672 238 &(presults->xmonitor),
charlesmn 0:3ac96e360672 239 pdata,
charlesmn 0:3ac96e360672 240 sizeof(VL53L1_range_data_t));
charlesmn 0:3ac96e360672 241
charlesmn 0:3ac96e360672 242 }
charlesmn 0:3ac96e360672 243 }
charlesmn 0:3ac96e360672 244
charlesmn 0:3ac96e360672 245 }
charlesmn 0:3ac96e360672 246
charlesmn 0:3ac96e360672 247
charlesmn 0:3ac96e360672 248
charlesmn 0:3ac96e360672 249 ppost_cfg->algo__crosstalk_compensation_enable = xtalk_enable;
charlesmn 0:3ac96e360672 250
charlesmn 0:3ac96e360672 251 LOG_FUNCTION_END(status);
charlesmn 0:3ac96e360672 252
charlesmn 0:3ac96e360672 253 return status;
charlesmn 0:3ac96e360672 254 }
charlesmn 0:3ac96e360672 255
charlesmn 0:3ac96e360672 256
charlesmn 0:3ac96e360672 257 VL53L1_Error VL53L1_hist_ambient_dmax(
charlesmn 0:3ac96e360672 258 uint16_t target_reflectance,
charlesmn 0:3ac96e360672 259 VL53L1_dmax_calibration_data_t *pdmax_cal,
charlesmn 0:3ac96e360672 260 VL53L1_hist_gen3_dmax_config_t *pdmax_cfg,
charlesmn 0:3ac96e360672 261 VL53L1_histogram_bin_data_t *pbins,
charlesmn 0:3ac96e360672 262 int16_t *pambient_dmax_mm)
charlesmn 0:3ac96e360672 263 {
charlesmn 0:3ac96e360672 264
charlesmn 0:3ac96e360672 265
charlesmn 0:3ac96e360672 266
charlesmn 0:3ac96e360672 267 VL53L1_Error status = VL53L1_ERROR_NONE;
charlesmn 0:3ac96e360672 268
charlesmn 0:3ac96e360672 269 VL53L1_hist_gen3_dmax_private_data_t dmax_algo;
charlesmn 0:3ac96e360672 270 VL53L1_hist_gen3_dmax_private_data_t *pdmax_algo = &dmax_algo;
charlesmn 0:3ac96e360672 271
charlesmn 0:3ac96e360672 272 LOG_FUNCTION_START("");
charlesmn 0:3ac96e360672 273
charlesmn 0:3ac96e360672 274 status =
charlesmn 0:3ac96e360672 275 VL53L1_f_001(
charlesmn 0:3ac96e360672 276 target_reflectance,
charlesmn 0:3ac96e360672 277 pdmax_cal,
charlesmn 0:3ac96e360672 278 pdmax_cfg,
charlesmn 0:3ac96e360672 279 pbins,
charlesmn 0:3ac96e360672 280 pdmax_algo,
charlesmn 0:3ac96e360672 281 pambient_dmax_mm);
charlesmn 0:3ac96e360672 282
charlesmn 0:3ac96e360672 283 LOG_FUNCTION_END(status);
charlesmn 0:3ac96e360672 284
charlesmn 0:3ac96e360672 285 return status;
charlesmn 0:3ac96e360672 286 }
charlesmn 0:3ac96e360672 287
charlesmn 0:3ac96e360672 288