]> git.sur5r.net Git - freertos/blob - FreeRTOS/Demo/Common/Minimal/dynamic.c
13d9b2fd070ebc665b745979fa7c2c0ef82a475f
[freertos] / FreeRTOS / Demo / Common / Minimal / dynamic.c
1 /*\r
2     FreeRTOS V7.5.2 - 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 its priority above that of the controller task before each\r
76  * increment, lowering it again to its 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 its 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 its 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 xContinuousIncrementHandle, 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 volatile 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, &xContinuousIncrementHandle );\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 volatile 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 the priority above the controller task to ensure a context\r
252                 switch does not occur while the variable is being accessed. */\r
253                 vTaskPrioritySet( NULL, uxOurPriority + 1 );\r
254                 {\r
255                         configASSERT( ( uxTaskPriorityGet( NULL ) == ( uxOurPriority + 1 ) ) );\r
256                         ( *pulCounter )++;\r
257                 }\r
258                 vTaskPrioritySet( NULL, uxOurPriority );\r
259                 configASSERT( ( uxTaskPriorityGet( NULL ) == uxOurPriority ) );\r
260         }\r
261 }\r
262 /*-----------------------------------------------------------*/\r
263 \r
264 /*\r
265  * Controller task as described above.\r
266  */\r
267 static portTASK_FUNCTION( vCounterControlTask, pvParameters )\r
268 {\r
269 unsigned long ulLastCounter;\r
270 short sLoops;\r
271 short sError = pdFALSE;\r
272 \r
273         /* Just to stop warning messages. */\r
274         ( void ) pvParameters;\r
275 \r
276         for( ;; )\r
277         {\r
278                 /* Start with the counter at zero. */\r
279                 ulCounter = ( unsigned long ) 0;\r
280 \r
281                 /* First section : */\r
282 \r
283                 /* Check the continuous count task is running. */\r
284                 for( sLoops = 0; sLoops < priLOOPS; sLoops++ )\r
285                 {\r
286                         /* Suspend the continuous count task so we can take a mirror of the\r
287                         shared variable without risk of corruption.  This is not really\r
288                         needed as the other task raises its priority above this task's\r
289                         priority. */\r
290                         vTaskSuspend( xContinuousIncrementHandle );\r
291                         {\r
292                                 #if( INCLUDE_eTaskGetState == 1 )\r
293                                 {\r
294                                         configASSERT( eTaskGetState( xContinuousIncrementHandle ) == eSuspended );\r
295                                 }\r
296                                 #endif /* INCLUDE_eTaskGetState */\r
297 \r
298                                 ulLastCounter = ulCounter;\r
299                         }\r
300                         vTaskResume( xContinuousIncrementHandle );\r
301 \r
302                         #if( INCLUDE_eTaskGetState == 1 )\r
303                         {\r
304                                 configASSERT( eTaskGetState( xContinuousIncrementHandle ) == eReady );\r
305                         }\r
306                         #endif /* INCLUDE_eTaskGetState */\r
307 \r
308                         /* Now delay to ensure the other task has processor time. */\r
309                         vTaskDelay( priSLEEP_TIME );\r
310 \r
311                         /* Check the shared variable again.  This time to ensure mutual\r
312                         exclusion the whole scheduler will be locked.  This is just for\r
313                         demo purposes! */\r
314                         vTaskSuspendAll();\r
315                         {\r
316                                 if( ulLastCounter == ulCounter )\r
317                                 {\r
318                                         /* The shared variable has not changed.  There is a problem\r
319                                         with the continuous count task so flag an error. */\r
320                                         sError = pdTRUE;\r
321                                 }\r
322                         }\r
323                         xTaskResumeAll();\r
324                 }\r
325 \r
326                 /* Second section: */\r
327 \r
328                 /* Suspend the continuous counter task so it stops accessing the shared\r
329                 variable. */\r
330                 vTaskSuspend( xContinuousIncrementHandle );\r
331 \r
332                 /* Reset the variable. */\r
333                 ulCounter = ( unsigned long ) 0;\r
334 \r
335                 #if( INCLUDE_eTaskGetState == 1 )\r
336                 {\r
337                         configASSERT( eTaskGetState( xLimitedIncrementHandle ) == eSuspended );\r
338                 }\r
339                 #endif /* INCLUDE_eTaskGetState */\r
340 \r
341                 /* Resume the limited count task which has a higher priority than us.\r
342                 We should therefore not return from this call until the limited count\r
343                 task has suspended itself with a known value in the counter variable. */\r
344                 vTaskResume( xLimitedIncrementHandle );\r
345 \r
346                 /* This task should not run again until xLimitedIncrementHandle has\r
347                 suspended itself. */\r
348                 #if( INCLUDE_eTaskGetState == 1 )\r
349                 {\r
350                         configASSERT( eTaskGetState( xLimitedIncrementHandle ) == eSuspended );\r
351                 }\r
352                 #endif /* INCLUDE_eTaskGetState */\r
353 \r
354                 /* Does the counter variable have the expected value? */\r
355                 if( ulCounter != priMAX_COUNT )\r
356                 {\r
357                         sError = pdTRUE;\r
358                 }\r
359 \r
360                 if( sError == pdFALSE )\r
361                 {\r
362                         /* If no errors have occurred then increment the check variable. */\r
363                         portENTER_CRITICAL();\r
364                                 usCheckVariable++;\r
365                         portEXIT_CRITICAL();\r
366                 }\r
367 \r
368                 /* Resume the continuous count task and do it all again. */\r
369                 vTaskResume( xContinuousIncrementHandle );\r
370         }\r
371 }\r
372 /*-----------------------------------------------------------*/\r
373 \r
374 static portTASK_FUNCTION( vQueueSendWhenSuspendedTask, pvParameters )\r
375 {\r
376 static unsigned long ulValueToSend = ( unsigned long ) 0;\r
377 \r
378         /* Just to stop warning messages. */\r
379         ( void ) pvParameters;\r
380 \r
381         for( ;; )\r
382         {\r
383                 vTaskSuspendAll();\r
384                 {\r
385                         /* We must not block while the scheduler is suspended! */\r
386                         if( xQueueSend( xSuspendedTestQueue, ( void * ) &ulValueToSend, priNO_BLOCK ) != pdTRUE )\r
387                         {\r
388                                 xSuspendedQueueSendError = pdTRUE;\r
389                         }\r
390                 }\r
391                 xTaskResumeAll();\r
392 \r
393                 vTaskDelay( priSLEEP_TIME );\r
394 \r
395                 ++ulValueToSend;\r
396         }\r
397 }\r
398 /*-----------------------------------------------------------*/\r
399 \r
400 static portTASK_FUNCTION( vQueueReceiveWhenSuspendedTask, pvParameters )\r
401 {\r
402 unsigned long ulReceivedValue;\r
403 portBASE_TYPE xGotValue;\r
404 \r
405         /* Just to stop warning messages. */\r
406         ( void ) pvParameters;\r
407 \r
408         for( ;; )\r
409         {\r
410                 do\r
411                 {\r
412                         /* Suspending the scheduler here is fairly pointless and\r
413                         undesirable for a normal application.  It is done here purely\r
414                         to test the scheduler.  The inner xTaskResumeAll() should\r
415                         never return pdTRUE as the scheduler is still locked by the\r
416                         outer call. */\r
417                         vTaskSuspendAll();\r
418                         {\r
419                                 vTaskSuspendAll();\r
420                                 {\r
421                                         xGotValue = xQueueReceive( xSuspendedTestQueue, ( void * ) &ulReceivedValue, priNO_BLOCK );\r
422                                 }\r
423                                 if( xTaskResumeAll() != pdFALSE )\r
424                                 {\r
425                                         xSuspendedQueueReceiveError = pdTRUE;\r
426                                 }\r
427                         }\r
428                         xTaskResumeAll();\r
429 \r
430                         #if configUSE_PREEMPTION == 0\r
431                         {\r
432                                 taskYIELD();\r
433                         }\r
434                         #endif\r
435 \r
436                 } while( xGotValue == pdFALSE );\r
437 \r
438                 if( ulReceivedValue != ulExpectedValue )\r
439                 {\r
440                         xSuspendedQueueReceiveError = pdTRUE;\r
441                 }\r
442 \r
443                 if( xSuspendedQueueReceiveError != pdTRUE )\r
444                 {\r
445                         /* Only increment the variable if an error has not occurred.  This\r
446                         allows xAreDynamicPriorityTasksStillRunning() to check for stalled\r
447                         tasks as well as explicit errors. */\r
448                         ++ulExpectedValue;\r
449                 }\r
450         }\r
451 }\r
452 /*-----------------------------------------------------------*/\r
453 \r
454 /* Called to check that all the created tasks are still running without error. */\r
455 portBASE_TYPE xAreDynamicPriorityTasksStillRunning( void )\r
456 {\r
457 /* Keep a history of the check variables so we know if it has been incremented\r
458 since the last call. */\r
459 static unsigned short usLastTaskCheck = ( unsigned short ) 0;\r
460 static unsigned long ulLastExpectedValue = ( unsigned long ) 0U;\r
461 portBASE_TYPE xReturn = pdTRUE;\r
462 \r
463         /* Check the tasks are still running by ensuring the check variable\r
464         is still incrementing. */\r
465 \r
466         if( usCheckVariable == usLastTaskCheck )\r
467         {\r
468                 /* The check has not incremented so an error exists. */\r
469                 xReturn = pdFALSE;\r
470         }\r
471 \r
472         if( ulExpectedValue == ulLastExpectedValue )\r
473         {\r
474                 /* The value being received by the queue receive task has not\r
475                 incremented so an error exists. */\r
476                 xReturn = pdFALSE;\r
477         }\r
478 \r
479         if( xSuspendedQueueSendError == pdTRUE )\r
480         {\r
481                 xReturn = pdFALSE;\r
482         }\r
483 \r
484         if( xSuspendedQueueReceiveError == pdTRUE )\r
485         {\r
486                 xReturn = pdFALSE;\r
487         }\r
488 \r
489         usLastTaskCheck = usCheckVariable;\r
490         ulLastExpectedValue = ulExpectedValue;\r
491 \r
492         return xReturn;\r
493 }\r