]> git.sur5r.net Git - freertos/blob - Source/portable/RVDS/ARM_CM3/port.c
Update version numbers to V4.8.0
[freertos] / Source / portable / RVDS / ARM_CM3 / port.c
1 /*\r
2         FreeRTOS.org V4.8.0 - 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         *                                                                                                                                                 *\r
29         * SAVE TIME AND MONEY!  Why not get us to quote to get FreeRTOS.org               *\r
30         * running on your hardware - or even write all or part of your application*\r
31         * for you?  See http://www.OpenRTOS.com for details.                                      *\r
32         *                                                                                                                                                 *\r
33         ***************************************************************************\r
34         ***************************************************************************\r
35 \r
36         Please ensure to read the configuration and relevant port sections of the\r
37         online documentation.\r
38 \r
39         http://www.FreeRTOS.org - Documentation, latest information, license and \r
40         contact details.\r
41 \r
42         http://www.SafeRTOS.com - A version that is certified for use in safety \r
43         critical systems.\r
44 \r
45         http://www.OpenRTOS.com - Commercial support, development, porting, \r
46         licensing and training services.\r
47 */\r
48 \r
49 /*-----------------------------------------------------------\r
50  * Implementation of functions defined in portable.h for the ARM CM3 port.\r
51  *----------------------------------------------------------*/\r
52 \r
53 /* Scheduler includes. */\r
54 #include "FreeRTOS.h"\r
55 #include "task.h"\r
56 \r
57 #ifndef configKERNEL_INTERRUPT_PRIORITY\r
58         #define configKERNEL_INTERRUPT_PRIORITY 255\r
59 #endif\r
60 \r
61 /* Constants required to manipulate the NVIC. */\r
62 #define portNVIC_SYSTICK_CTRL           ( ( volatile unsigned portLONG *) 0xe000e010 )\r
63 #define portNVIC_SYSTICK_LOAD           ( ( volatile unsigned portLONG *) 0xe000e014 )\r
64 #define portNVIC_INT_CTRL                       ( ( volatile unsigned portLONG *) 0xe000ed04 )\r
65 #define portNVIC_SYSPRI2                        ( ( volatile unsigned portLONG *) 0xe000ed20 )\r
66 #define portNVIC_SYSTICK_CLK            0x00000004\r
67 #define portNVIC_SYSTICK_INT            0x00000002\r
68 #define portNVIC_SYSTICK_ENABLE         0x00000001\r
69 #define portNVIC_PENDSVSET                      0x10000000\r
70 #define portNVIC_PENDSV_PRI                     ( ( ( unsigned portLONG ) configKERNEL_INTERRUPT_PRIORITY ) << 16 )\r
71 #define portNVIC_SYSTICK_PRI            ( ( ( unsigned portLONG ) configKERNEL_INTERRUPT_PRIORITY ) << 24 )\r
72 \r
73 /* Constants required to set up the initial stack. */\r
74 #define portINITIAL_XPSR                        ( 0x01000000 )\r
75 \r
76 /* Each task maintains its own interrupt status in the critical nesting\r
77 variable. */\r
78 unsigned portBASE_TYPE uxCriticalNesting = 0xaaaaaaaa;\r
79 \r
80 /* \r
81  * Setup the timer to generate the tick interrupts.\r
82  */\r
83 static void prvSetupTimerInterrupt( void );\r
84 \r
85 /*\r
86  * Exception handlers.\r
87  */\r
88 void xPortPendSVHandler( void );\r
89 void xPortSysTickHandler( void );\r
90 void vPortSVCHandler( void );\r
91 \r
92 /*\r
93  * Start first task is a separate function so it can be tested in isolation.\r
94  */\r
95 void vPortStartFirstTask( unsigned long ulValue );\r
96 \r
97 /*-----------------------------------------------------------*/\r
98 \r
99 /* \r
100  * See header file for description. \r
101  */\r
102 portSTACK_TYPE *pxPortInitialiseStack( portSTACK_TYPE *pxTopOfStack, pdTASK_CODE pxCode, void *pvParameters )\r
103 {\r
104         /* Simulate the stack frame as it would be created by a context switch\r
105         interrupt. */\r
106         *pxTopOfStack = portINITIAL_XPSR;       /* xPSR */\r
107         pxTopOfStack--;\r
108         *pxTopOfStack = ( portSTACK_TYPE ) pxCode;      /* PC */\r
109         pxTopOfStack--;\r
110         *pxTopOfStack = 0;      /* LR */\r
111         pxTopOfStack -= 5;      /* R12, R3, R2 and R1. */\r
112         *pxTopOfStack = ( portSTACK_TYPE ) pvParameters;        /* R0 */\r
113         pxTopOfStack -= 9;      /* R11, R10, R9, R8, R7, R6, R5 and R4. */\r
114         *pxTopOfStack = 0x00000000; /* uxCriticalNesting. */\r
115 \r
116         return pxTopOfStack;\r
117 }\r
118 /*-----------------------------------------------------------*/\r
119 \r
120 __asm void vPortSVCHandler( void )\r
121 {\r
122         PRESERVE8\r
123 \r
124         ldr     r3, =pxCurrentTCB               /* Restore the context. */\r
125         ldr r1, [r3]                            /* Use pxCurrentTCBConst to get the pxCurrentTCB address. */\r
126         ldr r0, [r1]                            /* The first item in pxCurrentTCB is the task top of stack. */\r
127         ldmia r0!, {r1, r4-r11}         /* Pop the registers that are not automatically saved on exception entry and the critical nesting count. */\r
128         ldr r2, =uxCriticalNesting      /* Restore the critical nesting count used by the task. */\r
129         str r1, [r2]                            \r
130         msr psp, r0                                     /* Restore the task stack pointer. */\r
131         orr r14, #0xd                           \r
132         bx r14                                          \r
133 }\r
134 /*-----------------------------------------------------------*/\r
135 \r
136 __asm void vPortStartFirstTask( unsigned long ulValue )\r
137 {\r
138         PRESERVE8\r
139 \r
140         msr msp, r0                                     /* Set the msp back to the start of the stack. */\r
141         svc 0                                           /* System call to start first task. */\r
142 }\r
143 /*-----------------------------------------------------------*/\r
144 \r
145 /* \r
146  * See header file for description. \r
147  */\r
148 portBASE_TYPE xPortStartScheduler( void )\r
149 {\r
150         /* Make PendSV, CallSV and SysTick the same priroity as the kernel. */\r
151         *(portNVIC_SYSPRI2) |= portNVIC_PENDSV_PRI;\r
152         *(portNVIC_SYSPRI2) |= portNVIC_SYSTICK_PRI;\r
153 \r
154         /* Start the timer that generates the tick ISR.  Interrupts are disabled\r
155         here already. */\r
156         prvSetupTimerInterrupt();\r
157         \r
158         /* Start the first task. */\r
159         vPortStartFirstTask( *((unsigned portLONG *) 0 ) );\r
160 \r
161         /* Should not get here! */\r
162         return 0;\r
163 }\r
164 /*-----------------------------------------------------------*/\r
165 \r
166 void vPortEndScheduler( void )\r
167 {\r
168         /* It is unlikely that the CM3 port will require this function as there\r
169         is nothing to return to.  */\r
170 }\r
171 /*-----------------------------------------------------------*/\r
172 \r
173 void vPortYieldFromISR( void )\r
174 {\r
175         /* Set a PendSV to request a context switch. */\r
176         *(portNVIC_INT_CTRL) |= portNVIC_PENDSVSET;\r
177 \r
178         /* This function is also called in response to a Yield(), so we want\r
179         the yield to occur immediately. */\r
180         portENABLE_INTERRUPTS();\r
181 }\r
182 /*-----------------------------------------------------------*/\r
183 \r
184 void vPortEnterCritical( void )\r
185 {\r
186         portDISABLE_INTERRUPTS();\r
187         uxCriticalNesting++;\r
188 }\r
189 /*-----------------------------------------------------------*/\r
190 \r
191 void vPortExitCritical( void )\r
192 {\r
193         uxCriticalNesting--;\r
194         if( uxCriticalNesting == 0 )\r
195         {\r
196                 portENABLE_INTERRUPTS();\r
197         }\r
198 }\r
199 /*-----------------------------------------------------------*/\r
200 \r
201 __asm void xPortPendSVHandler( void )\r
202 {\r
203         extern uxCriticalNesting;\r
204         extern pxCurrentTCB;\r
205         extern vTaskSwitchContext;\r
206 \r
207         PRESERVE8\r
208 \r
209         mrs r0, psp                                              \r
210 \r
211         ldr     r3, =pxCurrentTCB                       /* Get the location of the current TCB. */\r
212         ldr     r2, [r3]                                                \r
213 \r
214         ldr r1, =uxCriticalNesting              /* Save the remaining registers and the critical nesting count onto the task stack. */\r
215         ldr r1, [r1]                                    \r
216         stmdb r0!, {r1,r4-r11}                  \r
217         str r0, [r2]                                     /* Save the new top of stack into the first member of the TCB. */\r
218 \r
219         stmdb sp!, {r3, r14}                     \r
220         bl vTaskSwitchContext                   \r
221         ldmia sp!, {r3, r14}                    \r
222 \r
223         ldr r1, [r3]                                     \r
224         ldr r2, =uxCriticalNesting\r
225         ldr r0, [r1]                                     /* The first item in pxCurrentTCB is the task top of stack. */\r
226         ldmia r0!, {r1, r4-r11}                  /* Pop the registers and the critical nesting count. */\r
227         str r1, [r2]                                     /* Save the new critical nesting value into ulCriticalNesting. */ \r
228         msr psp, r0                                              \r
229         orr r14, #0xd                                   \r
230 \r
231         cbnz r1, sv_disable_interrupts   /* If the nesting count is greater than 0 we need to exit with interrupts masked. */\r
232         bx r14\r
233 \r
234 sv_disable_interrupts\r
235         mov r1, #configKERNEL_INTERRUPT_PRIORITY\r
236         msr     basepri, r1                                     \r
237         bx r14                                                  \r
238 }\r
239 /*-----------------------------------------------------------*/\r
240 \r
241 void xPortSysTickHandler( void )\r
242 {\r
243         vTaskIncrementTick();\r
244         \r
245         /* If using preemption, also force a context switch. */\r
246         #if configUSE_PREEMPTION == 1\r
247                 *(portNVIC_INT_CTRL) |= portNVIC_PENDSVSET;     \r
248         #endif\r
249 }\r
250 /*-----------------------------------------------------------*/\r
251 \r
252 /*\r
253  * Setup the systick timer to generate the tick interrupts at the required\r
254  * frequency.\r
255  */\r
256 void prvSetupTimerInterrupt( void )\r
257 {\r
258         /* Configure SysTick to interrupt at the requested rate. */\r
259         *(portNVIC_SYSTICK_LOAD) = configCPU_CLOCK_HZ / configTICK_RATE_HZ;\r
260         *(portNVIC_SYSTICK_CTRL) = portNVIC_SYSTICK_CLK | portNVIC_SYSTICK_INT | portNVIC_SYSTICK_ENABLE;\r
261 }\r
262 /*-----------------------------------------------------------*/\r
263 \r
264 __asm void vPortSetInterruptMask( void )\r
265 {\r
266         PRESERVE8\r
267 \r
268         push { r0 }\r
269         mov r0, #configKERNEL_INTERRUPT_PRIORITY\r
270         msr basepri, r0\r
271         pop { r0 }\r
272         bx r14\r
273 }\r
274 \r
275 /*-----------------------------------------------------------*/\r
276 \r
277 __asm void vPortClearInterruptMask( void )\r
278 {\r
279         PRESERVE8\r
280 \r
281         push { r0 }\r
282         mov r0, #0\r
283         msr basepri, r0\r
284         pop { r0 }\r
285         bx r14\r
286 }\r