]> git.sur5r.net Git - freertos/blob - FreeRTOS/Demo/RL78_E2Studio_GCC/src/Common-Demo-Tasks/dynamic.c
Update to MIT licensed FreeRTOS V10.0.0 - see https://www.freertos.org/History.txt
[freertos] / FreeRTOS / Demo / RL78_E2Studio_GCC / src / Common-Demo-Tasks / 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 it's priority above that of the controller task before each\r
40  * increment, lowering it again to it's 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 it's 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 it's 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                           ( ( TickType_t ) 128 / portTICK_PERIOD_MS )\r
111 #define priLOOPS                                        ( 5 )\r
112 #define priMAX_COUNT                            ( ( unsigned long ) 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 xContinousIncrementHandle, 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 unsigned long 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 unsigned short usCheckVariable = ( unsigned short ) 0;\r
131 static volatile portBASE_TYPE xSuspendedQueueSendError = pdFALSE;\r
132 static volatile portBASE_TYPE xSuspendedQueueReceiveError = pdFALSE;\r
133 \r
134 /* Queue used by the second test. */\r
135 static QueueHandle_t xSuspendedTestQueue;\r
136 \r
137 /*-----------------------------------------------------------*/\r
138 /*\r
139  * Start the three tasks as described at the top of the file.\r
140  * Note that the limited count task is given a higher priority.\r
141  */\r
142 void vStartDynamicPriorityTasks( void )\r
143 {\r
144         xSuspendedTestQueue = xQueueCreate( priSUSPENDED_QUEUE_LENGTH, sizeof( unsigned long ) );\r
145 \r
146         /* vQueueAddToRegistry() adds the queue to the queue registry, if one is\r
147         in use.  The queue registry is provided as a means for kernel aware\r
148         debuggers to locate queues and has no purpose if a kernel aware debugger\r
149         is not being used.  The call to vQueueAddToRegistry() will be removed\r
150         by the pre-processor if configQUEUE_REGISTRY_SIZE is not defined or is\r
151         defined to be less than 1. */\r
152         vQueueAddToRegistry( xSuspendedTestQueue, ( signed char * ) "Suspended_Test_Queue" );\r
153 \r
154         xTaskCreate( vContinuousIncrementTask, "CNT_INC", priSTACK_SIZE, ( void * ) &ulCounter, tskIDLE_PRIORITY, &xContinousIncrementHandle );\r
155         xTaskCreate( vLimitedIncrementTask, "LIM_INC", priSTACK_SIZE, ( void * ) &ulCounter, tskIDLE_PRIORITY + 1, &xLimitedIncrementHandle );\r
156         xTaskCreate( vCounterControlTask, "C_CTRL", priSTACK_SIZE, NULL, tskIDLE_PRIORITY, NULL );\r
157         xTaskCreate( vQueueSendWhenSuspendedTask, "SUSP_TX", priSTACK_SIZE, NULL, tskIDLE_PRIORITY, NULL );\r
158         xTaskCreate( vQueueReceiveWhenSuspendedTask, "SUSP_RX", priSTACK_SIZE, NULL, tskIDLE_PRIORITY, NULL );\r
159 }\r
160 /*-----------------------------------------------------------*/\r
161 \r
162 /*\r
163  * Just loops around incrementing the shared variable until the limit has been\r
164  * reached.  Once the limit has been reached it suspends itself.\r
165  */\r
166 static portTASK_FUNCTION( vLimitedIncrementTask, pvParameters )\r
167 {\r
168 unsigned long *pulCounter;\r
169 \r
170         /* Take a pointer to the shared variable from the parameters passed into\r
171         the task. */\r
172         pulCounter = ( unsigned long * ) pvParameters;\r
173 \r
174         /* This will run before the control task, so the first thing it does is\r
175         suspend - the control task will resume it when ready. */\r
176         vTaskSuspend( NULL );\r
177 \r
178         for( ;; )\r
179         {\r
180                 /* Just count up to a value then suspend. */\r
181                 ( *pulCounter )++;\r
182 \r
183                 if( *pulCounter >= priMAX_COUNT )\r
184                 {\r
185                         vTaskSuspend( NULL );\r
186                 }\r
187         }\r
188 }\r
189 /*-----------------------------------------------------------*/\r
190 \r
191 /*\r
192  * Just keep counting the shared variable up.  The control task will suspend\r
193  * this task when it wants.\r
194  */\r
195 static portTASK_FUNCTION( vContinuousIncrementTask, pvParameters )\r
196 {\r
197 unsigned long *pulCounter;\r
198 unsigned portBASE_TYPE uxOurPriority;\r
199 \r
200         /* Take a pointer to the shared variable from the parameters passed into\r
201         the task. */\r
202         pulCounter = ( unsigned long * ) pvParameters;\r
203 \r
204         /* Query our priority so we can raise it when exclusive access to the\r
205         shared variable is required. */\r
206         uxOurPriority = uxTaskPriorityGet( NULL );\r
207 \r
208         for( ;; )\r
209         {\r
210                 /* Raise our priority above the controller task to ensure a context\r
211                 switch does not occur while we are accessing this variable. */\r
212                 vTaskPrioritySet( NULL, uxOurPriority + 1 );\r
213                         ( *pulCounter )++;\r
214                 vTaskPrioritySet( NULL, uxOurPriority );\r
215         }\r
216 }\r
217 /*-----------------------------------------------------------*/\r
218 \r
219 /*\r
220  * Controller task as described above.\r
221  */\r
222 static portTASK_FUNCTION( vCounterControlTask, pvParameters )\r
223 {\r
224 unsigned long ulLastCounter;\r
225 short sLoops;\r
226 short sError = pdFALSE;\r
227 \r
228         /* Just to stop warning messages. */\r
229         ( void ) pvParameters;\r
230 \r
231         for( ;; )\r
232         {\r
233                 /* Start with the counter at zero. */\r
234                 ulCounter = ( unsigned long ) 0;\r
235 \r
236                 /* First section : */\r
237 \r
238                 /* Check the continuous count task is running. */\r
239                 for( sLoops = 0; sLoops < priLOOPS; sLoops++ )\r
240                 {\r
241                         /* Suspend the continuous count task so we can take a mirror of the\r
242                         shared variable without risk of corruption. */\r
243                         vTaskSuspend( xContinousIncrementHandle );\r
244                                 ulLastCounter = ulCounter;\r
245                         vTaskResume( xContinousIncrementHandle );\r
246 \r
247                         /* Now delay to ensure the other task has processor time. */\r
248                         vTaskDelay( priSLEEP_TIME );\r
249 \r
250                         /* Check the shared variable again.  This time to ensure mutual\r
251                         exclusion the whole scheduler will be locked.  This is just for\r
252                         demo purposes! */\r
253                         vTaskSuspendAll();\r
254                         {\r
255                                 if( ulLastCounter == ulCounter )\r
256                                 {\r
257                                         /* The shared variable has not changed.  There is a problem\r
258                                         with the continuous count task so flag an error. */\r
259                                         sError = pdTRUE;\r
260                                 }\r
261                         }\r
262                         xTaskResumeAll();\r
263                 }\r
264 \r
265 \r
266                 /* Second section: */\r
267 \r
268                 /* Suspend the continuous counter task so it stops accessing the shared variable. */\r
269                 vTaskSuspend( xContinousIncrementHandle );\r
270 \r
271                 /* Reset the variable. */\r
272                 ulCounter = ( unsigned long ) 0;\r
273 \r
274                 /* Resume the limited count task which has a higher priority than us.\r
275                 We should therefore not return from this call until the limited count\r
276                 task has suspended itself with a known value in the counter variable. */\r
277                 vTaskResume( xLimitedIncrementHandle );\r
278 \r
279                 /* Does the counter variable have the expected value? */\r
280                 if( ulCounter != priMAX_COUNT )\r
281                 {\r
282                         sError = pdTRUE;\r
283                 }\r
284 \r
285                 if( sError == pdFALSE )\r
286                 {\r
287                         /* If no errors have occurred then increment the check variable. */\r
288                         portENTER_CRITICAL();\r
289                                 usCheckVariable++;\r
290                         portEXIT_CRITICAL();\r
291                 }\r
292 \r
293                 /* Resume the continuous count task and do it all again. */\r
294                 vTaskResume( xContinousIncrementHandle );\r
295         }\r
296 }\r
297 /*-----------------------------------------------------------*/\r
298 \r
299 static portTASK_FUNCTION( vQueueSendWhenSuspendedTask, pvParameters )\r
300 {\r
301 static unsigned long ulValueToSend = ( unsigned long ) 0;\r
302 \r
303         /* Just to stop warning messages. */\r
304         ( void ) pvParameters;\r
305 \r
306         for( ;; )\r
307         {\r
308                 vTaskSuspendAll();\r
309                 {\r
310                         /* We must not block while the scheduler is suspended! */\r
311                         if( xQueueSend( xSuspendedTestQueue, ( void * ) &ulValueToSend, priNO_BLOCK ) != pdTRUE )\r
312                         {\r
313                                 xSuspendedQueueSendError = pdTRUE;\r
314                         }\r
315                 }\r
316                 xTaskResumeAll();\r
317 \r
318                 vTaskDelay( priSLEEP_TIME );\r
319 \r
320                 ++ulValueToSend;\r
321         }\r
322 }\r
323 /*-----------------------------------------------------------*/\r
324 \r
325 static portTASK_FUNCTION( vQueueReceiveWhenSuspendedTask, pvParameters )\r
326 {\r
327 static unsigned long ulExpectedValue = ( unsigned long ) 0, ulReceivedValue;\r
328 portBASE_TYPE xGotValue;\r
329 \r
330         /* Just to stop warning messages. */\r
331         ( void ) pvParameters;\r
332 \r
333         for( ;; )\r
334         {\r
335                 do\r
336                 {\r
337                         /* Suspending the scheduler here is fairly pointless and\r
338                         undesirable for a normal application.  It is done here purely\r
339                         to test the scheduler.  The inner xTaskResumeAll() should\r
340                         never return pdTRUE as the scheduler is still locked by the\r
341                         outer call. */\r
342                         vTaskSuspendAll();\r
343                         {\r
344                                 vTaskSuspendAll();\r
345                                 {\r
346                                         xGotValue = xQueueReceive( xSuspendedTestQueue, ( void * ) &ulReceivedValue, priNO_BLOCK );\r
347                                 }\r
348                                 if( xTaskResumeAll() )\r
349                                 {\r
350                                         xSuspendedQueueReceiveError = pdTRUE;\r
351                                 }\r
352                         }\r
353                         xTaskResumeAll();\r
354 \r
355                         #if configUSE_PREEMPTION == 0\r
356                         {\r
357                                 taskYIELD();\r
358                         }\r
359                         #endif\r
360 \r
361                 } while( xGotValue == pdFALSE );\r
362 \r
363                 if( ulReceivedValue != ulExpectedValue )\r
364                 {\r
365                         xSuspendedQueueReceiveError = pdTRUE;\r
366                 }\r
367 \r
368                 ++ulExpectedValue;\r
369         }\r
370 }\r
371 /*-----------------------------------------------------------*/\r
372 \r
373 /* Called to check that all the created tasks are still running without error. */\r
374 portBASE_TYPE xAreDynamicPriorityTasksStillRunning( void )\r
375 {\r
376 /* Keep a history of the check variables so we know if it has been incremented\r
377 since the last call. */\r
378 static unsigned short usLastTaskCheck = ( unsigned short ) 0;\r
379 portBASE_TYPE xReturn = pdTRUE;\r
380 \r
381         /* Check the tasks are still running by ensuring the check variable\r
382         is still incrementing. */\r
383 \r
384         if( usCheckVariable == usLastTaskCheck )\r
385         {\r
386                 /* The check has not incremented so an error exists. */\r
387                 xReturn = pdFALSE;\r
388         }\r
389 \r
390         if( xSuspendedQueueSendError == pdTRUE )\r
391         {\r
392                 xReturn = pdFALSE;\r
393         }\r
394 \r
395         if( xSuspendedQueueReceiveError == pdTRUE )\r
396         {\r
397                 xReturn = pdFALSE;\r
398         }\r
399 \r
400         usLastTaskCheck = usCheckVariable;\r
401         return xReturn;\r
402 }\r