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