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$ 00036 * Revision 1.7 2008/08/22 09:25:33 haraldkipp 00037 * Clock value caching and new functions NutArchClockGet, NutClockGet and 00038 * NutClockSet added. 00039 * 00040 * Revision 1.6 2008/08/11 06:59:12 haraldkipp 00041 * BSD types replaced by stdint types (feature request #1282721). 00042 * 00043 * Revision 1.5 2008/07/08 08:25:04 haraldkipp 00044 * NutDelay is no more architecture specific. 00045 * Number of loops per millisecond is configurable or will be automatically 00046 * determined. 00047 * A new function NutMicroDelay provides shorter delays. 00048 * 00049 * Revision 1.4 2006/10/08 16:48:07 haraldkipp 00050 * Documentation fixed 00051 * 00052 * Revision 1.3 2005/10/24 17:59:19 haraldkipp 00053 * Use correct header file, arm, not gba. 00054 * 00055 * Revision 1.2 2005/08/02 17:46:45 haraldkipp 00056 * Major API documentation update. 00057 * 00058 * Revision 1.1 2005/07/26 18:02:26 haraldkipp 00059 * Moved from dev. 00060 * 00061 * Revision 1.2 2005/07/20 09:17:26 haraldkipp 00062 * Default NUT_CPU_FREQ and NUT_TICK_FREQ added. 00063 * NutTimerIntr() removed, because we can use the hardware independent code. 00064 * 00065 * Revision 1.1 2005/05/27 17:16:40 drsung 00066 * Moved the file. 00067 * 00068 * Revision 1.5 2005/04/05 17:50:46 haraldkipp 00069 * Use register names in gba.h. 00070 * 00071 * Revision 1.4 2004/11/08 19:16:37 haraldkipp 00072 * Hacked in Gameboy timer support 00073 * 00074 * Revision 1.3 2004/10/03 18:42:21 haraldkipp 00075 * No GBA support yet, but let the compiler run through 00076 * 00077 * Revision 1.2 2004/09/08 10:19:39 haraldkipp 00078 * Running on AT91 and S3C, thanks to James Tyou 00079 * 00080 */ 00081 00082 #include <cfg/arch.h> 00083 #include <arch/arm.h> 00084 #include <dev/irqreg.h> 00085 00090 00091 #ifndef NUT_CPU_FREQ 00092 #define NUT_CPU_FREQ 1000000UL 00093 #endif 00094 00095 #ifndef NUT_TICK_FREQ 00096 #define NUT_TICK_FREQ 1000UL 00097 #endif 00098 00099 static void (*os_handler) (void *); 00100 00104 void Timer3Entry(void *arg) 00105 { 00106 outw(REG_IF, INT_TMR3); 00107 os_handler(0); 00108 } 00109 00121 void NutRegisterTimer(void (*handler) (void *)) 00122 { 00123 os_handler = handler; 00124 00125 00126 /* Disable master interrupt. */ 00127 outw(REG_IME, 0); 00128 00129 /* Set global interrupt vector. */ 00130 NutRegisterIrqHandler(&sig_TMR3, Timer3Entry, 0); 00131 00132 /* Enable timer and timer interrupts. */ 00133 outdw(REG_TMR3CNT, TMR_IRQ_ENA | TMR_ENA | 48756); 00134 00135 /* Enable timer 3 interrupts. */ 00136 outw(REG_IE, inw(REG_IE) | INT_TMR3); 00137 00138 /* Enable master interrupt. */ 00139 outw(REG_IME, 1); 00140 } 00141 00147 uint32_t NutArchClockGet(int idx) 00148 { 00149 return NUT_CPU_FREQ; 00150 } 00151 00157 uint32_t NutGetTickClock(void) 00158 { 00159 return NUT_TICK_FREQ; 00160 } 00161 00165 uint32_t NutTimerMillisToTicks(uint32_t ms) 00166 { 00167 #if (NUT_TICK_FREQ % 1000) 00168 if (ms >= 0x3E8000UL) 00169 return (ms / 1000UL) * NUT_TICK_FREQ; 00170 return (ms * NUT_TICK_FREQ + 999UL) / 1000UL; 00171 #else 00172 return ms * (NUT_TICK_FREQ / 1000UL); 00173 #endif 00174 } 00175