From a69916da52293e0afbd53d413eccade52a315da3 Mon Sep 17 00:00:00 2001 From: Werner Almesberger Date: Sat, 11 Jun 2011 01:59:03 -0300 Subject: [PATCH] atusb/fw/: make timer code use interrupts instead of polling - board_app.c (timer_poll): replaced with interrupt handler - board_app.c (__timer_read): merged polling of timer_poll into the read loop (so it works as before, provided that interrupts are disabled) - board_app.c (timer_read): run __timer_read with interrupts disabled - board_app.c (timer_init): enable timer 1 overflow interrupt - atusb.c (main): enabled global interrupts - atusb.c (main): don't call poll_timer anymore - board.h (timer_poll): removed --- atusb/fw/atusb.c | 7 ++++--- atusb/fw/board.h | 1 - atusb/fw/board_app.c | 30 +++++++++++++++++++++++------- 3 files changed, 27 insertions(+), 11 deletions(-) diff --git a/atusb/fw/atusb.c b/atusb/fw/atusb.c index cade73e..b51e058 100644 --- a/atusb/fw/atusb.c +++ b/atusb/fw/atusb.c @@ -14,6 +14,7 @@ #include #include +#include #include "usb.h" @@ -36,8 +37,8 @@ int main(void) ep0_init(); timer_init(); - while (1) { + sei(); + + while (1) usb_poll(); - timer_poll(); - } } diff --git a/atusb/fw/board.h b/atusb/fw/board.h index a01060c..1188a6b 100644 --- a/atusb/fw/board.h +++ b/atusb/fw/board.h @@ -77,7 +77,6 @@ void slp_tr(void); void led(int on); void panic(void); -void timer_poll(void); uint64_t timer_read(void); void timer_init(void); diff --git a/atusb/fw/board_app.c b/atusb/fw/board_app.c index 625f9c2..6ce0558 100644 --- a/atusb/fw/board_app.c +++ b/atusb/fw/board_app.c @@ -14,6 +14,7 @@ #include #include +#include #define F_CPU 8000000UL #include @@ -22,7 +23,7 @@ #include "spi.h" -static uint32_t timer_h = 0; /* 2^(16+32) / 8 MHz = ~1.1 years */ +static volatile uint32_t timer_h = 0; /* 2^(16+32) / 8 MHz = ~1.1 years */ void reset_cpu(void) @@ -44,22 +45,22 @@ void slp_tr(void) } -void timer_poll(void) +ISR(TIMER1_OVF_vect) { - if (!(TIFR1 & (1 << TOV1))) - return; - TIFR1 = 1 << TOV1; timer_h++; } -uint64_t timer_read(void) +static uint64_t __timer_read(void) { uint32_t high; uint8_t low, mid; do { - timer_poll(); + if (TIFR1 & (1 << TOV1)) { + TIFR1 = 1 << TOV1; + timer_h++; + } high = timer_h; low = TCNT1L; mid = TCNT1H; @@ -74,12 +75,27 @@ uint64_t timer_read(void) } +uint64_t timer_read(void) +{ + uint64_t res; + + cli(); + res = __timer_read(); + sei(); + return res; +} + + void timer_init(void) { /* configure timer 1 as a free-running CLK counter */ TCCR1A = 0; TCCR1B = 1 << CS10; + + /* enable timer overflow interrupt */ + + TIMSK1 = 1 << TOIE1; }