timer_avr.c

Go to the documentation of this file.
00001 
00044 #include <drv/timer_avr.h>
00045 #include <cfg/macros.h> // BV()
00046 
00047 #include <cpu/types.h>
00048 #include <cpu/irq.h>
00049 
00050 #include <avr/io.h>
00051 
00052 #if CPU_AVR_ATMEGA1281 || CPU_AVR_ATMEGA168 || CPU_AVR_ATMEGA328P
00053     #define REG_TIFR0 TIFR0
00054     #define REG_TIFR1 TIFR1
00055     #define REG_TIFR2 TIFR2
00056     #if CPU_AVR_ATMEGA1281
00057         #define REG_TIFR3 TIFR3
00058     #endif
00059 
00060     #define REG_TIMSK0 TIMSK0
00061     #define REG_TIMSK1 TIMSK1
00062     #define REG_TIMSK2 TIMSK2
00063     #if CPU_AVR_ATMEGA1281
00064         #define REG_TIMSK3 TIMSK3
00065     #endif
00066 
00067     #define REG_TCCR0A TCCR0A
00068     #define REG_TCCR0B TCCR0B
00069 
00070     #define REG_TCCR2A TCCR2A
00071     #define REG_TCCR2B TCCR2B
00072 
00073     #define REG_OCR0A  OCR0A
00074     #define REG_OCR2A  OCR2A
00075 
00076     #define BIT_OCF0A  OCF0A
00077     #define BIT_OCF2A  OCF2A
00078 
00079     #define BIT_OCIE0A OCIE0A
00080     #define BIT_OCIE2A OCIE2A
00081 #else
00082     #define REG_TIFR0 TIFR
00083     #define REG_TIFR1 TIFR
00084     #define REG_TIFR2 TIFR
00085     #define REG_TIFR3 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 = (uint8_t)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     #if CPU_AVR_ATMEGA168 || CPU_AVR_ATMEGA328P || CPU_AVR_ATMEGA32
00220         #error For select target there is not TIMER_ON_OVERFLOW3, please select an other one.
00221     #endif
00222 
00223     void timer_hw_init(void)
00224     {
00225         cpu_flags_t flags;
00226         IRQ_SAVE_DISABLE(flags);
00227 
00228         /* Reset Timer overflow flag */
00229         REG_TIFR3 |= BV(TOV3);
00230 
00231         /* Fast PWM mode, 9 bit, 24 kHz, no prescaling. */
00232         #if (TIMER_PRESCALER == 1) && (TIMER_HW_BITS == 9)
00233             TCCR3A |= BV(WGM31);
00234             TCCR3A &= ~BV(WGM30);
00235             TCCR3B |= BV(WGM32) | BV(CS30);
00236             TCCR3B &= ~(BV(WGM33) | BV(CS31) | BV(CS32));
00237         /* Fast PWM mode, 8 bit, 24 kHz, no prescaling. */
00238         #elif (TIMER_PRESCALER == 1) && (TIMER_HW_BITS == 8)
00239             TCCR3A |= BV(WGM30);
00240             TCCR3A &= ~BV(WGM31);
00241             TCCR3B |= BV(WGM32) | BV(CS30);
00242             TCCR3B &= ~(BV(WGM33) | BV(CS31) | BV(CS32));
00243         #else
00244             #error Unsupported value of TIMER_PRESCALER or TIMER_HW_BITS
00245         #endif
00246 
00247         /* initialization of Timer/Counter */
00248         TCNT3 = 0x00;
00249 
00250         /* Enable timer interrupt: Timer/Counter3 Overflow */
00251         REG_TIMSK3 |= BV(TOIE3);
00252 
00253         IRQ_RESTORE(flags);
00254     }
00255 
00256 #else
00257     #error Unimplemented value for CONFIG_TIMER
00258 #endif /* CONFIG_TIMER */
00259