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