How to get the max speed out of ESP32 for hardware related tasks?

thomasx
Posts: 8
Joined: Sun Mar 17, 2024 10:07 pm

How to get the max speed out of ESP32 for hardware related tasks?

Postby thomasx » Sun Mar 17, 2024 10:50 pm

Hi

Sometimes you really don't now how well something will work, and then you just have to try it out. This is one of those occassions.

So, considering the ESP32-DevKitC I happened to have lying around runs at a clockspeed of 260MHz, I figured it could do some pretty neat things, fast.

So I wrote this very simple code, just to try out a frequency 2x upscaler

Code: Select all

#define DIn GPIO_NUM_14
#define DOut GPIO_NUM_16


void IRAM_ATTR toggle_ISR() {  
    digitalWrite(DOut,HIGH);
    digitalWrite(DOut,LOW);
    return;
}

void setup() {
  pinMode(DIn, INPUT);
  pinMode(DOut, OUTPUT);
  digitalWrite(DOut,LOW);
  attachInterrupt(DIn, toggle_ISR, CHANGE);

}

void loop() {
  delay(10);
}
As you can see from the code, on each state change of the input signa an interrupt is called that will generate a short positive pulse on the output. So for any given input frequency, the frequency of the output pulses will be twice the input frequency, though not with the same duty cycle. But that was not important for this test.

"For any given input frequency", is of course a truth with some limitations.

But again, at 260MHz clock frequency, I figured a 6 MHz input signal should work fine.

It didn't.

So I tried a 10 kHz signal instead, and that worked. Then I noticed that the time delay from a state change on the input to the positive edge on the output was more or less exactly 2 us. The time delay to the negative edge of the output was about the same, 2 us. Which puts the maximum frequency to be doubled somewhere around 50-100kHz, and then there is really not much time left for anything else. That seems more than a bit on the low side for a 260MHz microcontroller.

I was a bit surprised at these relatively large delays, in relation to the ESP32 clock speed. I guess it has to do with the OS running in the background.

I come mainly from the old microcontroller "era" where we program microcontrollers "directly" with assembler or C to be compiled without any OS involved, so I am not very knowledgeable with these more modern things like ESP32 and what goes on in the background.

So I guess, I end up with the question, or questions - Can I use the ESP32 for more direct hardware-related stuff with some better control on what goes on and at what speed? And if so, how?

I did notice the possibility of inserting assembler instructions in-line in the c-code. Is that the way to go, or are there any "simpler" ways to go?

There is of course always the possibility to go for a "pure" hardware microcontroller, but now I am interested in learning about the ESP32 and how to do this with it and the popular development tools around.

ESP_Sprite
Posts: 9727
Joined: Thu Nov 26, 2015 4:08 am

Re: How to get the max speed out of ESP32 for hardware related tasks?

Postby ESP_Sprite » Mon Mar 18, 2024 2:39 am

The issue is that 'older' microcontrollers like AVRs etc are simpler devices, with the peripherals very close to the CPU core, and in the same clock domain as the CPU core only works at a few MHz. That doesn't work for faster CPUs; you need bus logic, clock domain crossings, etc, and the GPIO subsystem is all the way of the other end of that. That's why a 240MHz CPU core can be so slow in toggling a pin. In other words: Making the software faster isn't going to help you there.

On how to fix it: your best bet is to take one of the peripherals and see if you can bend it to do your bidding. The ESP32 has a fairly large set of flexible peripherals, and between parallel I2S, RMT, LEDC and others there tends to be one that can do what you want. If you still need to poke GPIOs directly: the later chips (ESP32-S3 for instance) have a 'fast GPIO' function that allow you to poke a few pins directly from the CPU core, although I'm not sure how good Arduino support for that is.

mikemoy
Posts: 626
Joined: Fri Jan 12, 2018 9:10 pm

Re: How to get the max speed out of ESP32 for hardware related tasks?

Postby mikemoy » Mon Mar 18, 2024 11:52 am

If you really want to toggle GPIO's faster you should look into
GPIO.out_w1ts, GPIO.out_w1tc, GPIO.out1_w1ts.val, GPIO.out1_w1tc.val

DrMickeyLauer
Posts: 168
Joined: Sun May 22, 2022 2:42 pm

Re: How to get the max speed out of ESP32 for hardware related tasks?

Postby DrMickeyLauer » Fri Mar 22, 2024 11:15 am

Slightly tangential to your original question (and probably an unpopular opinion), but... if you want to get the max speed out of ESP32, I'd advise going for ESP-IDF directly, not through the Arduino abstraction layer.

Who is online

Users browsing this forum: Bing [Bot] and 94 guests