2 FreeRTOS V8.2.0rc1 - Copyright (C) 2014 Real Time Engineers Ltd.
\r
5 VISIT http://www.FreeRTOS.org TO ENSURE YOU ARE USING THE LATEST VERSION.
\r
7 This file is part of the FreeRTOS distribution.
\r
9 FreeRTOS is free software; you can redistribute it and/or modify it under
\r
10 the terms of the GNU General Public License (version 2) as published by the
\r
11 Free Software Foundation >>!AND MODIFIED BY!<< the FreeRTOS exception.
\r
13 >>! NOTE: The modification to the GPL is included to allow you to !<<
\r
14 >>! distribute a combined work that includes FreeRTOS without being !<<
\r
15 >>! obliged to provide the source code for proprietary components !<<
\r
16 >>! outside of the FreeRTOS kernel. !<<
\r
18 FreeRTOS is distributed in the hope that it will be useful, but WITHOUT ANY
\r
19 WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
\r
20 FOR A PARTICULAR PURPOSE. Full license text is available on the following
\r
21 link: http://www.freertos.org/a00114.html
\r
25 ***************************************************************************
\r
27 * Having a problem? Start by reading the FAQ "My application does *
\r
28 * not run, what could be wrong?". Have you defined configASSERT()? *
\r
30 * http://www.FreeRTOS.org/FAQHelp.html *
\r
32 ***************************************************************************
\r
34 ***************************************************************************
\r
36 * FreeRTOS provides completely free yet professionally developed, *
\r
37 * robust, strictly quality controlled, supported, and cross *
\r
38 * platform software that is more than just the market leader, it *
\r
39 * is the industry's de facto standard. *
\r
41 * Help yourself get started quickly while simultaneously helping *
\r
42 * to support the FreeRTOS project by purchasing a FreeRTOS *
\r
43 * tutorial book, reference manual, or both: *
\r
44 * http://www.FreeRTOS.org/Documentation *
\r
46 ***************************************************************************
\r
48 ***************************************************************************
\r
50 * Investing in training allows your team to be as productive as *
\r
51 * possible as early as possible, lowering your overall development *
\r
52 * cost, and enabling you to bring a more robust product to market *
\r
53 * earlier than would otherwise be possible. Richard Barry is both *
\r
54 * the architect and key author of FreeRTOS, and so also the world's *
\r
55 * leading authority on what is the world's most popular real time *
\r
56 * kernel for deeply embedded MCU designs. Obtaining your training *
\r
57 * from Richard ensures your team will gain directly from his in-depth *
\r
58 * product knowledge and years of usage experience. Contact Real Time *
\r
59 * Engineers Ltd to enquire about the FreeRTOS Masterclass, presented *
\r
60 * by Richard Barry: http://www.FreeRTOS.org/contact
\r
62 ***************************************************************************
\r
64 ***************************************************************************
\r
66 * You are receiving this top quality software for free. Please play *
\r
67 * fair and reciprocate by reporting any suspected issues and *
\r
68 * participating in the community forum: *
\r
69 * http://www.FreeRTOS.org/support *
\r
73 ***************************************************************************
\r
75 http://www.FreeRTOS.org - Documentation, books, training, latest versions,
\r
76 license and Real Time Engineers Ltd. contact details.
\r
78 http://www.FreeRTOS.org/plus - A selection of FreeRTOS ecosystem products,
\r
79 including FreeRTOS+Trace - an indispensable productivity tool, a DOS
\r
80 compatible FAT file system, and our tiny thread aware UDP/IP stack.
\r
82 http://www.FreeRTOS.org/labs - Where new FreeRTOS products go to incubate.
\r
83 Come and try FreeRTOS+TCP, our new open source TCP/IP stack for FreeRTOS.
\r
85 http://www.OpenRTOS.com - Real Time Engineers ltd license FreeRTOS to High
\r
86 Integrity Systems ltd. to sell under the OpenRTOS brand. Low cost OpenRTOS
\r
87 licenses offer ticketed support, indemnification and commercial middleware.
\r
89 http://www.SafeRTOS.com - High Integrity Systems also provide a safety
\r
90 engineered and independently SIL3 certified version for use in safety and
\r
91 mission critical applications that require provable dependability.
\r
97 #include <intrinsics.h>
\r
99 /* Scheduler includes. */
\r
100 #include "FreeRTOS.h"
\r
103 #if configUSE_PORT_OPTIMISED_TASK_SELECTION == 1
\r
104 /* Check the configuration. */
\r
105 #if( configMAX_PRIORITIES > 32 )
\r
106 #error configUSE_PORT_OPTIMISED_TASK_SELECTION can only be set to 1 when configMAX_PRIORITIES is less than or equal to 32. It is very rare that a system requires more than 10 to 15 difference priorities as tasks that share a priority will time slice.
\r
108 #endif /* configUSE_PORT_OPTIMISED_TASK_SELECTION */
\r
110 #ifndef configSETUP_TICK_INTERRUPT
\r
111 #error configSETUP_TICK_INTERRUPT() must be defined in FreeRTOSConfig.h to call the function that sets up the tick interrupt. A default that uses the PIT is provided in the official demo application.
\r
114 #ifndef configCLEAR_TICK_INTERRUPT
\r
115 #error configCLEAR_TICK_INTERRUPT must be defined in FreeRTOSConfig.h to clear which ever interrupt was used to generate the tick interrupt. A default that uses the PIT is provided in the official demo application.
\r
118 /* A critical section is exited when the critical section nesting count reaches
\r
120 #define portNO_CRITICAL_NESTING ( ( uint32_t ) 0 )
\r
122 /* Tasks are not created with a floating point context, but can be given a
\r
123 floating point context after they have been created. A variable is stored as
\r
124 part of the tasks context that holds portNO_FLOATING_POINT_CONTEXT if the task
\r
125 does not have an FPU context, or any other value if the task does have an FPU
\r
127 #define portNO_FLOATING_POINT_CONTEXT ( ( StackType_t ) 0 )
\r
129 /* Constants required to setup the initial task context. */
\r
130 #define portINITIAL_SPSR ( ( StackType_t ) 0x1f ) /* System mode, ARM mode, interrupts enabled. */
\r
131 #define portTHUMB_MODE_BIT ( ( StackType_t ) 0x20 )
\r
132 #define portTHUMB_MODE_ADDRESS ( 0x01UL )
\r
134 /* Masks all bits in the APSR other than the mode bits. */
\r
135 #define portAPSR_MODE_BITS_MASK ( 0x1F )
\r
137 /* The value of the mode bits in the APSR when the CPU is executing in user
\r
139 #define portAPSR_USER_MODE ( 0x10 )
\r
141 /*-----------------------------------------------------------*/
\r
144 * Starts the first task executing. This function is necessarily written in
\r
145 * assembly code so is implemented in portASM.s.
\r
147 extern void vPortRestoreTaskContext( void );
\r
150 * Used to catch tasks that attempt to return from their implementing function.
\r
152 static void prvTaskExitError( void );
\r
154 /*-----------------------------------------------------------*/
\r
156 /* A variable is used to keep track of the critical section nesting. This
\r
157 variable has to be stored as part of the task context and must be initialised to
\r
158 a non zero value to ensure interrupts don't inadvertently become unmasked before
\r
159 the scheduler starts. As it is stored as part of the task context it will
\r
160 automatically be set to 0 when the first task is started. */
\r
161 volatile uint32_t ulCriticalNesting = 9999UL;
\r
163 /* Saved as part of the task context. If ulPortTaskHasFPUContext is non-zero
\r
164 then a floating point context must be saved and restored for the task. */
\r
165 uint32_t ulPortTaskHasFPUContext = pdFALSE;
\r
167 /* Set to 1 to pend a context switch from an ISR. */
\r
168 uint32_t ulPortYieldRequired = pdFALSE;
\r
170 /* Counts the interrupt nesting depth. A context switch is only performed if
\r
171 if the nesting depth is 0. */
\r
172 uint32_t ulPortInterruptNesting = 0UL;
\r
175 /*-----------------------------------------------------------*/
\r
178 * See header file for description.
\r
180 StackType_t *pxPortInitialiseStack( StackType_t *pxTopOfStack, TaskFunction_t pxCode, void *pvParameters )
\r
182 /* Setup the initial stack of the task. The stack is set exactly as
\r
183 expected by the portRESTORE_CONTEXT() macro.
\r
185 The fist real value on the stack is the status register, which is set for
\r
186 system mode, with interrupts enabled. A few NULLs are added first to ensure
\r
187 GDB does not try decoding a non-existent return address. */
\r
188 *pxTopOfStack = NULL;
\r
190 *pxTopOfStack = NULL;
\r
192 *pxTopOfStack = NULL;
\r
194 *pxTopOfStack = ( StackType_t ) portINITIAL_SPSR;
\r
196 if( ( ( uint32_t ) pxCode & portTHUMB_MODE_ADDRESS ) != 0x00UL )
\r
198 /* The task will start in THUMB mode. */
\r
199 *pxTopOfStack |= portTHUMB_MODE_BIT;
\r
204 /* Next the return address, which in this case is the start of the task. */
\r
205 *pxTopOfStack = ( StackType_t ) pxCode;
\r
208 /* Next all the registers other than the stack pointer. */
\r
209 *pxTopOfStack = ( StackType_t ) prvTaskExitError; /* R14 */
\r
211 *pxTopOfStack = ( StackType_t ) 0x12121212; /* R12 */
\r
213 *pxTopOfStack = ( StackType_t ) 0x11111111; /* R11 */
\r
215 *pxTopOfStack = ( StackType_t ) 0x10101010; /* R10 */
\r
217 *pxTopOfStack = ( StackType_t ) 0x09090909; /* R9 */
\r
219 *pxTopOfStack = ( StackType_t ) 0x08080808; /* R8 */
\r
221 *pxTopOfStack = ( StackType_t ) 0x07070707; /* R7 */
\r
223 *pxTopOfStack = ( StackType_t ) 0x06060606; /* R6 */
\r
225 *pxTopOfStack = ( StackType_t ) 0x05050505; /* R5 */
\r
227 *pxTopOfStack = ( StackType_t ) 0x04040404; /* R4 */
\r
229 *pxTopOfStack = ( StackType_t ) 0x03030303; /* R3 */
\r
231 *pxTopOfStack = ( StackType_t ) 0x02020202; /* R2 */
\r
233 *pxTopOfStack = ( StackType_t ) 0x01010101; /* R1 */
\r
235 *pxTopOfStack = ( StackType_t ) pvParameters; /* R0 */
\r
238 /* The task will start with a critical nesting count of 0 as interrupts are
\r
240 *pxTopOfStack = portNO_CRITICAL_NESTING;
\r
243 /* The task will start without a floating point context. A task that uses
\r
244 the floating point hardware must call vPortTaskUsesFPU() before executing
\r
245 any floating point instructions. */
\r
246 *pxTopOfStack = portNO_FLOATING_POINT_CONTEXT;
\r
248 return pxTopOfStack;
\r
250 /*-----------------------------------------------------------*/
\r
252 static void prvTaskExitError( void )
\r
254 /* A function that implements a task must not exit or attempt to return to
\r
255 its caller as there is nothing to return to. If a task wants to exit it
\r
256 should instead call vTaskDelete( NULL ).
\r
258 Artificially force an assert() to be triggered if configASSERT() is
\r
259 defined, then stop here so application writers can catch the error. */
\r
260 configASSERT( ulPortInterruptNesting == ~0UL );
\r
261 portDISABLE_INTERRUPTS();
\r
264 /*-----------------------------------------------------------*/
\r
266 BaseType_t xPortStartScheduler( void )
\r
270 /* Only continue if the CPU is not in User mode. The CPU must be in a
\r
271 Privileged mode for the scheduler to start. */
\r
272 __asm volatile ( "MRS %0, APSR" : "=r" ( ulAPSR ) );
\r
273 ulAPSR &= portAPSR_MODE_BITS_MASK;
\r
274 configASSERT( ulAPSR != portAPSR_USER_MODE );
\r
276 if( ulAPSR != portAPSR_USER_MODE )
\r
278 /* Start the timer that generates the tick ISR. */
\r
279 configSETUP_TICK_INTERRUPT();
\r
280 vPortRestoreTaskContext();
\r
283 /* Will only get here if xTaskStartScheduler() was called with the CPU in
\r
284 a non-privileged mode or the binary point register was not set to its lowest
\r
288 /*-----------------------------------------------------------*/
\r
290 void vPortEndScheduler( void )
\r
292 /* Not implemented in ports where there is nothing to return to.
\r
293 Artificially force an assert. */
\r
294 configASSERT( ulCriticalNesting == 1000UL );
\r
296 /*-----------------------------------------------------------*/
\r
298 void vPortEnterCritical( void )
\r
300 portDISABLE_INTERRUPTS();
\r
302 /* Now interrupts are disabled ulCriticalNesting can be accessed
\r
303 directly. Increment ulCriticalNesting to keep a count of how many times
\r
304 portENTER_CRITICAL() has been called. */
\r
305 ulCriticalNesting++;
\r
307 /* This is not the interrupt safe version of the enter critical function so
\r
308 assert() if it is being called from an interrupt context. Only API
\r
309 functions that end in "FromISR" can be used in an interrupt. Only assert if
\r
310 the critical nesting count is 1 to protect against recursive calls if the
\r
311 assert function also uses a critical section. */
\r
312 if( ulCriticalNesting == 1 )
\r
314 configASSERT( ulPortInterruptNesting == 0 );
\r
317 /*-----------------------------------------------------------*/
\r
319 void vPortExitCritical( void )
\r
321 if( ulCriticalNesting > portNO_CRITICAL_NESTING )
\r
323 /* Decrement the nesting count as the critical section is being
\r
325 ulCriticalNesting--;
\r
327 /* If the nesting level has reached zero then all interrupt
\r
328 priorities must be re-enabled. */
\r
329 if( ulCriticalNesting == portNO_CRITICAL_NESTING )
\r
331 /* Critical nesting has reached zero so all interrupt priorities
\r
332 should be unmasked. */
\r
333 portENABLE_INTERRUPTS();
\r
337 /*-----------------------------------------------------------*/
\r
339 void FreeRTOS_Tick_Handler( void )
\r
341 portDISABLE_INTERRUPTS();
\r
343 /* Increment the RTOS tick. */
\r
344 if( xTaskIncrementTick() != pdFALSE )
\r
346 ulPortYieldRequired = pdTRUE;
\r
349 portENABLE_INTERRUPTS();
\r
350 configCLEAR_TICK_INTERRUPT();
\r
352 /*-----------------------------------------------------------*/
\r
354 void vPortTaskUsesFPU( void )
\r
356 uint32_t ulInitialFPSCR = 0;
\r
358 /* A task is registering the fact that it needs an FPU context. Set the
\r
359 FPU flag (which is saved as part of the task context). */
\r
360 ulPortTaskHasFPUContext = pdTRUE;
\r
362 /* Initialise the floating point status register. */
\r
363 __asm( "FMXR FPSCR, %0" :: "r" (ulInitialFPSCR) );
\r
365 /*-----------------------------------------------------------*/
\r