2 * Copyright Droids Corporation, Microb Technology, Eirbot (2006)
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 * Revision : $Id: timer_definitions.h,v 1.1.2.5 2009-04-07 20:00:46 zer0 Exp $
22 #ifndef _TIMER_DEFINITIONS_H_
23 #define _TIMER_DEFINITIONS_H_
25 /* needed by nearly all .c */
26 #include <aversive/parts.h>
28 #define DEFINE_TIMER_START_STOP(x) \
30 /** start the timer */ \
31 void timer##x##_start(void) \
34 CS##x##0_REG = __timer##x##_div_to_reg(TIMER##x##_PRESCALER_DIV) << CS##x##0 ; \
37 /** stop the timer */ \
38 void timer##x##_stop(void) \
46 #define DEFINE_TIMER_GET_SET(x) \
48 uint16_t timer##x##_get(void) \
53 void timer##x##_set(uint16_t t) \
59 #define DEFINE_OV_INTR(x) \
62 if(timer_OV_callback_table[x##_NUM]) \
63 timer_OV_callback_table[x##_NUM](); \
67 #define DEFINE_OC_INTR(x) \
70 if(timer_OC_callback_table[x##_NUM]) \
71 timer_OC_callback_table[x##_NUM](); \
75 #define DEFINE_REGISTER_OV_INTR(x) \
77 void timer##x##_register_OV_intr(void (*func)(void)) \
82 timer_OV_callback_table[SIG_OVERFLOW##x##_NUM] = func; \
84 TOIE##x##_REG |= (1<<TOIE##x); \
87 TOIE##x##_REG &= (uint8_t)(~(1<<TOIE##x)); \
93 #define DEFINE_REGISTER_OC_INTR_AT_TICS(x) \
95 void timer##x##_register_OC_intr_at_tics(void (*func)(void), uint16_t t) \
100 timer_OC_callback_table[SIG_OUTPUT_COMPARE##x##_NUM] = func; \
102 OCIE##x##_REG |= (1<<OCIE##x); \
106 OCIE##x##_REG &= (uint8_t)(~(1<<OCIE##x)); \
112 #define DEFINE_REGISTER_OC_INTR_IN_US(x,y) \
114 int8_t timer##y##_register_OC_intr_in_us(void (*func)(void), uint16_t t) \
121 timer_OC_callback_table[SIG_OUTPUT_COMPARE##y##_NUM] = func; \
122 OCIE##y##_REG &= (uint8_t)(~(1<<OCIE##y)); \
127 tics = timer##x##_us_to_tics(t); \
128 if ( tics > 0xFFFF ) { /* XXX use MAX_TIMER */ \
133 OCR##y = TCNT##x + tics; \
134 timer_OC_callback_table[SIG_OUTPUT_COMPARE##y##_NUM] = func; \
135 OCIE##y##_REG |= (1<<OCIE##y); \
141 #define DEFINE_DYNAMIC_PRESCALER_FUNCS(x) \
143 int16_t timer##x##_div_to_reg(uint16_t div) \
145 return __timer##x##_div_to_reg(div); \
148 int16_t timer##x##_reg_to_div(uint8_t reg) \
150 return __timer##x##_reg_to_div(reg); \
153 uint16_t timer##x##_get_prescaler_div(void) \
155 return __timer##x##_reg_to_div(CS##x##0_REG >> CS##x##0); \
158 void timer##x##_set_prescaler_div(uint16_t div) \
160 CS##x##0_REG = __timer##x##_div_to_reg(div) << CS##x##0 ; \
164 #define DEFINE_STATIC_PRESCALER_FUNCS(x) \
166 int16_t timer##x##_div_to_reg(__attribute__((unused)) uint16_t div) \
168 return __timer##x##_div_to_reg(TIMER##x##_PRESCALER_DIV); \
171 uint16_t timer##x##_get_prescaler_div(void) \
173 return TIMER##x##_PRESCALER_DIV; \
176 #define DEFINE_TIMER_US_CONVERSIONS(x) \
178 static inline float timer##x##_us_to_tics(float us) \
180 return ((float)CONFIG_QUARTZ / \
181 ((float)MHz * timer##x##_get_prescaler_div()) ) * us; \
184 static inline float timer##x##_tics_to_us(float t) \
186 return t / ((float)CONFIG_QUARTZ / \
187 ((float)MHz * timer##x##_get_prescaler_div()) ); \