ec263f7ecfe43334663795062159bd02bdf49810
[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.2  2006/07/19 12:56:26  bernie
18  *#* Convert to new Doxygen style.
19  *#*
20  *#* Revision 1.1  2005/04/12 01:37:50  bernie
21  *#* Import into DevLib.
22  *#*
23  *#* Revision 1.8  2005/04/12 01:18:09  bernie
24  *#* time_t -> mtime_t.
25  *#*
26  *#* Revision 1.7  2005/03/20 04:18:41  bernie
27  *#* Fixes for CONFIG_WATCHDOG == 0.
28  *#*
29  *#* Revision 1.6  2004/10/27 09:38:07  aleph
30  *#* Bootloader working with watchdog enabled
31  *#*
32  *#* Revision 1.5  2004/10/20 10:00:37  customer_pw
33  *#* Add newline at eof
34  *#*
35  *#* Revision 1.4  2004/10/14 14:13:09  batt
36  *#* Add comment.
37  *#*
38  *#* Revision 1.3  2004/10/14 13:29:20  batt
39  *#* Fix 0ms delay bug.
40  *#*
41  *#* Revision 1.2  2004/10/13 17:53:05  batt
42  *#* Delay with hw timer.
43  *#*
44  *#* Revision 1.1  2004/10/13 16:36:32  batt
45  *#* Simplified timer delay routines.
46  *#*
47  *#*/
48 #include "hw.h"
49 #include "timer_simple.h"
50 #include <drv/wdt.h>
51 #include <compiler.h>
52 #include <cpu.h>
53 #include <macros.h> /* BV() */
54
55 #include <avr/io.h>
56
57
58 #define MS_PER_SEC       1000UL
59 #define TIMER_PRESCALER  64UL
60 #define TIMER_DELAY_1MS  (255 - CLOCK_FREQ / TIMER_PRESCALER / MS_PER_SEC)
61
62 /**
63  * Wait \a time ms using timer 0.
64  *
65  */
66 void timer_delay(mtime_t time)
67 {
68         /* Set timer clock to clock_freq/64 */
69         TCCR0 = BV(CS02);
70
71         while (time--)
72         {
73                 /* Initialize timer counter register */
74                 TCNT0 = TIMER_DELAY_1MS;
75                 /* Clear overflow bit. */
76                 TIFR |= BV(TOV0);
77                 /* Wait overflow. */
78                 while (!(TIFR & BV(TOV0)));
79 #if CONFIG_WATCHDOG
80                 wdt_reset();
81 #endif
82         }
83 }