Contiki-Inga 3.x
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  */
32 
33 #include <stdio.h>
34 #include "sys/mt.h"
35 
36 #ifdef __IAR_SYSTEMS_ICC__
37 #define __asm__ asm
38 #endif
39 
40 static unsigned short *sptmp;
41 static struct mtarch_thread *running;
42 
43 /*--------------------------------------------------------------------------*/
44 void
46 {
47 
48 }
49 /*--------------------------------------------------------------------------*/
50 static void
51 mtarch_wrapper(void)
52 {
53  /* Call thread function with argument */
54  ((void (*)(void *))running->function)((void*)running->data);
55 }
56 /*--------------------------------------------------------------------------*/
57 void
58 mtarch_start(struct mtarch_thread *t,
59  void (*function)(void *), void *data)
60 {
61  int i;
62 
63  for(i = 0; i < MTARCH_STACKSIZE; ++i) {
64  t->stack[i] = i;
65  }
66 
67  t->sp = &t->stack[MTARCH_STACKSIZE - 1];
68 
69  *t->sp = (unsigned short)mt_exit;
70  --t->sp;
71 
72  *t->sp = (unsigned short)mtarch_wrapper;
73  --t->sp;
74 
75  /* Space for registers. */
76  t->sp -= 11;
77 
78  /* Store function and argument (used in mtarch_wrapper) */
79  t->data = data;
80  t->function = function;
81 }
82 /*--------------------------------------------------------------------------*/
83 
84 static void
85 sw(void)
86 {
87 
88  sptmp = running->sp;
89 
90  __asm__("push r4");
91  __asm__("push r5");
92  __asm__("push r6");
93  __asm__("push r7");
94  __asm__("push r8");
95  __asm__("push r9");
96  __asm__("push r10");
97  __asm__("push r11");
98  __asm__("push r12");
99  __asm__("push r13");
100  __asm__("push r14");
101  __asm__("push r15");
102 
103 #ifdef __IAR_SYSTEMS_ICC__
104 /* use IAR intrinsic functions */
105  running->sp = (unsigned short *) __get_SP_register();
106  __set_SP_register((unsigned short) sptmp);
107 #else
108  __asm__("mov.w r1,%0" : "=r" (running->sp));
109  __asm__("mov.w %0,r1" : : "m" (sptmp));
110 #endif
111 
112  __asm__("pop r15");
113  __asm__("pop r14");
114  __asm__("pop r13");
115  __asm__("pop r12");
116  __asm__("pop r11");
117  __asm__("pop r10");
118  __asm__("pop r9");
119  __asm__("pop r8");
120  __asm__("pop r7");
121  __asm__("pop r6");
122  __asm__("pop r5");
123  __asm__("pop r4");
124 }
125 /*--------------------------------------------------------------------------*/
126 void
127 mtarch_exec(struct mtarch_thread *t)
128 {
129  running = t;
130  sw();
131  running = NULL;
132 }
133 /*--------------------------------------------------------------------------*/
134 void
136 {
137 
138 }
139 /*--------------------------------------------------------------------------*/
140 void
142 {
143  sw();
144 }
145 /*--------------------------------------------------------------------------*/
146 void
147 mtarch_pstop(void)
148 {
149 
150 }
151 /*--------------------------------------------------------------------------*/
152 void
153 mtarch_pstart(void)
154 {
155 
156 }
157 /*--------------------------------------------------------------------------*/
158 void
159 mtarch_stop(struct mtarch_thread *thread)
160 {
161 
162 }
163 /*--------------------------------------------------------------------------*/
164 int
165 mtarch_stack_usage(struct mt_thread *t)
166 {
167  int i;
168 
169  for(i = 0; i < MTARCH_STACKSIZE; ++i) {
170  if(t->thread.stack[i] != (unsigned short)i) {
171  return MTARCH_STACKSIZE - i;
172  }
173  }
174 
175  return MTARCH_STACKSIZE;
176 }
177 /*--------------------------------------------------------------------------*/