cancel
Showing results for 
Search instead for 
Did you mean: 

PerformRefSpadManagement doesn't work

IHern.2
Associate III

This is the detailed log of the I2C communication between an ESP32 and a VL53L4CX

[I][logger:242]: Log initialized
[VV][esp32.preferences:070]: nvs_get_blob: key: 233825507, len: 4
[C][ota:469]: There have been 0 suspected unsuccessful boot attempts.
[VV][esp32.preferences:039]: s_pending_save: key: 233825507, len: 4
[D][esp32.preferences:113]: Saving 1 preferences to flash...
[VV][esp32.preferences:122]: Checking if NVS data 233825507 has changed
[V][esp32.preferences:125]: sync: key: 233825507, len: 4
[D][esp32.preferences:142]: Saving 1 preferences to flash: 0 cached, 1 written, 0 failed
[I][app:029]: Running through setup()...
[V][app:030]: Sorting components by setup priority...
[I][i2c.arduino:161]: Performing I2C bus recovery
[V][esp32-hal-i2c.c:1492] i2cInit(): num=0 sda=21 scl=22 freq=0
[V][esp32-hal-i2c.c:1688] i2cSetFrequency(): freq=100000Hz
[V][esp32-hal-i2c.c:1704] i2cSetFrequency(): cpu Freq=240Mhz, i2c Freq=100000Hz
[V][esp32-hal-i2c.c:1712] i2cSetFrequency(): Fifo delta=1
[V][esp32-hal-i2c.c:1688] i2cSetFrequency(): freq=200000Hz
[V][esp32-hal-i2c.c:1704] i2cSetFrequency(): cpu Freq=240Mhz, i2c Freq=200000Hz
[V][esp32-hal-i2c.c:1712] i2cSetFrequency(): Fifo delta=2
[VV][scheduler:057]: set_interval(name='', interval=60000, offset=4566)
[V][vl53l4cx:048]: 'Distance' - setup BEGIN
[VV][vl53l4cx:058]: 'Distance' - setup I2CDevice
[VV][vl53l4cx:062]: 'Distance' - setup InitSensor 0x52
[VV][i2c.arduino:126]: 0x29 TX 0001
[VV][i2c.arduino:126]: 0x29 TX 29
[VV][i2c.arduino:126]: 0x29 TX 00E5
[VV][i2c.arduino:102]: 0x29 RX 03
[VV][i2c.arduino:126]: 0x29 TX 0001
[VV][i2c.arduino:102]: 0x29 RX 290210002DBCCC81800798
[VV][i2c.arduino:126]: 0x29 TX 000D
[VV][i2c.arduino:102]: 0x29 RX FFF9DFF7FB0F000D0200000000000000001FF500000000
[VV][i2c.arduino:126]: 0x29 TX 010F
[VV][i2c.arduino:102]: 0x29 RX EBAA10FFFE2D04063C3F0100011406FFFFFFFFEFFF9FFCFFEDFEFFCBFDFFFFEFFFFEFFF7FFBEDFFFFEFFFFF7FFDFFFCF55
[VV][i2c.arduino:126]: 0x29 TX 0085
...

The entire transfer is too large for a message so I cut to keep only the interesting parts:

