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