Page 1 of 1

Instruction execution times, esp. setting GPIOs

Posted: Fri Jul 26, 2019 6:01 pm
by scotthauck
I am working to port existing Arduino code to my ESP32, which had to be performance optimized before to remove divisions, and to do direct pin writes (both were substantial speedups). So to understand the ESP32 better, I built a loop that runs specific instructions repeatedly, and got the following timings for individual instructions/calls:

// int Add: 4.2ns = 1 cycle
// int Mult: 8.4ns = 2 cycles
// int Div: 16.79ns = 4 cycles
// int Add + Div: 20.98ns checks out.
// 2*digitalWrite: 244.06ns = 58 cycles
// 2*REG_WRITE(GPIO_OUT_W1TS_REG, BIT13); 92.50ns = 22 cycles
// Directly w/o asserts: 92.48ns

First off, nice that it seems to have a fast divider (though slightly surprised it is 2x a MULT). That's good.

My question is about the GPIO writing. digitalWrite is slow here, but also was on other Arduino boards. But, directly manipulating the registers via REG_WRITE was also slower than I would have expected - 11 cycles each. I did substitute the code for REG_WRITE directly into the code without the associated ASSERTs, so it isn't that. Any idea why register twiddling takes this long, and is there a way around it? Or have people found different speeds for this?

And if there's anything that will give the expected clock cycles per instruction somewhere, that would be great as well.


Re: Instruction execution times, esp. setting GPIOs

Posted: Mon Jul 29, 2019 2:11 am
by boarchuz
How about

Code: Select all

GPIO.out_w1ts = (1 << GPIO_NUM_x);
GPIO.out_w1tc = (1 << GPIO_NUM_x);

Re: Instruction execution times, esp. setting GPIOs

Posted: Mon Jul 29, 2019 8:39 pm
by scotthauck
Same - 11 cycles each.