Initial release.

Dependencies:   X_NUCLEO_COMMON ST_INTERFACES

Dependents:   X_NUCLEO_53L1A1_mbed X_NUCLEO_53L1A1_mbed VL53L1X_Ranging_With_Standalone_Satellite_MbedOS X_NUCLEO_53L1A1

Committer:
johnAlexander
Date:
Wed Jul 24 10:36:51 2019 +0000
Revision:
7:6d3ab15363a2
Child:
8:744e8b1b9837
Updated for mbed coding style guidelines.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
johnAlexander 7:6d3ab15363a2 1 /**
johnAlexander 7:6d3ab15363a2 2 ******************************************************************************
johnAlexander 7:6d3ab15363a2 3 * @file Vl53l1x_Class.cpp
johnAlexander 7:6d3ab15363a2 4 * @author JS
johnAlexander 7:6d3ab15363a2 5 * @version V0.0.1
johnAlexander 7:6d3ab15363a2 6 * @date 15-January-2019
johnAlexander 7:6d3ab15363a2 7 * @brief Implementation file for the VL53L1 sensor component driver class
johnAlexander 7:6d3ab15363a2 8 ******************************************************************************
johnAlexander 7:6d3ab15363a2 9 * @attention
johnAlexander 7:6d3ab15363a2 10 *
johnAlexander 7:6d3ab15363a2 11 * <h2><center>&copy; COPYRIGHT(c) 2019 STMicroelectronics</center></h2>
johnAlexander 7:6d3ab15363a2 12 *
johnAlexander 7:6d3ab15363a2 13 * Redistribution and use in source and binary forms, with or without modification,
johnAlexander 7:6d3ab15363a2 14 * are permitted provided that the following conditions are met:
johnAlexander 7:6d3ab15363a2 15 * 1. Redistributions of source code must retain the above copyright notice,
johnAlexander 7:6d3ab15363a2 16 * this list of conditions and the following disclaimer.
johnAlexander 7:6d3ab15363a2 17 * 2. Redistributions in binary form must reproduce the above copyright notice,
johnAlexander 7:6d3ab15363a2 18 * this list of conditions and the following disclaimer in the documentation
johnAlexander 7:6d3ab15363a2 19 * and/or other materials provided with the distribution.
johnAlexander 7:6d3ab15363a2 20 * 3. Neither the name of STMicroelectronics nor the names of its contributors
johnAlexander 7:6d3ab15363a2 21 * may be used to endorse or promote products derived from this software
johnAlexander 7:6d3ab15363a2 22 * without specific prior written permission.
johnAlexander 7:6d3ab15363a2 23 *
johnAlexander 7:6d3ab15363a2 24 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
johnAlexander 7:6d3ab15363a2 25 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
johnAlexander 7:6d3ab15363a2 26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
johnAlexander 7:6d3ab15363a2 27 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
johnAlexander 7:6d3ab15363a2 28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
johnAlexander 7:6d3ab15363a2 29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
johnAlexander 7:6d3ab15363a2 30 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
johnAlexander 7:6d3ab15363a2 31 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
johnAlexander 7:6d3ab15363a2 32 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
johnAlexander 7:6d3ab15363a2 33 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
johnAlexander 7:6d3ab15363a2 34 *
johnAlexander 7:6d3ab15363a2 35 ******************************************************************************
johnAlexander 7:6d3ab15363a2 36 */
johnAlexander 7:6d3ab15363a2 37
johnAlexander 7:6d3ab15363a2 38 /* Includes */
johnAlexander 7:6d3ab15363a2 39 #include <stdlib.h>
johnAlexander 7:6d3ab15363a2 40 #include "VL53L1X_Class.h"
johnAlexander 7:6d3ab15363a2 41
johnAlexander 7:6d3ab15363a2 42
johnAlexander 7:6d3ab15363a2 43 #define ALGO__PART_TO_PART_RANGE_OFFSET_MM 0x001E
johnAlexander 7:6d3ab15363a2 44 #define MM_CONFIG__INNER_OFFSET_MM 0x0020
johnAlexander 7:6d3ab15363a2 45 #define MM_CONFIG__OUTER_OFFSET_MM 0x0022
johnAlexander 7:6d3ab15363a2 46
johnAlexander 7:6d3ab15363a2 47 #include "vl53l1x_configuration.h"
johnAlexander 7:6d3ab15363a2 48
johnAlexander 7:6d3ab15363a2 49
johnAlexander 7:6d3ab15363a2 50 VL53L1X_ERROR VL53L1X::vl53l1x_get_sw_version(VL53L1X_Version_t *pVersion)
johnAlexander 7:6d3ab15363a2 51 {
johnAlexander 7:6d3ab15363a2 52 VL53L1X_ERROR Status = 0;
johnAlexander 7:6d3ab15363a2 53
johnAlexander 7:6d3ab15363a2 54 pVersion->major = VL53L1X_IMPLEMENTATION_VER_MAJOR;
johnAlexander 7:6d3ab15363a2 55 pVersion->minor = VL53L1X_IMPLEMENTATION_VER_MINOR;
johnAlexander 7:6d3ab15363a2 56 pVersion->build = VL53L1X_IMPLEMENTATION_VER_SUB;
johnAlexander 7:6d3ab15363a2 57 pVersion->revision = VL53L1X_IMPLEMENTATION_VER_REVISION;
johnAlexander 7:6d3ab15363a2 58 return Status;
johnAlexander 7:6d3ab15363a2 59 }
johnAlexander 7:6d3ab15363a2 60
johnAlexander 7:6d3ab15363a2 61 VL53L1X_ERROR VL53L1X::vl53l1x_set_i2c_address(uint8_t new_address)
johnAlexander 7:6d3ab15363a2 62 {
johnAlexander 7:6d3ab15363a2 63 VL53L1X_ERROR status = 0;
johnAlexander 7:6d3ab15363a2 64
johnAlexander 7:6d3ab15363a2 65 status = vl53l1_wr_byte(Device, VL53L1_I2C_SLAVE__DEVICE_ADDRESS, new_address >> 1);
johnAlexander 7:6d3ab15363a2 66 Device->I2cDevAddr = new_address;
johnAlexander 7:6d3ab15363a2 67 return status;
johnAlexander 7:6d3ab15363a2 68 }
johnAlexander 7:6d3ab15363a2 69
johnAlexander 7:6d3ab15363a2 70 int VL53L1X::init_sensor(uint8_t new_addr)
johnAlexander 7:6d3ab15363a2 71 {
johnAlexander 7:6d3ab15363a2 72 Device->I2cDevAddr = new_addr;
johnAlexander 7:6d3ab15363a2 73 int status = 0;
johnAlexander 7:6d3ab15363a2 74 vl53l1_off();
johnAlexander 7:6d3ab15363a2 75 vl53l1_on();
johnAlexander 7:6d3ab15363a2 76
johnAlexander 7:6d3ab15363a2 77 status = is_present();
johnAlexander 7:6d3ab15363a2 78 if (!status) {
johnAlexander 7:6d3ab15363a2 79 printf("Failed to init VL53L0X sensor!\n\r");
johnAlexander 7:6d3ab15363a2 80 return status;
johnAlexander 7:6d3ab15363a2 81 }
johnAlexander 7:6d3ab15363a2 82 return status;
johnAlexander 7:6d3ab15363a2 83 }
johnAlexander 7:6d3ab15363a2 84
johnAlexander 7:6d3ab15363a2 85
johnAlexander 7:6d3ab15363a2 86 VL53L1X_ERROR VL53L1X::vl53l1x_sensor_init()
johnAlexander 7:6d3ab15363a2 87 {
johnAlexander 7:6d3ab15363a2 88 VL53L1X_ERROR status = 0;
johnAlexander 7:6d3ab15363a2 89 uint8_t Addr = 0x00;
johnAlexander 7:6d3ab15363a2 90
johnAlexander 7:6d3ab15363a2 91 for (Addr = 0x2D; Addr <= 0x87; Addr++){
johnAlexander 7:6d3ab15363a2 92 status = vl53l1_wr_byte(Device, Addr, VL51L1X_DEFAULT_CONFIGURATION[Addr - 0x2D]);
johnAlexander 7:6d3ab15363a2 93 if (status != 0)
johnAlexander 7:6d3ab15363a2 94 {
johnAlexander 7:6d3ab15363a2 95 printf("Writing config failed - %d\r\n", status);
johnAlexander 7:6d3ab15363a2 96 }
johnAlexander 7:6d3ab15363a2 97 }
johnAlexander 7:6d3ab15363a2 98
johnAlexander 7:6d3ab15363a2 99 uint16_t sensorID= 0;
johnAlexander 7:6d3ab15363a2 100 status = vl53l1x_get_sensor_id(&sensorID);
johnAlexander 7:6d3ab15363a2 101 printf("Sensor id is - %d (%X)\r\n", sensorID, sensorID);
johnAlexander 7:6d3ab15363a2 102
johnAlexander 7:6d3ab15363a2 103 status = vl53l1x_start_ranging();
johnAlexander 7:6d3ab15363a2 104 if (status != 0)
johnAlexander 7:6d3ab15363a2 105 {
johnAlexander 7:6d3ab15363a2 106 printf("start ranging failed - %d\r\n", status);
johnAlexander 7:6d3ab15363a2 107 }
johnAlexander 7:6d3ab15363a2 108
johnAlexander 7:6d3ab15363a2 109 status = vl53l1x_clear_interrupt();
johnAlexander 7:6d3ab15363a2 110 status = vl53l1x_stop_ranging();
johnAlexander 7:6d3ab15363a2 111 status = vl53l1_wr_byte(Device, VL53L1_VHV_CONFIG__TIMEOUT_MACROP_LOOP_BOUND, 0x09); /* two bounds VHV */
johnAlexander 7:6d3ab15363a2 112 status = vl53l1_wr_byte(Device, 0x0B, 0); /* start VHV from the previous temperature */
johnAlexander 7:6d3ab15363a2 113 return status;
johnAlexander 7:6d3ab15363a2 114 }
johnAlexander 7:6d3ab15363a2 115
johnAlexander 7:6d3ab15363a2 116
johnAlexander 7:6d3ab15363a2 117 VL53L1X_ERROR VL53L1X::vl53l1x_clear_interrupt()
johnAlexander 7:6d3ab15363a2 118 {
johnAlexander 7:6d3ab15363a2 119 VL53L1X_ERROR status = 0;
johnAlexander 7:6d3ab15363a2 120
johnAlexander 7:6d3ab15363a2 121 status = vl53l1_wr_byte(Device, SYSTEM__INTERRUPT_CLEAR, 0x01);
johnAlexander 7:6d3ab15363a2 122 return status;
johnAlexander 7:6d3ab15363a2 123 }
johnAlexander 7:6d3ab15363a2 124
johnAlexander 7:6d3ab15363a2 125
johnAlexander 7:6d3ab15363a2 126 VL53L1X_ERROR VL53L1X::vl53l1x_set_interrupt_polarity(uint8_t NewPolarity)
johnAlexander 7:6d3ab15363a2 127 {
johnAlexander 7:6d3ab15363a2 128 uint8_t Temp;
johnAlexander 7:6d3ab15363a2 129 VL53L1X_ERROR status = 0;
johnAlexander 7:6d3ab15363a2 130
johnAlexander 7:6d3ab15363a2 131 status = vl53l1_rd_byte(Device, GPIO_HV_MUX__CTRL, &Temp);
johnAlexander 7:6d3ab15363a2 132 Temp = Temp & 0xEF;
johnAlexander 7:6d3ab15363a2 133 status = vl53l1_wr_byte(Device, GPIO_HV_MUX__CTRL, Temp | (!(NewPolarity & 1)) << 4);
johnAlexander 7:6d3ab15363a2 134 return status;
johnAlexander 7:6d3ab15363a2 135 }
johnAlexander 7:6d3ab15363a2 136
johnAlexander 7:6d3ab15363a2 137
johnAlexander 7:6d3ab15363a2 138
johnAlexander 7:6d3ab15363a2 139 VL53L1X_ERROR VL53L1X::vl53l1x_get_interrupt_polarity(uint8_t *pInterruptPolarity)
johnAlexander 7:6d3ab15363a2 140 {
johnAlexander 7:6d3ab15363a2 141 uint8_t Temp;
johnAlexander 7:6d3ab15363a2 142 VL53L1X_ERROR status = 0;
johnAlexander 7:6d3ab15363a2 143
johnAlexander 7:6d3ab15363a2 144 status = vl53l1_rd_byte(Device, GPIO_HV_MUX__CTRL, &Temp);
johnAlexander 7:6d3ab15363a2 145 Temp = Temp & 0x10;
johnAlexander 7:6d3ab15363a2 146 *pInterruptPolarity = !(Temp>>4);
johnAlexander 7:6d3ab15363a2 147 return status;
johnAlexander 7:6d3ab15363a2 148 }
johnAlexander 7:6d3ab15363a2 149
johnAlexander 7:6d3ab15363a2 150
johnAlexander 7:6d3ab15363a2 151
johnAlexander 7:6d3ab15363a2 152 VL53L1X_ERROR VL53L1X::vl53l1x_start_ranging()
johnAlexander 7:6d3ab15363a2 153 {
johnAlexander 7:6d3ab15363a2 154 VL53L1X_ERROR status = 0;
johnAlexander 7:6d3ab15363a2 155
johnAlexander 7:6d3ab15363a2 156 status = vl53l1_wr_byte(Device, SYSTEM__MODE_START, 0x40); /* Enable VL53L1X */
johnAlexander 7:6d3ab15363a2 157 return status;
johnAlexander 7:6d3ab15363a2 158 }
johnAlexander 7:6d3ab15363a2 159
johnAlexander 7:6d3ab15363a2 160 VL53L1X_ERROR VL53L1X::vl53l1x_stop_ranging()
johnAlexander 7:6d3ab15363a2 161 {
johnAlexander 7:6d3ab15363a2 162 VL53L1X_ERROR status = 0;
johnAlexander 7:6d3ab15363a2 163
johnAlexander 7:6d3ab15363a2 164 status = vl53l1_wr_byte(Device, SYSTEM__MODE_START, 0x00); /* Disable VL53L1X */
johnAlexander 7:6d3ab15363a2 165 return status;
johnAlexander 7:6d3ab15363a2 166 }
johnAlexander 7:6d3ab15363a2 167
johnAlexander 7:6d3ab15363a2 168
johnAlexander 7:6d3ab15363a2 169
johnAlexander 7:6d3ab15363a2 170 VL53L1X_ERROR VL53L1X::vl53l1x_check_for_data_ready(uint8_t *isDataReady)
johnAlexander 7:6d3ab15363a2 171 {
johnAlexander 7:6d3ab15363a2 172 uint8_t Temp;
johnAlexander 7:6d3ab15363a2 173 uint8_t IntPol;
johnAlexander 7:6d3ab15363a2 174 VL53L1X_ERROR status = 0;
johnAlexander 7:6d3ab15363a2 175
johnAlexander 7:6d3ab15363a2 176 status = vl53l1x_get_interrupt_polarity(&IntPol);
johnAlexander 7:6d3ab15363a2 177 status = vl53l1_rd_byte(Device, GPIO__TIO_HV_STATUS, &Temp);
johnAlexander 7:6d3ab15363a2 178 /* Read in the register to check if a new value is available */
johnAlexander 7:6d3ab15363a2 179 if (status == 0){
johnAlexander 7:6d3ab15363a2 180 if ((Temp & 1) == IntPol)
johnAlexander 7:6d3ab15363a2 181 *isDataReady = 1;
johnAlexander 7:6d3ab15363a2 182 else
johnAlexander 7:6d3ab15363a2 183 *isDataReady = 0;
johnAlexander 7:6d3ab15363a2 184 }
johnAlexander 7:6d3ab15363a2 185 return status;
johnAlexander 7:6d3ab15363a2 186 }
johnAlexander 7:6d3ab15363a2 187
johnAlexander 7:6d3ab15363a2 188
johnAlexander 7:6d3ab15363a2 189 VL53L1X_ERROR VL53L1X::vl53l1x_set_timing_budget_in_ms(uint16_t TimingBudgetInMs)
johnAlexander 7:6d3ab15363a2 190 {
johnAlexander 7:6d3ab15363a2 191 uint16_t DM;
johnAlexander 7:6d3ab15363a2 192 VL53L1X_ERROR status=0;
johnAlexander 7:6d3ab15363a2 193
johnAlexander 7:6d3ab15363a2 194 status = vl53l1x_get_distance_mode(&DM);
johnAlexander 7:6d3ab15363a2 195 if (DM == 0)
johnAlexander 7:6d3ab15363a2 196 return 1;
johnAlexander 7:6d3ab15363a2 197 else if (DM == 1) { /* Short DistanceMode */
johnAlexander 7:6d3ab15363a2 198 switch (TimingBudgetInMs) {
johnAlexander 7:6d3ab15363a2 199 case 15: /* only available in short distance mode */
johnAlexander 7:6d3ab15363a2 200 vl53l1_wr_word(Device, RANGE_CONFIG__TIMEOUT_MACROP_A_HI,
johnAlexander 7:6d3ab15363a2 201 0x01D);
johnAlexander 7:6d3ab15363a2 202 vl53l1_wr_word(Device, RANGE_CONFIG__TIMEOUT_MACROP_B_HI,
johnAlexander 7:6d3ab15363a2 203 0x0027);
johnAlexander 7:6d3ab15363a2 204 break;
johnAlexander 7:6d3ab15363a2 205 case 20:
johnAlexander 7:6d3ab15363a2 206 vl53l1_wr_word(Device, RANGE_CONFIG__TIMEOUT_MACROP_A_HI,
johnAlexander 7:6d3ab15363a2 207 0x0051);
johnAlexander 7:6d3ab15363a2 208 vl53l1_wr_word(Device, RANGE_CONFIG__TIMEOUT_MACROP_B_HI,
johnAlexander 7:6d3ab15363a2 209 0x006E);
johnAlexander 7:6d3ab15363a2 210 break;
johnAlexander 7:6d3ab15363a2 211 case 33:
johnAlexander 7:6d3ab15363a2 212 vl53l1_wr_word(Device, RANGE_CONFIG__TIMEOUT_MACROP_A_HI,
johnAlexander 7:6d3ab15363a2 213 0x00D6);
johnAlexander 7:6d3ab15363a2 214 vl53l1_wr_word(Device, RANGE_CONFIG__TIMEOUT_MACROP_B_HI,
johnAlexander 7:6d3ab15363a2 215 0x006E);
johnAlexander 7:6d3ab15363a2 216 break;
johnAlexander 7:6d3ab15363a2 217 case 50:
johnAlexander 7:6d3ab15363a2 218 vl53l1_wr_word(Device, RANGE_CONFIG__TIMEOUT_MACROP_A_HI,
johnAlexander 7:6d3ab15363a2 219 0x1AE);
johnAlexander 7:6d3ab15363a2 220 vl53l1_wr_word(Device, RANGE_CONFIG__TIMEOUT_MACROP_B_HI,
johnAlexander 7:6d3ab15363a2 221 0x01E8);
johnAlexander 7:6d3ab15363a2 222 break;
johnAlexander 7:6d3ab15363a2 223 case 100:
johnAlexander 7:6d3ab15363a2 224 vl53l1_wr_word(Device, RANGE_CONFIG__TIMEOUT_MACROP_A_HI,
johnAlexander 7:6d3ab15363a2 225 0x02E1);
johnAlexander 7:6d3ab15363a2 226 vl53l1_wr_word(Device, RANGE_CONFIG__TIMEOUT_MACROP_B_HI,
johnAlexander 7:6d3ab15363a2 227 0x0388);
johnAlexander 7:6d3ab15363a2 228 break;
johnAlexander 7:6d3ab15363a2 229 case 200:
johnAlexander 7:6d3ab15363a2 230 vl53l1_wr_word(Device, RANGE_CONFIG__TIMEOUT_MACROP_A_HI,
johnAlexander 7:6d3ab15363a2 231 0x03E1);
johnAlexander 7:6d3ab15363a2 232 vl53l1_wr_word(Device, RANGE_CONFIG__TIMEOUT_MACROP_B_HI,
johnAlexander 7:6d3ab15363a2 233 0x0496);
johnAlexander 7:6d3ab15363a2 234 break;
johnAlexander 7:6d3ab15363a2 235 case 500:
johnAlexander 7:6d3ab15363a2 236 vl53l1_wr_word(Device, RANGE_CONFIG__TIMEOUT_MACROP_A_HI,
johnAlexander 7:6d3ab15363a2 237 0x0591);
johnAlexander 7:6d3ab15363a2 238 vl53l1_wr_word(Device, RANGE_CONFIG__TIMEOUT_MACROP_B_HI,
johnAlexander 7:6d3ab15363a2 239 0x05C1);
johnAlexander 7:6d3ab15363a2 240 break;
johnAlexander 7:6d3ab15363a2 241 default:
johnAlexander 7:6d3ab15363a2 242 status = 1;
johnAlexander 7:6d3ab15363a2 243 break;
johnAlexander 7:6d3ab15363a2 244 }
johnAlexander 7:6d3ab15363a2 245 } else {
johnAlexander 7:6d3ab15363a2 246 switch (TimingBudgetInMs) {
johnAlexander 7:6d3ab15363a2 247 case 20:
johnAlexander 7:6d3ab15363a2 248 vl53l1_wr_word(Device, RANGE_CONFIG__TIMEOUT_MACROP_A_HI,
johnAlexander 7:6d3ab15363a2 249 0x001E);
johnAlexander 7:6d3ab15363a2 250 vl53l1_wr_word(Device, RANGE_CONFIG__TIMEOUT_MACROP_B_HI,
johnAlexander 7:6d3ab15363a2 251 0x0022);
johnAlexander 7:6d3ab15363a2 252 break;
johnAlexander 7:6d3ab15363a2 253 case 33:
johnAlexander 7:6d3ab15363a2 254 vl53l1_wr_word(Device, RANGE_CONFIG__TIMEOUT_MACROP_A_HI,
johnAlexander 7:6d3ab15363a2 255 0x0060);
johnAlexander 7:6d3ab15363a2 256 vl53l1_wr_word(Device, RANGE_CONFIG__TIMEOUT_MACROP_B_HI,
johnAlexander 7:6d3ab15363a2 257 0x006E);
johnAlexander 7:6d3ab15363a2 258 break;
johnAlexander 7:6d3ab15363a2 259 case 50:
johnAlexander 7:6d3ab15363a2 260 vl53l1_wr_word(Device, RANGE_CONFIG__TIMEOUT_MACROP_A_HI,
johnAlexander 7:6d3ab15363a2 261 0x00AD);
johnAlexander 7:6d3ab15363a2 262 vl53l1_wr_word(Device, RANGE_CONFIG__TIMEOUT_MACROP_B_HI,
johnAlexander 7:6d3ab15363a2 263 0x00C6);
johnAlexander 7:6d3ab15363a2 264 break;
johnAlexander 7:6d3ab15363a2 265 case 100:
johnAlexander 7:6d3ab15363a2 266 vl53l1_wr_word(Device, RANGE_CONFIG__TIMEOUT_MACROP_A_HI,
johnAlexander 7:6d3ab15363a2 267 0x01CC);
johnAlexander 7:6d3ab15363a2 268 vl53l1_wr_word(Device, RANGE_CONFIG__TIMEOUT_MACROP_B_HI,
johnAlexander 7:6d3ab15363a2 269 0x01EA);
johnAlexander 7:6d3ab15363a2 270 break;
johnAlexander 7:6d3ab15363a2 271 case 200:
johnAlexander 7:6d3ab15363a2 272 vl53l1_wr_word(Device, RANGE_CONFIG__TIMEOUT_MACROP_A_HI,
johnAlexander 7:6d3ab15363a2 273 0x02D9);
johnAlexander 7:6d3ab15363a2 274 vl53l1_wr_word(Device, RANGE_CONFIG__TIMEOUT_MACROP_B_HI,
johnAlexander 7:6d3ab15363a2 275 0x02F8);
johnAlexander 7:6d3ab15363a2 276 break;
johnAlexander 7:6d3ab15363a2 277 case 500:
johnAlexander 7:6d3ab15363a2 278 vl53l1_wr_word(Device, RANGE_CONFIG__TIMEOUT_MACROP_A_HI,
johnAlexander 7:6d3ab15363a2 279 0x048F);
johnAlexander 7:6d3ab15363a2 280 vl53l1_wr_word(Device, RANGE_CONFIG__TIMEOUT_MACROP_B_HI,
johnAlexander 7:6d3ab15363a2 281 0x04A4);
johnAlexander 7:6d3ab15363a2 282 break;
johnAlexander 7:6d3ab15363a2 283 default:
johnAlexander 7:6d3ab15363a2 284 status = 1;
johnAlexander 7:6d3ab15363a2 285 break;
johnAlexander 7:6d3ab15363a2 286 }
johnAlexander 7:6d3ab15363a2 287 }
johnAlexander 7:6d3ab15363a2 288 return status;
johnAlexander 7:6d3ab15363a2 289 }
johnAlexander 7:6d3ab15363a2 290
johnAlexander 7:6d3ab15363a2 291 VL53L1X_ERROR VL53L1X::vl53l1x_get_timing_budget_in_ms(uint16_t *pTimingBudget)
johnAlexander 7:6d3ab15363a2 292 {
johnAlexander 7:6d3ab15363a2 293 uint16_t Temp;
johnAlexander 7:6d3ab15363a2 294 VL53L1X_ERROR status = 0;
johnAlexander 7:6d3ab15363a2 295
johnAlexander 7:6d3ab15363a2 296 status = vl53l1_rd_word(Device, RANGE_CONFIG__TIMEOUT_MACROP_A_HI, &Temp);
johnAlexander 7:6d3ab15363a2 297 switch (Temp) {
johnAlexander 7:6d3ab15363a2 298 case 0x001D :
johnAlexander 7:6d3ab15363a2 299 *pTimingBudget = 15;
johnAlexander 7:6d3ab15363a2 300 break;
johnAlexander 7:6d3ab15363a2 301 case 0x0051 :
johnAlexander 7:6d3ab15363a2 302 case 0x001E :
johnAlexander 7:6d3ab15363a2 303 *pTimingBudget = 20;
johnAlexander 7:6d3ab15363a2 304 break;
johnAlexander 7:6d3ab15363a2 305 case 0x00D6 :
johnAlexander 7:6d3ab15363a2 306 case 0x0060 :
johnAlexander 7:6d3ab15363a2 307 *pTimingBudget = 33;
johnAlexander 7:6d3ab15363a2 308 break;
johnAlexander 7:6d3ab15363a2 309 case 0x1AE :
johnAlexander 7:6d3ab15363a2 310 case 0x00AD :
johnAlexander 7:6d3ab15363a2 311 *pTimingBudget = 50;
johnAlexander 7:6d3ab15363a2 312 break;
johnAlexander 7:6d3ab15363a2 313 case 0x02E1 :
johnAlexander 7:6d3ab15363a2 314 case 0x01CC :
johnAlexander 7:6d3ab15363a2 315 *pTimingBudget = 100;
johnAlexander 7:6d3ab15363a2 316 break;
johnAlexander 7:6d3ab15363a2 317 case 0x03E1 :
johnAlexander 7:6d3ab15363a2 318 case 0x02D9 :
johnAlexander 7:6d3ab15363a2 319 *pTimingBudget = 200;
johnAlexander 7:6d3ab15363a2 320 break;
johnAlexander 7:6d3ab15363a2 321 case 0x0591 :
johnAlexander 7:6d3ab15363a2 322 case 0x048F :
johnAlexander 7:6d3ab15363a2 323 *pTimingBudget = 500;
johnAlexander 7:6d3ab15363a2 324 break;
johnAlexander 7:6d3ab15363a2 325 default:
johnAlexander 7:6d3ab15363a2 326 *pTimingBudget = 0;
johnAlexander 7:6d3ab15363a2 327 break;
johnAlexander 7:6d3ab15363a2 328 }
johnAlexander 7:6d3ab15363a2 329 return status;
johnAlexander 7:6d3ab15363a2 330 }
johnAlexander 7:6d3ab15363a2 331
johnAlexander 7:6d3ab15363a2 332
johnAlexander 7:6d3ab15363a2 333 VL53L1X_ERROR VL53L1X::vl53l1x_set_distance_mode(uint16_t DM)
johnAlexander 7:6d3ab15363a2 334 {
johnAlexander 7:6d3ab15363a2 335 uint16_t TB;
johnAlexander 7:6d3ab15363a2 336 VL53L1X_ERROR status = 0;
johnAlexander 7:6d3ab15363a2 337
johnAlexander 7:6d3ab15363a2 338 status = vl53l1x_get_timing_budget_in_ms(&TB);
johnAlexander 7:6d3ab15363a2 339
johnAlexander 7:6d3ab15363a2 340
johnAlexander 7:6d3ab15363a2 341 switch (DM) {
johnAlexander 7:6d3ab15363a2 342 case 1:
johnAlexander 7:6d3ab15363a2 343 status = vl53l1_wr_byte(Device, PHASECAL_CONFIG__TIMEOUT_MACROP, 0x14);
johnAlexander 7:6d3ab15363a2 344 status = vl53l1_wr_byte(Device, RANGE_CONFIG__VCSEL_PERIOD_A, 0x07);
johnAlexander 7:6d3ab15363a2 345 status = vl53l1_wr_byte(Device, RANGE_CONFIG__VCSEL_PERIOD_B, 0x05);
johnAlexander 7:6d3ab15363a2 346 status = vl53l1_wr_byte(Device, RANGE_CONFIG__VALID_PHASE_HIGH, 0x38);
johnAlexander 7:6d3ab15363a2 347 status = vl53l1_wr_word(Device, SD_CONFIG__WOI_SD0, 0x0705);
johnAlexander 7:6d3ab15363a2 348 status = vl53l1_wr_word(Device, SD_CONFIG__INITIAL_PHASE_SD0, 0x0606);
johnAlexander 7:6d3ab15363a2 349 break;
johnAlexander 7:6d3ab15363a2 350 case 2:
johnAlexander 7:6d3ab15363a2 351 status = vl53l1_wr_byte(Device, PHASECAL_CONFIG__TIMEOUT_MACROP, 0x0A);
johnAlexander 7:6d3ab15363a2 352 status = vl53l1_wr_byte(Device, RANGE_CONFIG__VCSEL_PERIOD_A, 0x0F);
johnAlexander 7:6d3ab15363a2 353 status = vl53l1_wr_byte(Device, RANGE_CONFIG__VCSEL_PERIOD_B, 0x0D);
johnAlexander 7:6d3ab15363a2 354 status = vl53l1_wr_byte(Device, RANGE_CONFIG__VALID_PHASE_HIGH, 0xB8);
johnAlexander 7:6d3ab15363a2 355 status = vl53l1_wr_word(Device, SD_CONFIG__WOI_SD0, 0x0F0D);
johnAlexander 7:6d3ab15363a2 356 status = vl53l1_wr_word(Device, SD_CONFIG__INITIAL_PHASE_SD0, 0x0E0E);
johnAlexander 7:6d3ab15363a2 357 break;
johnAlexander 7:6d3ab15363a2 358 default:
johnAlexander 7:6d3ab15363a2 359 break;
johnAlexander 7:6d3ab15363a2 360 }
johnAlexander 7:6d3ab15363a2 361 status = vl53l1x_set_timing_budget_in_ms(TB);
johnAlexander 7:6d3ab15363a2 362 return status;
johnAlexander 7:6d3ab15363a2 363 }
johnAlexander 7:6d3ab15363a2 364
johnAlexander 7:6d3ab15363a2 365
johnAlexander 7:6d3ab15363a2 366
johnAlexander 7:6d3ab15363a2 367
johnAlexander 7:6d3ab15363a2 368 VL53L1X_ERROR VL53L1X::vl53l1x_get_distance_mode(uint16_t *DM)
johnAlexander 7:6d3ab15363a2 369 {
johnAlexander 7:6d3ab15363a2 370 uint8_t TempDM, status=0;
johnAlexander 7:6d3ab15363a2 371
johnAlexander 7:6d3ab15363a2 372 status = vl53l1_rd_byte(Device,PHASECAL_CONFIG__TIMEOUT_MACROP, &TempDM);
johnAlexander 7:6d3ab15363a2 373 if (TempDM == 0x14)
johnAlexander 7:6d3ab15363a2 374 *DM=1;
johnAlexander 7:6d3ab15363a2 375 if(TempDM == 0x0A)
johnAlexander 7:6d3ab15363a2 376 *DM=2;
johnAlexander 7:6d3ab15363a2 377 return status;
johnAlexander 7:6d3ab15363a2 378 }
johnAlexander 7:6d3ab15363a2 379
johnAlexander 7:6d3ab15363a2 380
johnAlexander 7:6d3ab15363a2 381
johnAlexander 7:6d3ab15363a2 382 VL53L1X_ERROR VL53L1X::vl53l1x_set_inter_measurement_in_ms(uint16_t InterMeasMs)
johnAlexander 7:6d3ab15363a2 383 {
johnAlexander 7:6d3ab15363a2 384 uint16_t ClockPLL;
johnAlexander 7:6d3ab15363a2 385 VL53L1X_ERROR status = 0;
johnAlexander 7:6d3ab15363a2 386
johnAlexander 7:6d3ab15363a2 387 status = vl53l1_rd_word(Device, VL53L1_RESULT__OSC_CALIBRATE_VAL, &ClockPLL);
johnAlexander 7:6d3ab15363a2 388 ClockPLL = ClockPLL&0x3FF;
johnAlexander 7:6d3ab15363a2 389 vl53l1_wr_double_word(Device, VL53L1_SYSTEM__INTERMEASUREMENT_PERIOD,
johnAlexander 7:6d3ab15363a2 390 (uint32_t)(ClockPLL * InterMeasMs * 1.075));
johnAlexander 7:6d3ab15363a2 391 return status;
johnAlexander 7:6d3ab15363a2 392
johnAlexander 7:6d3ab15363a2 393 }
johnAlexander 7:6d3ab15363a2 394
johnAlexander 7:6d3ab15363a2 395
johnAlexander 7:6d3ab15363a2 396 VL53L1X_ERROR VL53L1X::vl53l1x_get_inter_measurement_in_ms(uint16_t *pIM)
johnAlexander 7:6d3ab15363a2 397 {
johnAlexander 7:6d3ab15363a2 398 uint16_t ClockPLL;
johnAlexander 7:6d3ab15363a2 399 VL53L1X_ERROR status = 0;
johnAlexander 7:6d3ab15363a2 400 uint32_t tmp;
johnAlexander 7:6d3ab15363a2 401
johnAlexander 7:6d3ab15363a2 402 status = vl53l1_rd_double_word(Device,VL53L1_SYSTEM__INTERMEASUREMENT_PERIOD, &tmp);
johnAlexander 7:6d3ab15363a2 403 *pIM = (uint16_t)tmp;
johnAlexander 7:6d3ab15363a2 404 status = vl53l1_rd_word(Device, VL53L1_RESULT__OSC_CALIBRATE_VAL, &ClockPLL);
johnAlexander 7:6d3ab15363a2 405 ClockPLL = ClockPLL&0x3FF;
johnAlexander 7:6d3ab15363a2 406 *pIM= (uint16_t)(*pIM/(ClockPLL*1.065));
johnAlexander 7:6d3ab15363a2 407 return status;
johnAlexander 7:6d3ab15363a2 408 }
johnAlexander 7:6d3ab15363a2 409
johnAlexander 7:6d3ab15363a2 410
johnAlexander 7:6d3ab15363a2 411 VL53L1X_ERROR VL53L1X::vl53l1x_boot_state(uint8_t *state)
johnAlexander 7:6d3ab15363a2 412 {
johnAlexander 7:6d3ab15363a2 413 VL53L1X_ERROR status = 0;
johnAlexander 7:6d3ab15363a2 414 uint8_t tmp = 0;
johnAlexander 7:6d3ab15363a2 415
johnAlexander 7:6d3ab15363a2 416 status = vl53l1_rd_byte(Device,VL53L1_FIRMWARE__SYSTEM_STATUS, &tmp);
johnAlexander 7:6d3ab15363a2 417 *state = tmp;
johnAlexander 7:6d3ab15363a2 418 return status;
johnAlexander 7:6d3ab15363a2 419 }
johnAlexander 7:6d3ab15363a2 420
johnAlexander 7:6d3ab15363a2 421
johnAlexander 7:6d3ab15363a2 422 VL53L1X_ERROR VL53L1X::vl53l1x_get_sensor_id(uint16_t *sensorId)
johnAlexander 7:6d3ab15363a2 423 {
johnAlexander 7:6d3ab15363a2 424 VL53L1X_ERROR status = 0;
johnAlexander 7:6d3ab15363a2 425 uint16_t tmp = 0;
johnAlexander 7:6d3ab15363a2 426
johnAlexander 7:6d3ab15363a2 427 status = vl53l1_rd_word(Device, VL53L1_IDENTIFICATION__MODEL_ID, &tmp);
johnAlexander 7:6d3ab15363a2 428 *sensorId = tmp;
johnAlexander 7:6d3ab15363a2 429 return status;
johnAlexander 7:6d3ab15363a2 430 }
johnAlexander 7:6d3ab15363a2 431
johnAlexander 7:6d3ab15363a2 432
johnAlexander 7:6d3ab15363a2 433 VL53L1X_ERROR VL53L1X::vl53l1x_get_distance(uint16_t *distance)
johnAlexander 7:6d3ab15363a2 434 {
johnAlexander 7:6d3ab15363a2 435 VL53L1X_ERROR status = 0;
johnAlexander 7:6d3ab15363a2 436 uint16_t tmp;
johnAlexander 7:6d3ab15363a2 437
johnAlexander 7:6d3ab15363a2 438 status = (vl53l1_rd_word(Device,
johnAlexander 7:6d3ab15363a2 439 VL53L1_RESULT__FINAL_CROSSTALK_CORRECTED_RANGE_MM_SD0, &tmp));
johnAlexander 7:6d3ab15363a2 440 *distance = tmp;
johnAlexander 7:6d3ab15363a2 441 return status;
johnAlexander 7:6d3ab15363a2 442 }
johnAlexander 7:6d3ab15363a2 443
johnAlexander 7:6d3ab15363a2 444 VL53L1X_ERROR VL53L1X::vl53l1x_get_signal_per_spad(uint16_t *signalRate)
johnAlexander 7:6d3ab15363a2 445 {
johnAlexander 7:6d3ab15363a2 446 VL53L1X_ERROR status = 0;
johnAlexander 7:6d3ab15363a2 447 uint16_t SpNb=1, signal;
johnAlexander 7:6d3ab15363a2 448
johnAlexander 7:6d3ab15363a2 449 status = vl53l1_rd_word(Device,
johnAlexander 7:6d3ab15363a2 450 VL53L1_RESULT__PEAK_SIGNAL_COUNT_RATE_CROSSTALK_CORRECTED_MCPS_SD0, &signal);
johnAlexander 7:6d3ab15363a2 451 status = vl53l1_rd_word(Device,
johnAlexander 7:6d3ab15363a2 452 VL53L1_RESULT__DSS_ACTUAL_EFFECTIVE_SPADS_SD0, &SpNb);
johnAlexander 7:6d3ab15363a2 453 *signalRate = (uint16_t) (2000.0*signal/SpNb);
johnAlexander 7:6d3ab15363a2 454 return status;
johnAlexander 7:6d3ab15363a2 455 }
johnAlexander 7:6d3ab15363a2 456
johnAlexander 7:6d3ab15363a2 457
johnAlexander 7:6d3ab15363a2 458 VL53L1X_ERROR VL53L1X::vl53l1x_get_ambient_per_spad(uint16_t *ambPerSp)
johnAlexander 7:6d3ab15363a2 459 {
johnAlexander 7:6d3ab15363a2 460 VL53L1X_ERROR status=0;
johnAlexander 7:6d3ab15363a2 461 uint16_t AmbientRate, SpNb=1;
johnAlexander 7:6d3ab15363a2 462
johnAlexander 7:6d3ab15363a2 463 status = vl53l1_rd_word(Device, RESULT__AMBIENT_COUNT_RATE_MCPS_SD, &AmbientRate);
johnAlexander 7:6d3ab15363a2 464 status = vl53l1_rd_word(Device, VL53L1_RESULT__DSS_ACTUAL_EFFECTIVE_SPADS_SD0, &SpNb);
johnAlexander 7:6d3ab15363a2 465 *ambPerSp=(uint16_t) (2000.0 * AmbientRate / SpNb);
johnAlexander 7:6d3ab15363a2 466 return status;
johnAlexander 7:6d3ab15363a2 467 }
johnAlexander 7:6d3ab15363a2 468
johnAlexander 7:6d3ab15363a2 469
johnAlexander 7:6d3ab15363a2 470 VL53L1X_ERROR VL53L1X::vl53l1x_get_signal_rate(uint16_t *signal)
johnAlexander 7:6d3ab15363a2 471 {
johnAlexander 7:6d3ab15363a2 472 VL53L1X_ERROR status = 0;
johnAlexander 7:6d3ab15363a2 473 uint16_t tmp;
johnAlexander 7:6d3ab15363a2 474
johnAlexander 7:6d3ab15363a2 475 status = vl53l1_rd_word(Device,
johnAlexander 7:6d3ab15363a2 476 VL53L1_RESULT__PEAK_SIGNAL_COUNT_RATE_CROSSTALK_CORRECTED_MCPS_SD0, &tmp);
johnAlexander 7:6d3ab15363a2 477 *signal = tmp*8;
johnAlexander 7:6d3ab15363a2 478 return status;
johnAlexander 7:6d3ab15363a2 479 }
johnAlexander 7:6d3ab15363a2 480
johnAlexander 7:6d3ab15363a2 481
johnAlexander 7:6d3ab15363a2 482 VL53L1X_ERROR VL53L1X::vl53l1x_get_spad_nb(uint16_t *spNb)
johnAlexander 7:6d3ab15363a2 483 {
johnAlexander 7:6d3ab15363a2 484 VL53L1X_ERROR status = 0;
johnAlexander 7:6d3ab15363a2 485 uint16_t tmp;
johnAlexander 7:6d3ab15363a2 486
johnAlexander 7:6d3ab15363a2 487 status = vl53l1_rd_word(Device,
johnAlexander 7:6d3ab15363a2 488 VL53L1_RESULT__DSS_ACTUAL_EFFECTIVE_SPADS_SD0, &tmp);
johnAlexander 7:6d3ab15363a2 489 *spNb = tmp >> 8;
johnAlexander 7:6d3ab15363a2 490 return status;
johnAlexander 7:6d3ab15363a2 491 }
johnAlexander 7:6d3ab15363a2 492
johnAlexander 7:6d3ab15363a2 493
johnAlexander 7:6d3ab15363a2 494 VL53L1X_ERROR VL53L1X::vl53l1x_get_ambient_rate(uint16_t *ambRate)
johnAlexander 7:6d3ab15363a2 495 {
johnAlexander 7:6d3ab15363a2 496 VL53L1X_ERROR status = 0;
johnAlexander 7:6d3ab15363a2 497 uint16_t tmp;
johnAlexander 7:6d3ab15363a2 498
johnAlexander 7:6d3ab15363a2 499 status = vl53l1_rd_word(Device, RESULT__AMBIENT_COUNT_RATE_MCPS_SD, &tmp);
johnAlexander 7:6d3ab15363a2 500 *ambRate = tmp*8;
johnAlexander 7:6d3ab15363a2 501 return status;
johnAlexander 7:6d3ab15363a2 502 }
johnAlexander 7:6d3ab15363a2 503
johnAlexander 7:6d3ab15363a2 504
johnAlexander 7:6d3ab15363a2 505 VL53L1X_ERROR VL53L1X::vl53l1x_get_range_status(uint8_t *rangeStatus)
johnAlexander 7:6d3ab15363a2 506 {
johnAlexander 7:6d3ab15363a2 507 VL53L1X_ERROR status = 0;
johnAlexander 7:6d3ab15363a2 508 uint8_t RgSt;
johnAlexander 7:6d3ab15363a2 509
johnAlexander 7:6d3ab15363a2 510 status = vl53l1_rd_byte(Device, VL53L1_RESULT__RANGE_STATUS, &RgSt);
johnAlexander 7:6d3ab15363a2 511 RgSt = RgSt&0x1F;
johnAlexander 7:6d3ab15363a2 512 switch (RgSt) {
johnAlexander 7:6d3ab15363a2 513 case 9:
johnAlexander 7:6d3ab15363a2 514 RgSt = 0;
johnAlexander 7:6d3ab15363a2 515 break;
johnAlexander 7:6d3ab15363a2 516 case 6:
johnAlexander 7:6d3ab15363a2 517 RgSt = 1;
johnAlexander 7:6d3ab15363a2 518 break;
johnAlexander 7:6d3ab15363a2 519 case 4:
johnAlexander 7:6d3ab15363a2 520 RgSt = 2;
johnAlexander 7:6d3ab15363a2 521 break;
johnAlexander 7:6d3ab15363a2 522 case 8:
johnAlexander 7:6d3ab15363a2 523 RgSt = 3;
johnAlexander 7:6d3ab15363a2 524 break;
johnAlexander 7:6d3ab15363a2 525 case 5:
johnAlexander 7:6d3ab15363a2 526 RgSt = 4;
johnAlexander 7:6d3ab15363a2 527 break;
johnAlexander 7:6d3ab15363a2 528 case 3:
johnAlexander 7:6d3ab15363a2 529 RgSt = 5;
johnAlexander 7:6d3ab15363a2 530 break;
johnAlexander 7:6d3ab15363a2 531 case 19:
johnAlexander 7:6d3ab15363a2 532 RgSt = 6;
johnAlexander 7:6d3ab15363a2 533 break;
johnAlexander 7:6d3ab15363a2 534 case 7:
johnAlexander 7:6d3ab15363a2 535 RgSt = 7;
johnAlexander 7:6d3ab15363a2 536 break;
johnAlexander 7:6d3ab15363a2 537 case 12:
johnAlexander 7:6d3ab15363a2 538 RgSt = 9;
johnAlexander 7:6d3ab15363a2 539 break;
johnAlexander 7:6d3ab15363a2 540 case 18:
johnAlexander 7:6d3ab15363a2 541 RgSt = 10;
johnAlexander 7:6d3ab15363a2 542 break;
johnAlexander 7:6d3ab15363a2 543 case 22:
johnAlexander 7:6d3ab15363a2 544 RgSt = 11;
johnAlexander 7:6d3ab15363a2 545 break;
johnAlexander 7:6d3ab15363a2 546 case 23:
johnAlexander 7:6d3ab15363a2 547 RgSt = 12;
johnAlexander 7:6d3ab15363a2 548 break;
johnAlexander 7:6d3ab15363a2 549 case 13:
johnAlexander 7:6d3ab15363a2 550 RgSt = 13;
johnAlexander 7:6d3ab15363a2 551 break;
johnAlexander 7:6d3ab15363a2 552 default:
johnAlexander 7:6d3ab15363a2 553 RgSt = 255;
johnAlexander 7:6d3ab15363a2 554 break;
johnAlexander 7:6d3ab15363a2 555 }
johnAlexander 7:6d3ab15363a2 556 *rangeStatus = RgSt;
johnAlexander 7:6d3ab15363a2 557 return status;
johnAlexander 7:6d3ab15363a2 558 }
johnAlexander 7:6d3ab15363a2 559
johnAlexander 7:6d3ab15363a2 560
johnAlexander 7:6d3ab15363a2 561 VL53L1X_ERROR VL53L1X::vl53l1x_set_offset(int16_t OffsetValue)
johnAlexander 7:6d3ab15363a2 562 {
johnAlexander 7:6d3ab15363a2 563 VL53L1X_ERROR status = 0;
johnAlexander 7:6d3ab15363a2 564 int16_t Temp;
johnAlexander 7:6d3ab15363a2 565
johnAlexander 7:6d3ab15363a2 566 Temp = (OffsetValue*4);
johnAlexander 7:6d3ab15363a2 567 vl53l1_wr_word(Device, ALGO__PART_TO_PART_RANGE_OFFSET_MM,
johnAlexander 7:6d3ab15363a2 568 (uint16_t)Temp);
johnAlexander 7:6d3ab15363a2 569 vl53l1_wr_word(Device, MM_CONFIG__INNER_OFFSET_MM, 0x0);
johnAlexander 7:6d3ab15363a2 570 vl53l1_wr_word(Device, MM_CONFIG__OUTER_OFFSET_MM, 0x0);
johnAlexander 7:6d3ab15363a2 571 return status;
johnAlexander 7:6d3ab15363a2 572 }
johnAlexander 7:6d3ab15363a2 573
johnAlexander 7:6d3ab15363a2 574
johnAlexander 7:6d3ab15363a2 575 VL53L1X_ERROR VL53L1X::vl53l1x_get_offset(int16_t *offset)
johnAlexander 7:6d3ab15363a2 576 {
johnAlexander 7:6d3ab15363a2 577 VL53L1X_ERROR status = 0;
johnAlexander 7:6d3ab15363a2 578 uint16_t Temp;
johnAlexander 7:6d3ab15363a2 579
johnAlexander 7:6d3ab15363a2 580 status = vl53l1_rd_word(Device,ALGO__PART_TO_PART_RANGE_OFFSET_MM, &Temp);
johnAlexander 7:6d3ab15363a2 581 Temp = Temp<<3;
johnAlexander 7:6d3ab15363a2 582 Temp = Temp >>5;
johnAlexander 7:6d3ab15363a2 583 *offset = (int16_t)(Temp);
johnAlexander 7:6d3ab15363a2 584 return status;
johnAlexander 7:6d3ab15363a2 585 }
johnAlexander 7:6d3ab15363a2 586
johnAlexander 7:6d3ab15363a2 587 VL53L1X_ERROR VL53L1X::vl53l1x_set_xtalk(uint16_t XtalkValue)
johnAlexander 7:6d3ab15363a2 588 {
johnAlexander 7:6d3ab15363a2 589 /* XTalkValue in count per second to avoid float type */
johnAlexander 7:6d3ab15363a2 590 VL53L1X_ERROR status = 0;
johnAlexander 7:6d3ab15363a2 591
johnAlexander 7:6d3ab15363a2 592 status = vl53l1_wr_word(Device,
johnAlexander 7:6d3ab15363a2 593 ALGO__CROSSTALK_COMPENSATION_X_PLANE_GRADIENT_KCPS,
johnAlexander 7:6d3ab15363a2 594 0x0000);
johnAlexander 7:6d3ab15363a2 595 status = vl53l1_wr_word(Device, ALGO__CROSSTALK_COMPENSATION_Y_PLANE_GRADIENT_KCPS,
johnAlexander 7:6d3ab15363a2 596 0x0000);
johnAlexander 7:6d3ab15363a2 597 status = vl53l1_wr_word(Device, ALGO__CROSSTALK_COMPENSATION_PLANE_OFFSET_KCPS,
johnAlexander 7:6d3ab15363a2 598 (XtalkValue<<9)/1000); /* * << 9 (7.9 format) and /1000 to convert cps to kpcs */
johnAlexander 7:6d3ab15363a2 599 return status;
johnAlexander 7:6d3ab15363a2 600 }
johnAlexander 7:6d3ab15363a2 601
johnAlexander 7:6d3ab15363a2 602
johnAlexander 7:6d3ab15363a2 603 VL53L1X_ERROR VL53L1X::vl53l1x_get_xtalk(uint16_t *xtalk )
johnAlexander 7:6d3ab15363a2 604 {
johnAlexander 7:6d3ab15363a2 605 VL53L1X_ERROR status = 0;
johnAlexander 7:6d3ab15363a2 606 uint16_t tmp;
johnAlexander 7:6d3ab15363a2 607
johnAlexander 7:6d3ab15363a2 608 status = vl53l1_rd_word(Device,ALGO__CROSSTALK_COMPENSATION_PLANE_OFFSET_KCPS, &tmp);
johnAlexander 7:6d3ab15363a2 609 *xtalk = (tmp*1000)>>9; /* * 1000 to convert kcps to cps and >> 9 (7.9 format) */
johnAlexander 7:6d3ab15363a2 610 return status;
johnAlexander 7:6d3ab15363a2 611 }
johnAlexander 7:6d3ab15363a2 612
johnAlexander 7:6d3ab15363a2 613
johnAlexander 7:6d3ab15363a2 614 VL53L1X_ERROR VL53L1X::vl53l1x_set_distance_threshold(uint16_t ThreshLow,
johnAlexander 7:6d3ab15363a2 615 uint16_t ThreshHigh, uint8_t Window,
johnAlexander 7:6d3ab15363a2 616 uint8_t IntOnNoTarget)
johnAlexander 7:6d3ab15363a2 617 {
johnAlexander 7:6d3ab15363a2 618 VL53L1X_ERROR status = 0;
johnAlexander 7:6d3ab15363a2 619 uint8_t Temp = 0;
johnAlexander 7:6d3ab15363a2 620
johnAlexander 7:6d3ab15363a2 621 status = vl53l1_rd_byte(Device, SYSTEM__INTERRUPT_CONFIG_GPIO, &Temp);
johnAlexander 7:6d3ab15363a2 622 Temp = Temp & 0x47;
johnAlexander 7:6d3ab15363a2 623 if (IntOnNoTarget == 0) {
johnAlexander 7:6d3ab15363a2 624 status = vl53l1_wr_byte(Device, SYSTEM__INTERRUPT_CONFIG_GPIO,
johnAlexander 7:6d3ab15363a2 625 (Temp | (Window & 0x07)));
johnAlexander 7:6d3ab15363a2 626 } else {
johnAlexander 7:6d3ab15363a2 627 status = vl53l1_wr_byte(Device, SYSTEM__INTERRUPT_CONFIG_GPIO,
johnAlexander 7:6d3ab15363a2 628 ((Temp | (Window & 0x07)) | 0x40));
johnAlexander 7:6d3ab15363a2 629 }
johnAlexander 7:6d3ab15363a2 630 status = vl53l1_wr_word(Device, SYSTEM__THRESH_HIGH, ThreshHigh);
johnAlexander 7:6d3ab15363a2 631 status = vl53l1_wr_word(Device, SYSTEM__THRESH_LOW, ThreshLow);
johnAlexander 7:6d3ab15363a2 632 return status;
johnAlexander 7:6d3ab15363a2 633 }
johnAlexander 7:6d3ab15363a2 634
johnAlexander 7:6d3ab15363a2 635
johnAlexander 7:6d3ab15363a2 636 VL53L1X_ERROR VL53L1X::vl53l1x_get_distance_threshold_window(uint16_t *window)
johnAlexander 7:6d3ab15363a2 637 {
johnAlexander 7:6d3ab15363a2 638 VL53L1X_ERROR status = 0;
johnAlexander 7:6d3ab15363a2 639 uint8_t tmp;
johnAlexander 7:6d3ab15363a2 640 status = vl53l1_rd_byte(Device,SYSTEM__INTERRUPT_CONFIG_GPIO, &tmp);
johnAlexander 7:6d3ab15363a2 641 *window = (uint16_t)(tmp & 0x7);
johnAlexander 7:6d3ab15363a2 642 return status;
johnAlexander 7:6d3ab15363a2 643 }
johnAlexander 7:6d3ab15363a2 644
johnAlexander 7:6d3ab15363a2 645
johnAlexander 7:6d3ab15363a2 646 VL53L1X_ERROR VL53L1X::vl53l1x_get_distance_threshold_low(uint16_t *low)
johnAlexander 7:6d3ab15363a2 647 {
johnAlexander 7:6d3ab15363a2 648 VL53L1X_ERROR status = 0;
johnAlexander 7:6d3ab15363a2 649 uint16_t tmp;
johnAlexander 7:6d3ab15363a2 650
johnAlexander 7:6d3ab15363a2 651 status = vl53l1_rd_word(Device,SYSTEM__THRESH_LOW, &tmp);
johnAlexander 7:6d3ab15363a2 652 *low = tmp;
johnAlexander 7:6d3ab15363a2 653 return status;
johnAlexander 7:6d3ab15363a2 654 }
johnAlexander 7:6d3ab15363a2 655
johnAlexander 7:6d3ab15363a2 656 VL53L1X_ERROR VL53L1X::vl53l1x_get_distance_threshold_high(uint16_t *high)
johnAlexander 7:6d3ab15363a2 657 {
johnAlexander 7:6d3ab15363a2 658 VL53L1X_ERROR status = 0;
johnAlexander 7:6d3ab15363a2 659 uint16_t tmp;
johnAlexander 7:6d3ab15363a2 660
johnAlexander 7:6d3ab15363a2 661 status = vl53l1_rd_word(Device,SYSTEM__THRESH_HIGH, &tmp);
johnAlexander 7:6d3ab15363a2 662 *high = tmp;
johnAlexander 7:6d3ab15363a2 663 return status;
johnAlexander 7:6d3ab15363a2 664 }
johnAlexander 7:6d3ab15363a2 665
johnAlexander 7:6d3ab15363a2 666 VL53L1X_ERROR VL53L1X::vl53l1x_set_roi(uint16_t X, uint16_t Y)
johnAlexander 7:6d3ab15363a2 667 {
johnAlexander 7:6d3ab15363a2 668 uint8_t OpticalCenter;
johnAlexander 7:6d3ab15363a2 669 VL53L1X_ERROR status = 0;
johnAlexander 7:6d3ab15363a2 670
johnAlexander 7:6d3ab15363a2 671 status =vl53l1_rd_byte(Device, VL53L1_ROI_CONFIG__MODE_ROI_CENTRE_SPAD, &OpticalCenter);
johnAlexander 7:6d3ab15363a2 672 if (X > 16)
johnAlexander 7:6d3ab15363a2 673 X = 16;
johnAlexander 7:6d3ab15363a2 674 if (Y > 16)
johnAlexander 7:6d3ab15363a2 675 Y = 16;
johnAlexander 7:6d3ab15363a2 676 if (X > 10 || Y > 10){
johnAlexander 7:6d3ab15363a2 677 OpticalCenter = 199;
johnAlexander 7:6d3ab15363a2 678 }
johnAlexander 7:6d3ab15363a2 679 status = vl53l1_wr_byte(Device, ROI_CONFIG__USER_ROI_CENTRE_SPAD, OpticalCenter);
johnAlexander 7:6d3ab15363a2 680 status = vl53l1_wr_byte(Device, ROI_CONFIG__USER_ROI_REQUESTED_GLOBAL_XY_SIZE,
johnAlexander 7:6d3ab15363a2 681 (Y - 1) << 4 | (X - 1));
johnAlexander 7:6d3ab15363a2 682 return status;
johnAlexander 7:6d3ab15363a2 683 }
johnAlexander 7:6d3ab15363a2 684
johnAlexander 7:6d3ab15363a2 685 VL53L1X_ERROR VL53L1X::vl53l1x_get_roi_xy(uint16_t *ROI_X, uint16_t *ROI_Y)
johnAlexander 7:6d3ab15363a2 686 {
johnAlexander 7:6d3ab15363a2 687 VL53L1X_ERROR status = 0;
johnAlexander 7:6d3ab15363a2 688 uint8_t tmp;
johnAlexander 7:6d3ab15363a2 689
johnAlexander 7:6d3ab15363a2 690 status = vl53l1_rd_byte(Device,ROI_CONFIG__USER_ROI_REQUESTED_GLOBAL_XY_SIZE, &tmp);
johnAlexander 7:6d3ab15363a2 691 *ROI_X = ((uint16_t)tmp & 0x0F) + 1;
johnAlexander 7:6d3ab15363a2 692 *ROI_Y = (((uint16_t)tmp & 0xF0) >> 4) + 1;
johnAlexander 7:6d3ab15363a2 693 return status;
johnAlexander 7:6d3ab15363a2 694 }
johnAlexander 7:6d3ab15363a2 695
johnAlexander 7:6d3ab15363a2 696 VL53L1X_ERROR VL53L1X::vl53l1x_set_signal_threshold(uint16_t Signal)
johnAlexander 7:6d3ab15363a2 697 {
johnAlexander 7:6d3ab15363a2 698 VL53L1X_ERROR status = 0;
johnAlexander 7:6d3ab15363a2 699
johnAlexander 7:6d3ab15363a2 700 vl53l1_wr_word(Device,RANGE_CONFIG__MIN_COUNT_RATE_RTN_LIMIT_MCPS,Signal>>3);
johnAlexander 7:6d3ab15363a2 701 return status;
johnAlexander 7:6d3ab15363a2 702 }
johnAlexander 7:6d3ab15363a2 703
johnAlexander 7:6d3ab15363a2 704 VL53L1X_ERROR VL53L1X::vl53l1x_get_signal_threshold(uint16_t *signal)
johnAlexander 7:6d3ab15363a2 705 {
johnAlexander 7:6d3ab15363a2 706 VL53L1X_ERROR status = 0;
johnAlexander 7:6d3ab15363a2 707 uint16_t tmp;
johnAlexander 7:6d3ab15363a2 708
johnAlexander 7:6d3ab15363a2 709 status = vl53l1_rd_word(Device,
johnAlexander 7:6d3ab15363a2 710 RANGE_CONFIG__MIN_COUNT_RATE_RTN_LIMIT_MCPS, &tmp);
johnAlexander 7:6d3ab15363a2 711 *signal = tmp <<3;
johnAlexander 7:6d3ab15363a2 712 return status;
johnAlexander 7:6d3ab15363a2 713 }
johnAlexander 7:6d3ab15363a2 714
johnAlexander 7:6d3ab15363a2 715
johnAlexander 7:6d3ab15363a2 716 VL53L1X_ERROR VL53L1X::vl53l1x_set_sigma_threshold(uint16_t Sigma)
johnAlexander 7:6d3ab15363a2 717 {
johnAlexander 7:6d3ab15363a2 718 VL53L1X_ERROR status = 0;
johnAlexander 7:6d3ab15363a2 719
johnAlexander 7:6d3ab15363a2 720 if(Sigma>(0xFFFF>>2)){
johnAlexander 7:6d3ab15363a2 721 return 1;
johnAlexander 7:6d3ab15363a2 722 }
johnAlexander 7:6d3ab15363a2 723 /* 16 bits register 14.2 format */
johnAlexander 7:6d3ab15363a2 724 status = vl53l1_wr_word(Device,RANGE_CONFIG__SIGMA_THRESH,Sigma<<2);
johnAlexander 7:6d3ab15363a2 725 return status;
johnAlexander 7:6d3ab15363a2 726 }
johnAlexander 7:6d3ab15363a2 727
johnAlexander 7:6d3ab15363a2 728 VL53L1X_ERROR VL53L1X::vl53l1x_get_sigma_threshold(uint16_t *sigma)
johnAlexander 7:6d3ab15363a2 729 {
johnAlexander 7:6d3ab15363a2 730 VL53L1X_ERROR status = 0;
johnAlexander 7:6d3ab15363a2 731 uint16_t tmp;
johnAlexander 7:6d3ab15363a2 732
johnAlexander 7:6d3ab15363a2 733 status = vl53l1_rd_word(Device,RANGE_CONFIG__SIGMA_THRESH, &tmp);
johnAlexander 7:6d3ab15363a2 734 *sigma = tmp >> 2;
johnAlexander 7:6d3ab15363a2 735 return status;
johnAlexander 7:6d3ab15363a2 736
johnAlexander 7:6d3ab15363a2 737 }
johnAlexander 7:6d3ab15363a2 738
johnAlexander 7:6d3ab15363a2 739 VL53L1X_ERROR VL53L1X::vl53l1x_start_temperature_update()
johnAlexander 7:6d3ab15363a2 740 {
johnAlexander 7:6d3ab15363a2 741 VL53L1X_ERROR status = 0;
johnAlexander 7:6d3ab15363a2 742 uint8_t tmp=0;
johnAlexander 7:6d3ab15363a2 743
johnAlexander 7:6d3ab15363a2 744 status = vl53l1_wr_byte(Device,VL53L1_VHV_CONFIG__TIMEOUT_MACROP_LOOP_BOUND,0x81); /* full VHV */
johnAlexander 7:6d3ab15363a2 745 status = vl53l1_wr_byte(Device,0x0B,0x92);
johnAlexander 7:6d3ab15363a2 746 status = vl53l1x_start_ranging();
johnAlexander 7:6d3ab15363a2 747 while(tmp==0){
johnAlexander 7:6d3ab15363a2 748 status = vl53l1x_check_for_data_ready(&tmp);
johnAlexander 7:6d3ab15363a2 749 }
johnAlexander 7:6d3ab15363a2 750 tmp = 0;
johnAlexander 7:6d3ab15363a2 751 status = vl53l1x_clear_interrupt();
johnAlexander 7:6d3ab15363a2 752 status = vl53l1x_stop_ranging();
johnAlexander 7:6d3ab15363a2 753 status = vl53l1_wr_byte(Device, VL53L1_VHV_CONFIG__TIMEOUT_MACROP_LOOP_BOUND, 0x09); /* two bounds VHV */
johnAlexander 7:6d3ab15363a2 754 status = vl53l1_wr_byte(Device, 0x0B, 0); /* start VHV from the previous temperature */
johnAlexander 7:6d3ab15363a2 755 return status;
johnAlexander 7:6d3ab15363a2 756 }
johnAlexander 7:6d3ab15363a2 757
johnAlexander 7:6d3ab15363a2 758 /* VL53L1X_calibration.h functions */
johnAlexander 7:6d3ab15363a2 759
johnAlexander 7:6d3ab15363a2 760 int8_t VL53L1X::vl53l1x_calibrate_offset(uint16_t TargetDistInMm, int16_t *offset)
johnAlexander 7:6d3ab15363a2 761 {
johnAlexander 7:6d3ab15363a2 762 uint8_t i = 0, tmp;
johnAlexander 7:6d3ab15363a2 763 int16_t AverageDistance = 0;
johnAlexander 7:6d3ab15363a2 764 uint16_t distance;
johnAlexander 7:6d3ab15363a2 765 VL53L1X_ERROR status = 0;
johnAlexander 7:6d3ab15363a2 766
johnAlexander 7:6d3ab15363a2 767 status = vl53l1_wr_word(Device, ALGO__PART_TO_PART_RANGE_OFFSET_MM, 0x0);
johnAlexander 7:6d3ab15363a2 768 status = vl53l1_wr_word(Device, MM_CONFIG__INNER_OFFSET_MM, 0x0);
johnAlexander 7:6d3ab15363a2 769 status = vl53l1_wr_word(Device, MM_CONFIG__OUTER_OFFSET_MM, 0x0);
johnAlexander 7:6d3ab15363a2 770 status = vl53l1x_start_ranging(); /* Enable VL53L1X sensor */
johnAlexander 7:6d3ab15363a2 771 for (i = 0; i < 50; i++) {
johnAlexander 7:6d3ab15363a2 772 while (tmp == 0){
johnAlexander 7:6d3ab15363a2 773 status = vl53l1x_check_for_data_ready(&tmp);
johnAlexander 7:6d3ab15363a2 774 }
johnAlexander 7:6d3ab15363a2 775 tmp = 0;
johnAlexander 7:6d3ab15363a2 776 status = vl53l1x_get_distance(&distance);
johnAlexander 7:6d3ab15363a2 777 status = vl53l1x_clear_interrupt();
johnAlexander 7:6d3ab15363a2 778 AverageDistance = AverageDistance + distance;
johnAlexander 7:6d3ab15363a2 779 }
johnAlexander 7:6d3ab15363a2 780 status = vl53l1x_stop_ranging();
johnAlexander 7:6d3ab15363a2 781 AverageDistance = AverageDistance / 50;
johnAlexander 7:6d3ab15363a2 782 *offset = TargetDistInMm - AverageDistance;
johnAlexander 7:6d3ab15363a2 783 status = vl53l1_wr_word(Device, ALGO__PART_TO_PART_RANGE_OFFSET_MM, *offset*4);
johnAlexander 7:6d3ab15363a2 784 return status;
johnAlexander 7:6d3ab15363a2 785 }
johnAlexander 7:6d3ab15363a2 786
johnAlexander 7:6d3ab15363a2 787
johnAlexander 7:6d3ab15363a2 788 int8_t VL53L1X::vl53l1x_calibrate_xtalk(uint16_t TargetDistInMm, uint16_t *xtalk)
johnAlexander 7:6d3ab15363a2 789 {
johnAlexander 7:6d3ab15363a2 790 uint8_t i, tmp= 0;
johnAlexander 7:6d3ab15363a2 791 float AverageSignalRate = 0;
johnAlexander 7:6d3ab15363a2 792 float AverageDistance = 0;
johnAlexander 7:6d3ab15363a2 793 float AverageSpadNb = 0;
johnAlexander 7:6d3ab15363a2 794 uint16_t distance = 0, spadNum;
johnAlexander 7:6d3ab15363a2 795 uint16_t sr;
johnAlexander 7:6d3ab15363a2 796 VL53L1X_ERROR status = 0;
johnAlexander 7:6d3ab15363a2 797
johnAlexander 7:6d3ab15363a2 798 status = vl53l1_wr_word(Device, 0x0016,0);
johnAlexander 7:6d3ab15363a2 799 status = vl53l1x_start_ranging();
johnAlexander 7:6d3ab15363a2 800 for (i = 0; i < 50; i++) {
johnAlexander 7:6d3ab15363a2 801 while (tmp == 0){
johnAlexander 7:6d3ab15363a2 802 status = vl53l1x_check_for_data_ready(&tmp);
johnAlexander 7:6d3ab15363a2 803 }
johnAlexander 7:6d3ab15363a2 804 tmp=0;
johnAlexander 7:6d3ab15363a2 805 status= vl53l1x_get_signal_rate(&sr);
johnAlexander 7:6d3ab15363a2 806 status= vl53l1x_get_distance(&distance);
johnAlexander 7:6d3ab15363a2 807 status = vl53l1x_clear_interrupt();
johnAlexander 7:6d3ab15363a2 808 AverageDistance = AverageDistance + distance;
johnAlexander 7:6d3ab15363a2 809 status = vl53l1x_get_spad_nb(&spadNum);
johnAlexander 7:6d3ab15363a2 810 AverageSpadNb = AverageSpadNb + spadNum;
johnAlexander 7:6d3ab15363a2 811 AverageSignalRate =
johnAlexander 7:6d3ab15363a2 812 AverageSignalRate + sr;
johnAlexander 7:6d3ab15363a2 813 }
johnAlexander 7:6d3ab15363a2 814 status = vl53l1x_stop_ranging();
johnAlexander 7:6d3ab15363a2 815 AverageDistance = AverageDistance / 50;
johnAlexander 7:6d3ab15363a2 816 AverageSpadNb = AverageSpadNb / 50;
johnAlexander 7:6d3ab15363a2 817 AverageSignalRate = AverageSignalRate / 50;
johnAlexander 7:6d3ab15363a2 818 /* Calculate Xtalk value */
johnAlexander 7:6d3ab15363a2 819 *xtalk = (uint16_t)(512*(AverageSignalRate*(1-(AverageDistance/TargetDistInMm)))/AverageSpadNb);
johnAlexander 7:6d3ab15363a2 820 status = vl53l1_wr_word(Device, 0x0016, *xtalk);
johnAlexander 7:6d3ab15363a2 821 return status;
johnAlexander 7:6d3ab15363a2 822 }
johnAlexander 7:6d3ab15363a2 823
johnAlexander 7:6d3ab15363a2 824
johnAlexander 7:6d3ab15363a2 825
johnAlexander 7:6d3ab15363a2 826
johnAlexander 7:6d3ab15363a2 827 /* Write and read functions from I2C */
johnAlexander 7:6d3ab15363a2 828
johnAlexander 7:6d3ab15363a2 829
johnAlexander 7:6d3ab15363a2 830 VL53L1X_ERROR VL53L1X::vl53l1_write_multi(VL53L1_DEV Dev, uint16_t index, uint8_t *pdata, uint32_t count)
johnAlexander 7:6d3ab15363a2 831 {
johnAlexander 7:6d3ab15363a2 832 int status;
johnAlexander 7:6d3ab15363a2 833
johnAlexander 7:6d3ab15363a2 834 status = vl53l1_i2c_write(Dev->I2cDevAddr, index, pdata, (uint16_t)count);
johnAlexander 7:6d3ab15363a2 835 return status;
johnAlexander 7:6d3ab15363a2 836 }
johnAlexander 7:6d3ab15363a2 837
johnAlexander 7:6d3ab15363a2 838 VL53L1X_ERROR VL53L1X::vl53l1_read_multi(VL53L1_DEV Dev, uint16_t index, uint8_t *pdata, uint32_t count)
johnAlexander 7:6d3ab15363a2 839 {
johnAlexander 7:6d3ab15363a2 840 int status;
johnAlexander 7:6d3ab15363a2 841
johnAlexander 7:6d3ab15363a2 842 status = vl53l1_i2c_read(Dev->I2cDevAddr, index, pdata, (uint16_t)count);
johnAlexander 7:6d3ab15363a2 843
johnAlexander 7:6d3ab15363a2 844 return status;
johnAlexander 7:6d3ab15363a2 845 }
johnAlexander 7:6d3ab15363a2 846
johnAlexander 7:6d3ab15363a2 847
johnAlexander 7:6d3ab15363a2 848 VL53L1X_ERROR VL53L1X::vl53l1_wr_byte(VL53L1_DEV Dev, uint16_t index, uint8_t data)
johnAlexander 7:6d3ab15363a2 849 {
johnAlexander 7:6d3ab15363a2 850 int status;
johnAlexander 7:6d3ab15363a2 851
johnAlexander 7:6d3ab15363a2 852 status=vl53l1_i2c_write(Dev->I2cDevAddr, index, &data, 1);
johnAlexander 7:6d3ab15363a2 853 return status;
johnAlexander 7:6d3ab15363a2 854 }
johnAlexander 7:6d3ab15363a2 855
johnAlexander 7:6d3ab15363a2 856 VL53L1X_ERROR VL53L1X::vl53l1_wr_word(VL53L1_DEV Dev, uint16_t index, uint16_t data)
johnAlexander 7:6d3ab15363a2 857 {
johnAlexander 7:6d3ab15363a2 858 int status;
johnAlexander 7:6d3ab15363a2 859 uint8_t buffer[2];
johnAlexander 7:6d3ab15363a2 860
johnAlexander 7:6d3ab15363a2 861 buffer[0] = data >> 8;
johnAlexander 7:6d3ab15363a2 862 buffer[1] = data & 0x00FF;
johnAlexander 7:6d3ab15363a2 863 status=vl53l1_i2c_write(Dev->I2cDevAddr, index, (uint8_t *)buffer, 2);
johnAlexander 7:6d3ab15363a2 864 return status;
johnAlexander 7:6d3ab15363a2 865 }
johnAlexander 7:6d3ab15363a2 866
johnAlexander 7:6d3ab15363a2 867 VL53L1X_ERROR VL53L1X::vl53l1_wr_double_word(VL53L1_DEV Dev, uint16_t index, uint32_t data)
johnAlexander 7:6d3ab15363a2 868 {
johnAlexander 7:6d3ab15363a2 869 int status;
johnAlexander 7:6d3ab15363a2 870 uint8_t buffer[4];
johnAlexander 7:6d3ab15363a2 871
johnAlexander 7:6d3ab15363a2 872 buffer[0] = (data >> 24) & 0xFF;
johnAlexander 7:6d3ab15363a2 873 buffer[1] = (data >> 16) & 0xFF;
johnAlexander 7:6d3ab15363a2 874 buffer[2] = (data >> 8) & 0xFF;
johnAlexander 7:6d3ab15363a2 875 buffer[3] = (data >> 0) & 0xFF;
johnAlexander 7:6d3ab15363a2 876 status=vl53l1_i2c_write(Dev->I2cDevAddr, index, (uint8_t *)buffer, 4);
johnAlexander 7:6d3ab15363a2 877 return status;
johnAlexander 7:6d3ab15363a2 878 }
johnAlexander 7:6d3ab15363a2 879
johnAlexander 7:6d3ab15363a2 880
johnAlexander 7:6d3ab15363a2 881 VL53L1X_ERROR VL53L1X::vl53l1_rd_byte(VL53L1_DEV Dev, uint16_t index, uint8_t *data)
johnAlexander 7:6d3ab15363a2 882 {
johnAlexander 7:6d3ab15363a2 883 int status;
johnAlexander 7:6d3ab15363a2 884
johnAlexander 7:6d3ab15363a2 885 status = vl53l1_i2c_read(Dev->I2cDevAddr, index, data, 1);
johnAlexander 7:6d3ab15363a2 886
johnAlexander 7:6d3ab15363a2 887 if(status)
johnAlexander 7:6d3ab15363a2 888 return -1;
johnAlexander 7:6d3ab15363a2 889
johnAlexander 7:6d3ab15363a2 890 return 0;
johnAlexander 7:6d3ab15363a2 891 }
johnAlexander 7:6d3ab15363a2 892
johnAlexander 7:6d3ab15363a2 893 VL53L1X_ERROR VL53L1X::vl53l1_rd_word(VL53L1_DEV Dev, uint16_t index, uint16_t *data)
johnAlexander 7:6d3ab15363a2 894 {
johnAlexander 7:6d3ab15363a2 895 int status;
johnAlexander 7:6d3ab15363a2 896 uint8_t buffer[2] = {0,0};
johnAlexander 7:6d3ab15363a2 897
johnAlexander 7:6d3ab15363a2 898 status = vl53l1_i2c_read(Dev->I2cDevAddr, index, buffer, 2);
johnAlexander 7:6d3ab15363a2 899 if (!status)
johnAlexander 7:6d3ab15363a2 900 {
johnAlexander 7:6d3ab15363a2 901 *data = (buffer[0] << 8) + buffer[1];
johnAlexander 7:6d3ab15363a2 902 }
johnAlexander 7:6d3ab15363a2 903 return status;
johnAlexander 7:6d3ab15363a2 904
johnAlexander 7:6d3ab15363a2 905 }
johnAlexander 7:6d3ab15363a2 906
johnAlexander 7:6d3ab15363a2 907 VL53L1X_ERROR VL53L1X::vl53l1_rd_double_word(VL53L1_DEV Dev, uint16_t index, uint32_t *data)
johnAlexander 7:6d3ab15363a2 908 {
johnAlexander 7:6d3ab15363a2 909 int status;
johnAlexander 7:6d3ab15363a2 910 uint8_t buffer[4] = {0,0,0,0};
johnAlexander 7:6d3ab15363a2 911
johnAlexander 7:6d3ab15363a2 912 status = vl53l1_i2c_read(Dev->I2cDevAddr, index, buffer, 4);
johnAlexander 7:6d3ab15363a2 913 if(!status)
johnAlexander 7:6d3ab15363a2 914 {
johnAlexander 7:6d3ab15363a2 915 *data = (buffer[0] << 24) + (buffer[1] << 16) + (buffer[2] << 8) + buffer[3];
johnAlexander 7:6d3ab15363a2 916 }
johnAlexander 7:6d3ab15363a2 917 return status;
johnAlexander 7:6d3ab15363a2 918
johnAlexander 7:6d3ab15363a2 919 }
johnAlexander 7:6d3ab15363a2 920
johnAlexander 7:6d3ab15363a2 921 VL53L1X_ERROR VL53L1X::vl53l1_update_byte(VL53L1_DEV Dev, uint16_t index, uint8_t AndData, uint8_t OrData)
johnAlexander 7:6d3ab15363a2 922 {
johnAlexander 7:6d3ab15363a2 923 int status;
johnAlexander 7:6d3ab15363a2 924 uint8_t buffer = 0;
johnAlexander 7:6d3ab15363a2 925
johnAlexander 7:6d3ab15363a2 926 /* read data direct onto buffer */
johnAlexander 7:6d3ab15363a2 927 status = vl53l1_i2c_read(Dev->I2cDevAddr, index, &buffer,1);
johnAlexander 7:6d3ab15363a2 928 if (!status)
johnAlexander 7:6d3ab15363a2 929 {
johnAlexander 7:6d3ab15363a2 930 buffer = (buffer & AndData) | OrData;
johnAlexander 7:6d3ab15363a2 931 status = vl53l1_i2c_write(Dev->I2cDevAddr, index, &buffer, (uint16_t)1);
johnAlexander 7:6d3ab15363a2 932 }
johnAlexander 7:6d3ab15363a2 933 return status;
johnAlexander 7:6d3ab15363a2 934 }
johnAlexander 7:6d3ab15363a2 935
johnAlexander 7:6d3ab15363a2 936 VL53L1X_ERROR VL53L1X::vl53l1_i2c_write(uint8_t DeviceAddr, uint16_t RegisterAddr, uint8_t* pBuffer, uint16_t NumByteToWrite)
johnAlexander 7:6d3ab15363a2 937 {
johnAlexander 7:6d3ab15363a2 938 int ret;
johnAlexander 7:6d3ab15363a2 939 ret = dev_i2c->v53l1x_i2c_write(pBuffer, DeviceAddr, RegisterAddr, NumByteToWrite);
johnAlexander 7:6d3ab15363a2 940 if (ret) {
johnAlexander 7:6d3ab15363a2 941 return -1;
johnAlexander 7:6d3ab15363a2 942 }
johnAlexander 7:6d3ab15363a2 943 return 0;
johnAlexander 7:6d3ab15363a2 944
johnAlexander 7:6d3ab15363a2 945 }
johnAlexander 7:6d3ab15363a2 946
johnAlexander 7:6d3ab15363a2 947 VL53L1X_ERROR VL53L1X::vl53l1_i2c_read(uint8_t DeviceAddr, uint16_t RegisterAddr, uint8_t* pBuffer, uint16_t NumByteToRead)
johnAlexander 7:6d3ab15363a2 948 {
johnAlexander 7:6d3ab15363a2 949 int ret;
johnAlexander 7:6d3ab15363a2 950
johnAlexander 7:6d3ab15363a2 951 ret = dev_i2c->v53l1x_i2c_read(pBuffer, DeviceAddr, RegisterAddr, NumByteToRead);
johnAlexander 7:6d3ab15363a2 952
johnAlexander 7:6d3ab15363a2 953 if (ret) {
johnAlexander 7:6d3ab15363a2 954 return -1;
johnAlexander 7:6d3ab15363a2 955 }
johnAlexander 7:6d3ab15363a2 956 return 0;
johnAlexander 7:6d3ab15363a2 957 }
johnAlexander 7:6d3ab15363a2 958
johnAlexander 7:6d3ab15363a2 959
johnAlexander 7:6d3ab15363a2 960 VL53L1X_ERROR VL53L1X::vl53l1_get_tick_count(
johnAlexander 7:6d3ab15363a2 961 uint32_t *ptick_count_ms)
johnAlexander 7:6d3ab15363a2 962 {
johnAlexander 7:6d3ab15363a2 963
johnAlexander 7:6d3ab15363a2 964 /* Returns current tick count in [ms] */
johnAlexander 7:6d3ab15363a2 965
johnAlexander 7:6d3ab15363a2 966 VL53L1X_ERROR status = VL53L1_ERROR_NONE;
johnAlexander 7:6d3ab15363a2 967
johnAlexander 7:6d3ab15363a2 968 *ptick_count_ms = 0;
johnAlexander 7:6d3ab15363a2 969
johnAlexander 7:6d3ab15363a2 970 return status;
johnAlexander 7:6d3ab15363a2 971 }
johnAlexander 7:6d3ab15363a2 972
johnAlexander 7:6d3ab15363a2 973
johnAlexander 7:6d3ab15363a2 974
johnAlexander 7:6d3ab15363a2 975 VL53L1X_ERROR VL53L1X::vl53l1_wait_us(VL53L1_Dev_t *pdev, int32_t wait_us)
johnAlexander 7:6d3ab15363a2 976 {
johnAlexander 7:6d3ab15363a2 977 return VL53L1_ERROR_NONE;
johnAlexander 7:6d3ab15363a2 978 }
johnAlexander 7:6d3ab15363a2 979
johnAlexander 7:6d3ab15363a2 980
johnAlexander 7:6d3ab15363a2 981 VL53L1X_ERROR VL53L1X::vl53l1_wait_ms(VL53L1_Dev_t *pdev, int32_t wait_ms)
johnAlexander 7:6d3ab15363a2 982 {
johnAlexander 7:6d3ab15363a2 983 return VL53L1_ERROR_NONE;
johnAlexander 7:6d3ab15363a2 984 }
johnAlexander 7:6d3ab15363a2 985
johnAlexander 7:6d3ab15363a2 986
johnAlexander 7:6d3ab15363a2 987 VL53L1X_ERROR VL53L1X::vl53l1_wait_value_mask_ex(
johnAlexander 7:6d3ab15363a2 988 VL53L1_Dev_t *pdev,
johnAlexander 7:6d3ab15363a2 989 uint32_t timeout_ms,
johnAlexander 7:6d3ab15363a2 990 uint16_t index,
johnAlexander 7:6d3ab15363a2 991 uint8_t value,
johnAlexander 7:6d3ab15363a2 992 uint8_t mask,
johnAlexander 7:6d3ab15363a2 993 uint32_t poll_delay_ms)
johnAlexander 7:6d3ab15363a2 994 {
johnAlexander 7:6d3ab15363a2 995
johnAlexander 7:6d3ab15363a2 996 /*
johnAlexander 7:6d3ab15363a2 997 * Platform implementation of WaitValueMaskEx V2WReg script command
johnAlexander 7:6d3ab15363a2 998 *
johnAlexander 7:6d3ab15363a2 999 * WaitValueMaskEx(
johnAlexander 7:6d3ab15363a2 1000 * duration_ms,
johnAlexander 7:6d3ab15363a2 1001 * index,
johnAlexander 7:6d3ab15363a2 1002 * value,
johnAlexander 7:6d3ab15363a2 1003 * mask,
johnAlexander 7:6d3ab15363a2 1004 * poll_delay_ms);
johnAlexander 7:6d3ab15363a2 1005 */
johnAlexander 7:6d3ab15363a2 1006
johnAlexander 7:6d3ab15363a2 1007 VL53L1_Error status = VL53L1_ERROR_NONE;
johnAlexander 7:6d3ab15363a2 1008 uint32_t start_time_ms = 0;
johnAlexander 7:6d3ab15363a2 1009 uint32_t current_time_ms = 0;
johnAlexander 7:6d3ab15363a2 1010 uint32_t polling_time_ms = 0;
johnAlexander 7:6d3ab15363a2 1011 uint8_t byte_value = 0;
johnAlexander 7:6d3ab15363a2 1012 uint8_t found = 0;
johnAlexander 7:6d3ab15363a2 1013
johnAlexander 7:6d3ab15363a2 1014
johnAlexander 7:6d3ab15363a2 1015
johnAlexander 7:6d3ab15363a2 1016 /* calculate time limit in absolute time */
johnAlexander 7:6d3ab15363a2 1017
johnAlexander 7:6d3ab15363a2 1018 vl53l1_get_tick_count(&start_time_ms);
johnAlexander 7:6d3ab15363a2 1019
johnAlexander 7:6d3ab15363a2 1020 /* remember current trace functions and temporarily disable
johnAlexander 7:6d3ab15363a2 1021 * function logging
johnAlexander 7:6d3ab15363a2 1022 */
johnAlexander 7:6d3ab15363a2 1023
johnAlexander 7:6d3ab15363a2 1024
johnAlexander 7:6d3ab15363a2 1025 /* wait until value is found, timeout reached on error occurred */
johnAlexander 7:6d3ab15363a2 1026
johnAlexander 7:6d3ab15363a2 1027 while ((status == VL53L1_ERROR_NONE) &&
johnAlexander 7:6d3ab15363a2 1028 (polling_time_ms < timeout_ms) &&
johnAlexander 7:6d3ab15363a2 1029 (found == 0)) {
johnAlexander 7:6d3ab15363a2 1030
johnAlexander 7:6d3ab15363a2 1031 if (status == VL53L1_ERROR_NONE)
johnAlexander 7:6d3ab15363a2 1032 status = vl53l1_rd_byte(
johnAlexander 7:6d3ab15363a2 1033 pdev,
johnAlexander 7:6d3ab15363a2 1034 index,
johnAlexander 7:6d3ab15363a2 1035 &byte_value);
johnAlexander 7:6d3ab15363a2 1036
johnAlexander 7:6d3ab15363a2 1037 if ((byte_value & mask) == value)
johnAlexander 7:6d3ab15363a2 1038 found = 1;
johnAlexander 7:6d3ab15363a2 1039
johnAlexander 7:6d3ab15363a2 1040 if (status == VL53L1_ERROR_NONE &&
johnAlexander 7:6d3ab15363a2 1041 found == 0 &&
johnAlexander 7:6d3ab15363a2 1042 poll_delay_ms > 0)
johnAlexander 7:6d3ab15363a2 1043 status = vl53l1_wait_ms(
johnAlexander 7:6d3ab15363a2 1044 pdev,
johnAlexander 7:6d3ab15363a2 1045 poll_delay_ms);
johnAlexander 7:6d3ab15363a2 1046
johnAlexander 7:6d3ab15363a2 1047 /* Update polling time (Compare difference rather than absolute to
johnAlexander 7:6d3ab15363a2 1048 negate 32bit wrap around issue) */
johnAlexander 7:6d3ab15363a2 1049 vl53l1_get_tick_count(&current_time_ms);
johnAlexander 7:6d3ab15363a2 1050 polling_time_ms = current_time_ms - start_time_ms;
johnAlexander 7:6d3ab15363a2 1051
johnAlexander 7:6d3ab15363a2 1052 }
johnAlexander 7:6d3ab15363a2 1053
johnAlexander 7:6d3ab15363a2 1054
johnAlexander 7:6d3ab15363a2 1055 if (found == 0 && status == VL53L1_ERROR_NONE)
johnAlexander 7:6d3ab15363a2 1056 status = VL53L1_ERROR_TIME_OUT;
johnAlexander 7:6d3ab15363a2 1057
johnAlexander 7:6d3ab15363a2 1058 return status;
johnAlexander 7:6d3ab15363a2 1059 }
johnAlexander 7:6d3ab15363a2 1060
johnAlexander 7:6d3ab15363a2 1061 int VL53L1X::handle_irq(uint16_t *distance)
johnAlexander 7:6d3ab15363a2 1062 {
johnAlexander 7:6d3ab15363a2 1063 int status;
johnAlexander 7:6d3ab15363a2 1064 status = get_measurement(distance);
johnAlexander 7:6d3ab15363a2 1065 enable_interrupt_measure_detection_irq();
johnAlexander 7:6d3ab15363a2 1066 return status;
johnAlexander 7:6d3ab15363a2 1067 }
johnAlexander 7:6d3ab15363a2 1068
johnAlexander 7:6d3ab15363a2 1069 int VL53L1X::get_measurement(uint16_t *distance)
johnAlexander 7:6d3ab15363a2 1070 {
johnAlexander 7:6d3ab15363a2 1071 int status = 0;
johnAlexander 7:6d3ab15363a2 1072
johnAlexander 7:6d3ab15363a2 1073 status = vl53l1x_get_distance(distance);
johnAlexander 7:6d3ab15363a2 1074 status = vl53l1x_clear_interrupt();
johnAlexander 7:6d3ab15363a2 1075
johnAlexander 7:6d3ab15363a2 1076 return status;
johnAlexander 7:6d3ab15363a2 1077 }
johnAlexander 7:6d3ab15363a2 1078
johnAlexander 7:6d3ab15363a2 1079 int VL53L1X::start_measurement(void (*fptr)(void))
johnAlexander 7:6d3ab15363a2 1080 {
johnAlexander 7:6d3ab15363a2 1081 int status = 0;
johnAlexander 7:6d3ab15363a2 1082
johnAlexander 7:6d3ab15363a2 1083 if (_gpio1Int == NULL) {
johnAlexander 7:6d3ab15363a2 1084 printf("GPIO1 Error\r\n");
johnAlexander 7:6d3ab15363a2 1085 return 1;
johnAlexander 7:6d3ab15363a2 1086 }
johnAlexander 7:6d3ab15363a2 1087
johnAlexander 7:6d3ab15363a2 1088 status = vl53l1x_stop_ranging(); // it is safer to do this while sensor is stopped
johnAlexander 7:6d3ab15363a2 1089
johnAlexander 7:6d3ab15363a2 1090 if (status == 0) {
johnAlexander 7:6d3ab15363a2 1091 attach_interrupt_measure_detection_irq(fptr);
johnAlexander 7:6d3ab15363a2 1092 enable_interrupt_measure_detection_irq();
johnAlexander 7:6d3ab15363a2 1093 }
johnAlexander 7:6d3ab15363a2 1094
johnAlexander 7:6d3ab15363a2 1095 if (status == 0) {
johnAlexander 7:6d3ab15363a2 1096 status = vl53l1x_start_ranging();
johnAlexander 7:6d3ab15363a2 1097 }
johnAlexander 7:6d3ab15363a2 1098
johnAlexander 7:6d3ab15363a2 1099 return status;
johnAlexander 7:6d3ab15363a2 1100 }
johnAlexander 7:6d3ab15363a2 1101
johnAlexander 7:6d3ab15363a2 1102 int VL53L1X::stop_measurement()
johnAlexander 7:6d3ab15363a2 1103 {
johnAlexander 7:6d3ab15363a2 1104 int status = 0;
johnAlexander 7:6d3ab15363a2 1105
johnAlexander 7:6d3ab15363a2 1106 if (status == 0) {
johnAlexander 7:6d3ab15363a2 1107 printf("Call of VL53L0X_StopMeasurement\n");
johnAlexander 7:6d3ab15363a2 1108 status = vl53l1x_stop_ranging();
johnAlexander 7:6d3ab15363a2 1109 }
johnAlexander 7:6d3ab15363a2 1110
johnAlexander 7:6d3ab15363a2 1111 if (status == 0)
johnAlexander 7:6d3ab15363a2 1112 status = vl53l1x_clear_interrupt();
johnAlexander 7:6d3ab15363a2 1113
johnAlexander 7:6d3ab15363a2 1114 return status;
johnAlexander 7:6d3ab15363a2 1115 }