16static constexpr uint8_t DEFAULT_ADDRESS = 0x29;
17static constexpr uint16_t MODEL_ID = 0xEACC;
19static constexpr uint16_t I2C_DEVICE_ADDRESS = 0x0001;
20static constexpr uint16_t VHV_CONFIG_TIMEOUT_MACROP_LOOP_BOUND = 0x0008;
21static constexpr uint16_t VHV_CONFIG_INIT = 0x000B;
22static constexpr uint16_t ALGO_PART_TO_PART_RANGE_OFFSET_MM = 0x001E;
23static constexpr uint16_t MM_CONFIG_INNER_OFFSET_MM = 0x0020;
24static constexpr uint16_t MM_CONFIG_OUTER_OFFSET_MM = 0x0022;
25static constexpr uint16_t DEFAULT_CONFIG_START = 0x002D;
26static constexpr uint16_t GPIO_TIO_HV_STATUS = 0x0031;
27static constexpr uint16_t PHASECAL_CONFIG_TIMEOUT_MACROP = 0x004B;
28static constexpr uint16_t RANGE_CONFIG_TIMEOUT_MACROP_A_HI = 0x005E;
29static constexpr uint16_t RANGE_CONFIG_VCSEL_PERIOD_A = 0x0060;
30static constexpr uint16_t RANGE_CONFIG_TIMEOUT_MACROP_B_HI = 0x0061;
31static constexpr uint16_t RANGE_CONFIG_VCSEL_PERIOD_B = 0x0063;
32static constexpr uint16_t RANGE_CONFIG_MIN_COUNT_RATE_RTN_LIMIT_MCPS = 0x0066;
33static constexpr uint16_t RANGE_CONFIG_VALID_PHASE_HIGH = 0x0069;
34static constexpr uint16_t SD_CONFIG_WOI_SD0 = 0x0078;
35static constexpr uint16_t SD_CONFIG_INITIAL_PHASE_SD0 = 0x007A;
36static constexpr uint16_t ROI_CONFIG_USER_ROI_CENTRE_SPAD = 0x007F;
37static constexpr uint16_t ROI_CONFIG_USER_ROI_REQUESTED_GLOBAL_XY_SIZE = 0x0080;
38static constexpr uint16_t SYSTEM_INTERRUPT_CLEAR = 0x0086;
39static constexpr uint16_t SYSTEM_MODE_START = 0x0087;
40static constexpr uint16_t RESULT_RANGE_STATUS = 0x0089;
41static constexpr uint16_t FIRMWARE_SYSTEM_STATUS = 0x00E5;
42static constexpr uint16_t IDENTIFICATION_MODEL_ID = 0x010F;
43static constexpr uint16_t ROI_CONFIG_MODE_ROI_CENTRE_SPAD = 0x013E;
45static constexpr uint8_t MODE_START_SINGLE_SHOT = 0x10;
47static constexpr uint8_t ROI_CENTER_LARGE = 199;
49static constexpr size_t RESULT_LENGTH = 15;
51static constexpr uint8_t RANGE_STATUS_VALID = 9;
54static const uint8_t DEFAULT_CONFIG[] PROGMEM = {
150struct DistanceModeConfig {
151 uint8_t phasecal_timeout;
152 uint8_t vcsel_period_a;
153 uint8_t vcsel_period_b;
154 uint8_t valid_phase_high;
156 uint16_t initial_phase_sd0;
158static const DistanceModeConfig DISTANCE_MODE_CONFIGS[] PROGMEM = {
159 {0x14, 0x07, 0x05, 0x38, 0x0705, 0x0606},
160 {0x0B, 0x0B, 0x09, 0x78, 0x0B09, 0x0A0A},
161 {0x0A, 0x0F, 0x0D, 0xB8, 0x0F0D, 0x0E0E},
165struct TimingBudgetConfig {
170static const TimingBudgetConfig SHORT_TIMING_BUDGETS[]
PROGMEM = {
171 {15, 0x001D, 0x0027}, {20, 0x0051, 0x006E}, {33, 0x00D6, 0x006E}, {50, 0x01AE, 0x01E8},
172 {100, 0x02E1, 0x0388}, {200, 0x03E1, 0x0496}, {500, 0x0591, 0x05C1},
174static const TimingBudgetConfig LONG_TIMING_BUDGETS[]
PROGMEM = {
175 {20, 0x001E, 0x0022}, {33, 0x0060, 0x006E}, {50, 0x00AD, 0x00C6},
176 {100, 0x01CC, 0x01EA}, {200, 0x02D9, 0x02F8}, {500, 0x048F, 0x04A4},
181static const TimingBudgetConfig MEDIUM_TIMING_BUDGETS[]
PROGMEM = {
182 {20, 0x0028, 0x0030}, {33, 0x0080, 0x009A}, {50, 0x00E7, 0x018B},
183 {100, 0x0288, 0x02A4}, {200, 0x0390, 0x03AD}, {500, 0x04BE, 0x04E5},
187static bool find_timing_budget(
DistanceMode mode, uint16_t budget_ms, TimingBudgetConfig *
out) {
188 const TimingBudgetConfig *table = LONG_TIMING_BUDGETS;
189 size_t count = std::size(LONG_TIMING_BUDGETS);
191 table = SHORT_TIMING_BUDGETS;
192 count = std::size(SHORT_TIMING_BUDGETS);
194 table = MEDIUM_TIMING_BUDGETS;
195 count = std::size(MEDIUM_TIMING_BUDGETS);
197 for (
size_t i = 0; i <
count; i++) {
199 if (
out->budget_ms == budget_ms)
209 VL53L1XSensor::first_sensor =
this;
217 if (other->enable_pin_ !=
nullptr) {
218 other->enable_pin_->setup();
219 other->enable_pin_->digital_write(
false);
225 const uint8_t final_address = this->
address_;
238 if (!this->
read_word16_(IDENTIFICATION_MODEL_ID, &model_id)) {
242 if (model_id != MODEL_ID) {
243 ESP_LOGE(TAG,
"Wrong model ID 0x%04X", model_id);
248 if (final_address != DEFAULT_ADDRESS) {
249 if (!this->
write_byte16_(I2C_DEVICE_ADDRESS, final_address & 0x7F)) {
278 for (uint8_t i = 0; i < 100; i++) {
288 TimingBudgetConfig budget;
290 ESP_LOGE(TAG,
"Unsupported timing budget %u ms", this->
budget_ms_);
293 DistanceModeConfig
mode;
296 uint8_t config[
sizeof(DEFAULT_CONFIG)];
301 center = ROI_CENTER_LARGE;
303 !this->
read_byte16_(ROI_CONFIG_MODE_ROI_CENTRE_SPAD, ¢er)) {
317 this->
write_word16_(RANGE_CONFIG_TIMEOUT_MACROP_A_HI, budget.macrop_a) &&
318 this->
write_word16_(RANGE_CONFIG_TIMEOUT_MACROP_B_HI, budget.macrop_b) &&
323 this->
write_byte16_(ROI_CONFIG_USER_ROI_CENTRE_SPAD, center) &&
324 this->
write_byte16_(ROI_CONFIG_USER_ROI_REQUESTED_GLOBAL_XY_SIZE,
334 ESP_LOGD(TAG,
"Measurement still running");
338 ESP_LOGW(TAG,
"Starting measurement failed");
353 if (!this->
read_byte16_(GPIO_TIO_HV_STATUS, &gpio_status) || (gpio_status & 0x01) == 0) {
354 if (elapsed >
static_cast<uint32_t>(this->budget_ms_) * 2 + 100) {
355 ESP_LOGW(TAG,
"Measurement timed out");
363 uint8_t result[RESULT_LENGTH];
366 ESP_LOGW(TAG,
"Reading result failed");
385 const uint8_t range_status = result[0] & 0x1F;
386 if (range_status != RANGE_STATUS_VALID) {
387 ESP_LOGD(TAG,
"Range status %u", range_status);
391 const uint16_t distance_mm =
encode_uint16(result[13], result[14]);
399 LOG_SENSOR(
"",
"VL53L1X",
this);
400 LOG_UPDATE_INTERVAL(
this);
401 LOG_I2C_DEVICE(
this);
404 " Distance Mode: %s\n"
405 " Timing Budget: %u ms\n"
408 LOG_STR_ARG(DistanceModeStrings::get_log_str(this->
distance_mode_, DistanceModeStrings::LAST_INDEX)),
411 ESP_LOGCONFIG(TAG,
" ROI Center: %u", this->
roi_center_);
420 const uint8_t data[2] = {
static_cast<uint8_t
>(value >> 8),
static_cast<uint8_t
>(value)};