2 * Copyright (c) 2006, Technical University of Munich
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
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
29 * This file is part of the Contiki operating system.
31 * \file AVR specific implementation of multithreading architecture
33 * \author Adam Dunkels <adam@sics.se>
34 * \author Simon Barner <barner@in.tum.de>
36 * @(#)$Id: mtarch.c,v 1.1 2006/12/22 16:55:53 barner Exp $
40 #include <avr/interrupt.h>
43 #include "dev/rs232.h"
45 /*--------------------------------------------------------------------------*/
51 /*--------------------------------------------------------------------------*/
53 mtarch_start(struct mtarch_thread
*t
,
54 void (*function
)(void *), void *data
)
56 /* Initialize stack with number sequence (used for
57 * measuring stack usage */
60 for(i
= 0; i
< MTARCH_STACKSIZE
; ++i
) {
65 * Push pointer to mt_exit and the thread onto our stack:
67 * - The stack is defined as an array of bytes, but pointers are 16 bit wide
68 * - Function pointers are 16-bit addresses in flash ROM, but e.g.
69 * avr-objdump displays byte addresses
70 * - Get the high and low byte of the addresses onto the stack in the
74 /* Initialize stack. This is done in reverse order ("pushing") the
75 * pre-allocated array */
77 /* mt_exit function that is to be invoked if the thread dies */
78 t
->stack
[MTARCH_STACKSIZE
- 1] = (unsigned char)((unsigned short)mt_exit
) & 0xff;
79 t
->stack
[MTARCH_STACKSIZE
- 2] = (unsigned char)((unsigned short)mt_exit
>> 8) & 0xff;
81 /* The thread handler. Invoked when RET is called in mtarch_exec */
82 t
->stack
[MTARCH_STACKSIZE
- 3] = (unsigned char)((unsigned short)function
) & 0xff;
83 t
->stack
[MTARCH_STACKSIZE
- 4] = (unsigned char)((unsigned short)function
>> 8) & 0xff;
85 /* Register r0-r23 in t->stack[MTARCH_STACKSIZE - 5] to
86 * t->stack[MTARCH_STACKSIZE - 28].
88 * Per calling convention, the argument to the thread handler function
89 * (i.e. the 'data' pointer) is passed via r24-r25.
90 * See http://www.nongnu.org/avr-libc/user-manual/FAQ.html#faq_reg_usage) */
91 t
->stack
[MTARCH_STACKSIZE
- 29] = (unsigned char)((unsigned short)data
) & 0xff;
92 t
->stack
[MTARCH_STACKSIZE
- 30] = (unsigned char)((unsigned short)data
>> 8) & 0xff;
94 /* Initialize stack pointer: Space for 2 2-byte-addresses and 32 registers,
95 * post-decrement POP / pre-increment PUSH scheme */
96 t
->sp
= &t
->stack
[MTARCH_STACKSIZE
- 1 - 4 - 32];
99 /*--------------------------------------------------------------------------*/
100 static unsigned char *sptmp
;
101 static struct mtarch_thread
*running
;
106 /* Disable interrupts while we perform the context switch */
109 /* Push 32 general purpuse registers */
143 /* Switch stack pointer */
145 running
->sp
= (unsigned char*)SP
;
146 SP
= (unsigned short)sptmp
;
148 /* Pop 32 general purpose registers */
182 /* Renable interrupts */
185 /*--------------------------------------------------------------------------*/
187 mtarch_exec(struct mtarch_thread
*t
)
194 /*--------------------------------------------------------------------------*/
200 /*--------------------------------------------------------------------------*/
206 /*--------------------------------------------------------------------------*/
212 /*--------------------------------------------------------------------------*/
218 /*--------------------------------------------------------------------------*/
220 mtarch_stack_usage(struct mt_thread
*t
)
223 for(i
= 0; i
< MTARCH_STACKSIZE
; ++i
) {
224 if(t
->thread
.stack
[i
] != i
) {
228 return MTARCH_STACKSIZE
- i
;
230 /*--------------------------------------------------------------------------*/