FreeRTOS
[armadillo_firmware.git] / FreeRTOS / Source / portable / GCC / ARM7_LPC2000 / port.c
blob0ce8f13f9ece2bf47a0d58a10c0dff1a4a431b78
1 /*
2 FreeRTOS V6.0.5 - Copyright (C) 2010 Real Time Engineers Ltd.
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 eBook *
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 ***************************************************************************
25 This file is part of the FreeRTOS distribution.
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.
42 1 tab == 4 spaces!
44 http://www.FreeRTOS.org - Documentation, latest information, license and
45 contact details.
47 http://www.SafeRTOS.com - A version that is certified for use in safety
48 critical systems.
50 http://www.OpenRTOS.com - Commercial support, development, porting,
51 licensing and training services.
55 /*-----------------------------------------------------------
56 * Implementation of functions defined in portable.h for the ARM7 port.
58 * Components that can be compiled to either ARM or THUMB mode are
59 * contained in this file. The ISR routines, which can only be compiled
60 * to ARM mode are contained in portISR.c.
61 *----------------------------------------------------------*/
64 /* Standard includes. */
65 #include <stdlib.h>
67 /* Scheduler includes. */
68 #include "FreeRTOS.h"
69 #include "task.h"
71 /* Constants required to setup the task context. */
72 #define portINITIAL_SPSR ( ( portSTACK_TYPE ) 0x1f ) /* System mode, ARM mode, interrupts enabled. */
73 #define portTHUMB_MODE_BIT ( ( portSTACK_TYPE ) 0x20 )
74 #define portINSTRUCTION_SIZE ( ( portSTACK_TYPE ) 4 )
75 #define portNO_CRITICAL_SECTION_NESTING ( ( portSTACK_TYPE ) 0 )
77 /* Constants required to setup the tick ISR. */
78 #define portENABLE_TIMER ( ( unsigned char ) 0x01 )
79 #define portPRESCALE_VALUE 0x00
80 #define portINTERRUPT_ON_MATCH ( ( unsigned long ) 0x01 )
81 #define portRESET_COUNT_ON_MATCH ( ( unsigned long ) 0x02 )
83 /* Constants required to setup the VIC for the tick ISR. */
84 #define portTIMER_VIC_CHANNEL ( ( unsigned long ) 0x0004 )
85 #define portTIMER_VIC_CHANNEL_BIT ( ( unsigned long ) 0x0010 )
86 #define portTIMER_VIC_ENABLE ( ( unsigned long ) 0x0020 )
88 /*-----------------------------------------------------------*/
90 /* Setup the timer to generate the tick interrupts. */
91 static void prvSetupTimerInterrupt( void );
93 /*
94 * The scheduler can only be started from ARM mode, so
95 * vPortISRStartFirstSTask() is defined in portISR.c.
97 extern void vPortISRStartFirstTask( void );
99 /*-----------------------------------------------------------*/
102 * Initialise the stack of a task to look exactly as if a call to
103 * portSAVE_CONTEXT had been called.
105 * See header file for description.
107 portSTACK_TYPE *pxPortInitialiseStack( portSTACK_TYPE *pxTopOfStack, pdTASK_CODE pxCode, void *pvParameters )
109 portSTACK_TYPE *pxOriginalTOS;
111 pxOriginalTOS = pxTopOfStack;
113 /* Setup the initial stack of the task. The stack is set exactly as
114 expected by the portRESTORE_CONTEXT() macro. */
116 /* First on the stack is the return address - which in this case is the
117 start of the task. The offset is added to make the return address appear
118 as it would within an IRQ ISR. */
119 *pxTopOfStack = ( portSTACK_TYPE ) pxCode + portINSTRUCTION_SIZE;
120 pxTopOfStack--;
122 *pxTopOfStack = ( portSTACK_TYPE ) 0xaaaaaaaa; /* R14 */
123 pxTopOfStack--;
124 *pxTopOfStack = ( portSTACK_TYPE ) pxOriginalTOS; /* Stack used when task starts goes in R13. */
125 pxTopOfStack--;
126 *pxTopOfStack = ( portSTACK_TYPE ) 0x12121212; /* R12 */
127 pxTopOfStack--;
128 *pxTopOfStack = ( portSTACK_TYPE ) 0x11111111; /* R11 */
129 pxTopOfStack--;
130 *pxTopOfStack = ( portSTACK_TYPE ) 0x10101010; /* R10 */
131 pxTopOfStack--;
132 *pxTopOfStack = ( portSTACK_TYPE ) 0x09090909; /* R9 */
133 pxTopOfStack--;
134 *pxTopOfStack = ( portSTACK_TYPE ) 0x08080808; /* R8 */
135 pxTopOfStack--;
136 *pxTopOfStack = ( portSTACK_TYPE ) 0x07070707; /* R7 */
137 pxTopOfStack--;
138 *pxTopOfStack = ( portSTACK_TYPE ) 0x06060606; /* R6 */
139 pxTopOfStack--;
140 *pxTopOfStack = ( portSTACK_TYPE ) 0x05050505; /* R5 */
141 pxTopOfStack--;
142 *pxTopOfStack = ( portSTACK_TYPE ) 0x04040404; /* R4 */
143 pxTopOfStack--;
144 *pxTopOfStack = ( portSTACK_TYPE ) 0x03030303; /* R3 */
145 pxTopOfStack--;
146 *pxTopOfStack = ( portSTACK_TYPE ) 0x02020202; /* R2 */
147 pxTopOfStack--;
148 *pxTopOfStack = ( portSTACK_TYPE ) 0x01010101; /* R1 */
149 pxTopOfStack--;
151 /* When the task starts is will expect to find the function parameter in
152 R0. */
153 *pxTopOfStack = ( portSTACK_TYPE ) pvParameters; /* R0 */
154 pxTopOfStack--;
156 /* The last thing onto the stack is the status register, which is set for
157 system mode, with interrupts enabled. */
158 *pxTopOfStack = ( portSTACK_TYPE ) portINITIAL_SPSR;
160 if( ( ( unsigned long ) pxCode & 0x01UL ) != 0x00 )
162 /* We want the task to start in thumb mode. */
163 *pxTopOfStack |= portTHUMB_MODE_BIT;
166 pxTopOfStack--;
168 /* Some optimisation levels use the stack differently to others. This
169 means the interrupt flags cannot always be stored on the stack and will
170 instead be stored in a variable, which is then saved as part of the
171 tasks context. */
172 *pxTopOfStack = portNO_CRITICAL_SECTION_NESTING;
174 return pxTopOfStack;
176 /*-----------------------------------------------------------*/
178 portBASE_TYPE xPortStartScheduler( void )
180 /* Start the timer that generates the tick ISR. Interrupts are disabled
181 here already. */
182 prvSetupTimerInterrupt();
184 /* Start the first task. */
185 vPortISRStartFirstTask();
187 /* Should not get here! */
188 return 0;
190 /*-----------------------------------------------------------*/
192 void vPortEndScheduler( void )
194 /* It is unlikely that the ARM port will require this function as there
195 is nothing to return to. */
197 /*-----------------------------------------------------------*/
200 * Setup the timer 0 to generate the tick interrupts at the required frequency.
202 static void prvSetupTimerInterrupt( void )
204 unsigned long ulCompareMatch;
205 extern void ( vTickISR )( void );
207 /* A 1ms tick does not require the use of the timer prescale. This is
208 defaulted to zero but can be used if necessary. */
209 T0_PR = portPRESCALE_VALUE;
211 /* Calculate the match value required for our wanted tick rate. */
212 ulCompareMatch = configCPU_CLOCK_HZ / configTICK_RATE_HZ;
214 /* Protect against divide by zero. Using an if() statement still results
215 in a warning - hence the #if. */
216 #if portPRESCALE_VALUE != 0
218 ulCompareMatch /= ( portPRESCALE_VALUE + 1 );
220 #endif
221 T0_MR0 = ulCompareMatch;
223 /* Generate tick with timer 0 compare match. */
224 T0_MCR = portRESET_COUNT_ON_MATCH | portINTERRUPT_ON_MATCH;
226 /* Setup the VIC for the timer. */
227 VICIntSelect &= ~( portTIMER_VIC_CHANNEL_BIT );
228 VICIntEnable |= portTIMER_VIC_CHANNEL_BIT;
230 /* The ISR installed depends on whether the preemptive or cooperative
231 scheduler is being used. */
233 VICVectAddr0 = ( long ) vTickISR;
234 VICVectCntl0 = portTIMER_VIC_CHANNEL | portTIMER_VIC_ENABLE;
236 /* Start the timer - interrupts are disabled when this function is called
237 so it is okay to do this here. */
238 T0_TCR = portENABLE_TIMER;
240 /*-----------------------------------------------------------*/