Contiki-Inga 3.x
clock-avr.h
1 #ifndef CONTIKI_CLOCK_AVR_H
2 #define CONTIKI_CLOCK_AVR_H
3 
4 #if defined (__AVR_ATmega128__)
5 #define AVR_OUTPUT_COMPARE_INT TIMER0_COMP_vect
6 #define AVR_CLOCK_COUNTER TCNT0
7 #define AVR_CLOCK_MAX (F_CPU/1024UL/CLOCK_CONF_SECOND)
8 
9 #define OCRSetup() \
10  /* Select internal clock */ \
11  ASSR = 0x00; \
12 \
13  /* Set counter to zero */ \
14  AVR_CLOCK_COUNTER = 0; \
15 \
16  /* \
17  * Set comparison register: \
18  * Crystal freq. is F_CPU,\
19  * pre-scale factor is 1024, we want CLOCK_CONF_SECOND ticks / sec: \
20  * F_CPU = 1024 * CLOCK_CONF_SECOND * OCR0 \
21  */ \
22  OCR0 = AVR_CLOCK_MAX; \
23 \
24  /* \
25  * Set timer control register: \
26  * - prescale: 1024 (CS00 - CS02) \
27  * - counter reset via comparison register (WGM01) \
28  */ \
29  TCCR0 = _BV(CS00) | _BV(CS01) | _BV(CS02) | _BV(WGM01); \
30 \
31  /* Clear interrupt flag register */ \
32  TIFR = 0x00; \
33 \
34  /* \
35  * Raise interrupt when value in OCR0 is reached. Note that the \
36  * counter value in TCNT0 is cleared automatically. \
37  */ \
38  TIMSK = _BV (OCIE0);
39 
40 #elif defined (__AVR_ATmega128RFA1__) && 0
41 /* Uses the general routine below at present */
42 
43 #define AVR_OUTPUT_COMPARE_INT TIMER0_COMPA_vect
44 #define AVR_CLOCK_COUNTER TCNT0
45 #define AVR_CLOCK_MAX (F_CPU/1024/CLOCK_CONF_SECOND - 1)
46 
47 #define OCRSetup() \
48  /* Select internal clock */ \
49  ASSR = 0x00; \
50 \
51  /* Set counter to zero */ \
52  AVR_CLOCK_COUNTER = 0; \
53 \
54  /* \
55  * Set comparison register: \
56  * Crystal freq. is F_CPU,\
57  * pre-scale factor is 1024, we want CLOCK_CONF_SECOND ticks / sec: \
58  * F_CPU = 1024 * CLOCK_CONF_SECOND * OCR0A, less 1 for CTC mode \
59  */ \
60  OCR0A = AVR_CLOCK_MAX; \
61 \
62  /* \
63  * Set timer control register: \
64  * - prescale: 1024 (CS00 - CS02) \
65  * - counter reset via comparison register (WGM01) \
66  */ \
67  TCCR0A = _BV(WGM01); \
68  TCCR0B = _BV(CS00) | _BV(CS02); \
69 \
70  /* Clear interrupt flag register */ \
71  TIFR0 = TIFR0; \
72 \
73  /* \
74  * Raise interrupt when value in OCR0 is reached. Note that the \
75  * counter value in TCNT0 is cleared automatically. \
76  */ \
77  TIMSK0 = _BV (OCIE0A);
78 
79 
80 #elif defined (__AVR_ATmega1284P__) || (__AVR_AT90USB1287__) || (__AVR_ATmega1281__) || defined (__AVR_ATmega128RFA1__)
81 /*
82  The Raven has a 32768Hz watch crystal that can be used to clock the timer
83  while the 1284p is sleeping. The Jackdaw has pads for a crystal. The crystal
84  can be used to clock the 8 bit timer2.
85  The 1284p routine also uses TIMER2 to sleep a variable number of seconds.
86  It restores the values here after a wake.
87 */
88 #if AVR_CONF_USE32KCRYSTAL
89 #define AVR_OUTPUT_COMPARE_INT TIMER2_COMPA_vect
90 #define AVR_CLOCK_COUNTER TCNT2
91 #define AVR_CLOCK_MAX (32768/8/CLOCK_CONF_SECOND - 1)
92 
93 #define OCRSetup() \
94  /* Clock from crystal on TOSC0-1 */ \
95  ASSR = _BV(AS2); \
96 \
97  /* Set counter to zero */ \
98  AVR_CLOCK_COUNTER = 0; \
99 \
100  /* \
101  * Set comparison register: \
102  * Crystal freq. is 32768,\
103  * pre-scale factor is 8, we want CLOCK_CONF_SECOND ticks / sec: \
104  * 32768 = 8 * CLOCK_CONF_SECOND * OCR2A, less 1 for CTC mode\
105  */ \
106  OCR2A = AVR_CLOCK_MAX; \
107 \
108  /* \
109  * Set timer control register: \
110  * - prescale: 8 (CS21) \
111  * - counter reset via comparison register (WGM21) \
112  */ \
113  TCCR2A = _BV(WGM21); \
114  TCCR2B = _BV(CS21); \
115 \
116  /* Clear interrupt flag register */ \
117  TIFR2 = TIFR2; \
118 \
119  /* \
120  * Raise interrupt when value in OCR2 is reached. Note that the \
121  * counter value in TCNT2 is cleared automatically. \
122  */ \
123  TIMSK2 = _BV (OCIE2A);
124 #else /* !AVR_CONF_USE32KCRYSTAL */
125 
126 /* Determine the largest value that can be used with 8 bit timer0 */
127 #ifndef F_CPU
128 #error "Please define CPU clock speed for your platform. #define F_CPU 8000000UL is typical."
129 #endif
130 #if CLOCK_CONF_SECOND == 0
131 #error "Please define timer ticks per second for your platform. #define CLOCK_CONF_SECOND 128 is typical."
132 #endif
133 
134 #ifdef AVR_CONF_TMR0_PRESCALE
135 #elif F_CPU/CLOCK_CONF_SECOND < 256
136  #define AVR_CONF_TMR0_PRESCALE 1
137 #elif F_CPU/CLOCK_CONF_SECOND < 256 * 8
138  #define AVR_CONF_TMR0_PRESCALE 8
139 #elif F_CPU/CLOCK_CONF_SECOND < 256 * 64
140  #define AVR_CONF_TMR0_PRESCALE 64
141 #elif F_CPU/CLOCK_CONF_SECOND < 256 * 256
142  #define AVR_CONF_TMR0_PRESCALE 256
143 #else
144  #define AVR_CONF_TMR0_PRESCALE 1024
145 #endif
146 
147 #if F_CPU/CLOCK_CONF_SECOND/AVR_CONF_TMR0_PRESCALE > 255
148 #error "Can not prescale CPU clock to get specified ticks per second. F_CPU/CLOCK_CONF_SECOND/1024 must be less than 256."
149 #endif
150 
151 #if AVR_CONF_TMR0_PRESCALE == 1
152  #define AVR_TCCR0B_CONF _BV(CS00)
153 #elif AVR_CONF_TMR0_PRESCALE == 8
154  #define AVR_TCCR0B_CONF _BV(CS01)
155 #elif AVR_CONF_TMR0_PRESCALE == 64
156  #define AVR_TCCR0B_CONF _BV(CS01) | _BV(CS00)
157 #elif AVR_CONF_TMR0_PRESCALE == 256
158  #define AVR_TCCR0B_CONF _BV(CS02)
159 #elif AVR_CONF_TMR0_PRESCALE == 1024
160  #define AVR_TCCR0B_CONF _BV(CS02) | _BV(CS00)
161 #else
162 #error "Prescale factor not supported. Allowed values are 1,8,64,256,1024."
163 #endif
164 
165 #define AVR_OUTPUT_COMPARE_INT TIMER0_COMPA_vect
166 #define AVR_CLOCK_COUNTER TCNT0
167 #define AVR_CLOCK_MAX (F_CPU/AVR_CONF_TMR0_PRESCALE/CLOCK_CONF_SECOND - 1)
168 
169 #define OCRSetup() \
170  /* Select internal clock */ \
171  ASSR = 0x00; \
172 \
173  /* Set counter to zero */ \
174  AVR_CLOCK_COUNTER = 0; \
175 \
176  /* \
177  * Set comparison register: \
178  * Crystal freq. is F_CPU, prescale is given, \
179  * We want CLOCK_CONF_SECOND ticks / sec: \
180  * F_CPU = AVR_CONF_TMR0_PRESCALE * CLOCK_CONF_SECOND * OCR2A, less 1 for CTC mode \
181  */ \
182  OCR0A = AVR_CLOCK_MAX; \
183 \
184  /* \
185  * Set timer control register: \
186  * - prescale according to AVR_CONF_TMR0_PRESCALE \
187  * - counter reset via comparison register (WGM01) \
188  */ \
189  TCCR0A = _BV(WGM01); \
190  TCCR0B = AVR_TCCR0B_CONF; \
191 \
192  /* Clear interrupt flag register */ \
193  TIFR0 = TIFR0; \
194 \
195  /* \
196  * Raise interrupt when value in OCR0 is reached. Note that the \
197  * counter value in TCNT0 is cleared automatically. \
198  */ \
199  TIMSK0 = _BV (OCIE0A);
200 #endif /* AVR_CONF_USE32KCRYSTAL */
201 
202 #elif defined (__AVR_ATmega644__) || defined (__AVR_ATmega328P__)
203 
204 #define AVR_CLOCK_COUNTER TCNT0
205 #define AVR_CLOCK_MAX (F_CPU/256UL/CLOCK_CONF_SECOND - 1)
206 
207 #define OCRSetup() \
208  /* Set counter to zero */ \
209  AVR_CLOCK_COUNTER = 0; \
210 \
211  /* \
212  * Set comparison register: \
213  * Crystal freq. is F_CPU,\
214  * pre-scale factor is 256, want CLOCK_CONF_SECOND ticks / sec: \
215  */ \
216  OCR0A = AVR_CLOCK_MAX; \
217 \
218  /* \
219  * Set timer control register: \
220  * - prescale: 256 (CS02) \
221  * - counter reset via comparison register (WGM01) \
222  */ \
223  TCCR0A = _BV(WGM01); \
224  TCCR0B = _BV(CS02); \
225 \
226  /* Clear interrupt flag register */ \
227  TIFR0 = 0x00; \
228 \
229  /* \
230  * Raise interrupt when value in OCR0 is reached. Note that the \
231  * counter value in TCNT0 is cleared automatically. \
232  */ \
233  TIMSK0 = _BV (OCIE0A);
234 
235 #define AVR_OUTPUT_COMPARE_INT TIMER0_COMPA_vect
236 
237 #elif defined (__AVR_ATmega8515__) || defined (__AVR_ATmega16__) || defined (__AVR_ATmega32__)
238 
239 #define AVR_OUTPUT_COMPARE_INT TIMER0_COMP_vect
240 #define AVR_CLOCK_COUNTER TCNT0
241 #define AVR_CLOCK_MAX (F_CPU/256UL/CLOCK_CONF_SECOND)
242 
243 #define OCRSetup() \
244  /* Set counter to zero */ \
245  AVR_CLOCK_COUNTER = 0; \
246 \
247  /* \
248  * Set comparison register: \
249  * Crystal freq. is F_CPU,\
250  * pre-scale factor is 256, we want CLOCK_CONF_SECOND ticks / sec: \
251  * F_CPU = 256 * CLOCK_CONF_SECOND * OCR0 \
252  */ \
253  OCR0 = AVR_CLOCK_MAX; \
254 \
255  /* \
256  * Set timer control register: \
257  * - prescale: 256 (CS02) \
258  * - counter reset via comparison register (WGM01) \
259  */ \
260  TCCR0 = _BV(CS02) | _BV(WGM01); \
261 \
262  /* Clear interrupt flag register */ \
263  TIFR = 0x00; \
264 \
265  /* \
266  * Raise interrupt when value in OCR0 is reached. Note that the \
267  * counter value in TCNT0 is cleared automatically. \
268  */ \
269  TIMSK = _BV (OCIE0);
270 
271 #elif defined (__AVR_ATmega8__)
272 
273 #define AVR_CLOCK_COUNTER TCNT2
274 #define AVR_CLOCK_MAX (F_CPU/256UL/CLOCK_CONF_SECOND)
275 #define AVR_OUTPUT_COMPARE_INT TIMER2_COMP_vect
276 
277 #define OCRSetup() \
278  /* Set counter to zero */ \
279  AVR_CLOCK_COUNTER = 0; \
280 \
281  /* \
282  * Set comparison register: \
283  * Crystal freq. is F_CPU,\
284  * pre-scale factor is 256, we want CLOCK_CONF_SECOND ticks / sec: \
285  * F_CPU = 256 * CLOCK_CONF_SECOND * OCR2 \
286  */ \
287  OCR2 = AVR_CLOCK_MAX; \
288 \
289  /* \
290  * Set timer control register: \
291  * - prescale: 256 (CS21 CS22) \
292  * - counter reset via comparison register (WGM21) \
293  */ \
294  TCCR2 = _BV(CS22) | _BV(CS21) | _BV(WGM21); \
295 \
296  /* Clear interrupt flag register */ \
297  TIFR = 0x00; \
298 \
299  /* \
300  * Raise interrupt when value in OCR2 is reached. Note that the \
301  * counter value in TCNT2 is cleared automatically. \
302  */ \
303  TIMSK = _BV (OCIE2);
304 #elif defined(__AVR_XMEGA__)
305 
306 #include "contiki-conf.h"
307 #include "xmega_timer.h"
308 
309 // If XMEGA_TIMER_RTC is not defined, we use TCC0
310 #if (!defined(XMEGA_TIMER_RTC) || XMEGA_TIMER_RTC == 1)
311  #define AVR_OUTPUT_COMPARE_INT TCC0_OVF_vect
312 // Else we use RTC OVF only, if we are not using the event system
313 #else
314  #define AVR_OUTPUT_COMPARE_INT RTC_COMP_vect
315 #endif
316 
317 #define OCRSetup() \
318  TCC0.PER = TIMER_TOP; \
319  TCC0.INTCTRLA = TC0_OVFINTLVL_gm; \
320  TCC0.CTRLA = TIMER_PRESCALE;
321 #else
322 #error "Setup CPU in clock-avr.h"
323 #endif
324 
325 #endif //CONTIKI_CLOCK_AVR_H