Beware that this post is related to an SDK in maintenance mode
More Info: Consider nRF Connect SDK for new designs

app_timer2 issue with operations queued in interrupt context

Hello,

I encountered an issue with the app_timer2 library (SDK  16.0 and 17.1) : in some cases the timers won't be executed in the correct order and delay.
The problem happens when timer operations are queued in a particular order in an interrupt context with a priority level higher or equal to the RTC1 interrupt priority.

Let's say that we have 3 timers running that will expire in:
T1 : 100ms
T2 : 200ms
T3 : 300ms

Then the following timer operations are queued in the interrupt context  :
1. start a timer T4 with delay 1000ms.
2. stop timer T2.
3. start timer T2 with delay 2000ms.

T2 isn't removed from the queue yet because the stop request isn't processed but it's delay has been updated in app_timer_start.
So when exiting the interrupt the queue is the following:
T1 : 100ms
T2 : 2000ms
T3 : 300ms

Because when the start T4 operation will be processed we still have T2 at 2000ms on second position, T4 will be queued before T3. Then T2 will be removed from the queue and added again.
So after processing all the requests the queue is :
T1 : 100ms
T4 : 1000ms
T3 : 300ms
T2 : 2000ms

Hence the sortlist is not sorted anymore, some timers won't be executed in time.

One workaround would be to avoid queuing multiple timer operations in an interrupt context.
Another solution that still need to be fully tested : in app_timer_start we could also store the next delay and apply it only when the operation is processed.


Guillaume

Parents
  • Hi Guillaume

    I discussed this with one of the developers, and apparently this is a known limitation in the app_timer2 implementation. If you start or stop timers from a high priority interrupt it can cause issues. 

    If you make sure to only schedule app timer operations from the same or lower priority as the RTC interrupt it should work fine. 

    Best regards
    Torbjørn

  • Hello Torbjorn,
    Thanks for your reply.
    Yes it works fine if we schedule operations only in interrupts with lower priority (timer operation are processed directly). But I think the documentation should explain in more detail what can and cannot be done. That's not obvious because the point of having an operation queue in this library is to be able to process operations later.
    The information I found regarding this case are some comments in the code. But it actually suggests that it works ok in the end which is not always the case given my example above.

    Do you plan to rework this libray ? As I suggested storing the next delay and apply it when the operation is processed seems to work.

    Best regards

Reply
  • Hello Torbjorn,
    Thanks for your reply.
    Yes it works fine if we schedule operations only in interrupts with lower priority (timer operation are processed directly). But I think the documentation should explain in more detail what can and cannot be done. That's not obvious because the point of having an operation queue in this library is to be able to process operations later.
    The information I found regarding this case are some comments in the code. But it actually suggests that it works ok in the end which is not always the case given my example above.

    Do you plan to rework this libray ? As I suggested storing the next delay and apply it when the operation is processed seems to work.

    Best regards

Children
Related