From: Graeme Russ Date: Sun, 29 May 2011 23:44:13 +1000 Subject: [U-Boot] [RFC][PATCH 4/5][Timer]Create new timer API In-Reply-To: <1306676654-6276-1-git-send-email-graeme.russ@gmail.com> References: <1306676654-6276-1-git-send-email-graeme.russ@gmail.com> Message-ID: <1306676654-6276-5-git-send-email-graeme.russ@gmail.com> List-Id: MIME-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit To: u-boot@lists.denx.de --- include/common.h | 4 ++++ lib/time.c | 45 +++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 49 insertions(+), 0 deletions(-) diff --git a/include/common.h b/include/common.h index 21c05db..ddb5c03 100644 --- a/include/common.h +++ b/include/common.h @@ -622,6 +622,7 @@ void __udelay (unsigned long); ulong usec2ticks (unsigned long usec); ulong ticks2usec (unsigned long ticks); int init_timebase (void); +void sync_ticks (void); /* lib/gunzip.c */ int gunzip(void *, int, unsigned char *, unsigned long *); @@ -642,6 +643,9 @@ int strcmp_compar(const void *, const void *); /* lib/time.c */ void udelay (unsigned long); +void sync_timers(void); +u32 get_us_timer(u32); +u32 get_ms_timer(u32); /* lib/vsprintf.c */ ulong simple_strtoul(const char *cp,char **endp,unsigned int base); diff --git a/lib/time.c b/lib/time.c index a309c26..3341713 100644 --- a/lib/time.c +++ b/lib/time.c @@ -24,6 +24,8 @@ #include #include +DECLARE_GLOBAL_DATA_PTR; + #ifndef CONFIG_WD_PERIOD # define CONFIG_WD_PERIOD (10 * 1000 * 1000) /* 10 seconds default*/ #endif @@ -41,3 +43,46 @@ void udelay(unsigned long usec) usec -= kv; } while(usec); } + +void sync_timers(void) +{ + int flag; + u32 elapsed_ticks; + u32 elapsed_us; + u64 previous_ticks; + u64 current_ticks; + + flag = disable_interrupts(); + + previous_ticks = gd->ticks; + sync_ticks(); + current_ticks = gd->ticks; + + elapsed_ticks = (u32)(current_ticks - previous_ticks); + elapsed_us = elapsed_ticks / ticks2usec(1); + + gd->us_timer += elapsed_us; + gd->us_accumulator += elapsed_us; + + while(gd->us_accumulator >= 1000) { + gd->ms_timer++; + gd->us_accumulator -= 1000; + } + + if (flag) + enable_interrupts(); +} + +u32 get_us_timer(u32 base) +{ + sync_timers(); + + return gd->us_timer - base; +} + +u32 get_ms_timer(u32 base) +{ + sync_timers(); + + return gd->ms_timer - base; +} -- 1.7.5.2.317.g391b14