]> git.sur5r.net Git - freertos/blob - FreeRTOS/Demo/WIN32-MingW/main.c
Update license information text files for the CLI, TCP and UDP products to be correct...
[freertos] / FreeRTOS / Demo / WIN32-MingW / main.c
1 /*\r
2  * FreeRTOS Kernel V10.0.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. If you wish to use our Amazon\r
14  * FreeRTOS name, please do so in a fair use way that does not cause confusion.\r
15  *\r
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\r
17  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS\r
18  * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR\r
19  * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER\r
20  * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN\r
21  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.\r
22  *\r
23  * http://www.FreeRTOS.org\r
24  * http://aws.amazon.com/freertos\r
25  *\r
26  * 1 tab == 4 spaces!\r
27  */\r
28 \r
29 /******************************************************************************\r
30  * This project provides two demo applications.  A simple blinky style project,\r
31  * and a more comprehensive test and demo application.  The\r
32  * mainCREATE_SIMPLE_BLINKY_DEMO_ONLY setting is used to select between the two.\r
33  * The simply blinky demo is implemented and described in main_blinky.c.  The\r
34  * more comprehensive test and demo application is implemented and described in\r
35  * main_full.c.\r
36  *\r
37  * This file implements the code that is not demo specific, including the\r
38  * hardware setup and FreeRTOS hook functions.\r
39  *\r
40  *******************************************************************************\r
41  * NOTE: Windows will not be running the FreeRTOS demo threads continuously, so\r
42  * do not expect to get real time behaviour from the FreeRTOS Windows port, or\r
43  * this demo application.  Also, the timing information in the FreeRTOS+Trace\r
44  * logs have no meaningful units.  See the documentation page for the Windows\r
45  * port for further information:\r
46  * http://www.freertos.org/FreeRTOS-Windows-Simulator-Emulator-for-Visual-Studio-and-Eclipse-MingW.html\r
47  *\r
48 \r
49  *\r
50  *******************************************************************************\r
51  */\r
52 \r
53 /* Standard includes. */\r
54 #include <stdio.h>\r
55 #include <stdlib.h>\r
56 #include <conio.h>\r
57 \r
58 /* FreeRTOS kernel includes. */\r
59 #include "FreeRTOS.h"\r
60 #include "task.h"\r
61 \r
62 /* This project provides two demo applications.  A simple blinky style demo\r
63 application, and a more comprehensive test and demo application.  The\r
64 mainCREATE_SIMPLE_BLINKY_DEMO_ONLY setting is used to select between the two.\r
65 \r
66 If mainCREATE_SIMPLE_BLINKY_DEMO_ONLY is 1 then the blinky demo will be built.\r
67 The blinky demo is implemented and described in main_blinky.c.\r
68 \r
69 If mainCREATE_SIMPLE_BLINKY_DEMO_ONLY is not 1 then the comprehensive test and\r
70 demo application will be built.  The comprehensive test and demo application is\r
71 implemented and described in main_full.c. */\r
72 #define mainCREATE_SIMPLE_BLINKY_DEMO_ONLY      0\r
73 \r
74 /* This demo uses heap_5.c, and these constants define the sizes of the regions\r
75 that make up the total heap.  heap_5 is only used for test and example purposes\r
76 as this demo could easily create one large heap region instead of multiple\r
77 smaller heap regions - in which case heap_4.c would be the more appropriate\r
78 choice.  See http://www.freertos.org/a00111.html for an explanation. */\r
79 #define mainREGION_1_SIZE       7201\r
80 #define mainREGION_2_SIZE       29905\r
81 #define mainREGION_3_SIZE       6407\r
82 \r
83 /*-----------------------------------------------------------*/\r
84 \r
85 /*\r
86  * main_blinky() is used when mainCREATE_SIMPLE_BLINKY_DEMO_ONLY is set to 1.\r
87  * main_full() is used when mainCREATE_SIMPLE_BLINKY_DEMO_ONLY is set to 0.\r
88  */\r
89 extern void main_blinky( void );\r
90 extern void main_full( void );\r
91 \r
92 /*\r
93  * Only the comprehensive demo uses application hook (callback) functions.  See\r
94  * http://www.freertos.org/a00016.html for more information.\r
95  */\r
96 void vFullDemoTickHookFunction( void );\r
97 void vFullDemoIdleFunction( void );\r
98 \r
99 /*\r
100  * This demo uses heap_5.c, so start by defining some heap regions.  It is not\r
101  * necessary for this demo to use heap_5, as it could define one large heap\r
102  * region.  Heap_5 is only used for test and example purposes.  See\r
103  * http://www.freertos.org/a00111.html for an explanation.\r
104  */\r
105 static void  prvInitialiseHeap( void );\r
106 \r
107 /*\r
108  * Prototypes for the standard FreeRTOS application hook (callback) functions\r
109  * implemented within this file.  See http://www.freertos.org/a00016.html .\r
110  */\r
111 void vApplicationMallocFailedHook( void );\r
112 void vApplicationIdleHook( void );\r
113 void vApplicationStackOverflowHook( TaskHandle_t pxTask, char *pcTaskName );\r
114 void vApplicationTickHook( void );\r
115 \r
116 /*\r
117  * Writes trace data to a disk file when the trace recording is stopped.\r
118  * This function will simply overwrite any trace files that already exist.\r
119  */\r
120 static void prvSaveTraceFile( void );\r
121 \r
122 /* Notes if the trace is running or not. */\r
123 static BaseType_t xTraceRunning = pdTRUE;\r
124 \r
125 /*-----------------------------------------------------------*/\r
126 \r
127 int main( void )\r
128 {\r
129         /* This demo uses heap_5.c, so start by defining some heap regions.  heap_5\r
130         is only used for test and example reasons.  Heap_4 is more appropriate.  See\r
131         http://www.freertos.org/a00111.html for an explanation. */\r
132         prvInitialiseHeap();\r
133 \r
134         /* Initialise the trace recorder.  Use of the trace recorder is optional.\r
135         See http://www.FreeRTOS.org/trace for more information. */\r
136         vTraceEnable( TRC_START );\r
137 \r
138         /* The mainCREATE_SIMPLE_BLINKY_DEMO_ONLY setting is described at the top\r
139         of this file. */\r
140         #if ( mainCREATE_SIMPLE_BLINKY_DEMO_ONLY == 1 )\r
141         {\r
142                 main_blinky();\r
143         }\r
144         #else\r
145         {\r
146                 /* Start the trace recording - the recording is written to a file if\r
147                 configASSERT() is called. */\r
148                 printf( "\r\nTrace started.\r\nThe trace will be dumped to disk if a call to configASSERT() fails.\r\n" );\r
149                 printf( "Uncomment the call to kbhit() in this file to also dump trace with a key press.\r\n" );\r
150                 uiTraceStart();\r
151 \r
152                 main_full();\r
153         }\r
154         #endif\r
155 \r
156         return 0;\r
157 }\r
158 /*-----------------------------------------------------------*/\r
159 \r
160 void vApplicationMallocFailedHook( void )\r
161 {\r
162         /* vApplicationMallocFailedHook() will only be called if\r
163         configUSE_MALLOC_FAILED_HOOK is set to 1 in FreeRTOSConfig.h.  It is a hook\r
164         function that will get called if a call to pvPortMalloc() fails.\r
165         pvPortMalloc() is called internally by the kernel whenever a task, queue,\r
166         timer or semaphore is created.  It is also called by various parts of the\r
167         demo application.  If heap_1.c, heap_2.c or heap_4.c is being used, then the\r
168         size of the     heap available to pvPortMalloc() is defined by\r
169         configTOTAL_HEAP_SIZE in FreeRTOSConfig.h, and the xPortGetFreeHeapSize()\r
170         API function can be used to query the size of free heap space that remains\r
171         (although it does not provide information on how the remaining heap might be\r
172         fragmented).  See http://www.freertos.org/a00111.html for more\r
173         information. */\r
174         vAssertCalled( __LINE__, __FILE__ );\r
175 }\r
176 /*-----------------------------------------------------------*/\r
177 \r
178 void vApplicationIdleHook( void )\r
179 {\r
180         /* vApplicationIdleHook() will only be called if configUSE_IDLE_HOOK is set\r
181         to 1 in FreeRTOSConfig.h.  It will be called on each iteration of the idle\r
182         task.  It is essential that code added to this hook function never attempts\r
183         to block in any way (for example, call xQueueReceive() with a block time\r
184         specified, or call vTaskDelay()).  If application tasks make use of the\r
185         vTaskDelete() API function to delete themselves then it is also important\r
186         that vApplicationIdleHook() is permitted to return to its calling function,\r
187         because it is the responsibility of the idle task to clean up memory\r
188         allocated by the kernel to any task that has since deleted itself. */\r
189 \r
190         /* Uncomment the following code to allow the trace to be stopped with any\r
191         key press.  The code is commented out by default as the kbhit() function\r
192         interferes with the run time behaviour. */\r
193         /*\r
194                 if( _kbhit() != pdFALSE )\r
195                 {\r
196                         if( xTraceRunning == pdTRUE )\r
197                         {\r
198                                 vTraceStop();\r
199                                 prvSaveTraceFile();\r
200                                 xTraceRunning = pdFALSE;\r
201                         }\r
202                 }\r
203         */\r
204 \r
205         #if ( mainCREATE_SIMPLE_BLINKY_DEMO_ONLY != 1 )\r
206         {\r
207                 /* Call the idle task processing used by the full demo.  The simple\r
208                 blinky demo does not use the idle task hook. */\r
209                 vFullDemoIdleFunction();\r
210         }\r
211         #endif\r
212 }\r
213 /*-----------------------------------------------------------*/\r
214 \r
215 void vApplicationStackOverflowHook( TaskHandle_t pxTask, char *pcTaskName )\r
216 {\r
217         ( void ) pcTaskName;\r
218         ( void ) pxTask;\r
219 \r
220         /* Run time stack overflow checking is performed if\r
221         configCHECK_FOR_STACK_OVERFLOW is defined to 1 or 2.  This hook\r
222         function is called if a stack overflow is detected.  This function is\r
223         provided as an example only as stack overflow checking does not function\r
224         when running the FreeRTOS Windows port. */\r
225         vAssertCalled( __LINE__, __FILE__ );\r
226 }\r
227 /*-----------------------------------------------------------*/\r
228 \r
229 void vApplicationTickHook( void )\r
230 {\r
231         /* This function will be called by each tick interrupt if\r
232         configUSE_TICK_HOOK is set to 1 in FreeRTOSConfig.h.  User code can be\r
233         added here, but the tick hook is called from an interrupt context, so\r
234         code must not attempt to block, and only the interrupt safe FreeRTOS API\r
235         functions can be used (those that end in FromISR()). */\r
236 \r
237         #if ( mainCREATE_SIMPLE_BLINKY_DEMO_ONLY != 1 )\r
238         {\r
239                 vFullDemoTickHookFunction();\r
240         }\r
241         #endif /* mainCREATE_SIMPLE_BLINKY_DEMO_ONLY */\r
242 }\r
243 /*-----------------------------------------------------------*/\r
244 \r
245 void vApplicationDaemonTaskStartupHook( void )\r
246 {\r
247         /* This function will be called once only, when the daemon task starts to\r
248         execute (sometimes called the timer task).  This is useful if the\r
249         application includes initialisation code that would benefit from executing\r
250         after the scheduler has been started. */\r
251 }\r
252 /*-----------------------------------------------------------*/\r
253 \r
254 void vAssertCalled( unsigned long ulLine, const char * const pcFileName )\r
255 {\r
256 static BaseType_t xPrinted = pdFALSE;\r
257 volatile uint32_t ulSetToNonZeroInDebuggerToContinue = 0;\r
258 \r
259         /* Called if an assertion passed to configASSERT() fails.  See\r
260         http://www.freertos.org/a00110.html#configASSERT for more information. */\r
261 \r
262         /* Parameters are not used. */\r
263         ( void ) ulLine;\r
264         ( void ) pcFileName;\r
265 \r
266 \r
267         taskENTER_CRITICAL();\r
268         {\r
269                 /* Stop the trace recording. */\r
270                 if( xPrinted == pdFALSE )\r
271                 {\r
272                         xPrinted = pdTRUE;\r
273                         if( xTraceRunning == pdTRUE )\r
274                         {\r
275                                 vTraceStop();\r
276                                 prvSaveTraceFile();\r
277                         }\r
278                 }\r
279 \r
280                 /* You can step out of this function to debug the assertion by using\r
281                 the debugger to set ulSetToNonZeroInDebuggerToContinue to a non-zero\r
282                 value. */\r
283                 while( ulSetToNonZeroInDebuggerToContinue == 0 )\r
284                 {\r
285                         __asm volatile( "NOP" );\r
286                         __asm volatile( "NOP" );\r
287                 }\r
288         }\r
289         taskEXIT_CRITICAL();\r
290 }\r
291 /*-----------------------------------------------------------*/\r
292 \r
293 static void prvSaveTraceFile( void )\r
294 {\r
295 FILE* pxOutputFile;\r
296 \r
297         pxOutputFile = fopen( "Trace.dump", "wb");\r
298 \r
299         if( pxOutputFile != NULL )\r
300         {\r
301                 fwrite( RecorderDataPtr, sizeof( RecorderDataType ), 1, pxOutputFile );\r
302                 fclose( pxOutputFile );\r
303                 printf( "\r\nTrace output saved to Trace.dump\r\n" );\r
304         }\r
305         else\r
306         {\r
307                 printf( "\r\nFailed to create trace dump file\r\n" );\r
308         }\r
309 }\r
310 /*-----------------------------------------------------------*/\r
311 \r
312 static void  prvInitialiseHeap( void )\r
313 {\r
314 /* The Windows demo could create one large heap region, in which case it would\r
315 be appropriate to use heap_4.  However, purely for demonstration purposes,\r
316 heap_5 is used instead, so start by defining some heap regions.  No\r
317 initialisation is required when any other heap implementation is used.  See\r
318 http://www.freertos.org/a00111.html for more information.\r
319 \r
320 The xHeapRegions structure requires the regions to be defined in start address\r
321 order, so this just creates one big array, then populates the structure with\r
322 offsets into the array - with gaps in between and messy alignment just for test\r
323 purposes. */\r
324 static uint8_t ucHeap[ configTOTAL_HEAP_SIZE ];\r
325 volatile uint32_t ulAdditionalOffset = 19; /* Just to prevent 'condition is always true' warnings in configASSERT(). */\r
326 const HeapRegion_t xHeapRegions[] =\r
327 {\r
328         /* Start address with dummy offsets                                             Size */\r
329         { ucHeap + 1,                                                                                   mainREGION_1_SIZE },\r
330         { ucHeap + 15 + mainREGION_1_SIZE,                                              mainREGION_2_SIZE },\r
331         { ucHeap + 19 + mainREGION_1_SIZE + mainREGION_2_SIZE,  mainREGION_3_SIZE },\r
332         { NULL, 0 }\r
333 };\r
334 \r
335         /* Sanity check that the sizes and offsets defined actually fit into the\r
336         array. */\r
337         configASSERT( ( ulAdditionalOffset + mainREGION_1_SIZE + mainREGION_2_SIZE + mainREGION_3_SIZE ) < configTOTAL_HEAP_SIZE );\r
338 \r
339         /* Prevent compiler warnings when configASSERT() is not defined. */\r
340         ( void ) ulAdditionalOffset;\r
341 \r
342         vPortDefineHeapRegions( xHeapRegions );\r
343 }\r
344 /*-----------------------------------------------------------*/\r