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: nutinit.c,v $ 00036 * Revision 1.17 2008/08/11 06:59:14 haraldkipp 00037 * BSD types replaced by stdint types (feature request #1282721). 00038 * 00039 * Revision 1.16 2008/08/06 12:51:05 haraldkipp 00040 * Added support for Ethernut 5 (AT91SAM9XE reference design). 00041 * 00042 * Revision 1.15 2008/02/15 16:59:27 haraldkipp 00043 * Spport for AT91SAM7SE512 added. 00044 * 00045 * Revision 1.14 2007/10/04 20:08:00 olereinhardt 00046 * Support for SAM7S256 added 00047 * 00048 * Revision 1.13 2006/09/29 12:39:51 haraldkipp 00049 * Spurious interrupt handling on all supported AT91 devices. 00050 * 00051 * Revision 1.12 2006/07/26 11:17:16 haraldkipp 00052 * Defining AT91_PLL_MAINCK will automatically determine SAM7X clock by 00053 * reading PLL settings. 00054 * 00055 * Revision 1.11 2006/07/18 14:04:10 haraldkipp 00056 * Low level hardware initialization moved to crtat91sam7x256_rom.S. This 00057 * avoids the ugly jump from C code back into the runtime initialization. 00058 * Watchdog reset (tiger bell) removed from idle thread. 00059 * 00060 * Revision 1.10 2006/07/15 11:13:30 haraldkipp 00061 * CPU ran into the data pool of Sam7xLowLevelInit(). Temporarily 00062 * fixed by Andras Albert with an additional global label in the 00063 * startup code. Furthermore Andras changed the clock initialization. 00064 * The CPU is now running at 47.9232 MHz and the MAC starts working. 00065 * Great, TCP/IP is now running on the SAM7X. 00066 * 00067 * Revision 1.9 2006/07/10 14:27:03 haraldkipp 00068 * C++ will use main instead of NutAppMain. Contributed by Matthias Wilde. 00069 * 00070 * Revision 1.8 2006/07/05 07:57:52 haraldkipp 00071 * Daidai's support for AT91SAM7X added. Possibly taken from Atmel. 00072 * May require new coding from ground up in order to not conflict with 00073 * original copyright. 00074 * Nevertheless, many thanks to Daidai for providing his adaption. 00075 * 00076 * Revision 1.7 2006/06/28 17:22:34 haraldkipp 00077 * Make it compile for AT91SAM7X256. 00078 * 00079 * Revision 1.6 2006/03/02 19:43:11 haraldkipp 00080 * Added MCU specific hardware initialization routine. This should be done 00081 * later for all MCUs to avoid contaminating NutInit() with MCU specific 00082 * stuff. For the AT91 the spurious interrupt handler has been added, 00083 * which fixes SF 1440948. 00084 * 00085 * Revision 1.5 2006/02/23 15:34:00 haraldkipp 00086 * Support for Philips LPC2xxx Family and LPC-E2294 Board from Olimex added. 00087 * Many thanks to Michael Fischer for this port. 00088 * 00089 * Revision 1.4 2005/10/24 09:22:29 haraldkipp 00090 * Default idle and main thread stack sizes increased. 00091 * AT91 header file moved. 00092 * 00093 * Revision 1.3 2005/08/02 17:46:45 haraldkipp 00094 * Major API documentation update. 00095 * 00096 * Revision 1.2 2005/07/26 16:17:03 haraldkipp 00097 * Use default stack sizes for main and idle, if none had been defined. 00098 * 00099 * Revision 1.1 2005/05/27 17:16:40 drsung 00100 * Moved the file. 00101 * 00102 * Revision 1.4 2005/04/05 17:52:41 haraldkipp 00103 * Much better implementation of GBA interrupt registration. 00104 * 00105 * Revision 1.3 2004/11/08 18:58:59 haraldkipp 00106 * Configurable stack sizes 00107 * 00108 * Revision 1.2 2004/09/08 10:19:23 haraldkipp 00109 * Made it look more general 00110 * 00111 * Revision 1.1 2004/03/16 16:48:46 haraldkipp 00112 * Added Jan Dubiec's H8/300 port. 00113 * 00114 * 00115 */ 00116 00117 #include <cfg/arch.h> 00118 #include <cfg/memory.h> 00119 #include <cfg/os.h> 00120 #ifdef MCU_GBA 00121 #include <dev/irqreg.h> 00122 #elif defined(MCU_LPC2XXX) 00123 #include <arch/arm/lpc2xxx.h> 00124 #else 00125 #include <arch/arm/at91.h> 00126 #endif 00127 00132 00133 #ifndef NUT_THREAD_MAINSTACK 00134 #define NUT_THREAD_MAINSTACK 1024 00135 #endif 00136 00137 #ifndef NUT_THREAD_IDLESTACK 00138 #define NUT_THREAD_IDLESTACK 512 00139 #endif 00140 00141 #ifdef __CROSSWORKS4ARM__ 00142 extern void *__unused_start__; 00143 /* 00144 * Michael, Why does Crossworks needs this one. Is memory configurable 00145 * with the Configurator? 00146 */ 00147 extern void *__External_SRAM_segment_end__; 00148 00149 #define HEAP_START &__unused_start__ 00150 #define HEAP_SIZE ((uptr_t)(&__External_SRAM_segment_end__ - 1) - (uptr_t)(HEAP_START) - 256) 00151 #else /* GCC */ 00152 00155 #define NUTMEM_END (uptr_t)(NUTMEM_START + NUTMEM_SIZE - 1U) 00156 extern void *__heap_start; 00157 00158 #define HEAP_START &__heap_start 00159 #define HEAP_SIZE ((uptr_t) (NUTMEM_END - 256 - (uptr_t) (&__heap_start))) 00160 #endif 00161 00162 #if !defined(__arm__) && !defined(__cplusplus) 00163 extern void NutAppMain(void *arg) __attribute__ ((noreturn)); 00164 #else 00165 extern void main(void *); 00166 #endif 00167 00168 00169 #if defined(OLIMEX_LPCE2294) 00170 /* 00171 * InitHW for OLIMEX LPC-E2294 00172 */ 00173 static void InitHW (void) 00174 { 00175 PINSEL0 = 0; 00176 PINSEL1 = 0; 00177 00178 BCFG2 = 0x03501; 00179 PINSEL2 |= 0x00804000; 00180 } /* InitHW */ 00181 00182 #endif /* OLIMEX_LPCE2294 */ 00183 00184 00185 00193 THREAD(NutIdle, arg) 00194 { 00195 #if defined(MCU_GBA) || defined(MCU_LPC2XXX) 00196 InitIrqHandler(); 00197 #endif 00198 /* Initialize system timers. */ 00199 NutTimerInit(); 00200 00201 /* Create the main application thread. */ 00202 NutThreadCreate("main", main, 0, NUT_THREAD_MAINSTACK); 00203 00204 /* 00205 * Run in an idle loop at the lowest priority. We can still 00206 * do something useful here, like killing terminated threads 00207 * or putting the CPU into sleep mode. 00208 */ 00209 NutThreadSetPriority(254); 00210 for (;;) { 00211 NutThreadYield(); 00212 NutThreadDestroy(); 00213 } 00214 } 00215 00223 void NutInit(void) 00224 { 00225 #if defined(OLIMEX_LPCE2294) 00226 InitHW(); 00227 #elif defined(MCU_AT91R40008) || defined (MCU_AT91SAM7X256) || defined (MCU_AT91SAM7S256) || defined (MCU_AT91SAM9260) || defined(MCU_AT91SAM7SE512) || defined(MCU_AT91SAM9XE512) 00228 McuInit(); 00229 #endif 00230 #if defined(MCU_AT91SAM7X256) || defined (MCU_AT91SAM7S256) || defined(MCU_AT91SAM7SE512) 00231 { 00232 uint32_t freq = NutGetCpuClock(); 00233 /* Set Flash Waite state. */ 00234 outr(MC_FMR, ((((freq + freq / 2) / 1000000UL) & 0xFF) << 16) | MC_FWS_2R3W); 00235 } 00236 #endif 00237 00238 NutHeapAdd(HEAP_START, HEAP_SIZE); 00239 00240 /* 00241 * No EEPROM configuration. 00242 */ 00243 strcpy(confos.hostname, "ethernut"); 00244 00245 /* 00246 * Create idle thread 00247 */ 00248 NutThreadCreate("idle", NutIdle, 0, NUT_THREAD_IDLESTACK); 00249 } 00250