I'm working on a project on a STM32F4 CPU, generating signals (VGA) with DMA.
I have a generic timer on CPU clock (no prescaler) on a STM32 triggering interrupts on overflow, to generate a periodic signal with GPIO afterwards.
I need to trigger thr GPIO at a very regular time (basically down to a CPU cycle precision). I've managed to reduce this jitter to +-5 cycles by setting priorities & al, but this jitter exists, depending on what the CPU was doing and the interrupted instruction.
I need to compensate this few cycles jitter. Adding a few cycles more latency isn't a problem as long as I toggle GPIOs always at the same counter cycle.
My idea was to read the current value of the counter after the interrupt, and have an active loop of (FIXED_NUMBER-CNT->VAL) time, ensuring I would exit the loop at precise times.
However, doing a simple loop in C - being a FOR loop, or a while(counter->value < TARGET); doesn't work as it ADDS jitter instead of reducing it.I ensured with empty, non optimized but not hitting memory loop body (asm(""))
See this example on AVR (more predictable timings) See by example http://lucidscience.com/pro-vga%20video%20generator-7.aspx (search for "jitter")
I tried a simple loop in assembly such as (r0 has the number of cycles to wait to compensate counter value)
loop : SUBS r0,#1 ; tried with 2 also BGE loop
and, again, jitter is better without it.
To sum it up, I already know how much I should delay. Unfortunately, branches alone don't seem to work (nondeterminisctic pipeline refill ?) and IT conditional expressions don't either because they always take the same number of cycles (sometimes doing nothing).
Would running from RAM instead of flash improve consistency ?
Maybe I'm out of my league here ... any help would appreciated, thanks!
(crosspost from stackoverflow as I think I'd have more success here than there, sorry)