From 70451090900fd1079e1ab701495556b331461da6 Mon Sep 17 00:00:00 2001 From: Werner Almesberger Date: Thu, 21 Jun 2012 08:55:43 -0300 Subject: [PATCH] fw/accel.c: keep track of uptime and return it with uptime() (untested) --- fw/sweep.c | 26 ++++++++++++++++++++++++-- fw/sweep.h | 2 +- 2 files changed, 25 insertions(+), 3 deletions(-) diff --git a/fw/sweep.c b/fw/sweep.c index 603c0d7..059c286 100644 --- a/fw/sweep.c +++ b/fw/sweep.c @@ -24,6 +24,7 @@ volatile bool sweeping = 0; +static volatile uint32_t t_up; /* uptime, in timer ticks (wraps in 4295 s) */ static volatile uint32_t t_sw; /* cumulative number of timer ticks in sweep */ static uint16_t wait_periods; /* number of periods to wait before image */ @@ -38,9 +39,13 @@ static bool forward; ISR(TIMER1_OVF_vect) { - /* update the sweep time */ + uint16_t t; - t_sw += ICR1; + /* update the time counters */ + + t = ICR1; + t_sw += t; + t_up += t; /* if at the end of the image, only update the time */ @@ -79,6 +84,23 @@ ISR(TIMER1_OVF_vect) } +uint32_t uptime(void) +{ + uint32_t a, b; + uint16_t d; + + do { + cli(); + a = t_up; + d = ICR1; + b = t_up; + sei(); + } + while (a != b); + return a+d; +} + + void sweep_image(const struct sweep *sweep) { TCCR1B = 0; /* stop the timer */ diff --git a/fw/sweep.h b/fw/sweep.h index dbd914a..08b325e 100644 --- a/fw/sweep.h +++ b/fw/sweep.h @@ -29,7 +29,7 @@ struct sweep { extern volatile bool sweeping; - +uint32_t uptime(void); void sweep_image(const struct sweep *sweep); void sweep_init(void);