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