From 8ea79ddf6ab35ca48415d46f8c2ab09ed493b464 Mon Sep 17 00:00:00 2001 From: Marti Bolivar Date: Wed, 4 May 2011 13:22:00 -0400 Subject: Making micros() counter underrun safe. Thanks, ala42! --- wirish/wirish_time.h | 17 +++++++---------- 1 file changed, 7 insertions(+), 10 deletions(-) diff --git a/wirish/wirish_time.h b/wirish/wirish_time.h index a0b1c11..d5349e3 100644 --- a/wirish/wirish_time.h +++ b/wirish/wirish_time.h @@ -27,8 +27,8 @@ * @brief Timing and delay functions. */ -#ifndef _TIME_H -#define _TIME_H +#ifndef _TIME_H_ +#define _TIME_H_ #include "libmaple.h" #include "nvic.h" @@ -56,17 +56,15 @@ static inline uint32 micros(void) { uint32 cycle_cnt; uint32 res; - nvic_globalirq_disable(); - - cycle_cnt = systick_get_count(); - ms = millis(); - - nvic_globalirq_enable(); + do { + cycle_cnt = systick_get_count(); + ms = millis(); + } while (ms != millis()); /* SYSTICK_RELOAD_VAL is 1 less than the number of cycles it actually takes to complete a SysTick reload */ res = (ms * US_PER_MS) + - (SYSTICK_RELOAD_VAL + 1 - cycle_cnt)/CYCLES_PER_MICROSECOND; + (SYSTICK_RELOAD_VAL + 1 - cycle_cnt) / CYCLES_PER_MICROSECOND; return res; } @@ -96,4 +94,3 @@ void delay(unsigned long ms); void delayMicroseconds(uint32 us); #endif - -- cgit v1.2.3