Update to new directory layout.
[bertos.git] / bertos / cpu / avr / drv / timer_simple_avr.c
1 /**
2  * \file
3  * <!--
4  * This file is part of BeRTOS.
5  *
6  * Bertos is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
19  *
20  * As a special exception, you may use this file as part of a free software
21  * library without restriction.  Specifically, if other files instantiate
22  * templates or use macros or inline functions from this file, or you compile
23  * this file and link it with other files to produce an executable, this
24  * file does not by itself cause the resulting executable to be covered by
25  * the GNU General Public License.  This exception does not however
26  * invalidate any other reasons why the executable file might be covered by
27  * the GNU General Public License.
28  *
29  * Copyright 2004 Develer S.r.l. (http://www.develer.com/)
30  * All Rights Reserved.
31  * -->
32  *
33  * \brief Some simple delay routines.
34  *
35  * Simple serial driver
36  * \version $Id$
37  * \author Francesco Sacchi <batt@develer.com>
38  */
39
40 /*#*
41  *#* $Log$
42  *#* Revision 1.2  2006/07/19 12:56:26  bernie
43  *#* Convert to new Doxygen style.
44  *#*
45  *#* Revision 1.1  2005/04/12 01:37:50  bernie
46  *#* Import into DevLib.
47  *#*
48  *#* Revision 1.8  2005/04/12 01:18:09  bernie
49  *#* time_t -> mtime_t.
50  *#*
51  *#* Revision 1.7  2005/03/20 04:18:41  bernie
52  *#* Fixes for CONFIG_WATCHDOG == 0.
53  *#*
54  *#* Revision 1.6  2004/10/27 09:38:07  aleph
55  *#* Bootloader working with watchdog enabled
56  *#*
57  *#* Revision 1.5  2004/10/20 10:00:37  customer_pw
58  *#* Add newline at eof
59  *#*
60  *#* Revision 1.4  2004/10/14 14:13:09  batt
61  *#* Add comment.
62  *#*
63  *#* Revision 1.3  2004/10/14 13:29:20  batt
64  *#* Fix 0ms delay bug.
65  *#*
66  *#* Revision 1.2  2004/10/13 17:53:05  batt
67  *#* Delay with hw timer.
68  *#*
69  *#* Revision 1.1  2004/10/13 16:36:32  batt
70  *#* Simplified timer delay routines.
71  *#*
72  *#*/
73 #include "hw_cpu.h"
74 #include "timer_simple_avr.h"
75 #include <drv/wdt.h>
76 #include <cfg/compiler.h>
77 #include <cfg/macros.h> /* BV() */
78 #include <hw_cpu.h>  /* CLOCK_FREQ */
79
80 #include <avr/io.h>
81
82
83 #define MS_PER_SEC       1000UL
84 #define TIMER_PRESCALER  64UL
85 #define TIMER_DELAY_1MS  (255 - CLOCK_FREQ / TIMER_PRESCALER / MS_PER_SEC)
86
87 /**
88  * Wait \a time ms using timer 0.
89  *
90  */
91 void timer_delay(mtime_t time)
92 {
93         /* Set timer clock to clock_freq/64 */
94         TCCR0 = BV(CS02);
95
96         while (time--)
97         {
98                 /* Initialize timer counter register */
99                 TCNT0 = TIMER_DELAY_1MS;
100                 /* Clear overflow bit. */
101                 TIFR |= BV(TOV0);
102                 /* Wait overflow. */
103                 while (!(TIFR & BV(TOV0)));
104 #if CONFIG_WATCHDOG
105                 wdt_reset();
106 #endif
107         }
108 }