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