2 FreeRTOS V6.0.0 - Copyright (C) 2009 Real Time Engineers Ltd.
\r
4 ***************************************************************************
\r
8 * + New to FreeRTOS, *
\r
9 * + Wanting to learn FreeRTOS or multitasking in general quickly *
\r
10 * + Looking for basic training, *
\r
11 * + Wanting to improve your FreeRTOS skills and productivity *
\r
13 * then take a look at the FreeRTOS eBook *
\r
15 * "Using the FreeRTOS Real Time Kernel - a Practical Guide" *
\r
16 * http://www.FreeRTOS.org/Documentation *
\r
18 * A pdf reference manual is also available. Both are usually delivered *
\r
19 * to your inbox within 20 minutes to two hours when purchased between 8am *
\r
20 * and 8pm GMT (although please allow up to 24 hours in case of *
\r
21 * exceptional circumstances). Thank you for your support! *
\r
23 ***************************************************************************
\r
25 This file is part of the FreeRTOS distribution.
\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 exception to the GPL is included to allow you to distribute
\r
31 a combined work that includes FreeRTOS without being obliged to provide the
\r
32 source code for proprietary components outside of the FreeRTOS kernel.
\r
33 FreeRTOS is distributed in the hope that it will be useful, but WITHOUT
\r
34 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
\r
35 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
44 http://www.FreeRTOS.org - Documentation, latest information, license and
\r
47 http://www.SafeRTOS.com - A version that is certified for use in safety
\r
50 http://www.OpenRTOS.com - Commercial support, development, porting,
\r
51 licensing and training services.
\r
54 /*-----------------------------------------------------------
\r
55 * Implementation of functions defined in portable.h for the ARM CM3 port.
\r
56 *----------------------------------------------------------*/
\r
58 /* Scheduler includes. */
\r
59 #include "FreeRTOS.h"
\r
62 #ifndef configKERNEL_INTERRUPT_PRIORITY
\r
63 #define configKERNEL_INTERRUPT_PRIORITY 255
\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
78 /* Constants required to set up the initial stack. */
\r
79 #define portINITIAL_XPSR ( 0x01000000 )
\r
81 /* Each task maintains its own interrupt status in the critical nesting
\r
83 static unsigned portBASE_TYPE uxCriticalNesting = 0xaaaaaaaa;
\r
86 * Setup the timer to generate the tick interrupts.
\r
88 static void prvSetupTimerInterrupt( void );
\r
91 * Exception handlers.
\r
93 void xPortPendSVHandler( void );
\r
94 void xPortSysTickHandler( void );
\r
95 void vPortSVCHandler( void );
\r
98 * Start first task is a separate function so it can be tested in isolation.
\r
100 void vPortStartFirstTask( void );
\r
102 /*-----------------------------------------------------------*/
\r
105 * See header file for description.
\r
107 portSTACK_TYPE *pxPortInitialiseStack( portSTACK_TYPE *pxTopOfStack, pdTASK_CODE pxCode, void *pvParameters )
\r
109 /* Simulate the stack frame as it would be created by a context switch
\r
111 pxTopOfStack--; /* Offset added to account for the way the MCU uses the stack on entry/exit of interrupts. */
\r
112 *pxTopOfStack = portINITIAL_XPSR; /* xPSR */
\r
114 *pxTopOfStack = ( portSTACK_TYPE ) pxCode; /* PC */
\r
116 *pxTopOfStack = 0; /* LR */
\r
117 pxTopOfStack -= 5; /* R12, R3, R2 and R1. */
\r
118 *pxTopOfStack = ( portSTACK_TYPE ) pvParameters; /* R0 */
\r
119 pxTopOfStack -= 8; /* R11, R10, R9, R8, R7, R6, R5 and R4. */
\r
121 return pxTopOfStack;
\r
123 /*-----------------------------------------------------------*/
\r
125 __asm void vPortSVCHandler( void )
\r
129 ldr r3, =pxCurrentTCB /* Restore the context. */
\r
130 ldr r1, [r3] /* Use pxCurrentTCBConst to get the pxCurrentTCB address. */
\r
131 ldr r0, [r1] /* The first item in pxCurrentTCB is the task top of stack. */
\r
132 ldmia r0!, {r4-r11} /* Pop the registers that are not automatically saved on exception entry and the critical nesting count. */
\r
133 msr psp, r0 /* Restore the task stack pointer. */
\r
139 /*-----------------------------------------------------------*/
\r
141 __asm void vPortStartFirstTask( void )
\r
145 /* Use the NVIC offset register to locate the stack. */
\r
146 ldr r0, =0xE000ED08
\r
149 /* Set the msp back to the start of the stack. */
\r
151 /* Call SVC to start the first task. */
\r
154 /*-----------------------------------------------------------*/
\r
157 * See header file for description.
\r
159 portBASE_TYPE xPortStartScheduler( void )
\r
161 /* Make PendSV, CallSV and SysTick the same priroity as the kernel. */
\r
162 *(portNVIC_SYSPRI2) |= portNVIC_PENDSV_PRI;
\r
163 *(portNVIC_SYSPRI2) |= portNVIC_SYSTICK_PRI;
\r
165 /* Start the timer that generates the tick ISR. Interrupts are disabled
\r
167 prvSetupTimerInterrupt();
\r
169 /* Initialise the critical nesting count ready for the first task. */
\r
170 uxCriticalNesting = 0;
\r
172 /* Start the first task. */
\r
173 vPortStartFirstTask();
\r
175 /* Should not get here! */
\r
178 /*-----------------------------------------------------------*/
\r
180 void vPortEndScheduler( void )
\r
182 /* It is unlikely that the CM3 port will require this function as there
\r
183 is nothing to return to. */
\r
185 /*-----------------------------------------------------------*/
\r
187 void vPortYieldFromISR( void )
\r
189 /* Set a PendSV to request a context switch. */
\r
190 *(portNVIC_INT_CTRL) = portNVIC_PENDSVSET;
\r
192 /*-----------------------------------------------------------*/
\r
194 void vPortEnterCritical( void )
\r
196 portDISABLE_INTERRUPTS();
\r
197 uxCriticalNesting++;
\r
199 /*-----------------------------------------------------------*/
\r
201 void vPortExitCritical( void )
\r
203 uxCriticalNesting--;
\r
204 if( uxCriticalNesting == 0 )
\r
206 portENABLE_INTERRUPTS();
\r
209 /*-----------------------------------------------------------*/
\r
211 __asm void xPortPendSVHandler( void )
\r
213 extern uxCriticalNesting;
\r
214 extern pxCurrentTCB;
\r
215 extern vTaskSwitchContext;
\r
221 ldr r3, =pxCurrentTCB /* Get the location of the current TCB. */
\r
224 stmdb r0!, {r4-r11} /* Save the remaining registers. */
\r
225 str r0, [r2] /* Save the new top of stack into the first member of the TCB. */
\r
227 stmdb sp!, {r3, r14}
\r
228 mov r0, #configMAX_SYSCALL_INTERRUPT_PRIORITY
\r
230 bl vTaskSwitchContext
\r
233 ldmia sp!, {r3, r14}
\r
236 ldr r0, [r1] /* The first item in pxCurrentTCB is the task top of stack. */
\r
237 ldmia r0!, {r4-r11} /* Pop the registers and the critical nesting count. */
\r
242 /*-----------------------------------------------------------*/
\r
244 void xPortSysTickHandler( void )
\r
246 unsigned long ulDummy;
\r
248 /* If using preemption, also force a context switch. */
\r
249 #if configUSE_PREEMPTION == 1
\r
250 *(portNVIC_INT_CTRL) = portNVIC_PENDSVSET;
\r
253 ulDummy = portSET_INTERRUPT_MASK_FROM_ISR();
\r
255 vTaskIncrementTick();
\r
257 portCLEAR_INTERRUPT_MASK_FROM_ISR( ulDummy );
\r
259 /*-----------------------------------------------------------*/
\r
262 * Setup the systick timer to generate the tick interrupts at the required
\r
265 void prvSetupTimerInterrupt( void )
\r
267 /* Configure SysTick to interrupt at the requested rate. */
\r
268 *(portNVIC_SYSTICK_LOAD) = ( configCPU_CLOCK_HZ / configTICK_RATE_HZ ) - 1UL;
\r
269 *(portNVIC_SYSTICK_CTRL) = portNVIC_SYSTICK_CLK | portNVIC_SYSTICK_INT | portNVIC_SYSTICK_ENABLE;
\r
271 /*-----------------------------------------------------------*/
\r
273 __asm void vPortSetInterruptMask( void )
\r
278 mov r0, #configMAX_SYSCALL_INTERRUPT_PRIORITY
\r
284 /*-----------------------------------------------------------*/
\r
286 __asm void vPortClearInterruptMask( void )
\r