2 FreeRTOS V7.0.1 - Copyright (C) 2011 Real Time Engineers Ltd.
\r
5 ***************************************************************************
\r
7 * FreeRTOS tutorial books are available in pdf and paperback. *
\r
8 * Complete, revised, and edited pdf reference manuals are also *
\r
11 * Purchasing FreeRTOS documentation will not only help you, by *
\r
12 * ensuring you get running as quickly as possible and with an *
\r
13 * in-depth knowledge of how to use FreeRTOS, it will also help *
\r
14 * the FreeRTOS project to continue with its mission of providing *
\r
15 * professional grade, cross platform, de facto standard solutions *
\r
16 * for microcontrollers - completely free of charge! *
\r
18 * >>> See http://www.FreeRTOS.org/Documentation for details. <<< *
\r
20 * Thank you for using FreeRTOS, and thank you for your support! *
\r
22 ***************************************************************************
\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 modification to the GPL is included to allow you to
\r
31 distribute a combined work that includes FreeRTOS without being obliged to
\r
32 provide the source code for proprietary components outside of the FreeRTOS
\r
33 kernel. FreeRTOS is distributed in the hope that it will be useful, but
\r
34 WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
\r
35 or 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
55 /* Standard includes. */
\r
58 /* Scheduler includes. */
\r
59 #include "FreeRTOS.h"
\r
62 /* Constants required to setup the initial task context. */
\r
63 #define portINITIAL_SPSR ( ( portSTACK_TYPE ) 0x1f ) /* System mode, ARM mode, interrupts enabled. */
\r
64 #define portTHUMB_MODE_BIT ( ( portSTACK_TYPE ) 0x20 )
\r
65 #define portINSTRUCTION_SIZE ( ( portSTACK_TYPE ) 4 )
\r
66 #define portNO_CRITICAL_SECTION_NESTING ( ( portSTACK_TYPE ) 0 )
\r
68 /* Constants required to setup the tick ISR. */
\r
69 #define portENABLE_TIMER ( ( unsigned portCHAR ) 0x01 )
\r
70 #define portPRESCALE_VALUE 0x00
\r
71 #define portINTERRUPT_ON_MATCH ( ( unsigned portLONG ) 0x01 )
\r
72 #define portRESET_COUNT_ON_MATCH ( ( unsigned portLONG ) 0x02 )
\r
74 /* Constants required to setup the VIC for the tick ISR. */
\r
75 #define portTIMER_VIC_CHANNEL ( ( unsigned portLONG ) 0x0004 )
\r
76 #define portTIMER_VIC_CHANNEL_BIT ( ( unsigned portLONG ) 0x0010 )
\r
77 #define portTIMER_VIC_ENABLE ( ( unsigned portLONG ) 0x0020 )
\r
79 /* Constants required to handle interrupts. */
\r
80 #define portTIMER_MATCH_ISR_BIT ( ( unsigned portCHAR ) 0x01 )
\r
81 #define portCLEAR_VIC_INTERRUPT ( ( unsigned portLONG ) 0 )
\r
83 /*-----------------------------------------------------------*/
\r
85 /* The code generated by the Keil compiler does not maintain separate
\r
86 stack and frame pointers. The portENTER_CRITICAL macro cannot therefore
\r
87 use the stack as per other ports. Instead a variable is used to keep
\r
88 track of the critical section nesting. This variable has to be stored
\r
89 as part of the task context and must be initialised to a non zero value. */
\r
91 #define portNO_CRITICAL_NESTING ( ( unsigned portLONG ) 0 )
\r
92 volatile unsigned portLONG ulCriticalNesting = 9999UL;
\r
94 /*-----------------------------------------------------------*/
\r
96 /* Setup the timer to generate the tick interrupts. */
\r
97 static void prvSetupTimerInterrupt( void );
\r
100 * The scheduler can only be started from ARM mode, so
\r
101 * vPortStartFirstSTask() is defined in portISR.c.
\r
103 extern __asm void vPortStartFirstTask( void );
\r
105 /*-----------------------------------------------------------*/
\r
108 * See header file for description.
\r
110 portSTACK_TYPE *pxPortInitialiseStack( portSTACK_TYPE *pxTopOfStack, pdTASK_CODE pxCode, void *pvParameters )
\r
112 portSTACK_TYPE *pxOriginalTOS;
\r
114 /* Setup the initial stack of the task. The stack is set exactly as
\r
115 expected by the portRESTORE_CONTEXT() macro.
\r
117 Remember where the top of the (simulated) stack is before we place
\r
119 pxOriginalTOS = pxTopOfStack;
\r
121 /* To ensure asserts in tasks.c don't fail, although in this case the assert
\r
122 is not really required. */
\r
125 /* First on the stack is the return address - which in this case is the
\r
126 start of the task. The offset is added to make the return address appear
\r
127 as it would within an IRQ ISR. */
\r
128 *pxTopOfStack = ( portSTACK_TYPE ) pxCode + portINSTRUCTION_SIZE;
\r
131 *pxTopOfStack = ( portSTACK_TYPE ) 0xaaaaaaaa; /* R14 */
\r
133 *pxTopOfStack = ( portSTACK_TYPE ) pxOriginalTOS; /* Stack used when task starts goes in R13. */
\r
135 *pxTopOfStack = ( portSTACK_TYPE ) 0x12121212; /* R12 */
\r
137 *pxTopOfStack = ( portSTACK_TYPE ) 0x11111111; /* R11 */
\r
139 *pxTopOfStack = ( portSTACK_TYPE ) 0x10101010; /* R10 */
\r
141 *pxTopOfStack = ( portSTACK_TYPE ) 0x09090909; /* R9 */
\r
143 *pxTopOfStack = ( portSTACK_TYPE ) 0x08080808; /* R8 */
\r
145 *pxTopOfStack = ( portSTACK_TYPE ) 0x07070707; /* R7 */
\r
147 *pxTopOfStack = ( portSTACK_TYPE ) 0x06060606; /* R6 */
\r
149 *pxTopOfStack = ( portSTACK_TYPE ) 0x05050505; /* R5 */
\r
151 *pxTopOfStack = ( portSTACK_TYPE ) 0x04040404; /* R4 */
\r
153 *pxTopOfStack = ( portSTACK_TYPE ) 0x03030303; /* R3 */
\r
155 *pxTopOfStack = ( portSTACK_TYPE ) 0x02020202; /* R2 */
\r
157 *pxTopOfStack = ( portSTACK_TYPE ) 0x01010101; /* R1 */
\r
159 *pxTopOfStack = ( portSTACK_TYPE ) pvParameters; /* R0 */
\r
162 /* The last thing onto the stack is the status register, which is set for
\r
163 system mode, with interrupts enabled. */
\r
164 *pxTopOfStack = ( portSTACK_TYPE ) portINITIAL_SPSR;
\r
166 if( ( ( unsigned long ) pxCode & 0x01UL ) != 0x00UL )
\r
168 /* We want the task to start in thumb mode. */
\r
169 *pxTopOfStack |= portTHUMB_MODE_BIT;
\r
174 /* The code generated by the Keil compiler does not maintain separate
\r
175 stack and frame pointers. The portENTER_CRITICAL macro cannot therefore
\r
176 use the stack as per other ports. Instead a variable is used to keep
\r
177 track of the critical section nesting. This variable has to be stored
\r
178 as part of the task context and is initially set to zero. */
\r
179 *pxTopOfStack = portNO_CRITICAL_SECTION_NESTING;
\r
181 return pxTopOfStack;
\r
183 /*-----------------------------------------------------------*/
\r
185 portBASE_TYPE xPortStartScheduler( void )
\r
187 /* Start the timer that generates the tick ISR. */
\r
188 prvSetupTimerInterrupt();
\r
190 /* Start the first task. This is done from portISR.c as ARM mode must be
\r
192 vPortStartFirstTask();
\r
194 /* Should not get here! */
\r
197 /*-----------------------------------------------------------*/
\r
199 void vPortEndScheduler( void )
\r
201 /* It is unlikely that the ARM port will require this function as there
\r
202 is nothing to return to. If this is required - stop the tick ISR then
\r
203 return back to main. */
\r
205 /*-----------------------------------------------------------*/
\r
207 #if configUSE_PREEMPTION == 0
\r
210 * The cooperative scheduler requires a normal IRQ service routine to
\r
211 * simply increment the system tick.
\r
213 void vNonPreemptiveTick( void ) __irq;
\r
214 void vNonPreemptiveTick( void ) __irq
\r
216 /* Increment the tick count - this may make a delaying task ready
\r
217 to run - but a context switch is not performed. */
\r
218 vTaskIncrementTick();
\r
220 T0IR = portTIMER_MATCH_ISR_BIT; /* Clear the timer event */
\r
221 VICVectAddr = portCLEAR_VIC_INTERRUPT; /* Acknowledge the Interrupt */
\r
227 **************************************************************************
\r
228 * The preemptive scheduler ISR is written in assembler and can be found
\r
229 * in the portASM.s file. This will only get used if portUSE_PREEMPTION
\r
230 * is set to 1 in portmacro.h
\r
231 **************************************************************************
\r
234 void vPreemptiveTick( void );
\r
237 /*-----------------------------------------------------------*/
\r
239 static void prvSetupTimerInterrupt( void )
\r
241 unsigned portLONG ulCompareMatch;
\r
243 /* A 1ms tick does not require the use of the timer prescale. This is
\r
244 defaulted to zero but can be used if necessary. */
\r
245 T0PR = portPRESCALE_VALUE;
\r
247 /* Calculate the match value required for our wanted tick rate. */
\r
248 ulCompareMatch = configCPU_CLOCK_HZ / configTICK_RATE_HZ;
\r
250 /* Protect against divide by zero. Using an if() statement still results
\r
251 in a warning - hence the #if. */
\r
252 #if portPRESCALE_VALUE != 0
\r
254 ulCompareMatch /= ( portPRESCALE_VALUE + 1 );
\r
258 T0MR0 = ulCompareMatch;
\r
260 /* Generate tick with timer 0 compare match. */
\r
261 T0MCR = portRESET_COUNT_ON_MATCH | portINTERRUPT_ON_MATCH;
\r
263 /* Setup the VIC for the timer. */
\r
264 VICIntSelect &= ~( portTIMER_VIC_CHANNEL_BIT );
\r
265 VICIntEnable |= portTIMER_VIC_CHANNEL_BIT;
\r
267 /* The ISR installed depends on whether the preemptive or cooperative
\r
268 scheduler is being used. */
\r
269 #if configUSE_PREEMPTION == 1
\r
271 VICVectAddr0 = ( unsigned portLONG ) vPreemptiveTick;
\r
275 VICVectAddr0 = ( unsigned portLONG ) vNonPreemptiveTick;
\r
279 VICVectCntl0 = portTIMER_VIC_CHANNEL | portTIMER_VIC_ENABLE;
\r
281 /* Start the timer - interrupts are disabled when this function is called
\r
282 so it is okay to do this here. */
\r
283 T0TCR = portENABLE_TIMER;
\r
285 /*-----------------------------------------------------------*/
\r
287 void vPortEnterCritical( void )
\r
289 /* Disable interrupts as per portDISABLE_INTERRUPTS(); */
\r
292 /* Now interrupts are disabled ulCriticalNesting can be accessed
\r
293 directly. Increment ulCriticalNesting to keep a count of how many times
\r
294 portENTER_CRITICAL() has been called. */
\r
295 ulCriticalNesting++;
\r
297 /*-----------------------------------------------------------*/
\r
299 void vPortExitCritical( void )
\r
301 if( ulCriticalNesting > portNO_CRITICAL_NESTING )
\r
303 /* Decrement the nesting count as we are leaving a critical section. */
\r
304 ulCriticalNesting--;
\r
306 /* If the nesting level has reached zero then interrupts should be
\r
308 if( ulCriticalNesting == portNO_CRITICAL_NESTING )
\r
310 /* Enable interrupts as per portEXIT_CRITICAL(). */
\r
315 /*-----------------------------------------------------------*/
\r