2 FreeRTOS V7.4.1 - Copyright (C) 2013 Real Time Engineers Ltd.
\r
4 FEATURES AND PORTS ARE ADDED TO FREERTOS ALL THE TIME. PLEASE VISIT
\r
5 http://www.FreeRTOS.org TO ENSURE YOU ARE USING THE LATEST VERSION.
\r
7 ***************************************************************************
\r
9 * FreeRTOS tutorial books are available in pdf and paperback. *
\r
10 * Complete, revised, and edited pdf reference manuals are also *
\r
13 * Purchasing FreeRTOS documentation will not only help you, by *
\r
14 * ensuring you get running as quickly as possible and with an *
\r
15 * in-depth knowledge of how to use FreeRTOS, it will also help *
\r
16 * the FreeRTOS project to continue with its mission of providing *
\r
17 * professional grade, cross platform, de facto standard solutions *
\r
18 * for microcontrollers - completely free of charge! *
\r
20 * >>> See http://www.FreeRTOS.org/Documentation for details. <<< *
\r
22 * Thank you for using FreeRTOS, and thank you for your support! *
\r
24 ***************************************************************************
\r
27 This file is part of the FreeRTOS distribution.
\r
29 FreeRTOS is free software; you can redistribute it and/or modify it under
\r
30 the terms of the GNU General Public License (version 2) as published by the
\r
31 Free Software Foundation AND MODIFIED BY the FreeRTOS exception.
\r
33 >>>>>>NOTE<<<<<< The modification to the GPL is included to allow you to
\r
34 distribute a combined work that includes FreeRTOS without being obliged to
\r
35 provide the source code for proprietary components outside of the FreeRTOS
\r
38 FreeRTOS is distributed in the hope that it will be useful, but WITHOUT ANY
\r
39 WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
\r
40 FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
\r
41 details. You should have received a copy of the GNU General Public License
\r
42 and the FreeRTOS license exception along with FreeRTOS; if not it can be
\r
43 viewed here: http://www.freertos.org/a00114.html and also obtained by
\r
44 writing to Real Time Engineers Ltd., contact details for whom are available
\r
45 on the FreeRTOS WEB site.
\r
49 ***************************************************************************
\r
51 * Having a problem? Start by reading the FAQ "My application does *
\r
52 * not run, what could be wrong?" *
\r
54 * http://www.FreeRTOS.org/FAQHelp.html *
\r
56 ***************************************************************************
\r
59 http://www.FreeRTOS.org - Documentation, books, training, latest versions,
\r
60 license and Real Time Engineers Ltd. contact details.
\r
62 http://www.FreeRTOS.org/plus - A selection of FreeRTOS ecosystem products,
\r
63 including FreeRTOS+Trace - an indispensable productivity tool, and our new
\r
64 fully thread aware and reentrant UDP/IP stack.
\r
66 http://www.OpenRTOS.com - Real Time Engineers ltd license FreeRTOS to High
\r
67 Integrity Systems, who sell the code with commercial support,
\r
68 indemnification and middleware, under the OpenRTOS brand.
\r
70 http://www.SafeRTOS.com - High Integrity Systems also provide a safety
\r
71 engineered and independently SIL3 certified version for use in safety and
\r
72 mission critical applications that require provable dependability.
\r
75 /* FreeRTOS includes. */
\r
76 #include "FreeRTOS.h"
\r
79 /*-----------------------------------------------------------*/
\r
81 /* Count of the critical section nesting depth. */
\r
82 unsigned portLONG ulCriticalNesting = 9999;
\r
84 /*-----------------------------------------------------------*/
\r
86 /* Registers required to configure the RTI. */
\r
87 #define portRTI_GCTRL_REG ( * ( ( volatile unsigned long * ) 0xFFFFFC00 ) )
\r
88 #define portRTI_TBCTRL_REG ( * ( ( volatile unsigned long * ) 0xFFFFFC04 ) )
\r
89 #define portRTI_COMPCTRL_REG ( * ( ( volatile unsigned long * ) 0xFFFFFC0C ) )
\r
90 #define portRTI_CNT0_FRC0_REG ( * ( ( volatile unsigned long * ) 0xFFFFFC10 ) )
\r
91 #define portRTI_CNT0_UC0_REG ( * ( ( volatile unsigned long * ) 0xFFFFFC14 ) )
\r
92 #define portRTI_CNT0_CPUC0_REG ( * ( ( volatile unsigned long * ) 0xFFFFFC18 ) )
\r
93 #define portRTI_CNT0_COMP0_REG ( * ( ( volatile unsigned long * ) 0xFFFFFC50 ) )
\r
94 #define portRTI_CNT0_UDCP0_REG ( * ( ( volatile unsigned long * ) 0xFFFFFC54 ) )
\r
95 #define portRTI_SETINTENA_REG ( * ( ( volatile unsigned long * ) 0xFFFFFC80 ) )
\r
96 #define portRTI_CLEARINTENA_REG ( * ( ( volatile unsigned long * ) 0xFFFFFC84 ) )
\r
97 #define portRTI_INTFLAG_REG ( * ( ( volatile unsigned long * ) 0xFFFFFC88 ) )
\r
100 /* Constants required to set up the initial stack of each task. */
\r
101 #define portINITIAL_SPSR ( ( portSTACK_TYPE ) 0x1F )
\r
102 #define portINITIAL_FPSCR ( ( portSTACK_TYPE ) 0x00 )
\r
103 #define portINSTRUCTION_SIZE ( ( portSTACK_TYPE ) 0x04 )
\r
104 #define portTHUMB_MODE_BIT ( ( portSTACK_TYPE ) 0x20 )
\r
106 /* The number of words on the stack frame between the saved Top Of Stack and
\r
107 R0 (in which the parameters are passed. */
\r
108 #define portSPACE_BETWEEN_TOS_AND_PARAMETERS ( 12 )
\r
110 /*-----------------------------------------------------------*/
\r
112 /* vPortStartFirstSTask() is defined in portASM.asm */
\r
113 extern void vPortStartFirstTask( void );
\r
115 /*-----------------------------------------------------------*/
\r
117 /* Saved as part of the task context. Set to pdFALSE if the task does not
\r
118 require an FPU context. */
\r
119 unsigned long ulTaskHasFPUContext = 0;
\r
121 /*-----------------------------------------------------------*/
\r
125 * See header file for description.
\r
127 portSTACK_TYPE *pxPortInitialiseStack( portSTACK_TYPE *pxTopOfStack, pdTASK_CODE pxCode, void *pvParameters )
\r
129 portSTACK_TYPE *pxOriginalTOS;
\r
131 pxOriginalTOS = pxTopOfStack;
\r
133 #if __TI_VFP_SUPPORT__
\r
135 /* Ensure the stack is correctly aligned on exit. */
\r
140 /* Setup the initial stack of the task. The stack is set exactly as
\r
141 expected by the portRESTORE_CONTEXT() macro. */
\r
143 /* First on the stack is the return address - which is the start of the as
\r
144 the task has not executed yet. The offset is added to make the return
\r
145 address appear as it would within an IRQ ISR. */
\r
146 *pxTopOfStack = ( portSTACK_TYPE ) pxCode + portINSTRUCTION_SIZE;
\r
149 *pxTopOfStack = ( portSTACK_TYPE ) 0x00000000; /* R14 */
\r
151 *pxTopOfStack = ( portSTACK_TYPE ) pxOriginalTOS; /* Stack used when task starts goes in R13. */
\r
154 #ifdef portPRELOAD_TASK_REGISTERS
\r
156 *pxTopOfStack = ( portSTACK_TYPE ) 0x12121212; /* R12 */
\r
158 *pxTopOfStack = ( portSTACK_TYPE ) 0x11111111; /* R11 */
\r
160 *pxTopOfStack = ( portSTACK_TYPE ) 0x10101010; /* R10 */
\r
162 *pxTopOfStack = ( portSTACK_TYPE ) 0x09090909; /* R9 */
\r
164 *pxTopOfStack = ( portSTACK_TYPE ) 0x08080808; /* R8 */
\r
166 *pxTopOfStack = ( portSTACK_TYPE ) 0x07070707; /* R7 */
\r
168 *pxTopOfStack = ( portSTACK_TYPE ) 0x06060606; /* R6 */
\r
170 *pxTopOfStack = ( portSTACK_TYPE ) 0x05050505; /* R5 */
\r
172 *pxTopOfStack = ( portSTACK_TYPE ) 0x04040404; /* R4 */
\r
174 *pxTopOfStack = ( portSTACK_TYPE ) 0x03030303; /* R3 */
\r
176 *pxTopOfStack = ( portSTACK_TYPE ) 0x02020202; /* R2 */
\r
178 *pxTopOfStack = ( portSTACK_TYPE ) 0x01010101; /* R1 */
\r
183 pxTopOfStack -= portSPACE_BETWEEN_TOS_AND_PARAMETERS;
\r
187 /* Function parameters are passed in R0. */
\r
188 *pxTopOfStack = ( portSTACK_TYPE ) pvParameters; /* R0 */
\r
191 /* Set the status register for system mode, with interrupts enabled. */
\r
192 *pxTopOfStack = ( portSTACK_TYPE ) ( ( _get_CPSR() & ~0xFF ) | portINITIAL_SPSR );
\r
194 if( ( ( unsigned long ) pxCode & 0x01UL ) != 0x00 )
\r
196 /* The task will start in thumb mode. */
\r
197 *pxTopOfStack |= portTHUMB_MODE_BIT;
\r
200 #ifdef __TI_VFP_SUPPORT__
\r
204 /* The last thing on the stack is the tasks ulUsingFPU value, which by
\r
205 default is set to indicate that the stack frame does not include FPU
\r
207 *pxTopOfStack = pdFALSE;
\r
211 return pxTopOfStack;
\r
213 /*-----------------------------------------------------------*/
\r
215 static void prvSetupTimerInterrupt(void)
\r
217 /* Disable timer 0. */
\r
218 portRTI_GCTRL_REG &= 0xFFFFFFFEUL;
\r
220 /* Use the internal counter. */
\r
221 portRTI_TBCTRL_REG = 0x00000000U;
\r
223 /* COMPSEL0 will use the RTIFRC0 counter. */
\r
224 portRTI_COMPCTRL_REG = 0x00000000U;
\r
226 /* Initialise the counter and the prescale counter registers. */
\r
227 portRTI_CNT0_UC0_REG = 0x00000000U;
\r
228 portRTI_CNT0_FRC0_REG = 0x00000000U;
\r
230 /* Set Prescalar for RTI clock. */
\r
231 portRTI_CNT0_CPUC0_REG = 0x00000001U;
\r
232 portRTI_CNT0_COMP0_REG = ( configCPU_CLOCK_HZ / 2 ) / configTICK_RATE_HZ;
\r
233 portRTI_CNT0_UDCP0_REG = ( configCPU_CLOCK_HZ / 2 ) / configTICK_RATE_HZ;
\r
235 /* Clear interrupts. */
\r
236 portRTI_INTFLAG_REG = 0x0007000FU;
\r
237 portRTI_CLEARINTENA_REG = 0x00070F0FU;
\r
239 /* Enable the compare 0 interrupt. */
\r
240 portRTI_SETINTENA_REG = 0x00000001U;
\r
241 portRTI_GCTRL_REG |= 0x00000001U;
\r
243 /*-----------------------------------------------------------*/
\r
246 * See header file for description.
\r
248 portBASE_TYPE xPortStartScheduler(void)
\r
250 /* Start the timer that generates the tick ISR. */
\r
251 prvSetupTimerInterrupt();
\r
253 /* Reset the critical section nesting count read to execute the first task. */
\r
254 ulCriticalNesting = 0;
\r
256 /* Start the first task. This is done from portASM.asm as ARM mode must be
\r
258 vPortStartFirstTask();
\r
260 /* Should not get here! */
\r
263 /*-----------------------------------------------------------*/
\r
266 * See header file for description.
\r
268 void vPortEndScheduler(void)
\r
270 /* It is unlikely that the port will require this function as there
\r
271 is nothing to return to. */
\r
273 /*-----------------------------------------------------------*/
\r
275 #if configUSE_PREEMPTION == 0
\r
277 /* The cooperative scheduler requires a normal IRQ service routine to
\r
278 * simply increment the system tick. */
\r
279 __interrupt void vPortNonPreemptiveTick( void )
\r
281 /* clear clock interrupt flag */
\r
282 RTI->INTFLAG = 0x00000001;
\r
284 /* Increment the tick count - this may make a delaying task ready
\r
285 to run - but a context switch is not performed. */
\r
286 vTaskIncrementTick();
\r
292 **************************************************************************
\r
293 * The preemptive scheduler ISR is written in assembler and can be found
\r
294 * in the portASM.asm file. This will only get used if portUSE_PREEMPTION
\r
295 * is set to 1 in portmacro.h
\r
296 **************************************************************************
\r
298 void vPortPreemptiveTick( void );
\r
301 /*-----------------------------------------------------------*/
\r
305 * Disable interrupts, and keep a count of the nesting depth.
\r
307 void vPortEnterCritical( void )
\r
309 /* Disable interrupts as per portDISABLE_INTERRUPTS(); */
\r
310 portDISABLE_INTERRUPTS();
\r
312 /* Now interrupts are disabled ulCriticalNesting can be accessed
\r
313 directly. Increment ulCriticalNesting to keep a count of how many times
\r
314 portENTER_CRITICAL() has been called. */
\r
315 ulCriticalNesting++;
\r
317 /*-----------------------------------------------------------*/
\r
320 * Decrement the critical nesting count, and if it has reached zero, re-enable
\r
323 void vPortExitCritical( void )
\r
325 if( ulCriticalNesting > 0 )
\r
327 /* Decrement the nesting count as we are leaving a critical section. */
\r
328 ulCriticalNesting--;
\r
330 /* If the nesting level has reached zero then interrupts should be
\r
332 if( ulCriticalNesting == 0 )
\r
334 /* Enable interrupts as per portENABLE_INTERRUPTS(). */
\r
335 portENABLE_INTERRUPTS();
\r
339 /*-----------------------------------------------------------*/
\r
341 #if __TI_VFP_SUPPORT__
\r
343 void vPortTaskUsesFPU( void )
\r
345 extern void vPortInitialiseFPSCR( void );
\r
347 /* A task is registering the fact that it needs an FPU context. Set the
\r
348 FPU flag (saved as part of the task context. */
\r
349 ulTaskHasFPUContext = pdTRUE;
\r
351 /* Initialise the floating point status register. */
\r
352 vPortInitialiseFPSCR();
\r
355 #endif /* __TI_VFP_SUPPORT__ */
\r
357 /*-----------------------------------------------------------*/
\r