Contiki 2.6

mtarch.c

00001 /*
00002  * Copyright (c) 2005, Swedish Institute of Computer Science
00003  * All rights reserved.
00004  *
00005  * Redistribution and use in source and binary forms, with or without
00006  * modification, are permitted provided that the following conditions
00007  * are met:
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 Institute nor the names of its contributors
00014  *    may be used to endorse or promote products derived from this software
00015  *    without specific prior written permission.
00016  *
00017  * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
00018  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00019  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
00020  * ARE DISCLAIMED.  IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
00021  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
00022  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
00023  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
00024  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
00025  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
00026  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
00027  * SUCH DAMAGE.
00028  *
00029  * This file is part of the Contiki operating system.
00030  *
00031  * @(#)$Id: mtarch.c,v 1.6 2008/11/21 10:28:32 fros4943 Exp $
00032  */
00033 
00034 #include <stdio.h>
00035 #include "sys/mt.h"
00036 
00037 #ifdef __IAR_SYSTEMS_ICC__
00038 #define __asm__ asm
00039 #endif
00040 
00041 static unsigned short *sptmp;
00042 static struct mtarch_thread *running;
00043 
00044 /*--------------------------------------------------------------------------*/
00045 void
00046 mtarch_init(void)
00047 {
00048 
00049 }
00050 /*--------------------------------------------------------------------------*/
00051 static void
00052 mtarch_wrapper(void)
00053 {
00054   /* Call thread function with argument */
00055   ((void (*)(void *))running->function)((void*)running->data);
00056 }
00057 /*--------------------------------------------------------------------------*/
00058 void
00059 mtarch_start(struct mtarch_thread *t,
00060              void (*function)(void *), void *data)
00061 {
00062   int i;
00063 
00064   for(i = 0; i < MTARCH_STACKSIZE; ++i) {
00065     t->stack[i] = i;
00066   }
00067 
00068   t->sp = &t->stack[MTARCH_STACKSIZE - 1];
00069 
00070   *t->sp = (unsigned short)mt_exit;
00071   --t->sp;
00072 
00073   *t->sp = (unsigned short)mtarch_wrapper;
00074   --t->sp;
00075 
00076   /* Space for registers. */
00077   t->sp -= 11;
00078 
00079   /* Store function and argument (used in mtarch_wrapper) */
00080   t->data = data;
00081   t->function = function;
00082 }
00083 /*--------------------------------------------------------------------------*/
00084 
00085 static void
00086 sw(void)
00087 {
00088 
00089   sptmp = running->sp;
00090 
00091   __asm__("push r4");
00092   __asm__("push r5");
00093   __asm__("push r6");
00094   __asm__("push r7");
00095   __asm__("push r8");
00096   __asm__("push r9");
00097   __asm__("push r10");
00098   __asm__("push r11");
00099   __asm__("push r12");
00100   __asm__("push r13");
00101   __asm__("push r14");
00102   __asm__("push r15");
00103 
00104 #ifdef __IAR_SYSTEMS_ICC__
00105 /*  use IAR intrinsic functions */
00106   running->sp = (unsigned short *) __get_SP_register();
00107   __set_SP_register((unsigned short) sptmp);
00108 #else
00109   __asm__("mov.w r1,%0" : "=r" (running->sp));
00110   __asm__("mov.w %0,r1" : : "m" (sptmp));
00111 #endif
00112 
00113   __asm__("pop r15");
00114   __asm__("pop r14");
00115   __asm__("pop r13");
00116   __asm__("pop r12");
00117   __asm__("pop r11");
00118   __asm__("pop r10");
00119   __asm__("pop r9");
00120   __asm__("pop r8");
00121   __asm__("pop r7");
00122   __asm__("pop r6");
00123   __asm__("pop r5");
00124   __asm__("pop r4");
00125 }
00126 /*--------------------------------------------------------------------------*/
00127 void
00128 mtarch_exec(struct mtarch_thread *t)
00129 {
00130   running = t;
00131   sw();
00132   running = NULL;
00133 }
00134 /*--------------------------------------------------------------------------*/
00135 void
00136 mtarch_remove(void)
00137 {
00138 
00139 }
00140 /*--------------------------------------------------------------------------*/
00141 void
00142 mtarch_yield(void)
00143 {
00144   sw();
00145 }
00146 /*--------------------------------------------------------------------------*/
00147 void
00148 mtarch_pstop(void)
00149 {
00150 
00151 }
00152 /*--------------------------------------------------------------------------*/
00153 void
00154 mtarch_pstart(void)
00155 {
00156 
00157 }
00158 /*--------------------------------------------------------------------------*/
00159 void
00160 mtarch_stop(struct mtarch_thread *thread)
00161 {
00162 
00163 }
00164 /*--------------------------------------------------------------------------*/
00165 int
00166 mtarch_stack_usage(struct mt_thread *t)
00167 {
00168   int i;
00169 
00170   for(i = 0; i < MTARCH_STACKSIZE; ++i) {
00171     if(t->thread.stack[i] != (unsigned short)i) {
00172       return MTARCH_STACKSIZE - i;
00173     }
00174   }
00175 
00176   return MTARCH_STACKSIZE;
00177 }
00178 /*--------------------------------------------------------------------------*/