]> git.sur5r.net Git - freertos/blob - FreeRTOS/Demo/WIN32-MSVC/main.c
fea21698ed92ea9b63b6eb93208d02bfc2ecc167
[freertos] / FreeRTOS / Demo / WIN32-MSVC / main.c
1 /*\r
2     FreeRTOS V8.2.0 - Copyright (C) 2015 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       4001\r
120 #define mainREGION_2_SIZE       18105\r
121 #define mainREGION_3_SIZE       1807\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.\r\n" );\r
189         uiTraceStart();\r
190 \r
191         /* The mainCREATE_SIMPLE_BLINKY_DEMO_ONLY setting is described at the top\r
192         of this file. */\r
193         #if ( mainCREATE_SIMPLE_BLINKY_DEMO_ONLY == 1 )\r
194         {\r
195                 main_blinky();\r
196         }\r
197         #else\r
198         {\r
199                 main_full();\r
200         }\r
201         #endif\r
202 \r
203         return 0;\r
204 }\r
205 /*-----------------------------------------------------------*/\r
206 \r
207 void vApplicationMallocFailedHook( void )\r
208 {\r
209         /* vApplicationMallocFailedHook() will only be called if\r
210         configUSE_MALLOC_FAILED_HOOK is set to 1 in FreeRTOSConfig.h.  It is a hook\r
211         function that will get called if a call to pvPortMalloc() fails.\r
212         pvPortMalloc() is called internally by the kernel whenever a task, queue,\r
213         timer or semaphore is created.  It is also called by various parts of the\r
214         demo application.  If heap_1.c or heap_2.c are used, then the size of the\r
215         heap available to pvPortMalloc() is defined by configTOTAL_HEAP_SIZE in\r
216         FreeRTOSConfig.h, and the xPortGetFreeHeapSize() API function can be used\r
217         to query the size of free heap space that remains (although it does not\r
218         provide information on how the remaining heap might be fragmented). */\r
219         vAssertCalled( __LINE__, __FILE__ );\r
220 }\r
221 /*-----------------------------------------------------------*/\r
222 \r
223 void vApplicationIdleHook( void )\r
224 {\r
225         /* vApplicationIdleHook() will only be called if configUSE_IDLE_HOOK is set\r
226         to 1 in FreeRTOSConfig.h.  It will be called on each iteration of the idle\r
227         task.  It is essential that code added to this hook function never attempts\r
228         to block in any way (for example, call xQueueReceive() with a block time\r
229         specified, or call vTaskDelay()).  If the application makes use of the\r
230         vTaskDelete() API function (as this demo application does) then it is also\r
231         important that vApplicationIdleHook() is permitted to return to its calling\r
232         function, because it is the responsibility of the idle task to clean up\r
233         memory allocated by the kernel to any task that has since been deleted. */\r
234 \r
235         /* Uncomment the following code to allow the trace to be stopped with any\r
236         key press.  The code is commented out by default as the kbhit() function\r
237         interferes with the run time behaviour. */\r
238         /*\r
239                 if( _kbhit() != pdFALSE )\r
240                 {\r
241                         if( xTraceRunning == pdTRUE )\r
242                         {\r
243                                 vTraceStop();\r
244                                 prvSaveTraceFile();\r
245                                 xTraceRunning = pdFALSE;\r
246                         }\r
247                 }\r
248         */\r
249 \r
250         #if ( mainCREATE_SIMPLE_BLINKY_DEMO_ONLY != 1 )\r
251         {\r
252                 /* Call the idle task processing used by the full demo.  The simple\r
253                 blinky demo does not use the idle task hook. */\r
254                 vFullDemoIdleFunction();\r
255         }\r
256         #endif\r
257 }\r
258 /*-----------------------------------------------------------*/\r
259 \r
260 void vApplicationStackOverflowHook( TaskHandle_t pxTask, char *pcTaskName )\r
261 {\r
262         ( void ) pcTaskName;\r
263         ( void ) pxTask;\r
264 \r
265         /* Run time stack overflow checking is performed if\r
266         configCHECK_FOR_STACK_OVERFLOW is defined to 1 or 2.  This hook\r
267         function is called if a stack overflow is detected. */\r
268         vAssertCalled( __LINE__, __FILE__ );\r
269 }\r
270 /*-----------------------------------------------------------*/\r
271 \r
272 void vApplicationTickHook( void )\r
273 {\r
274         /* This function will be called by each tick interrupt if\r
275         configUSE_TICK_HOOK is set to 1 in FreeRTOSConfig.h.  User code can be\r
276         added here, but the tick hook is called from an interrupt context, so\r
277         code must not attempt to block, and only the interrupt safe FreeRTOS API\r
278         functions can be used (those that end in FromISR()). */\r
279 \r
280         #if ( mainCREATE_SIMPLE_BLINKY_DEMO_ONLY != 1 )\r
281         {\r
282                 vFullDemoTickHookFunction();\r
283         }\r
284         #endif /* mainCREATE_SIMPLE_BLINKY_DEMO_ONLY */\r
285 \r
286         /* Write a user event to the trace log.\r
287         Note tick events will not appear in the trace recording with regular period\r
288         because this project runs in a Windows simulator, and does not therefore\r
289         exhibit deterministic behaviour.  Windows will run the simulator in\r
290         bursts. */\r
291         vTraceUserEvent( xTickTraceUserEvent );\r
292 }\r
293 /*-----------------------------------------------------------*/\r
294 \r
295 void vAssertCalled( unsigned long ulLine, const char * const pcFileName )\r
296 {\r
297 static portBASE_TYPE xPrinted = pdFALSE;\r
298 volatile uint32_t ulSetToNonZeroInDebuggerToContinue = 0;\r
299 \r
300         /* Parameters are not used. */\r
301         ( void ) ulLine;\r
302         ( void ) pcFileName;\r
303 \r
304         taskENTER_CRITICAL();\r
305         {\r
306                 /* Stop the trace recording. */\r
307                 if( xPrinted == pdFALSE )\r
308                 {\r
309                         xPrinted = pdTRUE;\r
310                         if( xTraceRunning == pdTRUE )\r
311                         {\r
312                                 vTraceStop();\r
313                                 prvSaveTraceFile();\r
314                         }\r
315                 }\r
316 \r
317                 /* You can step out of this function to debug the assertion by using\r
318                 the debugger to set ulSetToNonZeroInDebuggerToContinue to a non-zero\r
319                 value. */\r
320                 while( ulSetToNonZeroInDebuggerToContinue == 0 )\r
321                 {\r
322                         __asm{ NOP };\r
323                         __asm{ NOP };\r
324                 }\r
325         }\r
326         taskEXIT_CRITICAL();\r
327 }\r
328 /*-----------------------------------------------------------*/\r
329 \r
330 static void prvSaveTraceFile( void )\r
331 {\r
332 FILE* pxOutputFile;\r
333 \r
334         fopen_s( &pxOutputFile, "Trace.dump", "wb");\r
335 \r
336         if( pxOutputFile != NULL )\r
337         {\r
338                 fwrite( RecorderDataPtr, sizeof( RecorderDataType ), 1, pxOutputFile );\r
339                 fclose( pxOutputFile );\r
340                 printf( "\r\nTrace output saved to Trace.dump\r\n" );\r
341         }\r
342         else\r
343         {\r
344                 printf( "\r\nFailed to create trace dump file\r\n" );\r
345         }\r
346 }\r
347 /*-----------------------------------------------------------*/\r
348 \r
349 static void  prvInitialiseHeap( void )\r
350 {\r
351 /* This demo uses heap_5.c, so start by defining some heap regions.  This is\r
352 only done to provide an example as this demo could easily create one large heap\r
353 region instead of multiple smaller heap regions - in which case heap_4.c would\r
354 be the more appropriate choice.  No initialisation is required when heap_4.c is\r
355 used.  The xHeapRegions structure requires the regions to be defined in order,\r
356 so this just creates one big array, then populates the structure with offsets\r
357 into the array - with gaps in between and messy alignment just for test\r
358 purposes. */\r
359 static uint8_t ucHeap[ configTOTAL_HEAP_SIZE ];\r
360 volatile uint32_t ulAdditionalOffset = 19; /* Just to prevent 'condition is always true' warnings in configASSERT(). */\r
361 const HeapRegion_t xHeapRegions[] =\r
362 {\r
363         /* Start address with dummy offsets                                             Size */\r
364         { ucHeap + 1,                                                                                   mainREGION_1_SIZE },\r
365         { ucHeap + 15 + mainREGION_1_SIZE,                                              mainREGION_2_SIZE },\r
366         { ucHeap + 19 + mainREGION_1_SIZE + mainREGION_2_SIZE,  mainREGION_3_SIZE },\r
367         { NULL, 0 }\r
368 };\r
369 \r
370         /* Sanity check that the sizes and offsets defined actually fit into the\r
371         array. */\r
372         configASSERT( ( ulAdditionalOffset + mainREGION_1_SIZE + mainREGION_2_SIZE + mainREGION_3_SIZE ) < configTOTAL_HEAP_SIZE );\r
373 \r
374         /* Prevent compiler warnings when configASSERT() is not defined. */\r
375         ( void ) ulAdditionalOffset;\r
376 \r
377         vPortDefineHeapRegions( xHeapRegions );\r
378 }\r
379 /*-----------------------------------------------------------*/\r
380 \r