2 FreeRTOS V6.0.4 - Copyright (C) 2010 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 Philips ARM7 port.
\r
56 *----------------------------------------------------------*/
\r
61 + Bug fix - The prescale value for the timer setup is now written to T0PR
\r
62 instead of T0PC. This bug would have had no effect unless a prescale
\r
63 value was actually used.
\r
66 /* Standard includes. */
\r
68 #include <intrinsics.h>
\r
70 /* Scheduler includes. */
\r
71 #include "FreeRTOS.h"
\r
74 /* Constants required to setup the tick ISR. */
\r
75 #define portENABLE_TIMER ( ( unsigned char ) 0x01 )
\r
76 #define portPRESCALE_VALUE 0x00
\r
77 #define portINTERRUPT_ON_MATCH ( ( unsigned long ) 0x01 )
\r
78 #define portRESET_COUNT_ON_MATCH ( ( unsigned long ) 0x02 )
\r
80 /* Constants required to setup the initial stack. */
\r
81 #define portINITIAL_SPSR ( ( portSTACK_TYPE ) 0x1f ) /* System mode, ARM mode, interrupts enabled. */
\r
82 #define portTHUMB_MODE_BIT ( ( portSTACK_TYPE ) 0x20 )
\r
83 #define portINSTRUCTION_SIZE ( ( portSTACK_TYPE ) 4 )
\r
85 /* Constants required to setup the PIT. */
\r
86 #define portPIT_CLOCK_DIVISOR ( ( unsigned long ) 16 )
\r
87 #define portPIT_COUNTER_VALUE ( ( ( configCPU_CLOCK_HZ / portPIT_CLOCK_DIVISOR ) / 1000UL ) * portTICK_RATE_MS )
\r
89 /* Constants required to handle interrupts. */
\r
90 #define portTIMER_MATCH_ISR_BIT ( ( unsigned char ) 0x01 )
\r
91 #define portCLEAR_VIC_INTERRUPT ( ( unsigned long ) 0 )
\r
93 /* Constants required to handle critical sections. */
\r
94 #define portNO_CRITICAL_NESTING ( ( unsigned long ) 0 )
\r
97 #define portINT_LEVEL_SENSITIVE 0
\r
98 #define portPIT_ENABLE ( ( unsigned short ) 0x1 << 24 )
\r
99 #define portPIT_INT_ENABLE ( ( unsigned short ) 0x1 << 25 )
\r
101 /* Constants required to setup the VIC for the tick ISR. */
\r
102 #define portTIMER_VIC_CHANNEL ( ( unsigned long ) 0x0004 )
\r
103 #define portTIMER_VIC_CHANNEL_BIT ( ( unsigned long ) 0x0010 )
\r
104 #define portTIMER_VIC_ENABLE ( ( unsigned long ) 0x0020 )
\r
106 /*-----------------------------------------------------------*/
\r
108 /* Setup the PIT to generate the tick interrupts. */
\r
109 static void prvSetupTimerInterrupt( void );
\r
111 /* ulCriticalNesting will get set to zero when the first task starts. It
\r
112 cannot be initialised to 0 as this will cause interrupts to be enabled
\r
113 during the kernel initialisation process. */
\r
114 unsigned long ulCriticalNesting = ( unsigned long ) 9999;
\r
116 /*-----------------------------------------------------------*/
\r
119 * Initialise the stack of a task to look exactly as if a call to
\r
120 * portSAVE_CONTEXT had been called.
\r
122 * See header file for description.
\r
124 portSTACK_TYPE *pxPortInitialiseStack( portSTACK_TYPE *pxTopOfStack, pdTASK_CODE pxCode, void *pvParameters )
\r
126 portSTACK_TYPE *pxOriginalTOS;
\r
128 pxOriginalTOS = pxTopOfStack;
\r
130 /* Setup the initial stack of the task. The stack is set exactly as
\r
131 expected by the portRESTORE_CONTEXT() macro. */
\r
133 /* First on the stack is the return address - which in this case is the
\r
134 start of the task. The offset is added to make the return address appear
\r
135 as it would within an IRQ ISR. */
\r
136 *pxTopOfStack = ( portSTACK_TYPE ) pxCode + portINSTRUCTION_SIZE;
\r
139 *pxTopOfStack = ( portSTACK_TYPE ) 0xaaaaaaaa; /* R14 */
\r
141 *pxTopOfStack = ( portSTACK_TYPE ) pxOriginalTOS; /* Stack used when task starts goes in R13. */
\r
143 *pxTopOfStack = ( portSTACK_TYPE ) 0x12121212; /* R12 */
\r
145 *pxTopOfStack = ( portSTACK_TYPE ) 0x11111111; /* R11 */
\r
147 *pxTopOfStack = ( portSTACK_TYPE ) 0x10101010; /* R10 */
\r
149 *pxTopOfStack = ( portSTACK_TYPE ) 0x09090909; /* R9 */
\r
151 *pxTopOfStack = ( portSTACK_TYPE ) 0x08080808; /* R8 */
\r
153 *pxTopOfStack = ( portSTACK_TYPE ) 0x07070707; /* R7 */
\r
155 *pxTopOfStack = ( portSTACK_TYPE ) 0x06060606; /* R6 */
\r
157 *pxTopOfStack = ( portSTACK_TYPE ) 0x05050505; /* R5 */
\r
159 *pxTopOfStack = ( portSTACK_TYPE ) 0x04040404; /* R4 */
\r
161 *pxTopOfStack = ( portSTACK_TYPE ) 0x03030303; /* R3 */
\r
163 *pxTopOfStack = ( portSTACK_TYPE ) 0x02020202; /* R2 */
\r
165 *pxTopOfStack = ( portSTACK_TYPE ) 0x01010101; /* R1 */
\r
168 /* When the task starts is will expect to find the function parameter in
\r
170 *pxTopOfStack = ( portSTACK_TYPE ) pvParameters; /* R0 */
\r
173 /* The status register is set for system mode, with interrupts enabled. */
\r
174 *pxTopOfStack = ( portSTACK_TYPE ) portINITIAL_SPSR;
\r
176 if( ( ( unsigned long ) pxCode & 0x01UL ) != 0x00UL )
\r
178 /* We want the task to start in thumb mode. */
\r
179 *pxTopOfStack |= portTHUMB_MODE_BIT;
\r
184 /* Interrupt flags cannot always be stored on the stack and will
\r
185 instead be stored in a variable, which is then saved as part of the
\r
187 *pxTopOfStack = portNO_CRITICAL_NESTING;
\r
189 return pxTopOfStack;
\r
191 /*-----------------------------------------------------------*/
\r
193 portBASE_TYPE xPortStartScheduler( void )
\r
195 extern void vPortStartFirstTask( void );
\r
197 /* Start the timer that generates the tick ISR. Interrupts are disabled
\r
199 prvSetupTimerInterrupt();
\r
201 /* Start the first task. */
\r
202 vPortStartFirstTask();
\r
204 /* Should not get here! */
\r
207 /*-----------------------------------------------------------*/
\r
209 void vPortEndScheduler( void )
\r
211 /* It is unlikely that the ARM port will require this function as there
\r
212 is nothing to return to. */
\r
214 /*-----------------------------------------------------------*/
\r
216 #if configUSE_PREEMPTION == 0
\r
218 /* The cooperative scheduler requires a normal IRQ service routine to
\r
219 simply increment the system tick. */
\r
220 static __arm __irq void vPortNonPreemptiveTick( void );
\r
221 static __arm __irq void vPortNonPreemptiveTick( void )
\r
223 /* Increment the tick count - which may wake some tasks but as the
\r
224 preemptive scheduler is not being used any woken task is not given
\r
225 processor time no matter what its priority. */
\r
226 vTaskIncrementTick();
\r
228 /* Ready for the next interrupt. */
\r
229 T0IR = portTIMER_MATCH_ISR_BIT;
\r
230 VICVectAddr = portCLEAR_VIC_INTERRUPT;
\r
235 /* This function is called from an asm wrapper, so does not require the __irq
\r
237 void vPortPreemptiveTick( void );
\r
238 void vPortPreemptiveTick( void )
\r
240 /* Increment the tick counter. */
\r
241 vTaskIncrementTick();
\r
243 /* The new tick value might unblock a task. Ensure the highest task that
\r
244 is ready to execute is the task that will execute when the tick ISR
\r
246 vTaskSwitchContext();
\r
248 /* Ready for the next interrupt. */
\r
249 T0IR = portTIMER_MATCH_ISR_BIT;
\r
250 VICVectAddr = portCLEAR_VIC_INTERRUPT;
\r
255 /*-----------------------------------------------------------*/
\r
257 static void prvSetupTimerInterrupt( void )
\r
259 unsigned long ulCompareMatch;
\r
261 /* A 1ms tick does not require the use of the timer prescale. This is
\r
262 defaulted to zero but can be used if necessary. */
\r
263 T0PR = portPRESCALE_VALUE;
\r
265 /* Calculate the match value required for our wanted tick rate. */
\r
266 ulCompareMatch = configCPU_CLOCK_HZ / configTICK_RATE_HZ;
\r
268 /* Protect against divide by zero. Using an if() statement still results
\r
269 in a warning - hence the #if. */
\r
270 #if portPRESCALE_VALUE != 0
\r
272 ulCompareMatch /= ( portPRESCALE_VALUE + 1 );
\r
276 T0MR0 = ulCompareMatch;
\r
278 /* Generate tick with timer 0 compare match. */
\r
279 T0MCR = portRESET_COUNT_ON_MATCH | portINTERRUPT_ON_MATCH;
\r
281 /* Setup the VIC for the timer. */
\r
282 VICIntSelect &= ~( portTIMER_VIC_CHANNEL_BIT );
\r
283 VICIntEnable |= portTIMER_VIC_CHANNEL_BIT;
\r
285 /* The ISR installed depends on whether the preemptive or cooperative
\r
286 scheduler is being used. */
\r
287 #if configUSE_PREEMPTION == 1
\r
289 extern void ( vPortPreemptiveTickEntry )( void );
\r
291 VICVectAddr0 = ( unsigned long ) vPortPreemptiveTickEntry;
\r
295 extern void ( vNonPreemptiveTick )( void );
\r
297 VICVectAddr0 = ( long ) vPortNonPreemptiveTick;
\r
301 VICVectCntl0 = portTIMER_VIC_CHANNEL | portTIMER_VIC_ENABLE;
\r
303 /* Start the timer - interrupts are disabled when this function is called
\r
304 so it is okay to do this here. */
\r
305 T0TCR = portENABLE_TIMER;
\r
307 /*-----------------------------------------------------------*/
\r
309 void vPortEnterCritical( void )
\r
311 /* Disable interrupts first! */
\r
312 __disable_interrupt();
\r
314 /* Now interrupts are disabled ulCriticalNesting can be accessed
\r
315 directly. Increment ulCriticalNesting to keep a count of how many times
\r
316 portENTER_CRITICAL() has been called. */
\r
317 ulCriticalNesting++;
\r
319 /*-----------------------------------------------------------*/
\r
321 void vPortExitCritical( void )
\r
323 if( ulCriticalNesting > portNO_CRITICAL_NESTING )
\r
325 /* Decrement the nesting count as we are leaving a critical section. */
\r
326 ulCriticalNesting--;
\r
328 /* If the nesting level has reached zero then interrupts should be
\r
330 if( ulCriticalNesting == portNO_CRITICAL_NESTING )
\r
332 __enable_interrupt();
\r
336 /*-----------------------------------------------------------*/
\r