2 FreeRTOS.org V4.7.1 - Copyright (C) 2003-2008 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
28 Please ensure to read the configuration and relevant port sections of the
\r
29 online documentation.
\r
31 +++ http://www.FreeRTOS.org +++
\r
32 Documentation, latest information, license and contact details.
\r
34 +++ http://www.SafeRTOS.com +++
\r
35 A version that is certified for use in safety critical systems.
\r
37 +++ http://www.OpenRTOS.com +++
\r
38 Commercial support, development, porting, licensing and training services.
\r
40 ***************************************************************************
\r
43 /*-----------------------------------------------------------
\r
44 * Implementation of functions defined in portable.h for the Philips ARM7 port.
\r
45 *----------------------------------------------------------*/
\r
50 + Bug fix - The prescale value for the timer setup is now written to T0PR
\r
51 instead of T0PC. This bug would have had no effect unless a prescale
\r
52 value was actually used.
\r
55 /* Standard includes. */
\r
57 #include <intrinsics.h>
\r
59 /* Scheduler includes. */
\r
60 #include "FreeRTOS.h"
\r
63 /* Constants required to setup the tick ISR. */
\r
64 #define portENABLE_TIMER ( ( unsigned portCHAR ) 0x01 )
\r
65 #define portPRESCALE_VALUE 0x00
\r
66 #define portINTERRUPT_ON_MATCH ( ( unsigned portLONG ) 0x01 )
\r
67 #define portRESET_COUNT_ON_MATCH ( ( unsigned portLONG ) 0x02 )
\r
69 /* Constants required to setup the initial stack. */
\r
70 #define portINITIAL_SPSR ( ( portSTACK_TYPE ) 0x3f ) /* System mode, THUMB mode, interrupts enabled. */
\r
71 #define portINSTRUCTION_SIZE ( ( portSTACK_TYPE ) 4 )
\r
73 /* Constants required to setup the PIT. */
\r
74 #define portPIT_CLOCK_DIVISOR ( ( unsigned portLONG ) 16 )
\r
75 #define portPIT_COUNTER_VALUE ( ( ( configCPU_CLOCK_HZ / portPIT_CLOCK_DIVISOR ) / 1000UL ) * portTICK_RATE_MS )
\r
77 /* Constants required to handle interrupts. */
\r
78 #define portTIMER_MATCH_ISR_BIT ( ( unsigned portCHAR ) 0x01 )
\r
79 #define portCLEAR_VIC_INTERRUPT ( ( unsigned portLONG ) 0 )
\r
81 /* Constants required to handle critical sections. */
\r
82 #define portNO_CRITICAL_NESTING ( ( unsigned portLONG ) 0 )
\r
85 #define portINT_LEVEL_SENSITIVE 0
\r
86 #define portPIT_ENABLE ( ( unsigned portSHORT ) 0x1 << 24 )
\r
87 #define portPIT_INT_ENABLE ( ( unsigned portSHORT ) 0x1 << 25 )
\r
89 /* Constants required to setup the VIC for the tick ISR. */
\r
90 #define portTIMER_VIC_CHANNEL ( ( unsigned portLONG ) 0x0004 )
\r
91 #define portTIMER_VIC_CHANNEL_BIT ( ( unsigned portLONG ) 0x0010 )
\r
92 #define portTIMER_VIC_ENABLE ( ( unsigned portLONG ) 0x0020 )
\r
94 /*-----------------------------------------------------------*/
\r
96 /* Setup the PIT to generate the tick interrupts. */
\r
97 static void prvSetupTimerInterrupt( void );
\r
99 /* ulCriticalNesting will get set to zero when the first task starts. It
\r
100 cannot be initialised to 0 as this will cause interrupts to be enabled
\r
101 during the kernel initialisation process. */
\r
102 unsigned portLONG ulCriticalNesting = ( unsigned portLONG ) 9999;
\r
104 /*-----------------------------------------------------------*/
\r
107 * Initialise the stack of a task to look exactly as if a call to
\r
108 * portSAVE_CONTEXT had been called.
\r
110 * See header file for description.
\r
112 portSTACK_TYPE *pxPortInitialiseStack( portSTACK_TYPE *pxTopOfStack, pdTASK_CODE pxCode, void *pvParameters )
\r
114 portSTACK_TYPE *pxOriginalTOS;
\r
116 pxOriginalTOS = pxTopOfStack;
\r
118 /* Setup the initial stack of the task. The stack is set exactly as
\r
119 expected by the portRESTORE_CONTEXT() macro. */
\r
121 /* First on the stack is the return address - which in this case is the
\r
122 start of the task. The offset is added to make the return address appear
\r
123 as it would within an IRQ ISR. */
\r
124 *pxTopOfStack = ( portSTACK_TYPE ) pxCode + portINSTRUCTION_SIZE;
\r
127 *pxTopOfStack = ( portSTACK_TYPE ) 0xaaaaaaaa; /* R14 */
\r
129 *pxTopOfStack = ( portSTACK_TYPE ) pxOriginalTOS; /* Stack used when task starts goes in R13. */
\r
131 *pxTopOfStack = ( portSTACK_TYPE ) 0x12121212; /* R12 */
\r
133 *pxTopOfStack = ( portSTACK_TYPE ) 0x11111111; /* R11 */
\r
135 *pxTopOfStack = ( portSTACK_TYPE ) 0x10101010; /* R10 */
\r
137 *pxTopOfStack = ( portSTACK_TYPE ) 0x09090909; /* R9 */
\r
139 *pxTopOfStack = ( portSTACK_TYPE ) 0x08080808; /* R8 */
\r
141 *pxTopOfStack = ( portSTACK_TYPE ) 0x07070707; /* R7 */
\r
143 *pxTopOfStack = ( portSTACK_TYPE ) 0x06060606; /* R6 */
\r
145 *pxTopOfStack = ( portSTACK_TYPE ) 0x05050505; /* R5 */
\r
147 *pxTopOfStack = ( portSTACK_TYPE ) 0x04040404; /* R4 */
\r
149 *pxTopOfStack = ( portSTACK_TYPE ) 0x03030303; /* R3 */
\r
151 *pxTopOfStack = ( portSTACK_TYPE ) 0x02020202; /* R2 */
\r
153 *pxTopOfStack = ( portSTACK_TYPE ) 0x01010101; /* R1 */
\r
156 /* When the task starts is will expect to find the function parameter in
\r
158 *pxTopOfStack = ( portSTACK_TYPE ) pvParameters; /* R0 */
\r
161 /* The status register is set for system mode, with interrupts enabled. */
\r
162 *pxTopOfStack = ( portSTACK_TYPE ) portINITIAL_SPSR;
\r
165 /* Interrupt flags cannot always be stored on the stack and will
\r
166 instead be stored in a variable, which is then saved as part of the
\r
168 *pxTopOfStack = portNO_CRITICAL_NESTING;
\r
170 return pxTopOfStack;
\r
172 /*-----------------------------------------------------------*/
\r
174 portBASE_TYPE xPortStartScheduler( void )
\r
176 extern void vPortStartFirstTask( void );
\r
178 /* Start the timer that generates the tick ISR. Interrupts are disabled
\r
180 prvSetupTimerInterrupt();
\r
182 /* Start the first task. */
\r
183 vPortStartFirstTask();
\r
185 /* Should not get here! */
\r
188 /*-----------------------------------------------------------*/
\r
190 void vPortEndScheduler( void )
\r
192 /* It is unlikely that the ARM port will require this function as there
\r
193 is nothing to return to. */
\r
195 /*-----------------------------------------------------------*/
\r
197 #if configUSE_PREEMPTION == 0
\r
199 /* The cooperative scheduler requires a normal IRQ service routine to
\r
200 simply increment the system tick. */
\r
201 static __arm __irq void vPortNonPreemptiveTick( void );
\r
202 static __arm __irq void vPortNonPreemptiveTick( void )
\r
204 /* Increment the tick count - which may wake some tasks but as the
\r
205 preemptive scheduler is not being used any woken task is not given
\r
206 processor time no matter what its priority. */
\r
207 vTaskIncrementTick();
\r
209 /* Ready for the next interrupt. */
\r
210 T0IR = portTIMER_MATCH_ISR_BIT;
\r
211 VICVectAddr = portCLEAR_VIC_INTERRUPT;
\r
216 /* This function is called from an asm wrapper, so does not require the __irq
\r
218 void vPortPreemptiveTick( void );
\r
219 void vPortPreemptiveTick( void )
\r
221 /* Increment the tick counter. */
\r
222 vTaskIncrementTick();
\r
224 /* The new tick value might unblock a task. Ensure the highest task that
\r
225 is ready to execute is the task that will execute when the tick ISR
\r
227 vTaskSwitchContext();
\r
229 /* Ready for the next interrupt. */
\r
230 T0IR = portTIMER_MATCH_ISR_BIT;
\r
231 VICVectAddr = portCLEAR_VIC_INTERRUPT;
\r
236 /*-----------------------------------------------------------*/
\r
238 static void prvSetupTimerInterrupt( void )
\r
240 unsigned portLONG ulCompareMatch;
\r
242 /* A 1ms tick does not require the use of the timer prescale. This is
\r
243 defaulted to zero but can be used if necessary. */
\r
244 T0PR = portPRESCALE_VALUE;
\r
246 /* Calculate the match value required for our wanted tick rate. */
\r
247 ulCompareMatch = configCPU_CLOCK_HZ / configTICK_RATE_HZ;
\r
249 /* Protect against divide by zero. Using an if() statement still results
\r
250 in a warning - hence the #if. */
\r
251 #if portPRESCALE_VALUE != 0
\r
253 ulCompareMatch /= ( portPRESCALE_VALUE + 1 );
\r
257 T0MR0 = ulCompareMatch;
\r
259 /* Generate tick with timer 0 compare match. */
\r
260 T0MCR = portRESET_COUNT_ON_MATCH | portINTERRUPT_ON_MATCH;
\r
262 /* Setup the VIC for the timer. */
\r
263 VICIntSelect &= ~( portTIMER_VIC_CHANNEL_BIT );
\r
264 VICIntEnable |= portTIMER_VIC_CHANNEL_BIT;
\r
266 /* The ISR installed depends on whether the preemptive or cooperative
\r
267 scheduler is being used. */
\r
268 #if configUSE_PREEMPTION == 1
\r
270 extern void ( vPortPreemptiveTickEntry )( void );
\r
272 VICVectAddr0 = ( unsigned portLONG ) vPortPreemptiveTickEntry;
\r
276 extern void ( vNonPreemptiveTick )( void );
\r
278 VICVectAddr0 = ( portLONG ) vPortNonPreemptiveTick;
\r
282 VICVectCntl0 = portTIMER_VIC_CHANNEL | portTIMER_VIC_ENABLE;
\r
284 /* Start the timer - interrupts are disabled when this function is called
\r
285 so it is okay to do this here. */
\r
286 T0TCR = portENABLE_TIMER;
\r
288 /*-----------------------------------------------------------*/
\r
290 void vPortEnterCritical( void )
\r
292 /* Disable interrupts first! */
\r
293 __disable_interrupt();
\r
295 /* Now interrupts are disabled ulCriticalNesting can be accessed
\r
296 directly. Increment ulCriticalNesting to keep a count of how many times
\r
297 portENTER_CRITICAL() has been called. */
\r
298 ulCriticalNesting++;
\r
300 /*-----------------------------------------------------------*/
\r
302 void vPortExitCritical( void )
\r
304 if( ulCriticalNesting > portNO_CRITICAL_NESTING )
\r
306 /* Decrement the nesting count as we are leaving a critical section. */
\r
307 ulCriticalNesting--;
\r
309 /* If the nesting level has reached zero then interrupts should be
\r
311 if( ulCriticalNesting == portNO_CRITICAL_NESTING )
\r
313 __enable_interrupt();
\r
317 /*-----------------------------------------------------------*/
\r