This post is older than 2 years and might not be relevant anymore
More Info: Consider searching for newer posts

app_timer_appsh and debug with repeat timer, nRF51

When using app_timer through scheduler, a break and go will overflow the event queue if a timer is a repeat timer. The effect is that it's not possible to debug.

To work around this I use a skip patch to skipp the repeated events when the debugger is put into go:

 components/libraries/timer/app_timer.c | 12 ++++++++++++
 1 file changed, 12 insertions(+)

diff --git a/components/libraries/timer/app_timer.c b/components/libraries/timer/app_timer.c
index 6fe3974..f69a668 100644
--- a/components/libraries/timer/app_timer.c
+++ b/components/libraries/timer/app_timer.c
@@ -537,11 +537,13 @@ static void expired_timers_handler(uint32_t         ticks_elapsed,
                                    timer_node_t **  p_restart_list_head)
 {
     uint32_t ticks_expired = 0;
+    uint32_t ticks_behind = ticks_diff_get(rtc1_counter_get(), m_ticks_latest);//ticks_previous);
 
     while (mp_timer_id_head != NULL)
     {
         timer_node_t * p_timer;
         timer_node_t * p_timer_expired;
+        uint32_t       ticks_overrun;
 
         // Auto variable for current timer node.
         p_timer = mp_timer_id_head;
@@ -569,6 +571,16 @@ static void expired_timers_handler(uint32_t         ticks_elapsed,
         {
             p_timer->ticks_at_start       = (ticks_previous + ticks_expired) & MAX_RTC_COUNTER_VAL;
             p_timer->ticks_first_interval = p_timer->ticks_periodic_interval;
+
+            // More behind than interval?
+            ticks_overrun = ticks_diff_get(ticks_behind, p_timer->ticks_first_interval);
+            if ((ticks_behind <= (MAX_RTC_COUNTER_VAL / 2))         // Are we behind
+                && (ticks_behind >= p_timer->ticks_first_interval)) // and more than one interval?
+            {
+                // Find number of complete periods behind and set to trigger there
+                p_timer->ticks_first_interval *= ticks_behind / p_timer->ticks_first_interval;
+            }
+
             p_timer->next                 = *p_restart_list_head;
             *p_restart_list_head          = p_timer_expired;
         }
Related