]> git.sur5r.net Git - freertos/blob - FreeRTOS/Demo/RL78_E2Studio_GCC/src/Common-Demo-Tasks/dynamic.c
5b34d8a6a974f3ffc780f501e302df3788136d9c
[freertos] / FreeRTOS / Demo / RL78_E2Studio_GCC / src / Common-Demo-Tasks / dynamic.c
1 /*\r
2     FreeRTOS V7.4.0 - Copyright (C) 2013 Real Time Engineers Ltd.\r
3 \r
4     FEATURES AND PORTS ARE ADDED TO FREERTOS ALL THE TIME.  PLEASE VISIT\r
5     http://www.FreeRTOS.org TO ENSURE YOU ARE USING THE LATEST VERSION.\r
6 \r
7     ***************************************************************************\r
8      *                                                                       *\r
9      *    FreeRTOS tutorial books are available in pdf and paperback.        *\r
10      *    Complete, revised, and edited pdf reference manuals are also       *\r
11      *    available.                                                         *\r
12      *                                                                       *\r
13      *    Purchasing FreeRTOS documentation will not only help you, by       *\r
14      *    ensuring you get running as quickly as possible and with an        *\r
15      *    in-depth knowledge of how to use FreeRTOS, it will also help       *\r
16      *    the FreeRTOS project to continue with its mission of providing     *\r
17      *    professional grade, cross platform, de facto standard solutions    *\r
18      *    for microcontrollers - completely free of charge!                  *\r
19      *                                                                       *\r
20      *    >>> See http://www.FreeRTOS.org/Documentation for details. <<<     *\r
21      *                                                                       *\r
22      *    Thank you for using FreeRTOS, and thank you for your support!      *\r
23      *                                                                       *\r
24     ***************************************************************************\r
25 \r
26 \r
27     This file is part of the FreeRTOS distribution.\r
28 \r
29     FreeRTOS is free software; you can redistribute it and/or modify it under\r
30     the terms of the GNU General Public License (version 2) as published by the\r
31     Free Software Foundation AND MODIFIED BY the FreeRTOS exception.\r
32 \r
33     >>>>>>NOTE<<<<<< The modification to the GPL is included to allow you to\r
34     distribute a combined work that includes FreeRTOS without being obliged to\r
35     provide the source code for proprietary components outside of the FreeRTOS\r
36     kernel.\r
37 \r
38     FreeRTOS is distributed in the hope that it will be useful, but WITHOUT ANY\r
39     WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS\r
40     FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more\r
41     details. You should have received a copy of the GNU General Public License\r
42     and the FreeRTOS license exception along with FreeRTOS; if not itcan be\r
43     viewed here: http://www.freertos.org/a00114.html and also obtained by\r
44     writing to Real Time Engineers Ltd., contact details for whom are available\r
45     on the FreeRTOS WEB site.\r
46 \r
47     1 tab == 4 spaces!\r
48 \r
49     ***************************************************************************\r
50      *                                                                       *\r
51      *    Having a problem?  Start by reading the FAQ "My application does   *\r
52      *    not run, what could be wrong?"                                     *\r
53      *                                                                       *\r
54      *    http://www.FreeRTOS.org/FAQHelp.html                               *\r
55      *                                                                       *\r
56     ***************************************************************************\r
57 \r
58 \r
59     http://www.FreeRTOS.org - Documentation, books, training, latest versions,\r
60     license and Real Time Engineers Ltd. contact details.\r
61 \r
62     http://www.FreeRTOS.org/plus - A selection of FreeRTOS ecosystem products,\r
63     including FreeRTOS+Trace - an indispensable productivity tool, and our new\r
64     fully thread aware and reentrant UDP/IP stack.\r
65 \r
66     http://www.OpenRTOS.com - Real Time Engineers ltd license FreeRTOS to High\r
67     Integrity Systems, who sell the code with commercial support,\r
68     indemnification and middleware, under the OpenRTOS brand.\r
69 \r
70     http://www.SafeRTOS.com - High Integrity Systems also provide a safety\r
71     engineered and independently SIL3 certified version for use in safety and\r
72     mission critical applications that require provable dependability.\r
73 */\r
74 \r
75 /*\r
76  * The first test creates three tasks - two counter tasks (one continuous count\r
77  * and one limited count) and one controller.  A "count" variable is shared\r
78  * between all three tasks.  The two counter tasks should never be in a "ready"\r
79  * state at the same time.  The controller task runs at the same priority as\r
80  * the continuous count task, and at a lower priority than the limited count\r
81  * task.\r
82  *\r
83  * One counter task loops indefinitely, incrementing the shared count variable\r
84  * on each iteration.  To ensure it has exclusive access to the variable it\r
85  * raises it's priority above that of the controller task before each\r
86  * increment, lowering it again to it's original priority before starting the\r
87  * next iteration.\r
88  *\r
89  * The other counter task increments the shared count variable on each\r
90  * iteration of it's loop until the count has reached a limit of 0xff - at\r
91  * which point it suspends itself.  It will not start a new loop until the\r
92  * controller task has made it "ready" again by calling vTaskResume ().\r
93  * This second counter task operates at a higher priority than controller\r
94  * task so does not need to worry about mutual exclusion of the counter\r
95  * variable.\r
96  *\r
97  * The controller task is in two sections.  The first section controls and\r
98  * monitors the continuous count task.  When this section is operational the\r
99  * limited count task is suspended.  Likewise, the second section controls\r
100  * and monitors the limited count task.  When this section is operational the\r
101  * continuous count task is suspended.\r
102  *\r
103  * In the first section the controller task first takes a copy of the shared\r
104  * count variable.  To ensure mutual exclusion on the count variable it\r
105  * suspends the continuous count task, resuming it again when the copy has been\r
106  * taken.  The controller task then sleeps for a fixed period - during which\r
107  * the continuous count task will execute and increment the shared variable.\r
108  * When the controller task wakes it checks that the continuous count task\r
109  * has executed by comparing the copy of the shared variable with its current\r
110  * value.  This time, to ensure mutual exclusion, the scheduler itself is\r
111  * suspended with a call to vTaskSuspendAll ().  This is for demonstration\r
112  * purposes only and is not a recommended technique due to its inefficiency.\r
113  *\r
114  * After a fixed number of iterations the controller task suspends the\r
115  * continuous count task, and moves on to its second section.\r
116  *\r
117  * At the start of the second section the shared variable is cleared to zero.\r
118  * The limited count task is then woken from it's suspension by a call to\r
119  * vTaskResume ().  As this counter task operates at a higher priority than\r
120  * the controller task the controller task should not run again until the\r
121  * shared variable has been counted up to the limited value causing the counter\r
122  * task to suspend itself.  The next line after vTaskResume () is therefore\r
123  * a check on the shared variable to ensure everything is as expected.\r
124  *\r
125  *\r
126  * The second test consists of a couple of very simple tasks that post onto a\r
127  * queue while the scheduler is suspended.  This test was added to test parts\r
128  * of the scheduler not exercised by the first test.\r
129  *\r
130  */\r
131 \r
132 #include <stdlib.h>\r
133 \r
134 /* Scheduler include files. */\r
135 #include "FreeRTOS.h"\r
136 #include "task.h"\r
137 #include "semphr.h"\r
138 \r
139 /* Demo app include files. */\r
140 #include "dynamic.h"\r
141 \r
142 /* Function that implements the "limited count" task as described above. */\r
143 static portTASK_FUNCTION_PROTO( vLimitedIncrementTask, pvParameters );\r
144 \r
145 /* Function that implements the "continuous count" task as described above. */\r
146 static portTASK_FUNCTION_PROTO( vContinuousIncrementTask, pvParameters );\r
147 \r
148 /* Function that implements the controller task as described above. */\r
149 static portTASK_FUNCTION_PROTO( vCounterControlTask, pvParameters );\r
150 \r
151 static portTASK_FUNCTION_PROTO( vQueueReceiveWhenSuspendedTask, pvParameters );\r
152 static portTASK_FUNCTION_PROTO( vQueueSendWhenSuspendedTask, pvParameters );\r
153 \r
154 /* Demo task specific constants. */\r
155 #define priSTACK_SIZE                           ( configMINIMAL_STACK_SIZE )\r
156 #define priSLEEP_TIME                           ( ( portTickType ) 128 / portTICK_RATE_MS )\r
157 #define priLOOPS                                        ( 5 )\r
158 #define priMAX_COUNT                            ( ( unsigned long ) 0xff )\r
159 #define priNO_BLOCK                                     ( ( portTickType ) 0 )\r
160 #define priSUSPENDED_QUEUE_LENGTH       ( 1 )\r
161 \r
162 /*-----------------------------------------------------------*/\r
163 \r
164 /* Handles to the two counter tasks.  These could be passed in as parameters\r
165 to the controller task to prevent them having to be file scope. */\r
166 static xTaskHandle xContinousIncrementHandle, xLimitedIncrementHandle;\r
167 \r
168 /* The shared counter variable.  This is passed in as a parameter to the two\r
169 counter variables for demonstration purposes. */\r
170 static unsigned long ulCounter;\r
171 \r
172 /* Variables used to check that the tasks are still operating without error.\r
173 Each complete iteration of the controller task increments this variable\r
174 provided no errors have been found.  The variable maintaining the same value\r
175 is therefore indication of an error. */\r
176 static volatile unsigned short usCheckVariable = ( unsigned short ) 0;\r
177 static volatile portBASE_TYPE xSuspendedQueueSendError = pdFALSE;\r
178 static volatile portBASE_TYPE xSuspendedQueueReceiveError = pdFALSE;\r
179 \r
180 /* Queue used by the second test. */\r
181 static xQueueHandle xSuspendedTestQueue;\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( unsigned long ) );\r
191 \r
192         /* vQueueAddToRegistry() adds the queue to the queue registry, if one is\r
193         in use.  The queue registry is provided as a means for kernel aware\r
194         debuggers to locate queues and has no purpose if a kernel aware debugger\r
195         is not being used.  The call to vQueueAddToRegistry() will be removed\r
196         by the pre-processor if configQUEUE_REGISTRY_SIZE is not defined or is\r
197         defined to be less than 1. */\r
198         vQueueAddToRegistry( xSuspendedTestQueue, ( signed char * ) "Suspended_Test_Queue" );\r
199 \r
200         xTaskCreate( vContinuousIncrementTask, ( signed char * ) "CNT_INC", priSTACK_SIZE, ( void * ) &ulCounter, tskIDLE_PRIORITY, &xContinousIncrementHandle );\r
201         xTaskCreate( vLimitedIncrementTask, ( signed char * ) "LIM_INC", priSTACK_SIZE, ( void * ) &ulCounter, tskIDLE_PRIORITY + 1, &xLimitedIncrementHandle );\r
202         xTaskCreate( vCounterControlTask, ( signed char * ) "C_CTRL", priSTACK_SIZE, NULL, tskIDLE_PRIORITY, NULL );\r
203         xTaskCreate( vQueueSendWhenSuspendedTask, ( signed char * ) "SUSP_TX", priSTACK_SIZE, NULL, tskIDLE_PRIORITY, NULL );\r
204         xTaskCreate( vQueueReceiveWhenSuspendedTask, ( signed char * ) "SUSP_RX", priSTACK_SIZE, NULL, tskIDLE_PRIORITY, NULL );\r
205 }\r
206 /*-----------------------------------------------------------*/\r
207 \r
208 /*\r
209  * Just loops around incrementing the shared variable until the limit has been\r
210  * reached.  Once the limit has been reached it suspends itself.\r
211  */\r
212 static portTASK_FUNCTION( vLimitedIncrementTask, pvParameters )\r
213 {\r
214 unsigned long *pulCounter;\r
215 \r
216         /* Take a pointer to the shared variable from the parameters passed into\r
217         the task. */\r
218         pulCounter = ( unsigned long * ) pvParameters;\r
219 \r
220         /* This will run before the control task, so the first thing it does is\r
221         suspend - the control task will resume it when ready. */\r
222         vTaskSuspend( NULL );\r
223 \r
224         for( ;; )\r
225         {\r
226                 /* Just count up to a value then suspend. */\r
227                 ( *pulCounter )++;\r
228 \r
229                 if( *pulCounter >= priMAX_COUNT )\r
230                 {\r
231                         vTaskSuspend( NULL );\r
232                 }\r
233         }\r
234 }\r
235 /*-----------------------------------------------------------*/\r
236 \r
237 /*\r
238  * Just keep counting the shared variable up.  The control task will suspend\r
239  * this task when it wants.\r
240  */\r
241 static portTASK_FUNCTION( vContinuousIncrementTask, pvParameters )\r
242 {\r
243 unsigned long *pulCounter;\r
244 unsigned portBASE_TYPE uxOurPriority;\r
245 \r
246         /* Take a pointer to the shared variable from the parameters passed into\r
247         the task. */\r
248         pulCounter = ( unsigned long * ) pvParameters;\r
249 \r
250         /* Query our priority so we can raise it when exclusive access to the\r
251         shared variable is required. */\r
252         uxOurPriority = uxTaskPriorityGet( NULL );\r
253 \r
254         for( ;; )\r
255         {\r
256                 /* Raise our priority above the controller task to ensure a context\r
257                 switch does not occur while we are accessing this variable. */\r
258                 vTaskPrioritySet( NULL, uxOurPriority + 1 );\r
259                         ( *pulCounter )++;\r
260                 vTaskPrioritySet( NULL, uxOurPriority );\r
261         }\r
262 }\r
263 /*-----------------------------------------------------------*/\r
264 \r
265 /*\r
266  * Controller task as described above.\r
267  */\r
268 static portTASK_FUNCTION( vCounterControlTask, pvParameters )\r
269 {\r
270 unsigned long ulLastCounter;\r
271 short sLoops;\r
272 short sError = pdFALSE;\r
273 \r
274         /* Just to stop warning messages. */\r
275         ( void ) pvParameters;\r
276 \r
277         for( ;; )\r
278         {\r
279                 /* Start with the counter at zero. */\r
280                 ulCounter = ( unsigned long ) 0;\r
281 \r
282                 /* First section : */\r
283 \r
284                 /* Check the continuous count task is running. */\r
285                 for( sLoops = 0; sLoops < priLOOPS; sLoops++ )\r
286                 {\r
287                         /* Suspend the continuous count task so we can take a mirror of the\r
288                         shared variable without risk of corruption. */\r
289                         vTaskSuspend( xContinousIncrementHandle );\r
290                                 ulLastCounter = ulCounter;\r
291                         vTaskResume( xContinousIncrementHandle );\r
292 \r
293                         /* Now delay to ensure the other task has processor time. */\r
294                         vTaskDelay( priSLEEP_TIME );\r
295 \r
296                         /* Check the shared variable again.  This time to ensure mutual\r
297                         exclusion the whole scheduler will be locked.  This is just for\r
298                         demo purposes! */\r
299                         vTaskSuspendAll();\r
300                         {\r
301                                 if( ulLastCounter == ulCounter )\r
302                                 {\r
303                                         /* The shared variable has not changed.  There is a problem\r
304                                         with the continuous count task so flag an error. */\r
305                                         sError = pdTRUE;\r
306                                 }\r
307                         }\r
308                         xTaskResumeAll();\r
309                 }\r
310 \r
311 \r
312                 /* Second section: */\r
313 \r
314                 /* Suspend the continuous counter task so it stops accessing the shared variable. */\r
315                 vTaskSuspend( xContinousIncrementHandle );\r
316 \r
317                 /* Reset the variable. */\r
318                 ulCounter = ( unsigned long ) 0;\r
319 \r
320                 /* Resume the limited count task which has a higher priority than us.\r
321                 We should therefore not return from this call until the limited count\r
322                 task has suspended itself with a known value in the counter variable. */\r
323                 vTaskResume( xLimitedIncrementHandle );\r
324 \r
325                 /* Does the counter variable have the expected value? */\r
326                 if( ulCounter != priMAX_COUNT )\r
327                 {\r
328                         sError = pdTRUE;\r
329                 }\r
330 \r
331                 if( sError == pdFALSE )\r
332                 {\r
333                         /* If no errors have occurred then increment the check variable. */\r
334                         portENTER_CRITICAL();\r
335                                 usCheckVariable++;\r
336                         portEXIT_CRITICAL();\r
337                 }\r
338 \r
339                 /* Resume the continuous count task and do it all again. */\r
340                 vTaskResume( xContinousIncrementHandle );\r
341         }\r
342 }\r
343 /*-----------------------------------------------------------*/\r
344 \r
345 static portTASK_FUNCTION( vQueueSendWhenSuspendedTask, pvParameters )\r
346 {\r
347 static unsigned long ulValueToSend = ( unsigned long ) 0;\r
348 \r
349         /* Just to stop warning messages. */\r
350         ( void ) pvParameters;\r
351 \r
352         for( ;; )\r
353         {\r
354                 vTaskSuspendAll();\r
355                 {\r
356                         /* We must not block while the scheduler is suspended! */\r
357                         if( xQueueSend( xSuspendedTestQueue, ( void * ) &ulValueToSend, priNO_BLOCK ) != pdTRUE )\r
358                         {\r
359                                 xSuspendedQueueSendError = pdTRUE;\r
360                         }\r
361                 }\r
362                 xTaskResumeAll();\r
363 \r
364                 vTaskDelay( priSLEEP_TIME );\r
365 \r
366                 ++ulValueToSend;\r
367         }\r
368 }\r
369 /*-----------------------------------------------------------*/\r
370 \r
371 static portTASK_FUNCTION( vQueueReceiveWhenSuspendedTask, pvParameters )\r
372 {\r
373 static unsigned long ulExpectedValue = ( unsigned long ) 0, ulReceivedValue;\r
374 portBASE_TYPE xGotValue;\r
375 \r
376         /* Just to stop warning messages. */\r
377         ( void ) pvParameters;\r
378 \r
379         for( ;; )\r
380         {\r
381                 do\r
382                 {\r
383                         /* Suspending the scheduler here is fairly pointless and\r
384                         undesirable for a normal application.  It is done here purely\r
385                         to test the scheduler.  The inner xTaskResumeAll() should\r
386                         never return pdTRUE as the scheduler is still locked by the\r
387                         outer call. */\r
388                         vTaskSuspendAll();\r
389                         {\r
390                                 vTaskSuspendAll();\r
391                                 {\r
392                                         xGotValue = xQueueReceive( xSuspendedTestQueue, ( void * ) &ulReceivedValue, priNO_BLOCK );\r
393                                 }\r
394                                 if( xTaskResumeAll() )\r
395                                 {\r
396                                         xSuspendedQueueReceiveError = pdTRUE;\r
397                                 }\r
398                         }\r
399                         xTaskResumeAll();\r
400 \r
401                         #if configUSE_PREEMPTION == 0\r
402                         {\r
403                                 taskYIELD();\r
404                         }\r
405                         #endif\r
406 \r
407                 } while( xGotValue == pdFALSE );\r
408 \r
409                 if( ulReceivedValue != ulExpectedValue )\r
410                 {\r
411                         xSuspendedQueueReceiveError = pdTRUE;\r
412                 }\r
413 \r
414                 ++ulExpectedValue;\r
415         }\r
416 }\r
417 /*-----------------------------------------------------------*/\r
418 \r
419 /* Called to check that all the created tasks are still running without error. */\r
420 portBASE_TYPE xAreDynamicPriorityTasksStillRunning( void )\r
421 {\r
422 /* Keep a history of the check variables so we know if it has been incremented\r
423 since the last call. */\r
424 static unsigned short usLastTaskCheck = ( unsigned short ) 0;\r
425 portBASE_TYPE xReturn = pdTRUE;\r
426 \r
427         /* Check the tasks are still running by ensuring the check variable\r
428         is still incrementing. */\r
429 \r
430         if( usCheckVariable == usLastTaskCheck )\r
431         {\r
432                 /* The check has not incremented so an error exists. */\r
433                 xReturn = pdFALSE;\r
434         }\r
435 \r
436         if( xSuspendedQueueSendError == pdTRUE )\r
437         {\r
438                 xReturn = pdFALSE;\r
439         }\r
440 \r
441         if( xSuspendedQueueReceiveError == pdTRUE )\r
442         {\r
443                 xReturn = pdFALSE;\r
444         }\r
445 \r
446         usLastTaskCheck = usCheckVariable;\r
447         return xReturn;\r
448 }\r