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