ESP32 read I2C speed increase

For general Flowcode discussion that does not belong in the other sections.
stefan.erni
Valued Contributor
Posts: 1012
http://meble-kuchenne.info.pl
Joined: Wed Dec 02, 2020 10:53 am
Has thanked: 193 times
Been thanked: 217 times

ESP32 read I2C speed increase

Post by stefan.erni »

Hi Ben

I am trying to sample an I2C IMU with 8Khz with an ESP32. I read just the Acceleration X,Y,Z axis
The I2C bus runs with 1Mega.
Generally it works for sampling.
But I can only use 2kHz. I looked at a sample and there is a long "Break" from 113uSec.
Can I make this break shorter somehow?

One IMU, One Sample:
2025-02-03_14-10-46.PNG
2025-02-03_14-10-46.PNG (256.36 KiB) Viewed 4657 times
Program part:
2025-02-03_14-30-05.PNG
2025-02-03_14-30-05.PNG (114.71 KiB) Viewed 4657 times
2025-02-03_14-31-40.PNG
2025-02-03_14-31-40.PNG (45.22 KiB) Viewed 4657 times

mnfisher
Valued Contributor
Posts: 1518
Joined: Wed Dec 09, 2020 9:37 pm
Has thanked: 138 times
Been thanked: 727 times

Re: ESP32 read I2C speed increase

Post by mnfisher »

How are you reading the sensor ? Can you post the code - the delay must be due to 'something' happening that is taking 112uS - so a print to UART / write to SD or somesuch - the reads should be continuous unless the task doing the read 'yields' (a delay - or call that does so (UART Prints do I think))) or is doing 113uS worth of work between reads?

I'm assuming ReadNRegisters - reads all registers correctly using in the 'bursts' on the trace - and the delay is between calls to this?

The i2c should run okay - unless the sensor is using clock-stretching to delay things (is it capable of being read at 2kHz or does it do some data processing ?)


Martin

stefan.erni
Valued Contributor
Posts: 1012
Joined: Wed Dec 02, 2020 10:53 am
Has thanked: 193 times
Been thanked: 217 times

Re: ESP32 read I2C speed increase

Post by stefan.erni »

Good idea Martin.
Instead of switching everything off, I made a new program
Now I have tested the senor in a small test program without Sd-card and uart and no IRQ.
It is the same.
123uSec
Then I tested other components and there is also a waiting time. This is different depending on the component, but between 69uSec to 123uSec

This is just one sample MAX17043:
scope_53.png
scope_53.png (37.43 KiB) Viewed 4589 times
This is just one sample LSM9 all 9axis:
scope_52.png
scope_52.png (44.45 KiB) Viewed 4589 times
And my testprogram
Test_I2C_Speed.fcfx
(20.09 KiB) Downloaded 24 times

mnfisher
Valued Contributor
Posts: 1518
Joined: Wed Dec 09, 2020 9:37 pm
Has thanked: 138 times
Been thanked: 727 times

Re: ESP32 read I2C speed increase

Post by mnfisher »

Hi Stefan,

That's probably the wrong version - doesn't do an i2c read? (Unless CellGauge1::ReadStateOfChargeByte() does this?)

Martin

stefan.erni
Valued Contributor
Posts: 1012
Joined: Wed Dec 02, 2020 10:53 am
Has thanked: 193 times
Been thanked: 217 times

Re: ESP32 read I2C speed increase

Post by stefan.erni »

Hi Martin

I have deleted this CellGauge1 from the program .
It's still the same.

I have switched to software I2C as a test.
There is no pause with the I2C software,

These 2x8 clock and break is this a mistake?

Hardware I2C:
scope_58.png
scope_58.png (38.04 KiB) Viewed 4544 times
Software I2C:
scope_57.png
scope_57.png (43 KiB) Viewed 4544 times

mnfisher
Valued Contributor
Posts: 1518
Joined: Wed Dec 09, 2020 9:37 pm
Has thanked: 138 times
Been thanked: 727 times

Re: ESP32 read I2C speed increase

Post by mnfisher »

That's bizarre. It looks like the transaction write to set the register address followed by the a pause before the transaction read?

But would expect the same if using software i2c and you are not seeing that.

Do you see the same at 100/400kHz or is it just with 1MHz i2c? The mcu should be able to easily do 1MHz - I'll try and test on an i2c chip but don't imagine it will struggle..

The gyro sensor read n regs (component) doesn't do anything odd does it?

Martin

stefan.erni
Valued Contributor
Posts: 1012
Joined: Wed Dec 02, 2020 10:53 am
Has thanked: 193 times
Been thanked: 217 times

Re: ESP32 read I2C speed increase

Post by stefan.erni »

Yes the component is working at 100/400kHz/1MHz
but all speed have the same 2x8 clock in advance

If i sample with 2K the signal from 3 axis off the IMU is perfect, :)


Show the clocksignal:
1MHz
400kHz
100kHz
2025-02-04_10-46-48.PNG
2025-02-04_10-46-48.PNG (285.33 KiB) Viewed 4529 times
show the "unnecessary" 2x8 clock in advance:
scope_64.png
scope_64.png (31.38 KiB) Viewed 4529 times

mnfisher
Valued Contributor
Posts: 1518
Joined: Wed Dec 09, 2020 9:37 pm
Has thanked: 138 times
Been thanked: 727 times

Re: ESP32 read I2C speed increase

Post by mnfisher »

One byte will be the device address, then one for the register address.

No idea why hardware mode is getting such a long pause though.

Read_n_regs just does ransaction_write(address, reg) transaction_read()

Strange.... Especially as not in software mode..

stefan.erni
Valued Contributor
Posts: 1012
Joined: Wed Dec 02, 2020 10:53 am
Has thanked: 193 times
Been thanked: 217 times

Re: ESP32 read I2C speed increase

Post by stefan.erni »

I tried it directly with the code abstract layer.
I can display the values with the UART.
But with the oscilloscope I measure the same duration
Total 174uSec with a break of 73uSec
What I still don't understand is the command “Write start address” with this parameter 0x8001

Program:
2025-02-04_14-32-47.PNG
2025-02-04_14-32-47.PNG (165.05 KiB) Viewed 4518 times

mnfisher
Valued Contributor
Posts: 1518
Joined: Wed Dec 09, 2020 9:37 pm
Has thanked: 138 times
Been thanked: 727 times

Re: ESP32 read I2C speed increase

Post by mnfisher »

I've just tested using a i2c eeprom - and set for a similar scenario. So doing the same (effectively) as GetNRegs - using hardware i2c there is a delay of ~75uS before the read. This doesn't happen with software mode.

Setting the high bit (0x800n) of the number of bytes to send doesn't send a 'stop' on the i2c - then the read can follow.

This gives a total time to read 10bytes (I went one better :-) )of 2.46ms in software mode. and 256uS in hardware mode - so it's probably worth using hardware mode anyways? Still odd though - might be the sensor is holding SDA low for some processing?

Software then Hardware i2c


Martin
Attachments
testi2c.fcfx
(11.22 KiB) Downloaded 22 times
LabNation_Screenshot4.png
LabNation_Screenshot4.png (213.54 KiB) Viewed 4515 times
LabNation_Screenshot5.png
LabNation_Screenshot5.png (208.01 KiB) Viewed 4515 times

Post Reply