]> git.sur5r.net Git - freertos/blob - FreeRTOS/Demo/Common/Minimal/dynamic.c
e57ae424ce78e14331c2950fee03a28ab2d36673
[freertos] / FreeRTOS / Demo / Common / Minimal / dynamic.c
1 /*\r
2  * FreeRTOS Kernel V10.1.0\r
3  * Copyright (C) 2018 Amazon.com, Inc. or its affiliates.  All Rights Reserved.\r
4  *\r
5  * Permission is hereby granted, free of charge, to any person obtaining a copy of\r
6  * this software and associated documentation files (the "Software"), to deal in\r
7  * the Software without restriction, including without limitation the rights to\r
8  * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of\r
9  * the Software, and to permit persons to whom the Software is furnished to do so,\r
10  * subject to the following conditions:\r
11  *\r
12  * The above copyright notice and this permission notice shall be included in all\r
13  * copies or substantial portions of the Software.\r
14  *\r
15  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\r
16  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS\r
17  * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR\r
18  * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER\r
19  * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN\r
20  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.\r
21  *\r
22  * http://www.FreeRTOS.org\r
23  * http://aws.amazon.com/freertos\r
24  *\r
25  * 1 tab == 4 spaces!\r
26  */\r
27 \r
28 /*\r
29  * The first test creates three tasks - two counter tasks (one continuous count\r
30  * and one limited count) and one controller.  A "count" variable is shared\r
31  * between all three tasks.  The two counter tasks should never be in a "ready"\r
32  * state at the same time.  The controller task runs at the same priority as\r
33  * the continuous count task, and at a lower priority than the limited count\r
34  * task.\r
35  *\r
36  * One counter task loops indefinitely, incrementing the shared count variable\r
37  * on each iteration.  To ensure it has exclusive access to the variable it\r
38  * raises its priority above that of the controller task before each\r
39  * increment, lowering it again to its original priority before starting the\r
40  * next iteration.\r
41  *\r
42  * The other counter task increments the shared count variable on each\r
43  * iteration of its loop until the count has reached a limit of 0xff - at\r
44  * which point it suspends itself.  It will not start a new loop until the\r
45  * controller task has made it "ready" again by calling vTaskResume().\r
46  * This second counter task operates at a higher priority than controller\r
47  * task so does not need to worry about mutual exclusion of the counter\r
48  * variable.\r
49  *\r
50  * The controller task is in two sections.  The first section controls and\r
51  * monitors the continuous count task.  When this section is operational the\r
52  * limited count task is suspended.  Likewise, the second section controls\r
53  * and monitors the limited count task.  When this section is operational the\r
54  * continuous count task is suspended.\r
55  *\r
56  * In the first section the controller task first takes a copy of the shared\r
57  * count variable.  To ensure mutual exclusion on the count variable it\r
58  * suspends the continuous count task, resuming it again when the copy has been\r
59  * taken.  The controller task then sleeps for a fixed period - during which\r
60  * the continuous count task will execute and increment the shared variable.\r
61  * When the controller task wakes it checks that the continuous count task\r
62  * has executed by comparing the copy of the shared variable with its current\r
63  * value.  This time, to ensure mutual exclusion, the scheduler itself is\r
64  * suspended with a call to vTaskSuspendAll ().  This is for demonstration\r
65  * purposes only and is not a recommended technique due to its inefficiency.\r
66  *\r
67  * After a fixed number of iterations the controller task suspends the\r
68  * continuous count task, and moves on to its second section.\r
69  *\r
70  * At the start of the second section the shared variable is cleared to zero.\r
71  * The limited count task is then woken from its suspension by a call to\r
72  * vTaskResume ().  As this counter task operates at a higher priority than\r
73  * the controller task the controller task should not run again until the\r
74  * shared variable has been counted up to the limited value causing the counter\r
75  * task to suspend itself.  The next line after vTaskResume () is therefore\r
76  * a check on the shared variable to ensure everything is as expected.\r
77  *\r
78  *\r
79  * The second test consists of a couple of very simple tasks that post onto a\r
80  * queue while the scheduler is suspended.  This test was added to test parts\r
81  * of the scheduler not exercised by the first test.\r
82  *\r
83  */\r
84 \r
85 #include <stdlib.h>\r
86 \r
87 /* Scheduler include files. */\r
88 #include "FreeRTOS.h"\r
89 #include "task.h"\r
90 #include "semphr.h"\r
91 \r
92 /* Demo app include files. */\r
93 #include "dynamic.h"\r
94 \r
95 /* Function that implements the "limited count" task as described above. */\r
96 static portTASK_FUNCTION_PROTO( vLimitedIncrementTask, pvParameters );\r
97 \r
98 /* Function that implements the "continuous count" task as described above. */\r
99 static portTASK_FUNCTION_PROTO( vContinuousIncrementTask, pvParameters );\r
100 \r
101 /* Function that implements the controller task as described above. */\r
102 static portTASK_FUNCTION_PROTO( vCounterControlTask, pvParameters );\r
103 \r
104 static portTASK_FUNCTION_PROTO( vQueueReceiveWhenSuspendedTask, pvParameters );\r
105 static portTASK_FUNCTION_PROTO( vQueueSendWhenSuspendedTask, pvParameters );\r
106 \r
107 /* Demo task specific constants. */\r
108 #define priSTACK_SIZE                           ( configMINIMAL_STACK_SIZE )\r
109 #define priSLEEP_TIME                           pdMS_TO_TICKS( 128 )\r
110 #define priLOOPS                                        ( 5 )\r
111 #define priMAX_COUNT                            ( ( uint32_t ) 0xff )\r
112 #define priNO_BLOCK                                     ( ( TickType_t ) 0 )\r
113 #define priSUSPENDED_QUEUE_LENGTH       ( 1 )\r
114 \r
115 /*-----------------------------------------------------------*/\r
116 \r
117 /* Handles to the two counter tasks.  These could be passed in as parameters\r
118 to the controller task to prevent them having to be file scope. */\r
119 static TaskHandle_t xContinuousIncrementHandle, xLimitedIncrementHandle;\r
120 \r
121 /* The shared counter variable.  This is passed in as a parameter to the two\r
122 counter variables for demonstration purposes. */\r
123 static uint32_t ulCounter;\r
124 \r
125 /* Variables used to check that the tasks are still operating without error.\r
126 Each complete iteration of the controller task increments this variable\r
127 provided no errors have been found.  The variable maintaining the same value\r
128 is therefore indication of an error. */\r
129 static volatile uint16_t usCheckVariable = ( uint16_t ) 0;\r
130 static volatile BaseType_t xSuspendedQueueSendError = pdFALSE;\r
131 static volatile BaseType_t xSuspendedQueueReceiveError = pdFALSE;\r
132 \r
133 /* Queue used by the second test. */\r
134 QueueHandle_t xSuspendedTestQueue;\r
135 \r
136 /* The value the queue receive task expects to receive next.  This is file\r
137 scope so xAreDynamicPriorityTasksStillRunning() can ensure it is still\r
138 incrementing. */\r
139 static uint32_t ulExpectedValue = ( uint32_t ) 0;\r
140 \r
141 /*-----------------------------------------------------------*/\r
142 /*\r
143  * Start the three tasks as described at the top of the file.\r
144  * Note that the limited count task is given a higher priority.\r
145  */\r
146 void vStartDynamicPriorityTasks( void )\r
147 {\r
148         xSuspendedTestQueue = xQueueCreate( priSUSPENDED_QUEUE_LENGTH, sizeof( uint32_t ) );\r
149 \r
150         if( xSuspendedTestQueue != NULL )\r
151         {\r
152                 /* vQueueAddToRegistry() adds the queue to the queue registry, if one is\r
153                 in use.  The queue registry is provided as a means for kernel aware\r
154                 debuggers to locate queues and has no purpose if a kernel aware debugger\r
155                 is not being used.  The call to vQueueAddToRegistry() will be removed\r
156                 by the pre-processor if configQUEUE_REGISTRY_SIZE is not defined or is\r
157                 defined to be less than 1. */\r
158                 vQueueAddToRegistry( xSuspendedTestQueue, "Suspended_Test_Queue" );\r
159 \r
160                 xTaskCreate( vContinuousIncrementTask, "CNT_INC", priSTACK_SIZE, ( void * ) &ulCounter, tskIDLE_PRIORITY, &xContinuousIncrementHandle );\r
161                 xTaskCreate( vLimitedIncrementTask, "LIM_INC", priSTACK_SIZE, ( void * ) &ulCounter, tskIDLE_PRIORITY + 1, &xLimitedIncrementHandle );\r
162                 xTaskCreate( vCounterControlTask, "C_CTRL", priSTACK_SIZE, NULL, tskIDLE_PRIORITY, NULL );\r
163                 xTaskCreate( vQueueSendWhenSuspendedTask, "SUSP_TX", priSTACK_SIZE, NULL, tskIDLE_PRIORITY, NULL );\r
164                 xTaskCreate( vQueueReceiveWhenSuspendedTask, "SUSP_RX", priSTACK_SIZE, NULL, tskIDLE_PRIORITY, NULL );\r
165         }\r
166 }\r
167 /*-----------------------------------------------------------*/\r
168 \r
169 /*\r
170  * Just loops around incrementing the shared variable until the limit has been\r
171  * reached.  Once the limit has been reached it suspends itself.\r
172  */\r
173 static portTASK_FUNCTION( vLimitedIncrementTask, pvParameters )\r
174 {\r
175 volatile uint32_t *pulCounter;\r
176 \r
177         /* Take a pointer to the shared variable from the parameters passed into\r
178         the task. */\r
179         pulCounter = ( volatile uint32_t * ) pvParameters;\r
180 \r
181         /* This will run before the control task, so the first thing it does is\r
182         suspend - the control task will resume it when ready. */\r
183         vTaskSuspend( NULL );\r
184 \r
185         for( ;; )\r
186         {\r
187                 /* Just count up to a value then suspend. */\r
188                 ( *pulCounter )++;\r
189 \r
190                 if( *pulCounter >= priMAX_COUNT )\r
191                 {\r
192                         vTaskSuspend( NULL );\r
193                 }\r
194         }\r
195 }\r
196 /*-----------------------------------------------------------*/\r
197 \r
198 /*\r
199  * Just keep counting the shared variable up.  The control task will suspend\r
200  * this task when it wants.\r
201  */\r
202 static portTASK_FUNCTION( vContinuousIncrementTask, pvParameters )\r
203 {\r
204 volatile uint32_t *pulCounter;\r
205 UBaseType_t uxOurPriority;\r
206 \r
207         /* Take a pointer to the shared variable from the parameters passed into\r
208         the task. */\r
209         pulCounter = ( volatile uint32_t * ) pvParameters;\r
210 \r
211         /* Query our priority so we can raise it when exclusive access to the\r
212         shared variable is required. */\r
213         uxOurPriority = uxTaskPriorityGet( NULL );\r
214 \r
215         for( ;; )\r
216         {\r
217                 /* Raise the priority above the controller task to ensure a context\r
218                 switch does not occur while the variable is being accessed. */\r
219                 vTaskPrioritySet( NULL, uxOurPriority + 1 );\r
220                 {\r
221                         configASSERT( ( uxTaskPriorityGet( NULL ) == ( uxOurPriority + 1 ) ) );\r
222                         ( *pulCounter )++;\r
223                 }\r
224                 vTaskPrioritySet( NULL, uxOurPriority );\r
225 \r
226                 #if( configUSE_PREEMPTION == 0 )\r
227                         taskYIELD();\r
228                 #endif\r
229 \r
230                 configASSERT( ( uxTaskPriorityGet( NULL ) == uxOurPriority ) );\r
231         }\r
232 }\r
233 /*-----------------------------------------------------------*/\r
234 \r
235 /*\r
236  * Controller task as described above.\r
237  */\r
238 static portTASK_FUNCTION( vCounterControlTask, pvParameters )\r
239 {\r
240 uint32_t ulLastCounter;\r
241 short sLoops;\r
242 short sError = pdFALSE;\r
243 \r
244         /* Just to stop warning messages. */\r
245         ( void ) pvParameters;\r
246 \r
247         for( ;; )\r
248         {\r
249                 /* Start with the counter at zero. */\r
250                 ulCounter = ( uint32_t ) 0;\r
251 \r
252                 /* First section : */\r
253 \r
254                 /* Check the continuous count task is running. */\r
255                 for( sLoops = 0; sLoops < priLOOPS; sLoops++ )\r
256                 {\r
257                         /* Suspend the continuous count task so we can take a mirror of the\r
258                         shared variable without risk of corruption.  This is not really\r
259                         needed as the other task raises its priority above this task's\r
260                         priority. */\r
261                         vTaskSuspend( xContinuousIncrementHandle );\r
262                         {\r
263                                 #if( INCLUDE_eTaskGetState == 1 )\r
264                                 {\r
265                                         configASSERT( eTaskGetState( xContinuousIncrementHandle ) == eSuspended );\r
266                                 }\r
267                                 #endif /* INCLUDE_eTaskGetState */\r
268 \r
269                                 ulLastCounter = ulCounter;\r
270                         }\r
271                         vTaskResume( xContinuousIncrementHandle );\r
272 \r
273                         #if( configUSE_PREEMPTION == 0 )\r
274                                 taskYIELD();\r
275                         #endif\r
276 \r
277                         #if( INCLUDE_eTaskGetState == 1 )\r
278                         {\r
279                                 configASSERT( eTaskGetState( xContinuousIncrementHandle ) == eReady );\r
280                         }\r
281                         #endif /* INCLUDE_eTaskGetState */\r
282 \r
283                         /* Now delay to ensure the other task has processor time. */\r
284                         vTaskDelay( priSLEEP_TIME );\r
285 \r
286                         /* Check the shared variable again.  This time to ensure mutual\r
287                         exclusion the whole scheduler will be locked.  This is just for\r
288                         demo purposes! */\r
289                         vTaskSuspendAll();\r
290                         {\r
291                                 if( ulLastCounter == ulCounter )\r
292                                 {\r
293                                         /* The shared variable has not changed.  There is a problem\r
294                                         with the continuous count task so flag an error. */\r
295                                         sError = pdTRUE;\r
296                                 }\r
297                         }\r
298                         xTaskResumeAll();\r
299                 }\r
300 \r
301                 /* Second section: */\r
302 \r
303                 /* Suspend the continuous counter task so it stops accessing the shared\r
304                 variable. */\r
305                 vTaskSuspend( xContinuousIncrementHandle );\r
306 \r
307                 /* Reset the variable. */\r
308                 ulCounter = ( uint32_t ) 0;\r
309 \r
310                 #if( INCLUDE_eTaskGetState == 1 )\r
311                 {\r
312                         configASSERT( eTaskGetState( xLimitedIncrementHandle ) == eSuspended );\r
313                 }\r
314                 #endif /* INCLUDE_eTaskGetState */\r
315 \r
316                 /* Resume the limited count task which has a higher priority than us.\r
317                 We should therefore not return from this call until the limited count\r
318                 task has suspended itself with a known value in the counter variable. */\r
319                 vTaskResume( xLimitedIncrementHandle );\r
320 \r
321                 #if( configUSE_PREEMPTION == 0 )\r
322                         taskYIELD();\r
323                 #endif\r
324 \r
325                 /* This task should not run again until xLimitedIncrementHandle has\r
326                 suspended itself. */\r
327                 #if( INCLUDE_eTaskGetState == 1 )\r
328                 {\r
329                         configASSERT( eTaskGetState( xLimitedIncrementHandle ) == eSuspended );\r
330                 }\r
331                 #endif /* INCLUDE_eTaskGetState */\r
332 \r
333                 /* Does the counter variable have the expected value? */\r
334                 if( ulCounter != priMAX_COUNT )\r
335                 {\r
336                         sError = pdTRUE;\r
337                 }\r
338 \r
339                 if( sError == pdFALSE )\r
340                 {\r
341                         /* If no errors have occurred then increment the check variable. */\r
342                         portENTER_CRITICAL();\r
343                                 usCheckVariable++;\r
344                         portEXIT_CRITICAL();\r
345                 }\r
346 \r
347                 /* Resume the continuous count task and do it all again. */\r
348                 vTaskResume( xContinuousIncrementHandle );\r
349 \r
350                 #if( configUSE_PREEMPTION == 0 )\r
351                         taskYIELD();\r
352                 #endif\r
353         }\r
354 }\r
355 /*-----------------------------------------------------------*/\r
356 \r
357 static portTASK_FUNCTION( vQueueSendWhenSuspendedTask, pvParameters )\r
358 {\r
359 static uint32_t ulValueToSend = ( uint32_t ) 0;\r
360 \r
361         /* Just to stop warning messages. */\r
362         ( void ) pvParameters;\r
363 \r
364         for( ;; )\r
365         {\r
366                 vTaskSuspendAll();\r
367                 {\r
368                         /* We must not block while the scheduler is suspended! */\r
369                         if( xQueueSend( xSuspendedTestQueue, ( void * ) &ulValueToSend, priNO_BLOCK ) != pdTRUE )\r
370                         {\r
371                                 xSuspendedQueueSendError = pdTRUE;\r
372                         }\r
373                 }\r
374                 xTaskResumeAll();\r
375 \r
376                 vTaskDelay( priSLEEP_TIME );\r
377 \r
378                 ++ulValueToSend;\r
379         }\r
380 }\r
381 /*-----------------------------------------------------------*/\r
382 \r
383 static portTASK_FUNCTION( vQueueReceiveWhenSuspendedTask, pvParameters )\r
384 {\r
385 uint32_t ulReceivedValue;\r
386 BaseType_t xGotValue;\r
387 \r
388         /* Just to stop warning messages. */\r
389         ( void ) pvParameters;\r
390 \r
391         for( ;; )\r
392         {\r
393                 do\r
394                 {\r
395                         /* Suspending the scheduler here is fairly pointless and\r
396                         undesirable for a normal application.  It is done here purely\r
397                         to test the scheduler.  The inner xTaskResumeAll() should\r
398                         never return pdTRUE as the scheduler is still locked by the\r
399                         outer call. */\r
400                         vTaskSuspendAll();\r
401                         {\r
402                                 vTaskSuspendAll();\r
403                                 {\r
404                                         xGotValue = xQueueReceive( xSuspendedTestQueue, ( void * ) &ulReceivedValue, priNO_BLOCK );\r
405                                 }\r
406                                 if( xTaskResumeAll() != pdFALSE )\r
407                                 {\r
408                                         xSuspendedQueueReceiveError = pdTRUE;\r
409                                 }\r
410                         }\r
411                         xTaskResumeAll();\r
412 \r
413                         #if configUSE_PREEMPTION == 0\r
414                         {\r
415                                 taskYIELD();\r
416                         }\r
417                         #endif\r
418 \r
419                 } while( xGotValue == pdFALSE );\r
420 \r
421                 if( ulReceivedValue != ulExpectedValue )\r
422                 {\r
423                         xSuspendedQueueReceiveError = pdTRUE;\r
424                 }\r
425 \r
426                 if( xSuspendedQueueReceiveError != pdTRUE )\r
427                 {\r
428                         /* Only increment the variable if an error has not occurred.  This\r
429                         allows xAreDynamicPriorityTasksStillRunning() to check for stalled\r
430                         tasks as well as explicit errors. */\r
431                         ++ulExpectedValue;\r
432                 }\r
433         }\r
434 }\r
435 /*-----------------------------------------------------------*/\r
436 \r
437 /* Called to check that all the created tasks are still running without error. */\r
438 BaseType_t xAreDynamicPriorityTasksStillRunning( void )\r
439 {\r
440 /* Keep a history of the check variables so we know if it has been incremented\r
441 since the last call. */\r
442 static uint16_t usLastTaskCheck = ( uint16_t ) 0;\r
443 static uint32_t ulLastExpectedValue = ( uint32_t ) 0U;\r
444 BaseType_t xReturn = pdTRUE;\r
445 \r
446         /* Check the tasks are still running by ensuring the check variable\r
447         is still incrementing. */\r
448 \r
449         if( usCheckVariable == usLastTaskCheck )\r
450         {\r
451                 /* The check has not incremented so an error exists. */\r
452                 xReturn = pdFALSE;\r
453         }\r
454 \r
455         if( ulExpectedValue == ulLastExpectedValue )\r
456         {\r
457                 /* The value being received by the queue receive task has not\r
458                 incremented so an error exists. */\r
459                 xReturn = pdFALSE;\r
460         }\r
461 \r
462         if( xSuspendedQueueSendError == pdTRUE )\r
463         {\r
464                 xReturn = pdFALSE;\r
465         }\r
466 \r
467         if( xSuspendedQueueReceiveError == pdTRUE )\r
468         {\r
469                 xReturn = pdFALSE;\r
470         }\r
471 \r
472         usLastTaskCheck = usCheckVariable;\r
473         ulLastExpectedValue = ulExpectedValue;\r
474 \r
475         return xReturn;\r
476 }\r