[VV][vl53l4cx:070]: 'Distance' - setup GetSensorId
[VV][i2c.arduino:126]: 0x29 TX 010F
[VV][i2c.arduino:102]: 0x29 RX EBAA
[VV][vl53l4cx:078]: 'Distance' - setup GetUID
[VV][i2c.arduino:126]: 0x29 TX 0085
[VV][i2c.arduino:126]: 0x29 TX 00
[VV][i2c.arduino:126]: 0x29 TX 0083
[VV][i2c.arduino:126]: 0x29 TX 01
[VV][i2c.arduino:126]: 0x29 TX 0781
[VV][i2c.arduino:126]: 0x29 TX 01
[VV][i2c.arduino:126]: 0x29 TX 0683
[VV][i2c.arduino:126]: 0x29 TX 05
[VV][i2c.arduino:126]: 0x29 TX 0780
[VV][i2c.arduino:126]: 0x29 TX 01
[VV][i2c.arduino:126]: 0x29 TX 0784
[VV][i2c.arduino:126]: 0x29 TX 0004
[VV][i2c.arduino:126]: 0x29 TX 0794
[VV][i2c.arduino:126]: 0x29 TX 7E
[VV][i2c.arduino:126]: 0x29 TX 0783
[VV][i2c.arduino:126]: 0x29 TX 00
[VV][i2c.arduino:126]: 0x29 TX 0783
[VV][i2c.arduino:126]: 0x29 TX 01
[VV][i2c.arduino:126]: 0x29 TX 0790
[VV][i2c.arduino:102]: 0x29 RX 00000000
[VV][i2c.arduino:126]: 0x29 TX 0794
[VV][i2c.arduino:126]: 0x29 TX 7F
[VV][i2c.arduino:126]: 0x29 TX 0783
[VV][i2c.arduino:126]: 0x29 TX 00
[VV][i2c.arduino:126]: 0x29 TX 0783
[VV][i2c.arduino:126]: 0x29 TX 01
[VV][i2c.arduino:126]: 0x29 TX 0790
[VV][i2c.arduino:102]: 0x29 RX 00000000
[VV][i2c.arduino:126]: 0x29 TX 0783
[VV][i2c.arduino:126]: 0x29 TX 01
[VV][i2c.arduino:126]: 0x29 TX 0781
[VV][i2c.arduino:126]: 0x29 TX 00
[VV][i2c.arduino:126]: 0x29 TX 0083
[VV][i2c.arduino:126]: 0x29 TX 00
[VV][i2c.arduino:126]: 0x29 TX 0085
[VV][i2c.arduino:126]: 0x29 TX 01
[VV][vl53l4cx:087]: 'Distance' - PerformRefSpadManagement
[VV][i2c.arduino:126]: 0x29 TX 0083
[VV][i2c.arduino:126]: 0x29 TX 01
[VV][i2c.arduino:126]: 0x29 TX 004B
[VV][i2c.arduino:126]: 0x29 TX 0E
[VV][i2c.arduino:126]: 0x29 TX 0060
[VV][i2c.arduino:126]: 0x29 TX 0B
[VV][i2c.arduino:126]: 0x29 TX 0078
[VV][i2c.arduino:126]: 0x29 TX 0B0B
[VV][i2c.arduino:126]: 0x29 TX 001C
[VV][i2c.arduino:126]: 0x29 TX 0A00
[VV][i2c.arduino:126]: 0x29 TX 0064
[VV][i2c.arduino:126]: 0x29 TX 1400
[VV][i2c.arduino:126]: 0x29 TX 0066
[VV][i2c.arduino:126]: 0x29 TX 0500
[VV][i2c.arduino:126]: 0x29 TX 0030
[VV][i2c.arduino:102]: 0x29 RX 11
[VV][i2c.arduino:126]: 0x29 TX 0027
[VV][i2c.arduino:126]: 0x29 TX 08
[VV][i2c.arduino:126]: 0x29 TX 0031
[VV][i2c.arduino:102]: 0x29 RX 03

From this point there is an infinite loop

[VV][i2c.arduino:126]: 0x29 TX 0031
[VV][i2c.arduino:102]: 0x29 RX 03

And ESP WDT activates and cancel the process.

What could be wrong?

1 ACCEPTED SOLUTION

Accepted Solutions
IHern.2
Associate III

It took me long time to find the root of the problem. I just wanted to document here the root of all my problems just to help others with similar issues.

I'm using an ESP32 and the distance sensor driver was slightly modified to run in the esphome development environment. The problem was related with memory management of ESP32 in that particular scenario.

The driver instance class was created during the setup method. And I removed MyDevice from the source code and used only Dev (that was a pointer to MyDevice) of course, I was doing a malloc and free in order to reserve the required memory for the driver. But, that memory was reserved in the heap of the processor running the setup method. But, the loop method was running in the other processor with different heap so that caused plenty of issues when configuring the device.

