]> git.sur5r.net Git - freertos/blob - FreeRTOS/Demo/WIN32-MSVC/main.c
FreeRTOS source:
[freertos] / FreeRTOS / Demo / WIN32-MSVC / main.c
1 /*\r
2     FreeRTOS V8.2.3 - 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: Windows will not be running the FreeRTOS demo threads continuously, so\r
83  * do not expect to get real time behaviour from the FreeRTOS Windows port, or\r
84  * this demo application.  Also, the timing information in the FreeRTOS+Trace\r
85  * logs have no meaningful units.  See the documentation page for the Windows\r
86  * port for further information:\r
87  * http://www.freertos.org/FreeRTOS-Windows-Simulator-Emulator-for-Visual-Studio-and-Eclipse-MingW.html\r
88  *\r
89  * This demo was created using Windows XP on a dual core laptop, and has since\r
90  * been maintained using Windows 7 on a quad core laptop.\r
91  *\r
92  *******************************************************************************\r
93  */\r
94 \r
95 /* Standard includes. */\r
96 #include <stdio.h>\r
97 #include <stdlib.h>\r
98 #include <conio.h>\r
99 \r
100 /* FreeRTOS kernel includes. */\r
101 #include "FreeRTOS.h"\r
102 #include "task.h"\r
103 \r
104 /* This project provides two demo applications.  A simple blinky style demo\r
105 application, and a more comprehensive test and demo application.  The\r
106 mainCREATE_SIMPLE_BLINKY_DEMO_ONLY setting is used to select between the two.\r
107 \r
108 If mainCREATE_SIMPLE_BLINKY_DEMO_ONLY is 1 then the blinky demo will be built.\r
109 The blinky demo is implemented and described in main_blinky.c.\r
110 \r
111 If mainCREATE_SIMPLE_BLINKY_DEMO_ONLY is not 1 then the comprehensive test and\r
112 demo application will be built.  The comprehensive test and demo application is\r
113 implemented and described in main_full.c. */\r
114 #define mainCREATE_SIMPLE_BLINKY_DEMO_ONLY      1\r
115 \r
116 /* This demo uses heap_5.c, and these constants define the sizes of the regions\r
117 that make up the total heap.  heap_5 is only used for test and example purposes\r
118 as this demo could easily create one large heap region instead of multiple\r
119 smaller heap regions - in which case heap_4.c would be the more appropriate\r
120 choice.  See http://www.freertos.org/a00111.html for an explanation. */\r
121 #define mainREGION_1_SIZE       4001\r
122 #define mainREGION_2_SIZE       18105\r
123 #define mainREGION_3_SIZE       1807\r
124 \r
125 /*-----------------------------------------------------------*/\r
126 \r
127 /*\r
128  * main_blinky() is used when mainCREATE_SIMPLE_BLINKY_DEMO_ONLY is set to 1.\r
129  * main_full() is used when mainCREATE_SIMPLE_BLINKY_DEMO_ONLY is set to 0.\r
130  */\r
131 extern void main_blinky( void );\r
132 extern void main_full( void );\r
133 \r
134 /*\r
135  * Only the comprehensive demo uses application hook (callback) functions.  See\r
136  * http://www.freertos.org/a00016.html for more information.\r
137  */\r
138 void vFullDemoTickHookFunction( void );\r
139 void vFullDemoIdleFunction( void );\r
140 \r
141 /*\r
142  * This demo uses heap_5.c, so start by defining some heap regions.  It is not\r
143  * necessary for this demo to use heap_5, as it could define one large heap\r
144  * region.  Heap_5 is only used for test and example purposes.  See\r
145  * http://www.freertos.org/a00111.html for an explanation.\r
146  */\r
147 static void  prvInitialiseHeap( void );\r
148 \r
149 /*\r
150  * Prototypes for the standard FreeRTOS application hook (callback) functions\r
151  * implemented within this file.  See http://www.freertos.org/a00016.html .\r
152  */\r
153 void vApplicationMallocFailedHook( void );\r
154 void vApplicationIdleHook( void );\r
155 void vApplicationStackOverflowHook( TaskHandle_t pxTask, char *pcTaskName );\r
156 void vApplicationTickHook( void );\r
157 \r
158 /*\r
159  * Writes trace data to a disk file when the trace recording is stopped.\r
160  * This function will simply overwrite any trace files that already exist.\r
161  */\r
162 static void prvSaveTraceFile( void );\r
163 \r
164 /* The user trace event posted to the trace recording on each tick interrupt.\r
165 Note:  This project runs under Windows, and Windows will not be executing the\r
166 RTOS threads continuously.  Therefore tick events will not appear with a regular\r
167 interval within the the trace recording. */\r
168 traceLabel xTickTraceUserEvent;\r
169 static portBASE_TYPE xTraceRunning = pdTRUE;\r
170 \r
171 /*-----------------------------------------------------------*/\r
172 \r
173 int main( void )\r
174 {\r
175         /* This demo uses heap_5.c, so start by defining some heap regions.  heap_5\r
176         is only used for test and example reasons.  Heap_4 is more appropriate.  See\r
177         http://www.freertos.org/a00111.html for an explanation. */\r
178         prvInitialiseHeap();\r
179 \r
180         /* Initialise the trace recorder and create the label used to post user\r
181         events to the trace recording on each tick interrupt.  Use of the trace\r
182         recorder is optional.  See http://www.FreeRTOS.org/trace for more\r
183         information. */\r
184         vTraceInitTraceData();\r
185         xTickTraceUserEvent = xTraceOpenLabel( "tick" );\r
186 \r
187         /* The mainCREATE_SIMPLE_BLINKY_DEMO_ONLY setting is described at the top\r
188         of this file. */\r
189         #if ( mainCREATE_SIMPLE_BLINKY_DEMO_ONLY == 1 )\r
190         {\r
191                 main_blinky();\r
192         }\r
193         #else\r
194         {\r
195                 /* Start the trace recording - the recording is written to a file if\r
196                 configASSERT() is called. */\r
197                 printf( "\r\nTrace started.\r\nThe trace will be dumped to disk if a call to configASSERT() fails.\r\n" );\r
198                 printf( "Uncomment the call to kbhit() in this file to also dump trace with a key press.\r\n" );\r
199                 uiTraceStart();\r
200 \r
201                 main_full();\r
202         }\r
203         #endif\r
204 \r
205         return 0;\r
206 }\r
207 /*-----------------------------------------------------------*/\r
208 \r
209 void vApplicationMallocFailedHook( void )\r
210 {\r
211         /* vApplicationMallocFailedHook() will only be called if\r
212         configUSE_MALLOC_FAILED_HOOK is set to 1 in FreeRTOSConfig.h.  It is a hook\r
213         function that will get called if a call to pvPortMalloc() fails.\r
214         pvPortMalloc() is called internally by the kernel whenever a task, queue,\r
215         timer or semaphore is created.  It is also called by various parts of the\r
216         demo application.  If heap_1.c, heap_2.c or heap_4.c is being used, then the\r
217         size of the     heap available to pvPortMalloc() is defined by\r
218         configTOTAL_HEAP_SIZE in FreeRTOSConfig.h, and the xPortGetFreeHeapSize()\r
219         API function can be used to query the size of free heap space that remains\r
220         (although it does not provide information on how the remaining heap might be\r
221         fragmented).  See http://www.freertos.org/a00111.html for more\r
222         information. */\r
223         vAssertCalled( __LINE__, __FILE__ );\r
224 }\r
225 /*-----------------------------------------------------------*/\r
226 \r
227 void vApplicationIdleHook( void )\r
228 {\r
229         /* vApplicationIdleHook() will only be called if configUSE_IDLE_HOOK is set\r
230         to 1 in FreeRTOSConfig.h.  It will be called on each iteration of the idle\r
231         task.  It is essential that code added to this hook function never attempts\r
232         to block in any way (for example, call xQueueReceive() with a block time\r
233         specified, or call vTaskDelay()).  If application tasks make use of the\r
234         vTaskDelete() API function to delete themselves then it is also important\r
235         that vApplicationIdleHook() is permitted to return to its calling function,\r
236         because it is the responsibility of the idle task to clean up memory\r
237         allocated by the kernel to any task that has since deleted itself. */\r
238 \r
239         /* Uncomment the following code to allow the trace to be stopped with any\r
240         key press.  The code is commented out by default as the kbhit() function\r
241         interferes with the run time behaviour. */\r
242         /*\r
243                 if( _kbhit() != pdFALSE )\r
244                 {\r
245                         if( xTraceRunning == pdTRUE )\r
246                         {\r
247                                 vTraceStop();\r
248                                 prvSaveTraceFile();\r
249                                 xTraceRunning = pdFALSE;\r
250                         }\r
251                 }\r
252         */\r
253 \r
254         #if ( mainCREATE_SIMPLE_BLINKY_DEMO_ONLY != 1 )\r
255         {\r
256                 /* Call the idle task processing used by the full demo.  The simple\r
257                 blinky demo does not use the idle task hook. */\r
258                 vFullDemoIdleFunction();\r
259         }\r
260         #endif\r
261 }\r
262 /*-----------------------------------------------------------*/\r
263 \r
264 void vApplicationStackOverflowHook( TaskHandle_t pxTask, char *pcTaskName )\r
265 {\r
266         ( void ) pcTaskName;\r
267         ( void ) pxTask;\r
268 \r
269         /* Run time stack overflow checking is performed if\r
270         configCHECK_FOR_STACK_OVERFLOW is defined to 1 or 2.  This hook\r
271         function is called if a stack overflow is detected.  This function is\r
272         provided as an example only as stack overflow checking does not function\r
273         when running the FreeRTOS Windows port. */\r
274         vAssertCalled( __LINE__, __FILE__ );\r
275 }\r
276 /*-----------------------------------------------------------*/\r
277 \r
278 void vApplicationTickHook( void )\r
279 {\r
280         /* This function will be called by each tick interrupt if\r
281         configUSE_TICK_HOOK is set to 1 in FreeRTOSConfig.h.  User code can be\r
282         added here, but the tick hook is called from an interrupt context, so\r
283         code must not attempt to block, and only the interrupt safe FreeRTOS API\r
284         functions can be used (those that end in FromISR()). */\r
285 \r
286         #if ( mainCREATE_SIMPLE_BLINKY_DEMO_ONLY != 1 )\r
287         {\r
288                 vFullDemoTickHookFunction();\r
289         }\r
290         #endif /* mainCREATE_SIMPLE_BLINKY_DEMO_ONLY */\r
291 \r
292         /* Write a user event to the trace log.  Note:  This project runs under\r
293         Windows, and Windows will not be executing the RTOS threads continuously.\r
294         Therefore tick events will not appear with a regular interval within the the\r
295         trace recording. */\r
296         vTraceUserEvent( xTickTraceUserEvent );\r
297 }\r
298 /*-----------------------------------------------------------*/\r
299 \r
300 void vAssertCalled( unsigned long ulLine, const char * const pcFileName )\r
301 {\r
302 static portBASE_TYPE xPrinted = pdFALSE;\r
303 volatile uint32_t ulSetToNonZeroInDebuggerToContinue = 0;\r
304 \r
305         /* Called if an assertion passed to configASSERT() fails.  See\r
306         http://www.freertos.org/a00110.html#configASSERT for more information. */\r
307 \r
308         /* Parameters are not used. */\r
309         ( void ) ulLine;\r
310         ( void ) pcFileName;\r
311 \r
312         printf( "ASSERT! Line %d, file %s\r\n", ulLine, pcFileName );\r
313 \r
314         taskENTER_CRITICAL();\r
315         {\r
316                 /* Stop the trace recording. */\r
317                 if( xPrinted == pdFALSE )\r
318                 {\r
319                         xPrinted = pdTRUE;\r
320                         if( xTraceRunning == pdTRUE )\r
321                         {\r
322                                 vTraceStop();\r
323                                 prvSaveTraceFile();\r
324                         }\r
325                 }\r
326 \r
327                 /* You can step out of this function to debug the assertion by using\r
328                 the debugger to set ulSetToNonZeroInDebuggerToContinue to a non-zero\r
329                 value. */\r
330                 while( ulSetToNonZeroInDebuggerToContinue == 0 )\r
331                 {\r
332                         __asm{ NOP };\r
333                         __asm{ NOP };\r
334                 }\r
335         }\r
336         taskEXIT_CRITICAL();\r
337 }\r
338 /*-----------------------------------------------------------*/\r
339 \r
340 static void prvSaveTraceFile( void )\r
341 {\r
342 FILE* pxOutputFile;\r
343 \r
344         fopen_s( &pxOutputFile, "Trace.dump", "wb");\r
345 \r
346         if( pxOutputFile != NULL )\r
347         {\r
348                 fwrite( RecorderDataPtr, sizeof( RecorderDataType ), 1, pxOutputFile );\r
349                 fclose( pxOutputFile );\r
350                 printf( "\r\nTrace output saved to Trace.dump\r\n" );\r
351         }\r
352         else\r
353         {\r
354                 printf( "\r\nFailed to create trace dump file\r\n" );\r
355         }\r
356 }\r
357 /*-----------------------------------------------------------*/\r
358 \r
359 static void  prvInitialiseHeap( void )\r
360 {\r
361 /* The Windows demo could create one large heap region, in which case it would\r
362 be appropriate to use heap_4.  However, purely for demonstration purposes,\r
363 heap_5 is used instead, so start by defining some heap regions.  No\r
364 initialisation is required when any other heap implementation is used.  See\r
365 http://www.freertos.org/a00111.html for more information.\r
366 \r
367 The xHeapRegions structure requires the regions to be defined in start address\r
368 order, so this just creates one big array, then populates the structure with\r
369 offsets into the array - with gaps in between and messy alignment just for test\r
370 purposes. */\r
371 static uint8_t ucHeap[ configTOTAL_HEAP_SIZE ];\r
372 volatile uint32_t ulAdditionalOffset = 19; /* Just to prevent 'condition is always true' warnings in configASSERT(). */\r
373 const HeapRegion_t xHeapRegions[] =\r
374 {\r
375         /* Start address with dummy offsets                                             Size */\r
376         { ucHeap + 1,                                                                                   mainREGION_1_SIZE },\r
377         { ucHeap + 15 + mainREGION_1_SIZE,                                              mainREGION_2_SIZE },\r
378         { ucHeap + 19 + mainREGION_1_SIZE + mainREGION_2_SIZE,  mainREGION_3_SIZE },\r
379         { NULL, 0 }\r
380 };\r
381 \r
382         /* Sanity check that the sizes and offsets defined actually fit into the\r
383         array. */\r
384         configASSERT( ( ulAdditionalOffset + mainREGION_1_SIZE + mainREGION_2_SIZE + mainREGION_3_SIZE ) < configTOTAL_HEAP_SIZE );\r
385 \r
386         /* Prevent compiler warnings when configASSERT() is not defined. */\r
387         ( void ) ulAdditionalOffset;\r
388 \r
389         vPortDefineHeapRegions( xHeapRegions );\r
390 }\r
391 /*-----------------------------------------------------------*/\r
392 \r