]> git.sur5r.net Git - freertos/blob - Demo/WIN32-MSVC-lwIP/main.c
First MicroBlaze hardware build with full Ethernet.
[freertos] / Demo / WIN32-MSVC-lwIP / 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  * - http://www.freertos.org/FreeRTOS-Windows-Simulator-Emulator-for-Visual-Studio-and-Eclipse-MingW.html\r
63  * - Note that the above linked page describes the simulator environment.  It\r
64  * - is not the correct page to view for information on using this lwIP demo.\r
65  *******************************************************************************\r
66  *\r
67  * This project demonstrates use of the lwIP stack.  The lwIP raw API is \r
68  * demonstrated by a simple http server that comes as part of the lwIP \r
69  * distribution - and executes in the tcpip task.  The lwIP sockets API\r
70  * is demonstrated by a simple command line interpreter interface, which\r
71  * executes in its own task.\r
72  *\r
73  * Both the http and command line server can be used to view task stats, and\r
74  * run time stats.  Task stats give a snapshot of the state of each task in\r
75  * the system.  Run time stats show how much processing time has been allocated\r
76  * to each task.  A few of the standard demo tasks are created, just to ensure\r
77  * there is some data to be viewed.\r
78  *\r
79  * Finally, a check timer is created.  The check timer is a software timer that\r
80  * inspects the few standard demo tasks that are created to ensure they are\r
81  * executing as expected.  It maintains a status string that can be viewed on\r
82  * the "task stats" page served by the web server.\r
83  *\r
84  * More information about this demo, including details of how to set up the \r
85  * network interface, and the command line commands that are available, is\r
86  * available on the documentation page for this demo on the \r
87  * http://www.FreeRTOS.org web site.\r
88  *\r
89  */\r
90 \r
91 \r
92 /* Standard includes. */\r
93 #include <stdio.h>\r
94 \r
95 /* Kernel includes. */\r
96 #include <FreeRTOS.h>\r
97 #include "task.h"\r
98 #include "timers.h"\r
99 \r
100 /* Standard demo includes. */\r
101 #include "GenQTest.h"\r
102 \r
103 /* lwIP includes. */\r
104 #include "lwip/tcpip.h"\r
105 #include "lwIP_Apps.h"\r
106 \r
107 /* Utils includes. */\r
108 #include "CommandInterpreter.h"\r
109 \r
110 /* Priorities at which the tasks are created. */\r
111 #define mainGEN_QUEUE_TASK_PRIORITY     ( tskIDLE_PRIORITY )\r
112 \r
113 /* The period at which the check timer will expire, in ms, provided no errors\r
114 have been reported by any of the standard demo tasks.  ms are converted to the\r
115 equivalent in ticks using the portTICK_RATE_MS constant. */\r
116 #define mainCHECK_TIMER_PERIOD_MS                       ( 3000UL / portTICK_RATE_MS )\r
117 \r
118 /* Check timer callback function. */\r
119 static void prvCheckTimerCallback( xTimerHandle xTimer );\r
120 \r
121 /* Defined in lwIPApps.c. */\r
122 extern void lwIPAppsInit( void *pvArguments );\r
123 \r
124 /* Callbacks to handle the command line commands defined by the xTaskStats and\r
125 xRunTimeStats command definitions respectively.  These functions are not \r
126 necessarily reentrant!  They must be used from one task only - or at least by \r
127 only one task at a time. */\r
128 static portBASE_TYPE prvTaskStatsCommand( signed char *pcWriteBuffer, size_t xWriteBufferLen );\r
129 static portBASE_TYPE prvRunTimeStatsCommand( signed char *pcWriteBuffer, size_t xWriteBufferLen );\r
130 \r
131 /* The string that latches the current demo status. */\r
132 static char *pcStatusMessage = "All tasks running without error";\r
133 \r
134 /* Variables used in the creation of the run time stats time base.  Run time \r
135 stats record how much time each task spends in the Running state. */\r
136 long long llInitialRunTimeCounterValue = 0LL, llRunTimeStatsDivisor = 0LL;\r
137 \r
138 /* The check timer.  This uses prvCheckTimerCallback() as its callback\r
139 function. */\r
140 static xTimerHandle xCheckTimer = NULL;\r
141 \r
142 /* Structure that defines the "run-time-stats" command line command. */\r
143 static const xCommandLineInput xRunTimeStats =\r
144 {\r
145         "run-time-stats",\r
146         "run-time-stats: Displays a table showing how much processing time each FreeRTOS task has used\r\n",\r
147         prvRunTimeStatsCommand,\r
148 };\r
149 \r
150 /* Structure that defines the "task-stats" command line command. */\r
151 static const xCommandLineInput xTaskStats =\r
152 {\r
153         "task-stats",\r
154         "task-stats: Displays a table showing the state of each FreeRTOS task\r\n",\r
155         prvTaskStatsCommand,\r
156 };\r
157 \r
158 /*-----------------------------------------------------------*/\r
159 \r
160 int main( void )\r
161 {\r
162 const unsigned long ulLongTime_ms = 1000UL;\r
163 \r
164         /* This call creates the TCP/IP thread. */\r
165         tcpip_init( lwIPAppsInit, NULL );\r
166 \r
167         /* Create and start the check timer, as described at the top of this file. */\r
168         xCheckTimer = xTimerCreate( ( const signed char * ) "CheckTimer",/* A text name, purely to help debugging. */\r
169                                                                 ( mainCHECK_TIMER_PERIOD_MS ),          /* The timer period, in this case 3000ms (3s). */\r
170                                                                 pdTRUE,                                                         /* This is an auto-reload timer, so xAutoReload is set to pdTRUE. */\r
171                                                                 ( void * ) 0,                                           /* The ID is not used, so can be set to anything. */\r
172                                                                 prvCheckTimerCallback                           /* The callback function that inspects the status of all the other tasks. */\r
173                                                           );\r
174 \r
175         /* Sanity check that the timer was created. */\r
176         configASSERT( xCheckTimer );\r
177 \r
178         /* Start the check timer. */\r
179         xTimerStart( xCheckTimer, 0UL );\r
180 \r
181         /* Create a few standard demo tasks, just so there are tasks running to\r
182         view on the web server and via the command line command interpreter. */\r
183         vStartGenericQueueTasks( mainGEN_QUEUE_TASK_PRIORITY );\r
184 \r
185         /* Register two command line commands to show task stats and run time stats\r
186         respectively. */\r
187         xCmdIntRegisterCommand( &xTaskStats );\r
188         xCmdIntRegisterCommand( &xRunTimeStats );\r
189 \r
190         /* Start the scheduler itself. */\r
191         vTaskStartScheduler();\r
192 \r
193         /* This line should never be reached.  If it does execute then there was\r
194         insufficient FreeRTOS heap memory available for the idle and/or timer\r
195         tasks to be created. */\r
196         for( ;; )\r
197         {\r
198                 Sleep( ulLongTime_ms );\r
199         }\r
200 }\r
201 /*-----------------------------------------------------------*/\r
202 \r
203 static void prvCheckTimerCallback( xTimerHandle xTimer )\r
204 {\r
205         /* The parameter is not used in this case. */\r
206         ( void ) xTimer;\r
207 \r
208         /* Check the standard demo tasks are running without error.   Latch the\r
209         latest reported error in the pcStatusMessage character pointer.  The latched\r
210         string can be viewed using the embedded web server and the command line\r
211         interpreter.  This project is really to demonstrate the lwIP stack - so very\r
212         few tasks are created - and those that are created are created purely so\r
213         there is something to view. */\r
214         if( xAreGenericQueueTasksStillRunning() != pdTRUE )\r
215         {\r
216                 pcStatusMessage = "Error: The GenQueue test reported an error.";\r
217         }\r
218 }\r
219 /*-----------------------------------------------------------*/\r
220 \r
221 void vApplicationIdleHook( void )\r
222 {\r
223 const unsigned long ulMSToSleep = 5;\r
224 \r
225         /* Sleep to reduce CPU load, but don't sleep indefinitely in case there are\r
226         tasks waiting to be terminated by the idle task. */\r
227         Sleep( ulMSToSleep );\r
228 }\r
229 /*-----------------------------------------------------------*/\r
230 \r
231 void vApplicationMallocFailedHook( void )\r
232 {\r
233 const unsigned long ulLongSleep = 1000UL;\r
234 \r
235         /* Can be implemented if required, but probably not required in this \r
236         environment and running this demo. */\r
237         taskDISABLE_INTERRUPTS();\r
238         for( ;; )\r
239         {\r
240                 Sleep( ulLongSleep );\r
241         }\r
242 }\r
243 /*-----------------------------------------------------------*/\r
244 \r
245 void vApplicationStackOverflowHook( void )\r
246 {\r
247 const unsigned long ulLongSleep = 1000UL;\r
248 \r
249         /* Can be implemented if required, but probably not required in this \r
250         environment and running this demo. */\r
251         taskDISABLE_INTERRUPTS();\r
252         for( ;; )\r
253         {\r
254                 Sleep( ulLongSleep );\r
255         }\r
256 }\r
257 /*-----------------------------------------------------------*/\r
258 \r
259 void vAssertCalled( void )\r
260 {\r
261 const unsigned long ulLongSleep = 1000UL;\r
262 \r
263         taskDISABLE_INTERRUPTS();\r
264         for( ;; )\r
265         {\r
266                 Sleep( ulLongSleep );\r
267         }\r
268 }\r
269 /*-----------------------------------------------------------*/\r
270 \r
271 char *pcMainGetTaskStatusMessage( void )\r
272 {\r
273         return pcStatusMessage;\r
274 }\r
275 /*-----------------------------------------------------------*/\r
276 \r
277 void vMainConfigureTimerForRunTimeStats( void )\r
278 {\r
279 LARGE_INTEGER liPerformanceCounterFrequency, liInitialRunTimeValue;\r
280 \r
281         /* Initialise the variables used to create the run time stats time base.\r
282         Run time stats record how much time each task spends in the Running \r
283         state. */\r
284 \r
285         if( QueryPerformanceFrequency( &liPerformanceCounterFrequency ) == 0 )\r
286         {\r
287                 llRunTimeStatsDivisor = 1;\r
288         }\r
289         else\r
290         {\r
291                 /* How many times does the performance counter increment in 10ms? */\r
292                 llRunTimeStatsDivisor = liPerformanceCounterFrequency.QuadPart / 1000LL;\r
293 \r
294                 /* What is the performance counter value now, this will be subtracted\r
295                 from readings taken at run time. */\r
296                 QueryPerformanceCounter( &liInitialRunTimeValue );\r
297                 llInitialRunTimeCounterValue = liInitialRunTimeValue.QuadPart;\r
298         }\r
299 }\r
300 /*-----------------------------------------------------------*/\r
301 \r
302 unsigned long ulMainGetRunTimeCounterValue( void )\r
303 {\r
304 LARGE_INTEGER liCurrentCount;\r
305 unsigned long ulReturn;\r
306 \r
307         /* What is the performance counter value now? */\r
308         QueryPerformanceCounter( &liCurrentCount );\r
309 \r
310         /* Subtract the performance counter value reading taken when the \r
311         application started to get a count from that reference point, then\r
312         scale to a 32 bit number. */\r
313         ulReturn = ( unsigned long ) ( ( liCurrentCount.QuadPart - llInitialRunTimeCounterValue ) / llRunTimeStatsDivisor );\r
314 \r
315         return ulReturn;\r
316 }\r
317 /*-----------------------------------------------------------*/\r
318 \r
319 static portBASE_TYPE prvTaskStatsCommand( signed char *pcWriteBuffer, size_t xWriteBufferLen )\r
320 {\r
321 const char *const pcHeader = "Task          State  Priority  Stack      #\r\n************************************************\r\n";\r
322 \r
323         configASSERT( pcWriteBuffer );\r
324 \r
325         /* This function assumes the buffer length is adequate. */\r
326         ( void ) xWriteBufferLen;\r
327 \r
328         /* Generate a table of task stats. */\r
329         strcpy( pcWriteBuffer, pcHeader );\r
330         vTaskList( pcWriteBuffer + strlen( pcHeader ) );\r
331 \r
332         /* There is no more data to return after this single string, so return \r
333         pdFALSE. */\r
334         return pdFALSE;\r
335 }\r
336 /*-----------------------------------------------------------*/\r
337 \r
338 static portBASE_TYPE prvRunTimeStatsCommand( signed char *pcWriteBuffer, size_t xWriteBufferLen )\r
339 {\r
340 const char * const pcHeader = "Task            Abs Time      % Time\r\n****************************************\r\n";\r
341 \r
342         configASSERT( pcWriteBuffer );\r
343 \r
344         /* This function assumes the buffer length is adequate. */\r
345         ( void ) xWriteBufferLen;\r
346 \r
347         /* Generate a table of task stats. */\r
348         strcpy( pcWriteBuffer, pcHeader );\r
349         vTaskGetRunTimeStats( pcWriteBuffer + strlen( pcHeader ) );\r
350 \r
351         /* There is no more data to return after this single string, so return \r
352         pdFALSE. */\r
353         return pdFALSE;\r
354 }\r
355 \r
356 \r