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