// like, sleeping.
avr_cycle_count_t cycle; // current cycle
+ // these next two allow the core to freely run between cycle timers and also allows
+ // for a maximum run cycle limit... run_cycle_count is set during cycle timer processing.
+ avr_cycle_count_t run_cycle_count; // cycles to run before next timer
+ avr_cycle_count_t run_cycle_limit; // maximum run cycle interval limit
+
/**
* Sleep requests are accumulated in sleep_usec until the minimum sleep value
* is reached, at which point sleep_usec is cleared and the sleep request
} \
}
+#define DEFAULT_SLEEP_CYCLES 1000
+
void
avr_cycle_timer_reset(
struct avr_t * avr)
avr_cycle_timer_slot_p t = &pool->timer_slots[i];
QUEUE(pool->timer_free, t);
}
+ avr->run_cycle_count = 1;
+ avr->run_cycle_limit = 1;
+}
+
+static avr_cycle_count_t
+avr_cycle_timer_return_sleep_run_cycles_limited(
+ avr_t *avr,
+ avr_cycle_count_t sleep_cycle_count)
+{
+ // run_cycle_count is bound to run_cycle_limit but NOT less than 1 cycle...
+ // this is not an error!.. unless you like deadlock.
+ avr_cycle_count_t run_cycle_count = ((avr->run_cycle_limit >= sleep_cycle_count) ?
+ sleep_cycle_count : avr->run_cycle_limit);
+ avr->run_cycle_count = run_cycle_count ? run_cycle_count : 1;
+
+ // sleep cycles are returned unbounded thus preserving original behavior.
+ return(sleep_cycle_count);
+}
+
+static void
+avr_cycle_timer_reset_sleep_run_cycles_limited(
+ avr_t *avr)
+{
+ avr_cycle_timer_pool_t * pool = &avr->cycle_timers;
+ avr_cycle_count_t sleep_cycle_count = DEFAULT_SLEEP_CYCLES;
+
+ if(pool->timer) {
+ if(pool->timer->when > avr->cycle) {
+ sleep_cycle_count = pool->timer->when - avr->cycle;
+ } else {
+ sleep_cycle_count = 0;
+ }
+ }
+
+ avr_cycle_timer_return_sleep_run_cycles_limited(avr, sleep_cycle_count);
}
// no sanity checks checking here, on purpose
return;
}
avr_cycle_timer_insert(avr, when, timer, param);
+ avr_cycle_timer_reset_sleep_run_cycles_limited(avr);
}
void
last = t;
t = t->next;
}
+ avr_cycle_timer_reset_sleep_run_cycles_limited(avr);
}
/*
{
avr_cycle_timer_pool_t * pool = &avr->cycle_timers;
- if (!pool->timer)
- return (avr_cycle_count_t)1000;
-
- do {
+ if (pool->timer) do {
avr_cycle_timer_slot_p t = pool->timer;
avr_cycle_count_t when = t->when;
if (when > avr->cycle)
- return t->when - avr->cycle;
+ return avr_cycle_timer_return_sleep_run_cycles_limited(avr, when - avr->cycle);
// detach from active timers
pool->timer = t->next;
QUEUE(pool->timer_free, t);
} while (pool->timer);
- return (avr_cycle_count_t)1000;
+ // original behavior was to return 1000 cycles when no timers were present...
+ // run_cycles are bound to at least one cycle but no more than requested limit...
+ // value passed here is returned unbounded, thus preserving original behavior.
+ return avr_cycle_timer_return_sleep_run_cycles_limited(avr, DEFAULT_SLEEP_CYCLES);
}