]> git.sur5r.net Git - freertos/blob - FreeRTOS/Demo/WIN32-MingW/main_full.c
Update to MIT licensed FreeRTOS V10.0.0 - see https://www.freertos.org/History.txt
[freertos] / FreeRTOS / Demo / WIN32-MingW / main_full.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  *******************************************************************************\r
31  * NOTE 1: The Win32 port is a simulation (or is that emulation?) only!  Do not\r
32  * expect to get real time behaviour from the Win32 port or this demo\r
33  * application.  It is provided as a convenient development and demonstration\r
34  * test bed only.\r
35  *\r
36  * Windows will not be running the FreeRTOS simulator threads continuously, so\r
37  * the timing information in the FreeRTOS+Trace logs have no meaningful units.\r
38  * See the documentation page for the Windows simulator for an explanation of\r
39  * the slow timing:\r
40  * http://www.freertos.org/FreeRTOS-Windows-Simulator-Emulator-for-Visual-Studio-and-Eclipse-MingW.html\r
41  * - READ THE WEB DOCUMENTATION FOR THIS PORT FOR MORE INFORMATION ON USING IT -\r
42  *\r
43  * NOTE 2:  This project provides two demo applications.  A simple blinky style\r
44  * project, and a more comprehensive test and demo application.  The\r
45  * mainCREATE_SIMPLE_BLINKY_DEMO_ONLY setting in main.c is used to select\r
46  * between the two.  See the notes on using mainCREATE_SIMPLE_BLINKY_DEMO_ONLY\r
47  * in main.c.  This file implements the comprehensive test and demo version.\r
48  *\r
49  * NOTE 3:  This file only contains the source code that is specific to the\r
50  * basic demo.  Generic functions, such FreeRTOS hook functions, are defined in\r
51  * main.c.\r
52  *******************************************************************************\r
53  *\r
54  * main() creates all the demo application tasks, then starts the scheduler.\r
55  * The web documentation provides more details of the standard demo application\r
56  * tasks, which provide no particular functionality but do provide a good\r
57  * example of how to use the FreeRTOS API.\r
58  *\r
59  * In addition to the standard demo tasks, the following tasks and tests are\r
60  * defined and/or created within this file:\r
61  *\r
62  * "Check" task - This only executes every five seconds but has a high priority\r
63  * to ensure it gets processor time.  Its main function is to check that all the\r
64  * standard demo tasks are still operational.  While no errors have been\r
65  * discovered the check task will print out "OK" and the current simulated tick\r
66  * time.  If an error is discovered in the execution of a task then the check\r
67  * task will print out an appropriate error message.\r
68  *\r
69  */\r
70 \r
71 \r
72 /* Standard includes. */\r
73 #include <stdio.h>\r
74 #include <stdlib.h>\r
75 \r
76 /* Kernel includes. */\r
77 #include <FreeRTOS.h>\r
78 #include "task.h"\r
79 #include "queue.h"\r
80 #include "timers.h"\r
81 #include "semphr.h"\r
82 \r
83 /* Standard demo includes. */\r
84 #include "BlockQ.h"\r
85 #include "integer.h"\r
86 #include "semtest.h"\r
87 #include "PollQ.h"\r
88 #include "GenQTest.h"\r
89 #include "QPeek.h"\r
90 #include "recmutex.h"\r
91 #include "flop.h"\r
92 #include "TimerDemo.h"\r
93 #include "countsem.h"\r
94 #include "death.h"\r
95 #include "dynamic.h"\r
96 #include "QueueSet.h"\r
97 #include "QueueOverwrite.h"\r
98 #include "EventGroupsDemo.h"\r
99 #include "IntSemTest.h"\r
100 #include "TaskNotify.h"\r
101 #include "QueueSetPolling.h"\r
102 #include "blocktim.h"\r
103 #include "AbortDelay.h"\r
104 #include "MessageBufferDemo.h"\r
105 #include "StreamBufferDemo.h"\r
106 \r
107 /* Priorities at which the tasks are created. */\r
108 #define mainCHECK_TASK_PRIORITY                 ( configMAX_PRIORITIES - 2 )\r
109 #define mainQUEUE_POLL_PRIORITY                 ( tskIDLE_PRIORITY + 1 )\r
110 #define mainSEM_TEST_PRIORITY                   ( tskIDLE_PRIORITY + 1 )\r
111 #define mainBLOCK_Q_PRIORITY                    ( tskIDLE_PRIORITY + 2 )\r
112 #define mainCREATOR_TASK_PRIORITY               ( tskIDLE_PRIORITY + 3 )\r
113 #define mainFLASH_TASK_PRIORITY                 ( tskIDLE_PRIORITY + 1 )\r
114 #define mainINTEGER_TASK_PRIORITY               ( tskIDLE_PRIORITY )\r
115 #define mainGEN_QUEUE_TASK_PRIORITY             ( tskIDLE_PRIORITY )\r
116 #define mainFLOP_TASK_PRIORITY                  ( tskIDLE_PRIORITY )\r
117 #define mainQUEUE_OVERWRITE_PRIORITY    ( tskIDLE_PRIORITY )\r
118 \r
119 #define mainTIMER_TEST_PERIOD                   ( 50 )\r
120 \r
121 /* Task function prototypes. */\r
122 static void prvCheckTask( void *pvParameters );\r
123 \r
124 /* A task that is created from the idle task to test the functionality of\r
125 eTaskStateGet(). */\r
126 static void prvTestTask( void *pvParameters );\r
127 \r
128 /*\r
129  * Called from the idle task hook function to demonstrate a few utility\r
130  * functions that are not demonstrated by any of the standard demo tasks.\r
131  */\r
132 static void prvDemonstrateTaskStateAndHandleGetFunctions( void );\r
133 \r
134 /*\r
135  * Called from the idle task hook function to demonstrate the use of\r
136  * xTimerPendFunctionCall() as xTimerPendFunctionCall() is not demonstrated by\r
137  * any of the standard demo tasks.\r
138  */\r
139 static void prvDemonstratePendingFunctionCall( void );\r
140 \r
141 /*\r
142  * The function that is pended by prvDemonstratePendingFunctionCall().\r
143  */\r
144 static void prvPendedFunction( void *pvParameter1, uint32_t ulParameter2 );\r
145 \r
146 /*\r
147  * prvDemonstrateTimerQueryFunctions() is called from the idle task hook\r
148  * function to demonstrate the use of functions that query information about a\r
149  * software timer.  prvTestTimerCallback() is the callback function for the\r
150  * timer being queried.\r
151  */\r
152 static void prvDemonstrateTimerQueryFunctions( void );\r
153 static void prvTestTimerCallback( TimerHandle_t xTimer );\r
154 \r
155 /*\r
156  * A task to demonstrate the use of the xQueueSpacesAvailable() function.\r
157  */\r
158 static void prvDemoQueueSpaceFunctions( void *pvParameters );\r
159 \r
160 /*\r
161  * Tasks that ensure indefinite delays are truly indefinite.\r
162  */\r
163 static void prvPermanentlyBlockingSemaphoreTask( void *pvParameters );\r
164 static void prvPermanentlyBlockingNotificationTask( void *pvParameters );\r
165 \r
166 /*-----------------------------------------------------------*/\r
167 \r
168 /* The variable into which error messages are latched. */\r
169 static char *pcStatusMessage = "No errors";\r
170 \r
171 /* This semaphore is created purely to test using the vSemaphoreDelete() and\r
172 semaphore tracing API functions.  It has no other purpose. */\r
173 static SemaphoreHandle_t xMutexToDelete = NULL;\r
174 \r
175 /*-----------------------------------------------------------*/\r
176 \r
177 int main_full( void )\r
178 {\r
179         /* Start the check task as described at the top of this file. */\r
180         xTaskCreate( prvCheckTask, "Check", configMINIMAL_STACK_SIZE, NULL, mainCHECK_TASK_PRIORITY, NULL );\r
181 \r
182         /* Create the standard demo tasks. */\r
183         vStartTaskNotifyTask();\r
184         vStartBlockingQueueTasks( mainBLOCK_Q_PRIORITY );\r
185         vStartSemaphoreTasks( mainSEM_TEST_PRIORITY );\r
186         vStartPolledQueueTasks( mainQUEUE_POLL_PRIORITY );\r
187         vStartIntegerMathTasks( mainINTEGER_TASK_PRIORITY );\r
188         vStartGenericQueueTasks( mainGEN_QUEUE_TASK_PRIORITY );\r
189         vStartQueuePeekTasks();\r
190         vStartMathTasks( mainFLOP_TASK_PRIORITY );\r
191         vStartRecursiveMutexTasks();\r
192         vStartCountingSemaphoreTasks();\r
193         vStartDynamicPriorityTasks();\r
194         vStartQueueSetTasks();\r
195         vStartQueueOverwriteTask( mainQUEUE_OVERWRITE_PRIORITY );\r
196         xTaskCreate( prvDemoQueueSpaceFunctions, "QSpace", configMINIMAL_STACK_SIZE, NULL, tskIDLE_PRIORITY, NULL );\r
197         vStartEventGroupTasks();\r
198         vStartInterruptSemaphoreTasks();\r
199         vStartQueueSetPollingTask();\r
200         vCreateBlockTimeTasks();\r
201         vCreateAbortDelayTasks();\r
202         xTaskCreate( prvDemoQueueSpaceFunctions, "QSpace", configMINIMAL_STACK_SIZE, NULL, tskIDLE_PRIORITY, NULL );\r
203         xTaskCreate( prvPermanentlyBlockingSemaphoreTask, "BlockSem", configMINIMAL_STACK_SIZE, NULL, tskIDLE_PRIORITY, NULL );\r
204         xTaskCreate( prvPermanentlyBlockingNotificationTask, "BlockNoti", configMINIMAL_STACK_SIZE, NULL, tskIDLE_PRIORITY, NULL );\r
205 \r
206         vStartMessageBufferTasks();\r
207         vStartStreamBufferTasks();\r
208 \r
209         #if( configUSE_PREEMPTION != 0  )\r
210         {\r
211                 /* Don't expect these tasks to pass when preemption is not used. */\r
212                 vStartTimerDemoTask( mainTIMER_TEST_PERIOD );\r
213         }\r
214         #endif\r
215 \r
216         /* The suicide tasks must be created last as they need to know how many\r
217         tasks were running prior to their creation.  This then allows them to\r
218         ascertain whether or not the correct/expected number of tasks are running at\r
219         any given time. */\r
220         vCreateSuicidalTasks( mainCREATOR_TASK_PRIORITY );\r
221 \r
222         /* Create the semaphore that will be deleted in the idle task hook.  This\r
223         is done purely to test the use of vSemaphoreDelete(). */\r
224         xMutexToDelete = xSemaphoreCreateMutex();\r
225 \r
226         /* Start the scheduler itself. */\r
227         vTaskStartScheduler();\r
228 \r
229         /* Should never get here unless there was not enough heap space to create\r
230         the idle and other system tasks. */\r
231         return 0;\r
232 }\r
233 /*-----------------------------------------------------------*/\r
234 \r
235 static void prvCheckTask( void *pvParameters )\r
236 {\r
237 TickType_t xNextWakeTime;\r
238 const TickType_t xCycleFrequency = pdMS_TO_TICKS( 2500UL );\r
239 \r
240         /* Just to remove compiler warning. */\r
241         ( void ) pvParameters;\r
242 \r
243         /* Initialise xNextWakeTime - this only needs to be done once. */\r
244         xNextWakeTime = xTaskGetTickCount();\r
245 \r
246         for( ;; )\r
247         {\r
248                 /* Place this task in the blocked state until it is time to run again. */\r
249                 vTaskDelayUntil( &xNextWakeTime, xCycleFrequency );\r
250 \r
251                 /* Check the standard demo tasks are running without error. */\r
252                 #if( configUSE_PREEMPTION != 0 )\r
253                 {\r
254                         /* These tasks are only created when preemption is used. */\r
255                         if( xAreTimerDemoTasksStillRunning( xCycleFrequency ) != pdTRUE )\r
256                         {\r
257                                 pcStatusMessage = "Error: TimerDemo";\r
258                         }\r
259                 }\r
260                 #endif\r
261 \r
262                 if( xAreStreamBufferTasksStillRunning() != pdTRUE )\r
263                 {\r
264                         pcStatusMessage = "Error:  StreamBuffer";\r
265                 }\r
266                 else if( xAreMessageBufferTasksStillRunning() != pdTRUE )\r
267                 {\r
268                         pcStatusMessage = "Error:  MessageBuffer";\r
269                 }\r
270                 else if( xAreTaskNotificationTasksStillRunning() != pdTRUE )\r
271                 {\r
272                         pcStatusMessage = "Error:  Notification";\r
273                 }\r
274                 else if( xAreInterruptSemaphoreTasksStillRunning() != pdTRUE )\r
275                 {\r
276                         pcStatusMessage = "Error: IntSem";\r
277                 }\r
278                 else if( xAreEventGroupTasksStillRunning() != pdTRUE )\r
279                 {\r
280                         pcStatusMessage = "Error: EventGroup";\r
281                 }\r
282                 else if( xAreIntegerMathsTaskStillRunning() != pdTRUE )\r
283                 {\r
284                         pcStatusMessage = "Error: IntMath";\r
285                 }\r
286                 else if( xAreGenericQueueTasksStillRunning() != pdTRUE )\r
287                 {\r
288                         pcStatusMessage = "Error: GenQueue";\r
289                 }\r
290                 else if( xAreQueuePeekTasksStillRunning() != pdTRUE )\r
291                 {\r
292                         pcStatusMessage = "Error: QueuePeek";\r
293                 }\r
294                 else if( xAreBlockingQueuesStillRunning() != pdTRUE )\r
295                 {\r
296                         pcStatusMessage = "Error: BlockQueue";\r
297                 }\r
298                 else if( xAreSemaphoreTasksStillRunning() != pdTRUE )\r
299                 {\r
300                         pcStatusMessage = "Error: SemTest";\r
301                 }\r
302                 else if( xArePollingQueuesStillRunning() != pdTRUE )\r
303                 {\r
304                         pcStatusMessage = "Error: PollQueue";\r
305                 }\r
306                 else if( xAreMathsTaskStillRunning() != pdPASS )\r
307                 {\r
308                         pcStatusMessage = "Error: Flop";\r
309                 }\r
310                 else if( xAreRecursiveMutexTasksStillRunning() != pdTRUE )\r
311                 {\r
312                         pcStatusMessage = "Error: RecMutex";\r
313                 }\r
314                 else if( xAreCountingSemaphoreTasksStillRunning() != pdTRUE )\r
315                 {\r
316                         pcStatusMessage = "Error: CountSem";\r
317                 }\r
318                 else if( xIsCreateTaskStillRunning() != pdTRUE )\r
319                 {\r
320                         pcStatusMessage = "Error: Death";\r
321                 }\r
322                 else if( xAreDynamicPriorityTasksStillRunning() != pdPASS )\r
323                 {\r
324                         pcStatusMessage = "Error: Dynamic";\r
325                 }\r
326                 else if( xAreQueueSetTasksStillRunning() != pdPASS )\r
327                 {\r
328                         pcStatusMessage = "Error: Queue set";\r
329                 }\r
330                 else if( xIsQueueOverwriteTaskStillRunning() != pdPASS )\r
331                 {\r
332                         pcStatusMessage = "Error: Queue overwrite";\r
333                 }\r
334                 else if( xAreQueueSetPollTasksStillRunning() != pdPASS )\r
335                 {\r
336                         pcStatusMessage = "Error: Queue set polling";\r
337                 }\r
338                 else if( xAreBlockTimeTestTasksStillRunning() != pdPASS )\r
339                 {\r
340                         pcStatusMessage = "Error: Block time";\r
341                 }\r
342                 else if( xAreAbortDelayTestTasksStillRunning() != pdPASS )\r
343                 {\r
344                         pcStatusMessage = "Error: Abort delay";\r
345                 }\r
346 \r
347                 /* This is the only task that uses stdout so its ok to call printf()\r
348                 directly. */\r
349                 printf( ( char * ) "%s - %u\r\n", pcStatusMessage, ( unsigned int ) xTaskGetTickCount() );\r
350                 fflush( stdout );\r
351         }\r
352 }\r
353 /*-----------------------------------------------------------*/\r
354 \r
355 static void prvTestTask( void *pvParameters )\r
356 {\r
357 const unsigned long ulMSToSleep = 5;\r
358 \r
359         /* Just to remove compiler warnings. */\r
360         ( void ) pvParameters;\r
361 \r
362         /* This task is just used to test the eTaskStateGet() function.  It\r
363         does not have anything to do. */\r
364         for( ;; )\r
365         {\r
366                 /* Sleep to reduce CPU load, but don't sleep indefinitely in case there are\r
367                 tasks waiting to be terminated by the idle task. */\r
368                 Sleep( ulMSToSleep );\r
369         }\r
370 }\r
371 /*-----------------------------------------------------------*/\r
372 \r
373 /* Called from vApplicationIdleHook(), which is defined in main.c. */\r
374 void vFullDemoIdleFunction( void )\r
375 {\r
376 const unsigned long ulMSToSleep = 15;\r
377 void *pvAllocated;\r
378 \r
379         /* Sleep to reduce CPU load, but don't sleep indefinitely in case there are\r
380         tasks waiting to be terminated by the idle task. */\r
381         Sleep( ulMSToSleep );\r
382 \r
383         /* Demonstrate a few utility functions that are not demonstrated by any of\r
384         the standard demo tasks. */\r
385         prvDemonstrateTaskStateAndHandleGetFunctions();\r
386 \r
387         /* Demonstrate the use of xTimerPendFunctionCall(), which is not\r
388         demonstrated by any of the standard demo tasks. */\r
389         prvDemonstratePendingFunctionCall();\r
390 \r
391         /* Demonstrate the use of functions that query information about a software\r
392         timer. */\r
393         prvDemonstrateTimerQueryFunctions();\r
394 \r
395 \r
396         /* If xMutexToDelete has not already been deleted, then delete it now.\r
397         This is done purely to demonstrate the use of, and test, the\r
398         vSemaphoreDelete() macro.  Care must be taken not to delete a semaphore\r
399         that has tasks blocked on it. */\r
400         if( xMutexToDelete != NULL )\r
401         {\r
402                 /* For test purposes, add the mutex to the registry, then remove it\r
403                 again, before it is deleted - checking its name is as expected before\r
404                 and after the assertion into the registry and its removal from the\r
405                 registry. */\r
406                 configASSERT( pcQueueGetName( xMutexToDelete ) == NULL );\r
407                 vQueueAddToRegistry( xMutexToDelete, "Test_Mutex" );\r
408                 configASSERT( strcmp( pcQueueGetName( xMutexToDelete ), "Test_Mutex" ) == 0 );\r
409                 vQueueUnregisterQueue( xMutexToDelete );\r
410                 configASSERT( pcQueueGetName( xMutexToDelete ) == NULL );\r
411 \r
412                 vSemaphoreDelete( xMutexToDelete );\r
413                 xMutexToDelete = NULL;\r
414         }\r
415 \r
416         /* Exercise heap_5 a bit.  The malloc failed hook will trap failed\r
417         allocations so there is no need to test here. */\r
418         pvAllocated = pvPortMalloc( ( rand() % 500 ) + 1 );\r
419         vPortFree( pvAllocated );\r
420 }\r
421 /*-----------------------------------------------------------*/\r
422 \r
423 /* Called by vApplicationTickHook(), which is defined in main.c. */\r
424 void vFullDemoTickHookFunction( void )\r
425 {\r
426 TaskHandle_t xTimerTask;\r
427 \r
428         /* Call the periodic timer test, which tests the timer API functions that\r
429         can be called from an ISR. */\r
430         #if( configUSE_PREEMPTION != 0 )\r
431         {\r
432                 /* Only created when preemption is used. */\r
433                 vTimerPeriodicISRTests();\r
434         }\r
435         #endif\r
436 \r
437         /* Call the periodic queue overwrite from ISR demo. */\r
438         vQueueOverwritePeriodicISRDemo();\r
439 \r
440         /* Write to a queue that is in use as part of the queue set demo to\r
441         demonstrate using queue sets from an ISR. */\r
442         vQueueSetAccessQueueSetFromISR();\r
443         vQueueSetPollingInterruptAccess();\r
444 \r
445         /* Exercise event groups from interrupts. */\r
446         vPeriodicEventGroupsProcessing();\r
447 \r
448         /* Exercise giving mutexes from an interrupt. */\r
449         vInterruptSemaphorePeriodicTest();\r
450 \r
451         /* Exercise using task notifications from an interrupt. */\r
452         xNotifyTaskFromISR();\r
453 \r
454         /* Writes to stream buffer byte by byte to test the stream buffer trigger\r
455         level functionality. */\r
456         vPeriodicStreamBufferProcessing();\r
457 \r
458         /* For code coverage purposes. */\r
459         xTimerTask = xTimerGetTimerDaemonTaskHandle();\r
460         configASSERT( uxTaskPriorityGetFromISR( xTimerTask ) == configTIMER_TASK_PRIORITY );\r
461         ( void ) xTimerTask; /* In case configASSERT() is not defined. */\r
462 }\r
463 /*-----------------------------------------------------------*/\r
464 \r
465 static void prvPendedFunction( void *pvParameter1, uint32_t ulParameter2 )\r
466 {\r
467 static uint32_t ulLastParameter1 = 1000UL, ulLastParameter2 = 0UL;\r
468 uint32_t ulParameter1;\r
469 \r
470         ulParameter1 = ( uint32_t ) pvParameter1;\r
471 \r
472         /* Ensure the parameters are as expected. */\r
473         configASSERT( ulParameter1 == ( ulLastParameter1 + 1 ) );\r
474         configASSERT( ulParameter2 == ( ulLastParameter2 + 1 ) );\r
475 \r
476         /* Remember the parameters for the next time the function is called. */\r
477         ulLastParameter1 = ulParameter1;\r
478         ulLastParameter2 = ulParameter2;\r
479 \r
480         /* Remove compiler warnings in case configASSERT() is not defined. */\r
481         ( void ) ulLastParameter1;\r
482         ( void ) ulLastParameter2;\r
483 }\r
484 /*-----------------------------------------------------------*/\r
485 \r
486 static void prvTestTimerCallback( TimerHandle_t xTimer )\r
487 {\r
488         /* This is the callback function for the timer accessed by\r
489         prvDemonstrateTimerQueryFunctions().  The callback does not do anything. */\r
490         ( void ) xTimer;\r
491 }\r
492 /*-----------------------------------------------------------*/\r
493 \r
494 static void prvDemonstrateTimerQueryFunctions( void )\r
495 {\r
496 static TimerHandle_t xTimer = NULL;\r
497 const char *pcTimerName = "TestTimer";\r
498 volatile TickType_t xExpiryTime;\r
499 const TickType_t xDontBlock = 0;\r
500 \r
501         if( xTimer == NULL )\r
502         {\r
503                 xTimer = xTimerCreate( pcTimerName, portMAX_DELAY, pdTRUE, NULL, prvTestTimerCallback );\r
504 \r
505                 if( xTimer != NULL )\r
506                 {\r
507                         /* Called from the idle task so a block time must not be\r
508                         specified. */\r
509                         xTimerStart( xTimer, xDontBlock );\r
510                 }\r
511         }\r
512 \r
513         if( xTimer != NULL )\r
514         {\r
515                 /* Demonstrate querying a timer's name. */\r
516                 configASSERT( strcmp( pcTimerGetName( xTimer ), pcTimerName ) == 0 );\r
517 \r
518                 /* Demonstrate querying a timer's period. */\r
519                 configASSERT( xTimerGetPeriod( xTimer ) == portMAX_DELAY );\r
520 \r
521                 /* Demonstrate querying a timer's next expiry time, although nothing is\r
522                 done with the returned value.  Note if the expiry time is less than the\r
523                 maximum tick count then the expiry time has overflowed from the current\r
524                 time.  In this case the expiry time was set to portMAX_DELAY, so it is\r
525                 expected to be less than the current time until the current time has\r
526                 itself overflowed. */\r
527                 xExpiryTime = xTimerGetExpiryTime( xTimer );\r
528                 ( void ) xExpiryTime;\r
529         }\r
530 }\r
531 /*-----------------------------------------------------------*/\r
532 \r
533 static void prvDemonstratePendingFunctionCall( void )\r
534 {\r
535 static uint32_t ulParameter1 = 1000UL, ulParameter2 = 0UL;\r
536 const TickType_t xDontBlock = 0; /* This is called from the idle task so must *not* attempt to block. */\r
537 \r
538         /* prvPendedFunction() just expects the parameters to be incremented by one\r
539         each time it is called. */\r
540         ulParameter1++;\r
541         ulParameter2++;\r
542 \r
543         /* Pend the function call, sending the parameters. */\r
544         xTimerPendFunctionCall( prvPendedFunction, ( void * ) ulParameter1, ulParameter2, xDontBlock );\r
545 }\r
546 /*-----------------------------------------------------------*/\r
547 \r
548 static void prvDemonstrateTaskStateAndHandleGetFunctions( void )\r
549 {\r
550 TaskHandle_t xIdleTaskHandle, xTimerTaskHandle;\r
551 char *pcTaskName;\r
552 static portBASE_TYPE xPerformedOneShotTests = pdFALSE;\r
553 TaskHandle_t xTestTask;\r
554 TaskStatus_t xTaskInfo;\r
555 \r
556         /* Demonstrate the use of the xTimerGetTimerDaemonTaskHandle() and\r
557         xTaskGetIdleTaskHandle() functions.  Also try using the function that sets\r
558         the task number. */\r
559         xIdleTaskHandle = xTaskGetIdleTaskHandle();\r
560         xTimerTaskHandle = xTimerGetTimerDaemonTaskHandle();\r
561 \r
562         /* This is the idle hook, so the current task handle should equal the\r
563         returned idle task handle. */\r
564         if( xTaskGetCurrentTaskHandle() != xIdleTaskHandle )\r
565         {\r
566                 pcStatusMessage = "Error:  Returned idle task handle was incorrect";\r
567         }\r
568 \r
569         /* Check the same handle is obtained using the idle task's name.  First try\r
570         with the wrong name, then the right name. */\r
571         if( xTaskGetHandle( "Idle" ) == xIdleTaskHandle )\r
572         {\r
573                 pcStatusMessage = "Error:  Returned handle for name Idle was incorrect";\r
574         }\r
575 \r
576         if( xTaskGetHandle( "IDLE" ) != xIdleTaskHandle )\r
577         {\r
578                 pcStatusMessage = "Error:  Returned handle for name Idle was incorrect";\r
579         }\r
580 \r
581         /* Check the timer task handle was returned correctly. */\r
582         pcTaskName = pcTaskGetName( xTimerTaskHandle );\r
583         if( strcmp( pcTaskName, "Tmr Svc" ) != 0 )\r
584         {\r
585                 pcStatusMessage = "Error:  Returned timer task handle was incorrect";\r
586         }\r
587 \r
588         if( xTaskGetHandle( "Tmr Svc" ) != xTimerTaskHandle )\r
589         {\r
590                 pcStatusMessage = "Error:  Returned handle for name Tmr Svc was incorrect";\r
591         }\r
592 \r
593         /* This task is running, make sure it's state is returned as running. */\r
594         if( eTaskStateGet( xIdleTaskHandle ) != eRunning )\r
595         {\r
596                 pcStatusMessage = "Error:  Returned idle task state was incorrect";\r
597         }\r
598 \r
599         /* If this task is running, then the timer task must be blocked. */\r
600         if( eTaskStateGet( xTimerTaskHandle ) != eBlocked )\r
601         {\r
602                 pcStatusMessage = "Error:  Returned timer task state was incorrect";\r
603         }\r
604 \r
605         /* Also with the vTaskGetInfo() function. */\r
606         vTaskGetInfo( xTimerTaskHandle, /* The task being queried. */\r
607                                           &xTaskInfo,           /* The structure into which information on the task will be written. */\r
608                                           pdTRUE,                       /* Include the task's high watermark in the structure. */\r
609                                           eInvalid );           /* Include the task state in the structure. */\r
610 \r
611         /* Check the information returned by vTaskGetInfo() is as expected. */\r
612         if( ( xTaskInfo.eCurrentState != eBlocked )                                              ||\r
613                 ( strcmp( xTaskInfo.pcTaskName, "Tmr Svc" ) != 0 )                       ||\r
614                 ( xTaskInfo.uxCurrentPriority != configTIMER_TASK_PRIORITY ) ||\r
615                 ( xTaskInfo.xHandle != xTimerTaskHandle ) )\r
616         {\r
617                 pcStatusMessage = "Error:  vTaskGetInfo() returned incorrect information about the timer task";\r
618         }\r
619 \r
620         /* Other tests that should only be performed once follow.  The test task\r
621         is not created on each iteration because to do so would cause the death\r
622         task to report an error (too many tasks running). */\r
623         if( xPerformedOneShotTests == pdFALSE )\r
624         {\r
625                 /* Don't run this part of the test again. */\r
626                 xPerformedOneShotTests = pdTRUE;\r
627 \r
628                 /* Create a test task to use to test other eTaskStateGet() return values. */\r
629                 if( xTaskCreate( prvTestTask, "Test", configMINIMAL_STACK_SIZE, NULL, tskIDLE_PRIORITY, &xTestTask ) == pdPASS )\r
630                 {\r
631                         /* If this task is running, the test task must be in the ready state. */\r
632                         if( eTaskStateGet( xTestTask ) != eReady )\r
633                         {\r
634                                 pcStatusMessage = "Error: Returned test task state was incorrect 1";\r
635                         }\r
636 \r
637                         /* Now suspend the test task and check its state is reported correctly. */\r
638                         vTaskSuspend( xTestTask );\r
639                         if( eTaskStateGet( xTestTask ) != eSuspended )\r
640                         {\r
641                                 pcStatusMessage = "Error: Returned test task state was incorrect 2";\r
642                         }\r
643 \r
644                         /* Now delete the task and check its state is reported correctly. */\r
645                         vTaskDelete( xTestTask );\r
646                         if( eTaskStateGet( xTestTask ) != eDeleted )\r
647                         {\r
648                                 pcStatusMessage = "Error: Returned test task state was incorrect 3";\r
649                         }\r
650                 }\r
651         }\r
652 }\r
653 /*-----------------------------------------------------------*/\r
654 \r
655 static void prvDemoQueueSpaceFunctions( void *pvParameters )\r
656 {\r
657 QueueHandle_t xQueue = NULL;\r
658 const unsigned portBASE_TYPE uxQueueLength = 10;\r
659 unsigned portBASE_TYPE uxReturn, x;\r
660 \r
661         /* Remove compiler warnings. */\r
662         ( void ) pvParameters;\r
663 \r
664         /* Create the queue that will be used.  Nothing is actually going to be\r
665         sent or received so the queue item size is set to 0. */\r
666         xQueue = xQueueCreate( uxQueueLength, 0 );\r
667         configASSERT( xQueue );\r
668 \r
669         for( ;; )\r
670         {\r
671                 for( x = 0; x < uxQueueLength; x++ )\r
672                 {\r
673                         /* Ask how many messages are available... */\r
674                         uxReturn = uxQueueMessagesWaiting( xQueue );\r
675 \r
676                         /* Check the number of messages being reported as being available\r
677                         is as expected, and force an assert if not. */\r
678                         if( uxReturn != x )\r
679                         {\r
680                                 /* xQueue cannot be NULL so this is deliberately causing an\r
681                                 assert to be triggered as there is an error. */\r
682                                 configASSERT( xQueue == NULL );\r
683                         }\r
684 \r
685                         /* Ask how many spaces remain in the queue... */\r
686                         uxReturn = uxQueueSpacesAvailable( xQueue );\r
687 \r
688                         /* Check the number of spaces being reported as being available\r
689                         is as expected, and force an assert if not. */\r
690                         if( uxReturn != ( uxQueueLength - x ) )\r
691                         {\r
692                                 /* xQueue cannot be NULL so this is deliberately causing an\r
693                                 assert to be triggered as there is an error. */\r
694                                 configASSERT( xQueue == NULL );\r
695                         }\r
696 \r
697                         /* Fill one more space in the queue. */\r
698                         xQueueSendToBack( xQueue, NULL, 0 );\r
699                 }\r
700 \r
701                 /* Perform the same check while the queue is full. */\r
702                 uxReturn = uxQueueMessagesWaiting( xQueue );\r
703                 if( uxReturn != uxQueueLength )\r
704                 {\r
705                         configASSERT( xQueue == NULL );\r
706                 }\r
707 \r
708                 uxReturn = uxQueueSpacesAvailable( xQueue );\r
709 \r
710                 if( uxReturn != 0 )\r
711                 {\r
712                         configASSERT( xQueue == NULL );\r
713                 }\r
714 \r
715                 /* The queue is full, start again. */\r
716                 xQueueReset( xQueue );\r
717 \r
718                 #if( configUSE_PREEMPTION == 0 )\r
719                         taskYIELD();\r
720                 #endif\r
721         }\r
722 }\r
723 /*-----------------------------------------------------------*/\r
724 \r
725 static void prvPermanentlyBlockingSemaphoreTask( void *pvParameters )\r
726 {\r
727 SemaphoreHandle_t xSemaphore;\r
728 \r
729         /* Prevent compiler warning about unused parameter in the case that\r
730         configASSERT() is not defined. */\r
731         ( void ) pvParameters;\r
732 \r
733         /* This task should block on a semaphore, and never return. */\r
734         xSemaphore = xSemaphoreCreateBinary();\r
735         configASSERT( xSemaphore );\r
736 \r
737         xSemaphoreTake( xSemaphore, portMAX_DELAY );\r
738 \r
739         /* The above xSemaphoreTake() call should never return, force an assert if\r
740         it does. */\r
741         configASSERT( pvParameters != NULL );\r
742         vTaskDelete( NULL );\r
743 }\r
744 /*-----------------------------------------------------------*/\r
745 \r
746 static void prvPermanentlyBlockingNotificationTask( void *pvParameters )\r
747 {\r
748         /* Prevent compiler warning about unused parameter in the case that\r
749         configASSERT() is not defined. */\r
750         ( void ) pvParameters;\r
751 \r
752         /* This task should block on a task notification, and never return. */\r
753         ulTaskNotifyTake( pdTRUE, portMAX_DELAY );\r
754 \r
755         /* The above ulTaskNotifyTake() call should never return, force an assert\r
756         if it does. */\r
757         configASSERT( pvParameters != NULL );\r
758         vTaskDelete( NULL );\r
759 }\r
760 \r
761 \r