ESP32 Speed Increase

Tips, Tricks and methods for programming, learn ways of making your programming life easier, and share your knowledge with others.
Post Reply
mnfisher
Valued Contributor
Posts: 1671
http://meble-kuchenne.info.pl
Joined: Wed Dec 09, 2020 9:37 pm
Has thanked: 144 times
Been thanked: 772 times

ESP32 Speed Increase

Post by mnfisher »

A couple of tips to increase the speed of your esp32 programs.
(I mentioned these recently - but they make such a big difference I thought them worthy of tricks and tips....)

1) Compile with optimisation set to speed (idf.py menuconfig - choose compiler options and select optimise for speed)
Do not try the run entirely in RAM option - this doesn't seem to work well at present :-(

2) Interrupts (which of course should be as short and fast as possible) - run more quickly when run in IRAM (rather using flash memory)
The speed increase obtained depends on the length of the ISR - but even with the very simple ISR in this demo - there is a speed increase.

I connected pin23 of an esp32-wroom to a signal generator at 300kHz. With the interrupt set for both edges (600kHz) - I got the following results:

IPS (Interrupts per second)
408000 FC Default (Debug)
553000 FC Default (Optimised for Speed)

416000 IRAM (Debug)
599080 IRAM (Speed)

Note that the speed up is more dramatic for longer ISRs (flash memory is much slower) - but even the minimal ISR displayed here shows an improvement.

In this demo - I display the number of interrupts handled per second - the display task is pinned to core 1 (to avoid a rather slow print effect) - and the different ISRs can be selected by toggling the marked blocks.

To create an ISR in IRAM - copy the code (use view code) to supplementary code and add a header with the same signature to the definitions and headers block.

To the code definition - add IRAM_ATTR

For example

Header
void ISR();

Body
void IRAM_ATTR ISR() {
...
}

ISRs in IRAM can only call functions that are also in IRAM (or ROM) - and ISRs should never call long functions (print for example). To read a pin use gpio_get_level(pin_number); (NOT FC's GET_PORT_PIN)

Adding a pin read - reduces the speed to 386000 for IRAM and crashes (WDT) using the flash ISR.

Martin
Attachments
ESP_ISR_Test.fcfx
(14.81 KiB) Downloaded 10 times

Post Reply