Nanosecond delays

I need to have a GPIO pin toggle low-high-low with it being in the high state for at least 10ns. This needs to be done right at the start in the setup() code of a component. The only delay function I can find is delay(ms) which has microsecond resolution. This means delay(1) will delay for 100 times longer than I need. Since the component setup is time critical, are there any finer resolution delay methods?

A dirty cludge for you:
for( q=0; q<qmax; q++) { q++;}

Yes I know all the docs say don't lay it out like this but for a dumb delay, I see no reason to space out the code more.
The number such as qmax=60; might need some trial and error, will change if you go to a different clock rate such as on a different board type or from a different 'sleep' state, and this method might get unannounced interruptions often enough that 'it worked once' does not tell you much about whether to expect it to work tomorrow.

Are you doing this more than once (the high bit)?
How will you notice an extra 1ms?
How often are you rebooting the device?

You can use delayMicroseconds(us)

But, if you really need to wait an even shorter period of time, implement a busy wait and use the high resolution timer to keep track. I doubt you will be able to get exactly 10ns.

Your math is not quite right.
1000ns = 1us
1000us = 1ms
So if you only need 10ns and you use 1ms, your delay is many orders of magnitude greater than needed.

Waiting longer will not have any detrimental effect on the device, but since setup() is single threaded for all components any unnecessary delay here will delay the boot. I simply want to be "nice" to the system and have my code execute as quickly as possible.

Quite right. I had it in my head that delay() used microseconds and not milliseconds!

Since there are some calculations that need to be done for the setup I could call millis() immediately after setting the GPIO and then again after the calculations and if it has changed not call delayMicroseconds(1) .

Although I am probably being overly concerned about a negligible delay.

Given the usual clocking frequency of CPUs, a single clock cycle takes between 5 and 20 nanoseconds at best.

Any high level code will consume tens or hundreds of cycles to do anything. Apparently it takes 1 to 2 clock cycles or more just to perform a raw write to a GPIO pin, so at best using machine code a low-high-low can only be done over 40 to 100 ns.

Very fast timing can only be achieved using hardware, ideally a single machine command or DMA designed into the CPU itself.

The best you can do is to turn the pin on and immediately off again, which I believe is going to take about 10 micro seconds in a low level language.

1 Like

No, its 100 000 times more than you need. Does it cause problems?

This thread is beginning to sound like the https://xyproblem.info/

You will likely get more helpful answers if you tell us what you are really trying to do/solve.

It could be you want your devices to be ready immediately after a power failure (since that is likely the reason the device is even executing the setup() code.

If so an extra ms isn't going to make a bit of difference that you will be able to notice. If you had to do the high-low-high transition 1000 times, you probably still wouldn't notice the difference.

Now, if you are talking about a battery powered device, it might make a difference in battery life. But there are a LOT more things that you need to solve before this one becomes meaningful.

For anything where you care about short durations the millis() clock is NOT the one you want. There are high resolution timers that can give you much better resolution. They are cool and interesting to get working, but totally not necessary for most things.