ESPHome 2026.5.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
15namespace esphome {
16namespace vl53l0x {
17
18static const char *const TAG = "vl53l0x";
19
20std::list<VL53L0XSensor *> VL53L0XSensor::vl53_sensors; // NOLINT(cppcoreguidelines-avoid-non-const-global-variables)
21bool VL53L0XSensor::enable_pin_setup_complete = false; // NOLINT(cppcoreguidelines-avoid-non-const-global-variables)
22
24
26 LOG_SENSOR("", "VL53L0X", this);
27 LOG_UPDATE_INTERVAL(this);
28 LOG_I2C_DEVICE(this);
29 if (this->enable_pin_ != nullptr) {
30 LOG_PIN(" Enable Pin: ", this->enable_pin_);
31 }
32 ESP_LOGCONFIG(TAG,
33 " Timeout: %" PRIu32 "%s\n"
34 " Timing Budget %" PRIu32 "us ",
35 this->timeout_us_, this->timeout_us_ > 0 ? "us" : " (no timeout)", this->measurement_timing_budget_us_);
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", this->name_.c_str());
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("update", 5000);
269 ESP_LOGW(TAG, "%s - update called before prior reading complete - initiated:%d waiting_for_interrupt:%d",
270 this->name_.c_str(), 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", this->name_.c_str());
310 this->publish_state(NAN);
311 return;
312 }
313
314 float range_m = range_mm / 1e3f;
315 ESP_LOGD(TAG, "'%s' - Got distance %.3f m", this->name_.c_str(), range_m);
316 this->publish_state(range_m);
317 }
318 }
319}
320
322 SequenceStepEnables enables{};
323 SequenceStepTimeouts timeouts{};
324
325 uint16_t start_overhead = 1910;
326 uint16_t end_overhead = 960;
327 uint16_t msrc_overhead = 660;
328 uint16_t tcc_overhead = 590;
329 uint16_t dss_overhead = 690;
330 uint16_t pre_range_overhead = 660;
331 uint16_t final_range_overhead = 550;
332
333 // "Start and end overhead times always present"
334 uint32_t budget_us = start_overhead + end_overhead;
335
337 get_sequence_step_timeouts_(&enables, &timeouts);
338
339 if (enables.tcc)
340 budget_us += (timeouts.msrc_dss_tcc_us + tcc_overhead);
341
342 if (enables.dss) {
343 budget_us += 2 * (timeouts.msrc_dss_tcc_us + dss_overhead);
344 } else if (enables.msrc) {
345 budget_us += (timeouts.msrc_dss_tcc_us + msrc_overhead);
346 }
347
348 if (enables.pre_range)
349 budget_us += (timeouts.pre_range_us + pre_range_overhead);
350
351 if (enables.final_range)
352 budget_us += (timeouts.final_range_us + final_range_overhead);
353
354 measurement_timing_budget_us_ = budget_us; // store for internal reuse
355 return budget_us;
356}
357
359 SequenceStepEnables enables{};
360 SequenceStepTimeouts timeouts{};
361
362 uint16_t start_overhead = 1320; // note that this is different than the value in get_
363 uint16_t end_overhead = 960;
364 uint16_t msrc_overhead = 660;
365 uint16_t tcc_overhead = 590;
366 uint16_t dss_overhead = 690;
367 uint16_t pre_range_overhead = 660;
368 uint16_t final_range_overhead = 550;
369
370 uint32_t min_timing_budget = 20000;
371
372 if (budget_us < min_timing_budget) {
373 return false;
374 }
375
376 uint32_t used_budget_us = start_overhead + end_overhead;
377
379 get_sequence_step_timeouts_(&enables, &timeouts);
380
381 if (enables.tcc) {
382 used_budget_us += (timeouts.msrc_dss_tcc_us + tcc_overhead);
383 }
384
385 if (enables.dss) {
386 used_budget_us += 2 * (timeouts.msrc_dss_tcc_us + dss_overhead);
387 } else if (enables.msrc) {
388 used_budget_us += (timeouts.msrc_dss_tcc_us + msrc_overhead);
389 }
390
391 if (enables.pre_range) {
392 used_budget_us += (timeouts.pre_range_us + pre_range_overhead);
393 }
394
395 if (enables.final_range) {
396 used_budget_us += final_range_overhead;
397
398 // "Note that the final range timeout is determined by the timing
399 // budget and the sum of all other timeouts within the sequence.
400 // If there is no room for the final range timeout, then an error
401 // will be set. Otherwise the remaining time will be applied to
402 // the final range."
403
404 if (used_budget_us > budget_us) {
405 // "Requested timeout too big."
406 return false;
407 }
408
409 uint32_t final_range_timeout_us = budget_us - used_budget_us;
410
411 // set_sequence_step_timeout() begin
412 // (SequenceStepId == VL53L0X_SEQUENCESTEP_FINAL_RANGE)
413
414 // "For the final range timeout, the pre-range timeout
415 // must be added. To do this both final and pre-range
416 // timeouts must be expressed in macro periods MClks
417 // because they have different vcsel periods."
418
419 uint16_t final_range_timeout_mclks =
420 timeout_microseconds_to_mclks_(final_range_timeout_us, timeouts.final_range_vcsel_period_pclks);
421
422 if (enables.pre_range) {
423 final_range_timeout_mclks += timeouts.pre_range_mclks;
424 }
425
426 write_byte_16(0x71, encode_timeout_(final_range_timeout_mclks));
427
428 // set_sequence_step_timeout() end
429
430 measurement_timing_budget_us_ = budget_us; // store for internal reuse
431 }
432 return true;
433}
434
436 uint8_t sequence_config = reg(0x01).get();
437 enables->tcc = (sequence_config >> 4) & 0x1;
438 enables->dss = (sequence_config >> 3) & 0x1;
439 enables->msrc = (sequence_config >> 2) & 0x1;
440 enables->pre_range = (sequence_config >> 6) & 0x1;
441 enables->final_range = (sequence_config >> 7) & 0x1;
442}
443
446
447 timeouts->msrc_dss_tcc_mclks = reg(0x46).get() + 1;
448 timeouts->msrc_dss_tcc_us =
450
451 uint16_t value;
452 read_byte_16(0x51, &value);
453 timeouts->pre_range_mclks = decode_timeout_(value);
454 timeouts->pre_range_us =
456
458
459 read_byte_16(0x71, &value);
460 timeouts->final_range_mclks = decode_timeout_(value);
461
462 if (enables->pre_range) {
463 timeouts->final_range_mclks -= timeouts->pre_range_mclks;
464 }
465
466 timeouts->final_range_us =
468}
469
471 uint8_t vcsel;
473 vcsel = reg(0x50).get();
474 } else if (type == VCSEL_PERIOD_FINAL_RANGE) {
475 vcsel = reg(0x70).get();
476 } else {
477 return 255;
478 }
479
480 return (vcsel + 1) << 1;
481}
482
483uint32_t VL53L0XSensor::get_macro_period_(uint8_t vcsel_period_pclks) {
484 return ((2304UL * vcsel_period_pclks * 1655UL) + 500UL) / 1000UL;
485}
486
487uint32_t VL53L0XSensor::timeout_mclks_to_microseconds_(uint16_t timeout_period_mclks, uint8_t vcsel_period_pclks) {
488 uint32_t macro_period_ns = get_macro_period_(vcsel_period_pclks);
489 return ((timeout_period_mclks * macro_period_ns) + (macro_period_ns / 2)) / 1000;
490}
491
492uint32_t VL53L0XSensor::timeout_microseconds_to_mclks_(uint32_t timeout_period_us, uint8_t vcsel_period_pclks) {
493 uint32_t macro_period_ns = get_macro_period_(vcsel_period_pclks);
494 return (((timeout_period_us * 1000) + (macro_period_ns / 2)) / macro_period_ns);
495}
496
497uint16_t VL53L0XSensor::decode_timeout_(uint16_t reg_val) {
498 // format: "(LSByte * 2^MSByte) + 1"
499 uint8_t msb = (reg_val >> 8) & 0xFF;
500 uint8_t lsb = (reg_val >> 0) & 0xFF;
501 return (uint16_t(lsb) << msb) + 1;
502}
503
504uint16_t VL53L0XSensor::encode_timeout_(uint16_t timeout_mclks) {
505 // format: "(LSByte * 2^MSByte) + 1"
506 uint32_t ls_byte = 0;
507 uint16_t ms_byte = 0;
508
509 if (timeout_mclks <= 0)
510 return 0;
511
512 ls_byte = timeout_mclks - 1;
513
514 while ((ls_byte & 0xFFFFFF00) > 0) {
515 ls_byte >>= 1;
516 ms_byte++;
517 }
518
519 return (ms_byte << 8) | (ls_byte & 0xFF);
520}
521
523 reg(0x00) = 0x01 | vhv_init_byte; // VL53L0X_REG_SYSRANGE_MODE_START_STOP
524
525 uint32_t start = millis();
526 while ((reg(0x13).get() & 0x07) == 0) {
527 if (millis() - start > 1000)
528 return false;
529 yield();
530 }
531
532 reg(0x0B) = 0x01;
533 reg(0x00) = 0x00;
534
535 return true;
536}
537
538} // namespace vl53l0x
539} // namespace esphome
void mark_failed()
Mark this component as failed.
void status_momentary_warning(const char *name, uint32_t length=5000)
Set warning status flag and automatically clear it after a timeout.
virtual void digital_write(bool value)=0
constexpr const char * c_str() const
Definition string_ref.h:73
uint8_t address_
store the address of the device on the bus
Definition i2c.h:270
bool read_byte_16(uint8_t a_register, uint16_t *data)
Definition i2c.h:249
I2CRegister reg(uint8_t a_register)
calls the I2CRegister constructor
Definition i2c.h:152
void set_i2c_address(uint8_t address)
We store the address of the device on the bus.
Definition i2c.h:139
bool write_bytes(uint8_t a_register, const uint8_t *data, uint8_t len) const
Definition i2c.h:251
bool write_byte_16(uint8_t a_register, uint16_t data) const
Definition i2c.h:267
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:217
uint8_t get() const
returns the register value
Definition i2c.cpp:84
void publish_state(float state)
Publish a new state to the front-end.
Definition sensor.cpp:68
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)
uint16_t type
mopeka_std_values val[3]
Providing packet encoding functions for exchanging data with a remote host.
Definition a01nyub.cpp:7
void IRAM_ATTR HOT delayMicroseconds(uint32_t us)
Definition core.cpp:30
void HOT yield()
Definition core.cpp:25
uint32_t IRAM_ATTR HOT micros()
Definition core.cpp:29
uint32_t IRAM_ATTR HOT millis()
Definition core.cpp:26
static void uint32_t