cancel
Showing results for 
Search instead for 
Did you mean: 

Nanosecond delay

MBHIR.12
Associate

I want to generate a nanosecond delay on the STM32F407 µC, the closest delay I can have is 6ns using either TIM 9/10 or 11, because they work on a 168 MHz clock. But when I use an oscilloscope to visualize whether I successfully generated a 6ns delay, I can't get below 800 ns, so I'm wondering if it's possible to generate that delay or is the oscilloscope isn't capable of tracking the delay.

4 REPLIES 4

It's hard to resolve at that level. You can't interrupt, and any registers or loops you have might give you rougher granularity, or the MCU needs to service something else.

In SW loops you could use DWT CYCCNT to resolve MCU cycles, or time blocks of code.

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..

Any two events generated in STM32F407 will be at least 6ns apart, so even if you set a pin from 0 to 1 and then to 0, it's guaranteed that it will be 1 at least 6ns.

If you would want events to be apart guaranteed at least 12ns, use a __NOP or a __DMB in between the events. It may be, that the delay will be longer, there's no guarantee for that.

For 18ns, use two NOPs, etc.

We're talking about assembly code, running out of zero-latency memory. C-code may be compiled to many instructions thus taking much longer to execute.

JW

Piranha
Chief II

Probably the safest approach is to disable the interrupts and then busy-loop on some hardware timer. Could be done even with SysTick, by implementing the wrap-around logic properly. If the delay function has to take in nanoseconds and convert to clock cycles at runtime, then the simplest approach is doing a multiplication with a predefined constant (in this case 6 ns). If that accuracy is not enough, the second best choice is multiplying with a fraction, where the divisor is a base-2 number, because it will be compiled as a bit shift. For other bases the division instruction alone can take up to 12 clock cycles. That said, the best option is to define the delays in clock cycles at compile time and, if necessary, create a macro, which converts nanoseconds to clock cycles.

Danish1
Lead II

Do we know if the OP wants to make a pulse of controllable duration (single cycle of pwm) or if the start is an external event e.g. an edge arriving at a GPIO pin?