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

Dependencies:   X_NUCLEO_COMMON ST_INTERFACES

Dependents:   VL53L1CB_noshield_1sensor_polls_auton VL53L1CB_noshield_1sensor_interrupt_auton X_NUCLEO_53L1A2

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

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

Who changed what in which revision?

UserRevisionLine numberNew contents of line
charlesmn 0:3ac96e360672 1
charlesmn 0:3ac96e360672 2 /*******************************************************************************
charlesmn 0:3ac96e360672 3 * Copyright (c) 2020, STMicroelectronics - All Rights Reserved
charlesmn 0:3ac96e360672 4
charlesmn 0:3ac96e360672 5 This file is part of VL53L1 Core and is dual licensed,
charlesmn 0:3ac96e360672 6 either 'STMicroelectronics
charlesmn 0:3ac96e360672 7 Proprietary license'
charlesmn 0:3ac96e360672 8 or 'BSD 3-clause "New" or "Revised" License' , at your option.
charlesmn 0:3ac96e360672 9
charlesmn 0:3ac96e360672 10 ********************************************************************************
charlesmn 0:3ac96e360672 11
charlesmn 0:3ac96e360672 12 'STMicroelectronics Proprietary license'
charlesmn 0:3ac96e360672 13
charlesmn 0:3ac96e360672 14 ********************************************************************************
charlesmn 0:3ac96e360672 15
charlesmn 0:3ac96e360672 16 License terms: STMicroelectronics Proprietary in accordance with licensing
charlesmn 0:3ac96e360672 17 terms at www.st.com/sla0081
charlesmn 0:3ac96e360672 18
charlesmn 0:3ac96e360672 19 STMicroelectronics confidential
charlesmn 0:3ac96e360672 20 Reproduction and Communication of this document is strictly prohibited unless
charlesmn 0:3ac96e360672 21 specifically authorized in writing by STMicroelectronics.
charlesmn 0:3ac96e360672 22
charlesmn 0:3ac96e360672 23
charlesmn 0:3ac96e360672 24 ********************************************************************************
charlesmn 0:3ac96e360672 25
charlesmn 0:3ac96e360672 26 Alternatively, VL53L1 Core may be distributed under the terms of
charlesmn 0:3ac96e360672 27 'BSD 3-clause "New" or "Revised" License', in which case the following
charlesmn 0:3ac96e360672 28 provisions apply instead of the ones
charlesmn 0:3ac96e360672 29 mentioned above :
charlesmn 0:3ac96e360672 30
charlesmn 0:3ac96e360672 31 ********************************************************************************
charlesmn 0:3ac96e360672 32
charlesmn 0:3ac96e360672 33 License terms: BSD 3-clause "New" or "Revised" License.
charlesmn 0:3ac96e360672 34
charlesmn 0:3ac96e360672 35 Redistribution and use in source and binary forms, with or without
charlesmn 0:3ac96e360672 36 modification, are permitted provided that the following conditions are met:
charlesmn 0:3ac96e360672 37
charlesmn 0:3ac96e360672 38 1. Redistributions of source code must retain the above copyright notice, this
charlesmn 0:3ac96e360672 39 list of conditions and the following disclaimer.
charlesmn 0:3ac96e360672 40
charlesmn 0:3ac96e360672 41 2. Redistributions in binary form must reproduce the above copyright notice,
charlesmn 0:3ac96e360672 42 this list of conditions and the following disclaimer in the documentation
charlesmn 0:3ac96e360672 43 and/or other materials provided with the distribution.
charlesmn 0:3ac96e360672 44
charlesmn 0:3ac96e360672 45 3. Neither the name of the copyright holder nor the names of its contributors
charlesmn 0:3ac96e360672 46 may be used to endorse or promote products derived from this software
charlesmn 0:3ac96e360672 47 without specific prior written permission.
charlesmn 0:3ac96e360672 48
charlesmn 0:3ac96e360672 49 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
charlesmn 0:3ac96e360672 50 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
charlesmn 0:3ac96e360672 51 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
charlesmn 0:3ac96e360672 52 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
charlesmn 0:3ac96e360672 53 FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
charlesmn 0:3ac96e360672 54 DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
charlesmn 0:3ac96e360672 55 SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
charlesmn 0:3ac96e360672 56 CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
charlesmn 0:3ac96e360672 57 OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
charlesmn 0:3ac96e360672 58 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
charlesmn 0:3ac96e360672 59
charlesmn 0:3ac96e360672 60
charlesmn 0:3ac96e360672 61 ********************************************************************************
charlesmn 0:3ac96e360672 62
charlesmn 0:3ac96e360672 63 */
charlesmn 0:3ac96e360672 64
charlesmn 0:3ac96e360672 65
charlesmn 0:3ac96e360672 66
charlesmn 0:3ac96e360672 67
charlesmn 0:3ac96e360672 68
charlesmn 0:3ac96e360672 69 #include "vl53l1_ll_def.h"
charlesmn 0:3ac96e360672 70 #include "vl53l1_ll_device.h"
charlesmn 0:3ac96e360672 71 #include "vl53l1_platform.h"
charlesmn 0:3ac96e360672 72 #include "vl53l1_core.h"
charlesmn 0:3ac96e360672 73 #include "vl53l1_silicon_core.h"
charlesmn 0:3ac96e360672 74 #include "vl53l1_wait.h"
charlesmn 0:3ac96e360672 75 #include "vl53l1_register_settings.h"
charlesmn 0:3ac96e360672 76
charlesmn 0:3ac96e360672 77
charlesmn 0:3ac96e360672 78 #define LOG_FUNCTION_START(fmt, ...) \
charlesmn 0:3ac96e360672 79 _LOG_FUNCTION_START(VL53L1_TRACE_MODULE_CORE, fmt, ##__VA_ARGS__)
charlesmn 0:3ac96e360672 80 #define LOG_FUNCTION_END(status, ...) \
charlesmn 0:3ac96e360672 81 _LOG_FUNCTION_END(VL53L1_TRACE_MODULE_CORE, status, ##__VA_ARGS__)
charlesmn 0:3ac96e360672 82 #define LOG_FUNCTION_END_FMT(status, fmt, ...) \
charlesmn 0:3ac96e360672 83 _LOG_FUNCTION_END_FMT(VL53L1_TRACE_MODULE_CORE, status, \
charlesmn 0:3ac96e360672 84 fmt, ##__VA_ARGS__)
charlesmn 0:3ac96e360672 85
charlesmn 0:3ac96e360672 86
charlesmn 0:3ac96e360672 87 VL53L1_Error VL53L1_wait_for_boot_completion(
charlesmn 0:3ac96e360672 88 VL53L1_DEV Dev)
charlesmn 0:3ac96e360672 89 {
charlesmn 0:3ac96e360672 90
charlesmn 0:3ac96e360672 91
charlesmn 0:3ac96e360672 92
charlesmn 0:3ac96e360672 93 VL53L1_Error status = VL53L1_ERROR_NONE;
charlesmn 0:3ac96e360672 94 VL53L1_LLDriverData_t *pdev = VL53L1DevStructGetLLDriverHandle(Dev);
charlesmn 0:3ac96e360672 95
charlesmn 0:3ac96e360672 96 uint8_t fw_ready = 0;
charlesmn 0:3ac96e360672 97
charlesmn 0:3ac96e360672 98 LOG_FUNCTION_START("");
charlesmn 0:3ac96e360672 99
charlesmn 0:3ac96e360672 100 if (pdev->wait_method == VL53L1_WAIT_METHOD_BLOCKING) {
charlesmn 0:3ac96e360672 101
charlesmn 0:3ac96e360672 102
charlesmn 0:3ac96e360672 103
charlesmn 0:3ac96e360672 104 status =
charlesmn 0:3ac96e360672 105 VL53L1_poll_for_boot_completion(
charlesmn 0:3ac96e360672 106 Dev,
charlesmn 0:3ac96e360672 107 VL53L1_BOOT_COMPLETION_POLLING_TIMEOUT_MS);
charlesmn 0:3ac96e360672 108
charlesmn 0:3ac96e360672 109 } else {
charlesmn 0:3ac96e360672 110
charlesmn 0:3ac96e360672 111
charlesmn 0:3ac96e360672 112
charlesmn 0:3ac96e360672 113 fw_ready = 0;
charlesmn 0:3ac96e360672 114 while (fw_ready == 0x00 && status == VL53L1_ERROR_NONE) {
charlesmn 0:3ac96e360672 115 status = VL53L1_is_boot_complete(
charlesmn 0:3ac96e360672 116 Dev,
charlesmn 0:3ac96e360672 117 &fw_ready);
charlesmn 0:3ac96e360672 118
charlesmn 0:3ac96e360672 119 if (status == VL53L1_ERROR_NONE) {
charlesmn 0:3ac96e360672 120 status = VL53L1_WaitMs(
charlesmn 0:3ac96e360672 121 Dev,
charlesmn 0:3ac96e360672 122 VL53L1_POLLING_DELAY_MS);
charlesmn 0:3ac96e360672 123 }
charlesmn 0:3ac96e360672 124 }
charlesmn 0:3ac96e360672 125 }
charlesmn 0:3ac96e360672 126
charlesmn 0:3ac96e360672 127 LOG_FUNCTION_END(status);
charlesmn 0:3ac96e360672 128
charlesmn 0:3ac96e360672 129 return status;
charlesmn 0:3ac96e360672 130
charlesmn 0:3ac96e360672 131 }
charlesmn 0:3ac96e360672 132
charlesmn 0:3ac96e360672 133
charlesmn 0:3ac96e360672 134 VL53L1_Error VL53L1_wait_for_firmware_ready(
charlesmn 0:3ac96e360672 135 VL53L1_DEV Dev)
charlesmn 0:3ac96e360672 136 {
charlesmn 0:3ac96e360672 137
charlesmn 0:3ac96e360672 138
charlesmn 0:3ac96e360672 139
charlesmn 0:3ac96e360672 140 VL53L1_Error status = VL53L1_ERROR_NONE;
charlesmn 0:3ac96e360672 141 VL53L1_LLDriverData_t *pdev = VL53L1DevStructGetLLDriverHandle(Dev);
charlesmn 0:3ac96e360672 142
charlesmn 0:3ac96e360672 143 uint8_t fw_ready = 0;
charlesmn 0:3ac96e360672 144 uint8_t mode_start = 0;
charlesmn 0:3ac96e360672 145
charlesmn 0:3ac96e360672 146 LOG_FUNCTION_START("");
charlesmn 0:3ac96e360672 147
charlesmn 0:3ac96e360672 148
charlesmn 0:3ac96e360672 149 mode_start =
charlesmn 0:3ac96e360672 150 pdev->sys_ctrl.system__mode_start &
charlesmn 0:3ac96e360672 151 VL53L1_DEVICEMEASUREMENTMODE_MODE_MASK;
charlesmn 0:3ac96e360672 152
charlesmn 0:3ac96e360672 153
charlesmn 0:3ac96e360672 154
charlesmn 0:3ac96e360672 155 if ((mode_start == VL53L1_DEVICEMEASUREMENTMODE_TIMED) ||
charlesmn 0:3ac96e360672 156 (mode_start == VL53L1_DEVICEMEASUREMENTMODE_SINGLESHOT)) {
charlesmn 0:3ac96e360672 157
charlesmn 0:3ac96e360672 158 if (pdev->wait_method == VL53L1_WAIT_METHOD_BLOCKING) {
charlesmn 0:3ac96e360672 159
charlesmn 0:3ac96e360672 160
charlesmn 0:3ac96e360672 161
charlesmn 0:3ac96e360672 162 status =
charlesmn 0:3ac96e360672 163 VL53L1_poll_for_firmware_ready(
charlesmn 0:3ac96e360672 164 Dev,
charlesmn 0:3ac96e360672 165 VL53L1_RANGE_COMPLETION_POLLING_TIMEOUT_MS);
charlesmn 0:3ac96e360672 166
charlesmn 0:3ac96e360672 167 } else {
charlesmn 0:3ac96e360672 168
charlesmn 0:3ac96e360672 169
charlesmn 0:3ac96e360672 170
charlesmn 0:3ac96e360672 171 fw_ready = 0;
charlesmn 0:3ac96e360672 172 while (fw_ready == 0x00 && status ==
charlesmn 0:3ac96e360672 173 VL53L1_ERROR_NONE) {
charlesmn 0:3ac96e360672 174 status = VL53L1_is_firmware_ready(
charlesmn 0:3ac96e360672 175 Dev,
charlesmn 0:3ac96e360672 176 &fw_ready);
charlesmn 0:3ac96e360672 177
charlesmn 0:3ac96e360672 178 if (status == VL53L1_ERROR_NONE) {
charlesmn 0:3ac96e360672 179 status = VL53L1_WaitMs(
charlesmn 0:3ac96e360672 180 Dev,
charlesmn 0:3ac96e360672 181 VL53L1_POLLING_DELAY_MS);
charlesmn 0:3ac96e360672 182 }
charlesmn 0:3ac96e360672 183 }
charlesmn 0:3ac96e360672 184 }
charlesmn 0:3ac96e360672 185 }
charlesmn 0:3ac96e360672 186
charlesmn 0:3ac96e360672 187 LOG_FUNCTION_END(status);
charlesmn 0:3ac96e360672 188
charlesmn 0:3ac96e360672 189 return status;
charlesmn 0:3ac96e360672 190 }
charlesmn 0:3ac96e360672 191
charlesmn 0:3ac96e360672 192
charlesmn 0:3ac96e360672 193 VL53L1_Error VL53L1_wait_for_range_completion(
charlesmn 0:3ac96e360672 194 VL53L1_DEV Dev)
charlesmn 0:3ac96e360672 195 {
charlesmn 0:3ac96e360672 196
charlesmn 0:3ac96e360672 197
charlesmn 0:3ac96e360672 198
charlesmn 0:3ac96e360672 199 VL53L1_Error status = VL53L1_ERROR_NONE;
charlesmn 0:3ac96e360672 200 VL53L1_LLDriverData_t *pdev = VL53L1DevStructGetLLDriverHandle(Dev);
charlesmn 0:3ac96e360672 201
charlesmn 0:3ac96e360672 202 uint8_t data_ready = 0;
charlesmn 0:3ac96e360672 203
charlesmn 0:3ac96e360672 204 LOG_FUNCTION_START("");
charlesmn 0:3ac96e360672 205
charlesmn 0:3ac96e360672 206 if (pdev->wait_method == VL53L1_WAIT_METHOD_BLOCKING) {
charlesmn 0:3ac96e360672 207
charlesmn 0:3ac96e360672 208
charlesmn 0:3ac96e360672 209
charlesmn 0:3ac96e360672 210 status =
charlesmn 0:3ac96e360672 211 VL53L1_poll_for_range_completion(
charlesmn 0:3ac96e360672 212 Dev,
charlesmn 0:3ac96e360672 213 VL53L1_RANGE_COMPLETION_POLLING_TIMEOUT_MS);
charlesmn 0:3ac96e360672 214
charlesmn 0:3ac96e360672 215 } else {
charlesmn 0:3ac96e360672 216
charlesmn 0:3ac96e360672 217
charlesmn 0:3ac96e360672 218
charlesmn 0:3ac96e360672 219 data_ready = 0;
charlesmn 0:3ac96e360672 220 while (data_ready == 0x00 && status == VL53L1_ERROR_NONE) {
charlesmn 0:3ac96e360672 221 status = VL53L1_is_new_data_ready(
charlesmn 0:3ac96e360672 222 Dev,
charlesmn 0:3ac96e360672 223 &data_ready);
charlesmn 0:3ac96e360672 224
charlesmn 0:3ac96e360672 225 if (status == VL53L1_ERROR_NONE) {
charlesmn 0:3ac96e360672 226 status = VL53L1_WaitMs(
charlesmn 0:3ac96e360672 227 Dev,
charlesmn 0:3ac96e360672 228 VL53L1_POLLING_DELAY_MS);
charlesmn 0:3ac96e360672 229 }
charlesmn 0:3ac96e360672 230 }
charlesmn 0:3ac96e360672 231 }
charlesmn 0:3ac96e360672 232
charlesmn 0:3ac96e360672 233 LOG_FUNCTION_END(status);
charlesmn 0:3ac96e360672 234
charlesmn 0:3ac96e360672 235 return status;
charlesmn 0:3ac96e360672 236 }
charlesmn 0:3ac96e360672 237
charlesmn 0:3ac96e360672 238
charlesmn 0:3ac96e360672 239 VL53L1_Error VL53L1_wait_for_test_completion(
charlesmn 0:3ac96e360672 240 VL53L1_DEV Dev)
charlesmn 0:3ac96e360672 241 {
charlesmn 0:3ac96e360672 242
charlesmn 0:3ac96e360672 243
charlesmn 0:3ac96e360672 244
charlesmn 0:3ac96e360672 245 VL53L1_Error status = VL53L1_ERROR_NONE;
charlesmn 0:3ac96e360672 246 VL53L1_LLDriverData_t *pdev = VL53L1DevStructGetLLDriverHandle(Dev);
charlesmn 0:3ac96e360672 247
charlesmn 0:3ac96e360672 248 uint8_t data_ready = 0;
charlesmn 0:3ac96e360672 249
charlesmn 0:3ac96e360672 250 LOG_FUNCTION_START("");
charlesmn 0:3ac96e360672 251
charlesmn 0:3ac96e360672 252 if (pdev->wait_method == VL53L1_WAIT_METHOD_BLOCKING) {
charlesmn 0:3ac96e360672 253
charlesmn 0:3ac96e360672 254
charlesmn 0:3ac96e360672 255
charlesmn 0:3ac96e360672 256 status =
charlesmn 0:3ac96e360672 257 VL53L1_poll_for_range_completion(
charlesmn 0:3ac96e360672 258 Dev,
charlesmn 0:3ac96e360672 259 VL53L1_TEST_COMPLETION_POLLING_TIMEOUT_MS);
charlesmn 0:3ac96e360672 260
charlesmn 0:3ac96e360672 261 } else {
charlesmn 0:3ac96e360672 262
charlesmn 0:3ac96e360672 263
charlesmn 0:3ac96e360672 264
charlesmn 0:3ac96e360672 265 data_ready = 0;
charlesmn 0:3ac96e360672 266 while (data_ready == 0x00 && status == VL53L1_ERROR_NONE) {
charlesmn 0:3ac96e360672 267 status = VL53L1_is_new_data_ready(
charlesmn 0:3ac96e360672 268 Dev,
charlesmn 0:3ac96e360672 269 &data_ready);
charlesmn 0:3ac96e360672 270
charlesmn 0:3ac96e360672 271 if (status == VL53L1_ERROR_NONE) {
charlesmn 0:3ac96e360672 272 status = VL53L1_WaitMs(
charlesmn 0:3ac96e360672 273 Dev,
charlesmn 0:3ac96e360672 274 VL53L1_POLLING_DELAY_MS);
charlesmn 0:3ac96e360672 275 }
charlesmn 0:3ac96e360672 276 }
charlesmn 0:3ac96e360672 277 }
charlesmn 0:3ac96e360672 278
charlesmn 0:3ac96e360672 279 LOG_FUNCTION_END(status);
charlesmn 0:3ac96e360672 280
charlesmn 0:3ac96e360672 281 return status;
charlesmn 0:3ac96e360672 282 }
charlesmn 0:3ac96e360672 283
charlesmn 0:3ac96e360672 284
charlesmn 0:3ac96e360672 285
charlesmn 0:3ac96e360672 286
charlesmn 0:3ac96e360672 287 VL53L1_Error VL53L1_is_boot_complete(
charlesmn 0:3ac96e360672 288 VL53L1_DEV Dev,
charlesmn 0:3ac96e360672 289 uint8_t *pready)
charlesmn 0:3ac96e360672 290 {
charlesmn 0:3ac96e360672 291
charlesmn 0:3ac96e360672 292
charlesmn 0:3ac96e360672 293 VL53L1_Error status = VL53L1_ERROR_NONE;
charlesmn 0:3ac96e360672 294 uint8_t firmware__system_status = 0;
charlesmn 0:3ac96e360672 295
charlesmn 0:3ac96e360672 296 LOG_FUNCTION_START("");
charlesmn 0:3ac96e360672 297
charlesmn 0:3ac96e360672 298
charlesmn 0:3ac96e360672 299
charlesmn 0:3ac96e360672 300 status =
charlesmn 0:3ac96e360672 301 VL53L1_RdByte(
charlesmn 0:3ac96e360672 302 Dev,
charlesmn 0:3ac96e360672 303 VL53L1_FIRMWARE__SYSTEM_STATUS,
charlesmn 0:3ac96e360672 304 &firmware__system_status);
charlesmn 0:3ac96e360672 305
charlesmn 0:3ac96e360672 306
charlesmn 0:3ac96e360672 307
charlesmn 0:3ac96e360672 308 if ((firmware__system_status & 0x01) == 0x01) {
charlesmn 0:3ac96e360672 309 *pready = 0x01;
charlesmn 0:3ac96e360672 310 VL53L1_init_ll_driver_state(
charlesmn 0:3ac96e360672 311 Dev,
charlesmn 0:3ac96e360672 312 VL53L1_DEVICESTATE_SW_STANDBY);
charlesmn 0:3ac96e360672 313 } else {
charlesmn 0:3ac96e360672 314 *pready = 0x00;
charlesmn 0:3ac96e360672 315 VL53L1_init_ll_driver_state(
charlesmn 0:3ac96e360672 316 Dev,
charlesmn 0:3ac96e360672 317 VL53L1_DEVICESTATE_FW_COLDBOOT);
charlesmn 0:3ac96e360672 318 }
charlesmn 0:3ac96e360672 319
charlesmn 0:3ac96e360672 320 LOG_FUNCTION_END(status);
charlesmn 0:3ac96e360672 321
charlesmn 0:3ac96e360672 322 return status;
charlesmn 0:3ac96e360672 323 }
charlesmn 0:3ac96e360672 324
charlesmn 0:3ac96e360672 325
charlesmn 0:3ac96e360672 326 VL53L1_Error VL53L1_is_firmware_ready(
charlesmn 0:3ac96e360672 327 VL53L1_DEV Dev,
charlesmn 0:3ac96e360672 328 uint8_t *pready)
charlesmn 0:3ac96e360672 329 {
charlesmn 0:3ac96e360672 330
charlesmn 0:3ac96e360672 331
charlesmn 0:3ac96e360672 332 VL53L1_Error status = VL53L1_ERROR_NONE;
charlesmn 0:3ac96e360672 333 VL53L1_LLDriverData_t *pdev = VL53L1DevStructGetLLDriverHandle(Dev);
charlesmn 0:3ac96e360672 334
charlesmn 0:3ac96e360672 335 LOG_FUNCTION_START("");
charlesmn 0:3ac96e360672 336
charlesmn 0:3ac96e360672 337 status = VL53L1_is_firmware_ready_silicon(
charlesmn 0:3ac96e360672 338 Dev,
charlesmn 0:3ac96e360672 339 pready);
charlesmn 0:3ac96e360672 340
charlesmn 0:3ac96e360672 341 pdev->fw_ready = *pready;
charlesmn 0:3ac96e360672 342
charlesmn 0:3ac96e360672 343 LOG_FUNCTION_END(status);
charlesmn 0:3ac96e360672 344
charlesmn 0:3ac96e360672 345 return status;
charlesmn 0:3ac96e360672 346 }
charlesmn 0:3ac96e360672 347
charlesmn 0:3ac96e360672 348
charlesmn 0:3ac96e360672 349 VL53L1_Error VL53L1_is_new_data_ready(
charlesmn 0:3ac96e360672 350 VL53L1_DEV Dev,
charlesmn 0:3ac96e360672 351 uint8_t *pready)
charlesmn 0:3ac96e360672 352 {
charlesmn 0:3ac96e360672 353
charlesmn 0:3ac96e360672 354
charlesmn 0:3ac96e360672 355 VL53L1_Error status = VL53L1_ERROR_NONE;
charlesmn 0:3ac96e360672 356 VL53L1_LLDriverData_t *pdev = VL53L1DevStructGetLLDriverHandle(Dev);
charlesmn 0:3ac96e360672 357
charlesmn 0:3ac96e360672 358 uint8_t gpio__mux_active_high_hv = 0;
charlesmn 0:3ac96e360672 359 uint8_t gpio__tio_hv_status = 0;
charlesmn 0:3ac96e360672 360 uint8_t interrupt_ready = 0;
charlesmn 0:3ac96e360672 361
charlesmn 0:3ac96e360672 362 LOG_FUNCTION_START("");
charlesmn 0:3ac96e360672 363
charlesmn 0:3ac96e360672 364 gpio__mux_active_high_hv =
charlesmn 0:3ac96e360672 365 pdev->stat_cfg.gpio_hv_mux__ctrl &
charlesmn 0:3ac96e360672 366 VL53L1_DEVICEINTERRUPTLEVEL_ACTIVE_MASK;
charlesmn 0:3ac96e360672 367
charlesmn 0:3ac96e360672 368 if (gpio__mux_active_high_hv == VL53L1_DEVICEINTERRUPTLEVEL_ACTIVE_HIGH)
charlesmn 0:3ac96e360672 369 interrupt_ready = 0x01;
charlesmn 0:3ac96e360672 370 else
charlesmn 0:3ac96e360672 371 interrupt_ready = 0x00;
charlesmn 0:3ac96e360672 372
charlesmn 0:3ac96e360672 373
charlesmn 0:3ac96e360672 374
charlesmn 0:3ac96e360672 375 status = VL53L1_RdByte(
charlesmn 0:3ac96e360672 376 Dev,
charlesmn 0:3ac96e360672 377 VL53L1_GPIO__TIO_HV_STATUS,
charlesmn 0:3ac96e360672 378 &gpio__tio_hv_status);
charlesmn 0:3ac96e360672 379
charlesmn 0:3ac96e360672 380
charlesmn 0:3ac96e360672 381
charlesmn 0:3ac96e360672 382 if ((gpio__tio_hv_status & 0x01) == interrupt_ready)
charlesmn 0:3ac96e360672 383 *pready = 0x01;
charlesmn 0:3ac96e360672 384 else
charlesmn 0:3ac96e360672 385 *pready = 0x00;
charlesmn 0:3ac96e360672 386
charlesmn 0:3ac96e360672 387 LOG_FUNCTION_END(status);
charlesmn 0:3ac96e360672 388
charlesmn 0:3ac96e360672 389 return status;
charlesmn 0:3ac96e360672 390 }
charlesmn 0:3ac96e360672 391
charlesmn 0:3ac96e360672 392
charlesmn 0:3ac96e360672 393
charlesmn 0:3ac96e360672 394
charlesmn 0:3ac96e360672 395 VL53L1_Error VL53L1_poll_for_boot_completion(
charlesmn 0:3ac96e360672 396 VL53L1_DEV Dev,
charlesmn 0:3ac96e360672 397 uint32_t timeout_ms)
charlesmn 0:3ac96e360672 398 {
charlesmn 0:3ac96e360672 399
charlesmn 0:3ac96e360672 400
charlesmn 0:3ac96e360672 401 VL53L1_Error status = VL53L1_ERROR_NONE;
charlesmn 0:3ac96e360672 402
charlesmn 0:3ac96e360672 403 LOG_FUNCTION_START("");
charlesmn 0:3ac96e360672 404
charlesmn 0:3ac96e360672 405
charlesmn 0:3ac96e360672 406
charlesmn 0:3ac96e360672 407 status = VL53L1_WaitUs(
charlesmn 0:3ac96e360672 408 Dev,
charlesmn 0:3ac96e360672 409 VL53L1_FIRMWARE_BOOT_TIME_US);
charlesmn 0:3ac96e360672 410
charlesmn 0:3ac96e360672 411 if (status == VL53L1_ERROR_NONE)
charlesmn 0:3ac96e360672 412 status =
charlesmn 0:3ac96e360672 413 VL53L1_WaitValueMaskEx(
charlesmn 0:3ac96e360672 414 Dev,
charlesmn 0:3ac96e360672 415 timeout_ms,
charlesmn 0:3ac96e360672 416 VL53L1_FIRMWARE__SYSTEM_STATUS,
charlesmn 0:3ac96e360672 417 0x01,
charlesmn 0:3ac96e360672 418 0x01,
charlesmn 0:3ac96e360672 419 VL53L1_POLLING_DELAY_MS);
charlesmn 0:3ac96e360672 420
charlesmn 0:3ac96e360672 421 if (status == VL53L1_ERROR_NONE)
charlesmn 0:3ac96e360672 422 VL53L1_init_ll_driver_state(Dev, VL53L1_DEVICESTATE_SW_STANDBY);
charlesmn 0:3ac96e360672 423
charlesmn 0:3ac96e360672 424 LOG_FUNCTION_END(status);
charlesmn 0:3ac96e360672 425
charlesmn 0:3ac96e360672 426 return status;
charlesmn 0:3ac96e360672 427 }
charlesmn 0:3ac96e360672 428
charlesmn 0:3ac96e360672 429
charlesmn 0:3ac96e360672 430 VL53L1_Error VL53L1_poll_for_firmware_ready(
charlesmn 0:3ac96e360672 431 VL53L1_DEV Dev,
charlesmn 0:3ac96e360672 432 uint32_t timeout_ms)
charlesmn 0:3ac96e360672 433 {
charlesmn 0:3ac96e360672 434
charlesmn 0:3ac96e360672 435
charlesmn 0:3ac96e360672 436 VL53L1_Error status = VL53L1_ERROR_NONE;
charlesmn 0:3ac96e360672 437 VL53L1_LLDriverData_t *pdev = VL53L1DevStructGetLLDriverHandle(Dev);
charlesmn 0:3ac96e360672 438
charlesmn 0:3ac96e360672 439 uint32_t start_time_ms = 0;
charlesmn 0:3ac96e360672 440 uint32_t current_time_ms = 0;
charlesmn 0:3ac96e360672 441 int32_t poll_delay_ms = VL53L1_POLLING_DELAY_MS;
charlesmn 0:3ac96e360672 442 uint8_t fw_ready = 0;
charlesmn 0:3ac96e360672 443
charlesmn 0:3ac96e360672 444
charlesmn 0:3ac96e360672 445
charlesmn 0:3ac96e360672 446 VL53L1_GetTickCount(&start_time_ms);
charlesmn 0:3ac96e360672 447 pdev->fw_ready_poll_duration_ms = 0;
charlesmn 0:3ac96e360672 448
charlesmn 0:3ac96e360672 449
charlesmn 0:3ac96e360672 450
charlesmn 0:3ac96e360672 451 while ((status == VL53L1_ERROR_NONE) &&
charlesmn 0:3ac96e360672 452 (pdev->fw_ready_poll_duration_ms < timeout_ms) &&
charlesmn 0:3ac96e360672 453 (fw_ready == 0)) {
charlesmn 0:3ac96e360672 454
charlesmn 0:3ac96e360672 455 status = VL53L1_is_firmware_ready(
charlesmn 0:3ac96e360672 456 Dev,
charlesmn 0:3ac96e360672 457 &fw_ready);
charlesmn 0:3ac96e360672 458
charlesmn 0:3ac96e360672 459 if (status == VL53L1_ERROR_NONE &&
charlesmn 0:3ac96e360672 460 fw_ready == 0 &&
charlesmn 0:3ac96e360672 461 poll_delay_ms > 0) {
charlesmn 0:3ac96e360672 462 status = VL53L1_WaitMs(
charlesmn 0:3ac96e360672 463 Dev,
charlesmn 0:3ac96e360672 464 poll_delay_ms);
charlesmn 0:3ac96e360672 465 }
charlesmn 0:3ac96e360672 466
charlesmn 0:3ac96e360672 467
charlesmn 0:3ac96e360672 468 VL53L1_GetTickCount(&current_time_ms);
charlesmn 0:3ac96e360672 469 pdev->fw_ready_poll_duration_ms =
charlesmn 0:3ac96e360672 470 current_time_ms - start_time_ms;
charlesmn 0:3ac96e360672 471 }
charlesmn 0:3ac96e360672 472
charlesmn 0:3ac96e360672 473 if (fw_ready == 0 && status == VL53L1_ERROR_NONE)
charlesmn 0:3ac96e360672 474 status = VL53L1_ERROR_TIME_OUT;
charlesmn 0:3ac96e360672 475
charlesmn 0:3ac96e360672 476 LOG_FUNCTION_END(status);
charlesmn 0:3ac96e360672 477
charlesmn 0:3ac96e360672 478 return status;
charlesmn 0:3ac96e360672 479 }
charlesmn 0:3ac96e360672 480
charlesmn 0:3ac96e360672 481
charlesmn 0:3ac96e360672 482 VL53L1_Error VL53L1_poll_for_range_completion(
charlesmn 0:3ac96e360672 483 VL53L1_DEV Dev,
charlesmn 0:3ac96e360672 484 uint32_t timeout_ms)
charlesmn 0:3ac96e360672 485 {
charlesmn 0:3ac96e360672 486
charlesmn 0:3ac96e360672 487
charlesmn 0:3ac96e360672 488 VL53L1_Error status = VL53L1_ERROR_NONE;
charlesmn 0:3ac96e360672 489 VL53L1_LLDriverData_t *pdev = VL53L1DevStructGetLLDriverHandle(Dev);
charlesmn 0:3ac96e360672 490
charlesmn 0:3ac96e360672 491 uint8_t gpio__mux_active_high_hv = 0;
charlesmn 0:3ac96e360672 492 uint8_t interrupt_ready = 0;
charlesmn 0:3ac96e360672 493
charlesmn 0:3ac96e360672 494 LOG_FUNCTION_START("");
charlesmn 0:3ac96e360672 495
charlesmn 0:3ac96e360672 496 gpio__mux_active_high_hv =
charlesmn 0:3ac96e360672 497 pdev->stat_cfg.gpio_hv_mux__ctrl &
charlesmn 0:3ac96e360672 498 VL53L1_DEVICEINTERRUPTLEVEL_ACTIVE_MASK;
charlesmn 0:3ac96e360672 499
charlesmn 0:3ac96e360672 500 if (gpio__mux_active_high_hv == VL53L1_DEVICEINTERRUPTLEVEL_ACTIVE_HIGH)
charlesmn 0:3ac96e360672 501 interrupt_ready = 0x01;
charlesmn 0:3ac96e360672 502 else
charlesmn 0:3ac96e360672 503 interrupt_ready = 0x00;
charlesmn 0:3ac96e360672 504
charlesmn 0:3ac96e360672 505 status =
charlesmn 0:3ac96e360672 506 VL53L1_WaitValueMaskEx(
charlesmn 0:3ac96e360672 507 Dev,
charlesmn 0:3ac96e360672 508 timeout_ms,
charlesmn 0:3ac96e360672 509 VL53L1_GPIO__TIO_HV_STATUS,
charlesmn 0:3ac96e360672 510 interrupt_ready,
charlesmn 0:3ac96e360672 511 0x01,
charlesmn 0:3ac96e360672 512 VL53L1_POLLING_DELAY_MS);
charlesmn 0:3ac96e360672 513
charlesmn 0:3ac96e360672 514 LOG_FUNCTION_END(status);
charlesmn 0:3ac96e360672 515 return status;
charlesmn 0:3ac96e360672 516 }
charlesmn 0:3ac96e360672 517
charlesmn 0:3ac96e360672 518
charlesmn 0:3ac96e360672 519