Contiki 2.5
mtarch.c
1 /*
2  * Copyright (c) 2005, Swedish Institute of Computer Science
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  * notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  * notice, this list of conditions and the following disclaimer in the
12  * documentation and/or other materials provided with the distribution.
13  * 3. Neither the name of the Institute nor the names of its contributors
14  * may be used to endorse or promote products derived from this software
15  * without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
18  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
21  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27  * SUCH DAMAGE.
28  *
29  * This file is part of the Contiki operating system.
30  *
31  * @(#)$Id: mtarch.c,v 1.6 2008/11/21 10:28:32 fros4943 Exp $
32  */
33 
34 #include <stdio.h>
35 #include "sys/mt.h"
36 
37 #ifdef __IAR_SYSTEMS_ICC__
38 #define __asm__ asm
39 #endif
40 
41 static unsigned short *sptmp;
42 static struct mtarch_thread *running;
43 
44 /*--------------------------------------------------------------------------*/
45 void
47 {
48 
49 }
50 /*--------------------------------------------------------------------------*/
51 static void
52 mtarch_wrapper(void)
53 {
54  /* Call thread function with argument */
55  ((void (*)(void *))running->function)((void*)running->data);
56 }
57 /*--------------------------------------------------------------------------*/
58 void
59 mtarch_start(struct mtarch_thread *t,
60  void (*function)(void *), void *data)
61 {
62  int i;
63 
64  for(i = 0; i < MTARCH_STACKSIZE; ++i) {
65  t->stack[i] = i;
66  }
67 
68  t->sp = &t->stack[MTARCH_STACKSIZE - 1];
69 
70  *t->sp = (unsigned short)mt_exit;
71  --t->sp;
72 
73  *t->sp = (unsigned short)mtarch_wrapper;
74  --t->sp;
75 
76  /* Space for registers. */
77  t->sp -= 11;
78 
79  /* Store function and argument (used in mtarch_wrapper) */
80  t->data = data;
81  t->function = function;
82 }
83 /*--------------------------------------------------------------------------*/
84 
85 static void
86 sw(void)
87 {
88 
89  sptmp = running->sp;
90 
91  __asm__("push r4");
92  __asm__("push r5");
93  __asm__("push r6");
94  __asm__("push r7");
95  __asm__("push r8");
96  __asm__("push r9");
97  __asm__("push r10");
98  __asm__("push r11");
99  __asm__("push r12");
100  __asm__("push r13");
101  __asm__("push r14");
102  __asm__("push r15");
103 
104 #ifdef __IAR_SYSTEMS_ICC__
105 /* use IAR intrinsic functions */
106  running->sp = (unsigned short *) __get_SP_register();
107  __set_SP_register((unsigned short) sptmp);
108 #else
109  __asm__("mov.w r1,%0" : "=r" (running->sp));
110  __asm__("mov.w %0,r1" : : "m" (sptmp));
111 #endif
112 
113  __asm__("pop r15");
114  __asm__("pop r14");
115  __asm__("pop r13");
116  __asm__("pop r12");
117  __asm__("pop r11");
118  __asm__("pop r10");
119  __asm__("pop r9");
120  __asm__("pop r8");
121  __asm__("pop r7");
122  __asm__("pop r6");
123  __asm__("pop r5");
124  __asm__("pop r4");
125 }
126 /*--------------------------------------------------------------------------*/
127 void
128 mtarch_exec(struct mtarch_thread *t)
129 {
130  running = t;
131  sw();
132  running = NULL;
133 }
134 /*--------------------------------------------------------------------------*/
135 void
137 {
138 
139 }
140 /*--------------------------------------------------------------------------*/
141 void
143 {
144  sw();
145 }
146 /*--------------------------------------------------------------------------*/
147 void
148 mtarch_pstop(void)
149 {
150 
151 }
152 /*--------------------------------------------------------------------------*/
153 void
154 mtarch_pstart(void)
155 {
156 
157 }
158 /*--------------------------------------------------------------------------*/
159 void
160 mtarch_stop(struct mtarch_thread *thread)
161 {
162 
163 }
164 /*--------------------------------------------------------------------------*/
165 int
166 mtarch_stack_usage(struct mt_thread *t)
167 {
168  int i;
169 
170  for(i = 0; i < MTARCH_STACKSIZE; ++i) {
171  if(t->thread.stack[i] != (unsigned short)i) {
172  return MTARCH_STACKSIZE - i;
173  }
174  }
175 
176  return MTARCH_STACKSIZE;
177 }
178 /*--------------------------------------------------------------------------*/