570ae42945b04b29939869ba6bce47f54829fa07
[bertos.git] / drv / timer_simple.c
1 /*!
2  * \file
3  * <!--
4  * Copyright 2004 Develer S.r.l. (http://www.develer.com/)
5  * All Rights Reserved.
6  * -->
7  *
8  * \brief Some simple delay routines.
9  *
10  * Simple serial driver
11  * \version $Id$
12  * \author Francesco Sacchi <batt@develer.com>
13  */
14
15 /*#*
16  *#* $Log$
17  *#* Revision 1.1  2005/04/12 01:37:50  bernie
18  *#* Import into DevLib.
19  *#*
20  *#* Revision 1.8  2005/04/12 01:18:09  bernie
21  *#* time_t -> mtime_t.
22  *#*
23  *#* Revision 1.7  2005/03/20 04:18:41  bernie
24  *#* Fixes for CONFIG_WATCHDOG == 0.
25  *#*
26  *#* Revision 1.6  2004/10/27 09:38:07  aleph
27  *#* Bootloader working with watchdog enabled
28  *#*
29  *#* Revision 1.5  2004/10/20 10:00:37  customer_pw
30  *#* Add newline at eof
31  *#*
32  *#* Revision 1.4  2004/10/14 14:13:09  batt
33  *#* Add comment.
34  *#*
35  *#* Revision 1.3  2004/10/14 13:29:20  batt
36  *#* Fix 0ms delay bug.
37  *#*
38  *#* Revision 1.2  2004/10/13 17:53:05  batt
39  *#* Delay with hw timer.
40  *#*
41  *#* Revision 1.1  2004/10/13 16:36:32  batt
42  *#* Simplified timer delay routines.
43  *#*
44  *#*/
45 #include "hw.h"
46 #include "timer_simple.h"
47 #include <drv/wdt.h>
48 #include <compiler.h>
49 #include <cpu.h>
50 #include <macros.h> /* BV() */
51
52 #include <avr/io.h>
53
54
55 #define MS_PER_SEC       1000UL
56 #define TIMER_PRESCALER  64UL
57 #define TIMER_DELAY_1MS  (255 - CLOCK_FREQ / TIMER_PRESCALER / MS_PER_SEC)
58
59 /*!
60  * Wait \a time ms using timer 0.
61  *
62  */
63 void timer_delay(mtime_t time)
64 {
65         /* Set timer clock to clock_freq/64 */
66         TCCR0 = BV(CS02);
67
68         while (time--)
69         {
70                 /* Initialize timer counter register */
71                 TCNT0 = TIMER_DELAY_1MS;
72                 /* Clear overflow bit. */
73                 TIFR |= BV(TOV0);
74                 /* Wait overflow. */
75                 while (!(TIFR & BV(TOV0)));
76 #if CONFIG_WATCHDOG
77                 wdt_reset();
78 #endif
79         }
80 }