]> git.sur5r.net Git - freertos/blob - Source/portable/RVDS/ARM7_LPC21xx/port.c
Update version number to V7.0.1.
[freertos] / Source / portable / RVDS / ARM7_LPC21xx / port.c
1 /*\r
2     FreeRTOS V7.0.1 - Copyright (C) 2011 Real Time Engineers Ltd.\r
3         \r
4 \r
5     ***************************************************************************\r
6      *                                                                       *\r
7      *    FreeRTOS tutorial books are available in pdf and paperback.        *\r
8      *    Complete, revised, and edited pdf reference manuals are also       *\r
9      *    available.                                                         *\r
10      *                                                                       *\r
11      *    Purchasing FreeRTOS documentation will not only help you, by       *\r
12      *    ensuring you get running as quickly as possible and with an        *\r
13      *    in-depth knowledge of how to use FreeRTOS, it will also help       *\r
14      *    the FreeRTOS project to continue with its mission of providing     *\r
15      *    professional grade, cross platform, de facto standard solutions    *\r
16      *    for microcontrollers - completely free of charge!                  *\r
17      *                                                                       *\r
18      *    >>> See http://www.FreeRTOS.org/Documentation for details. <<<     *\r
19      *                                                                       *\r
20      *    Thank you for using FreeRTOS, and thank you for your support!      *\r
21      *                                                                       *\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 modification to the GPL is included to allow you to\r
31     distribute a combined work that includes FreeRTOS without being obliged to\r
32     provide the source code for proprietary components outside of the FreeRTOS\r
33     kernel.  FreeRTOS is distributed in the hope that it will be useful, but\r
34     WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY\r
35     or 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 /* Standard includes. */\r
56 #include <stdlib.h>\r
57 \r
58 /* Scheduler includes. */\r
59 #include "FreeRTOS.h"\r
60 #include "task.h"\r
61 \r
62 /* Constants required to setup the initial task context. */\r
63 #define portINITIAL_SPSR                                ( ( portSTACK_TYPE ) 0x1f ) /* System mode, ARM mode, interrupts enabled. */\r
64 #define portTHUMB_MODE_BIT                              ( ( portSTACK_TYPE ) 0x20 )\r
65 #define portINSTRUCTION_SIZE                    ( ( portSTACK_TYPE ) 4 )\r
66 #define portNO_CRITICAL_SECTION_NESTING ( ( portSTACK_TYPE ) 0 )\r
67 \r
68 /* Constants required to setup the tick ISR. */\r
69 #define portENABLE_TIMER                        ( ( unsigned portCHAR ) 0x01 )\r
70 #define portPRESCALE_VALUE                      0x00\r
71 #define portINTERRUPT_ON_MATCH          ( ( unsigned portLONG ) 0x01 )\r
72 #define portRESET_COUNT_ON_MATCH        ( ( unsigned portLONG ) 0x02 )\r
73 \r
74 /* Constants required to setup the VIC for the tick ISR. */\r
75 #define portTIMER_VIC_CHANNEL           ( ( unsigned portLONG ) 0x0004 )\r
76 #define portTIMER_VIC_CHANNEL_BIT       ( ( unsigned portLONG ) 0x0010 )\r
77 #define portTIMER_VIC_ENABLE            ( ( unsigned portLONG ) 0x0020 )\r
78 \r
79 /* Constants required to handle interrupts. */\r
80 #define portTIMER_MATCH_ISR_BIT         ( ( unsigned portCHAR ) 0x01 )\r
81 #define portCLEAR_VIC_INTERRUPT         ( ( unsigned portLONG ) 0 )\r
82 \r
83 /*-----------------------------------------------------------*/\r
84 \r
85 /* The code generated by the Keil compiler does not maintain separate\r
86 stack and frame pointers. The portENTER_CRITICAL macro cannot therefore\r
87 use the stack as per other ports.  Instead a variable is used to keep\r
88 track of the critical section nesting.  This variable has to be stored\r
89 as part of the task context and must be initialised to a non zero value. */\r
90 \r
91 #define portNO_CRITICAL_NESTING         ( ( unsigned portLONG ) 0 )\r
92 volatile unsigned portLONG ulCriticalNesting = 9999UL;\r
93 \r
94 /*-----------------------------------------------------------*/\r
95 \r
96 /* Setup the timer to generate the tick interrupts. */\r
97 static void prvSetupTimerInterrupt( void );\r
98 \r
99 /* \r
100  * The scheduler can only be started from ARM mode, so \r
101  * vPortStartFirstSTask() is defined in portISR.c. \r
102  */\r
103 extern __asm void vPortStartFirstTask( void );\r
104 \r
105 /*-----------------------------------------------------------*/\r
106 \r
107 /* \r
108  * See header file for description. \r
109  */\r
110 portSTACK_TYPE *pxPortInitialiseStack( portSTACK_TYPE *pxTopOfStack, pdTASK_CODE pxCode, void *pvParameters )\r
111 {\r
112 portSTACK_TYPE *pxOriginalTOS;\r
113 \r
114         /* Setup the initial stack of the task.  The stack is set exactly as \r
115         expected by the portRESTORE_CONTEXT() macro.\r
116 \r
117         Remember where the top of the (simulated) stack is before we place \r
118         anything on it. */\r
119         pxOriginalTOS = pxTopOfStack;\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         *pxTopOfStack = ( portSTACK_TYPE ) pvParameters; /* R0 */\r
156         pxTopOfStack--;\r
157 \r
158         /* The last thing onto the stack is the status register, which is set for\r
159         system mode, with interrupts enabled. */\r
160         *pxTopOfStack = ( portSTACK_TYPE ) portINITIAL_SPSR;\r
161 \r
162         if( ( ( unsigned long ) pxCode & 0x01UL ) != 0x00UL )\r
163         {\r
164                 /* We want the task to start in thumb mode. */\r
165                 *pxTopOfStack |= portTHUMB_MODE_BIT;\r
166         }\r
167 \r
168         pxTopOfStack--;\r
169 \r
170         /* The code generated by the Keil compiler does not maintain separate\r
171         stack and frame pointers. The portENTER_CRITICAL macro cannot therefore\r
172         use the stack as per other ports.  Instead a variable is used to keep\r
173         track of the critical section nesting.  This variable has to be stored\r
174         as part of the task context and is initially set to zero. */\r
175         *pxTopOfStack = portNO_CRITICAL_SECTION_NESTING;\r
176 \r
177         return pxTopOfStack;\r
178 }\r
179 /*-----------------------------------------------------------*/\r
180 \r
181 portBASE_TYPE xPortStartScheduler( void )\r
182 {\r
183         /* Start the timer that generates the tick ISR. */\r
184         prvSetupTimerInterrupt();\r
185 \r
186         /* Start the first task.  This is done from portISR.c as ARM mode must be\r
187         used. */\r
188         vPortStartFirstTask();\r
189 \r
190         /* Should not get here! */\r
191         return 0;\r
192 }\r
193 /*-----------------------------------------------------------*/\r
194 \r
195 void vPortEndScheduler( void )\r
196 {\r
197         /* It is unlikely that the ARM port will require this function as there\r
198         is nothing to return to.  If this is required - stop the tick ISR then\r
199         return back to main. */\r
200 }\r
201 /*-----------------------------------------------------------*/\r
202 \r
203 #if configUSE_PREEMPTION == 0\r
204 \r
205         /* \r
206          * The cooperative scheduler requires a normal IRQ service routine to \r
207          * simply increment the system tick. \r
208          */\r
209         void vNonPreemptiveTick( void ) __irq;\r
210         void vNonPreemptiveTick( void ) __irq\r
211         {\r
212                 /* Increment the tick count - this may make a delaying task ready\r
213                 to run - but a context switch is not performed. */              \r
214                 vTaskIncrementTick();\r
215 \r
216                 T0IR = portTIMER_MATCH_ISR_BIT;                         /* Clear the timer event */\r
217                 VICVectAddr = portCLEAR_VIC_INTERRUPT;          /* Acknowledge the Interrupt */\r
218         }\r
219 \r
220  #else\r
221 \r
222         /*\r
223          **************************************************************************\r
224          * The preemptive scheduler ISR is written in assembler and can be found   \r
225          * in the portASM.s file. This will only get used if portUSE_PREEMPTION\r
226          * is set to 1 in portmacro.h\r
227          ************************************************************************** \r
228          */\r
229 \r
230           void vPreemptiveTick( void );\r
231 \r
232 #endif\r
233 /*-----------------------------------------------------------*/\r
234 \r
235 static void prvSetupTimerInterrupt( void )\r
236 {\r
237 unsigned portLONG ulCompareMatch;\r
238 \r
239         /* A 1ms tick does not require the use of the timer prescale.  This is\r
240         defaulted to zero but can be used if necessary. */\r
241         T0PR = portPRESCALE_VALUE;\r
242 \r
243         /* Calculate the match value required for our wanted tick rate. */\r
244         ulCompareMatch = configCPU_CLOCK_HZ / configTICK_RATE_HZ;\r
245 \r
246         /* Protect against divide by zero.  Using an if() statement still results\r
247         in a warning - hence the #if. */\r
248         #if portPRESCALE_VALUE != 0\r
249         {\r
250                 ulCompareMatch /= ( portPRESCALE_VALUE + 1 );\r
251         }\r
252         #endif\r
253 \r
254         T0MR0 = ulCompareMatch;\r
255 \r
256         /* Generate tick with timer 0 compare match. */\r
257         T0MCR = portRESET_COUNT_ON_MATCH | portINTERRUPT_ON_MATCH;\r
258 \r
259         /* Setup the VIC for the timer. */\r
260         VICIntSelect &= ~( portTIMER_VIC_CHANNEL_BIT );\r
261         VICIntEnable |= portTIMER_VIC_CHANNEL_BIT;\r
262         \r
263         /* The ISR installed depends on whether the preemptive or cooperative\r
264         scheduler is being used. */\r
265         #if configUSE_PREEMPTION == 1\r
266         {       \r
267                 VICVectAddr0 = ( unsigned portLONG ) vPreemptiveTick;\r
268         }\r
269         #else\r
270         {\r
271                 VICVectAddr0 = ( unsigned portLONG ) vNonPreemptiveTick;\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 as per portDISABLE_INTERRUPTS();                                                  */\r
286         __disable_irq();\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 interrupts as per portEXIT_CRITICAL(). */\r
307                         __enable_irq();\r
308                 }\r
309         }\r
310 }\r
311 /*-----------------------------------------------------------*/\r
312 \r
313 \r