]> git.sur5r.net Git - freertos/blob - Source/portable/RVDS/ARM_CM4F/port.c
Add two Cortex-M4F port layers.
[freertos] / Source / portable / RVDS / ARM_CM4F / port.c
1 /*\r
2     FreeRTOS V7.0.2 - 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  * Implementation of functions defined in portable.h for the ARM CM4F port.\r
56  *----------------------------------------------------------*/\r
57 \r
58 /* Scheduler includes. */\r
59 #include "FreeRTOS.h"\r
60 #include "task.h"\r
61 \r
62 #ifndef __TARGET_FPU_VFP \r
63         #error This port can only be used when the project options are configured to enable hardware floating point support.\r
64 #endif\r
65 \r
66 /* Constants required to manipulate the NVIC. */\r
67 #define portNVIC_SYSTICK_CTRL           ( ( volatile unsigned long *) 0xe000e010 )\r
68 #define portNVIC_SYSTICK_LOAD           ( ( volatile unsigned long *) 0xe000e014 )\r
69 #define portNVIC_INT_CTRL                       ( ( volatile unsigned long *) 0xe000ed04 )\r
70 #define portNVIC_SYSPRI2                        ( ( volatile unsigned long *) 0xe000ed20 )\r
71 #define portNVIC_SYSTICK_CLK            0x00000004\r
72 #define portNVIC_SYSTICK_INT            0x00000002\r
73 #define portNVIC_SYSTICK_ENABLE         0x00000001\r
74 #define portNVIC_PENDSVSET                      0x10000000\r
75 #define portNVIC_PENDSV_PRI                     ( ( ( unsigned long ) configKERNEL_INTERRUPT_PRIORITY ) << 16 )\r
76 #define portNVIC_SYSTICK_PRI            ( ( ( unsigned long ) configKERNEL_INTERRUPT_PRIORITY ) << 24 )\r
77 \r
78 /* Constants required to manipulate the VFP. */\r
79 #define portFPCCR                                       ( ( volatile unsigned long * ) 0xe000ef34 ) /* Floating point context control register. */\r
80 #define portASPEN_AND_LSPEN_BITS        ( 0x3UL << 30UL )\r
81 \r
82 /* Constants required to set up the initial stack. */\r
83 #define portINITIAL_XPSR                        ( 0x01000000 )\r
84 #define portINITIAL_EXEC_RETURN         ( 0xfffffffd )\r
85 \r
86 /* Each task maintains its own interrupt status in the critical nesting\r
87 variable. */\r
88 static unsigned portBASE_TYPE uxCriticalNesting = 0xaaaaaaaa;\r
89 \r
90 /* \r
91  * Setup the timer to generate the tick interrupts.\r
92  */\r
93 static void prvSetupTimerInterrupt( void );\r
94 \r
95 /*\r
96  * Exception handlers.\r
97  */\r
98 void xPortPendSVHandler( void );\r
99 void xPortSysTickHandler( void );\r
100 void vPortSVCHandler( void );\r
101 \r
102 /*\r
103  * Start first task is a separate function so it can be tested in isolation.\r
104  */\r
105 static void prvStartFirstTask( void );\r
106 \r
107 /*\r
108  * Functions defined in portasm.s to enable the VFP.\r
109  */\r
110 static void prvEnableVFP( void );\r
111 \r
112 /*-----------------------------------------------------------*/\r
113 \r
114 /* \r
115  * See header file for description. \r
116  */\r
117 portSTACK_TYPE *pxPortInitialiseStack( portSTACK_TYPE *pxTopOfStack, pdTASK_CODE pxCode, void *pvParameters )\r
118 {\r
119         /* Simulate the stack frame as it would be created by a context switch\r
120         interrupt. */\r
121         \r
122         /* Offset added to account for the way the MCU uses the stack on entry/exit\r
123         of interrupts, and to ensure alignment. */\r
124         pxTopOfStack -= 2;\r
125                 \r
126         *pxTopOfStack = portINITIAL_XPSR;       /* xPSR */\r
127         pxTopOfStack--;\r
128         *pxTopOfStack = ( portSTACK_TYPE ) pxCode;      /* PC */\r
129         pxTopOfStack--;\r
130         *pxTopOfStack = 0;      /* LR */\r
131         \r
132         /* Save code space by skipping register initialisation. */\r
133         pxTopOfStack -= 5;      /* R12, R3, R2 and R1. */\r
134         *pxTopOfStack = ( portSTACK_TYPE ) pvParameters;        /* R0 */\r
135 \r
136         /* A save method is being used that requiers each task to maintain its\r
137         own exec return value. */\r
138         pxTopOfStack--;\r
139         *pxTopOfStack = portINITIAL_EXEC_RETURN;\r
140 \r
141         pxTopOfStack -= 8;      /* R11, R10, R9, R8, R7, R6, R5 and R4. */\r
142         \r
143         return pxTopOfStack;\r
144 }\r
145 /*-----------------------------------------------------------*/\r
146 \r
147 __asm void vPortSVCHandler( void )\r
148 {\r
149         PRESERVE8\r
150 \r
151         /* Get the location of the current TCB. */\r
152         ldr     r3, =pxCurrentTCB\r
153         ldr r1, [r3]\r
154         ldr r0, [r1]\r
155         /* Pop the core registers. */\r
156         ldmia r0!, {r4-r11, r14}\r
157         msr psp, r0\r
158         mov r0, #0\r
159         msr     basepri, r0     \r
160         bx r14\r
161 }\r
162 /*-----------------------------------------------------------*/\r
163 \r
164 __asm void prvStartFirstTask( void )\r
165 {\r
166         PRESERVE8\r
167 \r
168         /* Use the NVIC offset register to locate the stack. */\r
169         ldr r0, =0xE000ED08\r
170         ldr r0, [r0]\r
171         ldr r0, [r0]\r
172         /* Set the msp back to the start of the stack. */\r
173         msr msp, r0\r
174         /* Globally enable interrupts. */\r
175         cpsie i\r
176         /* Call SVC to start the first task. */\r
177         svc 0\r
178         nop\r
179 }\r
180 /*-----------------------------------------------------------*/\r
181 \r
182 __asm void prvEnableVFP( void )\r
183 {\r
184         PRESERVE8\r
185         \r
186         /* The FPU enable bits are in the CPACR. */\r
187         ldr.w r0, =0xE000ED88\r
188         ldr     r1, [r0]\r
189         \r
190         /* Enable CP10 and CP11 coprocessors, then save back. */\r
191         orr     r1, r1, #( 0xf << 20 )\r
192         str r1, [r0]\r
193         bx      r14     \r
194         nop\r
195 }\r
196 /*-----------------------------------------------------------*/\r
197 \r
198 /* \r
199  * See header file for description. \r
200  */\r
201 portBASE_TYPE xPortStartScheduler( void )\r
202 {\r
203         /* Make PendSV, CallSV and SysTick the same priroity as the kernel. */\r
204         *(portNVIC_SYSPRI2) |= portNVIC_PENDSV_PRI;\r
205         *(portNVIC_SYSPRI2) |= portNVIC_SYSTICK_PRI;\r
206 \r
207         /* Start the timer that generates the tick ISR.  Interrupts are disabled\r
208         here already. */\r
209         prvSetupTimerInterrupt();\r
210         \r
211         /* Initialise the critical nesting count ready for the first task. */\r
212         uxCriticalNesting = 0;\r
213 \r
214         /* Ensure the VFP is enabled - it should be anyway. */\r
215         prvEnableVFP();\r
216         \r
217         /* Lazy save always. */\r
218         *( portFPCCR ) |= portASPEN_AND_LSPEN_BITS;\r
219         \r
220         /* Start the first task. */\r
221         prvStartFirstTask();\r
222 \r
223         /* Should not get here! */\r
224         return 0;\r
225 }\r
226 /*-----------------------------------------------------------*/\r
227 \r
228 void vPortEndScheduler( void )\r
229 {\r
230         /* It is unlikely that the CM4F port will require this function as there\r
231         is nothing to return to.  */\r
232 }\r
233 /*-----------------------------------------------------------*/\r
234 \r
235 void vPortYieldFromISR( void )\r
236 {\r
237         /* Set a PendSV to request a context switch. */\r
238         *(portNVIC_INT_CTRL) = portNVIC_PENDSVSET;\r
239 }\r
240 /*-----------------------------------------------------------*/\r
241 \r
242 void vPortEnterCritical( void )\r
243 {\r
244         portDISABLE_INTERRUPTS();\r
245         uxCriticalNesting++;\r
246 }\r
247 /*-----------------------------------------------------------*/\r
248 \r
249 void vPortExitCritical( void )\r
250 {\r
251         uxCriticalNesting--;\r
252         if( uxCriticalNesting == 0 )\r
253         {\r
254                 portENABLE_INTERRUPTS();\r
255         }\r
256 }\r
257 /*-----------------------------------------------------------*/\r
258 \r
259 __asm void xPortPendSVHandler( void )\r
260 {\r
261         extern uxCriticalNesting;\r
262         extern pxCurrentTCB;\r
263         extern vTaskSwitchContext;\r
264 \r
265         PRESERVE8\r
266 \r
267         mrs r0, psp                                             \r
268         \r
269         /* Get the location of the current TCB. */\r
270         ldr     r3, =pxCurrentTCB                       \r
271         ldr     r2, [r3]                                                \r
272 \r
273         /* Is the task using the FPU context?  If so, push high vfp registers. */\r
274         tst r14, #0x10\r
275         it eq\r
276         vstmdbeq r0!, {s16-s31}\r
277 \r
278         /* Save the core registers. */\r
279         stmdb r0!, {r4-r11, r14}                                \r
280         \r
281         /* Save the new top of stack into the first member of the TCB. */\r
282         str r0, [r2]\r
283         \r
284         stmdb sp!, {r3, r14}\r
285         mov r0, #configMAX_SYSCALL_INTERRUPT_PRIORITY\r
286         msr basepri, r0\r
287         bl vTaskSwitchContext                   \r
288         mov r0, #0\r
289         msr basepri, r0\r
290         ldmia sp!, {r3, r14}\r
291 \r
292         /* The first item in pxCurrentTCB is the task top of stack. */\r
293         ldr r1, [r3]    \r
294         ldr r0, [r1]\r
295         \r
296         /* Pop the core registers. */\r
297         ldmia r0!, {r4-r11, r14}\r
298 \r
299         /* Is the task using the FPU context?  If so, pop the high vfp registers \r
300         too. */\r
301         tst r14, #0x10\r
302         it eq\r
303         vldmiaeq r0!, {s16-s31}\r
304         \r
305         msr psp, r0                                             \r
306         bx r14          \r
307         nop                                     \r
308 }\r
309 /*-----------------------------------------------------------*/\r
310 \r
311 void xPortSysTickHandler( void )\r
312 {\r
313 unsigned long ulDummy;\r
314 \r
315         /* If using preemption, also force a context switch. */\r
316         #if configUSE_PREEMPTION == 1\r
317                 *(portNVIC_INT_CTRL) = portNVIC_PENDSVSET;      \r
318         #endif\r
319 \r
320         ulDummy = portSET_INTERRUPT_MASK_FROM_ISR();\r
321         {\r
322                 vTaskIncrementTick();\r
323         }\r
324         portCLEAR_INTERRUPT_MASK_FROM_ISR( ulDummy );\r
325 }\r
326 /*-----------------------------------------------------------*/\r
327 \r
328 /*\r
329  * Setup the systick timer to generate the tick interrupts at the required\r
330  * frequency.\r
331  */\r
332 void prvSetupTimerInterrupt( void )\r
333 {\r
334         /* Configure SysTick to interrupt at the requested rate. */\r
335         *(portNVIC_SYSTICK_LOAD) = ( configCPU_CLOCK_HZ / configTICK_RATE_HZ ) - 1UL;\r
336         *(portNVIC_SYSTICK_CTRL) = portNVIC_SYSTICK_CLK | portNVIC_SYSTICK_INT | portNVIC_SYSTICK_ENABLE;\r
337 }\r
338 /*-----------------------------------------------------------*/\r
339 \r
340 __asm void vPortSetInterruptMask( void )\r
341 {\r
342         PRESERVE8\r
343 \r
344         mov r0, #configMAX_SYSCALL_INTERRUPT_PRIORITY\r
345         msr basepri, r0\r
346         bx r14\r
347 }\r
348 \r
349 /*-----------------------------------------------------------*/\r
350 \r
351 __asm void vPortClearInterruptMask( void )\r
352 {\r
353         PRESERVE8\r
354 \r
355         mov r0, #0\r
356         msr basepri, r0\r
357         bx r14\r
358 }\r