2 FreeRTOS V8.2.3 - Copyright (C) 2015 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 ***************************************************************************
\r
14 >>! NOTE: The modification to the GPL is included to allow you to !<<
\r
15 >>! distribute a combined work that includes FreeRTOS without being !<<
\r
16 >>! obliged to provide the source code for proprietary components !<<
\r
17 >>! outside of the FreeRTOS kernel. !<<
\r
18 ***************************************************************************
\r
20 FreeRTOS is distributed in the hope that it will be useful, but WITHOUT ANY
\r
21 WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
\r
22 FOR A PARTICULAR PURPOSE. Full license text is available on the following
\r
23 link: http://www.freertos.org/a00114.html
\r
25 ***************************************************************************
\r
27 * FreeRTOS provides completely free yet professionally developed, *
\r
28 * robust, strictly quality controlled, supported, and cross *
\r
29 * platform software that is more than just the market leader, it *
\r
30 * is the industry's de facto standard. *
\r
32 * Help yourself get started quickly while simultaneously helping *
\r
33 * to support the FreeRTOS project by purchasing a FreeRTOS *
\r
34 * tutorial book, reference manual, or both: *
\r
35 * http://www.FreeRTOS.org/Documentation *
\r
37 ***************************************************************************
\r
39 http://www.FreeRTOS.org/FAQHelp.html - Having a problem? Start by reading
\r
40 the FAQ page "My application does not run, what could be wrong?". Have you
\r
41 defined configASSERT()?
\r
43 http://www.FreeRTOS.org/support - In return for receiving this top quality
\r
44 embedded software for free we request you assist our global community by
\r
45 participating in the support forum.
\r
47 http://www.FreeRTOS.org/training - Investing in training allows your team to
\r
48 be as productive as possible as early as possible. Now you can receive
\r
49 FreeRTOS training directly from Richard Barry, CEO of Real Time Engineers
\r
50 Ltd, and the world's leading authority on the world's leading RTOS.
\r
52 http://www.FreeRTOS.org/plus - A selection of FreeRTOS ecosystem products,
\r
53 including FreeRTOS+Trace - an indispensable productivity tool, a DOS
\r
54 compatible FAT file system, and our tiny thread aware UDP/IP stack.
\r
56 http://www.FreeRTOS.org/labs - Where new FreeRTOS products go to incubate.
\r
57 Come and try FreeRTOS+TCP, our new open source TCP/IP stack for FreeRTOS.
\r
59 http://www.OpenRTOS.com - Real Time Engineers ltd. license FreeRTOS to High
\r
60 Integrity Systems ltd. to sell under the OpenRTOS brand. Low cost OpenRTOS
\r
61 licenses offer ticketed support, indemnification and commercial middleware.
\r
63 http://www.SafeRTOS.com - High Integrity Systems also provide a safety
\r
64 engineered and independently SIL3 certified version for use in safety and
\r
65 mission critical applications that require provable dependability.
\r
72 * Demonstrates how to create FreeRTOS objects using pre-allocated memory,
\r
73 * rather than the normal dynamically allocated memory. Currently only tasks
\r
74 * are being allocated statically.
\r
76 * Two buffers are required by a task - one that is used by the task as its
\r
77 * stack, and one that holds the task's control block (TCB).
\r
78 * prvStaticallyAllocatedTaskCreator() creates and deletes tasks with all
\r
79 * possible combinations of statically allocated and dynamically allocated
\r
83 /* Scheduler include files. */
\r
84 #include "FreeRTOS.h"
\r
87 /* Demo program include files. */
\r
88 #include "StaticAllocation.h"
\r
90 /* Exclude the entire file if configSUPPORT_STATIC_ALLOCATION is 0. */
\r
91 #if( configSUPPORT_STATIC_ALLOCATION == 1 )
\r
93 #define staticTASK_PRIORITY ( tskIDLE_PRIORITY + 2 )
\r
96 * A task that is created multiple times, using both statically and dynamically
\r
97 * allocated stack and TCB.
\r
99 static void prvStaticallyAllocatedTask( void *pvParameters );
\r
102 * The task that creates and deletes the prvStaticallyAllocatedTask() task,
\r
103 * using various priorities, and sometimes with statically and sometimes
\r
104 * dynamically allocated stack and TCB.
\r
106 static void prvStaticallyAllocatedTaskCreator( void *pvParameters );
\r
109 * Utility function to create pseudo random numbers.
\r
111 static UBaseType_t prvRand( void );
\r
114 * The task that creates and deletes other tasks has to delay occasionally to
\r
115 * ensure lower priority tasks are not starved of processing time. A pseudo
\r
116 * random delay time is used just to add a little bit of randomisation into the
\r
117 * execution pattern. prvGetNextDelayTime() generates the pseudo random delay.
\r
119 static TickType_t prvGetNextDelayTime( void );
\r
121 /*-----------------------------------------------------------*/
\r
123 /* DummyTCB_t is a publicly accessible structure that has the same size and
\r
124 alignment requirements as the real TCB structure. It is provided as a mechanism
\r
125 for applications to know the size of the TCB (which is dependent on the
\r
126 architecture and configuration file settings) without breaking the strict data
\r
127 hiding policy by exposing the real TCB. This DummyTCB_t variable is passed into
\r
128 the xTaskCreateStatic() function, and will hold the task's TCB. */
\r
129 static DummyTCB_t xTCBBuffer;
\r
131 /* This is the stack that will be used by the task. The alignment requirements
\r
132 for the stack depend on the architecture, and the method of forcing an alignment
\r
133 is dependent on the compiler, but any bad alignment is corrected inside the
\r
135 static StackType_t uxStackBuffer[ configMINIMAL_STACK_SIZE ];
\r
137 /* Used by the pseudo random number generating function. */
\r
138 static uint32_t ulNextRand = 0;
\r
140 /* Used so a check task can ensure this test is still executing, and not
\r
142 static volatile UBaseType_t uxCycleCounter = 0;
\r
144 /*-----------------------------------------------------------*/
\r
146 void vStartStaticallyAllocatedTasks( void )
\r
148 /* Create a single task, which then repeatedly creates and deletes the
\r
149 task implemented by prvStaticallyAllocatedTask() at various different
\r
150 priorities, and both with and without statically allocated TCB and stack. */
\r
151 xTaskCreate( prvStaticallyAllocatedTaskCreator, "StatCreate", configMINIMAL_STACK_SIZE, NULL, staticTASK_PRIORITY, NULL );
\r
153 /* Pseudo seed the random number generator. */
\r
154 ulNextRand = ( uint32_t ) prvRand;
\r
156 /*-----------------------------------------------------------*/
\r
158 static void prvStaticallyAllocatedTaskCreator( void *pvParameters )
\r
160 TaskHandle_t xCreatedTask;
\r
161 BaseType_t xReturned;
\r
163 /* Avoid compiler warnings. */
\r
164 ( void ) pvParameters;
\r
168 /* Create the task. xTaskCreateStatic() has two more parameters than
\r
169 the usual xTaskCreate() function. The first new parameter is a pointer to
\r
170 the pre-allocated stack. The second new parameter is a pointer to the
\r
171 DummyTCB_t structure that will hold the task's TCB. If either pointer is
\r
172 passed as NULL then the respective object will be allocated dynamically as
\r
173 if xTaskCreate() had been called. */
\r
174 xReturned = xTaskCreateStatic(
\r
175 prvStaticallyAllocatedTask, /* Function that implements the task. */
\r
176 "Static", /* Human readable name for the task. */
\r
177 configMINIMAL_STACK_SIZE, /* Task's stack size, in words (not bytes!). */
\r
178 NULL, /* Parameter to pass into the task. */
\r
179 tskIDLE_PRIORITY, /* The priority of the task. */
\r
180 &xCreatedTask, /* Handle of the task being created. */
\r
181 &( uxStackBuffer[ 0 ] ), /* The buffer to use as the task's stack. */
\r
182 &xTCBBuffer ); /* The variable that will hold that task's TCB. */
\r
184 /* Check the task was created correctly, then delete the task. */
\r
185 configASSERT( xReturned == pdPASS );
\r
186 ( void ) xReturned; /* In case configASSERT() is not defined. */
\r
187 vTaskDelete( xCreatedTask );
\r
189 /* Ensure lower priority tasks get CPU time. */
\r
190 vTaskDelay( prvGetNextDelayTime() );
\r
192 /* Create and delete the task a few times again - testing both static and
\r
193 dynamic allocation for the stack and TCB. */
\r
194 xReturned = xTaskCreateStatic(
\r
195 prvStaticallyAllocatedTask, /* Function that implements the task. */
\r
196 "Static", /* Human readable name for the task. */
\r
197 configMINIMAL_STACK_SIZE, /* Task's stack size, in words (not bytes!). */
\r
198 NULL, /* Parameter to pass into the task. */
\r
199 staticTASK_PRIORITY + 1, /* The priority of the task. */
\r
200 &xCreatedTask, /* Handle of the task being created. */
\r
201 NULL, /* This time, dynamically allocate the stack. */
\r
202 &xTCBBuffer ); /* The variable that will hold that task's TCB. */
\r
204 configASSERT( xReturned == pdPASS );
\r
205 ( void ) xReturned; /* In case configASSERT() is not defined. */
\r
206 vTaskDelete( xCreatedTask );
\r
208 /* Just to show the check task that this task is still executing. */
\r
211 /* Ensure lower priority tasks get CPU time. */
\r
212 vTaskDelay( prvGetNextDelayTime() );
\r
214 xReturned = xTaskCreateStatic(
\r
215 prvStaticallyAllocatedTask, /* Function that implements the task. */
\r
216 "Static", /* Human readable name for the task. */
\r
217 configMINIMAL_STACK_SIZE, /* Task's stack size, in words (not bytes!). */
\r
218 NULL, /* Parameter to pass into the task. */
\r
219 staticTASK_PRIORITY - 1, /* The priority of the task. */
\r
220 &xCreatedTask, /* Handle of the task being created. */
\r
221 &( uxStackBuffer[ 0 ] ), /* The buffer to use as the task's stack. */
\r
222 NULL ); /* This time dynamically allocate the TCB. */
\r
224 configASSERT( xReturned == pdPASS );
\r
225 ( void ) xReturned; /* In case configASSERT() is not defined. */
\r
226 vTaskDelete( xCreatedTask );
\r
228 /* Ensure lower priority tasks get CPU time. */
\r
229 vTaskDelay( prvGetNextDelayTime() );
\r
231 xReturned = xTaskCreateStatic(
\r
232 prvStaticallyAllocatedTask, /* Function that implements the task. */
\r
233 "Static", /* Human readable name for the task. */
\r
234 configMINIMAL_STACK_SIZE, /* Task's stack size, in words (not bytes!). */
\r
235 NULL, /* Parameter to pass into the task. */
\r
236 staticTASK_PRIORITY, /* The priority of the task. */
\r
237 &xCreatedTask, /* Handle of the task being created. */
\r
238 NULL, /* This time dynamically allocate the stack and TCB. */
\r
239 NULL ); /* This time dynamically allocate the stack and TCB. */
\r
241 configASSERT( xReturned == pdPASS );
\r
242 ( void ) xReturned; /* In case configASSERT() is not defined. */
\r
243 vTaskDelete( xCreatedTask );
\r
245 /* Ensure lower priority tasks get CPU time. */
\r
246 vTaskDelay( prvGetNextDelayTime() );
\r
248 /* Just to show the check task that this task is still executing. */
\r
252 /*-----------------------------------------------------------*/
\r
254 static void prvStaticallyAllocatedTask( void *pvParameters )
\r
256 ( void ) pvParameters;
\r
258 /* The created task doesn't do anything - just waits to get deleted. */
\r
259 vTaskSuspend( NULL );
\r
261 /*-----------------------------------------------------------*/
\r
263 static UBaseType_t prvRand( void )
\r
265 const uint32_t ulMultiplier = 0x015a4e35UL, ulIncrement = 1UL;
\r
267 /* Utility function to generate a pseudo random number. */
\r
268 ulNextRand = ( ulMultiplier * ulNextRand ) + ulIncrement;
\r
269 return( ( ulNextRand >> 16UL ) & 0x7fffUL );
\r
271 /*-----------------------------------------------------------*/
\r
273 static TickType_t prvGetNextDelayTime( void )
\r
275 TickType_t xNextDelay;
\r
276 const TickType_t xMaxDelay = pdMS_TO_TICKS( ( TickType_t ) 150 );
\r
277 const TickType_t xMinDelay = pdMS_TO_TICKS( ( TickType_t ) 75 );
\r
278 const TickType_t xTinyDelay = pdMS_TO_TICKS( ( TickType_t ) 2 );
\r
280 /* Generate the next delay time. This is kept within a narrow band so as
\r
281 not to disturb the timing of other tests - but does add in some pseudo
\r
282 randomisation into the tests. */
\r
285 xNextDelay = prvRand() % xMaxDelay;
\r
287 /* Just in case this loop is executed lots of times. */
\r
288 vTaskDelay( xTinyDelay );
\r
290 } while ( xNextDelay < xMinDelay );
\r
294 /*-----------------------------------------------------------*/
\r
296 BaseType_t xAreStaticAllocationTasksStillRunning( void )
\r
298 static UBaseType_t uxLastCycleCounter = 0;
\r
299 BaseType_t xReturn;
\r
301 if( uxCycleCounter == uxLastCycleCounter )
\r
308 uxLastCycleCounter = uxCycleCounter;
\r
313 /*-----------------------------------------------------------*/
\r
315 /* Exclude the entire file if configSUPPORT_STATIC_ALLOCATION is 0. */
\r
316 #endif /* configSUPPORT_STATIC_ALLOCATION == 1 */
\r