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