1 |
/* |
---|
2 |
FreeRTOS V5.4.1 - Copyright (C) 2009 Real Time Engineers Ltd. |
---|
3 |
|
---|
4 |
This file is part of the FreeRTOS distribution. |
---|
5 |
|
---|
6 |
FreeRTOS is free software; you can redistribute it and/or modify it under |
---|
7 |
the terms of the GNU General Public License (version 2) as published by the |
---|
8 |
Free Software Foundation and modified by the FreeRTOS exception. |
---|
9 |
**NOTE** The exception to the GPL is included to allow you to distribute a |
---|
10 |
combined work that includes FreeRTOS without being obliged to provide the |
---|
11 |
source code for proprietary components outside of the FreeRTOS kernel. |
---|
12 |
Alternative commercial license and support terms are also available upon |
---|
13 |
request. See the licensing section of http://www.FreeRTOS.org for full |
---|
14 |
license details. |
---|
15 |
|
---|
16 |
FreeRTOS is distributed in the hope that it will be useful, but WITHOUT |
---|
17 |
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
---|
18 |
FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for |
---|
19 |
more details. |
---|
20 |
|
---|
21 |
You should have received a copy of the GNU General Public License along |
---|
22 |
with FreeRTOS; if not, write to the Free Software Foundation, Inc., 59 |
---|
23 |
Temple Place, Suite 330, Boston, MA 02111-1307 USA. |
---|
24 |
|
---|
25 |
|
---|
26 |
*************************************************************************** |
---|
27 |
* * |
---|
28 |
* Looking for a quick start? Then check out the FreeRTOS eBook! * |
---|
29 |
* See http://www.FreeRTOS.org/Documentation for details * |
---|
30 |
* * |
---|
31 |
*************************************************************************** |
---|
32 |
|
---|
33 |
1 tab == 4 spaces! |
---|
34 |
|
---|
35 |
Please ensure to read the configuration and relevant port sections of the |
---|
36 |
online documentation. |
---|
37 |
|
---|
38 |
http://www.FreeRTOS.org - Documentation, latest information, license and |
---|
39 |
contact details. |
---|
40 |
|
---|
41 |
http://www.SafeRTOS.com - A version that is certified for use in safety |
---|
42 |
critical systems. |
---|
43 |
|
---|
44 |
http://www.OpenRTOS.com - Commercial support, development, porting, |
---|
45 |
licensing and training services. |
---|
46 |
*/ |
---|
47 |
|
---|
48 |
|
---|
49 |
/*----------------------------------------------------------- |
---|
50 |
* Implementation of functions defined in portable.h for the ARM7 port. |
---|
51 |
* |
---|
52 |
* Components that can be compiled to either ARM or THUMB mode are |
---|
53 |
* contained in this file. The ISR routines, which can only be compiled |
---|
54 |
* to ARM mode are contained in portISR.c. |
---|
55 |
*----------------------------------------------------------*/ |
---|
56 |
|
---|
57 |
|
---|
58 |
/* Standard includes. */ |
---|
59 |
#include <stdlib.h> |
---|
60 |
|
---|
61 |
/* Scheduler includes. */ |
---|
62 |
#include "FreeRTOS.h" |
---|
63 |
#include "task.h" |
---|
64 |
|
---|
65 |
/* Constants required to setup the task context. */ |
---|
66 |
#define portINITIAL_SPSR ( ( portSTACK_TYPE ) 0x1f ) /* System mode, ARM mode, interrupts enabled. */ |
---|
67 |
#define portTHUMB_MODE_BIT ( ( portSTACK_TYPE ) 0x20 ) |
---|
68 |
#define portINSTRUCTION_SIZE ( ( portSTACK_TYPE ) 4 ) |
---|
69 |
#define portNO_CRITICAL_SECTION_NESTING ( ( portSTACK_TYPE ) 0 ) |
---|
70 |
|
---|
71 |
/* Constants required to setup the tick ISR. */ |
---|
72 |
#define portENABLE_TIMER ( ( unsigned portCHAR ) 0x01 ) |
---|
73 |
#define portPRESCALE_VALUE 0x00 |
---|
74 |
#define portINTERRUPT_ON_MATCH ( ( unsigned portLONG ) 0x01 ) |
---|
75 |
#define portRESET_COUNT_ON_MATCH ( ( unsigned portLONG ) 0x02 ) |
---|
76 |
|
---|
77 |
/* Constants required to setup the VIC for the tick ISR. */ |
---|
78 |
#define portTIMER_VIC_CHANNEL ( ( unsigned portLONG ) 0x0004 ) |
---|
79 |
#define portTIMER_VIC_CHANNEL_BIT ( ( unsigned portLONG ) 0x0010 ) |
---|
80 |
#define portTIMER_VIC_ENABLE ( ( unsigned portLONG ) 0x0020 ) |
---|
81 |
|
---|
82 |
/*-----------------------------------------------------------*/ |
---|
83 |
|
---|
84 |
/* Setup the timer to generate the tick interrupts. */ |
---|
85 |
static void prvSetupTimerInterrupt( void ); |
---|
86 |
|
---|
87 |
/* |
---|
88 |
* The scheduler can only be started from ARM mode, so |
---|
89 |
* vPortISRStartFirstSTask() is defined in portISR.c. |
---|
90 |
*/ |
---|
91 |
extern void vPortISRStartFirstTask( void ); |
---|
92 |
|
---|
93 |
/*-----------------------------------------------------------*/ |
---|
94 |
|
---|
95 |
/* |
---|
96 |
* Initialise the stack of a task to look exactly as if a call to |
---|
97 |
* portSAVE_CONTEXT had been called. |
---|
98 |
* |
---|
99 |
* See header file for description. |
---|
100 |
*/ |
---|
101 |
portSTACK_TYPE *pxPortInitialiseStack( portSTACK_TYPE *pxTopOfStack, pdTASK_CODE pxCode, void *pvParameters ) |
---|
102 |
{ |
---|
103 |
portSTACK_TYPE *pxOriginalTOS; |
---|
104 |
|
---|
105 |
pxOriginalTOS = pxTopOfStack; |
---|
106 |
|
---|
107 |
/* Setup the initial stack of the task. The stack is set exactly as |
---|
108 |
expected by the portRESTORE_CONTEXT() macro. */ |
---|
109 |
|
---|
110 |
/* First on the stack is the return address - which in this case is the |
---|
111 |
start of the task. The offset is added to make the return address appear |
---|
112 |
as it would within an IRQ ISR. */ |
---|
113 |
*pxTopOfStack = ( portSTACK_TYPE ) pxCode + portINSTRUCTION_SIZE; |
---|
114 |
pxTopOfStack--; |
---|
115 |
|
---|
116 |
*pxTopOfStack = ( portSTACK_TYPE ) 0x00000000; /* R14 */ |
---|
117 |
pxTopOfStack--; |
---|
118 |
*pxTopOfStack = ( portSTACK_TYPE ) pxOriginalTOS; /* Stack used when task starts goes in R13. */ |
---|
119 |
pxTopOfStack--; |
---|
120 |
*pxTopOfStack = ( portSTACK_TYPE ) 0x12121212; /* R12 */ |
---|
121 |
pxTopOfStack--; |
---|
122 |
*pxTopOfStack = ( portSTACK_TYPE ) 0x11111111; /* R11 */ |
---|
123 |
pxTopOfStack--; |
---|
124 |
*pxTopOfStack = ( portSTACK_TYPE ) 0x10101010; /* R10 */ |
---|
125 |
pxTopOfStack--; |
---|
126 |
*pxTopOfStack = ( portSTACK_TYPE ) 0x09090909; /* R9 */ |
---|
127 |
pxTopOfStack--; |
---|
128 |
*pxTopOfStack = ( portSTACK_TYPE ) 0x08080808; /* R8 */ |
---|
129 |
pxTopOfStack--; |
---|
130 |
*pxTopOfStack = ( portSTACK_TYPE ) 0x07070707; /* R7 */ |
---|
131 |
pxTopOfStack--; |
---|
132 |
*pxTopOfStack = ( portSTACK_TYPE ) 0x06060606; /* R6 */ |
---|
133 |
pxTopOfStack--; |
---|
134 |
*pxTopOfStack = ( portSTACK_TYPE ) 0x05050505; /* R5 */ |
---|
135 |
pxTopOfStack--; |
---|
136 |
*pxTopOfStack = ( portSTACK_TYPE ) 0x04040404; /* R4 */ |
---|
137 |
pxTopOfStack--; |
---|
138 |
*pxTopOfStack = ( portSTACK_TYPE ) 0x03030303; /* R3 */ |
---|
139 |
pxTopOfStack--; |
---|
140 |
*pxTopOfStack = ( portSTACK_TYPE ) 0x02020202; /* R2 */ |
---|
141 |
pxTopOfStack--; |
---|
142 |
*pxTopOfStack = ( portSTACK_TYPE ) 0x01010101; /* R1 */ |
---|
143 |
pxTopOfStack--; |
---|
144 |
|
---|
145 |
/* When the task starts is will expect to find the function parameter in |
---|
146 |
R0. */ |
---|
147 |
*pxTopOfStack = ( portSTACK_TYPE ) pvParameters; /* R0 */ |
---|
148 |
pxTopOfStack--; |
---|
149 |
|
---|
150 |
/* The last thing onto the stack is the status register, which is set for |
---|
151 |
system mode, with interrupts enabled. */ |
---|
152 |
*pxTopOfStack = ( portSTACK_TYPE ) portINITIAL_SPSR; |
---|
153 |
|
---|
154 |
#ifdef THUMB_INTERWORK |
---|
155 |
{ |
---|
156 |
/* We want the task to start in thumb mode. */ |
---|
157 |
*pxTopOfStack |= portTHUMB_MODE_BIT; |
---|
158 |
} |
---|
159 |
#endif |
---|
160 |
|
---|
161 |
pxTopOfStack--; |
---|
162 |
|
---|
163 |
/* Some optimisation levels use the stack differently to others. This |
---|
164 |
means the interrupt flags cannot always be stored on the stack and will |
---|
165 |
instead be stored in a variable, which is then saved as part of the |
---|
166 |
tasks context. */ |
---|
167 |
*pxTopOfStack = portNO_CRITICAL_SECTION_NESTING; |
---|
168 |
|
---|
169 |
return pxTopOfStack; |
---|
170 |
} |
---|
171 |
/*-----------------------------------------------------------*/ |
---|
172 |
|
---|
173 |
portBASE_TYPE xPortStartScheduler( void ) |
---|
174 |
{ |
---|
175 |
/* Start the timer that generates the tick ISR. Interrupts are disabled |
---|
176 |
here already. */ |
---|
177 |
prvSetupTimerInterrupt(); |
---|
178 |
|
---|
179 |
/* Start the first task. */ |
---|
180 |
vPortISRStartFirstTask(); |
---|
181 |
|
---|
182 |
/* Should not get here! */ |
---|
183 |
return 0; |
---|
184 |
} |
---|
185 |
/*-----------------------------------------------------------*/ |
---|
186 |
|
---|
187 |
void vPortEndScheduler( void ) |
---|
188 |
{ |
---|
189 |
/* It is unlikely that the ARM port will require this function as there |
---|
190 |
is nothing to return to. */ |
---|
191 |
} |
---|
192 |
/*-----------------------------------------------------------*/ |
---|
193 |
|
---|
194 |
/* |
---|
195 |
* Setup the timer 0 to generate the tick interrupts at the required frequency. |
---|
196 |
*/ |
---|
197 |
static void prvSetupTimerInterrupt( void ) |
---|
198 |
{ |
---|
199 |
unsigned portLONG ulCompareMatch; |
---|
200 |
|
---|
201 |
PCLKSEL0 = (PCLKSEL0 & (~(0x3<<2))) | (0x01 << 2); |
---|
202 |
T0TCR = 2; /* Stop and reset the timer */ |
---|
203 |
T0CTCR = 0; /* Timer mode */ |
---|
204 |
|
---|
205 |
/* A 1ms tick does not require the use of the timer prescale. This is |
---|
206 |
defaulted to zero but can be used if necessary. */ |
---|
207 |
T0PR = portPRESCALE_VALUE; |
---|
208 |
|
---|
209 |
/* Calculate the match value required for our wanted tick rate. */ |
---|
210 |
ulCompareMatch = configCPU_CLOCK_HZ / configTICK_RATE_HZ; |
---|
211 |
|
---|
212 |
/* Protect against divide by zero. Using an if() statement still results |
---|
213 |
in a warning - hence the #if. */ |
---|
214 |
#if portPRESCALE_VALUE != 0 |
---|
215 |
{ |
---|
216 |
ulCompareMatch /= ( portPRESCALE_VALUE + 1 ); |
---|
217 |
} |
---|
218 |
#endif |
---|
219 |
T0MR1 = ulCompareMatch; |
---|
220 |
|
---|
221 |
/* Generate tick with timer 0 compare match. */ |
---|
222 |
T0MCR = (3 << 3); /* Reset timer on match and generate interrupt */ |
---|
223 |
|
---|
224 |
/* Setup the VIC for the timer. */ |
---|
225 |
VICIntEnable = 0x00000010; |
---|
226 |
|
---|
227 |
/* The ISR installed depends on whether the preemptive or cooperative |
---|
228 |
scheduler is being used. */ |
---|
229 |
#if configUSE_PREEMPTION == 1 |
---|
230 |
{ |
---|
231 |
extern void ( vPreemptiveTick )( void ); |
---|
232 |
VICVectAddr4 = ( portLONG ) vPreemptiveTick; |
---|
233 |
} |
---|
234 |
#else |
---|
235 |
{ |
---|
236 |
extern void ( vNonPreemptiveTick )( void ); |
---|
237 |
VICVectAddr4 = ( portLONG ) vNonPreemptiveTick; |
---|
238 |
} |
---|
239 |
#endif |
---|
240 |
|
---|
241 |
VICVectCntl4 = 1; |
---|
242 |
|
---|
243 |
/* Start the timer - interrupts are disabled when this function is called |
---|
244 |
so it is okay to do this here. */ |
---|
245 |
T0TCR = portENABLE_TIMER; |
---|
246 |
} |
---|
247 |
/*-----------------------------------------------------------*/ |
---|
248 |
|
---|
249 |
|
---|
250 |
|
---|