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