From 6df40e824c0b686b12d14193ebb0e944aee697eb Mon Sep 17 00:00:00 2001 From: Sami Liedes Date: Thu, 17 Feb 2011 21:47:00 +0200 Subject: [PATCH] timer: Fix a subtle off-by-one bug in TCNT reading. There's a subtle bug in TCNT (timer counter) reading which only causes the counter to ever run from 0 to TOP-1, while it should stay in the TOP value for a full timer cycle before resetting to 0 (but the interrupt needs to come when TOP is first reached). Interestingly, this causes Arduino's micros() function to occasionally return incorrect values. micros() function relies on TCNT0 not transitioning from <= 254 to 0 in only a few cycles with /64 prescaler. Signed-off-by: Sami Liedes --- simavr/sim/avr_timer.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/simavr/sim/avr_timer.c b/simavr/sim/avr_timer.c index a218a67..b01b76f 100644 --- a/simavr/sim/avr_timer.c +++ b/simavr/sim/avr_timer.c @@ -126,7 +126,7 @@ static uint16_t _avr_timer_get_current_tcnt(avr_timer_t * p) if (p->tov_cycles) { uint64_t when = avr->cycle - p->tov_base; - return (when * p->tov_top) / p->tov_cycles; + return (when * (((uint32_t)p->tov_top)+1)) / p->tov_cycles; } return 0; } -- 2.39.5