]> git.sur5r.net Git - freertos/blob - Demo/WIN32-MSVC/main.c
Move the MSVC/lwIP code into the main line.
[freertos] / Demo / WIN32-MSVC / main.c
1 /*\r
2     FreeRTOS V7.0.1 - Copyright (C) 2011 Real Time Engineers Ltd.\r
3         \r
4 \r
5     ***************************************************************************\r
6      *                                                                       *\r
7      *    FreeRTOS tutorial books are available in pdf and paperback.        *\r
8      *    Complete, revised, and edited pdf reference manuals are also       *\r
9      *    available.                                                         *\r
10      *                                                                       *\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
17      *                                                                       *\r
18      *    >>> See http://www.FreeRTOS.org/Documentation for details. <<<     *\r
19      *                                                                       *\r
20      *    Thank you for using FreeRTOS, and thank you for your support!      *\r
21      *                                                                       *\r
22     ***************************************************************************\r
23 \r
24 \r
25     This file is part of the FreeRTOS distribution.\r
26 \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
40     FreeRTOS WEB site.\r
41 \r
42     1 tab == 4 spaces!\r
43 \r
44     http://www.FreeRTOS.org - Documentation, latest information, license and\r
45     contact details.\r
46 \r
47     http://www.SafeRTOS.com - A version that is certified for use in safety\r
48     critical systems.\r
49 \r
50     http://www.OpenRTOS.com - Commercial support, development, porting,\r
51     licensing and training services.\r
52 */\r
53 \r
54 /*\r
55  *******************************************************************************\r
56  * -NOTE- The Win32 port is a simulation (or is that emulation?) only!  Do not\r
57  * expect to get real time behaviour from the Win32 port or this demo\r
58  * application.  It is provided as a convenient development and demonstration\r
59  * test bed only.  This was tested using Windows XP on a dual core laptop.\r
60  *\r
61  * - READ THE WEB DOCUMENTATION FOR THIS PORT FOR MORE INFORMATION ON USING IT -\r
62  *******************************************************************************\r
63  *\r
64  * main() creates all the demo application tasks, then starts the scheduler.  \r
65  * The web documentation provides more details of the standard demo application \r
66  * tasks, which provide no particular functionality but do provide a good \r
67  * example of how to use the FreeRTOS API.\r
68  *\r
69  * In addition to the standard demo tasks, the following tasks and tests are\r
70  * defined and/or created within this file:\r
71  *\r
72  * "Check" task - This only executes every five seconds but has a high priority\r
73  * to ensure it gets processor time.  Its main function is to check that all the\r
74  * standard demo tasks are still operational.  While no errors have been\r
75  * discovered the check task will print out "OK" and the current simulated tick\r
76  * time.  If an error is discovered in the execution of a task then the check\r
77  * task will print out an appropriate error message.\r
78  *\r
79  */\r
80 \r
81 \r
82 /* Standard includes. */\r
83 #include <stdio.h>\r
84 \r
85 /* Kernel includes. */\r
86 #include <FreeRTOS.h>\r
87 #include "task.h"\r
88 #include "queue.h"\r
89 #include "timers.h"\r
90 #include "semphr.h"\r
91 \r
92 /* Standard demo includes. */\r
93 #include "BlockQ.h"\r
94 #include "integer.h"\r
95 #include "semtest.h"\r
96 #include "PollQ.h"\r
97 #include "GenQTest.h"\r
98 #include "QPeek.h"\r
99 #include "recmutex.h"\r
100 #include "flop.h"\r
101 #include "TimerDemo.h"\r
102 #include "countsem.h"\r
103 \r
104 /* Priorities at which the tasks are created. */\r
105 #define mainCHECK_TASK_PRIORITY         ( configMAX_PRIORITIES - 1 )\r
106 #define mainQUEUE_POLL_PRIORITY         ( tskIDLE_PRIORITY + 1 )\r
107 #define mainSEM_TEST_PRIORITY           ( tskIDLE_PRIORITY + 1 )\r
108 #define mainBLOCK_Q_PRIORITY            ( tskIDLE_PRIORITY + 2 )\r
109 #define mainCREATOR_TASK_PRIORITY   ( tskIDLE_PRIORITY + 3 )\r
110 #define mainFLASH_TASK_PRIORITY         ( tskIDLE_PRIORITY + 1 )\r
111 #define mainuIP_TASK_PRIORITY           ( tskIDLE_PRIORITY + 2 )\r
112 #define mainINTEGER_TASK_PRIORITY   ( tskIDLE_PRIORITY )\r
113 #define mainGEN_QUEUE_TASK_PRIORITY     ( tskIDLE_PRIORITY )\r
114 #define mainFLOP_TASK_PRIORITY          ( tskIDLE_PRIORITY )\r
115 \r
116 #define mainTIMER_TEST_PERIOD                   ( 50 )\r
117 \r
118 /* Task function prototypes. */\r
119 static void prvCheckTask( void *pvParameters );\r
120 \r
121 /* The variable into which error messages are latched. */\r
122 static char *pcStatusMessage = "OK";\r
123 \r
124 /* This semaphore is created purely to test using the vSemaphoreDelete() API\r
125 function.  It has no other purpose. */\r
126 static xSemaphoreHandle xSemaphoreToDelete = NULL;\r
127 \r
128 /*-----------------------------------------------------------*/\r
129 \r
130 int main( void )\r
131 {\r
132         /* Start the check task as described at the top of this file. */\r
133         xTaskCreate( prvCheckTask, ( signed char * ) "Check", configMINIMAL_STACK_SIZE, NULL, mainCHECK_TASK_PRIORITY, NULL );\r
134 \r
135         /* Create the standard demo tasks. */\r
136         vStartBlockingQueueTasks( mainBLOCK_Q_PRIORITY );\r
137         vStartSemaphoreTasks( mainSEM_TEST_PRIORITY );\r
138         vStartPolledQueueTasks( mainQUEUE_POLL_PRIORITY );\r
139         vStartIntegerMathTasks( mainINTEGER_TASK_PRIORITY );\r
140         vStartGenericQueueTasks( mainGEN_QUEUE_TASK_PRIORITY );\r
141         vStartQueuePeekTasks();\r
142         vStartMathTasks( mainFLOP_TASK_PRIORITY );\r
143         vStartRecursiveMutexTasks();\r
144         vStartTimerDemoTask( mainTIMER_TEST_PERIOD );\r
145         vStartCountingSemaphoreTasks();\r
146 \r
147         /* Create the semaphore that will be deleted in the idle task hook.  This\r
148         is done purely to test the use of vSemaphoreDelete(). */\r
149         xSemaphoreToDelete = xSemaphoreCreateMutex();\r
150 \r
151         /* Start the scheduler itself. */\r
152         vTaskStartScheduler();\r
153 \r
154     /* Should never get here unless there was not enough heap space to create \r
155         the idle and other system tasks. */\r
156     return 0;\r
157 }\r
158 /*-----------------------------------------------------------*/\r
159 \r
160 static void prvCheckTask( void *pvParameters )\r
161 {\r
162 portTickType xNextWakeTime;\r
163 const portTickType xCycleFrequency = 1000 / portTICK_RATE_MS;\r
164 \r
165         /* Just to remove compiler warning. */\r
166         ( void ) pvParameters;\r
167 \r
168         /* Initialise xNextWakeTime - this only needs to be done once. */\r
169         xNextWakeTime = xTaskGetTickCount();\r
170 \r
171         for( ;; )\r
172         {\r
173                 /* Place this task in the blocked state until it is time to run again. */\r
174                 vTaskDelayUntil( &xNextWakeTime, xCycleFrequency );\r
175 \r
176                 /* Check the standard demo tasks are running without error. */\r
177                 if( xAreTimerDemoTasksStillRunning( xCycleFrequency ) != pdTRUE )\r
178                 {\r
179                         pcStatusMessage = "Error: TimerDemo";\r
180                 }\r
181             else if( xAreIntegerMathsTaskStillRunning() != pdTRUE )\r
182             {\r
183                         pcStatusMessage = "Error: IntMath";\r
184             }   \r
185                 else if( xAreGenericQueueTasksStillRunning() != pdTRUE )\r
186                 {                       \r
187                         pcStatusMessage = "Error: GenQueue";\r
188                 }\r
189                 else if( xAreQueuePeekTasksStillRunning() != pdTRUE )\r
190                 {\r
191                         pcStatusMessage = "Error: QueuePeek";\r
192                 }\r
193                 else if( xAreBlockingQueuesStillRunning() != pdTRUE )\r
194                 {\r
195                         pcStatusMessage = "Error: BlockQueue";\r
196                 }\r
197             else if( xAreSemaphoreTasksStillRunning() != pdTRUE )\r
198             {\r
199                         pcStatusMessage = "Error: SemTest";\r
200             }\r
201             else if( xArePollingQueuesStillRunning() != pdTRUE )\r
202             {\r
203                         pcStatusMessage = "Error: PollQueue";\r
204             }\r
205                 else if( xAreMathsTaskStillRunning() != pdPASS )\r
206                 {\r
207                         pcStatusMessage = "Error: Flop";\r
208                 }\r
209             else if( xAreRecursiveMutexTasksStillRunning() != pdTRUE )\r
210             {\r
211                         pcStatusMessage = "Error: RecMutex";\r
212                 }\r
213                 else if( xAreCountingSemaphoreTasksStillRunning() != pdTRUE )\r
214                 {\r
215                         pcStatusMessage = "Error: CountSem";\r
216                 }\r
217 \r
218                 /* This is the only task that uses stdout so its ok to call printf() \r
219                 directly. */\r
220                 printf( "%s - %d\r\n", pcStatusMessage, xTaskGetTickCount() );\r
221         }\r
222 }\r
223 /*-----------------------------------------------------------*/\r
224 \r
225 void vApplicationIdleHook( void )\r
226 {\r
227 const unsigned long ulMSToSleep = 5;\r
228 xTaskHandle xIdleTaskHandle, xTimerTaskHandle;\r
229 signed char *pcTaskName;\r
230 \r
231         /* Sleep to reduce CPU load, but don't sleep indefinitely in case there are\r
232         tasks waiting to be terminated by the idle task. */\r
233         Sleep( ulMSToSleep );\r
234 \r
235         /* Demonstrate the use of the xTimerGetTimerTaskHandle() and \r
236         xTaskGetIdleTaskHandle() functions. */\r
237         xIdleTaskHandle = xTaskGetIdleTaskHandle();\r
238         xTimerTaskHandle = xTimerGetTimerTaskHandle();\r
239 \r
240         /* This is the idle hook, so the current task handle should equal the \r
241         returned idle task handle. */\r
242         if( xTaskGetCurrentTaskHandle() != xIdleTaskHandle )\r
243         {\r
244                 pcStatusMessage = "Error:  Returned idle task handle was incorrect";\r
245         }\r
246 \r
247         /* Check the timer task handle was returned correctly. */\r
248         pcTaskName = pcTaskGetTaskName( xTimerTaskHandle );\r
249         if( strcmp( pcTaskName, "Tmr Svc" ) != 0 )\r
250         {\r
251                 pcStatusMessage = "Error:  Returned timer task handle was incorrect";\r
252         }\r
253 \r
254         /* If xSemaphoreToDelete has not already been deleted, then delete it now.\r
255         This is done purely to demonstrate the use of, and test, the \r
256         vSemaphoreDelete() macro.  Care must be taken not to delete a semaphore\r
257         that has tasks blocked on it. */\r
258         if( xSemaphoreToDelete != NULL )\r
259         {\r
260                 vSemaphoreDelete( xSemaphoreToDelete );\r
261                 xSemaphoreToDelete = NULL;\r
262         }\r
263 }\r
264 /*-----------------------------------------------------------*/\r
265 \r
266 void vApplicationMallocFailedHook( void )\r
267 {\r
268         /* Can be implemented if required, but probably not required in this \r
269         environment and running this demo. */\r
270 }\r
271 /*-----------------------------------------------------------*/\r
272 \r
273 void vApplicationStackOverflowHook( void )\r
274 {\r
275         /* Can be implemented if required, but not required in this \r
276         environment and running this demo. */\r
277 }\r
278 /*-----------------------------------------------------------*/\r
279 \r
280 void vApplicationTickHook( void )\r
281 {\r
282         /* Call the periodic timer test, which tests the timer API functions that\r
283         can be called from an ISR. */\r
284         vTimerPeriodicISRTests();\r
285 }\r
286 /*-----------------------------------------------------------*/\r
287 \r
288 void vAssertCalled( void )\r
289 {\r
290         taskDISABLE_INTERRUPTS();\r
291         for( ;; );\r
292 }\r
293 \r