charles macneill / VL53L1CB
Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers vl53l1_wait.c Source File

vl53l1_wait.c

00001 
00002 /*******************************************************************************
00003  * Copyright (c) 2020, STMicroelectronics - All Rights Reserved
00004 
00005  This file is part of VL53L1 Core and is dual licensed,
00006  either 'STMicroelectronics
00007  Proprietary license'
00008  or 'BSD 3-clause "New" or "Revised" License' , at your option.
00009 
00010 ********************************************************************************
00011 
00012  'STMicroelectronics Proprietary license'
00013 
00014 ********************************************************************************
00015 
00016  License terms: STMicroelectronics Proprietary in accordance with licensing
00017  terms at www.st.com/sla0081
00018 
00019  STMicroelectronics confidential
00020  Reproduction and Communication of this document is strictly prohibited unless
00021  specifically authorized in writing by STMicroelectronics.
00022 
00023 
00024 ********************************************************************************
00025 
00026  Alternatively, VL53L1 Core may be distributed under the terms of
00027  'BSD 3-clause "New" or "Revised" License', in which case the following
00028  provisions apply instead of the ones
00029  mentioned above :
00030 
00031 ********************************************************************************
00032 
00033  License terms: BSD 3-clause "New" or "Revised" License.
00034 
00035  Redistribution and use in source and binary forms, with or without
00036  modification, are permitted provided that the following conditions are met:
00037 
00038  1. Redistributions of source code must retain the above copyright notice, this
00039  list of conditions and the following disclaimer.
00040 
00041  2. Redistributions in binary form must reproduce the above copyright notice,
00042  this list of conditions and the following disclaimer in the documentation
00043  and/or other materials provided with the distribution.
00044 
00045  3. Neither the name of the copyright holder nor the names of its contributors
00046  may be used to endorse or promote products derived from this software
00047  without specific prior written permission.
00048 
00049  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
00050  AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00051  IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
00052  DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
00053  FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
00054  DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
00055  SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
00056  CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
00057  OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
00058  OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00059 
00060 
00061 ********************************************************************************
00062 
00063 */
00064 
00065 
00066 
00067 
00068 
00069 #include "vl53l1_ll_def.h"
00070 #include "vl53l1_ll_device.h"
00071 #include "vl53l1_platform.h"
00072 #include "vl53l1_core.h"
00073 #include "vl53l1_silicon_core.h"
00074 #include "vl53l1_wait.h"
00075 #include "vl53l1_register_settings.h"
00076 
00077 
00078 #define LOG_FUNCTION_START(fmt, ...) \
00079     _LOG_FUNCTION_START(VL53L1_TRACE_MODULE_CORE, fmt, ##__VA_ARGS__)
00080 #define LOG_FUNCTION_END(status, ...) \
00081     _LOG_FUNCTION_END(VL53L1_TRACE_MODULE_CORE, status, ##__VA_ARGS__)
00082 #define LOG_FUNCTION_END_FMT(status, fmt, ...) \
00083     _LOG_FUNCTION_END_FMT(VL53L1_TRACE_MODULE_CORE, status, \
00084         fmt, ##__VA_ARGS__)
00085 
00086 
00087 VL53L1_Error VL53L1_wait_for_boot_completion(
00088     VL53L1_DEV     Dev)
00089 {
00090 
00091 
00092 
00093     VL53L1_Error status = VL53L1_ERROR_NONE;
00094     VL53L1_LLDriverData_t *pdev = VL53L1DevStructGetLLDriverHandle(Dev);
00095 
00096     uint8_t      fw_ready  = 0;
00097 
00098     LOG_FUNCTION_START("");
00099 
00100     if (pdev->wait_method == VL53L1_WAIT_METHOD_BLOCKING) {
00101 
00102 
00103 
00104         status =
00105             VL53L1_poll_for_boot_completion(
00106                 Dev,
00107                 VL53L1_BOOT_COMPLETION_POLLING_TIMEOUT_MS);
00108 
00109     } else {
00110 
00111 
00112 
00113         fw_ready = 0;
00114         while (fw_ready == 0x00 && status == VL53L1_ERROR_NONE) {
00115             status = VL53L1_is_boot_complete(
00116                 Dev,
00117                 &fw_ready);
00118 
00119             if (status == VL53L1_ERROR_NONE) {
00120                 status = VL53L1_WaitMs(
00121                     Dev,
00122                     VL53L1_POLLING_DELAY_MS);
00123             }
00124         }
00125     }
00126 
00127     LOG_FUNCTION_END(status);
00128 
00129     return status;
00130 
00131 }
00132 
00133 
00134 VL53L1_Error VL53L1_wait_for_firmware_ready(
00135     VL53L1_DEV     Dev)
00136 {
00137 
00138 
00139 
00140     VL53L1_Error status = VL53L1_ERROR_NONE;
00141     VL53L1_LLDriverData_t *pdev = VL53L1DevStructGetLLDriverHandle(Dev);
00142 
00143     uint8_t      fw_ready  = 0;
00144     uint8_t      mode_start  = 0;
00145 
00146     LOG_FUNCTION_START("");
00147 
00148 
00149     mode_start =
00150         pdev->sys_ctrl.system__mode_start &
00151         VL53L1_DEVICEMEASUREMENTMODE_MODE_MASK;
00152 
00153 
00154 
00155     if ((mode_start == VL53L1_DEVICEMEASUREMENTMODE_TIMED) ||
00156         (mode_start == VL53L1_DEVICEMEASUREMENTMODE_SINGLESHOT)) {
00157 
00158         if (pdev->wait_method == VL53L1_WAIT_METHOD_BLOCKING) {
00159 
00160 
00161 
00162             status =
00163             VL53L1_poll_for_firmware_ready(
00164                 Dev,
00165                 VL53L1_RANGE_COMPLETION_POLLING_TIMEOUT_MS);
00166 
00167         } else {
00168 
00169 
00170 
00171             fw_ready = 0;
00172             while (fw_ready == 0x00 && status ==
00173                     VL53L1_ERROR_NONE) {
00174                 status = VL53L1_is_firmware_ready(
00175                     Dev,
00176                     &fw_ready);
00177 
00178                 if (status == VL53L1_ERROR_NONE) {
00179                     status = VL53L1_WaitMs(
00180                         Dev,
00181                         VL53L1_POLLING_DELAY_MS);
00182                 }
00183             }
00184         }
00185     }
00186 
00187     LOG_FUNCTION_END(status);
00188 
00189     return status;
00190 }
00191 
00192 
00193 VL53L1_Error VL53L1_wait_for_range_completion(
00194     VL53L1_DEV     Dev)
00195 {
00196 
00197 
00198 
00199     VL53L1_Error status = VL53L1_ERROR_NONE;
00200     VL53L1_LLDriverData_t *pdev = VL53L1DevStructGetLLDriverHandle(Dev);
00201 
00202     uint8_t      data_ready  = 0;
00203 
00204     LOG_FUNCTION_START("");
00205 
00206     if (pdev->wait_method == VL53L1_WAIT_METHOD_BLOCKING) {
00207 
00208 
00209 
00210         status =
00211             VL53L1_poll_for_range_completion(
00212                 Dev,
00213                 VL53L1_RANGE_COMPLETION_POLLING_TIMEOUT_MS);
00214 
00215     } else {
00216 
00217 
00218 
00219         data_ready = 0;
00220         while (data_ready == 0x00 && status == VL53L1_ERROR_NONE) {
00221             status = VL53L1_is_new_data_ready(
00222                 Dev,
00223                 &data_ready);
00224 
00225             if (status == VL53L1_ERROR_NONE) {
00226                 status = VL53L1_WaitMs(
00227                     Dev,
00228                     VL53L1_POLLING_DELAY_MS);
00229             }
00230         }
00231     }
00232 
00233     LOG_FUNCTION_END(status);
00234 
00235     return status;
00236 }
00237 
00238 
00239 VL53L1_Error VL53L1_wait_for_test_completion(
00240     VL53L1_DEV     Dev)
00241 {
00242 
00243 
00244 
00245     VL53L1_Error status = VL53L1_ERROR_NONE;
00246     VL53L1_LLDriverData_t *pdev = VL53L1DevStructGetLLDriverHandle(Dev);
00247 
00248     uint8_t      data_ready  = 0;
00249 
00250     LOG_FUNCTION_START("");
00251 
00252     if (pdev->wait_method == VL53L1_WAIT_METHOD_BLOCKING) {
00253 
00254 
00255 
00256         status =
00257             VL53L1_poll_for_range_completion(
00258                 Dev,
00259                 VL53L1_TEST_COMPLETION_POLLING_TIMEOUT_MS);
00260 
00261     } else {
00262 
00263 
00264 
00265         data_ready = 0;
00266         while (data_ready == 0x00 && status == VL53L1_ERROR_NONE) {
00267             status = VL53L1_is_new_data_ready(
00268                 Dev,
00269                 &data_ready);
00270 
00271             if (status == VL53L1_ERROR_NONE) {
00272                 status = VL53L1_WaitMs(
00273                     Dev,
00274                     VL53L1_POLLING_DELAY_MS);
00275             }
00276         }
00277     }
00278 
00279     LOG_FUNCTION_END(status);
00280 
00281     return status;
00282 }
00283 
00284 
00285 
00286 
00287 VL53L1_Error VL53L1_is_boot_complete(
00288     VL53L1_DEV     Dev,
00289     uint8_t       *pready)
00290 {
00291 
00292 
00293     VL53L1_Error status = VL53L1_ERROR_NONE;
00294     uint8_t  firmware__system_status = 0;
00295 
00296     LOG_FUNCTION_START("");
00297 
00298 
00299 
00300     status =
00301         VL53L1_RdByte(
00302             Dev,
00303             VL53L1_FIRMWARE__SYSTEM_STATUS,
00304             &firmware__system_status);
00305 
00306 
00307 
00308     if ((firmware__system_status & 0x01) == 0x01) {
00309         *pready = 0x01;
00310         VL53L1_init_ll_driver_state(
00311             Dev,
00312             VL53L1_DEVICESTATE_SW_STANDBY);
00313     } else {
00314         *pready = 0x00;
00315         VL53L1_init_ll_driver_state(
00316             Dev,
00317             VL53L1_DEVICESTATE_FW_COLDBOOT);
00318     }
00319 
00320     LOG_FUNCTION_END(status);
00321 
00322     return status;
00323 }
00324 
00325 
00326 VL53L1_Error VL53L1_is_firmware_ready(
00327     VL53L1_DEV     Dev,
00328     uint8_t       *pready)
00329 {
00330 
00331 
00332     VL53L1_Error status = VL53L1_ERROR_NONE;
00333     VL53L1_LLDriverData_t *pdev = VL53L1DevStructGetLLDriverHandle(Dev);
00334 
00335     LOG_FUNCTION_START("");
00336 
00337     status = VL53L1_is_firmware_ready_silicon(
00338                 Dev,
00339                 pready);
00340 
00341     pdev->fw_ready = *pready;
00342 
00343     LOG_FUNCTION_END(status);
00344 
00345     return status;
00346 }
00347 
00348 
00349 VL53L1_Error VL53L1_is_new_data_ready(
00350     VL53L1_DEV     Dev,
00351     uint8_t       *pready)
00352 {
00353 
00354 
00355     VL53L1_Error status = VL53L1_ERROR_NONE;
00356     VL53L1_LLDriverData_t *pdev = VL53L1DevStructGetLLDriverHandle(Dev);
00357 
00358     uint8_t  gpio__mux_active_high_hv = 0;
00359     uint8_t  gpio__tio_hv_status      = 0;
00360     uint8_t  interrupt_ready          = 0;
00361 
00362     LOG_FUNCTION_START("");
00363 
00364     gpio__mux_active_high_hv =
00365             pdev->stat_cfg.gpio_hv_mux__ctrl &
00366             VL53L1_DEVICEINTERRUPTLEVEL_ACTIVE_MASK;
00367 
00368     if (gpio__mux_active_high_hv == VL53L1_DEVICEINTERRUPTLEVEL_ACTIVE_HIGH)
00369         interrupt_ready = 0x01;
00370     else
00371         interrupt_ready = 0x00;
00372 
00373 
00374 
00375     status = VL53L1_RdByte(
00376                     Dev,
00377                     VL53L1_GPIO__TIO_HV_STATUS,
00378                     &gpio__tio_hv_status);
00379 
00380 
00381 
00382     if ((gpio__tio_hv_status & 0x01) == interrupt_ready)
00383         *pready = 0x01;
00384     else
00385         *pready = 0x00;
00386 
00387     LOG_FUNCTION_END(status);
00388 
00389     return status;
00390 }
00391 
00392 
00393 
00394 
00395 VL53L1_Error VL53L1_poll_for_boot_completion(
00396     VL53L1_DEV    Dev,
00397     uint32_t      timeout_ms)
00398 {
00399 
00400 
00401     VL53L1_Error status       = VL53L1_ERROR_NONE;
00402 
00403     LOG_FUNCTION_START("");
00404 
00405 
00406 
00407     status = VL53L1_WaitUs(
00408             Dev,
00409             VL53L1_FIRMWARE_BOOT_TIME_US);
00410 
00411     if (status == VL53L1_ERROR_NONE)
00412         status =
00413             VL53L1_WaitValueMaskEx(
00414                 Dev,
00415                 timeout_ms,
00416                 VL53L1_FIRMWARE__SYSTEM_STATUS,
00417                 0x01,
00418                 0x01,
00419                 VL53L1_POLLING_DELAY_MS);
00420 
00421     if (status == VL53L1_ERROR_NONE)
00422         VL53L1_init_ll_driver_state(Dev, VL53L1_DEVICESTATE_SW_STANDBY);
00423 
00424     LOG_FUNCTION_END(status);
00425 
00426     return status;
00427 }
00428 
00429 
00430 VL53L1_Error VL53L1_poll_for_firmware_ready(
00431     VL53L1_DEV    Dev,
00432     uint32_t      timeout_ms)
00433 {
00434 
00435 
00436     VL53L1_Error status          = VL53L1_ERROR_NONE;
00437     VL53L1_LLDriverData_t *pdev = VL53L1DevStructGetLLDriverHandle(Dev);
00438 
00439     uint32_t     start_time_ms   = 0;
00440     uint32_t     current_time_ms = 0;
00441     int32_t      poll_delay_ms   = VL53L1_POLLING_DELAY_MS;
00442     uint8_t      fw_ready        = 0;
00443 
00444 
00445 
00446     VL53L1_GetTickCount(&start_time_ms);
00447     pdev->fw_ready_poll_duration_ms = 0;
00448 
00449 
00450 
00451     while ((status == VL53L1_ERROR_NONE) &&
00452            (pdev->fw_ready_poll_duration_ms < timeout_ms) &&
00453            (fw_ready == 0)) {
00454 
00455         status = VL53L1_is_firmware_ready(
00456             Dev,
00457             &fw_ready);
00458 
00459         if (status == VL53L1_ERROR_NONE &&
00460             fw_ready == 0 &&
00461             poll_delay_ms > 0) {
00462             status = VL53L1_WaitMs(
00463                 Dev,
00464                 poll_delay_ms);
00465         }
00466 
00467 
00468         VL53L1_GetTickCount(&current_time_ms);
00469         pdev->fw_ready_poll_duration_ms =
00470                 current_time_ms - start_time_ms;
00471     }
00472 
00473     if (fw_ready == 0 && status == VL53L1_ERROR_NONE)
00474         status = VL53L1_ERROR_TIME_OUT;
00475 
00476     LOG_FUNCTION_END(status);
00477 
00478     return status;
00479 }
00480 
00481 
00482 VL53L1_Error VL53L1_poll_for_range_completion(
00483     VL53L1_DEV     Dev,
00484     uint32_t       timeout_ms)
00485 {
00486 
00487 
00488     VL53L1_Error status = VL53L1_ERROR_NONE;
00489     VL53L1_LLDriverData_t *pdev = VL53L1DevStructGetLLDriverHandle(Dev);
00490 
00491     uint8_t  gpio__mux_active_high_hv = 0;
00492     uint8_t  interrupt_ready          = 0;
00493 
00494     LOG_FUNCTION_START("");
00495 
00496     gpio__mux_active_high_hv =
00497             pdev->stat_cfg.gpio_hv_mux__ctrl &
00498             VL53L1_DEVICEINTERRUPTLEVEL_ACTIVE_MASK;
00499 
00500     if (gpio__mux_active_high_hv == VL53L1_DEVICEINTERRUPTLEVEL_ACTIVE_HIGH)
00501         interrupt_ready = 0x01;
00502     else
00503         interrupt_ready = 0x00;
00504 
00505     status =
00506         VL53L1_WaitValueMaskEx(
00507             Dev,
00508             timeout_ms,
00509             VL53L1_GPIO__TIO_HV_STATUS,
00510             interrupt_ready,
00511             0x01,
00512             VL53L1_POLLING_DELAY_MS);
00513 
00514     LOG_FUNCTION_END(status);
00515     return status;
00516 }
00517 
00518 
00519