]> git.sur5r.net Git - freertos/blob - FreeRTOS/Demo/Common/Minimal/dynamic.c
Minor updates and change version number for V7.5.0 release.
[freertos] / FreeRTOS / Demo / Common / Minimal / dynamic.c
1 /*\r
2     FreeRTOS V7.5.0 - Copyright (C) 2013 Real Time Engineers Ltd.\r
3 \r
4     VISIT http://www.FreeRTOS.org TO ENSURE YOU ARE USING THE LATEST VERSION.\r
5 \r
6     ***************************************************************************\r
7      *                                                                       *\r
8      *    FreeRTOS provides completely free yet professionally developed,    *\r
9      *    robust, strictly quality controlled, supported, and cross          *\r
10      *    platform software that has become a de facto standard.             *\r
11      *                                                                       *\r
12      *    Help yourself get started quickly and support the FreeRTOS         *\r
13      *    project by purchasing a FreeRTOS tutorial book, reference          *\r
14      *    manual, or both from: http://www.FreeRTOS.org/Documentation        *\r
15      *                                                                       *\r
16      *    Thank you!                                                         *\r
17      *                                                                       *\r
18     ***************************************************************************\r
19 \r
20     This file is part of the FreeRTOS distribution.\r
21 \r
22     FreeRTOS is free software; you can redistribute it and/or modify it under\r
23     the terms of the GNU General Public License (version 2) as published by the\r
24     Free Software Foundation >>!AND MODIFIED BY!<< the FreeRTOS exception.\r
25 \r
26     >>! NOTE: The modification to the GPL is included to allow you to distribute\r
27     >>! a combined work that includes FreeRTOS without being obliged to provide\r
28     >>! the source code for proprietary components outside of the FreeRTOS\r
29     >>! kernel.\r
30 \r
31     FreeRTOS is distributed in the hope that it will be useful, but WITHOUT ANY\r
32     WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS\r
33     FOR A PARTICULAR PURPOSE.  Full license text is available from the following\r
34     link: http://www.freertos.org/a00114.html\r
35 \r
36     1 tab == 4 spaces!\r
37 \r
38     ***************************************************************************\r
39      *                                                                       *\r
40      *    Having a problem?  Start by reading the FAQ "My application does   *\r
41      *    not run, what could be wrong?"                                     *\r
42      *                                                                       *\r
43      *    http://www.FreeRTOS.org/FAQHelp.html                               *\r
44      *                                                                       *\r
45     ***************************************************************************\r
46 \r
47     http://www.FreeRTOS.org - Documentation, books, training, latest versions,\r
48     license and Real Time Engineers Ltd. contact details.\r
49 \r
50     http://www.FreeRTOS.org/plus - A selection of FreeRTOS ecosystem products,\r
51     including FreeRTOS+Trace - an indispensable productivity tool, a DOS\r
52     compatible FAT file system, and our tiny thread aware UDP/IP stack.\r
53 \r
54     http://www.OpenRTOS.com - Real Time Engineers ltd license FreeRTOS to High\r
55     Integrity Systems to sell under the OpenRTOS brand.  Low cost OpenRTOS\r
56     licenses offer ticketed support, indemnification and middleware.\r
57 \r
58     http://www.SafeRTOS.com - High Integrity Systems also provide a safety\r
59     engineered and independently SIL3 certified version for use in safety and\r
60     mission critical applications that require provable dependability.\r
61 \r
62     1 tab == 4 spaces!\r
63 */\r
64 \r
65 /*\r
66  * The first test creates three tasks - two counter tasks (one continuous count\r
67  * and one limited count) and one controller.  A "count" variable is shared\r
68  * between all three tasks.  The two counter tasks should never be in a "ready"\r
69  * state at the same time.  The controller task runs at the same priority as\r
70  * the continuous count task, and at a lower priority than the limited count\r
71  * task.\r
72  *\r
73  * One counter task loops indefinitely, incrementing the shared count variable\r
74  * on each iteration.  To ensure it has exclusive access to the variable it\r
75  * raises it's priority above that of the controller task before each\r
76  * increment, lowering it again to it's original priority before starting the\r
77  * next iteration.\r
78  *\r
79  * The other counter task increments the shared count variable on each\r
80  * iteration of it's loop until the count has reached a limit of 0xff - at\r
81  * which point it suspends itself.  It will not start a new loop until the\r
82  * controller task has made it "ready" again by calling vTaskResume ().\r
83  * This second counter task operates at a higher priority than controller\r
84  * task so does not need to worry about mutual exclusion of the counter\r
85  * variable.\r
86  *\r
87  * The controller task is in two sections.  The first section controls and\r
88  * monitors the continuous count task.  When this section is operational the\r
89  * limited count task is suspended.  Likewise, the second section controls\r
90  * and monitors the limited count task.  When this section is operational the\r
91  * continuous count task is suspended.\r
92  *\r
93  * In the first section the controller task first takes a copy of the shared\r
94  * count variable.  To ensure mutual exclusion on the count variable it\r
95  * suspends the continuous count task, resuming it again when the copy has been\r
96  * taken.  The controller task then sleeps for a fixed period - during which\r
97  * the continuous count task will execute and increment the shared variable.\r
98  * When the controller task wakes it checks that the continuous count task\r
99  * has executed by comparing the copy of the shared variable with its current\r
100  * value.  This time, to ensure mutual exclusion, the scheduler itself is\r
101  * suspended with a call to vTaskSuspendAll ().  This is for demonstration\r
102  * purposes only and is not a recommended technique due to its inefficiency.\r
103  *\r
104  * After a fixed number of iterations the controller task suspends the\r
105  * continuous count task, and moves on to its second section.\r
106  *\r
107  * At the start of the second section the shared variable is cleared to zero.\r
108  * The limited count task is then woken from it's suspension by a call to\r
109  * vTaskResume ().  As this counter task operates at a higher priority than\r
110  * the controller task the controller task should not run again until the\r
111  * shared variable has been counted up to the limited value causing the counter\r
112  * task to suspend itself.  The next line after vTaskResume () is therefore\r
113  * a check on the shared variable to ensure everything is as expected.\r
114  *\r
115  *\r
116  * The second test consists of a couple of very simple tasks that post onto a\r
117  * queue while the scheduler is suspended.  This test was added to test parts\r
118  * of the scheduler not exercised by the first test.\r
119  *\r
120  */\r
121 \r
122 #include <stdlib.h>\r
123 \r
124 /* Scheduler include files. */\r
125 #include "FreeRTOS.h"\r
126 #include "task.h"\r
127 #include "semphr.h"\r
128 \r
129 /* Demo app include files. */\r
130 #include "dynamic.h"\r
131 \r
132 /* Function that implements the "limited count" task as described above. */\r
133 static portTASK_FUNCTION_PROTO( vLimitedIncrementTask, pvParameters );\r
134 \r
135 /* Function that implements the "continuous count" task as described above. */\r
136 static portTASK_FUNCTION_PROTO( vContinuousIncrementTask, pvParameters );\r
137 \r
138 /* Function that implements the controller task as described above. */\r
139 static portTASK_FUNCTION_PROTO( vCounterControlTask, pvParameters );\r
140 \r
141 static portTASK_FUNCTION_PROTO( vQueueReceiveWhenSuspendedTask, pvParameters );\r
142 static portTASK_FUNCTION_PROTO( vQueueSendWhenSuspendedTask, pvParameters );\r
143 \r
144 /* Demo task specific constants. */\r
145 #define priSTACK_SIZE                           ( configMINIMAL_STACK_SIZE )\r
146 #define priSLEEP_TIME                           ( ( portTickType ) 128 / portTICK_RATE_MS )\r
147 #define priLOOPS                                        ( 5 )\r
148 #define priMAX_COUNT                            ( ( unsigned long ) 0xff )\r
149 #define priNO_BLOCK                                     ( ( portTickType ) 0 )\r
150 #define priSUSPENDED_QUEUE_LENGTH       ( 1 )\r
151 \r
152 /*-----------------------------------------------------------*/\r
153 \r
154 /* Handles to the two counter tasks.  These could be passed in as parameters\r
155 to the controller task to prevent them having to be file scope. */\r
156 static xTaskHandle xContinousIncrementHandle, xLimitedIncrementHandle;\r
157 \r
158 /* The shared counter variable.  This is passed in as a parameter to the two\r
159 counter variables for demonstration purposes. */\r
160 static unsigned long ulCounter;\r
161 \r
162 /* Variables used to check that the tasks are still operating without error.\r
163 Each complete iteration of the controller task increments this variable\r
164 provided no errors have been found.  The variable maintaining the same value\r
165 is therefore indication of an error. */\r
166 static volatile unsigned short usCheckVariable = ( unsigned short ) 0;\r
167 static volatile portBASE_TYPE xSuspendedQueueSendError = pdFALSE;\r
168 static volatile portBASE_TYPE xSuspendedQueueReceiveError = pdFALSE;\r
169 \r
170 /* Queue used by the second test. */\r
171 xQueueHandle xSuspendedTestQueue;\r
172 \r
173 /* The value the queue receive task expects to receive next.  This is file\r
174 scope so xAreDynamicPriorityTasksStillRunning() can ensure it is still\r
175 incrementing. */\r
176 static unsigned long ulExpectedValue = ( unsigned long ) 0;\r
177 \r
178 /*-----------------------------------------------------------*/\r
179 /*\r
180  * Start the three tasks as described at the top of the file.\r
181  * Note that the limited count task is given a higher priority.\r
182  */\r
183 void vStartDynamicPriorityTasks( void )\r
184 {\r
185         xSuspendedTestQueue = xQueueCreate( priSUSPENDED_QUEUE_LENGTH, sizeof( unsigned long ) );\r
186 \r
187         /* vQueueAddToRegistry() adds the queue to the queue registry, if one is\r
188         in use.  The queue registry is provided as a means for kernel aware\r
189         debuggers to locate queues and has no purpose if a kernel aware debugger\r
190         is not being used.  The call to vQueueAddToRegistry() will be removed\r
191         by the pre-processor if configQUEUE_REGISTRY_SIZE is not defined or is\r
192         defined to be less than 1. */\r
193         vQueueAddToRegistry( xSuspendedTestQueue, ( signed char * ) "Suspended_Test_Queue" );\r
194 \r
195         xTaskCreate( vContinuousIncrementTask, ( signed char * ) "CNT_INC", priSTACK_SIZE, ( void * ) &ulCounter, tskIDLE_PRIORITY, &xContinousIncrementHandle );\r
196         xTaskCreate( vLimitedIncrementTask, ( signed char * ) "LIM_INC", priSTACK_SIZE, ( void * ) &ulCounter, tskIDLE_PRIORITY + 1, &xLimitedIncrementHandle );\r
197         xTaskCreate( vCounterControlTask, ( signed char * ) "C_CTRL", priSTACK_SIZE, NULL, tskIDLE_PRIORITY, NULL );\r
198         xTaskCreate( vQueueSendWhenSuspendedTask, ( signed char * ) "SUSP_TX", priSTACK_SIZE, NULL, tskIDLE_PRIORITY, NULL );\r
199         xTaskCreate( vQueueReceiveWhenSuspendedTask, ( signed char * ) "SUSP_RX", priSTACK_SIZE, NULL, tskIDLE_PRIORITY, NULL );\r
200 }\r
201 /*-----------------------------------------------------------*/\r
202 \r
203 /*\r
204  * Just loops around incrementing the shared variable until the limit has been\r
205  * reached.  Once the limit has been reached it suspends itself.\r
206  */\r
207 static portTASK_FUNCTION( vLimitedIncrementTask, pvParameters )\r
208 {\r
209 unsigned long *pulCounter;\r
210 \r
211         /* Take a pointer to the shared variable from the parameters passed into\r
212         the task. */\r
213         pulCounter = ( unsigned long * ) pvParameters;\r
214 \r
215         /* This will run before the control task, so the first thing it does is\r
216         suspend - the control task will resume it when ready. */\r
217         vTaskSuspend( NULL );\r
218 \r
219         for( ;; )\r
220         {\r
221                 /* Just count up to a value then suspend. */\r
222                 ( *pulCounter )++;\r
223 \r
224                 if( *pulCounter >= priMAX_COUNT )\r
225                 {\r
226                         vTaskSuspend( NULL );\r
227                 }\r
228         }\r
229 }\r
230 /*-----------------------------------------------------------*/\r
231 \r
232 /*\r
233  * Just keep counting the shared variable up.  The control task will suspend\r
234  * this task when it wants.\r
235  */\r
236 static portTASK_FUNCTION( vContinuousIncrementTask, pvParameters )\r
237 {\r
238 unsigned long *pulCounter;\r
239 unsigned portBASE_TYPE uxOurPriority;\r
240 \r
241         /* Take a pointer to the shared variable from the parameters passed into\r
242         the task. */\r
243         pulCounter = ( unsigned long * ) pvParameters;\r
244 \r
245         /* Query our priority so we can raise it when exclusive access to the\r
246         shared variable is required. */\r
247         uxOurPriority = uxTaskPriorityGet( NULL );\r
248 \r
249         for( ;; )\r
250         {\r
251                 /* Raise our priority above the controller task to ensure a context\r
252                 switch does not occur while we are accessing this variable. */\r
253                 vTaskPrioritySet( NULL, uxOurPriority + 1 );\r
254                         ( *pulCounter )++;\r
255                 vTaskPrioritySet( NULL, uxOurPriority );\r
256         }\r
257 }\r
258 /*-----------------------------------------------------------*/\r
259 \r
260 /*\r
261  * Controller task as described above.\r
262  */\r
263 static portTASK_FUNCTION( vCounterControlTask, pvParameters )\r
264 {\r
265 unsigned long ulLastCounter;\r
266 short sLoops;\r
267 short sError = pdFALSE;\r
268 \r
269         /* Just to stop warning messages. */\r
270         ( void ) pvParameters;\r
271 \r
272         for( ;; )\r
273         {\r
274                 /* Start with the counter at zero. */\r
275                 ulCounter = ( unsigned long ) 0;\r
276 \r
277                 /* First section : */\r
278 \r
279                 /* Check the continuous count task is running. */\r
280                 for( sLoops = 0; sLoops < priLOOPS; sLoops++ )\r
281                 {\r
282                         /* Suspend the continuous count task so we can take a mirror of the\r
283                         shared variable without risk of corruption. */\r
284                         vTaskSuspend( xContinousIncrementHandle );\r
285                                 ulLastCounter = ulCounter;\r
286                         vTaskResume( xContinousIncrementHandle );\r
287 \r
288                         /* Now delay to ensure the other task has processor time. */\r
289                         vTaskDelay( priSLEEP_TIME );\r
290 \r
291                         /* Check the shared variable again.  This time to ensure mutual\r
292                         exclusion the whole scheduler will be locked.  This is just for\r
293                         demo purposes! */\r
294                         vTaskSuspendAll();\r
295                         {\r
296                                 if( ulLastCounter == ulCounter )\r
297                                 {\r
298                                         /* The shared variable has not changed.  There is a problem\r
299                                         with the continuous count task so flag an error. */\r
300                                         sError = pdTRUE;\r
301                                 }\r
302                         }\r
303                         xTaskResumeAll();\r
304                 }\r
305 \r
306 \r
307                 /* Second section: */\r
308 \r
309                 /* Suspend the continuous counter task so it stops accessing the shared variable. */\r
310                 vTaskSuspend( xContinousIncrementHandle );\r
311 \r
312                 /* Reset the variable. */\r
313                 ulCounter = ( unsigned long ) 0;\r
314 \r
315                 /* Resume the limited count task which has a higher priority than us.\r
316                 We should therefore not return from this call until the limited count\r
317                 task has suspended itself with a known value in the counter variable. */\r
318                 vTaskResume( xLimitedIncrementHandle );\r
319 \r
320                 /* Does the counter variable have the expected value? */\r
321                 if( ulCounter != priMAX_COUNT )\r
322                 {\r
323                         sError = pdTRUE;\r
324                 }\r
325 \r
326                 if( sError == pdFALSE )\r
327                 {\r
328                         /* If no errors have occurred then increment the check variable. */\r
329                         portENTER_CRITICAL();\r
330                                 usCheckVariable++;\r
331                         portEXIT_CRITICAL();\r
332                 }\r
333 \r
334                 /* Resume the continuous count task and do it all again. */\r
335                 vTaskResume( xContinousIncrementHandle );\r
336         }\r
337 }\r
338 /*-----------------------------------------------------------*/\r
339 \r
340 static portTASK_FUNCTION( vQueueSendWhenSuspendedTask, pvParameters )\r
341 {\r
342 static unsigned long ulValueToSend = ( unsigned long ) 0;\r
343 \r
344         /* Just to stop warning messages. */\r
345         ( void ) pvParameters;\r
346 \r
347         for( ;; )\r
348         {\r
349                 vTaskSuspendAll();\r
350                 {\r
351                         /* We must not block while the scheduler is suspended! */\r
352                         if( xQueueSend( xSuspendedTestQueue, ( void * ) &ulValueToSend, priNO_BLOCK ) != pdTRUE )\r
353                         {\r
354                                 xSuspendedQueueSendError = pdTRUE;\r
355                         }\r
356                 }\r
357                 xTaskResumeAll();\r
358 \r
359                 vTaskDelay( priSLEEP_TIME );\r
360 \r
361                 ++ulValueToSend;\r
362         }\r
363 }\r
364 /*-----------------------------------------------------------*/\r
365 \r
366 static portTASK_FUNCTION( vQueueReceiveWhenSuspendedTask, pvParameters )\r
367 {\r
368 unsigned long ulReceivedValue;\r
369 portBASE_TYPE xGotValue;\r
370 \r
371         /* Just to stop warning messages. */\r
372         ( void ) pvParameters;\r
373 \r
374         for( ;; )\r
375         {\r
376                 do\r
377                 {\r
378                         /* Suspending the scheduler here is fairly pointless and\r
379                         undesirable for a normal application.  It is done here purely\r
380                         to test the scheduler.  The inner xTaskResumeAll() should\r
381                         never return pdTRUE as the scheduler is still locked by the\r
382                         outer call. */\r
383                         vTaskSuspendAll();\r
384                         {\r
385                                 vTaskSuspendAll();\r
386                                 {\r
387                                         xGotValue = xQueueReceive( xSuspendedTestQueue, ( void * ) &ulReceivedValue, priNO_BLOCK );\r
388                                 }\r
389                                 if( xTaskResumeAll() != pdFALSE )\r
390                                 {\r
391                                         xSuspendedQueueReceiveError = pdTRUE;\r
392                                 }\r
393                         }\r
394                         xTaskResumeAll();\r
395 \r
396                         #if configUSE_PREEMPTION == 0\r
397                         {\r
398                                 taskYIELD();\r
399                         }\r
400                         #endif\r
401 \r
402                 } while( xGotValue == pdFALSE );\r
403 \r
404                 if( ulReceivedValue != ulExpectedValue )\r
405                 {\r
406                         xSuspendedQueueReceiveError = pdTRUE;\r
407                 }\r
408 \r
409                 if( xSuspendedQueueReceiveError != pdTRUE )\r
410                 {\r
411                         /* Only increment the variable if an error has not occurred.  This\r
412                         allows xAreDynamicPriorityTasksStillRunning() to check for stalled\r
413                         tasks as well as explicit errors. */\r
414                         ++ulExpectedValue;\r
415                 }\r
416         }\r
417 }\r
418 /*-----------------------------------------------------------*/\r
419 \r
420 /* Called to check that all the created tasks are still running without error. */\r
421 portBASE_TYPE xAreDynamicPriorityTasksStillRunning( void )\r
422 {\r
423 /* Keep a history of the check variables so we know if it has been incremented\r
424 since the last call. */\r
425 static unsigned short usLastTaskCheck = ( unsigned short ) 0;\r
426 static unsigned long ulLastExpectedValue = ( unsigned long ) 0U;\r
427 portBASE_TYPE xReturn = pdTRUE;\r
428 \r
429         /* Check the tasks are still running by ensuring the check variable\r
430         is still incrementing. */\r
431 \r
432         if( usCheckVariable == usLastTaskCheck )\r
433         {\r
434                 /* The check has not incremented so an error exists. */\r
435                 xReturn = pdFALSE;\r
436         }\r
437 \r
438         if( ulExpectedValue == ulLastExpectedValue )\r
439         {\r
440                 /* The value being received by the queue receive task has not\r
441                 incremented so an error exists. */\r
442                 xReturn = pdFALSE;\r
443         }\r
444 \r
445         if( xSuspendedQueueSendError == pdTRUE )\r
446         {\r
447                 xReturn = pdFALSE;\r
448         }\r
449 \r
450         if( xSuspendedQueueReceiveError == pdTRUE )\r
451         {\r
452                 xReturn = pdFALSE;\r
453         }\r
454 \r
455         usLastTaskCheck = usCheckVariable;\r
456         ulLastExpectedValue = ulExpectedValue;\r
457 \r
458         return xReturn;\r
459 }\r