CPU Spinning

Thread Starter

Futurist

Joined Apr 8, 2025
721
I've found myself writing tight loops while experimenting with NRF devices.

Ordinarily that kind of code is frowned up to put it mildly, but on MCUs with no OS I guess there's no choice.

For example in a receiver test app I spin checking a volatile flag, the flag gets set by an interrupt handler which sets the flag and when that returns and the "user code" loop resumes, it exits and the code reads the message from the device.

I wrapped that loop in a function SpinForInterrupt that takes a max spin arg (or -1 for forever).

I can envisage an extension of that idea, that can spin on multiple interrupts, perhaps a data structure with a volatile flag and a volatile ID indicating which interrupt took place. But that is too simplistic when there's the possibility of nested interrupts.

How do you professional microcontroller guys address this kind of thing?
 
Top