]> git.sur5r.net Git - freertos/blob - FreeRTOS/Demo/WIN32-MingW/main.c
Update version number to 9.0.0rc2.
[freertos] / FreeRTOS / Demo / WIN32-MingW / main.c
1 /*\r
2     FreeRTOS V9.0.0rc2 - Copyright (C) 2016 Real Time Engineers Ltd.\r
3     All rights reserved\r
4 \r
5     VISIT http://www.FreeRTOS.org TO ENSURE YOU ARE USING THE LATEST VERSION.\r
6 \r
7     This file is part of the FreeRTOS distribution.\r
8 \r
9     FreeRTOS is free software; you can redistribute it and/or modify it under\r
10     the terms of the GNU General Public License (version 2) as published by the\r
11     Free Software Foundation >>>> AND MODIFIED BY <<<< the FreeRTOS exception.\r
12 \r
13     ***************************************************************************\r
14     >>!   NOTE: The modification to the GPL is included to allow you to     !<<\r
15     >>!   distribute a combined work that includes FreeRTOS without being   !<<\r
16     >>!   obliged to provide the source code for proprietary components     !<<\r
17     >>!   outside of the FreeRTOS kernel.                                   !<<\r
18     ***************************************************************************\r
19 \r
20     FreeRTOS is distributed in the hope that it will be useful, but WITHOUT ANY\r
21     WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS\r
22     FOR A PARTICULAR PURPOSE.  Full license text is available on the following\r
23     link: http://www.freertos.org/a00114.html\r
24 \r
25     ***************************************************************************\r
26      *                                                                       *\r
27      *    FreeRTOS provides completely free yet professionally developed,    *\r
28      *    robust, strictly quality controlled, supported, and cross          *\r
29      *    platform software that is more than just the market leader, it     *\r
30      *    is the industry's de facto standard.                               *\r
31      *                                                                       *\r
32      *    Help yourself get started quickly while simultaneously helping     *\r
33      *    to support the FreeRTOS project by purchasing a FreeRTOS           *\r
34      *    tutorial book, reference manual, or both:                          *\r
35      *    http://www.FreeRTOS.org/Documentation                              *\r
36      *                                                                       *\r
37     ***************************************************************************\r
38 \r
39     http://www.FreeRTOS.org/FAQHelp.html - Having a problem?  Start by reading\r
40     the FAQ page "My application does not run, what could be wrong?".  Have you\r
41     defined configASSERT()?\r
42 \r
43     http://www.FreeRTOS.org/support - In return for receiving this top quality\r
44     embedded software for free we request you assist our global community by\r
45     participating in the support forum.\r
46 \r
47     http://www.FreeRTOS.org/training - Investing in training allows your team to\r
48     be as productive as possible as early as possible.  Now you can receive\r
49     FreeRTOS training directly from Richard Barry, CEO of Real Time Engineers\r
50     Ltd, and the world's leading authority on the world's leading RTOS.\r
51 \r
52     http://www.FreeRTOS.org/plus - A selection of FreeRTOS ecosystem products,\r
53     including FreeRTOS+Trace - an indispensable productivity tool, a DOS\r
54     compatible FAT file system, and our tiny thread aware UDP/IP stack.\r
55 \r
56     http://www.FreeRTOS.org/labs - Where new FreeRTOS products go to incubate.\r
57     Come and try FreeRTOS+TCP, our new open source TCP/IP stack for FreeRTOS.\r
58 \r
59     http://www.OpenRTOS.com - Real Time Engineers ltd. license FreeRTOS to High\r
60     Integrity Systems ltd. to sell under the OpenRTOS brand.  Low cost OpenRTOS\r
61     licenses offer ticketed support, indemnification and commercial middleware.\r
62 \r
63     http://www.SafeRTOS.com - High Integrity Systems also provide a safety\r
64     engineered and independently SIL3 certified version for use in safety and\r
65     mission critical applications that require provable dependability.\r
66 \r
67     1 tab == 4 spaces!\r
68 */\r
69 \r
70 /******************************************************************************\r
71  * This project provides two demo applications.  A simple blinky style project,\r
72  * and a more comprehensive test and demo application.  The\r
73  * mainCREATE_SIMPLE_BLINKY_DEMO_ONLY setting is used to select between the two.\r
74  * The simply blinky demo is implemented and described in main_blinky.c.  The\r
75  * more comprehensive test and demo application is implemented and described in\r
76  * main_full.c.\r
77  *\r
78  * This file implements the code that is not demo specific, including the\r
79  * hardware setup and FreeRTOS hook functions.\r
80  *\r
81  *******************************************************************************\r
82  * -NOTE- The Win32 port is a simulation (or is that emulation?) only!  Do not\r
83  * expect to get real time behaviour from the Win32 port or this demo\r
84  * application.  It is provided as a convenient development and demonstration\r
85  * test bed only.  This was tested using Windows XP on a dual core laptop.\r
86  *\r
87  * Windows will not be running the FreeRTOS simulator threads continuously, so\r
88  * the timing information in the FreeRTOS+Trace logs have no meaningful units.\r
89  * See the documentation page for the Windows simulator for an explanation of\r
90  * the slow timing:\r
91  * http://www.freertos.org/FreeRTOS-Windows-Simulator-Emulator-for-Visual-Studio-and-Eclipse-MingW.html\r
92  * - READ THE WEB DOCUMENTATION FOR THIS PORT FOR MORE INFORMATION ON USING IT -\r
93  *******************************************************************************\r
94  *\r
95  */\r
96 \r
97 /* Standard includes. */\r
98 #include <stdio.h>\r
99 #include <stdlib.h>\r
100 #include <conio.h>\r
101 \r
102 /* FreeRTOS kernel includes. */\r
103 #include "FreeRTOS.h"\r
104 #include "task.h"\r
105 \r
106 /* This project provides two demo applications.  A simple blinky style project,\r
107 and a more comprehensive test and demo application.  The\r
108 mainCREATE_SIMPLE_BLINKY_DEMO_ONLY setting is used to select between the two.\r
109 The simply blinky demo is implemented and described in main_blinky.c.  The more\r
110 comprehensive test and demo application is implemented and described in\r
111 main_full.c. */\r
112 #define mainCREATE_SIMPLE_BLINKY_DEMO_ONLY      0\r
113 \r
114 /* This demo uses heap_5.c, and these constants define the sizes of the regions\r
115 that make up the total heap.  This is only done to provide an example of heap_5\r
116 being used as this demo could easily create one large heap region instead of\r
117 multiple smaller heap regions - in which case heap_4.c would be the more\r
118 appropriate choice. */\r
119 #define mainREGION_1_SIZE       7001\r
120 #define mainREGION_2_SIZE       18105\r
121 #define mainREGION_3_SIZE       2807\r
122 \r
123 /*\r
124  * main_blinky() is used when mainCREATE_SIMPLE_BLINKY_DEMO_ONLY is set to 1.\r
125  * main_full() is used when mainCREATE_SIMPLE_BLINKY_DEMO_ONLY is set to 0.\r
126  */\r
127 extern void main_blinky( void );\r
128 extern void main_full( void );\r
129 \r
130 /*\r
131  * Some of the RTOS hook (callback) functions only need special processing when\r
132  * the full demo is being used.  The simply blinky demo has no special\r
133  * requirements, so these functions are called from the hook functions defined\r
134  * in this file, but are defined in main_full.c.\r
135  */\r
136 void vFullDemoTickHookFunction( void );\r
137 void vFullDemoIdleFunction( void );\r
138 \r
139 /*\r
140  * This demo uses heap_5.c, so start by defining some heap regions.  This is\r
141  * only done to provide an example as this demo could easily create one large\r
142  * heap region instead of multiple smaller heap regions - in which case heap_4.c\r
143  * would be the more appropriate choice.  No initialisation is required when\r
144  * heap_4.c is used.\r
145  */\r
146 static void  prvInitialiseHeap( void );\r
147 \r
148 /*\r
149  * Prototypes for the standard FreeRTOS callback/hook functions implemented\r
150  * within this file.\r
151  */\r
152 void vApplicationMallocFailedHook( void );\r
153 void vApplicationIdleHook( void );\r
154 void vApplicationStackOverflowHook( TaskHandle_t pxTask, char *pcTaskName );\r
155 void vApplicationTickHook( void );\r
156 \r
157 /*\r
158  * Writes trace data to a disk file when the trace recording is stopped.\r
159  * This function will simply overwrite any trace files that already exist.\r
160  */\r
161 static void prvSaveTraceFile( void );\r
162 \r
163 /* The user trace event posted to the trace recording on each tick interrupt.\r
164 Note tick events will not appear in the trace recording with regular period\r
165 because this project runs in a Windows simulator, and does not therefore\r
166 exhibit deterministic behaviour. */\r
167 traceLabel xTickTraceUserEvent;\r
168 static portBASE_TYPE xTraceRunning = pdTRUE;\r
169 \r
170 /*-----------------------------------------------------------*/\r
171 \r
172 int main( void )\r
173 {\r
174         /* This demo uses heap_5.c, so start by defining some heap regions.  This\r
175         is only done to provide an example as this demo could easily create one\r
176         large heap region instead of multiple smaller heap regions - in which case\r
177         heap_4.c would be the more appropriate choice.  No initialisation is\r
178         required when heap_4.c is used. */\r
179         prvInitialiseHeap();\r
180 \r
181         /* Initialise the trace recorder and create the label used to post user\r
182         events to the trace recording on each tick interrupt. */\r
183         vTraceInitTraceData();\r
184         xTickTraceUserEvent = xTraceOpenLabel( "tick" );\r
185 \r
186         /* Start the trace recording - the recording is written to a file if\r
187         configASSERT() is called. */\r
188         printf( "\r\nTrace started.  Hit a key to dump trace file to disk (does not work from Eclipse console).\r\n" );\r
189         fflush( stdout );\r
190         uiTraceStart();\r
191 \r
192         /* The mainCREATE_SIMPLE_BLINKY_DEMO_ONLY setting is described at the top\r
193         of this file. */\r
194         #if ( mainCREATE_SIMPLE_BLINKY_DEMO_ONLY == 1 )\r
195         {\r
196                 main_blinky();\r
197         }\r
198         #else\r
199         {\r
200                 main_full();\r
201         }\r
202         #endif\r
203 \r
204         return 0;\r
205 }\r
206 /*-----------------------------------------------------------*/\r
207 \r
208 void vApplicationMallocFailedHook( void )\r
209 {\r
210         /* vApplicationMallocFailedHook() will only be called if\r
211         configUSE_MALLOC_FAILED_HOOK is set to 1 in FreeRTOSConfig.h.  It is a hook\r
212         function that will get called if a call to pvPortMalloc() fails.\r
213         pvPortMalloc() is called internally by the kernel whenever a task, queue,\r
214         timer or semaphore is created.  It is also called by various parts of the\r
215         demo application.  If heap_1.c or heap_2.c are used, then the size of the\r
216         heap available to pvPortMalloc() is defined by configTOTAL_HEAP_SIZE in\r
217         FreeRTOSConfig.h, and the xPortGetFreeHeapSize() API function can be used\r
218         to query the size of free heap space that remains (although it does not\r
219         provide information on how the remaining heap might be fragmented). */\r
220         vAssertCalled( __LINE__, __FILE__ );\r
221 }\r
222 /*-----------------------------------------------------------*/\r
223 \r
224 void vApplicationIdleHook( void )\r
225 {\r
226         /* vApplicationIdleHook() will only be called if configUSE_IDLE_HOOK is set\r
227         to 1 in FreeRTOSConfig.h.  It will be called on each iteration of the idle\r
228         task.  It is essential that code added to this hook function never attempts\r
229         to block in any way (for example, call xQueueReceive() with a block time\r
230         specified, or call vTaskDelay()).  If the application makes use of the\r
231         vTaskDelete() API function (as this demo application does) then it is also\r
232         important that vApplicationIdleHook() is permitted to return to its calling\r
233         function, because it is the responsibility of the idle task to clean up\r
234         memory allocated by the kernel to any task that has since been deleted. */\r
235 \r
236         /* Uncomment the following code to allow the trace to be stopped with any \r
237         key press.  The code is commented out by default as the kbhit() function\r
238         interferes with the run time behaviour. */\r
239         /* \r
240                 if( _kbhit() != pdFALSE )\r
241                 {\r
242                         if( xTraceRunning == pdTRUE )\r
243                         {\r
244                                 vTraceStop();\r
245                                 prvSaveTraceFile();\r
246                                 xTraceRunning = pdFALSE;\r
247                         }\r
248                 }\r
249         */\r
250 \r
251         #if ( mainCREATE_SIMPLE_BLINKY_DEMO_ONLY != 1 )\r
252         {\r
253                 /* Call the idle task processing used by the full demo.  The simple\r
254                 blinky demo does not use the idle task hook. */\r
255                 vFullDemoIdleFunction();\r
256         }\r
257         #endif\r
258 }\r
259 /*-----------------------------------------------------------*/\r
260 \r
261 void vApplicationStackOverflowHook( TaskHandle_t pxTask, char *pcTaskName )\r
262 {\r
263         ( void ) pcTaskName;\r
264         ( void ) pxTask;\r
265 \r
266         /* Run time stack overflow checking is performed if\r
267         configCHECK_FOR_STACK_OVERFLOW is defined to 1 or 2.  This hook\r
268         function is called if a stack overflow is detected. */\r
269         vAssertCalled( __LINE__, __FILE__ );\r
270 }\r
271 /*-----------------------------------------------------------*/\r
272 \r
273 void vApplicationTickHook( void )\r
274 {\r
275         /* This function will be called by each tick interrupt if\r
276         configUSE_TICK_HOOK is set to 1 in FreeRTOSConfig.h.  User code can be\r
277         added here, but the tick hook is called from an interrupt context, so\r
278         code must not attempt to block, and only the interrupt safe FreeRTOS API\r
279         functions can be used (those that end in FromISR()). */\r
280 \r
281         #if ( mainCREATE_SIMPLE_BLINKY_DEMO_ONLY != 1 )\r
282         {\r
283                 vFullDemoTickHookFunction();\r
284         }\r
285         #endif /* mainCREATE_SIMPLE_BLINKY_DEMO_ONLY */\r
286 \r
287         /* Write a user event to the trace log.\r
288         Note tick events will not appear in the trace recording with regular period\r
289         because this project runs in a Windows simulator, and does not therefore\r
290         exhibit deterministic behaviour.  Windows will run the simulator in\r
291         bursts. */\r
292         vTraceUserEvent( xTickTraceUserEvent );\r
293 }\r
294 /*-----------------------------------------------------------*/\r
295 \r
296 void vAssertCalled( unsigned long ulLine, const char * const pcFileName )\r
297 {\r
298 static portBASE_TYPE xPrinted = pdFALSE;\r
299 volatile uint32_t ulSetToNonZeroInDebuggerToContinue = 0;\r
300 \r
301         /* Parameters are not used. */\r
302         ( void ) ulLine;\r
303         ( void ) pcFileName;\r
304 \r
305         taskENTER_CRITICAL();\r
306         {\r
307                 /* Stop the trace recording. */\r
308                 if( xPrinted == pdFALSE )\r
309                 {\r
310                         xPrinted = pdTRUE;\r
311                         if( xTraceRunning == pdTRUE )\r
312                         {\r
313                                 vTraceStop();\r
314                                 prvSaveTraceFile();\r
315                         }\r
316                 }\r
317 \r
318                 /* You can step out of this function to debug the assertion by using\r
319                 the debugger to set ulSetToNonZeroInDebuggerToContinue to a non-zero\r
320                 value. */\r
321                 while( ulSetToNonZeroInDebuggerToContinue == 0 )\r
322                 {\r
323                         __asm volatile( "NOP" );\r
324                         __asm volatile( "NOP" );\r
325                 }\r
326         }\r
327         taskEXIT_CRITICAL();\r
328 }\r
329 /*-----------------------------------------------------------*/\r
330 \r
331 static void prvSaveTraceFile( void )\r
332 {\r
333 FILE* pxOutputFile;\r
334 \r
335         pxOutputFile = fopen( "Trace.dump", "wb");\r
336 \r
337         if( pxOutputFile != NULL )\r
338         {\r
339                 fwrite( RecorderDataPtr, sizeof( RecorderDataType ), 1, pxOutputFile );\r
340                 fclose( pxOutputFile );\r
341                 printf( "\r\nTrace output saved to Trace.dump\r\n" );\r
342         }\r
343         else\r
344         {\r
345                 printf( "\r\nFailed to create trace dump file\r\n" );\r
346         }\r
347 }\r
348 /*-----------------------------------------------------------*/\r
349 \r
350 static void  prvInitialiseHeap( void )\r
351 {\r
352 /* This demo uses heap_5.c, so start by defining some heap regions.  This is\r
353 only done to provide an example as this demo could easily create one large heap\r
354 region instead of multiple smaller heap regions - in which case heap_4.c would\r
355 be the more appropriate choice.  No initialisation is required when heap_4.c is\r
356 used.  The xHeapRegions structure requires the regions to be defined in order,\r
357 so this just creates one big array, then populates the structure with offsets\r
358 into the array - with gaps in between and messy alignment just for test\r
359 purposes. */\r
360 static uint8_t ucHeap[ configTOTAL_HEAP_SIZE ];\r
361 volatile uint32_t ulAdditionalOffset = 19; /* Just to prevent 'condition is always true' warnings in configASSERT(). */\r
362 const HeapRegion_t xHeapRegions[] =\r
363 {\r
364         /* Start address with dummy offsets                                             Size */\r
365         { ucHeap + 1,                                                                                   mainREGION_1_SIZE },\r
366         { ucHeap + 15 + mainREGION_1_SIZE,                                              mainREGION_2_SIZE },\r
367         { ucHeap + 19 + mainREGION_1_SIZE + mainREGION_2_SIZE,  mainREGION_3_SIZE },\r
368         { NULL, 0 }\r
369 };\r
370 \r
371         /* Sanity check that the sizes and offsets defined actually fit into the\r
372         array. */\r
373         configASSERT( ( ulAdditionalOffset + mainREGION_1_SIZE + mainREGION_2_SIZE + mainREGION_3_SIZE ) < configTOTAL_HEAP_SIZE );\r
374 \r
375         vPortDefineHeapRegions( xHeapRegions );\r
376 }\r
377 /*-----------------------------------------------------------*/\r
378 \r