Contiki 3.x
mtarch.c
1 /*
2  * Copyright (c) 2004, Adam Dunkels.
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  * Author: Adam Dunkels <adam@sics.se>
32  *
33  */
34 
35 #include <string.h>
36 
37 #include "sys/mtarch.h"
38 
39 unsigned char mtarch_asm_threadspreg;
40 unsigned char *mtarch_asm_threadzp;
41 unsigned char *mtarch_asm_threadstack;
42 
43 void mtarch_asm_start(void);
44 void mtarch_asm_yield(void);
45 void mtarch_asm_exec(void);
46 
47 /*--------------------------------------------------------------------------*/
48 void
50 {
51 }
52 /*--------------------------------------------------------------------------*/
53 void
55 {
56 }
57 /*--------------------------------------------------------------------------*/
58 void
59 mtarch_start(struct mtarch_thread *thread,
60  void (* function)(void *data),
61  void *data)
62 {
63  memset(thread->cpustack, 0, sizeof(thread->cpustack));
64  memset(thread->cstack, 0, sizeof(thread->cstack));
65 
66  /* Copy current zero page content as template. */
67  mtarch_asm_threadzp = thread->zp;
68  mtarch_asm_start();
69 
70  /* Create a CPU stack frame with the appropriate values. */
71  thread->cpustack[MTARCH_CPUSTACKSIZE - 2] = ((unsigned short)function) / 0x100; /* high byte of return address */
72  thread->cpustack[MTARCH_CPUSTACKSIZE - 3] = ((unsigned short)function) % 0x100; /* low byte of return address */
73  thread->cpustack[MTARCH_CPUSTACKSIZE - 4] = 0x21; /* processor flags */
74  thread->cpustack[MTARCH_CPUSTACKSIZE - 5] = /* a register */
75  thread->cpustack[MTARCH_CPUSTACKSIZE - 6] = /* x register */
76  thread->cpustack[MTARCH_CPUSTACKSIZE - 7] = 0x00; /* y register */
77  thread->spreg = MTARCH_CPUSTACKSIZE - 8;
78 
79  /* Setup the C stack with the data pointer. */
80  thread->cstack[MTARCH_CSTACKSIZE - 2] = ((unsigned short)data) / 0x100; /* high byte of data pointer */
81  thread->cstack[MTARCH_CSTACKSIZE - 3] = ((unsigned short)data) % 0x100; /* low byte of data pointer */
82 
83  /* Setup the C stack pointer. */
84  thread->zp[1] = ((size_t)&thread->cstack[MTARCH_CSTACKSIZE - 3]) / 0x100; /* high byte of C stack pointer */
85  thread->zp[0] = ((size_t)&thread->cstack[MTARCH_CSTACKSIZE - 3]) % 0x100; /* low byte of C stack pointer */
86 }
87 /*--------------------------------------------------------------------------*/
88 void
90 {
91  mtarch_asm_yield();
92 }
93 /*--------------------------------------------------------------------------*/
94 void
95 mtarch_exec(struct mtarch_thread *thread)
96 {
97  /* Switch processor stack. The call to mtarch_asm_switch() will not
98  return until the process that we switch to calls yield(). */
99  mtarch_asm_threadspreg = thread->spreg;
100 
101  mtarch_asm_threadstack = thread->cpustack;
102  mtarch_asm_threadzp = thread->zp;
103 
104  mtarch_asm_exec();
105 
106  thread->spreg = mtarch_asm_threadspreg;
107 }
108 /*--------------------------------------------------------------------------*/
109 void
110 mtarch_stop(struct mtarch_thread *thread)
111 {
112 }
113 /*--------------------------------------------------------------------------*/
114 void
115 mtarch_pstart(void)
116 {
117 }
118 /*--------------------------------------------------------------------------*/
119 void
120 mtarch_pstop(void)
121 {
122 }
123 /*--------------------------------------------------------------------------*/
void mtarch_init(void)
Initialize the architecture specific support functions for the multi-thread library.
Definition: mtarch.c:49
void mtarch_yield(void)
Yield the processor.
Definition: mtarch.c:89
void mtarch_remove(void)
Uninstall library and clean up.
Definition: mtarch.c:54
void mtarch_start(struct mtarch_thread *thread, void(*function)(void *data), void *data)
Setup the stack frame for a thread that is being started.
Definition: mtarch.c:59
void mtarch_exec(struct mtarch_thread *thread)
Start executing a thread.
Definition: mtarch.c:95
void mtarch_stop(struct mtarch_thread *thread)
Clean up the stack of a thread.
Definition: mtarch.c:110
Copyright (c) 2014, Analog Devices, Inc.
Definition: mtarch.h:42