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