From ca540c9d078ad2c3c4f0626edf52edc75cb54560 Mon Sep 17 00:00:00 2001 From: Kaspar Schleiser Date: Tue, 15 Sep 2015 00:04:40 +0200 Subject: [PATCH] cpu: atmega2560: use double-read scheme for timer read --- cpu/atmega2560/periph/timer.c | 22 ++++++++++++++++------ 1 file changed, 16 insertions(+), 6 deletions(-) diff --git a/cpu/atmega2560/periph/timer.c b/cpu/atmega2560/periph/timer.c index d4e5eb660..cf1a68c5b 100644 --- a/cpu/atmega2560/periph/timer.c +++ b/cpu/atmega2560/periph/timer.c @@ -278,7 +278,8 @@ int timer_clear(tim_t dev, int channel) unsigned int timer_read(tim_t dev) { - uint16_t value; + uint16_t a; + uint32_t b; /* * Disabling interrupts globally because read from 16 Bit register can * otherwise be messed up @@ -289,29 +290,38 @@ unsigned int timer_read(tim_t dev) #if TIMER_0_EN case TIMER_0: - value = TIMER0_COUNTER; + do { + a = TIMER0_COUNTER; + b = TIMER0_COUNTER; + } while (a != b); break; #endif #if TIMER_1_EN case TIMER_1: - value = TIMER1_COUNTER; + do { + a = TIMER1_COUNTER; + b = TIMER1_COUNTER; + } while (a != b); break; #endif #if TIMER_2_EN case TIMER_2: - value = TIMER2_COUNTER; + do { + a = TIMER2_COUNTER; + b = TIMER2_COUNTER; + } while (a != b); break; #endif case TIMER_UNDEFINED: default: - value = 0; + a = 0; } restoreIRQ(state); - return value; + return a; } void timer_stop(tim_t dev)