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