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