]> git.sur5r.net Git - freertos/blob - FreeRTOS/Demo/WIN32-MSVC/main.c
Prepare for V7.3.0 release.
[freertos] / FreeRTOS / Demo / WIN32-MSVC / main.c
1 /*\r
2     FreeRTOS V7.3.0 - Copyright (C) 2012 Real Time Engineers Ltd.\r
3 \r
4     FEATURES AND PORTS ARE ADDED TO FREERTOS ALL THE TIME.  PLEASE VISIT \r
5     http://www.FreeRTOS.org TO ENSURE YOU ARE USING THE LATEST VERSION.\r
6 \r
7     ***************************************************************************\r
8      *                                                                       *\r
9      *    FreeRTOS tutorial books are available in pdf and paperback.        *\r
10      *    Complete, revised, and edited pdf reference manuals are also       *\r
11      *    available.                                                         *\r
12      *                                                                       *\r
13      *    Purchasing FreeRTOS documentation will not only help you, by       *\r
14      *    ensuring you get running as quickly as possible and with an        *\r
15      *    in-depth knowledge of how to use FreeRTOS, it will also help       *\r
16      *    the FreeRTOS project to continue with its mission of providing     *\r
17      *    professional grade, cross platform, de facto standard solutions    *\r
18      *    for microcontrollers - completely free of charge!                  *\r
19      *                                                                       *\r
20      *    >>> See http://www.FreeRTOS.org/Documentation for details. <<<     *\r
21      *                                                                       *\r
22      *    Thank you for using FreeRTOS, and thank you for your support!      *\r
23      *                                                                       *\r
24     ***************************************************************************\r
25 \r
26 \r
27     This file is part of the FreeRTOS distribution.\r
28 \r
29     FreeRTOS is free software; you can redistribute it and/or modify it under\r
30     the terms of the GNU General Public License (version 2) as published by the\r
31     Free Software Foundation AND MODIFIED BY the FreeRTOS exception.\r
32     >>>NOTE<<< The modification to the GPL is included to allow you to\r
33     distribute a combined work that includes FreeRTOS without being obliged to\r
34     provide the source code for proprietary components outside of the FreeRTOS\r
35     kernel.  FreeRTOS is distributed in the hope that it will be useful, but\r
36     WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY\r
37     or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for\r
38     more details. You should have received a copy of the GNU General Public\r
39     License and the FreeRTOS license exception along with FreeRTOS; if not it\r
40     can be viewed here: http://www.freertos.org/a00114.html and also obtained\r
41     by writing to Richard Barry, contact details for whom are available on the\r
42     FreeRTOS WEB site.\r
43 \r
44     1 tab == 4 spaces!\r
45     \r
46     ***************************************************************************\r
47      *                                                                       *\r
48      *    Having a problem?  Start by reading the FAQ "My application does   *\r
49      *    not run, what could be wrong?"                                     *\r
50      *                                                                       *\r
51      *    http://www.FreeRTOS.org/FAQHelp.html                               *\r
52      *                                                                       *\r
53     ***************************************************************************\r
54 \r
55     \r
56     http://www.FreeRTOS.org - Documentation, training, latest versions, license \r
57     and contact details.  \r
58     \r
59     http://www.FreeRTOS.org/plus - A selection of FreeRTOS ecosystem products,\r
60     including FreeRTOS+Trace - an indispensable productivity tool.\r
61 \r
62     Real Time Engineers ltd license FreeRTOS to High Integrity Systems, who sell \r
63     the code with commercial support, indemnification, and middleware, under \r
64     the OpenRTOS brand: http://www.OpenRTOS.com.  High Integrity Systems also\r
65     provide a safety engineered and independently SIL3 certified version under \r
66     the SafeRTOS brand: http://www.SafeRTOS.com.\r
67 */\r
68 \r
69 /*\r
70  *******************************************************************************\r
71  * -NOTE- The Win32 port is a simulation (or is that emulation?) only!  Do not\r
72  * expect to get real time behaviour from the Win32 port or this demo\r
73  * application.  It is provided as a convenient development and demonstration\r
74  * test bed only.  This was tested using Windows XP on a dual core laptop.\r
75  *\r
76  * - READ THE WEB DOCUMENTATION FOR THIS PORT FOR MORE INFORMATION ON USING IT -\r
77  *******************************************************************************\r
78  *\r
79  * main() creates all the demo application tasks, then starts the scheduler.  \r
80  * The web documentation provides more details of the standard demo application \r
81  * tasks, which provide no particular functionality but do provide a good \r
82  * example of how to use the FreeRTOS API.\r
83  *\r
84  * In addition to the standard demo tasks, the following tasks and tests are\r
85  * defined and/or created within this file:\r
86  *\r
87  * "Check" task - This only executes every five seconds but has a high priority\r
88  * to ensure it gets processor time.  Its main function is to check that all the\r
89  * standard demo tasks are still operational.  While no errors have been\r
90  * discovered the check task will print out "OK" and the current simulated tick\r
91  * time.  If an error is discovered in the execution of a task then the check\r
92  * task will print out an appropriate error message.\r
93  *\r
94  */\r
95 \r
96 \r
97 /* Standard includes. */\r
98 #include <stdio.h>\r
99 \r
100 /* Kernel includes. */\r
101 #include <FreeRTOS.h>\r
102 #include "task.h"\r
103 #include "queue.h"\r
104 #include "timers.h"\r
105 #include "semphr.h"\r
106 \r
107 /* Standard demo includes. */\r
108 #include "BlockQ.h"\r
109 #include "integer.h"\r
110 #include "semtest.h"\r
111 #include "PollQ.h"\r
112 #include "GenQTest.h"\r
113 #include "QPeek.h"\r
114 #include "recmutex.h"\r
115 #include "flop.h"\r
116 #include "TimerDemo.h"\r
117 #include "countsem.h"\r
118 #include "death.h"\r
119 #include "dynamic.h"\r
120 \r
121 /* Priorities at which the tasks are created. */\r
122 #define mainCHECK_TASK_PRIORITY         ( configMAX_PRIORITIES - 1 )\r
123 #define mainQUEUE_POLL_PRIORITY         ( tskIDLE_PRIORITY + 1 )\r
124 #define mainSEM_TEST_PRIORITY           ( tskIDLE_PRIORITY + 1 )\r
125 #define mainBLOCK_Q_PRIORITY            ( tskIDLE_PRIORITY + 2 )\r
126 #define mainCREATOR_TASK_PRIORITY   ( tskIDLE_PRIORITY + 3 )\r
127 #define mainFLASH_TASK_PRIORITY         ( tskIDLE_PRIORITY + 1 )\r
128 #define mainuIP_TASK_PRIORITY           ( tskIDLE_PRIORITY + 2 )\r
129 #define mainINTEGER_TASK_PRIORITY   ( tskIDLE_PRIORITY )\r
130 #define mainGEN_QUEUE_TASK_PRIORITY     ( tskIDLE_PRIORITY )\r
131 #define mainFLOP_TASK_PRIORITY          ( tskIDLE_PRIORITY )\r
132 \r
133 #define mainTIMER_TEST_PERIOD                   ( 50 )\r
134 \r
135 /* Task function prototypes. */\r
136 static void prvCheckTask( void *pvParameters );\r
137 \r
138 /* A task that is created from the idle task to test the functionality of \r
139 eTaskStateGet(). */\r
140 static void prvTestTask( void *pvParameters );\r
141 \r
142 /* The variable into which error messages are latched. */\r
143 static char *pcStatusMessage = "OK";\r
144 \r
145 /* This semaphore is created purely to test using the vSemaphoreDelete() and\r
146 semaphore tracing API functions.  It has no other purpose. */\r
147 static xSemaphoreHandle xMutexToDelete = NULL;\r
148 \r
149 /*-----------------------------------------------------------*/\r
150 \r
151 int main( void )\r
152 {\r
153         /* Start the check task as described at the top of this file. */\r
154         xTaskCreate( prvCheckTask, ( signed char * ) "Check", configMINIMAL_STACK_SIZE, NULL, mainCHECK_TASK_PRIORITY, NULL );\r
155 \r
156         /* Create the standard demo tasks. */\r
157         vStartBlockingQueueTasks( mainBLOCK_Q_PRIORITY );\r
158         vStartSemaphoreTasks( mainSEM_TEST_PRIORITY );\r
159         vStartPolledQueueTasks( mainQUEUE_POLL_PRIORITY );\r
160         vStartIntegerMathTasks( mainINTEGER_TASK_PRIORITY );\r
161         vStartGenericQueueTasks( mainGEN_QUEUE_TASK_PRIORITY );\r
162         vStartQueuePeekTasks();\r
163         vStartMathTasks( mainFLOP_TASK_PRIORITY );\r
164         vStartRecursiveMutexTasks();\r
165         vStartTimerDemoTask( mainTIMER_TEST_PERIOD );\r
166         vStartCountingSemaphoreTasks();\r
167         vStartDynamicPriorityTasks();\r
168 \r
169         /* The suicide tasks must be created last as they need to know how many\r
170         tasks were running prior to their creation.  This then allows them to \r
171         ascertain whether or not the correct/expected number of tasks are running at \r
172         any given time. */\r
173         vCreateSuicidalTasks( mainCREATOR_TASK_PRIORITY );\r
174 \r
175         /* Create the semaphore that will be deleted in the idle task hook.  This\r
176         is done purely to test the use of vSemaphoreDelete(). */\r
177         xMutexToDelete = xSemaphoreCreateMutex();\r
178 \r
179         /* Start the scheduler itself. */\r
180         vTaskStartScheduler();\r
181 \r
182     /* Should never get here unless there was not enough heap space to create \r
183         the idle and other system tasks. */\r
184     return 0;\r
185 }\r
186 /*-----------------------------------------------------------*/\r
187 \r
188 static void prvCheckTask( void *pvParameters )\r
189 {\r
190 portTickType xNextWakeTime;\r
191 const portTickType xCycleFrequency = 1000 / portTICK_RATE_MS;\r
192 \r
193         /* Just to remove compiler warning. */\r
194         ( void ) pvParameters;\r
195 \r
196         /* Initialise xNextWakeTime - this only needs to be done once. */\r
197         xNextWakeTime = xTaskGetTickCount();\r
198 \r
199         for( ;; )\r
200         {\r
201                 /* Place this task in the blocked state until it is time to run again. */\r
202                 vTaskDelayUntil( &xNextWakeTime, xCycleFrequency );\r
203 \r
204                 /* Check the standard demo tasks are running without error. */\r
205                 if( xAreTimerDemoTasksStillRunning( xCycleFrequency ) != pdTRUE )\r
206                 {\r
207                         pcStatusMessage = "Error: TimerDemo";\r
208                 }\r
209             else if( xAreIntegerMathsTaskStillRunning() != pdTRUE )\r
210             {\r
211                         pcStatusMessage = "Error: IntMath";\r
212             }   \r
213                 else if( xAreGenericQueueTasksStillRunning() != pdTRUE )\r
214                 {                       \r
215                         pcStatusMessage = "Error: GenQueue";\r
216                 }\r
217                 else if( xAreQueuePeekTasksStillRunning() != pdTRUE )\r
218                 {\r
219                         pcStatusMessage = "Error: QueuePeek";\r
220                 }\r
221                 else if( xAreBlockingQueuesStillRunning() != pdTRUE )\r
222                 {\r
223                         pcStatusMessage = "Error: BlockQueue";\r
224                 }\r
225             else if( xAreSemaphoreTasksStillRunning() != pdTRUE )\r
226             {\r
227                         pcStatusMessage = "Error: SemTest";\r
228             }\r
229             else if( xArePollingQueuesStillRunning() != pdTRUE )\r
230             {\r
231                         pcStatusMessage = "Error: PollQueue";\r
232             }\r
233                 else if( xAreMathsTaskStillRunning() != pdPASS )\r
234                 {\r
235                         pcStatusMessage = "Error: Flop";\r
236                 }\r
237             else if( xAreRecursiveMutexTasksStillRunning() != pdTRUE )\r
238             {\r
239                         pcStatusMessage = "Error: RecMutex";\r
240                 }\r
241                 else if( xAreCountingSemaphoreTasksStillRunning() != pdTRUE )\r
242                 {\r
243                         pcStatusMessage = "Error: CountSem";\r
244                 }\r
245                 else if( xIsCreateTaskStillRunning() != pdTRUE )\r
246                 {\r
247                         pcStatusMessage = "Error: Death";\r
248                 }\r
249                 else if( xAreDynamicPriorityTasksStillRunning() != pdPASS )\r
250                 {\r
251                         pcStatusMessage = "Error: Dynamic\r\n";\r
252                 }\r
253 \r
254                 /* This is the only task that uses stdout so its ok to call printf() \r
255                 directly. */\r
256                 printf( "%s - %d\r\n", pcStatusMessage, xTaskGetTickCount() );\r
257         }\r
258 }\r
259 /*-----------------------------------------------------------*/\r
260 \r
261 static void prvTestTask( void *pvParameters )\r
262 {\r
263 const unsigned long ulMSToSleep = 5;\r
264 \r
265         /* Just to remove compiler warnings. */\r
266         ( void ) pvParameters;\r
267 \r
268         /* This task is just used to test the eTaskStateGet() function.  It\r
269         does not have anything to do. */\r
270         for( ;; )\r
271         {\r
272                 /* Sleep to reduce CPU load, but don't sleep indefinitely in case there are\r
273                 tasks waiting to be terminated by the idle task. */\r
274                 Sleep( ulMSToSleep );\r
275         }\r
276 }\r
277 /*-----------------------------------------------------------*/\r
278 \r
279 void vApplicationIdleHook( void )\r
280 {\r
281 const unsigned long ulMSToSleep = 15;\r
282 xTaskHandle xIdleTaskHandle, xTimerTaskHandle, xTestTask;\r
283 signed char *pcTaskName;\r
284 const unsigned char ucConstQueueNumber = 0xaaU, ucConstTaskNumber = 0x55U;\r
285 \r
286 /* These three functions are only meant for use by trace code, and not for\r
287 direct use from application code, hence their prototypes are not in queue.h. */\r
288 extern void vQueueSetQueueNumber( xQueueHandle pxQueue, unsigned char ucQueueNumber );\r
289 extern unsigned char ucQueueGetQueueNumber( xQueueHandle pxQueue );\r
290 extern unsigned char ucQueueGetQueueType( xQueueHandle pxQueue );\r
291 extern void vTaskSetTaskNumber( xTaskHandle xTask, unsigned portBASE_TYPE uxHandle );\r
292 extern unsigned portBASE_TYPE uxTaskGetTaskNumber( xTaskHandle xTask );\r
293 \r
294         /* Sleep to reduce CPU load, but don't sleep indefinitely in case there are\r
295         tasks waiting to be terminated by the idle task. */\r
296         Sleep( ulMSToSleep );\r
297 \r
298         /* Demonstrate the use of the xTimerGetTimerDaemonTaskHandle() and \r
299         xTaskGetIdleTaskHandle() functions.  Also try using the function that sets\r
300         the task number. */\r
301         xIdleTaskHandle = xTaskGetIdleTaskHandle();\r
302         xTimerTaskHandle = xTimerGetTimerDaemonTaskHandle();\r
303         vTaskSetTaskNumber( xIdleTaskHandle, ( unsigned long ) ucConstTaskNumber );\r
304         configASSERT( uxTaskGetTaskNumber( xIdleTaskHandle ) == ucConstTaskNumber );\r
305 \r
306         /* This is the idle hook, so the current task handle should equal the \r
307         returned idle task handle. */\r
308         if( xTaskGetCurrentTaskHandle() != xIdleTaskHandle )\r
309         {\r
310                 pcStatusMessage = "Error:  Returned idle task handle was incorrect";\r
311         }\r
312 \r
313         /* Check the timer task handle was returned correctly. */\r
314         pcTaskName = pcTaskGetTaskName( xTimerTaskHandle );\r
315         if( strcmp( pcTaskName, "Tmr Svc" ) != 0 )\r
316         {\r
317                 pcStatusMessage = "Error:  Returned timer task handle was incorrect";\r
318         }\r
319 \r
320         /* This task is running, make sure its state is returned as running. */\r
321         if( eTaskStateGet( xIdleTaskHandle ) != eRunning )\r
322         {\r
323                 pcStatusMessage = "Error:  Returned idle task state was incorrect";\r
324         }\r
325 \r
326         /* If this task is running, then the timer task must be blocked. */\r
327         if( eTaskStateGet( xTimerTaskHandle ) != eBlocked )\r
328         {\r
329                 pcStatusMessage = "Error:  Returned timer task state was incorrect";\r
330         }\r
331 \r
332         /* If xMutexToDelete has not already been deleted, then delete it now.\r
333         This is done purely to demonstrate the use of, and test, the \r
334         vSemaphoreDelete() macro.  Care must be taken not to delete a semaphore\r
335         that has tasks blocked on it. */\r
336         if( xMutexToDelete != NULL )\r
337         {\r
338                 /* Before deleting the semaphore, test the function used to set its\r
339                 number.  This would normally only be done from trace software, rather\r
340                 than application code. */\r
341                 vQueueSetQueueNumber( xMutexToDelete, ucConstQueueNumber );\r
342 \r
343                 /* Before deleting the semaphore, test the functions used to get its\r
344                 type and number.  Again, these would normally only be done from trace\r
345                 software, rather than application code. */\r
346                 configASSERT( ucQueueGetQueueNumber( xMutexToDelete ) == ucConstQueueNumber );\r
347                 configASSERT( ucQueueGetQueueType( xMutexToDelete ) == queueQUEUE_TYPE_MUTEX );\r
348                 vSemaphoreDelete( xMutexToDelete );\r
349                 xMutexToDelete = NULL;\r
350 \r
351                 /* Other tests that should only be performed once follow.  The test task\r
352                 is not created on each iteration because to do so would cause the death\r
353                 task to report an error (too many tasks running). */\r
354 \r
355                 /* Create a test task to use to test other eTaskStateGet() return values. */\r
356                 if( xTaskCreate( prvTestTask, "Test", configMINIMAL_STACK_SIZE, NULL, tskIDLE_PRIORITY, &xTestTask ) == pdPASS )\r
357                 {\r
358                         /* If this task is running, the test task must be in the ready state. */\r
359                         if( eTaskStateGet( xTestTask ) != eReady )\r
360                         {\r
361                                 pcStatusMessage = "Error: Returned test task state was incorrect 1";\r
362                         }\r
363 \r
364                         /* Now suspend the test task and check its state is reported correctly. */\r
365                         vTaskSuspend( xTestTask );\r
366                         if( eTaskStateGet( xTestTask ) != eSuspended )\r
367                         {\r
368                                 pcStatusMessage = "Error: Returned test task state was incorrect 2";\r
369                         }\r
370 \r
371                         /* Now delete the task and check its state is reported correctly. */\r
372                         vTaskDelete( xTestTask );\r
373                         if( eTaskStateGet( xTestTask ) != eDeleted )\r
374                         {\r
375                                 pcStatusMessage = "Error: Returned test task state was incorrect 3";\r
376                         }\r
377                 }\r
378 \r
379         }\r
380 }\r
381 /*-----------------------------------------------------------*/\r
382 \r
383 void vApplicationMallocFailedHook( void )\r
384 {\r
385         /* Can be implemented if required, but probably not required in this \r
386         environment and running this demo. */\r
387 }\r
388 /*-----------------------------------------------------------*/\r
389 \r
390 void vApplicationStackOverflowHook( void )\r
391 {\r
392         /* Can be implemented if required, but not required in this \r
393         environment and running this demo. */\r
394 }\r
395 /*-----------------------------------------------------------*/\r
396 \r
397 void vApplicationTickHook( void )\r
398 {\r
399         /* Call the periodic timer test, which tests the timer API functions that\r
400         can be called from an ISR. */\r
401         vTimerPeriodicISRTests();\r
402 }\r
403 /*-----------------------------------------------------------*/\r
404 \r
405 void vAssertCalled( void )\r
406 {\r
407         taskDISABLE_INTERRUPTS();\r
408         for( ;; );\r
409 }\r
410 \r