aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMarti Bolivar <mbolivar@leaflabs.com>2011-05-04 13:22:00 -0400
committerMarti Bolivar <mbolivar@leaflabs.com>2011-05-04 13:31:53 -0400
commitbc745cd913006dae7b54074b5ed25323fca05e40 (patch)
tree0cbdb494240fce82e76cf09c94fa4967757aac1e
parent43b3ba82e692345883e168c57fde1e08d39c7064 (diff)
Making micros() counter underrun safe.
Thanks, ala42!
-rw-r--r--wirish/wirish_time.h17
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
-