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