'Timer or Delayed work which is more accurate in Zephyr OS?

In Zephyr Operating System, I have to write an application, which will update a variable (can be treated as a counter) every 30ms. Basically I have 2 options

  1. A timer which expires every 30ms and update the variable in the ISR
  2. A delayed work which updates the variable at 30ms

Both seems to work well but, I would like to know which of them would be more accurate? Or the recommended

I am developing on couple of chipsets, eg nRF52840 and NXP frdm-k64f I am also aware that the delayed work allows me to protect the variable using a Muxtex which cannot be done in ISR



Solution 1:[1]

If you follow Zephyr RTOS scheduling description, it's probably your option 1 which would be more accurate:

"Execution of ISRs takes precedence over thread execution, so the execution of the current thread may be replaced by an ISR at any time unless interrupts have been masked. This applies to both cooperative threads and preemptive threads."

But for my understanding, the following questions would be relevant to your problem:

  • What accuracy is needed?
  • Are the intervals summed? Occurs an error over time?
  • If option 2 is used (without ISR). Will the other threads be developed further (becoming bigger and using more processing time)?

I think you would get a very accurate answer (at least from a statistical point of view) when you toggle an output pin immediately (within the same routine) before/after (or both) you write to your variable and measure the signal (toggle delays) with an oscilloscope.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 El tornillo