2 FreeRTOS.org V4.2.1 - Copyright (C) 2003-2007 Richard Barry.
\r
4 This file is part of the FreeRTOS.org distribution.
\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
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
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
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
26 ***************************************************************************
\r
27 See http://www.FreeRTOS.org for documentation, latest information, license
\r
28 and contact details. Please ensure to read the configuration and relevant
\r
29 port sections of the online documentation.
\r
31 Also see http://www.SafeRTOS.com for an IEC 61508 compliant version along
\r
32 with commercial development and support options.
\r
33 ***************************************************************************
\r
36 /*-----------------------------------------------------------
\r
37 * Implementation of functions defined in portable.h for the Philips ARM7 port.
\r
38 *----------------------------------------------------------*/
\r
43 + Bug fix - The prescale value for the timer setup is now written to T0PR
\r
44 instead of T0PC. This bug would have had no effect unless a prescale
\r
45 value was actually used.
\r
48 /* Standard includes. */
\r
50 #include <intrinsic.h>
\r
52 /* Scheduler includes. */
\r
53 #include "FreeRTOS.h"
\r
56 /* Constants required to setup the tick ISR. */
\r
57 #define portENABLE_TIMER ( ( unsigned portCHAR ) 0x01 )
\r
58 #define portPRESCALE_VALUE 0x00
\r
59 #define portINTERRUPT_ON_MATCH ( ( unsigned portLONG ) 0x01 )
\r
60 #define portRESET_COUNT_ON_MATCH ( ( unsigned portLONG ) 0x02 )
\r
62 /* Constants required to setup the initial stack. */
\r
63 #define portINITIAL_SPSR ( ( portSTACK_TYPE ) 0x3f ) /* System mode, THUMB mode, interrupts enabled. */
\r
64 #define portINSTRUCTION_SIZE ( ( portSTACK_TYPE ) 4 )
\r
66 /* Constants required to setup the PIT. */
\r
67 #define portPIT_CLOCK_DIVISOR ( ( unsigned portLONG ) 16 )
\r
68 #define portPIT_COUNTER_VALUE ( ( ( configCPU_CLOCK_HZ / portPIT_CLOCK_DIVISOR ) / 1000UL ) * portTICK_RATE_MS )
\r
70 /* Constants required to handle interrupts. */
\r
71 #define portTIMER_MATCH_ISR_BIT ( ( unsigned portCHAR ) 0x01 )
\r
72 #define portCLEAR_VIC_INTERRUPT ( ( unsigned portLONG ) 0 )
\r
74 /* Constants required to handle critical sections. */
\r
75 #define portNO_CRITICAL_NESTING ( ( unsigned portLONG ) 0 )
\r
78 #define portINT_LEVEL_SENSITIVE 0
\r
79 #define portPIT_ENABLE ( ( unsigned portSHORT ) 0x1 << 24 )
\r
80 #define portPIT_INT_ENABLE ( ( unsigned portSHORT ) 0x1 << 25 )
\r
82 /* Constants required to setup the VIC for the tick ISR. */
\r
83 #define portTIMER_VIC_CHANNEL ( ( unsigned portLONG ) 0x0004 )
\r
84 #define portTIMER_VIC_CHANNEL_BIT ( ( unsigned portLONG ) 0x0010 )
\r
85 #define portTIMER_VIC_ENABLE ( ( unsigned portLONG ) 0x0020 )
\r
87 /*-----------------------------------------------------------*/
\r
89 /* Setup the PIT to generate the tick interrupts. */
\r
90 static void prvSetupTimerInterrupt( void );
\r
92 /* ulCriticalNesting will get set to zero when the first task starts. It
\r
93 cannot be initialised to 0 as this will cause interrupts to be enabled
\r
94 during the kernel initialisation process. */
\r
95 unsigned portLONG ulCriticalNesting = ( unsigned portLONG ) 9999;
\r
97 /*-----------------------------------------------------------*/
\r
100 * Initialise the stack of a task to look exactly as if a call to
\r
101 * portSAVE_CONTEXT had been called.
\r
103 * See header file for description.
\r
105 portSTACK_TYPE *pxPortInitialiseStack( portSTACK_TYPE *pxTopOfStack, pdTASK_CODE pxCode, void *pvParameters )
\r
107 portSTACK_TYPE *pxOriginalTOS;
\r
109 pxOriginalTOS = pxTopOfStack;
\r
111 /* Setup the initial stack of the task. The stack is set exactly as
\r
112 expected by the portRESTORE_CONTEXT() macro. */
\r
114 /* First on the stack is the return address - which in this case is the
\r
115 start of the task. The offset is added to make the return address appear
\r
116 as it would within an IRQ ISR. */
\r
117 *pxTopOfStack = ( portSTACK_TYPE ) pxCode + portINSTRUCTION_SIZE;
\r
120 *pxTopOfStack = ( portSTACK_TYPE ) 0xaaaaaaaa; /* R14 */
\r
122 *pxTopOfStack = ( portSTACK_TYPE ) pxOriginalTOS; /* Stack used when task starts goes in R13. */
\r
124 *pxTopOfStack = ( portSTACK_TYPE ) 0x12121212; /* R12 */
\r
126 *pxTopOfStack = ( portSTACK_TYPE ) 0x11111111; /* R11 */
\r
128 *pxTopOfStack = ( portSTACK_TYPE ) 0x10101010; /* R10 */
\r
130 *pxTopOfStack = ( portSTACK_TYPE ) 0x09090909; /* R9 */
\r
132 *pxTopOfStack = ( portSTACK_TYPE ) 0x08080808; /* R8 */
\r
134 *pxTopOfStack = ( portSTACK_TYPE ) 0x07070707; /* R7 */
\r
136 *pxTopOfStack = ( portSTACK_TYPE ) 0x06060606; /* R6 */
\r
138 *pxTopOfStack = ( portSTACK_TYPE ) 0x05050505; /* R5 */
\r
140 *pxTopOfStack = ( portSTACK_TYPE ) 0x04040404; /* R4 */
\r
142 *pxTopOfStack = ( portSTACK_TYPE ) 0x03030303; /* R3 */
\r
144 *pxTopOfStack = ( portSTACK_TYPE ) 0x02020202; /* R2 */
\r
146 *pxTopOfStack = ( portSTACK_TYPE ) 0x01010101; /* R1 */
\r
149 /* When the task starts is will expect to find the function parameter in
\r
151 *pxTopOfStack = ( portSTACK_TYPE ) pvParameters; /* R0 */
\r
154 /* The status register is set for system mode, with interrupts enabled. */
\r
155 *pxTopOfStack = ( portSTACK_TYPE ) portINITIAL_SPSR;
\r
158 /* Interrupt flags cannot always be stored on the stack and will
\r
159 instead be stored in a variable, which is then saved as part of the
\r
161 *pxTopOfStack = portNO_CRITICAL_NESTING;
\r
163 return pxTopOfStack;
\r
165 /*-----------------------------------------------------------*/
\r
167 portBASE_TYPE xPortStartScheduler( void )
\r
169 extern void vPortStartFirstTask( void );
\r
171 /* Start the timer that generates the tick ISR. Interrupts are disabled
\r
173 prvSetupTimerInterrupt();
\r
175 /* Start the first task. */
\r
176 vPortStartFirstTask();
\r
178 /* Should not get here! */
\r
181 /*-----------------------------------------------------------*/
\r
183 void vPortEndScheduler( void )
\r
185 /* It is unlikely that the ARM port will require this function as there
\r
186 is nothing to return to. */
\r
188 /*-----------------------------------------------------------*/
\r
190 #if configUSE_PREEMPTION == 0
\r
192 /* The cooperative scheduler requires a normal IRQ service routine to
\r
193 simply increment the system tick. */
\r
194 static __arm __irq void vPortNonPreemptiveTick( void );
\r
195 static __arm __irq void vPortNonPreemptiveTick( void )
\r
197 /* Increment the tick count - which may wake some tasks but as the
\r
198 preemptive scheduler is not being used any woken task is not given
\r
199 processor time no matter what its priority. */
\r
200 vTaskIncrementTick();
\r
202 /* Ready for the next interrupt. */
\r
203 T0IR = portTIMER_MATCH_ISR_BIT;
\r
204 VICVectAddr = portCLEAR_VIC_INTERRUPT;
\r
209 /* This function is called from an asm wrapper, so does not require the __irq
\r
211 void vPortPreemptiveTick( void );
\r
212 void vPortPreemptiveTick( void )
\r
214 /* Increment the tick counter. */
\r
215 vTaskIncrementTick();
\r
217 /* The new tick value might unblock a task. Ensure the highest task that
\r
218 is ready to execute is the task that will execute when the tick ISR
\r
220 vTaskSwitchContext();
\r
222 /* Ready for the next interrupt. */
\r
223 T0IR = portTIMER_MATCH_ISR_BIT;
\r
224 VICVectAddr = portCLEAR_VIC_INTERRUPT;
\r
229 /*-----------------------------------------------------------*/
\r
231 static void prvSetupTimerInterrupt( void )
\r
233 unsigned portLONG ulCompareMatch;
\r
235 /* A 1ms tick does not require the use of the timer prescale. This is
\r
236 defaulted to zero but can be used if necessary. */
\r
237 T0PR = portPRESCALE_VALUE;
\r
239 /* Calculate the match value required for our wanted tick rate. */
\r
240 ulCompareMatch = configCPU_CLOCK_HZ / configTICK_RATE_HZ;
\r
242 /* Protect against divide by zero. Using an if() statement still results
\r
243 in a warning - hence the #if. */
\r
244 #if portPRESCALE_VALUE != 0
\r
246 ulCompareMatch /= ( portPRESCALE_VALUE + 1 );
\r
250 T0MR0 = ulCompareMatch;
\r
252 /* Generate tick with timer 0 compare match. */
\r
253 T0MCR = portRESET_COUNT_ON_MATCH | portINTERRUPT_ON_MATCH;
\r
255 /* Setup the VIC for the timer. */
\r
256 VICIntSelect &= ~( portTIMER_VIC_CHANNEL_BIT );
\r
257 VICIntEnable |= portTIMER_VIC_CHANNEL_BIT;
\r
259 /* The ISR installed depends on whether the preemptive or cooperative
\r
260 scheduler is being used. */
\r
261 #if configUSE_PREEMPTION == 1
\r
263 extern void ( vPortPreemptiveTickEntry )( void );
\r
265 VICVectAddr0 = ( unsigned portLONG ) vPortPreemptiveTickEntry;
\r
269 extern void ( vNonPreemptiveTick )( void );
\r
271 VICVectAddr0 = ( portLONG ) vPortNonPreemptiveTick;
\r
275 VICVectCntl0 = portTIMER_VIC_CHANNEL | portTIMER_VIC_ENABLE;
\r
277 /* Start the timer - interrupts are disabled when this function is called
\r
278 so it is okay to do this here. */
\r
279 T0TCR = portENABLE_TIMER;
\r
281 /*-----------------------------------------------------------*/
\r
283 void vPortEnterCritical( void )
\r
285 /* Disable interrupts first! */
\r
286 __disable_interrupt();
\r
288 /* Now interrupts are disabled ulCriticalNesting can be accessed
\r
289 directly. Increment ulCriticalNesting to keep a count of how many times
\r
290 portENTER_CRITICAL() has been called. */
\r
291 ulCriticalNesting++;
\r
293 /*-----------------------------------------------------------*/
\r
295 void vPortExitCritical( void )
\r
297 if( ulCriticalNesting > portNO_CRITICAL_NESTING )
\r
299 /* Decrement the nesting count as we are leaving a critical section. */
\r
300 ulCriticalNesting--;
\r
302 /* If the nesting level has reached zero then interrupts should be
\r
304 if( ulCriticalNesting == portNO_CRITICAL_NESTING )
\r
306 __enable_interrupt();
\r
310 /*-----------------------------------------------------------*/
\r