I finally decided to allocate MyDevice as a static variable so it is allocated in a shared memory that can be used by either processor of the ESP32.

View solution in original post

8 REPLIES 8
John E KVAM
ST Employee

over the years I've seen issues like this on some particular sensors.

Turns out that in order to do the RefSpad managment, one needs to start of close, and let the code narrow in. But a few sensors seem to be so far off they cannot adjust.

The solution is to set the RefSpad to 3 PRIOR to doing the RefSpad calibration.

This sees to clear up the issue.

For code, simply add:

If refSpad failes - Set RefSpad to 3 and retry.

That should clear it up.

  • john

If this or any post solves your issue, please mark them as 'Accept as Solution' It really helps. And if you notice anything wrong do not hesitate to 'Report Inappropriate Content'. Someone will review it.
IHern.2
Associate III

Thank you for your response!

I'm digging into the code to see what can I do. Here is some feedback about the process.

  • refSpad enters in an infinite loop so it dones not "fail". Just the watch dog timer cancel the process and the ESP32 is restarted. As an "average user" I think the driver should be responsible of handling this situation. We "users" are calling calibration with no specific parameters initialized.
  • I've expanded the I2C communication between the ESP32 and the VL53L4CX to provide more information.

[VV][vl53l4cx:087]: 'Distance' - PerformRefSpadManagement
 
VL53L4CX_enable_powerforce
[VV][i2c.arduino:126]: 0x29 TX 0083
[VV][i2c.arduino:126]: 0x29 TX 01 <- constant, value copied to -> pdev->sys_ctrl.power_management__go1_power_force
 
VL53L4CX_set_ref_spad_char_config
[VV][i2c.arduino:126]: 0x29 TX 004B VL53L4CX_PHASECAL_CONFIG__TIMEOUT_MACROP
[VV][i2c.arduino:126]: 0x29 TX 0E <- derived from prefspadchar->timeout_us 
 
[VV][i2c.arduino:126]: 0x29 TX 0060 VL53L4CX_RANGE_CONFIG__VCSEL_PERIOD_A
[VV][i2c.arduino:126]: 0x29 TX 0B
[VV][i2c.arduino:126]: 0x29 TX 0078 VL53L4CX_SD_CONFIG__WOI_SD0
[VV][i2c.arduino:126]: 0x29 TX 0B0B
[VV][i2c.arduino:126]: 0x29 TX 001C VL53L4CX_REF_SPAD_CHAR__TOTAL_RATE_TARGET_MCPS
[VV][i2c.arduino:126]: 0x29 TX 0A00 
[VV][i2c.arduino:126]: 0x29 TX 0064 VL53L4CX_RANGE_CONFIG__SIGMA_THRESH
[VV][i2c.arduino:126]: 0x29 TX 1400
[VV][i2c.arduino:126]: 0x29 TX 0066 VL53L4CX_RANGE_CONFIG__MIN_COUNT_RATE_RTN_LIMIT_MCPS
[VV][i2c.arduino:126]: 0x29 TX 0500
 
VL53L4CX_run_device_test
[VV][i2c.arduino:126]: 0x29 TX 0030 VL53L4CX_GPIO_HV_MUX__CTRL
[VV][i2c.arduino:102]: 0x29 RX 11
 
VL53L4CX::VL53L4CX_start_test
[VV][i2c.arduino:126]: 0x29 TX 0027 VL53L4CX_TEST_MODE__CTRL
[VV][i2c.arduino:126]: 0x29 TX 08
 
VL53L4CX_wait_for_test_completion
[VV][i2c.arduino:126]: 0x29 TX 0031
[VV][i2c.arduino:102]: 0x29 RX 03

I realise, the call to VL53L4CX_run_device_test(Dev, prefspadchar->device_test_mode) requires device_test_mode to be set before and nothing in the code is providing a value here.

I've extracted the values of the prefspadchar struct prior to the first call to PerformRefSpadManagement

