2 * FreeRTOS Kernel V10.2.1
\r
3 * Copyright (C) 2019 Amazon.com, Inc. or its affiliates. All Rights Reserved.
\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
12 * The above copyright notice and this permission notice shall be included in all
\r
13 * copies or substantial portions of the Software.
\r
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
\r
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
\r
17 * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
\r
18 * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
\r
19 * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
\r
20 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
\r
22 * http://www.FreeRTOS.org
\r
23 * http://aws.amazon.com/freertos
\r
25 * 1 tab == 4 spaces!
\r
29 *******************************************************************************
\r
30 * NOTE 1: The Win32 port is a simulation (or is that emulation?) only! Do not
\r
31 * expect to get real time behaviour from the Win32 port or this demo
\r
32 * application. It is provided as a convenient development and demonstration
\r
35 * Windows will not be running the FreeRTOS simulator threads continuously, so
\r
36 * the timing information in the FreeRTOS+Trace logs have no meaningful units.
\r
37 * See the documentation page for the Windows simulator for an explanation of
\r
39 * http://www.freertos.org/FreeRTOS-Windows-Simulator-Emulator-for-Visual-Studio-and-Eclipse-MingW.html
\r
40 * - READ THE WEB DOCUMENTATION FOR THIS PORT FOR MORE INFORMATION ON USING IT -
\r
42 * NOTE 2: This project provides two demo applications. A simple blinky style
\r
43 * project, and a more comprehensive test and demo application. The
\r
44 * mainCREATE_SIMPLE_BLINKY_DEMO_ONLY setting in main.c is used to select
\r
45 * between the two. See the notes on using mainCREATE_SIMPLE_BLINKY_DEMO_ONLY
\r
46 * in main.c. This file implements the comprehensive test and demo version.
\r
48 * NOTE 3: This file only contains the source code that is specific to the
\r
49 * full demo. Generic functions, such FreeRTOS hook functions, are defined in
\r
51 *******************************************************************************
\r
53 * main() creates all the demo application tasks, then starts the scheduler.
\r
54 * The web documentation provides more details of the standard demo application
\r
55 * tasks, which provide no particular functionality but do provide a good
\r
56 * example of how to use the FreeRTOS API.
\r
58 * In addition to the standard demo tasks, the following tasks and tests are
\r
59 * defined and/or created within this file:
\r
61 * "Check" task - This only executes every five seconds but has a high priority
\r
62 * to ensure it gets processor time. Its main function is to check that all the
\r
63 * standard demo tasks are still operational. While no errors have been
\r
64 * discovered the check task will print out "OK" and the current simulated tick
\r
65 * time. If an error is discovered in the execution of a task then the check
\r
66 * task will print out an appropriate error message.
\r
71 /* Standard includes. */
\r
75 /* Kernel includes. */
\r
76 #include <FreeRTOS.h>
\r
82 /* Standard demo includes. */
\r
84 #include "integer.h"
\r
85 #include "semtest.h"
\r
87 #include "GenQTest.h"
\r
89 #include "recmutex.h"
\r
91 #include "TimerDemo.h"
\r
92 #include "countsem.h"
\r
94 #include "dynamic.h"
\r
95 #include "QueueSet.h"
\r
96 #include "QueueOverwrite.h"
\r
97 #include "EventGroupsDemo.h"
\r
98 #include "IntSemTest.h"
\r
99 #include "TaskNotify.h"
\r
100 #include "QueueSetPolling.h"
\r
101 #include "StaticAllocation.h"
\r
102 #include "blocktim.h"
\r
103 #include "AbortDelay.h"
\r
104 #include "MessageBufferDemo.h"
\r
105 #include "StreamBufferDemo.h"
\r
106 #include "StreamBufferInterrupt.h"
\r
107 #include "MessageBufferAMP.h"
\r
109 /* Priorities at which the tasks are created. */
\r
110 #define mainCHECK_TASK_PRIORITY ( configMAX_PRIORITIES - 2 )
\r
111 #define mainQUEUE_POLL_PRIORITY ( tskIDLE_PRIORITY + 1 )
\r
112 #define mainSEM_TEST_PRIORITY ( tskIDLE_PRIORITY + 1 )
\r
113 #define mainBLOCK_Q_PRIORITY ( tskIDLE_PRIORITY + 2 )
\r
114 #define mainCREATOR_TASK_PRIORITY ( tskIDLE_PRIORITY + 3 )
\r
115 #define mainFLASH_TASK_PRIORITY ( tskIDLE_PRIORITY + 1 )
\r
116 #define mainINTEGER_TASK_PRIORITY ( tskIDLE_PRIORITY )
\r
117 #define mainGEN_QUEUE_TASK_PRIORITY ( tskIDLE_PRIORITY )
\r
118 #define mainFLOP_TASK_PRIORITY ( tskIDLE_PRIORITY )
\r
119 #define mainQUEUE_OVERWRITE_PRIORITY ( tskIDLE_PRIORITY )
\r
121 #define mainTIMER_TEST_PERIOD ( 50 )
\r
124 * Exercises code that is not otherwise covered by the standard demo/test
\r
127 extern BaseType_t xRunCodeCoverageTestAdditions( void );
\r
129 /* Task function prototypes. */
\r
130 static void prvCheckTask( void *pvParameters );
\r
132 /* A task that is created from the idle task to test the functionality of
\r
133 eTaskStateGet(). */
\r
134 static void prvTestTask( void *pvParameters );
\r
137 * Called from the idle task hook function to demonstrate a few utility
\r
138 * functions that are not demonstrated by any of the standard demo tasks.
\r
140 static void prvDemonstrateTaskStateAndHandleGetFunctions( void );
\r
143 * Called from the idle task hook function to demonstrate the use of
\r
144 * xTimerPendFunctionCall() as xTimerPendFunctionCall() is not demonstrated by
\r
145 * any of the standard demo tasks.
\r
147 static void prvDemonstratePendingFunctionCall( void );
\r
150 * The function that is pended by prvDemonstratePendingFunctionCall().
\r
152 static void prvPendedFunction( void *pvParameter1, uint32_t ulParameter2 );
\r
155 * prvDemonstrateTimerQueryFunctions() is called from the idle task hook
\r
156 * function to demonstrate the use of functions that query information about a
\r
157 * software timer. prvTestTimerCallback() is the callback function for the
\r
158 * timer being queried.
\r
160 static void prvDemonstrateTimerQueryFunctions( void );
\r
161 static void prvTestTimerCallback( TimerHandle_t xTimer );
\r
164 * A task to demonstrate the use of the xQueueSpacesAvailable() function.
\r
166 static void prvDemoQueueSpaceFunctions( void *pvParameters );
\r
169 * Tasks that ensure indefinite delays are truly indefinite.
\r
171 static void prvPermanentlyBlockingSemaphoreTask( void *pvParameters );
\r
172 static void prvPermanentlyBlockingNotificationTask( void *pvParameters );
\r
175 * The test function and callback function used when exercising the timer AP
\r
176 * function that changes the timer's autoreload mode.
\r
178 static void prvDemonstrateChangingTimerReloadMode( void *pvParameters );
\r
179 static void prvReloadModeTestTimerCallback( TimerHandle_t xTimer );
\r
181 /*-----------------------------------------------------------*/
\r
183 /* The variable into which error messages are latched. */
\r
184 static char *pcStatusMessage = "No errors";
\r
186 /* This semaphore is created purely to test using the vSemaphoreDelete() and
\r
187 semaphore tracing API functions. It has no other purpose. */
\r
188 static SemaphoreHandle_t xMutexToDelete = NULL;
\r
190 /*-----------------------------------------------------------*/
\r
192 int main_full( void )
\r
194 /* Start the check task as described at the top of this file. */
\r
195 xTaskCreate( prvCheckTask, "Check", configMINIMAL_STACK_SIZE, NULL, mainCHECK_TASK_PRIORITY, NULL );
\r
197 /* Create the standard demo tasks. */
\r
198 vStartTaskNotifyTask();
\r
199 vStartBlockingQueueTasks( mainBLOCK_Q_PRIORITY );
\r
200 vStartSemaphoreTasks( mainSEM_TEST_PRIORITY );
\r
201 vStartPolledQueueTasks( mainQUEUE_POLL_PRIORITY );
\r
202 vStartIntegerMathTasks( mainINTEGER_TASK_PRIORITY );
\r
203 vStartGenericQueueTasks( mainGEN_QUEUE_TASK_PRIORITY );
\r
204 vStartQueuePeekTasks();
\r
205 vStartMathTasks( mainFLOP_TASK_PRIORITY );
\r
206 vStartRecursiveMutexTasks();
\r
207 vStartCountingSemaphoreTasks();
\r
208 vStartDynamicPriorityTasks();
\r
209 vStartQueueSetTasks();
\r
210 vStartQueueOverwriteTask( mainQUEUE_OVERWRITE_PRIORITY );
\r
211 xTaskCreate( prvDemoQueueSpaceFunctions, NULL, configMINIMAL_STACK_SIZE, NULL, tskIDLE_PRIORITY, NULL ); /* Name is null for code coverage. */
\r
212 vStartEventGroupTasks();
\r
213 vStartInterruptSemaphoreTasks();
\r
214 vStartQueueSetPollingTask();
\r
215 vCreateBlockTimeTasks();
\r
216 vCreateAbortDelayTasks();
\r
217 xTaskCreate( prvDemoQueueSpaceFunctions, "QSpace", configMINIMAL_STACK_SIZE, NULL, tskIDLE_PRIORITY, NULL );
\r
218 xTaskCreate( prvPermanentlyBlockingSemaphoreTask, "BlockSem", configMINIMAL_STACK_SIZE, NULL, tskIDLE_PRIORITY, NULL );
\r
219 xTaskCreate( prvPermanentlyBlockingNotificationTask, "BlockNoti", configMINIMAL_STACK_SIZE, NULL, tskIDLE_PRIORITY, NULL );
\r
220 xTaskCreate( prvDemonstrateChangingTimerReloadMode, "TimerMode", configMINIMAL_STACK_SIZE, NULL, configMAX_PRIORITIES - 1, NULL );
\r
222 vStartMessageBufferTasks( configMINIMAL_STACK_SIZE );
\r
223 vStartStreamBufferTasks();
\r
224 vStartStreamBufferInterruptDemo();
\r
225 vStartMessageBufferAMPTasks( configMINIMAL_STACK_SIZE );
\r
227 #if( configSUPPORT_STATIC_ALLOCATION == 1 )
\r
229 vStartStaticallyAllocatedTasks();
\r
233 #if( configUSE_PREEMPTION != 0 )
\r
235 /* Don't expect these tasks to pass when preemption is not used. */
\r
236 vStartTimerDemoTask( mainTIMER_TEST_PERIOD );
\r
240 /* The suicide tasks must be created last as they need to know how many
\r
241 tasks were running prior to their creation. This then allows them to
\r
242 ascertain whether or not the correct/expected number of tasks are running at
\r
244 vCreateSuicidalTasks( mainCREATOR_TASK_PRIORITY );
\r
246 /* Create the semaphore that will be deleted in the idle task hook. This
\r
247 is done purely to test the use of vSemaphoreDelete(). */
\r
248 xMutexToDelete = xSemaphoreCreateMutex();
\r
250 /* Start the scheduler itself. */
\r
251 vTaskStartScheduler();
\r
253 /* Should never get here unless there was not enough heap space to create
\r
254 the idle and other system tasks. */
\r
257 /*-----------------------------------------------------------*/
\r
259 static void prvCheckTask( void *pvParameters )
\r
261 TickType_t xNextWakeTime;
\r
262 const TickType_t xCycleFrequency = pdMS_TO_TICKS( 2500UL );
\r
264 /* Just to remove compiler warning. */
\r
265 ( void ) pvParameters;
\r
267 /* Initialise xNextWakeTime - this only needs to be done once. */
\r
268 xNextWakeTime = xTaskGetTickCount();
\r
272 /* Place this task in the blocked state until it is time to run again. */
\r
273 vTaskDelayUntil( &xNextWakeTime, xCycleFrequency );
\r
275 /* Check the standard demo tasks are running without error. */
\r
276 #if( configUSE_PREEMPTION != 0 )
\r
278 /* These tasks are only created when preemption is used. */
\r
279 if( xAreTimerDemoTasksStillRunning( xCycleFrequency ) != pdTRUE )
\r
281 pcStatusMessage = "Error: TimerDemo";
\r
286 if( xAreStreamBufferTasksStillRunning() != pdTRUE )
\r
288 pcStatusMessage = "Error: StreamBuffer";
\r
290 else if( xAreMessageBufferTasksStillRunning() != pdTRUE )
\r
292 pcStatusMessage = "Error: MessageBuffer";
\r
294 else if( xAreTaskNotificationTasksStillRunning() != pdTRUE )
\r
296 pcStatusMessage = "Error: Notification";
\r
298 else if( xAreInterruptSemaphoreTasksStillRunning() != pdTRUE )
\r
300 pcStatusMessage = "Error: IntSem";
\r
302 else if( xAreEventGroupTasksStillRunning() != pdTRUE )
\r
304 pcStatusMessage = "Error: EventGroup";
\r
306 else if( xAreIntegerMathsTaskStillRunning() != pdTRUE )
\r
308 pcStatusMessage = "Error: IntMath";
\r
310 else if( xAreGenericQueueTasksStillRunning() != pdTRUE )
\r
312 pcStatusMessage = "Error: GenQueue";
\r
314 else if( xAreQueuePeekTasksStillRunning() != pdTRUE )
\r
316 pcStatusMessage = "Error: QueuePeek";
\r
318 else if( xAreBlockingQueuesStillRunning() != pdTRUE )
\r
320 pcStatusMessage = "Error: BlockQueue";
\r
322 else if( xAreSemaphoreTasksStillRunning() != pdTRUE )
\r
324 pcStatusMessage = "Error: SemTest";
\r
326 else if( xArePollingQueuesStillRunning() != pdTRUE )
\r
328 pcStatusMessage = "Error: PollQueue";
\r
330 else if( xAreMathsTaskStillRunning() != pdPASS )
\r
332 pcStatusMessage = "Error: Flop";
\r
334 else if( xAreRecursiveMutexTasksStillRunning() != pdTRUE )
\r
336 pcStatusMessage = "Error: RecMutex";
\r
338 else if( xAreCountingSemaphoreTasksStillRunning() != pdTRUE )
\r
340 pcStatusMessage = "Error: CountSem";
\r
342 else if( xIsCreateTaskStillRunning() != pdTRUE )
\r
344 pcStatusMessage = "Error: Death";
\r
346 else if( xAreDynamicPriorityTasksStillRunning() != pdPASS )
\r
348 pcStatusMessage = "Error: Dynamic";
\r
350 else if( xAreQueueSetTasksStillRunning() != pdPASS )
\r
352 pcStatusMessage = "Error: Queue set";
\r
354 else if( xIsQueueOverwriteTaskStillRunning() != pdPASS )
\r
356 pcStatusMessage = "Error: Queue overwrite";
\r
358 else if( xAreQueueSetPollTasksStillRunning() != pdPASS )
\r
360 pcStatusMessage = "Error: Queue set polling";
\r
362 else if( xAreBlockTimeTestTasksStillRunning() != pdPASS )
\r
364 pcStatusMessage = "Error: Block time";
\r
366 else if( xAreAbortDelayTestTasksStillRunning() != pdPASS )
\r
368 pcStatusMessage = "Error: Abort delay";
\r
370 else if( xIsInterruptStreamBufferDemoStillRunning() != pdPASS )
\r
372 pcStatusMessage = "Error: Stream buffer interrupt";
\r
374 else if( xAreMessageBufferAMPTasksStillRunning() != pdPASS )
\r
376 pcStatusMessage = "Error: Message buffer AMP";
\r
379 #if( configSUPPORT_STATIC_ALLOCATION == 1 )
\r
380 else if( xAreStaticAllocationTasksStillRunning() != pdPASS )
\r
382 pcStatusMessage = "Error: Static allocation";
\r
384 #endif /* configSUPPORT_STATIC_ALLOCATION */
\r
386 /* This is the only task that uses stdout so its ok to call printf()
\r
388 printf( "%s - tick count %u - free heap %u - min free heap %u\r\n", pcStatusMessage,
\r
389 xTaskGetTickCount(),
\r
390 xPortGetFreeHeapSize(),
\r
391 xPortGetMinimumEverFreeHeapSize() );
\r
395 /*-----------------------------------------------------------*/
\r
397 static void prvTestTask( void *pvParameters )
\r
399 const unsigned long ulMSToSleep = 5;
\r
401 /* Just to remove compiler warnings. */
\r
402 ( void ) pvParameters;
\r
404 /* This task is just used to test the eTaskStateGet() function. It
\r
405 does not have anything to do. */
\r
408 /* Sleep to reduce CPU load, but don't sleep indefinitely in case there are
\r
409 tasks waiting to be terminated by the idle task. */
\r
410 Sleep( ulMSToSleep );
\r
413 /*-----------------------------------------------------------*/
\r
415 /* Called from vApplicationIdleHook(), which is defined in main.c. */
\r
416 void vFullDemoIdleFunction( void )
\r
418 const unsigned long ulMSToSleep = 15;
\r
421 /* Sleep to reduce CPU load, but don't sleep indefinitely in case there are
\r
422 tasks waiting to be terminated by the idle task. */
\r
423 Sleep( ulMSToSleep );
\r
425 /* Demonstrate a few utility functions that are not demonstrated by any of
\r
426 the standard demo tasks. */
\r
427 prvDemonstrateTaskStateAndHandleGetFunctions();
\r
429 /* Demonstrate the use of xTimerPendFunctionCall(), which is not
\r
430 demonstrated by any of the standard demo tasks. */
\r
431 prvDemonstratePendingFunctionCall();
\r
433 /* Demonstrate the use of functions that query information about a software
\r
435 prvDemonstrateTimerQueryFunctions();
\r
437 /* If xMutexToDelete has not already been deleted, then delete it now.
\r
438 This is done purely to demonstrate the use of, and test, the
\r
439 vSemaphoreDelete() macro. Care must be taken not to delete a semaphore
\r
440 that has tasks blocked on it. */
\r
441 if( xMutexToDelete != NULL )
\r
443 /* For test purposes, add the mutex to the registry, then remove it
\r
444 again, before it is deleted - checking its name is as expected before
\r
445 and after the assertion into the registry and its removal from the
\r
447 configASSERT( pcQueueGetName( xMutexToDelete ) == NULL );
\r
448 vQueueAddToRegistry( xMutexToDelete, "Test_Mutex" );
\r
449 configASSERT( strcmp( pcQueueGetName( xMutexToDelete ), "Test_Mutex" ) == 0 );
\r
450 vQueueUnregisterQueue( xMutexToDelete );
\r
451 configASSERT( pcQueueGetName( xMutexToDelete ) == NULL );
\r
453 vSemaphoreDelete( xMutexToDelete );
\r
454 xMutexToDelete = NULL;
\r
457 /* Exercise heap_5 a bit. The malloc failed hook will trap failed
\r
458 allocations so there is no need to test here. */
\r
459 pvAllocated = pvPortMalloc( ( rand() % 500 ) + 1 );
\r
460 vPortFree( pvAllocated );
\r
462 /* Exit after a fixed time so code coverage results are written to the
\r
464 #if( projCOVERAGE_TEST == 1 )
\r
466 const TickType_t xMaxRunTime = pdMS_TO_TICKS( 30000UL );
\r
468 /* Exercise code not otherwise executed by standard demo/test tasks. */
\r
469 if( xRunCodeCoverageTestAdditions() != pdPASS )
\r
471 pcStatusMessage = "Code coverage additions failed.\r\n";
\r
474 if( ( xTaskGetTickCount() - configINITIAL_TICK_COUNT ) >= xMaxRunTime )
\r
476 vTaskEndScheduler();
\r
481 /*-----------------------------------------------------------*/
\r
483 /* Called by vApplicationTickHook(), which is defined in main.c. */
\r
484 void vFullDemoTickHookFunction( void )
\r
486 TaskHandle_t xTimerTask;
\r
488 /* Call the periodic timer test, which tests the timer API functions that
\r
489 can be called from an ISR. */
\r
490 #if( configUSE_PREEMPTION != 0 )
\r
492 /* Only created when preemption is used. */
\r
493 vTimerPeriodicISRTests();
\r
497 /* Call the periodic queue overwrite from ISR demo. */
\r
498 vQueueOverwritePeriodicISRDemo();
\r
500 /* Write to a queue that is in use as part of the queue set demo to
\r
501 demonstrate using queue sets from an ISR. */
\r
502 vQueueSetAccessQueueSetFromISR();
\r
503 vQueueSetPollingInterruptAccess();
\r
505 /* Exercise event groups from interrupts. */
\r
506 vPeriodicEventGroupsProcessing();
\r
508 /* Exercise giving mutexes from an interrupt. */
\r
509 vInterruptSemaphorePeriodicTest();
\r
511 /* Exercise using task notifications from an interrupt. */
\r
512 xNotifyTaskFromISR();
\r
514 /* Writes to stream buffer byte by byte to test the stream buffer trigger
\r
515 level functionality. */
\r
516 vPeriodicStreamBufferProcessing();
\r
518 /* Writes a string to a string buffer four bytes at a time to demonstrate
\r
519 a stream being sent from an interrupt to a task. */
\r
520 vBasicStreamBufferSendFromISR();
\r
522 /* For code coverage purposes. */
\r
523 xTimerTask = xTimerGetTimerDaemonTaskHandle();
\r
524 configASSERT( uxTaskPriorityGetFromISR( xTimerTask ) == configTIMER_TASK_PRIORITY );
\r
525 ( void ) xTimerTask; /* In case configASSERT() is not defined. */
\r
527 /*-----------------------------------------------------------*/
\r
529 static void prvPendedFunction( void *pvParameter1, uint32_t ulParameter2 )
\r
531 static uint32_t ulLastParameter1 = 1000UL, ulLastParameter2 = 0UL;
\r
532 uint32_t ulParameter1;
\r
534 ulParameter1 = ( uint32_t ) pvParameter1;
\r
536 /* Ensure the parameters are as expected. */
\r
537 configASSERT( ulParameter1 == ( ulLastParameter1 + 1 ) );
\r
538 configASSERT( ulParameter2 == ( ulLastParameter2 + 1 ) );
\r
540 /* Remember the parameters for the next time the function is called. */
\r
541 ulLastParameter1 = ulParameter1;
\r
542 ulLastParameter2 = ulParameter2;
\r
544 /* Remove compiler warnings in case configASSERT() is not defined. */
\r
545 ( void ) ulLastParameter1;
\r
546 ( void ) ulLastParameter2;
\r
548 /*-----------------------------------------------------------*/
\r
550 static void prvTestTimerCallback( TimerHandle_t xTimer )
\r
552 /* This is the callback function for the timer accessed by
\r
553 prvDemonstrateTimerQueryFunctions(). The callback does not do anything. */
\r
556 /*-----------------------------------------------------------*/
\r
558 static void prvDemonstrateTimerQueryFunctions( void )
\r
560 static TimerHandle_t xTimer = NULL;
\r
561 const char *pcTimerName = "TestTimer";
\r
562 volatile TickType_t xExpiryTime;
\r
563 const TickType_t xDontBlock = 0;
\r
565 if( xTimer == NULL )
\r
567 xTimer = xTimerCreate( pcTimerName, portMAX_DELAY, pdTRUE, NULL, prvTestTimerCallback );
\r
569 if( xTimer != NULL )
\r
571 /* Called from the idle task so a block time must not be
\r
573 xTimerStart( xTimer, xDontBlock );
\r
577 if( xTimer != NULL )
\r
579 /* Demonstrate querying a timer's name. */
\r
580 configASSERT( strcmp( pcTimerGetName( xTimer ), pcTimerName ) == 0 );
\r
582 /* Demonstrate querying a timer's period. */
\r
583 configASSERT( xTimerGetPeriod( xTimer ) == portMAX_DELAY );
\r
585 /* Demonstrate querying a timer's next expiry time, although nothing is
\r
586 done with the returned value. Note if the expiry time is less than the
\r
587 maximum tick count then the expiry time has overflowed from the current
\r
588 time. In this case the expiry time was set to portMAX_DELAY, so it is
\r
589 expected to be less than the current time until the current time has
\r
590 itself overflowed. */
\r
591 xExpiryTime = xTimerGetExpiryTime( xTimer );
\r
592 ( void ) xExpiryTime;
\r
595 /*-----------------------------------------------------------*/
\r
597 static void prvDemonstratePendingFunctionCall( void )
\r
599 static uint32_t ulParameter1 = 1000UL, ulParameter2 = 0UL;
\r
600 const TickType_t xDontBlock = 0; /* This is called from the idle task so must *not* attempt to block. */
\r
602 /* prvPendedFunction() just expects the parameters to be incremented by one
\r
603 each time it is called. */
\r
607 /* Pend the function call, sending the parameters. */
\r
608 xTimerPendFunctionCall( prvPendedFunction, ( void * ) ulParameter1, ulParameter2, xDontBlock );
\r
610 /*-----------------------------------------------------------*/
\r
612 static void prvDemonstrateTaskStateAndHandleGetFunctions( void )
\r
614 TaskHandle_t xIdleTaskHandle, xTimerTaskHandle;
\r
616 static portBASE_TYPE xPerformedOneShotTests = pdFALSE;
\r
617 TaskHandle_t xTestTask;
\r
618 TaskStatus_t xTaskInfo;
\r
619 extern StackType_t uxTimerTaskStack[];
\r
620 static TickType_t xLastIdleExecutionTime = 0;
\r
621 TickType_t xIdleExecutionTime;
\r
623 /* Demonstrate the use of the xTimerGetTimerDaemonTaskHandle() and
\r
624 xTaskGetIdleTaskHandle() functions. Also try using the function that sets
\r
625 the task number. */
\r
626 xIdleTaskHandle = xTaskGetIdleTaskHandle();
\r
627 xTimerTaskHandle = xTimerGetTimerDaemonTaskHandle();
\r
629 /* This is the idle hook, so the current task handle should equal the
\r
630 returned idle task handle. */
\r
631 if( xTaskGetCurrentTaskHandle() != xIdleTaskHandle )
\r
633 pcStatusMessage = "Error: Returned idle task handle was incorrect";
\r
636 /* Check the same handle is obtained using the idle task's name. First try
\r
637 with the wrong name, then the right name. */
\r
638 if( xTaskGetHandle( "Idle" ) == xIdleTaskHandle )
\r
640 pcStatusMessage = "Error: Returned handle for name Idle was incorrect";
\r
643 if( xTaskGetHandle( "IDLE" ) != xIdleTaskHandle )
\r
645 pcStatusMessage = "Error: Returned handle for name Idle was incorrect";
\r
648 /* Check the timer task handle was returned correctly. */
\r
649 pcTaskName = pcTaskGetName( xTimerTaskHandle );
\r
650 if( strcmp( pcTaskName, "Tmr Svc" ) != 0 )
\r
652 pcStatusMessage = "Error: Returned timer task handle was incorrect";
\r
655 if( xTaskGetHandle( "Tmr Svc" ) != xTimerTaskHandle )
\r
657 pcStatusMessage = "Error: Returned handle for name Tmr Svc was incorrect";
\r
660 /* This task is running, make sure it's state is returned as running. */
\r
661 if( eTaskStateGet( xIdleTaskHandle ) != eRunning )
\r
663 pcStatusMessage = "Error: Returned idle task state was incorrect";
\r
666 /* If this task is running, then the timer task must be blocked. */
\r
667 if( eTaskStateGet( xTimerTaskHandle ) != eBlocked )
\r
669 pcStatusMessage = "Error: Returned timer task state was incorrect";
\r
672 /* Also with the vTaskGetInfo() function. */
\r
673 vTaskGetInfo( xTimerTaskHandle, /* The task being queried. */
\r
674 &xTaskInfo, /* The structure into which information on the task will be written. */
\r
675 pdTRUE, /* Include the task's high watermark in the structure. */
\r
676 eInvalid ); /* Include the task state in the structure. */
\r
678 /* Check the information returned by vTaskGetInfo() is as expected. */
\r
679 if( ( xTaskInfo.eCurrentState != eBlocked ) ||
\r
680 ( strcmp( xTaskInfo.pcTaskName, "Tmr Svc" ) != 0 ) ||
\r
681 ( xTaskInfo.uxCurrentPriority != configTIMER_TASK_PRIORITY ) ||
\r
682 ( xTaskInfo.pxStackBase != uxTimerTaskStack ) ||
\r
683 ( xTaskInfo.xHandle != xTimerTaskHandle ) )
\r
685 pcStatusMessage = "Error: vTaskGetInfo() returned incorrect information about the timer task";
\r
688 /* Other tests that should only be performed once follow. The test task
\r
689 is not created on each iteration because to do so would cause the death
\r
690 task to report an error (too many tasks running). */
\r
691 if( xPerformedOneShotTests == pdFALSE )
\r
693 /* Don't run this part of the test again. */
\r
694 xPerformedOneShotTests = pdTRUE;
\r
696 /* Create a test task to use to test other eTaskStateGet() return values. */
\r
697 if( xTaskCreate( prvTestTask, "Test", configMINIMAL_STACK_SIZE, NULL, tskIDLE_PRIORITY, &xTestTask ) == pdPASS )
\r
699 /* If this task is running, the test task must be in the ready state. */
\r
700 if( eTaskStateGet( xTestTask ) != eReady )
\r
702 pcStatusMessage = "Error: Returned test task state was incorrect 1";
\r
705 /* Now suspend the test task and check its state is reported correctly. */
\r
706 vTaskSuspend( xTestTask );
\r
707 if( eTaskStateGet( xTestTask ) != eSuspended )
\r
709 pcStatusMessage = "Error: Returned test task state was incorrect 2";
\r
712 /* Now delete the task and check its state is reported correctly. */
\r
713 vTaskDelete( xTestTask );
\r
714 if( eTaskStateGet( xTestTask ) != eDeleted )
\r
716 pcStatusMessage = "Error: Returned test task state was incorrect 3";
\r
721 xIdleExecutionTime = xTaskGetIdleRunTimeCounter();
\r
722 if( xIdleExecutionTime == xLastIdleExecutionTime )
\r
724 pcStatusMessage = "Error: Total amount of Idle task execution time did not change";
\r
726 xLastIdleExecutionTime = xIdleExecutionTime;
\r
728 /*-----------------------------------------------------------*/
\r
730 static void prvDemoQueueSpaceFunctions( void *pvParameters )
\r
732 QueueHandle_t xQueue = NULL;
\r
733 const unsigned portBASE_TYPE uxQueueLength = 10;
\r
734 unsigned portBASE_TYPE uxReturn, x;
\r
736 /* Remove compiler warnings. */
\r
737 ( void ) pvParameters;
\r
739 /* Create the queue that will be used. Nothing is actually going to be
\r
740 sent or received so the queue item size is set to 0. */
\r
741 xQueue = xQueueCreate( uxQueueLength, 0 );
\r
742 configASSERT( xQueue );
\r
746 for( x = 0; x < uxQueueLength; x++ )
\r
748 /* Ask how many messages are available... */
\r
749 uxReturn = uxQueueMessagesWaiting( xQueue );
\r
751 /* Check the number of messages being reported as being available
\r
752 is as expected, and force an assert if not. */
\r
753 if( uxReturn != x )
\r
755 /* xQueue cannot be NULL so this is deliberately causing an
\r
756 assert to be triggered as there is an error. */
\r
757 configASSERT( xQueue == NULL );
\r
760 /* Ask how many spaces remain in the queue... */
\r
761 uxReturn = uxQueueSpacesAvailable( xQueue );
\r
763 /* Check the number of spaces being reported as being available
\r
764 is as expected, and force an assert if not. */
\r
765 if( uxReturn != ( uxQueueLength - x ) )
\r
767 /* xQueue cannot be NULL so this is deliberately causing an
\r
768 assert to be triggered as there is an error. */
\r
769 configASSERT( xQueue == NULL );
\r
772 /* Fill one more space in the queue. */
\r
773 xQueueSendToBack( xQueue, NULL, 0 );
\r
776 /* Perform the same check while the queue is full. */
\r
777 uxReturn = uxQueueMessagesWaiting( xQueue );
\r
778 if( uxReturn != uxQueueLength )
\r
780 configASSERT( xQueue == NULL );
\r
783 uxReturn = uxQueueSpacesAvailable( xQueue );
\r
785 if( uxReturn != 0 )
\r
787 configASSERT( xQueue == NULL );
\r
790 /* The queue is full, start again. */
\r
791 xQueueReset( xQueue );
\r
793 #if( configUSE_PREEMPTION == 0 )
\r
798 /*-----------------------------------------------------------*/
\r
800 static void prvPermanentlyBlockingSemaphoreTask( void *pvParameters )
\r
802 SemaphoreHandle_t xSemaphore;
\r
804 /* Prevent compiler warning about unused parameter in the case that
\r
805 configASSERT() is not defined. */
\r
806 ( void ) pvParameters;
\r
808 /* This task should block on a semaphore, and never return. */
\r
809 xSemaphore = xSemaphoreCreateBinary();
\r
810 configASSERT( xSemaphore );
\r
812 xSemaphoreTake( xSemaphore, portMAX_DELAY );
\r
814 /* The above xSemaphoreTake() call should never return, force an assert if
\r
816 configASSERT( pvParameters != NULL );
\r
817 vTaskDelete( NULL );
\r
819 /*-----------------------------------------------------------*/
\r
821 static void prvPermanentlyBlockingNotificationTask( void *pvParameters )
\r
823 /* Prevent compiler warning about unused parameter in the case that
\r
824 configASSERT() is not defined. */
\r
825 ( void ) pvParameters;
\r
827 /* This task should block on a task notification, and never return. */
\r
828 ulTaskNotifyTake( pdTRUE, portMAX_DELAY );
\r
830 /* The above ulTaskNotifyTake() call should never return, force an assert
\r
832 configASSERT( pvParameters != NULL );
\r
833 vTaskDelete( NULL );
\r
835 /*-----------------------------------------------------------*/
\r
837 static void prvReloadModeTestTimerCallback( TimerHandle_t xTimer )
\r
839 uint32_t ulTimerID;
\r
841 /* Increment the timer's ID to show the callback has executed. */
\r
842 ulTimerID = ( uint32_t ) pvTimerGetTimerID( xTimer );
\r
844 vTimerSetTimerID( xTimer, ( void * ) ulTimerID );
\r
846 /*-----------------------------------------------------------*/
\r
848 static void prvDemonstrateChangingTimerReloadMode( void *pvParameters )
\r
850 TimerHandle_t xTimer;
\r
851 const char * const pcTimerName = "TestTimer";
\r
852 const TickType_t x100ms = pdMS_TO_TICKS( 100UL );
\r
854 /* Avoid compiler warnings about unused parameter. */
\r
855 ( void ) pvParameters;
\r
857 xTimer = xTimerCreate( pcTimerName,
\r
859 pdFALSE, /* Created as a one shot timer. */
\r
861 prvReloadModeTestTimerCallback );
\r
862 configASSERT( xTimer );
\r
863 configASSERT( xTimerIsTimerActive( xTimer ) == pdFALSE );
\r
864 configASSERT( xTimerGetTimerDaemonTaskHandle() != NULL );
\r
865 configASSERT( strcmp( pcTimerName, pcTimerGetName( xTimer ) ) == 0 );
\r
866 configASSERT( xTimerGetPeriod( xTimer ) == x100ms );
\r
868 /* Timer was created as a one shot timer. Its callback just increments the
\r
869 timer's ID - so set the ID to 0, let the timer run for a number of timeout
\r
870 periods, then check the timer has only executed once. */
\r
871 vTimerSetTimerID( xTimer, ( void * ) 0 );
\r
872 xTimerStart( xTimer, portMAX_DELAY );
\r
873 vTaskDelay( 3UL * x100ms );
\r
874 configASSERT( ( ( uint32_t ) ( pvTimerGetTimerID( xTimer ) ) ) == 1UL );
\r
876 /* Now change the timer to be an autoreload timer and check it executes
\r
877 the expected number of times. */
\r
878 vTimerSetReloadMode( xTimer, pdTRUE );
\r
879 vTimerSetTimerID( xTimer, ( void * ) 0 );
\r
880 xTimerStart( xTimer, 0 );
\r
881 vTaskDelay( ( 3UL * x100ms ) + ( x100ms / 2UL ) ); /* Three full periods. */
\r
882 configASSERT( ( uint32_t ) ( pvTimerGetTimerID( xTimer ) ) == 3UL );
\r
883 configASSERT( xTimerStop( xTimer, 0 ) != pdFAIL );
\r
885 /* Now change the timer back to be a one shot timer and check it only
\r
887 vTimerSetReloadMode( xTimer, pdFALSE );
\r
888 vTimerSetTimerID( xTimer, ( void * ) 0 );
\r
889 xTimerStart( xTimer, 0 );
\r
890 vTaskDelay( 3UL * x100ms );
\r
891 configASSERT( xTimerStop( xTimer, 0 ) != pdFAIL );
\r
892 configASSERT( ( uint32_t ) ( pvTimerGetTimerID( xTimer ) ) == 1UL );
\r
894 /* Clean up at the end. */
\r
895 xTimerDelete( xTimer, portMAX_DELAY );
\r
896 vTaskDelete( NULL );
\r