2 FreeRTOS V7.1.1 - Copyright (C) 2012 Real Time Engineers Ltd.
\r
5 ***************************************************************************
\r
7 * FreeRTOS tutorial books are available in pdf and paperback. *
\r
8 * Complete, revised, and edited pdf reference manuals are also *
\r
11 * Purchasing FreeRTOS documentation will not only help you, by *
\r
12 * ensuring you get running as quickly as possible and with an *
\r
13 * in-depth knowledge of how to use FreeRTOS, it will also help *
\r
14 * the FreeRTOS project to continue with its mission of providing *
\r
15 * professional grade, cross platform, de facto standard solutions *
\r
16 * for microcontrollers - completely free of charge! *
\r
18 * >>> See http://www.FreeRTOS.org/Documentation for details. <<< *
\r
20 * Thank you for using FreeRTOS, and thank you for your support! *
\r
22 ***************************************************************************
\r
25 This file is part of the FreeRTOS distribution.
\r
27 FreeRTOS is free software; you can redistribute it and/or modify it under
\r
28 the terms of the GNU General Public License (version 2) as published by the
\r
29 Free Software Foundation AND MODIFIED BY the FreeRTOS exception.
\r
30 >>>NOTE<<< The modification to the GPL is included to allow you to
\r
31 distribute a combined work that includes FreeRTOS without being obliged to
\r
32 provide the source code for proprietary components outside of the FreeRTOS
\r
33 kernel. FreeRTOS is distributed in the hope that it will be useful, but
\r
34 WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
\r
35 or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
\r
36 more details. You should have received a copy of the GNU General Public
\r
37 License and the FreeRTOS license exception along with FreeRTOS; if not it
\r
38 can be viewed here: http://www.freertos.org/a00114.html and also obtained
\r
39 by writing to Richard Barry, contact details for whom are available on the
\r
44 ***************************************************************************
\r
46 * Having a problem? Start by reading the FAQ "My application does *
\r
47 * not run, what could be wrong? *
\r
49 * http://www.FreeRTOS.org/FAQHelp.html *
\r
51 ***************************************************************************
\r
54 http://www.FreeRTOS.org - Documentation, training, latest information,
\r
55 license and contact details.
\r
57 http://www.FreeRTOS.org/plus - A selection of FreeRTOS ecosystem products,
\r
58 including FreeRTOS+Trace - an indispensable productivity tool.
\r
60 Real Time Engineers ltd license FreeRTOS to High Integrity Systems, who sell
\r
61 the code with commercial support, indemnification, and middleware, under
\r
62 the OpenRTOS brand: http://www.OpenRTOS.com. High Integrity Systems also
\r
63 provide a safety engineered and independently SIL3 certified version under
\r
64 the SafeRTOS brand: http://www.SafeRTOS.com.
\r
68 *******************************************************************************
\r
69 * -NOTE- The Win32 port is a simulation (or is that emulation?) only! Do not
\r
70 * expect to get real time behaviour from the Win32 port or this demo
\r
71 * application. It is provided as a convenient development and demonstration
\r
72 * test bed only. This was tested using Windows XP on a dual core laptop.
\r
74 * - READ THE WEB DOCUMENTATION FOR THIS PORT FOR MORE INFORMATION ON USING IT -
\r
75 *******************************************************************************
\r
77 * main() creates all the demo application tasks, then starts the scheduler.
\r
78 * The web documentation provides more details of the standard demo application
\r
79 * tasks, which provide no particular functionality but do provide a good
\r
80 * example of how to use the FreeRTOS API.
\r
82 * In addition to the standard demo tasks, the following tasks and tests are
\r
83 * defined and/or created within this file:
\r
85 * "Check" task - This only executes every five seconds but has a high priority
\r
86 * to ensure it gets processor time. Its main function is to check that all the
\r
87 * standard demo tasks are still operational. While no errors have been
\r
88 * discovered the check task will print out "OK" and the current simulated tick
\r
89 * time. If an error is discovered in the execution of a task then the check
\r
90 * task will print out an appropriate error message.
\r
95 /* Standard includes. */
\r
98 /* Kernel includes. */
\r
99 #include <FreeRTOS.h>
\r
102 #include "timers.h"
\r
103 #include "semphr.h"
\r
105 /* Standard demo includes. */
\r
106 #include "BlockQ.h"
\r
107 #include "integer.h"
\r
108 #include "semtest.h"
\r
110 #include "GenQTest.h"
\r
112 #include "recmutex.h"
\r
114 #include "TimerDemo.h"
\r
115 #include "countsem.h"
\r
117 #include "dynamic.h"
\r
119 /* Priorities at which the tasks are created. */
\r
120 #define mainCHECK_TASK_PRIORITY ( configMAX_PRIORITIES - 1 )
\r
121 #define mainQUEUE_POLL_PRIORITY ( tskIDLE_PRIORITY + 1 )
\r
122 #define mainSEM_TEST_PRIORITY ( tskIDLE_PRIORITY + 1 )
\r
123 #define mainBLOCK_Q_PRIORITY ( tskIDLE_PRIORITY + 2 )
\r
124 #define mainCREATOR_TASK_PRIORITY ( tskIDLE_PRIORITY + 3 )
\r
125 #define mainFLASH_TASK_PRIORITY ( tskIDLE_PRIORITY + 1 )
\r
126 #define mainuIP_TASK_PRIORITY ( tskIDLE_PRIORITY + 2 )
\r
127 #define mainINTEGER_TASK_PRIORITY ( tskIDLE_PRIORITY )
\r
128 #define mainGEN_QUEUE_TASK_PRIORITY ( tskIDLE_PRIORITY )
\r
129 #define mainFLOP_TASK_PRIORITY ( tskIDLE_PRIORITY )
\r
131 #define mainTIMER_TEST_PERIOD ( 50 )
\r
133 /* Task function prototypes. */
\r
134 static void prvCheckTask( void *pvParameters );
\r
136 /* The variable into which error messages are latched. */
\r
137 static char *pcStatusMessage = "OK";
\r
139 /* This semaphore is created purely to test using the vSemaphoreDelete() and
\r
140 semaphore tracing API functions. It has no other purpose. */
\r
141 static xSemaphoreHandle xMutexToDelete = NULL;
\r
143 /*-----------------------------------------------------------*/
\r
147 /* Start the check task as described at the top of this file. */
\r
148 xTaskCreate( prvCheckTask, ( signed char * ) "Check", configMINIMAL_STACK_SIZE, NULL, mainCHECK_TASK_PRIORITY, NULL );
\r
150 /* Create the standard demo tasks. */
\r
151 vStartBlockingQueueTasks( mainBLOCK_Q_PRIORITY );
\r
152 vStartSemaphoreTasks( mainSEM_TEST_PRIORITY );
\r
153 vStartPolledQueueTasks( mainQUEUE_POLL_PRIORITY );
\r
154 vStartIntegerMathTasks( mainINTEGER_TASK_PRIORITY );
\r
155 vStartGenericQueueTasks( mainGEN_QUEUE_TASK_PRIORITY );
\r
156 vStartQueuePeekTasks();
\r
157 vStartMathTasks( mainFLOP_TASK_PRIORITY );
\r
158 vStartRecursiveMutexTasks();
\r
159 vStartTimerDemoTask( mainTIMER_TEST_PERIOD );
\r
160 vStartCountingSemaphoreTasks();
\r
161 vStartDynamicPriorityTasks();
\r
163 /* The suicide tasks must be created last as they need to know how many
\r
164 tasks were running prior to their creation. This then allows them to
\r
165 ascertain whether or not the correct/expected number of tasks are running at
\r
167 vCreateSuicidalTasks( mainCREATOR_TASK_PRIORITY );
\r
169 /* Create the semaphore that will be deleted in the idle task hook. This
\r
170 is done purely to test the use of vSemaphoreDelete(). */
\r
171 xMutexToDelete = xSemaphoreCreateMutex();
\r
173 /* Start the scheduler itself. */
\r
174 vTaskStartScheduler();
\r
176 /* Should never get here unless there was not enough heap space to create
\r
177 the idle and other system tasks. */
\r
180 /*-----------------------------------------------------------*/
\r
182 static void prvCheckTask( void *pvParameters )
\r
184 portTickType xNextWakeTime;
\r
185 const portTickType xCycleFrequency = 1000 / portTICK_RATE_MS;
\r
187 /* Just to remove compiler warning. */
\r
188 ( void ) pvParameters;
\r
190 /* Initialise xNextWakeTime - this only needs to be done once. */
\r
191 xNextWakeTime = xTaskGetTickCount();
\r
195 /* Place this task in the blocked state until it is time to run again. */
\r
196 vTaskDelayUntil( &xNextWakeTime, xCycleFrequency );
\r
198 /* Check the standard demo tasks are running without error. */
\r
199 if( xAreTimerDemoTasksStillRunning( xCycleFrequency ) != pdTRUE )
\r
201 pcStatusMessage = "Error: TimerDemo";
\r
203 else if( xAreIntegerMathsTaskStillRunning() != pdTRUE )
\r
205 pcStatusMessage = "Error: IntMath";
\r
207 else if( xAreGenericQueueTasksStillRunning() != pdTRUE )
\r
209 pcStatusMessage = "Error: GenQueue";
\r
211 else if( xAreQueuePeekTasksStillRunning() != pdTRUE )
\r
213 pcStatusMessage = "Error: QueuePeek";
\r
215 else if( xAreBlockingQueuesStillRunning() != pdTRUE )
\r
217 pcStatusMessage = "Error: BlockQueue";
\r
219 else if( xAreSemaphoreTasksStillRunning() != pdTRUE )
\r
221 pcStatusMessage = "Error: SemTest";
\r
223 else if( xArePollingQueuesStillRunning() != pdTRUE )
\r
225 pcStatusMessage = "Error: PollQueue";
\r
227 else if( xAreMathsTaskStillRunning() != pdPASS )
\r
229 pcStatusMessage = "Error: Flop";
\r
231 else if( xAreRecursiveMutexTasksStillRunning() != pdTRUE )
\r
233 pcStatusMessage = "Error: RecMutex";
\r
235 else if( xAreCountingSemaphoreTasksStillRunning() != pdTRUE )
\r
237 pcStatusMessage = "Error: CountSem";
\r
239 else if( xIsCreateTaskStillRunning() != pdTRUE )
\r
241 pcStatusMessage = "Error: Death";
\r
243 else if( xAreDynamicPriorityTasksStillRunning() != pdPASS )
\r
245 pcStatusMessage = "Error: Dynamic\r\n";
\r
248 /* This is the only task that uses stdout so its ok to call printf()
\r
250 printf( "%s - %d\r\n", pcStatusMessage, xTaskGetTickCount() );
\r
253 /*-----------------------------------------------------------*/
\r
255 void vApplicationIdleHook( void )
\r
257 const unsigned long ulMSToSleep = 5;
\r
258 xTaskHandle xIdleTaskHandle, xTimerTaskHandle;
\r
259 signed char *pcTaskName;
\r
260 const unsigned char ucConstQueueNumber = 0xaaU, ucConstTaskNumber = 0x55U;
\r
262 /* These three functions are only meant for use by trace code, and not for
\r
263 direct use from application code, hence their prototypes are not in queue.h. */
\r
264 extern void vQueueSetQueueNumber( xQueueHandle pxQueue, unsigned char ucQueueNumber );
\r
265 extern unsigned char ucQueueGetQueueNumber( xQueueHandle pxQueue );
\r
266 extern unsigned char ucQueueGetQueueType( xQueueHandle pxQueue );
\r
267 extern void vTaskSetTaskNumber( xTaskHandle xTask, unsigned portBASE_TYPE uxHandle );
\r
268 extern unsigned portBASE_TYPE uxTaskGetTaskNumber( xTaskHandle xTask );
\r
270 /* Sleep to reduce CPU load, but don't sleep indefinitely in case there are
\r
271 tasks waiting to be terminated by the idle task. */
\r
272 Sleep( ulMSToSleep );
\r
274 /* Demonstrate the use of the xTimerGetTimerDaemonTaskHandle() and
\r
275 xTaskGetIdleTaskHandle() functions. Also try using the function that sets
\r
276 the task number. */
\r
277 xIdleTaskHandle = xTaskGetIdleTaskHandle();
\r
278 xTimerTaskHandle = xTimerGetTimerDaemonTaskHandle();
\r
279 vTaskSetTaskNumber( xIdleTaskHandle, ( unsigned long ) ucConstTaskNumber );
\r
280 configASSERT( uxTaskGetTaskNumber( xIdleTaskHandle ) == ucConstTaskNumber );
\r
282 /* This is the idle hook, so the current task handle should equal the
\r
283 returned idle task handle. */
\r
284 if( xTaskGetCurrentTaskHandle() != xIdleTaskHandle )
\r
286 pcStatusMessage = "Error: Returned idle task handle was incorrect";
\r
289 /* Check the timer task handle was returned correctly. */
\r
290 pcTaskName = pcTaskGetTaskName( xTimerTaskHandle );
\r
291 if( strcmp( pcTaskName, "Tmr Svc" ) != 0 )
\r
293 pcStatusMessage = "Error: Returned timer task handle was incorrect";
\r
296 /* If xMutexToDelete has not already been deleted, then delete it now.
\r
297 This is done purely to demonstrate the use of, and test, the
\r
298 vSemaphoreDelete() macro. Care must be taken not to delete a semaphore
\r
299 that has tasks blocked on it. */
\r
300 if( xMutexToDelete != NULL )
\r
302 /* Before deleting the semaphore, test the function used to set its
\r
303 number. This would normally only be done from trace software, rather
\r
304 than application code. */
\r
305 vQueueSetQueueNumber( xMutexToDelete, ucConstQueueNumber );
\r
307 /* Before deleting the semaphore, test the functions used to get its
\r
308 type and number. Again, these would normally only be done from trace
\r
309 software, rather than application code. */
\r
310 configASSERT( ucQueueGetQueueNumber( xMutexToDelete ) == ucConstQueueNumber );
\r
311 configASSERT( ucQueueGetQueueType( xMutexToDelete ) == queueQUEUE_TYPE_MUTEX );
\r
312 vSemaphoreDelete( xMutexToDelete );
\r
313 xMutexToDelete = NULL;
\r
316 /*-----------------------------------------------------------*/
\r
318 void vApplicationMallocFailedHook( void )
\r
320 /* Can be implemented if required, but probably not required in this
\r
321 environment and running this demo. */
\r
323 /*-----------------------------------------------------------*/
\r
325 void vApplicationStackOverflowHook( void )
\r
327 /* Can be implemented if required, but not required in this
\r
328 environment and running this demo. */
\r
330 /*-----------------------------------------------------------*/
\r
332 void vApplicationTickHook( void )
\r
334 /* Call the periodic timer test, which tests the timer API functions that
\r
335 can be called from an ISR. */
\r
336 vTimerPeriodicISRTests();
\r
338 /*-----------------------------------------------------------*/
\r
340 void vAssertCalled( void )
\r
342 taskDISABLE_INTERRUPTS();
\r