1 #ifndef CONTIKI_CLOCK_AVR_H
2 #define CONTIKI_CLOCK_AVR_H
4 #if defined (__AVR_ATmega128__)
6 #define AVR_OUTPUT_COMPARE_INT TIMER0_COMP_vect
7 #define AVR_CLOCK_COUNTER TCNT0
8 #define AVR_CLOCK_MAX (F_CPU/1024UL/CLOCK_CONF_SECOND)
15 AVR_CLOCK_COUNTER = 0; \
23 OCR0 = AVR_CLOCK_MAX; \
30 TCCR0 = _BV(CS00) | _BV(CS01) | _BV(CS02) | _BV(WGM01); \
41 #elif defined (__AVR_ATmega128RFA1__) && 0
44 #define AVR_OUTPUT_COMPARE_INT TIMER0_COMPA_vect
45 #define AVR_CLOCK_COUNTER TCNT0
46 #define AVR_CLOCK_MAX (F_CPU/1024/CLOCK_CONF_SECOND - 1)
53 AVR_CLOCK_COUNTER = 0; \
61 OCR0A = AVR_CLOCK_MAX; \
68 TCCR0A = _BV(WGM01); \
69 TCCR0B = _BV(CS00) | _BV(CS02); \
78 TIMSK0 = _BV (OCIE0A);
81 #elif defined (__AVR_ATmega1284P__) || (__AVR_AT90USB1287__) || (__AVR_ATmega1281__) || defined (__AVR_ATmega128RFA1__)
89 #if AVR_CONF_USE32KCRYSTAL
90 #define AVR_OUTPUT_COMPARE_INT TIMER2_COMPA_vect
91 #define AVR_CLOCK_COUNTER TCNT2
92 #define AVR_CLOCK_MAX (32768/8/CLOCK_CONF_SECOND - 1)
99 AVR_CLOCK_COUNTER = 0; \
107 OCR2A = AVR_CLOCK_MAX; \
114 TCCR2A = _BV(WGM21); \
115 TCCR2B = _BV(CS21); \
124 TIMSK2 = _BV (OCIE2A);
129 #error "Please define CPU clock speed for your platform. #define F_CPU 8000000UL is typical."
131 #if CLOCK_CONF_SECOND == 0
132 #error "Please define timer ticks per second for your platform. #define CLOCK_CONF_SECOND 128 is typical."
135 #ifdef AVR_CONF_TMR0_PRESCALE
136 #elif F_CPU/CLOCK_CONF_SECOND < 256
137 #define AVR_CONF_TMR0_PRESCALE 1
138 #elif F_CPU/CLOCK_CONF_SECOND < 256 * 8
139 #define AVR_CONF_TMR0_PRESCALE 8
140 #elif F_CPU/CLOCK_CONF_SECOND < 256 * 64
141 #define AVR_CONF_TMR0_PRESCALE 64
142 #elif F_CPU/CLOCK_CONF_SECOND < 256 * 256
143 #define AVR_CONF_TMR0_PRESCALE 256
145 #define AVR_CONF_TMR0_PRESCALE 1024
148 #if F_CPU/CLOCK_CONF_SECOND/AVR_CONF_TMR0_PRESCALE > 255
149 #error "Can not prescale CPU clock to get specified ticks per second. F_CPU/CLOCK_CONF_SECOND/1024 must be less than 256."
152 #if AVR_CONF_TMR0_PRESCALE == 1
153 #define AVR_TCCR0B_CONF _BV(CS00)
154 #elif AVR_CONF_TMR0_PRESCALE == 8
155 #define AVR_TCCR0B_CONF _BV(CS01)
156 #elif AVR_CONF_TMR0_PRESCALE == 64
157 #define AVR_TCCR0B_CONF _BV(CS01) | _BV(CS00)
158 #elif AVR_CONF_TMR0_PRESCALE == 256
159 #define AVR_TCCR0B_CONF _BV(CS02)
160 #elif AVR_CONF_TMR0_PRESCALE == 1024
161 #define AVR_TCCR0B_CONF _BV(CS02) | _BV(CS00)
163 #error "Prescale factor not supported. Allowed values are 1,8,64,256,1024."
166 #define AVR_OUTPUT_COMPARE_INT TIMER0_COMPA_vect
167 #define AVR_CLOCK_COUNTER TCNT0
168 #define AVR_CLOCK_MAX (F_CPU/AVR_CONF_TMR0_PRESCALE/CLOCK_CONF_SECOND - 1)
175 AVR_CLOCK_COUNTER = 0; \
183 OCR0A = AVR_CLOCK_MAX; \
190 TCCR0A = _BV(WGM01); \
191 TCCR0B = AVR_TCCR0B_CONF; \
200 TIMSK0 = _BV (OCIE0A);
203 #elif defined (__AVR_ATmega644__) || defined (__AVR_ATmega328P__)
205 #define AVR_CLOCK_COUNTER TCNT0
206 #define AVR_CLOCK_MAX (F_CPU/256UL/CLOCK_CONF_SECOND - 1)
210 AVR_CLOCK_COUNTER = 0; \
217 OCR0A = AVR_CLOCK_MAX; \
224 TCCR0A = _BV(WGM01); \
225 TCCR0B = _BV(CS02); \
234 TIMSK0 = _BV (OCIE0A);
236 #define AVR_OUTPUT_COMPARE_INT TIMER0_COMPA_vect
238 #elif defined (__AVR_ATmega8515__) || defined (__AVR_ATmega16__) || defined (__AVR_ATmega32__)
240 #define AVR_OUTPUT_COMPARE_INT TIMER0_COMP_vect
241 #define AVR_CLOCK_COUNTER TCNT0
242 #define AVR_CLOCK_MAX (F_CPU/256UL/CLOCK_CONF_SECOND)
246 AVR_CLOCK_COUNTER = 0; \
254 OCR0 = AVR_CLOCK_MAX; \
261 TCCR0 = _BV(CS02) | _BV(WGM01); \
272 #elif defined (__AVR_ATmega8__)
274 #define AVR_CLOCK_COUNTER TCNT2
275 #define AVR_CLOCK_MAX (F_CPU/256UL/CLOCK_CONF_SECOND)
276 #define AVR_OUTPUT_COMPARE_INT TIMER2_COMP_vect
280 AVR_CLOCK_COUNTER = 0; \
288 OCR2 = AVR_CLOCK_MAX; \
295 TCCR2 = _BV(CS22) | _BV(CS21) | _BV(WGM21); \
306 #error "Setup CPU in clock-avr.h"
309 #endif //CONTIKI_CLOCK_AVR_H