[VV][vl53l4cx:117]: 'Distance' - PerformRefSpadManagement
[VV][vl53l4cx:120]:    refspadchar_config {
[VV][vl53l4cx:121]:       device_test_mode 8,
[VV][vl53l4cx:122]:       timeout_us 1000,
[VV][vl53l4cx:123]:       target_count_rate_mcps 2560,
[VV][vl53l4cx:124]:       min_count_rate_limit_mcps 1280,
[VV][vl53l4cx:125]:       max_count_rate_limit_mcps 5120,
[VV][vl53l4cx:126]:    }

Now I've forced "device_test_mode" to 3 to see is this is what makes the difference.

[VV][i2c.arduino:140]: 0x29 TX 0083
[VV][i2c.arduino:140]: 0x29 TX 01
[VV][i2c.arduino:140]: 0x29 TX 004B
[VV][i2c.arduino:140]: 0x29 TX 0E
[VV][i2c.arduino:140]: 0x29 TX 0060
[VV][i2c.arduino:140]: 0x29 TX 0B
[VV][i2c.arduino:140]: 0x29 TX 0078
[VV][i2c.arduino:140]: 0x29 TX 0B0B
[VV][i2c.arduino:140]: 0x29 TX 001C
[VV][i2c.arduino:140]: 0x29 TX 0A00
[VV][i2c.arduino:140]: 0x29 TX 0064
[VV][i2c.arduino:140]: 0x29 TX 1400
[VV][i2c.arduino:140]: 0x29 TX 0066
[VV][i2c.arduino:140]: 0x29 TX 0500
[VV][i2c.arduino:140]: 0x29 TX 0030
[VV][i2c.arduino:116]: 0x29 RX 11
[VV][i2c.arduino:140]: 0x29 TX 0027
[VV][i2c.arduino:140]: 0x29 TX 03            <- This is the value of device_test_mode
[VV][i2c.arduino:140]: 0x29 TX 0031
[VV][i2c.arduino:116]: 0x29 RX 03
[VV][i2c.arduino:140]: 0x29 TX 0031
[VV][i2c.arduino:116]: 0x29 RX 03
[VV][i2c.arduino:140]: 0x29 TX 0031
[VV][i2c.arduino:116]: 0x29 RX 03
Loop...

The process enters the same infinite loop.

Ok, I'm going to submit this initial feedback and continue my tests to see if I can use this device.

Thanks again for your feedback

IHern.2
Associate III

I managed to change timeout to 10000 us instead of 1000us but the problem is the same

IHern.2
Associate III

I learn something.

I've added external pull-up resistors to SCL, SDA, GPIO and XSHUT pins on the ESP32 now I receive error -7 timed out

John E KVAM
ST Employee

if you think it's a hardware error, then simply comment out the refSpad and get to ranging. Without refspad, your answers will not be as accurate, but you want to prove the device ranges - for a long, long time.

If you can't range for hours and hours- then it's still a hardware error.

But if you can, then it's something with the refSpad.

But my guess is your I2C is unreliable.

You should be able to init the chip with a few dozen writes, and then set it to ranging.

If it hangs, check the I2C bus. when not in use they SCL and SDA should both be high. If one is stuck low, then you know you had a glitch.

  • john
  •  


If this or any post solves your issue, please mark them as 'Accept as Solution' It really helps. And if you notice anything wrong do not hesitate to 'Report Inappropriate Content'. Someone will review it.
IHern.2
Associate III

Thank you for your guidance,

> But my guess is your I2C is unreliable.

Yep. this is something possible as I modified that part of the code in order to use it from esphome IDE (that allows to choose between Arduino and ESP-IDF platform and my code is for Arduino at this time)

I've already ordered an Adafruit Metro M0 Express (will arrive on Wednesday) in order to run the examples using the driver without modifying them. If they run on that hardware, then it is for sure an I2C issue on the ESP32.

It is also a bit strange that reading the Device ID success but reading the UUID does not fail but returns 0x00.

