ST Expansion SW Team / VL53L1

Dependencies:   X_NUCLEO_COMMON ST_INTERFACES

Dependents:   X_NUCLEO_53L1CB

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers vl53l1_wait.c Source File

vl53l1_wait.c

00001 
00002 // SPDX-License-Identifier: GPL-2.0+ OR BSD-3-Clause
00003 /******************************************************************************
00004  * Copyright (c) 2020, STMicroelectronics - All Rights Reserved
00005 
00006  This file is part of VL53L1 and is dual licensed,
00007  either GPL-2.0+
00008  or 'BSD 3-clause "New" or "Revised" License' , at your option.
00009  ******************************************************************************
00010  */
00011 
00012 
00013 
00014 
00015 
00016 #include "vl53l1_ll_def.h"
00017 #include "vl53l1_ll_device.h"
00018 #include "vl53l1_platform.h"
00019 #include "vl53l1_core.h"
00020 #include "vl53l1_silicon_core.h"
00021 #include "vl53l1_wait.h"
00022 #include "vl53l1_register_settings.h"
00023 
00024 
00025 #define LOG_FUNCTION_START(fmt, ...) \
00026     _LOG_FUNCTION_START(VL53L1_TRACE_MODULE_CORE, fmt, ##__VA_ARGS__)
00027 #define LOG_FUNCTION_END(status, ...) \
00028     _LOG_FUNCTION_END(VL53L1_TRACE_MODULE_CORE, status, ##__VA_ARGS__)
00029 #define LOG_FUNCTION_END_FMT(status, fmt, ...) \
00030     _LOG_FUNCTION_END_FMT(VL53L1_TRACE_MODULE_CORE, status, \
00031         fmt, ##__VA_ARGS__)
00032 
00033 
00034 VL53L1_Error VL53L1_wait_for_boot_completion(
00035     VL53L1_DEV     Dev)
00036 {
00037 
00038 
00039 
00040     VL53L1_Error status = VL53L1_ERROR_NONE;
00041     VL53L1_LLDriverData_t *pdev = VL53L1DevStructGetLLDriverHandle(Dev);
00042 
00043     uint8_t      fw_ready  = 0;
00044 
00045     LOG_FUNCTION_START("");
00046 
00047     if (pdev->wait_method == VL53L1_WAIT_METHOD_BLOCKING) {
00048 
00049 
00050 
00051         status =
00052             VL53L1_poll_for_boot_completion(
00053                 Dev,
00054                 VL53L1_BOOT_COMPLETION_POLLING_TIMEOUT_MS);
00055 
00056     } else {
00057 
00058 
00059 
00060         fw_ready = 0;
00061         while (fw_ready == 0x00 && status == VL53L1_ERROR_NONE) {
00062             status = VL53L1_is_boot_complete(
00063                 Dev,
00064                 &fw_ready);
00065 
00066             if (status == VL53L1_ERROR_NONE) {
00067                 status = VL53L1_WaitMs(
00068                     Dev,
00069                     VL53L1_POLLING_DELAY_MS);
00070             }
00071         }
00072     }
00073 
00074     LOG_FUNCTION_END(status);
00075 
00076     return status;
00077 
00078 }
00079 
00080 
00081 VL53L1_Error VL53L1_wait_for_firmware_ready(
00082     VL53L1_DEV     Dev)
00083 {
00084 
00085 
00086 
00087     VL53L1_Error status = VL53L1_ERROR_NONE;
00088     VL53L1_LLDriverData_t *pdev = VL53L1DevStructGetLLDriverHandle(Dev);
00089 
00090     uint8_t      fw_ready  = 0;
00091     uint8_t      mode_start  = 0;
00092 
00093     LOG_FUNCTION_START("");
00094 
00095 
00096     mode_start =
00097         pdev->sys_ctrl.system__mode_start &
00098         VL53L1_DEVICEMEASUREMENTMODE_MODE_MASK;
00099 
00100 
00101 
00102     if ((mode_start == VL53L1_DEVICEMEASUREMENTMODE_TIMED) ||
00103         (mode_start == VL53L1_DEVICEMEASUREMENTMODE_SINGLESHOT)) {
00104 
00105         if (pdev->wait_method == VL53L1_WAIT_METHOD_BLOCKING) {
00106 
00107 
00108 
00109             status =
00110             VL53L1_poll_for_firmware_ready(
00111                 Dev,
00112                 VL53L1_RANGE_COMPLETION_POLLING_TIMEOUT_MS);
00113 
00114         } else {
00115 
00116 
00117 
00118             fw_ready = 0;
00119             while (fw_ready == 0x00 && status ==
00120                     VL53L1_ERROR_NONE) {
00121                 status = VL53L1_is_firmware_ready(
00122                     Dev,
00123                     &fw_ready);
00124 
00125                 if (status == VL53L1_ERROR_NONE) {
00126                     status = VL53L1_WaitMs(
00127                         Dev,
00128                         VL53L1_POLLING_DELAY_MS);
00129                 }
00130             }
00131         }
00132     }
00133 
00134     LOG_FUNCTION_END(status);
00135 
00136     return status;
00137 }
00138 
00139 
00140 VL53L1_Error VL53L1_wait_for_range_completion(
00141     VL53L1_DEV     Dev)
00142 {
00143 
00144 
00145 
00146     VL53L1_Error status = VL53L1_ERROR_NONE;
00147     VL53L1_LLDriverData_t *pdev = VL53L1DevStructGetLLDriverHandle(Dev);
00148 
00149     uint8_t      data_ready  = 0;
00150 
00151     LOG_FUNCTION_START("");
00152 
00153     if (pdev->wait_method == VL53L1_WAIT_METHOD_BLOCKING) {
00154 
00155 
00156 
00157         status =
00158             VL53L1_poll_for_range_completion(
00159                 Dev,
00160                 VL53L1_RANGE_COMPLETION_POLLING_TIMEOUT_MS);
00161 
00162     } else {
00163 
00164 
00165 
00166         data_ready = 0;
00167         while (data_ready == 0x00 && status == VL53L1_ERROR_NONE) {
00168             status = VL53L1_is_new_data_ready(
00169                 Dev,
00170                 &data_ready);
00171 
00172             if (status == VL53L1_ERROR_NONE) {
00173                 status = VL53L1_WaitMs(
00174                     Dev,
00175                     VL53L1_POLLING_DELAY_MS);
00176             }
00177         }
00178     }
00179 
00180     LOG_FUNCTION_END(status);
00181 
00182     return status;
00183 }
00184 
00185 
00186 VL53L1_Error VL53L1_wait_for_test_completion(
00187     VL53L1_DEV     Dev)
00188 {
00189 
00190 
00191 
00192     VL53L1_Error status = VL53L1_ERROR_NONE;
00193     VL53L1_LLDriverData_t *pdev = VL53L1DevStructGetLLDriverHandle(Dev);
00194 
00195     uint8_t      data_ready  = 0;
00196 
00197     LOG_FUNCTION_START("");
00198 
00199     if (pdev->wait_method == VL53L1_WAIT_METHOD_BLOCKING) {
00200 
00201 
00202 
00203         status =
00204             VL53L1_poll_for_range_completion(
00205                 Dev,
00206                 VL53L1_TEST_COMPLETION_POLLING_TIMEOUT_MS);
00207 
00208     } else {
00209 
00210 
00211 
00212         data_ready = 0;
00213         while (data_ready == 0x00 && status == VL53L1_ERROR_NONE) {
00214             status = VL53L1_is_new_data_ready(
00215                 Dev,
00216                 &data_ready);
00217 
00218             if (status == VL53L1_ERROR_NONE) {
00219                 status = VL53L1_WaitMs(
00220                     Dev,
00221                     VL53L1_POLLING_DELAY_MS);
00222             }
00223         }
00224     }
00225 
00226     LOG_FUNCTION_END(status);
00227 
00228     return status;
00229 }
00230 
00231 
00232 
00233 
00234 VL53L1_Error VL53L1_is_boot_complete(
00235     VL53L1_DEV     Dev,
00236     uint8_t       *pready)
00237 {
00238 
00239 
00240     VL53L1_Error status = VL53L1_ERROR_NONE;
00241     uint8_t  firmware__system_status = 0;
00242 
00243     LOG_FUNCTION_START("");
00244 
00245 
00246 
00247     status =
00248         VL53L1_RdByte(
00249             Dev,
00250             VL53L1_FIRMWARE__SYSTEM_STATUS,
00251             &firmware__system_status);
00252 
00253 
00254 
00255     if ((firmware__system_status & 0x01) == 0x01) {
00256         *pready = 0x01;
00257         VL53L1_init_ll_driver_state(
00258             Dev,
00259             VL53L1_DEVICESTATE_SW_STANDBY);
00260     } else {
00261         *pready = 0x00;
00262         VL53L1_init_ll_driver_state(
00263             Dev,
00264             VL53L1_DEVICESTATE_FW_COLDBOOT);
00265     }
00266 
00267     LOG_FUNCTION_END(status);
00268 
00269     return status;
00270 }
00271 
00272 
00273 VL53L1_Error VL53L1_is_firmware_ready(
00274     VL53L1_DEV     Dev,
00275     uint8_t       *pready)
00276 {
00277 
00278 
00279     VL53L1_Error status = VL53L1_ERROR_NONE;
00280     VL53L1_LLDriverData_t *pdev = VL53L1DevStructGetLLDriverHandle(Dev);
00281 
00282     LOG_FUNCTION_START("");
00283 
00284     status = VL53L1_is_firmware_ready_silicon(
00285                 Dev,
00286                 pready);
00287 
00288     pdev->fw_ready = *pready;
00289 
00290     LOG_FUNCTION_END(status);
00291 
00292     return status;
00293 }
00294 
00295 
00296 VL53L1_Error VL53L1_is_new_data_ready(
00297     VL53L1_DEV     Dev,
00298     uint8_t       *pready)
00299 {
00300 
00301 
00302     VL53L1_Error status = VL53L1_ERROR_NONE;
00303     VL53L1_LLDriverData_t *pdev = VL53L1DevStructGetLLDriverHandle(Dev);
00304 
00305     uint8_t  gpio__mux_active_high_hv = 0;
00306     uint8_t  gpio__tio_hv_status      = 0;
00307     uint8_t  interrupt_ready          = 0;
00308 
00309     LOG_FUNCTION_START("");
00310 
00311     gpio__mux_active_high_hv =
00312             pdev->stat_cfg.gpio_hv_mux__ctrl &
00313             VL53L1_DEVICEINTERRUPTLEVEL_ACTIVE_MASK;
00314 
00315     if (gpio__mux_active_high_hv == VL53L1_DEVICEINTERRUPTLEVEL_ACTIVE_HIGH)
00316         interrupt_ready = 0x01;
00317     else
00318         interrupt_ready = 0x00;
00319 
00320 
00321 
00322     status = VL53L1_RdByte(
00323                     Dev,
00324                     VL53L1_GPIO__TIO_HV_STATUS,
00325                     &gpio__tio_hv_status);
00326 
00327 
00328 
00329     if ((gpio__tio_hv_status & 0x01) == interrupt_ready)
00330         *pready = 0x01;
00331     else
00332         *pready = 0x00;
00333 
00334     LOG_FUNCTION_END(status);
00335 
00336     return status;
00337 }
00338 
00339 
00340 
00341 
00342 VL53L1_Error VL53L1_poll_for_boot_completion(
00343     VL53L1_DEV    Dev,
00344     uint32_t      timeout_ms)
00345 {
00346 
00347 
00348     VL53L1_Error status       = VL53L1_ERROR_NONE;
00349 
00350     LOG_FUNCTION_START("");
00351 
00352 
00353 
00354     status = VL53L1_WaitUs(
00355             Dev,
00356             VL53L1_FIRMWARE_BOOT_TIME_US);
00357 
00358     if (status == VL53L1_ERROR_NONE)
00359         status =
00360             VL53L1_WaitValueMaskEx(
00361                 Dev,
00362                 timeout_ms,
00363                 VL53L1_FIRMWARE__SYSTEM_STATUS,
00364                 0x01,
00365                 0x01,
00366                 VL53L1_POLLING_DELAY_MS);
00367 
00368     if (status == VL53L1_ERROR_NONE)
00369         VL53L1_init_ll_driver_state(Dev, VL53L1_DEVICESTATE_SW_STANDBY);
00370 
00371     LOG_FUNCTION_END(status);
00372 
00373     return status;
00374 }
00375 
00376 
00377 VL53L1_Error VL53L1_poll_for_firmware_ready(
00378     VL53L1_DEV    Dev,
00379     uint32_t      timeout_ms)
00380 {
00381 
00382 
00383     VL53L1_Error status          = VL53L1_ERROR_NONE;
00384     VL53L1_LLDriverData_t *pdev = VL53L1DevStructGetLLDriverHandle(Dev);
00385 
00386     uint32_t     start_time_ms   = 0;
00387     uint32_t     current_time_ms = 0;
00388     int32_t      poll_delay_ms   = VL53L1_POLLING_DELAY_MS;
00389     uint8_t      fw_ready        = 0;
00390 
00391 
00392 
00393     VL53L1_GetTickCount(&start_time_ms);
00394     pdev->fw_ready_poll_duration_ms = 0;
00395 
00396 
00397 
00398     while ((status == VL53L1_ERROR_NONE) &&
00399            (pdev->fw_ready_poll_duration_ms < timeout_ms) &&
00400            (fw_ready == 0)) {
00401 
00402         status = VL53L1_is_firmware_ready(
00403             Dev,
00404             &fw_ready);
00405 
00406         if (status == VL53L1_ERROR_NONE &&
00407             fw_ready == 0 &&
00408             poll_delay_ms > 0) {
00409             status = VL53L1_WaitMs(
00410                 Dev,
00411                 poll_delay_ms);
00412         }
00413 
00414 
00415         VL53L1_GetTickCount(&current_time_ms);
00416         pdev->fw_ready_poll_duration_ms =
00417                 current_time_ms - start_time_ms;
00418     }
00419 
00420     if (fw_ready == 0 && status == VL53L1_ERROR_NONE)
00421         status = VL53L1_ERROR_TIME_OUT;
00422 
00423     LOG_FUNCTION_END(status);
00424 
00425     return status;
00426 }
00427 
00428 
00429 VL53L1_Error VL53L1_poll_for_range_completion(
00430     VL53L1_DEV     Dev,
00431     uint32_t       timeout_ms)
00432 {
00433 
00434 
00435     VL53L1_Error status = VL53L1_ERROR_NONE;
00436     VL53L1_LLDriverData_t *pdev = VL53L1DevStructGetLLDriverHandle(Dev);
00437 
00438     uint8_t  gpio__mux_active_high_hv = 0;
00439     uint8_t  interrupt_ready          = 0;
00440 
00441     LOG_FUNCTION_START("");
00442 
00443     gpio__mux_active_high_hv =
00444             pdev->stat_cfg.gpio_hv_mux__ctrl &
00445             VL53L1_DEVICEINTERRUPTLEVEL_ACTIVE_MASK;
00446 
00447     if (gpio__mux_active_high_hv == VL53L1_DEVICEINTERRUPTLEVEL_ACTIVE_HIGH)
00448         interrupt_ready = 0x01;
00449     else
00450         interrupt_ready = 0x00;
00451 
00452     status =
00453         VL53L1_WaitValueMaskEx(
00454             Dev,
00455             timeout_ms,
00456             VL53L1_GPIO__TIO_HV_STATUS,
00457             interrupt_ready,
00458             0x01,
00459             VL53L1_POLLING_DELAY_MS);
00460 
00461     LOG_FUNCTION_END(status);
00462 
00463     return status;
00464 }
00465 
00466