diff options
author | Marti Bolivar <mbolivar@leaflabs.com> | 2011-05-04 13:22:00 -0400 |
---|---|---|
committer | Marti Bolivar <mbolivar@leaflabs.com> | 2011-05-04 13:31:53 -0400 |
commit | 8ea79ddf6ab35ca48415d46f8c2ab09ed493b464 (patch) | |
tree | 20dd2f13e7e64ef376704d6b4690d9d1efd71805 /wirish | |
parent | 55323faebe0abb6b8900fc7b1344d383abe08a9d (diff) | |
download | librambutan-8ea79ddf6ab35ca48415d46f8c2ab09ed493b464.tar.gz librambutan-8ea79ddf6ab35ca48415d46f8c2ab09ed493b464.zip |
Making micros() counter underrun safe.
Thanks, ala42!
Diffstat (limited to 'wirish')
-rw-r--r-- | wirish/wirish_time.h | 17 |
1 files 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 - |