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