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