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