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