65e20617b7c9a21dcdc081e3826d854d9b7530e5
[bertos.git] / drv / timer_avr.h
1 /**
2  * \file
3  * <!--
4  * Copyright 2003, 2004, 2005 Develer S.r.l. (http://www.develer.com/)
5  * Copyright 2000 Bernardo Innocenti <bernie@develer.com>
6  * This file is part of DevLib - See README.devlib for information.
7  * -->
8  *
9  * \version $Id$
10  *
11  * \author Bernardo Innocenti <bernie@develer.com>
12  * \author Francesco Sacchi <batt@develer.com>
13  *
14  * \brief Low-level timer module for AVR (interface).
15  */
16
17 /*#*
18  *#* $Log$
19  *#* Revision 1.31  2007/10/07 12:30:55  batt
20  *#* Add default timer for AVR.
21  *#*
22  *#* Revision 1.30  2007/06/07 14:35:12  batt
23  *#* Merge from project_ks.
24  *#*
25  *#* Revision 1.29  2007/03/21 11:01:36  batt
26  *#* Add missing support for ATMega1281.
27  *#*
28  *#* Revision 1.28  2006/07/19 12:56:26  bernie
29  *#* Convert to new Doxygen style.
30  *#*
31  *#* Revision 1.27  2006/05/18 00:38:24  bernie
32  *#* Use hw_cpu.h instead of ubiquitous hw.h.
33  *#*
34  *#* Revision 1.26  2006/02/21 21:28:02  bernie
35  *#* New time handling based on TIMER_TICKS_PER_SEC to support slow timers with ticks longer than 1ms.
36  *#*
37  *#* Revision 1.25  2005/07/19 07:26:37  bernie
38  *#* Refactor to decouple timer ticks from milliseconds.
39  *#*
40  *#* Revision 1.24  2005/04/11 19:10:28  bernie
41  *#* Include top-level headers from cfg/ subdir.
42  *#*
43  *#* Revision 1.23  2005/03/01 23:24:51  bernie
44  *#* Tweaks for avr-libc 1.2.x.
45  *#*
46  *#* Revision 1.21  2004/12/13 12:07:06  bernie
47  *#* DISABLE_IRQSAVE/ENABLE_IRQRESTORE: Convert to IRQ_SAVE_DISABLE/IRQ_RESTORE.
48  *#*
49  *#* Revision 1.20  2004/11/16 20:59:46  bernie
50  *#* Include <avr/io.h> explicitly.
51  *#*
52  *#* Revision 1.19  2004/10/19 08:56:41  bernie
53  *#* TIMER_STROBE_ON, TIMER_STROBE_OFF, TIMER_STROBE_INIT: Move from timer_avr.h to timer.h, where they really belong.
54  *#*
55  *#* Revision 1.18  2004/09/20 03:31:03  bernie
56  *#* Fix racy racy code.
57  *#*/
58 #ifndef DRV_TIMER_AVR_H
59 #define DRV_TIMER_AVR_H
60
61 #include <appconfig.h>     /* CONFIG_TIMER */
62 #include <cfg/compiler.h>  /* uint8_t */
63 #include <hw_cpu.h>        /* CLOCK_FREQ */
64
65 /**
66  * \name Values for CONFIG_TIMER.
67  *
68  * Select which hardware timer interrupt to use for system clock and softtimers.
69  * \note The timer 1 overflow mode set the timer as a 24 kHz PWM.
70  *
71  * \{
72  */
73 #define TIMER_ON_OUTPUT_COMPARE0  1
74 #define TIMER_ON_OVERFLOW1        2
75 #define TIMER_ON_OUTPUT_COMPARE2  3
76 #define TIMER_ON_OVERFLOW3        4
77
78 #define TIMER_DEFAULT TIMER_ON_OUTPUT_COMPARE0 ///< Default system timer
79 /* \} */
80
81 /*
82  * Hardware dependent timer initialization.
83  */
84 #if (CONFIG_TIMER == TIMER_ON_OUTPUT_COMPARE0)
85
86         #define TIMER_PRESCALER      64
87         #define TIMER_HW_BITS        8
88         #define DEFINE_TIMER_ISR     SIGNAL(SIG_OUTPUT_COMPARE0)
89         #define TIMER_TICKS_PER_SEC  1000
90         #define TIMER_HW_CNT         OCR_DIVISOR
91
92         /// Type of time expressed in ticks of the hardware high-precision timer
93         typedef uint8_t hptime_t;
94
95 #elif (CONFIG_TIMER == TIMER_ON_OVERFLOW1)
96
97         #define TIMER_PRESCALER      1
98         #define TIMER_HW_BITS        8
99         /** This value is the maximum in overflow based timers. */
100         #define TIMER_HW_CNT         (1 << TIMER_HW_BITS)
101         #define DEFINE_TIMER_ISR     SIGNAL(SIG_OVERFLOW1)
102         #define TIMER_TICKS_PER_SEC  ((TIMER_HW_HPTICKS_PER_SEC + TIMER_HW_CNT / 2) / TIMER_HW_CNT)
103
104         /// Type of time expressed in ticks of the hardware high precision timer
105         typedef uint16_t hptime_t;
106
107 #elif (CONFIG_TIMER == TIMER_ON_OUTPUT_COMPARE2)
108
109         #define TIMER_PRESCALER      64
110         #define TIMER_HW_BITS        8
111         #if CPU_AVR_ATMEGA1281 || CPU_AVR_ATMEGA168
112                 #define DEFINE_TIMER_ISR     SIGNAL(SIG_OUTPUT_COMPARE2A)
113         #else
114                 #define DEFINE_TIMER_ISR     SIGNAL(SIG_OUTPUT_COMPARE2)
115         #endif
116         #define TIMER_TICKS_PER_SEC  1000
117         /** Value for OCR register in output-compare based timers. */
118         #define TIMER_HW_CNT         OCR_DIVISOR
119
120
121         /// Type of time expressed in ticks of the hardware high precision timer
122         typedef uint8_t hptime_t;
123
124 #elif (CONFIG_TIMER == TIMER_ON_OVERFLOW3)
125
126         #define TIMER_PRESCALER      1
127         #define TIMER_HW_BITS        8
128         /** This value is the maximum in overflow based timers. */
129         #define TIMER_HW_CNT         (1 << TIMER_HW_BITS)
130         #define DEFINE_TIMER_ISR     SIGNAL(SIG_OVERFLOW3)
131         #define TIMER_TICKS_PER_SEC  ((TIMER_HW_HPTICKS_PER_SEC + TIMER_HW_CNT / 2) / TIMER_HW_CNT)
132
133         /// Type of time expressed in ticks of the hardware high precision timer
134         typedef uint16_t hptime_t;
135 #else
136
137         #error Unimplemented value for CONFIG_TIMER
138 #endif /* CONFIG_TIMER */
139
140
141 /** Frequency of the hardware high precision timer. */
142 #define TIMER_HW_HPTICKS_PER_SEC  ((CLOCK_FREQ + TIMER_PRESCALER / 2) / TIMER_PRESCALER)
143
144 /**
145  * System timer: additional division after the prescaler
146  * 12288000 / 64 / 192 (0..191) = 1 ms
147  */
148 #define OCR_DIVISOR  (((CLOCK_FREQ + TIMER_PRESCALER / 2) / TIMER_PRESCALER + TIMER_TICKS_PER_SEC / 2) / TIMER_TICKS_PER_SEC - 1)
149
150 /** Not needed, IRQ timer flag cleared automatically */
151 #define timer_hw_irq() do {} while (0)
152
153
154 #endif /* DRV_TIMER_AVR_H */