2 FreeRTOS V8.1.1 - Copyright (C) 2014 Real Time Engineers Ltd.
\r
5 VISIT http://www.FreeRTOS.org TO ENSURE YOU ARE USING THE LATEST VERSION.
\r
7 ***************************************************************************
\r
9 * FreeRTOS provides completely free yet professionally developed, *
\r
10 * robust, strictly quality controlled, supported, and cross *
\r
11 * platform software that has become a de facto standard. *
\r
13 * Help yourself get started quickly and support the FreeRTOS *
\r
14 * project by purchasing a FreeRTOS tutorial book, reference *
\r
15 * manual, or both from: http://www.FreeRTOS.org/Documentation *
\r
19 ***************************************************************************
\r
21 This file is part of the FreeRTOS distribution.
\r
23 FreeRTOS is free software; you can redistribute it and/or modify it under
\r
24 the terms of the GNU General Public License (version 2) as published by the
\r
25 Free Software Foundation >>!AND MODIFIED BY!<< the FreeRTOS exception.
\r
27 >>! NOTE: The modification to the GPL is included to allow you to !<<
\r
28 >>! distribute a combined work that includes FreeRTOS without being !<<
\r
29 >>! obliged to provide the source code for proprietary components !<<
\r
30 >>! outside of the FreeRTOS kernel. !<<
\r
32 FreeRTOS is distributed in the hope that it will be useful, but WITHOUT ANY
\r
33 WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
\r
34 FOR A PARTICULAR PURPOSE. Full license text is available from the following
\r
35 link: http://www.freertos.org/a00114.html
\r
39 ***************************************************************************
\r
41 * Having a problem? Start by reading the FAQ "My application does *
\r
42 * not run, what could be wrong?" *
\r
44 * http://www.FreeRTOS.org/FAQHelp.html *
\r
46 ***************************************************************************
\r
48 http://www.FreeRTOS.org - Documentation, books, training, latest versions,
\r
49 license and Real Time Engineers Ltd. contact details.
\r
51 http://www.FreeRTOS.org/plus - A selection of FreeRTOS ecosystem products,
\r
52 including FreeRTOS+Trace - an indispensable productivity tool, a DOS
\r
53 compatible FAT file system, and our tiny thread aware UDP/IP stack.
\r
55 http://www.OpenRTOS.com - Real Time Engineers ltd license FreeRTOS to High
\r
56 Integrity Systems to sell under the OpenRTOS brand. Low cost OpenRTOS
\r
57 licenses offer ticketed support, indemnification and middleware.
\r
59 http://www.SafeRTOS.com - High Integrity Systems also provide a safety
\r
60 engineered and independently SIL3 certified version for use in safety and
\r
61 mission critical applications that require provable dependability.
\r
66 /* FreeRTOS includes. */
\r
67 #include "FreeRTOS.h"
\r
70 /*-----------------------------------------------------------*/
\r
72 /* Count of the critical section nesting depth. */
\r
73 uint32_t ulCriticalNesting = 9999;
\r
75 /*-----------------------------------------------------------*/
\r
77 /* Registers required to configure the RTI. */
\r
78 #define portRTI_GCTRL_REG ( * ( ( volatile uint32_t * ) 0xFFFFFC00 ) )
\r
79 #define portRTI_TBCTRL_REG ( * ( ( volatile uint32_t * ) 0xFFFFFC04 ) )
\r
80 #define portRTI_COMPCTRL_REG ( * ( ( volatile uint32_t * ) 0xFFFFFC0C ) )
\r
81 #define portRTI_CNT0_FRC0_REG ( * ( ( volatile uint32_t * ) 0xFFFFFC10 ) )
\r
82 #define portRTI_CNT0_UC0_REG ( * ( ( volatile uint32_t * ) 0xFFFFFC14 ) )
\r
83 #define portRTI_CNT0_CPUC0_REG ( * ( ( volatile uint32_t * ) 0xFFFFFC18 ) )
\r
84 #define portRTI_CNT0_COMP0_REG ( * ( ( volatile uint32_t * ) 0xFFFFFC50 ) )
\r
85 #define portRTI_CNT0_UDCP0_REG ( * ( ( volatile uint32_t * ) 0xFFFFFC54 ) )
\r
86 #define portRTI_SETINTENA_REG ( * ( ( volatile uint32_t * ) 0xFFFFFC80 ) )
\r
87 #define portRTI_CLEARINTENA_REG ( * ( ( volatile uint32_t * ) 0xFFFFFC84 ) )
\r
88 #define portRTI_INTFLAG_REG ( * ( ( volatile uint32_t * ) 0xFFFFFC88 ) )
\r
91 /* Constants required to set up the initial stack of each task. */
\r
92 #define portINITIAL_SPSR ( ( StackType_t ) 0x1F )
\r
93 #define portINITIAL_FPSCR ( ( StackType_t ) 0x00 )
\r
94 #define portINSTRUCTION_SIZE ( ( StackType_t ) 0x04 )
\r
95 #define portTHUMB_MODE_BIT ( ( StackType_t ) 0x20 )
\r
97 /* The number of words on the stack frame between the saved Top Of Stack and
\r
98 R0 (in which the parameters are passed. */
\r
99 #define portSPACE_BETWEEN_TOS_AND_PARAMETERS ( 12 )
\r
101 /*-----------------------------------------------------------*/
\r
103 /* vPortStartFirstSTask() is defined in portASM.asm */
\r
104 extern void vPortStartFirstTask( void );
\r
106 /*-----------------------------------------------------------*/
\r
108 /* Saved as part of the task context. Set to pdFALSE if the task does not
\r
109 require an FPU context. */
\r
110 uint32_t ulTaskHasFPUContext = 0;
\r
112 /*-----------------------------------------------------------*/
\r
116 * See header file for description.
\r
118 StackType_t *pxPortInitialiseStack( StackType_t *pxTopOfStack, TaskFunction_t pxCode, void *pvParameters )
\r
120 StackType_t *pxOriginalTOS;
\r
122 pxOriginalTOS = pxTopOfStack;
\r
124 #if __TI_VFP_SUPPORT__
\r
126 /* Ensure the stack is correctly aligned on exit. */
\r
131 /* Setup the initial stack of the task. The stack is set exactly as
\r
132 expected by the portRESTORE_CONTEXT() macro. */
\r
134 /* First on the stack is the return address - which is the start of the as
\r
135 the task has not executed yet. The offset is added to make the return
\r
136 address appear as it would within an IRQ ISR. */
\r
137 *pxTopOfStack = ( StackType_t ) pxCode + portINSTRUCTION_SIZE;
\r
140 *pxTopOfStack = ( StackType_t ) 0x00000000; /* R14 */
\r
142 *pxTopOfStack = ( StackType_t ) pxOriginalTOS; /* Stack used when task starts goes in R13. */
\r
145 #ifdef portPRELOAD_TASK_REGISTERS
\r
147 *pxTopOfStack = ( StackType_t ) 0x12121212; /* R12 */
\r
149 *pxTopOfStack = ( StackType_t ) 0x11111111; /* R11 */
\r
151 *pxTopOfStack = ( StackType_t ) 0x10101010; /* R10 */
\r
153 *pxTopOfStack = ( StackType_t ) 0x09090909; /* R9 */
\r
155 *pxTopOfStack = ( StackType_t ) 0x08080808; /* R8 */
\r
157 *pxTopOfStack = ( StackType_t ) 0x07070707; /* R7 */
\r
159 *pxTopOfStack = ( StackType_t ) 0x06060606; /* R6 */
\r
161 *pxTopOfStack = ( StackType_t ) 0x05050505; /* R5 */
\r
163 *pxTopOfStack = ( StackType_t ) 0x04040404; /* R4 */
\r
165 *pxTopOfStack = ( StackType_t ) 0x03030303; /* R3 */
\r
167 *pxTopOfStack = ( StackType_t ) 0x02020202; /* R2 */
\r
169 *pxTopOfStack = ( StackType_t ) 0x01010101; /* R1 */
\r
174 pxTopOfStack -= portSPACE_BETWEEN_TOS_AND_PARAMETERS;
\r
178 /* Function parameters are passed in R0. */
\r
179 *pxTopOfStack = ( StackType_t ) pvParameters; /* R0 */
\r
182 /* Set the status register for system mode, with interrupts enabled. */
\r
183 *pxTopOfStack = ( StackType_t ) ( ( _get_CPSR() & ~0xFF ) | portINITIAL_SPSR );
\r
185 if( ( ( uint32_t ) pxCode & 0x01UL ) != 0x00 )
\r
187 /* The task will start in thumb mode. */
\r
188 *pxTopOfStack |= portTHUMB_MODE_BIT;
\r
191 #ifdef __TI_VFP_SUPPORT__
\r
195 /* The last thing on the stack is the tasks ulUsingFPU value, which by
\r
196 default is set to indicate that the stack frame does not include FPU
\r
198 *pxTopOfStack = pdFALSE;
\r
202 return pxTopOfStack;
\r
204 /*-----------------------------------------------------------*/
\r
206 static void prvSetupTimerInterrupt(void)
\r
208 /* Disable timer 0. */
\r
209 portRTI_GCTRL_REG &= 0xFFFFFFFEUL;
\r
211 /* Use the internal counter. */
\r
212 portRTI_TBCTRL_REG = 0x00000000U;
\r
214 /* COMPSEL0 will use the RTIFRC0 counter. */
\r
215 portRTI_COMPCTRL_REG = 0x00000000U;
\r
217 /* Initialise the counter and the prescale counter registers. */
\r
218 portRTI_CNT0_UC0_REG = 0x00000000U;
\r
219 portRTI_CNT0_FRC0_REG = 0x00000000U;
\r
221 /* Set Prescalar for RTI clock. */
\r
222 portRTI_CNT0_CPUC0_REG = 0x00000001U;
\r
223 portRTI_CNT0_COMP0_REG = ( configCPU_CLOCK_HZ / 2 ) / configTICK_RATE_HZ;
\r
224 portRTI_CNT0_UDCP0_REG = ( configCPU_CLOCK_HZ / 2 ) / configTICK_RATE_HZ;
\r
226 /* Clear interrupts. */
\r
227 portRTI_INTFLAG_REG = 0x0007000FU;
\r
228 portRTI_CLEARINTENA_REG = 0x00070F0FU;
\r
230 /* Enable the compare 0 interrupt. */
\r
231 portRTI_SETINTENA_REG = 0x00000001U;
\r
232 portRTI_GCTRL_REG |= 0x00000001U;
\r
234 /*-----------------------------------------------------------*/
\r
237 * See header file for description.
\r
239 BaseType_t xPortStartScheduler(void)
\r
241 /* Start the timer that generates the tick ISR. */
\r
242 prvSetupTimerInterrupt();
\r
244 /* Reset the critical section nesting count read to execute the first task. */
\r
245 ulCriticalNesting = 0;
\r
247 /* Start the first task. This is done from portASM.asm as ARM mode must be
\r
249 vPortStartFirstTask();
\r
251 /* Should not get here! */
\r
254 /*-----------------------------------------------------------*/
\r
257 * See header file for description.
\r
259 void vPortEndScheduler(void)
\r
261 /* Not implemented in ports where there is nothing to return to.
\r
262 Artificially force an assert. */
\r
263 configASSERT( ulCriticalNesting == 1000UL );
\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 portRTI_INTFLAG_REG = 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 xTaskIncrementTick();
\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