context_icc.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: context_icc.c,v $
00036  * Revision 1.6  2006/09/29 12:27:31  haraldkipp
00037  * All code should use dedicated stack allocation routines. For targets
00038  * allocating stack from the normal heap the API calls are remapped by
00039  * preprocessor macros.
00040  *
00041  * Revision 1.5  2006/07/10 08:46:52  haraldkipp
00042  * Properly set 3 byte return address for extended AVR.
00043  *
00044  * Revision 1.4  2006/02/08 15:20:21  haraldkipp
00045  * ATmega2561 Support
00046  *
00047  * Revision 1.3  2005/10/04 05:17:15  hwmaier
00048  * Added support for separating stack and conventional heap as required by AT09CAN128 MCUs
00049  *
00050  * Revision 1.2  2005/08/02 17:46:46  haraldkipp
00051  * Major API documentation update.
00052  *
00053  * Revision 1.1  2005/07/26 18:10:49  haraldkipp
00054  * Moved from os/thread.c
00055  *
00056  * Revision 1.2  2005/07/14 08:55:57  freckle
00057  * Rewrote CS in NutThreadCreate
00058  *
00059  * Revision 1.1  2005/05/27 17:17:31  drsung
00060  * Moved the file
00061  *
00062  * Revision 1.6  2005/04/30 16:42:42  chaac
00063  * Fixed bug in handling of NUTDEBUG. Added include for cfg/os.h. If NUTDEBUG
00064  * is defined in NutConf, it will make effect where it is used.
00065  *
00066  * Revision 1.5  2005/02/16 19:55:18  haraldkipp
00067  * Ready-to-run queue handling removed from interrupt context.
00068  * Avoid AVRGCC prologue and epilogue code. Thanks to Pete Allinson.
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  2004/09/22 08:15:56  haraldkipp
00074  * Speparate IRQ stack configurable
00075  *
00076  * Revision 1.2  2004/04/25 17:06:17  drsung
00077  * Separate IRQ stack now compatible with nested interrupts.
00078  *
00079  * Revision 1.1  2004/03/16 16:48:46  haraldkipp
00080  * Added Jan Dubiec's H8/300 port.
00081  *
00082  * Revision 1.2  2004/02/18 16:32:48  drsung
00083  * Bugfix in NutThreadCreate. Thanks to Mike Cornelius.
00084  *
00085  * Revision 1.1  2004/02/01 18:49:48  haraldkipp
00086  * Added CPU family support
00087  *
00088  */
00089 
00090 #include <cfg/os.h>
00091 #include <cfg/memory.h>
00092 
00093 #include <string.h>
00094 
00095 #include <sys/atom.h>
00096 #include <sys/heap.h>
00097 #include <sys/thread.h>
00098 
00103 
00110 typedef struct {
00111     u_char csf_r29;
00112     u_char csf_r28;
00113     u_char csf_r23;
00114     u_char csf_r22;
00115     u_char csf_r21;
00116     u_char csf_r20;
00117     u_char csf_r15;
00118     u_char csf_r14;
00119     u_char csf_r13;
00120     u_char csf_r12;
00121     u_char csf_r11;
00122     u_char csf_r10;
00123 #ifdef __AVR_ATmega2561__
00124     u_char csf_pcex;
00125 #endif
00126     u_char csf_pchi;
00127     u_char csf_pclo;
00128 } SWITCHFRAME;
00129 
00135 typedef struct {
00136     u_char cef_arghi;
00137     u_char cef_arglo;
00141     u_char cef_yhi;
00142     u_char cef_ylo;
00143     u_char cef_rampz;
00144     u_char cef_sreg;
00145     u_char cef_r1;
00146 #ifdef __AVR_ATmega2561__
00147     u_char cef_pcex;
00148 #endif
00149     u_char cef_pchi;
00150     u_char cef_pclo;
00151 } ENTERFRAME;
00152 
00153 
00154 /*
00155  * This code is executed when entering a thread.
00156  */
00157 static void NutThreadEntry(void)
00158 {
00159     asm("pop r17");             // first parameter for ICC
00160     asm("pop r16");
00161     asm("pop r29");             // SW-Stack; Y-Register
00162     asm("pop r28");
00163     asm("pop r0");              // r0 = _tmp_reg_
00164     asm("out 0x3B, r0");        // RAMPZ; replace with define later
00165     asm("pop r0");
00166     asm("pop r1");              // r1 = _zero_reg_
00167     asm("out 0x3F, r0");        // SREG; replace with define later
00168     asm("reti");
00169 }
00170 
00182 void NutThreadSwitch(void)
00183 {
00184     /*
00185      * Save all CPU registers.
00186      */
00187     register u_char i = 0;
00188     register u_char j = 0;
00189 
00190     asm("push r10");
00191     asm("push r11");
00192     asm("push r12");
00193     asm("push r13");
00194     asm("push r14");
00195     asm("push r15");
00196     asm("push r20");
00197     asm("push r21");
00198     asm("push r22");
00199     asm("push r23");
00200     asm("push r28");
00201     asm("push r29");
00202     asm("in %i, $3D");          // SPL
00203     asm("in %j, $3E");          // SPH
00204 
00205     runningThread->td_sp = (((u_short) j) << 8) & 0xFF00 | (i & 0xFF);
00206 
00207     /*
00208      * This defines a global label, which may be called
00209      * as an entry point into this function.
00210      */
00211     asm(".globl thread_start");
00212     asm("thread_start:");
00213 
00214     /*
00215      * Reload CPU registers from the thread in front
00216      * of the run queue.
00217      */
00218     runningThread = runQueue;
00219     runningThread->td_state = TDS_RUNNING;
00220 
00221     i = (u_char) (runningThread->td_sp & 0xFF);
00222     j = (u_char) ((runningThread->td_sp >> 8) & 0xFF);
00223 
00224     asm("out $3D, %i");         // SPL
00225     asm("out $3E, %j");         // SPH
00226     asm("pop r29");
00227     asm("pop r28");
00228     asm("pop r23");
00229     asm("pop r22");
00230     asm("pop r21");
00231     asm("pop r20");
00232     asm("pop r15");
00233     asm("pop r14");
00234     asm("pop r13");
00235     asm("pop r12");
00236     asm("pop r11");
00237     asm("pop r10");
00238 }
00239 
00258 HANDLE NutThreadCreate(u_char * name, void (*fn) (void *), void *arg, size_t stackSize)
00259 {
00260     u_char *threadMem;
00261     SWITCHFRAME *sf;
00262     ENTERFRAME *ef;
00263     NUTTHREADINFO *td;
00264     u_short yreg;
00265     const u_char *paddr;
00266 
00267     /*
00268      * Allocate stack and thread info structure in one block.
00269      */
00270     if ((threadMem = NutStackAlloc(stackSize + sizeof(NUTTHREADINFO))) == 0) {
00271         return 0;
00272     }
00273 
00274     td = (NUTTHREADINFO *) (threadMem + stackSize);
00275     ef = (ENTERFRAME *) ((u_short) td - sizeof(ENTERFRAME));
00276     sf = (SWITCHFRAME *) ((u_short) ef - sizeof(SWITCHFRAME));
00277 
00278 
00279     memcpy(td->td_name, name, sizeof(td->td_name) - 1);
00280     td->td_name[sizeof(td->td_name) - 1] = 0;
00281     td->td_sp = (u_short) sf - 1;
00282     td->td_memory = threadMem;
00283     *((u_long *) threadMem) = DEADBEEF;
00284     *((u_long *) (threadMem + 4)) = DEADBEEF;
00285     *((u_long *) (threadMem + 8)) = DEADBEEF;
00286     *((u_long *) (threadMem + 12)) = DEADBEEF;
00287     td->td_priority = 64;
00288 
00289     /*
00290      * Setup entry frame to simulate C function entry.
00291      */
00292     paddr = (const u_char *) fn;
00293     ef->cef_pclo = *paddr;
00294     ef->cef_pchi = *(paddr + 1);
00295 #ifdef __AVR_ATmega2561__
00296     ef->cef_pcex = *(paddr + 2);
00297 #endif
00298     ef->cef_sreg = 0x80;
00299     ef->cef_rampz = 0;
00300     ef->cef_r1 = 0;
00301 
00302     ef->cef_arglo = (u_char) (((u_short) arg) & 0xff);
00303     ef->cef_arghi = (u_char) (((u_short) arg) >> 8);
00304 
00305     yreg = td->td_sp - 40;
00306     ef->cef_yhi = (u_char) (yreg >> 8);
00307     ef->cef_ylo = (u_char) (yreg & 0xFF);
00308 
00309     paddr = (const u_char *) NutThreadEntry;
00310     sf->csf_pclo = *paddr;
00311     sf->csf_pchi = *(paddr + 1);
00312 #ifdef __AVR_ATmega2561__
00313     sf->csf_pcex = *(paddr + 2);
00314 #endif
00315 
00316     /*
00317      * Insert into the thread list and the run queue.
00318      */
00319 
00320     td->td_next = nutThreadList;
00321     nutThreadList = td;
00322     td->td_state = TDS_READY;
00323     td->td_timer = 0;
00324     td->td_queue = 0;
00325 #ifdef NUTDEBUG
00326     if (__os_trf)
00327         fprintf(__os_trs, "Cre<%04x>", (uptr_t) td);
00328 #endif
00329 
00330     NutThreadAddPriQueue(td, (NUTTHREADINFO **) & runQueue);
00331 
00332 #ifdef NUTDEBUG
00333     if (__os_trf) {
00334         NutDumpThreadList(__os_trs);
00335         //NutDumpThreadQueue(__os_trs, runQueue);
00336     }
00337 #endif
00338 
00339     /*
00340      * If no thread is active, switch to new thread.
00341      */
00342     if (runningThread == 0) {
00343         NutEnterCritical();
00344         asm("rjmp thread_start");
00345         /* we will never come back here .. */
00346     }
00347 
00348     /*
00349      * If current context is not in front of
00350      * the run queue (highest priority), then
00351      * switch to the thread in front.
00352      */
00353     if (runningThread != runQueue) {
00354         runningThread->td_state = TDS_READY;
00355 #ifdef NUTDEBUG
00356         if (__os_trf)
00357             fprintf(__os_trs, "New<%04x %04x>", (uptr_t) runningThread, (uptr_t) runQueue);
00358 #endif
00359         NutEnterCritical();
00360         NutThreadSwitch();
00361         NutExitCritical();
00362     }
00363 
00364     return td;
00365 }
00366 

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