timer_avr.c

Go to the documentation of this file.
00001 
00045 #include <drv/timer_avr.h>
00046 #include <cfg/macros.h> // BV()
00047 
00048 #include <cpu/types.h>
00049 #include <cpu/irq.h>
00050 
00051 #include <avr/io.h>
00052 
00053 #if CPU_AVR_ATMEGA1281 || CPU_AVR_ATMEGA168
00054     #define REG_TIFR0 TIFR0
00055     #define REG_TIFR1 TIFR1
00056     #define REG_TIFR2 TIFR2
00057     #if CPU_AVR_ATMEGA1281
00058         #define REG_TIFR3 TIFR3
00059     #endif
00060 
00061     #define REG_TIMSK0 TIMSK0
00062     #define REG_TIMSK1 TIMSK1
00063     #define REG_TIMSK2 TIMSK2
00064     #if CPU_AVR_ATMEGA1281
00065         #define REG_TIMSK3 TIMSK3
00066     #endif
00067 
00068     #define REG_TCCR0A TCCR0A
00069     #define REG_TCCR0B TCCR0B
00070 
00071     #define REG_TCCR2A TCCR2A
00072     #define REG_TCCR2B TCCR2B
00073 
00074     #define REG_OCR0A  OCR0A
00075     #define REG_OCR2A  OCR2A
00076 
00077     #define BIT_OCF0A  OCF0A
00078     #define BIT_OCF2A  OCF2A
00079 
00080     #define BIT_OCIE0A OCIE0A
00081     #define BIT_OCIE2A OCIE2A
00082 #else
00083     #define REG_TIFR0 TIFR
00084     #define REG_TIFR1 TIFR
00085     #define REG_TIFR2 TIFR
00086 
00087     #define REG_TIMSK0 TIMSK
00088     #define REG_TIMSK1 TIMSK
00089     #define REG_TIMSK2 TIMSK
00090     #define REG_TIMSK3 ETIMSK
00091 
00092     #define REG_TCCR0A TCCR0
00093     #define REG_TCCR0B TCCR0
00094 
00095     #define REG_TCCR2A TCCR2
00096     #define REG_TCCR2B TCCR2
00097 
00098     #define REG_OCR0A  OCR0
00099     #define REG_OCR2A  OCR2
00100 
00101     #define BIT_OCF0A  OCF0
00102     #define BIT_OCF2A  OCF2
00103 
00104     #define BIT_OCIE0A OCIE0
00105     #define BIT_OCIE2A OCIE2
00106 #endif
00107 
00108 #if CPU_AVR_ATMEGA128 || CPU_AVR_ATMEGA64 || CPU_AVR_ATMEGA103
00109     /* These ATMega have different prescaler options. */
00110     #define TIMER0_PRESCALER_64 BV(CS02)
00111     #define TIMER2_PRESCALER_64 (BV(CS21) | BV(CS20))
00112 #else
00113     #define TIMER0_PRESCALER_64 (BV(CS01) | BV(CS00))
00114     #define TIMER2_PRESCALER_64 BV(CS22)
00115 #endif
00116 
00118 #if (CONFIG_TIMER == TIMER_ON_OUTPUT_COMPARE0)
00119 
00120     void timer_hw_init(void)
00121     {
00122         cpu_flags_t flags;
00123         IRQ_SAVE_DISABLE(flags);
00124 
00125         /* Reset Timer flags */
00126         REG_TIFR0 = BV(BIT_OCF0A) | BV(TOV0);
00127 
00128         /* Setup Timer/Counter interrupt */
00129         ASSR = 0x00;                  /* Internal system clock */
00130 
00131         REG_TCCR0A = 0; // TCCR2 reg could be separate or a unique register with both A & B values, this is needed to
00132         REG_TCCR0B = 0;
00133 
00134         REG_TCCR0A = BV(WGM01);             /* Clear on Compare match */
00135             #if TIMER_PRESCALER == 64
00136             REG_TCCR0B |= TIMER0_PRESCALER_64;
00137             #else
00138                 #error Unsupported value of TIMER_PRESCALER
00139             #endif
00140         ;
00141         TCNT0 = 0x00;                 /* Initialization of Timer/Counter */
00142         REG_OCR0A = OCR_DIVISOR;           /* Timer/Counter Output Compare Register */
00143 
00144         /* Enable timer interrupts: Timer/Counter2 Output Compare (OCIE2) */
00145         REG_TIMSK0 &= ~BV(TOIE0);
00146         REG_TIMSK0 |= BV(BIT_OCIE0A);
00147 
00148         IRQ_RESTORE(flags);
00149     }
00150 
00151 #elif (CONFIG_TIMER == TIMER_ON_OVERFLOW1)
00152 
00153     void timer_hw_init(void)
00154     {
00155         cpu_flags_t flags;
00156         IRQ_SAVE_DISABLE(flags);
00157 
00158         /* Reset Timer overflow flag */
00159         REG_TIFR1 |= BV(TOV1);
00160 
00161         /* Fast PWM mode, 9 bit, 24 kHz, no prescaling. */
00162         #if (TIMER_PRESCALER == 1) && (TIMER_HW_BITS == 9)
00163             TCCR1A |= BV(WGM11);
00164             TCCR1A &= ~BV(WGM10);
00165             TCCR1B |= BV(WGM12) | BV(CS10);
00166             TCCR1B &= ~(BV(WGM13) | BV(CS11) | BV(CS12));
00167         /* Fast PWM mode, 8 bit, 24 kHz, no prescaling. */
00168         #elif (TIMER_PRESCALER == 1) && (TIMER_HW_BITS == 8)
00169             TCCR1A |= BV(WGM10);
00170             TCCR1A &= ~BV(WGM11);
00171             TCCR1B |= BV(WGM12) | BV(CS10);
00172             TCCR1B &= ~(BV(WGM13) | BV(CS11) | BV(CS12));
00173         #else
00174             #error Unsupported value of TIMER_PRESCALER or TIMER_HW_BITS
00175         #endif
00176 
00177         TCNT1 = 0x00;         /* initialization of Timer/Counter */
00178 
00179         /* Enable timer interrupt: Timer/Counter1 Overflow */
00180         REG_TIMSK1 |= BV(TOIE1);
00181 
00182         IRQ_RESTORE(flags);
00183     }
00184 
00185 #elif (CONFIG_TIMER == TIMER_ON_OUTPUT_COMPARE2)
00186     void timer_hw_init(void)
00187     {
00188         cpu_flags_t flags;
00189         IRQ_SAVE_DISABLE(flags);
00190 
00191         /* Reset Timer flags */
00192         REG_TIFR2 = BV(BIT_OCF2A) | BV(TOV2);
00193 
00194         /* Setup Timer/Counter interrupt */
00195         REG_TCCR2A = 0; // TCCR2 reg could be separate or a unique register with both A & B values, this is needed to
00196         REG_TCCR2B = 0; // ensure correct initialization.
00197 
00198         REG_TCCR2A = BV(WGM21);
00199         #if TIMER_PRESCALER == 64
00200             REG_TCCR2B |= TIMER2_PRESCALER_64;
00201         #else
00202             #error Unsupported value of TIMER_PRESCALER
00203         #endif
00204 
00205         /* Clear on Compare match & prescaler = 64, internal sys clock.
00206            When changing prescaler change TIMER_HW_HPTICKS_PER_SEC too */
00207         TCNT2 = 0x00;         /* initialization of Timer/Counter */
00208         REG_OCR2A = OCR_DIVISOR;   /* Timer/Counter Output Compare Register */
00209 
00210         /* Enable timer interrupts: Timer/Counter2 Output Compare (OCIE2) */
00211         REG_TIMSK2 &= ~BV(TOIE2);
00212         REG_TIMSK2 |= BV(BIT_OCIE2A);
00213 
00214         IRQ_RESTORE(flags);
00215     }
00216 
00217 #elif (CONFIG_TIMER == TIMER_ON_OVERFLOW3)
00218 
00219     void timer_hw_init(void)
00220     {
00221         cpu_flags_t flags;
00222         IRQ_SAVE_DISABLE(flags);
00223 
00224         /* Reset Timer overflow flag */
00225         REG_TIFR3 |= BV(TOV3);
00226 
00227         /* Fast PWM mode, 9 bit, 24 kHz, no prescaling. */
00228         #if (TIMER_PRESCALER == 1) && (TIMER_HW_BITS == 9)
00229             TCCR3A |= BV(WGM31);
00230             TCCR3A &= ~BV(WGM30);
00231             TCCR3B |= BV(WGM32) | BV(CS30);
00232             TCCR3B &= ~(BV(WGM33) | BV(CS31) | BV(CS32));
00233         /* Fast PWM mode, 8 bit, 24 kHz, no prescaling. */
00234         #elif (TIMER_PRESCALER == 1) && (TIMER_HW_BITS == 8)
00235             TCCR3A |= BV(WGM30);
00236             TCCR3A &= ~BV(WGM31);
00237             TCCR3B |= BV(WGM32) | BV(CS30);
00238             TCCR3B &= ~(BV(WGM33) | BV(CS31) | BV(CS32));
00239         #else
00240             #error Unsupported value of TIMER_PRESCALER or TIMER_HW_BITS
00241         #endif
00242 
00243         /* initialization of Timer/Counter */
00244         TCNT3 = 0x00;
00245 
00246         /* Enable timer interrupt: Timer/Counter3 Overflow */
00247         REG_TIMSK3 = |= BV(TOIE3);
00248 
00249         IRQ_RESTORE(flags);
00250     }
00251 
00252 #else
00253     #error Unimplemented value for CONFIG_TIMER
00254 #endif /* CONFIG_TIMER */
00255