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