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