]> git.sur5r.net Git - freertos/blob - Demo/Common/Minimal/dynamic.c
Add volatile qualifier to loop counters used to detect stalled tasks.
[freertos] / Demo / Common / Minimal / dynamic.c
1 /*
2         FreeRTOS.org V5.0.0 - Copyright (C) 2003-2008 Richard Barry.
3
4         This file is part of the FreeRTOS.org distribution.
5
6         FreeRTOS.org is free software; you can redistribute it and/or modify
7         it under the terms of the GNU General Public License as published by
8         the Free Software Foundation; either version 2 of the License, or
9         (at your option) any later version.
10
11         FreeRTOS.org is distributed in the hope that it will be useful,
12         but WITHOUT ANY WARRANTY; without even the implied warranty of
13         MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14         GNU General Public License for more details.
15
16         You should have received a copy of the GNU General Public License
17         along with FreeRTOS.org; if not, write to the Free Software
18         Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19
20         A special exception to the GPL can be applied should you wish to distribute
21         a combined work that includes FreeRTOS.org, without being obliged to provide
22         the source code for any proprietary components.  See the licensing section 
23         of http://www.FreeRTOS.org for full details of how and when the exception
24         can be applied.
25
26     ***************************************************************************
27     ***************************************************************************
28     *                                                                         *
29     * SAVE TIME AND MONEY!  We can port FreeRTOS.org to your own hardware,    *
30     * and even write all or part of your application on your behalf.          *
31     * See http://www.OpenRTOS.com for details of the services we provide to   *
32     * expedite your project.                                                  *
33     *                                                                         *
34     ***************************************************************************
35     ***************************************************************************
36
37         Please ensure to read the configuration and relevant port sections of the
38         online documentation.
39
40         http://www.FreeRTOS.org - Documentation, latest information, license and 
41         contact details.
42
43         http://www.SafeRTOS.com - A version that is certified for use in safety 
44         critical systems.
45
46         http://www.OpenRTOS.com - Commercial support, development, porting, 
47         licensing and training services.
48 */
49
50 /*
51  * The first test creates three tasks - two counter tasks (one continuous count 
52  * and one limited count) and one controller.  A "count" variable is shared 
53  * between all three tasks.  The two counter tasks should never be in a "ready" 
54  * state at the same time.  The controller task runs at the same priority as 
55  * the continuous count task, and at a lower priority than the limited count 
56  * task.
57  *
58  * One counter task loops indefinitely, incrementing the shared count variable
59  * on each iteration.  To ensure it has exclusive access to the variable it
60  * raises it's priority above that of the controller task before each 
61  * increment, lowering it again to it's original priority before starting the
62  * next iteration.
63  *
64  * The other counter task increments the shared count variable on each
65  * iteration of it's loop until the count has reached a limit of 0xff - at
66  * which point it suspends itself.  It will not start a new loop until the 
67  * controller task has made it "ready" again by calling vTaskResume ().  
68  * This second counter task operates at a higher priority than controller 
69  * task so does not need to worry about mutual exclusion of the counter 
70  * variable.
71  *
72  * The controller task is in two sections.  The first section controls and
73  * monitors the continuous count task.  When this section is operational the 
74  * limited count task is suspended.  Likewise, the second section controls 
75  * and monitors the limited count task.  When this section is operational the 
76  * continuous count task is suspended.
77  *
78  * In the first section the controller task first takes a copy of the shared
79  * count variable.  To ensure mutual exclusion on the count variable it
80  * suspends the continuous count task, resuming it again when the copy has been
81  * taken.  The controller task then sleeps for a fixed period - during which
82  * the continuous count task will execute and increment the shared variable.
83  * When the controller task wakes it checks that the continuous count task
84  * has executed by comparing the copy of the shared variable with its current
85  * value.  This time, to ensure mutual exclusion, the scheduler itself is 
86  * suspended with a call to vTaskSuspendAll ().  This is for demonstration 
87  * purposes only and is not a recommended technique due to its inefficiency.
88  *
89  * After a fixed number of iterations the controller task suspends the 
90  * continuous count task, and moves on to its second section.
91  *
92  * At the start of the second section the shared variable is cleared to zero.
93  * The limited count task is then woken from it's suspension by a call to
94  * vTaskResume ().  As this counter task operates at a higher priority than
95  * the controller task the controller task should not run again until the
96  * shared variable has been counted up to the limited value causing the counter
97  * task to suspend itself.  The next line after vTaskResume () is therefore
98  * a check on the shared variable to ensure everything is as expected.
99  *
100  *
101  * The second test consists of a couple of very simple tasks that post onto a 
102  * queue while the scheduler is suspended.  This test was added to test parts
103  * of the scheduler not exercised by the first test.
104  *
105  */
106
107 #include <stdlib.h>
108
109 /* Scheduler include files. */
110 #include "FreeRTOS.h"
111 #include "task.h"
112 #include "semphr.h"
113
114 /* Demo app include files. */
115 #include "dynamic.h"
116
117 /* Function that implements the "limited count" task as described above. */
118 static portTASK_FUNCTION_PROTO( vLimitedIncrementTask, pvParameters );
119
120 /* Function that implements the "continuous count" task as described above. */
121 static portTASK_FUNCTION_PROTO( vContinuousIncrementTask, pvParameters );
122
123 /* Function that implements the controller task as described above. */
124 static portTASK_FUNCTION_PROTO( vCounterControlTask, pvParameters );
125
126 static portTASK_FUNCTION_PROTO( vQueueReceiveWhenSuspendedTask, pvParameters );
127 static portTASK_FUNCTION_PROTO( vQueueSendWhenSuspendedTask, pvParameters );
128
129 /* Demo task specific constants. */
130 #define priSTACK_SIZE                           ( configMINIMAL_STACK_SIZE )
131 #define priSLEEP_TIME                           ( ( portTickType ) 128 / portTICK_RATE_MS )
132 #define priLOOPS                                        ( 5 )
133 #define priMAX_COUNT                            ( ( unsigned portLONG ) 0xff )
134 #define priNO_BLOCK                                     ( ( portTickType ) 0 )
135 #define priSUSPENDED_QUEUE_LENGTH       ( 1 )
136
137 /*-----------------------------------------------------------*/
138
139 /* Handles to the two counter tasks.  These could be passed in as parameters
140 to the controller task to prevent them having to be file scope. */
141 static xTaskHandle xContinousIncrementHandle, xLimitedIncrementHandle;
142
143 /* The shared counter variable.  This is passed in as a parameter to the two 
144 counter variables for demonstration purposes. */
145 static unsigned portLONG ulCounter;
146
147 /* Variables used to check that the tasks are still operating without error.
148 Each complete iteration of the controller task increments this variable
149 provided no errors have been found.  The variable maintaining the same value
150 is therefore indication of an error. */
151 static volatile unsigned portSHORT usCheckVariable = ( unsigned portSHORT ) 0;
152 static volatile portBASE_TYPE xSuspendedQueueSendError = pdFALSE;
153 static volatile portBASE_TYPE xSuspendedQueueReceiveError = pdFALSE;
154
155 /* Queue used by the second test. */
156 xQueueHandle xSuspendedTestQueue;
157
158 /*-----------------------------------------------------------*/
159 /*
160  * Start the three tasks as described at the top of the file.
161  * Note that the limited count task is given a higher priority.
162  */
163 void vStartDynamicPriorityTasks( void )
164 {
165         xSuspendedTestQueue = xQueueCreate( priSUSPENDED_QUEUE_LENGTH, sizeof( unsigned portLONG ) );
166         xTaskCreate( vContinuousIncrementTask, ( signed portCHAR * ) "CNT_INC", priSTACK_SIZE, ( void * ) &ulCounter, tskIDLE_PRIORITY, &xContinousIncrementHandle );
167         xTaskCreate( vLimitedIncrementTask, ( signed portCHAR * ) "LIM_INC", priSTACK_SIZE, ( void * ) &ulCounter, tskIDLE_PRIORITY + 1, &xLimitedIncrementHandle );
168         xTaskCreate( vCounterControlTask, ( signed portCHAR * ) "C_CTRL", priSTACK_SIZE, NULL, tskIDLE_PRIORITY, NULL );
169         xTaskCreate( vQueueSendWhenSuspendedTask, ( signed portCHAR * ) "SUSP_TX", priSTACK_SIZE, NULL, tskIDLE_PRIORITY, NULL );
170         xTaskCreate( vQueueReceiveWhenSuspendedTask, ( signed portCHAR * ) "SUSP_RX", priSTACK_SIZE, NULL, tskIDLE_PRIORITY, NULL );
171 }
172 /*-----------------------------------------------------------*/
173
174 /*
175  * Just loops around incrementing the shared variable until the limit has been
176  * reached.  Once the limit has been reached it suspends itself. 
177  */
178 static portTASK_FUNCTION( vLimitedIncrementTask, pvParameters )
179 {
180 unsigned portLONG *pulCounter;
181
182         /* Take a pointer to the shared variable from the parameters passed into
183         the task. */
184         pulCounter = ( unsigned portLONG * ) pvParameters;
185
186         /* This will run before the control task, so the first thing it does is
187         suspend - the control task will resume it when ready. */
188         vTaskSuspend( NULL );
189
190         for( ;; )
191         {
192                 /* Just count up to a value then suspend. */
193                 ( *pulCounter )++;      
194                 
195                 if( *pulCounter >= priMAX_COUNT )
196                 {
197                         vTaskSuspend( NULL );
198                 }       
199         }
200 }
201 /*-----------------------------------------------------------*/
202
203 /*
204  * Just keep counting the shared variable up.  The control task will suspend
205  * this task when it wants.
206  */
207 static portTASK_FUNCTION( vContinuousIncrementTask, pvParameters )
208 {
209 unsigned portLONG *pulCounter;
210 unsigned portBASE_TYPE uxOurPriority;
211
212         /* Take a pointer to the shared variable from the parameters passed into
213         the task. */
214         pulCounter = ( unsigned portLONG * ) pvParameters;
215
216         /* Query our priority so we can raise it when exclusive access to the 
217         shared variable is required. */
218         uxOurPriority = uxTaskPriorityGet( NULL );
219
220         for( ;; )
221         {
222                 /* Raise our priority above the controller task to ensure a context
223                 switch does not occur while we are accessing this variable. */
224                 vTaskPrioritySet( NULL, uxOurPriority + 1 );
225                         ( *pulCounter )++;              
226                 vTaskPrioritySet( NULL, uxOurPriority );
227         }
228 }
229 /*-----------------------------------------------------------*/
230
231 /*
232  * Controller task as described above.
233  */
234 static portTASK_FUNCTION( vCounterControlTask, pvParameters )
235 {
236 unsigned portLONG ulLastCounter;
237 portSHORT sLoops;
238 portSHORT sError = pdFALSE;
239
240         /* Just to stop warning messages. */
241         ( void ) pvParameters;
242
243         for( ;; )
244         {
245                 /* Start with the counter at zero. */
246                 ulCounter = ( unsigned portLONG ) 0;
247
248                 /* First section : */
249
250                 /* Check the continuous count task is running. */
251                 for( sLoops = 0; sLoops < priLOOPS; sLoops++ )
252                 {
253                         /* Suspend the continuous count task so we can take a mirror of the
254                         shared variable without risk of corruption. */
255                         vTaskSuspend( xContinousIncrementHandle );
256                                 ulLastCounter = ulCounter;
257                         vTaskResume( xContinousIncrementHandle );
258                         
259                         /* Now delay to ensure the other task has processor time. */
260                         vTaskDelay( priSLEEP_TIME );
261
262                         /* Check the shared variable again.  This time to ensure mutual 
263                         exclusion the whole scheduler will be locked.  This is just for
264                         demo purposes! */
265                         vTaskSuspendAll();
266                         {
267                                 if( ulLastCounter == ulCounter )
268                                 {
269                                         /* The shared variable has not changed.  There is a problem
270                                         with the continuous count task so flag an error. */
271                                         sError = pdTRUE;
272                                 }
273                         }
274                         xTaskResumeAll();
275                 }
276
277
278                 /* Second section: */
279
280                 /* Suspend the continuous counter task so it stops accessing the shared variable. */
281                 vTaskSuspend( xContinousIncrementHandle );
282
283                 /* Reset the variable. */
284                 ulCounter = ( unsigned portLONG ) 0;
285
286                 /* Resume the limited count task which has a higher priority than us.
287                 We should therefore not return from this call until the limited count
288                 task has suspended itself with a known value in the counter variable. */
289                 vTaskResume( xLimitedIncrementHandle );
290
291                 /* Does the counter variable have the expected value? */
292                 if( ulCounter != priMAX_COUNT )
293                 {
294                         sError = pdTRUE;
295                 }
296
297                 if( sError == pdFALSE )
298                 {
299                         /* If no errors have occurred then increment the check variable. */
300                         portENTER_CRITICAL();
301                                 usCheckVariable++;
302                         portEXIT_CRITICAL();
303                 }
304
305                 /* Resume the continuous count task and do it all again. */
306                 vTaskResume( xContinousIncrementHandle );
307         }
308 }
309 /*-----------------------------------------------------------*/
310
311 static portTASK_FUNCTION( vQueueSendWhenSuspendedTask, pvParameters )
312 {
313 static unsigned portLONG ulValueToSend = ( unsigned portLONG ) 0;
314
315         /* Just to stop warning messages. */
316         ( void ) pvParameters;
317
318         for( ;; )
319         {
320                 vTaskSuspendAll();
321                 {
322                         /* We must not block while the scheduler is suspended! */
323                         if( xQueueSend( xSuspendedTestQueue, ( void * ) &ulValueToSend, priNO_BLOCK ) != pdTRUE )
324                         {
325                                 xSuspendedQueueSendError = pdTRUE;
326                         }
327                 }
328                 xTaskResumeAll();
329
330                 vTaskDelay( priSLEEP_TIME );
331
332                 ++ulValueToSend;
333         }
334 }
335 /*-----------------------------------------------------------*/
336
337 static portTASK_FUNCTION( vQueueReceiveWhenSuspendedTask, pvParameters )
338 {
339 static unsigned portLONG ulExpectedValue = ( unsigned portLONG ) 0, ulReceivedValue;
340 portBASE_TYPE xGotValue;
341
342         /* Just to stop warning messages. */
343         ( void ) pvParameters;
344
345         for( ;; )
346         {
347                 do
348                 {
349                         /* Suspending the scheduler here is fairly pointless and 
350                         undesirable for a normal application.  It is done here purely
351                         to test the scheduler.  The inner xTaskResumeAll() should
352                         never return pdTRUE as the scheduler is still locked by the
353                         outer call. */
354                         vTaskSuspendAll();
355                         {
356                                 vTaskSuspendAll();
357                                 {
358                                         xGotValue = xQueueReceive( xSuspendedTestQueue, ( void * ) &ulReceivedValue, priNO_BLOCK );
359                                 }
360                                 if( xTaskResumeAll() )
361                                 {
362                                         xSuspendedQueueReceiveError = pdTRUE;
363                                 }
364                         }
365                         xTaskResumeAll();
366
367                         #if configUSE_PREEMPTION == 0
368                         {
369                                 taskYIELD();
370                         }
371                         #endif
372
373                 } while( xGotValue == pdFALSE );
374
375                 if( ulReceivedValue != ulExpectedValue )
376                 {
377                         xSuspendedQueueReceiveError = pdTRUE;
378                 }
379
380                 ++ulExpectedValue;
381         }
382 }
383 /*-----------------------------------------------------------*/
384
385 /* Called to check that all the created tasks are still running without error. */
386 portBASE_TYPE xAreDynamicPriorityTasksStillRunning( void )
387 {
388 /* Keep a history of the check variables so we know if it has been incremented 
389 since the last call. */
390 static unsigned portSHORT usLastTaskCheck = ( unsigned portSHORT ) 0;
391 portBASE_TYPE xReturn = pdTRUE;
392
393         /* Check the tasks are still running by ensuring the check variable
394         is still incrementing. */
395
396         if( usCheckVariable == usLastTaskCheck )
397         {
398                 /* The check has not incremented so an error exists. */
399                 xReturn = pdFALSE;
400         }
401
402         if( xSuspendedQueueSendError == pdTRUE )
403         {
404                 xReturn = pdFALSE;
405         }
406
407         if( xSuspendedQueueReceiveError == pdTRUE )
408         {
409                 xReturn = pdFALSE;
410         }
411
412         usLastTaskCheck = usCheckVariable;
413         return xReturn;
414 }