Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: ST_INTERFACES X_NUCLEO_COMMON
Dependents: ConcorsoFinal HelloWorld_IHM01A1 m3pi_BT m3pi_LIDAR ... more
Fork of X_NUCLEO_53L0A1 by
X-NUCLEO-53L0A1 Proximity Sensor Expansion Board Firmware Package
Introduction
This firmware package includes Component Device Drivers and the Board Support Package for STMicroelectronics' X-NUCLEO-53L0A1 Proximity sensor expansion board based on VL53L0X.
Firmware Library
Class X_NUCLEO_53L0A1 is intended to represent the Proximity sensor expansion board with the same name.
The expansion board provides support for the following components:
- on-board VL53L0X proximity sensor,
- up to two additional VL53L0X Satellites,
- on-board 4-digit display
It is intentionally implemented as a singleton because only one X-NUCLEO-VL53L0A1 may be deployed at a time in a HW component stack. In order to get the singleton instance you have to call class method `Instance()`, e.g.:
// Sensors expansion board singleton instance static X_NUCLEO_53L0A1 *board = X_NUCLEO_53L0A1::Instance(device_i2c, A2, D8, D2);
Example Applications
- Hello World 53L0
- Display 53L0A1
- Display 53L0A1 Interrupts
- Display 53L0A1 with satellites
- 53L0A1 Satellites with Interrupts
- 53L0A1_HandGestureRecognition
The library and sample application code were tested against mbed revision 143, dated 26th May 2017.
Components/VL53L0X/vl53l0x_class.cpp@14:8320b5ff96fa, 2017-08-07 (annotated)
- Committer:
- johnAlexander
- Date:
- Mon Aug 07 14:30:21 2017 +0000
- Revision:
- 14:8320b5ff96fa
- Parent:
- 13:615f7e38568c
- Child:
- 15:44e6c9013bff
Aligned to ARM mbed coding style.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
johnAlexander | 0:c523920bcc09 | 1 | /** |
johnAlexander | 0:c523920bcc09 | 2 | ****************************************************************************** |
johnAlexander | 0:c523920bcc09 | 3 | * @file vl53l0x_class.cpp |
johnAlexander | 0:c523920bcc09 | 4 | * @author IMG |
johnAlexander | 0:c523920bcc09 | 5 | * @version V0.0.1 |
johnAlexander | 0:c523920bcc09 | 6 | * @date 28-June-2016 |
johnAlexander | 0:c523920bcc09 | 7 | * @brief Implementation file for the VL53L0X driver class |
johnAlexander | 0:c523920bcc09 | 8 | ****************************************************************************** |
johnAlexander | 0:c523920bcc09 | 9 | * @attention |
johnAlexander | 0:c523920bcc09 | 10 | * |
johnAlexander | 0:c523920bcc09 | 11 | * <h2><center>© COPYRIGHT(c) 2016 STMicroelectronics</center></h2> |
johnAlexander | 0:c523920bcc09 | 12 | * |
johnAlexander | 0:c523920bcc09 | 13 | * Redistribution and use in source and binary forms, with or without modification, |
johnAlexander | 0:c523920bcc09 | 14 | * are permitted provided that the following conditions are met: |
johnAlexander | 0:c523920bcc09 | 15 | * 1. Redistributions of source code must retain the above copyright notice, |
johnAlexander | 0:c523920bcc09 | 16 | * this list of conditions and the following disclaimer. |
johnAlexander | 0:c523920bcc09 | 17 | * 2. Redistributions in binary form must reproduce the above copyright notice, |
johnAlexander | 0:c523920bcc09 | 18 | * this list of conditions and the following disclaimer in the documentation |
johnAlexander | 0:c523920bcc09 | 19 | * and/or other materials provided with the distribution. |
johnAlexander | 0:c523920bcc09 | 20 | * 3. Neither the name of STMicroelectronics nor the names of its contributors |
johnAlexander | 0:c523920bcc09 | 21 | * may be used to endorse or promote products derived from this software |
johnAlexander | 0:c523920bcc09 | 22 | * without specific prior written permission. |
johnAlexander | 0:c523920bcc09 | 23 | * |
johnAlexander | 0:c523920bcc09 | 24 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
johnAlexander | 0:c523920bcc09 | 25 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
johnAlexander | 0:c523920bcc09 | 26 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
johnAlexander | 0:c523920bcc09 | 27 | * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE |
johnAlexander | 0:c523920bcc09 | 28 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
johnAlexander | 0:c523920bcc09 | 29 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR |
johnAlexander | 0:c523920bcc09 | 30 | * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER |
johnAlexander | 0:c523920bcc09 | 31 | * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, |
johnAlexander | 0:c523920bcc09 | 32 | * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
johnAlexander | 0:c523920bcc09 | 33 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
johnAlexander | 0:c523920bcc09 | 34 | * |
johnAlexander | 0:c523920bcc09 | 35 | ****************************************************************************** |
johnAlexander | 0:c523920bcc09 | 36 | */ |
johnAlexander | 14:8320b5ff96fa | 37 | |
johnAlexander | 0:c523920bcc09 | 38 | /* Includes */ |
johnAlexander | 0:c523920bcc09 | 39 | #include <stdlib.h> |
johnAlexander | 0:c523920bcc09 | 40 | |
johnAlexander | 0:c523920bcc09 | 41 | #include "vl53l0x_class.h" |
johnAlexander | 0:c523920bcc09 | 42 | |
johnAlexander | 0:c523920bcc09 | 43 | //#include "vl53l0x_api_core.h" |
johnAlexander | 0:c523920bcc09 | 44 | //#include "vl53l0x_api_calibration.h" |
johnAlexander | 0:c523920bcc09 | 45 | //#include "vl53l0x_api_strings.h" |
johnAlexander | 0:c523920bcc09 | 46 | #include "vl53l0x_interrupt_threshold_settings.h" |
johnAlexander | 0:c523920bcc09 | 47 | #include "vl53l0x_tuning.h" |
johnAlexander | 0:c523920bcc09 | 48 | #include "vl53l0x_types.h" |
johnAlexander | 0:c523920bcc09 | 49 | |
johnAlexander | 0:c523920bcc09 | 50 | |
johnAlexander | 0:c523920bcc09 | 51 | /****************** define for i2c configuration *******************************/ |
johnAlexander | 14:8320b5ff96fa | 52 | |
johnAlexander | 0:c523920bcc09 | 53 | #define TEMP_BUF_SIZE 64 |
johnAlexander | 0:c523920bcc09 | 54 | |
johnAlexander | 0:c523920bcc09 | 55 | /** Maximum buffer size to be used in i2c */ |
johnAlexander | 0:c523920bcc09 | 56 | #define VL53L0X_MAX_I2C_XFER_SIZE 64 /* Maximum buffer size to be used in i2c */ |
johnAlexander | 0:c523920bcc09 | 57 | #define VL53L0X_I2C_USER_VAR /* none but could be for a flag var to get/pass to mutex interruptible return flags and try again */ |
johnAlexander | 14:8320b5ff96fa | 58 | |
johnAlexander | 0:c523920bcc09 | 59 | |
johnAlexander | 0:c523920bcc09 | 60 | #define LOG_FUNCTION_START(fmt, ...) \ |
johnAlexander | 0:c523920bcc09 | 61 | _LOG_FUNCTION_START(TRACE_MODULE_API, fmt, ##__VA_ARGS__) |
johnAlexander | 0:c523920bcc09 | 62 | #define LOG_FUNCTION_END(status, ...) \ |
johnAlexander | 0:c523920bcc09 | 63 | _LOG_FUNCTION_END(TRACE_MODULE_API, status, ##__VA_ARGS__) |
johnAlexander | 0:c523920bcc09 | 64 | #define LOG_FUNCTION_END_FMT(status, fmt, ...) \ |
johnAlexander | 0:c523920bcc09 | 65 | _LOG_FUNCTION_END_FMT(TRACE_MODULE_API, status, fmt, ##__VA_ARGS__) |
johnAlexander | 0:c523920bcc09 | 66 | |
johnAlexander | 0:c523920bcc09 | 67 | #ifdef VL53L0X_LOG_ENABLE |
johnAlexander | 0:c523920bcc09 | 68 | #define trace_print(level, ...) trace_print_module_function(TRACE_MODULE_API, \ |
johnAlexander | 0:c523920bcc09 | 69 | level, TRACE_FUNCTION_NONE, ##__VA_ARGS__) |
johnAlexander | 0:c523920bcc09 | 70 | #endif |
johnAlexander | 0:c523920bcc09 | 71 | |
johnAlexander | 0:c523920bcc09 | 72 | #define REF_ARRAY_SPAD_0 0 |
johnAlexander | 0:c523920bcc09 | 73 | #define REF_ARRAY_SPAD_5 5 |
johnAlexander | 0:c523920bcc09 | 74 | #define REF_ARRAY_SPAD_10 10 |
johnAlexander | 0:c523920bcc09 | 75 | |
johnAlexander | 0:c523920bcc09 | 76 | uint32_t refArrayQuadrants[4] = {REF_ARRAY_SPAD_10, REF_ARRAY_SPAD_5, |
johnAlexander | 14:8320b5ff96fa | 77 | REF_ARRAY_SPAD_0, REF_ARRAY_SPAD_5 |
johnAlexander | 14:8320b5ff96fa | 78 | }; |
johnAlexander | 14:8320b5ff96fa | 79 | |
johnAlexander | 14:8320b5ff96fa | 80 | |
johnAlexander | 14:8320b5ff96fa | 81 | |
johnAlexander | 14:8320b5ff96fa | 82 | |
johnAlexander | 14:8320b5ff96fa | 83 | VL53L0X_Error VL53L0X::vl53l0x_device_read_strobe(VL53L0X_DEV dev) |
johnAlexander | 0:c523920bcc09 | 84 | { |
johnAlexander | 14:8320b5ff96fa | 85 | VL53L0X_Error status = VL53L0X_ERROR_NONE; |
johnAlexander | 14:8320b5ff96fa | 86 | uint8_t strobe; |
johnAlexander | 14:8320b5ff96fa | 87 | uint32_t loop_nb; |
johnAlexander | 14:8320b5ff96fa | 88 | LOG_FUNCTION_START(""); |
johnAlexander | 14:8320b5ff96fa | 89 | |
johnAlexander | 14:8320b5ff96fa | 90 | status |= vl53l0x_write_byte(dev, 0x83, 0x00); |
johnAlexander | 14:8320b5ff96fa | 91 | |
johnAlexander | 14:8320b5ff96fa | 92 | /* polling |
johnAlexander | 14:8320b5ff96fa | 93 | * use timeout to avoid deadlock*/ |
johnAlexander | 14:8320b5ff96fa | 94 | if (status == VL53L0X_ERROR_NONE) { |
johnAlexander | 14:8320b5ff96fa | 95 | loop_nb = 0; |
johnAlexander | 14:8320b5ff96fa | 96 | do { |
johnAlexander | 14:8320b5ff96fa | 97 | status = vl53l0x_read_byte(dev, 0x83, &strobe); |
johnAlexander | 14:8320b5ff96fa | 98 | if ((strobe != 0x00) || status != VL53L0X_ERROR_NONE) |
johnAlexander | 14:8320b5ff96fa | 99 | break; |
johnAlexander | 14:8320b5ff96fa | 100 | |
johnAlexander | 14:8320b5ff96fa | 101 | loop_nb = loop_nb + 1; |
johnAlexander | 14:8320b5ff96fa | 102 | } while (loop_nb < VL53L0X_DEFAULT_MAX_LOOP); |
johnAlexander | 14:8320b5ff96fa | 103 | |
johnAlexander | 14:8320b5ff96fa | 104 | if (loop_nb >= VL53L0X_DEFAULT_MAX_LOOP) |
johnAlexander | 14:8320b5ff96fa | 105 | status = VL53L0X_ERROR_TIME_OUT; |
johnAlexander | 14:8320b5ff96fa | 106 | |
johnAlexander | 14:8320b5ff96fa | 107 | } |
johnAlexander | 14:8320b5ff96fa | 108 | |
johnAlexander | 14:8320b5ff96fa | 109 | status |= vl53l0x_write_byte(dev, 0x83, 0x01); |
johnAlexander | 14:8320b5ff96fa | 110 | |
johnAlexander | 14:8320b5ff96fa | 111 | LOG_FUNCTION_END(status); |
johnAlexander | 14:8320b5ff96fa | 112 | return status; |
johnAlexander | 0:c523920bcc09 | 113 | |
johnAlexander | 0:c523920bcc09 | 114 | } |
johnAlexander | 0:c523920bcc09 | 115 | |
johnAlexander | 14:8320b5ff96fa | 116 | VL53L0X_Error VL53L0X::vl53l0x_get_info_from_device(VL53L0X_DEV dev, uint8_t option) |
johnAlexander | 0:c523920bcc09 | 117 | { |
johnAlexander | 0:c523920bcc09 | 118 | |
johnAlexander | 14:8320b5ff96fa | 119 | VL53L0X_Error status = VL53L0X_ERROR_NONE; |
johnAlexander | 14:8320b5ff96fa | 120 | uint8_t byte; |
johnAlexander | 14:8320b5ff96fa | 121 | uint32_t tmp_dword; |
johnAlexander | 14:8320b5ff96fa | 122 | uint8_t module_id; |
johnAlexander | 14:8320b5ff96fa | 123 | uint8_t revision; |
johnAlexander | 14:8320b5ff96fa | 124 | uint8_t reference_spad_count = 0; |
johnAlexander | 14:8320b5ff96fa | 125 | uint8_t reference_spad_type = 0; |
johnAlexander | 14:8320b5ff96fa | 126 | uint32_t part_uid_upper = 0; |
johnAlexander | 14:8320b5ff96fa | 127 | uint32_t part_uid_lower = 0; |
johnAlexander | 14:8320b5ff96fa | 128 | uint32_t offset_fixed1104_mm = 0; |
johnAlexander | 14:8320b5ff96fa | 129 | int16_t offset_micro_meters = 0; |
johnAlexander | 14:8320b5ff96fa | 130 | uint32_t dist_meas_tgt_fixed1104_mm = 400 << 4; |
johnAlexander | 14:8320b5ff96fa | 131 | uint32_t dist_meas_fixed1104_400_mm = 0; |
johnAlexander | 14:8320b5ff96fa | 132 | uint32_t signal_rate_meas_fixed1104_400_mm = 0; |
johnAlexander | 14:8320b5ff96fa | 133 | char product_id[19]; |
johnAlexander | 14:8320b5ff96fa | 134 | char *product_id_tmp; |
johnAlexander | 14:8320b5ff96fa | 135 | uint8_t read_data_from_device_done; |
johnAlexander | 14:8320b5ff96fa | 136 | FixPoint1616_t signal_rate_meas_fixed400_mm_fix = 0; |
johnAlexander | 14:8320b5ff96fa | 137 | uint8_t nvm_ref_good_spad_map[VL53L0X_REF_SPAD_BUFFER_SIZE]; |
johnAlexander | 14:8320b5ff96fa | 138 | int i; |
johnAlexander | 14:8320b5ff96fa | 139 | |
johnAlexander | 14:8320b5ff96fa | 140 | |
johnAlexander | 14:8320b5ff96fa | 141 | LOG_FUNCTION_START(""); |
johnAlexander | 14:8320b5ff96fa | 142 | |
johnAlexander | 14:8320b5ff96fa | 143 | read_data_from_device_done = VL53L0X_GETDEVICESPECIFICPARAMETER(dev, |
johnAlexander | 14:8320b5ff96fa | 144 | ReadDataFromDeviceDone); |
johnAlexander | 14:8320b5ff96fa | 145 | |
johnAlexander | 14:8320b5ff96fa | 146 | /* This access is done only once after that a GetDeviceInfo or |
johnAlexander | 14:8320b5ff96fa | 147 | * datainit is done*/ |
johnAlexander | 14:8320b5ff96fa | 148 | if (read_data_from_device_done != 7) { |
johnAlexander | 14:8320b5ff96fa | 149 | |
johnAlexander | 14:8320b5ff96fa | 150 | status |= vl53l0x_write_byte(dev, 0x80, 0x01); |
johnAlexander | 14:8320b5ff96fa | 151 | status |= vl53l0x_write_byte(dev, 0xFF, 0x01); |
johnAlexander | 14:8320b5ff96fa | 152 | status |= vl53l0x_write_byte(dev, 0x00, 0x00); |
johnAlexander | 14:8320b5ff96fa | 153 | |
johnAlexander | 14:8320b5ff96fa | 154 | status |= vl53l0x_write_byte(dev, 0xFF, 0x06); |
johnAlexander | 14:8320b5ff96fa | 155 | status |= vl53l0x_read_byte(dev, 0x83, &byte); |
johnAlexander | 14:8320b5ff96fa | 156 | status |= vl53l0x_write_byte(dev, 0x83, byte | 4); |
johnAlexander | 14:8320b5ff96fa | 157 | status |= vl53l0x_write_byte(dev, 0xFF, 0x07); |
johnAlexander | 14:8320b5ff96fa | 158 | status |= vl53l0x_write_byte(dev, 0x81, 0x01); |
johnAlexander | 14:8320b5ff96fa | 159 | |
johnAlexander | 14:8320b5ff96fa | 160 | status |= vl53l0x_polling_delay(dev); |
johnAlexander | 14:8320b5ff96fa | 161 | |
johnAlexander | 14:8320b5ff96fa | 162 | status |= vl53l0x_write_byte(dev, 0x80, 0x01); |
johnAlexander | 14:8320b5ff96fa | 163 | |
johnAlexander | 14:8320b5ff96fa | 164 | if (((option & 1) == 1) && |
johnAlexander | 14:8320b5ff96fa | 165 | ((read_data_from_device_done & 1) == 0)) { |
johnAlexander | 14:8320b5ff96fa | 166 | status |= vl53l0x_write_byte(dev, 0x94, 0x6b); |
johnAlexander | 14:8320b5ff96fa | 167 | status |= vl53l0x_device_read_strobe(dev); |
johnAlexander | 14:8320b5ff96fa | 168 | status |= vl53l0x_read_dword(dev, 0x90, &tmp_dword); |
johnAlexander | 14:8320b5ff96fa | 169 | |
johnAlexander | 14:8320b5ff96fa | 170 | reference_spad_count = (uint8_t)((tmp_dword >> 8) & 0x07f); |
johnAlexander | 14:8320b5ff96fa | 171 | reference_spad_type = (uint8_t)((tmp_dword >> 15) & 0x01); |
johnAlexander | 14:8320b5ff96fa | 172 | |
johnAlexander | 14:8320b5ff96fa | 173 | status |= vl53l0x_write_byte(dev, 0x94, 0x24); |
johnAlexander | 14:8320b5ff96fa | 174 | status |= vl53l0x_device_read_strobe(dev); |
johnAlexander | 14:8320b5ff96fa | 175 | status |= vl53l0x_read_dword(dev, 0x90, &tmp_dword); |
johnAlexander | 14:8320b5ff96fa | 176 | |
johnAlexander | 14:8320b5ff96fa | 177 | |
johnAlexander | 14:8320b5ff96fa | 178 | nvm_ref_good_spad_map[0] = (uint8_t)((tmp_dword >> 24) |
johnAlexander | 14:8320b5ff96fa | 179 | & 0xff); |
johnAlexander | 14:8320b5ff96fa | 180 | nvm_ref_good_spad_map[1] = (uint8_t)((tmp_dword >> 16) |
johnAlexander | 14:8320b5ff96fa | 181 | & 0xff); |
johnAlexander | 14:8320b5ff96fa | 182 | nvm_ref_good_spad_map[2] = (uint8_t)((tmp_dword >> 8) |
johnAlexander | 14:8320b5ff96fa | 183 | & 0xff); |
johnAlexander | 14:8320b5ff96fa | 184 | nvm_ref_good_spad_map[3] = (uint8_t)(tmp_dword & 0xff); |
johnAlexander | 14:8320b5ff96fa | 185 | |
johnAlexander | 14:8320b5ff96fa | 186 | status |= vl53l0x_write_byte(dev, 0x94, 0x25); |
johnAlexander | 14:8320b5ff96fa | 187 | status |= vl53l0x_device_read_strobe(dev); |
johnAlexander | 14:8320b5ff96fa | 188 | status |= vl53l0x_read_dword(dev, 0x90, &tmp_dword); |
johnAlexander | 14:8320b5ff96fa | 189 | |
johnAlexander | 14:8320b5ff96fa | 190 | nvm_ref_good_spad_map[4] = (uint8_t)((tmp_dword >> 24) |
johnAlexander | 14:8320b5ff96fa | 191 | & 0xff); |
johnAlexander | 14:8320b5ff96fa | 192 | nvm_ref_good_spad_map[5] = (uint8_t)((tmp_dword >> 16) |
johnAlexander | 14:8320b5ff96fa | 193 | & 0xff); |
johnAlexander | 14:8320b5ff96fa | 194 | } |
johnAlexander | 14:8320b5ff96fa | 195 | |
johnAlexander | 14:8320b5ff96fa | 196 | if (((option & 2) == 2) && |
johnAlexander | 14:8320b5ff96fa | 197 | ((read_data_from_device_done & 2) == 0)) { |
johnAlexander | 14:8320b5ff96fa | 198 | |
johnAlexander | 14:8320b5ff96fa | 199 | status |= vl53l0x_write_byte(dev, 0x94, 0x02); |
johnAlexander | 14:8320b5ff96fa | 200 | status |= vl53l0x_device_read_strobe(dev); |
johnAlexander | 14:8320b5ff96fa | 201 | status |= vl53l0x_read_byte(dev, 0x90, &module_id); |
johnAlexander | 14:8320b5ff96fa | 202 | |
johnAlexander | 14:8320b5ff96fa | 203 | status |= vl53l0x_write_byte(dev, 0x94, 0x7B); |
johnAlexander | 14:8320b5ff96fa | 204 | status |= vl53l0x_device_read_strobe(dev); |
johnAlexander | 14:8320b5ff96fa | 205 | status |= vl53l0x_read_byte(dev, 0x90, &revision); |
johnAlexander | 14:8320b5ff96fa | 206 | |
johnAlexander | 14:8320b5ff96fa | 207 | status |= vl53l0x_write_byte(dev, 0x94, 0x77); |
johnAlexander | 14:8320b5ff96fa | 208 | status |= vl53l0x_device_read_strobe(dev); |
johnAlexander | 14:8320b5ff96fa | 209 | status |= vl53l0x_read_dword(dev, 0x90, &tmp_dword); |
johnAlexander | 14:8320b5ff96fa | 210 | |
johnAlexander | 14:8320b5ff96fa | 211 | product_id[0] = (char)((tmp_dword >> 25) & 0x07f); |
johnAlexander | 14:8320b5ff96fa | 212 | product_id[1] = (char)((tmp_dword >> 18) & 0x07f); |
johnAlexander | 14:8320b5ff96fa | 213 | product_id[2] = (char)((tmp_dword >> 11) & 0x07f); |
johnAlexander | 14:8320b5ff96fa | 214 | product_id[3] = (char)((tmp_dword >> 4) & 0x07f); |
johnAlexander | 14:8320b5ff96fa | 215 | |
johnAlexander | 14:8320b5ff96fa | 216 | byte = (uint8_t)((tmp_dword & 0x00f) << 3); |
johnAlexander | 14:8320b5ff96fa | 217 | |
johnAlexander | 14:8320b5ff96fa | 218 | status |= vl53l0x_write_byte(dev, 0x94, 0x78); |
johnAlexander | 14:8320b5ff96fa | 219 | status |= vl53l0x_device_read_strobe(dev); |
johnAlexander | 14:8320b5ff96fa | 220 | status |= vl53l0x_read_dword(dev, 0x90, &tmp_dword); |
johnAlexander | 14:8320b5ff96fa | 221 | |
johnAlexander | 14:8320b5ff96fa | 222 | product_id[4] = (char)(byte + |
johnAlexander | 14:8320b5ff96fa | 223 | ((tmp_dword >> 29) & 0x07f)); |
johnAlexander | 14:8320b5ff96fa | 224 | product_id[5] = (char)((tmp_dword >> 22) & 0x07f); |
johnAlexander | 14:8320b5ff96fa | 225 | product_id[6] = (char)((tmp_dword >> 15) & 0x07f); |
johnAlexander | 14:8320b5ff96fa | 226 | product_id[7] = (char)((tmp_dword >> 8) & 0x07f); |
johnAlexander | 14:8320b5ff96fa | 227 | product_id[8] = (char)((tmp_dword >> 1) & 0x07f); |
johnAlexander | 14:8320b5ff96fa | 228 | |
johnAlexander | 14:8320b5ff96fa | 229 | byte = (uint8_t)((tmp_dword & 0x001) << 6); |
johnAlexander | 14:8320b5ff96fa | 230 | |
johnAlexander | 14:8320b5ff96fa | 231 | status |= vl53l0x_write_byte(dev, 0x94, 0x79); |
johnAlexander | 14:8320b5ff96fa | 232 | |
johnAlexander | 14:8320b5ff96fa | 233 | status |= vl53l0x_device_read_strobe(dev); |
johnAlexander | 14:8320b5ff96fa | 234 | |
johnAlexander | 14:8320b5ff96fa | 235 | status |= vl53l0x_read_dword(dev, 0x90, &tmp_dword); |
johnAlexander | 14:8320b5ff96fa | 236 | |
johnAlexander | 14:8320b5ff96fa | 237 | product_id[9] = (char)(byte + |
johnAlexander | 14:8320b5ff96fa | 238 | ((tmp_dword >> 26) & 0x07f)); |
johnAlexander | 14:8320b5ff96fa | 239 | product_id[10] = (char)((tmp_dword >> 19) & 0x07f); |
johnAlexander | 14:8320b5ff96fa | 240 | product_id[11] = (char)((tmp_dword >> 12) & 0x07f); |
johnAlexander | 14:8320b5ff96fa | 241 | product_id[12] = (char)((tmp_dword >> 5) & 0x07f); |
johnAlexander | 14:8320b5ff96fa | 242 | |
johnAlexander | 14:8320b5ff96fa | 243 | byte = (uint8_t)((tmp_dword & 0x01f) << 2); |
johnAlexander | 14:8320b5ff96fa | 244 | |
johnAlexander | 14:8320b5ff96fa | 245 | status |= vl53l0x_write_byte(dev, 0x94, 0x7A); |
johnAlexander | 14:8320b5ff96fa | 246 | |
johnAlexander | 14:8320b5ff96fa | 247 | status |= vl53l0x_device_read_strobe(dev); |
johnAlexander | 14:8320b5ff96fa | 248 | |
johnAlexander | 14:8320b5ff96fa | 249 | status |= vl53l0x_read_dword(dev, 0x90, &tmp_dword); |
johnAlexander | 14:8320b5ff96fa | 250 | |
johnAlexander | 14:8320b5ff96fa | 251 | product_id[13] = (char)(byte + |
johnAlexander | 14:8320b5ff96fa | 252 | ((tmp_dword >> 30) & 0x07f)); |
johnAlexander | 14:8320b5ff96fa | 253 | product_id[14] = (char)((tmp_dword >> 23) & 0x07f); |
johnAlexander | 14:8320b5ff96fa | 254 | product_id[15] = (char)((tmp_dword >> 16) & 0x07f); |
johnAlexander | 14:8320b5ff96fa | 255 | product_id[16] = (char)((tmp_dword >> 9) & 0x07f); |
johnAlexander | 14:8320b5ff96fa | 256 | product_id[17] = (char)((tmp_dword >> 2) & 0x07f); |
johnAlexander | 14:8320b5ff96fa | 257 | product_id[18] = '\0'; |
johnAlexander | 14:8320b5ff96fa | 258 | |
johnAlexander | 14:8320b5ff96fa | 259 | } |
johnAlexander | 14:8320b5ff96fa | 260 | |
johnAlexander | 14:8320b5ff96fa | 261 | if (((option & 4) == 4) && |
johnAlexander | 14:8320b5ff96fa | 262 | ((read_data_from_device_done & 4) == 0)) { |
johnAlexander | 14:8320b5ff96fa | 263 | |
johnAlexander | 14:8320b5ff96fa | 264 | status |= vl53l0x_write_byte(dev, 0x94, 0x7B); |
johnAlexander | 14:8320b5ff96fa | 265 | status |= vl53l0x_device_read_strobe(dev); |
johnAlexander | 14:8320b5ff96fa | 266 | status |= vl53l0x_read_dword(dev, 0x90, &part_uid_upper); |
johnAlexander | 14:8320b5ff96fa | 267 | |
johnAlexander | 14:8320b5ff96fa | 268 | status |= vl53l0x_write_byte(dev, 0x94, 0x7C); |
johnAlexander | 14:8320b5ff96fa | 269 | status |= vl53l0x_device_read_strobe(dev); |
johnAlexander | 14:8320b5ff96fa | 270 | status |= vl53l0x_read_dword(dev, 0x90, &part_uid_lower); |
johnAlexander | 14:8320b5ff96fa | 271 | |
johnAlexander | 14:8320b5ff96fa | 272 | status |= vl53l0x_write_byte(dev, 0x94, 0x73); |
johnAlexander | 14:8320b5ff96fa | 273 | status |= vl53l0x_device_read_strobe(dev); |
johnAlexander | 14:8320b5ff96fa | 274 | status |= vl53l0x_read_dword(dev, 0x90, &tmp_dword); |
johnAlexander | 14:8320b5ff96fa | 275 | |
johnAlexander | 14:8320b5ff96fa | 276 | signal_rate_meas_fixed1104_400_mm = (tmp_dword & |
johnAlexander | 14:8320b5ff96fa | 277 | 0x0000000ff) << 8; |
johnAlexander | 14:8320b5ff96fa | 278 | |
johnAlexander | 14:8320b5ff96fa | 279 | status |= vl53l0x_write_byte(dev, 0x94, 0x74); |
johnAlexander | 14:8320b5ff96fa | 280 | status |= vl53l0x_device_read_strobe(dev); |
johnAlexander | 14:8320b5ff96fa | 281 | status |= vl53l0x_read_dword(dev, 0x90, &tmp_dword); |
johnAlexander | 14:8320b5ff96fa | 282 | |
johnAlexander | 14:8320b5ff96fa | 283 | signal_rate_meas_fixed1104_400_mm |= ((tmp_dword & |
johnAlexander | 14:8320b5ff96fa | 284 | 0xff000000) >> 24); |
johnAlexander | 14:8320b5ff96fa | 285 | |
johnAlexander | 14:8320b5ff96fa | 286 | status |= vl53l0x_write_byte(dev, 0x94, 0x75); |
johnAlexander | 14:8320b5ff96fa | 287 | status |= vl53l0x_device_read_strobe(dev); |
johnAlexander | 14:8320b5ff96fa | 288 | status |= vl53l0x_read_dword(dev, 0x90, &tmp_dword); |
johnAlexander | 14:8320b5ff96fa | 289 | |
johnAlexander | 14:8320b5ff96fa | 290 | dist_meas_fixed1104_400_mm = (tmp_dword & 0x0000000ff) |
johnAlexander | 14:8320b5ff96fa | 291 | << 8; |
johnAlexander | 14:8320b5ff96fa | 292 | |
johnAlexander | 14:8320b5ff96fa | 293 | status |= vl53l0x_write_byte(dev, 0x94, 0x76); |
johnAlexander | 14:8320b5ff96fa | 294 | status |= vl53l0x_device_read_strobe(dev); |
johnAlexander | 14:8320b5ff96fa | 295 | status |= vl53l0x_read_dword(dev, 0x90, &tmp_dword); |
johnAlexander | 14:8320b5ff96fa | 296 | |
johnAlexander | 14:8320b5ff96fa | 297 | dist_meas_fixed1104_400_mm |= ((tmp_dword & 0xff000000) |
johnAlexander | 14:8320b5ff96fa | 298 | >> 24); |
johnAlexander | 14:8320b5ff96fa | 299 | } |
johnAlexander | 14:8320b5ff96fa | 300 | |
johnAlexander | 14:8320b5ff96fa | 301 | status |= vl53l0x_write_byte(dev, 0x81, 0x00); |
johnAlexander | 14:8320b5ff96fa | 302 | status |= vl53l0x_write_byte(dev, 0xFF, 0x06); |
johnAlexander | 14:8320b5ff96fa | 303 | status |= vl53l0x_read_byte(dev, 0x83, &byte); |
johnAlexander | 14:8320b5ff96fa | 304 | status |= vl53l0x_write_byte(dev, 0x83, byte & 0xfb); |
johnAlexander | 14:8320b5ff96fa | 305 | status |= vl53l0x_write_byte(dev, 0xFF, 0x01); |
johnAlexander | 14:8320b5ff96fa | 306 | status |= vl53l0x_write_byte(dev, 0x00, 0x01); |
johnAlexander | 14:8320b5ff96fa | 307 | |
johnAlexander | 14:8320b5ff96fa | 308 | status |= vl53l0x_write_byte(dev, 0xFF, 0x00); |
johnAlexander | 14:8320b5ff96fa | 309 | status |= vl53l0x_write_byte(dev, 0x80, 0x00); |
johnAlexander | 14:8320b5ff96fa | 310 | } |
johnAlexander | 14:8320b5ff96fa | 311 | |
johnAlexander | 14:8320b5ff96fa | 312 | if ((status == VL53L0X_ERROR_NONE) && |
johnAlexander | 14:8320b5ff96fa | 313 | (read_data_from_device_done != 7)) { |
johnAlexander | 14:8320b5ff96fa | 314 | /* Assign to variable if status is ok */ |
johnAlexander | 14:8320b5ff96fa | 315 | if (((option & 1) == 1) && |
johnAlexander | 14:8320b5ff96fa | 316 | ((read_data_from_device_done & 1) == 0)) { |
johnAlexander | 14:8320b5ff96fa | 317 | VL53L0X_SETDEVICESPECIFICPARAMETER(dev, |
johnAlexander | 14:8320b5ff96fa | 318 | ReferenceSpadCount, reference_spad_count); |
johnAlexander | 14:8320b5ff96fa | 319 | |
johnAlexander | 14:8320b5ff96fa | 320 | VL53L0X_SETDEVICESPECIFICPARAMETER(dev, |
johnAlexander | 14:8320b5ff96fa | 321 | ReferenceSpadType, reference_spad_type); |
johnAlexander | 14:8320b5ff96fa | 322 | |
johnAlexander | 14:8320b5ff96fa | 323 | for (i = 0; i < VL53L0X_REF_SPAD_BUFFER_SIZE; i++) { |
johnAlexander | 14:8320b5ff96fa | 324 | dev->Data.SpadData.RefGoodSpadMap[i] = |
johnAlexander | 14:8320b5ff96fa | 325 | nvm_ref_good_spad_map[i]; |
johnAlexander | 14:8320b5ff96fa | 326 | } |
johnAlexander | 14:8320b5ff96fa | 327 | } |
johnAlexander | 14:8320b5ff96fa | 328 | |
johnAlexander | 14:8320b5ff96fa | 329 | if (((option & 2) == 2) && |
johnAlexander | 14:8320b5ff96fa | 330 | ((read_data_from_device_done & 2) == 0)) { |
johnAlexander | 14:8320b5ff96fa | 331 | VL53L0X_SETDEVICESPECIFICPARAMETER(dev, |
johnAlexander | 14:8320b5ff96fa | 332 | ModuleId, module_id); |
johnAlexander | 14:8320b5ff96fa | 333 | |
johnAlexander | 14:8320b5ff96fa | 334 | VL53L0X_SETDEVICESPECIFICPARAMETER(dev, |
johnAlexander | 14:8320b5ff96fa | 335 | Revision, revision); |
johnAlexander | 14:8320b5ff96fa | 336 | |
johnAlexander | 14:8320b5ff96fa | 337 | product_id_tmp = VL53L0X_GETDEVICESPECIFICPARAMETER(dev, |
johnAlexander | 14:8320b5ff96fa | 338 | ProductId); |
johnAlexander | 14:8320b5ff96fa | 339 | VL53L0X_COPYSTRING(product_id_tmp, product_id); |
johnAlexander | 14:8320b5ff96fa | 340 | |
johnAlexander | 14:8320b5ff96fa | 341 | } |
johnAlexander | 14:8320b5ff96fa | 342 | |
johnAlexander | 14:8320b5ff96fa | 343 | if (((option & 4) == 4) && |
johnAlexander | 14:8320b5ff96fa | 344 | ((read_data_from_device_done & 4) == 0)) { |
johnAlexander | 14:8320b5ff96fa | 345 | VL53L0X_SETDEVICESPECIFICPARAMETER(dev, |
johnAlexander | 14:8320b5ff96fa | 346 | PartUIDUpper, part_uid_upper); |
johnAlexander | 14:8320b5ff96fa | 347 | |
johnAlexander | 14:8320b5ff96fa | 348 | VL53L0X_SETDEVICESPECIFICPARAMETER(dev, |
johnAlexander | 14:8320b5ff96fa | 349 | PartUIDLower, part_uid_lower); |
johnAlexander | 14:8320b5ff96fa | 350 | |
johnAlexander | 14:8320b5ff96fa | 351 | signal_rate_meas_fixed400_mm_fix = |
johnAlexander | 14:8320b5ff96fa | 352 | VL53L0X_FIXPOINT97TOFIXPOINT1616( |
johnAlexander | 14:8320b5ff96fa | 353 | signal_rate_meas_fixed1104_400_mm); |
johnAlexander | 14:8320b5ff96fa | 354 | |
johnAlexander | 14:8320b5ff96fa | 355 | VL53L0X_SETDEVICESPECIFICPARAMETER(dev, |
johnAlexander | 14:8320b5ff96fa | 356 | SignalRateMeasFixed400mm, |
johnAlexander | 14:8320b5ff96fa | 357 | signal_rate_meas_fixed400_mm_fix); |
johnAlexander | 14:8320b5ff96fa | 358 | |
johnAlexander | 14:8320b5ff96fa | 359 | offset_micro_meters = 0; |
johnAlexander | 14:8320b5ff96fa | 360 | if (dist_meas_fixed1104_400_mm != 0) { |
johnAlexander | 14:8320b5ff96fa | 361 | offset_fixed1104_mm = |
johnAlexander | 14:8320b5ff96fa | 362 | dist_meas_fixed1104_400_mm - |
johnAlexander | 14:8320b5ff96fa | 363 | dist_meas_tgt_fixed1104_mm; |
johnAlexander | 14:8320b5ff96fa | 364 | offset_micro_meters = (offset_fixed1104_mm |
johnAlexander | 14:8320b5ff96fa | 365 | * 1000) >> 4; |
johnAlexander | 14:8320b5ff96fa | 366 | offset_micro_meters *= -1; |
johnAlexander | 14:8320b5ff96fa | 367 | } |
johnAlexander | 14:8320b5ff96fa | 368 | |
johnAlexander | 14:8320b5ff96fa | 369 | PALDevDataSet(dev, |
johnAlexander | 14:8320b5ff96fa | 370 | Part2PartOffsetAdjustmentNVMMicroMeter, |
johnAlexander | 14:8320b5ff96fa | 371 | offset_micro_meters); |
johnAlexander | 14:8320b5ff96fa | 372 | } |
johnAlexander | 14:8320b5ff96fa | 373 | byte = (uint8_t)(read_data_from_device_done | option); |
johnAlexander | 14:8320b5ff96fa | 374 | VL53L0X_SETDEVICESPECIFICPARAMETER(dev, ReadDataFromDeviceDone, |
johnAlexander | 14:8320b5ff96fa | 375 | byte); |
johnAlexander | 14:8320b5ff96fa | 376 | } |
johnAlexander | 14:8320b5ff96fa | 377 | |
johnAlexander | 14:8320b5ff96fa | 378 | LOG_FUNCTION_END(status); |
johnAlexander | 14:8320b5ff96fa | 379 | return status; |
johnAlexander | 0:c523920bcc09 | 380 | } |
johnAlexander | 0:c523920bcc09 | 381 | |
johnAlexander | 14:8320b5ff96fa | 382 | VL53L0X_Error VL53L0X::wrapped_vl53l0x_get_offset_calibration_data_micro_meter(VL53L0X_DEV dev, |
johnAlexander | 14:8320b5ff96fa | 383 | int32_t *p_offset_calibration_data_micro_meter) |
johnAlexander | 0:c523920bcc09 | 384 | { |
johnAlexander | 14:8320b5ff96fa | 385 | VL53L0X_Error status = VL53L0X_ERROR_NONE; |
johnAlexander | 14:8320b5ff96fa | 386 | uint16_t range_offset_register; |
johnAlexander | 14:8320b5ff96fa | 387 | int16_t c_max_offset = 2047; |
johnAlexander | 14:8320b5ff96fa | 388 | int16_t c_offset_range = 4096; |
johnAlexander | 14:8320b5ff96fa | 389 | |
johnAlexander | 14:8320b5ff96fa | 390 | /* Note that offset has 10.2 format */ |
johnAlexander | 14:8320b5ff96fa | 391 | |
johnAlexander | 14:8320b5ff96fa | 392 | status = vl53l0x_read_word(dev, |
johnAlexander | 14:8320b5ff96fa | 393 | VL53L0X_REG_ALGO_PART_TO_PART_RANGE_OFFSET_MM, |
johnAlexander | 14:8320b5ff96fa | 394 | &range_offset_register); |
johnAlexander | 14:8320b5ff96fa | 395 | |
johnAlexander | 14:8320b5ff96fa | 396 | if (status == VL53L0X_ERROR_NONE) { |
johnAlexander | 14:8320b5ff96fa | 397 | range_offset_register = (range_offset_register & 0x0fff); |
johnAlexander | 14:8320b5ff96fa | 398 | |
johnAlexander | 14:8320b5ff96fa | 399 | /* Apply 12 bit 2's compliment conversion */ |
johnAlexander | 14:8320b5ff96fa | 400 | if (range_offset_register > c_max_offset) |
johnAlexander | 14:8320b5ff96fa | 401 | *p_offset_calibration_data_micro_meter = |
johnAlexander | 14:8320b5ff96fa | 402 | (int16_t)(range_offset_register - c_offset_range) |
johnAlexander | 14:8320b5ff96fa | 403 | * 250; |
johnAlexander | 14:8320b5ff96fa | 404 | else |
johnAlexander | 14:8320b5ff96fa | 405 | *p_offset_calibration_data_micro_meter = |
johnAlexander | 14:8320b5ff96fa | 406 | (int16_t)range_offset_register * 250; |
johnAlexander | 14:8320b5ff96fa | 407 | |
johnAlexander | 14:8320b5ff96fa | 408 | } |
johnAlexander | 14:8320b5ff96fa | 409 | |
johnAlexander | 14:8320b5ff96fa | 410 | return status; |
johnAlexander | 0:c523920bcc09 | 411 | } |
johnAlexander | 0:c523920bcc09 | 412 | |
johnAlexander | 14:8320b5ff96fa | 413 | VL53L0X_Error VL53L0X::vl53l0x_get_offset_calibration_data_micro_meter(VL53L0X_DEV dev, |
johnAlexander | 14:8320b5ff96fa | 414 | int32_t *p_offset_calibration_data_micro_meter) |
johnAlexander | 0:c523920bcc09 | 415 | { |
johnAlexander | 14:8320b5ff96fa | 416 | VL53L0X_Error status = VL53L0X_ERROR_NONE; |
johnAlexander | 14:8320b5ff96fa | 417 | LOG_FUNCTION_START(""); |
johnAlexander | 14:8320b5ff96fa | 418 | |
johnAlexander | 14:8320b5ff96fa | 419 | status = wrapped_vl53l0x_get_offset_calibration_data_micro_meter(dev, |
johnAlexander | 14:8320b5ff96fa | 420 | p_offset_calibration_data_micro_meter); |
johnAlexander | 14:8320b5ff96fa | 421 | |
johnAlexander | 14:8320b5ff96fa | 422 | LOG_FUNCTION_END(status); |
johnAlexander | 14:8320b5ff96fa | 423 | return status; |
johnAlexander | 0:c523920bcc09 | 424 | } |
johnAlexander | 0:c523920bcc09 | 425 | |
johnAlexander | 14:8320b5ff96fa | 426 | VL53L0X_Error VL53L0X::wrapped_vl53l0x_set_offset_calibration_data_micro_meter(VL53L0X_DEV dev, |
johnAlexander | 14:8320b5ff96fa | 427 | int32_t offset_calibration_data_micro_meter) |
johnAlexander | 0:c523920bcc09 | 428 | { |
johnAlexander | 14:8320b5ff96fa | 429 | VL53L0X_Error status = VL53L0X_ERROR_NONE; |
johnAlexander | 14:8320b5ff96fa | 430 | int32_t c_max_offset_micro_meter = 511000; |
johnAlexander | 14:8320b5ff96fa | 431 | int32_t c_min_offset_micro_meter = -512000; |
johnAlexander | 14:8320b5ff96fa | 432 | int16_t c_offset_range = 4096; |
johnAlexander | 14:8320b5ff96fa | 433 | uint32_t encoded_offset_val; |
johnAlexander | 14:8320b5ff96fa | 434 | |
johnAlexander | 14:8320b5ff96fa | 435 | LOG_FUNCTION_START(""); |
johnAlexander | 14:8320b5ff96fa | 436 | |
johnAlexander | 14:8320b5ff96fa | 437 | if (offset_calibration_data_micro_meter > c_max_offset_micro_meter) |
johnAlexander | 14:8320b5ff96fa | 438 | offset_calibration_data_micro_meter = c_max_offset_micro_meter; |
johnAlexander | 14:8320b5ff96fa | 439 | else if (offset_calibration_data_micro_meter < c_min_offset_micro_meter) |
johnAlexander | 14:8320b5ff96fa | 440 | offset_calibration_data_micro_meter = c_min_offset_micro_meter; |
johnAlexander | 14:8320b5ff96fa | 441 | |
johnAlexander | 14:8320b5ff96fa | 442 | /* The offset register is 10.2 format and units are mm |
johnAlexander | 14:8320b5ff96fa | 443 | * therefore conversion is applied by a division of |
johnAlexander | 14:8320b5ff96fa | 444 | * 250. |
johnAlexander | 14:8320b5ff96fa | 445 | */ |
johnAlexander | 14:8320b5ff96fa | 446 | if (offset_calibration_data_micro_meter >= 0) { |
johnAlexander | 14:8320b5ff96fa | 447 | encoded_offset_val = |
johnAlexander | 14:8320b5ff96fa | 448 | offset_calibration_data_micro_meter / 250; |
johnAlexander | 14:8320b5ff96fa | 449 | } else { |
johnAlexander | 14:8320b5ff96fa | 450 | encoded_offset_val = |
johnAlexander | 14:8320b5ff96fa | 451 | c_offset_range + |
johnAlexander | 14:8320b5ff96fa | 452 | offset_calibration_data_micro_meter / 250; |
johnAlexander | 14:8320b5ff96fa | 453 | } |
johnAlexander | 14:8320b5ff96fa | 454 | |
johnAlexander | 14:8320b5ff96fa | 455 | status = vl53l0x_write_word(dev, |
johnAlexander | 14:8320b5ff96fa | 456 | VL53L0X_REG_ALGO_PART_TO_PART_RANGE_OFFSET_MM, |
johnAlexander | 14:8320b5ff96fa | 457 | encoded_offset_val); |
johnAlexander | 14:8320b5ff96fa | 458 | |
johnAlexander | 14:8320b5ff96fa | 459 | LOG_FUNCTION_END(status); |
johnAlexander | 14:8320b5ff96fa | 460 | return status; |
johnAlexander | 0:c523920bcc09 | 461 | } |
johnAlexander | 0:c523920bcc09 | 462 | |
johnAlexander | 14:8320b5ff96fa | 463 | VL53L0X_Error VL53L0X::vl53l0x_set_offset_calibration_data_micro_meter(VL53L0X_DEV dev, |
johnAlexander | 14:8320b5ff96fa | 464 | int32_t offset_calibration_data_micro_meter) |
johnAlexander | 0:c523920bcc09 | 465 | { |
johnAlexander | 14:8320b5ff96fa | 466 | VL53L0X_Error status = VL53L0X_ERROR_NONE; |
johnAlexander | 14:8320b5ff96fa | 467 | LOG_FUNCTION_START(""); |
johnAlexander | 14:8320b5ff96fa | 468 | |
johnAlexander | 14:8320b5ff96fa | 469 | status = wrapped_vl53l0x_set_offset_calibration_data_micro_meter(dev, |
johnAlexander | 14:8320b5ff96fa | 470 | offset_calibration_data_micro_meter); |
johnAlexander | 14:8320b5ff96fa | 471 | |
johnAlexander | 14:8320b5ff96fa | 472 | LOG_FUNCTION_END(status); |
johnAlexander | 14:8320b5ff96fa | 473 | return status; |
johnAlexander | 0:c523920bcc09 | 474 | } |
johnAlexander | 0:c523920bcc09 | 475 | |
johnAlexander | 14:8320b5ff96fa | 476 | VL53L0X_Error VL53L0X::vl53l0x_apply_offset_adjustment(VL53L0X_DEV dev) |
johnAlexander | 0:c523920bcc09 | 477 | { |
johnAlexander | 14:8320b5ff96fa | 478 | VL53L0X_Error status = VL53L0X_ERROR_NONE; |
johnAlexander | 14:8320b5ff96fa | 479 | int32_t corrected_offset_micro_meters; |
johnAlexander | 14:8320b5ff96fa | 480 | int32_t current_offset_micro_meters; |
johnAlexander | 14:8320b5ff96fa | 481 | |
johnAlexander | 14:8320b5ff96fa | 482 | /* if we run on this function we can read all the NVM info |
johnAlexander | 14:8320b5ff96fa | 483 | * used by the API */ |
johnAlexander | 14:8320b5ff96fa | 484 | status = vl53l0x_get_info_from_device(dev, 7); |
johnAlexander | 14:8320b5ff96fa | 485 | |
johnAlexander | 14:8320b5ff96fa | 486 | /* Read back current device offset */ |
johnAlexander | 14:8320b5ff96fa | 487 | if (status == VL53L0X_ERROR_NONE) { |
johnAlexander | 14:8320b5ff96fa | 488 | status = vl53l0x_get_offset_calibration_data_micro_meter(dev, |
johnAlexander | 14:8320b5ff96fa | 489 | ¤t_offset_micro_meters); |
johnAlexander | 14:8320b5ff96fa | 490 | } |
johnAlexander | 14:8320b5ff96fa | 491 | |
johnAlexander | 14:8320b5ff96fa | 492 | /* Apply Offset Adjustment derived from 400mm measurements */ |
johnAlexander | 14:8320b5ff96fa | 493 | if (status == VL53L0X_ERROR_NONE) { |
johnAlexander | 14:8320b5ff96fa | 494 | |
johnAlexander | 14:8320b5ff96fa | 495 | /* Store initial device offset */ |
johnAlexander | 14:8320b5ff96fa | 496 | PALDevDataSet(dev, Part2PartOffsetNVMMicroMeter, |
johnAlexander | 14:8320b5ff96fa | 497 | current_offset_micro_meters); |
johnAlexander | 14:8320b5ff96fa | 498 | |
johnAlexander | 14:8320b5ff96fa | 499 | corrected_offset_micro_meters = current_offset_micro_meters + |
johnAlexander | 14:8320b5ff96fa | 500 | (int32_t)PALDevDataGet(dev, |
johnAlexander | 14:8320b5ff96fa | 501 | Part2PartOffsetAdjustmentNVMMicroMeter); |
johnAlexander | 14:8320b5ff96fa | 502 | |
johnAlexander | 14:8320b5ff96fa | 503 | status = vl53l0x_set_offset_calibration_data_micro_meter(dev, |
johnAlexander | 14:8320b5ff96fa | 504 | corrected_offset_micro_meters); |
johnAlexander | 14:8320b5ff96fa | 505 | |
johnAlexander | 14:8320b5ff96fa | 506 | /* store current, adjusted offset */ |
johnAlexander | 14:8320b5ff96fa | 507 | if (status == VL53L0X_ERROR_NONE) { |
johnAlexander | 14:8320b5ff96fa | 508 | VL53L0X_SETPARAMETERFIELD(dev, RangeOffsetMicroMeters, |
johnAlexander | 14:8320b5ff96fa | 509 | corrected_offset_micro_meters); |
johnAlexander | 14:8320b5ff96fa | 510 | } |
johnAlexander | 14:8320b5ff96fa | 511 | } |
johnAlexander | 14:8320b5ff96fa | 512 | |
johnAlexander | 14:8320b5ff96fa | 513 | return status; |
johnAlexander | 0:c523920bcc09 | 514 | } |
johnAlexander | 0:c523920bcc09 | 515 | |
johnAlexander | 14:8320b5ff96fa | 516 | VL53L0X_Error VL53L0X::vl53l0x_get_device_mode(VL53L0X_DEV dev, |
johnAlexander | 14:8320b5ff96fa | 517 | VL53L0X_DeviceModes *p_device_mode) |
johnAlexander | 0:c523920bcc09 | 518 | { |
johnAlexander | 14:8320b5ff96fa | 519 | VL53L0X_Error status = VL53L0X_ERROR_NONE; |
johnAlexander | 14:8320b5ff96fa | 520 | LOG_FUNCTION_START(""); |
johnAlexander | 14:8320b5ff96fa | 521 | |
johnAlexander | 14:8320b5ff96fa | 522 | VL53L0X_GETPARAMETERFIELD(dev, DeviceMode, *p_device_mode); |
johnAlexander | 14:8320b5ff96fa | 523 | |
johnAlexander | 14:8320b5ff96fa | 524 | LOG_FUNCTION_END(status); |
johnAlexander | 14:8320b5ff96fa | 525 | return status; |
johnAlexander | 0:c523920bcc09 | 526 | } |
johnAlexander | 0:c523920bcc09 | 527 | |
johnAlexander | 14:8320b5ff96fa | 528 | VL53L0X_Error VL53L0X::vl53l0x_get_inter_measurement_period_milli_seconds(VL53L0X_DEV dev, |
johnAlexander | 14:8320b5ff96fa | 529 | uint32_t *p_inter_measurement_period_milli_seconds) |
johnAlexander | 0:c523920bcc09 | 530 | { |
johnAlexander | 14:8320b5ff96fa | 531 | VL53L0X_Error status = VL53L0X_ERROR_NONE; |
johnAlexander | 14:8320b5ff96fa | 532 | uint16_t osc_calibrate_val; |
johnAlexander | 14:8320b5ff96fa | 533 | uint32_t im_period_milli_seconds; |
johnAlexander | 14:8320b5ff96fa | 534 | |
johnAlexander | 14:8320b5ff96fa | 535 | LOG_FUNCTION_START(""); |
johnAlexander | 14:8320b5ff96fa | 536 | |
johnAlexander | 14:8320b5ff96fa | 537 | status = vl53l0x_read_word(dev, VL53L0X_REG_OSC_CALIBRATE_VAL, |
johnAlexander | 14:8320b5ff96fa | 538 | &osc_calibrate_val); |
johnAlexander | 14:8320b5ff96fa | 539 | |
johnAlexander | 14:8320b5ff96fa | 540 | if (status == VL53L0X_ERROR_NONE) { |
johnAlexander | 14:8320b5ff96fa | 541 | status = vl53l0x_read_dword(dev, |
johnAlexander | 14:8320b5ff96fa | 542 | VL53L0X_REG_SYSTEM_INTERMEASUREMENT_PERIOD, |
johnAlexander | 14:8320b5ff96fa | 543 | &im_period_milli_seconds); |
johnAlexander | 14:8320b5ff96fa | 544 | } |
johnAlexander | 14:8320b5ff96fa | 545 | |
johnAlexander | 14:8320b5ff96fa | 546 | if (status == VL53L0X_ERROR_NONE) { |
johnAlexander | 14:8320b5ff96fa | 547 | if (osc_calibrate_val != 0) { |
johnAlexander | 14:8320b5ff96fa | 548 | *p_inter_measurement_period_milli_seconds = |
johnAlexander | 14:8320b5ff96fa | 549 | im_period_milli_seconds / osc_calibrate_val; |
johnAlexander | 14:8320b5ff96fa | 550 | } |
johnAlexander | 14:8320b5ff96fa | 551 | VL53L0X_SETPARAMETERFIELD(dev, |
johnAlexander | 14:8320b5ff96fa | 552 | InterMeasurementPeriodMilliSeconds, |
johnAlexander | 14:8320b5ff96fa | 553 | *p_inter_measurement_period_milli_seconds); |
johnAlexander | 14:8320b5ff96fa | 554 | } |
johnAlexander | 14:8320b5ff96fa | 555 | |
johnAlexander | 14:8320b5ff96fa | 556 | LOG_FUNCTION_END(status); |
johnAlexander | 14:8320b5ff96fa | 557 | return status; |
johnAlexander | 0:c523920bcc09 | 558 | } |
johnAlexander | 0:c523920bcc09 | 559 | |
johnAlexander | 14:8320b5ff96fa | 560 | VL53L0X_Error VL53L0X::vl53l0x_get_x_talk_compensation_rate_mega_cps(VL53L0X_DEV dev, |
johnAlexander | 14:8320b5ff96fa | 561 | FixPoint1616_t *p_xtalk_compensation_rate_mega_cps) |
johnAlexander | 0:c523920bcc09 | 562 | { |
johnAlexander | 14:8320b5ff96fa | 563 | VL53L0X_Error status = VL53L0X_ERROR_NONE; |
johnAlexander | 14:8320b5ff96fa | 564 | uint16_t value; |
johnAlexander | 14:8320b5ff96fa | 565 | FixPoint1616_t temp_fix1616; |
johnAlexander | 14:8320b5ff96fa | 566 | |
johnAlexander | 14:8320b5ff96fa | 567 | LOG_FUNCTION_START(""); |
johnAlexander | 14:8320b5ff96fa | 568 | |
johnAlexander | 14:8320b5ff96fa | 569 | status = vl53l0x_read_word(dev, |
johnAlexander | 14:8320b5ff96fa | 570 | VL53L0X_REG_CROSSTALK_COMPENSATION_PEAK_RATE_MCPS, (uint16_t *)&value); |
johnAlexander | 14:8320b5ff96fa | 571 | if (status == VL53L0X_ERROR_NONE) { |
johnAlexander | 14:8320b5ff96fa | 572 | if (value == 0) { |
johnAlexander | 14:8320b5ff96fa | 573 | /* the Xtalk is disabled return value from memory */ |
johnAlexander | 14:8320b5ff96fa | 574 | VL53L0X_GETPARAMETERFIELD(dev, |
johnAlexander | 14:8320b5ff96fa | 575 | XTalkCompensationRateMegaCps, temp_fix1616); |
johnAlexander | 14:8320b5ff96fa | 576 | *p_xtalk_compensation_rate_mega_cps = temp_fix1616; |
johnAlexander | 14:8320b5ff96fa | 577 | VL53L0X_SETPARAMETERFIELD(dev, XTalkCompensationEnable, |
johnAlexander | 14:8320b5ff96fa | 578 | 0); |
johnAlexander | 14:8320b5ff96fa | 579 | } else { |
johnAlexander | 14:8320b5ff96fa | 580 | temp_fix1616 = VL53L0X_FIXPOINT313TOFIXPOINT1616(value); |
johnAlexander | 14:8320b5ff96fa | 581 | *p_xtalk_compensation_rate_mega_cps = temp_fix1616; |
johnAlexander | 14:8320b5ff96fa | 582 | VL53L0X_SETPARAMETERFIELD(dev, |
johnAlexander | 14:8320b5ff96fa | 583 | XTalkCompensationRateMegaCps, temp_fix1616); |
johnAlexander | 14:8320b5ff96fa | 584 | VL53L0X_SETPARAMETERFIELD(dev, XTalkCompensationEnable, |
johnAlexander | 14:8320b5ff96fa | 585 | 1); |
johnAlexander | 14:8320b5ff96fa | 586 | } |
johnAlexander | 14:8320b5ff96fa | 587 | } |
johnAlexander | 14:8320b5ff96fa | 588 | |
johnAlexander | 14:8320b5ff96fa | 589 | LOG_FUNCTION_END(status); |
johnAlexander | 14:8320b5ff96fa | 590 | return status; |
johnAlexander | 0:c523920bcc09 | 591 | } |
johnAlexander | 0:c523920bcc09 | 592 | |
johnAlexander | 14:8320b5ff96fa | 593 | VL53L0X_Error VL53L0X::vl53l0x_get_limit_check_value(VL53L0X_DEV dev, uint16_t limit_check_id, |
johnAlexander | 14:8320b5ff96fa | 594 | FixPoint1616_t *p_limit_check_value) |
johnAlexander | 0:c523920bcc09 | 595 | { |
johnAlexander | 14:8320b5ff96fa | 596 | VL53L0X_Error status = VL53L0X_ERROR_NONE; |
johnAlexander | 14:8320b5ff96fa | 597 | uint8_t enable_zero_value = 0; |
johnAlexander | 14:8320b5ff96fa | 598 | uint16_t temp16; |
johnAlexander | 14:8320b5ff96fa | 599 | FixPoint1616_t temp_fix1616; |
johnAlexander | 14:8320b5ff96fa | 600 | |
johnAlexander | 14:8320b5ff96fa | 601 | LOG_FUNCTION_START(""); |
johnAlexander | 14:8320b5ff96fa | 602 | |
johnAlexander | 14:8320b5ff96fa | 603 | switch (limit_check_id) { |
johnAlexander | 14:8320b5ff96fa | 604 | |
johnAlexander | 14:8320b5ff96fa | 605 | case VL53L0X_CHECKENABLE_SIGMA_FINAL_RANGE: |
johnAlexander | 14:8320b5ff96fa | 606 | /* internal computation: */ |
johnAlexander | 14:8320b5ff96fa | 607 | VL53L0X_GETARRAYPARAMETERFIELD(dev, LimitChecksValue, |
johnAlexander | 14:8320b5ff96fa | 608 | VL53L0X_CHECKENABLE_SIGMA_FINAL_RANGE, temp_fix1616); |
johnAlexander | 14:8320b5ff96fa | 609 | enable_zero_value = 0; |
johnAlexander | 14:8320b5ff96fa | 610 | break; |
johnAlexander | 14:8320b5ff96fa | 611 | |
johnAlexander | 14:8320b5ff96fa | 612 | case VL53L0X_CHECKENABLE_SIGNAL_RATE_FINAL_RANGE: |
johnAlexander | 14:8320b5ff96fa | 613 | status = vl53l0x_read_word(dev, |
johnAlexander | 14:8320b5ff96fa | 614 | VL53L0X_REG_FINAL_RANGE_CONFIG_MIN_COUNT_RATE_RTN_LIMIT, |
johnAlexander | 14:8320b5ff96fa | 615 | &temp16); |
johnAlexander | 14:8320b5ff96fa | 616 | if (status == VL53L0X_ERROR_NONE) |
johnAlexander | 14:8320b5ff96fa | 617 | temp_fix1616 = VL53L0X_FIXPOINT97TOFIXPOINT1616(temp16); |
johnAlexander | 14:8320b5ff96fa | 618 | |
johnAlexander | 14:8320b5ff96fa | 619 | |
johnAlexander | 14:8320b5ff96fa | 620 | enable_zero_value = 1; |
johnAlexander | 14:8320b5ff96fa | 621 | break; |
johnAlexander | 14:8320b5ff96fa | 622 | |
johnAlexander | 14:8320b5ff96fa | 623 | case VL53L0X_CHECKENABLE_SIGNAL_REF_CLIP: |
johnAlexander | 14:8320b5ff96fa | 624 | /* internal computation: */ |
johnAlexander | 14:8320b5ff96fa | 625 | VL53L0X_GETARRAYPARAMETERFIELD(dev, LimitChecksValue, |
johnAlexander | 14:8320b5ff96fa | 626 | VL53L0X_CHECKENABLE_SIGNAL_REF_CLIP, temp_fix1616); |
johnAlexander | 14:8320b5ff96fa | 627 | enable_zero_value = 0; |
johnAlexander | 14:8320b5ff96fa | 628 | break; |
johnAlexander | 14:8320b5ff96fa | 629 | |
johnAlexander | 14:8320b5ff96fa | 630 | case VL53L0X_CHECKENABLE_RANGE_IGNORE_THRESHOLD: |
johnAlexander | 14:8320b5ff96fa | 631 | /* internal computation: */ |
johnAlexander | 14:8320b5ff96fa | 632 | VL53L0X_GETARRAYPARAMETERFIELD(dev, LimitChecksValue, |
johnAlexander | 14:8320b5ff96fa | 633 | VL53L0X_CHECKENABLE_RANGE_IGNORE_THRESHOLD, temp_fix1616); |
johnAlexander | 14:8320b5ff96fa | 634 | enable_zero_value = 0; |
johnAlexander | 14:8320b5ff96fa | 635 | break; |
johnAlexander | 14:8320b5ff96fa | 636 | |
johnAlexander | 14:8320b5ff96fa | 637 | case VL53L0X_CHECKENABLE_SIGNAL_RATE_MSRC: |
johnAlexander | 14:8320b5ff96fa | 638 | case VL53L0X_CHECKENABLE_SIGNAL_RATE_PRE_RANGE: |
johnAlexander | 14:8320b5ff96fa | 639 | status = vl53l0x_read_word(dev, |
johnAlexander | 14:8320b5ff96fa | 640 | VL53L0X_REG_PRE_RANGE_MIN_COUNT_RATE_RTN_LIMIT, |
johnAlexander | 14:8320b5ff96fa | 641 | &temp16); |
johnAlexander | 14:8320b5ff96fa | 642 | if (status == VL53L0X_ERROR_NONE) |
johnAlexander | 14:8320b5ff96fa | 643 | temp_fix1616 = VL53L0X_FIXPOINT97TOFIXPOINT1616(temp16); |
johnAlexander | 14:8320b5ff96fa | 644 | |
johnAlexander | 14:8320b5ff96fa | 645 | |
johnAlexander | 14:8320b5ff96fa | 646 | enable_zero_value = 0; |
johnAlexander | 14:8320b5ff96fa | 647 | break; |
johnAlexander | 14:8320b5ff96fa | 648 | |
johnAlexander | 14:8320b5ff96fa | 649 | default: |
johnAlexander | 14:8320b5ff96fa | 650 | status = VL53L0X_ERROR_INVALID_PARAMS; |
johnAlexander | 14:8320b5ff96fa | 651 | |
johnAlexander | 14:8320b5ff96fa | 652 | } |
johnAlexander | 14:8320b5ff96fa | 653 | |
johnAlexander | 14:8320b5ff96fa | 654 | if (status == VL53L0X_ERROR_NONE) { |
johnAlexander | 14:8320b5ff96fa | 655 | |
johnAlexander | 14:8320b5ff96fa | 656 | if (enable_zero_value == 1) { |
johnAlexander | 14:8320b5ff96fa | 657 | |
johnAlexander | 14:8320b5ff96fa | 658 | if (temp_fix1616 == 0) { |
johnAlexander | 14:8320b5ff96fa | 659 | /* disabled: return value from memory */ |
johnAlexander | 14:8320b5ff96fa | 660 | VL53L0X_GETARRAYPARAMETERFIELD(dev, |
johnAlexander | 14:8320b5ff96fa | 661 | LimitChecksValue, limit_check_id, |
johnAlexander | 14:8320b5ff96fa | 662 | temp_fix1616); |
johnAlexander | 14:8320b5ff96fa | 663 | *p_limit_check_value = temp_fix1616; |
johnAlexander | 14:8320b5ff96fa | 664 | VL53L0X_SETARRAYPARAMETERFIELD(dev, |
johnAlexander | 14:8320b5ff96fa | 665 | LimitChecksEnable, limit_check_id, 0); |
johnAlexander | 14:8320b5ff96fa | 666 | } else { |
johnAlexander | 14:8320b5ff96fa | 667 | *p_limit_check_value = temp_fix1616; |
johnAlexander | 14:8320b5ff96fa | 668 | VL53L0X_SETARRAYPARAMETERFIELD(dev, |
johnAlexander | 14:8320b5ff96fa | 669 | LimitChecksValue, limit_check_id, |
johnAlexander | 14:8320b5ff96fa | 670 | temp_fix1616); |
johnAlexander | 14:8320b5ff96fa | 671 | VL53L0X_SETARRAYPARAMETERFIELD(dev, |
johnAlexander | 14:8320b5ff96fa | 672 | LimitChecksEnable, limit_check_id, 1); |
johnAlexander | 14:8320b5ff96fa | 673 | } |
johnAlexander | 14:8320b5ff96fa | 674 | } else { |
johnAlexander | 14:8320b5ff96fa | 675 | *p_limit_check_value = temp_fix1616; |
johnAlexander | 14:8320b5ff96fa | 676 | } |
johnAlexander | 14:8320b5ff96fa | 677 | } |
johnAlexander | 14:8320b5ff96fa | 678 | |
johnAlexander | 14:8320b5ff96fa | 679 | LOG_FUNCTION_END(status); |
johnAlexander | 14:8320b5ff96fa | 680 | return status; |
johnAlexander | 0:c523920bcc09 | 681 | |
johnAlexander | 0:c523920bcc09 | 682 | } |
johnAlexander | 0:c523920bcc09 | 683 | |
johnAlexander | 14:8320b5ff96fa | 684 | VL53L0X_Error VL53L0X::vl53l0x_get_limit_check_enable(VL53L0X_DEV dev, uint16_t limit_check_id, |
johnAlexander | 14:8320b5ff96fa | 685 | uint8_t *p_limit_check_enable) |
johnAlexander | 0:c523920bcc09 | 686 | { |
johnAlexander | 14:8320b5ff96fa | 687 | VL53L0X_Error status = VL53L0X_ERROR_NONE; |
johnAlexander | 14:8320b5ff96fa | 688 | uint8_t temp8; |
johnAlexander | 14:8320b5ff96fa | 689 | |
johnAlexander | 14:8320b5ff96fa | 690 | LOG_FUNCTION_START(""); |
johnAlexander | 14:8320b5ff96fa | 691 | |
johnAlexander | 14:8320b5ff96fa | 692 | if (limit_check_id >= VL53L0X_CHECKENABLE_NUMBER_OF_CHECKS) { |
johnAlexander | 14:8320b5ff96fa | 693 | status = VL53L0X_ERROR_INVALID_PARAMS; |
johnAlexander | 14:8320b5ff96fa | 694 | *p_limit_check_enable = 0; |
johnAlexander | 14:8320b5ff96fa | 695 | } else { |
johnAlexander | 14:8320b5ff96fa | 696 | VL53L0X_GETARRAYPARAMETERFIELD(dev, LimitChecksEnable, |
johnAlexander | 14:8320b5ff96fa | 697 | limit_check_id, temp8); |
johnAlexander | 14:8320b5ff96fa | 698 | *p_limit_check_enable = temp8; |
johnAlexander | 14:8320b5ff96fa | 699 | } |
johnAlexander | 14:8320b5ff96fa | 700 | |
johnAlexander | 14:8320b5ff96fa | 701 | LOG_FUNCTION_END(status); |
johnAlexander | 14:8320b5ff96fa | 702 | return status; |
johnAlexander | 0:c523920bcc09 | 703 | } |
johnAlexander | 0:c523920bcc09 | 704 | |
johnAlexander | 14:8320b5ff96fa | 705 | VL53L0X_Error VL53L0X::vl53l0x_get_wrap_around_check_enable(VL53L0X_DEV dev, |
johnAlexander | 14:8320b5ff96fa | 706 | uint8_t *p_wrap_around_check_enable) |
johnAlexander | 0:c523920bcc09 | 707 | { |
johnAlexander | 14:8320b5ff96fa | 708 | VL53L0X_Error status = VL53L0X_ERROR_NONE; |
johnAlexander | 14:8320b5ff96fa | 709 | uint8_t data; |
johnAlexander | 14:8320b5ff96fa | 710 | |
johnAlexander | 14:8320b5ff96fa | 711 | LOG_FUNCTION_START(""); |
johnAlexander | 14:8320b5ff96fa | 712 | |
johnAlexander | 14:8320b5ff96fa | 713 | status = vl53l0x_read_byte(dev, VL53L0X_REG_SYSTEM_SEQUENCE_CONFIG, &data); |
johnAlexander | 14:8320b5ff96fa | 714 | if (status == VL53L0X_ERROR_NONE) { |
johnAlexander | 14:8320b5ff96fa | 715 | PALDevDataSet(dev, SequenceConfig, data); |
johnAlexander | 14:8320b5ff96fa | 716 | if (data & (0x01 << 7)) |
johnAlexander | 14:8320b5ff96fa | 717 | *p_wrap_around_check_enable = 0x01; |
johnAlexander | 14:8320b5ff96fa | 718 | else |
johnAlexander | 14:8320b5ff96fa | 719 | *p_wrap_around_check_enable = 0x00; |
johnAlexander | 14:8320b5ff96fa | 720 | } |
johnAlexander | 14:8320b5ff96fa | 721 | if (status == VL53L0X_ERROR_NONE) { |
johnAlexander | 14:8320b5ff96fa | 722 | VL53L0X_SETPARAMETERFIELD(dev, WrapAroundCheckEnable, |
johnAlexander | 14:8320b5ff96fa | 723 | *p_wrap_around_check_enable); |
johnAlexander | 14:8320b5ff96fa | 724 | } |
johnAlexander | 14:8320b5ff96fa | 725 | |
johnAlexander | 14:8320b5ff96fa | 726 | LOG_FUNCTION_END(status); |
johnAlexander | 14:8320b5ff96fa | 727 | return status; |
johnAlexander | 0:c523920bcc09 | 728 | } |
johnAlexander | 0:c523920bcc09 | 729 | |
johnAlexander | 14:8320b5ff96fa | 730 | VL53L0X_Error VL53L0X::sequence_step_enabled(VL53L0X_DEV dev, |
johnAlexander | 14:8320b5ff96fa | 731 | VL53L0X_SequenceStepId sequence_step_id, uint8_t sequence_config, |
johnAlexander | 14:8320b5ff96fa | 732 | uint8_t *p_sequence_step_enabled) |
johnAlexander | 0:c523920bcc09 | 733 | { |
johnAlexander | 14:8320b5ff96fa | 734 | VL53L0X_Error Status = VL53L0X_ERROR_NONE; |
johnAlexander | 14:8320b5ff96fa | 735 | *p_sequence_step_enabled = 0; |
johnAlexander | 14:8320b5ff96fa | 736 | LOG_FUNCTION_START(""); |
johnAlexander | 14:8320b5ff96fa | 737 | |
johnAlexander | 14:8320b5ff96fa | 738 | switch (sequence_step_id) { |
johnAlexander | 14:8320b5ff96fa | 739 | case VL53L0X_SEQUENCESTEP_TCC: |
johnAlexander | 14:8320b5ff96fa | 740 | *p_sequence_step_enabled = (sequence_config & 0x10) >> 4; |
johnAlexander | 14:8320b5ff96fa | 741 | break; |
johnAlexander | 14:8320b5ff96fa | 742 | case VL53L0X_SEQUENCESTEP_DSS: |
johnAlexander | 14:8320b5ff96fa | 743 | *p_sequence_step_enabled = (sequence_config & 0x08) >> 3; |
johnAlexander | 14:8320b5ff96fa | 744 | break; |
johnAlexander | 14:8320b5ff96fa | 745 | case VL53L0X_SEQUENCESTEP_MSRC: |
johnAlexander | 14:8320b5ff96fa | 746 | *p_sequence_step_enabled = (sequence_config & 0x04) >> 2; |
johnAlexander | 14:8320b5ff96fa | 747 | break; |
johnAlexander | 14:8320b5ff96fa | 748 | case VL53L0X_SEQUENCESTEP_PRE_RANGE: |
johnAlexander | 14:8320b5ff96fa | 749 | *p_sequence_step_enabled = (sequence_config & 0x40) >> 6; |
johnAlexander | 14:8320b5ff96fa | 750 | break; |
johnAlexander | 14:8320b5ff96fa | 751 | case VL53L0X_SEQUENCESTEP_FINAL_RANGE: |
johnAlexander | 14:8320b5ff96fa | 752 | *p_sequence_step_enabled = (sequence_config & 0x80) >> 7; |
johnAlexander | 14:8320b5ff96fa | 753 | break; |
johnAlexander | 14:8320b5ff96fa | 754 | default: |
johnAlexander | 14:8320b5ff96fa | 755 | Status = VL53L0X_ERROR_INVALID_PARAMS; |
johnAlexander | 14:8320b5ff96fa | 756 | } |
johnAlexander | 14:8320b5ff96fa | 757 | |
johnAlexander | 14:8320b5ff96fa | 758 | LOG_FUNCTION_END(status); |
johnAlexander | 14:8320b5ff96fa | 759 | return Status; |
johnAlexander | 0:c523920bcc09 | 760 | } |
johnAlexander | 0:c523920bcc09 | 761 | |
johnAlexander | 14:8320b5ff96fa | 762 | VL53L0X_Error VL53L0X::vl53l0x_get_sequence_step_enables(VL53L0X_DEV dev, |
johnAlexander | 14:8320b5ff96fa | 763 | VL53L0X_SchedulerSequenceSteps_t *p_scheduler_sequence_steps) |
johnAlexander | 0:c523920bcc09 | 764 | { |
johnAlexander | 14:8320b5ff96fa | 765 | VL53L0X_Error status = VL53L0X_ERROR_NONE; |
johnAlexander | 14:8320b5ff96fa | 766 | uint8_t sequence_config = 0; |
johnAlexander | 14:8320b5ff96fa | 767 | LOG_FUNCTION_START(""); |
johnAlexander | 14:8320b5ff96fa | 768 | |
johnAlexander | 14:8320b5ff96fa | 769 | status = vl53l0x_read_byte(dev, VL53L0X_REG_SYSTEM_SEQUENCE_CONFIG, |
johnAlexander | 14:8320b5ff96fa | 770 | &sequence_config); |
johnAlexander | 14:8320b5ff96fa | 771 | |
johnAlexander | 14:8320b5ff96fa | 772 | if (status == VL53L0X_ERROR_NONE) { |
johnAlexander | 14:8320b5ff96fa | 773 | status = sequence_step_enabled(dev, |
johnAlexander | 14:8320b5ff96fa | 774 | VL53L0X_SEQUENCESTEP_TCC, sequence_config, |
johnAlexander | 14:8320b5ff96fa | 775 | &p_scheduler_sequence_steps->TccOn); |
johnAlexander | 14:8320b5ff96fa | 776 | } |
johnAlexander | 14:8320b5ff96fa | 777 | if (status == VL53L0X_ERROR_NONE) { |
johnAlexander | 14:8320b5ff96fa | 778 | status = sequence_step_enabled(dev, |
johnAlexander | 14:8320b5ff96fa | 779 | VL53L0X_SEQUENCESTEP_DSS, sequence_config, |
johnAlexander | 14:8320b5ff96fa | 780 | &p_scheduler_sequence_steps->DssOn); |
johnAlexander | 14:8320b5ff96fa | 781 | } |
johnAlexander | 14:8320b5ff96fa | 782 | if (status == VL53L0X_ERROR_NONE) { |
johnAlexander | 14:8320b5ff96fa | 783 | status = sequence_step_enabled(dev, |
johnAlexander | 14:8320b5ff96fa | 784 | VL53L0X_SEQUENCESTEP_MSRC, sequence_config, |
johnAlexander | 14:8320b5ff96fa | 785 | &p_scheduler_sequence_steps->MsrcOn); |
johnAlexander | 14:8320b5ff96fa | 786 | } |
johnAlexander | 14:8320b5ff96fa | 787 | if (status == VL53L0X_ERROR_NONE) { |
johnAlexander | 14:8320b5ff96fa | 788 | status = sequence_step_enabled(dev, |
johnAlexander | 14:8320b5ff96fa | 789 | VL53L0X_SEQUENCESTEP_PRE_RANGE, sequence_config, |
johnAlexander | 14:8320b5ff96fa | 790 | &p_scheduler_sequence_steps->PreRangeOn); |
johnAlexander | 14:8320b5ff96fa | 791 | } |
johnAlexander | 14:8320b5ff96fa | 792 | if (status == VL53L0X_ERROR_NONE) { |
johnAlexander | 14:8320b5ff96fa | 793 | status = sequence_step_enabled(dev, |
johnAlexander | 14:8320b5ff96fa | 794 | VL53L0X_SEQUENCESTEP_FINAL_RANGE, sequence_config, |
johnAlexander | 14:8320b5ff96fa | 795 | &p_scheduler_sequence_steps->FinalRangeOn); |
johnAlexander | 14:8320b5ff96fa | 796 | } |
johnAlexander | 14:8320b5ff96fa | 797 | |
johnAlexander | 14:8320b5ff96fa | 798 | LOG_FUNCTION_END(status); |
johnAlexander | 14:8320b5ff96fa | 799 | return status; |
johnAlexander | 0:c523920bcc09 | 800 | } |
johnAlexander | 0:c523920bcc09 | 801 | |
johnAlexander | 14:8320b5ff96fa | 802 | uint8_t VL53L0X::vl53l0x_decode_vcsel_period(uint8_t vcsel_period_reg) |
johnAlexander | 0:c523920bcc09 | 803 | { |
johnAlexander | 14:8320b5ff96fa | 804 | /*! |
johnAlexander | 14:8320b5ff96fa | 805 | * Converts the encoded VCSEL period register value into the real |
johnAlexander | 14:8320b5ff96fa | 806 | * period in PLL clocks |
johnAlexander | 14:8320b5ff96fa | 807 | */ |
johnAlexander | 14:8320b5ff96fa | 808 | |
johnAlexander | 14:8320b5ff96fa | 809 | uint8_t vcsel_period_pclks = 0; |
johnAlexander | 14:8320b5ff96fa | 810 | |
johnAlexander | 14:8320b5ff96fa | 811 | vcsel_period_pclks = (vcsel_period_reg + 1) << 1; |
johnAlexander | 14:8320b5ff96fa | 812 | |
johnAlexander | 14:8320b5ff96fa | 813 | return vcsel_period_pclks; |
johnAlexander | 0:c523920bcc09 | 814 | } |
johnAlexander | 0:c523920bcc09 | 815 | |
johnAlexander | 14:8320b5ff96fa | 816 | uint8_t VL53L0X::lv53l0x_encode_vcsel_period(uint8_t vcsel_period_pclks) |
johnAlexander | 0:c523920bcc09 | 817 | { |
johnAlexander | 14:8320b5ff96fa | 818 | /*! |
johnAlexander | 14:8320b5ff96fa | 819 | * Converts the encoded VCSEL period register value into the real period |
johnAlexander | 14:8320b5ff96fa | 820 | * in PLL clocks |
johnAlexander | 14:8320b5ff96fa | 821 | */ |
johnAlexander | 14:8320b5ff96fa | 822 | |
johnAlexander | 14:8320b5ff96fa | 823 | uint8_t vcsel_period_reg = 0; |
johnAlexander | 14:8320b5ff96fa | 824 | |
johnAlexander | 14:8320b5ff96fa | 825 | vcsel_period_reg = (vcsel_period_pclks >> 1) - 1; |
johnAlexander | 14:8320b5ff96fa | 826 | |
johnAlexander | 14:8320b5ff96fa | 827 | return vcsel_period_reg; |
johnAlexander | 0:c523920bcc09 | 828 | } |
johnAlexander | 0:c523920bcc09 | 829 | |
johnAlexander | 0:c523920bcc09 | 830 | |
johnAlexander | 14:8320b5ff96fa | 831 | VL53L0X_Error VL53L0X::wrapped_vl53l0x_set_vcsel_pulse_period(VL53L0X_DEV dev, |
johnAlexander | 14:8320b5ff96fa | 832 | VL53L0X_VcselPeriod vcsel_period_type, uint8_t vcsel_pulse_period_pclk) |
johnAlexander | 0:c523920bcc09 | 833 | { |
johnAlexander | 14:8320b5ff96fa | 834 | VL53L0X_Error status = VL53L0X_ERROR_NONE; |
johnAlexander | 14:8320b5ff96fa | 835 | uint8_t vcsel_period_reg; |
johnAlexander | 14:8320b5ff96fa | 836 | uint8_t min_pre_vcsel_period_pclk = 12; |
johnAlexander | 14:8320b5ff96fa | 837 | uint8_t max_pre_vcsel_period_pclk = 18; |
johnAlexander | 14:8320b5ff96fa | 838 | uint8_t min_final_vcsel_period_pclk = 8; |
johnAlexander | 14:8320b5ff96fa | 839 | uint8_t max_final_vcsel_period_pclk = 14; |
johnAlexander | 14:8320b5ff96fa | 840 | uint32_t measurement_timing_budget_micro_seconds; |
johnAlexander | 14:8320b5ff96fa | 841 | uint32_t final_range_timeout_micro_seconds; |
johnAlexander | 14:8320b5ff96fa | 842 | uint32_t pre_range_timeout_micro_seconds; |
johnAlexander | 14:8320b5ff96fa | 843 | uint32_t msrc_timeout_micro_seconds; |
johnAlexander | 14:8320b5ff96fa | 844 | uint8_t phase_cal_int = 0; |
johnAlexander | 14:8320b5ff96fa | 845 | |
johnAlexander | 14:8320b5ff96fa | 846 | /* Check if valid clock period requested */ |
johnAlexander | 14:8320b5ff96fa | 847 | |
johnAlexander | 14:8320b5ff96fa | 848 | if ((vcsel_pulse_period_pclk % 2) != 0) { |
johnAlexander | 14:8320b5ff96fa | 849 | /* Value must be an even number */ |
johnAlexander | 14:8320b5ff96fa | 850 | status = VL53L0X_ERROR_INVALID_PARAMS; |
johnAlexander | 14:8320b5ff96fa | 851 | } else if (vcsel_period_type == VL53L0X_VCSEL_PERIOD_PRE_RANGE && |
johnAlexander | 14:8320b5ff96fa | 852 | (vcsel_pulse_period_pclk < min_pre_vcsel_period_pclk || |
johnAlexander | 14:8320b5ff96fa | 853 | vcsel_pulse_period_pclk > max_pre_vcsel_period_pclk)) { |
johnAlexander | 14:8320b5ff96fa | 854 | status = VL53L0X_ERROR_INVALID_PARAMS; |
johnAlexander | 14:8320b5ff96fa | 855 | } else if (vcsel_period_type == VL53L0X_VCSEL_PERIOD_FINAL_RANGE && |
johnAlexander | 14:8320b5ff96fa | 856 | (vcsel_pulse_period_pclk < min_final_vcsel_period_pclk || |
johnAlexander | 14:8320b5ff96fa | 857 | vcsel_pulse_period_pclk > max_final_vcsel_period_pclk)) { |
johnAlexander | 14:8320b5ff96fa | 858 | |
johnAlexander | 14:8320b5ff96fa | 859 | status = VL53L0X_ERROR_INVALID_PARAMS; |
johnAlexander | 14:8320b5ff96fa | 860 | } |
johnAlexander | 14:8320b5ff96fa | 861 | |
johnAlexander | 14:8320b5ff96fa | 862 | /* Apply specific settings for the requested clock period */ |
johnAlexander | 14:8320b5ff96fa | 863 | |
johnAlexander | 14:8320b5ff96fa | 864 | if (status != VL53L0X_ERROR_NONE) |
johnAlexander | 14:8320b5ff96fa | 865 | return status; |
johnAlexander | 14:8320b5ff96fa | 866 | |
johnAlexander | 14:8320b5ff96fa | 867 | |
johnAlexander | 14:8320b5ff96fa | 868 | if (vcsel_period_type == VL53L0X_VCSEL_PERIOD_PRE_RANGE) { |
johnAlexander | 14:8320b5ff96fa | 869 | |
johnAlexander | 14:8320b5ff96fa | 870 | /* Set phase check limits */ |
johnAlexander | 14:8320b5ff96fa | 871 | if (vcsel_pulse_period_pclk == 12) { |
johnAlexander | 14:8320b5ff96fa | 872 | |
johnAlexander | 14:8320b5ff96fa | 873 | status = vl53l0x_write_byte(dev, |
johnAlexander | 14:8320b5ff96fa | 874 | VL53L0X_REG_PRE_RANGE_CONFIG_VALID_PHASE_HIGH, |
johnAlexander | 14:8320b5ff96fa | 875 | 0x18); |
johnAlexander | 14:8320b5ff96fa | 876 | status = vl53l0x_write_byte(dev, |
johnAlexander | 14:8320b5ff96fa | 877 | VL53L0X_REG_PRE_RANGE_CONFIG_VALID_PHASE_LOW, |
johnAlexander | 14:8320b5ff96fa | 878 | 0x08); |
johnAlexander | 14:8320b5ff96fa | 879 | } else if (vcsel_pulse_period_pclk == 14) { |
johnAlexander | 14:8320b5ff96fa | 880 | |
johnAlexander | 14:8320b5ff96fa | 881 | status = vl53l0x_write_byte(dev, |
johnAlexander | 14:8320b5ff96fa | 882 | VL53L0X_REG_PRE_RANGE_CONFIG_VALID_PHASE_HIGH, |
johnAlexander | 14:8320b5ff96fa | 883 | 0x30); |
johnAlexander | 14:8320b5ff96fa | 884 | status = vl53l0x_write_byte(dev, |
johnAlexander | 14:8320b5ff96fa | 885 | VL53L0X_REG_PRE_RANGE_CONFIG_VALID_PHASE_LOW, |
johnAlexander | 14:8320b5ff96fa | 886 | 0x08); |
johnAlexander | 14:8320b5ff96fa | 887 | } else if (vcsel_pulse_period_pclk == 16) { |
johnAlexander | 14:8320b5ff96fa | 888 | |
johnAlexander | 14:8320b5ff96fa | 889 | status = vl53l0x_write_byte(dev, |
johnAlexander | 14:8320b5ff96fa | 890 | VL53L0X_REG_PRE_RANGE_CONFIG_VALID_PHASE_HIGH, |
johnAlexander | 14:8320b5ff96fa | 891 | 0x40); |
johnAlexander | 14:8320b5ff96fa | 892 | status = vl53l0x_write_byte(dev, |
johnAlexander | 14:8320b5ff96fa | 893 | VL53L0X_REG_PRE_RANGE_CONFIG_VALID_PHASE_LOW, |
johnAlexander | 14:8320b5ff96fa | 894 | 0x08); |
johnAlexander | 14:8320b5ff96fa | 895 | } else if (vcsel_pulse_period_pclk == 18) { |
johnAlexander | 14:8320b5ff96fa | 896 | |
johnAlexander | 14:8320b5ff96fa | 897 | status = vl53l0x_write_byte(dev, |
johnAlexander | 14:8320b5ff96fa | 898 | VL53L0X_REG_PRE_RANGE_CONFIG_VALID_PHASE_HIGH, |
johnAlexander | 14:8320b5ff96fa | 899 | 0x50); |
johnAlexander | 14:8320b5ff96fa | 900 | status = vl53l0x_write_byte(dev, |
johnAlexander | 14:8320b5ff96fa | 901 | VL53L0X_REG_PRE_RANGE_CONFIG_VALID_PHASE_LOW, |
johnAlexander | 14:8320b5ff96fa | 902 | 0x08); |
johnAlexander | 14:8320b5ff96fa | 903 | } |
johnAlexander | 14:8320b5ff96fa | 904 | } else if (vcsel_period_type == VL53L0X_VCSEL_PERIOD_FINAL_RANGE) { |
johnAlexander | 14:8320b5ff96fa | 905 | |
johnAlexander | 14:8320b5ff96fa | 906 | if (vcsel_pulse_period_pclk == 8) { |
johnAlexander | 14:8320b5ff96fa | 907 | |
johnAlexander | 14:8320b5ff96fa | 908 | status = vl53l0x_write_byte(dev, |
johnAlexander | 14:8320b5ff96fa | 909 | VL53L0X_REG_FINAL_RANGE_CONFIG_VALID_PHASE_HIGH, |
johnAlexander | 14:8320b5ff96fa | 910 | 0x10); |
johnAlexander | 14:8320b5ff96fa | 911 | status = vl53l0x_write_byte(dev, |
johnAlexander | 14:8320b5ff96fa | 912 | VL53L0X_REG_FINAL_RANGE_CONFIG_VALID_PHASE_LOW, |
johnAlexander | 14:8320b5ff96fa | 913 | 0x08); |
johnAlexander | 14:8320b5ff96fa | 914 | |
johnAlexander | 14:8320b5ff96fa | 915 | status |= vl53l0x_write_byte(dev, |
johnAlexander | 14:8320b5ff96fa | 916 | VL53L0X_REG_GLOBAL_CONFIG_VCSEL_WIDTH, 0x02); |
johnAlexander | 14:8320b5ff96fa | 917 | status |= vl53l0x_write_byte(dev, |
johnAlexander | 14:8320b5ff96fa | 918 | VL53L0X_REG_ALGO_PHASECAL_CONFIG_TIMEOUT, 0x0C); |
johnAlexander | 14:8320b5ff96fa | 919 | |
johnAlexander | 14:8320b5ff96fa | 920 | status |= vl53l0x_write_byte(dev, 0xff, 0x01); |
johnAlexander | 14:8320b5ff96fa | 921 | status |= vl53l0x_write_byte(dev, |
johnAlexander | 14:8320b5ff96fa | 922 | VL53L0X_REG_ALGO_PHASECAL_LIM, |
johnAlexander | 14:8320b5ff96fa | 923 | 0x30); |
johnAlexander | 14:8320b5ff96fa | 924 | status |= vl53l0x_write_byte(dev, 0xff, 0x00); |
johnAlexander | 14:8320b5ff96fa | 925 | } else if (vcsel_pulse_period_pclk == 10) { |
johnAlexander | 14:8320b5ff96fa | 926 | |
johnAlexander | 14:8320b5ff96fa | 927 | status = vl53l0x_write_byte(dev, |
johnAlexander | 14:8320b5ff96fa | 928 | VL53L0X_REG_FINAL_RANGE_CONFIG_VALID_PHASE_HIGH, |
johnAlexander | 14:8320b5ff96fa | 929 | 0x28); |
johnAlexander | 14:8320b5ff96fa | 930 | status = vl53l0x_write_byte(dev, |
johnAlexander | 14:8320b5ff96fa | 931 | VL53L0X_REG_FINAL_RANGE_CONFIG_VALID_PHASE_LOW, |
johnAlexander | 14:8320b5ff96fa | 932 | 0x08); |
johnAlexander | 14:8320b5ff96fa | 933 | |
johnAlexander | 14:8320b5ff96fa | 934 | status |= vl53l0x_write_byte(dev, |
johnAlexander | 14:8320b5ff96fa | 935 | VL53L0X_REG_GLOBAL_CONFIG_VCSEL_WIDTH, 0x03); |
johnAlexander | 14:8320b5ff96fa | 936 | status |= vl53l0x_write_byte(dev, |
johnAlexander | 14:8320b5ff96fa | 937 | VL53L0X_REG_ALGO_PHASECAL_CONFIG_TIMEOUT, 0x09); |
johnAlexander | 14:8320b5ff96fa | 938 | |
johnAlexander | 14:8320b5ff96fa | 939 | status |= vl53l0x_write_byte(dev, 0xff, 0x01); |
johnAlexander | 14:8320b5ff96fa | 940 | status |= vl53l0x_write_byte(dev, |
johnAlexander | 14:8320b5ff96fa | 941 | VL53L0X_REG_ALGO_PHASECAL_LIM, |
johnAlexander | 14:8320b5ff96fa | 942 | 0x20); |
johnAlexander | 14:8320b5ff96fa | 943 | status |= vl53l0x_write_byte(dev, 0xff, 0x00); |
johnAlexander | 14:8320b5ff96fa | 944 | } else if (vcsel_pulse_period_pclk == 12) { |
johnAlexander | 14:8320b5ff96fa | 945 | |
johnAlexander | 14:8320b5ff96fa | 946 | status = vl53l0x_write_byte(dev, |
johnAlexander | 14:8320b5ff96fa | 947 | VL53L0X_REG_FINAL_RANGE_CONFIG_VALID_PHASE_HIGH, |
johnAlexander | 14:8320b5ff96fa | 948 | 0x38); |
johnAlexander | 14:8320b5ff96fa | 949 | status = vl53l0x_write_byte(dev, |
johnAlexander | 14:8320b5ff96fa | 950 | VL53L0X_REG_FINAL_RANGE_CONFIG_VALID_PHASE_LOW, |
johnAlexander | 14:8320b5ff96fa | 951 | 0x08); |
johnAlexander | 14:8320b5ff96fa | 952 | |
johnAlexander | 14:8320b5ff96fa | 953 | status |= vl53l0x_write_byte(dev, |
johnAlexander | 14:8320b5ff96fa | 954 | VL53L0X_REG_GLOBAL_CONFIG_VCSEL_WIDTH, 0x03); |
johnAlexander | 14:8320b5ff96fa | 955 | status |= vl53l0x_write_byte(dev, |
johnAlexander | 14:8320b5ff96fa | 956 | VL53L0X_REG_ALGO_PHASECAL_CONFIG_TIMEOUT, 0x08); |
johnAlexander | 14:8320b5ff96fa | 957 | |
johnAlexander | 14:8320b5ff96fa | 958 | status |= vl53l0x_write_byte(dev, 0xff, 0x01); |
johnAlexander | 14:8320b5ff96fa | 959 | status |= vl53l0x_write_byte(dev, |
johnAlexander | 14:8320b5ff96fa | 960 | VL53L0X_REG_ALGO_PHASECAL_LIM, |
johnAlexander | 14:8320b5ff96fa | 961 | 0x20); |
johnAlexander | 14:8320b5ff96fa | 962 | status |= vl53l0x_write_byte(dev, 0xff, 0x00); |
johnAlexander | 14:8320b5ff96fa | 963 | } else if (vcsel_pulse_period_pclk == 14) { |
johnAlexander | 14:8320b5ff96fa | 964 | |
johnAlexander | 14:8320b5ff96fa | 965 | status = vl53l0x_write_byte(dev, |
johnAlexander | 14:8320b5ff96fa | 966 | VL53L0X_REG_FINAL_RANGE_CONFIG_VALID_PHASE_HIGH, |
johnAlexander | 14:8320b5ff96fa | 967 | 0x048); |
johnAlexander | 14:8320b5ff96fa | 968 | status = vl53l0x_write_byte(dev, |
johnAlexander | 14:8320b5ff96fa | 969 | VL53L0X_REG_FINAL_RANGE_CONFIG_VALID_PHASE_LOW, |
johnAlexander | 14:8320b5ff96fa | 970 | 0x08); |
johnAlexander | 14:8320b5ff96fa | 971 | |
johnAlexander | 14:8320b5ff96fa | 972 | status |= vl53l0x_write_byte(dev, |
johnAlexander | 14:8320b5ff96fa | 973 | VL53L0X_REG_GLOBAL_CONFIG_VCSEL_WIDTH, 0x03); |
johnAlexander | 14:8320b5ff96fa | 974 | status |= vl53l0x_write_byte(dev, |
johnAlexander | 14:8320b5ff96fa | 975 | VL53L0X_REG_ALGO_PHASECAL_CONFIG_TIMEOUT, 0x07); |
johnAlexander | 14:8320b5ff96fa | 976 | |
johnAlexander | 14:8320b5ff96fa | 977 | status |= vl53l0x_write_byte(dev, 0xff, 0x01); |
johnAlexander | 14:8320b5ff96fa | 978 | status |= vl53l0x_write_byte(dev, |
johnAlexander | 14:8320b5ff96fa | 979 | VL53L0X_REG_ALGO_PHASECAL_LIM, |
johnAlexander | 14:8320b5ff96fa | 980 | 0x20); |
johnAlexander | 14:8320b5ff96fa | 981 | status |= vl53l0x_write_byte(dev, 0xff, 0x00); |
johnAlexander | 14:8320b5ff96fa | 982 | } |
johnAlexander | 14:8320b5ff96fa | 983 | } |
johnAlexander | 14:8320b5ff96fa | 984 | |
johnAlexander | 14:8320b5ff96fa | 985 | |
johnAlexander | 14:8320b5ff96fa | 986 | /* Re-calculate and apply timeouts, in macro periods */ |
johnAlexander | 14:8320b5ff96fa | 987 | |
johnAlexander | 14:8320b5ff96fa | 988 | if (status == VL53L0X_ERROR_NONE) { |
johnAlexander | 14:8320b5ff96fa | 989 | vcsel_period_reg = lv53l0x_encode_vcsel_period((uint8_t) |
johnAlexander | 14:8320b5ff96fa | 990 | vcsel_pulse_period_pclk); |
johnAlexander | 14:8320b5ff96fa | 991 | |
johnAlexander | 14:8320b5ff96fa | 992 | /* When the VCSEL period for the pre or final range is changed, |
johnAlexander | 14:8320b5ff96fa | 993 | * the corresponding timeout must be read from the device using |
johnAlexander | 14:8320b5ff96fa | 994 | * the current VCSEL period, then the new VCSEL period can be |
johnAlexander | 14:8320b5ff96fa | 995 | * applied. The timeout then must be written back to the device |
johnAlexander | 14:8320b5ff96fa | 996 | * using the new VCSEL period. |
johnAlexander | 14:8320b5ff96fa | 997 | * |
johnAlexander | 14:8320b5ff96fa | 998 | * For the MSRC timeout, the same applies - this timeout being |
johnAlexander | 14:8320b5ff96fa | 999 | * dependant on the pre-range vcsel period. |
johnAlexander | 14:8320b5ff96fa | 1000 | */ |
johnAlexander | 14:8320b5ff96fa | 1001 | switch (vcsel_period_type) { |
johnAlexander | 14:8320b5ff96fa | 1002 | case VL53L0X_VCSEL_PERIOD_PRE_RANGE: |
johnAlexander | 14:8320b5ff96fa | 1003 | status = get_sequence_step_timeout(dev, |
johnAlexander | 14:8320b5ff96fa | 1004 | VL53L0X_SEQUENCESTEP_PRE_RANGE, |
johnAlexander | 14:8320b5ff96fa | 1005 | &pre_range_timeout_micro_seconds); |
johnAlexander | 14:8320b5ff96fa | 1006 | |
johnAlexander | 14:8320b5ff96fa | 1007 | if (status == VL53L0X_ERROR_NONE) |
johnAlexander | 14:8320b5ff96fa | 1008 | status = get_sequence_step_timeout(dev, |
johnAlexander | 14:8320b5ff96fa | 1009 | VL53L0X_SEQUENCESTEP_MSRC, |
johnAlexander | 14:8320b5ff96fa | 1010 | &msrc_timeout_micro_seconds); |
johnAlexander | 14:8320b5ff96fa | 1011 | |
johnAlexander | 14:8320b5ff96fa | 1012 | if (status == VL53L0X_ERROR_NONE) |
johnAlexander | 14:8320b5ff96fa | 1013 | status = vl53l0x_write_byte(dev, |
johnAlexander | 14:8320b5ff96fa | 1014 | VL53L0X_REG_PRE_RANGE_CONFIG_VCSEL_PERIOD, |
johnAlexander | 14:8320b5ff96fa | 1015 | vcsel_period_reg); |
johnAlexander | 14:8320b5ff96fa | 1016 | |
johnAlexander | 14:8320b5ff96fa | 1017 | |
johnAlexander | 14:8320b5ff96fa | 1018 | if (status == VL53L0X_ERROR_NONE) |
johnAlexander | 14:8320b5ff96fa | 1019 | status = set_sequence_step_timeout(dev, |
johnAlexander | 14:8320b5ff96fa | 1020 | VL53L0X_SEQUENCESTEP_PRE_RANGE, |
johnAlexander | 14:8320b5ff96fa | 1021 | pre_range_timeout_micro_seconds); |
johnAlexander | 14:8320b5ff96fa | 1022 | |
johnAlexander | 14:8320b5ff96fa | 1023 | |
johnAlexander | 14:8320b5ff96fa | 1024 | if (status == VL53L0X_ERROR_NONE) |
johnAlexander | 14:8320b5ff96fa | 1025 | status = set_sequence_step_timeout(dev, |
johnAlexander | 14:8320b5ff96fa | 1026 | VL53L0X_SEQUENCESTEP_MSRC, |
johnAlexander | 14:8320b5ff96fa | 1027 | msrc_timeout_micro_seconds); |
johnAlexander | 14:8320b5ff96fa | 1028 | |
johnAlexander | 14:8320b5ff96fa | 1029 | VL53L0X_SETDEVICESPECIFICPARAMETER( |
johnAlexander | 14:8320b5ff96fa | 1030 | dev, |
johnAlexander | 14:8320b5ff96fa | 1031 | PreRangeVcselPulsePeriod, |
johnAlexander | 14:8320b5ff96fa | 1032 | vcsel_pulse_period_pclk); |
johnAlexander | 14:8320b5ff96fa | 1033 | break; |
johnAlexander | 14:8320b5ff96fa | 1034 | case VL53L0X_VCSEL_PERIOD_FINAL_RANGE: |
johnAlexander | 14:8320b5ff96fa | 1035 | status = get_sequence_step_timeout(dev, |
johnAlexander | 14:8320b5ff96fa | 1036 | VL53L0X_SEQUENCESTEP_FINAL_RANGE, |
johnAlexander | 14:8320b5ff96fa | 1037 | &final_range_timeout_micro_seconds); |
johnAlexander | 14:8320b5ff96fa | 1038 | |
johnAlexander | 14:8320b5ff96fa | 1039 | if (status == VL53L0X_ERROR_NONE) |
johnAlexander | 14:8320b5ff96fa | 1040 | status = vl53l0x_write_byte(dev, |
johnAlexander | 14:8320b5ff96fa | 1041 | VL53L0X_REG_FINAL_RANGE_CONFIG_VCSEL_PERIOD, |
johnAlexander | 14:8320b5ff96fa | 1042 | vcsel_period_reg); |
johnAlexander | 14:8320b5ff96fa | 1043 | |
johnAlexander | 14:8320b5ff96fa | 1044 | |
johnAlexander | 14:8320b5ff96fa | 1045 | if (status == VL53L0X_ERROR_NONE) |
johnAlexander | 14:8320b5ff96fa | 1046 | status = set_sequence_step_timeout(dev, |
johnAlexander | 14:8320b5ff96fa | 1047 | VL53L0X_SEQUENCESTEP_FINAL_RANGE, |
johnAlexander | 14:8320b5ff96fa | 1048 | final_range_timeout_micro_seconds); |
johnAlexander | 14:8320b5ff96fa | 1049 | |
johnAlexander | 14:8320b5ff96fa | 1050 | VL53L0X_SETDEVICESPECIFICPARAMETER( |
johnAlexander | 14:8320b5ff96fa | 1051 | dev, |
johnAlexander | 14:8320b5ff96fa | 1052 | FinalRangeVcselPulsePeriod, |
johnAlexander | 14:8320b5ff96fa | 1053 | vcsel_pulse_period_pclk); |
johnAlexander | 14:8320b5ff96fa | 1054 | break; |
johnAlexander | 14:8320b5ff96fa | 1055 | default: |
johnAlexander | 14:8320b5ff96fa | 1056 | status = VL53L0X_ERROR_INVALID_PARAMS; |
johnAlexander | 14:8320b5ff96fa | 1057 | } |
johnAlexander | 14:8320b5ff96fa | 1058 | } |
johnAlexander | 14:8320b5ff96fa | 1059 | |
johnAlexander | 14:8320b5ff96fa | 1060 | /* Finally, the timing budget must be re-applied */ |
johnAlexander | 14:8320b5ff96fa | 1061 | if (status == VL53L0X_ERROR_NONE) { |
johnAlexander | 14:8320b5ff96fa | 1062 | VL53L0X_GETPARAMETERFIELD(dev, |
johnAlexander | 14:8320b5ff96fa | 1063 | MeasurementTimingBudgetMicroSeconds, |
johnAlexander | 14:8320b5ff96fa | 1064 | measurement_timing_budget_micro_seconds); |
johnAlexander | 14:8320b5ff96fa | 1065 | |
johnAlexander | 14:8320b5ff96fa | 1066 | status = vl53l0x_set_measurement_timing_budget_micro_seconds(dev, |
johnAlexander | 14:8320b5ff96fa | 1067 | measurement_timing_budget_micro_seconds); |
johnAlexander | 14:8320b5ff96fa | 1068 | } |
johnAlexander | 14:8320b5ff96fa | 1069 | |
johnAlexander | 14:8320b5ff96fa | 1070 | /* Perform the phase calibration. This is needed after changing on |
johnAlexander | 14:8320b5ff96fa | 1071 | * vcsel period. |
johnAlexander | 14:8320b5ff96fa | 1072 | * get_data_enable = 0, restore_config = 1 */ |
johnAlexander | 14:8320b5ff96fa | 1073 | if (status == VL53L0X_ERROR_NONE) |
johnAlexander | 14:8320b5ff96fa | 1074 | status = vl53l0x_perform_phase_calibration( |
johnAlexander | 14:8320b5ff96fa | 1075 | dev, &phase_cal_int, 0, 1); |
johnAlexander | 14:8320b5ff96fa | 1076 | |
johnAlexander | 14:8320b5ff96fa | 1077 | return status; |
johnAlexander | 0:c523920bcc09 | 1078 | } |
johnAlexander | 0:c523920bcc09 | 1079 | |
johnAlexander | 14:8320b5ff96fa | 1080 | VL53L0X_Error VL53L0X::vl53l0x_set_vcsel_pulse_period(VL53L0X_DEV dev, |
johnAlexander | 14:8320b5ff96fa | 1081 | VL53L0X_VcselPeriod vcsel_period_type, uint8_t vcsel_pulse_period) |
johnAlexander | 0:c523920bcc09 | 1082 | { |
johnAlexander | 14:8320b5ff96fa | 1083 | VL53L0X_Error status = VL53L0X_ERROR_NONE; |
johnAlexander | 14:8320b5ff96fa | 1084 | LOG_FUNCTION_START(""); |
johnAlexander | 14:8320b5ff96fa | 1085 | |
johnAlexander | 14:8320b5ff96fa | 1086 | status = wrapped_vl53l0x_set_vcsel_pulse_period(dev, vcsel_period_type, |
johnAlexander | 14:8320b5ff96fa | 1087 | vcsel_pulse_period); |
johnAlexander | 14:8320b5ff96fa | 1088 | |
johnAlexander | 14:8320b5ff96fa | 1089 | LOG_FUNCTION_END(status); |
johnAlexander | 14:8320b5ff96fa | 1090 | return status; |
johnAlexander | 0:c523920bcc09 | 1091 | } |
johnAlexander | 0:c523920bcc09 | 1092 | |
johnAlexander | 14:8320b5ff96fa | 1093 | VL53L0X_Error VL53L0X::wrapped_vl53l0x_get_vcsel_pulse_period(VL53L0X_DEV dev, |
johnAlexander | 14:8320b5ff96fa | 1094 | VL53L0X_VcselPeriod vcsel_period_type, uint8_t *p_vcsel_pulse_period_pclk) |
johnAlexander | 0:c523920bcc09 | 1095 | { |
johnAlexander | 14:8320b5ff96fa | 1096 | VL53L0X_Error status = VL53L0X_ERROR_NONE; |
johnAlexander | 14:8320b5ff96fa | 1097 | uint8_t vcsel_period_reg; |
johnAlexander | 14:8320b5ff96fa | 1098 | |
johnAlexander | 14:8320b5ff96fa | 1099 | switch (vcsel_period_type) { |
johnAlexander | 14:8320b5ff96fa | 1100 | case VL53L0X_VCSEL_PERIOD_PRE_RANGE: |
johnAlexander | 14:8320b5ff96fa | 1101 | status = vl53l0x_read_byte(dev, |
johnAlexander | 14:8320b5ff96fa | 1102 | VL53L0X_REG_PRE_RANGE_CONFIG_VCSEL_PERIOD, |
johnAlexander | 14:8320b5ff96fa | 1103 | &vcsel_period_reg); |
johnAlexander | 14:8320b5ff96fa | 1104 | break; |
johnAlexander | 14:8320b5ff96fa | 1105 | case VL53L0X_VCSEL_PERIOD_FINAL_RANGE: |
johnAlexander | 14:8320b5ff96fa | 1106 | status = vl53l0x_read_byte(dev, |
johnAlexander | 14:8320b5ff96fa | 1107 | VL53L0X_REG_FINAL_RANGE_CONFIG_VCSEL_PERIOD, |
johnAlexander | 14:8320b5ff96fa | 1108 | &vcsel_period_reg); |
johnAlexander | 14:8320b5ff96fa | 1109 | break; |
johnAlexander | 14:8320b5ff96fa | 1110 | default: |
johnAlexander | 14:8320b5ff96fa | 1111 | status = VL53L0X_ERROR_INVALID_PARAMS; |
johnAlexander | 14:8320b5ff96fa | 1112 | } |
johnAlexander | 14:8320b5ff96fa | 1113 | |
johnAlexander | 14:8320b5ff96fa | 1114 | if (status == VL53L0X_ERROR_NONE) |
johnAlexander | 14:8320b5ff96fa | 1115 | *p_vcsel_pulse_period_pclk = |
johnAlexander | 14:8320b5ff96fa | 1116 | vl53l0x_decode_vcsel_period(vcsel_period_reg); |
johnAlexander | 14:8320b5ff96fa | 1117 | |
johnAlexander | 14:8320b5ff96fa | 1118 | return status; |
johnAlexander | 0:c523920bcc09 | 1119 | } |
johnAlexander | 0:c523920bcc09 | 1120 | |
johnAlexander | 14:8320b5ff96fa | 1121 | VL53L0X_Error VL53L0X::vl53l0x_get_vcsel_pulse_period(VL53L0X_DEV dev, |
johnAlexander | 14:8320b5ff96fa | 1122 | VL53L0X_VcselPeriod vcsel_period_type, uint8_t *p_vcsel_pulse_period_pclk) |
johnAlexander | 0:c523920bcc09 | 1123 | { |
johnAlexander | 14:8320b5ff96fa | 1124 | VL53L0X_Error status = VL53L0X_ERROR_NONE; |
johnAlexander | 14:8320b5ff96fa | 1125 | LOG_FUNCTION_START(""); |
johnAlexander | 14:8320b5ff96fa | 1126 | |
johnAlexander | 14:8320b5ff96fa | 1127 | status = wrapped_vl53l0x_get_vcsel_pulse_period(dev, vcsel_period_type, |
johnAlexander | 14:8320b5ff96fa | 1128 | p_vcsel_pulse_period_pclk); |
johnAlexander | 14:8320b5ff96fa | 1129 | |
johnAlexander | 14:8320b5ff96fa | 1130 | LOG_FUNCTION_END(status); |
johnAlexander | 14:8320b5ff96fa | 1131 | return status; |
johnAlexander | 0:c523920bcc09 | 1132 | } |
johnAlexander | 0:c523920bcc09 | 1133 | |
johnAlexander | 14:8320b5ff96fa | 1134 | uint32_t VL53L0X::vl53l0x_decode_timeout(uint16_t encoded_timeout) |
johnAlexander | 0:c523920bcc09 | 1135 | { |
johnAlexander | 14:8320b5ff96fa | 1136 | /*! |
johnAlexander | 14:8320b5ff96fa | 1137 | * Decode 16-bit timeout register value - format (LSByte * 2^MSByte) + 1 |
johnAlexander | 14:8320b5ff96fa | 1138 | */ |
johnAlexander | 14:8320b5ff96fa | 1139 | |
johnAlexander | 14:8320b5ff96fa | 1140 | uint32_t timeout_macro_clks = 0; |
johnAlexander | 14:8320b5ff96fa | 1141 | |
johnAlexander | 14:8320b5ff96fa | 1142 | timeout_macro_clks = ((uint32_t)(encoded_timeout & 0x00FF) |
johnAlexander | 14:8320b5ff96fa | 1143 | << (uint32_t)((encoded_timeout & 0xFF00) >> 8)) + 1; |
johnAlexander | 14:8320b5ff96fa | 1144 | |
johnAlexander | 14:8320b5ff96fa | 1145 | return timeout_macro_clks; |
johnAlexander | 0:c523920bcc09 | 1146 | } |
johnAlexander | 0:c523920bcc09 | 1147 | |
johnAlexander | 14:8320b5ff96fa | 1148 | uint32_t VL53L0X::vl53l0x_calc_macro_period_ps(VL53L0X_DEV dev, uint8_t vcsel_period_pclks) |
johnAlexander | 0:c523920bcc09 | 1149 | { |
johnAlexander | 14:8320b5ff96fa | 1150 | uint64_t pll_period_ps; |
johnAlexander | 14:8320b5ff96fa | 1151 | uint32_t macro_period_vclks; |
johnAlexander | 14:8320b5ff96fa | 1152 | uint32_t macro_period_ps; |
johnAlexander | 14:8320b5ff96fa | 1153 | |
johnAlexander | 14:8320b5ff96fa | 1154 | LOG_FUNCTION_START(""); |
johnAlexander | 14:8320b5ff96fa | 1155 | |
johnAlexander | 14:8320b5ff96fa | 1156 | /* The above calculation will produce rounding errors, |
johnAlexander | 14:8320b5ff96fa | 1157 | therefore set fixed value |
johnAlexander | 14:8320b5ff96fa | 1158 | */ |
johnAlexander | 14:8320b5ff96fa | 1159 | pll_period_ps = 1655; |
johnAlexander | 14:8320b5ff96fa | 1160 | |
johnAlexander | 14:8320b5ff96fa | 1161 | macro_period_vclks = 2304; |
johnAlexander | 14:8320b5ff96fa | 1162 | macro_period_ps = (uint32_t)(macro_period_vclks |
johnAlexander | 14:8320b5ff96fa | 1163 | * vcsel_period_pclks * pll_period_ps); |
johnAlexander | 14:8320b5ff96fa | 1164 | |
johnAlexander | 14:8320b5ff96fa | 1165 | LOG_FUNCTION_END(""); |
johnAlexander | 14:8320b5ff96fa | 1166 | return macro_period_ps; |
johnAlexander | 0:c523920bcc09 | 1167 | } |
johnAlexander | 0:c523920bcc09 | 1168 | |
johnAlexander | 0:c523920bcc09 | 1169 | /* To convert register value into us */ |
johnAlexander | 14:8320b5ff96fa | 1170 | uint32_t VL53L0X::vl53l0x_calc_timeout_us(VL53L0X_DEV dev, |
johnAlexander | 14:8320b5ff96fa | 1171 | uint16_t timeout_period_mclks, |
johnAlexander | 14:8320b5ff96fa | 1172 | uint8_t vcsel_period_pclks) |
johnAlexander | 0:c523920bcc09 | 1173 | { |
johnAlexander | 14:8320b5ff96fa | 1174 | uint32_t macro_period_ps; |
johnAlexander | 14:8320b5ff96fa | 1175 | uint32_t macro_period_ns; |
johnAlexander | 14:8320b5ff96fa | 1176 | uint32_t actual_timeout_period_us = 0; |
johnAlexander | 14:8320b5ff96fa | 1177 | |
johnAlexander | 14:8320b5ff96fa | 1178 | macro_period_ps = vl53l0x_calc_macro_period_ps(dev, vcsel_period_pclks); |
johnAlexander | 14:8320b5ff96fa | 1179 | macro_period_ns = (macro_period_ps + 500) / 1000; |
johnAlexander | 14:8320b5ff96fa | 1180 | |
johnAlexander | 14:8320b5ff96fa | 1181 | actual_timeout_period_us = |
johnAlexander | 14:8320b5ff96fa | 1182 | ((timeout_period_mclks * macro_period_ns) + 500) / 1000; |
johnAlexander | 14:8320b5ff96fa | 1183 | |
johnAlexander | 14:8320b5ff96fa | 1184 | return actual_timeout_period_us; |
johnAlexander | 0:c523920bcc09 | 1185 | } |
johnAlexander | 0:c523920bcc09 | 1186 | |
johnAlexander | 14:8320b5ff96fa | 1187 | VL53L0X_Error VL53L0X::get_sequence_step_timeout(VL53L0X_DEV dev, |
johnAlexander | 14:8320b5ff96fa | 1188 | VL53L0X_SequenceStepId sequence_step_id, |
johnAlexander | 14:8320b5ff96fa | 1189 | uint32_t *p_time_out_micro_secs) |
johnAlexander | 0:c523920bcc09 | 1190 | { |
johnAlexander | 14:8320b5ff96fa | 1191 | VL53L0X_Error status = VL53L0X_ERROR_NONE; |
johnAlexander | 14:8320b5ff96fa | 1192 | uint8_t current_vcsel_pulse_period_p_clk; |
johnAlexander | 14:8320b5ff96fa | 1193 | uint8_t encoded_time_out_byte = 0; |
johnAlexander | 14:8320b5ff96fa | 1194 | uint32_t timeout_micro_seconds = 0; |
johnAlexander | 14:8320b5ff96fa | 1195 | uint16_t pre_range_encoded_time_out = 0; |
johnAlexander | 14:8320b5ff96fa | 1196 | uint16_t msrc_time_out_m_clks; |
johnAlexander | 14:8320b5ff96fa | 1197 | uint16_t pre_range_time_out_m_clks; |
johnAlexander | 14:8320b5ff96fa | 1198 | uint16_t final_range_time_out_m_clks = 0; |
johnAlexander | 14:8320b5ff96fa | 1199 | uint16_t final_range_encoded_time_out; |
johnAlexander | 14:8320b5ff96fa | 1200 | VL53L0X_SchedulerSequenceSteps_t scheduler_sequence_steps; |
johnAlexander | 14:8320b5ff96fa | 1201 | |
johnAlexander | 14:8320b5ff96fa | 1202 | if ((sequence_step_id == VL53L0X_SEQUENCESTEP_TCC) || |
johnAlexander | 14:8320b5ff96fa | 1203 | (sequence_step_id == VL53L0X_SEQUENCESTEP_DSS) || |
johnAlexander | 14:8320b5ff96fa | 1204 | (sequence_step_id == VL53L0X_SEQUENCESTEP_MSRC)) { |
johnAlexander | 14:8320b5ff96fa | 1205 | |
johnAlexander | 14:8320b5ff96fa | 1206 | status = vl53l0x_get_vcsel_pulse_period(dev, |
johnAlexander | 14:8320b5ff96fa | 1207 | VL53L0X_VCSEL_PERIOD_PRE_RANGE, |
johnAlexander | 14:8320b5ff96fa | 1208 | ¤t_vcsel_pulse_period_p_clk); |
johnAlexander | 14:8320b5ff96fa | 1209 | if (status == VL53L0X_ERROR_NONE) { |
johnAlexander | 14:8320b5ff96fa | 1210 | status = vl53l0x_read_byte(dev, |
johnAlexander | 14:8320b5ff96fa | 1211 | VL53L0X_REG_MSRC_CONFIG_TIMEOUT_MACROP, |
johnAlexander | 14:8320b5ff96fa | 1212 | &encoded_time_out_byte); |
johnAlexander | 14:8320b5ff96fa | 1213 | } |
johnAlexander | 14:8320b5ff96fa | 1214 | msrc_time_out_m_clks = vl53l0x_decode_timeout(encoded_time_out_byte); |
johnAlexander | 14:8320b5ff96fa | 1215 | |
johnAlexander | 14:8320b5ff96fa | 1216 | timeout_micro_seconds = vl53l0x_calc_timeout_us(dev, |
johnAlexander | 14:8320b5ff96fa | 1217 | msrc_time_out_m_clks, |
johnAlexander | 14:8320b5ff96fa | 1218 | current_vcsel_pulse_period_p_clk); |
johnAlexander | 14:8320b5ff96fa | 1219 | } else if (sequence_step_id == VL53L0X_SEQUENCESTEP_PRE_RANGE) { |
johnAlexander | 14:8320b5ff96fa | 1220 | /* Retrieve PRE-RANGE VCSEL Period */ |
johnAlexander | 14:8320b5ff96fa | 1221 | status = vl53l0x_get_vcsel_pulse_period(dev, |
johnAlexander | 14:8320b5ff96fa | 1222 | VL53L0X_VCSEL_PERIOD_PRE_RANGE, |
johnAlexander | 14:8320b5ff96fa | 1223 | ¤t_vcsel_pulse_period_p_clk); |
johnAlexander | 14:8320b5ff96fa | 1224 | |
johnAlexander | 14:8320b5ff96fa | 1225 | /* Retrieve PRE-RANGE Timeout in Macro periods (MCLKS) */ |
johnAlexander | 14:8320b5ff96fa | 1226 | if (status == VL53L0X_ERROR_NONE) { |
johnAlexander | 14:8320b5ff96fa | 1227 | |
johnAlexander | 14:8320b5ff96fa | 1228 | /* Retrieve PRE-RANGE VCSEL Period */ |
johnAlexander | 14:8320b5ff96fa | 1229 | status = vl53l0x_get_vcsel_pulse_period(dev, |
johnAlexander | 14:8320b5ff96fa | 1230 | VL53L0X_VCSEL_PERIOD_PRE_RANGE, |
johnAlexander | 14:8320b5ff96fa | 1231 | ¤t_vcsel_pulse_period_p_clk); |
johnAlexander | 14:8320b5ff96fa | 1232 | |
johnAlexander | 14:8320b5ff96fa | 1233 | if (status == VL53L0X_ERROR_NONE) { |
johnAlexander | 14:8320b5ff96fa | 1234 | status = vl53l0x_read_word(dev, |
johnAlexander | 14:8320b5ff96fa | 1235 | VL53L0X_REG_PRE_RANGE_CONFIG_TIMEOUT_MACROP_HI, |
johnAlexander | 14:8320b5ff96fa | 1236 | &pre_range_encoded_time_out); |
johnAlexander | 14:8320b5ff96fa | 1237 | } |
johnAlexander | 14:8320b5ff96fa | 1238 | |
johnAlexander | 14:8320b5ff96fa | 1239 | pre_range_time_out_m_clks = vl53l0x_decode_timeout( |
johnAlexander | 14:8320b5ff96fa | 1240 | pre_range_encoded_time_out); |
johnAlexander | 14:8320b5ff96fa | 1241 | |
johnAlexander | 14:8320b5ff96fa | 1242 | timeout_micro_seconds = vl53l0x_calc_timeout_us(dev, |
johnAlexander | 14:8320b5ff96fa | 1243 | pre_range_time_out_m_clks, |
johnAlexander | 14:8320b5ff96fa | 1244 | current_vcsel_pulse_period_p_clk); |
johnAlexander | 14:8320b5ff96fa | 1245 | } |
johnAlexander | 14:8320b5ff96fa | 1246 | } else if (sequence_step_id == VL53L0X_SEQUENCESTEP_FINAL_RANGE) { |
johnAlexander | 14:8320b5ff96fa | 1247 | |
johnAlexander | 14:8320b5ff96fa | 1248 | vl53l0x_get_sequence_step_enables(dev, &scheduler_sequence_steps); |
johnAlexander | 14:8320b5ff96fa | 1249 | pre_range_time_out_m_clks = 0; |
johnAlexander | 14:8320b5ff96fa | 1250 | |
johnAlexander | 14:8320b5ff96fa | 1251 | if (scheduler_sequence_steps.PreRangeOn) { |
johnAlexander | 14:8320b5ff96fa | 1252 | /* Retrieve PRE-RANGE VCSEL Period */ |
johnAlexander | 14:8320b5ff96fa | 1253 | status = vl53l0x_get_vcsel_pulse_period(dev, |
johnAlexander | 14:8320b5ff96fa | 1254 | VL53L0X_VCSEL_PERIOD_PRE_RANGE, |
johnAlexander | 14:8320b5ff96fa | 1255 | ¤t_vcsel_pulse_period_p_clk); |
johnAlexander | 14:8320b5ff96fa | 1256 | |
johnAlexander | 14:8320b5ff96fa | 1257 | /* Retrieve PRE-RANGE Timeout in Macro periods |
johnAlexander | 14:8320b5ff96fa | 1258 | * (MCLKS) */ |
johnAlexander | 14:8320b5ff96fa | 1259 | if (status == VL53L0X_ERROR_NONE) { |
johnAlexander | 14:8320b5ff96fa | 1260 | status = vl53l0x_read_word(dev, |
johnAlexander | 14:8320b5ff96fa | 1261 | VL53L0X_REG_PRE_RANGE_CONFIG_TIMEOUT_MACROP_HI, |
johnAlexander | 14:8320b5ff96fa | 1262 | &pre_range_encoded_time_out); |
johnAlexander | 14:8320b5ff96fa | 1263 | pre_range_time_out_m_clks = vl53l0x_decode_timeout( |
johnAlexander | 14:8320b5ff96fa | 1264 | pre_range_encoded_time_out); |
johnAlexander | 14:8320b5ff96fa | 1265 | } |
johnAlexander | 14:8320b5ff96fa | 1266 | } |
johnAlexander | 14:8320b5ff96fa | 1267 | |
johnAlexander | 14:8320b5ff96fa | 1268 | if (status == VL53L0X_ERROR_NONE) { |
johnAlexander | 14:8320b5ff96fa | 1269 | /* Retrieve FINAL-RANGE VCSEL Period */ |
johnAlexander | 14:8320b5ff96fa | 1270 | status = vl53l0x_get_vcsel_pulse_period(dev, |
johnAlexander | 14:8320b5ff96fa | 1271 | VL53L0X_VCSEL_PERIOD_FINAL_RANGE, |
johnAlexander | 14:8320b5ff96fa | 1272 | ¤t_vcsel_pulse_period_p_clk); |
johnAlexander | 14:8320b5ff96fa | 1273 | } |
johnAlexander | 14:8320b5ff96fa | 1274 | |
johnAlexander | 14:8320b5ff96fa | 1275 | /* Retrieve FINAL-RANGE Timeout in Macro periods (MCLKS) */ |
johnAlexander | 14:8320b5ff96fa | 1276 | if (status == VL53L0X_ERROR_NONE) { |
johnAlexander | 14:8320b5ff96fa | 1277 | status = vl53l0x_read_word(dev, |
johnAlexander | 14:8320b5ff96fa | 1278 | VL53L0X_REG_FINAL_RANGE_CONFIG_TIMEOUT_MACROP_HI, |
johnAlexander | 14:8320b5ff96fa | 1279 | &final_range_encoded_time_out); |
johnAlexander | 14:8320b5ff96fa | 1280 | final_range_time_out_m_clks = vl53l0x_decode_timeout( |
johnAlexander | 14:8320b5ff96fa | 1281 | final_range_encoded_time_out); |
johnAlexander | 14:8320b5ff96fa | 1282 | } |
johnAlexander | 14:8320b5ff96fa | 1283 | |
johnAlexander | 14:8320b5ff96fa | 1284 | final_range_time_out_m_clks -= pre_range_time_out_m_clks; |
johnAlexander | 14:8320b5ff96fa | 1285 | timeout_micro_seconds = vl53l0x_calc_timeout_us(dev, |
johnAlexander | 14:8320b5ff96fa | 1286 | final_range_time_out_m_clks, |
johnAlexander | 14:8320b5ff96fa | 1287 | current_vcsel_pulse_period_p_clk); |
johnAlexander | 14:8320b5ff96fa | 1288 | } |
johnAlexander | 14:8320b5ff96fa | 1289 | |
johnAlexander | 14:8320b5ff96fa | 1290 | *p_time_out_micro_secs = timeout_micro_seconds; |
johnAlexander | 14:8320b5ff96fa | 1291 | |
johnAlexander | 14:8320b5ff96fa | 1292 | return status; |
johnAlexander | 0:c523920bcc09 | 1293 | } |
johnAlexander | 0:c523920bcc09 | 1294 | |
johnAlexander | 14:8320b5ff96fa | 1295 | VL53L0X_Error VL53L0X::wrapped_vl53l0x_get_measurement_timing_budget_micro_seconds(VL53L0X_DEV dev, |
johnAlexander | 14:8320b5ff96fa | 1296 | uint32_t *p_measurement_timing_budget_micro_seconds) |
johnAlexander | 0:c523920bcc09 | 1297 | { |
johnAlexander | 14:8320b5ff96fa | 1298 | VL53L0X_Error status = VL53L0X_ERROR_NONE; |
johnAlexander | 14:8320b5ff96fa | 1299 | VL53L0X_SchedulerSequenceSteps_t scheduler_sequence_steps; |
johnAlexander | 14:8320b5ff96fa | 1300 | uint32_t final_range_timeout_micro_seconds; |
johnAlexander | 14:8320b5ff96fa | 1301 | uint32_t msrc_dcc_tcc_timeout_micro_seconds = 2000; |
johnAlexander | 14:8320b5ff96fa | 1302 | uint32_t start_overhead_micro_seconds = 1910; |
johnAlexander | 14:8320b5ff96fa | 1303 | uint32_t end_overhead_micro_seconds = 960; |
johnAlexander | 14:8320b5ff96fa | 1304 | uint32_t msrc_overhead_micro_seconds = 660; |
johnAlexander | 14:8320b5ff96fa | 1305 | uint32_t tcc_overhead_micro_seconds = 590; |
johnAlexander | 14:8320b5ff96fa | 1306 | uint32_t dss_overhead_micro_seconds = 690; |
johnAlexander | 14:8320b5ff96fa | 1307 | uint32_t pre_range_overhead_micro_seconds = 660; |
johnAlexander | 14:8320b5ff96fa | 1308 | uint32_t final_range_overhead_micro_seconds = 550; |
johnAlexander | 14:8320b5ff96fa | 1309 | uint32_t pre_range_timeout_micro_seconds = 0; |
johnAlexander | 14:8320b5ff96fa | 1310 | |
johnAlexander | 14:8320b5ff96fa | 1311 | LOG_FUNCTION_START(""); |
johnAlexander | 14:8320b5ff96fa | 1312 | |
johnAlexander | 14:8320b5ff96fa | 1313 | /* Start and end overhead times always present */ |
johnAlexander | 14:8320b5ff96fa | 1314 | *p_measurement_timing_budget_micro_seconds |
johnAlexander | 14:8320b5ff96fa | 1315 | = start_overhead_micro_seconds + end_overhead_micro_seconds; |
johnAlexander | 14:8320b5ff96fa | 1316 | |
johnAlexander | 14:8320b5ff96fa | 1317 | status = vl53l0x_get_sequence_step_enables(dev, &scheduler_sequence_steps); |
johnAlexander | 14:8320b5ff96fa | 1318 | |
johnAlexander | 14:8320b5ff96fa | 1319 | if (status != VL53L0X_ERROR_NONE) { |
johnAlexander | 14:8320b5ff96fa | 1320 | LOG_FUNCTION_END(status); |
johnAlexander | 14:8320b5ff96fa | 1321 | return status; |
johnAlexander | 14:8320b5ff96fa | 1322 | } |
johnAlexander | 14:8320b5ff96fa | 1323 | |
johnAlexander | 14:8320b5ff96fa | 1324 | |
johnAlexander | 14:8320b5ff96fa | 1325 | if (scheduler_sequence_steps.TccOn || |
johnAlexander | 14:8320b5ff96fa | 1326 | scheduler_sequence_steps.MsrcOn || |
johnAlexander | 14:8320b5ff96fa | 1327 | scheduler_sequence_steps.DssOn) { |
johnAlexander | 14:8320b5ff96fa | 1328 | |
johnAlexander | 14:8320b5ff96fa | 1329 | status = get_sequence_step_timeout(dev, |
johnAlexander | 14:8320b5ff96fa | 1330 | VL53L0X_SEQUENCESTEP_MSRC, |
johnAlexander | 14:8320b5ff96fa | 1331 | &msrc_dcc_tcc_timeout_micro_seconds); |
johnAlexander | 14:8320b5ff96fa | 1332 | |
johnAlexander | 14:8320b5ff96fa | 1333 | if (status == VL53L0X_ERROR_NONE) { |
johnAlexander | 14:8320b5ff96fa | 1334 | if (scheduler_sequence_steps.TccOn) { |
johnAlexander | 14:8320b5ff96fa | 1335 | *p_measurement_timing_budget_micro_seconds += |
johnAlexander | 14:8320b5ff96fa | 1336 | msrc_dcc_tcc_timeout_micro_seconds + |
johnAlexander | 14:8320b5ff96fa | 1337 | tcc_overhead_micro_seconds; |
johnAlexander | 14:8320b5ff96fa | 1338 | } |
johnAlexander | 14:8320b5ff96fa | 1339 | |
johnAlexander | 14:8320b5ff96fa | 1340 | if (scheduler_sequence_steps.DssOn) { |
johnAlexander | 14:8320b5ff96fa | 1341 | *p_measurement_timing_budget_micro_seconds += |
johnAlexander | 14:8320b5ff96fa | 1342 | 2 * (msrc_dcc_tcc_timeout_micro_seconds + |
johnAlexander | 14:8320b5ff96fa | 1343 | dss_overhead_micro_seconds); |
johnAlexander | 14:8320b5ff96fa | 1344 | } else if (scheduler_sequence_steps.MsrcOn) { |
johnAlexander | 14:8320b5ff96fa | 1345 | *p_measurement_timing_budget_micro_seconds += |
johnAlexander | 14:8320b5ff96fa | 1346 | msrc_dcc_tcc_timeout_micro_seconds + |
johnAlexander | 14:8320b5ff96fa | 1347 | msrc_overhead_micro_seconds; |
johnAlexander | 14:8320b5ff96fa | 1348 | } |
johnAlexander | 14:8320b5ff96fa | 1349 | } |
johnAlexander | 14:8320b5ff96fa | 1350 | } |
johnAlexander | 14:8320b5ff96fa | 1351 | |
johnAlexander | 14:8320b5ff96fa | 1352 | if (status == VL53L0X_ERROR_NONE) { |
johnAlexander | 14:8320b5ff96fa | 1353 | if (scheduler_sequence_steps.PreRangeOn) { |
johnAlexander | 14:8320b5ff96fa | 1354 | status = get_sequence_step_timeout(dev, |
johnAlexander | 14:8320b5ff96fa | 1355 | VL53L0X_SEQUENCESTEP_PRE_RANGE, |
johnAlexander | 14:8320b5ff96fa | 1356 | &pre_range_timeout_micro_seconds); |
johnAlexander | 14:8320b5ff96fa | 1357 | *p_measurement_timing_budget_micro_seconds += |
johnAlexander | 14:8320b5ff96fa | 1358 | pre_range_timeout_micro_seconds + |
johnAlexander | 14:8320b5ff96fa | 1359 | pre_range_overhead_micro_seconds; |
johnAlexander | 14:8320b5ff96fa | 1360 | } |
johnAlexander | 14:8320b5ff96fa | 1361 | } |
johnAlexander | 14:8320b5ff96fa | 1362 | |
johnAlexander | 14:8320b5ff96fa | 1363 | if (status == VL53L0X_ERROR_NONE) { |
johnAlexander | 14:8320b5ff96fa | 1364 | if (scheduler_sequence_steps.FinalRangeOn) { |
johnAlexander | 14:8320b5ff96fa | 1365 | status = get_sequence_step_timeout(dev, |
johnAlexander | 14:8320b5ff96fa | 1366 | VL53L0X_SEQUENCESTEP_FINAL_RANGE, |
johnAlexander | 14:8320b5ff96fa | 1367 | &final_range_timeout_micro_seconds); |
johnAlexander | 14:8320b5ff96fa | 1368 | *p_measurement_timing_budget_micro_seconds += |
johnAlexander | 14:8320b5ff96fa | 1369 | (final_range_timeout_micro_seconds + |
johnAlexander | 14:8320b5ff96fa | 1370 | final_range_overhead_micro_seconds); |
johnAlexander | 14:8320b5ff96fa | 1371 | } |
johnAlexander | 14:8320b5ff96fa | 1372 | } |
johnAlexander | 14:8320b5ff96fa | 1373 | |
johnAlexander | 14:8320b5ff96fa | 1374 | if (status == VL53L0X_ERROR_NONE) { |
johnAlexander | 14:8320b5ff96fa | 1375 | VL53L0X_SETPARAMETERFIELD(dev, |
johnAlexander | 14:8320b5ff96fa | 1376 | MeasurementTimingBudgetMicroSeconds, |
johnAlexander | 14:8320b5ff96fa | 1377 | *p_measurement_timing_budget_micro_seconds); |
johnAlexander | 14:8320b5ff96fa | 1378 | } |
johnAlexander | 14:8320b5ff96fa | 1379 | |
johnAlexander | 14:8320b5ff96fa | 1380 | LOG_FUNCTION_END(status); |
johnAlexander | 14:8320b5ff96fa | 1381 | return status; |
johnAlexander | 0:c523920bcc09 | 1382 | } |
johnAlexander | 0:c523920bcc09 | 1383 | |
johnAlexander | 14:8320b5ff96fa | 1384 | VL53L0X_Error VL53L0X::vl53l0x_get_measurement_timing_budget_micro_seconds(VL53L0X_DEV dev, |
johnAlexander | 14:8320b5ff96fa | 1385 | uint32_t *p_measurement_timing_budget_micro_seconds) |
johnAlexander | 0:c523920bcc09 | 1386 | { |
johnAlexander | 14:8320b5ff96fa | 1387 | VL53L0X_Error status = VL53L0X_ERROR_NONE; |
johnAlexander | 14:8320b5ff96fa | 1388 | LOG_FUNCTION_START(""); |
johnAlexander | 14:8320b5ff96fa | 1389 | |
johnAlexander | 14:8320b5ff96fa | 1390 | status = wrapped_vl53l0x_get_measurement_timing_budget_micro_seconds(dev, |
johnAlexander | 14:8320b5ff96fa | 1391 | p_measurement_timing_budget_micro_seconds); |
johnAlexander | 14:8320b5ff96fa | 1392 | |
johnAlexander | 14:8320b5ff96fa | 1393 | LOG_FUNCTION_END(status); |
johnAlexander | 14:8320b5ff96fa | 1394 | return status; |
johnAlexander | 0:c523920bcc09 | 1395 | } |
johnAlexander | 0:c523920bcc09 | 1396 | |
johnAlexander | 14:8320b5ff96fa | 1397 | VL53L0X_Error VL53L0X::vl53l0x_get_device_parameters(VL53L0X_DEV dev, |
johnAlexander | 14:8320b5ff96fa | 1398 | VL53L0X_DeviceParameters_t *p_device_parameters) |
johnAlexander | 0:c523920bcc09 | 1399 | { |
johnAlexander | 14:8320b5ff96fa | 1400 | VL53L0X_Error status = VL53L0X_ERROR_NONE; |
johnAlexander | 14:8320b5ff96fa | 1401 | int i; |
johnAlexander | 14:8320b5ff96fa | 1402 | |
johnAlexander | 14:8320b5ff96fa | 1403 | LOG_FUNCTION_START(""); |
johnAlexander | 14:8320b5ff96fa | 1404 | |
johnAlexander | 14:8320b5ff96fa | 1405 | status = vl53l0x_get_device_mode(dev, &(p_device_parameters->DeviceMode)); |
johnAlexander | 14:8320b5ff96fa | 1406 | |
johnAlexander | 14:8320b5ff96fa | 1407 | if (status == VL53L0X_ERROR_NONE) |
johnAlexander | 14:8320b5ff96fa | 1408 | status = vl53l0x_get_inter_measurement_period_milli_seconds(dev, |
johnAlexander | 14:8320b5ff96fa | 1409 | &(p_device_parameters->InterMeasurementPeriodMilliSeconds)); |
johnAlexander | 14:8320b5ff96fa | 1410 | |
johnAlexander | 14:8320b5ff96fa | 1411 | |
johnAlexander | 14:8320b5ff96fa | 1412 | if (status == VL53L0X_ERROR_NONE) |
johnAlexander | 14:8320b5ff96fa | 1413 | p_device_parameters->XTalkCompensationEnable = 0; |
johnAlexander | 14:8320b5ff96fa | 1414 | |
johnAlexander | 14:8320b5ff96fa | 1415 | if (status == VL53L0X_ERROR_NONE) |
johnAlexander | 14:8320b5ff96fa | 1416 | status = vl53l0x_get_x_talk_compensation_rate_mega_cps(dev, |
johnAlexander | 14:8320b5ff96fa | 1417 | &(p_device_parameters->XTalkCompensationRateMegaCps)); |
johnAlexander | 14:8320b5ff96fa | 1418 | |
johnAlexander | 14:8320b5ff96fa | 1419 | |
johnAlexander | 14:8320b5ff96fa | 1420 | if (status == VL53L0X_ERROR_NONE) |
johnAlexander | 14:8320b5ff96fa | 1421 | status = vl53l0x_get_offset_calibration_data_micro_meter(dev, |
johnAlexander | 14:8320b5ff96fa | 1422 | &(p_device_parameters->RangeOffsetMicroMeters)); |
johnAlexander | 14:8320b5ff96fa | 1423 | |
johnAlexander | 14:8320b5ff96fa | 1424 | |
johnAlexander | 14:8320b5ff96fa | 1425 | if (status == VL53L0X_ERROR_NONE) { |
johnAlexander | 14:8320b5ff96fa | 1426 | for (i = 0; i < VL53L0X_CHECKENABLE_NUMBER_OF_CHECKS; i++) { |
johnAlexander | 14:8320b5ff96fa | 1427 | /* get first the values, then the enables. |
johnAlexander | 14:8320b5ff96fa | 1428 | * VL53L0X_GetLimitCheckValue will modify the enable |
johnAlexander | 14:8320b5ff96fa | 1429 | * flags |
johnAlexander | 14:8320b5ff96fa | 1430 | */ |
johnAlexander | 14:8320b5ff96fa | 1431 | if (status == VL53L0X_ERROR_NONE) { |
johnAlexander | 14:8320b5ff96fa | 1432 | status |= vl53l0x_get_limit_check_value(dev, i, |
johnAlexander | 14:8320b5ff96fa | 1433 | &(p_device_parameters->LimitChecksValue[i])); |
johnAlexander | 14:8320b5ff96fa | 1434 | } else { |
johnAlexander | 14:8320b5ff96fa | 1435 | break; |
johnAlexander | 14:8320b5ff96fa | 1436 | } |
johnAlexander | 14:8320b5ff96fa | 1437 | if (status == VL53L0X_ERROR_NONE) { |
johnAlexander | 14:8320b5ff96fa | 1438 | status |= vl53l0x_get_limit_check_enable(dev, i, |
johnAlexander | 14:8320b5ff96fa | 1439 | &(p_device_parameters->LimitChecksEnable[i])); |
johnAlexander | 14:8320b5ff96fa | 1440 | } else { |
johnAlexander | 14:8320b5ff96fa | 1441 | break; |
johnAlexander | 14:8320b5ff96fa | 1442 | } |
johnAlexander | 14:8320b5ff96fa | 1443 | } |
johnAlexander | 14:8320b5ff96fa | 1444 | } |
johnAlexander | 14:8320b5ff96fa | 1445 | |
johnAlexander | 14:8320b5ff96fa | 1446 | if (status == VL53L0X_ERROR_NONE) { |
johnAlexander | 14:8320b5ff96fa | 1447 | status = vl53l0x_get_wrap_around_check_enable(dev, |
johnAlexander | 14:8320b5ff96fa | 1448 | &(p_device_parameters->WrapAroundCheckEnable)); |
johnAlexander | 14:8320b5ff96fa | 1449 | } |
johnAlexander | 14:8320b5ff96fa | 1450 | |
johnAlexander | 14:8320b5ff96fa | 1451 | /* Need to be done at the end as it uses VCSELPulsePeriod */ |
johnAlexander | 14:8320b5ff96fa | 1452 | if (status == VL53L0X_ERROR_NONE) { |
johnAlexander | 14:8320b5ff96fa | 1453 | status = vl53l0x_get_measurement_timing_budget_micro_seconds(dev, |
johnAlexander | 14:8320b5ff96fa | 1454 | &(p_device_parameters->MeasurementTimingBudgetMicroSeconds)); |
johnAlexander | 14:8320b5ff96fa | 1455 | } |
johnAlexander | 14:8320b5ff96fa | 1456 | |
johnAlexander | 14:8320b5ff96fa | 1457 | LOG_FUNCTION_END(status); |
johnAlexander | 14:8320b5ff96fa | 1458 | return status; |
johnAlexander | 0:c523920bcc09 | 1459 | } |
johnAlexander | 0:c523920bcc09 | 1460 | |
johnAlexander | 14:8320b5ff96fa | 1461 | VL53L0X_Error VL53L0X::vl53l0x_set_limit_check_value(VL53L0X_DEV dev, uint16_t limit_check_id, |
johnAlexander | 14:8320b5ff96fa | 1462 | FixPoint1616_t limit_check_value) |
johnAlexander | 0:c523920bcc09 | 1463 | { |
johnAlexander | 14:8320b5ff96fa | 1464 | VL53L0X_Error status = VL53L0X_ERROR_NONE; |
johnAlexander | 14:8320b5ff96fa | 1465 | uint8_t temp8; |
johnAlexander | 14:8320b5ff96fa | 1466 | |
johnAlexander | 14:8320b5ff96fa | 1467 | LOG_FUNCTION_START(""); |
johnAlexander | 14:8320b5ff96fa | 1468 | |
johnAlexander | 14:8320b5ff96fa | 1469 | VL53L0X_GETARRAYPARAMETERFIELD(dev, LimitChecksEnable, limit_check_id, |
johnAlexander | 14:8320b5ff96fa | 1470 | temp8); |
johnAlexander | 14:8320b5ff96fa | 1471 | |
johnAlexander | 14:8320b5ff96fa | 1472 | if (temp8 == 0) { /* disabled write only internal value */ |
johnAlexander | 14:8320b5ff96fa | 1473 | VL53L0X_SETARRAYPARAMETERFIELD(dev, LimitChecksValue, |
johnAlexander | 14:8320b5ff96fa | 1474 | limit_check_id, limit_check_value); |
johnAlexander | 14:8320b5ff96fa | 1475 | } else { |
johnAlexander | 14:8320b5ff96fa | 1476 | |
johnAlexander | 14:8320b5ff96fa | 1477 | switch (limit_check_id) { |
johnAlexander | 14:8320b5ff96fa | 1478 | |
johnAlexander | 14:8320b5ff96fa | 1479 | case VL53L0X_CHECKENABLE_SIGMA_FINAL_RANGE: |
johnAlexander | 14:8320b5ff96fa | 1480 | /* internal computation: */ |
johnAlexander | 14:8320b5ff96fa | 1481 | VL53L0X_SETARRAYPARAMETERFIELD(dev, LimitChecksValue, |
johnAlexander | 14:8320b5ff96fa | 1482 | VL53L0X_CHECKENABLE_SIGMA_FINAL_RANGE, |
johnAlexander | 14:8320b5ff96fa | 1483 | limit_check_value); |
johnAlexander | 14:8320b5ff96fa | 1484 | break; |
johnAlexander | 14:8320b5ff96fa | 1485 | |
johnAlexander | 14:8320b5ff96fa | 1486 | case VL53L0X_CHECKENABLE_SIGNAL_RATE_FINAL_RANGE: |
johnAlexander | 14:8320b5ff96fa | 1487 | |
johnAlexander | 14:8320b5ff96fa | 1488 | status = vl53l0x_write_word(dev, |
johnAlexander | 14:8320b5ff96fa | 1489 | VL53L0X_REG_FINAL_RANGE_CONFIG_MIN_COUNT_RATE_RTN_LIMIT, |
johnAlexander | 14:8320b5ff96fa | 1490 | VL53L0X_FIXPOINT1616TOFIXPOINT97( |
johnAlexander | 14:8320b5ff96fa | 1491 | limit_check_value)); |
johnAlexander | 14:8320b5ff96fa | 1492 | |
johnAlexander | 14:8320b5ff96fa | 1493 | break; |
johnAlexander | 14:8320b5ff96fa | 1494 | |
johnAlexander | 14:8320b5ff96fa | 1495 | case VL53L0X_CHECKENABLE_SIGNAL_REF_CLIP: |
johnAlexander | 14:8320b5ff96fa | 1496 | |
johnAlexander | 14:8320b5ff96fa | 1497 | /* internal computation: */ |
johnAlexander | 14:8320b5ff96fa | 1498 | VL53L0X_SETARRAYPARAMETERFIELD(dev, LimitChecksValue, |
johnAlexander | 14:8320b5ff96fa | 1499 | VL53L0X_CHECKENABLE_SIGNAL_REF_CLIP, |
johnAlexander | 14:8320b5ff96fa | 1500 | limit_check_value); |
johnAlexander | 14:8320b5ff96fa | 1501 | |
johnAlexander | 14:8320b5ff96fa | 1502 | break; |
johnAlexander | 14:8320b5ff96fa | 1503 | |
johnAlexander | 14:8320b5ff96fa | 1504 | case VL53L0X_CHECKENABLE_RANGE_IGNORE_THRESHOLD: |
johnAlexander | 14:8320b5ff96fa | 1505 | |
johnAlexander | 14:8320b5ff96fa | 1506 | /* internal computation: */ |
johnAlexander | 14:8320b5ff96fa | 1507 | VL53L0X_SETARRAYPARAMETERFIELD(dev, LimitChecksValue, |
johnAlexander | 14:8320b5ff96fa | 1508 | VL53L0X_CHECKENABLE_RANGE_IGNORE_THRESHOLD, |
johnAlexander | 14:8320b5ff96fa | 1509 | limit_check_value); |
johnAlexander | 14:8320b5ff96fa | 1510 | |
johnAlexander | 14:8320b5ff96fa | 1511 | break; |
johnAlexander | 14:8320b5ff96fa | 1512 | |
johnAlexander | 14:8320b5ff96fa | 1513 | case VL53L0X_CHECKENABLE_SIGNAL_RATE_MSRC: |
johnAlexander | 14:8320b5ff96fa | 1514 | case VL53L0X_CHECKENABLE_SIGNAL_RATE_PRE_RANGE: |
johnAlexander | 14:8320b5ff96fa | 1515 | |
johnAlexander | 14:8320b5ff96fa | 1516 | status = vl53l0x_write_word(dev, |
johnAlexander | 14:8320b5ff96fa | 1517 | VL53L0X_REG_PRE_RANGE_MIN_COUNT_RATE_RTN_LIMIT, |
johnAlexander | 14:8320b5ff96fa | 1518 | VL53L0X_FIXPOINT1616TOFIXPOINT97( |
johnAlexander | 14:8320b5ff96fa | 1519 | limit_check_value)); |
johnAlexander | 14:8320b5ff96fa | 1520 | |
johnAlexander | 14:8320b5ff96fa | 1521 | break; |
johnAlexander | 14:8320b5ff96fa | 1522 | |
johnAlexander | 14:8320b5ff96fa | 1523 | default: |
johnAlexander | 14:8320b5ff96fa | 1524 | status = VL53L0X_ERROR_INVALID_PARAMS; |
johnAlexander | 14:8320b5ff96fa | 1525 | |
johnAlexander | 14:8320b5ff96fa | 1526 | } |
johnAlexander | 14:8320b5ff96fa | 1527 | |
johnAlexander | 14:8320b5ff96fa | 1528 | if (status == VL53L0X_ERROR_NONE) { |
johnAlexander | 14:8320b5ff96fa | 1529 | VL53L0X_SETARRAYPARAMETERFIELD(dev, LimitChecksValue, |
johnAlexander | 14:8320b5ff96fa | 1530 | limit_check_id, limit_check_value); |
johnAlexander | 14:8320b5ff96fa | 1531 | } |
johnAlexander | 14:8320b5ff96fa | 1532 | } |
johnAlexander | 14:8320b5ff96fa | 1533 | |
johnAlexander | 14:8320b5ff96fa | 1534 | LOG_FUNCTION_END(status); |
johnAlexander | 14:8320b5ff96fa | 1535 | return status; |
johnAlexander | 0:c523920bcc09 | 1536 | } |
johnAlexander | 0:c523920bcc09 | 1537 | |
johnAlexander | 14:8320b5ff96fa | 1538 | VL53L0X_Error VL53L0X::vl53l0x_data_init(VL53L0X_DEV dev) |
johnAlexander | 0:c523920bcc09 | 1539 | { |
johnAlexander | 14:8320b5ff96fa | 1540 | VL53L0X_Error status = VL53L0X_ERROR_NONE; |
johnAlexander | 14:8320b5ff96fa | 1541 | VL53L0X_DeviceParameters_t CurrentParameters; |
johnAlexander | 14:8320b5ff96fa | 1542 | int i; |
johnAlexander | 14:8320b5ff96fa | 1543 | uint8_t StopVariable; |
johnAlexander | 14:8320b5ff96fa | 1544 | |
johnAlexander | 14:8320b5ff96fa | 1545 | LOG_FUNCTION_START(""); |
johnAlexander | 14:8320b5ff96fa | 1546 | |
johnAlexander | 14:8320b5ff96fa | 1547 | /* by default the I2C is running at 1V8 if you want to change it you |
johnAlexander | 14:8320b5ff96fa | 1548 | * need to include this define at compilation level. */ |
johnAlexander | 0:c523920bcc09 | 1549 | #ifdef USE_I2C_2V8 |
johnAlexander | 14:8320b5ff96fa | 1550 | Status = VL53L0X_UpdateByte(Dev, |
johnAlexander | 14:8320b5ff96fa | 1551 | VL53L0X_REG_VHV_CONFIG_PAD_SCL_SDA__EXTSUP_HV, |
johnAlexander | 14:8320b5ff96fa | 1552 | 0xFE, |
johnAlexander | 14:8320b5ff96fa | 1553 | 0x01); |
johnAlexander | 0:c523920bcc09 | 1554 | #endif |
johnAlexander | 0:c523920bcc09 | 1555 | |
johnAlexander | 14:8320b5ff96fa | 1556 | /* Set I2C standard mode */ |
johnAlexander | 14:8320b5ff96fa | 1557 | if (status == VL53L0X_ERROR_NONE) |
johnAlexander | 14:8320b5ff96fa | 1558 | status = vl53l0x_write_byte(dev, 0x88, 0x00); |
johnAlexander | 14:8320b5ff96fa | 1559 | |
johnAlexander | 14:8320b5ff96fa | 1560 | VL53L0X_SETDEVICESPECIFICPARAMETER(dev, ReadDataFromDeviceDone, 0); |
johnAlexander | 0:c523920bcc09 | 1561 | |
johnAlexander | 0:c523920bcc09 | 1562 | #ifdef USE_IQC_STATION |
johnAlexander | 14:8320b5ff96fa | 1563 | if (Status == VL53L0X_ERROR_NONE) |
johnAlexander | 14:8320b5ff96fa | 1564 | Status = VL53L0X_apply_offset_adjustment(Dev); |
johnAlexander | 0:c523920bcc09 | 1565 | #endif |
johnAlexander | 0:c523920bcc09 | 1566 | |
johnAlexander | 14:8320b5ff96fa | 1567 | /* Default value is 1000 for Linearity Corrective Gain */ |
johnAlexander | 14:8320b5ff96fa | 1568 | PALDevDataSet(dev, LinearityCorrectiveGain, 1000); |
johnAlexander | 14:8320b5ff96fa | 1569 | |
johnAlexander | 14:8320b5ff96fa | 1570 | /* Dmax default Parameter */ |
johnAlexander | 14:8320b5ff96fa | 1571 | PALDevDataSet(dev, DmaxCalRangeMilliMeter, 400); |
johnAlexander | 14:8320b5ff96fa | 1572 | PALDevDataSet(dev, DmaxCalSignalRateRtnMegaCps, |
johnAlexander | 14:8320b5ff96fa | 1573 | (FixPoint1616_t)((0x00016B85))); /* 1.42 No Cover Glass*/ |
johnAlexander | 14:8320b5ff96fa | 1574 | |
johnAlexander | 14:8320b5ff96fa | 1575 | /* Set Default static parameters |
johnAlexander | 14:8320b5ff96fa | 1576 | *set first temporary values 9.44MHz * 65536 = 618660 */ |
johnAlexander | 14:8320b5ff96fa | 1577 | VL53L0X_SETDEVICESPECIFICPARAMETER(dev, OscFrequencyMHz, 618660); |
johnAlexander | 14:8320b5ff96fa | 1578 | |
johnAlexander | 14:8320b5ff96fa | 1579 | /* Set Default XTalkCompensationRateMegaCps to 0 */ |
johnAlexander | 14:8320b5ff96fa | 1580 | VL53L0X_SETPARAMETERFIELD(dev, XTalkCompensationRateMegaCps, 0); |
johnAlexander | 14:8320b5ff96fa | 1581 | |
johnAlexander | 14:8320b5ff96fa | 1582 | /* Get default parameters */ |
johnAlexander | 14:8320b5ff96fa | 1583 | status = vl53l0x_get_device_parameters(dev, &CurrentParameters); |
johnAlexander | 14:8320b5ff96fa | 1584 | if (status == VL53L0X_ERROR_NONE) { |
johnAlexander | 14:8320b5ff96fa | 1585 | /* initialize PAL values */ |
johnAlexander | 14:8320b5ff96fa | 1586 | CurrentParameters.DeviceMode = VL53L0X_DEVICEMODE_SINGLE_RANGING; |
johnAlexander | 14:8320b5ff96fa | 1587 | CurrentParameters.HistogramMode = VL53L0X_HISTOGRAMMODE_DISABLED; |
johnAlexander | 14:8320b5ff96fa | 1588 | PALDevDataSet(dev, CurrentParameters, CurrentParameters); |
johnAlexander | 14:8320b5ff96fa | 1589 | } |
johnAlexander | 14:8320b5ff96fa | 1590 | |
johnAlexander | 14:8320b5ff96fa | 1591 | /* Sigma estimator variable */ |
johnAlexander | 14:8320b5ff96fa | 1592 | PALDevDataSet(dev, SigmaEstRefArray, 100); |
johnAlexander | 14:8320b5ff96fa | 1593 | PALDevDataSet(dev, SigmaEstEffPulseWidth, 900); |
johnAlexander | 14:8320b5ff96fa | 1594 | PALDevDataSet(dev, SigmaEstEffAmbWidth, 500); |
johnAlexander | 14:8320b5ff96fa | 1595 | PALDevDataSet(dev, targetRefRate, 0x0A00); /* 20 MCPS in 9:7 format */ |
johnAlexander | 14:8320b5ff96fa | 1596 | |
johnAlexander | 14:8320b5ff96fa | 1597 | /* Use internal default settings */ |
johnAlexander | 14:8320b5ff96fa | 1598 | PALDevDataSet(dev, UseInternalTuningSettings, 1); |
johnAlexander | 14:8320b5ff96fa | 1599 | |
johnAlexander | 14:8320b5ff96fa | 1600 | status |= vl53l0x_write_byte(dev, 0x80, 0x01); |
johnAlexander | 14:8320b5ff96fa | 1601 | status |= vl53l0x_write_byte(dev, 0xFF, 0x01); |
johnAlexander | 14:8320b5ff96fa | 1602 | status |= vl53l0x_write_byte(dev, 0x00, 0x00); |
johnAlexander | 14:8320b5ff96fa | 1603 | status |= vl53l0x_read_byte(dev, 0x91, &StopVariable); |
johnAlexander | 14:8320b5ff96fa | 1604 | PALDevDataSet(dev, StopVariable, StopVariable); |
johnAlexander | 14:8320b5ff96fa | 1605 | status |= vl53l0x_write_byte(dev, 0x00, 0x01); |
johnAlexander | 14:8320b5ff96fa | 1606 | status |= vl53l0x_write_byte(dev, 0xFF, 0x00); |
johnAlexander | 14:8320b5ff96fa | 1607 | status |= vl53l0x_write_byte(dev, 0x80, 0x00); |
johnAlexander | 14:8320b5ff96fa | 1608 | |
johnAlexander | 14:8320b5ff96fa | 1609 | /* Enable all check */ |
johnAlexander | 14:8320b5ff96fa | 1610 | for (i = 0; i < VL53L0X_CHECKENABLE_NUMBER_OF_CHECKS; i++) { |
johnAlexander | 14:8320b5ff96fa | 1611 | if (status == VL53L0X_ERROR_NONE) |
johnAlexander | 14:8320b5ff96fa | 1612 | status |= vl53l0x_set_limit_check_enable(dev, i, 1); |
johnAlexander | 14:8320b5ff96fa | 1613 | else |
johnAlexander | 14:8320b5ff96fa | 1614 | break; |
johnAlexander | 14:8320b5ff96fa | 1615 | |
johnAlexander | 14:8320b5ff96fa | 1616 | } |
johnAlexander | 14:8320b5ff96fa | 1617 | |
johnAlexander | 14:8320b5ff96fa | 1618 | /* Disable the following checks */ |
johnAlexander | 14:8320b5ff96fa | 1619 | if (status == VL53L0X_ERROR_NONE) |
johnAlexander | 14:8320b5ff96fa | 1620 | status = vl53l0x_set_limit_check_enable(dev, |
johnAlexander | 14:8320b5ff96fa | 1621 | VL53L0X_CHECKENABLE_SIGNAL_REF_CLIP, 0); |
johnAlexander | 14:8320b5ff96fa | 1622 | |
johnAlexander | 14:8320b5ff96fa | 1623 | if (status == VL53L0X_ERROR_NONE) |
johnAlexander | 14:8320b5ff96fa | 1624 | status = vl53l0x_set_limit_check_enable(dev, |
johnAlexander | 14:8320b5ff96fa | 1625 | VL53L0X_CHECKENABLE_RANGE_IGNORE_THRESHOLD, 0); |
johnAlexander | 14:8320b5ff96fa | 1626 | |
johnAlexander | 14:8320b5ff96fa | 1627 | if (status == VL53L0X_ERROR_NONE) |
johnAlexander | 14:8320b5ff96fa | 1628 | status = vl53l0x_set_limit_check_enable(dev, |
johnAlexander | 14:8320b5ff96fa | 1629 | VL53L0X_CHECKENABLE_SIGNAL_RATE_MSRC, 0); |
johnAlexander | 14:8320b5ff96fa | 1630 | |
johnAlexander | 14:8320b5ff96fa | 1631 | if (status == VL53L0X_ERROR_NONE) |
johnAlexander | 14:8320b5ff96fa | 1632 | status = vl53l0x_set_limit_check_enable(dev, |
johnAlexander | 14:8320b5ff96fa | 1633 | VL53L0X_CHECKENABLE_SIGNAL_RATE_PRE_RANGE, 0); |
johnAlexander | 14:8320b5ff96fa | 1634 | |
johnAlexander | 14:8320b5ff96fa | 1635 | /* Limit default values */ |
johnAlexander | 14:8320b5ff96fa | 1636 | if (status == VL53L0X_ERROR_NONE) { |
johnAlexander | 14:8320b5ff96fa | 1637 | status = vl53l0x_set_limit_check_value(dev, |
johnAlexander | 14:8320b5ff96fa | 1638 | VL53L0X_CHECKENABLE_SIGMA_FINAL_RANGE, |
johnAlexander | 14:8320b5ff96fa | 1639 | (FixPoint1616_t)(18 * 65536)); |
johnAlexander | 14:8320b5ff96fa | 1640 | } |
johnAlexander | 14:8320b5ff96fa | 1641 | if (status == VL53L0X_ERROR_NONE) { |
johnAlexander | 14:8320b5ff96fa | 1642 | status = vl53l0x_set_limit_check_value(dev, |
johnAlexander | 14:8320b5ff96fa | 1643 | VL53L0X_CHECKENABLE_SIGNAL_RATE_FINAL_RANGE, |
johnAlexander | 14:8320b5ff96fa | 1644 | (FixPoint1616_t)(25 * 65536 / 100)); |
johnAlexander | 14:8320b5ff96fa | 1645 | /* 0.25 * 65536 */ |
johnAlexander | 14:8320b5ff96fa | 1646 | } |
johnAlexander | 14:8320b5ff96fa | 1647 | |
johnAlexander | 14:8320b5ff96fa | 1648 | if (status == VL53L0X_ERROR_NONE) { |
johnAlexander | 14:8320b5ff96fa | 1649 | status = vl53l0x_set_limit_check_value(dev, |
johnAlexander | 14:8320b5ff96fa | 1650 | VL53L0X_CHECKENABLE_SIGNAL_REF_CLIP, |
johnAlexander | 14:8320b5ff96fa | 1651 | (FixPoint1616_t)(35 * 65536)); |
johnAlexander | 14:8320b5ff96fa | 1652 | } |
johnAlexander | 14:8320b5ff96fa | 1653 | |
johnAlexander | 14:8320b5ff96fa | 1654 | if (status == VL53L0X_ERROR_NONE) { |
johnAlexander | 14:8320b5ff96fa | 1655 | status = vl53l0x_set_limit_check_value(dev, |
johnAlexander | 14:8320b5ff96fa | 1656 | VL53L0X_CHECKENABLE_RANGE_IGNORE_THRESHOLD, |
johnAlexander | 14:8320b5ff96fa | 1657 | (FixPoint1616_t)(0 * 65536)); |
johnAlexander | 14:8320b5ff96fa | 1658 | } |
johnAlexander | 14:8320b5ff96fa | 1659 | |
johnAlexander | 14:8320b5ff96fa | 1660 | if (status == VL53L0X_ERROR_NONE) { |
johnAlexander | 14:8320b5ff96fa | 1661 | |
johnAlexander | 14:8320b5ff96fa | 1662 | PALDevDataSet(dev, SequenceConfig, 0xFF); |
johnAlexander | 14:8320b5ff96fa | 1663 | status = vl53l0x_write_byte(dev, VL53L0X_REG_SYSTEM_SEQUENCE_CONFIG, |
johnAlexander | 14:8320b5ff96fa | 1664 | 0xFF); |
johnAlexander | 14:8320b5ff96fa | 1665 | |
johnAlexander | 14:8320b5ff96fa | 1666 | /* Set PAL state to tell that we are waiting for call to |
johnAlexander | 14:8320b5ff96fa | 1667 | * VL53L0X_StaticInit */ |
johnAlexander | 14:8320b5ff96fa | 1668 | PALDevDataSet(dev, PalState, VL53L0X_STATE_WAIT_STATICINIT); |
johnAlexander | 14:8320b5ff96fa | 1669 | } |
johnAlexander | 14:8320b5ff96fa | 1670 | |
johnAlexander | 14:8320b5ff96fa | 1671 | if (status == VL53L0X_ERROR_NONE) |
johnAlexander | 14:8320b5ff96fa | 1672 | VL53L0X_SETDEVICESPECIFICPARAMETER(dev, RefSpadsInitialised, 0); |
johnAlexander | 14:8320b5ff96fa | 1673 | |
johnAlexander | 14:8320b5ff96fa | 1674 | |
johnAlexander | 14:8320b5ff96fa | 1675 | LOG_FUNCTION_END(status); |
johnAlexander | 14:8320b5ff96fa | 1676 | return status; |
johnAlexander | 0:c523920bcc09 | 1677 | } |
johnAlexander | 0:c523920bcc09 | 1678 | |
johnAlexander | 14:8320b5ff96fa | 1679 | VL53L0X_Error VL53L0X::vl53l0x_check_part_used(VL53L0X_DEV dev, |
johnAlexander | 14:8320b5ff96fa | 1680 | uint8_t *revision, |
johnAlexander | 14:8320b5ff96fa | 1681 | VL53L0X_DeviceInfo_t *p_vl53l0x_device_info) |
johnAlexander | 0:c523920bcc09 | 1682 | { |
johnAlexander | 14:8320b5ff96fa | 1683 | VL53L0X_Error status = VL53L0X_ERROR_NONE; |
johnAlexander | 14:8320b5ff96fa | 1684 | uint8_t module_id_int; |
johnAlexander | 14:8320b5ff96fa | 1685 | char *product_id_tmp; |
johnAlexander | 14:8320b5ff96fa | 1686 | |
johnAlexander | 14:8320b5ff96fa | 1687 | LOG_FUNCTION_START(""); |
johnAlexander | 14:8320b5ff96fa | 1688 | |
johnAlexander | 14:8320b5ff96fa | 1689 | status = vl53l0x_get_info_from_device(dev, 2); |
johnAlexander | 14:8320b5ff96fa | 1690 | |
johnAlexander | 14:8320b5ff96fa | 1691 | if (status == VL53L0X_ERROR_NONE) { |
johnAlexander | 14:8320b5ff96fa | 1692 | module_id_int = VL53L0X_GETDEVICESPECIFICPARAMETER(dev, ModuleId); |
johnAlexander | 14:8320b5ff96fa | 1693 | |
johnAlexander | 14:8320b5ff96fa | 1694 | if (module_id_int == 0) { |
johnAlexander | 14:8320b5ff96fa | 1695 | *revision = 0; |
johnAlexander | 14:8320b5ff96fa | 1696 | VL53L0X_COPYSTRING(p_vl53l0x_device_info->ProductId, ""); |
johnAlexander | 14:8320b5ff96fa | 1697 | } else { |
johnAlexander | 14:8320b5ff96fa | 1698 | *revision = VL53L0X_GETDEVICESPECIFICPARAMETER(dev, Revision); |
johnAlexander | 14:8320b5ff96fa | 1699 | product_id_tmp = VL53L0X_GETDEVICESPECIFICPARAMETER(dev, |
johnAlexander | 14:8320b5ff96fa | 1700 | ProductId); |
johnAlexander | 14:8320b5ff96fa | 1701 | VL53L0X_COPYSTRING(p_vl53l0x_device_info->ProductId, product_id_tmp); |
johnAlexander | 14:8320b5ff96fa | 1702 | } |
johnAlexander | 14:8320b5ff96fa | 1703 | } |
johnAlexander | 14:8320b5ff96fa | 1704 | |
johnAlexander | 14:8320b5ff96fa | 1705 | LOG_FUNCTION_END(status); |
johnAlexander | 14:8320b5ff96fa | 1706 | return status; |
johnAlexander | 0:c523920bcc09 | 1707 | } |
johnAlexander | 0:c523920bcc09 | 1708 | |
johnAlexander | 14:8320b5ff96fa | 1709 | VL53L0X_Error VL53L0X::wrapped_vl53l0x_get_device_info(VL53L0X_DEV dev, |
johnAlexander | 14:8320b5ff96fa | 1710 | VL53L0X_DeviceInfo_t *p_vl53l0x_device_info) |
johnAlexander | 0:c523920bcc09 | 1711 | { |
johnAlexander | 14:8320b5ff96fa | 1712 | VL53L0X_Error status = VL53L0X_ERROR_NONE; |
johnAlexander | 14:8320b5ff96fa | 1713 | uint8_t revision_id; |
johnAlexander | 14:8320b5ff96fa | 1714 | uint8_t revision; |
johnAlexander | 14:8320b5ff96fa | 1715 | |
johnAlexander | 14:8320b5ff96fa | 1716 | status = vl53l0x_check_part_used(dev, &revision, p_vl53l0x_device_info); |
johnAlexander | 14:8320b5ff96fa | 1717 | |
johnAlexander | 14:8320b5ff96fa | 1718 | if (status == VL53L0X_ERROR_NONE) { |
johnAlexander | 14:8320b5ff96fa | 1719 | if (revision == 0) { |
johnAlexander | 14:8320b5ff96fa | 1720 | VL53L0X_COPYSTRING(p_vl53l0x_device_info->Name, |
johnAlexander | 14:8320b5ff96fa | 1721 | VL53L0X_STRING_DEVICE_INFO_NAME_TS0); |
johnAlexander | 14:8320b5ff96fa | 1722 | } else if ((revision <= 34) && (revision != 32)) { |
johnAlexander | 14:8320b5ff96fa | 1723 | VL53L0X_COPYSTRING(p_vl53l0x_device_info->Name, |
johnAlexander | 14:8320b5ff96fa | 1724 | VL53L0X_STRING_DEVICE_INFO_NAME_TS1); |
johnAlexander | 14:8320b5ff96fa | 1725 | } else if (revision < 39) { |
johnAlexander | 14:8320b5ff96fa | 1726 | VL53L0X_COPYSTRING(p_vl53l0x_device_info->Name, |
johnAlexander | 14:8320b5ff96fa | 1727 | VL53L0X_STRING_DEVICE_INFO_NAME_TS2); |
johnAlexander | 14:8320b5ff96fa | 1728 | } else { |
johnAlexander | 14:8320b5ff96fa | 1729 | VL53L0X_COPYSTRING(p_vl53l0x_device_info->Name, |
johnAlexander | 14:8320b5ff96fa | 1730 | VL53L0X_STRING_DEVICE_INFO_NAME_ES1); |
johnAlexander | 14:8320b5ff96fa | 1731 | } |
johnAlexander | 14:8320b5ff96fa | 1732 | |
johnAlexander | 14:8320b5ff96fa | 1733 | VL53L0X_COPYSTRING(p_vl53l0x_device_info->Type, |
johnAlexander | 14:8320b5ff96fa | 1734 | VL53L0X_STRING_DEVICE_INFO_TYPE); |
johnAlexander | 14:8320b5ff96fa | 1735 | |
johnAlexander | 14:8320b5ff96fa | 1736 | } |
johnAlexander | 14:8320b5ff96fa | 1737 | |
johnAlexander | 14:8320b5ff96fa | 1738 | if (status == VL53L0X_ERROR_NONE) { |
johnAlexander | 14:8320b5ff96fa | 1739 | status = vl53l0x_read_byte(dev, VL53L0X_REG_IDENTIFICATION_MODEL_ID, |
johnAlexander | 14:8320b5ff96fa | 1740 | &p_vl53l0x_device_info->ProductType); |
johnAlexander | 14:8320b5ff96fa | 1741 | } |
johnAlexander | 14:8320b5ff96fa | 1742 | |
johnAlexander | 14:8320b5ff96fa | 1743 | if (status == VL53L0X_ERROR_NONE) { |
johnAlexander | 14:8320b5ff96fa | 1744 | status = vl53l0x_read_byte(dev, |
johnAlexander | 14:8320b5ff96fa | 1745 | VL53L0X_REG_IDENTIFICATION_REVISION_ID, |
johnAlexander | 14:8320b5ff96fa | 1746 | &revision_id); |
johnAlexander | 14:8320b5ff96fa | 1747 | p_vl53l0x_device_info->ProductRevisionMajor = 1; |
johnAlexander | 14:8320b5ff96fa | 1748 | p_vl53l0x_device_info->ProductRevisionMinor = |
johnAlexander | 14:8320b5ff96fa | 1749 | (revision_id & 0xF0) >> 4; |
johnAlexander | 14:8320b5ff96fa | 1750 | } |
johnAlexander | 14:8320b5ff96fa | 1751 | |
johnAlexander | 14:8320b5ff96fa | 1752 | return status; |
johnAlexander | 0:c523920bcc09 | 1753 | } |
johnAlexander | 0:c523920bcc09 | 1754 | |
johnAlexander | 14:8320b5ff96fa | 1755 | VL53L0X_Error VL53L0X::vl53l0x_get_device_info(VL53L0X_DEV dev, |
johnAlexander | 14:8320b5ff96fa | 1756 | VL53L0X_DeviceInfo_t *p_vl53l0x_device_info) |
johnAlexander | 0:c523920bcc09 | 1757 | { |
johnAlexander | 14:8320b5ff96fa | 1758 | VL53L0X_Error status = VL53L0X_ERROR_NONE; |
johnAlexander | 14:8320b5ff96fa | 1759 | LOG_FUNCTION_START(""); |
johnAlexander | 14:8320b5ff96fa | 1760 | |
johnAlexander | 14:8320b5ff96fa | 1761 | status = wrapped_vl53l0x_get_device_info(dev, p_vl53l0x_device_info); |
johnAlexander | 14:8320b5ff96fa | 1762 | |
johnAlexander | 14:8320b5ff96fa | 1763 | LOG_FUNCTION_END(status); |
johnAlexander | 14:8320b5ff96fa | 1764 | return status; |
johnAlexander | 0:c523920bcc09 | 1765 | } |
johnAlexander | 0:c523920bcc09 | 1766 | |
johnAlexander | 14:8320b5ff96fa | 1767 | VL53L0X_Error VL53L0X::vl53l0x_get_interrupt_mask_status(VL53L0X_DEV dev, |
johnAlexander | 14:8320b5ff96fa | 1768 | uint32_t *p_interrupt_mask_status) |
johnAlexander | 0:c523920bcc09 | 1769 | { |
johnAlexander | 14:8320b5ff96fa | 1770 | VL53L0X_Error status = VL53L0X_ERROR_NONE; |
johnAlexander | 14:8320b5ff96fa | 1771 | uint8_t byte; |
johnAlexander | 14:8320b5ff96fa | 1772 | LOG_FUNCTION_START(""); |
johnAlexander | 14:8320b5ff96fa | 1773 | |
johnAlexander | 14:8320b5ff96fa | 1774 | status = vl53l0x_read_byte(dev, VL53L0X_REG_RESULT_INTERRUPT_STATUS, &byte); |
johnAlexander | 14:8320b5ff96fa | 1775 | *p_interrupt_mask_status = byte & 0x07; |
johnAlexander | 14:8320b5ff96fa | 1776 | |
johnAlexander | 14:8320b5ff96fa | 1777 | if (byte & 0x18) |
johnAlexander | 14:8320b5ff96fa | 1778 | status = VL53L0X_ERROR_RANGE_ERROR; |
johnAlexander | 14:8320b5ff96fa | 1779 | |
johnAlexander | 14:8320b5ff96fa | 1780 | LOG_FUNCTION_END(status); |
johnAlexander | 14:8320b5ff96fa | 1781 | return status; |
johnAlexander | 0:c523920bcc09 | 1782 | } |
johnAlexander | 0:c523920bcc09 | 1783 | |
johnAlexander | 14:8320b5ff96fa | 1784 | VL53L0X_Error VL53L0X::vl53l0x_get_measurement_data_ready(VL53L0X_DEV dev, |
johnAlexander | 14:8320b5ff96fa | 1785 | uint8_t *p_measurement_data_ready) |
johnAlexander | 0:c523920bcc09 | 1786 | { |
johnAlexander | 14:8320b5ff96fa | 1787 | VL53L0X_Error status = VL53L0X_ERROR_NONE; |
johnAlexander | 14:8320b5ff96fa | 1788 | uint8_t sys_range_status_register; |
johnAlexander | 14:8320b5ff96fa | 1789 | uint8_t interrupt_config; |
johnAlexander | 14:8320b5ff96fa | 1790 | uint32_t interrupt_mask; |
johnAlexander | 14:8320b5ff96fa | 1791 | LOG_FUNCTION_START(""); |
johnAlexander | 14:8320b5ff96fa | 1792 | |
johnAlexander | 14:8320b5ff96fa | 1793 | interrupt_config = VL53L0X_GETDEVICESPECIFICPARAMETER(dev, |
johnAlexander | 14:8320b5ff96fa | 1794 | Pin0GpioFunctionality); |
johnAlexander | 14:8320b5ff96fa | 1795 | |
johnAlexander | 14:8320b5ff96fa | 1796 | if (interrupt_config == |
johnAlexander | 14:8320b5ff96fa | 1797 | VL53L0X_REG_SYSTEM_INTERRUPT_GPIO_NEW_SAMPLE_READY) { |
johnAlexander | 14:8320b5ff96fa | 1798 | status = vl53l0x_get_interrupt_mask_status(dev, &interrupt_mask); |
johnAlexander | 14:8320b5ff96fa | 1799 | if (interrupt_mask == |
johnAlexander | 14:8320b5ff96fa | 1800 | VL53L0X_REG_SYSTEM_INTERRUPT_GPIO_NEW_SAMPLE_READY) |
johnAlexander | 14:8320b5ff96fa | 1801 | *p_measurement_data_ready = 1; |
johnAlexander | 14:8320b5ff96fa | 1802 | else |
johnAlexander | 14:8320b5ff96fa | 1803 | *p_measurement_data_ready = 0; |
johnAlexander | 14:8320b5ff96fa | 1804 | } else { |
johnAlexander | 14:8320b5ff96fa | 1805 | status = vl53l0x_read_byte(dev, VL53L0X_REG_RESULT_RANGE_STATUS, |
johnAlexander | 14:8320b5ff96fa | 1806 | &sys_range_status_register); |
johnAlexander | 14:8320b5ff96fa | 1807 | if (status == VL53L0X_ERROR_NONE) { |
johnAlexander | 14:8320b5ff96fa | 1808 | if (sys_range_status_register & 0x01) |
johnAlexander | 14:8320b5ff96fa | 1809 | *p_measurement_data_ready = 1; |
johnAlexander | 14:8320b5ff96fa | 1810 | else |
johnAlexander | 14:8320b5ff96fa | 1811 | *p_measurement_data_ready = 0; |
johnAlexander | 14:8320b5ff96fa | 1812 | } |
johnAlexander | 14:8320b5ff96fa | 1813 | } |
johnAlexander | 14:8320b5ff96fa | 1814 | |
johnAlexander | 14:8320b5ff96fa | 1815 | LOG_FUNCTION_END(status); |
johnAlexander | 14:8320b5ff96fa | 1816 | return status; |
johnAlexander | 0:c523920bcc09 | 1817 | } |
johnAlexander | 0:c523920bcc09 | 1818 | |
johnAlexander | 14:8320b5ff96fa | 1819 | VL53L0X_Error VL53L0X::vl53l0x_polling_delay(VL53L0X_DEV dev) |
johnAlexander | 14:8320b5ff96fa | 1820 | { |
johnAlexander | 0:c523920bcc09 | 1821 | VL53L0X_Error status = VL53L0X_ERROR_NONE; |
johnAlexander | 0:c523920bcc09 | 1822 | |
johnAlexander | 0:c523920bcc09 | 1823 | // do nothing |
johnAlexander | 0:c523920bcc09 | 1824 | VL53L0X_OsDelay(); |
johnAlexander | 0:c523920bcc09 | 1825 | return status; |
johnAlexander | 0:c523920bcc09 | 1826 | } |
johnAlexander | 0:c523920bcc09 | 1827 | |
johnAlexander | 14:8320b5ff96fa | 1828 | VL53L0X_Error VL53L0X::vl53l0x_measurement_poll_for_completion(VL53L0X_DEV dev) |
johnAlexander | 0:c523920bcc09 | 1829 | { |
johnAlexander | 14:8320b5ff96fa | 1830 | VL53L0X_Error status = VL53L0X_ERROR_NONE; |
johnAlexander | 14:8320b5ff96fa | 1831 | uint8_t new_data_ready = 0; |
johnAlexander | 14:8320b5ff96fa | 1832 | uint32_t loop_nb; |
johnAlexander | 14:8320b5ff96fa | 1833 | |
johnAlexander | 14:8320b5ff96fa | 1834 | LOG_FUNCTION_START(""); |
johnAlexander | 14:8320b5ff96fa | 1835 | |
johnAlexander | 14:8320b5ff96fa | 1836 | loop_nb = 0; |
johnAlexander | 14:8320b5ff96fa | 1837 | |
johnAlexander | 14:8320b5ff96fa | 1838 | do { |
johnAlexander | 14:8320b5ff96fa | 1839 | status = vl53l0x_get_measurement_data_ready(dev, &new_data_ready); |
johnAlexander | 14:8320b5ff96fa | 1840 | if (status != 0) |
johnAlexander | 14:8320b5ff96fa | 1841 | break; /* the error is set */ |
johnAlexander | 14:8320b5ff96fa | 1842 | |
johnAlexander | 14:8320b5ff96fa | 1843 | if (new_data_ready == 1) |
johnAlexander | 14:8320b5ff96fa | 1844 | break; /* done note that status == 0 */ |
johnAlexander | 14:8320b5ff96fa | 1845 | |
johnAlexander | 14:8320b5ff96fa | 1846 | loop_nb++; |
johnAlexander | 14:8320b5ff96fa | 1847 | if (loop_nb >= VL53L0X_DEFAULT_MAX_LOOP) { |
johnAlexander | 14:8320b5ff96fa | 1848 | status = VL53L0X_ERROR_TIME_OUT; |
johnAlexander | 14:8320b5ff96fa | 1849 | break; |
johnAlexander | 14:8320b5ff96fa | 1850 | } |
johnAlexander | 14:8320b5ff96fa | 1851 | |
johnAlexander | 14:8320b5ff96fa | 1852 | vl53l0x_polling_delay(dev); |
johnAlexander | 14:8320b5ff96fa | 1853 | } while (1); |
johnAlexander | 14:8320b5ff96fa | 1854 | |
johnAlexander | 14:8320b5ff96fa | 1855 | LOG_FUNCTION_END(status); |
johnAlexander | 14:8320b5ff96fa | 1856 | |
johnAlexander | 14:8320b5ff96fa | 1857 | return status; |
johnAlexander | 0:c523920bcc09 | 1858 | } |
johnAlexander | 0:c523920bcc09 | 1859 | |
johnAlexander | 0:c523920bcc09 | 1860 | /* Group PAL Interrupt Functions */ |
johnAlexander | 14:8320b5ff96fa | 1861 | VL53L0X_Error VL53L0X::vl53l0x_clear_interrupt_mask(VL53L0X_DEV dev, uint32_t interrupt_mask) |
johnAlexander | 0:c523920bcc09 | 1862 | { |
johnAlexander | 14:8320b5ff96fa | 1863 | VL53L0X_Error status = VL53L0X_ERROR_NONE; |
johnAlexander | 14:8320b5ff96fa | 1864 | uint8_t loop_count; |
johnAlexander | 14:8320b5ff96fa | 1865 | uint8_t byte; |
johnAlexander | 14:8320b5ff96fa | 1866 | LOG_FUNCTION_START(""); |
johnAlexander | 14:8320b5ff96fa | 1867 | |
johnAlexander | 14:8320b5ff96fa | 1868 | /* clear bit 0 range interrupt, bit 1 error interrupt */ |
johnAlexander | 14:8320b5ff96fa | 1869 | loop_count = 0; |
johnAlexander | 14:8320b5ff96fa | 1870 | do { |
johnAlexander | 14:8320b5ff96fa | 1871 | status = vl53l0x_write_byte(dev, |
johnAlexander | 14:8320b5ff96fa | 1872 | VL53L0X_REG_SYSTEM_INTERRUPT_CLEAR, 0x01); |
johnAlexander | 14:8320b5ff96fa | 1873 | status |= vl53l0x_write_byte(dev, |
johnAlexander | 14:8320b5ff96fa | 1874 | VL53L0X_REG_SYSTEM_INTERRUPT_CLEAR, 0x00); |
johnAlexander | 14:8320b5ff96fa | 1875 | status |= vl53l0x_read_byte(dev, |
johnAlexander | 14:8320b5ff96fa | 1876 | VL53L0X_REG_RESULT_INTERRUPT_STATUS, &byte); |
johnAlexander | 14:8320b5ff96fa | 1877 | loop_count++; |
johnAlexander | 14:8320b5ff96fa | 1878 | } while (((byte & 0x07) != 0x00) |
johnAlexander | 14:8320b5ff96fa | 1879 | && (loop_count < 3) |
johnAlexander | 14:8320b5ff96fa | 1880 | && (status == VL53L0X_ERROR_NONE)); |
johnAlexander | 14:8320b5ff96fa | 1881 | |
johnAlexander | 14:8320b5ff96fa | 1882 | |
johnAlexander | 14:8320b5ff96fa | 1883 | if (loop_count >= 3) |
johnAlexander | 14:8320b5ff96fa | 1884 | status = VL53L0X_ERROR_INTERRUPT_NOT_CLEARED; |
johnAlexander | 14:8320b5ff96fa | 1885 | |
johnAlexander | 14:8320b5ff96fa | 1886 | LOG_FUNCTION_END(status); |
johnAlexander | 14:8320b5ff96fa | 1887 | return status; |
johnAlexander | 0:c523920bcc09 | 1888 | } |
johnAlexander | 0:c523920bcc09 | 1889 | |
johnAlexander | 14:8320b5ff96fa | 1890 | VL53L0X_Error VL53L0X::vl53l0x_perform_single_ref_calibration(VL53L0X_DEV dev, |
johnAlexander | 14:8320b5ff96fa | 1891 | uint8_t vhv_init_byte) |
johnAlexander | 0:c523920bcc09 | 1892 | { |
johnAlexander | 14:8320b5ff96fa | 1893 | VL53L0X_Error status = VL53L0X_ERROR_NONE; |
johnAlexander | 14:8320b5ff96fa | 1894 | |
johnAlexander | 14:8320b5ff96fa | 1895 | if (status == VL53L0X_ERROR_NONE) |
johnAlexander | 14:8320b5ff96fa | 1896 | status = vl53l0x_write_byte(dev, VL53L0X_REG_SYSRANGE_START, |
johnAlexander | 14:8320b5ff96fa | 1897 | VL53L0X_REG_SYSRANGE_MODE_START_STOP | |
johnAlexander | 14:8320b5ff96fa | 1898 | vhv_init_byte); |
johnAlexander | 14:8320b5ff96fa | 1899 | |
johnAlexander | 14:8320b5ff96fa | 1900 | if (status == VL53L0X_ERROR_NONE) |
johnAlexander | 14:8320b5ff96fa | 1901 | status = vl53l0x_measurement_poll_for_completion(dev); |
johnAlexander | 14:8320b5ff96fa | 1902 | |
johnAlexander | 14:8320b5ff96fa | 1903 | if (status == VL53L0X_ERROR_NONE) |
johnAlexander | 14:8320b5ff96fa | 1904 | status = vl53l0x_clear_interrupt_mask(dev, 0); |
johnAlexander | 14:8320b5ff96fa | 1905 | |
johnAlexander | 14:8320b5ff96fa | 1906 | if (status == VL53L0X_ERROR_NONE) |
johnAlexander | 14:8320b5ff96fa | 1907 | status = vl53l0x_write_byte(dev, VL53L0X_REG_SYSRANGE_START, 0x00); |
johnAlexander | 14:8320b5ff96fa | 1908 | |
johnAlexander | 14:8320b5ff96fa | 1909 | return status; |
johnAlexander | 0:c523920bcc09 | 1910 | } |
johnAlexander | 0:c523920bcc09 | 1911 | |
johnAlexander | 14:8320b5ff96fa | 1912 | VL53L0X_Error VL53L0X::vl53l0x_ref_calibration_io(VL53L0X_DEV dev, uint8_t read_not_write, |
johnAlexander | 14:8320b5ff96fa | 1913 | uint8_t vhv_settings, uint8_t phase_cal, |
johnAlexander | 14:8320b5ff96fa | 1914 | uint8_t *p_vhv_settings, uint8_t *p_phase_cal, |
johnAlexander | 14:8320b5ff96fa | 1915 | const uint8_t vhv_enable, const uint8_t phase_enable) |
johnAlexander | 0:c523920bcc09 | 1916 | { |
johnAlexander | 14:8320b5ff96fa | 1917 | VL53L0X_Error status = VL53L0X_ERROR_NONE; |
johnAlexander | 14:8320b5ff96fa | 1918 | uint8_t phase_calint = 0; |
johnAlexander | 14:8320b5ff96fa | 1919 | |
johnAlexander | 14:8320b5ff96fa | 1920 | /* Read VHV from device */ |
johnAlexander | 14:8320b5ff96fa | 1921 | status |= vl53l0x_write_byte(dev, 0xFF, 0x01); |
johnAlexander | 14:8320b5ff96fa | 1922 | status |= vl53l0x_write_byte(dev, 0x00, 0x00); |
johnAlexander | 14:8320b5ff96fa | 1923 | status |= vl53l0x_write_byte(dev, 0xFF, 0x00); |
johnAlexander | 14:8320b5ff96fa | 1924 | |
johnAlexander | 14:8320b5ff96fa | 1925 | if (read_not_write) { |
johnAlexander | 14:8320b5ff96fa | 1926 | if (vhv_enable) |
johnAlexander | 14:8320b5ff96fa | 1927 | status |= vl53l0x_read_byte(dev, 0xCB, p_vhv_settings); |
johnAlexander | 14:8320b5ff96fa | 1928 | if (phase_enable) |
johnAlexander | 14:8320b5ff96fa | 1929 | status |= vl53l0x_read_byte(dev, 0xEE, &phase_calint); |
johnAlexander | 14:8320b5ff96fa | 1930 | } else { |
johnAlexander | 14:8320b5ff96fa | 1931 | if (vhv_enable) |
johnAlexander | 14:8320b5ff96fa | 1932 | status |= vl53l0x_write_byte(dev, 0xCB, vhv_settings); |
johnAlexander | 14:8320b5ff96fa | 1933 | if (phase_enable) |
johnAlexander | 14:8320b5ff96fa | 1934 | status |= vl53l0x_update_byte(dev, 0xEE, 0x80, phase_cal); |
johnAlexander | 14:8320b5ff96fa | 1935 | } |
johnAlexander | 14:8320b5ff96fa | 1936 | |
johnAlexander | 14:8320b5ff96fa | 1937 | status |= vl53l0x_write_byte(dev, 0xFF, 0x01); |
johnAlexander | 14:8320b5ff96fa | 1938 | status |= vl53l0x_write_byte(dev, 0x00, 0x01); |
johnAlexander | 14:8320b5ff96fa | 1939 | status |= vl53l0x_write_byte(dev, 0xFF, 0x00); |
johnAlexander | 14:8320b5ff96fa | 1940 | |
johnAlexander | 14:8320b5ff96fa | 1941 | *p_phase_cal = (uint8_t)(phase_calint & 0xEF); |
johnAlexander | 14:8320b5ff96fa | 1942 | |
johnAlexander | 14:8320b5ff96fa | 1943 | return status; |
johnAlexander | 0:c523920bcc09 | 1944 | } |
johnAlexander | 0:c523920bcc09 | 1945 | |
johnAlexander | 14:8320b5ff96fa | 1946 | VL53L0X_Error VL53L0X::vl53l0x_perform_vhv_calibration(VL53L0X_DEV dev, |
johnAlexander | 14:8320b5ff96fa | 1947 | uint8_t *p_vhv_settings, const uint8_t get_data_enable, |
johnAlexander | 14:8320b5ff96fa | 1948 | const uint8_t restore_config) |
johnAlexander | 0:c523920bcc09 | 1949 | { |
johnAlexander | 14:8320b5ff96fa | 1950 | VL53L0X_Error status = VL53L0X_ERROR_NONE; |
johnAlexander | 14:8320b5ff96fa | 1951 | uint8_t sequence_config = 0; |
johnAlexander | 14:8320b5ff96fa | 1952 | uint8_t vhv_settings = 0; |
johnAlexander | 14:8320b5ff96fa | 1953 | uint8_t phase_cal = 0; |
johnAlexander | 14:8320b5ff96fa | 1954 | uint8_t phase_cal_int = 0; |
johnAlexander | 14:8320b5ff96fa | 1955 | |
johnAlexander | 14:8320b5ff96fa | 1956 | /* store the value of the sequence config, |
johnAlexander | 14:8320b5ff96fa | 1957 | * this will be reset before the end of the function |
johnAlexander | 14:8320b5ff96fa | 1958 | */ |
johnAlexander | 14:8320b5ff96fa | 1959 | |
johnAlexander | 14:8320b5ff96fa | 1960 | if (restore_config) |
johnAlexander | 14:8320b5ff96fa | 1961 | sequence_config = PALDevDataGet(dev, SequenceConfig); |
johnAlexander | 14:8320b5ff96fa | 1962 | |
johnAlexander | 14:8320b5ff96fa | 1963 | /* Run VHV */ |
johnAlexander | 14:8320b5ff96fa | 1964 | status = vl53l0x_write_byte(dev, VL53L0X_REG_SYSTEM_SEQUENCE_CONFIG, 0x01); |
johnAlexander | 14:8320b5ff96fa | 1965 | |
johnAlexander | 14:8320b5ff96fa | 1966 | if (status == VL53L0X_ERROR_NONE) |
johnAlexander | 14:8320b5ff96fa | 1967 | status = vl53l0x_perform_single_ref_calibration(dev, 0x40); |
johnAlexander | 14:8320b5ff96fa | 1968 | |
johnAlexander | 14:8320b5ff96fa | 1969 | /* Read VHV from device */ |
johnAlexander | 14:8320b5ff96fa | 1970 | if ((status == VL53L0X_ERROR_NONE) && (get_data_enable == 1)) { |
johnAlexander | 14:8320b5ff96fa | 1971 | status = vl53l0x_ref_calibration_io(dev, 1, |
johnAlexander | 14:8320b5ff96fa | 1972 | vhv_settings, phase_cal, /* Not used here */ |
johnAlexander | 14:8320b5ff96fa | 1973 | p_vhv_settings, &phase_cal_int, |
johnAlexander | 14:8320b5ff96fa | 1974 | 1, 0); |
johnAlexander | 14:8320b5ff96fa | 1975 | } else |
johnAlexander | 14:8320b5ff96fa | 1976 | *p_vhv_settings = 0; |
johnAlexander | 14:8320b5ff96fa | 1977 | |
johnAlexander | 14:8320b5ff96fa | 1978 | |
johnAlexander | 14:8320b5ff96fa | 1979 | if ((status == VL53L0X_ERROR_NONE) && restore_config) { |
johnAlexander | 14:8320b5ff96fa | 1980 | /* restore the previous Sequence Config */ |
johnAlexander | 14:8320b5ff96fa | 1981 | status = vl53l0x_write_byte(dev, VL53L0X_REG_SYSTEM_SEQUENCE_CONFIG, |
johnAlexander | 14:8320b5ff96fa | 1982 | sequence_config); |
johnAlexander | 14:8320b5ff96fa | 1983 | if (status == VL53L0X_ERROR_NONE) |
johnAlexander | 14:8320b5ff96fa | 1984 | PALDevDataSet(dev, SequenceConfig, sequence_config); |
johnAlexander | 14:8320b5ff96fa | 1985 | |
johnAlexander | 14:8320b5ff96fa | 1986 | } |
johnAlexander | 14:8320b5ff96fa | 1987 | |
johnAlexander | 14:8320b5ff96fa | 1988 | return status; |
johnAlexander | 0:c523920bcc09 | 1989 | } |
johnAlexander | 0:c523920bcc09 | 1990 | |
johnAlexander | 14:8320b5ff96fa | 1991 | VL53L0X_Error VL53L0X::vl53l0x_perform_phase_calibration(VL53L0X_DEV dev, |
johnAlexander | 14:8320b5ff96fa | 1992 | uint8_t *p_phase_cal, const uint8_t get_data_enable, |
johnAlexander | 14:8320b5ff96fa | 1993 | const uint8_t restore_config) |
johnAlexander | 0:c523920bcc09 | 1994 | { |
johnAlexander | 14:8320b5ff96fa | 1995 | VL53L0X_Error status = VL53L0X_ERROR_NONE; |
johnAlexander | 14:8320b5ff96fa | 1996 | uint8_t sequence_config = 0; |
johnAlexander | 14:8320b5ff96fa | 1997 | uint8_t vhv_settings = 0; |
johnAlexander | 14:8320b5ff96fa | 1998 | uint8_t phase_cal = 0; |
johnAlexander | 14:8320b5ff96fa | 1999 | uint8_t vhv_settingsint; |
johnAlexander | 14:8320b5ff96fa | 2000 | |
johnAlexander | 14:8320b5ff96fa | 2001 | /* store the value of the sequence config, |
johnAlexander | 14:8320b5ff96fa | 2002 | * this will be reset before the end of the function |
johnAlexander | 14:8320b5ff96fa | 2003 | */ |
johnAlexander | 14:8320b5ff96fa | 2004 | |
johnAlexander | 14:8320b5ff96fa | 2005 | if (restore_config) |
johnAlexander | 14:8320b5ff96fa | 2006 | sequence_config = PALDevDataGet(dev, SequenceConfig); |
johnAlexander | 14:8320b5ff96fa | 2007 | |
johnAlexander | 14:8320b5ff96fa | 2008 | /* Run PhaseCal */ |
johnAlexander | 14:8320b5ff96fa | 2009 | status = vl53l0x_write_byte(dev, VL53L0X_REG_SYSTEM_SEQUENCE_CONFIG, 0x02); |
johnAlexander | 14:8320b5ff96fa | 2010 | |
johnAlexander | 14:8320b5ff96fa | 2011 | if (status == VL53L0X_ERROR_NONE) |
johnAlexander | 14:8320b5ff96fa | 2012 | status = vl53l0x_perform_single_ref_calibration(dev, 0x0); |
johnAlexander | 14:8320b5ff96fa | 2013 | |
johnAlexander | 14:8320b5ff96fa | 2014 | /* Read PhaseCal from device */ |
johnAlexander | 14:8320b5ff96fa | 2015 | if ((status == VL53L0X_ERROR_NONE) && (get_data_enable == 1)) { |
johnAlexander | 14:8320b5ff96fa | 2016 | status = vl53l0x_ref_calibration_io(dev, 1, |
johnAlexander | 14:8320b5ff96fa | 2017 | vhv_settings, phase_cal, /* Not used here */ |
johnAlexander | 14:8320b5ff96fa | 2018 | &vhv_settingsint, p_phase_cal, |
johnAlexander | 14:8320b5ff96fa | 2019 | 0, 1); |
johnAlexander | 14:8320b5ff96fa | 2020 | } else |
johnAlexander | 14:8320b5ff96fa | 2021 | *p_phase_cal = 0; |
johnAlexander | 14:8320b5ff96fa | 2022 | |
johnAlexander | 14:8320b5ff96fa | 2023 | |
johnAlexander | 14:8320b5ff96fa | 2024 | if ((status == VL53L0X_ERROR_NONE) && restore_config) { |
johnAlexander | 14:8320b5ff96fa | 2025 | /* restore the previous Sequence Config */ |
johnAlexander | 14:8320b5ff96fa | 2026 | status = vl53l0x_write_byte(dev, VL53L0X_REG_SYSTEM_SEQUENCE_CONFIG, |
johnAlexander | 14:8320b5ff96fa | 2027 | sequence_config); |
johnAlexander | 14:8320b5ff96fa | 2028 | if (status == VL53L0X_ERROR_NONE) |
johnAlexander | 14:8320b5ff96fa | 2029 | PALDevDataSet(dev, SequenceConfig, sequence_config); |
johnAlexander | 14:8320b5ff96fa | 2030 | |
johnAlexander | 14:8320b5ff96fa | 2031 | } |
johnAlexander | 14:8320b5ff96fa | 2032 | |
johnAlexander | 14:8320b5ff96fa | 2033 | return status; |
johnAlexander | 0:c523920bcc09 | 2034 | } |
johnAlexander | 0:c523920bcc09 | 2035 | |
johnAlexander | 14:8320b5ff96fa | 2036 | VL53L0X_Error VL53L0X::vl53l0x_perform_ref_calibration(VL53L0X_DEV dev, |
johnAlexander | 14:8320b5ff96fa | 2037 | uint8_t *p_vhv_settings, uint8_t *p_phase_cal, uint8_t get_data_enable) |
johnAlexander | 0:c523920bcc09 | 2038 | { |
johnAlexander | 14:8320b5ff96fa | 2039 | VL53L0X_Error status = VL53L0X_ERROR_NONE; |
johnAlexander | 14:8320b5ff96fa | 2040 | uint8_t sequence_config = 0; |
johnAlexander | 14:8320b5ff96fa | 2041 | |
johnAlexander | 14:8320b5ff96fa | 2042 | /* store the value of the sequence config, |
johnAlexander | 14:8320b5ff96fa | 2043 | * this will be reset before the end of the function |
johnAlexander | 14:8320b5ff96fa | 2044 | */ |
johnAlexander | 14:8320b5ff96fa | 2045 | |
johnAlexander | 14:8320b5ff96fa | 2046 | sequence_config = PALDevDataGet(dev, SequenceConfig); |
johnAlexander | 14:8320b5ff96fa | 2047 | |
johnAlexander | 14:8320b5ff96fa | 2048 | /* In the following function we don't save the config to optimize |
johnAlexander | 14:8320b5ff96fa | 2049 | * writes on device. Config is saved and restored only once. */ |
johnAlexander | 14:8320b5ff96fa | 2050 | status = vl53l0x_perform_vhv_calibration( |
johnAlexander | 14:8320b5ff96fa | 2051 | dev, p_vhv_settings, get_data_enable, 0); |
johnAlexander | 14:8320b5ff96fa | 2052 | |
johnAlexander | 14:8320b5ff96fa | 2053 | |
johnAlexander | 14:8320b5ff96fa | 2054 | if (status == VL53L0X_ERROR_NONE) |
johnAlexander | 14:8320b5ff96fa | 2055 | status = vl53l0x_perform_phase_calibration( |
johnAlexander | 14:8320b5ff96fa | 2056 | dev, p_phase_cal, get_data_enable, 0); |
johnAlexander | 14:8320b5ff96fa | 2057 | |
johnAlexander | 14:8320b5ff96fa | 2058 | |
johnAlexander | 14:8320b5ff96fa | 2059 | if (status == VL53L0X_ERROR_NONE) { |
johnAlexander | 14:8320b5ff96fa | 2060 | /* restore the previous Sequence Config */ |
johnAlexander | 14:8320b5ff96fa | 2061 | status = vl53l0x_write_byte(dev, VL53L0X_REG_SYSTEM_SEQUENCE_CONFIG, |
johnAlexander | 14:8320b5ff96fa | 2062 | sequence_config); |
johnAlexander | 14:8320b5ff96fa | 2063 | if (status == VL53L0X_ERROR_NONE) |
johnAlexander | 14:8320b5ff96fa | 2064 | PALDevDataSet(dev, SequenceConfig, sequence_config); |
johnAlexander | 14:8320b5ff96fa | 2065 | |
johnAlexander | 14:8320b5ff96fa | 2066 | } |
johnAlexander | 14:8320b5ff96fa | 2067 | |
johnAlexander | 14:8320b5ff96fa | 2068 | return status; |
johnAlexander | 0:c523920bcc09 | 2069 | } |
johnAlexander | 0:c523920bcc09 | 2070 | |
johnAlexander | 14:8320b5ff96fa | 2071 | void VL53L0X::get_next_good_spad(uint8_t good_spad_array[], uint32_t size, |
johnAlexander | 14:8320b5ff96fa | 2072 | uint32_t curr, int32_t *p_next) |
johnAlexander | 0:c523920bcc09 | 2073 | { |
johnAlexander | 14:8320b5ff96fa | 2074 | uint32_t start_index; |
johnAlexander | 14:8320b5ff96fa | 2075 | uint32_t fine_offset; |
johnAlexander | 14:8320b5ff96fa | 2076 | uint32_t c_spads_per_byte = 8; |
johnAlexander | 14:8320b5ff96fa | 2077 | uint32_t coarse_index; |
johnAlexander | 14:8320b5ff96fa | 2078 | uint32_t fine_index; |
johnAlexander | 14:8320b5ff96fa | 2079 | uint8_t data_byte; |
johnAlexander | 14:8320b5ff96fa | 2080 | uint8_t success = 0; |
johnAlexander | 14:8320b5ff96fa | 2081 | |
johnAlexander | 14:8320b5ff96fa | 2082 | /* |
johnAlexander | 14:8320b5ff96fa | 2083 | * Starting with the current good spad, loop through the array to find |
johnAlexander | 14:8320b5ff96fa | 2084 | * the next. i.e. the next bit set in the sequence. |
johnAlexander | 14:8320b5ff96fa | 2085 | * |
johnAlexander | 14:8320b5ff96fa | 2086 | * The coarse index is the byte index of the array and the fine index is |
johnAlexander | 14:8320b5ff96fa | 2087 | * the index of the bit within each byte. |
johnAlexander | 14:8320b5ff96fa | 2088 | */ |
johnAlexander | 14:8320b5ff96fa | 2089 | |
johnAlexander | 14:8320b5ff96fa | 2090 | *p_next = -1; |
johnAlexander | 14:8320b5ff96fa | 2091 | |
johnAlexander | 14:8320b5ff96fa | 2092 | start_index = curr / c_spads_per_byte; |
johnAlexander | 14:8320b5ff96fa | 2093 | fine_offset = curr % c_spads_per_byte; |
johnAlexander | 14:8320b5ff96fa | 2094 | |
johnAlexander | 14:8320b5ff96fa | 2095 | for (coarse_index = start_index; ((coarse_index < size) && !success); |
johnAlexander | 14:8320b5ff96fa | 2096 | coarse_index++) { |
johnAlexander | 14:8320b5ff96fa | 2097 | fine_index = 0; |
johnAlexander | 14:8320b5ff96fa | 2098 | data_byte = good_spad_array[coarse_index]; |
johnAlexander | 14:8320b5ff96fa | 2099 | |
johnAlexander | 14:8320b5ff96fa | 2100 | if (coarse_index == start_index) { |
johnAlexander | 14:8320b5ff96fa | 2101 | /* locate the bit position of the provided current |
johnAlexander | 14:8320b5ff96fa | 2102 | * spad bit before iterating */ |
johnAlexander | 14:8320b5ff96fa | 2103 | data_byte >>= fine_offset; |
johnAlexander | 14:8320b5ff96fa | 2104 | fine_index = fine_offset; |
johnAlexander | 14:8320b5ff96fa | 2105 | } |
johnAlexander | 14:8320b5ff96fa | 2106 | |
johnAlexander | 14:8320b5ff96fa | 2107 | while (fine_index < c_spads_per_byte) { |
johnAlexander | 14:8320b5ff96fa | 2108 | if ((data_byte & 0x1) == 1) { |
johnAlexander | 14:8320b5ff96fa | 2109 | success = 1; |
johnAlexander | 14:8320b5ff96fa | 2110 | *p_next = coarse_index * c_spads_per_byte + fine_index; |
johnAlexander | 14:8320b5ff96fa | 2111 | break; |
johnAlexander | 14:8320b5ff96fa | 2112 | } |
johnAlexander | 14:8320b5ff96fa | 2113 | data_byte >>= 1; |
johnAlexander | 14:8320b5ff96fa | 2114 | fine_index++; |
johnAlexander | 14:8320b5ff96fa | 2115 | } |
johnAlexander | 14:8320b5ff96fa | 2116 | } |
johnAlexander | 0:c523920bcc09 | 2117 | } |
johnAlexander | 0:c523920bcc09 | 2118 | |
johnAlexander | 14:8320b5ff96fa | 2119 | uint8_t VL53L0X::is_aperture(uint32_t spad_index) |
johnAlexander | 0:c523920bcc09 | 2120 | { |
johnAlexander | 14:8320b5ff96fa | 2121 | /* |
johnAlexander | 14:8320b5ff96fa | 2122 | * This function reports if a given spad index is an aperture SPAD by |
johnAlexander | 14:8320b5ff96fa | 2123 | * deriving the quadrant. |
johnAlexander | 14:8320b5ff96fa | 2124 | */ |
johnAlexander | 14:8320b5ff96fa | 2125 | uint32_t quadrant; |
johnAlexander | 14:8320b5ff96fa | 2126 | uint8_t is_aperture = 1; |
johnAlexander | 14:8320b5ff96fa | 2127 | quadrant = spad_index >> 6; |
johnAlexander | 14:8320b5ff96fa | 2128 | if (refArrayQuadrants[quadrant] == REF_ARRAY_SPAD_0) |
johnAlexander | 14:8320b5ff96fa | 2129 | is_aperture = 0; |
johnAlexander | 14:8320b5ff96fa | 2130 | |
johnAlexander | 14:8320b5ff96fa | 2131 | return is_aperture; |
johnAlexander | 0:c523920bcc09 | 2132 | } |
johnAlexander | 0:c523920bcc09 | 2133 | |
johnAlexander | 14:8320b5ff96fa | 2134 | VL53L0X_Error VL53L0X::enable_spad_bit(uint8_t spad_array[], uint32_t size, |
johnAlexander | 14:8320b5ff96fa | 2135 | uint32_t spad_index) |
johnAlexander | 0:c523920bcc09 | 2136 | { |
johnAlexander | 14:8320b5ff96fa | 2137 | VL53L0X_Error status = VL53L0X_ERROR_NONE; |
johnAlexander | 14:8320b5ff96fa | 2138 | uint32_t c_spads_per_byte = 8; |
johnAlexander | 14:8320b5ff96fa | 2139 | uint32_t coarse_index; |
johnAlexander | 14:8320b5ff96fa | 2140 | uint32_t fine_index; |
johnAlexander | 14:8320b5ff96fa | 2141 | |
johnAlexander | 14:8320b5ff96fa | 2142 | coarse_index = spad_index / c_spads_per_byte; |
johnAlexander | 14:8320b5ff96fa | 2143 | fine_index = spad_index % c_spads_per_byte; |
johnAlexander | 14:8320b5ff96fa | 2144 | if (coarse_index >= size) |
johnAlexander | 14:8320b5ff96fa | 2145 | status = VL53L0X_ERROR_REF_SPAD_INIT; |
johnAlexander | 14:8320b5ff96fa | 2146 | else |
johnAlexander | 14:8320b5ff96fa | 2147 | spad_array[coarse_index] |= (1 << fine_index); |
johnAlexander | 14:8320b5ff96fa | 2148 | |
johnAlexander | 14:8320b5ff96fa | 2149 | return status; |
johnAlexander | 0:c523920bcc09 | 2150 | } |
johnAlexander | 0:c523920bcc09 | 2151 | |
johnAlexander | 14:8320b5ff96fa | 2152 | VL53L0X_Error VL53L0X::set_ref_spad_map(VL53L0X_DEV dev, uint8_t *p_ref_spad_array) |
johnAlexander | 0:c523920bcc09 | 2153 | { |
johnAlexander | 14:8320b5ff96fa | 2154 | VL53L0X_Error status = vl53l0x_write_multi(dev, |
johnAlexander | 14:8320b5ff96fa | 2155 | VL53L0X_REG_GLOBAL_CONFIG_SPAD_ENABLES_REF_0, |
johnAlexander | 14:8320b5ff96fa | 2156 | p_ref_spad_array, 6); |
johnAlexander | 14:8320b5ff96fa | 2157 | |
johnAlexander | 14:8320b5ff96fa | 2158 | return status; |
johnAlexander | 0:c523920bcc09 | 2159 | } |
johnAlexander | 0:c523920bcc09 | 2160 | |
johnAlexander | 14:8320b5ff96fa | 2161 | VL53L0X_Error VL53L0X::get_ref_spad_map(VL53L0X_DEV dev, uint8_t *p_ref_spad_array) |
johnAlexander | 0:c523920bcc09 | 2162 | { |
johnAlexander | 14:8320b5ff96fa | 2163 | VL53L0X_Error status = vl53l0x_read_multi(dev, |
johnAlexander | 14:8320b5ff96fa | 2164 | VL53L0X_REG_GLOBAL_CONFIG_SPAD_ENABLES_REF_0, |
johnAlexander | 14:8320b5ff96fa | 2165 | p_ref_spad_array, |
johnAlexander | 14:8320b5ff96fa | 2166 | 6); |
johnAlexander | 0:c523920bcc09 | 2167 | // VL53L0X_Error status = VL53L0X_ERROR_NONE; |
johnAlexander | 0:c523920bcc09 | 2168 | // uint8_t count=0; |
johnAlexander | 0:c523920bcc09 | 2169 | |
johnAlexander | 0:c523920bcc09 | 2170 | // for (count = 0; count < 6; count++) |
johnAlexander | 0:c523920bcc09 | 2171 | // status = VL53L0X_RdByte(Dev, (VL53L0X_REG_GLOBAL_CONFIG_SPAD_ENABLES_REF_0 + count), &refSpadArray[count]); |
johnAlexander | 14:8320b5ff96fa | 2172 | return status; |
johnAlexander | 0:c523920bcc09 | 2173 | } |
johnAlexander | 0:c523920bcc09 | 2174 | |
johnAlexander | 14:8320b5ff96fa | 2175 | VL53L0X_Error VL53L0X::enable_ref_spads(VL53L0X_DEV dev, |
johnAlexander | 14:8320b5ff96fa | 2176 | uint8_t aperture_spads, |
johnAlexander | 14:8320b5ff96fa | 2177 | uint8_t good_spad_array[], |
johnAlexander | 14:8320b5ff96fa | 2178 | uint8_t spad_array[], |
johnAlexander | 14:8320b5ff96fa | 2179 | uint32_t size, |
johnAlexander | 14:8320b5ff96fa | 2180 | uint32_t start, |
johnAlexander | 14:8320b5ff96fa | 2181 | uint32_t offset, |
johnAlexander | 14:8320b5ff96fa | 2182 | uint32_t spad_count, |
johnAlexander | 14:8320b5ff96fa | 2183 | uint32_t *p_last_spad) |
johnAlexander | 0:c523920bcc09 | 2184 | { |
johnAlexander | 14:8320b5ff96fa | 2185 | VL53L0X_Error status = VL53L0X_ERROR_NONE; |
johnAlexander | 14:8320b5ff96fa | 2186 | uint32_t index; |
johnAlexander | 14:8320b5ff96fa | 2187 | uint32_t i; |
johnAlexander | 14:8320b5ff96fa | 2188 | int32_t next_good_spad = offset; |
johnAlexander | 14:8320b5ff96fa | 2189 | uint32_t current_spad; |
johnAlexander | 14:8320b5ff96fa | 2190 | uint8_t check_spad_array[6]; |
johnAlexander | 14:8320b5ff96fa | 2191 | |
johnAlexander | 14:8320b5ff96fa | 2192 | /* |
johnAlexander | 14:8320b5ff96fa | 2193 | * This function takes in a spad array which may or may not have SPADS |
johnAlexander | 14:8320b5ff96fa | 2194 | * already enabled and appends from a given offset a requested number |
johnAlexander | 14:8320b5ff96fa | 2195 | * of new SPAD enables. The 'good spad map' is applied to |
johnAlexander | 14:8320b5ff96fa | 2196 | * determine the next SPADs to enable. |
johnAlexander | 14:8320b5ff96fa | 2197 | * |
johnAlexander | 14:8320b5ff96fa | 2198 | * This function applies to only aperture or only non-aperture spads. |
johnAlexander | 14:8320b5ff96fa | 2199 | * Checks are performed to ensure this. |
johnAlexander | 14:8320b5ff96fa | 2200 | */ |
johnAlexander | 14:8320b5ff96fa | 2201 | |
johnAlexander | 14:8320b5ff96fa | 2202 | current_spad = offset; |
johnAlexander | 14:8320b5ff96fa | 2203 | for (index = 0; index < spad_count; index++) { |
johnAlexander | 14:8320b5ff96fa | 2204 | get_next_good_spad(good_spad_array, size, current_spad, |
johnAlexander | 14:8320b5ff96fa | 2205 | &next_good_spad); |
johnAlexander | 14:8320b5ff96fa | 2206 | |
johnAlexander | 14:8320b5ff96fa | 2207 | if (next_good_spad == -1) { |
johnAlexander | 14:8320b5ff96fa | 2208 | status = VL53L0X_ERROR_REF_SPAD_INIT; |
johnAlexander | 14:8320b5ff96fa | 2209 | break; |
johnAlexander | 14:8320b5ff96fa | 2210 | } |
johnAlexander | 14:8320b5ff96fa | 2211 | |
johnAlexander | 14:8320b5ff96fa | 2212 | /* Confirm that the next good SPAD is non-aperture */ |
johnAlexander | 14:8320b5ff96fa | 2213 | if (is_aperture(start + next_good_spad) != aperture_spads) { |
johnAlexander | 14:8320b5ff96fa | 2214 | /* if we can't get the required number of good aperture |
johnAlexander | 14:8320b5ff96fa | 2215 | * spads from the current quadrant then this is an error |
johnAlexander | 14:8320b5ff96fa | 2216 | */ |
johnAlexander | 14:8320b5ff96fa | 2217 | status = VL53L0X_ERROR_REF_SPAD_INIT; |
johnAlexander | 14:8320b5ff96fa | 2218 | break; |
johnAlexander | 14:8320b5ff96fa | 2219 | } |
johnAlexander | 14:8320b5ff96fa | 2220 | current_spad = (uint32_t)next_good_spad; |
johnAlexander | 14:8320b5ff96fa | 2221 | enable_spad_bit(spad_array, size, current_spad); |
johnAlexander | 14:8320b5ff96fa | 2222 | current_spad++; |
johnAlexander | 14:8320b5ff96fa | 2223 | } |
johnAlexander | 14:8320b5ff96fa | 2224 | *p_last_spad = current_spad; |
johnAlexander | 14:8320b5ff96fa | 2225 | |
johnAlexander | 14:8320b5ff96fa | 2226 | if (status == VL53L0X_ERROR_NONE) |
johnAlexander | 14:8320b5ff96fa | 2227 | status = set_ref_spad_map(dev, spad_array); |
johnAlexander | 14:8320b5ff96fa | 2228 | |
johnAlexander | 14:8320b5ff96fa | 2229 | |
johnAlexander | 14:8320b5ff96fa | 2230 | if (status == VL53L0X_ERROR_NONE) { |
johnAlexander | 14:8320b5ff96fa | 2231 | status = get_ref_spad_map(dev, check_spad_array); |
johnAlexander | 14:8320b5ff96fa | 2232 | |
johnAlexander | 14:8320b5ff96fa | 2233 | i = 0; |
johnAlexander | 14:8320b5ff96fa | 2234 | |
johnAlexander | 14:8320b5ff96fa | 2235 | /* Compare spad maps. If not equal report error. */ |
johnAlexander | 14:8320b5ff96fa | 2236 | while (i < size) { |
johnAlexander | 14:8320b5ff96fa | 2237 | if (spad_array[i] != check_spad_array[i]) { |
johnAlexander | 14:8320b5ff96fa | 2238 | status = VL53L0X_ERROR_REF_SPAD_INIT; |
johnAlexander | 14:8320b5ff96fa | 2239 | break; |
johnAlexander | 14:8320b5ff96fa | 2240 | } |
johnAlexander | 14:8320b5ff96fa | 2241 | i++; |
johnAlexander | 14:8320b5ff96fa | 2242 | } |
johnAlexander | 14:8320b5ff96fa | 2243 | } |
johnAlexander | 14:8320b5ff96fa | 2244 | return status; |
johnAlexander | 0:c523920bcc09 | 2245 | } |
johnAlexander | 0:c523920bcc09 | 2246 | |
johnAlexander | 14:8320b5ff96fa | 2247 | VL53L0X_Error VL53L0X::vl53l0x_set_device_mode(VL53L0X_DEV dev, VL53L0X_DeviceModes device_mode) |
johnAlexander | 0:c523920bcc09 | 2248 | { |
johnAlexander | 14:8320b5ff96fa | 2249 | VL53L0X_Error status = VL53L0X_ERROR_NONE; |
johnAlexander | 14:8320b5ff96fa | 2250 | |
johnAlexander | 14:8320b5ff96fa | 2251 | LOG_FUNCTION_START("%d", (int)DeviceMode); |
johnAlexander | 14:8320b5ff96fa | 2252 | |
johnAlexander | 14:8320b5ff96fa | 2253 | switch (device_mode) { |
johnAlexander | 14:8320b5ff96fa | 2254 | case VL53L0X_DEVICEMODE_SINGLE_RANGING: |
johnAlexander | 14:8320b5ff96fa | 2255 | case VL53L0X_DEVICEMODE_CONTINUOUS_RANGING: |
johnAlexander | 14:8320b5ff96fa | 2256 | case VL53L0X_DEVICEMODE_CONTINUOUS_TIMED_RANGING: |
johnAlexander | 14:8320b5ff96fa | 2257 | case VL53L0X_DEVICEMODE_GPIO_DRIVE: |
johnAlexander | 14:8320b5ff96fa | 2258 | case VL53L0X_DEVICEMODE_GPIO_OSC: |
johnAlexander | 14:8320b5ff96fa | 2259 | /* Supported modes */ |
johnAlexander | 14:8320b5ff96fa | 2260 | VL53L0X_SETPARAMETERFIELD(dev, DeviceMode, device_mode); |
johnAlexander | 14:8320b5ff96fa | 2261 | break; |
johnAlexander | 14:8320b5ff96fa | 2262 | default: |
johnAlexander | 14:8320b5ff96fa | 2263 | /* Unsupported mode */ |
johnAlexander | 14:8320b5ff96fa | 2264 | status = VL53L0X_ERROR_MODE_NOT_SUPPORTED; |
johnAlexander | 14:8320b5ff96fa | 2265 | } |
johnAlexander | 14:8320b5ff96fa | 2266 | |
johnAlexander | 14:8320b5ff96fa | 2267 | LOG_FUNCTION_END(status); |
johnAlexander | 14:8320b5ff96fa | 2268 | return status; |
johnAlexander | 0:c523920bcc09 | 2269 | } |
johnAlexander | 0:c523920bcc09 | 2270 | |
johnAlexander | 14:8320b5ff96fa | 2271 | VL53L0X_Error VL53L0X::vl53l0x_set_interrupt_thresholds(VL53L0X_DEV dev, |
johnAlexander | 14:8320b5ff96fa | 2272 | VL53L0X_DeviceModes device_mode, FixPoint1616_t threshold_low, |
johnAlexander | 14:8320b5ff96fa | 2273 | FixPoint1616_t threshold_high) |
johnAlexander | 13:615f7e38568c | 2274 | { |
johnAlexander | 14:8320b5ff96fa | 2275 | VL53L0X_Error status = VL53L0X_ERROR_NONE; |
johnAlexander | 14:8320b5ff96fa | 2276 | uint16_t threshold16; |
johnAlexander | 14:8320b5ff96fa | 2277 | LOG_FUNCTION_START(""); |
johnAlexander | 14:8320b5ff96fa | 2278 | |
johnAlexander | 14:8320b5ff96fa | 2279 | /* no dependency on DeviceMode for Ewok */ |
johnAlexander | 14:8320b5ff96fa | 2280 | /* Need to divide by 2 because the FW will apply a x2 */ |
johnAlexander | 14:8320b5ff96fa | 2281 | threshold16 = (uint16_t)((threshold_low >> 17) & 0x00fff); |
johnAlexander | 14:8320b5ff96fa | 2282 | status = vl53l0x_write_word(dev, VL53L0X_REG_SYSTEM_THRESH_LOW, threshold16); |
johnAlexander | 14:8320b5ff96fa | 2283 | |
johnAlexander | 14:8320b5ff96fa | 2284 | if (status == VL53L0X_ERROR_NONE) { |
johnAlexander | 14:8320b5ff96fa | 2285 | /* Need to divide by 2 because the FW will apply a x2 */ |
johnAlexander | 14:8320b5ff96fa | 2286 | threshold16 = (uint16_t)((threshold_high >> 17) & 0x00fff); |
johnAlexander | 14:8320b5ff96fa | 2287 | status = vl53l0x_write_word(dev, VL53L0X_REG_SYSTEM_THRESH_HIGH, |
johnAlexander | 14:8320b5ff96fa | 2288 | threshold16); |
johnAlexander | 14:8320b5ff96fa | 2289 | } |
johnAlexander | 14:8320b5ff96fa | 2290 | |
johnAlexander | 14:8320b5ff96fa | 2291 | LOG_FUNCTION_END(status); |
johnAlexander | 14:8320b5ff96fa | 2292 | return status; |
johnAlexander | 13:615f7e38568c | 2293 | } |
johnAlexander | 13:615f7e38568c | 2294 | |
johnAlexander | 14:8320b5ff96fa | 2295 | VL53L0X_Error VL53L0X::vl53l0x_get_interrupt_thresholds(VL53L0X_DEV dev, |
johnAlexander | 14:8320b5ff96fa | 2296 | VL53L0X_DeviceModes device_mode, FixPoint1616_t *p_threshold_low, |
johnAlexander | 14:8320b5ff96fa | 2297 | FixPoint1616_t *p_threshold_high) |
johnAlexander | 0:c523920bcc09 | 2298 | { |
johnAlexander | 14:8320b5ff96fa | 2299 | VL53L0X_Error status = VL53L0X_ERROR_NONE; |
johnAlexander | 14:8320b5ff96fa | 2300 | uint16_t threshold16; |
johnAlexander | 14:8320b5ff96fa | 2301 | LOG_FUNCTION_START(""); |
johnAlexander | 14:8320b5ff96fa | 2302 | |
johnAlexander | 14:8320b5ff96fa | 2303 | /* no dependency on DeviceMode for Ewok */ |
johnAlexander | 14:8320b5ff96fa | 2304 | |
johnAlexander | 14:8320b5ff96fa | 2305 | status = vl53l0x_read_word(dev, VL53L0X_REG_SYSTEM_THRESH_LOW, &threshold16); |
johnAlexander | 14:8320b5ff96fa | 2306 | /* Need to multiply by 2 because the FW will apply a x2 */ |
johnAlexander | 14:8320b5ff96fa | 2307 | *p_threshold_low = (FixPoint1616_t)((0x00fff & threshold16) << 17); |
johnAlexander | 14:8320b5ff96fa | 2308 | |
johnAlexander | 14:8320b5ff96fa | 2309 | if (status == VL53L0X_ERROR_NONE) { |
johnAlexander | 14:8320b5ff96fa | 2310 | status = vl53l0x_read_word(dev, VL53L0X_REG_SYSTEM_THRESH_HIGH, |
johnAlexander | 14:8320b5ff96fa | 2311 | &threshold16); |
johnAlexander | 14:8320b5ff96fa | 2312 | /* Need to multiply by 2 because the FW will apply a x2 */ |
johnAlexander | 14:8320b5ff96fa | 2313 | *p_threshold_high = |
johnAlexander | 14:8320b5ff96fa | 2314 | (FixPoint1616_t)((0x00fff & threshold16) << 17); |
johnAlexander | 14:8320b5ff96fa | 2315 | } |
johnAlexander | 14:8320b5ff96fa | 2316 | |
johnAlexander | 14:8320b5ff96fa | 2317 | LOG_FUNCTION_END(status); |
johnAlexander | 14:8320b5ff96fa | 2318 | return status; |
johnAlexander | 0:c523920bcc09 | 2319 | } |
johnAlexander | 0:c523920bcc09 | 2320 | |
johnAlexander | 14:8320b5ff96fa | 2321 | VL53L0X_Error VL53L0X::vl53l0x_load_tuning_settings(VL53L0X_DEV dev, |
johnAlexander | 14:8320b5ff96fa | 2322 | uint8_t *p_tuning_setting_buffer) |
johnAlexander | 0:c523920bcc09 | 2323 | { |
johnAlexander | 14:8320b5ff96fa | 2324 | VL53L0X_Error status = VL53L0X_ERROR_NONE; |
johnAlexander | 14:8320b5ff96fa | 2325 | int i; |
johnAlexander | 14:8320b5ff96fa | 2326 | int index; |
johnAlexander | 14:8320b5ff96fa | 2327 | uint8_t msb; |
johnAlexander | 14:8320b5ff96fa | 2328 | uint8_t lsb; |
johnAlexander | 14:8320b5ff96fa | 2329 | uint8_t select_param; |
johnAlexander | 14:8320b5ff96fa | 2330 | uint8_t number_of_writes; |
johnAlexander | 14:8320b5ff96fa | 2331 | uint8_t address; |
johnAlexander | 14:8320b5ff96fa | 2332 | uint8_t local_buffer[4]; /* max */ |
johnAlexander | 14:8320b5ff96fa | 2333 | uint16_t temp16; |
johnAlexander | 14:8320b5ff96fa | 2334 | |
johnAlexander | 14:8320b5ff96fa | 2335 | LOG_FUNCTION_START(""); |
johnAlexander | 14:8320b5ff96fa | 2336 | |
johnAlexander | 14:8320b5ff96fa | 2337 | index = 0; |
johnAlexander | 14:8320b5ff96fa | 2338 | |
johnAlexander | 14:8320b5ff96fa | 2339 | while ((*(p_tuning_setting_buffer + index) != 0) && |
johnAlexander | 14:8320b5ff96fa | 2340 | (status == VL53L0X_ERROR_NONE)) { |
johnAlexander | 14:8320b5ff96fa | 2341 | number_of_writes = *(p_tuning_setting_buffer + index); |
johnAlexander | 14:8320b5ff96fa | 2342 | index++; |
johnAlexander | 14:8320b5ff96fa | 2343 | if (number_of_writes == 0xFF) { |
johnAlexander | 14:8320b5ff96fa | 2344 | /* internal parameters */ |
johnAlexander | 14:8320b5ff96fa | 2345 | select_param = *(p_tuning_setting_buffer + index); |
johnAlexander | 14:8320b5ff96fa | 2346 | index++; |
johnAlexander | 14:8320b5ff96fa | 2347 | switch (select_param) { |
johnAlexander | 14:8320b5ff96fa | 2348 | case 0: /* uint16_t SigmaEstRefArray -> 2 bytes */ |
johnAlexander | 14:8320b5ff96fa | 2349 | msb = *(p_tuning_setting_buffer + index); |
johnAlexander | 14:8320b5ff96fa | 2350 | index++; |
johnAlexander | 14:8320b5ff96fa | 2351 | lsb = *(p_tuning_setting_buffer + index); |
johnAlexander | 14:8320b5ff96fa | 2352 | index++; |
johnAlexander | 14:8320b5ff96fa | 2353 | temp16 = VL53L0X_MAKEUINT16(lsb, msb); |
johnAlexander | 14:8320b5ff96fa | 2354 | PALDevDataSet(dev, SigmaEstRefArray, temp16); |
johnAlexander | 14:8320b5ff96fa | 2355 | break; |
johnAlexander | 14:8320b5ff96fa | 2356 | case 1: /* uint16_t SigmaEstEffPulseWidth -> 2 bytes */ |
johnAlexander | 14:8320b5ff96fa | 2357 | msb = *(p_tuning_setting_buffer + index); |
johnAlexander | 14:8320b5ff96fa | 2358 | index++; |
johnAlexander | 14:8320b5ff96fa | 2359 | lsb = *(p_tuning_setting_buffer + index); |
johnAlexander | 14:8320b5ff96fa | 2360 | index++; |
johnAlexander | 14:8320b5ff96fa | 2361 | temp16 = VL53L0X_MAKEUINT16(lsb, msb); |
johnAlexander | 14:8320b5ff96fa | 2362 | PALDevDataSet(dev, SigmaEstEffPulseWidth, |
johnAlexander | 14:8320b5ff96fa | 2363 | temp16); |
johnAlexander | 14:8320b5ff96fa | 2364 | break; |
johnAlexander | 14:8320b5ff96fa | 2365 | case 2: /* uint16_t SigmaEstEffAmbWidth -> 2 bytes */ |
johnAlexander | 14:8320b5ff96fa | 2366 | msb = *(p_tuning_setting_buffer + index); |
johnAlexander | 14:8320b5ff96fa | 2367 | index++; |
johnAlexander | 14:8320b5ff96fa | 2368 | lsb = *(p_tuning_setting_buffer + index); |
johnAlexander | 14:8320b5ff96fa | 2369 | index++; |
johnAlexander | 14:8320b5ff96fa | 2370 | temp16 = VL53L0X_MAKEUINT16(lsb, msb); |
johnAlexander | 14:8320b5ff96fa | 2371 | PALDevDataSet(dev, SigmaEstEffAmbWidth, temp16); |
johnAlexander | 14:8320b5ff96fa | 2372 | break; |
johnAlexander | 14:8320b5ff96fa | 2373 | case 3: /* uint16_t targetRefRate -> 2 bytes */ |
johnAlexander | 14:8320b5ff96fa | 2374 | msb = *(p_tuning_setting_buffer + index); |
johnAlexander | 14:8320b5ff96fa | 2375 | index++; |
johnAlexander | 14:8320b5ff96fa | 2376 | lsb = *(p_tuning_setting_buffer + index); |
johnAlexander | 14:8320b5ff96fa | 2377 | index++; |
johnAlexander | 14:8320b5ff96fa | 2378 | temp16 = VL53L0X_MAKEUINT16(lsb, msb); |
johnAlexander | 14:8320b5ff96fa | 2379 | PALDevDataSet(dev, targetRefRate, temp16); |
johnAlexander | 14:8320b5ff96fa | 2380 | break; |
johnAlexander | 14:8320b5ff96fa | 2381 | default: /* invalid parameter */ |
johnAlexander | 14:8320b5ff96fa | 2382 | status = VL53L0X_ERROR_INVALID_PARAMS; |
johnAlexander | 14:8320b5ff96fa | 2383 | } |
johnAlexander | 14:8320b5ff96fa | 2384 | |
johnAlexander | 14:8320b5ff96fa | 2385 | } else if (number_of_writes <= 4) { |
johnAlexander | 14:8320b5ff96fa | 2386 | address = *(p_tuning_setting_buffer + index); |
johnAlexander | 14:8320b5ff96fa | 2387 | index++; |
johnAlexander | 14:8320b5ff96fa | 2388 | |
johnAlexander | 14:8320b5ff96fa | 2389 | for (i = 0; i < number_of_writes; i++) { |
johnAlexander | 14:8320b5ff96fa | 2390 | local_buffer[i] = *(p_tuning_setting_buffer + |
johnAlexander | 14:8320b5ff96fa | 2391 | index); |
johnAlexander | 14:8320b5ff96fa | 2392 | index++; |
johnAlexander | 14:8320b5ff96fa | 2393 | } |
johnAlexander | 14:8320b5ff96fa | 2394 | |
johnAlexander | 14:8320b5ff96fa | 2395 | status = vl53l0x_write_multi(dev, address, local_buffer, |
johnAlexander | 14:8320b5ff96fa | 2396 | number_of_writes); |
johnAlexander | 14:8320b5ff96fa | 2397 | |
johnAlexander | 14:8320b5ff96fa | 2398 | } else { |
johnAlexander | 14:8320b5ff96fa | 2399 | status = VL53L0X_ERROR_INVALID_PARAMS; |
johnAlexander | 14:8320b5ff96fa | 2400 | } |
johnAlexander | 14:8320b5ff96fa | 2401 | } |
johnAlexander | 14:8320b5ff96fa | 2402 | |
johnAlexander | 14:8320b5ff96fa | 2403 | LOG_FUNCTION_END(status); |
johnAlexander | 14:8320b5ff96fa | 2404 | return status; |
johnAlexander | 0:c523920bcc09 | 2405 | } |
johnAlexander | 0:c523920bcc09 | 2406 | |
johnAlexander | 14:8320b5ff96fa | 2407 | VL53L0X_Error VL53L0X::vl53l0x_check_and_load_interrupt_settings(VL53L0X_DEV dev, |
johnAlexander | 14:8320b5ff96fa | 2408 | uint8_t start_not_stopflag) |
johnAlexander | 0:c523920bcc09 | 2409 | { |
johnAlexander | 14:8320b5ff96fa | 2410 | uint8_t interrupt_config; |
johnAlexander | 14:8320b5ff96fa | 2411 | FixPoint1616_t threshold_low; |
johnAlexander | 14:8320b5ff96fa | 2412 | FixPoint1616_t threshold_high; |
johnAlexander | 14:8320b5ff96fa | 2413 | VL53L0X_Error status = VL53L0X_ERROR_NONE; |
johnAlexander | 14:8320b5ff96fa | 2414 | |
johnAlexander | 14:8320b5ff96fa | 2415 | interrupt_config = VL53L0X_GETDEVICESPECIFICPARAMETER(dev, |
johnAlexander | 14:8320b5ff96fa | 2416 | Pin0GpioFunctionality); |
johnAlexander | 14:8320b5ff96fa | 2417 | |
johnAlexander | 14:8320b5ff96fa | 2418 | if ((interrupt_config == |
johnAlexander | 14:8320b5ff96fa | 2419 | VL53L0X_GPIOFUNCTIONALITY_THRESHOLD_CROSSED_LOW) || |
johnAlexander | 14:8320b5ff96fa | 2420 | (interrupt_config == |
johnAlexander | 14:8320b5ff96fa | 2421 | VL53L0X_GPIOFUNCTIONALITY_THRESHOLD_CROSSED_HIGH) || |
johnAlexander | 14:8320b5ff96fa | 2422 | (interrupt_config == |
johnAlexander | 14:8320b5ff96fa | 2423 | VL53L0X_GPIOFUNCTIONALITY_THRESHOLD_CROSSED_OUT)) { |
johnAlexander | 14:8320b5ff96fa | 2424 | |
johnAlexander | 14:8320b5ff96fa | 2425 | status = vl53l0x_get_interrupt_thresholds(dev, |
johnAlexander | 14:8320b5ff96fa | 2426 | VL53L0X_DEVICEMODE_CONTINUOUS_RANGING, |
johnAlexander | 14:8320b5ff96fa | 2427 | &threshold_low, &threshold_high); |
johnAlexander | 14:8320b5ff96fa | 2428 | |
johnAlexander | 14:8320b5ff96fa | 2429 | if (((threshold_low > 255 * 65536) || |
johnAlexander | 14:8320b5ff96fa | 2430 | (threshold_high > 255 * 65536)) && |
johnAlexander | 14:8320b5ff96fa | 2431 | (status == VL53L0X_ERROR_NONE)) { |
johnAlexander | 14:8320b5ff96fa | 2432 | |
johnAlexander | 14:8320b5ff96fa | 2433 | if (start_not_stopflag != 0) { |
johnAlexander | 14:8320b5ff96fa | 2434 | status = vl53l0x_load_tuning_settings(dev, |
johnAlexander | 14:8320b5ff96fa | 2435 | InterruptThresholdSettings); |
johnAlexander | 14:8320b5ff96fa | 2436 | } else { |
johnAlexander | 14:8320b5ff96fa | 2437 | status |= vl53l0x_write_byte(dev, 0xFF, 0x04); |
johnAlexander | 14:8320b5ff96fa | 2438 | status |= vl53l0x_write_byte(dev, 0x70, 0x00); |
johnAlexander | 14:8320b5ff96fa | 2439 | status |= vl53l0x_write_byte(dev, 0xFF, 0x00); |
johnAlexander | 14:8320b5ff96fa | 2440 | status |= vl53l0x_write_byte(dev, 0x80, 0x00); |
johnAlexander | 14:8320b5ff96fa | 2441 | } |
johnAlexander | 14:8320b5ff96fa | 2442 | |
johnAlexander | 14:8320b5ff96fa | 2443 | } |
johnAlexander | 14:8320b5ff96fa | 2444 | |
johnAlexander | 14:8320b5ff96fa | 2445 | |
johnAlexander | 14:8320b5ff96fa | 2446 | } |
johnAlexander | 14:8320b5ff96fa | 2447 | |
johnAlexander | 14:8320b5ff96fa | 2448 | return status; |
johnAlexander | 0:c523920bcc09 | 2449 | |
johnAlexander | 0:c523920bcc09 | 2450 | } |
johnAlexander | 0:c523920bcc09 | 2451 | |
johnAlexander | 14:8320b5ff96fa | 2452 | VL53L0X_Error VL53L0X::vl53l0x_start_measurement(VL53L0X_DEV dev) |
johnAlexander | 0:c523920bcc09 | 2453 | { |
johnAlexander | 14:8320b5ff96fa | 2454 | VL53L0X_Error status = VL53L0X_ERROR_NONE; |
johnAlexander | 14:8320b5ff96fa | 2455 | VL53L0X_DeviceModes device_mode; |
johnAlexander | 14:8320b5ff96fa | 2456 | uint8_t byte; |
johnAlexander | 14:8320b5ff96fa | 2457 | uint8_t start_stop_byte = VL53L0X_REG_SYSRANGE_MODE_START_STOP; |
johnAlexander | 14:8320b5ff96fa | 2458 | uint32_t loop_nb; |
johnAlexander | 14:8320b5ff96fa | 2459 | LOG_FUNCTION_START(""); |
johnAlexander | 14:8320b5ff96fa | 2460 | |
johnAlexander | 14:8320b5ff96fa | 2461 | /* Get Current DeviceMode */ |
johnAlexander | 14:8320b5ff96fa | 2462 | vl53l0x_get_device_mode(dev, &device_mode); |
johnAlexander | 14:8320b5ff96fa | 2463 | |
johnAlexander | 14:8320b5ff96fa | 2464 | status = vl53l0x_write_byte(dev, 0x80, 0x01); |
johnAlexander | 14:8320b5ff96fa | 2465 | status = vl53l0x_write_byte(dev, 0xFF, 0x01); |
johnAlexander | 14:8320b5ff96fa | 2466 | status = vl53l0x_write_byte(dev, 0x00, 0x00); |
johnAlexander | 14:8320b5ff96fa | 2467 | status = vl53l0x_write_byte(dev, 0x91, PALDevDataGet(dev, StopVariable)); |
johnAlexander | 14:8320b5ff96fa | 2468 | status = vl53l0x_write_byte(dev, 0x00, 0x01); |
johnAlexander | 14:8320b5ff96fa | 2469 | status = vl53l0x_write_byte(dev, 0xFF, 0x00); |
johnAlexander | 14:8320b5ff96fa | 2470 | status = vl53l0x_write_byte(dev, 0x80, 0x00); |
johnAlexander | 14:8320b5ff96fa | 2471 | |
johnAlexander | 14:8320b5ff96fa | 2472 | switch (device_mode) { |
johnAlexander | 14:8320b5ff96fa | 2473 | case VL53L0X_DEVICEMODE_SINGLE_RANGING: |
johnAlexander | 14:8320b5ff96fa | 2474 | status = vl53l0x_write_byte(dev, VL53L0X_REG_SYSRANGE_START, 0x01); |
johnAlexander | 14:8320b5ff96fa | 2475 | |
johnAlexander | 14:8320b5ff96fa | 2476 | byte = start_stop_byte; |
johnAlexander | 14:8320b5ff96fa | 2477 | if (status == VL53L0X_ERROR_NONE) { |
johnAlexander | 14:8320b5ff96fa | 2478 | /* Wait until start bit has been cleared */ |
johnAlexander | 14:8320b5ff96fa | 2479 | loop_nb = 0; |
johnAlexander | 14:8320b5ff96fa | 2480 | do { |
johnAlexander | 14:8320b5ff96fa | 2481 | if (loop_nb > 0) |
johnAlexander | 14:8320b5ff96fa | 2482 | status = vl53l0x_read_byte(dev, |
johnAlexander | 14:8320b5ff96fa | 2483 | VL53L0X_REG_SYSRANGE_START, &byte); |
johnAlexander | 14:8320b5ff96fa | 2484 | loop_nb = loop_nb + 1; |
johnAlexander | 14:8320b5ff96fa | 2485 | } while (((byte & start_stop_byte) == start_stop_byte) |
johnAlexander | 14:8320b5ff96fa | 2486 | && (status == VL53L0X_ERROR_NONE) |
johnAlexander | 14:8320b5ff96fa | 2487 | && (loop_nb < VL53L0X_DEFAULT_MAX_LOOP)); |
johnAlexander | 14:8320b5ff96fa | 2488 | |
johnAlexander | 14:8320b5ff96fa | 2489 | if (loop_nb >= VL53L0X_DEFAULT_MAX_LOOP) |
johnAlexander | 14:8320b5ff96fa | 2490 | status = VL53L0X_ERROR_TIME_OUT; |
johnAlexander | 14:8320b5ff96fa | 2491 | |
johnAlexander | 14:8320b5ff96fa | 2492 | } |
johnAlexander | 14:8320b5ff96fa | 2493 | |
johnAlexander | 14:8320b5ff96fa | 2494 | break; |
johnAlexander | 14:8320b5ff96fa | 2495 | case VL53L0X_DEVICEMODE_CONTINUOUS_RANGING: |
johnAlexander | 14:8320b5ff96fa | 2496 | /* Back-to-back mode */ |
johnAlexander | 14:8320b5ff96fa | 2497 | |
johnAlexander | 14:8320b5ff96fa | 2498 | /* Check if need to apply interrupt settings */ |
johnAlexander | 14:8320b5ff96fa | 2499 | if (status == VL53L0X_ERROR_NONE) |
johnAlexander | 14:8320b5ff96fa | 2500 | status = vl53l0x_check_and_load_interrupt_settings(dev, 1); |
johnAlexander | 14:8320b5ff96fa | 2501 | |
johnAlexander | 14:8320b5ff96fa | 2502 | status = vl53l0x_write_byte(dev, |
johnAlexander | 14:8320b5ff96fa | 2503 | VL53L0X_REG_SYSRANGE_START, |
johnAlexander | 14:8320b5ff96fa | 2504 | VL53L0X_REG_SYSRANGE_MODE_BACKTOBACK); |
johnAlexander | 14:8320b5ff96fa | 2505 | if (status == VL53L0X_ERROR_NONE) { |
johnAlexander | 14:8320b5ff96fa | 2506 | /* Set PAL State to Running */ |
johnAlexander | 14:8320b5ff96fa | 2507 | PALDevDataSet(dev, PalState, VL53L0X_STATE_RUNNING); |
johnAlexander | 14:8320b5ff96fa | 2508 | } |
johnAlexander | 14:8320b5ff96fa | 2509 | break; |
johnAlexander | 14:8320b5ff96fa | 2510 | case VL53L0X_DEVICEMODE_CONTINUOUS_TIMED_RANGING: |
johnAlexander | 14:8320b5ff96fa | 2511 | /* Continuous mode */ |
johnAlexander | 14:8320b5ff96fa | 2512 | /* Check if need to apply interrupt settings */ |
johnAlexander | 14:8320b5ff96fa | 2513 | if (status == VL53L0X_ERROR_NONE) |
johnAlexander | 14:8320b5ff96fa | 2514 | status = vl53l0x_check_and_load_interrupt_settings(dev, 1); |
johnAlexander | 14:8320b5ff96fa | 2515 | |
johnAlexander | 14:8320b5ff96fa | 2516 | status = vl53l0x_write_byte(dev, |
johnAlexander | 14:8320b5ff96fa | 2517 | VL53L0X_REG_SYSRANGE_START, |
johnAlexander | 14:8320b5ff96fa | 2518 | VL53L0X_REG_SYSRANGE_MODE_TIMED); |
johnAlexander | 14:8320b5ff96fa | 2519 | |
johnAlexander | 14:8320b5ff96fa | 2520 | if (status == VL53L0X_ERROR_NONE) { |
johnAlexander | 14:8320b5ff96fa | 2521 | /* Set PAL State to Running */ |
johnAlexander | 14:8320b5ff96fa | 2522 | PALDevDataSet(dev, PalState, VL53L0X_STATE_RUNNING); |
johnAlexander | 14:8320b5ff96fa | 2523 | } |
johnAlexander | 14:8320b5ff96fa | 2524 | break; |
johnAlexander | 14:8320b5ff96fa | 2525 | default: |
johnAlexander | 14:8320b5ff96fa | 2526 | /* Selected mode not supported */ |
johnAlexander | 14:8320b5ff96fa | 2527 | status = VL53L0X_ERROR_MODE_NOT_SUPPORTED; |
johnAlexander | 14:8320b5ff96fa | 2528 | } |
johnAlexander | 14:8320b5ff96fa | 2529 | |
johnAlexander | 14:8320b5ff96fa | 2530 | |
johnAlexander | 14:8320b5ff96fa | 2531 | LOG_FUNCTION_END(status); |
johnAlexander | 14:8320b5ff96fa | 2532 | return status; |
johnAlexander | 0:c523920bcc09 | 2533 | } |
johnAlexander | 0:c523920bcc09 | 2534 | |
johnAlexander | 0:c523920bcc09 | 2535 | /* Group PAL Measurement Functions */ |
johnAlexander | 14:8320b5ff96fa | 2536 | VL53L0X_Error VL53L0X::vl53l0x_perform_single_measurement(VL53L0X_DEV dev) |
johnAlexander | 0:c523920bcc09 | 2537 | { |
johnAlexander | 14:8320b5ff96fa | 2538 | VL53L0X_Error status = VL53L0X_ERROR_NONE; |
johnAlexander | 14:8320b5ff96fa | 2539 | VL53L0X_DeviceModes device_mode; |
johnAlexander | 14:8320b5ff96fa | 2540 | |
johnAlexander | 14:8320b5ff96fa | 2541 | LOG_FUNCTION_START(""); |
johnAlexander | 14:8320b5ff96fa | 2542 | |
johnAlexander | 14:8320b5ff96fa | 2543 | /* Get Current DeviceMode */ |
johnAlexander | 14:8320b5ff96fa | 2544 | status = vl53l0x_get_device_mode(dev, &device_mode); |
johnAlexander | 14:8320b5ff96fa | 2545 | |
johnAlexander | 14:8320b5ff96fa | 2546 | /* Start immediately to run a single ranging measurement in case of |
johnAlexander | 14:8320b5ff96fa | 2547 | * single ranging or single histogram */ |
johnAlexander | 14:8320b5ff96fa | 2548 | if (status == VL53L0X_ERROR_NONE |
johnAlexander | 14:8320b5ff96fa | 2549 | && device_mode == VL53L0X_DEVICEMODE_SINGLE_RANGING) |
johnAlexander | 14:8320b5ff96fa | 2550 | status = vl53l0x_start_measurement(dev); |
johnAlexander | 14:8320b5ff96fa | 2551 | |
johnAlexander | 14:8320b5ff96fa | 2552 | |
johnAlexander | 14:8320b5ff96fa | 2553 | if (status == VL53L0X_ERROR_NONE) |
johnAlexander | 14:8320b5ff96fa | 2554 | status = vl53l0x_measurement_poll_for_completion(dev); |
johnAlexander | 14:8320b5ff96fa | 2555 | |
johnAlexander | 14:8320b5ff96fa | 2556 | |
johnAlexander | 14:8320b5ff96fa | 2557 | /* Change PAL State in case of single ranging or single histogram */ |
johnAlexander | 14:8320b5ff96fa | 2558 | if (status == VL53L0X_ERROR_NONE |
johnAlexander | 14:8320b5ff96fa | 2559 | && device_mode == VL53L0X_DEVICEMODE_SINGLE_RANGING) |
johnAlexander | 14:8320b5ff96fa | 2560 | PALDevDataSet(dev, PalState, VL53L0X_STATE_IDLE); |
johnAlexander | 14:8320b5ff96fa | 2561 | |
johnAlexander | 14:8320b5ff96fa | 2562 | |
johnAlexander | 14:8320b5ff96fa | 2563 | LOG_FUNCTION_END(status); |
johnAlexander | 14:8320b5ff96fa | 2564 | return status; |
johnAlexander | 0:c523920bcc09 | 2565 | } |
johnAlexander | 0:c523920bcc09 | 2566 | |
johnAlexander | 14:8320b5ff96fa | 2567 | VL53L0X_Error VL53L0X::vl53l0x_get_x_talk_compensation_enable(VL53L0X_DEV dev, |
johnAlexander | 14:8320b5ff96fa | 2568 | uint8_t *p_x_talk_compensation_enable) |
johnAlexander | 0:c523920bcc09 | 2569 | { |
johnAlexander | 14:8320b5ff96fa | 2570 | VL53L0X_Error status = VL53L0X_ERROR_NONE; |
johnAlexander | 14:8320b5ff96fa | 2571 | uint8_t temp8; |
johnAlexander | 14:8320b5ff96fa | 2572 | LOG_FUNCTION_START(""); |
johnAlexander | 14:8320b5ff96fa | 2573 | |
johnAlexander | 14:8320b5ff96fa | 2574 | VL53L0X_GETPARAMETERFIELD(dev, XTalkCompensationEnable, temp8); |
johnAlexander | 14:8320b5ff96fa | 2575 | *p_x_talk_compensation_enable = temp8; |
johnAlexander | 14:8320b5ff96fa | 2576 | |
johnAlexander | 14:8320b5ff96fa | 2577 | LOG_FUNCTION_END(status); |
johnAlexander | 14:8320b5ff96fa | 2578 | return status; |
johnAlexander | 0:c523920bcc09 | 2579 | } |
johnAlexander | 0:c523920bcc09 | 2580 | |
johnAlexander | 14:8320b5ff96fa | 2581 | VL53L0X_Error VL53L0X::vl53l0x_get_total_xtalk_rate(VL53L0X_DEV dev, |
johnAlexander | 14:8320b5ff96fa | 2582 | VL53L0X_RangingMeasurementData_t *p_ranging_measurement_data, |
johnAlexander | 14:8320b5ff96fa | 2583 | FixPoint1616_t *p_total_xtalk_rate_mcps) |
johnAlexander | 0:c523920bcc09 | 2584 | { |
johnAlexander | 14:8320b5ff96fa | 2585 | VL53L0X_Error status = VL53L0X_ERROR_NONE; |
johnAlexander | 14:8320b5ff96fa | 2586 | |
johnAlexander | 14:8320b5ff96fa | 2587 | uint8_t xtalk_comp_enable; |
johnAlexander | 14:8320b5ff96fa | 2588 | FixPoint1616_t total_xtalk_mega_cps; |
johnAlexander | 14:8320b5ff96fa | 2589 | FixPoint1616_t xtalk_per_spad_mega_cps; |
johnAlexander | 14:8320b5ff96fa | 2590 | |
johnAlexander | 14:8320b5ff96fa | 2591 | *p_total_xtalk_rate_mcps = 0; |
johnAlexander | 14:8320b5ff96fa | 2592 | |
johnAlexander | 14:8320b5ff96fa | 2593 | status = vl53l0x_get_x_talk_compensation_enable(dev, &xtalk_comp_enable); |
johnAlexander | 14:8320b5ff96fa | 2594 | if (status == VL53L0X_ERROR_NONE) { |
johnAlexander | 14:8320b5ff96fa | 2595 | |
johnAlexander | 14:8320b5ff96fa | 2596 | if (xtalk_comp_enable) { |
johnAlexander | 14:8320b5ff96fa | 2597 | |
johnAlexander | 14:8320b5ff96fa | 2598 | VL53L0X_GETPARAMETERFIELD( |
johnAlexander | 14:8320b5ff96fa | 2599 | dev, |
johnAlexander | 14:8320b5ff96fa | 2600 | XTalkCompensationRateMegaCps, |
johnAlexander | 14:8320b5ff96fa | 2601 | xtalk_per_spad_mega_cps); |
johnAlexander | 14:8320b5ff96fa | 2602 | |
johnAlexander | 14:8320b5ff96fa | 2603 | /* FixPoint1616 * FixPoint 8:8 = FixPoint0824 */ |
johnAlexander | 14:8320b5ff96fa | 2604 | total_xtalk_mega_cps = |
johnAlexander | 14:8320b5ff96fa | 2605 | p_ranging_measurement_data->EffectiveSpadRtnCount * |
johnAlexander | 14:8320b5ff96fa | 2606 | xtalk_per_spad_mega_cps; |
johnAlexander | 14:8320b5ff96fa | 2607 | |
johnAlexander | 14:8320b5ff96fa | 2608 | /* FixPoint0824 >> 8 = FixPoint1616 */ |
johnAlexander | 14:8320b5ff96fa | 2609 | *p_total_xtalk_rate_mcps = |
johnAlexander | 14:8320b5ff96fa | 2610 | (total_xtalk_mega_cps + 0x80) >> 8; |
johnAlexander | 14:8320b5ff96fa | 2611 | } |
johnAlexander | 14:8320b5ff96fa | 2612 | } |
johnAlexander | 14:8320b5ff96fa | 2613 | |
johnAlexander | 14:8320b5ff96fa | 2614 | return status; |
johnAlexander | 0:c523920bcc09 | 2615 | } |
johnAlexander | 0:c523920bcc09 | 2616 | |
johnAlexander | 14:8320b5ff96fa | 2617 | VL53L0X_Error VL53L0X::vl53l0x_get_total_signal_rate(VL53L0X_DEV dev, |
johnAlexander | 14:8320b5ff96fa | 2618 | VL53L0X_RangingMeasurementData_t *p_ranging_measurement_data, |
johnAlexander | 14:8320b5ff96fa | 2619 | FixPoint1616_t *p_total_signal_rate_mcps) |
johnAlexander | 0:c523920bcc09 | 2620 | { |
johnAlexander | 14:8320b5ff96fa | 2621 | VL53L0X_Error status = VL53L0X_ERROR_NONE; |
johnAlexander | 14:8320b5ff96fa | 2622 | FixPoint1616_t total_xtalk_mega_cps; |
johnAlexander | 14:8320b5ff96fa | 2623 | |
johnAlexander | 14:8320b5ff96fa | 2624 | LOG_FUNCTION_START(""); |
johnAlexander | 14:8320b5ff96fa | 2625 | |
johnAlexander | 14:8320b5ff96fa | 2626 | *p_total_signal_rate_mcps = |
johnAlexander | 14:8320b5ff96fa | 2627 | p_ranging_measurement_data->SignalRateRtnMegaCps; |
johnAlexander | 14:8320b5ff96fa | 2628 | |
johnAlexander | 14:8320b5ff96fa | 2629 | status = vl53l0x_get_total_xtalk_rate( |
johnAlexander | 14:8320b5ff96fa | 2630 | dev, p_ranging_measurement_data, &total_xtalk_mega_cps); |
johnAlexander | 14:8320b5ff96fa | 2631 | |
johnAlexander | 14:8320b5ff96fa | 2632 | if (status == VL53L0X_ERROR_NONE) |
johnAlexander | 14:8320b5ff96fa | 2633 | *p_total_signal_rate_mcps += total_xtalk_mega_cps; |
johnAlexander | 14:8320b5ff96fa | 2634 | |
johnAlexander | 14:8320b5ff96fa | 2635 | return status; |
johnAlexander | 0:c523920bcc09 | 2636 | } |
johnAlexander | 0:c523920bcc09 | 2637 | |
johnAlexander | 0:c523920bcc09 | 2638 | /* To convert ms into register value */ |
johnAlexander | 14:8320b5ff96fa | 2639 | uint32_t VL53L0X::vl53l0x_calc_timeout_mclks(VL53L0X_DEV dev, |
johnAlexander | 14:8320b5ff96fa | 2640 | uint32_t timeout_period_us, |
johnAlexander | 14:8320b5ff96fa | 2641 | uint8_t vcsel_period_pclks) |
johnAlexander | 0:c523920bcc09 | 2642 | { |
johnAlexander | 14:8320b5ff96fa | 2643 | uint32_t macro_period_ps; |
johnAlexander | 14:8320b5ff96fa | 2644 | uint32_t macro_period_ns; |
johnAlexander | 14:8320b5ff96fa | 2645 | uint32_t timeout_period_mclks = 0; |
johnAlexander | 14:8320b5ff96fa | 2646 | |
johnAlexander | 14:8320b5ff96fa | 2647 | macro_period_ps = vl53l0x_calc_macro_period_ps(dev, vcsel_period_pclks); |
johnAlexander | 14:8320b5ff96fa | 2648 | macro_period_ns = (macro_period_ps + 500) / 1000; |
johnAlexander | 14:8320b5ff96fa | 2649 | |
johnAlexander | 14:8320b5ff96fa | 2650 | timeout_period_mclks = |
johnAlexander | 14:8320b5ff96fa | 2651 | (uint32_t)(((timeout_period_us * 1000) |
johnAlexander | 14:8320b5ff96fa | 2652 | + (macro_period_ns / 2)) / macro_period_ns); |
johnAlexander | 0:c523920bcc09 | 2653 | |
johnAlexander | 0:c523920bcc09 | 2654 | return timeout_period_mclks; |
johnAlexander | 0:c523920bcc09 | 2655 | } |
johnAlexander | 0:c523920bcc09 | 2656 | |
johnAlexander | 14:8320b5ff96fa | 2657 | uint32_t VL53L0X::vl53l0x_isqrt(uint32_t num) |
johnAlexander | 0:c523920bcc09 | 2658 | { |
johnAlexander | 14:8320b5ff96fa | 2659 | /* |
johnAlexander | 14:8320b5ff96fa | 2660 | * Implements an integer square root |
johnAlexander | 14:8320b5ff96fa | 2661 | * |
johnAlexander | 14:8320b5ff96fa | 2662 | * From: http://en.wikipedia.org/wiki/Methods_of_computing_square_roots |
johnAlexander | 14:8320b5ff96fa | 2663 | */ |
johnAlexander | 14:8320b5ff96fa | 2664 | |
johnAlexander | 14:8320b5ff96fa | 2665 | uint32_t res = 0; |
johnAlexander | 14:8320b5ff96fa | 2666 | uint32_t bit = 1 << 30; |
johnAlexander | 14:8320b5ff96fa | 2667 | /* The second-to-top bit is set: |
johnAlexander | 14:8320b5ff96fa | 2668 | * 1 << 14 for 16-bits, 1 << 30 for 32 bits */ |
johnAlexander | 14:8320b5ff96fa | 2669 | |
johnAlexander | 14:8320b5ff96fa | 2670 | /* "bit" starts at the highest power of four <= the argument. */ |
johnAlexander | 14:8320b5ff96fa | 2671 | while (bit > num) |
johnAlexander | 14:8320b5ff96fa | 2672 | bit >>= 2; |
johnAlexander | 14:8320b5ff96fa | 2673 | |
johnAlexander | 14:8320b5ff96fa | 2674 | |
johnAlexander | 14:8320b5ff96fa | 2675 | while (bit != 0) { |
johnAlexander | 14:8320b5ff96fa | 2676 | if (num >= res + bit) { |
johnAlexander | 14:8320b5ff96fa | 2677 | num -= res + bit; |
johnAlexander | 14:8320b5ff96fa | 2678 | res = (res >> 1) + bit; |
johnAlexander | 14:8320b5ff96fa | 2679 | } else |
johnAlexander | 14:8320b5ff96fa | 2680 | res >>= 1; |
johnAlexander | 14:8320b5ff96fa | 2681 | |
johnAlexander | 14:8320b5ff96fa | 2682 | bit >>= 2; |
johnAlexander | 14:8320b5ff96fa | 2683 | } |
johnAlexander | 14:8320b5ff96fa | 2684 | |
johnAlexander | 14:8320b5ff96fa | 2685 | return res; |
johnAlexander | 0:c523920bcc09 | 2686 | } |
johnAlexander | 0:c523920bcc09 | 2687 | |
johnAlexander | 14:8320b5ff96fa | 2688 | VL53L0X_Error VL53L0X::vl53l0x_calc_dmax( |
johnAlexander | 14:8320b5ff96fa | 2689 | VL53L0X_DEV dev, |
johnAlexander | 14:8320b5ff96fa | 2690 | FixPoint1616_t total_signal_rate_mcps, |
johnAlexander | 14:8320b5ff96fa | 2691 | FixPoint1616_t total_corr_signal_rate_mcps, |
johnAlexander | 14:8320b5ff96fa | 2692 | FixPoint1616_t pw_mult, |
johnAlexander | 14:8320b5ff96fa | 2693 | uint32_t sigma_estimate_p1, |
johnAlexander | 14:8320b5ff96fa | 2694 | FixPoint1616_t sigma_estimate_p2, |
johnAlexander | 14:8320b5ff96fa | 2695 | uint32_t peak_vcsel_duration_us, |
johnAlexander | 14:8320b5ff96fa | 2696 | uint32_t *pd_max_mm) |
johnAlexander | 0:c523920bcc09 | 2697 | { |
johnAlexander | 14:8320b5ff96fa | 2698 | const uint32_t c_sigma_limit = 18; |
johnAlexander | 14:8320b5ff96fa | 2699 | const FixPoint1616_t c_signal_limit = 0x4000; /* 0.25 */ |
johnAlexander | 14:8320b5ff96fa | 2700 | const FixPoint1616_t c_sigma_est_ref = 0x00000042; /* 0.001 */ |
johnAlexander | 14:8320b5ff96fa | 2701 | const uint32_t c_amb_eff_width_sigma_est_ns = 6; |
johnAlexander | 14:8320b5ff96fa | 2702 | const uint32_t c_amb_eff_width_d_max_ns = 7; |
johnAlexander | 14:8320b5ff96fa | 2703 | uint32_t dmax_cal_range_mm; |
johnAlexander | 14:8320b5ff96fa | 2704 | FixPoint1616_t dmax_cal_signal_rate_rtn_mcps; |
johnAlexander | 14:8320b5ff96fa | 2705 | FixPoint1616_t min_signal_needed; |
johnAlexander | 14:8320b5ff96fa | 2706 | FixPoint1616_t min_signal_needed_p1; |
johnAlexander | 14:8320b5ff96fa | 2707 | FixPoint1616_t min_signal_needed_p2; |
johnAlexander | 14:8320b5ff96fa | 2708 | FixPoint1616_t min_signal_needed_p3; |
johnAlexander | 14:8320b5ff96fa | 2709 | FixPoint1616_t min_signal_needed_p4; |
johnAlexander | 14:8320b5ff96fa | 2710 | FixPoint1616_t sigma_limit_tmp; |
johnAlexander | 14:8320b5ff96fa | 2711 | FixPoint1616_t sigma_est_sq_tmp; |
johnAlexander | 14:8320b5ff96fa | 2712 | FixPoint1616_t signal_limit_tmp; |
johnAlexander | 14:8320b5ff96fa | 2713 | FixPoint1616_t signal_at0_mm; |
johnAlexander | 14:8320b5ff96fa | 2714 | FixPoint1616_t dmax_dark; |
johnAlexander | 14:8320b5ff96fa | 2715 | FixPoint1616_t dmax_ambient; |
johnAlexander | 14:8320b5ff96fa | 2716 | FixPoint1616_t dmax_dark_tmp; |
johnAlexander | 14:8320b5ff96fa | 2717 | FixPoint1616_t sigma_est_p2_tmp; |
johnAlexander | 14:8320b5ff96fa | 2718 | uint32_t signal_rate_temp_mcps; |
johnAlexander | 14:8320b5ff96fa | 2719 | |
johnAlexander | 14:8320b5ff96fa | 2720 | VL53L0X_Error status = VL53L0X_ERROR_NONE; |
johnAlexander | 14:8320b5ff96fa | 2721 | |
johnAlexander | 14:8320b5ff96fa | 2722 | LOG_FUNCTION_START(""); |
johnAlexander | 14:8320b5ff96fa | 2723 | |
johnAlexander | 14:8320b5ff96fa | 2724 | dmax_cal_range_mm = |
johnAlexander | 14:8320b5ff96fa | 2725 | PALDevDataGet(dev, DmaxCalRangeMilliMeter); |
johnAlexander | 14:8320b5ff96fa | 2726 | |
johnAlexander | 14:8320b5ff96fa | 2727 | dmax_cal_signal_rate_rtn_mcps = |
johnAlexander | 14:8320b5ff96fa | 2728 | PALDevDataGet(dev, DmaxCalSignalRateRtnMegaCps); |
johnAlexander | 14:8320b5ff96fa | 2729 | |
johnAlexander | 14:8320b5ff96fa | 2730 | /* uint32 * FixPoint1616 = FixPoint1616 */ |
johnAlexander | 14:8320b5ff96fa | 2731 | signal_at0_mm = dmax_cal_range_mm * dmax_cal_signal_rate_rtn_mcps; |
johnAlexander | 14:8320b5ff96fa | 2732 | |
johnAlexander | 14:8320b5ff96fa | 2733 | /* FixPoint1616 >> 8 = FixPoint2408 */ |
johnAlexander | 14:8320b5ff96fa | 2734 | signal_at0_mm = (signal_at0_mm + 0x80) >> 8; |
johnAlexander | 14:8320b5ff96fa | 2735 | signal_at0_mm *= dmax_cal_range_mm; |
johnAlexander | 14:8320b5ff96fa | 2736 | |
johnAlexander | 14:8320b5ff96fa | 2737 | min_signal_needed_p1 = 0; |
johnAlexander | 14:8320b5ff96fa | 2738 | if (total_corr_signal_rate_mcps > 0) { |
johnAlexander | 14:8320b5ff96fa | 2739 | |
johnAlexander | 14:8320b5ff96fa | 2740 | /* Shift by 10 bits to increase resolution prior to the |
johnAlexander | 14:8320b5ff96fa | 2741 | * division */ |
johnAlexander | 14:8320b5ff96fa | 2742 | signal_rate_temp_mcps = total_signal_rate_mcps << 10; |
johnAlexander | 14:8320b5ff96fa | 2743 | |
johnAlexander | 14:8320b5ff96fa | 2744 | /* Add rounding value prior to division */ |
johnAlexander | 14:8320b5ff96fa | 2745 | min_signal_needed_p1 = signal_rate_temp_mcps + |
johnAlexander | 14:8320b5ff96fa | 2746 | (total_corr_signal_rate_mcps / 2); |
johnAlexander | 14:8320b5ff96fa | 2747 | |
johnAlexander | 14:8320b5ff96fa | 2748 | /* FixPoint0626/FixPoint1616 = FixPoint2210 */ |
johnAlexander | 14:8320b5ff96fa | 2749 | min_signal_needed_p1 /= total_corr_signal_rate_mcps; |
johnAlexander | 14:8320b5ff96fa | 2750 | |
johnAlexander | 14:8320b5ff96fa | 2751 | /* Apply a factored version of the speed of light. |
johnAlexander | 14:8320b5ff96fa | 2752 | Correction to be applied at the end */ |
johnAlexander | 14:8320b5ff96fa | 2753 | min_signal_needed_p1 *= 3; |
johnAlexander | 14:8320b5ff96fa | 2754 | |
johnAlexander | 14:8320b5ff96fa | 2755 | /* FixPoint2210 * FixPoint2210 = FixPoint1220 */ |
johnAlexander | 14:8320b5ff96fa | 2756 | min_signal_needed_p1 *= min_signal_needed_p1; |
johnAlexander | 14:8320b5ff96fa | 2757 | |
johnAlexander | 14:8320b5ff96fa | 2758 | /* FixPoint1220 >> 16 = FixPoint2804 */ |
johnAlexander | 14:8320b5ff96fa | 2759 | min_signal_needed_p1 = (min_signal_needed_p1 + 0x8000) >> 16; |
johnAlexander | 14:8320b5ff96fa | 2760 | } |
johnAlexander | 14:8320b5ff96fa | 2761 | |
johnAlexander | 14:8320b5ff96fa | 2762 | min_signal_needed_p2 = pw_mult * sigma_estimate_p1; |
johnAlexander | 14:8320b5ff96fa | 2763 | |
johnAlexander | 14:8320b5ff96fa | 2764 | /* FixPoint1616 >> 16 = uint32 */ |
johnAlexander | 14:8320b5ff96fa | 2765 | min_signal_needed_p2 = (min_signal_needed_p2 + 0x8000) >> 16; |
johnAlexander | 14:8320b5ff96fa | 2766 | |
johnAlexander | 14:8320b5ff96fa | 2767 | /* uint32 * uint32 = uint32 */ |
johnAlexander | 14:8320b5ff96fa | 2768 | min_signal_needed_p2 *= min_signal_needed_p2; |
johnAlexander | 14:8320b5ff96fa | 2769 | |
johnAlexander | 14:8320b5ff96fa | 2770 | /* Check sigmaEstimateP2 |
johnAlexander | 14:8320b5ff96fa | 2771 | * If this value is too high there is not enough signal rate |
johnAlexander | 14:8320b5ff96fa | 2772 | * to calculate dmax value so set a suitable value to ensure |
johnAlexander | 14:8320b5ff96fa | 2773 | * a very small dmax. |
johnAlexander | 14:8320b5ff96fa | 2774 | */ |
johnAlexander | 14:8320b5ff96fa | 2775 | sigma_est_p2_tmp = (sigma_estimate_p2 + 0x8000) >> 16; |
johnAlexander | 14:8320b5ff96fa | 2776 | sigma_est_p2_tmp = (sigma_est_p2_tmp + c_amb_eff_width_sigma_est_ns / 2) / |
johnAlexander | 14:8320b5ff96fa | 2777 | c_amb_eff_width_sigma_est_ns; |
johnAlexander | 14:8320b5ff96fa | 2778 | sigma_est_p2_tmp *= c_amb_eff_width_d_max_ns; |
johnAlexander | 14:8320b5ff96fa | 2779 | |
johnAlexander | 14:8320b5ff96fa | 2780 | if (sigma_est_p2_tmp > 0xffff) { |
johnAlexander | 14:8320b5ff96fa | 2781 | min_signal_needed_p3 = 0xfff00000; |
johnAlexander | 14:8320b5ff96fa | 2782 | } else { |
johnAlexander | 14:8320b5ff96fa | 2783 | |
johnAlexander | 14:8320b5ff96fa | 2784 | /* DMAX uses a different ambient width from sigma, so apply |
johnAlexander | 14:8320b5ff96fa | 2785 | * correction. |
johnAlexander | 14:8320b5ff96fa | 2786 | * Perform division before multiplication to prevent overflow. |
johnAlexander | 14:8320b5ff96fa | 2787 | */ |
johnAlexander | 14:8320b5ff96fa | 2788 | sigma_estimate_p2 = (sigma_estimate_p2 + c_amb_eff_width_sigma_est_ns / 2) / |
johnAlexander | 14:8320b5ff96fa | 2789 | c_amb_eff_width_sigma_est_ns; |
johnAlexander | 14:8320b5ff96fa | 2790 | sigma_estimate_p2 *= c_amb_eff_width_d_max_ns; |
johnAlexander | 14:8320b5ff96fa | 2791 | |
johnAlexander | 14:8320b5ff96fa | 2792 | /* FixPoint1616 >> 16 = uint32 */ |
johnAlexander | 14:8320b5ff96fa | 2793 | min_signal_needed_p3 = (sigma_estimate_p2 + 0x8000) >> 16; |
johnAlexander | 14:8320b5ff96fa | 2794 | |
johnAlexander | 14:8320b5ff96fa | 2795 | min_signal_needed_p3 *= min_signal_needed_p3; |
johnAlexander | 14:8320b5ff96fa | 2796 | |
johnAlexander | 14:8320b5ff96fa | 2797 | } |
johnAlexander | 14:8320b5ff96fa | 2798 | |
johnAlexander | 14:8320b5ff96fa | 2799 | /* FixPoint1814 / uint32 = FixPoint1814 */ |
johnAlexander | 14:8320b5ff96fa | 2800 | sigma_limit_tmp = ((c_sigma_limit << 14) + 500) / 1000; |
johnAlexander | 14:8320b5ff96fa | 2801 | |
johnAlexander | 14:8320b5ff96fa | 2802 | /* FixPoint1814 * FixPoint1814 = FixPoint3628 := FixPoint0428 */ |
johnAlexander | 14:8320b5ff96fa | 2803 | sigma_limit_tmp *= sigma_limit_tmp; |
johnAlexander | 14:8320b5ff96fa | 2804 | |
johnAlexander | 14:8320b5ff96fa | 2805 | /* FixPoint1616 * FixPoint1616 = FixPoint3232 */ |
johnAlexander | 14:8320b5ff96fa | 2806 | sigma_est_sq_tmp = c_sigma_est_ref * c_sigma_est_ref; |
johnAlexander | 14:8320b5ff96fa | 2807 | |
johnAlexander | 14:8320b5ff96fa | 2808 | /* FixPoint3232 >> 4 = FixPoint0428 */ |
johnAlexander | 14:8320b5ff96fa | 2809 | sigma_est_sq_tmp = (sigma_est_sq_tmp + 0x08) >> 4; |
johnAlexander | 14:8320b5ff96fa | 2810 | |
johnAlexander | 14:8320b5ff96fa | 2811 | /* FixPoint0428 - FixPoint0428 = FixPoint0428 */ |
johnAlexander | 14:8320b5ff96fa | 2812 | sigma_limit_tmp -= sigma_est_sq_tmp; |
johnAlexander | 14:8320b5ff96fa | 2813 | |
johnAlexander | 14:8320b5ff96fa | 2814 | /* uint32_t * FixPoint0428 = FixPoint0428 */ |
johnAlexander | 14:8320b5ff96fa | 2815 | min_signal_needed_p4 = 4 * 12 * sigma_limit_tmp; |
johnAlexander | 14:8320b5ff96fa | 2816 | |
johnAlexander | 14:8320b5ff96fa | 2817 | /* FixPoint0428 >> 14 = FixPoint1814 */ |
johnAlexander | 14:8320b5ff96fa | 2818 | min_signal_needed_p4 = (min_signal_needed_p4 + 0x2000) >> 14; |
johnAlexander | 14:8320b5ff96fa | 2819 | |
johnAlexander | 14:8320b5ff96fa | 2820 | /* uint32 + uint32 = uint32 */ |
johnAlexander | 14:8320b5ff96fa | 2821 | min_signal_needed = (min_signal_needed_p2 + min_signal_needed_p3); |
johnAlexander | 14:8320b5ff96fa | 2822 | |
johnAlexander | 14:8320b5ff96fa | 2823 | /* uint32 / uint32 = uint32 */ |
johnAlexander | 14:8320b5ff96fa | 2824 | min_signal_needed += (peak_vcsel_duration_us / 2); |
johnAlexander | 14:8320b5ff96fa | 2825 | min_signal_needed /= peak_vcsel_duration_us; |
johnAlexander | 14:8320b5ff96fa | 2826 | |
johnAlexander | 14:8320b5ff96fa | 2827 | /* uint32 << 14 = FixPoint1814 */ |
johnAlexander | 14:8320b5ff96fa | 2828 | min_signal_needed <<= 14; |
johnAlexander | 14:8320b5ff96fa | 2829 | |
johnAlexander | 14:8320b5ff96fa | 2830 | /* FixPoint1814 / FixPoint1814 = uint32 */ |
johnAlexander | 14:8320b5ff96fa | 2831 | min_signal_needed += (min_signal_needed_p4 / 2); |
johnAlexander | 14:8320b5ff96fa | 2832 | min_signal_needed /= min_signal_needed_p4; |
johnAlexander | 14:8320b5ff96fa | 2833 | |
johnAlexander | 14:8320b5ff96fa | 2834 | /* FixPoint3200 * FixPoint2804 := FixPoint2804*/ |
johnAlexander | 14:8320b5ff96fa | 2835 | min_signal_needed *= min_signal_needed_p1; |
johnAlexander | 14:8320b5ff96fa | 2836 | |
johnAlexander | 14:8320b5ff96fa | 2837 | /* Apply correction by dividing by 1000000. |
johnAlexander | 14:8320b5ff96fa | 2838 | * This assumes 10E16 on the numerator of the equation |
johnAlexander | 14:8320b5ff96fa | 2839 | * and 10E-22 on the denominator. |
johnAlexander | 14:8320b5ff96fa | 2840 | * We do this because 32bit fix point calculation can't |
johnAlexander | 14:8320b5ff96fa | 2841 | * handle the larger and smaller elements of this equation, |
johnAlexander | 14:8320b5ff96fa | 2842 | * i.e. speed of light and pulse widths. |
johnAlexander | 14:8320b5ff96fa | 2843 | */ |
johnAlexander | 14:8320b5ff96fa | 2844 | min_signal_needed = (min_signal_needed + 500) / 1000; |
johnAlexander | 14:8320b5ff96fa | 2845 | min_signal_needed <<= 4; |
johnAlexander | 14:8320b5ff96fa | 2846 | |
johnAlexander | 14:8320b5ff96fa | 2847 | min_signal_needed = (min_signal_needed + 500) / 1000; |
johnAlexander | 14:8320b5ff96fa | 2848 | |
johnAlexander | 14:8320b5ff96fa | 2849 | /* FixPoint1616 >> 8 = FixPoint2408 */ |
johnAlexander | 14:8320b5ff96fa | 2850 | signal_limit_tmp = (c_signal_limit + 0x80) >> 8; |
johnAlexander | 14:8320b5ff96fa | 2851 | |
johnAlexander | 14:8320b5ff96fa | 2852 | /* FixPoint2408/FixPoint2408 = uint32 */ |
johnAlexander | 14:8320b5ff96fa | 2853 | if (signal_limit_tmp != 0) |
johnAlexander | 14:8320b5ff96fa | 2854 | dmax_dark_tmp = (signal_at0_mm + (signal_limit_tmp / 2)) |
johnAlexander | 14:8320b5ff96fa | 2855 | / signal_limit_tmp; |
johnAlexander | 14:8320b5ff96fa | 2856 | else |
johnAlexander | 14:8320b5ff96fa | 2857 | dmax_dark_tmp = 0; |
johnAlexander | 14:8320b5ff96fa | 2858 | |
johnAlexander | 14:8320b5ff96fa | 2859 | dmax_dark = vl53l0x_isqrt(dmax_dark_tmp); |
johnAlexander | 14:8320b5ff96fa | 2860 | |
johnAlexander | 14:8320b5ff96fa | 2861 | /* FixPoint2408/FixPoint2408 = uint32 */ |
johnAlexander | 14:8320b5ff96fa | 2862 | if (min_signal_needed != 0) |
johnAlexander | 14:8320b5ff96fa | 2863 | dmax_ambient = (signal_at0_mm + min_signal_needed / 2) |
johnAlexander | 14:8320b5ff96fa | 2864 | / min_signal_needed; |
johnAlexander | 14:8320b5ff96fa | 2865 | else |
johnAlexander | 14:8320b5ff96fa | 2866 | dmax_ambient = 0; |
johnAlexander | 14:8320b5ff96fa | 2867 | |
johnAlexander | 14:8320b5ff96fa | 2868 | dmax_ambient = vl53l0x_isqrt(dmax_ambient); |
johnAlexander | 14:8320b5ff96fa | 2869 | |
johnAlexander | 14:8320b5ff96fa | 2870 | *pd_max_mm = dmax_dark; |
johnAlexander | 14:8320b5ff96fa | 2871 | if (dmax_dark > dmax_ambient) |
johnAlexander | 14:8320b5ff96fa | 2872 | *pd_max_mm = dmax_ambient; |
johnAlexander | 14:8320b5ff96fa | 2873 | |
johnAlexander | 14:8320b5ff96fa | 2874 | LOG_FUNCTION_END(status); |
johnAlexander | 14:8320b5ff96fa | 2875 | |
johnAlexander | 14:8320b5ff96fa | 2876 | return status; |
johnAlexander | 0:c523920bcc09 | 2877 | } |
johnAlexander | 0:c523920bcc09 | 2878 | |
johnAlexander | 14:8320b5ff96fa | 2879 | VL53L0X_Error VL53L0X::vl53l0x_calc_sigma_estimate(VL53L0X_DEV dev, |
johnAlexander | 14:8320b5ff96fa | 2880 | VL53L0X_RangingMeasurementData_t *p_ranging_measurement_data, |
johnAlexander | 14:8320b5ff96fa | 2881 | FixPoint1616_t *p_sigma_estimate, |
johnAlexander | 14:8320b5ff96fa | 2882 | uint32_t *p_dmax_mm) |
johnAlexander | 0:c523920bcc09 | 2883 | { |
johnAlexander | 14:8320b5ff96fa | 2884 | /* Expressed in 100ths of a ns, i.e. centi-ns */ |
johnAlexander | 14:8320b5ff96fa | 2885 | const uint32_t c_pulse_effective_width_centi_ns = 800; |
johnAlexander | 14:8320b5ff96fa | 2886 | /* Expressed in 100ths of a ns, i.e. centi-ns */ |
johnAlexander | 14:8320b5ff96fa | 2887 | const uint32_t c_ambient_effective_width_centi_ns = 600; |
johnAlexander | 14:8320b5ff96fa | 2888 | const FixPoint1616_t c_dflt_final_range_integration_time_milli_secs = 0x00190000; /* 25ms */ |
johnAlexander | 14:8320b5ff96fa | 2889 | const uint32_t c_vcsel_pulse_width_ps = 4700; /* pico secs */ |
johnAlexander | 14:8320b5ff96fa | 2890 | const FixPoint1616_t c_sigma_est_max = 0x028F87AE; |
johnAlexander | 14:8320b5ff96fa | 2891 | const FixPoint1616_t c_sigma_est_rtn_max = 0xF000; |
johnAlexander | 14:8320b5ff96fa | 2892 | const FixPoint1616_t c_amb_to_signal_ratio_max = 0xF0000000 / |
johnAlexander | 14:8320b5ff96fa | 2893 | c_ambient_effective_width_centi_ns; |
johnAlexander | 14:8320b5ff96fa | 2894 | /* Time Of Flight per mm (6.6 pico secs) */ |
johnAlexander | 14:8320b5ff96fa | 2895 | const FixPoint1616_t c_tof_per_mm_ps = 0x0006999A; |
johnAlexander | 14:8320b5ff96fa | 2896 | const uint32_t c_16bit_rounding_param = 0x00008000; |
johnAlexander | 14:8320b5ff96fa | 2897 | const FixPoint1616_t c_max_x_talk_kcps = 0x00320000; |
johnAlexander | 14:8320b5ff96fa | 2898 | const uint32_t c_pll_period_ps = 1655; |
johnAlexander | 14:8320b5ff96fa | 2899 | |
johnAlexander | 14:8320b5ff96fa | 2900 | uint32_t vcsel_total_events_rtn; |
johnAlexander | 14:8320b5ff96fa | 2901 | uint32_t final_range_timeout_micro_secs; |
johnAlexander | 14:8320b5ff96fa | 2902 | uint32_t pre_range_timeout_micro_secs; |
johnAlexander | 14:8320b5ff96fa | 2903 | uint32_t final_range_integration_time_milli_secs; |
johnAlexander | 14:8320b5ff96fa | 2904 | FixPoint1616_t sigma_estimate_p1; |
johnAlexander | 14:8320b5ff96fa | 2905 | FixPoint1616_t sigma_estimate_p2; |
johnAlexander | 14:8320b5ff96fa | 2906 | FixPoint1616_t sigma_estimate_p3; |
johnAlexander | 14:8320b5ff96fa | 2907 | FixPoint1616_t delta_t_ps; |
johnAlexander | 14:8320b5ff96fa | 2908 | FixPoint1616_t pw_mult; |
johnAlexander | 14:8320b5ff96fa | 2909 | FixPoint1616_t sigma_est_rtn; |
johnAlexander | 14:8320b5ff96fa | 2910 | FixPoint1616_t sigma_estimate; |
johnAlexander | 14:8320b5ff96fa | 2911 | FixPoint1616_t x_talk_correction; |
johnAlexander | 14:8320b5ff96fa | 2912 | FixPoint1616_t ambient_rate_kcps; |
johnAlexander | 14:8320b5ff96fa | 2913 | FixPoint1616_t peak_signal_rate_kcps; |
johnAlexander | 14:8320b5ff96fa | 2914 | FixPoint1616_t x_talk_comp_rate_mcps; |
johnAlexander | 14:8320b5ff96fa | 2915 | uint32_t x_talk_comp_rate_kcps; |
johnAlexander | 14:8320b5ff96fa | 2916 | VL53L0X_Error status = VL53L0X_ERROR_NONE; |
johnAlexander | 14:8320b5ff96fa | 2917 | FixPoint1616_t diff1_mcps; |
johnAlexander | 14:8320b5ff96fa | 2918 | FixPoint1616_t diff2_mcps; |
johnAlexander | 14:8320b5ff96fa | 2919 | FixPoint1616_t sqr1; |
johnAlexander | 14:8320b5ff96fa | 2920 | FixPoint1616_t sqr2; |
johnAlexander | 14:8320b5ff96fa | 2921 | FixPoint1616_t sqr_sum; |
johnAlexander | 14:8320b5ff96fa | 2922 | FixPoint1616_t sqrt_result_centi_ns; |
johnAlexander | 14:8320b5ff96fa | 2923 | FixPoint1616_t sqrt_result; |
johnAlexander | 14:8320b5ff96fa | 2924 | FixPoint1616_t total_signal_rate_mcps; |
johnAlexander | 14:8320b5ff96fa | 2925 | FixPoint1616_t corrected_signal_rate_mcps; |
johnAlexander | 14:8320b5ff96fa | 2926 | FixPoint1616_t sigma_est_ref; |
johnAlexander | 14:8320b5ff96fa | 2927 | uint32_t vcsel_width; |
johnAlexander | 14:8320b5ff96fa | 2928 | uint32_t final_range_macro_pclks; |
johnAlexander | 14:8320b5ff96fa | 2929 | uint32_t pre_range_macro_pclks; |
johnAlexander | 14:8320b5ff96fa | 2930 | uint32_t peak_vcsel_duration_us; |
johnAlexander | 14:8320b5ff96fa | 2931 | uint8_t final_range_vcsel_pclks; |
johnAlexander | 14:8320b5ff96fa | 2932 | uint8_t pre_range_vcsel_pclks; |
johnAlexander | 14:8320b5ff96fa | 2933 | /*! \addtogroup calc_sigma_estimate |
johnAlexander | 14:8320b5ff96fa | 2934 | * @{ |
johnAlexander | 14:8320b5ff96fa | 2935 | * |
johnAlexander | 14:8320b5ff96fa | 2936 | * Estimates the range sigma |
johnAlexander | 14:8320b5ff96fa | 2937 | */ |
johnAlexander | 14:8320b5ff96fa | 2938 | |
johnAlexander | 14:8320b5ff96fa | 2939 | LOG_FUNCTION_START(""); |
johnAlexander | 14:8320b5ff96fa | 2940 | |
johnAlexander | 14:8320b5ff96fa | 2941 | VL53L0X_GETPARAMETERFIELD(dev, XTalkCompensationRateMegaCps, |
johnAlexander | 14:8320b5ff96fa | 2942 | x_talk_comp_rate_mcps); |
johnAlexander | 14:8320b5ff96fa | 2943 | |
johnAlexander | 14:8320b5ff96fa | 2944 | /* |
johnAlexander | 14:8320b5ff96fa | 2945 | * We work in kcps rather than mcps as this helps keep within the |
johnAlexander | 14:8320b5ff96fa | 2946 | * confines of the 32 Fix1616 type. |
johnAlexander | 14:8320b5ff96fa | 2947 | */ |
johnAlexander | 14:8320b5ff96fa | 2948 | |
johnAlexander | 14:8320b5ff96fa | 2949 | ambient_rate_kcps = |
johnAlexander | 14:8320b5ff96fa | 2950 | (p_ranging_measurement_data->AmbientRateRtnMegaCps * 1000) >> 16; |
johnAlexander | 14:8320b5ff96fa | 2951 | |
johnAlexander | 14:8320b5ff96fa | 2952 | corrected_signal_rate_mcps = |
johnAlexander | 14:8320b5ff96fa | 2953 | p_ranging_measurement_data->SignalRateRtnMegaCps; |
johnAlexander | 14:8320b5ff96fa | 2954 | |
johnAlexander | 14:8320b5ff96fa | 2955 | |
johnAlexander | 14:8320b5ff96fa | 2956 | status = vl53l0x_get_total_signal_rate( |
johnAlexander | 14:8320b5ff96fa | 2957 | dev, p_ranging_measurement_data, &total_signal_rate_mcps); |
johnAlexander | 14:8320b5ff96fa | 2958 | status = vl53l0x_get_total_xtalk_rate( |
johnAlexander | 14:8320b5ff96fa | 2959 | dev, p_ranging_measurement_data, &x_talk_comp_rate_mcps); |
johnAlexander | 14:8320b5ff96fa | 2960 | |
johnAlexander | 14:8320b5ff96fa | 2961 | |
johnAlexander | 14:8320b5ff96fa | 2962 | /* Signal rate measurement provided by device is the |
johnAlexander | 14:8320b5ff96fa | 2963 | * peak signal rate, not average. |
johnAlexander | 14:8320b5ff96fa | 2964 | */ |
johnAlexander | 14:8320b5ff96fa | 2965 | peak_signal_rate_kcps = (total_signal_rate_mcps * 1000); |
johnAlexander | 14:8320b5ff96fa | 2966 | peak_signal_rate_kcps = (peak_signal_rate_kcps + 0x8000) >> 16; |
johnAlexander | 14:8320b5ff96fa | 2967 | |
johnAlexander | 14:8320b5ff96fa | 2968 | x_talk_comp_rate_kcps = x_talk_comp_rate_mcps * 1000; |
johnAlexander | 14:8320b5ff96fa | 2969 | |
johnAlexander | 14:8320b5ff96fa | 2970 | if (x_talk_comp_rate_kcps > c_max_x_talk_kcps) |
johnAlexander | 14:8320b5ff96fa | 2971 | x_talk_comp_rate_kcps = c_max_x_talk_kcps; |
johnAlexander | 14:8320b5ff96fa | 2972 | |
johnAlexander | 14:8320b5ff96fa | 2973 | if (status == VL53L0X_ERROR_NONE) { |
johnAlexander | 14:8320b5ff96fa | 2974 | |
johnAlexander | 14:8320b5ff96fa | 2975 | /* Calculate final range macro periods */ |
johnAlexander | 14:8320b5ff96fa | 2976 | final_range_timeout_micro_secs = VL53L0X_GETDEVICESPECIFICPARAMETER( |
johnAlexander | 14:8320b5ff96fa | 2977 | dev, FinalRangeTimeoutMicroSecs); |
johnAlexander | 14:8320b5ff96fa | 2978 | |
johnAlexander | 14:8320b5ff96fa | 2979 | final_range_vcsel_pclks = VL53L0X_GETDEVICESPECIFICPARAMETER( |
johnAlexander | 14:8320b5ff96fa | 2980 | dev, FinalRangeVcselPulsePeriod); |
johnAlexander | 14:8320b5ff96fa | 2981 | |
johnAlexander | 14:8320b5ff96fa | 2982 | final_range_macro_pclks = vl53l0x_calc_timeout_mclks( |
johnAlexander | 14:8320b5ff96fa | 2983 | dev, final_range_timeout_micro_secs, final_range_vcsel_pclks); |
johnAlexander | 14:8320b5ff96fa | 2984 | |
johnAlexander | 14:8320b5ff96fa | 2985 | /* Calculate pre-range macro periods */ |
johnAlexander | 14:8320b5ff96fa | 2986 | pre_range_timeout_micro_secs = VL53L0X_GETDEVICESPECIFICPARAMETER( |
johnAlexander | 14:8320b5ff96fa | 2987 | dev, PreRangeTimeoutMicroSecs); |
johnAlexander | 14:8320b5ff96fa | 2988 | |
johnAlexander | 14:8320b5ff96fa | 2989 | pre_range_vcsel_pclks = VL53L0X_GETDEVICESPECIFICPARAMETER( |
johnAlexander | 14:8320b5ff96fa | 2990 | dev, PreRangeVcselPulsePeriod); |
johnAlexander | 14:8320b5ff96fa | 2991 | |
johnAlexander | 14:8320b5ff96fa | 2992 | pre_range_macro_pclks = vl53l0x_calc_timeout_mclks( |
johnAlexander | 14:8320b5ff96fa | 2993 | dev, pre_range_timeout_micro_secs, pre_range_vcsel_pclks); |
johnAlexander | 14:8320b5ff96fa | 2994 | |
johnAlexander | 14:8320b5ff96fa | 2995 | vcsel_width = 3; |
johnAlexander | 14:8320b5ff96fa | 2996 | if (final_range_vcsel_pclks == 8) |
johnAlexander | 14:8320b5ff96fa | 2997 | vcsel_width = 2; |
johnAlexander | 14:8320b5ff96fa | 2998 | |
johnAlexander | 14:8320b5ff96fa | 2999 | |
johnAlexander | 14:8320b5ff96fa | 3000 | peak_vcsel_duration_us = vcsel_width * 2048 * |
johnAlexander | 14:8320b5ff96fa | 3001 | (pre_range_macro_pclks + final_range_macro_pclks); |
johnAlexander | 14:8320b5ff96fa | 3002 | peak_vcsel_duration_us = (peak_vcsel_duration_us + 500) / 1000; |
johnAlexander | 14:8320b5ff96fa | 3003 | peak_vcsel_duration_us *= c_pll_period_ps; |
johnAlexander | 14:8320b5ff96fa | 3004 | peak_vcsel_duration_us = (peak_vcsel_duration_us + 500) / 1000; |
johnAlexander | 14:8320b5ff96fa | 3005 | |
johnAlexander | 14:8320b5ff96fa | 3006 | /* Fix1616 >> 8 = Fix2408 */ |
johnAlexander | 14:8320b5ff96fa | 3007 | total_signal_rate_mcps = (total_signal_rate_mcps + 0x80) >> 8; |
johnAlexander | 14:8320b5ff96fa | 3008 | |
johnAlexander | 14:8320b5ff96fa | 3009 | /* Fix2408 * uint32 = Fix2408 */ |
johnAlexander | 14:8320b5ff96fa | 3010 | vcsel_total_events_rtn = total_signal_rate_mcps * |
johnAlexander | 14:8320b5ff96fa | 3011 | peak_vcsel_duration_us; |
johnAlexander | 14:8320b5ff96fa | 3012 | |
johnAlexander | 14:8320b5ff96fa | 3013 | /* Fix2408 >> 8 = uint32 */ |
johnAlexander | 14:8320b5ff96fa | 3014 | vcsel_total_events_rtn = (vcsel_total_events_rtn + 0x80) >> 8; |
johnAlexander | 14:8320b5ff96fa | 3015 | |
johnAlexander | 14:8320b5ff96fa | 3016 | /* Fix2408 << 8 = Fix1616 = */ |
johnAlexander | 14:8320b5ff96fa | 3017 | total_signal_rate_mcps <<= 8; |
johnAlexander | 14:8320b5ff96fa | 3018 | } |
johnAlexander | 14:8320b5ff96fa | 3019 | |
johnAlexander | 14:8320b5ff96fa | 3020 | if (status != VL53L0X_ERROR_NONE) { |
johnAlexander | 14:8320b5ff96fa | 3021 | LOG_FUNCTION_END(status); |
johnAlexander | 14:8320b5ff96fa | 3022 | return status; |
johnAlexander | 14:8320b5ff96fa | 3023 | } |
johnAlexander | 14:8320b5ff96fa | 3024 | |
johnAlexander | 14:8320b5ff96fa | 3025 | if (peak_signal_rate_kcps == 0) { |
johnAlexander | 14:8320b5ff96fa | 3026 | *p_sigma_estimate = c_sigma_est_max; |
johnAlexander | 14:8320b5ff96fa | 3027 | PALDevDataSet(dev, SigmaEstimate, c_sigma_est_max); |
johnAlexander | 14:8320b5ff96fa | 3028 | *p_dmax_mm = 0; |
johnAlexander | 14:8320b5ff96fa | 3029 | } else { |
johnAlexander | 14:8320b5ff96fa | 3030 | if (vcsel_total_events_rtn < 1) |
johnAlexander | 14:8320b5ff96fa | 3031 | vcsel_total_events_rtn = 1; |
johnAlexander | 14:8320b5ff96fa | 3032 | |
johnAlexander | 14:8320b5ff96fa | 3033 | sigma_estimate_p1 = c_pulse_effective_width_centi_ns; |
johnAlexander | 14:8320b5ff96fa | 3034 | |
johnAlexander | 14:8320b5ff96fa | 3035 | /* ((FixPoint1616 << 16)* uint32)/uint32 = FixPoint1616 */ |
johnAlexander | 14:8320b5ff96fa | 3036 | sigma_estimate_p2 = (ambient_rate_kcps << 16) / peak_signal_rate_kcps; |
johnAlexander | 14:8320b5ff96fa | 3037 | if (sigma_estimate_p2 > c_amb_to_signal_ratio_max) { |
johnAlexander | 14:8320b5ff96fa | 3038 | /* Clip to prevent overflow. Will ensure safe |
johnAlexander | 14:8320b5ff96fa | 3039 | * max result. */ |
johnAlexander | 14:8320b5ff96fa | 3040 | sigma_estimate_p2 = c_amb_to_signal_ratio_max; |
johnAlexander | 14:8320b5ff96fa | 3041 | } |
johnAlexander | 14:8320b5ff96fa | 3042 | sigma_estimate_p2 *= c_ambient_effective_width_centi_ns; |
johnAlexander | 14:8320b5ff96fa | 3043 | |
johnAlexander | 14:8320b5ff96fa | 3044 | sigma_estimate_p3 = 2 * vl53l0x_isqrt(vcsel_total_events_rtn * 12); |
johnAlexander | 14:8320b5ff96fa | 3045 | |
johnAlexander | 14:8320b5ff96fa | 3046 | /* uint32 * FixPoint1616 = FixPoint1616 */ |
johnAlexander | 14:8320b5ff96fa | 3047 | delta_t_ps = p_ranging_measurement_data->RangeMilliMeter * |
johnAlexander | 14:8320b5ff96fa | 3048 | c_tof_per_mm_ps; |
johnAlexander | 14:8320b5ff96fa | 3049 | |
johnAlexander | 14:8320b5ff96fa | 3050 | /* |
johnAlexander | 14:8320b5ff96fa | 3051 | * vcselRate - xtalkCompRate |
johnAlexander | 14:8320b5ff96fa | 3052 | * (uint32 << 16) - FixPoint1616 = FixPoint1616. |
johnAlexander | 14:8320b5ff96fa | 3053 | * Divide result by 1000 to convert to mcps. |
johnAlexander | 14:8320b5ff96fa | 3054 | * 500 is added to ensure rounding when integer division |
johnAlexander | 14:8320b5ff96fa | 3055 | * truncates. |
johnAlexander | 14:8320b5ff96fa | 3056 | */ |
johnAlexander | 14:8320b5ff96fa | 3057 | diff1_mcps = (((peak_signal_rate_kcps << 16) - |
johnAlexander | 14:8320b5ff96fa | 3058 | 2 * x_talk_comp_rate_kcps) + 500) / 1000; |
johnAlexander | 14:8320b5ff96fa | 3059 | |
johnAlexander | 14:8320b5ff96fa | 3060 | /* vcselRate + xtalkCompRate */ |
johnAlexander | 14:8320b5ff96fa | 3061 | diff2_mcps = ((peak_signal_rate_kcps << 16) + 500) / 1000; |
johnAlexander | 14:8320b5ff96fa | 3062 | |
johnAlexander | 14:8320b5ff96fa | 3063 | /* Shift by 8 bits to increase resolution prior to the |
johnAlexander | 14:8320b5ff96fa | 3064 | * division */ |
johnAlexander | 14:8320b5ff96fa | 3065 | diff1_mcps <<= 8; |
johnAlexander | 14:8320b5ff96fa | 3066 | |
johnAlexander | 14:8320b5ff96fa | 3067 | /* FixPoint0824/FixPoint1616 = FixPoint2408 */ |
johnAlexander | 0:c523920bcc09 | 3068 | // xTalkCorrection = abs(diff1_mcps/diff2_mcps); |
johnAlexander | 0:c523920bcc09 | 3069 | // abs is causing compiler overloading isue in C++, but unsigned types. So, redundant call anyway! |
johnAlexander | 14:8320b5ff96fa | 3070 | x_talk_correction = diff1_mcps / diff2_mcps; |
johnAlexander | 14:8320b5ff96fa | 3071 | |
johnAlexander | 14:8320b5ff96fa | 3072 | /* FixPoint2408 << 8 = FixPoint1616 */ |
johnAlexander | 14:8320b5ff96fa | 3073 | x_talk_correction <<= 8; |
johnAlexander | 14:8320b5ff96fa | 3074 | |
johnAlexander | 14:8320b5ff96fa | 3075 | if (p_ranging_measurement_data->RangeStatus != 0) { |
johnAlexander | 14:8320b5ff96fa | 3076 | pw_mult = 1 << 16; |
johnAlexander | 14:8320b5ff96fa | 3077 | } else { |
johnAlexander | 14:8320b5ff96fa | 3078 | /* FixPoint1616/uint32 = FixPoint1616 */ |
johnAlexander | 14:8320b5ff96fa | 3079 | pw_mult = delta_t_ps / c_vcsel_pulse_width_ps; /* smaller than 1.0f */ |
johnAlexander | 14:8320b5ff96fa | 3080 | |
johnAlexander | 14:8320b5ff96fa | 3081 | /* |
johnAlexander | 14:8320b5ff96fa | 3082 | * FixPoint1616 * FixPoint1616 = FixPoint3232, however both |
johnAlexander | 14:8320b5ff96fa | 3083 | * values are small enough such that32 bits will not be |
johnAlexander | 14:8320b5ff96fa | 3084 | * exceeded. |
johnAlexander | 14:8320b5ff96fa | 3085 | */ |
johnAlexander | 14:8320b5ff96fa | 3086 | pw_mult *= ((1 << 16) - x_talk_correction); |
johnAlexander | 14:8320b5ff96fa | 3087 | |
johnAlexander | 14:8320b5ff96fa | 3088 | /* (FixPoint3232 >> 16) = FixPoint1616 */ |
johnAlexander | 14:8320b5ff96fa | 3089 | pw_mult = (pw_mult + c_16bit_rounding_param) >> 16; |
johnAlexander | 14:8320b5ff96fa | 3090 | |
johnAlexander | 14:8320b5ff96fa | 3091 | /* FixPoint1616 + FixPoint1616 = FixPoint1616 */ |
johnAlexander | 14:8320b5ff96fa | 3092 | pw_mult += (1 << 16); |
johnAlexander | 14:8320b5ff96fa | 3093 | |
johnAlexander | 14:8320b5ff96fa | 3094 | /* |
johnAlexander | 14:8320b5ff96fa | 3095 | * At this point the value will be 1.xx, therefore if we square |
johnAlexander | 14:8320b5ff96fa | 3096 | * the value this will exceed 32 bits. To address this perform |
johnAlexander | 14:8320b5ff96fa | 3097 | * a single shift to the right before the multiplication. |
johnAlexander | 14:8320b5ff96fa | 3098 | */ |
johnAlexander | 14:8320b5ff96fa | 3099 | pw_mult >>= 1; |
johnAlexander | 14:8320b5ff96fa | 3100 | /* FixPoint1715 * FixPoint1715 = FixPoint3430 */ |
johnAlexander | 14:8320b5ff96fa | 3101 | pw_mult = pw_mult * pw_mult; |
johnAlexander | 14:8320b5ff96fa | 3102 | |
johnAlexander | 14:8320b5ff96fa | 3103 | /* (FixPoint3430 >> 14) = Fix1616 */ |
johnAlexander | 14:8320b5ff96fa | 3104 | pw_mult >>= 14; |
johnAlexander | 14:8320b5ff96fa | 3105 | } |
johnAlexander | 14:8320b5ff96fa | 3106 | |
johnAlexander | 14:8320b5ff96fa | 3107 | /* FixPoint1616 * uint32 = FixPoint1616 */ |
johnAlexander | 14:8320b5ff96fa | 3108 | sqr1 = pw_mult * sigma_estimate_p1; |
johnAlexander | 14:8320b5ff96fa | 3109 | |
johnAlexander | 14:8320b5ff96fa | 3110 | /* (FixPoint1616 >> 16) = FixPoint3200 */ |
johnAlexander | 14:8320b5ff96fa | 3111 | sqr1 = (sqr1 + 0x8000) >> 16; |
johnAlexander | 14:8320b5ff96fa | 3112 | |
johnAlexander | 14:8320b5ff96fa | 3113 | /* FixPoint3200 * FixPoint3200 = FixPoint6400 */ |
johnAlexander | 14:8320b5ff96fa | 3114 | sqr1 *= sqr1; |
johnAlexander | 14:8320b5ff96fa | 3115 | |
johnAlexander | 14:8320b5ff96fa | 3116 | sqr2 = sigma_estimate_p2; |
johnAlexander | 14:8320b5ff96fa | 3117 | |
johnAlexander | 14:8320b5ff96fa | 3118 | /* (FixPoint1616 >> 16) = FixPoint3200 */ |
johnAlexander | 14:8320b5ff96fa | 3119 | sqr2 = (sqr2 + 0x8000) >> 16; |
johnAlexander | 14:8320b5ff96fa | 3120 | |
johnAlexander | 14:8320b5ff96fa | 3121 | /* FixPoint3200 * FixPoint3200 = FixPoint6400 */ |
johnAlexander | 14:8320b5ff96fa | 3122 | sqr2 *= sqr2; |
johnAlexander | 14:8320b5ff96fa | 3123 | |
johnAlexander | 14:8320b5ff96fa | 3124 | /* FixPoint64000 + FixPoint6400 = FixPoint6400 */ |
johnAlexander | 14:8320b5ff96fa | 3125 | sqr_sum = sqr1 + sqr2; |
johnAlexander | 14:8320b5ff96fa | 3126 | |
johnAlexander | 14:8320b5ff96fa | 3127 | /* SQRT(FixPoin6400) = FixPoint3200 */ |
johnAlexander | 14:8320b5ff96fa | 3128 | sqrt_result_centi_ns = vl53l0x_isqrt(sqr_sum); |
johnAlexander | 14:8320b5ff96fa | 3129 | |
johnAlexander | 14:8320b5ff96fa | 3130 | /* (FixPoint3200 << 16) = FixPoint1616 */ |
johnAlexander | 14:8320b5ff96fa | 3131 | sqrt_result_centi_ns <<= 16; |
johnAlexander | 14:8320b5ff96fa | 3132 | |
johnAlexander | 14:8320b5ff96fa | 3133 | /* |
johnAlexander | 14:8320b5ff96fa | 3134 | * Note that the Speed Of Light is expressed in um per 1E-10 |
johnAlexander | 14:8320b5ff96fa | 3135 | * seconds (2997) Therefore to get mm/ns we have to divide by |
johnAlexander | 14:8320b5ff96fa | 3136 | * 10000 |
johnAlexander | 14:8320b5ff96fa | 3137 | */ |
johnAlexander | 14:8320b5ff96fa | 3138 | sigma_est_rtn = (((sqrt_result_centi_ns + 50) / 100) / |
johnAlexander | 14:8320b5ff96fa | 3139 | sigma_estimate_p3); |
johnAlexander | 14:8320b5ff96fa | 3140 | sigma_est_rtn *= VL53L0X_SPEED_OF_LIGHT_IN_AIR; |
johnAlexander | 14:8320b5ff96fa | 3141 | |
johnAlexander | 14:8320b5ff96fa | 3142 | /* Add 5000 before dividing by 10000 to ensure rounding. */ |
johnAlexander | 14:8320b5ff96fa | 3143 | sigma_est_rtn += 5000; |
johnAlexander | 14:8320b5ff96fa | 3144 | sigma_est_rtn /= 10000; |
johnAlexander | 14:8320b5ff96fa | 3145 | |
johnAlexander | 14:8320b5ff96fa | 3146 | if (sigma_est_rtn > c_sigma_est_rtn_max) { |
johnAlexander | 14:8320b5ff96fa | 3147 | /* Clip to prevent overflow. Will ensure safe |
johnAlexander | 14:8320b5ff96fa | 3148 | * max result. */ |
johnAlexander | 14:8320b5ff96fa | 3149 | sigma_est_rtn = c_sigma_est_rtn_max; |
johnAlexander | 14:8320b5ff96fa | 3150 | } |
johnAlexander | 14:8320b5ff96fa | 3151 | final_range_integration_time_milli_secs = |
johnAlexander | 14:8320b5ff96fa | 3152 | (final_range_timeout_micro_secs + pre_range_timeout_micro_secs + 500) / 1000; |
johnAlexander | 14:8320b5ff96fa | 3153 | |
johnAlexander | 14:8320b5ff96fa | 3154 | /* sigmaEstRef = 1mm * 25ms/final range integration time (inc pre-range) |
johnAlexander | 14:8320b5ff96fa | 3155 | * sqrt(FixPoint1616/int) = FixPoint2408) |
johnAlexander | 14:8320b5ff96fa | 3156 | */ |
johnAlexander | 14:8320b5ff96fa | 3157 | sigma_est_ref = |
johnAlexander | 14:8320b5ff96fa | 3158 | vl53l0x_isqrt((c_dflt_final_range_integration_time_milli_secs + |
johnAlexander | 14:8320b5ff96fa | 3159 | final_range_integration_time_milli_secs / 2) / |
johnAlexander | 14:8320b5ff96fa | 3160 | final_range_integration_time_milli_secs); |
johnAlexander | 14:8320b5ff96fa | 3161 | |
johnAlexander | 14:8320b5ff96fa | 3162 | /* FixPoint2408 << 8 = FixPoint1616 */ |
johnAlexander | 14:8320b5ff96fa | 3163 | sigma_est_ref <<= 8; |
johnAlexander | 14:8320b5ff96fa | 3164 | sigma_est_ref = (sigma_est_ref + 500) / 1000; |
johnAlexander | 14:8320b5ff96fa | 3165 | |
johnAlexander | 14:8320b5ff96fa | 3166 | /* FixPoint1616 * FixPoint1616 = FixPoint3232 */ |
johnAlexander | 14:8320b5ff96fa | 3167 | sqr1 = sigma_est_rtn * sigma_est_rtn; |
johnAlexander | 14:8320b5ff96fa | 3168 | /* FixPoint1616 * FixPoint1616 = FixPoint3232 */ |
johnAlexander | 14:8320b5ff96fa | 3169 | sqr2 = sigma_est_ref * sigma_est_ref; |
johnAlexander | 14:8320b5ff96fa | 3170 | |
johnAlexander | 14:8320b5ff96fa | 3171 | /* sqrt(FixPoint3232) = FixPoint1616 */ |
johnAlexander | 14:8320b5ff96fa | 3172 | sqrt_result = vl53l0x_isqrt((sqr1 + sqr2)); |
johnAlexander | 14:8320b5ff96fa | 3173 | /* |
johnAlexander | 14:8320b5ff96fa | 3174 | * Note that the Shift by 4 bits increases resolution prior to |
johnAlexander | 14:8320b5ff96fa | 3175 | * the sqrt, therefore the result must be shifted by 2 bits to |
johnAlexander | 14:8320b5ff96fa | 3176 | * the right to revert back to the FixPoint1616 format. |
johnAlexander | 14:8320b5ff96fa | 3177 | */ |
johnAlexander | 14:8320b5ff96fa | 3178 | |
johnAlexander | 14:8320b5ff96fa | 3179 | sigma_estimate = 1000 * sqrt_result; |
johnAlexander | 14:8320b5ff96fa | 3180 | |
johnAlexander | 14:8320b5ff96fa | 3181 | if ((peak_signal_rate_kcps < 1) || (vcsel_total_events_rtn < 1) || |
johnAlexander | 14:8320b5ff96fa | 3182 | (sigma_estimate > c_sigma_est_max)) { |
johnAlexander | 14:8320b5ff96fa | 3183 | sigma_estimate = c_sigma_est_max; |
johnAlexander | 14:8320b5ff96fa | 3184 | } |
johnAlexander | 14:8320b5ff96fa | 3185 | |
johnAlexander | 14:8320b5ff96fa | 3186 | *p_sigma_estimate = (uint32_t)(sigma_estimate); |
johnAlexander | 14:8320b5ff96fa | 3187 | PALDevDataSet(dev, SigmaEstimate, *p_sigma_estimate); |
johnAlexander | 14:8320b5ff96fa | 3188 | status = vl53l0x_calc_dmax( |
johnAlexander | 14:8320b5ff96fa | 3189 | dev, |
johnAlexander | 14:8320b5ff96fa | 3190 | total_signal_rate_mcps, |
johnAlexander | 14:8320b5ff96fa | 3191 | corrected_signal_rate_mcps, |
johnAlexander | 14:8320b5ff96fa | 3192 | pw_mult, |
johnAlexander | 14:8320b5ff96fa | 3193 | sigma_estimate_p1, |
johnAlexander | 14:8320b5ff96fa | 3194 | sigma_estimate_p2, |
johnAlexander | 14:8320b5ff96fa | 3195 | peak_vcsel_duration_us, |
johnAlexander | 14:8320b5ff96fa | 3196 | p_dmax_mm); |
johnAlexander | 14:8320b5ff96fa | 3197 | } |
johnAlexander | 14:8320b5ff96fa | 3198 | |
johnAlexander | 14:8320b5ff96fa | 3199 | LOG_FUNCTION_END(status); |
johnAlexander | 14:8320b5ff96fa | 3200 | return status; |
johnAlexander | 0:c523920bcc09 | 3201 | } |
johnAlexander | 0:c523920bcc09 | 3202 | |
johnAlexander | 14:8320b5ff96fa | 3203 | VL53L0X_Error VL53L0X::vl53l0x_get_pal_range_status(VL53L0X_DEV dev, |
johnAlexander | 14:8320b5ff96fa | 3204 | uint8_t device_range_status, |
johnAlexander | 14:8320b5ff96fa | 3205 | FixPoint1616_t signal_rate, |
johnAlexander | 14:8320b5ff96fa | 3206 | uint16_t effective_spad_rtn_count, |
johnAlexander | 14:8320b5ff96fa | 3207 | VL53L0X_RangingMeasurementData_t *p_ranging_measurement_data, |
johnAlexander | 14:8320b5ff96fa | 3208 | uint8_t *p_pal_range_status) |
johnAlexander | 0:c523920bcc09 | 3209 | { |
johnAlexander | 14:8320b5ff96fa | 3210 | VL53L0X_Error status = VL53L0X_ERROR_NONE; |
johnAlexander | 14:8320b5ff96fa | 3211 | uint8_t none_flag; |
johnAlexander | 14:8320b5ff96fa | 3212 | uint8_t sigma_limitflag = 0; |
johnAlexander | 14:8320b5ff96fa | 3213 | uint8_t signal_ref_clipflag = 0; |
johnAlexander | 14:8320b5ff96fa | 3214 | uint8_t range_ignore_thresholdflag = 0; |
johnAlexander | 14:8320b5ff96fa | 3215 | uint8_t sigma_limit_check_enable = 0; |
johnAlexander | 14:8320b5ff96fa | 3216 | uint8_t signal_rate_final_range_limit_check_enable = 0; |
johnAlexander | 14:8320b5ff96fa | 3217 | uint8_t signal_ref_clip_limit_check_enable = 0; |
johnAlexander | 14:8320b5ff96fa | 3218 | uint8_t range_ignore_threshold_limit_check_enable = 0; |
johnAlexander | 14:8320b5ff96fa | 3219 | FixPoint1616_t sigma_estimate; |
johnAlexander | 14:8320b5ff96fa | 3220 | FixPoint1616_t sigma_limit_value; |
johnAlexander | 14:8320b5ff96fa | 3221 | FixPoint1616_t signal_ref_clip_value; |
johnAlexander | 14:8320b5ff96fa | 3222 | FixPoint1616_t range_ignore_threshold_value; |
johnAlexander | 14:8320b5ff96fa | 3223 | FixPoint1616_t signal_rate_per_spad; |
johnAlexander | 14:8320b5ff96fa | 3224 | uint8_t device_range_status_internal = 0; |
johnAlexander | 14:8320b5ff96fa | 3225 | uint16_t tmp_word = 0; |
johnAlexander | 14:8320b5ff96fa | 3226 | uint8_t temp8; |
johnAlexander | 14:8320b5ff96fa | 3227 | uint32_t dmax_mm = 0; |
johnAlexander | 14:8320b5ff96fa | 3228 | FixPoint1616_t last_signal_ref_mcps; |
johnAlexander | 14:8320b5ff96fa | 3229 | |
johnAlexander | 14:8320b5ff96fa | 3230 | LOG_FUNCTION_START(""); |
johnAlexander | 14:8320b5ff96fa | 3231 | |
johnAlexander | 14:8320b5ff96fa | 3232 | |
johnAlexander | 14:8320b5ff96fa | 3233 | /* |
johnAlexander | 14:8320b5ff96fa | 3234 | * VL53L0X has a good ranging when the value of the |
johnAlexander | 14:8320b5ff96fa | 3235 | * DeviceRangeStatus = 11. This function will replace the value 0 with |
johnAlexander | 14:8320b5ff96fa | 3236 | * the value 11 in the DeviceRangeStatus. |
johnAlexander | 14:8320b5ff96fa | 3237 | * In addition, the SigmaEstimator is not included in the VL53L0X |
johnAlexander | 14:8320b5ff96fa | 3238 | * DeviceRangeStatus, this will be added in the PalRangeStatus. |
johnAlexander | 14:8320b5ff96fa | 3239 | */ |
johnAlexander | 14:8320b5ff96fa | 3240 | |
johnAlexander | 14:8320b5ff96fa | 3241 | device_range_status_internal = ((device_range_status & 0x78) >> 3); |
johnAlexander | 14:8320b5ff96fa | 3242 | |
johnAlexander | 14:8320b5ff96fa | 3243 | if (device_range_status_internal == 0 || |
johnAlexander | 14:8320b5ff96fa | 3244 | device_range_status_internal == 5 || |
johnAlexander | 14:8320b5ff96fa | 3245 | device_range_status_internal == 7 || |
johnAlexander | 14:8320b5ff96fa | 3246 | device_range_status_internal == 12 || |
johnAlexander | 14:8320b5ff96fa | 3247 | device_range_status_internal == 13 || |
johnAlexander | 14:8320b5ff96fa | 3248 | device_range_status_internal == 14 || |
johnAlexander | 14:8320b5ff96fa | 3249 | device_range_status_internal == 15 |
johnAlexander | 14:8320b5ff96fa | 3250 | ) { |
johnAlexander | 14:8320b5ff96fa | 3251 | none_flag = 1; |
johnAlexander | 14:8320b5ff96fa | 3252 | } else { |
johnAlexander | 14:8320b5ff96fa | 3253 | none_flag = 0; |
johnAlexander | 14:8320b5ff96fa | 3254 | } |
johnAlexander | 14:8320b5ff96fa | 3255 | |
johnAlexander | 14:8320b5ff96fa | 3256 | /* |
johnAlexander | 14:8320b5ff96fa | 3257 | * Check if Sigma limit is enabled, if yes then do comparison with limit |
johnAlexander | 14:8320b5ff96fa | 3258 | * value and put the result back into pPalRangeStatus. |
johnAlexander | 14:8320b5ff96fa | 3259 | */ |
johnAlexander | 14:8320b5ff96fa | 3260 | if (status == VL53L0X_ERROR_NONE) |
johnAlexander | 14:8320b5ff96fa | 3261 | status = vl53l0x_get_limit_check_enable(dev, |
johnAlexander | 14:8320b5ff96fa | 3262 | VL53L0X_CHECKENABLE_SIGMA_FINAL_RANGE, |
johnAlexander | 14:8320b5ff96fa | 3263 | &sigma_limit_check_enable); |
johnAlexander | 14:8320b5ff96fa | 3264 | |
johnAlexander | 14:8320b5ff96fa | 3265 | if ((sigma_limit_check_enable != 0) && (status == VL53L0X_ERROR_NONE)) { |
johnAlexander | 14:8320b5ff96fa | 3266 | /* |
johnAlexander | 14:8320b5ff96fa | 3267 | * compute the Sigma and check with limit |
johnAlexander | 14:8320b5ff96fa | 3268 | */ |
johnAlexander | 14:8320b5ff96fa | 3269 | status = vl53l0x_calc_sigma_estimate( |
johnAlexander | 14:8320b5ff96fa | 3270 | dev, |
johnAlexander | 14:8320b5ff96fa | 3271 | p_ranging_measurement_data, |
johnAlexander | 14:8320b5ff96fa | 3272 | &sigma_estimate, |
johnAlexander | 14:8320b5ff96fa | 3273 | &dmax_mm); |
johnAlexander | 14:8320b5ff96fa | 3274 | if (status == VL53L0X_ERROR_NONE) |
johnAlexander | 14:8320b5ff96fa | 3275 | p_ranging_measurement_data->RangeDMaxMilliMeter = dmax_mm; |
johnAlexander | 14:8320b5ff96fa | 3276 | |
johnAlexander | 14:8320b5ff96fa | 3277 | if (status == VL53L0X_ERROR_NONE) { |
johnAlexander | 14:8320b5ff96fa | 3278 | status = vl53l0x_get_limit_check_value(dev, |
johnAlexander | 14:8320b5ff96fa | 3279 | VL53L0X_CHECKENABLE_SIGMA_FINAL_RANGE, |
johnAlexander | 14:8320b5ff96fa | 3280 | &sigma_limit_value); |
johnAlexander | 14:8320b5ff96fa | 3281 | |
johnAlexander | 14:8320b5ff96fa | 3282 | if ((sigma_limit_value > 0) && |
johnAlexander | 14:8320b5ff96fa | 3283 | (sigma_estimate > sigma_limit_value)) |
johnAlexander | 14:8320b5ff96fa | 3284 | /* Limit Fail */ |
johnAlexander | 14:8320b5ff96fa | 3285 | sigma_limitflag = 1; |
johnAlexander | 14:8320b5ff96fa | 3286 | } |
johnAlexander | 14:8320b5ff96fa | 3287 | } |
johnAlexander | 14:8320b5ff96fa | 3288 | |
johnAlexander | 14:8320b5ff96fa | 3289 | /* |
johnAlexander | 14:8320b5ff96fa | 3290 | * Check if Signal ref clip limit is enabled, if yes then do comparison |
johnAlexander | 14:8320b5ff96fa | 3291 | * with limit value and put the result back into pPalRangeStatus. |
johnAlexander | 14:8320b5ff96fa | 3292 | */ |
johnAlexander | 14:8320b5ff96fa | 3293 | if (status == VL53L0X_ERROR_NONE) |
johnAlexander | 14:8320b5ff96fa | 3294 | status = vl53l0x_get_limit_check_enable(dev, |
johnAlexander | 14:8320b5ff96fa | 3295 | VL53L0X_CHECKENABLE_SIGNAL_REF_CLIP, |
johnAlexander | 14:8320b5ff96fa | 3296 | &signal_ref_clip_limit_check_enable); |
johnAlexander | 14:8320b5ff96fa | 3297 | |
johnAlexander | 14:8320b5ff96fa | 3298 | if ((signal_ref_clip_limit_check_enable != 0) && |
johnAlexander | 14:8320b5ff96fa | 3299 | (status == VL53L0X_ERROR_NONE)) { |
johnAlexander | 14:8320b5ff96fa | 3300 | |
johnAlexander | 14:8320b5ff96fa | 3301 | status = vl53l0x_get_limit_check_value(dev, |
johnAlexander | 14:8320b5ff96fa | 3302 | VL53L0X_CHECKENABLE_SIGNAL_REF_CLIP, |
johnAlexander | 14:8320b5ff96fa | 3303 | &signal_ref_clip_value); |
johnAlexander | 14:8320b5ff96fa | 3304 | |
johnAlexander | 14:8320b5ff96fa | 3305 | /* Read LastSignalRefMcps from device */ |
johnAlexander | 14:8320b5ff96fa | 3306 | if (status == VL53L0X_ERROR_NONE) |
johnAlexander | 14:8320b5ff96fa | 3307 | status = vl53l0x_write_byte(dev, 0xFF, 0x01); |
johnAlexander | 14:8320b5ff96fa | 3308 | |
johnAlexander | 14:8320b5ff96fa | 3309 | if (status == VL53L0X_ERROR_NONE) |
johnAlexander | 14:8320b5ff96fa | 3310 | status = vl53l0x_read_word(dev, |
johnAlexander | 14:8320b5ff96fa | 3311 | VL53L0X_REG_RESULT_PEAK_SIGNAL_RATE_REF, |
johnAlexander | 14:8320b5ff96fa | 3312 | &tmp_word); |
johnAlexander | 14:8320b5ff96fa | 3313 | |
johnAlexander | 14:8320b5ff96fa | 3314 | if (status == VL53L0X_ERROR_NONE) |
johnAlexander | 14:8320b5ff96fa | 3315 | status = vl53l0x_write_byte(dev, 0xFF, 0x00); |
johnAlexander | 14:8320b5ff96fa | 3316 | |
johnAlexander | 14:8320b5ff96fa | 3317 | last_signal_ref_mcps = VL53L0X_FIXPOINT97TOFIXPOINT1616(tmp_word); |
johnAlexander | 14:8320b5ff96fa | 3318 | PALDevDataSet(dev, LastSignalRefMcps, last_signal_ref_mcps); |
johnAlexander | 14:8320b5ff96fa | 3319 | |
johnAlexander | 14:8320b5ff96fa | 3320 | if ((signal_ref_clip_value > 0) && |
johnAlexander | 14:8320b5ff96fa | 3321 | (last_signal_ref_mcps > signal_ref_clip_value)) { |
johnAlexander | 14:8320b5ff96fa | 3322 | /* Limit Fail */ |
johnAlexander | 14:8320b5ff96fa | 3323 | signal_ref_clipflag = 1; |
johnAlexander | 14:8320b5ff96fa | 3324 | } |
johnAlexander | 14:8320b5ff96fa | 3325 | } |
johnAlexander | 14:8320b5ff96fa | 3326 | |
johnAlexander | 14:8320b5ff96fa | 3327 | /* |
johnAlexander | 14:8320b5ff96fa | 3328 | * Check if Signal ref clip limit is enabled, if yes then do comparison |
johnAlexander | 14:8320b5ff96fa | 3329 | * with limit value and put the result back into pPalRangeStatus. |
johnAlexander | 14:8320b5ff96fa | 3330 | * EffectiveSpadRtnCount has a format 8.8 |
johnAlexander | 14:8320b5ff96fa | 3331 | * If (Return signal rate < (1.5 x Xtalk x number of Spads)) : FAIL |
johnAlexander | 14:8320b5ff96fa | 3332 | */ |
johnAlexander | 14:8320b5ff96fa | 3333 | if (status == VL53L0X_ERROR_NONE) |
johnAlexander | 14:8320b5ff96fa | 3334 | status = vl53l0x_get_limit_check_enable(dev, |
johnAlexander | 14:8320b5ff96fa | 3335 | VL53L0X_CHECKENABLE_RANGE_IGNORE_THRESHOLD, |
johnAlexander | 14:8320b5ff96fa | 3336 | &range_ignore_threshold_limit_check_enable); |
johnAlexander | 14:8320b5ff96fa | 3337 | |
johnAlexander | 14:8320b5ff96fa | 3338 | if ((range_ignore_threshold_limit_check_enable != 0) && |
johnAlexander | 14:8320b5ff96fa | 3339 | (status == VL53L0X_ERROR_NONE)) { |
johnAlexander | 14:8320b5ff96fa | 3340 | |
johnAlexander | 14:8320b5ff96fa | 3341 | /* Compute the signal rate per spad */ |
johnAlexander | 14:8320b5ff96fa | 3342 | if (effective_spad_rtn_count == 0) { |
johnAlexander | 14:8320b5ff96fa | 3343 | signal_rate_per_spad = 0; |
johnAlexander | 14:8320b5ff96fa | 3344 | } else { |
johnAlexander | 14:8320b5ff96fa | 3345 | signal_rate_per_spad = (FixPoint1616_t)((256 * signal_rate) |
johnAlexander | 14:8320b5ff96fa | 3346 | / effective_spad_rtn_count); |
johnAlexander | 14:8320b5ff96fa | 3347 | } |
johnAlexander | 14:8320b5ff96fa | 3348 | |
johnAlexander | 14:8320b5ff96fa | 3349 | status = vl53l0x_get_limit_check_value(dev, |
johnAlexander | 14:8320b5ff96fa | 3350 | VL53L0X_CHECKENABLE_RANGE_IGNORE_THRESHOLD, |
johnAlexander | 14:8320b5ff96fa | 3351 | &range_ignore_threshold_value); |
johnAlexander | 14:8320b5ff96fa | 3352 | |
johnAlexander | 14:8320b5ff96fa | 3353 | if ((range_ignore_threshold_value > 0) && |
johnAlexander | 14:8320b5ff96fa | 3354 | (signal_rate_per_spad < range_ignore_threshold_value)) { |
johnAlexander | 14:8320b5ff96fa | 3355 | /* Limit Fail add 2^6 to range status */ |
johnAlexander | 14:8320b5ff96fa | 3356 | range_ignore_thresholdflag = 1; |
johnAlexander | 14:8320b5ff96fa | 3357 | } |
johnAlexander | 14:8320b5ff96fa | 3358 | } |
johnAlexander | 14:8320b5ff96fa | 3359 | |
johnAlexander | 14:8320b5ff96fa | 3360 | if (status == VL53L0X_ERROR_NONE) { |
johnAlexander | 14:8320b5ff96fa | 3361 | if (none_flag == 1) { |
johnAlexander | 14:8320b5ff96fa | 3362 | *p_pal_range_status = 255; /* NONE */ |
johnAlexander | 14:8320b5ff96fa | 3363 | } else if (device_range_status_internal == 1 || |
johnAlexander | 14:8320b5ff96fa | 3364 | device_range_status_internal == 2 || |
johnAlexander | 14:8320b5ff96fa | 3365 | device_range_status_internal == 3) { |
johnAlexander | 14:8320b5ff96fa | 3366 | *p_pal_range_status = 5; /* HW fail */ |
johnAlexander | 14:8320b5ff96fa | 3367 | } else if (device_range_status_internal == 6 || |
johnAlexander | 14:8320b5ff96fa | 3368 | device_range_status_internal == 9) { |
johnAlexander | 14:8320b5ff96fa | 3369 | *p_pal_range_status = 4; /* Phase fail */ |
johnAlexander | 14:8320b5ff96fa | 3370 | } else if (device_range_status_internal == 8 || |
johnAlexander | 14:8320b5ff96fa | 3371 | device_range_status_internal == 10 || |
johnAlexander | 14:8320b5ff96fa | 3372 | signal_ref_clipflag == 1) { |
johnAlexander | 14:8320b5ff96fa | 3373 | *p_pal_range_status = 3; /* Min range */ |
johnAlexander | 14:8320b5ff96fa | 3374 | } else if (device_range_status_internal == 4 || |
johnAlexander | 14:8320b5ff96fa | 3375 | range_ignore_thresholdflag == 1) { |
johnAlexander | 14:8320b5ff96fa | 3376 | *p_pal_range_status = 2; /* Signal Fail */ |
johnAlexander | 14:8320b5ff96fa | 3377 | } else if (sigma_limitflag == 1) { |
johnAlexander | 14:8320b5ff96fa | 3378 | *p_pal_range_status = 1; /* Sigma Fail */ |
johnAlexander | 14:8320b5ff96fa | 3379 | } else { |
johnAlexander | 14:8320b5ff96fa | 3380 | *p_pal_range_status = 0; /* Range Valid */ |
johnAlexander | 14:8320b5ff96fa | 3381 | } |
johnAlexander | 14:8320b5ff96fa | 3382 | } |
johnAlexander | 14:8320b5ff96fa | 3383 | |
johnAlexander | 14:8320b5ff96fa | 3384 | /* DMAX only relevant during range error */ |
johnAlexander | 14:8320b5ff96fa | 3385 | if (*p_pal_range_status == 0) |
johnAlexander | 14:8320b5ff96fa | 3386 | p_ranging_measurement_data->RangeDMaxMilliMeter = 0; |
johnAlexander | 14:8320b5ff96fa | 3387 | |
johnAlexander | 14:8320b5ff96fa | 3388 | /* fill the Limit Check Status */ |
johnAlexander | 14:8320b5ff96fa | 3389 | |
johnAlexander | 14:8320b5ff96fa | 3390 | status = vl53l0x_get_limit_check_enable(dev, |
johnAlexander | 14:8320b5ff96fa | 3391 | VL53L0X_CHECKENABLE_SIGNAL_RATE_FINAL_RANGE, |
johnAlexander | 14:8320b5ff96fa | 3392 | &signal_rate_final_range_limit_check_enable); |
johnAlexander | 14:8320b5ff96fa | 3393 | |
johnAlexander | 14:8320b5ff96fa | 3394 | if (status == VL53L0X_ERROR_NONE) { |
johnAlexander | 14:8320b5ff96fa | 3395 | if ((sigma_limit_check_enable == 0) || (sigma_limitflag == 1)) |
johnAlexander | 14:8320b5ff96fa | 3396 | temp8 = 1; |
johnAlexander | 14:8320b5ff96fa | 3397 | else |
johnAlexander | 14:8320b5ff96fa | 3398 | temp8 = 0; |
johnAlexander | 14:8320b5ff96fa | 3399 | VL53L0X_SETARRAYPARAMETERFIELD(dev, LimitChecksStatus, |
johnAlexander | 14:8320b5ff96fa | 3400 | VL53L0X_CHECKENABLE_SIGMA_FINAL_RANGE, temp8); |
johnAlexander | 14:8320b5ff96fa | 3401 | |
johnAlexander | 14:8320b5ff96fa | 3402 | if ((device_range_status_internal == 4) || |
johnAlexander | 14:8320b5ff96fa | 3403 | (signal_rate_final_range_limit_check_enable == 0)) |
johnAlexander | 14:8320b5ff96fa | 3404 | temp8 = 1; |
johnAlexander | 14:8320b5ff96fa | 3405 | else |
johnAlexander | 14:8320b5ff96fa | 3406 | temp8 = 0; |
johnAlexander | 14:8320b5ff96fa | 3407 | VL53L0X_SETARRAYPARAMETERFIELD(dev, LimitChecksStatus, |
johnAlexander | 14:8320b5ff96fa | 3408 | VL53L0X_CHECKENABLE_SIGNAL_RATE_FINAL_RANGE, |
johnAlexander | 14:8320b5ff96fa | 3409 | temp8); |
johnAlexander | 14:8320b5ff96fa | 3410 | |
johnAlexander | 14:8320b5ff96fa | 3411 | if ((signal_ref_clip_limit_check_enable == 0) || |
johnAlexander | 14:8320b5ff96fa | 3412 | (signal_ref_clipflag == 1)) |
johnAlexander | 14:8320b5ff96fa | 3413 | temp8 = 1; |
johnAlexander | 14:8320b5ff96fa | 3414 | else |
johnAlexander | 14:8320b5ff96fa | 3415 | temp8 = 0; |
johnAlexander | 14:8320b5ff96fa | 3416 | |
johnAlexander | 14:8320b5ff96fa | 3417 | VL53L0X_SETARRAYPARAMETERFIELD(dev, LimitChecksStatus, |
johnAlexander | 14:8320b5ff96fa | 3418 | VL53L0X_CHECKENABLE_SIGNAL_REF_CLIP, temp8); |
johnAlexander | 14:8320b5ff96fa | 3419 | |
johnAlexander | 14:8320b5ff96fa | 3420 | if ((range_ignore_threshold_limit_check_enable == 0) || |
johnAlexander | 14:8320b5ff96fa | 3421 | (range_ignore_thresholdflag == 1)) |
johnAlexander | 14:8320b5ff96fa | 3422 | temp8 = 1; |
johnAlexander | 14:8320b5ff96fa | 3423 | else |
johnAlexander | 14:8320b5ff96fa | 3424 | temp8 = 0; |
johnAlexander | 14:8320b5ff96fa | 3425 | |
johnAlexander | 14:8320b5ff96fa | 3426 | VL53L0X_SETARRAYPARAMETERFIELD(dev, LimitChecksStatus, |
johnAlexander | 14:8320b5ff96fa | 3427 | VL53L0X_CHECKENABLE_RANGE_IGNORE_THRESHOLD, |
johnAlexander | 14:8320b5ff96fa | 3428 | temp8); |
johnAlexander | 14:8320b5ff96fa | 3429 | } |
johnAlexander | 14:8320b5ff96fa | 3430 | |
johnAlexander | 14:8320b5ff96fa | 3431 | LOG_FUNCTION_END(status); |
johnAlexander | 14:8320b5ff96fa | 3432 | return status; |
johnAlexander | 0:c523920bcc09 | 3433 | |
johnAlexander | 0:c523920bcc09 | 3434 | } |
johnAlexander | 0:c523920bcc09 | 3435 | |
johnAlexander | 14:8320b5ff96fa | 3436 | VL53L0X_Error VL53L0X::vl53l0x_get_ranging_measurement_data(VL53L0X_DEV dev, |
johnAlexander | 14:8320b5ff96fa | 3437 | VL53L0X_RangingMeasurementData_t *p_ranging_measurement_data) |
johnAlexander | 0:c523920bcc09 | 3438 | { |
johnAlexander | 14:8320b5ff96fa | 3439 | VL53L0X_Error status = VL53L0X_ERROR_NONE; |
johnAlexander | 14:8320b5ff96fa | 3440 | uint8_t device_range_status; |
johnAlexander | 14:8320b5ff96fa | 3441 | uint8_t range_fractional_enable; |
johnAlexander | 14:8320b5ff96fa | 3442 | uint8_t pal_range_status; |
johnAlexander | 14:8320b5ff96fa | 3443 | uint8_t x_talk_compensation_enable; |
johnAlexander | 14:8320b5ff96fa | 3444 | uint16_t ambient_rate; |
johnAlexander | 14:8320b5ff96fa | 3445 | FixPoint1616_t signal_rate; |
johnAlexander | 14:8320b5ff96fa | 3446 | uint16_t x_talk_compensation_rate_mega_cps; |
johnAlexander | 14:8320b5ff96fa | 3447 | uint16_t effective_spad_rtn_count; |
johnAlexander | 14:8320b5ff96fa | 3448 | uint16_t tmpuint16; |
johnAlexander | 14:8320b5ff96fa | 3449 | uint16_t xtalk_range_milli_meter; |
johnAlexander | 14:8320b5ff96fa | 3450 | uint16_t linearity_corrective_gain; |
johnAlexander | 14:8320b5ff96fa | 3451 | uint8_t localBuffer[12]; |
johnAlexander | 14:8320b5ff96fa | 3452 | VL53L0X_RangingMeasurementData_t last_range_data_buffer; |
johnAlexander | 14:8320b5ff96fa | 3453 | |
johnAlexander | 14:8320b5ff96fa | 3454 | LOG_FUNCTION_START(""); |
johnAlexander | 14:8320b5ff96fa | 3455 | |
johnAlexander | 14:8320b5ff96fa | 3456 | /* |
johnAlexander | 14:8320b5ff96fa | 3457 | * use multi read even if some registers are not useful, result will |
johnAlexander | 14:8320b5ff96fa | 3458 | * be more efficient |
johnAlexander | 14:8320b5ff96fa | 3459 | * start reading at 0x14 dec20 |
johnAlexander | 14:8320b5ff96fa | 3460 | * end reading at 0x21 dec33 total 14 bytes to read |
johnAlexander | 14:8320b5ff96fa | 3461 | */ |
johnAlexander | 14:8320b5ff96fa | 3462 | status = vl53l0x_read_multi(dev, 0x14, localBuffer, 12); |
johnAlexander | 14:8320b5ff96fa | 3463 | |
johnAlexander | 14:8320b5ff96fa | 3464 | if (status == VL53L0X_ERROR_NONE) { |
johnAlexander | 14:8320b5ff96fa | 3465 | |
johnAlexander | 14:8320b5ff96fa | 3466 | p_ranging_measurement_data->ZoneId = 0; /* Only one zone */ |
johnAlexander | 14:8320b5ff96fa | 3467 | p_ranging_measurement_data->TimeStamp = 0; /* Not Implemented */ |
johnAlexander | 14:8320b5ff96fa | 3468 | |
johnAlexander | 14:8320b5ff96fa | 3469 | tmpuint16 = VL53L0X_MAKEUINT16(localBuffer[11], localBuffer[10]); |
johnAlexander | 14:8320b5ff96fa | 3470 | /* cut1.1 if SYSTEM__RANGE_CONFIG if 1 range is 2bits fractional |
johnAlexander | 14:8320b5ff96fa | 3471 | *(format 11.2) else no fractional |
johnAlexander | 14:8320b5ff96fa | 3472 | */ |
johnAlexander | 14:8320b5ff96fa | 3473 | |
johnAlexander | 14:8320b5ff96fa | 3474 | p_ranging_measurement_data->MeasurementTimeUsec = 0; |
johnAlexander | 14:8320b5ff96fa | 3475 | |
johnAlexander | 14:8320b5ff96fa | 3476 | signal_rate = VL53L0X_FIXPOINT97TOFIXPOINT1616( |
johnAlexander | 14:8320b5ff96fa | 3477 | VL53L0X_MAKEUINT16(localBuffer[7], localBuffer[6])); |
johnAlexander | 14:8320b5ff96fa | 3478 | /* peak_signal_count_rate_rtn_mcps */ |
johnAlexander | 14:8320b5ff96fa | 3479 | p_ranging_measurement_data->SignalRateRtnMegaCps = signal_rate; |
johnAlexander | 14:8320b5ff96fa | 3480 | |
johnAlexander | 14:8320b5ff96fa | 3481 | ambient_rate = VL53L0X_MAKEUINT16(localBuffer[9], localBuffer[8]); |
johnAlexander | 14:8320b5ff96fa | 3482 | p_ranging_measurement_data->AmbientRateRtnMegaCps = |
johnAlexander | 14:8320b5ff96fa | 3483 | VL53L0X_FIXPOINT97TOFIXPOINT1616(ambient_rate); |
johnAlexander | 14:8320b5ff96fa | 3484 | |
johnAlexander | 14:8320b5ff96fa | 3485 | effective_spad_rtn_count = VL53L0X_MAKEUINT16(localBuffer[3], |
johnAlexander | 14:8320b5ff96fa | 3486 | localBuffer[2]); |
johnAlexander | 14:8320b5ff96fa | 3487 | /* EffectiveSpadRtnCount is 8.8 format */ |
johnAlexander | 14:8320b5ff96fa | 3488 | p_ranging_measurement_data->EffectiveSpadRtnCount = |
johnAlexander | 14:8320b5ff96fa | 3489 | effective_spad_rtn_count; |
johnAlexander | 14:8320b5ff96fa | 3490 | |
johnAlexander | 14:8320b5ff96fa | 3491 | device_range_status = localBuffer[0]; |
johnAlexander | 14:8320b5ff96fa | 3492 | |
johnAlexander | 14:8320b5ff96fa | 3493 | /* Get Linearity Corrective Gain */ |
johnAlexander | 14:8320b5ff96fa | 3494 | linearity_corrective_gain = PALDevDataGet(dev, |
johnAlexander | 14:8320b5ff96fa | 3495 | LinearityCorrectiveGain); |
johnAlexander | 14:8320b5ff96fa | 3496 | |
johnAlexander | 14:8320b5ff96fa | 3497 | /* Get ranging configuration */ |
johnAlexander | 14:8320b5ff96fa | 3498 | range_fractional_enable = PALDevDataGet(dev, |
johnAlexander | 14:8320b5ff96fa | 3499 | RangeFractionalEnable); |
johnAlexander | 14:8320b5ff96fa | 3500 | |
johnAlexander | 14:8320b5ff96fa | 3501 | if (linearity_corrective_gain != 1000) { |
johnAlexander | 14:8320b5ff96fa | 3502 | |
johnAlexander | 14:8320b5ff96fa | 3503 | tmpuint16 = (uint16_t)((linearity_corrective_gain |
johnAlexander | 14:8320b5ff96fa | 3504 | * tmpuint16 + 500) / 1000); |
johnAlexander | 14:8320b5ff96fa | 3505 | |
johnAlexander | 14:8320b5ff96fa | 3506 | /* Implement Xtalk */ |
johnAlexander | 14:8320b5ff96fa | 3507 | VL53L0X_GETPARAMETERFIELD(dev, |
johnAlexander | 14:8320b5ff96fa | 3508 | XTalkCompensationRateMegaCps, |
johnAlexander | 14:8320b5ff96fa | 3509 | x_talk_compensation_rate_mega_cps); |
johnAlexander | 14:8320b5ff96fa | 3510 | VL53L0X_GETPARAMETERFIELD(dev, XTalkCompensationEnable, |
johnAlexander | 14:8320b5ff96fa | 3511 | x_talk_compensation_enable); |
johnAlexander | 14:8320b5ff96fa | 3512 | |
johnAlexander | 14:8320b5ff96fa | 3513 | if (x_talk_compensation_enable) { |
johnAlexander | 14:8320b5ff96fa | 3514 | |
johnAlexander | 14:8320b5ff96fa | 3515 | if ((signal_rate |
johnAlexander | 14:8320b5ff96fa | 3516 | - ((x_talk_compensation_rate_mega_cps |
johnAlexander | 14:8320b5ff96fa | 3517 | * effective_spad_rtn_count) >> 8)) |
johnAlexander | 14:8320b5ff96fa | 3518 | <= 0) { |
johnAlexander | 14:8320b5ff96fa | 3519 | if (range_fractional_enable) |
johnAlexander | 14:8320b5ff96fa | 3520 | xtalk_range_milli_meter = 8888; |
johnAlexander | 14:8320b5ff96fa | 3521 | else |
johnAlexander | 14:8320b5ff96fa | 3522 | xtalk_range_milli_meter = 8888 |
johnAlexander | 14:8320b5ff96fa | 3523 | << 2; |
johnAlexander | 14:8320b5ff96fa | 3524 | } else { |
johnAlexander | 14:8320b5ff96fa | 3525 | xtalk_range_milli_meter = |
johnAlexander | 14:8320b5ff96fa | 3526 | (tmpuint16 * signal_rate) |
johnAlexander | 14:8320b5ff96fa | 3527 | / (signal_rate |
johnAlexander | 14:8320b5ff96fa | 3528 | - ((x_talk_compensation_rate_mega_cps |
johnAlexander | 14:8320b5ff96fa | 3529 | * effective_spad_rtn_count) |
johnAlexander | 14:8320b5ff96fa | 3530 | >> 8)); |
johnAlexander | 14:8320b5ff96fa | 3531 | } |
johnAlexander | 14:8320b5ff96fa | 3532 | |
johnAlexander | 14:8320b5ff96fa | 3533 | tmpuint16 = xtalk_range_milli_meter; |
johnAlexander | 14:8320b5ff96fa | 3534 | } |
johnAlexander | 14:8320b5ff96fa | 3535 | |
johnAlexander | 14:8320b5ff96fa | 3536 | } |
johnAlexander | 14:8320b5ff96fa | 3537 | |
johnAlexander | 14:8320b5ff96fa | 3538 | if (range_fractional_enable) { |
johnAlexander | 14:8320b5ff96fa | 3539 | p_ranging_measurement_data->RangeMilliMeter = |
johnAlexander | 14:8320b5ff96fa | 3540 | (uint16_t)((tmpuint16) >> 2); |
johnAlexander | 14:8320b5ff96fa | 3541 | p_ranging_measurement_data->RangeFractionalPart = |
johnAlexander | 14:8320b5ff96fa | 3542 | (uint8_t)((tmpuint16 & 0x03) << 6); |
johnAlexander | 14:8320b5ff96fa | 3543 | } else { |
johnAlexander | 14:8320b5ff96fa | 3544 | p_ranging_measurement_data->RangeMilliMeter = tmpuint16; |
johnAlexander | 14:8320b5ff96fa | 3545 | p_ranging_measurement_data->RangeFractionalPart = 0; |
johnAlexander | 14:8320b5ff96fa | 3546 | } |
johnAlexander | 14:8320b5ff96fa | 3547 | |
johnAlexander | 14:8320b5ff96fa | 3548 | /* |
johnAlexander | 14:8320b5ff96fa | 3549 | * For a standard definition of RangeStatus, this should |
johnAlexander | 14:8320b5ff96fa | 3550 | * return 0 in case of good result after a ranging |
johnAlexander | 14:8320b5ff96fa | 3551 | * The range status depends on the device so call a device |
johnAlexander | 14:8320b5ff96fa | 3552 | * specific function to obtain the right Status. |
johnAlexander | 14:8320b5ff96fa | 3553 | */ |
johnAlexander | 14:8320b5ff96fa | 3554 | status |= vl53l0x_get_pal_range_status(dev, device_range_status, |
johnAlexander | 14:8320b5ff96fa | 3555 | signal_rate, effective_spad_rtn_count, |
johnAlexander | 14:8320b5ff96fa | 3556 | p_ranging_measurement_data, &pal_range_status); |
johnAlexander | 14:8320b5ff96fa | 3557 | |
johnAlexander | 14:8320b5ff96fa | 3558 | if (status == VL53L0X_ERROR_NONE) |
johnAlexander | 14:8320b5ff96fa | 3559 | p_ranging_measurement_data->RangeStatus = pal_range_status; |
johnAlexander | 14:8320b5ff96fa | 3560 | |
johnAlexander | 14:8320b5ff96fa | 3561 | } |
johnAlexander | 14:8320b5ff96fa | 3562 | |
johnAlexander | 14:8320b5ff96fa | 3563 | if (status == VL53L0X_ERROR_NONE) { |
johnAlexander | 14:8320b5ff96fa | 3564 | /* Copy last read data into Dev buffer */ |
johnAlexander | 14:8320b5ff96fa | 3565 | last_range_data_buffer = PALDevDataGet(dev, LastRangeMeasure); |
johnAlexander | 14:8320b5ff96fa | 3566 | |
johnAlexander | 14:8320b5ff96fa | 3567 | last_range_data_buffer.RangeMilliMeter = |
johnAlexander | 14:8320b5ff96fa | 3568 | p_ranging_measurement_data->RangeMilliMeter; |
johnAlexander | 14:8320b5ff96fa | 3569 | last_range_data_buffer.RangeFractionalPart = |
johnAlexander | 14:8320b5ff96fa | 3570 | p_ranging_measurement_data->RangeFractionalPart; |
johnAlexander | 14:8320b5ff96fa | 3571 | last_range_data_buffer.RangeDMaxMilliMeter = |
johnAlexander | 14:8320b5ff96fa | 3572 | p_ranging_measurement_data->RangeDMaxMilliMeter; |
johnAlexander | 14:8320b5ff96fa | 3573 | last_range_data_buffer.MeasurementTimeUsec = |
johnAlexander | 14:8320b5ff96fa | 3574 | p_ranging_measurement_data->MeasurementTimeUsec; |
johnAlexander | 14:8320b5ff96fa | 3575 | last_range_data_buffer.SignalRateRtnMegaCps = |
johnAlexander | 14:8320b5ff96fa | 3576 | p_ranging_measurement_data->SignalRateRtnMegaCps; |
johnAlexander | 14:8320b5ff96fa | 3577 | last_range_data_buffer.AmbientRateRtnMegaCps = |
johnAlexander | 14:8320b5ff96fa | 3578 | p_ranging_measurement_data->AmbientRateRtnMegaCps; |
johnAlexander | 14:8320b5ff96fa | 3579 | last_range_data_buffer.EffectiveSpadRtnCount = |
johnAlexander | 14:8320b5ff96fa | 3580 | p_ranging_measurement_data->EffectiveSpadRtnCount; |
johnAlexander | 14:8320b5ff96fa | 3581 | last_range_data_buffer.RangeStatus = |
johnAlexander | 14:8320b5ff96fa | 3582 | p_ranging_measurement_data->RangeStatus; |
johnAlexander | 14:8320b5ff96fa | 3583 | |
johnAlexander | 14:8320b5ff96fa | 3584 | PALDevDataSet(dev, LastRangeMeasure, last_range_data_buffer); |
johnAlexander | 14:8320b5ff96fa | 3585 | } |
johnAlexander | 14:8320b5ff96fa | 3586 | |
johnAlexander | 14:8320b5ff96fa | 3587 | LOG_FUNCTION_END(status); |
johnAlexander | 14:8320b5ff96fa | 3588 | return status; |
johnAlexander | 0:c523920bcc09 | 3589 | } |
johnAlexander | 0:c523920bcc09 | 3590 | |
johnAlexander | 14:8320b5ff96fa | 3591 | VL53L0X_Error VL53L0X::vl53l0x_perform_single_ranging_measurement(VL53L0X_DEV dev, |
johnAlexander | 14:8320b5ff96fa | 3592 | VL53L0X_RangingMeasurementData_t *p_ranging_measurement_data) |
johnAlexander | 0:c523920bcc09 | 3593 | { |
johnAlexander | 14:8320b5ff96fa | 3594 | VL53L0X_Error status = VL53L0X_ERROR_NONE; |
johnAlexander | 14:8320b5ff96fa | 3595 | |
johnAlexander | 14:8320b5ff96fa | 3596 | LOG_FUNCTION_START(""); |
johnAlexander | 14:8320b5ff96fa | 3597 | |
johnAlexander | 14:8320b5ff96fa | 3598 | /* This function will do a complete single ranging |
johnAlexander | 14:8320b5ff96fa | 3599 | * Here we fix the mode! */ |
johnAlexander | 14:8320b5ff96fa | 3600 | status = vl53l0x_set_device_mode(dev, VL53L0X_DEVICEMODE_SINGLE_RANGING); |
johnAlexander | 14:8320b5ff96fa | 3601 | |
johnAlexander | 14:8320b5ff96fa | 3602 | if (status == VL53L0X_ERROR_NONE) |
johnAlexander | 14:8320b5ff96fa | 3603 | status = vl53l0x_perform_single_measurement(dev); |
johnAlexander | 14:8320b5ff96fa | 3604 | |
johnAlexander | 14:8320b5ff96fa | 3605 | |
johnAlexander | 14:8320b5ff96fa | 3606 | if (status == VL53L0X_ERROR_NONE) |
johnAlexander | 14:8320b5ff96fa | 3607 | status = vl53l0x_get_ranging_measurement_data(dev, |
johnAlexander | 14:8320b5ff96fa | 3608 | p_ranging_measurement_data); |
johnAlexander | 14:8320b5ff96fa | 3609 | |
johnAlexander | 14:8320b5ff96fa | 3610 | |
johnAlexander | 14:8320b5ff96fa | 3611 | if (status == VL53L0X_ERROR_NONE) |
johnAlexander | 14:8320b5ff96fa | 3612 | status = vl53l0x_clear_interrupt_mask(dev, 0); |
johnAlexander | 14:8320b5ff96fa | 3613 | |
johnAlexander | 14:8320b5ff96fa | 3614 | |
johnAlexander | 14:8320b5ff96fa | 3615 | LOG_FUNCTION_END(status); |
johnAlexander | 14:8320b5ff96fa | 3616 | return status; |
johnAlexander | 0:c523920bcc09 | 3617 | } |
johnAlexander | 0:c523920bcc09 | 3618 | |
johnAlexander | 14:8320b5ff96fa | 3619 | VL53L0X_Error VL53L0X::perform_ref_signal_measurement(VL53L0X_DEV dev, |
johnAlexander | 14:8320b5ff96fa | 3620 | uint16_t *p_ref_signal_rate) |
johnAlexander | 0:c523920bcc09 | 3621 | { |
johnAlexander | 14:8320b5ff96fa | 3622 | VL53L0X_Error status = VL53L0X_ERROR_NONE; |
johnAlexander | 14:8320b5ff96fa | 3623 | VL53L0X_RangingMeasurementData_t ranging_measurement_data; |
johnAlexander | 14:8320b5ff96fa | 3624 | |
johnAlexander | 14:8320b5ff96fa | 3625 | uint8_t sequence_config = 0; |
johnAlexander | 14:8320b5ff96fa | 3626 | |
johnAlexander | 14:8320b5ff96fa | 3627 | /* store the value of the sequence config, |
johnAlexander | 14:8320b5ff96fa | 3628 | * this will be reset before the end of the function |
johnAlexander | 14:8320b5ff96fa | 3629 | */ |
johnAlexander | 14:8320b5ff96fa | 3630 | |
johnAlexander | 14:8320b5ff96fa | 3631 | sequence_config = PALDevDataGet(dev, SequenceConfig); |
johnAlexander | 14:8320b5ff96fa | 3632 | |
johnAlexander | 14:8320b5ff96fa | 3633 | /* |
johnAlexander | 14:8320b5ff96fa | 3634 | * This function performs a reference signal rate measurement. |
johnAlexander | 14:8320b5ff96fa | 3635 | */ |
johnAlexander | 14:8320b5ff96fa | 3636 | if (status == VL53L0X_ERROR_NONE) |
johnAlexander | 14:8320b5ff96fa | 3637 | status = vl53l0x_write_byte(dev, |
johnAlexander | 14:8320b5ff96fa | 3638 | VL53L0X_REG_SYSTEM_SEQUENCE_CONFIG, 0xC0); |
johnAlexander | 14:8320b5ff96fa | 3639 | |
johnAlexander | 14:8320b5ff96fa | 3640 | if (status == VL53L0X_ERROR_NONE) |
johnAlexander | 14:8320b5ff96fa | 3641 | status = vl53l0x_perform_single_ranging_measurement(dev, |
johnAlexander | 14:8320b5ff96fa | 3642 | &ranging_measurement_data); |
johnAlexander | 14:8320b5ff96fa | 3643 | |
johnAlexander | 14:8320b5ff96fa | 3644 | if (status == VL53L0X_ERROR_NONE) |
johnAlexander | 14:8320b5ff96fa | 3645 | status = vl53l0x_write_byte(dev, 0xFF, 0x01); |
johnAlexander | 14:8320b5ff96fa | 3646 | |
johnAlexander | 14:8320b5ff96fa | 3647 | if (status == VL53L0X_ERROR_NONE) |
johnAlexander | 14:8320b5ff96fa | 3648 | status = vl53l0x_read_word(dev, |
johnAlexander | 14:8320b5ff96fa | 3649 | VL53L0X_REG_RESULT_PEAK_SIGNAL_RATE_REF, |
johnAlexander | 14:8320b5ff96fa | 3650 | p_ref_signal_rate); |
johnAlexander | 14:8320b5ff96fa | 3651 | |
johnAlexander | 14:8320b5ff96fa | 3652 | if (status == VL53L0X_ERROR_NONE) |
johnAlexander | 14:8320b5ff96fa | 3653 | status = vl53l0x_write_byte(dev, 0xFF, 0x00); |
johnAlexander | 14:8320b5ff96fa | 3654 | |
johnAlexander | 14:8320b5ff96fa | 3655 | if (status == VL53L0X_ERROR_NONE) { |
johnAlexander | 14:8320b5ff96fa | 3656 | /* restore the previous Sequence Config */ |
johnAlexander | 14:8320b5ff96fa | 3657 | status = vl53l0x_write_byte(dev, VL53L0X_REG_SYSTEM_SEQUENCE_CONFIG, |
johnAlexander | 14:8320b5ff96fa | 3658 | sequence_config); |
johnAlexander | 14:8320b5ff96fa | 3659 | if (status == VL53L0X_ERROR_NONE) |
johnAlexander | 14:8320b5ff96fa | 3660 | PALDevDataSet(dev, SequenceConfig, sequence_config); |
johnAlexander | 14:8320b5ff96fa | 3661 | } |
johnAlexander | 14:8320b5ff96fa | 3662 | |
johnAlexander | 14:8320b5ff96fa | 3663 | return status; |
johnAlexander | 0:c523920bcc09 | 3664 | } |
johnAlexander | 0:c523920bcc09 | 3665 | |
johnAlexander | 14:8320b5ff96fa | 3666 | VL53L0X_Error VL53L0X::wrapped_vl53l0x_perform_ref_spad_management(VL53L0X_DEV dev, |
johnAlexander | 14:8320b5ff96fa | 3667 | uint32_t *ref_spad_count, |
johnAlexander | 14:8320b5ff96fa | 3668 | uint8_t *is_aperture_spads) |
johnAlexander | 0:c523920bcc09 | 3669 | { |
johnAlexander | 14:8320b5ff96fa | 3670 | VL53L0X_Error status = VL53L0X_ERROR_NONE; |
johnAlexander | 14:8320b5ff96fa | 3671 | uint8_t last_spad_array[6]; |
johnAlexander | 14:8320b5ff96fa | 3672 | uint8_t start_select = 0xB4; |
johnAlexander | 14:8320b5ff96fa | 3673 | uint32_t minimum_spad_count = 3; |
johnAlexander | 14:8320b5ff96fa | 3674 | uint32_t max_spad_count = 44; |
johnAlexander | 14:8320b5ff96fa | 3675 | uint32_t current_spad_index = 0; |
johnAlexander | 14:8320b5ff96fa | 3676 | uint32_t last_spad_index = 0; |
johnAlexander | 14:8320b5ff96fa | 3677 | int32_t next_good_spad = 0; |
johnAlexander | 14:8320b5ff96fa | 3678 | uint16_t target_ref_rate = 0x0A00; /* 20 MCPS in 9:7 format */ |
johnAlexander | 14:8320b5ff96fa | 3679 | uint16_t peak_signal_rate_ref; |
johnAlexander | 14:8320b5ff96fa | 3680 | uint32_t need_apt_spads = 0; |
johnAlexander | 14:8320b5ff96fa | 3681 | uint32_t index = 0; |
johnAlexander | 14:8320b5ff96fa | 3682 | uint32_t spad_array_size = 6; |
johnAlexander | 14:8320b5ff96fa | 3683 | uint32_t signal_rate_diff = 0; |
johnAlexander | 14:8320b5ff96fa | 3684 | uint32_t last_signal_rate_diff = 0; |
johnAlexander | 14:8320b5ff96fa | 3685 | uint8_t complete = 0; |
johnAlexander | 14:8320b5ff96fa | 3686 | uint8_t vhv_settings = 0; |
johnAlexander | 14:8320b5ff96fa | 3687 | uint8_t phase_cal = 0; |
johnAlexander | 14:8320b5ff96fa | 3688 | uint32_t ref_spad_count_int = 0; |
johnAlexander | 14:8320b5ff96fa | 3689 | uint8_t is_aperture_spads_int = 0; |
johnAlexander | 14:8320b5ff96fa | 3690 | |
johnAlexander | 14:8320b5ff96fa | 3691 | /* |
johnAlexander | 14:8320b5ff96fa | 3692 | * The reference SPAD initialization procedure determines the minimum |
johnAlexander | 14:8320b5ff96fa | 3693 | * amount of reference spads to be enables to achieve a target reference |
johnAlexander | 14:8320b5ff96fa | 3694 | * signal rate and should be performed once during initialization. |
johnAlexander | 14:8320b5ff96fa | 3695 | * |
johnAlexander | 14:8320b5ff96fa | 3696 | * Either aperture or non-aperture spads are applied but never both. |
johnAlexander | 14:8320b5ff96fa | 3697 | * Firstly non-aperture spads are set, begining with 5 spads, and |
johnAlexander | 14:8320b5ff96fa | 3698 | * increased one spad at a time until the closest measurement to the |
johnAlexander | 14:8320b5ff96fa | 3699 | * target rate is achieved. |
johnAlexander | 14:8320b5ff96fa | 3700 | * |
johnAlexander | 14:8320b5ff96fa | 3701 | * If the target rate is exceeded when 5 non-aperture spads are enabled, |
johnAlexander | 14:8320b5ff96fa | 3702 | * initialization is performed instead with aperture spads. |
johnAlexander | 14:8320b5ff96fa | 3703 | * |
johnAlexander | 14:8320b5ff96fa | 3704 | * When setting spads, a 'Good Spad Map' is applied. |
johnAlexander | 14:8320b5ff96fa | 3705 | * |
johnAlexander | 14:8320b5ff96fa | 3706 | * This procedure operates within a SPAD window of interest of a maximum |
johnAlexander | 14:8320b5ff96fa | 3707 | * 44 spads. |
johnAlexander | 14:8320b5ff96fa | 3708 | * The start point is currently fixed to 180, which lies towards the end |
johnAlexander | 14:8320b5ff96fa | 3709 | * of the non-aperture quadrant and runs in to the adjacent aperture |
johnAlexander | 14:8320b5ff96fa | 3710 | * quadrant. |
johnAlexander | 14:8320b5ff96fa | 3711 | */ |
johnAlexander | 14:8320b5ff96fa | 3712 | |
johnAlexander | 14:8320b5ff96fa | 3713 | |
johnAlexander | 14:8320b5ff96fa | 3714 | target_ref_rate = PALDevDataGet(dev, targetRefRate); |
johnAlexander | 14:8320b5ff96fa | 3715 | |
johnAlexander | 14:8320b5ff96fa | 3716 | /* |
johnAlexander | 14:8320b5ff96fa | 3717 | * Initialize Spad arrays. |
johnAlexander | 14:8320b5ff96fa | 3718 | * Currently the good spad map is initialised to 'All good'. |
johnAlexander | 14:8320b5ff96fa | 3719 | * This is a short term implementation. The good spad map will be |
johnAlexander | 14:8320b5ff96fa | 3720 | * provided as an input. |
johnAlexander | 14:8320b5ff96fa | 3721 | * Note that there are 6 bytes. Only the first 44 bits will be used to |
johnAlexander | 14:8320b5ff96fa | 3722 | * represent spads. |
johnAlexander | 14:8320b5ff96fa | 3723 | */ |
johnAlexander | 14:8320b5ff96fa | 3724 | for (index = 0; index < spad_array_size; index++) |
johnAlexander | 14:8320b5ff96fa | 3725 | dev->Data.SpadData.RefSpadEnables[index] = 0; |
johnAlexander | 14:8320b5ff96fa | 3726 | |
johnAlexander | 14:8320b5ff96fa | 3727 | |
johnAlexander | 14:8320b5ff96fa | 3728 | status = vl53l0x_write_byte(dev, 0xFF, 0x01); |
johnAlexander | 14:8320b5ff96fa | 3729 | |
johnAlexander | 14:8320b5ff96fa | 3730 | if (status == VL53L0X_ERROR_NONE) |
johnAlexander | 14:8320b5ff96fa | 3731 | status = vl53l0x_write_byte(dev, |
johnAlexander | 14:8320b5ff96fa | 3732 | VL53L0X_REG_DYNAMIC_SPAD_REF_EN_START_OFFSET, 0x00); |
johnAlexander | 14:8320b5ff96fa | 3733 | |
johnAlexander | 14:8320b5ff96fa | 3734 | if (status == VL53L0X_ERROR_NONE) |
johnAlexander | 14:8320b5ff96fa | 3735 | status = vl53l0x_write_byte(dev, |
johnAlexander | 14:8320b5ff96fa | 3736 | VL53L0X_REG_DYNAMIC_SPAD_NUM_REQUESTED_REF_SPAD, 0x2C); |
johnAlexander | 14:8320b5ff96fa | 3737 | |
johnAlexander | 14:8320b5ff96fa | 3738 | if (status == VL53L0X_ERROR_NONE) |
johnAlexander | 14:8320b5ff96fa | 3739 | status = vl53l0x_write_byte(dev, 0xFF, 0x00); |
johnAlexander | 14:8320b5ff96fa | 3740 | |
johnAlexander | 14:8320b5ff96fa | 3741 | if (status == VL53L0X_ERROR_NONE) |
johnAlexander | 14:8320b5ff96fa | 3742 | status = vl53l0x_write_byte(dev, |
johnAlexander | 14:8320b5ff96fa | 3743 | VL53L0X_REG_GLOBAL_CONFIG_REF_EN_START_SELECT, |
johnAlexander | 14:8320b5ff96fa | 3744 | start_select); |
johnAlexander | 14:8320b5ff96fa | 3745 | |
johnAlexander | 14:8320b5ff96fa | 3746 | |
johnAlexander | 14:8320b5ff96fa | 3747 | if (status == VL53L0X_ERROR_NONE) |
johnAlexander | 14:8320b5ff96fa | 3748 | status = vl53l0x_write_byte(dev, |
johnAlexander | 14:8320b5ff96fa | 3749 | VL53L0X_REG_POWER_MANAGEMENT_GO1_POWER_FORCE, 0); |
johnAlexander | 14:8320b5ff96fa | 3750 | |
johnAlexander | 14:8320b5ff96fa | 3751 | /* Perform ref calibration */ |
johnAlexander | 14:8320b5ff96fa | 3752 | if (status == VL53L0X_ERROR_NONE) |
johnAlexander | 14:8320b5ff96fa | 3753 | status = vl53l0x_perform_ref_calibration(dev, &vhv_settings, |
johnAlexander | 14:8320b5ff96fa | 3754 | &phase_cal, 0); |
johnAlexander | 14:8320b5ff96fa | 3755 | |
johnAlexander | 14:8320b5ff96fa | 3756 | if (status == VL53L0X_ERROR_NONE) { |
johnAlexander | 14:8320b5ff96fa | 3757 | /* Enable Minimum NON-APERTURE Spads */ |
johnAlexander | 14:8320b5ff96fa | 3758 | current_spad_index = 0; |
johnAlexander | 14:8320b5ff96fa | 3759 | last_spad_index = current_spad_index; |
johnAlexander | 14:8320b5ff96fa | 3760 | need_apt_spads = 0; |
johnAlexander | 14:8320b5ff96fa | 3761 | status = enable_ref_spads(dev, |
johnAlexander | 14:8320b5ff96fa | 3762 | need_apt_spads, |
johnAlexander | 14:8320b5ff96fa | 3763 | dev->Data.SpadData.RefGoodSpadMap, |
johnAlexander | 14:8320b5ff96fa | 3764 | dev->Data.SpadData.RefSpadEnables, |
johnAlexander | 14:8320b5ff96fa | 3765 | spad_array_size, |
johnAlexander | 14:8320b5ff96fa | 3766 | start_select, |
johnAlexander | 14:8320b5ff96fa | 3767 | current_spad_index, |
johnAlexander | 14:8320b5ff96fa | 3768 | minimum_spad_count, |
johnAlexander | 14:8320b5ff96fa | 3769 | &last_spad_index); |
johnAlexander | 14:8320b5ff96fa | 3770 | } |
johnAlexander | 14:8320b5ff96fa | 3771 | |
johnAlexander | 14:8320b5ff96fa | 3772 | if (status == VL53L0X_ERROR_NONE) { |
johnAlexander | 14:8320b5ff96fa | 3773 | current_spad_index = last_spad_index; |
johnAlexander | 14:8320b5ff96fa | 3774 | |
johnAlexander | 14:8320b5ff96fa | 3775 | status = perform_ref_signal_measurement(dev, |
johnAlexander | 14:8320b5ff96fa | 3776 | &peak_signal_rate_ref); |
johnAlexander | 14:8320b5ff96fa | 3777 | if ((status == VL53L0X_ERROR_NONE) && |
johnAlexander | 14:8320b5ff96fa | 3778 | (peak_signal_rate_ref > target_ref_rate)) { |
johnAlexander | 14:8320b5ff96fa | 3779 | /* Signal rate measurement too high, |
johnAlexander | 14:8320b5ff96fa | 3780 | * switch to APERTURE SPADs */ |
johnAlexander | 14:8320b5ff96fa | 3781 | |
johnAlexander | 14:8320b5ff96fa | 3782 | for (index = 0; index < spad_array_size; index++) |
johnAlexander | 14:8320b5ff96fa | 3783 | dev->Data.SpadData.RefSpadEnables[index] = 0; |
johnAlexander | 14:8320b5ff96fa | 3784 | |
johnAlexander | 14:8320b5ff96fa | 3785 | |
johnAlexander | 14:8320b5ff96fa | 3786 | /* Increment to the first APERTURE spad */ |
johnAlexander | 14:8320b5ff96fa | 3787 | while ((is_aperture(start_select + current_spad_index) |
johnAlexander | 14:8320b5ff96fa | 3788 | == 0) && (current_spad_index < max_spad_count)) { |
johnAlexander | 14:8320b5ff96fa | 3789 | current_spad_index++; |
johnAlexander | 14:8320b5ff96fa | 3790 | } |
johnAlexander | 14:8320b5ff96fa | 3791 | |
johnAlexander | 14:8320b5ff96fa | 3792 | need_apt_spads = 1; |
johnAlexander | 14:8320b5ff96fa | 3793 | |
johnAlexander | 14:8320b5ff96fa | 3794 | status = enable_ref_spads(dev, |
johnAlexander | 14:8320b5ff96fa | 3795 | need_apt_spads, |
johnAlexander | 14:8320b5ff96fa | 3796 | dev->Data.SpadData.RefGoodSpadMap, |
johnAlexander | 14:8320b5ff96fa | 3797 | dev->Data.SpadData.RefSpadEnables, |
johnAlexander | 14:8320b5ff96fa | 3798 | spad_array_size, |
johnAlexander | 14:8320b5ff96fa | 3799 | start_select, |
johnAlexander | 14:8320b5ff96fa | 3800 | current_spad_index, |
johnAlexander | 14:8320b5ff96fa | 3801 | minimum_spad_count, |
johnAlexander | 14:8320b5ff96fa | 3802 | &last_spad_index); |
johnAlexander | 14:8320b5ff96fa | 3803 | |
johnAlexander | 14:8320b5ff96fa | 3804 | if (status == VL53L0X_ERROR_NONE) { |
johnAlexander | 14:8320b5ff96fa | 3805 | current_spad_index = last_spad_index; |
johnAlexander | 14:8320b5ff96fa | 3806 | status = perform_ref_signal_measurement(dev, |
johnAlexander | 14:8320b5ff96fa | 3807 | &peak_signal_rate_ref); |
johnAlexander | 14:8320b5ff96fa | 3808 | |
johnAlexander | 14:8320b5ff96fa | 3809 | if ((status == VL53L0X_ERROR_NONE) && |
johnAlexander | 14:8320b5ff96fa | 3810 | (peak_signal_rate_ref > target_ref_rate)) { |
johnAlexander | 14:8320b5ff96fa | 3811 | /* Signal rate still too high after |
johnAlexander | 14:8320b5ff96fa | 3812 | * setting the minimum number of |
johnAlexander | 14:8320b5ff96fa | 3813 | * APERTURE spads. Can do no more |
johnAlexander | 14:8320b5ff96fa | 3814 | * therefore set the min number of |
johnAlexander | 14:8320b5ff96fa | 3815 | * aperture spads as the result. |
johnAlexander | 14:8320b5ff96fa | 3816 | */ |
johnAlexander | 14:8320b5ff96fa | 3817 | is_aperture_spads_int = 1; |
johnAlexander | 14:8320b5ff96fa | 3818 | ref_spad_count_int = minimum_spad_count; |
johnAlexander | 14:8320b5ff96fa | 3819 | } |
johnAlexander | 14:8320b5ff96fa | 3820 | } |
johnAlexander | 14:8320b5ff96fa | 3821 | } else { |
johnAlexander | 14:8320b5ff96fa | 3822 | need_apt_spads = 0; |
johnAlexander | 14:8320b5ff96fa | 3823 | } |
johnAlexander | 14:8320b5ff96fa | 3824 | } |
johnAlexander | 14:8320b5ff96fa | 3825 | |
johnAlexander | 14:8320b5ff96fa | 3826 | if ((status == VL53L0X_ERROR_NONE) && |
johnAlexander | 14:8320b5ff96fa | 3827 | (peak_signal_rate_ref < target_ref_rate)) { |
johnAlexander | 14:8320b5ff96fa | 3828 | /* At this point, the minimum number of either aperture |
johnAlexander | 14:8320b5ff96fa | 3829 | * or non-aperture spads have been set. Proceed to add |
johnAlexander | 14:8320b5ff96fa | 3830 | * spads and perform measurements until the target |
johnAlexander | 14:8320b5ff96fa | 3831 | * reference is reached. |
johnAlexander | 14:8320b5ff96fa | 3832 | */ |
johnAlexander | 14:8320b5ff96fa | 3833 | is_aperture_spads_int = need_apt_spads; |
johnAlexander | 14:8320b5ff96fa | 3834 | ref_spad_count_int = minimum_spad_count; |
johnAlexander | 14:8320b5ff96fa | 3835 | |
johnAlexander | 14:8320b5ff96fa | 3836 | memcpy(last_spad_array, dev->Data.SpadData.RefSpadEnables, |
johnAlexander | 14:8320b5ff96fa | 3837 | spad_array_size); |
johnAlexander | 14:8320b5ff96fa | 3838 | last_signal_rate_diff = abs(peak_signal_rate_ref - |
johnAlexander | 14:8320b5ff96fa | 3839 | target_ref_rate); |
johnAlexander | 14:8320b5ff96fa | 3840 | complete = 0; |
johnAlexander | 14:8320b5ff96fa | 3841 | |
johnAlexander | 14:8320b5ff96fa | 3842 | while (!complete) { |
johnAlexander | 14:8320b5ff96fa | 3843 | get_next_good_spad( |
johnAlexander | 14:8320b5ff96fa | 3844 | dev->Data.SpadData.RefGoodSpadMap, |
johnAlexander | 14:8320b5ff96fa | 3845 | spad_array_size, current_spad_index, |
johnAlexander | 14:8320b5ff96fa | 3846 | &next_good_spad); |
johnAlexander | 14:8320b5ff96fa | 3847 | |
johnAlexander | 14:8320b5ff96fa | 3848 | if (next_good_spad == -1) { |
johnAlexander | 14:8320b5ff96fa | 3849 | status = VL53L0X_ERROR_REF_SPAD_INIT; |
johnAlexander | 14:8320b5ff96fa | 3850 | break; |
johnAlexander | 14:8320b5ff96fa | 3851 | } |
johnAlexander | 14:8320b5ff96fa | 3852 | |
johnAlexander | 14:8320b5ff96fa | 3853 | /* Cannot combine Aperture and Non-Aperture spads, so |
johnAlexander | 14:8320b5ff96fa | 3854 | * ensure the current spad is of the correct type. |
johnAlexander | 14:8320b5ff96fa | 3855 | */ |
johnAlexander | 14:8320b5ff96fa | 3856 | if (is_aperture((uint32_t)start_select + next_good_spad) != |
johnAlexander | 14:8320b5ff96fa | 3857 | need_apt_spads) { |
johnAlexander | 14:8320b5ff96fa | 3858 | /* At this point we have enabled the maximum |
johnAlexander | 14:8320b5ff96fa | 3859 | * number of Aperture spads. |
johnAlexander | 14:8320b5ff96fa | 3860 | */ |
johnAlexander | 14:8320b5ff96fa | 3861 | complete = 1; |
johnAlexander | 14:8320b5ff96fa | 3862 | break; |
johnAlexander | 14:8320b5ff96fa | 3863 | } |
johnAlexander | 14:8320b5ff96fa | 3864 | |
johnAlexander | 14:8320b5ff96fa | 3865 | (ref_spad_count_int)++; |
johnAlexander | 14:8320b5ff96fa | 3866 | |
johnAlexander | 14:8320b5ff96fa | 3867 | current_spad_index = next_good_spad; |
johnAlexander | 14:8320b5ff96fa | 3868 | status = enable_spad_bit( |
johnAlexander | 14:8320b5ff96fa | 3869 | dev->Data.SpadData.RefSpadEnables, |
johnAlexander | 14:8320b5ff96fa | 3870 | spad_array_size, current_spad_index); |
johnAlexander | 14:8320b5ff96fa | 3871 | |
johnAlexander | 14:8320b5ff96fa | 3872 | if (status == VL53L0X_ERROR_NONE) { |
johnAlexander | 14:8320b5ff96fa | 3873 | current_spad_index++; |
johnAlexander | 14:8320b5ff96fa | 3874 | /* Proceed to apply the additional spad and |
johnAlexander | 14:8320b5ff96fa | 3875 | * perform measurement. */ |
johnAlexander | 14:8320b5ff96fa | 3876 | status = set_ref_spad_map(dev, |
johnAlexander | 14:8320b5ff96fa | 3877 | dev->Data.SpadData.RefSpadEnables); |
johnAlexander | 14:8320b5ff96fa | 3878 | } |
johnAlexander | 14:8320b5ff96fa | 3879 | |
johnAlexander | 14:8320b5ff96fa | 3880 | if (status != VL53L0X_ERROR_NONE) |
johnAlexander | 14:8320b5ff96fa | 3881 | break; |
johnAlexander | 14:8320b5ff96fa | 3882 | |
johnAlexander | 14:8320b5ff96fa | 3883 | status = perform_ref_signal_measurement(dev, |
johnAlexander | 14:8320b5ff96fa | 3884 | &peak_signal_rate_ref); |
johnAlexander | 14:8320b5ff96fa | 3885 | |
johnAlexander | 14:8320b5ff96fa | 3886 | if (status != VL53L0X_ERROR_NONE) |
johnAlexander | 14:8320b5ff96fa | 3887 | break; |
johnAlexander | 14:8320b5ff96fa | 3888 | |
johnAlexander | 14:8320b5ff96fa | 3889 | signal_rate_diff = abs(peak_signal_rate_ref - target_ref_rate); |
johnAlexander | 14:8320b5ff96fa | 3890 | |
johnAlexander | 14:8320b5ff96fa | 3891 | if (peak_signal_rate_ref > target_ref_rate) { |
johnAlexander | 14:8320b5ff96fa | 3892 | /* Select the spad map that provides the |
johnAlexander | 14:8320b5ff96fa | 3893 | * measurement closest to the target rate, |
johnAlexander | 14:8320b5ff96fa | 3894 | * either above or below it. |
johnAlexander | 14:8320b5ff96fa | 3895 | */ |
johnAlexander | 14:8320b5ff96fa | 3896 | if (signal_rate_diff > last_signal_rate_diff) { |
johnAlexander | 14:8320b5ff96fa | 3897 | /* Previous spad map produced a closer |
johnAlexander | 14:8320b5ff96fa | 3898 | * measurement, so choose this. */ |
johnAlexander | 14:8320b5ff96fa | 3899 | status = set_ref_spad_map(dev, |
johnAlexander | 14:8320b5ff96fa | 3900 | last_spad_array); |
johnAlexander | 14:8320b5ff96fa | 3901 | memcpy( |
johnAlexander | 14:8320b5ff96fa | 3902 | dev->Data.SpadData.RefSpadEnables, |
johnAlexander | 14:8320b5ff96fa | 3903 | last_spad_array, spad_array_size); |
johnAlexander | 14:8320b5ff96fa | 3904 | |
johnAlexander | 14:8320b5ff96fa | 3905 | (ref_spad_count_int)--; |
johnAlexander | 14:8320b5ff96fa | 3906 | } |
johnAlexander | 14:8320b5ff96fa | 3907 | complete = 1; |
johnAlexander | 14:8320b5ff96fa | 3908 | } else { |
johnAlexander | 14:8320b5ff96fa | 3909 | /* Continue to add spads */ |
johnAlexander | 14:8320b5ff96fa | 3910 | last_signal_rate_diff = signal_rate_diff; |
johnAlexander | 14:8320b5ff96fa | 3911 | memcpy(last_spad_array, |
johnAlexander | 14:8320b5ff96fa | 3912 | dev->Data.SpadData.RefSpadEnables, |
johnAlexander | 14:8320b5ff96fa | 3913 | spad_array_size); |
johnAlexander | 14:8320b5ff96fa | 3914 | } |
johnAlexander | 14:8320b5ff96fa | 3915 | |
johnAlexander | 14:8320b5ff96fa | 3916 | } /* while */ |
johnAlexander | 14:8320b5ff96fa | 3917 | } |
johnAlexander | 14:8320b5ff96fa | 3918 | |
johnAlexander | 14:8320b5ff96fa | 3919 | if (status == VL53L0X_ERROR_NONE) { |
johnAlexander | 14:8320b5ff96fa | 3920 | *ref_spad_count = ref_spad_count_int; |
johnAlexander | 14:8320b5ff96fa | 3921 | *is_aperture_spads = is_aperture_spads_int; |
johnAlexander | 14:8320b5ff96fa | 3922 | |
johnAlexander | 14:8320b5ff96fa | 3923 | VL53L0X_SETDEVICESPECIFICPARAMETER(dev, RefSpadsInitialised, 1); |
johnAlexander | 14:8320b5ff96fa | 3924 | VL53L0X_SETDEVICESPECIFICPARAMETER(dev, |
johnAlexander | 14:8320b5ff96fa | 3925 | ReferenceSpadCount, (uint8_t)(*ref_spad_count)); |
johnAlexander | 14:8320b5ff96fa | 3926 | VL53L0X_SETDEVICESPECIFICPARAMETER(dev, |
johnAlexander | 14:8320b5ff96fa | 3927 | ReferenceSpadType, *is_aperture_spads); |
johnAlexander | 14:8320b5ff96fa | 3928 | } |
johnAlexander | 14:8320b5ff96fa | 3929 | |
johnAlexander | 14:8320b5ff96fa | 3930 | return status; |
johnAlexander | 0:c523920bcc09 | 3931 | } |
johnAlexander | 0:c523920bcc09 | 3932 | |
johnAlexander | 14:8320b5ff96fa | 3933 | VL53L0X_Error VL53L0X::vl53l0x_set_reference_spads(VL53L0X_DEV dev, |
johnAlexander | 14:8320b5ff96fa | 3934 | uint32_t count, uint8_t is_aperture_spads) |
johnAlexander | 0:c523920bcc09 | 3935 | { |
johnAlexander | 14:8320b5ff96fa | 3936 | VL53L0X_Error status = VL53L0X_ERROR_NONE; |
johnAlexander | 14:8320b5ff96fa | 3937 | uint32_t current_spad_index = 0; |
johnAlexander | 14:8320b5ff96fa | 3938 | uint8_t start_select = 0xB4; |
johnAlexander | 14:8320b5ff96fa | 3939 | uint32_t spad_array_size = 6; |
johnAlexander | 14:8320b5ff96fa | 3940 | uint32_t max_spad_count = 44; |
johnAlexander | 14:8320b5ff96fa | 3941 | uint32_t last_spad_index; |
johnAlexander | 14:8320b5ff96fa | 3942 | uint32_t index; |
johnAlexander | 14:8320b5ff96fa | 3943 | |
johnAlexander | 14:8320b5ff96fa | 3944 | /* |
johnAlexander | 14:8320b5ff96fa | 3945 | * This function applies a requested number of reference spads, either |
johnAlexander | 14:8320b5ff96fa | 3946 | * aperture or |
johnAlexander | 14:8320b5ff96fa | 3947 | * non-aperture, as requested. |
johnAlexander | 14:8320b5ff96fa | 3948 | * The good spad map will be applied. |
johnAlexander | 14:8320b5ff96fa | 3949 | */ |
johnAlexander | 14:8320b5ff96fa | 3950 | |
johnAlexander | 14:8320b5ff96fa | 3951 | status = vl53l0x_write_byte(dev, 0xFF, 0x01); |
johnAlexander | 14:8320b5ff96fa | 3952 | |
johnAlexander | 14:8320b5ff96fa | 3953 | if (status == VL53L0X_ERROR_NONE) |
johnAlexander | 14:8320b5ff96fa | 3954 | status = vl53l0x_write_byte(dev, |
johnAlexander | 14:8320b5ff96fa | 3955 | VL53L0X_REG_DYNAMIC_SPAD_REF_EN_START_OFFSET, 0x00); |
johnAlexander | 14:8320b5ff96fa | 3956 | |
johnAlexander | 14:8320b5ff96fa | 3957 | if (status == VL53L0X_ERROR_NONE) |
johnAlexander | 14:8320b5ff96fa | 3958 | status = vl53l0x_write_byte(dev, |
johnAlexander | 14:8320b5ff96fa | 3959 | VL53L0X_REG_DYNAMIC_SPAD_NUM_REQUESTED_REF_SPAD, 0x2C); |
johnAlexander | 14:8320b5ff96fa | 3960 | |
johnAlexander | 14:8320b5ff96fa | 3961 | if (status == VL53L0X_ERROR_NONE) |
johnAlexander | 14:8320b5ff96fa | 3962 | status = vl53l0x_write_byte(dev, 0xFF, 0x00); |
johnAlexander | 14:8320b5ff96fa | 3963 | |
johnAlexander | 14:8320b5ff96fa | 3964 | if (status == VL53L0X_ERROR_NONE) |
johnAlexander | 14:8320b5ff96fa | 3965 | status = vl53l0x_write_byte(dev, |
johnAlexander | 14:8320b5ff96fa | 3966 | VL53L0X_REG_GLOBAL_CONFIG_REF_EN_START_SELECT, |
johnAlexander | 14:8320b5ff96fa | 3967 | start_select); |
johnAlexander | 14:8320b5ff96fa | 3968 | |
johnAlexander | 14:8320b5ff96fa | 3969 | for (index = 0; index < spad_array_size; index++) |
johnAlexander | 14:8320b5ff96fa | 3970 | dev->Data.SpadData.RefSpadEnables[index] = 0; |
johnAlexander | 14:8320b5ff96fa | 3971 | |
johnAlexander | 14:8320b5ff96fa | 3972 | if (is_aperture_spads) { |
johnAlexander | 14:8320b5ff96fa | 3973 | /* Increment to the first APERTURE spad */ |
johnAlexander | 14:8320b5ff96fa | 3974 | while ((is_aperture(start_select + current_spad_index) == 0) && |
johnAlexander | 14:8320b5ff96fa | 3975 | (current_spad_index < max_spad_count)) { |
johnAlexander | 14:8320b5ff96fa | 3976 | current_spad_index++; |
johnAlexander | 14:8320b5ff96fa | 3977 | } |
johnAlexander | 14:8320b5ff96fa | 3978 | } |
johnAlexander | 14:8320b5ff96fa | 3979 | status = enable_ref_spads(dev, |
johnAlexander | 14:8320b5ff96fa | 3980 | is_aperture_spads, |
johnAlexander | 14:8320b5ff96fa | 3981 | dev->Data.SpadData.RefGoodSpadMap, |
johnAlexander | 14:8320b5ff96fa | 3982 | dev->Data.SpadData.RefSpadEnables, |
johnAlexander | 14:8320b5ff96fa | 3983 | spad_array_size, |
johnAlexander | 14:8320b5ff96fa | 3984 | start_select, |
johnAlexander | 14:8320b5ff96fa | 3985 | current_spad_index, |
johnAlexander | 14:8320b5ff96fa | 3986 | count, |
johnAlexander | 14:8320b5ff96fa | 3987 | &last_spad_index); |
johnAlexander | 14:8320b5ff96fa | 3988 | |
johnAlexander | 14:8320b5ff96fa | 3989 | if (status == VL53L0X_ERROR_NONE) { |
johnAlexander | 14:8320b5ff96fa | 3990 | VL53L0X_SETDEVICESPECIFICPARAMETER(dev, RefSpadsInitialised, 1); |
johnAlexander | 14:8320b5ff96fa | 3991 | VL53L0X_SETDEVICESPECIFICPARAMETER(dev, |
johnAlexander | 14:8320b5ff96fa | 3992 | ReferenceSpadCount, (uint8_t)(count)); |
johnAlexander | 14:8320b5ff96fa | 3993 | VL53L0X_SETDEVICESPECIFICPARAMETER(dev, |
johnAlexander | 14:8320b5ff96fa | 3994 | ReferenceSpadType, is_aperture_spads); |
johnAlexander | 14:8320b5ff96fa | 3995 | } |
johnAlexander | 14:8320b5ff96fa | 3996 | |
johnAlexander | 14:8320b5ff96fa | 3997 | return status; |
johnAlexander | 0:c523920bcc09 | 3998 | } |
johnAlexander | 0:c523920bcc09 | 3999 | |
johnAlexander | 14:8320b5ff96fa | 4000 | VL53L0X_Error VL53L0X::vl53l0x_wait_device_booted(VL53L0X_DEV dev) |
johnAlexander | 0:c523920bcc09 | 4001 | { |
johnAlexander | 14:8320b5ff96fa | 4002 | VL53L0X_Error status = VL53L0X_ERROR_NOT_IMPLEMENTED; |
johnAlexander | 14:8320b5ff96fa | 4003 | LOG_FUNCTION_START(""); |
johnAlexander | 14:8320b5ff96fa | 4004 | |
johnAlexander | 14:8320b5ff96fa | 4005 | /* not implemented on VL53L0X */ |
johnAlexander | 14:8320b5ff96fa | 4006 | |
johnAlexander | 14:8320b5ff96fa | 4007 | LOG_FUNCTION_END(status); |
johnAlexander | 14:8320b5ff96fa | 4008 | return status; |
johnAlexander | 0:c523920bcc09 | 4009 | } |
johnAlexander | 0:c523920bcc09 | 4010 | |
johnAlexander | 14:8320b5ff96fa | 4011 | VL53L0X_Error VL53L0X::vl53l0x_perform_ref_calibration(VL53L0X_DEV dev, uint8_t *p_vhv_settings, |
johnAlexander | 14:8320b5ff96fa | 4012 | uint8_t *p_phase_cal) |
johnAlexander | 0:c523920bcc09 | 4013 | { |
johnAlexander | 14:8320b5ff96fa | 4014 | VL53L0X_Error status = VL53L0X_ERROR_NONE; |
johnAlexander | 14:8320b5ff96fa | 4015 | LOG_FUNCTION_START(""); |
johnAlexander | 14:8320b5ff96fa | 4016 | |
johnAlexander | 14:8320b5ff96fa | 4017 | status = vl53l0x_perform_ref_calibration(dev, p_vhv_settings, |
johnAlexander | 14:8320b5ff96fa | 4018 | p_phase_cal, 1); |
johnAlexander | 14:8320b5ff96fa | 4019 | |
johnAlexander | 14:8320b5ff96fa | 4020 | LOG_FUNCTION_END(status); |
johnAlexander | 14:8320b5ff96fa | 4021 | return status; |
johnAlexander | 0:c523920bcc09 | 4022 | } |
johnAlexander | 0:c523920bcc09 | 4023 | |
johnAlexander | 14:8320b5ff96fa | 4024 | VL53L0X_Error VL53L0X::vl53l0x_perform_ref_spad_management(VL53L0X_DEV dev, |
johnAlexander | 14:8320b5ff96fa | 4025 | uint32_t *ref_spad_count, uint8_t *is_aperture_spads) |
johnAlexander | 0:c523920bcc09 | 4026 | { |
johnAlexander | 14:8320b5ff96fa | 4027 | VL53L0X_Error status = VL53L0X_ERROR_NONE; |
johnAlexander | 14:8320b5ff96fa | 4028 | LOG_FUNCTION_START(""); |
johnAlexander | 14:8320b5ff96fa | 4029 | |
johnAlexander | 14:8320b5ff96fa | 4030 | status = wrapped_vl53l0x_perform_ref_spad_management(dev, ref_spad_count, |
johnAlexander | 14:8320b5ff96fa | 4031 | is_aperture_spads); |
johnAlexander | 14:8320b5ff96fa | 4032 | |
johnAlexander | 14:8320b5ff96fa | 4033 | LOG_FUNCTION_END(status); |
johnAlexander | 14:8320b5ff96fa | 4034 | |
johnAlexander | 14:8320b5ff96fa | 4035 | return status; |
johnAlexander | 0:c523920bcc09 | 4036 | } |
johnAlexander | 0:c523920bcc09 | 4037 | |
johnAlexander | 0:c523920bcc09 | 4038 | /* Group PAL Init Functions */ |
johnAlexander | 14:8320b5ff96fa | 4039 | VL53L0X_Error VL53L0X::vl53l0x_set_device_address(VL53L0X_DEV dev, uint8_t device_address) |
johnAlexander | 0:c523920bcc09 | 4040 | { |
johnAlexander | 14:8320b5ff96fa | 4041 | VL53L0X_Error status = VL53L0X_ERROR_NONE; |
johnAlexander | 14:8320b5ff96fa | 4042 | LOG_FUNCTION_START(""); |
johnAlexander | 14:8320b5ff96fa | 4043 | |
johnAlexander | 14:8320b5ff96fa | 4044 | status = vl53l0x_write_byte(dev, VL53L0X_REG_I2C_SLAVE_DEVICE_ADDRESS, |
johnAlexander | 14:8320b5ff96fa | 4045 | device_address / 2); |
johnAlexander | 14:8320b5ff96fa | 4046 | |
johnAlexander | 14:8320b5ff96fa | 4047 | LOG_FUNCTION_END(status); |
johnAlexander | 14:8320b5ff96fa | 4048 | return status; |
johnAlexander | 0:c523920bcc09 | 4049 | } |
johnAlexander | 0:c523920bcc09 | 4050 | |
johnAlexander | 14:8320b5ff96fa | 4051 | VL53L0X_Error VL53L0X::vl53l0x_set_gpio_config(VL53L0X_DEV dev, uint8_t pin, |
johnAlexander | 14:8320b5ff96fa | 4052 | VL53L0X_DeviceModes device_mode, VL53L0X_GpioFunctionality functionality, |
johnAlexander | 14:8320b5ff96fa | 4053 | VL53L0X_InterruptPolarity polarity) |
johnAlexander | 0:c523920bcc09 | 4054 | { |
johnAlexander | 14:8320b5ff96fa | 4055 | VL53L0X_Error status = VL53L0X_ERROR_NONE; |
johnAlexander | 14:8320b5ff96fa | 4056 | uint8_t data; |
johnAlexander | 14:8320b5ff96fa | 4057 | |
johnAlexander | 14:8320b5ff96fa | 4058 | LOG_FUNCTION_START(""); |
johnAlexander | 14:8320b5ff96fa | 4059 | |
johnAlexander | 14:8320b5ff96fa | 4060 | if (pin != 0) { |
johnAlexander | 14:8320b5ff96fa | 4061 | status = VL53L0X_ERROR_GPIO_NOT_EXISTING; |
johnAlexander | 14:8320b5ff96fa | 4062 | } else if (device_mode == VL53L0X_DEVICEMODE_GPIO_DRIVE) { |
johnAlexander | 14:8320b5ff96fa | 4063 | if (polarity == VL53L0X_INTERRUPTPOLARITY_LOW) |
johnAlexander | 14:8320b5ff96fa | 4064 | data = 0x10; |
johnAlexander | 14:8320b5ff96fa | 4065 | else |
johnAlexander | 14:8320b5ff96fa | 4066 | data = 1; |
johnAlexander | 14:8320b5ff96fa | 4067 | |
johnAlexander | 14:8320b5ff96fa | 4068 | status = vl53l0x_write_byte(dev, |
johnAlexander | 14:8320b5ff96fa | 4069 | VL53L0X_REG_GPIO_HV_MUX_ACTIVE_HIGH, data); |
johnAlexander | 14:8320b5ff96fa | 4070 | |
johnAlexander | 14:8320b5ff96fa | 4071 | } else if (device_mode == VL53L0X_DEVICEMODE_GPIO_OSC) { |
johnAlexander | 14:8320b5ff96fa | 4072 | |
johnAlexander | 14:8320b5ff96fa | 4073 | status |= vl53l0x_write_byte(dev, 0xff, 0x01); |
johnAlexander | 14:8320b5ff96fa | 4074 | status |= vl53l0x_write_byte(dev, 0x00, 0x00); |
johnAlexander | 14:8320b5ff96fa | 4075 | |
johnAlexander | 14:8320b5ff96fa | 4076 | status |= vl53l0x_write_byte(dev, 0xff, 0x00); |
johnAlexander | 14:8320b5ff96fa | 4077 | status |= vl53l0x_write_byte(dev, 0x80, 0x01); |
johnAlexander | 14:8320b5ff96fa | 4078 | status |= vl53l0x_write_byte(dev, 0x85, 0x02); |
johnAlexander | 14:8320b5ff96fa | 4079 | |
johnAlexander | 14:8320b5ff96fa | 4080 | status |= vl53l0x_write_byte(dev, 0xff, 0x04); |
johnAlexander | 14:8320b5ff96fa | 4081 | status |= vl53l0x_write_byte(dev, 0xcd, 0x00); |
johnAlexander | 14:8320b5ff96fa | 4082 | status |= vl53l0x_write_byte(dev, 0xcc, 0x11); |
johnAlexander | 14:8320b5ff96fa | 4083 | |
johnAlexander | 14:8320b5ff96fa | 4084 | status |= vl53l0x_write_byte(dev, 0xff, 0x07); |
johnAlexander | 14:8320b5ff96fa | 4085 | status |= vl53l0x_write_byte(dev, 0xbe, 0x00); |
johnAlexander | 14:8320b5ff96fa | 4086 | |
johnAlexander | 14:8320b5ff96fa | 4087 | status |= vl53l0x_write_byte(dev, 0xff, 0x06); |
johnAlexander | 14:8320b5ff96fa | 4088 | status |= vl53l0x_write_byte(dev, 0xcc, 0x09); |
johnAlexander | 14:8320b5ff96fa | 4089 | |
johnAlexander | 14:8320b5ff96fa | 4090 | status |= vl53l0x_write_byte(dev, 0xff, 0x00); |
johnAlexander | 14:8320b5ff96fa | 4091 | status |= vl53l0x_write_byte(dev, 0xff, 0x01); |
johnAlexander | 14:8320b5ff96fa | 4092 | status |= vl53l0x_write_byte(dev, 0x00, 0x00); |
johnAlexander | 14:8320b5ff96fa | 4093 | |
johnAlexander | 14:8320b5ff96fa | 4094 | } else { |
johnAlexander | 14:8320b5ff96fa | 4095 | |
johnAlexander | 14:8320b5ff96fa | 4096 | if (status == VL53L0X_ERROR_NONE) { |
johnAlexander | 14:8320b5ff96fa | 4097 | switch (functionality) { |
johnAlexander | 14:8320b5ff96fa | 4098 | case VL53L0X_GPIOFUNCTIONALITY_OFF: |
johnAlexander | 14:8320b5ff96fa | 4099 | data = 0x00; |
johnAlexander | 14:8320b5ff96fa | 4100 | break; |
johnAlexander | 14:8320b5ff96fa | 4101 | case VL53L0X_GPIOFUNCTIONALITY_THRESHOLD_CROSSED_LOW: |
johnAlexander | 14:8320b5ff96fa | 4102 | data = 0x01; |
johnAlexander | 14:8320b5ff96fa | 4103 | break; |
johnAlexander | 14:8320b5ff96fa | 4104 | case VL53L0X_GPIOFUNCTIONALITY_THRESHOLD_CROSSED_HIGH: |
johnAlexander | 14:8320b5ff96fa | 4105 | data = 0x02; |
johnAlexander | 14:8320b5ff96fa | 4106 | break; |
johnAlexander | 14:8320b5ff96fa | 4107 | case VL53L0X_GPIOFUNCTIONALITY_THRESHOLD_CROSSED_OUT: |
johnAlexander | 14:8320b5ff96fa | 4108 | data = 0x03; |
johnAlexander | 14:8320b5ff96fa | 4109 | break; |
johnAlexander | 14:8320b5ff96fa | 4110 | case VL53L0X_GPIOFUNCTIONALITY_NEW_MEASURE_READY: |
johnAlexander | 14:8320b5ff96fa | 4111 | data = 0x04; |
johnAlexander | 14:8320b5ff96fa | 4112 | break; |
johnAlexander | 14:8320b5ff96fa | 4113 | default: |
johnAlexander | 14:8320b5ff96fa | 4114 | status = |
johnAlexander | 14:8320b5ff96fa | 4115 | VL53L0X_ERROR_GPIO_FUNCTIONALITY_NOT_SUPPORTED; |
johnAlexander | 14:8320b5ff96fa | 4116 | } |
johnAlexander | 14:8320b5ff96fa | 4117 | } |
johnAlexander | 14:8320b5ff96fa | 4118 | |
johnAlexander | 14:8320b5ff96fa | 4119 | if (status == VL53L0X_ERROR_NONE) |
johnAlexander | 14:8320b5ff96fa | 4120 | status = vl53l0x_write_byte(dev, |
johnAlexander | 14:8320b5ff96fa | 4121 | VL53L0X_REG_SYSTEM_INTERRUPT_CONFIG_GPIO, data); |
johnAlexander | 14:8320b5ff96fa | 4122 | |
johnAlexander | 14:8320b5ff96fa | 4123 | if (status == VL53L0X_ERROR_NONE) { |
johnAlexander | 14:8320b5ff96fa | 4124 | if (polarity == VL53L0X_INTERRUPTPOLARITY_LOW) |
johnAlexander | 14:8320b5ff96fa | 4125 | data = 0; |
johnAlexander | 14:8320b5ff96fa | 4126 | else |
johnAlexander | 14:8320b5ff96fa | 4127 | data = (uint8_t)(1 << 4); |
johnAlexander | 14:8320b5ff96fa | 4128 | |
johnAlexander | 14:8320b5ff96fa | 4129 | status = vl53l0x_update_byte(dev, |
johnAlexander | 14:8320b5ff96fa | 4130 | VL53L0X_REG_GPIO_HV_MUX_ACTIVE_HIGH, 0xEF, data); |
johnAlexander | 14:8320b5ff96fa | 4131 | } |
johnAlexander | 14:8320b5ff96fa | 4132 | |
johnAlexander | 14:8320b5ff96fa | 4133 | if (status == VL53L0X_ERROR_NONE) |
johnAlexander | 14:8320b5ff96fa | 4134 | VL53L0X_SETDEVICESPECIFICPARAMETER(dev, |
johnAlexander | 14:8320b5ff96fa | 4135 | Pin0GpioFunctionality, functionality); |
johnAlexander | 14:8320b5ff96fa | 4136 | |
johnAlexander | 14:8320b5ff96fa | 4137 | if (status == VL53L0X_ERROR_NONE) |
johnAlexander | 14:8320b5ff96fa | 4138 | status = vl53l0x_clear_interrupt_mask(dev, 0); |
johnAlexander | 14:8320b5ff96fa | 4139 | |
johnAlexander | 14:8320b5ff96fa | 4140 | } |
johnAlexander | 14:8320b5ff96fa | 4141 | LOG_FUNCTION_END(status); |
johnAlexander | 14:8320b5ff96fa | 4142 | return status; |
johnAlexander | 0:c523920bcc09 | 4143 | } |
johnAlexander | 0:c523920bcc09 | 4144 | |
johnAlexander | 14:8320b5ff96fa | 4145 | VL53L0X_Error VL53L0X::vl53l0x_get_fraction_enable(VL53L0X_DEV dev, uint8_t *p_enabled) |
johnAlexander | 0:c523920bcc09 | 4146 | { |
johnAlexander | 14:8320b5ff96fa | 4147 | VL53L0X_Error status = VL53L0X_ERROR_NONE; |
johnAlexander | 14:8320b5ff96fa | 4148 | LOG_FUNCTION_START(""); |
johnAlexander | 14:8320b5ff96fa | 4149 | |
johnAlexander | 14:8320b5ff96fa | 4150 | status = vl53l0x_read_byte(dev, VL53L0X_REG_SYSTEM_RANGE_CONFIG, p_enabled); |
johnAlexander | 14:8320b5ff96fa | 4151 | |
johnAlexander | 14:8320b5ff96fa | 4152 | if (status == VL53L0X_ERROR_NONE) |
johnAlexander | 14:8320b5ff96fa | 4153 | *p_enabled = (*p_enabled & 1); |
johnAlexander | 14:8320b5ff96fa | 4154 | |
johnAlexander | 14:8320b5ff96fa | 4155 | LOG_FUNCTION_END(status); |
johnAlexander | 14:8320b5ff96fa | 4156 | return status; |
johnAlexander | 0:c523920bcc09 | 4157 | } |
johnAlexander | 0:c523920bcc09 | 4158 | |
johnAlexander | 14:8320b5ff96fa | 4159 | uint16_t VL53L0X::vl53l0x_encode_timeout(uint32_t timeout_macro_clks) |
johnAlexander | 0:c523920bcc09 | 4160 | { |
johnAlexander | 14:8320b5ff96fa | 4161 | /*! |
johnAlexander | 14:8320b5ff96fa | 4162 | * Encode timeout in macro periods in (LSByte * 2^MSByte) + 1 format |
johnAlexander | 14:8320b5ff96fa | 4163 | */ |
johnAlexander | 14:8320b5ff96fa | 4164 | |
johnAlexander | 14:8320b5ff96fa | 4165 | uint16_t encoded_timeout = 0; |
johnAlexander | 14:8320b5ff96fa | 4166 | uint32_t ls_byte = 0; |
johnAlexander | 14:8320b5ff96fa | 4167 | uint16_t ms_byte = 0; |
johnAlexander | 14:8320b5ff96fa | 4168 | |
johnAlexander | 14:8320b5ff96fa | 4169 | if (timeout_macro_clks > 0) { |
johnAlexander | 14:8320b5ff96fa | 4170 | ls_byte = timeout_macro_clks - 1; |
johnAlexander | 14:8320b5ff96fa | 4171 | |
johnAlexander | 14:8320b5ff96fa | 4172 | while ((ls_byte & 0xFFFFFF00) > 0) { |
johnAlexander | 14:8320b5ff96fa | 4173 | ls_byte = ls_byte >> 1; |
johnAlexander | 14:8320b5ff96fa | 4174 | ms_byte++; |
johnAlexander | 14:8320b5ff96fa | 4175 | } |
johnAlexander | 14:8320b5ff96fa | 4176 | |
johnAlexander | 14:8320b5ff96fa | 4177 | encoded_timeout = (ms_byte << 8) |
johnAlexander | 14:8320b5ff96fa | 4178 | + (uint16_t)(ls_byte & 0x000000FF); |
johnAlexander | 14:8320b5ff96fa | 4179 | } |
johnAlexander | 14:8320b5ff96fa | 4180 | |
johnAlexander | 14:8320b5ff96fa | 4181 | return encoded_timeout; |
johnAlexander | 0:c523920bcc09 | 4182 | |
johnAlexander | 0:c523920bcc09 | 4183 | } |
johnAlexander | 0:c523920bcc09 | 4184 | |
johnAlexander | 14:8320b5ff96fa | 4185 | VL53L0X_Error VL53L0X::set_sequence_step_timeout(VL53L0X_DEV dev, |
johnAlexander | 14:8320b5ff96fa | 4186 | VL53L0X_SequenceStepId sequence_step_id, |
johnAlexander | 14:8320b5ff96fa | 4187 | uint32_t timeout_micro_secs) |
johnAlexander | 0:c523920bcc09 | 4188 | { |
johnAlexander | 14:8320b5ff96fa | 4189 | VL53L0X_Error status = VL53L0X_ERROR_NONE; |
johnAlexander | 14:8320b5ff96fa | 4190 | uint8_t current_vcsel_pulse_period_p_clk; |
johnAlexander | 14:8320b5ff96fa | 4191 | uint8_t msrc_encoded_time_out; |
johnAlexander | 14:8320b5ff96fa | 4192 | uint16_t pre_range_encoded_time_out; |
johnAlexander | 14:8320b5ff96fa | 4193 | uint16_t pre_range_time_out_m_clks; |
johnAlexander | 14:8320b5ff96fa | 4194 | uint16_t msrc_range_time_out_m_clks; |
johnAlexander | 14:8320b5ff96fa | 4195 | uint32_t final_range_time_out_m_clks; |
johnAlexander | 14:8320b5ff96fa | 4196 | uint16_t final_range_encoded_time_out; |
johnAlexander | 14:8320b5ff96fa | 4197 | VL53L0X_SchedulerSequenceSteps_t scheduler_sequence_steps; |
johnAlexander | 14:8320b5ff96fa | 4198 | |
johnAlexander | 14:8320b5ff96fa | 4199 | if ((sequence_step_id == VL53L0X_SEQUENCESTEP_TCC) || |
johnAlexander | 14:8320b5ff96fa | 4200 | (sequence_step_id == VL53L0X_SEQUENCESTEP_DSS) || |
johnAlexander | 14:8320b5ff96fa | 4201 | (sequence_step_id == VL53L0X_SEQUENCESTEP_MSRC)) { |
johnAlexander | 14:8320b5ff96fa | 4202 | |
johnAlexander | 14:8320b5ff96fa | 4203 | status = vl53l0x_get_vcsel_pulse_period(dev, |
johnAlexander | 14:8320b5ff96fa | 4204 | VL53L0X_VCSEL_PERIOD_PRE_RANGE, |
johnAlexander | 14:8320b5ff96fa | 4205 | ¤t_vcsel_pulse_period_p_clk); |
johnAlexander | 14:8320b5ff96fa | 4206 | |
johnAlexander | 14:8320b5ff96fa | 4207 | if (status == VL53L0X_ERROR_NONE) { |
johnAlexander | 14:8320b5ff96fa | 4208 | msrc_range_time_out_m_clks = vl53l0x_calc_timeout_mclks(dev, |
johnAlexander | 14:8320b5ff96fa | 4209 | timeout_micro_secs, |
johnAlexander | 14:8320b5ff96fa | 4210 | (uint8_t)current_vcsel_pulse_period_p_clk); |
johnAlexander | 14:8320b5ff96fa | 4211 | |
johnAlexander | 14:8320b5ff96fa | 4212 | if (msrc_range_time_out_m_clks > 256) |
johnAlexander | 14:8320b5ff96fa | 4213 | msrc_encoded_time_out = 255; |
johnAlexander | 14:8320b5ff96fa | 4214 | else |
johnAlexander | 14:8320b5ff96fa | 4215 | msrc_encoded_time_out = |
johnAlexander | 14:8320b5ff96fa | 4216 | (uint8_t)msrc_range_time_out_m_clks - 1; |
johnAlexander | 14:8320b5ff96fa | 4217 | |
johnAlexander | 14:8320b5ff96fa | 4218 | VL53L0X_SETDEVICESPECIFICPARAMETER(dev, |
johnAlexander | 14:8320b5ff96fa | 4219 | LastEncodedTimeout, |
johnAlexander | 14:8320b5ff96fa | 4220 | msrc_encoded_time_out); |
johnAlexander | 14:8320b5ff96fa | 4221 | } |
johnAlexander | 14:8320b5ff96fa | 4222 | |
johnAlexander | 14:8320b5ff96fa | 4223 | if (status == VL53L0X_ERROR_NONE) { |
johnAlexander | 14:8320b5ff96fa | 4224 | status = vl53l0x_write_byte(dev, |
johnAlexander | 14:8320b5ff96fa | 4225 | VL53L0X_REG_MSRC_CONFIG_TIMEOUT_MACROP, |
johnAlexander | 14:8320b5ff96fa | 4226 | msrc_encoded_time_out); |
johnAlexander | 14:8320b5ff96fa | 4227 | } |
johnAlexander | 14:8320b5ff96fa | 4228 | } else { |
johnAlexander | 14:8320b5ff96fa | 4229 | |
johnAlexander | 14:8320b5ff96fa | 4230 | if (sequence_step_id == VL53L0X_SEQUENCESTEP_PRE_RANGE) { |
johnAlexander | 14:8320b5ff96fa | 4231 | |
johnAlexander | 14:8320b5ff96fa | 4232 | if (status == VL53L0X_ERROR_NONE) { |
johnAlexander | 14:8320b5ff96fa | 4233 | status = vl53l0x_get_vcsel_pulse_period(dev, |
johnAlexander | 14:8320b5ff96fa | 4234 | VL53L0X_VCSEL_PERIOD_PRE_RANGE, |
johnAlexander | 14:8320b5ff96fa | 4235 | ¤t_vcsel_pulse_period_p_clk); |
johnAlexander | 14:8320b5ff96fa | 4236 | pre_range_time_out_m_clks = |
johnAlexander | 14:8320b5ff96fa | 4237 | vl53l0x_calc_timeout_mclks(dev, |
johnAlexander | 14:8320b5ff96fa | 4238 | timeout_micro_secs, |
johnAlexander | 14:8320b5ff96fa | 4239 | (uint8_t)current_vcsel_pulse_period_p_clk); |
johnAlexander | 14:8320b5ff96fa | 4240 | pre_range_encoded_time_out = vl53l0x_encode_timeout( |
johnAlexander | 14:8320b5ff96fa | 4241 | pre_range_time_out_m_clks); |
johnAlexander | 14:8320b5ff96fa | 4242 | |
johnAlexander | 14:8320b5ff96fa | 4243 | VL53L0X_SETDEVICESPECIFICPARAMETER(dev, |
johnAlexander | 14:8320b5ff96fa | 4244 | LastEncodedTimeout, |
johnAlexander | 14:8320b5ff96fa | 4245 | pre_range_encoded_time_out); |
johnAlexander | 14:8320b5ff96fa | 4246 | } |
johnAlexander | 14:8320b5ff96fa | 4247 | |
johnAlexander | 14:8320b5ff96fa | 4248 | if (status == VL53L0X_ERROR_NONE) { |
johnAlexander | 14:8320b5ff96fa | 4249 | status = vl53l0x_write_word(dev, |
johnAlexander | 14:8320b5ff96fa | 4250 | VL53L0X_REG_PRE_RANGE_CONFIG_TIMEOUT_MACROP_HI, |
johnAlexander | 14:8320b5ff96fa | 4251 | pre_range_encoded_time_out); |
johnAlexander | 14:8320b5ff96fa | 4252 | } |
johnAlexander | 14:8320b5ff96fa | 4253 | |
johnAlexander | 14:8320b5ff96fa | 4254 | if (status == VL53L0X_ERROR_NONE) { |
johnAlexander | 14:8320b5ff96fa | 4255 | VL53L0X_SETDEVICESPECIFICPARAMETER( |
johnAlexander | 14:8320b5ff96fa | 4256 | dev, |
johnAlexander | 14:8320b5ff96fa | 4257 | PreRangeTimeoutMicroSecs, |
johnAlexander | 14:8320b5ff96fa | 4258 | timeout_micro_secs); |
johnAlexander | 14:8320b5ff96fa | 4259 | } |
johnAlexander | 14:8320b5ff96fa | 4260 | } else if (sequence_step_id == VL53L0X_SEQUENCESTEP_FINAL_RANGE) { |
johnAlexander | 14:8320b5ff96fa | 4261 | |
johnAlexander | 14:8320b5ff96fa | 4262 | /* For the final range timeout, the pre-range timeout |
johnAlexander | 14:8320b5ff96fa | 4263 | * must be added. To do this both final and pre-range |
johnAlexander | 14:8320b5ff96fa | 4264 | * timeouts must be expressed in macro periods MClks |
johnAlexander | 14:8320b5ff96fa | 4265 | * because they have different vcsel periods. |
johnAlexander | 14:8320b5ff96fa | 4266 | */ |
johnAlexander | 14:8320b5ff96fa | 4267 | |
johnAlexander | 14:8320b5ff96fa | 4268 | vl53l0x_get_sequence_step_enables(dev, |
johnAlexander | 14:8320b5ff96fa | 4269 | &scheduler_sequence_steps); |
johnAlexander | 14:8320b5ff96fa | 4270 | pre_range_time_out_m_clks = 0; |
johnAlexander | 14:8320b5ff96fa | 4271 | if (scheduler_sequence_steps.PreRangeOn) { |
johnAlexander | 14:8320b5ff96fa | 4272 | |
johnAlexander | 14:8320b5ff96fa | 4273 | /* Retrieve PRE-RANGE VCSEL Period */ |
johnAlexander | 14:8320b5ff96fa | 4274 | status = vl53l0x_get_vcsel_pulse_period(dev, |
johnAlexander | 14:8320b5ff96fa | 4275 | VL53L0X_VCSEL_PERIOD_PRE_RANGE, |
johnAlexander | 14:8320b5ff96fa | 4276 | ¤t_vcsel_pulse_period_p_clk); |
johnAlexander | 14:8320b5ff96fa | 4277 | |
johnAlexander | 14:8320b5ff96fa | 4278 | /* Retrieve PRE-RANGE Timeout in Macro periods |
johnAlexander | 14:8320b5ff96fa | 4279 | * (MCLKS) */ |
johnAlexander | 14:8320b5ff96fa | 4280 | if (status == VL53L0X_ERROR_NONE) { |
johnAlexander | 14:8320b5ff96fa | 4281 | status = vl53l0x_read_word(dev, 0x51, |
johnAlexander | 14:8320b5ff96fa | 4282 | &pre_range_encoded_time_out); |
johnAlexander | 14:8320b5ff96fa | 4283 | pre_range_time_out_m_clks = |
johnAlexander | 14:8320b5ff96fa | 4284 | vl53l0x_decode_timeout( |
johnAlexander | 14:8320b5ff96fa | 4285 | pre_range_encoded_time_out); |
johnAlexander | 14:8320b5ff96fa | 4286 | } |
johnAlexander | 14:8320b5ff96fa | 4287 | } |
johnAlexander | 14:8320b5ff96fa | 4288 | |
johnAlexander | 14:8320b5ff96fa | 4289 | /* Calculate FINAL RANGE Timeout in Macro Periods |
johnAlexander | 14:8320b5ff96fa | 4290 | * (MCLKS) and add PRE-RANGE value |
johnAlexander | 14:8320b5ff96fa | 4291 | */ |
johnAlexander | 14:8320b5ff96fa | 4292 | if (status == VL53L0X_ERROR_NONE) { |
johnAlexander | 14:8320b5ff96fa | 4293 | |
johnAlexander | 14:8320b5ff96fa | 4294 | status = vl53l0x_get_vcsel_pulse_period(dev, |
johnAlexander | 14:8320b5ff96fa | 4295 | VL53L0X_VCSEL_PERIOD_FINAL_RANGE, |
johnAlexander | 14:8320b5ff96fa | 4296 | ¤t_vcsel_pulse_period_p_clk); |
johnAlexander | 14:8320b5ff96fa | 4297 | } |
johnAlexander | 14:8320b5ff96fa | 4298 | if (status == VL53L0X_ERROR_NONE) { |
johnAlexander | 14:8320b5ff96fa | 4299 | |
johnAlexander | 14:8320b5ff96fa | 4300 | final_range_time_out_m_clks = |
johnAlexander | 14:8320b5ff96fa | 4301 | vl53l0x_calc_timeout_mclks(dev, |
johnAlexander | 14:8320b5ff96fa | 4302 | timeout_micro_secs, |
johnAlexander | 14:8320b5ff96fa | 4303 | (uint8_t) current_vcsel_pulse_period_p_clk); |
johnAlexander | 14:8320b5ff96fa | 4304 | |
johnAlexander | 14:8320b5ff96fa | 4305 | final_range_time_out_m_clks += pre_range_time_out_m_clks; |
johnAlexander | 14:8320b5ff96fa | 4306 | |
johnAlexander | 14:8320b5ff96fa | 4307 | final_range_encoded_time_out = |
johnAlexander | 14:8320b5ff96fa | 4308 | vl53l0x_encode_timeout(final_range_time_out_m_clks); |
johnAlexander | 14:8320b5ff96fa | 4309 | |
johnAlexander | 14:8320b5ff96fa | 4310 | if (status == VL53L0X_ERROR_NONE) { |
johnAlexander | 14:8320b5ff96fa | 4311 | status = vl53l0x_write_word(dev, 0x71, |
johnAlexander | 14:8320b5ff96fa | 4312 | final_range_encoded_time_out); |
johnAlexander | 14:8320b5ff96fa | 4313 | } |
johnAlexander | 14:8320b5ff96fa | 4314 | |
johnAlexander | 14:8320b5ff96fa | 4315 | if (status == VL53L0X_ERROR_NONE) { |
johnAlexander | 14:8320b5ff96fa | 4316 | VL53L0X_SETDEVICESPECIFICPARAMETER( |
johnAlexander | 14:8320b5ff96fa | 4317 | dev, |
johnAlexander | 14:8320b5ff96fa | 4318 | FinalRangeTimeoutMicroSecs, |
johnAlexander | 14:8320b5ff96fa | 4319 | timeout_micro_secs); |
johnAlexander | 14:8320b5ff96fa | 4320 | } |
johnAlexander | 14:8320b5ff96fa | 4321 | } |
johnAlexander | 14:8320b5ff96fa | 4322 | } else |
johnAlexander | 14:8320b5ff96fa | 4323 | status = VL53L0X_ERROR_INVALID_PARAMS; |
johnAlexander | 14:8320b5ff96fa | 4324 | |
johnAlexander | 14:8320b5ff96fa | 4325 | } |
johnAlexander | 14:8320b5ff96fa | 4326 | return status; |
johnAlexander | 0:c523920bcc09 | 4327 | } |
johnAlexander | 0:c523920bcc09 | 4328 | |
johnAlexander | 14:8320b5ff96fa | 4329 | VL53L0X_Error VL53L0X::wrapped_vl53l0x_set_measurement_timing_budget_micro_seconds(VL53L0X_DEV dev, |
johnAlexander | 14:8320b5ff96fa | 4330 | uint32_t measurement_timing_budget_micro_seconds) |
johnAlexander | 0:c523920bcc09 | 4331 | { |
johnAlexander | 14:8320b5ff96fa | 4332 | VL53L0X_Error status = VL53L0X_ERROR_NONE; |
johnAlexander | 14:8320b5ff96fa | 4333 | uint32_t final_range_timing_budget_micro_seconds; |
johnAlexander | 14:8320b5ff96fa | 4334 | VL53L0X_SchedulerSequenceSteps_t scheduler_sequence_steps; |
johnAlexander | 14:8320b5ff96fa | 4335 | uint32_t msrc_dcc_tcc_timeout_micro_seconds = 2000; |
johnAlexander | 14:8320b5ff96fa | 4336 | uint32_t start_overhead_micro_seconds = 1910; |
johnAlexander | 14:8320b5ff96fa | 4337 | uint32_t end_overhead_micro_seconds = 960; |
johnAlexander | 14:8320b5ff96fa | 4338 | uint32_t msrc_overhead_micro_seconds = 660; |
johnAlexander | 14:8320b5ff96fa | 4339 | uint32_t tcc_overhead_micro_seconds = 590; |
johnAlexander | 14:8320b5ff96fa | 4340 | uint32_t dss_overhead_micro_seconds = 690; |
johnAlexander | 14:8320b5ff96fa | 4341 | uint32_t pre_range_overhead_micro_seconds = 660; |
johnAlexander | 14:8320b5ff96fa | 4342 | uint32_t final_range_overhead_micro_seconds = 550; |
johnAlexander | 14:8320b5ff96fa | 4343 | uint32_t pre_range_timeout_micro_seconds = 0; |
johnAlexander | 14:8320b5ff96fa | 4344 | uint32_t c_min_timing_budget_micro_seconds = 20000; |
johnAlexander | 14:8320b5ff96fa | 4345 | uint32_t sub_timeout = 0; |
johnAlexander | 14:8320b5ff96fa | 4346 | |
johnAlexander | 14:8320b5ff96fa | 4347 | LOG_FUNCTION_START(""); |
johnAlexander | 14:8320b5ff96fa | 4348 | |
johnAlexander | 14:8320b5ff96fa | 4349 | if (measurement_timing_budget_micro_seconds |
johnAlexander | 14:8320b5ff96fa | 4350 | < c_min_timing_budget_micro_seconds) { |
johnAlexander | 14:8320b5ff96fa | 4351 | status = VL53L0X_ERROR_INVALID_PARAMS; |
johnAlexander | 14:8320b5ff96fa | 4352 | return status; |
johnAlexander | 14:8320b5ff96fa | 4353 | } |
johnAlexander | 14:8320b5ff96fa | 4354 | |
johnAlexander | 14:8320b5ff96fa | 4355 | final_range_timing_budget_micro_seconds = |
johnAlexander | 14:8320b5ff96fa | 4356 | measurement_timing_budget_micro_seconds - |
johnAlexander | 14:8320b5ff96fa | 4357 | (start_overhead_micro_seconds + end_overhead_micro_seconds); |
johnAlexander | 14:8320b5ff96fa | 4358 | |
johnAlexander | 14:8320b5ff96fa | 4359 | status = vl53l0x_get_sequence_step_enables(dev, &scheduler_sequence_steps); |
johnAlexander | 14:8320b5ff96fa | 4360 | |
johnAlexander | 14:8320b5ff96fa | 4361 | if (status == VL53L0X_ERROR_NONE && |
johnAlexander | 14:8320b5ff96fa | 4362 | (scheduler_sequence_steps.TccOn || |
johnAlexander | 14:8320b5ff96fa | 4363 | scheduler_sequence_steps.MsrcOn || |
johnAlexander | 14:8320b5ff96fa | 4364 | scheduler_sequence_steps.DssOn)) { |
johnAlexander | 14:8320b5ff96fa | 4365 | |
johnAlexander | 14:8320b5ff96fa | 4366 | /* TCC, MSRC and DSS all share the same timeout */ |
johnAlexander | 14:8320b5ff96fa | 4367 | status = get_sequence_step_timeout(dev, |
johnAlexander | 14:8320b5ff96fa | 4368 | VL53L0X_SEQUENCESTEP_MSRC, |
johnAlexander | 14:8320b5ff96fa | 4369 | &msrc_dcc_tcc_timeout_micro_seconds); |
johnAlexander | 14:8320b5ff96fa | 4370 | |
johnAlexander | 14:8320b5ff96fa | 4371 | /* Subtract the TCC, MSRC and DSS timeouts if they are |
johnAlexander | 14:8320b5ff96fa | 4372 | * enabled. */ |
johnAlexander | 14:8320b5ff96fa | 4373 | |
johnAlexander | 14:8320b5ff96fa | 4374 | if (status != VL53L0X_ERROR_NONE) |
johnAlexander | 14:8320b5ff96fa | 4375 | return status; |
johnAlexander | 14:8320b5ff96fa | 4376 | |
johnAlexander | 14:8320b5ff96fa | 4377 | /* TCC */ |
johnAlexander | 14:8320b5ff96fa | 4378 | if (scheduler_sequence_steps.TccOn) { |
johnAlexander | 14:8320b5ff96fa | 4379 | |
johnAlexander | 14:8320b5ff96fa | 4380 | sub_timeout = msrc_dcc_tcc_timeout_micro_seconds |
johnAlexander | 14:8320b5ff96fa | 4381 | + tcc_overhead_micro_seconds; |
johnAlexander | 14:8320b5ff96fa | 4382 | |
johnAlexander | 14:8320b5ff96fa | 4383 | if (sub_timeout < |
johnAlexander | 14:8320b5ff96fa | 4384 | final_range_timing_budget_micro_seconds) { |
johnAlexander | 14:8320b5ff96fa | 4385 | final_range_timing_budget_micro_seconds -= |
johnAlexander | 14:8320b5ff96fa | 4386 | sub_timeout; |
johnAlexander | 14:8320b5ff96fa | 4387 | } else { |
johnAlexander | 14:8320b5ff96fa | 4388 | /* Requested timeout too big. */ |
johnAlexander | 14:8320b5ff96fa | 4389 | status = VL53L0X_ERROR_INVALID_PARAMS; |
johnAlexander | 14:8320b5ff96fa | 4390 | } |
johnAlexander | 14:8320b5ff96fa | 4391 | } |
johnAlexander | 14:8320b5ff96fa | 4392 | |
johnAlexander | 14:8320b5ff96fa | 4393 | if (status != VL53L0X_ERROR_NONE) { |
johnAlexander | 14:8320b5ff96fa | 4394 | LOG_FUNCTION_END(status); |
johnAlexander | 14:8320b5ff96fa | 4395 | return status; |
johnAlexander | 14:8320b5ff96fa | 4396 | } |
johnAlexander | 14:8320b5ff96fa | 4397 | |
johnAlexander | 14:8320b5ff96fa | 4398 | /* DSS */ |
johnAlexander | 14:8320b5ff96fa | 4399 | if (scheduler_sequence_steps.DssOn) { |
johnAlexander | 14:8320b5ff96fa | 4400 | |
johnAlexander | 14:8320b5ff96fa | 4401 | sub_timeout = 2 * (msrc_dcc_tcc_timeout_micro_seconds + |
johnAlexander | 14:8320b5ff96fa | 4402 | dss_overhead_micro_seconds); |
johnAlexander | 14:8320b5ff96fa | 4403 | |
johnAlexander | 14:8320b5ff96fa | 4404 | if (sub_timeout < final_range_timing_budget_micro_seconds) { |
johnAlexander | 14:8320b5ff96fa | 4405 | final_range_timing_budget_micro_seconds |
johnAlexander | 14:8320b5ff96fa | 4406 | -= sub_timeout; |
johnAlexander | 14:8320b5ff96fa | 4407 | } else { |
johnAlexander | 14:8320b5ff96fa | 4408 | /* Requested timeout too big. */ |
johnAlexander | 14:8320b5ff96fa | 4409 | status = VL53L0X_ERROR_INVALID_PARAMS; |
johnAlexander | 14:8320b5ff96fa | 4410 | } |
johnAlexander | 14:8320b5ff96fa | 4411 | } else if (scheduler_sequence_steps.MsrcOn) { |
johnAlexander | 14:8320b5ff96fa | 4412 | /* MSRC */ |
johnAlexander | 14:8320b5ff96fa | 4413 | sub_timeout = msrc_dcc_tcc_timeout_micro_seconds + |
johnAlexander | 14:8320b5ff96fa | 4414 | msrc_overhead_micro_seconds; |
johnAlexander | 14:8320b5ff96fa | 4415 | |
johnAlexander | 14:8320b5ff96fa | 4416 | if (sub_timeout < final_range_timing_budget_micro_seconds) { |
johnAlexander | 14:8320b5ff96fa | 4417 | final_range_timing_budget_micro_seconds |
johnAlexander | 14:8320b5ff96fa | 4418 | -= sub_timeout; |
johnAlexander | 14:8320b5ff96fa | 4419 | } else { |
johnAlexander | 14:8320b5ff96fa | 4420 | /* Requested timeout too big. */ |
johnAlexander | 14:8320b5ff96fa | 4421 | status = VL53L0X_ERROR_INVALID_PARAMS; |
johnAlexander | 14:8320b5ff96fa | 4422 | } |
johnAlexander | 14:8320b5ff96fa | 4423 | } |
johnAlexander | 14:8320b5ff96fa | 4424 | |
johnAlexander | 14:8320b5ff96fa | 4425 | } |
johnAlexander | 14:8320b5ff96fa | 4426 | |
johnAlexander | 14:8320b5ff96fa | 4427 | if (status != VL53L0X_ERROR_NONE) { |
johnAlexander | 14:8320b5ff96fa | 4428 | LOG_FUNCTION_END(status); |
johnAlexander | 14:8320b5ff96fa | 4429 | return status; |
johnAlexander | 14:8320b5ff96fa | 4430 | } |
johnAlexander | 14:8320b5ff96fa | 4431 | |
johnAlexander | 14:8320b5ff96fa | 4432 | if (scheduler_sequence_steps.PreRangeOn) { |
johnAlexander | 14:8320b5ff96fa | 4433 | |
johnAlexander | 14:8320b5ff96fa | 4434 | /* Subtract the Pre-range timeout if enabled. */ |
johnAlexander | 14:8320b5ff96fa | 4435 | |
johnAlexander | 14:8320b5ff96fa | 4436 | status = get_sequence_step_timeout(dev, |
johnAlexander | 14:8320b5ff96fa | 4437 | VL53L0X_SEQUENCESTEP_PRE_RANGE, |
johnAlexander | 14:8320b5ff96fa | 4438 | &pre_range_timeout_micro_seconds); |
johnAlexander | 14:8320b5ff96fa | 4439 | |
johnAlexander | 14:8320b5ff96fa | 4440 | sub_timeout = pre_range_timeout_micro_seconds + |
johnAlexander | 14:8320b5ff96fa | 4441 | pre_range_overhead_micro_seconds; |
johnAlexander | 14:8320b5ff96fa | 4442 | |
johnAlexander | 14:8320b5ff96fa | 4443 | if (sub_timeout < final_range_timing_budget_micro_seconds) { |
johnAlexander | 14:8320b5ff96fa | 4444 | final_range_timing_budget_micro_seconds -= sub_timeout; |
johnAlexander | 14:8320b5ff96fa | 4445 | } else { |
johnAlexander | 14:8320b5ff96fa | 4446 | /* Requested timeout too big. */ |
johnAlexander | 14:8320b5ff96fa | 4447 | status = VL53L0X_ERROR_INVALID_PARAMS; |
johnAlexander | 14:8320b5ff96fa | 4448 | } |
johnAlexander | 14:8320b5ff96fa | 4449 | } |
johnAlexander | 14:8320b5ff96fa | 4450 | |
johnAlexander | 14:8320b5ff96fa | 4451 | |
johnAlexander | 14:8320b5ff96fa | 4452 | if (status == VL53L0X_ERROR_NONE && |
johnAlexander | 14:8320b5ff96fa | 4453 | scheduler_sequence_steps.FinalRangeOn) { |
johnAlexander | 14:8320b5ff96fa | 4454 | |
johnAlexander | 14:8320b5ff96fa | 4455 | final_range_timing_budget_micro_seconds -= |
johnAlexander | 14:8320b5ff96fa | 4456 | final_range_overhead_micro_seconds; |
johnAlexander | 14:8320b5ff96fa | 4457 | |
johnAlexander | 14:8320b5ff96fa | 4458 | /* Final Range Timeout |
johnAlexander | 14:8320b5ff96fa | 4459 | * Note that the final range timeout is determined by the timing |
johnAlexander | 14:8320b5ff96fa | 4460 | * budget and the sum of all other timeouts within the sequence. |
johnAlexander | 14:8320b5ff96fa | 4461 | * If there is no room for the final range timeout, then an error |
johnAlexander | 14:8320b5ff96fa | 4462 | * will be set. Otherwise the remaining time will be applied to |
johnAlexander | 14:8320b5ff96fa | 4463 | * the final range. |
johnAlexander | 14:8320b5ff96fa | 4464 | */ |
johnAlexander | 14:8320b5ff96fa | 4465 | status = set_sequence_step_timeout(dev, |
johnAlexander | 14:8320b5ff96fa | 4466 | VL53L0X_SEQUENCESTEP_FINAL_RANGE, |
johnAlexander | 14:8320b5ff96fa | 4467 | final_range_timing_budget_micro_seconds); |
johnAlexander | 14:8320b5ff96fa | 4468 | |
johnAlexander | 14:8320b5ff96fa | 4469 | VL53L0X_SETPARAMETERFIELD(dev, |
johnAlexander | 14:8320b5ff96fa | 4470 | MeasurementTimingBudgetMicroSeconds, |
johnAlexander | 14:8320b5ff96fa | 4471 | measurement_timing_budget_micro_seconds); |
johnAlexander | 14:8320b5ff96fa | 4472 | } |
johnAlexander | 14:8320b5ff96fa | 4473 | |
johnAlexander | 14:8320b5ff96fa | 4474 | LOG_FUNCTION_END(status); |
johnAlexander | 14:8320b5ff96fa | 4475 | |
johnAlexander | 14:8320b5ff96fa | 4476 | return status; |
johnAlexander | 0:c523920bcc09 | 4477 | } |
johnAlexander | 0:c523920bcc09 | 4478 | |
johnAlexander | 14:8320b5ff96fa | 4479 | VL53L0X_Error VL53L0X::vl53l0x_set_measurement_timing_budget_micro_seconds(VL53L0X_DEV dev, |
johnAlexander | 14:8320b5ff96fa | 4480 | uint32_t measurement_timing_budget_micro_seconds) |
johnAlexander | 0:c523920bcc09 | 4481 | { |
johnAlexander | 14:8320b5ff96fa | 4482 | VL53L0X_Error status = VL53L0X_ERROR_NONE; |
johnAlexander | 14:8320b5ff96fa | 4483 | LOG_FUNCTION_START(""); |
johnAlexander | 14:8320b5ff96fa | 4484 | |
johnAlexander | 14:8320b5ff96fa | 4485 | status = wrapped_vl53l0x_set_measurement_timing_budget_micro_seconds(dev, |
johnAlexander | 14:8320b5ff96fa | 4486 | measurement_timing_budget_micro_seconds); |
johnAlexander | 14:8320b5ff96fa | 4487 | |
johnAlexander | 14:8320b5ff96fa | 4488 | LOG_FUNCTION_END(status); |
johnAlexander | 14:8320b5ff96fa | 4489 | |
johnAlexander | 14:8320b5ff96fa | 4490 | return status; |
johnAlexander | 0:c523920bcc09 | 4491 | } |
johnAlexander | 0:c523920bcc09 | 4492 | |
johnAlexander | 14:8320b5ff96fa | 4493 | VL53L0X_Error VL53L0X::vl53l0x_set_sequence_step_enable(VL53L0X_DEV dev, |
johnAlexander | 14:8320b5ff96fa | 4494 | VL53L0X_SequenceStepId sequence_step_id, uint8_t sequence_step_enabled) |
johnAlexander | 0:c523920bcc09 | 4495 | { |
johnAlexander | 14:8320b5ff96fa | 4496 | VL53L0X_Error status = VL53L0X_ERROR_NONE; |
johnAlexander | 14:8320b5ff96fa | 4497 | uint8_t sequence_config = 0; |
johnAlexander | 14:8320b5ff96fa | 4498 | uint8_t sequence_config_new = 0; |
johnAlexander | 14:8320b5ff96fa | 4499 | uint32_t measurement_timing_budget_micro_seconds; |
johnAlexander | 14:8320b5ff96fa | 4500 | LOG_FUNCTION_START(""); |
johnAlexander | 14:8320b5ff96fa | 4501 | |
johnAlexander | 14:8320b5ff96fa | 4502 | status = vl53l0x_read_byte(dev, VL53L0X_REG_SYSTEM_SEQUENCE_CONFIG, |
johnAlexander | 14:8320b5ff96fa | 4503 | &sequence_config); |
johnAlexander | 14:8320b5ff96fa | 4504 | |
johnAlexander | 14:8320b5ff96fa | 4505 | sequence_config_new = sequence_config; |
johnAlexander | 14:8320b5ff96fa | 4506 | |
johnAlexander | 14:8320b5ff96fa | 4507 | if (status == VL53L0X_ERROR_NONE) { |
johnAlexander | 14:8320b5ff96fa | 4508 | if (sequence_step_enabled == 1) { |
johnAlexander | 14:8320b5ff96fa | 4509 | |
johnAlexander | 14:8320b5ff96fa | 4510 | /* Enable requested sequence step |
johnAlexander | 14:8320b5ff96fa | 4511 | */ |
johnAlexander | 14:8320b5ff96fa | 4512 | switch (sequence_step_id) { |
johnAlexander | 14:8320b5ff96fa | 4513 | case VL53L0X_SEQUENCESTEP_TCC: |
johnAlexander | 14:8320b5ff96fa | 4514 | sequence_config_new |= 0x10; |
johnAlexander | 14:8320b5ff96fa | 4515 | break; |
johnAlexander | 14:8320b5ff96fa | 4516 | case VL53L0X_SEQUENCESTEP_DSS: |
johnAlexander | 14:8320b5ff96fa | 4517 | sequence_config_new |= 0x28; |
johnAlexander | 14:8320b5ff96fa | 4518 | break; |
johnAlexander | 14:8320b5ff96fa | 4519 | case VL53L0X_SEQUENCESTEP_MSRC: |
johnAlexander | 14:8320b5ff96fa | 4520 | sequence_config_new |= 0x04; |
johnAlexander | 14:8320b5ff96fa | 4521 | break; |
johnAlexander | 14:8320b5ff96fa | 4522 | case VL53L0X_SEQUENCESTEP_PRE_RANGE: |
johnAlexander | 14:8320b5ff96fa | 4523 | sequence_config_new |= 0x40; |
johnAlexander | 14:8320b5ff96fa | 4524 | break; |
johnAlexander | 14:8320b5ff96fa | 4525 | case VL53L0X_SEQUENCESTEP_FINAL_RANGE: |
johnAlexander | 14:8320b5ff96fa | 4526 | sequence_config_new |= 0x80; |
johnAlexander | 14:8320b5ff96fa | 4527 | break; |
johnAlexander | 14:8320b5ff96fa | 4528 | default: |
johnAlexander | 14:8320b5ff96fa | 4529 | status = VL53L0X_ERROR_INVALID_PARAMS; |
johnAlexander | 14:8320b5ff96fa | 4530 | } |
johnAlexander | 14:8320b5ff96fa | 4531 | } else { |
johnAlexander | 14:8320b5ff96fa | 4532 | /* Disable requested sequence step |
johnAlexander | 14:8320b5ff96fa | 4533 | */ |
johnAlexander | 14:8320b5ff96fa | 4534 | switch (sequence_step_id) { |
johnAlexander | 14:8320b5ff96fa | 4535 | case VL53L0X_SEQUENCESTEP_TCC: |
johnAlexander | 14:8320b5ff96fa | 4536 | sequence_config_new &= 0xef; |
johnAlexander | 14:8320b5ff96fa | 4537 | break; |
johnAlexander | 14:8320b5ff96fa | 4538 | case VL53L0X_SEQUENCESTEP_DSS: |
johnAlexander | 14:8320b5ff96fa | 4539 | sequence_config_new &= 0xd7; |
johnAlexander | 14:8320b5ff96fa | 4540 | break; |
johnAlexander | 14:8320b5ff96fa | 4541 | case VL53L0X_SEQUENCESTEP_MSRC: |
johnAlexander | 14:8320b5ff96fa | 4542 | sequence_config_new &= 0xfb; |
johnAlexander | 14:8320b5ff96fa | 4543 | break; |
johnAlexander | 14:8320b5ff96fa | 4544 | case VL53L0X_SEQUENCESTEP_PRE_RANGE: |
johnAlexander | 14:8320b5ff96fa | 4545 | sequence_config_new &= 0xbf; |
johnAlexander | 14:8320b5ff96fa | 4546 | break; |
johnAlexander | 14:8320b5ff96fa | 4547 | case VL53L0X_SEQUENCESTEP_FINAL_RANGE: |
johnAlexander | 14:8320b5ff96fa | 4548 | sequence_config_new &= 0x7f; |
johnAlexander | 14:8320b5ff96fa | 4549 | break; |
johnAlexander | 14:8320b5ff96fa | 4550 | default: |
johnAlexander | 14:8320b5ff96fa | 4551 | status = VL53L0X_ERROR_INVALID_PARAMS; |
johnAlexander | 14:8320b5ff96fa | 4552 | } |
johnAlexander | 14:8320b5ff96fa | 4553 | } |
johnAlexander | 14:8320b5ff96fa | 4554 | } |
johnAlexander | 14:8320b5ff96fa | 4555 | |
johnAlexander | 14:8320b5ff96fa | 4556 | if (sequence_config_new != sequence_config) { |
johnAlexander | 14:8320b5ff96fa | 4557 | /* Apply New Setting */ |
johnAlexander | 14:8320b5ff96fa | 4558 | if (status == VL53L0X_ERROR_NONE) { |
johnAlexander | 14:8320b5ff96fa | 4559 | status = vl53l0x_write_byte(dev, |
johnAlexander | 14:8320b5ff96fa | 4560 | VL53L0X_REG_SYSTEM_SEQUENCE_CONFIG, sequence_config_new); |
johnAlexander | 14:8320b5ff96fa | 4561 | } |
johnAlexander | 14:8320b5ff96fa | 4562 | if (status == VL53L0X_ERROR_NONE) |
johnAlexander | 14:8320b5ff96fa | 4563 | PALDevDataSet(dev, SequenceConfig, sequence_config_new); |
johnAlexander | 14:8320b5ff96fa | 4564 | |
johnAlexander | 14:8320b5ff96fa | 4565 | |
johnAlexander | 14:8320b5ff96fa | 4566 | /* Recalculate timing budget */ |
johnAlexander | 14:8320b5ff96fa | 4567 | if (status == VL53L0X_ERROR_NONE) { |
johnAlexander | 14:8320b5ff96fa | 4568 | VL53L0X_GETPARAMETERFIELD(dev, |
johnAlexander | 14:8320b5ff96fa | 4569 | MeasurementTimingBudgetMicroSeconds, |
johnAlexander | 14:8320b5ff96fa | 4570 | measurement_timing_budget_micro_seconds); |
johnAlexander | 14:8320b5ff96fa | 4571 | |
johnAlexander | 14:8320b5ff96fa | 4572 | vl53l0x_set_measurement_timing_budget_micro_seconds(dev, |
johnAlexander | 14:8320b5ff96fa | 4573 | measurement_timing_budget_micro_seconds); |
johnAlexander | 14:8320b5ff96fa | 4574 | } |
johnAlexander | 14:8320b5ff96fa | 4575 | } |
johnAlexander | 14:8320b5ff96fa | 4576 | |
johnAlexander | 14:8320b5ff96fa | 4577 | LOG_FUNCTION_END(status); |
johnAlexander | 14:8320b5ff96fa | 4578 | |
johnAlexander | 14:8320b5ff96fa | 4579 | return status; |
johnAlexander | 0:c523920bcc09 | 4580 | } |
johnAlexander | 0:c523920bcc09 | 4581 | |
johnAlexander | 14:8320b5ff96fa | 4582 | VL53L0X_Error VL53L0X::vl53l0x_set_limit_check_enable(VL53L0X_DEV dev, uint16_t limit_check_id, |
johnAlexander | 14:8320b5ff96fa | 4583 | uint8_t limit_check_enable) |
johnAlexander | 0:c523920bcc09 | 4584 | { |
johnAlexander | 14:8320b5ff96fa | 4585 | VL53L0X_Error status = VL53L0X_ERROR_NONE; |
johnAlexander | 14:8320b5ff96fa | 4586 | FixPoint1616_t temp_fix1616 = 0; |
johnAlexander | 14:8320b5ff96fa | 4587 | uint8_t limit_check_enable_int = 0; |
johnAlexander | 14:8320b5ff96fa | 4588 | uint8_t limit_check_disable = 0; |
johnAlexander | 14:8320b5ff96fa | 4589 | uint8_t temp8; |
johnAlexander | 14:8320b5ff96fa | 4590 | |
johnAlexander | 14:8320b5ff96fa | 4591 | LOG_FUNCTION_START(""); |
johnAlexander | 14:8320b5ff96fa | 4592 | |
johnAlexander | 14:8320b5ff96fa | 4593 | if (limit_check_id >= VL53L0X_CHECKENABLE_NUMBER_OF_CHECKS) { |
johnAlexander | 14:8320b5ff96fa | 4594 | status = VL53L0X_ERROR_INVALID_PARAMS; |
johnAlexander | 14:8320b5ff96fa | 4595 | } else { |
johnAlexander | 14:8320b5ff96fa | 4596 | if (limit_check_enable == 0) { |
johnAlexander | 14:8320b5ff96fa | 4597 | temp_fix1616 = 0; |
johnAlexander | 14:8320b5ff96fa | 4598 | limit_check_enable_int = 0; |
johnAlexander | 14:8320b5ff96fa | 4599 | limit_check_disable = 1; |
johnAlexander | 14:8320b5ff96fa | 4600 | |
johnAlexander | 14:8320b5ff96fa | 4601 | } else { |
johnAlexander | 14:8320b5ff96fa | 4602 | VL53L0X_GETARRAYPARAMETERFIELD(dev, LimitChecksValue, |
johnAlexander | 14:8320b5ff96fa | 4603 | limit_check_id, temp_fix1616); |
johnAlexander | 14:8320b5ff96fa | 4604 | limit_check_disable = 0; |
johnAlexander | 14:8320b5ff96fa | 4605 | /* this to be sure to have either 0 or 1 */ |
johnAlexander | 14:8320b5ff96fa | 4606 | limit_check_enable_int = 1; |
johnAlexander | 14:8320b5ff96fa | 4607 | } |
johnAlexander | 14:8320b5ff96fa | 4608 | |
johnAlexander | 14:8320b5ff96fa | 4609 | switch (limit_check_id) { |
johnAlexander | 14:8320b5ff96fa | 4610 | |
johnAlexander | 14:8320b5ff96fa | 4611 | case VL53L0X_CHECKENABLE_SIGMA_FINAL_RANGE: |
johnAlexander | 14:8320b5ff96fa | 4612 | /* internal computation: */ |
johnAlexander | 14:8320b5ff96fa | 4613 | VL53L0X_SETARRAYPARAMETERFIELD(dev, LimitChecksEnable, |
johnAlexander | 14:8320b5ff96fa | 4614 | VL53L0X_CHECKENABLE_SIGMA_FINAL_RANGE, |
johnAlexander | 14:8320b5ff96fa | 4615 | limit_check_enable_int); |
johnAlexander | 14:8320b5ff96fa | 4616 | |
johnAlexander | 14:8320b5ff96fa | 4617 | break; |
johnAlexander | 14:8320b5ff96fa | 4618 | |
johnAlexander | 14:8320b5ff96fa | 4619 | case VL53L0X_CHECKENABLE_SIGNAL_RATE_FINAL_RANGE: |
johnAlexander | 14:8320b5ff96fa | 4620 | |
johnAlexander | 14:8320b5ff96fa | 4621 | status = vl53l0x_write_word(dev, |
johnAlexander | 14:8320b5ff96fa | 4622 | VL53L0X_REG_FINAL_RANGE_CONFIG_MIN_COUNT_RATE_RTN_LIMIT, |
johnAlexander | 14:8320b5ff96fa | 4623 | VL53L0X_FIXPOINT1616TOFIXPOINT97(temp_fix1616)); |
johnAlexander | 14:8320b5ff96fa | 4624 | |
johnAlexander | 14:8320b5ff96fa | 4625 | break; |
johnAlexander | 14:8320b5ff96fa | 4626 | |
johnAlexander | 14:8320b5ff96fa | 4627 | case VL53L0X_CHECKENABLE_SIGNAL_REF_CLIP: |
johnAlexander | 14:8320b5ff96fa | 4628 | |
johnAlexander | 14:8320b5ff96fa | 4629 | /* internal computation: */ |
johnAlexander | 14:8320b5ff96fa | 4630 | VL53L0X_SETARRAYPARAMETERFIELD(dev, LimitChecksEnable, |
johnAlexander | 14:8320b5ff96fa | 4631 | VL53L0X_CHECKENABLE_SIGNAL_REF_CLIP, |
johnAlexander | 14:8320b5ff96fa | 4632 | limit_check_enable_int); |
johnAlexander | 14:8320b5ff96fa | 4633 | |
johnAlexander | 14:8320b5ff96fa | 4634 | break; |
johnAlexander | 14:8320b5ff96fa | 4635 | |
johnAlexander | 14:8320b5ff96fa | 4636 | case VL53L0X_CHECKENABLE_RANGE_IGNORE_THRESHOLD: |
johnAlexander | 14:8320b5ff96fa | 4637 | |
johnAlexander | 14:8320b5ff96fa | 4638 | /* internal computation: */ |
johnAlexander | 14:8320b5ff96fa | 4639 | VL53L0X_SETARRAYPARAMETERFIELD(dev, LimitChecksEnable, |
johnAlexander | 14:8320b5ff96fa | 4640 | VL53L0X_CHECKENABLE_RANGE_IGNORE_THRESHOLD, |
johnAlexander | 14:8320b5ff96fa | 4641 | limit_check_enable_int); |
johnAlexander | 14:8320b5ff96fa | 4642 | |
johnAlexander | 14:8320b5ff96fa | 4643 | break; |
johnAlexander | 14:8320b5ff96fa | 4644 | |
johnAlexander | 14:8320b5ff96fa | 4645 | case VL53L0X_CHECKENABLE_SIGNAL_RATE_MSRC: |
johnAlexander | 14:8320b5ff96fa | 4646 | |
johnAlexander | 14:8320b5ff96fa | 4647 | temp8 = (uint8_t)(limit_check_disable << 1); |
johnAlexander | 14:8320b5ff96fa | 4648 | status = vl53l0x_update_byte(dev, |
johnAlexander | 14:8320b5ff96fa | 4649 | VL53L0X_REG_MSRC_CONFIG_CONTROL, |
johnAlexander | 14:8320b5ff96fa | 4650 | 0xFE, temp8); |
johnAlexander | 14:8320b5ff96fa | 4651 | |
johnAlexander | 14:8320b5ff96fa | 4652 | break; |
johnAlexander | 14:8320b5ff96fa | 4653 | |
johnAlexander | 14:8320b5ff96fa | 4654 | case VL53L0X_CHECKENABLE_SIGNAL_RATE_PRE_RANGE: |
johnAlexander | 14:8320b5ff96fa | 4655 | |
johnAlexander | 14:8320b5ff96fa | 4656 | temp8 = (uint8_t)(limit_check_disable << 4); |
johnAlexander | 14:8320b5ff96fa | 4657 | status = vl53l0x_update_byte(dev, |
johnAlexander | 14:8320b5ff96fa | 4658 | VL53L0X_REG_MSRC_CONFIG_CONTROL, |
johnAlexander | 14:8320b5ff96fa | 4659 | 0xEF, temp8); |
johnAlexander | 14:8320b5ff96fa | 4660 | |
johnAlexander | 14:8320b5ff96fa | 4661 | break; |
johnAlexander | 14:8320b5ff96fa | 4662 | |
johnAlexander | 14:8320b5ff96fa | 4663 | |
johnAlexander | 14:8320b5ff96fa | 4664 | default: |
johnAlexander | 14:8320b5ff96fa | 4665 | status = VL53L0X_ERROR_INVALID_PARAMS; |
johnAlexander | 14:8320b5ff96fa | 4666 | |
johnAlexander | 14:8320b5ff96fa | 4667 | } |
johnAlexander | 14:8320b5ff96fa | 4668 | |
johnAlexander | 14:8320b5ff96fa | 4669 | } |
johnAlexander | 14:8320b5ff96fa | 4670 | |
johnAlexander | 14:8320b5ff96fa | 4671 | if (status == VL53L0X_ERROR_NONE) { |
johnAlexander | 14:8320b5ff96fa | 4672 | if (limit_check_enable == 0) { |
johnAlexander | 14:8320b5ff96fa | 4673 | VL53L0X_SETARRAYPARAMETERFIELD(dev, LimitChecksEnable, |
johnAlexander | 14:8320b5ff96fa | 4674 | limit_check_id, 0); |
johnAlexander | 14:8320b5ff96fa | 4675 | } else { |
johnAlexander | 14:8320b5ff96fa | 4676 | VL53L0X_SETARRAYPARAMETERFIELD(dev, LimitChecksEnable, |
johnAlexander | 14:8320b5ff96fa | 4677 | limit_check_id, 1); |
johnAlexander | 14:8320b5ff96fa | 4678 | } |
johnAlexander | 14:8320b5ff96fa | 4679 | } |
johnAlexander | 14:8320b5ff96fa | 4680 | |
johnAlexander | 14:8320b5ff96fa | 4681 | LOG_FUNCTION_END(status); |
johnAlexander | 14:8320b5ff96fa | 4682 | return status; |
johnAlexander | 0:c523920bcc09 | 4683 | } |
johnAlexander | 0:c523920bcc09 | 4684 | |
johnAlexander | 14:8320b5ff96fa | 4685 | VL53L0X_Error VL53L0X::vl53l0x_static_init(VL53L0X_DEV dev) |
johnAlexander | 0:c523920bcc09 | 4686 | { |
johnAlexander | 14:8320b5ff96fa | 4687 | VL53L0X_Error status = VL53L0X_ERROR_NONE; |
johnAlexander | 14:8320b5ff96fa | 4688 | VL53L0X_DeviceParameters_t current_parameters = {0}; |
johnAlexander | 14:8320b5ff96fa | 4689 | uint8_t *p_tuning_setting_buffer; |
johnAlexander | 14:8320b5ff96fa | 4690 | uint16_t tempword = 0; |
johnAlexander | 14:8320b5ff96fa | 4691 | uint8_t tempbyte = 0; |
johnAlexander | 14:8320b5ff96fa | 4692 | uint8_t use_internal_tuning_settings = 0; |
johnAlexander | 14:8320b5ff96fa | 4693 | uint32_t count = 0; |
johnAlexander | 14:8320b5ff96fa | 4694 | uint8_t is_aperture_spads = 0; |
johnAlexander | 14:8320b5ff96fa | 4695 | uint32_t ref_spad_count = 0; |
johnAlexander | 14:8320b5ff96fa | 4696 | uint8_t aperture_spads = 0; |
johnAlexander | 14:8320b5ff96fa | 4697 | uint8_t vcsel_pulse_period_pclk; |
johnAlexander | 14:8320b5ff96fa | 4698 | uint32_t seq_timeout_micro_secs; |
johnAlexander | 14:8320b5ff96fa | 4699 | |
johnAlexander | 14:8320b5ff96fa | 4700 | LOG_FUNCTION_START(""); |
johnAlexander | 14:8320b5ff96fa | 4701 | |
johnAlexander | 14:8320b5ff96fa | 4702 | status = vl53l0x_get_info_from_device(dev, 1); |
johnAlexander | 14:8320b5ff96fa | 4703 | |
johnAlexander | 14:8320b5ff96fa | 4704 | /* set the ref spad from NVM */ |
johnAlexander | 14:8320b5ff96fa | 4705 | count = (uint32_t)VL53L0X_GETDEVICESPECIFICPARAMETER(dev, |
johnAlexander | 14:8320b5ff96fa | 4706 | ReferenceSpadCount); |
johnAlexander | 14:8320b5ff96fa | 4707 | aperture_spads = VL53L0X_GETDEVICESPECIFICPARAMETER(dev, |
johnAlexander | 14:8320b5ff96fa | 4708 | ReferenceSpadType); |
johnAlexander | 14:8320b5ff96fa | 4709 | |
johnAlexander | 14:8320b5ff96fa | 4710 | /* NVM value invalid */ |
johnAlexander | 14:8320b5ff96fa | 4711 | if ((aperture_spads > 1) || |
johnAlexander | 14:8320b5ff96fa | 4712 | ((aperture_spads == 1) && (count > 32)) || |
johnAlexander | 14:8320b5ff96fa | 4713 | ((aperture_spads == 0) && (count > 12))) |
johnAlexander | 14:8320b5ff96fa | 4714 | status = wrapped_vl53l0x_perform_ref_spad_management(dev, &ref_spad_count, |
johnAlexander | 14:8320b5ff96fa | 4715 | &is_aperture_spads); |
johnAlexander | 14:8320b5ff96fa | 4716 | else |
johnAlexander | 14:8320b5ff96fa | 4717 | status = vl53l0x_set_reference_spads(dev, count, aperture_spads); |
johnAlexander | 14:8320b5ff96fa | 4718 | |
johnAlexander | 14:8320b5ff96fa | 4719 | |
johnAlexander | 14:8320b5ff96fa | 4720 | /* Initialize tuning settings buffer to prevent compiler warning. */ |
johnAlexander | 14:8320b5ff96fa | 4721 | p_tuning_setting_buffer = DefaultTuningSettings; |
johnAlexander | 14:8320b5ff96fa | 4722 | |
johnAlexander | 14:8320b5ff96fa | 4723 | if (status == VL53L0X_ERROR_NONE) { |
johnAlexander | 14:8320b5ff96fa | 4724 | use_internal_tuning_settings = PALDevDataGet(dev, |
johnAlexander | 14:8320b5ff96fa | 4725 | UseInternalTuningSettings); |
johnAlexander | 14:8320b5ff96fa | 4726 | |
johnAlexander | 14:8320b5ff96fa | 4727 | if (use_internal_tuning_settings == 0) |
johnAlexander | 14:8320b5ff96fa | 4728 | p_tuning_setting_buffer = PALDevDataGet(dev, |
johnAlexander | 14:8320b5ff96fa | 4729 | pTuningSettingsPointer); |
johnAlexander | 14:8320b5ff96fa | 4730 | else |
johnAlexander | 14:8320b5ff96fa | 4731 | p_tuning_setting_buffer = DefaultTuningSettings; |
johnAlexander | 14:8320b5ff96fa | 4732 | |
johnAlexander | 14:8320b5ff96fa | 4733 | } |
johnAlexander | 14:8320b5ff96fa | 4734 | |
johnAlexander | 14:8320b5ff96fa | 4735 | if (status == VL53L0X_ERROR_NONE) |
johnAlexander | 14:8320b5ff96fa | 4736 | status = vl53l0x_load_tuning_settings(dev, p_tuning_setting_buffer); |
johnAlexander | 14:8320b5ff96fa | 4737 | |
johnAlexander | 14:8320b5ff96fa | 4738 | |
johnAlexander | 14:8320b5ff96fa | 4739 | /* Set interrupt config to new sample ready */ |
johnAlexander | 14:8320b5ff96fa | 4740 | if (status == VL53L0X_ERROR_NONE) { |
johnAlexander | 14:8320b5ff96fa | 4741 | status = vl53l0x_set_gpio_config(dev, 0, 0, |
johnAlexander | 14:8320b5ff96fa | 4742 | VL53L0X_REG_SYSTEM_INTERRUPT_GPIO_NEW_SAMPLE_READY, |
johnAlexander | 14:8320b5ff96fa | 4743 | VL53L0X_INTERRUPTPOLARITY_LOW); |
johnAlexander | 14:8320b5ff96fa | 4744 | } |
johnAlexander | 14:8320b5ff96fa | 4745 | |
johnAlexander | 14:8320b5ff96fa | 4746 | if (status == VL53L0X_ERROR_NONE) { |
johnAlexander | 14:8320b5ff96fa | 4747 | status = vl53l0x_write_byte(dev, 0xFF, 0x01); |
johnAlexander | 14:8320b5ff96fa | 4748 | status |= vl53l0x_read_word(dev, 0x84, &tempword); |
johnAlexander | 14:8320b5ff96fa | 4749 | status |= vl53l0x_write_byte(dev, 0xFF, 0x00); |
johnAlexander | 14:8320b5ff96fa | 4750 | } |
johnAlexander | 14:8320b5ff96fa | 4751 | |
johnAlexander | 14:8320b5ff96fa | 4752 | if (status == VL53L0X_ERROR_NONE) { |
johnAlexander | 14:8320b5ff96fa | 4753 | VL53L0X_SETDEVICESPECIFICPARAMETER(dev, OscFrequencyMHz, |
johnAlexander | 14:8320b5ff96fa | 4754 | VL53L0X_FIXPOINT412TOFIXPOINT1616(tempword)); |
johnAlexander | 14:8320b5ff96fa | 4755 | } |
johnAlexander | 14:8320b5ff96fa | 4756 | |
johnAlexander | 14:8320b5ff96fa | 4757 | /* After static init, some device parameters may be changed, |
johnAlexander | 14:8320b5ff96fa | 4758 | * so update them */ |
johnAlexander | 14:8320b5ff96fa | 4759 | if (status == VL53L0X_ERROR_NONE) |
johnAlexander | 14:8320b5ff96fa | 4760 | status = vl53l0x_get_device_parameters(dev, ¤t_parameters); |
johnAlexander | 14:8320b5ff96fa | 4761 | |
johnAlexander | 14:8320b5ff96fa | 4762 | |
johnAlexander | 14:8320b5ff96fa | 4763 | if (status == VL53L0X_ERROR_NONE) { |
johnAlexander | 14:8320b5ff96fa | 4764 | status = vl53l0x_get_fraction_enable(dev, &tempbyte); |
johnAlexander | 14:8320b5ff96fa | 4765 | if (status == VL53L0X_ERROR_NONE) |
johnAlexander | 14:8320b5ff96fa | 4766 | PALDevDataSet(dev, RangeFractionalEnable, tempbyte); |
johnAlexander | 14:8320b5ff96fa | 4767 | |
johnAlexander | 14:8320b5ff96fa | 4768 | } |
johnAlexander | 14:8320b5ff96fa | 4769 | |
johnAlexander | 14:8320b5ff96fa | 4770 | if (status == VL53L0X_ERROR_NONE) |
johnAlexander | 14:8320b5ff96fa | 4771 | PALDevDataSet(dev, CurrentParameters, current_parameters); |
johnAlexander | 14:8320b5ff96fa | 4772 | |
johnAlexander | 14:8320b5ff96fa | 4773 | |
johnAlexander | 14:8320b5ff96fa | 4774 | /* read the sequence config and save it */ |
johnAlexander | 14:8320b5ff96fa | 4775 | if (status == VL53L0X_ERROR_NONE) { |
johnAlexander | 14:8320b5ff96fa | 4776 | status = vl53l0x_read_byte(dev, |
johnAlexander | 14:8320b5ff96fa | 4777 | VL53L0X_REG_SYSTEM_SEQUENCE_CONFIG, &tempbyte); |
johnAlexander | 14:8320b5ff96fa | 4778 | if (status == VL53L0X_ERROR_NONE) |
johnAlexander | 14:8320b5ff96fa | 4779 | PALDevDataSet(dev, SequenceConfig, tempbyte); |
johnAlexander | 14:8320b5ff96fa | 4780 | |
johnAlexander | 14:8320b5ff96fa | 4781 | } |
johnAlexander | 14:8320b5ff96fa | 4782 | |
johnAlexander | 14:8320b5ff96fa | 4783 | /* Disable MSRC and TCC by default */ |
johnAlexander | 14:8320b5ff96fa | 4784 | if (status == VL53L0X_ERROR_NONE) |
johnAlexander | 14:8320b5ff96fa | 4785 | status = vl53l0x_set_sequence_step_enable(dev, |
johnAlexander | 14:8320b5ff96fa | 4786 | VL53L0X_SEQUENCESTEP_TCC, 0); |
johnAlexander | 14:8320b5ff96fa | 4787 | |
johnAlexander | 14:8320b5ff96fa | 4788 | |
johnAlexander | 14:8320b5ff96fa | 4789 | if (status == VL53L0X_ERROR_NONE) |
johnAlexander | 14:8320b5ff96fa | 4790 | status = vl53l0x_set_sequence_step_enable(dev, |
johnAlexander | 14:8320b5ff96fa | 4791 | VL53L0X_SEQUENCESTEP_MSRC, 0); |
johnAlexander | 14:8320b5ff96fa | 4792 | |
johnAlexander | 14:8320b5ff96fa | 4793 | |
johnAlexander | 14:8320b5ff96fa | 4794 | /* Set PAL State to standby */ |
johnAlexander | 14:8320b5ff96fa | 4795 | if (status == VL53L0X_ERROR_NONE) |
johnAlexander | 14:8320b5ff96fa | 4796 | PALDevDataSet(dev, PalState, VL53L0X_STATE_IDLE); |
johnAlexander | 14:8320b5ff96fa | 4797 | |
johnAlexander | 14:8320b5ff96fa | 4798 | |
johnAlexander | 14:8320b5ff96fa | 4799 | |
johnAlexander | 14:8320b5ff96fa | 4800 | /* Store pre-range vcsel period */ |
johnAlexander | 14:8320b5ff96fa | 4801 | if (status == VL53L0X_ERROR_NONE) { |
johnAlexander | 14:8320b5ff96fa | 4802 | status = vl53l0x_get_vcsel_pulse_period( |
johnAlexander | 14:8320b5ff96fa | 4803 | dev, |
johnAlexander | 14:8320b5ff96fa | 4804 | VL53L0X_VCSEL_PERIOD_PRE_RANGE, |
johnAlexander | 14:8320b5ff96fa | 4805 | &vcsel_pulse_period_pclk); |
johnAlexander | 14:8320b5ff96fa | 4806 | } |
johnAlexander | 14:8320b5ff96fa | 4807 | |
johnAlexander | 14:8320b5ff96fa | 4808 | if (status == VL53L0X_ERROR_NONE) { |
johnAlexander | 14:8320b5ff96fa | 4809 | VL53L0X_SETDEVICESPECIFICPARAMETER( |
johnAlexander | 14:8320b5ff96fa | 4810 | dev, |
johnAlexander | 14:8320b5ff96fa | 4811 | PreRangeVcselPulsePeriod, |
johnAlexander | 14:8320b5ff96fa | 4812 | vcsel_pulse_period_pclk); |
johnAlexander | 14:8320b5ff96fa | 4813 | } |
johnAlexander | 14:8320b5ff96fa | 4814 | |
johnAlexander | 14:8320b5ff96fa | 4815 | /* Store final-range vcsel period */ |
johnAlexander | 14:8320b5ff96fa | 4816 | if (status == VL53L0X_ERROR_NONE) { |
johnAlexander | 14:8320b5ff96fa | 4817 | status = vl53l0x_get_vcsel_pulse_period( |
johnAlexander | 14:8320b5ff96fa | 4818 | dev, |
johnAlexander | 14:8320b5ff96fa | 4819 | VL53L0X_VCSEL_PERIOD_FINAL_RANGE, |
johnAlexander | 14:8320b5ff96fa | 4820 | &vcsel_pulse_period_pclk); |
johnAlexander | 14:8320b5ff96fa | 4821 | } |
johnAlexander | 14:8320b5ff96fa | 4822 | |
johnAlexander | 14:8320b5ff96fa | 4823 | if (status == VL53L0X_ERROR_NONE) { |
johnAlexander | 14:8320b5ff96fa | 4824 | VL53L0X_SETDEVICESPECIFICPARAMETER( |
johnAlexander | 14:8320b5ff96fa | 4825 | dev, |
johnAlexander | 14:8320b5ff96fa | 4826 | FinalRangeVcselPulsePeriod, |
johnAlexander | 14:8320b5ff96fa | 4827 | vcsel_pulse_period_pclk); |
johnAlexander | 14:8320b5ff96fa | 4828 | } |
johnAlexander | 14:8320b5ff96fa | 4829 | |
johnAlexander | 14:8320b5ff96fa | 4830 | /* Store pre-range timeout */ |
johnAlexander | 14:8320b5ff96fa | 4831 | if (status == VL53L0X_ERROR_NONE) { |
johnAlexander | 14:8320b5ff96fa | 4832 | status = get_sequence_step_timeout( |
johnAlexander | 14:8320b5ff96fa | 4833 | dev, |
johnAlexander | 14:8320b5ff96fa | 4834 | VL53L0X_SEQUENCESTEP_PRE_RANGE, |
johnAlexander | 14:8320b5ff96fa | 4835 | &seq_timeout_micro_secs); |
johnAlexander | 14:8320b5ff96fa | 4836 | } |
johnAlexander | 14:8320b5ff96fa | 4837 | |
johnAlexander | 14:8320b5ff96fa | 4838 | if (status == VL53L0X_ERROR_NONE) { |
johnAlexander | 14:8320b5ff96fa | 4839 | VL53L0X_SETDEVICESPECIFICPARAMETER( |
johnAlexander | 14:8320b5ff96fa | 4840 | dev, |
johnAlexander | 14:8320b5ff96fa | 4841 | PreRangeTimeoutMicroSecs, |
johnAlexander | 14:8320b5ff96fa | 4842 | seq_timeout_micro_secs); |
johnAlexander | 14:8320b5ff96fa | 4843 | } |
johnAlexander | 14:8320b5ff96fa | 4844 | |
johnAlexander | 14:8320b5ff96fa | 4845 | /* Store final-range timeout */ |
johnAlexander | 14:8320b5ff96fa | 4846 | if (status == VL53L0X_ERROR_NONE) { |
johnAlexander | 14:8320b5ff96fa | 4847 | status = get_sequence_step_timeout( |
johnAlexander | 14:8320b5ff96fa | 4848 | dev, |
johnAlexander | 14:8320b5ff96fa | 4849 | VL53L0X_SEQUENCESTEP_FINAL_RANGE, |
johnAlexander | 14:8320b5ff96fa | 4850 | &seq_timeout_micro_secs); |
johnAlexander | 14:8320b5ff96fa | 4851 | } |
johnAlexander | 14:8320b5ff96fa | 4852 | |
johnAlexander | 14:8320b5ff96fa | 4853 | if (status == VL53L0X_ERROR_NONE) { |
johnAlexander | 14:8320b5ff96fa | 4854 | VL53L0X_SETDEVICESPECIFICPARAMETER( |
johnAlexander | 14:8320b5ff96fa | 4855 | dev, |
johnAlexander | 14:8320b5ff96fa | 4856 | FinalRangeTimeoutMicroSecs, |
johnAlexander | 14:8320b5ff96fa | 4857 | seq_timeout_micro_secs); |
johnAlexander | 14:8320b5ff96fa | 4858 | } |
johnAlexander | 14:8320b5ff96fa | 4859 | |
johnAlexander | 14:8320b5ff96fa | 4860 | LOG_FUNCTION_END(status); |
johnAlexander | 14:8320b5ff96fa | 4861 | return status; |
johnAlexander | 0:c523920bcc09 | 4862 | } |
johnAlexander | 0:c523920bcc09 | 4863 | |
johnAlexander | 0:c523920bcc09 | 4864 | |
johnAlexander | 14:8320b5ff96fa | 4865 | VL53L0X_Error VL53L0X::vl53l0x_stop_measurement(VL53L0X_DEV dev) |
johnAlexander | 0:c523920bcc09 | 4866 | { |
johnAlexander | 14:8320b5ff96fa | 4867 | VL53L0X_Error status = VL53L0X_ERROR_NONE; |
johnAlexander | 14:8320b5ff96fa | 4868 | LOG_FUNCTION_START(""); |
johnAlexander | 14:8320b5ff96fa | 4869 | |
johnAlexander | 14:8320b5ff96fa | 4870 | status = vl53l0x_write_byte(dev, VL53L0X_REG_SYSRANGE_START, |
johnAlexander | 14:8320b5ff96fa | 4871 | VL53L0X_REG_SYSRANGE_MODE_SINGLESHOT); |
johnAlexander | 14:8320b5ff96fa | 4872 | |
johnAlexander | 14:8320b5ff96fa | 4873 | status = vl53l0x_write_byte(dev, 0xFF, 0x01); |
johnAlexander | 14:8320b5ff96fa | 4874 | status = vl53l0x_write_byte(dev, 0x00, 0x00); |
johnAlexander | 14:8320b5ff96fa | 4875 | status = vl53l0x_write_byte(dev, 0x91, 0x00); |
johnAlexander | 14:8320b5ff96fa | 4876 | status = vl53l0x_write_byte(dev, 0x00, 0x01); |
johnAlexander | 14:8320b5ff96fa | 4877 | status = vl53l0x_write_byte(dev, 0xFF, 0x00); |
johnAlexander | 14:8320b5ff96fa | 4878 | |
johnAlexander | 14:8320b5ff96fa | 4879 | if (status == VL53L0X_ERROR_NONE) { |
johnAlexander | 14:8320b5ff96fa | 4880 | /* Set PAL State to Idle */ |
johnAlexander | 14:8320b5ff96fa | 4881 | PALDevDataSet(dev, PalState, VL53L0X_STATE_IDLE); |
johnAlexander | 14:8320b5ff96fa | 4882 | } |
johnAlexander | 14:8320b5ff96fa | 4883 | |
johnAlexander | 14:8320b5ff96fa | 4884 | /* Check if need to apply interrupt settings */ |
johnAlexander | 14:8320b5ff96fa | 4885 | if (status == VL53L0X_ERROR_NONE) |
johnAlexander | 14:8320b5ff96fa | 4886 | status = vl53l0x_check_and_load_interrupt_settings(dev, 0); |
johnAlexander | 14:8320b5ff96fa | 4887 | |
johnAlexander | 14:8320b5ff96fa | 4888 | LOG_FUNCTION_END(status); |
johnAlexander | 14:8320b5ff96fa | 4889 | return status; |
johnAlexander | 0:c523920bcc09 | 4890 | } |
johnAlexander | 0:c523920bcc09 | 4891 | |
johnAlexander | 14:8320b5ff96fa | 4892 | VL53L0X_Error VL53L0X::vl53l0x_get_stop_completed_status(VL53L0X_DEV dev, |
johnAlexander | 14:8320b5ff96fa | 4893 | uint32_t *p_stop_status) |
johnAlexander | 0:c523920bcc09 | 4894 | { |
johnAlexander | 14:8320b5ff96fa | 4895 | VL53L0X_Error status = VL53L0X_ERROR_NONE; |
johnAlexander | 14:8320b5ff96fa | 4896 | uint8_t byte = 0; |
johnAlexander | 14:8320b5ff96fa | 4897 | LOG_FUNCTION_START(""); |
johnAlexander | 14:8320b5ff96fa | 4898 | |
johnAlexander | 14:8320b5ff96fa | 4899 | status = vl53l0x_write_byte(dev, 0xFF, 0x01); |
johnAlexander | 14:8320b5ff96fa | 4900 | |
johnAlexander | 14:8320b5ff96fa | 4901 | if (status == VL53L0X_ERROR_NONE) |
johnAlexander | 14:8320b5ff96fa | 4902 | status = vl53l0x_read_byte(dev, 0x04, &byte); |
johnAlexander | 14:8320b5ff96fa | 4903 | |
johnAlexander | 14:8320b5ff96fa | 4904 | if (status == VL53L0X_ERROR_NONE) |
johnAlexander | 14:8320b5ff96fa | 4905 | status = vl53l0x_write_byte(dev, 0xFF, 0x0); |
johnAlexander | 14:8320b5ff96fa | 4906 | |
johnAlexander | 14:8320b5ff96fa | 4907 | *p_stop_status = byte; |
johnAlexander | 14:8320b5ff96fa | 4908 | |
johnAlexander | 14:8320b5ff96fa | 4909 | if (byte == 0) { |
johnAlexander | 14:8320b5ff96fa | 4910 | status = vl53l0x_write_byte(dev, 0x80, 0x01); |
johnAlexander | 14:8320b5ff96fa | 4911 | status = vl53l0x_write_byte(dev, 0xFF, 0x01); |
johnAlexander | 14:8320b5ff96fa | 4912 | status = vl53l0x_write_byte(dev, 0x00, 0x00); |
johnAlexander | 14:8320b5ff96fa | 4913 | status = vl53l0x_write_byte(dev, 0x91, |
johnAlexander | 14:8320b5ff96fa | 4914 | PALDevDataGet(dev, StopVariable)); |
johnAlexander | 14:8320b5ff96fa | 4915 | status = vl53l0x_write_byte(dev, 0x00, 0x01); |
johnAlexander | 14:8320b5ff96fa | 4916 | status = vl53l0x_write_byte(dev, 0xFF, 0x00); |
johnAlexander | 14:8320b5ff96fa | 4917 | status = vl53l0x_write_byte(dev, 0x80, 0x00); |
johnAlexander | 14:8320b5ff96fa | 4918 | } |
johnAlexander | 14:8320b5ff96fa | 4919 | |
johnAlexander | 14:8320b5ff96fa | 4920 | LOG_FUNCTION_END(status); |
johnAlexander | 14:8320b5ff96fa | 4921 | return status; |
johnAlexander | 0:c523920bcc09 | 4922 | } |
johnAlexander | 0:c523920bcc09 | 4923 | |
johnAlexander | 0:c523920bcc09 | 4924 | /****************** Write and read functions from I2C *************************/ |
johnAlexander | 0:c523920bcc09 | 4925 | |
johnAlexander | 14:8320b5ff96fa | 4926 | VL53L0X_Error VL53L0X::vl53l0x_write_multi(VL53L0X_DEV dev, uint8_t index, uint8_t *p_data, uint32_t count) |
johnAlexander | 0:c523920bcc09 | 4927 | { |
johnAlexander | 14:8320b5ff96fa | 4928 | int status; |
johnAlexander | 14:8320b5ff96fa | 4929 | |
johnAlexander | 14:8320b5ff96fa | 4930 | status = vl53l0x_i2c_write(dev->I2cDevAddr, index, p_data, (uint16_t)count); |
johnAlexander | 14:8320b5ff96fa | 4931 | return status; |
johnAlexander | 0:c523920bcc09 | 4932 | } |
johnAlexander | 0:c523920bcc09 | 4933 | |
johnAlexander | 14:8320b5ff96fa | 4934 | VL53L0X_Error VL53L0X::vl53l0x_read_multi(VL53L0X_DEV dev, uint8_t index, uint8_t *p_data, uint32_t count) |
johnAlexander | 0:c523920bcc09 | 4935 | { |
johnAlexander | 0:c523920bcc09 | 4936 | int status; |
johnAlexander | 0:c523920bcc09 | 4937 | |
johnAlexander | 14:8320b5ff96fa | 4938 | if (count >= VL53L0X_MAX_I2C_XFER_SIZE) { |
johnAlexander | 0:c523920bcc09 | 4939 | status = VL53L0X_ERROR_INVALID_PARAMS; |
johnAlexander | 0:c523920bcc09 | 4940 | } |
johnAlexander | 0:c523920bcc09 | 4941 | |
johnAlexander | 14:8320b5ff96fa | 4942 | status = vl53l0x_i2c_read(dev->I2cDevAddr, index, p_data, (uint16_t)count); |
johnAlexander | 0:c523920bcc09 | 4943 | |
johnAlexander | 0:c523920bcc09 | 4944 | return status; |
johnAlexander | 0:c523920bcc09 | 4945 | } |
johnAlexander | 0:c523920bcc09 | 4946 | |
johnAlexander | 0:c523920bcc09 | 4947 | |
johnAlexander | 14:8320b5ff96fa | 4948 | VL53L0X_Error VL53L0X::vl53l0x_write_byte(VL53L0X_DEV Dev, uint8_t index, uint8_t data) |
johnAlexander | 0:c523920bcc09 | 4949 | { |
johnAlexander | 14:8320b5ff96fa | 4950 | int status; |
johnAlexander | 14:8320b5ff96fa | 4951 | |
johnAlexander | 14:8320b5ff96fa | 4952 | status = vl53l0x_i2c_write(Dev->I2cDevAddr, index, &data, 1); |
johnAlexander | 14:8320b5ff96fa | 4953 | return status; |
johnAlexander | 0:c523920bcc09 | 4954 | } |
johnAlexander | 14:8320b5ff96fa | 4955 | |
johnAlexander | 14:8320b5ff96fa | 4956 | VL53L0X_Error VL53L0X::vl53l0x_write_word(VL53L0X_DEV dev, uint8_t index, uint16_t data) |
johnAlexander | 0:c523920bcc09 | 4957 | { |
johnAlexander | 14:8320b5ff96fa | 4958 | int status; |
johnAlexander | 14:8320b5ff96fa | 4959 | uint8_t buffer[2]; |
johnAlexander | 14:8320b5ff96fa | 4960 | |
johnAlexander | 14:8320b5ff96fa | 4961 | buffer[0] = data >> 8; |
johnAlexander | 14:8320b5ff96fa | 4962 | buffer[1] = data & 0x00FF; |
johnAlexander | 14:8320b5ff96fa | 4963 | status = vl53l0x_i2c_write(dev->I2cDevAddr, index, (uint8_t *)buffer, 2); |
johnAlexander | 14:8320b5ff96fa | 4964 | return status; |
johnAlexander | 0:c523920bcc09 | 4965 | } |
johnAlexander | 14:8320b5ff96fa | 4966 | |
johnAlexander | 14:8320b5ff96fa | 4967 | VL53L0X_Error VL53L0X::vl53l0x_write_dword(VL53L0X_DEV Dev, uint8_t index, uint32_t data) |
johnAlexander | 0:c523920bcc09 | 4968 | { |
johnAlexander | 14:8320b5ff96fa | 4969 | int status; |
johnAlexander | 14:8320b5ff96fa | 4970 | uint8_t buffer[4]; |
johnAlexander | 14:8320b5ff96fa | 4971 | |
johnAlexander | 14:8320b5ff96fa | 4972 | buffer[0] = (data >> 24) & 0xFF; |
johnAlexander | 14:8320b5ff96fa | 4973 | buffer[1] = (data >> 16) & 0xFF; |
johnAlexander | 14:8320b5ff96fa | 4974 | buffer[2] = (data >> 8) & 0xFF; |
johnAlexander | 14:8320b5ff96fa | 4975 | buffer[3] = (data >> 0) & 0xFF; |
johnAlexander | 14:8320b5ff96fa | 4976 | status = vl53l0x_i2c_write(Dev->I2cDevAddr, index, (uint8_t *)buffer, 4); |
johnAlexander | 14:8320b5ff96fa | 4977 | return status; |
johnAlexander | 0:c523920bcc09 | 4978 | } |
johnAlexander | 0:c523920bcc09 | 4979 | |
johnAlexander | 0:c523920bcc09 | 4980 | |
johnAlexander | 14:8320b5ff96fa | 4981 | VL53L0X_Error VL53L0X::vl53l0x_read_byte(VL53L0X_DEV Dev, uint8_t index, uint8_t *p_data) |
johnAlexander | 0:c523920bcc09 | 4982 | { |
johnAlexander | 14:8320b5ff96fa | 4983 | int status; |
johnAlexander | 14:8320b5ff96fa | 4984 | |
johnAlexander | 14:8320b5ff96fa | 4985 | status = vl53l0x_i2c_read(Dev->I2cDevAddr, index, p_data, 1); |
johnAlexander | 14:8320b5ff96fa | 4986 | |
johnAlexander | 14:8320b5ff96fa | 4987 | if (status) |
johnAlexander | 14:8320b5ff96fa | 4988 | return -1; |
johnAlexander | 14:8320b5ff96fa | 4989 | |
johnAlexander | 14:8320b5ff96fa | 4990 | return 0; |
johnAlexander | 0:c523920bcc09 | 4991 | } |
johnAlexander | 14:8320b5ff96fa | 4992 | |
johnAlexander | 14:8320b5ff96fa | 4993 | VL53L0X_Error VL53L0X::vl53l0x_read_word(VL53L0X_DEV Dev, uint8_t index, uint16_t *p_data) |
johnAlexander | 0:c523920bcc09 | 4994 | { |
johnAlexander | 14:8320b5ff96fa | 4995 | int status; |
johnAlexander | 14:8320b5ff96fa | 4996 | uint8_t buffer[2] = {0, 0}; |
johnAlexander | 14:8320b5ff96fa | 4997 | |
johnAlexander | 14:8320b5ff96fa | 4998 | status = vl53l0x_i2c_read(Dev->I2cDevAddr, index, buffer, 2); |
johnAlexander | 14:8320b5ff96fa | 4999 | if (!status) { |
johnAlexander | 14:8320b5ff96fa | 5000 | *p_data = (buffer[0] << 8) + buffer[1]; |
johnAlexander | 14:8320b5ff96fa | 5001 | } |
johnAlexander | 14:8320b5ff96fa | 5002 | return status; |
johnAlexander | 0:c523920bcc09 | 5003 | |
johnAlexander | 0:c523920bcc09 | 5004 | } |
johnAlexander | 14:8320b5ff96fa | 5005 | |
johnAlexander | 14:8320b5ff96fa | 5006 | VL53L0X_Error VL53L0X::vl53l0x_read_dword(VL53L0X_DEV Dev, uint8_t index, uint32_t *p_data) |
johnAlexander | 0:c523920bcc09 | 5007 | { |
johnAlexander | 14:8320b5ff96fa | 5008 | int status; |
johnAlexander | 14:8320b5ff96fa | 5009 | uint8_t buffer[4] = {0, 0, 0, 0}; |
johnAlexander | 14:8320b5ff96fa | 5010 | |
johnAlexander | 14:8320b5ff96fa | 5011 | status = vl53l0x_i2c_read(Dev->I2cDevAddr, index, buffer, 4); |
johnAlexander | 14:8320b5ff96fa | 5012 | if (!status) { |
johnAlexander | 14:8320b5ff96fa | 5013 | *p_data = (buffer[0] << 24) + (buffer[1] << 16) + (buffer[2] << 8) + buffer[3]; |
johnAlexander | 14:8320b5ff96fa | 5014 | } |
johnAlexander | 14:8320b5ff96fa | 5015 | return status; |
johnAlexander | 0:c523920bcc09 | 5016 | |
johnAlexander | 0:c523920bcc09 | 5017 | } |
johnAlexander | 0:c523920bcc09 | 5018 | |
johnAlexander | 14:8320b5ff96fa | 5019 | VL53L0X_Error VL53L0X::vl53l0x_update_byte(VL53L0X_DEV Dev, uint8_t index, uint8_t and_data, uint8_t or_data) |
johnAlexander | 0:c523920bcc09 | 5020 | { |
johnAlexander | 14:8320b5ff96fa | 5021 | int status; |
johnAlexander | 14:8320b5ff96fa | 5022 | uint8_t buffer = 0; |
johnAlexander | 14:8320b5ff96fa | 5023 | |
johnAlexander | 14:8320b5ff96fa | 5024 | /* read data direct onto buffer */ |
johnAlexander | 14:8320b5ff96fa | 5025 | status = vl53l0x_i2c_read(Dev->I2cDevAddr, index, &buffer, 1); |
johnAlexander | 14:8320b5ff96fa | 5026 | if (!status) { |
johnAlexander | 14:8320b5ff96fa | 5027 | buffer = (buffer & and_data) | or_data; |
johnAlexander | 14:8320b5ff96fa | 5028 | status = vl53l0x_i2c_write(Dev->I2cDevAddr, index, &buffer, (uint8_t)1); |
johnAlexander | 14:8320b5ff96fa | 5029 | } |
johnAlexander | 14:8320b5ff96fa | 5030 | return status; |
johnAlexander | 0:c523920bcc09 | 5031 | } |
johnAlexander | 14:8320b5ff96fa | 5032 | |
johnAlexander | 14:8320b5ff96fa | 5033 | VL53L0X_Error VL53L0X::vl53l0x_i2c_write(uint8_t DeviceAddr, uint8_t RegisterAddr, uint8_t *p_data, |
johnAlexander | 14:8320b5ff96fa | 5034 | uint16_t NumByteToWrite) |
johnAlexander | 0:c523920bcc09 | 5035 | { |
johnAlexander | 14:8320b5ff96fa | 5036 | int ret; |
johnAlexander | 14:8320b5ff96fa | 5037 | |
johnAlexander | 14:8320b5ff96fa | 5038 | ret = dev_i2c.i2c_write(p_data, DeviceAddr, RegisterAddr, NumByteToWrite); |
johnAlexander | 14:8320b5ff96fa | 5039 | |
johnAlexander | 14:8320b5ff96fa | 5040 | if (ret) |
johnAlexander | 14:8320b5ff96fa | 5041 | return -1; |
johnAlexander | 14:8320b5ff96fa | 5042 | return 0; |
johnAlexander | 0:c523920bcc09 | 5043 | } |
johnAlexander | 14:8320b5ff96fa | 5044 | |
johnAlexander | 14:8320b5ff96fa | 5045 | VL53L0X_Error VL53L0X::vl53l0x_i2c_read(uint8_t DeviceAddr, uint8_t RegisterAddr, uint8_t *p_data, |
johnAlexander | 14:8320b5ff96fa | 5046 | uint16_t NumByteToRead) |
johnAlexander | 0:c523920bcc09 | 5047 | { |
johnAlexander | 14:8320b5ff96fa | 5048 | int ret; |
johnAlexander | 14:8320b5ff96fa | 5049 | |
johnAlexander | 14:8320b5ff96fa | 5050 | ret = dev_i2c.i2c_read(p_data, DeviceAddr, RegisterAddr, NumByteToRead); |
johnAlexander | 14:8320b5ff96fa | 5051 | |
johnAlexander | 14:8320b5ff96fa | 5052 | if (ret) |
johnAlexander | 14:8320b5ff96fa | 5053 | return -1; |
johnAlexander | 14:8320b5ff96fa | 5054 | return 0; |
johnAlexander | 14:8320b5ff96fa | 5055 | } |
johnAlexander | 0:c523920bcc09 | 5056 | |
JerrySzczurak | 11:ceaa5a026412 | 5057 | int VL53L0X::read_id(uint8_t *id) |
johnAlexander | 0:c523920bcc09 | 5058 | { |
johnAlexander | 0:c523920bcc09 | 5059 | int status = 0; |
johnAlexander | 14:8320b5ff96fa | 5060 | uint16_t rl_id = 0; |
johnAlexander | 14:8320b5ff96fa | 5061 | |
johnAlexander | 14:8320b5ff96fa | 5062 | status = vl53l0x_read_word(_device, VL53L0X_REG_IDENTIFICATION_MODEL_ID, &rl_id); |
johnAlexander | 0:c523920bcc09 | 5063 | if (rl_id == 0xEEAA) |
johnAlexander | 0:c523920bcc09 | 5064 | return status; |
johnAlexander | 14:8320b5ff96fa | 5065 | |
johnAlexander | 0:c523920bcc09 | 5066 | return -1; |
johnAlexander | 0:c523920bcc09 | 5067 | } |
JerrySzczurak | 11:ceaa5a026412 | 5068 | |
johnAlexander | 14:8320b5ff96fa | 5069 | |
johnAlexander | 14:8320b5ff96fa | 5070 | VL53L0X_Error VL53L0X::wait_measurement_data_ready(VL53L0X_DEV dev) |
JerrySzczurak | 11:ceaa5a026412 | 5071 | { |
johnAlexander | 14:8320b5ff96fa | 5072 | VL53L0X_Error status = VL53L0X_ERROR_NONE; |
johnAlexander | 14:8320b5ff96fa | 5073 | uint8_t new_dat_ready = 0; |
johnAlexander | 14:8320b5ff96fa | 5074 | uint32_t loop_nb; |
johnAlexander | 0:c523920bcc09 | 5075 | |
johnAlexander | 0:c523920bcc09 | 5076 | // Wait until it finished |
johnAlexander | 0:c523920bcc09 | 5077 | // use timeout to avoid deadlock |
johnAlexander | 14:8320b5ff96fa | 5078 | if (status == VL53L0X_ERROR_NONE) { |
johnAlexander | 14:8320b5ff96fa | 5079 | loop_nb = 0; |
johnAlexander | 0:c523920bcc09 | 5080 | do { |
johnAlexander | 14:8320b5ff96fa | 5081 | status = vl53l0x_get_measurement_data_ready(dev, &new_dat_ready); |
johnAlexander | 14:8320b5ff96fa | 5082 | if ((new_dat_ready == 0x01) || status != VL53L0X_ERROR_NONE) { |
johnAlexander | 0:c523920bcc09 | 5083 | break; |
johnAlexander | 0:c523920bcc09 | 5084 | } |
johnAlexander | 14:8320b5ff96fa | 5085 | loop_nb = loop_nb + 1; |
johnAlexander | 14:8320b5ff96fa | 5086 | vl53l0x_polling_delay(dev); |
johnAlexander | 14:8320b5ff96fa | 5087 | } while (loop_nb < VL53L0X_DEFAULT_MAX_LOOP); |
johnAlexander | 14:8320b5ff96fa | 5088 | |
johnAlexander | 14:8320b5ff96fa | 5089 | if (loop_nb >= VL53L0X_DEFAULT_MAX_LOOP) { |
johnAlexander | 14:8320b5ff96fa | 5090 | status = VL53L0X_ERROR_TIME_OUT; |
johnAlexander | 0:c523920bcc09 | 5091 | } |
johnAlexander | 0:c523920bcc09 | 5092 | } |
johnAlexander | 0:c523920bcc09 | 5093 | |
johnAlexander | 14:8320b5ff96fa | 5094 | return status; |
johnAlexander | 0:c523920bcc09 | 5095 | } |
johnAlexander | 0:c523920bcc09 | 5096 | |
johnAlexander | 14:8320b5ff96fa | 5097 | VL53L0X_Error VL53L0X::wait_stop_completed(VL53L0X_DEV dev) |
johnAlexander | 0:c523920bcc09 | 5098 | { |
johnAlexander | 14:8320b5ff96fa | 5099 | VL53L0X_Error status = VL53L0X_ERROR_NONE; |
johnAlexander | 14:8320b5ff96fa | 5100 | uint32_t stop_completed = 0; |
johnAlexander | 14:8320b5ff96fa | 5101 | uint32_t loop_nb; |
johnAlexander | 0:c523920bcc09 | 5102 | |
johnAlexander | 0:c523920bcc09 | 5103 | // Wait until it finished |
johnAlexander | 0:c523920bcc09 | 5104 | // use timeout to avoid deadlock |
johnAlexander | 14:8320b5ff96fa | 5105 | if (status == VL53L0X_ERROR_NONE) { |
johnAlexander | 14:8320b5ff96fa | 5106 | loop_nb = 0; |
johnAlexander | 0:c523920bcc09 | 5107 | do { |
johnAlexander | 14:8320b5ff96fa | 5108 | status = vl53l0x_get_stop_completed_status(dev, &stop_completed); |
johnAlexander | 14:8320b5ff96fa | 5109 | if ((stop_completed == 0x00) || status != VL53L0X_ERROR_NONE) { |
johnAlexander | 0:c523920bcc09 | 5110 | break; |
johnAlexander | 0:c523920bcc09 | 5111 | } |
johnAlexander | 14:8320b5ff96fa | 5112 | loop_nb = loop_nb + 1; |
johnAlexander | 14:8320b5ff96fa | 5113 | vl53l0x_polling_delay(dev); |
johnAlexander | 14:8320b5ff96fa | 5114 | } while (loop_nb < VL53L0X_DEFAULT_MAX_LOOP); |
johnAlexander | 14:8320b5ff96fa | 5115 | |
johnAlexander | 14:8320b5ff96fa | 5116 | if (loop_nb >= VL53L0X_DEFAULT_MAX_LOOP) { |
johnAlexander | 14:8320b5ff96fa | 5117 | status = VL53L0X_ERROR_TIME_OUT; |
johnAlexander | 0:c523920bcc09 | 5118 | } |
johnAlexander | 0:c523920bcc09 | 5119 | |
johnAlexander | 0:c523920bcc09 | 5120 | } |
johnAlexander | 0:c523920bcc09 | 5121 | |
johnAlexander | 14:8320b5ff96fa | 5122 | return status; |
johnAlexander | 0:c523920bcc09 | 5123 | } |
johnAlexander | 0:c523920bcc09 | 5124 | |
johnAlexander | 0:c523920bcc09 | 5125 | |
johnAlexander | 14:8320b5ff96fa | 5126 | int VL53L0X::init_sensor(uint8_t new_addr) |
johnAlexander | 0:c523920bcc09 | 5127 | { |
johnAlexander | 14:8320b5ff96fa | 5128 | int status; |
johnAlexander | 14:8320b5ff96fa | 5129 | |
johnAlexander | 14:8320b5ff96fa | 5130 | vl53l0x_off(); |
johnAlexander | 14:8320b5ff96fa | 5131 | vl53l0x_on(); |
johnAlexander | 0:c523920bcc09 | 5132 | |
johnAlexander | 0:c523920bcc09 | 5133 | // status=VL53L0X_WaitDeviceBooted(Device); |
johnAlexander | 0:c523920bcc09 | 5134 | // if(status) |
johnAlexander | 0:c523920bcc09 | 5135 | // printf("WaitDeviceBooted fail\n\r"); |
johnAlexander | 14:8320b5ff96fa | 5136 | status = is_present(); |
johnAlexander | 14:8320b5ff96fa | 5137 | if (!status) { |
johnAlexander | 14:8320b5ff96fa | 5138 | status = init(&_my_device); |
johnAlexander | 14:8320b5ff96fa | 5139 | if (status != VL53L0X_ERROR_NONE) { |
johnAlexander | 14:8320b5ff96fa | 5140 | printf("Failed to init VL53L0X sensor!\n\r"); |
johnAlexander | 14:8320b5ff96fa | 5141 | return status; |
johnAlexander | 14:8320b5ff96fa | 5142 | } |
johnAlexander | 14:8320b5ff96fa | 5143 | |
johnAlexander | 14:8320b5ff96fa | 5144 | // deduce silicon version |
johnAlexander | 14:8320b5ff96fa | 5145 | status = vl53l0x_get_device_info(&_my_device, &_device_info); |
johnAlexander | 14:8320b5ff96fa | 5146 | |
johnAlexander | 14:8320b5ff96fa | 5147 | |
johnAlexander | 14:8320b5ff96fa | 5148 | status = prepare(); |
johnAlexander | 14:8320b5ff96fa | 5149 | if (status != VL53L0X_ERROR_NONE) { |
johnAlexander | 14:8320b5ff96fa | 5150 | printf("Failed to prepare VL53L0X!\n\r"); |
johnAlexander | 0:c523920bcc09 | 5151 | return status; |
johnAlexander | 14:8320b5ff96fa | 5152 | } |
johnAlexander | 14:8320b5ff96fa | 5153 | |
johnAlexander | 14:8320b5ff96fa | 5154 | if (new_addr != DEFAULT_DEVICE_ADDRESS) { |
johnAlexander | 14:8320b5ff96fa | 5155 | status = set_device_address(new_addr); |
johnAlexander | 14:8320b5ff96fa | 5156 | if (status) { |
johnAlexander | 14:8320b5ff96fa | 5157 | printf("Failed to change I2C address!\n\r"); |
johnAlexander | 14:8320b5ff96fa | 5158 | return status; |
johnAlexander | 14:8320b5ff96fa | 5159 | } |
johnAlexander | 14:8320b5ff96fa | 5160 | } else { |
johnAlexander | 14:8320b5ff96fa | 5161 | printf("Invalid new address!\n\r"); |
johnAlexander | 14:8320b5ff96fa | 5162 | return VL53L0X_ERROR_INVALID_PARAMS; |
johnAlexander | 14:8320b5ff96fa | 5163 | } |
johnAlexander | 14:8320b5ff96fa | 5164 | } |
johnAlexander | 14:8320b5ff96fa | 5165 | return status; |
johnAlexander | 0:c523920bcc09 | 5166 | } |
johnAlexander | 12:f6e2bad00dc7 | 5167 | |
johnAlexander | 14:8320b5ff96fa | 5168 | int VL53L0X::range_meas_int_continuous_mode(void (*fptr)(void)) |
johnAlexander | 12:f6e2bad00dc7 | 5169 | { |
johnAlexander | 14:8320b5ff96fa | 5170 | int status, clr_status; |
johnAlexander | 14:8320b5ff96fa | 5171 | |
johnAlexander | 14:8320b5ff96fa | 5172 | status = vl53l0x_stop_measurement(_device); // it is safer to do this while sensor is stopped |
johnAlexander | 13:615f7e38568c | 5173 | |
johnAlexander | 13:615f7e38568c | 5174 | // status = VL53L0X_SetInterruptThresholds(Device, VL53L0X_DEVICEMODE_CONTINUOUS_RANGING, 0, 300); |
johnAlexander | 13:615f7e38568c | 5175 | |
johnAlexander | 14:8320b5ff96fa | 5176 | status = vl53l0x_set_gpio_config(_device, 0, VL53L0X_DEVICEMODE_CONTINUOUS_RANGING, |
johnAlexander | 14:8320b5ff96fa | 5177 | VL53L0X_REG_SYSTEM_INTERRUPT_GPIO_NEW_SAMPLE_READY, |
johnAlexander | 14:8320b5ff96fa | 5178 | VL53L0X_INTERRUPTPOLARITY_HIGH); |
johnAlexander | 14:8320b5ff96fa | 5179 | |
johnAlexander | 14:8320b5ff96fa | 5180 | if (!status) { |
johnAlexander | 14:8320b5ff96fa | 5181 | attach_interrupt_measure_detection_irq(fptr); |
johnAlexander | 14:8320b5ff96fa | 5182 | enable_interrupt_measure_detection_irq(); |
johnAlexander | 14:8320b5ff96fa | 5183 | } |
johnAlexander | 14:8320b5ff96fa | 5184 | |
johnAlexander | 14:8320b5ff96fa | 5185 | clr_status = clear_interrupt(VL53L0X_REG_RESULT_INTERRUPT_STATUS | VL53L0X_REG_RESULT_RANGE_STATUS); |
johnAlexander | 14:8320b5ff96fa | 5186 | if (clr_status) |
johnAlexander | 14:8320b5ff96fa | 5187 | VL53L0X_ErrLog("VL53L0X_ClearErrorInterrupt fail\r\n"); |
johnAlexander | 14:8320b5ff96fa | 5188 | |
johnAlexander | 14:8320b5ff96fa | 5189 | if (!status) { |
johnAlexander | 14:8320b5ff96fa | 5190 | status = range_start_continuous_mode(); |
johnAlexander | 14:8320b5ff96fa | 5191 | } |
johnAlexander | 14:8320b5ff96fa | 5192 | return status; |
johnAlexander | 12:f6e2bad00dc7 | 5193 | } |
johnAlexander | 0:c523920bcc09 | 5194 | |
johnAlexander | 0:c523920bcc09 | 5195 | |
johnAlexander | 14:8320b5ff96fa | 5196 | int VL53L0X::start_measurement(OperatingMode operating_mode, void (*fptr)(void)) |
johnAlexander | 0:c523920bcc09 | 5197 | { |
johnAlexander | 0:c523920bcc09 | 5198 | int Status = VL53L0X_ERROR_NONE; |
johnAlexander | 13:615f7e38568c | 5199 | int ClrStatus; |
johnAlexander | 0:c523920bcc09 | 5200 | |
johnAlexander | 0:c523920bcc09 | 5201 | uint8_t VhvSettings; |
johnAlexander | 0:c523920bcc09 | 5202 | uint8_t PhaseCal; |
johnAlexander | 0:c523920bcc09 | 5203 | // *** from mass market cube expansion v1.1, ranging with satellites. |
johnAlexander | 0:c523920bcc09 | 5204 | // default settings, for normal range. |
johnAlexander | 14:8320b5ff96fa | 5205 | FixPoint1616_t signalLimit = (FixPoint1616_t)(0.25 * 65536); |
johnAlexander | 14:8320b5ff96fa | 5206 | FixPoint1616_t sigmaLimit = (FixPoint1616_t)(18 * 65536); |
johnAlexander | 14:8320b5ff96fa | 5207 | uint32_t timingBudget = 33000; |
johnAlexander | 14:8320b5ff96fa | 5208 | uint8_t preRangeVcselPeriod = 14; |
johnAlexander | 14:8320b5ff96fa | 5209 | uint8_t finalRangeVcselPeriod = 10; |
johnAlexander | 14:8320b5ff96fa | 5210 | |
johnAlexander | 14:8320b5ff96fa | 5211 | if (operating_mode == range_continuous_interrupt) { |
johnAlexander | 14:8320b5ff96fa | 5212 | if (gpio1Int == NULL) { |
johnAlexander | 14:8320b5ff96fa | 5213 | printf("GPIO1 Error\r\n"); |
johnAlexander | 12:f6e2bad00dc7 | 5214 | return 1; |
johnAlexander | 12:f6e2bad00dc7 | 5215 | } |
johnAlexander | 13:615f7e38568c | 5216 | |
johnAlexander | 14:8320b5ff96fa | 5217 | Status = vl53l0x_stop_measurement(_device); // it is safer to do this while sensor is stopped |
johnAlexander | 13:615f7e38568c | 5218 | |
johnAlexander | 13:615f7e38568c | 5219 | // Status = VL53L0X_SetInterruptThresholds(Device, VL53L0X_DEVICEMODE_CONTINUOUS_RANGING, 0, 300); |
johnAlexander | 13:615f7e38568c | 5220 | |
johnAlexander | 14:8320b5ff96fa | 5221 | Status = vl53l0x_set_gpio_config(_device, 0, VL53L0X_DEVICEMODE_CONTINUOUS_RANGING, |
johnAlexander | 14:8320b5ff96fa | 5222 | VL53L0X_REG_SYSTEM_INTERRUPT_GPIO_NEW_SAMPLE_READY, |
johnAlexander | 14:8320b5ff96fa | 5223 | VL53L0X_INTERRUPTPOLARITY_HIGH); |
johnAlexander | 14:8320b5ff96fa | 5224 | |
johnAlexander | 14:8320b5ff96fa | 5225 | if (Status == VL53L0X_ERROR_NONE) { |
johnAlexander | 14:8320b5ff96fa | 5226 | attach_interrupt_measure_detection_irq(fptr); |
johnAlexander | 14:8320b5ff96fa | 5227 | enable_interrupt_measure_detection_irq(); |
johnAlexander | 12:f6e2bad00dc7 | 5228 | } |
johnAlexander | 13:615f7e38568c | 5229 | |
johnAlexander | 14:8320b5ff96fa | 5230 | ClrStatus = clear_interrupt(VL53L0X_REG_RESULT_INTERRUPT_STATUS | VL53L0X_REG_RESULT_RANGE_STATUS); |
johnAlexander | 14:8320b5ff96fa | 5231 | if (ClrStatus) |
johnAlexander | 14:8320b5ff96fa | 5232 | VL53L0X_ErrLog("VL53L0X_ClearErrorInterrupt fail\r\n"); |
johnAlexander | 14:8320b5ff96fa | 5233 | |
johnAlexander | 14:8320b5ff96fa | 5234 | if (Status == VL53L0X_ERROR_NONE) { |
johnAlexander | 14:8320b5ff96fa | 5235 | Status = vl53l0x_set_device_mode(_device, VL53L0X_DEVICEMODE_CONTINUOUS_RANGING); // Setup in continuous ranging mode |
johnAlexander | 13:615f7e38568c | 5236 | } |
johnAlexander | 13:615f7e38568c | 5237 | |
johnAlexander | 14:8320b5ff96fa | 5238 | if (Status == VL53L0X_ERROR_NONE) { |
johnAlexander | 14:8320b5ff96fa | 5239 | Status = vl53l0x_start_measurement(_device); |
johnAlexander | 13:615f7e38568c | 5240 | } |
johnAlexander | 12:f6e2bad00dc7 | 5241 | } |
johnAlexander | 0:c523920bcc09 | 5242 | |
johnAlexander | 14:8320b5ff96fa | 5243 | if (operating_mode == range_single_shot_polling) { |
johnAlexander | 0:c523920bcc09 | 5244 | // singelshot, polled ranging |
johnAlexander | 14:8320b5ff96fa | 5245 | if (Status == VL53L0X_ERROR_NONE) { |
johnAlexander | 0:c523920bcc09 | 5246 | // no need to do this when we use VL53L0X_PerformSingleRangingMeasurement |
johnAlexander | 14:8320b5ff96fa | 5247 | Status = vl53l0x_set_device_mode(_device, VL53L0X_DEVICEMODE_SINGLE_RANGING); // Setup in single ranging mode |
johnAlexander | 0:c523920bcc09 | 5248 | } |
johnAlexander | 0:c523920bcc09 | 5249 | |
johnAlexander | 0:c523920bcc09 | 5250 | // Enable/Disable Sigma and Signal check |
johnAlexander | 0:c523920bcc09 | 5251 | if (Status == VL53L0X_ERROR_NONE) { |
johnAlexander | 14:8320b5ff96fa | 5252 | Status = vl53l0x_set_limit_check_enable(_device, |
johnAlexander | 14:8320b5ff96fa | 5253 | VL53L0X_CHECKENABLE_SIGMA_FINAL_RANGE, 1); |
johnAlexander | 0:c523920bcc09 | 5254 | } |
johnAlexander | 0:c523920bcc09 | 5255 | if (Status == VL53L0X_ERROR_NONE) { |
johnAlexander | 14:8320b5ff96fa | 5256 | Status = vl53l0x_set_limit_check_enable(_device, |
johnAlexander | 14:8320b5ff96fa | 5257 | VL53L0X_CHECKENABLE_SIGNAL_RATE_FINAL_RANGE, 1); |
johnAlexander | 0:c523920bcc09 | 5258 | } |
johnAlexander | 0:c523920bcc09 | 5259 | |
johnAlexander | 0:c523920bcc09 | 5260 | // *** from mass market cube expansion v1.1, ranging with satellites. |
johnAlexander | 14:8320b5ff96fa | 5261 | /* Ranging configuration */ |
johnAlexander | 0:c523920bcc09 | 5262 | //* |
johnAlexander | 0:c523920bcc09 | 5263 | // switch(rangingConfig) { |
johnAlexander | 0:c523920bcc09 | 5264 | // case LONG_RANGE: |
johnAlexander | 14:8320b5ff96fa | 5265 | signalLimit = (FixPoint1616_t)(0.1 * 65536); |
johnAlexander | 14:8320b5ff96fa | 5266 | sigmaLimit = (FixPoint1616_t)(60 * 65536); |
johnAlexander | 14:8320b5ff96fa | 5267 | timingBudget = 33000; |
johnAlexander | 14:8320b5ff96fa | 5268 | preRangeVcselPeriod = 18; |
johnAlexander | 14:8320b5ff96fa | 5269 | finalRangeVcselPeriod = 14; |
johnAlexander | 14:8320b5ff96fa | 5270 | /* break; |
johnAlexander | 14:8320b5ff96fa | 5271 | case HIGH_ACCURACY: |
johnAlexander | 14:8320b5ff96fa | 5272 | signalLimit = (FixPoint1616_t)(0.25*65536); |
johnAlexander | 14:8320b5ff96fa | 5273 | sigmaLimit = (FixPoint1616_t)(18*65536); |
johnAlexander | 14:8320b5ff96fa | 5274 | timingBudget = 200000; |
johnAlexander | 14:8320b5ff96fa | 5275 | preRangeVcselPeriod = 14; |
johnAlexander | 14:8320b5ff96fa | 5276 | finalRangeVcselPeriod = 10; |
johnAlexander | 14:8320b5ff96fa | 5277 | break; |
johnAlexander | 14:8320b5ff96fa | 5278 | case HIGH_SPEED: |
johnAlexander | 14:8320b5ff96fa | 5279 | signalLimit = (FixPoint1616_t)(0.25*65536); |
johnAlexander | 14:8320b5ff96fa | 5280 | sigmaLimit = (FixPoint1616_t)(32*65536); |
johnAlexander | 14:8320b5ff96fa | 5281 | timingBudget = 20000; |
johnAlexander | 14:8320b5ff96fa | 5282 | preRangeVcselPeriod = 14; |
johnAlexander | 14:8320b5ff96fa | 5283 | finalRangeVcselPeriod = 10; |
johnAlexander | 14:8320b5ff96fa | 5284 | break; |
johnAlexander | 14:8320b5ff96fa | 5285 | default: |
johnAlexander | 14:8320b5ff96fa | 5286 | debug_printf("Not Supported"); |
johnAlexander | 14:8320b5ff96fa | 5287 | } |
johnAlexander | 14:8320b5ff96fa | 5288 | */ |
johnAlexander | 0:c523920bcc09 | 5289 | |
johnAlexander | 0:c523920bcc09 | 5290 | if (Status == VL53L0X_ERROR_NONE) { |
johnAlexander | 14:8320b5ff96fa | 5291 | Status = vl53l0x_set_limit_check_value(_device, |
johnAlexander | 14:8320b5ff96fa | 5292 | VL53L0X_CHECKENABLE_SIGNAL_RATE_FINAL_RANGE, signalLimit); |
johnAlexander | 0:c523920bcc09 | 5293 | } |
johnAlexander | 0:c523920bcc09 | 5294 | |
johnAlexander | 0:c523920bcc09 | 5295 | if (Status == VL53L0X_ERROR_NONE) { |
johnAlexander | 14:8320b5ff96fa | 5296 | Status = vl53l0x_set_limit_check_value(_device, |
johnAlexander | 14:8320b5ff96fa | 5297 | VL53L0X_CHECKENABLE_SIGMA_FINAL_RANGE, sigmaLimit); |
johnAlexander | 14:8320b5ff96fa | 5298 | } |
johnAlexander | 14:8320b5ff96fa | 5299 | |
johnAlexander | 14:8320b5ff96fa | 5300 | if (Status == VL53L0X_ERROR_NONE) { |
johnAlexander | 14:8320b5ff96fa | 5301 | Status = vl53l0x_set_measurement_timing_budget_micro_seconds(_device, timingBudget); |
johnAlexander | 14:8320b5ff96fa | 5302 | } |
johnAlexander | 0:c523920bcc09 | 5303 | |
johnAlexander | 0:c523920bcc09 | 5304 | if (Status == VL53L0X_ERROR_NONE) { |
johnAlexander | 14:8320b5ff96fa | 5305 | Status = vl53l0x_set_vcsel_pulse_period(_device, |
johnAlexander | 14:8320b5ff96fa | 5306 | VL53L0X_VCSEL_PERIOD_PRE_RANGE, preRangeVcselPeriod); |
johnAlexander | 14:8320b5ff96fa | 5307 | } |
johnAlexander | 0:c523920bcc09 | 5308 | |
johnAlexander | 0:c523920bcc09 | 5309 | if (Status == VL53L0X_ERROR_NONE) { |
johnAlexander | 14:8320b5ff96fa | 5310 | Status = vl53l0x_set_vcsel_pulse_period(_device, |
johnAlexander | 14:8320b5ff96fa | 5311 | VL53L0X_VCSEL_PERIOD_FINAL_RANGE, finalRangeVcselPeriod); |
johnAlexander | 14:8320b5ff96fa | 5312 | } |
johnAlexander | 14:8320b5ff96fa | 5313 | |
johnAlexander | 14:8320b5ff96fa | 5314 | if (Status == VL53L0X_ERROR_NONE) { |
johnAlexander | 14:8320b5ff96fa | 5315 | Status = vl53l0x_perform_ref_calibration(_device, &VhvSettings, &PhaseCal); |
johnAlexander | 14:8320b5ff96fa | 5316 | } |
johnAlexander | 0:c523920bcc09 | 5317 | |
johnAlexander | 0:c523920bcc09 | 5318 | } |
johnAlexander | 0:c523920bcc09 | 5319 | |
johnAlexander | 14:8320b5ff96fa | 5320 | if (operating_mode == range_continuous_polling) { |
johnAlexander | 14:8320b5ff96fa | 5321 | if (Status == VL53L0X_ERROR_NONE) { |
johnAlexander | 14:8320b5ff96fa | 5322 | printf("Call of VL53L0X_SetDeviceMode\n"); |
johnAlexander | 14:8320b5ff96fa | 5323 | Status = vl53l0x_set_device_mode(_device, VL53L0X_DEVICEMODE_CONTINUOUS_RANGING); // Setup in continuous ranging mode |
johnAlexander | 0:c523920bcc09 | 5324 | } |
johnAlexander | 0:c523920bcc09 | 5325 | |
johnAlexander | 14:8320b5ff96fa | 5326 | if (Status == VL53L0X_ERROR_NONE) { |
johnAlexander | 14:8320b5ff96fa | 5327 | printf("Call of VL53L0X_StartMeasurement\n"); |
johnAlexander | 14:8320b5ff96fa | 5328 | Status = vl53l0x_start_measurement(_device); |
johnAlexander | 0:c523920bcc09 | 5329 | } |
johnAlexander | 0:c523920bcc09 | 5330 | } |
johnAlexander | 0:c523920bcc09 | 5331 | |
johnAlexander | 0:c523920bcc09 | 5332 | return Status; |
johnAlexander | 0:c523920bcc09 | 5333 | } |
johnAlexander | 14:8320b5ff96fa | 5334 | |
johnAlexander | 14:8320b5ff96fa | 5335 | |
johnAlexander | 14:8320b5ff96fa | 5336 | int VL53L0X::get_measurement(OperatingMode operating_mode, VL53L0X_RangingMeasurementData_t *p_data) |
johnAlexander | 0:c523920bcc09 | 5337 | { |
johnAlexander | 0:c523920bcc09 | 5338 | int Status = VL53L0X_ERROR_NONE; |
johnAlexander | 0:c523920bcc09 | 5339 | |
johnAlexander | 14:8320b5ff96fa | 5340 | if (operating_mode == range_single_shot_polling) { |
johnAlexander | 14:8320b5ff96fa | 5341 | Status = vl53l0x_perform_single_ranging_measurement(_device, p_data); |
johnAlexander | 0:c523920bcc09 | 5342 | } |
johnAlexander | 0:c523920bcc09 | 5343 | |
johnAlexander | 14:8320b5ff96fa | 5344 | if (operating_mode == range_continuous_polling) { |
johnAlexander | 14:8320b5ff96fa | 5345 | if (Status == VL53L0X_ERROR_NONE) |
johnAlexander | 14:8320b5ff96fa | 5346 | Status = vl53l0x_measurement_poll_for_completion(_device); |
johnAlexander | 14:8320b5ff96fa | 5347 | |
johnAlexander | 14:8320b5ff96fa | 5348 | if (Status == VL53L0X_ERROR_NONE) { |
johnAlexander | 14:8320b5ff96fa | 5349 | Status = vl53l0x_get_ranging_measurement_data(_device, p_data); |
johnAlexander | 14:8320b5ff96fa | 5350 | |
johnAlexander | 14:8320b5ff96fa | 5351 | // Clear the interrupt |
johnAlexander | 14:8320b5ff96fa | 5352 | vl53l0x_clear_interrupt_mask(_device, VL53L0X_REG_SYSTEM_INTERRUPT_GPIO_NEW_SAMPLE_READY); |
johnAlexander | 14:8320b5ff96fa | 5353 | vl53l0x_polling_delay(_device); |
johnAlexander | 0:c523920bcc09 | 5354 | } |
johnAlexander | 0:c523920bcc09 | 5355 | } |
johnAlexander | 0:c523920bcc09 | 5356 | |
johnAlexander | 14:8320b5ff96fa | 5357 | if (operating_mode == range_continuous_interrupt) { |
johnAlexander | 14:8320b5ff96fa | 5358 | Status = vl53l0x_get_ranging_measurement_data(_device, p_data); |
johnAlexander | 14:8320b5ff96fa | 5359 | vl53l0x_clear_interrupt_mask(_device, VL53L0X_REG_SYSTEM_INTERRUPT_CLEAR | VL53L0X_REG_RESULT_INTERRUPT_STATUS); |
johnAlexander | 14:8320b5ff96fa | 5360 | } |
johnAlexander | 0:c523920bcc09 | 5361 | |
johnAlexander | 0:c523920bcc09 | 5362 | return Status; |
johnAlexander | 0:c523920bcc09 | 5363 | } |
johnAlexander | 14:8320b5ff96fa | 5364 | |
johnAlexander | 14:8320b5ff96fa | 5365 | |
johnAlexander | 14:8320b5ff96fa | 5366 | int VL53L0X::stop_measurement(OperatingMode operating_mode) |
johnAlexander | 0:c523920bcc09 | 5367 | { |
johnAlexander | 0:c523920bcc09 | 5368 | int status = VL53L0X_ERROR_NONE; |
johnAlexander | 0:c523920bcc09 | 5369 | |
johnAlexander | 0:c523920bcc09 | 5370 | |
johnAlexander | 14:8320b5ff96fa | 5371 | // don't need to stop for a singleshot range! |
johnAlexander | 14:8320b5ff96fa | 5372 | if (operating_mode == range_single_shot_polling) { |
johnAlexander | 0:c523920bcc09 | 5373 | } |
johnAlexander | 0:c523920bcc09 | 5374 | |
johnAlexander | 14:8320b5ff96fa | 5375 | if (operating_mode == range_continuous_interrupt || operating_mode == range_continuous_polling) { |
johnAlexander | 14:8320b5ff96fa | 5376 | // continuous mode |
johnAlexander | 14:8320b5ff96fa | 5377 | if (status == VL53L0X_ERROR_NONE) { |
johnAlexander | 14:8320b5ff96fa | 5378 | printf("Call of VL53L0X_StopMeasurement\n"); |
johnAlexander | 14:8320b5ff96fa | 5379 | status = vl53l0x_stop_measurement(_device); |
johnAlexander | 0:c523920bcc09 | 5380 | } |
johnAlexander | 0:c523920bcc09 | 5381 | |
johnAlexander | 14:8320b5ff96fa | 5382 | if (status == VL53L0X_ERROR_NONE) { |
johnAlexander | 14:8320b5ff96fa | 5383 | printf("Wait Stop to be competed\n"); |
johnAlexander | 14:8320b5ff96fa | 5384 | status = wait_stop_completed(_device); |
johnAlexander | 0:c523920bcc09 | 5385 | } |
johnAlexander | 0:c523920bcc09 | 5386 | |
johnAlexander | 14:8320b5ff96fa | 5387 | if (status == VL53L0X_ERROR_NONE) |
johnAlexander | 14:8320b5ff96fa | 5388 | status = vl53l0x_clear_interrupt_mask(_device, |
johnAlexander | 14:8320b5ff96fa | 5389 | VL53L0X_REG_SYSTEM_INTERRUPT_GPIO_NEW_SAMPLE_READY); |
johnAlexander | 0:c523920bcc09 | 5390 | } |
johnAlexander | 0:c523920bcc09 | 5391 | |
johnAlexander | 0:c523920bcc09 | 5392 | return status; |
johnAlexander | 0:c523920bcc09 | 5393 | } |
johnAlexander | 0:c523920bcc09 | 5394 | |
johnAlexander | 0:c523920bcc09 | 5395 | |
johnAlexander | 14:8320b5ff96fa | 5396 | int VL53L0X::handle_irq(OperatingMode operating_mode, VL53L0X_RangingMeasurementData_t *data) |
johnAlexander | 0:c523920bcc09 | 5397 | { |
johnAlexander | 14:8320b5ff96fa | 5398 | int status; |
johnAlexander | 14:8320b5ff96fa | 5399 | status = get_measurement(operating_mode, data); |
johnAlexander | 14:8320b5ff96fa | 5400 | enable_interrupt_measure_detection_irq(); |
johnAlexander | 14:8320b5ff96fa | 5401 | return status; |
johnAlexander | 0:c523920bcc09 | 5402 | } |
johnAlexander | 14:8320b5ff96fa | 5403 | |
johnAlexander | 14:8320b5ff96fa | 5404 | |
johnAlexander | 14:8320b5ff96fa | 5405 | |
johnAlexander | 0:c523920bcc09 | 5406 | /******************************************************************************/ |
johnAlexander | 0:c523920bcc09 | 5407 | |
johnAlexander | 0:c523920bcc09 | 5408 | |
johnAlexander | 0:c523920bcc09 | 5409 | |
johnAlexander | 9:367d1f390cb2 | 5410 | |
johnAlexander | 9:367d1f390cb2 | 5411 |