Updates to follow mbed SDK coding style guidelines.

Dependencies:   ST_INTERFACES X_NUCLEO_COMMON

Dependents:   53L0A1_Satellites_with_Interrupts_OS5 Display_53L0A1_OS5

Fork of X_NUCLEO_53L0A1 by ST

Committer:
JerrySzczurak
Date:
Wed Jun 28 12:23:03 2017 +0000
Revision:
21:627b65069e6d
Parent:
20:cf211a3b3d9e
Cosmetic changes to v53l0x class; wrWord and rdWord calls changed to write_word and read_word

Who changed what in which revision?

UserRevisionLine numberNew 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>&copy; 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 15:932d8b4e52c9 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 15:932d8b4e52c9 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 15:932d8b4e52c9 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 15:932d8b4e52c9 77 REF_ARRAY_SPAD_0, REF_ARRAY_SPAD_5
johnAlexander 15:932d8b4e52c9 78 };
johnAlexander 0:c523920bcc09 79
johnAlexander 0:c523920bcc09 80
johnAlexander 0:c523920bcc09 81
johnAlexander 0:c523920bcc09 82
JerrySzczurak 20:cf211a3b3d9e 83 VL53L0X_Error VL53L0X::vl53l0x_device_read_strobe( VL53L0X_DEV dev )
johnAlexander 0:c523920bcc09 84 {
johnAlexander 16:98ce55ddbb1a 85 VL53L0X_Error status = VL53L0X_ERROR_NONE;
johnAlexander 15:932d8b4e52c9 86 uint8_t strobe;
johnAlexander 16:98ce55ddbb1a 87 uint32_t loop_nb;
JerrySzczurak 20:cf211a3b3d9e 88 LOG_FUNCTION_START( "" );
JerrySzczurak 20:cf211a3b3d9e 89
JerrySzczurak 20:cf211a3b3d9e 90 status |= vl53l0x_write_byte( dev, 0x83, 0x00 );
johnAlexander 15:932d8b4e52c9 91
johnAlexander 15:932d8b4e52c9 92 /* polling
johnAlexander 15:932d8b4e52c9 93 * use timeout to avoid deadlock*/
JerrySzczurak 20:cf211a3b3d9e 94 if ( status == VL53L0X_ERROR_NONE ) {
johnAlexander 16:98ce55ddbb1a 95 loop_nb = 0;
johnAlexander 15:932d8b4e52c9 96 do {
JerrySzczurak 20:cf211a3b3d9e 97 status = vl53l0x_read_byte( dev, 0x83, &strobe );
JerrySzczurak 20:cf211a3b3d9e 98 if ( ( strobe != 0x00 ) || status != VL53L0X_ERROR_NONE )
johnAlexander 15:932d8b4e52c9 99 break;
johnAlexander 15:932d8b4e52c9 100
johnAlexander 16:98ce55ddbb1a 101 loop_nb = loop_nb + 1;
JerrySzczurak 20:cf211a3b3d9e 102 } while ( loop_nb < VL53L0X_DEFAULT_MAX_LOOP );
JerrySzczurak 20:cf211a3b3d9e 103
JerrySzczurak 20:cf211a3b3d9e 104 if ( loop_nb >= VL53L0X_DEFAULT_MAX_LOOP )
johnAlexander 16:98ce55ddbb1a 105 status = VL53L0X_ERROR_TIME_OUT;
johnAlexander 15:932d8b4e52c9 106
johnAlexander 15:932d8b4e52c9 107 }
johnAlexander 15:932d8b4e52c9 108
JerrySzczurak 20:cf211a3b3d9e 109 status |= vl53l0x_write_byte( dev, 0x83, 0x01 );
JerrySzczurak 20:cf211a3b3d9e 110
JerrySzczurak 20:cf211a3b3d9e 111 LOG_FUNCTION_END( status );
JerrySzczurak 20:cf211a3b3d9e 112 return status;
JerrySzczurak 20:cf211a3b3d9e 113
JerrySzczurak 20:cf211a3b3d9e 114 }
JerrySzczurak 20:cf211a3b3d9e 115
JerrySzczurak 20:cf211a3b3d9e 116 VL53L0X_Error VL53L0X::vl53l0x_get_info_from_device( VL53L0X_DEV dev, uint8_t option )
johnAlexander 0:c523920bcc09 117 {
johnAlexander 0:c523920bcc09 118
johnAlexander 16:98ce55ddbb1a 119 VL53L0X_Error status = VL53L0X_ERROR_NONE;
johnAlexander 15:932d8b4e52c9 120 uint8_t byte;
johnAlexander 16:98ce55ddbb1a 121 uint32_t tmp_dword;
johnAlexander 16:98ce55ddbb1a 122 uint8_t module_id;
johnAlexander 16:98ce55ddbb1a 123 uint8_t revision;
johnAlexander 16:98ce55ddbb1a 124 uint8_t reference_spad_count = 0;
johnAlexander 16:98ce55ddbb1a 125 uint8_t reference_spad_type = 0;
johnAlexander 16:98ce55ddbb1a 126 uint32_t part_uid_upper = 0;
johnAlexander 16:98ce55ddbb1a 127 uint32_t part_uid_lower = 0;
johnAlexander 16:98ce55ddbb1a 128 uint32_t offset_fixed1104_mm = 0;
johnAlexander 16:98ce55ddbb1a 129 int16_t offset_micro_meters = 0;
johnAlexander 16:98ce55ddbb1a 130 uint32_t dist_meas_tgt_fixed1104_mm = 400 << 4;
johnAlexander 16:98ce55ddbb1a 131 uint32_t dist_meas_fixed1104_400_mm = 0;
johnAlexander 16:98ce55ddbb1a 132 uint32_t signal_rate_meas_fixed1104_400_mm = 0;
johnAlexander 16:98ce55ddbb1a 133 char product_id[19];
johnAlexander 16:98ce55ddbb1a 134 char *product_id_tmp;
johnAlexander 16:98ce55ddbb1a 135 uint8_t read_data_from_device_done;
johnAlexander 16:98ce55ddbb1a 136 FixPoint1616_t signal_rate_meas_fixed400_mm_fix = 0;
johnAlexander 16:98ce55ddbb1a 137 uint8_t nvm_ref_good_spad_map[VL53L0X_REF_SPAD_BUFFER_SIZE];
johnAlexander 15:932d8b4e52c9 138 int i;
johnAlexander 15:932d8b4e52c9 139
johnAlexander 15:932d8b4e52c9 140
JerrySzczurak 20:cf211a3b3d9e 141 LOG_FUNCTION_START( "" );
JerrySzczurak 20:cf211a3b3d9e 142
JerrySzczurak 20:cf211a3b3d9e 143 read_data_from_device_done = VL53L0X_GETDEVICESPECIFICPARAMETER( dev,
JerrySzczurak 20:cf211a3b3d9e 144 ReadDataFromDeviceDone );
johnAlexander 15:932d8b4e52c9 145
johnAlexander 15:932d8b4e52c9 146 /* This access is done only once after that a GetDeviceInfo or
johnAlexander 15:932d8b4e52c9 147 * datainit is done*/
JerrySzczurak 20:cf211a3b3d9e 148 if ( read_data_from_device_done != 7 ) {
JerrySzczurak 20:cf211a3b3d9e 149
JerrySzczurak 20:cf211a3b3d9e 150 status |= vl53l0x_write_byte( dev, 0x80, 0x01 );
JerrySzczurak 20:cf211a3b3d9e 151 status |= vl53l0x_write_byte( dev, 0xFF, 0x01 );
JerrySzczurak 20:cf211a3b3d9e 152 status |= vl53l0x_write_byte( dev, 0x00, 0x00 );
JerrySzczurak 20:cf211a3b3d9e 153
JerrySzczurak 20:cf211a3b3d9e 154 status |= vl53l0x_write_byte( dev, 0xFF, 0x06 );
JerrySzczurak 20:cf211a3b3d9e 155 status |= vl53l0x_read_byte( dev, 0x83, &byte );
JerrySzczurak 20:cf211a3b3d9e 156 status |= vl53l0x_write_byte( dev, 0x83, byte | 4 );
JerrySzczurak 20:cf211a3b3d9e 157 status |= vl53l0x_write_byte( dev, 0xFF, 0x07 );
JerrySzczurak 20:cf211a3b3d9e 158 status |= vl53l0x_write_byte( dev, 0x81, 0x01 );
JerrySzczurak 20:cf211a3b3d9e 159
JerrySzczurak 20:cf211a3b3d9e 160 status |= vl53l0x_polling_delay( dev );
JerrySzczurak 20:cf211a3b3d9e 161
JerrySzczurak 20:cf211a3b3d9e 162 status |= vl53l0x_write_byte( dev, 0x80, 0x01 );
JerrySzczurak 20:cf211a3b3d9e 163
JerrySzczurak 20:cf211a3b3d9e 164 if ( ( ( option & 1 ) == 1 ) &&
JerrySzczurak 20:cf211a3b3d9e 165 ( ( read_data_from_device_done & 1 ) == 0 ) ) {
JerrySzczurak 20:cf211a3b3d9e 166 status |= vl53l0x_write_byte( dev, 0x94, 0x6b );
JerrySzczurak 20:cf211a3b3d9e 167 status |= vl53l0x_device_read_strobe( dev );
JerrySzczurak 20:cf211a3b3d9e 168 status |= vl53l0x_read_dword( dev, 0x90, &tmp_dword );
JerrySzczurak 20:cf211a3b3d9e 169
JerrySzczurak 20:cf211a3b3d9e 170 reference_spad_count = ( uint8_t )( ( tmp_dword >> 8 ) & 0x07f );
JerrySzczurak 20:cf211a3b3d9e 171 reference_spad_type = ( uint8_t )( ( tmp_dword >> 15 ) & 0x01 );
JerrySzczurak 20:cf211a3b3d9e 172
JerrySzczurak 20:cf211a3b3d9e 173 status |= vl53l0x_write_byte( dev, 0x94, 0x24 );
JerrySzczurak 20:cf211a3b3d9e 174 status |= vl53l0x_device_read_strobe( dev );
JerrySzczurak 20:cf211a3b3d9e 175 status |= vl53l0x_read_dword( dev, 0x90, &tmp_dword );
JerrySzczurak 20:cf211a3b3d9e 176
JerrySzczurak 20:cf211a3b3d9e 177
JerrySzczurak 20:cf211a3b3d9e 178 nvm_ref_good_spad_map[0] = ( uint8_t )( ( tmp_dword >> 24 )
JerrySzczurak 20:cf211a3b3d9e 179 & 0xff );
JerrySzczurak 20:cf211a3b3d9e 180 nvm_ref_good_spad_map[1] = ( uint8_t )( ( tmp_dword >> 16 )
JerrySzczurak 20:cf211a3b3d9e 181 & 0xff );
JerrySzczurak 20:cf211a3b3d9e 182 nvm_ref_good_spad_map[2] = ( uint8_t )( ( tmp_dword >> 8 )
JerrySzczurak 20:cf211a3b3d9e 183 & 0xff );
JerrySzczurak 20:cf211a3b3d9e 184 nvm_ref_good_spad_map[3] = ( uint8_t )( tmp_dword & 0xff );
JerrySzczurak 20:cf211a3b3d9e 185
JerrySzczurak 20:cf211a3b3d9e 186 status |= vl53l0x_write_byte( dev, 0x94, 0x25 );
JerrySzczurak 20:cf211a3b3d9e 187 status |= vl53l0x_device_read_strobe( dev );
JerrySzczurak 20:cf211a3b3d9e 188 status |= vl53l0x_read_dword( dev, 0x90, &tmp_dword );
JerrySzczurak 20:cf211a3b3d9e 189
JerrySzczurak 20:cf211a3b3d9e 190 nvm_ref_good_spad_map[4] = ( uint8_t )( ( tmp_dword >> 24 )
JerrySzczurak 20:cf211a3b3d9e 191 & 0xff );
JerrySzczurak 20:cf211a3b3d9e 192 nvm_ref_good_spad_map[5] = ( uint8_t )( ( tmp_dword >> 16 )
JerrySzczurak 20:cf211a3b3d9e 193 & 0xff );
johnAlexander 15:932d8b4e52c9 194 }
johnAlexander 15:932d8b4e52c9 195
JerrySzczurak 20:cf211a3b3d9e 196 if ( ( ( option & 2 ) == 2 ) &&
JerrySzczurak 20:cf211a3b3d9e 197 ( ( read_data_from_device_done & 2 ) == 0 ) ) {
JerrySzczurak 20:cf211a3b3d9e 198
JerrySzczurak 20:cf211a3b3d9e 199 status |= vl53l0x_write_byte( dev, 0x94, 0x02 );
JerrySzczurak 20:cf211a3b3d9e 200 status |= vl53l0x_device_read_strobe( dev );
JerrySzczurak 20:cf211a3b3d9e 201 status |= vl53l0x_read_byte( dev, 0x90, &module_id );
JerrySzczurak 20:cf211a3b3d9e 202
JerrySzczurak 20:cf211a3b3d9e 203 status |= vl53l0x_write_byte( dev, 0x94, 0x7B );
JerrySzczurak 20:cf211a3b3d9e 204 status |= vl53l0x_device_read_strobe( dev );
JerrySzczurak 20:cf211a3b3d9e 205 status |= vl53l0x_read_byte( dev, 0x90, &revision );
JerrySzczurak 20:cf211a3b3d9e 206
JerrySzczurak 20:cf211a3b3d9e 207 status |= vl53l0x_write_byte( dev, 0x94, 0x77 );
JerrySzczurak 20:cf211a3b3d9e 208 status |= vl53l0x_device_read_strobe( dev );
JerrySzczurak 20:cf211a3b3d9e 209 status |= vl53l0x_read_dword( dev, 0x90, &tmp_dword );
JerrySzczurak 20:cf211a3b3d9e 210
JerrySzczurak 20:cf211a3b3d9e 211 product_id[0] = ( char )( ( tmp_dword >> 25 ) & 0x07f );
JerrySzczurak 20:cf211a3b3d9e 212 product_id[1] = ( char )( ( tmp_dword >> 18 ) & 0x07f );
JerrySzczurak 20:cf211a3b3d9e 213 product_id[2] = ( char )( ( tmp_dword >> 11 ) & 0x07f );
JerrySzczurak 20:cf211a3b3d9e 214 product_id[3] = ( char )( ( tmp_dword >> 4 ) & 0x07f );
JerrySzczurak 20:cf211a3b3d9e 215
JerrySzczurak 20:cf211a3b3d9e 216 byte = ( uint8_t )( ( tmp_dword & 0x00f ) << 3 );
JerrySzczurak 20:cf211a3b3d9e 217
JerrySzczurak 20:cf211a3b3d9e 218 status |= vl53l0x_write_byte( dev, 0x94, 0x78 );
JerrySzczurak 20:cf211a3b3d9e 219 status |= vl53l0x_device_read_strobe( dev );
JerrySzczurak 20:cf211a3b3d9e 220 status |= vl53l0x_read_dword( dev, 0x90, &tmp_dword );
JerrySzczurak 20:cf211a3b3d9e 221
JerrySzczurak 20:cf211a3b3d9e 222 product_id[4] = ( char )( byte +
JerrySzczurak 20:cf211a3b3d9e 223 ( ( tmp_dword >> 29 ) & 0x07f ) );
JerrySzczurak 20:cf211a3b3d9e 224 product_id[5] = ( char )( ( tmp_dword >> 22 ) & 0x07f );
JerrySzczurak 20:cf211a3b3d9e 225 product_id[6] = ( char )( ( tmp_dword >> 15 ) & 0x07f );
JerrySzczurak 20:cf211a3b3d9e 226 product_id[7] = ( char )( ( tmp_dword >> 8 ) & 0x07f );
JerrySzczurak 20:cf211a3b3d9e 227 product_id[8] = ( char )( ( tmp_dword >> 1 ) & 0x07f );
JerrySzczurak 20:cf211a3b3d9e 228
JerrySzczurak 20:cf211a3b3d9e 229 byte = ( uint8_t )( ( tmp_dword & 0x001 ) << 6 );
JerrySzczurak 20:cf211a3b3d9e 230
JerrySzczurak 20:cf211a3b3d9e 231 status |= vl53l0x_write_byte( dev, 0x94, 0x79 );
JerrySzczurak 20:cf211a3b3d9e 232
JerrySzczurak 20:cf211a3b3d9e 233 status |= vl53l0x_device_read_strobe( dev );
JerrySzczurak 20:cf211a3b3d9e 234
JerrySzczurak 20:cf211a3b3d9e 235 status |= vl53l0x_read_dword( dev, 0x90, &tmp_dword );
JerrySzczurak 20:cf211a3b3d9e 236
JerrySzczurak 20:cf211a3b3d9e 237 product_id[9] = ( char )( byte +
JerrySzczurak 20:cf211a3b3d9e 238 ( ( tmp_dword >> 26 ) & 0x07f ) );
JerrySzczurak 20:cf211a3b3d9e 239 product_id[10] = ( char )( ( tmp_dword >> 19 ) & 0x07f );
JerrySzczurak 20:cf211a3b3d9e 240 product_id[11] = ( char )( ( tmp_dword >> 12 ) & 0x07f );
JerrySzczurak 20:cf211a3b3d9e 241 product_id[12] = ( char )( ( tmp_dword >> 5 ) & 0x07f );
JerrySzczurak 20:cf211a3b3d9e 242
JerrySzczurak 20:cf211a3b3d9e 243 byte = ( uint8_t )( ( tmp_dword & 0x01f ) << 2 );
JerrySzczurak 20:cf211a3b3d9e 244
JerrySzczurak 20:cf211a3b3d9e 245 status |= vl53l0x_write_byte( dev, 0x94, 0x7A );
JerrySzczurak 20:cf211a3b3d9e 246
JerrySzczurak 20:cf211a3b3d9e 247 status |= vl53l0x_device_read_strobe( dev );
JerrySzczurak 20:cf211a3b3d9e 248
JerrySzczurak 20:cf211a3b3d9e 249 status |= vl53l0x_read_dword( dev, 0x90, &tmp_dword );
JerrySzczurak 20:cf211a3b3d9e 250
JerrySzczurak 20:cf211a3b3d9e 251 product_id[13] = ( char )( byte +
JerrySzczurak 20:cf211a3b3d9e 252 ( ( tmp_dword >> 30 ) & 0x07f ) );
JerrySzczurak 20:cf211a3b3d9e 253 product_id[14] = ( char )( ( tmp_dword >> 23 ) & 0x07f );
JerrySzczurak 20:cf211a3b3d9e 254 product_id[15] = ( char )( ( tmp_dword >> 16 ) & 0x07f );
JerrySzczurak 20:cf211a3b3d9e 255 product_id[16] = ( char )( ( tmp_dword >> 9 ) & 0x07f );
JerrySzczurak 20:cf211a3b3d9e 256 product_id[17] = ( char )( ( tmp_dword >> 2 ) & 0x07f );
johnAlexander 16:98ce55ddbb1a 257 product_id[18] = '\0';
johnAlexander 15:932d8b4e52c9 258
johnAlexander 15:932d8b4e52c9 259 }
johnAlexander 15:932d8b4e52c9 260
JerrySzczurak 20:cf211a3b3d9e 261 if ( ( ( option & 4 ) == 4 ) &&
JerrySzczurak 20:cf211a3b3d9e 262 ( ( read_data_from_device_done & 4 ) == 0 ) ) {
JerrySzczurak 20:cf211a3b3d9e 263
JerrySzczurak 20:cf211a3b3d9e 264 status |= vl53l0x_write_byte( dev, 0x94, 0x7B );
JerrySzczurak 20:cf211a3b3d9e 265 status |= vl53l0x_device_read_strobe( dev );
JerrySzczurak 20:cf211a3b3d9e 266 status |= vl53l0x_read_dword( dev, 0x90, &part_uid_upper );
JerrySzczurak 20:cf211a3b3d9e 267
JerrySzczurak 20:cf211a3b3d9e 268 status |= vl53l0x_write_byte( dev, 0x94, 0x7C );
JerrySzczurak 20:cf211a3b3d9e 269 status |= vl53l0x_device_read_strobe( dev );
JerrySzczurak 20:cf211a3b3d9e 270 status |= vl53l0x_read_dword( dev, 0x90, &part_uid_lower );
JerrySzczurak 20:cf211a3b3d9e 271
JerrySzczurak 20:cf211a3b3d9e 272 status |= vl53l0x_write_byte( dev, 0x94, 0x73 );
JerrySzczurak 20:cf211a3b3d9e 273 status |= vl53l0x_device_read_strobe( dev );
JerrySzczurak 20:cf211a3b3d9e 274 status |= vl53l0x_read_dword( dev, 0x90, &tmp_dword );
JerrySzczurak 20:cf211a3b3d9e 275
JerrySzczurak 20:cf211a3b3d9e 276 signal_rate_meas_fixed1104_400_mm = ( tmp_dword &
JerrySzczurak 20:cf211a3b3d9e 277 0x0000000ff ) << 8;
JerrySzczurak 20:cf211a3b3d9e 278
JerrySzczurak 20:cf211a3b3d9e 279 status |= vl53l0x_write_byte( dev, 0x94, 0x74 );
JerrySzczurak 20:cf211a3b3d9e 280 status |= vl53l0x_device_read_strobe( dev );
JerrySzczurak 20:cf211a3b3d9e 281 status |= vl53l0x_read_dword( dev, 0x90, &tmp_dword );
JerrySzczurak 20:cf211a3b3d9e 282
JerrySzczurak 20:cf211a3b3d9e 283 signal_rate_meas_fixed1104_400_mm |= ( ( tmp_dword &
JerrySzczurak 20:cf211a3b3d9e 284 0xff000000 ) >> 24 );
JerrySzczurak 20:cf211a3b3d9e 285
JerrySzczurak 20:cf211a3b3d9e 286 status |= vl53l0x_write_byte( dev, 0x94, 0x75 );
JerrySzczurak 20:cf211a3b3d9e 287 status |= vl53l0x_device_read_strobe( dev );
JerrySzczurak 20:cf211a3b3d9e 288 status |= vl53l0x_read_dword( dev, 0x90, &tmp_dword );
JerrySzczurak 20:cf211a3b3d9e 289
JerrySzczurak 20:cf211a3b3d9e 290 dist_meas_fixed1104_400_mm = ( tmp_dword & 0x0000000ff )
johnAlexander 16:98ce55ddbb1a 291 << 8;
johnAlexander 16:98ce55ddbb1a 292
JerrySzczurak 20:cf211a3b3d9e 293 status |= vl53l0x_write_byte( dev, 0x94, 0x76 );
JerrySzczurak 20:cf211a3b3d9e 294 status |= vl53l0x_device_read_strobe( dev );
JerrySzczurak 20:cf211a3b3d9e 295 status |= vl53l0x_read_dword( dev, 0x90, &tmp_dword );
JerrySzczurak 20:cf211a3b3d9e 296
JerrySzczurak 20:cf211a3b3d9e 297 dist_meas_fixed1104_400_mm |= ( ( tmp_dword & 0xff000000 )
JerrySzczurak 20:cf211a3b3d9e 298 >> 24 );
johnAlexander 15:932d8b4e52c9 299 }
johnAlexander 15:932d8b4e52c9 300
JerrySzczurak 20:cf211a3b3d9e 301 status |= vl53l0x_write_byte( dev, 0x81, 0x00 );
JerrySzczurak 20:cf211a3b3d9e 302 status |= vl53l0x_write_byte( dev, 0xFF, 0x06 );
JerrySzczurak 20:cf211a3b3d9e 303 status |= vl53l0x_read_byte( dev, 0x83, &byte );
JerrySzczurak 20:cf211a3b3d9e 304 status |= vl53l0x_write_byte( dev, 0x83, byte & 0xfb );
JerrySzczurak 20:cf211a3b3d9e 305 status |= vl53l0x_write_byte( dev, 0xFF, 0x01 );
JerrySzczurak 20:cf211a3b3d9e 306 status |= vl53l0x_write_byte( dev, 0x00, 0x01 );
JerrySzczurak 20:cf211a3b3d9e 307
JerrySzczurak 20:cf211a3b3d9e 308 status |= vl53l0x_write_byte( dev, 0xFF, 0x00 );
JerrySzczurak 20:cf211a3b3d9e 309 status |= vl53l0x_write_byte( dev, 0x80, 0x00 );
JerrySzczurak 20:cf211a3b3d9e 310 }
JerrySzczurak 20:cf211a3b3d9e 311
JerrySzczurak 20:cf211a3b3d9e 312 if ( ( status == VL53L0X_ERROR_NONE ) &&
JerrySzczurak 20:cf211a3b3d9e 313 ( read_data_from_device_done != 7 ) ) {
johnAlexander 15:932d8b4e52c9 314 /* Assign to variable if status is ok */
JerrySzczurak 20:cf211a3b3d9e 315 if ( ( ( option & 1 ) == 1 ) &&
JerrySzczurak 20:cf211a3b3d9e 316 ( ( read_data_from_device_done & 1 ) == 0 ) ) {
JerrySzczurak 20:cf211a3b3d9e 317 VL53L0X_SETDEVICESPECIFICPARAMETER( dev,
JerrySzczurak 20:cf211a3b3d9e 318 ReferenceSpadCount, reference_spad_count );
JerrySzczurak 20:cf211a3b3d9e 319
JerrySzczurak 20:cf211a3b3d9e 320 VL53L0X_SETDEVICESPECIFICPARAMETER( dev,
JerrySzczurak 20:cf211a3b3d9e 321 ReferenceSpadType, reference_spad_type );
JerrySzczurak 20:cf211a3b3d9e 322
JerrySzczurak 20:cf211a3b3d9e 323 for ( i = 0; i < VL53L0X_REF_SPAD_BUFFER_SIZE; i++ ) {
johnAlexander 16:98ce55ddbb1a 324 dev->Data.SpadData.RefGoodSpadMap[i] =
johnAlexander 16:98ce55ddbb1a 325 nvm_ref_good_spad_map[i];
johnAlexander 15:932d8b4e52c9 326 }
johnAlexander 15:932d8b4e52c9 327 }
johnAlexander 15:932d8b4e52c9 328
JerrySzczurak 20:cf211a3b3d9e 329 if ( ( ( option & 2 ) == 2 ) &&
JerrySzczurak 20:cf211a3b3d9e 330 ( ( read_data_from_device_done & 2 ) == 0 ) ) {
JerrySzczurak 20:cf211a3b3d9e 331 VL53L0X_SETDEVICESPECIFICPARAMETER( dev,
JerrySzczurak 20:cf211a3b3d9e 332 ModuleId, module_id );
JerrySzczurak 20:cf211a3b3d9e 333
JerrySzczurak 20:cf211a3b3d9e 334 VL53L0X_SETDEVICESPECIFICPARAMETER( dev,
JerrySzczurak 20:cf211a3b3d9e 335 Revision, revision );
JerrySzczurak 20:cf211a3b3d9e 336
JerrySzczurak 20:cf211a3b3d9e 337 product_id_tmp = VL53L0X_GETDEVICESPECIFICPARAMETER( dev,
JerrySzczurak 20:cf211a3b3d9e 338 ProductId );
JerrySzczurak 20:cf211a3b3d9e 339 VL53L0X_COPYSTRING( product_id_tmp, product_id );
johnAlexander 15:932d8b4e52c9 340
johnAlexander 15:932d8b4e52c9 341 }
johnAlexander 15:932d8b4e52c9 342
JerrySzczurak 20:cf211a3b3d9e 343 if ( ( ( option & 4 ) == 4 ) &&
JerrySzczurak 20:cf211a3b3d9e 344 ( ( read_data_from_device_done & 4 ) == 0 ) ) {
JerrySzczurak 20:cf211a3b3d9e 345 VL53L0X_SETDEVICESPECIFICPARAMETER( dev,
JerrySzczurak 20:cf211a3b3d9e 346 PartUIDUpper, part_uid_upper );
JerrySzczurak 20:cf211a3b3d9e 347
JerrySzczurak 20:cf211a3b3d9e 348 VL53L0X_SETDEVICESPECIFICPARAMETER( dev,
JerrySzczurak 20:cf211a3b3d9e 349 PartUIDLower, part_uid_lower );
johnAlexander 16:98ce55ddbb1a 350
johnAlexander 16:98ce55ddbb1a 351 signal_rate_meas_fixed400_mm_fix =
johnAlexander 15:932d8b4e52c9 352 VL53L0X_FIXPOINT97TOFIXPOINT1616(
JerrySzczurak 20:cf211a3b3d9e 353 signal_rate_meas_fixed1104_400_mm );
JerrySzczurak 20:cf211a3b3d9e 354
JerrySzczurak 20:cf211a3b3d9e 355 VL53L0X_SETDEVICESPECIFICPARAMETER( dev,
JerrySzczurak 20:cf211a3b3d9e 356 SignalRateMeasFixed400mm,
JerrySzczurak 20:cf211a3b3d9e 357 signal_rate_meas_fixed400_mm_fix );
johnAlexander 16:98ce55ddbb1a 358
johnAlexander 16:98ce55ddbb1a 359 offset_micro_meters = 0;
JerrySzczurak 20:cf211a3b3d9e 360 if ( dist_meas_fixed1104_400_mm != 0 ) {
johnAlexander 16:98ce55ddbb1a 361 offset_fixed1104_mm =
johnAlexander 16:98ce55ddbb1a 362 dist_meas_fixed1104_400_mm -
johnAlexander 16:98ce55ddbb1a 363 dist_meas_tgt_fixed1104_mm;
JerrySzczurak 20:cf211a3b3d9e 364 offset_micro_meters = ( offset_fixed1104_mm
JerrySzczurak 20:cf211a3b3d9e 365 * 1000 ) >> 4;
johnAlexander 16:98ce55ddbb1a 366 offset_micro_meters *= -1;
johnAlexander 15:932d8b4e52c9 367 }
johnAlexander 15:932d8b4e52c9 368
JerrySzczurak 20:cf211a3b3d9e 369 PALDevDataSet( dev,
JerrySzczurak 20:cf211a3b3d9e 370 Part2PartOffsetAdjustmentNVMMicroMeter,
JerrySzczurak 20:cf211a3b3d9e 371 offset_micro_meters );
johnAlexander 15:932d8b4e52c9 372 }
JerrySzczurak 20:cf211a3b3d9e 373 byte = ( uint8_t )( read_data_from_device_done | option );
JerrySzczurak 20:cf211a3b3d9e 374 VL53L0X_SETDEVICESPECIFICPARAMETER( dev, ReadDataFromDeviceDone,
JerrySzczurak 20:cf211a3b3d9e 375 byte );
JerrySzczurak 20:cf211a3b3d9e 376 }
JerrySzczurak 20:cf211a3b3d9e 377
JerrySzczurak 20:cf211a3b3d9e 378 LOG_FUNCTION_END( status );
JerrySzczurak 20:cf211a3b3d9e 379 return status;
JerrySzczurak 20:cf211a3b3d9e 380 }
JerrySzczurak 20:cf211a3b3d9e 381
JerrySzczurak 20:cf211a3b3d9e 382 VL53L0X_Error VL53L0X::wrapped_vl53l0x_get_offset_calibration_data_micro_meter( VL53L0X_DEV dev,
JerrySzczurak 20:cf211a3b3d9e 383 int32_t *p_offset_calibration_data_micro_meter )
johnAlexander 0:c523920bcc09 384 {
johnAlexander 16:98ce55ddbb1a 385 VL53L0X_Error status = VL53L0X_ERROR_NONE;
johnAlexander 16:98ce55ddbb1a 386 uint16_t range_offset_register;
johnAlexander 16:98ce55ddbb1a 387 int16_t c_max_offset = 2047;
johnAlexander 16:98ce55ddbb1a 388 int16_t c_offset_range = 4096;
johnAlexander 15:932d8b4e52c9 389
johnAlexander 15:932d8b4e52c9 390 /* Note that offset has 10.2 format */
johnAlexander 15:932d8b4e52c9 391
JerrySzczurak 20:cf211a3b3d9e 392 status = vl53l0x_read_word( dev,
JerrySzczurak 20:cf211a3b3d9e 393 VL53L0X_REG_ALGO_PART_TO_PART_RANGE_OFFSET_MM,
JerrySzczurak 20:cf211a3b3d9e 394 &range_offset_register );
JerrySzczurak 20:cf211a3b3d9e 395
JerrySzczurak 20:cf211a3b3d9e 396 if ( status == VL53L0X_ERROR_NONE ) {
JerrySzczurak 20:cf211a3b3d9e 397 range_offset_register = ( range_offset_register & 0x0fff );
johnAlexander 15:932d8b4e52c9 398
johnAlexander 15:932d8b4e52c9 399 /* Apply 12 bit 2's compliment conversion */
JerrySzczurak 20:cf211a3b3d9e 400 if ( range_offset_register > c_max_offset )
johnAlexander 16:98ce55ddbb1a 401 *p_offset_calibration_data_micro_meter =
JerrySzczurak 20:cf211a3b3d9e 402 ( int16_t )( range_offset_register - c_offset_range )
johnAlexander 15:932d8b4e52c9 403 * 250;
johnAlexander 15:932d8b4e52c9 404 else
johnAlexander 16:98ce55ddbb1a 405 *p_offset_calibration_data_micro_meter =
JerrySzczurak 20:cf211a3b3d9e 406 ( int16_t )range_offset_register * 250;
JerrySzczurak 20:cf211a3b3d9e 407
JerrySzczurak 20:cf211a3b3d9e 408 }
JerrySzczurak 20:cf211a3b3d9e 409
JerrySzczurak 20:cf211a3b3d9e 410 return status;
JerrySzczurak 20:cf211a3b3d9e 411 }
JerrySzczurak 20:cf211a3b3d9e 412
JerrySzczurak 20:cf211a3b3d9e 413 VL53L0X_Error VL53L0X::vl53l0x_get_offset_calibration_data_micro_meter( VL53L0X_DEV dev,
JerrySzczurak 20:cf211a3b3d9e 414 int32_t *p_offset_calibration_data_micro_meter )
johnAlexander 0:c523920bcc09 415 {
johnAlexander 16:98ce55ddbb1a 416 VL53L0X_Error status = VL53L0X_ERROR_NONE;
JerrySzczurak 20:cf211a3b3d9e 417 LOG_FUNCTION_START( "" );
JerrySzczurak 20:cf211a3b3d9e 418
JerrySzczurak 20:cf211a3b3d9e 419 status = wrapped_vl53l0x_get_offset_calibration_data_micro_meter( dev,
JerrySzczurak 20:cf211a3b3d9e 420 p_offset_calibration_data_micro_meter );
JerrySzczurak 20:cf211a3b3d9e 421
JerrySzczurak 20:cf211a3b3d9e 422 LOG_FUNCTION_END( status );
JerrySzczurak 20:cf211a3b3d9e 423 return status;
JerrySzczurak 20:cf211a3b3d9e 424 }
JerrySzczurak 20:cf211a3b3d9e 425
JerrySzczurak 20:cf211a3b3d9e 426 VL53L0X_Error VL53L0X::wrapped_vl53l0x_set_offset_calibration_data_micro_meter( VL53L0X_DEV dev,
JerrySzczurak 20:cf211a3b3d9e 427 int32_t offset_calibration_data_micro_meter )
johnAlexander 0:c523920bcc09 428 {
johnAlexander 16:98ce55ddbb1a 429 VL53L0X_Error status = VL53L0X_ERROR_NONE;
johnAlexander 16:98ce55ddbb1a 430 int32_t c_max_offset_micro_meter = 511000;
johnAlexander 16:98ce55ddbb1a 431 int32_t c_min_offset_micro_meter = -512000;
johnAlexander 16:98ce55ddbb1a 432 int16_t c_offset_range = 4096;
johnAlexander 16:98ce55ddbb1a 433 uint32_t encoded_offset_val;
johnAlexander 15:932d8b4e52c9 434
JerrySzczurak 20:cf211a3b3d9e 435 LOG_FUNCTION_START( "" );
JerrySzczurak 20:cf211a3b3d9e 436
JerrySzczurak 20:cf211a3b3d9e 437 if ( offset_calibration_data_micro_meter > c_max_offset_micro_meter )
johnAlexander 16:98ce55ddbb1a 438 offset_calibration_data_micro_meter = c_max_offset_micro_meter;
JerrySzczurak 20:cf211a3b3d9e 439 else if ( offset_calibration_data_micro_meter < c_min_offset_micro_meter )
johnAlexander 16:98ce55ddbb1a 440 offset_calibration_data_micro_meter = c_min_offset_micro_meter;
johnAlexander 15:932d8b4e52c9 441
johnAlexander 15:932d8b4e52c9 442 /* The offset register is 10.2 format and units are mm
johnAlexander 15:932d8b4e52c9 443 * therefore conversion is applied by a division of
johnAlexander 15:932d8b4e52c9 444 * 250.
johnAlexander 15:932d8b4e52c9 445 */
JerrySzczurak 20:cf211a3b3d9e 446 if ( offset_calibration_data_micro_meter >= 0 ) {
johnAlexander 16:98ce55ddbb1a 447 encoded_offset_val =
JerrySzczurak 20:cf211a3b3d9e 448 offset_calibration_data_micro_meter / 250;
johnAlexander 15:932d8b4e52c9 449 } else {
johnAlexander 16:98ce55ddbb1a 450 encoded_offset_val =
johnAlexander 16:98ce55ddbb1a 451 c_offset_range +
JerrySzczurak 20:cf211a3b3d9e 452 offset_calibration_data_micro_meter / 250;
JerrySzczurak 20:cf211a3b3d9e 453 }
JerrySzczurak 20:cf211a3b3d9e 454
JerrySzczurak 20:cf211a3b3d9e 455 status = vl53l0x_write_word( dev,
JerrySzczurak 20:cf211a3b3d9e 456 VL53L0X_REG_ALGO_PART_TO_PART_RANGE_OFFSET_MM,
JerrySzczurak 20:cf211a3b3d9e 457 encoded_offset_val );
JerrySzczurak 20:cf211a3b3d9e 458
JerrySzczurak 20:cf211a3b3d9e 459 LOG_FUNCTION_END( status );
JerrySzczurak 20:cf211a3b3d9e 460 return status;
JerrySzczurak 20:cf211a3b3d9e 461 }
JerrySzczurak 20:cf211a3b3d9e 462
JerrySzczurak 20:cf211a3b3d9e 463 VL53L0X_Error VL53L0X::vl53l0x_set_offset_calibration_data_micro_meter( VL53L0X_DEV dev,
JerrySzczurak 20:cf211a3b3d9e 464 int32_t offset_calibration_data_micro_meter )
johnAlexander 0:c523920bcc09 465 {
johnAlexander 16:98ce55ddbb1a 466 VL53L0X_Error status = VL53L0X_ERROR_NONE;
JerrySzczurak 20:cf211a3b3d9e 467 LOG_FUNCTION_START( "" );
JerrySzczurak 20:cf211a3b3d9e 468
JerrySzczurak 20:cf211a3b3d9e 469 status = wrapped_vl53l0x_set_offset_calibration_data_micro_meter( dev,
JerrySzczurak 20:cf211a3b3d9e 470 offset_calibration_data_micro_meter );
JerrySzczurak 20:cf211a3b3d9e 471
JerrySzczurak 20:cf211a3b3d9e 472 LOG_FUNCTION_END( status );
JerrySzczurak 20:cf211a3b3d9e 473 return status;
JerrySzczurak 20:cf211a3b3d9e 474 }
JerrySzczurak 20:cf211a3b3d9e 475
JerrySzczurak 20:cf211a3b3d9e 476 VL53L0X_Error VL53L0X::vl53l0x_apply_offset_adjustment( VL53L0X_DEV dev )
johnAlexander 0:c523920bcc09 477 {
johnAlexander 16:98ce55ddbb1a 478 VL53L0X_Error status = VL53L0X_ERROR_NONE;
johnAlexander 16:98ce55ddbb1a 479 int32_t corrected_offset_micro_meters;
johnAlexander 16:98ce55ddbb1a 480 int32_t current_offset_micro_meters;
johnAlexander 15:932d8b4e52c9 481
johnAlexander 15:932d8b4e52c9 482 /* if we run on this function we can read all the NVM info
johnAlexander 15:932d8b4e52c9 483 * used by the API */
JerrySzczurak 20:cf211a3b3d9e 484 status = vl53l0x_get_info_from_device( dev, 7 );
johnAlexander 15:932d8b4e52c9 485
johnAlexander 15:932d8b4e52c9 486 /* Read back current device offset */
JerrySzczurak 20:cf211a3b3d9e 487 if ( status == VL53L0X_ERROR_NONE ) {
JerrySzczurak 20:cf211a3b3d9e 488 status = vl53l0x_get_offset_calibration_data_micro_meter( dev,
JerrySzczurak 20:cf211a3b3d9e 489 &current_offset_micro_meters );
johnAlexander 15:932d8b4e52c9 490 }
johnAlexander 15:932d8b4e52c9 491
johnAlexander 15:932d8b4e52c9 492 /* Apply Offset Adjustment derived from 400mm measurements */
JerrySzczurak 20:cf211a3b3d9e 493 if ( status == VL53L0X_ERROR_NONE ) {
johnAlexander 15:932d8b4e52c9 494
johnAlexander 15:932d8b4e52c9 495 /* Store initial device offset */
JerrySzczurak 20:cf211a3b3d9e 496 PALDevDataSet( dev, Part2PartOffsetNVMMicroMeter,
JerrySzczurak 20:cf211a3b3d9e 497 current_offset_micro_meters );
johnAlexander 16:98ce55ddbb1a 498
johnAlexander 16:98ce55ddbb1a 499 corrected_offset_micro_meters = current_offset_micro_meters +
JerrySzczurak 20:cf211a3b3d9e 500 ( int32_t )PALDevDataGet( dev,
JerrySzczurak 20:cf211a3b3d9e 501 Part2PartOffsetAdjustmentNVMMicroMeter );
JerrySzczurak 20:cf211a3b3d9e 502
JerrySzczurak 20:cf211a3b3d9e 503 status = vl53l0x_set_offset_calibration_data_micro_meter( dev,
JerrySzczurak 20:cf211a3b3d9e 504 corrected_offset_micro_meters );
johnAlexander 15:932d8b4e52c9 505
johnAlexander 15:932d8b4e52c9 506 /* store current, adjusted offset */
JerrySzczurak 20:cf211a3b3d9e 507 if ( status == VL53L0X_ERROR_NONE ) {
JerrySzczurak 20:cf211a3b3d9e 508 VL53L0X_SETPARAMETERFIELD( dev, RangeOffsetMicroMeters,
JerrySzczurak 20:cf211a3b3d9e 509 corrected_offset_micro_meters );
johnAlexander 15:932d8b4e52c9 510 }
johnAlexander 15:932d8b4e52c9 511 }
johnAlexander 15:932d8b4e52c9 512
johnAlexander 16:98ce55ddbb1a 513 return status;
johnAlexander 0:c523920bcc09 514 }
johnAlexander 0:c523920bcc09 515
JerrySzczurak 20:cf211a3b3d9e 516 VL53L0X_Error VL53L0X::vl53l0x_get_device_mode( VL53L0X_DEV dev,
JerrySzczurak 20:cf211a3b3d9e 517 VL53L0X_DeviceModes *p_device_mode )
johnAlexander 0:c523920bcc09 518 {
johnAlexander 16:98ce55ddbb1a 519 VL53L0X_Error status = VL53L0X_ERROR_NONE;
JerrySzczurak 20:cf211a3b3d9e 520 LOG_FUNCTION_START( "" );
JerrySzczurak 20:cf211a3b3d9e 521
JerrySzczurak 20:cf211a3b3d9e 522 VL53L0X_GETPARAMETERFIELD( dev, DeviceMode, *p_device_mode );
JerrySzczurak 20:cf211a3b3d9e 523
JerrySzczurak 20:cf211a3b3d9e 524 LOG_FUNCTION_END( status );
JerrySzczurak 20:cf211a3b3d9e 525 return status;
JerrySzczurak 20:cf211a3b3d9e 526 }
JerrySzczurak 20:cf211a3b3d9e 527
JerrySzczurak 20:cf211a3b3d9e 528 VL53L0X_Error VL53L0X::vl53l0x_get_inter_measurement_period_milli_seconds( VL53L0X_DEV dev,
JerrySzczurak 20:cf211a3b3d9e 529 uint32_t *p_inter_measurement_period_milli_seconds )
johnAlexander 0:c523920bcc09 530 {
johnAlexander 16:98ce55ddbb1a 531 VL53L0X_Error status = VL53L0X_ERROR_NONE;
johnAlexander 15:932d8b4e52c9 532 uint16_t osc_calibrate_val;
johnAlexander 16:98ce55ddbb1a 533 uint32_t im_period_milli_seconds;
johnAlexander 15:932d8b4e52c9 534
JerrySzczurak 20:cf211a3b3d9e 535 LOG_FUNCTION_START( "" );
JerrySzczurak 20:cf211a3b3d9e 536
JerrySzczurak 20:cf211a3b3d9e 537 status = vl53l0x_read_word( dev, VL53L0X_REG_OSC_CALIBRATE_VAL,
JerrySzczurak 20:cf211a3b3d9e 538 &osc_calibrate_val );
JerrySzczurak 20:cf211a3b3d9e 539
JerrySzczurak 20:cf211a3b3d9e 540 if ( status == VL53L0X_ERROR_NONE ) {
JerrySzczurak 20:cf211a3b3d9e 541 status = vl53l0x_read_dword( dev,
JerrySzczurak 20:cf211a3b3d9e 542 VL53L0X_REG_SYSTEM_INTERMEASUREMENT_PERIOD,
JerrySzczurak 20:cf211a3b3d9e 543 &im_period_milli_seconds );
JerrySzczurak 20:cf211a3b3d9e 544 }
JerrySzczurak 20:cf211a3b3d9e 545
JerrySzczurak 20:cf211a3b3d9e 546 if ( status == VL53L0X_ERROR_NONE ) {
JerrySzczurak 20:cf211a3b3d9e 547 if ( osc_calibrate_val != 0 ) {
johnAlexander 17:1b842521063a 548 *p_inter_measurement_period_milli_seconds =
johnAlexander 16:98ce55ddbb1a 549 im_period_milli_seconds / osc_calibrate_val;
johnAlexander 15:932d8b4e52c9 550 }
JerrySzczurak 20:cf211a3b3d9e 551 VL53L0X_SETPARAMETERFIELD( dev,
JerrySzczurak 20:cf211a3b3d9e 552 InterMeasurementPeriodMilliSeconds,
JerrySzczurak 20:cf211a3b3d9e 553 *p_inter_measurement_period_milli_seconds );
JerrySzczurak 20:cf211a3b3d9e 554 }
JerrySzczurak 20:cf211a3b3d9e 555
JerrySzczurak 20:cf211a3b3d9e 556 LOG_FUNCTION_END( status );
JerrySzczurak 20:cf211a3b3d9e 557 return status;
JerrySzczurak 20:cf211a3b3d9e 558 }
JerrySzczurak 20:cf211a3b3d9e 559
JerrySzczurak 20:cf211a3b3d9e 560 VL53L0X_Error VL53L0X::vl53l0x_get_x_talk_compensation_rate_mega_cps( VL53L0X_DEV dev,
JerrySzczurak 20:cf211a3b3d9e 561 FixPoint1616_t *p_xtalk_compensation_rate_mega_cps )
johnAlexander 0:c523920bcc09 562 {
johnAlexander 16:98ce55ddbb1a 563 VL53L0X_Error status = VL53L0X_ERROR_NONE;
johnAlexander 16:98ce55ddbb1a 564 uint16_t value;
johnAlexander 16:98ce55ddbb1a 565 FixPoint1616_t temp_fix1616;
johnAlexander 15:932d8b4e52c9 566
JerrySzczurak 20:cf211a3b3d9e 567 LOG_FUNCTION_START( "" );
JerrySzczurak 20:cf211a3b3d9e 568
JerrySzczurak 20:cf211a3b3d9e 569 status = vl53l0x_read_word( dev,
JerrySzczurak 20:cf211a3b3d9e 570 VL53L0X_REG_CROSSTALK_COMPENSATION_PEAK_RATE_MCPS, ( uint16_t * )&value );
JerrySzczurak 20:cf211a3b3d9e 571 if ( status == VL53L0X_ERROR_NONE ) {
JerrySzczurak 20:cf211a3b3d9e 572 if ( value == 0 ) {
johnAlexander 15:932d8b4e52c9 573 /* the Xtalk is disabled return value from memory */
JerrySzczurak 20:cf211a3b3d9e 574 VL53L0X_GETPARAMETERFIELD( dev,
JerrySzczurak 20:cf211a3b3d9e 575 XTalkCompensationRateMegaCps, temp_fix1616 );
johnAlexander 17:1b842521063a 576 *p_xtalk_compensation_rate_mega_cps = temp_fix1616;
JerrySzczurak 20:cf211a3b3d9e 577 VL53L0X_SETPARAMETERFIELD( dev, XTalkCompensationEnable,
JerrySzczurak 20:cf211a3b3d9e 578 0 );
johnAlexander 15:932d8b4e52c9 579 } else {
JerrySzczurak 20:cf211a3b3d9e 580 temp_fix1616 = VL53L0X_FIXPOINT313TOFIXPOINT1616( value );
johnAlexander 17:1b842521063a 581 *p_xtalk_compensation_rate_mega_cps = temp_fix1616;
JerrySzczurak 20:cf211a3b3d9e 582 VL53L0X_SETPARAMETERFIELD( dev,
JerrySzczurak 20:cf211a3b3d9e 583 XTalkCompensationRateMegaCps, temp_fix1616 );
JerrySzczurak 20:cf211a3b3d9e 584 VL53L0X_SETPARAMETERFIELD( dev, XTalkCompensationEnable,
JerrySzczurak 20:cf211a3b3d9e 585 1 );
johnAlexander 15:932d8b4e52c9 586 }
johnAlexander 15:932d8b4e52c9 587 }
johnAlexander 15:932d8b4e52c9 588
JerrySzczurak 20:cf211a3b3d9e 589 LOG_FUNCTION_END( status );
JerrySzczurak 20:cf211a3b3d9e 590 return status;
JerrySzczurak 20:cf211a3b3d9e 591 }
JerrySzczurak 20:cf211a3b3d9e 592
JerrySzczurak 20:cf211a3b3d9e 593 VL53L0X_Error VL53L0X::vl53l0x_get_limit_check_value( VL53L0X_DEV dev, uint16_t limit_check_id,
JerrySzczurak 20:cf211a3b3d9e 594 FixPoint1616_t *p_limit_check_value )
johnAlexander 0:c523920bcc09 595 {
johnAlexander 16:98ce55ddbb1a 596 VL53L0X_Error status = VL53L0X_ERROR_NONE;
johnAlexander 16:98ce55ddbb1a 597 uint8_t enable_zero_value = 0;
johnAlexander 16:98ce55ddbb1a 598 uint16_t temp16;
johnAlexander 16:98ce55ddbb1a 599 FixPoint1616_t temp_fix1616;
johnAlexander 15:932d8b4e52c9 600
JerrySzczurak 20:cf211a3b3d9e 601 LOG_FUNCTION_START( "" );
JerrySzczurak 20:cf211a3b3d9e 602
JerrySzczurak 20:cf211a3b3d9e 603 switch ( limit_check_id ) {
johnAlexander 15:932d8b4e52c9 604
johnAlexander 15:932d8b4e52c9 605 case VL53L0X_CHECKENABLE_SIGMA_FINAL_RANGE:
johnAlexander 15:932d8b4e52c9 606 /* internal computation: */
JerrySzczurak 20:cf211a3b3d9e 607 VL53L0X_GETARRAYPARAMETERFIELD( dev, LimitChecksValue,
JerrySzczurak 20:cf211a3b3d9e 608 VL53L0X_CHECKENABLE_SIGMA_FINAL_RANGE, temp_fix1616 );
johnAlexander 16:98ce55ddbb1a 609 enable_zero_value = 0;
johnAlexander 15:932d8b4e52c9 610 break;
johnAlexander 15:932d8b4e52c9 611
johnAlexander 15:932d8b4e52c9 612 case VL53L0X_CHECKENABLE_SIGNAL_RATE_FINAL_RANGE:
JerrySzczurak 20:cf211a3b3d9e 613 status = vl53l0x_read_word( dev,
JerrySzczurak 20:cf211a3b3d9e 614 VL53L0X_REG_FINAL_RANGE_CONFIG_MIN_COUNT_RATE_RTN_LIMIT,
JerrySzczurak 20:cf211a3b3d9e 615 &temp16 );
JerrySzczurak 20:cf211a3b3d9e 616 if ( status == VL53L0X_ERROR_NONE )
JerrySzczurak 20:cf211a3b3d9e 617 temp_fix1616 = VL53L0X_FIXPOINT97TOFIXPOINT1616( temp16 );
johnAlexander 16:98ce55ddbb1a 618
johnAlexander 16:98ce55ddbb1a 619
johnAlexander 16:98ce55ddbb1a 620 enable_zero_value = 1;
johnAlexander 15:932d8b4e52c9 621 break;
johnAlexander 15:932d8b4e52c9 622
johnAlexander 15:932d8b4e52c9 623 case VL53L0X_CHECKENABLE_SIGNAL_REF_CLIP:
johnAlexander 15:932d8b4e52c9 624 /* internal computation: */
JerrySzczurak 20:cf211a3b3d9e 625 VL53L0X_GETARRAYPARAMETERFIELD( dev, LimitChecksValue,
JerrySzczurak 20:cf211a3b3d9e 626 VL53L0X_CHECKENABLE_SIGNAL_REF_CLIP, temp_fix1616 );
johnAlexander 16:98ce55ddbb1a 627 enable_zero_value = 0;
johnAlexander 15:932d8b4e52c9 628 break;
johnAlexander 15:932d8b4e52c9 629
johnAlexander 15:932d8b4e52c9 630 case VL53L0X_CHECKENABLE_RANGE_IGNORE_THRESHOLD:
johnAlexander 15:932d8b4e52c9 631 /* internal computation: */
JerrySzczurak 20:cf211a3b3d9e 632 VL53L0X_GETARRAYPARAMETERFIELD( dev, LimitChecksValue,
JerrySzczurak 20:cf211a3b3d9e 633 VL53L0X_CHECKENABLE_RANGE_IGNORE_THRESHOLD, temp_fix1616 );
johnAlexander 16:98ce55ddbb1a 634 enable_zero_value = 0;
johnAlexander 15:932d8b4e52c9 635 break;
johnAlexander 15:932d8b4e52c9 636
johnAlexander 15:932d8b4e52c9 637 case VL53L0X_CHECKENABLE_SIGNAL_RATE_MSRC:
johnAlexander 15:932d8b4e52c9 638 case VL53L0X_CHECKENABLE_SIGNAL_RATE_PRE_RANGE:
JerrySzczurak 20:cf211a3b3d9e 639 status = vl53l0x_read_word( dev,
JerrySzczurak 20:cf211a3b3d9e 640 VL53L0X_REG_PRE_RANGE_MIN_COUNT_RATE_RTN_LIMIT,
JerrySzczurak 20:cf211a3b3d9e 641 &temp16 );
JerrySzczurak 20:cf211a3b3d9e 642 if ( status == VL53L0X_ERROR_NONE )
JerrySzczurak 20:cf211a3b3d9e 643 temp_fix1616 = VL53L0X_FIXPOINT97TOFIXPOINT1616( temp16 );
johnAlexander 16:98ce55ddbb1a 644
johnAlexander 16:98ce55ddbb1a 645
johnAlexander 16:98ce55ddbb1a 646 enable_zero_value = 0;
johnAlexander 15:932d8b4e52c9 647 break;
johnAlexander 15:932d8b4e52c9 648
johnAlexander 15:932d8b4e52c9 649 default:
johnAlexander 16:98ce55ddbb1a 650 status = VL53L0X_ERROR_INVALID_PARAMS;
johnAlexander 15:932d8b4e52c9 651
johnAlexander 15:932d8b4e52c9 652 }
johnAlexander 15:932d8b4e52c9 653
JerrySzczurak 20:cf211a3b3d9e 654 if ( status == VL53L0X_ERROR_NONE ) {
JerrySzczurak 20:cf211a3b3d9e 655
JerrySzczurak 20:cf211a3b3d9e 656 if ( enable_zero_value == 1 ) {
JerrySzczurak 20:cf211a3b3d9e 657
JerrySzczurak 20:cf211a3b3d9e 658 if ( temp_fix1616 == 0 ) {
johnAlexander 15:932d8b4e52c9 659 /* disabled: return value from memory */
JerrySzczurak 20:cf211a3b3d9e 660 VL53L0X_GETARRAYPARAMETERFIELD( dev,
JerrySzczurak 20:cf211a3b3d9e 661 LimitChecksValue, limit_check_id,
JerrySzczurak 20:cf211a3b3d9e 662 temp_fix1616 );
johnAlexander 17:1b842521063a 663 *p_limit_check_value = temp_fix1616;
JerrySzczurak 20:cf211a3b3d9e 664 VL53L0X_SETARRAYPARAMETERFIELD( dev,
JerrySzczurak 20:cf211a3b3d9e 665 LimitChecksEnable, limit_check_id, 0 );
johnAlexander 15:932d8b4e52c9 666 } else {
johnAlexander 17:1b842521063a 667 *p_limit_check_value = temp_fix1616;
JerrySzczurak 20:cf211a3b3d9e 668 VL53L0X_SETARRAYPARAMETERFIELD( dev,
JerrySzczurak 20:cf211a3b3d9e 669 LimitChecksValue, limit_check_id,
JerrySzczurak 20:cf211a3b3d9e 670 temp_fix1616 );
JerrySzczurak 20:cf211a3b3d9e 671 VL53L0X_SETARRAYPARAMETERFIELD( dev,
JerrySzczurak 20:cf211a3b3d9e 672 LimitChecksEnable, limit_check_id, 1 );
johnAlexander 15:932d8b4e52c9 673 }
johnAlexander 15:932d8b4e52c9 674 } else {
johnAlexander 17:1b842521063a 675 *p_limit_check_value = temp_fix1616;
johnAlexander 15:932d8b4e52c9 676 }
johnAlexander 15:932d8b4e52c9 677 }
johnAlexander 15:932d8b4e52c9 678
JerrySzczurak 20:cf211a3b3d9e 679 LOG_FUNCTION_END( status );
JerrySzczurak 20:cf211a3b3d9e 680 return status;
JerrySzczurak 20:cf211a3b3d9e 681
JerrySzczurak 20:cf211a3b3d9e 682 }
JerrySzczurak 20:cf211a3b3d9e 683
JerrySzczurak 20:cf211a3b3d9e 684 VL53L0X_Error VL53L0X::vl53l0x_get_limit_check_enable( VL53L0X_DEV dev, uint16_t limit_check_id,
JerrySzczurak 20:cf211a3b3d9e 685 uint8_t *p_limit_check_enable )
johnAlexander 0:c523920bcc09 686 {
johnAlexander 16:98ce55ddbb1a 687 VL53L0X_Error status = VL53L0X_ERROR_NONE;
johnAlexander 16:98ce55ddbb1a 688 uint8_t temp8;
johnAlexander 15:932d8b4e52c9 689
JerrySzczurak 20:cf211a3b3d9e 690 LOG_FUNCTION_START( "" );
JerrySzczurak 20:cf211a3b3d9e 691
JerrySzczurak 20:cf211a3b3d9e 692 if ( limit_check_id >= VL53L0X_CHECKENABLE_NUMBER_OF_CHECKS ) {
johnAlexander 16:98ce55ddbb1a 693 status = VL53L0X_ERROR_INVALID_PARAMS;
johnAlexander 17:1b842521063a 694 *p_limit_check_enable = 0;
johnAlexander 15:932d8b4e52c9 695 } else {
JerrySzczurak 20:cf211a3b3d9e 696 VL53L0X_GETARRAYPARAMETERFIELD( dev, LimitChecksEnable,
JerrySzczurak 20:cf211a3b3d9e 697 limit_check_id, temp8 );
johnAlexander 17:1b842521063a 698 *p_limit_check_enable = temp8;
johnAlexander 15:932d8b4e52c9 699 }
johnAlexander 15:932d8b4e52c9 700
JerrySzczurak 20:cf211a3b3d9e 701 LOG_FUNCTION_END( status );
JerrySzczurak 20:cf211a3b3d9e 702 return status;
JerrySzczurak 20:cf211a3b3d9e 703 }
JerrySzczurak 20:cf211a3b3d9e 704
JerrySzczurak 20:cf211a3b3d9e 705 VL53L0X_Error VL53L0X::vl53l0x_get_wrap_around_check_enable( VL53L0X_DEV dev,
JerrySzczurak 20:cf211a3b3d9e 706 uint8_t *p_wrap_around_check_enable )
johnAlexander 0:c523920bcc09 707 {
johnAlexander 16:98ce55ddbb1a 708 VL53L0X_Error status = VL53L0X_ERROR_NONE;
johnAlexander 15:932d8b4e52c9 709 uint8_t data;
johnAlexander 15:932d8b4e52c9 710
JerrySzczurak 20:cf211a3b3d9e 711 LOG_FUNCTION_START( "" );
JerrySzczurak 20:cf211a3b3d9e 712
JerrySzczurak 20:cf211a3b3d9e 713 status = vl53l0x_read_byte( dev, VL53L0X_REG_SYSTEM_SEQUENCE_CONFIG, &data );
JerrySzczurak 20:cf211a3b3d9e 714 if ( status == VL53L0X_ERROR_NONE ) {
JerrySzczurak 20:cf211a3b3d9e 715 PALDevDataSet( dev, SequenceConfig, data );
JerrySzczurak 20:cf211a3b3d9e 716 if ( data & ( 0x01 << 7 ) )
johnAlexander 17:1b842521063a 717 *p_wrap_around_check_enable = 0x01;
johnAlexander 15:932d8b4e52c9 718 else
johnAlexander 17:1b842521063a 719 *p_wrap_around_check_enable = 0x00;
johnAlexander 15:932d8b4e52c9 720 }
JerrySzczurak 20:cf211a3b3d9e 721 if ( status == VL53L0X_ERROR_NONE ) {
JerrySzczurak 20:cf211a3b3d9e 722 VL53L0X_SETPARAMETERFIELD( dev, WrapAroundCheckEnable,
JerrySzczurak 20:cf211a3b3d9e 723 *p_wrap_around_check_enable );
JerrySzczurak 20:cf211a3b3d9e 724 }
JerrySzczurak 20:cf211a3b3d9e 725
JerrySzczurak 20:cf211a3b3d9e 726 LOG_FUNCTION_END( status );
JerrySzczurak 20:cf211a3b3d9e 727 return status;
JerrySzczurak 20:cf211a3b3d9e 728 }
JerrySzczurak 20:cf211a3b3d9e 729
JerrySzczurak 20:cf211a3b3d9e 730 VL53L0X_Error VL53L0X::sequence_step_enabled( VL53L0X_DEV dev,
JerrySzczurak 20:cf211a3b3d9e 731 VL53L0X_SequenceStepId sequence_step_id, uint8_t sequence_config,
JerrySzczurak 20:cf211a3b3d9e 732 uint8_t *p_sequence_step_enabled )
johnAlexander 0:c523920bcc09 733 {
johnAlexander 15:932d8b4e52c9 734 VL53L0X_Error Status = VL53L0X_ERROR_NONE;
johnAlexander 17:1b842521063a 735 *p_sequence_step_enabled = 0;
JerrySzczurak 20:cf211a3b3d9e 736 LOG_FUNCTION_START( "" );
JerrySzczurak 20:cf211a3b3d9e 737
JerrySzczurak 20:cf211a3b3d9e 738 switch ( sequence_step_id ) {
johnAlexander 15:932d8b4e52c9 739 case VL53L0X_SEQUENCESTEP_TCC:
JerrySzczurak 20:cf211a3b3d9e 740 *p_sequence_step_enabled = ( sequence_config & 0x10 ) >> 4;
johnAlexander 15:932d8b4e52c9 741 break;
johnAlexander 15:932d8b4e52c9 742 case VL53L0X_SEQUENCESTEP_DSS:
JerrySzczurak 20:cf211a3b3d9e 743 *p_sequence_step_enabled = ( sequence_config & 0x08 ) >> 3;
johnAlexander 15:932d8b4e52c9 744 break;
johnAlexander 15:932d8b4e52c9 745 case VL53L0X_SEQUENCESTEP_MSRC:
JerrySzczurak 20:cf211a3b3d9e 746 *p_sequence_step_enabled = ( sequence_config & 0x04 ) >> 2;
johnAlexander 15:932d8b4e52c9 747 break;
johnAlexander 15:932d8b4e52c9 748 case VL53L0X_SEQUENCESTEP_PRE_RANGE:
JerrySzczurak 20:cf211a3b3d9e 749 *p_sequence_step_enabled = ( sequence_config & 0x40 ) >> 6;
johnAlexander 15:932d8b4e52c9 750 break;
johnAlexander 15:932d8b4e52c9 751 case VL53L0X_SEQUENCESTEP_FINAL_RANGE:
JerrySzczurak 20:cf211a3b3d9e 752 *p_sequence_step_enabled = ( sequence_config & 0x80 ) >> 7;
johnAlexander 15:932d8b4e52c9 753 break;
johnAlexander 15:932d8b4e52c9 754 default:
johnAlexander 15:932d8b4e52c9 755 Status = VL53L0X_ERROR_INVALID_PARAMS;
johnAlexander 15:932d8b4e52c9 756 }
johnAlexander 15:932d8b4e52c9 757
JerrySzczurak 20:cf211a3b3d9e 758 LOG_FUNCTION_END( status );
johnAlexander 15:932d8b4e52c9 759 return Status;
johnAlexander 0:c523920bcc09 760 }
johnAlexander 0:c523920bcc09 761
JerrySzczurak 20:cf211a3b3d9e 762 VL53L0X_Error VL53L0X::vl53l0x_get_sequence_step_enables( VL53L0X_DEV dev,
JerrySzczurak 20:cf211a3b3d9e 763 VL53L0X_SchedulerSequenceSteps_t *p_scheduler_sequence_steps )
johnAlexander 0:c523920bcc09 764 {
johnAlexander 16:98ce55ddbb1a 765 VL53L0X_Error status = VL53L0X_ERROR_NONE;
johnAlexander 16:98ce55ddbb1a 766 uint8_t sequence_config = 0;
JerrySzczurak 20:cf211a3b3d9e 767 LOG_FUNCTION_START( "" );
JerrySzczurak 20:cf211a3b3d9e 768
JerrySzczurak 20:cf211a3b3d9e 769 status = vl53l0x_read_byte( dev, VL53L0X_REG_SYSTEM_SEQUENCE_CONFIG,
JerrySzczurak 20:cf211a3b3d9e 770 &sequence_config );
JerrySzczurak 20:cf211a3b3d9e 771
JerrySzczurak 20:cf211a3b3d9e 772 if ( status == VL53L0X_ERROR_NONE ) {
JerrySzczurak 20:cf211a3b3d9e 773 status = sequence_step_enabled( dev,
JerrySzczurak 20:cf211a3b3d9e 774 VL53L0X_SEQUENCESTEP_TCC, sequence_config,
JerrySzczurak 20:cf211a3b3d9e 775 &p_scheduler_sequence_steps->TccOn );
JerrySzczurak 20:cf211a3b3d9e 776 }
JerrySzczurak 20:cf211a3b3d9e 777 if ( status == VL53L0X_ERROR_NONE ) {
JerrySzczurak 20:cf211a3b3d9e 778 status = sequence_step_enabled( dev,
JerrySzczurak 20:cf211a3b3d9e 779 VL53L0X_SEQUENCESTEP_DSS, sequence_config,
JerrySzczurak 20:cf211a3b3d9e 780 &p_scheduler_sequence_steps->DssOn );
JerrySzczurak 20:cf211a3b3d9e 781 }
JerrySzczurak 20:cf211a3b3d9e 782 if ( status == VL53L0X_ERROR_NONE ) {
JerrySzczurak 20:cf211a3b3d9e 783 status = sequence_step_enabled( dev,
JerrySzczurak 20:cf211a3b3d9e 784 VL53L0X_SEQUENCESTEP_MSRC, sequence_config,
JerrySzczurak 20:cf211a3b3d9e 785 &p_scheduler_sequence_steps->MsrcOn );
JerrySzczurak 20:cf211a3b3d9e 786 }
JerrySzczurak 20:cf211a3b3d9e 787 if ( status == VL53L0X_ERROR_NONE ) {
JerrySzczurak 20:cf211a3b3d9e 788 status = sequence_step_enabled( dev,
JerrySzczurak 20:cf211a3b3d9e 789 VL53L0X_SEQUENCESTEP_PRE_RANGE, sequence_config,
JerrySzczurak 20:cf211a3b3d9e 790 &p_scheduler_sequence_steps->PreRangeOn );
JerrySzczurak 20:cf211a3b3d9e 791 }
JerrySzczurak 20:cf211a3b3d9e 792 if ( status == VL53L0X_ERROR_NONE ) {
JerrySzczurak 20:cf211a3b3d9e 793 status = sequence_step_enabled( dev,
JerrySzczurak 20:cf211a3b3d9e 794 VL53L0X_SEQUENCESTEP_FINAL_RANGE, sequence_config,
JerrySzczurak 20:cf211a3b3d9e 795 &p_scheduler_sequence_steps->FinalRangeOn );
JerrySzczurak 20:cf211a3b3d9e 796 }
JerrySzczurak 20:cf211a3b3d9e 797
JerrySzczurak 20:cf211a3b3d9e 798 LOG_FUNCTION_END( status );
JerrySzczurak 20:cf211a3b3d9e 799 return status;
JerrySzczurak 20:cf211a3b3d9e 800 }
JerrySzczurak 20:cf211a3b3d9e 801
JerrySzczurak 20:cf211a3b3d9e 802 uint8_t VL53L0X::vl53l0x_decode_vcsel_period( uint8_t vcsel_period_reg )
johnAlexander 0:c523920bcc09 803 {
johnAlexander 15:932d8b4e52c9 804 /*!
johnAlexander 15:932d8b4e52c9 805 * Converts the encoded VCSEL period register value into the real
johnAlexander 15:932d8b4e52c9 806 * period in PLL clocks
johnAlexander 15:932d8b4e52c9 807 */
johnAlexander 15:932d8b4e52c9 808
johnAlexander 15:932d8b4e52c9 809 uint8_t vcsel_period_pclks = 0;
johnAlexander 15:932d8b4e52c9 810
JerrySzczurak 20:cf211a3b3d9e 811 vcsel_period_pclks = ( vcsel_period_reg + 1 ) << 1;
johnAlexander 15:932d8b4e52c9 812
johnAlexander 15:932d8b4e52c9 813 return vcsel_period_pclks;
johnAlexander 0:c523920bcc09 814 }
johnAlexander 0:c523920bcc09 815
JerrySzczurak 20:cf211a3b3d9e 816 uint8_t VL53L0X::lv53l0x_encode_vcsel_period( uint8_t vcsel_period_pclks )
johnAlexander 0:c523920bcc09 817 {
johnAlexander 15:932d8b4e52c9 818 /*!
johnAlexander 15:932d8b4e52c9 819 * Converts the encoded VCSEL period register value into the real period
johnAlexander 15:932d8b4e52c9 820 * in PLL clocks
johnAlexander 15:932d8b4e52c9 821 */
johnAlexander 15:932d8b4e52c9 822
johnAlexander 15:932d8b4e52c9 823 uint8_t vcsel_period_reg = 0;
johnAlexander 15:932d8b4e52c9 824
JerrySzczurak 20:cf211a3b3d9e 825 vcsel_period_reg = ( vcsel_period_pclks >> 1 ) - 1;
johnAlexander 15:932d8b4e52c9 826
johnAlexander 15:932d8b4e52c9 827 return vcsel_period_reg;
johnAlexander 0:c523920bcc09 828 }
johnAlexander 0:c523920bcc09 829
johnAlexander 0:c523920bcc09 830
JerrySzczurak 20:cf211a3b3d9e 831 VL53L0X_Error VL53L0X::wrapped_vl53l0x_set_vcsel_pulse_period( VL53L0X_DEV dev,
JerrySzczurak 20:cf211a3b3d9e 832 VL53L0X_VcselPeriod vcsel_period_type, uint8_t vcsel_pulse_period_pclk )
johnAlexander 0:c523920bcc09 833 {
johnAlexander 16:98ce55ddbb1a 834 VL53L0X_Error status = VL53L0X_ERROR_NONE;
johnAlexander 15:932d8b4e52c9 835 uint8_t vcsel_period_reg;
johnAlexander 16:98ce55ddbb1a 836 uint8_t min_pre_vcsel_period_pclk = 12;
johnAlexander 16:98ce55ddbb1a 837 uint8_t max_pre_vcsel_period_pclk = 18;
johnAlexander 16:98ce55ddbb1a 838 uint8_t min_final_vcsel_period_pclk = 8;
johnAlexander 16:98ce55ddbb1a 839 uint8_t max_final_vcsel_period_pclk = 14;
johnAlexander 16:98ce55ddbb1a 840 uint32_t measurement_timing_budget_micro_seconds;
johnAlexander 16:98ce55ddbb1a 841 uint32_t final_range_timeout_micro_seconds;
johnAlexander 16:98ce55ddbb1a 842 uint32_t pre_range_timeout_micro_seconds;
johnAlexander 16:98ce55ddbb1a 843 uint32_t msrc_timeout_micro_seconds;
johnAlexander 16:98ce55ddbb1a 844 uint8_t phase_cal_int = 0;
johnAlexander 15:932d8b4e52c9 845
johnAlexander 15:932d8b4e52c9 846 /* Check if valid clock period requested */
johnAlexander 15:932d8b4e52c9 847
JerrySzczurak 20:cf211a3b3d9e 848 if ( ( vcsel_pulse_period_pclk % 2 ) != 0 ) {
johnAlexander 15:932d8b4e52c9 849 /* Value must be an even number */
johnAlexander 16:98ce55ddbb1a 850 status = VL53L0X_ERROR_INVALID_PARAMS;
JerrySzczurak 20:cf211a3b3d9e 851 } else if ( vcsel_period_type == VL53L0X_VCSEL_PERIOD_PRE_RANGE &&
JerrySzczurak 20:cf211a3b3d9e 852 ( vcsel_pulse_period_pclk < min_pre_vcsel_period_pclk ||
JerrySzczurak 20:cf211a3b3d9e 853 vcsel_pulse_period_pclk > max_pre_vcsel_period_pclk ) ) {
johnAlexander 16:98ce55ddbb1a 854 status = VL53L0X_ERROR_INVALID_PARAMS;
JerrySzczurak 20:cf211a3b3d9e 855 } else if ( vcsel_period_type == VL53L0X_VCSEL_PERIOD_FINAL_RANGE &&
JerrySzczurak 20:cf211a3b3d9e 856 ( vcsel_pulse_period_pclk < min_final_vcsel_period_pclk ||
JerrySzczurak 20:cf211a3b3d9e 857 vcsel_pulse_period_pclk > max_final_vcsel_period_pclk ) ) {
johnAlexander 16:98ce55ddbb1a 858
johnAlexander 16:98ce55ddbb1a 859 status = VL53L0X_ERROR_INVALID_PARAMS;
johnAlexander 15:932d8b4e52c9 860 }
johnAlexander 15:932d8b4e52c9 861
johnAlexander 15:932d8b4e52c9 862 /* Apply specific settings for the requested clock period */
johnAlexander 15:932d8b4e52c9 863
JerrySzczurak 20:cf211a3b3d9e 864 if ( status != VL53L0X_ERROR_NONE )
johnAlexander 16:98ce55ddbb1a 865 return status;
johnAlexander 16:98ce55ddbb1a 866
johnAlexander 16:98ce55ddbb1a 867
JerrySzczurak 20:cf211a3b3d9e 868 if ( vcsel_period_type == VL53L0X_VCSEL_PERIOD_PRE_RANGE ) {
johnAlexander 15:932d8b4e52c9 869
johnAlexander 15:932d8b4e52c9 870 /* Set phase check limits */
JerrySzczurak 20:cf211a3b3d9e 871 if ( vcsel_pulse_period_pclk == 12 ) {
JerrySzczurak 20:cf211a3b3d9e 872
JerrySzczurak 20:cf211a3b3d9e 873 status = vl53l0x_write_byte( dev,
JerrySzczurak 20:cf211a3b3d9e 874 VL53L0X_REG_PRE_RANGE_CONFIG_VALID_PHASE_HIGH,
JerrySzczurak 20:cf211a3b3d9e 875 0x18 );
JerrySzczurak 20:cf211a3b3d9e 876 status = vl53l0x_write_byte( dev,
JerrySzczurak 20:cf211a3b3d9e 877 VL53L0X_REG_PRE_RANGE_CONFIG_VALID_PHASE_LOW,
JerrySzczurak 20:cf211a3b3d9e 878 0x08 );
JerrySzczurak 20:cf211a3b3d9e 879 } else if ( vcsel_pulse_period_pclk == 14 ) {
JerrySzczurak 20:cf211a3b3d9e 880
JerrySzczurak 20:cf211a3b3d9e 881 status = vl53l0x_write_byte( dev,
JerrySzczurak 20:cf211a3b3d9e 882 VL53L0X_REG_PRE_RANGE_CONFIG_VALID_PHASE_HIGH,
JerrySzczurak 20:cf211a3b3d9e 883 0x30 );
JerrySzczurak 20:cf211a3b3d9e 884 status = vl53l0x_write_byte( dev,
JerrySzczurak 20:cf211a3b3d9e 885 VL53L0X_REG_PRE_RANGE_CONFIG_VALID_PHASE_LOW,
JerrySzczurak 20:cf211a3b3d9e 886 0x08 );
JerrySzczurak 20:cf211a3b3d9e 887 } else if ( vcsel_pulse_period_pclk == 16 ) {
JerrySzczurak 20:cf211a3b3d9e 888
JerrySzczurak 20:cf211a3b3d9e 889 status = vl53l0x_write_byte( dev,
JerrySzczurak 20:cf211a3b3d9e 890 VL53L0X_REG_PRE_RANGE_CONFIG_VALID_PHASE_HIGH,
JerrySzczurak 20:cf211a3b3d9e 891 0x40 );
JerrySzczurak 20:cf211a3b3d9e 892 status = vl53l0x_write_byte( dev,
JerrySzczurak 20:cf211a3b3d9e 893 VL53L0X_REG_PRE_RANGE_CONFIG_VALID_PHASE_LOW,
JerrySzczurak 20:cf211a3b3d9e 894 0x08 );
JerrySzczurak 20:cf211a3b3d9e 895 } else if ( vcsel_pulse_period_pclk == 18 ) {
JerrySzczurak 20:cf211a3b3d9e 896
JerrySzczurak 20:cf211a3b3d9e 897 status = vl53l0x_write_byte( dev,
JerrySzczurak 20:cf211a3b3d9e 898 VL53L0X_REG_PRE_RANGE_CONFIG_VALID_PHASE_HIGH,
JerrySzczurak 20:cf211a3b3d9e 899 0x50 );
JerrySzczurak 20:cf211a3b3d9e 900 status = vl53l0x_write_byte( dev,
JerrySzczurak 20:cf211a3b3d9e 901 VL53L0X_REG_PRE_RANGE_CONFIG_VALID_PHASE_LOW,
JerrySzczurak 20:cf211a3b3d9e 902 0x08 );
johnAlexander 15:932d8b4e52c9 903 }
JerrySzczurak 20:cf211a3b3d9e 904 } else if ( vcsel_period_type == VL53L0X_VCSEL_PERIOD_FINAL_RANGE ) {
JerrySzczurak 20:cf211a3b3d9e 905
JerrySzczurak 20:cf211a3b3d9e 906 if ( vcsel_pulse_period_pclk == 8 ) {
JerrySzczurak 20:cf211a3b3d9e 907
JerrySzczurak 20:cf211a3b3d9e 908 status = vl53l0x_write_byte( dev,
JerrySzczurak 20:cf211a3b3d9e 909 VL53L0X_REG_FINAL_RANGE_CONFIG_VALID_PHASE_HIGH,
JerrySzczurak 20:cf211a3b3d9e 910 0x10 );
JerrySzczurak 20:cf211a3b3d9e 911 status = vl53l0x_write_byte( dev,
JerrySzczurak 20:cf211a3b3d9e 912 VL53L0X_REG_FINAL_RANGE_CONFIG_VALID_PHASE_LOW,
JerrySzczurak 20:cf211a3b3d9e 913 0x08 );
JerrySzczurak 20:cf211a3b3d9e 914
JerrySzczurak 20:cf211a3b3d9e 915 status |= vl53l0x_write_byte( dev,
JerrySzczurak 20:cf211a3b3d9e 916 VL53L0X_REG_GLOBAL_CONFIG_VCSEL_WIDTH, 0x02 );
JerrySzczurak 20:cf211a3b3d9e 917 status |= vl53l0x_write_byte( dev,
JerrySzczurak 20:cf211a3b3d9e 918 VL53L0X_REG_ALGO_PHASECAL_CONFIG_TIMEOUT, 0x0C );
JerrySzczurak 20:cf211a3b3d9e 919
JerrySzczurak 20:cf211a3b3d9e 920 status |= vl53l0x_write_byte( dev, 0xff, 0x01 );
JerrySzczurak 20:cf211a3b3d9e 921 status |= vl53l0x_write_byte( dev,
JerrySzczurak 20:cf211a3b3d9e 922 VL53L0X_REG_ALGO_PHASECAL_LIM,
JerrySzczurak 20:cf211a3b3d9e 923 0x30 );
JerrySzczurak 20:cf211a3b3d9e 924 status |= vl53l0x_write_byte( dev, 0xff, 0x00 );
JerrySzczurak 20:cf211a3b3d9e 925 } else if ( vcsel_pulse_period_pclk == 10 ) {
JerrySzczurak 20:cf211a3b3d9e 926
JerrySzczurak 20:cf211a3b3d9e 927 status = vl53l0x_write_byte( dev,
JerrySzczurak 20:cf211a3b3d9e 928 VL53L0X_REG_FINAL_RANGE_CONFIG_VALID_PHASE_HIGH,
JerrySzczurak 20:cf211a3b3d9e 929 0x28 );
JerrySzczurak 20:cf211a3b3d9e 930 status = vl53l0x_write_byte( dev,
JerrySzczurak 20:cf211a3b3d9e 931 VL53L0X_REG_FINAL_RANGE_CONFIG_VALID_PHASE_LOW,
JerrySzczurak 20:cf211a3b3d9e 932 0x08 );
JerrySzczurak 20:cf211a3b3d9e 933
JerrySzczurak 20:cf211a3b3d9e 934 status |= vl53l0x_write_byte( dev,
JerrySzczurak 20:cf211a3b3d9e 935 VL53L0X_REG_GLOBAL_CONFIG_VCSEL_WIDTH, 0x03 );
JerrySzczurak 20:cf211a3b3d9e 936 status |= vl53l0x_write_byte( dev,
JerrySzczurak 20:cf211a3b3d9e 937 VL53L0X_REG_ALGO_PHASECAL_CONFIG_TIMEOUT, 0x09 );
JerrySzczurak 20:cf211a3b3d9e 938
JerrySzczurak 20:cf211a3b3d9e 939 status |= vl53l0x_write_byte( dev, 0xff, 0x01 );
JerrySzczurak 20:cf211a3b3d9e 940 status |= vl53l0x_write_byte( dev,
JerrySzczurak 20:cf211a3b3d9e 941 VL53L0X_REG_ALGO_PHASECAL_LIM,
JerrySzczurak 20:cf211a3b3d9e 942 0x20 );
JerrySzczurak 20:cf211a3b3d9e 943 status |= vl53l0x_write_byte( dev, 0xff, 0x00 );
JerrySzczurak 20:cf211a3b3d9e 944 } else if ( vcsel_pulse_period_pclk == 12 ) {
JerrySzczurak 20:cf211a3b3d9e 945
JerrySzczurak 20:cf211a3b3d9e 946 status = vl53l0x_write_byte( dev,
JerrySzczurak 20:cf211a3b3d9e 947 VL53L0X_REG_FINAL_RANGE_CONFIG_VALID_PHASE_HIGH,
JerrySzczurak 20:cf211a3b3d9e 948 0x38 );
JerrySzczurak 20:cf211a3b3d9e 949 status = vl53l0x_write_byte( dev,
JerrySzczurak 20:cf211a3b3d9e 950 VL53L0X_REG_FINAL_RANGE_CONFIG_VALID_PHASE_LOW,
JerrySzczurak 20:cf211a3b3d9e 951 0x08 );
JerrySzczurak 20:cf211a3b3d9e 952
JerrySzczurak 20:cf211a3b3d9e 953 status |= vl53l0x_write_byte( dev,
JerrySzczurak 20:cf211a3b3d9e 954 VL53L0X_REG_GLOBAL_CONFIG_VCSEL_WIDTH, 0x03 );
JerrySzczurak 20:cf211a3b3d9e 955 status |= vl53l0x_write_byte( dev,
JerrySzczurak 20:cf211a3b3d9e 956 VL53L0X_REG_ALGO_PHASECAL_CONFIG_TIMEOUT, 0x08 );
JerrySzczurak 20:cf211a3b3d9e 957
JerrySzczurak 20:cf211a3b3d9e 958 status |= vl53l0x_write_byte( dev, 0xff, 0x01 );
JerrySzczurak 20:cf211a3b3d9e 959 status |= vl53l0x_write_byte( dev,
JerrySzczurak 20:cf211a3b3d9e 960 VL53L0X_REG_ALGO_PHASECAL_LIM,
JerrySzczurak 20:cf211a3b3d9e 961 0x20 );
JerrySzczurak 20:cf211a3b3d9e 962 status |= vl53l0x_write_byte( dev, 0xff, 0x00 );
JerrySzczurak 20:cf211a3b3d9e 963 } else if ( vcsel_pulse_period_pclk == 14 ) {
JerrySzczurak 20:cf211a3b3d9e 964
JerrySzczurak 20:cf211a3b3d9e 965 status = vl53l0x_write_byte( dev,
JerrySzczurak 20:cf211a3b3d9e 966 VL53L0X_REG_FINAL_RANGE_CONFIG_VALID_PHASE_HIGH,
JerrySzczurak 20:cf211a3b3d9e 967 0x048 );
JerrySzczurak 20:cf211a3b3d9e 968 status = vl53l0x_write_byte( dev,
JerrySzczurak 20:cf211a3b3d9e 969 VL53L0X_REG_FINAL_RANGE_CONFIG_VALID_PHASE_LOW,
JerrySzczurak 20:cf211a3b3d9e 970 0x08 );
JerrySzczurak 20:cf211a3b3d9e 971
JerrySzczurak 20:cf211a3b3d9e 972 status |= vl53l0x_write_byte( dev,
JerrySzczurak 20:cf211a3b3d9e 973 VL53L0X_REG_GLOBAL_CONFIG_VCSEL_WIDTH, 0x03 );
JerrySzczurak 20:cf211a3b3d9e 974 status |= vl53l0x_write_byte( dev,
JerrySzczurak 20:cf211a3b3d9e 975 VL53L0X_REG_ALGO_PHASECAL_CONFIG_TIMEOUT, 0x07 );
JerrySzczurak 20:cf211a3b3d9e 976
JerrySzczurak 20:cf211a3b3d9e 977 status |= vl53l0x_write_byte( dev, 0xff, 0x01 );
JerrySzczurak 20:cf211a3b3d9e 978 status |= vl53l0x_write_byte( dev,
JerrySzczurak 20:cf211a3b3d9e 979 VL53L0X_REG_ALGO_PHASECAL_LIM,
JerrySzczurak 20:cf211a3b3d9e 980 0x20 );
JerrySzczurak 20:cf211a3b3d9e 981 status |= vl53l0x_write_byte( dev, 0xff, 0x00 );
johnAlexander 15:932d8b4e52c9 982 }
johnAlexander 15:932d8b4e52c9 983 }
johnAlexander 15:932d8b4e52c9 984
johnAlexander 15:932d8b4e52c9 985
johnAlexander 15:932d8b4e52c9 986 /* Re-calculate and apply timeouts, in macro periods */
johnAlexander 15:932d8b4e52c9 987
JerrySzczurak 20:cf211a3b3d9e 988 if ( status == VL53L0X_ERROR_NONE ) {
JerrySzczurak 20:cf211a3b3d9e 989 vcsel_period_reg = lv53l0x_encode_vcsel_period( ( uint8_t )
JerrySzczurak 20:cf211a3b3d9e 990 vcsel_pulse_period_pclk );
johnAlexander 15:932d8b4e52c9 991
johnAlexander 15:932d8b4e52c9 992 /* When the VCSEL period for the pre or final range is changed,
johnAlexander 15:932d8b4e52c9 993 * the corresponding timeout must be read from the device using
johnAlexander 15:932d8b4e52c9 994 * the current VCSEL period, then the new VCSEL period can be
johnAlexander 15:932d8b4e52c9 995 * applied. The timeout then must be written back to the device
johnAlexander 15:932d8b4e52c9 996 * using the new VCSEL period.
johnAlexander 15:932d8b4e52c9 997 *
johnAlexander 15:932d8b4e52c9 998 * For the MSRC timeout, the same applies - this timeout being
johnAlexander 15:932d8b4e52c9 999 * dependant on the pre-range vcsel period.
johnAlexander 15:932d8b4e52c9 1000 */
JerrySzczurak 20:cf211a3b3d9e 1001 switch ( vcsel_period_type ) {
johnAlexander 15:932d8b4e52c9 1002 case VL53L0X_VCSEL_PERIOD_PRE_RANGE:
JerrySzczurak 20:cf211a3b3d9e 1003 status = get_sequence_step_timeout( dev,
JerrySzczurak 20:cf211a3b3d9e 1004 VL53L0X_SEQUENCESTEP_PRE_RANGE,
JerrySzczurak 20:cf211a3b3d9e 1005 &pre_range_timeout_micro_seconds );
JerrySzczurak 20:cf211a3b3d9e 1006
JerrySzczurak 20:cf211a3b3d9e 1007 if ( status == VL53L0X_ERROR_NONE )
JerrySzczurak 20:cf211a3b3d9e 1008 status = get_sequence_step_timeout( dev,
JerrySzczurak 20:cf211a3b3d9e 1009 VL53L0X_SEQUENCESTEP_MSRC,
JerrySzczurak 20:cf211a3b3d9e 1010 &msrc_timeout_micro_seconds );
JerrySzczurak 20:cf211a3b3d9e 1011
JerrySzczurak 20:cf211a3b3d9e 1012 if ( status == VL53L0X_ERROR_NONE )
JerrySzczurak 20:cf211a3b3d9e 1013 status = vl53l0x_write_byte( dev,
JerrySzczurak 20:cf211a3b3d9e 1014 VL53L0X_REG_PRE_RANGE_CONFIG_VCSEL_PERIOD,
JerrySzczurak 20:cf211a3b3d9e 1015 vcsel_period_reg );
JerrySzczurak 20:cf211a3b3d9e 1016
JerrySzczurak 20:cf211a3b3d9e 1017
JerrySzczurak 20:cf211a3b3d9e 1018 if ( status == VL53L0X_ERROR_NONE )
JerrySzczurak 20:cf211a3b3d9e 1019 status = set_sequence_step_timeout( dev,
JerrySzczurak 20:cf211a3b3d9e 1020 VL53L0X_SEQUENCESTEP_PRE_RANGE,
JerrySzczurak 20:cf211a3b3d9e 1021 pre_range_timeout_micro_seconds );
JerrySzczurak 20:cf211a3b3d9e 1022
JerrySzczurak 20:cf211a3b3d9e 1023
JerrySzczurak 20:cf211a3b3d9e 1024 if ( status == VL53L0X_ERROR_NONE )
JerrySzczurak 20:cf211a3b3d9e 1025 status = set_sequence_step_timeout( dev,
JerrySzczurak 20:cf211a3b3d9e 1026 VL53L0X_SEQUENCESTEP_MSRC,
JerrySzczurak 20:cf211a3b3d9e 1027 msrc_timeout_micro_seconds );
johnAlexander 15:932d8b4e52c9 1028
johnAlexander 15:932d8b4e52c9 1029 VL53L0X_SETDEVICESPECIFICPARAMETER(
johnAlexander 16:98ce55ddbb1a 1030 dev,
johnAlexander 15:932d8b4e52c9 1031 PreRangeVcselPulsePeriod,
JerrySzczurak 20:cf211a3b3d9e 1032 vcsel_pulse_period_pclk );
johnAlexander 15:932d8b4e52c9 1033 break;
johnAlexander 15:932d8b4e52c9 1034 case VL53L0X_VCSEL_PERIOD_FINAL_RANGE:
JerrySzczurak 20:cf211a3b3d9e 1035 status = get_sequence_step_timeout( dev,
JerrySzczurak 20:cf211a3b3d9e 1036 VL53L0X_SEQUENCESTEP_FINAL_RANGE,
JerrySzczurak 20:cf211a3b3d9e 1037 &final_range_timeout_micro_seconds );
JerrySzczurak 20:cf211a3b3d9e 1038
JerrySzczurak 20:cf211a3b3d9e 1039 if ( status == VL53L0X_ERROR_NONE )
JerrySzczurak 20:cf211a3b3d9e 1040 status = vl53l0x_write_byte( dev,
JerrySzczurak 20:cf211a3b3d9e 1041 VL53L0X_REG_FINAL_RANGE_CONFIG_VCSEL_PERIOD,
JerrySzczurak 20:cf211a3b3d9e 1042 vcsel_period_reg );
JerrySzczurak 20:cf211a3b3d9e 1043
JerrySzczurak 20:cf211a3b3d9e 1044
JerrySzczurak 20:cf211a3b3d9e 1045 if ( status == VL53L0X_ERROR_NONE )
JerrySzczurak 20:cf211a3b3d9e 1046 status = set_sequence_step_timeout( dev,
JerrySzczurak 20:cf211a3b3d9e 1047 VL53L0X_SEQUENCESTEP_FINAL_RANGE,
JerrySzczurak 20:cf211a3b3d9e 1048 final_range_timeout_micro_seconds );
johnAlexander 15:932d8b4e52c9 1049
johnAlexander 15:932d8b4e52c9 1050 VL53L0X_SETDEVICESPECIFICPARAMETER(
johnAlexander 16:98ce55ddbb1a 1051 dev,
johnAlexander 15:932d8b4e52c9 1052 FinalRangeVcselPulsePeriod,
JerrySzczurak 20:cf211a3b3d9e 1053 vcsel_pulse_period_pclk );
johnAlexander 15:932d8b4e52c9 1054 break;
johnAlexander 15:932d8b4e52c9 1055 default:
johnAlexander 16:98ce55ddbb1a 1056 status = VL53L0X_ERROR_INVALID_PARAMS;
johnAlexander 15:932d8b4e52c9 1057 }
johnAlexander 15:932d8b4e52c9 1058 }
johnAlexander 15:932d8b4e52c9 1059
johnAlexander 15:932d8b4e52c9 1060 /* Finally, the timing budget must be re-applied */
JerrySzczurak 20:cf211a3b3d9e 1061 if ( status == VL53L0X_ERROR_NONE ) {
JerrySzczurak 20:cf211a3b3d9e 1062 VL53L0X_GETPARAMETERFIELD( dev,
JerrySzczurak 20:cf211a3b3d9e 1063 MeasurementTimingBudgetMicroSeconds,
JerrySzczurak 20:cf211a3b3d9e 1064 measurement_timing_budget_micro_seconds );
JerrySzczurak 20:cf211a3b3d9e 1065
JerrySzczurak 20:cf211a3b3d9e 1066 status = vl53l0x_set_measurement_timing_budget_micro_seconds( dev,
JerrySzczurak 20:cf211a3b3d9e 1067 measurement_timing_budget_micro_seconds );
johnAlexander 15:932d8b4e52c9 1068 }
johnAlexander 15:932d8b4e52c9 1069
johnAlexander 15:932d8b4e52c9 1070 /* Perform the phase calibration. This is needed after changing on
johnAlexander 15:932d8b4e52c9 1071 * vcsel period.
johnAlexander 15:932d8b4e52c9 1072 * get_data_enable = 0, restore_config = 1 */
JerrySzczurak 20:cf211a3b3d9e 1073 if ( status == VL53L0X_ERROR_NONE )
johnAlexander 16:98ce55ddbb1a 1074 status = vl53l0x_perform_phase_calibration(
JerrySzczurak 20:cf211a3b3d9e 1075 dev, &phase_cal_int, 0, 1 );
JerrySzczurak 20:cf211a3b3d9e 1076
JerrySzczurak 20:cf211a3b3d9e 1077 return status;
JerrySzczurak 20:cf211a3b3d9e 1078 }
JerrySzczurak 20:cf211a3b3d9e 1079
JerrySzczurak 20:cf211a3b3d9e 1080 VL53L0X_Error VL53L0X::vl53l0x_set_vcsel_pulse_period( VL53L0X_DEV dev,
JerrySzczurak 20:cf211a3b3d9e 1081 VL53L0X_VcselPeriod vcsel_period_type, uint8_t vcsel_pulse_period )
johnAlexander 16:98ce55ddbb1a 1082 {
johnAlexander 16:98ce55ddbb1a 1083 VL53L0X_Error status = VL53L0X_ERROR_NONE;
JerrySzczurak 20:cf211a3b3d9e 1084 LOG_FUNCTION_START( "" );
JerrySzczurak 20:cf211a3b3d9e 1085
JerrySzczurak 20:cf211a3b3d9e 1086 status = wrapped_vl53l0x_set_vcsel_pulse_period( dev, vcsel_period_type,
JerrySzczurak 20:cf211a3b3d9e 1087 vcsel_pulse_period );
JerrySzczurak 20:cf211a3b3d9e 1088
JerrySzczurak 20:cf211a3b3d9e 1089 LOG_FUNCTION_END( status );
JerrySzczurak 20:cf211a3b3d9e 1090 return status;
JerrySzczurak 20:cf211a3b3d9e 1091 }
JerrySzczurak 20:cf211a3b3d9e 1092
JerrySzczurak 20:cf211a3b3d9e 1093 VL53L0X_Error VL53L0X::wrapped_vl53l0x_get_vcsel_pulse_period( VL53L0X_DEV dev,
JerrySzczurak 20:cf211a3b3d9e 1094 VL53L0X_VcselPeriod vcsel_period_type, uint8_t *p_vcsel_pulse_period_pclk )
johnAlexander 0:c523920bcc09 1095 {
johnAlexander 16:98ce55ddbb1a 1096 VL53L0X_Error status = VL53L0X_ERROR_NONE;
johnAlexander 16:98ce55ddbb1a 1097 uint8_t vcsel_period_reg;
johnAlexander 16:98ce55ddbb1a 1098
JerrySzczurak 20:cf211a3b3d9e 1099 switch ( vcsel_period_type ) {
johnAlexander 16:98ce55ddbb1a 1100 case VL53L0X_VCSEL_PERIOD_PRE_RANGE:
JerrySzczurak 20:cf211a3b3d9e 1101 status = vl53l0x_read_byte( dev,
JerrySzczurak 20:cf211a3b3d9e 1102 VL53L0X_REG_PRE_RANGE_CONFIG_VCSEL_PERIOD,
JerrySzczurak 20:cf211a3b3d9e 1103 &vcsel_period_reg );
johnAlexander 16:98ce55ddbb1a 1104 break;
johnAlexander 16:98ce55ddbb1a 1105 case VL53L0X_VCSEL_PERIOD_FINAL_RANGE:
JerrySzczurak 20:cf211a3b3d9e 1106 status = vl53l0x_read_byte( dev,
JerrySzczurak 20:cf211a3b3d9e 1107 VL53L0X_REG_FINAL_RANGE_CONFIG_VCSEL_PERIOD,
JerrySzczurak 20:cf211a3b3d9e 1108 &vcsel_period_reg );
johnAlexander 16:98ce55ddbb1a 1109 break;
johnAlexander 16:98ce55ddbb1a 1110 default:
johnAlexander 16:98ce55ddbb1a 1111 status = VL53L0X_ERROR_INVALID_PARAMS;
johnAlexander 16:98ce55ddbb1a 1112 }
johnAlexander 16:98ce55ddbb1a 1113
JerrySzczurak 20:cf211a3b3d9e 1114 if ( status == VL53L0X_ERROR_NONE )
johnAlexander 16:98ce55ddbb1a 1115 *p_vcsel_pulse_period_pclk =
JerrySzczurak 20:cf211a3b3d9e 1116 vl53l0x_decode_vcsel_period( vcsel_period_reg );
JerrySzczurak 20:cf211a3b3d9e 1117
JerrySzczurak 20:cf211a3b3d9e 1118 return status;
JerrySzczurak 20:cf211a3b3d9e 1119 }
JerrySzczurak 20:cf211a3b3d9e 1120
JerrySzczurak 20:cf211a3b3d9e 1121 VL53L0X_Error VL53L0X::vl53l0x_get_vcsel_pulse_period( VL53L0X_DEV dev,
JerrySzczurak 20:cf211a3b3d9e 1122 VL53L0X_VcselPeriod vcsel_period_type, uint8_t *p_vcsel_pulse_period_pclk )
johnAlexander 0:c523920bcc09 1123 {
johnAlexander 16:98ce55ddbb1a 1124 VL53L0X_Error status = VL53L0X_ERROR_NONE;
JerrySzczurak 20:cf211a3b3d9e 1125 LOG_FUNCTION_START( "" );
JerrySzczurak 20:cf211a3b3d9e 1126
JerrySzczurak 20:cf211a3b3d9e 1127 status = wrapped_vl53l0x_get_vcsel_pulse_period( dev, vcsel_period_type,
JerrySzczurak 20:cf211a3b3d9e 1128 p_vcsel_pulse_period_pclk );
JerrySzczurak 20:cf211a3b3d9e 1129
JerrySzczurak 20:cf211a3b3d9e 1130 LOG_FUNCTION_END( status );
JerrySzczurak 20:cf211a3b3d9e 1131 return status;
JerrySzczurak 20:cf211a3b3d9e 1132 }
JerrySzczurak 20:cf211a3b3d9e 1133
JerrySzczurak 20:cf211a3b3d9e 1134 uint32_t VL53L0X::vl53l0x_decode_timeout( uint16_t encoded_timeout )
johnAlexander 0:c523920bcc09 1135 {
johnAlexander 15:932d8b4e52c9 1136 /*!
johnAlexander 15:932d8b4e52c9 1137 * Decode 16-bit timeout register value - format (LSByte * 2^MSByte) + 1
johnAlexander 15:932d8b4e52c9 1138 */
johnAlexander 15:932d8b4e52c9 1139
johnAlexander 15:932d8b4e52c9 1140 uint32_t timeout_macro_clks = 0;
johnAlexander 15:932d8b4e52c9 1141
JerrySzczurak 20:cf211a3b3d9e 1142 timeout_macro_clks = ( ( uint32_t ) ( encoded_timeout & 0x00FF )
JerrySzczurak 20:cf211a3b3d9e 1143 << ( uint32_t ) ( ( encoded_timeout & 0xFF00 ) >> 8 ) ) + 1;
johnAlexander 15:932d8b4e52c9 1144
johnAlexander 15:932d8b4e52c9 1145 return timeout_macro_clks;
johnAlexander 0:c523920bcc09 1146 }
johnAlexander 0:c523920bcc09 1147
JerrySzczurak 20:cf211a3b3d9e 1148 uint32_t VL53L0X::vl53l0x_calc_macro_period_ps( VL53L0X_DEV dev, uint8_t vcsel_period_pclks )
johnAlexander 0:c523920bcc09 1149 {
johnAlexander 16:98ce55ddbb1a 1150 uint64_t pll_period_ps;
johnAlexander 15:932d8b4e52c9 1151 uint32_t macro_period_vclks;
johnAlexander 15:932d8b4e52c9 1152 uint32_t macro_period_ps;
johnAlexander 15:932d8b4e52c9 1153
JerrySzczurak 20:cf211a3b3d9e 1154 LOG_FUNCTION_START( "" );
johnAlexander 15:932d8b4e52c9 1155
johnAlexander 15:932d8b4e52c9 1156 /* The above calculation will produce rounding errors,
johnAlexander 15:932d8b4e52c9 1157 therefore set fixed value
johnAlexander 15:932d8b4e52c9 1158 */
johnAlexander 16:98ce55ddbb1a 1159 pll_period_ps = 1655;
johnAlexander 15:932d8b4e52c9 1160
johnAlexander 15:932d8b4e52c9 1161 macro_period_vclks = 2304;
JerrySzczurak 20:cf211a3b3d9e 1162 macro_period_ps = ( uint32_t )( macro_period_vclks
JerrySzczurak 20:cf211a3b3d9e 1163 * vcsel_period_pclks * pll_period_ps );
JerrySzczurak 20:cf211a3b3d9e 1164
JerrySzczurak 20:cf211a3b3d9e 1165 LOG_FUNCTION_END( "" );
johnAlexander 15:932d8b4e52c9 1166 return macro_period_ps;
johnAlexander 0:c523920bcc09 1167 }
johnAlexander 0:c523920bcc09 1168
johnAlexander 0:c523920bcc09 1169 /* To convert register value into us */
JerrySzczurak 20:cf211a3b3d9e 1170 uint32_t VL53L0X::vl53l0x_calc_timeout_us( VL53L0X_DEV dev,
johnAlexander 15:932d8b4e52c9 1171 uint16_t timeout_period_mclks,
JerrySzczurak 20:cf211a3b3d9e 1172 uint8_t vcsel_period_pclks )
johnAlexander 0:c523920bcc09 1173 {
johnAlexander 15:932d8b4e52c9 1174 uint32_t macro_period_ps;
johnAlexander 15:932d8b4e52c9 1175 uint32_t macro_period_ns;
johnAlexander 15:932d8b4e52c9 1176 uint32_t actual_timeout_period_us = 0;
johnAlexander 15:932d8b4e52c9 1177
JerrySzczurak 20:cf211a3b3d9e 1178 macro_period_ps = vl53l0x_calc_macro_period_ps( dev, vcsel_period_pclks );
JerrySzczurak 20:cf211a3b3d9e 1179 macro_period_ns = ( macro_period_ps + 500 ) / 1000;
johnAlexander 15:932d8b4e52c9 1180
johnAlexander 15:932d8b4e52c9 1181 actual_timeout_period_us =
JerrySzczurak 20:cf211a3b3d9e 1182 ( ( timeout_period_mclks * macro_period_ns ) + 500 ) / 1000;
johnAlexander 15:932d8b4e52c9 1183
johnAlexander 15:932d8b4e52c9 1184 return actual_timeout_period_us;
johnAlexander 0:c523920bcc09 1185 }
johnAlexander 0:c523920bcc09 1186
JerrySzczurak 20:cf211a3b3d9e 1187 VL53L0X_Error VL53L0X::get_sequence_step_timeout( VL53L0X_DEV dev,
johnAlexander 16:98ce55ddbb1a 1188 VL53L0X_SequenceStepId sequence_step_id,
JerrySzczurak 20:cf211a3b3d9e 1189 uint32_t *p_time_out_micro_secs )
johnAlexander 0:c523920bcc09 1190 {
johnAlexander 16:98ce55ddbb1a 1191 VL53L0X_Error status = VL53L0X_ERROR_NONE;
johnAlexander 16:98ce55ddbb1a 1192 uint8_t current_vcsel_pulse_period_p_clk;
johnAlexander 16:98ce55ddbb1a 1193 uint8_t encoded_time_out_byte = 0;
johnAlexander 16:98ce55ddbb1a 1194 uint32_t timeout_micro_seconds = 0;
johnAlexander 16:98ce55ddbb1a 1195 uint16_t pre_range_encoded_time_out = 0;
johnAlexander 16:98ce55ddbb1a 1196 uint16_t msrc_time_out_m_clks;
johnAlexander 16:98ce55ddbb1a 1197 uint16_t pre_range_time_out_m_clks;
johnAlexander 16:98ce55ddbb1a 1198 uint16_t final_range_time_out_m_clks = 0;
johnAlexander 16:98ce55ddbb1a 1199 uint16_t final_range_encoded_time_out;
johnAlexander 16:98ce55ddbb1a 1200 VL53L0X_SchedulerSequenceSteps_t scheduler_sequence_steps;
johnAlexander 16:98ce55ddbb1a 1201
JerrySzczurak 20:cf211a3b3d9e 1202 if ( ( sequence_step_id == VL53L0X_SEQUENCESTEP_TCC ) ||
JerrySzczurak 20:cf211a3b3d9e 1203 ( sequence_step_id == VL53L0X_SEQUENCESTEP_DSS ) ||
JerrySzczurak 20:cf211a3b3d9e 1204 ( sequence_step_id == VL53L0X_SEQUENCESTEP_MSRC ) ) {
JerrySzczurak 20:cf211a3b3d9e 1205
JerrySzczurak 20:cf211a3b3d9e 1206 status = vl53l0x_get_vcsel_pulse_period( dev,
JerrySzczurak 20:cf211a3b3d9e 1207 VL53L0X_VCSEL_PERIOD_PRE_RANGE,
JerrySzczurak 20:cf211a3b3d9e 1208 &current_vcsel_pulse_period_p_clk );
JerrySzczurak 20:cf211a3b3d9e 1209 if ( status == VL53L0X_ERROR_NONE ) {
JerrySzczurak 20:cf211a3b3d9e 1210 status = vl53l0x_read_byte( dev,
JerrySzczurak 20:cf211a3b3d9e 1211 VL53L0X_REG_MSRC_CONFIG_TIMEOUT_MACROP,
JerrySzczurak 20:cf211a3b3d9e 1212 &encoded_time_out_byte );
johnAlexander 15:932d8b4e52c9 1213 }
JerrySzczurak 20:cf211a3b3d9e 1214 msrc_time_out_m_clks = vl53l0x_decode_timeout( encoded_time_out_byte );
JerrySzczurak 20:cf211a3b3d9e 1215
JerrySzczurak 20:cf211a3b3d9e 1216 timeout_micro_seconds = vl53l0x_calc_timeout_us( dev,
johnAlexander 16:98ce55ddbb1a 1217 msrc_time_out_m_clks,
JerrySzczurak 20:cf211a3b3d9e 1218 current_vcsel_pulse_period_p_clk );
JerrySzczurak 20:cf211a3b3d9e 1219 } else if ( sequence_step_id == VL53L0X_SEQUENCESTEP_PRE_RANGE ) {
johnAlexander 15:932d8b4e52c9 1220 /* Retrieve PRE-RANGE VCSEL Period */
JerrySzczurak 20:cf211a3b3d9e 1221 status = vl53l0x_get_vcsel_pulse_period( dev,
JerrySzczurak 20:cf211a3b3d9e 1222 VL53L0X_VCSEL_PERIOD_PRE_RANGE,
JerrySzczurak 20:cf211a3b3d9e 1223 &current_vcsel_pulse_period_p_clk );
johnAlexander 15:932d8b4e52c9 1224
johnAlexander 15:932d8b4e52c9 1225 /* Retrieve PRE-RANGE Timeout in Macro periods (MCLKS) */
JerrySzczurak 20:cf211a3b3d9e 1226 if ( status == VL53L0X_ERROR_NONE ) {
johnAlexander 15:932d8b4e52c9 1227
johnAlexander 15:932d8b4e52c9 1228 /* Retrieve PRE-RANGE VCSEL Period */
JerrySzczurak 20:cf211a3b3d9e 1229 status = vl53l0x_get_vcsel_pulse_period( dev,
JerrySzczurak 20:cf211a3b3d9e 1230 VL53L0X_VCSEL_PERIOD_PRE_RANGE,
JerrySzczurak 20:cf211a3b3d9e 1231 &current_vcsel_pulse_period_p_clk );
JerrySzczurak 20:cf211a3b3d9e 1232
JerrySzczurak 20:cf211a3b3d9e 1233 if ( status == VL53L0X_ERROR_NONE ) {
JerrySzczurak 20:cf211a3b3d9e 1234 status = vl53l0x_read_word( dev,
JerrySzczurak 20:cf211a3b3d9e 1235 VL53L0X_REG_PRE_RANGE_CONFIG_TIMEOUT_MACROP_HI,
JerrySzczurak 20:cf211a3b3d9e 1236 &pre_range_encoded_time_out );
johnAlexander 15:932d8b4e52c9 1237 }
johnAlexander 15:932d8b4e52c9 1238
johnAlexander 16:98ce55ddbb1a 1239 pre_range_time_out_m_clks = vl53l0x_decode_timeout(
JerrySzczurak 20:cf211a3b3d9e 1240 pre_range_encoded_time_out );
JerrySzczurak 20:cf211a3b3d9e 1241
JerrySzczurak 20:cf211a3b3d9e 1242 timeout_micro_seconds = vl53l0x_calc_timeout_us( dev,
johnAlexander 16:98ce55ddbb1a 1243 pre_range_time_out_m_clks,
JerrySzczurak 20:cf211a3b3d9e 1244 current_vcsel_pulse_period_p_clk );
johnAlexander 15:932d8b4e52c9 1245 }
JerrySzczurak 20:cf211a3b3d9e 1246 } else if ( sequence_step_id == VL53L0X_SEQUENCESTEP_FINAL_RANGE ) {
JerrySzczurak 20:cf211a3b3d9e 1247
JerrySzczurak 20:cf211a3b3d9e 1248 vl53l0x_get_sequence_step_enables( dev, &scheduler_sequence_steps );
johnAlexander 16:98ce55ddbb1a 1249 pre_range_time_out_m_clks = 0;
johnAlexander 16:98ce55ddbb1a 1250
JerrySzczurak 20:cf211a3b3d9e 1251 if ( scheduler_sequence_steps.PreRangeOn ) {
johnAlexander 15:932d8b4e52c9 1252 /* Retrieve PRE-RANGE VCSEL Period */
JerrySzczurak 20:cf211a3b3d9e 1253 status = vl53l0x_get_vcsel_pulse_period( dev,
JerrySzczurak 20:cf211a3b3d9e 1254 VL53L0X_VCSEL_PERIOD_PRE_RANGE,
JerrySzczurak 20:cf211a3b3d9e 1255 &current_vcsel_pulse_period_p_clk );
johnAlexander 15:932d8b4e52c9 1256
johnAlexander 15:932d8b4e52c9 1257 /* Retrieve PRE-RANGE Timeout in Macro periods
johnAlexander 15:932d8b4e52c9 1258 * (MCLKS) */
JerrySzczurak 20:cf211a3b3d9e 1259 if ( status == VL53L0X_ERROR_NONE ) {
JerrySzczurak 20:cf211a3b3d9e 1260 status = vl53l0x_read_word( dev,
JerrySzczurak 20:cf211a3b3d9e 1261 VL53L0X_REG_PRE_RANGE_CONFIG_TIMEOUT_MACROP_HI,
JerrySzczurak 20:cf211a3b3d9e 1262 &pre_range_encoded_time_out );
johnAlexander 16:98ce55ddbb1a 1263 pre_range_time_out_m_clks = vl53l0x_decode_timeout(
JerrySzczurak 20:cf211a3b3d9e 1264 pre_range_encoded_time_out );
johnAlexander 15:932d8b4e52c9 1265 }
johnAlexander 15:932d8b4e52c9 1266 }
johnAlexander 15:932d8b4e52c9 1267
JerrySzczurak 20:cf211a3b3d9e 1268 if ( status == VL53L0X_ERROR_NONE ) {
johnAlexander 15:932d8b4e52c9 1269 /* Retrieve FINAL-RANGE VCSEL Period */
JerrySzczurak 20:cf211a3b3d9e 1270 status = vl53l0x_get_vcsel_pulse_period( dev,
JerrySzczurak 20:cf211a3b3d9e 1271 VL53L0X_VCSEL_PERIOD_FINAL_RANGE,
JerrySzczurak 20:cf211a3b3d9e 1272 &current_vcsel_pulse_period_p_clk );
johnAlexander 15:932d8b4e52c9 1273 }
johnAlexander 15:932d8b4e52c9 1274
johnAlexander 15:932d8b4e52c9 1275 /* Retrieve FINAL-RANGE Timeout in Macro periods (MCLKS) */
JerrySzczurak 20:cf211a3b3d9e 1276 if ( status == VL53L0X_ERROR_NONE ) {
JerrySzczurak 20:cf211a3b3d9e 1277 status = vl53l0x_read_word( dev,
JerrySzczurak 20:cf211a3b3d9e 1278 VL53L0X_REG_FINAL_RANGE_CONFIG_TIMEOUT_MACROP_HI,
JerrySzczurak 20:cf211a3b3d9e 1279 &final_range_encoded_time_out );
johnAlexander 16:98ce55ddbb1a 1280 final_range_time_out_m_clks = vl53l0x_decode_timeout(
JerrySzczurak 20:cf211a3b3d9e 1281 final_range_encoded_time_out );
johnAlexander 15:932d8b4e52c9 1282 }
johnAlexander 15:932d8b4e52c9 1283
johnAlexander 16:98ce55ddbb1a 1284 final_range_time_out_m_clks -= pre_range_time_out_m_clks;
JerrySzczurak 20:cf211a3b3d9e 1285 timeout_micro_seconds = vl53l0x_calc_timeout_us( dev,
johnAlexander 16:98ce55ddbb1a 1286 final_range_time_out_m_clks,
JerrySzczurak 20:cf211a3b3d9e 1287 current_vcsel_pulse_period_p_clk );
johnAlexander 15:932d8b4e52c9 1288 }
johnAlexander 15:932d8b4e52c9 1289
johnAlexander 16:98ce55ddbb1a 1290 *p_time_out_micro_secs = timeout_micro_seconds;
johnAlexander 16:98ce55ddbb1a 1291
johnAlexander 16:98ce55ddbb1a 1292 return status;
johnAlexander 0:c523920bcc09 1293 }
johnAlexander 0:c523920bcc09 1294
JerrySzczurak 20:cf211a3b3d9e 1295 VL53L0X_Error VL53L0X::wrapped_vl53l0x_get_measurement_timing_budget_micro_seconds( VL53L0X_DEV dev,
JerrySzczurak 20:cf211a3b3d9e 1296 uint32_t *p_measurement_timing_budget_micro_seconds )
johnAlexander 0:c523920bcc09 1297 {
johnAlexander 16:98ce55ddbb1a 1298 VL53L0X_Error status = VL53L0X_ERROR_NONE;
johnAlexander 16:98ce55ddbb1a 1299 VL53L0X_SchedulerSequenceSteps_t scheduler_sequence_steps;
johnAlexander 16:98ce55ddbb1a 1300 uint32_t final_range_timeout_micro_seconds;
johnAlexander 16:98ce55ddbb1a 1301 uint32_t msrc_dcc_tcc_timeout_micro_seconds = 2000;
johnAlexander 16:98ce55ddbb1a 1302 uint32_t start_overhead_micro_seconds = 1910;
johnAlexander 16:98ce55ddbb1a 1303 uint32_t end_overhead_micro_seconds = 960;
johnAlexander 16:98ce55ddbb1a 1304 uint32_t msrc_overhead_micro_seconds = 660;
johnAlexander 16:98ce55ddbb1a 1305 uint32_t tcc_overhead_micro_seconds = 590;
johnAlexander 16:98ce55ddbb1a 1306 uint32_t dss_overhead_micro_seconds = 690;
johnAlexander 16:98ce55ddbb1a 1307 uint32_t pre_range_overhead_micro_seconds = 660;
johnAlexander 16:98ce55ddbb1a 1308 uint32_t final_range_overhead_micro_seconds = 550;
johnAlexander 16:98ce55ddbb1a 1309 uint32_t pre_range_timeout_micro_seconds = 0;
johnAlexander 15:932d8b4e52c9 1310
JerrySzczurak 20:cf211a3b3d9e 1311 LOG_FUNCTION_START( "" );
johnAlexander 15:932d8b4e52c9 1312
johnAlexander 15:932d8b4e52c9 1313 /* Start and end overhead times always present */
johnAlexander 16:98ce55ddbb1a 1314 *p_measurement_timing_budget_micro_seconds
johnAlexander 16:98ce55ddbb1a 1315 = start_overhead_micro_seconds + end_overhead_micro_seconds;
johnAlexander 16:98ce55ddbb1a 1316
JerrySzczurak 20:cf211a3b3d9e 1317 status = vl53l0x_get_sequence_step_enables( dev, &scheduler_sequence_steps );
JerrySzczurak 20:cf211a3b3d9e 1318
JerrySzczurak 20:cf211a3b3d9e 1319 if ( status != VL53L0X_ERROR_NONE ) {
JerrySzczurak 20:cf211a3b3d9e 1320 LOG_FUNCTION_END( status );
johnAlexander 16:98ce55ddbb1a 1321 return status;
johnAlexander 15:932d8b4e52c9 1322 }
johnAlexander 15:932d8b4e52c9 1323
johnAlexander 15:932d8b4e52c9 1324
JerrySzczurak 20:cf211a3b3d9e 1325 if ( scheduler_sequence_steps.TccOn ||
johnAlexander 16:98ce55ddbb1a 1326 scheduler_sequence_steps.MsrcOn ||
JerrySzczurak 20:cf211a3b3d9e 1327 scheduler_sequence_steps.DssOn ) {
JerrySzczurak 20:cf211a3b3d9e 1328
JerrySzczurak 20:cf211a3b3d9e 1329 status = get_sequence_step_timeout( dev,
JerrySzczurak 20:cf211a3b3d9e 1330 VL53L0X_SEQUENCESTEP_MSRC,
JerrySzczurak 20:cf211a3b3d9e 1331 &msrc_dcc_tcc_timeout_micro_seconds );
JerrySzczurak 20:cf211a3b3d9e 1332
JerrySzczurak 20:cf211a3b3d9e 1333 if ( status == VL53L0X_ERROR_NONE ) {
JerrySzczurak 20:cf211a3b3d9e 1334 if ( scheduler_sequence_steps.TccOn ) {
johnAlexander 16:98ce55ddbb1a 1335 *p_measurement_timing_budget_micro_seconds +=
johnAlexander 16:98ce55ddbb1a 1336 msrc_dcc_tcc_timeout_micro_seconds +
johnAlexander 16:98ce55ddbb1a 1337 tcc_overhead_micro_seconds;
johnAlexander 15:932d8b4e52c9 1338 }
johnAlexander 15:932d8b4e52c9 1339
JerrySzczurak 20:cf211a3b3d9e 1340 if ( scheduler_sequence_steps.DssOn ) {
johnAlexander 16:98ce55ddbb1a 1341 *p_measurement_timing_budget_micro_seconds +=
JerrySzczurak 20:cf211a3b3d9e 1342 2 * ( msrc_dcc_tcc_timeout_micro_seconds +
JerrySzczurak 20:cf211a3b3d9e 1343 dss_overhead_micro_seconds );
JerrySzczurak 20:cf211a3b3d9e 1344 } else if ( scheduler_sequence_steps.MsrcOn ) {
johnAlexander 16:98ce55ddbb1a 1345 *p_measurement_timing_budget_micro_seconds +=
johnAlexander 16:98ce55ddbb1a 1346 msrc_dcc_tcc_timeout_micro_seconds +
johnAlexander 16:98ce55ddbb1a 1347 msrc_overhead_micro_seconds;
johnAlexander 15:932d8b4e52c9 1348 }
johnAlexander 15:932d8b4e52c9 1349 }
johnAlexander 15:932d8b4e52c9 1350 }
johnAlexander 15:932d8b4e52c9 1351
JerrySzczurak 20:cf211a3b3d9e 1352 if ( status == VL53L0X_ERROR_NONE ) {
JerrySzczurak 20:cf211a3b3d9e 1353 if ( scheduler_sequence_steps.PreRangeOn ) {
JerrySzczurak 20:cf211a3b3d9e 1354 status = get_sequence_step_timeout( dev,
JerrySzczurak 20:cf211a3b3d9e 1355 VL53L0X_SEQUENCESTEP_PRE_RANGE,
JerrySzczurak 20:cf211a3b3d9e 1356 &pre_range_timeout_micro_seconds );
johnAlexander 16:98ce55ddbb1a 1357 *p_measurement_timing_budget_micro_seconds +=
johnAlexander 16:98ce55ddbb1a 1358 pre_range_timeout_micro_seconds +
johnAlexander 16:98ce55ddbb1a 1359 pre_range_overhead_micro_seconds;
johnAlexander 15:932d8b4e52c9 1360 }
johnAlexander 15:932d8b4e52c9 1361 }
johnAlexander 15:932d8b4e52c9 1362
JerrySzczurak 20:cf211a3b3d9e 1363 if ( status == VL53L0X_ERROR_NONE ) {
JerrySzczurak 20:cf211a3b3d9e 1364 if ( scheduler_sequence_steps.FinalRangeOn ) {
JerrySzczurak 20:cf211a3b3d9e 1365 status = get_sequence_step_timeout( dev,
JerrySzczurak 20:cf211a3b3d9e 1366 VL53L0X_SEQUENCESTEP_FINAL_RANGE,
JerrySzczurak 20:cf211a3b3d9e 1367 &final_range_timeout_micro_seconds );
johnAlexander 16:98ce55ddbb1a 1368 *p_measurement_timing_budget_micro_seconds +=
JerrySzczurak 20:cf211a3b3d9e 1369 ( final_range_timeout_micro_seconds +
JerrySzczurak 20:cf211a3b3d9e 1370 final_range_overhead_micro_seconds );
johnAlexander 15:932d8b4e52c9 1371 }
johnAlexander 15:932d8b4e52c9 1372 }
johnAlexander 15:932d8b4e52c9 1373
JerrySzczurak 20:cf211a3b3d9e 1374 if ( status == VL53L0X_ERROR_NONE ) {
JerrySzczurak 20:cf211a3b3d9e 1375 VL53L0X_SETPARAMETERFIELD( dev,
JerrySzczurak 20:cf211a3b3d9e 1376 MeasurementTimingBudgetMicroSeconds,
JerrySzczurak 20:cf211a3b3d9e 1377 *p_measurement_timing_budget_micro_seconds );
JerrySzczurak 20:cf211a3b3d9e 1378 }
JerrySzczurak 20:cf211a3b3d9e 1379
JerrySzczurak 20:cf211a3b3d9e 1380 LOG_FUNCTION_END( status );
JerrySzczurak 20:cf211a3b3d9e 1381 return status;
JerrySzczurak 20:cf211a3b3d9e 1382 }
JerrySzczurak 20:cf211a3b3d9e 1383
JerrySzczurak 20:cf211a3b3d9e 1384 VL53L0X_Error VL53L0X::vl53l0x_get_measurement_timing_budget_micro_seconds( VL53L0X_DEV dev,
JerrySzczurak 20:cf211a3b3d9e 1385 uint32_t *p_measurement_timing_budget_micro_seconds )
johnAlexander 0:c523920bcc09 1386 {
johnAlexander 16:98ce55ddbb1a 1387 VL53L0X_Error status = VL53L0X_ERROR_NONE;
JerrySzczurak 20:cf211a3b3d9e 1388 LOG_FUNCTION_START( "" );
JerrySzczurak 20:cf211a3b3d9e 1389
JerrySzczurak 20:cf211a3b3d9e 1390 status = wrapped_vl53l0x_get_measurement_timing_budget_micro_seconds( dev,
JerrySzczurak 20:cf211a3b3d9e 1391 p_measurement_timing_budget_micro_seconds );
JerrySzczurak 20:cf211a3b3d9e 1392
JerrySzczurak 20:cf211a3b3d9e 1393 LOG_FUNCTION_END( status );
JerrySzczurak 20:cf211a3b3d9e 1394 return status;
JerrySzczurak 20:cf211a3b3d9e 1395 }
JerrySzczurak 20:cf211a3b3d9e 1396
JerrySzczurak 20:cf211a3b3d9e 1397 VL53L0X_Error VL53L0X::vl53l0x_get_device_parameters( VL53L0X_DEV dev,
JerrySzczurak 20:cf211a3b3d9e 1398 VL53L0X_DeviceParameters_t *p_device_parameters )
johnAlexander 0:c523920bcc09 1399 {
johnAlexander 16:98ce55ddbb1a 1400 VL53L0X_Error status = VL53L0X_ERROR_NONE;
johnAlexander 15:932d8b4e52c9 1401 int i;
johnAlexander 15:932d8b4e52c9 1402
JerrySzczurak 20:cf211a3b3d9e 1403 LOG_FUNCTION_START( "" );
JerrySzczurak 20:cf211a3b3d9e 1404
JerrySzczurak 20:cf211a3b3d9e 1405 status = vl53l0x_get_device_mode( dev, &( p_device_parameters->DeviceMode ) );
JerrySzczurak 20:cf211a3b3d9e 1406
JerrySzczurak 20:cf211a3b3d9e 1407 if ( status == VL53L0X_ERROR_NONE )
JerrySzczurak 20:cf211a3b3d9e 1408 status = vl53l0x_get_inter_measurement_period_milli_seconds( dev,
JerrySzczurak 20:cf211a3b3d9e 1409 &( p_device_parameters->InterMeasurementPeriodMilliSeconds ) );
JerrySzczurak 20:cf211a3b3d9e 1410
JerrySzczurak 20:cf211a3b3d9e 1411
JerrySzczurak 20:cf211a3b3d9e 1412 if ( status == VL53L0X_ERROR_NONE )
johnAlexander 17:1b842521063a 1413 p_device_parameters->XTalkCompensationEnable = 0;
johnAlexander 15:932d8b4e52c9 1414
JerrySzczurak 20:cf211a3b3d9e 1415 if ( status == VL53L0X_ERROR_NONE )
JerrySzczurak 20:cf211a3b3d9e 1416 status = vl53l0x_get_x_talk_compensation_rate_mega_cps( dev,
JerrySzczurak 20:cf211a3b3d9e 1417 &( p_device_parameters->XTalkCompensationRateMegaCps ) );
JerrySzczurak 20:cf211a3b3d9e 1418
JerrySzczurak 20:cf211a3b3d9e 1419
JerrySzczurak 20:cf211a3b3d9e 1420 if ( status == VL53L0X_ERROR_NONE )
JerrySzczurak 20:cf211a3b3d9e 1421 status = vl53l0x_get_offset_calibration_data_micro_meter( dev,
JerrySzczurak 20:cf211a3b3d9e 1422 &( p_device_parameters->RangeOffsetMicroMeters ) );
JerrySzczurak 20:cf211a3b3d9e 1423
JerrySzczurak 20:cf211a3b3d9e 1424
JerrySzczurak 20:cf211a3b3d9e 1425 if ( status == VL53L0X_ERROR_NONE ) {
JerrySzczurak 20:cf211a3b3d9e 1426 for ( i = 0; i < VL53L0X_CHECKENABLE_NUMBER_OF_CHECKS; i++ ) {
johnAlexander 15:932d8b4e52c9 1427 /* get first the values, then the enables.
johnAlexander 15:932d8b4e52c9 1428 * VL53L0X_GetLimitCheckValue will modify the enable
johnAlexander 15:932d8b4e52c9 1429 * flags
johnAlexander 15:932d8b4e52c9 1430 */
JerrySzczurak 20:cf211a3b3d9e 1431 if ( status == VL53L0X_ERROR_NONE ) {
JerrySzczurak 20:cf211a3b3d9e 1432 status |= vl53l0x_get_limit_check_value( dev, i,
JerrySzczurak 20:cf211a3b3d9e 1433 &( p_device_parameters->LimitChecksValue[i] ) );
johnAlexander 15:932d8b4e52c9 1434 } else {
johnAlexander 15:932d8b4e52c9 1435 break;
johnAlexander 15:932d8b4e52c9 1436 }
JerrySzczurak 20:cf211a3b3d9e 1437 if ( status == VL53L0X_ERROR_NONE ) {
JerrySzczurak 20:cf211a3b3d9e 1438 status |= vl53l0x_get_limit_check_enable( dev, i,
JerrySzczurak 20:cf211a3b3d9e 1439 &( p_device_parameters->LimitChecksEnable[i] ) );
johnAlexander 15:932d8b4e52c9 1440 } else {
johnAlexander 15:932d8b4e52c9 1441 break;
johnAlexander 15:932d8b4e52c9 1442 }
johnAlexander 15:932d8b4e52c9 1443 }
johnAlexander 15:932d8b4e52c9 1444 }
johnAlexander 15:932d8b4e52c9 1445
JerrySzczurak 20:cf211a3b3d9e 1446 if ( status == VL53L0X_ERROR_NONE ) {
JerrySzczurak 20:cf211a3b3d9e 1447 status = vl53l0x_get_wrap_around_check_enable( dev,
JerrySzczurak 20:cf211a3b3d9e 1448 &( p_device_parameters->WrapAroundCheckEnable ) );
johnAlexander 15:932d8b4e52c9 1449 }
johnAlexander 15:932d8b4e52c9 1450
johnAlexander 15:932d8b4e52c9 1451 /* Need to be done at the end as it uses VCSELPulsePeriod */
JerrySzczurak 20:cf211a3b3d9e 1452 if ( status == VL53L0X_ERROR_NONE ) {
JerrySzczurak 20:cf211a3b3d9e 1453 status = vl53l0x_get_measurement_timing_budget_micro_seconds( dev,
JerrySzczurak 20:cf211a3b3d9e 1454 &( p_device_parameters->MeasurementTimingBudgetMicroSeconds ) );
JerrySzczurak 20:cf211a3b3d9e 1455 }
JerrySzczurak 20:cf211a3b3d9e 1456
JerrySzczurak 20:cf211a3b3d9e 1457 LOG_FUNCTION_END( status );
JerrySzczurak 20:cf211a3b3d9e 1458 return status;
JerrySzczurak 20:cf211a3b3d9e 1459 }
JerrySzczurak 20:cf211a3b3d9e 1460
JerrySzczurak 20:cf211a3b3d9e 1461 VL53L0X_Error VL53L0X::vl53l0x_set_limit_check_value( VL53L0X_DEV dev, uint16_t limit_check_id,
JerrySzczurak 20:cf211a3b3d9e 1462 FixPoint1616_t limit_check_value )
johnAlexander 0:c523920bcc09 1463 {
johnAlexander 16:98ce55ddbb1a 1464 VL53L0X_Error status = VL53L0X_ERROR_NONE;
johnAlexander 16:98ce55ddbb1a 1465 uint8_t temp8;
johnAlexander 15:932d8b4e52c9 1466
JerrySzczurak 20:cf211a3b3d9e 1467 LOG_FUNCTION_START( "" );
JerrySzczurak 20:cf211a3b3d9e 1468
JerrySzczurak 20:cf211a3b3d9e 1469 VL53L0X_GETARRAYPARAMETERFIELD( dev, LimitChecksEnable, limit_check_id,
JerrySzczurak 20:cf211a3b3d9e 1470 temp8 );
JerrySzczurak 20:cf211a3b3d9e 1471
JerrySzczurak 20:cf211a3b3d9e 1472 if ( temp8 == 0 ) { /* disabled write only internal value */
JerrySzczurak 20:cf211a3b3d9e 1473 VL53L0X_SETARRAYPARAMETERFIELD( dev, LimitChecksValue,
JerrySzczurak 20:cf211a3b3d9e 1474 limit_check_id, limit_check_value );
johnAlexander 15:932d8b4e52c9 1475 } else {
johnAlexander 15:932d8b4e52c9 1476
JerrySzczurak 20:cf211a3b3d9e 1477 switch ( limit_check_id ) {
johnAlexander 15:932d8b4e52c9 1478
johnAlexander 15:932d8b4e52c9 1479 case VL53L0X_CHECKENABLE_SIGMA_FINAL_RANGE:
johnAlexander 15:932d8b4e52c9 1480 /* internal computation: */
JerrySzczurak 20:cf211a3b3d9e 1481 VL53L0X_SETARRAYPARAMETERFIELD( dev, LimitChecksValue,
JerrySzczurak 20:cf211a3b3d9e 1482 VL53L0X_CHECKENABLE_SIGMA_FINAL_RANGE,
JerrySzczurak 20:cf211a3b3d9e 1483 limit_check_value );
johnAlexander 15:932d8b4e52c9 1484 break;
johnAlexander 15:932d8b4e52c9 1485
johnAlexander 15:932d8b4e52c9 1486 case VL53L0X_CHECKENABLE_SIGNAL_RATE_FINAL_RANGE:
johnAlexander 15:932d8b4e52c9 1487
JerrySzczurak 20:cf211a3b3d9e 1488 status = vl53l0x_write_word( dev,
JerrySzczurak 20:cf211a3b3d9e 1489 VL53L0X_REG_FINAL_RANGE_CONFIG_MIN_COUNT_RATE_RTN_LIMIT,
JerrySzczurak 20:cf211a3b3d9e 1490 VL53L0X_FIXPOINT1616TOFIXPOINT97(
JerrySzczurak 20:cf211a3b3d9e 1491 limit_check_value ) );
johnAlexander 15:932d8b4e52c9 1492
johnAlexander 15:932d8b4e52c9 1493 break;
johnAlexander 15:932d8b4e52c9 1494
johnAlexander 15:932d8b4e52c9 1495 case VL53L0X_CHECKENABLE_SIGNAL_REF_CLIP:
johnAlexander 15:932d8b4e52c9 1496
johnAlexander 15:932d8b4e52c9 1497 /* internal computation: */
JerrySzczurak 20:cf211a3b3d9e 1498 VL53L0X_SETARRAYPARAMETERFIELD( dev, LimitChecksValue,
JerrySzczurak 20:cf211a3b3d9e 1499 VL53L0X_CHECKENABLE_SIGNAL_REF_CLIP,
JerrySzczurak 20:cf211a3b3d9e 1500 limit_check_value );
johnAlexander 15:932d8b4e52c9 1501
johnAlexander 15:932d8b4e52c9 1502 break;
johnAlexander 15:932d8b4e52c9 1503
johnAlexander 15:932d8b4e52c9 1504 case VL53L0X_CHECKENABLE_RANGE_IGNORE_THRESHOLD:
johnAlexander 15:932d8b4e52c9 1505
johnAlexander 15:932d8b4e52c9 1506 /* internal computation: */
JerrySzczurak 20:cf211a3b3d9e 1507 VL53L0X_SETARRAYPARAMETERFIELD( dev, LimitChecksValue,
JerrySzczurak 20:cf211a3b3d9e 1508 VL53L0X_CHECKENABLE_RANGE_IGNORE_THRESHOLD,
JerrySzczurak 20:cf211a3b3d9e 1509 limit_check_value );
johnAlexander 15:932d8b4e52c9 1510
johnAlexander 15:932d8b4e52c9 1511 break;
johnAlexander 15:932d8b4e52c9 1512
johnAlexander 15:932d8b4e52c9 1513 case VL53L0X_CHECKENABLE_SIGNAL_RATE_MSRC:
johnAlexander 15:932d8b4e52c9 1514 case VL53L0X_CHECKENABLE_SIGNAL_RATE_PRE_RANGE:
johnAlexander 15:932d8b4e52c9 1515
JerrySzczurak 20:cf211a3b3d9e 1516 status = vl53l0x_write_word( dev,
JerrySzczurak 20:cf211a3b3d9e 1517 VL53L0X_REG_PRE_RANGE_MIN_COUNT_RATE_RTN_LIMIT,
JerrySzczurak 20:cf211a3b3d9e 1518 VL53L0X_FIXPOINT1616TOFIXPOINT97(
JerrySzczurak 20:cf211a3b3d9e 1519 limit_check_value ) );
johnAlexander 15:932d8b4e52c9 1520
johnAlexander 15:932d8b4e52c9 1521 break;
johnAlexander 15:932d8b4e52c9 1522
johnAlexander 15:932d8b4e52c9 1523 default:
johnAlexander 16:98ce55ddbb1a 1524 status = VL53L0X_ERROR_INVALID_PARAMS;
johnAlexander 15:932d8b4e52c9 1525
johnAlexander 15:932d8b4e52c9 1526 }
johnAlexander 15:932d8b4e52c9 1527
JerrySzczurak 20:cf211a3b3d9e 1528 if ( status == VL53L0X_ERROR_NONE ) {
JerrySzczurak 20:cf211a3b3d9e 1529 VL53L0X_SETARRAYPARAMETERFIELD( dev, LimitChecksValue,
JerrySzczurak 20:cf211a3b3d9e 1530 limit_check_id, limit_check_value );
johnAlexander 15:932d8b4e52c9 1531 }
johnAlexander 15:932d8b4e52c9 1532 }
johnAlexander 15:932d8b4e52c9 1533
JerrySzczurak 20:cf211a3b3d9e 1534 LOG_FUNCTION_END( status );
JerrySzczurak 20:cf211a3b3d9e 1535 return status;
JerrySzczurak 20:cf211a3b3d9e 1536 }
JerrySzczurak 20:cf211a3b3d9e 1537
JerrySzczurak 20:cf211a3b3d9e 1538 VL53L0X_Error VL53L0X::vl53l0x_data_init( VL53L0X_DEV dev )
johnAlexander 0:c523920bcc09 1539 {
johnAlexander 16:98ce55ddbb1a 1540 VL53L0X_Error status = VL53L0X_ERROR_NONE;
johnAlexander 15:932d8b4e52c9 1541 VL53L0X_DeviceParameters_t CurrentParameters;
johnAlexander 15:932d8b4e52c9 1542 int i;
johnAlexander 15:932d8b4e52c9 1543 uint8_t StopVariable;
johnAlexander 15:932d8b4e52c9 1544
JerrySzczurak 20:cf211a3b3d9e 1545 LOG_FUNCTION_START( "" );
johnAlexander 15:932d8b4e52c9 1546
johnAlexander 15:932d8b4e52c9 1547 /* by default the I2C is running at 1V8 if you want to change it you
johnAlexander 15:932d8b4e52c9 1548 * need to include this define at compilation level. */
johnAlexander 0:c523920bcc09 1549 #ifdef USE_I2C_2V8
JerrySzczurak 21:627b65069e6d 1550 Status = vl53l0x_update_word( Dev,
JerrySzczurak 20:cf211a3b3d9e 1551 VL53L0X_REG_VHV_CONFIG_PAD_SCL_SDA__EXTSUP_HV,
JerrySzczurak 20:cf211a3b3d9e 1552 0xFE,
JerrySzczurak 20:cf211a3b3d9e 1553 0x01 );
johnAlexander 0:c523920bcc09 1554 #endif
johnAlexander 0:c523920bcc09 1555
johnAlexander 15:932d8b4e52c9 1556 /* Set I2C standard mode */
JerrySzczurak 20:cf211a3b3d9e 1557 if ( status == VL53L0X_ERROR_NONE )
JerrySzczurak 20:cf211a3b3d9e 1558 status = vl53l0x_write_byte( dev, 0x88, 0x00 );
JerrySzczurak 20:cf211a3b3d9e 1559
JerrySzczurak 20:cf211a3b3d9e 1560 VL53L0X_SETDEVICESPECIFICPARAMETER( dev, ReadDataFromDeviceDone, 0 );
johnAlexander 0:c523920bcc09 1561
johnAlexander 0:c523920bcc09 1562 #ifdef USE_IQC_STATION
JerrySzczurak 20:cf211a3b3d9e 1563 if ( Status == VL53L0X_ERROR_NONE )
JerrySzczurak 20:cf211a3b3d9e 1564 Status = VL53L0X_apply_offset_adjustment( Dev );
johnAlexander 0:c523920bcc09 1565 #endif
johnAlexander 0:c523920bcc09 1566
johnAlexander 15:932d8b4e52c9 1567 /* Default value is 1000 for Linearity Corrective Gain */
JerrySzczurak 20:cf211a3b3d9e 1568 PALDevDataSet( dev, LinearityCorrectiveGain, 1000 );
johnAlexander 15:932d8b4e52c9 1569
johnAlexander 15:932d8b4e52c9 1570 /* Dmax default Parameter */
JerrySzczurak 20:cf211a3b3d9e 1571 PALDevDataSet( dev, DmaxCalRangeMilliMeter, 400 );
JerrySzczurak 20:cf211a3b3d9e 1572 PALDevDataSet( dev, DmaxCalSignalRateRtnMegaCps,
JerrySzczurak 20:cf211a3b3d9e 1573 ( FixPoint1616_t )( ( 0x00016B85 ) ) ); /* 1.42 No Cover Glass*/
johnAlexander 15:932d8b4e52c9 1574
johnAlexander 15:932d8b4e52c9 1575 /* Set Default static parameters
johnAlexander 15:932d8b4e52c9 1576 *set first temporary values 9.44MHz * 65536 = 618660 */
JerrySzczurak 20:cf211a3b3d9e 1577 VL53L0X_SETDEVICESPECIFICPARAMETER( dev, OscFrequencyMHz, 618660 );
johnAlexander 15:932d8b4e52c9 1578
johnAlexander 15:932d8b4e52c9 1579 /* Set Default XTalkCompensationRateMegaCps to 0 */
JerrySzczurak 20:cf211a3b3d9e 1580 VL53L0X_SETPARAMETERFIELD( dev, XTalkCompensationRateMegaCps, 0 );
johnAlexander 15:932d8b4e52c9 1581
johnAlexander 15:932d8b4e52c9 1582 /* Get default parameters */
JerrySzczurak 20:cf211a3b3d9e 1583 status = vl53l0x_get_device_parameters( dev, &CurrentParameters );
JerrySzczurak 20:cf211a3b3d9e 1584 if ( status == VL53L0X_ERROR_NONE ) {
johnAlexander 15:932d8b4e52c9 1585 /* initialize PAL values */
johnAlexander 15:932d8b4e52c9 1586 CurrentParameters.DeviceMode = VL53L0X_DEVICEMODE_SINGLE_RANGING;
johnAlexander 15:932d8b4e52c9 1587 CurrentParameters.HistogramMode = VL53L0X_HISTOGRAMMODE_DISABLED;
JerrySzczurak 20:cf211a3b3d9e 1588 PALDevDataSet( dev, CurrentParameters, CurrentParameters );
johnAlexander 15:932d8b4e52c9 1589 }
johnAlexander 15:932d8b4e52c9 1590
johnAlexander 15:932d8b4e52c9 1591 /* Sigma estimator variable */
JerrySzczurak 20:cf211a3b3d9e 1592 PALDevDataSet( dev, SigmaEstRefArray, 100 );
JerrySzczurak 20:cf211a3b3d9e 1593 PALDevDataSet( dev, SigmaEstEffPulseWidth, 900 );
JerrySzczurak 20:cf211a3b3d9e 1594 PALDevDataSet( dev, SigmaEstEffAmbWidth, 500 );
JerrySzczurak 20:cf211a3b3d9e 1595 PALDevDataSet( dev, targetRefRate, 0x0A00 ); /* 20 MCPS in 9:7 format */
johnAlexander 15:932d8b4e52c9 1596
johnAlexander 15:932d8b4e52c9 1597 /* Use internal default settings */
JerrySzczurak 20:cf211a3b3d9e 1598 PALDevDataSet( dev, UseInternalTuningSettings, 1 );
JerrySzczurak 20:cf211a3b3d9e 1599
JerrySzczurak 20:cf211a3b3d9e 1600 status |= vl53l0x_write_byte( dev, 0x80, 0x01 );
JerrySzczurak 20:cf211a3b3d9e 1601 status |= vl53l0x_write_byte( dev, 0xFF, 0x01 );
JerrySzczurak 20:cf211a3b3d9e 1602 status |= vl53l0x_write_byte( dev, 0x00, 0x00 );
JerrySzczurak 20:cf211a3b3d9e 1603 status |= vl53l0x_read_byte( dev, 0x91, &StopVariable );
JerrySzczurak 20:cf211a3b3d9e 1604 PALDevDataSet( dev, StopVariable, StopVariable );
JerrySzczurak 20:cf211a3b3d9e 1605 status |= vl53l0x_write_byte( dev, 0x00, 0x01 );
JerrySzczurak 20:cf211a3b3d9e 1606 status |= vl53l0x_write_byte( dev, 0xFF, 0x00 );
JerrySzczurak 20:cf211a3b3d9e 1607 status |= vl53l0x_write_byte( dev, 0x80, 0x00 );
johnAlexander 15:932d8b4e52c9 1608
johnAlexander 15:932d8b4e52c9 1609 /* Enable all check */
JerrySzczurak 20:cf211a3b3d9e 1610 for ( i = 0; i < VL53L0X_CHECKENABLE_NUMBER_OF_CHECKS; i++ ) {
JerrySzczurak 20:cf211a3b3d9e 1611 if ( status == VL53L0X_ERROR_NONE )
JerrySzczurak 20:cf211a3b3d9e 1612 status |= vl53l0x_set_limit_check_enable( dev, i, 1 );
johnAlexander 15:932d8b4e52c9 1613 else
johnAlexander 15:932d8b4e52c9 1614 break;
johnAlexander 15:932d8b4e52c9 1615
johnAlexander 15:932d8b4e52c9 1616 }
johnAlexander 15:932d8b4e52c9 1617
johnAlexander 15:932d8b4e52c9 1618 /* Disable the following checks */
JerrySzczurak 20:cf211a3b3d9e 1619 if ( status == VL53L0X_ERROR_NONE )
JerrySzczurak 20:cf211a3b3d9e 1620 status = vl53l0x_set_limit_check_enable( dev,
JerrySzczurak 20:cf211a3b3d9e 1621 VL53L0X_CHECKENABLE_SIGNAL_REF_CLIP, 0 );
JerrySzczurak 20:cf211a3b3d9e 1622
JerrySzczurak 20:cf211a3b3d9e 1623 if ( status == VL53L0X_ERROR_NONE )
JerrySzczurak 20:cf211a3b3d9e 1624 status = vl53l0x_set_limit_check_enable( dev,
JerrySzczurak 20:cf211a3b3d9e 1625 VL53L0X_CHECKENABLE_RANGE_IGNORE_THRESHOLD, 0 );
JerrySzczurak 20:cf211a3b3d9e 1626
JerrySzczurak 20:cf211a3b3d9e 1627 if ( status == VL53L0X_ERROR_NONE )
JerrySzczurak 20:cf211a3b3d9e 1628 status = vl53l0x_set_limit_check_enable( dev,
JerrySzczurak 20:cf211a3b3d9e 1629 VL53L0X_CHECKENABLE_SIGNAL_RATE_MSRC, 0 );
JerrySzczurak 20:cf211a3b3d9e 1630
JerrySzczurak 20:cf211a3b3d9e 1631 if ( status == VL53L0X_ERROR_NONE )
JerrySzczurak 20:cf211a3b3d9e 1632 status = vl53l0x_set_limit_check_enable( dev,
JerrySzczurak 20:cf211a3b3d9e 1633 VL53L0X_CHECKENABLE_SIGNAL_RATE_PRE_RANGE, 0 );
johnAlexander 15:932d8b4e52c9 1634
johnAlexander 15:932d8b4e52c9 1635 /* Limit default values */
JerrySzczurak 20:cf211a3b3d9e 1636 if ( status == VL53L0X_ERROR_NONE ) {
JerrySzczurak 20:cf211a3b3d9e 1637 status = vl53l0x_set_limit_check_value( dev,
JerrySzczurak 20:cf211a3b3d9e 1638 VL53L0X_CHECKENABLE_SIGMA_FINAL_RANGE,
JerrySzczurak 20:cf211a3b3d9e 1639 ( FixPoint1616_t )( 18 * 65536 ) );
JerrySzczurak 20:cf211a3b3d9e 1640 }
JerrySzczurak 20:cf211a3b3d9e 1641 if ( status == VL53L0X_ERROR_NONE ) {
JerrySzczurak 20:cf211a3b3d9e 1642 status = vl53l0x_set_limit_check_value( dev,
JerrySzczurak 20:cf211a3b3d9e 1643 VL53L0X_CHECKENABLE_SIGNAL_RATE_FINAL_RANGE,
JerrySzczurak 20:cf211a3b3d9e 1644 ( FixPoint1616_t )( 25 * 65536 / 100 ) );
johnAlexander 15:932d8b4e52c9 1645 /* 0.25 * 65536 */
johnAlexander 15:932d8b4e52c9 1646 }
johnAlexander 15:932d8b4e52c9 1647
JerrySzczurak 20:cf211a3b3d9e 1648 if ( status == VL53L0X_ERROR_NONE ) {
JerrySzczurak 20:cf211a3b3d9e 1649 status = vl53l0x_set_limit_check_value( dev,
JerrySzczurak 20:cf211a3b3d9e 1650 VL53L0X_CHECKENABLE_SIGNAL_REF_CLIP,
JerrySzczurak 20:cf211a3b3d9e 1651 ( FixPoint1616_t )( 35 * 65536 ) );
JerrySzczurak 20:cf211a3b3d9e 1652 }
JerrySzczurak 20:cf211a3b3d9e 1653
JerrySzczurak 20:cf211a3b3d9e 1654 if ( status == VL53L0X_ERROR_NONE ) {
JerrySzczurak 20:cf211a3b3d9e 1655 status = vl53l0x_set_limit_check_value( dev,
JerrySzczurak 20:cf211a3b3d9e 1656 VL53L0X_CHECKENABLE_RANGE_IGNORE_THRESHOLD,
JerrySzczurak 20:cf211a3b3d9e 1657 ( FixPoint1616_t )( 0 * 65536 ) );
JerrySzczurak 20:cf211a3b3d9e 1658 }
JerrySzczurak 20:cf211a3b3d9e 1659
JerrySzczurak 20:cf211a3b3d9e 1660 if ( status == VL53L0X_ERROR_NONE ) {
JerrySzczurak 20:cf211a3b3d9e 1661
JerrySzczurak 20:cf211a3b3d9e 1662 PALDevDataSet( dev, SequenceConfig, 0xFF );
JerrySzczurak 20:cf211a3b3d9e 1663 status = vl53l0x_write_byte( dev, VL53L0X_REG_SYSTEM_SEQUENCE_CONFIG,
JerrySzczurak 20:cf211a3b3d9e 1664 0xFF );
johnAlexander 15:932d8b4e52c9 1665
johnAlexander 15:932d8b4e52c9 1666 /* Set PAL state to tell that we are waiting for call to
johnAlexander 15:932d8b4e52c9 1667 * VL53L0X_StaticInit */
JerrySzczurak 20:cf211a3b3d9e 1668 PALDevDataSet( dev, PalState, VL53L0X_STATE_WAIT_STATICINIT );
JerrySzczurak 20:cf211a3b3d9e 1669 }
JerrySzczurak 20:cf211a3b3d9e 1670
JerrySzczurak 20:cf211a3b3d9e 1671 if ( status == VL53L0X_ERROR_NONE )
JerrySzczurak 20:cf211a3b3d9e 1672 VL53L0X_SETDEVICESPECIFICPARAMETER( dev, RefSpadsInitialised, 0 );
JerrySzczurak 20:cf211a3b3d9e 1673
JerrySzczurak 20:cf211a3b3d9e 1674
JerrySzczurak 20:cf211a3b3d9e 1675 LOG_FUNCTION_END( status );
JerrySzczurak 20:cf211a3b3d9e 1676 return status;
JerrySzczurak 20:cf211a3b3d9e 1677 }
JerrySzczurak 20:cf211a3b3d9e 1678
JerrySzczurak 20:cf211a3b3d9e 1679 VL53L0X_Error VL53L0X::vl53l0x_check_part_used( VL53L0X_DEV dev,
johnAlexander 16:98ce55ddbb1a 1680 uint8_t *revision,
JerrySzczurak 20:cf211a3b3d9e 1681 VL53L0X_DeviceInfo_t *p_vl53l0x_device_info )
johnAlexander 0:c523920bcc09 1682 {
johnAlexander 16:98ce55ddbb1a 1683 VL53L0X_Error status = VL53L0X_ERROR_NONE;
johnAlexander 16:98ce55ddbb1a 1684 uint8_t module_id_int;
johnAlexander 16:98ce55ddbb1a 1685 char *product_id_tmp;
johnAlexander 15:932d8b4e52c9 1686
JerrySzczurak 20:cf211a3b3d9e 1687 LOG_FUNCTION_START( "" );
JerrySzczurak 20:cf211a3b3d9e 1688
JerrySzczurak 20:cf211a3b3d9e 1689 status = vl53l0x_get_info_from_device( dev, 2 );
JerrySzczurak 20:cf211a3b3d9e 1690
JerrySzczurak 20:cf211a3b3d9e 1691 if ( status == VL53L0X_ERROR_NONE ) {
JerrySzczurak 20:cf211a3b3d9e 1692 module_id_int = VL53L0X_GETDEVICESPECIFICPARAMETER( dev, ModuleId );
JerrySzczurak 20:cf211a3b3d9e 1693
JerrySzczurak 20:cf211a3b3d9e 1694 if ( module_id_int == 0 ) {
johnAlexander 16:98ce55ddbb1a 1695 *revision = 0;
JerrySzczurak 20:cf211a3b3d9e 1696 VL53L0X_COPYSTRING( p_vl53l0x_device_info->ProductId, "" );
johnAlexander 15:932d8b4e52c9 1697 } else {
JerrySzczurak 20:cf211a3b3d9e 1698 *revision = VL53L0X_GETDEVICESPECIFICPARAMETER( dev, Revision );
JerrySzczurak 20:cf211a3b3d9e 1699 product_id_tmp = VL53L0X_GETDEVICESPECIFICPARAMETER( dev,
JerrySzczurak 20:cf211a3b3d9e 1700 ProductId );
JerrySzczurak 20:cf211a3b3d9e 1701 VL53L0X_COPYSTRING( p_vl53l0x_device_info->ProductId, product_id_tmp );
johnAlexander 15:932d8b4e52c9 1702 }
johnAlexander 15:932d8b4e52c9 1703 }
johnAlexander 15:932d8b4e52c9 1704
JerrySzczurak 20:cf211a3b3d9e 1705 LOG_FUNCTION_END( status );
JerrySzczurak 20:cf211a3b3d9e 1706 return status;
JerrySzczurak 20:cf211a3b3d9e 1707 }
JerrySzczurak 20:cf211a3b3d9e 1708
JerrySzczurak 20:cf211a3b3d9e 1709 VL53L0X_Error VL53L0X::wrapped_vl53l0x_get_device_info( VL53L0X_DEV dev,
JerrySzczurak 20:cf211a3b3d9e 1710 VL53L0X_DeviceInfo_t *p_vl53l0x_device_info )
johnAlexander 0:c523920bcc09 1711 {
johnAlexander 16:98ce55ddbb1a 1712 VL53L0X_Error status = VL53L0X_ERROR_NONE;
johnAlexander 15:932d8b4e52c9 1713 uint8_t revision_id;
johnAlexander 16:98ce55ddbb1a 1714 uint8_t revision;
johnAlexander 16:98ce55ddbb1a 1715
JerrySzczurak 20:cf211a3b3d9e 1716 status = vl53l0x_check_part_used( dev, &revision, p_vl53l0x_device_info );
JerrySzczurak 20:cf211a3b3d9e 1717
JerrySzczurak 20:cf211a3b3d9e 1718 if ( status == VL53L0X_ERROR_NONE ) {
JerrySzczurak 20:cf211a3b3d9e 1719 if ( revision == 0 ) {
JerrySzczurak 20:cf211a3b3d9e 1720 VL53L0X_COPYSTRING( p_vl53l0x_device_info->Name,
JerrySzczurak 20:cf211a3b3d9e 1721 VL53L0X_STRING_DEVICE_INFO_NAME_TS0 );
JerrySzczurak 20:cf211a3b3d9e 1722 } else if ( ( revision <= 34 ) && ( revision != 32 ) ) {
JerrySzczurak 20:cf211a3b3d9e 1723 VL53L0X_COPYSTRING( p_vl53l0x_device_info->Name,
JerrySzczurak 20:cf211a3b3d9e 1724 VL53L0X_STRING_DEVICE_INFO_NAME_TS1 );
JerrySzczurak 20:cf211a3b3d9e 1725 } else if ( revision < 39 ) {
JerrySzczurak 20:cf211a3b3d9e 1726 VL53L0X_COPYSTRING( p_vl53l0x_device_info->Name,
JerrySzczurak 20:cf211a3b3d9e 1727 VL53L0X_STRING_DEVICE_INFO_NAME_TS2 );
johnAlexander 15:932d8b4e52c9 1728 } else {
JerrySzczurak 20:cf211a3b3d9e 1729 VL53L0X_COPYSTRING( p_vl53l0x_device_info->Name,
JerrySzczurak 20:cf211a3b3d9e 1730 VL53L0X_STRING_DEVICE_INFO_NAME_ES1 );
johnAlexander 15:932d8b4e52c9 1731 }
johnAlexander 15:932d8b4e52c9 1732
JerrySzczurak 20:cf211a3b3d9e 1733 VL53L0X_COPYSTRING( p_vl53l0x_device_info->Type,
JerrySzczurak 20:cf211a3b3d9e 1734 VL53L0X_STRING_DEVICE_INFO_TYPE );
JerrySzczurak 20:cf211a3b3d9e 1735
JerrySzczurak 20:cf211a3b3d9e 1736 }
JerrySzczurak 20:cf211a3b3d9e 1737
JerrySzczurak 20:cf211a3b3d9e 1738 if ( status == VL53L0X_ERROR_NONE ) {
JerrySzczurak 20:cf211a3b3d9e 1739 status = vl53l0x_read_byte( dev, VL53L0X_REG_IDENTIFICATION_MODEL_ID,
JerrySzczurak 20:cf211a3b3d9e 1740 &p_vl53l0x_device_info->ProductType );
JerrySzczurak 20:cf211a3b3d9e 1741 }
JerrySzczurak 20:cf211a3b3d9e 1742
JerrySzczurak 20:cf211a3b3d9e 1743 if ( status == VL53L0X_ERROR_NONE ) {
JerrySzczurak 20:cf211a3b3d9e 1744 status = vl53l0x_read_byte( dev,
JerrySzczurak 20:cf211a3b3d9e 1745 VL53L0X_REG_IDENTIFICATION_REVISION_ID,
JerrySzczurak 20:cf211a3b3d9e 1746 &revision_id );
johnAlexander 16:98ce55ddbb1a 1747 p_vl53l0x_device_info->ProductRevisionMajor = 1;
johnAlexander 16:98ce55ddbb1a 1748 p_vl53l0x_device_info->ProductRevisionMinor =
JerrySzczurak 20:cf211a3b3d9e 1749 ( revision_id & 0xF0 ) >> 4;
JerrySzczurak 20:cf211a3b3d9e 1750 }
JerrySzczurak 20:cf211a3b3d9e 1751
JerrySzczurak 20:cf211a3b3d9e 1752 return status;
JerrySzczurak 20:cf211a3b3d9e 1753 }
JerrySzczurak 20:cf211a3b3d9e 1754
JerrySzczurak 20:cf211a3b3d9e 1755 VL53L0X_Error VL53L0X::vl53l0x_get_device_info( VL53L0X_DEV dev,
JerrySzczurak 20:cf211a3b3d9e 1756 VL53L0X_DeviceInfo_t *p_vl53l0x_device_info )
johnAlexander 0:c523920bcc09 1757 {
johnAlexander 16:98ce55ddbb1a 1758 VL53L0X_Error status = VL53L0X_ERROR_NONE;
JerrySzczurak 20:cf211a3b3d9e 1759 LOG_FUNCTION_START( "" );
JerrySzczurak 20:cf211a3b3d9e 1760
JerrySzczurak 20:cf211a3b3d9e 1761 status = wrapped_vl53l0x_get_device_info( dev, p_vl53l0x_device_info );
JerrySzczurak 20:cf211a3b3d9e 1762
JerrySzczurak 20:cf211a3b3d9e 1763 LOG_FUNCTION_END( status );
JerrySzczurak 20:cf211a3b3d9e 1764 return status;
JerrySzczurak 20:cf211a3b3d9e 1765 }
JerrySzczurak 20:cf211a3b3d9e 1766
JerrySzczurak 20:cf211a3b3d9e 1767 VL53L0X_Error VL53L0X::vl53l0x_get_interrupt_mask_status( VL53L0X_DEV dev,
JerrySzczurak 20:cf211a3b3d9e 1768 uint32_t *p_interrupt_mask_status )
johnAlexander 0:c523920bcc09 1769 {
johnAlexander 16:98ce55ddbb1a 1770 VL53L0X_Error status = VL53L0X_ERROR_NONE;
johnAlexander 16:98ce55ddbb1a 1771 uint8_t byte;
JerrySzczurak 20:cf211a3b3d9e 1772 LOG_FUNCTION_START( "" );
JerrySzczurak 20:cf211a3b3d9e 1773
JerrySzczurak 20:cf211a3b3d9e 1774 status = vl53l0x_read_byte( dev, VL53L0X_REG_RESULT_INTERRUPT_STATUS, &byte );
johnAlexander 17:1b842521063a 1775 *p_interrupt_mask_status = byte & 0x07;
johnAlexander 16:98ce55ddbb1a 1776
JerrySzczurak 20:cf211a3b3d9e 1777 if ( byte & 0x18 )
johnAlexander 16:98ce55ddbb1a 1778 status = VL53L0X_ERROR_RANGE_ERROR;
johnAlexander 16:98ce55ddbb1a 1779
JerrySzczurak 20:cf211a3b3d9e 1780 LOG_FUNCTION_END( status );
JerrySzczurak 20:cf211a3b3d9e 1781 return status;
JerrySzczurak 20:cf211a3b3d9e 1782 }
JerrySzczurak 20:cf211a3b3d9e 1783
JerrySzczurak 20:cf211a3b3d9e 1784 VL53L0X_Error VL53L0X::vl53l0x_get_measurement_data_ready( VL53L0X_DEV dev,
JerrySzczurak 20:cf211a3b3d9e 1785 uint8_t *p_measurement_data_ready )
johnAlexander 0:c523920bcc09 1786 {
johnAlexander 16:98ce55ddbb1a 1787 VL53L0X_Error status = VL53L0X_ERROR_NONE;
johnAlexander 16:98ce55ddbb1a 1788 uint8_t sys_range_status_register;
johnAlexander 16:98ce55ddbb1a 1789 uint8_t interrupt_config;
johnAlexander 16:98ce55ddbb1a 1790 uint32_t interrupt_mask;
JerrySzczurak 20:cf211a3b3d9e 1791 LOG_FUNCTION_START( "" );
JerrySzczurak 20:cf211a3b3d9e 1792
JerrySzczurak 20:cf211a3b3d9e 1793 interrupt_config = VL53L0X_GETDEVICESPECIFICPARAMETER( dev,
JerrySzczurak 20:cf211a3b3d9e 1794 Pin0GpioFunctionality );
JerrySzczurak 20:cf211a3b3d9e 1795
JerrySzczurak 20:cf211a3b3d9e 1796 if ( interrupt_config ==
JerrySzczurak 20:cf211a3b3d9e 1797 VL53L0X_REG_SYSTEM_INTERRUPT_GPIO_NEW_SAMPLE_READY ) {
JerrySzczurak 20:cf211a3b3d9e 1798 status = vl53l0x_get_interrupt_mask_status( dev, &interrupt_mask );
JerrySzczurak 20:cf211a3b3d9e 1799 if ( interrupt_mask ==
JerrySzczurak 20:cf211a3b3d9e 1800 VL53L0X_REG_SYSTEM_INTERRUPT_GPIO_NEW_SAMPLE_READY )
johnAlexander 17:1b842521063a 1801 *p_measurement_data_ready = 1;
johnAlexander 15:932d8b4e52c9 1802 else
johnAlexander 17:1b842521063a 1803 *p_measurement_data_ready = 0;
johnAlexander 15:932d8b4e52c9 1804 } else {
JerrySzczurak 20:cf211a3b3d9e 1805 status = vl53l0x_read_byte( dev, VL53L0X_REG_RESULT_RANGE_STATUS,
JerrySzczurak 20:cf211a3b3d9e 1806 &sys_range_status_register );
JerrySzczurak 20:cf211a3b3d9e 1807 if ( status == VL53L0X_ERROR_NONE ) {
JerrySzczurak 20:cf211a3b3d9e 1808 if ( sys_range_status_register & 0x01 )
johnAlexander 17:1b842521063a 1809 *p_measurement_data_ready = 1;
johnAlexander 15:932d8b4e52c9 1810 else
johnAlexander 17:1b842521063a 1811 *p_measurement_data_ready = 0;
johnAlexander 15:932d8b4e52c9 1812 }
johnAlexander 15:932d8b4e52c9 1813 }
johnAlexander 15:932d8b4e52c9 1814
JerrySzczurak 20:cf211a3b3d9e 1815 LOG_FUNCTION_END( status );
JerrySzczurak 20:cf211a3b3d9e 1816 return status;
JerrySzczurak 20:cf211a3b3d9e 1817 }
JerrySzczurak 20:cf211a3b3d9e 1818
JerrySzczurak 20:cf211a3b3d9e 1819 VL53L0X_Error VL53L0X::vl53l0x_polling_delay( VL53L0X_DEV dev )
johnAlexander 15:932d8b4e52c9 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
JerrySzczurak 20:cf211a3b3d9e 1828 VL53L0X_Error VL53L0X::vl53l0x_measurement_poll_for_completion( VL53L0X_DEV dev )
johnAlexander 0:c523920bcc09 1829 {
johnAlexander 16:98ce55ddbb1a 1830 VL53L0X_Error status = VL53L0X_ERROR_NONE;
johnAlexander 16:98ce55ddbb1a 1831 uint8_t new_data_ready = 0;
johnAlexander 16:98ce55ddbb1a 1832 uint32_t loop_nb;
johnAlexander 15:932d8b4e52c9 1833
JerrySzczurak 20:cf211a3b3d9e 1834 LOG_FUNCTION_START( "" );
johnAlexander 15:932d8b4e52c9 1835
johnAlexander 16:98ce55ddbb1a 1836 loop_nb = 0;
johnAlexander 15:932d8b4e52c9 1837
johnAlexander 15:932d8b4e52c9 1838 do {
JerrySzczurak 20:cf211a3b3d9e 1839 status = vl53l0x_get_measurement_data_ready( dev, &new_data_ready );
JerrySzczurak 20:cf211a3b3d9e 1840 if ( status != 0 )
johnAlexander 15:932d8b4e52c9 1841 break; /* the error is set */
johnAlexander 15:932d8b4e52c9 1842
JerrySzczurak 20:cf211a3b3d9e 1843 if ( new_data_ready == 1 )
johnAlexander 15:932d8b4e52c9 1844 break; /* done note that status == 0 */
johnAlexander 15:932d8b4e52c9 1845
johnAlexander 16:98ce55ddbb1a 1846 loop_nb++;
JerrySzczurak 20:cf211a3b3d9e 1847 if ( loop_nb >= VL53L0X_DEFAULT_MAX_LOOP ) {
johnAlexander 16:98ce55ddbb1a 1848 status = VL53L0X_ERROR_TIME_OUT;
johnAlexander 15:932d8b4e52c9 1849 break;
johnAlexander 15:932d8b4e52c9 1850 }
johnAlexander 15:932d8b4e52c9 1851
JerrySzczurak 20:cf211a3b3d9e 1852 vl53l0x_polling_delay( dev );
JerrySzczurak 20:cf211a3b3d9e 1853 } while ( 1 );
JerrySzczurak 20:cf211a3b3d9e 1854
JerrySzczurak 20:cf211a3b3d9e 1855 LOG_FUNCTION_END( status );
johnAlexander 16:98ce55ddbb1a 1856
johnAlexander 16:98ce55ddbb1a 1857 return status;
johnAlexander 0:c523920bcc09 1858 }
johnAlexander 0:c523920bcc09 1859
johnAlexander 0:c523920bcc09 1860 /* Group PAL Interrupt Functions */
JerrySzczurak 20:cf211a3b3d9e 1861 VL53L0X_Error VL53L0X::vl53l0x_clear_interrupt_mask( VL53L0X_DEV dev, uint32_t interrupt_mask )
johnAlexander 0:c523920bcc09 1862 {
johnAlexander 16:98ce55ddbb1a 1863 VL53L0X_Error status = VL53L0X_ERROR_NONE;
johnAlexander 16:98ce55ddbb1a 1864 uint8_t loop_count;
johnAlexander 17:1b842521063a 1865 uint8_t byte;
JerrySzczurak 20:cf211a3b3d9e 1866 LOG_FUNCTION_START( "" );
johnAlexander 15:932d8b4e52c9 1867
johnAlexander 15:932d8b4e52c9 1868 /* clear bit 0 range interrupt, bit 1 error interrupt */
johnAlexander 16:98ce55ddbb1a 1869 loop_count = 0;
johnAlexander 15:932d8b4e52c9 1870 do {
JerrySzczurak 20:cf211a3b3d9e 1871 status = vl53l0x_write_byte( dev,
JerrySzczurak 20:cf211a3b3d9e 1872 VL53L0X_REG_SYSTEM_INTERRUPT_CLEAR, 0x01 );
JerrySzczurak 20:cf211a3b3d9e 1873 status |= vl53l0x_write_byte( dev,
JerrySzczurak 20:cf211a3b3d9e 1874 VL53L0X_REG_SYSTEM_INTERRUPT_CLEAR, 0x00 );
JerrySzczurak 20:cf211a3b3d9e 1875 status |= vl53l0x_read_byte( dev,
JerrySzczurak 20:cf211a3b3d9e 1876 VL53L0X_REG_RESULT_INTERRUPT_STATUS, &byte );
johnAlexander 16:98ce55ddbb1a 1877 loop_count++;
JerrySzczurak 20:cf211a3b3d9e 1878 } while ( ( ( byte & 0x07 ) != 0x00 )
JerrySzczurak 20:cf211a3b3d9e 1879 && ( loop_count < 3 )
JerrySzczurak 20:cf211a3b3d9e 1880 && ( status == VL53L0X_ERROR_NONE ) );
JerrySzczurak 20:cf211a3b3d9e 1881
JerrySzczurak 20:cf211a3b3d9e 1882
JerrySzczurak 20:cf211a3b3d9e 1883 if ( loop_count >= 3 )
johnAlexander 16:98ce55ddbb1a 1884 status = VL53L0X_ERROR_INTERRUPT_NOT_CLEARED;
johnAlexander 16:98ce55ddbb1a 1885
JerrySzczurak 20:cf211a3b3d9e 1886 LOG_FUNCTION_END( status );
JerrySzczurak 20:cf211a3b3d9e 1887 return status;
JerrySzczurak 20:cf211a3b3d9e 1888 }
JerrySzczurak 20:cf211a3b3d9e 1889
JerrySzczurak 20:cf211a3b3d9e 1890 VL53L0X_Error VL53L0X::vl53l0x_perform_single_ref_calibration( VL53L0X_DEV dev,
JerrySzczurak 20:cf211a3b3d9e 1891 uint8_t vhv_init_byte )
johnAlexander 0:c523920bcc09 1892 {
johnAlexander 16:98ce55ddbb1a 1893 VL53L0X_Error status = VL53L0X_ERROR_NONE;
johnAlexander 16:98ce55ddbb1a 1894
JerrySzczurak 20:cf211a3b3d9e 1895 if ( status == VL53L0X_ERROR_NONE )
JerrySzczurak 20:cf211a3b3d9e 1896 status = vl53l0x_write_byte( dev, VL53L0X_REG_SYSRANGE_START,
JerrySzczurak 20:cf211a3b3d9e 1897 VL53L0X_REG_SYSRANGE_MODE_START_STOP |
JerrySzczurak 20:cf211a3b3d9e 1898 vhv_init_byte );
JerrySzczurak 20:cf211a3b3d9e 1899
JerrySzczurak 20:cf211a3b3d9e 1900 if ( status == VL53L0X_ERROR_NONE )
JerrySzczurak 20:cf211a3b3d9e 1901 status = vl53l0x_measurement_poll_for_completion( dev );
JerrySzczurak 20:cf211a3b3d9e 1902
JerrySzczurak 20:cf211a3b3d9e 1903 if ( status == VL53L0X_ERROR_NONE )
JerrySzczurak 20:cf211a3b3d9e 1904 status = vl53l0x_clear_interrupt_mask( dev, 0 );
JerrySzczurak 20:cf211a3b3d9e 1905
JerrySzczurak 20:cf211a3b3d9e 1906 if ( status == VL53L0X_ERROR_NONE )
JerrySzczurak 20:cf211a3b3d9e 1907 status = vl53l0x_write_byte( dev, VL53L0X_REG_SYSRANGE_START, 0x00 );
JerrySzczurak 20:cf211a3b3d9e 1908
JerrySzczurak 20:cf211a3b3d9e 1909 return status;
JerrySzczurak 20:cf211a3b3d9e 1910 }
JerrySzczurak 20:cf211a3b3d9e 1911
JerrySzczurak 20:cf211a3b3d9e 1912 VL53L0X_Error VL53L0X::vl53l0x_ref_calibration_io( VL53L0X_DEV dev, uint8_t read_not_write,
johnAlexander 16:98ce55ddbb1a 1913 uint8_t vhv_settings, uint8_t phase_cal,
johnAlexander 16:98ce55ddbb1a 1914 uint8_t *p_vhv_settings, uint8_t *p_phase_cal,
JerrySzczurak 20:cf211a3b3d9e 1915 const uint8_t vhv_enable, const uint8_t phase_enable )
johnAlexander 0:c523920bcc09 1916 {
johnAlexander 16:98ce55ddbb1a 1917 VL53L0X_Error status = VL53L0X_ERROR_NONE;
johnAlexander 16:98ce55ddbb1a 1918 uint8_t phase_calint = 0;
johnAlexander 15:932d8b4e52c9 1919
johnAlexander 15:932d8b4e52c9 1920 /* Read VHV from device */
JerrySzczurak 20:cf211a3b3d9e 1921 status |= vl53l0x_write_byte( dev, 0xFF, 0x01 );
JerrySzczurak 20:cf211a3b3d9e 1922 status |= vl53l0x_write_byte( dev, 0x00, 0x00 );
JerrySzczurak 20:cf211a3b3d9e 1923 status |= vl53l0x_write_byte( dev, 0xFF, 0x00 );
JerrySzczurak 20:cf211a3b3d9e 1924
JerrySzczurak 20:cf211a3b3d9e 1925 if ( read_not_write ) {
JerrySzczurak 20:cf211a3b3d9e 1926 if ( vhv_enable )
JerrySzczurak 20:cf211a3b3d9e 1927 status |= vl53l0x_read_byte( dev, 0xCB, p_vhv_settings );
JerrySzczurak 20:cf211a3b3d9e 1928 if ( phase_enable )
JerrySzczurak 20:cf211a3b3d9e 1929 status |= vl53l0x_read_byte( dev, 0xEE, &phase_calint );
johnAlexander 15:932d8b4e52c9 1930 } else {
JerrySzczurak 20:cf211a3b3d9e 1931 if ( vhv_enable )
JerrySzczurak 20:cf211a3b3d9e 1932 status |= vl53l0x_write_byte( dev, 0xCB, vhv_settings );
JerrySzczurak 20:cf211a3b3d9e 1933 if ( phase_enable )
JerrySzczurak 20:cf211a3b3d9e 1934 status |= vl53l0x_update_byte( dev, 0xEE, 0x80, phase_cal );
JerrySzczurak 20:cf211a3b3d9e 1935 }
JerrySzczurak 20:cf211a3b3d9e 1936
JerrySzczurak 20:cf211a3b3d9e 1937 status |= vl53l0x_write_byte( dev, 0xFF, 0x01 );
JerrySzczurak 20:cf211a3b3d9e 1938 status |= vl53l0x_write_byte( dev, 0x00, 0x01 );
JerrySzczurak 20:cf211a3b3d9e 1939 status |= vl53l0x_write_byte( dev, 0xFF, 0x00 );
JerrySzczurak 20:cf211a3b3d9e 1940
JerrySzczurak 20:cf211a3b3d9e 1941 *p_phase_cal = ( uint8_t )( phase_calint & 0xEF );
JerrySzczurak 20:cf211a3b3d9e 1942
JerrySzczurak 20:cf211a3b3d9e 1943 return status;
JerrySzczurak 20:cf211a3b3d9e 1944 }
JerrySzczurak 20:cf211a3b3d9e 1945
JerrySzczurak 20:cf211a3b3d9e 1946 VL53L0X_Error VL53L0X::vl53l0x_perform_vhv_calibration( VL53L0X_DEV dev,
johnAlexander 16:98ce55ddbb1a 1947 uint8_t *p_vhv_settings, const uint8_t get_data_enable,
JerrySzczurak 20:cf211a3b3d9e 1948 const uint8_t restore_config )
johnAlexander 0:c523920bcc09 1949 {
johnAlexander 16:98ce55ddbb1a 1950 VL53L0X_Error status = VL53L0X_ERROR_NONE;
johnAlexander 16:98ce55ddbb1a 1951 uint8_t sequence_config = 0;
johnAlexander 16:98ce55ddbb1a 1952 uint8_t vhv_settings = 0;
johnAlexander 16:98ce55ddbb1a 1953 uint8_t phase_cal = 0;
johnAlexander 16:98ce55ddbb1a 1954 uint8_t phase_cal_int = 0;
johnAlexander 15:932d8b4e52c9 1955
johnAlexander 15:932d8b4e52c9 1956 /* store the value of the sequence config,
johnAlexander 15:932d8b4e52c9 1957 * this will be reset before the end of the function
johnAlexander 15:932d8b4e52c9 1958 */
johnAlexander 15:932d8b4e52c9 1959
JerrySzczurak 20:cf211a3b3d9e 1960 if ( restore_config )
JerrySzczurak 20:cf211a3b3d9e 1961 sequence_config = PALDevDataGet( dev, SequenceConfig );
johnAlexander 15:932d8b4e52c9 1962
johnAlexander 15:932d8b4e52c9 1963 /* Run VHV */
JerrySzczurak 20:cf211a3b3d9e 1964 status = vl53l0x_write_byte( dev, VL53L0X_REG_SYSTEM_SEQUENCE_CONFIG, 0x01 );
JerrySzczurak 20:cf211a3b3d9e 1965
JerrySzczurak 20:cf211a3b3d9e 1966 if ( status == VL53L0X_ERROR_NONE )
JerrySzczurak 20:cf211a3b3d9e 1967 status = vl53l0x_perform_single_ref_calibration( dev, 0x40 );
johnAlexander 15:932d8b4e52c9 1968
johnAlexander 15:932d8b4e52c9 1969 /* Read VHV from device */
JerrySzczurak 20:cf211a3b3d9e 1970 if ( ( status == VL53L0X_ERROR_NONE ) && ( get_data_enable == 1 ) ) {
JerrySzczurak 20:cf211a3b3d9e 1971 status = vl53l0x_ref_calibration_io( dev, 1,
JerrySzczurak 20:cf211a3b3d9e 1972 vhv_settings, phase_cal, /* Not used here */
JerrySzczurak 20:cf211a3b3d9e 1973 p_vhv_settings, &phase_cal_int,
JerrySzczurak 20:cf211a3b3d9e 1974 1, 0 );
johnAlexander 15:932d8b4e52c9 1975 } else
johnAlexander 16:98ce55ddbb1a 1976 *p_vhv_settings = 0;
johnAlexander 16:98ce55ddbb1a 1977
johnAlexander 16:98ce55ddbb1a 1978
JerrySzczurak 20:cf211a3b3d9e 1979 if ( ( status == VL53L0X_ERROR_NONE ) && restore_config ) {
johnAlexander 15:932d8b4e52c9 1980 /* restore the previous Sequence Config */
JerrySzczurak 20:cf211a3b3d9e 1981 status = vl53l0x_write_byte( dev, VL53L0X_REG_SYSTEM_SEQUENCE_CONFIG,
JerrySzczurak 20:cf211a3b3d9e 1982 sequence_config );
JerrySzczurak 20:cf211a3b3d9e 1983 if ( status == VL53L0X_ERROR_NONE )
JerrySzczurak 20:cf211a3b3d9e 1984 PALDevDataSet( dev, SequenceConfig, sequence_config );
JerrySzczurak 20:cf211a3b3d9e 1985
JerrySzczurak 20:cf211a3b3d9e 1986 }
JerrySzczurak 20:cf211a3b3d9e 1987
JerrySzczurak 20:cf211a3b3d9e 1988 return status;
JerrySzczurak 20:cf211a3b3d9e 1989 }
JerrySzczurak 20:cf211a3b3d9e 1990
JerrySzczurak 20:cf211a3b3d9e 1991 VL53L0X_Error VL53L0X::vl53l0x_perform_phase_calibration( VL53L0X_DEV dev,
johnAlexander 16:98ce55ddbb1a 1992 uint8_t *p_phase_cal, const uint8_t get_data_enable,
JerrySzczurak 20:cf211a3b3d9e 1993 const uint8_t restore_config )
johnAlexander 0:c523920bcc09 1994 {
johnAlexander 16:98ce55ddbb1a 1995 VL53L0X_Error status = VL53L0X_ERROR_NONE;
johnAlexander 16:98ce55ddbb1a 1996 uint8_t sequence_config = 0;
johnAlexander 16:98ce55ddbb1a 1997 uint8_t vhv_settings = 0;
johnAlexander 16:98ce55ddbb1a 1998 uint8_t phase_cal = 0;
johnAlexander 16:98ce55ddbb1a 1999 uint8_t vhv_settingsint;
johnAlexander 15:932d8b4e52c9 2000
johnAlexander 15:932d8b4e52c9 2001 /* store the value of the sequence config,
johnAlexander 15:932d8b4e52c9 2002 * this will be reset before the end of the function
johnAlexander 15:932d8b4e52c9 2003 */
johnAlexander 15:932d8b4e52c9 2004
JerrySzczurak 20:cf211a3b3d9e 2005 if ( restore_config )
JerrySzczurak 20:cf211a3b3d9e 2006 sequence_config = PALDevDataGet( dev, SequenceConfig );
johnAlexander 15:932d8b4e52c9 2007
johnAlexander 15:932d8b4e52c9 2008 /* Run PhaseCal */
JerrySzczurak 20:cf211a3b3d9e 2009 status = vl53l0x_write_byte( dev, VL53L0X_REG_SYSTEM_SEQUENCE_CONFIG, 0x02 );
JerrySzczurak 20:cf211a3b3d9e 2010
JerrySzczurak 20:cf211a3b3d9e 2011 if ( status == VL53L0X_ERROR_NONE )
JerrySzczurak 20:cf211a3b3d9e 2012 status = vl53l0x_perform_single_ref_calibration( dev, 0x0 );
johnAlexander 15:932d8b4e52c9 2013
johnAlexander 15:932d8b4e52c9 2014 /* Read PhaseCal from device */
JerrySzczurak 20:cf211a3b3d9e 2015 if ( ( status == VL53L0X_ERROR_NONE ) && ( get_data_enable == 1 ) ) {
JerrySzczurak 20:cf211a3b3d9e 2016 status = vl53l0x_ref_calibration_io( dev, 1,
JerrySzczurak 20:cf211a3b3d9e 2017 vhv_settings, phase_cal, /* Not used here */
JerrySzczurak 20:cf211a3b3d9e 2018 &vhv_settingsint, p_phase_cal,
JerrySzczurak 20:cf211a3b3d9e 2019 0, 1 );
johnAlexander 15:932d8b4e52c9 2020 } else
johnAlexander 16:98ce55ddbb1a 2021 *p_phase_cal = 0;
johnAlexander 16:98ce55ddbb1a 2022
johnAlexander 16:98ce55ddbb1a 2023
JerrySzczurak 20:cf211a3b3d9e 2024 if ( ( status == VL53L0X_ERROR_NONE ) && restore_config ) {
johnAlexander 15:932d8b4e52c9 2025 /* restore the previous Sequence Config */
JerrySzczurak 20:cf211a3b3d9e 2026 status = vl53l0x_write_byte( dev, VL53L0X_REG_SYSTEM_SEQUENCE_CONFIG,
JerrySzczurak 20:cf211a3b3d9e 2027 sequence_config );
JerrySzczurak 20:cf211a3b3d9e 2028 if ( status == VL53L0X_ERROR_NONE )
JerrySzczurak 20:cf211a3b3d9e 2029 PALDevDataSet( dev, SequenceConfig, sequence_config );
JerrySzczurak 20:cf211a3b3d9e 2030
JerrySzczurak 20:cf211a3b3d9e 2031 }
JerrySzczurak 20:cf211a3b3d9e 2032
JerrySzczurak 20:cf211a3b3d9e 2033 return status;
JerrySzczurak 20:cf211a3b3d9e 2034 }
JerrySzczurak 20:cf211a3b3d9e 2035
JerrySzczurak 20:cf211a3b3d9e 2036 VL53L0X_Error VL53L0X::vl53l0x_perform_ref_calibration( VL53L0X_DEV dev,
JerrySzczurak 20:cf211a3b3d9e 2037 uint8_t *p_vhv_settings, uint8_t *p_phase_cal, uint8_t get_data_enable )
johnAlexander 0:c523920bcc09 2038 {
johnAlexander 16:98ce55ddbb1a 2039 VL53L0X_Error status = VL53L0X_ERROR_NONE;
johnAlexander 16:98ce55ddbb1a 2040 uint8_t sequence_config = 0;
johnAlexander 15:932d8b4e52c9 2041
johnAlexander 15:932d8b4e52c9 2042 /* store the value of the sequence config,
johnAlexander 15:932d8b4e52c9 2043 * this will be reset before the end of the function
johnAlexander 15:932d8b4e52c9 2044 */
johnAlexander 15:932d8b4e52c9 2045
JerrySzczurak 20:cf211a3b3d9e 2046 sequence_config = PALDevDataGet( dev, SequenceConfig );
johnAlexander 15:932d8b4e52c9 2047
johnAlexander 15:932d8b4e52c9 2048 /* In the following function we don't save the config to optimize
johnAlexander 15:932d8b4e52c9 2049 * writes on device. Config is saved and restored only once. */
johnAlexander 16:98ce55ddbb1a 2050 status = vl53l0x_perform_vhv_calibration(
JerrySzczurak 20:cf211a3b3d9e 2051 dev, p_vhv_settings, get_data_enable, 0 );
JerrySzczurak 20:cf211a3b3d9e 2052
JerrySzczurak 20:cf211a3b3d9e 2053
JerrySzczurak 20:cf211a3b3d9e 2054 if ( status == VL53L0X_ERROR_NONE )
johnAlexander 16:98ce55ddbb1a 2055 status = vl53l0x_perform_phase_calibration(
JerrySzczurak 20:cf211a3b3d9e 2056 dev, p_phase_cal, get_data_enable, 0 );
JerrySzczurak 20:cf211a3b3d9e 2057
JerrySzczurak 20:cf211a3b3d9e 2058
JerrySzczurak 20:cf211a3b3d9e 2059 if ( status == VL53L0X_ERROR_NONE ) {
johnAlexander 15:932d8b4e52c9 2060 /* restore the previous Sequence Config */
JerrySzczurak 20:cf211a3b3d9e 2061 status = vl53l0x_write_byte( dev, VL53L0X_REG_SYSTEM_SEQUENCE_CONFIG,
JerrySzczurak 20:cf211a3b3d9e 2062 sequence_config );
JerrySzczurak 20:cf211a3b3d9e 2063 if ( status == VL53L0X_ERROR_NONE )
JerrySzczurak 20:cf211a3b3d9e 2064 PALDevDataSet( dev, SequenceConfig, sequence_config );
JerrySzczurak 20:cf211a3b3d9e 2065
JerrySzczurak 20:cf211a3b3d9e 2066 }
JerrySzczurak 20:cf211a3b3d9e 2067
JerrySzczurak 20:cf211a3b3d9e 2068 return status;
JerrySzczurak 20:cf211a3b3d9e 2069 }
JerrySzczurak 20:cf211a3b3d9e 2070
JerrySzczurak 20:cf211a3b3d9e 2071 void VL53L0X::get_next_good_spad( uint8_t good_spad_array[], uint32_t size,
JerrySzczurak 20:cf211a3b3d9e 2072 uint32_t curr, int32_t *p_next )
johnAlexander 0:c523920bcc09 2073 {
johnAlexander 16:98ce55ddbb1a 2074 uint32_t start_index;
johnAlexander 16:98ce55ddbb1a 2075 uint32_t fine_offset;
johnAlexander 16:98ce55ddbb1a 2076 uint32_t c_spads_per_byte = 8;
johnAlexander 16:98ce55ddbb1a 2077 uint32_t coarse_index;
johnAlexander 16:98ce55ddbb1a 2078 uint32_t fine_index;
johnAlexander 16:98ce55ddbb1a 2079 uint8_t data_byte;
johnAlexander 15:932d8b4e52c9 2080 uint8_t success = 0;
johnAlexander 15:932d8b4e52c9 2081
johnAlexander 15:932d8b4e52c9 2082 /*
johnAlexander 15:932d8b4e52c9 2083 * Starting with the current good spad, loop through the array to find
johnAlexander 15:932d8b4e52c9 2084 * the next. i.e. the next bit set in the sequence.
johnAlexander 15:932d8b4e52c9 2085 *
johnAlexander 15:932d8b4e52c9 2086 * The coarse index is the byte index of the array and the fine index is
johnAlexander 15:932d8b4e52c9 2087 * the index of the bit within each byte.
johnAlexander 15:932d8b4e52c9 2088 */
johnAlexander 15:932d8b4e52c9 2089
johnAlexander 16:98ce55ddbb1a 2090 *p_next = -1;
johnAlexander 16:98ce55ddbb1a 2091
johnAlexander 16:98ce55ddbb1a 2092 start_index = curr / c_spads_per_byte;
johnAlexander 16:98ce55ddbb1a 2093 fine_offset = curr % c_spads_per_byte;
johnAlexander 16:98ce55ddbb1a 2094
JerrySzczurak 20:cf211a3b3d9e 2095 for ( coarse_index = start_index; ( ( coarse_index < size ) && !success );
JerrySzczurak 20:cf211a3b3d9e 2096 coarse_index++ ) {
johnAlexander 16:98ce55ddbb1a 2097 fine_index = 0;
johnAlexander 16:98ce55ddbb1a 2098 data_byte = good_spad_array[coarse_index];
johnAlexander 16:98ce55ddbb1a 2099
JerrySzczurak 20:cf211a3b3d9e 2100 if ( coarse_index == start_index ) {
johnAlexander 15:932d8b4e52c9 2101 /* locate the bit position of the provided current
johnAlexander 15:932d8b4e52c9 2102 * spad bit before iterating */
johnAlexander 16:98ce55ddbb1a 2103 data_byte >>= fine_offset;
johnAlexander 16:98ce55ddbb1a 2104 fine_index = fine_offset;
johnAlexander 15:932d8b4e52c9 2105 }
johnAlexander 15:932d8b4e52c9 2106
JerrySzczurak 20:cf211a3b3d9e 2107 while ( fine_index < c_spads_per_byte ) {
JerrySzczurak 20:cf211a3b3d9e 2108 if ( ( data_byte & 0x1 ) == 1 ) {
johnAlexander 15:932d8b4e52c9 2109 success = 1;
johnAlexander 16:98ce55ddbb1a 2110 *p_next = coarse_index * c_spads_per_byte + fine_index;
johnAlexander 15:932d8b4e52c9 2111 break;
johnAlexander 15:932d8b4e52c9 2112 }
johnAlexander 16:98ce55ddbb1a 2113 data_byte >>= 1;
johnAlexander 16:98ce55ddbb1a 2114 fine_index++;
johnAlexander 15:932d8b4e52c9 2115 }
johnAlexander 15:932d8b4e52c9 2116 }
johnAlexander 0:c523920bcc09 2117 }
johnAlexander 0:c523920bcc09 2118
JerrySzczurak 20:cf211a3b3d9e 2119 uint8_t VL53L0X::is_aperture( uint32_t spad_index )
johnAlexander 0:c523920bcc09 2120 {
johnAlexander 15:932d8b4e52c9 2121 /*
johnAlexander 15:932d8b4e52c9 2122 * This function reports if a given spad index is an aperture SPAD by
johnAlexander 15:932d8b4e52c9 2123 * deriving the quadrant.
johnAlexander 15:932d8b4e52c9 2124 */
johnAlexander 15:932d8b4e52c9 2125 uint32_t quadrant;
johnAlexander 16:98ce55ddbb1a 2126 uint8_t is_aperture = 1;
johnAlexander 16:98ce55ddbb1a 2127 quadrant = spad_index >> 6;
JerrySzczurak 20:cf211a3b3d9e 2128 if ( refArrayQuadrants[quadrant] == REF_ARRAY_SPAD_0 )
johnAlexander 16:98ce55ddbb1a 2129 is_aperture = 0;
johnAlexander 16:98ce55ddbb1a 2130
johnAlexander 16:98ce55ddbb1a 2131 return is_aperture;
johnAlexander 0:c523920bcc09 2132 }
johnAlexander 0:c523920bcc09 2133
JerrySzczurak 20:cf211a3b3d9e 2134 VL53L0X_Error VL53L0X::enable_spad_bit( uint8_t spad_array[], uint32_t size,
JerrySzczurak 20:cf211a3b3d9e 2135 uint32_t spad_index )
johnAlexander 0:c523920bcc09 2136 {
johnAlexander 15:932d8b4e52c9 2137 VL53L0X_Error status = VL53L0X_ERROR_NONE;
johnAlexander 16:98ce55ddbb1a 2138 uint32_t c_spads_per_byte = 8;
johnAlexander 16:98ce55ddbb1a 2139 uint32_t coarse_index;
johnAlexander 16:98ce55ddbb1a 2140 uint32_t fine_index;
johnAlexander 16:98ce55ddbb1a 2141
johnAlexander 16:98ce55ddbb1a 2142 coarse_index = spad_index / c_spads_per_byte;
johnAlexander 16:98ce55ddbb1a 2143 fine_index = spad_index % c_spads_per_byte;
JerrySzczurak 20:cf211a3b3d9e 2144 if ( coarse_index >= size )
johnAlexander 15:932d8b4e52c9 2145 status = VL53L0X_ERROR_REF_SPAD_INIT;
johnAlexander 15:932d8b4e52c9 2146 else
JerrySzczurak 20:cf211a3b3d9e 2147 spad_array[coarse_index] |= ( 1 << fine_index );
JerrySzczurak 20:cf211a3b3d9e 2148
JerrySzczurak 20:cf211a3b3d9e 2149 return status;
JerrySzczurak 20:cf211a3b3d9e 2150 }
JerrySzczurak 20:cf211a3b3d9e 2151
JerrySzczurak 20:cf211a3b3d9e 2152 VL53L0X_Error VL53L0X::set_ref_spad_map( VL53L0X_DEV dev, uint8_t *p_ref_spad_array )
JerrySzczurak 20:cf211a3b3d9e 2153 {
JerrySzczurak 20:cf211a3b3d9e 2154 VL53L0X_Error status = vl53l0x_write_multi( dev,
johnAlexander 15:932d8b4e52c9 2155 VL53L0X_REG_GLOBAL_CONFIG_SPAD_ENABLES_REF_0,
JerrySzczurak 20:cf211a3b3d9e 2156 p_ref_spad_array, 6 );
JerrySzczurak 20:cf211a3b3d9e 2157
JerrySzczurak 20:cf211a3b3d9e 2158 return status;
JerrySzczurak 20:cf211a3b3d9e 2159 }
JerrySzczurak 20:cf211a3b3d9e 2160
JerrySzczurak 20:cf211a3b3d9e 2161 VL53L0X_Error VL53L0X::get_ref_spad_map( VL53L0X_DEV dev, uint8_t *p_ref_spad_array )
JerrySzczurak 20:cf211a3b3d9e 2162 {
JerrySzczurak 20:cf211a3b3d9e 2163 VL53L0X_Error status = vl53l0x_read_multi( dev,
johnAlexander 15:932d8b4e52c9 2164 VL53L0X_REG_GLOBAL_CONFIG_SPAD_ENABLES_REF_0,
johnAlexander 16:98ce55ddbb1a 2165 p_ref_spad_array,
JerrySzczurak 20:cf211a3b3d9e 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++)
JerrySzczurak 21:627b65069e6d 2171 // status = vl53l0x_read_byte(Dev, (VL53L0X_REG_GLOBAL_CONFIG_SPAD_ENABLES_REF_0 + count), &refSpadArray[count]);
johnAlexander 15:932d8b4e52c9 2172 return status;
johnAlexander 0:c523920bcc09 2173 }
johnAlexander 0:c523920bcc09 2174
JerrySzczurak 20:cf211a3b3d9e 2175 VL53L0X_Error VL53L0X::enable_ref_spads( VL53L0X_DEV dev,
JerrySzczurak 20:cf211a3b3d9e 2176 uint8_t aperture_spads,
JerrySzczurak 20:cf211a3b3d9e 2177 uint8_t good_spad_array[],
JerrySzczurak 20:cf211a3b3d9e 2178 uint8_t spad_array[],
JerrySzczurak 20:cf211a3b3d9e 2179 uint32_t size,
JerrySzczurak 20:cf211a3b3d9e 2180 uint32_t start,
JerrySzczurak 20:cf211a3b3d9e 2181 uint32_t offset,
JerrySzczurak 20:cf211a3b3d9e 2182 uint32_t spad_count,
JerrySzczurak 20:cf211a3b3d9e 2183 uint32_t *p_last_spad )
johnAlexander 0:c523920bcc09 2184 {
johnAlexander 15:932d8b4e52c9 2185 VL53L0X_Error status = VL53L0X_ERROR_NONE;
johnAlexander 15:932d8b4e52c9 2186 uint32_t index;
johnAlexander 15:932d8b4e52c9 2187 uint32_t i;
johnAlexander 16:98ce55ddbb1a 2188 int32_t next_good_spad = offset;
johnAlexander 16:98ce55ddbb1a 2189 uint32_t current_spad;
johnAlexander 16:98ce55ddbb1a 2190 uint8_t check_spad_array[6];
johnAlexander 15:932d8b4e52c9 2191
johnAlexander 15:932d8b4e52c9 2192 /*
johnAlexander 15:932d8b4e52c9 2193 * This function takes in a spad array which may or may not have SPADS
johnAlexander 15:932d8b4e52c9 2194 * already enabled and appends from a given offset a requested number
johnAlexander 15:932d8b4e52c9 2195 * of new SPAD enables. The 'good spad map' is applied to
johnAlexander 15:932d8b4e52c9 2196 * determine the next SPADs to enable.
johnAlexander 15:932d8b4e52c9 2197 *
johnAlexander 15:932d8b4e52c9 2198 * This function applies to only aperture or only non-aperture spads.
johnAlexander 15:932d8b4e52c9 2199 * Checks are performed to ensure this.
johnAlexander 15:932d8b4e52c9 2200 */
johnAlexander 15:932d8b4e52c9 2201
johnAlexander 16:98ce55ddbb1a 2202 current_spad = offset;
JerrySzczurak 20:cf211a3b3d9e 2203 for ( index = 0; index < spad_count; index++ ) {
JerrySzczurak 20:cf211a3b3d9e 2204 get_next_good_spad( good_spad_array, size, current_spad,
JerrySzczurak 20:cf211a3b3d9e 2205 &next_good_spad );
JerrySzczurak 20:cf211a3b3d9e 2206
JerrySzczurak 20:cf211a3b3d9e 2207 if ( next_good_spad == -1 ) {
johnAlexander 15:932d8b4e52c9 2208 status = VL53L0X_ERROR_REF_SPAD_INIT;
johnAlexander 15:932d8b4e52c9 2209 break;
johnAlexander 15:932d8b4e52c9 2210 }
johnAlexander 15:932d8b4e52c9 2211
johnAlexander 15:932d8b4e52c9 2212 /* Confirm that the next good SPAD is non-aperture */
JerrySzczurak 20:cf211a3b3d9e 2213 if ( is_aperture( start + next_good_spad ) != aperture_spads ) {
johnAlexander 15:932d8b4e52c9 2214 /* if we can't get the required number of good aperture
johnAlexander 15:932d8b4e52c9 2215 * spads from the current quadrant then this is an error
johnAlexander 15:932d8b4e52c9 2216 */
johnAlexander 15:932d8b4e52c9 2217 status = VL53L0X_ERROR_REF_SPAD_INIT;
johnAlexander 15:932d8b4e52c9 2218 break;
johnAlexander 15:932d8b4e52c9 2219 }
JerrySzczurak 20:cf211a3b3d9e 2220 current_spad = ( uint32_t )next_good_spad;
JerrySzczurak 20:cf211a3b3d9e 2221 enable_spad_bit( spad_array, size, current_spad );
johnAlexander 16:98ce55ddbb1a 2222 current_spad++;
johnAlexander 15:932d8b4e52c9 2223 }
johnAlexander 16:98ce55ddbb1a 2224 *p_last_spad = current_spad;
johnAlexander 15:932d8b4e52c9 2225
JerrySzczurak 20:cf211a3b3d9e 2226 if ( status == VL53L0X_ERROR_NONE )
JerrySzczurak 20:cf211a3b3d9e 2227 status = set_ref_spad_map( dev, spad_array );
JerrySzczurak 20:cf211a3b3d9e 2228
JerrySzczurak 20:cf211a3b3d9e 2229
JerrySzczurak 20:cf211a3b3d9e 2230 if ( status == VL53L0X_ERROR_NONE ) {
JerrySzczurak 20:cf211a3b3d9e 2231 status = get_ref_spad_map( dev, check_spad_array );
johnAlexander 15:932d8b4e52c9 2232
johnAlexander 15:932d8b4e52c9 2233 i = 0;
johnAlexander 15:932d8b4e52c9 2234
johnAlexander 15:932d8b4e52c9 2235 /* Compare spad maps. If not equal report error. */
JerrySzczurak 20:cf211a3b3d9e 2236 while ( i < size ) {
JerrySzczurak 20:cf211a3b3d9e 2237 if ( spad_array[i] != check_spad_array[i] ) {
johnAlexander 15:932d8b4e52c9 2238 status = VL53L0X_ERROR_REF_SPAD_INIT;
johnAlexander 15:932d8b4e52c9 2239 break;
johnAlexander 15:932d8b4e52c9 2240 }
johnAlexander 15:932d8b4e52c9 2241 i++;
johnAlexander 15:932d8b4e52c9 2242 }
johnAlexander 15:932d8b4e52c9 2243 }
johnAlexander 15:932d8b4e52c9 2244 return status;
johnAlexander 0:c523920bcc09 2245 }
johnAlexander 0:c523920bcc09 2246
JerrySzczurak 20:cf211a3b3d9e 2247 VL53L0X_Error VL53L0X::vl53l0x_set_device_mode( VL53L0X_DEV dev, VL53L0X_DeviceModes device_mode )
johnAlexander 0:c523920bcc09 2248 {
johnAlexander 16:98ce55ddbb1a 2249 VL53L0X_Error status = VL53L0X_ERROR_NONE;
johnAlexander 15:932d8b4e52c9 2250
JerrySzczurak 20:cf211a3b3d9e 2251 LOG_FUNCTION_START( "%d", ( int )DeviceMode );
JerrySzczurak 20:cf211a3b3d9e 2252
JerrySzczurak 20:cf211a3b3d9e 2253 switch ( device_mode ) {
johnAlexander 15:932d8b4e52c9 2254 case VL53L0X_DEVICEMODE_SINGLE_RANGING:
johnAlexander 15:932d8b4e52c9 2255 case VL53L0X_DEVICEMODE_CONTINUOUS_RANGING:
johnAlexander 15:932d8b4e52c9 2256 case VL53L0X_DEVICEMODE_CONTINUOUS_TIMED_RANGING:
johnAlexander 15:932d8b4e52c9 2257 case VL53L0X_DEVICEMODE_GPIO_DRIVE:
johnAlexander 15:932d8b4e52c9 2258 case VL53L0X_DEVICEMODE_GPIO_OSC:
johnAlexander 15:932d8b4e52c9 2259 /* Supported modes */
JerrySzczurak 20:cf211a3b3d9e 2260 VL53L0X_SETPARAMETERFIELD( dev, DeviceMode, device_mode );
johnAlexander 15:932d8b4e52c9 2261 break;
johnAlexander 15:932d8b4e52c9 2262 default:
johnAlexander 15:932d8b4e52c9 2263 /* Unsupported mode */
johnAlexander 16:98ce55ddbb1a 2264 status = VL53L0X_ERROR_MODE_NOT_SUPPORTED;
johnAlexander 15:932d8b4e52c9 2265 }
johnAlexander 15:932d8b4e52c9 2266
JerrySzczurak 20:cf211a3b3d9e 2267 LOG_FUNCTION_END( status );
JerrySzczurak 20:cf211a3b3d9e 2268 return status;
JerrySzczurak 20:cf211a3b3d9e 2269 }
JerrySzczurak 20:cf211a3b3d9e 2270
JerrySzczurak 20:cf211a3b3d9e 2271 VL53L0X_Error VL53L0X::vl53l0x_set_interrupt_thresholds( VL53L0X_DEV dev,
johnAlexander 16:98ce55ddbb1a 2272 VL53L0X_DeviceModes device_mode, FixPoint1616_t threshold_low,
JerrySzczurak 20:cf211a3b3d9e 2273 FixPoint1616_t threshold_high )
johnAlexander 13:615f7e38568c 2274 {
johnAlexander 16:98ce55ddbb1a 2275 VL53L0X_Error status = VL53L0X_ERROR_NONE;
johnAlexander 16:98ce55ddbb1a 2276 uint16_t threshold16;
JerrySzczurak 20:cf211a3b3d9e 2277 LOG_FUNCTION_START( "" );
johnAlexander 15:932d8b4e52c9 2278
johnAlexander 15:932d8b4e52c9 2279 /* no dependency on DeviceMode for Ewok */
johnAlexander 15:932d8b4e52c9 2280 /* Need to divide by 2 because the FW will apply a x2 */
JerrySzczurak 20:cf211a3b3d9e 2281 threshold16 = ( uint16_t )( ( threshold_low >> 17 ) & 0x00fff );
JerrySzczurak 20:cf211a3b3d9e 2282 status = vl53l0x_write_word( dev, VL53L0X_REG_SYSTEM_THRESH_LOW, threshold16 );
JerrySzczurak 20:cf211a3b3d9e 2283
JerrySzczurak 20:cf211a3b3d9e 2284 if ( status == VL53L0X_ERROR_NONE ) {
johnAlexander 15:932d8b4e52c9 2285 /* Need to divide by 2 because the FW will apply a x2 */
JerrySzczurak 20:cf211a3b3d9e 2286 threshold16 = ( uint16_t )( ( threshold_high >> 17 ) & 0x00fff );
JerrySzczurak 20:cf211a3b3d9e 2287 status = vl53l0x_write_word( dev, VL53L0X_REG_SYSTEM_THRESH_HIGH,
JerrySzczurak 20:cf211a3b3d9e 2288 threshold16 );
JerrySzczurak 20:cf211a3b3d9e 2289 }
JerrySzczurak 20:cf211a3b3d9e 2290
JerrySzczurak 20:cf211a3b3d9e 2291 LOG_FUNCTION_END( status );
JerrySzczurak 20:cf211a3b3d9e 2292 return status;
JerrySzczurak 20:cf211a3b3d9e 2293 }
JerrySzczurak 20:cf211a3b3d9e 2294
JerrySzczurak 20:cf211a3b3d9e 2295 VL53L0X_Error VL53L0X::vl53l0x_get_interrupt_thresholds( VL53L0X_DEV dev,
johnAlexander 16:98ce55ddbb1a 2296 VL53L0X_DeviceModes device_mode, FixPoint1616_t *p_threshold_low,
JerrySzczurak 20:cf211a3b3d9e 2297 FixPoint1616_t *p_threshold_high )
johnAlexander 0:c523920bcc09 2298 {
johnAlexander 16:98ce55ddbb1a 2299 VL53L0X_Error status = VL53L0X_ERROR_NONE;
johnAlexander 16:98ce55ddbb1a 2300 uint16_t threshold16;
JerrySzczurak 20:cf211a3b3d9e 2301 LOG_FUNCTION_START( "" );
johnAlexander 15:932d8b4e52c9 2302
johnAlexander 15:932d8b4e52c9 2303 /* no dependency on DeviceMode for Ewok */
johnAlexander 15:932d8b4e52c9 2304
JerrySzczurak 20:cf211a3b3d9e 2305 status = vl53l0x_read_word( dev, VL53L0X_REG_SYSTEM_THRESH_LOW, &threshold16 );
johnAlexander 15:932d8b4e52c9 2306 /* Need to multiply by 2 because the FW will apply a x2 */
JerrySzczurak 20:cf211a3b3d9e 2307 *p_threshold_low = ( FixPoint1616_t )( ( 0x00fff & threshold16 ) << 17 );
JerrySzczurak 20:cf211a3b3d9e 2308
JerrySzczurak 20:cf211a3b3d9e 2309 if ( status == VL53L0X_ERROR_NONE ) {
JerrySzczurak 20:cf211a3b3d9e 2310 status = vl53l0x_read_word( dev, VL53L0X_REG_SYSTEM_THRESH_HIGH,
JerrySzczurak 20:cf211a3b3d9e 2311 &threshold16 );
johnAlexander 15:932d8b4e52c9 2312 /* Need to multiply by 2 because the FW will apply a x2 */
johnAlexander 16:98ce55ddbb1a 2313 *p_threshold_high =
JerrySzczurak 20:cf211a3b3d9e 2314 ( FixPoint1616_t )( ( 0x00fff & threshold16 ) << 17 );
JerrySzczurak 20:cf211a3b3d9e 2315 }
JerrySzczurak 20:cf211a3b3d9e 2316
JerrySzczurak 20:cf211a3b3d9e 2317 LOG_FUNCTION_END( status );
JerrySzczurak 20:cf211a3b3d9e 2318 return status;
JerrySzczurak 20:cf211a3b3d9e 2319 }
JerrySzczurak 20:cf211a3b3d9e 2320
JerrySzczurak 20:cf211a3b3d9e 2321 VL53L0X_Error VL53L0X::vl53l0x_load_tuning_settings( VL53L0X_DEV dev,
JerrySzczurak 20:cf211a3b3d9e 2322 uint8_t *p_tuning_setting_buffer )
johnAlexander 0:c523920bcc09 2323 {
johnAlexander 16:98ce55ddbb1a 2324 VL53L0X_Error status = VL53L0X_ERROR_NONE;
johnAlexander 15:932d8b4e52c9 2325 int i;
johnAlexander 16:98ce55ddbb1a 2326 int index;
johnAlexander 15:932d8b4e52c9 2327 uint8_t msb;
johnAlexander 15:932d8b4e52c9 2328 uint8_t lsb;
johnAlexander 16:98ce55ddbb1a 2329 uint8_t select_param;
johnAlexander 16:98ce55ddbb1a 2330 uint8_t number_of_writes;
johnAlexander 16:98ce55ddbb1a 2331 uint8_t address;
johnAlexander 16:98ce55ddbb1a 2332 uint8_t local_buffer[4]; /* max */
johnAlexander 16:98ce55ddbb1a 2333 uint16_t temp16;
johnAlexander 15:932d8b4e52c9 2334
JerrySzczurak 20:cf211a3b3d9e 2335 LOG_FUNCTION_START( "" );
johnAlexander 15:932d8b4e52c9 2336
johnAlexander 16:98ce55ddbb1a 2337 index = 0;
johnAlexander 16:98ce55ddbb1a 2338
JerrySzczurak 20:cf211a3b3d9e 2339 while ( ( *( p_tuning_setting_buffer + index ) != 0 ) &&
JerrySzczurak 20:cf211a3b3d9e 2340 ( status == VL53L0X_ERROR_NONE ) ) {
JerrySzczurak 20:cf211a3b3d9e 2341 number_of_writes = *( p_tuning_setting_buffer + index );
johnAlexander 16:98ce55ddbb1a 2342 index++;
JerrySzczurak 20:cf211a3b3d9e 2343 if ( number_of_writes == 0xFF ) {
johnAlexander 15:932d8b4e52c9 2344 /* internal parameters */
JerrySzczurak 20:cf211a3b3d9e 2345 select_param = *( p_tuning_setting_buffer + index );
johnAlexander 16:98ce55ddbb1a 2346 index++;
JerrySzczurak 20:cf211a3b3d9e 2347 switch ( select_param ) {
johnAlexander 15:932d8b4e52c9 2348 case 0: /* uint16_t SigmaEstRefArray -> 2 bytes */
JerrySzczurak 20:cf211a3b3d9e 2349 msb = *( p_tuning_setting_buffer + index );
johnAlexander 16:98ce55ddbb1a 2350 index++;
JerrySzczurak 20:cf211a3b3d9e 2351 lsb = *( p_tuning_setting_buffer + index );
johnAlexander 16:98ce55ddbb1a 2352 index++;
JerrySzczurak 20:cf211a3b3d9e 2353 temp16 = VL53L0X_MAKEUINT16( lsb, msb );
JerrySzczurak 20:cf211a3b3d9e 2354 PALDevDataSet( dev, SigmaEstRefArray, temp16 );
johnAlexander 15:932d8b4e52c9 2355 break;
johnAlexander 15:932d8b4e52c9 2356 case 1: /* uint16_t SigmaEstEffPulseWidth -> 2 bytes */
JerrySzczurak 20:cf211a3b3d9e 2357 msb = *( p_tuning_setting_buffer + index );
johnAlexander 16:98ce55ddbb1a 2358 index++;
JerrySzczurak 20:cf211a3b3d9e 2359 lsb = *( p_tuning_setting_buffer + index );
johnAlexander 16:98ce55ddbb1a 2360 index++;
JerrySzczurak 20:cf211a3b3d9e 2361 temp16 = VL53L0X_MAKEUINT16( lsb, msb );
JerrySzczurak 20:cf211a3b3d9e 2362 PALDevDataSet( dev, SigmaEstEffPulseWidth,
JerrySzczurak 20:cf211a3b3d9e 2363 temp16 );
johnAlexander 15:932d8b4e52c9 2364 break;
johnAlexander 15:932d8b4e52c9 2365 case 2: /* uint16_t SigmaEstEffAmbWidth -> 2 bytes */
JerrySzczurak 20:cf211a3b3d9e 2366 msb = *( p_tuning_setting_buffer + index );
johnAlexander 16:98ce55ddbb1a 2367 index++;
JerrySzczurak 20:cf211a3b3d9e 2368 lsb = *( p_tuning_setting_buffer + index );
johnAlexander 16:98ce55ddbb1a 2369 index++;
JerrySzczurak 20:cf211a3b3d9e 2370 temp16 = VL53L0X_MAKEUINT16( lsb, msb );
JerrySzczurak 20:cf211a3b3d9e 2371 PALDevDataSet( dev, SigmaEstEffAmbWidth, temp16 );
johnAlexander 15:932d8b4e52c9 2372 break;
johnAlexander 15:932d8b4e52c9 2373 case 3: /* uint16_t targetRefRate -> 2 bytes */
JerrySzczurak 20:cf211a3b3d9e 2374 msb = *( p_tuning_setting_buffer + index );
johnAlexander 16:98ce55ddbb1a 2375 index++;
JerrySzczurak 20:cf211a3b3d9e 2376 lsb = *( p_tuning_setting_buffer + index );
johnAlexander 16:98ce55ddbb1a 2377 index++;
JerrySzczurak 20:cf211a3b3d9e 2378 temp16 = VL53L0X_MAKEUINT16( lsb, msb );
JerrySzczurak 20:cf211a3b3d9e 2379 PALDevDataSet( dev, targetRefRate, temp16 );
johnAlexander 15:932d8b4e52c9 2380 break;
johnAlexander 15:932d8b4e52c9 2381 default: /* invalid parameter */
johnAlexander 16:98ce55ddbb1a 2382 status = VL53L0X_ERROR_INVALID_PARAMS;
johnAlexander 15:932d8b4e52c9 2383 }
johnAlexander 15:932d8b4e52c9 2384
JerrySzczurak 20:cf211a3b3d9e 2385 } else if ( number_of_writes <= 4 ) {
JerrySzczurak 20:cf211a3b3d9e 2386 address = *( p_tuning_setting_buffer + index );
johnAlexander 16:98ce55ddbb1a 2387 index++;
johnAlexander 16:98ce55ddbb1a 2388
JerrySzczurak 20:cf211a3b3d9e 2389 for ( i = 0; i < number_of_writes; i++ ) {
JerrySzczurak 20:cf211a3b3d9e 2390 local_buffer[i] = *( p_tuning_setting_buffer +
JerrySzczurak 20:cf211a3b3d9e 2391 index );
johnAlexander 16:98ce55ddbb1a 2392 index++;
johnAlexander 15:932d8b4e52c9 2393 }
johnAlexander 15:932d8b4e52c9 2394
JerrySzczurak 20:cf211a3b3d9e 2395 status = vl53l0x_write_multi( dev, address, local_buffer,
JerrySzczurak 20:cf211a3b3d9e 2396 number_of_writes );
johnAlexander 15:932d8b4e52c9 2397
johnAlexander 15:932d8b4e52c9 2398 } else {
johnAlexander 16:98ce55ddbb1a 2399 status = VL53L0X_ERROR_INVALID_PARAMS;
johnAlexander 15:932d8b4e52c9 2400 }
johnAlexander 15:932d8b4e52c9 2401 }
johnAlexander 15:932d8b4e52c9 2402
JerrySzczurak 20:cf211a3b3d9e 2403 LOG_FUNCTION_END( status );
JerrySzczurak 20:cf211a3b3d9e 2404 return status;
JerrySzczurak 20:cf211a3b3d9e 2405 }
JerrySzczurak 20:cf211a3b3d9e 2406
JerrySzczurak 20:cf211a3b3d9e 2407 VL53L0X_Error VL53L0X::vl53l0x_check_and_load_interrupt_settings( VL53L0X_DEV dev,
JerrySzczurak 20:cf211a3b3d9e 2408 uint8_t start_not_stopflag )
johnAlexander 0:c523920