Dependencies:   mbed

Committer:
mbed714
Date:
Tue Sep 21 19:48:05 2010 +0000
Revision:
0:331db0b44b67

        

Who changed what in which revision?

UserRevisionLine numberNew contents of line
mbed714 0:331db0b44b67 1 ; ******************************************************************************
mbed714 0:331db0b44b67 2 ; *
mbed714 0:331db0b44b67 3 ; * FULLNAME: Single-Chip Microcontroller Real-Time Operating System
mbed714 0:331db0b44b67 4 ; *
mbed714 0:331db0b44b67 5 ; * NICKNAME: scmRTOS
mbed714 0:331db0b44b67 6 ; *
mbed714 0:331db0b44b67 7 ; * PROCESSOR: ARM Cortex-M3
mbed714 0:331db0b44b67 8 ; *
mbed714 0:331db0b44b67 9 ; * TOOLKIT: EWARM (IAR Systems)
mbed714 0:331db0b44b67 10 ; *
mbed714 0:331db0b44b67 11 ; * PURPOSE: Target Dependent Low-Level Stuff
mbed714 0:331db0b44b67 12 ; *
mbed714 0:331db0b44b67 13 ; * Version: 3.10
mbed714 0:331db0b44b67 14 ; *
mbed714 0:331db0b44b67 15 ; * $Revision: 195 $
mbed714 0:331db0b44b67 16 ; * $Date:: 2008-06-19 #$
mbed714 0:331db0b44b67 17 ; *
mbed714 0:331db0b44b67 18 ; * Copyright (c) 2003-2010, Harry E. Zhurov
mbed714 0:331db0b44b67 19 ; *
mbed714 0:331db0b44b67 20 ; * Permission is hereby granted, free of charge, to any person
mbed714 0:331db0b44b67 21 ; * obtaining a copy of this software and associated documentation
mbed714 0:331db0b44b67 22 ; * files (the "Software"), to deal in the Software without restriction,
mbed714 0:331db0b44b67 23 ; * including without limitation the rights to use, copy, modify, merge,
mbed714 0:331db0b44b67 24 ; * publish, distribute, sublicense, and/or sell copies of the Software,
mbed714 0:331db0b44b67 25 ; * and to permit persons to whom the Software is furnished to do so,
mbed714 0:331db0b44b67 26 ; * subject to the following conditions:
mbed714 0:331db0b44b67 27 ; *
mbed714 0:331db0b44b67 28 ; * The above copyright notice and this permission notice shall be included
mbed714 0:331db0b44b67 29 ; * in all copies or substantial portions of the Software.
mbed714 0:331db0b44b67 30 ; *
mbed714 0:331db0b44b67 31 ; * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
mbed714 0:331db0b44b67 32 ; * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
mbed714 0:331db0b44b67 33 ; * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
mbed714 0:331db0b44b67 34 ; * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
mbed714 0:331db0b44b67 35 ; * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
mbed714 0:331db0b44b67 36 ; * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH
mbed714 0:331db0b44b67 37 ; * THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
mbed714 0:331db0b44b67 38 ; *
mbed714 0:331db0b44b67 39 ; * =================================================================
mbed714 0:331db0b44b67 40 ; * See http://scmrtos.sourceforge.net for documentation, latest
mbed714 0:331db0b44b67 41 ; * information, license and contact details.
mbed714 0:331db0b44b67 42 ; * =================================================================
mbed714 0:331db0b44b67 43 ; *
mbed714 0:331db0b44b67 44 ; ******************************************************************************
mbed714 0:331db0b44b67 45 ; * Ported by Andrey Chuikin, Copyright (c) 2008-2010
mbed714 0:331db0b44b67 46
mbed714 0:331db0b44b67 47 ; #include "scmRTOS_TARGET_CFG.h"
mbed714 0:331db0b44b67 48
mbed714 0:331db0b44b67 49 ; -----------------------------------------------------------------------------
mbed714 0:331db0b44b67 50 ; PUBLIC FUNCTIONS
mbed714 0:331db0b44b67 51 ;
mbed714 0:331db0b44b67 52 EXTERN OS_ContextSwitchHook
mbed714 0:331db0b44b67 53
mbed714 0:331db0b44b67 54 EXPORT OS_Start
mbed714 0:331db0b44b67 55 EXPORT PendSV_Handler
mbed714 0:331db0b44b67 56
mbed714 0:331db0b44b67 57 ; -----------------------------------------------------------------------------
mbed714 0:331db0b44b67 58 ; EQUATES
mbed714 0:331db0b44b67 59 ;
mbed714 0:331db0b44b67 60 NVIC_INT_CTRL EQU 0xE000ED04 ; Interrupt control state register.
mbed714 0:331db0b44b67 61 NVIC_PENDSVSET EQU 0x10000000 ; Value to trigger PendSV exception.
mbed714 0:331db0b44b67 62
mbed714 0:331db0b44b67 63 NVIC_SYSPRI14 EQU 0xE000ED22 ; System priority register (priority 14).
mbed714 0:331db0b44b67 64 NVIC_PENDSV_PRI EQU 0xFF ; PendSV priority value (lowest).
mbed714 0:331db0b44b67 65 NVIC_SYSPRI15 EQU 0xE000ED23 ; System priority register (priority 15).
mbed714 0:331db0b44b67 66 NVIC_ST_PRI EQU 0xFF ; SysTick priority value (lowest).
mbed714 0:331db0b44b67 67
mbed714 0:331db0b44b67 68 NVIC_ST_CTRL EQU 0xE000E010 ; SysTick Ctrl & Status Reg.
mbed714 0:331db0b44b67 69 NVIC_ST_RELOAD EQU 0xE000E014 ; SysTick Reload Value Reg.
mbed714 0:331db0b44b67 70 NVIC_ST_CTRL_CLK_SRC EQU 0x00000004 ; Clock Source.
mbed714 0:331db0b44b67 71 NVIC_ST_CTRL_INTEN EQU 0x00000002 ; Interrupt enable.
mbed714 0:331db0b44b67 72 NVIC_ST_CTRL_ENABLE EQU 0x00000001 ; Counter mode.
mbed714 0:331db0b44b67 73
mbed714 0:331db0b44b67 74 ; should be the same as in scmRTOS_TARGET_CFG.h
mbed714 0:331db0b44b67 75 SYSTICKFREQ EQU 100000000
mbed714 0:331db0b44b67 76 SYSTICKINTRATE EQU 1000
mbed714 0:331db0b44b67 77
mbed714 0:331db0b44b67 78 ; -----------------------------------------------------------------------------
mbed714 0:331db0b44b67 79 ; CODE GENERATION DIRECTIVES
mbed714 0:331db0b44b67 80 ;
mbed714 0:331db0b44b67 81 AREA TEXT, CODE, READONLY
mbed714 0:331db0b44b67 82 THUMB
mbed714 0:331db0b44b67 83
mbed714 0:331db0b44b67 84 ; -----------------------------------------------------------------------------
mbed714 0:331db0b44b67 85 ; HANDLE PendSV EXCEPTION
mbed714 0:331db0b44b67 86 ; void PendSV_Handler(void)
mbed714 0:331db0b44b67 87 ;
mbed714 0:331db0b44b67 88 ; Note(s) : 1) PendSV is used to cause a context switch. This is a recommended method for performing
mbed714 0:331db0b44b67 89 ; context switches with Cortex-M3. This is because the Cortex-M3 auto-saves half of the
mbed714 0:331db0b44b67 90 ; processor context on any exception, and restores same on return from exception. So only
mbed714 0:331db0b44b67 91 ; saving of R4-R11 is required and fixing up the stack pointers. Using the PendSV exception
mbed714 0:331db0b44b67 92 ; this way means that context saving and restoring is identical whether it is initiated from
mbed714 0:331db0b44b67 93 ; a thread or occurs due to an interrupt or exception.
mbed714 0:331db0b44b67 94 ;
mbed714 0:331db0b44b67 95 ; 2) Pseudo-code is:
mbed714 0:331db0b44b67 96 ; a) Get the process SP, if 0 then skip (goto f) the saving part (first context switch);
mbed714 0:331db0b44b67 97 ; b) Save remaining regs r4-r11 on process stack;
mbed714 0:331db0b44b67 98 ; c) Call OS_ContextSwitchHook for save current task SP and get new task SP;
mbed714 0:331db0b44b67 99 ; d) Restore R4-R11 from new process stack;
mbed714 0:331db0b44b67 100 ; e) Perform exception return which will restore remaining context.
mbed714 0:331db0b44b67 101 ; f) Get SP for the first context switch (R2 hold it);
mbed714 0:331db0b44b67 102 ; run SysTick; goto d);
mbed714 0:331db0b44b67 103 ;
mbed714 0:331db0b44b67 104 ; 3) On entry into PendSV handler:
mbed714 0:331db0b44b67 105 ; a) The following have been saved on the process stack (by processor):
mbed714 0:331db0b44b67 106 ; xPSR, PC, LR, R12, R0-R3
mbed714 0:331db0b44b67 107 ; b) Processor mode is switched to Handler mode (from Thread mode)
mbed714 0:331db0b44b67 108 ; c) Stack is Main stack (switched from Process stack)
mbed714 0:331db0b44b67 109 ;
mbed714 0:331db0b44b67 110 ; 4) Since PendSV is set to lowest priority in the system (by OS_Start() below), we
mbed714 0:331db0b44b67 111 ; know that it will only be run when no other exception or interrupt is active, and
mbed714 0:331db0b44b67 112 ; therefore safe to assume that context being switched out was using the process stack (PSP).
mbed714 0:331db0b44b67 113 ;
mbed714 0:331db0b44b67 114 PendSV_Handler
mbed714 0:331db0b44b67 115 CPSID I ; Prevent interruption during context switch
mbed714 0:331db0b44b67 116 MRS R0, PSP ; PSP is process stack pointer
mbed714 0:331db0b44b67 117 CBZ R0, nosave ; Skip register save the first time
mbed714 0:331db0b44b67 118
mbed714 0:331db0b44b67 119 STMDB R0!, {R4-R11} ; Save remaining regs r4-11 on process stack
mbed714 0:331db0b44b67 120 ; At this point, entire context of process has been saved
mbed714 0:331db0b44b67 121
mbed714 0:331db0b44b67 122 PUSH {R14} ; Save LR exc_return value
mbed714 0:331db0b44b67 123 LDR R1, =OS_ContextSwitchHook ; OS_ContextSwitchHook();
mbed714 0:331db0b44b67 124 BLX R1
mbed714 0:331db0b44b67 125 POP {R14}
mbed714 0:331db0b44b67 126
mbed714 0:331db0b44b67 127 ContextRestore
mbed714 0:331db0b44b67 128 ; R0 is new process SP;
mbed714 0:331db0b44b67 129 LDMIA R0!, {R4-R11} ; Restore r4-11 from new process stack
mbed714 0:331db0b44b67 130 MSR PSP, R0 ; Load PSP with new process SP
mbed714 0:331db0b44b67 131 ORR LR, LR, #0x04 ; Ensure exception return uses process stack
mbed714 0:331db0b44b67 132 CPSIE I
mbed714 0:331db0b44b67 133 BX LR ; Exception return will restore remaining context
mbed714 0:331db0b44b67 134 nosave
mbed714 0:331db0b44b67 135 MOV R0, R2 ; R2 hold the first task SP
mbed714 0:331db0b44b67 136
mbed714 0:331db0b44b67 137 LDR R1, =NVIC_ST_CTRL ; Enable and run SysTick
mbed714 0:331db0b44b67 138 LDR R2, =(NVIC_ST_CTRL_CLK_SRC :OR: NVIC_ST_CTRL_INTEN :OR: NVIC_ST_CTRL_ENABLE)
mbed714 0:331db0b44b67 139 STR R2, [R1]
mbed714 0:331db0b44b67 140
mbed714 0:331db0b44b67 141 B ContextRestore
mbed714 0:331db0b44b67 142
mbed714 0:331db0b44b67 143
mbed714 0:331db0b44b67 144 ; -----------------------------------------------------------------------------
mbed714 0:331db0b44b67 145 ; START MULTITASKING
mbed714 0:331db0b44b67 146 ; void OS_Start(TStackItem* sp)
mbed714 0:331db0b44b67 147 ;
mbed714 0:331db0b44b67 148 ; Note(s) : 1) OS_Start() MUST:
mbed714 0:331db0b44b67 149 ; a) Setup PendSV and SysTick exception priority to lowest;
mbed714 0:331db0b44b67 150 ; b) Setup SysTick (reload value);
mbed714 0:331db0b44b67 151 ; c) Enable interrupts (tasks will run with interrupts enabled).
mbed714 0:331db0b44b67 152 ;
mbed714 0:331db0b44b67 153 OS_Start
mbed714 0:331db0b44b67 154 LDR R1, =NVIC_SYSPRI14 ; Set the PendSV exception priority (lowest)
mbed714 0:331db0b44b67 155 LDR R2, =NVIC_PENDSV_PRI
mbed714 0:331db0b44b67 156 STRB R2, [R1]
mbed714 0:331db0b44b67 157 LDR R1, =NVIC_SYSPRI15 ; Set the SysTick exception priority (lowest)
mbed714 0:331db0b44b67 158 LDR R2, =NVIC_ST_PRI
mbed714 0:331db0b44b67 159 STRB R2, [R1]
mbed714 0:331db0b44b67 160
mbed714 0:331db0b44b67 161 LDR R1, =NVIC_ST_RELOAD ; Setup SysTick
mbed714 0:331db0b44b67 162 LDR R2, =(SYSTICKFREQ/SYSTICKINTRATE-1)
mbed714 0:331db0b44b67 163 STR R2, [R1]
mbed714 0:331db0b44b67 164
mbed714 0:331db0b44b67 165 MOV R2, R0 ; Save the first task stack
mbed714 0:331db0b44b67 166 MOVS R0, #0 ; Set the PSP to 0 for initial context switch call
mbed714 0:331db0b44b67 167 MSR PSP, R0
mbed714 0:331db0b44b67 168
mbed714 0:331db0b44b67 169 LDR R0, =NVIC_INT_CTRL ; Trigger the PendSV exception (causes context switch)
mbed714 0:331db0b44b67 170 LDR R1, =NVIC_PENDSVSET
mbed714 0:331db0b44b67 171 STR R1, [R0]
mbed714 0:331db0b44b67 172
mbed714 0:331db0b44b67 173 CPSIE I ; Enable interrupts at processor level
mbed714 0:331db0b44b67 174 loop
mbed714 0:331db0b44b67 175 B loop ; Should never get here
mbed714 0:331db0b44b67 176
mbed714 0:331db0b44b67 177
mbed714 0:331db0b44b67 178 END