Page 4 of 4

Re: ESP32 read I2C speed increase

Posted: Tue Mar 04, 2025 12:30 pm
by mnfisher
Just for fun - I tried bit-banging the i2c... No delay in the reads ! :-) Might need to tweak the delay (i2c_delay) to get the right speed...

Re: ESP32 read I2C speed increase

Posted: Tue Mar 04, 2025 3:38 pm
by mnfisher
It's not faster though :-( Setting i2c_delay to 0 - gives a read time of 160uS (reading 8 bytes from a EEPROM)...

Not sure what the i2c speed equivalent would be - about 620kbps?

Martin

Re: ESP32 read I2C speed increase

Posted: Tue Mar 04, 2025 4:25 pm
by mnfisher
Getting rid of the call to ets_delay_us and replacing it with some 'nops'..

Code: Select all

gpio_set_level(FCL_PIN, FCL_LEVEL);
//ets_delay_us(FCV_I2C_DELAY);
int n = 8;  // Adjust based on actual cycle count per loop iteration
while (n--) {
     __asm__ __volatile__("nop");
}

(in gpio_set)

Setting n to 8 is the fastest that seems to give reliable results from the EEPROM chip (other devices may differ)

This gives a read time of almost exactly the same as ets_delay_us(0); (I measured as 157uS) - so the overhead of calling the function is quite high...
However, using a delay like this would allow the speed to be set more exactly (100kHz, 400kHz) and also does away with the external call - so this could be converted to a component (though I haven't tested this!)

Martin

Re: ESP32 read I2C speed increase

Posted: Wed Mar 05, 2025 7:44 am
by stefan.erni
I remember the nop when I had to program in assembler.
Instead of a loop, it sometimes works better (more accurately) with just the commands

I'm also interested if this will work in interrupt macro, That would be great.
I'm going to test that too.
I have not yet given up on being able to read I2C with an interrupt macro.
It is so convenient and accurate to sample data precisely with the STM32 and PIC32

Code: Select all


[i]while (n--) {
     __asm__ __volatile__("nop");
}[/i]

 __asm__ __volatile__("nop");
 __asm__ __volatile__("nop");
 __asm__ __volatile__("nop");
 __asm__ __volatile__("nop");
 __asm__ __volatile__("nop");




Re: ESP32 read I2C speed increase

Posted: Wed Mar 05, 2025 9:23 am
by mnfisher
Will be interesting to hear how you get on..

In theory it should work in an interrupt - I don't think any of the code relies on an interrupt on it... The eta-delay is also a busy wait rather than a yield to RTOS.

The loop does have overhead - I was initially thinking of a 1uS delay which would be about n = 48 so unrolling the loop was a bit clunky...

Also - I don't actually check ACK, probably sensible so to do...

I also wondered if writing / reading the register for output would be quicker than using gpio_ set. That's a job for another day though...

Martin

Re: ESP32 read I2C speed increase

Posted: Wed Mar 05, 2025 10:37 am
by mnfisher
Just tested and it does work in an interrupt... I tried 1ms. I needed a longer delay than n = 8 for reliable read - at least on my logic analyser...

Looking at the trace and I think the delay could be a little smarter (so maybe passed to gpio_set as a parameter) - as in some places there are very short pulses (increase delay a bit) - whereas other cases where there are other function calls are longer (reduce delay).

Martin

Re: ESP32 read I2C speed increase

Posted: Wed Mar 05, 2025 3:05 pm
by stefan.erni
Hi Martin

Yes, that works. For a 1Mhz clock I don't need a wait.

I have not yet tested the values I am reading, but it is fast and in the interrupt macro! That is very good!

no delay ist the best:
2025-03-05_15-58-13.PNG
2025-03-05_15-58-13.PNG (121.6 KiB) Viewed 2268 times

with while:
scope_109.png
scope_109.png (38.67 KiB) Viewed 2268 times
no wait 1Mhz:
scope_114.png
scope_114.png (32.71 KiB) Viewed 2268 times

Re: ESP32 read I2C speed increase

Posted: Fri Mar 07, 2025 10:39 am
by mnfisher
Just tested - spitting the values read out to UART (instead of dumping them) - and my EEPROM test 'subject' needs a delay of 8...
Faster speeds return 255 as the value read (I programmed the chip with 0x7Bs)
How goes it with the gyro?

I just used a global array - but you would probably be using xQueueSendFromISR? - though this might necessitate waking a task too?

Martin

Re: ESP32 read I2C speed increase

Posted: Fri Mar 21, 2025 7:57 am
by stefan.erni
Hi Martin

I will continue to use the ESP32 in my project with slow sample rate
and also because of the SD-Card which is perfect for slow sampling rates.
Bluetooth and USB also work great with the ESP32.
But the fact that the interrupt hangs on most commands bothers me.
And so everything together, it's a good cpu but not the right processor for my next project.
I have done a lot of testing and compared it with an STM32F469. This STM does it easily with the desired I2C bus time and I can also simply set the I2C clock to 2...3Mhz. I leave it at 1Mhz and the STM32 has the advantage that I can sample in the interrupt macro. This also makes the samples a little more stable. Now I have to look for an STM32 board that works with the SD card and also with a USB-C connector. And Martin, thank you very much for your great support.