[VV][vl53l4cx:070]: 'Distance' - setup GetSensorId
[VV][i2c.arduino:126]: 0x29 TX 010F
[VV][i2c.arduino:102]: 0x29 RX EBAA
[VV][vl53l4cx:078]: 'Distance' - setup GetUID
[VV][i2c.arduino:126]: 0x29 TX 0085
[VV][i2c.arduino:126]: 0x29 TX 00
[VV][i2c.arduino:126]: 0x29 TX 0083
[VV][i2c.arduino:126]: 0x29 TX 01
[VV][i2c.arduino:126]: 0x29 TX 0781
[VV][i2c.arduino:126]: 0x29 TX 01
[VV][i2c.arduino:126]: 0x29 TX 0683
[VV][i2c.arduino:126]: 0x29 TX 05
[VV][i2c.arduino:126]: 0x29 TX 0780
[VV][i2c.arduino:126]: 0x29 TX 01
[VV][i2c.arduino:126]: 0x29 TX 0784
[VV][i2c.arduino:126]: 0x29 TX 0004
[VV][i2c.arduino:126]: 0x29 TX 0794
[VV][i2c.arduino:126]: 0x29 TX 7E
[VV][i2c.arduino:126]: 0x29 TX 0783
[VV][i2c.arduino:126]: 0x29 TX 00
[VV][i2c.arduino:126]: 0x29 TX 0783
[VV][i2c.arduino:126]: 0x29 TX 01
[VV][i2c.arduino:126]: 0x29 TX 0790
[VV][i2c.arduino:102]: 0x29 RX 00000000
[VV][i2c.arduino:126]: 0x29 TX 0794
[VV][i2c.arduino:126]: 0x29 TX 7F
[VV][i2c.arduino:126]: 0x29 TX 0783
[VV][i2c.arduino:126]: 0x29 TX 00
[VV][i2c.arduino:126]: 0x29 TX 0783
[VV][i2c.arduino:126]: 0x29 TX 01
[VV][i2c.arduino:126]: 0x29 TX 0790
[VV][i2c.arduino:102]: 0x29 RX 00000000
[VV][i2c.arduino:126]: 0x29 TX 0783
[VV][i2c.arduino:126]: 0x29 TX 01
[VV][i2c.arduino:126]: 0x29 TX 0781
[VV][i2c.arduino:126]: 0x29 TX 00
[VV][i2c.arduino:126]: 0x29 TX 0083
[VV][i2c.arduino:126]: 0x29 TX 00
[VV][i2c.arduino:126]: 0x29 TX 0085
[VV][i2c.arduino:126]: 0x29 TX 01

Regarding the infinite loop and the -7 (timeout) error received It has to do with the value of pdev->wait_method when it is 1 (NON BLOCKING) an infinite loop is obtained. When it is 0 (BLOCKING) the -7 error is raised. In principle, the value of wait_method is established statically from the initialisation unless the value is later read from the device for some reason (I'm not sure if there is a test about the use of the GPIO0 that might be used for this purpose)

I'll also try to skip the refspad phase and see if I can range. When not in calibration mode, the device never returns a measure (valid or invalid). When using the GPIO0 interrupt, the ESP32 is never interrupted, when not using the GPIO0 Interrupt, the call to WaitMeasurementDataReady returns NO data ready.

I purchased two units for testing purposes and both of them raise the same issues.

I'll report back my findings

Again, Thanks for your support!

IHern.2
Associate III

The Adafruit Metro M0 Express arrived today and I tested the sensor with it and default libraries. Everything works OK and I receive measures on the serial output.

Tomorrow I'll continue exploring the possible reasons for the sensor not working on ESP32 using esphome.

IHern.2
Associate III

It took me long time to find the root of the problem. I just wanted to document here the root of all my problems just to help others with similar issues.

I'm using an ESP32 and the distance sensor driver was slightly modified to run in the esphome development environment. The problem was related with memory management of ESP32 in that particular scenario.

The driver instance class was created during the setup method. And I removed MyDevice from the source code and used only Dev (that was a pointer to MyDevice) of course, I was doing a malloc and free in order to reserve the required memory for the driver. But, that memory was reserved in the heap of the processor running the setup method. But, the loop method was running in the other processor with different heap so that caused plenty of issues when configuring the device.

I finally decided to allocate MyDevice as a static variable so it is allocated in a shared memory that can be used by either processor of the ESP32.