STM32 HAL + FreeRTOS (tickless) -> systick drift

rtel wrote on Wednesday, January 25, 2017:

So attached is an experimental implementation (NOT TESTED!) that only enables interrupts after all calculations have been completed. This will no doubt help the tick drift, but comes at the expense of needing to perform the caluclations before the interrupt that brings the system out of sleep mode can execute. Comments?

janoshnosh wrote on Monday, February 13, 2017:

Thank you very much for attending to the issue and for your example.

So in this example you have implemented my last (“simpler”) suggestion, to only enable the interrupts after everything is done.

I understand the dilemma. We would lose the response time of the interrupt that wakes up the system.
However, if we would implement my first suggestion, extend the critical section to the whole calculation, we would serve the wakening interrupt directly - with a running SysTick. The SysTick however would be incorrect because we haven’t adjusted the sleep time yet. Do you agree with that?

So it looks like, in order to have a consistent system, the best that can be done is:

  1. Wake up by an interrupt
  2. Perform the time calculations.
  3. Enable interrupts and serve the causing interrupt.

This way, we should keep the following constraints:

  1. The system never misses time. (no interrupts are executed with halted time).
  2. The system time is always correct. (the system time is up-to-date in interrupts).

What are your thoughts on this?