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