]> git.sur5r.net Git - freertos/blob - FreeRTOS/Demo/Common/Minimal/dynamic.c
50e5051a152ed27d77c4aee263fe1dd61d3fe9f1
[freertos] / FreeRTOS / Demo / Common / Minimal / dynamic.c
1 /*\r
2     FreeRTOS V8.1.2 - Copyright (C) 2014 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     !<<\r
28     >>!   distribute a combined work that includes FreeRTOS without being   !<<\r
29     >>!   obliged to provide the source code for proprietary components     !<<\r
30     >>!   outside of the FreeRTOS 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                           ( ( TickType_t ) 128 / portTICK_PERIOD_MS )\r
148 #define priLOOPS                                        ( 5 )\r
149 #define priMAX_COUNT                            ( ( uint32_t ) 0xff )\r
150 #define priNO_BLOCK                                     ( ( TickType_t ) 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 TaskHandle_t 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 uint32_t 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 uint16_t usCheckVariable = ( uint16_t ) 0;\r
168 static volatile BaseType_t xSuspendedQueueSendError = pdFALSE;\r
169 static volatile BaseType_t xSuspendedQueueReceiveError = pdFALSE;\r
170 \r
171 /* Queue used by the second test. */\r
172 QueueHandle_t 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 uint32_t ulExpectedValue = ( uint32_t ) 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( uint32_t ) );\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, "Suspended_Test_Queue" );\r
195 \r
196         xTaskCreate( vContinuousIncrementTask, "CNT_INC", priSTACK_SIZE, ( void * ) &ulCounter, tskIDLE_PRIORITY, &xContinuousIncrementHandle );\r
197         xTaskCreate( vLimitedIncrementTask, "LIM_INC", priSTACK_SIZE, ( void * ) &ulCounter, tskIDLE_PRIORITY + 1, &xLimitedIncrementHandle );\r
198         xTaskCreate( vCounterControlTask, "C_CTRL", priSTACK_SIZE, NULL, tskIDLE_PRIORITY, NULL );\r
199         xTaskCreate( vQueueSendWhenSuspendedTask, "SUSP_TX", priSTACK_SIZE, NULL, tskIDLE_PRIORITY, NULL );\r
200         xTaskCreate( vQueueReceiveWhenSuspendedTask, "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 uint32_t *pulCounter;\r
211 \r
212         /* Take a pointer to the shared variable from the parameters passed into\r
213         the task. */\r
214         pulCounter = ( uint32_t * ) 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 uint32_t *pulCounter;\r
240 UBaseType_t uxOurPriority;\r
241 \r
242         /* Take a pointer to the shared variable from the parameters passed into\r
243         the task. */\r
244         pulCounter = ( uint32_t * ) 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 \r
261                 #if( configUSE_PREEMPTION == 0 )\r
262                         taskYIELD();\r
263                 #endif\r
264 \r
265                 configASSERT( ( uxTaskPriorityGet( NULL ) == uxOurPriority ) );\r
266         }\r
267 }\r
268 /*-----------------------------------------------------------*/\r
269 \r
270 /*\r
271  * Controller task as described above.\r
272  */\r
273 static portTASK_FUNCTION( vCounterControlTask, pvParameters )\r
274 {\r
275 uint32_t ulLastCounter;\r
276 short sLoops;\r
277 short sError = pdFALSE;\r
278 \r
279         /* Just to stop warning messages. */\r
280         ( void ) pvParameters;\r
281 \r
282         for( ;; )\r
283         {\r
284                 /* Start with the counter at zero. */\r
285                 ulCounter = ( uint32_t ) 0;\r
286 \r
287                 /* First section : */\r
288 \r
289                 /* Check the continuous count task is running. */\r
290                 for( sLoops = 0; sLoops < priLOOPS; sLoops++ )\r
291                 {\r
292                         /* Suspend the continuous count task so we can take a mirror of the\r
293                         shared variable without risk of corruption.  This is not really\r
294                         needed as the other task raises its priority above this task's\r
295                         priority. */\r
296                         vTaskSuspend( xContinuousIncrementHandle );\r
297                         {\r
298                                 #if( INCLUDE_eTaskGetState == 1 )\r
299                                 {\r
300                                         configASSERT( eTaskGetState( xContinuousIncrementHandle ) == eSuspended );\r
301                                 }\r
302                                 #endif /* INCLUDE_eTaskGetState */\r
303 \r
304                                 ulLastCounter = ulCounter;\r
305                         }\r
306                         vTaskResume( xContinuousIncrementHandle );\r
307 \r
308                         #if( configUSE_PREEMPTION == 0 )\r
309                                 taskYIELD();\r
310                         #endif\r
311 \r
312                         #if( INCLUDE_eTaskGetState == 1 )\r
313                         {\r
314                                 configASSERT( eTaskGetState( xContinuousIncrementHandle ) == eReady );\r
315                         }\r
316                         #endif /* INCLUDE_eTaskGetState */\r
317 \r
318                         /* Now delay to ensure the other task has processor time. */\r
319                         vTaskDelay( priSLEEP_TIME );\r
320 \r
321                         /* Check the shared variable again.  This time to ensure mutual\r
322                         exclusion the whole scheduler will be locked.  This is just for\r
323                         demo purposes! */\r
324                         vTaskSuspendAll();\r
325                         {\r
326                                 if( ulLastCounter == ulCounter )\r
327                                 {\r
328                                         /* The shared variable has not changed.  There is a problem\r
329                                         with the continuous count task so flag an error. */\r
330                                         sError = pdTRUE;\r
331                                 }\r
332                         }\r
333                         xTaskResumeAll();\r
334                 }\r
335 \r
336                 /* Second section: */\r
337 \r
338                 /* Suspend the continuous counter task so it stops accessing the shared\r
339                 variable. */\r
340                 vTaskSuspend( xContinuousIncrementHandle );\r
341 \r
342                 /* Reset the variable. */\r
343                 ulCounter = ( uint32_t ) 0;\r
344 \r
345                 #if( INCLUDE_eTaskGetState == 1 )\r
346                 {\r
347                         configASSERT( eTaskGetState( xLimitedIncrementHandle ) == eSuspended );\r
348                 }\r
349                 #endif /* INCLUDE_eTaskGetState */\r
350 \r
351                 /* Resume the limited count task which has a higher priority than us.\r
352                 We should therefore not return from this call until the limited count\r
353                 task has suspended itself with a known value in the counter variable. */\r
354                 vTaskResume( xLimitedIncrementHandle );\r
355 \r
356                 #if( configUSE_PREEMPTION == 0 )\r
357                         taskYIELD();\r
358                 #endif\r
359 \r
360                 /* This task should not run again until xLimitedIncrementHandle has\r
361                 suspended itself. */\r
362                 #if( INCLUDE_eTaskGetState == 1 )\r
363                 {\r
364                         configASSERT( eTaskGetState( xLimitedIncrementHandle ) == eSuspended );\r
365                 }\r
366                 #endif /* INCLUDE_eTaskGetState */\r
367 \r
368                 /* Does the counter variable have the expected value? */\r
369                 if( ulCounter != priMAX_COUNT )\r
370                 {\r
371                         sError = pdTRUE;\r
372                 }\r
373 \r
374                 if( sError == pdFALSE )\r
375                 {\r
376                         /* If no errors have occurred then increment the check variable. */\r
377                         portENTER_CRITICAL();\r
378                                 usCheckVariable++;\r
379                         portEXIT_CRITICAL();\r
380                 }\r
381 \r
382                 /* Resume the continuous count task and do it all again. */\r
383                 vTaskResume( xContinuousIncrementHandle );\r
384 \r
385                 #if( configUSE_PREEMPTION == 0 )\r
386                         taskYIELD();\r
387                 #endif\r
388         }\r
389 }\r
390 /*-----------------------------------------------------------*/\r
391 \r
392 static portTASK_FUNCTION( vQueueSendWhenSuspendedTask, pvParameters )\r
393 {\r
394 static uint32_t ulValueToSend = ( uint32_t ) 0;\r
395 \r
396         /* Just to stop warning messages. */\r
397         ( void ) pvParameters;\r
398 \r
399         for( ;; )\r
400         {\r
401                 vTaskSuspendAll();\r
402                 {\r
403                         /* We must not block while the scheduler is suspended! */\r
404                         if( xQueueSend( xSuspendedTestQueue, ( void * ) &ulValueToSend, priNO_BLOCK ) != pdTRUE )\r
405                         {\r
406                                 xSuspendedQueueSendError = pdTRUE;\r
407                         }\r
408                 }\r
409                 xTaskResumeAll();\r
410 \r
411                 vTaskDelay( priSLEEP_TIME );\r
412 \r
413                 ++ulValueToSend;\r
414         }\r
415 }\r
416 /*-----------------------------------------------------------*/\r
417 \r
418 static portTASK_FUNCTION( vQueueReceiveWhenSuspendedTask, pvParameters )\r
419 {\r
420 uint32_t ulReceivedValue;\r
421 BaseType_t xGotValue;\r
422 \r
423         /* Just to stop warning messages. */\r
424         ( void ) pvParameters;\r
425 \r
426         for( ;; )\r
427         {\r
428                 do\r
429                 {\r
430                         /* Suspending the scheduler here is fairly pointless and\r
431                         undesirable for a normal application.  It is done here purely\r
432                         to test the scheduler.  The inner xTaskResumeAll() should\r
433                         never return pdTRUE as the scheduler is still locked by the\r
434                         outer call. */\r
435                         vTaskSuspendAll();\r
436                         {\r
437                                 vTaskSuspendAll();\r
438                                 {\r
439                                         xGotValue = xQueueReceive( xSuspendedTestQueue, ( void * ) &ulReceivedValue, priNO_BLOCK );\r
440                                 }\r
441                                 if( xTaskResumeAll() != pdFALSE )\r
442                                 {\r
443                                         xSuspendedQueueReceiveError = pdTRUE;\r
444                                 }\r
445                         }\r
446                         xTaskResumeAll();\r
447 \r
448                         #if configUSE_PREEMPTION == 0\r
449                         {\r
450                                 taskYIELD();\r
451                         }\r
452                         #endif\r
453 \r
454                 } while( xGotValue == pdFALSE );\r
455 \r
456                 if( ulReceivedValue != ulExpectedValue )\r
457                 {\r
458                         xSuspendedQueueReceiveError = pdTRUE;\r
459                 }\r
460 \r
461                 if( xSuspendedQueueReceiveError != pdTRUE )\r
462                 {\r
463                         /* Only increment the variable if an error has not occurred.  This\r
464                         allows xAreDynamicPriorityTasksStillRunning() to check for stalled\r
465                         tasks as well as explicit errors. */\r
466                         ++ulExpectedValue;\r
467                 }\r
468         }\r
469 }\r
470 /*-----------------------------------------------------------*/\r
471 \r
472 /* Called to check that all the created tasks are still running without error. */\r
473 BaseType_t xAreDynamicPriorityTasksStillRunning( void )\r
474 {\r
475 /* Keep a history of the check variables so we know if it has been incremented\r
476 since the last call. */\r
477 static uint16_t usLastTaskCheck = ( uint16_t ) 0;\r
478 static uint32_t ulLastExpectedValue = ( uint32_t ) 0U;\r
479 BaseType_t xReturn = pdTRUE;\r
480 \r
481         /* Check the tasks are still running by ensuring the check variable\r
482         is still incrementing. */\r
483 \r
484         if( usCheckVariable == usLastTaskCheck )\r
485         {\r
486                 /* The check has not incremented so an error exists. */\r
487                 xReturn = pdFALSE;\r
488         }\r
489 \r
490         if( ulExpectedValue == ulLastExpectedValue )\r
491         {\r
492                 /* The value being received by the queue receive task has not\r
493                 incremented so an error exists. */\r
494                 xReturn = pdFALSE;\r
495         }\r
496 \r
497         if( xSuspendedQueueSendError == pdTRUE )\r
498         {\r
499                 xReturn = pdFALSE;\r
500         }\r
501 \r
502         if( xSuspendedQueueReceiveError == pdTRUE )\r
503         {\r
504                 xReturn = pdFALSE;\r
505         }\r
506 \r
507         usLastTaskCheck = usCheckVariable;\r
508         ulLastExpectedValue = ulExpectedValue;\r
509 \r
510         return xReturn;\r
511 }\r