2 FreeRTOS.org V5.3.0 - Copyright (C) 2003-2009 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 it
\r
7 under the terms of the GNU General Public License (version 2) as published
\r
8 by the Free Software Foundation and modified by the FreeRTOS exception.
\r
9 **NOTE** The exception to the GPL is included to allow you to distribute a
\r
10 combined work that includes FreeRTOS.org without being obliged to provide
\r
11 the source code for any proprietary components. Alternative commercial
\r
12 license and support terms are also available upon request. See the
\r
13 licensing section of http://www.FreeRTOS.org for full details.
\r
15 FreeRTOS.org is distributed in the hope that it will be useful, but WITHOUT
\r
16 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
\r
17 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
\r
20 You should have received a copy of the GNU General Public License along
\r
21 with FreeRTOS.org; if not, write to the Free Software Foundation, Inc., 59
\r
22 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
\r
25 ***************************************************************************
\r
27 * Get the FreeRTOS eBook! See http://www.FreeRTOS.org/Documentation *
\r
29 * This is a concise, step by step, 'hands on' guide that describes both *
\r
30 * general multitasking concepts and FreeRTOS specifics. It presents and *
\r
31 * explains numerous examples that are written using the FreeRTOS API. *
\r
32 * Full source code for all the examples is provided in an accompanying *
\r
35 ***************************************************************************
\r
39 Please ensure to read the configuration and relevant port sections of the
\r
40 online documentation.
\r
42 http://www.FreeRTOS.org - Documentation, latest information, license and
\r
45 http://www.SafeRTOS.com - A version that is certified for use in safety
\r
48 http://www.OpenRTOS.com - Commercial support, development, porting,
\r
49 licensing and training services.
\r
53 /*-----------------------------------------------------------
\r
54 * Implementation of functions defined in portable.h for the ARM7 port.
\r
56 * Components that can be compiled to either ARM or THUMB mode are
\r
57 * contained in this file. The ISR routines, which can only be compiled
\r
58 * to ARM mode are contained in portISR.c.
\r
59 *----------------------------------------------------------*/
\r
62 /* Standard includes. */
\r
65 /* Scheduler includes. */
\r
66 #include "FreeRTOS.h"
\r
69 /* Constants required to setup the task context. */
\r
70 #define portINITIAL_SPSR ( ( portSTACK_TYPE ) 0x1f ) /* System mode, ARM mode, interrupts enabled. */
\r
71 #define portTHUMB_MODE_BIT ( ( portSTACK_TYPE ) 0x20 )
\r
72 #define portINSTRUCTION_SIZE ( ( portSTACK_TYPE ) 4 )
\r
73 #define portNO_CRITICAL_SECTION_NESTING ( ( portSTACK_TYPE ) 0 )
\r
75 /* Constants required to setup the tick ISR. */
\r
76 #define portENABLE_TIMER ( ( unsigned portCHAR ) 0x01 )
\r
77 #define portPRESCALE_VALUE 0x00
\r
78 #define portINTERRUPT_ON_MATCH ( ( unsigned portLONG ) 0x01 )
\r
79 #define portRESET_COUNT_ON_MATCH ( ( unsigned portLONG ) 0x02 )
\r
81 /* Constants required to setup the VIC for the tick ISR. */
\r
82 #define portTIMER_VIC_CHANNEL ( ( unsigned portLONG ) 0x0004 )
\r
83 #define portTIMER_VIC_CHANNEL_BIT ( ( unsigned portLONG ) 0x0010 )
\r
84 #define portTIMER_VIC_ENABLE ( ( unsigned portLONG ) 0x0020 )
\r
86 /*-----------------------------------------------------------*/
\r
88 /* Setup the timer to generate the tick interrupts. */
\r
89 static void prvSetupTimerInterrupt( void );
\r
92 * The scheduler can only be started from ARM mode, so
\r
93 * vPortISRStartFirstSTask() is defined in portISR.c.
\r
95 extern void vPortISRStartFirstTask( void );
\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 ) 0x00000000; /* 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 last thing onto the stack is the status register, which is set for
\r
155 system mode, with interrupts enabled. */
\r
156 *pxTopOfStack = ( portSTACK_TYPE ) portINITIAL_SPSR;
\r
158 #ifdef THUMB_INTERWORK
\r
160 /* We want the task to start in thumb mode. */
\r
161 *pxTopOfStack |= portTHUMB_MODE_BIT;
\r
167 /* Some optimisation levels use the stack differently to others. This
\r
168 means the interrupt flags cannot always be stored on the stack and will
\r
169 instead be stored in a variable, which is then saved as part of the
\r
171 *pxTopOfStack = portNO_CRITICAL_SECTION_NESTING;
\r
173 return pxTopOfStack;
\r
175 /*-----------------------------------------------------------*/
\r
177 portBASE_TYPE xPortStartScheduler( void )
\r
179 /* Start the timer that generates the tick ISR. Interrupts are disabled
\r
181 prvSetupTimerInterrupt();
\r
183 /* Start the first task. */
\r
184 vPortISRStartFirstTask();
\r
186 /* Should not get here! */
\r
189 /*-----------------------------------------------------------*/
\r
191 void vPortEndScheduler( void )
\r
193 /* It is unlikely that the ARM port will require this function as there
\r
194 is nothing to return to. */
\r
196 /*-----------------------------------------------------------*/
\r
199 * Setup the timer 0 to generate the tick interrupts at the required frequency.
\r
201 static void prvSetupTimerInterrupt( void )
\r
203 unsigned portLONG ulCompareMatch;
\r
205 PCLKSEL0 = (PCLKSEL0 & (~(0x3<<2))) | (0x01 << 2);
\r
206 T0TCR = 2; /* Stop and reset the timer */
\r
207 T0CTCR = 0; /* Timer mode */
\r
209 /* A 1ms tick does not require the use of the timer prescale. This is
\r
210 defaulted to zero but can be used if necessary. */
\r
211 T0PR = portPRESCALE_VALUE;
\r
213 /* Calculate the match value required for our wanted tick rate. */
\r
214 ulCompareMatch = configCPU_CLOCK_HZ / configTICK_RATE_HZ;
\r
216 /* Protect against divide by zero. Using an if() statement still results
\r
217 in a warning - hence the #if. */
\r
218 #if portPRESCALE_VALUE != 0
\r
220 ulCompareMatch /= ( portPRESCALE_VALUE + 1 );
\r
223 T0MR1 = ulCompareMatch;
\r
225 /* Generate tick with timer 0 compare match. */
\r
226 T0MCR = (3 << 3); /* Reset timer on match and generate interrupt */
\r
228 /* Setup the VIC for the timer. */
\r
229 VICIntEnable = 0x00000010;
\r
231 /* The ISR installed depends on whether the preemptive or cooperative
\r
232 scheduler is being used. */
\r
233 #if configUSE_PREEMPTION == 1
\r
235 extern void ( vPreemptiveTick )( void );
\r
236 VICVectAddr4 = ( portLONG ) vPreemptiveTick;
\r
240 extern void ( vNonPreemptiveTick )( void );
\r
241 VICVectAddr4 = ( portLONG ) vNonPreemptiveTick;
\r
247 /* Start the timer - interrupts are disabled when this function is called
\r
248 so it is okay to do this here. */
\r
249 T0TCR = portENABLE_TIMER;
\r
251 /*-----------------------------------------------------------*/
\r