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