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