From b0211916c74480cb4585ee190f5085c61ffccbaa Mon Sep 17 00:00:00 2001 From: patacongo Date: Wed, 13 May 2009 19:53:16 +0000 Subject: [PATCH] Add basic context switching logic git-svn-id: svn://svn.code.sf.net/p/nuttx/code/trunk@1777 42af7a65-404d-4744-a932-0658087f49c3 --- arch/arm/src/lm3s/Make.defs | 6 +- arch/arm/src/lm3s/lm3s_context.S | 145 ++++++++++++++++++++++++++++++ arch/arm/src/lm3s/lm3s_internal.h | 13 +++ arch/arm/src/lm3s/lm3s_irq.c | 15 ++-- arch/arm/src/lm3s/lm3s_pendsv.c | 118 ++++++++++++++++++++++++ arch/arm/src/lm3s/lm3s_vectors.S | 3 +- 6 files changed, 287 insertions(+), 13 deletions(-) create mode 100644 arch/arm/src/lm3s/lm3s_context.S create mode 100644 arch/arm/src/lm3s/lm3s_pendsv.c diff --git a/arch/arm/src/lm3s/Make.defs b/arch/arm/src/lm3s/Make.defs index b312d330c8..0684a35e72 100644 --- a/arch/arm/src/lm3s/Make.defs +++ b/arch/arm/src/lm3s/Make.defs @@ -45,9 +45,9 @@ CMN_CSRCS = up_allocateheap.c up_assert.c up_blocktask.c up_copystate.c \ up_sigdeliver.c up_syscall.c up_unblocktask.c \ up_undefinedinsn.c up_usestack.c -CHIP_ASRCS = -CHIP_CSRCS = lm3s_start.c lm3s_syscontrol.c lm3s_irq.c lm3s_timerisr.c \ - lm3s_lowputc.c lm3s_serial.c +CHIP_ASRCS = lm3s_context.S +CHIP_CSRCS = lm3s_start.c lm3s_syscontrol.c lm3s_irq.c lm3s_pendsv.c \ + lm3s_timerisr.c lm3s_lowputc.c lm3s_serial.c ifdef CONFIG_NET CHIP_CSRCS += lm3s_ethernet.c diff --git a/arch/arm/src/lm3s/lm3s_context.S b/arch/arm/src/lm3s/lm3s_context.S new file mode 100644 index 0000000000..a0a75fea44 --- /dev/null +++ b/arch/arm/src/lm3s/lm3s_context.S @@ -0,0 +1,145 @@ +/************************************************************************************ + * arch/arm/src/lm3s/lm3s_context.S + * arch/arm/src/chip/lm3s_context.S + * + * Copyright (C) 2009 Gregory Nutt. All rights reserved. + * Author: Gregory Nutt + * + * 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 NuttX nor the names of its contributors may be + * used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS 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 + * COPYRIGHT OWNER 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. + * + ************************************************************************************/ + +/************************************************************************************ + * Included Files + ************************************************************************************/ + +#include +#include +#include "cortexm3_nvic.h" + +/************************************************************************************ + * Preprocessor Definitions + ************************************************************************************/ + +/************************************************************************************ + * Global Symbols + ************************************************************************************/ + + .syntax unified + .thumb + .file "lm3s_context.S" + +/************************************************************************************ + * Macros + ************************************************************************************/ + +/************************************************************************************ + * Public Functions + ************************************************************************************/ + +/************************************************************************** + * Name: up_saveusercontext + * + * Description: + * Save the current thread context. Full prototype is: + * + * int up_saveusercontext(uint32 *regs); + * + * Return: + * 0: Normal return + * 1: Context switch return + * + **************************************************************************/ + + .text + .thumb_func + .globl up_saveusercontext + .type up_saveusercontext, function +up_saveusercontext: + + /* Perform the System call with R0=0 and R1=regs */ + + ldr r2, nvic_intctrl /* R2: Address NVIC INTCTRL registers */ + ldr r3, [r2, #0] /* R3: Contentx of NVIC INCTRL */ + orr r3, r3, #NVIC_INTCTRL_PENDSVSET + mov r1, r0 /* R1: regs */ + mov r0, #0 /* R0: 0 means save context (also return value) */ + str r3, [r2, #0] /* Force Pend SV */ + + /* There are two return conditions. On the first return, R0 (the + * return value will be zero. On the second return we need to + * force R0 to be 1. + */ + + add r2, r1, #(4*REG_R0) + mov r3, #1 + str r3, [r2, #0] + bx lr /* "normal" return with r0=0 or + * context switch with r0=1 */ + .size up_saveusercontext, .-up_saveusercontext + + +/************************************************************************** + * Name: up_fullcontextrestore + * + * Description: + * Restore the current thread context. Full prototype is: + * + * void up_fullcontextrestore(uint32 *regs) __attribute__ ((noreturn)); + * + * Return: + * None + * + **************************************************************************/ + + .thumb_func + .globl up_fullcontextrestore + .type up_fullcontextrestore, function +up_fullcontextrestore: + + /* Perform the System call with R0=1 and R1=regs */ + + ldr r2, nvic_intctrl /* R2: Address NVIC INTCTRL registers */ + ldr r3, [r2, #0] /* R3: Contentx of NVIC INCTRL */ + orr r3, r3, #NVIC_INTCTRL_PENDSVSET + mov r1, r0 /* R1: regs */ + mov r0, #1 /* R0: 1 means restore context */ + str r3, [r2, #0] /* Force Pend SV */ + + /* These call should not return */ + + bx lr /* Unnecessary ... will not return */ + .size up_fullcontextrestore, .-up_fullcontextrestore + + .align 2 + .type nvic_intctrl, object +nvic_intctrl: + .word NVIC_INTCTRL + .size nvic_intctrl, .-nvic_intctrl + .end + diff --git a/arch/arm/src/lm3s/lm3s_internal.h b/arch/arm/src/lm3s/lm3s_internal.h index 349a3461ea..3f8540dd51 100644 --- a/arch/arm/src/lm3s/lm3s_internal.h +++ b/arch/arm/src/lm3s/lm3s_internal.h @@ -43,6 +43,10 @@ #include #include +#include "up_internal.h" +#include "lm3s_memorymap.h" +#include "lm3s_gpio.h" + /************************************************************************************ * Definitions ************************************************************************************/ @@ -247,6 +251,15 @@ EXTERN void up_clockconfig(void); EXTERN int lm3s_configgpio(uint32 bitset); +/**************************************************************************** + * Name: lm3s_pendsv + * + * Description: + * This is PendSV exception handler that performs context switching + * + ****************************************************************************/ + +EXTERN int lm3s_pendsv(int irq, FAR void *context); #undef EXTERN #if defined(__cplusplus) diff --git a/arch/arm/src/lm3s/lm3s_irq.c b/arch/arm/src/lm3s/lm3s_irq.c index 04110e3cfe..4b3c404e05 100644 --- a/arch/arm/src/lm3s/lm3s_irq.c +++ b/arch/arm/src/lm3s/lm3s_irq.c @@ -173,6 +173,12 @@ void up_irqinitialize(void) current_regs = NULL; + /* Attach the PendSV exception handler. This is used for performing + * context switches. + */ + + irq_attach(LMSB_IRQ_PENDSV, lm3s_pendsv); + /* Attach all processor exceptions (except reset and sys tick) */ #ifdef CONFIG_DEBUG @@ -183,7 +189,6 @@ void up_irqinitialize(void) irq_attach(LMSB_IRQ_USAGEFAULT, lm3s_usagefault); irq_attach(LMSB_IRQ_SVCALL, lm3s_svcall); irq_attach(LMSB_IRQ_DBGMONITOR, lm3s_dbgmonitor); - irq_attach(LMSB_IRQ_PENDSV, lm3s_pendsv); irq_attach(LMSB_IRQ_RESERVED, lm3s_reserved); #endif @@ -368,14 +373,6 @@ int lm3s_dbgmonitor(int irq, FAR void *context) return 0; } -int lm3s_pendsv(int irq, FAR void *context) -{ - (void)irqsave(); - dbg("PANIC!!! PendSV received\n"); - PANIC(OSERR_UNEXPECTEDISR); - return 0; -} - int lm3s_reserved(int irq, FAR void *context) { (void)irqsave(); diff --git a/arch/arm/src/lm3s/lm3s_pendsv.c b/arch/arm/src/lm3s/lm3s_pendsv.c new file mode 100644 index 0000000000..1db255fef7 --- /dev/null +++ b/arch/arm/src/lm3s/lm3s_pendsv.c @@ -0,0 +1,118 @@ +/**************************************************************************** + * arch/arm/src/lm3s/lm3s_pendsv.c + * arch/arm/src/chip/lm3s_pendsv.c + * + * Copyright (C) 2009 Gregory Nutt. All rights reserved. + * Author: Gregory Nutt + * + * 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 NuttX nor the names of its contributors may be + * used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS 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 + * COPYRIGHT OWNER 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. + * + ****************************************************************************/ + +/**************************************************************************** + * Included Files + ****************************************************************************/ + +#include +#include + +#include +#include +#include + +#include + +#include "os_internal.h" +#include "lm3s_internal.h" + +/**************************************************************************** + * Private Definitions + ****************************************************************************/ + +/**************************************************************************** + * Private Data + ****************************************************************************/ + +/**************************************************************************** + * Public Data + ****************************************************************************/ + +/**************************************************************************** + * Private Functions + ****************************************************************************/ + +/**************************************************************************** + * Public Functions + ****************************************************************************/ + +/**************************************************************************** + * Name: lm3xs_pendxv + * + * Description: + * This is PendSV exception handler that performs context switching + * + ****************************************************************************/ + +int lm3s_pendsv(int irq, FAR void *context) +{ + uint32 *svregs = (uint32*)context; + uint32 *tcbregs = (uint32*)svregs[REG_R1]; + + DEBUGASSERT(svregs && svregs == current_regs && tcbregs); + + /* The PendSV software interrupt is called with R0 = PendSV command and R1 = + * the TCB register save area. + */ + + switch (svregs[REG_R0]) + { + /* R0=0: This is a save context command. In this case, we simply need + * to copy the svregs to the tdbregs and return. + */ + + case 0: + memcpy(tcbregs, svregs, XCPTCONTEXT_SIZE); + break; + + /* R1=1: This a restore context command. In this case, we simply need to + * set current_regs to tcbrgs. svregs == current_regs is the normal exception + * turn. By setting current_regs = tcbregs, we force the return through + * the saved context. + */ + + case 1: + current_regs = tcbregs; + break; + + default: + PANIC(OSERR_INTERNAL); + break; + } + + return OK; +} diff --git a/arch/arm/src/lm3s/lm3s_vectors.S b/arch/arm/src/lm3s/lm3s_vectors.S index c17a387ae6..2873f72e17 100644 --- a/arch/arm/src/lm3s/lm3s_vectors.S +++ b/arch/arm/src/lm3s/lm3s_vectors.S @@ -1,5 +1,6 @@ /************************************************************************************ - * arch/arm/src/lm3s/lm3slm3s_vectors.S + * arch/arm/src/lm3s/lm3s_vectors.S + * arch/arm/src/chip/lm3s_vectors.S * * Copyright (C) 2009 Gregory Nutt. All rights reserved. * Author: Gregory Nutt