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