]> git.sur5r.net Git - freertos/blob - FreeRTOS/Demo/WIN32-MSVC-lwIP/main.c
Update version numbers in preparation for a new release.
[freertos] / FreeRTOS / Demo / WIN32-MSVC-lwIP / main.c
1 /*\r
2  * FreeRTOS Kernel V10.1.0\r
3  * Copyright (C) 2017 Amazon.com, Inc. or its affiliates.  All Rights Reserved.\r
4  *\r
5  * Permission is hereby granted, free of charge, to any person obtaining a copy of\r
6  * this software and associated documentation files (the "Software"), to deal in\r
7  * the Software without restriction, including without limitation the rights to\r
8  * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of\r
9  * the Software, and to permit persons to whom the Software is furnished to do so,\r
10  * subject to the following conditions:\r
11  *\r
12  * The above copyright notice and this permission notice shall be included in all\r
13  * copies or substantial portions of the Software.\r
14  *\r
15  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\r
16  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS\r
17  * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR\r
18  * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER\r
19  * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN\r
20  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.\r
21  *\r
22  * http://www.FreeRTOS.org\r
23  * http://aws.amazon.com/freertos\r
24  *\r
25  * 1 tab == 4 spaces!\r
26  */\r
27 \r
28 /*\r
29  *******************************************************************************\r
30  * -NOTE- The Win32 port is a simulation (or is that emulation?) only!  Do not\r
31  * expect to get real time behaviour from the Win32 port or this demo\r
32  * application.  It is provided as a convenient development and demonstration\r
33  * test bed only.  This was tested using Windows XP on a dual core laptop.\r
34  *\r
35  * - READ THE WEB DOCUMENTATION FOR THIS PORT FOR MORE INFORMATION ON USING IT -\r
36  * - http://www.freertos.org/FreeRTOS-Windows-Simulator-Emulator-for-Visual-Studio-and-Eclipse-MingW.html\r
37  * - Note that the above linked page describes the simulator environment.  It\r
38  * - is not the correct page to view for information on using this lwIP demo.\r
39  *******************************************************************************\r
40  *\r
41  * This project demonstrates use of the lwIP stack.  The lwIP raw API is\r
42  * demonstrated by a simple http server that comes as part of the lwIP\r
43  * distribution - and executes in the tcpip task.  The lwIP sockets API\r
44  * is demonstrated by a simple command line interpreter interface, which\r
45  * executes in its own task.\r
46  *\r
47  * Both the http and command line server can be used to view task stats, and\r
48  * run time stats.  Task stats give a snapshot of the state of each task in\r
49  * the system.  Run time stats show how much processing time has been allocated\r
50  * to each task.  A few of the standard demo tasks are created, just to ensure\r
51  * there is some data to be viewed.\r
52  *\r
53  * Finally, a check timer is created.  The check timer is a software timer that\r
54  * inspects the few standard demo tasks that are created to ensure they are\r
55  * executing as expected.  It maintains a status string that can be viewed on\r
56  * the "task stats" page served by the web server.\r
57  *\r
58  * More information about this demo, including details of how to set up the\r
59  * network interface, and the command line commands that are available, is\r
60  * available on the documentation page for this demo on the\r
61  * http://www.FreeRTOS.org web site.\r
62  *\r
63  */\r
64 \r
65 \r
66 /* Standard includes. */\r
67 #include <stdio.h>\r
68 \r
69 /* Kernel includes. */\r
70 #include <FreeRTOS.h>\r
71 #include "task.h"\r
72 #include "timers.h"\r
73 \r
74 /* Standard demo includes. */\r
75 #include "GenQTest.h"\r
76 \r
77 /* lwIP includes. */\r
78 #include "lwip/tcpip.h"\r
79 #include "lwIP_Apps.h"\r
80 \r
81 /* Utils includes. */\r
82 #include "CommandInterpreter.h"\r
83 \r
84 /* Priorities at which the tasks are created. */\r
85 #define mainGEN_QUEUE_TASK_PRIORITY     ( tskIDLE_PRIORITY )\r
86 \r
87 /* The period at which the check timer will expire, in ms, provided no errors\r
88 have been reported by any of the standard demo tasks.  ms are converted to the\r
89 equivalent in ticks using the portTICK_PERIOD_MS constant. */\r
90 #define mainCHECK_TIMER_PERIOD_MS                       ( 3000UL / portTICK_PERIOD_MS )\r
91 \r
92 /* Check timer callback function. */\r
93 static void prvCheckTimerCallback( TimerHandle_t xTimer );\r
94 \r
95 /* Defined in lwIPApps.c. */\r
96 extern void lwIPAppsInit( void *pvArguments );\r
97 \r
98 /* Callbacks to handle the command line commands defined by the xTaskStats and\r
99 xRunTimeStats command definitions respectively.  These functions are not\r
100 necessarily reentrant!  They must be used from one task only - or at least by\r
101 only one task at a time. */\r
102 static portBASE_TYPE prvTaskStatsCommand( signed char *pcWriteBuffer, size_t xWriteBufferLen, const signed char * pcCommandString );\r
103 static portBASE_TYPE prvRunTimeStatsCommand( signed char *pcWriteBuffer, size_t xWriteBufferLen, const signed char * pcCommandString );\r
104 \r
105 /* The string that latches the current demo status. */\r
106 static char *pcStatusMessage = "All tasks running without error";\r
107 \r
108 /* Variables used in the creation of the run time stats time base.  Run time\r
109 stats record how much time each task spends in the Running state. */\r
110 long long llInitialRunTimeCounterValue = 0LL, llRunTimeStatsDivisor = 0LL;\r
111 \r
112 /* The check timer.  This uses prvCheckTimerCallback() as its callback\r
113 function. */\r
114 static TimerHandle_t xCheckTimer = NULL;\r
115 \r
116 /* Structure that defines the "run-time-stats" command line command. */\r
117 static const xCommandLineInput xRunTimeStats =\r
118 {\r
119         "run-time-stats",\r
120         "run-time-stats: Displays a table showing how much processing time each FreeRTOS task has used\r\n",\r
121         prvRunTimeStatsCommand,\r
122         0\r
123 };\r
124 \r
125 /* Structure that defines the "task-stats" command line command. */\r
126 static const xCommandLineInput xTaskStats =\r
127 {\r
128         "task-stats",\r
129         "task-stats: Displays a table showing the state of each FreeRTOS task\r\n",\r
130         prvTaskStatsCommand,\r
131         0\r
132 };\r
133 \r
134 /*-----------------------------------------------------------*/\r
135 \r
136 int main( void )\r
137 {\r
138 const unsigned long ulLongTime_ms = 1000UL;\r
139 \r
140         /* This call creates the TCP/IP thread. */\r
141         tcpip_init( lwIPAppsInit, NULL );\r
142 \r
143         /* Create and start the check timer, as described at the top of this file. */\r
144         xCheckTimer = xTimerCreate( "CheckTimer",/* A text name, purely to help debugging. */\r
145                                                                 ( mainCHECK_TIMER_PERIOD_MS ),          /* The timer period, in this case 3000ms (3s). */\r
146                                                                 pdTRUE,                                                         /* This is an auto-reload timer, so xAutoReload is set to pdTRUE. */\r
147                                                                 ( void * ) 0,                                           /* The ID is not used, so can be set to anything. */\r
148                                                                 prvCheckTimerCallback                           /* The callback function that inspects the status of all the other tasks. */\r
149                                                           );\r
150 \r
151         /* Sanity check that the timer was created. */\r
152         configASSERT( xCheckTimer );\r
153 \r
154         /* Start the check timer. */\r
155         xTimerStart( xCheckTimer, 0UL );\r
156 \r
157         /* Create a few standard demo tasks, just so there are tasks running to\r
158         view on the web server and via the command line command interpreter. */\r
159         vStartGenericQueueTasks( mainGEN_QUEUE_TASK_PRIORITY );\r
160 \r
161         /* Register two command line commands to show task stats and run time stats\r
162         respectively. */\r
163         xCmdIntRegisterCommand( &xTaskStats );\r
164         xCmdIntRegisterCommand( &xRunTimeStats );\r
165 \r
166         /* Start the scheduler itself. */\r
167         vTaskStartScheduler();\r
168 \r
169         /* This line should never be reached.  If it does execute then there was\r
170         insufficient FreeRTOS heap memory available for the idle and/or timer\r
171         tasks to be created. */\r
172         for( ;; )\r
173         {\r
174                 Sleep( ulLongTime_ms );\r
175         }\r
176 }\r
177 /*-----------------------------------------------------------*/\r
178 \r
179 static void prvCheckTimerCallback( TimerHandle_t xTimer )\r
180 {\r
181         /* The parameter is not used in this case. */\r
182         ( void ) xTimer;\r
183 \r
184         /* Check the standard demo tasks are running without error.   Latch the\r
185         latest reported error in the pcStatusMessage character pointer.  The latched\r
186         string can be viewed using the embedded web server and the command line\r
187         interpreter.  This project is really to demonstrate the lwIP stack - so very\r
188         few tasks are created - and those that are created are created purely so\r
189         there is something to view. */\r
190         if( xAreGenericQueueTasksStillRunning() != pdTRUE )\r
191         {\r
192                 pcStatusMessage = "Error: The GenQueue test reported an error.";\r
193         }\r
194 }\r
195 /*-----------------------------------------------------------*/\r
196 \r
197 void vApplicationIdleHook( void )\r
198 {\r
199 const unsigned long ulMSToSleep = 5;\r
200 \r
201         /* Sleep to reduce CPU load, but don't sleep indefinitely in case there are\r
202         tasks waiting to be terminated by the idle task. */\r
203         Sleep( ulMSToSleep );\r
204 }\r
205 /*-----------------------------------------------------------*/\r
206 \r
207 void vApplicationMallocFailedHook( void )\r
208 {\r
209 const unsigned long ulLongSleep = 1000UL;\r
210 \r
211         /* Can be implemented if required, but probably not required in this\r
212         environment and running this demo. */\r
213         taskDISABLE_INTERRUPTS();\r
214         for( ;; )\r
215         {\r
216                 Sleep( ulLongSleep );\r
217         }\r
218 }\r
219 /*-----------------------------------------------------------*/\r
220 \r
221 void vApplicationStackOverflowHook( void )\r
222 {\r
223 const unsigned long ulLongSleep = 1000UL;\r
224 \r
225         /* Can be implemented if required, but probably not required in this\r
226         environment and running this demo. */\r
227         taskDISABLE_INTERRUPTS();\r
228         for( ;; )\r
229         {\r
230                 Sleep( ulLongSleep );\r
231         }\r
232 }\r
233 /*-----------------------------------------------------------*/\r
234 \r
235 void vAssertCalled( void )\r
236 {\r
237 const unsigned long ulLongSleep = 1000UL;\r
238 \r
239         taskDISABLE_INTERRUPTS();\r
240         for( ;; )\r
241         {\r
242                 Sleep( ulLongSleep );\r
243         }\r
244 }\r
245 /*-----------------------------------------------------------*/\r
246 \r
247 char *pcMainGetTaskStatusMessage( void )\r
248 {\r
249         return pcStatusMessage;\r
250 }\r
251 /*-----------------------------------------------------------*/\r
252 \r
253 void vMainConfigureTimerForRunTimeStats( void )\r
254 {\r
255 LARGE_INTEGER liPerformanceCounterFrequency, liInitialRunTimeValue;\r
256 \r
257         /* Initialise the variables used to create the run time stats time base.\r
258         Run time stats record how much time each task spends in the Running\r
259         state. */\r
260 \r
261         if( QueryPerformanceFrequency( &liPerformanceCounterFrequency ) == 0 )\r
262         {\r
263                 llRunTimeStatsDivisor = 1;\r
264         }\r
265         else\r
266         {\r
267                 /* How many times does the performance counter increment in 10ms? */\r
268                 llRunTimeStatsDivisor = liPerformanceCounterFrequency.QuadPart / 1000LL;\r
269 \r
270                 /* What is the performance counter value now, this will be subtracted\r
271                 from readings taken at run time. */\r
272                 QueryPerformanceCounter( &liInitialRunTimeValue );\r
273                 llInitialRunTimeCounterValue = liInitialRunTimeValue.QuadPart;\r
274         }\r
275 }\r
276 /*-----------------------------------------------------------*/\r
277 \r
278 unsigned long ulMainGetRunTimeCounterValue( void )\r
279 {\r
280 LARGE_INTEGER liCurrentCount;\r
281 unsigned long ulReturn;\r
282 \r
283         /* What is the performance counter value now? */\r
284         QueryPerformanceCounter( &liCurrentCount );\r
285 \r
286         /* Subtract the performance counter value reading taken when the\r
287         application started to get a count from that reference point, then\r
288         scale to a 32 bit number. */\r
289         ulReturn = ( unsigned long ) ( ( liCurrentCount.QuadPart - llInitialRunTimeCounterValue ) / llRunTimeStatsDivisor );\r
290 \r
291         return ulReturn;\r
292 }\r
293 /*-----------------------------------------------------------*/\r
294 \r
295 static portBASE_TYPE prvTaskStatsCommand( signed char *pcWriteBuffer, size_t xWriteBufferLen, const signed char * pcCommandString )\r
296 {\r
297 const char *const pcHeader = "Task          State  Priority  Stack      #\r\n************************************************\r\n";\r
298 \r
299         configASSERT( pcWriteBuffer );\r
300 \r
301         /* This function assumes the buffer length is adequate and does not look\r
302         for parameters. */\r
303         ( void ) xWriteBufferLen;\r
304         ( void ) pcCommandString;\r
305 \r
306         /* Generate a table of task stats. */\r
307         strcpy( pcWriteBuffer, pcHeader );\r
308         vTaskList( pcWriteBuffer + strlen( pcHeader ) );\r
309 \r
310         /* There is no more data to return after this single string, so return\r
311         pdFALSE. */\r
312         return pdFALSE;\r
313 }\r
314 /*-----------------------------------------------------------*/\r
315 \r
316 static portBASE_TYPE prvRunTimeStatsCommand( signed char *pcWriteBuffer, size_t xWriteBufferLen, const signed char * pcCommandString )\r
317 {\r
318 const char * const pcHeader = "Task            Abs Time      % Time\r\n****************************************\r\n";\r
319 \r
320         configASSERT( pcWriteBuffer );\r
321 \r
322         /* This function assumes the buffer length is adequate and does not look\r
323         for parameters. */\r
324         ( void ) xWriteBufferLen;\r
325         ( void ) pcCommandString;\r
326 \r
327         /* Generate a table of task stats. */\r
328         strcpy( pcWriteBuffer, pcHeader );\r
329         vTaskGetRunTimeStats( ( char * ) pcWriteBuffer + strlen( pcHeader ) );\r
330 \r
331         /* There is no more data to return after this single string, so return\r
332         pdFALSE. */\r
333         return pdFALSE;\r
334 }\r
335 \r
336 \r