1 |
572 |
jeremybenn |
/*
|
2 |
|
|
FreeRTOS V6.1.1 - Copyright (C) 2011 Real Time Engineers Ltd.
|
3 |
|
|
|
4 |
|
|
***************************************************************************
|
5 |
|
|
* *
|
6 |
|
|
* If you are: *
|
7 |
|
|
* *
|
8 |
|
|
* + New to FreeRTOS, *
|
9 |
|
|
* + Wanting to learn FreeRTOS or multitasking in general quickly *
|
10 |
|
|
* + Looking for basic training, *
|
11 |
|
|
* + Wanting to improve your FreeRTOS skills and productivity *
|
12 |
|
|
* *
|
13 |
|
|
* then take a look at the FreeRTOS books - available as PDF or paperback *
|
14 |
|
|
* *
|
15 |
|
|
* "Using the FreeRTOS Real Time Kernel - a Practical Guide" *
|
16 |
|
|
* http://www.FreeRTOS.org/Documentation *
|
17 |
|
|
* *
|
18 |
|
|
* A pdf reference manual is also available. Both are usually delivered *
|
19 |
|
|
* to your inbox within 20 minutes to two hours when purchased between 8am *
|
20 |
|
|
* and 8pm GMT (although please allow up to 24 hours in case of *
|
21 |
|
|
* exceptional circumstances). Thank you for your support! *
|
22 |
|
|
* *
|
23 |
|
|
***************************************************************************
|
24 |
|
|
|
25 |
|
|
This file is part of the FreeRTOS distribution.
|
26 |
|
|
|
27 |
|
|
FreeRTOS is free software; you can redistribute it and/or modify it under
|
28 |
|
|
the terms of the GNU General Public License (version 2) as published by the
|
29 |
|
|
Free Software Foundation AND MODIFIED BY the FreeRTOS exception.
|
30 |
|
|
***NOTE*** The exception to the GPL is included to allow you to distribute
|
31 |
|
|
a combined work that includes FreeRTOS without being obliged to provide the
|
32 |
|
|
source code for proprietary components outside of the FreeRTOS kernel.
|
33 |
|
|
FreeRTOS is distributed in the hope that it will be useful, but WITHOUT
|
34 |
|
|
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
35 |
|
|
FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
|
36 |
|
|
more details. You should have received a copy of the GNU General Public
|
37 |
|
|
License and the FreeRTOS license exception along with FreeRTOS; if not it
|
38 |
|
|
can be viewed here: http://www.freertos.org/a00114.html and also obtained
|
39 |
|
|
by writing to Richard Barry, contact details for whom are available on the
|
40 |
|
|
FreeRTOS WEB site.
|
41 |
|
|
|
42 |
|
|
1 tab == 4 spaces!
|
43 |
|
|
|
44 |
|
|
http://www.FreeRTOS.org - Documentation, latest information, license and
|
45 |
|
|
contact details.
|
46 |
|
|
|
47 |
|
|
http://www.SafeRTOS.com - A version that is certified for use in safety
|
48 |
|
|
critical systems.
|
49 |
|
|
|
50 |
|
|
http://www.OpenRTOS.com - Commercial support, development, porting,
|
51 |
|
|
licensing and training services.
|
52 |
|
|
*/
|
53 |
|
|
|
54 |
|
|
/*-----------------------------------------------------------
|
55 |
|
|
* Implementation of functions defined in portable.h for the Philips ARM7 port.
|
56 |
|
|
*----------------------------------------------------------*/
|
57 |
|
|
|
58 |
|
|
/*
|
59 |
|
|
Changes from V3.2.2
|
60 |
|
|
|
61 |
|
|
+ Bug fix - The prescale value for the timer setup is now written to T0PR
|
62 |
|
|
instead of T0PC. This bug would have had no effect unless a prescale
|
63 |
|
|
value was actually used.
|
64 |
|
|
*/
|
65 |
|
|
|
66 |
|
|
/* Standard includes. */
|
67 |
|
|
#include <stdlib.h>
|
68 |
|
|
#include <intrinsics.h>
|
69 |
|
|
|
70 |
|
|
/* Scheduler includes. */
|
71 |
|
|
#include "FreeRTOS.h"
|
72 |
|
|
#include "task.h"
|
73 |
|
|
|
74 |
|
|
/* Constants required to setup the tick ISR. */
|
75 |
|
|
#define portENABLE_TIMER ( ( unsigned char ) 0x01 )
|
76 |
|
|
#define portPRESCALE_VALUE 0x00
|
77 |
|
|
#define portINTERRUPT_ON_MATCH ( ( unsigned long ) 0x01 )
|
78 |
|
|
#define portRESET_COUNT_ON_MATCH ( ( unsigned long ) 0x02 )
|
79 |
|
|
|
80 |
|
|
/* Constants required to setup the initial stack. */
|
81 |
|
|
#define portINITIAL_SPSR ( ( portSTACK_TYPE ) 0x1f ) /* System mode, ARM mode, interrupts enabled. */
|
82 |
|
|
#define portTHUMB_MODE_BIT ( ( portSTACK_TYPE ) 0x20 )
|
83 |
|
|
#define portINSTRUCTION_SIZE ( ( portSTACK_TYPE ) 4 )
|
84 |
|
|
|
85 |
|
|
/* Constants required to setup the PIT. */
|
86 |
|
|
#define portPIT_CLOCK_DIVISOR ( ( unsigned long ) 16 )
|
87 |
|
|
#define portPIT_COUNTER_VALUE ( ( ( configCPU_CLOCK_HZ / portPIT_CLOCK_DIVISOR ) / 1000UL ) * portTICK_RATE_MS )
|
88 |
|
|
|
89 |
|
|
/* Constants required to handle interrupts. */
|
90 |
|
|
#define portTIMER_MATCH_ISR_BIT ( ( unsigned char ) 0x01 )
|
91 |
|
|
#define portCLEAR_VIC_INTERRUPT ( ( unsigned long ) 0 )
|
92 |
|
|
|
93 |
|
|
/* Constants required to handle critical sections. */
|
94 |
|
|
#define portNO_CRITICAL_NESTING ( ( unsigned long ) 0 )
|
95 |
|
|
|
96 |
|
|
|
97 |
|
|
#define portINT_LEVEL_SENSITIVE 0
|
98 |
|
|
#define portPIT_ENABLE ( ( unsigned short ) 0x1 << 24 )
|
99 |
|
|
#define portPIT_INT_ENABLE ( ( unsigned short ) 0x1 << 25 )
|
100 |
|
|
|
101 |
|
|
/* Constants required to setup the VIC for the tick ISR. */
|
102 |
|
|
#define portTIMER_VIC_CHANNEL ( ( unsigned long ) 0x0004 )
|
103 |
|
|
#define portTIMER_VIC_CHANNEL_BIT ( ( unsigned long ) 0x0010 )
|
104 |
|
|
#define portTIMER_VIC_ENABLE ( ( unsigned long ) 0x0020 )
|
105 |
|
|
|
106 |
|
|
/*-----------------------------------------------------------*/
|
107 |
|
|
|
108 |
|
|
/* Setup the PIT to generate the tick interrupts. */
|
109 |
|
|
static void prvSetupTimerInterrupt( void );
|
110 |
|
|
|
111 |
|
|
/* ulCriticalNesting will get set to zero when the first task starts. It
|
112 |
|
|
cannot be initialised to 0 as this will cause interrupts to be enabled
|
113 |
|
|
during the kernel initialisation process. */
|
114 |
|
|
unsigned long ulCriticalNesting = ( unsigned long ) 9999;
|
115 |
|
|
|
116 |
|
|
/*-----------------------------------------------------------*/
|
117 |
|
|
|
118 |
|
|
/*
|
119 |
|
|
* Initialise the stack of a task to look exactly as if a call to
|
120 |
|
|
* portSAVE_CONTEXT had been called.
|
121 |
|
|
*
|
122 |
|
|
* See header file for description.
|
123 |
|
|
*/
|
124 |
|
|
portSTACK_TYPE *pxPortInitialiseStack( portSTACK_TYPE *pxTopOfStack, pdTASK_CODE pxCode, void *pvParameters )
|
125 |
|
|
{
|
126 |
|
|
portSTACK_TYPE *pxOriginalTOS;
|
127 |
|
|
|
128 |
|
|
pxOriginalTOS = pxTopOfStack;
|
129 |
|
|
|
130 |
|
|
/* Setup the initial stack of the task. The stack is set exactly as
|
131 |
|
|
expected by the portRESTORE_CONTEXT() macro. */
|
132 |
|
|
|
133 |
|
|
/* First on the stack is the return address - which in this case is the
|
134 |
|
|
start of the task. The offset is added to make the return address appear
|
135 |
|
|
as it would within an IRQ ISR. */
|
136 |
|
|
*pxTopOfStack = ( portSTACK_TYPE ) pxCode + portINSTRUCTION_SIZE;
|
137 |
|
|
pxTopOfStack--;
|
138 |
|
|
|
139 |
|
|
*pxTopOfStack = ( portSTACK_TYPE ) 0xaaaaaaaa; /* R14 */
|
140 |
|
|
pxTopOfStack--;
|
141 |
|
|
*pxTopOfStack = ( portSTACK_TYPE ) pxOriginalTOS; /* Stack used when task starts goes in R13. */
|
142 |
|
|
pxTopOfStack--;
|
143 |
|
|
*pxTopOfStack = ( portSTACK_TYPE ) 0x12121212; /* R12 */
|
144 |
|
|
pxTopOfStack--;
|
145 |
|
|
*pxTopOfStack = ( portSTACK_TYPE ) 0x11111111; /* R11 */
|
146 |
|
|
pxTopOfStack--;
|
147 |
|
|
*pxTopOfStack = ( portSTACK_TYPE ) 0x10101010; /* R10 */
|
148 |
|
|
pxTopOfStack--;
|
149 |
|
|
*pxTopOfStack = ( portSTACK_TYPE ) 0x09090909; /* R9 */
|
150 |
|
|
pxTopOfStack--;
|
151 |
|
|
*pxTopOfStack = ( portSTACK_TYPE ) 0x08080808; /* R8 */
|
152 |
|
|
pxTopOfStack--;
|
153 |
|
|
*pxTopOfStack = ( portSTACK_TYPE ) 0x07070707; /* R7 */
|
154 |
|
|
pxTopOfStack--;
|
155 |
|
|
*pxTopOfStack = ( portSTACK_TYPE ) 0x06060606; /* R6 */
|
156 |
|
|
pxTopOfStack--;
|
157 |
|
|
*pxTopOfStack = ( portSTACK_TYPE ) 0x05050505; /* R5 */
|
158 |
|
|
pxTopOfStack--;
|
159 |
|
|
*pxTopOfStack = ( portSTACK_TYPE ) 0x04040404; /* R4 */
|
160 |
|
|
pxTopOfStack--;
|
161 |
|
|
*pxTopOfStack = ( portSTACK_TYPE ) 0x03030303; /* R3 */
|
162 |
|
|
pxTopOfStack--;
|
163 |
|
|
*pxTopOfStack = ( portSTACK_TYPE ) 0x02020202; /* R2 */
|
164 |
|
|
pxTopOfStack--;
|
165 |
|
|
*pxTopOfStack = ( portSTACK_TYPE ) 0x01010101; /* R1 */
|
166 |
|
|
pxTopOfStack--;
|
167 |
|
|
|
168 |
|
|
/* When the task starts is will expect to find the function parameter in
|
169 |
|
|
R0. */
|
170 |
|
|
*pxTopOfStack = ( portSTACK_TYPE ) pvParameters; /* R0 */
|
171 |
|
|
pxTopOfStack--;
|
172 |
|
|
|
173 |
|
|
/* The status register is set for system mode, with interrupts enabled. */
|
174 |
|
|
*pxTopOfStack = ( portSTACK_TYPE ) portINITIAL_SPSR;
|
175 |
|
|
|
176 |
|
|
if( ( ( unsigned long ) pxCode & 0x01UL ) != 0x00UL )
|
177 |
|
|
{
|
178 |
|
|
/* We want the task to start in thumb mode. */
|
179 |
|
|
*pxTopOfStack |= portTHUMB_MODE_BIT;
|
180 |
|
|
}
|
181 |
|
|
|
182 |
|
|
pxTopOfStack--;
|
183 |
|
|
|
184 |
|
|
/* Interrupt flags cannot always be stored on the stack and will
|
185 |
|
|
instead be stored in a variable, which is then saved as part of the
|
186 |
|
|
tasks context. */
|
187 |
|
|
*pxTopOfStack = portNO_CRITICAL_NESTING;
|
188 |
|
|
|
189 |
|
|
return pxTopOfStack;
|
190 |
|
|
}
|
191 |
|
|
/*-----------------------------------------------------------*/
|
192 |
|
|
|
193 |
|
|
portBASE_TYPE xPortStartScheduler( void )
|
194 |
|
|
{
|
195 |
|
|
extern void vPortStartFirstTask( void );
|
196 |
|
|
|
197 |
|
|
/* Start the timer that generates the tick ISR. Interrupts are disabled
|
198 |
|
|
here already. */
|
199 |
|
|
prvSetupTimerInterrupt();
|
200 |
|
|
|
201 |
|
|
/* Start the first task. */
|
202 |
|
|
vPortStartFirstTask();
|
203 |
|
|
|
204 |
|
|
/* Should not get here! */
|
205 |
|
|
return 0;
|
206 |
|
|
}
|
207 |
|
|
/*-----------------------------------------------------------*/
|
208 |
|
|
|
209 |
|
|
void vPortEndScheduler( void )
|
210 |
|
|
{
|
211 |
|
|
/* It is unlikely that the ARM port will require this function as there
|
212 |
|
|
is nothing to return to. */
|
213 |
|
|
}
|
214 |
|
|
/*-----------------------------------------------------------*/
|
215 |
|
|
|
216 |
|
|
#if configUSE_PREEMPTION == 0
|
217 |
|
|
|
218 |
|
|
/* The cooperative scheduler requires a normal IRQ service routine to
|
219 |
|
|
simply increment the system tick. */
|
220 |
|
|
static __arm __irq void vPortNonPreemptiveTick( void );
|
221 |
|
|
static __arm __irq void vPortNonPreemptiveTick( void )
|
222 |
|
|
{
|
223 |
|
|
/* Increment the tick count - which may wake some tasks but as the
|
224 |
|
|
preemptive scheduler is not being used any woken task is not given
|
225 |
|
|
processor time no matter what its priority. */
|
226 |
|
|
vTaskIncrementTick();
|
227 |
|
|
|
228 |
|
|
/* Ready for the next interrupt. */
|
229 |
|
|
T0IR = portTIMER_MATCH_ISR_BIT;
|
230 |
|
|
VICVectAddr = portCLEAR_VIC_INTERRUPT;
|
231 |
|
|
}
|
232 |
|
|
|
233 |
|
|
#else
|
234 |
|
|
|
235 |
|
|
/* This function is called from an asm wrapper, so does not require the __irq
|
236 |
|
|
keyword. */
|
237 |
|
|
void vPortPreemptiveTick( void );
|
238 |
|
|
void vPortPreemptiveTick( void )
|
239 |
|
|
{
|
240 |
|
|
/* Increment the tick counter. */
|
241 |
|
|
vTaskIncrementTick();
|
242 |
|
|
|
243 |
|
|
/* The new tick value might unblock a task. Ensure the highest task that
|
244 |
|
|
is ready to execute is the task that will execute when the tick ISR
|
245 |
|
|
exits. */
|
246 |
|
|
vTaskSwitchContext();
|
247 |
|
|
|
248 |
|
|
/* Ready for the next interrupt. */
|
249 |
|
|
T0IR = portTIMER_MATCH_ISR_BIT;
|
250 |
|
|
VICVectAddr = portCLEAR_VIC_INTERRUPT;
|
251 |
|
|
}
|
252 |
|
|
|
253 |
|
|
#endif
|
254 |
|
|
|
255 |
|
|
/*-----------------------------------------------------------*/
|
256 |
|
|
|
257 |
|
|
static void prvSetupTimerInterrupt( void )
|
258 |
|
|
{
|
259 |
|
|
unsigned long ulCompareMatch;
|
260 |
|
|
|
261 |
|
|
/* A 1ms tick does not require the use of the timer prescale. This is
|
262 |
|
|
defaulted to zero but can be used if necessary. */
|
263 |
|
|
T0PR = portPRESCALE_VALUE;
|
264 |
|
|
|
265 |
|
|
/* Calculate the match value required for our wanted tick rate. */
|
266 |
|
|
ulCompareMatch = configCPU_CLOCK_HZ / configTICK_RATE_HZ;
|
267 |
|
|
|
268 |
|
|
/* Protect against divide by zero. Using an if() statement still results
|
269 |
|
|
in a warning - hence the #if. */
|
270 |
|
|
#if portPRESCALE_VALUE != 0
|
271 |
|
|
{
|
272 |
|
|
ulCompareMatch /= ( portPRESCALE_VALUE + 1 );
|
273 |
|
|
}
|
274 |
|
|
#endif
|
275 |
|
|
|
276 |
|
|
T0MR0 = ulCompareMatch;
|
277 |
|
|
|
278 |
|
|
/* Generate tick with timer 0 compare match. */
|
279 |
|
|
T0MCR = portRESET_COUNT_ON_MATCH | portINTERRUPT_ON_MATCH;
|
280 |
|
|
|
281 |
|
|
/* Setup the VIC for the timer. */
|
282 |
|
|
VICIntSelect &= ~( portTIMER_VIC_CHANNEL_BIT );
|
283 |
|
|
VICIntEnable |= portTIMER_VIC_CHANNEL_BIT;
|
284 |
|
|
|
285 |
|
|
/* The ISR installed depends on whether the preemptive or cooperative
|
286 |
|
|
scheduler is being used. */
|
287 |
|
|
#if configUSE_PREEMPTION == 1
|
288 |
|
|
{
|
289 |
|
|
extern void ( vPortPreemptiveTickEntry )( void );
|
290 |
|
|
|
291 |
|
|
VICVectAddr0 = ( unsigned long ) vPortPreemptiveTickEntry;
|
292 |
|
|
}
|
293 |
|
|
#else
|
294 |
|
|
{
|
295 |
|
|
extern void ( vNonPreemptiveTick )( void );
|
296 |
|
|
|
297 |
|
|
VICVectAddr0 = ( long ) vPortNonPreemptiveTick;
|
298 |
|
|
}
|
299 |
|
|
#endif
|
300 |
|
|
|
301 |
|
|
VICVectCntl0 = portTIMER_VIC_CHANNEL | portTIMER_VIC_ENABLE;
|
302 |
|
|
|
303 |
|
|
/* Start the timer - interrupts are disabled when this function is called
|
304 |
|
|
so it is okay to do this here. */
|
305 |
|
|
T0TCR = portENABLE_TIMER;
|
306 |
|
|
}
|
307 |
|
|
/*-----------------------------------------------------------*/
|
308 |
|
|
|
309 |
|
|
void vPortEnterCritical( void )
|
310 |
|
|
{
|
311 |
|
|
/* Disable interrupts first! */
|
312 |
|
|
__disable_interrupt();
|
313 |
|
|
|
314 |
|
|
/* Now interrupts are disabled ulCriticalNesting can be accessed
|
315 |
|
|
directly. Increment ulCriticalNesting to keep a count of how many times
|
316 |
|
|
portENTER_CRITICAL() has been called. */
|
317 |
|
|
ulCriticalNesting++;
|
318 |
|
|
}
|
319 |
|
|
/*-----------------------------------------------------------*/
|
320 |
|
|
|
321 |
|
|
void vPortExitCritical( void )
|
322 |
|
|
{
|
323 |
|
|
if( ulCriticalNesting > portNO_CRITICAL_NESTING )
|
324 |
|
|
{
|
325 |
|
|
/* Decrement the nesting count as we are leaving a critical section. */
|
326 |
|
|
ulCriticalNesting--;
|
327 |
|
|
|
328 |
|
|
/* If the nesting level has reached zero then interrupts should be
|
329 |
|
|
re-enabled. */
|
330 |
|
|
if( ulCriticalNesting == portNO_CRITICAL_NESTING )
|
331 |
|
|
{
|
332 |
|
|
__enable_interrupt();
|
333 |
|
|
}
|
334 |
|
|
}
|
335 |
|
|
}
|
336 |
|
|
/*-----------------------------------------------------------*/
|
337 |
|
|
|
338 |
|
|
|
339 |
|
|
|
340 |
|
|
|
341 |
|
|
|
342 |
|
|
|