00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051
00052
00053
00054
00055
00056
00057
00058
00059
00060
00061
00062
00063
00064
00065
00066
00067
00068
00069
00070
00071
00072
00073
00074
00075
00076
00077
00078
00079
00080
00081
00082
00083
00084
00085
00086
00087
00088
00089
00090
00091
00092
00093 #include <cfg/os.h>
00094 #include <sys/atom.h>
00095 #include <dev/irqreg.h>
00096 #include <arch/timer.h>
00097
00107 #ifndef NUT_TICK_NFREQ
00108 #ifdef NUT_CPU_FREQ
00109 #define NUT_TICK_NFREQ 1000L
00110 #else
00111 #define NUT_TICK_NFREQ 1024L
00112 #endif
00113 #endif
00114
00115 #ifdef NUT_CPU_FREQ
00116 #ifndef NUT_TIMER_CRYSTAL
00117 #define NUT_TIMER_CRYSTAL NUT_CPU_FREQ
00118 #endif
00119 #ifndef NUT_TIMER_PRESCALE
00120 #define NUT_TIMER_PRESCALE 128
00121 #endif
00122 #else
00123 #ifndef NUT_TIMER_CRYSTAL
00124 #define NUT_TIMER_CRYSTAL 32768L
00125 #endif
00126 #ifndef NUT_TIMER_PRESCALE
00127 #define NUT_TIMER_PRESCALE 1
00128 #endif
00129 #endif
00130
00131
00132 #define OCR_VALUE (((2 * NUT_TIMER_CRYSTAL / (NUT_TIMER_PRESCALE * NUT_TICK_NFREQ) + 1) / 2) - 1)
00133
00134
00135 #define NUT_TICK_FREQ ((2 * NUT_TIMER_CRYSTAL / (OCR_VALUE + 1) / NUT_TIMER_PRESCALE + 1) / 2)
00136
00137
00138 #ifdef NUT_CPU_FREQ
00139 #if defined(MCU_AT90CAN128)
00140 #define TCCR_FLAGS (_BV(CS20) | _BV(CS22) | _BV(WGM21))
00141 #elif defined(MCU_ATMEGA2561)
00142 #define TCCR_FLAGS (_BV(WGM21))
00143 #define TCCR2B_FLAGS (_BV(CS20) | _BV(CS22))
00144 #elif defined(MCU_ATMEGA103)
00145 #define TCCR_FLAGS (_BV(CS00) | _BV(CS02) | _BV(CTC0))
00146 #else
00147 #define TCCR_FLAGS (_BV(CS00) | _BV(CS02) | _BV(WGM01))
00148 #endif
00149 #else
00150 #if defined(MCU_ATMEGA103)
00151 #define TCCR_FLAGS (_BV(CS00) | _BV(CTC0))
00152 #define TCCR_AFLAGS _BV(CS01)
00153 #define ASSR_BIT AS0
00154 #define ASSR_BUSY (_BV(TCN0UB) | _BV(OCR0UB) | _BV(TCR0UB))
00155 #elif defined(MCU_ATMEGA128)
00156 #define TCCR_FLAGS (_BV(CS00) | _BV(WGM01))
00157 #define TCCR_AFLAGS _BV(CS01)
00158 #define ASSR_BIT AS0
00159 #define ASSR_BUSY (_BV(TCN0UB) | _BV(OCR0UB) | _BV(TCR0UB))
00160 #else
00161 #error Define NUT_CPU_FREQ to compile for this CPU.
00162 #endif
00163 #endif
00164
00165 #if defined(MCU_AT90CAN128) || defined(MCU_ATMEGA2561)
00166 #define TCCRx TCCR2A
00167 #define TCNTx TCNT2
00168 #define OCRx OCR2A
00169 #define TIFR_OCFx _BV(OCF2A)
00170 #define TIFR_TOVx _BV(TOV2)
00171 #define sig_TIMER sig_OUTPUT_COMPARE2
00172 #else
00173 #define TCCRx TCCR0
00174 #define TCNTx TCNT0
00175 #define OCRx OCR0
00176 #define TIFR_OCFx _BV(OCF0)
00177 #define TIFR_TOVx _BV(TOV0)
00178 #define sig_TIMER sig_OUTPUT_COMPARE0
00179 #endif
00180
00181 static u_long cpu_clock;
00182
00188 #ifndef NUT_CPU_FREQ
00189 static u_long CountCpuLoops(void)
00190 {
00191 #ifdef __GNUC__
00192 u_long rc = 1;
00193
00194 __asm__ __volatile__("firstovf: \n\t"
00195 "in %D0,%1 \n\t"
00196 "andi %D0,%2 \n\t"
00197 "breq firstovf \n\t"
00198 "out %1,%D0 \n\n"
00199
00200 "nextovf: \n\t"
00201 "sec \n\t"
00202 "adc %A0,__zero_reg__ \n\t"
00203 "adc %B0,__zero_reg__ \n\t"
00204 "adc %C0,__zero_reg__ \n\t"
00205 "in %D0,%1 \n\t"
00206 "andi %D0,%2 \n\t"
00207 "breq nextovf \n\t"
00208 "clr %D0 "
00209 :"=d"(rc)
00210 :"I"(_SFR_IO_ADDR(TIFR))
00211 , "I"(TIFR_TOVx)
00212 , "0"(rc)
00213 );
00214 return rc;
00215 #elif defined(__IMAGECRAFT__)
00216 u_long rc;
00217
00218 asm("CLR R0");
00219 asm("CLR R16");
00220 asm("CLR R17");
00221 asm("CLR R18");
00222 asm("firstovf:");
00223 asm("IN R19, 0x36");
00224 asm("ANDI R19,1");
00225 asm("BREQ firstovf");
00226 asm("OUT 0x36, R19");
00227
00228 asm("nextovf:");
00229 asm("SEC");
00230 asm("ADC R16, R0");
00231 asm("ADC R17, R0");
00232 asm("ADC R18, R0");
00233 asm("IN R19, 0x36");
00234 asm("ANDI R19, 1");
00235 asm("BREQ nextovf");
00236 asm("CLR R19");
00237 asm("STD Y+0, R16");
00238 asm("STD Y+1, R17");
00239 asm("STD Y+2, R18");
00240 asm("STD Y+3, R19");
00241
00242 return rc;
00243 #endif
00244 }
00245 #endif
00246
00256 #ifndef NUT_CPU_FREQ
00257 static u_long NutComputeCpuClock(void)
00258 {
00259 u_long rc;
00260
00261
00262 NutDisableTimerIrq();
00263
00264
00265 sbi(ASSR, ASSR_BIT);
00266
00267
00268 outb(TCNTx, 0);
00269
00270
00271 outb(TCCRx, TCCR_AFLAGS);
00272
00273
00274 while ((inb(ASSR) & ASSR_BUSY) != 0);
00275
00276
00277 outb(TIFR, TIFR_TOVx);
00278
00279 NutEnterCritical();
00280 rc = CountCpuLoops();
00281 NutExitCritical();
00282
00283 return rc << 7;
00284 }
00285 #endif
00286
00295 void NutRegisterTimer(void (*handler) (void *))
00296 {
00297 #ifdef NUT_CPU_FREQ
00298 cpu_clock = NUT_CPU_FREQ;
00299 #else
00300 cpu_clock = NutComputeCpuClock();
00301 #endif
00302
00303
00304 NutDisableTimerIrq();
00305
00306 #ifndef NUT_CPU_FREQ
00307
00308 sbi(ASSR, ASSR_BIT);
00309 #endif
00310
00311
00312 outb(TCNTx, 0);
00313
00314
00315 outb(TCCRx, TCCR_FLAGS);
00316 #ifdef TCCR2B_FLAGS
00317 outb(TCCR2B, TCCR2B_FLAGS);
00318 #endif
00319
00320
00321 outb(OCRx, OCR_VALUE);
00322
00323 #ifndef NUT_CPU_FREQ
00324
00325 while ((inb(ASSR) & ASSR_BUSY) != 0);
00326 #endif
00327
00328
00329 outb(TIFR, TIFR_OCFx);
00330
00331
00332 NutRegisterIrqHandler(&sig_TIMER, handler, 0);
00333 }
00334
00340 u_long NutGetCpuClock(void)
00341 {
00342 return cpu_clock;
00343 }
00344
00350 u_long NutGetTickClock(void)
00351 {
00352 return NUT_TICK_FREQ;
00353 }
00354
00358 u_long NutTimerMillisToTicks(u_long ms)
00359 {
00360 u_long x;
00361
00362 x = ms * NutGetTickClock() / 1000UL;
00363 if (x == 0) {
00364 x = 1;
00365 }
00366
00367 return (x);
00368 }
00369