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.5  2008/07/08 08:25:04  haraldkipp
00037  * NutDelay is no more architecture specific.
00038  * Number of loops per millisecond is configurable or will be automatically
00039  * determined.
00040  * A new function NutMicroDelay provides shorter delays.
00041  *
00042  * Revision 1.4  2006/12/20 15:14:41  freckle
00043  * corrected millisecond to nut ticks . Same bug as fixed at 2006-09-05
00044  *
00045  * Revision 1.3  2006/10/05 17:13:12  haraldkipp
00046  * Fixes bug #1567730. The problem had been reported by several people.
00047  * Lars Andersson provided the most complete solution, IMHO.
00048  *
00049  * Revision 1.2  2006/02/08 15:18:49  haraldkipp
00050  * ATmega2561 Support
00051  *
00052  * Revision 1.1  2005/07/26 18:02:40  haraldkipp
00053  * Moved from dev.
00054  *
00055  * Revision 1.2  2005/06/12 16:50:57  haraldkipp
00056  * Major redesign to provide better portability and reduce interrupt latency.
00057  *
00058  * Revision 1.1  2005/05/27 17:17:31  drsung
00059  * Moved the file
00060  *
00061  * Revision 1.7  2005/05/16 08:54:45  haraldkipp
00062  * Original routines did not work for Arthernet.
00063  *
00064  * Revision 1.6  2005/03/09 08:33:34  hwmaier
00065  * 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.
00066  *
00067  * Revision 1.5  2005/02/21 12:38:00  phblum
00068  * Removed tabs and added semicolons after NUTTRACER macros
00069  *
00070  * Revision 1.4  2005/02/10 07:06:48  hwmaier
00071  * Changes to incorporate support for AT90CAN128 CPU
00072  *
00073  * Revision 1.3  2005/01/24 22:34:50  freckle
00074  * Added new tracer by Phlipp Blum <blum@tik.ee.ethz.ch>
00075  *
00076  * Revision 1.2  2004/12/16 08:40:35  haraldkipp
00077  * Late increment fixes ICCAVR bug.
00078  *
00079  * Revision 1.1  2004/03/16 16:48:46  haraldkipp
00080  * Added Jan Dubiec's H8/300 port.
00081  *
00082  * Revision 1.3  2004/03/05 20:38:18  drsung
00083  * Bugfix from bugfix. sorry!
00084  *
00085  * Revision 1.2  2004/03/05 20:19:45  drsung
00086  * Bugfix in NutTimerInit. ICCAVR failed to compile, if NUT_CPU_FREQ is defined.
00087  *
00088  * Revision 1.1  2004/02/01 18:49:48  haraldkipp
00089  * Added CPU family support
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             /* ----- 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                           /* ----- !NUT_CPU_FREQ */
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                          /* ----- NUT_CPU_FREQ */
00130 
00131 /* Output compare register value. */
00132 #define OCR_VALUE       (((2 * NUT_TIMER_CRYSTAL / (NUT_TIMER_PRESCALE * NUT_TICK_NFREQ) + 1) / 2) - 1)
00133 
00134 /* Calculated number of timer ticks per second. */
00135 #define NUT_TICK_FREQ   ((2 * NUT_TIMER_CRYSTAL / (OCR_VALUE + 1) / NUT_TIMER_PRESCALE + 1) / 2)
00136 
00137 
00138 #ifdef NUT_CPU_FREQ             /* ----- 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)    /* MCU_ATMEGA103 */
00145 #define TCCR_FLAGS  (_BV(CS00) | _BV(CS02) | _BV(CTC0))
00146 #else                           /* MCU_ATMEGA128 */
00147 #define TCCR_FLAGS  (_BV(CS00) | _BV(CS02) | _BV(WGM01))
00148 #endif
00149 #else                           /* ----- !NUT_CPU_FREQ */
00150 #if defined(MCU_ATMEGA103)      /* 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)    /* 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                           /* Other MCU */
00161 #error Define NUT_CPU_FREQ to compile for this CPU.
00162 #endif
00163 #endif                          /* ----- NUT_CPU_FREQ */
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                          /* This loop has 8 cycles. */
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)      /* Output %0 */
00210                          :"I"(_SFR_IO_ADDR(TIFR))       /* Input %1 */
00211                          , "I"(TIFR_TOVx)      /* Input %2 */
00212                          , "0"(rc)      /* Input %0 */
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     /* This loop has 8 cycles. */
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     /* Disable timer interrupts. */
00262     NutDisableTimerIrq();
00263 
00264     /* Select asynchronous oscillator. */
00265     sbi(ASSR, ASSR_BIT);
00266 
00267     /* Reset counter register. */
00268     outb(TCNTx, 0);
00269 
00270     /* Set prescaler to 8. Overflow will occur every 62.5 ms. */
00271     outb(TCCRx, TCCR_AFLAGS);
00272 
00273     /* Wait for asynchronous busy clear. */
00274     while ((inb(ASSR) & ASSR_BUSY) != 0);
00275 
00276     /* Clear interrupt flag. */
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     /* Disable timer interrupts. */
00304     NutDisableTimerIrq();
00305 
00306 #ifndef NUT_CPU_FREQ
00307     /* Select asynchronous oscillator. */
00308     sbi(ASSR, ASSR_BIT);
00309 #endif
00310 
00311     /* Reset counter register. */
00312     outb(TCNTx, 0);
00313 
00314     /* Set CTC mode and prescaler. */
00315     outb(TCCRx, TCCR_FLAGS);
00316 #ifdef TCCR2B_FLAGS
00317     outb(TCCR2B, TCCR2B_FLAGS);
00318 #endif
00319 
00320     /* Set output compare register. */
00321     outb(OCRx, OCR_VALUE);
00322 
00323 #ifndef NUT_CPU_FREQ
00324     /* Wait for asynchronous busy clear. */
00325     while ((inb(ASSR) & ASSR_BUSY) != 0);
00326 #endif
00327 
00328     /* Clear interrupt flags. */
00329     outb(TIFR, TIFR_OCFx);
00330 
00331     /* Enable timer compare interrupts. */
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 

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