2006-06-17 22:41:10 +00:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2005, Swedish Institute of Computer Science
|
2006-09-01 22:56:18 +00:00
|
|
|
* All rights reserved.
|
2006-06-17 22:41:10 +00:00
|
|
|
*
|
2006-09-01 22:56:18 +00:00
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
|
|
|
* 3. Neither the name of the Institute nor the names of its contributors
|
|
|
|
* may be used to endorse or promote products derived from this software
|
|
|
|
* without specific prior written permission.
|
2006-06-17 22:41:10 +00:00
|
|
|
*
|
2006-09-01 22:56:18 +00:00
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
|
|
|
|
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
|
|
* ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
|
|
|
|
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
|
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
|
|
|
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
|
|
|
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
|
|
|
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
|
|
|
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
|
|
|
* SUCH DAMAGE.
|
2006-06-17 22:41:10 +00:00
|
|
|
*
|
|
|
|
* This file is part of the Contiki operating system.
|
|
|
|
*
|
2008-11-21 10:28:32 +00:00
|
|
|
* @(#)$Id: mtarch.c,v 1.6 2008/11/21 10:28:32 fros4943 Exp $
|
2006-06-17 22:41:10 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include <stdio.h>
|
2006-09-01 22:56:18 +00:00
|
|
|
#include "sys/mt.h"
|
2006-06-17 22:41:10 +00:00
|
|
|
|
2011-04-06 18:08:48 +00:00
|
|
|
#ifdef __IAR_SYSTEMS_ICC__
|
|
|
|
#define __asm__ asm
|
|
|
|
#endif
|
|
|
|
|
2008-11-21 10:28:32 +00:00
|
|
|
static unsigned short *sptmp;
|
|
|
|
static struct mtarch_thread *running;
|
2006-06-17 22:41:10 +00:00
|
|
|
|
|
|
|
/*--------------------------------------------------------------------------*/
|
|
|
|
void
|
|
|
|
mtarch_init(void)
|
|
|
|
{
|
2008-08-27 13:10:29 +00:00
|
|
|
|
2008-11-21 10:28:32 +00:00
|
|
|
}
|
|
|
|
/*--------------------------------------------------------------------------*/
|
|
|
|
static void
|
|
|
|
mtarch_wrapper(void)
|
|
|
|
{
|
|
|
|
/* Call thread function with argument */
|
|
|
|
((void (*)(void *))running->function)((void*)running->data);
|
2006-06-17 22:41:10 +00:00
|
|
|
}
|
|
|
|
/*--------------------------------------------------------------------------*/
|
|
|
|
void
|
|
|
|
mtarch_start(struct mtarch_thread *t,
|
|
|
|
void (*function)(void *), void *data)
|
|
|
|
{
|
2006-09-26 20:56:42 +00:00
|
|
|
int i;
|
|
|
|
|
|
|
|
for(i = 0; i < MTARCH_STACKSIZE; ++i) {
|
|
|
|
t->stack[i] = i;
|
|
|
|
}
|
2006-06-17 22:41:10 +00:00
|
|
|
|
|
|
|
t->sp = &t->stack[MTARCH_STACKSIZE - 1];
|
2006-09-26 20:56:42 +00:00
|
|
|
|
2006-06-17 22:41:10 +00:00
|
|
|
*t->sp = (unsigned short)mt_exit;
|
|
|
|
--t->sp;
|
2006-09-26 20:56:42 +00:00
|
|
|
|
2008-11-21 10:28:32 +00:00
|
|
|
*t->sp = (unsigned short)mtarch_wrapper;
|
2006-09-01 22:56:18 +00:00
|
|
|
--t->sp;
|
2006-09-26 20:56:42 +00:00
|
|
|
|
2006-06-17 22:41:10 +00:00
|
|
|
/* Space for registers. */
|
|
|
|
t->sp -= 11;
|
2008-11-21 10:28:32 +00:00
|
|
|
|
|
|
|
/* Store function and argument (used in mtarch_wrapper) */
|
|
|
|
t->data = data;
|
|
|
|
t->function = function;
|
2006-06-17 22:41:10 +00:00
|
|
|
}
|
|
|
|
/*--------------------------------------------------------------------------*/
|
|
|
|
|
|
|
|
static void
|
|
|
|
sw(void)
|
|
|
|
{
|
|
|
|
|
|
|
|
sptmp = running->sp;
|
2008-08-27 13:10:29 +00:00
|
|
|
|
2006-06-17 22:41:10 +00:00
|
|
|
__asm__("push r4");
|
|
|
|
__asm__("push r5");
|
|
|
|
__asm__("push r6");
|
|
|
|
__asm__("push r7");
|
|
|
|
__asm__("push r8");
|
|
|
|
__asm__("push r9");
|
|
|
|
__asm__("push r10");
|
|
|
|
__asm__("push r11");
|
|
|
|
__asm__("push r12");
|
|
|
|
__asm__("push r13");
|
|
|
|
__asm__("push r14");
|
|
|
|
__asm__("push r15");
|
2008-08-27 13:10:29 +00:00
|
|
|
|
2011-04-06 18:08:48 +00:00
|
|
|
#ifdef __IAR_SYSTEMS_ICC__
|
|
|
|
/* use IAR intrinsic functions */
|
|
|
|
running->sp = (unsigned short *) __get_SP_register();
|
|
|
|
__set_SP_register((unsigned short) sptmp);
|
|
|
|
#else
|
2006-06-17 22:41:10 +00:00
|
|
|
__asm__("mov.w r1,%0" : "=r" (running->sp));
|
|
|
|
__asm__("mov.w %0,r1" : : "m" (sptmp));
|
2011-04-06 18:08:48 +00:00
|
|
|
#endif
|
2006-06-17 22:41:10 +00:00
|
|
|
|
|
|
|
__asm__("pop r15");
|
|
|
|
__asm__("pop r14");
|
|
|
|
__asm__("pop r13");
|
|
|
|
__asm__("pop r12");
|
|
|
|
__asm__("pop r11");
|
|
|
|
__asm__("pop r10");
|
|
|
|
__asm__("pop r9");
|
|
|
|
__asm__("pop r8");
|
|
|
|
__asm__("pop r7");
|
|
|
|
__asm__("pop r6");
|
|
|
|
__asm__("pop r5");
|
|
|
|
__asm__("pop r4");
|
|
|
|
}
|
|
|
|
/*--------------------------------------------------------------------------*/
|
|
|
|
void
|
|
|
|
mtarch_exec(struct mtarch_thread *t)
|
|
|
|
{
|
|
|
|
running = t;
|
|
|
|
sw();
|
|
|
|
running = NULL;
|
|
|
|
}
|
|
|
|
/*--------------------------------------------------------------------------*/
|
|
|
|
void
|
|
|
|
mtarch_remove(void)
|
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
/*--------------------------------------------------------------------------*/
|
|
|
|
void
|
|
|
|
mtarch_yield(void)
|
|
|
|
{
|
|
|
|
sw();
|
|
|
|
}
|
|
|
|
/*--------------------------------------------------------------------------*/
|
|
|
|
void
|
|
|
|
mtarch_pstop(void)
|
|
|
|
{
|
2008-08-27 13:10:29 +00:00
|
|
|
|
2006-06-17 22:41:10 +00:00
|
|
|
}
|
|
|
|
/*--------------------------------------------------------------------------*/
|
|
|
|
void
|
|
|
|
mtarch_pstart(void)
|
|
|
|
{
|
2008-08-27 13:10:29 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
/*--------------------------------------------------------------------------*/
|
|
|
|
void
|
|
|
|
mtarch_stop(struct mtarch_thread *thread)
|
|
|
|
{
|
|
|
|
|
2006-06-17 22:41:10 +00:00
|
|
|
}
|
|
|
|
/*--------------------------------------------------------------------------*/
|
2006-09-26 20:56:42 +00:00
|
|
|
int
|
|
|
|
mtarch_stack_usage(struct mt_thread *t)
|
|
|
|
{
|
|
|
|
int i;
|
2007-09-14 20:21:01 +00:00
|
|
|
|
2006-09-26 20:56:42 +00:00
|
|
|
for(i = 0; i < MTARCH_STACKSIZE; ++i) {
|
|
|
|
if(t->thread.stack[i] != (unsigned short)i) {
|
|
|
|
return MTARCH_STACKSIZE - i;
|
|
|
|
}
|
|
|
|
}
|
2007-09-14 20:21:01 +00:00
|
|
|
|
|
|
|
return MTARCH_STACKSIZE;
|
2006-09-26 20:56:42 +00:00
|
|
|
}
|
|
|
|
/*--------------------------------------------------------------------------*/
|