I'm building a raspberry pi project that focuses on outputting analog signals through a DAC connected via SPI. For now, I only need the outputs to be updated every millisecond. I'm testing on a pi3 and pi4 with the latest raspberry pi os lite, but I have installed xorg and there's a bunch of stuff going on concurrently. Still, overall CPU usage isn't very high I believe ('top' says around 85% idle, the load average says 2.0 <- not sure what that means).
There is a single C++ process under all this that does one thing in a loop :
- if 1ms has passed since the last update, update the DAC with a new value (that comes from an in memory buffer, no blocking or anything)
Measuring time is done with clock_gettime(CLOCK_REALTIME, ...).
I can carefully analyze the analog output with an oscilloscope and I notice a few things:
- The output is close to perfect, but every few seconds a pause in updates occurs that can last up to about 10ms (much more so on the pi 3)
- Any increase in activity in the other processes causes much more of these short glitches
- There are a few monster glitches that are >100ms, but they are very rare (less than one every couple of minutes, very long stretches of time without any, especially with a light load => CPU throttling?)
Although this is clearly linked to the other processes running (including one making small writes to the SD card), memory usage is very stable and the CPU never comes close to full load.
My question is what can I do to get the best possible output stability? Is it inevitable that a process will sometimes have its execution suspended for 10ms? One option I'm guessing is attaching a microcontroller as a buffer to the DAC. But even then, a 100ms latency is too much and I see no other way of covering the "monster glitches".