2 FreeRTOS V7.2.0 - Copyright (C) 2012 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 ***************************************************************************
\r
46 * Having a problem? Start by reading the FAQ "My application does *
\r
47 * not run, what could be wrong? *
\r
49 * http://www.FreeRTOS.org/FAQHelp.html *
\r
51 ***************************************************************************
\r
54 http://www.FreeRTOS.org - Documentation, training, latest information,
\r
55 license and contact details.
\r
57 http://www.FreeRTOS.org/plus - A selection of FreeRTOS ecosystem products,
\r
58 including FreeRTOS+Trace - an indispensable productivity tool.
\r
60 Real Time Engineers ltd license FreeRTOS to High Integrity Systems, who sell
\r
61 the code with commercial support, indemnification, and middleware, under
\r
62 the OpenRTOS brand: http://www.OpenRTOS.com. High Integrity Systems also
\r
63 provide a safety engineered and independently SIL3 certified version under
\r
64 the SafeRTOS brand: http://www.SafeRTOS.com.
\r
67 /* FreeRTOS includes. */
\r
68 #include "FreeRTOS.h"
\r
71 /*-----------------------------------------------------------*/
\r
73 /* Count of the critical section nesting depth. */
\r
74 unsigned portLONG ulCriticalNesting = 9999;
\r
76 /*-----------------------------------------------------------*/
\r
78 /* Registers required to configure the RTI. */
\r
79 #define portRTI_GCTRL_REG ( * ( ( volatile unsigned long * ) 0xFFFFFC00 ) )
\r
80 #define portRTI_TBCTRL_REG ( * ( ( volatile unsigned long * ) 0xFFFFFC04 ) )
\r
81 #define portRTI_COMPCTRL_REG ( * ( ( volatile unsigned long * ) 0xFFFFFC0C ) )
\r
82 #define portRTI_CNT0_FRC0_REG ( * ( ( volatile unsigned long * ) 0xFFFFFC10 ) )
\r
83 #define portRTI_CNT0_UC0_REG ( * ( ( volatile unsigned long * ) 0xFFFFFC14 ) )
\r
84 #define portRTI_CNT0_CPUC0_REG ( * ( ( volatile unsigned long * ) 0xFFFFFC18 ) )
\r
85 #define portRTI_CNT0_COMP0_REG ( * ( ( volatile unsigned long * ) 0xFFFFFC50 ) )
\r
86 #define portRTI_CNT0_UDCP0_REG ( * ( ( volatile unsigned long * ) 0xFFFFFC54 ) )
\r
87 #define portRTI_SETINTENA_REG ( * ( ( volatile unsigned long * ) 0xFFFFFC80 ) )
\r
88 #define portRTI_CLEARINTENA_REG ( * ( ( volatile unsigned long * ) 0xFFFFFC84 ) )
\r
89 #define portRTI_INTFLAG_REG ( * ( ( volatile unsigned long * ) 0xFFFFFC88 ) )
\r
92 /* Constants required to set up the initial stack of each task. */
\r
93 #define portINITIAL_SPSR ( ( portSTACK_TYPE ) 0x1F )
\r
94 #define portINITIAL_FPSCR ( ( portSTACK_TYPE ) 0x00 )
\r
95 #define portINSTRUCTION_SIZE ( ( portSTACK_TYPE ) 0x04 )
\r
96 #define portTHUMB_MODE_BIT ( ( portSTACK_TYPE ) 0x20 )
\r
98 /* The number of words on the stack frame between the saved Top Of Stack and
\r
99 R0 (in which the parameters are passed. */
\r
100 #define portSPACE_BETWEEN_TOS_AND_PARAMETERS ( 12 )
\r
102 /*-----------------------------------------------------------*/
\r
104 /* vPortStartFirstSTask() is defined in portASM.asm */
\r
105 extern void vPortStartFirstTask( void );
\r
107 /*-----------------------------------------------------------*/
\r
109 /* Saved as part of the task context. Set to pdFALSE if the task does not
\r
110 require an FPU context. */
\r
111 unsigned long ulTaskHasFPUContext = 0;
\r
113 /*-----------------------------------------------------------*/
\r
117 * See header file for description.
\r
119 portSTACK_TYPE *pxPortInitialiseStack( portSTACK_TYPE *pxTopOfStack, pdTASK_CODE pxCode, void *pvParameters )
\r
121 portSTACK_TYPE *pxOriginalTOS;
\r
123 pxOriginalTOS = pxTopOfStack;
\r
125 #if __TI_VFP_SUPPORT__
\r
127 /* Ensure the stack is correctly aligned on exit. */
\r
132 /* Setup the initial stack of the task. The stack is set exactly as
\r
133 expected by the portRESTORE_CONTEXT() macro. */
\r
135 /* First on the stack is the return address - which is the start of the as
\r
136 the task has not executed yet. The offset is added to make the return
\r
137 address appear as it would within an IRQ ISR. */
\r
138 *pxTopOfStack = ( portSTACK_TYPE ) pxCode + portINSTRUCTION_SIZE;
\r
141 *pxTopOfStack = ( portSTACK_TYPE ) 0x00000000; /* R14 */
\r
143 *pxTopOfStack = ( portSTACK_TYPE ) pxOriginalTOS; /* Stack used when task starts goes in R13. */
\r
146 #ifdef portPRELOAD_TASK_REGISTERS
\r
148 *pxTopOfStack = ( portSTACK_TYPE ) 0x12121212; /* R12 */
\r
150 *pxTopOfStack = ( portSTACK_TYPE ) 0x11111111; /* R11 */
\r
152 *pxTopOfStack = ( portSTACK_TYPE ) 0x10101010; /* R10 */
\r
154 *pxTopOfStack = ( portSTACK_TYPE ) 0x09090909; /* R9 */
\r
156 *pxTopOfStack = ( portSTACK_TYPE ) 0x08080808; /* R8 */
\r
158 *pxTopOfStack = ( portSTACK_TYPE ) 0x07070707; /* R7 */
\r
160 *pxTopOfStack = ( portSTACK_TYPE ) 0x06060606; /* R6 */
\r
162 *pxTopOfStack = ( portSTACK_TYPE ) 0x05050505; /* R5 */
\r
164 *pxTopOfStack = ( portSTACK_TYPE ) 0x04040404; /* R4 */
\r
166 *pxTopOfStack = ( portSTACK_TYPE ) 0x03030303; /* R3 */
\r
168 *pxTopOfStack = ( portSTACK_TYPE ) 0x02020202; /* R2 */
\r
170 *pxTopOfStack = ( portSTACK_TYPE ) 0x01010101; /* R1 */
\r
175 pxTopOfStack -= portSPACE_BETWEEN_TOS_AND_PARAMETERS;
\r
179 /* Function parameters are passed in R0. */
\r
180 *pxTopOfStack = ( portSTACK_TYPE ) pvParameters; /* R0 */
\r
183 /* Set the status register for system mode, with interrupts enabled. */
\r
184 *pxTopOfStack = ( portSTACK_TYPE ) ( ( _get_CPSR() & ~0xFF ) | portINITIAL_SPSR );
\r
186 if( ( ( unsigned long ) pxCode & 0x01UL ) != 0x00 )
\r
188 /* The task will start in thumb mode. */
\r
189 *pxTopOfStack |= portTHUMB_MODE_BIT;
\r
192 #ifdef __TI_VFP_SUPPORT__
\r
196 /* The last thing on the stack is the tasks ulUsingFPU value, which by
\r
197 default is set to indicate that the stack frame does not include FPU
\r
199 *pxTopOfStack = pdFALSE;
\r
203 return pxTopOfStack;
\r
205 /*-----------------------------------------------------------*/
\r
207 static void prvSetupTimerInterrupt(void)
\r
209 /* Disable timer 0. */
\r
210 portRTI_GCTRL_REG &= 0xFFFFFFFEUL;
\r
212 /* Use the internal counter. */
\r
213 portRTI_TBCTRL_REG = 0x00000000U;
\r
215 /* COMPSEL0 will use the RTIFRC0 counter. */
\r
216 portRTI_COMPCTRL_REG = 0x00000000U;
\r
218 /* Initialise the counter and the prescale counter registers. */
\r
219 portRTI_CNT0_UC0_REG = 0x00000000U;
\r
220 portRTI_CNT0_FRC0_REG = 0x00000000U;
\r
222 /* Set Prescalar for RTI clock. */
\r
223 portRTI_CNT0_CPUC0_REG = 0x00000001U;
\r
224 portRTI_CNT0_COMP0_REG = ( configCPU_CLOCK_HZ / 2 ) / configTICK_RATE_HZ;
\r
225 portRTI_CNT0_UDCP0_REG = ( configCPU_CLOCK_HZ / 2 ) / configTICK_RATE_HZ;
\r
227 /* Clear interrupts. */
\r
228 portRTI_INTFLAG_REG = 0x0007000FU;
\r
229 portRTI_CLEARINTENA_REG = 0x00070F0FU;
\r
231 /* Enable the compare 0 interrupt. */
\r
232 portRTI_SETINTENA_REG = 0x00000001U;
\r
233 portRTI_GCTRL_REG |= 0x00000001U;
\r
235 /*-----------------------------------------------------------*/
\r
238 * See header file for description.
\r
240 portBASE_TYPE xPortStartScheduler(void)
\r
242 /* Start the timer that generates the tick ISR. */
\r
243 prvSetupTimerInterrupt();
\r
245 /* Reset the critical section nesting count read to execute the first task. */
\r
246 ulCriticalNesting = 0;
\r
248 /* Start the first task. This is done from portASM.asm as ARM mode must be
\r
250 vPortStartFirstTask();
\r
252 /* Should not get here! */
\r
255 /*-----------------------------------------------------------*/
\r
258 * See header file for description.
\r
260 void vPortEndScheduler(void)
\r
262 /* It is unlikely that the port will require this function as there
\r
263 is nothing to return to. */
\r
265 /*-----------------------------------------------------------*/
\r
267 #if configUSE_PREEMPTION == 0
\r
269 /* The cooperative scheduler requires a normal IRQ service routine to
\r
270 * simply increment the system tick. */
\r
271 __interrupt void vPortNonPreemptiveTick( void )
\r
273 /* clear clock interrupt flag */
\r
274 RTI->INTFLAG = 0x00000001;
\r
276 /* Increment the tick count - this may make a delaying task ready
\r
277 to run - but a context switch is not performed. */
\r
278 vTaskIncrementTick();
\r
284 **************************************************************************
\r
285 * The preemptive scheduler ISR is written in assembler and can be found
\r
286 * in the portASM.asm file. This will only get used if portUSE_PREEMPTION
\r
287 * is set to 1 in portmacro.h
\r
288 **************************************************************************
\r
290 void vPortPreemptiveTick( void );
\r
293 /*-----------------------------------------------------------*/
\r
297 * Disable interrupts, and keep a count of the nesting depth.
\r
299 void vPortEnterCritical( void )
\r
301 /* Disable interrupts as per portDISABLE_INTERRUPTS(); */
\r
302 portDISABLE_INTERRUPTS();
\r
304 /* Now interrupts are disabled ulCriticalNesting can be accessed
\r
305 directly. Increment ulCriticalNesting to keep a count of how many times
\r
306 portENTER_CRITICAL() has been called. */
\r
307 ulCriticalNesting++;
\r
309 /*-----------------------------------------------------------*/
\r
312 * Decrement the critical nesting count, and if it has reached zero, re-enable
\r
315 void vPortExitCritical( void )
\r
317 if( ulCriticalNesting > 0 )
\r
319 /* Decrement the nesting count as we are leaving a critical section. */
\r
320 ulCriticalNesting--;
\r
322 /* If the nesting level has reached zero then interrupts should be
\r
324 if( ulCriticalNesting == 0 )
\r
326 /* Enable interrupts as per portENABLE_INTERRUPTS(). */
\r
327 portENABLE_INTERRUPTS();
\r
331 /*-----------------------------------------------------------*/
\r
333 #if __TI_VFP_SUPPORT__
\r
335 void vPortTaskUsesFPU( void )
\r
337 extern void vPortInitialiseFPSCR( void );
\r
339 /* A task is registering the fact that it needs an FPU context. Set the
\r
340 FPU flag (saved as part of the task context. */
\r
341 ulTaskHasFPUContext = pdTRUE;
\r
343 /* Initialise the floating point status register. */
\r
344 vPortInitialiseFPSCR();
\r
347 #endif /* __TI_VFP_SUPPORT__ */
\r
349 /*-----------------------------------------------------------*/
\r