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 #include <cfg/os.h>
00088 #include <sys/atom.h>
00089 #include <dev/irqreg.h>
00090 #include <arch/timer.h>
00091
00101 #ifndef NUT_TICK_NFREQ
00102 #ifdef NUT_CPU_FREQ
00103 #define NUT_TICK_NFREQ 1000L
00104 #else
00105 #define NUT_TICK_NFREQ 1024L
00106 #endif
00107 #endif
00108
00109 #ifdef NUT_CPU_FREQ
00110 #ifndef NUT_TIMER_CRYSTAL
00111 #define NUT_TIMER_CRYSTAL NUT_CPU_FREQ
00112 #endif
00113 #ifndef NUT_TIMER_PRESCALE
00114 #define NUT_TIMER_PRESCALE 128
00115 #endif
00116 #else
00117 #ifndef NUT_TIMER_CRYSTAL
00118 #define NUT_TIMER_CRYSTAL 32768L
00119 #endif
00120 #ifndef NUT_TIMER_PRESCALE
00121 #define NUT_TIMER_PRESCALE 1
00122 #endif
00123 #endif
00124
00125
00126 #define OCR_VALUE (((2 * NUT_TIMER_CRYSTAL / (NUT_TIMER_PRESCALE * NUT_TICK_NFREQ) + 1) / 2) - 1)
00127
00128
00129 #define NUT_TICK_FREQ ((2 * NUT_TIMER_CRYSTAL / (OCR_VALUE + 1) / NUT_TIMER_PRESCALE + 1) / 2)
00130
00131
00132 #ifdef NUT_CPU_FREQ
00133 #if defined(MCU_AT90CAN128)
00134 #define TCCR_FLAGS (_BV(CS20) | _BV(CS22) | _BV(WGM21))
00135 #elif defined(MCU_ATMEGA2561)
00136 #define TCCR_FLAGS (_BV(WGM21))
00137 #define TCCR2B_FLAGS (_BV(CS20) | _BV(CS22))
00138 #elif defined(MCU_ATMEGA103)
00139 #define TCCR_FLAGS (_BV(CS00) | _BV(CS02) | _BV(CTC0))
00140 #else
00141 #define TCCR_FLAGS (_BV(CS00) | _BV(CS02) | _BV(WGM01))
00142 #endif
00143 #else
00144 #if defined(MCU_ATMEGA103)
00145 #define TCCR_FLAGS (_BV(CS00) | _BV(CTC0))
00146 #define TCCR_AFLAGS _BV(CS01)
00147 #define ASSR_BIT AS0
00148 #define ASSR_BUSY (_BV(TCN0UB) | _BV(OCR0UB) | _BV(TCR0UB))
00149 #elif defined(MCU_ATMEGA128)
00150 #define TCCR_FLAGS (_BV(CS00) | _BV(WGM01))
00151 #define TCCR_AFLAGS _BV(CS01)
00152 #define ASSR_BIT AS0
00153 #define ASSR_BUSY (_BV(TCN0UB) | _BV(OCR0UB) | _BV(TCR0UB))
00154 #else
00155 #error Define NUT_CPU_FREQ to compile for this CPU.
00156 #endif
00157 #endif
00158
00159 #if defined(MCU_AT90CAN128) || defined(MCU_ATMEGA2561)
00160 #define TCCRx TCCR2A
00161 #define TCNTx TCNT2
00162 #define OCRx OCR2A
00163 #define TIFR_OCFx _BV(OCF2A)
00164 #define TIFR_TOVx _BV(TOV2)
00165 #define sig_TIMER sig_OUTPUT_COMPARE2
00166 #else
00167 #define TCCRx TCCR0
00168 #define TCNTx TCNT0
00169 #define OCRx OCR0
00170 #define TIFR_OCFx _BV(OCF0)
00171 #define TIFR_TOVx _BV(TOV0)
00172 #define sig_TIMER sig_OUTPUT_COMPARE0
00173 #endif
00174
00175 static u_long cpu_clock;
00176
00177
00193 void NutDelay(u_char ms)
00194 {
00195 #ifdef __GNUC__
00196 u_short cnt;
00197 __asm__ __volatile__("L_dl1%=: \n\t"
00198 "mov %A0, %A2 \n\t"
00199 "mov %B0, %B2 \n\n"
00200 "L_dl2%=: \n\t"
00201 "sbiw %A0, 1 \n\t"
00202 "brne L_dl2%= \n\t"
00203 "dec %1 \n\t"
00204 "brne L_dl1%= "
00205 :"=&w"(cnt)
00206 :"r"(ms)
00207 , "r"((u_short) (cpu_clock / 4000))
00208 );
00209 #else
00210 u_short delay_cnt = 2400;
00211 u_short delay_cnt_buffer;
00212
00213 while (ms--) {
00214 delay_cnt_buffer = delay_cnt;
00215 while (delay_cnt_buffer--);
00216 }
00217 #endif
00218 }
00219
00225 #ifndef NUT_CPU_FREQ
00226 static u_long CountCpuLoops(void)
00227 {
00228 #ifdef __GNUC__
00229 u_long rc = 1;
00230
00231 __asm__ __volatile__("firstovf: \n\t"
00232 "in %D0,%1 \n\t"
00233 "andi %D0,%2 \n\t"
00234 "breq firstovf \n\t"
00235 "out %1,%D0 \n\n"
00236
00237 "nextovf: \n\t"
00238 "sec \n\t"
00239 "adc %A0,__zero_reg__ \n\t"
00240 "adc %B0,__zero_reg__ \n\t"
00241 "adc %C0,__zero_reg__ \n\t"
00242 "in %D0,%1 \n\t"
00243 "andi %D0,%2 \n\t"
00244 "breq nextovf \n\t"
00245 "clr %D0 "
00246 :"=d"(rc)
00247 :"I"(_SFR_IO_ADDR(TIFR))
00248 , "I"(TIFR_TOVx)
00249 , "0"(rc)
00250 );
00251 return rc;
00252 #elif defined(__IMAGECRAFT__)
00253 u_long rc;
00254
00255 asm("CLR R0");
00256 asm("CLR R16");
00257 asm("CLR R17");
00258 asm("CLR R18");
00259 asm("firstovf:");
00260 asm("IN R19, 0x36");
00261 asm("ANDI R19,1");
00262 asm("BREQ firstovf");
00263 asm("OUT 0x36, R19");
00264
00265 asm("nextovf:");
00266 asm("SEC");
00267 asm("ADC R16, R0");
00268 asm("ADC R17, R0");
00269 asm("ADC R18, R0");
00270 asm("IN R19, 0x36");
00271 asm("ANDI R19, 1");
00272 asm("BREQ nextovf");
00273 asm("CLR R19");
00274 asm("STD Y+0, R16");
00275 asm("STD Y+1, R17");
00276 asm("STD Y+2, R18");
00277 asm("STD Y+3, R19");
00278
00279 return rc;
00280 #endif
00281 }
00282 #endif
00283
00293 #ifndef NUT_CPU_FREQ
00294 static u_long NutComputeCpuClock(void)
00295 {
00296 u_long rc;
00297
00298
00299 NutDisableTimerIrq();
00300
00301
00302 sbi(ASSR, ASSR_BIT);
00303
00304
00305 outb(TCNTx, 0);
00306
00307
00308 outb(TCCRx, TCCR_AFLAGS);
00309
00310
00311 while ((inb(ASSR) & ASSR_BUSY) != 0);
00312
00313
00314 outb(TIFR, TIFR_TOVx);
00315
00316 NutEnterCritical();
00317 rc = CountCpuLoops();
00318 NutExitCritical();
00319
00320 return rc << 7;
00321 }
00322 #endif
00323
00332 void NutRegisterTimer(void (*handler) (void *))
00333 {
00334 #ifdef NUT_CPU_FREQ
00335 cpu_clock = NUT_CPU_FREQ;
00336 #else
00337 cpu_clock = NutComputeCpuClock();
00338 #endif
00339
00340
00341 NutDisableTimerIrq();
00342
00343 #ifndef NUT_CPU_FREQ
00344
00345 sbi(ASSR, ASSR_BIT);
00346 #endif
00347
00348
00349 outb(TCNTx, 0);
00350
00351
00352 outb(TCCRx, TCCR_FLAGS);
00353 #ifdef TCCR2B_FLAGS
00354 outb(TCCR2B, TCCR2B_FLAGS);
00355 #endif
00356
00357
00358 outb(OCRx, OCR_VALUE);
00359
00360 #ifndef NUT_CPU_FREQ
00361
00362 while ((inb(ASSR) & ASSR_BUSY) != 0);
00363 #endif
00364
00365
00366 outb(TIFR, TIFR_OCFx);
00367
00368
00369 NutRegisterIrqHandler(&sig_TIMER, handler, 0);
00370 }
00371
00377 u_long NutGetCpuClock(void)
00378 {
00379 return cpu_clock;
00380 }
00381
00387 u_long NutGetTickClock(void)
00388 {
00389 return NUT_TICK_FREQ;
00390 }
00391
00395 u_long NutTimerMillisToTicks(u_long ms)
00396 {
00397 u_long x;
00398
00399 x = ms * NutGetTickClock() / 1000UL;
00400 if (x == 0) {
00401 x = 1;
00402 }
00403
00404 return (x);
00405 }
00406