]> git.sur5r.net Git - freertos/blob - Source/portable/RVDS/ARM7_LPC21xx/port.c
Update version number.
[freertos] / Source / portable / RVDS / ARM7_LPC21xx / port.c
1 /*\r
2         FreeRTOS.org V5.4.0 - Copyright (C) 2003-2009 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 it\r
7         under the terms of the GNU General Public License (version 2) as published\r
8         by the Free Software Foundation and modified by the FreeRTOS exception.\r
9         **NOTE** The exception to the GPL is included to allow you to distribute a\r
10         combined work that includes FreeRTOS.org without being obliged to provide\r
11         the source code for any proprietary components.  Alternative commercial\r
12         license and support terms are also available upon request.  See the \r
13         licensing section of http://www.FreeRTOS.org for full details.\r
14 \r
15         FreeRTOS.org is distributed in the hope that it will be useful, but WITHOUT\r
16         ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or\r
17         FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for\r
18         more details.\r
19 \r
20         You should have received a copy of the GNU General Public License along\r
21         with FreeRTOS.org; if not, write to the Free Software Foundation, Inc., 59\r
22         Temple Place, Suite 330, Boston, MA  02111-1307  USA.\r
23 \r
24 \r
25         ***************************************************************************\r
26         *                                                                         *\r
27         * Get the FreeRTOS eBook!  See http://www.FreeRTOS.org/Documentation      *\r
28         *                                                                         *\r
29         * This is a concise, step by step, 'hands on' guide that describes both   *\r
30         * general multitasking concepts and FreeRTOS specifics. It presents and   *\r
31         * explains numerous examples that are written using the FreeRTOS API.     *\r
32         * Full source code for all the examples is provided in an accompanying    *\r
33         * .zip file.                                                              *\r
34         *                                                                         *\r
35         ***************************************************************************\r
36 \r
37         1 tab == 4 spaces!\r
38 \r
39         Please ensure to read the configuration and relevant port sections of the\r
40         online documentation.\r
41 \r
42         http://www.FreeRTOS.org - Documentation, latest information, license and\r
43         contact details.\r
44 \r
45         http://www.SafeRTOS.com - A version that is certified for use in safety\r
46         critical systems.\r
47 \r
48         http://www.OpenRTOS.com - Commercial support, development, porting,\r
49         licensing and training services.\r
50 */\r
51 \r
52 \r
53 /* Standard includes. */\r
54 #include <stdlib.h>\r
55 \r
56 /* Scheduler includes. */\r
57 #include "FreeRTOS.h"\r
58 #include "task.h"\r
59 \r
60 /* Constants required to setup the initial task context. */\r
61 #define portINITIAL_SPSR                                ( ( portSTACK_TYPE ) 0x1f ) /* System mode, ARM mode, interrupts enabled. */\r
62 #define portTHUMB_MODE_BIT                              ( ( portSTACK_TYPE ) 0x20 )\r
63 #define portINSTRUCTION_SIZE                    ( ( portSTACK_TYPE ) 4 )\r
64 #define portNO_CRITICAL_SECTION_NESTING ( ( portSTACK_TYPE ) 0 )\r
65 \r
66 /* Constants required to setup the tick ISR. */\r
67 #define portENABLE_TIMER                        ( ( unsigned portCHAR ) 0x01 )\r
68 #define portPRESCALE_VALUE                      0x00\r
69 #define portINTERRUPT_ON_MATCH          ( ( unsigned portLONG ) 0x01 )\r
70 #define portRESET_COUNT_ON_MATCH        ( ( unsigned portLONG ) 0x02 )\r
71 \r
72 /* Constants required to setup the VIC for the tick ISR. */\r
73 #define portTIMER_VIC_CHANNEL           ( ( unsigned portLONG ) 0x0004 )\r
74 #define portTIMER_VIC_CHANNEL_BIT       ( ( unsigned portLONG ) 0x0010 )\r
75 #define portTIMER_VIC_ENABLE            ( ( unsigned portLONG ) 0x0020 )\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 /*-----------------------------------------------------------*/\r
82 \r
83 /* The code generated by the Keil compiler does not maintain separate\r
84 stack and frame pointers. The portENTER_CRITICAL macro cannot therefore\r
85 use the stack as per other ports.  Instead a variable is used to keep\r
86 track of the critical section nesting.  This variable has to be stored\r
87 as part of the task context and must be initialised to a non zero value. */\r
88 \r
89 #define portNO_CRITICAL_NESTING         ( ( unsigned portLONG ) 0 )\r
90 volatile unsigned portLONG ulCriticalNesting = 9999UL;\r
91 \r
92 /*-----------------------------------------------------------*/\r
93 \r
94 /* Setup the timer to generate the tick interrupts. */\r
95 static void prvSetupTimerInterrupt( void );\r
96 \r
97 /* \r
98  * The scheduler can only be started from ARM mode, so \r
99  * vPortStartFirstSTask() is defined in portISR.c. \r
100  */\r
101 extern __asm void vPortStartFirstTask( void );\r
102 \r
103 /*-----------------------------------------------------------*/\r
104 \r
105 /* \r
106  * See header file for description. \r
107  */\r
108 portSTACK_TYPE *pxPortInitialiseStack( portSTACK_TYPE *pxTopOfStack, pdTASK_CODE pxCode, void *pvParameters )\r
109 {\r
110 portSTACK_TYPE *pxOriginalTOS;\r
111 \r
112         /* Setup the initial stack of the task.  The stack is set exactly as \r
113         expected by the portRESTORE_CONTEXT() macro.\r
114 \r
115         Remember where the top of the (simulated) stack is before we place \r
116         anything on it. */\r
117         pxOriginalTOS = pxTopOfStack;\r
118 \r
119         /* First on the stack is the return address - which in this case is the\r
120         start of the task.  The offset is added to make the return address appear\r
121         as it would within an IRQ ISR. */\r
122         *pxTopOfStack = ( portSTACK_TYPE ) pxCode + portINSTRUCTION_SIZE;               \r
123         pxTopOfStack--;\r
124 \r
125         *pxTopOfStack = ( portSTACK_TYPE ) 0xaaaaaaaa;  /* R14 */\r
126         pxTopOfStack--; \r
127         *pxTopOfStack = ( portSTACK_TYPE ) pxOriginalTOS; /* Stack used when task starts goes in R13. */\r
128         pxTopOfStack--;\r
129         *pxTopOfStack = ( portSTACK_TYPE ) 0x12121212;  /* R12 */\r
130         pxTopOfStack--; \r
131         *pxTopOfStack = ( portSTACK_TYPE ) 0x11111111;  /* R11 */\r
132         pxTopOfStack--; \r
133         *pxTopOfStack = ( portSTACK_TYPE ) 0x10101010;  /* R10 */\r
134         pxTopOfStack--; \r
135         *pxTopOfStack = ( portSTACK_TYPE ) 0x09090909;  /* R9 */\r
136         pxTopOfStack--; \r
137         *pxTopOfStack = ( portSTACK_TYPE ) 0x08080808;  /* R8 */\r
138         pxTopOfStack--; \r
139         *pxTopOfStack = ( portSTACK_TYPE ) 0x07070707;  /* R7 */\r
140         pxTopOfStack--; \r
141         *pxTopOfStack = ( portSTACK_TYPE ) 0x06060606;  /* R6 */\r
142         pxTopOfStack--; \r
143         *pxTopOfStack = ( portSTACK_TYPE ) 0x05050505;  /* R5 */\r
144         pxTopOfStack--; \r
145         *pxTopOfStack = ( portSTACK_TYPE ) 0x04040404;  /* R4 */\r
146         pxTopOfStack--; \r
147         *pxTopOfStack = ( portSTACK_TYPE ) 0x03030303;  /* R3 */\r
148         pxTopOfStack--; \r
149         *pxTopOfStack = ( portSTACK_TYPE ) 0x02020202;  /* R2 */\r
150         pxTopOfStack--; \r
151         *pxTopOfStack = ( portSTACK_TYPE ) 0x01010101;  /* R1 */\r
152         pxTopOfStack--; \r
153         *pxTopOfStack = ( portSTACK_TYPE ) pvParameters; /* R0 */\r
154         pxTopOfStack--;\r
155 \r
156         /* The last thing onto the stack is the status register, which is set for\r
157         system mode, with interrupts enabled. */\r
158         *pxTopOfStack = ( portSTACK_TYPE ) portINITIAL_SPSR;\r
159 \r
160         #ifdef KEIL_THUMB_INTERWORK\r
161         {               \r
162                 /* We want the task to start in thumb mode. */\r
163                 *pxTopOfStack |= portTHUMB_MODE_BIT;\r
164         }\r
165         #endif\r
166 \r
167         pxTopOfStack--;\r
168 \r
169         /* The code generated by the Keil compiler does not maintain separate\r
170         stack and frame pointers. The portENTER_CRITICAL macro cannot therefore\r
171         use the stack as per other ports.  Instead a variable is used to keep\r
172         track of the critical section nesting.  This variable has to be stored\r
173         as part of the task context and is initially set to zero. */\r
174         *pxTopOfStack = portNO_CRITICAL_SECTION_NESTING;\r
175 \r
176         return pxTopOfStack;\r
177 }\r
178 /*-----------------------------------------------------------*/\r
179 \r
180 portBASE_TYPE xPortStartScheduler( void )\r
181 {\r
182         /* Start the timer that generates the tick ISR. */\r
183         prvSetupTimerInterrupt();\r
184 \r
185         /* Start the first task.  This is done from portISR.c as ARM mode must be\r
186         used. */\r
187         vPortStartFirstTask();\r
188 \r
189         /* Should not get here! */\r
190         return 0;\r
191 }\r
192 /*-----------------------------------------------------------*/\r
193 \r
194 void vPortEndScheduler( void )\r
195 {\r
196         /* It is unlikely that the ARM port will require this function as there\r
197         is nothing to return to.  If this is required - stop the tick ISR then\r
198         return back to main. */\r
199 }\r
200 /*-----------------------------------------------------------*/\r
201 \r
202 #if configUSE_PREEMPTION == 0\r
203 \r
204         /* \r
205          * The cooperative scheduler requires a normal IRQ service routine to \r
206          * simply increment the system tick. \r
207          */\r
208         void vNonPreemptiveTick( void ) __irq;\r
209         void vNonPreemptiveTick( void ) __irq\r
210         {\r
211                 /* Increment the tick count - this may make a delaying task ready\r
212                 to run - but a context switch is not performed. */              \r
213                 vTaskIncrementTick();\r
214 \r
215                 T0IR = portTIMER_MATCH_ISR_BIT;                         /* Clear the timer event */\r
216                 VICVectAddr = portCLEAR_VIC_INTERRUPT;          /* Acknowledge the Interrupt */\r
217         }\r
218 \r
219  #else\r
220 \r
221         /*\r
222          **************************************************************************\r
223          * The preemptive scheduler ISR is written in assembler and can be found   \r
224          * in the portASM.s file. This will only get used if portUSE_PREEMPTION\r
225          * is set to 1 in portmacro.h\r
226          ************************************************************************** \r
227          */\r
228 \r
229           void vPreemptiveTick( void );\r
230 \r
231 #endif\r
232 /*-----------------------------------------------------------*/\r
233 \r
234 static void prvSetupTimerInterrupt( void )\r
235 {\r
236 unsigned portLONG ulCompareMatch;\r
237 \r
238         /* A 1ms tick does not require the use of the timer prescale.  This is\r
239         defaulted to zero but can be used if necessary. */\r
240         T0PR = portPRESCALE_VALUE;\r
241 \r
242         /* Calculate the match value required for our wanted tick rate. */\r
243         ulCompareMatch = configCPU_CLOCK_HZ / configTICK_RATE_HZ;\r
244 \r
245         /* Protect against divide by zero.  Using an if() statement still results\r
246         in a warning - hence the #if. */\r
247         #if portPRESCALE_VALUE != 0\r
248         {\r
249                 ulCompareMatch /= ( portPRESCALE_VALUE + 1 );\r
250         }\r
251         #endif\r
252 \r
253         T0MR0 = ulCompareMatch;\r
254 \r
255         /* Generate tick with timer 0 compare match. */\r
256         T0MCR = portRESET_COUNT_ON_MATCH | portINTERRUPT_ON_MATCH;\r
257 \r
258         /* Setup the VIC for the timer. */\r
259         VICIntSelect &= ~( portTIMER_VIC_CHANNEL_BIT );\r
260         VICIntEnable |= portTIMER_VIC_CHANNEL_BIT;\r
261         \r
262         /* The ISR installed depends on whether the preemptive or cooperative\r
263         scheduler is being used. */\r
264         #if configUSE_PREEMPTION == 1\r
265         {       \r
266                 VICVectAddr0 = ( unsigned portLONG ) vPreemptiveTick;\r
267         }\r
268         #else\r
269         {\r
270                 VICVectAddr0 = ( unsigned portLONG ) vNonPreemptiveTick;\r
271         }\r
272         #endif\r
273 \r
274         VICVectCntl0 = portTIMER_VIC_CHANNEL | portTIMER_VIC_ENABLE;\r
275 \r
276         /* Start the timer - interrupts are disabled when this function is called\r
277         so it is okay to do this here. */\r
278         T0TCR = portENABLE_TIMER;\r
279 }\r
280 /*-----------------------------------------------------------*/\r
281 \r
282 void vPortEnterCritical( void )\r
283 {\r
284         /* Disable interrupts as per portDISABLE_INTERRUPTS();                                                  */\r
285         __disable_irq();\r
286 \r
287         /* Now interrupts are disabled ulCriticalNesting can be accessed \r
288         directly.  Increment ulCriticalNesting to keep a count of how many times\r
289         portENTER_CRITICAL() has been called. */\r
290         ulCriticalNesting++;\r
291 }\r
292 /*-----------------------------------------------------------*/\r
293 \r
294 void vPortExitCritical( void )\r
295 {\r
296         if( ulCriticalNesting > portNO_CRITICAL_NESTING )\r
297         {\r
298                 /* Decrement the nesting count as we are leaving a critical section. */\r
299                 ulCriticalNesting--;\r
300 \r
301                 /* If the nesting level has reached zero then interrupts should be\r
302                 re-enabled. */\r
303                 if( ulCriticalNesting == portNO_CRITICAL_NESTING )\r
304                 {\r
305                         /* Enable interrupts as per portEXIT_CRITICAL(). */\r
306                         __enable_irq();\r
307                 }\r
308         }\r
309 }\r
310 /*-----------------------------------------------------------*/\r
311 \r
312 \r