Due Loop time

I am developing a product based on the Due and I am having speed issues.

Here is the code I am testing with:

const byte outPin = 41;
Pio* outPort = g_APinDescription[outPin].pPort;
unsigned int outMask = g_APinDescription[outPin].ulPin;

void setup() {
  pinMode(outPin, OUTPUT);  

void loop() {
  outPort->PIO_SODR = outMask;
  outPort->PIO_CODR = outMask;

This gives a pulse as expected, but the time between pulses is 2.2 uS. I can only guess Arduino is doing background stuff, but it is eating up about 180 clock cycles.

Is there any way to speed this up?

You might want to read the "Ethernet2 (UDP) SPI transfers have a lot of dead time" further down the page, some progress has been made :slight_smile:

I see similar times, and I don't understand it either :frowning:

Ah. This appears to be due to the SerialEventRun() polling crap in main() Due has 4 serial ports, so that's minimally 4 calls to SerialX.available() I was under the impression that the compile was smart enough to omit that when there were no serial ports in use, but I guess not :frowning:
Removing the SerialEventRun() call reduces the interval to about 400ns (~33 cycles.) That still seems a bit longer than I'd hope (the instruction sequence looks "ok"), but it's a lot better.

Wow. Thanks for the info. I guessed there was stuff going on that is out of sight. How does one go about removing SerialEventRun()?

Apologies for my previous misleading post, for some reason I thought I'd read SPI in there, I'm pretty sure I'd only had one beer :slight_smile: