]> git.sur5r.net Git - freertos/blob - FreeRTOS/Source/portable/RVDS/ARM_CM3/port.c
Put comments in the code that give a reference to people who think that setting BASE_...
[freertos] / FreeRTOS / Source / portable / RVDS / ARM_CM3 / port.c
1 /*\r
2     FreeRTOS V7.2.0 - Copyright (C) 2012 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     ***************************************************************************\r
45      *                                                                       *\r
46      *    Having a problem?  Start by reading the FAQ "My application does   *\r
47      *    not run, what could be wrong?                                      *\r
48      *                                                                       *\r
49      *    http://www.FreeRTOS.org/FAQHelp.html                               *\r
50      *                                                                       *\r
51     ***************************************************************************\r
52 \r
53     \r
54     http://www.FreeRTOS.org - Documentation, training, latest information, \r
55     license and contact details.\r
56     \r
57     http://www.FreeRTOS.org/plus - A selection of FreeRTOS ecosystem products,\r
58     including FreeRTOS+Trace - an indispensable productivity tool.\r
59 \r
60     Real Time Engineers ltd license FreeRTOS to High Integrity Systems, who sell \r
61     the code with commercial support, indemnification, and middleware, under \r
62     the OpenRTOS brand: http://www.OpenRTOS.com.  High Integrity Systems also\r
63     provide a safety engineered and independently SIL3 certified version under \r
64     the SafeRTOS brand: http://www.SafeRTOS.com.\r
65 */\r
66 \r
67 /*-----------------------------------------------------------\r
68  * Implementation of functions defined in portable.h for the ARM CM3 port.\r
69  *----------------------------------------------------------*/\r
70 \r
71 /* Scheduler includes. */\r
72 #include "FreeRTOS.h"\r
73 #include "task.h"\r
74 \r
75 #ifndef configKERNEL_INTERRUPT_PRIORITY\r
76         #define configKERNEL_INTERRUPT_PRIORITY 255\r
77 #endif\r
78 \r
79 #if configMAX_SYSCALL_INTERRUPT_PRIORITY == 0\r
80         #error configMAX_SYSCALL_INTERRUPT_PRIORITY must not be set to 0.  See http://www.FreeRTOS.org/RTOS-Cortex-M3-M4.html\r
81 #endif\r
82 \r
83 /* Constants required to manipulate the NVIC. */\r
84 #define portNVIC_SYSTICK_CTRL           ( ( volatile unsigned long *) 0xe000e010 )\r
85 #define portNVIC_SYSTICK_LOAD           ( ( volatile unsigned long *) 0xe000e014 )\r
86 #define portNVIC_INT_CTRL                       ( ( volatile unsigned long *) 0xe000ed04 )\r
87 #define portNVIC_SYSPRI2                        ( ( volatile unsigned long *) 0xe000ed20 )\r
88 #define portNVIC_SYSTICK_CLK            0x00000004\r
89 #define portNVIC_SYSTICK_INT            0x00000002\r
90 #define portNVIC_SYSTICK_ENABLE         0x00000001\r
91 #define portNVIC_PENDSVSET                      0x10000000\r
92 #define portNVIC_PENDSV_PRI                     ( ( ( unsigned long ) configKERNEL_INTERRUPT_PRIORITY ) << 16 )\r
93 #define portNVIC_SYSTICK_PRI            ( ( ( unsigned long ) configKERNEL_INTERRUPT_PRIORITY ) << 24 )\r
94 \r
95 /* Constants required to set up the initial stack. */\r
96 #define portINITIAL_XPSR                        ( 0x01000000 )\r
97 \r
98 /* Each task maintains its own interrupt status in the critical nesting\r
99 variable. */\r
100 static unsigned portBASE_TYPE uxCriticalNesting = 0xaaaaaaaa;\r
101 \r
102 /* \r
103  * Setup the timer to generate the tick interrupts.\r
104  */\r
105 static void prvSetupTimerInterrupt( void );\r
106 \r
107 /*\r
108  * Exception handlers.\r
109  */\r
110 void xPortPendSVHandler( void );\r
111 void xPortSysTickHandler( void );\r
112 void vPortSVCHandler( void );\r
113 \r
114 /*\r
115  * Start first task is a separate function so it can be tested in isolation.\r
116  */\r
117 void vPortStartFirstTask( void );\r
118 \r
119 /*-----------------------------------------------------------*/\r
120 \r
121 /* \r
122  * See header file for description. \r
123  */\r
124 portSTACK_TYPE *pxPortInitialiseStack( portSTACK_TYPE *pxTopOfStack, pdTASK_CODE pxCode, void *pvParameters )\r
125 {\r
126         /* Simulate the stack frame as it would be created by a context switch\r
127         interrupt. */\r
128         pxTopOfStack--; /* Offset added to account for the way the MCU uses the stack on entry/exit of interrupts. */\r
129         *pxTopOfStack = portINITIAL_XPSR;       /* xPSR */\r
130         pxTopOfStack--;\r
131         *pxTopOfStack = ( portSTACK_TYPE ) pxCode;      /* PC */\r
132         pxTopOfStack--;\r
133         *pxTopOfStack = 0;      /* LR */\r
134         pxTopOfStack -= 5;      /* R12, R3, R2 and R1. */\r
135         *pxTopOfStack = ( portSTACK_TYPE ) pvParameters;        /* R0 */\r
136         pxTopOfStack -= 8;      /* R11, R10, R9, R8, R7, R6, R5 and R4. */\r
137 \r
138         return pxTopOfStack;\r
139 }\r
140 /*-----------------------------------------------------------*/\r
141 \r
142 __asm void vPortSVCHandler( void )\r
143 {\r
144         PRESERVE8\r
145 \r
146         ldr     r3, =pxCurrentTCB               /* Restore the context. */\r
147         ldr r1, [r3]                            /* Use pxCurrentTCBConst to get the pxCurrentTCB address. */\r
148         ldr r0, [r1]                            /* The first item in pxCurrentTCB is the task top of stack. */\r
149         ldmia r0!, {r4-r11}             /* Pop the registers that are not automatically saved on exception entry and the critical nesting count. */\r
150         msr psp, r0                                     /* Restore the task stack pointer. */\r
151         mov r0, #0\r
152         msr     basepri, r0\r
153         orr r14, #0xd                           \r
154         bx r14                                          \r
155 }\r
156 /*-----------------------------------------------------------*/\r
157 \r
158 __asm void vPortStartFirstTask( void )\r
159 {\r
160         PRESERVE8\r
161 \r
162         /* Use the NVIC offset register to locate the stack. */\r
163         ldr r0, =0xE000ED08\r
164         ldr r0, [r0]\r
165         ldr r0, [r0]\r
166         /* Set the msp back to the start of the stack. */\r
167         msr msp, r0\r
168         /* Globally enable interrupts. */\r
169         cpsie i\r
170         /* Call SVC to start the first task. */\r
171         svc 0\r
172         nop\r
173 }\r
174 /*-----------------------------------------------------------*/\r
175 \r
176 /* \r
177  * See header file for description. \r
178  */\r
179 portBASE_TYPE xPortStartScheduler( void )\r
180 {\r
181         /* Make PendSV, CallSV and SysTick the same priroity as the kernel. */\r
182         *(portNVIC_SYSPRI2) |= portNVIC_PENDSV_PRI;\r
183         *(portNVIC_SYSPRI2) |= portNVIC_SYSTICK_PRI;\r
184 \r
185         /* Start the timer that generates the tick ISR.  Interrupts are disabled\r
186         here already. */\r
187         prvSetupTimerInterrupt();\r
188         \r
189         /* Initialise the critical nesting count ready for the first task. */\r
190         uxCriticalNesting = 0;\r
191 \r
192         /* Start the first task. */\r
193         vPortStartFirstTask();\r
194 \r
195         /* Should not get here! */\r
196         return 0;\r
197 }\r
198 /*-----------------------------------------------------------*/\r
199 \r
200 void vPortEndScheduler( void )\r
201 {\r
202         /* It is unlikely that the CM3 port will require this function as there\r
203         is nothing to return to.  */\r
204 }\r
205 /*-----------------------------------------------------------*/\r
206 \r
207 void vPortYieldFromISR( void )\r
208 {\r
209         /* Set a PendSV to request a context switch. */\r
210         *(portNVIC_INT_CTRL) = portNVIC_PENDSVSET;\r
211 }\r
212 /*-----------------------------------------------------------*/\r
213 \r
214 void vPortEnterCritical( void )\r
215 {\r
216         portDISABLE_INTERRUPTS();\r
217         uxCriticalNesting++;\r
218 }\r
219 /*-----------------------------------------------------------*/\r
220 \r
221 void vPortExitCritical( void )\r
222 {\r
223         uxCriticalNesting--;\r
224         if( uxCriticalNesting == 0 )\r
225         {\r
226                 portENABLE_INTERRUPTS();\r
227         }\r
228 }\r
229 /*-----------------------------------------------------------*/\r
230 \r
231 __asm void xPortPendSVHandler( void )\r
232 {\r
233         extern uxCriticalNesting;\r
234         extern pxCurrentTCB;\r
235         extern vTaskSwitchContext;\r
236 \r
237         PRESERVE8\r
238 \r
239         mrs r0, psp                                              \r
240 \r
241         ldr     r3, =pxCurrentTCB                        /* Get the location of the current TCB. */\r
242         ldr     r2, [r3]                                                \r
243 \r
244         stmdb r0!, {r4-r11}                              /* Save the remaining registers. */\r
245         str r0, [r2]                                     /* Save the new top of stack into the first member of the TCB. */\r
246 \r
247         stmdb sp!, {r3, r14}            \r
248         mov r0, #configMAX_SYSCALL_INTERRUPT_PRIORITY\r
249         msr basepri, r0          \r
250         bl vTaskSwitchContext\r
251         mov r0, #0\r
252         msr basepri, r0\r
253         ldmia sp!, {r3, r14}                    \r
254 \r
255         ldr r1, [r3]                                     \r
256         ldr r0, [r1]                                     /* The first item in pxCurrentTCB is the task top of stack. */\r
257         ldmia r0!, {r4-r11}                      /* Pop the registers and the critical nesting count. */\r
258         msr psp, r0                                              \r
259         bx r14\r
260         nop\r
261 }\r
262 /*-----------------------------------------------------------*/\r
263 \r
264 void xPortSysTickHandler( void )\r
265 {\r
266 unsigned long ulDummy;\r
267 \r
268         /* If using preemption, also force a context switch. */\r
269         #if configUSE_PREEMPTION == 1\r
270                 *(portNVIC_INT_CTRL) = portNVIC_PENDSVSET;      \r
271         #endif\r
272 \r
273         ulDummy = portSET_INTERRUPT_MASK_FROM_ISR();\r
274         {\r
275                 vTaskIncrementTick();\r
276         }\r
277         portCLEAR_INTERRUPT_MASK_FROM_ISR( ulDummy );\r
278 }\r
279 /*-----------------------------------------------------------*/\r
280 \r
281 /*\r
282  * Setup the systick timer to generate the tick interrupts at the required\r
283  * frequency.\r
284  */\r
285 void prvSetupTimerInterrupt( void )\r
286 {\r
287         /* Configure SysTick to interrupt at the requested rate. */\r
288         *(portNVIC_SYSTICK_LOAD) = ( configCPU_CLOCK_HZ / configTICK_RATE_HZ ) - 1UL;\r
289         *(portNVIC_SYSTICK_CTRL) = portNVIC_SYSTICK_CLK | portNVIC_SYSTICK_INT | portNVIC_SYSTICK_ENABLE;\r
290 }\r
291 /*-----------------------------------------------------------*/\r
292 \r
293 __asm void vPortSetInterruptMask( void )\r
294 {\r
295         PRESERVE8\r
296 \r
297         mov r0, #configMAX_SYSCALL_INTERRUPT_PRIORITY\r
298         msr basepri, r0\r
299         bx r14\r
300 }\r
301 \r
302 /*-----------------------------------------------------------*/\r
303 \r
304 __asm void vPortClearInterruptMask( void )\r
305 {\r
306         PRESERVE8\r
307 \r
308         /* FAQ:  Setting BASEPRI to 0 is not a bug.  Please see \r
309         http://www.FreeRTOS.org/RTOS-Cortex-M3-M4.html before disagreeing. */\r
310         mov r0, #0\r
311         msr basepri, r0\r
312         bx r14\r
313 }\r