ostimer.c

Go to the documentation of this file.
00001 /*
00002  * Copyright (C) 2001-2005 by egnite Software GmbH. All rights reserved.
00003  *
00004  * Redistribution and use in source and binary forms, with or without
00005  * modification, are permitted provided that the following conditions
00006  * are met:
00007  *
00008  * 1. Redistributions of source code must retain the above copyright
00009  *    notice, this list of conditions and the following disclaimer.
00010  * 2. Redistributions in binary form must reproduce the above copyright
00011  *    notice, this list of conditions and the following disclaimer in the
00012  *    documentation and/or other materials provided with the distribution.
00013  * 3. Neither the name of the copyright holders nor the names of
00014  *    contributors may be used to endorse or promote products derived
00015  *    from this software without specific prior written permission.
00016  *
00017  * THIS SOFTWARE IS PROVIDED BY EGNITE SOFTWARE GMBH AND CONTRIBUTORS
00018  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
00019  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
00020  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL EGNITE
00021  * SOFTWARE GMBH OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
00022  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
00023  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
00024  * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
00025  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
00026  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
00027  * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
00028  * SUCH DAMAGE.
00029  *
00030  * For additional information see http://www.ethernut.de/
00031  *
00032  */
00033 
00034 /*
00035  * $Log: ostimer.c,v $
00036  * Revision 1.4  2006/12/20 15:14:41  freckle
00037  * corrected millisecond to nut ticks . Same bug as fixed at 2006-09-05
00038  *
00039  * Revision 1.3  2006/10/05 17:13:12  haraldkipp
00040  * Fixes bug #1567730. The problem had been reported by several people.
00041  * Lars Andersson provided the most complete solution, IMHO.
00042  *
00043  * Revision 1.2  2006/02/08 15:18:49  haraldkipp
00044  * ATmega2561 Support
00045  *
00046  * Revision 1.1  2005/07/26 18:02:40  haraldkipp
00047  * Moved from dev.
00048  *
00049  * Revision 1.2  2005/06/12 16:50:57  haraldkipp
00050  * Major redesign to provide better portability and reduce interrupt latency.
00051  *
00052  * Revision 1.1  2005/05/27 17:17:31  drsung
00053  * Moved the file
00054  *
00055  * Revision 1.7  2005/05/16 08:54:45  haraldkipp
00056  * Original routines did not work for Arthernet.
00057  *
00058  * Revision 1.6  2005/03/09 08:33:34  hwmaier
00059  * Finally implemented the correct timer routines and init for AT90CAN128. Timer2 is now used on AT90CAN128 rather Timer0 because Atmel (don't blame me) swapped the Timer designation.
00060  *
00061  * Revision 1.5  2005/02/21 12:38:00  phblum
00062  * Removed tabs and added semicolons after NUTTRACER macros
00063  *
00064  * Revision 1.4  2005/02/10 07:06:48  hwmaier
00065  * Changes to incorporate support for AT90CAN128 CPU
00066  *
00067  * Revision 1.3  2005/01/24 22:34:50  freckle
00068  * Added new tracer by Phlipp Blum <blum@tik.ee.ethz.ch>
00069  *
00070  * Revision 1.2  2004/12/16 08:40:35  haraldkipp
00071  * Late increment fixes ICCAVR bug.
00072  *
00073  * Revision 1.1  2004/03/16 16:48:46  haraldkipp
00074  * Added Jan Dubiec's H8/300 port.
00075  *
00076  * Revision 1.3  2004/03/05 20:38:18  drsung
00077  * Bugfix from bugfix. sorry!
00078  *
00079  * Revision 1.2  2004/03/05 20:19:45  drsung
00080  * Bugfix in NutTimerInit. ICCAVR failed to compile, if NUT_CPU_FREQ is defined.
00081  *
00082  * Revision 1.1  2004/02/01 18:49:48  haraldkipp
00083  * Added CPU family support
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             /* ----- 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                           /* ----- !NUT_CPU_FREQ */
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                          /* ----- NUT_CPU_FREQ */
00124 
00125 /* Output compare register value. */
00126 #define OCR_VALUE       (((2 * NUT_TIMER_CRYSTAL / (NUT_TIMER_PRESCALE * NUT_TICK_NFREQ) + 1) / 2) - 1)
00127 
00128 /* Calculated number of timer ticks per second. */
00129 #define NUT_TICK_FREQ   ((2 * NUT_TIMER_CRYSTAL / (OCR_VALUE + 1) / NUT_TIMER_PRESCALE + 1) / 2)
00130 
00131 
00132 #ifdef NUT_CPU_FREQ             /* ----- 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)    /* MCU_ATMEGA103 */
00139 #define TCCR_FLAGS  (_BV(CS00) | _BV(CS02) | _BV(CTC0))
00140 #else                           /* MCU_ATMEGA128 */
00141 #define TCCR_FLAGS  (_BV(CS00) | _BV(CS02) | _BV(WGM01))
00142 #endif
00143 #else                           /* ----- !NUT_CPU_FREQ */
00144 #if defined(MCU_ATMEGA103)      /* 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)    /* 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                           /* Other MCU */
00155 #error Define NUT_CPU_FREQ to compile for this CPU.
00156 #endif
00157 #endif                          /* ----- NUT_CPU_FREQ */
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)    /* Output %0 */
00206                          :"r"(ms)       /* Input %1 */
00207                          , "r"((u_short) (cpu_clock / 4000))    /* Input %2 */
00208         );
00209 #else
00210     u_short delay_cnt = 2400;   //*KU* for 14.745600 MHz Clock
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                          /* This loop has 8 cycles. */
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)      /* Output %0 */
00247                          :"I"(_SFR_IO_ADDR(TIFR))       /* Input %1 */
00248                          , "I"(TIFR_TOVx)      /* Input %2 */
00249                          , "0"(rc)      /* Input %0 */
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     /* This loop has 8 cycles. */
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     /* Disable timer interrupts. */
00299     NutDisableTimerIrq();
00300 
00301     /* Select asynchronous oscillator. */
00302     sbi(ASSR, ASSR_BIT);
00303 
00304     /* Reset counter register. */
00305     outb(TCNTx, 0);
00306 
00307     /* Set prescaler to 8. Overflow will occur every 62.5 ms. */
00308     outb(TCCRx, TCCR_AFLAGS);
00309 
00310     /* Wait for asynchronous busy clear. */
00311     while ((inb(ASSR) & ASSR_BUSY) != 0);
00312 
00313     /* Clear interrupt flag. */
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     /* Disable timer interrupts. */
00341     NutDisableTimerIrq();
00342 
00343 #ifndef NUT_CPU_FREQ
00344     /* Select asynchronous oscillator. */
00345     sbi(ASSR, ASSR_BIT);
00346 #endif
00347 
00348     /* Reset counter register. */
00349     outb(TCNTx, 0);
00350 
00351     /* Set CTC mode and prescaler. */
00352     outb(TCCRx, TCCR_FLAGS);
00353 #ifdef TCCR2B_FLAGS
00354     outb(TCCR2B, TCCR2B_FLAGS);
00355 #endif
00356 
00357     /* Set output compare register. */
00358     outb(OCRx, OCR_VALUE);
00359 
00360 #ifndef NUT_CPU_FREQ
00361     /* Wait for asynchronous busy clear. */
00362     while ((inb(ASSR) & ASSR_BUSY) != 0);
00363 #endif
00364 
00365     /* Clear interrupt flags. */
00366     outb(TIFR, TIFR_OCFx);
00367 
00368     /* Enable timer compare interrupts. */
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 

© 2000-2007 by egnite Software GmbH - visit http://www.ethernut.de/