]> git.sur5r.net Git - freertos/blob - FreeRTOS/Demo/Common/Minimal/StaticAllocation.c
Changes to the FreeRTOS code:
[freertos] / FreeRTOS / Demo / Common / Minimal / StaticAllocation.c
1 /*\r
2     FreeRTOS V8.2.3 - Copyright (C) 2015 Real Time Engineers Ltd.\r
3     All rights reserved\r
4 \r
5     VISIT http://www.FreeRTOS.org TO ENSURE YOU ARE USING THE LATEST VERSION.\r
6 \r
7     This file is part of the FreeRTOS distribution.\r
8 \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
12 \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
19 \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
24 \r
25     ***************************************************************************\r
26      *                                                                       *\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
31      *                                                                       *\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
36      *                                                                       *\r
37     ***************************************************************************\r
38 \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
42 \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
46 \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
51 \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
55 \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
58 \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
62 \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
66 \r
67     1 tab == 4 spaces!\r
68 */\r
69 \r
70 \r
71 /*\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
75  *\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
80  * stacks and TCBs.\r
81  */\r
82 \r
83 /* Scheduler include files. */\r
84 #include "FreeRTOS.h"\r
85 #include "task.h"\r
86 \r
87 /* Demo program include files. */\r
88 #include "StaticAllocation.h"\r
89 \r
90 #define staticTASK_PRIORITY             ( tskIDLE_PRIORITY + 2 )\r
91 \r
92 /*\r
93  * A task that is created multiple times, using both statically and dynamically\r
94  * allocated stack and TCB.\r
95  */\r
96 static void prvStaticallyAllocatedTask( void *pvParameters );\r
97 \r
98 /*\r
99  * The task that creates and deletes the prvStaticallyAllocatedTask() task,\r
100  * using various priorities, and sometimes with statically and sometimes\r
101  * dynamically allocated stack and TCB.\r
102  */\r
103 static void prvStaticallyAllocatedTaskCreator( void *pvParameters );\r
104 \r
105 /*\r
106  * Utility function to create pseudo random numbers.\r
107  */\r
108 static UBaseType_t prvRand( void );\r
109 \r
110 /*\r
111  * The task that creates and deletes other tasks has to delay occasionally to\r
112  * ensure lower priority tasks are not starved of processing time.  A pseudo\r
113  * random delay time is used just to add a little bit of randomisation into the\r
114  * execution pattern.  prvGetNextDelayTime() generates the pseudo random delay.\r
115  */\r
116 static TickType_t prvGetNextDelayTime( void );\r
117 \r
118 /*-----------------------------------------------------------*/\r
119 \r
120 /* DummyTCB_t is a publicly accessible structure that has the same size and\r
121 alignment requirements as the real TCB structure.  It is provided as a mechanism\r
122 for applications to know the size of the TCB (which is dependent on the\r
123 architecture and configuration file settings) without breaking the strict data\r
124 hiding policy by exposing the real TCB.  This DummyTCB_t variable is passed into\r
125 the xTaskCreateStatic() function, and will hold the task's TCB. */\r
126 static DummyTCB_t xTCBBuffer;\r
127 \r
128 /* This is the stack that will be used by the task.  The alignment requirements\r
129 for the stack depend on the architecture, and the method of forcing an alignment\r
130 is dependent on the compiler, but any bad alignment is corrected inside the\r
131 FreeRTOS code. */\r
132 static StackType_t uxStackBuffer[ configMINIMAL_STACK_SIZE ];\r
133 \r
134 /* Used by the pseudo random number generating function. */\r
135 static uint32_t ulNextRand = 0;\r
136 \r
137 /* Used so a check task can ensure this test is still executing, and not\r
138 stalled. */\r
139 static volatile UBaseType_t uxCycleCounter = 0;\r
140 \r
141 /*-----------------------------------------------------------*/\r
142 \r
143 void vStartStaticallyAllocatedTasks( void  )\r
144 {\r
145         /* Create a single task, which then repeatedly creates and deletes the\r
146         task implemented by prvStaticallyAllocatedTask() at various different\r
147         priorities, and both with and without statically allocated TCB and stack. */\r
148         xTaskCreate( prvStaticallyAllocatedTaskCreator, "StatCreate", configMINIMAL_STACK_SIZE, NULL, staticTASK_PRIORITY, NULL );\r
149 \r
150         /* Pseudo seed the random number generator. */\r
151         ulNextRand = ( uint32_t ) prvRand;\r
152 }\r
153 /*-----------------------------------------------------------*/\r
154 \r
155 static void prvStaticallyAllocatedTaskCreator( void *pvParameters )\r
156 {\r
157 TaskHandle_t xCreatedTask;\r
158 BaseType_t xReturned;\r
159 \r
160         /* Avoid compiler warnings. */\r
161         ( void ) pvParameters;\r
162 \r
163         for( ;; )\r
164         {\r
165                 /* Create the task.  xTaskCreateStatic() has two more parameters than\r
166                 the usual xTaskCreate() function.  The first new parameter is a pointer to\r
167                 the pre-allocated stack.  The second new parameter is a pointer to the\r
168                 DummyTCB_t structure that will hold the task's TCB.  If either pointer is\r
169                 passed as NULL then the respective object will be allocated dynamically as\r
170                 if xTaskCreate() had been called. */\r
171                 xReturned = xTaskCreateStatic(\r
172                                                         prvStaticallyAllocatedTask, /* Function that implements the task. */\r
173                                                         "Static",                                       /* Human readable name for the task. */\r
174                                                         configMINIMAL_STACK_SIZE,       /* Task's stack size, in words (not bytes!). */\r
175                                                         NULL,                                           /* Parameter to pass into the task. */\r
176                                                         tskIDLE_PRIORITY,                       /* The priority of the task. */\r
177                                                         &xCreatedTask,                          /* Handle of the task being created. */\r
178                                                         &( uxStackBuffer[ 0 ] ),        /* The buffer to use as the task's stack. */\r
179                                                         &xTCBBuffer );                          /* The variable that will hold that task's TCB. */\r
180 \r
181                 /* Check the task was created correctly, then delete the task. */\r
182                 configASSERT( xReturned == pdPASS );\r
183                 ( void ) xReturned; /* In case configASSERT() is not defined. */\r
184                 vTaskDelete( xCreatedTask );\r
185 \r
186                 /* Ensure lower priority tasks get CPU time. */\r
187                 vTaskDelay( prvGetNextDelayTime() );\r
188 \r
189                 /* Create and delete the task a few times again - testing both static and\r
190                 dynamic allocation for the stack and TCB. */\r
191                 xReturned = xTaskCreateStatic(\r
192                                                         prvStaticallyAllocatedTask, /* Function that implements the task. */\r
193                                                         "Static",                                       /* Human readable name for the task. */\r
194                                                         configMINIMAL_STACK_SIZE,       /* Task's stack size, in words (not bytes!). */\r
195                                                         NULL,                                           /* Parameter to pass into the task. */\r
196                                                         staticTASK_PRIORITY + 1,        /* The priority of the task. */\r
197                                                         &xCreatedTask,                          /* Handle of the task being created. */\r
198                                                         NULL,                                           /* This time, dynamically allocate the stack. */\r
199                                                         &xTCBBuffer );                          /* The variable that will hold that task's TCB. */\r
200 \r
201                 configASSERT( xReturned == pdPASS );\r
202                 ( void ) xReturned; /* In case configASSERT() is not defined. */\r
203                 vTaskDelete( xCreatedTask );\r
204 \r
205                 /* Just to show the check task that this task is still executing. */\r
206                 uxCycleCounter++;\r
207 \r
208                 /* Ensure lower priority tasks get CPU time. */\r
209                 vTaskDelay( prvGetNextDelayTime() );\r
210 \r
211                 xReturned = xTaskCreateStatic(\r
212                                                         prvStaticallyAllocatedTask, /* Function that implements the task. */\r
213                                                         "Static",                                       /* Human readable name for the task. */\r
214                                                         configMINIMAL_STACK_SIZE,       /* Task's stack size, in words (not bytes!). */\r
215                                                         NULL,                                           /* Parameter to pass into the task. */\r
216                                                         staticTASK_PRIORITY - 1,        /* The priority of the task. */\r
217                                                         &xCreatedTask,                          /* Handle of the task being created. */\r
218                                                         &( uxStackBuffer[ 0 ] ),        /* The buffer to use as the task's stack. */\r
219                                                         NULL );                                         /* This time dynamically allocate the TCB. */\r
220 \r
221                 configASSERT( xReturned == pdPASS );\r
222                 ( void ) xReturned; /* In case configASSERT() is not defined. */\r
223                 vTaskDelete( xCreatedTask );\r
224 \r
225                 /* Ensure lower priority tasks get CPU time. */\r
226                 vTaskDelay( prvGetNextDelayTime() );\r
227 \r
228                 xReturned = xTaskCreateStatic(\r
229                                                         prvStaticallyAllocatedTask, /* Function that implements the task. */\r
230                                                         "Static",                                       /* Human readable name for the task. */\r
231                                                         configMINIMAL_STACK_SIZE,       /* Task's stack size, in words (not bytes!). */\r
232                                                         NULL,                                           /* Parameter to pass into the task. */\r
233                                                         staticTASK_PRIORITY,            /* The priority of the task. */\r
234                                                         &xCreatedTask,                          /* Handle of the task being created. */\r
235                                                         NULL,                                           /* This time dynamically allocate the stack and TCB. */\r
236                                                         NULL );                                         /* This time dynamically allocate the stack and TCB. */\r
237 \r
238                 configASSERT( xReturned == pdPASS );\r
239                 ( void ) xReturned; /* In case configASSERT() is not defined. */\r
240                 vTaskDelete( xCreatedTask );\r
241 \r
242                 /* Ensure lower priority tasks get CPU time. */\r
243                 vTaskDelay( prvGetNextDelayTime() );\r
244 \r
245                 /* Just to show the check task that this task is still executing. */\r
246                 uxCycleCounter++;\r
247         }\r
248 }\r
249 /*-----------------------------------------------------------*/\r
250 \r
251 static void prvStaticallyAllocatedTask( void *pvParameters )\r
252 {\r
253         ( void ) pvParameters;\r
254 \r
255         /* The created task doesn't do anything - just waits to get deleted. */\r
256         vTaskSuspend( NULL );\r
257 }\r
258 /*-----------------------------------------------------------*/\r
259 \r
260 static UBaseType_t prvRand( void )\r
261 {\r
262 const uint32_t ulMultiplier = 0x015a4e35UL, ulIncrement = 1UL;\r
263 \r
264         /* Utility function to generate a pseudo random number. */\r
265         ulNextRand = ( ulMultiplier * ulNextRand ) + ulIncrement;\r
266         return( ( ulNextRand >> 16UL ) & 0x7fffUL );\r
267 }\r
268 /*-----------------------------------------------------------*/\r
269 \r
270 static TickType_t prvGetNextDelayTime( void )\r
271 {\r
272 TickType_t xNextDelay;\r
273 const TickType_t xMaxDelay = pdMS_TO_TICKS( ( TickType_t ) 150 );\r
274 const TickType_t xMinDelay = pdMS_TO_TICKS( ( TickType_t ) 75 );\r
275 const TickType_t xTinyDelay = pdMS_TO_TICKS( ( TickType_t ) 2 );\r
276 \r
277         /* Generate the next delay time.  This is kept within a narrow band so as\r
278         not to disturb the timing of other tests - but does add in some pseudo\r
279         randomisation into the tests. */\r
280         do\r
281         {\r
282                 xNextDelay = prvRand() % xMaxDelay;\r
283 \r
284                 /* Just in case this loop is executed lots of times. */\r
285                 vTaskDelay( xTinyDelay );\r
286 \r
287         } while ( xNextDelay < xMinDelay );\r
288 \r
289         return xNextDelay;\r
290 }\r
291 /*-----------------------------------------------------------*/\r
292 \r
293 BaseType_t xAreStaticAllocationTasksStillRunning( void )\r
294 {\r
295 static UBaseType_t uxLastCycleCounter = 0;\r
296 BaseType_t xReturn;\r
297 \r
298         if( uxCycleCounter == uxLastCycleCounter )\r
299         {\r
300                 xReturn = pdFAIL;\r
301         }\r
302         else\r
303         {\r
304                 xReturn = pdPASS;\r
305                 uxLastCycleCounter = uxCycleCounter;\r
306         }\r
307 \r
308         return xReturn;\r
309 }\r
310 \r