ESPHome 2026.11.0-dev
Loading...
Searching...
No Matches
vl53l0x_sensor.cpp
Go to the documentation of this file.
1#include "vl53l0x_sensor.h"
2#include "esphome/core/log.h"
3
4#include <cinttypes>
5
6/*
7 * Most of the code in this integration is based on the VL53L0x library
8 * by Pololu (Pololu Corporation), which in turn is based on the VL53L0X
9 * API from ST.
10 *
11 * For more information about licensing, please view the included LICENSE.txt file
12 * in the vl53l0x integration directory.
13 */
14
16
17ESPHOME_LOG_TAG(TAG, "vl53l0x");
18
19std::list<VL53L0XSensor *> VL53L0XSensor::vl53_sensors; // NOLINT(cppcoreguidelines-avoid-non-const-global-variables)
20bool VL53L0XSensor::enable_pin_setup_complete = false; // NOLINT(cppcoreguidelines-avoid-non-const-global-variables)
21
23
25 LOG_SENSOR("", "VL53L0X", this);
26 LOG_UPDATE_INTERVAL(this);
27 LOG_I2C_DEVICE(this);
28 if (this->enable_pin_ != nullptr) {
29 LOG_PIN(" Enable Pin: ", this->enable_pin_);
30 }
31 ESP_LOGCONFIG(TAG,
32 " Timeout: %" PRIu32 "%s\n"
33 " Timing Budget %" PRIu32 "us ",
34 this->timeout_us_, this->timeout_us_ > 0 ? LOG_STR_LITERAL("us") : LOG_STR_LITERAL(" (no timeout)"),
36}
37
40 for (auto &vl53_sensor : vl53_sensors) {
41 if (vl53_sensor->enable_pin_ != nullptr) {
42 // Set enable pin as OUTPUT and disable the enable pin to force vl53 to HW Standby mode
43 vl53_sensor->enable_pin_->setup();
44 vl53_sensor->enable_pin_->digital_write(false);
45 }
46 }
48 }
49
50 if (this->enable_pin_ != nullptr) {
51 // Enable the enable pin to cause FW boot (to get back to 0x29 default address)
52 this->enable_pin_->digital_write(true);
54 }
55
56 // Save the i2c address we want and force it to use the default 0x29
57 // until we finish setup, then re-address to final desired address.
58 uint8_t final_address = address_;
59 this->set_i2c_address(0x29);
60
61 reg(0x89) |= 0x01;
62 reg(0x88) = 0x00;
63
64 reg(0x80) = 0x01;
65 reg(0xFF) = 0x01;
66 reg(0x00) = 0x00;
67 this->stop_variable_ = reg(0x91).get();
68
69 reg(0x00) = 0x01;
70 reg(0xFF) = 0x00;
71 reg(0x80) = 0x00;
72 reg(0x60) |= 0x12;
73 if (this->long_range_)
74 this->signal_rate_limit_ = 0.1;
75 auto rate_value = static_cast<uint16_t>(signal_rate_limit_ * 128);
76 write_byte_16(0x44, rate_value);
77
78 reg(0x01) = 0xFF;
79
80 // getSpadInfo()
81 reg(0x80) = 0x01;
82 reg(0xFF) = 0x01;
83 reg(0x00) = 0x00;
84 reg(0xFF) = 0x06;
85 reg(0x83) |= 0x04;
86 reg(0xFF) = 0x07;
87 reg(0x81) = 0x01;
88 reg(0x80) = 0x01;
89 reg(0x94) = 0x6B;
90 reg(0x83) = 0x00;
91
92 uint32_t timeout_start_us = micros();
93 while (reg(0x83).get() == 0x00) {
94 if (this->timeout_us_ > 0 && (micros() - timeout_start_us > this->timeout_us_)) {
95 ESP_LOGE(TAG, "'%s' - setup timeout", LOG_STR_ARG(this->get_log_name()));
96 this->mark_failed();
97 return;
98 }
99 yield();
100 }
101
102 reg(0x83) = 0x01;
103 uint8_t tmp = reg(0x92).get();
104 uint8_t spad_count = tmp & 0x7F;
105 bool spad_type_is_aperture = tmp & 0x80;
106
107 reg(0x81) = 0x00;
108 reg(0xFF) = 0x06;
109 reg(0x83) &= ~0x04;
110 reg(0xFF) = 0x01;
111 reg(0x00) = 0x01;
112 reg(0xFF) = 0x00;
113 reg(0x80) = 0x00;
114
115 uint8_t ref_spad_map[6] = {};
116 this->read_bytes(0xB0, ref_spad_map, 6);
117
118 reg(0xFF) = 0x01;
119 reg(0x4F) = 0x00;
120 reg(0x4E) = 0x2C;
121 reg(0xFF) = 0x00;
122 reg(0xB6) = 0xB4;
123
124 uint8_t first_spad_to_enable = spad_type_is_aperture ? 12 : 0;
125 uint8_t spads_enabled = 0;
126 for (int i = 0; i < 48; i++) {
127 uint8_t &val = ref_spad_map[i / 8];
128 uint8_t mask = 1 << (i % 8);
129
130 if (i < first_spad_to_enable || spads_enabled == spad_count) {
131 val &= ~mask;
132 } else if (val & mask) {
133 spads_enabled += 1;
134 }
135 }
136
137 this->write_bytes(0xB0, ref_spad_map, 6);
138
139 reg(0xFF) = 0x01;
140 reg(0x00) = 0x00;
141 reg(0xFF) = 0x00;
142 reg(0x09) = 0x00;
143 reg(0x10) = 0x00;
144 reg(0x11) = 0x00;
145 reg(0x24) = 0x01;
146 reg(0x25) = 0xFF;
147 reg(0x75) = 0x00;
148 reg(0xFF) = 0x01;
149 reg(0x4E) = 0x2C;
150 reg(0x48) = 0x00;
151 reg(0x30) = 0x20;
152 reg(0xFF) = 0x00;
153 if (this->long_range_) {
154 reg(0x30) = 0x07; // WAS 0x09
155 } else {
156 reg(0x30) = 0x09;
157 }
158 reg(0x54) = 0x00;
159 reg(0x31) = 0x04;
160 reg(0x32) = 0x03;
161 reg(0x40) = 0x83;
162 reg(0x46) = 0x25;
163 reg(0x60) = 0x00;
164 reg(0x27) = 0x00;
165 reg(0x50) = 0x06;
166 reg(0x51) = 0x00;
167 reg(0x52) = 0x96;
168 reg(0x56) = 0x08;
169 if (this->long_range_) {
170 reg(0x57) = 0x50; // was 0x30
171 } else {
172 reg(0x57) = 0x30;
173 }
174 reg(0x61) = 0x00;
175 reg(0x62) = 0x00;
176 reg(0x64) = 0x00;
177 reg(0x65) = 0x00;
178 reg(0x66) = 0xA0;
179 reg(0xFF) = 0x01;
180 reg(0x22) = 0x32;
181 reg(0x47) = 0x14;
182 reg(0x49) = 0xFF;
183 reg(0x4A) = 0x00;
184 reg(0xFF) = 0x00;
185 reg(0x7A) = 0x0A;
186 reg(0x7B) = 0x00;
187 reg(0x78) = 0x21;
188 reg(0xFF) = 0x01;
189 reg(0x23) = 0x34;
190 reg(0x42) = 0x00;
191 reg(0x44) = 0xFF;
192 reg(0x45) = 0x26;
193 reg(0x46) = 0x05;
194 reg(0x40) = 0x40;
195 reg(0x0E) = 0x06;
196 reg(0x20) = 0x1A;
197 reg(0x43) = 0x40;
198 reg(0xFF) = 0x00;
199 reg(0x34) = 0x03;
200 reg(0x35) = 0x44;
201 reg(0xFF) = 0x01;
202 reg(0x31) = 0x04;
203 reg(0x4B) = 0x09;
204 reg(0x4C) = 0x05;
205 reg(0x4D) = 0x04;
206 reg(0xFF) = 0x00;
207 reg(0x44) = 0x00;
208 reg(0x45) = 0x20;
209 reg(0x47) = 0x08;
210 if (this->long_range_) {
211 reg(0x48) = 0x48; // was 0x28
212 } else {
213 reg(0x48) = 0x28;
214 }
215 reg(0x67) = 0x00;
216 reg(0x70) = 0x04;
217 reg(0x71) = 0x01;
218 reg(0x72) = 0xFE;
219 reg(0x76) = 0x00;
220 reg(0x77) = 0x00;
221 reg(0xFF) = 0x01;
222 reg(0x0D) = 0x01;
223 reg(0xFF) = 0x00;
224 reg(0x80) = 0x01;
225 reg(0x01) = 0xF8;
226 reg(0xFF) = 0x01;
227 reg(0x8E) = 0x01;
228 reg(0x00) = 0x01;
229 reg(0xFF) = 0x00;
230 reg(0x80) = 0x00;
231
232 reg(0x0A) = 0x04;
233 reg(0x84) &= ~0x10;
234 reg(0x0B) = 0x01;
235
236 if (this->measurement_timing_budget_us_ == 0) {
238 }
239
240 reg(0x01) = 0xE8;
242 reg(0x01) = 0x01;
243
245 ESP_LOGW(TAG, "1st reference calibration failed!");
246 this->mark_failed();
247 return;
248 }
249 reg(0x01) = 0x02;
251 ESP_LOGW(TAG, "2nd reference calibration failed!");
252 this->mark_failed();
253 return;
254 }
255 reg(0x01) = 0xE8;
256
257 // Set the sensor to the desired final address
258 // The following is different for VL53L0X vs VL53L1X
259 // I2C_SXXXX_DEVICE_ADDRESS = 0x8A for VL53L0X
260 // I2C_SXXXX__DEVICE_ADDRESS = 0x0001 for VL53L1X
261 reg(0x8A) = final_address & 0x7F;
262 this->set_i2c_address(final_address);
263}
264
266 if (this->initiated_read_ || this->waiting_for_interrupt_) {
267 this->publish_state(NAN);
268 this->status_momentary_warning(5000);
269 ESP_LOGW(TAG, "%s - update called before prior reading complete - initiated:%d waiting_for_interrupt:%d",
270 LOG_STR_ARG(this->get_log_name()), this->initiated_read_, this->waiting_for_interrupt_);
271 return;
272 }
273
274 // initiate single shot measurement
275 reg(0x80) = 0x01;
276 reg(0xFF) = 0x01;
277
278 reg(0x00) = 0x00;
279 reg(0x91) = this->stop_variable_;
280 reg(0x00) = 0x01;
281 reg(0xFF) = 0x00;
282 reg(0x80) = 0x00;
283
284 reg(0x00) = 0x01;
285 this->waiting_for_interrupt_ = false;
286 this->initiated_read_ = true;
287 // wait for timeout
288}
289
291 if (this->initiated_read_) {
292 if (reg(0x00).get() & 0x01) {
293 // waiting
294 } else {
295 // done
296 // wait until reg(0x13) & 0x07 is set
297 this->initiated_read_ = false;
298 this->waiting_for_interrupt_ = true;
299 }
300 }
301 if (this->waiting_for_interrupt_) {
302 if (reg(0x13).get() & 0x07) {
303 uint16_t range_mm = 0;
304 this->read_byte_16(0x14 + 10, &range_mm);
305 reg(0x0B) = 0x01;
306 this->waiting_for_interrupt_ = false;
307
308 if (range_mm >= 8190) {
309 ESP_LOGD(TAG, "'%s' - Distance is out of range, please move the target closer",
310 LOG_STR_ARG(this->get_log_name()));
311 this->publish_state(NAN);
312 return;
313 }
314
315 float range_m = range_mm / 1e3f;
316 ESP_LOGD(TAG, "'%s' - Got distance %.3f m", LOG_STR_ARG(this->get_log_name()), range_m);
317 this->publish_state(range_m);
318 }
319 }
320}
321
323 SequenceStepEnables enables{};
324 SequenceStepTimeouts timeouts{};
325
326 uint16_t start_overhead = 1910;
327 uint16_t end_overhead = 960;
328 uint16_t msrc_overhead = 660;
329 uint16_t tcc_overhead = 590;
330 uint16_t dss_overhead = 690;
331 uint16_t pre_range_overhead = 660;
332 uint16_t final_range_overhead = 550;
333
334 // "Start and end overhead times always present"
335 uint32_t budget_us = start_overhead + end_overhead;
336
338 get_sequence_step_timeouts_(&enables, &timeouts);
339
340 if (enables.tcc)
341 budget_us += (timeouts.msrc_dss_tcc_us + tcc_overhead);
342
343 if (enables.dss) {
344 budget_us += 2 * (timeouts.msrc_dss_tcc_us + dss_overhead);
345 } else if (enables.msrc) {
346 budget_us += (timeouts.msrc_dss_tcc_us + msrc_overhead);
347 }
348
349 if (enables.pre_range)
350 budget_us += (timeouts.pre_range_us + pre_range_overhead);
351
352 if (enables.final_range)
353 budget_us += (timeouts.final_range_us + final_range_overhead);
354
355 measurement_timing_budget_us_ = budget_us; // store for internal reuse
356 return budget_us;
357}
358
360 SequenceStepEnables enables{};
361 SequenceStepTimeouts timeouts{};
362
363 uint16_t start_overhead = 1320; // note that this is different than the value in get_
364 uint16_t end_overhead = 960;
365 uint16_t msrc_overhead = 660;
366 uint16_t tcc_overhead = 590;
367 uint16_t dss_overhead = 690;
368 uint16_t pre_range_overhead = 660;
369 uint16_t final_range_overhead = 550;
370
371 uint32_t min_timing_budget = 20000;
372
373 if (budget_us < min_timing_budget) {
374 return false;
375 }
376
377 uint32_t used_budget_us = start_overhead + end_overhead;
378
380 get_sequence_step_timeouts_(&enables, &timeouts);
381
382 if (enables.tcc) {
383 used_budget_us += (timeouts.msrc_dss_tcc_us + tcc_overhead);
384 }
385
386 if (enables.dss) {
387 used_budget_us += 2 * (timeouts.msrc_dss_tcc_us + dss_overhead);
388 } else if (enables.msrc) {
389 used_budget_us += (timeouts.msrc_dss_tcc_us + msrc_overhead);
390 }
391
392 if (enables.pre_range) {
393 used_budget_us += (timeouts.pre_range_us + pre_range_overhead);
394 }
395
396 if (enables.final_range) {
397 used_budget_us += final_range_overhead;
398
399 // "Note that the final range timeout is determined by the timing
400 // budget and the sum of all other timeouts within the sequence.
401 // If there is no room for the final range timeout, then an error
402 // will be set. Otherwise the remaining time will be applied to
403 // the final range."
404
405 if (used_budget_us > budget_us) {
406 // "Requested timeout too big."
407 return false;
408 }
409
410 uint32_t final_range_timeout_us = budget_us - used_budget_us;
411
412 // set_sequence_step_timeout() begin
413 // (SequenceStepId == VL53L0X_SEQUENCESTEP_FINAL_RANGE)
414
415 // "For the final range timeout, the pre-range timeout
416 // must be added. To do this both final and pre-range
417 // timeouts must be expressed in macro periods MClks
418 // because they have different vcsel periods."
419
420 uint16_t final_range_timeout_mclks =
421 timeout_microseconds_to_mclks_(final_range_timeout_us, timeouts.final_range_vcsel_period_pclks);
422
423 if (enables.pre_range) {
424 final_range_timeout_mclks += timeouts.pre_range_mclks;
425 }
426
427 write_byte_16(0x71, encode_timeout_(final_range_timeout_mclks));
428
429 // set_sequence_step_timeout() end
430
431 measurement_timing_budget_us_ = budget_us; // store for internal reuse
432 }
433 return true;
434}
435
437 uint8_t sequence_config = reg(0x01).get();
438 enables->tcc = (sequence_config >> 4) & 0x1;
439 enables->dss = (sequence_config >> 3) & 0x1;
440 enables->msrc = (sequence_config >> 2) & 0x1;
441 enables->pre_range = (sequence_config >> 6) & 0x1;
442 enables->final_range = (sequence_config >> 7) & 0x1;
443}
444
447
448 timeouts->msrc_dss_tcc_mclks = reg(0x46).get() + 1;
449 timeouts->msrc_dss_tcc_us =
451
452 uint16_t value;
453 read_byte_16(0x51, &value);
454 timeouts->pre_range_mclks = decode_timeout_(value);
455 timeouts->pre_range_us =
457
459
460 read_byte_16(0x71, &value);
461 timeouts->final_range_mclks = decode_timeout_(value);
462
463 if (enables->pre_range) {
464 timeouts->final_range_mclks -= timeouts->pre_range_mclks;
465 }
466
467 timeouts->final_range_us =
469}
470
472 uint8_t vcsel;
474 vcsel = reg(0x50).get();
475 } else if (type == VCSEL_PERIOD_FINAL_RANGE) {
476 vcsel = reg(0x70).get();
477 } else {
478 return 255;
479 }
480
481 return (vcsel + 1) << 1;
482}
483
484uint32_t VL53L0XSensor::get_macro_period_(uint8_t vcsel_period_pclks) {
485 return ((2304UL * vcsel_period_pclks * 1655UL) + 500UL) / 1000UL;
486}
487
488uint32_t VL53L0XSensor::timeout_mclks_to_microseconds_(uint16_t timeout_period_mclks, uint8_t vcsel_period_pclks) {
489 uint32_t macro_period_ns = get_macro_period_(vcsel_period_pclks);
490 return ((timeout_period_mclks * macro_period_ns) + (macro_period_ns / 2)) / 1000;
491}
492
493uint32_t VL53L0XSensor::timeout_microseconds_to_mclks_(uint32_t timeout_period_us, uint8_t vcsel_period_pclks) {
494 uint32_t macro_period_ns = get_macro_period_(vcsel_period_pclks);
495 return (((timeout_period_us * 1000) + (macro_period_ns / 2)) / macro_period_ns);
496}
497
498uint16_t VL53L0XSensor::decode_timeout_(uint16_t reg_val) {
499 // format: "(LSByte * 2^MSByte) + 1"
500 uint8_t msb = (reg_val >> 8) & 0xFF;
501 uint8_t lsb = (reg_val >> 0) & 0xFF;
502 return (uint16_t(lsb) << msb) + 1;
503}
504
505uint16_t VL53L0XSensor::encode_timeout_(uint16_t timeout_mclks) {
506 // format: "(LSByte * 2^MSByte) + 1"
507 uint32_t ls_byte = 0;
508 uint16_t ms_byte = 0;
509
510 if (timeout_mclks <= 0)
511 return 0;
512
513 ls_byte = timeout_mclks - 1;
514
515 while ((ls_byte & 0xFFFFFF00) > 0) {
516 ls_byte >>= 1;
517 ms_byte++;
518 }
519
520 return (ms_byte << 8) | (ls_byte & 0xFF);
521}
522
524 reg(0x00) = 0x01 | vhv_init_byte; // VL53L0X_REG_SYSRANGE_MODE_START_STOP
525
526 uint32_t start = millis();
527 while ((reg(0x13).get() & 0x07) == 0) {
528 if (millis() - start > 1000)
529 return false;
530 yield();
531 }
532
533 reg(0x0B) = 0x01;
534 reg(0x00) = 0x00;
535
536 return true;
537}
538
539} // namespace esphome::vl53l0x
void mark_failed()
Mark this component as failed.
void status_momentary_warning(uint32_t length=5000)
Set warning status flag and clear it after length ms.
const LogString * get_log_name() const
Get the name for a "%s" log argument (LOG_STR_ARG).
virtual void digital_write(bool value)=0
uint8_t address_
store the address of the device on the bus
Definition i2c.h:327
bool read_byte_16(uint8_t a_register, uint16_t *data)
Definition i2c.h:281
I2CRegister reg(uint8_t a_register)
calls the I2CRegister constructor
Definition i2c.h:164
void set_i2c_address(uint8_t address)
We store the address of the device on the bus.
Definition i2c.h:151
bool write_bytes(uint8_t a_register, const uint8_t *data, uint8_t len) const
Definition i2c.h:298
bool write_byte_16(uint8_t a_register, uint16_t data) const
Definition i2c.cpp:101
bool read_bytes(uint8_t a_register, uint8_t *data, uint8_t len)
Compat APIs All methods below have been added for compatibility reasons.
Definition i2c.h:249
uint8_t get() const
returns the register value
Definition i2c.cpp:126
void publish_state(float state)
Publish a new state to the front-end.
Definition sensor.cpp:65
uint32_t timeout_mclks_to_microseconds_(uint16_t timeout_period_mclks, uint8_t vcsel_period_pclks)
void get_sequence_step_enables_(SequenceStepEnables *enables)
bool perform_single_ref_calibration_(uint8_t vhv_init_byte)
uint16_t decode_timeout_(uint16_t reg_val)
uint32_t timeout_microseconds_to_mclks_(uint32_t timeout_period_us, uint8_t vcsel_period_pclks)
static std::list< VL53L0XSensor * > vl53_sensors
void get_sequence_step_timeouts_(SequenceStepEnables const *enables, SequenceStepTimeouts *timeouts)
bool set_measurement_timing_budget_(uint32_t budget_us)
uint32_t get_macro_period_(uint8_t vcsel_period_pclks)
uint8_t get_vcsel_pulse_period_(VcselPeriodType type)
uint16_t encode_timeout_(uint16_t timeout_mclks)
void yield(void)
uint8_t type
mopeka_std_values val[3]
ESPHOME_LOG_TAG(TAG, "vl53l0x")
void IRAM_ATTR HOT delayMicroseconds(uint32_t us)
Definition hal.cpp:48
uint32_t IRAM_ATTR HOT micros()
Definition hal.cpp:43
uint32_t IRAM_ATTR HOT millis()
Definition hal.cpp:25
static void uint32_t