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