How to get the max speed out of ESP32 for hardware related tasks?
Posted: 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
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.
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);
}
"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.