timer_simple_avr.c

Go to the documentation of this file.
00001 
00040 /*#*
00041  *#* $Log$
00042  *#* Revision 1.2  2006/07/19 12:56:26  bernie
00043  *#* Convert to new Doxygen style.
00044  *#*
00045  *#* Revision 1.1  2005/04/12 01:37:50  bernie
00046  *#* Import into DevLib.
00047  *#*
00048  *#* Revision 1.8  2005/04/12 01:18:09  bernie
00049  *#* time_t -> mtime_t.
00050  *#*
00051  *#* Revision 1.7  2005/03/20 04:18:41  bernie
00052  *#* Fixes for CONFIG_WATCHDOG == 0.
00053  *#*
00054  *#* Revision 1.6  2004/10/27 09:38:07  aleph
00055  *#* Bootloader working with watchdog enabled
00056  *#*
00057  *#* Revision 1.5  2004/10/20 10:00:37  customer_pw
00058  *#* Add newline at eof
00059  *#*
00060  *#* Revision 1.4  2004/10/14 14:13:09  batt
00061  *#* Add comment.
00062  *#*
00063  *#* Revision 1.3  2004/10/14 13:29:20  batt
00064  *#* Fix 0ms delay bug.
00065  *#*
00066  *#* Revision 1.2  2004/10/13 17:53:05  batt
00067  *#* Delay with hw timer.
00068  *#*
00069  *#* Revision 1.1  2004/10/13 16:36:32  batt
00070  *#* Simplified timer delay routines.
00071  *#*
00072  *#*/
00073 #include "hw_cpu.h"
00074 #include "timer_simple_avr.h"
00075 #include <drv/wdt.h>
00076 #include <cfg/compiler.h>
00077 #include <cfg/macros.h> /* BV() */
00078 #include <hw_cpu.h>  /* CLOCK_FREQ */
00079 
00080 #include <avr/io.h>
00081 
00082 
00083 #define MS_PER_SEC       1000UL
00084 #define TIMER_PRESCALER  64UL
00085 #define TIMER_DELAY_1MS  (255 - CLOCK_FREQ / TIMER_PRESCALER / MS_PER_SEC)
00086 
00091 void timer_delay(mtime_t time)
00092 {
00093     /* Set timer clock to clock_freq/64 */
00094     TCCR0 = BV(CS02);
00095 
00096     while (time--)
00097     {
00098         /* Initialize timer counter register */
00099         TCNT0 = TIMER_DELAY_1MS;
00100         /* Clear overflow bit. */
00101         TIFR |= BV(TOV0);
00102         /* Wait overflow. */
00103         while (!(TIFR & BV(TOV0)));
00104 #if CONFIG_WATCHDOG
00105         wdt_reset();
00106 #endif
00107     }
00108 }