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