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