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