clock-avr.h

00001 #ifndef CONTIKI_CLOCK_AVR_H
00002 #define CONTIKI_CLOCK_AVR_H
00003 
00004 #if defined (__AVR_ATmega128__)
00005 
00006 #define AVR_OUTPUT_COMPARE_INT TIMER0_COMP_vect
00007 
00008 #define OCRSetup() \
00009   /* Select internal clock */ \
00010   ASSR = 0x00;                            \
00011 \
00012   /* Set counter to zero */   \
00013   TCNT0 = 0;                              \
00014 \
00015   /*                                              \
00016    * Set comparison register: \
00017    * Crystal freq. is 16000000,\
00018    * pre-scale factor is 1024, i.e. we have 125 "ticks" / sec: \
00019    * 16000000 = 1024 * 125 * 125 \
00020    */ \
00021   OCR0 = 125; \
00022 \
00023   /*                                                            \
00024    * Set timer control register:        \
00025    *  - prescale: 1024 (CS00 - CS02) \
00026    *  - counter reset via comparison register (WGM01) \
00027    */                                                           \
00028   TCCR0 =  _BV(CS00) | _BV(CS01) |  _BV(CS02) |  _BV(WGM01); \
00029 \
00030   /* Clear interrupt flag register */ \
00031   TIFR = 0x00; \
00032 \
00033   /* \
00034    * Raise interrupt when value in OCR0 is reached. Note that the \
00035    * counter value in TCNT0 is cleared automatically. \
00036    */ \
00037   TIMSK = _BV (OCIE0);
00038 
00039 #elif defined (__AVR_ATmega128RFA1__) && 0
00040 
00041 #define AVR_OUTPUT_COMPARE_INT TIMER0_COMPA_vect
00042 #define OCRSetup() \
00043   /* Select internal clock */ \
00044   ASSR = 0x00;                            \
00045 \
00046   /* Set counter to zero */   \
00047   TCNT0 = 0;                              \
00048 \
00049   /*                                              \
00050    * Set comparison register: \
00051    * Crystal freq. is 8000000,\
00052    * pre-scale factor is 1024, we want 125 ticks / sec: \
00053    * 8000000 = 1024 * 126.01 * 62, less 1 for CTC mode \
00054    */ \
00055   OCR0A = 61; \
00056 \
00057   /*                                                            \
00058    * Set timer control register:        \
00059    *  - prescale: 1024 (CS00 - CS02) \
00060    *  - counter reset via comparison register (WGM01) \
00061    */                                                           \
00062   TCCR0A = _BV(WGM01); \
00063   TCCR0B =  _BV(CS00) | _BV(CS02); \
00064 \
00065   /* Clear interrupt flag register */ \
00066   TIFR0 = TIFR0; \
00067 \
00068   /* \
00069    * Raise interrupt when value in OCR0 is reached. Note that the \
00070    * counter value in TCNT0 is cleared automatically. \
00071    */ \
00072   TIMSK0 = _BV (OCIE0A);
00073 
00074   
00075 #elif defined (__AVR_ATmega1284P__) || (__AVR_AT90USB1287__) || (__AVR_ATmega1281__) || defined (__AVR_ATmega128RFA1__)
00076 /*
00077   The Raven has a 32768Hz watch crystal that can be used to clock the timer
00078   while the 1284p is sleeping. The Jackdaw has pads for a crystal. The crystal
00079   can be used to clock the 8 bit timer2.
00080   The 1284p routine also uses TIMER2 to sleep a variable number of seconds.
00081   It restores the values here after a wake.
00082 */
00083 #if AVR_CONF_USE32KCRYSTAL
00084 #define AVR_OUTPUT_COMPARE_INT TIMER2_COMPA_vect
00085 #define OCRSetup() \
00086   /* Clock from crystal on TOSC0-1 */ \
00087   ASSR = _BV(AS2);                    \
00088 \
00089   /* Set counter to zero */   \
00090   TCNT2 = 0;                              \
00091 \
00092   /*                                              \
00093    * Set comparison register: \
00094    * Crystal freq. is 32768,\
00095    * pre-scale factor is 8, we want 125 ticks / sec: \
00096    * 32768 = 8 * 124.1 * 33, less 1 for CTC mode\
00097    */ \
00098   OCR2A = 32; \
00099 \
00100   /*                                                            \
00101    * Set timer control register:        \
00102    *  - prescale: 8 (CS21) \
00103    *  - counter reset via comparison register (WGM21) \
00104    */                                                           \
00105   TCCR2A = _BV(WGM21); \
00106   TCCR2B = _BV(CS21);  \
00107 \
00108   /* Clear interrupt flag register */ \
00109   TIFR2 = TIFR2; \
00110 \
00111   /* \
00112    * Raise interrupt when value in OCR2 is reached. Note that the \
00113    * counter value in TCNT2 is cleared automatically. \
00114    */ \
00115   TIMSK2 = _BV (OCIE2A);
00116 #else
00117 #define AVR_OUTPUT_COMPARE_INT TIMER0_COMPA_vect
00118 #define OCRSetup() \
00119   /* Select internal clock */ \
00120   ASSR = 0x00;                            \
00121 \
00122   /* Set counter to zero */   \
00123   TCNT0 = 0;                              \
00124 \
00125   /*                                              \
00126    * Set comparison register: \
00127    * Crystal freq. is 8000000,\
00128    * pre-scale factor is 1024, we want 125 ticks / sec: \
00129    * 8000000 = 1024 * 126.01 * 62, less 1 for CTC mode \
00130    */ \
00131   OCR0A = 61; \
00132 \
00133   /*                                                            \
00134    * Set timer control register:        \
00135    *  - prescale: 1024 (CS00 - CS02) \
00136    *  - counter reset via comparison register (WGM01) \
00137    */                                                           \
00138   TCCR0A = _BV(WGM01); \
00139   TCCR0B =  _BV(CS00) | _BV(CS02); \
00140 \
00141   /* Clear interrupt flag register */ \
00142   TIFR0 = TIFR0; \
00143 \
00144   /* \
00145    * Raise interrupt when value in OCR0 is reached. Note that the \
00146    * counter value in TCNT0 is cleared automatically. \
00147    */ \
00148   TIMSK0 = _BV (OCIE0A);
00149 #endif /* AVR_CONF_USE32KCRYSTAL */
00150 
00151 #else
00152 #error "Setup CPU in clock-avr.h"
00153 #endif
00154 
00155 #endif //CONTIKI_CLOCK_AVR_H

Generated on Mon Apr 11 14:23:36 2011 for Contiki 2.5 by  doxygen 1.6.1