]> git.sur5r.net Git - freertos/blob - Source/portable/GCC/ARM7_LPC23xx/port.c
Prepare for V5.3.0 release.
[freertos] / Source / portable / GCC / ARM7_LPC23xx / port.c
1 /*\r
2         FreeRTOS.org V5.3.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 /*-----------------------------------------------------------\r
54  * Implementation of functions defined in portable.h for the ARM7 port.\r
55  *\r
56  * Components that can be compiled to either ARM or THUMB mode are\r
57  * contained in this file.  The ISR routines, which can only be compiled\r
58  * to ARM mode are contained in portISR.c.\r
59  *----------------------------------------------------------*/\r
60 \r
61 \r
62 /* Standard includes. */\r
63 #include <stdlib.h>\r
64 \r
65 /* Scheduler includes. */\r
66 #include "FreeRTOS.h"\r
67 #include "task.h"\r
68 \r
69 /* Constants required to setup the task context. */\r
70 #define portINITIAL_SPSR                                ( ( portSTACK_TYPE ) 0x1f ) /* System mode, ARM mode, interrupts enabled. */\r
71 #define portTHUMB_MODE_BIT                              ( ( portSTACK_TYPE ) 0x20 )\r
72 #define portINSTRUCTION_SIZE                    ( ( portSTACK_TYPE ) 4 )\r
73 #define portNO_CRITICAL_SECTION_NESTING ( ( portSTACK_TYPE ) 0 )\r
74 \r
75 /* Constants required to setup the tick ISR. */\r
76 #define portENABLE_TIMER                ( ( unsigned portCHAR ) 0x01 )\r
77 #define portPRESCALE_VALUE              0x00\r
78 #define portINTERRUPT_ON_MATCH          ( ( unsigned portLONG ) 0x01 )\r
79 #define portRESET_COUNT_ON_MATCH        ( ( unsigned portLONG ) 0x02 )\r
80 \r
81 /* Constants required to setup the VIC for the tick ISR. */\r
82 #define portTIMER_VIC_CHANNEL           ( ( unsigned portLONG ) 0x0004 )\r
83 #define portTIMER_VIC_CHANNEL_BIT       ( ( unsigned portLONG ) 0x0010 )\r
84 #define portTIMER_VIC_ENABLE            ( ( unsigned portLONG ) 0x0020 )\r
85 \r
86 /*-----------------------------------------------------------*/\r
87 \r
88 /* Setup the timer to generate the tick interrupts. */\r
89 static void prvSetupTimerInterrupt( void );\r
90 \r
91 /* \r
92  * The scheduler can only be started from ARM mode, so \r
93  * vPortISRStartFirstSTask() is defined in portISR.c. \r
94  */\r
95 extern void vPortISRStartFirstTask( void );\r
96 \r
97 /*-----------------------------------------------------------*/\r
98 \r
99 /* \r
100  * Initialise the stack of a task to look exactly as if a call to \r
101  * portSAVE_CONTEXT had been called.\r
102  *\r
103  * See header file for description. \r
104  */\r
105 portSTACK_TYPE *pxPortInitialiseStack( portSTACK_TYPE *pxTopOfStack, pdTASK_CODE pxCode, void *pvParameters )\r
106 {\r
107 portSTACK_TYPE *pxOriginalTOS;\r
108 \r
109         pxOriginalTOS = pxTopOfStack;\r
110 \r
111         /* Setup the initial stack of the task.  The stack is set exactly as \r
112         expected by the portRESTORE_CONTEXT() macro. */\r
113 \r
114         /* First on the stack is the return address - which in this case is the\r
115         start of the task.  The offset is added to make the return address appear\r
116         as it would within an IRQ ISR. */\r
117         *pxTopOfStack = ( portSTACK_TYPE ) pxCode + portINSTRUCTION_SIZE;               \r
118         pxTopOfStack--;\r
119 \r
120         *pxTopOfStack = ( portSTACK_TYPE ) 0x00000000;  /* R14 */\r
121         pxTopOfStack--; \r
122         *pxTopOfStack = ( portSTACK_TYPE ) pxOriginalTOS; /* Stack used when task starts goes in R13. */\r
123         pxTopOfStack--;\r
124         *pxTopOfStack = ( portSTACK_TYPE ) 0x12121212;  /* R12 */\r
125         pxTopOfStack--; \r
126         *pxTopOfStack = ( portSTACK_TYPE ) 0x11111111;  /* R11 */\r
127         pxTopOfStack--; \r
128         *pxTopOfStack = ( portSTACK_TYPE ) 0x10101010;  /* R10 */\r
129         pxTopOfStack--; \r
130         *pxTopOfStack = ( portSTACK_TYPE ) 0x09090909;  /* R9 */\r
131         pxTopOfStack--; \r
132         *pxTopOfStack = ( portSTACK_TYPE ) 0x08080808;  /* R8 */\r
133         pxTopOfStack--; \r
134         *pxTopOfStack = ( portSTACK_TYPE ) 0x07070707;  /* R7 */\r
135         pxTopOfStack--; \r
136         *pxTopOfStack = ( portSTACK_TYPE ) 0x06060606;  /* R6 */\r
137         pxTopOfStack--; \r
138         *pxTopOfStack = ( portSTACK_TYPE ) 0x05050505;  /* R5 */\r
139         pxTopOfStack--; \r
140         *pxTopOfStack = ( portSTACK_TYPE ) 0x04040404;  /* R4 */\r
141         pxTopOfStack--; \r
142         *pxTopOfStack = ( portSTACK_TYPE ) 0x03030303;  /* R3 */\r
143         pxTopOfStack--; \r
144         *pxTopOfStack = ( portSTACK_TYPE ) 0x02020202;  /* R2 */\r
145         pxTopOfStack--; \r
146         *pxTopOfStack = ( portSTACK_TYPE ) 0x01010101;  /* R1 */\r
147         pxTopOfStack--; \r
148 \r
149         /* When the task starts is will expect to find the function parameter in\r
150         R0. */\r
151         *pxTopOfStack = ( portSTACK_TYPE ) pvParameters; /* R0 */\r
152         pxTopOfStack--;\r
153 \r
154         /* The last thing onto the stack is the status register, which is set for\r
155         system mode, with interrupts enabled. */\r
156         *pxTopOfStack = ( portSTACK_TYPE ) portINITIAL_SPSR;\r
157 \r
158         #ifdef THUMB_INTERWORK\r
159         {\r
160                 /* We want the task to start in thumb mode. */\r
161                 *pxTopOfStack |= portTHUMB_MODE_BIT;\r
162         }\r
163         #endif\r
164 \r
165         pxTopOfStack--;\r
166 \r
167         /* Some optimisation levels use the stack differently to others.  This \r
168         means the interrupt flags cannot always be stored on the stack and will\r
169         instead be stored in a variable, which is then saved as part of the\r
170         tasks context. */\r
171         *pxTopOfStack = portNO_CRITICAL_SECTION_NESTING;\r
172 \r
173         return pxTopOfStack;\r
174 }\r
175 /*-----------------------------------------------------------*/\r
176 \r
177 portBASE_TYPE xPortStartScheduler( void )\r
178 {\r
179         /* Start the timer that generates the tick ISR.  Interrupts are disabled\r
180         here already. */\r
181         prvSetupTimerInterrupt();\r
182 \r
183         /* Start the first task. */\r
184         vPortISRStartFirstTask();       \r
185 \r
186         /* Should not get here! */\r
187         return 0;\r
188 }\r
189 /*-----------------------------------------------------------*/\r
190 \r
191 void vPortEndScheduler( void )\r
192 {\r
193         /* It is unlikely that the ARM port will require this function as there\r
194         is nothing to return to.  */\r
195 }\r
196 /*-----------------------------------------------------------*/\r
197 \r
198 /*\r
199  * Setup the timer 0 to generate the tick interrupts at the required frequency.\r
200  */\r
201 static void prvSetupTimerInterrupt( void )\r
202 {\r
203 unsigned portLONG ulCompareMatch;\r
204 \r
205         PCLKSEL0 = (PCLKSEL0 & (~(0x3<<2))) | (0x01 << 2);\r
206         T0TCR  = 2;         /* Stop and reset the timer */\r
207         T0CTCR = 0;         /* Timer mode               */\r
208         \r
209         /* A 1ms tick does not require the use of the timer prescale.  This is\r
210         defaulted to zero but can be used if necessary. */\r
211         T0PR = portPRESCALE_VALUE;\r
212 \r
213         /* Calculate the match value required for our wanted tick rate. */\r
214         ulCompareMatch = configCPU_CLOCK_HZ / configTICK_RATE_HZ;\r
215 \r
216         /* Protect against divide by zero.  Using an if() statement still results\r
217         in a warning - hence the #if. */\r
218         #if portPRESCALE_VALUE != 0\r
219         {\r
220                 ulCompareMatch /= ( portPRESCALE_VALUE + 1 );\r
221         }\r
222         #endif\r
223         T0MR1 = ulCompareMatch;\r
224 \r
225         /* Generate tick with timer 0 compare match. */\r
226         T0MCR  = (3 << 3);  /* Reset timer on match and generate interrupt */\r
227 \r
228         /* Setup the VIC for the timer. */\r
229         VICIntEnable = 0x00000010;\r
230         \r
231         /* The ISR installed depends on whether the preemptive or cooperative\r
232         scheduler is being used. */\r
233         #if configUSE_PREEMPTION == 1\r
234         {\r
235                 extern void ( vPreemptiveTick )( void );\r
236                 VICVectAddr4 = ( portLONG ) vPreemptiveTick;\r
237         }\r
238         #else\r
239         {\r
240                 extern void ( vNonPreemptiveTick )( void );\r
241                 VICVectAddr4 = ( portLONG ) vNonPreemptiveTick;\r
242         }\r
243         #endif\r
244 \r
245         VICVectCntl4 = 1;\r
246 \r
247         /* Start the timer - interrupts are disabled when this function is called\r
248         so it is okay to do this here. */\r
249         T0TCR = portENABLE_TIMER;\r
250 }\r
251 /*-----------------------------------------------------------*/\r
252 \r
253 \r
254 \r