2 FreeRTOS V8.1.2 - Copyright (C) 2014 Real Time Engineers Ltd.
\r
5 VISIT http://www.FreeRTOS.org TO ENSURE YOU ARE USING THE LATEST VERSION.
\r
7 ***************************************************************************
\r
9 * FreeRTOS provides completely free yet professionally developed, *
\r
10 * robust, strictly quality controlled, supported, and cross *
\r
11 * platform software that has become a de facto standard. *
\r
13 * Help yourself get started quickly and support the FreeRTOS *
\r
14 * project by purchasing a FreeRTOS tutorial book, reference *
\r
15 * manual, or both from: http://www.FreeRTOS.org/Documentation *
\r
19 ***************************************************************************
\r
21 This file is part of the FreeRTOS distribution.
\r
23 FreeRTOS is free software; you can redistribute it and/or modify it under
\r
24 the terms of the GNU General Public License (version 2) as published by the
\r
25 Free Software Foundation >>!AND MODIFIED BY!<< the FreeRTOS exception.
\r
27 >>! NOTE: The modification to the GPL is included to allow you to !<<
\r
28 >>! distribute a combined work that includes FreeRTOS without being !<<
\r
29 >>! obliged to provide the source code for proprietary components !<<
\r
30 >>! outside of the FreeRTOS kernel. !<<
\r
32 FreeRTOS is distributed in the hope that it will be useful, but WITHOUT ANY
\r
33 WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
\r
34 FOR A PARTICULAR PURPOSE. Full license text is available from the following
\r
35 link: http://www.freertos.org/a00114.html
\r
39 ***************************************************************************
\r
41 * Having a problem? Start by reading the FAQ "My application does *
\r
42 * not run, what could be wrong?" *
\r
44 * http://www.FreeRTOS.org/FAQHelp.html *
\r
46 ***************************************************************************
\r
48 http://www.FreeRTOS.org - Documentation, books, training, latest versions,
\r
49 license and Real Time Engineers Ltd. contact details.
\r
51 http://www.FreeRTOS.org/plus - A selection of FreeRTOS ecosystem products,
\r
52 including FreeRTOS+Trace - an indispensable productivity tool, a DOS
\r
53 compatible FAT file system, and our tiny thread aware UDP/IP stack.
\r
55 http://www.OpenRTOS.com - Real Time Engineers ltd license FreeRTOS to High
\r
56 Integrity Systems to sell under the OpenRTOS brand. Low cost OpenRTOS
\r
57 licenses offer ticketed support, indemnification and middleware.
\r
59 http://www.SafeRTOS.com - High Integrity Systems also provide a safety
\r
60 engineered and independently SIL3 certified version for use in safety and
\r
61 mission critical applications that require provable dependability.
\r
67 *******************************************************************************
\r
68 * NOTE 1: The Win32 port is a simulation (or is that emulation?) only! Do not
\r
69 * expect to get real time behaviour from the Win32 port or this demo
\r
70 * application. It is provided as a convenient development and demonstration
\r
71 * test bed only. This was tested using Windows XP on a dual core laptop.
\r
73 * Windows will not be running the FreeRTOS simulator threads continuously, so
\r
74 * the timing information in the FreeRTOS+Trace logs have no meaningful units.
\r
75 * See the documentation page for the Windows simulator for an explanation of
\r
77 * http://www.freertos.org/FreeRTOS-Windows-Simulator-Emulator-for-Visual-Studio-and-Eclipse-MingW.html
\r
78 * - READ THE WEB DOCUMENTATION FOR THIS PORT FOR MORE INFORMATION ON USING IT -
\r
80 * NOTE 2: This project provides two demo applications. A simple blinky style
\r
81 * project, and a more comprehensive test and demo application. The
\r
82 * mainCREATE_SIMPLE_BLINKY_DEMO_ONLY setting in main.c is used to select
\r
83 * between the two. See the notes on using mainCREATE_SIMPLE_BLINKY_DEMO_ONLY
\r
84 * in main.c. This file implements the comprehensive test and demo version.
\r
86 * NOTE 3: This file only contains the source code that is specific to the
\r
87 * basic demo. Generic functions, such FreeRTOS hook functions, are defined in
\r
89 *******************************************************************************
\r
91 * main() creates all the demo application tasks, then starts the scheduler.
\r
92 * The web documentation provides more details of the standard demo application
\r
93 * tasks, which provide no particular functionality but do provide a good
\r
94 * example of how to use the FreeRTOS API.
\r
96 * In addition to the standard demo tasks, the following tasks and tests are
\r
97 * defined and/or created within this file:
\r
99 * "Check" task - This only executes every five seconds but has a high priority
\r
100 * to ensure it gets processor time. Its main function is to check that all the
\r
101 * standard demo tasks are still operational. While no errors have been
\r
102 * discovered the check task will print out "OK" and the current simulated tick
\r
103 * time. If an error is discovered in the execution of a task then the check
\r
104 * task will print out an appropriate error message.
\r
109 /* Standard includes. */
\r
111 #include <stdlib.h>
\r
113 /* Kernel includes. */
\r
114 #include <FreeRTOS.h>
\r
117 #include <timers.h>
\r
118 #include <semphr.h>
\r
120 /* Standard demo includes. */
\r
121 #include "BlockQ.h"
\r
122 #include "integer.h"
\r
123 #include "semtest.h"
\r
125 #include "GenQTest.h"
\r
127 #include "recmutex.h"
\r
129 #include "TimerDemo.h"
\r
130 #include "countsem.h"
\r
132 #include "dynamic.h"
\r
133 #include "QueueSet.h"
\r
134 #include "QueueOverwrite.h"
\r
135 #include "EventGroupsDemo.h"
\r
136 #include "IntSemTest.h"
\r
137 #include "TaskNotify.h"
\r
139 /* Priorities at which the tasks are created. */
\r
140 #define mainCHECK_TASK_PRIORITY ( configMAX_PRIORITIES - 2 )
\r
141 #define mainQUEUE_POLL_PRIORITY ( tskIDLE_PRIORITY + 1 )
\r
142 #define mainSEM_TEST_PRIORITY ( tskIDLE_PRIORITY + 1 )
\r
143 #define mainBLOCK_Q_PRIORITY ( tskIDLE_PRIORITY + 2 )
\r
144 #define mainCREATOR_TASK_PRIORITY ( tskIDLE_PRIORITY + 3 )
\r
145 #define mainFLASH_TASK_PRIORITY ( tskIDLE_PRIORITY + 1 )
\r
146 #define mainINTEGER_TASK_PRIORITY ( tskIDLE_PRIORITY )
\r
147 #define mainGEN_QUEUE_TASK_PRIORITY ( tskIDLE_PRIORITY )
\r
148 #define mainFLOP_TASK_PRIORITY ( tskIDLE_PRIORITY )
\r
149 #define mainQUEUE_OVERWRITE_PRIORITY ( tskIDLE_PRIORITY )
\r
151 #define mainTIMER_TEST_PERIOD ( 50 )
\r
153 /* Task function prototypes. */
\r
154 static void prvCheckTask( void *pvParameters );
\r
156 /* A task that is created from the idle task to test the functionality of
\r
157 eTaskStateGet(). */
\r
158 static void prvTestTask( void *pvParameters );
\r
161 * Called from the idle task hook function to demonstrate a few utility
\r
162 * functions that are not demonstrated by any of the standard demo tasks.
\r
164 static void prvDemonstrateTaskStateAndHandleGetFunctions( void );
\r
167 * Called from the idle task hook function to demonstrate the use of
\r
168 * xTimerPendFunctionCall() as xTimerPendFunctionCall() is not demonstrated by
\r
169 * any of the standard demo tasks.
\r
171 static void prvDemonstratePendingFunctionCall( void );
\r
174 * The function that is pended by prvDemonstratePendingFunctionCall().
\r
176 static void prvPendedFunction( void *pvParameter1, uint32_t ulParameter2 );
\r
179 * A task to demonstrate the use of the xQueueSpacesAvailable() function.
\r
181 static void prvDemoQueueSpaceFunctions( void *pvParameters );
\r
183 /*-----------------------------------------------------------*/
\r
185 /* The variable into which error messages are latched. */
\r
186 static char *pcStatusMessage = "OK";
\r
188 /* This semaphore is created purely to test using the vSemaphoreDelete() and
\r
189 semaphore tracing API functions. It has no other purpose. */
\r
190 static SemaphoreHandle_t xMutexToDelete = NULL;
\r
192 /*-----------------------------------------------------------*/
\r
194 int main_full( void )
\r
196 /* Start the check task as described at the top of this file. */
\r
197 xTaskCreate( prvCheckTask, "Check", configMINIMAL_STACK_SIZE, NULL, mainCHECK_TASK_PRIORITY, NULL );
\r
199 /* Create the standard demo tasks. */
\r
200 vStartTaskNotifyTask();
\r
201 vStartBlockingQueueTasks( mainBLOCK_Q_PRIORITY );
\r
202 vStartSemaphoreTasks( mainSEM_TEST_PRIORITY );
\r
203 vStartPolledQueueTasks( mainQUEUE_POLL_PRIORITY );
\r
204 vStartIntegerMathTasks( mainINTEGER_TASK_PRIORITY );
\r
205 vStartGenericQueueTasks( mainGEN_QUEUE_TASK_PRIORITY );
\r
206 vStartQueuePeekTasks();
\r
207 vStartMathTasks( mainFLOP_TASK_PRIORITY );
\r
208 vStartRecursiveMutexTasks();
\r
209 vStartCountingSemaphoreTasks();
\r
210 vStartDynamicPriorityTasks();
\r
211 vStartQueueSetTasks();
\r
212 vStartQueueOverwriteTask( mainQUEUE_OVERWRITE_PRIORITY );
\r
213 xTaskCreate( prvDemoQueueSpaceFunctions, "QSpace", configMINIMAL_STACK_SIZE, NULL, tskIDLE_PRIORITY, NULL );
\r
214 vStartEventGroupTasks();
\r
215 vStartInterruptSemaphoreTasks();
\r
217 #if( configUSE_PREEMPTION != 0 )
\r
219 /* Don't expect these tasks to pass when preemption is not used. */
\r
220 vStartTimerDemoTask( mainTIMER_TEST_PERIOD );
\r
224 /* The suicide tasks must be created last as they need to know how many
\r
225 tasks were running prior to their creation. This then allows them to
\r
226 ascertain whether or not the correct/expected number of tasks are running at
\r
228 vCreateSuicidalTasks( mainCREATOR_TASK_PRIORITY );
\r
230 /* Create the semaphore that will be deleted in the idle task hook. This
\r
231 is done purely to test the use of vSemaphoreDelete(). */
\r
232 xMutexToDelete = xSemaphoreCreateMutex();
\r
234 /* Start the scheduler itself. */
\r
235 vTaskStartScheduler();
\r
237 /* Should never get here unless there was not enough heap space to create
\r
238 the idle and other system tasks. */
\r
241 /*-----------------------------------------------------------*/
\r
243 static void prvCheckTask( void *pvParameters )
\r
245 TickType_t xNextWakeTime;
\r
246 const TickType_t xCycleFrequency = 2500 / portTICK_PERIOD_MS;
\r
248 /* Just to remove compiler warning. */
\r
249 ( void ) pvParameters;
\r
251 /* Initialise xNextWakeTime - this only needs to be done once. */
\r
252 xNextWakeTime = xTaskGetTickCount();
\r
256 /* Place this task in the blocked state until it is time to run again. */
\r
257 vTaskDelayUntil( &xNextWakeTime, xCycleFrequency );
\r
259 /* Check the standard demo tasks are running without error. */
\r
260 #if( configUSE_PREEMPTION != 0 )
\r
262 /* These tasks are only created when preemption is used. */
\r
263 if( xAreTimerDemoTasksStillRunning( xCycleFrequency ) != pdTRUE )
\r
265 pcStatusMessage = "Error: TimerDemo";
\r
270 if( xAreTaskNotificationTasksStillRunning() != pdTRUE )
\r
272 pcStatusMessage = "Error: Notification";
\r
275 if( xAreInterruptSemaphoreTasksStillRunning() != pdTRUE )
\r
277 pcStatusMessage = "Error: IntSem";
\r
279 else if( xAreEventGroupTasksStillRunning() != pdTRUE )
\r
281 pcStatusMessage = "Error: EventGroup";
\r
283 else if( xAreIntegerMathsTaskStillRunning() != pdTRUE )
\r
285 pcStatusMessage = "Error: IntMath";
\r
287 else if( xAreGenericQueueTasksStillRunning() != pdTRUE )
\r
289 pcStatusMessage = "Error: GenQueue";
\r
291 else if( xAreQueuePeekTasksStillRunning() != pdTRUE )
\r
293 pcStatusMessage = "Error: QueuePeek";
\r
295 else if( xAreBlockingQueuesStillRunning() != pdTRUE )
\r
297 pcStatusMessage = "Error: BlockQueue";
\r
299 else if( xAreSemaphoreTasksStillRunning() != pdTRUE )
\r
301 pcStatusMessage = "Error: SemTest";
\r
303 else if( xArePollingQueuesStillRunning() != pdTRUE )
\r
305 pcStatusMessage = "Error: PollQueue";
\r
307 else if( xAreMathsTaskStillRunning() != pdPASS )
\r
309 pcStatusMessage = "Error: Flop";
\r
311 else if( xAreRecursiveMutexTasksStillRunning() != pdTRUE )
\r
313 pcStatusMessage = "Error: RecMutex";
\r
315 else if( xAreCountingSemaphoreTasksStillRunning() != pdTRUE )
\r
317 pcStatusMessage = "Error: CountSem";
\r
319 else if( xIsCreateTaskStillRunning() != pdTRUE )
\r
321 pcStatusMessage = "Error: Death";
\r
323 else if( xAreDynamicPriorityTasksStillRunning() != pdPASS )
\r
325 pcStatusMessage = "Error: Dynamic";
\r
327 else if( xAreQueueSetTasksStillRunning() != pdPASS )
\r
329 pcStatusMessage = "Error: Queue set";
\r
331 else if( xIsQueueOverwriteTaskStillRunning() != pdPASS )
\r
333 pcStatusMessage = "Error: Queue overwrite";
\r
336 /* This is the only task that uses stdout so its ok to call printf()
\r
338 printf( "%s - %d\r\n", pcStatusMessage, xTaskGetTickCount() );
\r
341 /*-----------------------------------------------------------*/
\r
343 static void prvTestTask( void *pvParameters )
\r
345 const unsigned long ulMSToSleep = 5;
\r
347 /* Just to remove compiler warnings. */
\r
348 ( void ) pvParameters;
\r
350 /* This task is just used to test the eTaskStateGet() function. It
\r
351 does not have anything to do. */
\r
354 /* Sleep to reduce CPU load, but don't sleep indefinitely in case there are
\r
355 tasks waiting to be terminated by the idle task. */
\r
356 Sleep( ulMSToSleep );
\r
359 /*-----------------------------------------------------------*/
\r
361 /* Called from vApplicationIdleHook(), which is defined in main.c. */
\r
362 void vFullDemoIdleFunction( void )
\r
364 const unsigned long ulMSToSleep = 15;
\r
367 /* Sleep to reduce CPU load, but don't sleep indefinitely in case there are
\r
368 tasks waiting to be terminated by the idle task. */
\r
369 Sleep( ulMSToSleep );
\r
371 /* Demonstrate a few utility functions that are not demonstrated by any of
\r
372 the standard demo tasks. */
\r
373 prvDemonstrateTaskStateAndHandleGetFunctions();
\r
375 /* Demonstrate the use of xTimerPendFunctionCall(), which is not
\r
376 demonstrated by any of the standard demo tasks. */
\r
377 prvDemonstratePendingFunctionCall();
\r
379 /* If xMutexToDelete has not already been deleted, then delete it now.
\r
380 This is done purely to demonstrate the use of, and test, the
\r
381 vSemaphoreDelete() macro. Care must be taken not to delete a semaphore
\r
382 that has tasks blocked on it. */
\r
383 if( xMutexToDelete != NULL )
\r
385 vSemaphoreDelete( xMutexToDelete );
\r
386 xMutexToDelete = NULL;
\r
389 /* Exercise heap_5 a bit. The malloc failed hook will trap failed
\r
390 allocations so there is no need to test here. */
\r
391 pvAllocated = pvPortMalloc( ( rand() % 100 ) + 1 );
\r
392 vPortFree( pvAllocated );
\r
394 /*-----------------------------------------------------------*/
\r
396 /* Called by vApplicationTickHook(), which is defined in main.c. */
\r
397 void vFullDemoTickHookFunction( void )
\r
399 /* Call the periodic timer test, which tests the timer API functions that
\r
400 can be called from an ISR. */
\r
401 #if( configUSE_PREEMPTION != 0 )
\r
403 /* Only created when preemption is used. */
\r
404 vTimerPeriodicISRTests();
\r
408 /* Call the periodic queue overwrite from ISR demo. */
\r
409 vQueueOverwritePeriodicISRDemo();
\r
411 /* Write to a queue that is in use as part of the queue set demo to
\r
412 demonstrate using queue sets from an ISR. */
\r
413 vQueueSetAccessQueueSetFromISR();
\r
415 /* Exercise event groups from interrupts. */
\r
416 vPeriodicEventGroupsProcessing();
\r
418 /* Exercise giving mutexes from an interrupt. */
\r
419 vInterruptSemaphorePeriodicTest();
\r
421 /* Exercise using task notifications from an interrupt. */
\r
422 xNotifyTaskFromISR();
\r
424 /*-----------------------------------------------------------*/
\r
426 static void prvPendedFunction( void *pvParameter1, uint32_t ulParameter2 )
\r
428 static uint32_t ulLastParameter1 = 1000UL, ulLastParameter2 = 0UL;
\r
429 uint32_t ulParameter1;
\r
431 ulParameter1 = ( uint32_t ) pvParameter1;
\r
433 /* Ensure the parameters are as expected. */
\r
434 configASSERT( ulParameter1 == ( ulLastParameter1 + 1 ) );
\r
435 configASSERT( ulParameter2 == ( ulLastParameter2 + 1 ) );
\r
437 /* Remember the parameters for the next time the function is called. */
\r
438 ulLastParameter1 = ulParameter1;
\r
439 ulLastParameter2 = ulParameter2;
\r
441 /*-----------------------------------------------------------*/
\r
443 static void prvDemonstratePendingFunctionCall( void )
\r
445 static uint32_t ulParameter1 = 1000UL, ulParameter2 = 0UL;
\r
446 const TickType_t xDontBlock = 0; /* This is called from the idle task so must *not* attempt to block. */
\r
448 /* prvPendedFunction() just expects the parameters to be incremented by one
\r
449 each time it is called. */
\r
453 /* Pend the function call, sending the parameters. */
\r
454 xTimerPendFunctionCall( prvPendedFunction, ( void * ) ulParameter1, ulParameter2, xDontBlock );
\r
456 /*-----------------------------------------------------------*/
\r
458 static void prvDemonstrateTaskStateAndHandleGetFunctions( void )
\r
460 TaskHandle_t xIdleTaskHandle, xTimerTaskHandle;
\r
462 static portBASE_TYPE xPerformedOneShotTests = pdFALSE;
\r
463 TaskHandle_t xTestTask;
\r
465 /* Demonstrate the use of the xTimerGetTimerDaemonTaskHandle() and
\r
466 xTaskGetIdleTaskHandle() functions. Also try using the function that sets
\r
467 the task number. */
\r
468 xIdleTaskHandle = xTaskGetIdleTaskHandle();
\r
469 xTimerTaskHandle = xTimerGetTimerDaemonTaskHandle();
\r
471 /* This is the idle hook, so the current task handle should equal the
\r
472 returned idle task handle. */
\r
473 if( xTaskGetCurrentTaskHandle() != xIdleTaskHandle )
\r
475 pcStatusMessage = "Error: Returned idle task handle was incorrect";
\r
478 /* Check the timer task handle was returned correctly. */
\r
479 pcTaskName = pcTaskGetTaskName( xTimerTaskHandle );
\r
480 if( strcmp( pcTaskName, "Tmr Svc" ) != 0 )
\r
482 pcStatusMessage = "Error: Returned timer task handle was incorrect";
\r
485 /* This task is running, make sure it's state is returned as running. */
\r
486 if( eTaskStateGet( xIdleTaskHandle ) != eRunning )
\r
488 pcStatusMessage = "Error: Returned idle task state was incorrect";
\r
491 /* If this task is running, then the timer task must be blocked. */
\r
492 if( eTaskStateGet( xTimerTaskHandle ) != eBlocked )
\r
494 pcStatusMessage = "Error: Returned timer task state was incorrect";
\r
497 /* Other tests that should only be performed once follow. The test task
\r
498 is not created on each iteration because to do so would cause the death
\r
499 task to report an error (too many tasks running). */
\r
500 if( xPerformedOneShotTests == pdFALSE )
\r
502 /* Don't run this part of the test again. */
\r
503 xPerformedOneShotTests = pdTRUE;
\r
505 /* Create a test task to use to test other eTaskStateGet() return values. */
\r
506 if( xTaskCreate( prvTestTask, "Test", configMINIMAL_STACK_SIZE, NULL, tskIDLE_PRIORITY, &xTestTask ) == pdPASS )
\r
508 /* If this task is running, the test task must be in the ready state. */
\r
509 if( eTaskStateGet( xTestTask ) != eReady )
\r
511 pcStatusMessage = "Error: Returned test task state was incorrect 1";
\r
514 /* Now suspend the test task and check its state is reported correctly. */
\r
515 vTaskSuspend( xTestTask );
\r
516 if( eTaskStateGet( xTestTask ) != eSuspended )
\r
518 pcStatusMessage = "Error: Returned test task state was incorrect 2";
\r
521 /* Now delete the task and check its state is reported correctly. */
\r
522 vTaskDelete( xTestTask );
\r
523 if( eTaskStateGet( xTestTask ) != eDeleted )
\r
525 pcStatusMessage = "Error: Returned test task state was incorrect 3";
\r
530 /*-----------------------------------------------------------*/
\r
532 static void prvDemoQueueSpaceFunctions( void *pvParameters )
\r
534 QueueHandle_t xQueue = NULL;
\r
535 const unsigned portBASE_TYPE uxQueueLength = 10;
\r
536 unsigned portBASE_TYPE uxReturn, x;
\r
538 /* Remove compiler warnings. */
\r
539 ( void ) pvParameters;
\r
541 /* Create the queue that will be used. Nothing is actually going to be
\r
542 sent or received so the queue item size is set to 0. */
\r
543 xQueue = xQueueCreate( uxQueueLength, 0 );
\r
544 configASSERT( xQueue );
\r
548 for( x = 0; x < uxQueueLength; x++ )
\r
550 /* Ask how many messages are available... */
\r
551 uxReturn = uxQueueMessagesWaiting( xQueue );
\r
553 /* Check the number of messages being reported as being available
\r
554 is as expected, and force an assert if not. */
\r
555 if( uxReturn != x )
\r
557 /* xQueue cannot be NULL so this is deliberately causing an
\r
558 assert to be triggered as there is an error. */
\r
559 configASSERT( xQueue == NULL );
\r
562 /* Ask how many spaces remain in the queue... */
\r
563 uxReturn = uxQueueSpacesAvailable( xQueue );
\r
565 /* Check the number of spaces being reported as being available
\r
566 is as expected, and force an assert if not. */
\r
567 if( uxReturn != ( uxQueueLength - x ) )
\r
569 /* xQueue cannot be NULL so this is deliberately causing an
\r
570 assert to be triggered as there is an error. */
\r
571 configASSERT( xQueue == NULL );
\r
574 /* Fill one more space in the queue. */
\r
575 xQueueSendToBack( xQueue, NULL, 0 );
\r
578 /* Perform the same check while the queue is full. */
\r
579 uxReturn = uxQueueMessagesWaiting( xQueue );
\r
580 if( uxReturn != uxQueueLength )
\r
582 configASSERT( xQueue == NULL );
\r
585 uxReturn = uxQueueSpacesAvailable( xQueue );
\r
587 if( uxReturn != 0 )
\r
589 configASSERT( xQueue == NULL );
\r
592 /* The queue is full, start again. */
\r
593 xQueueReset( xQueue );
\r
595 #if( configUSE_PREEMPTION == 0 )
\r