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